Skip to content

Commit 1b0bba3

Browse files
committed
notifications: log directly on file deletions
1 parent 4328637 commit 1b0bba3

File tree

4 files changed

+22
-35
lines changed

4 files changed

+22
-35
lines changed

src/dist/component/transaction.rs

Lines changed: 9 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -36,7 +36,7 @@ use crate::utils;
3636
/// already exists.
3737
pub struct Transaction<'a> {
3838
prefix: InstallPrefix,
39-
changes: Vec<ChangedItem<'a>>,
39+
changes: Vec<ChangedItem>,
4040
tmp_cx: &'a temp::Context,
4141
committed: bool,
4242
process: &'a Process,
@@ -59,7 +59,7 @@ impl<'a> Transaction<'a> {
5959
self.committed = true;
6060
}
6161

62-
fn change(&mut self, item: ChangedItem<'a>) {
62+
fn change(&mut self, item: ChangedItem) {
6363
self.changes.push(item);
6464
}
6565

@@ -183,15 +183,15 @@ impl Drop for Transaction<'_> {
183183
/// package, or updating a component, distill down into a series of
184184
/// these primitives.
185185
#[derive(Debug)]
186-
enum ChangedItem<'a> {
186+
enum ChangedItem {
187187
AddedFile(PathBuf),
188188
AddedDir(PathBuf),
189-
RemovedFile(PathBuf, temp::File<'a>),
189+
RemovedFile(PathBuf, temp::File),
190190
RemovedDir(PathBuf, temp::Dir),
191-
ModifiedFile(PathBuf, Option<temp::File<'a>>),
191+
ModifiedFile(PathBuf, Option<temp::File>),
192192
}
193193

194-
impl<'a> ChangedItem<'a> {
194+
impl ChangedItem {
195195
fn roll_back(&self, prefix: &InstallPrefix, process: &Process) -> Result<()> {
196196
use self::ChangedItem::*;
197197
match self {
@@ -259,7 +259,7 @@ impl<'a> ChangedItem<'a> {
259259
prefix: &InstallPrefix,
260260
component: &str,
261261
relpath: PathBuf,
262-
tmp_cx: &'a temp::Context,
262+
tmp_cx: &temp::Context,
263263
process: &Process,
264264
) -> Result<Self> {
265265
let abs_path = prefix.abs_path(&relpath);
@@ -279,7 +279,7 @@ impl<'a> ChangedItem<'a> {
279279
prefix: &InstallPrefix,
280280
component: &str,
281281
relpath: PathBuf,
282-
tmp_cx: &'a temp::Context,
282+
tmp_cx: &temp::Context,
283283
process: &Process,
284284
) -> Result<Self> {
285285
let abs_path = prefix.abs_path(&relpath);
@@ -298,7 +298,7 @@ impl<'a> ChangedItem<'a> {
298298
fn modify_file(
299299
prefix: &InstallPrefix,
300300
relpath: PathBuf,
301-
tmp_cx: &'a temp::Context,
301+
tmp_cx: &temp::Context,
302302
) -> Result<Self> {
303303
let abs_path = prefix.abs_path(&relpath);
304304

src/dist/download.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -148,7 +148,7 @@ impl<'a> DownloadCfg<'a> {
148148
url_str: &str,
149149
update_hash: Option<&Path>,
150150
ext: &str,
151-
) -> Result<Option<(temp::File<'a>, String)>> {
151+
) -> Result<Option<(temp::File, String)>> {
152152
let hash = self.download_hash(url_str).await?;
153153
let partial_hash: String = hash.chars().take(UPDATE_HASH_LEN).collect();
154154

src/dist/temp.rs

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -45,24 +45,27 @@ impl Drop for Dir {
4545
}
4646

4747
#[derive(Debug)]
48-
pub struct File<'a> {
49-
cfg: &'a Context,
48+
pub struct File {
5049
path: PathBuf,
5150
}
5251

53-
impl ops::Deref for File<'_> {
52+
impl ops::Deref for File {
5453
type Target = Path;
5554

5655
fn deref(&self) -> &Path {
5756
self.path.as_path()
5857
}
5958
}
6059

61-
impl Drop for File<'_> {
60+
impl Drop for File {
6261
fn drop(&mut self) {
6362
if raw::is_file(&self.path) {
64-
let n = Notification::FileDeletion(&self.path, fs::remove_file(&self.path));
65-
(self.cfg.notify_handler)(n);
63+
match fs::remove_file(&self.path) {
64+
Ok(()) => debug!(path = %self.path.display(), "deleted temp file"),
65+
Err(e) => {
66+
warn!(path = %self.path.display(), error = %e, "could not delete temp file")
67+
}
68+
}
6669
}
6770
}
6871
}
@@ -112,11 +115,11 @@ impl Context {
112115
}
113116
}
114117

115-
pub fn new_file(&self) -> Result<File<'_>> {
118+
pub fn new_file(&self) -> Result<File> {
116119
self.new_file_with_ext("", "")
117120
}
118121

119-
pub(crate) fn new_file_with_ext(&self, prefix: &str, ext: &str) -> Result<File<'_>> {
122+
pub(crate) fn new_file_with_ext(&self, prefix: &str, ext: &str) -> Result<File> {
120123
self.create_root()?;
121124

122125
loop {
@@ -130,10 +133,7 @@ impl Context {
130133
(self.notify_handler)(Notification::CreatingFile(&temp_file));
131134
fs::File::create(&temp_file)
132135
.with_context(|| CreatingError::File(PathBuf::from(&temp_file)))?;
133-
return Ok(File {
134-
cfg: self,
135-
path: temp_file,
136-
});
136+
return Ok(File { path: temp_file });
137137
}
138138
}
139139
}

src/notifications.rs

Lines changed: 0 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,4 @@
11
use std::fmt::{self, Display};
2-
use std::io;
32
use std::path::{Path, PathBuf};
43

54
use url::Url;
@@ -55,7 +54,6 @@ pub enum Notification<'a> {
5554
UsingReqwest,
5655
CreatingRoot(&'a Path),
5756
CreatingFile(&'a Path),
58-
FileDeletion(&'a Path, io::Result<()>),
5957
SetAutoInstall(&'a str),
6058
SetDefaultToolchain(Option<&'a ToolchainName>),
6159
SetOverrideToolchain(&'a Path, &'a str),
@@ -118,10 +116,6 @@ impl Notification<'_> {
118116
| UsingReqwest => NotificationLevel::Debug,
119117
Error(_) => NotificationLevel::Error,
120118
CreatingRoot(_) | CreatingFile(_) => NotificationLevel::Debug,
121-
FileDeletion(_, result) => match result {
122-
Ok(_) => NotificationLevel::Debug,
123-
Err(_) => NotificationLevel::Warn,
124-
},
125119
ToolchainDirectory(_)
126120
| LookingForToolchain(_)
127121
| InstallingToolchain(_)
@@ -253,13 +247,6 @@ impl Display for Notification<'_> {
253247
UsingReqwest => write!(f, "downloading with reqwest"),
254248
CreatingRoot(path) => write!(f, "creating temp root: {}", path.display()),
255249
CreatingFile(path) => write!(f, "creating temp file: {}", path.display()),
256-
FileDeletion(path, result) => {
257-
if result.is_ok() {
258-
write!(f, "deleted temp file: {}", path.display())
259-
} else {
260-
write!(f, "could not delete temp file: {}", path.display())
261-
}
262-
}
263250
SetAutoInstall(auto) => write!(f, "auto install set to '{auto}'"),
264251
SetDefaultToolchain(None) => write!(f, "default toolchain unset"),
265252
SetDefaultToolchain(Some(name)) => write!(f, "default toolchain set to '{name}'"),

0 commit comments

Comments
 (0)