added support for paths with environment variables in sync.xml

Sun, 20 Mar 2016 15:28:08 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sun, 20 Mar 2016 15:28:08 +0100
changeset 226
acc997e0d0f9
parent 225
a297c2e28fa1
child 227
bf485439222a

added support for paths with environment variables in sync.xml

dav/scfg.c file | annotate | diff | comparison | revisions
dav/scfg.h file | annotate | diff | comparison | revisions
--- a/dav/scfg.c	Sun Mar 20 11:27:31 2016 +0100
+++ b/dav/scfg.c	Sun Mar 20 15:28:08 2016 +0100
@@ -194,7 +194,7 @@
     
     SyncDirectory *dir = malloc(sizeof(SyncDirectory));
     dir->name = strdup(name);
-    dir->path = strdup(path);
+    dir->path = scfg_create_path(path);
     dir->collection = collection ? strdup(collection) : NULL;
     dir->repository = strdup(repository);
     dir->database = strdup(database);
@@ -218,10 +218,16 @@
     }
     
     if (trash && sstrtrim(sstr(trash)).length > 0) {
-        if (trash[0] == '/') {
-            dir->trash = strdup(trash);
+        if (trash[0] == '/' || trash[0] == '$') {
+            dir->trash = scfg_create_path(trash);
         } else {
-            char *t = util_concat_path(path, trash);
+            char *t = util_concat_path(dir->path, trash);
+            dir->trash = util_concat_path(t, "/");
+            free(t);
+        }
+        
+        if(dir->trash[strlen(dir->trash)-1] != '/') {
+            char *t = dir->trash;
             dir->trash = util_concat_path(t, "/");
             free(t);
         }
@@ -246,6 +252,54 @@
     return ucx_map_cstr_get(directories, name);
 }
 
+char* scfg_create_path(char *cfg) {
+    if(!cfg) {
+        return NULL;
+    }
+    if(cfg[0] != '$') {
+        return strdup(cfg);
+    }
+    
+    sstr_t s = sstr(cfg);
+    sstr_t path = sstrchr(sstr(cfg), '/');
+    char *localpath = NULL;
+    if(path.length > 0) {
+        // path = $var/path/
+        
+        sstr_t var = sstrsubsl(s, 1, path.ptr - s.ptr - 1);
+        if(var.length > 0) {
+            char *env = sstrdup(var).ptr;
+            char *envval = getenv(env);
+            free(env);
+            if(envval) {
+                localpath = util_concat_path(envval, path.ptr);
+            } else {
+                fprintf(
+                        stderr,
+                        "Environment variable %.*s not set.\nAbort.\n",
+                        (int)var.length,
+                        var.ptr);
+                exit(-1);
+            }
+        } else {
+            localpath = sstrdup(path).ptr;
+        }
+    } else {
+        // path = $var
+        char *envval = getenv(cfg + 1);
+        if(envval) {
+            localpath = strdup(envval);
+        } else {
+            fprintf(
+                    stderr,
+                    "Environment variable %s not set.\nAbort.\n",
+                    cfg);
+            exit(-1);
+        }
+    }
+    return localpath;
+}
+
 
 int add_directory(SyncDirectory *dir) {
     char *file = util_concat_path(ENV_HOME, ".dav/sync.xml");
--- a/dav/scfg.h	Sun Mar 20 11:27:31 2016 +0100
+++ b/dav/scfg.h	Sun Mar 20 15:28:08 2016 +0100
@@ -61,6 +61,8 @@
 
 SyncDirectory* scfg_get_dir(char *name);
 
+char* scfg_create_path(char *cfg);
+
 int add_directory(SyncDirectory *dir);
 int list_syncdirs();
 char* generate_db_name(char *basename);

mercurial