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
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
use globset::GlobSet;
use crate::blockdir::StoreFiles;
use crate::index::IndexEntryIter;
use crate::stats::CopyStats;
use crate::*;
#[derive(Debug)]
pub struct BackupOptions {
pub print_filenames: bool,
pub excludes: GlobSet,
}
impl Default for BackupOptions {
fn default() -> Self {
BackupOptions {
print_filenames: false,
excludes: GlobSet::empty(),
}
}
}
pub struct BackupWriter {
band: Band,
index_builder: IndexBuilder,
store_files: StoreFiles,
basis_index: Option<IndexEntryIter>,
}
impl BackupWriter {
pub fn begin(archive: &Archive) -> Result<BackupWriter> {
if gc_lock::GarbageCollectionLock::is_locked(archive)? {
return Err(Error::GarbageCollectionLockHeld);
}
let basis_index = archive
.last_complete_band()?
.map(|b| b.iter_entries())
.transpose()?;
let band = Band::create(archive)?;
let index_builder = band.index_builder();
Ok(BackupWriter {
band,
index_builder,
store_files: StoreFiles::new(archive.block_dir().clone()),
basis_index,
})
}
pub(crate) fn push_entry(&mut self, index_entry: IndexEntry) -> Result<()> {
self.index_builder.push_entry(index_entry)?;
Ok(())
}
}
impl tree::WriteTree for BackupWriter {
fn finish(self) -> Result<CopyStats> {
let index_builder_stats = self.index_builder.finish()?;
self.band.close(index_builder_stats.index_hunks)?;
Ok(CopyStats {
index_builder_stats,
..CopyStats::default()
})
}
fn copy_dir<E: Entry>(&mut self, source_entry: &E) -> Result<()> {
self.push_entry(IndexEntry::metadata_from(source_entry))
}
fn copy_file<R: ReadTree>(
&mut self,
source_entry: &R::Entry,
from_tree: &R,
) -> Result<CopyStats> {
let mut stats = CopyStats::default();
let apath = source_entry.apath();
if let Some(basis_entry) = self
.basis_index
.as_mut()
.map(|bi| bi.advance_to(&apath))
.flatten()
{
if source_entry.is_unchanged_from(&basis_entry) {
stats.unmodified_files += 1;
self.push_entry(basis_entry)?;
return Ok(stats);
} else {
stats.modified_files += 1;
}
} else {
stats.new_files += 1;
}
let content = &mut from_tree.file_contents(&source_entry)?;
let (addrs, file_stats) = self.store_files.store_file_content(&apath, content)?;
stats += file_stats;
self.push_entry(IndexEntry {
addrs,
..IndexEntry::metadata_from(source_entry)
})?;
Ok(stats)
}
fn copy_symlink<E: Entry>(&mut self, source_entry: &E) -> Result<()> {
let target = source_entry.symlink_target().clone();
assert!(target.is_some());
self.push_entry(IndexEntry::metadata_from(source_entry))
}
}