From 62317b63f41046d9b385355ef934a9591a7fd7a5 Mon Sep 17 00:00:00 2001 From: Jiahao XU Date: Fri, 5 Aug 2022 00:26:17 +1000 Subject: [PATCH] Refactor: Run `TempDir` creation in `block_in_place` Since it could also issues blocking operations. Signed-off-by: Jiahao XU --- src/main.rs | 28 ++++++++++++++-------------- 1 file changed, 14 insertions(+), 14 deletions(-) diff --git a/src/main.rs b/src/main.rs index dbf0d09ef..cbda83fc8 100644 --- a/src/main.rs +++ b/src/main.rs @@ -324,8 +324,8 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> { // Initialize UI thread let mut uithread = UIThread::new(!opts.no_confirm); - // Compute install directory - let (install_path, metadata) = block_in_place(|| -> Result<_> { + let (install_path, metadata, temp_dir) = block_in_place(|| -> Result<_> { + // Compute install directory let (install_path, custom_install_path) = get_install_path(opts.install_path.as_deref()); let install_path = install_path.ok_or_else(|| { error!("No viable install path found of specified, try `--install-path`"); @@ -342,7 +342,18 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> { None }; - Ok((install_path, metadata)) + // Create a temporary directory for downloads etc. + // + // Put all binaries to a temporary directory under `dst` first, catching + // some failure modes (e.g., out of space) before touching the existing + // binaries. This directory will get cleaned up via RAII. + let temp_dir = tempfile::Builder::new() + .prefix("cargo-binstall") + .tempdir_in(&install_path) + .map_err(BinstallError::from) + .wrap_err("Creating a temporary directory failed.")?; + + Ok((install_path, metadata, temp_dir)) })?; // Remove installed crates @@ -356,17 +367,6 @@ async fn entry(jobserver_client: LazyJobserverClient) -> Result<()> { } }); - // Create a temporary directory for downloads etc. - // - // Put all binaries to a temporary directory under `dst` first, catching - // some failure modes (e.g., out of space) before touching the existing - // binaries. This directory will get cleaned up via RAII. - let temp_dir = tempfile::Builder::new() - .prefix("cargo-binstall") - .tempdir_in(&install_path) - .map_err(BinstallError::from) - .wrap_err("Creating a temporary directory failed.")?; - let temp_dir_path: Arc = Arc::from(temp_dir.path()); // Create binstall_opts