Repository navigation
Expand file tree
/
Copy pathpaths.rs
More file actions
119 lines (107 loc) · 4.06 KB
/
Copy pathpaths.rs
File metadata and controls
119 lines (107 loc) · 4.06 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
//! Canonicalization minus Windows' `\\?\` prefix, which `CreateProcessW` rejects as a cwd.
//! Every canonicalization goes through here so containment checks compare one spelling.
//! Also the running binary's spawnable path, minus Linux's replaced-binary marker.
#[cfg(windows)]
use std::path::{Component, Prefix};
use std::path::{Path, PathBuf};
/// `std::fs::canonicalize`, minus the Windows verbatim prefix.
pub fn canonicalize<P: AsRef<Path>>(path: P) -> std::io::Result<PathBuf> {
std::fs::canonicalize(path).map(plain)
}
/// `std::env::current_exe`, as a path to spawn. Linux reports a binary replaced
/// under a live process as `<path> (deleted)`; its replacement, if any, is at `<path>`.
pub fn spawnable_exe() -> std::io::Result<PathBuf> {
std::env::current_exe().map(undeleted)
}
fn undeleted(exe: PathBuf) -> PathBuf {
exe.to_str()
.and_then(|exe| exe.strip_suffix(" (deleted)"))
.map(PathBuf::from)
.unwrap_or(exe)
}
/// Whether the canonical `path` is inside the Linux AppImage mounted at `appdir`
/// (its runtime's `APPDIR`, which every program the app starts inherits).
pub fn in_appimage_mount(path: &Path, appdir: Option<&std::ffi::OsStr>) -> bool {
let Some(appdir) = appdir.map(Path::new) else {
return false;
};
// An empty or root APPDIR would claim every path on the machine.
if !appdir.is_absolute() || appdir.parent().is_none() {
return false;
}
// The runtime mounts under a temp dir that may be a symlink.
let appdir = canonicalize(appdir).unwrap_or_else(|_| appdir.to_path_buf());
path.starts_with(appdir)
}
#[cfg(not(windows))]
fn plain(path: PathBuf) -> PathBuf {
path
}
/// Matched on the parsed prefix: a non-UTF-8 path has no `to_str` and would stay verbatim.
#[cfg(windows)]
fn plain(path: PathBuf) -> PathBuf {
let mut components = path.components();
let Some(Component::Prefix(prefix)) = components.next() else {
return path;
};
let head = match prefix.kind() {
Prefix::VerbatimDisk(drive) => std::ffi::OsString::from(format!("{}:\\", drive as char)),
// `\\?\UNC\server\share` is an encoding of `\\server\share`.
Prefix::VerbatimUNC(server, share) => {
let mut head = std::ffi::OsString::from(r"\\");
head.push(server);
head.push(r"\");
head.push(share);
head.push(r"\");
head
}
// A device path is itself, not an encoding of anything shorter.
_ => return path,
};
let mut out = PathBuf::from(head);
out.extend(components.filter(|part| !matches!(part, Component::RootDir)));
out
}
#[cfg(test)]
mod spawnable_exe_tests {
use super::*;
#[test]
fn the_deleted_marker_is_stripped() {
assert_eq!(
undeleted(PathBuf::from("/x/orx (deleted)")),
PathBuf::from("/x/orx")
);
assert_eq!(undeleted(PathBuf::from("/x/orx")), PathBuf::from("/x/orx"));
}
}
#[cfg(all(test, windows))]
mod tests {
use super::*;
#[test]
fn a_verbatim_path_keeps_only_the_spelling_a_child_process_accepts() {
assert_eq!(
plain(PathBuf::from(r"\\?\C:\Users\me")),
PathBuf::from(r"C:\Users\me")
);
assert_eq!(
plain(PathBuf::from(r"\\?\UNC\server\share\dir")),
PathBuf::from(r"\\server\share\dir")
);
// Not an encoding of a shorter path, so it stays as it is.
let device = PathBuf::from(r"\\.\PIPE\orx");
assert_eq!(plain(device.clone()), device);
let plain_already = PathBuf::from(r"C:\Users\me");
assert_eq!(plain(plain_already.clone()), plain_already);
}
#[test]
fn a_canonicalized_directory_is_accepted_as_a_working_directory() {
let dir = canonicalize(std::env::temp_dir()).expect("temp dir");
let out = crate::process::command("cmd")
.args(["/C", "cd"])
.current_dir(&dir)
.output()
.expect("cmd");
assert!(out.status.success());
assert!(!String::from_utf8_lossy(&out.stdout).contains(r"\\?\"));
}
}