added recursive put

Thu, 22 Aug 2013 11:25:16 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Thu, 22 Aug 2013 11:25:16 +0200
changeset 28
4e46c65711ef
parent 27
e584c351b402
child 29
938957a4eea7

added recursive put

dav/davql.c file | annotate | diff | comparison | revisions
dav/main.c file | annotate | diff | comparison | revisions
dav/main.h file | annotate | diff | comparison | revisions
dav/utils.c file | annotate | diff | comparison | revisions
--- a/dav/davql.c	Wed Aug 21 13:08:22 2013 +0200
+++ b/dav/davql.c	Thu Aug 22 11:25:16 2013 +0200
@@ -268,6 +268,8 @@
                         operation->type = DAVQOP_RESPROP;
                     } else if(!sstrcmp(token, S("path"))) {
                         operation->type = DAVQOP_RESPROP;
+                    } else if(!sstrcmp(token, S("iscollection"))) {
+                        operation->type = DAVQOP_RESPROP;
                     } else {
                         operation->type = DAVQOP_PROPERTY;
                     }
@@ -448,7 +450,7 @@
                     if(stackpos < 1) {
                         // error
                         printf("no data on stack\n");
-                        return NULL;
+                        return 0;
                     }
                     int pos = stackpos-1;
                     if(stack[pos].type == DAVQOP_INTEGER) {
@@ -457,7 +459,7 @@
                     } else {
                         // error
                         printf("wrong value for 'not' operator\n");
-                        return NULL;
+                        return 0;
                     }
                 } else {
                     DavQOp val1 = stack[stackpos-2];
@@ -535,6 +537,10 @@
                     value.type = DAVQOP_STRING;
                     value.val = res->path;
                     value.intval = strlen(res->path);
+                } else if(!sstrcmp(name, S("iscollection"))) {
+                    value.type = DAVQOP_INTEGER;
+                    value.val = NULL;
+                    value.intval = res->iscollection;
                 }
                 stack[stackpos++] = value;
                 break;
@@ -680,7 +686,7 @@
         case 13: {
             // xor
             printf("str compare: %.*s xor %.*s\n", s1.length, s1.ptr, s2.length, s2.ptr);
-            res.intval = (int)s1.ptr ^ (int)s2.ptr;
+            res.intval = (intptr_t)s1.ptr ^ (intptr_t)s2.ptr;
             break;
         }
     }
--- a/dav/main.c	Wed Aug 21 13:08:22 2013 +0200
+++ b/dav/main.c	Thu Aug 22 11:25:16 2013 +0200
@@ -36,6 +36,9 @@
 #include <sys/types.h>
 #include <sys/stat.h>
 #include <ucx/string.h>
+#include <sys/dirent.h>
+#include <dirent.h>
+
 
 #include "utils.h"
 #include "config.h"
@@ -581,15 +584,74 @@
         sn = dav_session_new(ctx, root);
     }
     
-    /*
-     * use stdin if the input file is -
-     */
-    FILE *in = !strcmp(file, "-") ? in : fopen(file, "r");
-    if(!in) {
-        fprintf(stderr, "cannot open input file\n");
+    int ret;
+    if(!strcmp(file, "-")) {
+        FILE *in = stdin;
+        ret = put_file(repo, a, sn, path, "stdin", in);
+    } else {
+        ret = put_entry(repo, a, sn, path, file);        
+    }
+    
+    return ret;
+}
+
+int put_entry(Repository *repo, CmdArgs *a, DavSession *sn, char *path, char *file) {
+    int recursive = cmd_getoption(a, "recursive") ? 1 : 0;
+    if(recursive) {
+        printf("put: %s\n", file);
+    }
+    struct stat s;
+    if(stat(file, &s)) {
+        perror("stat");
+        fprintf(stderr, "cannot stat file %s\n", file);
         return -1;
     }
     
+    int ret = 0;
+    if(S_ISDIR(s.st_mode)) {
+        if(!recursive) {
+            return 1;
+        }
+        DIR *dir = opendir(file);
+        if(!dir) {
+            // error
+        }
+        struct dirent *entry;
+        while((entry = readdir(dir)) != NULL) {
+            if(!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
+                continue;
+            }
+            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);
+            free(entry_path);
+            free(entry_file);
+            if(r) {
+                ret = 1;
+                break;
+            }
+        }
+        closedir(dir);
+    } else if(S_ISREG(s.st_mode)) {
+        /*
+         * use stdin if the input file is -
+         */
+        FILE *in = fopen(file, "r");
+        if(!in) {
+            fprintf(stderr, "cannot open input file\n");
+            return -1;
+        }
+        char *filename = util_resource_name(file);
+        //path = util_concat_path(path, filename);
+        ret = put_file(repo, a, sn, path, filename, in);
+        free(path);
+        fclose(in);
+    }
+    
+    return ret;
+}
+
+int put_file(Repository *repo, CmdArgs *a, DavSession *sn, char *path, char *name, FILE *in) {
     DavResource *res = dav_query(sn, "get - from %s", path);
     if(!res) {
         if(sn->error == DAV_NOT_FOUND) {
@@ -608,7 +670,7 @@
         }
     } else if(res->iscollection) {
         // TODO: free res
-        char *newpath = util_concat_path(path, file);
+        char *newpath = util_concat_path(path, name);
         free(path);
         path = newpath;
         res = dav_resource_new(sn, path);
@@ -651,10 +713,10 @@
     if(enc) {
         aes_encrypter_close(enc);
     }
-    fclose(in);
     return 0;
 }
 
+
 int cmd_remove(CmdArgs *a) {
     if(a->argc < 1) {
         fprintf(stderr, "Too few arguments\n");
--- a/dav/main.h	Wed Aug 21 13:08:22 2013 +0200
+++ b/dav/main.h	Thu Aug 22 11:25:16 2013 +0200
@@ -47,6 +47,9 @@
 int get_resource(Repository *repo, DavResource *res, CmdArgs *a, char *out);
 
 int cmd_put(CmdArgs *args);
+int put_entry(Repository *repo, CmdArgs *a, DavSession *sn, char *path, char *file);
+int put_file(Repository *repo, CmdArgs *a, DavSession *sn, char *path, char *name, FILE *in);
+
 int cmd_remove(CmdArgs *args);
 int cmd_mkdir(CmdArgs *args);
 
--- a/dav/utils.c	Wed Aug 21 13:08:22 2013 +0200
+++ b/dav/utils.c	Thu Aug 22 11:25:16 2013 +0200
@@ -98,18 +98,20 @@
     int si = 0;
     int osi = 0;
     int i = 0;
+    int p = 0;
     char c;
     while((c = url[i]) != 0) {
         if(c == '/') {
             osi = si;
             si = i;
+            p = 1;
         }
         i++;
     }
     
-    char *name = url + si + 1;;
+    char *name = url + si + p;
     if(name[0] == 0) {
-        name = url + osi + 1;
+        name = url + osi + p;
         if(name[0] == 0) {
             return url;
         }

mercurial