mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-21 13:08:42 +00:00
Fix CratesToml::append_to_path
: Lock file to avoid race condition
Signed-off-by: Jiahao XU <Jiahao_XU@outlook.com>
This commit is contained in:
parent
e1b6fb85aa
commit
d7bd96660e
1 changed files with 8 additions and 9 deletions
|
@ -1,6 +1,7 @@
|
||||||
use std::{
|
use std::{
|
||||||
collections::{BTreeMap, BTreeSet},
|
collections::{BTreeMap, BTreeSet},
|
||||||
fs, io,
|
fs,
|
||||||
|
io::{self, Seek},
|
||||||
iter::IntoIterator,
|
iter::IntoIterator,
|
||||||
path::{Path, PathBuf},
|
path::{Path, PathBuf},
|
||||||
str::FromStr,
|
str::FromStr,
|
||||||
|
@ -74,17 +75,15 @@ impl CratesToml {
|
||||||
where
|
where
|
||||||
Iter: IntoIterator<Item = (&'a CrateVersionSource, BTreeSet<String>)>,
|
Iter: IntoIterator<Item = (&'a CrateVersionSource, BTreeSet<String>)>,
|
||||||
{
|
{
|
||||||
let mut c1 = match Self::load_from_path(path.as_ref()) {
|
let mut file = FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?;
|
||||||
Ok(c1) => c1,
|
let mut c1 = Self::load_from_reader(&mut *file)?;
|
||||||
Err(CratesTomlParseError::Io(io_err)) if io_err.kind() == io::ErrorKind::NotFound => {
|
|
||||||
Self::default()
|
|
||||||
}
|
|
||||||
Err(err) => return Err(err),
|
|
||||||
};
|
|
||||||
for (cvs, bins) in iter {
|
for (cvs, bins) in iter {
|
||||||
c1.insert(cvs, bins);
|
c1.insert(cvs, bins);
|
||||||
}
|
}
|
||||||
c1.write_to_path(path.as_ref())?;
|
|
||||||
|
file.rewind()?;
|
||||||
|
c1.write_to_file(&mut *file)?;
|
||||||
|
|
||||||
Ok(())
|
Ok(())
|
||||||
}
|
}
|
||||||
|
|
Loading…
Add table
Reference in a new issue