Skip to content

Commit

Permalink
WIP: Windows
Browse files Browse the repository at this point in the history
  • Loading branch information
cberner committed Sep 28, 2024
1 parent ef273e7 commit ed5f0f3
Show file tree
Hide file tree
Showing 2 changed files with 61 additions and 5 deletions.
4 changes: 4 additions & 0 deletions library/std/src/sys/pal/windows/c/windows_sys.rs
Original file line number Diff line number Diff line change
Expand Up @@ -65,6 +65,7 @@ windows_targets::link!("kernel32.dll" "system" fn InitOnceBeginInitialize(lpinit
windows_targets::link!("kernel32.dll" "system" fn InitOnceComplete(lpinitonce : *mut INIT_ONCE, dwflags : u32, lpcontext : *const core::ffi::c_void) -> BOOL);
windows_targets::link!("kernel32.dll" "system" fn InitializeProcThreadAttributeList(lpattributelist : LPPROC_THREAD_ATTRIBUTE_LIST, dwattributecount : u32, dwflags : u32, lpsize : *mut usize) -> BOOL);
windows_targets::link!("kernel32.dll" "system" fn LocalFree(hmem : HLOCAL) -> HLOCAL);
windows_targets::link!("kernel32.dll" "system" fn LockFileEx(hfile : HANDLE, dwflags : u32, dwreserved : u32, nnumberofbytestolocklow : u32, nnumberofbytestolockhigh : u32, lpoverlapped : *mut OVERLAPPED) -> BOOL);
windows_targets::link!("kernel32.dll" "system" fn MoveFileExW(lpexistingfilename : PCWSTR, lpnewfilename : PCWSTR, dwflags : MOVE_FILE_FLAGS) -> BOOL);
windows_targets::link!("kernel32.dll" "system" fn MultiByteToWideChar(codepage : u32, dwflags : MULTI_BYTE_TO_WIDE_CHAR_FLAGS, lpmultibytestr : PCSTR, cbmultibyte : i32, lpwidecharstr : PWSTR, cchwidechar : i32) -> i32);
windows_targets::link!("kernel32.dll" "system" fn QueryPerformanceCounter(lpperformancecount : *mut i64) -> BOOL);
Expand Down Expand Up @@ -96,6 +97,7 @@ windows_targets::link!("kernel32.dll" "system" fn TlsGetValue(dwtlsindex : u32)
windows_targets::link!("kernel32.dll" "system" fn TlsSetValue(dwtlsindex : u32, lptlsvalue : *const core::ffi::c_void) -> BOOL);
windows_targets::link!("kernel32.dll" "system" fn TryAcquireSRWLockExclusive(srwlock : *mut SRWLOCK) -> BOOLEAN);
windows_targets::link!("kernel32.dll" "system" fn TryAcquireSRWLockShared(srwlock : *mut SRWLOCK) -> BOOLEAN);
windows_targets::link!("kernel32.dll" "system" fn UnlockFile(hfile : HANDLE, dwfileoffsetlow: u32, dwfileoffsethigh : u32, nnumberofbytestolocklow : u32, nnumberofbytestolockhigh : u32) -> BOOL);
windows_targets::link!("kernel32.dll" "system" fn UpdateProcThreadAttribute(lpattributelist : LPPROC_THREAD_ATTRIBUTE_LIST, dwflags : u32, attribute : usize, lpvalue : *const core::ffi::c_void, cbsize : usize, lppreviousvalue : *mut core::ffi::c_void, lpreturnsize : *const usize) -> BOOL);
windows_targets::link!("kernel32.dll" "system" fn WaitForMultipleObjects(ncount : u32, lphandles : *const HANDLE, bwaitall : BOOL, dwmilliseconds : u32) -> WAIT_EVENT);
windows_targets::link!("kernel32.dll" "system" fn WaitForSingleObject(hhandle : HANDLE, dwmilliseconds : u32) -> WAIT_EVENT);
Expand Down Expand Up @@ -2763,6 +2765,8 @@ pub struct M128A {
pub Low: u64,
pub High: i64,
}
pub const LOCKFILE_FAIL_IMMEDIATELY: u32 = 1u32;
pub const LOCKFILE_EXCLUSIVE_LOCK: u32 = 2u32;
pub const MAXIMUM_REPARSE_DATA_BUFFER_SIZE: u32 = 16384u32;
pub const MAX_PATH: u32 = 260u32;
pub const MB_COMPOSITE: MULTI_BYTE_TO_WIDE_CHAR_FLAGS = 2u32;
Expand Down
62 changes: 57 additions & 5 deletions library/std/src/sys/pal/windows/fs.rs
Original file line number Diff line number Diff line change
Expand Up @@ -349,23 +349,75 @@ impl File {
}

pub fn lock(&self) -> io::Result<()> {
unsupported()
cvt(unsafe {
let mut overlapped = mem::zeroed();
c::LockFileEx(
self.handle.as_raw_handle(),
c::LOCKFILE_EXCLUSIVE_LOCK,
0,
u32::MAX,
u32::MAX,
&mut overlapped,
)
})?;
Ok(())
}

pub fn lock_shared(&self) -> io::Result<()> {
unsupported()
cvt(unsafe {
let mut overlapped = mem::zeroed();
c::LockFileEx(self.handle.as_raw_handle(), 0, 0, u32::MAX, u32::MAX, &mut overlapped)
})?;
Ok(())
}

pub fn try_lock(&self) -> io::Result<bool> {
unsupported()
let result = cvt(unsafe {
let mut overlapped = mem::zeroed();
c::LockFileEx(
self.handle.as_raw_handle(),
c::LOCKFILE_EXCLUSIVE_LOCK | c::LOCKFILE_FAIL_IMMEDIATELY,
0,
u32::MAX,
u32::MAX,
&mut overlapped,
)
});

if let Err(ref err) = result {
if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32) {
return Ok(false);
}
}
result?;
Ok(true)
}

pub fn try_lock_shared(&self) -> io::Result<bool> {
unsupported()
let result = cvt(unsafe {
let mut overlapped = mem::zeroed();
c::LockFileEx(
self.handle.as_raw_handle(),
c::LOCKFILE_FAIL_IMMEDIATELY,
0,
u32::MAX,
u32::MAX,
&mut overlapped,
)
});

if let Err(ref err) = result {
if err.raw_os_error() == Some(c::ERROR_IO_PENDING as i32) {
return Ok(false);
}
}
result?;
Ok(true)
}

pub fn unlock(&self) -> io::Result<()> {
unsupported()
cvt(unsafe { c::UnlockFile(self.handle.as_raw_handle(), 0, 0, u32::MAX, u32::MAX) })?;
Ok(())
}

pub fn truncate(&self, size: u64) -> io::Result<()> {
Expand Down

0 comments on commit ed5f0f3

Please sign in to comment.