summaryrefslogtreecommitdiff
path: root/src/util/file/file_stdio.c
diff options
context:
space:
mode:
authorAndrew Opalach <andrew@akon.city> 2026-08-25 19:35:41 -0400
committerAndrew Opalach <andrew@akon.city> 2026-08-25 19:35:41 -0400
commitd2d8e2fa52c5633f6376e4d3b1d7aca9a9ea14b0 (patch)
tree8ffa2790d581fc375720d1ab1146d6ac3cb63bff /src/util/file/file_stdio.c
parenta389b8f2f9c55b76fe5a28c3f1fea798ed37aa98 (diff)
downloadlibnaunet-d2d8e2fa52c5633f6376e4d3b1d7aca9a9ea14b0.tar.gz
libnaunet-d2d8e2fa52c5633f6376e4d3b1d7aca9a9ea14b0.tar.bz2
libnaunet-d2d8e2fa52c5633f6376e4d3b1d7aca9a9ea14b0.zip
File fixes
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'src/util/file/file_stdio.c')
-rw-r--r--src/util/file/file_stdio.c37
1 files changed, 24 insertions, 13 deletions
diff --git a/src/util/file/file_stdio.c b/src/util/file/file_stdio.c
index fa74be7..5bdc7d9 100644
--- a/src/util/file/file_stdio.c
+++ b/src/util/file/file_stdio.c
@@ -2,18 +2,14 @@
#include <al/log.h>
#include <al/lib.h>
+#ifdef NAUNET_ON_WINDOWS
+#include "../../winwrap.h"
+#endif
+
#include "../error.h"
#include "file.h"
-/*
-#if defined NAUNET_ON_WINDOWS && defined AL_WE_32BIT
-AL_ASSERT_TYPE_SIZE(off_t, 4);
-#else
-AL_ASSERT_TYPE_SIZE(off_t, 8);
-#endif
-*/
-
static inline bool open_stdio_file(FILE **file, str *path, const char *mode)
{
char *c_str = al_str_to_c_str(path);
@@ -27,13 +23,11 @@ static inline bool open_stdio_file(FILE **file, str *path, const char *mode)
return ret == 0;
}
-static bool query_filesize_stdio(struct nn_file *file)
+static inline off_t seek_for_size(struct nn_file *file)
{
off_t pos = nn_file_seek(file, 0, SEEK_END);
- if (pos == -1) return false;
- file->size = pos;
rewind(file->file);
- return true;
+ return pos;
}
bool nn_file_open(struct nn_file *file, str *path, s32 flags)
@@ -44,7 +38,8 @@ bool nn_file_open(struct nn_file *file, str *path, s32 flags)
return false;
}
- if (!query_filesize_stdio(file)) {
+ file->size = seek_for_size(file);
+ if (file->size == -1) {
nn_file_close(file);
return false;
}
@@ -54,6 +49,13 @@ bool nn_file_open(struct nn_file *file, str *path, s32 flags)
return true;
}
+bool nn_file_wrap_fd(struct nn_file *file, s32 fd)
+{
+ (void)file;
+ (void)fd;
+ al_assert_and_return(false);
+}
+
bool nn_file_exists(str *path)
{
FILE *file;
@@ -81,6 +83,15 @@ off_t nn_file_seek(struct nn_file *file, off_t offset, s32 whence)
bool nn_file_truncate(struct nn_file *file, off_t size)
{
+#ifdef NAUNET_ON_WINDOWS
+ errno_t ret = _chsize_s(fileno(file->file), size);
+#else
+ s32 ret = ftruncate(fileno(file->file), size);
+#endif
+ if (ret != 0) {
+ log_error("ftruncate(%zd) failed (%s).", size, nn_strerror(errno));
+ return false;
+ }
file->size = size;
return true;
}