src/server/plugins/postgresql/vfs.c

branch
webdav
changeset 374
77506ec632a4
parent 372
1d2538a1ba8f
child 379
4e2cb3adc0f2
--- a/src/server/plugins/postgresql/vfs.c	Sat Aug 13 15:56:51 2022 +0200
+++ b/src/server/plugins/postgresql/vfs.c	Sun Aug 14 11:40:54 2022 +0200
@@ -83,7 +83,7 @@
             regexp_split_to_array($1, '/') as pathelm,\n\
             1 as pathdepth\n\
         from Resource\n\
-        where parent_id is null\n\
+        where resource_id = $2\n\
         union\n\
         select\n\
             r.resource_id,\n\
@@ -106,7 +106,7 @@
 
 // Same as sql_resolve_path, but it returns the root collection
 // params: $1: path string (should be '/')
-static const char *sql_get_root = "select resource_id, parent_id, $1 as fullpath, resoid, true as iscollection, lastmodified, creationdate, contentlength, etag from Resource where parent_id is null;";
+static const char *sql_get_root = "select resource_id, parent_id, $1 as fullpath, resoid, true as iscollection, lastmodified, creationdate, contentlength, etag from Resource where resource_id = $2;";
 
 // Get all children of a specific collection
 // params: $1: parent resource_id
@@ -169,10 +169,10 @@
     }
     // resdata will be freed automatically when the request is finished
     
-    return pg_vfs_create_from_resourcedata(sn, rq, resdata);
+    return pg_vfs_create_from_resourcedata(sn, rq, repo, resdata);
 }
 
-VFS* pg_vfs_create_from_resourcedata(Session *sn, Request *rq, ResourceData *resdata) {
+VFS* pg_vfs_create_from_resourcedata(Session *sn, Request *rq, PgRepository *repo, ResourceData *resdata) {
     // Create a new VFS object and a separate instance object
     // VFS contains fptrs that can be copied from pg_vfs_class
     // instance contains request specific data (PGconn)
@@ -188,6 +188,8 @@
     }
     vfs_priv->connection = resdata->data;
     vfs_priv->pg_resource = resdata;
+    vfs_priv->root_resource_id = repo->root_resource_id;
+    snprintf(vfs_priv->root_resource_id_str, 32, "%" PRId64, repo->root_resource_id);
     
     memcpy(vfs, &pg_vfs_class, sizeof(VFS));
     vfs->flags = 0;
@@ -200,6 +202,7 @@
 int pg_resolve_path(
         PGconn *connection,
         const char *path,
+        const char *root_id,
         int64_t *parent_id,
         int64_t *resource_id,
         Oid *oid,
@@ -228,13 +231,14 @@
     // get last node of path
     *resource_name = util_resource_name(path);
     
-    const char *sql = pathlen == 1 ? sql_get_root : sql_resolve_path;  
+    const char *sql = pathlen == 1 ? sql_get_root : sql_resolve_path;
+    const char* params[2] = { path, root_id };
     PGresult *result = PQexecParams(
             connection,
             sql,
-            1,     // number of parameters
+            2,     // number of parameters
             NULL,
-            &path, // parameter value
+            params, // parameter value
             NULL,
             NULL,
             0);    // 0: result in text format
@@ -463,7 +467,18 @@
     WSBool iscollection;
     Oid unused_oid = 0;
     
-    int err = pg_resolve_path(pg->connection, parent_path, &parent_id, &resource_id, &unused_oid, &resname, &iscollection, NULL, NULL, &ctx->vfs_errno);
+    int err = pg_resolve_path(
+            pg->connection,
+            parent_path,
+            pg->root_resource_id_str,
+            &parent_id,
+            &resource_id,
+            &unused_oid,
+            &resname,
+            &iscollection,
+            NULL,
+            NULL,
+            &ctx->vfs_errno);
     FREE(parent_path);
     if(err) {
         ctx->vfs_errno = ENOENT;
@@ -589,7 +604,7 @@
     struct stat s;
     char etag[PG_ETAG_MAXLEN];
     Oid oid = 0;
-    if(pg_resolve_path(pg->connection, path, &parent_id, &resource_id, &oid, &resname, &iscollection, &s, etag, &ctx->vfs_errno)) {
+    if(pg_resolve_path(pg->connection, path, pg->root_resource_id_str, &parent_id, &resource_id, &oid, &resname, &iscollection, &s, etag, &ctx->vfs_errno)) {
         if((oflags & O_CREAT) == O_CREAT) {
             if(pg_create_file(ctx, pg, path, &resource_id, &parent_id, &oid, &resname, &s, FALSE)) {
                 return NULL;
@@ -664,7 +679,7 @@
     int64_t parent_id, resource_id;
     const char *resname;
     WSBool iscollection;
-    return pg_resolve_path(pg->connection, path, &parent_id, &resource_id, NULL, &resname, &iscollection, buf, NULL, &ctx->vfs_errno);
+    return pg_resolve_path(pg->connection, path, pg->root_resource_id_str, &parent_id, &resource_id, NULL, &resname, &iscollection, buf, NULL, &ctx->vfs_errno);
 }
 
 int pg_vfs_fstat(VFSContext *ctx, SYS_FILE fd, struct stat *buf) {
@@ -722,7 +737,7 @@
     WSBool iscollection;
     struct stat s;
     Oid oid = 0;
-    if(!pg_resolve_path(pg->connection, path, &parent_id, &resource_id, &oid, &resname, &iscollection, &s, NULL, &ctx->vfs_errno)) {
+    if(!pg_resolve_path(pg->connection, path, pg->root_resource_id_str, &parent_id, &resource_id, &oid, &resname, &iscollection, &s, NULL, &ctx->vfs_errno)) {
         ctx->vfs_errno = EEXIST;
         return 1;
     }
@@ -744,7 +759,7 @@
     parent_id = -1;
     WSBool iscollection;
     Oid oid = 0;
-    if(pg_resolve_path(pg->connection, path, &parent_id, &resource_id, &oid, &resname, &iscollection, NULL, NULL, &ctx->vfs_errno)) {
+    if(pg_resolve_path(pg->connection, path, pg->root_resource_id_str, &parent_id, &resource_id, &oid, &resname, &iscollection, NULL, NULL, &ctx->vfs_errno)) {
         return 1;
     }
     
@@ -765,7 +780,7 @@
     resource_id = -1;
     parent_id = -1;
     WSBool iscollection;
-    if(pg_resolve_path(pg->connection, path, &parent_id, &resource_id, NULL, &resname, &iscollection, NULL, NULL, &ctx->vfs_errno)) {
+    if(pg_resolve_path(pg->connection, path, pg->root_resource_id_str, &parent_id, &resource_id, NULL, &resname, &iscollection, NULL, NULL, &ctx->vfs_errno)) {
         return 1;
     }
     

mercurial