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 if (!root_mounted || dir[0] != '/') {
47 reply_with_error ("root must be mounted and path must be absolute");
51 /* "tar -C /sysroot%s -xf -" but we have to quote the dir. */
52 if (asprintf_nowarn (&cmd, "tar -C %R -xf -", dir) == -1) {
56 reply_with_perror ("asprintf");
61 fprintf (stderr, "%s\n", cmd);
63 fp = popen (cmd, "w");
68 reply_with_perror ("%s", cmd);
74 /* The semantics of fwrite are too undefined, so write to the
75 * file descriptor directly instead.
79 r = receive_file (write_cb, &fd);
80 if (r == -1) { /* write error */
82 reply_with_error ("write error on directory: %s", dir);
86 if (r == -2) { /* cancellation from library */
88 /* Do NOT send any error. */
92 if (pclose (fp) != 0) {
93 if (r == -1) /* if r == 0, file transfer ended already */
95 reply_with_error ("tar subcommand failed on directory: %s", dir);
102 /* Has one FileOut parameter. */
104 do_tar_out (const char *dir)
109 char buf[GUESTFS_MAX_CHUNK_SIZE];
111 /* "tar -C /sysroot%s -cf - ." but we have to quote the dir. */
112 if (asprintf_nowarn (&cmd, "tar -C %R -cf - .", dir) == -1) {
113 reply_with_perror ("asprintf");
118 fprintf (stderr, "%s\n", cmd);
120 fp = popen (cmd, "r");
122 reply_with_perror ("%s", cmd);
128 /* Now we must send the reply message, before the file contents. After
129 * this there is no opportunity in the protocol to send any error
130 * message back. Instead we can only cancel the transfer.
134 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
135 if (send_file_write (buf, r) < 0) {
143 send_file_end (1); /* Cancel. */
148 if (pclose (fp) != 0) {
150 send_file_end (1); /* Cancel. */
154 if (send_file_end (0)) /* Normal end of file. */
160 /* Has one FileIn parameter. */
162 do_tXz_in (const char *dir, char filter)
168 if (!root_mounted || dir[0] != '/') {
170 reply_with_error ("root must be mounted and path must be absolute");
174 /* "tar -C /sysroot%s -zxf -" but we have to quote the dir. */
175 if (asprintf_nowarn (&cmd, "tar -C %R -%cxf -", dir, filter) == -1) {
179 reply_with_perror ("asprintf");
184 fprintf (stderr, "%s\n", cmd);
186 fp = popen (cmd, "w");
191 reply_with_perror ("%s", cmd);
197 int fd = fileno (fp);
199 r = receive_file (write_cb, &fd);
200 if (r == -1) { /* write error */
202 reply_with_error ("write error on directory: %s", dir);
206 if (r == -2) { /* cancellation from library */
208 /* Do NOT send any error. */
212 if (pclose (fp) != 0) {
213 if (r == -1) /* if r == 0, file transfer ended already */
215 reply_with_error ("tar subcommand failed on directory: %s", dir);
222 /* Has one FileIn parameter. */
224 do_tgz_in (const char *dir)
226 return do_tXz_in (dir, 'z');
229 /* Has one FileIn parameter. */
231 do_txz_in (const char *dir)
233 return do_tXz_in (dir, 'J');
236 /* Has one FileOut parameter. */
238 do_tXz_out (const char *dir, char filter)
243 char buf[GUESTFS_MAX_CHUNK_SIZE];
245 /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
246 if (asprintf_nowarn (&cmd, "tar -C %R -%ccf - .", dir, filter) == -1) {
247 reply_with_perror ("asprintf");
252 fprintf (stderr, "%s\n", cmd);
254 fp = popen (cmd, "r");
256 reply_with_perror ("%s", cmd);
262 /* Now we must send the reply message, before the file contents. After
263 * this there is no opportunity in the protocol to send any error
264 * message back. Instead we can only cancel the transfer.
268 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
269 if (send_file_write (buf, r) < 0) {
277 send_file_end (1); /* Cancel. */
282 if (pclose (fp) != 0) {
284 send_file_end (1); /* Cancel. */
288 if (send_file_end (0)) /* Normal end of file. */
294 /* Has one FileOut parameter. */
296 do_tgz_out (const char *dir)
298 return do_tXz_out (dir, 'z');
301 /* Has one FileOut parameter. */
303 do_txz_out (const char *dir)
305 return do_tXz_out (dir, 'J');