rustc_data_structures/flock/
linux.rs1use std::fs::{File, OpenOptions};
6use std::io;
7use std::os::unix::prelude::*;
8use std::path::Path;
9
10#[derive(#[automatically_derived]
impl ::core::fmt::Debug for Lock {
#[inline]
fn fmt(&self, f: &mut ::core::fmt::Formatter) -> ::core::fmt::Result {
::core::fmt::Formatter::debug_struct_field1_finish(f, "Lock", "_file",
&&self._file)
}
}Debug)]
11pub struct Lock {
12 _file: File,
13}
14
15impl Lock {
16 pub fn new(p: &Path, wait: bool, create: bool, exclusive: bool) -> io::Result<Lock> {
17 let file = OpenOptions::new().read(true).write(true).create(create).mode(0o600).open(p)?;
18
19 let mut operation = if exclusive { libc::LOCK_EX } else { libc::LOCK_SH };
20 if !wait {
21 operation |= libc::LOCK_NB
22 }
23
24 let ret = unsafe { libc::flock(file.as_raw_fd(), operation) };
25 if ret == -1 { Err(io::Error::last_os_error()) } else { Ok(Lock { _file: file }) }
26 }
27
28 pub fn error_unsupported(err: &io::Error) -> bool {
29 #[allow(non_exhaustive_omitted_patterns)] match err.raw_os_error() {
Some(libc::ENOTSUP) | Some(libc::ENOSYS) => true,
_ => false,
}matches!(err.raw_os_error(), Some(libc::ENOTSUP) | Some(libc::ENOSYS))
30 }
31}
32
33