Use fcntl-based file lock for non-Linux unix
This commit is contained in:
parent
a23dd0d1e6
commit
564ebbb0d1
@ -7,13 +7,13 @@
|
|||||||
#![allow(non_camel_case_types)]
|
#![allow(non_camel_case_types)]
|
||||||
#![allow(nonstandard_style)]
|
#![allow(nonstandard_style)]
|
||||||
|
|
||||||
|
use std::fs::{File, OpenOptions};
|
||||||
use std::io;
|
use std::io;
|
||||||
use std::path::Path;
|
use std::path::Path;
|
||||||
|
|
||||||
cfg_if! {
|
cfg_if! {
|
||||||
if #[cfg(unix)] {
|
if #[cfg(target_os = "linux")] {
|
||||||
use std::os::unix::prelude::*;
|
use std::os::unix::prelude::*;
|
||||||
use std::fs::{File, OpenOptions};
|
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct Lock {
|
pub struct Lock {
|
||||||
@ -27,11 +27,11 @@ pub fn new(p: &Path,
|
|||||||
exclusive: bool)
|
exclusive: bool)
|
||||||
-> io::Result<Lock> {
|
-> io::Result<Lock> {
|
||||||
let file = OpenOptions::new()
|
let file = OpenOptions::new()
|
||||||
.read(true)
|
.read(true)
|
||||||
.write(true)
|
.write(true)
|
||||||
.create(create)
|
.create(create)
|
||||||
.mode(libc::S_IRWXU as u32)
|
.mode(libc::S_IRWXU as u32)
|
||||||
.open(p)?;
|
.open(p)?;
|
||||||
|
|
||||||
let mut operation = if exclusive {
|
let mut operation = if exclusive {
|
||||||
libc::LOCK_EX
|
libc::LOCK_EX
|
||||||
@ -44,8 +44,7 @@ pub fn new(p: &Path,
|
|||||||
|
|
||||||
let ret = unsafe { libc::flock(file.as_raw_fd(), operation) };
|
let ret = unsafe { libc::flock(file.as_raw_fd(), operation) };
|
||||||
if ret == -1 {
|
if ret == -1 {
|
||||||
let err = io::Error::last_os_error();
|
Err(io::Error::last_os_error())
|
||||||
Err(err)
|
|
||||||
} else {
|
} else {
|
||||||
Ok(Lock { _file: file })
|
Ok(Lock { _file: file })
|
||||||
}
|
}
|
||||||
@ -55,10 +54,68 @@ pub fn new(p: &Path,
|
|||||||
// Note that we don't need a Drop impl to execute `flock(fd, LOCK_UN)`. Lock acquired by
|
// 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
|
// `flock` is associated with the file descriptor and closing the file release it
|
||||||
// automatically.
|
// 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
|
||||||
|
} 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 })
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
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);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
} else if #[cfg(windows)] {
|
} else if #[cfg(windows)] {
|
||||||
use std::mem;
|
use std::mem;
|
||||||
use std::os::windows::prelude::*;
|
use std::os::windows::prelude::*;
|
||||||
use std::fs::{File, OpenOptions};
|
|
||||||
|
|
||||||
use winapi::um::minwinbase::{OVERLAPPED, LOCKFILE_FAIL_IMMEDIATELY, LOCKFILE_EXCLUSIVE_LOCK};
|
use winapi::um::minwinbase::{OVERLAPPED, LOCKFILE_FAIL_IMMEDIATELY, LOCKFILE_EXCLUSIVE_LOCK};
|
||||||
use winapi::um::fileapi::LockFileEx;
|
use winapi::um::fileapi::LockFileEx;
|
||||||
|
Loading…
Reference in New Issue
Block a user