First implementation of IO rewrite. (#31)

This commit is contained in:
Darío 2024-12-14 05:59:06 -03:00 committed by Skyth
parent 2c405f5d2c
commit 26c4ab2eac

View file

@ -6,9 +6,46 @@
#include <cpu/guest_thread.h> #include <cpu/guest_thread.h>
#include <os/logger.h> #include <os/logger.h>
static constexpr uint32_t GUEST_INVALID_HANDLE_VALUE = 0xFFFFFFFF;
struct FileHandle
{
std::fstream stream;
std::filesystem::path path;
};
struct FindHandle
{
std::filesystem::path searchPath;
std::filesystem::directory_iterator iterator;
void fillFindData(LPWIN32_FIND_DATAA lpFindFileData)
{
if (iterator->is_directory())
lpFindFileData->dwFileAttributes = ByteSwap(FILE_ATTRIBUTE_DIRECTORY);
else if (iterator->is_regular_file())
lpFindFileData->dwFileAttributes = ByteSwap(FILE_ATTRIBUTE_NORMAL);
std::u8string pathU8Str = std::filesystem::relative(iterator->path(), searchPath).u8string();
uint64_t fileSize = iterator->file_size();
strncpy(lpFindFileData->cFileName, (const char *)(pathU8Str.c_str()), sizeof(lpFindFileData->cFileName));
lpFindFileData->nFileSizeLow = ByteSwap(uint32_t(fileSize >> 32U));
lpFindFileData->nFileSizeHigh = ByteSwap(uint32_t(fileSize));
lpFindFileData->ftCreationTime = {};
lpFindFileData->ftLastAccessTime = {};
lpFindFileData->ftLastWriteTime = {};
}
};
bool FileHandleCloser(void* handle)
{
delete (FileHandle *)(handle);
return false;
}
bool FindHandleCloser(void* handle) bool FindHandleCloser(void* handle)
{ {
FindClose(handle); delete (FindHandle *)(handle);
return false; return false;
} }
@ -22,41 +59,78 @@ SWA_API uint32_t XCreateFileA
DWORD dwFlagsAndAttributes DWORD dwFlagsAndAttributes
) )
{ {
const auto handle = (uint32_t)CreateFileA( assert(((dwDesiredAccess & ~(GENERIC_READ | GENERIC_WRITE | FILE_READ_DATA)) == 0) && "Unknown desired access bits.");
FileSystem::TransformPath(lpFileName), assert(((dwShareMode & ~(FILE_SHARE_READ | FILE_SHARE_WRITE)) == 0) && "Unknown share mode bits.");
dwDesiredAccess, assert(((dwCreationDisposition & ~(CREATE_NEW | CREATE_ALWAYS)) == 0) && "Unknown creation disposition bits.");
dwShareMode,
nullptr,
dwCreationDisposition,
dwFlagsAndAttributes & ~(FILE_FLAG_NO_BUFFERING | FILE_FLAG_OVERLAPPED),
nullptr);
GuestThread::SetLastError(GetLastError()); std::filesystem::path filePath = FileSystem::TransformPath(lpFileName);
std::fstream fileStream;
std::ios::openmode fileOpenMode = std::ios::binary;
if (dwDesiredAccess & (GENERIC_READ | FILE_READ_DATA))
{
fileOpenMode |= std::ios::in;
}
LOGF_UTILITY("\"{}\", 0x{:X}, 0x{:X}, 0x{:X}, 0x{:X}, 0x{:X} -> 0x{:X}", if (dwDesiredAccess & GENERIC_WRITE)
lpFileName, dwDesiredAccess, dwShareMode, (intptr_t)lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, handle); {
fileOpenMode |= std::ios::out;
}
return handle; fileStream.open(filePath, fileOpenMode);
if (!fileStream.is_open())
{
#ifdef _WIN32
GuestThread::SetLastError(GetLastError());
#endif
return GUEST_INVALID_HANDLE_VALUE;
}
FileHandle *fileHandle = new FileHandle();
fileHandle->stream = std::move(fileStream);
fileHandle->path = std::move(filePath);
return GUEST_HANDLE(ObInsertObject(fileHandle, FileHandleCloser));
} }
static DWORD XGetFileSizeA(uint32_t hFile, LPDWORD lpFileSizeHigh) static DWORD XGetFileSizeA(uint32_t hFile, LPDWORD lpFileSizeHigh)
{ {
DWORD fileSize = GetFileSize((HANDLE)hFile, lpFileSizeHigh); FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile)));
if (handle != nullptr)
{
std::error_code ec;
auto fileSize = std::filesystem::file_size(handle->path, ec);
if (!ec)
{
if (lpFileSizeHigh != nullptr)
{
*lpFileSizeHigh = ByteSwap(DWORD(fileSize >> 32U));
}
if (lpFileSizeHigh != nullptr) return (DWORD)(fileSize);
*lpFileSizeHigh = ByteSwap(*lpFileSizeHigh); }
}
return fileSize; return INVALID_FILE_SIZE;
} }
BOOL XGetFileSizeExA(uint32_t hFile, PLARGE_INTEGER lpFileSize) BOOL XGetFileSizeExA(uint32_t hFile, PLARGE_INTEGER lpFileSize)
{ {
BOOL result = GetFileSizeEx((HANDLE)hFile, lpFileSize); FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile)));
if (handle != nullptr)
{
std::error_code ec;
auto fileSize = std::filesystem::file_size(handle->path, ec);
if (!ec)
{
if (lpFileSize != nullptr)
{
lpFileSize->QuadPart = ByteSwap(fileSize);
}
if (result) return 1;
lpFileSize->QuadPart = ByteSwap(lpFileSize->QuadPart); }
}
return result; return 0;
} }
BOOL XReadFile BOOL XReadFile
@ -68,16 +142,31 @@ BOOL XReadFile
XOVERLAPPED* lpOverlapped XOVERLAPPED* lpOverlapped
) )
{ {
FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile)));
if (handle == nullptr)
{
return FALSE;
}
BOOL result = FALSE;
if (lpOverlapped != nullptr) if (lpOverlapped != nullptr)
{ {
LONG distanceToMoveHigh = lpOverlapped->OffsetHigh; std::streamoff streamOffset = lpOverlapped->Offset + (std::streamoff(lpOverlapped->OffsetHigh.get()) << 32U);
handle->stream.clear();
if (SetFilePointer((HANDLE)hFile, lpOverlapped->Offset, &distanceToMoveHigh, FILE_BEGIN) == INVALID_SET_FILE_POINTER) handle->stream.seekg(streamOffset, std::ios::beg);
if (handle->stream.bad())
{
return FALSE; return FALSE;
}
} }
DWORD numberOfBytesRead; DWORD numberOfBytesRead;
BOOL result = ReadFile((HANDLE)hFile, lpBuffer, nNumberOfBytesToRead, &numberOfBytesRead, nullptr); handle->stream.read((char *)(lpBuffer), nNumberOfBytesToRead);
if (!handle->stream.bad())
{
numberOfBytesRead = DWORD(handle->stream.gcount());
result = TRUE;
}
if (result) if (result)
{ {
@ -95,78 +184,165 @@ BOOL XReadFile
} }
} }
// printf("ReadFile(): %x %x %x %x %x %x\n", hFile, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped, result);
return result; return result;
} }
DWORD XSetFilePointer(uint32_t hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod) DWORD XSetFilePointer(uint32_t hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod)
{ {
FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile)));
if (handle == nullptr)
{
return INVALID_SET_FILE_POINTER;
}
LONG distanceToMoveHigh = lpDistanceToMoveHigh ? ByteSwap(*lpDistanceToMoveHigh) : 0; LONG distanceToMoveHigh = lpDistanceToMoveHigh ? ByteSwap(*lpDistanceToMoveHigh) : 0;
DWORD result = SetFilePointer((HANDLE)hFile, lDistanceToMove, lpDistanceToMoveHigh ? &distanceToMoveHigh : nullptr, dwMoveMethod); std::streamoff streamOffset = lDistanceToMove + (std::streamoff(distanceToMoveHigh) << 32U);
std::fstream::seekdir streamSeekDir = {};
switch (dwMoveMethod)
{
case FILE_BEGIN:
streamSeekDir = std::ios::beg;
break;
case FILE_CURRENT:
streamSeekDir = std::ios::cur;
break;
case FILE_END:
streamSeekDir = std::ios::end;
break;
default:
assert(false && "Unknown move method.");
break;
}
handle->stream.clear();
handle->stream.seekg(streamOffset, streamSeekDir);
if (handle->stream.bad())
{
return INVALID_SET_FILE_POINTER;
}
std::streampos streamPos = handle->stream.tellg();
if (lpDistanceToMoveHigh != nullptr) if (lpDistanceToMoveHigh != nullptr)
*lpDistanceToMoveHigh = ByteSwap(distanceToMoveHigh); *lpDistanceToMoveHigh = ByteSwap(LONG(streamPos >> 32U));
return result; return DWORD(streamPos);
} }
BOOL XSetFilePointerEx(uint32_t hFile, LONG lDistanceToMove, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod) BOOL XSetFilePointerEx(uint32_t hFile, LONG lDistanceToMove, PLARGE_INTEGER lpNewFilePointer, DWORD dwMoveMethod)
{ {
LARGE_INTEGER distanceToMove; FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile)));
distanceToMove.QuadPart = lDistanceToMove; if (handle == nullptr)
{
return FALSE;
}
DWORD result = SetFilePointerEx((HANDLE)hFile, distanceToMove, lpNewFilePointer, dwMoveMethod); std::fstream::seekdir streamSeekDir = {};
switch (dwMoveMethod)
{
case FILE_BEGIN:
streamSeekDir = std::ios::beg;
break;
case FILE_CURRENT:
streamSeekDir = std::ios::cur;
break;
case FILE_END:
streamSeekDir = std::ios::end;
break;
default:
assert(false && "Unknown move method.");
break;
}
handle->stream.clear();
handle->stream.seekg(lDistanceToMove, streamSeekDir);
if (handle->stream.bad())
{
return FALSE;
}
if (lpNewFilePointer != nullptr) if (lpNewFilePointer != nullptr)
lpNewFilePointer->QuadPart = ByteSwap(lpNewFilePointer->QuadPart); {
lpNewFilePointer->QuadPart = ByteSwap(LONGLONG(handle->stream.tellg()));
}
return result; return TRUE;
} }
uint32_t XFindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData) uint32_t XFindFirstFileA(LPCSTR lpFileName, LPWIN32_FIND_DATAA lpFindFileData)
{ {
auto& data = *lpFindFileData; const char *transformedPath = FileSystem::TransformPath(lpFileName);
const auto handle = FindFirstFileA(FileSystem::TransformPath(lpFileName), &data); size_t transformedPathLength = strlen(transformedPath);
if (transformedPathLength == 0)
return GUEST_INVALID_HANDLE_VALUE;
GuestThread::SetLastError(GetLastError()); std::filesystem::path dirPath;
if (strstr(transformedPath, "\\*") == (&transformedPath[transformedPathLength - 2]))
{
dirPath = std::string(transformedPath, transformedPathLength - 2);
}
else if (strstr(transformedPath, "\\*.*") == (&transformedPath[transformedPathLength - 4]))
{
dirPath = std::string(transformedPath, transformedPathLength - 4);
}
else
{
dirPath = std::string(transformedPath, transformedPathLength);
assert(!dirPath.has_extension() && "Unknown search pattern.");
}
if (handle == INVALID_HANDLE_VALUE) if (!std::filesystem::is_directory(dirPath))
return 0xFFFFFFFF; return GUEST_INVALID_HANDLE_VALUE;
ByteSwapInplace(data.dwFileAttributes); std::filesystem::directory_iterator dirIterator(dirPath);
ByteSwapInplace(*(uint64_t*)&data.ftCreationTime); if (dirIterator == std::filesystem::directory_iterator())
ByteSwapInplace(*(uint64_t*)&data.ftLastAccessTime); return GUEST_INVALID_HANDLE_VALUE;
ByteSwapInplace(*(uint64_t*)&data.ftLastWriteTime);
ByteSwapInplace(*(uint64_t*)&data.nFileSizeHigh);
return GUEST_HANDLE(ObInsertObject(handle, FindHandleCloser)); FindHandle *findHandle = new FindHandle();
findHandle->searchPath = std::move(dirPath);
findHandle->iterator = std::move(dirIterator);
findHandle->fillFindData(lpFindFileData);
return GUEST_HANDLE(ObInsertObject(findHandle, FindHandleCloser));
} }
uint32_t XFindNextFileA(uint32_t Handle, LPWIN32_FIND_DATAA lpFindFileData) uint32_t XFindNextFileA(uint32_t Handle, LPWIN32_FIND_DATAA lpFindFileData)
{ {
auto* handle = ObQueryObject(HOST_HANDLE(Handle)); FindHandle *findHandle = (FindHandle *)(ObQueryObject(HOST_HANDLE(Handle)));
auto& data = *lpFindFileData; if (findHandle == nullptr)
const auto result = FindNextFileA(handle, &data); return FALSE;
ByteSwapInplace(data.dwFileAttributes); findHandle->iterator++;
ByteSwapInplace(*(uint64_t*)&data.ftCreationTime);
ByteSwapInplace(*(uint64_t*)&data.ftLastAccessTime);
ByteSwapInplace(*(uint64_t*)&data.ftLastWriteTime);
ByteSwapInplace(*(uint64_t*)&data.nFileSizeHigh);
return result; if (findHandle->iterator == std::filesystem::directory_iterator())
{
return FALSE;
}
else
{
findHandle->fillFindData(lpFindFileData);
return TRUE;
}
} }
BOOL XReadFileEx(uint32_t hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, XOVERLAPPED* lpOverlapped, uint32_t lpCompletionRoutine) BOOL XReadFileEx(uint32_t hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, XOVERLAPPED* lpOverlapped, uint32_t lpCompletionRoutine)
{ {
LONG distanceToMoveHigh = lpOverlapped->OffsetHigh; FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile)));
if (handle == nullptr)
if (SetFilePointer((HANDLE)hFile, lpOverlapped->Offset, &distanceToMoveHigh, FILE_BEGIN) == INVALID_SET_FILE_POINTER)
return FALSE; return FALSE;
BOOL result = FALSE;
DWORD numberOfBytesRead; DWORD numberOfBytesRead;
BOOL result = ReadFile((HANDLE)hFile, lpBuffer, nNumberOfBytesToRead, &numberOfBytesRead, nullptr); std::streamoff streamOffset = lpOverlapped->Offset + (std::streamoff(lpOverlapped->OffsetHigh.get()) << 32U);
handle->stream.clear();
handle->stream.seekg(streamOffset, std::ios::beg);
if (handle->stream.bad())
return FALSE;
handle->stream.read((char *)(lpBuffer), nNumberOfBytesToRead);
if (!handle->stream.bad())
{
numberOfBytesRead = DWORD(handle->stream.gcount());
result = TRUE;
}
if (result) if (result)
{ {
@ -177,26 +353,36 @@ BOOL XReadFileEx(uint32_t hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, XO
SetEvent((HANDLE)lpOverlapped->hEvent.get()); SetEvent((HANDLE)lpOverlapped->hEvent.get());
} }
// printf("ReadFileEx(): %x %x %x %x %x %x\n", hFile, lpBuffer, nNumberOfBytesToRead, lpOverlapped, lpCompletionRoutine, result);
return result; return result;
} }
DWORD XGetFileAttributesA(LPCSTR lpFileName) DWORD XGetFileAttributesA(LPCSTR lpFileName)
{ {
return GetFileAttributesA(FileSystem::TransformPath(lpFileName)); std::filesystem::path filePath = FileSystem::TransformPath(lpFileName);
if (std::filesystem::is_directory(filePath))
return FILE_ATTRIBUTE_DIRECTORY;
else if (std::filesystem::is_regular_file(filePath))
return FILE_ATTRIBUTE_NORMAL;
else
return INVALID_FILE_ATTRIBUTES;
} }
BOOL XWriteFile(uint32_t hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped) BOOL XWriteFile(uint32_t hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten, LPOVERLAPPED lpOverlapped)
{ {
assert(lpOverlapped == nullptr); assert(lpOverlapped == nullptr && "Overlapped not implemented.");
BOOL result = WriteFile((HANDLE)hFile, lpBuffer, nNumberOfBytesToWrite, lpNumberOfBytesWritten, nullptr); FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile)));
if (handle == nullptr)
return FALSE;
if (result && lpNumberOfBytesWritten != nullptr) handle->stream.write((const char *)(lpBuffer), nNumberOfBytesToWrite);
ByteSwapInplace(*lpNumberOfBytesWritten); if (handle->stream.bad())
return FALSE;
return result; if (lpNumberOfBytesWritten != nullptr)
*lpNumberOfBytesWritten = DWORD(handle->stream.gcount());
return TRUE;
} }
const char* FileSystem::TransformPath(const char* path) const char* FileSystem::TransformPath(const char* path)