summaryrefslogtreecommitdiff
path: root/src/util/file
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/file')
-rw-r--r--src/util/file/file.h10
-rw-r--r--src/util/file/file_linux.c16
-rw-r--r--src/util/file/file_stdio.c18
3 files changed, 26 insertions, 18 deletions
diff --git a/src/util/file/file.h b/src/util/file/file.h
index 03b82ba..91aa82c 100644
--- a/src/util/file/file.h
+++ b/src/util/file/file.h
@@ -4,18 +4,22 @@
#include <al/lib.h>
#ifdef NAUNET_NEEDS_STDIO_ASSIST
-#define _FILE_OFFSET_BITS 64
+// It seems meson adds _FILE_OFFSET_BITS=64 on all "linuxlike" compilers even MinGW.
+// https://github.com/mesonbuild/meson/commit/853634a48da025c59eef70161dba0d150833f60d
+// https://github.com/mesonbuild/meson/issues/12931
+// I don't think we necessarily want this on 32-bit Windows.
#include <stdio.h>
#ifdef NAUNET_ON_WINDOWS
+#if _FILE_OFFSET_BITS == 64
#define fseek _fseeki64
#define ftell _ftelli64
+#endif
#else
#define fseek fseeko
#define ftell ftello
#endif
#else
#ifdef NAUNET_ON_WINDOWS
-// @TODO:
#else
#include <dirent.h>
#include <fcntl.h>
@@ -43,7 +47,6 @@ struct nn_file {
FILE *file;
#else
#ifdef NAUNET_ON_WINDOWS
- // @TODO:
#else
s32 fd;
#endif
@@ -56,7 +59,6 @@ struct nn_dir {
#ifdef NAUNET_NEEDS_STDIO_ASSIST
#else
#ifdef NAUNET_ON_WINDOWS
- // @TODO:
#else
DIR *dir;
#endif
diff --git a/src/util/file/file_linux.c b/src/util/file/file_linux.c
index 5a3621b..c87bc9d 100644
--- a/src/util/file/file_linux.c
+++ b/src/util/file/file_linux.c
@@ -38,13 +38,13 @@ static bool open_file_linux(s32 *fd, str *path, s32 flags)
bool nn_file_open(struct nn_file *file, str *path, s32 flags)
{
file->flags = flags;
- s32 oflags = flags & NNWT_FILE_READONLY ? O_RDONLY : O_RDWR;
+ s32 oflags = (flags & NNWT_FILE_READONLY) ? O_RDONLY : O_RDWR;
if (flags & NNWT_FILE_CREATE) {
oflags |= O_CREAT;
}
if (!open_file_linux(&file->fd, path, oflags)) {
- error("open(%.*s) failed (%d: %s).", al_str_fmt(path), errno, nn_strerror(errno));
+ error("open(%.*s) failed (%d: %s).", al_str_x(path), errno, nn_strerror(errno));
return false;
}
@@ -128,10 +128,10 @@ bool nn_file_read(struct nn_file *file, void *buf, size_t size)
void *nn_file_mmap(struct nn_file *file)
{
- s32 flags = PROT_READ | (file->flags & NNWT_FILE_READONLY ? 0 : PROT_WRITE);
+ 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) {
- error("mmap(%.*s) failed (%s).", al_str_fmt(&file->path), nn_strerror(errno));
+ error("mmap(%.*s) failed (%s).", al_str_x(&file->path), nn_strerror(errno));
return NULL;
}
return map;
@@ -170,7 +170,7 @@ bool nn_dir_open(struct nn_dir *dir, str *path)
dir->dir = opendir(c_str);
al_free(c_str);
if (!dir->dir) {
- error("opendir(%.*s) failed (%s).", al_str_fmt(path), nn_strerror(errno));
+ error("opendir(%.*s) failed (%s).", al_str_x(path), nn_strerror(errno));
return false;
}
al_str_clone(&dir->path, path);
@@ -191,7 +191,7 @@ bool nn_dir_exists(str *path)
al_free(c_str);
if (!dir) {
if (errno != ENOENT) {
- error("opendir(%.*s) failed (%s).", al_str_fmt(path), nn_strerror(errno));
+ error("opendir(%.*s) failed (%s).", al_str_x(path), nn_strerror(errno));
}
return false;
}
@@ -205,7 +205,7 @@ bool nn_dir_create(str *path)
s32 ret = mkdir(c_str, 0755);
al_free(c_str);
if (ret == -1) {
- error("mkdir(%.*s) failed (%s).", al_str_fmt(path), nn_strerror(errno));
+ error("mkdir(%.*s) failed (%s).", al_str_x(path), nn_strerror(errno));
return false;
}
return true;
@@ -216,7 +216,7 @@ bool nn_dir_read(struct nn_dir *dir, struct nn_dir_entry *entry)
errno = 0;
if (!(entry->entry = readdir(dir->dir))) {
if (errno) {
- error("readdir(%.*s) failed (%s).", al_str_fmt(&dir->path), nn_strerror(errno));
+ error("readdir(%.*s) failed (%s).", al_str_x(&dir->path), nn_strerror(errno));
}
return false;
}
diff --git a/src/util/file/file_stdio.c b/src/util/file/file_stdio.c
index 3f58664..1969477 100644
--- a/src/util/file/file_stdio.c
+++ b/src/util/file/file_stdio.c
@@ -1,13 +1,19 @@
#define AL_LOG_SECTION "file_stdio"
#include <al/log.h>
+#include <al/lib.h>
#include "../error.h"
-#ifndef NAUNET_NEEDS_STDIO_ASSIST
-#define NAUNET_NEEDS_STDIO_ASSIST
-#endif
#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);
@@ -15,7 +21,7 @@ static inline bool open_stdio_file(FILE **file, str *path, const char *mode)
// Still sets errno, according to the Microsoft docs.
errno_t ret = fopen_s(file, c_str, mode);
#else
- s32 ret = (*file = fopen(c_str, mode)) != NULL ? 0 : errno;
+ s32 ret = (*file = fopen(c_str, mode)) ? 0 : errno;
#endif
al_free(c_str);
return ret == 0;
@@ -32,9 +38,9 @@ static bool query_filesize_stdio(struct nn_file *file)
bool nn_file_open(struct nn_file *file, str *path, s32 flags)
{
- const char *mode = flags & NNWT_FILE_CREATE ? "wb+" : "rb+";
+ const char *mode = (flags & NNWT_FILE_CREATE) ? "wb+" : "rb+";
if (!open_stdio_file(&file->file, path, mode)) {
- error("fopen(%.*s) failed (%d: %s).", al_str_fmt(path), errno, nn_strerror(errno));
+ error("fopen(%.*s) failed (%d: %s).", al_str_x(path), errno, nn_strerror(errno));
return false;
}