summaryrefslogtreecommitdiff
path: root/src/util/file
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2024-12-24 14:38:53 -0500
committerAndrew Opalach <andrew@akon.city> 2024-12-24 14:38:53 -0500
commit822e754cafdc5a32c80c97ca015cd6f9008329c3 (patch)
treeaa8831eca4f896928e9c3ba78446ca95762fccac /src/util/file
parentdcc43df0a40cbb2126505396cac4db26008d49b1 (diff)
downloadlibnaunet-822e754cafdc5a32c80c97ca015cd6f9008329c3.tar.gz
libnaunet-822e754cafdc5a32c80c97ca015cd6f9008329c3.tar.bz2
libnaunet-822e754cafdc5a32c80c97ca015cd6f9008329c3.zip
Rename, cleanup and pass on readability
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'src/util/file')
-rw-r--r--src/util/file/file.h71
-rw-r--r--src/util/file/file_linux.c130
-rw-r--r--src/util/file/file_stdio.c48
-rw-r--r--src/util/file/util.c16
4 files changed, 132 insertions, 133 deletions
diff --git a/src/util/file/file.h b/src/util/file/file.h
index 0544d3c..30b44b6 100644
--- a/src/util/file/file.h
+++ b/src/util/file/file.h
@@ -3,7 +3,7 @@
#include <al/str.h>
#include <al/lib.h>
-#ifdef AKIYO_FILE_STDIO_COMPAT
+#ifdef NAUNET_NEEDS_STDIO_ASSIST
#include <stdio.h>
#else
#ifndef _WIN32
@@ -16,76 +16,73 @@
#endif
enum {
- AKI_FILE_READONLY = 1,
- AKI_FILE_CREATE = 1 << 1,
- AKI_FILE_LOCK = 1 << 2
+ NNWT_FILE_READONLY = 1,
+ NNWT_FILE_CREATE = 1 << 1,
+ NNWT_FILE_LOCK = 1 << 2
};
enum {
- AKI_ENTRY_FILE = 0,
- AKI_ENTRY_DIR,
- AKI_ENTRY_OTHER
+ NNWT_ENTRY_FILE = 0,
+ NNWT_ENTRY_DIR,
+ NNWT_ENTRY_OTHER
};
-struct aki_file {
-#ifdef AKIYO_FILE_STDIO_COMPAT
+struct nn_file {
+#ifdef NAUNET_NEEDS_STDIO_ASSIST
FILE *file;
#else
#ifndef _WIN32
s32 fd;
#else
- // WINDOWS
#endif
bool locked;
#endif
size_t filesize;
};
-struct aki_dir {
-#ifdef AKIYO_FILE_STDIO_COMPAT
+struct nn_dir {
+#ifdef NAUNET_NEEDS_STDIO_ASSIST
#else
#ifndef _WIN32
DIR *dir;
#else
- // WINDOWS
#endif
#endif
str path;
};
-struct aki_dir_entry {
+struct nn_dir_entry {
u8 type;
-#ifdef AKIYO_FILE_STDIO_COMPAT
+#ifdef NAUNET_NEEDS_STDIO_ASSIST
#else
#ifndef _WIN32
struct dirent *entry;
#else
- // WINDOWS
#endif
#endif
str name;
str path;
};
-bool aki_file_open(struct aki_file *file, str *path, s32 flags);
-bool aki_file_exists(str *path);
-bool aki_file_create(str *path);
-bool aki_file_truncate(struct aki_file *file, off_t size);
-off_t aki_file_seek(struct aki_file *file, off_t offset, s32 whence);
-size_t aki_file_get_filesize(struct aki_file *file);
-bool aki_file_write(struct aki_file *file, void *buf, size_t size);
-bool aki_file_read(struct aki_file *file, void *buf, size_t size);
-s32 aki_file_read_as_str(struct aki_file *file, str *out);
-s32 aki_file_read_as_c_str(struct aki_file *file, char **out);
-s32 aki_file_replace(struct aki_file *file, str *buf);
-void *aki_file_mmap(struct aki_file *file);
-void *aki_file_mremap(struct aki_file *file, size_t size, void *old_map);
-void aki_file_munmap(struct aki_file *file, void *map);
-void aki_file_close(struct aki_file *file);
+bool nn_file_open(struct nn_file *file, str *path, s32 flags);
+bool nn_file_exists(str *path);
+bool nn_file_create(str *path);
+bool nn_file_truncate(struct nn_file *file, off_t size);
+off_t nn_file_seek(struct nn_file *file, off_t offset, s32 whence);
+size_t nn_file_get_filesize(struct nn_file *file);
+bool nn_file_write(struct nn_file *file, void *buf, size_t size);
+bool nn_file_read(struct nn_file *file, void *buf, size_t size);
+s32 nn_file_read_as_str(struct nn_file *file, str *out);
+s32 nn_file_read_as_c_str(struct nn_file *file, char **out);
+s32 nn_file_replace(struct nn_file *file, str *buf);
+void *nn_file_mmap(struct nn_file *file);
+void *nn_file_mremap(struct nn_file *file, size_t size, void *old_map);
+void nn_file_munmap(struct nn_file *file, void *map);
+void nn_file_close(struct nn_file *file);
-bool aki_dir_open(struct aki_dir *dir, str *path);
-void aki_dir_close(struct aki_dir *dir);
-bool aki_dir_exists(str *path);
-bool aki_dir_create(str *path);
-bool aki_dir_read(struct aki_dir *dir, struct aki_dir_entry *entry);
-void aki_dir_entry_free(struct aki_dir_entry *entry);
+bool nn_dir_open(struct nn_dir *dir, str *path);
+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);
diff --git a/src/util/file/file_linux.c b/src/util/file/file_linux.c
index 977853c..9c6b1ce 100644
--- a/src/util/file/file_linux.c
+++ b/src/util/file/file_linux.c
@@ -9,7 +9,7 @@ static bool lock_file_internal(s32 fd, size_t size)
{
struct flock l = { .l_type = F_WRLCK, .l_whence = SEEK_SET, .l_start = 0, .l_len = size };
if (fcntl(fd, F_SETLKW, &l) == -1) {
- al_log_error("file", "fcntl(F_SETLKW, F_WRLCK) failed (%s).", aki_strerror(errno));
+ al_log_error("file", "fcntl(F_SETLKW, F_WRLCK) failed (%s).", nn_strerror(errno));
close(fd);
return false;
}
@@ -20,41 +20,46 @@ static bool unlock_file_internal(s32 fd, size_t size)
{
struct flock l = { .l_type = F_ULOCK, .l_whence = SEEK_SET, .l_start = 0, .l_len = size };
if (fcntl(fd, F_SETLKW, &l) == -1) {
- al_log_error("file", "fcntl(F_SETLKW, F_ULOCK) failed (%s).", aki_strerror(errno));
+ al_log_error("file", "fcntl(F_SETLKW, F_ULOCK) failed (%s).", nn_strerror(errno));
return false;
}
return true;
}
-bool aki_file_open(struct aki_file *file, str *path, s32 flags)
+bool nn_file_open(struct nn_file *file, str *path, s32 flags)
{
- al_assert(!((flags & AKI_FILE_READONLY) && (flags & AKI_FILE_LOCK)));
+ al_assert(!((flags & NNWT_FILE_READONLY) && (flags & NNWT_FILE_LOCK)));
+ 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);
- s32 oflags = (flags & AKI_FILE_READONLY) ? O_RDONLY : O_RDWR;
- if (flags & AKI_FILE_CREATE) oflags |= O_CREAT;
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, aki_strerror(errno));
+ al_log_error("file", "open(%.*s) failed (%d: %s).", AL_STR_PRINTF(path), errno, nn_strerror(errno));
return false;
}
+
struct stat sb;
s32 res = fstat(file->fd, &sb);
if (res == -1) {
- al_log_error("file", "fstat() failed (%s).", aki_strerror(errno));
+ al_log_error("file", "fstat() failed (%s).", nn_strerror(errno));
close(file->fd);
return false;
}
file->filesize = sb.st_size;
- if (flags & AKI_FILE_LOCK) {
- file->locked = lock_file_internal(file->fd, file->filesize);
- } else {
- file->locked = false;
+
+ file->locked = flags & NNWT_FILE_LOCK;
+ if (file->locked && !lock_file_internal(file->fd, file->filesize)) {
+ close(file->fd);
+ return false;
}
+
return true;
}
-bool aki_file_exists(str *path)
+bool nn_file_exists(str *path)
{
char *c_str = al_str_to_c_str(path);
s32 ret = access(c_str, F_OK);
@@ -62,37 +67,36 @@ bool aki_file_exists(str *path)
return ret == 0;
}
-bool aki_file_create(str *path)
+bool nn_file_create(str *path)
{
- struct aki_file file;
- if (aki_file_open(&file, path, AKI_FILE_CREATE)) {
- aki_file_close(&file);
+ struct nn_file file;
+ if (nn_file_open(&file, path, NNWT_FILE_CREATE)) {
+ nn_file_close(&file);
return true;
}
return false;
}
-off_t aki_file_seek(struct aki_file *file, off_t offset, s32 whence)
+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, aki_strerror(errno));
- }
+ if (res == (off_t)-1)
+ al_log_error("file", "lseek(%jd, %d) failed (%s).", (intmax_t)offset, whence, nn_strerror(errno));
return res;
}
-bool aki_file_truncate(struct aki_file *file, off_t size)
+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, aki_strerror(errno));
+ al_log_error("file", "ftruncate(%jd) failed (%s).", (intmax_t)size, nn_strerror(errno));
return false;
}
file->filesize = size;
return true;
}
-size_t aki_file_get_filesize(struct aki_file *file)
+size_t nn_file_get_filesize(struct nn_file *file)
{
return file->filesize;
}
@@ -100,140 +104,136 @@ size_t aki_file_get_filesize(struct aki_file *file)
#define file_io_loop(call, fail_case) \
size_t bc = 0; \
ssize_t res; \
- do { \
+ for (;;) { \
res = call(file->fd, buf, size - bc); \
if (fail_case) { \
- al_log_error("file", ""#call"() failed (%s).", aki_strerror(errno)); \
+ al_log_error("file", ""#call"() failed (%s).", nn_strerror(errno)); \
return false; \
} \
- bc += res; \
- } while (bc < size); \
+ if ((bc += res) >= size) break; \
+ } \
return true
-bool aki_file_write(struct aki_file *file, void *buf, size_t size)
+bool nn_file_write(struct nn_file *file, void *buf, size_t size)
{
file_io_loop(write, (res == -1));
}
-bool aki_file_read(struct aki_file *file, void *buf, size_t size)
+bool nn_file_read(struct nn_file *file, void *buf, size_t size)
{
file_io_loop(read, (res == -1));
}
-void *aki_file_mmap(struct aki_file *file)
+void *nn_file_mmap(struct nn_file *file)
{
void *map = mmap(NULL, file->filesize, PROT_READ | PROT_WRITE, MAP_SHARED, file->fd, 0L);
if (map == MAP_FAILED) {
- al_log_error("file", "mmap() failed (%s).", aki_strerror(errno));
+ al_log_error("file", "mmap() failed (%s).", nn_strerror(errno));
return NULL;
}
return map;
}
-void *aki_file_mremap(struct aki_file *file, size_t size, void *old_map)
+void *nn_file_mremap(struct nn_file *file, size_t size, void *old_map)
{
void *map = mremap(old_map, file->filesize, size, MREMAP_MAYMOVE);
if (map == MAP_FAILED) {
- al_log_error("file", "mremap(%p, %zu, %zu) failed (%s).", old_map,
- file->filesize, size, aki_strerror(errno));
+ al_log_error("file", "mremap(%p, %zu, %zu) failed (%s).",
+ old_map, file->filesize, size, nn_strerror(errno));
return NULL;
}
return map;
}
-void aki_file_munmap(struct aki_file *file, void *map)
+void nn_file_munmap(struct nn_file *file, void *map)
{
s32 ret = munmap(map, file->filesize);
- if (ret != 0) {
- al_log_error("file", "munmap(%p, %zu) failed (%s).", map, file->filesize, aki_strerror(errno));
- }
+ if (ret != 0)
+ al_log_error("file", "munmap(%p, %zu) failed (%s).", map, file->filesize, nn_strerror(errno));
}
-void aki_file_close(struct aki_file *file)
+void nn_file_close(struct nn_file *file)
{
- if (file->locked) {
- unlock_file_internal(file->fd, file->filesize);
- }
+ if (file->locked) unlock_file_internal(file->fd, file->filesize);
close(file->fd);
}
-bool aki_dir_open(struct aki_dir *dir, str *path)
+bool nn_dir_open(struct nn_dir *dir, str *path)
{
char *c_str = al_str_to_c_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), aki_strerror(errno));
+ al_log_error("file", "opendir(%.*s) failed (%s).", AL_STR_PRINTF(path), nn_strerror(errno));
return false;
}
al_str_clone(&dir->path, path);
return true;
}
-void aki_dir_close(struct aki_dir *dir)
+void nn_dir_close(struct nn_dir *dir)
{
al_str_free(&dir->path);
closedir(dir->dir);
}
-bool aki_dir_exists(str *path)
+bool nn_dir_exists(str *path)
{
- struct aki_dir dir;
+ struct nn_dir dir;
char *c_str = al_str_to_c_str(path);
dir.dir = opendir(c_str);
al_free(c_str);
if (!dir.dir) {
- if (errno != ENOENT) {
- al_log_error("file", "opendir(%.*s) failed (%s).", AL_STR_PRINTF(path), aki_strerror(errno));
- }
+ if (errno != ENOENT)
+ al_log_error("file", "opendir(%.*s) failed (%s).", AL_STR_PRINTF(path), nn_strerror(errno));
return false;
}
closedir(dir.dir);
return true;
}
-bool aki_dir_create(str *path)
+bool nn_dir_create(str *path)
{
char *c_str = al_str_to_c_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), aki_strerror(errno));
+ al_log_error("file", "mkdir(%.*s) failed (%s).", AL_STR_PRINTF(path), nn_strerror(errno));
return false;
}
return true;
}
-bool aki_dir_read(struct aki_dir *dir, struct aki_dir_entry *entry)
+bool nn_dir_read(struct nn_dir *dir, struct nn_dir_entry *entry)
{
- errno = 0;
- entry->entry = readdir(dir->dir);
- if (!entry->entry) {
- if (errno) {
- al_log_error("file", "readdir() failed (%s).", aki_strerror(errno));
- }
+ if (!(entry->entry = readdir(dir->dir))) {
+ if (errno)
+ al_log_error("file", "readdir() failed (%s).", nn_strerror(errno));
return false;
}
+
switch (entry->entry->d_type) {
- case DT_REG:
case DT_UNKNOWN:
- entry->type = AKI_ENTRY_FILE;
+ entry->type = NNWT_ENTRY_FILE;
break;
+ case DT_REG:
case DT_DIR:
- entry->type = AKI_ENTRY_DIR;
+ entry->type = NNWT_ENTRY_DIR;
break;
default:
- entry->type = AKI_ENTRY_OTHER;
+ entry->type = NNWT_ENTRY_OTHER;
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 aki_dir_entry_free(struct aki_dir_entry *entry)
+void nn_dir_entry_free(struct nn_dir_entry *entry)
{
al_str_free(&entry->name);
al_str_free(&entry->path);
diff --git a/src/util/file/file_stdio.c b/src/util/file/file_stdio.c
index fa1e4ff..8f6d7ae 100644
--- a/src/util/file/file_stdio.c
+++ b/src/util/file/file_stdio.c
@@ -4,10 +4,10 @@
#include "file.h"
-bool aki_file_open(struct aki_file *file, str *path, s32 flags)
+bool nn_file_open(struct nn_file *file, str *path, s32 flags)
{
+ const char *mode = flags & NNWT_FILE_CREATE ? "wb+" : "rb+";
char *c_str = al_str_to_c_str(path);
- const char *mode = (flags & AKI_FILE_CREATE) ? "wb+" : "rb+";
#ifndef _WIN32
file->file = fopen(c_str, mode);
#else
@@ -19,82 +19,84 @@ bool aki_file_open(struct aki_file *file, str *path, s32 flags)
#else
if (ret != 0) {
#endif
- al_log_error("file_stdio", "fopen(%.*s) failed (%s).", AL_STR_PRINTF(path), aki_strerror(errno));
+ al_log_error("file_stdio", "fopen(%.*s) failed (%s).", AL_STR_PRINTF(path), nn_strerror(errno));
return false;
}
- off_t pos = aki_file_seek(file, 0, SEEK_END);
+
+ off_t pos = nn_file_seek(file, 0, SEEK_END);
if (pos == -1) {
- aki_file_close(file);
+ nn_file_close(file);
return false;
}
file->filesize = pos;
rewind(file->file);
+
return true;
}
-bool aki_file_exists(str *path)
+bool nn_file_exists(str *path)
{
(void)path;
return false;
}
-off_t aki_file_seek(struct aki_file *file, off_t offset, s32 whence)
+off_t nn_file_seek(struct nn_file *file, off_t offset, s32 whence)
{
if (fseek(file->file, offset, whence) != 0) {
- al_log_error("file_stdio", "fseek(%jd, %d) failed (%s).", (intmax_t)offset,
- whence, aki_strerror(errno));
+ al_log_error("file_stdio", "fseek(%jd, %d) failed (%s).",
+ (intmax_t)offset, whence, nn_strerror(errno));
return -1;
}
return ftell(file->file);
}
-bool aki_file_truncate(struct aki_file *file, off_t size)
+bool nn_file_truncate(struct nn_file *file, off_t size)
{
(void)file;
(void)size;
return true;
}
-size_t aki_file_get_filesize(struct aki_file *file)
+size_t nn_file_get_filesize(struct nn_file *file)
{
return file->filesize;
}
-bool aki_file_write(struct aki_file *file, void *buf, size_t size)
+bool nn_file_write(struct nn_file *file, void *buf, size_t size)
{
size_t ret = fwrite(buf, size, 1, file->file);
if (ret == 0) {
- al_log_error("file_stdio", "fwrite(%zu) failed (%s).", size, aki_strerror(errno));
+ al_log_error("file_stdio", "fwrite(%zu) failed (%s).", size, nn_strerror(errno));
return false;
}
return true;
}
-bool aki_file_read(struct aki_file *file, void *buf, size_t size)
+bool nn_file_read(struct nn_file *file, void *buf, size_t size)
{
size_t ret = fread(buf, size, 1, file->file);
if (ret == 0) {
- al_log_error("file_stdio", "fread(%zu) failed (%s).", size, aki_strerror(errno));
+ al_log_error("file_stdio", "fread(%zu) failed (%s).", size, nn_strerror(errno));
return false;
}
return true;
}
-void *aki_file_mmap(struct aki_file *file)
+void *nn_file_mmap(struct nn_file *file)
{
(void)file;
al_assert(false);
return NULL;
}
-void aki_file_close(struct aki_file *file)
+void nn_file_close(struct nn_file *file)
{
fclose(file->file);
}
-bool aki_dir_open(struct aki_dir *dir, str *path) { (void)dir; (void)path; return false; }
-void aki_dir_close(struct aki_dir *dir) { (void)dir; }
-bool aki_dir_exists(str *path) { (void)path; return false; }
-bool aki_dir_create(str *path) { (void)path; return false; }
-bool aki_dir_read(struct aki_dir *dir, struct aki_dir_entry *entry) { (void)dir; (void)entry; return false; }
-void aki_dir_entry_free(struct aki_dir_entry *entry) { (void)entry; }
+bool nn_dir_open(struct nn_dir *dir, str *path) { (void)dir; (void)path; return false; }
+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; }
diff --git a/src/util/file/util.c b/src/util/file/util.c
index fe7785f..50e2c4a 100644
--- a/src/util/file/util.c
+++ b/src/util/file/util.c
@@ -1,10 +1,10 @@
#include "file.h"
-s32 aki_file_read_as_str(struct aki_file *file, str *out)
+s32 nn_file_read_as_str(struct nn_file *file, str *out)
{
size_t size = file->filesize;
al_str_sized(out, size);
- if (!aki_file_read(file, (void *)out->data, size)) {
+ if (!nn_file_read(file, (void *)out->data, size)) {
al_str_free(out);
return -1;
}
@@ -12,11 +12,11 @@ s32 aki_file_read_as_str(struct aki_file *file, str *out)
return size;
}
-s32 aki_file_read_as_c_str(struct aki_file *file, char **out)
+s32 nn_file_read_as_c_str(struct nn_file *file, char **out)
{
size_t size = file->filesize;
*out = al_malloc(size + 1);
- if (!aki_file_read(file, (void *)*out, size)) {
+ if (!nn_file_read(file, (void *)*out, size)) {
al_free(*out);
return -1;
}
@@ -24,11 +24,11 @@ s32 aki_file_read_as_c_str(struct aki_file *file, char **out)
return size;
}
-s32 aki_file_replace(struct aki_file *file, str *buf)
+s32 nn_file_replace(struct nn_file *file, str *buf)
{
- if (aki_file_seek(file, 0, SEEK_SET) == (off_t)-1 ||
- !aki_file_write(file, buf->data, buf->len) ||
- !aki_file_truncate(file, buf->len)) {
+ 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)) {
return -1;
}
return buf->len;