summaryrefslogtreecommitdiffstats
path: root/fish/copy.c
blob: c1c1b4749b525c6bb25f3d197da09821da893759 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
/* guestfish - the filesystem interactive shell
 * Copyright (C) 2010-2011 Red Hat Inc.
 *
 * This program is free software; you can redistribute it and/or modify
 * it under the terms of the GNU General Public License as published by
 * the Free Software Foundation; either version 2 of the License, or
 * (at your option) any later version.
 *
 * This program is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
 * GNU General Public License for more details.
 *
 * You should have received a copy of the GNU General Public License
 * along with this program; if not, write to the Free Software
 * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <string.h>
#include <unistd.h>
#include <limits.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <sys/wait.h>

#include "fish.h"

static int make_tar_from_local (const char *local);
static int make_tar_output (const char *local, const char *basename);
static int split_path (char *buf, size_t buf_size, const char *path, const char **dirname, const char **basename);

int
run_copy_in (const char *cmd, size_t argc, char *argv[])
{
  if (argc < 2) {
    fprintf (stderr,
             _("use 'copy-in <local> [<local>...] <remotedir>' to copy files into the image\n"));
    return -1;
  }

  /* Remote directory is always the last arg. */
  char *remote = argv[argc-1];

  /* Allow win: prefix on remote. */
  remote = win_prefix (remote);
  if (remote == NULL)
    return -1;

  int nr_locals = argc-1;

  int remote_is_dir = guestfs_is_dir (g, remote);
  if (remote_is_dir == -1) {
    free (remote);
    return -1;
  }

  if (!remote_is_dir) {
    fprintf (stderr, _("copy-in: target '%s' is not a directory\n"), remote);
    free (remote);
    return -1;
  }

  /* Upload each local one at a time using tar-in. */
  int i;
  for (i = 0; i < nr_locals; ++i) {
    int fd = make_tar_from_local (argv[i]);
    if (fd == -1) {
      free (remote);
      return -1;
    }

    char fdbuf[64];
    snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fd);

    int r = guestfs_tar_in (g, fdbuf, remote);

    if (close (fd) == -1) {
      perror ("close (tar-from-local subprocess)");
      r = -1;
    }

    int status;
    if (wait (&status) == -1) {
      perror ("wait (tar-from-local subprocess)");
      free (remote);
      return -1;
    }
    if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0)) {
      free (remote);
      return -1;
    }

    if (r == -1) {
      free (remote);
      return -1;
    }
  }

  free (remote);

  return 0;
}

static void tar_create (const char *dir, const char *path) __attribute__((noreturn));

/* This creates a subprocess which feeds a tar file back to the
 * main guestfish process.
 */
static int
make_tar_from_local (const char *local)
{
  int fd[2];

  if (pipe (fd) == -1) {
    perror ("pipe");
    return -1;
  }

  pid_t pid = fork ();
  if (pid == -1) {
    perror ("fork");
    return -1;
  }

  if (pid > 0) {                /* Parent */
    close (fd[1]);
    return fd[0];
  }

  /* Child. */
  close (fd[0]);
  dup2 (fd[1], 1);
  close (fd[1]);

  char buf[PATH_MAX];
  const char *dirname, *basename;
  if (split_path (buf, sizeof buf, local, &dirname, &basename) == -1)
    _exit (EXIT_FAILURE);

  tar_create (dirname, basename);
}

/* Split path into directory name and base name, using the buffer
 * provided as a working area.  If there is no directory name
 * (eg. path == "/") then this can return dirname as NULL.
 */
static int
split_path (char *buf, size_t buf_size,
            const char *path, const char **dirname, const char **basename)
{
  size_t len = strlen (path);
  if (len == 0 || len > buf_size - 1) {
    fprintf (stderr, _("error: argument is zero length or longer than maximum permitted\n"));
    return -1;
  }

  strcpy (buf, path);

  if (len >= 2 && buf[len-1] == '/') {
    buf[len-1] = '\0';
    len--;
  }

  char *p = strrchr (buf, '/');
  if (p && p > buf) {           /* "foo/bar" */
    *p = '\0';
    p++;
    if (dirname) *dirname = buf;
    if (basename) *basename = p;
  } else if (p && p == buf) {   /* "/foo" */
    if (dirname) *dirname = "/";
    if (basename) *basename = buf+1;
  } else {
    if (dirname) *dirname = NULL;
    if (basename) *basename = buf;
  }

  return 0;
}

