Echo Writes Code

fs.cpp

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
#include "crucible/windows/fs.hpp"

#include "crucible/core/resource_warden.inl"
#include "crucible/windows/transcoding.hpp"

#include <Windows.h>

#include <limits>
#include <type_traits>

namespace crucible::windows::fs {
  struct CloseHandleAdapter {
    auto operator()(::HANDLE file_handle) const -> void {
      ::CloseHandle(file_handle);
    }
  };

  using FileHandleWarden = core::resource_warden::ResourceWarden<::HANDLE, CloseHandleAdapter>;

  struct FindCloseAdapter {
    auto operator()(::HANDLE find_handle) const -> void {
      ::FindClose(find_handle);
    }
  };

  using FindHandleWarden = core::resource_warden::ResourceWarden<::HANDLE, FindCloseAdapter>;

  [[nodiscard]]
  auto read_file_size_from_file_handle(::HANDLE file_handle) -> errors::WindowsResult<std::size_t> {
    std::size_t file_size { 0 };

    {
      ::LARGE_INTEGER raw_size {};
      bool const succeeded { ::GetFileSizeEx(file_handle, &raw_size) != 0 };
      if (!succeeded) {
        return core::either::make_lhs<errors::WindowsError>(errors::GetFileSizeExFailed {});
      }

      static_assert(sizeof(raw_size.QuadPart) >= sizeof(std::uint64_t));
      static_assert(sizeof(raw_size.QuadPart) <= sizeof(file_size));

      if (raw_size.QuadPart < 0) {
        return core::either::make_lhs<errors::WindowsError>(errors::NegativeFileSize {});
      }

      file_size = raw_size.QuadPart;
    }

    return core::either::make_rhs(file_size);
  }

  auto create_directory(std::wstring const &path) -> errors::WindowsResult<core::none::None> {
    bool const succeeded { ::CreateDirectoryW(path.data(), nullptr) != 0 };
    if (!succeeded) {
      return core::either::make_lhs<errors::WindowsError>(errors::CreateDirectoryWFailed {});
    }

    return core::either::make_rhs<core::none::None>();
  }

  auto create_file(std::wstring const &path, std::vector<std::byte> const &contents) -> errors::WindowsResult<core::none::None> {
    ::HANDLE file_handle { ::CreateFileW(path.data(), GENERIC_WRITE, FILE_SHARE_WRITE, nullptr, CREATE_NEW, FILE_ATTRIBUTE_NORMAL, nullptr) };
    if (file_handle == INVALID_HANDLE_VALUE) {
      return core::either::make_lhs<errors::WindowsError>(errors::CreateFileWFailed {});
    }

    FileHandleWarden warden { file_handle };

    std::size_t total_bytes_written { 0 };

    do {
      constexpr ::DWORD DWORD_MAX { std::numeric_limits<::DWORD>::max() };
      std::size_t const remaining_size { contents.size() - total_bytes_written };

      ::DWORD const intended_write_size { remaining_size >= DWORD_MAX ? DWORD_MAX : static_cast<::DWORD>(remaining_size) };
      ::DWORD bytes_actually_written { 0 };

      bool const succeeded { ::WriteFile(file_handle, contents.data() + total_bytes_written, intended_write_size, &bytes_actually_written, nullptr) != 0 };
      if (!succeeded) {
        break;
      }

      total_bytes_written += bytes_actually_written;
    } while (total_bytes_written < contents.size());

    if (total_bytes_written < contents.size()) {
      return core::either::make_lhs<errors::WindowsError>(errors::WriteFileFailed {});
    }

    return core::either::make_rhs<core::none::None>();
  }

  auto delete_directory(std::wstring const &path) -> errors::WindowsResult<core::none::None> {
    bool const succeeded { ::RemoveDirectoryW(path.data()) != 0 };
    if (!succeeded) {
      return core::either::make_lhs<errors::WindowsError>(errors::RemoveDirectoryWFailed {});
    }

    return core::either::make_rhs<core::none::None>();
  }

  auto delete_file(std::wstring const &path) -> errors::WindowsResult<core::none::None> {
    bool const succeeded { ::DeleteFileW(path.data()) != 0 };
    if (!succeeded) {
      return core::either::make_lhs<errors::WindowsError>(errors::DeleteFileWFailed {});
    }

    return core::either::make_rhs<core::none::None>();
  }

