fish: const-correctness fixes in copy.c
[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, const char **dirname, const 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   const 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, const char **dirname, const 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) {           /* "foo/bar" */
152     *p = '\0';
153     p++;
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;
159   } else {
160     if (dirname) *dirname = NULL;
161     if (basename) *basename = buf;
162   }
163
164   return 0;
165 }
166
167 static void
168 tar_create (const char *dir, const char *path)
169 {
170   if (dir)
171     execlp ("tar", "tar", "-C", dir, "-cf", "-", path, NULL);
172   else
173     execlp ("tar", "tar", "-cf", "-", path, NULL);
174
175   perror ("execlp: tar");
176   _exit (EXIT_FAILURE);
177 }
178
179 int
180 do_copy_out (const char *cmd, int argc, char *argv[])
181 {
182   if (argc < 2) {
183     fprintf (stderr,
184              _("use 'copy-out <remote> [<remote>...] <localdir>' to copy files out of the image\n"));
185     return -1;
186   }
187
188   /* Local directory is always the last arg. */
189   const char *local = argv[argc-1];
190   int nr_remotes = argc-1;
191
192   struct stat statbuf;
193   if (stat (local, &statbuf) == -1 ||
194       ! (S_ISDIR (statbuf.st_mode))) {
195     fprintf (stderr, _("copy-in: target '%s' is not a directory\n"), local);
196     return -1;
197   }
198
199   /* Download each remote one at a time using tar-out. */
200   int i, r;
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.
204      */
205     r = guestfs_is_file (g, argv[i]);
206     if (r == -1)
207       return -1;
208     if (r == 1) {               /* is file */
209       char buf[PATH_MAX];
210       const char *basename;
211       if (split_path (buf, sizeof buf, argv[i], NULL, &basename) == -1)
212         return -1;
213
214       char filename[PATH_MAX];
215       snprintf (filename, sizeof filename, "%s/%s", local, basename);
216       if (guestfs_download (g, argv[i], filename) == -1)
217         return -1;
218     }
219     else {                      /* not a regular file */
220       r = guestfs_is_dir (g, argv[i]);
221       if (r == -1)
222         return -1;
223
224       if (r == 0) {
225         fprintf (stderr, _("copy-out: '%s' is not a file or directory\n"),
226                  argv[i]);
227         return -1;
228       }
229
230       char buf[PATH_MAX];
231       const char *basename;
232       if (split_path (buf, sizeof buf, argv[i], NULL, &basename) == -1)
233         return -1;
234
235       int fd = make_tar_output (local, basename);
236       if (fd == -1)
237         return -1;
238
239       char fdbuf[64];
240       snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fd);
241
242       int r = guestfs_tar_out (g, argv[i], fdbuf);
243
244       if (close (fd) == -1) {
245         perror ("close (tar-output subprocess)");
246         r = -1;
247       }
248
249       int status;
250       if (wait (&status) == -1) {
251         perror ("wait (tar-output subprocess)");
252         return -1;
253       }
254       if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0))
255         return -1;
256
257       if (r == -1)
258         return -1;
259     }
260   }
261
262   return 0;
263 }
264
265 /* This creates a subprocess which takes a tar file from the main
266  * guestfish process and unpacks it into the 'local/basename'
267  * directory.
268  */
269 static int
270 make_tar_output (const char *local, const char *basename)
271 {
272   int fd[2];
273
274   if (pipe (fd) == -1) {
275     perror ("pipe");
276     return -1;
277   }
278
279   pid_t pid = fork ();
280   if (pid == -1) {
281     perror ("fork");
282     return -1;
283   }
284
285   if (pid > 0) {                /* Parent */
286     close (fd[0]);
287     return fd[1];
288   }
289
290   /* Child. */
291   close (fd[1]);
292   dup2 (fd[0], 0);
293   close (fd[0]);
294
295   if (chdir (local) == -1) {
296     perror (local);
297     _exit (EXIT_FAILURE);
298   }
299
300   mkdir (basename, 0777);
301
302   if (chdir (basename) == -1) {
303     perror (basename);
304     _exit (EXIT_FAILURE);
305   }
306
307   execlp ("tar", "tar", "-xf", "-", NULL);
308   perror ("execlp: tar");
309   _exit (EXIT_FAILURE);
310 }