mirror of
https://github.com/cargo-bins/cargo-binstall.git
synced 2025-04-21 04:58: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::{
|
||||
collections::{BTreeMap, BTreeSet},
|
||||
fs, io,
|
||||
fs,
|
||||
io::{self, Seek},
|
||||
iter::IntoIterator,
|
||||
path::{Path, PathBuf},
|
||||
str::FromStr,
|
||||
|
@ -74,17 +75,15 @@ impl CratesToml {
|
|||
where
|
||||
Iter: IntoIterator<Item = (&'a CrateVersionSource, BTreeSet<String>)>,
|
||||
{
|
||||
let mut c1 = match Self::load_from_path(path.as_ref()) {
|
||||
Ok(c1) => c1,
|
||||
Err(CratesTomlParseError::Io(io_err)) if io_err.kind() == io::ErrorKind::NotFound => {
|
||||
Self::default()
|
||||
}
|
||||
Err(err) => return Err(err),
|
||||
};
|
||||
let mut file = FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?;
|
||||
let mut c1 = Self::load_from_reader(&mut *file)?;
|
||||
|
||||
for (cvs, bins) in iter {
|
||||
c1.insert(cvs, bins);
|
||||
}
|
||||
c1.write_to_path(path.as_ref())?;
|
||||
|
||||
file.rewind()?;
|
||||
c1.write_to_file(&mut *file)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
|
Loading…
Add table
Reference in a new issue