1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
4 * This program is free software; you can redistribute it and/or modify
5 * it under the terms of the GNU General Public License as published by
6 * the Free Software Foundation; either version 2 of the License, or
7 * (at your option) any later version.
9 * This program is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
12 * GNU General Public License for more details.
14 * You should have received a copy of the GNU General Public License
15 * along with this program; if not, write to the Free Software
16 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
24 #include "../src/guestfs_protocol.h"
28 #if defined(HAVE_ATTR_XATTR_H) || defined(HAVE_SYS_XATTR_H)
30 #ifdef HAVE_ATTR_XATTR_H
31 #include <attr/xattr.h>
33 #ifdef HAVE_SYS_XATTR_H
34 #include <sys/xattr.h>
38 static guestfs_int_xattr_list *getxattrs (const char *path, ssize_t (*listxattr) (const char *path, char *list, size_t size), ssize_t (*getxattr) (const char *path, const char *name, void *value, size_t size));
39 static int _setxattr (const char *xattr, const char *val, int vallen, const char *path, int (*setxattr) (const char *path, const char *name, const void *value, size_t size, int flags));
40 static int _removexattr (const char *xattr, const char *path, int (*removexattr) (const char *path, const char *name));
42 guestfs_int_xattr_list *
43 do_getxattrs (const char *path)
45 #if defined(HAVE_LISTXATTR) && defined(HAVE_GETXATTR)
46 return getxattrs (path, listxattr, getxattr);
48 reply_with_error ("getxattrs: no support for listxattr and getxattr");
53 guestfs_int_xattr_list *
54 do_lgetxattrs (const char *path)
56 #if defined(HAVE_LLISTXATTR) && defined(HAVE_LGETXATTR)
57 return getxattrs (path, llistxattr, lgetxattr);
59 reply_with_error ("lgetxattrs: no support for llistxattr and lgetxattr");
65 do_setxattr (const char *xattr, const char *val, int vallen, const char *path)
67 #if defined(HAVE_SETXATTR)
68 return _setxattr (xattr, val, vallen, path, setxattr);
70 reply_with_error ("setxattr: no support for setxattr");
76 do_lsetxattr (const char *xattr, const char *val, int vallen, const char *path)
78 #if defined(HAVE_LSETXATTR)
79 return _setxattr (xattr, val, vallen, path, lsetxattr);
81 reply_with_error ("lsetxattr: no support for lsetxattr");
87 do_removexattr (const char *xattr, const char *path)
89 #if defined(HAVE_REMOVEXATTR)
90 return _removexattr (xattr, path, removexattr);
92 reply_with_error ("removexattr: no support for removexattr");
98 do_lremovexattr (const char *xattr, const char *path)
100 #if defined(HAVE_LREMOVEXATTR)
101 return _removexattr (xattr, path, lremovexattr);
103 reply_with_error ("lremovexattr: no support for lremovexattr");
108 static guestfs_int_xattr_list *
109 getxattrs (const char *path,
110 ssize_t (*listxattr) (const char *path, char *list, size_t size),
111 ssize_t (*getxattr) (const char *path, const char *name,
112 void *value, size_t size))
117 guestfs_int_xattr_list *r = NULL;
120 len = listxattr (path, NULL, 0);
123 reply_with_perror ("listxattr");
129 reply_with_perror ("malloc");
134 len = listxattr (path, buf, len);
137 reply_with_perror ("listxattr");
141 r = calloc (1, sizeof (*r));
143 reply_with_perror ("malloc");
147 /* What we get from the kernel is a string "foo\0bar\0baz" of length
148 * len. First count the strings.
150 r->guestfs_int_xattr_list_len = 0;
151 for (i = 0; i < len; i += strlen (&buf[i]) + 1)
152 r->guestfs_int_xattr_list_len++;
154 r->guestfs_int_xattr_list_val =
155 calloc (r->guestfs_int_xattr_list_len, sizeof (guestfs_int_xattr));
156 if (r->guestfs_int_xattr_list_val == NULL) {
157 reply_with_perror ("calloc");
161 for (i = 0, j = 0; i < len; i += strlen (&buf[i]) + 1, ++j) {
163 vlen = getxattr (path, &buf[i], NULL, 0);
166 reply_with_perror ("getxattr");
170 r->guestfs_int_xattr_list_val[j].attrname = strdup (&buf[i]);
171 r->guestfs_int_xattr_list_val[j].attrval.attrval_val = malloc (vlen);
172 r->guestfs_int_xattr_list_val[j].attrval.attrval_len = vlen;
174 if (r->guestfs_int_xattr_list_val[j].attrname == NULL ||
175 r->guestfs_int_xattr_list_val[j].attrval.attrval_val == NULL) {
176 reply_with_perror ("malloc");
181 vlen = getxattr (path, &buf[i],
182 r->guestfs_int_xattr_list_val[j].attrval.attrval_val,
186 reply_with_perror ("getxattr");
198 if (r->guestfs_int_xattr_list_val) {
200 for (k = 0; k < r->guestfs_int_xattr_list_len; ++k) {
201 free (r->guestfs_int_xattr_list_val[k].attrname);
202 free (r->guestfs_int_xattr_list_val[k].attrval.attrval_val);
205 free (r->guestfs_int_xattr_list_val);
212 _setxattr (const char *xattr, const char *val, int vallen, const char *path,
213 int (*setxattr) (const char *path, const char *name,
214 const void *value, size_t size, int flags))
219 r = setxattr (path, xattr, val, vallen, 0);
222 reply_with_perror ("setxattr");
230 _removexattr (const char *xattr, const char *path,
231 int (*removexattr) (const char *path, const char *name))
236 r = removexattr (path, xattr);
239 reply_with_perror ("removexattr");
246 #else /* no xattr.h */
248 guestfs_int_xattr_list *
249 do_getxattrs (const char *path)
251 reply_with_error ("getxattrs: no support for xattrs");
255 guestfs_int_xattr_list *
256 do_lgetxattrs (const char *path)
258 reply_with_error ("lgetxattrs: no support for xattrs");
263 do_setxattr (const char *xattr, const char *val, int vallen, const char *path)
265 reply_with_error ("setxattr: no support for xattrs");
270 do_lsetxattr (const char *xattr, const char *val, int vallen, const char *path)
272 reply_with_error ("lsetxattr: no support for xattrs");
277 do_removexattr (const char *xattr, const char *path)
279 reply_with_error ("removexattr: no support for xattrs");
284 do_lremovexattr (const char *xattr, const char *path)
286 reply_with_error ("lremovexattr: no support for xattrs");
290 #endif /* no xattr.h */