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.
26 #include "../src/guestfs_protocol.h"
31 write_cb (void *fd_ptr, const void *buf, size_t len)
33 int fd = *(int *)fd_ptr;
34 return xwrite (fd, buf, len);
37 /* Has one FileIn parameter. */
39 do_tar_in (const char *dir)
45 /* "tar -C /sysroot%s -xf -" but we have to quote the dir. */
46 if (asprintf_nowarn (&cmd, "tar -C %R -xf -", dir) == -1) {
48 r = cancel_receive ();
50 if (r != -2) reply_with_perror ("asprintf");
55 fprintf (stderr, "%s\n", cmd);
57 fp = popen (cmd, "w");
60 r = cancel_receive ();
62 if (r != -2) reply_with_perror ("%s", cmd);
68 /* The semantics of fwrite are too undefined, so write to the
69 * file descriptor directly instead.
73 r = receive_file (write_cb, &fd);
74 if (r == -1) { /* write error */
75 if (cancel_receive () != -2)
76 reply_with_error ("write error on directory: %s", dir);
80 if (r == -2) { /* cancellation from library */
82 /* Do NOT send any error. */
86 if (pclose (fp) != 0) {
87 if (r == -1) /* if r == 0, file transfer ended already */
88 r = cancel_receive ();
90 reply_with_error ("tar subcommand failed on directory: %s", dir);
97 /* Has one FileOut parameter. */
99 do_tar_out (const char *dir)
104 char buf[GUESTFS_MAX_CHUNK_SIZE];
106 /* "tar -C /sysroot%s -cf - ." but we have to quote the dir. */
107 if (asprintf_nowarn (&cmd, "tar -C %R -cf - .", dir) == -1) {
108 reply_with_perror ("asprintf");
113 fprintf (stderr, "%s\n", cmd);
115 fp = popen (cmd, "r");
117 reply_with_perror ("%s", cmd);
123 /* Now we must send the reply message, before the file contents. After
124 * this there is no opportunity in the protocol to send any error
125 * message back. Instead we can only cancel the transfer.
129 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
130 if (send_file_write (buf, r) < 0) {
138 send_file_end (1); /* Cancel. */
143 if (pclose (fp) != 0) {
145 send_file_end (1); /* Cancel. */
149 if (send_file_end (0)) /* Normal end of file. */
155 /* Has one FileIn parameter. */
157 do_tXz_in (const char *dir, char filter)
163 /* "tar -C /sysroot%s -zxf -" but we have to quote the dir. */
164 if (asprintf_nowarn (&cmd, "tar -C %R -%cxf -", dir, filter) == -1) {
166 r = cancel_receive ();
168 if (r != -2) reply_with_perror ("asprintf");
173 fprintf (stderr, "%s\n", cmd);
175 fp = popen (cmd, "w");
178 r = cancel_receive ();
180 if (r != -2) reply_with_perror ("%s", cmd);
186 int fd = fileno (fp);
188 r = receive_file (write_cb, &fd);
189 if (r == -1) { /* write error */
190 r = cancel_receive ();
191 if (r != -2) reply_with_error ("write error on directory: %s", dir);
195 if (r == -2) { /* cancellation from library */
197 /* Do NOT send any error. */
201 if (pclose (fp) != 0) {
202 if (r == -1) /* if r == 0, file transfer ended already */
203 r = cancel_receive ();
205 reply_with_error ("tar subcommand failed on directory: %s", dir);
212 /* Has one FileIn parameter. */
214 do_tgz_in (const char *dir)
216 return do_tXz_in (dir, 'z');
219 /* Has one FileIn parameter. */
221 do_txz_in (const char *dir)
223 return do_tXz_in (dir, 'J');
226 /* Has one FileOut parameter. */
228 do_tXz_out (const char *dir, char filter)
233 char buf[GUESTFS_MAX_CHUNK_SIZE];
235 /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
236 if (asprintf_nowarn (&cmd, "tar -C %R -%ccf - .", dir, filter) == -1) {
237 reply_with_perror ("asprintf");
242 fprintf (stderr, "%s\n", cmd);
244 fp = popen (cmd, "r");
246 reply_with_perror ("%s", cmd);
252 /* Now we must send the reply message, before the file contents. After
253 * this there is no opportunity in the protocol to send any error
254 * message back. Instead we can only cancel the transfer.
258 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
259 if (send_file_write (buf, r) < 0) {
267 send_file_end (1); /* Cancel. */
272 if (pclose (fp) != 0) {
274 send_file_end (1); /* Cancel. */
278 if (send_file_end (0)) /* Normal end of file. */
284 /* Has one FileOut parameter. */
286 do_tgz_out (const char *dir)
288 return do_tXz_out (dir, 'z');
291 /* Has one FileOut parameter. */
293 do_txz_out (const char *dir)
295 return do_tXz_out (dir, 'J');