daemon: Remove some less useful debugging messages.
[libguestfs.git] / daemon / find.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009-2011 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     len = strlen (str);
104     if (len <= sysrootdirlen)
105       continue;
106
107     /* Remove the directory part of the path when adding it. */
108     if (add_string (&res, &size, &alloc, str + sysrootdirlen) == -1) {
109       pclose (fp);
110       return NULL;
111     }
112   }
113   if (pclose (fp) != 0) {
114     reply_with_perror ("pclose");
115     free_stringslen (res, size);
116     return NULL;
117   }
118
119   if (r == -1) {
120     free_stringslen (res, size);
121     return NULL;
122   }
123
124   if (add_string (&res, &size, &alloc, NULL) == -1)
125     return NULL;
126
127   sort_strings (res, size-1);
128
129   return res;                        /* caller frees */
130 }
131
132 /* The code below assumes each path returned can fit into a protocol
133  * chunk.  If this turns out not to be true at some point in the
134  * future then we'll need to modify the code a bit to handle it.
135  */
136 #if PATH_MAX > GUESTFS_MAX_CHUNK_SIZE
137 #error "PATH_MAX > GUESTFS_MAX_CHUNK_SIZE"
138 #endif
139
140 /* Has one FileOut parameter. */
141 int
142 do_find0 (const char *dir)
143 {
144   struct stat statbuf;
145   int r;
146   FILE *fp;
147   char *cmd;
148   char *sysrootdir;
149   size_t sysrootdirlen, len;
150   char str[GUESTFS_MAX_CHUNK_SIZE];
151
152   sysrootdir = sysroot_path (dir);
153   if (!sysrootdir) {
154     reply_with_perror ("malloc");
155     return -1;
156   }
157
158   r = stat (sysrootdir, &statbuf);
159   if (r == -1) {
160     reply_with_perror ("%s", dir);
161     free (sysrootdir);
162     return -1;
163   }
164   if (!S_ISDIR (statbuf.st_mode)) {
165     reply_with_error ("%s: not a directory", dir);
166     free (sysrootdir);
167     return -1;
168   }
169
170   sysrootdirlen = strlen (sysrootdir);
171
172   if (asprintf_nowarn (&cmd, "find %Q -print0", sysrootdir) == -1) {
173     reply_with_perror ("asprintf");
174     free (sysrootdir);
175     return -1;
176   }
177   free (sysrootdir);
178
179   if (verbose)
180     fprintf (stderr, "%s\n", cmd);
181
182   fp = popen (cmd, "r");
183   if (fp == NULL) {
184     reply_with_perror ("%s", cmd);
185     free (cmd);
186     return -1;
187   }
188   free (cmd);
189
190   /* Now we must send the reply message, before the file contents.  After
191    * this there is no opportunity in the protocol to send any error
192    * message back.  Instead we can only cancel the transfer.
193    */
194   reply (NULL, NULL);
195
196   while ((r = input_to_nul (fp, str, GUESTFS_MAX_CHUNK_SIZE)) > 0) {
197     len = strlen (str);
198     if (len <= sysrootdirlen)
199       continue;
200
201     /* Remove the directory part of the path before sending it. */
202     if (send_file_write (str + sysrootdirlen, r - sysrootdirlen) < 0) {
203       pclose (fp);
204       return -1;
205     }
206   }
207
208   if (ferror (fp)) {
209     perror (dir);
210     send_file_end (1);                /* Cancel. */
211     pclose (fp);
212     return -1;
213   }
214
215   if (pclose (fp) != 0) {
216     perror (dir);
217     send_file_end (1);                /* Cancel. */
218     return -1;
219   }
220
221   if (send_file_end (0))        /* Normal end of file. */
222     return -1;
223
224   return 0;
225 }