1 /* libguestfs - the guestfsd daemon
2 * Copyright (C) 2010 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 write_cb (void *fd_ptr, const void *buf, size_t len)
33 int fd = *(int *)fd_ptr;
34 return xwrite (fd, buf, len);
37 /* Has one FileIn parameter. */
39 do_base64_in (const char *file)
45 if (asprintf_nowarn (&cmd, "base64 -d -i > %R", file) == -1) {
49 reply_with_perror ("asprintf");
54 fprintf (stderr, "%s\n", cmd);
56 fp = popen (cmd, "w");
61 reply_with_perror ("%s", cmd);
67 /* The semantics of fwrite are too undefined, so write to the
68 * file descriptor directly instead.
72 r = receive_file (write_cb, &fd);
73 if (r == -1) { /* write error */
75 reply_with_error ("write error on file: %s", file);
79 if (r == -2) { /* cancellation from library */
80 /* This error is ignored by the library since it initiated the
81 * cancel. Nevertheless we must send an error reply here.
83 reply_with_error ("file upload cancelled");
88 if (pclose (fp) != 0) {
89 reply_with_error ("base64 subcommand failed on file: %s", file);
96 /* Has one FileOut parameter. */
98 do_base64_out (const char *file)
103 char buf[GUESTFS_MAX_CHUNK_SIZE];
105 if (asprintf_nowarn (&cmd, "base64 %R", file) == -1) {
106 reply_with_perror ("asprintf");
111 fprintf (stderr, "%s\n", cmd);
113 fp = popen (cmd, "r");
115 reply_with_perror ("%s", cmd);
121 /* Now we must send the reply message, before the file contents. After
122 * this there is no opportunity in the protocol to send any error
123 * message back. Instead we can only cancel the transfer.
127 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
128 if (send_file_write (buf, r) < 0) {
136 send_file_end (1); /* Cancel. */
141 if (pclose (fp) != 0) {
143 send_file_end (1); /* Cancel. */
147 if (send_file_end (0)) /* Normal end of file. */