Skip to content
Myr

filesystem.rs

This file

Path

src/filesystem.rs

156 lines · 3.8 KiB · Rust

Last changed by

Merge pull request #1928 from tmccombs/full-path-fixesTavian Barnesbefore adoptiond6b44b8

History of this file

Take it

The walk stopped after 200 commits: lines older than 86d6e2d point at it.

Merge pull request #1657 from sharkdp/dependabot/cargo/clap-4.5.23Tavian Barnesbefore adoption86d6e2d

lines 1–102why, one step back
1use std::borrow::Cow;
2use std::env;3use std::ffi::OsStr;4use std::fs;5use std::io;6#[cfg(any(unix, target_os = "redox"))]7use std::os::unix::fs::FileTypeExt;8use std::path::{Path, PathBuf};910use normpath::PathExt;1112use crate::dir_entry;1314pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {15 if path.is_absolute() {16 return Ok(path.to_path_buf());17 }1819 let path = path.strip_prefix(".").unwrap_or(path);20 env::current_dir().map(|path_buf| path_buf.join(path))21}2223pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {24 let path_buf = path_absolute_form(path)?;2526 #[cfg(windows)]27 let path_buf = Path::new(28 path_buf29 .as_path()30 .to_string_lossy()31 .trim_start_matches(r"\\?\"),32 )33 .to_path_buf();3435 Ok(path_buf)36}3738pub fn is_existing_directory(path: &Path) -> bool {39 // Note: we do not use `.exists()` here, as `.` always exists, even if40 // the CWD has been deleted.41 path.is_dir() && (path.file_name().is_some() || path.normalize().is_ok())42}4344pub fn is_empty(entry: &dir_entry::DirEntry) -> bool {45 if let Some(file_type) = entry.file_type() {46 if file_type.is_dir() {47 if let Ok(mut entries) = fs::read_dir(entry.path()) {48 entries.next().is_none()49 } else {50 false51 }52 } else if file_type.is_file() {53 entry.metadata().map(|m| m.len() == 0).unwrap_or(false)54 } else {55 false56 }57 } else {58 false59 }60}6162#[cfg(any(unix, target_os = "redox"))]63pub fn is_block_device(ft: fs::FileType) -> bool {64 ft.is_block_device()65}6667#[cfg(windows)]68pub fn is_block_device(_: fs::FileType) -> bool {69 false70}7172#[cfg(any(unix, target_os = "redox"))]73pub fn is_char_device(ft: fs::FileType) -> bool {74 ft.is_char_device()75}7677#[cfg(windows)]78pub fn is_char_device(_: fs::FileType) -> bool {79 false80}8182#[cfg(any(unix, target_os = "redox"))]83pub fn is_socket(ft: fs::FileType) -> bool {84 ft.is_socket()85}8687#[cfg(windows)]88pub fn is_socket(_: fs::FileType) -> bool {89 false90}9192#[cfg(any(unix, target_os = "redox"))]93pub fn is_pipe(ft: fs::FileType) -> bool {94 ft.is_fifo()95}9697#[cfg(windows)]98pub fn is_pipe(_: fs::FileType) -> bool {99 false100}101102#[cfg(any(unix, target_os = "redox"))]

Merge pull request #1772 from sharkdp/fix-clippy-lifetime-elisionThayne McCombsbefore adoptionacb7f72

line 103why, one step back
103pub fn osstr_to_bytes(input: &OsStr) -> Cow<'_, [u8]> {

Merge pull request #1657 from sharkdp/dependabot/cargo/clap-4.5.23Tavian Barnesbefore adoption86d6e2d

lines 104–108why, one step back
104 use std::os::unix::ffi::OsStrExt;
105 Cow::Borrowed(input.as_bytes())106}107108#[cfg(windows)]

Merge pull request #1772 from sharkdp/fix-clippy-lifetime-elisionThayne McCombsbefore adoptionacb7f72

line 109why, one step back
109pub fn osstr_to_bytes(input: &OsStr) -> Cow<'_, [u8]> {

Merge pull request #1657 from sharkdp/dependabot/cargo/clap-4.5.23Tavian Barnesbefore adoption86d6e2d

lines 110–156why, one step back
110 let string = input.to_string_lossy();
111112 match string {113 Cow::Owned(string) => Cow::Owned(string.into_bytes()),114 Cow::Borrowed(string) => Cow::Borrowed(string.as_bytes()),115 }116}117118/// Remove the `./` prefix from a path.119pub fn strip_current_dir(path: &Path) -> &Path {120 path.strip_prefix(".").unwrap_or(path)121}122123/// Default value for the path_separator, mainly for MSYS/MSYS2, which set the MSYSTEM124/// environment variable, and we set fd's path separator to '/' rather than Rust's default of '\'.125///126/// Returns Some to use a nonstandard path separator, or None to use rust's default on the target127/// platform.128pub fn default_path_separator() -> Option<String> {129 if cfg!(windows) {130 let msystem = env::var("MSYSTEM").ok()?;131 if !msystem.is_empty() {132 return Some("/".to_owned());133 }134 }135 None136}137138#[cfg(test)]139mod tests {140 use super::strip_current_dir;141 use std::path::Path;142143 #[test]144 fn strip_current_dir_basic() {145 assert_eq!(strip_current_dir(Path::new("./foo")), Path::new("foo"));146 assert_eq!(strip_current_dir(Path::new("foo")), Path::new("foo"));147 assert_eq!(148 strip_current_dir(Path::new("./foo/bar/baz")),149 Path::new("foo/bar/baz")150 );151 assert_eq!(152 strip_current_dir(Path::new("foo/bar/baz")),153 Path::new("foo/bar/baz")154 );155 }156}