From d712190db45df8c5b810fa5d01ce87680d6b10cb Mon Sep 17 00:00:00 2001 From: Ming-Hung Tsai Date: Sat, 11 Sep 2021 22:38:00 +0800 Subject: [PATCH] [io_engine (rust)] Open file exclusively --- src/io_engine.rs | 14 ++++++++++---- 1 file changed, 10 insertions(+), 4 deletions(-) diff --git a/src/io_engine.rs b/src/io_engine.rs index 37f0d49..4d68d75 100644 --- a/src/io_engine.rs +++ b/src/io_engine.rs @@ -132,19 +132,24 @@ impl<'a> Drop for FileGuard<'a> { impl SyncIoEngine { fn open_file(path: &Path, writable: bool) -> Result { - let file = OpenOptions::new().read(true).write(writable).open(path)?; + let file = OpenOptions::new() + .read(true) + .write(writable) + .custom_flags(libc::O_EXCL) + .open(path)?; Ok(file) } pub fn new(path: &Path, nr_files: usize, writable: 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)?); } Ok(SyncIoEngine { - nr_blocks: get_nr_blocks(path)?, + nr_blocks, files: Mutex::new(files), cvar: Condvar::new(), }) @@ -232,17 +237,18 @@ pub struct AsyncIoEngine { impl AsyncIoEngine { pub fn new(path: &Path, queue_len: u32, writable: bool) -> Result { + let nr_blocks = get_nr_blocks(path)?; // check file mode earlier let input = OpenOptions::new() .read(true) .write(writable) - .custom_flags(libc::O_DIRECT) + .custom_flags(libc::O_DIRECT | libc::O_EXCL) .open(path)?; Ok(AsyncIoEngine { inner: Mutex::new(AsyncIoEngine_ { queue_len, ring: IoUring::new(queue_len)?, - nr_blocks: get_nr_blocks(path)?, + nr_blocks, fd: input.as_raw_fd(), input: Arc::new(input), }),