Use redox-path external crate
This commit is contained in:
Generated
+5
-3
@@ -10,9 +10,9 @@ checksum = "d468802bab17cbc0cc575e9b053f41e72aa36bfa6b7f55e3529ffa43161b97fa"
|
||||
|
||||
[[package]]
|
||||
name = "bitflags"
|
||||
version = "2.4.1"
|
||||
version = "2.4.2"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "327762f6e5a765692301e5bb513e0d9fef63be86bbc14528052b1cd3e6f03e07"
|
||||
checksum = "ed570934406eb16438a4e976b1b4500774099c13b8cb96eec99f620f05090ddf"
|
||||
|
||||
[[package]]
|
||||
name = "cbitset"
|
||||
@@ -200,7 +200,9 @@ dependencies = [
|
||||
|
||||
[[package]]
|
||||
name = "redox-path"
|
||||
version = "0.1.0"
|
||||
version = "0.1.1"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "f45c7275fe1467ea17542c01766ae9872fa98aa7fe06ba5ea6595f7a9f0699c6"
|
||||
|
||||
[[package]]
|
||||
name = "redox_syscall"
|
||||
|
||||
+1
-2
@@ -15,7 +15,6 @@ members = [
|
||||
"src/crtn",
|
||||
"src/ld_so",
|
||||
"src/platform/redox/redox-exec",
|
||||
"src/platform/redox/redox-path",
|
||||
]
|
||||
exclude = ["tests", "dlmalloc-rs"]
|
||||
|
||||
@@ -54,7 +53,7 @@ sc = "0.2.3"
|
||||
[target.'cfg(target_os = "redox")'.dependencies]
|
||||
redox_syscall = "0.4"
|
||||
redox-exec = { path = "src/platform/redox/redox-exec" }
|
||||
redox-path = { path = "src/platform/redox/redox-path" }
|
||||
redox-path = "0.1"
|
||||
|
||||
[features]
|
||||
default = ["check_against_libc_crate"]
|
||||
|
||||
@@ -1,8 +0,0 @@
|
||||
[package]
|
||||
name = "redox-path"
|
||||
version = "0.1.0"
|
||||
edition = "2021"
|
||||
|
||||
# See more keys and their definitions at https://doc.rust-lang.org/cargo/reference/manifest.html
|
||||
|
||||
[dependencies]
|
||||
@@ -1,171 +0,0 @@
|
||||
#![no_std]
|
||||
|
||||
extern crate alloc;
|
||||
|
||||
use alloc::{borrow::ToOwned, format, string::String, vec::Vec};
|
||||
|
||||
pub fn split_path<'a>(path: &'a str) -> Option<(&'a str, &'a str)> {
|
||||
let mut parts = path.splitn(2, ':');
|
||||
let scheme = parts.next()?;
|
||||
let reference = parts.next()?;
|
||||
Some((scheme, reference))
|
||||
}
|
||||
|
||||
/// Make a relative path absolute.
|
||||
///
|
||||
/// Given a cwd of "scheme:/path", this his function will turn "foo" into "scheme:/path/foo".
|
||||
/// "/foo" will turn into "file:/foo". "bar:/foo" will be used directly, as it is already
|
||||
/// absolute
|
||||
pub fn canonicalize_using_cwd<'a>(cwd_opt: Option<&str>, path: &'a str) -> Option<String> {
|
||||
let (scheme, reference) = match split_path(path) {
|
||||
Some((scheme, reference)) => (scheme, reference.to_owned()),
|
||||
None => {
|
||||
let cwd = cwd_opt?;
|
||||
let (scheme, reference) = split_path(cwd)?;
|
||||
if path.starts_with('/') {
|
||||
(scheme, path.to_owned())
|
||||
} else {
|
||||
let mut reference = reference.to_owned();
|
||||
if !reference.ends_with('/') {
|
||||
reference.push('/');
|
||||
}
|
||||
reference.push_str(path);
|
||||
(scheme, reference)
|
||||
}
|
||||
}
|
||||
};
|
||||
|
||||
let mut canonical = {
|
||||
let parts = reference
|
||||
.split('/')
|
||||
.rev()
|
||||
.scan(0, |nskip, part| {
|
||||
if part == "." {
|
||||
Some(None)
|
||||
} else if part == ".." {
|
||||
*nskip += 1;
|
||||
Some(None)
|
||||
} else if *nskip > 0 {
|
||||
*nskip -= 1;
|
||||
Some(None)
|
||||
} else {
|
||||
Some(Some(part))
|
||||
}
|
||||
})
|
||||
.filter_map(|x| x)
|
||||
.filter(|x| !x.is_empty())
|
||||
.collect::<Vec<_>>();
|
||||
parts.iter().rev().fold(String::new(), |mut string, &part| {
|
||||
string.push_str(part);
|
||||
string.push('/');
|
||||
string
|
||||
})
|
||||
};
|
||||
canonical.pop(); // remove extra '/'
|
||||
|
||||
Some(format!("{}:{}", scheme, canonical))
|
||||
}
|
||||
|
||||
#[cfg(test)]
|
||||
mod tests {
|
||||
use super::*;
|
||||
use alloc::string::ToString;
|
||||
|
||||
// Tests absolute paths without scheme
|
||||
#[test]
|
||||
fn test_absolute() {
|
||||
let cwd_opt = Some("foo:");
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "/"),
|
||||
Some("foo:".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "/file"),
|
||||
Some("foo:file".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "/folder/file"),
|
||||
Some("foo:folder/file".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "/folder/../file"),
|
||||
Some("foo:file".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "/folder/../.."),
|
||||
Some("foo:".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "/folder/../../../.."),
|
||||
Some("foo:".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "/.."),
|
||||
Some("foo:".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
// Test relative paths
|
||||
#[test]
|
||||
fn test_relative() {
|
||||
let cwd_opt = Some("foo:");
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "file"),
|
||||
Some("foo:file".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "folder/file"),
|
||||
Some("foo:folder/file".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "folder/../file"),
|
||||
Some("foo:file".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "folder/../.."),
|
||||
Some("foo:".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "folder/../../../.."),
|
||||
Some("foo:".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, ".."),
|
||||
Some("foo:".to_string())
|
||||
);
|
||||
}
|
||||
|
||||
// Tests paths prefixed with scheme
|
||||
#[test]
|
||||
fn test_scheme() {
|
||||
let cwd_opt = Some("foo:");
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "bar:"),
|
||||
Some("bar:".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "bar:file"),
|
||||
Some("bar:file".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "bar:folder/file"),
|
||||
Some("bar:folder/file".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "bar:folder/../file"),
|
||||
Some("bar:file".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "bar:folder/../.."),
|
||||
Some("bar:".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "bar:folder/../../../.."),
|
||||
Some("bar:".to_string())
|
||||
);
|
||||
assert_eq!(
|
||||
canonicalize_using_cwd(cwd_opt, "bar:.."),
|
||||
Some("bar:".to_string())
|
||||
);
|
||||
}
|
||||
}
|
||||
Reference in New Issue
Block a user