[libvirt] [PATCH 1/2] Use '-' to read from stdin

Modify virFileReadAll to open stdin when '-' is given as the filename. Signed-off-by: Michael Williams <mspacex@gmail.com> --- src/util/util.c | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-) diff --git a/src/util/util.c b/src/util/util.c index df4dfac..554d68e 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -443,7 +443,13 @@ virFileReadLimFD(int fd, int maxlen, char **buf) int virFileReadAll(const char *path, int maxlen, char **buf) { - int fd = open(path, O_RDONLY); + int fd; + + if (strcmp(path,"-") == 0) + fd = fileno(stdin); + else + fd = open(path, O_RDONLY); + if (fd < 0) { virReportSystemError(errno, _("Failed to open file '%s'"), path); return -1; -- 1.7.3.4

On 06/13/2011 09:39 PM, Michael Williams wrote:
Modify virFileReadAll to open stdin when '-' is given as the filename.
Signed-off-by: Michael Williams <mspacex@gmail.com> --- src/util/util.c | 8 +++++++- 1 files changed, 7 insertions(+), 1 deletions(-)
diff --git a/src/util/util.c b/src/util/util.c index df4dfac..554d68e 100644 --- a/src/util/util.c +++ b/src/util/util.c @@ -443,7 +443,13 @@ virFileReadLimFD(int fd, int maxlen, char **buf) int virFileReadAll(const char *path, int maxlen, char **buf) { - int fd = open(path, O_RDONLY); + int fd; + + if (strcmp(path,"-") == 0)
s/,"/, "/ - space after comma.
+ fd = fileno(stdin);
Micro-optimization: POSIX guarantees that the constant STDIN_FILENO works here, which is slightly more efficient than fileno(stdin). Problem: this closes fd 0 after virFileReadLimFD, even though the user probably wants to keep on using stdin (especially if stdin is a tty, and EOF was triggered by ^D although the terminal still accepts input). You have to avoid closing the fd if "-" was present. Question: is this sufficient? That is, do all virsh.c callers that read file contents eventually call into this function, or do you need to also hook up some other locations to honor "-"? -- Eric Blake eblake@redhat.com +1-801-349-2682 Libvirt virtualization library http://libvirt.org

Question: is this sufficient? That is, do all virsh.c callers that read file contents eventually call into this function, or do you need to also hook up some other locations to honor "-"? The virsh commands that I was looking at (create, define, network-create, etc) called virFileReadAll when reading in the entire file contents, and the other abstracted read functions use fd's rather
Changes made, will submit an updated patchset after finalizing the next patch. On 6/13/11 10:50 PM, Eric Blake wrote: than a path. By the time it expects an fd, it seemed that it was too late to be checking for stdin. -- ~Michael Williams
participants (2)
-
Eric Blake
-
Michael Williams