Matteo Cafasso
2016-Dec-14 22:12 UTC
[Libguestfs] [PATCH] daemon: expose file upload logic
Exposing file upload logic as suggested in previous patch: https://www.redhat.com/archives/libguestfs/2016-November/msg00109.html Matteo Cafasso (1): daemon: expose upload logic daemon/daemon.h | 3 +++ daemon/upload.c | 70 ++++++++++++++++++++++++++++++++------------------------- 2 files changed, 42 insertions(+), 31 deletions(-) -- 2.10.2
Matteo Cafasso
2016-Dec-14 22:12 UTC
[Libguestfs] [PATCH] daemon: expose file upload logic
Allows other modules to use the same logic for uploading files. Signed-off-by: Matteo Cafasso <noxdafox@gmail.com> --- daemon/daemon.h | 3 +++ daemon/upload.c | 70 ++++++++++++++++++++++++++++++++------------------------- 2 files changed, 42 insertions(+), 31 deletions(-) diff --git a/daemon/daemon.h b/daemon/daemon.h index 2379e31..1723b68 100644 --- a/daemon/daemon.h +++ b/daemon/daemon.h @@ -256,6 +256,9 @@ extern int64_t ntfs_minimum_size (const char *device); extern int swap_set_uuid (const char *device, const char *uuid); extern int swap_set_label (const char *device, const char *label); +/*-- in upload.c --*/ +extern int upload_to_fd (int fd); + /* ordinary daemon functions use these to indicate errors * NB: you don't need to prefix the string with the current command, * it is added automatically by the client-side RPC stubs. diff --git a/daemon/upload.c b/daemon/upload.c index 8b4f600..0f385b6 100644 --- a/daemon/upload.c +++ b/daemon/upload.c @@ -54,60 +54,68 @@ write_cb (void *data_vp, const void *buf, size_t len) return 0; } +int +upload_to_fd (int fd) +{ + int ret = 0, err = 0; + struct write_cb_data data = { .fd = fd, .written = 0 }; + + ret = receive_file (write_cb, &data); + if (ret == -1) { /* write error */ + err = errno; + ret = cancel_receive (); + errno = err; + reply_with_error ("write error"); + close (fd); + return -1; + } + if (ret == -2) { /* cancellation from library */ + /* This error is ignored by the library since it initiated the + * cancel. Nevertheless we must send an error reply here. + */ + reply_with_error ("file upload cancelled"); + close (fd); + return -1; + } + + if (close (fd) == -1) { + reply_with_perror ("close"); + return -1; + } + + return 0; +} + /* Has one FileIn parameter. */ static int upload (const char *filename, int flags, int64_t offset) { - struct write_cb_data data = { .written = 0 }; - int err, r, is_dev; + int err, is_dev, fd; is_dev = STRPREFIX (filename, "/dev/"); if (!is_dev) CHROOT_IN; - data.fd = open (filename, flags, 0666); + fd = open (filename, flags, 0666); if (!is_dev) CHROOT_OUT; - if (data.fd == -1) { + if (fd == -1) { err = errno; - r = cancel_receive (); + cancel_receive (); errno = err; reply_with_perror ("%s", filename); return -1; } if (offset) { - if (lseek (data.fd, offset, SEEK_SET) == -1) { + if (lseek (fd, offset, SEEK_SET) == -1) { err = errno; - r = cancel_receive (); + cancel_receive (); errno = err; reply_with_perror ("lseek: %s", filename); return -1; } } - r = receive_file (write_cb, &data); - if (r == -1) { /* write error */ - err = errno; - r = cancel_receive (); - errno = err; - reply_with_error ("write error: %s", filename); - close (data.fd); - return -1; - } - if (r == -2) { /* cancellation from library */ - /* This error is ignored by the library since it initiated the - * cancel. Nevertheless we must send an error reply here. - */ - reply_with_error ("file upload cancelled"); - close (data.fd); - return -1; - } - - if (close (data.fd) == -1) { - reply_with_perror ("close: %s", filename); - return -1; - } - - return 0; + return upload_to_fd (fd); } /* Has one FileIn parameter. */ -- 2.10.2
Richard W.M. Jones
2016-Dec-15 08:59 UTC
Re: [Libguestfs] [PATCH] daemon: expose file upload logic
On Thu, Dec 15, 2016 at 12:12:12AM +0200, Matteo Cafasso wrote:> +int > +upload_to_fd (int fd) > +{ > + int ret = 0, err = 0;Don't initialize these variables (they were not initialized in the previous code). It stops GCC from noticing if you've used the variables uninitialized later. I'm going to wait on this patch to see how the upload_to_fd function is used in later patches. Rich. -- Richard Jones, Virtualization Group, Red Hat http://people.redhat.com/~rjones Read my programming and virtualization blog: http://rwmj.wordpress.com virt-top is 'top' for virtual machines. Tiny program with many powerful monitoring features, net stats, disk stats, logging, etc. http://people.redhat.com/~rjones/virt-top