# HG changeset patch # User Olaf Wintermann # Date 1650280670 -7200 # Node ID 78e92337fa4ed92e1a93ab11b7a509abc1877d69 # Parent 73a1243fce15e813e98575ed9c16631e09a4db73 implement pg rmdir diff -r 73a1243fce15 -r 78e92337fa4e src/server/plugins/postgresql/pgtest.c --- a/src/server/plugins/postgresql/pgtest.c Mon Apr 18 11:49:37 2022 +0200 +++ b/src/server/plugins/postgresql/pgtest.c Mon Apr 18 13:17:50 2022 +0200 @@ -46,6 +46,21 @@ static PGconn *test_connection; static ResourceData resdata; +void debug_print_resources(void) { + PGresult *result = PQexec(test_connection, "select * from Resource;"); + int n = PQntuples(result); + printf("\nntuples: %d\n-----------------------------------------------\n", n); + printf("%10s %10s %s\n", "resource_id", "parent_id", "nodename"); + for(int i=0;ipool, "PUT", "/"); + rq->vfs = create_test_pgvfs(sn, rq); + VFSContext *vfs = vfs_request_context(sn, rq); + + PQexec(test_connection, "delete from Resource where parent_id is not null;"); + + UCX_TEST_BEGIN; + + int r; + SYS_FILE f1; + + // prepare some dirs/files + r = vfs_mkdir(vfs, "/rmdir_test"); + UCX_TEST_ASSERT(r == 0, "mkdir failed (1)"); + r = vfs_mkdir(vfs, "/rmdir_test/subdir1"); + UCX_TEST_ASSERT(r == 0, "mkdir failed (2)"); + r = vfs_mkdir(vfs, "/rmdir_test/subdir2"); + UCX_TEST_ASSERT(r == 0, "mkdir failed (3)"); + + f1 = vfs_open(vfs, "/rmdir_test/subdir2/file", O_CREAT|O_WRONLY); + UCX_TEST_ASSERT(f1, "open failed"); + vfs_close(f1); + + // test rmdir + r = vfs_rmdir(vfs, "/rmdir_test/subdir1"); + UCX_TEST_ASSERT(r == 0, "rmdir failed");; + + //r = vfs_rmdir(vfs, "/rmdir_test/subdir2"); + //UCX_TEST_ASSERT(r != 0, "rmdir should fail if the dir is not empty"); + + r = vfs_unlink(vfs, "/rmdir_test/subdir2/file"); + UCX_TEST_ASSERT(r == 0, "unlink failed"); + + r = vfs_rmdir(vfs, "/rmdir_test/subdir2"); + UCX_TEST_ASSERT(r == 0, "rmdir failed 2"); + + UCX_TEST_END; + + testutil_destroy_session(sn); +} diff -r 73a1243fce15 -r 78e92337fa4e src/server/plugins/postgresql/pgtest.h --- a/src/server/plugins/postgresql/pgtest.h Mon Apr 18 11:49:37 2022 +0200 +++ b/src/server/plugins/postgresql/pgtest.h Mon Apr 18 13:17:50 2022 +0200 @@ -27,6 +27,7 @@ UCX_TEST(test_pg_vfs_stat); UCX_TEST(test_pg_vfs_mkdir); UCX_TEST(test_pg_vfs_unlink); +UCX_TEST(test_pg_vfs_rmdir); #ifdef __cplusplus } diff -r 73a1243fce15 -r 78e92337fa4e src/server/plugins/postgresql/vfs.c --- a/src/server/plugins/postgresql/vfs.c Mon Apr 18 11:49:37 2022 +0200 +++ b/src/server/plugins/postgresql/vfs.c Mon Apr 18 13:17:50 2022 +0200 @@ -135,7 +135,7 @@ // Delete a resource // params: $1: resource_id -static const char *sql_delete_res = "delete from Resource where resource_id = $1;"; +static const char *sql_delete_res = "delete from Resource where parent_id is not null and resource_id = $1;"; VFS* pg_vfs_create(Session *sn, Request *rq, pblock *pb) { // resourcepool is required @@ -464,7 +464,7 @@ return ret; } -int pg_remove_file( +int pg_remove_res( VFSContext *ctx, PgVFS *pg, int64_t resource_id, @@ -489,9 +489,12 @@ NULL, NULL, 0); // 0: result in text format - + int ret = PQresultStatus(result) == PGRES_COMMAND_OK ? 0 : 1; - PQclear(result); + if(ret == 1) { + fprintf(stdout, "%s", PQerrorMessage(pg->connection)); + } + PQclear(result); return ret; } @@ -676,11 +679,29 @@ return 1; } - return pg_remove_file(ctx, pg, resource_id, oid); + return pg_remove_res(ctx, pg, resource_id, oid); } int pg_vfs_rmdir(VFSContext *ctx, const char *path) { + 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; + if(pg_resolve_path(ctx, path, &parent_id, &resource_id, NULL, &resname, &iscollection, NULL)) { + ctx->vfs_errno = ENOENT; + return 1; + } + + if(!iscollection) { + ctx->vfs_errno = ENOTDIR; + return 1; + } + + return pg_remove_res(ctx, pg, resource_id, 0); } diff -r 73a1243fce15 -r 78e92337fa4e src/server/plugins/postgresql/vfs.h --- a/src/server/plugins/postgresql/vfs.h Mon Apr 18 11:49:37 2022 +0200 +++ b/src/server/plugins/postgresql/vfs.h Mon Apr 18 13:17:50 2022 +0200 @@ -102,7 +102,7 @@ struct stat *s, WSBool collection); -int pg_remove_file( +int pg_remove_res( VFSContext *ctx, PgVFS *pg, int64_t resource_id,