Skip to content

Commit 8c012c8

Browse files
committed
Exclusively lock handles before compaction (#40)
1 parent c2df969 commit 8c012c8

File tree

1 file changed

+5
-0
lines changed

1 file changed

+5
-0
lines changed

src/compression.rs

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@ use std::path::PathBuf;
55
use compresstimator::Compresstimator;
66
use crossbeam_channel::{Receiver, Sender};
77
use filetime::FileTime;
8+
use fs2::FileExt;
89
use winapi::um::winnt::{FILE_READ_DATA, FILE_WRITE_ATTRIBUTES};
910

1011
use crate::background::Background;
@@ -39,6 +40,8 @@ fn handle_file(file: &PathBuf, compression: Option<Compression>) -> io::Result<b
3940
.access_mode(FILE_WRITE_ATTRIBUTES | FILE_READ_DATA)
4041
.open(&file)?;
4142

43+
handle.try_lock_exclusive()?;
44+
4245
let ret = match compression {
4346
Some(compression) => match est.compresstimate(&handle, meta.len()) {
4447
Ok(ratio) if ratio < 0.95 => compact::compress_file_handle(&handle, compression),
@@ -54,6 +57,8 @@ fn handle_file(file: &PathBuf, compression: Option<Compression>) -> io::Result<b
5457
Some(FileTime::from_last_modification_time(&meta)),
5558
);
5659

60+
handle.unlock()?;
61+
5762
ret
5863
}
5964

0 commit comments

Comments
 (0)