generator: Make 'xz' into an optional group.
[libguestfs.git] / daemon / dir.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 <stdlib.h>
23 #include <string.h>
24 #include <unistd.h>
25 #include <sys/stat.h>
26 #include <sys/types.h>
27
28 #include "../src/guestfs_protocol.h"
29 #include "daemon.h"
30 #include "actions.h"
31
32 int
33 do_rmdir (const char *path)
34 {
35   int r;
36
37   CHROOT_IN;
38   r = rmdir (path);
39   CHROOT_OUT;
40
41   if (r == -1) {
42     reply_with_perror ("%s", path);
43     return -1;
44   }
45
46   return 0;
47 }
48
49 /* This implementation is quick and dirty, and allows people to try
50  * to remove parts of the initramfs (eg. "rm -r /..") but if people
51  * do stupid stuff, who are we to try to stop them?
52  */
53 int
54 do_rm_rf (const char *path)
55 {
56   int r;
57   char *buf, *err;
58
59   if (STREQ (path, "/")) {
60     reply_with_error ("cannot remove root directory");
61     return -1;
62   }
63
64   buf = sysroot_path (path);
65   if (buf == NULL) {
66     reply_with_perror ("malloc");
67     return -1;
68   }
69
70   r = command (NULL, &err, "rm", "-rf", buf, NULL);
71   free (buf);
72
73   /* rm -rf is never supposed to fail.  I/O errors perhaps? */
74   if (r == -1) {
75     reply_with_error ("%s: %s", path, err);
76     free (err);
77     return -1;
78   }
79
80   free (err);
81
82   return 0;
83 }
84
85 int
86 do_mkdir (const char *path)
87 {
88   int r;
89
90   CHROOT_IN;
91   r = mkdir (path, 0777);
92   CHROOT_OUT;
93
94   if (r == -1) {
95     reply_with_perror ("%s", path);
96     return -1;
97   }
98
99   return 0;
100 }
101
102 int
103 do_mkdir_mode (const char *path, int mode)
104 {
105   int r;
106
107   if (mode < 0) {
108     reply_with_error ("%s: mode is negative", path);
109     return -1;
110   }
111
112   CHROOT_IN;
113   r = mkdir (path, mode);
114   CHROOT_OUT;
115
116   if (r == -1) {
117     reply_with_perror ("%s", path);
118     return -1;
119   }
120
121   return 0;
122 }
123
124 /* Returns:
125  * 0  if everything was OK,
126  * -1 for a general error (sets errno),
127  * -2 if an existing path element was not a directory.
128  */
129 static int
130 recursive_mkdir (const char *path)
131 {
132   int loop = 0;
133   int r;
134   char *ppath, *p;
135   struct stat buf;
136
137  again:
138   r = mkdir (path, 0777);
139   if (r == -1) {
140     if (errno == EEXIST) {      /* Something exists here, might not be a dir. */
141       r = lstat (path, &buf);
142       if (r == -1) return -1;
143       if (!S_ISDIR (buf.st_mode)) return -2;
144       return 0;                 /* OK - directory exists here already. */
145     }
146
147     if (!loop && errno == ENOENT) {
148       loop = 1;                 /* Stops it looping forever. */
149
150       /* If we're at the root, and we failed, just give up. */
151       if (path[0] == '/' && path[1] == '\0') return -1;
152
153       /* Try to make the parent directory first. */
154       ppath = strdup (path);
155       if (ppath == NULL) return -1;
156
157       p = strrchr (ppath, '/');
158       if (p) *p = '\0';
159
160       r = recursive_mkdir (ppath);
161       free (ppath);
162
163       if (r != 0) return r;
164
165       goto again;
166     } else        /* Failed for some other reason, so return error. */
167       return -1;
168   }
169   return 0;
170 }
171
172 int
173 do_mkdir_p (const char *path)
174 {
175   int r;
176
177   CHROOT_IN;
178   r = recursive_mkdir (path);
179   CHROOT_OUT;
180
181   if (r == -1) {
182     reply_with_perror ("%s", path);
183     return -1;
184   }
185   if (r == -2) {
186     reply_with_error ("%s: a path element was not a directory", path);
187     return -1;
188   }
189
190   return 0;
191 }
192
193 int
194 do_is_dir (const char *path)
195 {
196   int r;
197   struct stat buf;
198
199   CHROOT_IN;
200   r = lstat (path, &buf);
201   CHROOT_OUT;
202
203   if (r == -1) {
204     if (errno != ENOENT && errno != ENOTDIR) {
205       reply_with_perror ("stat: %s", path);
206       return -1;
207     }
208     else
209       return 0;                 /* Not a directory. */
210   }
211
212   return S_ISDIR (buf.st_mode);
213 }
214
215 char *
216 do_mkdtemp (const char *template)
217 {
218   char *writable = strdup (template);
219   if (writable == NULL) {
220     reply_with_perror ("strdup");
221     return NULL;
222   }
223
224   CHROOT_IN;
225   char *r = mkdtemp (writable);
226   CHROOT_OUT;
227
228   if (r == NULL) {
229     reply_with_perror ("%s", template);
230     free (writable);
231   }
232
233   return r;
234 }