First implementation of IO rewrite.

This commit is contained in:
Dario 2024-12-13 14:52:57 -03:00
parent 8531582dc4
commit 586e17d8a4

View file

@ -5,9 +5,46 @@
#include <kernel/function.h> #include <kernel/function.h>
#include <cpu/guest_thread.h> #include <cpu/guest_thread.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 = std::byteswap(FILE_ATTRIBUTE_DIRECTORY);
else if (iterator->is_regular_file())
lpFindFileData->dwFileAttributes = std::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 = std::byteswap(uint32_t(fileSize >> 32U));
lpFindFileData->nFileSizeHigh = std::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;
} }
@ -19,41 +56,82 @@ SWA_API uint32_t XCreateFileA(
DWORD dwCreationDisposition, DWORD dwCreationDisposition,
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);
printf("CreateFileA(%s, %lx, %lx, %p, %lx, %lx): %x\n", lpFileName, dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition, dwFlagsAndAttributes, handle); std::fstream fileStream;
std::ios::openmode fileOpenMode = std::ios::binary;
if (dwDesiredAccess & (GENERIC_READ | FILE_READ_DATA))
{
fileOpenMode |= std::ios::in;
}
return handle; if (dwDesiredAccess & GENERIC_WRITE)
{
fileOpenMode |= std::ios::out;
}
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( static DWORD XGetFileSizeA(
uint32_t hFile, uint32_t hFile,
LPDWORD lpFileSizeHigh) LPDWORD lpFileSizeHigh)
{ {
DWORD fileSize = GetFileSize((HANDLE)hFile, lpFileSizeHigh); FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile)));
if (lpFileSizeHigh != nullptr) if (handle != nullptr)
*lpFileSizeHigh = std::byteswap(*lpFileSizeHigh); {
std::error_code ec;
auto fileSize = std::filesystem::file_size(handle->path, ec);
if (!ec)
{
if (lpFileSizeHigh != nullptr)
{
*lpFileSizeHigh = std::byteswap(DWORD(fileSize >> 32U));
}
return fileSize; return (DWORD)(fileSize);
}
}
return INVALID_FILE_SIZE;
} }
BOOL XGetFileSizeExA( BOOL XGetFileSizeExA(
uint32_t hFile, uint32_t hFile,
PLARGE_INTEGER lpFileSize) PLARGE_INTEGER lpFileSize)
{ {
BOOL result = GetFileSizeEx((HANDLE)hFile, lpFileSize); FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile)));
if (result) if (handle != nullptr)
lpFileSize->QuadPart = std::byteswap(lpFileSize->QuadPart); {
std::error_code ec;
auto fileSize = std::filesystem::file_size(handle->path, ec);
if (!ec)
{
if (lpFileSize != nullptr)
{
lpFileSize->QuadPart = std::byteswap(fileSize);
}
return result; return 1;
}
}
return 0;
} }
BOOL XReadFile( BOOL XReadFile(
@ -63,15 +141,32 @@ BOOL XReadFile(
XLPDWORD lpNumberOfBytesRead, XLPDWORD lpNumberOfBytesRead,
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);
if (SetFilePointer((HANDLE)hFile, lpOverlapped->Offset, &distanceToMoveHigh, FILE_BEGIN) == INVALID_SET_FILE_POINTER) handle->stream.clear();
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)
{ {
if (lpOverlapped != nullptr) if (lpOverlapped != nullptr)
@ -88,8 +183,6 @@ BOOL XReadFile(
} }
} }
//printf("ReadFile(): %x %x %x %x %x %x\n", hFile, lpBuffer, nNumberOfBytesToRead, lpNumberOfBytesRead, lpOverlapped, result);
return result; return result;
} }
@ -99,12 +192,43 @@ DWORD XSetFilePointer(
PLONG lpDistanceToMoveHigh, PLONG lpDistanceToMoveHigh,
DWORD dwMoveMethod) DWORD dwMoveMethod)
{ {
LONG distanceToMoveHigh = lpDistanceToMoveHigh ? std::byteswap(*lpDistanceToMoveHigh) : 0; FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile)));
DWORD result = SetFilePointer((HANDLE)hFile, lDistanceToMove, lpDistanceToMoveHigh ? &distanceToMoveHigh : nullptr, dwMoveMethod); if (handle == nullptr)
if (lpDistanceToMoveHigh != nullptr) {
*lpDistanceToMoveHigh = std::byteswap(distanceToMoveHigh); return INVALID_SET_FILE_POINTER;
}
return result; LONG distanceToMoveHigh = lpDistanceToMoveHigh ? std::byteswap(*lpDistanceToMoveHigh) : 0;
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)
*lpDistanceToMoveHigh = std::byteswap(LONG(streamPos >> 32U));
return DWORD(streamPos);
} }
BOOL XSetFilePointerEx( BOOL XSetFilePointerEx(
@ -113,50 +237,99 @@ BOOL XSetFilePointerEx(
PLARGE_INTEGER lpNewFilePointer, PLARGE_INTEGER lpNewFilePointer,
DWORD dwMoveMethod) DWORD dwMoveMethod)
{ {
LARGE_INTEGER distanceToMove; FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile)));
distanceToMove.QuadPart = lDistanceToMove; if (handle == nullptr)
{
return FALSE;
}
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;
}
DWORD result = SetFilePointerEx((HANDLE)hFile, distanceToMove, lpNewFilePointer, dwMoveMethod);
if (lpNewFilePointer != nullptr) if (lpNewFilePointer != nullptr)
lpNewFilePointer->QuadPart = std::byteswap(lpNewFilePointer->QuadPart); {
lpNewFilePointer->QuadPart = std::byteswap(LONGLONG(handle->stream.tellg()));
}
return result; return TRUE;
} }
uint32_t XFindFirstFileA( uint32_t XFindFirstFileA(
LPCSTR lpFileName, LPCSTR lpFileName,
LPWIN32_FIND_DATAA lpFindFileData) 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);
GuestThread::SetLastError(GetLastError()); if (transformedPathLength == 0)
if (handle == INVALID_HANDLE_VALUE) return GUEST_INVALID_HANDLE_VALUE;
std::filesystem::path dirPath;
if (strstr(transformedPath, "\\*") == (&transformedPath[transformedPathLength - 2]))
{ {
return 0xFFFFFFFF; 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.");
} }
ByteSwap(data.dwFileAttributes); if (!std::filesystem::is_directory(dirPath))
ByteSwap(*(uint64_t*)&data.ftCreationTime); return GUEST_INVALID_HANDLE_VALUE;
ByteSwap(*(uint64_t*)&data.ftLastAccessTime);
ByteSwap(*(uint64_t*)&data.ftLastWriteTime);
ByteSwap(*(uint64_t*)&data.nFileSizeHigh);
return GUEST_HANDLE(ObInsertObject(handle, FindHandleCloser)); std::filesystem::directory_iterator dirIterator(dirPath);
if (dirIterator == std::filesystem::directory_iterator())
return GUEST_INVALID_HANDLE_VALUE;
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;
ByteSwap(data.dwFileAttributes); findHandle->iterator++;
ByteSwap(*(uint64_t*)&data.ftCreationTime);
ByteSwap(*(uint64_t*)&data.ftLastAccessTime);
ByteSwap(*(uint64_t*)&data.ftLastWriteTime);
ByteSwap(*(uint64_t*)&data.nFileSizeHigh);
return result; if (findHandle->iterator == std::filesystem::directory_iterator())
{
return FALSE;
}
else
{
findHandle->fillFindData(lpFindFileData);
return TRUE;
}
} }
BOOL XReadFileEx( BOOL XReadFileEx(
@ -166,12 +339,25 @@ BOOL XReadFileEx(
XOVERLAPPED* lpOverlapped, XOVERLAPPED* lpOverlapped,
uint32_t lpCompletionRoutine) uint32_t lpCompletionRoutine)
{ {
LONG distanceToMoveHigh = lpOverlapped->OffsetHigh; FileHandle *handle = (FileHandle *)(ObQueryObject(HOST_HANDLE(hFile)));
if (SetFilePointer((HANDLE)hFile, lpOverlapped->Offset, &distanceToMoveHigh, FILE_BEGIN) == INVALID_SET_FILE_POINTER) if (handle == nullptr)
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)
{ {
lpOverlapped->Internal = 0; lpOverlapped->Internal = 0;
@ -181,14 +367,18 @@ BOOL XReadFileEx(
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( BOOL XWriteFile(
@ -198,13 +388,20 @@ BOOL XWriteFile(
LPDWORD lpNumberOfBytesWritten, LPDWORD lpNumberOfBytesWritten,
LPOVERLAPPED lpOverlapped) 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 (result && lpNumberOfBytesWritten != nullptr) if (handle == nullptr)
ByteSwap(*lpNumberOfBytesWritten); return FALSE;
return result; handle->stream.write((const char *)(lpBuffer), nNumberOfBytesToWrite);
if (handle->stream.bad())
return FALSE;
if (lpNumberOfBytesWritten != nullptr)
*lpNumberOfBytesWritten = DWORD(handle->stream.gcount());
return TRUE;
} }
const char* FileSystem::TransformPath(const char* path) const char* FileSystem::TransformPath(const char* path)