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