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"
32 /* Redirect errors from the tar command to the error file, then
33 * provide functions for reading it in. We overwrite the file each
34 * time, and since it's small and stored on the appliance we don't
35 * bother to delete it.
37 static const char *error_file = "/tmp/error";
40 read_error_file (void)
43 char *str = read_file (error_file, &len);
45 str = strdup ("(no error)");
53 /* Remove trailing \n character if any. */
54 if (len > 0 && str[len-1] == '\n')
57 return str; /* caller frees */
61 write_cb (void *fd_ptr, const void *buf, size_t len)
63 int fd = *(int *)fd_ptr;
64 return xwrite (fd, buf, len);
67 /* Has one FileIn parameter. */
69 do_tXz_in (const char *dir, const char *filter)
75 /* "tar -C /sysroot%s -xf -" but we have to quote the dir. */
76 if (asprintf_nowarn (&cmd, "tar -C %R -%sxf - 2> %s",
77 dir, filter, error_file) == -1) {
79 r = cancel_receive ();
81 if (r != -2) reply_with_perror ("asprintf");
86 fprintf (stderr, "%s\n", cmd);
88 fp = popen (cmd, "w");
91 r = cancel_receive ();
93 if (r != -2) reply_with_perror ("%s", cmd);
99 /* The semantics of fwrite are too undefined, so write to the
100 * file descriptor directly instead.
102 int fd = fileno (fp);
104 r = receive_file (write_cb, &fd);
105 if (r == -1) { /* write error */
106 if (cancel_receive () != -2) {
107 char *errstr = read_error_file ();
108 reply_with_error ("write error on directory: %s: %s", dir, errstr);
114 if (r == -2) { /* cancellation from library */
116 /* Do NOT send any error. */
120 if (pclose (fp) != 0) {
121 if (r == -1) /* if r == 0, file transfer ended already */
122 r = cancel_receive ();
124 char *errstr = read_error_file ();
125 reply_with_error ("tar subcommand failed on directory: %s: %s",
135 /* Has one FileIn parameter. */
137 do_tar_in (const char *dir)
139 return do_tXz_in (dir, "");
142 /* Has one FileIn parameter. */
144 do_tgz_in (const char *dir)
146 return do_tXz_in (dir, "z");
149 /* Has one FileIn parameter. */
151 do_txz_in (const char *dir)
153 return do_tXz_in (dir, "J");
156 /* Has one FileOut parameter. */
158 do_tXz_out (const char *dir, const char *filter)
163 char buf[GUESTFS_MAX_CHUNK_SIZE];
165 /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
166 if (asprintf_nowarn (&cmd, "tar -C %R -%scf - .", dir, filter) == -1) {
167 reply_with_perror ("asprintf");
172 fprintf (stderr, "%s\n", cmd);
174 fp = popen (cmd, "r");
176 reply_with_perror ("%s", cmd);
182 /* Now we must send the reply message, before the file contents. After
183 * this there is no opportunity in the protocol to send any error
184 * message back. Instead we can only cancel the transfer.
188 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
189 if (send_file_write (buf, r) < 0) {
197 send_file_end (1); /* Cancel. */
202 if (pclose (fp) != 0) {
204 send_file_end (1); /* Cancel. */
208 if (send_file_end (0)) /* Normal end of file. */
214 /* Has one FileOut parameter. */
216 do_tar_out (const char *dir)
218 return do_tXz_out (dir, "");
221 /* Has one FileOut parameter. */
223 do_tgz_out (const char *dir)
225 return do_tXz_out (dir, "z");
228 /* Has one FileOut parameter. */
230 do_txz_out (const char *dir)
232 return do_tXz_out (dir, "J");