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 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);
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) == -1) {
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];
112 /* "tar -C /sysroot%s -cf - ." but we have to quote the dir. */
113 len = 2 * strlen (dir) + 32;
116 reply_with_perror ("malloc");
119 strcpy (cmd, "tar -C /sysroot");
120 shell_quote (cmd+15, len-15, dir);
121 strcat (cmd, " -cf - .");
123 fp = popen (cmd, "r");
125 reply_with_perror ("%s", cmd);
129 /* Now we must send the reply message, before the file contents. After
130 * this there is no opportunity in the protocol to send any error
131 * message back. Instead we can only cancel the transfer.
135 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
136 if (send_file_write (buf, r) < 0) {
144 send_file_end (1); /* Cancel. */
149 if (pclose (fp) == -1) {
151 send_file_end (1); /* Cancel. */
155 send_file_end (0); /* Normal end of file. */
159 /* Has one FileIn parameter. */
161 do_tgz_in (const char *dir)
167 if (!root_mounted || dir[0] != '/') {
169 reply_with_error ("tar-in: root must be mounted and path must be absolute");
173 /* "tar -C /sysroot%s -zxf -" but we have to quote the dir. */
174 len = 2 * strlen (dir) + 32;
180 reply_with_perror ("malloc");
183 strcpy (cmd, "tar -C /sysroot");
184 shell_quote (cmd+15, len-15, dir);
185 strcat (cmd, " -zxf -");
187 fp = popen (cmd, "w");
192 reply_with_perror ("%s", cmd);
196 r = receive_file (fwrite_cb, &fp);
197 if (r == -1) { /* write error */
201 reply_with_perror ("write: %s", dir);
205 if (r == -2) { /* cancellation from library */
207 /* Do NOT send any error. */
211 if (pclose (fp) == -1) {
215 reply_with_perror ("pclose: %s", dir);
222 /* Has one FileOut parameter. */
224 do_tgz_out (const char *dir)
229 char buf[GUESTFS_MAX_CHUNK_SIZE];
234 /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
235 len = 2 * strlen (dir) + 32;
238 reply_with_perror ("malloc");
241 strcpy (cmd, "tar -C /sysroot");
242 shell_quote (cmd+15, len-15, dir);
243 strcat (cmd, " -zcf - .");
245 fp = popen (cmd, "r");
247 reply_with_perror ("%s", cmd);
251 /* Now we must send the reply message, before the file contents. After
252 * this there is no opportunity in the protocol to send any error
253 * message back. Instead we can only cancel the transfer.
257 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
258 if (send_file_write (buf, r) < 0) {
266 send_file_end (1); /* Cancel. */
271 if (pclose (fp) == -1) {
273 send_file_end (1); /* Cancel. */
277 send_file_end (0); /* Normal end of file. */