Code cleanups related to 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 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 int
162 do_tgz_in (const char *dir)
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 -zxf -", dir) == -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 FileOut parameter. */
223 int
224 do_tgz_out (const char *dir)
225 {
226   int r;
227   FILE *fp;
228   char *cmd;
229   char buf[GUESTFS_MAX_CHUNK_SIZE];
230
231   /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
232   if (asprintf_nowarn (&cmd, "tar -C %R -zcf - .", dir) == -1) {
233     reply_with_perror ("asprintf");
234     return -1;
235   }
236
237   if (verbose)
238     fprintf (stderr, "%s\n", cmd);
239
240   fp = popen (cmd, "r");
241   if (fp == NULL) {
242     reply_with_perror ("%s", cmd);
243     free (cmd);
244     return -1;
245   }
246   free (cmd);
247
248   /* Now we must send the reply message, before the file contents.  After
249    * this there is no opportunity in the protocol to send any error
250    * message back.  Instead we can only cancel the transfer.
251    */
252   reply (NULL, NULL);
253
254   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
255     if (send_file_write (buf, r) < 0) {
256       pclose (fp);
257       return -1;
258     }
259   }
260
261   if (ferror (fp)) {
262     perror (dir);
263     send_file_end (1);          /* Cancel. */
264     pclose (fp);
265     return -1;
266   }
267
268   if (pclose (fp) != 0) {
269     perror (dir);
270     send_file_end (1);          /* Cancel. */
271     return -1;
272   }
273
274   if (send_file_end (0))        /* Normal end of file. */
275     return -1;
276
277   return 0;
278 }