diff --git a/src/diskio/mod.rs b/src/diskio/mod.rs index 67f99a635d5..69db45f3723 100644 --- a/src/diskio/mod.rs +++ b/src/diskio/mod.rs @@ -59,6 +59,7 @@ use std::io::{self, Write}; use std::path::{Path, PathBuf}; use std::time::{Duration, Instant}; +use crate::errors::{ErrorKind, Result, ResultExt}; use crate::process; use crate::utils::notifications::Notification; @@ -194,21 +195,16 @@ pub fn create_dir>(path: P) -> io::Result<()> { /// Get the executor for disk IO. pub fn get_executor<'a>( notify_handler: Option<&'a dyn Fn(Notification<'_>)>, -) -> Box { +) -> Result> { // If this gets lots of use, consider exposing via the config file. - let thread_count = match process() - .var("RUSTUP_IO_THREADS") - .map(|x| x.parse::()) - { - Ok(Ok(n)) => n, - Ok(Err(_)) => { - eprintln!("Invalid value for RUSTUP_IO_THREADS. Has to be an integer"); - num_cpus::get() - } + let thread_count = match process().var("RUSTUP_IO_THREADS") { Err(_) => num_cpus::get(), + Ok(n) => n + .parse::() + .chain_err(|| ErrorKind::InvalidIoThreadsCount)?, }; match thread_count { - 0 | 1 => Box::new(immediate::ImmediateUnpacker::new()), - thread_count => Box::new(threaded::Threaded::new(notify_handler, thread_count)), + 0 | 1 => Ok(Box::new(immediate::ImmediateUnpacker::new())), + n => Ok(Box::new(threaded::Threaded::new(notify_handler, n))), } } diff --git a/src/dist/component/package.rs b/src/dist/component/package.rs index 02469a99896..3beddf54fe6 100644 --- a/src/dist/component/package.rs +++ b/src/dist/component/package.rs @@ -293,7 +293,7 @@ fn unpack_without_first_dir<'a, R: Read>( path: &Path, notify_handler: Option<&'a dyn Fn(Notification<'_>)>, ) -> Result<()> { - let mut io_executor: Box = get_executor(notify_handler); + let mut io_executor: Box = get_executor(notify_handler)?; let entries = archive .entries() .chain_err(|| ErrorKind::ExtractingPackage)?; diff --git a/src/errors.rs b/src/errors.rs index e25b05ddbcb..8253b8a0589 100644 --- a/src/errors.rs +++ b/src/errors.rs @@ -375,6 +375,9 @@ error_chain! { BrokenPartialFile { description("partially downloaded file may have been damaged and was removed, please try again") } + InvalidIoThreadsCount { + description("invalid value in RUSTUP_IO_THREADS. Must be a natural number") + } } }