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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
26 #include <sys/types.h>
33 int fd; /* -1 == error */
37 static struct fd_pid make_tar_from_local (const char *local);
38 static struct fd_pid make_tar_output (const char *local, const char *basename);
39 static int split_path (char *buf, size_t buf_size, const char *path, const char **dirname, const char **basename);
42 run_copy_in (const char *cmd, size_t argc, char *argv[])
46 _("use 'copy-in <local> [<local>...] <remotedir>' to copy files into the image\n"));
50 /* Remote directory is always the last arg. */
51 char *remote = argv[argc-1];
53 /* Allow win: prefix on remote. */
54 remote = win_prefix (remote);
58 int nr_locals = argc-1;
60 int remote_is_dir = guestfs_is_dir (g, remote);
61 if (remote_is_dir == -1) {
67 fprintf (stderr, _("copy-in: target '%s' is not a directory\n"), remote);
72 /* Upload each local one at a time using tar-in. */
74 for (i = 0; i < nr_locals; ++i) {
75 struct fd_pid fdpid = make_tar_from_local (argv[i]);
82 snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fdpid.fd);
84 int r = guestfs_tar_in (g, fdbuf, remote);
86 if (close (fdpid.fd) == -1) {
87 perror ("close (tar-from-local subprocess)");
92 if (waitpid (fdpid.pid, &status, 0) == -1) {
93 perror ("wait (tar-from-local subprocess)");
97 if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0)) {
113 static void tar_create (const char *dir, const char *path) __attribute__((noreturn));
115 /* This creates a subprocess which feeds a tar file back to the
116 * main guestfish process.
119 make_tar_from_local (const char *local)
122 struct fd_pid r = { .fd = -1 };
124 if (pipe (fd) == -1) {
135 if (r.pid > 0) { /* Parent */
147 const char *dirname, *basename;
148 if (split_path (buf, sizeof buf, local, &dirname, &basename) == -1)
149 _exit (EXIT_FAILURE);
151 tar_create (dirname, basename);
154 /* Split path into directory name and base name, using the buffer
155 * provided as a working area. If there is no directory name
156 * (eg. path == "/") then this can return dirname as NULL.
159 split_path (char *buf, size_t buf_size,
160 const char *path, const char **dirname, const char **basename)
162 size_t len = strlen (path);
163 if (len == 0 || len > buf_size - 1) {
164 fprintf (stderr, _("error: argument is zero length or longer than maximum permitted\n"));
170 if (len >= 2 && buf[len-1] == '/') {
175 char *p = strrchr (buf, '/');
176 if (p && p > buf) { /* "foo/bar" */
179 if (dirname) *dirname = buf;
180 if (basename) *basename = p;
181 } else if (p && p == buf) { /* "/foo" */
182 if (dirname) *dirname = "/";
183 if (basename) *basename = buf+1;
185 if (dirname) *dirname = NULL;
186 if (basename) *basename = buf;
193 tar_create (const char *dir, const char *path)
196 execlp ("tar", "tar", "-C", dir, "-cf", "-", path, NULL);
198 execlp ("tar", "tar", "-cf", "-", path, NULL);
200 perror ("execlp: tar");
201 _exit (EXIT_FAILURE);
205 run_copy_out (const char *cmd, size_t argc, char *argv[])
209 _("use 'copy-out <remote> [<remote>...] <localdir>' to copy files out of the image\n"));
213 /* Local directory is always the last arg. */
214 const char *local = argv[argc-1];
215 int nr_remotes = argc-1;
218 if (stat (local, &statbuf) == -1 ||
219 ! (S_ISDIR (statbuf.st_mode))) {
220 fprintf (stderr, _("copy-out: target '%s' is not a directory\n"), local);
224 /* Download each remote one at a time using tar-out. */
226 for (i = 0; i < nr_remotes; ++i) {
227 char *remote = argv[i];
229 /* Allow win:... prefix on remotes. */
230 remote = win_prefix (remote);
234 /* If the remote is a file, download it. If it's a directory,
235 * create the directory in local first before using tar-out.
237 r = guestfs_is_file (g, remote);
242 if (r == 1) { /* is file */
244 const char *basename;
245 if (split_path (buf, sizeof buf, remote, NULL, &basename) == -1) {
250 char filename[PATH_MAX];
251 snprintf (filename, sizeof filename, "%s/%s", local, basename);
252 if (guestfs_download (g, remote, filename) == -1) {
257 else { /* not a regular file */
258 r = guestfs_is_dir (g, remote);
265 fprintf (stderr, _("copy-out: '%s' is not a file or directory\n"),
272 const char *basename;
273 if (split_path (buf, sizeof buf, remote, NULL, &basename) == -1) {
278 struct fd_pid fdpid = make_tar_output (local, basename);
279 if (fdpid.fd == -1) {
285 snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fdpid.fd);
287 int r = guestfs_tar_out (g, remote, fdbuf);
289 if (close (fdpid.fd) == -1) {
290 perror ("close (tar-output subprocess)");
296 if (waitpid (fdpid.pid, &status, 0) == -1) {
297 perror ("wait (tar-output subprocess)");
301 if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0)) {
318 /* This creates a subprocess which takes a tar file from the main
319 * guestfish process and unpacks it into the 'local/basename'
323 make_tar_output (const char *local, const char *basename)
326 struct fd_pid r = { .fd = -1 };
328 if (pipe (fd) == -1) {
339 if (r.pid > 0) { /* Parent */
350 if (chdir (local) == -1) {
352 _exit (EXIT_FAILURE);
355 mkdir (basename, 0777);
357 if (chdir (basename) == -1) {
359 _exit (EXIT_FAILURE);
362 execlp ("tar", "tar", "-xf", "-", NULL);
363 perror ("execlp: tar");
364 _exit (EXIT_FAILURE);