[pbs-devel] [PATCH proxmox-backup 2/4] api types: introduce `BackupArchiveName` type

Christian Ebner c.ebner at proxmox.com
Thu Jul 4 10:55:57 CEST 2024


Introduces a dedicated wrapper type to be used for backup archive
names instead of String.

Signed-off-by: Christian Ebner <c.ebner at proxmox.com>
---
 pbs-api-types/src/datastore.rs | 102 ++++++++++++++++++++++++++++++++-
 1 file changed, 101 insertions(+), 1 deletion(-)

diff --git a/pbs-api-types/src/datastore.rs b/pbs-api-types/src/datastore.rs
index b63e7c2ff..16c3eef59 100644
--- a/pbs-api-types/src/datastore.rs
+++ b/pbs-api-types/src/datastore.rs
@@ -1570,7 +1570,7 @@ pub fn print_store_and_ns(store: &str, ns: &BackupNamespace) -> String {
     }
 }
 
-#[derive(PartialEq, Eq)]
+#[derive(Clone, PartialEq, Eq)]
 /// Allowed variants of backup archives to be contained in a snapshot's manifest
 pub enum ArchiveType {
     FixedIndex,
@@ -1590,3 +1590,103 @@ impl ArchiveType {
         Ok(archive_type)
     }
 }
+
+#[derive(Clone, PartialEq, Eq)]
+/// Name of archive files contained in snapshot's manifest
+pub struct BackupArchiveName {
+    // archive name including the `.fidx`, `.didx` or `.blob` extension
+    name: String,
+    // type parsed based on given extension
+    ty: ArchiveType,
+}
+
+impl std::fmt::Display for BackupArchiveName {
+    fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
+        write!(f, "{name}", name = self.name)
+    }
+}
+
+serde_plain::derive_deserialize_from_fromstr!(BackupArchiveName, "archive name");
+
+impl std::str::FromStr for BackupArchiveName {
+    type Err = Error;
+
+    fn from_str(name: &str) -> Result<Self, Self::Err> {
+        Self::try_from(name)
+    }
+}
+
+serde_plain::derive_serialize_from_display!(BackupArchiveName);
+
+impl std::convert::TryFrom<&str> for BackupArchiveName {
+    type Error = anyhow::Error;
+
+    fn try_from(value: &str) -> Result<Self, Self::Error> {
+        let (name, ty) = Self::parse_archive_type(value)?;
+        Ok(Self { name, ty })
+    }
+}
+
+impl std::convert::AsRef<str> for BackupArchiveName {
+    fn as_ref(&self) -> &str {
+        &self.name
+    }
+}
+
+impl BackupArchiveName {
+    pub fn from_path(path: impl AsRef<Path>) -> Result<Self, Error> {
+        let path = path.as_ref();
+        let file_name = path
+            .file_name()
+            .ok_or_else(|| format_err!("invalid archive name"))?;
+        let name = file_name
+            .to_str()
+            .ok_or_else(|| format_err!("archive name not valid UTF-8"))?;
+
+        Self::try_from(name)
+    }
+
+    pub fn archive_type(&self) -> ArchiveType {
+        self.ty.clone()
+    }
+
+    pub fn ends_with(&self, postfix: &str) -> bool {
+        self.name.ends_with(postfix)
+    }
+
+    pub fn has_pxar_filename_extension(&self) -> bool {
+        self.name.ends_with(".pxar.didx")
+            || self.name.ends_with(".mpxar.didx")
+            || self.name.ends_with(".ppxar.didx")
+    }
+
+    pub fn without_type_extension(&self) -> String {
+        match self.ty {
+            ArchiveType::DynamicIndex => self.name.strip_suffix(".didx").unwrap().into(),
+            ArchiveType::FixedIndex => self.name.strip_suffix(".fidx").unwrap().into(),
+            ArchiveType::Blob => self.name.strip_suffix(".blob").unwrap().into(),
+        }
+    }
+
+    fn parse_archive_type(archive_name: &str) -> Result<(String, ArchiveType), Error> {
+        if archive_name.ends_with(".didx")
+            || archive_name.ends_with(".fidx")
+            || archive_name.ends_with(".blob")
+        {
+            Ok((archive_name.into(), ArchiveType::from_path(archive_name)?))
+        } else if archive_name.ends_with(".pxar")
+            || archive_name.ends_with(".mpxar")
+            || archive_name.ends_with(".ppxar")
+        {
+            Ok((format!("{}.didx", archive_name), ArchiveType::DynamicIndex))
+        } else if archive_name.ends_with(".img") {
+            Ok((format!("{}.fidx", archive_name), ArchiveType::FixedIndex))
+        } else {
+            Ok((format!("{}.blob", archive_name), ArchiveType::Blob))
+        }
+    }
+}
+
+impl ApiType for BackupArchiveName {
+    const API_SCHEMA: Schema = BACKUP_ARCHIVE_NAME_SCHEMA;
+}
-- 
2.39.2





More information about the pbs-devel mailing list