public inbox for pbs-devel@lists.proxmox.com
 help / color / mirror / Atom feed
* [pbs-devel] [PATCH proxmox-backup 1/2] tape: mam: make sure attributes to write/clear don't diverge
@ 2024-05-23  7:37 Dominik Csapak
  2024-05-23  7:37 ` [pbs-devel] [PATCH proxmox-backup 2/2] tape: mam: set the text localization identifier to utf-8 Dominik Csapak
  0 siblings, 1 reply; 2+ messages in thread
From: Dominik Csapak @ 2024-05-23  7:37 UTC (permalink / raw)
  To: pbs-devel

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


^ permalink raw reply	[flat|nested] 2+ messages in thread

end of thread, other threads:[~2024-05-23  7:37 UTC | newest]

Thread overview: 2+ messages (download: mbox.gz / follow: Atom feed)
-- links below jump to the message on this page --
2024-05-23  7:37 [pbs-devel] [PATCH proxmox-backup 1/2] tape: mam: make sure attributes to write/clear don't diverge Dominik Csapak
2024-05-23  7:37 ` [pbs-devel] [PATCH proxmox-backup 2/2] tape: mam: set the text localization identifier to utf-8 Dominik Csapak

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