implements check-repositories command for dav-sync

Fri, 18 Nov 2016 16:00:15 +0100

author
Mike Becker <universe@uap-core.de>
date
Fri, 18 Nov 2016 16:00:15 +0100
changeset 256
54433cb371df
parent 255
bf19378aed58
child 257
49f3c58cc06c

implements check-repositories command for dav-sync

dav/main.c file | annotate | diff | comparison | revisions
dav/scfg.c file | annotate | diff | comparison | revisions
dav/scfg.h file | annotate | diff | comparison | revisions
dav/sync.c file | annotate | diff | comparison | revisions
dav/sync.h file | annotate | diff | comparison | revisions
ucx/list.c file | annotate | diff | comparison | revisions
ucx/list.h file | annotate | diff | comparison | revisions
--- a/dav/main.c	Fri Nov 18 15:27:45 2016 +0100
+++ b/dav/main.c	Fri Nov 18 16:00:15 2016 +0100
@@ -130,9 +130,11 @@
             ret = cmd_unlock(args);
         }  else if(!strcasecmp(cmd, "info")) {
             ret = cmd_info(args);
-        } else if(!strcasecmp(cmd, "add-repository")) {
+        } else if(!strcasecmp(cmd, "add-repository")
+                || !strcasecmp(cmd, "add-repo")) {
             ret = cmd_add_repository(args);
-        } else if(!strcasecmp(cmd, "list-repositories")) {
+        } else if(!strcasecmp(cmd, "list-repositories")
+                || !strcasecmp(cmd, "list-repos")) {
             ret = list_repositories();
         } else if(!strcasecmp(cmd, "version") || !strcasecmp(cmd, "-version")
                 || !strcasecmp(cmd, "--version")) {
@@ -201,7 +203,7 @@
     fprintf(stderr, "Config commands:\n");
     fprintf(stderr, "        add-repository\n");
     fprintf(stderr, "        list-repositories\n");
-    fprintf(stderr, "        check (or check-config)\n");
+    fprintf(stderr, "        check-config\n");
     fprintf(stderr, "\n");
     fprintf(stderr,
             "Instead of an url you can pass a repository name "
@@ -1349,7 +1351,7 @@
 
 char* stdin2str() {
     UcxBuffer *buf = ucx_buffer_new(NULL, 1024, UCX_BUFFER_AUTOEXTEND);
-    size_t size = ucx_stream_hcopy(stdin, buf, fread, ucx_buffer_write);
+    size_t size = ucx_stream_copy(stdin, buf, fread, ucx_buffer_write);
     if(size == 0) {
         ucx_buffer_free(buf);
         return NULL;
--- a/dav/scfg.c	Fri Nov 18 15:27:45 2016 +0100
+++ b/dav/scfg.c	Fri Nov 18 16:00:15 2016 +0100
@@ -59,6 +59,10 @@
 
 static UcxMap *directories;
 
+UcxMapIterator scfg_directory_iterator() {
+    return ucx_map_iterator(directories);
+}
+
 static int create_default_sync_config(char *file) {
     FILE *out = fopen(file, "w");
     if(!out) {
@@ -74,55 +78,6 @@
     return 0;
 }
 
-int load_sync_config() {
-    directories = ucx_map_new(8);
-    
-    char *file = util_concat_path(ENV_HOME, ".dav/sync.xml");
-    
-    struct stat s;
-    if(stat(file, &s)) {
-        switch(errno) {
-            case ENOENT: {
-                if(create_default_sync_config(file)) {
-                    return 1;
-                }
-                break;
-            }
-            default: {
-                perror("Cannot load sync.xml");
-            }
-        }
-        free(file);
-        return 0;
-    }
-    
-    xmlDoc *doc = xmlReadFile(file, NULL, 0);
-    if(!doc) {
-        fprintf(stderr, "Cannot load sync.xml\n");
-        free(file);
-        return -1;
-    }
-    
-    int ret = 0;
-    xmlNode *node = xmlDocGetRootElement(doc)->children;
-    while(node && !ret) {
-        if(node->type == XML_ELEMENT_NODE) {
-            if(xstreq(node->name, "directory")) {
-                ret = scfg_load_directory(node);
-            } else {
-                print_error(node->line,
-                        "unknown config element: %s\n", node->name);
-                ret = 1;
-            }
-        }
-        node = node->next;
-    }
-    
-    xmlFreeDoc(doc);
-    free(file);
-    return ret;
-}
-
 static UcxList* add_regex_pattern(UcxList *list, char *value,
         unsigned short xmlline) {
     regex_t *regex = malloc(sizeof(regex_t));
@@ -168,7 +123,7 @@
     return 0;
 }
 
-int scfg_load_directory(xmlNode *node) {
+static int scfg_load_directory(xmlNode *node) {
     char *name = NULL;
     char *path = NULL;
     char *trash = NULL;
@@ -304,6 +259,55 @@
     return 0;
 }
 
+int load_sync_config() {
+    directories = ucx_map_new(8);
+    
+    char *file = util_concat_path(ENV_HOME, ".dav/sync.xml");
+    
+    struct stat s;
+    if(stat(file, &s)) {
+        switch(errno) {
+            case ENOENT: {
+                if(create_default_sync_config(file)) {
+                    return 1;
+                }
+                break;
+            }
+            default: {
+                perror("Cannot load sync.xml");
+            }
+        }
+        free(file);
+        return 0;
+    }
+    
+    xmlDoc *doc = xmlReadFile(file, NULL, 0);
+    if(!doc) {
+        fprintf(stderr, "Cannot load sync.xml\n");
+        free(file);
+        return -1;
+    }
+    
+    int ret = 0;
+    xmlNode *node = xmlDocGetRootElement(doc)->children;
+    while(node && !ret) {
+        if(node->type == XML_ELEMENT_NODE) {
+            if(xstreq(node->name, "directory")) {
+                ret = scfg_load_directory(node);
+            } else {
+                print_error(node->line,
+                        "unknown config element: %s\n", node->name);
+                ret = 1;
+            }
+        }
+        node = node->next;
+    }
+    
+    xmlFreeDoc(doc);
+    free(file);
+    return ret;
+}
+
 SyncDirectory* scfg_get_dir(char *name) {
     return ucx_map_cstr_get(directories, name);
 }
@@ -432,15 +436,6 @@
     return ret;
 }
 
-int list_syncdirs() {
-    UcxMapIterator i = ucx_map_iterator(directories);
-    SyncDirectory *dir;
-    UCX_MAP_FOREACH(key, dir, i) {
-        printf("%s\n", dir->name);
-    }
-    return 0;
-}
-
 char* generate_db_name(char *basename) {
     char *dbname = NULL;
     int count = -1;
--- a/dav/scfg.h	Fri Nov 18 15:27:45 2016 +0100
+++ b/dav/scfg.h	Fri Nov 18 16:00:15 2016 +0100
@@ -56,8 +56,7 @@
 
 int load_sync_config();
 
-int scfg_load_directory(xmlNode *node);
-
+UcxMapIterator scfg_directory_iterator();
 SyncDirectory* scfg_get_dir(char *name);
 
 int scfg_check_dir(SyncDirectory *dir);
@@ -65,7 +64,7 @@
 char* scfg_create_path(char *cfg);
 
 int add_directory(SyncDirectory *dir);
-int list_syncdirs();
+
 char* generate_db_name(char *basename);
 
 void free_sync_config();
--- a/dav/sync.c	Fri Nov 18 15:27:45 2016 +0100
+++ b/dav/sync.c	Fri Nov 18 16:00:15 2016 +0100
@@ -36,6 +36,7 @@
 #include <sys/types.h>
 #include <ucx/string.h>
 #include <ucx/utils.h>
+#include <ucx/properties.h>
 #include <dirent.h>
 
 #include <libidav/webdav.h>
@@ -49,7 +50,6 @@
 #include "assistant.h"
 
 #include "sync.h"
-#include "ucx/properties.h"
 #include "libidav/session.h"
 
 static DavContext *ctx;
@@ -109,11 +109,14 @@
             ret = cmd_trash_info(args);
         } else if(!strcmp(cmd, "empty-trash")) {
             ret = cmd_empty_trash(args);
-        } else if(!strcmp(cmd, "add-directory")) {
+        } else if(!strcmp(cmd, "add-dir")
+                || !strcmp(cmd, "add-directory")) {
             ret = cmd_add_directory(args);
-        } else if(!strcmp(cmd, "list-directories")) {
-            ret = list_syncdirs();
-        } else if(!strcmp(cmd, "check-repositories")) {
+        } else if(!strcmp(cmd, "list-dirs")
+                || !strcmp(cmd, "list-directories")) {
+            ret = cmd_list_dirs();
+        } else if(!strcmp(cmd, "check-repos")
+                || !strcmp(cmd, "check-repositories")) {
             ret = cmd_check_repositories();
         } else {
             print_usage(argv[0]);
@@ -154,7 +157,7 @@
     fprintf(stderr, "Config commands:\n");
     fprintf(stderr, "        add-directory\n");
     fprintf(stderr, "        list-directories\n");
-    fprintf(stderr, "        check (or check-config)\n");
+    fprintf(stderr, "        check-config\n");
     fprintf(stderr, "        check-repositories\n\n");
 }
 
@@ -1638,7 +1641,56 @@
     return ret;
 }
 
+int cmd_list_dirs() {
+    UcxMapIterator iter = scfg_directory_iterator();
+    SyncDirectory *dir;
+    UCX_MAP_FOREACH(key, dir, iter) {
+        printf("%s\n", dir->name);
+    }
+    return 0;
+}
+
 int cmd_check_repositories() {
-    fprintf(stderr, "Not implemented.\n");
-    return EXIT_FAILURE;
+    int ret = EXIT_SUCCESS;
+
+    UcxList *reponames = NULL;
+    {
+        UcxMapIterator iter = scfg_directory_iterator();
+        SyncDirectory *dir;
+        UCX_MAP_FOREACH(key, dir, iter) {
+            reponames = ucx_list_append_once(reponames,
+                    dir->repository, ucx_strcmp, NULL);
+        }
+    }
+
+    UCX_FOREACH(listelem, reponames) {
+        char *reponame = listelem->data;
+        printf("Checking %s... ", reponame);
+        Repository* repo = get_repository(sstr(reponame));
+        if (!repo) {
+            printf(" not found in config.xml!\n");
+            ret = EXIT_FAILURE;
+        } else {
+            DavSession *sn = create_session(ctx, repo, repo->url);
+            if (sn) {
+                DavResource *res = dav_query(sn,
+                        "select - from / with depth = 0");
+                if (res) {
+                    printf("OK.\n");
+                    dav_resource_free(res);
+                } else {
+                    printf("unavailable!\n");
+                    ret = EXIT_FAILURE;
+                }
+                dav_session_destroy(sn);
+            } else {
+                printf("cannot create session!\n");
+                ret = EXIT_FAILURE;
+            }
+        }
+    }
+    
+    ucx_list_free(reponames);
+    
+    return ret;
 }
--- a/dav/sync.h	Fri Nov 18 15:27:45 2016 +0100
+++ b/dav/sync.h	Fri Nov 18 16:00:15 2016 +0100
@@ -87,7 +87,7 @@
 int cmd_trash_info(CmdArgs *args);
 int cmd_empty_trash(CmdArgs *args);
 int cmd_add_directory(CmdArgs *args);
-
+int cmd_list_dirs();
 int cmd_check_repositories();
 
 #ifdef	__cplusplus
--- a/ucx/list.c	Fri Nov 18 15:27:45 2016 +0100
+++ b/ucx/list.c	Fri Nov 18 16:00:15 2016 +0100
@@ -159,40 +159,6 @@
     return nl;
 }
 
-UcxList *ucx_list_prepend_once(UcxList *l, void *data,
-        cmp_func cmpfnc, void* cmpdata) {
-    return ucx_list_prepend_once_a(ucx_default_allocator(), l,
-            data, cmpfnc, cmpdata);
-}
-
-UcxList *ucx_list_prepend_once_a(UcxAllocator *alloc, UcxList *l, void *data,
-        cmp_func cmpfnc, void *cmpdata) {
-    
-    if (l) {
-        int found = 0;
-        UcxList *first;
-        {
-            UcxList *e = l;
-            while (e) {
-                found |= (cmpfnc(e->data, data, cmpdata) == 0);
-                first = e;
-                e = e->prev;
-            }
-        }
-
-        if (found) {
-            return first;
-        } else {
-            UcxList *nl = ucx_list_append_a(alloc, NULL, data);
-            nl->next = first;
-            first->prev = nl;
-            return nl;
-        }
-    } else {
-        return ucx_list_append_a(alloc, NULL, data);
-    }
-}
-
 UcxList *ucx_list_concat(UcxList *l1, UcxList *l2) {
     if (l1) {
         UcxList *last = ucx_list_last(l1);
--- a/ucx/list.h	Fri Nov 18 15:27:45 2016 +0100
+++ b/ucx/list.h	Fri Nov 18 16:00:15 2016 +0100
@@ -276,37 +276,6 @@
 UcxList *ucx_list_prepend_a(UcxAllocator *allocator, UcxList *list, void *data);
 
 /**
- * Inserts an element at the beginning of the list, if it is not present
- * in the list.
- * 
- * @param list the list where to insert the data or <code>NULL</code> to create
- * a new list
- * @param data the data to insert
- * @param cmpfnc the compare function
- * @param cmpdata additional data for the compare function
- * @return a pointer to the new list head
- * @see ucx_list_prepend()
- */
-UcxList *ucx_list_prepend_once(UcxList *list, void *data,
-        cmp_func cmpfnc, void *cmpdata);
-
-/**
- * Inserts an element at the beginning of the list, if it is not present in
- * the list, using a UcxAllocator.
- * 
- * @param allocator the allocator to use
- * @param list the list where to insert the data or <code>NULL</code> to create
- * a new list
- * @param data the data to insert
- * @param cmpfnc the compare function
- * @param cmpdata additional data for the compare function
- * @return a pointer to the new list head
- * @see ucx_list_prepend_a()
- */
-UcxList *ucx_list_prepend_once_a(UcxAllocator *allocator,
-        UcxList *list, void *data, cmp_func cmpfnc, void *cmpdata);
-
-/**
  * Concatenates two lists.
  * 
  * Either of the two arguments may be <code>NULL</code>.

mercurial