MCPcopy Create free account
hub / github.com/Freaky/Compactor / handle_file

Function handle_file

src/compression.rs:36–63  ·  view source on GitHub ↗
(file: &PathBuf, compression: Option<Compression>)

Source from the content-addressed store, hash-verified

34}
35
36fn handle_file(file: &PathBuf, compression: Option<Compression>) -> io::Result<bool> {
37 let est = Compresstimator::with_block_size(8192);
38 let meta = std::fs::metadata(&file)?;
39 let handle = std::fs::OpenOptions::new()
40 .access_mode(FILE_WRITE_ATTRIBUTES | FILE_READ_DATA)
41 .open(&file)?;
42
43 handle.try_lock_exclusive()?;
44
45 let ret = match compression {
46 Some(compression) => match est.compresstimate(&handle, meta.len()) {
47 Ok(ratio) if ratio < 0.95 => compact::compress_file_handle(&handle, compression),
48 Ok(_) => Ok(false),
49 Err(e) => Err(e),
50 },
51 None => compact::uncompress_file_handle(&handle).map(|_| true),
52 };
53
54 let _ = filetime::set_file_handle_times(
55 &handle,
56 Some(FileTime::from_last_access_time(&meta)),
57 Some(FileTime::from_last_modification_time(&meta)),
58 );
59
60 handle.unlock()?;
61
62 ret
63}
64
65impl Background for BackgroundCompactor {
66 type Output = ();

Callers 1

runMethod · 0.85

Calls 4

compress_file_handleFunction · 0.85
uncompress_file_handleFunction · 0.85
openMethod · 0.80
lenMethod · 0.80

Tested by

no test coverage detected