Skip to content

Commit

Permalink
Eliminate "text file busy" errors
Browse files Browse the repository at this point in the history
  • Loading branch information
smoelius committed Aug 27, 2024
1 parent 99c5de8 commit e7daf1a
Show file tree
Hide file tree
Showing 3 changed files with 17 additions and 3 deletions.
1 change: 1 addition & 0 deletions crates/svm-rs/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@ semver = { workspace = true, features = ["serde"] }
serde = { workspace = true, features = ["derive"] }
serde_json.workspace = true
sha2 = "0.10"
tempfile = "3.10"
thiserror = "1.0"
url = "2.5"

Expand Down
2 changes: 2 additions & 0 deletions crates/svm-rs/src/error.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,8 @@ pub enum SvmError {
#[error(transparent)]
IoError(#[from] std::io::Error),
#[error(transparent)]
PersistError(#[from] tempfile::PathPersistError),
#[error(transparent)]
ReqwestError(#[from] reqwest::Error),
#[error(transparent)]
SemverError(#[from] semver::Error),
Expand Down
17 changes: 14 additions & 3 deletions crates/svm-rs/src/install.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@ use std::{
process::Command,
time::Duration,
};
use tempfile::NamedTempFile;

#[cfg(target_family = "unix")]
use std::{fs::Permissions, os::unix::fs::PermissionsExt};
Expand Down Expand Up @@ -159,17 +160,27 @@ struct Installer<'a> {
impl Installer<'_> {
/// Installs the solc version at the version specific destination and returns the path to the installed solc file.
fn install(self) -> Result<PathBuf, SvmError> {
let solc_path = version_binary(&self.version.to_string());
let named_temp_file = NamedTempFile::new_in(data_dir())?;
let (mut f, temp_path) = named_temp_file.into_parts();

let mut f = fs::File::create(&solc_path)?;
#[cfg(target_family = "unix")]
f.set_permissions(Permissions::from_mode(0o755))?;
f.write_all(self.binbytes)?;

if platform::is_nixos() && *self.version >= NIXOS_MIN_PATCH_VERSION {
patch_for_nixos(&solc_path)?;
patch_for_nixos(&temp_path)?;
}

let solc_path = version_binary(&self.version.to_string());

// Windows requires that the old file be moved out of the way first.
if cfg!(target_os = "windows") {
let temp_path = NamedTempFile::new_in(data_dir()).map(NamedTempFile::into_temp_path)?;
fs::rename(&solc_path, &temp_path).unwrap_or_default();
}

temp_path.persist(&solc_path)?;

Ok(solc_path)
}

Expand Down

0 comments on commit e7daf1a

Please sign in to comment.