add arguments to runio to allow read/write from/to arbitrary
file descriptors, as opposed to just stdin and stdout.
Signed-off-by: Claudio Fontana <cfontana@suse.de>
---
src/util/iohelper.c | 2 +-
src/util/runio.c | 10 +++++-----
src/util/runio.h | 17 ++++++++++++++++-
3 files changed, 22 insertions(+), 7 deletions(-)
diff --git a/src/util/iohelper.c b/src/util/iohelper.c
index 5a0098542e..93674c1e2f 100644
--- a/src/util/iohelper.c
+++ b/src/util/iohelper.c
@@ -96,7 +96,7 @@ main(int argc, char **argv)
usage(EXIT_FAILURE);
}
- if (fd < 0 || runIO(path, fd, oflags) < 0)
+ if (fd < 0 || runIO(path, fd, oflags, STDIN_FILENO, STDOUT_FILENO) < 0)
goto error;
return 0;
diff --git a/src/util/runio.c b/src/util/runio.c
index a7b902af7e..f42acddae9 100644
--- a/src/util/runio.c
+++ b/src/util/runio.c
@@ -134,7 +134,7 @@ runIOCopy(const struct runIOParams p)
off_t
-runIO(const char *path, int fd, int oflags)
+runIO(const char *path, int fd, int oflags, int in_fd, int out_fd)
{
int ret = -1;
off_t total = 0;
@@ -155,13 +155,13 @@ runIO(const char *path, int fd, int oflags)
p.isWrite = false;
p.fdin = fd;
p.fdinname = path;
- p.fdout = STDOUT_FILENO;
- p.fdoutname = "stdout";
+ p.fdout = out_fd;
+ p.fdoutname = "output";
break;
case O_WRONLY:
p.isWrite = true;
- p.fdin = STDIN_FILENO;
- p.fdinname = "stdin";
+ p.fdin = in_fd;
+ p.fdinname = "input";
p.fdout = fd;
p.fdoutname = path;
break;
diff --git a/src/util/runio.h b/src/util/runio.h
index beb58606c9..66df588881 100644
--- a/src/util/runio.h
+++ b/src/util/runio.h
@@ -20,4 +20,19 @@
#pragma once
-off_t runIO(const char *path, int fd, int oflags);
+/*
+ * runIO: copy unidirectionally all data to/from a file descriptor.
+ *
+ * @path: the pathname corresponding to FD.
+ * @fd: the file descriptor to read from or write to.
+ * @oflags: the file status flags of FD.
+ *
+ * If the oflags indicate O_RDONLY, then the direction will be from FD,
+ * and @out_fd indicates the file to write to.
+ *
+ * If the oflags indicate O_WRONLY, then the direction will be to FD,
+ * and @in_fd indicates the file to read from.
+ *
+ * Returns the number of bytes transferred, or < 0 on error.
+ */
+off_t runIO(const char *path, int fd, int oflags, int in_fd, int out_fd);
--
2.34.1
On Wed, Apr 27, 2022 at 11:13:23PM +0200, Claudio Fontana wrote:
> add arguments to runio to allow read/write from/to arbitrary
> file descriptors, as opposed to just stdin and stdout.
>
> Signed-off-by: Claudio Fontana <cfontana@suse.de>
> ---
> src/util/iohelper.c | 2 +-
> src/util/runio.c | 10 +++++-----
> src/util/runio.h | 17 ++++++++++++++++-
> 3 files changed, 22 insertions(+), 7 deletions(-)
>
> diff --git a/src/util/iohelper.c b/src/util/iohelper.c
> index 5a0098542e..93674c1e2f 100644
> --- a/src/util/iohelper.c
> +++ b/src/util/iohelper.c
> @@ -96,7 +96,7 @@ main(int argc, char **argv)
> usage(EXIT_FAILURE);
> }
>
> - if (fd < 0 || runIO(path, fd, oflags) < 0)
> + if (fd < 0 || runIO(path, fd, oflags, STDIN_FILENO, STDOUT_FILENO) < 0)
> goto error;
>
> return 0;
> diff --git a/src/util/runio.c b/src/util/runio.c
> index a7b902af7e..f42acddae9 100644
> --- a/src/util/runio.c
> +++ b/src/util/runio.c
> @@ -134,7 +134,7 @@ runIOCopy(const struct runIOParams p)
>
>
> off_t
> -runIO(const char *path, int fd, int oflags)
> +runIO(const char *path, int fd, int oflags, int in_fd, int out_fd)
This is getting rather wierd as a signature.
If O_RDONLY, then in_fd is ignored, 'fd' is input.
If O_WRONLY, then out_fd is ignored, 'fd' is output
What about instead simply :
runIO(const char *srcpath, int srcfd,
const char *dstpath, int dstfd)
so there's no read vs write distinction at all.
With regards,
Daniel
--
|: https://berrange.com -o- https://www.flickr.com/photos/dberrange :|
|: https://libvirt.org -o- https://fstop138.berrange.com :|
|: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
On 4/28/22 2:54 PM, Daniel P. Berrangé wrote: > On Wed, Apr 27, 2022 at 11:13:23PM +0200, Claudio Fontana wrote: >> add arguments to runio to allow read/write from/to arbitrary >> file descriptors, as opposed to just stdin and stdout. >> >> Signed-off-by: Claudio Fontana <cfontana@suse.de> >> --- >> src/util/iohelper.c | 2 +- >> src/util/runio.c | 10 +++++----- >> src/util/runio.h | 17 ++++++++++++++++- >> 3 files changed, 22 insertions(+), 7 deletions(-) >> >> diff --git a/src/util/iohelper.c b/src/util/iohelper.c >> index 5a0098542e..93674c1e2f 100644 >> --- a/src/util/iohelper.c >> +++ b/src/util/iohelper.c >> @@ -96,7 +96,7 @@ main(int argc, char **argv) >> usage(EXIT_FAILURE); >> } >> >> - if (fd < 0 || runIO(path, fd, oflags) < 0) >> + if (fd < 0 || runIO(path, fd, oflags, STDIN_FILENO, STDOUT_FILENO) < 0) >> goto error; >> >> return 0; >> diff --git a/src/util/runio.c b/src/util/runio.c >> index a7b902af7e..f42acddae9 100644 >> --- a/src/util/runio.c >> +++ b/src/util/runio.c >> @@ -134,7 +134,7 @@ runIOCopy(const struct runIOParams p) >> >> >> off_t >> -runIO(const char *path, int fd, int oflags) >> +runIO(const char *path, int fd, int oflags, int in_fd, int out_fd) > > This is getting rather wierd as a signature. > > If O_RDONLY, then in_fd is ignored, 'fd' is input. > > If O_WRONLY, then out_fd is ignored, 'fd' is output > > What about instead simply : > > runIO(const char *srcpath, int srcfd, > const char *dstpath, int dstfd) > > so there's no read vs write distinction at all. maybe I am a bit confused/tired, but I don't see how this would work, which side one of those is the disk, where we want to check for S_ISBLK and O_DIRECT, and buffer accordingly? ... Alternative: off_t runIO(int disk_fd, const char *disk_path, int remote_fd, char *remote_path) ? And we could check oflags of disk_fd inside runIO instead of having it as an argument, to remove some clutter. Downside is for stdin and stdout we need then something special. For example: runIO(fd, path, -1, "stdio"); where "stdio" alerts runIO that it needs to use STDIN_FILENO if we are writing to the disk, or STDOUT_FILENO if we are reading from the disk. Wdyt? Thanks, Claudio
On Thu, Apr 28, 2022 at 06:24:11PM +0200, Claudio Fontana wrote: > On 4/28/22 2:54 PM, Daniel P. Berrangé wrote: > > On Wed, Apr 27, 2022 at 11:13:23PM +0200, Claudio Fontana wrote: > >> add arguments to runio to allow read/write from/to arbitrary > >> file descriptors, as opposed to just stdin and stdout. > >> > >> Signed-off-by: Claudio Fontana <cfontana@suse.de> > >> --- > >> src/util/iohelper.c | 2 +- > >> src/util/runio.c | 10 +++++----- > >> src/util/runio.h | 17 ++++++++++++++++- > >> 3 files changed, 22 insertions(+), 7 deletions(-) > >> > >> diff --git a/src/util/iohelper.c b/src/util/iohelper.c > >> index 5a0098542e..93674c1e2f 100644 > >> --- a/src/util/iohelper.c > >> +++ b/src/util/iohelper.c > >> @@ -96,7 +96,7 @@ main(int argc, char **argv) > >> usage(EXIT_FAILURE); > >> } > >> > >> - if (fd < 0 || runIO(path, fd, oflags) < 0) > >> + if (fd < 0 || runIO(path, fd, oflags, STDIN_FILENO, STDOUT_FILENO) < 0) > >> goto error; > >> > >> return 0; > >> diff --git a/src/util/runio.c b/src/util/runio.c > >> index a7b902af7e..f42acddae9 100644 > >> --- a/src/util/runio.c > >> +++ b/src/util/runio.c > >> @@ -134,7 +134,7 @@ runIOCopy(const struct runIOParams p) > >> > >> > >> off_t > >> -runIO(const char *path, int fd, int oflags) > >> +runIO(const char *path, int fd, int oflags, int in_fd, int out_fd) > > > > This is getting rather wierd as a signature. > > > > If O_RDONLY, then in_fd is ignored, 'fd' is input. > > > > If O_WRONLY, then out_fd is ignored, 'fd' is output > > > > What about instead simply : > > > > runIO(const char *srcpath, int srcfd, > > const char *dstpath, int dstfd) > > > > so there's no read vs write distinction at all. > > maybe I am a bit confused/tired, but I don't see how this would work, > which side one of those is the disk, where we want to check for S_ISBLK and O_DIRECT, and buffer accordingly? We call fstat on the FD, we'll know which FD is a pipe/socket vs which FD is a file/blockdev. If we call fcntl(GET_FL) we can also discover if O_DIRECT is turned on or not. With regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
On 4/28/22 6:26 PM, Daniel P. Berrangé wrote: > On Thu, Apr 28, 2022 at 06:24:11PM +0200, Claudio Fontana wrote: >> On 4/28/22 2:54 PM, Daniel P. Berrangé wrote: >>> On Wed, Apr 27, 2022 at 11:13:23PM +0200, Claudio Fontana wrote: >>>> add arguments to runio to allow read/write from/to arbitrary >>>> file descriptors, as opposed to just stdin and stdout. >>>> >>>> Signed-off-by: Claudio Fontana <cfontana@suse.de> >>>> --- >>>> src/util/iohelper.c | 2 +- >>>> src/util/runio.c | 10 +++++----- >>>> src/util/runio.h | 17 ++++++++++++++++- >>>> 3 files changed, 22 insertions(+), 7 deletions(-) >>>> >>>> diff --git a/src/util/iohelper.c b/src/util/iohelper.c >>>> index 5a0098542e..93674c1e2f 100644 >>>> --- a/src/util/iohelper.c >>>> +++ b/src/util/iohelper.c >>>> @@ -96,7 +96,7 @@ main(int argc, char **argv) >>>> usage(EXIT_FAILURE); >>>> } >>>> >>>> - if (fd < 0 || runIO(path, fd, oflags) < 0) >>>> + if (fd < 0 || runIO(path, fd, oflags, STDIN_FILENO, STDOUT_FILENO) < 0) >>>> goto error; >>>> >>>> return 0; >>>> diff --git a/src/util/runio.c b/src/util/runio.c >>>> index a7b902af7e..f42acddae9 100644 >>>> --- a/src/util/runio.c >>>> +++ b/src/util/runio.c >>>> @@ -134,7 +134,7 @@ runIOCopy(const struct runIOParams p) >>>> >>>> >>>> off_t >>>> -runIO(const char *path, int fd, int oflags) >>>> +runIO(const char *path, int fd, int oflags, int in_fd, int out_fd) >>> >>> This is getting rather wierd as a signature. >>> >>> If O_RDONLY, then in_fd is ignored, 'fd' is input. >>> >>> If O_WRONLY, then out_fd is ignored, 'fd' is output >>> >>> What about instead simply : >>> >>> runIO(const char *srcpath, int srcfd, >>> const char *dstpath, int dstfd) >>> >>> so there's no read vs write distinction at all. >> >> maybe I am a bit confused/tired, but I don't see how this would work, >> which side one of those is the disk, where we want to check for S_ISBLK and O_DIRECT, and buffer accordingly? > > We call fstat on the FD, we'll know which FD is a pipe/socket vs > which FD is a file/blockdev. If we call fcntl(GET_FL) we can also > discover if O_DIRECT is turned on or not. > Probably I am indeed tired :-) I cannot figure out how to call runIO() following your idea in iohelper.c. Can you show me? Because we'd need to first use fcntl(fd, F_GETFL) in iohelper to figure out if the disk is actually being read or written to, to then then pass the parameters to runIO in the right order in terms of source vs destination, and then inside runIO run fcntl(fd, F_GETFL) again to figure out which of the two sides is actually the disk, to check for S_ISBLK, O_DIRECT. It seems quite convoluted to me.. but maybe I am not seeing something simple (and in that case time for me to get some rest indeed). Ciao, Claudio
On Thu, Apr 28, 2022 at 01:54:28PM +0100, Daniel P. Berrangé wrote: > On Wed, Apr 27, 2022 at 11:13:23PM +0200, Claudio Fontana wrote: > > add arguments to runio to allow read/write from/to arbitrary > > file descriptors, as opposed to just stdin and stdout. > > > > Signed-off-by: Claudio Fontana <cfontana@suse.de> > > --- > > src/util/iohelper.c | 2 +- > > src/util/runio.c | 10 +++++----- > > src/util/runio.h | 17 ++++++++++++++++- > > 3 files changed, 22 insertions(+), 7 deletions(-) > > > > diff --git a/src/util/iohelper.c b/src/util/iohelper.c > > index 5a0098542e..93674c1e2f 100644 > > --- a/src/util/iohelper.c > > +++ b/src/util/iohelper.c > > @@ -96,7 +96,7 @@ main(int argc, char **argv) > > usage(EXIT_FAILURE); > > } > > > > - if (fd < 0 || runIO(path, fd, oflags) < 0) > > + if (fd < 0 || runIO(path, fd, oflags, STDIN_FILENO, STDOUT_FILENO) < 0) > > goto error; > > > > return 0; > > diff --git a/src/util/runio.c b/src/util/runio.c > > index a7b902af7e..f42acddae9 100644 > > --- a/src/util/runio.c > > +++ b/src/util/runio.c > > @@ -134,7 +134,7 @@ runIOCopy(const struct runIOParams p) > > > > > > off_t > > -runIO(const char *path, int fd, int oflags) > > +runIO(const char *path, int fd, int oflags, int in_fd, int out_fd) > > This is getting rather wierd as a signature. > > If O_RDONLY, then in_fd is ignored, 'fd' is input. > > If O_WRONLY, then out_fd is ignored, 'fd' is output > > What about instead simply : > > runIO(const char *srcpath, int srcfd, > const char *dstpath, int dstfd) > > so there's no read vs write distinction at all. I guess we need 'int srcflags and 'int dstflags' too, unless we just call fcntl() to query them instead. With regards, Daniel -- |: https://berrange.com -o- https://www.flickr.com/photos/dberrange :| |: https://libvirt.org -o- https://fstop138.berrange.com :| |: https://entangle-photo.org -o- https://www.instagram.com/dberrange :|
© 2016 - 2026 Red Hat, Inc.