|
1 /* |
|
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER. |
|
3 * |
|
4 * Copyright 2014 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 "db.h" |
|
34 |
|
35 #include <libidav/utils.h> |
|
36 |
|
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 UcxMap* load_db(char *name) { |
|
47 char *dav_dir = util_concat_path(ENV_HOME, ".dav"); |
|
48 char *db_file = util_concat_path(dav_dir, name); |
|
49 free(dav_dir); |
|
50 |
|
51 xmlTextReaderPtr reader = xmlReaderForFile(db_file, NULL, 0); |
|
52 if(!reader) { |
|
53 fprintf(stderr, "Cannot open database file: %s\n", db_file); |
|
54 free(db_file); |
|
55 return NULL; |
|
56 } |
|
57 free(db_file); |
|
58 |
|
59 UcxMap *map = ucx_map_new(2048); |
|
60 int error = 0; |
|
61 while(xmlTextReaderRead(reader)) { |
|
62 int type = xmlTextReaderNodeType(reader); |
|
63 const xmlChar *name = xmlTextReaderConstName(reader); |
|
64 |
|
65 if(type == XML_READER_TYPE_ELEMENT) { |
|
66 if(xstreq(name, "resource")) { |
|
67 LocalResource *res = process_resource(reader); |
|
68 if(res) { |
|
69 ucx_map_cstr_put(map, res->path, res); |
|
70 } else { |
|
71 error = 1; |
|
72 break; |
|
73 } |
|
74 } |
|
75 } |
|
76 } |
|
77 |
|
78 xmlFreeTextReader(reader); |
|
79 if(error) { |
|
80 // TODO: free resources and map |
|
81 return NULL; |
|
82 } else { |
|
83 return map; |
|
84 } |
|
85 } |
|
86 |
|
87 LocalResource* process_resource(xmlTextReaderPtr reader) { |
|
88 LocalResource *res = calloc(1, sizeof(LocalResource)); |
|
89 |
|
90 int field = -1; |
|
91 while(xmlTextReaderRead(reader)) { |
|
92 int type = xmlTextReaderNodeType(reader); |
|
93 const xmlChar *name = xmlTextReaderConstName(reader); |
|
94 |
|
95 if(type == XML_READER_TYPE_ELEMENT) { |
|
96 if(xstreq(name, "path")) { |
|
97 field = 0; |
|
98 } else if(xstreq(name, "etag")) { |
|
99 field = 1; |
|
100 } else if(xstreq(name, "lastmodified")) { |
|
101 field = 2; |
|
102 } else if(xstreq(name, "size")) { |
|
103 field = 3; |
|
104 } |
|
105 } else if(type == XML_READER_TYPE_TEXT) { |
|
106 const xmlChar *value = xmlTextReaderConstValue(reader); |
|
107 int b = 0; |
|
108 switch(field) { |
|
109 case 0: { |
|
110 res->name = strdup((char*)value); |
|
111 break; |
|
112 } |
|
113 case 1: { |
|
114 res->etag = strdup((char*)value); |
|
115 break; |
|
116 } |
|
117 case 2: { |
|
118 res->last_modified = util_parse_lastmodified((char*)value); |
|
119 break; |
|
120 } |
|
121 case 3: { |
|
122 res->size = atoi((char*)value); |
|
123 break; |
|
124 } |
|
125 } |
|
126 } else if(XML_READER_TYPE_END_ELEMENT) { |
|
127 if(xstreq(name, "resource")) { |
|
128 break; |
|
129 } else { |
|
130 field = -1; |
|
131 } |
|
132 } |
|
133 } |
|
134 |
|
135 if(!res->name || !res->path || res->last_modified == 0) { |
|
136 // TODO: free res |
|
137 return NULL; |
|
138 } else { |
|
139 return res; |
|
140 } |
|
141 } |
|
142 |
|
143 int store_db(UcxMap *db, char *name) { |
|
144 // TODO: |
|
145 |
|
146 } |