diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms index 7cc7bf8858..ee59caeb7e 100644 --- a/src/libvirt_private.syms +++ b/src/libvirt_private.syms @@ -1578,6 +1578,7 @@ virFileIsLink; virFileIsMountPoint; virFileIsSharedFS; virFileIsSharedFSType; +virFileLength; virFileLinkPointsTo; virFileLock; virFileLoopDeviceAssociate; diff --git a/src/util/virfile.c b/src/util/virfile.c index 1fb89ce1ca..fcd0d92889 100644 --- a/src/util/virfile.c +++ b/src/util/virfile.c @@ -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) { diff --git a/src/util/virfile.h b/src/util/virfile.h index b4ae6ea5f1..836cc602c8 100644 --- a/src/util/virfile.h +++ b/src/util/virfile.h @@ -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);