Skip to content
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Next Next commit
nt._path_splitroot
  • Loading branch information
youknowone committed Aug 15, 2022
commit b39c03512e44669025753ae0ce712e010588776d
54 changes: 49 additions & 5 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

6 changes: 6 additions & 0 deletions vm/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,12 @@ winreg = "0.10.1"
schannel = "0.1.19"
widestring = "0.5.1"

[target.'cfg(windows)'.dependencies.windows]
version = "0.39"
features = [
"Win32_UI_Shell",
]

[target.'cfg(windows)'.dependencies.winapi]
version = "0.3.9"
features = [
Expand Down
43 changes: 42 additions & 1 deletion vm/src/stdlib/nt.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,7 +21,10 @@ pub(crate) mod module {
},
PyResult, TryFromObject, VirtualMachine,
};
use std::{env, fs, io};
use std::{
env, fs, io,
os::windows::ffi::{OsStrExt, OsStringExt},
};

use crate::builtins::PyDictRef;
#[cfg(target_env = "msvc")]
Expand Down Expand Up @@ -292,6 +295,44 @@ pub(crate) mod module {
path.mode.process_path(buffer.to_os_string(), vm)
}

#[pyfunction]
fn _path_splitroot(path: PyPathLike, vm: &VirtualMachine) -> PyResult<(String, String)> {
let orig: Vec<_> = path.path.into_os_string().encode_wide().collect();
if orig.is_empty() {
return Ok(("".to_owned(), "".to_owned()));
}
let backslashed: Vec<_> = orig
.iter()
.copied()
.map(|c| if c == b'/' as u16 { b'\\' as u16 } else { c })
.chain(std::iter::once(0)) // null-terminated
.collect();

fn from_utf16(wstr: &[u16], vm: &VirtualMachine) -> PyResult<String> {
String::from_utf16(wstr).map_err(|e| vm.new_unicode_decode_error(e.to_string()))
}

let wbuf = windows::core::PCWSTR::from_raw(backslashed.as_ptr());
let (root, path) = match unsafe { windows::Win32::UI::Shell::PathCchSkipRoot(wbuf) } {
Ok(end) => {
assert!(!end.is_null());
let len: usize = unsafe { end.as_ptr().offset_from(wbuf.as_ptr()) }
.try_into()
.expect("len must be non-negative");
assert!(
len < backslashed.len(), // backslashed is null-terminated
"path: {:?} {} < {}",
std::path::PathBuf::from(std::ffi::OsString::from_wide(&backslashed)),
len,
backslashed.len()
);
(from_utf16(&orig[..len], vm)?, from_utf16(&orig[len..], vm)?)
}
Err(_) => ("".to_owned(), from_utf16(&orig, vm)?),
};
Ok((root, path))
}

#[pyfunction]
fn _getdiskusage(path: PyPathLike, vm: &VirtualMachine) -> PyResult<(u64, u64)> {
use um::fileapi::GetDiskFreeSpaceExW;
Expand Down