Sun, 17 Apr 2022 11:51:37 +0200
update pg resource metadata on close
src/server/plugins/postgresql/vfs.c | file | annotate | diff | comparison | revisions | |
src/server/plugins/postgresql/vfs.h | file | annotate | diff | comparison | revisions |
--- a/src/server/plugins/postgresql/vfs.c Sun Apr 17 11:22:34 2022 +0200 +++ b/src/server/plugins/postgresql/vfs.c Sun Apr 17 11:51:37 2022 +0200 @@ -128,6 +128,11 @@ ($1, $2, true, now(), now(), 0)\n\ returning resource_id, lastmodified, creationdate;"; +// Update resource metadata +// params: $1: resource_id +// $2: contentlength +static const char *sql_update_resource = "update Resource set contentlength = $2, lastmodified = now() where resource_id = $1;"; + VFS* pg_vfs_create(Session *sn, Request *rq, pblock *pb) { // resourcepool is required char *resource_pool = pblock_findval("resourcepool", pb); @@ -455,6 +460,28 @@ return ret; } +int pg_update_resource(PgVFS *pg, int64_t resource_id, int64_t contentlength) { + char resid_str[32]; + char ctlen_str[32]; + snprintf(resid_str, 32, "%" PRId64, resource_id); + snprintf(ctlen_str, 32, "%" PRId64, contentlength); + + const char* params[2] = { resid_str, ctlen_str }; + PGresult *result = PQexecParams( + pg->connection, + sql_update_resource, + 2, // 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; +} + /* -------------------------- VFS functions -------------------------- */ SYS_FILE pg_vfs_open(VFSContext *ctx, const char *path, int oflags) { @@ -509,6 +536,7 @@ pgfile->parent_id = parent_id; pgfile->oid = oid; pgfile->fd = fd; + pgfile->oflags = oflags; pgfile->s = s; file->ctx = ctx; @@ -648,9 +676,18 @@ void pg_vfs_io_close(SYS_FILE fd) { pool_handle_t *pool = fd->ctx->pool; + PgVFS *pgvfs = fd->ctx->vfs->instance; PgFile *pg = fd->data; if(pg->fd >= 0) { + if((pg->oflags & (O_WRONLY|O_RDWR)) > 0) { + // file modified, update length and lastmodified + off_t len = pg_vfs_io_seek(fd, 0, SEEK_END); + if(len < 0) len = 0; + + pg_update_resource(pgvfs, pg->resource_id, len); + } + PgVFS *pgvfs = fd->ctx->vfs->instance; lo_close(pgvfs->connection, pg->fd); }
--- a/src/server/plugins/postgresql/vfs.h Sun Apr 17 11:22:34 2022 +0200 +++ b/src/server/plugins/postgresql/vfs.h Sun Apr 17 11:51:37 2022 +0200 @@ -50,6 +50,7 @@ WSBool iscollection; Oid oid; int fd; + int oflags; struct stat s; } PgFile; @@ -100,6 +101,8 @@ const char **resource_name, struct stat *s, WSBool collection); + +int pg_update_resource(PgVFS *pg, int64_t resource_id, int64_t contentlength); SYS_FILE pg_vfs_open(VFSContext *ctx, const char *path, int oflags); int pg_vfs_stat(VFSContext *ctx, const char *path, struct stat *buf);