Skip to content

Commit

Permalink
Improve config.toml vs config symlink check on Unix
Browse files Browse the repository at this point in the history
The code attempts to suppress the warning if `config` is merely a
symlink to `config.toml`.  But it does this by calling readlink and
comparing the result textually.

That's not ideal because it depends on the precise spelling
(`config.toml` vs `./config.toml` vs `/path/to/config.toml`)
and also minds whether it's config.toml -> config, or v.v.
On Unix, where we can get st_dev and st_ino, use that to suppress the
check when the two names resolve to the same underlying file.
  • Loading branch information
ijackson committed Apr 23, 2024
1 parent ea23780 commit 5c31ec3
Showing 1 changed file with 23 additions and 0 deletions.
23 changes: 23 additions & 0 deletions src/cargo/util/context/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1545,6 +1545,29 @@ impl GlobalContext {
// WITH an extension, which people may want to do to
// support multiple Cargo versions at once and not
// get a warning.

// On Unix we can see if the two files are the same by using stat(2),
// and comparing the inode numbers. That way it doesn't matter precisely
// what spelling the link uses to refers to the other file (relative
// vs absolute pathname, etc.) Also, this is symmetrical: it doesn't mind
// which way round the symlink is.
#[cfg(unix)]
let skip_warning = if let (Ok(metadata_1), Ok(metadata_2)) = (
fs::metadata(&possible),
fs::metadata(&possible_with_extension),
) {
use std::os::unix::fs::MetadataExt;
let devino = |md: fs::Metadata| (md.dev(), md.ino());
devino(metadata_1) == devino(metadata_2)
} else {
false
};

// Platforms other than unix don't have std::os::unix::fs::MetadataExt
// and its st_ino and st_dev methods. We can still ignore a symlink,
// but only based on its *contents*, which we must hope are identical
// to our computed pathname.
#[cfg(not(unix))]
let skip_warning = if let Ok(target_path) = fs::read_link(&possible) {
target_path == possible_with_extension ||
// allow `config` -> `config.toml`, without the parent dir(s),
Expand Down

0 comments on commit 5c31ec3

Please sign in to comment.