744 int ret; |
744 int ret; |
745 if(!strcmp(file, "-")) { |
745 if(!strcmp(file, "-")) { |
746 FILE *in = stdin; |
746 FILE *in = stdin; |
747 ret = put_file(repo, a, sn, path, "stdin", in, 0); |
747 ret = put_file(repo, a, sn, path, "stdin", in, 0); |
748 } else { |
748 } else { |
749 ret = put_entry(repo, a, sn, path, file); |
749 ret = put_entry(repo, a, sn, path, file, TRUE); |
750 } |
750 } |
751 |
751 |
752 free(path); |
752 free(path); |
753 return ret; |
753 return ret; |
754 } |
754 } |
755 |
755 |
756 int put_entry(Repository *repo, CmdArgs *a, DavSession *sn, char *path, char *file) { |
756 int put_entry(Repository *repo, CmdArgs *a, DavSession *sn, char *path, char *file, DavBool root) { |
757 int recursive = cmd_getoption(a, "recursive") ? 1 : 0; |
757 int recursive = cmd_getoption(a, "recursive") ? 1 : 0; |
758 if(recursive) { |
758 if(recursive && !root) { |
759 printf("put: %s\n", file); |
759 printf("put: %s\n", file); |
760 } |
760 } |
761 struct stat s; |
761 struct stat s; |
762 if(stat(file, &s)) { |
762 if(stat(file, &s)) { |
763 perror("stat"); |
763 perror("stat"); |
766 } |
766 } |
767 |
767 |
768 int ret = 0; |
768 int ret = 0; |
769 if(S_ISDIR(s.st_mode)) { |
769 if(S_ISDIR(s.st_mode)) { |
770 if(!recursive) { |
770 if(!recursive) { |
|
771 fprintf( |
|
772 stderr, |
|
773 "%s is a directory.\nUse the -R option to upload directories.", |
|
774 file); |
771 return 1; |
775 return 1; |
772 } |
776 } |
773 DIR *dir = opendir(file); |
777 DIR *dir = opendir(file); |
774 if(!dir) { |
778 if(!dir) { |
775 // error |
779 // error |
776 } |
780 } |
777 struct dirent *entry; |
781 struct dirent *entry; |
|
782 int nument = 0; |
778 while((entry = readdir(dir)) != NULL) { |
783 while((entry = readdir(dir)) != NULL) { |
779 if(!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) { |
784 if(!strcmp(entry->d_name, ".") || !strcmp(entry->d_name, "..")) { |
780 continue; |
785 continue; |
781 } |
786 } |
|
787 nument++; |
782 char *entry_file = util_concat_path(file, entry->d_name); |
788 char *entry_file = util_concat_path(file, entry->d_name); |
783 char *entry_path = util_concat_path(path, entry->d_name); |
789 char *entry_path = util_concat_path(path, entry->d_name); |
784 int r = put_entry(repo, a, sn, entry_path, entry_file); |
790 int r = put_entry(repo, a, sn, entry_path, entry_file, FALSE); |
785 free(entry_path); |
791 free(entry_path); |
786 free(entry_file); |
792 free(entry_file); |
787 if(r) { |
793 if(r) { |
788 ret = 1; |
794 ret = 1; |
789 break; |
795 break; |
790 } |
796 } |
791 } |
797 } |
792 closedir(dir); |
798 closedir(dir); |
|
799 |
|
800 if(nument == 0) { |
|
801 // create empty directory |
|
802 DavResource *res = dav_resource_new(sn, path); |
|
803 res->iscollection = TRUE; |
|
804 if(!dav_exists(res)) { |
|
805 if(dav_create(res)) { |
|
806 fprintf(stderr, "Cannot create collection %s\n", path); |
|
807 print_resource_error(sn, res->path); |
|
808 ret = 1; |
|
809 } |
|
810 } |
|
811 dav_resource_free(res); |
|
812 } |
793 } else if(S_ISREG(s.st_mode)) { |
813 } else if(S_ISREG(s.st_mode)) { |
794 /* |
814 /* |
795 * use stdin if the input file is - |
815 * use stdin if the input file is - |
796 */ |
816 */ |
797 FILE *in = fopen(file, "rb"); |
817 FILE *in = fopen(file, "rb"); |