daemon: blkid cache is at a different location on Debian.
[libguestfs.git] / daemon / find.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 <fcntl.h>
26 #include <limits.h>
27 #include <sys/stat.h>
28
29 #include "guestfs_protocol.h"
30 #include "daemon.h"
31 #include "actions.h"
32
33 static int
34 input_to_nul (FILE *fp, char *buf, int maxlen)
35 {
36   int i = 0, c;
37
38   while (i < maxlen) {
39     c = fgetc (fp);
40     if (c == EOF)
41       return 0;
42     buf[i++] = c;
43     if (c == '\0')
44       return i;
45   }
46
47   reply_with_error ("input_to_nul: input string too long");
48   return -1;
49 }
50
51 char **
52 do_find (const char *dir)
53 {
54   struct stat statbuf;
55   int r, len, sysrootdirlen;
56   char *cmd;
57   FILE *fp;
58   char **res = NULL;
59   int size = 0, alloc = 0;
60   char *sysrootdir;
61   char str[PATH_MAX];
62
63   sysrootdir = sysroot_path (dir);
64   if (!sysrootdir) {
65     reply_with_perror ("malloc");
66     return NULL;
67   }
68
69   r = stat (sysrootdir, &statbuf);
70   if (r == -1) {
71     reply_with_perror ("%s", dir);
72     free (sysrootdir);
73     return NULL;
74   }
75   if (!S_ISDIR (statbuf.st_mode)) {
76     reply_with_error ("%s: not a directory", dir);
77     free (sysrootdir);
78     return NULL;
79   }
80
81   sysrootdirlen = strlen (sysrootdir);
82
83   /* Assemble the external find command. */
84   if (asprintf_nowarn (&cmd, "find %Q -print0", sysrootdir) == -1) {
85     reply_with_perror ("malloc");
86     free (sysrootdir);
87     return NULL;
88   }
89   free (sysrootdir);
90
91   if (verbose)
92     fprintf (stderr, "%s\n", cmd);
93
94   fp = popen (cmd, "r");
95   if (fp == NULL) {
96     reply_with_perror ("%s", cmd);
97     free (cmd);
98     return NULL;
99   }
100   free (cmd);
101
102   while ((r = input_to_nul (fp, str, PATH_MAX)) > 0) {
103     if (verbose)
104       printf ("find string: %s\n", str);
105
106     len = strlen (str);
107     if (len <= sysrootdirlen)
108       continue;
109
110     /* Remove the directory part of the path when adding it. */
111     if (add_string (&res, &size, &alloc, str + sysrootdirlen) == -1) {
112       pclose (fp);
113       return NULL;
114     }
115   }
116   if (pclose (fp) != 0) {
117     reply_with_perror ("pclose");
118     free_stringslen (res, size);
119     return NULL;
120   }
121
122   if (r == -1) {
123     free_stringslen (res, size);
124     return NULL;
125   }
126
127   if (add_string (&res, &size, &alloc, NULL) == -1)
128     return NULL;
129
130   sort_strings (res, size-1);
131
132   return res;                        /* caller frees */
133 }
134
135 /* The code below assumes each path returned can fit into a protocol
136  * chunk.  If this turns out not to be true at some point in the
137  * future then we'll need to modify the code a bit to handle it.
138  */
139 #if PATH_MAX > GUESTFS_MAX_CHUNK_SIZE
140 #error "PATH_MAX > GUESTFS_MAX_CHUNK_SIZE"
141 #endif
142
143 /* Has one FileOut parameter. */
144 int
145 do_find0 (const char *dir)
146 {
147   struct stat statbuf;
148   int r;
149   FILE *fp;
150   char *cmd;
151   char *sysrootdir;
152   size_t sysrootdirlen, len;
153   char str[GUESTFS_MAX_CHUNK_SIZE];
154
155   sysrootdir = sysroot_path (dir);
156   if (!sysrootdir) {
157     reply_with_perror ("malloc");
158     return -1;
159   }
160
161   r = stat (sysrootdir, &statbuf);
162   if (r == -1) {
163     reply_with_perror ("%s", dir);
164     free (sysrootdir);
165     return -1;
166   }
167   if (!S_ISDIR (statbuf.st_mode)) {
168     reply_with_error ("%s: not a directory", dir);
169     free (sysrootdir);
170     return -1;
171   }
172
173   sysrootdirlen = strlen (sysrootdir);
174
175   if (asprintf_nowarn (&cmd, "find %Q -print0", sysrootdir) == -1) {
176     reply_with_perror ("asprintf");
177     free (sysrootdir);
178     return -1;
179   }
180   free (sysrootdir);
181
182   if (verbose)
183     fprintf (stderr, "%s\n", cmd);
184
185   fp = popen (cmd, "r");
186   if (fp == NULL) {
187     reply_with_perror ("%s", cmd);
188     free (cmd);
189     return -1;
190   }
191   free (cmd);
192
193   /* Now we must send the reply message, before the file contents.  After
194    * this there is no opportunity in the protocol to send any error
195    * message back.  Instead we can only cancel the transfer.
196    */
197   reply (NULL, NULL);
198
199   while ((r = input_to_nul (fp, str, GUESTFS_MAX_CHUNK_SIZE)) > 0) {
200     if (verbose)
201       printf ("find0 string: %s\n", str);
202
203     len = strlen (str);
204     if (len <= sysrootdirlen)
205       continue;
206
207     /* Remove the directory part of the path before sending it. */
208     if (send_file_write (str + sysrootdirlen, r - sysrootdirlen) < 0) {
209       pclose (fp);
210       return -1;
211     }
212   }
213
214   if (ferror (fp)) {
215     perror (dir);
216     send_file_end (1);                /* Cancel. */
217     pclose (fp);
218     return -1;
219   }
220
221   if (pclose (fp) != 0) {
222     perror (dir);
223     send_file_end (1);                /* Cancel. */
224     return -1;
225   }
226
227   if (send_file_end (0))        /* Normal end of file. */
228     return -1;
229
230   return 0;
231 }