-
Notifications
You must be signed in to change notification settings - Fork 389
Add flock
shim
#3759
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add flock
shim
#3759
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
oli-obk marked this conversation as resolved.
Show resolved
Hide resolved
|
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. | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. The comment is confusing... CI seems to indicate that it works just fine on a windows host? It doesn't work with a windows target but that's normal for these libc tests. There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Yeah, it was copied from another test and I forgot to update it after the emulation was implemented. |
||
//@ignore-target-windows: File handling is not implemented yet | ||
oli-obk marked this conversation as resolved.
Show resolved
Hide resolved
|
||
//@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); | ||
} | ||
} |
Uh oh!
There was an error while loading. Please reload this page.