From 55d19e61dad8c6a12f95ffa26edb1af98687ba9b Mon Sep 17 00:00:00 2001 From: Dmitri Makarov Date: Wed, 3 Nov 2021 14:06:14 -0700 Subject: [PATCH] Make cargo-build-bpf clean up after failed installation of bpf-tools (#21143) --- programs/bpf/tests/programs.rs | 2 +- sdk/cargo-build-bpf/src/main.rs | 50 ++++++++++++++++++++++----------- 2 files changed, 35 insertions(+), 17 deletions(-) diff --git a/programs/bpf/tests/programs.rs b/programs/bpf/tests/programs.rs index 40296c7b76..39a10212d7 100644 --- a/programs/bpf/tests/programs.rs +++ b/programs/bpf/tests/programs.rs @@ -1400,7 +1400,7 @@ fn assert_instruction_count() { ("solana_bpf_rust_noop", 480), ("solana_bpf_rust_param_passing", 146), ("solana_bpf_rust_rand", 487), - ("solana_bpf_rust_sanity", 8397), + ("solana_bpf_rust_sanity", 8406), ("solana_bpf_rust_secp256k1_recover", 25216), ("solana_bpf_rust_sha", 30704), ]); diff --git a/sdk/cargo-build-bpf/src/main.rs b/sdk/cargo-build-bpf/src/main.rs index 927db40f15..533b83e0ae 100644 --- a/sdk/cargo-build-bpf/src/main.rs +++ b/sdk/cargo-build-bpf/src/main.rs @@ -119,19 +119,8 @@ fn install_if_missing( version: &str, url: &str, download_file_name: &str, + target_path: &Path, ) -> Result<(), String> { - // Check whether the package is already in ~/.cache/solana. - // 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); - })); - let target_path = home_dir - .join(".cache") - .join("solana") - .join(version) - .join(package); - // Check whether the target path is an empty directory. This can // happen if package download failed on previous run of // cargo-build-bpf. Remove the target_path directory in this @@ -146,6 +135,8 @@ fn install_if_missing( fs::remove_dir(&target_path).map_err(|err| err.to_string())?; } + // Check whether the package is already in ~/.cache/solana. + // Download it and place in the proper location if not found. if !target_path.is_dir() && !target_path .symlink_metadata() @@ -183,7 +174,7 @@ fn install_if_missing( let source_path = source_base.join(package); // Check whether the correct symbolic link exists. let invalid_link = if let Ok(link_target) = source_path.read_link() { - if link_target != target_path { + if link_target.ne(target_path) { fs::remove_file(&source_path).map_err(|err| err.to_string())?; true } else { @@ -477,14 +468,41 @@ fn build_bpf_package(config: &Config, target_directory: &Path, package: &cargo_m } else { "solana-bpf-tools-linux.tar.bz2" }; + + let home_dir = PathBuf::from(env::var("HOME").unwrap_or_else(|err| { + eprintln!("Can't get home directory path: {}", err); + exit(1); + })); + let version = "v1.18"; + let package = "bpf-tools"; + let target_path = home_dir + .join(".cache") + .join("solana") + .join(version) + .join(package); install_if_missing( config, - "bpf-tools", - "v1.18", + package, + version, "https://github.com/solana-labs/bpf-tools/releases/download", bpf_tools_download_file_name, + &target_path, ) - .expect("Failed to install bpf-tools"); + .unwrap_or_else(|err| { + // The package version directory doesn't contain a valid + // installation, and it should be removed. + let target_path_parent = target_path.parent().expect("Invalid package path"); + fs::remove_dir_all(&target_path_parent).unwrap_or_else(|err| { + eprintln!( + "Failed to remove {} while recovering from installation failure: {}", + target_path_parent.to_string_lossy(), + err, + ); + exit(1); + }); + eprintln!("Failed to install bpf-tools: {}", err); + exit(1); + }); link_bpf_toolchain(config); let llvm_bin = config