dav/main.c

changeset 38
b855f76e965b
parent 36
c8755c87ce7f
child 40
a95ee94b9204
equal deleted inserted replaced
37:1c81083a3e46 38:b855f76e965b
76 if(!strcasecmp(cmd, "list") || !strcasecmp(cmd, "ls")) { 76 if(!strcasecmp(cmd, "list") || !strcasecmp(cmd, "ls")) {
77 ret = cmd_list(args); 77 ret = cmd_list(args);
78 } else if(!strcasecmp(cmd, "get")) { 78 } else if(!strcasecmp(cmd, "get")) {
79 ret = cmd_get(args); 79 ret = cmd_get(args);
80 } else if(!strcasecmp(cmd, "put")) { 80 } else if(!strcasecmp(cmd, "put")) {
81 printf("put\n");
82 ret = cmd_put(args); 81 ret = cmd_put(args);
83 } else if( 82 } else if(
84 !strcasecmp(cmd, "remove") || 83 !strcasecmp(cmd, "remove") ||
85 !strcasecmp(cmd, "rm") || 84 !strcasecmp(cmd, "rm") ||
86 !strcasecmp(cmd, "delete")) 85 !strcasecmp(cmd, "delete"))
131 "with an optional path:\n"); 130 "with an optional path:\n");
132 fprintf(stderr, " <repository>/path/\n"); 131 fprintf(stderr, " <repository>/path/\n");
133 fprintf(stderr, "\n"); 132 fprintf(stderr, "\n");
134 } 133 }
135 134
136 void url_get_parts(char *url, char **root, char **path) { 135 int request_auth(Repository *repo, DavSession *sn) {
136 static int login = 0;
137 if(login) {
138 return 0;
139 }
140
141 char *user = NULL;
142 char *password = NULL;
143 char ubuf[256];
144 char pbuf[256];
145 if(repo->user) {
146 user = repo->user;
147 } else {
148 printf("user: ");
149 fflush(stdout);
150 user = fgets(ubuf, 256, stdin);
151 }
152
153 printf("password: ");
154 fflush(stdout);
155 password = fgets(pbuf, 256, stdin);
156
157 size_t ulen = strlen(user);
158 if(user[ulen-1] == '\n') {
159 user[ulen-1] = '\0';
160 }
161 size_t plen = strlen(password);
162 if(password[plen-1] == '\n') {
163 password[plen-1] = '\0';
164 }
165
166 dav_session_set_auth(sn, user, password);
167 login = 1;
168 return 1;
169 }
170
171 Repository* url2repo(char *url, char **path) {
137 size_t ulen = strlen(url); 172 size_t ulen = strlen(url);
138 *root = NULL;
139 *path = NULL; 173 *path = NULL;
140 174
141 int s; 175 int s;
142 if(ulen > 7 && !strncasecmp(url, "http://", 7)) { 176 if(ulen > 7 && !strncasecmp(url, "http://", 7)) {
143 s = 7; 177 s = 7;
145 s = 8; 179 s = 8;
146 } else { 180 } else {
147 s = 1; 181 s = 1;
148 } 182 }
149 183
184 sstr_t r = sstr(url);
185 sstr_t p = sstr("/");
150 for(int i=s;i<ulen;i++) { 186 for(int i=s;i<ulen;i++) {
151 char c = url[i]; 187 char c = url[i];
152 if(c == '/') { 188 if(c == '/') {
153 sstr_t r = sstrn(url, i); 189 r = sstrn(url, i);
154 sstr_t p = sstrsubs(sstr(url), i); 190 p = sstrsubs(sstr(url), i);
155 if(p.length == 0) { 191 if(p.length == 0) {
156 p = sstrn("/", 1); 192 p = sstrn("/", 1);
157 } 193 }
158 *root = sstrdup(r).ptr; 194 break;
159 *path = sstrdup(p).ptr; 195 }
160 return; 196 }
161 } 197
162 } 198 Repository *repo = get_repository(r);
163 199 if(repo) {
164 *root = strdup(url); 200 *path = sstrdup(p).ptr;
165 *path = strdup("/"); 201 } else {
202 repo = calloc(1, sizeof(Repository));
203 repo->name = "";
204 repo->url = strdup(url);
205 repo->store_key_property = true;
206 repo->decrypt = true;
207 *path = strdup("/");
208 }
209
210 return repo;
166 } 211 }
167 212
168 void print_resource_error(DavSession *sn, char *path) { 213 void print_resource_error(DavSession *sn, char *path) {
169 char *res_url = util_concat_path(sn->base_url, path); 214 char *res_url = util_concat_path(sn->base_url, path);
170 switch(sn->error) { 215 switch(sn->error) {
202 if(a->argc != 1) { 247 if(a->argc != 1) {
203 fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many"); 248 fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many");
204 return -1; 249 return -1;
205 } 250 }
206 251
207 DavSession *sn = NULL;
208 char *url = a->argv[0]; 252 char *url = a->argv[0];
209 char *root = NULL;
210 char *path = NULL; 253 char *path = NULL;
211 char *base = NULL; 254 char *base = NULL;
212 url_get_parts(url, &root, &path); 255 DavSession *sn = NULL;
213 256 Repository *repo = url2repo(url, &path);
214 Repository *repo = get_repository(root); 257 base = util_concat_path(repo->url, path);
215 if(repo) { 258 sn = dav_session_new_auth(ctx, base, repo->user, repo->password);
216 base = util_concat_path(repo->url, path);
217 sn = dav_session_new_auth(ctx, base, repo->user, repo->password);
218 } else {
219 base = util_concat_path(root, path);
220 sn = dav_session_new(ctx, base);
221 }
222 259
223 char *update = cmd_getoption(a, "update"); 260 char *update = cmd_getoption(a, "update");
224 time_t t = 0; 261 time_t t = 0;
225 if(update) { 262 if(update) {
226 t = util_parse_lastmodified(update); 263 t = util_parse_lastmodified(update);
227 } 264 }
228 265
266 int ret = -1;
229 DavResource *ls; 267 DavResource *ls;
230 if(cmd_getoption(a, "recursive")) { 268 while(ret != 0) {
231 printf("base: %s\n", base); 269 if(cmd_getoption(a, "recursive")) {
232 if(update) { 270 printf("base: %s\n", base);
233 ls = dav_query( 271 if(update) {
234 sn, 272 ls = dav_query(
235 "get U:crypto-key from /* where lastmodified > %t", 273 sn,
236 t); 274 "get U:crypto-key from /* where lastmodified > %t",
237 } else { 275 t);
238 ls = dav_query(sn, "get U:crypto-key from /*"); 276 } else {
239 } 277 ls = dav_query(sn, "get U:crypto-key from /*");
240 } else { 278 }
241 if(update) { 279 } else {
242 ls = dav_query( 280 if(update) {
243 sn, 281 ls = dav_query(
244 "get U:crypto-key from / where lastmodified > %t", t); 282 sn,
245 } else { 283 "get U:crypto-key from / where lastmodified > %t", t);
246 ls = dav_query(sn, "get U:crypto-key from /"); 284 } else {
247 } 285 ls = dav_query(sn, "get U:crypto-key from /");
248 } 286 }
249 if(!ls) { 287 }
250 print_resource_error(sn, path); 288
251 free(root); 289 if(!ls) {
252 free(path); 290 if(sn->error == DAV_UNAUTHORIZED) {
253 free(base); 291 if(request_auth(repo, sn)) {
254 return -1; 292 continue;
255 } 293 }
256 294 }
257 // parameters 295 print_resource_error(sn, path);
258 int show_all = cmd_getoption(a, "all") ? 1 : 0; 296 break;
259 void (*print_func)(DavResource*, CmdArgs *); 297 }
260 if(cmd_getoption(a, "list")) { 298
261 print_func = ls_print_list_elm; 299 // parameters
262 } else { 300 int show_all = cmd_getoption(a, "all") ? 1 : 0;
263 print_func = ls_print_elm; 301 void (*print_func)(DavResource*, CmdArgs *);
264 } 302 if(cmd_getoption(a, "list")) {
265 DavResource *child = ls->children; 303 print_func = ls_print_list_elm;
266 while(child) { 304 } else {
267 if(child->name[0] != '.' || show_all) { 305 print_func = ls_print_elm;
268 print_func(child, a); 306 }
269 } 307 DavResource *child = ls->children;
270 child = child->next; 308 while(child) {
271 } 309 if(child->name[0] != '.' || show_all) {
272 310 print_func(child, a);
273 free(root); 311 }
312 child = child->next;
313 }
314
315 // leave loop
316 ret = 0;
317 }
318
274 free(path); 319 free(path);
275 free(base); 320 free(base);
276 321
277 return 0; 322 return ret;
278 } 323 }
279 324
280 static char* ls_date_str(time_t tm) { 325 static char* ls_date_str(time_t tm) {
281 struct tm t; 326 struct tm t;
282 struct tm n; 327 struct tm n;
434 // TODO: change this, when get supports retrieval of multiple files 479 // TODO: change this, when get supports retrieval of multiple files
435 fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many"); 480 fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many");
436 return -1; 481 return -1;
437 } 482 }
438 483
484 char *url = a->argv[0];
485 char *path = NULL;
439 DavSession *sn = NULL; 486 DavSession *sn = NULL;
440 char *url = a->argv[0]; 487 Repository *repo = url2repo(url, &path);
441 char *root = NULL; 488 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
442 char *path = NULL;
443 url_get_parts(url, &root, &path);
444
445 Repository *repo = get_repository(root);
446 if(repo) {
447 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
448 } else {
449 sn = dav_session_new(ctx, root);
450 }
451 489
452 char *update = cmd_getoption(a, "update"); 490 char *update = cmd_getoption(a, "update");
453 time_t t = 0; 491 time_t t = 0;
454 if(update) { 492 if(update) {
455 t = util_parse_lastmodified(update); 493 t = util_parse_lastmodified(update);
626 // TODO: change, when put supports multiple files (however it should do) 664 // TODO: change, when put supports multiple files (however it should do)
627 fprintf(stderr, "Too %s arguments\n", a->argc < 2 ? "few":"many"); 665 fprintf(stderr, "Too %s arguments\n", a->argc < 2 ? "few":"many");
628 return -1; 666 return -1;
629 } 667 }
630 668
631 DavSession *sn = NULL;
632 char *url = a->argv[0]; 669 char *url = a->argv[0];
633 char *file = a->argv[1]; 670 char *file = a->argv[1];
634 char *root = NULL;
635 char *path = NULL; 671 char *path = NULL;
636 url_get_parts(url, &root, &path); 672 DavSession *sn = NULL;
637 673 Repository *repo = url2repo(url, &path);
638 Repository *repo = get_repository(root); 674 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
639 if(repo) {
640 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
641 } else {
642 sn = dav_session_new(ctx, root);
643 }
644 675
645 int ret; 676 int ret;
646 if(!strcmp(file, "-")) { 677 if(!strcmp(file, "-")) {
647 FILE *in = stdin; 678 FILE *in = stdin;
648 ret = put_file(repo, a, sn, path, "stdin", in); 679 ret = put_file(repo, a, sn, path, "stdin", in);
780 // TODO: change, when removal of multiple files is supported 811 // TODO: change, when removal of multiple files is supported
781 fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many"); 812 fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many");
782 return -1; 813 return -1;
783 } 814 }
784 815
816 char *url = a->argv[0];
817 char *path = NULL;
785 DavSession *sn = NULL; 818 DavSession *sn = NULL;
786 char *url = a->argv[0]; 819 Repository *repo = url2repo(url, &path);
787 char *root = NULL; 820 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
788 char *path = NULL;
789 url_get_parts(url, &root, &path);
790
791 Repository *repo = get_repository(root);
792 if(repo) {
793 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
794 } else {
795 sn = dav_session_new(ctx, root);
796 }
797 821
798 DavResource *res = dav_resource_new(sn, path); 822 DavResource *res = dav_resource_new(sn, path);
799 if(!res) { 823 if(!res) {
800 fprintf(stderr, "error\n"); 824 fprintf(stderr, "error\n");
801 return -1; 825 return -1;
815 // TODO: change, when creation of multiple dirs is supported 839 // TODO: change, when creation of multiple dirs is supported
816 fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many"); 840 fprintf(stderr, "Too %s arguments\n", a->argc < 1 ? "few":"many");
817 return -1; 841 return -1;
818 } 842 }
819 843
844 char *url = a->argv[0];
845 char *path = NULL;
820 DavSession *sn = NULL; 846 DavSession *sn = NULL;
821 char *url = a->argv[0]; 847 Repository *repo = url2repo(url, &path);
822 char *root = NULL; 848 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
823 char *path = NULL;
824 url_get_parts(url, &root, &path);
825
826 Repository *repo = get_repository(root);
827 if(repo) {
828 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
829 } else {
830 sn = dav_session_new(ctx, root);
831 }
832 849
833 DavResource *res = dav_resource_new(sn, path); 850 DavResource *res = dav_resource_new(sn, path);
834 if(!res) { 851 if(!res) {
835 fprintf(stderr, "error\n"); 852 fprintf(stderr, "error\n");
836 return -1; 853 return -1;
867 char str[32]; 884 char str[32];
868 putenv("LC_TIME=C"); 885 putenv("LC_TIME=C");
869 size_t len = strftime(str, 32, "%a, %d %b %Y %H:%M:%S GMT\n", date); 886 size_t len = strftime(str, 32, "%a, %d %b %Y %H:%M:%S GMT\n", date);
870 fwrite(str, 1, len, stdout); 887 fwrite(str, 1, len, stdout);
871 } else if (a->argc == 1) { 888 } else if (a->argc == 1) {
889 char *url = a->argv[0];
890 char *path = NULL;
872 DavSession *sn = NULL; 891 DavSession *sn = NULL;
873 char *url = a->argv[0]; 892 Repository *repo = url2repo(url, &path);
874 char *root = NULL; 893 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
875 char *path = NULL;
876 url_get_parts(url, &root, &path);
877
878 Repository *repo = get_repository(root);
879 if(repo) {
880 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
881 } else {
882 sn = dav_session_new(ctx, root);
883 }
884 894
885 DavResource *res = dav_resource_new(sn, path); 895 DavResource *res = dav_resource_new(sn, path);
886 char *date = NULL; 896 char *date = NULL;
887 curl_easy_setopt(sn->handle, CURLOPT_HEADERFUNCTION, get_date_header_cb); 897 curl_easy_setopt(sn->handle, CURLOPT_HEADERFUNCTION, get_date_header_cb);
888 curl_easy_setopt(sn->handle, CURLOPT_WRITEHEADER, &date); 898 curl_easy_setopt(sn->handle, CURLOPT_WRITEHEADER, &date);

mercurial