Newer
Older
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
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
use std::{
fs::File,
io::{BufReader, Seek, Write},
path::Path,
};
use filetime::FileTime;
use log::{debug, trace};
use crate::{
chunker::{Chunker, FixedSizeChunker},
codec::{ArCodec, BLOCK_SIZE},
datamodel::{AllZeroExt, ArEntry, BlockPool, CompressionType, EntryType, FileTable},
dirwalk::{FsEntry, WalkDir},
read_write_extension::WriteExtTypes,
};
pub struct Archiver<T: Write + Seek> {
codec: ArCodec,
blocks_out: BlockPool<T>,
reference_file_table: Option<FileTable>,
}
impl<T: Write + Seek> Archiver<T> {
pub fn new(mut codec: ArCodec, blocks_out: BlockPool<T>) -> Self {
// FileTable and IndexTable don't have a UID, so this is a fresh backup. Take the random
// UID from the BlockPool
if codec.file_table.uid.is_all_zero() && codec.index_table.uid.is_all_zero() {
debug!("FileTable & IndexTable have no UID, using BlockPool UID");
codec.file_table.uid = blocks_out.uid;
codec.index_table.uid = blocks_out.uid;
}
// BlockPool and IndexTable have the same UID, but FileTable is default. This is a new
// addition to an existing BackupPool
if codec.index_table.uid == blocks_out.uid && codec.file_table.uid.is_all_zero() {
codec.file_table.uid = blocks_out.uid;
}
Self {
codec,
blocks_out,
reference_file_table: None,
}
}
/// Check that all loaded files have the same UID
///
/// Returns true if the UIDs are the same, false otherwise
pub fn check_uids(&self) -> bool {
self.codec.file_table.uid == self.codec.index_table.uid
&& self.codec.file_table.uid == self.blocks_out.uid
}
pub fn codec(&self) -> &ArCodec {
&self.codec
}
pub fn load_reference_metadata(
&mut self,
ft_path: impl AsRef<Path>,
) -> Result<(), std::io::Error> {
let ft_file = File::open(ft_path.as_ref())?;
let ft_file = BufReader::new(ft_file);
let mut decoder = zstd::Decoder::new(ft_file)?;
self.reference_file_table = Some(FileTable::try_deserialize_from(&mut decoder)?);
Ok(())
}
pub fn archive_dir_recursive(&mut self, path: impl AsRef<Path>) -> Result<(), std::io::Error> {
if !path.as_ref().is_dir() {
return self.archive_file(&FsEntry::new(path.as_ref().to_path_buf())?);
}
let entries_to_archive = WalkDir::new(path).run_iter();
let _dbg_num_blocks_before = self.codec.index_table.blocks.len();
let _dbg_num_pool_bytes_before = self.blocks_out.next_block_offset;
for entry in entries_to_archive {
match entry {
Ok(entry) if entry.metadata.is_symlink() => self.archive_symlink(&entry)?,
Ok(entry) if entry.metadata.is_dir() => self.archive_empty_dir(&entry)?,
Ok(entry) => self.archive_file(&entry)?,
Err(err) => {
eprintln!("Archive error for '{}': {}", err.path.display(), err.error);
continue;
}
}
}
let _dbg_num_blocks_after = self.codec.index_table.blocks.len();
let _dbg_num_new_blocks = _dbg_num_blocks_after - _dbg_num_blocks_before;
let _dbg_num_pool_bytes_after = self.blocks_out.next_block_offset;
let _dbg_num_pool_bytes_new = _dbg_num_pool_bytes_after - _dbg_num_pool_bytes_before;
debug!(
"Recursively archived dir. Added {} blocks / {} bytes to BlockPool",
_dbg_num_new_blocks, _dbg_num_pool_bytes_new
);
Ok(())
}
pub fn archive_empty_dir(&mut self, entry: &FsEntry) -> Result<(), std::io::Error> {
let path = &entry.path;
let mut ar_entry = ArEntry::default();
// let md = std::fs::symlink_metadata(path.as_ref())?;
let md = &entry.metadata;
if !md.is_dir() {
panic!(
"Called archive_empty_dir on a non-dir path: {}",
path.display()
);
}
let mod_time = FileTime::from_last_modification_time(md);
ar_entry
.metadata
.entry_and_os_type
.set_entry_type(EntryType::Directory);
ar_entry.metadata.modified_unix_seconds = mod_time.unix_seconds();
ar_entry.metadata.modified_nanos = mod_time.nanoseconds();
ar_entry.path = path.to_string_lossy().to_string();
self.codec.file_table.entries.push(ar_entry);
Ok(())
}
pub fn archive_symlink(&mut self, entry: &FsEntry) -> Result<(), std::io::Error> {
let path = &entry.path;
let mut ar_entry = ArEntry::default();
// let md = std::fs::symlink_metadata(path.as_ref())?;
let md = &entry.metadata;
if !md.is_symlink() {
panic!(
"Called archive_symlink on a non-symlink path: {}",
path.display()
);
}
let md_target = std::fs::metadata(&path);
let symlink_type = match md_target {
Err(e) if e.kind() == std::io::ErrorKind::NotFound => EntryType::SymbolicLinkFile,
Err(e) => return Err(e),
Ok(md_target) => {
match (
md_target.is_file(),
md_target.is_dir(),
md_target.is_symlink(),
) {
(true, false, false) => EntryType::SymbolicLinkFile,
(false, true, false) => EntryType::SymbolicLinkDir,
_ => panic!("Invalid symlink target type"),
}
}
};
let mod_time = FileTime::from_last_modification_time(md);
ar_entry
.metadata
.entry_and_os_type
.set_entry_type(symlink_type);
ar_entry.metadata.modified_unix_seconds = mod_time.unix_seconds();
ar_entry.metadata.modified_nanos = mod_time.nanoseconds();
ar_entry.path = path.to_string_lossy().to_string();
let symlink_target = std::fs::read_link(&path)?.to_string_lossy().to_string();
ar_entry.symlink_target = Some(symlink_target);
self.codec.file_table.entries.push(ar_entry);
Ok(())
}
pub fn archive_file(&mut self, entry: &FsEntry) -> Result<(), std::io::Error> {
let path = &entry.path;
let s_path = path.to_string_lossy().to_string();
if let Some(ref_ft) = &self.reference_file_table {
if let Some(ref_entry) = ref_ft
.entries
.iter()
.find(|it| it.path == entry.path.to_string_lossy())
{
let mod_time = FileTime::from_last_modification_time(&entry.metadata);
if ref_entry.metadata.modified_unix_seconds == mod_time.unix_seconds()
&& ref_entry.metadata.modified_nanos == mod_time.nanoseconds()
&& ref_entry.metadata.file_size == entry.metadata.len()
{
debug!(
"Detected same mtime + len, copying from ref-entry: {}",
s_path
);
self.codec.file_table.entries.push(ref_entry.clone());
return Ok(());
}
}
}
trace!("Archiving file: {s_path}");
let mut ar_entry = ArEntry::default();
// let md = std::fs::metadata(path.as_ref())?;
let md = &entry.metadata;
if !md.is_file() {
panic!("Called archive_file on a non-file path");
}
let mod_time = FileTime::from_last_modification_time(md);
ar_entry
.metadata
.entry_and_os_type
.set_entry_type(EntryType::File);
ar_entry.metadata.modified_unix_seconds = mod_time.unix_seconds();
ar_entry.metadata.modified_nanos = mod_time.nanoseconds();
ar_entry.metadata.file_size = md.len();
ar_entry.path = s_path;
let file = File::open(path)?;
let file = BufReader::new(file);
let mut file_hash = [0_u8; 32];
let mut file_hasher = blake3::Hasher::new();
let mut chunker = FixedSizeChunker::new(file, BLOCK_SIZE);
loop {
let bytes_read = match chunker.get_chunk(&mut self.codec.buffer)? {
(0, _) => break,
(n, _) => n as usize,
};
let block = &self.codec.buffer[..bytes_read];
let hash = blake3::hash(block);
let hash = hash.as_bytes();
// Update the full file hash
file_hasher.update(block);
if let Some(block_offset) = self.codec.index_table.blocks.get(hash) {
ar_entry.blocks.push(*block_offset);
continue;
}
let mut encode_buf = Vec::with_capacity(self.codec.buffer.len());
let mut encoder = zstd::Encoder::new(&mut encode_buf, 2).unwrap();
encoder.include_checksum(false)?;
encoder.include_dictid(false)?;
encoder.include_contentsize(false)?;
encoder.write_all(block)?;
encoder.finish()?;
self.blocks_out.write_all_u32(encode_buf.len() as u32)?;
self.blocks_out.write_all_u8(CompressionType::Zstd as u8)?;
self.blocks_out.write_all(&encode_buf)?;
let block_start = self.blocks_out.next_block_offset;
ar_entry.blocks.push(block_start);
self.codec.index_table.blocks.insert(*hash, block_start);
// + 4 to skip the 32-bit blocklength, + 1 to skip the compression type
self.blocks_out.next_block_offset += encode_buf.len() as u64 + 4 + 1;
}
file_hash.copy_from_slice(file_hasher.finalize().as_bytes());
ar_entry.metadata.file_hash = file_hash;
self.codec.file_table.entries.push(ar_entry);
Ok(())
}
}