dav/main.c

changeset 38
b855f76e965b
parent 36
c8755c87ce7f
child 40
a95ee94b9204
--- a/dav/main.c	Mon Sep 02 10:50:29 2013 +0200
+++ b/dav/main.c	Tue Sep 03 12:08:35 2013 +0200
@@ -78,7 +78,6 @@
     } else if(!strcasecmp(cmd, "get")) {
         ret = cmd_get(args);
     } else if(!strcasecmp(cmd, "put")) {
-        printf("put\n");
         ret = cmd_put(args);
     } else if(
             !strcasecmp(cmd, "remove") ||
@@ -133,9 +132,44 @@
     fprintf(stderr, "\n");
 }
 
-void url_get_parts(char *url, char **root, char **path) {
+int request_auth(Repository *repo, DavSession *sn) {
+    static int login = 0;
+    if(login) {
+        return 0;
+    }
+    
+    char *user = NULL;
+    char *password = NULL;
+    char ubuf[256];
+    char pbuf[256];
+    if(repo->user) {
+       user = repo->user; 
+    } else {
+        printf("user: ");
+        fflush(stdout);
+        user = fgets(ubuf, 256, stdin);
+    }
+    
+    printf("password: ");
+    fflush(stdout);
+    password = fgets(pbuf, 256, stdin);
+    
+    size_t ulen = strlen(user);
+    if(user[ulen-1] == '\n') {
+        user[ulen-1] = '\0';
+    }
+    size_t plen = strlen(password);
+    if(password[plen-1] == '\n') {
+        password[plen-1] = '\0';
+    }
+    
+    dav_session_set_auth(sn, user, password);
+    login = 1;
+    return 1;
+}
+
+Repository* url2repo(char *url, char **path) {
     size_t ulen = strlen(url);
-    *root = NULL;
     *path = NULL;
     
     int s;
@@ -147,22 +181,33 @@
         s = 1;
     }
     
+    sstr_t r = sstr(url);
+    sstr_t p = sstr("/");
     for(int i=s;i<ulen;i++) {
         char c = url[i];
         if(c == '/') {
-            sstr_t r = sstrn(url, i);
-            sstr_t p = sstrsubs(sstr(url), i);
+            r = sstrn(url, i);
+            p = sstrsubs(sstr(url), i);
             if(p.length == 0) {
                 p = sstrn("/", 1);
             }
-            *root = sstrdup(r).ptr;
-            *path = sstrdup(p).ptr;
-            return;
+            break;
         }
     }
     
-    *root = strdup(url);
-    *path = strdup("/");
+    Repository *repo = get_repository(r);
+    if(repo) {
+        *path = sstrdup(p).ptr;
+    } else {
+        repo = calloc(1, sizeof(Repository));
+        repo->name = "";
+        repo->url = strdup(url);
+        repo->store_key_property = true;
+        repo->decrypt = true;
+        *path = strdup("/");
+    }
+    
+    return repo;
 }
 
 void print_resource_error(DavSession *sn, char *path) {
@@ -204,21 +249,13 @@
         return -1;
     }
     
-    DavSession *sn = NULL;
     char *url = a->argv[0];
-    char *root = NULL;
     char *path = NULL;
     char *base = NULL;
-    url_get_parts(url, &root, &path);
-    
-    Repository *repo = get_repository(root);
-    if(repo) {
-        base = util_concat_path(repo->url, path);
-        sn = dav_session_new_auth(ctx, base, repo->user, repo->password);
-    } else {
-        base = util_concat_path(root, path);
-        sn = dav_session_new(ctx, base);
-    }
+    DavSession *sn = NULL;
+    Repository *repo = url2repo(url, &path);
+    base = util_concat_path(repo->url, path);
+    sn = dav_session_new_auth(ctx, base, repo->user, repo->password);
     
     char *update = cmd_getoption(a, "update");
     time_t t = 0;
@@ -226,55 +263,63 @@
         t = util_parse_lastmodified(update);
     }
     
+    int ret = -1;
     DavResource *ls;
