dav/main.c

changeset 500
0fe1514667e6
parent 498
37af2eac3e6a
child 507
dea7d1b4eea0
equal deleted inserted replaced
499:efd8f489d415 500:0fe1514667e6
1203 1203
1204 return 0; 1204 return 0;
1205 } 1205 }
1206 1206
1207 int cmd_put(CmdArgs *a, DavBool import) { 1207 int cmd_put(CmdArgs *a, DavBool import) {
1208 if(a->argc != 2) { 1208 if(a->argc < 2) {
1209 // TODO: change, when put supports multiple files (however it should do) 1209 fprintf(stderr, "Too few arguments\n");
1210 fprintf(stderr, "Too %s arguments\n", a->argc < 2 ? "few":"many"); 1210 fprintf(stderr,
1211 fprintf(stderr, "Usage: dav %s\n", find_usage_str("put")); 1211 "Usage: dav %s\n",
1212 return -1; 1212 find_usage_str(import ? "import" : "put"));
1213 } 1213 return -1;
1214 }
1215 DavBool use_stdin = FALSE;
1216 for(int i=1;i<a->argc;i++) {
1217 if(!strcmp(a->argv[i], "-")) {
1218 if(use_stdin) {
1219 fprintf(stderr, "Error: stdin can only occur once in input list\n");
1220 return -1;
1221 } else {
1222 use_stdin = TRUE;
1223 }
1224 }
1225 }
1226
1214 if(import) { 1227 if(import) {
1215 ucx_map_cstr_put(a->options, "resursive", ""); 1228 ucx_map_cstr_put(a->options, "resursive", "");
1216 } 1229 }
1217 1230
1218 char *url = a->argv[0]; 1231 char *url = a->argv[0];
1219 char *file = a->argv[1];
1220 char *path = NULL; 1232 char *path = NULL;
1221 Repository *repo = url2repo(url, &path); 1233 Repository *repo = url2repo(url, &path);
1222 DavSession *sn = connect_to_repo(repo, path, a); 1234 DavSession *sn = connect_to_repo(repo, path, a);
1223 1235
1224 if(set_session_config(sn, a)) { 1236 if(set_session_config(sn, a)) {
1229 if(check_encryption_key(a, sn)) { 1241 if(check_encryption_key(a, sn)) {
1230 // TODO: free 1242 // TODO: free
1231 return -1; 1243 return -1;
1232 } 1244 }
1233 1245
1246 DavBool printfile = FALSE;
1247 DavBool ignoredirerr = FALSE;
1248 if(a->argc > 2) {
1249 printfile = TRUE;
1250 ignoredirerr = TRUE;
1251 } else if(ucx_map_cstr_get(a->options, "recursive")) {
1252 printfile = TRUE;
1253 }
1254
1255
1234 int ret; 1256 int ret;
1235 if(!import) { 1257 for(int i=1;i<a->argc;i++) {
1236 if(!strcmp(file, "-")) { 1258 char *file = a->argv[i];
1237 FILE *in = stdin; 1259 if(!import) {
1238 ret = put_file(repo, a, sn, path, "stdin", in, 0); 1260 if(!strcmp(file, "-")) {
1239 } else { 1261 FILE *in = stdin;
1240 ret = put_entry(repo, a, sn, path, file, TRUE); 1262 ret = put_file(repo, a, sn, path, "stdin", in, 0);
1241 } 1263 } else {
1242 } else { 1264 ret = put_entry(
1243 ret = put_tar(repo, a, sn, file, path); 1265 repo,
1266 a,
1267 sn,
1268 path,
1269 file,
1270 TRUE,
1271 printfile,
1272 ignoredirerr);
1273 }
1274 } else {
1275 ret = put_tar(repo, a, sn, file, path);
1276 }
1277 if(ret) {
1278 break;
1279 }
1244 } 1280 }
1245 1281
1246 free(path); 1282 free(path);
1283 dav_session_destroy(sn);
1247 return ret; 1284 return ret;
1248 } 1285 }
1249 1286
1250 int put_entry(Repository *repo, CmdArgs *a, DavSession *sn, char *path, char *file, DavBool root) { 1287 int put_entry(
1288 Repository *repo,
1289 CmdArgs *a,
1290 DavSession *sn,
1291 char *path,
1292 char *file,
1293 DavBool root,
1294 DavBool printfile,
1295 DavBool ignoredirerr)
1296 {
1251 int recursive = cmd_getoption(a, "recursive") ? 1 : 0; 1297 int recursive = cmd_getoption(a, "recursive") ? 1 : 0;
1252 struct stat s; 1298 struct stat s;
1253 if(stat(file, &s)) { 1299 if(stat(file, &s)) {
1254 perror("stat"); 1300 perror("stat");
1255 fprintf(stderr, "cannot stat file %s\n", file); 1301 fprintf(stderr, "cannot stat file %s\n", file);
1257 } 1303 }
1258 1304
1259 int ret = 0; 1305 int ret = 0;
1260 if(S_ISDIR(s.st_mode)) { 1306 if(S_ISDIR(s.st_mode)) {
1261 if(!recursive) { 1307 if(!recursive) {
1262 fprintf( 1308 if(ignoredirerr) {
1309 printf("skip: %s\n", file);
1310 } else {
1311 fprintf(
1263 stderr, 1312 stderr,
1264 "%s is a directory.\nUse the -R option to upload directories.\n", 1313 "%s is a directory.\nUse the -R option to upload directories.\n",
1265 file); 1314 file);
1266 return 1; 1315 return 1;
1316 }
1267 } 1317 }
1268 1318
1269 if(!root) { 1319 if(!root) {
1270 printf("mkcol: %s\n", file); 1320 printf("mkcol: %s\n", file);
1271 } 1321 }
1281 continue; 1331 continue;
1282 } 1332 }
1283 nument++; 1333 nument++;
1284 char *entry_file = util_concat_path(file, entry->d_name); 1334 char *entry_file = util_concat_path(file, entry->d_name);
1285 char *entry_path = util_concat_path(path, entry->d_name); 1335 char *entry_path = util_concat_path(path, entry->d_name);
1286 int r = put_entry(repo, a, sn, entry_path, entry_file, FALSE); 1336 int r = put_entry(
1337 repo,
1338 a,
1339 sn,
1340 entry_path,
1341 entry_file,
1342 FALSE,
1343 printfile,
1344 ignoredirerr);
1287 free(entry_path); 1345 free(entry_path);
1288 free(entry_file); 1346 free(entry_file);
1289 if(r) { 1347 if(r) {
1290 ret = 1; 1348 ret = 1;
1291 break; 1349 break;
1305 } 1363 }
1306 } 1364 }
1307 dav_resource_free(res); 1365 dav_resource_free(res);
1308 } 1366 }
1309 } else if(S_ISREG(s.st_mode)) { 1367 } else if(S_ISREG(s.st_mode)) {
1310 if(recursive) { 1368 if(printfile) {
1311 printf("put: %s\n", file); 1369 printf("put: %s\n", file);
1312 } 1370 }
1313 1371
1314 /* 1372 /*
1315 * use stdin if the input file is - 1373 * use stdin if the input file is -

mercurial