dav/sync.c

changeset 224
4b55f05f4e00
parent 223
cbbdf207e67e
child 225
a297c2e28fa1
equal deleted inserted replaced
223:cbbdf207e67e 224:4b55f05f4e00
112 ret = cmd_add_directory(args); 112 ret = cmd_add_directory(args);
113 } else if(!strcmp(cmd, "list-directories")) { 113 } else if(!strcmp(cmd, "list-directories")) {
114 ret = list_syncdirs(); 114 ret = list_syncdirs();
115 } 115 }
116 116
117 // TODO: cleanup sync config (don't forget to call regfree for regex) 117 // cleanup
118 cmd_args_free(args); 118 cmd_args_free(args);
119 dav_context_destroy(ctx); 119 dav_context_destroy(ctx);
120 120
121 free_config(); 121 free_config();
122 free_sync_config();
123
124 curl_global_cleanup();
125 xmlCleanupParser();
122 126
123 return ret; 127 return ret;
124 } 128 }
125 129
126 void print_usage(char *cmd) { 130 void print_usage(char *cmd) {
261 265
262 dav_session_destroy(sn); 266 dav_session_destroy(sn);
263 // TODO: free 267 // TODO: free
264 return -1; 268 return -1;
265 } 269 }
270 if(!ls->iscollection) {
271 fprintf(stderr, "%s is not a collection.\nAbort.\n", ls->path);
272 if(locked) {
273 if(dav_unlock(root)) {
274 print_resource_error(sn, "/");
275 }
276 }
277 // TODO: free
278 dav_session_destroy(sn);
279 return -1;
280 }
266 281
267 int sync_success = 0; 282 int sync_success = 0;
268 int sync_delete = 0; 283 int sync_delete = 0;
269 int sync_error = 0; 284 int sync_error = 0;
270
271 if(!ls->children) {
272 // TODO: free
273 fprintf(stderr, "Repository is empty\n");
274 if(locked) {
275 if(dav_unlock(root)) {
276 print_resource_error(sn, "/");
277 }
278 }
279 return 0; // empty repository
280 }
281 285
282 UcxMap *svrres = ucx_map_new(db->resources->count); 286 UcxMap *svrres = ucx_map_new(db->resources->count);
283 287
284 UcxList *stack = ucx_list_prepend(NULL, ls->children); 288 UcxList *stack = ucx_list_prepend(NULL, ls->children);
285 while(stack) { 289 while(stack) {
336 } 340 }
337 UCX_FOREACH(elm, rmdirs) { 341 UCX_FOREACH(elm, rmdirs) {
338 LocalResource *local_dir = elm->data; 342 LocalResource *local_dir = elm->data;
339 sync_remove_local_directory(dir, local_dir); 343 sync_remove_local_directory(dir, local_dir);
340 } 344 }
345 ucx_map_free_content(db->resources, (ucx_destructor)local_resource_free);
341 ucx_map_free(db->resources); 346 ucx_map_free(db->resources);
342 db->resources = svrres; 347 db->resources = svrres;
343 348
344 // unlock repository 349 // unlock repository
345 if(locked) { 350 if(locked) {
353 if(store_db(db, dir->database)) { 358 if(store_db(db, dir->database)) {
354 fprintf(stderr, "Cannot store sync db\n"); 359 fprintf(stderr, "Cannot store sync db\n");
355 ret = -2; 360 ret = -2;
356 } 361 }
357 362
358 // TODO: cleanup 363 // cleanup
359 dav_session_destroy(sn); 364 dav_session_destroy(sn);
360 365
361 // Report 366 // Report
362 if(ret != -2) { 367 if(ret != -2) {
363 char *str_success = sync_success == 1 ? "file" : "files"; 368 char *str_success = sync_success == 1 ? "file" : "files";
549 void sync_remove_local_directory(SyncDirectory *dir, LocalResource *res) { 554 void sync_remove_local_directory(SyncDirectory *dir, LocalResource *res) {
550 char *local_path = util_concat_path(dir->path, res->path); 555 char *local_path = util_concat_path(dir->path, res->path);
551 556
552 printf("delete: %s\n", res->path); 557 printf("delete: %s\n", res->path);
553 if(rmdir(local_path)) { 558 if(rmdir(local_path)) {
554 // TODO 559 fprintf(stderr, "rmdir: %s : ", local_path);
560 perror(NULL);
555 } 561 }
556 562
557 free(local_path); 563 free(local_path);
558 } 564 }
559 565
1245 1251
1246 int ret = 0; 1252 int ret = 0;
1247 1253
1248 // remove conflicts 1254 // remove conflicts
1249 int num_conflict = db->conflict->count; 1255 int num_conflict = db->conflict->count;
1250 //TODO: ucx_map_free_content(db->conflict, destr); 1256 ucx_map_free_content(db->conflict, (ucx_destructor)local_resource_free);
1251 ucx_map_clear(db->conflict); 1257 ucx_map_clear(db->conflict);
1252 1258
1253 // store db 1259 // store db
1254 if(store_db(db, dir->database)) { 1260 if(store_db(db, dir->database)) {
1255 fprintf(stderr, "Cannot store sync db\n"); 1261 fprintf(stderr, "Cannot store sync db\n");
1256 fprintf(stderr, "Abort\n"); 1262 fprintf(stderr, "Abort\n");
1257 ret = -1; 1263 ret = -2;
1258 } 1264 }
1265
1266 // cleanup
1259 destroy_db(db); 1267 destroy_db(db);
1260 1268
1261 // Report 1269 // Report
1262 if(ret == 0) { 1270 if(ret != -2) {
1263 char *str_conflict = num_conflict == 1 ? "conflict" : "conflicts"; 1271 char *str_conflict = num_conflict == 1 ? "conflict" : "conflicts";
1264 printf("Result: %d %s resolved\n", num_conflict, str_conflict); 1272 printf("Result: %d %s resolved\n", num_conflict, str_conflict);
1265 } 1273 }
1266 1274
1267 return ret; 1275 return ret;
1304 } else { 1312 } else {
1305 num_del++; 1313 num_del++;
1306 } 1314 }
1307 free(path); 1315 free(path);
1308 } 1316 }
1309 //TODO: ucx_map_free_content(db->conflict, destr); 1317 ucx_map_free_content(db->conflict, (ucx_destructor)local_resource_free);
1310 ucx_map_clear(db->conflict); 1318 ucx_map_clear(db->conflict);
1311 1319
1312 // store db 1320 // store db
1313 if(store_db(db, dir->database)) { 1321 if(store_db(db, dir->database)) {
1314 fprintf(stderr, "Cannot store sync db\n"); 1322 fprintf(stderr, "Cannot store sync db\n");
1315 fprintf(stderr, "Abort\n"); 1323 fprintf(stderr, "Abort\n");
1316 ret = -1; 1324 ret = -1;
1317 } 1325 }
1326
1327 // cleanup
1318 destroy_db(db); 1328 destroy_db(db);
1319 1329
1320 // Report 1330 // Report
1321 if(ret == 0) { 1331 if(ret == 0) {
1322 char *str_delete = num_del == 1 ? "file" : "files"; 1332 char *str_delete = num_del == 1 ? "file" : "files";

mercurial