SystemWindows.cpp 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187
  1. /*
  2. * Copyright (c) 2021-2022, Andreas Kling <kling@serenityos.org>
  3. * Copyright (c) 2021-2022, Kenneth Myhra <kennethmyhra@serenityos.org>
  4. * Copyright (c) 2021-2022, Sam Atkins <atkinssj@serenityos.org>
  5. * Copyright (c) 2022, Matthias Zimmerman <matthias291999@gmail.com>
  6. * Copyright (c) 2023, Cameron Youell <cameronyouell@gmail.com>
  7. * Copyright (c) 2024, stasoid <stasoid@yahoo.com>
  8. *
  9. * SPDX-License-Identifier: BSD-2-Clause
  10. */
  11. #include <AK/ByteString.h>
  12. #include <AK/ScopeGuard.h>
  13. #include <LibCore/System.h>
  14. #include <Windows.h>
  15. #include <direct.h>
  16. #include <io.h>
  17. #include <sys/mman.h>
  18. namespace Core::System {
  19. ErrorOr<int> open(StringView path, int options, mode_t mode)
  20. {
  21. ByteString string_path = path;
  22. auto sz_path = string_path.characters();
  23. int rc = _open(sz_path, options | O_BINARY, mode);
  24. if (rc < 0) {
  25. int error = errno;
  26. struct stat st = {};
  27. if (::stat(sz_path, &st) == 0 && (st.st_mode & S_IFDIR)) {
  28. HANDLE dir_handle = CreateFile(sz_path, GENERIC_ALL, 0, NULL, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, NULL);
  29. if (dir_handle == INVALID_HANDLE_VALUE)
  30. return Error::from_windows_error(GetLastError());
  31. int dir_fd = _open_osfhandle((intptr_t)dir_handle, 0);
  32. if (dir_fd != -1)
  33. return dir_fd;
  34. }
  35. return Error::from_syscall("open"sv, -error);
  36. }
  37. return rc;
  38. }
  39. ErrorOr<void> close(int fd)
  40. {
  41. if (_close(fd) < 0)
  42. return Error::from_syscall("close"sv, -errno);
  43. return {};
  44. }
  45. ErrorOr<ssize_t> read(int fd, Bytes buffer)
  46. {
  47. int rc = _read(fd, buffer.data(), buffer.size());
  48. if (rc < 0)
  49. return Error::from_syscall("read"sv, -errno);
  50. return rc;
  51. }
  52. ErrorOr<ssize_t> write(int fd, ReadonlyBytes buffer)
  53. {
  54. int rc = _write(fd, buffer.data(), buffer.size());
  55. if (rc < 0)
  56. return Error::from_syscall("write"sv, -errno);
  57. return rc;
  58. }
  59. ErrorOr<off_t> lseek(int fd, off_t offset, int whence)
  60. {
  61. long rc = _lseek(fd, offset, whence);
  62. if (rc < 0)
  63. return Error::from_syscall("lseek"sv, -errno);
  64. return rc;
  65. }
  66. ErrorOr<void> ftruncate(int fd, off_t length)
  67. {
  68. long position = _tell(fd);
  69. if (position == -1)
  70. return Error::from_errno(errno);
  71. ScopeGuard restore_position { [&] { _lseek(fd, position, SEEK_SET); } };
  72. auto result = lseek(fd, length, SEEK_SET);
  73. if (result.is_error())
  74. return result.release_error();
  75. if (SetEndOfFile((HANDLE)_get_osfhandle(fd)) == 0)
  76. return Error::from_windows_error(GetLastError());
  77. return {};
  78. }
  79. ErrorOr<struct stat> fstat(int fd)
  80. {
  81. struct stat st = {};
  82. if (::fstat(fd, &st) < 0)
  83. return Error::from_syscall("fstat"sv, -errno);
  84. return st;
  85. }
  86. ErrorOr<void> ioctl(int, unsigned, ...)
  87. {
  88. dbgln("Core::System::ioctl() is not implemented");
  89. VERIFY_NOT_REACHED();
  90. }
  91. ErrorOr<ByteString> getcwd()
  92. {
  93. auto* cwd = _getcwd(nullptr, 0);
  94. if (!cwd)
  95. return Error::from_syscall("getcwd"sv, -errno);
  96. ByteString string_cwd(cwd);
  97. free(cwd);
  98. return string_cwd;
  99. }
  100. ErrorOr<struct stat> stat(StringView path)
  101. {
  102. if (path.is_null())
  103. return Error::from_syscall("stat"sv, -EFAULT);
  104. struct stat st = {};
  105. ByteString path_string = path;
  106. if (::stat(path_string.characters(), &st) < 0)
  107. return Error::from_syscall("stat"sv, -errno);
  108. return st;
  109. }
  110. ErrorOr<void> rmdir(StringView path)
  111. {
  112. if (path.is_null())
  113. return Error::from_errno(EFAULT);
  114. ByteString path_string = path;
  115. if (_rmdir(path_string.characters()) < 0)
  116. return Error::from_syscall("rmdir"sv, -errno);
  117. return {};
  118. }
  119. ErrorOr<void> unlink(StringView path)
  120. {
  121. if (path.is_null())
  122. return Error::from_errno(EFAULT);
  123. ByteString path_string = path;
  124. if (_unlink(path_string.characters()) < 0)
  125. return Error::from_syscall("unlink"sv, -errno);
  126. return {};
  127. }
  128. ErrorOr<void> mkdir(StringView path, mode_t)
  129. {
  130. ByteString str = path;
  131. if (_mkdir(str.characters()) < 0)
  132. return Error::from_syscall("mkdir"sv, -errno);
  133. return {};
  134. }
  135. ErrorOr<int> openat(int, StringView, int, mode_t)
  136. {
  137. dbgln("Core::System::openat() is not implemented");
  138. VERIFY_NOT_REACHED();
  139. }
  140. ErrorOr<struct stat> fstatat(int, StringView, int)
  141. {
  142. dbgln("Core::System::fstatat() is not implemented");
  143. VERIFY_NOT_REACHED();
  144. }
  145. ErrorOr<void*> mmap(void* address, size_t size, int protection, int flags, int fd, off_t offset, size_t alignment, StringView)
  146. {
  147. // custom alignment is not supported
  148. VERIFY(!alignment);
  149. void* ptr = ::mmap(address, size, protection, flags, fd, offset);
  150. if (ptr == MAP_FAILED)
  151. return Error::from_syscall("mmap"sv, -errno);
  152. return ptr;
  153. }
  154. ErrorOr<void> munmap(void* address, size_t size)
  155. {
  156. if (::munmap(address, size) < 0)
  157. return Error::from_syscall("munmap"sv, -errno);
  158. return {};
  159. }
  160. }