Skip to content

Commit c52ad2f

Browse files
committed
implement more nt
1 parent cd1c9be commit c52ad2f

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

vm/src/stdlib/nt.rs

+102
Original file line numberDiff line numberDiff line change
@@ -445,6 +445,108 @@ 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 find;
452+
let mut buffer = [0u16; 257];
453+
find = unsafe {
454+
FileSystem::FindFirstVolumeW(
455+
buffer.as_mut_ptr(),
456+
buffer.len() as _,
457+
)
458+
};
459+
if find == windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE {
460+
return Err(errno_err(vm));
461+
}
462+
let mut err = 0;
463+
while err == 0 {
464+
let s = unsafe { widestring::WideCString::from_ptr_str(buffer.as_mut_ptr()) };
465+
let s = s.to_string_lossy();
466+
if s.is_empty() {
467+
break;
468+
}
469+
volumes.push(s.to_string());
470+
let ret = unsafe {
471+
FileSystem::FindNextVolumeW(
472+
find,
473+
buffer.as_mut_ptr(),
474+
buffer.len() as _,
475+
)
476+
};
477+
if ret == 0 {
478+
err = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
479+
}
480+
}
481+
if find != windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE {
482+
unsafe { FileSystem::FindVolumeClose(find) };
483+
}
484+
if err != 0 && err != windows_sys::Win32::Foundation::ERROR_NO_MORE_FILES as i32 {
485+
return Err(std::io::Error::from_raw_os_error(err).to_pyexception(vm));
486+
}
487+
let volumes: Vec<_> = volumes
488+
.into_iter()
489+
.map(|v| vm.new_pyobj(v))
490+
.collect();
491+
Ok(vm.ctx.new_list(volumes))
492+
}
493+
494+
// TOOD: these functions are not fully compataible with CPython
495+
// they exist for some compatibility
496+
#[pyfunction]
497+
fn _path_exists(path: OsPath, vm: &VirtualMachine) -> PyResult<bool> {
498+
let path = path.as_ref();
499+
if path.exists() {
500+
return Ok(true);
501+
}
502+
if path.is_dir() {
503+
return Ok(false);
504+
}
505+
let metadata = fs::metadata(path).map_err(|err| err.to_pyexception(vm))?;
506+
Ok(metadata.is_file())
507+
}
508+
509+
#[pyfunction]
510+
fn _path_isdir(path: OsPath, vm: &VirtualMachine) -> PyResult<bool> {
511+
let path = path.as_ref();
512+
if path.is_dir() {
513+
return Ok(true);
514+
}
515+
if path.exists() {
516+
return Ok(false);
517+
}
518+
let metadata = fs::metadata(path).map_err(|err| err.to_pyexception(vm))?;
519+
Ok(metadata.is_dir())
520+
}
521+
522+
#[pyfunction]
523+
fn _path_isfile(path: OsPath, vm: &VirtualMachine) -> PyResult<bool> {
524+
let path = path.as_ref();
525+
if path.is_file() {
526+
return Ok(true);
527+
}
528+
if path.exists() {
529+
return Ok(false);
530+
}
531+
let metadata = fs::metadata(path).map_err(|err| err.to_pyexception(vm))?;
532+
Ok(metadata.is_file())
533+
}
534+
535+
#[pyfunction]
536+
fn _path_islink(path: OsPath, vm: &VirtualMachine) -> PyResult<bool> {
537+
let path = path.as_ref();
538+
if path.is_symlink() {
539+
return Ok(true);
540+
}
541+
if path.exists() {
542+
return Ok(false);
543+
}
544+
let metadata = fs::symlink_metadata(path).map_err(|err| err.to_pyexception(vm))?;
545+
Ok(metadata.file_type().is_symlink())
546+
}
547+
548+
// End of functions that are not fully compatible with CPython
549+
448550
#[pyfunction]
449551
fn set_handle_inheritable(
450552
handle: intptr_t,

0 commit comments

Comments
 (0)