summaryrefslogtreecommitdiff
path: root/src/util/file/file_stdio.c
diff options
context:
space:
mode:
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;
}