2 * Copyright (c) 2001-2003,2005 Silicon Graphics, Inc.
5 * This program is free software: you can redistribute it and/or modify it
6 * under the terms of the GNU Lesser General Public License as published
7 * by the Free Software Foundation, either version 2.1 of the License, or
8 * (at your option) any later version.
10 * This program is distributed in the hope that it will be useful,
11 * but WITHOUT ANY WARRANTY; without even the implied warranty of
12 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 * GNU Lesser General Public License for more details.
15 * You should have received a copy of the GNU Lesser General Public License
16 * along with this program. If not, see <http://www.gnu.org/licenses/>.
23 #include <sys/types.h>
25 #include <attr/xattr.h>
26 #include <attr/attributes.h>
29 #define MAXNAMELEN 256
31 #define MAXLISTLEN 65536
34 #define roundup(x,y) ((((x)+((y)-1))/(y))*(y))
36 static const char *user_name = "user.";
37 static const char *secure_name = "security.";
38 static const char *trusted_name = "trusted.";
39 static const char *xfsroot_name = "xfsroot.";
42 * Convert IRIX API components into Linux/XFS API components,
46 api_convert(char *name, const char *irixname, int irixflags, int compat)
48 if (strlen(irixname) >= MAXNAMELEN) {
52 if (irixflags & ATTR_ROOT) {
54 strcpy(name, xfsroot_name);
56 strcpy(name, trusted_name);
57 } else if (irixflags & ATTR_SECURE) {
58 strcpy(name, secure_name);
60 strcpy(name, user_name);
62 strcat(name, irixname);
67 api_unconvert(char *name, const char *linuxname, int irixflags)
71 length = strlen(user_name);
72 if (strncmp(linuxname, user_name, length) == 0) {
73 type = 0; /*ATTR_USER*/
76 length = strlen(secure_name);
77 if (strncmp(linuxname, secure_name, length) == 0) {
81 length = strlen(trusted_name);
82 if (strncmp(linuxname, trusted_name, length) == 0) {
86 length = strlen(xfsroot_name);
87 if (strncmp(linuxname, xfsroot_name, length) == 0) {
94 if ((irixflags & ATTR_SECURE) != 0 && (type != ATTR_SECURE))
96 if ((irixflags & ATTR_ROOT) != 0 && (type != ATTR_ROOT))
98 strcpy(name, linuxname + length);
104 attr_get(const char *path, const char *attrname, char *attrvalue,
105 int *valuelength, int flags)
108 char name[MAXNAMELEN+16];
110 for (compat = 0; compat < 2; compat++) {
111 if ((c = api_convert(name, attrname, flags, compat)) < 0)
113 if (flags & ATTR_DONTFOLLOW)
114 c = lgetxattr(path, name, attrvalue, *valuelength);
116 c = getxattr(path, name, attrvalue, *valuelength);
117 if (c < 0 && (errno == ENOATTR || errno == ENOTSUP))
128 attr_getf(int fd, const char *attrname, char *attrvalue,
129 int *valuelength, int flags)
132 char name[MAXNAMELEN+16];
134 for (compat = 0; compat < 2; compat++) {
135 if ((c = api_convert(name, attrname, flags, compat)) < 0)
137 c = fgetxattr(fd, name, attrvalue, *valuelength);
138 if (c < 0 && (errno == ENOATTR || errno == ENOTSUP))
149 attr_set(const char *path, const char *attrname, const char *attrvalue,
150 const int valuelength, int flags)
152 int c, compat, lflags = 0;
153 char name[MAXNAMELEN+16];
154 void *buffer = (void *)attrvalue;
156 if (flags & ATTR_CREATE)
157 lflags = XATTR_CREATE;
158 else if (flags & ATTR_REPLACE)
159 lflags = XATTR_REPLACE;
161 for (compat = 0; compat < 2; compat++) {
162 if ((c = api_convert(name, attrname, flags, compat)) < 0)
164 if (flags & ATTR_DONTFOLLOW)
165 c = lsetxattr(path, name, buffer, valuelength, lflags);
167 c = setxattr(path, name, buffer, valuelength, lflags);
168 if (c < 0 && (errno == ENOATTR || errno == ENOTSUP))
176 attr_setf(int fd, const char *attrname,
177 const char *attrvalue, const int valuelength, int flags)
179 int c, compat, lflags = 0;
180 char name[MAXNAMELEN+16];
181 void *buffer = (void *)attrvalue;
183 if (flags & ATTR_CREATE)
184 lflags = XATTR_CREATE;
185 else if (flags & ATTR_REPLACE)
186 lflags = XATTR_REPLACE;
188 for (compat = 0; compat < 2; compat++) {
189 if ((c = api_convert(name, attrname, flags, compat)) < 0)
191 c = fsetxattr(fd, name, buffer, valuelength, lflags);
192 if (c < 0 && (errno == ENOATTR || errno == ENOTSUP))
200 attr_remove(const char *path, const char *attrname, int flags)
203 char name[MAXNAMELEN+16];
205 for (compat = 0; compat < 2; compat++) {
206 if ((c = api_convert(name, attrname, flags, compat)) < 0)
208 if (flags & ATTR_DONTFOLLOW)
209 c = lremovexattr(path, name);
211 c = removexattr(path, name);
212 if (c < 0 && (errno == ENOATTR || errno == ENOTSUP))
220 attr_removef(int fd, const char *attrname, int flags)
223 char name[MAXNAMELEN+16];
225 for (compat = 0; compat < 2; compat++) {
226 if ((c = api_convert(name, attrname, flags, compat)) < 0)
228 c = fremovexattr(fd, name);
229 if (c < 0 && (errno == ENOATTR || errno == ENOTSUP))
238 * Helper routine for attr_list functions.
242 attr_list_pack(const char *name, const int valuelen,
243 char *buffer, const int buffersize,
244 int *start_offset, int *end_offset)
246 attrlist_ent_t *aentp;
247 attrlist_t *alist = (attrlist_t *)buffer;
248 int size = roundup(strlen(name) + 1 + sizeof(aentp->a_valuelen), 8);
250 if ((*end_offset - size) < (*start_offset + sizeof(alist->al_count))) {
256 aentp = (attrlist_ent_t *)&buffer[ *end_offset ];
257 aentp->a_valuelen = valuelen;
258 strncpy(aentp->a_name, name, size - sizeof(aentp->a_valuelen));
260 *start_offset += sizeof(alist->al_offset);
261 alist->al_offset[alist->al_count] = *end_offset;
267 attr_list(const char *path, char *buffer, const int buffersize, int flags,
268 attrlist_cursor_t *cursor)
271 int length, vlength, count = 0;
272 char lbuf[MAXLISTLEN];
273 char name[MAXNAMELEN+16];
274 int start_offset, end_offset;
276 if (buffersize < sizeof(attrlist_t)) {
280 bzero(buffer, sizeof(attrlist_t));
282 if (flags & ATTR_DONTFOLLOW)
283 length = llistxattr(path, lbuf, sizeof(lbuf));
285 length = listxattr(path, lbuf, sizeof(lbuf));
289 start_offset = sizeof(attrlist_t);
290 end_offset = buffersize & ~(8-1); /* 8 byte align */
292 for (l = lbuf; l != lbuf + length; l = strchr(l, '\0') + 1) {
293 if (api_unconvert(name, l, flags))
295 if (flags & ATTR_DONTFOLLOW)
296 vlength = lgetxattr(path, l, NULL, 0);
298 vlength = getxattr(path, l, NULL, 0);
299 if (vlength < 0 && (errno == ENOATTR || errno == ENOTSUP))
301 if (count++ < cursor->opaque[0])
303 if (attr_list_pack(name, vlength, buffer, buffersize,
304 &start_offset, &end_offset)) {
305 cursor->opaque[0] = count;
313 attr_listf(int fd, char *buffer, const int buffersize, int flags,
314 attrlist_cursor_t *cursor)
317 int length, vlength, count = 0;
318 char lbuf[MAXLISTLEN];
319 char name[MAXNAMELEN+16];
320 int start_offset, end_offset;
322 if (buffersize < sizeof(attrlist_t)) {
326 bzero(buffer, sizeof(attrlist_t));
328 length = flistxattr(fd, lbuf, sizeof(lbuf));
332 start_offset = sizeof(attrlist_t);
333 end_offset = buffersize & ~(8-1); /* 8 byte align */
335 for (l = lbuf; l != lbuf + length; l = strchr(l, '\0') + 1) {
336 if (api_unconvert(name, l, flags))
338 vlength = fgetxattr(fd, l, NULL, 0);
339 if (vlength < 0 && (errno == ENOATTR || errno == ENOTSUP))
341 if (count++ < cursor->opaque[0])
343 if (attr_list_pack(name, vlength, buffer, buffersize,
344 &start_offset, &end_offset)) {
345 cursor->opaque[0] = count;
354 * Helper routines for the attr_multi functions. In IRIX, the
355 * multi routines are a single syscall - in Linux, we break em
356 * apart in userspace and make individual syscalls for each.
360 attr_single(const char *path, attr_multiop_t *op, int flags)
365 flags |= op->am_flags;
366 if (op->am_opcode & ATTR_OP_GET)
367 r = attr_get(path, op->am_attrname, op->am_attrvalue,
368 &op->am_length, flags);
369 else if (op->am_opcode & ATTR_OP_SET)
370 r = attr_set(path, op->am_attrname, op->am_attrvalue,
371 op->am_length, flags);
372 else if (op->am_opcode & ATTR_OP_REMOVE)
373 r = attr_remove(path, op->am_attrname, flags);
378 attr_singlef(const int fd, attr_multiop_t *op, int flags)
383 flags |= op->am_flags;
384 if (op->am_opcode & ATTR_OP_GET)
385 r = attr_getf(fd, op->am_attrname, op->am_attrvalue,
386 &op->am_length, flags);
387 else if (op->am_opcode & ATTR_OP_SET)
388 r = attr_setf(fd, op->am_attrname, op->am_attrvalue,
389 op->am_length, flags);
390 else if (op->am_opcode & ATTR_OP_REMOVE)
391 r = attr_removef(fd, op->am_attrname, flags);
396 * Operate on multiple attributes of the same object simultaneously
398 * From the manpage: "attr_multi will fail if ... a bit other than
399 * ATTR_DONTFOLLOW was set in the flag argument." flags must be
400 * checked here as they are not passed into the kernel.
403 attr_multi(const char *path, attr_multiop_t *multiops, int count, int flags)
408 if ((flags & ATTR_DONTFOLLOW) != flags)
412 for (i = 0; i < count; i++) {
413 tmp = attr_single(path, &multiops[i], flags);
420 attr_multif(int fd, attr_multiop_t *multiops, int count, int flags)
425 if ((flags & ATTR_DONTFOLLOW) != flags)
429 for (i = 0; i < count; i++) {
430 tmp = attr_singlef(fd, &multiops[i], flags);