# HG changeset patch # User Olaf Wintermann # Date 1415025784 -3600 # Node ID 3b813c57c106196287fee52651733e19cee5bcfa # Parent cdca2eef48ee54eba27869a8af1cf2762fb3ec42 added property-set and property-get command diff -r cdca2eef48ee -r 3b813c57c106 dav/main.c --- 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 #include #include +#include #include @@ -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 ] \n"); fprintf(stderr, " mkdir [-pc] [-k ] \n"); fprintf(stderr, " remove [-pc] \n"); + fprintf( + stderr, + " get-property [-pc] [-n ] \n"); + fprintf( + stderr, + " set-property [-pc] [-n ] [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 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; + } +} + diff -r cdca2eef48ee -r 3b813c57c106 dav/main.h --- a/dav/main.h Mon Sep 01 17:09:18 2014 +0200 +++ b/dav/main.h Mon Nov 03 15:43:04 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 } diff -r cdca2eef48ee -r 3b813c57c106 dav/optparser.c --- a/dav/optparser.c Mon Sep 01 17:09:18 2014 +0200 +++ b/dav/optparser.c Mon Nov 03 15:43:04 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) { diff -r cdca2eef48ee -r 3b813c57c106 libidav/methods.c --- a/libidav/methods.c Mon Sep 01 17:09:18 2014 +0200 +++ b/libidav/methods.c Mon Nov 03 15:43:04 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("\n\n"); + s = S("\n\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("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("\n\n"); + s = S("\n\n"); ucx_buffer_write(s.ptr, 1, s.length, buf); }