rdedup-lib 3.2.0

Data deduplication with compression and public key encryption. - library
Documentation
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
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
//! Primitives used for reading the chunked data stored in the `Repo`
// {{{ use and mod
use std::cell::RefCell;
use std::collections::HashSet;
use std::io;
use std::io::Write;

use slog::{trace, warn, FnValue, Logger};

use crate::Generation;
use crate::VerifyResults;
use crate::{ArcCompression, ArcDecrypter};
use crate::{
    DataAddressRef, DataType, Digest, DigestRef, Error, Repo, DIGEST_SIZE,
};
// }}}

/// Translates index stream into data stream
///
/// This type implements `io::Write` and interprets what's written to it as a
/// stream of digests.
///
/// For every digest written to it, it will access the corresponding chunk and
/// write it into `writer` that it wraps.
struct IndexTranslator<'a, 'b> {
    writer: Option<&'b mut dyn Write>,
    digest_buf: Digest,
    data_type: DataType,
    read_context: &'a ReadContext<'a>,
    log: Logger,
}

impl<'a, 'b> IndexTranslator<'a, 'b> {
    pub(crate) fn new(
        writer: Option<&'b mut dyn Write>,
        data_type: DataType,
        read_context: &'a ReadContext<'a>,
        log: Logger,
    ) -> Self {
        IndexTranslator {
            data_type,
            digest_buf: Digest(Vec::with_capacity(DIGEST_SIZE)),
            read_context,
            writer,
            log,
        }
    }
}

impl<'a, 'b> Write for IndexTranslator<'a, 'b> {
    fn write(&mut self, mut bytes: &[u8]) -> io::Result<usize> {
        assert!(!bytes.is_empty());

        let total_len = bytes.len();
        loop {
            let has_already = self.digest_buf.0.len();
            if (has_already + bytes.len()) < DIGEST_SIZE {
                self.digest_buf.0.extend_from_slice(bytes);

                trace!(
                    self.log,
                    "left with a buffer";
                    "digest" => FnValue(|_| hex::encode(&self.digest_buf.0)),
                );
                return Ok(total_len);
            }

            let &mut IndexTranslator {
                ref mut digest_buf,
                data_type,
                ref mut writer,
                read_context,
                ..
            } = self;
            let needs = DIGEST_SIZE - has_already;

            if digest_buf.0.is_empty() {
                let digest = &bytes[..needs];
                debug_assert_eq!(digest.len(), DIGEST_SIZE);
                bytes = &bytes[needs..];

                read_context.read_recursively(ReadRequest::new(
                    data_type,
                    DataAddressRef {
                        digest: DigestRef(digest),
                        index_level: 0,
                    },
                    writer.as_mut().map(|w| w as &mut dyn io::Write),
                    self.log.clone(),
                ))?;
            } else {
                digest_buf.0.extend_from_slice(&bytes[..needs]);
                debug_assert_eq!(digest_buf.0.len(), DIGEST_SIZE);
                bytes = &bytes[needs..];

                let res = read_context.read_recursively(ReadRequest::new(
                    data_type,
                    DataAddressRef {
                        digest: digest_buf.as_digest_ref(),
                        index_level: 0,
                    },
                    writer.as_mut().map(|w| w as &mut dyn io::Write),
                    self.log.clone(),
                ));
                digest_buf.0.clear();
                res?;
            }
        }
    }

    fn flush(&mut self) -> io::Result<()> {
        Ok(())
    }
}

impl<'a, 'b> Drop for IndexTranslator<'a, 'b> {
    fn drop(&mut self) {
        if !std::thread::panicking() {
            debug_assert_eq!(self.digest_buf.0.len(), 0);
        }
    }
}

/// Information specific to a given read operation
/// of a data in the Repo
pub(crate) struct ReadRequest<'a> {
    data_address: DataAddressRef<'a>,
    data_type: DataType,
    writer: Option<&'a mut dyn Write>,
    log: Logger,
}

