From 7cd3433c45cacd2b275f04ff459dd537227259be Mon Sep 17 00:00:00 2001 From: Dan Sheppard Date: Fri, 11 Apr 2025 01:30:14 +0100 Subject: [PATCH] fsync --- posix.c | 25 ++++++++++++++++++++++++- vfs.h | 7 +++++-- 2 files changed, 29 insertions(+), 3 deletions(-) diff --git a/posix.c b/posix.c index 18f2ff1..588aa73 100644 --- a/posix.c +++ b/posix.c @@ -382,6 +382,28 @@ static int posix_delete(void *vfs_data, int which_file) { return COQUET_RET_OK; } +static int posix_sync(void * vfs_data, int which_file, bool_t data_only) { + struct posix_data * pd = (struct posix_data *)vfs_data; + int *fd, r; + + fd = file_fd(pd,which_file); + if(fd == NULL || *fd == -1) { + set_error(pd,"file not open",0); + return COQUET_RET_VFSERR; + } + + if(data_only) { + r = fdatasync(*fd); + } else { + r = fsync(*fd); + } + if(r == -1) { + set_error(pd,"fsync failed",1); + return COQUET_RET_VFSERR; + } + + return COQUET_RET_OK; +} vfs_t vfs_posix = { .make = posix_make, @@ -393,5 +415,6 @@ vfs_t vfs_posix = { .write = posix_write, .read = posix_read, .finish = posix_finish, - .delete = posix_delete + .delete = posix_delete, + .sync = posix_sync }; diff --git a/vfs.h b/vfs.h index 8e144d5..b2e55c3 100644 --- a/vfs.h +++ b/vfs.h @@ -47,6 +47,9 @@ typedef struct vfs { */ int (*open)(void * vfs_data, int which_file, bool_t allow_create); + /* Sync given file to disk. */ + int (*sync)(void * vfs_data, int which_file, bool_t data_only); + /* Close the given file. Which_file is drawn from COQUET_FILE_*. The * file is guaranteed to be open when this function is called. If file * is COQUET_FILE_MAIN, all locks must be dropped. @@ -72,11 +75,11 @@ typedef struct vfs { /* Finish VFS layer. Release OS resources (files etc), and free the * passed vfs_data. */ - int (*finish)(void *vfs_data); + int (*finish)(void * vfs_data); /* Delete the given file. File will be closed. */ - int (*delete)(void *vfs_data, int which_file); + int (*delete)(void * vfs_data, int which_file); } vfs_t; -- 2.30.2