summaryrefslogtreecommitdiff
path: root/src/util/file
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/file')
-rw-r--r--src/util/file/file.h1
-rw-r--r--src/util/file/file_linux.c39
-rw-r--r--src/util/file/file_stdio.c8
3 files changed, 30 insertions, 18 deletions
diff --git a/src/util/file/file.h b/src/util/file/file.h
index 91aa82c..4df4c96 100644
--- a/src/util/file/file.h
+++ b/src/util/file/file.h
@@ -81,6 +81,7 @@ bool nn_file_open(struct nn_file *file, str *path, s32 flags);
bool nn_file_exists(str *path);
bool nn_file_create(str *path);
bool nn_file_truncate(struct nn_file *file, off_t size);
+bool nn_file_flush(struct nn_file *file);
off_t nn_file_seek(struct nn_file *file, off_t offset, s32 whence);
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);
diff --git a/src/util/file/file_linux.c b/src/util/file/file_linux.c
index c87bc9d..9693d4b 100644
--- a/src/util/file/file_linux.c
+++ b/src/util/file/file_linux.c
@@ -10,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) {
- error("fcntl(F_SETLKW, F_WRLCK) failed (%s).", nn_strerror(errno));
+ log_error("fcntl(F_SETLKW, F_WRLCK) failed (%s).", nn_strerror(errno));
close(fd);
return false;
}
@@ -21,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) {
- error("fcntl(F_SETLKW, F_ULOCK) failed (%s).", nn_strerror(errno));
+ log_error("fcntl(F_SETLKW, F_ULOCK) failed (%s).", nn_strerror(errno));
return false;
}
return true;
@@ -44,14 +44,14 @@ bool nn_file_open(struct nn_file *file, str *path, s32 flags)
}
if (!open_file_linux(&file->fd, path, oflags)) {
- error("open(%.*s) failed (%d: %s).", al_str_x(path), errno, nn_strerror(errno));
+ log_error("open(%.*s) failed (%d: %s).", al_str_x(path), errno, nn_strerror(errno));
return false;
}
struct stat sb;
s32 res = fstat(file->fd, &sb);
if (res == -1) {
- error("fstat() failed (%s).", nn_strerror(errno));
+ log_error("fstat() failed (%s).", nn_strerror(errno));
close(file->fd);
return false;
}
@@ -87,7 +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) {
- error("lseek(%zd, %d) failed (%s).", offset, whence, nn_strerror(errno));
+ log_error("lseek(%zd, %d) failed (%s).", offset, whence, nn_strerror(errno));
}
return res;
}
@@ -96,20 +96,30 @@ bool nn_file_truncate(struct nn_file *file, off_t size)
{
s32 ret = ftruncate(file->fd, size);
if (ret != 0) {
- error("ftruncate(%zd) failed (%s).", size, nn_strerror(errno));
+ log_error("ftruncate(%zd) failed (%s).", size, nn_strerror(errno));
return false;
}
file->size = size;
return true;
}
+bool nn_file_flush(struct nn_file *file)
+{
+ s32 ret = fsync(file->fd);
+ if (ret != 0) {
+ log_error("fsync() failed (%s).", nn_strerror(errno));
+ return false;
+ }
+ return true;
+}
+
#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) { \
- error(""#call"() failed (%s).", nn_strerror(errno)); \
+ log_error(""#call"() failed (%s).", nn_strerror(errno)); \
return false; \
} \
if ((bc += res) >= size) break; \
@@ -128,10 +138,11 @@ bool nn_file_read(struct nn_file *file, void *buf, size_t size)
void *nn_file_mmap(struct nn_file *file)
{
+ if (file->size == 0) return NULL;
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_x(&file->path), nn_strerror(errno));
+ log_error("mmap(%.*s) failed (%s).", al_str_x(&file->path), nn_strerror(errno));
return NULL;
}
return map;
@@ -141,7 +152,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) {
- error("mremap(%p, %zu, %zu) failed (%s).", old_map, file->size, size, nn_strerror(errno));
+ log_error("mremap(%p, %zu, %zu) failed (%s).", old_map, file->size, size, nn_strerror(errno));
return NULL;
}
return map;
@@ -151,7 +162,7 @@ void nn_file_munmap(struct nn_file *file, void *map)
{
s32 ret = munmap(map, file->size);
if (ret != 0) {
- error("munmap(%p, %zu) failed (%s).", map, file->size, nn_strerror(errno));
+ log_error("munmap(%p, %zu) failed (%s).", map, file->size, nn_strerror(errno));
}
}
@@ -170,7 +181,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_x(path), nn_strerror(errno));
+ log_error("opendir(%.*s) failed (%s).", al_str_x(path), nn_strerror(errno));
return false;
}
al_str_clone(&dir->path, path);
@@ -191,7 +202,7 @@ bool nn_dir_exists(str *path)
al_free(c_str);
if (!dir) {
if (errno != ENOENT) {
- error("opendir(%.*s) failed (%s).", al_str_x(path), nn_strerror(errno));
+ log_error("opendir(%.*s) failed (%s).", al_str_x(path), nn_strerror(errno));
}
return false;
}
@@ -205,7 +216,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_x(path), nn_strerror(errno));
+ log_error("mkdir(%.*s) failed (%s).", al_str_x(path), nn_strerror(errno));
return false;
}
return true;
@@ -216,7 +227,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_x(&dir->path), nn_strerror(errno));
+ log_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 1969477..48dd545 100644
--- a/src/util/file/file_stdio.c
+++ b/src/util/file/file_stdio.c
@@ -40,7 +40,7 @@ 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)) {
- error("fopen(%.*s) failed (%d: %s).", al_str_x(path), errno, nn_strerror(errno));
+ log_error("fopen(%.*s) failed (%d: %s).", al_str_x(path), errno, nn_strerror(errno));
return false;
}
@@ -71,7 +71,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) {
- error("fseek(%zd, %d) failed (%s).", offset, whence, nn_strerror(errno));
+ log_error("fseek(%zd, %d) failed (%s).", offset, whence, nn_strerror(errno));
return -1;
}
return ftell(file->file);
@@ -87,7 +87,7 @@ 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) {
- error("fwrite(%zu) failed (%s).", size, nn_strerror(errno));
+ log_error("fwrite(%zu) failed (%s).", size, nn_strerror(errno));
return false;
}
return true;
@@ -97,7 +97,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) {
- error("fread(%zu) failed (%s).", size, nn_strerror(errno));
+ log_error("fread(%zu) failed (%s).", size, nn_strerror(errno));
return false;
}
return true;