]> xenbits.xensource.com Git - libvirt.git/commitdiff
util: new function virFileLength()
authorLaine Stump <laine@laine.org>
Thu, 17 Nov 2016 17:18:27 +0000 (12:18 -0500)
committerLaine Stump <laine@laine.org>
Wed, 30 Nov 2016 20:18:57 +0000 (15:18 -0500)
This new function just calls fstat() (if provided with a valid fd) or
stat() (if fd is -1) and returns st_size (or -1 if there is an
error). We may decide we want this function to be more complex, and
handle things like block devices - this is a placeholder (that works)
for any more complicated function.

src/libvirt_private.syms
src/util/virfile.c
src/util/virfile.h

index 7cc7bf88587d4c25fcaef9513ed0b956c75c46aa..ee59caeb7ef131d74a69fa68f7db60ad88df3cfd 100644 (file)
@@ -1578,6 +1578,7 @@ virFileIsLink;
 virFileIsMountPoint;
 virFileIsSharedFS;
 virFileIsSharedFSType;
+virFileLength;
 virFileLinkPointsTo;
 virFileLock;
 virFileLoopDeviceAssociate;
index 1fb89ce1ca85d950dc24f12013bc95fe854002f9..fcd0d92889c7b24ec9363057efb0a1253ce60c28 100644 (file)
@@ -1735,6 +1735,40 @@ virFileActivateDirOverride(const char *argv0)
     }
 }
 
+
+/**
+ * virFileLength:
+ * @path: full path of the file
+ * @fd: open file descriptor for file (or -1 to use @path)
+ *
+ * If fd >= 0, return the length of the open file indicated by @fd.
+ * If fd < 0 (i.e. -1) return the length of the file indicated by
+ * @path.
+ *
+ * Returns the length, or -1 if the file doesn't
+ * exist or its info was inaccessible. No error is logged.
+ */
+off_t
+virFileLength(const char *path, int fd)
+{
+    struct stat s;
+
+    if (fd >= 0) {
+        if (fstat(fd, &s) < 0)
+            return -1;
+    } else {
+        if (stat(path, &s) < 0)
+            return -1;
+    }
+
+    if (!S_ISREG(s.st_mode))
+       return -1;
+
+    return s.st_size;
+
+}
+
+
 bool
 virFileIsDir(const char *path)
 {
index b4ae6ea5f1922bfc4d269f74c193b34394a62d4c..836cc602c8329ae78ce1ac5f5cfe0bfb4c88edd9 100644 (file)
@@ -179,6 +179,7 @@ char *virFileFindResourceFull(const char *filename,
 void virFileActivateDirOverride(const char *argv0)
     ATTRIBUTE_NONNULL(1);
 
+off_t virFileLength(const char *path, int fd) ATTRIBUTE_NONNULL(1);
 bool virFileIsDir (const char *file) ATTRIBUTE_NONNULL(1);
 bool virFileExists(const char *file) ATTRIBUTE_NONNULL(1);
 bool virFileIsExecutable(const char *file) ATTRIBUTE_NONNULL(1);