From 28aeae938e392a8449d34f7fdcacfc1f6ab17663 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 4 Aug 2022 13:59:50 +1000 Subject: [PATCH 1/2] `fs::create_dir_all(&install_path)` in `main.rs` Signed-off-by: Jiahao XU --- src/main.rs | 6 ++++++ 1 file changed, 6 insertions(+) diff --git a/src/main.rs b/src/main.rs index dc536330b..a794c52cd 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,5 +1,6 @@ use std::{ ffi::OsString, + fs, mem::take, path::{Path, PathBuf}, process::{ExitCode, Termination}, @@ -295,6 +296,7 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> { error!("No viable install path found of specified, try `--install-path`"); miette!("No install path found or specified") })?; + fs::create_dir_all(&install_path).map_err(BinstallError::Io)?; debug!("Using install path: {}", install_path.display()); // Create a temporary directory for downloads etc. @@ -393,6 +395,10 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> { block_in_place(|| { if !custom_install_path { + // If using standardised install path, + // then create_dir_all(&install_path) would also + // create .cargo. + debug!("Writing .crates.toml"); metafiles::v1::CratesToml::append(metadata_vec.iter())?; From 71566383db36188fb99a158e1feeb4f1792e6fa0 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Thu, 4 Aug 2022 14:08:06 +1000 Subject: [PATCH 2/2] Fix creating `.cargo/.crates.toml` in `metadata::v1` Signed-off-by: Jiahao XU --- src/metafiles/v1.rs | 35 ++++++++++++++++++++++++++++++++++- 1 file changed, 34 insertions(+), 1 deletion(-) diff --git a/src/metafiles/v1.rs b/src/metafiles/v1.rs index 25ef6c3f0..a48575301 100644 --- a/src/metafiles/v1.rs +++ b/src/metafiles/v1.rs @@ -74,7 +74,11 @@ impl CratesToml { Iter: IntoIterator, { let mut file = FileLock::new_exclusive(create_if_not_exist(path.as_ref())?)?; - let mut c1 = Self::load_from_reader(&mut *file)?; + let mut c1 = if file.metadata()?.len() != 0 { + Self::load_from_reader(&mut *file)? + } else { + Self::default() + }; for metadata in iter { c1.insert(&CrateVersionSource::from(metadata), metadata.bins.clone()); @@ -108,3 +112,32 @@ pub enum CratesTomlParseError { #[error(transparent)] CvsParse(#[from] super::CvsParseError), } + +#[cfg(test)] +mod tests { + use super::{super::binstall_v1, *}; + use crate::target::TARGET; + + use semver::Version; + use tempfile::TempDir; + + #[test] + fn test_empty() { + let tempdir = TempDir::new().unwrap(); + let path = tempdir.path().join("crates-v1.toml"); + + CratesToml::append_to_path( + &path, + &[MetaData { + name: "cargo-binstall".into(), + version_req: "*".into(), + current_version: Version::new(0, 11, 1), + source: binstall_v1::Source::cratesio_registry(), + target: TARGET.into(), + bins: vec!["cargo-binstall".into()], + other: Default::default(), + }], + ) + .unwrap(); + } +}