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.
25 #include <sys/types.h>
31 static int make_tar_from_local (const char *local);
32 static int make_tar_output (const char *local, const char *basename);
33 static int split_path (char *buf, size_t buf_size, const char *path, const char **dirname, const char **basename);
36 run_copy_in (const char *cmd, size_t argc, char *argv[])
40 _("use 'copy-in <local> [<local>...] <remotedir>' to copy files into the image\n"));
44 /* Remote directory is always the last arg. */
45 const char *remote = argv[argc-1];
46 int nr_locals = argc-1;
48 int remote_is_dir = guestfs_is_dir (g, remote);
49 if (remote_is_dir == -1)
53 fprintf (stderr, _("copy-in: target '%s' is not a directory\n"), remote);
57 /* Upload each local one at a time using tar-in. */
59 for (i = 0; i < nr_locals; ++i) {
60 int fd = make_tar_from_local (argv[i]);
65 snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fd);
67 int r = guestfs_tar_in (g, fdbuf, remote);
69 if (close (fd) == -1) {
70 perror ("close (tar-from-local subprocess)");
75 if (wait (&status) == -1) {
76 perror ("wait (tar-from-local subprocess)");
79 if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0))
89 static void tar_create (const char *dir, const char *path) __attribute__((noreturn));
91 /* This creates a subprocess which feeds a tar file back to the
92 * main guestfish process.
95 make_tar_from_local (const char *local)
99 if (pipe (fd) == -1) {
110 if (pid > 0) { /* Parent */
121 const char *dirname, *basename;
122 if (split_path (buf, sizeof buf, local, &dirname, &basename) == -1)
123 _exit (EXIT_FAILURE);
125 tar_create (dirname, basename);
128 /* Split path into directory name and base name, using the buffer
129 * provided as a working area. If there is no directory name
130 * (eg. path == "/") then this can return dirname as NULL.
133 split_path (char *buf, size_t buf_size,
134 const char *path, const char **dirname, const char **basename)
136 size_t len = strlen (path);
137 if (len == 0 || len > buf_size - 1) {
138 fprintf (stderr, _("error: argument is zero length or longer than maximum permitted\n"));
144 if (len >= 2 && buf[len-1] == '/') {
149 char *p = strrchr (buf, '/');
150 if (p && p > buf) { /* "foo/bar" */
153 if (dirname) *dirname = buf;
154 if (basename) *basename = p;
155 } else if (p && p == buf) { /* "/foo" */
156 if (dirname) *dirname = "/";
157 if (basename) *basename = buf+1;
159 if (dirname) *dirname = NULL;
160 if (basename) *basename = buf;
167 tar_create (const char *dir, const char *path)
170 execlp ("tar", "tar", "-C", dir, "-cf", "-", path, NULL);
172 execlp ("tar", "tar", "-cf", "-", path, NULL);
174 perror ("execlp: tar");
175 _exit (EXIT_FAILURE);
179 run_copy_out (const char *cmd, size_t argc, char *argv[])
183 _("use 'copy-out <remote> [<remote>...] <localdir>' to copy files out of the image\n"));
187 /* Local directory is always the last arg. */
188 const char *local = argv[argc-1];
189 int nr_remotes = argc-1;
192 if (stat (local, &statbuf) == -1 ||
193 ! (S_ISDIR (statbuf.st_mode))) {
194 fprintf (stderr, _("copy-in: target '%s' is not a directory\n"), local);
198 /* Download each remote one at a time using tar-out. */
200 for (i = 0; i < nr_remotes; ++i) {
201 /* If the remote is a file, download it. If it's a directory,
202 * create the directory in local first before using tar-out.
204 r = guestfs_is_file (g, argv[i]);
207 if (r == 1) { /* is file */
209 const char *basename;
210 if (split_path (buf, sizeof buf, argv[i], NULL, &basename) == -1)
213 char filename[PATH_MAX];
214 snprintf (filename, sizeof filename, "%s/%s", local, basename);
215 if (guestfs_download (g, argv[i], filename) == -1)
218 else { /* not a regular file */
219 r = guestfs_is_dir (g, argv[i]);
224 fprintf (stderr, _("copy-out: '%s' is not a file or directory\n"),
230 const char *basename;
231 if (split_path (buf, sizeof buf, argv[i], NULL, &basename) == -1)
234 int fd = make_tar_output (local, basename);
239 snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fd);
241 int r = guestfs_tar_out (g, argv[i], fdbuf);
243 if (close (fd) == -1) {
244 perror ("close (tar-output subprocess)");
249 if (wait (&status) == -1) {
250 perror ("wait (tar-output subprocess)");
253 if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0))
264 /* This creates a subprocess which takes a tar file from the main
265 * guestfish process and unpacks it into the 'local/basename'
269 make_tar_output (const char *local, const char *basename)
273 if (pipe (fd) == -1) {
284 if (pid > 0) { /* Parent */
294 if (chdir (local) == -1) {
296 _exit (EXIT_FAILURE);
299 mkdir (basename, 0777);
301 if (chdir (basename) == -1) {
303 _exit (EXIT_FAILURE);
306 execlp ("tar", "tar", "-xf", "-", NULL);
307 perror ("execlp: tar");
308 _exit (EXIT_FAILURE);