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