|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2013 Olaf Wintermann. All rights reserved. |
|
5 * |
|
6 * Redistribution and use in source and binary forms, with or without |
|
7 * modification, are permitted provided that the following conditions are met: |
|
8 * |
|
9 * 1. Redistributions of source code must retain the above copyright |
|
10 * notice, this list of conditions and the following disclaimer. |
|
11 * |
|
12 * 2. Redistributions in binary form must reproduce the above copyright |
|
13 * notice, this list of conditions and the following disclaimer in the |
|
14 * documentation and/or other materials provided with the distribution. |
|
15 * |
|
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" |
|
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE |
|
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE |
|
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE |
|
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR |
|
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF |
|
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS |
|
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN |
|
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) |
|
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE |
|
26 * POSSIBILITY OF SUCH DAMAGE. |
|
27 */ |
|
28 |
|
29 #include <stdio.h> |
|
30 #include <stdlib.h> |
|
31 #include <string.h> |
|
32 |
|
33 #include <libidav/utils.h> |
|
34 #include <ucx/map.h> |
|
35 |
|
36 #include "scfg.h" |
|
37 |
|
38 #define xstreq(a,b) xmlStrEqual(BAD_CAST a, BAD_CAST b) |
|
39 |
|
40 #ifdef _WIN32 |
|
41 #define ENV_HOME getenv("USERPROFILE") |
|
42 #else |
|
43 #define ENV_HOME getenv("HOME") |
|
44 #endif /* _WIN32 */ |
|
45 |
|
46 static UcxMap *directories; |
|
47 |
|
48 int load_sync_config() { |
|
49 char *file = util_concat_path(ENV_HOME, ".dav/sync.xml"); |
|
50 xmlDoc *doc = xmlReadFile(file, NULL, 0); |
|
51 if(!doc) { |
|
52 fprintf(stderr, "Missing configuration file\n"); |
|
53 scfg_print_example(); |
|
54 return -1; |
|
55 } |
|
56 |
|
57 directories = ucx_map_new(8); |
|
58 |
|
59 int ret = 0; |
|
60 xmlNode *node = xmlDocGetRootElement(doc)->children; |
|
61 while(node) { |
|
62 if(node->type == XML_ELEMENT_NODE) { |
|
63 if(xstreq(node->name, "directory")) { |
|
64 if(scfg_load_directory(node)) { |
|
65 ret = 1; |
|
66 ucx_map_free(directories); |
|
67 break; |
|
68 } |
|
69 } |
|
70 } |
|
71 node = node->next; |
|
72 } |
|
73 |
|
74 xmlFreeDoc(doc); |
|
75 |
|
76 return ret; |
|
77 } |
|
78 |
|
79 int scfg_load_directory(xmlNode *node) { |
|
80 char *name = NULL; |
|
81 char *path = NULL; |
|
82 char *repository = NULL; |
|
83 char *database = NULL; |
|
84 |
|
85 node = node->children; |
|
86 while(node) { |
|
87 if(node->type == XML_ELEMENT_NODE) { |
|
88 char *value = util_xml_get_text(node); |
|
89 if(!value) { |
|
90 // next |
|
91 } else if(xstreq(node->name, "name")) { |
|
92 name = value; |
|
93 } else if(xstreq(node->name, "path")) { |
|
94 path = value; |
|
95 } else if(xstreq(node->name, "repository")) { |
|
96 repository = value; |
|
97 } else if(xstreq(node->name, "database")) { |
|
98 database = value; |
|
99 } |
|
100 } |
|
101 node = node->next; |
|
102 } |
|
103 |
|
104 if(!name) { |
|
105 fprintf(stderr, "Missing name element for directory\n"); |
|
106 return -1; |
|
107 } |
|
108 if(!path) { |
|
109 fprintf(stderr, "Missing path element for directory\n"); |
|
110 return -1; |
|
111 } |
|
112 if(!repository) { |
|
113 fprintf(stderr, "Missing repository element for directory\n"); |
|
114 return -1; |
|
115 } |
|
116 if(!database) { |
|
117 fprintf(stderr, "Missing database element for directory\n"); |
|
118 return -1; |
|
119 } |
|
120 |
|
121 SyncDirectory *dir = malloc(sizeof(SyncDirectory)); |
|
122 dir->name = strdup(name); |
|
123 dir->path = strdup(path); |
|
124 dir->repository = strdup(repository); |
|
125 dir->database = strdup(database); |
|
126 |
|
127 ucx_map_cstr_put(directories, name, dir); |
|
128 |
|
129 return 0; |
|
130 } |
|
131 |
|
132 void scfg_print_example() { |
|
133 fprintf(stderr, "example sync.xml\n\n"); |
|
134 fprintf(stderr, "<?xml version=\"1.0\" encoding=\"UTF-8\" ?>\n"); |
|
135 fprintf(stderr, "<configuration>\n"); |
|
136 fprintf(stderr, " <directory>\n"); |
|
137 fprintf(stderr, " <!-- identifier -->\n"); |
|
138 fprintf(stderr, " <name>documents</name>\n\n"); |
|
139 fprintf(stderr, " <!-- local path to the directory -->\n"); |
|
140 fprintf(stderr, " <path>/home/user/Documents</path>\n\n"); |
|
141 fprintf(stderr, " <!-- repository name specified in config.xml -->\n"); |
|
142 fprintf(stderr, " <repository>server</repository>\n\n"); |
|
143 fprintf(stderr, " <!-- database file name -->\n"); |
|
144 fprintf(stderr, " <database>documents.db</database>\n"); |
|
145 fprintf(stderr, " </directory>\n"); |
|
146 fprintf(stderr, "</configuration>\n"); |
|
147 } |
|
148 |
|
149 SyncDirectory* scfg_get_dir(char *name) { |
|
150 return ucx_map_cstr_get(directories, name); |
|
151 } |