From: Markus Frank <m.frank@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup v2] api: enhance directory existence check
Date: Thu, 30 Nov 2023 10:33:46 +0100 [thread overview]
Message-ID: <20231130093346.38897-1-m.frank@proxmox.com> (raw)
If a directory exists on the specified path,
it now also checks whether the directory is empty and not already mounted.
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>
---
v2: added check if another file system is mounted on the specified path
src/api2/node/disks/directory.rs | 13 +++++++++++--
1 file changed, 11 insertions(+), 2 deletions(-)
diff --git a/src/api2/node/disks/directory.rs b/src/api2/node/disks/directory.rs
index 5e1cb124..48e0bd57 100644
--- a/src/api2/node/disks/directory.rs
+++ b/src/api2/node/disks/directory.rs
@@ -1,6 +1,7 @@
use ::serde::{Deserialize, Serialize};
use anyhow::{bail, Error};
use serde_json::json;
+use std::os::linux::fs::MetadataExt;
use proxmox_router::{Permission, Router, RpcEnvironment, RpcEnvironmentType};
use proxmox_schema::api;
@@ -156,12 +157,20 @@ 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
+ // and is not empty or another file system is already mounted on it
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);
+ 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");
+ }
+ let is_empty = default_path.read_dir()?.next().is_none();
+ if !is_empty {
+ bail!("path {default_path:?} already exists and is not empty");
+ }
}
}
--
2.39.2
next reply other threads:[~2023-11-30 9:34 UTC|newest]
Thread overview: 2+ messages / expand[flat|nested] mbox.gz Atom feed top
2023-11-30 9:33 Markus Frank [this message]
2023-11-30 10:26 ` Christian Ebner
Reply instructions:
You may reply publicly to this message via plain-text email
using any one of the following methods:
* Save the following mbox file, import it into your mail client,
and reply-to-all from there: mbox
Avoid top-posting and favor interleaved quoting:
https://en.wikipedia.org/wiki/Posting_style#Interleaved_style
* Reply using the --to, --cc, and --in-reply-to
switches of git-send-email(1):
git send-email \
--in-reply-to=20231130093346.38897-1-m.frank@proxmox.com \
--to=m.frank@proxmox.com \
--cc=pbs-devel@lists.proxmox.com \
/path/to/YOUR_REPLY
https://kernel.org/pub/software/scm/git/docs/git-send-email.html
* If your mail client supports setting the In-Reply-To header
via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line
before the message body.
This is an external index of several public inboxes,
see mirroring instructions on how to clone and mirror
all data and code used by this external index.