Update FSF address.
[libguestfs.git] / daemon / tar.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009-2011 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 /* Read the error file.  Returns a string that the caller must free. */
40 static char *
41 read_error_file (char *error_file)
42 {
43   size_t len;
44   char *str;
45
46   str = read_file (error_file, &len);
47   if (str == NULL) {
48     str = strdup ("(no error)");
49     if (str == NULL) {
50       perror ("strdup");
51       exit (EXIT_FAILURE);
52     }
53     len = strlen (str);
54   }
55
56   /* Remove trailing \n character if any. */
57   if (len > 0 && str[len-1] == '\n')
58     str[--len] = '\0';
59
60   return str;                   /* caller frees */
61 }
62
63 static int
64 write_cb (void *fd_ptr, const void *buf, size_t len)
65 {
66   int fd = *(int *)fd_ptr;
67   return xwrite (fd, buf, len);
68 }
69
70 /* Has one FileIn parameter. */
71 static int
72 do_tXz_in (const char *dir, const char *filter)
73 {
74   int err, r;
75   FILE *fp;
76   char *cmd;
77   char error_file[] = "/tmp/tarXXXXXX";
78   int fd;
79
80   fd = mkstemp (error_file);
81   if (fd == -1) {
82     reply_with_perror ("mkstemp");
83     return -1;
84   }
85
86   close (fd);
87
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) {
91     err = errno;
92     r = cancel_receive ();
93     errno = err;
94     reply_with_perror ("asprintf");
95     unlink (error_file);
96     return -1;
97   }
98
99   if (verbose)
100     fprintf (stderr, "%s\n", cmd);
101
102   fp = popen (cmd, "w");
103   if (fp == NULL) {
104     err = errno;
105     r = cancel_receive ();
106     errno = err;
107     reply_with_perror ("%s", cmd);
108     unlink (error_file);
109     free (cmd);
110     return -1;
111   }
112   free (cmd);
113
114   /* The semantics of fwrite are too undefined, so write to the
115    * file descriptor directly instead.
116    */
117   fd = fileno (fp);
118
119   r = receive_file (write_cb, &fd);
120   if (r == -1) {                /* write error */
121     cancel_receive ();
122     char *errstr = read_error_file (error_file);
123     reply_with_error ("write error on directory: %s: %s", dir, errstr);
124     free (errstr);
125     unlink (error_file);
126     pclose (fp);
127     return -1;
128   }
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.
132      */
133     reply_with_error ("file upload cancelled");
134     pclose (fp);
135     unlink (error_file);
136     return -1;
137   }
138
139   if (pclose (fp) != 0) {
140     char *errstr = read_error_file (error_file);
141     reply_with_error ("tar subcommand failed on directory: %s: %s",
142                       dir, errstr);
143     free (errstr);
144     unlink (error_file);
145     return -1;
146   }
147
148   unlink (error_file);
149
150   return 0;
151 }
152
153 /* Has one FileIn parameter. */
154 int
155 do_tar_in (const char *dir)
156 {
157   return do_tXz_in (dir, "");
158 }
159
160 /* Has one FileIn parameter. */
161 int
162 do_tgz_in (const char *dir)
163 {
164   return do_tXz_in (dir, "z");
165 }
166
167 /* Has one FileIn parameter. */
168 int
169 do_txz_in (const char *dir)
170 {
171   return do_tXz_in (dir, "J");
172 }
173
174 /* Has one FileOut parameter. */
175 static int
176 do_tXz_out (const char *dir, const char *filter)
177 {
178   int r;
179   FILE *fp;
180   char *cmd;
181   char buf[GUESTFS_MAX_CHUNK_SIZE];
182
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");
186     return -1;
187   }
188
189   if (verbose)
190     fprintf (stderr, "%s\n", cmd);
191
192   fp = popen (cmd, "r");
193   if (fp == NULL) {
194     reply_with_perror ("%s", cmd);
195     free (cmd);
196     return -1;
197   }
198   free (cmd);
199
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.
203    */
204   reply (NULL, NULL);
205
206   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
207     if (send_file_write (buf, r) < 0) {
208       pclose (fp);
209       return -1;
210     }
211   }
212
213   if (ferror (fp)) {
214     perror (dir);
215     send_file_end (1);          /* Cancel. */
216     pclose (fp);
217     return -1;
218   }
219
220   if (pclose (fp) != 0) {
221     perror (dir);
222     send_file_end (1);          /* Cancel. */
223     return -1;
224   }
225
226   if (send_file_end (0))        /* Normal end of file. */
227     return -1;
228
229   return 0;
230 }
231
232 /* Has one FileOut parameter. */
233 int
234 do_tar_out (const char *dir)
235 {
236   return do_tXz_out (dir, "");
237 }
238
239 /* Has one FileOut parameter. */
240 int
241 do_tgz_out (const char *dir)
242 {
243   return do_tXz_out (dir, "z");
244 }
245
246 /* Has one FileOut parameter. */
247 int
248 do_txz_out (const char *dir)
249 {
250   return do_tXz_out (dir, "J");
251 }