use super::internal_open;
use crate::fs::{canonicalize_options, FollowSymlinks, MaybeOwnedFile};
use std::path::{Path, PathBuf};
use std::{fs, io};
pub(crate) fn canonicalize(start: &fs::File, path: &Path) -> io::Result<PathBuf> {
canonicalize_with(start, path, FollowSymlinks::Yes)
}
pub(crate) fn canonicalize_with(
start: &fs::File,
path: &Path,
follow: FollowSymlinks,
) -> io::Result<PathBuf> {
let mut symlink_count = 0;
let mut canonical_path = PathBuf::new();
let start = MaybeOwnedFile::borrowed(start);
match internal_open(
start,
path,
canonicalize_options().follow(follow),
&mut symlink_count,
Some(&mut canonical_path),
) {
Ok(_) => (),
Err(err) if err.kind() == io::ErrorKind::InvalidInput => {
return Err(err);
}
#[cfg(io_error_more)]
Err(err) if err.kind() == io::ErrorKind::InvalidFilename => {
return Err(err);
}
#[cfg(windows)]
Err(err)
if err.raw_os_error()
== Some(windows_sys::Win32::Foundation::ERROR_INVALID_NAME as _)
|| err.raw_os_error()
== Some(windows_sys::Win32::Foundation::ERROR_DIRECTORY as _) =>
{
return Err(err);
}
Err(err) => {
if canonical_path.as_os_str().is_empty() {
return Err(err);
}
}
}
Ok(canonical_path)
}