Skip to content

Commit

Permalink
Add metadata and symlink_metadata functions to cargo_util::paths
Browse files Browse the repository at this point in the history
  • Loading branch information
ehuss committed Jun 10, 2022
1 parent 85e457e commit fb68ae4
Showing 1 changed file with 12 additions and 4 deletions.
16 changes: 12 additions & 4 deletions crates/cargo-util/src/paths.rs
Original file line number Diff line number Diff line change
Expand Up @@ -211,18 +211,26 @@ pub fn open<P: AsRef<Path>>(path: P) -> Result<File> {
File::open(path).with_context(|| format!("failed to open file `{}`", path.display()))
}

/// Returns the metadata of the path (follows symlinks).
pub fn metadata(path: &Path) -> Result<fs::Metadata> {
fs::metadata(path).with_context(|| format!("failed to stat `{}`", path.display()))
}

/// Returns the metadata of the path (does not follow symlinks).
pub fn symlink_metadata(path: &Path) -> Result<fs::Metadata> {
fs::symlink_metadata(path).with_context(|| format!("failed to lstat `{}`", path.display()))
}

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

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

0 comments on commit fb68ae4

Please sign in to comment.