generator.ml: use new "Pathname" designation
[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       for (i = 0; i < r->guestfs_int_xattr_list_len; ++i) {
200         free (r->guestfs_int_xattr_list_val[i].attrname);
201         free (r->guestfs_int_xattr_list_val[i].attrval.attrval_val);
202       }
203     free (r->guestfs_int_xattr_list_val);
204   }
205   free (r);
206   return NULL;
207 }
208
209 static int
210 _setxattr (const char *xattr, const char *val, int vallen, const char *path,
211            int (*setxattr) (const char *path, const char *name,
212                             const void *value, size_t size, int flags))
213 {
214   int r;
215
216   CHROOT_IN;
217   r = setxattr (path, xattr, val, vallen, 0);
218   CHROOT_OUT;
219   if (r == -1) {
220     reply_with_perror ("setxattr");
221     return -1;
222   }
223
224   return 0;
225 }
226
227 static int
228 _removexattr (const char *xattr, const char *path,
229               int (*removexattr) (const char *path, const char *name))
230 {
231   int r;
232
233   CHROOT_IN;
234   r = removexattr (path, xattr);
235   CHROOT_OUT;
236   if (r == -1) {
237     reply_with_perror ("removexattr");
238     return -1;
239   }
240
241   return 0;
242 }
243
244 #else /* no xattr.h */
245
246 guestfs_int_xattr_list *
247 do_getxattrs (const char *path)
248 {
249   reply_with_error ("getxattrs: no support for xattrs");
250   return NULL;
251 }
252
253 guestfs_int_xattr_list *
254 do_lgetxattrs (const char *path)
255 {
256   reply_with_error ("lgetxattrs: no support for xattrs");
257   return NULL;
258 }
259
260 int
261 do_setxattr (const char *xattr, const char *val, int vallen, const char *path)
262 {
263   reply_with_error ("setxattr: no support for xattrs");
264   return -1;
265 }
266
267 int
268 do_lsetxattr (const char *xattr, const char *val, int vallen, const char *path)
269 {
270   reply_with_error ("lsetxattr: no support for xattrs");
271   return -1;
272 }
273
274 int
275 do_removexattr (const char *xattr, const char *path)
276 {
277   reply_with_error ("removexattr: no support for xattrs");
278   return -1;
279 }
280
281 int
282 do_lremovexattr (const char *xattr, const char *path)
283 {
284   reply_with_error ("lremovexattr: no support for xattrs");
285   return -1;
286 }
287
288 #endif /* no xattr.h */