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