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.
27 #include <sys/types.h>
30 #include "guestfs_protocol.h"
34 struct write_cb_data {
35 int fd; /* file descriptor */
36 uint64_t written; /* bytes written so far */
40 write_cb (void *data_vp, const void *buf, size_t len)
42 struct write_cb_data *data = data_vp;
45 r = xwrite (data->fd, buf, len);
51 if (progress_hint > 0)
52 notify_progress (data->written, progress_hint);
57 /* Has one FileIn parameter. */
59 upload (const char *filename, int flags, int64_t offset)
61 struct write_cb_data data = { .written = 0 };
64 is_dev = STRPREFIX (filename, "/dev/");
66 if (!is_dev) CHROOT_IN;
67 data.fd = open (filename, flags, 0666);
68 if (!is_dev) CHROOT_OUT;
71 r = cancel_receive ();
73 if (r != -2) reply_with_perror ("%s", filename);
78 if (lseek (data.fd, offset, SEEK_SET) == -1) {
80 r = cancel_receive ();
82 if (r != -2) reply_with_perror ("lseek: %s", filename);
87 r = receive_file (write_cb, &data.fd);
88 if (r == -1) { /* write error */
90 r = cancel_receive ();
92 if (r != -2) reply_with_error ("write error: %s", filename);
96 if (r == -2) { /* cancellation from library */
98 /* Do NOT send any error. */
102 if (close (data.fd) == -1) {
104 if (r == -1) /* if r == 0, file transfer ended already */
105 r = cancel_receive ();
108 reply_with_perror ("close: %s", filename);
115 /* Has one FileIn parameter. */
117 do_upload (const char *filename)
119 return upload (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0);
122 /* Has one FileIn parameter. */
124 do_upload_offset (const char *filename, int64_t offset)
127 reply_with_perror ("%s: offset in file is negative", filename);
131 return upload (filename, O_WRONLY|O_CREAT|O_NOCTTY, offset);
134 /* Has one FileOut parameter. */
136 do_download (const char *filename)
139 char buf[GUESTFS_MAX_CHUNK_SIZE];
141 is_dev = STRPREFIX (filename, "/dev/");
143 if (!is_dev) CHROOT_IN;
144 fd = open (filename, O_RDONLY);
145 if (!is_dev) CHROOT_OUT;
147 reply_with_perror ("%s", filename);
151 /* Calculate the size of the file or device for notification messages. */
152 uint64_t total, sent = 0;
155 if (fstat (fd, &statbuf) == -1) {
156 reply_with_perror ("%s", filename);
160 total = statbuf.st_size;
162 int64_t size = do_blockdev_getsize64 (filename);
164 /* do_blockdev_getsize64 has already sent a reply. */
168 total = (uint64_t) size;
171 /* Now we must send the reply message, before the file contents. After
172 * this there is no opportunity in the protocol to send any error
173 * message back. Instead we can only cancel the transfer.
177 while ((r = read (fd, buf, sizeof buf)) > 0) {
178 if (send_file_write (buf, r) < 0) {
184 notify_progress (sent, total);
189 send_file_end (1); /* Cancel. */
194 if (close (fd) == -1) {
196 send_file_end (1); /* Cancel. */
200 if (send_file_end (0)) /* Normal end of file. */
206 /* Has one FileOut parameter. */
208 do_download_offset (const char *filename, int64_t offset, int64_t size)
211 char buf[GUESTFS_MAX_CHUNK_SIZE];
214 reply_with_perror ("%s: offset in file is negative", filename);
219 reply_with_perror ("%s: size is negative", filename);
222 uint64_t usize = (uint64_t) size;
224 is_dev = STRPREFIX (filename, "/dev/");
226 if (!is_dev) CHROOT_IN;
227 fd = open (filename, O_RDONLY);
228 if (!is_dev) CHROOT_OUT;
230 reply_with_perror ("%s", filename);
235 if (lseek (fd, offset, SEEK_SET) == -1) {
236 reply_with_perror ("lseek: %s", filename);
241 uint64_t total = usize, sent = 0;
243 /* Now we must send the reply message, before the file contents. After
244 * this there is no opportunity in the protocol to send any error
245 * message back. Instead we can only cancel the transfer.
250 r = read (fd, buf, usize > sizeof buf ? sizeof buf : usize);
253 send_file_end (1); /* Cancel. */
259 /* The documentation leaves this case undefined. Currently we
260 * just read fewer bytes than requested.
264 if (send_file_write (buf, r) < 0) {
271 notify_progress (sent, total);
274 if (close (fd) == -1) {
276 send_file_end (1); /* Cancel. */
280 if (send_file_end (0)) /* Normal end of file. */