avoid leak upon failed realloc
[libguestfs.git] / daemon / upload.c
1 /* libguestfs - the guestfsd daemon
2  * Copyright (C) 2009 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, int 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_upload (char *filename)
40 {
41   int err, fd, r, is_dev;
42
43   is_dev = strncmp (filename, "/dev/", 5) == 0;
44   if (!is_dev) {
45     if (!root_mounted || filename[0] != '/') {
46       cancel_receive ();
47       reply_with_error ("upload: root must be mounted and path must be absolute");
48       return -1;
49     }
50   }
51
52   if (!is_dev) CHROOT_IN;
53   fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666);
54   if (!is_dev) CHROOT_OUT;
55   if (fd == -1) {
56     err = errno;
57     cancel_receive ();
58     errno = err;
59     reply_with_perror ("%s", filename);
60     return -1;
61   }
62
63   r = receive_file (write_cb, &fd);
64   if (r == -1) {                /* write error */
65     err = errno;
66     cancel_receive ();
67     errno = err;
68     reply_with_perror ("write: %s", filename);
69     close (fd);
70     return -1;
71   }
72   if (r == -2) {                /* cancellation from library */
73     close (fd);
74     /* Do NOT send any error. */
75     return -1;
76   }
77
78   if (close (fd) == -1) {
79     err = errno;
80     cancel_receive ();
81     errno = err;
82     reply_with_perror ("close: %s", filename);
83     return -1;
84   }
85
86   return 0;
87 }
88
89 /* Has one FileOut parameter. */
90 int
91 do_download (char *filename)
92 {
93   int fd, r, is_dev;
94   char buf[GUESTFS_MAX_CHUNK_SIZE];
95
96   NEED_ROOT_OR_IS_DEVICE (filename, -1);
97
98   is_dev = strncmp (filename, "/dev/", 5) == 0;
99
100   if (!is_dev) CHROOT_IN;
101   fd = open (filename, O_RDONLY);
102   if (!is_dev) CHROOT_OUT;
103   if (fd == -1) {
104     reply_with_perror ("%s", filename);
105     return -1;
106   }
107
108   /* Now we must send the reply message, before the file contents.  After
109    * this there is no opportunity in the protocol to send any error
110    * message back.  Instead we can only cancel the transfer.
111    */
112   reply (NULL, NULL);
113
114   while ((r = read (fd, buf, sizeof buf)) > 0) {
115     if (send_file_write (buf, r) < 0) {
116       close (fd);
117       return -1;
118     }
119   }
120
121   if (r == -1) {
122     perror (filename);
123     send_file_end (1);          /* Cancel. */
124     close (fd);
125     return -1;
126   }
127
128   if (close (fd) == -1) {
129     perror (filename);
130     send_file_end (1);          /* Cancel. */
131     return -1;
132   }
133
134   send_file_end (0);            /* Normal end of file. */
135   return 0;
136 }