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 write_cb (void *fd_ptr, const void *buf, int len)
33 int fd = *(int *)fd_ptr;
34 return xwrite (fd, buf, len);
37 /* Has one FileIn parameter. */
39 do_upload (const char *filename)
41 int err, fd, r, is_dev;
43 NEED_ROOT_OR_IS_DEVICE (filename, -1);
45 is_dev = strncmp (filename, "/dev/", 5) == 0;
47 if (!is_dev) CHROOT_IN;
48 fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666);
49 if (!is_dev) CHROOT_OUT;
54 reply_with_perror ("%s", filename);
58 r = receive_file (write_cb, &fd);
59 if (r == -1) { /* write error */
63 reply_with_perror ("write: %s", filename);
66 if (r == -2) { /* cancellation from library */
68 /* Do NOT send any error. */
72 if (close (fd) == -1) {
76 reply_with_perror ("close: %s", filename);
83 /* Has one FileOut parameter. */
85 do_download (const char *filename)
88 char buf[GUESTFS_MAX_CHUNK_SIZE];
90 NEED_ROOT_OR_IS_DEVICE (filename, -1);
92 is_dev = strncmp (filename, "/dev/", 5) == 0;
94 if (!is_dev) CHROOT_IN;
95 fd = open (filename, O_RDONLY);
96 if (!is_dev) CHROOT_OUT;
98 reply_with_perror ("%s", filename);
102 /* Now we must send the reply message, before the file contents. After
103 * this there is no opportunity in the protocol to send any error
104 * message back. Instead we can only cancel the transfer.
108 while ((r = read (fd, buf, sizeof buf)) > 0) {
109 if (send_file_write (buf, r) < 0)
115 send_file_end (1); /* Cancel. */
119 if (close (fd) == -1) {
121 send_file_end (1); /* Cancel. */
125 send_file_end (0); /* Normal end of file. */