diff --git a/sdk/cargo-build-bpf/src/main.rs b/sdk/cargo-build-bpf/src/main.rs index 43388d0d99..15c6567ddd 100644 --- a/sdk/cargo-build-bpf/src/main.rs +++ b/sdk/cargo-build-bpf/src/main.rs @@ -91,10 +91,10 @@ fn install_if_missing( package: &str, version: &str, url: &str, - file: &Path, + download_file_name: &str, ) -> Result<(), String> { // Check whether the package is already in ~/.cache/solana. - // Donwload it and place in the proper location if not found. + // Download it and place in the proper location if not found. let home_dir = PathBuf::from(env::var("HOME").unwrap_or_else(|err| { eprintln!("Can't get home directory path: {}", err); exit(1); @@ -114,20 +114,24 @@ fn install_if_missing( if target_path.exists() { fs::remove_file(&target_path).map_err(|err| err.to_string())?; } + fs::create_dir_all(&target_path).map_err(|err| err.to_string())?; let mut url = String::from(url); url.push('/'); url.push_str(version); url.push('/'); - url.push_str(file.to_str().unwrap()); - download_file(url.as_str(), file, true, &mut None)?; - fs::create_dir_all(&target_path).map_err(|err| err.to_string())?; - let zip = File::open(&file).map_err(|err| err.to_string())?; + url.push_str(download_file_name); + let download_file_path = target_path.join(download_file_name); + if download_file_path.exists() { + fs::remove_file(&download_file_path).map_err(|err| err.to_string())?; + } + download_file(url.as_str(), &download_file_path, true, &mut None)?; + let zip = File::open(&download_file_path).map_err(|err| err.to_string())?; let tar = BzDecoder::new(BufReader::new(zip)); let mut archive = Archive::new(tar); archive .unpack(&target_path) .map_err(|err| err.to_string())?; - fs::remove_file(file).map_err(|err| err.to_string())?; + fs::remove_file(download_file_path).map_err(|err| err.to_string())?; } // Make a symbolic link source_path -> target_path in the // sdk/bpf/dependencies directory if no valid link found. @@ -137,7 +141,17 @@ fn install_if_missing( } let source_path = source_base.join(package); // Check whether the correct symbolic link exists. - if source_path.read_link().is_err() { + let invalid_link = if let Ok(link_target) = source_path.read_link() { + if link_target != target_path { + fs::remove_file(&source_path).map_err(|err| err.to_string())?; + true + } else { + false + } + } else { + true + }; + if invalid_link { #[cfg(unix)] std::os::unix::fs::symlink(target_path, source_path).map_err(|err| err.to_string())?; #[cfg(windows)] @@ -411,7 +425,7 @@ fn build_bpf_package(config: &Config, target_directory: &Path, package: &cargo_m if legacy_program_feature_present { println!("Legacy program feature detected"); } - let bpf_tools_filename = if cfg!(target_os = "macos") { + let bpf_tools_download_file_name = if cfg!(target_os = "macos") { "solana-bpf-tools-osx.tar.bz2" } else { "solana-bpf-tools-linux.tar.bz2" @@ -421,7 +435,7 @@ fn build_bpf_package(config: &Config, target_directory: &Path, package: &cargo_m "bpf-tools", "v1.9", "https://github.com/solana-labs/bpf-tools/releases/download", - &PathBuf::from(bpf_tools_filename), + bpf_tools_download_file_name, ) .expect("Failed to install bpf-tools"); link_bpf_toolchain(config);