dav/main.c

changeset 62
3b813c57c106
parent 46
0542668d0f26
child 68
f6d3db6113d3
--- a/dav/main.c	Mon Sep 01 17:09:18 2014 +0200
+++ b/dav/main.c	Mon Nov 03 15:43:04 2014 +0100
@@ -35,6 +35,7 @@
 #include <libxml/xmlerror.h>
 #include <sys/types.h>
 #include <ucx/string.h>
+#include <ucx/utils.h>
 #include <dirent.h>
 
 
@@ -96,6 +97,10 @@
         ret = cmd_mkdir(args);
     } else if(!strcasecmp(cmd, "date")) {
         ret = cmd_date(args);
+    } else if(!strcasecmp(cmd, "set-property")) {
+        ret = cmd_set_property(args);
+    } else if(!strcasecmp(cmd, "get-property")) {
+        ret = cmd_get_property(args);
     } else {
         print_usage(argv[0]);
     }
@@ -114,6 +119,12 @@
     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,
+            "        get-property [-pc] [-n <uri>] <url> <property>\n");
+    fprintf(
+            stderr,
+            "        set-property [-pc] [-n <uri>] <url> <property> [value]\n");
     fprintf(stderr, "        date [url]\n");
     fprintf(stderr, "\n");
     fprintf(stderr, "Options:\n");
@@ -132,6 +143,7 @@
     fprintf(stderr, "        -a         show all files\n");
     fprintf(stderr, "        -l         print resources in long list format\n");
     fprintf(stderr, "        -t         print content type\n");
+    fprintf(stderr, "        -n <uri>   specify namespace uri");
     fprintf(stderr, "\n");
     fprintf(stderr,
             "Instead of an url you can pass a repository name "
@@ -289,7 +301,7 @@
     }
     
     char *update = cmd_getoption(a, "update");
-    time_t t = 0;
+    time_t t = -1;
     if(update) {
         t = util_parse_lastmodified(update);
     }
@@ -750,7 +762,6 @@
         if(sn->errorstr) {
             fprintf(stderr, "%s\n", sn->errorstr);
         }
-        fclose(in);
         return -1;
     }
     return 0;
@@ -884,11 +895,105 @@
     return 0;
 }
 
-int cmd_sync(CmdArgs *a) {
+int cmd_get_property(CmdArgs *a) {
     if(a->argc < 2) {
         fprintf(stderr, "Too few arguments\n");
         return -1;
     }
     
+    char *url = a->argv[0];
+    char *path = NULL;
+    Repository *repo = url2repo(url, &path);
+    DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
+    dav_session_set_flags(sn, get_repository_flags(repo));
+    sn->key = dav_context_get_key(ctx, repo->default_key);
+    
+    if(set_session_config(sn, a)) {
+        return -1;
+    }
+    
+    char *namespace = cmd_getoption(a, "namespace");
+    char *property = a->argv[1];
+    
+    DavResource *res = dav_resource_new(sn, path);
+    if(dav_load(res)) {
+        print_resource_error(sn, res->path);
+        return -1;
+    }
+    
+    char *value = namespace ?
+            dav_get_property_ns(res, namespace, property) : 
+            dav_get_property(res, property);
+    if(!value) {
+        fprintf(stderr, "Error: no property value.\n");
+        return -1;
+    }
+    
+    int ret = 0;
+    if(value) {
+        printf("%s\n", value);
+    } else {
+        // TODO: correct error message
+        fprintf(stderr, "Error: property not found.\n");
+        ret = -1;
+    }
+    
+    return ret;
+}
+
+int cmd_set_property(CmdArgs *a) {
+    if(a->argc < 2) {
+        fprintf(stderr, "Too few arguments\n");
+        return -1;
+    }
+    
+    char *url = a->argv[0];
+    char *path = NULL;
+    Repository *repo = url2repo(url, &path);
+    DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
+    dav_session_set_flags(sn, get_repository_flags(repo));
+    sn->key = dav_context_get_key(ctx, repo->default_key);
+    
+    if(set_session_config(sn, a)) {
+        return -1;
+    }
+    
+    DavResource *res = dav_resource_new(sn, path);
+    if(!dav_exists(res)) {
+        print_resource_error(sn, res->path);
+        return -1;
+    }
+    
+    char *namespace = cmd_getoption(a, "namespace");
+    char *property = a->argv[1];
+    char *value = a->argc > 2 ? a->argv[2] : stdin2str();
+    
+    if(namespace) {
+        dav_set_property_ns(res, namespace, property, value);
+    } else {
+        dav_set_property(res, property, value);
+    }
+    
+    if(dav_store(res)) {
+        print_resource_error(sn, res->path);
+        fprintf(stderr, "Cannot set property.\n");
+        return -1;
+    }
+    
     return 0;
 }
+
+char* stdin2str() {
+    UcxBuffer *buf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
+    size_t size = ucx_stream_hcopy(stdin, buf, fread, ucx_buffer_write);
+    if(size == 0) {
+        ucx_buffer_free(buf);
+        return NULL;
+    } else {
+        ucx_buffer_putc(buf, '\0');
+        char *str = buf->space;
+        free(buf);
+        return str;
+    }
+}
+

mercurial