]> git.lizzy.rs Git - rust.git/blob - library/std/src/sys/unix/path.rs
Rollup merge of #93926 - PatchMixolydic:bugfix/must_use-on-exprs, r=cjgillot
[rust.git] / library / std / src / sys / unix / path.rs
1 use crate::env;
2 use crate::ffi::OsStr;
3 use crate::io;
4 use crate::path::{Path, PathBuf, Prefix};
5
6 #[inline]
7 pub fn is_sep_byte(b: u8) -> bool {
8     b == b'/'
9 }
10
11 #[inline]
12 pub fn is_verbatim_sep(b: u8) -> bool {
13     b == b'/'
14 }
15
16 #[inline]
17 pub fn parse_prefix(_: &OsStr) -> Option<Prefix<'_>> {
18     None
19 }
20
21 pub const MAIN_SEP_STR: &str = "/";
22 pub const MAIN_SEP: char = '/';
23
24 /// Make a POSIX path absolute without changing its semantics.
25 pub(crate) fn absolute(path: &Path) -> io::Result<PathBuf> {
26     // This is mostly a wrapper around collecting `Path::components`, with
27     // exceptions made where this conflicts with the POSIX specification.
28     // See 4.13 Pathname Resolution, IEEE Std 1003.1-2017
29     // https://pubs.opengroup.org/onlinepubs/9699919799/basedefs/V1_chap04.html#tag_04_13
30
31     let mut components = path.components();
32     let path_os = path.as_os_str().bytes();
33
34     let mut normalized = if path.is_absolute() {
35         // "If a pathname begins with two successive <slash> characters, the
36         // first component following the leading <slash> characters may be
37         // interpreted in an implementation-defined manner, although more than
38         // two leading <slash> characters shall be treated as a single <slash>
39         // character."
40         if path_os.starts_with(b"//") && !path_os.starts_with(b"///") {
41             components.next();
42             PathBuf::from("//")
43         } else {
44             PathBuf::new()
45         }
46     } else {
47         env::current_dir()?
48     };
49     normalized.extend(components);
50
51     // "Interfaces using pathname resolution may specify additional constraints
52     // when a pathname that does not name an existing directory contains at
53     // least one non- <slash> character and contains one or more trailing
54     // <slash> characters".
55     // A trailing <slash> is also meaningful if "a symbolic link is
56     // encountered during pathname resolution".
57     if path_os.ends_with(b"/") {
58         normalized.push("");
59     }
60
61     Ok(normalized)
62 }