Split kernel32 into separate files (part 3)

This commit is contained in:
2025-10-02 01:02:26 -06:00
parent fa3ed4893a
commit 2cbd624119
18 changed files with 1153 additions and 994 deletions

View File

@@ -2,8 +2,10 @@
#include "errors.h"
#include "files.h"
#include "handles.h"
#include "internal.h"
#include "strutil.h"
#include "timeutil.h"
#include <algorithm>
#include <cerrno>
@@ -12,8 +14,12 @@
#include <fcntl.h>
#include <filesystem>
#include <fnmatch.h>
#include <limits>
#include <optional>
#include <random>
#include <string>
#include <sys/stat.h>
#include <sys/time.h>
#include <system_error>
#include <unistd.h>
@@ -31,6 +37,48 @@ const FILETIME kDefaultFindFileTime = {
static_cast<DWORD>((kSecondsBetween1601And1970 * kWindowsTicksPerSecond) & 0xFFFFFFFFULL),
static_cast<DWORD>((kSecondsBetween1601And1970 * kWindowsTicksPerSecond) >> 32)};
const FILETIME kDefaultFileInformationTime = {static_cast<DWORD>(UNIX_TIME_ZERO & 0xFFFFFFFFULL),
static_cast<DWORD>(UNIX_TIME_ZERO >> 32)};
struct timespec accessTimespec(const struct stat &st) {
#if defined(__APPLE__)
return st.st_atimespec;
#elif defined(__linux__)
return st.st_atim;
#else
struct timespec ts{};
ts.tv_sec = st.st_atime;
ts.tv_nsec = 0;
return ts;
#endif
}
struct timespec modifyTimespec(const struct stat &st) {
#if defined(__APPLE__)
return st.st_mtimespec;
#elif defined(__linux__)
return st.st_mtim;
#else
struct timespec ts{};
ts.tv_sec = st.st_mtime;
ts.tv_nsec = 0;
return ts;
#endif
}
struct timespec changeTimespec(const struct stat &st) {
#if defined(__APPLE__)
return st.st_ctimespec;
#elif defined(__linux__)
return st.st_ctim;
#else
struct timespec ts{};
ts.tv_sec = st.st_ctime;
ts.tv_nsec = 0;
return ts;
#endif
}
struct FindFirstFileHandle {
std::filesystem::directory_iterator it;
std::filesystem::directory_iterator end;
@@ -177,6 +225,701 @@ bool initializeEnumeration(const std::filesystem::path &parent, const std::strin
namespace kernel32 {
int64_t getFileSizeFromHandle(HANDLE hFile) {
FILE *fp = files::fpFromHandle(hFile);
if (!fp) {
wibo::lastError = ERROR_INVALID_HANDLE;
return -1;
}
struct stat64 st{};
fflush(fp);
if (fstat64(fileno(fp), &st) == -1 || !S_ISREG(st.st_mode)) {
setLastErrorFromErrno();
return -1;
}
return st.st_size;
}
DWORD WIN_FUNC GetFileAttributesA(LPCSTR lpFileName) {
if (!lpFileName) {
wibo::lastError = ERROR_INVALID_PARAMETER;
return INVALID_FILE_ATTRIBUTES;
}
std::filesystem::path path = files::pathFromWindows(lpFileName);
std::string pathStr = path.string();
DEBUG_LOG("GetFileAttributesA(%s) -> %s\n", lpFileName, pathStr.c_str());
if (endsWith(pathStr, "/license.dat")) {
DEBUG_LOG("MWCC license override\n");
wibo::lastError = ERROR_SUCCESS;
return FILE_ATTRIBUTE_NORMAL;
}
std::error_code ec;
auto status = std::filesystem::status(path, ec);
if (ec) {
errno = ec.value();
setLastErrorFromErrno();
return INVALID_FILE_ATTRIBUTES;
}
wibo::lastError = ERROR_SUCCESS;
switch (status.type()) {
case std::filesystem::file_type::regular:
DEBUG_LOG("File exists\n");
return FILE_ATTRIBUTE_NORMAL;
case std::filesystem::file_type::directory:
return FILE_ATTRIBUTE_DIRECTORY;
case std::filesystem::file_type::not_found:
default:
DEBUG_LOG("File does not exist\n");
wibo::lastError = ERROR_FILE_NOT_FOUND;
return INVALID_FILE_ATTRIBUTES;
}
}
DWORD WIN_FUNC GetFileAttributesW(LPCWSTR lpFileName) {
DEBUG_LOG("GetFileAttributesW -> ");
if (!lpFileName) {
wibo::lastError = ERROR_INVALID_PARAMETER;
return INVALID_FILE_ATTRIBUTES;
}
std::string str = wideStringToString(lpFileName);
return GetFileAttributesA(str.c_str());
}
BOOL WIN_FUNC WriteFile(HANDLE hFile, LPCVOID lpBuffer, DWORD nNumberOfBytesToWrite, LPDWORD lpNumberOfBytesWritten,
LPOVERLAPPED lpOverlapped) {
DEBUG_LOG("WriteFile(%p, %u)\n", hFile, nNumberOfBytesToWrite);
wibo::lastError = ERROR_SUCCESS;
auto file = files::fileHandleFromHandle(hFile);
if (!file || !file->fp) {
wibo::lastError = ERROR_INVALID_HANDLE;
return FALSE;
}
bool handleOverlapped = (file->flags & FILE_FLAG_OVERLAPPED) != 0;
auto *overlapped = reinterpret_cast<OVERLAPPED *>(lpOverlapped);
bool usingOverlapped = overlapped != nullptr;
if (!usingOverlapped && lpNumberOfBytesWritten == nullptr) {
wibo::lastError = ERROR_INVALID_PARAMETER;
return FALSE;
}
std::optional<uint64_t> offset;
bool updateFilePointer = true;
if (usingOverlapped) {
offset = (static_cast<uint64_t>(overlapped->Offset) | (static_cast<uint64_t>(overlapped->OffsetHigh) << 32));
overlapped->Internal = STATUS_PENDING;
overlapped->InternalHigh = 0;
updateFilePointer = !handleOverlapped;
resetOverlappedEvent(overlapped);
}
auto io = files::write(file, lpBuffer, nNumberOfBytesToWrite, offset, updateFilePointer);
DWORD completionStatus = STATUS_SUCCESS;
if (io.unixError != 0) {
completionStatus = wibo::winErrorFromErrno(io.unixError);
wibo::lastError = completionStatus;
if (lpNumberOfBytesWritten) {
*lpNumberOfBytesWritten = static_cast<DWORD>(io.bytesTransferred);
}
if (usingOverlapped) {
overlapped->Internal = completionStatus;
overlapped->InternalHigh = io.bytesTransferred;
signalOverlappedEvent(overlapped);
}
return FALSE;
}
if (lpNumberOfBytesWritten && (!handleOverlapped || !usingOverlapped)) {
*lpNumberOfBytesWritten = static_cast<DWORD>(io.bytesTransferred);
}
if (usingOverlapped) {
overlapped->Internal = completionStatus;
overlapped->InternalHigh = io.bytesTransferred;
if (!handleOverlapped) {
uint64_t baseOffset = offset.value_or(0);
uint64_t newOffset = baseOffset + io.bytesTransferred;
overlapped->Offset = static_cast<DWORD>(newOffset & 0xFFFFFFFFu);
overlapped->OffsetHigh = static_cast<DWORD>(newOffset >> 32);
}
signalOverlappedEvent(overlapped);
}
return (io.bytesTransferred == nNumberOfBytesToWrite) ? TRUE : FALSE;
}
BOOL WIN_FUNC FlushFileBuffers(HANDLE hFile) {
DEBUG_LOG("FlushFileBuffers(%p)\n", hFile);
auto data = handles::dataFromHandle(hFile, false);
if (data.type != handles::TYPE_FILE || data.ptr == nullptr) {
wibo::lastError = ERROR_INVALID_HANDLE;
return FALSE;
}
auto file = reinterpret_cast<files::FileHandle *>(data.ptr);
if (!file || !file->fp) {
wibo::lastError = ERROR_INVALID_HANDLE;
return FALSE;
}
FILE *fp = file->fp;
if (fflush(fp) != 0) {
wibo::lastError = ERROR_ACCESS_DENIED;
return FALSE;
}
int fd = file->fd;
if (fd >= 0 && fsync(fd) != 0) {
wibo::lastError = ERROR_ACCESS_DENIED;
return FALSE;
}
wibo::lastError = ERROR_SUCCESS;
return TRUE;
}
BOOL WIN_FUNC ReadFile(HANDLE hFile, LPVOID lpBuffer, DWORD nNumberOfBytesToRead, LPDWORD lpNumberOfBytesRead,
LPOVERLAPPED lpOverlapped) {
DEBUG_LOG("ReadFile(%p, %u)\n", hFile, nNumberOfBytesToRead);
wibo::lastError = ERROR_SUCCESS;
auto file = files::fileHandleFromHandle(hFile);
if (!file || !file->fp) {
wibo::lastError = ERROR_INVALID_HANDLE;
return FALSE;
}
bool handleOverlapped = (file->flags & FILE_FLAG_OVERLAPPED) != 0;
auto *overlapped = reinterpret_cast<OVERLAPPED *>(lpOverlapped);
bool usingOverlapped = overlapped != nullptr;
if (!usingOverlapped && lpNumberOfBytesRead == nullptr) {
wibo::lastError = ERROR_INVALID_PARAMETER;
return FALSE;
}
std::optional<uint64_t> offset;
bool updateFilePointer = true;
if (usingOverlapped) {
offset = (static_cast<uint64_t>(overlapped->Offset) | (static_cast<uint64_t>(overlapped->OffsetHigh) << 32));
overlapped->Internal = STATUS_PENDING;
overlapped->InternalHigh = 0;
updateFilePointer = !handleOverlapped;
resetOverlappedEvent(overlapped);
}
auto io = files::read(file, lpBuffer, nNumberOfBytesToRead, offset, updateFilePointer);
DWORD completionStatus = STATUS_SUCCESS;
if (io.unixError != 0) {
completionStatus = wibo::winErrorFromErrno(io.unixError);
wibo::lastError = completionStatus;
if (lpNumberOfBytesRead) {
*lpNumberOfBytesRead = static_cast<DWORD>(io.bytesTransferred);
}
if (usingOverlapped) {
overlapped->Internal = completionStatus;
overlapped->InternalHigh = io.bytesTransferred;
signalOverlappedEvent(overlapped);
}
return FALSE;
}
if (io.reachedEnd && io.bytesTransferred == 0 && handleOverlapped) {
completionStatus = ERROR_HANDLE_EOF;
}
if (lpNumberOfBytesRead && (!handleOverlapped || !usingOverlapped)) {
*lpNumberOfBytesRead = static_cast<DWORD>(io.bytesTransferred);
}
if (usingOverlapped) {
overlapped->Internal = completionStatus;
overlapped->InternalHigh = io.bytesTransferred;
if (!handleOverlapped) {
uint64_t baseOffset = offset.value_or(0);
uint64_t newOffset = baseOffset + io.bytesTransferred;
overlapped->Offset = static_cast<DWORD>(newOffset & 0xFFFFFFFFu);
overlapped->OffsetHigh = static_cast<DWORD>(newOffset >> 32);
}
signalOverlappedEvent(overlapped);
}
return TRUE;
}
HANDLE WIN_FUNC CreateFileA(LPCSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) {
(void)lpSecurityAttributes;
(void)hTemplateFile;
if (!lpFileName) {
wibo::lastError = ERROR_INVALID_PARAMETER;
return INVALID_HANDLE_VALUE;
}
std::string path = files::pathFromWindows(lpFileName);
DEBUG_LOG("CreateFileA(filename=%s (%s), desiredAccess=0x%x, shareMode=%u, securityAttributes=%p, "
"creationDisposition=%u, flagsAndAttributes=%u)\n",
lpFileName, path.c_str(), dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition,
dwFlagsAndAttributes);
wibo::lastError = ERROR_SUCCESS;
bool fileExists = (access(path.c_str(), F_OK) == 0);
bool shouldTruncate = false;
switch (dwCreationDisposition) {
case CREATE_ALWAYS:
if (fileExists) {
wibo::lastError = ERROR_ALREADY_EXISTS;
shouldTruncate = true;
}
break;
case CREATE_NEW:
if (fileExists) {
wibo::lastError = ERROR_FILE_EXISTS;
return INVALID_HANDLE_VALUE;
}
break;
case OPEN_ALWAYS:
if (fileExists) {
wibo::lastError = ERROR_ALREADY_EXISTS;
}
break;
case OPEN_EXISTING:
if (!fileExists) {
wibo::lastError = ERROR_FILE_NOT_FOUND;
return INVALID_HANDLE_VALUE;
}
break;
case TRUNCATE_EXISTING:
shouldTruncate = true;
if (!fileExists) {
wibo::lastError = ERROR_FILE_NOT_FOUND;
return INVALID_HANDLE_VALUE;
}
break;
default:
assert(false);
}
FILE *fp = nullptr;
if (dwDesiredAccess == GENERIC_READ) {
fp = fopen(path.c_str(), "rb");
} else if (dwDesiredAccess == GENERIC_WRITE) {
if (shouldTruncate || !fileExists) {
fp = fopen(path.c_str(), "wb");
} else {
fp = fopen(path.c_str(), "rb+");
}
} else if (dwDesiredAccess == (GENERIC_READ | GENERIC_WRITE)) {
if (shouldTruncate || !fileExists) {
fp = fopen(path.c_str(), "wb+");
} else {
fp = fopen(path.c_str(), "rb+");
}
} else {
assert(false);
}
if (fp) {
void *handle = files::allocFpHandle(fp, dwDesiredAccess, dwShareMode, dwFlagsAndAttributes, true);
DEBUG_LOG("-> %p\n", handle);
return handle;
}
setLastErrorFromErrno();
return INVALID_HANDLE_VALUE;
}
HANDLE WIN_FUNC CreateFileW(LPCWSTR lpFileName, DWORD dwDesiredAccess, DWORD dwShareMode,
LPSECURITY_ATTRIBUTES lpSecurityAttributes, DWORD dwCreationDisposition,
DWORD dwFlagsAndAttributes, HANDLE hTemplateFile) {
DEBUG_LOG("CreateFileW -> ");
if (!lpFileName) {
wibo::lastError = ERROR_INVALID_PARAMETER;
return INVALID_HANDLE_VALUE;
}
std::string lpFileNameA = wideStringToString(lpFileName);
return CreateFileA(lpFileNameA.c_str(), dwDesiredAccess, dwShareMode, lpSecurityAttributes, dwCreationDisposition,
dwFlagsAndAttributes, hTemplateFile);
}
BOOL WIN_FUNC DeleteFileA(LPCSTR lpFileName) {
if (!lpFileName) {
wibo::lastError = ERROR_INVALID_PARAMETER;
DEBUG_LOG("DeleteFileA(NULL) -> ERROR_INVALID_PARAMETER\n");
return FALSE;
}
std::string path = files::pathFromWindows(lpFileName);
DEBUG_LOG("DeleteFileA(%s) -> %s\n", lpFileName, path.c_str());
if (unlink(path.c_str()) == 0) {
wibo::lastError = ERROR_SUCCESS;
return TRUE;
}
setLastErrorFromErrno();
return FALSE;
}
BOOL WIN_FUNC DeleteFileW(LPCWSTR lpFileName) {
DEBUG_LOG("DeleteFileW -> ");
if (!lpFileName) {
wibo::lastError = ERROR_INVALID_PARAMETER;
return FALSE;
}
std::string name = wideStringToString(lpFileName);
return DeleteFileA(name.c_str());
}
BOOL WIN_FUNC MoveFileA(LPCSTR lpExistingFileName, LPCSTR lpNewFileName) {
DEBUG_LOG("MoveFileA(%s, %s)\n", lpExistingFileName ? lpExistingFileName : "(null)",
lpNewFileName ? lpNewFileName : "(null)");
if (!lpExistingFileName || !lpNewFileName) {
wibo::lastError = ERROR_INVALID_PARAMETER;
return FALSE;
}
auto fromPath = files::pathFromWindows(lpExistingFileName);
auto toPath = files::pathFromWindows(lpNewFileName);
std::error_code ec;
if (std::filesystem::exists(toPath, ec)) {
wibo::lastError = ERROR_ALREADY_EXISTS;
return FALSE;
}
if (ec) {
errno = ec.value();
setLastErrorFromErrno();
return FALSE;
}
std::filesystem::rename(fromPath, toPath, ec);
if (ec) {
errno = ec.value();
setLastErrorFromErrno();
return FALSE;
}
wibo::lastError = ERROR_SUCCESS;
return TRUE;
}
BOOL WIN_FUNC MoveFileW(LPCWSTR lpExistingFileName, LPCWSTR lpNewFileName) {
DEBUG_LOG("MoveFileW -> ");
if (!lpExistingFileName || !lpNewFileName) {
wibo::lastError = ERROR_INVALID_PARAMETER;
return FALSE;
}
std::string from = wideStringToString(lpExistingFileName);
std::string to = wideStringToString(lpNewFileName);
return MoveFileA(from.c_str(), to.c_str());
}
DWORD WIN_FUNC SetFilePointer(HANDLE hFile, LONG lDistanceToMove, PLONG lpDistanceToMoveHigh, DWORD dwMoveMethod) {
DEBUG_LOG("SetFilePointer(%p, %ld, %p, %u)\n", hFile, static_cast<long>(lDistanceToMove), lpDistanceToMoveHigh,
dwMoveMethod);
if (hFile == nullptr) {
wibo::lastError = ERROR_INVALID_HANDLE;
return INVALID_SET_FILE_POINTER;
}
FILE *fp = files::fpFromHandle(hFile);
if (!fp) {
wibo::lastError = ERROR_INVALID_HANDLE;
return INVALID_SET_FILE_POINTER;
}
int origin = SEEK_SET;
if (dwMoveMethod == FILE_CURRENT) {
origin = SEEK_CUR;
} else if (dwMoveMethod == FILE_END) {
origin = SEEK_END;
}
wibo::lastError = ERROR_SUCCESS;
if (fseek(fp, lDistanceToMove, origin) < 0) {
if (errno == EINVAL) {
wibo::lastError = ERROR_NEGATIVE_SEEK;
} else {
wibo::lastError = ERROR_INVALID_PARAMETER;
}
return INVALID_SET_FILE_POINTER;
}
long position = ftell(fp);
if (position < 0) {
setLastErrorFromErrno();
return INVALID_SET_FILE_POINTER;
}
if (lpDistanceToMoveHigh) {
*lpDistanceToMoveHigh = static_cast<LONG>(static_cast<uint64_t>(position) >> 32);
}
return static_cast<DWORD>(static_cast<uint64_t>(position) & 0xFFFFFFFFu);
}
BOOL WIN_FUNC SetFilePointerEx(HANDLE hFile, LARGE_INTEGER liDistanceToMove, PLARGE_INTEGER lpNewFilePointer,
DWORD dwMoveMethod) {
if (hFile == nullptr) {
wibo::lastError = ERROR_INVALID_HANDLE;
return FALSE;
}
FILE *fp = files::fpFromHandle(hFile);
if (!fp) {
wibo::lastError = ERROR_INVALID_HANDLE;
return FALSE;
}
int origin = SEEK_SET;
if (dwMoveMethod == FILE_CURRENT) {
origin = SEEK_CUR;
} else if (dwMoveMethod == FILE_END) {
origin = SEEK_END;
}
wibo::lastError = ERROR_SUCCESS;
if (fseeko(fp, static_cast<off_t>(liDistanceToMove), origin) < 0) {
if (errno == EINVAL) {
wibo::lastError = ERROR_NEGATIVE_SEEK;
} else {
wibo::lastError = ERROR_INVALID_PARAMETER;
}
return FALSE;
}
off_t position = ftello(fp);
if (position < 0) {
setLastErrorFromErrno();
return FALSE;
}
if (lpNewFilePointer) {
*lpNewFilePointer = static_cast<LARGE_INTEGER>(position);
}
return TRUE;
}
BOOL WIN_FUNC SetEndOfFile(HANDLE hFile) {
DEBUG_LOG("SetEndOfFile(%p)\n", hFile);
FILE *fp = files::fpFromHandle(hFile);
if (!fp) {
wibo::lastError = ERROR_INVALID_HANDLE;
return FALSE;
}
if (fflush(fp) != 0 || ftruncate(fileno(fp), ftell(fp)) != 0) {
setLastErrorFromErrno();
return FALSE;
}
wibo::lastError = ERROR_SUCCESS;
return TRUE;
}
BOOL WIN_FUNC CreateDirectoryA(LPCSTR lpPathName, LPSECURITY_ATTRIBUTES lpSecurityAttributes) {
(void)lpSecurityAttributes;
if (!lpPathName) {
wibo::lastError = ERROR_INVALID_PARAMETER;
return FALSE;
}
std::string path = files::pathFromWindows(lpPathName);
DEBUG_LOG("CreateDirectoryA(%s, %p)\n", path.c_str(), lpSecurityAttributes);
if (mkdir(path.c_str(), 0755) == 0) {
wibo::lastError = ERROR_SUCCESS;
return TRUE;
}
setLastErrorFromErrno();
return FALSE;
}
BOOL WIN_FUNC RemoveDirectoryA(LPCSTR lpPathName) {
if (!lpPathName) {
wibo::lastError = ERROR_INVALID_PARAMETER;
return FALSE;
}
std::string path = files::pathFromWindows(lpPathName);
DEBUG_LOG("RemoveDirectoryA(%s)\n", path.c_str());
if (rmdir(path.c_str()) == 0) {
wibo::lastError = ERROR_SUCCESS;
return TRUE;
}
setLastErrorFromErrno();
return FALSE;
}
BOOL WIN_FUNC SetFileAttributesA(LPCSTR lpFileName, DWORD dwFileAttributes) {
(void)dwFileAttributes;
if (!lpFileName) {
wibo::lastError = ERROR_INVALID_PARAMETER;
return FALSE;
}
DEBUG_LOG("SetFileAttributesA(%s, %u)\n", lpFileName, dwFileAttributes);
wibo::lastError = ERROR_SUCCESS;
return TRUE;
}
DWORD WIN_FUNC GetFileSize(HANDLE hFile, LPDWORD lpFileSizeHigh) {
DEBUG_LOG("GetFileSize(%p, %p) ", hFile, lpFileSizeHigh);
int64_t size = getFileSizeFromHandle(hFile);
if (size < 0) {
if (lpFileSizeHigh) {
*lpFileSizeHigh = 0;
}
return INVALID_FILE_SIZE;
}
uint64_t uSize = static_cast<uint64_t>(size);
if (lpFileSizeHigh) {
*lpFileSizeHigh = static_cast<DWORD>(uSize >> 32);
}
wibo::lastError = ERROR_SUCCESS;
return static_cast<DWORD>(uSize & 0xFFFFFFFFu);
}
BOOL WIN_FUNC GetFileTime(HANDLE hFile, LPFILETIME lpCreationTime, LPFILETIME lpLastAccessTime,
LPFILETIME lpLastWriteTime) {
DEBUG_LOG("GetFileTime(%p, %p, %p, %p)\n", hFile, lpCreationTime, lpLastAccessTime, lpLastWriteTime);
auto file = files::fileHandleFromHandle(hFile);
if (!file || !file->fp) {
wibo::lastError = ERROR_INVALID_HANDLE;
return FALSE;
}
struct stat st{};
if (fstat(fileno(file->fp), &st) != 0) {
setLastErrorFromErrno();
return FALSE;
}
auto assignFileTime = [](LPFILETIME target, const struct timespec &spec) -> bool {
if (!target) {
return true;
}
FILETIME result{};
uint32_t hundreds = static_cast<uint32_t>(spec.tv_nsec / 100L);
if (!unixPartsToFileTime(static_cast<int64_t>(spec.tv_sec), hundreds, result)) {
return false;
}
*target = result;
return true;
};
if (!assignFileTime(lpCreationTime, changeTimespec(st)) || !assignFileTime(lpLastAccessTime, accessTimespec(st)) ||
!assignFileTime(lpLastWriteTime, modifyTimespec(st))) {
wibo::lastError = ERROR_INVALID_PARAMETER;
return FALSE;
}
wibo::lastError = ERROR_SUCCESS;
return TRUE;
}
BOOL WIN_FUNC SetFileTime(HANDLE hFile, const FILETIME *lpCreationTime, const FILETIME *lpLastAccessTime,
const FILETIME *lpLastWriteTime) {
DEBUG_LOG("SetFileTime(%p, %p, %p, %p)\n", hFile, lpCreationTime, lpLastAccessTime, lpLastWriteTime);
FILE *fp = files::fpFromHandle(hFile);
if (!fp) {
wibo::lastError = ERROR_INVALID_HANDLE;
return FALSE;
}
int fd = fileno(fp);
if (fd < 0) {
setLastErrorFromErrno();
return FALSE;
}
bool changeAccess = !shouldIgnoreFileTimeParam(lpLastAccessTime);
bool changeWrite = !shouldIgnoreFileTimeParam(lpLastWriteTime);
if (!changeAccess && !changeWrite) {
wibo::lastError = ERROR_SUCCESS;
return TRUE;
}
struct stat st{};
if (fstat(fd, &st) != 0) {
setLastErrorFromErrno();
return FALSE;
}
struct timespec accessSpec = accessTimespec(st);
struct timespec writeSpec = modifyTimespec(st);
if (changeAccess) {
int64_t seconds = 0;
uint32_t hundreds = 0;
if (!fileTimeToUnixParts(*lpLastAccessTime, seconds, hundreds) ||
!unixPartsToTimespec(seconds, hundreds, accessSpec)) {
wibo::lastError = ERROR_INVALID_PARAMETER;
return FALSE;
}
}
if (changeWrite) {
int64_t seconds = 0;
uint32_t hundreds = 0;
if (!fileTimeToUnixParts(*lpLastWriteTime, seconds, hundreds) ||
!unixPartsToTimespec(seconds, hundreds, writeSpec)) {
wibo::lastError = ERROR_INVALID_PARAMETER;
return FALSE;
}
}
#if defined(__APPLE__) || defined(__FreeBSD__)
struct timeval tv[2];
tv[0].tv_sec = accessSpec.tv_sec;
tv[0].tv_usec = accessSpec.tv_nsec / 1000L;
tv[1].tv_sec = writeSpec.tv_sec;
tv[1].tv_usec = writeSpec.tv_nsec / 1000L;
if (futimes(fd, tv) != 0) {
setLastErrorFromErrno();
return FALSE;
}
#else
struct timespec times[2] = {accessSpec, writeSpec};
if (futimens(fd, times) != 0) {
setLastErrorFromErrno();
return FALSE;
}
#endif
if (!shouldIgnoreFileTimeParam(lpCreationTime) && lpCreationTime) {
DEBUG_LOG("SetFileTime: creation time not supported\n");
}
wibo::lastError = ERROR_SUCCESS;
return TRUE;
}
BOOL WIN_FUNC GetFileInformationByHandle(HANDLE hFile, LPBY_HANDLE_FILE_INFORMATION lpFileInformation) {
DEBUG_LOG("GetFileInformationByHandle(%p, %p)\n", hFile, lpFileInformation);
FILE *fp = files::fpFromHandle(hFile);
if (fp == nullptr) {
wibo::lastError = ERROR_INVALID_HANDLE;
return FALSE;
}
struct stat64 st{};
if (fstat64(fileno(fp), &st) != 0) {
setLastErrorFromErrno();
return FALSE;
}
if (lpFileInformation) {
lpFileInformation->dwFileAttributes = 0;
if (S_ISDIR(st.st_mode)) {
lpFileInformation->dwFileAttributes |= FILE_ATTRIBUTE_DIRECTORY;
}
if (S_ISREG(st.st_mode)) {
lpFileInformation->dwFileAttributes |= FILE_ATTRIBUTE_NORMAL;
}
lpFileInformation->ftCreationTime = kDefaultFileInformationTime;
lpFileInformation->ftLastAccessTime = kDefaultFileInformationTime;
lpFileInformation->ftLastWriteTime = kDefaultFileInformationTime;
lpFileInformation->dwVolumeSerialNumber = 0;
lpFileInformation->nFileSizeHigh = static_cast<DWORD>(static_cast<uint64_t>(st.st_size) >> 32);
lpFileInformation->nFileSizeLow = static_cast<DWORD>(st.st_size & 0xFFFFFFFFULL);
lpFileInformation->nNumberOfLinks = 0;
lpFileInformation->nFileIndexHigh = 0;
lpFileInformation->nFileIndexLow = 0;
}
wibo::lastError = ERROR_SUCCESS;
return TRUE;
}
DWORD WIN_FUNC GetFileType(HANDLE hFile) {
DEBUG_LOG("GetFileType(%p) ", hFile);
auto *file = files::fileHandleFromHandle(hFile);
if (!file || file->fd < 0) {
wibo::lastError = ERROR_INVALID_HANDLE;
DEBUG_LOG("-> ERROR_INVALID_HANDLE\n");
return FILE_TYPE_UNKNOWN;
}
struct stat st{};
if (fstat(file->fd, &st) != 0) {
setLastErrorFromErrno();
DEBUG_LOG("-> fstat error\n");
return FILE_TYPE_UNKNOWN;
}
wibo::lastError = ERROR_SUCCESS;
DWORD type = FILE_TYPE_UNKNOWN;
if (S_ISREG(st.st_mode) || S_ISDIR(st.st_mode) || S_ISBLK(st.st_mode)) {
type = FILE_TYPE_DISK;
}
if (S_ISCHR(st.st_mode)) {
type = FILE_TYPE_CHAR;
}
if (S_ISSOCK(st.st_mode) || S_ISFIFO(st.st_mode)) {
type = FILE_TYPE_PIPE;
}
DEBUG_LOG("-> %u\n", type);
return type;
}
DWORD WIN_FUNC GetFullPathNameA(LPCSTR lpFileName, DWORD nBufferLength, LPSTR lpBuffer, LPSTR *lpFilePart) {
DEBUG_LOG("GetFullPathNameA(%s, %u)\n", lpFileName ? lpFileName : "(null)", nBufferLength);