|
|
use std::{ |
|
|
borrow::Cow, |
|
|
io::{self, ErrorKind}, |
|
|
path::Path, |
|
|
}; |
|
|
|
|
|
use anyhow::{Context, Result, anyhow}; |
|
|
use turbo_tasks::Vc; |
|
|
|
|
|
use crate::{DiskFileSystem, FileSystemPath}; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn join_path(fs_path: &str, join: &str) -> Option<String> { |
|
|
|
|
|
|
|
|
|
|
|
debug_assert!( |
|
|
!join.contains('\\'), |
|
|
"joined path {join} must not contain a Windows directory '\\', it must be normalized to \ |
|
|
Unix '/'" |
|
|
); |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
if fs_path.is_empty() { |
|
|
normalize_path(join) |
|
|
} else if join.is_empty() { |
|
|
normalize_path(fs_path) |
|
|
} else { |
|
|
normalize_path(&[fs_path, "/", join].concat()) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[inline] |
|
|
pub fn sys_to_unix(path: &str) -> Cow<'_, str> { |
|
|
#[cfg(not(target_family = "windows"))] |
|
|
{ |
|
|
Cow::from(path) |
|
|
} |
|
|
#[cfg(target_family = "windows")] |
|
|
{ |
|
|
Cow::Owned(path.replace(std::path::MAIN_SEPARATOR_STR, "/")) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
#[inline] |
|
|
pub fn unix_to_sys(path: &str) -> Cow<'_, str> { |
|
|
#[cfg(not(target_family = "windows"))] |
|
|
{ |
|
|
Cow::from(path) |
|
|
} |
|
|
#[cfg(target_family = "windows")] |
|
|
{ |
|
|
Cow::Owned(path.replace('/', std::path::MAIN_SEPARATOR_STR)) |
|
|
} |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn normalize_path(str: &str) -> Option<String> { |
|
|
let mut segments = Vec::new(); |
|
|
for segment in str.split('/') { |
|
|
match segment { |
|
|
"." | "" => {} |
|
|
".." => { |
|
|
segments.pop()?; |
|
|
} |
|
|
segment => { |
|
|
segments.push(segment); |
|
|
} |
|
|
} |
|
|
} |
|
|
Some(segments.join("/")) |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn normalize_request(str: &str) -> String { |
|
|
let mut segments = vec!["."]; |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
let mut depth = 0; |
|
|
let mut popped_dot = false; |
|
|
for segment in str.split('/') { |
|
|
match segment { |
|
|
"." => {} |
|
|
".." => { |
|
|
if depth > 0 { |
|
|
depth -= 1; |
|
|
segments.pop(); |
|
|
} else { |
|
|
|
|
|
|
|
|
if !popped_dot { |
|
|
popped_dot = true; |
|
|
segments.pop(); |
|
|
} |
|
|
segments.push(segment); |
|
|
} |
|
|
} |
|
|
segment => { |
|
|
segments.push(segment); |
|
|
depth += 1; |
|
|
} |
|
|
} |
|
|
} |
|
|
segments.join("/") |
|
|
} |
|
|
|
|
|
|
|
|
|
|
|
|
|
|
pub fn extract_disk_access<T>(value: io::Result<T>, path: &Path) -> Result<Option<T>> { |
|
|
match value { |
|
|
Ok(v) => Ok(Some(v)), |
|
|
Err(e) if matches!(e.kind(), ErrorKind::NotFound | ErrorKind::InvalidFilename) => Ok(None), |
|
|
Err(e) => Err(anyhow!(e).context(format!("reading file {}", path.display()))), |
|
|
} |
|
|
} |
|
|
|
|
|
#[cfg(not(target_os = "windows"))] |
|
|
pub async fn uri_from_file(root: FileSystemPath, path: Option<&str>) -> Result<String> { |
|
|
let root_fs = root.fs(); |
|
|
let root_fs = &*Vc::try_resolve_downcast_type::<DiskFileSystem>(root_fs) |
|
|
.await? |
|
|
.context("Expected root to have a DiskFileSystem")? |
|
|
.await?; |
|
|
|
|
|
Ok(format!( |
|
|
"file://{}", |
|
|
&sys_to_unix( |
|
|
&root_fs |
|
|
.to_sys_path(match path { |
|
|
Some(path) => root.join(path)?, |
|
|
None => root, |
|
|
}) |
|
|
.await? |
|
|
.to_string_lossy() |
|
|
) |
|
|
.split('/') |
|
|
.map(|s| urlencoding::encode(s)) |
|
|
.collect::<Vec<_>>() |
|
|
.join("/") |
|
|
)) |
|
|
} |
|
|
|
|
|
#[cfg(target_os = "windows")] |
|
|
pub async fn uri_from_file(root: FileSystemPath, path: Option<&str>) -> Result<String> { |
|
|
let root_fs = root.fs(); |
|
|
let root_fs = &*Vc::try_resolve_downcast_type::<DiskFileSystem>(root_fs) |
|
|
.await? |
|
|
.context("Expected root to have a DiskFileSystem")? |
|
|
.await?; |
|
|
|
|
|
let sys_path = root_fs |
|
|
.to_sys_path(match path { |
|
|
Some(path) => root.join(path.into())?, |
|
|
None => root, |
|
|
}) |
|
|
.await?; |
|
|
|
|
|
let raw_path = sys_path.to_string_lossy().to_string(); |
|
|
let normalized_path = raw_path.replace('\\', "/"); |
|
|
|
|
|
let mut segments = normalized_path.split('/'); |
|
|
|
|
|
let first = segments.next().unwrap_or_default(); |
|
|
let encoded_path = std::iter::once(first.to_string()) |
|
|
.chain(segments.map(|s| urlencoding::encode(s).into_owned())) |
|
|
.collect::<Vec<_>>() |
|
|
.join("/"); |
|
|
|
|
|
let uri = format!("file:///{}", encoded_path); |
|
|
|
|
|
Ok(uri) |
|
|
} |
|
|
|
|
|
#[cfg(test)] |
|
|
mod tests { |
|
|
|
|
|
use rstest::*; |
|
|
|
|
|
use crate::util::normalize_path; |
|
|
|
|
|
#[rstest] |
|
|
#[case("file.js")] |
|
|
#[case("a/b/c/d/e/file.js")] |
|
|
fn test_normalize_path_no_op(#[case] path: &str) { |
|
|
assert_eq!(path, normalize_path(path).unwrap()); |
|
|
} |
|
|
|
|
|
#[rstest] |
|
|
#[case("/file.js", "file.js")] |
|
|
#[case("./file.js", "file.js")] |
|
|
#[case("././file.js", "file.js")] |
|
|
#[case("a/../c/../file.js", "file.js")] |
|
|
fn test_normalize_path(#[case] path: &str, #[case] normalized: &str) { |
|
|
assert_eq!(normalized, normalize_path(path).unwrap()); |
|
|
} |
|
|
|
|
|
#[rstest] |
|
|
#[case("../file.js")] |
|
|
#[case("a/../../file.js")] |
|
|
fn test_normalize_path_invalid(#[case] path: &str) { |
|
|
assert_eq!(None, normalize_path(path)); |
|
|
} |
|
|
} |
|
|
|