mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-06-15 07:06:36 +00:00
Add FileLock::set_file_path
Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>
This commit is contained in:
parent
b2cddb8f9d
commit
644c70819f
1 changed files with 24 additions and 5 deletions
|
@ -12,16 +12,28 @@ use fs4::fs_std::FileExt;
|
||||||
|
|
||||||
/// A locked file.
|
/// A locked file.
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
pub struct FileLock(File, Option<Box<Path>>);
|
pub struct FileLock(
|
||||||
|
File,
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
Option<Box<Path>>,
|
||||||
|
);
|
||||||
|
|
||||||
impl FileLock {
|
impl FileLock {
|
||||||
|
fn new(file: File) -> Self {
|
||||||
|
Self(
|
||||||
|
file,
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
None,
|
||||||
|
)
|
||||||
|
}
|
||||||
|
|
||||||
/// Take an exclusive lock on a [`File`].
|
/// Take an exclusive lock on a [`File`].
|
||||||
///
|
///
|
||||||
/// Note that this operation is blocking, and should not be called in async contexts.
|
/// Note that this operation is blocking, and should not be called in async contexts.
|
||||||
pub fn new_exclusive(file: File) -> io::Result<Self> {
|
pub fn new_exclusive(file: File) -> io::Result<Self> {
|
||||||
FileExt::lock_exclusive(&file)?;
|
FileExt::lock_exclusive(&file)?;
|
||||||
|
|
||||||
Ok(Self(file, None))
|
Ok(Self:new(file))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Try to take an exclusive lock on a [`File`].
|
/// Try to take an exclusive lock on a [`File`].
|
||||||
|
@ -33,7 +45,7 @@ impl FileLock {
|
||||||
/// Note that this operation is blocking, and should not be called in async contexts.
|
/// Note that this operation is blocking, and should not be called in async contexts.
|
||||||
pub fn new_try_exclusive(file: File) -> Result<Self, (File, Option<io::Error>)> {
|
pub fn new_try_exclusive(file: File) -> Result<Self, (File, Option<io::Error>)> {
|
||||||
match FileExt::try_lock_exclusive(&file) {
|
match FileExt::try_lock_exclusive(&file) {
|
||||||
Ok(()) => Ok(Self(file, None)),
|
Ok(()) => Ok(Self::new(file)),
|
||||||
Err(e) if e.raw_os_error() == fs4::lock_contended_error().raw_os_error() => {
|
Err(e) if e.raw_os_error() == fs4::lock_contended_error().raw_os_error() => {
|
||||||
Err((file, None))
|
Err((file, None))
|
||||||
}
|
}
|
||||||
|
@ -47,7 +59,7 @@ impl FileLock {
|
||||||
pub fn new_shared(file: File) -> io::Result<Self> {
|
pub fn new_shared(file: File) -> io::Result<Self> {
|
||||||
FileExt::lock_shared(&file)?;
|
FileExt::lock_shared(&file)?;
|
||||||
|
|
||||||
Ok(Self(file, None))
|
Ok(Self::new(file))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Try to take a shared lock on a [`File`].
|
/// Try to take a shared lock on a [`File`].
|
||||||
|
@ -59,13 +71,20 @@ impl FileLock {
|
||||||
/// Note that this operation is blocking, and should not be called in async contexts.
|
/// Note that this operation is blocking, and should not be called in async contexts.
|
||||||
pub fn new_try_shared(file: File) -> Result<Self, (File, Option<io::Error>)> {
|
pub fn new_try_shared(file: File) -> Result<Self, (File, Option<io::Error>)> {
|
||||||
match FileExt::try_lock_shared(&file) {
|
match FileExt::try_lock_shared(&file) {
|
||||||
Ok(()) => Ok(Self(file, None)),
|
Ok(()) => Ok(Self::new(file)),
|
||||||
Err(e) if e.raw_os_error() == fs4::lock_contended_error().raw_os_error() => {
|
Err(e) if e.raw_os_error() == fs4::lock_contended_error().raw_os_error() => {
|
||||||
Err((file, None))
|
Err((file, None))
|
||||||
}
|
}
|
||||||
Err(e) => Err((file, Some(e))),
|
Err(e) => Err((file, Some(e))),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/// Set path to the file for logging on unlock error, if feature tracing is enabled
|
||||||
|
pub fn set_file_path(mut self, path: impl Into<Box<Path>>) -> Self {
|
||||||
|
#[cfg(feature = "tracing")]
|
||||||
|
self.1 = Some(path.into());
|
||||||
|
self
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
impl Drop for FileLock {
|
impl Drop for FileLock {
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue