[PATCH 4/6] virfile: Handle directories in virFileBindMountDevice()

Michal Privoznik posted 6 patches 9 weeks ago

[PATCH 4/6] virfile: Handle directories in virFileBindMountDevice()

Posted by Michal Privoznik 9 weeks ago
The @src is not always a file. It may also be a directory (for
instance qemuDomainCreateDeviceRecursive() assumes that) - even
though it doesn't happen usually. Anyway, mount() can mount only
a dir onto a dir and a file onto a file.

Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
---
 src/util/virfile.c | 13 +++++++++++--
 1 file changed, 11 insertions(+), 2 deletions(-)

diff --git a/src/util/virfile.c b/src/util/virfile.c
index 0f31554323..9c175b041f 100644
--- a/src/util/virfile.c
+++ b/src/util/virfile.c
@@ -3743,8 +3743,17 @@ int
 virFileBindMountDevice(const char *src,
                        const char *dst)
 {
-    if (virFileTouch(dst, 0666) < 0)
-        return -1;
+    if (!virFileExists(dst)) {
+        if (virFileIsDir(src)) {
+            if (virFileMakePath(dst)) {
+                virReportSystemError(errno, _("Unable to make dir %s"), dst);
+                return -1;
+            }
+        } else {
+            if (virFileTouch(dst, 0666) < 0)
+                return -1;
+        }
+    }
 
     if (mount(src, dst, "none", MS_BIND, NULL) < 0) {
         virReportSystemError(errno, _("Failed to bind %s on to %s"), src,
-- 
2.24.1

Re: [PATCH 4/6] virfile: Handle directories in virFileBindMountDevice()

Posted by Pavel Mores 9 weeks ago
On Wed, Mar 18, 2020 at 06:32:14PM +0100, Michal Privoznik wrote:
> The @src is not always a file. It may also be a directory (for
> instance qemuDomainCreateDeviceRecursive() assumes that) - even
> though it doesn't happen usually. Anyway, mount() can mount only
> a dir onto a dir and a file onto a file.
> 
> Signed-off-by: Michal Privoznik <mprivozn@redhat.com>
> ---
>  src/util/virfile.c | 13 +++++++++++--
>  1 file changed, 11 insertions(+), 2 deletions(-)
> 
> diff --git a/src/util/virfile.c b/src/util/virfile.c
> index 0f31554323..9c175b041f 100644
> --- a/src/util/virfile.c
> +++ b/src/util/virfile.c
> @@ -3743,8 +3743,17 @@ int
>  virFileBindMountDevice(const char *src,
>                         const char *dst)
>  {
> -    if (virFileTouch(dst, 0666) < 0)
> -        return -1;
> +    if (!virFileExists(dst)) {
> +        if (virFileIsDir(src)) {
> +            if (virFileMakePath(dst)) {

Personally, I'd consider

if (virFileMakePath(dst) < 0)

clearer here for a reader who's not familiar with the function and what exactly
it returns.

Reviewed-by: Pavel Mores <pmores@redhat.com>

> +                virReportSystemError(errno, _("Unable to make dir %s"), dst);
> +                return -1;
> +            }
> +        } else {
> +            if (virFileTouch(dst, 0666) < 0)
> +                return -1;
> +        }
> +    }
>  
>      if (mount(src, dst, "none", MS_BIND, NULL) < 0) {
>          virReportSystemError(errno, _("Failed to bind %s on to %s"), src,
> -- 
> 2.24.1
>