improved dav-sync error handling on startup

Tue, 22 Mar 2016 13:05:09 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Tue, 22 Mar 2016 13:05:09 +0100
changeset 233
d8042cede66c
parent 232
df3fa8637a58
child 234
33f373b28c8c

improved dav-sync error handling on startup

dav/scfg.c file | annotate | diff | comparison | revisions
dav/scfg.h file | annotate | diff | comparison | revisions
dav/sync.c file | annotate | diff | comparison | revisions
--- a/dav/scfg.c	Tue Mar 22 11:51:48 2016 +0100
+++ b/dav/scfg.c	Tue Mar 22 13:05:09 2016 +0100
@@ -231,14 +231,6 @@
             dir->trash = util_concat_path(t, "/");
             free(t);
         }
-        
-        // create trash directory
-        mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
-        if (util_mkdir(dir->trash, mode)) {
-            if (errno != EEXIST) {
-                fprintf(stderr, "Cannot create trash directory: %s\n", dir->trash);
-            }
-        }
     } else {
         dir->trash = NULL;
     }
@@ -252,6 +244,34 @@
     return ucx_map_cstr_get(directories, name);
 }
 
+int scfg_check_dir(SyncDirectory *dir) {
+    struct stat s;
+    if(stat(dir->path, &s)) {
+        int err = errno;
+        if(err == ENOENT) {
+            fprintf(stderr, "directory %s does not exist.\n", dir->path);
+        } else {
+            fprintf(stderr, "Cannot stat directory %s.\n", dir->path);
+            perror(NULL);
+        }
+        fprintf(stderr, "Abort.\n");
+        return -1;
+    }
+    
+    if(dir->trash) {
+        // create trash directory
+        mode_t mode = S_IRWXU | S_IRGRP | S_IXGRP | S_IROTH | S_IXOTH;
+        if (util_mkdir(dir->trash, mode)) {
+            if (errno != EEXIST) {
+                fprintf(stderr, "Cannot create trash directory: %s\nAbort.\n", dir->trash);
+                return -1;
+            }
+        }
+    }
+    
+    return 0;
+}
+
 char* scfg_create_path(char *cfg) {
     if(!cfg) {
         return NULL;
--- a/dav/scfg.h	Tue Mar 22 11:51:48 2016 +0100
+++ b/dav/scfg.h	Tue Mar 22 13:05:09 2016 +0100
@@ -61,6 +61,8 @@
 
 SyncDirectory* scfg_get_dir(char *name);
 
+int scfg_check_dir(SyncDirectory *dir);
+
 char* scfg_create_path(char *cfg);
 
 int add_directory(SyncDirectory *dir);
--- a/dav/sync.c	Tue Mar 22 11:51:48 2016 +0100
+++ b/dav/sync.c	Tue Mar 22 13:05:09 2016 +0100
@@ -50,6 +50,7 @@
 
 #include "sync.h"
 #include "ucx/properties.h"
+#include "libidav/session.h"
 
 static DavContext *ctx;
 
@@ -209,6 +210,9 @@
         fprintf(stderr, "Unknown sync dir: %s\n", a->argv[0]);
         return -1;
     }
+    if(scfg_check_dir(dir)) {
+        return -1;
+    }
     
     Repository *repo = get_repository(sstr(dir->repository));
     if(!repo) {
@@ -249,6 +253,10 @@
             fprintf(stderr, "Abort\n");
             return -1;
         }
+        DavLock *lock = dav_get_lock(sn, "/");
+        if(lock) {
+            printf("Lock-Token: %s\n", lock->token);
+        }
         locked = TRUE;
     }
     
@@ -692,6 +700,9 @@
         fprintf(stderr, "Unknown sync dir: %s\n", a->argv[0]);
         return -1;
     }
+    if(scfg_check_dir(dir)) {
+        return -1;
+    }
     
     Repository *repo = get_repository(sstr(dir->repository));
     if(!repo) {
@@ -739,6 +750,10 @@
             fprintf(stderr, "Abort\n");
             return -1;
         }
+        DavLock *lock = dav_get_lock(sn, "/");
+        if(lock) {
+            printf("Lock-Token: %s\n", lock->token);
+        }
         locked = TRUE;
     }
     
@@ -1246,6 +1261,9 @@
         fprintf(stderr, "Unknown sync dir: %s\n", a->argv[0]);
         return -1;
     }
+    if(scfg_check_dir(dir)) {
+        return -1;
+    }
     
     SyncDatabase *db = load_db(dir->database);
     if(!db) {
@@ -1290,6 +1308,9 @@
         fprintf(stderr, "Unknown sync dir: %s\n", a->argv[0]);
         return -1;
     }
+    if(scfg_check_dir(dir)) {
+        return -1;
+    }
     
     SyncDatabase *db = load_db(dir->database);
     if(!db) {
@@ -1421,6 +1442,9 @@
         fprintf(stderr, "Unknown sync dir: %s\n", a->argv[0]);
         return -1;
     }
+    if(scfg_check_dir(syncdir)) {
+        return -1;
+    }
     
     if(!syncdir->trash) {
         printf("trash not configured for %s\n", syncdir->name);

mercurial