impl<'a> ReadRequest<'a> {
    pub(crate) fn new(
        data_type: DataType,
        data_address: DataAddressRef<'a>,
        writer: Option<&'a mut dyn Write>,
        log: Logger,
    ) -> Self {
        ReadRequest {
            data_address,
            data_type,
            writer,
            log,
        }
    }
}

/// Read Context
///
/// Information about the `Repo` that is open for reaading
pub(crate) struct ReadContext<'a> {
    /// Writer to write the data to; `None` will discard the data
    accessor: &'a dyn ChunkAccessor,
}

impl<'a> ReadContext<'a> {
    pub(crate) fn new(accessor: &'a dyn ChunkAccessor) -> Self {
        ReadContext { accessor }
    }

    fn on_index(&self, mut req: ReadRequest<'_>) -> io::Result<()> {
        trace!(
            req.log,
            "Traversing index";
            "digest" => FnValue(|_| hex::encode(req.data_address.digest.0)),
        );

        let mut translator = IndexTranslator::new(
            req.writer.take(),
            req.data_type,
            self,
            req.log.clone(),
        );

        let da = DataAddressRef {
            digest: req.data_address.digest,
            index_level: req.data_address.index_level - 1,
        };
        let req = ReadRequest::new(
            DataType::Index,
            da,
            Some(&mut translator),
            req.log,
        );
        self.read_recursively(req)
    }

    fn on_data(&self, mut req: ReadRequest<'_>) -> io::Result<()> {
        trace!(
            req.log,
            "Traversing data";
            "digest" => FnValue(|_| hex::encode(req.data_address.digest.0)),
        );
        if let Some(writer) = req.writer.take() {
            self.accessor.read_chunk_into(
                req.data_address.digest,
                req.data_type,
                writer,
            )
        } else {
            self.accessor.touch(req.data_address.digest)
        }
    }

    pub(crate) fn read_recursively(
        &self,
        req: ReadRequest<'_>,
    ) -> io::Result<()> {
        trace!(
            req.log,
            "Reading recursively";
            "digest" => FnValue(|_| hex::encode(req.data_address.digest.0)),
        );

        if req.data_address.index_level == 0 {
            self.on_data(req)
        } else {
            self.on_index(req)
        }
    }
}

/// Abstraction over accessing chunks stored in the repository
pub(crate) trait ChunkAccessor {
    fn repo(&self) -> &Repo;

    /// Read a chunk identified by `digest` into `writer`
    fn read_chunk_into(
        &self,
        digest: DigestRef<'_>,
        data_type: DataType,
        writer: &mut dyn Write,
    ) -> io::Result<()>;

    fn touch(&self, _digest: DigestRef<'_>) -> io::Result<()>;
}

/// `ChunkAccessor` that just reads the chunks as requested, without doing
/// anything
pub(crate) struct DefaultChunkAccessor<'a> {
    repo: &'a Repo,
    decrypter: Option<ArcDecrypter>,
    compression: ArcCompression,
    gen_strings: Vec<String>,
}

impl<'a> DefaultChunkAccessor<'a> {
    pub(crate) fn new(
        repo: &'a Repo,
        decrypter: Option<ArcDecrypter>,
        compression: ArcCompression,
        generations: Vec<Generation>,
    ) -> Self {
        DefaultChunkAccessor {
            repo,
            decrypter,
            compression,
            gen_strings: generations.iter().map(|g| g.to_string()).collect(),
        }
    }
}

impl<'a> ChunkAccessor for DefaultChunkAccessor<'a> {
    fn repo(&self) -> &Repo {
        self.repo
    }

