daemon: Use gnulib futimens module.
[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   if (send_file_end (0))        /* Normal end of file. */
153     return -1;
154
155   return 0;
156 }
157
158 /* Has one FileIn parameter. */
159 int
160 do_tgz_in (const char *dir)
161 {
162   int err, r;
163   FILE *fp;
164   char *cmd;
165
166   if (!root_mounted || dir[0] != '/') {
167     cancel_receive ();
168     reply_with_error ("tar-in: root must be mounted and path must be absolute");
169     return -1;
170   }
171
172   /* "tar -C /sysroot%s -zxf -" but we have to quote the dir. */
173   if (asprintf_nowarn (&cmd, "tar -C %R -zxf -", dir) == -1) {
174     err = errno;
175     cancel_receive ();
176     errno = err;
177     reply_with_perror ("asprintf");
178     return -1;
179   }
180
181   if (verbose)
182     fprintf (stderr, "%s\n", cmd);
183
184   fp = popen (cmd, "w");
185   if (fp == NULL) {
186     err = errno;
187     cancel_receive ();
188     errno = err;
189     reply_with_perror ("%s", cmd);
190     free (cmd);
191     return -1;
192   }
193   free (cmd);
194
195   r = receive_file (fwrite_cb, &fp);
196   if (r == -1) {                /* write error */
197     err = errno;
198     cancel_receive ();
199     errno = err;
200     reply_with_perror ("write: %s", dir);
201     pclose (fp);
202     return -1;
203   }
204   if (r == -2) {                /* cancellation from library */
205     pclose (fp);
206     /* Do NOT send any error. */
207     return -1;
208   }
209
210   if (pclose (fp) != 0) {
211     err = errno;
212     cancel_receive ();
213     errno = err;
214     reply_with_perror ("pclose: %s", dir);
215     return -1;
216   }
217
218   return 0;
219 }
220
221 /* Has one FileOut parameter. */
222 int
223 do_tgz_out (const char *dir)
224 {
225   int r;
226   FILE *fp;
227   char *cmd;
228   char buf[GUESTFS_MAX_CHUNK_SIZE];
229
230   /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
231   if (asprintf_nowarn (&cmd, "tar -C %R -zcf - .", dir) == -1) {
232     reply_with_perror ("asprintf");
233     return -1;
234   }
235
236   if (verbose)
237     fprintf (stderr, "%s\n", cmd);
238
239   fp = popen (cmd, "r");
240   if (fp == NULL) {
241     reply_with_perror ("%s", cmd);
242     free (cmd);
243     return -1;
244   }
245   free (cmd);
246
247   /* Now we must send the reply message, before the file contents.  After
248    * this there is no opportunity in the protocol to send any error
249    * message back.  Instead we can only cancel the transfer.
250    */
251   reply (NULL, NULL);
252
253   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
254     if (send_file_write (buf, r) < 0) {
255       pclose (fp);
256       return -1;
257     }
258   }
259
260   if (ferror (fp)) {
261     perror (dir);
262     send_file_end (1);          /* Cancel. */
263     pclose (fp);
264     return -1;
265   }
266
267   if (pclose (fp) != 0) {
268     perror (dir);
269     send_file_end (1);          /* Cancel. */
270     return -1;
271   }
272
273   if (send_file_end (0))        /* Normal end of file. */
274     return -1;
275
276   return 0;
277 }