#include "daemon.h"
#include "actions.h"
+struct write_cb_data {
+ int fd; /* file descriptor */
+ uint64_t written; /* bytes written so far */
+};
+
static int
-write_cb (void *fd_ptr, const void *buf, size_t len)
+write_cb (void *data_vp, const void *buf, size_t len)
{
- int fd = *(int *)fd_ptr;
- return xwrite (fd, buf, len);
+ struct write_cb_data *data = data_vp;
+ int r;
+
+ r = xwrite (data->fd, buf, len);
+ if (r == -1)
+ return -1;
+
+ data->written += len;
+
+ if (progress_hint > 0)
+ notify_progress (data->written, progress_hint);
+
+ return 0;
}
/* Has one FileIn parameter. */
static int
upload (const char *filename, int flags, int64_t offset)
{
- int err, fd, r, is_dev;
+ struct write_cb_data data = { .written = 0 };
+ int err, r, is_dev;
is_dev = STRPREFIX (filename, "/dev/");
if (!is_dev) CHROOT_IN;
- fd = open (filename, flags, 0666);
+ data.fd = open (filename, flags, 0666);
if (!is_dev) CHROOT_OUT;
- if (fd == -1) {
+ if (data.fd == -1) {
err = errno;
r = cancel_receive ();
errno = err;
}
if (offset) {
- if (lseek (fd, offset, SEEK_SET) == -1) {
+ if (lseek (data.fd, offset, SEEK_SET) == -1) {
err = errno;
r = cancel_receive ();
errno = err;
}
}
- r = receive_file (write_cb, &fd);
+ r = receive_file (write_cb, &data.fd);
if (r == -1) { /* write error */
err = errno;
r = cancel_receive ();
errno = err;
if (r != -2) reply_with_error ("write error: %s", filename);
- close (fd);
+ close (data.fd);
return -1;
}
if (r == -2) { /* cancellation from library */
- close (fd);
+ close (data.fd);
/* Do NOT send any error. */
return -1;
}
- if (close (fd) == -1) {
+ if (close (data.fd) == -1) {
err = errno;
if (r == -1) /* if r == 0, file transfer ended already */
r = cancel_receive ();
This uses the L<blockdev(8)> command.");
- ("upload", (RErr, [FileIn "filename"; Dev_or_Path "remotefilename"], []), 66, [],
+ ("upload", (RErr, [FileIn "filename"; Dev_or_Path "remotefilename"], []), 66, [Progress],
[InitScratchFS, Always, TestOutput (
(* Pick a file from cwd which isn't likely to change. *)
[["mkdir"; "/upload"];
The named partition must exist, for example as a string returned
from C<guestfs_list_partitions>.");
- ("upload_offset", (RErr, [FileIn "filename"; Dev_or_Path "remotefilename"; Int64 "offset"], []), 273, [],
+ ("upload_offset", (RErr, [FileIn "filename"; Dev_or_Path "remotefilename"; Int64 "offset"], []), 273, [Progress],
(let md5 = Digest.to_hex (Digest.file "COPYING.LIB") in
[InitScratchFS, Always, TestOutput (
[["upload_offset"; "../COPYING.LIB"; "/upload_offset"; "0"];