use STREQ, not strcmp: part 1
[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 ("rmdir: %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 ("rm -rf: 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 ("rm -rf: %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 ("mkdir: %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   CHROOT_IN;
108   r = mkdir (path, mode);
109   CHROOT_OUT;
110
111   if (r == -1) {
112     reply_with_perror ("mkdir_mode: %s", path);
113     return -1;
114   }
115
116   return 0;
117 }
118
119 static int
120 recursive_mkdir (const char *path)
121 {
122   int loop = 0;
123   int r;
124   char *ppath, *p;
125   struct stat buf;
126
127  again:
128   r = mkdir (path, 0777);
129   if (r == -1) {
130     if (errno == EEXIST) {      /* Something exists here, might not be a dir. */
131       r = lstat (path, &buf);
132       if (r == -1) return -1;
133       if (!S_ISDIR (buf.st_mode)) {
134         errno = ENOTDIR;
135         return -1;
136       }
137       return 0;                 /* OK - directory exists here already. */
138     }
139
140     if (!loop && errno == ENOENT) {
141       loop = 1;                 /* Stops it looping forever. */
142
143       /* If we're at the root, and we failed, just give up. */
144       if (path[0] == '/' && path[1] == '\0') return -1;
145
146       /* Try to make the parent directory first. */
147       ppath = strdup (path);
148       if (ppath == NULL) return -1;
149
150       p = strrchr (ppath, '/');
151       if (p) *p = '\0';
152
153       r = recursive_mkdir (ppath);
154       free (ppath);
155
156       if (r == -1) return -1;
157
158       goto again;
159     } else        /* Failed for some other reason, so return error. */
160       return -1;
161   }
162   return 0;
163 }
164
165 int
166 do_mkdir_p (const char *path)
167 {
168   int r;
169
170   CHROOT_IN;
171   r = recursive_mkdir (path);
172   CHROOT_OUT;
173
174   if (r == -1) {
175     reply_with_perror ("mkdir -p: %s", path);
176     return -1;
177   }
178
179   return 0;
180 }
181
182 int
183 do_is_dir (const char *path)
184 {
185   int r;
186   struct stat buf;
187
188   CHROOT_IN;
189   r = lstat (path, &buf);
190   CHROOT_OUT;
191
192   if (r == -1) {
193     if (errno != ENOENT && errno != ENOTDIR) {
194       reply_with_perror ("stat: %s", path);
195       return -1;
196     }
197     else
198       return 0;                 /* Not a directory. */
199   }
200
201   return S_ISDIR (buf.st_mode);
202 }
203
204 char *
205 do_mkdtemp (const char *template)
206 {
207   char *writable = strdup (template);
208   if (writable == NULL) {
209     reply_with_perror ("strdup");
210     return NULL;
211   }
212
213   CHROOT_IN;
214   char *r = mkdtemp (writable);
215   CHROOT_OUT;
216
217   if (r == NULL) {
218     reply_with_perror ("mkdtemp: %s", template);
219     free (writable);
220   }
221
222   return r;
223 }