1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134
// Copyright 2017, 2018, 2019, 2020, 2021 Martin Pool.
// This program is free software; you can redistribute it and/or modify
// it under the terms of the GNU General Public License as published by
// the Free Software Foundation; either version 2 of the License, or
// (at your option) any later version.
//
// This program is distributed in the hope that it will be useful,
// but WITHOUT ANY WARRANTY; without even the implied warranty of
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
// GNU General Public License for more details.
//! Access a versioned tree stored in the archive.
//!
//! Through this interface you can iterate the contents and retrieve file contents.
//!
//! This is the preferred higher-level interface for reading stored versions. It'll abstract
//! across incremental backups, hiding from the caller that data may be distributed across
//! multiple index files, bands, and blocks.
use std::sync::Arc;
use crate::blockdir::BlockDir;
use crate::stitch::IterStitchedIndexHunks;
use crate::*;
/// Read index and file contents for a version stored in the archive.
#[derive(Debug)]
pub struct StoredTree {
band: Band,
archive: Archive,
block_dir: Arc<BlockDir>,
}
impl StoredTree {
pub(crate) fn open(archive: &Archive, band_id: BandId) -> Result<StoredTree> {
Ok(StoredTree {
band: Band::open(archive, band_id)?,
block_dir: archive.block_dir.clone(),
archive: archive.clone(),
})
}
pub fn band(&self) -> &Band {
&self.band
}
pub fn is_closed(&self) -> Result<bool> {
self.band.is_closed()
}
pub fn block_dir(&self) -> &BlockDir {
&self.block_dir
}
}
impl ReadTree for StoredTree {
type Entry = IndexEntry;
type IT = index::IndexEntryIter<stitch::IterStitchedIndexHunks>;
/// Return an iter of index entries in this stored tree.
// TODO: Should return an iter of Result<Entry> so that we can inspect them...
fn iter_entries(&self, subtree: Apath, exclude: Exclude) -> Result<Self::IT> {
Ok(
IterStitchedIndexHunks::new(&self.archive, Some(self.band.id()))
.iter_entries(subtree, exclude),
)
}
}
#[cfg(test)]
mod test {
use std::path::Path;
use super::super::test_fixtures::*;
use super::super::*;
#[test]
pub fn open_stored_tree() {
let af = ScratchArchive::new();
af.store_two_versions();
let last_band_id = af.last_band_id().unwrap().unwrap();
let st = af.open_stored_tree(BandSelectionPolicy::Latest).unwrap();
assert_eq!(st.band().id(), last_band_id);
let names: Vec<String> = st
.iter_entries(Apath::root(), Exclude::nothing())
.unwrap()
.map(|e| e.apath.into())
.collect();
let expected = if SYMLINKS_SUPPORTED {
vec![
"/",
"/hello",
"/hello2",
"/link",
"/subdir",
"/subdir/subfile",
]
} else {
vec!["/", "/hello", "/hello2", "/subdir", "/subdir/subfile"]
};
assert_eq!(expected, names);
}
#[test]
pub fn cant_open_no_versions() {
let af = ScratchArchive::new();
assert_eq!(
af.open_stored_tree(BandSelectionPolicy::Latest)
.unwrap_err()
.to_string(),
"Archive is empty"
);
}
#[test]
fn iter_entries() {
let archive = Archive::open_path(Path::new("testdata/archive/minimal/v0.6.3/")).unwrap();
let st = archive
.open_stored_tree(BandSelectionPolicy::Latest)
.unwrap();
let names: Vec<String> = st
.iter_entries("/subdir".into(), Exclude::nothing())
.unwrap()
.map(|entry| entry.apath.into())
.collect();
assert_eq!(names.as_slice(), ["/subdir", "/subdir/subfile"]);
}
}