src/server/plugins/postgresql/vfs.c

branch
webdav
changeset 294
277a5896a2ec
parent 292
7c31bcd5b4be
child 295
73a1243fce15
--- a/src/server/plugins/postgresql/vfs.c	Sun Apr 17 12:04:41 2022 +0200
+++ b/src/server/plugins/postgresql/vfs.c	Mon Apr 18 10:53:13 2022 +0200
@@ -133,6 +133,10 @@
 //         $2: contentlength
 static const char *sql_update_resource = "update Resource set contentlength = $2, lastmodified = now() where resource_id = $1;";
 
+// Delete a resource
+// params: $1: resource_id
+static const char *sql_delete_res = "delete from Resource where resource_id = $1;";
+
 VFS* pg_vfs_create(Session *sn, Request *rq, pblock *pb) {
     // resourcepool is required
     char *resource_pool = pblock_findval("resourcepool", pb);
@@ -460,6 +464,37 @@
     return ret;
 }
 
+int pg_remove_file(
+        VFSContext *ctx,
+        PgVFS *pg,
+        int64_t resource_id,
+        Oid oid)
+{
+    if(oid > 0) {
+        if(lo_unlink(pg->connection, oid) != 1) {
+            return 1; // error
+        }
+    }
+    
+    char resid_str[32];
+    snprintf(resid_str, 32, "%" PRId64, resource_id);
+    
+    const char* params[1] = { resid_str };
+    PGresult *result = PQexecParams(
+            pg->connection,
+            sql_delete_res,
+            1,     // number of parameters
+            NULL,
+            params, // parameter value
+            NULL,
+            NULL,
+            0);    // 0: result in text format
+    
+    int ret = PQresultStatus(result) == PGRES_COMMAND_OK ? 0 : 1;
+    PQclear(result);
+    return ret;
+}
+
 int pg_update_resource(PgVFS *pg, int64_t resource_id, int64_t contentlength) {
     char resid_str[32];
     char ctlen_str[32];
@@ -622,7 +657,26 @@
 }
 
 int pg_vfs_unlink(VFSContext *ctx, const char *path) {
-    return 1;
+    VFS *vfs = ctx->vfs;
+    PgVFS *pg = vfs->instance;
+    
+    const char *resname;
+    int64_t resource_id, parent_id;
+    resource_id = -1;
+    parent_id = -1;
+    WSBool iscollection;
+    Oid oid = 0;
+    if(pg_resolve_path(ctx, path, &parent_id, &resource_id, &oid, &resname, &iscollection, NULL)) {
+        ctx->vfs_errno = ENOENT;
+        return 1;
+    }
+    
+    if(iscollection) {
+        ctx->vfs_errno = EISDIR;
+        return 1;
+    }
+    
+    return pg_remove_file(ctx, pg, resource_id, oid);
 }
 
 int pg_vfs_rmdir(VFSContext *Ctx, const char *path) {

mercurial