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
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
//! # DedupeFS
//!
//! [![badge github]][url github]
//! [![badge crates.io]][url crates.io]
//! [![badge docs.rs]][url docs.rs]
//! [![badge license]][url license]
//!
//! [//]: # (@formatter:off)
//! [badge github]: https://img.shields.io/badge/github-FloGa%2Fdedupefs-green
//! [badge crates.io]: https://img.shields.io/crates/v/dedupefs
//! [badge docs.rs]: https://img.shields.io/docsrs/dedupefs
//! [badge license]: https://img.shields.io/crates/l/dedupefs
//!
//! [url github]: https://github.com/FloGa/dedupefs
//! [url crates.io]: https://crates.io/crates/dedupefs
//! [url docs.rs]: https://docs.rs/dedupefs
//! [url license]: https://github.com/FloGa/dedupefs/blob/develop/LICENSE
//! [//]: # (@formatter:on)
//!
//! > Presents files as deduplicated, content-addressed 1MB chunks with selectable hash algorithms.
//!
//! *DedupeFS* is a FUSE filesystem over my [*Crazy Deduper*][crazy-deduper github] application. It is so to speak the
//! logical successor of [*SCFS*][scfs github]. While *SCFS* presented each file as chunks, independent of each other,
//! *DedupeFS* calculates the checksum of each chunk and collects them all in one directory. That way, each unique chunk is
//! only presented once, even if it is used by multiple files.
//!
//! *DedupeFS* is mainly useful to create efficient backups and upload them to a cloud provider. The file chunks have the
//! advantage that the upload does not have to be all-or-nothing, so if your internet connection vanishes for a second, your
//! 4GB file upload will not be completely cancelled, only the currently transferred chunk upload will be aborted.
//!
//! By keeping multiple cache files around, you can easily and efficiently have incremental backups that all share the same
//! chunks.
//!
//! [//]: # (@formatter:off)
//! [crazy-deduper github]: https://github.com/FloGa/crazy-deduper
//! [scfs github]: https://github.com/FloGa/scfs
//! [//]: # (@formatter:on)
//!
//! ## Installation
//!
//! This tool can be installed easily through Cargo via `crates.io`:
//!
//! ```shell
//! cargo install --locked dedupefs
//! ```
//!
//! Please note that the `--locked` flag is necessary here to have the exact same dependencies as when the application was
//! tagged and tested. Without it, you might get more up-to-date versions of dependencies, but you have the risk of
//! undefined and unexpected behavior if the dependencies changed some functionalities. The application might even fail to
//! build if the public API of a dependency changed too much.
//!
//! Alternatively, pre-built binaries can be downloaded from the [GitHub releases][gh-releases] page.
//!
//! [gh-releases]: https://github.com/FloGa/dedupefs/releases
//!
//! ## Usage
//!
//! ```text
//! Usage: dedupefs [OPTIONS] <SOURCE> <MOUNTPOINT>
//!
//! Arguments:
//! <SOURCE>
//! Source directory
//!
//! <MOUNTPOINT>
//! Mount point
//!
//! Options:
//! --cache-file <CACHE_FILE>
//! Path to cache file
//!
//! Can be used multiple times. The files are read in reverse order, so they should be sorted with the most accurate ones in the beginning. The first given will be written.
//!
//! --hashing-algorithm <HASHING_ALGORITHM>
//! Hashing algorithm to use for chunk filenames
//!
//! [default: sha1]
//! [possible values: md5, sha1, sha256, sha512]
//!
//! -f, --foreground
//! Stay in foreground, do not daemonize into the background
//!
//! --declutter-levels <DECLUTTER_LEVELS>
//! Declutter files into this many subdirectory levels
//!
//! [default: 3]
//!
//! --reverse
//! Reverse mode, present chunks re-hydrated
//!
//! -h, --help
//! Print help (see a summary with '-h')
//!
//! -V, --version
//! Print version
//! ```
//!
//! To mount a deduped version of `source` directory to `deduped`, you can use:
//!
//! ```shell
//! dedupefs --cache-file cache.json.zst source deduped
//! ```
//!
//! If the cache file ends with `.zst`, it will be encoded (or decoded in the case of hydrating) using the ZSTD compression
//! algorithm. For any other extension, plain JSON will be used.
//!
//! To mount a re-hydrated version of `deduped` directory to `restored`, you can use:
//!
//! ```shell
//! dedupefs --reverse --cache-file cache.json.zst deduped restored
//! ```
//!
//! Before mounting, it will be checked if all chunks present in the cache file are available in the `deduped/data`
//! directory. If not, the mount will fail.
//!
//! ## Cache Files
//!
//! The cache file is necessary to keep track of all file chunks and hashes. Without the cache you would not be able to
//! restore your files.
//!
//! The cache file can be re-used, even if the source directory changed. It keeps track of the file sizes and modification
//! times and only re-hashes new or changed files. Deleted files are deleted from the cache.
//!
//! You can also use older cache files in addition to a new one:
//!
//! ```shell
//! dedupefs --cache-file cache.json.zst --cache-file cache-from-yesterday.json.zst source deduped
//! ```
//!
//! The cache files are read in reverse order in which they are given on the command line, so the content of earlier cache
//! files is preferred over later ones. Hence, you should put your most accurate cache files to the beginning. Moreover, the
//! first given cache file is the one that will be written to, it does not need to exist.
//!
//! In the given example, if `cache.json.zst` does not exist, the internal cache is pre-filled from
//! `cache-from-yesterday.json.zst` so that only new and modified files need to be re-hashed. The result is then written
//! into `cache.json.zst`.
//!
//! In the mounted deduped directory, the first cache file given on the command line will be presented with the same name
//! directly under the mountpoint. next to the data directory. When uploading your chunks, always make sure to also upload
//! this cache file, otherwise you wil not be able to properly re-hydrate your files afterward!
//!
//! ## Helper Commands
//!
//! There are several helper commands available to work with the cache files and to inspect the internal state of the
//! deduplicated data chunks:
//!
//! ### Check Cache
//!
//! ```text
//! Check if cache file is valid and all chunks exist.
//!
//! Usage: dedupefs_check_cache [OPTIONS] <SOURCE>
//!
//! Arguments:
//! <SOURCE>
//! Source directory to deduped files
//!
//! Options:
//! --cache-file <CACHE_FILE>
//! Path to cache file
//!
//! Can be used multiple times. The files are read in reverse order, so they should be sorted with the most accurate ones in the beginning. They will only be read, not written.
//!
//! --declutter-levels <DECLUTTER_LEVELS>
//! Declutter files into this many subdirectory levels
//!
//! [default: 3]
//!
//! -h, --help
//! Print help (see a summary with '-h')
//!
//! -V, --version
//! Print version
//! ```
//!
//! ### Create Cache
//!
//! ```text
//! Only create cache file without actually mounting.
//!
//! Usage: dedupefs_create_cache [OPTIONS] <SOURCE>
//!
//! Arguments:
//! <SOURCE>
//! Source directory
//!
//! Options:
//! --cache-file <CACHE_FILE>
//! Path to cache file
//!
//! Can be used multiple times. The files are read in reverse order, so they should be sorted with the most accurate ones in the beginning. The first given will be written.
//!
//! --hashing-algorithm <HASHING_ALGORITHM>
//! Hashing algorithm to use for chunk filenames
//!
//! [default: sha1]
//! [possible values: md5, sha1, sha256, sha512]
//!
//! --declutter-levels <DECLUTTER_LEVELS>
//! Declutter files into this many subdirectory levels
//!
//! [default: 3]
//!
//! -h, --help
//! Print help (see a summary with '-h')
//!
//! -V, --version
//! Print version
//! ```
//!
//! ### Delete Extra Files
//!
//! ```text
//! Delete files not present in any cache files.
//!
//! Usage: dedupefs_delete_extra_files [OPTIONS] <SOURCE>
//!
//! Arguments:
//! <SOURCE>
//! Source directory
//!
//! Options:
//! --cache-file <CACHE_FILE>
//! Path to cache file
//!
//! Can be used multiple times. The files are read in reverse order, so they should be sorted with the most accurate ones in the beginning. They will only be read, not written.
//!
//! -v
//! List deleted files
//!
//! -f
//! Force deletion
//!
//! --declutter-levels <DECLUTTER_LEVELS>
//! Declutter files into this many subdirectory levels
//!
//! [default: 3]
//!
//! -h, --help
//! Print help (see a summary with '-h')
//!
//! -V, --version
//! Print version
//! ```
//!
//! ### List Extra Files
//!
//! ```text
//! List files not present in any cache files.
//!
//! Usage: dedupefs_list_extra_files [OPTIONS] <SOURCE>
//!
//! Arguments:
//! <SOURCE>
//! Source directory
//!
//! Options:
//! --cache-file <CACHE_FILE>
//! Path to cache file
//!
//! Can be used multiple times. The files are read in reverse order, so they should be sorted with the most accurate ones in the beginning. They will only be read, not written.
//!
//! -0
//! Separate file names with null character instead of newline
//!
//! --declutter-levels <DECLUTTER_LEVELS>
//! Declutter files into this many subdirectory levels
//!
//! [default: 3]
//!
//! -h, --help
//! Print help (see a summary with '-h')
//!
//! -V, --version
//! Print version
//! ```
//!
//! ### List Missing Chunks
//!
//! ```text
//! List chunks from cache files that are not present in the source directory.
//!
//! Usage: dedupefs_list_missing_chunks [OPTIONS] <SOURCE>
//!
//! Arguments:
//! <SOURCE>
//! Source directory
//!
//! Options:
//! --cache-file <CACHE_FILE>
//! Path to cache file
//!
//! Can be used multiple times. The files are read in reverse order, so they should be sorted with the most accurate ones in the beginning. They will only be read, not written.
//!
//! --with-reason
//! Also display the reason for the missing or invalid chunk
//!
//! -0
//! Separate file names with null character instead of newline
//!
//! -h, --help
//! Print help (see a summary with '-h')
//!
//! -V, --version
//! Print version
//! ```
//!
//! ## TODO
//!
//! - Make chunk size configurable (via *Crazy Deduper*, fixed to 1MB at the moment).
//! - Provide better documentation with examples and use case descriptions.
use HashMap;
use ;
use Metadata;
use MetadataExt;
use Path;
use ;
use ;
use ;
use ;
use ;
pub use DedupeFS;
pub use DedupeReverseFS;
const TTL: Duration = from_secs;
const INO_ROOT: u64 = 1;
const INO_CACHE: u64 = 2;
const ATTRS_DEFAULT: FileAttr = FileAttr ;
type DropHookFn = ;