Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Support getting file metadata on Windows #4067

Open
wants to merge 22 commits into
base: master
Choose a base branch
from
Open
Changes from 1 commit
Commits
Show all changes
22 commits
Select commit Hold shift + click to select a range
9b61c05
Refactor `Handle` slightly
CraftSpider Dec 2, 2024
ab0e138
Implement trivial file operations - opening and closing handles. Just…
CraftSpider Dec 2, 2024
e19deaa
Fix MAIN_THREAD handle in windows_join_main
CraftSpider Dec 2, 2024
f1bb77d
Try fix for Windows paths on non-Windows machines
CraftSpider Dec 2, 2024
9a76a85
Most review comments - still missing shim tests
CraftSpider Dec 6, 2024
cc6eae5
Don't leak miri implementation details
CraftSpider Dec 6, 2024
3653169
Fix clippy
CraftSpider Dec 6, 2024
a64dbd1
Test file creation and metadata shims directly
CraftSpider Dec 6, 2024
274d90f
Move windows-fs to pass-dep and use windows_sys
CraftSpider Dec 8, 2024
51273f5
Move FdNum to shims/files.rs, use it in FdTable definitions
CraftSpider Dec 8, 2024
aa6bbf6
Slightly improve flag handling - parse and validate in one place
CraftSpider Dec 8, 2024
938430f
Fixup imports, compile
CraftSpider Dec 8, 2024
3c2ed8a
Make metadata handle store the metadata, instead of just a path. Add …
CraftSpider Dec 8, 2024
ebfc768
Improve extract_windows_epoch impl
CraftSpider Dec 8, 2024
d989984
Improve extract_windows_epoch impl comments
CraftSpider Dec 8, 2024
e5ada76
Add invalid handle encoding test
CraftSpider Dec 8, 2024
52c1676
Add tests for CREATE_ALWAYS and OPEN_ALWAYS error behavior. Add comme…
CraftSpider Dec 8, 2024
5ac99da
Extract Windows epoch helpers from GetSystemTimeAsFileTime and use th…
CraftSpider Dec 8, 2024
ef5ab7f
Merge FileHandle implementation between Unix and Windows
CraftSpider Dec 9, 2024
6ee99aa
Use u32::MAX constant
CraftSpider Dec 13, 2024
a61daf2
Some fs improvements
CraftSpider Dec 16, 2024
92f41ce
Use FdNum more places
CraftSpider Dec 16, 2024
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
131 changes: 131 additions & 0 deletions tests/pass/shims/windows-fs.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,131 @@
//@only-target: windows # this directly tests windows-only functions
//@compile-flags: -Zmiri-disable-isolation
#![allow(nonstandard_style)]

use std::os::windows::ffi::OsStrExt;
use std::ptr;

#[path = "../../utils/mod.rs"]
mod utils;

// Windows API definitions.
type HANDLE = isize;
type BOOL = i32;
type DWORD = u32;
type LPCWSTR = *const u16;

const GENERIC_READ: u32 = 2147483648u32;
const GENERIC_WRITE: u32 = 1073741824u32;
pub const FILE_SHARE_NONE: u32 = 0u32;
pub const FILE_SHARE_READ: u32 = 1u32;
pub const FILE_SHARE_WRITE: u32 = 2u32;
pub const OPEN_ALWAYS: u32 = 4u32;
pub const OPEN_EXISTING: u32 = 3u32;
pub const CREATE_NEW: u32 = 1u32;
pub const FILE_ATTRIBUTE_DIRECTORY: u32 = 16u32;
pub const FILE_ATTRIBUTE_NORMAL: u32 = 128u32;
pub const FILE_FLAG_BACKUP_SEMANTICS: u32 = 0x02000000u32;

#[repr(C)]
struct FILETIME {
dwLowDateTime: DWORD,
dwHighDateTime: DWORD,
}

#[repr(C)]
struct BY_HANDLE_FILE_INFORMATION {
dwFileAttributes: DWORD,
ftCreationTime: FILETIME,
ftLastAccessTime: FILETIME,
ftLastWriteTime: FILETIME,
dwVolumeSerialNumber: DWORD,
nFileSizeHigh: DWORD,
nFileSizeLow: DWORD,
nNumberOfLinks: DWORD,
nFileIndexHigh: DWORD,
nFileIndexLow: DWORD,
}

#[repr(C)]
struct SECURITY_ATTRIBUTES {
nLength: DWORD,
lpSecurityDescriptor: *mut std::ffi::c_void,
bInheritHandle: BOOL,
}

extern "system" {
fn CreateFileW(
file_name: LPCWSTR,
dwDesiredAccess: DWORD,
dwShareMode: DWORD,
lpSecurityAttributes: *mut SECURITY_ATTRIBUTES,
dwCreationDisposition: DWORD,
dwFlagsAndAttributes: DWORD,
hTemplateFile: HANDLE,
) -> HANDLE;
fn GetFileInformationByHandle(
handle: HANDLE,
file_info: *mut BY_HANDLE_FILE_INFORMATION,
) -> BOOL;
fn CloseHandle(handle: HANDLE) -> BOOL;
fn GetLastError() -> DWORD;
}
CraftSpider marked this conversation as resolved.
Show resolved Hide resolved

fn main() {
unsafe {
test_create_dir_file();
test_create_normal_file();
}
}

unsafe fn test_create_dir_file() {
let temp = utils::tmp();
let mut raw_path = temp.as_os_str().encode_wide().collect::<Vec<_>>();
// encode_wide doesn't add a null-terminator
raw_path.push(0);
raw_path.push(0);
CraftSpider marked this conversation as resolved.
Show resolved Hide resolved
let handle = CreateFileW(
raw_path.as_ptr(),
GENERIC_READ,
FILE_SHARE_READ | FILE_SHARE_WRITE,
ptr::null_mut(),
OPEN_EXISTING,
FILE_FLAG_BACKUP_SEMANTICS,
0,
);
assert_ne!(handle, -1, "CreateNewW Failed: {}", GetLastError());
let mut info = std::mem::zeroed::<BY_HANDLE_FILE_INFORMATION>();
if GetFileInformationByHandle(handle, &mut info) == 0 {
panic!("Failed to get file information")
};
assert!(info.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY != 0);
if CloseHandle(handle) == 0 {
panic!("Failed to close file")
};
}

unsafe fn test_create_normal_file() {
let temp = utils::tmp().join("test.txt");
let mut raw_path = temp.as_os_str().encode_wide().collect::<Vec<_>>();
// encode_wide doesn't add a null-terminator
raw_path.push(0);
raw_path.push(0);
let handle = CreateFileW(
raw_path.as_ptr(),
GENERIC_READ | GENERIC_WRITE,
FILE_SHARE_READ | FILE_SHARE_WRITE,
ptr::null_mut(),
CREATE_NEW,
0,
0,
);
assert_ne!(handle, -1, "CreateNewW Failed: {}", GetLastError());
let mut info = std::mem::zeroed::<BY_HANDLE_FILE_INFORMATION>();
if GetFileInformationByHandle(handle, &mut info) == 0 {
panic!("Failed to get file information: {}", GetLastError())
};
assert!(info.dwFileAttributes & FILE_ATTRIBUTE_NORMAL != 0);
if CloseHandle(handle) == 0 {
panic!("Failed to close file")
};
}
Loading