-    if(cmd_getoption(a, "recursive")) {
-        printf("base: %s\n", base);
-        if(update) {
-            ls = dav_query(
-                    sn,
-                    "get U:crypto-key from /* where lastmodified > %t",
-                    t);
+    while(ret != 0) {
+        if(cmd_getoption(a, "recursive")) {
+            printf("base: %s\n", base);
+            if(update) {
+                ls = dav_query(
+                        sn,
+                        "get U:crypto-key from /* where lastmodified > %t",
+                        t);
+            } else {
+                ls = dav_query(sn, "get U:crypto-key from /*");
+            }
         } else {
-            ls = dav_query(sn, "get U:crypto-key from /*");
+            if(update) {
+                ls = dav_query(
+                        sn,
+                        "get U:crypto-key from / where lastmodified > %t", t);
+            } else {
+                ls = dav_query(sn, "get U:crypto-key from /");
+            }
         }
-    } else {
-        if(update) {
-            ls = dav_query(
-                    sn,
-                    "get U:crypto-key from / where lastmodified > %t", t);
-        } else {
-            ls = dav_query(sn, "get U:crypto-key from /");
+        
+        if(!ls) {
+            if(sn->error == DAV_UNAUTHORIZED) {
+                if(request_auth(repo, sn)) {
+                    continue;
+                }
+            }
+            print_resource_error(sn, path);
+            break;
         }
-    }
-    if(!ls) {
-        print_resource_error(sn, path);
-        free(root);
-        free(path);
-        free(base);
-        return -1;
+        
+        // parameters
+        int show_all = cmd_getoption(a, "all") ? 1 : 0;
+        void (*print_func)(DavResource*, CmdArgs *);
+        if(cmd_getoption(a, "list")) {
+            print_func = ls_print_list_elm;
+        } else {
+            print_func = ls_print_elm;
+        }
+        DavResource *child = ls->children;
+        while(child) {
+            if(child->name[0] != '.' || show_all) {
+                print_func(child, a);
+            }
+            child = child->next;
+        }
+        
+        // leave loop
+        ret = 0;
     }
     
-    // parameters
-    int show_all = cmd_getoption(a, "all") ? 1 : 0;
-    void (*print_func)(DavResource*, CmdArgs *);
-    if(cmd_getoption(a, "list")) {
-        print_func = ls_print_list_elm;
-    } else {
-        print_func = ls_print_elm;
-    }
-    DavResource *child = ls->children;
-    while(child) {
-        if(child->name[0] != '.' || show_all) {
-            print_func(child, a);
-        }
-        child = child->next;
-    }
-    
-    free(root);
     free(path);
     free(base);
     
-    return 0;
+    return ret;
 }
 
 static char* ls_date_str(time_t tm) {
@@ -436,18 +481,11 @@
         return -1;
     }
     
-    DavSession *sn = NULL;
     char *url = a->argv[0];
-    char *root = NULL;
     char *path = NULL;
-    url_get_parts(url, &root, &path);
-    
-    Repository *repo = get_repository(root);
-    if(repo) {
-        sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
-    } else {
-        sn = dav_session_new(ctx, root);
-    }
+    DavSession *sn = NULL;
+    Repository *repo = url2repo(url, &path);
+    sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
     
     char *update = cmd_getoption(a, "update");
     time_t t = 0;
@@ -628,19 +666,12 @@
         return -1;
     }
     
-    DavSession *sn = NULL;
     char *url = a->argv[0];
     char *file = a->argv[1];
-    char *root = NULL;
     char *path = NULL;
-    url_get_parts(url, &root, &path);
-    
-    Repository *repo = get_repository(root);
-    if(repo) {
-        sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
-    } else {
-        sn = dav_session_new(ctx, root);
-    }
+    DavSession *sn = NULL;
+    Repository *repo = url2repo(url, &path);
+    sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
     
     int ret;
     if(!strcmp(file, "-")) {
@@ -782,18 +813,11 @@
         return -1;
     }
     
-    DavSession *sn = NULL;
     char *url = a->argv[0];
-    char *root = NULL;
     char *path = NULL;
-    url_get_parts(url, &root, &path);
-    
-    Repository *repo = get_repository(root);
-    if(repo) {
-        sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
-    } else {
-        sn = dav_session_new(ctx, root);
-    }
+    DavSession *sn = NULL;
+    Repository *repo = url2repo(url, &path);
+    sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
     
     DavResource *res = dav_resource_new(sn, path);
     if(!res) {
@@ -817,18 +841,11 @@
         return -1;
     }
     
-    DavSession *sn = NULL;
     char *url = a->argv[0];
-    char *root = NULL;
     char *path = NULL;
-    url_get_parts(url, &root, &path);
-    
-    Repository *repo = get_repository(root);
-    if(repo) {
-        sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
-    } else {
-        sn = dav_session_new(ctx, root);
-    }
+    DavSession *sn = NULL;
+    Repository *repo = url2repo(url, &path);
+    sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
     
     DavResource *res = dav_resource_new(sn, path);
     if(!res) {
@@ -869,18 +886,11 @@
         size_t len = strftime(str, 32, "%a, %d %b %Y %H:%M:%S GMT\n", date);
         fwrite(str, 1, len, stdout);
     } else if (a->argc == 1) {
-        DavSession *sn = NULL;
         char *url = a->argv[0];
-        char *root = NULL;
         char *path = NULL;
-        url_get_parts(url, &root, &path);
-
-        Repository *repo = get_repository(root);
-        if(repo) {
-            sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
-        } else {
-            sn = dav_session_new(ctx, root);
-        }
+        DavSession *sn = NULL;
+        Repository *repo = url2repo(url, &path);
+        sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
 
         DavResource *res = dav_resource_new(sn, path);
         char *date = NULL;

mercurial