dav/main.c

changeset 28
4e46c65711ef
parent 27
e584c351b402
child 29
938957a4eea7
equal deleted inserted replaced
27:e584c351b402 28:4e46c65711ef
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 <sys/stat.h> 37 #include <sys/stat.h>
38 #include <ucx/string.h> 38 #include <ucx/string.h>
39 #include <sys/dirent.h>
40 #include <dirent.h>
41
39 42
40 #include "utils.h" 43 #include "utils.h"
41 #include "config.h" 44 #include "config.h"
42 #include "crypto.h" 45 #include "crypto.h"
43 #include "main.h" 46 #include "main.h"
579 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password); 582 sn = dav_session_new_auth(ctx, repo->url, repo->user, repo->password);
580 } else { 583 } else {
581 sn = dav_session_new(ctx, root); 584 sn = dav_session_new(ctx, root);
582 } 585 }
583 586
584 /* 587 int ret;
585 * use stdin if the input file is - 588 if(!strcmp(file, "-")) {
586 */ 589 FILE *in = stdin;
587 FILE *in = !strcmp(file, "-") ? in : fopen(file, "r"); 590 ret = put_file(repo, a, sn, path, "stdin", in);
588 if(!in) { 591 } else {
589 fprintf(stderr, "cannot open input file\n"); 592 ret = put_entry(repo, a, sn, path, file);
590 return -1; 593 }
591 } 594
592 595 return ret;
596 }
597
598 int put_entry(Repository *repo, CmdArgs *a, DavSession *sn, char *path, char *file) {
599 int recursive = cmd_getoption(a, "recursive") ? 1 : 0;
600 if(recursive) {
601 printf("put: %s\n", file);
602 }
603 struct stat s;
604 if(stat(file, &s)) {
605 perror("stat");
606 fprintf(stderr, "cannot stat file %s\n", file);
607 return -1;
608 }
609
610 int ret = 0;
611 if(S_ISDIR(s.st_mode)) {
612 if(!recursive) {
613 return 1;
614 }
615 DIR *dir = opendir(file);
616 if(!dir) {
617 // error
618 }
619 struct dirent *entry;
620 while((entry = readdir(dir)) != NULL) {
621 if(!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) {
622 continue;
623 }
624 char *entry_file = util_concat_path(file, entry->d_name);
625 char *entry_path = util_concat_path(path, entry->d_name);
626 int r = put_entry(repo, a, sn, entry_path, entry_file);
627 free(entry_path);
628 free(entry_file);
629 if(r) {
630 ret = 1;
631 break;
632 }
633 }
634 closedir(dir);
635 } else if(S_ISREG(s.st_mode)) {
636 /*
637 * use stdin if the input file is -
638 */
639 FILE *in = fopen(file, "r");
640 if(!in) {
641 fprintf(stderr, "cannot open input file\n");
642 return -1;
643 }
644 char *filename = util_resource_name(file);
645 //path = util_concat_path(path, filename);
646 ret = put_file(repo, a, sn, path, filename, in);
647 free(path);
648 fclose(in);
649 }
650
651 return ret;
652 }
653
654 int put_file(Repository *repo, CmdArgs *a, DavSession *sn, char *path, char *name, FILE *in) {
593 DavResource *res = dav_query(sn, "get - from %s", path); 655 DavResource *res = dav_query(sn, "get - from %s", path);
594 if(!res) { 656 if(!res) {
595 if(sn->error == DAV_NOT_FOUND) { 657 if(sn->error == DAV_NOT_FOUND) {
596 res = dav_resource_new(sn, path); 658 res = dav_resource_new(sn, path);
597 if(dav_create(res)) { 659 if(dav_create(res)) {
606 } 668 }
607 return -1; 669 return -1;
608 } 670 }
609 } else if(res->iscollection) { 671 } else if(res->iscollection) {
610 // TODO: free res 672 // TODO: free res
611 char *newpath = util_concat_path(path, file); 673 char *newpath = util_concat_path(path, name);
612 free(path); 674 free(path);
613 path = newpath; 675 path = newpath;
614 res = dav_resource_new(sn, path); 676 res = dav_resource_new(sn, path);
615 } 677 }
616 678
649 return -1; 711 return -1;
650 } 712 }
651 if(enc) { 713 if(enc) {
652 aes_encrypter_close(enc); 714 aes_encrypter_close(enc);
653 } 715 }
654 fclose(in);
655 return 0; 716 return 0;
656 } 717 }
718
657 719
658 int cmd_remove(CmdArgs *a) { 720 int cmd_remove(CmdArgs *a) {
659 if(a->argc < 1) { 721 if(a->argc < 1) {
660 fprintf(stderr, "Too few arguments\n"); 722 fprintf(stderr, "Too few arguments\n");
661 return -1; 723 return -1;

mercurial