    fn read_chunk_into(
        &self,
        digest: DigestRef<'_>,
        data_type: DataType,
        writer: &mut dyn Write,
    ) -> io::Result<()> {
        let mut data = None;
        let cur_gen_str = self.gen_strings.last().unwrap();
        let mut data_gen_str = None;

        for gen_str in self.gen_strings.iter().rev() {
            let path = self.repo.chunk_rel_path_by_digest(digest, gen_str);
            match self.repo.aio.read(path).wait() {
                Ok(d) => {
                    data = Some(d);
                    data_gen_str = Some(gen_str);
                    break;
                }
                Err(_e) => {}
            }
        }

        if data.is_none() {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                format!("Couldn't not find chunk: {}", hex::encode(digest.0),),
            ));
        }

        let data_gen_str = data_gen_str.unwrap();

        if cur_gen_str != data_gen_str {
            let data_gen_path =
                self.repo.chunk_rel_path_by_digest(digest, data_gen_str);
            let cur_gen_path =
                self.repo.chunk_rel_path_by_digest(digest, cur_gen_str);

            // `rename` is best effort
            //
            // Should we fail if we're GCing, and we want to make sure
            // everything reachable has been moved? Well, if it wa
            let res = self
                .repo
                .aio
                .rename(data_gen_path.clone(), cur_gen_path.clone())
                .wait();
            if let Err(e) = res {
                if e.kind() != io::ErrorKind::NotFound {
                    warn!(self.repo.log, "Couldn't move chunk to the current generation";
                          "src-path" => data_gen_path.display(),
                          "dst-path" => cur_gen_path.display(),
                          "err" => %e);
                    return Err(e);
                }
            }
        }

        let data = data.unwrap();
        let data = if data_type.should_encrypt() {
            self.decrypter
                .as_ref()
                .expect("Decrypter expected")
                .decrypt(data, digest.0)?
        } else {
            data
        };

        let data = if data_type.should_compress() {
            self.compression.decompress(data)?
        } else {
            data
        };

        let vec_result = self.repo.hasher.calculate_digest(&data);

        if vec_result != digest.0 {
            Err(io::Error::new(
                io::ErrorKind::InvalidData,
                format!(
                    "{} corrupted, data read: {}",
                    hex::encode(digest.0),
                    hex::encode(vec_result)
                ),
            ))
        } else {
            for part in data.as_parts() {
                writer.write_all(&*part)?;
            }
            Ok(())
        }
    }

    fn touch(&self, _digest: DigestRef<'_>) -> io::Result<()> {
        Ok(())
    }
}

/// `ChunkAccessor` that records which chunks
/// were accessed
///
/// This is useful for chunk garbage-collection
pub(crate) struct RecordingChunkAccessor<'a> {
    raw: DefaultChunkAccessor<'a>,
    accessed: RefCell<&'a mut HashSet<Vec<u8>>>,
}

impl<'a> RecordingChunkAccessor<'a> {
    pub(crate) fn new(
        repo: &'a Repo,
        accessed: &'a mut HashSet<Vec<u8>>,
        decrypter: Option<ArcDecrypter>,
        compression: ArcCompression,
        generations: Vec<Generation>,
    ) -> Self {
        RecordingChunkAccessor {
            raw: DefaultChunkAccessor::new(
                repo,
                decrypter,
                compression,
                generations,
            ),
            accessed: RefCell::new(accessed),
        }
    }
}

impl<'a> ChunkAccessor for RecordingChunkAccessor<'a> {
    fn repo(&self) -> &Repo {
        self.raw.repo()
    }

    fn read_chunk_into(
        &self,
        digest: DigestRef<'_>,
        data_type: DataType,
        writer: &mut dyn Write,
    ) -> io::Result<()> {
        self.touch(digest)?;
        self.raw.read_chunk_into(digest, data_type, writer)
    }

    fn touch(&self, digest: DigestRef<'_>) -> io::Result<()> {
        self.accessed.borrow_mut().insert(digest.0.into());
        Ok(())
    }
}

/// `ChunkAccessor` that verifies the chunks
/// that are accessed
///
/// This is used to verify a name / index
pub(crate) struct VerifyingChunkAccessor<'a> {
    raw: DefaultChunkAccessor<'a>,
    accessed: RefCell<HashSet<Vec<u8>>>,
    errors: RefCell<Vec<(Vec<u8>, Error)>>,
}

