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 "../src/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 size)
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);
117 size_t n = size > (int64_t) (sizeof buf) ? sizeof buf : (size_t) size;
118 ssize_t r = read (src_fd, buf, n);
120 reply_with_perror ("%s: read", src);
126 reply_with_error ("%s: input file too short", src);
132 if (xwrite (dest_fd, buf, r) == -1) {
133 reply_with_perror ("%s: write", dest);
142 if (close (src_fd) == -1) {
143 reply_with_perror ("%s: close", src);
147 if (close (dest_fd) == -1) {
148 reply_with_perror ("%s: close", dest);