Skip to content

Commit 8908e00

Browse files
Ayush1325gitbot
authored and
gitbot
committed
path: Move is_absolute check to sys::path
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>
1 parent 08749e2 commit 8908e00

File tree

5 files changed

+27
-11
lines changed

5 files changed

+27
-11
lines changed

std/src/path.rs

+4-11
Original file line numberDiff line numberDiff line change
@@ -298,7 +298,7 @@ where
298298
}
299299

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

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

@@ -2323,14 +2323,7 @@ impl Path {
23232323
#[must_use]
23242324
#[allow(deprecated)]
23252325
pub fn is_absolute(&self) -> bool {
2326-
if cfg!(target_os = "redox") {
2327-
// FIXME: Allow Redox prefixes
2328-
self.has_root() || has_redox_scheme(self.as_u8_slice())
2329-
} else {
2330-
self.has_root()
2331-
&& (cfg!(any(unix, target_os = "hermit", target_os = "wasi"))
2332-
|| self.prefix().is_some())
2333-
}
2326+
sys::path::is_absolute(self)
23342327
}
23352328

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

2356-
fn prefix(&self) -> Option<Prefix<'_>> {
2349+
pub(crate) fn prefix(&self) -> Option<Prefix<'_>> {
23572350
self.components().prefix
23582351
}
23592352

std/src/sys/path/sgx.rs

+4
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,7 @@ pub const MAIN_SEP: char = '/';
2323
pub(crate) fn absolute(_path: &Path) -> io::Result<PathBuf> {
2424
unsupported()
2525
}
26+
27+
pub(crate) fn is_absolute(path: &Path) -> bool {
28+
path.has_root() && path.prefix().is_some()
29+
}

std/src/sys/path/unix.rs

+11
Original file line numberDiff line numberDiff line change
@@ -60,3 +60,14 @@ pub(crate) fn absolute(path: &Path) -> io::Result<PathBuf> {
6060

6161
Ok(normalized)
6262
}
63+
64+
pub(crate) fn is_absolute(path: &Path) -> bool {
65+
if cfg!(target_os = "redox") {
66+
// FIXME: Allow Redox prefixes
67+
path.has_root() || crate::path::has_redox_scheme(path.as_u8_slice())
68+
} else if cfg!(any(unix, target_os = "hermit", target_os = "wasi")) {
69+
path.has_root()
70+
} else {
71+
path.has_root() && path.prefix().is_some()
72+
}
73+
}

std/src/sys/path/unsupported_backslash.rs

+4
Original file line numberDiff line numberDiff line change
@@ -24,3 +24,7 @@ pub const MAIN_SEP: char = '\\';
2424
pub(crate) fn absolute(_path: &Path) -> io::Result<PathBuf> {
2525
unsupported()
2626
}
27+
28+
pub(crate) fn is_absolute(path: &Path) -> bool {
29+
path.has_root() && path.prefix().is_some()
30+
}

std/src/sys/path/windows.rs

+4
Original file line numberDiff line numberDiff line change
@@ -346,3 +346,7 @@ pub(crate) fn absolute(path: &Path) -> io::Result<PathBuf> {
346346
os2path,
347347
)
348348
}
349+
350+
pub(crate) fn is_absolute(path: &Path) -> bool {
351+
path.has_root() && path.prefix().is_some()
352+
}

0 commit comments

Comments
 (0)