]> git.proxmox.com Git - proxmox-backup.git/blob - src/backup/datastore.rs
start impl garbage collection
[proxmox-backup.git] / src / backup / datastore.rs
1 use failure::*;
2
3 use std::path::{PathBuf, Path};
4
5 use crate::config::datastore;
6 use super::chunk_store::*;
7 use super::image_index::*;
8
9 pub struct DataStore {
10 chunk_store: ChunkStore,
11 }
12
13 impl DataStore {
14
15 pub fn open(store_name: &str) -> Result<Self, Error> {
16
17 let config = datastore::config()?;
18 let (_, store_config) = config.sections.get(store_name)
19 .ok_or(format_err!("no such datastore '{}'", store_name))?;
20
21 let path = store_config["path"].as_str().unwrap();
22
23 let chunk_store = ChunkStore::open(path)?;
24
25 Ok(Self {
26 chunk_store: chunk_store,
27 })
28 }
29
30 pub fn create_image_writer<P: AsRef<Path>>(&mut self, filename: P, size: usize) -> Result<ImageIndexWriter, Error> {
31
32 let index = ImageIndexWriter::create(&mut self.chunk_store, filename.as_ref(), size)?;
33
34 Ok(index)
35 }
36
37 pub fn open_image_reader<P: AsRef<Path>>(&mut self, filename: P) -> Result<ImageIndexReader, Error> {
38
39 let index = ImageIndexReader::open(&mut self.chunk_store, filename.as_ref())?;
40
41 Ok(index)
42 }
43
44 pub fn list_images(&self) -> Result<Vec<PathBuf>, Error> {
45 let base = self.chunk_store.base_path();
46
47 let mut list = vec![];
48
49 for entry in std::fs::read_dir(base)? {
50 let entry = entry?;
51 if entry.file_type()?.is_file() {
52 let path = entry.path();
53 if let Some(ext) = path.extension() {
54 if ext == "idx" {
55 list.push(path);
56 }
57 }
58 }
59 }
60
61 Ok(list)
62 }
63
64 fn sweep_used_chunks(&mut self) -> Result<(), Error> {
65
66 Ok(())
67 }
68
69 fn mark_used_chunks(&mut self) -> Result<(), Error> {
70
71 let image_list = self.list_images()?;
72
73 for path in image_list {
74 let mut index = self.open_image_reader(path)?;
75 index.mark_used_chunks();
76 }
77
78 Ok(())
79 }
80
81 pub fn garbage_collection(&mut self) -> Result<(), Error> {
82
83 self.mark_used_chunks()?;
84 self.sweep_used_chunks()?;
85
86 Ok(())
87 }
88 }