Skip to content
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

Replace fcntl-based file lock with flock #72161

Merged
merged 3 commits into from
May 22, 2020
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
81 changes: 57 additions & 24 deletions src/librustc_data_structures/flock.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,18 +7,22 @@
#![allow(non_camel_case_types)]
#![allow(nonstandard_style)]

use std::fs::{File, OpenOptions};
use std::io;
use std::path::Path;

cfg_if! {
if #[cfg(unix)] {
use std::ffi::{CString, OsStr};
use std::mem;
// We use `flock` rather than `fcntl` on Linux, because WSL1 does not support
// `fcntl`-style advisory locks properly (rust-lang/rust#72157).
//
// For other Unix targets we still use `fcntl` because it's more portable than
// `flock`.
if #[cfg(target_os = "linux")] {
cuviper marked this conversation as resolved.
Show resolved Hide resolved
use std::os::unix::prelude::*;

#[derive(Debug)]
pub struct Lock {
fd: libc::c_int,
_file: File,
}

impl Lock {
Expand All @@ -27,22 +31,55 @@ cfg_if! {
create: bool,
exclusive: bool)
-> io::Result<Lock> {
let os: &OsStr = p.as_ref();
let buf = CString::new(os.as_bytes()).unwrap();
let open_flags = if create {
libc::O_RDWR | libc::O_CREAT
let file = OpenOptions::new()
.read(true)
.write(true)
.create(create)
.mode(libc::S_IRWXU as u32)
.open(p)?;

let mut operation = if exclusive {
libc::LOCK_EX
} else {
libc::O_RDWR
};

let fd = unsafe {
libc::open(buf.as_ptr(), open_flags,
libc::S_IRWXU as libc::c_int)
libc::LOCK_SH
};
if !wait {
operation |= libc::LOCK_NB
}

if fd < 0 {
return Err(io::Error::last_os_error());
let ret = unsafe { libc::flock(file.as_raw_fd(), operation) };
if ret == -1 {
Err(io::Error::last_os_error())
} else {
Ok(Lock { _file: file })
}
}
}

// Note that we don't need a Drop impl to execute `flock(fd, LOCK_UN)`. Lock acquired by
// `flock` is associated with the file descriptor and closing the file release it
// automatically.
} else if #[cfg(unix)] {
use std::mem;
use std::os::unix::prelude::*;

#[derive(Debug)]
pub struct Lock {
file: File,
}

impl Lock {
pub fn new(p: &Path,
wait: bool,
create: bool,
exclusive: bool)
-> io::Result<Lock> {
let file = OpenOptions::new()
.read(true)
.write(true)
.create(create)
.mode(libc::S_IRWXU as u32)
.open(p)?;

let lock_type = if exclusive {
libc::F_WRLCK
Expand All @@ -58,14 +95,12 @@ cfg_if! {

let cmd = if wait { libc::F_SETLKW } else { libc::F_SETLK };
let ret = unsafe {
libc::fcntl(fd, cmd, &flock)
libc::fcntl(file.as_raw_fd(), cmd, &flock)
};
if ret == -1 {
let err = io::Error::last_os_error();
unsafe { libc::close(fd); }
Err(err)
Err(io::Error::last_os_error())
} else {
Ok(Lock { fd })
Ok(Lock { file })
}
}
}
Expand All @@ -79,15 +114,13 @@ cfg_if! {
flock.l_len = 0;

unsafe {
libc::fcntl(self.fd, libc::F_SETLK, &flock);
libc::close(self.fd);
libc::fcntl(self.file.as_raw_fd(), libc::F_SETLK, &flock);
}
}
}
} else if #[cfg(windows)] {
use std::mem;
use std::os::windows::prelude::*;
use std::fs::{File, OpenOptions};

use winapi::um::minwinbase::{OVERLAPPED, LOCKFILE_FAIL_IMMEDIATELY, LOCKFILE_EXCLUSIVE_LOCK};
use winapi::um::fileapi::LockFileEx;
Expand Down