File: copy.c

package info (click to toggle)
libguestfs 1%3A1.44.0-2
  • links: PTS, VCS
  • area: main
  • in suites: bullseye
  • size: 118,932 kB
  • sloc: ansic: 458,017; ml: 51,424; sh: 13,191; java: 9,578; makefile: 7,931; cs: 6,328; haskell: 5,674; python: 3,871; perl: 3,528; erlang: 2,446; xml: 1,347; ruby: 350; pascal: 257; javascript: 157; lex: 135; yacc: 128; cpp: 10
file content (303 lines) | stat: -rw-r--r-- 7,926 bytes parent folder | download | duplicates (5)
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
/* libguestfs - the guestfsd daemon
 * Copyright (C) 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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA.
 */

#include <config.h>

#include <stdio.h>
#include <stdlib.h>
#include <stdint.h>
#include <unistd.h>
#include <fcntl.h>
#include <sys/types.h>

#include "guestfs_protocol.h"
#include "daemon.h"
#include "actions.h"

/* wrflags */
#define DEST_DEVICE_FLAGS O_WRONLY|O_CLOEXEC, 0

/* flags */
#define COPY_UNLINK_DEST_ON_FAILURE 1

/* NB: We cheat slightly by assuming that optargs_bitmask is
 * compatible for all four of the calls.  This is true provided they
 * all take the same set of optional arguments.
 */

/* Takes optional arguments, consult optargs_bitmask. */
static int
copy (const char *src, const char *src_display,
      const char *dest, const char *dest_display,
      int wrflags, int wrmode,
      int flags,
      int64_t srcoffset, int64_t destoffset, int64_t size, int sparse)
{
  const int64_t saved_size = size;
  int src_fd, dest_fd;
  CLEANUP_FREE char *buf = NULL;
  size_t n;
  ssize_t r;
  int err;

  buf = malloc (BUFSIZ);
  if (buf == NULL) {
    reply_with_perror ("malloc");
    return -1;
  }

  if ((optargs_bitmask & GUESTFS_COPY_DEVICE_TO_DEVICE_SRCOFFSET_BITMASK)) {
    if (srcoffset < 0) {
      reply_with_error ("srcoffset is negative");
      return -1;
    }
  }
  else
    srcoffset = 0;

  if ((optargs_bitmask & GUESTFS_COPY_DEVICE_TO_DEVICE_DESTOFFSET_BITMASK)) {
    if (destoffset < 0) {
      reply_with_error ("destoffset is negative");
      return -1;
    }
  }
  else
    destoffset = 0;

  if ((optargs_bitmask & GUESTFS_COPY_DEVICE_TO_DEVICE_SIZE_BITMASK)) {
    if (size < 0) {
      reply_with_error ("size is negative");
      return -1;
    }
  }
  else
    size = -1;

  if (! (optargs_bitmask & GUESTFS_COPY_DEVICE_TO_DEVICE_SPARSE_BITMASK))
    sparse = 0;

  /* Open source and destination. */
  src_fd = open (src, O_RDONLY|O_CLOEXEC);
  if (src_fd == -1) {
    reply_with_perror ("%s", src_display);
    return -1;
  }

  if (srcoffset > 0 && lseek (src_fd, srcoffset, SEEK_SET) == (off_t) -1) {
    reply_with_perror ("lseek: %s", src_display);
    close (src_fd);
    return -1;
  }

  dest_fd = open (dest, wrflags, wrmode);
  if (dest_fd == -1) {
    reply_with_perror ("%s", dest_display);
    close (src_fd);
    return -1;
  }

  if (destoffset > 0 && lseek (dest_fd, destoffset, SEEK_SET) == (off_t) -1) {
    reply_with_perror ("lseek: %s", dest_display);
    close (src_fd);
    close (dest_fd);
    if (flags & COPY_UNLINK_DEST_ON_FAILURE)
      unlink (dest);
    return -1;
  }

  if (size == -1)
    pulse_mode_start ();

  while (size != 0) {
    /* Calculate bytes to copy. */
    if (size == -1 || size > BUFSIZ)
      n = BUFSIZ;
    else
      n = size;

    r = read (src_fd, buf, n);
    if (r == -1) {
      err = errno;
      if (size == -1)
        pulse_mode_cancel ();
      errno = err;
      reply_with_perror ("read: %s", src_display);
      close (src_fd);
      close (dest_fd);
      if (flags & COPY_UNLINK_DEST_ON_FAILURE)
        unlink (dest);
      return -1;
    }

    if (r == 0) {
      if (size == -1) /* if size == -1, this is normal end of loop */
        break;
      reply_with_error ("%s: input too short", src_display);
      close (src_fd);
      close (dest_fd);
      if (flags & COPY_UNLINK_DEST_ON_FAILURE)
        unlink (dest);
      return -1;
    }

    if (sparse && is_zero (buf, r)) {
      if (lseek (dest_fd, r, SEEK_CUR) == -1) {
        err = errno;
        if (size == -1)
          pulse_mode_cancel ();
        errno = err;
        reply_with_perror ("%s: seek (because of sparse flag)", dest_display);
        close (src_fd);
        close (dest_fd);
        if (flags & COPY_UNLINK_DEST_ON_FAILURE)
          unlink (dest);
        return -1;
      }
      goto sparse_skip;
    }

    if (xwrite (dest_fd, buf, r) == -1) {
      err = errno;
      if (size == -1)
        pulse_mode_cancel ();
      errno = err;
      reply_with_perror ("%s: write", dest_display);
      close (src_fd);
      close (dest_fd);
      if (flags & COPY_UNLINK_DEST_ON_FAILURE)
        unlink (dest);
      return -1;
    }
  sparse_skip:

    if (size != -1) {
      size -= r;
      notify_progress ((uint64_t) (saved_size - size), (uint64_t) saved_size);
    }
  }

  if (size == -1)
    pulse_mode_end ();

  if (close (src_fd) == -1) {
    reply_with_perror ("close: %s", src_display);
    close (dest_fd);
    if (flags & COPY_UNLINK_DEST_ON_FAILURE)
      unlink (dest);
    return -1;
  }

  if (close (dest_fd) == -1) {
    reply_with_perror ("close: %s", dest_display);
    if (flags & COPY_UNLINK_DEST_ON_FAILURE)
      unlink (dest);
    return -1;
  }

  return 0;
}

