(path: OsPath, vm: &VirtualMachine)
| 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 { |
no test coverage detected