Implement POSIX file IO
This commit is contained in:
@@ -6,160 +6,72 @@
|
||||
#include "../../common/aliases/aliases.h"
|
||||
#include "../../base/array/array.h"
|
||||
#include "../../base/strings/str8/str8.h"
|
||||
#include <stdio.h>
|
||||
|
||||
WFile *wapp_file_open(Str8RO *filepath, FileAccessMode mode) {
|
||||
wapp_debug_assert(filepath != NULL, "`filepath` should not be NULL");
|
||||
|
||||
wapp_persist const char *modes[FILE_ACCESS_MODE_COUNT] = {
|
||||
[WAPP_FA_MODE_R] = "r",
|
||||
[WAPP_FA_MODE_W] = "w",
|
||||
[WAPP_FA_MODE_A] = "a",
|
||||
[WAPP_FA_MODE_R_EX] = "r+",
|
||||
[WAPP_FA_MODE_W_EX] = "w+",
|
||||
[WAPP_FA_MODE_A_EX] = "a+",
|
||||
[WAPP_FA_MODE_RB] = "rb",
|
||||
[WAPP_FA_MODE_WB] = "wb",
|
||||
[WAPP_FA_MODE_AB] = "ab",
|
||||
[WAPP_FA_MODE_RB_EX] = "rb+",
|
||||
[WAPP_FA_MODE_WB_EX] = "wb+",
|
||||
[WAPP_FA_MODE_AB_EX] = "ab+",
|
||||
[WAPP_FA_MODE_WX] = "wx",
|
||||
[WAPP_FA_MODE_WX_EX] = "wx+",
|
||||
[WAPP_FA_MODE_WBX] = "wbx",
|
||||
[WAPP_FA_MODE_WBX_EX] = "wbx+",
|
||||
};
|
||||
wapp_persist c8 tmp[WAPP_PATH_MAX] = {0};
|
||||
WFile *wapp_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode) {
|
||||
wapp_debug_assert(allocator != NULL && filepath != NULL, "`allocator` and `filepath` should not be NULL");
|
||||
wapp_debug_assert(filepath->size < WAPP_PATH_MAX, "`filepath` exceeds max path limit.");
|
||||
|
||||
memset(tmp, 0, WAPP_PATH_MAX);
|
||||
memcpy(tmp, filepath->buf, filepath->size);
|
||||
|
||||
return fopen((const char *)tmp, modes[mode]);
|
||||
return _file_open(allocator, filepath, mode);
|
||||
}
|
||||
|
||||
i64 wapp_file_get_current_position(WFile *file) {
|
||||
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
||||
return ftell(file);
|
||||
return _file_get_current_position(file);
|
||||
}
|
||||
|
||||
i32 wapp_file_seek(WFile *file, u64 offset, FileSeekOrigin origin) {
|
||||
i64 wapp_file_seek(WFile *file, i64 offset, FileSeekOrigin origin) {
|
||||
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
||||
// TODO (Abdelrahman): Revisit conversion to long
|
||||
return fseek(file, (long)offset, origin);
|
||||
return _file_seek(file, offset, origin);
|
||||
}
|
||||
|
||||
i64 wapp_file_get_length(WFile *file) {
|
||||
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
||||
|
||||
i64 current = wapp_file_get_current_position(file);
|
||||
|
||||
wapp_file_seek(file, 0, WAPP_SEEK_END);
|
||||
|
||||
i64 output = wapp_file_get_current_position(file);
|
||||
|
||||
// Restore position
|
||||
wapp_file_seek(file, current, WAPP_SEEK_START);
|
||||
|
||||
return output;
|
||||
return _file_get_length(file);
|
||||
}
|
||||
|
||||
u64 wapp_file_read(void *dst_buf, WFile *file, u64 byte_count) {
|
||||
wapp_debug_assert(dst_buf != NULL && file != NULL,
|
||||
"`dst_buf` and `file` should not be NULL.");
|
||||
|
||||
u64 file_length = wapp_file_get_length(file);
|
||||
u64 copy_byte_count = file_length <= byte_count ? file_length : byte_count;
|
||||
|
||||
u64 count = fread(dst_buf, sizeof(u8), copy_byte_count, file);
|
||||
if (ferror(file)) { return 0; }
|
||||
|
||||
return count;
|
||||
return _file_read(dst_buf, file, byte_count);
|
||||
}
|
||||
|
||||
u64 wapp_file_write(const void *src_buf, WFile *file, u64 byte_count) {
|
||||
i64 wapp_file_write(const void *src_buf, WFile *file, u64 byte_count) {
|
||||
wapp_debug_assert(src_buf != NULL && file != NULL,
|
||||
"`src_buf` and `file` should not be NULL.");
|
||||
|
||||
return fwrite(src_buf, sizeof(u8), byte_count, file);
|
||||
return _file_write(src_buf, file, byte_count);
|
||||
}
|
||||
|
||||
u64 wapp_file_read_array(GenericArray dst_buf, WFile *file, u64 item_count) {
|
||||
wapp_debug_assert(dst_buf != NULL && file != NULL,
|
||||
"`dst_buf` and `file` should not be NULL.");
|
||||
|
||||
u64 file_length = wapp_file_get_length(file);
|
||||
u64 item_size = wapp_array_item_size(dst_buf);
|
||||
u64 dst_byte_capacity = wapp_array_capacity(dst_buf) * item_size;
|
||||
u64 req_byte_count = item_count * item_size;
|
||||
u64 copy_byte_count = 0;
|
||||
|
||||
if (req_byte_count <= file_length && req_byte_count <= dst_byte_capacity) {
|
||||
copy_byte_count = req_byte_count;
|
||||
} else {
|
||||
copy_byte_count = file_length <= dst_byte_capacity ? file_length : dst_byte_capacity;
|
||||
}
|
||||
|
||||
u64 byte_count = wapp_file_read(dst_buf, file, copy_byte_count);
|
||||
if (byte_count == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
wapp_array_set_count(dst_buf, byte_count / item_size);
|
||||
|
||||
return wapp_array_count(dst_buf);
|
||||
return _file_read_array(dst_buf, file, item_count);
|
||||
}
|
||||
|
||||
u64 wapp_file_write_array(const GenericArray src_buf, WFile *file, u64 item_count) {
|
||||
i64 wapp_file_write_array(const GenericArray src_buf, WFile *file, u64 item_count) {
|
||||
wapp_debug_assert(src_buf != NULL && file != NULL,
|
||||
"`src_buf` and `file` should not be NULL.");
|
||||
|
||||
u64 item_size = wapp_array_item_size(src_buf);
|
||||
u64 src_byte_count = wapp_array_count(src_buf) * item_size;
|
||||
u64 req_byte_count = item_count * item_size;
|
||||
u64 to_copy = req_byte_count <= src_byte_count ? req_byte_count : src_byte_count;
|
||||
|
||||
u64 byte_count = wapp_file_write(src_buf, file, to_copy);
|
||||
|
||||
return byte_count / item_size;
|
||||
return _file_write_array(src_buf, file, item_count);
|
||||
}
|
||||
|
||||
i32 wapp_file_flush(WFile *file) {
|
||||
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
||||
return fflush(file);
|
||||
return _file_flush(file);
|
||||
}
|
||||
|
||||
i32 wapp_file_close(WFile *file) {
|
||||
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
|
||||
return fclose(file);
|
||||
return _file_close(file);
|
||||
}
|
||||
|
||||
i32 wapp_file_rename(Str8RO *old_filepath, Str8RO *new_filepath) {
|
||||
wapp_debug_assert(old_filepath != NULL && new_filepath != NULL,
|
||||
"`old_filepath` and `new_filepath` should not be NULL");
|
||||
|
||||
wapp_persist c8 old_tmp[WAPP_PATH_MAX] = {0};
|
||||
wapp_debug_assert(old_filepath->size < WAPP_PATH_MAX, "`old_filepath` exceeds max path limit.");
|
||||
|
||||
wapp_persist c8 new_tmp[WAPP_PATH_MAX] = {0};
|
||||
wapp_debug_assert(new_filepath->size < WAPP_PATH_MAX, "`new_filepath` exceeds max path limit.");
|
||||
|
||||
memset(old_tmp, 0, WAPP_PATH_MAX);
|
||||
memcpy(old_tmp, old_filepath->buf, old_filepath->size);
|
||||
memset(new_tmp, 0, WAPP_PATH_MAX);
|
||||
memcpy(new_tmp, new_filepath->buf, new_filepath->size);
|
||||
|
||||
return rename((const char *)old_tmp, (const char *)new_tmp);
|
||||
return _file_rename(old_filepath, new_filepath);
|
||||
}
|
||||
|
||||
i32 wapp_file_remove(Str8RO *filepath) {
|
||||
wapp_debug_assert(filepath != NULL, "`filepath` should not be NULL");
|
||||
|
||||
wapp_persist c8 tmp[WAPP_PATH_MAX] = {0};
|
||||
wapp_debug_assert(filepath->size < WAPP_PATH_MAX, "`filepath` exceeds max path limit.");
|
||||
|
||||
memset(tmp, 0, WAPP_PATH_MAX);
|
||||
memcpy(tmp, filepath->buf, filepath->size);
|
||||
|
||||
return remove((const char *)tmp);
|
||||
return _file_remove(filepath);
|
||||
}
|
||||
|
||||
@@ -3,56 +3,71 @@
|
||||
#ifndef FILE_H
|
||||
#define FILE_H
|
||||
|
||||
#include "../../base/mem/allocator/mem_allocator.h"
|
||||
#include "../../common/aliases/aliases.h"
|
||||
#include "../../base/strings/str8/str8.h"
|
||||
#include <stdio.h>
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
BEGIN_C_LINKAGE
|
||||
#endif // !WAPP_PLATFORM_CPP
|
||||
|
||||
typedef FILE WFile;
|
||||
typedef struct WFile WFile;
|
||||
|
||||
typedef enum {
|
||||
WAPP_FA_MODE_R, // Equivalent to r
|
||||
WAPP_FA_MODE_W, // Equivalent to w
|
||||
WAPP_FA_MODE_A, // Equivalent to a
|
||||
WAPP_FA_MODE_R_EX, // Equivalent to r+
|
||||
WAPP_FA_MODE_W_EX, // Equivalent to w+
|
||||
WAPP_FA_MODE_A_EX, // Equivalent to a+
|
||||
WAPP_FA_MODE_RB, // Equivalent to rb
|
||||
WAPP_FA_MODE_WB, // Equivalent to wb
|
||||
WAPP_FA_MODE_AB, // Equivalent to ab
|
||||
WAPP_FA_MODE_RB_EX, // Equivalent to rb+
|
||||
WAPP_FA_MODE_WB_EX, // Equivalent to wb+
|
||||
WAPP_FA_MODE_AB_EX, // Equivalent to ab+
|
||||
WAPP_FA_MODE_WX, // Equivalent to wx
|
||||
WAPP_FA_MODE_WX_EX, // Equivalent to wx+
|
||||
WAPP_FA_MODE_WBX, // Equivalent to wbx
|
||||
WAPP_FA_MODE_WBX_EX, // Equivalent to wbx+
|
||||
WAPP_ACCESS_READ, // Equivalent to r
|
||||
WAPP_ACCESS_WRITE, // Equivalent to w
|
||||
WAPP_ACCESS_APPEND, // Equivalent to a
|
||||
WAPP_ACCESS_READ_EX, // Equivalent to r+
|
||||
WAPP_ACCESS_WRITE_EX, // Equivalent to w+
|
||||
WAPP_ACCESS_APPEND_EX, // Equivalent to a+
|
||||
WAPP_ACCESS_READ_BIN, // Equivalent to rb
|
||||
WAPP_ACCESS_WRITE_BIN, // Equivalent to wb
|
||||
WAPP_ACCESS_APPEND_BIN, // Equivalent to ab
|
||||
WAPP_ACCESS_READ_BIN_EX, // Equivalent to rb+
|
||||
WAPP_ACCESS_WRITE_BIN_EX, // Equivalent to wb+
|
||||
WAPP_ACCESS_APPEND_BIN_EX, // Equivalent to ab+
|
||||
WAPP_ACCESS_WRITE_FAIL_ON_EXIST, // Equivalent to wx
|
||||
WAPP_ACCESS_WRITE_FAIL_ON_EXIST_EX, // Equivalent to wx+
|
||||
WAPP_ACCESS_WRITE_BIN_FAIL_ON_EXIST, // Equivalent to wbx
|
||||
WAPP_ACCESS_WRITE_BIN_FAIL_ON_EXIST_EX, // Equivalent to wbx+
|
||||
|
||||
FILE_ACCESS_MODE_COUNT,
|
||||
} FileAccessMode;
|
||||
|
||||
typedef enum {
|
||||
WAPP_SEEK_START = SEEK_SET,
|
||||
WAPP_SEEK_CURRENT = SEEK_CUR,
|
||||
WAPP_SEEK_END = SEEK_END,
|
||||
WAPP_SEEK_START,
|
||||
WAPP_SEEK_CURRENT,
|
||||
WAPP_SEEK_END,
|
||||
|
||||
FILE_SEEK_ORIGIN_COUNT,
|
||||
} FileSeekOrigin;
|
||||
|
||||
WFile *wapp_file_open(Str8RO *filename, FileAccessMode mode);
|
||||
WFile *wapp_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode);
|
||||
i64 wapp_file_get_current_position(WFile *file);
|
||||
i32 wapp_file_seek(WFile *file, u64 offset, FileSeekOrigin origin);
|
||||
i64 wapp_file_seek(WFile *file, i64 offset, FileSeekOrigin origin);
|
||||
i64 wapp_file_get_length(WFile *file);
|
||||
u64 wapp_file_read(void *dst_buf, WFile *file, u64 byte_count);
|
||||
u64 wapp_file_write(const void *src_buf, WFile *file, u64 byte_count);
|
||||
i64 wapp_file_write(const void *src_buf, WFile *file, u64 byte_count);
|
||||
u64 wapp_file_read_array(GenericArray dst_buf, WFile *file, u64 item_count);
|
||||
u64 wapp_file_write_array(const GenericArray src_buf, WFile *file, u64 item_count);
|
||||
i64 wapp_file_write_array(const GenericArray src_buf, WFile *file, u64 item_count);
|
||||
i32 wapp_file_flush(WFile *file);
|
||||
i32 wapp_file_close(WFile *file);
|
||||
i32 wapp_file_rename(Str8RO *old_filepath, Str8RO *new_filepath);
|
||||
i32 wapp_file_remove(Str8RO *filepath);
|
||||
|
||||
extern WFile *_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode);
|
||||
extern i64 _file_get_current_position(WFile *file);
|
||||
extern i64 _file_seek(WFile *file, i64 offset, FileSeekOrigin origin);
|
||||
extern i64 _file_get_length(WFile *file);
|
||||
extern u64 _file_read(void *dst_buf, WFile *file, u64 byte_count);
|
||||
extern i64 _file_write(const void *src_buf, WFile *file, u64 byte_count);
|
||||
extern u64 _file_read_array(GenericArray dst_buf, WFile *file, u64 item_count);
|
||||
extern i64 _file_write_array(const GenericArray src_buf, WFile *file, u64 item_count);
|
||||
extern i32 _file_flush(WFile *file);
|
||||
extern i32 _file_close(WFile *file);
|
||||
extern i32 _file_rename(Str8RO *old_filepath, Str8RO *new_filepath);
|
||||
extern i32 _file_remove(Str8RO *filepath);
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
END_C_LINKAGE
|
||||
#endif // !WAPP_PLATFORM_CPP
|
||||
|
||||
180
src/os/file/posix/file_posix.c
Normal file
180
src/os/file/posix/file_posix.c
Normal file
@@ -0,0 +1,180 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#include "file_posix.h"
|
||||
#include "../../../common/platform/platform.h"
|
||||
|
||||
#ifdef WAPP_PLATFORM_POSIX
|
||||
|
||||
#include "../file.h"
|
||||
#include "../../cpath/cpath.h"
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../base/array/array.h"
|
||||
#include "../../../base/strings/str8/str8.h"
|
||||
#include <fcntl.h>
|
||||
#include <unistd.h>
|
||||
#include <sys/types.h>
|
||||
|
||||
wapp_intern i32 file_flags[FILE_ACCESS_MODE_COUNT] = {
|
||||
[WAPP_ACCESS_READ] = O_RDONLY,
|
||||
[WAPP_ACCESS_WRITE] = O_WRONLY | O_CREAT,
|
||||
[WAPP_ACCESS_APPEND] = O_WRONLY | O_APPEND | O_CREAT,
|
||||
[WAPP_ACCESS_READ_EX] = O_RDWR,
|
||||
[WAPP_ACCESS_WRITE_EX] = O_RDWR | O_CREAT,
|
||||
[WAPP_ACCESS_APPEND_EX] = O_RDWR | O_APPEND | O_CREAT,
|
||||
[WAPP_ACCESS_READ_BIN] = O_RDONLY,
|
||||
[WAPP_ACCESS_WRITE_BIN] = O_WRONLY | O_CREAT,
|
||||
[WAPP_ACCESS_APPEND_BIN] = O_WRONLY | O_APPEND | O_CREAT,
|
||||
[WAPP_ACCESS_READ_BIN_EX] = O_RDWR,
|
||||
[WAPP_ACCESS_WRITE_BIN_EX] = O_RDWR | O_CREAT,
|
||||
[WAPP_ACCESS_APPEND_BIN_EX] = O_RDWR | O_APPEND | O_CREAT,
|
||||
[WAPP_ACCESS_WRITE_FAIL_ON_EXIST] = O_WRONLY | O_CREAT | O_EXCL,
|
||||
[WAPP_ACCESS_WRITE_FAIL_ON_EXIST_EX] = O_RDWR | O_CREAT | O_EXCL,
|
||||
[WAPP_ACCESS_WRITE_BIN_FAIL_ON_EXIST] = O_WRONLY | O_CREAT | O_EXCL,
|
||||
[WAPP_ACCESS_WRITE_BIN_FAIL_ON_EXIST_EX] = O_RDWR | O_CREAT | O_EXCL,
|
||||
};
|
||||
|
||||
wapp_intern mode_t file_modes[FILE_ACCESS_MODE_COUNT] = {
|
||||
[WAPP_ACCESS_READ] = 0,
|
||||
[WAPP_ACCESS_WRITE] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
|
||||
[WAPP_ACCESS_APPEND] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
|
||||
[WAPP_ACCESS_READ_EX] = 0,
|
||||
[WAPP_ACCESS_WRITE_EX] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
|
||||
[WAPP_ACCESS_APPEND_EX] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
|
||||
[WAPP_ACCESS_READ_BIN] = 0,
|
||||
[WAPP_ACCESS_WRITE_BIN] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
|
||||
[WAPP_ACCESS_APPEND_BIN] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
|
||||
[WAPP_ACCESS_READ_BIN_EX] = 0,
|
||||
[WAPP_ACCESS_WRITE_BIN_EX] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
|
||||
[WAPP_ACCESS_APPEND_BIN_EX] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
|
||||
[WAPP_ACCESS_WRITE_FAIL_ON_EXIST] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
|
||||
[WAPP_ACCESS_WRITE_FAIL_ON_EXIST_EX] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
|
||||
[WAPP_ACCESS_WRITE_BIN_FAIL_ON_EXIST] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
|
||||
[WAPP_ACCESS_WRITE_BIN_FAIL_ON_EXIST_EX] = S_IRUSR | S_IWUSR | S_IRGRP | S_IWGRP,
|
||||
};
|
||||
|
||||
wapp_intern i32 file_seek_origins[FILE_SEEK_ORIGIN_COUNT] = {
|
||||
[WAPP_SEEK_START] = SEEK_SET,
|
||||
[WAPP_SEEK_CURRENT] = SEEK_CUR,
|
||||
[WAPP_SEEK_END] = SEEK_END,
|
||||
};
|
||||
|
||||
WFile *_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode mode) {
|
||||
wapp_persist c8 tmp[WAPP_PATH_MAX] = {0};
|
||||
memset(tmp, 0, WAPP_PATH_MAX);
|
||||
memcpy(tmp, filepath->buf, filepath->size);
|
||||
|
||||
i32 fd = open((const char *)tmp, file_flags[mode], file_modes[mode]);
|
||||
if (fd < 0) {
|
||||
return NULL;
|
||||
}
|
||||
|
||||
WFile *output = wapp_mem_allocator_alloc(allocator, sizeof(WFile));
|
||||
if (output) {
|
||||
output->fd = fd;
|
||||
}
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
i64 _file_get_current_position(WFile *file) {
|
||||
return lseek64(file->fd, 0, WAPP_SEEK_CURRENT);
|
||||
}
|
||||
|
||||
i64 _file_seek(WFile *file, i64 offset, FileSeekOrigin origin) {
|
||||
return lseek64(file->fd, offset, file_seek_origins[origin]);
|
||||
}
|
||||
|
||||
i64 _file_get_length(WFile *file) {
|
||||
i64 current = _file_get_current_position(file);
|
||||
|
||||
_file_seek(file, 0, WAPP_SEEK_END);
|
||||
|
||||
i64 output = _file_get_current_position(file);
|
||||
|
||||
// Restore position
|
||||
_file_seek(file, current, WAPP_SEEK_START);
|
||||
|
||||
return output;
|
||||
}
|
||||
|
||||
u64 _file_read(void *dst_buf, WFile *file, u64 byte_count) {
|
||||
u64 file_length = _file_get_length(file);
|
||||
u64 copy_byte_count = file_length <= byte_count ? file_length : byte_count;
|
||||
|
||||
i64 count = read(file->fd, dst_buf, copy_byte_count);
|
||||
if (count < 0) { return 0; }
|
||||
|
||||
return count;
|
||||
}
|
||||
|
||||
i64 _file_write(const void *src_buf, WFile *file, u64 byte_count) {
|
||||
return write(file->fd, src_buf, byte_count);
|
||||
}
|
||||
|
||||
u64 _file_read_array(GenericArray dst_buf, WFile *file, u64 item_count) {
|
||||
u64 file_length = _file_get_length(file);
|
||||
u64 item_size = wapp_array_item_size(dst_buf);
|
||||
u64 dst_byte_capacity = wapp_array_capacity(dst_buf) * item_size;
|
||||
u64 req_byte_count = item_count * item_size;
|
||||
u64 copy_byte_count = 0;
|
||||
|
||||
if (req_byte_count <= file_length && req_byte_count <= dst_byte_capacity) {
|
||||
copy_byte_count = req_byte_count;
|
||||
} else {
|
||||
copy_byte_count = file_length <= dst_byte_capacity ? file_length : dst_byte_capacity;
|
||||
}
|
||||
|
||||
u64 byte_count = _file_read(dst_buf, file, copy_byte_count);
|
||||
if (byte_count == 0) {
|
||||
return 0;
|
||||
}
|
||||
|
||||
wapp_array_set_count(dst_buf, byte_count / item_size);
|
||||
|
||||
return wapp_array_count(dst_buf);
|
||||
}
|
||||
|
||||
i64 _file_write_array(const GenericArray src_buf, WFile *file, u64 item_count) {
|
||||
u64 item_size = wapp_array_item_size(src_buf);
|
||||
u64 src_byte_count = wapp_array_count(src_buf) * item_size;
|
||||
u64 req_byte_count = item_count * item_size;
|
||||
u64 to_copy = req_byte_count <= src_byte_count ? req_byte_count : src_byte_count;
|
||||
|
||||
u64 byte_count = _file_write(src_buf, file, to_copy);
|
||||
|
||||
return byte_count / item_size;
|
||||
}
|
||||
|
||||
i32 _file_flush(WFile *file) {
|
||||
return fsync(file->fd);
|
||||
}
|
||||
|
||||
i32 _file_close(WFile *file) {
|
||||
return close(file->fd);
|
||||
}
|
||||
|
||||
i32 _file_rename(Str8RO *old_filepath, Str8RO *new_filepath) {
|
||||
wapp_persist c8 old_tmp[WAPP_PATH_MAX] = {0};
|
||||
wapp_persist c8 new_tmp[WAPP_PATH_MAX] = {0};
|
||||
memset(old_tmp, 0, WAPP_PATH_MAX);
|
||||
memcpy(old_tmp, old_filepath->buf, old_filepath->size);
|
||||
memset(new_tmp, 0, WAPP_PATH_MAX);
|
||||
memcpy(new_tmp, new_filepath->buf, new_filepath->size);
|
||||
|
||||
i32 link_result = link((const char *)old_tmp, (const char *)new_tmp);
|
||||
if (link_result == 0) {
|
||||
_file_remove(old_filepath);
|
||||
}
|
||||
|
||||
return link_result;
|
||||
}
|
||||
|
||||
i32 _file_remove(Str8RO *filepath) {
|
||||
wapp_persist c8 tmp[WAPP_PATH_MAX] = {0};
|
||||
memset(tmp, 0, WAPP_PATH_MAX);
|
||||
memcpy(tmp, filepath->buf, filepath->size);
|
||||
|
||||
return unlink((const char *)tmp);
|
||||
}
|
||||
|
||||
#endif // !WAPP_PLATFORM_POSIX
|
||||
25
src/os/file/posix/file_posix.h
Normal file
25
src/os/file/posix/file_posix.h
Normal file
@@ -0,0 +1,25 @@
|
||||
// vim:fileencoding=utf-8:foldmethod=marker
|
||||
|
||||
#ifndef FILE_POSIX_H
|
||||
#define FILE_POSIX_H
|
||||
|
||||
#include "../../../common/aliases/aliases.h"
|
||||
#include "../../../common/platform/platform.h"
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
BEGIN_C_LINKAGE
|
||||
#endif // !WAPP_PLATFORM_CPP
|
||||
|
||||
#ifdef WAPP_PLATFORM_POSIX
|
||||
|
||||
struct WFile {
|
||||
i32 fd;
|
||||
};
|
||||
|
||||
#endif // !WAPP_PLATFORM_POSIX
|
||||
|
||||
#ifdef WAPP_PLATFORM_CPP
|
||||
END_C_LINKAGE
|
||||
#endif // !WAPP_PLATFORM_CPP
|
||||
|
||||
#endif // !FILE_POSIX_H
|
||||
@@ -5,6 +5,7 @@
|
||||
|
||||
#include "wapp_os.h"
|
||||
#include "file/file.c"
|
||||
#include "file/posix/file_posix.c"
|
||||
#include "shell/termcolour/posix/termcolour_posix.c"
|
||||
#include "shell/termcolour/win/termcolour_win.c"
|
||||
#include "shell/termcolour/termcolour.c"
|
||||
|
||||
@@ -4,6 +4,7 @@
|
||||
#define WAPP_CORE_H
|
||||
|
||||
#include "file/file.h"
|
||||
#include "file/posix/file_posix.h"
|
||||
#include "shell/termcolour/termcolour.h"
|
||||
#include "shell/termcolour/terminal_colours.h"
|
||||
#include "shell/commander/commander.h"
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "test_file.h"
|
||||
|
||||
#define DST_CAPACITY 5
|
||||
wapp_intern Allocator arena = {0};
|
||||
wapp_intern Str8RO test_filename = wapp_str8_lit_ro_initialiser_list("wapptest.bin");
|
||||
wapp_intern Str8RO new_filename = wapp_str8_lit_ro_initialiser_list("wapptest2.bin");
|
||||
wapp_intern WFile *test_fp = NULL;
|
||||
@@ -13,7 +14,8 @@ wapp_intern I32Array dst_array = wapp_array_with_capacity(i32, DST_CAPACI
|
||||
wapp_intern i32 dst_buf[DST_CAPACITY] = {0};
|
||||
|
||||
TestFuncResult test_wapp_file_open(void) {
|
||||
test_fp = wapp_file_open(&test_filename, WAPP_FA_MODE_WB_EX);
|
||||
arena = wapp_mem_arena_allocator_init(KiB(16));
|
||||
test_fp = wapp_file_open(&arena, &test_filename, WAPP_ACCESS_WRITE_BIN_EX);
|
||||
return wapp_tester_result(test_fp != NULL);
|
||||
}
|
||||
|
||||
@@ -25,8 +27,8 @@ TestFuncResult test_wapp_file_get_current_position(void) {
|
||||
TestFuncResult test_wapp_file_seek(void) {
|
||||
wapp_file_write_array((GenericArray)src_array1, test_fp, wapp_array_count(src_array1));
|
||||
|
||||
i32 seek_result = wapp_file_seek(test_fp, 0, WAPP_SEEK_END);
|
||||
b8 result = seek_result == 0;
|
||||
i64 seek_result = wapp_file_seek(test_fp, 0, WAPP_SEEK_END);
|
||||
b8 result = seek_result == (i64)(wapp_array_count(src_array1) * wapp_array_item_size(src_array1));
|
||||
|
||||
wapp_file_seek(test_fp, 0, WAPP_SEEK_START);
|
||||
|
||||
|
||||
@@ -3,6 +3,7 @@
|
||||
#include "test_file.h"
|
||||
|
||||
#define DST_CAPACITY 5
|
||||
wapp_intern Allocator arena = {};
|
||||
wapp_intern Str8RO test_filename = wapp_str8_lit_ro_initialiser_list("wapptest.bin");
|
||||
wapp_intern Str8RO new_filename = wapp_str8_lit_ro_initialiser_list("wapptest2.bin");
|
||||
wapp_intern WFile *test_fp = NULL;
|
||||
@@ -13,11 +14,12 @@ wapp_intern I32Array src_array3;
|
||||
wapp_intern I32Array dst_array ;
|
||||
|
||||
TestFuncResult test_wapp_file_open(void) {
|
||||
arena = wapp_mem_arena_allocator_init(KiB(16));
|
||||
src_array1 = wapp_array(i32, 0, 1, 2, 3, 4);
|
||||
src_array2 = wapp_array(i32, 5, 6, 7, 8, 9);
|
||||
src_array3 = wapp_array(i32, 10, 11, 12, 13, 14);
|
||||
dst_array = wapp_array_with_capacity(i32, DST_CAPACITY, false);
|
||||
test_fp = wapp_file_open(&test_filename, WAPP_FA_MODE_WB_EX);
|
||||
test_fp = wapp_file_open(&arena, &test_filename, WAPP_ACCESS_WRITE_BIN_EX);
|
||||
return wapp_tester_result(test_fp != NULL);
|
||||
}
|
||||
|
||||
@@ -29,8 +31,8 @@ TestFuncResult test_wapp_file_get_current_position(void) {
|
||||
TestFuncResult test_wapp_file_seek(void) {
|
||||
wapp_file_write_array((GenericArray)src_array1, test_fp, wapp_array_count(src_array1));
|
||||
|
||||
i32 seek_result = wapp_file_seek(test_fp, 0, WAPP_SEEK_END);
|
||||
b8 result = seek_result == 0;
|
||||
i64 seek_result = wapp_file_seek(test_fp, 0, WAPP_SEEK_END);
|
||||
b8 result = seek_result == (i64)(wapp_array_count(src_array1) * wapp_array_item_size(src_array1));
|
||||
|
||||
wapp_file_seek(test_fp, 0, WAPP_SEEK_START);
|
||||
|
||||
|
||||
Reference in New Issue
Block a user