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

Added Flock object for automatic unlock on drop. #2170

Merged
merged 35 commits into from
Dec 3, 2023
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
Show all changes
35 commits
Select commit Hold shift + click to select a range
bdc7e8e
Added object for automatic unlock on drop.
coderBlitz Oct 10, 2023
1740131
Added appropriate changelog file.
coderBlitz Oct 10, 2023
aa8369d
Fixed doc error on `Flock`.
coderBlitz Oct 10, 2023
a566bb2
Requested changes to make `fcntl::flock` private and OwnedFd instead …
coderBlitz Oct 14, 2023
7912e88
Indent fix.
coderBlitz Oct 14, 2023
d234054
Removed doc links to private item, updated imports.
coderBlitz Oct 14, 2023
e0c53bb
More import fixes.
coderBlitz Oct 14, 2023
ad58d40
Format changes.
coderBlitz Oct 14, 2023
647059d
Remove unused import for redox.
coderBlitz Oct 14, 2023
7cf6765
Added `Flockable` trait per discussions, added tests for `Flock`.
coderBlitz Nov 19, 2023
99e9f6c
Simplified flock error conditionals.
coderBlitz Nov 19, 2023
32443fa
Added missing cfg flags for `Flockable`.
coderBlitz Nov 19, 2023
81ca0e5
Added missing cfg flags for impl of `Flockable` on `File`.
coderBlitz Nov 19, 2023
5704efe
Update src/fcntl.rs
coderBlitz Nov 20, 2023
f256883
Merge remote-tracking branch 'upstream/master'
coderBlitz Nov 20, 2023
2103ec7
Updated `Flockable` comment per suggestion.
coderBlitz Nov 22, 2023
5609d07
Finalized `FlockArg` as enum and removed TODO accordingly, removed fl…
coderBlitz Nov 22, 2023
2eaca17
Implemented `Flockable` for `OwnedFd` as well.
coderBlitz Nov 22, 2023
4919bba
Fixed linting error.
coderBlitz Nov 22, 2023
91c9590
Updated changelog accordingly.
coderBlitz Nov 22, 2023
d61c038
Corrected errno logic in `Flock::lock`.
coderBlitz Nov 22, 2023
40fda24
Properly dropped inner type for `Flock`.
coderBlitz Nov 22, 2023
b544cd1
Replaced `ManuallyDrop` with `Option` as inner `Flock` type to avoid …
coderBlitz Nov 22, 2023
91273fb
Fixed linting errors.
coderBlitz Nov 22, 2023
836986c
Removed unnecessary cfg condition, updated documentation.
coderBlitz Nov 22, 2023
723c386
Modified Flock behavior for drop() and unlock().
coderBlitz Nov 22, 2023
85b76ee
Reverted changes to original `flock()` and `FlockArg` for deprecation…
coderBlitz Nov 22, 2023
c470752
Refactored `Flock` to wrap T directly and avoid a double-free after `…
coderBlitz Nov 23, 2023
a28425b
Fixed linting errors.
coderBlitz Nov 23, 2023
47e1f5d
More linting fixes.
coderBlitz Nov 23, 2023
936e3ce
Fixed example code for `Flock::unlock()`.
coderBlitz Nov 23, 2023
bcb5880
Made requested changes.
coderBlitz Dec 2, 2023
6dc7bed
Merge branch 'master' into master
coderBlitz Dec 2, 2023
b04334f
Removed duplicate import.
coderBlitz Dec 2, 2023
d3225bb
Format change.
coderBlitz Dec 2, 2023
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
1 change: 1 addition & 0 deletions changelog/2170.added.md
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
Added newtype `Flock` to automatically unlock a held flock upon drop.
36 changes: 36 additions & 0 deletions src/fcntl.rs
Original file line number Diff line number Diff line change
Expand Up @@ -631,6 +631,42 @@ pub fn flock(fd: RawFd, arg: FlockArg) -> Result<()> {

Errno::result(res).map(drop)
}

/// Represents a file lock on a particular [RawFd], which unlocks when dropped.
///
/// See [flock] for details on locking semantics.
#[cfg(not(any(target_os = "redox", target_os = "solaris")))]
#[derive(Debug, Eq, Hash, PartialEq)]
pub struct Flock(RawFd);

#[cfg(not(any(target_os = "redox", target_os = "solaris")))]
impl Drop for Flock {
fn drop(&mut self) {
_ = flock(self.0, FlockArg::Unlock);
}
}

#[cfg(not(any(target_os = "redox", target_os = "solaris")))]
impl Flock {
/// Lock the given `fd` using [flock].
///
/// # Example
/// ```
/// # use std::os::fd::RawFd;
/// # use nix::fcntl::{Flock, FlockArg};
/// fn do_stuff(fd: RawFd) -> nix::Result<()> {
/// let lock = Flock::lock(fd, FlockArg::LockExclusive)?;
///
/// // Do stuff
///
/// Ok(())
/// } // File is unlocked once `lock` goes out of scope.
pub fn lock(fd: RawFd, args: FlockArg) -> Result<Self> {
flock(fd, args)?;

Ok(Self(fd))
}
}
}

#[cfg(any(target_os = "android", target_os = "linux"))]
Expand Down