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 (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 (char *xattr, char *val, int vallen, char *path, int (*setxattr) (const char *path, const char *name, const void *value, size_t size, int flags));
40 static int _removexattr (char *xattr, char *path, int (*removexattr) (const char *path, const char *name));
42 guestfs_int_xattr_list *
43 do_getxattrs (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 (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 (char *xattr, char *val, int vallen, 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 (char *xattr, char *val, int vallen, 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 (char *xattr, 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 (char *xattr, 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 (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 ABS_PATH (path, NULL);
123 len = listxattr (path, NULL, 0);
126 reply_with_perror ("listxattr");
132 reply_with_perror ("malloc");
137 len = listxattr (path, buf, len);
140 reply_with_perror ("listxattr");
144 r = calloc (1, sizeof (*r));
146 reply_with_perror ("malloc");
150 /* What we get from the kernel is a string "foo\0bar\0baz" of length
151 * len. First count the strings.
153 r->guestfs_int_xattr_list_len = 0;
154 for (i = 0; i < len; i += strlen (&buf[i]) + 1)
155 r->guestfs_int_xattr_list_len++;
157 r->guestfs_int_xattr_list_val =
158 calloc (r->guestfs_int_xattr_list_len, sizeof (guestfs_int_xattr));
159 if (r->guestfs_int_xattr_list_val == NULL) {
160 reply_with_perror ("calloc");
164 for (i = 0, j = 0; i < len; i += strlen (&buf[i]) + 1, ++j) {
166 vlen = getxattr (path, &buf[i], NULL, 0);
169 reply_with_perror ("getxattr");
173 r->guestfs_int_xattr_list_val[j].attrname = strdup (&buf[i]);
174 r->guestfs_int_xattr_list_val[j].attrval.attrval_val = malloc (vlen);
175 r->guestfs_int_xattr_list_val[j].attrval.attrval_len = vlen;
177 if (r->guestfs_int_xattr_list_val[j].attrname == NULL ||
178 r->guestfs_int_xattr_list_val[j].attrval.attrval_val == NULL) {
179 reply_with_perror ("malloc");
184 vlen = getxattr (path, &buf[i],
185 r->guestfs_int_xattr_list_val[j].attrval.attrval_val,
189 reply_with_perror ("getxattr");
201 if (r->guestfs_int_xattr_list_val)
202 for (i = 0; i < r->guestfs_int_xattr_list_len; ++i) {
203 free (r->guestfs_int_xattr_list_val[i].attrname);
204 free (r->guestfs_int_xattr_list_val[i].attrval.attrval_val);
206 free (r->guestfs_int_xattr_list_val);
213 _setxattr (char *xattr, char *val, int vallen, char *path,
214 int (*setxattr) (const char *path, const char *name,
215 const void *value, size_t size, int flags))
220 r = setxattr (path, xattr, val, vallen, 0);
223 reply_with_perror ("setxattr");
231 _removexattr (char *xattr, char *path,
232 int (*removexattr) (const char *path, const char *name))
237 r = removexattr (path, xattr);
240 reply_with_perror ("removexattr");
247 #else /* no xattr.h */
249 guestfs_int_xattr_list *
250 do_getxattrs (char *path)
252 reply_with_error ("getxattrs: no support for xattrs");
256 guestfs_int_xattr_list *
257 do_lgetxattrs (char *path)
259 reply_with_error ("lgetxattrs: no support for xattrs");
264 do_setxattr (char *xattr, char *val, int vallen, char *path)
266 reply_with_error ("setxattr: no support for xattrs");
271 do_lsetxattr (char *xattr, char *val, int vallen, char *path)
273 reply_with_error ("lsetxattr: no support for xattrs");
278 do_removexattr (char *xattr, char *path)
280 reply_with_error ("removexattr: no support for xattrs");
285 do_lremovexattr (char *xattr, char *path)
287 reply_with_error ("lremovexattr: no support for xattrs");
291 #endif /* no xattr.h */