From mboxrd@z Thu Jan  1 00:00:00 1970
Return-Path: <pbs-devel-bounces@lists.proxmox.com>
Received: from firstgate.proxmox.com (firstgate.proxmox.com [212.224.123.68])
	by lore.proxmox.com (Postfix) with ESMTPS id 820231FF399
	for <inbox@lore.proxmox.com>; Thu, 23 May 2024 09:36:54 +0200 (CEST)
Received: from firstgate.proxmox.com (localhost [127.0.0.1])
	by firstgate.proxmox.com (Proxmox) with ESMTP id 3EA5E1A032;
	Thu, 23 May 2024 09:37:13 +0200 (CEST)
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Date: Thu, 23 May 2024 09:37:08 +0200
Message-Id: <20240523073709.1696815-1-d.csapak@proxmox.com>
X-Mailer: git-send-email 2.39.2
MIME-Version: 1.0
X-SPAM-LEVEL: Spam detection results:  0
 AWL -0.134 Adjusted score from AWL reputation of From: address
 BAYES_00                 -1.9 Bayes spam probability is 0 to 1%
 DMARC_MISSING             0.1 Missing DMARC policy
 KAM_DMARC_STATUS 0.01 Test Rule for DKIM or SPF Failure with Strict Alignment
 POISEN_SPAM_PILL          0.1 Meta: its spam
 POISEN_SPAM_PILL_2        0.1 random spam to be learned in bayes
 POISEN_SPAM_PILL_4        0.1 random spam to be learned in bayes
 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. [mam.rs]
Subject: [pbs-devel] [PATCH proxmox-backup 1/2] tape: mam: make sure
 attributes to write/clear don't diverge
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>
Reply-To: Proxmox Backup Server development discussion
 <pbs-devel@lists.proxmox.com>
Content-Type: text/plain; charset="us-ascii"
Content-Transfer-Encoding: 7bit
Errors-To: pbs-devel-bounces@lists.proxmox.com
Sender: "pbs-devel" <pbs-devel-bounces@lists.proxmox.com>

by creating an enum for all used host attributes and a static list we
want to write. Use this to generate the list to write and clear, so they
cannot diverge.

Additionally this also will show when we want to use a field that we
didn't define or when we defined one but didn't use.

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
---
 pbs-tape/src/sg_tape.rs     | 18 +--------
 pbs-tape/src/sg_tape/mam.rs | 74 +++++++++++++++++++++++++++++++++++++
 2 files changed, 76 insertions(+), 16 deletions(-)

