[PATCH 08/10] utils: Use overrides in virFileIsSharedFS()

Andrea Bolognani posted 10 patches 1 year, 10 months ago
There is a newer version of this series
[PATCH 08/10] utils: Use overrides in virFileIsSharedFS()
Posted by Andrea Bolognani 1 year, 10 months ago
If the filesystem wasn't determined to be a shared one via the
type check, try comparing it with the additional paths that
have been configured by the local admin.

Signed-off-by: Andrea Bolognani <abologna@redhat.com>
---
 src/util/virfile.c | 86 ++++++++++++++++++++++++++++++++++++++--------
 1 file changed, 72 insertions(+), 14 deletions(-)

diff --git a/src/util/virfile.c b/src/util/virfile.c
index a6a7de9829..ac9b5a77a6 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -3795,22 +3795,80 @@ virFileGetDefaultHugepage(virHugeTLBFS *fs,
     return NULL;
 }
 
+static int
+virFileIsSharedFSOverrideCompare(const char *path,
+                                 char *const *overrides)
+{
+    char *const *iter = overrides;
+
+    while (*iter != NULL) {
+        if (STREQ(path, *iter))
+            return 1;
+        iter++;
+    }
+
+    return 0;
+}
+
+static int
+virFileIsSharedFSOverride(const char *path,
+                          char *const *overrides)
+{
+    g_autofree char *dirpath = NULL;
+    char *p = NULL;
+    int ret = 0;
+
+    if (!path || path[0] != '/' || !overrides)
+        return ret;
+
+    dirpath = g_strdup(path);
+
+    ret = virFileIsSharedFSOverrideCompare(dirpath, overrides);
+
+    /* Continue until we've scanned the entire path or found a match */
+    while (p != dirpath && ret == 0) {
+
+        /* Find the last slash */
+        if ((p = strrchr(dirpath, '/')) == NULL)
+            break;
+
+        /* Truncate the path by overwriting the slash that we've just
+         * found with a null byte. If it is the very first slash in
+         * the path, we need to handle things slightly differently */
+        if (p == dirpath)
+            *(p+1) = '\0';
+        else
+            *p = '\0';
+
+        ret = virFileIsSharedFSOverrideCompare(dirpath, overrides);
+    }
+
+    return ret;
+}
+
 int virFileIsSharedFS(const char *path,
-                      char *const *overrides G_GNUC_UNUSED)
+                      char *const *overrides)
 {
-    return virFileIsSharedFSType(path,
-                                 VIR_FILE_SHFS_NFS |
-                                 VIR_FILE_SHFS_GFS2 |
-                                 VIR_FILE_SHFS_OCFS |
-                                 VIR_FILE_SHFS_AFS |
-                                 VIR_FILE_SHFS_SMB |
-                                 VIR_FILE_SHFS_CIFS |
-                                 VIR_FILE_SHFS_CEPH |
-                                 VIR_FILE_SHFS_GPFS|
-                                 VIR_FILE_SHFS_QB |
-                                 VIR_FILE_SHFS_ACFS |
-                                 VIR_FILE_SHFS_GLUSTERFS |
-                                 VIR_FILE_SHFS_BEEGFS);
+    int ret;
+
+    ret = virFileIsSharedFSType(path,
+                                VIR_FILE_SHFS_NFS |
+                                VIR_FILE_SHFS_GFS2 |
+                                VIR_FILE_SHFS_OCFS |
+                                VIR_FILE_SHFS_AFS |
+                                VIR_FILE_SHFS_SMB |
+                                VIR_FILE_SHFS_CIFS |
+                                VIR_FILE_SHFS_CEPH |
+                                VIR_FILE_SHFS_GPFS|
+                                VIR_FILE_SHFS_QB |
+                                VIR_FILE_SHFS_ACFS |
+                                VIR_FILE_SHFS_GLUSTERFS |
+                                VIR_FILE_SHFS_BEEGFS);
+
+    if (ret == 0)
+        ret = virFileIsSharedFSOverride(path, overrides);
+
+    return ret;
 }
 
 
-- 
2.44.0
_______________________________________________
Devel mailing list -- devel@lists.libvirt.org
To unsubscribe send an email to devel-leave@lists.libvirt.org
Re: [PATCH 08/10] utils: Use overrides in virFileIsSharedFS()
Posted by Peter Krempa 1 year, 10 months ago
On Wed, Mar 20, 2024 at 10:19:13 +0100, Andrea Bolognani wrote:
> If the filesystem wasn't determined to be a shared one via the
> type check, try comparing it with the additional paths that
> have been configured by the local admin.
> 
> Signed-off-by: Andrea Bolognani <abologna@redhat.com>
> ---
>  src/util/virfile.c | 86 ++++++++++++++++++++++++++++++++++++++--------
>  1 file changed, 72 insertions(+), 14 deletions(-)
> 
> diff --git a/src/util/virfile.c b/src/util/virfile.c
> index a6a7de9829..ac9b5a77a6 100644
> --- a/src/util/virfile.c
> +++ b/src/util/virfile.c
> @@ -3795,22 +3795,80 @@ virFileGetDefaultHugepage(virHugeTLBFS *fs,
>      return NULL;
>  }
>  
> +static int
> +virFileIsSharedFSOverrideCompare(const char *path,
> +                                 char *const *overrides)
> +{
> +    char *const *iter = overrides;
> +
> +    while (*iter != NULL) {
> +        if (STREQ(path, *iter))
> +            return 1;
> +        iter++;

This is g_strv_contains in disguise.


> +    }
> +
> +    return 0;
> +}
> +
> +static int
> +virFileIsSharedFSOverride(const char *path,
> +                          char *const *overrides)
> +{
> +    g_autofree char *dirpath = NULL;
> +    char *p = NULL;
> +    int ret = 0;
> +
> +    if (!path || path[0] != '/' || !overrides)
> +        return ret;

Return 0 directly.

> +
> +    dirpath = g_strdup(path);
> +
> +    ret = virFileIsSharedFSOverrideCompare(dirpath, overrides);

Same here, return success directly rather than having the reader look
what's happening.

> +
> +    /* Continue until we've scanned the entire path or found a match */
> +    while (p != dirpath && ret == 0) {
> +
> +        /* Find the last slash */
> +        if ((p = strrchr(dirpath, '/')) == NULL)
> +            break;
> +
> +        /* Truncate the path by overwriting the slash that we've just
> +         * found with a null byte. If it is the very first slash in
> +         * the path, we need to handle things slightly differently */
> +        if (p == dirpath)
> +            *(p+1) = '\0';
> +        else
> +            *p = '\0';
> +
> +        ret = virFileIsSharedFSOverrideCompare(dirpath, overrides);

Here too.

> +    }
> +
> +    return ret;

And return failure/negative answer here also directly. Also consider
returning boolean as it's returning just 1 and 0, which would make the
first question I had (What do the return values mean?!?!) auto-answered.

> +}
> +
_______________________________________________
Devel mailing list -- devel@lists.libvirt.org
To unsubscribe send an email to devel-leave@lists.libvirt.org