From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <d.csapak@proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by lists.proxmox.com (Postfix) with ESMTPS id 7D84E6BF19
 for <pbs-devel@lists.proxmox.com>; Thu, 28 Jan 2021 13:00:36 +0100 (CET)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
 by firstgate.proxmox.com (Proxmox) with ESMTP id BC2952D325
 for <pbs-devel@lists.proxmox.com>; Thu, 28 Jan 2021 13:00:04 +0100 (CET)
Received: from proxmox-new.maurer-it.com (proxmox-new.maurer-it.com
 [212.186.127.180])
 (using TLSv1.3 with cipher TLS_AES_256_GCM_SHA384 (256/256 bits)
 key-exchange X25519 server-signature RSA-PSS (2048 bits))
 (No client certificate requested)
 by firstgate.proxmox.com (Proxmox) with ESMTPS id F25572D241
 for <pbs-devel@lists.proxmox.com>; Thu, 28 Jan 2021 12:59:57 +0100 (CET)
Received: from proxmox-new.maurer-it.com (localhost.localdomain [127.0.0.1])
 by proxmox-new.maurer-it.com (Proxmox) with ESMTP id BC20B46143
 for <pbs-devel@lists.proxmox.com>; Thu, 28 Jan 2021 12:59:57 +0100 (CET)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Thu, 28 Jan 2021 12:59:54 +0100
Message-Id: <20210128115955.23136-15-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.20.1
In-Reply-To: <20210128115955.23136-1-d.csapak@proxmox.com>
References: <20210128115955.23136-1-d.csapak@proxmox.com>
MIME-Version: 1.0
Content-Transfer-Encoding: 8bit
X-SPAM-LEVEL: Spam detection results:  0
 AWL 0.246 Adjusted score from AWL reputation of From: address
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 RCVD_IN_DNSWL_MED        -2.3 Sender listed at https://www.dnswl.org/,
 medium trust
 SPF_HELO_NONE           0.001 SPF: HELO does not publish an SPF Record
 SPF_PASS               -0.001 SPF: sender matches SPF record
 URIBL_BLOCKED 0.001 ADMINISTRATOR NOTICE: The query to URIBL was blocked. See
 http://wiki.apache.org/spamassassin/DnsBlocklists#dnsbl-block for more
 information. [mod.rs]
Subject: [pbs-devel] [PATCH proxmox-backup v2 14/15] tape/changer: refactor
 marking of import/export slots from config
X-BeenThere: pbs-devel@lists.proxmox.com
X-Mailman-Version: 2.1.29
Precedence: list
List-Id: Proxmox Backup Server development discussion
 <pbs-devel.lists.proxmox.com>
List-Unsubscribe: <https://lists.proxmox.com/cgi-bin/mailman/options/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=unsubscribe>
List-Archive: <http://lists.proxmox.com/pipermail/pbs-devel/>
List-Post: <mailto:pbs-devel@lists.proxmox.com>
List-Help: <mailto:pbs-devel-request@lists.proxmox.com?subject=help>
List-Subscribe: <https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel>, 
 <mailto:pbs-devel-request@lists.proxmox.com?subject=subscribe>
X-List-Received-Date: Thu, 28 Jan 2021 12:00:36 -0000

we did this for 'mtx', but missed it for the sg_pt_changer code
refactor it into the MtxStatus strut, and call it from both
code paths

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 src/tape/changer/mod.rs             | 32 ++++++++++++++++++++--
 src/tape/changer/mtx/mtx_wrapper.rs | 41 ++++-------------------------
 src/tape/changer/sg_pt_changer.rs   | 16 +++++++++++
 3 files changed, 51 insertions(+), 38 deletions(-)

diff --git a/src/tape/changer/mod.rs b/src/tape/changer/mod.rs
index 0d378587..f9f9da98 100644
--- a/src/tape/changer/mod.rs
+++ b/src/tape/changer/mod.rs
@@ -10,10 +10,16 @@ pub mod mtx;
 mod online_status_map;
 pub use online_status_map::*;
 
+use std::collections::HashSet;
+
 use anyhow::{bail, Error};
 use serde::{Serialize, Deserialize};
+use serde_json::Value;
+
+use proxmox::api::schema::parse_property_string;
 
 use crate::api2::types::{
+    SLOT_ARRAY_SCHEMA,
     ScsiTapeChanger,
     LinuxTapeDrive,
 };
@@ -124,6 +130,29 @@ impl MtxStatus {
         }
         free_slot
     }
