dav/main.c

changeset 476
b4e3453e2b49
parent 475
52e4171d42ce
child 478
baa63fef5c5c
--- a/dav/main.c	Sun Sep 23 12:51:41 2018 +0200
+++ b/dav/main.c	Sun Sep 23 13:07:35 2018 +0200
@@ -207,7 +207,7 @@
 
 static char *cmdusageinfo[] = {
     "list [-altdepcR] [-u <date>] <url>",
-    "get [-pcRK] [-o <file>] [-u <date>] <url>",
+    "get [-pcRK] [-o <file>] [-u <date>] [-V <version>] <url>",
     "put [-pcR] [-k <key>] [-L <lock>] <url> <file>",
     "mkdir [-pc] [-k <key>] [-L <lock>] <url>",
     "remove [-pc] [-L <lock>] <url>",
@@ -215,12 +215,12 @@
     "move [-pcO] [-L <lock>] <url> <url>",
     "export [-pc] [-o <file>] [-u <date>] <url>",
     "import [-pc] [-k <key>] [-L <lock>] <url> <file>",
-    "get-property [-pcx] [-n <uri>] <url> <property>",
+    "get-property [-pcx] [-V <version>] [-n <uri>] <url> <property>",
     "set-property [-pcx] [-L <lock>] [-n <uri>] <url> <property> [value]",
     "remove-property [-pc] [-n <uri>] <url> <property>",
     "lock [-pc] [-T timeout] <url>",
     "unlock [-pc] [-L <lock>] <url>",
-    "info [-pc] <url>",
+    "info [-pc] [-V <version>] <url>",
     "date [url]",
     NULL
 };
@@ -910,6 +910,13 @@
     }
     
     int recursive = cmd_getoption(a, "recursive") ? 1 : 0;
+    char *version = cmd_getoption(a, "version");
+    
+    if(recursive && version) {
+        fprintf(stderr, "-V option can only be used without -R option\n");
+        return -1;
+    }
+    
     DavResource *res;
     
     int depth = recursive ? -1 : 1;
@@ -929,6 +936,16 @@
         return -1;
     }
     
+    if(version) {
+        DavResource *vres = find_version(res, version);
+        if(!vres) {
+            fprintf(stderr, "Cannot find version '%s' for resource.\n", version);
+            return -1;
+        }
+        dav_resource_free_all(res);
+        res = vres;
+    }
+    
     /*
      * determine the output file
      * use stdout if the output file is -
@@ -1616,6 +1633,8 @@
     char *namespace = cmd_getoption(a, "namespace");
     char *property = a->argv[1];
     
+    char *version = cmd_getoption(a, "version");
+    
     DavPropName propname;
     if(namespace) {
         propname.ns = namespace;
@@ -1629,6 +1648,16 @@
     }
     
     DavResource *res = dav_resource_new(sn, path);
+    if(version) {
+        DavResource *vres = find_version(res, version);
+        if(!vres) {
+            fprintf(stderr, "Cannot find version '%s' for resource.\n", version);
+            return -1;
+        }
+        dav_resource_free_all(res);
+        res = vres;
+    }
+    
     if(dav_load_prop(res, &propname, 1)) {
         print_resource_error(sn, res->path);
         return -1;
@@ -1905,7 +1934,19 @@
         return -1;
     }
     
+    char *version = cmd_getoption(a, "version");
+    
     DavResource *res = dav_resource_new(sn, path);
+    if(version) {
+        DavResource *vres = find_version(res, version);
+        if(!vres) {
+            fprintf(stderr, "Cannot find version '%s' for resource.\n", version);
+            return -1;
+        }
+        dav_resource_free_all(res);
+        res = vres;
+    }
+    
     if(!dav_load(res)) {
         printf("name: %s\n", res->name);
         printf("path: %s\n", res->path);
@@ -2128,6 +2169,25 @@
     return ret;
 }
 
+DavResource* find_version(DavResource *res, char *version) {
+    DavResource *list = dav_versiontree(res, NULL);
+    DavResource *ret = NULL;
+    while(list) {
+        DavResource *next = list->next;
+        if(!ret) {
+            char *vname = dav_get_string_property(list, "D:version-name");
+            if(vname && !strcmp(vname, version)) {
+                ret = list;
+            }
+        }
+        if(list != ret) {
+            dav_resource_free(list);
+        }
+        list = next;
+    }
+    return ret;
+}
+
 
 char* stdin2str() {
     UcxBuffer *buf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);

mercurial