public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
From: Dominik Csapak <d.csapak@proxmox.com>
To: pbs-devel@lists.proxmox.com
Subject: [pbs-devel] [PATCH proxmox-backup 1/2] tape: mam: make sure attributes to write/clear don't diverge
Date: Thu, 23 May 2024 09:37:08 +0200	[thread overview]
Message-ID: <20240523073709.1696815-1-d.csapak@proxmox.com> (raw)

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


             reply	other threads:[~2024-05-23  7:36 UTC|newest]

Thread overview: 2+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2024-05-23  7:37 Dominik Csapak [this message]
2024-05-23  7:37 ` [pbs-devel] [PATCH proxmox-backup 2/2] tape: mam: set the text localization identifier to utf-8 Dominik Csapak

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=20240523073709.1696815-1-d.csapak@proxmox.com \
    --to=d.csapak@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 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