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
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
use super::sync_backend::{SyncBackend, SyncIndex, SyncManifest};
use crate::repository::backend::{
BackendError, Chunk, ChunkID, ChunkSettings, EncryptedKey, Result, SegmentDescriptor,
StoredArchive,
};
use crate::repository::Key;
use asuran_core::repository::backend::flatfile::{
EntryFooter, EntryFooterData, EntryHeader, FlatFileHeader,
};
use asuran_core::repository::chunk::{ChunkBody, ChunkHeader};
use chrono::{DateTime, FixedOffset};
use semver::Version;
use uuid::Uuid;
use std::collections::{HashMap, HashSet};
use std::convert::TryInto;
use std::fmt::Debug;
use std::io::{Read, Seek, SeekFrom, Write};
use std::path::{Path, PathBuf};
pub use asuran_core::repository::backend::flatfile::MAGIC_NUMBER;
pub struct GenericFlatFile<F: Read + Write + Seek + 'static> {
file: F,
path: PathBuf,
chunk_settings: ChunkSettings,
index: HashMap<ChunkID, SegmentDescriptor>,
length_map: HashMap<SegmentDescriptor, u64>,
manifest: Vec<StoredArchive>,
entry_footer_data: EntryFooterData,
chunk_settings_modified: bool,
enc_key: EncryptedKey,
key: Key,
chunk_headers: HashMap<SegmentDescriptor, ChunkHeader>,
header_offset: u64,
seen_versions: HashSet<(Version, Uuid)>,
}
impl<F: Read + Write + Seek + 'static> Debug for GenericFlatFile<F> {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
f.debug_struct("GenericFlatFile")
.field("file_type", &std::any::type_name::<F>())
.field("path", &self.path)
.finish()
}
}
impl<F: Read + Write + Seek + 'static> GenericFlatFile<F> {
#[allow(clippy::too_many_lines)]
pub fn new_raw(
mut file: F,
path: impl AsRef<Path>,
settings: Option<ChunkSettings>,
key: Key,
enc_key: Option<EncryptedKey>,
) -> Result<GenericFlatFile<F>> {
let mut seen_versions = HashSet::new();
let current_version = semver::Version::new(
crate::VERSION_STRUCT.major,
crate::VERSION_STRUCT.minor,
crate::VERSION_STRUCT.patch,
);
seen_versions.insert((current_version, *crate::IMPLEMENTATION_UUID));
let file_length = file.seek(SeekFrom::End(0))?;
if file_length == 0 {
let settings = settings.ok_or_else(|| {
BackendError::ManifestError(
"Attempted to create a FlatFile without supplying chunk settings".to_string(),
)
})?;
let enc_key = enc_key.ok_or_else(|| {
BackendError::ManifestError(
"Attempted to create a FlatFile without supplying an encrypted key".to_string(),
)
})?;
let header = FlatFileHeader::new(&enc_key)?;
header.to_write(&mut file)?;
let header =
EntryHeader::new(&*crate::VERSION_STRUCT, 0, 0, *crate::IMPLEMENTATION_UUID)?;
let header_location = file.seek(SeekFrom::End(0))?;
header.to_write(&mut file)?;
let flat_file = GenericFlatFile {
file,
path: path.as_ref().to_owned(),
chunk_settings: settings,
index: HashMap::new(),
length_map: HashMap::new(),
manifest: Vec::new(),
entry_footer_data: EntryFooterData::new(settings),
chunk_settings_modified: true,
enc_key,
key,
chunk_headers: HashMap::new(),
header_offset: header_location,
seen_versions,
};
Ok(flat_file)
} else {
let path: PathBuf = path.as_ref().to_owned();
file.seek(SeekFrom::Start(0))?;
let global_header = FlatFileHeader::from_read(&mut file)?;
if enc_key.is_some() {
return Err(BackendError::ManifestError(
"Attempted to set a key on an already existing flatfile repository".to_string(),
));
}
let enc_key = global_header.key()?;
let mut header_offset = file.seek(SeekFrom::Current(0))?;
let mut entry_header = EntryHeader::from_read(&mut file)?;
let mut chunk_settings: Option<ChunkSettings> = None;
let mut index = HashMap::new();
let mut length_map = HashMap::new();
let mut manifest = Vec::new();
let mut chunk_headers = HashMap::new();
while entry_header.footer_offset != 0 && entry_header.next_header_offset != 0 {
seen_versions.insert((entry_header.version(), entry_header.uuid()));
file.seek(SeekFrom::Start(entry_header.footer_offset))?;
let footer = EntryFooter::from_read(&mut file)?.into_data(&key)?;
chunk_settings = Some(footer.chunk_settings);
for (id, start, length) in footer.chunk_locations {
let descriptor = SegmentDescriptor {
segment_id: 0,
start,
};
index.insert(id, descriptor);
length_map.insert(descriptor, length);
let header = footer
.chunk_headers
.get(&id)
.ok_or_else(|| {
BackendError::IndexError(format!(
"Chunk with id {:?} did not have an associated header.",
id
))
})?
.clone();
chunk_headers.insert(descriptor, header);
}
for (id, timestamp) in footer.archives {
manifest.push(StoredArchive { id, timestamp });
}
header_offset = file.seek(SeekFrom::Start(entry_header.next_header_offset))?;
entry_header = EntryHeader::from_read(&mut file)?;
}
let chunk_settings = chunk_settings.ok_or_else(|| {
BackendError::ManifestError(format!(
"FlatFile repository at {:?} did not contain any valid entries",
path
))
})?;
let flat_file = GenericFlatFile {
file,
path,
chunk_settings,
index,
length_map,
manifest,
entry_footer_data: EntryFooterData::new(chunk_settings),
chunk_settings_modified: false,
enc_key,
key,
chunk_headers,
header_offset,
seen_versions,
};
Ok(flat_file)
}
}
pub fn load_encrypted_key(mut file: F) -> Result<EncryptedKey> {
file.seek(SeekFrom::Start(0))?;
let header = FlatFileHeader::from_read(&mut file)?;
Ok(header.key()?)
}
}
impl<F: Read + Write + Seek + 'static> SyncManifest for GenericFlatFile<F> {
type Iterator = std::vec::IntoIter<StoredArchive>;
fn last_modification(&mut self) -> Result<DateTime<FixedOffset>> {
if self.manifest.is_empty() {
Err(BackendError::ManifestError(
"No archives/timestamps present".to_string(),
))
} else {
let archive = &self.manifest[self.manifest.len() - 1];
Ok(archive.timestamp())
}
}
fn chunk_settings(&mut self) -> ChunkSettings {
self.chunk_settings
}
fn write_chunk_settings(&mut self, settings: ChunkSettings) -> Result<()> {
self.chunk_settings = settings;
self.entry_footer_data.chunk_settings = settings;
self.chunk_settings_modified = true;
Ok(())
}
fn archive_iterator(&mut self) -> Self::Iterator {
self.manifest.clone().into_iter()
}
fn write_archive(&mut self, archive: StoredArchive) -> Result<()> {
self.entry_footer_data
.add_archive(archive.id, archive.timestamp);
self.manifest.push(archive);
Ok(())
}
fn touch(&mut self) -> Result<()> {
Ok(())
}
fn seen_versions(&mut self) -> HashSet<(Version, Uuid)> {
self.seen_versions.clone()
}
}
impl<F: Read + Write + Seek + 'static> SyncIndex for GenericFlatFile<F> {
fn lookup_chunk(&mut self, id: ChunkID) -> Option<SegmentDescriptor> {
self.index.get(&id).copied()
}
fn set_chunk(&mut self, id: ChunkID, location: SegmentDescriptor) -> Result<()> {
let length = self.length_map.get(&location).ok_or_else(|| {
BackendError::IndexError(format!(
"Attempted to add chunk with id {:?} to the index, whose length was not known",
id
))
})?;
self.index.insert(id, location);
let location = location.start;
self.entry_footer_data.add_chunk(id, location, *length);
Ok(())
}
fn known_chunks(&mut self) -> HashSet<ChunkID> {
self.index.keys().copied().collect()
}
fn commit_index(&mut self) -> Result<()> {
if self.chunk_settings_modified || self.entry_footer_data.dirty() {
self.chunk_settings_modified = false;
let mut footer = EntryFooterData::new(self.chunk_settings);
std::mem::swap(&mut self.entry_footer_data, &mut footer);
let footer = EntryFooter::from_data(&footer, &self.key, self.chunk_settings);
let file = &mut self.file;
let footer_location = file.seek(SeekFrom::End(0))?;
footer.to_write(Write::by_ref(file))?;
let header_location = file.seek(SeekFrom::End(0))?;
EntryHeader::new(&*crate::VERSION_STRUCT, 0, 0, *crate::IMPLEMENTATION_UUID)?
.to_write(Write::by_ref(file))?;
file.seek(SeekFrom::Start(self.header_offset))?;
EntryHeader::new(
&*crate::VERSION_STRUCT,
footer_location,
header_location,
*crate::IMPLEMENTATION_UUID,
)?
.to_write(Write::by_ref(file))?;
self.header_offset = header_location;
Ok(())
} else {
Ok(())
}
}
fn chunk_count(&mut self) -> usize {
self.index.len()
}
}
impl<F: Read + Write + Seek + 'static> SyncBackend for GenericFlatFile<F> {
type SyncManifest = Self;
type SyncIndex = Self;
fn get_index(&mut self) -> &mut Self::SyncIndex {
self
}
fn get_manifest(&mut self) -> &mut Self::SyncManifest {
self
}
fn write_key(&mut self, _key: EncryptedKey) -> Result<()> {
Err(BackendError::Unknown(
"Changing the key of a FlatFile repository is not supported at this time.".to_string(),
))
}
fn read_key(&mut self) -> Result<EncryptedKey> {
Ok(self.enc_key.clone())
}
fn read_chunk(&mut self, location: SegmentDescriptor) -> Result<Chunk> {
let start = location.start;
let length = *self.length_map.get(&location).ok_or_else(|| {
BackendError::SegmentError(format!(
"Attempted to look up chunk with location {:?}, but its length was not known",
location
))
})?;
let file = &mut self.file;
file.seek(SeekFrom::Start(start))?;
let buffer_len: usize = length
.try_into()
.expect("Attempted to read a chunk that could not possibly fit into memory");
let mut buffer = vec![0_u8; buffer_len];
file.read_exact(&mut buffer[..])?;
let header = self
.chunk_headers
.get(&location)
.ok_or_else(|| {
BackendError::SegmentError(format!(
"Attempted to look up chunk with location {:?},\
but there was no associated chunk header",
location
))
})?
.clone();
let chunk = Chunk::unsplit(header, ChunkBody(buffer));
Ok(chunk)
}
fn write_chunk(&mut self, chunk: Chunk) -> Result<SegmentDescriptor> {
let id = chunk.get_id();
let file = &mut self.file;
let location = file.seek(SeekFrom::End(0))?;
let (header, body) = chunk.split();
let length = body.0.len() as u64;
let descriptor = SegmentDescriptor {
segment_id: 0,
start: location,
};
self.length_map.insert(descriptor, length);
self.entry_footer_data.add_chunk(id, location, length);
self.entry_footer_data.add_header(id, header.clone());
self.chunk_headers.insert(descriptor, header);
file.write_all(&body.0[..])?;
Ok(descriptor)
}
}
impl<T: Read + Write + Seek + 'static> Drop for GenericFlatFile<T> {
fn drop(&mut self) {
let res = self.commit_index();
if res.is_err() && !std::thread::panicking() {
panic!(
"Failed to commit index during drop. Path was {:?}",
self.path
)
}
}
}