LibCore/System: Add initial Windows support

Co-authored-by: Cameron Youell <cameronyouell@gmail.com>
This commit is contained in:
stasoid 2024-10-20 06:57:00 +05:00 committed by Andrew Kaster
parent 6cba55893e
commit f6430035e7
3 changed files with 130 additions and 26 deletions

View File

@ -8,7 +8,6 @@ set(SOURCES
Environment.cpp
File.cpp
StandardPaths.cpp
System.cpp
Version.cpp
)
@ -16,11 +15,13 @@ if (WIN32)
list(APPEND SOURCES
DirectoryEntryWindows.cpp
DirIteratorWindows.cpp
SystemWindows.cpp
)
else()
list(APPEND SOURCES
DirectoryEntry.cpp
DirIterator.cpp
System.cpp
)
endif()

View File

@ -14,23 +14,30 @@
#include <AK/StringView.h>
#include <AK/Vector.h>
#include <fcntl.h>
#include <netdb.h>
#include <poll.h>
#include <pwd.h>
#include <signal.h>
#include <spawn.h>
#include <sys/ioctl.h>
#include <sys/resource.h>
#include <sys/socket.h>
#include <sys/stat.h>
#include <sys/time.h>
#include <sys/utsname.h>
#include <sys/wait.h>
#include <termios.h>
#include <time.h>
#include <utime.h>
#if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_ANDROID)
#if !defined(AK_OS_WINDOWS)
# include <netdb.h>
# include <poll.h>
# include <pwd.h>
# include <spawn.h>
# include <sys/ioctl.h>
# include <sys/resource.h>
# include <sys/socket.h>
# include <sys/time.h>
# include <sys/utsname.h>
# include <sys/wait.h>
# include <termios.h>
# include <utime.h>
#else
# define O_CLOEXEC O_NOINHERIT
using sighandler_t = void (*)(int);
using socklen_t = int;
#endif
#if !defined(AK_OS_BSD_GENERIC) && !defined(AK_OS_ANDROID) && !defined(AK_OS_WINDOWS)
# include <shadow.h>
#endif
@ -71,7 +78,6 @@ ErrorOr<struct stat> stat(StringView path);
ErrorOr<struct stat> lstat(StringView path);
ErrorOr<ssize_t> read(int fd, Bytes buffer);
ErrorOr<ssize_t> write(int fd, ReadonlyBytes buffer);
ErrorOr<void> kill(pid_t, int signal);
ErrorOr<int> dup(int source_fd);
ErrorOr<int> dup2(int source_fd, int destination_fd);
ErrorOr<ByteString> getcwd();
@ -79,17 +85,8 @@ ErrorOr<void> ioctl(int fd, unsigned request, ...);
ErrorOr<struct termios> tcgetattr(int fd);
ErrorOr<void> tcsetattr(int fd, int optional_actions, struct termios const&);
ErrorOr<void> chmod(StringView pathname, mode_t mode);
ErrorOr<void> chown(StringView pathname, uid_t uid, gid_t gid);
ErrorOr<pid_t> posix_spawn(StringView path, posix_spawn_file_actions_t const* file_actions, posix_spawnattr_t const* attr, char* const arguments[], char* const envp[]);
ErrorOr<pid_t> posix_spawnp(StringView path, posix_spawn_file_actions_t* const file_actions, posix_spawnattr_t* const attr, char* const arguments[], char* const envp[]);
ErrorOr<off_t> lseek(int fd, off_t, int whence);
struct WaitPidResult {
pid_t pid;
int status;
};
ErrorOr<WaitPidResult> waitpid(pid_t waitee, int options = 0);
ErrorOr<bool> isatty(int fd);
ErrorOr<void> link(StringView old_path, StringView new_path);
ErrorOr<void> symlink(StringView target, StringView link_path);
@ -99,11 +96,9 @@ ErrorOr<void> rmdir(StringView path);
ErrorOr<int> mkstemp(Span<char> pattern);
ErrorOr<String> mkdtemp(Span<char> pattern);
ErrorOr<void> fchmod(int fd, mode_t mode);
ErrorOr<void> fchown(int fd, uid_t, gid_t);
ErrorOr<void> rename(StringView old_path, StringView new_path);
ErrorOr<void> unlink(StringView path);
ErrorOr<void> utimensat(int fd, StringView path, struct timespec const times[2], int flag);
ErrorOr<struct utsname> uname();
ErrorOr<Array<int, 2>> pipe2(int flags);
ErrorOr<int> socket(int domain, int type, int protocol);
@ -127,6 +122,20 @@ ErrorOr<void> access(StringView pathname, int mode, int flags = 0);
ErrorOr<ByteString> readlink(StringView pathname);
ErrorOr<int> poll(Span<struct pollfd>, int timeout);
#if !defined(AK_OS_WINDOWS)
ErrorOr<void> kill(pid_t, int signal);
ErrorOr<void> chown(StringView pathname, uid_t uid, gid_t gid);
ErrorOr<pid_t> posix_spawn(StringView path, posix_spawn_file_actions_t const* file_actions, posix_spawnattr_t const* attr, char* const arguments[], char* const envp[]);
ErrorOr<pid_t> posix_spawnp(StringView path, posix_spawn_file_actions_t* const file_actions, posix_spawnattr_t* const attr, char* const arguments[], char* const envp[]);
struct WaitPidResult {
pid_t pid;
int status;
};
ErrorOr<WaitPidResult> waitpid(pid_t waitee, int options = 0);
ErrorOr<void> fchown(int fd, uid_t, gid_t);
ErrorOr<struct utsname> uname();
class AddressInfoVector {
AK_MAKE_NONCOPYABLE(AddressInfoVector);
AK_MAKE_DEFAULT_MOVABLE(AddressInfoVector);
@ -158,13 +167,16 @@ private:
};
ErrorOr<AddressInfoVector> getaddrinfo(char const* nodename, char const* servname, struct addrinfo const& hints);
#endif
unsigned hardware_concurrency();
u64 physical_memory_bytes();
ErrorOr<ByteString> current_executable_path();
#if !defined(AK_OS_WINDOWS)
ErrorOr<rlimit> get_resource_limits(int resource);
ErrorOr<void> set_resource_limits(int resource, rlim_t limit);
#endif
}

