From 9f3017742e41222b69f022fb61d33f07dfab4f80 Mon Sep 17 00:00:00 2001 From: daimond113 <72147841+daimond113@users.noreply.github.com> Date: Mon, 11 Nov 2024 18:57:44 +0100 Subject: [PATCH] fix: correct cas finding algorithm --- CHANGELOG.md | 2 ++ src/main.rs | 22 ++++++++++++++++++++-- 2 files changed, 22 insertions(+), 2 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 7401031..19f4a10 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -13,6 +13,8 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0 - Use a different algorithm for finding a CAS directory to avoid issues with mounted drives by @daimond113 - Remove default.project.json from Git pesde dependencies by @daimond113 - Correctly (de)serialize workspace specifiers by @daimond113 +- Fix CAS finder algorithm issues with Windows by @daimond113 +- Fix CAS finder algorithm's AlreadyExists error by @daimond113 ### Changed - Switched to fs-err for better errors with file system operations by @daimond113 diff --git a/src/main.rs b/src/main.rs index 8687bfe..eb7b137 100644 --- a/src/main.rs +++ b/src/main.rs @@ -36,9 +36,27 @@ struct Cli { async fn get_linkable_dir(path: &Path) -> PathBuf { let mut curr_path = PathBuf::new(); let file_to_try = NamedTempFile::new_in(path).expect("failed to create temporary file"); - let temp_file_name = file_to_try.path().file_name().unwrap(); - for component in path.components() { + let temp_path = tempfile::Builder::new() + .make(|_| Ok(())) + .expect("failed to create temporary file") + .into_temp_path(); + let temp_file_name = temp_path.file_name().expect("failed to get file name"); + + // C: and \ are different components on Windows + #[cfg(windows)] + let components = path.components().map(|c| { + let mut path = c.as_os_str().to_os_string(); + if let std::path::Component::Prefix(_) = c { + path.push(std::path::MAIN_SEPARATOR_STR); + } + + path + }); + #[cfg(not(windows))] + let components = path.components().map(|c| c.as_os_str().to_os_string()); + + for component in components { curr_path.push(component); let try_path = curr_path.join(temp_file_name);