Skip to content

Commit

Permalink
Correctly handle root path on windows
Browse files Browse the repository at this point in the history
  • Loading branch information
GuillaumeGomez committed Nov 14, 2023
1 parent a1c283d commit ac3a042
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 6 deletions.
20 changes: 17 additions & 3 deletions src/windows/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -394,6 +394,7 @@ impl ProcessInner {
written_bytes: 0,
};
get_process_params(&mut p, refresh_kind);
// Should always be called after we refreshed `cwd`.
update_root(&mut p, refresh_kind);
Some(p)
}
Expand Down Expand Up @@ -443,6 +444,7 @@ impl ProcessInner {
};

get_process_params(&mut p, refresh_kind);
// Should always be called after we refreshed `cwd`.
update_root(&mut p, refresh_kind);
p
}
Expand Down Expand Up @@ -476,6 +478,7 @@ impl ProcessInner {
read_bytes: 0,
written_bytes: 0,
};
// Should always be called after we refreshed `cwd`.
update_root(&mut p, refresh_kind);
p
}
Expand Down Expand Up @@ -508,6 +511,7 @@ impl ProcessInner {
self.exe = exe;
}
}
// Should always be called after we refreshed `cwd`.
update_root(self, refresh_kind);
self.run_time = now.saturating_sub(self.start_time());
self.updated = true;
Expand Down Expand Up @@ -658,10 +662,20 @@ unsafe fn get_process_times(handle: HANDLE) -> u64 {
super::utils::filetime_to_u64(fstart)
}

// On Windows, the root folder is always the current drive. So we get it from its `cwd`.
fn update_root(process: &mut ProcessInner, refresh_kind: ProcessRefreshKind) {
if refresh_kind.root() && process.exe.parent().is_some() {
process.root = process.exe.clone();
process.root.pop();
if refresh_kind.root() && process.cwd.parent().is_some() {
if !process.cwd.has_root() {
process.root = PathBuf::new();
return;
}
let mut ancestors = process.cwd.ancestors().peekable();
while let Some(path) = ancestors.next() {
if ancestors.peek().is_none() {
process.root = path.into();
break;
}
}
}
}

Expand Down
4 changes: 1 addition & 3 deletions tests/process.rs
Original file line number Diff line number Diff line change
Expand Up @@ -725,14 +725,12 @@ fn test_process_specific_refresh() {
update_specific_and_check!(memory);
update_specific_and_check!(environ, with_environ, .len(), 0);
update_specific_and_check!(cmd, with_cmd, .len(), 0);
// if cfg!(not(target_os = "windows")) {
update_specific_and_check!(exe, with_exe, , Path::new(""));
// }
update_specific_and_check!(cwd, with_cwd, , Path::new(""));
if !cfg!(any(
target_os = "macos",
target_os = "ios",
feature = "apple-sandbox"
feature = "apple-sandbox",
)) {
update_specific_and_check!(root, with_root, , Path::new(""));
}
Expand Down

0 comments on commit ac3a042

Please sign in to comment.