std/sys/path/
unix_like.rs1use 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
45pub(crate) fn absolute(path: &Path) -> io::Result<PathBuf> {
47 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 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 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}