dav/main.c

changeset 62
3b813c57c106
parent 46
0542668d0f26
child 68
f6d3db6113d3
equal deleted inserted replaced
60:cdca2eef48ee 62:3b813c57c106
33 #include <unistd.h> 33 #include <unistd.h>
34 #include <time.h> 34 #include <time.h>
35 #include <libxml/xmlerror.h> 35 #include <libxml/xmlerror.h>
36 #include <sys/types.h> 36 #include <sys/types.h>
37 #include <ucx/string.h> 37 #include <ucx/string.h>
38 #include <ucx/utils.h>
38 #include <dirent.h> 39 #include <dirent.h>
39 40
40 41
41 #include <libidav/utils.h> 42 #include <libidav/utils.h>
42 #include <libidav/crypto.h> 43 #include <libidav/crypto.h>
94 ret = cmd_remove(args); 95 ret = cmd_remove(args);
95 } else if(!strcasecmp(cmd, "mkdir") || !strcasecmp(cmd, "mkcol")) { 96 } else if(!strcasecmp(cmd, "mkdir") || !strcasecmp(cmd, "mkcol")) {
96 ret = cmd_mkdir(args); 97 ret = cmd_mkdir(args);
97 } else if(!strcasecmp(cmd, "date")) { 98 } else if(!strcasecmp(cmd, "date")) {
98 ret = cmd_date(args); 99 ret = cmd_date(args);
100 } else if(!strcasecmp(cmd, "set-property")) {
101 ret = cmd_set_property(args);
102 } else if(!strcasecmp(cmd, "get-property")) {
103 ret = cmd_get_property(args);
99 } else { 104 } else {
100 print_usage(argv[0]); 105 print_usage(argv[0]);
101 } 106 }
102 dav_context_destroy(ctx); 107 dav_context_destroy(ctx);
103 108
112 stderr, 117 stderr,
113 " get [-pcR] [-o <file>] [-u <date>] <url>\n"); 118 " get [-pcR] [-o <file>] [-u <date>] <url>\n");
114 fprintf(stderr, " put [-pcR] [-k <key>] <url> <file>\n"); 119 fprintf(stderr, " put [-pcR] [-k <key>] <url> <file>\n");
115 fprintf(stderr, " mkdir [-pc] [-k <key>] <url>\n"); 120 fprintf(stderr, " mkdir [-pc] [-k <key>] <url>\n");
116 fprintf(stderr, " remove [-pc] <url>\n"); 121 fprintf(stderr, " remove [-pc] <url>\n");
122 fprintf(
123 stderr,
124 " get-property [-pc] [-n <uri>] <url> <property>\n");
125 fprintf(
126 stderr,
127 " set-property [-pc] [-n <uri>] <url> <property> [value]\n");
117 fprintf(stderr, " date [url]\n"); 128 fprintf(stderr, " date [url]\n");
118 fprintf(stderr, "\n"); 129 fprintf(stderr, "\n");
119 fprintf(stderr, "Options:\n"); 130 fprintf(stderr, "Options:\n");
120 fprintf(stderr, 131 fprintf(stderr,
121 " -k <key> Key to use for encryption or decryption\n"); 132 " -k <key> Key to use for encryption or decryption\n");
130 " -u <date> " 141 " -u <date> "
131 "Get resources which are modified since the specified date\n"); 142 "Get resources which are modified since the specified date\n");
132 fprintf(stderr, " -a show all files\n"); 143 fprintf(stderr, " -a show all files\n");
133 fprintf(stderr, " -l print resources in long list format\n"); 144 fprintf(stderr, " -l print resources in long list format\n");
134 fprintf(stderr, " -t print content type\n"); 145 fprintf(stderr, " -t print content type\n");
146 fprintf(stderr, " -n <uri> specify namespace uri");
135 fprintf(stderr, "\n"); 147 fprintf(stderr, "\n");
136 fprintf(stderr, 148 fprintf(stderr,
137 "Instead of an url you can pass a repository name " 149 "Instead of an url you can pass a repository name "
138 "with an optional path:\n"); 150 "with an optional path:\n");
139 fprintf(stderr, " <repository>/path/\n"); 151 fprintf(stderr, " <repository>/path/\n");
287 if(set_session_config(sn, a)) { 299 if(set_session_config(sn, a)) {
288 return -1; 300 return -1;
289 } 301 }
290 302
291 char *update = cmd_getoption(a, "update"); 303 char *update = cmd_getoption(a, "update");
292 time_t t = 0; 304 time_t t = -1;
293 if(update) { 305 if(update) {
294 t = util_parse_lastmodified(update); 306 t = util_parse_lastmodified(update);
295 } 307 }
296 308
297 int depth = cmd_getoption(a, "recursive") ? -1 : 1; 309 int depth = cmd_getoption(a, "recursive") ? -1 : 1;
748 print_resource_error(sn, res->path); 760 print_resource_error(sn, res->path);
749 fprintf(stderr, "Cannot upload file.\n"); 761 fprintf(stderr, "Cannot upload file.\n");
750 if(sn->errorstr) { 762 if(sn->errorstr) {
751 fprintf(stderr, "%s\n", sn->errorstr); 763 fprintf(stderr, "%s\n", sn->errorstr);
752 } 764 }
753 fclose(in);
754 return -1; 765 return -1;
755 } 766 }
756 return 0; 767 return 0;
757 } 768 }
758 769
882 return -1; 893 return -1;
883 } 894 }
884 return 0; 895 return 0;
885 } 896 }
886 897
887 int cmd_sync(CmdArgs *a) { 898 int cmd_get_property(CmdArgs *a) {
888 if(a->argc < 2) { 899 if(a->argc < 2) {
889 fprintf(stderr, "Too few arguments\n"); 900 fprintf(stderr, "Too few arguments\n");
890 return -1; 901 return -1;
891 } 902 }
892 903
904 char *url = a->argv[0];
905 char *path = NULL;
906 Repository *repo = url2repo(url, &path);
907 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
908 dav_session_set_flags(sn, get_repository_flags(repo));
909 sn->key = dav_context_get_key(ctx, repo->default_key);
910
911 if(set_session_config(sn, a)) {
912 return -1;
913 }
914
915 char *namespace = cmd_getoption(a, "namespace");
916 char *property = a->argv[1];
917
918 DavResource *res = dav_resource_new(sn, path);
919 if(dav_load(res)) {
920 print_resource_error(sn, res->path);
921 return -1;
922 }
923
924 char *value = namespace ?
925 dav_get_property_ns(res, namespace, property) :
926 dav_get_property(res, property);
927 if(!value) {
928 fprintf(stderr, "Error: no property value.\n");
929 return -1;
930 }
931
932 int ret = 0;
933 if(value) {
934 printf("%s\n", value);
935 } else {
936 // TODO: correct error message
937 fprintf(stderr, "Error: property not found.\n");
938 ret = -1;
939 }
940
941 return ret;
942 }
943
944 int cmd_set_property(CmdArgs *a) {
945 if(a->argc < 2) {
946 fprintf(stderr, "Too few arguments\n");
947 return -1;
948 }
949
950 char *url = a->argv[0];
951 char *path = NULL;
952 Repository *repo = url2repo(url, &path);
953 DavSession *sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
954 dav_session_set_flags(sn, get_repository_flags(repo));
955 sn->key = dav_context_get_key(ctx, repo->default_key);
956
957 if(set_session_config(sn, a)) {
958 return -1;
959 }
960
961 DavResource *res = dav_resource_new(sn, path);
962 if(!dav_exists(res)) {
963 print_resource_error(sn, res->path);
964 return -1;
965 }
966
967 char *namespace = cmd_getoption(a, "namespace");
968 char *property = a->argv[1];
969 char *value = a->argc > 2 ? a->argv[2] : stdin2str();
970
971 if(namespace) {
972 dav_set_property_ns(res, namespace, property, value);
973 } else {
974 dav_set_property(res, property, value);
975 }
976
977 if(dav_store(res)) {
978 print_resource_error(sn, res->path);
979 fprintf(stderr, "Cannot set property.\n");
980 return -1;
981 }
982
893 return 0; 983 return 0;
894 } 984 }
985
986 char* stdin2str() {
987 UcxBuffer *buf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
988 size_t size = ucx_stream_hcopy(stdin, buf, fread, ucx_buffer_write);
989 if(size == 0) {
990 ucx_buffer_free(buf);
991 return NULL;
992 } else {
993 ucx_buffer_putc(buf, '\0');
994 char *str = buf->space;
995 free(buf);
996 return str;
997 }
998 }
999

mercurial