dav/main.c

changeset 28
4e46c65711ef
parent 27
e584c351b402
child 29
938957a4eea7
--- 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");

mercurial