Make cargo-build-bpf clean up after failed installation of bpf-tools (#21143)
This commit is contained in:
		@@ -1400,7 +1400,7 @@ fn assert_instruction_count() {
 | 
				
			|||||||
            ("solana_bpf_rust_noop", 480),
 | 
					            ("solana_bpf_rust_noop", 480),
 | 
				
			||||||
            ("solana_bpf_rust_param_passing", 146),
 | 
					            ("solana_bpf_rust_param_passing", 146),
 | 
				
			||||||
            ("solana_bpf_rust_rand", 487),
 | 
					            ("solana_bpf_rust_rand", 487),
 | 
				
			||||||
            ("solana_bpf_rust_sanity", 8397),
 | 
					            ("solana_bpf_rust_sanity", 8406),
 | 
				
			||||||
            ("solana_bpf_rust_secp256k1_recover", 25216),
 | 
					            ("solana_bpf_rust_secp256k1_recover", 25216),
 | 
				
			||||||
            ("solana_bpf_rust_sha", 30704),
 | 
					            ("solana_bpf_rust_sha", 30704),
 | 
				
			||||||
        ]);
 | 
					        ]);
 | 
				
			||||||
 
 | 
				
			|||||||
@@ -119,19 +119,8 @@ fn install_if_missing(
 | 
				
			|||||||
    version: &str,
 | 
					    version: &str,
 | 
				
			||||||
    url: &str,
 | 
					    url: &str,
 | 
				
			||||||
    download_file_name: &str,
 | 
					    download_file_name: &str,
 | 
				
			||||||
 | 
					    target_path: &Path,
 | 
				
			||||||
) -> Result<(), String> {
 | 
					) -> 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
 | 
					    // Check whether the target path is an empty directory. This can
 | 
				
			||||||
    // happen if package download failed on previous run of
 | 
					    // happen if package download failed on previous run of
 | 
				
			||||||
    // cargo-build-bpf.  Remove the target_path directory in this
 | 
					    // 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())?;
 | 
					        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()
 | 
					    if !target_path.is_dir()
 | 
				
			||||||
        && !target_path
 | 
					        && !target_path
 | 
				
			||||||
            .symlink_metadata()
 | 
					            .symlink_metadata()
 | 
				
			||||||
@@ -183,7 +174,7 @@ fn install_if_missing(
 | 
				
			|||||||
    let source_path = source_base.join(package);
 | 
					    let source_path = source_base.join(package);
 | 
				
			||||||
    // Check whether the correct symbolic link exists.
 | 
					    // Check whether the correct symbolic link exists.
 | 
				
			||||||
    let invalid_link = if let Ok(link_target) = source_path.read_link() {
 | 
					    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())?;
 | 
					            fs::remove_file(&source_path).map_err(|err| err.to_string())?;
 | 
				
			||||||
            true
 | 
					            true
 | 
				
			||||||
        } else {
 | 
					        } else {
 | 
				
			||||||
@@ -477,14 +468,41 @@ fn build_bpf_package(config: &Config, target_directory: &Path, package: &cargo_m
 | 
				
			|||||||
    } else {
 | 
					    } else {
 | 
				
			||||||
        "solana-bpf-tools-linux.tar.bz2"
 | 
					        "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(
 | 
					    install_if_missing(
 | 
				
			||||||
        config,
 | 
					        config,
 | 
				
			||||||
        "bpf-tools",
 | 
					        package,
 | 
				
			||||||
        "v1.18",
 | 
					        version,
 | 
				
			||||||
        "https://github.com/solana-labs/bpf-tools/releases/download",
 | 
					        "https://github.com/solana-labs/bpf-tools/releases/download",
 | 
				
			||||||
        bpf_tools_download_file_name,
 | 
					        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);
 | 
					    link_bpf_toolchain(config);
 | 
				
			||||||
 | 
					
 | 
				
			||||||
    let llvm_bin = config
 | 
					    let llvm_bin = config
 | 
				
			||||||
 
 | 
				
			|||||||
		Reference in New Issue
	
	Block a user