command.c: avoid shadowing a global function
[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 ("tar-in: 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     cancel_receive ();
92     errno = err;
93     reply_with_perror ("pclose: %s", dir);
94     return -1;
95   }
96
97   return 0;
98 }
99
100 /* Has one FileOut parameter. */
101 int
102 do_tar_out (const char *dir)
103 {
104   int r;
105   FILE *fp;
106   char *cmd;
107   char buf[GUESTFS_MAX_CHUNK_SIZE];
108
109   /* "tar -C /sysroot%s -cf - ." but we have to quote the dir. */
110   if (asprintf_nowarn (&cmd, "tar -C %R -cf - .", dir) == -1) {
111     reply_with_perror ("asprintf");
112     return -1;
113   }
114
115   if (verbose)
116     fprintf (stderr, "%s\n", cmd);
117
118   fp = popen (cmd, "r");
119   if (fp == NULL) {
120     reply_with_perror ("%s", cmd);
121     free (cmd);
122     return -1;
123   }
124   free (cmd);
125
126   /* Now we must send the reply message, before the file contents.  After
127    * this there is no opportunity in the protocol to send any error
128    * message back.  Instead we can only cancel the transfer.
129    */
130   reply (NULL, NULL);
131
132   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
133     if (send_file_write (buf, r) < 0) {
134       pclose (fp);
135       return -1;
136     }
137   }
138
139   if (ferror (fp)) {
140     perror (dir);
141     send_file_end (1);          /* Cancel. */
142     pclose (fp);
143     return -1;
144   }
145
146   if (pclose (fp) != 0) {
147     perror (dir);
148     send_file_end (1);          /* Cancel. */
149     return -1;
150   }
151
152   send_file_end (0);            /* Normal end of file. */
153   return 0;
154 }
155
156 /* Has one FileIn parameter. */
157 int
158 do_tgz_in (const char *dir)
159 {
160   int err, r;
161   FILE *fp;
162   char *cmd;
163
164   if (!root_mounted || dir[0] != '/') {
165     cancel_receive ();
166     reply_with_error ("tar-in: root must be mounted and path must be absolute");
167     return -1;
168   }
169
170   /* "tar -C /sysroot%s -zxf -" but we have to quote the dir. */
171   if (asprintf_nowarn (&cmd, "tar -C %R -zxf -", dir) == -1) {
172     err = errno;
173     cancel_receive ();
174     errno = err;
175     reply_with_perror ("asprintf");
176     return -1;
177   }
178
179   if (verbose)
180     fprintf (stderr, "%s\n", cmd);
181
182   fp = popen (cmd, "w");
183   if (fp == NULL) {
184     err = errno;
185     cancel_receive ();
186     errno = err;
187     reply_with_perror ("%s", cmd);
188     free (cmd);
189     return -1;
190   }
191   free (cmd);
192
193   r = receive_file (fwrite_cb, &fp);
194   if (r == -1) {                /* write error */
195     err = errno;
196     cancel_receive ();
197     errno = err;
198     reply_with_perror ("write: %s", dir);
199     pclose (fp);
200     return -1;
201   }
202   if (r == -2) {                /* cancellation from library */
203     pclose (fp);
204     /* Do NOT send any error. */
205     return -1;
206   }
207
208   if (pclose (fp) != 0) {
209     err = errno;
210     cancel_receive ();
211     errno = err;
212     reply_with_perror ("pclose: %s", dir);
213     return -1;
214   }
215
216   return 0;
217 }
218
219 /* Has one FileOut parameter. */
220 int
221 do_tgz_out (const char *dir)
222 {
223   int r;
224   FILE *fp;
225   char *cmd;
226   char buf[GUESTFS_MAX_CHUNK_SIZE];
227
228   /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
229   if (asprintf_nowarn (&cmd, "tar -C %R -zcf - .", dir) == -1) {
230     reply_with_perror ("asprintf");
231     return -1;
232   }
233
234   if (verbose)
235     fprintf (stderr, "%s\n", cmd);
236
237   fp = popen (cmd, "r");
238   if (fp == NULL) {
239     reply_with_perror ("%s", cmd);
240     free (cmd);
241     return -1;
242   }
243   free (cmd);
244
245   /* Now we must send the reply message, before the file contents.  After
246    * this there is no opportunity in the protocol to send any error
247    * message back.  Instead we can only cancel the transfer.
248    */
249   reply (NULL, NULL);
250
251   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
252     if (send_file_write (buf, r) < 0) {
253       pclose (fp);
254       return -1;
255     }
256   }
257
258   if (ferror (fp)) {
259     perror (dir);
260     send_file_end (1);          /* Cancel. */
261     pclose (fp);
262     return -1;
263   }
264
265   if (pclose (fp) != 0) {
266     perror (dir);
267     send_file_end (1);          /* Cancel. */
268     return -1;
269   }
270
271   send_file_end (0);            /* Normal end of file. */
272   return 0;
273 }