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"
35 write_cb (void *fd_ptr, const void *buf, size_t len)
37 int fd = *(int *)fd_ptr;
38 return xwrite (fd, buf, len);
41 /* Has one FileIn parameter. */
43 upload (const char *filename, int flags, int64_t offset)
45 int err, fd, r, is_dev;
47 is_dev = STRPREFIX (filename, "/dev/");
49 if (!is_dev) CHROOT_IN;
50 fd = open (filename, flags, 0666);
51 if (!is_dev) CHROOT_OUT;
54 r = cancel_receive ();
56 if (r != -2) reply_with_perror ("%s", filename);
61 if (lseek (fd, offset, SEEK_SET) == -1) {
63 r = cancel_receive ();
65 if (r != -2) reply_with_perror ("lseek: %s", filename);
70 r = receive_file (write_cb, &fd);
71 if (r == -1) { /* write error */
73 r = cancel_receive ();
75 if (r != -2) reply_with_error ("write error: %s", filename);
79 if (r == -2) { /* cancellation from library */
81 /* Do NOT send any error. */
85 if (close (fd) == -1) {
87 if (r == -1) /* if r == 0, file transfer ended already */
88 r = cancel_receive ();
91 reply_with_perror ("close: %s", filename);
98 /* Has one FileIn parameter. */
100 do_upload (const char *filename)
102 return upload (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0);
105 /* Has one FileIn parameter. */
107 do_upload_offset (const char *filename, int64_t offset)
110 reply_with_perror ("%s: offset in file is negative", filename);
114 return upload (filename, O_WRONLY|O_CREAT|O_NOCTTY, offset);
117 /* Has one FileOut parameter. */
119 do_download (const char *filename)
122 char buf[GUESTFS_MAX_CHUNK_SIZE];
124 is_dev = STRPREFIX (filename, "/dev/");
126 if (!is_dev) CHROOT_IN;
127 fd = open (filename, O_RDONLY);
128 if (!is_dev) CHROOT_OUT;
130 reply_with_perror ("%s", filename);
134 /* Calculate the size of the file or device for notification messages. */
135 uint64_t total, sent = 0;
138 if (fstat (fd, &statbuf) == -1) {
139 reply_with_perror ("%s", filename);
143 total = statbuf.st_size;
145 int64_t size = do_blockdev_getsize64 (filename);
147 /* do_blockdev_getsize64 has already sent a reply. */
151 total = (uint64_t) size;
154 /* Now we must send the reply message, before the file contents. After
155 * this there is no opportunity in the protocol to send any error
156 * message back. Instead we can only cancel the transfer.
160 while ((r = read (fd, buf, sizeof buf)) > 0) {
161 if (send_file_write (buf, r) < 0) {
167 notify_progress (sent, total);
172 send_file_end (1); /* Cancel. */
177 if (close (fd) == -1) {
179 send_file_end (1); /* Cancel. */
183 if (send_file_end (0)) /* Normal end of file. */
189 /* Has one FileOut parameter. */
191 do_download_offset (const char *filename, int64_t offset, int64_t size)
194 char buf[GUESTFS_MAX_CHUNK_SIZE];
197 reply_with_perror ("%s: offset in file is negative", filename);
202 reply_with_perror ("%s: size is negative", filename);
205 uint64_t usize = (uint64_t) size;
207 is_dev = STRPREFIX (filename, "/dev/");
209 if (!is_dev) CHROOT_IN;
210 fd = open (filename, O_RDONLY);
211 if (!is_dev) CHROOT_OUT;
213 reply_with_perror ("%s", filename);
218 if (lseek (fd, offset, SEEK_SET) == -1) {
219 reply_with_perror ("lseek: %s", filename);
224 uint64_t total = usize, sent = 0;
226 /* Now we must send the reply message, before the file contents. After
227 * this there is no opportunity in the protocol to send any error
228 * message back. Instead we can only cancel the transfer.
233 r = read (fd, buf, usize > sizeof buf ? sizeof buf : usize);
236 send_file_end (1); /* Cancel. */
242 /* The documentation leaves this case undefined. Currently we
243 * just read fewer bytes than requested.
247 if (send_file_write (buf, r) < 0) {
254 notify_progress (sent, total);
257 if (close (fd) == -1) {
259 send_file_end (1); /* Cancel. */
263 if (send_file_end (0)) /* Normal end of file. */