adds support for multiple file arguments for dav put

Fri, 07 Dec 2018 11:48:55 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 07 Dec 2018 11:48:55 +0100
changeset 500
0fe1514667e6
parent 499
efd8f489d415
child 501
868da3f76267

adds support for multiple file arguments for dav put

dav/main.c file | annotate | diff | comparison | revisions
dav/main.h file | annotate | diff | comparison | revisions
--- a/dav/main.c	Fri Dec 07 11:17:29 2018 +0100
+++ b/dav/main.c	Fri Dec 07 11:48:55 2018 +0100
@@ -1205,18 +1205,30 @@
 }
 
 int cmd_put(CmdArgs *a, DavBool import) {
-    if(a->argc != 2) {
-        // TODO: change, when put supports multiple files (however it should do)
-        fprintf(stderr, "Too %s arguments\n", a->argc < 2 ? "few":"many");
-        fprintf(stderr, "Usage: dav %s\n", find_usage_str("put"));
+    if(a->argc < 2) {
+        fprintf(stderr, "Too few arguments\n");
+        fprintf(stderr,
+                "Usage: dav %s\n",
+                find_usage_str(import ? "import" : "put"));
         return -1;
     }
+    DavBool use_stdin = FALSE;
+    for(int i=1;i<a->argc;i++) {
+        if(!strcmp(a->argv[i], "-")) {
+            if(use_stdin) {
+                fprintf(stderr, "Error: stdin can only occur once in input list\n");
+                return -1;
+            } else {
+                use_stdin = TRUE;
+            }
+        }
+    }
+    
     if(import) {
         ucx_map_cstr_put(a->options, "resursive", "");
     }
     
     char *url = a->argv[0];
-    char *file = a->argv[1];
     char *path = NULL;
     Repository *repo = url2repo(url, &path);
     DavSession *sn = connect_to_repo(repo, path, a);
@@ -1231,23 +1243,57 @@
         return -1;
     }    
     
+    DavBool printfile = FALSE;
+    DavBool ignoredirerr = FALSE;
+    if(a->argc > 2) {
+        printfile = TRUE;
+        ignoredirerr = TRUE;
+    } else if(ucx_map_cstr_get(a->options, "recursive")) {
+        printfile = TRUE;
+    }
+    
+    
     int ret;
-    if(!import) {
-        if(!strcmp(file, "-")) {
-            FILE *in = stdin;
-            ret = put_file(repo, a, sn, path, "stdin", in, 0);
+    for(int i=1;i<a->argc;i++) {
+        char *file = a->argv[i];
+        if(!import) {
+            if(!strcmp(file, "-")) {
+                FILE *in = stdin;
+                ret = put_file(repo, a, sn, path, "stdin", in, 0);
+            } else {
+                ret = put_entry(
+                        repo,
+                        a,
+                        sn,
+                        path,
+                        file,
+                        TRUE,
+                        printfile,
+                        ignoredirerr); 
+            }
         } else {
-            ret = put_entry(repo, a, sn, path, file, TRUE); 
+            ret = put_tar(repo, a, sn, file, path);
         }
-    } else {
-        ret = put_tar(repo, a, sn, file, path);
+        if(ret) {
+            break;
+        }
     }
     
     free(path);
+    dav_session_destroy(sn);
     return ret;
 }
 
-int put_entry(Repository *repo, CmdArgs *a, DavSession *sn, char *path, char *file,  DavBool root) {
+int put_entry(
+        Repository *repo,
+        CmdArgs *a,
+        DavSession *sn,
+        char *path,
+        char *file,
+        DavBool root,
+        DavBool printfile,
+        DavBool ignoredirerr)
+{
     int recursive = cmd_getoption(a, "recursive") ? 1 : 0;
     struct stat s;
     if(stat(file, &s)) {
@@ -1259,11 +1305,15 @@
     int ret = 0;
     if(S_ISDIR(s.st_mode)) {
         if(!recursive) {
-            fprintf(
+            if(ignoredirerr) {
+                printf("skip: %s\n", file);
+            } else {
+                fprintf(
                     stderr,
                     "%s is a directory.\nUse the -R option to upload directories.\n",
                     file);
-            return 1;
+                return 1;
+            }
         }
         
         if(!root) {
@@ -1283,7 +1333,15 @@
             nument++;
             char *entry_file = util_concat_path(file, entry->d_name);
             char *entry_path = util_concat_path(path, entry->d_name);
-            int r = put_entry(repo, a, sn, entry_path, entry_file, FALSE);
+            int r = put_entry(
+                    repo,
+                    a,
+                    sn,
+                    entry_path,
+                    entry_file,
+                    FALSE,
+                    printfile,
+                    ignoredirerr);
             free(entry_path);
             free(entry_file);
             if(r) {
@@ -1307,7 +1365,7 @@
             dav_resource_free(res);
         }
     } else if(S_ISREG(s.st_mode)) {
-        if(recursive) {
+        if(printfile) {
             printf("put: %s\n", file);
         }
         
--- a/dav/main.h	Fri Dec 07 11:17:29 2018 +0100
+++ b/dav/main.h	Fri Dec 07 11:48:55 2018 +0100
@@ -74,7 +74,15 @@
 int resource2tar(Repository *repo, GetResource *res, CmdArgs *a, TarOutputStream *tar);
 
 int cmd_put(CmdArgs *args, DavBool import);
-int put_entry(Repository *repo, CmdArgs *a, DavSession *sn, char *path, char *file, DavBool root);
+int put_entry(
+        Repository *repo,
+        CmdArgs *a,
+        DavSession *sn,
+        char *path,
+        char *file,
+        DavBool root,
+        DavBool printfile,
+        DavBool ignoredirerr);
 int put_tar(Repository *repo, CmdArgs *a, DavSession *sn, char *tarfile, char *path);
 int put_file(Repository *repo, CmdArgs *a, DavSession *sn, char *path, char *name, FILE *in, off_t len);
 

mercurial