Skip to content

Commit e888668

Browse files
committed
dist: store owned temp::Context in Transaction
1 parent 6f277b6 commit e888668

File tree

7 files changed

+47
-37
lines changed

7 files changed

+47
-37
lines changed

src/dist/component/components.rs

Lines changed: 7 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -54,7 +54,7 @@ impl Components {
5454
Ok(None)
5555
}
5656
}
57-
fn write_version(&self, tx: &mut Transaction<'_>) -> Result<()> {
57+
fn write_version(&self, tx: &mut Transaction) -> Result<()> {
5858
tx.modify_file(self.prefix.rel_manifest_file(VERSION_FILE))?;
5959
utils::write_file(
6060
VERSION_FILE,
@@ -78,7 +78,7 @@ impl Components {
7878
})
7979
.collect())
8080
}
81-
pub(crate) fn add<'a>(&self, name: &str, tx: Transaction<'a>) -> ComponentBuilder<'a> {
81+
pub(crate) fn add(&self, name: &str, tx: Transaction) -> ComponentBuilder {
8282
ComponentBuilder {
8383
components: self.clone(),
8484
name: name.to_owned(),
@@ -95,14 +95,14 @@ impl Components {
9595
}
9696
}
9797

98-
pub(crate) struct ComponentBuilder<'a> {
98+
pub(crate) struct ComponentBuilder {
9999
components: Components,
100100
name: String,
101101
parts: Vec<ComponentPart>,
102-
tx: Transaction<'a>,
102+
tx: Transaction,
103103
}
104104

105-
impl<'a> ComponentBuilder<'a> {
105+
impl ComponentBuilder {
106106
pub(crate) fn copy_file(&mut self, path: PathBuf, src: &Path) -> Result<()> {
107107
self.parts.push(ComponentPart {
108108
kind: ComponentPartKind::File,
@@ -131,7 +131,7 @@ impl<'a> ComponentBuilder<'a> {
131131
});
132132
self.tx.move_dir(&self.name, path, src)
133133
}
134-
pub(crate) fn finish(mut self) -> Result<Transaction<'a>> {
134+
pub(crate) fn finish(mut self) -> Result<Transaction> {
135135
// Write component manifest
136136
let path = self.components.rel_component_manifest(&self.name);
137137
let abs_path = self.components.prefix.abs_path(&path);
@@ -254,7 +254,7 @@ impl Component {
254254
}
255255
Ok(result)
256256
}
257-
pub fn uninstall<'a>(&self, mut tx: Transaction<'a>) -> Result<Transaction<'a>> {
257+
pub fn uninstall(&self, mut tx: Transaction) -> Result<Transaction> {
258258
// Update components file
259259
let path = self.components.rel_components_file();
260260
let abs_path = self.components.prefix.abs_path(&path);

src/dist/component/package.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -96,13 +96,13 @@ impl<P: Deref<Target = Path>> DirectoryPackage<P> {
9696
}
9797
}
9898

99-
pub fn install<'a>(
99+
pub fn install(
100100
&self,
101101
target: &Components,
102102
name: &str,
103103
short_name: Option<&str>,
104-
tx: Transaction<'a>,
105-
) -> Result<Transaction<'a>> {
104+
tx: Transaction,
105+
) -> Result<Transaction> {
106106
let actual_name = if self.components.contains(name) {
107107
name
108108
} else if let Some(n) = short_name {

src/dist/component/transaction.rs

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@
1111
1212
use std::fs::File;
1313
use std::path::{Path, PathBuf};
14+
use std::sync::Arc;
1415

1516
use anyhow::{Context, Result, anyhow};
1617
use tracing::{error, info};
@@ -33,16 +34,20 @@ use crate::utils;
3334
///
3435
/// All operations that create files will fail if the destination
3536
/// already exists.
36-
pub struct Transaction<'a> {
37+
pub struct Transaction {
3738
prefix: InstallPrefix,
3839
changes: Vec<ChangedItem>,
39-
tmp_cx: &'a temp::Context,
40+
tmp_cx: Arc<temp::Context>,
4041
committed: bool,
4142
pub(super) permit_copy_rename: bool,
4243
}
4344

44-
impl<'a> Transaction<'a> {
45-
pub fn new(prefix: InstallPrefix, tmp_cx: &'a temp::Context, permit_copy_rename: bool) -> Self {
45+
impl Transaction {
46+
pub fn new(
47+
prefix: InstallPrefix,
48+
tmp_cx: Arc<temp::Context>,
49+
permit_copy_rename: bool,
50+
) -> Self {
4651
Transaction {
4752
prefix,
4853
changes: Vec::new(),
@@ -189,14 +194,14 @@ impl<'a> Transaction<'a> {
189194
Ok(())
190195
}
191196

192-
pub(crate) fn temp(&self) -> &'a temp::Context {
193-
self.tmp_cx
197+
pub(crate) fn temp(&self) -> &temp::Context {
198+
&self.tmp_cx
194199
}
195200
}
196201

197202
/// If a Transaction is dropped without being committed, the changes
198203
/// are automatically rolled back.
199-
impl Drop for Transaction<'_> {
204+
impl Drop for Transaction {
200205
fn drop(&mut self) {
201206
if !self.committed {
202207
info!("rolling back changes");

src/dist/manifestation.rs

Lines changed: 14 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,8 @@
55
mod tests;
66

77
use std::path::Path;
8+
#[cfg(test)]
9+
use std::sync::Arc;
810

911
use anyhow::{Context, Result, anyhow, bail};
1012
use futures_util::stream::{FuturesUnordered, StreamExt};
@@ -171,7 +173,7 @@ impl Manifestation {
171173
// Begin transaction
172174
let mut tx = Transaction::new(
173175
prefix.clone(),
174-
&download_cfg.tmp_cx,
176+
download_cfg.tmp_cx.clone(),
175177
download_cfg.permit_copy_rename,
176178
);
177179

@@ -265,7 +267,7 @@ impl Manifestation {
265267
pub(crate) fn uninstall(
266268
&self,
267269
manifest: &Manifest,
268-
tmp_cx: &temp::Context,
270+
tmp_cx: Arc<temp::Context>,
269271
permit_copy_rename: bool,
270272
) -> Result<()> {
271273
let prefix = self.installation.prefix();
@@ -290,12 +292,12 @@ impl Manifestation {
290292
Ok(())
291293
}
292294

293-
fn uninstall_component<'a>(
295+
fn uninstall_component(
294296
&self,
295297
component: Component,
296298
manifest: &Manifest,
297-
mut tx: Transaction<'a>,
298-
) -> Result<Transaction<'a>> {
299+
mut tx: Transaction,
300+
) -> Result<Transaction> {
299301
// For historical reasons, the rust-installer component
300302
// names are not the same as the dist manifest component
301303
// names. Some are just the component name some are the
@@ -393,7 +395,7 @@ impl Manifestation {
393395
info!("installing component rust");
394396

395397
// Begin transaction
396-
let mut tx = Transaction::new(prefix, &dl_cfg.tmp_cx, dl_cfg.permit_copy_rename);
398+
let mut tx = Transaction::new(prefix, dl_cfg.tmp_cx.clone(), dl_cfg.permit_copy_rename);
397399

398400
// Uninstall components
399401
let components = self.installation.list()?;
@@ -424,11 +426,11 @@ impl Manifestation {
424426
// doesn't have a configuration or manifest-derived list of
425427
// component/target pairs. Uninstall it using the installer's
426428
// component list before upgrading.
427-
fn maybe_handle_v2_upgrade<'a>(
429+
fn maybe_handle_v2_upgrade(
428430
&self,
429431
config: &Option<Config>,
430-
mut tx: Transaction<'a>,
431-
) -> Result<Transaction<'a>> {
432+
mut tx: Transaction,
433+
) -> Result<Transaction> {
432434
let installed_components = self.installation.list()?;
433435
let looks_like_v1 = config.is_none() && !installed_components.is_empty();
434436

@@ -674,12 +676,12 @@ impl<'a> ComponentBinary<'a> {
674676
Ok((self, downloaded_file))
675677
}
676678

677-
fn install<'t>(
679+
fn install(
678680
self,
679681
installer_file: File,
680-
tx: Transaction<'t>,
682+
tx: Transaction,
681683
manifestation: &Manifestation,
682-
) -> Result<Transaction<'t>> {
684+
) -> Result<Transaction> {
683685
// For historical reasons, the rust-installer component
684686
// names are not the same as the dist manifest component
685687
// names. Some are just the component name some are the

src/dist/manifestation/tests.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -527,7 +527,7 @@ impl TestContext {
527527

528528
manifestation.uninstall(
529529
&manifest,
530-
&self.tmp_cx,
530+
self.tmp_cx.clone(),
531531
self.tp.process.permit_copy_rename(),
532532
)?;
533533

src/test/dist.rs

Lines changed: 9 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@ use std::collections::HashMap;
55
use std::fs::{self, File};
66
use std::io::{self, Read, Write};
77
use std::path::{Path, PathBuf};
8-
use std::sync::{LazyLock, Mutex};
8+
use std::sync::{Arc, LazyLock, Mutex};
99

1010
use url::Url;
1111

@@ -29,7 +29,7 @@ pub struct DistContext {
2929
pub inst_dir: tempfile::TempDir,
3030
pub prefix: InstallPrefix,
3131
_tmp_dir: tempfile::TempDir,
32-
pub cx: temp::Context,
32+
pub cx: Arc<temp::Context>,
3333
pub tp: TestProcess,
3434
}
3535

@@ -48,23 +48,26 @@ impl DistContext {
4848
pkg_dir,
4949
inst_dir,
5050
prefix,
51-
cx: temp::Context::new(tmp_dir.path().to_owned(), DEFAULT_DIST_SERVER),
51+
cx: Arc::new(temp::Context::new(
52+
tmp_dir.path().to_owned(),
53+
DEFAULT_DIST_SERVER,
54+
)),
5255
tp: TestProcess::default(),
5356
_tmp_dir: tmp_dir,
5457
})
5558
}
5659

57-
pub fn start(&self) -> anyhow::Result<(Transaction<'_>, Components, DirectoryPackage<&Path>)> {
60+
pub fn start(&self) -> anyhow::Result<(Transaction, Components, DirectoryPackage<&Path>)> {
5861
let tx = self.transaction();
5962
let components = Components::open(self.prefix.clone())?;
6063
let pkg = DirectoryPackage::new(self.pkg_dir.path(), true)?;
6164
Ok((tx, components, pkg))
6265
}
6366

64-
pub fn transaction(&self) -> Transaction<'_> {
67+
pub fn transaction(&self) -> Transaction {
6568
Transaction::new(
6669
self.prefix.clone(),
67-
&self.cx,
70+
self.cx.clone(),
6871
self.tp.process.permit_copy_rename(),
6972
)
7073
}

tests/suite/dist_install.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -169,7 +169,7 @@ fn uninstall() {
169169
// Now uninstall
170170
let mut tx = Transaction::new(
171171
cx.prefix.clone(),
172-
&cx.cx,
172+
cx.cx.clone(),
173173
cx.tp.process.permit_copy_rename(),
174174
);
175175
for component in components.list().unwrap() {

0 commit comments

Comments
 (0)