New API: lvresize-free to extend LVs into percentage of free space.
[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 write_cb (void *fd_ptr, const void *buf, size_t len)
32 {
33   int fd = *(int *)fd_ptr;
34   return xwrite (fd, buf, len);
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   /* The semantics of fwrite are too undefined, so write to the
75    * file descriptor directly instead.
76    */
77   int fd = fileno (fp);
78
79   r = receive_file (write_cb, &fd);
80   if (r == -1) {                /* write error */
81     cancel_receive ();
82     reply_with_error ("write error on directory: %s", dir);
83     pclose (fp);
84     return -1;
85   }
86   if (r == -2) {                /* cancellation from library */
87     pclose (fp);
88     /* Do NOT send any error. */
89     return -1;
90   }
91
92   if (pclose (fp) != 0) {
93     if (r == -1)                /* if r == 0, file transfer ended already */
94       cancel_receive ();
95     reply_with_error ("tar subcommand failed on directory: %s", dir);
96     return -1;
97   }
98
99   return 0;
100 }
101
102 /* Has one FileOut parameter. */
103 int
104 do_tar_out (const char *dir)
105 {
106   int r;
107   FILE *fp;
108   char *cmd;
109   char buf[GUESTFS_MAX_CHUNK_SIZE];
110
111   /* "tar -C /sysroot%s -cf - ." but we have to quote the dir. */
112   if (asprintf_nowarn (&cmd, "tar -C %R -cf - .", dir) == -1) {
113     reply_with_perror ("asprintf");
114     return -1;
115   }
116
117   if (verbose)
118     fprintf (stderr, "%s\n", cmd);
119
120   fp = popen (cmd, "r");
121   if (fp == NULL) {
122     reply_with_perror ("%s", cmd);
123     free (cmd);
124     return -1;
125   }
126   free (cmd);
127
128   /* Now we must send the reply message, before the file contents.  After
129    * this there is no opportunity in the protocol to send any error
130    * message back.  Instead we can only cancel the transfer.
131    */
132   reply (NULL, NULL);
133
134   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
135     if (send_file_write (buf, r) < 0) {
136       pclose (fp);
137       return -1;
138     }
139   }
140
141   if (ferror (fp)) {
142     perror (dir);
143     send_file_end (1);          /* Cancel. */
144     pclose (fp);
145     return -1;
146   }
147
148   if (pclose (fp) != 0) {
149     perror (dir);
150     send_file_end (1);          /* Cancel. */
151     return -1;
152   }
153
154   if (send_file_end (0))        /* Normal end of file. */
155     return -1;
156
157   return 0;
158 }
159
160 /* Has one FileIn parameter. */
161 static int
162 do_tXz_in (const char *dir, char filter)
163 {
164   int err, r;
165   FILE *fp;
166   char *cmd;
167
168   if (!root_mounted || dir[0] != '/') {
169     cancel_receive ();
170     reply_with_error ("root must be mounted and path must be absolute");
171     return -1;
172   }
173
174   /* "tar -C /sysroot%s -zxf -" but we have to quote the dir. */
175   if (asprintf_nowarn (&cmd, "tar -C %R -%cxf -", dir, filter) == -1) {
176     err = errno;
177     cancel_receive ();
178     errno = err;
179     reply_with_perror ("asprintf");
180     return -1;
181   }
182
183   if (verbose)
184     fprintf (stderr, "%s\n", cmd);
185
186   fp = popen (cmd, "w");
187   if (fp == NULL) {
188     err = errno;
189     cancel_receive ();
190     errno = err;
191     reply_with_perror ("%s", cmd);
192     free (cmd);
193     return -1;
194   }
195   free (cmd);
196
197   int fd = fileno (fp);
198
199   r = receive_file (write_cb, &fd);
200   if (r == -1) {                /* write error */
201     cancel_receive ();
202     reply_with_error ("write error on directory: %s", dir);
203     pclose (fp);
204     return -1;
205   }
206   if (r == -2) {                /* cancellation from library */
207     pclose (fp);
208     /* Do NOT send any error. */
209     return -1;
210   }
211
212   if (pclose (fp) != 0) {
213     if (r == -1)                /* if r == 0, file transfer ended already */
214       cancel_receive ();
215     reply_with_error ("tar subcommand failed on directory: %s", dir);
216     return -1;
217   }
218
219   return 0;
220 }
221
222 /* Has one FileIn parameter. */
223 int
224 do_tgz_in (const char *dir)
225 {
226   return do_tXz_in (dir, 'z');
227 }
228
229 /* Has one FileIn parameter. */
230 int
231 do_txz_in (const char *dir)
232 {
233   return do_tXz_in (dir, 'J');
234 }
235
236 /* Has one FileOut parameter. */
237 static int
238 do_tXz_out (const char *dir, char filter)
239 {
240   int r;
241   FILE *fp;
242   char *cmd;
243   char buf[GUESTFS_MAX_CHUNK_SIZE];
244
245   /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
246   if (asprintf_nowarn (&cmd, "tar -C %R -%ccf - .", dir, filter) == -1) {
247     reply_with_perror ("asprintf");
248     return -1;
249   }
250
251   if (verbose)
252     fprintf (stderr, "%s\n", cmd);
253
254   fp = popen (cmd, "r");
255   if (fp == NULL) {
256     reply_with_perror ("%s", cmd);
257     free (cmd);
258     return -1;
259   }
260   free (cmd);
261
262   /* Now we must send the reply message, before the file contents.  After
263    * this there is no opportunity in the protocol to send any error
264    * message back.  Instead we can only cancel the transfer.
265    */
266   reply (NULL, NULL);
267
268   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
269     if (send_file_write (buf, r) < 0) {
270       pclose (fp);
271       return -1;
272     }
273   }
274
275   if (ferror (fp)) {
276     perror (dir);
277     send_file_end (1);          /* Cancel. */
278     pclose (fp);
279     return -1;
280   }
281
282   if (pclose (fp) != 0) {
283     perror (dir);
284     send_file_end (1);          /* Cancel. */
285     return -1;
286   }
287
288   if (send_file_end (0))        /* Normal end of file. */
289     return -1;
290
291   return 0;
292 }
293
294 /* Has one FileOut parameter. */
295 int
296 do_tgz_out (const char *dir)
297 {
298   return do_tXz_out (dir, 'z');
299 }
300
301 /* Has one FileOut parameter. */
302 int
303 do_txz_out (const char *dir)
304 {
305   return do_tXz_out (dir, 'J');
306 }