Skip to content

Commit fb68ae4

Browse files
committed
Add metadata and symlink_metadata functions to cargo_util::paths
1 parent 85e457e commit fb68ae4

File tree

1 file changed

+12
-4
lines changed

1 file changed

+12
-4
lines changed

crates/cargo-util/src/paths.rs

Lines changed: 12 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -211,18 +211,26 @@ pub fn open<P: AsRef<Path>>(path: P) -> Result<File> {
211211
File::open(path).with_context(|| format!("failed to open file `{}`", path.display()))
212212
}
213213

214+
/// Returns the metadata of the path (follows symlinks).
215+
pub fn metadata(path: &Path) -> Result<fs::Metadata> {
216+
fs::metadata(path).with_context(|| format!("failed to stat `{}`", path.display()))
217+
}
218+
219+
/// Returns the metadata of the path (does not follow symlinks).
220+
pub fn symlink_metadata(path: &Path) -> Result<fs::Metadata> {
221+
fs::symlink_metadata(path).with_context(|| format!("failed to lstat `{}`", path.display()))
222+
}
223+
214224
/// Returns the last modification time of a file.
215225
pub fn mtime(path: &Path) -> Result<FileTime> {
216-
let meta =
217-
fs::metadata(path).with_context(|| format!("failed to stat `{}`", path.display()))?;
226+
let meta = metadata(path)?;
218227
Ok(FileTime::from_last_modification_time(&meta))
219228
}
220229

221230
/// Returns the maximum mtime of the given path, recursing into
222231
/// subdirectories, and following symlinks.
223232
pub fn mtime_recursive(path: &Path) -> Result<FileTime> {
224-
let meta =
225-
fs::metadata(path).with_context(|| format!("failed to stat `{}`", path.display()))?;
233+
let meta = metadata(path)?;
226234
if !meta.is_dir() {
227235
return Ok(FileTime::from_last_modification_time(&meta));
228236
}

0 commit comments

Comments
 (0)