1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384
//! A cross-platform library for simple advisory file locking.
//!
//! The lock supports both exclusive and shared locking modes for a byte range
//! of an opened `File` object. Exclusively locking a portion of a file denies
//! all other processes both shared and exclusive access to the specified
//! region of the file. Shared locking a portion of a file denies all processes
//! exclusive access to the specified region of the file. The locked range does
//! not need to exist within the file, and the ranges may be used for any
//! arbitrary advisory locking protocol between processes.
//!
//! The result of a [`lock()`], [`try_lock()`], or [`lock_any()`] is a
//! [`FileGuard`]. When dropped, this [`FileGuard`] will unlock the region of
//! the file currently held. Exclusive locks may be [`.downgrade()`]'ed to
//! either a shared lock cross platform.
//!
//! On Unix systems `fcntl` is used to perform the locking, and on Windows, `LockFileEx`.
//! All generally available behavior is consistent across platforms. For platform-
//! specific behavior, traits may be used for the respective platform. For example,
//! on Windows, locks cannot be safely upgraded, whereas on Unix systems, this can
//! be done safely and atomically. To use this feature, the
//! [`file_guard::os::unix::FileGuardExt`] may `use`ed, enabling the [`.upgrade()`]
//! and [`.try_upgrade()`] methods.
//!
//! Note that on Windows, the file must be open with write permissions to lock it.
//!
//! # Examples
//!
//! ```
//! use file_guard::Lock;
//! use std::fs::OpenOptions;
//!
//! # fn main() -> std::io::Result<()> {
//! let mut file = OpenOptions::new()
//! .read(true)
//! .write(true)
//! .create(true)
//! .open("example-lock")?;
//!
//! let mut lock = file_guard::lock(&mut file, Lock::Exclusive, 0, 1)?;
//! write_to_file(&mut lock)?;
//! # fn write_to_file(f: &mut std::fs::File) -> std::io::Result<()> { Ok(()) }
//! // the lock will be unlocked when it goes out of scope
//! # Ok(())
//! # }
//! ```
//!
//! You can store one or more locks in a struct:
//!
//! ```
//! use file_guard::{FileGuard, Lock};
//! use std::fs::{File, OpenOptions};
//!
//! # fn main() -> std::io::Result<()> {
//! let file = OpenOptions::new()
//! .read(true)
//! .write(true)
//! .create(true)
//! .open("example-lock")?;
//!
//! struct Thing<'file> {
//! a: FileGuard<&'file File>,
//! b: FileGuard<&'file File>,
//! }
//!
//! let t = Thing {
//! a: file_guard::lock(&file, Lock::Exclusive, 0, 1)?,
//! b: file_guard::lock(&file, Lock::Shared, 1, 2)?,
//! };
//! // both locks will be unlocked when t goes out of scope
//! # Ok(())
//! # }
//! ```
//!
//! Anything that can `Deref` or `DerefMut` to a `File` can be used with the [`FileGuard`]
//! (i.e. `Rc<File>`):
//!
//! ```
//! use file_guard::{FileGuard, Lock};
//! use std::fs::{File, OpenOptions};
//! use std::rc::Rc;
//!
//! # fn main() -> std::io::Result<()> {
//! let file = Rc::new(
//! OpenOptions::new()
//! .read(true)
//! .write(true)
//! .create(true)
//! .open("example-lock")?
//! );
//!
//! struct Thing {
//! a: FileGuard<Rc<File>>,
//! b: FileGuard<Rc<File>>,
//! }
//!
//! let t = Thing {
//! a: file_guard::lock(file.clone(), Lock::Exclusive, 0, 1)?,
//! b: file_guard::lock(file, Lock::Shared, 1, 2)?,
//! };
//! // both locks will be unlocked and the file will be closed when t goes out of scope
//! # Ok(())
//! # }
//! ```
//!
//! [`FileGuard`]: struct.FileGuard.html
//! [`lock()`]: fn.lock.html
//! [`try_lock()`]: fn.try_lock.html
//! [`lock_any()`]: fn.lock_any.html
//! [`.downgrade()`]: struct.FileGuard.html#method.downgrade
//! [`file_guard::os::unix::FileGuardExt`]: os/unix/trait.FileGuardExt.html
//! [`.upgrade()`]: os/unix/trait.FileGuardExt.html#tymethod.upgrade
//! [`.try_upgrade()`]: os/unix/trait.FileGuardExt.html#tymethod.try_upgrade
#![deny(missing_docs)]
use std::fs::File;
use std::io::ErrorKind;
use std::ops::{Deref, DerefMut, Range};
use std::{fmt, io};
pub mod os;
use self::os::{raw_file_downgrade, raw_file_lock};
/// The type of a lock operation.
///
/// This is used to specify the desired lock type when used with [`lock()`]
/// and [`try_lock()`], and it is the successful result type returned by
/// [`lock_any()`].
///
/// [`lock()`]: fn.lock.html
/// [`try_lock()`]: fn.try_lock.html
/// [`lock_any()`]: fn.lock_any.html
#[derive(Copy, Clone, PartialEq, Debug)]
pub enum Lock {
/// A shared lock may be concurrently held by multiple processes while
/// preventing future exclusive locks its lifetime.
///
/// The shared lock type cannot be obtained while an exclusive lock is held
/// by another process. When successful, a shared lock guarantees that only
/// one or more shared locks are concurrently held, and that no exclusive
/// locks are held.
///
/// This lock type--often referred to as a read lock--may be used as a
/// means of signaling read integrity. When used cooperatively, they ensure
/// no exclusive lock is held, and thus, no other process may be writing to
/// a shared resource.
Shared,
/// An exclusive lock may only be held by a single process.
///
/// The exclusive lock type can neither be obtained while any shared locks
/// are held or while any other exclusive locks are held. This linearizes
/// the sequence of processes attempting to acquire an exclusive lock.
///
/// This lock type--also known as a write lock--may be used as a means of
/// ensuring write exclusivity. In a cooperative locking environment, all
/// access to a shared resource is halted until the exlusive lock is
/// released.
Exclusive,
}
/// Wait and claim the desired [`Lock`] type using a byte range of a file.
///
/// The byte range does not need to exist in the underlying file.
///
/// [`Lock`]: enum.Lock.html
pub fn lock<T: Deref<Target = File>>(
file: T,
lock: Lock,
offset: usize,
len: usize,
) -> io::Result<FileGuard<T>> {
unsafe {
raw_file_lock(&file, Some(lock), offset, len, true)?;
}
Ok(FileGuard {
offset,
len,
file,
lock,
})
}
/// Attempt to claim the desired [`Lock`] type using a byte range of a file.
///
/// If the desired [`Lock`] type cannot be obtained without blocking, an
/// `Error` of kind `ErrorKind::WouldBlock` is returned. Otherwise if
/// successful, the lock is held.
///
/// The byte range does not need to exist in the underlying file.
///
/// [`Lock`]: enum.Lock.html
pub fn try_lock<T: Deref<Target = File>>(
file: T,
lock: Lock,
offset: usize,
len: usize,
) -> io::Result<FileGuard<T>> {
unsafe {
raw_file_lock(&file, Some(lock), offset, len, false)?;
}
Ok(FileGuard {
offset,
len,
file,
lock,
})
}
/// First attempt to claim an [`Exclusive`] lock and then fallback to a
/// [`Shared`] lock for a byte range of a file. This is not currently an
/// atomic operation.
///
/// When successful, the [`FileGuard`] may be inspected for the lock type
/// obtained using [`.lock_type()`], [`.is_shared()`], or [`.is_exclusive()`].
///
/// The byte range does not need to exist in the underlying file.
///
/// [`Exclusive`]: enum.Lock.html#variant.Exclusive
/// [`Shared`]: enum.Lock.html#variant.Shared
/// [`FileGuard`]: struct.FileGuard.html
/// [`.lock_type()`]: struct.FileGuard.html#method.lock_type
/// [`.is_shared()`]: struct.FileGuard.html#method.is_shared
/// [`.is_exclusive()`]: struct.FileGuard.html#method.is_exclusive
pub fn lock_any<T: Deref<Target = File>>(
file: T,
offset: usize,
len: usize,
) -> io::Result<FileGuard<T>> {
let lock = match unsafe { raw_file_lock(&file, Some(Lock::Exclusive), offset, len, false) } {
Ok(_) => Lock::Exclusive,
Err(e) => {
if e.kind() == ErrorKind::WouldBlock {
unsafe {
raw_file_lock(&file, Some(Lock::Shared), offset, len, true)?;
}
Lock::Shared
} else {
return Err(e);
}
}
};
Ok(FileGuard {
offset,
len,
file,
lock,
})
}
/// An RAII implementation of a "scoped lock" of a file. When this structure
/// is dropped (falls out of scope), the lock will be unlocked.
///
/// This structure is created by the [`lock()`], [`try_lock()`], and
/// [`lock_any()`] functions.
///
/// [`lock()`]: fn.lock.html
/// [`try_lock()`]: fn.try_lock.html
/// [`lock_any()`]: fn.lock_any.html
#[must_use = "if unused the file lock will immediately unlock"]
pub struct FileGuard<T: Deref<Target = File>> {
offset: usize,
len: usize,
file: T,
lock: Lock,
}
impl<T> fmt::Debug for FileGuard<T>
where
T: Deref<Target = File>,
{
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(
f,
"FileGuard::{:?}({}, {})",
self.lock, self.offset, self.len
)
}
}
impl<T> FileGuard<T>
where
T: Deref<Target = File>,
{
/// Gets the [`Lock`] type currently held.
///
/// [`Lock`]: enum.Lock.html
#[inline]
pub fn lock_type(&self) -> Lock {
self.lock
}
/// Test if the currently held [`Lock`] type is [`Shared`].
///
/// [`Lock`]: enum.Lock.html
/// [`Shared`]: enum.Lock.html#variant.Shared
#[inline]
pub fn is_shared(&self) -> bool {
self.lock == Lock::Shared
}
/// Test if the currently held [`Lock`] type is [`Exclusive`].
///
/// [`Lock`]: enum.Lock.html
/// [`Exclusive`]: enum.Lock.html#variant.Exclusive
#[inline]
pub fn is_exclusive(&self) -> bool {
self.lock == Lock::Exclusive
}
/// Gets the byte range of the held lock.
#[inline]
pub fn range(&self) -> Range<usize> {
self.offset..(self.offset + self.len)
}
/// Gets the byte offset of the held lock.
#[inline]
pub fn offset(&self) -> usize {
self.offset
}
/// Gets the byte length of the held lock.
#[inline]
pub fn len(&self) -> usize {
self.len
}
/// Tests if the byte range of the lock has a length of zero.
#[inline]
pub fn is_empty(&self) -> bool {
self.len == 0
}
/// Safely exchanges an [`Exclusive`] [`Lock`] for a [`Shared`] one.
///
/// If the currently held lock is already [`Shared`], no change is made and
/// the method succeeds. This exchange safely ensures no lock is released
/// during operation. That is, no waiting [`Exclusive`] lock attempts may
/// obtain the lock during the downgrade. Other [`Shared`] locks waiting
/// will be granted a lock as a result, however.
///
/// [`Lock`]: enum.Lock.html
/// [`Exclusive`]: enum.Lock.html#variant.Exclusive
/// [`Shared`]: enum.Lock.html#variant.Shared
pub fn downgrade(&mut self) -> io::Result<()> {
if self.is_exclusive() {
unsafe {
raw_file_downgrade(&self.file, self.offset, self.len)?;
}
self.lock = Lock::Shared;
}
Ok(())
}
}
impl<T> Deref for FileGuard<T>
where
T: Deref<Target = File>,
{
type Target = T;
fn deref(&self) -> &T {
&self.file
}
}
impl<T> DerefMut for FileGuard<T>
where
T: DerefMut<Target = File>,
{
fn deref_mut(&mut self) -> &mut T {
&mut self.file
}
}
impl<T> Drop for FileGuard<T>
where
T: Deref<Target = File>,
{
#[inline]
fn drop(&mut self) {
let _ = unsafe { raw_file_lock(&self.file, None, self.offset, self.len, false) };
}
}