New API: Implement pwrite system call (partial fix for RHBZ#592883).
[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 "read-file.h"
27
28 #include "../src/guestfs_protocol.h"
29 #include "daemon.h"
30 #include "actions.h"
31
32 /* Redirect errors from the tar command to the error file, then
33  * provide functions for reading it in.  We overwrite the file each
34  * time, and since it's small and stored on the appliance we don't
35  * bother to delete it.
36  */
37 static const char *error_file = "/tmp/error";
38
39 static char *
40 read_error_file (void)
41 {
42   size_t len;
43   char *str = read_file (error_file, &len);
44   if (str == NULL) {
45     str = strdup ("(no error)");
46     if (str == NULL) {
47       perror ("strdup");
48       exit (EXIT_FAILURE);
49     }
50     len = strlen (str);
51   }
52
53   /* Remove trailing \n character if any. */
54   if (len > 0 && str[len-1] == '\n')
55     str[--len] = '\0';
56
57   return str;                   /* caller frees */
58 }
59
60 static int
61 write_cb (void *fd_ptr, const void *buf, size_t len)
62 {
63   int fd = *(int *)fd_ptr;
64   return xwrite (fd, buf, len);
65 }
66
67 /* Has one FileIn parameter. */
68 static int
69 do_tXz_in (const char *dir, const char *filter)
70 {
71   int err, r;
72   FILE *fp;
73   char *cmd;
74
75   /* "tar -C /sysroot%s -xf -" but we have to quote the dir. */
76   if (asprintf_nowarn (&cmd, "tar -C %R -%sxf - 2> %s",
77                        dir, filter, error_file) == -1) {
78     err = errno;
79     r = cancel_receive ();
80     errno = err;
81     if (r != -2) reply_with_perror ("asprintf");
82     return -1;
83   }
84
85   if (verbose)
86     fprintf (stderr, "%s\n", cmd);
87
88   fp = popen (cmd, "w");
89   if (fp == NULL) {
90     err = errno;
91     r = cancel_receive ();
92     errno = err;
93     if (r != -2) reply_with_perror ("%s", cmd);
94     free (cmd);
95     return -1;
96   }
97   free (cmd);
98
99   /* The semantics of fwrite are too undefined, so write to the
100    * file descriptor directly instead.
101    */
102   int fd = fileno (fp);
103
104   r = receive_file (write_cb, &fd);
105   if (r == -1) {                /* write error */
106     if (cancel_receive () != -2) {
107       char *errstr = read_error_file ();
108       reply_with_error ("write error on directory: %s: %s", dir, errstr);
109       free (errstr);
110     }
111     pclose (fp);
112     return -1;
113   }
114   if (r == -2) {                /* cancellation from library */
115     pclose (fp);
116     /* Do NOT send any error. */
117     return -1;
118   }
119
120   if (pclose (fp) != 0) {
121     if (r == -1)                /* if r == 0, file transfer ended already */
122       r = cancel_receive ();
123     if (r != -2) {
124       char *errstr = read_error_file ();
125       reply_with_error ("tar subcommand failed on directory: %s: %s",
126                         dir, errstr);
127       free (errstr);
128     }
129     return -1;
130   }
131
132   return 0;
133 }
134
135 /* Has one FileIn parameter. */
136 int
137 do_tar_in (const char *dir)
138 {
139   return do_tXz_in (dir, "");
140 }
141
142 /* Has one FileIn parameter. */
143 int
144 do_tgz_in (const char *dir)
145 {
146   return do_tXz_in (dir, "z");
147 }
148
149 /* Has one FileIn parameter. */
150 int
151 do_txz_in (const char *dir)
152 {
153   return do_tXz_in (dir, "J");
154 }
155
156 /* Has one FileOut parameter. */
157 static int
158 do_tXz_out (const char *dir, const char *filter)
159 {
160   int r;
161   FILE *fp;
162   char *cmd;
163   char buf[GUESTFS_MAX_CHUNK_SIZE];
164
165   /* "tar -C /sysroot%s -zcf - ." but we have to quote the dir. */
166   if (asprintf_nowarn (&cmd, "tar -C %R -%scf - .", dir, filter) == -1) {
167     reply_with_perror ("asprintf");
168     return -1;
169   }
170
171   if (verbose)
172     fprintf (stderr, "%s\n", cmd);
173
174   fp = popen (cmd, "r");
175   if (fp == NULL) {
176     reply_with_perror ("%s", cmd);
177     free (cmd);
178     return -1;
179   }
180   free (cmd);
181
182   /* Now we must send the reply message, before the file contents.  After
183    * this there is no opportunity in the protocol to send any error
184    * message back.  Instead we can only cancel the transfer.
185    */
186   reply (NULL, NULL);
187
188   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
189     if (send_file_write (buf, r) < 0) {
190       pclose (fp);
191       return -1;
192     }
193   }
194
195   if (ferror (fp)) {
196     perror (dir);
197     send_file_end (1);          /* Cancel. */
198     pclose (fp);
199     return -1;
200   }
201
202   if (pclose (fp) != 0) {
203     perror (dir);
204     send_file_end (1);          /* Cancel. */
205     return -1;
206   }
207
208   if (send_file_end (0))        /* Normal end of file. */
209     return -1;
210
211   return 0;
212 }
213
214 /* Has one FileOut parameter. */
215 int
216 do_tar_out (const char *dir)
217 {
218   return do_tXz_out (dir, "");
219 }
220
221 /* Has one FileOut parameter. */
222 int
223 do_tgz_out (const char *dir)
224 {
225   return do_tXz_out (dir, "z");
226 }
227
228 /* Has one FileOut parameter. */
229 int
230 do_txz_out (const char *dir)
231 {
232   return do_tXz_out (dir, "J");
233 }