dav/libxattr.c

changeset 361
b6f2462ee055
child 364
3769ba002fd1
equal deleted inserted replaced
355:5da2cf15eb44 361:b6f2462ee055
1 #define _GNU_SOURCE
2
3 #include <stdio.h>
4 #include <stdlib.h>
5
6 #include "libxattr.h"
7
8 #include <errno.h>
9 #include <sys/types.h>
10
11 #include <string.h>
12
13 #define LIST_BUF_LEN 1024
14 #define LIST_ARRAY_LEN 8
15 #define ATTR_BUF_LEN 1024
16
17 #define ARRAY_ADD(array, pos, len, obj) if(pos >= len) { \
18 len *= 2; \
19 array = realloc(array, len * sizeof(char*)); \
20 } \
21 array[pos] = obj; \
22 pos++;
23
24
25 #ifdef __linux__
26 #define XATTR_SUPPORTED
27 #include <sys/xattr.h>
28
29 static char ** parse_xattrlist(char *buf, ssize_t length, ssize_t *nelm) {
30 size_t arraylen = LIST_ARRAY_LEN;
31 size_t arraypos = 0;
32 char **array = malloc(LIST_ARRAY_LEN * sizeof(char*));
33
34 char *begin = buf;
35 char *name = NULL;
36 for(int i=0;i<length;i++) {
37 if(!name && buf[i] == '.') {
38 int nslen = (buf+i-begin);
39 //printf("%.*s\n", nslen, begin);
40 name = buf + i + 1;
41 }
42 if(buf[i] == '\0') {
43 char *attrname = strdup(name);
44 ARRAY_ADD(array, arraypos, arraylen, attrname);
45 begin = buf + i + 1;
46 name = 0;
47 }
48 }
49
50 if(arraypos == 0) {
51 free(array);
52 array = NULL;
53 }
54
55 *nelm = arraypos;
56 return array;
57 }
58
59 char ** xattr_list(const char *path, ssize_t *nelm) {
60 char *list = malloc(LIST_BUF_LEN);
61 ssize_t len = listxattr(path, list, LIST_BUF_LEN);
62 if(len == -1) {
63 switch(errno) {
64 case ERANGE: {
65 // buffer too, get size of attribute list
66 ssize_t newlen = listxattr(path, NULL, 0);
67 if(newlen > 0) {
68 // second try
69 list = realloc(list, newlen);
70 len = listxattr(path, list, newlen);
71 if(len != -1) {
72 // this time it worked
73 break;
74 }
75 }
76 }
77 default: {
78 free(list);
79 *nelm = -1;
80 return NULL;
81 }
82 }
83 }
84
85 char **ret = parse_xattrlist(list, len, nelm);
86 free(list);
87 return ret;
88 }
89
90 static char* name2nsname(const char *name) {
91 // add the 'user' namespace to the name
92 size_t namelen = strlen(name);
93 char *attrname = malloc(8 + namelen);
94 memcpy(attrname, "user.", 5);
95 memcpy(attrname+5, name, namelen + 1);
96 return attrname;
97 }
98
99 char * xattr_get(const char *path, const char *attr, ssize_t *len) {
100 char *attrname = name2nsname(attr);
101
102 char *buf = malloc(ATTR_BUF_LEN);
103 ssize_t vlen = getxattr(path, attrname, buf, ATTR_BUF_LEN);
104 if(vlen < 0) {
105 switch(errno) {
106 case ERANGE: {
107 ssize_t attrlen = getxattr(path, attrname, NULL, 0);
108 if(attrlen > 0) {
109 free(buf);
110 buf = malloc(attrlen);
111 vlen = getxattr(path, attrname, buf, attrlen);
112 if(vlen > 0) {
113 break;
114 }
115 }
116 }
117 default: {
118 *len = -1;
119 free(buf);
120 free(attrname);
121 return NULL;
122 }
123 }
124 }
125
126 free(attrname);
127 *len = vlen;
128 return buf;
129 }
130
131 int xattr_set(const char *path, const char *name, const void *value, size_t len) {
132 char *attrname = name2nsname(name);
133 int ret = setxattr(path, attrname, value, len, 0);
134 free(attrname);
135 return ret;
136 }
137
138 #endif /* Linux */
139
140 #ifdef __APPLE__
141 #define XATTR_SUPPORTED
142 #include <sys/xattr.h>
143
144 static char ** parse_xattrlist(char *buf, ssize_t length, ssize_t *nelm) {
145 size_t arraylen = LIST_ARRAY_LEN;
146 size_t arraypos = 0;
147 char **array = malloc(LIST_ARRAY_LEN * sizeof(char*));
148
149 char *name = buf;
150 for(int i=0;i<length;i++) {
151 if(buf[i] == '\0') {
152 char *attrname = strdup(name);
153 ARRAY_ADD(array, arraypos, arraylen, attrname);
154 name = buf + i + 1;
155 }
156 }
157
158 if(arraypos == 0) {
159 free(array);
160 array = NULL;
161 }
162
163 *nelm = arraypos;
164 return array;
165 }
166
167 char ** xattr_list(const char *path, ssize_t *nelm) {
168 char *list = malloc(LIST_BUF_LEN);
169 ssize_t len = listxattr(path, list, LIST_BUF_LEN, 0);
170 if(len == -1) {
171 switch(errno) {
172 case ERANGE: {
173 // buffer too, get size of attribute list
174 ssize_t newlen = listxattr(path, NULL, 0, 0);
175 if(newlen > 0) {
176 // second try
177 list = realloc(list, newlen);
178 len = listxattr(path, list, newlen, 0);
179 if(len != -1) {
180 // this time it worked
181 break;
182 }
183 }
184 }
185 default: {
186 free(list);
187 *nelm = -1;
188 return NULL;
189 }
190 }
191 }
192
193 char **ret = parse_xattrlist(list, len, nelm);
194 free(list);
195 return ret;
196 }
197
198 char * xattr_get(const char *path, const char *attr, ssize_t *len) {
199 // get attribute length
200 ssize_t attrlen = getxattr(path, attr, NULL, 0, 0, 0);
201 if(attrlen < 0) {
202 *len = -1;
203 return NULL;
204 }
205
206 char *buf = malloc(attrlen);
207 ssize_t vlen = getxattr(path, attr, buf, attrlen, 0, 0);
208 if(vlen < 0) {
209 *len = -1;
210 free(buf);
211 return NULL;
212 }
213
214 *len = vlen;
215 return buf;
216 }
217
218 int xattr_set(const char *path, const char *name, const void *value, size_t len) {
219 int ret = setxattr(path, name, value, len, 0, 0);
220 return ret;
221 }
222
223 #endif /* Apple */
224
225 #ifdef __sun
226 #define XATTR_SUPPORTED
227 #include <unistd.h>
228 #include <sys/types.h>
229 #include <sys/stat.h>
230 #include <dirent.h>
231 #include <fcntl.h>
232
233 static int open_attrfile(const char *path, const char *attr, int oflag) {
234 int file = open(path, O_RDONLY);
235 if(file == -1) {
236 return -1;
237 }
238
239 int attrfile = openat(file, attr, oflag, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
240 close(file);
241 return attrfile;
242 }
243
244 char ** xattr_list(const char *path, ssize_t *nelm) {
245 *nelm = -1;
246
247 int attrdir = open_attrfile(path, ".", O_RDONLY|O_XATTR);
248 if(attrdir == -1) {
249 return NULL;
250 }
251
252 DIR *dir = fdopendir(attrdir);
253 if(!dir) {
254 close(attrdir);
255 return NULL;
256 }
257
258 size_t arraylen = LIST_ARRAY_LEN;
259 size_t arraypos = 0;
260 char **array = malloc(LIST_ARRAY_LEN * sizeof(char*));
261
262 struct dirent *ent;
263 while((ent = readdir(dir)) != NULL) {
264 if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..") || !strcmp(ent->d_name, "SUNWattr_ro") || !strcmp(ent->d_name, "SUNWattr_rw")) {
265 continue;
266 }
267 char *name = strdup(ent->d_name);
268 ARRAY_ADD(array, arraypos, arraylen, name);
269 }
270 closedir(dir);
271
272 *nelm = arraypos;
273 return array;
274 }
275
276 char * xattr_get(const char *path, const char *attr, ssize_t *len) {
277 *len = -1;
278
279 int attrfile = open_attrfile(path, attr, O_RDONLY|O_XATTR);
280 if(attrfile == -1) {
281 return NULL;
282 }
283
284 struct stat s;
285 if(fstat(attrfile, &s)) {
286 close(attrfile);
287 return NULL;
288 }
289
290 size_t bufsize = (size_t)s.st_size;
291 char *buf = malloc(bufsize);
292
293 char *b = buf;
294 size_t cur = 0;
295 while(cur < bufsize) {
296 ssize_t r = read(attrfile, buf + cur, bufsize - cur);
297 if(r <= 0) {
298 break;
299 }
300 cur += r;
301 }
302
303 close(attrfile);
304 if(cur != bufsize) {
305 free(buf);
306 return NULL;
307 }
308
309 *len = (ssize_t)bufsize;
310 return buf;
311 }
312
313 int xattr_set(const char *path, const char *name, const void *value, size_t len) {
314 int attrfile = open_attrfile(path, name, O_CREAT|O_WRONLY|O_XATTR);
315 if(attrfile == -1) {
316 return -1;
317 }
318
319 const char *p = value;
320 size_t remaining = len;
321 while(remaining > 0) {
322 ssize_t w = write(attrfile, p, remaining);
323 if(w <= 0) {
324 break;
325 }
326 p += w;
327 remaining -= w;
328 }
329
330 close(attrfile);
331
332 return remaining > 0 ? -1 : 0;
333 }
334
335 #endif /* Sun */
336
337
338 #ifdef __FreeBSD__
339 #define XATTR_SUPPORTED
340
341 #include <sys/types.h>
342 #include <sys/extattr.h>
343
344 static char ** parse_xattrlist(char *buf, ssize_t length, ssize_t *nelm) {
345 size_t arraylen = LIST_ARRAY_LEN;
346 size_t arraypos = 0;
347 char **array = malloc(LIST_ARRAY_LEN * sizeof(char*));
348
349 char *name = buf;
350 for(int i=0;i<length;i++) {
351 char namelen = buf[i];
352 char *name = buf + i + 1;
353 char *attrname = malloc(namelen + 1);
354 memcpy(attrname, name, namelen);
355 attrname[namelen] = 0;
356 ARRAY_ADD(array, arraypos, arraylen, attrname);
357 i += namelen;
358 }
359
360 if(arraypos == 0) {
361 free(array);
362 array = NULL;
363 }
364
365 *nelm = arraypos;
366 return array;
367 }
368
369 char ** xattr_list(const char *path, ssize_t *nelm) {
370 *nelm = -1;
371 ssize_t lslen = extattr_list_file(path, EXTATTR_NAMESPACE_USER, NULL, 0);
372 if(lslen <= 0) {
373 if(lslen == 0) {
374 *nelm = 0;
375 }
376 return NULL;
377 }
378
379 char *list = malloc(lslen);
380 ssize_t len = extattr_list_file(path, EXTATTR_NAMESPACE_USER, list, lslen);
381 if(len == -1) {
382 free(list);
383 return NULL;
384 }
385
386 char **ret = parse_xattrlist(list, len, nelm);
387 free(list);
388 return ret;
389 }
390
391 char * xattr_get(const char *path, const char *attr, ssize_t *len) {
392 // get attribute length
393 ssize_t attrlen = extattr_get_file(path, EXTATTR_NAMESPACE_USER, attr, NULL, 0);
394 if(attrlen < 0) {
395 *len = -1;
396 return NULL;
397 }
398
399 char *buf = malloc(attrlen);
400 ssize_t vlen = extattr_get_file(path, EXTATTR_NAMESPACE_USER, attr, buf, attrlen);
401 if(vlen < 0) {
402 *len = -1;
403 free(buf);
404 return NULL;
405 }
406
407 *len = vlen;
408 return buf;
409 }
410
411 int xattr_set(const char *path, const char *name, const void *value, size_t len) {
412 int ret = extattr_set_file(path, EXTATTR_NAMESPACE_USER, name, value, len);
413 return ret;
414 }
415
416 #endif /* FreeBSD */
417
418
419 #ifndef XATTR_SUPPORTED
420
421 char ** xattr_list(const char *path, ssize_t *nelm) {
422 *nelm = -1;
423 return NULL;
424 }
425
426 char * xattr_get(const char *path, const char *attr, ssize_t *len) {
427 *len = -1;
428 return NULL;
429 }
430
431 int xattr_set(const char *path, const char *name, const void *value, size_t len) {
432 return -1;
433 }
434
435 #endif /* unsupported platform */
436
437 void xattr_free_list(char **attrnames, ssize_t nelm) {
438 if(attrnames) {
439 for(int i=0;i<nelm;i++) {
440 free(attrnames[i]);
441 }
442 free(attrnames);
443 }
444 }

mercurial