daemon: Print error for invalid chunk.cancel field.
[libguestfs.git] / daemon / tar.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009-2010 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <fcntl.h>
25
26 #include "read-file.h"
27
28 #include "guestfs_protocol.h"
29 #include "daemon.h"
30 #include "actions.h"
31 #include "optgroups.h"
32
33 int
34 optgroup_xz_available (void)
35 {
36   return prog_exists ("xz");
37 }
38
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.
43  */
44 static const char *error_file = "/tmp/error";
45
46 static char *
47 read_error_file (void)
48 {
49   size_t len;
50   char *str = read_file (error_file, &len);
51   if (str == NULL) {
52     str = strdup ("(no error)");
53     if (str == NULL) {
54       perror ("strdup");
55       exit (EXIT_FAILURE);
56     }
57     len = strlen (str);
58   }
59
60   /* Remove trailing \n character if any. */
61   if (len > 0 && str[len-1] == '\n')
62     str[--len] = '\0';
63
64   return str;                   /* caller frees */
65 }
66
67 static int
68 write_cb (void *fd_ptr, const void *buf, size_t len)
69 {
70   int fd = *(int *)fd_ptr;
71   return xwrite (fd, buf, len);
72 }
73
74 /* Has one FileIn parameter. */
75 static int
76 do_tXz_in (const char *dir, const char *filter)
77 {
78   int err, r;
79   FILE *fp;
80   char *cmd;
81
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) {
85     err = errno;
86     r = cancel_receive ();
87     errno = err;
88     if (r != -2) reply_with_perror ("asprintf");
89     return -1;
90   }
91
92   if (verbose)
93     fprintf (stderr, "%s\n", cmd);
94
95   fp = popen (cmd, "w");
96   if (fp == NULL) {
97     err = errno;
98     r = cancel_receive ();
99     errno = err;
100     if (r != -2) reply_with_perror ("%s", cmd);
101     free (cmd);
102     return -1;
103   }
104   free (cmd);
105
106   /* The semantics of fwrite are too undefined, so write to the
107    * file descriptor directly instead.
108    */
109   int fd = fileno (fp);
110
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);
116       free (errstr);
117     }
118     pclose (fp);
119     return -1;
120   }
121   if (r == -2) {                /* cancellation from library */
122     pclose (fp);
123     /* Do NOT send any error. */
124     return -1;
125   }
126
127   if (pclose (fp) != 0) {
128     if (r == -1)                /* if r == 0, file transfer ended already */
129       r = cancel_receive ();
130     if (r != -2) {
131       char *errstr = read_error_file ();
132       reply_with_error ("tar subcommand failed on directory: %s: %s",
133                         dir, errstr);
134       free (errstr);
135     }
136     return -1;
137   }
138
139   return 0;
140 }
141
142 /* Has one FileIn parameter. */
143 int
144 do_tar_in (const char *dir)
145 {
146   return do_tXz_in (dir, "");
147 }
148
149 /* Has one FileIn parameter. */
150 int
151 do_tgz_in (const char *dir)
152 {
153   return do_tXz_in (dir, "z");
154 }
155
156 /* Has one FileIn parameter. */
157 int
158 do_txz_in (const char *dir)
159 {
160   return do_tXz_in (dir, "J");
161 }
162
163 /* Has one FileOut parameter. */
164 static int
165 do_tXz_out (const char *dir, const char *filter)
166 {
167   int r;
168   FILE *fp;
169   char *cmd;
170   char buf[GUESTFS_MAX_CHUNK_SIZE];
171
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");
175     return -1;
176   }
177
178   if (verbose)
179     fprintf (stderr, "%s\n", cmd);
180
181   fp = popen (cmd, "r");
182   if (fp == NULL) {
183     reply_with_perror ("%s", cmd);
184     free (cmd);
185     return -1;
186   }
187   free (cmd);
188
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.
192    */
193   reply (NULL, NULL);
194
195   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
196     if (send_file_write (buf, r) < 0) {
197       pclose (fp);
198       return -1;
199     }
200   }
201
202   if (ferror (fp)) {
203     perror (dir);
204     send_file_end (1);          /* Cancel. */
205     pclose (fp);
206     return -1;
207   }
208
209   if (pclose (fp) != 0) {
210     perror (dir);
211     send_file_end (1);          /* Cancel. */
212     return -1;
213   }
214
215   if (send_file_end (0))        /* Normal end of file. */
216     return -1;
217
218   return 0;
219 }
220
221 /* Has one FileOut parameter. */
222 int
223 do_tar_out (const char *dir)
224 {
225   return do_tXz_out (dir, "");
226 }
227
228 /* Has one FileOut parameter. */
229 int
230 do_tgz_out (const char *dir)
231 {
232   return do_tXz_out (dir, "z");
233 }
234
235 /* Has one FileOut parameter. */
236 int
237 do_txz_out (const char *dir)
238 {
239   return do_tXz_out (dir, "J");
240 }