From: Dan Sheppard Date: Thu, 10 Apr 2025 12:28:13 +0000 (+0100) Subject: delete X-Git-Url: http://www.chiark.greenend.org.uk/ucgi/~dans/git?a=commitdiff_plain;h=8c9764d3d15fbe5be73abb92409aa947fa31a805;p=coquet.git delete --- diff --git a/coquet b/coquet index d2ea004..2a2d542 100755 Binary files a/coquet and b/coquet differ diff --git a/main.c b/main.c index c308131..4242725 100644 --- 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 693e9b8..18f2ff1 100644 --- 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 8982bd9..8e144d5 100644 --- 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