Update TODO.
[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     /* This error is ignored by the library since it initiated the
122      * cancel.  Nevertheless we must send an error reply here.
123      */
124     reply_with_error ("file upload cancelled");
125     pclose (fp);
126     return -1;
127   }
128
129   if (pclose (fp) != 0) {
130     char *errstr = read_error_file ();
131     reply_with_error ("tar subcommand failed on directory: %s: %s",
132                       dir, errstr);
133     free (errstr);
134     return -1;
135   }
136
137   return 0;
138 }
139
140 /* Has one FileIn parameter. */
141 int
142 do_tar_in (const char *dir)
143 {
144   return do_tXz_in (dir, "");
145 }
146
147 /* Has one FileIn parameter. */
148 int
149 do_tgz_in (const char *dir)
150 {
151   return do_tXz_in (dir, "z");
152 }
153
154 /* Has one FileIn parameter. */
155 int
156 do_txz_in (const char *dir)
157 {
158   return do_tXz_in (dir, "J");
159 }
160
161 /* Has one FileOut parameter. */
162 static int
163 do_tXz_out (const char *dir, const char *filter)
164 {
165   int r;
166   FILE *fp;
167   char *cmd;
168   char buf[GUESTFS_MAX_CHUNK_SIZE];
169
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");
173     return -1;
174   }
175
176   if (verbose)
177     fprintf (stderr, "%s\n", cmd);
178
179   fp = popen (cmd, "r");
180   if (fp == NULL) {
181     reply_with_perror ("%s", cmd);
182     free (cmd);
183     return -1;
184   }
185   free (cmd);
186
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.
190    */
191   reply (NULL, NULL);
192
193   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
194     if (send_file_write (buf, r) < 0) {
195       pclose (fp);
196       return -1;
197     }
198   }
199
200   if (ferror (fp)) {
201     perror (dir);
202     send_file_end (1);          /* Cancel. */
203     pclose (fp);
204     return -1;
205   }
206
207   if (pclose (fp) != 0) {
208     perror (dir);
209     send_file_end (1);          /* Cancel. */
210     return -1;
211   }
212
213   if (send_file_end (0))        /* Normal end of file. */
214     return -1;
215
216   return 0;
217 }
218
219 /* Has one FileOut parameter. */
220 int
221 do_tar_out (const char *dir)
222 {
223   return do_tXz_out (dir, "");
224 }
225
226 /* Has one FileOut parameter. */
227 int
228 do_tgz_out (const char *dir)
229 {
230   return do_tXz_out (dir, "z");
231 }
232
233 /* Has one FileOut parameter. */
234 int
235 do_txz_out (const char *dir)
236 {
237   return do_tXz_out (dir, "J");
238 }