adds multiple file support for mkdir and remove

Tue, 26 Mar 2019 18:28:37 +0100

author
Mike Becker <universe@uap-core.de>
date
Tue, 26 Mar 2019 18:28:37 +0100
changeset 540
d18f92483945
parent 539
8deb52292c99
child 541
e59a989d890d

adds multiple file support for mkdir and remove

dav/main.c file | annotate | diff | comparison | revisions
--- a/dav/main.c	Tue Mar 26 18:11:34 2019 +0100
+++ b/dav/main.c	Tue Mar 26 18:28:37 2019 +0100
@@ -26,6 +26,7 @@
  * POSSIBILITY OF SUCH DAMAGE.
  */
 
+#include <assert.h>
 #include <stdio.h>
 #include <stdlib.h>
 #include <string.h>
@@ -216,9 +217,9 @@
 static char *cmdusageinfo[] = {
     "list [-altdepcR] [-u <date>] <url>",
     "get [-pcRK] [-o <file>] [-u <date>] [-V <version>] <url>",
-    "put [-pcR] [-k <key>] [-L <lock>] <url> <file>",
-    "mkdir [-pc] [-k <key>] [-L <lock>] <url>",
-    "remove [-pc] [-L <lock>] <url>",
+    "put [-pcR] [-k <key>] [-L <lock>] <url> <file...>",
+    "mkdir [-pc] [-k <key>] [-L <lock>] <url> [file...]",
+    "remove [-pc] [-L <lock>] <url> [file...]",
     "copy [-pcO] [-L <lock>] <url> <url>",
     "move [-pcO] [-L <lock>] <url> <url>",
     "rename [-pcO] [-L <lock>] <url> <name>",
@@ -1547,12 +1548,18 @@
     return 0;
 }
 
+static int dav_create_col(DavResource *res) {
+    res->iscollection = 1;
+    return dav_create(res);
+}
 
-int cmd_remove(CmdArgs *a) {
-    if(a->argc != 1) {
-        // TODO: change, when removal of multiple files is supported
-        fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many");
-        fprintf(stderr, "Usage: dav %s\n", find_usage_str("remove"));
+static int cmd_operation_on_resources(CmdArgs* a,
+                                      int(*operation)(DavResource*),
+                                      const char* command,
+                                      const char* message) {
+    if(a->argc < 1) {
+        fprintf(stderr, "Too few arguments\n");
+        fprintf(stderr, "Usage: dav %s\n", find_usage_str(command));
         return -1;
     }
     
@@ -1561,68 +1568,57 @@
     Repository *repo = url2repo(url, &path);
     DavSession *sn = connect_to_repo(repo, path, a);
     
+    int exit_code = -1;
+    assert(!!path && !!sn);
+    
     if(set_session_config(sn, a)) {
-        return -1;
+        goto cmd_oponres_exit;
     }
+    
     set_session_lock(sn, a);
     
+    if(check_encryption_key(a, sn)) {
+        goto cmd_oponres_exit;
+    }
+    
     DavResource *res = dav_resource_new(sn, path);
-    if(!res) {
-        fprintf(stderr, "error\n");
-        return -1;
+    assert(!!res);
+    res->iscollection = 1;
+    
+    if(a->argc == 1) {
+        if(operation(res)) {
+            fprintf(stderr, "Cannot %s.\n", message);
+            print_resource_error(sn, res->path);
+            goto cmd_oponres_exit;
+        }
+    } else {
+        for(int i = 1 ; i < a->argc ;++i) {
+            DavResource *child = dav_resource_new_child(sn, res, a->argv[i]);
+            assert(!!child);
+            child->iscollection = 1;
+            if(operation(child)) {
+                fprintf(stderr, "Cannot %s %s.\n", message, a->argv[i]);
+                print_resource_error(sn, child->path);
+                goto cmd_oponres_exit;
+            }
+        }
     }
     
-    if(dav_delete(res)) {
-        print_resource_error(sn, res->path);
-        fprintf(stderr, "Cannot delete resource.\n");
-        return -1;
-    }
-    
+    exit_code = 0;
+cmd_oponres_exit:
     free(path);
-    return 0;
+    dav_session_destroy(sn);
+    return exit_code;
+}
+
+int cmd_remove(CmdArgs *a) {
+    return cmd_operation_on_resources(a, dav_delete,
+            "remove", "delete resource");
 }
 
 int cmd_mkdir(CmdArgs *a) {
-    if(a->argc != 1) {
-        // TODO: change, when creation of multiple dirs is supported
-        fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many");
-        fprintf(stderr, "Usage: dav %s\n", find_usage_str("mkdir"));
-        return -1;
-    }
-    
-    char *url = a->argv[0];
-    char *path = NULL;
-    Repository *repo = url2repo(url, &path);
-    DavSession *sn = connect_to_repo(repo, path, a);
-    
-    if(set_session_config(sn, a)) {
-        return -1;
-    }
-    set_session_lock(sn, a);
-    
-    if(check_encryption_key(a, sn)) {
-        // TODO: free
-        return -1;
-    }
-    
-    DavResource *res = dav_resource_new(sn, path);
-    if(!res) {
-        fprintf(stderr, "error\n");
-        // TODO: free
-        return -1;
-    }
-    res->iscollection = 1;
-    
-    if(dav_create(res)) {
-        print_resource_error(sn, res->path);
-        fprintf(stderr, "Cannot create collection.\n");
-        // TODO: free
-        return -1;
-    }
-    
-    free(path);
-    dav_session_destroy(sn);
-    return 0;
+    return cmd_operation_on_resources(a, dav_create_col,
+            "mkdir", "create collection");
 }
 
 int cmd_move(CmdArgs *a, int cp) {

mercurial