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 "read-file.h"
28 #include "../src/guestfs_protocol.h"
31 #include "optgroups.h"
34 optgroup_xz_available (void)
36 int r = access ("/usr/bin/xz", X_OK);
40 /* Redirect errors from the tar command to the error file, then
41 * provide functions for reading it in. We overwrite the file each
42 * time, and since it's small and stored on the appliance we don't
43 * bother to delete it.
45 static const char *error_file = "/tmp/error";
48 read_error_file (void)
51 char *str = read_file (error_file, &len);
53 str = strdup ("(no error)");
61 /* Remove trailing \n character if any. */
62 if (len > 0 && str[len-1] == '\n')
65 return str; /* caller frees */
69 write_cb (void *fd_ptr, const void *buf, size_t len)
71 int fd = *(int *)fd_ptr;
72 return xwrite (fd, buf, len);
75 /* Has one FileIn parameter. */
77 do_tXz_in (const char *dir, const char *filter)
83 /* "tar -C /sysroot%s -xf -" but we have to quote the dir. */
84 if (asprintf_nowarn (&cmd, "tar -C %R -%sxf - 2> %s",
85 dir, filter, error_file) == -1) {
87 r = cancel_receive ();
89 if (r != -2) reply_with_perror ("asprintf");
94 fprintf (stderr, "%s\n", cmd);
96 fp = popen (cmd, "w");
99 r = cancel_receive ();
101 if (r != -2) reply_with_perror ("%s", cmd);
107 /* The semantics of fwrite are too undefined, so write to the
108 * file descriptor directly instead.
110 int fd = fileno (fp);
112 r = receive_file (write_cb, &fd);
113 if (r == -1) { /* write error */
114 if (cancel_receive () != -2) {
115 char *errstr = read_error_file ();
116 reply_with_error ("write error on directory: %s: %s", dir, errstr);
122 if (r == -2) { /* cancellation from library */
124 /* Do NOT send any error. */
128 if (pclose (fp) != 0) {
129 if (r == -1) /* if r == 0, file transfer ended already */
130 r = cancel_receive ();
132 char *errstr = read_error_file ();
133 reply_with_error ("tar subcommand failed on directory: %s: %s",
143 /* Has one FileIn parameter. */
145 do_tar_in (const char *dir)
147 return do_tXz_in (dir, "");
150 /* Has one FileIn parameter. */
152 do_tgz_in (const char *dir)
154 return do_tXz_in (dir, "z");
157 /* Has one FileIn parameter. */
159 do_txz_in (const char *dir)
161 return do_tXz_in (dir, "J");
164 /* Has one FileOut parameter. */
166 do_tXz_out (const char *dir, const char *filter)
171 char buf[GUESTFS_MAX_CHUNK_SIZE];
173 /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
174 if (asprintf_nowarn (&cmd, "tar -C %R -%scf - .", dir, filter) == -1) {
175 reply_with_perror ("asprintf");
180 fprintf (stderr, "%s\n", cmd);
182 fp = popen (cmd, "r");
184 reply_with_perror ("%s", cmd);
190 /* Now we must send the reply message, before the file contents. After
191 * this there is no opportunity in the protocol to send any error
192 * message back. Instead we can only cancel the transfer.
196 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
197 if (send_file_write (buf, r) < 0) {
205 send_file_end (1); /* Cancel. */
210 if (pclose (fp) != 0) {
212 send_file_end (1); /* Cancel. */
216 if (send_file_end (0)) /* Normal end of file. */
222 /* Has one FileOut parameter. */
224 do_tar_out (const char *dir)
226 return do_tXz_out (dir, "");
229 /* Has one FileOut parameter. */
231 do_tgz_out (const char *dir)
233 return do_tXz_out (dir, "z");
236 /* Has one FileOut parameter. */
238 do_txz_out (const char *dir)
240 return do_tXz_out (dir, "J");