Skip to content

Commit 7856997

Browse files
committed
dist: hoist environment variable extraction
1 parent e452927 commit 7856997

File tree

1 file changed

+17
-10
lines changed

1 file changed

+17
-10
lines changed

src/dist/component/package.rs

Lines changed: 17 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,10 @@
44
55
use std::collections::{HashMap, HashSet};
66
use std::io::{self, ErrorKind as IOErrorKind, Read};
7+
use std::mem;
78
use std::ops::Deref;
89
use std::path::{Path, PathBuf};
910
use std::sync::OnceLock;
10-
use std::{env, mem};
1111

1212
use anyhow::{Context, Result, anyhow, bail};
1313
use tar::EntryType;
@@ -52,11 +52,17 @@ impl DirectoryPackage<temp::Dir> {
5252
fn from_tar(stream: impl Read, dl_cfg: &DownloadCfg<'_>) -> Result<Self> {
5353
let temp_dir = dl_cfg.tmp_cx.new_directory()?;
5454
let mut archive = tar::Archive::new(stream);
55+
5556
// The rust-installer packages unpack to a directory called
5657
// $pkgname-$version-$target. Skip that directory when
5758
// unpacking.
58-
unpack_without_first_dir(&mut archive, &temp_dir, dl_cfg)
59-
.context("failed to extract package")?;
59+
unpack_without_first_dir(
60+
&mut archive,
61+
&temp_dir,
62+
dl_cfg.process.unpack_ram()?,
63+
dl_cfg.process.io_thread_count()?,
64+
)
65+
.context("failed to extract package")?;
6066

6167
Self::new(temp_dir, false)
6268
}
@@ -148,8 +154,8 @@ impl<P: Deref<Target = Path>> DirectoryPackage<P> {
148154
fn unpack_ram(
149155
io_chunk_size: usize,
150156
effective_max_ram: Option<usize>,
151-
dl_cfg: &DownloadCfg<'_>,
152-
) -> Result<usize, env::VarError> {
157+
budget: Option<usize>,
158+
) -> usize {
153159
const RAM_ALLOWANCE_FOR_RUSTUP_AND_BUFFERS: usize = 200 * 1024 * 1024;
154160
let minimum_ram = io_chunk_size * 2;
155161
let default_max_unpack_ram = if let Some(effective_max_ram) = effective_max_ram {
@@ -162,7 +168,7 @@ fn unpack_ram(
162168
// Rustup does not know how much RAM the machine has: use the minimum
163169
minimum_ram
164170
};
165-
let unpack_ram = match dl_cfg.process.unpack_ram()? {
171+
let unpack_ram = match budget {
166172
Some(budget) => {
167173
if budget < minimum_ram {
168174
warn!(
@@ -191,7 +197,7 @@ fn unpack_ram(
191197
if minimum_ram > unpack_ram {
192198
panic!("RUSTUP_UNPACK_RAM must be larger than {minimum_ram}");
193199
} else {
194-
Ok(unpack_ram)
200+
unpack_ram
195201
}
196202
}
197203

@@ -269,7 +275,8 @@ enum DirStatus {
269275
fn unpack_without_first_dir<R: Read>(
270276
archive: &mut tar::Archive<R>,
271277
path: &Path,
272-
dl_cfg: &DownloadCfg<'_>,
278+
unpack_ram_budget: Option<usize>,
279+
io_thread_count: usize,
273280
) -> Result<()> {
274281
let entries = archive.entries()?;
275282
let effective_max_ram = match effective_limits::memory_limit() {
@@ -279,8 +286,8 @@ fn unpack_without_first_dir<R: Read>(
279286
None
280287
}
281288
};
282-
let unpack_ram = unpack_ram(IO_CHUNK_SIZE, effective_max_ram, dl_cfg)?;
283-
let mut io_executor = get_executor(unpack_ram, dl_cfg.process.io_thread_count()?);
289+
let unpack_ram = unpack_ram(IO_CHUNK_SIZE, effective_max_ram, unpack_ram_budget);
290+
let mut io_executor = get_executor(unpack_ram, io_thread_count);
284291

285292
let mut directories: HashMap<PathBuf, DirStatus> = HashMap::new();
286293
// Path is presumed to exist. Call it a precondition.

0 commit comments

Comments
 (0)