Skip to content

Commit

Permalink
path: Move is_absolute check to sys::path
Browse files Browse the repository at this point in the history
I am working on fs support for UEFI [0], which similar to windows has prefix
components, but is not quite same as Windows. It also seems that Prefix
is tied closely to Windows and cannot really be extended [1].

This PR just tries to remove coupling between Prefix and absolute path
checking to allow platforms to provide there own implementation to check
if a path is absolute or not.

I am not sure if any platform other than windows currently uses Prefix,
so I have kept the path.prefix().is_some() check in most cases.

[0]: rust-lang#135368
[1]: rust-lang#52331 (comment)

Signed-off-by: Ayush Singh <ayush@beagleboard.org>
  • Loading branch information
Ayush1325 authored and gitbot committed Feb 20, 2025
1 parent c0b3faf commit 83edfbb
Show file tree
Hide file tree
Showing 5 changed files with 27 additions and 11 deletions.
15 changes: 4 additions & 11 deletions std/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -298,7 +298,7 @@ where
}

// Detect scheme on Redox
fn has_redox_scheme(s: &[u8]) -> bool {
pub(crate) fn has_redox_scheme(s: &[u8]) -> bool {
cfg!(target_os = "redox") && s.contains(&b':')
}

Expand Down Expand Up @@ -2155,7 +2155,7 @@ impl Path {
unsafe { Path::new(OsStr::from_encoded_bytes_unchecked(s)) }
}
// The following (private!) function reveals the byte encoding used for OsStr.
fn as_u8_slice(&self) -> &[u8] {
pub(crate) fn as_u8_slice(&self) -> &[u8] {
self.inner.as_encoded_bytes()
}

Expand Down Expand Up @@ -2323,14 +2323,7 @@ impl Path {
#[must_use]
#[allow(deprecated)]
pub fn is_absolute(&self) -> bool {
if cfg!(target_os = "redox") {
// FIXME: Allow Redox prefixes
self.has_root() || has_redox_scheme(self.as_u8_slice())
} else {
self.has_root()
&& (cfg!(any(unix, target_os = "hermit", target_os = "wasi"))
|| self.prefix().is_some())
}
sys::path::is_absolute(self)
}

/// Returns `true` if the `Path` is relative, i.e., not absolute.
Expand All @@ -2353,7 +2346,7 @@ impl Path {
!self.is_absolute()
}

fn prefix(&self) -> Option<Prefix<'_>> {
pub(crate) fn prefix(&self) -> Option<Prefix<'_>> {
self.components().prefix
}

Expand Down
4 changes: 4 additions & 0 deletions std/src/sys/path/sgx.rs
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ pub const MAIN_SEP: char = '/';
pub(crate) fn absolute(_path: &Path) -> io::Result<PathBuf> {
unsupported()
}

pub(crate) fn is_absolute(path: &Path) -> bool {
path.has_root() && path.prefix().is_some()
}
11 changes: 11 additions & 0 deletions std/src/sys/path/unix.rs
Original file line number Diff line number Diff line change
Expand Up @@ -60,3 +60,14 @@ pub(crate) fn absolute(path: &Path) -> io::Result<PathBuf> {

Ok(normalized)
}

pub(crate) fn is_absolute(path: &Path) -> bool {
if cfg!(target_os = "redox") {
// FIXME: Allow Redox prefixes
path.has_root() || crate::path::has_redox_scheme(path.as_u8_slice())
} else if cfg!(any(unix, target_os = "hermit", target_os = "wasi")) {
path.has_root()
} else {
path.has_root() && path.prefix().is_some()
}
}
4 changes: 4 additions & 0 deletions std/src/sys/path/unsupported_backslash.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,3 +24,7 @@ pub const MAIN_SEP: char = '\\';
pub(crate) fn absolute(_path: &Path) -> io::Result<PathBuf> {
unsupported()
}

pub(crate) fn is_absolute(path: &Path) -> bool {
path.has_root() && path.prefix().is_some()
}
4 changes: 4 additions & 0 deletions std/src/sys/path/windows.rs
Original file line number Diff line number Diff line change
Expand Up @@ -346,3 +346,7 @@ pub(crate) fn absolute(path: &Path) -> io::Result<PathBuf> {
os2path,
)
}

pub(crate) fn is_absolute(path: &Path) -> bool {
path.has_root() && path.prefix().is_some()
}

0 comments on commit 83edfbb

Please sign in to comment.