int
do_copy_device_to_device (const char *src, const char *dest,
                          int64_t srcoffset, int64_t destoffset, int64_t size,
                          int sparse, int append)
{
  if ((optargs_bitmask & GUESTFS_COPY_DEVICE_TO_DEVICE_APPEND_BITMASK) &&
      append) {
    reply_with_error ("the append flag cannot be set for this call");
    return -1;
  }
  return copy (src, src, dest, dest, DEST_DEVICE_FLAGS, 0,
               srcoffset, destoffset, size, sparse);
}

int
do_copy_device_to_file (const char *src, const char *dest,
                        int64_t srcoffset, int64_t destoffset, int64_t size,
                        int sparse, int append)
{
  CLEANUP_FREE char *dest_buf = sysroot_path (dest);
  int wrflags = O_WRONLY|O_CREAT|O_NOCTTY|O_CLOEXEC;

  if (!dest_buf) {
    reply_with_perror ("malloc");
    return -1;
  }

  if ((optargs_bitmask & GUESTFS_COPY_DEVICE_TO_FILE_APPEND_BITMASK) &&
      append)
    wrflags |= O_APPEND;
  else
    wrflags |= O_TRUNC;

  return copy (src, src, dest_buf, dest, wrflags, 0666, 0,
               srcoffset, destoffset, size, sparse);
}

int
do_copy_file_to_device (const char *src, const char *dest,
                        int64_t srcoffset, int64_t destoffset, int64_t size,
                        int sparse, int append)
{
  CLEANUP_FREE char *src_buf = sysroot_path (src);

  if (!src_buf) {
    reply_with_perror ("malloc");
    return -1;
  }

  if ((optargs_bitmask & GUESTFS_COPY_FILE_TO_DEVICE_APPEND_BITMASK) &&
      append) {
    reply_with_error ("the append flag cannot be set for this call");
    return -1;
  }

  return copy (src_buf, src, dest, dest, DEST_DEVICE_FLAGS, 0,
               srcoffset, destoffset, size, sparse);
}

int
do_copy_file_to_file (const char *src, const char *dest,
                      int64_t srcoffset, int64_t destoffset, int64_t size,
                      int sparse, int append)
{
  CLEANUP_FREE char *src_buf = NULL, *dest_buf = NULL;
  int wrflags = O_WRONLY|O_CREAT|O_NOCTTY|O_CLOEXEC;

  src_buf = sysroot_path (src);
  if (!src_buf) {
    reply_with_perror ("malloc");
    return -1;
  }

  dest_buf = sysroot_path (dest);
  if (!dest_buf) {
    reply_with_perror ("malloc");
    return -1;
  }

  if ((optargs_bitmask & GUESTFS_COPY_FILE_TO_FILE_APPEND_BITMASK) &&
      append)
    wrflags |= O_APPEND;
  else
    wrflags |= O_TRUNC;

  return copy (src_buf, src, dest_buf, dest, wrflags, 0666,
               COPY_UNLINK_DEST_ON_FAILURE,
               srcoffset, destoffset, size, sparse);
}