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