|
| 1 | +// Copyright 2019-2023 Tauri Programme within The Commons Conservancy |
| 2 | +// SPDX-License-Identifier: Apache-2.0 |
| 3 | +// SPDX-License-Identifier: MIT |
| 4 | + |
| 5 | +use std::{ |
| 6 | + borrow::Cow, |
| 7 | + ffi::OsString, |
| 8 | + io, |
| 9 | + os::windows::ffi::OsStringExt, |
| 10 | + path::{Component, Path, PathBuf, Prefix, PrefixComponent}, |
| 11 | +}; |
| 12 | + |
| 13 | +use windows::{core::HSTRING, Win32::Storage::FileSystem::GetFullPathNameW}; |
| 14 | + |
| 15 | +pub fn absolute_and_check_exists(path: &Path) -> io::Result<PathBuf> { |
| 16 | + let path = absolute(path)?; |
| 17 | + if path.exists() { |
| 18 | + Ok(path) |
| 19 | + } else { |
| 20 | + Err(std::io::Error::new( |
| 21 | + std::io::ErrorKind::NotFound, |
| 22 | + "path doesn't exist", |
| 23 | + )) |
| 24 | + } |
| 25 | +} |
| 26 | + |
| 27 | +// TODO: Switch to use `std::path::absolute` once MSRV > 1.79 |
| 28 | +// Modified from https://github.com/rust-lang/rust/blob/b49ecc9eb70a51e89f32a7358e790f7b3808ccb3/library/std/src/sys/path/windows.rs#L185 |
| 29 | +// Note: this doesn't resolve symlinks |
| 30 | +fn absolute(path: &Path) -> io::Result<PathBuf> { |
| 31 | + if path.as_os_str().is_empty() { |
| 32 | + return Err(io::Error::new( |
| 33 | + io::ErrorKind::InvalidInput, |
| 34 | + "cannot make an empty path absolute", |
| 35 | + )); |
| 36 | + } |
| 37 | + |
| 38 | + let prefix = path.components().next(); |
| 39 | + // Verbatim paths should not be modified. |
| 40 | + if prefix |
| 41 | + .map(|component| { |
| 42 | + let Component::Prefix(prefix) = component else { |
| 43 | + return false; |
| 44 | + }; |
| 45 | + matches!( |
| 46 | + prefix.kind(), |
| 47 | + Prefix::Verbatim(..) | Prefix::VerbatimDisk(..) | Prefix::VerbatimUNC(..) |
| 48 | + ) |
| 49 | + }) |
| 50 | + .unwrap_or(false) |
| 51 | + { |
| 52 | + // NULs in verbatim paths are rejected for consistency. |
| 53 | + if path.as_os_str().as_encoded_bytes().contains(&0) { |
| 54 | + return Err(io::Error::new( |
| 55 | + io::ErrorKind::InvalidInput, |
| 56 | + "strings passed to WinAPI cannot contain NULs", |
| 57 | + )); |
| 58 | + } |
| 59 | + return Ok(path.to_owned()); |
| 60 | + } |
| 61 | + |
| 62 | + // This is an additional check to make sure we don't pass in a single driver letter to GetFullPathNameW |
| 63 | + // which will resolves to the current working directory |
| 64 | + // |
| 65 | + // > https://learn.microsoft.com/en-us/windows/win32/api/fileapi/nf-fileapi-getfullpathnamew#:~:text=If%20you%20specify%20%22U%3A%22%20the%20path%20returned%20is%20the%20current%20directory%20on%20the%20%22U%3A%5C%22%20drive |
| 66 | + #[allow(clippy::collapsible_if)] |
| 67 | + if let Some(Component::Prefix(last_prefix)) = path.components().next_back() { |
| 68 | + if matches!(last_prefix.kind(), Prefix::Disk(..)) { |
| 69 | + return Ok(PathBuf::from(last_prefix.as_os_str())); |
| 70 | + } |
| 71 | + } |
| 72 | + |
| 73 | + let path_hstring = HSTRING::from(path); |
| 74 | + |
| 75 | + let size = unsafe { GetFullPathNameW(&path_hstring, None, None) }; |
| 76 | + if size == 0 { |
| 77 | + return Err(io::Error::last_os_error()); |
| 78 | + } |
| 79 | + let mut buffer = vec![0; size as usize]; |
| 80 | + let size = unsafe { GetFullPathNameW(&path_hstring, Some(&mut buffer), None) }; |
| 81 | + if size == 0 { |
| 82 | + return Err(io::Error::last_os_error()); |
| 83 | + } |
| 84 | + |
| 85 | + Ok(PathBuf::from(OsString::from_wide(&buffer[..size as usize]))) |
| 86 | +} |
| 87 | + |
| 88 | +/// Similar to [`Path::parent`] but resolves parent of `C:`/`C:\` to `""` and handles UNC host name (`\\wsl.localhost\Ubuntu\` to `\\wsl.localhost`) |
| 89 | +pub fn shell_parent_path(path: &Path) -> Option<Cow<'_, Path>> { |
| 90 | + fn handle_prefix(prefix: PrefixComponent<'_>) -> Option<Cow<'_, Path>> { |
| 91 | + match prefix.kind() { |
| 92 | + Prefix::UNC(host_name, _share_name) => { |
| 93 | + let mut path = OsString::from(r"\\"); |
| 94 | + path.push(host_name); |
| 95 | + Some(PathBuf::from(path).into()) |
| 96 | + } |
| 97 | + Prefix::Disk(_) => Some(PathBuf::from("").into()), |
| 98 | + _ => None, |
| 99 | + } |
| 100 | + } |
| 101 | + |
| 102 | + let mut components = path.components(); |
| 103 | + let component = components.next_back()?; |
| 104 | + match component { |
| 105 | + Component::Normal(_) | Component::CurDir | Component::ParentDir => { |
| 106 | + Some(components.as_path().into()) |
| 107 | + } |
| 108 | + Component::Prefix(prefix) => handle_prefix(prefix), |
| 109 | + // Handle cases like `C:\` and `\\wsl.localhost\Ubuntu\` |
| 110 | + Component::RootDir => { |
| 111 | + if let Component::Prefix(prefix) = components.next_back()? { |
| 112 | + handle_prefix(prefix) |
| 113 | + } else { |
| 114 | + None |
| 115 | + } |
| 116 | + } |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +#[cfg(test)] |
| 121 | +mod tests { |
| 122 | + use super::*; |
| 123 | + use std::path::Path; |
| 124 | + |
| 125 | + // absolute() tests |
| 126 | + |
| 127 | + #[test] |
| 128 | + fn absolute_empty_error() { |
| 129 | + let err = absolute(Path::new("")).unwrap_err(); |
| 130 | + assert_eq!(err.kind(), io::ErrorKind::InvalidInput); |
| 131 | + } |
| 132 | + |
| 133 | + #[test] |
| 134 | + fn absolute_verbatim_passthrough() { |
| 135 | + let path = Path::new(r"\\?\C:\foo"); |
| 136 | + assert_eq!(absolute(path).unwrap(), path); |
| 137 | + } |
| 138 | + |
| 139 | + #[test] |
| 140 | + fn absolute_verbatim_unc_passthrough() { |
| 141 | + let path = Path::new(r"\\?\UNC\server\share"); |
| 142 | + assert_eq!(absolute(path).unwrap(), path); |
| 143 | + } |
| 144 | + |
| 145 | + #[test] |
| 146 | + fn absolute_bare_drive_letter() { |
| 147 | + let result = absolute(Path::new("C:")).unwrap(); |
| 148 | + assert_eq!(result, Path::new("C:")); |
| 149 | + } |
| 150 | + |
| 151 | + #[test] |
| 152 | + fn absolute_already_absolute() { |
| 153 | + let result = absolute(Path::new(r"C:\Windows")).unwrap(); |
| 154 | + assert_eq!(result, Path::new(r"C:\Windows")); |
| 155 | + } |
| 156 | + |
| 157 | + #[test] |
| 158 | + fn absolute_unc_path() { |
| 159 | + let result = absolute(Path::new(r"\\server\share\folder")).unwrap(); |
| 160 | + assert_eq!(result, Path::new(r"\\server\share\folder")); |
| 161 | + } |
| 162 | + |
| 163 | + #[test] |
| 164 | + fn absolute_converts_forward_slashes() { |
| 165 | + let result = absolute(Path::new("C:/Windows/System32")).unwrap(); |
| 166 | + assert_eq!(result, Path::new(r"C:\Windows\System32")); |
| 167 | + } |
| 168 | + |
| 169 | + // absolute_and_check_exists() tests |
| 170 | + |
| 171 | + #[test] |
| 172 | + fn absolute_and_check_exists_existing_path() { |
| 173 | + assert!(absolute_and_check_exists(Path::new(r"C:\Windows")).is_ok()); |
| 174 | + } |
| 175 | + |
| 176 | + #[test] |
| 177 | + fn absolute_and_check_exists_nonexistent_path() { |
| 178 | + let err = absolute_and_check_exists(Path::new(r"C:\nonexistent_xyz_12345")).unwrap_err(); |
| 179 | + assert_eq!(err.kind(), io::ErrorKind::NotFound); |
| 180 | + } |
| 181 | + |
| 182 | + #[test] |
| 183 | + fn absolute_and_check_exists_empty_propagates() { |
| 184 | + let err = absolute_and_check_exists(Path::new("")).unwrap_err(); |
| 185 | + assert_eq!(err.kind(), io::ErrorKind::InvalidInput); |
| 186 | + } |
| 187 | + |
| 188 | + // shell_parent_path() tests |
| 189 | + |
| 190 | + #[test] |
| 191 | + fn shell_parent_path_local_path() { |
| 192 | + let result = shell_parent_path(Path::new(r"C:\Users\foo")); |
| 193 | + assert_eq!(result.as_deref(), Some(Path::new(r"C:\Users"))); |
| 194 | + } |
| 195 | + |
| 196 | + #[test] |
| 197 | + fn shell_parent_path_nested_path() { |
| 198 | + let result = shell_parent_path(Path::new(r"C:\a\b\c\d")); |
| 199 | + assert_eq!(result.as_deref(), Some(Path::new(r"C:\a\b\c"))); |
| 200 | + } |
| 201 | + |
| 202 | + #[test] |
| 203 | + fn shell_parent_path_drive_root_trailing() { |
| 204 | + let result = shell_parent_path(Path::new(r"C:\")); |
| 205 | + assert_eq!(result.as_deref(), Some(Path::new(""))); |
| 206 | + } |
| 207 | + |
| 208 | + #[test] |
| 209 | + fn shell_parent_path_bare_drive() { |
| 210 | + let result = shell_parent_path(Path::new("C:")); |
| 211 | + assert_eq!(result.as_deref(), Some(Path::new(""))); |
| 212 | + } |
| 213 | + |
| 214 | + #[test] |
| 215 | + fn shell_parent_path_unc_with_subfolder() { |
| 216 | + let result = shell_parent_path(Path::new(r"\\server\share\folder")); |
| 217 | + assert_eq!(result.as_deref(), Some(Path::new(r"\\server\share"))); |
| 218 | + } |
| 219 | + |
| 220 | + #[test] |
| 221 | + fn shell_parent_path_unc_share_trailing_slash() { |
| 222 | + let result = shell_parent_path(Path::new(r"\\server.local\share\")); |
| 223 | + assert_eq!(result.as_deref(), Some(Path::new(r"\\server.local"))); |
| 224 | + } |
| 225 | + |
| 226 | + #[test] |
| 227 | + fn shell_parent_path_unc_share_no_slash() { |
| 228 | + let result = shell_parent_path(Path::new(r"\\server\share")); |
| 229 | + assert_eq!(result.as_deref(), Some(Path::new(r"\\server"))); |
| 230 | + } |
| 231 | + |
| 232 | + #[test] |
| 233 | + fn shell_parent_path_relative() { |
| 234 | + let result = shell_parent_path(Path::new(r"foo\bar")); |
| 235 | + assert_eq!(result.as_deref(), Some(Path::new("foo"))); |
| 236 | + } |
| 237 | + |
| 238 | + #[test] |
| 239 | + fn shell_parent_path_single_component() { |
| 240 | + let result = shell_parent_path(Path::new("foo")); |
| 241 | + assert_eq!(result.as_deref(), Some(Path::new(""))); |
| 242 | + } |
| 243 | + |
| 244 | + #[test] |
| 245 | + fn shell_parent_path_empty() { |
| 246 | + let result = shell_parent_path(Path::new("")); |
| 247 | + assert!(result.is_none()); |
| 248 | + } |
| 249 | + |
| 250 | + #[test] |
| 251 | + fn shell_parent_path_verbatim() { |
| 252 | + let result = shell_parent_path(Path::new(r"\\?\C:\foo")); |
| 253 | + assert_eq!(result.as_deref(), Some(Path::new(r"\\?\C:\"))); |
| 254 | + } |
| 255 | +} |
0 commit comments