]> git.proxmox.com Git - proxmox-backup.git/blame - src/bin/dynamic-files.rs
switch from failure to anyhow
[proxmox-backup.git] / src / bin / dynamic-files.rs
CommitLineData
f7d4e4b5 1use anyhow::{bail, Error};
ba10f2b0
DM
2
3use std::thread;
4use std::path::PathBuf;
5use std::io::Write;
6
7// tar handle files that shrink during backup, by simply padding with zeros.
8//
9// this binary run multiple thread which writes some large files, then truncates
10// them in a loop.
11
12// # tar cf test.tar ./dyntest1/
13// tar: dyntest1/testfile0.dat: File shrank by 2768972800 bytes; padding with zeros
14// tar: dyntest1/testfile17.dat: File shrank by 2899853312 bytes; padding with zeros
15// tar: dyntest1/testfile2.dat: File shrank by 3093422080 bytes; padding with zeros
16// tar: dyntest1/testfile7.dat: File shrank by 2833252864 bytes; padding with zeros
17
18// # pxar create test.pxar ./dyntest1/
19// Error: detected shrinked file "./dyntest1/testfile0.dat" (22020096 < 12679380992)
20
21fn create_large_file(path: PathBuf) {
22
23 println!("TEST {:?}", path);
24
25 let mut file = std::fs::OpenOptions::new()
26 .write(true)
27 .create_new(true)
28 .open(&path).unwrap();
29
30 let buffer = vec![0u8; 64*1024];
31
32 loop {
33 for _ in 0..64 {
34 file.write_all(&buffer).unwrap();
35 }
36 file.sync_all().unwrap();
37 //println!("TRUNCATE {:?}", path);
38 file.set_len(0).unwrap();
39 }
40}
41
42fn main() -> Result<(), Error> {
43
44 let base = PathBuf::from("dyntest1");
45 let _ = std::fs::create_dir(&base);
46
47 let mut handles = vec![];
48 for i in 0..20 {
49 let base = base.clone();
50 handles.push(thread::spawn(move || {
51 create_large_file(base.join(format!("testfile{}.dat", i)));
52 }));
53 }
54
55 for h in handles {
11377a47 56 if h.join().is_err() {
ba10f2b0
DM
57 bail!("join failed");
58 }
59 }
60
61 Ok(())
62}