1 /* guestfish - the filesystem interactive shell
2 * Copyright (C) 2010-2011 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 char *remote = argv[argc-1];
48 /* Allow win: prefix on remote. */
49 remote = win_prefix (remote);
53 int nr_locals = argc-1;
55 int remote_is_dir = guestfs_is_dir (g, remote);
56 if (remote_is_dir == -1) {
62 fprintf (stderr, _("copy-in: target '%s' is not a directory\n"), remote);
67 /* Upload each local one at a time using tar-in. */
69 for (i = 0; i < nr_locals; ++i) {
70 int fd = make_tar_from_local (argv[i]);
77 snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fd);
79 int r = guestfs_tar_in (g, fdbuf, remote);
81 if (close (fd) == -1) {
82 perror ("close (tar-from-local subprocess)");
87 if (wait (&status) == -1) {
88 perror ("wait (tar-from-local subprocess)");
92 if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0)) {
108 static void tar_create (const char *dir, const char *path) __attribute__((noreturn));
110 /* This creates a subprocess which feeds a tar file back to the
111 * main guestfish process.
114 make_tar_from_local (const char *local)
118 if (pipe (fd) == -1) {
129 if (pid > 0) { /* Parent */
140 const char *dirname, *basename;
141 if (split_path (buf, sizeof buf, local, &dirname, &basename) == -1)
142 _exit (EXIT_FAILURE);
144 tar_create (dirname, basename);
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.
152 split_path (char *buf, size_t buf_size,
153 const char *path, const char **dirname, const char **basename)
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"));
163 if (len >= 2 && buf[len-1] == '/') {
168 char *p = strrchr (buf, '/');
169 if (p && p > buf) { /* "foo/bar" */
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;
178 if (dirname) *dirname = NULL;
179 if (basename) *basename = buf;
186 tar_create (const char *dir, const char *path)
189 execlp ("tar", "tar", "-C", dir, "-cf", "-", path, NULL);
191 execlp ("tar", "tar", "-cf", "-", path, NULL);
193 perror ("execlp: tar");
194 _exit (EXIT_FAILURE);
198 run_copy_out (const char *cmd, size_t argc, char *argv[])
202 _("use 'copy-out <remote> [<remote>...] <localdir>' to copy files out of the image\n"));
206 /* Local directory is always the last arg. */
207 const char *local = argv[argc-1];
208 int nr_remotes = argc-1;
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);
217 /* Download each remote one at a time using tar-out. */
219 for (i = 0; i < nr_remotes; ++i) {
220 char *remote = argv[i];
222 /* Allow win:... prefix on remotes. */
223 remote = win_prefix (remote);
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.
230 r = guestfs_is_file (g, remote);
235 if (r == 1) { /* is file */
237 const char *basename;
238 if (split_path (buf, sizeof buf, remote, NULL, &basename) == -1) {
243 char filename[PATH_MAX];
244 snprintf (filename, sizeof filename, "%s/%s", local, basename);
245 if (guestfs_download (g, remote, filename) == -1) {
250 else { /* not a regular file */
251 r = guestfs_is_dir (g, remote);
258 fprintf (stderr, _("copy-out: '%s' is not a file or directory\n"),
265 const char *basename;
266 if (split_path (buf, sizeof buf, remote, NULL, &basename) == -1) {
271 int fd = make_tar_output (local, basename);
278 snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fd);
280 int r = guestfs_tar_out (g, remote, fdbuf);
282 if (close (fd) == -1) {
283 perror ("close (tar-output subprocess)");
289 if (wait (&status) == -1) {
290 perror ("wait (tar-output subprocess)");
294 if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0)) {
311 /* This creates a subprocess which takes a tar file from the main
312 * guestfish process and unpacks it into the 'local/basename'
316 make_tar_output (const char *local, const char *basename)
320 if (pipe (fd) == -1) {
331 if (pid > 0) { /* Parent */
341 if (chdir (local) == -1) {
343 _exit (EXIT_FAILURE);
346 mkdir (basename, 0777);
348 if (chdir (basename) == -1) {
350 _exit (EXIT_FAILURE);
353 execlp ("tar", "tar", "-xf", "-", NULL);
354 perror ("execlp: tar");
355 _exit (EXIT_FAILURE);