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 reply_with_perror ("asprintf");
93 fprintf (stderr, "%s\n", cmd);
95 fp = popen (cmd, "w");
98 r = cancel_receive ();
100 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 */
114 char *errstr = read_error_file ();
115 reply_with_error ("write error on directory: %s: %s", dir, errstr);
120 if (r == -2) { /* cancellation from library */
121 /* This error is ignored by the library since it initiated the
122 * cancel. Nevertheless we must send an error reply here.
124 reply_with_error ("file upload cancelled");
129 if (pclose (fp) != 0) {
130 char *errstr = read_error_file ();
131 reply_with_error ("tar subcommand failed on directory: %s: %s",
140 /* Has one FileIn parameter. */
142 do_tar_in (const char *dir)
144 return do_tXz_in (dir, "");
147 /* Has one FileIn parameter. */
149 do_tgz_in (const char *dir)
151 return do_tXz_in (dir, "z");
154 /* Has one FileIn parameter. */
156 do_txz_in (const char *dir)
158 return do_tXz_in (dir, "J");
161 /* Has one FileOut parameter. */
163 do_tXz_out (const char *dir, const char *filter)
168 char buf[GUESTFS_MAX_CHUNK_SIZE];
170 /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
171 if (asprintf_nowarn (&cmd, "tar -C %R -%scf - .", dir, filter) == -1) {
172 reply_with_perror ("asprintf");
177 fprintf (stderr, "%s\n", cmd);
179 fp = popen (cmd, "r");
181 reply_with_perror ("%s", cmd);
187 /* Now we must send the reply message, before the file contents. After
188 * this there is no opportunity in the protocol to send any error
189 * message back. Instead we can only cancel the transfer.
193 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
194 if (send_file_write (buf, r) < 0) {
202 send_file_end (1); /* Cancel. */
207 if (pclose (fp) != 0) {
209 send_file_end (1); /* Cancel. */
213 if (send_file_end (0)) /* Normal end of file. */
219 /* Has one FileOut parameter. */
221 do_tar_out (const char *dir)
223 return do_tXz_out (dir, "");
226 /* Has one FileOut parameter. */
228 do_tgz_out (const char *dir)
230 return do_tXz_out (dir, "z");
233 /* Has one FileOut parameter. */
235 do_txz_out (const char *dir)
237 return do_tXz_out (dir, "J");