Log when FileLock::drop fails to unlock file (#2064)

* Add optional dependency tracing to fs-lock

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Add optional logging to FileLock::drop

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Enable fs-lock/tracing in binstalk-manifest

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Add FileLock::set_file_path

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Update create_if_not_exist to return FileLock on successs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Update create_if_not_exist usage in mod binstall_crates_v1

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Simplify create_if_not_exist

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Refactor mod crates_manifests to use create_if_not_exist

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Set file path for FileLock

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Set file path for file lock in mod cargo_config

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Fix fs-lock impl

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Import Path in fs-lock lib.rs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Fix fs-lock lib.rs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Fix fs-lock lib.rs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Fix typo in crates_manifests.rs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Fix calling create_if_not_exist in crates_manifests.rs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Fox fmt crates_manifests.rs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

* Fix fmt in lib.rs

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>

---------

Signed-off-by: Jiahao XU <30436523+NobodyXu@users.noreply.github.com>
This commit is contained in:
Jiahao XU 2025-02-23 01:33:36 +11:00 committed by GitHub
parent 86a7e90175
commit 8ee6c537e4
No known key found for this signature in database
GPG key ID: B5690EEEBB952194
8 changed files with 69 additions and 28 deletions

View file

@ -13,7 +13,7 @@ license = "Apache-2.0 OR MIT"
beef = { version = "0.5.2", features = ["impl_serde"] }
binstalk-types = { version = "0.9.3", path = "../binstalk-types" }
compact_str = { version = "0.8.0", features = ["serde"] }
fs-lock = { version = "0.1.7", path = "../fs-lock" }
fs-lock = { version = "0.1.7", path = "../fs-lock", features = ["tracing"] }
home = "0.5.9"
miette = "7.0.0"
semver = { version = "1.0.17", features = ["serde"] }

View file

@ -43,7 +43,8 @@ where
Iter: IntoIterator<Item = T>,
Data: From<T>,
{
let mut file = FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?;
let path = path.as_ref();
let mut file = create_if_not_exist(path)?;
// Move the cursor to EOF
file.seek(io::SeekFrom::End(0))?;
@ -166,7 +167,7 @@ impl Records {
pub fn load_from_path(path: impl AsRef<Path>) -> Result<Self, Error> {
let mut this = Self {
file: FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?,
file: create_if_not_exist(path.as_ref())?,
data: BTreeSet::default(),
};
this.load_impl()?;

View file

@ -138,7 +138,7 @@ impl Config {
fn inner(path: &Path) -> Result<Config, ConfigLoadError> {
match File::open(path) {
Ok(file) => {
let file = FileLock::new_shared(file)?;
let file = FileLock::new_shared(file)?.set_file_path(path);
// Any regular file must have a parent dir
Config::load_from_reader(file, path.parent().unwrap())
}

View file

@ -62,7 +62,8 @@ impl CratesToml<'_> {
}
pub fn load_from_path(path: impl AsRef<Path>) -> Result<Self, CratesTomlParseError> {
let file = FileLock::new_shared(File::open(path)?)?;
let path = path.as_ref();
let file = FileLock::new_shared(File::open(path)?)?.set_file_path(path);
Self::load_from_reader(file)
}
@ -100,7 +101,8 @@ impl CratesToml<'_> {
}
pub fn write_to_path(&self, path: impl AsRef<Path>) -> Result<(), CratesTomlParseError> {
let mut file = FileLock::new_exclusive(File::create(path)?)?;
let path = path.as_ref();
let mut file = FileLock::new_exclusive(File::create(path)?)?.set_file_path(path);
self.write_to_file(&mut file)
}
@ -142,7 +144,7 @@ impl CratesToml<'_> {
where
Iter: IntoIterator<Item = &'a CrateInfo>,
{
let mut file = FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?;
let mut file = create_if_not_exist(path.as_ref())?;
Self::append_to_file(&mut file, iter)
}

View file

@ -13,6 +13,7 @@ use crate::{
binstall_crates_v1::{Error as BinstallCratesV1Error, Records as BinstallCratesV1Records},
cargo_crates_v1::{CratesToml, CratesTomlParseError},
crate_info::CrateInfo,
helpers::create_if_not_exist,
CompactString, Version,
};
@ -47,13 +48,7 @@ impl Manifests {
// Read cargo_install_v1_metadata
let manifest_path = cargo_roots.join(".crates.toml");
let cargo_crates_v1 = fs::File::options()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(manifest_path)
.and_then(FileLock::new_exclusive)?;
let cargo_crates_v1 = create_if_not_exist(&manifest_path)?;
Ok(Self {
binstall,

View file

@ -1,13 +1,15 @@
use std::{fs, io, path::Path};
/// Returned file is readable and writable.
pub(crate) fn create_if_not_exist(path: &Path) -> io::Result<fs::File> {
let mut options = fs::File::options();
options.read(true).write(true);
use fs_lock::FileLock;
options
.clone()
.create_new(true)
/// Return exclusively locked file that is readable and writable.
pub(crate) fn create_if_not_exist(path: &Path) -> io::Result<FileLock> {
fs::File::options()
.read(true)
.write(true)
.create(true)
.truncate(false)
.open(path)
.or_else(|_| options.open(path))
.and_then(FileLock::new_exclusive)
.map(|file_lock| file_lock.set_file_path(path))
}