chiark / gitweb /
fsync
authorDan Sheppard <dan.sheppard.circle@gmail.com>
Fri, 11 Apr 2025 00:30:14 +0000 (01:30 +0100)
committerDan Sheppard <dan.sheppard.circle@gmail.com>
Fri, 11 Apr 2025 00:30:14 +0000 (01:30 +0100)
posix.c
vfs.h

diff --git a/posix.c b/posix.c
index 18f2ff1572a9cdbd8025d0dccdd14e498adf7a67..588aa7301b77eef2f17fbe998e1fe38c02fb2027 100644 (file)
--- 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 8e144d59b50bee6e874e9a0a8968eb5f7dd81a75..b2e55c332678f31220959951eec0a3562794e697 100644 (file)
--- 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;