dav/main.c

changeset 4
ae5a98f0545c
parent 3
323689ada09d
child 5
88625853ae74
equal deleted inserted replaced
3:323689ada09d 4:ae5a98f0545c
27 */ 27 */
28 28
29 #include <stdio.h> 29 #include <stdio.h>
30 #include <stdlib.h> 30 #include <stdlib.h>
31 #include <string.h> 31 #include <string.h>
32 #include <unistd.h>
32 #include <libxml/xmlerror.h> 33 #include <libxml/xmlerror.h>
34 #include <sys/types.h>
35 #include <sys/stat.h>
33 36
34 #include "propfind.h" 37 #include "propfind.h"
35 #include "utils.h" 38 #include "utils.h"
36 #include "main.h" 39 #include "main.h"
37 40
50 53
51 if(!strcmp(argv[1], "list") || !strcmp(argv[1], "ls")) { 54 if(!strcmp(argv[1], "list") || !strcmp(argv[1], "ls")) {
52 return cmd_list(argc - 2, argv + 2); 55 return cmd_list(argc - 2, argv + 2);
53 } else if(!strcmp(argv[1], "get")) { 56 } else if(!strcmp(argv[1], "get")) {
54 return cmd_get(argc - 2, argv + 2); 57 return cmd_get(argc - 2, argv + 2);
58 } else if(!strcmp(argv[1], "put")) {
59 return cmd_put(argc - 2, argv + 2);
55 } 60 }
56 61
57 print_usage(); 62 print_usage();
58 return -1; 63 return -1;
59 } 64 }
60 65
61 void print_usage() { 66 void print_usage() {
62 67
68 }
69
70 void get_file(CURL *curl, char *url, char *path) {
71 curl_easy_setopt(curl, CURLOPT_URL, url);
72 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
73
74 FILE *out = fopen(path, "w");
75
76 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite);
77 curl_easy_setopt(curl, CURLOPT_WRITEDATA, out);
78
79 CURLcode res = curl_easy_perform(curl);
80
81 fclose(out);
82 // handle some errors (http://curl.haxx.se/libcurl/c/libcurl-errors.html)
83 switch(res) {
84 case CURLE_OK: {
85 return;
86 }
87 case CURLE_REMOTE_ACCESS_DENIED: {
88
89 break;
90 }
91 case CURLE_SSL_CONNECT_ERROR: {
92
93 break;
94 }
95 case CURLE_LOGIN_DENIED: {
96
97 break;
98 }
99 case CURLE_REMOTE_FILE_NOT_FOUND: {
100
101 break;
102 }
103 default: {
104
105 break;
106 }
107 }
108
109 unlink(path);
110 }
111
112 void put_file(CURL *curl, char *url, char *path) {
113 curl_easy_setopt(curl, CURLOPT_URL, url);
114 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0);
115
116 curl_easy_setopt(curl, CURLOPT_PUT, 1L);
117
118 struct stat s;
119 if(stat(path, &s) != 0) {
120 perror("stat");
121 fprintf(stderr, "file: %s\n", path);
122 return;
123 }
124
125 FILE *in = fopen(path, "r");
126
127 curl_easy_setopt(curl, CURLOPT_UPLOAD, 1L);
128 curl_easy_setopt(curl, CURLOPT_READFUNCTION, fread);
129 curl_easy_setopt(curl, CURLOPT_READDATA, in);
130 curl_easy_setopt(curl, CURLOPT_INFILESIZE_LARGE, (curl_off_t)s.st_size);
131
132 CURLcode res = curl_easy_perform(curl);
133 fclose(in);
134
135 if(res != CURLE_OK) {
136 fprintf(stderr, "put_file: %s\n", curl_easy_strerror(res));
137 }
63 } 138 }
64 139
65 int cmd_list(int argc, char **argv) { 140 int cmd_list(int argc, char **argv) {
66 if(argc == 0) { 141 if(argc == 0) {
67 return -1; 142 return -1;
111 curl_easy_cleanup(curl); 186 curl_easy_cleanup(curl);
112 187
113 return 0; 188 return 0;
114 } 189 }
115 190
116 void get_file(CURL *curl, char *url, char *path) { 191 int cmd_put(int argc, char **argv) {
117 curl_easy_setopt(curl, CURLOPT_URL, url); 192 if(argc == 0) {
118 curl_easy_setopt(curl, CURLOPT_SSL_VERIFYPEER, 0); 193 return -1;
119 194 }
120 FILE *out = fopen(path, "w"); 195
121 196 char *url = argv[0]; // TODO: use arg as url or alias
122 curl_easy_setopt(curl, CURLOPT_WRITEFUNCTION, fwrite); 197 char *path;
123 curl_easy_setopt(curl, CURLOPT_WRITEDATA, out); 198 if(argc > 0) {
124 199 path = argv[1];
125 CURLcode res = curl_easy_perform(curl); 200 } else {
126 201 fprintf(stderr, "put: missing file argument\n");
127 fclose(out); 202 return -1;
128 // handle some errors (http://curl.haxx.se/libcurl/c/libcurl-errors.html) 203 }
129 switch(res) { 204
130 case CURLE_OK: { 205 char *uploadurl = util_upload_url(url, path);
131 return; 206
132 } 207 CURL *curl = curl_easy_init();
133 case CURLE_REMOTE_ACCESS_DENIED: { 208
134 209 // TODO: check possible name conflicts
135 break; 210
136 } 211 put_file(curl, uploadurl, path);
137 case CURLE_SSL_CONNECT_ERROR: { 212
138 213 curl_easy_cleanup(curl);
139 break; 214 }
140 }
141 case CURLE_LOGIN_DENIED: {
142
143 break;
144 }
145 case CURLE_REMOTE_FILE_NOT_FOUND: {
146
147 break;
148 }
149 default: {
150
151 break;
152 }
153 }
154
155 unlink(path);
156 }

mercurial