Skip to content

Commit 4307205

Browse files
committed
implement nt.listvolumes
1 parent cd1c9be commit 4307205

File tree

1 file changed

+34
-0
lines changed

1 file changed

+34
-0
lines changed

vm/src/stdlib/nt.rs

Lines changed: 34 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,39 @@ pub(crate) mod module {
445445
Ok(vm.ctx.new_list(drives))
446446
}
447447

448+
#[pyfunction]
449+
fn listvolumes(vm: &VirtualMachine) -> PyResult<PyListRef> {
450+
let mut volumes = vec![];
451+
let mut buffer = [0u16; 257];
452+
let find = unsafe { FileSystem::FindFirstVolumeW(buffer.as_mut_ptr(), buffer.len() as _) };
453+
if find == windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE {
454+
return Err(errno_err(vm));
455+
}
456+
let mut err = 0;
457+
while err == 0 {
458+
let s = unsafe { widestring::WideCString::from_ptr_str(buffer.as_mut_ptr()) };
459+
let s = s.to_string_lossy();
460+
if s.is_empty() {
461+
break;
462+
}
463+
volumes.push(s.to_string());
464+
let ret = unsafe {
465+
FileSystem::FindNextVolumeW(find, buffer.as_mut_ptr(), buffer.len() as _)
466+
};
467+
if ret == 0 {
468+
err = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
469+
}
470+
}
471+
if find != windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE {
472+
unsafe { FileSystem::FindVolumeClose(find) };
473+
}
474+
if err != 0 && err != windows_sys::Win32::Foundation::ERROR_NO_MORE_FILES as i32 {
475+
return Err(std::io::Error::from_raw_os_error(err).to_pyexception(vm));
476+
}
477+
let volumes: Vec<_> = volumes.into_iter().map(|v| vm.new_pyobj(v)).collect();
478+
Ok(vm.ctx.new_list(volumes))
479+
}
480+
448481
#[pyfunction]
449482
fn set_handle_inheritable(
450483
handle: intptr_t,
@@ -476,3 +509,4 @@ pub(crate) mod module {
476509
Vec::new()
477510
}
478511
}
512+
}

0 commit comments

Comments
 (0)