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_tXz_in (const char *dir, const char *filter)
45 /* "tar -C /sysroot%s -xf -" but we have to quote the dir. */
46 if (asprintf_nowarn (&cmd, "tar -C %R -%sxf -",
49 r = cancel_receive ();
51 if (r != -2) reply_with_perror ("asprintf");
56 fprintf (stderr, "%s\n", cmd);
58 fp = popen (cmd, "w");
61 r = cancel_receive ();
63 if (r != -2) reply_with_perror ("%s", cmd);
69 /* The semantics of fwrite are too undefined, so write to the
70 * file descriptor directly instead.
74 r = receive_file (write_cb, &fd);
75 if (r == -1) { /* write error */
76 if (cancel_receive () != -2)
77 reply_with_error ("write error on directory: %s", dir);
81 if (r == -2) { /* cancellation from library */
83 /* Do NOT send any error. */
87 if (pclose (fp) != 0) {
88 if (r == -1) /* if r == 0, file transfer ended already */
89 r = cancel_receive ();
91 reply_with_error ("tar subcommand failed on directory: %s", dir);
98 /* Has one FileIn parameter. */
100 do_tar_in (const char *dir)
102 return do_tXz_in (dir, "");
105 /* Has one FileIn parameter. */
107 do_tgz_in (const char *dir)
109 return do_tXz_in (dir, "z");
112 /* Has one FileIn parameter. */
114 do_txz_in (const char *dir)
116 return do_tXz_in (dir, "J");
119 /* Has one FileOut parameter. */
121 do_tXz_out (const char *dir, const char *filter)
126 char buf[GUESTFS_MAX_CHUNK_SIZE];
128 /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
129 if (asprintf_nowarn (&cmd, "tar -C %R -%scf - .", dir, filter) == -1) {
130 reply_with_perror ("asprintf");
135 fprintf (stderr, "%s\n", cmd);
137 fp = popen (cmd, "r");
139 reply_with_perror ("%s", cmd);
145 /* Now we must send the reply message, before the file contents. After
146 * this there is no opportunity in the protocol to send any error
147 * message back. Instead we can only cancel the transfer.
151 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
152 if (send_file_write (buf, r) < 0) {
160 send_file_end (1); /* Cancel. */
165 if (pclose (fp) != 0) {
167 send_file_end (1); /* Cancel. */
171 if (send_file_end (0)) /* Normal end of file. */
177 /* Has one FileOut parameter. */
179 do_tar_out (const char *dir)
181 return do_tXz_out (dir, "");
184 /* Has one FileOut parameter. */
186 do_tgz_out (const char *dir)
188 return do_tXz_out (dir, "z");
191 /* Has one FileOut parameter. */
193 do_txz_out (const char *dir)
195 return do_tXz_out (dir, "J");