From b272f6ca05e9210dbe6c605a301d5a16007f322f Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Fri, 18 Aug 2017 12:04:45 -0700 Subject: [PATCH 1/4] redox: Require scheme for path to be absolute Redox paths are problematic. It would make sense to add a `Scheme` variant to the `std::path::Component` enum; but that would presumably be a breaking change due to exhaustive matching. Alternately it could use the existing `Prefix` variant, like Windows, but none of the existing types of prefix make sense, Redox only has one kind, and adding a new variant to that enum has the same issue as `Component`. --- src/libstd/path.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 4496de09b2590..866b65ac7e4c3 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -1685,8 +1685,16 @@ impl Path { #[stable(feature = "rust1", since = "1.0.0")] #[allow(deprecated)] pub fn is_absolute(&self) -> bool { - // FIXME: Remove target_os = "redox" and allow Redox prefixes - self.has_root() && (cfg!(unix) || cfg!(target_os = "redox") || self.prefix().is_some()) + #[cfg(not(target_os = "redox"))] + { + self.has_root() && (cfg!(unix) || self.prefix().is_some()) + } + #[cfg(target_os = "redox")] + { + // FIXME: Allow Redox prefixes + use os::unix::ffi::OsStrExt; + self.as_os_str().as_bytes().split(|b| *b == b'/').next().unwrap_or(b"").contains(&b':') + } } /// Returns `true` if the `Path` is relative, i.e. not absolute. From e0f0fd08b5e823561848c9541ac432eb30b976bd Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Fri, 18 Aug 2017 15:56:13 -0700 Subject: [PATCH 2/4] Correct has_root() on Redox --- src/libstd/path.rs | 23 ++++++++++++++++++++--- 1 file changed, 20 insertions(+), 3 deletions(-) diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 866b65ac7e4c3..5757d447c54d0 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -323,6 +323,20 @@ unsafe fn u8_slice_as_os_str(s: &[u8]) -> &OsStr { mem::transmute(s) } +// Detect scheme on Redox +#[inline] +#[allow(unused_variables)] +fn has_scheme(s: &[u8]) -> bool { + #[cfg(target_os = "redox")] + { + s.split(|b| *b == b'/').next().unwrap_or(b"").contains(&b':') + } + #[cfg(not(target_os = "redox"))] + { + false + } +} + //////////////////////////////////////////////////////////////////////////////// // Cross-platform, iterator-independent parsing //////////////////////////////////////////////////////////////////////////////// @@ -605,6 +619,9 @@ pub struct Components<'a> { // normalization, e.g. \\server\share == \\server\share\. has_physical_root: bool, + // For Redox + has_scheme: bool, + // The iterator is double-ended, and these two states keep track of what has // been produced from either end front: State, @@ -725,7 +742,7 @@ impl<'a> Components<'a> { /// Is the *original* path rooted? fn has_root(&self) -> bool { - if self.has_physical_root { + if self.has_physical_root || self.has_scheme { return true; } if let Some(p) = self.prefix { @@ -1692,8 +1709,7 @@ impl Path { #[cfg(target_os = "redox")] { // FIXME: Allow Redox prefixes - use os::unix::ffi::OsStrExt; - self.as_os_str().as_bytes().split(|b| *b == b'/').next().unwrap_or(b"").contains(&b':') + has_scheme(self.as_u8_slice()) } } @@ -2059,6 +2075,7 @@ impl Path { path: self.as_u8_slice(), prefix, has_physical_root: has_physical_root(self.as_u8_slice(), prefix), + has_scheme: has_scheme(self.as_u8_slice()), front: State::Prefix, back: State::Body, } From ab48de88472f19596ba2a67ff4e8a8c1c4015989 Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Tue, 22 Aug 2017 08:17:05 -0700 Subject: [PATCH 3/4] Use cfg! instead of #[cfg] --- src/libstd/path.rs | 16 +++------------- 1 file changed, 3 insertions(+), 13 deletions(-) diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 5757d447c54d0..32e1781c3c45c 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -327,14 +327,7 @@ unsafe fn u8_slice_as_os_str(s: &[u8]) -> &OsStr { #[inline] #[allow(unused_variables)] fn has_scheme(s: &[u8]) -> bool { - #[cfg(target_os = "redox")] - { - s.split(|b| *b == b'/').next().unwrap_or(b"").contains(&b':') - } - #[cfg(not(target_os = "redox"))] - { - false - } + cfg!(target_os = "redox") && s.split(|b| *b == b'/').next().unwrap_or(b"").contains(&b':') } //////////////////////////////////////////////////////////////////////////////// @@ -1702,12 +1695,9 @@ impl Path { #[stable(feature = "rust1", since = "1.0.0")] #[allow(deprecated)] pub fn is_absolute(&self) -> bool { - #[cfg(not(target_os = "redox"))] - { + if !cfg!(target_os = "redox") { self.has_root() && (cfg!(unix) || self.prefix().is_some()) - } - #[cfg(target_os = "redox")] - { + } else { // FIXME: Allow Redox prefixes has_scheme(self.as_u8_slice()) } From fe2d661931852c4303b45bed472640bd0b32448f Mon Sep 17 00:00:00 2001 From: Ian Douglas Scott Date: Tue, 22 Aug 2017 10:33:26 -0700 Subject: [PATCH 4/4] Simplify code for handling Redox paths --- src/libstd/path.rs | 15 +++++---------- 1 file changed, 5 insertions(+), 10 deletions(-) diff --git a/src/libstd/path.rs b/src/libstd/path.rs index 32e1781c3c45c..d529b2153e151 100644 --- a/src/libstd/path.rs +++ b/src/libstd/path.rs @@ -324,9 +324,7 @@ unsafe fn u8_slice_as_os_str(s: &[u8]) -> &OsStr { } // Detect scheme on Redox -#[inline] -#[allow(unused_variables)] -fn has_scheme(s: &[u8]) -> bool { +fn has_redox_scheme(s: &[u8]) -> bool { cfg!(target_os = "redox") && s.split(|b| *b == b'/').next().unwrap_or(b"").contains(&b':') } @@ -612,9 +610,6 @@ pub struct Components<'a> { // normalization, e.g. \\server\share == \\server\share\. has_physical_root: bool, - // For Redox - has_scheme: bool, - // The iterator is double-ended, and these two states keep track of what has // been produced from either end front: State, @@ -735,7 +730,7 @@ impl<'a> Components<'a> { /// Is the *original* path rooted? fn has_root(&self) -> bool { - if self.has_physical_root || self.has_scheme { + if self.has_physical_root { return true; } if let Some(p) = self.prefix { @@ -1699,7 +1694,7 @@ impl Path { self.has_root() && (cfg!(unix) || self.prefix().is_some()) } else { // FIXME: Allow Redox prefixes - has_scheme(self.as_u8_slice()) + has_redox_scheme(self.as_u8_slice()) } } @@ -2064,8 +2059,8 @@ impl Path { Components { path: self.as_u8_slice(), prefix, - has_physical_root: has_physical_root(self.as_u8_slice(), prefix), - has_scheme: has_scheme(self.as_u8_slice()), + has_physical_root: has_physical_root(self.as_u8_slice(), prefix) || + has_redox_scheme(self.as_u8_slice()), front: State::Prefix, back: State::Body, }