tape: hide internal use of all zero uuid for unassigned tapes

a tape assigned to a pool but no media-set, gets the special 'all zero'
media set in it's MediaSetLabel. Instead of having that constant
scattered all over the code, hide this fact by using wrapper functions
to initialize it that way and to check for it

Signed-off-by: Dominik Csapak <d.csapak@proxmox.com>
This commit is contained in:
Dominik Csapak 2022-11-29 15:17:00 +01:00 committed by Thomas Lamprecht
parent ab6964799c
commit 52517f7b9e
5 changed files with 24 additions and 21 deletions

View File

@ -528,7 +528,7 @@ fn write_media_label(
label.label_text, label.label_text,
pool pool
); );
let set = MediaSetLabel::with_data(pool, [0u8; 16].into(), 0, label.ctime, None); let set = MediaSetLabel::new_unassigned(pool, label.ctime);
drive.write_media_set_label(&set, None)?; drive.write_media_set_label(&set, None)?;
@ -575,7 +575,7 @@ fn write_media_label(
if let Some(ref pool) = pool { if let Some(ref pool) = pool {
match info.media_set_label { match info.media_set_label {
Some(set) => { Some(set) => {
if set.uuid != [0u8; 16].into() { if !set.unassigned() {
bail!("verify media set label failed - got wrong set uuid"); bail!("verify media set label failed - got wrong set uuid");
} }
if &set.pool != pool { if &set.pool != pool {
@ -1301,7 +1301,7 @@ pub fn catalog_media(
return Ok(()); return Ok(());
} }
Some(ref set) => { Some(ref set) => {
if set.uuid.as_ref() == [0u8; 16] { if set.unassigned() {
// media is empty // media is empty
task_log!(worker, "media is empty"); task_log!(worker, "media is empty");
let _lock = lock_unassigned_media_pool(TAPE_STATUS_DIR)?; let _lock = lock_unassigned_media_pool(TAPE_STATUS_DIR)?;

View File

@ -336,8 +336,7 @@ pub fn destroy_media(label_text: String, force: Option<bool>) -> Result<(), Erro
if !force { if !force {
if let Some(ref set) = media_id.media_set_label { if let Some(ref set) = media_id.media_set_label {
let is_empty = set.uuid.as_ref() == [0u8; 16]; if !set.unassigned() {
if !is_empty {
bail!( bail!(
"media '{}' contains data (please use 'force' flag to remove.", "media '{}' contains data (please use 'force' flag to remove.",
label_text label_text

View File

@ -168,4 +168,12 @@ impl MediaSetLabel {
encryption_key_fingerprint, encryption_key_fingerprint,
} }
} }
pub fn new_unassigned(pool: &str, ctime: i64) -> Self {
Self::with_data(pool, [0u8; 16].into(), 0, ctime, None)
}
pub fn unassigned(&self) -> bool {
self.uuid.as_ref() == [0u8; 16]
}
} }

View File

@ -185,7 +185,7 @@ impl Inventory {
// do not overwrite unsaved pool assignments // do not overwrite unsaved pool assignments
if media_id.media_set_label.is_none() { if media_id.media_set_label.is_none() {
if let Some(ref set) = previous.id.media_set_label { if let Some(ref set) = previous.id.media_set_label {
if set.uuid.as_ref() == [0u8; 16] { if set.unassigned() {
media_id.media_set_label = Some(set.clone()); media_id.media_set_label = Some(set.clone());
} }
} }
@ -251,15 +251,11 @@ impl Inventory {
pub fn lookup_media_pool(&self, uuid: &Uuid) -> Option<(&str, bool)> { pub fn lookup_media_pool(&self, uuid: &Uuid) -> Option<(&str, bool)> {
match self.map.get(uuid) { match self.map.get(uuid) {
None => None, None => None,
Some(entry) => { Some(entry) => entry
match entry.id.media_set_label { .id
None => None, // not assigned to any pool .media_set_label
Some(ref set) => { .as_ref()
let is_empty = set.uuid.as_ref() == [0u8; 16]; .map(|set| (set.pool.as_str(), set.unassigned())),
Some((&set.pool, is_empty))
}
}
}
} }
} }
@ -275,7 +271,7 @@ impl Inventory {
continue; // belong to another pool continue; // belong to another pool
} }
if set.uuid.as_ref() == [0u8; 16] { if set.unassigned() {
list.push(MediaId { list.push(MediaId {
label: entry.id.label.clone(), label: entry.id.label.clone(),
media_set_label: None, media_set_label: None,
@ -298,7 +294,7 @@ impl Inventory {
match entry.id.media_set_label { match entry.id.media_set_label {
None => continue, // not assigned to any pool None => continue, // not assigned to any pool
Some(ref set) => { Some(ref set) => {
if set.uuid.as_ref() != [0u8; 16] { if set.unassigned() {
list.push(entry.id.clone()); list.push(entry.id.clone());
} }
} }
@ -410,7 +406,7 @@ impl Inventory {
.map .map
.values() .values()
.filter_map(|entry| entry.id.media_set_label.as_ref()) .filter_map(|entry| entry.id.media_set_label.as_ref())
.filter(|set| set.pool == pool && set.uuid.as_ref() != [0u8; 16]); .filter(|set| set.pool == pool && !set.unassigned());
for set in set_list { for set in set_list {
match last_set { match last_set {
@ -435,7 +431,7 @@ impl Inventory {
.map .map
.values() .values()
.filter_map(|entry| entry.id.media_set_label.as_ref()) .filter_map(|entry| entry.id.media_set_label.as_ref())
.filter(|set| set.pool == pool && set.uuid.as_ref() != [0u8; 16]); .filter(|set| set.pool == pool && !set.unassigned());
for set in set_list { for set in set_list {
if set.uuid != uuid && set.ctime >= ctime { if set.uuid != uuid && set.ctime >= ctime {
@ -600,7 +596,7 @@ impl Inventory {
let uuid = label.uuid.clone(); let uuid = label.uuid.clone();
let set = MediaSetLabel::with_data(pool, [0u8; 16].into(), 0, ctime, None); let set = MediaSetLabel::new_unassigned(pool, ctime);
self.store( self.store(
MediaId { MediaId {

View File

@ -181,7 +181,7 @@ impl MediaPool {
// should never trigger // should never trigger
return (MediaStatus::Unknown, location); // belong to another pool return (MediaStatus::Unknown, location); // belong to another pool
} }
if set.uuid.as_ref() == [0u8; 16] { if set.unassigned() {
// not assigned to any pool // not assigned to any pool
return (MediaStatus::Writable, location); return (MediaStatus::Writable, location);
} }