MCPcopy Index your code
hub / github.com/RustPython/RustPython / _findfirstfile

Function _findfirstfile

crates/vm/src/stdlib/nt.rs:413–444  ·  view source on GitHub ↗
(path: OsPath, vm: &VirtualMachine)

Source from the content-addressed store, hash-verified

411 /// Uses FindFirstFileW to get the name as stored on the filesystem.
412 #[pyfunction]
413 fn _findfirstfile(path: OsPath, vm: &VirtualMachine) -> PyResult<PyStrRef> {
414 use crate::common::windows::ToWideString;
415 use std::os::windows::ffi::OsStringExt;
416 use windows_sys::Win32::Storage::FileSystem::{
417 FindClose, FindFirstFileW, WIN32_FIND_DATAW,
418 };
419
420 let wide_path = path.as_ref().to_wide_with_nul();
421 let mut find_data: WIN32_FIND_DATAW = unsafe { core::mem::zeroed() };
422
423 let handle = unsafe { FindFirstFileW(wide_path.as_ptr(), &mut find_data) };
424 if handle == INVALID_HANDLE_VALUE {
425 let err = io::Error::last_os_error();
426 return Err(OSErrorBuilder::with_filename(&err, path, vm));
427 }
428
429 unsafe { FindClose(handle) };
430
431 // Convert the filename from the find data to a Rust string
432 // cFileName is a null-terminated wide string
433 let len = find_data
434 .cFileName
435 .iter()
436 .position(|&c| c == 0)
437 .unwrap_or(find_data.cFileName.len());
438 let filename = std::ffi::OsString::from_wide(&find_data.cFileName[..len]);
439 let filename_str = filename
440 .to_str()
441 .ok_or_else(|| vm.new_unicode_decode_error("filename contains invalid UTF-8"))?;
442
443 Ok(vm.ctx.new_str(filename_str).to_owned())
444 }
445
446 #[derive(FromArgs)]
447 struct PathArg {

Callers 1

Calls 11

to_wide_with_nulMethod · 0.80
ok_or_elseMethod · 0.80
to_strMethod · 0.80
ErrClass · 0.50
as_refMethod · 0.45
as_ptrMethod · 0.45
positionMethod · 0.45
iterMethod · 0.45
lenMethod · 0.45
to_ownedMethod · 0.45
new_strMethod · 0.45

Tested by

no test coverage detected