filesystem.rs
This file
Path
src/filesystem.rs
129 lines · 3.2 KiB · Rust
Last changed by
Do not allow list-details in combination with -1sharkdpbefore adoption0335cc3
History of this fileTake it
1use std::borrow::Cow;2use std::env::current_dir;3use std::ffi::OsStr;4use std::fs;5use std::io;6#[cfg(any(unix, target_os = "redox"))]7use std::os::unix::fs::{FileTypeExt, PermissionsExt};8use std::path::{Path, PathBuf};910use crate::walk;1112pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {13 if path.is_absolute() {14 return Ok(path.to_path_buf());15 }1617 let path = path.strip_prefix(".").unwrap_or(path);18 current_dir().map(|path_buf| path_buf.join(path))19}2021pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {22 let path_buf = path_absolute_form(path)?;2324 #[cfg(windows)]25 let path_buf = Path::new(26 path_buf27 .as_path()28 .to_string_lossy()29 .trim_start_matches(r"\\?\"),30 )31 .to_path_buf();3233 Ok(path_buf)34}3536// Path::is_dir() is not guaranteed to be intuitively correct for "." and ".."37// See: https://github.com/rust-lang/rust/issues/4530238pub fn is_dir(path: &Path) -> bool {39 path.is_dir() && (path.file_name().is_some() || path.canonicalize().is_ok())40}4142#[cfg(any(unix, target_os = "redox"))]43pub fn is_executable(md: &fs::Metadata) -> bool {44 md.permissions().mode() & 0o111 != 045}4647#[cfg(windows)]48pub fn is_executable(_: &fs::Metadata) -> bool {49 false50}5152pub fn is_empty(entry: &walk::DirEntry) -> bool {53 if let Some(file_type) = entry.file_type() {54 if file_type.is_dir() {55 if let Ok(mut entries) = fs::read_dir(entry.path()) {56 entries.next().is_none()57 } else {58 false59 }60 } else if file_type.is_file() {61 entry.metadata().map(|m| m.len() == 0).unwrap_or(false)62 } else {63 false64 }65 } else {66 false67 }68}6970#[cfg(any(unix, target_os = "redox"))]71pub fn is_socket(ft: &fs::FileType) -> bool {72 ft.is_socket()73}7475#[cfg(windows)]76pub fn is_socket(_: &fs::FileType) -> bool {77 false78}7980#[cfg(any(unix, target_os = "redox"))]81pub fn is_pipe(ft: &fs::FileType) -> bool {82 ft.is_fifo()83}8485#[cfg(windows)]86pub fn is_pipe(_: &fs::FileType) -> bool {87 false88}8990#[cfg(any(unix, target_os = "redox"))]91pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {92 use std::os::unix::ffi::OsStrExt;93 Cow::Borrowed(input.as_bytes())94}9596#[cfg(windows)]97pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {98 let string = input.to_string_lossy();99100 match string {101 Cow::Owned(string) => Cow::Owned(string.into_bytes()),102 Cow::Borrowed(string) => Cow::Borrowed(string.as_bytes()),103 }104}105106/// Remove the `./` prefix from a path.107pub fn strip_current_dir(path: &Path) -> &Path {108 path.strip_prefix(".").unwrap_or(path)109}110111#[cfg(test)]112mod tests {113 use super::strip_current_dir;114 use std::path::Path;115116 #[test]117 fn strip_current_dir_basic() {118 assert_eq!(strip_current_dir(Path::new("./foo")), Path::new("foo"));119 assert_eq!(strip_current_dir(Path::new("foo")), Path::new("foo"));120 assert_eq!(121 strip_current_dir(Path::new("./foo/bar/baz")),122 Path::new("foo/bar/baz")123 );124 assert_eq!(125 strip_current_dir(Path::new("foo/bar/baz")),126 Path::new("foo/bar/baz")127 );128 }129}