diff --git a/src/tape/inventory.rs b/src/tape/inventory.rs index b8ab149f..cc3872fd 100644 --- a/src/tape/inventory.rs +++ b/src/tape/inventory.rs @@ -32,6 +32,7 @@ use crate::{ tape::{ TAPE_STATUS_DIR, OnlineStatusMap, + MediaSet, file_formats::{ MediaLabel, MediaSetLabel, @@ -49,79 +50,6 @@ pub struct MediaId { pub media_set_label: Option, } -/// Media Set -/// -/// A List of backup media -#[derive(Debug, Serialize, Deserialize)] -pub struct MediaSet { - /// Unique media set ID - uuid: Uuid, - /// List of BackupMedia - media_list: Vec>, -} - -impl MediaSet { - - pub const MEDIA_SET_MAX_SEQ_NR: u64 = 100; - - pub fn new() -> Self { - let uuid = Uuid::generate(); - Self { - uuid, - media_list: Vec::new(), - } - } - - pub fn with_data(uuid: Uuid, media_list: Vec>) -> Self { - Self { uuid, media_list } - } - - pub fn uuid(&self) -> &Uuid { - &self.uuid - } - - pub fn media_list(&self) -> &[Option] { - &self.media_list - } - - pub fn add_media(&mut self, uuid: Uuid) { - self.media_list.push(Some(uuid)); - } - - pub fn insert_media(&mut self, uuid: Uuid, seq_nr: u64) -> Result<(), Error> { - if seq_nr > Self::MEDIA_SET_MAX_SEQ_NR { - bail!("media set sequence number to large in media set {} ({} > {})", - self.uuid.to_string(), seq_nr, Self::MEDIA_SET_MAX_SEQ_NR); - } - let seq_nr = seq_nr as usize; - if self.media_list.len() > seq_nr { - if self.media_list[seq_nr].is_some() { - bail!("found duplicate squence number in media set '{}/{}'", - self.uuid.to_string(), seq_nr); - } - } else { - self.media_list.resize(seq_nr + 1, None); - } - self.media_list[seq_nr] = Some(uuid); - Ok(()) - } - - pub fn last_media_uuid(&self) -> Option<&Uuid> { - match self.media_list.last() { - None => None, - Some(None) => None, - Some(Some(ref last_uuid)) => Some(last_uuid), - } - } - - pub fn is_last_media(&self, uuid: &Uuid) -> bool { - match self.media_list.last() { - None => false, - Some(None) => false, - Some(Some(last_uuid)) => uuid == last_uuid, - } - } -} #[derive(Serialize,Deserialize)] struct MediaStateEntry { diff --git a/src/tape/media_set.rs b/src/tape/media_set.rs new file mode 100644 index 00000000..0cef10b7 --- /dev/null +++ b/src/tape/media_set.rs @@ -0,0 +1,76 @@ +use anyhow::{bail, Error}; +use serde::{Serialize, Deserialize}; + +use proxmox::tools::Uuid; + +/// MediaSet - Ordered group of media +#[derive(Debug, Serialize, Deserialize)] +pub struct MediaSet { + /// Unique media set ID + uuid: Uuid, + /// List of member media IDs + media_list: Vec>, +} + +impl MediaSet { + + pub const MEDIA_SET_MAX_SEQ_NR: u64 = 100; + + pub fn new() -> Self { + let uuid = Uuid::generate(); + Self { + uuid, + media_list: Vec::new(), + } + } + + pub fn with_data(uuid: Uuid, media_list: Vec>) -> Self { + Self { uuid, media_list } + } + + pub fn uuid(&self) -> &Uuid { + &self.uuid + } + + pub fn media_list(&self) -> &[Option] { + &self.media_list + } + + pub fn add_media(&mut self, uuid: Uuid) { + self.media_list.push(Some(uuid)); + } + + pub fn insert_media(&mut self, uuid: Uuid, seq_nr: u64) -> Result<(), Error> { + if seq_nr > Self::MEDIA_SET_MAX_SEQ_NR { + bail!("media set sequence number to large in media set {} ({} > {})", + self.uuid.to_string(), seq_nr, Self::MEDIA_SET_MAX_SEQ_NR); + } + let seq_nr = seq_nr as usize; + if self.media_list.len() > seq_nr { + if self.media_list[seq_nr].is_some() { + bail!("found duplicate squence number in media set '{}/{}'", + self.uuid.to_string(), seq_nr); + } + } else { + self.media_list.resize(seq_nr + 1, None); + } + self.media_list[seq_nr] = Some(uuid); + Ok(()) + } + + pub fn last_media_uuid(&self) -> Option<&Uuid> { + match self.media_list.last() { + None => None, + Some(None) => None, + Some(Some(ref last_uuid)) => Some(last_uuid), + } + } + + pub fn is_last_media(&self, uuid: &Uuid) -> bool { + match self.media_list.last() { + None => false, + Some(None) => false, + Some(Some(last_uuid)) => uuid == last_uuid, + } + } +} diff --git a/src/tape/mod.rs b/src/tape/mod.rs index 485ffc66..2729eef9 100644 --- a/src/tape/mod.rs +++ b/src/tape/mod.rs @@ -20,6 +20,9 @@ pub use tape_read::*; mod helpers; pub use helpers::*; +mod media_set; +pub use media_set::*; + mod inventory; pub use inventory::*;