View File

@ -0,0 +1,91 @@
/*
* Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
* Copyright (c) 2021-2022, Kenneth Myhra <kennethmyhra@serenityos.org>
* Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
* Copyright (c) 2022, Matthias Zimmerman <matthias291999@gmail.com>
* Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
* Copyright (c) 2024, stasoid <stasoid@yahoo.com>
*
* SPDX-License-Identifier: BSD-2-Clause
*/
#include <AK/ByteString.h>
#include <AK/ScopeGuard.h>
#include <LibCore/System.h>
#include <WinSock2.h>
#include <io.h>
namespace Core::System {
ErrorOr<int> open(StringView path, int options, mode_t mode)
{
ByteString string_path = path;
int rc = _open(string_path.characters(), options, mode);
if (rc < 0)
return Error::from_syscall("open"sv, -errno);
return rc;
}
ErrorOr<void> close(int fd)
{
if (_close(fd) < 0)
return Error::from_syscall("close"sv, -errno);
return {};
}
ErrorOr<ssize_t> read(int fd, Bytes buffer)
{
int rc = _read(fd, buffer.data(), buffer.size());
if (rc < 0)
return Error::from_syscall("read"sv, -errno);
return rc;
}
ErrorOr<ssize_t> write(int fd, ReadonlyBytes buffer)
{
int rc = _write(fd, buffer.data(), buffer.size());
if (rc < 0)
return Error::from_syscall("write"sv, -errno);
return rc;
}
ErrorOr<off_t> lseek(int fd, off_t offset, int whence)
{
long rc = _lseek(fd, offset, whence);
if (rc < 0)
return Error::from_syscall("lseek"sv, -errno);
return rc;
}
ErrorOr<void> ftruncate(int fd, off_t length)
{
long position = _tell(fd);
if (position == -1)
return Error::from_errno(errno);
ScopeGuard restore_position { [&] { _lseek(fd, position, SEEK_SET); } };
auto result = lseek(fd, length, SEEK_SET);
if (result.is_error())
return result.release_error();
if (SetEndOfFile((HANDLE)_get_osfhandle(fd)) == 0)
return Error::from_windows_error(GetLastError());
return {};
}
ErrorOr<struct stat> fstat(int fd)
{
struct stat st = {};
if (::fstat(fd, &st) < 0)
return Error::from_syscall("fstat"sv, -errno);
return st;
}
ErrorOr<void> ioctl(int, unsigned, ...)
{
dbgln("Core::System::ioctl() is not implemented");
VERIFY_NOT_REACHED();
}
}