[libvirt] [PATCH v3 01/14] util: implement virFileReadOffsetQuiet()

Stefan Berger posted 14 patches 7 years ago
There is a newer version of this series
[libvirt] [PATCH v3 01/14] util: implement virFileReadOffsetQuiet()
Posted by Stefan Berger 7 years ago
Implement virFileReadOffsetQuiet() that reads a given maximum number
of bytes into a buffer that will be allocated. The reading starts
from a given offset.

Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
---
 src/libvirt_private.syms |  1 +
 src/util/virfile.c       | 14 +++++++++++++-
 src/util/virfile.h       |  3 +++
 3 files changed, 17 insertions(+), 1 deletion(-)

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 92b5e0f..f2a4921 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1807,6 +1807,7 @@ virFileReadHeaderFD;
 virFileReadHeaderQuiet;
 virFileReadLimFD;
 virFileReadLink;
+virFileReadOffsetQuiet;
 virFileReadValueBitmap;
 virFileReadValueInt;
 virFileReadValueScaledInt;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index 40f106d..526b9ad 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -1432,12 +1432,18 @@ virFileReadAll(const char *path, int maxlen, char **buf)
 }
 
 int
-virFileReadAllQuiet(const char *path, int maxlen, char **buf)
+virFileReadOffsetQuiet(const char *path, off_t offset,
+                       int maxlen, char **buf)
 {
     int fd = open(path, O_RDONLY);
     if (fd < 0)
         return -errno;
 
+    if (offset > 0 && lseek(fd, offset, SEEK_SET) < 0) {
+        VIR_FORCE_CLOSE(fd);
+        return -errno;
+    }
+
     int len = virFileReadLimFD(fd, maxlen, buf);
     VIR_FORCE_CLOSE(fd);
     if (len < 0)
@@ -1446,6 +1452,12 @@ virFileReadAllQuiet(const char *path, int maxlen, char **buf)
     return len;
 }
 
+int
+virFileReadAllQuiet(const char *path, int maxlen, char **buf)
+{
+    return virFileReadOffsetQuiet(path, 0, maxlen, buf);
+}
+
 /* Read @file into preallocated buffer @buf of size @len.
  * Return value is -errno in case of errors and size
  * of data read (no trailing zero) in case of success.
diff --git a/src/util/virfile.h b/src/util/virfile.h
index 341320b..13d3cf6 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -137,6 +137,9 @@ int virFileReadLimFD(int fd, int maxlen, char **buf)
     ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(3);
 int virFileReadAll(const char *path, int maxlen, char **buf)
     ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
+int virFileReadOffsetQuiet(const char *path, off_t offset,
+                           int maxlen, char **buf)
+    ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
 int virFileReadAllQuiet(const char *path, int maxlen, char **buf)
     ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
 int virFileReadBufQuiet(const char *file, char *buf, int len)
-- 
2.5.5

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list
Re: [libvirt] [PATCH v3 01/14] util: implement virFileReadOffsetQuiet()
Posted by John Ferlan 7 years ago
$SUBJ: "Implement"

On 05/04/2018 04:21 PM, Stefan Berger wrote:
> Implement virFileReadOffsetQuiet() that reads a given maximum number
> of bytes into a buffer that will be allocated. The reading starts
> from a given offset.
> 
> Signed-off-by: Stefan Berger <stefanb@linux.vnet.ibm.com>
> ---
>  src/libvirt_private.syms |  1 +
>  src/util/virfile.c       | 14 +++++++++++++-
>  src/util/virfile.h       |  3 +++
>  3 files changed, 17 insertions(+), 1 deletion(-)
> 
> diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
> index 92b5e0f..f2a4921 100644
> --- a/src/libvirt_private.syms
> +++ b/src/libvirt_private.syms
> @@ -1807,6 +1807,7 @@ virFileReadHeaderFD;
>  virFileReadHeaderQuiet;
>  virFileReadLimFD;
>  virFileReadLink;
> +virFileReadOffsetQuiet;
>  virFileReadValueBitmap;
>  virFileReadValueInt;
>  virFileReadValueScaledInt;
> diff --git a/src/util/virfile.c b/src/util/virfile.c
> index 40f106d..526b9ad 100644
> --- a/src/util/virfile.c
> +++ b/src/util/virfile.c
> @@ -1432,12 +1432,18 @@ virFileReadAll(const char *path, int maxlen, char **buf)
>  }
>  

Two blank lines between functions is the new normal...

>  int
> -virFileReadAllQuiet(const char *path, int maxlen, char **buf)
> +virFileReadOffsetQuiet(const char *path, off_t offset,
> +                       int maxlen, char **buf)

...and each argument on it's own line...

>  {
>      int fd = open(path, O_RDONLY);
>      if (fd < 0)
>          return -errno;
>  
> +    if (offset > 0 && lseek(fd, offset, SEEK_SET) < 0) {
> +        VIR_FORCE_CLOSE(fd);
> +        return -errno;
> +    }
> +
>      int len = virFileReadLimFD(fd, maxlen, buf);
>      VIR_FORCE_CLOSE(fd);
>      if (len < 0)
> @@ -1446,6 +1452,12 @@ virFileReadAllQuiet(const char *path, int maxlen, char **buf)
>      return len;
>  }
>  
> +int
> +virFileReadAllQuiet(const char *path, int maxlen, char **buf)

... here too (as well as the 2 empty lines on either side since we're
touching the code).

With the adjustments,

Reviewed-by: John Ferlan <jferlan@redhat.com>

but I do have some other concerns later in patch 7 when this is used as
to whether it's necessary.

John

> +{
> +    return virFileReadOffsetQuiet(path, 0, maxlen, buf);
> +}
> +
>  /* Read @file into preallocated buffer @buf of size @len.
>   * Return value is -errno in case of errors and size
>   * of data read (no trailing zero) in case of success.
> diff --git a/src/util/virfile.h b/src/util/virfile.h
> index 341320b..13d3cf6 100644
> --- a/src/util/virfile.h
> +++ b/src/util/virfile.h
> @@ -137,6 +137,9 @@ int virFileReadLimFD(int fd, int maxlen, char **buf)
>      ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(3);
>  int virFileReadAll(const char *path, int maxlen, char **buf)
>      ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
> +int virFileReadOffsetQuiet(const char *path, off_t offset,
> +                           int maxlen, char **buf)
> +    ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(4);
>  int virFileReadAllQuiet(const char *path, int maxlen, char **buf)
>      ATTRIBUTE_RETURN_CHECK ATTRIBUTE_NONNULL(1) ATTRIBUTE_NONNULL(3);
>  int virFileReadBufQuiet(const char *file, char *buf, int len)
> 

--
libvir-list mailing list
libvir-list@redhat.com
https://www.redhat.com/mailman/listinfo/libvir-list