diff options
| author | 2025-01-17 11:28:50 -0500 | |
|---|---|---|
| committer | 2025-01-17 11:28:50 -0500 | |
| commit | cef6cabce7ae97e9edfb6821ce4393d714579866 (patch) | |
| tree | ddae72f25aa290a47308c7c955fb647cf9d759fe /src/util/file | |
| parent | 86dd8affaff59cf9d2657ad1c04fd62571b9a470 (diff) | |
| download | libnaunet-cef6cabce7ae97e9edfb6821ce4393d714579866.tar.gz libnaunet-cef6cabce7ae97e9edfb6821ce4393d714579866.tar.bz2 libnaunet-cef6cabce7ae97e9edfb6821ce4393d714579866.zip | |
Incorporate libalabaster changes, Windows fixes
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'src/util/file')
| -rw-r--r-- | src/util/file/file.h | 8 | ||||
| -rw-r--r-- | src/util/file/file_linux.c | 91 | ||||
| -rw-r--r-- | src/util/file/file_stdio.c | 47 | ||||
| -rw-r--r-- | src/util/file/util.c | 8 |
4 files changed, 95 insertions, 59 deletions
diff --git a/src/util/file/file.h b/src/util/file/file.h index f79bfe5..13a4165 100644 --- a/src/util/file/file.h +++ b/src/util/file/file.h @@ -30,6 +30,7 @@ enum { }; struct nn_file { + s32 flags; #ifdef NAUNET_NEEDS_STDIO_ASSIST FILE *file; #else @@ -38,7 +39,6 @@ struct nn_file { #else s32 fd; #endif - bool locked; #endif str path; size_t size; @@ -65,8 +65,6 @@ struct nn_dir_entry { struct dirent *entry; #endif #endif - str name; - str path; }; bool nn_file_open(struct nn_file *file, str *path, s32 flags); @@ -90,4 +88,6 @@ void nn_dir_close(struct nn_dir *dir); bool nn_dir_exists(str *path); bool nn_dir_create(str *path); bool nn_dir_read(struct nn_dir *dir, struct nn_dir_entry *entry); -void nn_dir_entry_free(struct nn_dir_entry *entry); +void nn_dir_entry_get_name(struct nn_dir_entry *entry, str *name); +const char *nn_dir_entry_get_os_name(struct nn_dir_entry *entry); +void nn_dir_entry_get_path(struct nn_dir_entry *entry, struct nn_dir *dir, str *path); diff --git a/src/util/file/file_linux.c b/src/util/file/file_linux.c index bbc4c67..6b52c60 100644 --- a/src/util/file/file_linux.c +++ b/src/util/file/file_linux.c @@ -26,19 +26,25 @@ static bool unlock_file_internal(s32 fd, size_t size) return true; } +static bool open_file_linux(s32 *fd, str *path, s32 flags) +{ + char *c_str = al_str_to_c_str(path); + bool success = (*fd = open(c_str, flags, 0644)) != -1; + al_free(c_str); + return success; +} + bool nn_file_open(struct nn_file *file, str *path, s32 flags) { - al_assert(!((flags & NNWT_FILE_READONLY) && (flags & NNWT_FILE_LOCK))); + file->flags = flags; s32 oflags = flags & NNWT_FILE_READONLY ? O_RDONLY : O_RDWR; if (flags & NNWT_FILE_CREATE) { oflags |= O_CREAT; } - char *c_str = al_str_to_c_str(path); - file->fd = open(c_str, oflags, 0644); - al_free(c_str); - if (file->fd == -1) { - al_log_error("file", "open(%.*s) failed (%d: %s).", AL_STR_PRINTF(path), errno, nn_strerror(errno)); + if (!open_file_linux(&file->fd, path, oflags)) { + al_log_error("file", "open(%.*s) failed (%d: %s).", + al_str_fmt(path), errno, nn_strerror(errno)); return false; } @@ -51,8 +57,7 @@ bool nn_file_open(struct nn_file *file, str *path, s32 flags) } file->size = sb.st_size; - file->locked = flags & NNWT_FILE_LOCK; - if (file->locked && !lock_file_internal(file->fd, file->size)) { + if (flags & NNWT_FILE_LOCK && !lock_file_internal(file->fd, file->size)) { close(file->fd); return false; } @@ -72,19 +77,18 @@ bool nn_file_exists(str *path) bool nn_file_create(str *path) { - struct nn_file file; - if (nn_file_open(&file, path, NNWT_FILE_CREATE)) { - nn_file_close(&file); - return true; - } - return false; + s32 fd; + bool opened = open_file_linux(&fd, path, O_RDWR | O_CREAT); + if (opened) close(fd); + return opened; } off_t nn_file_seek(struct nn_file *file, off_t offset, s32 whence) { off_t res = lseek(file->fd, offset, whence); if (res == (off_t)-1) { - al_log_error("file", "lseek(%jd, %d) failed (%s).", (intmax_t)offset, whence, nn_strerror(errno)); + al_log_error("file", "lseek(%jd, %d) failed (%s).", + (intmax_t)offset, whence, nn_strerror(errno)); } return res; } @@ -93,7 +97,8 @@ bool nn_file_truncate(struct nn_file *file, off_t size) { s32 ret = ftruncate(file->fd, size); if (ret != 0) { - al_log_error("file", "ftruncate(%jd) failed (%s).", (intmax_t)size, nn_strerror(errno)); + al_log_error("file", "ftruncate(%jd) failed (%s).", + (intmax_t)size, nn_strerror(errno)); return false; } file->size = size; @@ -130,9 +135,11 @@ bool nn_file_read(struct nn_file *file, void *buf, size_t size) void *nn_file_mmap(struct nn_file *file) { - void *map = mmap(NULL, file->size, PROT_READ | PROT_WRITE, MAP_SHARED, file->fd, 0L); + s32 flags = PROT_READ | (file->flags & NNWT_FILE_READONLY ? 0 : PROT_WRITE); + void *map = mmap(NULL, file->size, flags, MAP_SHARED, file->fd, 0L); if (map == MAP_FAILED) { - al_log_error("file", "mmap(%.*s) failed (%s).", AL_STR_PRINTF(&file->path), nn_strerror(errno)); + al_log_error("file", "mmap(%.*s) failed (%s).", + al_str_fmt(&file->path), nn_strerror(errno)); return NULL; } return map; @@ -153,13 +160,14 @@ void nn_file_munmap(struct nn_file *file, void *map) { s32 ret = munmap(map, file->size); if (ret != 0) { - al_log_error("file", "munmap(%p, %zu) failed (%s).", map, file->size, nn_strerror(errno)); + al_log_error("file", "munmap(%p, %zu) failed (%s).", + map, file->size, nn_strerror(errno)); } } void nn_file_close(struct nn_file *file) { - if (file->locked) { + if (file->flags & NNWT_FILE_LOCK) { unlock_file_internal(file->fd, file->size); } close(file->fd); @@ -172,7 +180,8 @@ bool nn_dir_open(struct nn_dir *dir, str *path) dir->dir = opendir(c_str); al_free(c_str); if (!dir->dir) { - al_log_error("file", "opendir(%.*s) failed (%s).", AL_STR_PRINTF(path), nn_strerror(errno)); + al_log_error("file", "opendir(%.*s) failed (%s).", + al_str_fmt(path), nn_strerror(errno)); return false; } al_str_clone(&dir->path, path); @@ -187,17 +196,18 @@ void nn_dir_close(struct nn_dir *dir) bool nn_dir_exists(str *path) { - struct nn_dir dir; + DIR *dir; char *c_str = al_str_to_c_str(path); - dir.dir = opendir(c_str); + dir = opendir(c_str); al_free(c_str); - if (!dir.dir) { + if (!dir) { if (errno != ENOENT) { - al_log_error("file", "opendir(%.*s) failed (%s).", AL_STR_PRINTF(path), nn_strerror(errno)); + al_log_error("file", "opendir(%.*s) failed (%s).", + al_str_fmt(path), nn_strerror(errno)); } return false; } - closedir(dir.dir); + closedir(dir); return true; } @@ -207,7 +217,8 @@ bool nn_dir_create(str *path) s32 ret = mkdir(c_str, 0755); al_free(c_str); if (ret == -1) { - al_log_error("file", "mkdir(%.*s) failed (%s).", AL_STR_PRINTF(path), nn_strerror(errno)); + al_log_error("file", "mkdir(%.*s) failed (%s).", + al_str_fmt(path), nn_strerror(errno)); return false; } return true; @@ -215,9 +226,11 @@ bool nn_dir_create(str *path) bool nn_dir_read(struct nn_dir *dir, struct nn_dir_entry *entry) { + errno = 0; if (!(entry->entry = readdir(dir->dir))) { if (errno) { - al_log_error("file", "readdir() failed (%s).", nn_strerror(errno)); + al_log_error("file", "readdir(%.*s) failed (%s).", + al_str_fmt(&dir->path), nn_strerror(errno)); } return false; } @@ -235,16 +248,22 @@ bool nn_dir_read(struct nn_dir *dir, struct nn_dir_entry *entry) break; } - al_str_clone(&entry->name, al_str_cr(entry->entry->d_name)); - al_str_clone(&entry->path, &dir->path); - al_str_cat(&entry->path, al_str_c("/")); - al_str_cat(&entry->path, &entry->name); - return true; } -void nn_dir_entry_free(struct nn_dir_entry *entry) +void nn_dir_entry_get_name(struct nn_dir_entry *entry, str *name) +{ + al_str_clone(name, &al_str_cr(entry->entry->d_name)); +} + +const char *nn_dir_entry_get_os_name(struct nn_dir_entry *entry) +{ + return entry->entry->d_name; +} + +void nn_dir_entry_get_path(struct nn_dir_entry *entry, struct nn_dir *dir, str *path) { - al_str_free(&entry->name); - al_str_free(&entry->path); + al_str_clone(path, &dir->path); + al_str_cat(path, &al_str_c("/")); + al_str_cat(path, &al_str_cr(entry->entry->d_name)); } diff --git a/src/util/file/file_stdio.c b/src/util/file/file_stdio.c index 2a18c81..ada5ae3 100644 --- a/src/util/file/file_stdio.c +++ b/src/util/file/file_stdio.c @@ -2,24 +2,30 @@ #include "../error.h" +#ifndef NAUNET_NEEDS_STDIO_ASSIST +#define NAUNET_NEEDS_STDIO_ASSIST +#endif #include "file.h" -bool nn_file_open(struct nn_file *file, str *path, s32 flags) +static inline bool open_stdio_file(FILE **file, str *path, const char *mode) { - const char *mode = flags & NNWT_FILE_CREATE ? "wb+" : "rb+"; char *c_str = al_str_to_c_str(path); #ifdef NAUNET_ON_WINDOWS - errno_t ret = fopen_s(&file->file, c_str, mode); + // Still sets errno, according to the microsoft docs. + errno_t ret = fopen_s(file, c_str, mode); #else - file->file = fopen(c_str, mode); + s32 ret = (*file = fopen(c_str, mode)) != NULL ? 0 : errno; #endif al_free(c_str); -#ifdef NAUNET_ON_WINDOWS - if (ret != 0) { -#else - if (!file->file) { -#endif - al_log_error("file_stdio", "fopen(%.*s) failed (%s).", AL_STR_PRINTF(path), nn_strerror(errno)); + return ret == 0; +} + +bool nn_file_open(struct nn_file *file, str *path, s32 flags) +{ + const char *mode = flags & NNWT_FILE_CREATE ? "wb+" : "rb+"; + if (!open_stdio_file(&file->file, path, mode)) { + al_log_error("file_stdio", "fopen(%.*s) failed (%d: %s).", + al_str_fmt(path), errno, nn_strerror(errno)); return false; } @@ -36,8 +42,18 @@ bool nn_file_open(struct nn_file *file, str *path, s32 flags) bool nn_file_exists(str *path) { - (void)path; - return false; + FILE *file; + bool opened = open_stdio_file(&file, path, "r"); + if (opened) fclose(file); + return opened; +} + +bool nn_file_create(str *path) +{ + FILE *file; + bool opened = open_stdio_file(&file, path, "w+"); + if (opened) fclose(file); + return opened; } off_t nn_file_seek(struct nn_file *file, off_t offset, s32 whence) @@ -85,8 +101,7 @@ bool nn_file_read(struct nn_file *file, void *buf, size_t size) void *nn_file_mmap(struct nn_file *file) { (void)file; - al_assert(false); - return NULL; + al_assert_and_return(NULL); } void nn_file_close(struct nn_file *file) @@ -99,4 +114,6 @@ void nn_dir_close(struct nn_dir *dir) { (void)dir; } bool nn_dir_exists(str *path) { (void)path; return false; } bool nn_dir_create(str *path) { (void)path; return false; } bool nn_dir_read(struct nn_dir *dir, struct nn_dir_entry *entry) { (void)dir; (void)entry; return false; } -void nn_dir_entry_free(struct nn_dir_entry *entry) { (void)entry; } +void nn_dir_entry_get_name(struct nn_dir_entry *entry, str *name) { (void)entry; (void)name; }; +const char *nn_dir_entry_get_os_name(struct nn_dir_entry *entry) { (void)entry; return NULL; }; +void nn_dir_entry_get_path(struct nn_dir_entry *entry, struct nn_dir *dir, str *path) { (void)entry; (void)dir; (void)path; } diff --git a/src/util/file/util.c b/src/util/file/util.c index 93d0825..cf2d31c 100644 --- a/src/util/file/util.c +++ b/src/util/file/util.c @@ -8,7 +8,7 @@ s32 nn_file_read_as_str(struct nn_file *file, str *out) al_str_free(out); return -1; } - out->len = size; + out->length = size; return size; } @@ -27,9 +27,9 @@ s32 nn_file_read_as_c_str(struct nn_file *file, char **out) s32 nn_file_replace(struct nn_file *file, str *buf) { if ((nn_file_seek(file, 0, SEEK_SET) == (off_t)-1) || - (!nn_file_write(file, buf->data, buf->len)) || - (!nn_file_truncate(file, buf->len))) { + (!nn_file_write(file, buf->data, buf->length)) || + (!nn_file_truncate(file, buf->length))) { return -1; } - return buf->len; + return buf->length; } |