1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 Red Hat Inc.
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.
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.
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.
28 #include "../src/guestfs_protocol.h"
33 input_to_nul (FILE *fp, char *buf, int maxlen)
46 reply_with_error ("input_to_nul: input string too long");
51 do_find (const char *dir)
54 int r, len, sysrootdirlen;
58 int size = 0, alloc = 0;
62 sysrootdir = sysroot_path (dir);
64 reply_with_perror ("malloc");
68 r = stat (sysrootdir, &statbuf);
70 reply_with_perror ("%s", dir);
74 if (!S_ISDIR (statbuf.st_mode)) {
75 reply_with_error ("%s: not a directory", dir);
80 sysrootdirlen = strlen (sysrootdir);
82 /* Assemble the external find command. */
83 if (asprintf_nowarn (&cmd, "find %Q -print0", sysrootdir) == -1) {
84 reply_with_perror ("malloc");
91 fprintf (stderr, "%s\n", cmd);
93 fp = popen (cmd, "r");
95 reply_with_perror ("%s", cmd);
101 while ((r = input_to_nul (fp, str, PATH_MAX)) > 0) {
103 printf ("find string: %s\n", str);
106 if (len <= sysrootdirlen)
109 /* Remove the directory part of the path when adding it. */
110 if (add_string (&res, &size, &alloc, str + sysrootdirlen) == -1) {
115 if (pclose (fp) != 0) {
116 reply_with_perror ("pclose: find");
117 free_stringslen (res, size);
122 free_stringslen (res, size);
126 if (add_string (&res, &size, &alloc, NULL) == -1)
129 sort_strings (res, size-1);
131 return res; /* caller frees */
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.
138 #if PATH_MAX > GUESTFS_MAX_CHUNK_SIZE
139 #error "PATH_MAX > GUESTFS_MAX_CHUNK_SIZE"
142 /* Has one FileOut parameter. */
144 do_find0 (const char *dir)
151 size_t sysrootdirlen, len;
152 char str[GUESTFS_MAX_CHUNK_SIZE];
154 sysrootdir = sysroot_path (dir);
156 reply_with_perror ("malloc");
160 r = stat (sysrootdir, &statbuf);
162 reply_with_perror ("%s", dir);
166 if (!S_ISDIR (statbuf.st_mode)) {
167 reply_with_error ("%s: not a directory", dir);
172 sysrootdirlen = strlen (sysrootdir);
174 if (asprintf_nowarn (&cmd, "find %Q -print0", sysrootdir) == -1) {
175 reply_with_perror ("asprintf");
182 fprintf (stderr, "%s\n", cmd);
184 fp = popen (cmd, "r");
186 reply_with_perror ("%s", cmd);
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.
198 while ((r = input_to_nul (fp, str, GUESTFS_MAX_CHUNK_SIZE)) > 0) {
200 printf ("find0 string: %s\n", str);
203 if (len <= sysrootdirlen)
206 /* Remove the directory part of the path before sending it. */
207 if (send_file_write (str + sysrootdirlen, r - sysrootdirlen) < 0) {
215 send_file_end (1); /* Cancel. */
220 if (pclose (fp) != 0) {
222 send_file_end (1); /* Cancel. */
226 if (send_file_end (0)) /* Normal end of file. */