proto: Fix FileIn ops that abort during the chunk upload stage.
[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     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     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     cancel_receive ();
114     char *errstr = read_error_file ();
115     reply_with_error ("write error on directory: %s: %s", dir, errstr);
116     free (errstr);
117     pclose (fp);
118     return -1;
119   }
120   if (r == -2) {                /* cancellation from library */
121     pclose (fp);
122     /* Do NOT send any error. */
123     return -1;
124   }
125
126   if (pclose (fp) != 0) {
127     if (r == -1)                /* if r == 0, file transfer ended already */
128       r = cancel_receive ();
129     char *errstr = read_error_file ();
130     reply_with_error ("tar subcommand failed on directory: %s: %s",
131                       dir, errstr);
132     free (errstr);
133     return -1;
134   }
135
136   return 0;
137 }
138
139 /* Has one FileIn parameter. */
140 int
141 do_tar_in (const char *dir)
142 {
143   return do_tXz_in (dir, "");
144 }
145
146 /* Has one FileIn parameter. */
147 int
148 do_tgz_in (const char *dir)
149 {
150   return do_tXz_in (dir, "z");
151 }
152
153 /* Has one FileIn parameter. */
154 int
155 do_txz_in (const char *dir)
156 {
157   return do_tXz_in (dir, "J");
158 }
159
160 /* Has one FileOut parameter. */
161 static int
162 do_tXz_out (const char *dir, const char *filter)
163 {
164   int r;
165   FILE *fp;
166   char *cmd;
167   char buf[GUESTFS_MAX_CHUNK_SIZE];
168
169   /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
170   if (asprintf_nowarn (&cmd, "tar -C %R -%scf - .", dir, filter) == -1) {
171     reply_with_perror ("asprintf");
172     return -1;
173   }
174
175   if (verbose)
176     fprintf (stderr, "%s\n", cmd);
177
178   fp = popen (cmd, "r");
179   if (fp == NULL) {
180     reply_with_perror ("%s", cmd);
181     free (cmd);
182     return -1;
183   }
184   free (cmd);
185
186   /* Now we must send the reply message, before the file contents.  After
187    * this there is no opportunity in the protocol to send any error
188    * message back.  Instead we can only cancel the transfer.
189    */
190   reply (NULL, NULL);
191
192   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
193     if (send_file_write (buf, r) < 0) {
194       pclose (fp);
195       return -1;
196     }
197   }
198
199   if (ferror (fp)) {
200     perror (dir);
201     send_file_end (1);          /* Cancel. */
202     pclose (fp);
203     return -1;
204   }
205
206   if (pclose (fp) != 0) {
207     perror (dir);
208     send_file_end (1);          /* Cancel. */
209     return -1;
210   }
211
212   if (send_file_end (0))        /* Normal end of file. */
213     return -1;
214
215   return 0;
216 }
217
218 /* Has one FileOut parameter. */
219 int
220 do_tar_out (const char *dir)
221 {
222   return do_tXz_out (dir, "");
223 }
224
225 /* Has one FileOut parameter. */
226 int
227 do_tgz_out (const char *dir)
228 {
229   return do_tXz_out (dir, "z");
230 }
231
232 /* Has one FileOut parameter. */
233 int
234 do_txz_out (const char *dir)
235 {
236   return do_tXz_out (dir, "J");
237 }