dav/libxattr.c

Sat, 15 Sep 2018 11:56:36 +0200

author
Olaf Wintermann <olaf.wintermann@gmail.com>
date
Sat, 15 Sep 2018 11:56:36 +0200
changeset 470
6bf798ad3aec
parent 389
fe855ce911f9
child 764
a41b18c67d95
permissions
-rw-r--r--

adds encrypted password store

new repo config element: <stored-user>
new dav command: add-user

364
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
1 /*
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
2 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS HEADER.
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
3 *
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
4 * Copyright 2018 Olaf Wintermann. All rights reserved.
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
5 *
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
6 * Redistribution and use in source and binary forms, with or without
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
7 * modification, are permitted provided that the following conditions are met:
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
8 *
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
9 * 1. Redistributions of source code must retain the above copyright
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
10 * notice, this list of conditions and the following disclaimer.
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
11 *
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
12 * 2. Redistributions in binary form must reproduce the above copyright
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
13 * notice, this list of conditions and the following disclaimer in the
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
14 * documentation and/or other materials provided with the distribution.
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
15 *
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
16 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
17 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
18 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
19 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT HOLDER OR CONTRIBUTORS BE
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
20 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
21 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
22 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
23 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
24 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
25 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
26 * POSSIBILITY OF SUCH DAMAGE.
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
27 */
3769ba002fd1 updates copyright date
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 361
diff changeset
28
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
29 #define _GNU_SOURCE
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
30
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
31 #include <stdio.h>
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
32 #include <stdlib.h>
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
33
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
34 #include "libxattr.h"
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
35
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
36 #include <errno.h>
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
37 #include <sys/types.h>
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
38
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
39 #include <string.h>
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
40
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
41 #define LIST_BUF_LEN 1024
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
42 #define LIST_ARRAY_LEN 8
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
43 #define ATTR_BUF_LEN 1024
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
44
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
45 #define ARRAY_ADD(array, pos, len, obj) if(pos >= len) { \
389
fe855ce911f9 introduces SyncTagFilter type which for now implements the old tag list filter
Mike Becker <universe@uap-core.de>
parents: 379
diff changeset
46 len *= 2; /* TODO: missing error handling for realloc() */ \
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
47 array = realloc(array, len * sizeof(char*)); \
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
48 } \
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
49 array[pos] = obj; \
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
50 pos++;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
51
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
52
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
53 #ifdef __linux__
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
54 #define XATTR_SUPPORTED
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
55 #include <sys/xattr.h>
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
56
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
57 static char ** parse_xattrlist(char *buf, ssize_t length, ssize_t *nelm) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
58 size_t arraylen = LIST_ARRAY_LEN;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
59 size_t arraypos = 0;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
60 char **array = malloc(LIST_ARRAY_LEN * sizeof(char*));
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
61
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
62 char *begin = buf;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
63 char *name = NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
64 for(int i=0;i<length;i++) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
65 if(!name && buf[i] == '.') {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
66 int nslen = (buf+i-begin);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
67 //printf("%.*s\n", nslen, begin);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
68 name = buf + i + 1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
69 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
70 if(buf[i] == '\0') {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
71 char *attrname = strdup(name);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
72 ARRAY_ADD(array, arraypos, arraylen, attrname);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
73 begin = buf + i + 1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
74 name = 0;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
75 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
76 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
77
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
78 if(arraypos == 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
79 free(array);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
80 array = NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
81 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
82
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
83 *nelm = arraypos;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
84 return array;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
85 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
86
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
87 char ** xattr_list(const char *path, ssize_t *nelm) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
88 char *list = malloc(LIST_BUF_LEN);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
89 ssize_t len = listxattr(path, list, LIST_BUF_LEN);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
90 if(len == -1) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
91 switch(errno) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
92 case ERANGE: {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
93 // buffer too, get size of attribute list
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
94 ssize_t newlen = listxattr(path, NULL, 0);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
95 if(newlen > 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
96 // second try
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
97 list = realloc(list, newlen);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
98 len = listxattr(path, list, newlen);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
99 if(len != -1) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
100 // this time it worked
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
101 break;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
102 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
103 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
104 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
105 default: {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
106 free(list);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
107 *nelm = -1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
108 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
109 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
110 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
111 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
112
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
113 char **ret = parse_xattrlist(list, len, nelm);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
114 free(list);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
115 return ret;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
116 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
117
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
118 static char* name2nsname(const char *name) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
119 // add the 'user' namespace to the name
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
120 size_t namelen = strlen(name);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
121 char *attrname = malloc(8 + namelen);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
122 memcpy(attrname, "user.", 5);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
123 memcpy(attrname+5, name, namelen + 1);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
124 return attrname;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
125 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
126
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
127 char * xattr_get(const char *path, const char *attr, ssize_t *len) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
128 char *attrname = name2nsname(attr);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
129
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
130 char *buf = malloc(ATTR_BUF_LEN);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
131 ssize_t vlen = getxattr(path, attrname, buf, ATTR_BUF_LEN);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
132 if(vlen < 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
133 switch(errno) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
134 case ERANGE: {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
135 ssize_t attrlen = getxattr(path, attrname, NULL, 0);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
136 if(attrlen > 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
137 free(buf);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
138 buf = malloc(attrlen);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
139 vlen = getxattr(path, attrname, buf, attrlen);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
140 if(vlen > 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
141 break;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
142 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
143 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
144 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
145 default: {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
146 *len = -1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
147 free(buf);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
148 free(attrname);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
149 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
150 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
151 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
152 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
153
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
154 free(attrname);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
155 *len = vlen;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
156 return buf;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
157 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
158
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
159 int xattr_set(const char *path, const char *name, const void *value, size_t len) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
160 char *attrname = name2nsname(name);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
161 int ret = setxattr(path, attrname, value, len, 0);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
162 free(attrname);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
163 return ret;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
164 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
165
379
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
166 int xattr_remove(const char *path, const char *name) {
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
167 char *attrname = name2nsname(name);
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
168 int ret = removexattr(path, attrname);
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
169 free(attrname);
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
170 return ret;
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
171 }
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
172
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
173 #endif /* Linux */
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
174
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
175 #ifdef __APPLE__
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
176 #define XATTR_SUPPORTED
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
177 #include <sys/xattr.h>
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
178
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
179 static char ** parse_xattrlist(char *buf, ssize_t length, ssize_t *nelm) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
180 size_t arraylen = LIST_ARRAY_LEN;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
181 size_t arraypos = 0;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
182 char **array = malloc(LIST_ARRAY_LEN * sizeof(char*));
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
183
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
184 char *name = buf;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
185 for(int i=0;i<length;i++) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
186 if(buf[i] == '\0') {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
187 char *attrname = strdup(name);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
188 ARRAY_ADD(array, arraypos, arraylen, attrname);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
189 name = buf + i + 1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
190 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
191 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
192
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
193 if(arraypos == 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
194 free(array);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
195 array = NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
196 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
197
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
198 *nelm = arraypos;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
199 return array;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
200 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
201
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
202 char ** xattr_list(const char *path, ssize_t *nelm) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
203 char *list = malloc(LIST_BUF_LEN);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
204 ssize_t len = listxattr(path, list, LIST_BUF_LEN, 0);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
205 if(len == -1) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
206 switch(errno) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
207 case ERANGE: {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
208 // buffer too, get size of attribute list
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
209 ssize_t newlen = listxattr(path, NULL, 0, 0);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
210 if(newlen > 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
211 // second try
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
212 list = realloc(list, newlen);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
213 len = listxattr(path, list, newlen, 0);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
214 if(len != -1) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
215 // this time it worked
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
216 break;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
217 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
218 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
219 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
220 default: {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
221 free(list);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
222 *nelm = -1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
223 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
224 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
225 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
226 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
227
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
228 char **ret = parse_xattrlist(list, len, nelm);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
229 free(list);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
230 return ret;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
231 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
232
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
233 char * xattr_get(const char *path, const char *attr, ssize_t *len) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
234 // get attribute length
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
235 ssize_t attrlen = getxattr(path, attr, NULL, 0, 0, 0);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
236 if(attrlen < 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
237 *len = -1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
238 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
239 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
240
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
241 char *buf = malloc(attrlen);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
242 ssize_t vlen = getxattr(path, attr, buf, attrlen, 0, 0);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
243 if(vlen < 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
244 *len = -1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
245 free(buf);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
246 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
247 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
248
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
249 *len = vlen;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
250 return buf;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
251 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
252
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
253 int xattr_set(const char *path, const char *name, const void *value, size_t len) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
254 int ret = setxattr(path, name, value, len, 0, 0);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
255 return ret;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
256 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
257
379
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
258 int xattr_remove(const char *path, const char *name) {
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
259 return removexattr(path, name, 0);
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
260 }
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
261
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
262 #endif /* Apple */
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
263
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
264 #ifdef __sun
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
265 #define XATTR_SUPPORTED
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
266 #include <unistd.h>
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
267 #include <sys/types.h>
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
268 #include <sys/stat.h>
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
269 #include <dirent.h>
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
270 #include <fcntl.h>
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
271
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
272 static int open_attrfile(const char *path, const char *attr, int oflag) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
273 int file = open(path, O_RDONLY);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
274 if(file == -1) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
275 return -1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
276 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
277
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
278 int attrfile = openat(file, attr, oflag, S_IRUSR|S_IWUSR|S_IRGRP|S_IROTH);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
279 close(file);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
280 return attrfile;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
281 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
282
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
283 char ** xattr_list(const char *path, ssize_t *nelm) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
284 *nelm = -1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
285
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
286 int attrdir = open_attrfile(path, ".", O_RDONLY|O_XATTR);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
287 if(attrdir == -1) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
288 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
289 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
290
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
291 DIR *dir = fdopendir(attrdir);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
292 if(!dir) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
293 close(attrdir);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
294 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
295 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
296
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
297 size_t arraylen = LIST_ARRAY_LEN;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
298 size_t arraypos = 0;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
299 char **array = malloc(LIST_ARRAY_LEN * sizeof(char*));
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
300
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
301 struct dirent *ent;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
302 while((ent = readdir(dir)) != NULL) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
303 if(!strcmp(ent->d_name, ".") || !strcmp(ent->d_name, "..") || !strcmp(ent->d_name, "SUNWattr_ro") || !strcmp(ent->d_name, "SUNWattr_rw")) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
304 continue;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
305 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
306 char *name = strdup(ent->d_name);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
307 ARRAY_ADD(array, arraypos, arraylen, name);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
308 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
309 closedir(dir);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
310
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
311 *nelm = arraypos;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
312 return array;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
313 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
314
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
315 char * xattr_get(const char *path, const char *attr, ssize_t *len) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
316 *len = -1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
317
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
318 int attrfile = open_attrfile(path, attr, O_RDONLY|O_XATTR);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
319 if(attrfile == -1) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
320 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
321 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
322
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
323 struct stat s;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
324 if(fstat(attrfile, &s)) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
325 close(attrfile);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
326 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
327 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
328
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
329 size_t bufsize = (size_t)s.st_size;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
330 char *buf = malloc(bufsize);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
331
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
332 char *b = buf;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
333 size_t cur = 0;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
334 while(cur < bufsize) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
335 ssize_t r = read(attrfile, buf + cur, bufsize - cur);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
336 if(r <= 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
337 break;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
338 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
339 cur += r;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
340 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
341
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
342 close(attrfile);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
343 if(cur != bufsize) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
344 free(buf);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
345 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
346 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
347
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
348 *len = (ssize_t)bufsize;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
349 return buf;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
350 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
351
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
352 int xattr_set(const char *path, const char *name, const void *value, size_t len) {
369
4322b8953bd5 adds tag conflict detection for dav-sync push
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 364
diff changeset
353 int attrfile = open_attrfile(path, name, O_CREAT|O_WRONLY|O_XATTR|O_TRUNC);
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
354 if(attrfile == -1) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
355 return -1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
356 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
357
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
358 const char *p = value;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
359 size_t remaining = len;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
360 while(remaining > 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
361 ssize_t w = write(attrfile, p, remaining);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
362 if(w <= 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
363 break;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
364 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
365 p += w;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
366 remaining -= w;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
367 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
368
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
369 close(attrfile);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
370
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
371 return remaining > 0 ? -1 : 0;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
372 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
373
379
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
374 int xattr_remove(const char *path, const char *name) {
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
375 int attrdir = open_attrfile(path, ".", O_RDONLY|O_XATTR);
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
376 if(attrdir == -1) {
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
377 return -1;
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
378 }
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
379
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
380 int ret = unlinkat(attrdir, name, 0);
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
381 close(attrdir);
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
382 return ret;
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
383 }
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
384
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
385 #endif /* Sun */
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
386
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
387
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
388 #ifdef __FreeBSD__
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
389 #define XATTR_SUPPORTED
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
390
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
391 #include <sys/types.h>
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
392 #include <sys/extattr.h>
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
393
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
394 static char ** parse_xattrlist(char *buf, ssize_t length, ssize_t *nelm) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
395 size_t arraylen = LIST_ARRAY_LEN;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
396 size_t arraypos = 0;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
397 char **array = malloc(LIST_ARRAY_LEN * sizeof(char*));
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
398
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
399 char *name = buf;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
400 for(int i=0;i<length;i++) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
401 char namelen = buf[i];
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
402 char *name = buf + i + 1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
403 char *attrname = malloc(namelen + 1);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
404 memcpy(attrname, name, namelen);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
405 attrname[namelen] = 0;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
406 ARRAY_ADD(array, arraypos, arraylen, attrname);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
407 i += namelen;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
408 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
409
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
410 if(arraypos == 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
411 free(array);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
412 array = NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
413 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
414
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
415 *nelm = arraypos;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
416 return array;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
417 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
418
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
419 char ** xattr_list(const char *path, ssize_t *nelm) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
420 *nelm = -1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
421 ssize_t lslen = extattr_list_file(path, EXTATTR_NAMESPACE_USER, NULL, 0);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
422 if(lslen <= 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
423 if(lslen == 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
424 *nelm = 0;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
425 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
426 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
427 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
428
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
429 char *list = malloc(lslen);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
430 ssize_t len = extattr_list_file(path, EXTATTR_NAMESPACE_USER, list, lslen);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
431 if(len == -1) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
432 free(list);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
433 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
434 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
435
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
436 char **ret = parse_xattrlist(list, len, nelm);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
437 free(list);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
438 return ret;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
439 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
440
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
441 char * xattr_get(const char *path, const char *attr, ssize_t *len) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
442 // get attribute length
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
443 ssize_t attrlen = extattr_get_file(path, EXTATTR_NAMESPACE_USER, attr, NULL, 0);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
444 if(attrlen < 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
445 *len = -1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
446 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
447 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
448
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
449 char *buf = malloc(attrlen);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
450 ssize_t vlen = extattr_get_file(path, EXTATTR_NAMESPACE_USER, attr, buf, attrlen);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
451 if(vlen < 0) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
452 *len = -1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
453 free(buf);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
454 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
455 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
456
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
457 *len = vlen;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
458 return buf;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
459 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
460
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
461 int xattr_set(const char *path, const char *name, const void *value, size_t len) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
462 int ret = extattr_set_file(path, EXTATTR_NAMESPACE_USER, name, value, len);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
463 return ret;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
464 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
465
379
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
466 int xattr_remove(const char *path, const char *name) {
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
467 return extattr_delete_file(path, EXTATTR_NAMESPACE_USER, name);
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
468 }
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
469
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
470 #endif /* FreeBSD */
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
471
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
472
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
473 #ifndef XATTR_SUPPORTED
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
474
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
475 char ** xattr_list(const char *path, ssize_t *nelm) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
476 *nelm = -1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
477 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
478 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
479
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
480 char * xattr_get(const char *path, const char *attr, ssize_t *len) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
481 *len = -1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
482 return NULL;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
483 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
484
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
485 int xattr_set(const char *path, const char *name, const void *value, size_t len) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
486 return -1;
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
487 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
488
379
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
489 int xattr_remove(const char *path, const char *name) {
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
490 return -1;
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
491 }
cdaf5a3d3a50 fixes tag syncing
Olaf Wintermann <olaf.wintermann@gmail.com>
parents: 369
diff changeset
492
361
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
493 #endif /* unsupported platform */
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
494
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
495 void xattr_free_list(char **attrnames, ssize_t nelm) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
496 if(attrnames) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
497 for(int i=0;i<nelm;i++) {
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
498 free(attrnames[i]);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
499 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
500 free(attrnames);
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
501 }
b6f2462ee055 adds xattr lib and tag support
Olaf Wintermann <olaf.wintermann@gmail.com>
parents:
diff changeset
502 }

mercurial