diff --git a/pbs-tape/src/sg_tape.rs b/pbs-tape/src/sg_tape.rs
index 50f5c079..a7bd5d59 100644
--- a/pbs-tape/src/sg_tape.rs
+++ b/pbs-tape/src/sg_tape.rs
@@ -1053,21 +1053,7 @@ impl SgTape {
 
     /// Tries to write useful attributes to the MAM like Vendor/Application/Version
     pub fn write_mam_attributes(&mut self, label: Option<String>, pool: Option<String>) {
-        use pbs_buildcfg::PROXMOX_BACKUP_CRATE_VERSION;
-        let mut attribute_list: Vec<(u16, &[u8])> = vec![
-            (0x08_00, b"Proxmox"),               // APPLICATION VENDOR, 8 bytes
-            (0x08_01, b"Proxmox Backup Server"), // APPLICATION NAME, 32 bytes
-            (0x08_02, PROXMOX_BACKUP_CRATE_VERSION.as_bytes()), // APPLICATION VERSION, 8 bytes
-        ];
-        if let Some(ref label) = label {
-            attribute_list.push((0x08_03, label.as_bytes())); // USER MEDIUM TEXT LABEL, 160 bytes
-        }
-
-        if let Some(ref pool) = pool {
-            attribute_list.push((0x08_08, pool.as_bytes())); // MEDIA POOL, 160 bytes
-        }
-
-        for (id, data) in attribute_list {
+        for (id, data) in create_attribute_list_to_write(label.as_deref(), pool.as_deref()) {
             if let Err(err) = write_mam_attribute(&mut self.file, id, data) {
                 log::warn!("could not set MAM Attribute {id:x}: {err}");
             }
@@ -1076,7 +1062,7 @@ impl SgTape {
 
     // clear all custom set mam attributes
     fn clear_mam_attributes(&mut self) {
-        for attr in [0x08_00, 0x08_01, 0x08_02, 0x08_03, 0x08_08] {
+        for attr in create_attribute_list_to_clear() {
             if let Err(err) = write_mam_attribute(&mut self.file, attr, b"") {
                 log::warn!("could not clear MAM attribute {attr:x}: {err}");
             }
diff --git a/pbs-tape/src/sg_tape/mam.rs b/pbs-tape/src/sg_tape/mam.rs
index 4e995d0b..bc055a28 100644
--- a/pbs-tape/src/sg_tape/mam.rs
+++ b/pbs-tape/src/sg_tape/mam.rs
@@ -114,6 +114,80 @@ static MAM_ATTRIBUTES: &[MamType] = &[
     MamType::bin(0x10_01, 24, "Alternate Unique Cartridge Identify (Alt-UCI)"),
 ];
 
+#[derive(Copy, Clone)]
+// attributes that can be written to MAM
+enum HostAttributes {
+    ApplicationVendor = 0x08_00,
+    ApplicationName = 0x08_01,
+    ApplicationVersion = 0x08_02,
+    UserMediumTextLabel = 0x08_03,
+    //LastWritten = 0x08_04,
+    //TextLocalizationIdentifier = 0x08_05,
+    //Barcode = 0x08_06,
+    //OwningHostTextualName = 0x08_07,
+    MediaPool = 0x08_08,
+    // 0x08_09 reserved
+    // 0x08_0A reserved
+    //ApplicationFormatVersion = 0x08_0B,
+    //VolumeCoherencyInformation = 0x08_0C,
+    // 0x08_0D-0x0BFF reserved
+}
+
+impl HostAttributes {
+    // for some fields we can generate a static value
+    fn value(&self) -> Option<&'static [u8]> {
+        match self {
+            HostAttributes::ApplicationVendor => Some(b"Proxmox"),
+            HostAttributes::ApplicationName => Some(b"Proxmox Backup Server"),
+            HostAttributes::ApplicationVersion => {
+                Some(pbs_buildcfg::PROXMOX_BACKUP_CRATE_VERSION.as_bytes())
+            }
+            HostAttributes::UserMediumTextLabel => None,
+            HostAttributes::MediaPool => None,
+        }
+    }
+}
+
+// attributes we want to write/clear
+static ATTRIBUTES_TO_WRITE: &[HostAttributes] = &[
+    HostAttributes::ApplicationVendor,
+    HostAttributes::ApplicationName,
+    HostAttributes::ApplicationVersion,
+    HostAttributes::UserMediumTextLabel,
+    HostAttributes::MediaPool,
+];
+
+/// Returns a list of MAM attributes to write to tape
+pub(crate) fn create_attribute_list_to_write<'a>(
+    label: Option<&'a str>,
+    pool: Option<&'a str>,
+) -> Vec<(u16, &'a [u8])> {
+    let mut list = Vec::with_capacity(ATTRIBUTES_TO_WRITE.len());
+    for attr in ATTRIBUTES_TO_WRITE.iter() {
+        let value = match attr {
+            HostAttributes::ApplicationVendor
+            | HostAttributes::ApplicationName
+            | HostAttributes::ApplicationVersion => attr.value(),
+            HostAttributes::UserMediumTextLabel => label.map(|label| label.as_bytes()),
+            HostAttributes::MediaPool => pool.map(|pool| pool.as_bytes()),
+        };
+
+        if let Some(value) = value {
+            list.push((*attr as u16, value));
+        }
+    }
+
+    list
+}
+
+/// Returns a list of MAM attributes that we want to clear
+pub(crate) fn create_attribute_list_to_clear() -> Vec<u16> {
+    ATTRIBUTES_TO_WRITE
+        .iter()
+        .map(|attr| *attr as u16)
+        .collect()
+}
+
 lazy_static::lazy_static! {
 
     static ref MAM_ATTRIBUTE_NAMES: HashMap<u16, &'static MamType> = {
-- 
2.39.2



_______________________________________________
pbs-devel mailing list
pbs-devel@lists.proxmox.com
https://lists.proxmox.com/cgi-bin/mailman/listinfo/pbs-devel