impl<'a> VerifyingChunkAccessor<'a> {
    pub(crate) fn new(
        repo: &'a Repo,
        decrypter: Option<ArcDecrypter>,
        compression: ArcCompression,
        generations: Vec<Generation>,
    ) -> Self {
        VerifyingChunkAccessor {
            raw: DefaultChunkAccessor::new(
                repo,
                decrypter,
                compression,
                generations,
            ),
            accessed: RefCell::new(HashSet::new()),
            errors: RefCell::new(Vec::new()),
        }
    }

    pub(crate) fn get_results(self) -> VerifyResults {
        VerifyResults {
            scanned: self.accessed.borrow().len(),
            errors: self.errors.into_inner(),
        }
    }
}

impl<'a> ChunkAccessor for VerifyingChunkAccessor<'a> {
    fn repo(&self) -> &Repo {
        self.raw.repo()
    }

    fn read_chunk_into(
        &self,
        digest: DigestRef<'_>,
        data_type: DataType,
        writer: &mut dyn Write,
    ) -> io::Result<()> {
        {
            let mut accessed = self.accessed.borrow_mut();
            if accessed.contains(digest.0) {
                return Ok(());
            }
            accessed.insert(digest.0.into());
        }
        let res = self.raw.read_chunk_into(digest, data_type, writer);

        if res.is_err() {
            self.errors
                .borrow_mut()
                .push((digest.0.into(), res.err().unwrap()));
        }
        Ok(())
    }

    fn touch(&self, digest: DigestRef<'_>) -> io::Result<()> {
        self.raw.touch(digest)
    }
}

/// `ChunkAccessor` that update accessed chunks
/// to the latest generation
pub(crate) struct GenerationUpdateChunkAccessor<'a> {
    raw: DefaultChunkAccessor<'a>,
}

impl<'a> GenerationUpdateChunkAccessor<'a> {
    pub(crate) fn new(
        repo: &'a Repo,
        compression: ArcCompression,
        generations: Vec<Generation>,
    ) -> Self {
        GenerationUpdateChunkAccessor {
            raw: DefaultChunkAccessor::new(
                repo,
                None,
                compression,
                generations,
            ),
        }
    }
}

impl<'a> ChunkAccessor for GenerationUpdateChunkAccessor<'a> {
    fn repo(&self) -> &Repo {
        self.raw.repo()
    }

    fn read_chunk_into(
        &self,
        digest: DigestRef<'_>,
        data_type: DataType,
        writer: &mut dyn Write,
    ) -> io::Result<()> {
        self.raw.read_chunk_into(digest, data_type, writer)
    }

    fn touch(&self, digest: DigestRef<'_>) -> io::Result<()> {
        let cur_gen_str = self.raw.gen_strings.last().unwrap();
        let mut data_gen_str = None;

        for gen_str in self.raw.gen_strings.iter().rev() {
            let path = self.raw.repo.chunk_rel_path_by_digest(digest, gen_str);
            match self.raw.repo.aio.read_metadata(path).wait() {
                Ok(_metadata) => {
                    data_gen_str = Some(gen_str);
                    break;
                }
                Err(_e) => {}
            }
        }

        if data_gen_str.is_none() {
            return Err(io::Error::new(
                io::ErrorKind::NotFound,
                format!("Couldn't not find chunk: {}", hex::encode(digest.0),),
            ));
        }

        let data_gen_str = data_gen_str.unwrap();

        if cur_gen_str != data_gen_str {
            let data_gen_path =
                self.raw.repo.chunk_rel_path_by_digest(digest, data_gen_str);
            let cur_gen_path =
                self.raw.repo.chunk_rel_path_by_digest(digest, cur_gen_str);

            // `rename` is best effort
            let res = self
                .raw
                .repo
                .aio
                .rename(data_gen_path.clone(), cur_gen_path.clone())
                .wait();
            if let Err(e) = res {
                if e.kind() != io::ErrorKind::NotFound {
                    warn!(self.raw.repo.log, "Couldn't move chunk to the current generation";
                          "src-path" => data_gen_path.display(),
                          "dst-path" => cur_gen_path.display(),
                          "err" => %e);
                    return Err(e);
                }
            }
        }
        Ok(())
    }
}
// vim: foldmethod=marker foldmarker={{{,}}}