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
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
use std::path::PathBuf;
/// Streaming path normalization with push/pop operations.
/// This replaces intermediate Vec allocation with direct PathBuf manipulation.
/// Contract:
/// - Input: any Path (absolute/relative)
/// - Output: a PathBuf where `.` is removed and `..` pops one component when possible
/// - Root semantics are preserved (never pops past root)
#[inline]
pub(crate) fn simple_normalize_path(path: &std::path::Path) -> PathBuf {
#[cfg(windows)]
{
use std::ffi::OsString;
use std::path::{Component, Prefix};
// Capture prefix and root semantics, and normalize components lexically with clamping
enum Anchor {
None,
Drive(OsString), // e.g., "C:"
Unc(OsString, OsString), // (server, share)
DeviceNS, // raw device prefix (e.g., \\., \\?\GLOBALROOT\...)
}
let mut anchor = Anchor::None;
let mut prefix_os: Option<OsString> = None; // original prefix text
let mut has_root_dir = false;
let mut stack: Vec<OsString> = Vec::new();
for comp in path.components() {
match comp {
Component::Prefix(p) => {
// Identify and preserve prefix verbatim, but capture parsed parts for UNC/Drive
prefix_os = Some(p.as_os_str().to_os_string());
match p.kind() {
Prefix::UNC(server, share) | Prefix::VerbatimUNC(server, share) => {
anchor = Anchor::Unc(server.to_os_string(), share.to_os_string());
}
Prefix::Disk(d) | Prefix::VerbatimDisk(d) => {
// Store like "C:" — build without an intermediate String allocation.
// `d` is a Windows drive letter (A-Z), so always valid ASCII/UTF-8.
let bytes = [d, b':'];
let drive_str = std::str::from_utf8(&bytes).unwrap_or_default();
let mut s = OsString::with_capacity(2);
s.push(drive_str);
anchor = Anchor::Drive(s);
// For drive-absolute, RootDir will activate floor
}
Prefix::DeviceNS(_) | Prefix::Verbatim(_) => {
anchor = Anchor::DeviceNS;
}
}
}
Component::RootDir => {
has_root_dir = true;
}
Component::CurDir => {
// skip
}
Component::Normal(name) => {
stack.push(name.to_os_string());
}
Component::ParentDir => {
if !stack.is_empty() {
stack.pop();
} else {
// Either no floor or at floor: ignore/clamp, do nothing
}
}
}
}
// Fallback: if no anchor detected but the raw path starts with two slashes (UNC-like),
// treat the first two components as server/share and clamp at that share root.
if matches!(anchor, Anchor::None) {
// Detect raw leading UNC (\\server\share) and override anchor,
// excluding verbatim (\\?\) and device (\\.\) namespaces.
let raw = path.as_os_str().to_string_lossy();
if raw.starts_with("\\\\") && !raw.starts_with("\\\\?\\") && !raw.starts_with("\\\\.\\")
{
// Tokenize by both separators
let mut parts = raw
.trim_start_matches(['\\', '/'])
.split(['\\', '/'])
.filter(|s| !s.is_empty());
if let (Some(server_s), Some(share_s)) = (parts.next(), parts.next()) {
let server = std::ffi::OsString::from(server_s);
let share = std::ffi::OsString::from(share_s);
anchor = Anchor::Unc(server, share);
has_root_dir = true;
// Lexically normalize the remainder
let mut new_stack: Vec<std::ffi::OsString> = Vec::new();
for seg in parts {
match seg {
"." => {}
".." => {
let _ = new_stack.pop();
}
_ => new_stack.push(std::ffi::OsString::from(seg)),
}
}
stack = new_stack;
}
}
}
// Rebuild path using Anchor where possible (UNC/Drive), falling back to original prefix for DeviceNS
let mut out = PathBuf::new();
match &anchor {
Anchor::Unc(server, share) => {
// Build non-verbatim UNC: \\server\share
let base = PathBuf::from(format!(
r"\\{}\{}",
server.to_string_lossy(),
share.to_string_lossy()
));
out.push(base);
if has_root_dir {
out.push(Component::RootDir.as_os_str());
}
}
Anchor::Drive(drive) => {
out.push(drive);
if has_root_dir {
out.push(Component::RootDir.as_os_str());
}
}
Anchor::DeviceNS => {
if let Some(p) = &prefix_os {
out.push(p);
}
// No RootDir for DeviceNS
}
Anchor::None => {
if let Some(p) = &prefix_os {
out.push(p);
}
if has_root_dir {
out.push(Component::RootDir.as_os_str());
}
}
}
// If we have a Drive or UNC anchor, return an extended-length path now
match anchor {
Anchor::Unc(ref server, ref share) => {
let mut ext = PathBuf::from(r"\\?\UNC");
ext.push(server);
ext.push(share);
for seg in stack {
ext.push(seg);
}
return ext;
}
Anchor::Drive(ref drive) => {
let mut ext = PathBuf::from(r"\\?\");
ext.push(drive);
if has_root_dir {
ext.push(Component::RootDir.as_os_str());
}
for seg in stack {
ext.push(seg);
}
return ext;
}
_ => {}
}
for seg in stack {
out.push(seg);
}
out
}
#[cfg(not(windows))]
{
let mut result = PathBuf::new();
for component in path.components() {
match component {
std::path::Component::Prefix(_) | std::path::Component::RootDir => {
result.push(component.as_os_str());
}
std::path::Component::Normal(name) => {
result.push(name);
}
std::path::Component::ParentDir => {
// Pop only if there is a parent (stay at root otherwise)
let _ = result.pop();
}
std::path::Component::CurDir => {
// Skip
}
}
}
result
}
}