[pbs-devel] [PATCH proxmox-backup v2 04/15] api2/config/{drive, changer}: prevent adding same device multiple times

Dominik Csapak d.csapak at proxmox.com
Thu Jan 28 12:59:44 CET 2021


this check is not perfect since there are often multiple device
nodes per drive/changer, but from the scan api we should return always
the same, so for an api user this should be enough

Signed-off-by: Dominik Csapak <d.csapak at proxmox.com>
---
 src/api2/config/changer.rs | 12 ++++++++++--
 src/api2/config/drive.rs   | 11 +++++++++--
 2 files changed, 19 insertions(+), 4 deletions(-)

diff --git a/src/api2/config/changer.rs b/src/api2/config/changer.rs
index 1b429659..8404d20b 100644
--- a/src/api2/config/changer.rs
+++ b/src/api2/config/changer.rs
@@ -59,8 +59,16 @@ pub fn create_changer(
 
     check_drive_path(&linux_changers, &path)?;
 
-    if config.sections.get(&name).is_some() {
-        bail!("Entry '{}' already exists", name);
+    let existing: Vec<ScsiTapeChanger> = config.convert_to_typed_array("changer")?;
+
+    for changer in existing {
+        if changer.name == name {
+            bail!("Entry '{}' already exists", name);
+        }
+
+        if changer.path == path {
+            bail!("Path '{}' already in use by '{}'", path, changer.name);
+        }
     }
 
     let item = ScsiTapeChanger {
diff --git a/src/api2/config/drive.rs b/src/api2/config/drive.rs
index 78ebecb3..7d7803d3 100644
--- a/src/api2/config/drive.rs
+++ b/src/api2/config/drive.rs
@@ -56,8 +56,15 @@ pub fn create_drive(param: Value) -> Result<(), Error> {
 
     check_drive_path(&linux_drives, &item.path)?;
 
-    if config.sections.get(&item.name).is_some() {
-        bail!("Entry '{}' already exists", item.name);
+    let existing: Vec<LinuxTapeDrive> = config.convert_to_typed_array("linux")?;
+
+    for drive in existing {
+        if drive.name == item.name {
+            bail!("Entry '{}' already exists", item.name);
+        }
+        if drive.path == item.path {
+            bail!("Path '{}' already used in drive '{}'", item.path, drive.name);
+        }
     }
 
     config.set_data(&item.name, "linux", &item)?;
-- 
2.20.1






More information about the pbs-devel mailing list