[libvirt] [PATCH] util: Implement and use virFileIsRegular() rather than d_type

Stefan Berger stefanb at linux.vnet.ibm.com
Wed Jun 6 16:37:06 UTC 2018


The dirent's d_type field is not portable to all platforms. So we have
to use stat() to determine the type of file for the functions that need
to be cross-platform. Fix virFileChownFiles() by calling the new
virFileIsRegular() function.

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

diff --git a/src/libvirt_private.syms b/src/libvirt_private.syms
index 43bbdef8f1..b4ab1f3629 100644
--- a/src/libvirt_private.syms
+++ b/src/libvirt_private.syms
@@ -1796,6 +1796,7 @@ virFileIsDir;
 virFileIsExecutable;
 virFileIsLink;
 virFileIsMountPoint;
+virFileIsRegular;
 virFileIsSharedFS;
 virFileIsSharedFSType;
 virFileLength;
diff --git a/src/util/virfile.c b/src/util/virfile.c
index e41881f6c9..12b41a64e0 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -1851,6 +1851,15 @@ virFileIsDir(const char *path)
     return (stat(path, &s) == 0) && S_ISDIR(s.st_mode);
 }
 
+
+bool
+virFileIsRegular(const char *path)
+{
+    struct stat s;
+    return (stat(path, &s) == 0) && S_ISREG(s.st_mode);
+}
+
+
 /**
  * virFileExists: Check for presence of file
  * @path: Path of file to check
@@ -3005,12 +3014,13 @@ int virFileChownFiles(const char *name,
         return -1;
 
     while ((direrr = virDirRead(dir, &ent, name)) > 0) {
-        if (ent->d_type != DT_REG)
-            continue;
-
+        VIR_FREE(path);
         if (virAsprintf(&path, "%s/%s", name, ent->d_name) < 0)
             goto cleanup;
 
+        if (!virFileIsRegular(path))
+            continue;
+
         if (chown(path, uid, gid) < 0) {
             virReportSystemError(errno,
                                  _("cannot chown '%s' to (%u, %u)"),
@@ -3018,7 +3028,6 @@ int virFileChownFiles(const char *name,
                                  (unsigned int) gid);
             goto cleanup;
         }
-        VIR_FREE(path);
     }
 
     if (direrr < 0)
diff --git a/src/util/virfile.h b/src/util/virfile.h
index c7a32c30a8..59c14b97a6 100644
--- a/src/util/virfile.h
+++ b/src/util/virfile.h
@@ -194,6 +194,7 @@ 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) ATTRIBUTE_NOINLINE;
 bool virFileIsExecutable(const char *file) ATTRIBUTE_NONNULL(1);
+bool virFileIsRegular(const char *file) ATTRIBUTE_NONNULL(1);
 
 enum {
     VIR_FILE_SHFS_NFS = (1 << 0),
-- 
2.14.4




More information about the libvir-list mailing list