summaryrefslogtreecommitdiff
path: root/src/util/file/file_linux.c
diff options
context:
space:
mode:
Diffstat (limited to 'src/util/file/file_linux.c')
-rw-r--r--src/util/file/file_linux.c53
1 files changed, 37 insertions, 16 deletions
diff --git a/src/util/file/file_linux.c b/src/util/file/file_linux.c
index 6537d26..761b675 100644
--- a/src/util/file/file_linux.c
+++ b/src/util/file/file_linux.c
@@ -6,7 +6,7 @@
#include "file.h"
-static bool lock_file_internal(s32 fd, size_t size)
+static bool lock_file_internal(s32 fd, off_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) {
@@ -17,7 +17,7 @@ static bool lock_file_internal(s32 fd, size_t size)
return true;
}
-static bool unlock_file_internal(s32 fd, size_t size)
+static bool unlock_file_internal(s32 fd, off_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) {
@@ -35,6 +35,16 @@ static bool open_file_linux(s32 *fd, str *path, s32 flags)
return success;
}
+static inline off_t fstat_for_size(s32 fd)
+{
+ struct stat sb;
+ if (fstat(fd, &sb) == -1) {
+ log_error("fstat() failed (%s).", nn_strerror(errno));
+ return -1;
+ }
+ return sb.st_size;
+}
+
bool nn_file_open(struct nn_file *file, str *path, s32 flags)
{
file->flags = flags;
@@ -48,14 +58,11 @@ bool nn_file_open(struct nn_file *file, str *path, s32 flags)
return false;
}
- struct stat sb;
- s32 res = fstat(file->fd, &sb);
- if (res == -1) {
- log_error("fstat() failed (%s).", nn_strerror(errno));
+ file->size = fstat_for_size(file->fd);
+ if (file->size == -1) {
close(file->fd);
return false;
}
- file->size = sb.st_size;
if (flags & NNWT_FILE_LOCK && !lock_file_internal(file->fd, file->size)) {
close(file->fd);
@@ -67,6 +74,20 @@ bool nn_file_open(struct nn_file *file, str *path, s32 flags)
return true;
}
+bool nn_file_wrap_fd(struct nn_file *file, s32 fd)
+{
+ file->fd = fd;
+ file->size = fstat_for_size(file->fd);
+ if (file->size == -1) {
+ close(file->fd);
+ return false;
+ }
+ al_str_from(&file->path, "<fd:");
+ al_str_cat(&file->path, al_print_s32(fd));
+ al_str_cat(&file->path, &al_str_c(">"));
+ return true;
+}
+
bool nn_file_exists(str *path)
{
char *c_str = al_str_to_c_str(path);
@@ -85,11 +106,11 @@ bool nn_file_create(str *path)
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) {
+ off_t ret = lseek(file->fd, offset, whence);
+ if (ret == (off_t)-1) {
log_error("lseek(%zd, %d) failed (%s).", offset, whence, nn_strerror(errno));
}
- return res;
+ return ret;
}
bool nn_file_truncate(struct nn_file *file, off_t size)
@@ -115,25 +136,25 @@ bool nn_file_flush(struct nn_file *file)
#define file_io_loop(call, fail_case) \
size_t bc = 0; \
- ssize_t res; \
+ ssize_t ret; \
for (;;) { \
- res = call(file->fd, buf, size - bc); \
+ ret = call(file->fd, buf, size - bc); \
if (fail_case) { \
log_error(""#call"() failed (%s).", nn_strerror(errno)); \
return false; \
} \
- if ((bc += res) >= size) break; \
+ if ((bc += ret) >= size) break; \
} \
return true
bool nn_file_write(struct nn_file *file, void *buf, size_t size)
{
- file_io_loop(write, (res == -1));
+ file_io_loop(write, (ret == -1));
}
bool nn_file_read(struct nn_file *file, void *buf, size_t size)
{
- file_io_loop(read, (res == -1));
+ file_io_loop(read, (ret == -1));
}
void *nn_file_mmap(struct nn_file *file)
@@ -233,10 +254,10 @@ bool nn_dir_read(struct nn_dir *dir, struct nn_dir_entry *entry)
}
switch (entry->entry->d_type) {
+ case DT_REG:
case DT_UNKNOWN:
entry->type = NNWT_ENTRY_FILE;
break;
- case DT_REG:
case DT_DIR:
entry->type = NNWT_ENTRY_DIR;
break;