Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions crates/host_env/src/ctypes.rs
Original file line number Diff line number Diff line change
Expand Up @@ -33,8 +33,7 @@ use libloading::Library;
use libloading::os::unix::Library as UnixLibrary;
#[cfg(any(unix, windows))]
use parking_lot::{Mutex, RwLock};
use rustpython_wtf8::Wtf8;
use rustpython_wtf8::Wtf8Buf;
use rustpython_wtf8::{Wtf8, Wtf8Buf};
#[cfg(any(unix, windows))]
use std::{collections::HashMap, ffi::OsStr, sync::OnceLock};
use widestring::WideCStr;
Expand Down Expand Up @@ -1091,10 +1090,15 @@ pub fn utf16z_bytes(s: &Wtf8) -> Vec<u8> {
.collect()
}

/// Return a NUL terminated copy of `bytes`.
///
/// The input may contain interior NULs.
pub fn null_terminated_bytes(bytes: &[u8]) -> Vec<u8> {
let mut buffer = bytes.to_vec();
buffer.push(0);
buffer
if bytes.last() == Some(&0) {
bytes.to_vec()
} else {
bytes.iter().copied().chain(Some(0)).collect()
}
}

pub fn decode_type_code(type_code: &str, bytes: &[u8]) -> DecodedValue {
Expand Down
29 changes: 11 additions & 18 deletions crates/host_env/src/fileutils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,10 +17,10 @@ pub fn fstat(fd: crate::crt_fd::Borrowed<'_>) -> std::io::Result<StatStruct> {
#[cfg(windows)]
pub mod windows {
use crate::crt_fd;
use crate::windows::ToWideString;
use libc::{S_IFCHR, S_IFDIR, S_IFMT};
use std::ffi::OsStr;
use std::os::windows::io::AsRawHandle;
use std::path::Path;
use std::sync::OnceLock;
use windows_sys::Win32::Foundation::{
ERROR_INVALID_HANDLE, ERROR_NOT_SUPPORTED, FILETIME, FreeLibrary, SetLastError,
Expand Down Expand Up @@ -67,21 +67,15 @@ pub mod windows {
impl StatStruct {
// update_st_mode_from_path in cpython
pub fn update_st_mode_from_path(&mut self, path: &OsStr, attr: u32) {
if attr & FILE_ATTRIBUTE_DIRECTORY == 0 {
let file_extension = path
.to_wide()
.split(|&c| c == '.' as u16)
.next_back()
.and_then(|s| String::from_utf16(s).ok());

if let Some(file_extension) = file_extension
&& (file_extension.eq_ignore_ascii_case("exe")
|| file_extension.eq_ignore_ascii_case("bat")
|| file_extension.eq_ignore_ascii_case("cmd")
|| file_extension.eq_ignore_ascii_case("com"))
{
self.st_mode |= 0o111;
}
if attr & FILE_ATTRIBUTE_DIRECTORY == 0
&& let Some(file_extension) =
Path::new(path).extension().and_then(|ext| ext.to_str())
&& (file_extension.eq_ignore_ascii_case("exe")
|| file_extension.eq_ignore_ascii_case("bat")
|| file_extension.eq_ignore_ascii_case("cmd")
|| file_extension.eq_ignore_ascii_case("com"))
{
self.st_mode |= 0o111;
}
}
}
Expand Down Expand Up @@ -288,7 +282,7 @@ pub mod windows {

// _Py_GetFileInformationByName in cpython
pub fn get_file_information_by_name(
file_name: &OsStr,
file_name: &widestring::WideCStr,
file_information_class: FILE_INFO_BY_NAME_CLASS,
) -> std::io::Result<FILE_STAT_BASIC_INFORMATION> {
static GET_FILE_INFORMATION_BY_NAME: OnceLock<
Expand Down Expand Up @@ -329,7 +323,6 @@ pub mod windows {
})
.ok_or_else(|| std::io::Error::from_raw_os_error(ERROR_NOT_SUPPORTED as _))?;

let file_name = file_name.to_wide_with_nul();
let file_info_buffer_size = core::mem::size_of::<FILE_STAT_BASIC_INFORMATION>() as u32;
let mut file_info_buffer = core::mem::MaybeUninit::<FILE_STAT_BASIC_INFORMATION>::uninit();
unsafe {
Expand Down
Loading
Loading