Skip to content

Commit 614399b

Browse files
committed
dist: replace PackageContext with DownloadCfg
1 parent f8dab60 commit 614399b

File tree

2 files changed

+22
-40
lines changed

2 files changed

+22
-40
lines changed

src/dist/component/package.rs

Lines changed: 17 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,9 @@ use tracing::{error, trace, warn};
1616
use crate::diskio::{CompletedIo, Executor, FileBuffer, IO_CHUNK_SIZE, Item, Kind, get_executor};
1717
use crate::dist::component::components::*;
1818
use crate::dist::component::transaction::*;
19-
use crate::dist::download::Notifier;
19+
use crate::dist::download::DownloadCfg;
2020
use crate::dist::temp;
2121
use crate::errors::*;
22-
use crate::process::Process;
2322
use crate::utils;
2423
use crate::utils::units::Size;
2524

@@ -143,13 +142,13 @@ impl Package for DirectoryPackage {
143142
pub(crate) struct TarPackage(DirectoryPackage, temp::Dir);
144143

145144
impl TarPackage {
146-
pub(crate) fn new<R: Read>(stream: R, cx: &PackageContext<'_>) -> Result<Self> {
147-
let temp_dir = cx.tmp_cx.new_directory()?;
145+
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg<'_>) -> Result<Self> {
146+
let temp_dir = dl_cfg.tmp_cx.new_directory()?;
148147
let mut archive = tar::Archive::new(stream);
149148
// The rust-installer packages unpack to a directory called
150149
// $pkgname-$version-$target. Skip that directory when
151150
// unpacking.
152-
unpack_without_first_dir(&mut archive, &temp_dir, cx)
151+
unpack_without_first_dir(&mut archive, &temp_dir, dl_cfg)
153152
.context("failed to extract package")?;
154153

155154
Ok(TarPackage(
@@ -163,7 +162,7 @@ impl TarPackage {
163162
fn unpack_ram(
164163
io_chunk_size: usize,
165164
effective_max_ram: Option<usize>,
166-
cx: &PackageContext<'_>,
165+
dl_cfg: &DownloadCfg<'_>,
167166
) -> usize {
168167
const RAM_ALLOWANCE_FOR_RUSTUP_AND_BUFFERS: usize = 200 * 1024 * 1024;
169168
let minimum_ram = io_chunk_size * 2;
@@ -177,7 +176,7 @@ fn unpack_ram(
177176
// Rustup does not know how much RAM the machine has: use the minimum
178177
minimum_ram
179178
};
180-
let unpack_ram = match cx
179+
let unpack_ram = match dl_cfg
181180
.process
182181
.var("RUSTUP_UNPACK_RAM")
183182
.ok()
@@ -289,7 +288,7 @@ enum DirStatus {
289288
fn unpack_without_first_dir<R: Read>(
290289
archive: &mut tar::Archive<R>,
291290
path: &Path,
292-
cx: &PackageContext<'_>,
291+
dl_cfg: &DownloadCfg<'_>,
293292
) -> Result<()> {
294293
let entries = archive.entries()?;
295294
let effective_max_ram = match effective_limits::memory_limit() {
@@ -299,8 +298,9 @@ fn unpack_without_first_dir<R: Read>(
299298
None
300299
}
301300
};
302-
let unpack_ram = unpack_ram(IO_CHUNK_SIZE, effective_max_ram, cx);
303-
let mut io_executor: Box<dyn Executor> = get_executor(cx.notifier, unpack_ram, cx.process)?;
301+
let unpack_ram = unpack_ram(IO_CHUNK_SIZE, effective_max_ram, dl_cfg);
302+
let mut io_executor: Box<dyn Executor> =
303+
get_executor(Some(&dl_cfg.notifier), unpack_ram, dl_cfg.process)?;
304304

305305
let mut directories: HashMap<PathBuf, DirStatus> = HashMap::new();
306306
// Path is presumed to exist. Call it a precondition.
@@ -461,7 +461,7 @@ fn unpack_without_first_dir<R: Read>(
461461
// Complain about this so we can see if these exist.
462462
use std::io::Write as _;
463463
writeln!(
464-
cx.process.stderr().lock(),
464+
dl_cfg.process.stderr().lock(),
465465
"Unexpected: missing parent '{}' for '{}'",
466466
parent.display(),
467467
entry.path()?.display()
@@ -552,9 +552,9 @@ impl Package for TarPackage {
552552
pub(crate) struct TarGzPackage(TarPackage);
553553

554554
impl TarGzPackage {
555-
pub(crate) fn new<R: Read>(stream: R, cx: &PackageContext<'_>) -> Result<Self> {
555+
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg<'_>) -> Result<Self> {
556556
let stream = flate2::read::GzDecoder::new(stream);
557-
Ok(TarGzPackage(TarPackage::new(stream, cx)?))
557+
Ok(TarGzPackage(TarPackage::new(stream, dl_cfg)?))
558558
}
559559
}
560560

@@ -580,9 +580,9 @@ impl Package for TarGzPackage {
580580
pub(crate) struct TarXzPackage(TarPackage);
581581

582582
impl TarXzPackage {
583-
pub(crate) fn new<R: Read>(stream: R, cx: &PackageContext<'_>) -> Result<Self> {
583+
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg<'_>) -> Result<Self> {
584584
let stream = xz2::read::XzDecoder::new(stream);
585-
Ok(TarXzPackage(TarPackage::new(stream, cx)?))
585+
Ok(TarXzPackage(TarPackage::new(stream, dl_cfg)?))
586586
}
587587
}
588588

@@ -608,9 +608,9 @@ impl Package for TarXzPackage {
608608
pub(crate) struct TarZStdPackage(TarPackage);
609609

610610
impl TarZStdPackage {
611-
pub(crate) fn new<R: Read>(stream: R, cx: &PackageContext<'_>) -> Result<Self> {
611+
pub(crate) fn new<R: Read>(stream: R, dl_cfg: &DownloadCfg<'_>) -> Result<Self> {
612612
let stream = zstd::stream::read::Decoder::new(stream)?;
613-
Ok(TarZStdPackage(TarPackage::new(stream, cx)?))
613+
Ok(TarZStdPackage(TarPackage::new(stream, dl_cfg)?))
614614
}
615615
}
616616

@@ -631,9 +631,3 @@ impl Package for TarZStdPackage {
631631
self.0.components()
632632
}
633633
}
634-
635-
pub(crate) struct PackageContext<'a> {
636-
pub(crate) tmp_cx: &'a temp::Context,
637-
pub(crate) notifier: Option<&'a Notifier>,
638-
pub(crate) process: &'a Process,
639-
}

src/dist/manifestation.rs

Lines changed: 5 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,7 @@ use tracing::{info, warn};
1414
use url::Url;
1515

1616
use crate::dist::component::{
17-
Components, Package, PackageContext, TarGzPackage, TarXzPackage, TarZStdPackage, Transaction,
17+
Components, Package, TarGzPackage, TarXzPackage, TarZStdPackage, Transaction,
1818
};
1919
use crate::dist::config::Config;
2020
use crate::dist::download::{DownloadCfg, File, Notification};
@@ -277,18 +277,12 @@ impl Manifestation {
277277
}
278278
}
279279

280-
let cx = PackageContext {
281-
tmp_cx,
282-
notifier: Some(&download_cfg.notifier),
283-
process: download_cfg.process,
284-
};
285-
286280
let reader =
287281
utils::FileReaderWithProgress::new_file(&installer_file, &download_cfg.notifier)?;
288282
let package = match format {
289-
CompressionKind::GZip => &TarGzPackage::new(reader, &cx)? as &dyn Package,
290-
CompressionKind::XZ => &TarXzPackage::new(reader, &cx)?,
291-
CompressionKind::ZStd => &TarZStdPackage::new(reader, &cx)?,
283+
CompressionKind::GZip => &TarGzPackage::new(reader, download_cfg)? as &dyn Package,
284+
CompressionKind::XZ => &TarXzPackage::new(reader, download_cfg)?,
285+
CompressionKind::ZStd => &TarZStdPackage::new(reader, download_cfg)?,
292286
};
293287

294288
// If the package doesn't contain the component that the
@@ -475,13 +469,7 @@ impl Manifestation {
475469

476470
// Install all the components in the installer
477471
let reader = utils::FileReaderWithProgress::new_file(&installer_file, &dl_cfg.notifier)?;
478-
let cx = PackageContext {
479-
tmp_cx: dl_cfg.tmp_cx,
480-
notifier: Some(&dl_cfg.notifier),
481-
process: dl_cfg.process,
482-
};
483-
484-
let package: &dyn Package = &TarGzPackage::new(reader, &cx)?;
472+
let package: &dyn Package = &TarGzPackage::new(reader, dl_cfg)?;
485473
for component in package.components() {
486474
tx = package.install(&self.installation, &component, None, tx)?;
487475
}

0 commit comments

Comments
 (0)