added info command

Fri, 03 Apr 2015 14:53:28 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Fri, 03 Apr 2015 14:53:28 +0200
changeset 84
7fca3788261d
parent 83
7d20ce5d235b
child 85
0ab1cf261a44

added info command

dav/main.c file | annotate | diff | comparison | revisions
dav/main.h file | annotate | diff | comparison | revisions
libidav/resource.c file | annotate | diff | comparison | revisions
libidav/webdav.h file | annotate | diff | comparison | revisions
--- a/dav/main.c	Wed Apr 01 11:42:38 2015 +0200
+++ b/dav/main.c	Fri Apr 03 14:53:28 2015 +0200
@@ -101,6 +101,8 @@
         ret = cmd_set_property(args);
     } else if(!strcasecmp(cmd, "get-property")) {
         ret = cmd_get_property(args);
+    } else if(!strcasecmp(cmd, "info")) {
+        ret = cmd_info(args);
     } else {
         print_usage(argv[0]);
     }
@@ -992,6 +994,73 @@
     return 0;
 }
 
+static int count_children(DavResource *res) {
+    DavResource *child = res->children;
+    int count = 0;
+    while(child) {
+        count++;
+        child = child->next;
+    }
+    return count;
+}
+
+int cmd_info(CmdArgs *a) {
+    if(a->argc < 1) {
+        fprintf(stderr, "Too few arguments\n");
+        return -1;
+    }
+    
+    char *url = a->argv[0];
+    char *path = NULL;
+    DavSession *sn = connect_to_repo(url2repo(url, &path));
+    
+    if(set_session_config(sn, a)) {
+        return -1;
+    }
+    
+    DavResource *res = dav_resource_new(sn, path);
+    if(!dav_load(res)) {
+        printf("name: %s\n", res->name);
+        printf("path: %s\n", res->path);
+        
+        char *url = util_path_to_url(sn, res->path);
+        printf("url:  %s\n", url);
+        free(url);
+        
+        if(res->iscollection) {
+            printf("type: collection\n");
+            printf("size: %d\n", count_children(res));
+        } else {
+            printf("type: resource\n");
+            char *len = ls_size_str(res);
+            printf("size: %s\n", len);
+            free(len);
+        }
+        
+        size_t count = 0;
+        DavPropName *properties = dav_get_property_names(res, &count);
+        
+        char *last_ns = NULL;
+        for(int i=0;i<count;i++) {
+            DavPropName p = properties[i];
+            if(!last_ns || strcmp(last_ns, p.ns)) {
+                printf("\nnamespace: %s\n", p.ns);
+                last_ns = p.ns;
+            }
+            
+            sstr_t value = sstr(dav_get_property_ns(res, p.ns, p.name));
+            value = sstrtrim(value);
+            printf("  %s: %.*s\n", p.name, (int)value.length, value.ptr);
+        }
+        
+        dav_session_free(sn, properties);
+    } else {
+        print_resource_error(sn, res->path);
+        return -1;
+    }
+}
+
+
 char* stdin2str() {
     UcxBuffer *buf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
     size_t size = ucx_stream_hcopy(stdin, buf, fread, ucx_buffer_write);
--- a/dav/main.h	Wed Apr 01 11:42:38 2015 +0200
+++ b/dav/main.h	Fri Apr 03 14:53:28 2015 +0200
@@ -56,8 +56,10 @@
 int cmd_date(CmdArgs *args);
 
 int cmd_get_property(CmdArgs *args);
+int cmd_set_property(CmdArgs *args);
 
-int cmd_set_property(CmdArgs *args);
+int cmd_info(CmdArgs *args);
+
 char* stdin2str();
 
 #ifdef	__cplusplus
--- a/libidav/resource.c	Wed Apr 01 11:42:38 2015 +0200
+++ b/libidav/resource.c	Fri Apr 03 14:53:28 2015 +0200
@@ -245,7 +245,7 @@
     sstr_t name_str = sstr(name);
     
     sstr_t key;
-    key = sstrcat(3, ns_str, S(" "), name_str);
+    key = sstrcat(4, ns_str, S("\0"), name_str, S("\0"));
     
     return ucx_key(key.ptr, key.length);
 }
@@ -356,6 +356,49 @@
     data->remove = ucx_list_append_a(a, data->remove, property);
 }
 
+static int compare_propname(const void *a, const void *b) {
+    const DavPropName *p1 = a;
+    const DavPropName *p2 = b;
+    
+    int result = strcmp(p1->ns, p2->ns);
+    if(result) {
+        return result;
+    } else {
+        return strcmp(p1->name, p2->name);
+    }
+}
+
+DavPropName* dav_get_property_names(DavResource *res, size_t *count) {
+    DavResourceData *data = res->data;
+    
+    *count = data->properties->count;
+    DavPropName *names = dav_session_calloc(
+            res->session,
+            *count,
+            sizeof(DavPropName));
+    
+    
+    UcxMapIterator i = ucx_map_iterator(data->properties);
+    void *value;
+    int j = 0;
+    UCX_MAP_FOREACH(key, value, i) {
+        DavPropName *name = &names[j];
+        // the map key is namespace + '\0' + name
+        name->ns = key.data;
+        for(int k=0;j<key.len;k++) {
+            if(((char*)key.data)[k] == '\0') {
+                name->name = key.data + k + 1;
+                break;
+            }
+        }
+        j++;
+    }
+    
+    qsort(names, *count, sizeof(DavPropName), compare_propname);
+    
+    return names;
+}
+
 
 void dav_set_content(DavResource *res, void *stream, dav_read_func read_func) {
     DavResourceData *data = res->data;
--- a/libidav/webdav.h	Wed Apr 01 11:42:38 2015 +0200
+++ b/libidav/webdav.h	Fri Apr 03 14:53:28 2015 +0200
@@ -48,6 +48,7 @@
 typedef struct DavRequest    DavRequest;
 typedef struct DavNamespace  DavNamespace;
 typedef struct DavProperty   DavProperty;
+typedef struct DavPropName   DavPropName;
 typedef struct DavKey        DavKey;
 
 #include "davql.h"
@@ -133,6 +134,11 @@
     char         *value;
 };
 
+struct DavPropName {
+    char *ns;
+    char *name;
+};
+
 #define DAV_KEY_AES128     0
 #define DAV_KEY_AES256     1
 #define DAV_KEY_PASSWORD   2
@@ -209,6 +215,8 @@
 void dav_remove_property(DavResource *res, char *name);
 void dav_remove_property_ns(DavResource *res, char *ns, char *name);
 
+DavPropName* dav_get_property_names(DavResource *res, size_t *count);
+
 void dav_set_content(DavResource *res, void *stream, dav_read_func read_func);
 void dav_set_content_data(DavResource *res, char *content, size_t length);
 

mercurial