fix directories not stored in db

Sat, 10 Aug 2019 13:48:14 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 10 Aug 2019 13:48:14 +0200
changeset 613
f6072141f5ee
parent 612
66dc8b992d8d
child 614
e72d2b99b0f6

fix directories not stored in db

dav/db.c file | annotate | diff | comparison | revisions
dav/sync.c file | annotate | diff | comparison | revisions
dav/sync.h file | annotate | diff | comparison | revisions
--- a/dav/db.c	Fri Aug 02 22:05:28 2019 +0200
+++ b/dav/db.c	Sat Aug 10 13:48:14 2019 +0200
@@ -223,6 +223,8 @@
                 res->tags_updated = TRUE;
             } else if(xstreq(name, "parts")) {
                 process_parts(reader, res);
+            } else if(xstreq(name, "isdirectory")) {
+                res->isdirectory = 1;
             }
         } else if(type == XML_READER_TYPE_TEXT) {
             const xmlChar *value = xmlTextReaderConstValue(reader);
@@ -419,6 +421,16 @@
             return -1;
         }
         
+        if(res->isdirectory) {
+            r = xmlTextWriterStartElement(writer, BAD_CAST "isdirectory");
+            r += xmlTextWriterEndElement(writer);
+            if(r < 0) {
+                fprintf(stderr, "Cannot write isdirectory\n");
+                xmlFreeTextWriter(writer);
+                return -1;
+            }
+        }
+        
         if(res->etag) {
             r = xmlTextWriterWriteElement(
                     writer,
--- a/dav/sync.c	Fri Aug 02 22:05:28 2019 +0200
+++ b/dav/sync.c	Sat Aug 10 13:48:14 2019 +0200
@@ -719,16 +719,9 @@
     // the first thing we need are all directories to put the files in
     UCX_FOREACH(elm, res_mkdir) {
         DavResource *res = elm->data;
-        char *res_path = resource_local_path(res);
-        char *local_path = create_local_path(dir, res->path);
-        free(res_path);
-        if(sys_mkdir(local_path) && errno != EEXIST) {
-            fprintf(stderr,
-                    "Cannot create directory %s: %s",
-                    local_path, strerror(errno));
+        if(sync_get_collection(a, dir, res, db)) {
+            sync_error++;
         }
-        free(local_path);
-        // TODO: create localres for dir
     }
     
     // we need a map for all conflicts for fast lookups
@@ -1473,6 +1466,69 @@
     return ret;
 }
 
+int sync_get_collection(
+        CmdArgs *a,
+        SyncDirectory *dir,
+        DavResource *res,
+        SyncDatabase *db)
+{
+    char *res_path = resource_local_path(res);
+    char *local_path = create_local_path(dir, res->path);
+    free(res_path);
+    
+    printf("get: %s\n", res->path);
+    // create directory
+    // ignore error if it already exists
+    if(sys_mkdir(local_path) && errno != EEXIST) {
+        fprintf(stderr,
+                "Cannot create directory %s: %s",
+                local_path, strerror(errno));
+        free(local_path);
+        return 1;
+    }
+    SYS_STAT s;
+    if(sys_stat(local_path, &s)) {
+        fprintf(stderr, "Cannot stat directory %s: %s", local_path, strerror(errno));
+        free(local_path);
+        return 1;
+    }
+    
+    // if it doesn't exist in the db, create an entry for the dir
+    LocalResource *local = ucx_map_cstr_get(db->resources, res->path);
+    if(!local) {
+        local = calloc(1, sizeof(LocalResource));
+        local->path = strdup(res->path);
+        ucx_map_cstr_put(db->resources, local->path, local);
+    }
+    local->isdirectory = 1;
+    
+    // cleanup LocalResource
+    if(local->etag) {
+        free(local->etag);
+        local->etag = NULL;
+    }
+    if(local->hash) {
+        free(local->hash);
+        local->hash = NULL;
+    }
+    if(local->link_target) {
+        free(local->link_target);
+        local->link_target = NULL;
+    }
+    local->skipped = 0;
+    local->size = 0;
+    
+    // set metadata
+    if(sync_store_metadata(dir, local_path, local, res)) {
+        fprintf(stderr, "Cannot store metadata: %s\n", res->path);
+    }
+    sync_set_metadata_from_stat(local, &s);
+    
+    // cleanup
+    free(local_path);
+    return 0;
+}
+
 int sync_remove_local_resource(SyncDirectory *dir, LocalResource *res) {
     char *local_path = create_local_path(dir, res->path);
     SYS_STAT s;
@@ -1923,7 +1979,7 @@
             sync_error++;
         }
         
-        //int abort = 0;
+        int abort = 0;
         
         dav_exists(res);
         if(sn->error == DAV_NOT_FOUND) {
@@ -1935,17 +1991,7 @@
                 ret = -1;
                 sync_error++;
                 error = 1;
-                //abort = 1;
-            } else {
-                // success
-                if(local_res->metadata_updated) {
-                    sync_update_metadata(dir, sn, res, local_res);
-                }
-                
-                // remove old db entry (if it exists)
-                // and add add new entry
-                LocalResource *dbres = ucx_map_cstr_remove(db->resources, local_res->path);
-                ucx_map_cstr_put(db->resources, local_res->path, local_res);
+                abort = 1;
             }
         } else if(sn->error != DAV_OK) {
             // dav_exists() failed
@@ -1953,7 +1999,19 @@
             ret = -1;
             sync_error++;
             error = 1;
-            //abort = 1;
+            abort = 1;
+        }
+        
+        if(!abort) {
+            // success
+            if(local_res->metadata_updated) {
+                sync_update_metadata(dir, sn, res, local_res);
+            }
+
+            // remove old db entry (if it exists)
+            // and add add new entry
+            LocalResource *dbres = ucx_map_cstr_remove(db->resources, local_res->path);
+            ucx_map_cstr_put(db->resources, local_res->path, local_res);
         }
         
         dav_resource_free(res);
--- a/dav/sync.h	Fri Aug 02 22:05:28 2019 +0200
+++ b/dav/sync.h	Sat Aug 10 13:48:14 2019 +0200
@@ -123,6 +123,11 @@
         DavResource *res,
         SyncDatabase *db,
         int *counter);
+int sync_get_collection(
+        CmdArgs *a,
+        SyncDirectory *dir,
+        DavResource *res,
+        SyncDatabase *db);
 int sync_remove_local_resource(SyncDirectory *dir, LocalResource *res);
 int sync_remove_local_directory(SyncDirectory *dir, LocalResource *res);
 void rename_conflict_file(SyncDirectory *dir, SyncDatabase *db, char *path, DavBool copy);

mercurial