-
Notifications
You must be signed in to change notification settings - Fork 12.8k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Rollup merge of #96058 - euclio:flock-impls, r=nagisa
separate flock implementations into separate modules The main benefit of doing this is that rustfmt will now format each of these modules.
- Loading branch information
Showing
5 changed files
with
194 additions
and
215 deletions.
There are no files selected for viewing
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,40 @@ | ||
//! 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`. | ||
use std::fs::{File, OpenOptions}; | ||
use std::io; | ||
use std::os::unix::prelude::*; | ||
use std::path::Path; | ||
|
||
#[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 mut operation = if exclusive { libc::LOCK_EX } else { libc::LOCK_SH }; | ||
if !wait { | ||
operation |= libc::LOCK_NB | ||
} | ||
|
||
let ret = unsafe { libc::flock(file.as_raw_fd(), operation) }; | ||
if ret == -1 { Err(io::Error::last_os_error()) } else { Ok(Lock { _file: file }) } | ||
} | ||
|
||
pub fn error_unsupported(err: &io::Error) -> bool { | ||
matches!(err.raw_os_error(), Some(libc::ENOTSUP) | Some(libc::ENOSYS)) | ||
} | ||
} | ||
|
||
// Note that we don't need a Drop impl to execute `flock(fd, LOCK_UN)`. A lock acquired by | ||
// `flock` is associated with the file descriptor and closing the file releases it | ||
// automatically. |
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,51 @@ | ||
use std::fs::{File, OpenOptions}; | ||
use std::io; | ||
use std::mem; | ||
use std::os::unix::prelude::*; | ||
use std::path::Path; | ||
|
||
#[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 } else { libc::F_RDLCK }; | ||
|
||
let mut flock: libc::flock = unsafe { mem::zeroed() }; | ||
flock.l_type = lock_type as libc::c_short; | ||
flock.l_whence = libc::SEEK_SET as libc::c_short; | ||
flock.l_start = 0; | ||
flock.l_len = 0; | ||
|
||
let cmd = if wait { libc::F_SETLKW } else { libc::F_SETLK }; | ||
let ret = unsafe { libc::fcntl(file.as_raw_fd(), cmd, &flock) }; | ||
if ret == -1 { Err(io::Error::last_os_error()) } else { Ok(Lock { file }) } | ||
} | ||
|
||
pub fn error_unsupported(err: &io::Error) -> bool { | ||
matches!(err.raw_os_error(), Some(libc::ENOTSUP) | Some(libc::ENOSYS)) | ||
} | ||
} | ||
|
||
impl Drop for Lock { | ||
fn drop(&mut self) { | ||
let mut flock: libc::flock = unsafe { mem::zeroed() }; | ||
flock.l_type = libc::F_UNLCK as libc::c_short; | ||
flock.l_whence = libc::SEEK_SET as libc::c_short; | ||
flock.l_start = 0; | ||
flock.l_len = 0; | ||
|
||
unsafe { | ||
libc::fcntl(self.file.as_raw_fd(), libc::F_SETLK, &flock); | ||
} | ||
} | ||
} |
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,16 @@ | ||
use std::io; | ||
use std::path::Path; | ||
|
||
#[derive(Debug)] | ||
pub struct Lock(()); | ||
|
||
impl Lock { | ||
pub fn new(_p: &Path, _wait: bool, _create: bool, _exclusive: bool) -> io::Result<Lock> { | ||
let msg = "file locks not supported on this platform"; | ||
Err(io::Error::new(io::ErrorKind::Other, msg)) | ||
} | ||
|
||
pub fn error_unsupported(_err: &io::Error) -> bool { | ||
true | ||
} | ||
} |
Oops, something went wrong.