dav/sync.c

changeset 222
7b73058d782e
parent 221
e22c29b7ee2f
child 223
cbbdf207e67e
--- a/dav/sync.c	Fri Mar 18 15:08:30 2016 +0100
+++ b/dav/sync.c	Fri Mar 18 19:54:36 2016 +0100
@@ -90,6 +90,8 @@
         memcpy(ctx->https_proxy, get_https_proxy(), sizeof(Proxy));
         
         if(load_sync_config()) {
+            cmd_args_free(args);
+            dav_context_destroy(ctx);
             return EXIT_FAILURE;
         }
     }
@@ -113,6 +115,10 @@
     }
     
     // TODO: cleanup sync config (don't forget to call regfree for regex)
+    cmd_args_free(args);
+    dav_context_destroy(ctx);
+    
+    free_config();
     
     return ret;
 }
@@ -208,6 +214,7 @@
     SyncDatabase *db = load_db(dir->database);
     if(!db) {
         fprintf(stderr, "Cannot load database file: %s\n", dir->database);
+        destroy_db(db);
         return -1;
     }
     remove_deleted_conflicts(dir, db);
@@ -217,6 +224,7 @@
         new_url = util_concat_path(repo->url, dir->collection);
     }
     DavSession *sn = create_session(ctx, repo, new_url ? new_url : repo->url);
+    ucx_mempool_reg_destr(sn->mp, db, (ucx_destructor)destroy_db);
     if(new_url) {
         free(new_url);
     }
@@ -400,6 +408,7 @@
             }
             if(!strcmp(e.ptr, local->etag)) {
                 // resource is already up-to-date on the client
+                free(local_path);
                 return 0;
             }
         }
@@ -491,7 +500,7 @@
                 free(local->etag);
             }
             // set metadata from stat
-            local->etag = etag;
+            local->etag = strdup(etag);
             local->last_modified = s.st_mtime;
             local->size = s.st_size;
         } else {
@@ -695,6 +704,7 @@
         new_url = util_concat_path(repo->url, dir->collection);
     }
     DavSession *sn = create_session(ctx, repo, new_url ? new_url : repo->url);
+    ucx_mempool_reg_destr(sn->mp, db, (ucx_destructor)destroy_db);
     if(new_url) {
         free(new_url);
     }
@@ -743,6 +753,7 @@
             if(res_isconflict(db, local_res)) {
                 printf("skip: %s\n", local_res->path);
                 sync_skipped++;
+                local_resource_free(local_res);
                 continue;
             }
             
@@ -766,6 +777,7 @@
                     if(cdt && remote_resource_is_changed(sn, dir, db, local_res)) {
                         printf("conflict: %s\n", local_res->path);
                         sync_skipped++;
+                        local_resource_free(local_res);
                         continue;
                     }
                     
@@ -783,7 +795,10 @@
             // remove every locally available resource from db->resource
             // the remaining elements are all deleted files
             ucx_map_cstr_put(lclres, local_res->path, local_res);
-            ucx_map_cstr_remove(db->resources, local_res->path); // TODO: element leaked
+            LocalResource *lr = ucx_map_cstr_remove(db->resources, local_res->path);
+            if(lr) {
+                local_resource_free(lr);
+            }
         }
     }
     ucx_list_free(resources);
@@ -824,6 +839,7 @@
     
     // cleanup
     dav_session_destroy(sn);
+    ucx_list_free(resources); // content is already freed
     
     // Report
     if(ret == 0) {
@@ -1194,6 +1210,8 @@
     for(int i=0;i<numdc;i++) {
         ucx_map_cstr_remove(db->conflict, dc[i]);
     }
+    
+    free(dc);
 }
 
 int cmd_resolve_conflicts(CmdArgs *a) {
@@ -1214,6 +1232,8 @@
         return -1;
     }
     
+    int ret = 0;
+    
     // remove conflicts
     int num_conflict = db->conflict->count;
     //TODO: ucx_map_free_content(db->conflict, destr);
@@ -1223,14 +1243,17 @@
     if(store_db(db, dir->database)) {
         fprintf(stderr, "Cannot store sync db\n");
         fprintf(stderr, "Abort\n");
-        return -1; // TODO: don't return here
+        ret = -1;
     }
+    destroy_db(db);
     
     // Report
-    char *str_conflict = num_conflict == 1 ? "conflict" : "conflicts";
-    printf("Result: %d %s resolved\n", num_conflict, str_conflict);
+    if(ret == 0) {
+        char *str_conflict = num_conflict == 1 ? "conflict" : "conflicts";
+        printf("Result: %d %s resolved\n", num_conflict, str_conflict);
+    }
     
-    return 0;
+    return ret;
 }
 
 int cmd_delete_conflicts(CmdArgs *a) {
@@ -1254,6 +1277,8 @@
     int num_del = 0;
     int num_err = 0;
     
+    int ret = 0;
+    
     // delete all conflict files
     UcxMapIterator i = ucx_map_iterator(db->conflict);
     LocalResource *res;
@@ -1277,17 +1302,20 @@
     if(store_db(db, dir->database)) {
         fprintf(stderr, "Cannot store sync db\n");
         fprintf(stderr, "Abort\n");
-        return -1; // TODO: don't return here
+        ret = -1;
     }
+    destroy_db(db);
     
     // Report
-    char *str_delete = num_del == 1 ? "file" : "files";
-    char *str_error = num_err == 1 ? "error" : "errors";
-    printf("Result: %d conflict %s deleted, %d %s\n",
-            num_del, str_delete,
-            num_err, str_error);
+    if(ret == 0) {
+        char *str_delete = num_del == 1 ? "file" : "files";
+        char *str_error = num_err == 1 ? "error" : "errors";
+        printf("Result: %d conflict %s deleted, %d %s\n",
+                num_del, str_delete,
+                num_err, str_error);
+    }
     
-    return 0;
+    return ret;
 }
 
 

mercurial