improved copy/move

Mon, 25 Jan 2016 11:39:46 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 25 Jan 2016 11:39:46 +0100
changeset 186
05d11fbcaeb3
parent 185
cd42cccee550
child 187
38295f2fa231

improved copy/move

dav/main.c file | annotate | diff | comparison | revisions
dav/optparser.c file | annotate | diff | comparison | revisions
libidav/resource.c file | annotate | diff | comparison | revisions
libidav/utils.c file | annotate | diff | comparison | revisions
libidav/webdav.h file | annotate | diff | comparison | revisions
--- a/dav/main.c	Mon Jan 25 10:48:58 2016 +0100
+++ b/dav/main.c	Mon Jan 25 11:39:46 2016 +0100
@@ -119,8 +119,6 @@
         ret = cmd_get_property(args);
     } else if(!strcasecmp(cmd, "info")) {
         ret = cmd_info(args);
-    } else if(!strcasecmp(cmd, "add-repository")) {
-        ret = cmd_add_repository(args);
     } else {
         print_usage(argv[0]);
     }
@@ -961,10 +959,37 @@
             return -1;
         }
     } else {
-        fprintf(stderr, "Copy or Move not supported for different hosts.\n");
-        return -1;
+        char *server1 = util_url_base(repo1->url);
+        char *server2 = util_url_base(repo2->url);     
+        if(!strcmp(server1, server2)) {
+            DavSession *sn2 = connect_to_repo(repo2);
+            if(set_session_config(sn2, a)) {
+                return -1;
+            }
+            DavResource *dest = dav_resource_new(sn2, path2);
+            char *desthref = dav_resource_get_href(dest);
+            char *desturl = util_get_url(sn2, desthref);
+            
+            DavResource *res = dav_resource_new(sn, path1);
+            int err = cp ? dav_copyto(res, desturl, override)
+                     : dav_moveto(res, desturl, override);
+            
+            free(desturl);
+            dav_session_destroy(sn2);
+            
+            if(err) {
+                print_resource_error(sn, res->path);
+                fprintf(stderr, "Cannot %s resource.\n", cp ? "copy" : "move");
+                return -1;
+            }
+        } else {
+            fprintf(stderr, "Copy or Move not supported for different hosts.\n");
+            return -1;
+        }
     }
     
+    dav_session_destroy(sn);
+    
     return 0;
 }
 
@@ -1251,50 +1276,3 @@
     
     return;
 }
-
-int cmd_add_repository(CmdArgs *a) {
-    char *interactive = cmd_getoption(a, "interactive");
-    
-    if(!interactive && a->argc < 2) {
-        fprintf(stderr, "Too few arguments\n");
-        fprintf(stderr, "Alternatively, use the interactive mode");
-        return -1;
-    }
-    
-    Repository *repo = repository_new();
-    // create repo object and fill config with cmd args
-    for(int i=0;i<a->argc;i++) {
-        char *key;
-        char *value;
-        if(!getkeyvalue(a->argv[i], &key, &value)) {
-            if(!strcmp(key, "password")) {
-                // passwords from command line args are plain text
-                // but the repository needs a base64 encoded password
-                char *pw = util_base64encode(value, strlen(value));
-                free(value);
-                value = pw;
-            }
-            
-            if(repo_add_config(repo, key, value)) {
-                fprintf(
-                        stderr,
-                        "Unkown repository config element: %s\n",
-                        key);
-            }
-            
-            free(key);
-            free(value);
-        } else {
-            fprintf(stderr, "´%s´ not in key=value format\n", a->argv[i]);
-            // TODO: cleanup
-            return -1;
-        }
-    }
-    
-    if(interactive) {
-        // start interactive assistent to adjust config
-        repository_assistent(repo, 0);
-    }
-    
-    return 0;
-}
--- a/dav/optparser.c	Mon Jan 25 10:48:58 2016 +0100
+++ b/dav/optparser.c	Mon Jan 25 11:39:46 2016 +0100
@@ -147,10 +147,6 @@
                         }
                         break;
                     }
-                    case 'i': {
-                        ucx_map_cstr_put(a->options, "interactive", NOARG);
-                        break;
-                    }
                 }
             }
         } else if(option) {
--- a/libidav/resource.c	Mon Jan 25 10:48:58 2016 +0100
+++ b/libidav/resource.c	Mon Jan 25 11:39:46 2016 +0100
@@ -872,17 +872,13 @@
     }
 }
 
-static int dav_cp_mv(DavResource *res, char *newpath, _Bool copy, _Bool override) {
+static int dav_cp_mv_url(DavResource *res, char *desturl, _Bool copy, _Bool override) {
     DavSession *sn = res->session;
     CURL *handle = sn->handle;
     util_set_url(sn, dav_resource_get_href(res));
     
-    char *dest = dav_session_get_href(sn, newpath);
-    dav_session_free(sn, dest);
-    char *desturl = util_get_url(sn, dest);
+    CURLcode ret = do_copy_move_request(handle, desturl, copy, override);
     
-    CURLcode ret = do_copy_move_request(handle, desturl, copy, override);
-    free(desturl);
     int status = 0;
     curl_easy_getinfo (handle, CURLINFO_RESPONSE_CODE, &status);
     if(ret == CURLE_OK && (status >= 200 && status < 300)) {
@@ -893,6 +889,16 @@
     }
 }
 
+static int dav_cp_mv(DavResource *res, char *newpath, _Bool copy, _Bool override) {
+    char *dest = dav_session_get_href(res->session, newpath);
+    char *desturl = util_get_url(res->session, dest);
+    dav_session_free(res->session, dest);
+    
+    int ret = dav_cp_mv_url(res, desturl, copy, override);
+    free(desturl);
+    return ret;
+}
+
 int dav_copy(DavResource *res, char *newpath) {
     return dav_cp_mv(res, newpath, true, false);
 }
@@ -909,6 +915,14 @@
     return dav_cp_mv(res, newpath, false, override);
 }
 
+int dav_copyto(DavResource *res, char *url, DavBool override) {
+    return dav_cp_mv_url(res, url, true, override);
+}
+
+int dav_moveto(DavResource *res, char *url, DavBool override) {
+    return dav_cp_mv_url(res, url, false, override);
+}
+
 
 int resource_add_crypto_info(DavSession *sn, char *href, char *name, char *hash) {
     if(!DAV_IS_ENCRYPTED(sn)) {
--- a/libidav/utils.c	Mon Jan 25 10:48:58 2016 +0100
+++ b/libidav/utils.c	Mon Jan 25 11:39:46 2016 +0100
@@ -70,7 +70,7 @@
     return n;
 }
 
-static int parse_iso8601(char *iso8601str) {
+static time_t parse_iso8601(char *iso8601str) {
 
     // safety
     if(!iso8601str) {
--- a/libidav/webdav.h	Mon Jan 25 10:48:58 2016 +0100
+++ b/libidav/webdav.h	Mon Jan 25 11:39:46 2016 +0100
@@ -228,6 +228,8 @@
 int dav_move(DavResource *res, char *newpath);
 int dav_copy_o(DavResource *res, char *newpath, DavBool override);
 int dav_move_o(DavResource *res, char *newpath, DavBool override);
+int dav_copyto(DavResource *res, char *url, DavBool override);
+int dav_moveto(DavResource *res, char *url, DavBool override);
 
 char* dav_get_property(DavResource *res, char *name);
 char* dav_get_property_ns(DavResource *res, char *ns, char *name);

mercurial