-
Notifications
You must be signed in to change notification settings - Fork 346
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Auto merge of #3759 - newpavlov:flock, r=oli-obk
Add `flock` shim
- Loading branch information
Showing
6 changed files
with
230 additions
and
2 deletions.
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,71 @@ | ||
// Flock tests are separate since they don't in general work on a Windows host. | ||
//@ignore-target-windows: File handling is not implemented yet | ||
//@compile-flags: -Zmiri-disable-isolation | ||
|
||
use std::{fs::File, io::Error, os::fd::AsRawFd}; | ||
|
||
#[path = "../../utils/mod.rs"] | ||
mod utils; | ||
|
||
fn main() { | ||
let bytes = b"Hello, World!\n"; | ||
let path = utils::prepare_with_content("miri_test_fs_shared_lock.txt", bytes); | ||
|
||
let files: Vec<File> = (0..3).map(|_| File::open(&path).unwrap()).collect(); | ||
|
||
// Test that we can apply many shared locks | ||
for file in files.iter() { | ||
let fd = file.as_raw_fd(); | ||
let ret = unsafe { libc::flock(fd, libc::LOCK_SH) }; | ||
if ret != 0 { | ||
panic!("flock error: {}", Error::last_os_error()); | ||
} | ||
} | ||
|
||
// Test that shared lock prevents exclusive lock | ||
{ | ||
let fd = files[0].as_raw_fd(); | ||
let ret = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) }; | ||
assert_eq!(ret, -1); | ||
let err = Error::last_os_error().raw_os_error().unwrap(); | ||
assert_eq!(err, libc::EWOULDBLOCK); | ||
} | ||
|
||
// Unlock shared lock | ||
for file in files.iter() { | ||
let fd = file.as_raw_fd(); | ||
let ret = unsafe { libc::flock(fd, libc::LOCK_UN) }; | ||
if ret != 0 { | ||
panic!("flock error: {}", Error::last_os_error()); | ||
} | ||
} | ||
|
||
// Take exclusive lock | ||
{ | ||
let fd = files[0].as_raw_fd(); | ||
let ret = unsafe { libc::flock(fd, libc::LOCK_EX) }; | ||
assert_eq!(ret, 0); | ||
} | ||
|
||
// Test that shared lock prevents exclusive and shared locks | ||
{ | ||
let fd = files[1].as_raw_fd(); | ||
let ret = unsafe { libc::flock(fd, libc::LOCK_EX | libc::LOCK_NB) }; | ||
assert_eq!(ret, -1); | ||
let err = Error::last_os_error().raw_os_error().unwrap(); | ||
assert_eq!(err, libc::EWOULDBLOCK); | ||
|
||
let fd = files[2].as_raw_fd(); | ||
let ret = unsafe { libc::flock(fd, libc::LOCK_SH | libc::LOCK_NB) }; | ||
assert_eq!(ret, -1); | ||
let err = Error::last_os_error().raw_os_error().unwrap(); | ||
assert_eq!(err, libc::EWOULDBLOCK); | ||
} | ||
|
||
// Unlock exclusive lock | ||
{ | ||
let fd = files[0].as_raw_fd(); | ||
let ret = unsafe { libc::flock(fd, libc::LOCK_UN) }; | ||
assert_eq!(ret, 0); | ||
} | ||
} |