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

Function get_file_information_by_name

crates/common/src/fileutils.rs:292–353  ·  view source on GitHub ↗

_Py_GetFileInformationByName in cpython

(
        file_name: &OsStr,
        file_information_class: FILE_INFO_BY_NAME_CLASS,
    )

Source from the content-addressed store, hash-verified

290
291 // _Py_GetFileInformationByName in cpython
292 pub fn get_file_information_by_name(
293 file_name: &OsStr,
294 file_information_class: FILE_INFO_BY_NAME_CLASS,
295 ) -> std::io::Result<FILE_STAT_BASIC_INFORMATION> {
296 static GET_FILE_INFORMATION_BY_NAME: OnceLock<
297 Option<
298 unsafe extern "system" fn(
299 PCWSTR,
300 FILE_INFO_BY_NAME_CLASS,
301 *mut libc::c_void,
302 u32,
303 ) -> i32,
304 >,
305 > = OnceLock::new();
306
307 let GetFileInformationByName = GET_FILE_INFORMATION_BY_NAME
308 .get_or_init(|| {
309 let library_name =
310 OsString::from("api-ms-win-core-file-l2-1-4.dll").to_wide_with_nul();
311 let module = unsafe { LoadLibraryW(library_name.as_ptr()) };
312 if module.is_null() {
313 return None;
314 }
315 let name = CString::new("GetFileInformationByName").unwrap();
316 if let Some(proc) =
317 unsafe { GetProcAddress(module, name.as_bytes_with_nul().as_ptr()) }
318 {
319 Some(unsafe {
320 core::mem::transmute::<
321 unsafe extern "system" fn() -> isize,
322 unsafe extern "system" fn(
323 *const u16,
324 FILE_INFO_BY_NAME_CLASS,
325 *mut libc::c_void,
326 u32,
327 ) -> i32,
328 >(proc)
329 })
330 } else {
331 unsafe { FreeLibrary(module) };
332 None
333 }
334 })
335 .ok_or_else(|| std::io::Error::from_raw_os_error(ERROR_NOT_SUPPORTED as _))?;
336
337 let file_name = file_name.to_wide_with_nul();
338 let file_info_buffer_size = core::mem::size_of::<FILE_STAT_BASIC_INFORMATION>() as u32;
339 let mut file_info_buffer = core::mem::MaybeUninit::<FILE_STAT_BASIC_INFORMATION>::uninit();
340 unsafe {
341 if GetFileInformationByName(
342 file_name.as_ptr(),
343 file_information_class as _,
344 file_info_buffer.as_mut_ptr() as _,
345 file_info_buffer_size,
346 ) == 0
347 {
348 Err(std::io::Error::last_os_error())
349 } else {

Callers 3

win32_xstat_implFunction · 0.85
_test_file_type_by_nameFunction · 0.85

Calls 9

newFunction · 0.85
ok_or_elseMethod · 0.80
to_wide_with_nulMethod · 0.80
as_mut_ptrMethod · 0.80
SomeClass · 0.50
ErrClass · 0.50
get_or_initMethod · 0.45
as_ptrMethod · 0.45
unwrapMethod · 0.45

Tested by

no test coverage detected