30 |
30 |
31 #include <stdio.h> |
31 #include <stdio.h> |
32 #include <stdlib.h> |
32 #include <stdlib.h> |
33 #include <string.h> |
33 #include <string.h> |
34 #include <unistd.h> |
34 #include <unistd.h> |
|
35 #include <errno.h> |
35 #include <sys/stat.h> |
36 #include <sys/stat.h> |
36 |
37 |
37 #include <ucx/string.h> |
38 #include <ucx/string.h> |
|
39 #include <ucx/list.h> |
38 #include <libidav/crypto.h> |
40 #include <libidav/crypto.h> |
39 #include <libidav/utils.h> |
41 #include <libidav/utils.h> |
40 |
42 |
41 #include "libxattr.h" |
43 #include "libxattr.h" |
42 |
44 |
43 uint32_t parse_finfo(const char *str, char **error) { |
45 uint32_t parse_finfo_settings(const char *str, char **error) { |
44 scstr_t s = scstr(str); |
46 scstr_t s = scstr(str); |
45 |
47 |
46 if(!sstrcmp(s, SC("*")) || !sstrcmp(s, SC("a")) || !sstrcmp(s, SC("all"))) { |
48 if(!sstrcmp(s, SC("*")) || !sstrcmp(s, SC("a")) || !sstrcmp(s, SC("all"))) { |
47 return FINFO_DATE|FINFO_OWNER|FINFO_MODE; |
49 return FINFO_DATE|FINFO_OWNER|FINFO_MODE; |
48 } |
50 } |
123 |
125 |
124 |
126 |
125 dav_set_property(res, "idav:finfo", content);; |
127 dav_set_property(res, "idav:finfo", content);; |
126 |
128 |
127 return 0; |
129 return 0; |
|
130 } |
|
131 |
|
132 XAttributes* xml_get_attributes(DavXmlNode *xml) { |
|
133 UcxList *names = NULL; |
|
134 UcxList *values = NULL; |
|
135 |
|
136 size_t count = 0; |
|
137 |
|
138 char *hash = NULL; |
|
139 |
|
140 DavXmlNode *node = xml; |
|
141 while(node) { |
|
142 if(node->type == DAV_XML_ELEMENT) { |
|
143 if(!strcmp(node->name, "hash")) { |
|
144 hash = dav_xml_getstring(node->children); |
|
145 } else if(!strcmp(node->name, "xattr")) { |
|
146 char *xattr_name = dav_xml_get_attr(node, "name"); |
|
147 if(xattr_name) { |
|
148 names = ucx_list_append(names, strdup(xattr_name)); |
|
149 |
|
150 char *text = dav_xml_getstring(node->children); |
|
151 if(!text) { |
|
152 text = ""; |
|
153 } |
|
154 |
|
155 int len = 0; |
|
156 char *val = util_base64decode_len(text, &len); |
|
157 |
|
158 sstr_t *value = malloc(sizeof(sstr_t)); |
|
159 value->ptr = val; |
|
160 value->length = len; |
|
161 |
|
162 values = ucx_list_append(values, value); |
|
163 |
|
164 count++; |
|
165 } |
|
166 } |
|
167 } |
|
168 |
|
169 node = node->next; |
|
170 } |
|
171 |
|
172 XAttributes *attributes = NULL; |
|
173 if(count > 0) { |
|
174 attributes = calloc(1, sizeof(XAttributes)); |
|
175 attributes->hash = hash ? strdup(hash) : NULL; |
|
176 attributes->nattr = count; |
|
177 int i=0; |
|
178 UCX_FOREACH(elm, names) { |
|
179 attributes->names[i] = elm->data; |
|
180 i++; |
|
181 } |
|
182 i=0; |
|
183 UCX_FOREACH(elm, values) { |
|
184 attributes->values[i] = *(sstr_t*)elm->data; |
|
185 i++; |
|
186 } |
|
187 } |
|
188 return attributes; |
128 } |
189 } |
129 |
190 |
130 XAttributes* file_get_attributes(const char *path) { |
191 XAttributes* file_get_attributes(const char *path) { |
131 ssize_t nelm = 0; |
192 ssize_t nelm = 0; |
132 char **attributes = xattr_list(path, &nelm); |
193 char **attributes = xattr_list(path, &nelm); |
210 free(xattr->names[i]); |
271 free(xattr->names[i]); |
211 free(xattr->values[i].ptr); |
272 free(xattr->values[i].ptr); |
212 } |
273 } |
213 free(xattr); |
274 free(xattr); |
214 } |
275 } |
|
276 |
|
277 char* get_xattr_hash(DavXmlNode *finfo) { |
|
278 DavXmlNode *node = finfo; |
|
279 while(node) { |
|
280 if(node->type == DAV_XML_ELEMENT && !strcmp(node->name, "hash")) { |
|
281 return dav_xml_getstring(node->children); |
|
282 } |
|
283 node = node->next; |
|
284 } |
|
285 return NULL; |
|
286 } |
|
287 |
|
288 void finfo_get_values(DavXmlNode *xml, FileInfo *outval) { |
|
289 memset(outval, 0, sizeof(FileInfo)); |
|
290 DavXmlNode *node = xml; |
|
291 while(node) { |
|
292 if(node->type == DAV_XML_ELEMENT) { |
|
293 if(!strcmp(node->name, "mtime")) { |
|
294 char *mtime = dav_xml_getstring(node->children); |
|
295 if(mtime) { |
|
296 outval->last_modified = util_parse_lastmodified(mtime); |
|
297 outval->date_set = TRUE; |
|
298 } |
|
299 } else if(!strcmp(node->name, "mode")) { |
|
300 char *mode_str = dav_xml_getstring(node->children); |
|
301 if(mode_str) { |
|
302 char *end; |
|
303 errno = 0; |
|
304 long int mode = strtol(mode_str, &end, 8); |
|
305 if(errno == 0) { |
|
306 mode &= 07777; |
|
307 outval->mode = mode; |
|
308 outval->mode_set = TRUE; |
|
309 } |
|
310 } |
|
311 } |
|
312 } |
|
313 node = node->next; |
|
314 } |
|
315 |
|
316 } |