1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009-2011 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.
29 #include "guestfs_protocol.h"
34 input_to_nul (FILE *fp, char *buf, int maxlen)
47 reply_with_error ("input_to_nul: input string too long");
52 do_find (const char *dir)
55 int r, len, sysrootdirlen;
59 int size = 0, alloc = 0;
63 sysrootdir = sysroot_path (dir);
65 reply_with_perror ("malloc");
69 r = stat (sysrootdir, &statbuf);
71 reply_with_perror ("%s", dir);
75 if (!S_ISDIR (statbuf.st_mode)) {
76 reply_with_error ("%s: not a directory", dir);
81 sysrootdirlen = strlen (sysrootdir);
83 /* Assemble the external find command. */
84 if (asprintf_nowarn (&cmd, "find %Q -print0", sysrootdir) == -1) {
85 reply_with_perror ("malloc");
92 fprintf (stderr, "%s\n", cmd);
94 fp = popen (cmd, "r");
96 reply_with_perror ("%s", cmd);
102 while ((r = input_to_nul (fp, str, PATH_MAX)) > 0) {
104 if (len <= sysrootdirlen)
107 /* Remove the directory part of the path when adding it. */
108 if (add_string (&res, &size, &alloc, str + sysrootdirlen) == -1) {
113 if (pclose (fp) != 0) {
114 reply_with_perror ("pclose");
115 free_stringslen (res, size);
120 free_stringslen (res, size);
124 if (add_string (&res, &size, &alloc, NULL) == -1)
127 sort_strings (res, size-1);
129 return res; /* caller frees */
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.
136 #if PATH_MAX > GUESTFS_MAX_CHUNK_SIZE
137 #error "PATH_MAX > GUESTFS_MAX_CHUNK_SIZE"
140 /* Has one FileOut parameter. */
142 do_find0 (const char *dir)
149 size_t sysrootdirlen, len;
150 char str[GUESTFS_MAX_CHUNK_SIZE];
152 sysrootdir = sysroot_path (dir);
154 reply_with_perror ("malloc");
158 r = stat (sysrootdir, &statbuf);
160 reply_with_perror ("%s", dir);
164 if (!S_ISDIR (statbuf.st_mode)) {
165 reply_with_error ("%s: not a directory", dir);
170 sysrootdirlen = strlen (sysrootdir);
172 if (asprintf_nowarn (&cmd, "find %Q -print0", sysrootdir) == -1) {
173 reply_with_perror ("asprintf");
180 fprintf (stderr, "%s\n", cmd);
182 fp = popen (cmd, "r");
184 reply_with_perror ("%s", cmd);
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.
196 while ((r = input_to_nul (fp, str, GUESTFS_MAX_CHUNK_SIZE)) > 0) {
198 if (len <= sysrootdirlen)
201 /* Remove the directory part of the path before sending it. */
202 if (send_file_write (str + sysrootdirlen, r - sysrootdirlen) < 0) {
210 send_file_end (1); /* Cancel. */
215 if (pclose (fp) != 0) {
217 send_file_end (1); /* Cancel. */
221 if (send_file_end (0)) /* Normal end of file. */