diff --git a/core/src/docker_bridge/virtual_fs.rs b/core/src/docker_bridge/virtual_fs.rs index e30a231d..09932cf3 100644 --- a/core/src/docker_bridge/virtual_fs.rs +++ b/core/src/docker_bridge/virtual_fs.rs @@ -3,16 +3,19 @@ use std::path::{Path, PathBuf}; use crate::util::scoped_join_win_safe; /// relative_path must start with a component that matches one of the roots -/// +/// /// For ex. -/// +/// /// `["/home/user", "/home/user/other"]`` and `"other/file.txt"`` is valid -/// +/// /// The function will match the "other" component with the second root /// and return ("/home/user/other/file.txt", "/home/user/other", "other") -/// +/// /// no backlinks are allowed in the relative_path, the function will return None -pub fn get_virtual_path(virtual_roots: &[PathBuf], relative_path: &Path) -> Option<(PathBuf, PathBuf, String)> { +pub fn get_virtual_path( + virtual_roots: &[PathBuf], + relative_path: &Path, +) -> Option<(PathBuf, PathBuf, String)> { let safe_relative_path = scoped_join_win_safe("/", relative_path).ok()?; let request_mount_point = safe_relative_path .components() @@ -20,19 +23,26 @@ pub fn get_virtual_path(virtual_roots: &[PathBuf], relative_path: &Path) -> Opti .as_os_str() .to_str()?; let request_path_without_mount_point = safe_relative_path - .strip_prefix("/".to_string() + request_mount_point).ok()?; - let mount_point = virtual_roots.iter().find(|m| m.ends_with(request_mount_point))?; + .strip_prefix("/".to_string() + request_mount_point) + .ok()?; + let mount_point = virtual_roots + .iter() + .find(|m| m.ends_with(request_mount_point))?; let path = mount_point.join(request_path_without_mount_point); Some((path, mount_point.clone(), request_mount_point.to_string())) } /// Given an absolute path, a virtual root, and a mount point, return the virtual path that is situated at the mount point -/// +/// /// For ex. -/// +/// /// `to_virtual_path("/home/user/other/file.txt", "/home/user", "mount")` will return `Some("mount/other/file.txt")` -/// +/// /// This function is prone to backlinks, so it is recommended to use it with a path that is already validated -pub fn to_virtual_path(absolute_path : &Path, absolute_virtual_root : &Path, mount_point : &str) -> Option { +pub fn to_virtual_path( + absolute_path: &Path, + absolute_virtual_root: &Path, + mount_point: &str, +) -> Option { Some(PathBuf::from(mount_point).join(absolute_path.strip_prefix(absolute_virtual_root).ok()?)) } @@ -50,7 +60,7 @@ mod tests { #[test] fn test_to_virtual_path() { - let absolute_path = Path::new("/Users/peter/dev-p/minecraft-docker/data/libraries/com"); + let absolute_path = Path::new("/home/user/other/file.txt"); let virtual_root = Path::new("/home/user"); let mount_point = "mount"; let virtual_path = to_virtual_path(absolute_path, virtual_root, mount_point).unwrap();