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

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



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@xxxxxxxxxxxxxxxxxx>
---
 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@xxxxxxxxxx
https://www.redhat.com/mailman/listinfo/libvir-list



[Index of Archives]     [Virt Tools]     [Libvirt Users]     [Lib OS Info]     [Fedora Users]     [Fedora Desktop]     [Fedora SELinux]     [Big List of Linux Books]     [Yosemite News]     [KDE Users]     [Fedora Tools]

  Powered by Linux