fix(fs): Fix absolute_path() for broken symlinks

This commit is contained in:
Tamino Bauknecht 2024-01-21 21:22:58 +01:00 committed by Christina E. Sørensen
parent 4611e37a7f
commit 4799a49249

View file

@ -343,7 +343,19 @@ impl<'dir> File<'dir> {
/// Determine the full path resolving all symbolic links on demand.
pub fn absolute_path(&self) -> Option<&PathBuf> {
self.absolute_path
.get_or_init(|| std::fs::canonicalize(&self.path).ok())
.get_or_init(|| {
if self.link_target().is_broken() {
// workaround for broken symlinks to get absolute path for parent and then
// append name of file; std::fs::canonicalize requires all path components
// (including the last one) to exist
self.path
.parent()
.and_then(|parent| std::fs::canonicalize(parent).ok())
.map(|p| p.join(self.name.clone()))
} else {
std::fs::canonicalize(&self.path).ok()
}
})
.as_ref()
}