Fix tar-in command hangs when running out of disk space (RHBZ#580246).
[libguestfs.git] / daemon / tar.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 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 "../src/guestfs_protocol.h"
27 #include "daemon.h"
28 #include "actions.h"
29
30 static int
31 fwrite_cb (void *fp_ptr, const void *buf, int len)
32 {
33   FILE *fp = *(FILE **)fp_ptr;
34   return fwrite (buf, len, 1, fp) == 1 ? 0 : -1;
35 }
36
37 /* Has one FileIn parameter. */
38 int
39 do_tar_in (const char *dir)
40 {
41   int err, r;
42   FILE *fp;
43   char *cmd;
44
45   if (!root_mounted || dir[0] != '/') {
46     cancel_receive ();
47     reply_with_error ("root must be mounted and path must be absolute");
48     return -1;
49   }
50
51   /* "tar -C /sysroot%s -xf -" but we have to quote the dir. */
52   if (asprintf_nowarn (&cmd, "tar -C %R -xf -", dir) == -1) {
53     err = errno;
54     cancel_receive ();
55     errno = err;
56     reply_with_perror ("asprintf");
57     return -1;
58   }
59
60   if (verbose)
61     fprintf (stderr, "%s\n", cmd);
62
63   fp = popen (cmd, "w");
64   if (fp == NULL) {
65     err = errno;
66     cancel_receive ();
67     errno = err;
68     reply_with_perror ("%s", cmd);
69     free (cmd);
70     return -1;
71   }
72   free (cmd);
73
74   r = receive_file (fwrite_cb, &fp);
75   if (r == -1) {                /* write error */
76     err = errno;
77     cancel_receive ();
78     errno = err;
79     reply_with_perror ("write: %s", dir);
80     pclose (fp);
81     return -1;
82   }
83   if (r == -2) {                /* cancellation from library */
84     pclose (fp);
85     /* Do NOT send any error. */
86     return -1;
87   }
88
89   if (pclose (fp) != 0) {
90     err = errno;
91     if (r == -1)                /* if r == 0, file transfer ended already */
92       cancel_receive ();
93     errno = err;
94     reply_with_perror ("pclose: %s", dir);
95     return -1;
96   }
97
98   return 0;
99 }
100
101 /* Has one FileOut parameter. */
102 int
103 do_tar_out (const char *dir)
104 {
105   int r;
106   FILE *fp;
107   char *cmd;
108   char buf[GUESTFS_MAX_CHUNK_SIZE];
109
110   /* "tar -C /sysroot%s -cf - ." but we have to quote the dir. */
111   if (asprintf_nowarn (&cmd, "tar -C %R -cf - .", dir) == -1) {
112     reply_with_perror ("asprintf");
113     return -1;
114   }
115
116   if (verbose)
117     fprintf (stderr, "%s\n", cmd);
118
119   fp = popen (cmd, "r");
120   if (fp == NULL) {
121     reply_with_perror ("%s", cmd);
122     free (cmd);
123     return -1;
124   }
125   free (cmd);
126
127   /* Now we must send the reply message, before the file contents.  After
128    * this there is no opportunity in the protocol to send any error
129    * message back.  Instead we can only cancel the transfer.
130    */
131   reply (NULL, NULL);
132
133   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
134     if (send_file_write (buf, r) < 0) {
135       pclose (fp);
136       return -1;
137     }
138   }
139
140   if (ferror (fp)) {
141     perror (dir);
142     send_file_end (1);          /* Cancel. */
143     pclose (fp);
144     return -1;
145   }
146
147   if (pclose (fp) != 0) {
148     perror (dir);
149     send_file_end (1);          /* Cancel. */
150     return -1;
151   }
152
153   if (send_file_end (0))        /* Normal end of file. */
154     return -1;
155
156   return 0;
157 }
158
159 /* Has one FileIn parameter. */
160 int
161 do_tgz_in (const char *dir)
162 {
163   int err, r;
164   FILE *fp;
165   char *cmd;
166
167   if (!root_mounted || dir[0] != '/') {
168     cancel_receive ();
169     reply_with_error ("root must be mounted and path must be absolute");
170     return -1;
171   }
172
173   /* "tar -C /sysroot%s -zxf -" but we have to quote the dir. */
174   if (asprintf_nowarn (&cmd, "tar -C %R -zxf -", dir) == -1) {
175     err = errno;
176     cancel_receive ();
177     errno = err;
178     reply_with_perror ("asprintf");
179     return -1;
180   }
181
182   if (verbose)
183     fprintf (stderr, "%s\n", cmd);
184
185   fp = popen (cmd, "w");
186   if (fp == NULL) {
187     err = errno;
188     cancel_receive ();
189     errno = err;
190     reply_with_perror ("%s", cmd);
191     free (cmd);
192     return -1;
193   }
194   free (cmd);
195
196   r = receive_file (fwrite_cb, &fp);
197   if (r == -1) {                /* write error */
198     err = errno;
199     cancel_receive ();
200     errno = err;
201     reply_with_perror ("write: %s", dir);
202     pclose (fp);
203     return -1;
204   }
205   if (r == -2) {                /* cancellation from library */
206     pclose (fp);
207     /* Do NOT send any error. */
208     return -1;
209   }
210
211   if (pclose (fp) != 0) {
212     err = errno;
213     if (r == -1)                /* if r == 0, file transfer ended already */
214       cancel_receive ();
215     errno = err;
216     reply_with_perror ("pclose: %s", dir);
217     return -1;
218   }
219
220   return 0;
221 }
222
223 /* Has one FileOut parameter. */
224 int
225 do_tgz_out (const char *dir)
226 {
227   int r;
228   FILE *fp;
229   char *cmd;
230   char buf[GUESTFS_MAX_CHUNK_SIZE];
231
232   /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
233   if (asprintf_nowarn (&cmd, "tar -C %R -zcf - .", dir) == -1) {
234     reply_with_perror ("asprintf");
235     return -1;
236   }
237
238   if (verbose)
239     fprintf (stderr, "%s\n", cmd);
240
241   fp = popen (cmd, "r");
242   if (fp == NULL) {
243     reply_with_perror ("%s", cmd);
244     free (cmd);
245     return -1;
246   }
247   free (cmd);
248
249   /* Now we must send the reply message, before the file contents.  After
250    * this there is no opportunity in the protocol to send any error
251    * message back.  Instead we can only cancel the transfer.
252    */
253   reply (NULL, NULL);
254
255   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
256     if (send_file_write (buf, r) < 0) {
257       pclose (fp);
258       return -1;
259     }
260   }
261
262   if (ferror (fp)) {
263     perror (dir);
264     send_file_end (1);          /* Cancel. */
265     pclose (fp);
266     return -1;
267   }
268
269   if (pclose (fp) != 0) {
270     perror (dir);
271     send_file_end (1);          /* Cancel. */
272     return -1;
273   }
274
275   if (send_file_end (0))        /* Normal end of file. */
276     return -1;
277
278   return 0;
279 }