]> git.proxmox.com Git - proxmox-backup.git/blame - examples/dynamic-files.rs
api: backup env: use check_privs
[proxmox-backup.git] / examples / dynamic-files.rs
CommitLineData
ba10f2b0 1use std::io::Write;
a22d3388
TL
2use std::path::PathBuf;
3use std::thread;
ba10f2b0 4
6ef1b649
WB
5use anyhow::{bail, Error};
6
ba10f2b0
DM
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/
add5861e 19// Error: detected shrunk file "./dyntest1/testfile0.dat" (22020096 < 12679380992)
ba10f2b0
DM
20
21fn create_large_file(path: PathBuf) {
ba10f2b0
DM
22 println!("TEST {:?}", path);
23
24 let mut file = std::fs::OpenOptions::new()
25 .write(true)
26 .create_new(true)
a22d3388
TL
27 .open(&path)
28 .unwrap();
ba10f2b0 29
a22d3388 30 let buffer = vec![0u8; 64 * 1024];
ba10f2b0
DM
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> {
ba10f2b0
DM
43 let base = PathBuf::from("dyntest1");
44 let _ = std::fs::create_dir(&base);
45
46 let mut handles = vec![];
47 for i in 0..20 {
48 let base = base.clone();
49 handles.push(thread::spawn(move || {
50 create_large_file(base.join(format!("testfile{}.dat", i)));
51 }));
52 }
53
54 for h in handles {
11377a47 55 if h.join().is_err() {
ba10f2b0
DM
56 bail!("join failed");
57 }
58 }
59
60 Ok(())
61}