adds auth prompt to dav get-property, set-property, lock, unlock and info

Fri, 09 Dec 2016 10:55:18 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 09 Dec 2016 10:55:18 +0100
changeset 258
571b62700df6
parent 257
49f3c58cc06c
child 259
6c8d5f8a1d6f

adds auth prompt to dav get-property, set-property, lock, unlock and info

dav/main.c file | annotate | diff | comparison | revisions
dav/version.h file | annotate | diff | comparison | revisions
--- a/dav/main.c	Fri Dec 02 13:24:01 2016 +0100
+++ b/dav/main.c	Fri Dec 09 10:55:18 2016 +0100
@@ -231,8 +231,14 @@
         fflush(stderr);
         user = fgets(ubuf, 256, stdin);
     }
+    if(!user) {
+        return 0;
+    }
     
     char *password = util_password_input("Password: ");
+    if(!password || strlen(password) == 0) {
+        return 0;
+    }
     
     size_t ulen = strlen(user);
     if(user[ulen-1] == '\n') {
@@ -1109,7 +1115,8 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    DavSession *sn = connect_to_repo(url2repo(url, &path), a);
+    Repository *repo = url2repo(url, &path);
+    DavSession *sn = connect_to_repo(repo, a);
     
     if(set_session_config(sn, a)) {
         return -1;
@@ -1119,9 +1126,14 @@
     char *property = a->argv[1];
     
     DavResource *res = dav_resource_new(sn, path);
-    if(dav_load(res)) {
-        print_resource_error(sn, res->path);
-        return -1;
+    for(int i=0;i<2;i++) {
+        if(dav_load(res)) {
+            if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) {
+                continue;
+            }
+            print_resource_error(sn, res->path);
+            return -1;
+        }
     }
     
     char *value = namespace ?
@@ -1153,7 +1165,8 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    DavSession *sn = connect_to_repo(url2repo(url, &path), a);
+    Repository *repo = url2repo(url, &path);
+    DavSession *sn = connect_to_repo(repo, a);
     
     if(set_session_config(sn, a)) {
         return -1;
@@ -1161,9 +1174,14 @@
     set_session_lock(sn, a);
     
     DavResource *res = dav_resource_new(sn, path);
-    if(!dav_exists(res)) {
-        print_resource_error(sn, res->path);
-        return -1;
+    for(int i=0;i<2;i++) {
+        if(!dav_exists(res)) {
+            if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) {
+                continue;
+            }
+            print_resource_error(sn, res->path);
+            return -1;
+        }
     }
     
     char *namespace = cmd_getoption(a, "namespace");
@@ -1176,14 +1194,21 @@
         dav_set_property(res, property, value);
     }
     
-    if(dav_store(res)) {
-        print_resource_error(sn, res->path);
-        fprintf(stderr, "Cannot set property.\n");
-        return -1;
+    int ret = 0;
+    for(int i=0;i<2;i++) {
+        if(dav_store(res)) {
+            if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) {
+                continue;
+            }
+            print_resource_error(sn, res->path);
+            fprintf(stderr, "Cannot set property.\n");
+            ret = -1;
+            break;
+        }
     }
     
     free(path);
-    return 0;
+    return ret;
 }
 
 int cmd_lock(CmdArgs *a) {
@@ -1194,7 +1219,8 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    DavSession *sn = connect_to_repo(url2repo(url, &path), a);
+    Repository *repo = url2repo(url, &path);
+    DavSession *sn = connect_to_repo(repo, a);
     ucx_mempool_reg_destr(sn->mp, path, free);
     
     if(set_session_config(sn, a)) {
@@ -1202,9 +1228,14 @@
     }
     
     DavResource *res = dav_resource_new(sn, path);
-    if(dav_lock(res)) {
-        print_resource_error(sn, res->path);
-        return -1;
+    for(int i=0;i<2;i++) {
+        if(dav_lock(res)) {
+            if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) {
+                continue;
+            }
+            print_resource_error(sn, res->path);
+            return -1;
+        }
     }
     
     DavLock *lock = dav_get_lock(sn, res->path);
@@ -1248,6 +1279,7 @@
     
     char *url = a->argv[0];
     char *path = NULL;
+    Repository *repo = url2repo(url, &path);
     DavSession *sn = connect_to_repo(url2repo(url, &path), a);
     ucx_mempool_reg_destr(sn->mp, path, free);
     if(set_session_config(sn, a)) {
@@ -1269,14 +1301,21 @@
         free(locktoken);
     }
     
+    int ret = 0;
     DavResource *res = dav_resource_new(sn, path);
-    if(dav_unlock(res)) {
-        print_resource_error(sn, res->path);
-        return -1;
+    for(int i=0;i<2;i++) {
+        if(dav_unlock(res)) {
+            if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) {
+                continue;
+            }
+            print_resource_error(sn, res->path);
+            ret = -1;
+            break;
+        }
     }
     
     dav_session_destroy(sn);
-    return 0;
+    return ret;
 }
 
 static int count_children(DavResource *res) {
@@ -1297,55 +1336,62 @@
     
     char *url = a->argv[0];
     char *path = NULL;
-    DavSession *sn = connect_to_repo(url2repo(url, &path), a);
+    Repository *repo = url2repo(url, &path);
+    DavSession *sn = connect_to_repo(repo, a);
     
     if(set_session_config(sn, a)) {
         return -1;
     }
     
     DavResource *res = dav_resource_new(sn, path);
-    if(!dav_load(res)) {
-        printf("name: %s\n", res->name);
-        printf("path: %s\n", res->path);
-        
-        char *server = util_url_base(sn->base_url);
-        char *url = util_concat_path(server, res->href);
-        printf("url:  %s\n", url);
-        free(url);
-        free(server);
-        
-        if(res->iscollection) {
-            printf("type: collection\n");
-            printf("size: %d\n", count_children(res));
+    for(int i=0;i<2;i++) {
+        if(!dav_load(res)) {
+            printf("name: %s\n", res->name);
+            printf("path: %s\n", res->path);
+
+            char *server = util_url_base(sn->base_url);
+            char *url = util_concat_path(server, res->href);
+            printf("url:  %s\n", url);
+            free(url);
+            free(server);
+
+            if(res->iscollection) {
+                printf("type: collection\n");
+                printf("size: %d\n", count_children(res));
+            } else {
+                printf("type: resource\n");
+                char *len = ls_size_str(res);
+                printf("size: %s\n", len);
+                free(len);
+            }
+
+            size_t count = 0;
+            DavPropName *properties = dav_get_property_names(res, &count);
+
+            char *last_ns = NULL;
+            for(int i=0;i<count;i++) {
+                DavPropName p = properties[i];
+                if(!last_ns || strcmp(last_ns, p.ns)) {
+                    printf("\nnamespace: %s\n", p.ns);
+                    last_ns = p.ns;
+                }
+
+                sstr_t value = sstr(dav_get_property_ns(res, p.ns, p.name));
+                value = sstrtrim(value);
+                printf("  %s: %.*s\n", p.name, (int)value.length, value.ptr);
+            }
+
+            dav_session_free(sn, properties);
+            return 0;
         } else {
-            printf("type: resource\n");
-            char *len = ls_size_str(res);
-            printf("size: %s\n", len);
-            free(len);
+            if(i == 0 && sn->error == DAV_UNAUTHORIZED && request_auth(repo, sn, a)) {
+                continue;
+            }
+            print_resource_error(sn, res->path);
+            break;
         }
-        
-        size_t count = 0;
-        DavPropName *properties = dav_get_property_names(res, &count);
-        
-        char *last_ns = NULL;
-        for(int i=0;i<count;i++) {
-            DavPropName p = properties[i];
-            if(!last_ns || strcmp(last_ns, p.ns)) {
-                printf("\nnamespace: %s\n", p.ns);
-                last_ns = p.ns;
-            }
-            
-            sstr_t value = sstr(dav_get_property_ns(res, p.ns, p.name));
-            value = sstrtrim(value);
-            printf("  %s: %.*s\n", p.name, (int)value.length, value.ptr);
-        }
-        
-        dav_session_free(sn, properties);
-        return 0;
-    } else {
-        print_resource_error(sn, res->path);
-        return -1;
     }
+    return -1;
 }
 
 
--- a/dav/version.h	Fri Dec 02 13:24:01 2016 +0100
+++ b/dav/version.h	Fri Dec 09 10:55:18 2016 +0100
@@ -29,7 +29,7 @@
 #ifndef VERSION_H
 #define VERSION_H
 
-#define DAV_VERSION "1.0.0 RC3"
+#define DAV_VERSION "1.0.0 RC4"
 
 #endif /* VERSION_H */
 

mercurial