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. */
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 len = 2 * strlen (dir) + sysroot_len + 32;
58 reply_with_perror ("malloc");
61 strcpy (cmd, "tar -C ");
62 strcat (cmd, sysroot);
63 shell_quote (cmd+15, len-15, dir);
64 strcat (cmd, " -xf -");
66 fp = popen (cmd, "w");
71 reply_with_perror ("%s", cmd);
77 r = receive_file (fwrite_cb, &fp);
78 if (r == -1) { /* write error */
82 reply_with_perror ("write: %s", dir);
86 if (r == -2) { /* cancellation from library */
88 /* Do NOT send any error. */
92 if (pclose (fp) != 0) {
96 reply_with_perror ("pclose: %s", dir);
103 /* Has one FileOut parameter. */
105 do_tar_out (char *dir)
110 char buf[GUESTFS_MAX_CHUNK_SIZE];
115 /* "tar -C /sysroot%s -cf - ." but we have to quote the dir. */
116 len = 2 * strlen (dir) + sysroot_len + 32;
119 reply_with_perror ("malloc");
122 strcpy (cmd, "tar -C ");
123 strcat (cmd, sysroot);
124 shell_quote (cmd+15, len-15, dir);
125 strcat (cmd, " -cf - .");
127 fp = popen (cmd, "r");
129 reply_with_perror ("%s", cmd);
135 /* Now we must send the reply message, before the file contents. After
136 * this there is no opportunity in the protocol to send any error
137 * message back. Instead we can only cancel the transfer.
141 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
142 if (send_file_write (buf, r) < 0) {
150 send_file_end (1); /* Cancel. */
155 if (pclose (fp) != 0) {
157 send_file_end (1); /* Cancel. */
161 send_file_end (0); /* Normal end of file. */
165 /* Has one FileIn parameter. */
167 do_tgz_in (char *dir)
173 if (!root_mounted || dir[0] != '/') {
175 reply_with_error ("tar-in: root must be mounted and path must be absolute");
179 /* "tar -C /sysroot%s -zxf -" but we have to quote the dir. */
180 len = 2 * strlen (dir) + sysroot_len + 32;
186 reply_with_perror ("malloc");
189 strcpy (cmd, "tar -C ");
190 strcat (cmd, sysroot);
191 shell_quote (cmd+15, len-15, dir);
192 strcat (cmd, " -zxf -");
194 fp = popen (cmd, "w");
199 reply_with_perror ("%s", cmd);
205 r = receive_file (fwrite_cb, &fp);
206 if (r == -1) { /* write error */
210 reply_with_perror ("write: %s", dir);
214 if (r == -2) { /* cancellation from library */
216 /* Do NOT send any error. */
220 if (pclose (fp) != 0) {
224 reply_with_perror ("pclose: %s", dir);
231 /* Has one FileOut parameter. */
233 do_tgz_out (char *dir)
238 char buf[GUESTFS_MAX_CHUNK_SIZE];
243 /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
244 len = 2 * strlen (dir) + sysroot_len + 32;
247 reply_with_perror ("malloc");
250 strcpy (cmd, "tar -C ");
251 strcat (cmd, sysroot);
252 shell_quote (cmd+15, len-15, dir);
253 strcat (cmd, " -zcf - .");
255 fp = popen (cmd, "r");
257 reply_with_perror ("%s", cmd);
263 /* Now we must send the reply message, before the file contents. After
264 * this there is no opportunity in the protocol to send any error
265 * message back. Instead we can only cancel the transfer.
269 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
270 if (send_file_write (buf, r) < 0) {
278 send_file_end (1); /* Cancel. */
283 if (pclose (fp) != 0) {
285 send_file_end (1); /* Cancel. */
289 send_file_end (0); /* Normal end of file. */