2020-04-03 12:02:59 +02:00
|
|
|
use std::borrow::Cow;
|
2017-10-18 20:04:34 +02:00
|
|
|
use std::env::current_dir;
|
2020-04-03 12:02:59 +02:00
|
|
|
use std::ffi::OsStr;
|
2018-03-25 19:48:09 +02:00
|
|
|
use std::fs;
|
2018-04-13 22:46:17 +02:00
|
|
|
use std::io;
|
2018-03-25 19:48:09 +02:00
|
|
|
#[cfg(any(unix, target_os = "redox"))]
|
2020-04-16 10:11:05 +02:00
|
|
|
use std::os::unix::fs::{FileTypeExt, PermissionsExt};
|
2020-04-04 10:11:56 +02:00
|
|
|
use std::path::{Path, PathBuf};
|
2017-06-05 16:25:13 +02:00
|
|
|
|
2020-02-28 18:19:54 +01:00
|
|
|
use crate::walk;
|
2018-08-19 17:05:04 +02:00
|
|
|
|
2017-10-18 20:04:34 +02:00
|
|
|
pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {
|
|
|
|
if path.is_absolute() {
|
2018-10-03 16:48:38 +02:00
|
|
|
return Ok(path.to_path_buf());
|
2017-06-05 16:25:13 +02:00
|
|
|
}
|
2018-10-03 16:48:38 +02:00
|
|
|
|
|
|
|
let path = path.strip_prefix(".").unwrap_or(path);
|
|
|
|
current_dir().map(|path_buf| path_buf.join(path))
|
2017-06-05 16:25:13 +02:00
|
|
|
}
|
2017-10-07 09:40:44 +02:00
|
|
|
|
|
|
|
pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {
|
2017-10-18 20:04:34 +02:00
|
|
|
let path_buf = path_absolute_form(path)?;
|
2017-10-07 09:40:44 +02:00
|
|
|
|
|
|
|
#[cfg(windows)]
|
2018-01-01 12:16:43 +01:00
|
|
|
let path_buf = Path::new(
|
|
|
|
path_buf
|
|
|
|
.as_path()
|
|
|
|
.to_string_lossy()
|
2019-03-02 01:19:47 +01:00
|
|
|
.trim_start_matches(r"\\?\"),
|
2018-09-27 23:01:38 +02:00
|
|
|
)
|
|
|
|
.to_path_buf();
|
2017-10-07 09:40:44 +02:00
|
|
|
|
|
|
|
Ok(path_buf)
|
|
|
|
}
|
2017-10-18 20:04:34 +02:00
|
|
|
|
2018-08-19 17:05:04 +02:00
|
|
|
// Path::is_dir() is not guaranteed to be intuitively correct for "." and ".."
|
2017-10-18 20:04:34 +02:00
|
|
|
// See: https://github.com/rust-lang/rust/issues/45302
|
|
|
|
pub fn is_dir(path: &Path) -> bool {
|
2018-10-03 16:48:38 +02:00
|
|
|
path.is_dir() && (path.file_name().is_some() || path.canonicalize().is_ok())
|
2017-10-18 20:04:34 +02:00
|
|
|
}
|
2018-03-25 19:48:09 +02:00
|
|
|
|
|
|
|
#[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
|
|
|
|
}
|
2018-08-19 17:05:04 +02:00
|
|
|
|
2020-02-28 18:19:54 +01:00
|
|
|
pub fn is_empty(entry: &walk::DirEntry) -> bool {
|
2018-08-19 17:05:04 +02:00
|
|
|
if let Some(file_type) = entry.file_type() {
|
|
|
|
if file_type.is_dir() {
|
|
|
|
if let Ok(mut entries) = fs::read_dir(entry.path()) {
|
|
|
|
entries.next().is_none()
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
} else if file_type.is_file() {
|
|
|
|
entry.metadata().map(|m| m.len() == 0).unwrap_or(false)
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
}
|
2020-04-03 12:02:59 +02:00
|
|
|
|
2020-04-16 09:41:24 +02:00
|
|
|
#[cfg(any(unix, target_os = "redox"))]
|
|
|
|
pub fn is_socket(ft: &fs::FileType) -> bool {
|
|
|
|
ft.is_socket()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn is_socket(_: &fs::FileType) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(any(unix, target_os = "redox"))]
|
|
|
|
pub fn is_pipe(ft: &fs::FileType) -> bool {
|
|
|
|
ft.is_fifo()
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn is_pipe(_: &fs::FileType) -> bool {
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2020-04-03 12:02:59 +02:00
|
|
|
#[cfg(any(unix, target_os = "redox"))]
|
|
|
|
pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {
|
|
|
|
use std::os::unix::ffi::OsStrExt;
|
|
|
|
Cow::Borrowed(input.as_bytes())
|
|
|
|
}
|
|
|
|
|
|
|
|
#[cfg(windows)]
|
|
|
|
pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {
|
|
|
|
let string = input.to_string_lossy();
|
|
|
|
|
|
|
|
match string {
|
|
|
|
Cow::Owned(string) => Cow::Owned(string.into_bytes()),
|
|
|
|
Cow::Borrowed(string) => Cow::Borrowed(string.as_bytes()),
|
|
|
|
}
|
|
|
|
}
|
2020-04-03 19:01:29 +02:00
|
|
|
|
|
|
|
/// Remove the `./` prefix from a path.
|
2020-04-04 10:11:56 +02:00
|
|
|
pub fn strip_current_dir(path: &Path) -> &Path {
|
|
|
|
path.strip_prefix(".").unwrap_or(path)
|
2020-04-03 19:01:29 +02:00
|
|
|
}
|
2020-04-03 19:09:35 +02:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
2020-04-03 19:25:50 +02:00
|
|
|
use super::strip_current_dir;
|
2020-04-04 10:11:56 +02:00
|
|
|
use std::path::Path;
|
2020-04-03 19:25:50 +02:00
|
|
|
|
2020-04-03 19:09:35 +02:00
|
|
|
#[test]
|
|
|
|
fn strip_current_dir_basic() {
|
2020-04-04 10:11:56 +02:00
|
|
|
assert_eq!(strip_current_dir(Path::new("./foo")), Path::new("foo"));
|
|
|
|
assert_eq!(strip_current_dir(Path::new("foo")), Path::new("foo"));
|
2020-04-03 19:09:35 +02:00
|
|
|
assert_eq!(
|
2020-04-04 10:11:56 +02:00
|
|
|
strip_current_dir(Path::new("./foo/bar/baz")),
|
2020-04-03 19:09:35 +02:00
|
|
|
Path::new("foo/bar/baz")
|
|
|
|
);
|
|
|
|
assert_eq!(
|
2020-04-04 10:11:56 +02:00
|
|
|
strip_current_dir(Path::new("foo/bar/baz")),
|
2020-04-03 19:09:35 +02:00
|
|
|
Path::new("foo/bar/baz")
|
|
|
|
);
|
|
|
|
}
|
|
|
|
}
|