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 fwrite_cb (void *fp_ptr, const void *buf, int len)
33 FILE *fp = *(FILE **)fp_ptr;
34 return fwrite (buf, len, 1, fp) == 1 ? 0 : -1;
37 /* Has one FileIn parameter. */
39 do_tar_in (const char *dir)
45 if (!root_mounted || dir[0] != '/') {
47 reply_with_error ("tar-in: 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 r = receive_file (fwrite_cb, &fp);
75 if (r == -1) { /* write error */
79 reply_with_perror ("write: %s", dir);
83 if (r == -2) { /* cancellation from library */
85 /* Do NOT send any error. */
89 if (pclose (fp) != 0) {
93 reply_with_perror ("pclose: %s", dir);
100 /* Has one FileOut parameter. */
102 do_tar_out (const char *dir)
107 char buf[GUESTFS_MAX_CHUNK_SIZE];
109 /* "tar -C /sysroot%s -cf - ." but we have to quote the dir. */
110 if (asprintf_nowarn (&cmd, "tar -C %R -cf - .", dir) == -1) {
111 reply_with_perror ("asprintf");
116 fprintf (stderr, "%s\n", cmd);
118 fp = popen (cmd, "r");
120 reply_with_perror ("%s", cmd);
126 /* Now we must send the reply message, before the file contents. After
127 * this there is no opportunity in the protocol to send any error
128 * message back. Instead we can only cancel the transfer.
132 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
133 if (send_file_write (buf, r) < 0) {
141 send_file_end (1); /* Cancel. */
146 if (pclose (fp) != 0) {
148 send_file_end (1); /* Cancel. */
152 if (send_file_end (0)) /* Normal end of file. */
158 /* Has one FileIn parameter. */
160 do_tgz_in (const char *dir)
166 if (!root_mounted || dir[0] != '/') {
168 reply_with_error ("tar-in: root must be mounted and path must be absolute");
172 /* "tar -C /sysroot%s -zxf -" but we have to quote the dir. */
173 if (asprintf_nowarn (&cmd, "tar -C %R -zxf -", dir) == -1) {
177 reply_with_perror ("asprintf");
182 fprintf (stderr, "%s\n", cmd);
184 fp = popen (cmd, "w");
189 reply_with_perror ("%s", cmd);
195 r = receive_file (fwrite_cb, &fp);
196 if (r == -1) { /* write error */
200 reply_with_perror ("write: %s", dir);
204 if (r == -2) { /* cancellation from library */
206 /* Do NOT send any error. */
210 if (pclose (fp) != 0) {
214 reply_with_perror ("pclose: %s", dir);
221 /* Has one FileOut parameter. */
223 do_tgz_out (const char *dir)
228 char buf[GUESTFS_MAX_CHUNK_SIZE];
230 /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
231 if (asprintf_nowarn (&cmd, "tar -C %R -zcf - .", dir) == -1) {
232 reply_with_perror ("asprintf");
237 fprintf (stderr, "%s\n", cmd);
239 fp = popen (cmd, "r");
241 reply_with_perror ("%s", cmd);
247 /* Now we must send the reply message, before the file contents. After
248 * this there is no opportunity in the protocol to send any error
249 * message back. Instead we can only cancel the transfer.
253 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
254 if (send_file_write (buf, r) < 0) {
262 send_file_end (1); /* Cancel. */
267 if (pclose (fp) != 0) {
269 send_file_end (1); /* Cancel. */
273 if (send_file_end (0)) /* Normal end of file. */