2020-04-17 12:11:25 +00:00
|
|
|
use anyhow::{bail, Error};
|
2019-03-16 13:22:14 +00:00
|
|
|
|
|
|
|
use std::thread;
|
|
|
|
use std::path::PathBuf;
|
|
|
|
use std::io::Write;
|
|
|
|
|
|
|
|
// tar handle files that shrink during backup, by simply padding with zeros.
|
|
|
|
//
|
|
|
|
// this binary run multiple thread which writes some large files, then truncates
|
|
|
|
// them in a loop.
|
|
|
|
|
|
|
|
// # tar cf test.tar ./dyntest1/
|
|
|
|
// tar: dyntest1/testfile0.dat: File shrank by 2768972800 bytes; padding with zeros
|
|
|
|
// tar: dyntest1/testfile17.dat: File shrank by 2899853312 bytes; padding with zeros
|
|
|
|
// tar: dyntest1/testfile2.dat: File shrank by 3093422080 bytes; padding with zeros
|
|
|
|
// tar: dyntest1/testfile7.dat: File shrank by 2833252864 bytes; padding with zeros
|
|
|
|
|
|
|
|
// # pxar create test.pxar ./dyntest1/
|
2020-05-30 14:37:33 +00:00
|
|
|
// Error: detected shrunk file "./dyntest1/testfile0.dat" (22020096 < 12679380992)
|
2019-03-16 13:22:14 +00:00
|
|
|
|
|
|
|
fn create_large_file(path: PathBuf) {
|
|
|
|
|
|
|
|
println!("TEST {:?}", path);
|
|
|
|
|
|
|
|
let mut file = std::fs::OpenOptions::new()
|
|
|
|
.write(true)
|
|
|
|
.create_new(true)
|
|
|
|
.open(&path).unwrap();
|
|
|
|
|
|
|
|
let buffer = vec![0u8; 64*1024];
|
|
|
|
|
|
|
|
loop {
|
|
|
|
for _ in 0..64 {
|
|
|
|
file.write_all(&buffer).unwrap();
|
|
|
|
}
|
|
|
|
file.sync_all().unwrap();
|
|
|
|
//println!("TRUNCATE {:?}", path);
|
|
|
|
file.set_len(0).unwrap();
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fn main() -> Result<(), Error> {
|
|
|
|
|
|
|
|
let base = PathBuf::from("dyntest1");
|
|
|
|
let _ = std::fs::create_dir(&base);
|
|
|
|
|
|
|
|
let mut handles = vec![];
|
|
|
|
for i in 0..20 {
|
|
|
|
let base = base.clone();
|
|
|
|
handles.push(thread::spawn(move || {
|
|
|
|
create_large_file(base.join(format!("testfile{}.dat", i)));
|
|
|
|
}));
|
|
|
|
}
|
|
|
|
|
|
|
|
for h in handles {
|
2019-10-25 16:04:37 +00:00
|
|
|
if h.join().is_err() {
|
2019-03-16 13:22:14 +00:00
|
|
|
bail!("join failed");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
Ok(())
|
|
|
|
}
|