1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009-2010 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 "read-file.h"
28 #include "guestfs_protocol.h"
31 #include "optgroups.h"
34 optgroup_xz_available (void)
36 return prog_exists ("xz");
39 /* Redirect errors from the tar command to the error file, then
40 * provide functions for reading it in. We overwrite the file each
41 * time, and since it's small and stored on the appliance we don't
42 * bother to delete it.
44 static const char *error_file = "/tmp/error";
47 read_error_file (void)
50 char *str = read_file (error_file, &len);
52 str = strdup ("(no error)");
60 /* Remove trailing \n character if any. */
61 if (len > 0 && str[len-1] == '\n')
64 return str; /* caller frees */
68 write_cb (void *fd_ptr, const void *buf, size_t len)
70 int fd = *(int *)fd_ptr;
71 return xwrite (fd, buf, len);
74 /* Has one FileIn parameter. */
76 do_tXz_in (const char *dir, const char *filter)
82 /* "tar -C /sysroot%s -xf -" but we have to quote the dir. */
83 if (asprintf_nowarn (&cmd, "tar -C %R -%sxf - 2> %s",
84 dir, filter, error_file) == -1) {
86 r = cancel_receive ();
88 if (r != -2) reply_with_perror ("asprintf");
93 fprintf (stderr, "%s\n", cmd);
95 fp = popen (cmd, "w");
98 r = cancel_receive ();
100 if (r != -2) reply_with_perror ("%s", cmd);
106 /* The semantics of fwrite are too undefined, so write to the
107 * file descriptor directly instead.
109 int fd = fileno (fp);
111 r = receive_file (write_cb, &fd);
112 if (r == -1) { /* write error */
113 if (cancel_receive () != -2) {
114 char *errstr = read_error_file ();
115 reply_with_error ("write error on directory: %s: %s", dir, errstr);
121 if (r == -2) { /* cancellation from library */
123 /* Do NOT send any error. */
127 if (pclose (fp) != 0) {
128 if (r == -1) /* if r == 0, file transfer ended already */
129 r = cancel_receive ();
131 char *errstr = read_error_file ();
132 reply_with_error ("tar subcommand failed on directory: %s: %s",
142 /* Has one FileIn parameter. */
144 do_tar_in (const char *dir)
146 return do_tXz_in (dir, "");
149 /* Has one FileIn parameter. */
151 do_tgz_in (const char *dir)
153 return do_tXz_in (dir, "z");
156 /* Has one FileIn parameter. */
158 do_txz_in (const char *dir)
160 return do_tXz_in (dir, "J");
163 /* Has one FileOut parameter. */
165 do_tXz_out (const char *dir, const char *filter)
170 char buf[GUESTFS_MAX_CHUNK_SIZE];
172 /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
173 if (asprintf_nowarn (&cmd, "tar -C %R -%scf - .", dir, filter) == -1) {
174 reply_with_perror ("asprintf");
179 fprintf (stderr, "%s\n", cmd);
181 fp = popen (cmd, "r");
183 reply_with_perror ("%s", cmd);
189 /* Now we must send the reply message, before the file contents. After
190 * this there is no opportunity in the protocol to send any error
191 * message back. Instead we can only cancel the transfer.
195 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
196 if (send_file_write (buf, r) < 0) {
204 send_file_end (1); /* Cancel. */
209 if (pclose (fp) != 0) {
211 send_file_end (1); /* Cancel. */
215 if (send_file_end (0)) /* Normal end of file. */
221 /* Has one FileOut parameter. */
223 do_tar_out (const char *dir)
225 return do_tXz_out (dir, "");
228 /* Has one FileOut parameter. */
230 do_tgz_out (const char *dir)
232 return do_tXz_out (dir, "z");
235 /* Has one FileOut parameter. */
237 do_txz_out (const char *dir)
239 return do_tXz_out (dir, "J");