From 3630b844e70772c17ba3b225d731b2984966a42c Mon Sep 17 00:00:00 2001 From: Riccardo Strina Date: Mon, 10 Nov 2025 22:18:45 +0100 Subject: [PATCH] Expand usage of secure create_dir function --- src/debugger.rs | 2 +- src/jdtls.rs | 9 +++++---- src/util.rs | 3 ++- 3 files changed, 8 insertions(+), 6 deletions(-) diff --git a/src/debugger.rs b/src/debugger.rs index 7c3ed82..c6b98ca 100644 --- a/src/debugger.rs +++ b/src/debugger.rs @@ -210,7 +210,7 @@ impl Debugger { language_server_id, &LanguageServerInstallationStatus::Downloading, ); - fs::create_dir(prefix).map_err(|err| err.to_string())?; + create_path_if_not_exists(prefix)?; let url = format!( "https://repo1.maven.org/maven2/com/microsoft/java/{artifact}/{latest_version}/{jar_name}" diff --git a/src/jdtls.rs b/src/jdtls.rs index 338ba45..e48f223 100644 --- a/src/jdtls.rs +++ b/src/jdtls.rs @@ -1,6 +1,6 @@ use std::{ env::current_dir, - fs::{create_dir, metadata, read_dir}, + fs::{metadata, read_dir}, path::{Path, PathBuf}, }; @@ -18,8 +18,9 @@ use crate::{ config::is_java_autodownload, jdk::try_to_fetch_and_install_latest_jdk, util::{ - get_curr_dir, get_java_exec_name, get_java_executable, get_java_major_version, - get_latest_versions_from_tag, path_to_string, remove_all_files_except, + create_path_if_not_exists, get_curr_dir, get_java_exec_name, get_java_executable, + get_java_major_version, get_latest_versions_from_tag, path_to_string, + remove_all_files_except, }, }; @@ -219,7 +220,7 @@ pub fn try_to_fetch_and_install_latest_lombok( language_server_id, &LanguageServerInstallationStatus::Downloading, ); - create_dir(prefix).map_err(|err| err.to_string())?; + create_path_if_not_exists(prefix)?; download_file( &format!("https://projectlombok.org/downloads/{jar_name}"), path_to_string(jar_path.clone())?.as_str(), diff --git a/src/util.rs b/src/util.rs index 645d859..b01bb2c 100644 --- a/src/util.rs +++ b/src/util.rs @@ -28,6 +28,7 @@ const JAVA_EXEC_NOT_FOUND_ERROR: &str = "Could not find Java executable in JAVA_ const TAG_RETRIEVAL_ERROR: &str = "Failed to fetch GitHub tags"; const TAG_RESPONSE_ERROR: &str = "Failed to deserialize GitHub tags response"; const TAG_UNEXPECTED_FORMAT_ERROR: &str = "Malformed GitHub tags response"; +const PATH_IS_NOT_DIR: &str = "File exists but is not a path"; /// Create a Path if it does not exist /// @@ -46,7 +47,7 @@ pub fn create_path_if_not_exists>(path: P) -> zed::Result<()> { if metadata.is_dir() { Ok(()) } else { - Err(format!("File exists but is not a path: {:?}", path_ref)) + Err(format!("{PATH_IS_NOT_DIR}: {:?}", path_ref)) } } Err(e) if e.kind() == std::io::ErrorKind::NotFound => {