SystemWindows.cpp 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136
  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 <WinSock2.h>
  15. #include <io.h>
  16. namespace Core::System {
  17. ErrorOr<int> open(StringView path, int options, mode_t mode)
  18. {
  19. ByteString string_path = path;
  20. int rc = _open(string_path.characters(), options, mode);
  21. if (rc < 0)
  22. return Error::from_syscall("open"sv, -errno);
  23. return rc;
  24. }
  25. ErrorOr<void> close(int fd)
  26. {
  27. if (_close(fd) < 0)
  28. return Error::from_syscall("close"sv, -errno);
  29. return {};
  30. }
  31. ErrorOr<ssize_t> read(int fd, Bytes buffer)
  32. {
  33. int rc = _read(fd, buffer.data(), buffer.size());
  34. if (rc < 0)
  35. return Error::from_syscall("read"sv, -errno);
  36. return rc;
  37. }
  38. ErrorOr<ssize_t> write(int fd, ReadonlyBytes buffer)
  39. {
  40. int rc = _write(fd, buffer.data(), buffer.size());
  41. if (rc < 0)
  42. return Error::from_syscall("write"sv, -errno);
  43. return rc;
  44. }
  45. ErrorOr<off_t> lseek(int fd, off_t offset, int whence)
  46. {
  47. long rc = _lseek(fd, offset, whence);
  48. if (rc < 0)
  49. return Error::from_syscall("lseek"sv, -errno);
  50. return rc;
  51. }
  52. ErrorOr<void> ftruncate(int fd, off_t length)
  53. {
  54. long position = _tell(fd);
  55. if (position == -1)
  56. return Error::from_errno(errno);
  57. ScopeGuard restore_position { [&] { _lseek(fd, position, SEEK_SET); } };
  58. auto result = lseek(fd, length, SEEK_SET);
  59. if (result.is_error())
  60. return result.release_error();
  61. if (SetEndOfFile((HANDLE)_get_osfhandle(fd)) == 0)
  62. return Error::from_windows_error(GetLastError());
  63. return {};
  64. }
  65. ErrorOr<struct stat> fstat(int fd)
  66. {
  67. struct stat st = {};
  68. if (::fstat(fd, &st) < 0)
  69. return Error::from_syscall("fstat"sv, -errno);
  70. return st;
  71. }
  72. ErrorOr<void> ioctl(int, unsigned, ...)
  73. {
  74. dbgln("Core::System::ioctl() is not implemented");
  75. VERIFY_NOT_REACHED();
  76. }
  77. ErrorOr<ByteString> getcwd()
  78. {
  79. auto* cwd = _getcwd(nullptr, 0);
  80. if (!cwd)
  81. return Error::from_syscall("getcwd"sv, -errno);
  82. ByteString string_cwd(cwd);
  83. free(cwd);
  84. return string_cwd;
  85. }
  86. ErrorOr<struct stat> stat(StringView path)
  87. {
  88. if (path.is_null())
  89. return Error::from_syscall("stat"sv, -EFAULT);
  90. struct stat st = {};
  91. ByteString path_string = path;
  92. if (::stat(path_string.characters(), &st) < 0)
  93. return Error::from_syscall("stat"sv, -errno);
  94. return st;
  95. }
  96. ErrorOr<void> rmdir(StringView path)
  97. {
  98. if (path.is_null())
  99. return Error::from_errno(EFAULT);
  100. ByteString path_string = path;
  101. if (_rmdir(path_string.characters()) < 0)
  102. return Error::from_syscall("rmdir"sv, -errno);
  103. return {};
  104. }
  105. ErrorOr<void> unlink(StringView path)
  106. {
  107. if (path.is_null())
  108. return Error::from_errno(EFAULT);
  109. ByteString path_string = path;
  110. if (_unlink(path_string.characters()) < 0)
  111. return Error::from_syscall("unlink"sv, -errno);
  112. return {};
  113. }
  114. }