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) + 32;
58 reply_with_perror ("malloc");
61 strcpy (cmd, "tar -C /sysroot");
62 shell_quote (cmd+15, len-15, dir);
63 strcat (cmd, " -xf -");
65 fp = popen (cmd, "w");
70 reply_with_perror ("%s", cmd);
76 r = receive_file (fwrite_cb, &fp);
77 if (r == -1) { /* write error */
81 reply_with_perror ("write: %s", dir);
85 if (r == -2) { /* cancellation from library */
87 /* Do NOT send any error. */
91 if (pclose (fp) == -1) {
95 reply_with_perror ("pclose: %s", dir);
102 /* Has one FileOut parameter. */
104 do_tar_out (char *dir)
109 char buf[GUESTFS_MAX_CHUNK_SIZE];
114 /* "tar -C /sysroot%s -cf - ." but we have to quote the dir. */
115 len = 2 * strlen (dir) + 32;
118 reply_with_perror ("malloc");
121 strcpy (cmd, "tar -C /sysroot");
122 shell_quote (cmd+15, len-15, dir);
123 strcat (cmd, " -cf - .");
125 fp = popen (cmd, "r");
127 reply_with_perror ("%s", cmd);
133 /* Now we must send the reply message, before the file contents. After
134 * this there is no opportunity in the protocol to send any error
135 * message back. Instead we can only cancel the transfer.
139 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
140 if (send_file_write (buf, r) < 0) {
148 send_file_end (1); /* Cancel. */
153 if (pclose (fp) == -1) {
155 send_file_end (1); /* Cancel. */
159 send_file_end (0); /* Normal end of file. */
163 /* Has one FileIn parameter. */
165 do_tgz_in (char *dir)
171 if (!root_mounted || dir[0] != '/') {
173 reply_with_error ("tar-in: root must be mounted and path must be absolute");
177 /* "tar -C /sysroot%s -zxf -" but we have to quote the dir. */
178 len = 2 * strlen (dir) + 32;
184 reply_with_perror ("malloc");
187 strcpy (cmd, "tar -C /sysroot");
188 shell_quote (cmd+15, len-15, dir);
189 strcat (cmd, " -zxf -");
191 fp = popen (cmd, "w");
196 reply_with_perror ("%s", cmd);
202 r = receive_file (fwrite_cb, &fp);
203 if (r == -1) { /* write error */
207 reply_with_perror ("write: %s", dir);
211 if (r == -2) { /* cancellation from library */
213 /* Do NOT send any error. */
217 if (pclose (fp) == -1) {
221 reply_with_perror ("pclose: %s", dir);
228 /* Has one FileOut parameter. */
230 do_tgz_out (char *dir)
235 char buf[GUESTFS_MAX_CHUNK_SIZE];
240 /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
241 len = 2 * strlen (dir) + 32;
244 reply_with_perror ("malloc");
247 strcpy (cmd, "tar -C /sysroot");
248 shell_quote (cmd+15, len-15, dir);
249 strcat (cmd, " -zcf - .");
251 fp = popen (cmd, "r");
253 reply_with_perror ("%s", cmd);
259 /* Now we must send the reply message, before the file contents. After
260 * this there is no opportunity in the protocol to send any error
261 * message back. Instead we can only cancel the transfer.
265 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
266 if (send_file_write (buf, r) < 0) {
274 send_file_end (1); /* Cancel. */
279 if (pclose (fp) == -1) {
281 send_file_end (1); /* Cancel. */
285 send_file_end (0); /* Normal end of file. */