added lock and unlock commands to dav

Tue, 15 Mar 2016 10:35:57 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 15 Mar 2016 10:35:57 +0100
changeset 209
d24761f1e07c
parent 208
1fb26aca5093
child 210
1d19e0ff18d9

added lock and unlock commands to dav

dav/main.c file | annotate | diff | comparison | revisions
dav/main.h file | annotate | diff | comparison | revisions
dav/optparser.c file | annotate | diff | comparison | revisions
--- a/dav/main.c	Mon Mar 14 17:18:33 2016 +0100
+++ b/dav/main.c	Tue Mar 15 10:35:57 2016 +0100
@@ -119,7 +119,11 @@
         ret = cmd_set_property(args);
     } else if(!strcasecmp(cmd, "get-property")) {
         ret = cmd_get_property(args);
-    } else if(!strcasecmp(cmd, "info")) {
+    } else if(!strcasecmp(cmd, "lock")) {
+        ret = cmd_lock(args);
+    } else if(!strcasecmp(cmd, "unlock")) {
+        ret = cmd_unlock(args);
+    }  else if(!strcasecmp(cmd, "info")) {
         ret = cmd_info(args);
     } else if(!strcasecmp(cmd, "add-repository")) {
         ret = cmd_add_repository(args);
@@ -151,17 +155,19 @@
     fprintf(
             stderr,
             "        get [-pcR] [-o <file>] [-u <date>] <url>\n");
-    fprintf(stderr, "        put [-pcR] [-k <key>] <url> <file>\n");
-    fprintf(stderr, "        mkdir [-pc] [-k <key>] <url>\n");
-    fprintf(stderr, "        remove [-pc] <url>\n");
-    fprintf(stderr, "        copy [-pcO] <url> <url>\n");
-    fprintf(stderr, "        move [-pcO] <url> <url>\n");
+    fprintf(stderr, "        put [-pcR] [-k <key>] [-L <lock>] <url> <file>\n");
+    fprintf(stderr, "        mkdir [-pc] [-k <key>] [-L <lock>] <url>\n");
+    fprintf(stderr, "        remove [-pc] [-L <lock>] <url>\n");
+    fprintf(stderr, "        copy [-pcO] [-L <lock>] <url> <url>\n");
+    fprintf(stderr, "        move [-pcO] [-L <lock>] <url> <url>\n");
     fprintf(
             stderr,
             "        get-property [-pc] [-n <uri>] <url> <property>\n");
     fprintf(
             stderr,
-            "        set-property [-pc] [-n <uri>] <url> <property> [value]\n");
+            "        set-property [-pc] [-L <lock>] [-n <uri>] <url> <property> [value]\n");
+    fprintf(stderr, "        lock [-pc] <url>\n");
+    fprintf(stderr, "        unlock [-pc] [-L <lock>] <url>\n");
     fprintf(stderr, "        info [-pc] <url>\n");
     fprintf(stderr, "        date [url]\n");
     fprintf(stderr, "\n");
@@ -182,6 +188,7 @@
     fprintf(stderr, "        -l         print resources in long list format\n");
     fprintf(stderr, "        -t         print content type\n");
     fprintf(stderr, "        -O         override resources\n");
+    fprintf(stderr, "        -L <lock>  specificy lock token\n");
     fprintf(stderr, "        -n <uri>   specify namespace uri\n");
     fprintf(stderr, "        -i         disable cert verification (all commands)\n");
     fprintf(stderr, "        -v         verbose output (all commands)\n");
@@ -1123,6 +1130,97 @@
     return 0;
 }
 
+int cmd_lock(CmdArgs *a) {
+    if(a->argc != 1) {
+        fprintf(stderr, "Too %s arguments\n", a->argc > 1 ? "many" : "few");
+        return -1;
+    }
+    
+    char *url = a->argv[0];
+    char *path = NULL;
+    DavSession *sn = connect_to_repo(url2repo(url, &path), a);
+    
+    if(set_session_config(sn, a)) {
+        return -1;
+    }
+    
+    DavResource *res = dav_resource_new(sn, path);
+    if(dav_lock(res)) {
+        print_resource_error(sn, res->path);
+        return -1;
+    }
+    
+    DavLock *lock = dav_get_lock(sn, res->path);
+    if(!lock) {
+        // this should really not happen
+        // do some damage control
+        dav_unlock(res);
+        fprintf(stderr, "Error: Cannot find lock token for %s\n", res->path);
+        return -1;
+    }
+    
+    printf("%s\n", lock->token);
+    
+    dav_session_destroy(sn);
+    return 0;
+}
+
+static char* read_line() {
+    UcxBuffer *buf = ucx_buffer_new(NULL, 128, UCX_BUFFER_AUTOEXTEND);
+    int c;
+    while((c = getchar()) != EOF) {
+        if(c == '\n') {
+            break;
+        }
+        ucx_buffer_putc(buf, c);
+    } 
+    char *str = NULL;
+    sstr_t line = sstrtrim(sstrn(buf->space, buf->size));
+    if(line.length != 0) {
+        str = sstrdup(line).ptr;
+    }
+    ucx_buffer_free(buf);
+    return str;
+}
+
+int cmd_unlock(CmdArgs *a) {
+    if(a->argc != 1) {
+        fprintf(stderr, "Too %s arguments\n", a->argc > 1 ? "many" : "few");
+        return -1;
+    }
+    
+    char *url = a->argv[0];
+    char *path = NULL;
+    DavSession *sn = connect_to_repo(url2repo(url, &path), a);
+    
+    if(set_session_config(sn, a)) {
+        return -1;
+    }
+    
+    char *locktoken = cmd_getoption(a, "lock");
+    if(locktoken) {
+        DavLock *lock = dav_create_lock(sn, locktoken, NULL);
+        dav_add_collection_lock(sn, "/", lock);
+    } else {
+        locktoken = read_line();
+        if(!locktoken) {
+            fprintf(stderr, "No lock token specified.\nAbort.\n");
+            return -1;
+        }
+        DavLock *lock = dav_create_lock(sn, locktoken, NULL);
+        dav_add_collection_lock(sn, "/", lock);
+    }
+    
+    DavResource *res = dav_resource_new(sn, path);
+    if(dav_unlock(res)) {
+        print_resource_error(sn, res->path);
+        return -1;
+    }
+    
+    dav_session_destroy(sn);
+    return 0;
+}
+
 static int count_children(DavResource *res) {
     DavResource *child = res->children;
     int count = 0;
--- a/dav/main.h	Mon Mar 14 17:18:33 2016 +0100
+++ b/dav/main.h	Tue Mar 15 10:35:57 2016 +0100
@@ -64,6 +64,9 @@
 int cmd_get_property(CmdArgs *args);
 int cmd_set_property(CmdArgs *args);
 
+int cmd_lock(CmdArgs *args);
+int cmd_unlock(CmdArgs *args);
+
 int cmd_info(CmdArgs *args);
 
 char* stdin2str();
--- a/dav/optparser.c	Mon Mar 14 17:18:33 2016 +0100
+++ b/dav/optparser.c	Tue Mar 15 10:35:57 2016 +0100
@@ -151,6 +151,20 @@
                         }
                         break;
                     }
+                    case 'L': {
+                        if(!option) {
+                            option = "lock";
+                            optchar = 'L';
+                        } else {
+                            fprintf(
+                                    stderr,
+                                    "Missing argument for option -%c\n",
+                                    optchar);
+                            cmd_args_free(a);
+                            return NULL;
+                        }
+                        break;
+                    }
                 }
             }
         } else if(option) {

mercurial