diff options
| author | 2024-11-26 16:18:41 -0500 | |
|---|---|---|
| committer | 2024-11-26 16:21:16 -0500 | |
| commit | 62b75446102ca32b23a78188b0aca8fe31d8b2c0 (patch) | |
| tree | 383a43e744a9bbb1d025e784bbb369e5f8ae8d7f /src/util/file | |
| parent | b0878176dd3e0a3a9314025fa832effe25efa9b4 (diff) | |
| download | libnaunet-62b75446102ca32b23a78188b0aca8fe31d8b2c0.tar.gz libnaunet-62b75446102ca32b23a78188b0aca8fe31d8b2c0.tar.bz2 libnaunet-62b75446102ca32b23a78188b0aca8fe31d8b2c0.zip | |
Track file lock and unlock
Signed-off-by: Andrew Opalach <andrew@akon.city>
Diffstat (limited to 'src/util/file')
| -rw-r--r-- | src/util/file/file.c | 0 | ||||
| -rw-r--r-- | src/util/file/file.h | 8 | ||||
| -rw-r--r-- | src/util/file/file_linux.c | 23 |
3 files changed, 24 insertions, 7 deletions
diff --git a/src/util/file/file.c b/src/util/file/file.c deleted file mode 100644 index e69de29..0000000 --- a/src/util/file/file.c +++ /dev/null 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 <al/str.h> +#include <al/lib.h> + #ifdef AKIYO_FILE_STDIO_COMPAT #include <stdio.h> #else @@ -8,14 +11,10 @@ #include <fcntl.h> #include <unistd.h> #include <sys/stat.h> -#include <sys/stat.h> #include <sys/mman.h> #endif #endif -#include <al/str.h> -#include <al/lib.h> - 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); } |