Skip to content

Commit 25c72d8

Browse files
committed
dist: deduplicate decompression setup code
1 parent e9246d0 commit 25c72d8

File tree

2 files changed

+43
-78
lines changed

2 files changed

+43
-78
lines changed

src/dist/component/package.rs

Lines changed: 25 additions & 54 deletions
Original file line numberDiff line numberDiff line change
@@ -140,18 +140,13 @@ impl Package for DirectoryPackage {
140140
pub(crate) struct TarPackage<'a>(DirectoryPackage, temp::Dir<'a>);
141141

142142
impl<'a> TarPackage<'a> {
143-
pub(crate) fn new<R: Read>(
144-
stream: R,
145-
tmp_cx: &'a temp::Context,
146-
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
147-
process: &Process,
148-
) -> Result<Self> {
149-
let temp_dir = tmp_cx.new_directory()?;
143+
pub(crate) fn new<R: Read>(stream: R, cx: &PackageContext<'a>) -> Result<Self> {
144+
let temp_dir = cx.tmp_cx.new_directory()?;
150145
let mut archive = tar::Archive::new(stream);
151146
// The rust-installer packages unpack to a directory called
152147
// $pkgname-$version-$target. Skip that directory when
153148
// unpacking.
154-
unpack_without_first_dir(&mut archive, &temp_dir, notify_handler, process)
149+
unpack_without_first_dir(&mut archive, &temp_dir, cx)
155150
.context("failed to extract package")?;
156151

157152
Ok(TarPackage(
@@ -165,8 +160,7 @@ impl<'a> TarPackage<'a> {
165160
fn unpack_ram(
166161
io_chunk_size: usize,
167162
effective_max_ram: Option<usize>,
168-
notify_handler: Option<&dyn Fn(Notification<'_>)>,
169-
process: &Process,
163+
cx: &PackageContext<'_>,
170164
) -> usize {
171165
const RAM_ALLOWANCE_FOR_RUSTUP_AND_BUFFERS: usize = 200 * 1024 * 1024;
172166
let minimum_ram = io_chunk_size * 2;
@@ -180,7 +174,8 @@ fn unpack_ram(
180174
// Rustup does not know how much RAM the machine has: use the minimum
181175
minimum_ram
182176
};
183-
let unpack_ram = match process
177+
let unpack_ram = match cx
178+
.process
184179
.var("RUSTUP_UNPACK_RAM")
185180
.ok()
186181
.and_then(|budget_str| budget_str.parse::<usize>().ok())
@@ -203,7 +198,7 @@ fn unpack_ram(
203198
}
204199
}
205200
None => {
206-
if let Some(h) = notify_handler {
201+
if let Some(h) = cx.notify_handler {
207202
h(Notification::SetDefaultBufferSize(default_max_unpack_ram))
208203
}
209204
default_max_unpack_ram
@@ -289,21 +284,21 @@ enum DirStatus {
289284
fn unpack_without_first_dir<R: Read>(
290285
archive: &mut tar::Archive<R>,
291286
path: &Path,
292-
notify_handler: Option<&dyn Fn(Notification<'_>)>,
293-
process: &Process,
287+
cx: &PackageContext<'_>,
294288
) -> Result<()> {
295289
let entries = archive.entries()?;
296290
let effective_max_ram = match effective_limits::memory_limit() {
297291
Ok(ram) => Some(ram as usize),
298292
Err(e) => {
299-
if let Some(h) = notify_handler {
293+
if let Some(h) = cx.notify_handler {
300294
h(Notification::Error(e.to_string()))
301295
}
302296
None
303297
}
304298
};
305-
let unpack_ram = unpack_ram(IO_CHUNK_SIZE, effective_max_ram, notify_handler, process);
306-
let mut io_executor: Box<dyn Executor> = get_executor(notify_handler, unpack_ram, process)?;
299+
let unpack_ram = unpack_ram(IO_CHUNK_SIZE, effective_max_ram, cx);
300+
let mut io_executor: Box<dyn Executor> =
301+
get_executor(cx.notify_handler, unpack_ram, cx.process)?;
307302

308303
let mut directories: HashMap<PathBuf, DirStatus> = HashMap::new();
309304
// Path is presumed to exist. Call it a precondition.
@@ -463,7 +458,7 @@ fn unpack_without_first_dir<R: Read>(
463458
// Tar has item before containing directory
464459
// Complain about this so we can see if these exist.
465460
writeln!(
466-
process.stderr().lock(),
461+
cx.process.stderr().lock(),
467462
"Unexpected: missing parent '{}' for '{}'",
468463
parent.display(),
469464
entry.path()?.display()
@@ -554,19 +549,9 @@ impl Package for TarPackage<'_> {
554549
pub(crate) struct TarGzPackage<'a>(TarPackage<'a>);
555550

556551
impl<'a> TarGzPackage<'a> {
557-
pub(crate) fn new<R: Read>(
558-
stream: R,
559-
tmp_cx: &'a temp::Context,
560-
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
561-
process: &Process,
562-
) -> Result<Self> {
552+
pub(crate) fn new<R: Read>(stream: R, cx: &PackageContext<'a>) -> Result<Self> {
563553
let stream = flate2::read::GzDecoder::new(stream);
564-
Ok(TarGzPackage(TarPackage::new(
565-
stream,
566-
tmp_cx,
567-
notify_handler,
568-
process,
569-
)?))
554+
Ok(TarGzPackage(TarPackage::new(stream, cx)?))
570555
}
571556
}
572557

@@ -592,19 +577,9 @@ impl Package for TarGzPackage<'_> {
592577
pub(crate) struct TarXzPackage<'a>(TarPackage<'a>);
593578

594579
impl<'a> TarXzPackage<'a> {
595-
pub(crate) fn new<R: Read>(
596-
stream: R,
597-
tmp_cx: &'a temp::Context,
598-
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
599-
process: &Process,
600-
) -> Result<Self> {
580+
pub(crate) fn new<R: Read>(stream: R, cx: &PackageContext<'a>) -> Result<Self> {
601581
let stream = xz2::read::XzDecoder::new(stream);
602-
Ok(TarXzPackage(TarPackage::new(
603-
stream,
604-
tmp_cx,
605-
notify_handler,
606-
process,
607-
)?))
582+
Ok(TarXzPackage(TarPackage::new(stream, cx)?))
608583
}
609584
}
610585

@@ -630,19 +605,9 @@ impl Package for TarXzPackage<'_> {
630605
pub(crate) struct TarZStdPackage<'a>(TarPackage<'a>);
631606

632607
impl<'a> TarZStdPackage<'a> {
633-
pub(crate) fn new<R: Read>(
634-
stream: R,
635-
tmp_cx: &'a temp::Context,
636-
notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
637-
process: &Process,
638-
) -> Result<Self> {
608+
pub(crate) fn new<R: Read>(stream: R, cx: &PackageContext<'a>) -> Result<Self> {
639609
let stream = zstd::stream::read::Decoder::new(stream)?;
640-
Ok(TarZStdPackage(TarPackage::new(
641-
stream,
642-
tmp_cx,
643-
notify_handler,
644-
process,
645-
)?))
610+
Ok(TarZStdPackage(TarPackage::new(stream, cx)?))
646611
}
647612
}
648613

@@ -663,3 +628,9 @@ impl Package for TarZStdPackage<'_> {
663628
self.0.components()
664629
}
665630
}
631+
632+
pub(crate) struct PackageContext<'a> {
633+
pub(crate) tmp_cx: &'a temp::Context,
634+
pub(crate) notify_handler: Option<&'a dyn Fn(Notification<'_>)>,
635+
pub(crate) process: &'a Process,
636+
}

src/dist/manifestation.rs

Lines changed: 18 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ use tokio::sync::Semaphore;
1313
use tracing::info;
1414

1515
use crate::dist::component::{
16-
Components, Package, TarGzPackage, TarXzPackage, TarZStdPackage, Transaction,
16+
Components, Package, PackageContext, TarGzPackage, TarXzPackage, TarZStdPackage, Transaction,
1717
};
1818
use crate::dist::config::Config;
1919
use crate::dist::download::{DownloadCfg, File};
@@ -267,37 +267,27 @@ impl Manifestation {
267267
let notification_converter = |notification: crate::utils::Notification<'_>| {
268268
(download_cfg.notify_handler)(notification.into());
269269
};
270-
let gz;
271-
let xz;
272-
let zst;
270+
271+
let cx = PackageContext {
272+
tmp_cx,
273+
notify_handler: Some(&notification_converter),
274+
process: download_cfg.process,
275+
};
276+
277+
let (gz, xz, zst);
273278
let reader =
274279
utils::FileReaderWithProgress::new_file(&installer_file, &notification_converter)?;
275280
let package: &dyn Package = match format {
276281
CompressionKind::GZip => {
277-
gz = TarGzPackage::new(
278-
reader,
279-
tmp_cx,
280-
Some(&notification_converter),
281-
download_cfg.process,
282-
)?;
282+
gz = TarGzPackage::new(reader, &cx)?;
283283
&gz
284284
}
285285
CompressionKind::XZ => {
286-
xz = TarXzPackage::new(
287-
reader,
288-
tmp_cx,
289-
Some(&notification_converter),
290-
download_cfg.process,
291-
)?;
286+
xz = TarXzPackage::new(reader, &cx)?;
292287
&xz
293288
}
294289
CompressionKind::ZStd => {
295-
zst = TarZStdPackage::new(
296-
reader,
297-
tmp_cx,
298-
Some(&notification_converter),
299-
download_cfg.process,
300-
)?;
290+
zst = TarZStdPackage::new(reader, &cx)?;
301291
&zst
302292
}
303293
};
@@ -511,9 +501,13 @@ impl Manifestation {
511501
};
512502
let reader =
513503
utils::FileReaderWithProgress::new_file(&installer_file, &notification_converter)?;
514-
let package: &dyn Package =
515-
&TarGzPackage::new(reader, tmp_cx, Some(&notification_converter), process)?;
504+
let cx = PackageContext {
505+
tmp_cx,
506+
notify_handler: Some(&notification_converter),
507+
process,
508+
};
516509

510+
let package: &dyn Package = &TarGzPackage::new(reader, &cx)?;
517511
for component in package.components() {
518512
tx = package.install(&self.installation, &component, None, tx)?;
519513
}

0 commit comments

Comments
 (0)