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 "../src/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 > %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 */
81 /* Do NOT send any error. */
85 if (pclose (fp) != 0) {
86 if (r == -1) /* if r == 0, file transfer ended already */
88 reply_with_error ("base64 subcommand failed on file: %s", file);
95 /* Has one FileOut parameter. */
97 do_base64_out (const char *file)
102 char buf[GUESTFS_MAX_CHUNK_SIZE];
104 if (asprintf_nowarn (&cmd, "base64 %R", file) == -1) {
105 reply_with_perror ("asprintf");
110 fprintf (stderr, "%s\n", cmd);
112 fp = popen (cmd, "r");
114 reply_with_perror ("%s", cmd);
120 /* Now we must send the reply message, before the file contents. After
121 * this there is no opportunity in the protocol to send any error
122 * message back. Instead we can only cancel the transfer.
126 while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
127 if (send_file_write (buf, r) < 0) {
135 send_file_end (1); /* Cancel. */
140 if (pclose (fp) != 0) {
142 send_file_end (1); /* Cancel. */
146 if (send_file_end (0)) /* Normal end of file. */