diff options
author | Lars Wirzenius <liw@liw.fi> | 2023-11-14 21:54:52 +0200 |
---|---|---|
committer | Lars Wirzenius <liw@liw.fi> | 2023-11-14 21:54:52 +0200 |
commit | 1e84eeef4179c4864516798db9f548eacad4dbb1 (patch) | |
tree | 665e11035d6326ba0c1f5c22e18500bbcb3f51fd | |
parent | 4f74a254dba998f7ddfe1a80d6194e980f361cc1 (diff) | |
download | pathdedup-1e84eeef4179c4864516798db9f548eacad4dbb1.tar.gz |
drop symlinks that point to other directories in $PATH
Signed-off-by: Lars Wirzenius <liw@liw.fi>
Sponsored-by: author
-rw-r--r-- | src/main.rs | 11 |
1 files changed, 10 insertions, 1 deletions
diff --git a/src/main.rs b/src/main.rs index 5c179e7..dc2c068 100644 --- a/src/main.rs +++ b/src/main.rs @@ -1,3 +1,5 @@ +use std::{collections::HashSet, fs::metadata, os::unix::fs::MetadataExt}; + fn main() -> anyhow::Result<()> { println!("{}", pathdedup(&std::env::var("PATH")?)); Ok(()) @@ -5,10 +7,17 @@ fn main() -> anyhow::Result<()> { fn pathdedup(path: &str) -> String { let mut elements: Vec<&str> = vec![]; + let mut seen: HashSet<(u64, u64)> = HashSet::new(); for e in path.split(':') { if !elements.contains(&e) { - elements.push(e); + if let Ok(meta) = metadata(e) { + let key = (meta.dev(), meta.ino()); + if !seen.contains(&key) { + seen.insert(key); + elements.push(e); + } + } } } |