debug: Add 'debug progress' command.
[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 <stdint.h>
24 #include <string.h>
25 #include <fcntl.h>
26 #include <unistd.h>
27 #include <sys/types.h>
28 #include <sys/stat.h>
29
30 #include "../src/guestfs_protocol.h"
31 #include "daemon.h"
32 #include "actions.h"
33
34 static int
35 write_cb (void *fd_ptr, const void *buf, size_t len)
36 {
37   int fd = *(int *)fd_ptr;
38   return xwrite (fd, buf, len);
39 }
40
41 /* Has one FileIn parameter. */
42 int
43 do_upload (const char *filename)
44 {
45   int err, fd, r, is_dev;
46
47   is_dev = STRPREFIX (filename, "/dev/");
48
49   if (!is_dev) CHROOT_IN;
50   fd = open (filename, O_WRONLY|O_CREAT|O_TRUNC|O_NOCTTY, 0666);
51   if (!is_dev) CHROOT_OUT;
52   if (fd == -1) {
53     err = errno;
54     r = cancel_receive ();
55     errno = err;
56     if (r != -2) reply_with_perror ("%s", filename);
57     return -1;
58   }
59
60   r = receive_file (write_cb, &fd);
61   if (r == -1) {                /* write error */
62     err = errno;
63     r = cancel_receive ();
64     errno = err;
65     if (r != -2) reply_with_error ("write error: %s", filename);
66     close (fd);
67     return -1;
68   }
69   if (r == -2) {                /* cancellation from library */
70     close (fd);
71     /* Do NOT send any error. */
72     return -1;
73   }
74
75   if (close (fd) == -1) {
76     err = errno;
77     if (r == -1)                /* if r == 0, file transfer ended already */
78       r = cancel_receive ();
79     errno = err;
80     if (r != -2)
81       reply_with_perror ("close: %s", filename);
82     return -1;
83   }
84
85   return 0;
86 }
87
88 /* Has one FileOut parameter. */
89 int
90 do_download (const char *filename)
91 {
92   int fd, r, is_dev;
93   char buf[GUESTFS_MAX_CHUNK_SIZE];
94
95   is_dev = STRPREFIX (filename, "/dev/");
96
97   if (!is_dev) CHROOT_IN;
98   fd = open (filename, O_RDONLY);
99   if (!is_dev) CHROOT_OUT;
100   if (fd == -1) {
101     reply_with_perror ("%s", filename);
102     return -1;
103   }
104
105   /* Calculate the size of the file or device for notification messages. */
106   uint64_t total, sent = 0;
107   if (!is_dev) {
108     struct stat statbuf;
109     if (fstat (fd, &statbuf) == -1) {
110       reply_with_perror ("%s", filename);
111       close (fd);
112       return -1;
113     }
114     total = statbuf.st_size;
115   } else {
116     int64_t size = do_blockdev_getsize64 (filename);
117     if (size == -1) {
118       /* do_blockdev_getsize64 has already sent a reply. */
119       close (fd);
120       return -1;
121     }
122     total = (uint64_t) size;
123   }
124
125   /* Now we must send the reply message, before the file contents.  After
126    * this there is no opportunity in the protocol to send any error
127    * message back.  Instead we can only cancel the transfer.
128    */
129   reply (NULL, NULL);
130
131   while ((r = read (fd, buf, sizeof buf)) > 0) {
132     if (send_file_write (buf, r) < 0) {
133       close (fd);
134       return -1;
135     }
136
137     sent += r;
138     notify_progress (sent, total);
139   }
140
141   if (r == -1) {
142     perror (filename);
143     send_file_end (1);          /* Cancel. */
144     close (fd);
145     return -1;
146   }
147
148   if (close (fd) == -1) {
149     perror (filename);
150     send_file_end (1);          /* Cancel. */
151     return -1;
152   }
153
154   if (send_file_end (0))        /* Normal end of file. */
155     return -1;
156
157   return 0;
158 }