1 /* guestfish - the filesystem interactive shell
2 * Copyright (C) 2010 Red Hat Inc.
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.
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.
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.
26 #include <sys/types.h>
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);
37 run_copy_in (const char *cmd, size_t argc, char *argv[])
41 _("use 'copy-in <local> [<local>...] <remotedir>' to copy files into the image\n"));
45 /* Remote directory is always the last arg. */
46 const char *remote = argv[argc-1];
47 int nr_locals = argc-1;
49 int remote_is_dir = guestfs_is_dir (g, remote);
50 if (remote_is_dir == -1)
54 fprintf (stderr, _("copy-in: target '%s' is not a directory\n"), remote);
58 /* Upload each local one at a time using tar-in. */
60 for (i = 0; i < nr_locals; ++i) {
61 int fd = make_tar_from_local (argv[i]);
66 snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fd);
68 int r = guestfs_tar_in (g, fdbuf, remote);
70 if (close (fd) == -1) {
71 perror ("close (tar-from-local subprocess)");
76 if (wait (&status) == -1) {
77 perror ("wait (tar-from-local subprocess)");
80 if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0))
90 static void tar_create (const char *dir, const char *path) __attribute__((noreturn));
92 /* This creates a subprocess which feeds a tar file back to the
93 * main guestfish process.
96 make_tar_from_local (const char *local)
100 if (pipe (fd) == -1) {
111 if (pid > 0) { /* Parent */
122 const char *dirname, *basename;
123 if (split_path (buf, sizeof buf, local, &dirname, &basename) == -1)
124 _exit (EXIT_FAILURE);
126 tar_create (dirname, basename);
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.
134 split_path (char *buf, size_t buf_size,
135 const char *path, const char **dirname, const char **basename)
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"));
145 if (len >= 2 && buf[len-1] == '/') {
150 char *p = strrchr (buf, '/');
151 if (p && p > buf) { /* "foo/bar" */
154 if (dirname) *dirname = buf;
155 if (basename) *basename = p;
156 } else if (p && p == buf) { /* "/foo" */
157 if (dirname) *dirname = "/";
158 if (basename) *basename = buf+1;
160 if (dirname) *dirname = NULL;
161 if (basename) *basename = buf;
168 tar_create (const char *dir, const char *path)
171 execlp ("tar", "tar", "-C", dir, "-cf", "-", path, NULL);
173 execlp ("tar", "tar", "-cf", "-", path, NULL);
175 perror ("execlp: tar");
176 _exit (EXIT_FAILURE);
180 run_copy_out (const char *cmd, size_t argc, char *argv[])
184 _("use 'copy-out <remote> [<remote>...] <localdir>' to copy files out of the image\n"));
188 /* Local directory is always the last arg. */
189 const char *local = argv[argc-1];
190 int nr_remotes = argc-1;
193 if (stat (local, &statbuf) == -1 ||
194 ! (S_ISDIR (statbuf.st_mode))) {
195 fprintf (stderr, _("copy-out: target '%s' is not a directory\n"), local);
199 /* Download each remote one at a time using tar-out. */
201 for (i = 0; i < nr_remotes; ++i) {
202 /* If the remote is a file, download it. If it's a directory,
203 * create the directory in local first before using tar-out.
205 r = guestfs_is_file (g, argv[i]);
208 if (r == 1) { /* is file */
210 const char *basename;
211 if (split_path (buf, sizeof buf, argv[i], NULL, &basename) == -1)
214 char filename[PATH_MAX];
215 snprintf (filename, sizeof filename, "%s/%s", local, basename);
216 if (guestfs_download (g, argv[i], filename) == -1)
219 else { /* not a regular file */
220 r = guestfs_is_dir (g, argv[i]);
225 fprintf (stderr, _("copy-out: '%s' is not a file or directory\n"),
231 const char *basename;
232 if (split_path (buf, sizeof buf, argv[i], NULL, &basename) == -1)
235 int fd = make_tar_output (local, basename);
240 snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fd);
242 int r = guestfs_tar_out (g, argv[i], fdbuf);
244 if (close (fd) == -1) {
245 perror ("close (tar-output subprocess)");
250 if (wait (&status) == -1) {
251 perror ("wait (tar-output subprocess)");
254 if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0))
265 /* This creates a subprocess which takes a tar file from the main
266 * guestfish process and unpacks it into the 'local/basename'
270 make_tar_output (const char *local, const char *basename)
274 if (pipe (fd) == -1) {
285 if (pid > 0) { /* Parent */
295 if (chdir (local) == -1) {
297 _exit (EXIT_FAILURE);
300 mkdir (basename, 0777);
302 if (chdir (basename) == -1) {
304 _exit (EXIT_FAILURE);
307 execlp ("tar", "tar", "-xf", "-", NULL);
308 perror ("execlp: tar");
309 _exit (EXIT_FAILURE);