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