dav/scfg.c

Mon, 25 Jan 2016 10:48:58 +0100

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Mon, 25 Jan 2016 10:48:58 +0100
changeset 185
cd42cccee550
parent 145
82475dc12dd4
child 198
44054c452de1
permissions
-rw-r--r--

updated copyright notice

/*
 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
 *
 * Copyright 2016 Olaf Wintermann. All rights reserved.
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions are met:
 *
 *   1. Redistributions of source code must retain the above copyright
 *      notice, this list of conditions and the following disclaimer.
 *
 *   2. Redistributions in binary form must reproduce the above copyright
 *      notice, this list of conditions and the following disclaimer in the
 *      documentation and/or other materials provided with the distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <errno.h>
#include <libidav/utils.h>
#include <ucx/map.h>

#include "scfg.h"

#define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b)

#ifdef _WIN32
#define ENV_HOME getenv("USERPROFILE")
#else
#define ENV_HOME getenv("HOME")
#endif /* _WIN32 */

static UcxMap *directories;

int load_sync_config() {
    char *file = util_concat_path(ENV_HOME, ".dav/sync.xml");
    xmlDoc *doc = xmlReadFile(file, NULL, 0);
    if(!doc) {
        fprintf(stderr, "Missing or broken configuration file\n");
        scfg_print_example();
        return -1;
    }
    
    directories = ucx_map_new(8);
    
    int ret = 0;
    xmlNode *node = xmlDocGetRootElement(doc)->children;
    while(node) {
        if(node->type == XML_ELEMENT_NODE) {
            if(xstreq(node->name, "directory")) {
                if(scfg_load_directory(node)) {
                    ret = 1;
                    ucx_map_free(directories);
                    break;
                }
            }
        }
        node = node->next;
    }
    
    xmlFreeDoc(doc);
    
    return ret;
}

static UcxList* add_regex_pattern(UcxList *list, char *value) {
    regex_t *regex = malloc(sizeof(regex_t));
    if (regcomp(regex, value, REG_EXTENDED|REG_NOSUB)) {
        fprintf(stderr, "Invalid regular expression (%s) ... skipped\n", value);
        return list;
    } else {
        return ucx_list_append(list, regex);
    }
}

int scfg_load_directory(xmlNode *node) {
    char *name = NULL;
    char *path = NULL;
    char *trash = NULL;
    char *collection = NULL;
    char *repository = NULL;
    char *database = NULL;
    UcxList *include = NULL;
    UcxList *exclude = NULL;
    int max_retry = DAV_MAX_RETRY;
    bool backuppull = false;
    
    node = node->children;
    while(node) {
        if(node->type == XML_ELEMENT_NODE) {
            char *value = util_xml_get_text(node);
            if(!value) {
                // next
            } else if(xstreq(node->name, "name")) {
                name = value;
            } else if(xstreq(node->name, "path")) {
                path = value;
            } else if(xstreq(node->name, "trash")) {
                trash = value;
            } else if(xstreq(node->name, "collection")) {
                collection = value;
            } else if(xstreq(node->name, "repository")) {
                repository = value;
            } else if(xstreq(node->name, "database")) {
                database = value;
            } else if(xstreq(node->name, "include")) {
                include = add_regex_pattern(include, value);
            } else if(xstreq(node->name, "exclude")) {
                exclude = add_regex_pattern(exclude, value);
            } else if(xstreq(node->name, "max-retry")) {
                int64_t i;
                if(util_strtoint(value, &i)) {
                    max_retry = (int)i;
                } else {
                    fprintf(stderr, "Warnung: sync.xml: integer value "
                                    "expected in <max-retry> element\n");
                }
            } else if(xstreq(node->name, "backup-on-pull")) {
                backuppull = util_getboolean(value);
            }
        }
        node = node->next;
    }
    
    if(!name) {
        fprintf(stderr, "Missing name element for directory\n");
        return -1;
    }
    if(!path) {
        fprintf(stderr, "Missing path element for directory\n");
        return -1;
    }
    if(!repository) {
        fprintf(stderr, "Missing repository element for directory\n");
        return -1;
    }
    if(!database) {
        fprintf(stderr, "Missing database element for directory\n");
        return -1;
    }
    
    SyncDirectory *dir = malloc(sizeof(SyncDirectory));
    dir->name = strdup(name);
    dir->path = strdup(path);
    dir->collection = collection ? strdup(collection) : NULL;
    dir->repository = strdup(repository);
    dir->database = strdup(database);
    dir->max_retry = max_retry;
    dir->backuppull = backuppull;
    if (include) {
        dir->include = include;
    } else {
        regex_t *matchall = malloc(sizeof(regex_t));
        regcomp(matchall, ".*", REG_NOSUB);
        dir->include = ucx_list_append(NULL, matchall);
    }
    if (exclude) {
        dir->exclude = exclude;
    } else {
        regex_t *matchnothing = malloc(sizeof(regex_t));
        regcomp(matchnothing, "///", REG_NOSUB);
        dir->exclude = ucx_list_append(NULL, matchnothing);
    }
    
    if (trash && sstrtrim(sstr(trash)).length > 0) {
        if (trash[0] == '/') {
            dir->trash = strdup(trash);
        } else {
            char *t = util_concat_path(path, trash);
            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;
    }
    
    ucx_map_cstr_put(directories, name, dir);
    
    return 0;
}

void scfg_print_example() {
    fprintf(stderr, 
        "example sync.xml\n\n"
        "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"
        "<configuration>\n"
        "  <directory>\n"
        "    <!-- identifier -->\n"
        "    <name>documents</name>\n\n"
        "    <!-- local path to the directory -->\n"
        "    <path>/home/user/Documents</path>\n\n"
        "    <!-- optional trash path -->\n"
        "    <trash>.trash</trash>"
        "    <!-- repository name specified in config.xml -->\n"
        "    <repository>server</repository>\n\n"
        "    <!-- collection to synchronize (optional) -->\n"
        "    <collection>somecol</collection>\n\n"
        "    <!-- database file name -->\n"
        "    <database>documents-db.xml</database>\n"
        "  </directory>\n"
        "</configuration>\n");
}

SyncDirectory* scfg_get_dir(char *name) {
    return ucx_map_cstr_get(directories, name);
}

mercurial