diff --git a/rust/src/files.rs b/rust/src/files.rs index 49bb95f3794b39..fe74af29892c81 100644 --- a/rust/src/files.rs +++ b/rust/src/files.rs @@ -69,9 +69,13 @@ impl BrowserPath { } } -pub fn create_parent_path_if_not_exists(path: &Path) -> Result<(), Box> { +pub fn create_empty_parent_path_if_not_exists(path: &Path) -> Result<(), Box> { if let Some(p) = path.parent() { create_path_if_not_exists(p)?; + if p.to_path_buf().read_dir()?.next().is_some() { + // If not empty + fs::remove_dir_all(p).and_then(|_| fs::create_dir(p))?; + } } Ok(()) } @@ -183,7 +187,7 @@ pub fn uncompress_sfx( "Moving extracted files and folders from {} to {}", core_str, target_str )); - create_parent_path_if_not_exists(target)?; + create_empty_parent_path_if_not_exists(target)?; fs::rename(&core_str, &target_str)?; Ok(()) @@ -293,7 +297,7 @@ pub fn uncompress_deb( "Moving extracted files and folders from {} to {}", opt_edge_str, target_str )); - create_parent_path_if_not_exists(target)?; + create_empty_parent_path_if_not_exists(target)?; fs::rename(&opt_edge_str, &target_str)?; Ok(()) @@ -406,7 +410,7 @@ pub fn unzip( fs::create_dir_all(&tmp_path)?; } else { let target_path = tmp_path.join(path.clone()); - create_parent_path_if_not_exists(target_path.as_path())?; + create_empty_parent_path_if_not_exists(target_path.as_path())?; let mut outfile = File::create(&target_path)?; // Set permissions in Unix-like systems diff --git a/rust/src/lib.rs b/rust/src/lib.rs index 9764eaea377855..14ad6bf2ccee9e 100644 --- a/rust/src/lib.rs +++ b/rust/src/lib.rs @@ -18,7 +18,7 @@ use crate::chrome::{ChromeManager, CHROMEDRIVER_NAME, CHROME_NAME}; use crate::edge::{EdgeManager, EDGEDRIVER_NAME, EDGE_NAMES}; use crate::files::{ - create_parent_path_if_not_exists, create_path_if_not_exists, default_cache_folder, + create_empty_parent_path_if_not_exists, create_path_if_not_exists, default_cache_folder, get_binary_extension, path_buf_to_string, }; use crate::firefox::{FirefoxManager, FIREFOX_NAME, GECKODRIVER_NAME}; @@ -190,7 +190,7 @@ pub trait SeleniumManager { if self.is_grid() { let driver_path_in_cache = self.get_driver_path_in_cache()?; - create_parent_path_if_not_exists(&driver_path_in_cache)?; + create_empty_parent_path_if_not_exists(&driver_path_in_cache)?; Ok(fs::rename(driver_zip_file, driver_path_in_cache)?) } else { let driver_path_in_cache = self.get_driver_path_in_cache()?;