  auto is_directory(std::wstring const &path) -> errors::WindowsResult<bool> {
    ::DWORD const attributes { ::GetFileAttributesW(path.data()) };
    if (attributes == INVALID_FILE_ATTRIBUTES) {
      return core::either::make_lhs<errors::WindowsError>(errors::GetFileAttributesWFailed {});
    }

    return core::either::make_rhs((attributes & FILE_ATTRIBUTE_DIRECTORY) == FILE_ATTRIBUTE_DIRECTORY);
  }

  auto is_file(std::wstring const &path) -> errors::WindowsResult<bool> {
    auto const result { is_directory(path) };
    return result.map_rhs(std::logical_not<bool> {});
  }

  auto read_directory_list(std::wstring const &path) -> errors::WindowsResult<std::vector<std::wstring>> {
    auto const wildcard { path + L"\\*" };

    ::WIN32_FIND_DATAW find_data {};
    ::HANDLE find_handle { ::FindFirstFileW(wildcard.data(), &find_data) };
    if (find_handle == INVALID_HANDLE_VALUE) {
      return core::either::make_lhs<errors::WindowsError>(errors::FindFirstFileWFailed {});
    }

    FindHandleWarden warden { find_handle };

    std::vector<std::wstring> items;

    do {
      std::wstring const filename { find_data.cFileName };

      bool const is_dot { filename == L"." };
      bool const is_dot_dot { filename == L".." };

      if (!is_dot && !is_dot_dot) {
        items.emplace_back(find_data.cFileName);
      }
    } while (::FindNextFileW(find_handle, &find_data) != 0);

    if (::GetLastError() != ERROR_NO_MORE_FILES) {
      return core::either::make_lhs<errors::WindowsError>(errors::FindNextFileWFailed {});
    }

    return core::either::make_rhs(std::move(items));
  }

  auto read_file_data(std::wstring const &path) -> errors::WindowsResult<std::vector<std::byte>> {
    ::HANDLE file_handle { ::CreateFileW(path.data(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr) };
    if (file_handle == INVALID_HANDLE_VALUE) {
      return core::either::make_lhs<errors::WindowsError>(errors::CreateFileWFailed {});
    }

    FileHandleWarden warden { file_handle };

    auto const read_file_size_result { read_file_size_from_file_handle(file_handle) };
    if (read_file_size_result.has_lhs()) {
      return read_file_size_result.forward_lhs();
    }

    std::size_t const file_size { read_file_size_result.get_rhs() };
    std::vector<std::byte> buffer(file_size, std::byte(0x00));

    std::size_t total_bytes_read { 0 };

    do {
      constexpr ::DWORD DWORD_MAX { std::numeric_limits<::DWORD>::max() };
      std::size_t const remaining_size { file_size - total_bytes_read };

      ::DWORD const intended_read_size { remaining_size >= DWORD_MAX ? DWORD_MAX : static_cast<::DWORD>(remaining_size) };
      ::DWORD bytes_actually_read { 0 };

      bool const succeeded { ::ReadFile(file_handle, buffer.data(), intended_read_size, &bytes_actually_read, nullptr) != 0 };
      if (!succeeded) {
        break;
      }

      total_bytes_read += bytes_actually_read;
    } while (total_bytes_read < file_size);

    if (total_bytes_read < file_size) {
      return core::either::make_lhs<errors::WindowsError>(errors::ReadFileFailed {});
    }

    return core::either::make_rhs(std::move(buffer));
  }

  auto read_file_size(std::wstring const &path) -> errors::WindowsResult<std::size_t> {
    ::HANDLE file_handle { ::CreateFileW(path.data(), GENERIC_READ, FILE_SHARE_READ, nullptr, OPEN_EXISTING, FILE_ATTRIBUTE_NORMAL, nullptr) };
    if (file_handle == INVALID_HANDLE_VALUE) {
      return core::either::make_lhs<errors::WindowsError>(errors::CreateFileWFailed {});
    }

    FileHandleWarden warden(file_handle);
    return read_file_size_from_file_handle(file_handle);
  }
}