merge

Mon, 03 Nov 2014 15:44:17 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 03 Nov 2014 15:44:17 +0100
changeset 63
ee9da935645d
parent 62
3b813c57c106 (diff)
parent 61
e9b102d5a6f6 (current diff)
child 64
112dbf7ba8b0

merge

--- a/dav/main.c	Sat Oct 11 19:05:11 2014 +0200
+++ b/dav/main.c	Mon Nov 03 15:44:17 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;
+    }
+}
+
--- a/dav/main.h	Sat Oct 11 19:05:11 2014 +0200
+++ b/dav/main.h	Mon Nov 03 15:44:17 2014 +0100
@@ -55,7 +55,10 @@
 
 int cmd_date(CmdArgs *args);
 
-int cmd_sync(CmdArgs *args);
+int cmd_get_property(CmdArgs *args);
+
+int cmd_set_property(CmdArgs *args);
+char* stdin2str();
 
 #ifdef	__cplusplus
 }
--- a/dav/optparser.c	Sat Oct 11 19:05:11 2014 +0200
+++ b/dav/optparser.c	Mon Nov 03 15:44:17 2014 +0100
@@ -122,6 +122,20 @@
                         }
                         break;
                     }
+                    case 'n': {
+                        if(!option) {
+                            option = "namespace";
+                            optchar = 'n';
+                        } else {
+                            fprintf(
+                                    stderr,
+                                    "Missing argument for option -%c\n",
+                                    optchar);
+                            cmd_args_free(a);
+                            return NULL;
+                        }
+                        break;
+                    }
                 }
             }
         } else if(option) {
--- a/libidav/methods.c	Sat Oct 11 19:05:11 2014 +0200
+++ b/libidav/methods.c	Mon Nov 03 15:44:17 2014 +0100
@@ -232,6 +232,7 @@
         root = dav_resource_new_href(sn, util_url_path(url)); // TODO: remove
     }
     
+    //printf("%.*s\n\n", response->size, response->space);
     xmlDoc *doc = xmlReadMemory(response->space, response->size, url, NULL, 0);
     if(!doc) {
         // TODO: free stuff
@@ -529,9 +530,9 @@
         ucx_buffer_write(s.ptr, 1, s.length, buf);
     }
     if(data->remove) {
-        s = S("<D:set>\n<D:prop>\n");
+        s = S("<D:remove>\n<D:prop>\n");
         ucx_buffer_write(s.ptr, 1, s.length, buf);
-        UCX_FOREACH(elm, data->set) {
+        UCX_FOREACH(elm, data->remove) {
             DavProperty *property = elm->data;
             char *prefix = ucx_map_cstr_get(namespaces, property->ns->name);
             
@@ -543,22 +544,10 @@
             ucx_buffer_write(s.ptr, 1, s.length, buf);
             s = sstr(property->name);
             ucx_buffer_write(s.ptr, 1, s.length, buf);
-            s = S(">");
-            ucx_buffer_write(s.ptr, 1, s.length, buf);
-            s = sstr(property->value);
-            ucx_buffer_write(s.ptr, 1, s.length, buf);
-            s = S("</");
-            ucx_buffer_write(s.ptr, 1, s.length, buf);
-            s = sstr(prefix);
-            ucx_buffer_write(s.ptr, 1, s.length, buf);
-            s = S(":");
-            ucx_buffer_write(s.ptr, 1, s.length, buf);
-            s = sstr(property->name);
-            ucx_buffer_write(s.ptr, 1, s.length, buf);
-            s = S(">\n");
+            s = S(" />\n");
             ucx_buffer_write(s.ptr, 1, s.length, buf);
         }
-        s = S("</D:prop>\n</D:set>\n");
+        s = S("</D:prop>\n</D:remove>\n");
         ucx_buffer_write(s.ptr, 1, s.length, buf);
     }
     

mercurial