"Fossies" - the Fresh Open Source Software Archive

Member "fd-8.1.1/src/filesystem.rs" (25 May 2020, 3228 Bytes) of package /linux/privat/fd-8.1.1.tar.gz:


As a special service "Fossies" has tried to format the requested source page into HTML format using (guessed) Rust source code syntax highlighting (style: standard) with prefixed line numbers and code folding option. Alternatively you can here view or download the uninterpreted source code file.

    1 use std::borrow::Cow;
    2 use std::env::current_dir;
    3 use std::ffi::OsStr;
    4 use std::fs;
    5 use std::io;
    6 #[cfg(any(unix, target_os = "redox"))]
    7 use std::os::unix::fs::{FileTypeExt, PermissionsExt};
    8 use std::path::{Path, PathBuf};
    9 
   10 use crate::walk;
   11 
   12 pub fn path_absolute_form(path: &Path) -> io::Result<PathBuf> {
   13     if path.is_absolute() {
   14         return Ok(path.to_path_buf());
   15     }
   16 
   17     let path = path.strip_prefix(".").unwrap_or(path);
   18     current_dir().map(|path_buf| path_buf.join(path))
   19 }
   20 
   21 pub fn absolute_path(path: &Path) -> io::Result<PathBuf> {
   22     let path_buf = path_absolute_form(path)?;
   23 
   24     #[cfg(windows)]
   25     let path_buf = Path::new(
   26         path_buf
   27             .as_path()
   28             .to_string_lossy()
   29             .trim_start_matches(r"\\?\"),
   30     )
   31     .to_path_buf();
   32 
   33     Ok(path_buf)
   34 }
   35 
   36 // Path::is_dir() is not guaranteed to be intuitively correct for "." and ".."
   37 // See: https://github.com/rust-lang/rust/issues/45302
   38 pub fn is_dir(path: &Path) -> bool {
   39     path.is_dir() && (path.file_name().is_some() || path.canonicalize().is_ok())
   40 }
   41 
   42 #[cfg(any(unix, target_os = "redox"))]
   43 pub fn is_executable(md: &fs::Metadata) -> bool {
   44     md.permissions().mode() & 0o111 != 0
   45 }
   46 
   47 #[cfg(windows)]
   48 pub fn is_executable(_: &fs::Metadata) -> bool {
   49     false
   50 }
   51 
   52 pub 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                 false
   59             }
   60         } else if file_type.is_file() {
   61             entry.metadata().map(|m| m.len() == 0).unwrap_or(false)
   62         } else {
   63             false
   64         }
   65     } else {
   66         false
   67     }
   68 }
   69 
   70 #[cfg(any(unix, target_os = "redox"))]
   71 pub fn is_socket(ft: &fs::FileType) -> bool {
   72     ft.is_socket()
   73 }
   74 
   75 #[cfg(windows)]
   76 pub fn is_socket(_: &fs::FileType) -> bool {
   77     false
   78 }
   79 
   80 #[cfg(any(unix, target_os = "redox"))]
   81 pub fn is_pipe(ft: &fs::FileType) -> bool {
   82     ft.is_fifo()
   83 }
   84 
   85 #[cfg(windows)]
   86 pub fn is_pipe(_: &fs::FileType) -> bool {
   87     false
   88 }
   89 
   90 #[cfg(any(unix, target_os = "redox"))]
   91 pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {
   92     use std::os::unix::ffi::OsStrExt;
   93     Cow::Borrowed(input.as_bytes())
   94 }
   95 
   96 #[cfg(windows)]
   97 pub fn osstr_to_bytes(input: &OsStr) -> Cow<[u8]> {
   98     let string = input.to_string_lossy();
   99 
  100     match string {
  101         Cow::Owned(string) => Cow::Owned(string.into_bytes()),
  102         Cow::Borrowed(string) => Cow::Borrowed(string.as_bytes()),
  103     }
  104 }
  105 
  106 /// Remove the `./` prefix from a path.
  107 pub fn strip_current_dir(path: &Path) -> &Path {
  108     path.strip_prefix(".").unwrap_or(path)
  109 }
  110 
  111 #[cfg(test)]
  112 mod tests {
  113     use super::strip_current_dir;
  114     use std::path::Path;
  115 
  116     #[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 }