From 62b75446102ca32b23a78188b0aca8fe31d8b2c0 Mon Sep 17 00:00:00 2001 From: Andrew Opalach Date: Tue, 26 Nov 2024 16:18:41 -0500 Subject: Track file lock and unlock Signed-off-by: Andrew Opalach --- src/util/error.c | 2 +- src/util/error.h | 2 +- src/util/file/file.c | 0 src/util/file/file.h | 8 ++++---- src/util/file/file_linux.c | 23 ++++++++++++++++++++--- 5 files changed, 26 insertions(+), 9 deletions(-) delete mode 100644 src/util/file/file.c (limited to 'src/util') diff --git a/src/util/error.c b/src/util/error.c index 7e09d20..c75696d 100644 --- a/src/util/error.c +++ b/src/util/error.c @@ -1,3 +1,3 @@ #include "error.h" -__al_thread char errorbuf[AKI_ERROR_STR_MAXLEN]; +__thread char errorbuf[AKI_ERROR_STR_MAXLEN]; diff --git a/src/util/error.h b/src/util/error.h index c45f0f0..7f63aee 100644 --- a/src/util/error.h +++ b/src/util/error.h @@ -6,7 +6,7 @@ #define AKI_ERROR_STR_MAXLEN 94 -extern __al_thread char errorbuf[AKI_ERROR_STR_MAXLEN]; +extern __thread char errorbuf[AKI_ERROR_STR_MAXLEN]; static inline char *aki_strerror(s32 errnum) { diff --git a/src/util/file/file.c b/src/util/file/file.c deleted file mode 100644 index e69de29..0000000 diff --git a/src/util/file/file.h b/src/util/file/file.h index a6611bd..0544d3c 100644 --- a/src/util/file/file.h +++ b/src/util/file/file.h @@ -1,5 +1,8 @@ #pragma once +#include +#include + #ifdef AKIYO_FILE_STDIO_COMPAT #include #else @@ -8,14 +11,10 @@ #include #include #include -#include #include #endif #endif -#include -#include - enum { AKI_FILE_READONLY = 1, AKI_FILE_CREATE = 1 << 1, @@ -37,6 +36,7 @@ struct aki_file { #else // WINDOWS #endif + bool locked; #endif size_t filesize; }; diff --git a/src/util/file/file_linux.c b/src/util/file/file_linux.c index ac78bd8..977853c 100644 --- a/src/util/file/file_linux.c +++ b/src/util/file/file_linux.c @@ -9,13 +9,23 @@ 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) failed (%s).", aki_strerror(errno)); + al_log_error("file", "fcntl(F_SETLKW, F_WRLCK) failed (%s).", aki_strerror(errno)); close(fd); return false; } return true; } +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).", aki_strerror(errno)); + return false; + } + return true; +} + bool aki_file_open(struct aki_file *file, str *path, s32 flags) { al_assert(!((flags & AKI_FILE_READONLY) && (flags & AKI_FILE_LOCK))); @@ -25,7 +35,7 @@ bool aki_file_open(struct aki_file *file, str *path, s32 flags) file->fd = open(c_str, oflags, 0644); al_free(c_str); if (file->fd == -1) { - al_log_error("file", "open(%.*s) failed (%s).", AL_STR_PRINTF(path), aki_strerror(errno)); + al_log_error("file", "open(%.*s) failed (%d: %s).", AL_STR_PRINTF(path), errno, aki_strerror(errno)); return false; } struct stat sb; @@ -36,7 +46,11 @@ bool aki_file_open(struct aki_file *file, str *path, s32 flags) return false; } file->filesize = sb.st_size; - if (flags & AKI_FILE_LOCK) lock_file_internal(file->fd, file->filesize); + if (flags & AKI_FILE_LOCK) { + file->locked = lock_file_internal(file->fd, file->filesize); + } else { + file->locked = false; + } return true; } @@ -137,6 +151,9 @@ void aki_file_munmap(struct aki_file *file, void *map) void aki_file_close(struct aki_file *file) { + if (file->locked) { + unlock_file_internal(file->fd, file->filesize); + } close(file->fd); } -- cgit v1.2.3-101-g0448