diff --git a/src/io_engine.rs b/src/io_engine.rs index 55782dc..091ed76 100644 --- a/src/io_engine.rs +++ b/src/io_engine.rs @@ -131,21 +131,30 @@ impl<'a> Drop for FileGuard<'a> { } impl SyncIoEngine { - fn open_file(path: &Path, writable: bool) -> Result { + fn open_file(path: &Path, writable: bool, excl: bool) -> Result { let file = OpenOptions::new() .read(true) .write(writable) - .custom_flags(libc::O_EXCL) + .custom_flags(if excl { libc::O_EXCL } else { 0 }) .open(path)?; Ok(file) } pub fn new(path: &Path, nr_files: usize, writable: bool) -> Result { + SyncIoEngine::new_with(path, nr_files, writable, true) + } + + pub fn new_with( + path: &Path, + nr_files: usize, + writable: bool, + excl: bool, + ) -> Result { let nr_blocks = get_nr_blocks(path)?; // check file mode eariler let mut files = Vec::with_capacity(nr_files); for _n in 0..nr_files { - files.push(SyncIoEngine::open_file(path, writable)?); + files.push(SyncIoEngine::open_file(path, writable, excl)?); } Ok(SyncIoEngine { @@ -237,11 +246,24 @@ pub struct AsyncIoEngine { impl AsyncIoEngine { pub fn new(path: &Path, queue_len: u32, writable: bool) -> Result { + AsyncIoEngine::new_with(path, queue_len, writable, true) + } + + pub fn new_with( + path: &Path, + queue_len: u32, + writable: bool, + excl: bool, + ) -> Result { let nr_blocks = get_nr_blocks(path)?; // check file mode earlier + let mut flags = libc::O_DIRECT; + if excl { + flags |= libc::O_EXCL; + } let input = OpenOptions::new() .read(true) .write(writable) - .custom_flags(libc::O_DIRECT | libc::O_EXCL) + .custom_flags(flags) .open(path)?; Ok(AsyncIoEngine {