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