c1c1b4749b525c6bb25f3d197da09821da893759
[libguestfs.git] / fish / copy.c
1 /* guestfish - the filesystem interactive shell
2  * Copyright (C) 2010-2011 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 <unistd.h>
25 #include <limits.h>
26 #include <sys/types.h>
27 #include <sys/stat.h>
28 #include <sys/wait.h>
29
30 #include "fish.h"
31
32 static int make_tar_from_local (const char *local);
33 static int make_tar_output (const char *local, const char *basename);
34 static int split_path (char *buf, size_t buf_size, const char *path, const char **dirname, const char **basename);
35
36 int
37 run_copy_in (const char *cmd, size_t argc, char *argv[])
38 {
39   if (argc < 2) {
40     fprintf (stderr,
41              _("use 'copy-in <local> [<local>...] <remotedir>' to copy files into the image\n"));
42     return -1;
43   }
44
45   /* Remote directory is always the last arg. */
46   char *remote = argv[argc-1];
47
48   /* Allow win: prefix on remote. */
49   remote = win_prefix (remote);
50   if (remote == NULL)
51     return -1;
52
53   int nr_locals = argc-1;
54
55   int remote_is_dir = guestfs_is_dir (g, remote);
56   if (remote_is_dir == -1) {
57     free (remote);
58     return -1;
59   }
60
61   if (!remote_is_dir) {
62     fprintf (stderr, _("copy-in: target '%s' is not a directory\n"), remote);
63     free (remote);
64     return -1;
65   }
66
67   /* Upload each local one at a time using tar-in. */
68   int i;
69   for (i = 0; i < nr_locals; ++i) {
70     int fd = make_tar_from_local (argv[i]);
71     if (fd == -1) {
72       free (remote);
73       return -1;
74     }
75
76     char fdbuf[64];
77     snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fd);
78
79     int r = guestfs_tar_in (g, fdbuf, remote);
80
81     if (close (fd) == -1) {
82       perror ("close (tar-from-local subprocess)");
83       r = -1;
84     }
85
86     int status;
87     if (wait (&status) == -1) {
88       perror ("wait (tar-from-local subprocess)");
89       free (remote);
90       return -1;
91     }
92     if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0)) {
93       free (remote);
94       return -1;
95     }
96
97     if (r == -1) {
98       free (remote);
99       return -1;
100     }
101   }
102
103   free (remote);
104
105   return 0;
106 }
107
108 static void tar_create (const char *dir, const char *path) __attribute__((noreturn));
109
110 /* This creates a subprocess which feeds a tar file back to the
111  * main guestfish process.
112  */
113 static int
114 make_tar_from_local (const char *local)
115 {
116   int fd[2];
117
118   if (pipe (fd) == -1) {
119     perror ("pipe");
120     return -1;
121   }
122
123   pid_t pid = fork ();
124   if (pid == -1) {
125     perror ("fork");
126     return -1;
127   }
128
129   if (pid > 0) {                /* Parent */
130     close (fd[1]);
131     return fd[0];
132   }
133
134   /* Child. */
135   close (fd[0]);
136   dup2 (fd[1], 1);
137   close (fd[1]);
138
139   char buf[PATH_MAX];
140   const char *dirname, *basename;
141   if (split_path (buf, sizeof buf, local, &dirname, &basename) == -1)
142     _exit (EXIT_FAILURE);
143
144   tar_create (dirname, basename);
145 }
146
147 /* Split path into directory name and base name, using the buffer
148  * provided as a working area.  If there is no directory name
149  * (eg. path == "/") then this can return dirname as NULL.
150  */
151 static int
152 split_path (char *buf, size_t buf_size,
153             const char *path, const char **dirname, const char **basename)
154 {
155   size_t len = strlen (path);
156   if (len == 0 || len > buf_size - 1) {
157     fprintf (stderr, _("error: argument is zero length or longer than maximum permitted\n"));
158     return -1;
159   }
160
161   strcpy (buf, path);
162
163   if (len >= 2 && buf[len-1] == '/') {
164     buf[len-1] = '\0';
165     len--;
166   }
167
168   char *p = strrchr (buf, '/');
169   if (p && p > buf) {           /* "foo/bar" */
170     *p = '\0';
171     p++;
172     if (dirname) *dirname = buf;
173     if (basename) *basename = p;
174   } else if (p && p == buf) {   /* "/foo" */
175     if (dirname) *dirname = "/";
176     if (basename) *basename = buf+1;
177   } else {
178     if (dirname) *dirname = NULL;
179     if (basename) *basename = buf;
180   }
181
182   return 0;
183 }
184
185 static void
186 tar_create (const char *dir, const char *path)
187 {
188   if (dir)
189     execlp ("tar", "tar", "-C", dir, "-cf", "-", path, NULL);
190   else
191     execlp ("tar", "tar", "-cf", "-", path, NULL);
192
193   perror ("execlp: tar");
194   _exit (EXIT_FAILURE);
195 }
196
197 int
198 run_copy_out (const char *cmd, size_t argc, char *argv[])
199 {
200   if (argc < 2) {
201     fprintf (stderr,
202              _("use 'copy-out <remote> [<remote>...] <localdir>' to copy files out of the image\n"));
203     return -1;
204   }
205
206   /* Local directory is always the last arg. */
207   const char *local = argv[argc-1];
208   int nr_remotes = argc-1;
209
210   struct stat statbuf;
211   if (stat (local, &statbuf) == -1 ||
212       ! (S_ISDIR (statbuf.st_mode))) {
213     fprintf (stderr, _("copy-out: target '%s' is not a directory\n"), local);
214     return -1;
215   }
216
217   /* Download each remote one at a time using tar-out. */
218   int i, r;
219   for (i = 0; i < nr_remotes; ++i) {
220     char *remote = argv[i];
221
222     /* Allow win:... prefix on remotes. */
223     remote = win_prefix (remote);
224     if (remote == NULL)
225       return -1;
226
227     /* If the remote is a file, download it.  If it's a directory,
228      * create the directory in local first before using tar-out.
229      */
230     r = guestfs_is_file (g, remote);
231     if (r == -1) {
232       free (remote);
233       return -1;
234     }
235     if (r == 1) {               /* is file */
236       char buf[PATH_MAX];
237       const char *basename;
238       if (split_path (buf, sizeof buf, remote, NULL, &basename) == -1) {
239         free (remote);
240         return -1;
241       }
242
243       char filename[PATH_MAX];
244       snprintf (filename, sizeof filename, "%s/%s", local, basename);
245       if (guestfs_download (g, remote, filename) == -1) {
246         free (remote);
247         return -1;
248       }
249     }
250     else {                      /* not a regular file */
251       r = guestfs_is_dir (g, remote);
252       if (r == -1) {
253         free (remote);
254         return -1;
255       }
256
257       if (r == 0) {
258         fprintf (stderr, _("copy-out: '%s' is not a file or directory\n"),
259                  remote);
260         free (remote);
261         return -1;
262       }
263
264       char buf[PATH_MAX];
265       const char *basename;
266       if (split_path (buf, sizeof buf, remote, NULL, &basename) == -1) {
267         free (remote);
268         return -1;
269       }
270
271       int fd = make_tar_output (local, basename);
272       if (fd == -1) {
273         free (remote);
274         return -1;
275       }
276
277       char fdbuf[64];
278       snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fd);
279
280       int r = guestfs_tar_out (g, remote, fdbuf);
281
282       if (close (fd) == -1) {
283         perror ("close (tar-output subprocess)");
284         free (remote);
285         r = -1;
286       }
287
288       int status;
289       if (wait (&status) == -1) {
290         perror ("wait (tar-output subprocess)");
291         free (remote);
292         return -1;
293       }
294       if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0)) {
295         free (remote);
296         return -1;
297       }
298
299       if (r == -1) {
300         free (remote);
301         return -1;
302       }
303     }
304
305     free (remote);
306   }
307
308   return 0;
309 }
310
311 /* This creates a subprocess which takes a tar file from the main
312  * guestfish process and unpacks it into the 'local/basename'
313  * directory.
314  */
315 static int
316 make_tar_output (const char *local, const char *basename)
317 {
318   int fd[2];
319
320   if (pipe (fd) == -1) {
321     perror ("pipe");
322     return -1;
323   }
324
325   pid_t pid = fork ();
326   if (pid == -1) {
327     perror ("fork");
328     return -1;
329   }
330
331   if (pid > 0) {                /* Parent */
332     close (fd[0]);
333     return fd[1];
334   }
335
336   /* Child. */
337   close (fd[1]);
338   dup2 (fd[0], 0);
339   close (fd[0]);
340
341   if (chdir (local) == -1) {
342     perror (local);
343     _exit (EXIT_FAILURE);
344   }
345
346   mkdir (basename, 0777);
347
348   if (chdir (basename) == -1) {
349     perror (basename);
350     _exit (EXIT_FAILURE);
351   }
352
353   execlp ("tar", "tar", "-xf", "-", NULL);
354   perror ("execlp: tar");
355   _exit (EXIT_FAILURE);
356 }