summaryrefslogtreecommitdiff
path: root/src/util
diff options
context:
space:
mode:
Diffstat (limited to 'src/util')
-rw-r--r--src/util/file/file.h9
-rw-r--r--src/util/file/file_linux.c44
-rw-r--r--src/util/file/file_stdio.c35
-rw-r--r--src/util/thread/thread_windows.c2
4 files changed, 41 insertions, 49 deletions
diff --git a/src/util/file/file.h b/src/util/file/file.h
index e53390d..03b82ba 100644
--- a/src/util/file/file.h
+++ b/src/util/file/file.h
@@ -4,7 +4,15 @@
#include <al/lib.h>
#ifdef NAUNET_NEEDS_STDIO_ASSIST
+#define _FILE_OFFSET_BITS 64
#include <stdio.h>
+#ifdef NAUNET_ON_WINDOWS
+#define fseek _fseeki64
+#define ftell _ftelli64
+#else
+#define fseek fseeko
+#define ftell ftello
+#endif
#else
#ifdef NAUNET_ON_WINDOWS
// @TODO:
@@ -72,7 +80,6 @@ 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);
diff --git a/src/util/file/file_linux.c b/src/util/file/file_linux.c
index 6b52c60..5a3621b 100644
--- a/src/util/file/file_linux.c
+++ b/src/util/file/file_linux.c
@@ -1,3 +1,4 @@
+#define AL_LOG_SECTION "file_linux"
#include <al/log.h>
#include <sys/mman.h>
@@ -9,7 +10,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).", nn_strerror(errno));
+ error("fcntl(F_SETLKW, F_WRLCK) failed (%s).", nn_strerror(errno));
close(fd);
return false;
}
@@ -20,7 +21,7 @@ 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).", nn_strerror(errno));
+ error("fcntl(F_SETLKW, F_ULOCK) failed (%s).", nn_strerror(errno));
return false;
}
return true;
@@ -43,15 +44,14 @@ bool nn_file_open(struct nn_file *file, str *path, s32 flags)
}
if (!open_file_linux(&file->fd, path, oflags)) {
- al_log_error("file", "open(%.*s) failed (%d: %s).",
- al_str_fmt(path), errno, nn_strerror(errno));
+ error("open(%.*s) failed (%d: %s).", al_str_fmt(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).", nn_strerror(errno));
+ error("fstat() failed (%s).", nn_strerror(errno));
close(file->fd);
return false;
}
@@ -87,8 +87,7 @@ 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, nn_strerror(errno));
+ error("lseek(%zd, %d) failed (%s).", offset, whence, nn_strerror(errno));
}
return res;
}
@@ -97,26 +96,20 @@ 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, nn_strerror(errno));
+ error("ftruncate(%zd) failed (%s).", size, nn_strerror(errno));
return false;
}
file->size = size;
return true;
}
-size_t nn_file_get_filesize(struct nn_file *file)
-{
- return file->size;
-}
-
#define file_io_loop(call, fail_case) \
size_t bc = 0; \
ssize_t res; \
for (;;) { \
res = call(file->fd, buf, size - bc); \
if (fail_case) { \
- al_log_error("file", ""#call"() failed (%s).", nn_strerror(errno)); \
+ error(""#call"() failed (%s).", nn_strerror(errno)); \
return false; \
} \
if ((bc += res) >= size) break; \
@@ -138,8 +131,7 @@ void *nn_file_mmap(struct nn_file *file)
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) {
- al_log_error("file", "mmap(%.*s) failed (%s).",
- al_str_fmt(&file->path), nn_strerror(errno));
+ error("mmap(%.*s) failed (%s).", al_str_fmt(&file->path), nn_strerror(errno));
return NULL;
}
return map;
@@ -149,8 +141,7 @@ void *nn_file_mremap(struct nn_file *file, size_t size, void *old_map)
{
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->size, size, nn_strerror(errno));
+ error("mremap(%p, %zu, %zu) failed (%s).", old_map, file->size, size, nn_strerror(errno));
return NULL;
}
return map;
@@ -160,8 +151,7 @@ void nn_file_munmap(struct nn_file *file, void *map)
{
s32 ret = munmap(map, file->size);
if (ret != 0) {
- al_log_error("file", "munmap(%p, %zu) failed (%s).",
- map, file->size, nn_strerror(errno));
+ error("munmap(%p, %zu) failed (%s).", map, file->size, nn_strerror(errno));
}
}
@@ -180,8 +170,7 @@ bool nn_dir_open(struct nn_dir *dir, str *path)
dir->dir = opendir(c_str);
al_free(c_str);
if (!dir->dir) {
- al_log_error("file", "opendir(%.*s) failed (%s).",
- al_str_fmt(path), nn_strerror(errno));
+ error("opendir(%.*s) failed (%s).", al_str_fmt(path), nn_strerror(errno));
return false;
}
al_str_clone(&dir->path, path);
@@ -202,8 +191,7 @@ bool nn_dir_exists(str *path)
al_free(c_str);
if (!dir) {
if (errno != ENOENT) {
- al_log_error("file", "opendir(%.*s) failed (%s).",
- al_str_fmt(path), nn_strerror(errno));
+ error("opendir(%.*s) failed (%s).", al_str_fmt(path), nn_strerror(errno));
}
return false;
}
@@ -217,8 +205,7 @@ bool nn_dir_create(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_fmt(path), nn_strerror(errno));
+ error("mkdir(%.*s) failed (%s).", al_str_fmt(path), nn_strerror(errno));
return false;
}
return true;
@@ -229,8 +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) {
- al_log_error("file", "readdir(%.*s) failed (%s).",
- al_str_fmt(&dir->path), nn_strerror(errno));
+ error("readdir(%.*s) failed (%s).", al_str_fmt(&dir->path), nn_strerror(errno));
}
return false;
}
diff --git a/src/util/file/file_stdio.c b/src/util/file/file_stdio.c
index ada5ae3..3f58664 100644
--- a/src/util/file/file_stdio.c
+++ b/src/util/file/file_stdio.c
@@ -1,3 +1,4 @@
+#define AL_LOG_SECTION "file_stdio"
#include <al/log.h>
#include "../error.h"
@@ -11,7 +12,7 @@ static inline bool open_stdio_file(FILE **file, str *path, const char *mode)
{
char *c_str = al_str_to_c_str(path);
#ifdef NAUNET_ON_WINDOWS
- // Still sets errno, according to the microsoft docs.
+ // 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;
@@ -20,22 +21,27 @@ 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)
+{
+ off_t pos = nn_file_seek(file, 0, SEEK_END);
+ if (pos == -1) return false;
+ file->size = pos;
+ rewind(file->file);
+ return true;
+}
+
bool nn_file_open(struct nn_file *file, str *path, s32 flags)
{
const char *mode = flags & NNWT_FILE_CREATE ? "wb+" : "rb+";
if (!open_stdio_file(&file->file, path, mode)) {
- al_log_error("file_stdio", "fopen(%.*s) failed (%d: %s).",
- al_str_fmt(path), errno, nn_strerror(errno));
+ error("fopen(%.*s) failed (%d: %s).", al_str_fmt(path), errno, nn_strerror(errno));
return false;
}
- off_t pos = nn_file_seek(file, 0, SEEK_END);
- if (pos == -1) {
+ if (!query_filesize_stdio(file)) {
nn_file_close(file);
return false;
}
- file->size = pos;
- rewind(file->file);
return true;
}
@@ -59,8 +65,7 @@ bool nn_file_create(str *path)
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, nn_strerror(errno));
+ error("fseek(%zd, %d) failed (%s).", offset, whence, nn_strerror(errno));
return -1;
}
return ftell(file->file);
@@ -68,21 +73,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)
{
- (void)file;
- (void)size;
+ file->size = size;
return true;
}
-size_t nn_file_get_filesize(struct nn_file *file)
-{
- return file->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, nn_strerror(errno));
+ error("fwrite(%zu) failed (%s).", size, nn_strerror(errno));
return false;
}
return true;
@@ -92,7 +91,7 @@ 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, nn_strerror(errno));
+ error("fread(%zu) failed (%s).", size, nn_strerror(errno));
return false;
}
return true;
diff --git a/src/util/thread/thread_windows.c b/src/util/thread/thread_windows.c
index ca9be80..f1f1dd7 100644
--- a/src/util/thread/thread_windows.c
+++ b/src/util/thread/thread_windows.c
@@ -4,7 +4,7 @@ NTSTATUS(__stdcall *NtDelayExecution)(BOOL Alertable, PLARGE_INTEGER DelayInterv
void nn_thread_sleep(nn_os_tstamp delay)
{
- NtDelayExecution(false, &((LARGE_INTEGER){ .QuadPart = -(s64)(delay * 1e7L) }));
+ NtDelayExecution(false, &((LARGE_INTEGER){ .QuadPart = -(LONGLONG)(delay * 1E7L) }));
}
void nn_thread_create(struct nn_thread *thread, nn_thread_func func, void *userdata)