+
+    pub fn mark_import_export_slots(&mut self, config: &ScsiTapeChanger) -> Result<(), Error>{
+        let mut export_slots: HashSet<u64> = HashSet::new();
+
+        if let Some(slots) = &config.export_slots {
+            let slots: Value = parse_property_string(&slots, &SLOT_ARRAY_SCHEMA)?;
+            export_slots = slots
+                .as_array()
+                .unwrap()
+                .iter()
+                .filter_map(|v| v.as_u64())
+                .collect();
+        }
+
+        for (i, entry) in self.slots.iter_mut().enumerate() {
+            let slot = i as u64 + 1;
+            if export_slots.contains(&slot) {
+                entry.import_export = true; // mark as IMPORT/EXPORT
+            }
+        }
+
+        Ok(())
+    }
 }
 
 /// Interface to SCSI changer devices
@@ -373,8 +402,7 @@ impl ScsiMediaChange for ScsiTapeChanger {
         if USE_MTX {
             mtx::mtx_status(&self)
         } else {
-            let mut file = sg_pt_changer::open(&self.path)?;
-            sg_pt_changer::read_element_status(&mut file)
+            sg_pt_changer::status(&self)
         }
     }
 
diff --git a/src/tape/changer/mtx/mtx_wrapper.rs b/src/tape/changer/mtx/mtx_wrapper.rs
index 116382f1..44d03251 100644
--- a/src/tape/changer/mtx/mtx_wrapper.rs
+++ b/src/tape/changer/mtx/mtx_wrapper.rs
@@ -1,45 +1,19 @@
-use std::collections::HashSet;
-
 use anyhow::Error;
 use serde_json::Value;
 
-use proxmox::{
-    api::schema::parse_property_string,
-};
-
 use crate::{
     tools::run_command,
-    api2::types::{
-        SLOT_ARRAY_SCHEMA,
-        ScsiTapeChanger,
-    },
-    tape::{
-        changer::{
-            MtxStatus,
-            mtx::{
-                parse_mtx_status,
-            },
-        },
+    api2::types::ScsiTapeChanger,
+    tape::changer::{
+        MtxStatus,
+        mtx::parse_mtx_status,
     },
 };
 
 /// Run 'mtx status' and return parsed result.
 pub fn mtx_status(config: &ScsiTapeChanger) -> Result<MtxStatus, Error> {
-
     let path = &config.path;
 
-    let mut export_slots: HashSet<u64> = HashSet::new();
-
-    if let Some(slots) = &config.export_slots {
-        let slots: Value = parse_property_string(&slots, &SLOT_ARRAY_SCHEMA)?;
-        export_slots = slots
-            .as_array()
-            .unwrap()
-            .iter()
-            .filter_map(|v| v.as_u64())
-            .collect();
-    }
-
     let mut command = std::process::Command::new("mtx");
     command.args(&["-f", path, "status"]);
 
@@ -47,12 +21,7 @@ pub fn mtx_status(config: &ScsiTapeChanger) -> Result<MtxStatus, Error> {
 
     let mut status = parse_mtx_status(&output)?;
 
-    for (i, entry) in status.slots.iter_mut().enumerate() {
-        let slot = i as u64 + 1;
-        if export_slots.contains(&slot) {
-            entry.import_export = true; // mark as IMPORT/EXPORT
-        }
-    }
+    status.mark_import_export_slots(&config)?;
 
     Ok(status)
 }
diff --git a/src/tape/changer/sg_pt_changer.rs b/src/tape/changer/sg_pt_changer.rs
index b6c64381..b2be9548 100644
--- a/src/tape/changer/sg_pt_changer.rs
+++ b/src/tape/changer/sg_pt_changer.rs
@@ -31,6 +31,7 @@ use crate::{
         scsi_ascii_to_string,
         scsi_inquiry,
     },
+    api2::types::ScsiTapeChanger,
 };
 
 const SCSI_CHANGER_DEFAULT_TIMEOUT: usize = 60*5; // 5 minutes
@@ -397,6 +398,21 @@ pub fn read_element_status<F: AsRawFd>(file: &mut F) -> Result<MtxStatus, Error>
     Ok(status)
 }
 
+/// Read status and map import-export slots from config
+pub fn status(config: &ScsiTapeChanger) -> Result<MtxStatus, Error> {
+    let path = &config.path;
+
+    let mut file = open(path)
+        .map_err(|err| format_err!("error opening '{}': {}", path, err))?;
+    let mut status = read_element_status(&mut file)
+        .map_err(|err| format_err!("error reading element status: {}", err))?;
+
+    status.mark_import_export_slots(&config)?;
+
+    Ok(status)
+}
+
+
 #[repr(C, packed)]
 #[derive(Endian)]
 struct ElementStatusHeader {
-- 
2.20.1