Todo: ntfsclone.
[libguestfs.git] / daemon / xattr.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <unistd.h>
23
24 #include "../src/guestfs_protocol.h"
25 #include "daemon.h"
26 #include "actions.h"
27
28 #if defined(HAVE_ATTR_XATTR_H) || defined(HAVE_SYS_XATTR_H)
29
30 #ifdef HAVE_ATTR_XATTR_H
31 #include <attr/xattr.h>
32 #else
33 #ifdef HAVE_SYS_XATTR_H
34 #include <sys/xattr.h>
35 #endif
36 #endif
37
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));
41
42 guestfs_int_xattr_list *
43 do_getxattrs (const char *path)
44 {
45 #if defined(HAVE_LISTXATTR) && defined(HAVE_GETXATTR)
46   return getxattrs (path, listxattr, getxattr);
47 #else
48   reply_with_error ("getxattrs: no support for listxattr and getxattr");
49   return NULL;
50 #endif
51 }
52
53 guestfs_int_xattr_list *
54 do_lgetxattrs (const char *path)
55 {
56 #if defined(HAVE_LLISTXATTR) && defined(HAVE_LGETXATTR)
57   return getxattrs (path, llistxattr, lgetxattr);
58 #else
59   reply_with_error ("lgetxattrs: no support for llistxattr and lgetxattr");
60   return NULL;
61 #endif
62 }
63
64 int
65 do_setxattr (const char *xattr, const char *val, int vallen, const char *path)
66 {
67 #if defined(HAVE_SETXATTR)
68   return _setxattr (xattr, val, vallen, path, setxattr);
69 #else
70   reply_with_error ("setxattr: no support for setxattr");
71   return -1;
72 #endif
73 }
74
75 int
76 do_lsetxattr (const char *xattr, const char *val, int vallen, const char *path)
77 {
78 #if defined(HAVE_LSETXATTR)
79   return _setxattr (xattr, val, vallen, path, lsetxattr);
80 #else
81   reply_with_error ("lsetxattr: no support for lsetxattr");
82   return -1;
83 #endif
84 }
85
86 int
87 do_removexattr (const char *xattr, const char *path)
88 {
89 #if defined(HAVE_REMOVEXATTR)
90   return _removexattr (xattr, path, removexattr);
91 #else
92   reply_with_error ("removexattr: no support for removexattr");
93   return -1;
94 #endif
95 }
96
97 int
98 do_lremovexattr (const char *xattr, const char *path)
99 {
100 #if defined(HAVE_LREMOVEXATTR)
101   return _removexattr (xattr, path, lremovexattr);
102 #else
103   reply_with_error ("lremovexattr: no support for lremovexattr");
104   return -1;
105 #endif
106 }
107
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))
113 {
114   ssize_t len, vlen;
115   char *buf = NULL;
116   int i, j;
117   guestfs_int_xattr_list *r = NULL;
118
119   CHROOT_IN;
120   len = listxattr (path, NULL, 0);
121   CHROOT_OUT;
122   if (len == -1) {
123     reply_with_perror ("listxattr");
124     goto error;
125   }
126
127   buf = malloc (len);
128   if (buf == NULL) {
129     reply_with_perror ("malloc");
130     goto error;
131   }
132
133   CHROOT_IN;
134   len = listxattr (path, buf, len);
135   CHROOT_OUT;
136   if (len == -1) {
137     reply_with_perror ("listxattr");
138     goto error;
139   }
140
141   r = calloc (1, sizeof (*r));
142   if (r == NULL) {
143     reply_with_perror ("malloc");
144     goto error;
145   }
146
147   /* What we get from the kernel is a string "foo\0bar\0baz" of length
148    * len.  First count the strings.
149    */
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++;
153
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");
158     goto error;
159   }
160
161   for (i = 0, j = 0; i < len; i += strlen (&buf[i]) + 1, ++j) {
162     CHROOT_IN;
163     vlen = getxattr (path, &buf[i], NULL, 0);
164     CHROOT_OUT;
165     if (vlen == -1) {
166       reply_with_perror ("getxattr");
167       goto error;
168     }
169
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;
173
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");
177       goto error;
178     }
179
180     CHROOT_IN;
181     vlen = getxattr (path, &buf[i],
182                      r->guestfs_int_xattr_list_val[j].attrval.attrval_val,
183                      vlen);
184     CHROOT_OUT;
185     if (vlen == -1) {
186       reply_with_perror ("getxattr");
187       goto error;
188     }
189   }
190
191   free (buf);
192
193   return r;
194
195  error:
196   free (buf);
197   if (r) {
198     if (r->guestfs_int_xattr_list_val) {
199       unsigned int k;
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);
203       }
204     }
205     free (r->guestfs_int_xattr_list_val);
206   }
207   free (r);
208   return NULL;
209 }
210
211 static int
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))
215 {
216   int r;
217
218   CHROOT_IN;
219   r = setxattr (path, xattr, val, vallen, 0);
220   CHROOT_OUT;
221   if (r == -1) {
222     reply_with_perror ("setxattr");
223     return -1;
224   }
225
226   return 0;
227 }
228
229 static int
230 _removexattr (const char *xattr, const char *path,
231               int (*removexattr) (const char *path, const char *name))
232 {
233   int r;
234
235   CHROOT_IN;
236   r = removexattr (path, xattr);
237   CHROOT_OUT;
238   if (r == -1) {
239     reply_with_perror ("removexattr");
240     return -1;
241   }
242
243   return 0;
244 }
245
246 #else /* no xattr.h */
247
248 guestfs_int_xattr_list *
249 do_getxattrs (const char *path)
250 {
251   reply_with_error ("getxattrs: no support for xattrs");
252   return NULL;
253 }
254
255 guestfs_int_xattr_list *
256 do_lgetxattrs (const char *path)
257 {
258   reply_with_error ("lgetxattrs: no support for xattrs");
259   return NULL;
260 }
261
262 int
263 do_setxattr (const char *xattr, const char *val, int vallen, const char *path)
264 {
265   reply_with_error ("setxattr: no support for xattrs");
266   return -1;
267 }
268
269 int
270 do_lsetxattr (const char *xattr, const char *val, int vallen, const char *path)
271 {
272   reply_with_error ("lsetxattr: no support for xattrs");
273   return -1;
274 }
275
276 int
277 do_removexattr (const char *xattr, const char *path)
278 {
279   reply_with_error ("removexattr: no support for xattrs");
280   return -1;
281 }
282
283 int
284 do_lremovexattr (const char *xattr, const char *path)
285 {
286   reply_with_error ("lremovexattr: no support for xattrs");
287   return -1;
288 }
289
290 #endif /* no xattr.h */