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