devela::_dep::nc

Function flock

pub unsafe fn flock(fd: i32, operation: i32) -> Result<(), i32> 
Available on crate feature dep_nc only.
Expand description

Apply or remove an advisory lock on an open file.

§Examples

let path = "/tmp/nc-flock";
let ret = unsafe { nc::openat(nc::AT_FDCWD, path, nc::O_WRONLY | nc::O_CREAT, 0o644) };
assert!(ret.is_ok());
let fd = ret.unwrap();
let ret = unsafe { nc::flock(fd, nc::LOCK_EX) };
assert!(ret.is_ok());
let msg = "Hello, Rust";
let ret = unsafe { nc::write(fd, msg.as_bytes()) };
assert!(ret.is_ok());
assert_eq!(ret, Ok(msg.len() as nc::ssize_t));
let ret = unsafe { nc::flock(fd, nc::LOCK_UN) };
assert!(ret.is_ok());
let ret = unsafe { nc::close(fd) };
assert!(ret.is_ok());
let ret = unsafe { nc::unlinkat(nc::AT_FDCWD, path,0 ) };
assert!(ret.is_ok());