If qemu dies during launch, set an error message (RHBZ#588851).
[libguestfs.git] / daemon / base64.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2010 Red Hat Inc.
3  *
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.
8  *
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.
13  *
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.
17  */
18
19 #include <config.h>
20
21 #include <stdio.h>
22 #include <stdlib.h>
23 #include <string.h>
24 #include <fcntl.h>
25
26 #include "../src/guestfs_protocol.h"
27 #include "daemon.h"
28 #include "actions.h"
29
30 static int
31 write_cb (void *fd_ptr, const void *buf, size_t len)
32 {
33   int fd = *(int *)fd_ptr;
34   return xwrite (fd, buf, len);
35 }
36
37 /* Has one FileIn parameter. */
38 int
39 do_base64_in (const char *file)
40 {
41   int err, r;
42   FILE *fp;
43   char *cmd;
44
45   if (asprintf_nowarn (&cmd, "base64 -d > %R", file) == -1) {
46     err = errno;
47     cancel_receive ();
48     errno = err;
49     reply_with_perror ("asprintf");
50     return -1;
51   }
52
53   if (verbose)
54     fprintf (stderr, "%s\n", cmd);
55
56   fp = popen (cmd, "w");
57   if (fp == NULL) {
58     err = errno;
59     cancel_receive ();
60     errno = err;
61     reply_with_perror ("%s", cmd);
62     free (cmd);
63     return -1;
64   }
65   free (cmd);
66
67   /* The semantics of fwrite are too undefined, so write to the
68    * file descriptor directly instead.
69    */
70   int fd = fileno (fp);
71
72   r = receive_file (write_cb, &fd);
73   if (r == -1) {                /* write error */
74     cancel_receive ();
75     reply_with_error ("write error on file: %s", file);
76     pclose (fp);
77     return -1;
78   }
79   if (r == -2) {                /* cancellation from library */
80     pclose (fp);
81     /* Do NOT send any error. */
82     return -1;
83   }
84
85   if (pclose (fp) != 0) {
86     if (r == -1)                /* if r == 0, file transfer ended already */
87       cancel_receive ();
88     reply_with_error ("base64 subcommand failed on file: %s", file);
89     return -1;
90   }
91
92   return 0;
93 }
94
95 /* Has one FileOut parameter. */
96 int
97 do_base64_out (const char *file)
98 {
99   int r;
100   FILE *fp;
101   char *cmd;
102   char buf[GUESTFS_MAX_CHUNK_SIZE];
103
104   if (asprintf_nowarn (&cmd, "base64 %R", file) == -1) {
105     reply_with_perror ("asprintf");
106     return -1;
107   }
108
109   if (verbose)
110     fprintf (stderr, "%s\n", cmd);
111
112   fp = popen (cmd, "r");
113   if (fp == NULL) {
114     reply_with_perror ("%s", cmd);
115     free (cmd);
116     return -1;
117   }
118   free (cmd);
119
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.
123    */
124   reply (NULL, NULL);
125
126   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
127     if (send_file_write (buf, r) < 0) {
128       pclose (fp);
129       return -1;
130     }
131   }
132
133   if (ferror (fp)) {
134     perror (file);
135     send_file_end (1);          /* Cancel. */
136     pclose (fp);
137     return -1;
138   }
139
140   if (pclose (fp) != 0) {
141     perror (file);
142     send_file_end (1);          /* Cancel. */
143     return -1;
144   }
145
146   if (send_file_end (0))        /* Normal end of file. */
147     return -1;
148
149   return 0;
150 }