dav/finfo.c

changeset 520
da2b0cc44e4f
parent 519
ac5ac55b1b2e
child 522
46f96dcd6eab
equal deleted inserted replaced
519:ac5ac55b1b2e 520:da2b0cc44e4f
28 28
29 #include "finfo.h" 29 #include "finfo.h"
30 30
31 #include <stdio.h> 31 #include <stdio.h>
32 #include <stdlib.h> 32 #include <stdlib.h>
33 #include <string.h>
33 #include <unistd.h> 34 #include <unistd.h>
34 #include <sys/stat.h> 35 #include <sys/stat.h>
35 36
36 #include <ucx/string.h> 37 #include <ucx/string.h>
38 #include <libidav/crypto.h>
39 #include <libidav/utils.h>
40
41 #include "libxattr.h"
37 42
38 uint32_t parse_finfo(const char *str) { 43 uint32_t parse_finfo(const char *str) {
39 scstr_t s = scstr(str); 44 scstr_t s = scstr(str);
40 45
41 if(!sstrcmp(s, SC("*")) || !sstrcmp(s, SC("a")) || !sstrcmp(s, SC("all"))) { 46 if(!sstrcmp(s, SC("*")) || !sstrcmp(s, SC("a")) || !sstrcmp(s, SC("all"))) {
52 finfo |= FINFO_DATE; 57 finfo |= FINFO_DATE;
53 } else if(!sstrcasecmp(f, SC("owner"))) { 58 } else if(!sstrcasecmp(f, SC("owner"))) {
54 finfo |= FINFO_OWNER; 59 finfo |= FINFO_OWNER;
55 } else if(!sstrcasecmp(f, SC("mode"))) { 60 } else if(!sstrcasecmp(f, SC("mode"))) {
56 finfo |= FINFO_MODE; 61 finfo |= FINFO_MODE;
62 } else if(!sstrcasecmp(f, SC("xattr"))) {
63 finfo |= FINFO_XATTR;
57 } 64 }
58 free(f.ptr); 65 free(f.ptr);
59 } 66 }
60 67
61 free(fs); 68 free(fs);
112 119
113 dav_set_property(res, "idav:finfo", content);; 120 dav_set_property(res, "idav:finfo", content);;
114 121
115 return 0; 122 return 0;
116 } 123 }
124
125 XAttributes* file_get_attributes(const char *path) {
126 ssize_t nelm = 0;
127 char **attributes = xattr_list(path, &nelm);
128 if(nelm <= 0) {
129 return NULL;
130 }
131
132 XAttributes *xattr = malloc(sizeof(XAttributes));
133 xattr->nattr = 0;
134 xattr->names = calloc(nelm, sizeof(char*));
135 xattr->values = calloc(nelm, sizeof(sstr_t));
136
137 DAV_SHA_CTX *sha256 = dav_hash_init();
138
139 size_t nattr = 0;
140 for(int i=0;i<nelm;i++) {
141 ssize_t valuelen = 0;
142 char *value = xattr_get(path, attributes[i], &valuelen);
143 if(valuelen >= 0) {
144 dav_hash_update(sha256, attributes[i], strlen(attributes[i]));
145 dav_hash_update(sha256, value, valuelen);
146 // add name and value
147 xattr->names[nattr] = attributes[i];
148 sstr_t v;
149 v.ptr = value;
150 v.length = valuelen;
151 xattr->values[nattr] = v;
152 nattr++;
153 } else {
154 // discard not readable attributes
155 free(attributes[i]);
156 }
157 }
158
159 xattr->nattr = nattr;
160
161 unsigned char hash[DAV_SHA256_DIGEST_LENGTH];
162 dav_hash_final(sha256, hash);
163 xattr->hash = util_hexstr(hash, DAV_SHA256_DIGEST_LENGTH);
164
165 free(attributes);
166
167 return xattr;
168 }
169
170 int resource_set_xattr(DavResource *res, XAttributes *xattr) {
171 if(!xattr || xattr->nattr == 0) {
172 return 0;
173 }
174
175 DavXmlNode *content = dav_xml_createnode_with_text(DAV_NS, "hash", xattr->hash);
176 DavXmlNode *last = content;
177
178 for(int i=0;i<xattr->nattr;i++) {
179 DavXmlNode *attr = dav_xml_createnode(DAV_NS, "xattr");
180 dav_xml_add_attr(attr, "name", xattr->names[i]);
181 last->next = attr;
182 last = attr;
183
184 sstr_t value = xattr->values[i];
185 if(value.length > 0) {
186 char *encval = util_base64encode(value.ptr, value.length);
187 attr->children = dav_xml_createtextnode(encval);
188 free(encval);
189 }
190 }
191
192 dav_set_property(res, "idav:xattributes", content);
193
194 return 0;
195 }
196
197 void xattributes_free(XAttributes *xattr) {
198
199 }

mercurial