Skip to content

Commit b8bf65d

Browse files
committed
os.listdrives
1 parent 075c69a commit b8bf65d

1 file changed

Lines changed: 20 additions & 0 deletions

File tree

‎vm/src/stdlib/nt.rs‎

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -389,6 +389,26 @@ pub(crate) mod module {
389389
}
390390
}
391391

392+
#[pyfunction]
393+
fn listdrives(vm: &VirtualMachine) -> PyResult<PyListRef> {
394+
use windows_sys::Win32::Foundation::ERROR_MORE_DATA;
395+
396+
let mut buffer = [0u16; 256];
397+
let len =
398+
unsafe { FileSystem::GetLogicalDriveStringsW(buffer.len() as _, buffer.as_mut_ptr()) };
399+
if len == 0 {
400+
return Err(errno_err(vm));
401+
}
402+
if len as usize >= buffer.len() {
403+
return Err(std::io::Error::from_raw_os_error(ERROR_MORE_DATA as _).to_pyexception(vm));
404+
}
405+
let drives: Vec<_> = buffer[..(len - 1) as usize]
406+
.split(|&c| c == 0)
407+
.map(|drive| vm.new_pyobj(String::from_utf16_lossy(drive)))
408+
.collect();
409+
Ok(vm.ctx.new_list(drives))
410+
}
411+
392412
#[pyfunction]
393413
fn set_handle_inheritable(
394414
handle: intptr_t,

0 commit comments

Comments
 (0)