daemon: debug segv correct use of dereferencing NULL.
[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., 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 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 "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 -i > %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     /* This error is ignored by the library since it initiated the
81      * cancel.  Nevertheless we must send an error reply here.
82      */
83     reply_with_error ("file upload cancelled");
84     pclose (fp);
85     return -1;
86   }
87
88   if (pclose (fp) != 0) {
89     reply_with_error ("base64 subcommand failed on file: %s", file);
90     return -1;
91   }
92
93   return 0;
94 }
95
96 /* Has one FileOut parameter. */
97 int
98 do_base64_out (const char *file)
99 {
100   int r;
101   FILE *fp;
102   char *cmd;
103   char buf[GUESTFS_MAX_CHUNK_SIZE];
104
105   if (asprintf_nowarn (&cmd, "base64 %R", file) == -1) {
106     reply_with_perror ("asprintf");
107     return -1;
108   }
109
110   if (verbose)
111     fprintf (stderr, "%s\n", cmd);
112
113   fp = popen (cmd, "r");
114   if (fp == NULL) {
115     reply_with_perror ("%s", cmd);
116     free (cmd);
117     return -1;
118   }
119   free (cmd);
120
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.
124    */
125   reply (NULL, NULL);
126
127   while ((r = fread (buf, 1, sizeof buf, fp)) > 0) {
128     if (send_file_write (buf, r) < 0) {
129       pclose (fp);
130       return -1;
131     }
132   }
133
134   if (ferror (fp)) {
135     perror (file);
136     send_file_end (1);          /* Cancel. */
137     pclose (fp);
138     return -1;
139   }
140
141   if (pclose (fp) != 0) {
142     perror (file);
143     send_file_end (1);          /* Cancel. */
144     return -1;
145   }
146
147   if (send_file_end (0))        /* Normal end of file. */
148     return -1;
149
150   return 0;
151 }