1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 /* Read the error file. Returns a string that the caller must free. */
41 read_error_file (char *error_file)
46 str = read_file (error_file, &len);
48 str = strdup ("(no error)");
56 /* Remove trailing \n character if any. */
57 if (len > 0 && str[len-1] == '\n')
60 return str; /* caller frees */
64 write_cb (void *fd_ptr, const void *buf, size_t len)
66 int fd = *(int *)fd_ptr;
67 return xwrite (fd, buf, len);
70 /* Has one FileIn parameter. */
72 do_tXz_in (const char *dir, const char *filter)
77 char error_file[] = "/tmp/tarXXXXXX";
80 fd = mkstemp (error_file);
82 reply_with_perror ("mkstemp");
88 /* "tar -C /sysroot%s -xf -" but we have to quote the dir. */
89 if (asprintf_nowarn (&cmd, "tar -C %R -%sxf - 2> %s",
90 dir, filter, error_file) == -1) {
92 r = cancel_receive ();
94 reply_with_perror ("asprintf");
100 fprintf (stderr, "%s\n", cmd);
102 fp = popen (cmd, "w");
105 r = cancel_receive ();
107 reply_with_perror ("%s", cmd);
114 /* The semantics of fwrite are too undefined, so write to the
115 * file descriptor directly instead.
119 r = receive_file (write_cb, &fd);
120 if (r == -1) { /* write error */
122 char *errstr = read_error_file (error_file);
123 reply_with_error ("write error on directory: %s: %s", dir, errstr);
129 if (r == -2) { /* cancellation from library */
130 /* This error is ignored by the library since it initiated the
131 * cancel. Nevertheless we must send an error reply here.
133 reply_with_error ("file upload cancelled");
139 if (pclose (fp) != 0) {
140 char *errstr = read_error_file (error_file);
141 reply_with_error ("tar subcommand failed on directory: %s: %s",
153 /* Has one FileIn parameter. */
155 do_tar_in (const char *dir)
157 return do_tXz_in (dir, "");
160 /* Has one FileIn parameter. */
162 do_tgz_in (const char *dir)
164 return do_tXz_in (dir, "z");
167 /* Has one FileIn parameter. */
169 do_txz_in (const char *dir)
171 return do_tXz_in (dir, "J");
174 /* Has one FileOut parameter. */
176 do_tXz_out (const char *dir, const char *filter)
181 char buf[GUESTFS_MAX_CHUNK_SIZE];
183 /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
184 if (asprintf_nowarn (&cmd, "tar -C %R -%scf - .", dir, filter) == -1) {
185 reply_with_perror ("asprintf");
190 fprintf (stderr, "%s\n", cmd);
192 fp = popen (cmd, "r");
194 reply_with_perror ("%s", cmd);
200 /* Now we must send the reply message, before the file contents. After
201 * this there is no opportunity in the protocol to send any error
202 * message back. Instead we can only cancel the transfer.
206 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
207 if (send_file_write (buf, r) < 0) {
215 send_file_end (1); /* Cancel. */
220 if (pclose (fp) != 0) {
222 send_file_end (1); /* Cancel. */
226 if (send_file_end (0)) /* Normal end of file. */
232 /* Has one FileOut parameter. */
234 do_tar_out (const char *dir)
236 return do_tXz_out (dir, "");
239 /* Has one FileOut parameter. */
241 do_tgz_out (const char *dir)
243 return do_tXz_out (dir, "z");
246 /* Has one FileOut parameter. */
248 do_txz_out (const char *dir)
250 return do_tXz_out (dir, "J");