]> git.proxmox.com Git - proxmox-backup.git/blob - src/tape/pool_writer/catalog_set.rs
update to first proxmox crate split
[proxmox-backup.git] / src / tape / pool_writer / catalog_set.rs
1 use anyhow::{bail, Error};
2
3 use proxmox_uuid::Uuid;
4
5 use crate::{
6 tape::{
7 MediaCatalog,
8 MediaSetCatalog,
9 },
10 };
11
12 /// Helper to build and query sets of catalogs
13 ///
14 /// Similar to MediaSetCatalog, but allows to modify the last catalog.
15 pub struct CatalogSet {
16 // read only part
17 pub media_set_catalog: MediaSetCatalog,
18 // catalog to modify (latest in set)
19 pub catalog: Option<MediaCatalog>,
20 }
21
22 impl CatalogSet {
23
24 /// Create empty instance
25 pub fn new() -> Self {
26 Self {
27 media_set_catalog: MediaSetCatalog::new(),
28 catalog: None,
29 }
30 }
31
32 /// Add catalog to the read-only set
33 pub fn append_read_only_catalog(&mut self, catalog: MediaCatalog) -> Result<(), Error> {
34 self.media_set_catalog.append_catalog(catalog)
35 }
36
37 /// Test if the catalog already contains a snapshot
38 pub fn contains_snapshot(&self, store: &str, snapshot: &str) -> bool {
39 if let Some(ref catalog) = self.catalog {
40 if catalog.contains_snapshot(store, snapshot) {
41 return true;
42 }
43 }
44 self.media_set_catalog.contains_snapshot(store, snapshot)
45 }
46
47 /// Test if the catalog already contains a chunk
48 pub fn contains_chunk(&self, store: &str, digest: &[u8;32]) -> bool {
49 if let Some(ref catalog) = self.catalog {
50 if catalog.contains_chunk(store, digest) {
51 return true;
52 }
53 }
54 self.media_set_catalog.contains_chunk(store, digest)
55 }
56
57 /// Add a new catalog, move the old on to the read-only set
58 pub fn append_catalog(&mut self, new_catalog: MediaCatalog) -> Result<(), Error> {
59
60 // append current catalog to read-only set
61 if let Some(catalog) = self.catalog.take() {
62 self.media_set_catalog.append_catalog(catalog)?;
63 }
64
65 // remove read-only version from set (in case it is there)
66 self.media_set_catalog.remove_catalog(&new_catalog.uuid());
67
68 self.catalog = Some(new_catalog);
69
70 Ok(())
71 }
72
73 /// Register a snapshot
74 pub fn register_snapshot(
75 &mut self,
76 uuid: Uuid, // Uuid form MediaContentHeader
77 file_number: u64,
78 store: &str,
79 snapshot: &str,
80 ) -> Result<(), Error> {
81 match self.catalog {
82 Some(ref mut catalog) => {
83 catalog.register_snapshot(uuid, file_number, store, snapshot)?;
84 }
85 None => bail!("no catalog loaded - internal error"),
86 }
87 Ok(())
88 }
89
90 /// Register a chunk archive
91 pub fn register_chunk_archive(
92 &mut self,
93 uuid: Uuid, // Uuid form MediaContentHeader
94 file_number: u64,
95 store: &str,
96 chunk_list: &[[u8; 32]],
97 ) -> Result<(), Error> {
98 match self.catalog {
99 Some(ref mut catalog) => {
100 catalog.register_chunk_archive(uuid, file_number, store, chunk_list)?;
101 }
102 None => bail!("no catalog loaded - internal error"),
103 }
104 Ok(())
105 }
106
107 /// Commit the catalog changes
108 pub fn commit(&mut self) -> Result<(), Error> {
109 if let Some(ref mut catalog) = self.catalog {
110 catalog.commit()?;
111 }
112 Ok(())
113 }
114 }