From 0791208ca4bfa4f67bf6b55c1203f5d659a68a18 Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Fri, 24 Sep 2021 18:24:13 +0800 Subject: [PATCH] [io_engine (rust)] Open file exclusively --- src/io_engine.rs | 30 ++++++++++++++++++++++++++---- 1 file changed, 26 insertions(+), 4 deletions(-) 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 {