Skip to main content

std/sys/path/
unix_like.rs

1use crate::ffi::OsStr;
2#[cfg(target_os = "motor")]
3use crate::os::motor::ffi::OsStrExt;
4use crate::path::{Path, PathBuf, Prefix};
5use crate::{env, io};
6
7cfg_select! {
8    target_os = "motor" => {
9        #[inline]
10        pub fn with_native_path<T>(
11            path: &Path,
12            f: &dyn Fn(&str) -> io::Result<T>,
13        ) -> io::Result<T> {
14            f(path.as_os_str().as_str())
15        }
16    }
17    any(target_family = "unix", target_os = "vexos", target_os = "hermit", target_os = "wasi") => {
18        pub use crate::sys::helpers::run_path_with_cstr as with_native_path;
19    }
20    _ => {
21        #[inline]
22        pub fn with_native_path<T>(
23            path: &Path,
24            f: &dyn Fn(&Path) -> io::Result<T>,
25        ) -> io::Result<T> {
26            f(path)
27        }
28    }
29}
30
31path_separator_bytes!(b'/');
32
33#[inline]
34pub const fn is_verbatim_sep(b: u8) -> bool {
35    is_sep_byte(b)
36}
37
38#[inline]
39pub fn parse_prefix(_: &OsStr) -> Option<Prefix<'_>> {
40    None
41}
42
43pub const HAS_PREFIXES: bool = false;
44
45/// Make a POSIX path absolute without changing its semantics.
46pub(crate) fn absolute(path: &Path) -> io::Result<PathBuf> {
47    // This is mostly a wrapper around collecting `Path::components`, with
48    // exceptions made where this conflicts with the POSIX specification.
49    // See 4.16 Pathname Resolution, IEEE Std 1003.1-2024
50    // https://pubs.opengroup.org/onlinepubs/9799919799/basedefs/V1_chap04.html#tag_04_16
51
52    // Get the components, skipping the redundant leading "." component if it exists.
53    let mut components = path.strip_prefix(".").unwrap_or(path).components();
54    let path_os = path.as_os_str().as_encoded_bytes();
55
56    let mut normalized = if path.is_absolute() {
57        // "If a pathname begins with two successive <slash> characters, the
58        // first component following the leading <slash> characters may be
59        // interpreted in an implementation-defined manner, although more than
60        // two leading <slash> characters shall be treated as a single <slash>
61        // character."
62        if path_os.starts_with(b"//") && !path_os.starts_with(b"///") {
63            components.next();
64            PathBuf::from("//")
65        } else {
66            PathBuf::new()
67        }
68    } else {
69        env::current_dir()?
70    };
71    normalized.extend(components);
72
73    // "Interfaces using pathname resolution may specify additional constraints
74    // when a pathname that does not name an existing directory contains at
75    // least one non- <slash> character and contains one or more trailing
76    // <slash> characters".
77    // A trailing <slash> is also meaningful if "a symbolic link is
78    // encountered during pathname resolution".
79    if path_os.ends_with(b"/") {
80        normalized.push("");
81    }
82
83    Ok(normalized)
84}
85
86pub(crate) fn is_absolute(path: &Path) -> bool {
87    if cfg!(any(unix, target_os = "hermit", target_os = "wasi", target_os = "motor")) {
88        path.has_root()
89    } else {
90        path.has_root() && path.prefix().is_some()
91    }
92}