mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-22 21:48:42 +00:00
Fix Crates2Json::append_to_path
: Use file lock
to fix race condition. Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
d432d54c28
commit
0a753f3e4b
1 changed files with 7 additions and 9 deletions
|
@ -11,7 +11,7 @@ use serde::{Deserialize, Serialize};
|
||||||
use thiserror::Error;
|
use thiserror::Error;
|
||||||
|
|
||||||
use super::CrateVersionSource;
|
use super::CrateVersionSource;
|
||||||
use crate::cargo_home;
|
use crate::{cargo_home, create_if_not_exist, FileLock};
|
||||||
|
|
||||||
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
#[derive(Clone, Debug, Default, Deserialize, Serialize)]
|
||||||
pub struct Crates2Json {
|
pub struct Crates2Json {
|
||||||
|
@ -91,17 +91,15 @@ impl Crates2Json {
|
||||||
where
|
where
|
||||||
Iter: IntoIterator<Item = (CrateVersionSource, CrateInfo)>,
|
Iter: IntoIterator<Item = (CrateVersionSource, CrateInfo)>,
|
||||||
{
|
{
|
||||||
let mut c2 = match Self::load_from_path(path.as_ref()) {
|
let mut file = FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?;
|
||||||
Ok(c2) => c2,
|
let mut c2 = Self::load_from_reader(&mut *file)?;
|
||||||
Err(Crates2JsonParseError::Io(io_err)) if io_err.kind() == io::ErrorKind::NotFound => {
|
|
||||||
Self::default()
|
|
||||||
}
|
|
||||||
Err(err) => return Err(err),
|
|
||||||
};
|
|
||||||
for (cvs, info) in iter {
|
for (cvs, info) in iter {
|
||||||
c2.insert(&cvs, info);
|
c2.insert(&cvs, info);
|
||||||
}
|
}
|
||||||
c2.write_to_path(path.as_ref())?;
|
|
||||||
|
file.rewind()?;
|
||||||
|
c2.write_to_file(&mut *file)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue