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

Add overlapped (asynchronous file read/writes) to WinAPI #664

Merged
merged 2 commits into from
May 4, 2024
Merged
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
1 change: 1 addition & 0 deletions lib/winapi/Makefile
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ WINAPI_SRCS := \
$(NXDK_DIR)/lib/winapi/filemanip.c \
$(NXDK_DIR)/lib/winapi/findfile.c \
$(NXDK_DIR)/lib/winapi/handleapi.c \
$(NXDK_DIR)/lib/winapi/ioapi.c \
$(NXDK_DIR)/lib/winapi/memory.c \
$(NXDK_DIR)/lib/winapi/libloaderapi.c \
$(NXDK_DIR)/lib/winapi/profiling.c \
Expand Down
77 changes: 67 additions & 10 deletions lib/winapi/fileio.c
Original file line number Diff line number Diff line change
Expand Up @@ -103,12 +103,42 @@ BOOL ReadFile (HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWOR
NTSTATUS status;
IO_STATUS_BLOCK ioStatusBlock;

// overlapped I/O not supported yet
assert(!lpOverlapped);
if (lpOverlapped == NULL) {
// A null pointer makes the code crash on Windows, we'd rather catch it
assert(lpNumberOfBytesRead);
}

if (lpNumberOfBytesRead) {
*lpNumberOfBytesRead = 0;
}

if (lpOverlapped) {
LARGE_INTEGER overlappedOffset = {
.LowPart = lpOverlapped->Offset,
.HighPart = lpOverlapped->OffsetHigh,
};

// Internal contains the status code for the I/O request. Set to STATUS_PENDING at the start of the request.
lpOverlapped->Internal = (DWORD)STATUS_PENDING;
// InternalHigh contains the actual number of bytes transferred for the I/O request
lpOverlapped->InternalHigh = 0;

// A null pointer makes the code crash on Windows, we'd rather catch it
assert(lpNumberOfBytesRead);
*lpNumberOfBytesRead = 0;
status = NtReadFile(hFile, lpOverlapped->hEvent, NULL, NULL, (PIO_STATUS_BLOCK)&lpOverlapped->Internal,
lpBuffer, nNumberOfBytesToRead, &overlappedOffset);

// The read can finish immediately. Handle this case
if (NT_SUCCESS(status) && status != STATUS_PENDING) {
if (lpNumberOfBytesRead) {
*lpNumberOfBytesRead = (DWORD)lpOverlapped->InternalHigh;
}
return TRUE;
} else if (status == STATUS_END_OF_FILE) {
*lpNumberOfBytesRead = 0;
return TRUE;
}
thrimbor marked this conversation as resolved.
Show resolved Hide resolved
SetLastError(RtlNtStatusToDosError(status));
return FALSE;
}

status = NtReadFile(hFile, NULL, NULL, NULL, &ioStatusBlock, lpBuffer, nNumberOfBytesToRead, NULL);

Expand Down Expand Up @@ -139,12 +169,39 @@ BOOL WriteFile (HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPD
NTSTATUS status;
IO_STATUS_BLOCK ioStatusBlock;

// overlapped I/O not supported yet
assert(!lpOverlapped);
if (lpOverlapped == NULL) {
// A null pointer makes the code crash on Windows, we'd rather catch it
assert(lpNumberOfBytesWritten);
}

if (lpNumberOfBytesWritten) {
*lpNumberOfBytesWritten = 0;
}

if (lpOverlapped) {
LARGE_INTEGER overlappedOffset = {
.LowPart = lpOverlapped->Offset,
.HighPart = lpOverlapped->OffsetHigh,
};

// Internal contains the status code for the I/O request. Set to STATUS_PENDING at the start of the request
lpOverlapped->Internal = (DWORD)STATUS_PENDING;
// InternalHigh contains the actual number of bytes transferred for the I/O request
lpOverlapped->InternalHigh = 0;

status = NtWriteFile(hFile, lpOverlapped->hEvent, NULL, NULL, (PIO_STATUS_BLOCK)&lpOverlapped->Internal,
(PVOID)lpBuffer, nNumberOfBytesToWrite, &overlappedOffset);

// A null pointer makes the code crash on Windows, we'd rather catch it
assert(lpNumberOfBytesWritten);
*lpNumberOfBytesWritten = 0;
// The write can finish immediately. Handle this case
if (NT_SUCCESS(status) && status != STATUS_PENDING) {
if (lpNumberOfBytesWritten) {
*lpNumberOfBytesWritten = (DWORD)lpOverlapped->InternalHigh;
}
return TRUE;
}
SetLastError(RtlNtStatusToDosError(status));
return FALSE;
}

status = NtWriteFile(hFile, NULL, NULL, NULL, &ioStatusBlock, (PVOID)lpBuffer, nNumberOfBytesToWrite, NULL);

Expand Down
47 changes: 47 additions & 0 deletions lib/winapi/ioapi.c
Original file line number Diff line number Diff line change
@@ -0,0 +1,47 @@
// SPDX-License-Identifier: MIT

// SPDX-FileCopyrightText: 2023 Ryan Wendland

#include <synchapi.h>
#include <winbase.h>
#include <winerror.h>
#include <xboxkrnl/xboxkrnl.h>

BOOL GetOverlappedResult (HANDLE hFile, LPOVERLAPPED lpOverlapped, LPDWORD lpNumberOfBytesTransferred, BOOL bWait)
{
DWORD status;
HANDLE waitHandle;

status = (DWORD)lpOverlapped->Internal;

if (status == STATUS_PENDING && bWait == FALSE) {
SetLastError(ERROR_IO_INCOMPLETE);
return FALSE;
}

// If the hEvent member of the OVERLAPPED structure is NULL, the system
// uses the state of the hFile handle to signal when the operation has been completed
if (lpOverlapped->hEvent == NULL) {
waitHandle = hFile;
} else {
waitHandle = lpOverlapped->hEvent;
}

if (status == STATUS_PENDING) {
if (WaitForSingleObject(waitHandle, INFINITE) != WAIT_OBJECT_0) {
SetLastError(ERROR_IO_INCOMPLETE);
return FALSE;
thrimbor marked this conversation as resolved.
Show resolved Hide resolved
}
// Get final status of the transfer
status = (DWORD)lpOverlapped->Internal;
}

// InternalHigh contains the actual number of bytes transferred for the I/O request
*lpNumberOfBytesTransferred = (DWORD)lpOverlapped->InternalHigh;

if (!NT_SUCCESS(status)) {
SetLastError(RtlNtStatusToDosError(status));
return FALSE;
}
return TRUE;
}
2 changes: 2 additions & 0 deletions lib/winapi/winbase.h
Original file line number Diff line number Diff line change
Expand Up @@ -134,6 +134,8 @@ void WINAPI OutputDebugStringA (LPCTSTR lpOutputString);

BOOL IsBadWritePtr (LPVOID lp, UINT_PTR ucb);

BOOL GetOverlappedResult (HANDLE hFile, LPOVERLAPPED lpOverlapped, LPDWORD lpNumberOfBytesTransferred, BOOL bWait);

#ifndef UNICODE
#define OutputDebugString OutputDebugStringA
#else
Expand Down
Loading