chiark / gitweb /
delete
authorDan Sheppard <dan.sheppard.circle@gmail.com>
Thu, 10 Apr 2025 12:28:13 +0000 (13:28 +0100)
committerDan Sheppard <dan.sheppard.circle@gmail.com>
Thu, 10 Apr 2025 12:28:13 +0000 (13:28 +0100)
coquet
main.c
posix.c
vfs.h

diff --git a/coquet b/coquet
index d2ea0047be821f44919cd4b68a4e570569a7edf4..2a2d542b019c61c2c8838dc232f8088f1d23d7e4 100755 (executable)
Binary files a/coquet and b/coquet differ
diff --git a/main.c b/main.c
index c308131695dad9bf4c7a086d5f4b18c4d7fbb26c..424272560180717701d29ae997f845fe15b5626f 100644 (file)
--- a/main.c
+++ b/main.c
@@ -42,6 +42,8 @@ int main() {
     bail(&cq,r);
     r = (cq.vfs_funcs.close)(cq.vfs_data,COQUET_FILE_MAIN);
     bail(&cq,r);
+    r = (cq.vfs_funcs.delete)(cq.vfs_data,COQUET_FILE_MAIN);
+    bail(&cq,r);
     r = coquet_finish(&cq);
     bail(&cq,r);
     return 0;
diff --git a/posix.c b/posix.c
index 693e9b84d56217baf3d80a2e39c22d4010ed777d..18f2ff1572a9cdbd8025d0dccdd14e498adf7a67 100644 (file)
--- a/posix.c
+++ b/posix.c
@@ -180,9 +180,8 @@ static int posix_open(void * vfs_data, int which_file,
     }
 
     r = sync_dir(pd);
-    if(r) {
-        set_error(pd,"Failed to sync parent directory",1);
-        return COQUET_RET_VFSERR;
+    if(r != COQUET_RET_OK) {
+        return r;
     }
 
     *fd_field = fd;
@@ -198,11 +197,14 @@ static int posix_close(void * vfs_data, int which_file) {
         set_error(pd,"File not open",0);
         return COQUET_RET_VFSERR;
     }
+    
     r = close(*fd_field);
     if(r==-1) {
         set_error(pd,"close failed",1);
         return COQUET_RET_VFSERR;
     }
+
+    *fd_field = -1;
     return COQUET_RET_OK;
 }
 
@@ -349,6 +351,38 @@ static int posix_lock(void * vfs_data, int which_lock, int lock_mode,
     return COQUET_RET_OK;
 }
 
+static int posix_delete(void *vfs_data, int which_file) {
+    struct posix_data * pd = (struct posix_data *)vfs_data;
+    int *fd, r;
+    char *path;
+
+    fd = file_fd(pd,which_file);
+    if(fd && *fd != -1) {
+        set_error(pd,"trying to delete open file",0);
+        return COQUET_RET_VFSERR;
+    }
+
+    path = filename(pd,which_file);
+    if(path == NULL) {
+        return COQUET_RET_HEAPERR;
+    }
+
+    r = unlink(path);
+    free(path);
+    if(r == -1) {
+        set_error(pd,"delete failed",1);
+        return COQUET_RET_VFSERR;
+    }
+
+    r = sync_dir(pd);
+    if(r != COQUET_RET_OK) {
+        return r;
+    }
+
+    return COQUET_RET_OK;
+}
+
+
 vfs_t vfs_posix = {
     .make = posix_make,
     .start = posix_start,
@@ -358,5 +392,6 @@ vfs_t vfs_posix = {
     .close = posix_close,
     .write = posix_write,
     .read = posix_read,
-    .finish = posix_finish
+    .finish = posix_finish,
+    .delete = posix_delete
 };
diff --git a/vfs.h b/vfs.h
index 8982bd9194e8ab89b7d09dbfecb6ad56cc2d43a8..8e144d59b50bee6e874e9a0a8968eb5f7dd81a75 100644 (file)
--- a/vfs.h
+++ b/vfs.h
@@ -74,6 +74,10 @@ typedef struct vfs {
      */
     int (*finish)(void *vfs_data);
 
+    /* Delete the given file. File will be closed.
+     */
+    int (*delete)(void *vfs_data, int which_file);
+
 } vfs_t;
 
 #endif