dav/main.c

changeset 383
bc215bdaf9bc
parent 382
1ce7211a1021
child 384
8bfde5687890
equal deleted inserted replaced
382:1ce7211a1021 383:bc215bdaf9bc
1297 free(path); 1297 free(path);
1298 return 0; 1298 return 0;
1299 } 1299 }
1300 1300
1301 int cmd_move(CmdArgs *a, int cp) { 1301 int cmd_move(CmdArgs *a, int cp) {
1302 const char* actionstr = cp ? "copy" : "move";
1303
1302 if(a->argc != 2) { 1304 if(a->argc != 2) {
1303 // TODO: change, when creation of multiple dirs is supported 1305 // TODO: change, when creation of multiple dirs is supported
1304 fprintf(stderr, "Too %s arguments\n", a->argc < 2 ? "few":"many"); 1306 fprintf(stderr, "Too %s arguments\n", a->argc < 2 ? "few":"many");
1305 fprintf(stderr, "Usage: dav %s\n", find_usage_str(cp ? "copy" : "move")); 1307 fprintf(stderr, "Usage: dav %s\n", find_usage_str(actionstr));
1306 return -1; 1308 return -1;
1307 } 1309 }
1308 1310
1309 char *url1 = a->argv[0]; 1311 char *srcurl = a->argv[0];
1310 char *path1 = NULL; 1312 char *srcpath = NULL;
1311 Repository *repo1 = url2repo(url1, &path1); 1313 Repository *srcrepo = url2repo(srcurl, &srcpath);
1312 1314
1313 DavSession *sn = connect_to_repo(repo1, a); 1315 DavSession *srcsn = connect_to_repo(srcrepo, a);
1314 if(set_session_config(sn, a)) { 1316 if(set_session_config(srcsn, a)) {
1315 return -1; 1317 return -1;
1316 } 1318 }
1317 set_session_lock(sn, a); 1319 set_session_lock(srcsn, a);
1318 1320
1319 DavBool override = cmd_getoption(a, "override") ? true : false; 1321 DavBool override = cmd_getoption(a, "override") ? true : false;
1320 1322
1321 char *url2 = a->argv[1]; 1323 char *desturl = a->argv[1];
1322 char *path2 = NULL; 1324 char *destpath = NULL;
1323 Repository *repo2 = url2repo(url2, &path2); 1325 Repository *destrepo = url2repo(desturl, &destpath);
1324 1326
1325 if(repo1 == repo2) { 1327 if(srcrepo == destrepo) {
1326 DavResource *res = dav_resource_new(sn, path1); 1328 DavResource *res = dav_resource_new(srcsn, srcpath);
1327 int err = cp ? dav_copy_o(res, path2, override) 1329 int err = cp ? dav_copy_o(res, destpath, override)
1328 : dav_move_o(res, path2, override); 1330 : dav_move_o(res, destpath, override);
1329 if(err) { 1331 if(err) {
1330 print_resource_error(sn, res->path); 1332 print_resource_error(srcsn, res->path);
1331 fprintf(stderr, "Cannot %s resource.\n", cp ? "copy" : "move"); 1333 fprintf(stderr, "Cannot %s resource.\n", actionstr);
1332 return -1; 1334 return -1;
1333 } 1335 }
1334 } else { 1336 } else {
1335 char *server1 = util_url_base(repo1->url); 1337 char *srchost = util_url_base(srcrepo->url);
1336 char *server2 = util_url_base(repo2->url); 1338 char *desthost = util_url_base(destrepo->url);
1337 if(!strcmp(server1, server2)) { 1339 if(!strcmp(srchost, desthost)) {
1338 DavSession *sn2 = connect_to_repo(repo2, a); 1340 DavSession *destsn = connect_to_repo(destrepo, a);
1339 if(set_session_config(sn2, a)) { 1341 if(set_session_config(destsn, a)) {
1340 return -1; 1342 return -1;
1341 } 1343 }
1342 DavResource *dest = dav_resource_new(sn2, path2); 1344 DavResource *dest = dav_resource_new(destsn, destpath);
1343 char *desthref = dav_resource_get_href(dest); 1345 char *desthref = dav_resource_get_href(dest);
1344 char *desturl = util_get_url(sn2, desthref); 1346 char *desturl = util_get_url(destsn, desthref);
1345 1347
1346 DavResource *res = dav_resource_new(sn, path1); 1348 DavResource *res = dav_resource_new(srcsn, srcpath);
1347 int err = cp ? dav_copyto(res, desturl, override) 1349 int err = cp ? dav_copyto(res, desturl, override)
1348 : dav_moveto(res, desturl, override); 1350 : dav_moveto(res, desturl, override);
1349 1351
1350 free(desturl); 1352 free(desturl);
1351 dav_session_destroy(sn2); 1353 dav_session_destroy(destsn);
1352 1354
1353 if(err) { 1355 if(err) {
1354 print_resource_error(sn, res->path); 1356 print_resource_error(srcsn, res->path);
1355 fprintf(stderr, "Cannot %s resource.\n", cp ? "copy" : "move"); 1357 fprintf(stderr, "Cannot %s resource.\n", actionstr);
1356 return -1; 1358 return -1;
1357 } 1359 }
1358 } else { 1360 } else {
1359 fprintf(stderr, "Copy or Move not supported for different hosts.\n"); 1361 fprintf(stderr, "Cannot %s between different hosts.\n", actionstr);
1360 return -1; 1362 return -1;
1361 } 1363 }
1362 } 1364 }
1363 1365
1364 dav_session_destroy(sn); 1366 dav_session_destroy(srcsn);
1365 1367
1366 return 0; 1368 return 0;
1367 } 1369 }
1368 1370
1369 1371

mercurial