summaryrefslogtreecommitdiff
path: root/src/util/file
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2025-01-01 15:44:14 -0500
committerAndrew Opalach <andrew@akon.city> 2025-01-01 15:44:14 -0500
commit8b5c569b53f9287e8b6c84e4402ec6f796e4cb6b (patch)
tree1603adacfcc46c5489203d1054467d3b42353aaa /src/util/file
parentbe4eb3831a2e5b44fb6e4c2d077e3c07edf6b776 (diff)
downloadlibnaunet-8b5c569b53f9287e8b6c84e4402ec6f796e4cb6b.tar.gz
libnaunet-8b5c569b53f9287e8b6c84e4402ec6f796e4cb6b.tar.bz2
libnaunet-8b5c569b53f9287e8b6c84e4402ec6f796e4cb6b.zip
Add direct (same process) mode for packet stream
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'src/util/file')
-rw-r--r--src/util/file/file.h21
-rw-r--r--src/util/file/file_linux.c27
-rw-r--r--src/util/file/file_stdio.c16
-rw-r--r--src/util/file/util.c4
4 files changed, 38 insertions, 30 deletions
diff --git a/src/util/file/file.h b/src/util/file/file.h
index 30b44b6..f79bfe5 100644
--- a/src/util/file/file.h
+++ b/src/util/file/file.h
@@ -6,7 +6,9 @@
#ifdef NAUNET_NEEDS_STDIO_ASSIST
#include <stdio.h>
#else
-#ifndef _WIN32
+#ifdef NAUNET_ON_WINDOWS
+// TODO
+#else
#include <dirent.h>
#include <fcntl.h>
#include <unistd.h>
@@ -31,21 +33,24 @@ struct nn_file {
#ifdef NAUNET_NEEDS_STDIO_ASSIST
FILE *file;
#else
-#ifndef _WIN32
- s32 fd;
+#ifdef NAUNET_ON_WINDOWS
+ // TODO
#else
+ s32 fd;
#endif
bool locked;
#endif
- size_t filesize;
+ str path;
+ size_t size;
};
struct nn_dir {
#ifdef NAUNET_NEEDS_STDIO_ASSIST
#else
-#ifndef _WIN32
- DIR *dir;
+#ifdef NAUNET_ON_WINDOWS
+ // TODO
#else
+ DIR *dir;
#endif
#endif
str path;
@@ -55,9 +60,9 @@ struct nn_dir_entry {
u8 type;
#ifdef NAUNET_NEEDS_STDIO_ASSIST
#else
-#ifndef _WIN32
- struct dirent *entry;
+#ifdef NAUNET_ON_WINDOWS
#else
+ struct dirent *entry;
#endif
#endif
str name;
diff --git a/src/util/file/file_linux.c b/src/util/file/file_linux.c
index a362637..bbc4c67 100644
--- a/src/util/file/file_linux.c
+++ b/src/util/file/file_linux.c
@@ -49,14 +49,16 @@ bool nn_file_open(struct nn_file *file, str *path, s32 flags)
close(file->fd);
return false;
}
- file->filesize = sb.st_size;
+ file->size = sb.st_size;
file->locked = flags & NNWT_FILE_LOCK;
- if (file->locked && !lock_file_internal(file->fd, file->filesize)) {
+ if (file->locked && !lock_file_internal(file->fd, file->size)) {
close(file->fd);
return false;
}
+ al_str_clone(&file->path, path);
+
return true;
}
@@ -94,13 +96,13 @@ bool nn_file_truncate(struct nn_file *file, off_t size)
al_log_error("file", "ftruncate(%jd) failed (%s).", (intmax_t)size, nn_strerror(errno));
return false;
}
- file->filesize = size;
+ file->size = size;
return true;
}
size_t nn_file_get_filesize(struct nn_file *file)
{
- return file->filesize;
+ return file->size;
}
#define file_io_loop(call, fail_case) \
@@ -128,9 +130,9 @@ 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->filesize, PROT_READ | PROT_WRITE, MAP_SHARED, file->fd, 0L);
+ void *map = mmap(NULL, file->size, PROT_READ | PROT_WRITE, MAP_SHARED, file->fd, 0L);
if (map == MAP_FAILED) {
- al_log_error("file", "mmap() failed (%s).", nn_strerror(errno));
+ al_log_error("file", "mmap(%.*s) failed (%s).", AL_STR_PRINTF(&file->path), nn_strerror(errno));
return NULL;
}
return map;
@@ -138,10 +140,10 @@ void *nn_file_mmap(struct nn_file *file)
void *nn_file_mremap(struct nn_file *file, size_t size, void *old_map)
{
- void *map = mremap(old_map, file->filesize, size, MREMAP_MAYMOVE);
+ void *map = mremap(old_map, file->size, size, MREMAP_MAYMOVE);
if (map == MAP_FAILED) {
al_log_error("file", "mremap(%p, %zu, %zu) failed (%s).",
- old_map, file->filesize, size, nn_strerror(errno));
+ old_map, file->size, size, nn_strerror(errno));
return NULL;
}
return map;
@@ -149,18 +151,19 @@ void *nn_file_mremap(struct nn_file *file, size_t size, void *old_map)
void nn_file_munmap(struct nn_file *file, void *map)
{
- s32 ret = munmap(map, file->filesize);
+ s32 ret = munmap(map, file->size);
if (ret != 0) {
- al_log_error("file", "munmap(%p, %zu) failed (%s).", map, file->filesize, 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) {
- unlock_file_internal(file->fd, file->filesize);
+ unlock_file_internal(file->fd, file->size);
}
close(file->fd);
+ al_str_free(&file->path);
}
bool nn_dir_open(struct nn_dir *dir, str *path)
@@ -178,8 +181,8 @@ bool nn_dir_open(struct nn_dir *dir, str *path)
void nn_dir_close(struct nn_dir *dir)
{
- al_str_free(&dir->path);
closedir(dir->dir);
+ al_str_free(&dir->path);
}
bool nn_dir_exists(str *path)
diff --git a/src/util/file/file_stdio.c b/src/util/file/file_stdio.c
index 8f6d7ae..2a18c81 100644
--- a/src/util/file/file_stdio.c
+++ b/src/util/file/file_stdio.c
@@ -8,16 +8,16 @@ 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);
-#ifndef _WIN32
- file->file = fopen(c_str, mode);
-#else
+#ifdef NAUNET_ON_WINDOWS
errno_t ret = fopen_s(&file->file, c_str, mode);
+#else
+ file->file = fopen(c_str, mode);
#endif
al_free(c_str);
-#ifndef _WIN32
- if (!file->file) {
-#else
+#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 false;
@@ -28,7 +28,7 @@ bool nn_file_open(struct nn_file *file, str *path, s32 flags)
nn_file_close(file);
return false;
}
- file->filesize = pos;
+ file->size = pos;
rewind(file->file);
return true;
@@ -59,7 +59,7 @@ bool nn_file_truncate(struct nn_file *file, off_t size)
size_t nn_file_get_filesize(struct nn_file *file)
{
- return file->filesize;
+ return file->size;
}
bool nn_file_write(struct nn_file *file, void *buf, size_t size)
diff --git a/src/util/file/util.c b/src/util/file/util.c
index eab9893..93d0825 100644
--- a/src/util/file/util.c
+++ b/src/util/file/util.c
@@ -2,7 +2,7 @@
s32 nn_file_read_as_str(struct nn_file *file, str *out)
{
- size_t size = file->filesize;
+ size_t size = file->size;
al_str_sized(out, size);
if (!nn_file_read(file, (void *)out->data, size)) {
al_str_free(out);
@@ -14,7 +14,7 @@ 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)
{
- size_t size = file->filesize;
+ size_t size = file->size;
*out = al_malloc(size + 1);
if (!nn_file_read(file, (void *)*out, size)) {
al_free(*out);