1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2009 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 "guestfs_protocol.h"
31 do_dd (const char *src, const char *dest)
33 int src_is_dev, dest_is_dev;
34 char *if_arg, *of_arg;
38 src_is_dev = STRPREFIX (src, "/dev/");
41 r = asprintf (&if_arg, "if=%s", src);
43 r = asprintf (&if_arg, "if=%s%s", sysroot, src);
45 reply_with_perror ("asprintf");
49 dest_is_dev = STRPREFIX (dest, "/dev/");
52 r = asprintf (&of_arg, "of=%s", dest);
54 r = asprintf (&of_arg, "of=%s%s", sysroot, dest);
56 reply_with_perror ("asprintf");
61 r = command (NULL, &err, "dd", "bs=1024K", if_arg, of_arg, NULL);
66 reply_with_error ("%s: %s: %s", src, dest, err);
76 do_copy_size (const char *src, const char *dest, int64_t ssize)
81 if (STRPREFIX (src, "/dev/"))
82 src_fd = open (src, O_RDONLY);
84 buf = sysroot_path (src);
86 reply_with_perror ("malloc");
89 src_fd = open (buf, O_RDONLY);
93 reply_with_perror ("%s", src);
97 if (STRPREFIX (dest, "/dev/"))
98 dest_fd = open (dest, O_WRONLY);
100 buf = sysroot_path (dest);
102 reply_with_perror ("malloc");
106 dest_fd = open (buf, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666);
110 reply_with_perror ("%s", dest);
115 uint64_t position = 0, size = (uint64_t) ssize;
117 while (position < size) {
120 /* Calculate bytes to copy. */
121 uint64_t n64 = size - position;
123 if (n64 > sizeof buf)
126 n = (size_t) n64; /* safe because of if condition */
128 ssize_t r = read (src_fd, buf, n);
130 reply_with_perror ("%s: read", src);
136 reply_with_error ("%s: input file too short", src);
142 if (xwrite (dest_fd, buf, r) == -1) {
143 reply_with_perror ("%s: write", dest);
150 notify_progress ((uint64_t) position, (uint64_t) size);
153 if (close (src_fd) == -1) {
154 reply_with_perror ("%s: close", src);
158 if (close (dest_fd) == -1) {
159 reply_with_perror ("%s: close", dest);