public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox-backup] api: enhance directory existence check
@ 2023-11-29 14:08 Markus Frank
  2023-11-29 14:51 ` Lukas Wagner
  0 siblings, 1 reply; 3+ messages in thread
From: Markus Frank @ 2023-11-29 14:08 UTC (permalink / raw)
  To: pbs-devel

Instead of just checking the existence of the path, the code now also checks
whether the directory is empty, continues if it is, and aborts if it is not.

Previously, if a directory were deleted and a directory with the same name
would be created, the old check prevented the creation even though the
directory could be used as a mount point.

Signed-off-by: Markus Frank <m.frank@proxmox.com>
---
 src/api2/node/disks/directory.rs | 8 ++++++--
 1 file changed, 6 insertions(+), 2 deletions(-)

diff --git a/src/api2/node/disks/directory.rs b/src/api2/node/disks/directory.rs
index 5e1cb124..cd7b44a3 100644
--- a/src/api2/node/disks/directory.rs
+++ b/src/api2/node/disks/directory.rs
@@ -155,13 +155,17 @@ pub fn create_datastore_disk(
 
     let mount_point = format!("{}{}", BASE_MOUNT_DIR, &name);
 
-    // check if the default path does exist already and bail if it does
+    // check if the default path does exist already
+    // and bail if it does and is not empty
     let default_path = std::path::PathBuf::from(&mount_point);
 
     match std::fs::metadata(&default_path) {
         Err(_) => {} // path does not exist
         Ok(_) => {
-            bail!("path {:?} already exists", default_path);
+            let is_empty = default_path.read_dir()?.next().is_none();
+            if !is_empty {
+                bail!("path {:?} already exists and is not empty", default_path);
+            }
         }
     }
 
-- 
2.39.2





^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [pbs-devel] [PATCH proxmox-backup] api: enhance directory existence check
  2023-11-29 14:08 [pbs-devel] [PATCH proxmox-backup] api: enhance directory existence check Markus Frank
@ 2023-11-29 14:51 ` Lukas Wagner
  2023-11-29 15:00   ` Christian Ebner
  0 siblings, 1 reply; 3+ messages in thread
From: Lukas Wagner @ 2023-11-29 14:51 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Markus Frank



On 11/29/23 15:08, Markus Frank wrote:
> Instead of just checking the existence of the path, the code now also checks
> whether the directory is empty, continues if it is, and aborts if it is not.
> 
> Previously, if a directory were deleted and a directory with the same name
> would be created, the old check prevented the creation even though the
> directory could be used as a mount point.


But that does not detect if another (empty) filesystem is already 
mounted at that directory, right?

> 
> Signed-off-by: Markus Frank <m.frank@proxmox.com>
> ---
>   src/api2/node/disks/directory.rs | 8 ++++++--
>   1 file changed, 6 insertions(+), 2 deletions(-)
> 
> diff --git a/src/api2/node/disks/directory.rs b/src/api2/node/disks/directory.rs
> index 5e1cb124..cd7b44a3 100644
> --- a/src/api2/node/disks/directory.rs
> +++ b/src/api2/node/disks/directory.rs
> @@ -155,13 +155,17 @@ pub fn create_datastore_disk(
>   
>       let mount_point = format!("{}{}", BASE_MOUNT_DIR, &name);
>   
> -    // check if the default path does exist already and bail if it does
> +    // check if the default path does exist already
> +    // and bail if it does and is not empty
>       let default_path = std::path::PathBuf::from(&mount_point);
>   
>       match std::fs::metadata(&default_path) {
>           Err(_) => {} // path does not exist
>           Ok(_) => {
> -            bail!("path {:?} already exists", default_path);
> +            let is_empty = default_path.read_dir()?.next().is_none();
> +            if !is_empty {
> +                bail!("path {:?} already exists and is not empty", default_path);
You can inline 'default_path' here: {default_path:?}

> +            }
>           }
>       }
>   

Also, I guess you could change the 'match' to a
'if std::fs::metadata(...).is_ok()', since we ignore the enclosed values 
anyway.

-- 
- Lukas




^ permalink raw reply	[flat|nested] 3+ messages in thread

* Re: [pbs-devel] [PATCH proxmox-backup] api: enhance directory existence check
  2023-11-29 14:51 ` Lukas Wagner
@ 2023-11-29 15:00   ` Christian Ebner
  0 siblings, 0 replies; 3+ messages in thread
From: Christian Ebner @ 2023-11-29 15:00 UTC (permalink / raw)
  To: Proxmox Backup Server development discussion, Lukas Wagner, Markus Frank

> On 29.11.2023 15:51 CET Lukas Wagner <l.wagner@proxmox.com> wrote:
> 
> 
> 
> But that does not detect if another (empty) filesystem is already 
> mounted at that directory, right?

To add to Lukas response, you could compare the `st_dev` of parent and mountpoint
to check if there already if a different filesystem mounted, something like:

use std::os::linux::fs::MetadataExt;

...

match std::fs::metadata(&default_path) {
     Err(_) => {} // path does not exist
     Ok(stat) => {
         let basedir_dev = std::fs::metadata(BASE_MOUNT_DIR)?.st_dev();
         if stat.st_dev != basedir_dev {
             bail!("path {default_path:?} already exists and is mountpoint");
         }
         ...
    }
}




^ permalink raw reply	[flat|nested] 3+ messages in thread

end of thread, other threads:[~2023-11-29 15:00 UTC | newest]

Thread overview: 3+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2023-11-29 14:08 [pbs-devel] [PATCH proxmox-backup] api: enhance directory existence check Markus Frank
2023-11-29 14:51 ` Lukas Wagner
2023-11-29 15:00   ` Christian Ebner

This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox
Service provided by Proxmox Server Solutions GmbH | Privacy | Legal