static void
tar_create (const char *dir, const char *path)
{
  if (dir)
    execlp ("tar", "tar", "-C", dir, "-cf", "-", path, NULL);
  else
    execlp ("tar", "tar", "-cf", "-", path, NULL);

  perror ("execlp: tar");
  _exit (EXIT_FAILURE);
}

int
run_copy_out (const char *cmd, size_t argc, char *argv[])
{
  if (argc < 2) {
    fprintf (stderr,
             _("use 'copy-out <remote> [<remote>...] <localdir>' to copy files out of the image\n"));
    return -1;
  }

  /* Local directory is always the last arg. */
  const char *local = argv[argc-1];
  int nr_remotes = argc-1;

  struct stat statbuf;
  if (stat (local, &statbuf) == -1 ||
      ! (S_ISDIR (statbuf.st_mode))) {
    fprintf (stderr, _("copy-out: target '%s' is not a directory\n"), local);
    return -1;
  }

  /* Download each remote one at a time using tar-out. */
  int i, r;
  for (i = 0; i < nr_remotes; ++i) {
    char *remote = argv[i];

    /* Allow win:... prefix on remotes. */
    remote = win_prefix (remote);
    if (remote == NULL)
      return -1;

    /* If the remote is a file, download it.  If it's a directory,
     * create the directory in local first before using tar-out.
     */
    r = guestfs_is_file (g, remote);
    if (r == -1) {
      free (remote);
      return -1;
    }
    if (r == 1) {               /* is file */
      char buf[PATH_MAX];
      const char *basename;
      if (split_path (buf, sizeof buf, remote, NULL, &basename) == -1) {
        free (remote);
        return -1;
      }

      char filename[PATH_MAX];
      snprintf (filename, sizeof filename, "%s/%s", local, basename);
      if (guestfs_download (g, remote, filename) == -1) {
        free (remote);
        return -1;
      }
    }
    else {                      /* not a regular file */
      r = guestfs_is_dir (g, remote);
      if (r == -1) {
        free (remote);
        return -1;
      }

      if (r == 0) {
        fprintf (stderr, _("copy-out: '%s' is not a file or directory\n"),
                 remote);
        free (remote);
        return -1;
      }

      char buf[PATH_MAX];
      const char *basename;
      if (split_path (buf, sizeof buf, remote, NULL, &basename) == -1) {
        free (remote);
        return -1;
      }

      int fd = make_tar_output (local, basename);
      if (fd == -1) {
        free (remote);
        return -1;
      }

      char fdbuf[64];
      snprintf (fdbuf, sizeof fdbuf, "/dev/fd/%d", fd);

      int r = guestfs_tar_out (g, remote, fdbuf);

      if (close (fd) == -1) {
        perror ("close (tar-output subprocess)");
        free (remote);
        r = -1;
      }

      int status;
      if (wait (&status) == -1) {
        perror ("wait (tar-output subprocess)");
        free (remote);
        return -1;
      }
      if (!(WIFEXITED (status) && WEXITSTATUS (status) == 0)) {
        free (remote);
        return -1;
      }

      if (r == -1) {
        free (remote);
        return -1;
      }
    }

    free (remote);
  }

  return 0;
}

/* This creates a subprocess which takes a tar file from the main
 * guestfish process and unpacks it into the 'local/basename'
 * directory.
 */
static int
make_tar_output (const char *local, const char *basename)
{
  int fd[2];

  if (pipe (fd) == -1) {
    perror ("pipe");
    return -1;
  }

  pid_t pid = fork ();
  if (pid == -1) {
    perror ("fork");
    return -1;
  }

  if (pid > 0) {                /* Parent */
    close (fd[0]);
    return fd[1];
  }

  /* Child. */
  close (fd[1]);
  dup2 (fd[0], 0);
  close (fd[0]);

  if (chdir (local) == -1) {
    perror (local);
    _exit (EXIT_FAILURE);
  }

  mkdir (basename, 0777);

  if (chdir (basename) == -1) {
    perror (basename);
    _exit (EXIT_FAILURE);
  }

  execlp ("tar", "tar", "-xf", "-", NULL);
  perror ("execlp: tar");
  _exit (EXIT_FAILURE);
}