Skip to content

Commit 15558e0

Browse files
committed
implement more nt
1 parent 82a6238 commit 15558e0

File tree

1 file changed

+102
-0
lines changed

1 file changed

+102
-0
lines changed

vm/src/stdlib/nt.rs

Lines changed: 102 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -424,6 +424,108 @@ pub(crate) mod module {
424424
Ok(vm.ctx.new_list(drives))
425425
}
426426

427+
#[pyfunction]
428+
fn listvolumes(vm: &VirtualMachine) -> PyResult<PyListRef> {
429+
let mut volumes = vec![];
430+
let find;
431+
let mut buffer = [0u16; 257];
432+
find = unsafe {
433+
FileSystem::FindFirstVolumeW(
434+
buffer.as_mut_ptr(),
435+
buffer.len() as _,
436+
)
437+
};
438+
if find == windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE {
439+
return Err(errno_err(vm));
440+
}
441+
let mut err = 0;
442+
while err == 0 {
443+
let s = unsafe { widestring::WideCString::from_ptr_str(buffer.as_mut_ptr()) };
444+
let s = s.to_string_lossy();
445+
if s.is_empty() {
446+
break;
447+
}
448+
volumes.push(s.to_string());
449+
let ret = unsafe {
450+
FileSystem::FindNextVolumeW(
451+
find,
452+
buffer.as_mut_ptr(),
453+
buffer.len() as _,
454+
)
455+
};
456+
if ret == 0 {
457+
err = std::io::Error::last_os_error().raw_os_error().unwrap_or(0);
458+
}
459+
}
460+
if find != windows_sys::Win32::Foundation::INVALID_HANDLE_VALUE {
461+
unsafe { FileSystem::FindVolumeClose(find) };
462+
}
463+
if err != 0 && err != windows_sys::Win32::Foundation::ERROR_NO_MORE_FILES as i32 {
464+
return Err(std::io::Error::from_raw_os_error(err).to_pyexception(vm));
465+
}
466+
let volumes: Vec<_> = volumes
467+
.into_iter()
468+
.map(|v| vm.new_pyobj(v))
469+
.collect();
470+
Ok(vm.ctx.new_list(volumes))
471+
}
472+
473+
// TOOD: these functions are not fully compataible with CPython
474+
// they exist for some compatibility
475+
#[pyfunction]
476+
fn _path_exists(path: OsPath, vm: &VirtualMachine) -> PyResult<bool> {
477+
let path = path.as_ref();
478+
if path.exists() {
479+
return Ok(true);
480+
}
481+
if path.is_dir() {
482+
return Ok(false);
483+
}
484+
let metadata = fs::metadata(path).map_err(|err| err.to_pyexception(vm))?;
485+
Ok(metadata.is_file())
486+
}
487+
488+
#[pyfunction]
489+
fn _path_isdir(path: OsPath, vm: &VirtualMachine) -> PyResult<bool> {
490+
let path = path.as_ref();
491+
if path.is_dir() {
492+
return Ok(true);
493+
}
494+
if path.exists() {
495+
return Ok(false);
496+
}
497+
let metadata = fs::metadata(path).map_err(|err| err.to_pyexception(vm))?;
498+
Ok(metadata.is_dir())
499+
}
500+
501+
#[pyfunction]
502+
fn _path_isfile(path: OsPath, vm: &VirtualMachine) -> PyResult<bool> {
503+
let path = path.as_ref();
504+
if path.is_file() {
505+
return Ok(true);
506+
}
507+
if path.exists() {
508+
return Ok(false);
509+
}
510+
let metadata = fs::metadata(path).map_err(|err| err.to_pyexception(vm))?;
511+
Ok(metadata.is_file())
512+
}
513+
514+
#[pyfunction]
515+
fn _path_islink(path: OsPath, vm: &VirtualMachine) -> PyResult<bool> {
516+
let path = path.as_ref();
517+
if path.is_symlink() {
518+
return Ok(true);
519+
}
520+
if path.exists() {
521+
return Ok(false);
522+
}
523+
let metadata = fs::symlink_metadata(path).map_err(|err| err.to_pyexception(vm))?;
524+
Ok(metadata.file_type().is_symlink())
525+
}
526+
527+
// End of functions that are not fully compatible with CPython
528+
427529
#[pyfunction]
428530
fn set_handle_inheritable(
429531
handle: intptr_t,

0 commit comments

Comments
 (0)