// Copyright (c) 2017 fd developers // Licensed under the Apache License, Version 2.0 // // or the MIT license , // at your option. All files in the project carrying such // notice may not be copied, modified, or distributed except // according to those terms. use std::env::current_dir; use std::fs; use std::io; #[cfg(any(unix, target_os = "redox"))] use std::os::unix::fs::PermissionsExt; use std::path::{Path, PathBuf}; pub fn path_absolute_form(path: &Path) -> io::Result { if path.is_absolute() { Ok(path.to_path_buf()) } else { let path = path.strip_prefix(".").unwrap_or(path); current_dir().map(|path_buf| path_buf.join(path)) } } pub fn absolute_path(path: &Path) -> io::Result { let path_buf = path_absolute_form(path)?; #[cfg(windows)] let path_buf = Path::new( path_buf .as_path() .to_string_lossy() .trim_left_matches(r"\\?\"), ).to_path_buf(); Ok(path_buf) } // Path::is_dir() is not guarandteed to be intuitively correct for "." and ".." // See: https://github.com/rust-lang/rust/issues/45302 pub fn is_dir(path: &Path) -> bool { if path.file_name().is_some() { path.is_dir() } else { path.is_dir() && path.canonicalize().is_ok() } } #[cfg(any(unix, target_os = "redox"))] pub fn is_executable(md: &fs::Metadata) -> bool { md.permissions().mode() & 0o111 != 0 } #[cfg(windows)] pub fn is_executable(_: &fs::Metadata) -> bool { false }