Repository navigation
Expand file tree
/
Copy pathDirectory.cpp
More file actions
302 lines (293 loc) · 12.6 KB
/
Copy pathDirectory.cpp
File metadata and controls
302 lines (293 loc) · 12.6 KB
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
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
#include "prx/libc/include/GuestDirectory.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/FilesystemError.hpp"
#include "prx/libc/include/AppArchive.hpp"
#include <dirent.h>
#include <unistd.h>
#include <cerrno>
#include <cstring>
#include <memory>
#include <mutex>
#include <stdexcept>
#include <string>
#ifdef _WIN32
#include "prx/libc/include/WindowsDescriptor.hpp"
#include <cstddef>
#include <string_view>
#endif
namespace {
int DirectoryError(int error) {
switch (error) {
case ENOENT: return 2;
case EACCES: return 13;
case ENOTDIR: return 20;
case EINVAL: return 22;
case EMFILE: return 24;
case ENFILE: return 23;
case ENOMEM: return 12;
case ENAMETOOLONG: return 63;
#ifdef ELOOP
case ELOOP: return 62;
#endif
default: return 5;
}
}
#ifdef _WIN32
struct NativeDirectory {
HANDLE handle = INVALID_HANDLE_VALUE;
std::uint32_t seed = 0;
bool identifiers = true;
bool restart = true;
bool buffered = false;
std::size_t next = 0;
long position = 0;
alignas(8) std::byte buffer[65536];
~NativeDirectory() { if (handle != INVALID_HANDLE_VALUE) CloseHandle(handle); }
};
int NativeOpenError(DWORD error) {
if (error == ERROR_CANT_RESOLVE_FILENAME) return ELOOP;
if (error == ERROR_DIRECTORY) return ENOTDIR;
return WindowsOpenError_nid_no_patch(error);
}
NativeDirectory* OpenNative(const std::filesystem::path& path) {
const DWORD attributes = GetFileAttributesW(path.c_str());
if (attributes == INVALID_FILE_ATTRIBUTES) { errno = NativeOpenError(GetLastError()); return nullptr; }
if ((attributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { errno = ENOTDIR; return nullptr; }
auto native = std::make_unique_for_overwrite<NativeDirectory>();
native->handle = CreateFileW(path.c_str(), FILE_LIST_DIRECTORY | SYNCHRONIZE, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE,
nullptr, OPEN_EXISTING, FILE_FLAG_BACKUP_SEMANTICS, nullptr);
if (native->handle == INVALID_HANDLE_VALUE) { errno = NativeOpenError(GetLastError()); return nullptr; }
BY_HANDLE_FILE_INFORMATION identity{};
if (GetFileInformationByHandle(native->handle, &identity)) {
if ((identity.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) == 0) { errno = ENOTDIR; return nullptr; }
native->seed = WindowsFileNumber_nid_no_patch(identity);
}
return native.release();
}
int CloseNative(NativeDirectory* native) { delete native; return 0; }
void RewindNative(NativeDirectory* native) {
native->restart = true;
native->buffered = false;
native->position = 0;
}
bool QueryNative(NativeDirectory* native) {
const auto query = [native](FILE_INFO_BY_HANDLE_CLASS restartClass, FILE_INFO_BY_HANDLE_CLASS nextClass) {
return GetFileInformationByHandleEx(native->handle, native->restart ? restartClass : nextClass, native->buffer, sizeof(native->buffer)) != 0;
};
bool filled = native->identifiers ? query(FileIdBothDirectoryRestartInfo, FileIdBothDirectoryInfo) : query(FileFullDirectoryRestartInfo, FileFullDirectoryInfo);
DWORD error = filled ? ERROR_SUCCESS : GetLastError();
if (!filled && native->restart && native->identifiers && (error == ERROR_INVALID_PARAMETER || error == ERROR_INVALID_FUNCTION ||
error == ERROR_INVALID_LEVEL || error == ERROR_NOT_SUPPORTED)) {
native->identifiers = false;
filled = query(FileFullDirectoryRestartInfo, FileFullDirectoryInfo);
error = filled ? ERROR_SUCCESS : GetLastError();
}
if (!filled) {
if (error != ERROR_NO_MORE_FILES && error != ERROR_FILE_NOT_FOUND) errno = EIO;
return false;
}
native->restart = false;
native->buffered = true;
native->next = 0;
return true;
}
const std::byte* NextRecord(NativeDirectory* native) {
if (!native->buffered && !QueryNative(native)) return nullptr;
const std::byte* record = native->buffer + native->next;
const DWORD offset = native->identifiers ? reinterpret_cast<const FILE_ID_BOTH_DIR_INFO*>(record)->NextEntryOffset
: reinterpret_cast<const FILE_FULL_DIR_INFO*>(record)->NextEntryOffset;
if (offset == 0) native->buffered = false;
native->next += offset;
++native->position;
return record;
}
std::uint32_t StableFileNumber(std::uint32_t seed, std::wstring_view name) {
std::uint32_t hash = 2166136261u ^ seed;
for (const wchar_t character : name) hash = (hash ^ static_cast<std::uint16_t>(character)) * 16777619u;
return hash == 0 ? 1 : hash;
}
long TellNative(NativeDirectory* native) { return native->position; }
void SeekNative(NativeDirectory* native, long location) {
if (location < 0) return;
RewindNative(native);
while (native->position < location && NextRecord(native)) {}
}
bool ReadNative(NativeDirectory* native, std::filesystem::path& component, std::string& name, std::uint64_t& inode) {
const std::byte* record = NextRecord(native);
if (!record) return false;
std::wstring_view fileName;
std::uint64_t fileId = 0;
if (native->identifiers) {
const auto* information = reinterpret_cast<const FILE_ID_BOTH_DIR_INFO*>(record);
fileName = std::wstring_view(information->FileName, information->FileNameLength / sizeof(WCHAR));
fileId = static_cast<std::uint64_t>(information->FileId.QuadPart);
} else {
const auto* information = reinterpret_cast<const FILE_FULL_DIR_INFO*>(record);
fileName = std::wstring_view(information->FileName, information->FileNameLength / sizeof(WCHAR));
}
component = fileName;
const auto utf8 = component.u8string();
name.assign(utf8.begin(), utf8.end());
inode = fileId != 0 ? WindowsFileNumber_nid_no_patch(fileId) : StableFileNumber(native->seed, fileName);
return true;
}
#else
using NativeDirectory = DIR;
NativeDirectory* OpenNative(const std::filesystem::path& path) { return ::opendir(path.c_str()); }
int CloseNative(NativeDirectory* native) { return ::closedir(native); }
void RewindNative(NativeDirectory* native) { ::rewinddir(native); }
long TellNative(NativeDirectory* native) { return ::telldir(native); }
void SeekNative(NativeDirectory* native, long location) { ::seekdir(native, location); }
bool ReadNative(NativeDirectory* native, std::filesystem::path& component, std::string& name, std::uint64_t& inode) {
auto* entry = ::readdir(native);
if (!entry) return false;
name = entry->d_name;
component = name;
inode = static_cast<std::uint64_t>(entry->d_ino);
return true;
}
#endif
struct Directory {
NativeDirectory* native = nullptr;
std::filesystem::path path;
GuestDirectoryEntry entry{};
std::mutex mutex;
int descriptor = -1;
int (APS5_VABI* closeDescriptor)(int) = nullptr;
~Directory() { if (native) CloseNative(native); }
};
std::uint8_t DirectoryType(const std::filesystem::path& path) {
std::error_code error;
switch (std::filesystem::symlink_status(path, error).type()) {
case std::filesystem::file_type::regular: return 8;
case std::filesystem::file_type::directory: return 4;
case std::filesystem::file_type::symlink: return 10;
case std::filesystem::file_type::block: return 6;
case std::filesystem::file_type::character: return 2;
case std::filesystem::file_type::fifo: return 1;
case std::filesystem::file_type::socket: return 12;
default: return 0;
}
}
GuestDirectoryEntry* ReadEntry(Directory& directory) {
const int savedError = errno;
errno = 0;
std::filesystem::path component;
std::string name;
std::uint64_t inode = 0;
try {
if (!ReadNative(directory.native, component, name, inode)) {
errno = errno ? DirectoryError(errno) : savedError;
return nullptr;
}
} catch (const std::bad_alloc&) { errno = 12; return nullptr; }
const auto length = name.size();
if (length > 255) { errno = 63; return nullptr; }
try {
directory.entry = {};
directory.entry.fileNumber = static_cast<std::uint32_t>(inode);
directory.entry.recordLength = static_cast<std::uint16_t>(8 + ((length + 1 + 3) & ~3));
directory.entry.nameLength = static_cast<std::uint8_t>(length);
directory.entry.type = DirectoryType(directory.path / component);
std::memcpy(directory.entry.name, name.c_str(), length + 1);
errno = savedError;
return &directory.entry;
} catch (const std::bad_alloc&) { errno = 12; return nullptr; }
catch (const std::filesystem::filesystem_error&) { errno = 5; return nullptr; }
}
}
extern "C" {
void* APS5_VABI opendir_nid_postfix(const char* path) {
if (!path) { errno = 14; return nullptr; }
if (!*path) { errno = 2; return nullptr; }
try {
auto directory = std::make_unique<Directory>();
if (const int error = ResolvePathError_nid_no_patch(path, directory->path)) { errno = error; return nullptr; }
AppArchiveRefuseMergedListing_nid_no_patch(directory->path, path);
directory->native = OpenNative(directory->path);
if (!directory->native) {
errno = DirectoryError(errno);
if (errno == 2) errno = NotFoundError_nid_no_patch(directory->path);
return nullptr;
}
return directory.release();
} catch (const std::bad_alloc&) { errno = 12; return nullptr; }
catch (const std::filesystem::filesystem_error&) { errno = 5; return nullptr; }
}
GuestDirectoryEntry* APS5_VABI readdir_nid_postfix(void* handle) {
if (!handle) { errno = 9; return nullptr; }
auto& directory = *static_cast<Directory*>(handle);
std::lock_guard lock(directory.mutex);
return ReadEntry(directory);
}
int APS5_VABI readdir_r_nid_postfix(void* handle, GuestDirectoryEntry* entry, GuestDirectoryEntry** result) {
if (!handle) return 9;
if (!entry || !result) throw std::invalid_argument("readdir_r: null entry or result");
auto& directory = *static_cast<Directory*>(handle);
const int savedError = errno;
errno = 0;
GuestDirectoryEntry* next = nullptr;
{
std::lock_guard lock(directory.mutex);
next = ReadEntry(directory);
if (next) std::memcpy(entry, next, next->recordLength);
}
if (!next && errno) return errno;
errno = savedError;
*result = next ? entry : nullptr;
return 0;
}
int APS5_VABI closedir_nid_postfix(void* handle) {
if (!handle) { errno = 9; return -1; }
std::unique_ptr<Directory> directory(static_cast<Directory*>(handle));
int result = CloseNative(directory->native);
directory->native = nullptr;
if (result) errno = DirectoryError(errno);
if (directory->closeDescriptor && directory->closeDescriptor(directory->descriptor) != 0) result = -1;
return result;
}
void APS5_VABI rewinddir_nid_postfix(void* handle) {
if (!handle) { errno = 9; return; }
auto& directory = *static_cast<Directory*>(handle);
std::lock_guard lock(directory.mutex);
RewindNative(directory.native);
}
std::int64_t APS5_VABI telldir_nid_postfix(void* handle) {
if (!handle) { errno = 9; return -1; }
auto& directory = *static_cast<Directory*>(handle);
std::lock_guard lock(directory.mutex);
return TellNative(directory.native);
}
void APS5_VABI seekdir_nid_postfix(void* handle, std::int64_t location) {
if (!handle) { errno = 9; return; }
auto& directory = *static_cast<Directory*>(handle);
std::lock_guard lock(directory.mutex);
SeekNative(directory.native, static_cast<long>(location));
}
void* APS5_VABI OpenDescriptorDirectory_nid_no_patch(const char* hostPath, int descriptor, int (APS5_VABI* closeDescriptor)(int), std::uint64_t position) {
try {
auto directory = std::make_unique<Directory>();
directory->path = std::filesystem::path(reinterpret_cast<const char8_t*>(hostPath));
#ifdef _WIN32
directory->native = OpenNative(directory->path);
if (!directory->native) { errno = DirectoryError(errno); return nullptr; }
std::filesystem::path component;
std::string name;
std::uint64_t inode = 0;
for (std::uint64_t skipped = 0; skipped < position; ++skipped) {
if (!ReadNative(directory->native, component, name, inode)) break;
}
#else
(void)position;
const int duplicate = ::dup(descriptor);
if (duplicate < 0) { errno = DirectoryError(errno); return nullptr; }
directory->native = ::fdopendir(duplicate);
if (!directory->native) { const int error = errno; ::close(duplicate); errno = DirectoryError(error); return nullptr; }
#endif
directory->descriptor = descriptor;
directory->closeDescriptor = closeDescriptor;
return directory.release();
} catch (const std::bad_alloc&) { errno = 12; return nullptr; }
catch (const std::filesystem::filesystem_error&) { errno = 5; return nullptr; }
}
}