Skip to content

Commit baca00e

Browse files
committed
Auto merge of #12636 - ehuss:fs-metadata-wrappers, r=weihanglo
Add wrappers around std::fs::metadata This adds wrappers around `std::fs::metadata` and `std::fs::symlink_metadata` which provide better error messages indicating the path that caused the error. This just helps clean up some duplicated code, and is also going to be used to assist with some code changes in #12634.
2 parents 282424e + 62d8647 commit baca00e

File tree

1 file changed

+22
-9
lines changed

1 file changed

+22
-9
lines changed

crates/cargo-util/src/paths.rs

+22-9
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use anyhow::{Context, Result};
44
use filetime::FileTime;
55
use std::env;
66
use std::ffi::{OsStr, OsString};
7-
use std::fs::{self, File, OpenOptions};
7+
use std::fs::{self, File, Metadata, OpenOptions};
88
use std::io;
99
use std::io::prelude::*;
1010
use std::iter;
@@ -136,6 +136,24 @@ pub fn resolve_executable(exec: &Path) -> Result<PathBuf> {
136136
}
137137
}
138138

139+
/// Returns metadata for a file (follows symlinks).
140+
///
141+
/// Equivalent to [`std::fs::metadata`] with better error messages.
142+
pub fn metadata<P: AsRef<Path>>(path: P) -> Result<Metadata> {
143+
let path = path.as_ref();
144+
std::fs::metadata(path)
145+
.with_context(|| format!("failed to load metadata for path `{}`", path.display()))
146+
}
147+
148+
/// Returns metadata for a file without following symlinks.
149+
///
150+
/// Equivalent to [`std::fs::metadata`] with better error messages.
151+
pub fn symlink_metadata<P: AsRef<Path>>(path: P) -> Result<Metadata> {
152+
let path = path.as_ref();
153+
std::fs::symlink_metadata(path)
154+
.with_context(|| format!("failed to load metadata for path `{}`", path.display()))
155+
}
156+
139157
/// Reads a file to a string.
140158
///
141159
/// Equivalent to [`std::fs::read_to_string`] with better error messages.
@@ -216,16 +234,14 @@ pub fn open<P: AsRef<Path>>(path: P) -> Result<File> {
216234

217235
/// Returns the last modification time of a file.
218236
pub fn mtime(path: &Path) -> Result<FileTime> {
219-
let meta =
220-
fs::metadata(path).with_context(|| format!("failed to stat `{}`", path.display()))?;
237+
let meta = metadata(path)?;
221238
Ok(FileTime::from_last_modification_time(&meta))
222239
}
223240

224241
/// Returns the maximum mtime of the given path, recursing into
225242
/// subdirectories, and following symlinks.
226243
pub fn mtime_recursive(path: &Path) -> Result<FileTime> {
227-
let meta =
228-
fs::metadata(path).with_context(|| format!("failed to stat `{}`", path.display()))?;
244+
let meta = metadata(path)?;
229245
if !meta.is_dir() {
230246
return Ok(FileTime::from_last_modification_time(&meta));
231247
}
@@ -432,10 +448,7 @@ pub fn remove_dir_all<P: AsRef<Path>>(p: P) -> Result<()> {
432448
}
433449

434450
fn _remove_dir_all(p: &Path) -> Result<()> {
435-
if p.symlink_metadata()
436-
.with_context(|| format!("could not get metadata for `{}` to remove", p.display()))?
437-
.is_symlink()
438-
{
451+
if symlink_metadata(p)?.is_symlink() {
439452
return remove_file(p);
440453
}
441454
let entries = p

0 commit comments

Comments
 (0)