]> git.ipfire.org Git - thirdparty/libvirt.git/commitdiff
util: Avoid statfs in virFileGetExistingParent
authorJiri Denemark <jdenemar@redhat.com>
Tue, 3 Jun 2025 08:33:03 +0000 (10:33 +0200)
committerJiri Denemark <jdenemar@redhat.com>
Tue, 3 Jun 2025 09:28:15 +0000 (11:28 +0200)
The code was separated from virFileIsSharedFSType which is Linux-only,
but virFileGetExistingParent is also called from
virFileIsSharedFSOverride which is OS independent. Thus we can't use
statfs. Let's use virFileExists (access) instead, we were not interested
in anything but success/failure from statfs anyway.

Signed-off-by: Jiri Denemark <jdenemar@redhat.com>
Reviewed-by: Martin Kletzander <mkletzan@redhat.com>
src/util/virfile.c
tests/virfilemock.c

index 06e8e08ddca2e27581612135ed03e62cfb2ffe2b..795f0218b4820d3b56723c76372feacf936ecd78 100644 (file)
@@ -3561,14 +3561,13 @@ static char *
 virFileGetExistingParent(const char *path)
 {
     g_autofree char *dirpath = g_strdup(path);
-    struct statfs sb;
     char *p = NULL;
 
-    /* Try less and less of the path until we get to a directory we can stat.
+    /* Try less and less of the path until we get to a directory we can access.
      * Even if we don't have 'x' permission on any directory in the path on the
      * NFS server (assuming it's NFS), we will be able to stat the mount point.
      */
-    while (statfs(dirpath, &sb) < 0 && p != dirpath) {
+    while (!virFileExists(dirpath) && p != dirpath) {
         if (!(p = strrchr(dirpath, '/'))) {
             virReportSystemError(EINVAL,
                                  _("Invalid relative path '%1$s'"), path);
index 4f1b8aecd7d4dd59b77d7a41a4b0d9996b0e97fb..5a09cce85532cfa0979cda46dcfa8c233554afdd 100644 (file)
@@ -21,6 +21,7 @@
 #include <stdio.h>
 #include <mntent.h>
 #include <sys/vfs.h>
+#include <unistd.h>
 #ifdef __linux__
 # include <linux/magic.h>
 #endif
@@ -32,6 +33,7 @@
 static FILE *(*real_setmntent)(const char *filename, const char *type);
 static int (*real_statfs)(const char *path, struct statfs *buf);
 static char *(*real_realpath)(const char *path, char *resolved);
+static int (*real_access)(const char *path, int mode);
 
 
 static void
@@ -43,6 +45,7 @@ init_syms(void)
     VIR_MOCK_REAL_INIT(setmntent);
     VIR_MOCK_REAL_INIT(statfs);
     VIR_MOCK_REAL_INIT(realpath);
+    VIR_MOCK_REAL_INIT(access);
 }
 
 
@@ -200,3 +203,28 @@ realpath(const char *path, char *resolved)
 
     return real_realpath(path, resolved);
 }
+
+
+int
+access(const char *path, int mode)
+{
+    const char *mtab = getenv("LIBVIRT_MTAB");
+
+    init_syms();
+
+    if (mtab && mode == F_OK) {
+        struct statfs buf;
+
+        /* The real statfs works on any existing file on a filesystem, while
+         * our mocked version only works on the mount point. Thus we have to
+         * pretend no files on the filesystem exist to make sure
+         * virFileGetExistingParent returns the mount point which can later be
+         * checked by statfs. Instead of checking we were called for a mount
+         * point by walking through the mtab, we just call our mocked statfs
+         * that does it for us.
+         */
+        return statfs_mock(mtab, path, &buf);
+    }
+
+    return real_access(path, mode);
+}