dav-sync writes the locktoken to a file now

Tue, 05 Sep 2017 13:07:29 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 05 Sep 2017 13:07:29 +0200
changeset 296
bb49953b1cf8
parent 295
8ac7b8d56115
child 297
3cbe57eeaed1

dav-sync writes the locktoken to a file now

dav/config.c file | annotate | diff | comparison | revisions
dav/config.h file | annotate | diff | comparison | revisions
dav/sync.c file | annotate | diff | comparison | revisions
dav/sync.h file | annotate | diff | comparison | revisions
--- a/dav/config.c	Tue Sep 05 12:11:09 2017 +0200
+++ b/dav/config.c	Tue Sep 05 13:07:29 2017 +0200
@@ -84,6 +84,16 @@
     xmlFreeDoc(doc);
 }
 
+char* config_file_path(char *name) {
+    char *davd = util_concat_path(ENV_HOME, ".dav");
+    if(!davd) {
+        return NULL;
+    }
+    char *path = util_concat_path(davd, name);
+    free(davd);
+    return path;
+}
+
 int load_config(DavContext *ctx) {
     context = ctx;
     // TODO: free the config somewhere
--- a/dav/config.h	Tue Sep 05 12:11:09 2017 +0200
+++ b/dav/config.h	Tue Sep 05 13:07:29 2017 +0200
@@ -66,6 +66,8 @@
     int ssl_version;
     unsigned long authmethods;
 };
+
+char* config_file_path(char *name);
     
 int load_config(DavContext *ctx);
 void free_config(void);
--- a/dav/sync.c	Tue Sep 05 12:11:09 2017 +0200
+++ b/dav/sync.c	Tue Sep 05 13:07:29 2017 +0200
@@ -251,6 +251,7 @@
     }
     
     // lock repository
+    char *locktokenfile = NULL;
     DavBool locked = FALSE;
     DavResource *root = dav_resource_new(sn, "/");
     root->iscollection = TRUE;
@@ -266,6 +267,7 @@
             printf("Lock-Token: %s\n", lock->token);
         }
         locked = TRUE;
+        locktokenfile = create_locktoken_file(dir->name, lock->token);
     }
     
     int ret = 0;
@@ -275,6 +277,8 @@
         if(locked) {
             if(dav_unlock(root)) {
                 print_resource_error(sn, "/");
+            } else {
+                locked = FALSE;
             }
         }
         
@@ -289,10 +293,17 @@
         if(locked) {
             if(dav_unlock(root)) {
                 print_resource_error(sn, "/");
+            } else {
+                locked = FALSE;
             }
         }
         // TODO: free
         dav_session_destroy(sn);
+        
+        if(!locked && locktokenfile) {
+            remove(locktokenfile);
+        }
+        
         return -1;
     }
     
@@ -387,6 +398,8 @@
         if(dav_unlock(root)) {
             print_resource_error(sn, "/");
             ret = -1;
+        } else {
+            locked = FALSE;
         }
     }
     
@@ -399,6 +412,10 @@
     // cleanup
     dav_session_destroy(sn);
     
+    if(!locked && locktokenfile) {
+        remove(locktokenfile);
+    }
+    
     // Report
     if(ret != -2) {
         char *str_success = sync_success == 1 ? "file" : "files";
@@ -772,6 +789,7 @@
     
     // lock repository
     DavBool locked = FALSE;
+    char *locktokenfile = NULL;
     if((dir->lockpush || cmd_getoption(a, "lock")) && !cmd_getoption(a, "nolock")) {
         if(dav_lock(root)) {
             print_resource_error(sn, "/");
@@ -784,6 +802,7 @@
             printf("Lock-Token: %s\n", lock->token);
         }
         locked = TRUE;
+        locktokenfile = create_locktoken_file(dir->name, lock->token);
     }
     
     int sync_success = 0;
@@ -884,6 +903,8 @@
         if(dav_unlock(root)) {
             print_resource_error(sn, "/");
             ret = -1;
+        } else {
+            locked = FALSE;
         }
     }
     
@@ -894,6 +915,10 @@
     }
     
     // cleanup
+    if(!locked && locktokenfile) {
+        remove(locktokenfile);
+    }
+    
     dav_session_destroy(sn);
     while(resources) {
         UcxList *next = resources->next;
@@ -1738,3 +1763,20 @@
     
     return ret;
 }
+
+char* create_locktoken_file(const char *syncdirname, const char *locktoken) {
+    sstr_t fname = ucx_sprintf("locktoken-%s.txt", syncdirname);
+    char *path = config_file_path(fname.ptr);
+    free(fname.ptr);
+    
+    FILE *file = fopen(path, "w");
+    if(!file) {
+        perror("Cannot create locktoken file");
+        free(path);
+    }
+    
+    fprintf(file, "%s\n", locktoken);
+    fclose(file);
+    return path;
+}
+
--- a/dav/sync.h	Tue Sep 05 12:11:09 2017 +0200
+++ b/dav/sync.h	Tue Sep 05 13:07:29 2017 +0200
@@ -90,6 +90,8 @@
 int cmd_list_dirs();
 int cmd_check_repositories();
 
+char* create_locktoken_file(const char *syncdirname, const char *locktoken);
+
 #ifdef	__cplusplus
 }
 #endif

mercurial