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)) { |