Skip to content

Commit

Permalink
[rust] Ensure empty parent path before moving files
Browse files Browse the repository at this point in the history
  • Loading branch information
bonigarcia committed Sep 29, 2023
1 parent bc7df44 commit bdcf7e4
Show file tree
Hide file tree
Showing 2 changed files with 10 additions and 6 deletions.
12 changes: 8 additions & 4 deletions rust/src/files.rs
Original file line number Diff line number Diff line change
Expand Up @@ -69,9 +69,13 @@ impl BrowserPath {
}
}

pub fn create_parent_path_if_not_exists(path: &Path) -> Result<(), Box<dyn Error>> {
pub fn create_empty_parent_path_if_not_exists(path: &Path) -> Result<(), Box<dyn Error>> {
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(())
}
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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(())
Expand Down Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions rust/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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};
Expand Down Expand Up @@ -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()?;
Expand Down

0 comments on commit bdcf7e4

Please sign in to comment.