Update POSIX file IO

This commit is contained in:
2026-01-03 03:13:42 +00:00
parent c2ca99cac4
commit 576699996f
4 changed files with 61 additions and 87 deletions

View File

@@ -15,7 +15,7 @@ WFile *wapp_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMo
i64 wapp_file_get_current_position(WFile *file) {
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
return _file_get_current_position(file);
return _file_seek(file, 0, WAPP_SEEK_CURRENT);
}
i64 wapp_file_seek(WFile *file, i64 offset, FileSeekOrigin origin) {
@@ -25,13 +25,29 @@ i64 wapp_file_seek(WFile *file, i64 offset, FileSeekOrigin origin) {
i64 wapp_file_get_length(WFile *file) {
wapp_debug_assert(file != NULL, "`file` should not be NULL.");
return _file_get_length(file);
i64 current = wapp_file_get_current_position(file);
_file_seek(file, 0, WAPP_SEEK_END);
i64 output = wapp_file_get_current_position(file);
// Restore position
_file_seek(file, current, WAPP_SEEK_START);
return output;
}
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.");
return _file_read(dst_buf, file, byte_count);
i64 file_length = wapp_file_get_length(file);
if (file_length < 0) {
return 0;
}
return _file_read(dst_buf, byte_count, file, file_length);
}
i64 wapp_file_write(const void *src_buf, WFile *file, u64 byte_count) {
@@ -43,13 +59,49 @@ 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) {
wapp_debug_assert(dst_buf != NULL && file != NULL,
"`dst_buf` and `file` should not be NULL.");
return _file_read_array(dst_buf, file, item_count);
i64 _file_length = wapp_file_get_length(file);
if (_file_length < 0) {
return 0;
}
u64 file_length = (u64)_file_length;
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, copy_byte_count, file, file_length);
if (byte_count == 0) {
return 0;
}
wapp_array_set_count(dst_buf, byte_count / item_size);
return wapp_array_count(dst_buf);
}
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.");
return _file_write_array(src_buf, file, 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;
i64 bytes_written = _file_write(src_buf, file, to_copy);
if (bytes_written < 0) {
return 0;
}
return (u64)bytes_written / item_size;
}
i32 wapp_file_flush(WFile *file) {

View File

@@ -20,16 +20,8 @@ typedef enum {
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;
@@ -56,13 +48,9 @@ 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 u64 _file_read(void *dst_buf, u64 byte_count, WFile *file, u64 file_length);
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);

View File

@@ -21,16 +21,8 @@ wapp_intern i32 file_flags[FILE_ACCESS_MODE_COUNT] = {
[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] = {
@@ -40,16 +32,8 @@ wapp_intern mode_t file_modes[FILE_ACCESS_MODE_COUNT] = {
[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] = {
@@ -76,29 +60,11 @@ WFile *_file_open(const Allocator *allocator, Str8RO *filepath, FileAccessMode m
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 _file_read(void *dst_buf, u64 byte_count, WFile *file, u64 file_length) {
u64 copy_byte_count = file_length <= byte_count ? file_length : byte_count;
i64 count = read(file->fd, dst_buf, copy_byte_count);
@@ -111,40 +77,6 @@ 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);
}

View File

@@ -12,6 +12,8 @@ BEGIN_C_LINKAGE
#ifdef WAPP_PLATFORM_POSIX
#define END_OF_LINE "\n"
struct WFile {
i32 fd;
};