This repository was archived by the owner on Oct 28, 2026. It is now read-only.
Repository navigation
Expand file tree
/
Copy pathExport.cpp
More file actions
639 lines (596 loc) · 29 KB
/
Copy pathExport.cpp
File metadata and controls
639 lines (596 loc) · 29 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
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
#include <algorithm>
#include <cstdarg>
#include <cstdint>
#include <cstddef>
#include <cstring>
#include <filesystem>
#include <limits>
#include <stdexcept>
#include <string>
#include <string_view>
#include <system_error>
#include <sys/stat.h>
#ifdef __linux__
#include <cerrno>
#include <fcntl.h>
#include <sys/stat.h>
#include <unistd.h>
#endif
#include "SceTypes.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/FilesystemError.hpp"
#include "prx/libkernel/File/include/DirectoryDescriptor.hpp"
#include "prx/libc/include/CreationMask.hpp"
#include "prx/libkernel/File/include/FileFlags.hpp"
#ifdef _WIN32
#ifndef NOMINMAX
#define NOMINMAX
#endif
#include <windows.h>
#include <io.h>
#include <sys/stat.h>
#include "prx/libkernel/File/include/WindowsSymbolicLink.hpp"
#ifndef SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE
#define SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE 0x2
#endif
#endif
extern "C" int* APS5_VABI __error_nid_postfix();
namespace {
constexpr int GuestEnoent = 2;
constexpr int GuestEperm = 1;
constexpr int GuestEio = 5;
constexpr int GuestEfault = 14;
constexpr int GuestEnotdir = 20;
constexpr int GuestEisdir = 21;
constexpr int GuestEinval = 22;
constexpr int GuestEexist = 17;
constexpr int GuestExdev = 18;
constexpr int GuestEdeadlk = 11;
constexpr int GuestEagain = 35;
constexpr int GuestEloop = 62;
constexpr int GuestEnametoolong = 63;
constexpr int GuestEnotempty = 66;
constexpr int GuestEoverflow = 84;
constexpr int GuestAtFdcwd = -100;
constexpr int GuestAtRemovedir = 0x800;
constexpr std::size_t GuestPathMax = 1024;
constexpr std::size_t GuestNameMax = 255;
constexpr int PcLinkMax = 1;
constexpr int PcMaxCanon = 2;
constexpr int PcMaxInput = 3;
constexpr int PcNameMax = 4;
constexpr int PcPathMax = 5;
constexpr int PcPipeBuf = 6;
constexpr int PcChownRestricted = 7;
constexpr int PcNoTrunc = 8;
constexpr int PcVdisable = 9;
constexpr int PcAllocSizeMin = 10;
constexpr int PcFileSizeBits = 12;
constexpr int PcRecIncrXferSize = 14;
constexpr int PcRecMaxXferSize = 15;
constexpr int PcRecMinXferSize = 16;
constexpr int PcRecXferAlign = 17;
constexpr int PcSymlinkMax = 18;
constexpr int PcMinHoleSize = 21;
constexpr int PcAsyncIo = 53;
constexpr int PcPrioIo = 54;
constexpr int PcSyncIo = 55;
constexpr int PcAclExtended = 59;
constexpr int PcAclPathMax = 60;
constexpr int PcMacPresent = 63;
constexpr int PcAclNfs4 = 64;
constexpr int GuestAtSymlinkNofollow = 0x0200;
int GuestFileError(int error) {
switch (error) {
case ENOENT: return GuestEnoent;
case EACCES: return 13;
case ENOTDIR: return 20;
case EINVAL: return GuestEinval;
case EPERM: return GuestEperm;
case EROFS: return 30;
case ENAMETOOLONG: return GuestEnametoolong;
case EIO: return GuestEio;
#ifdef ELOOP
case ELOOP: return GuestEloop;
#endif
default: return GuestEio;
}
}
std::int64_t Fail(int error) {
*__error_nid_postfix() = error;
return -1;
}
int GuestError(const std::error_code& error) {
const auto condition = error.default_error_condition();
if (condition == std::errc::resource_unavailable_try_again) return GuestEagain;
if (condition == std::errc::resource_deadlock_would_occur) return GuestEdeadlk;
if (condition == std::errc::too_many_symbolic_link_levels) return GuestEloop;
if (condition == std::errc::filename_too_long) return GuestEnametoolong;
if (condition == std::errc::directory_not_empty) return GuestEnotempty;
if (condition == std::errc::value_too_large) return GuestEoverflow;
const int value = condition.value();
return value > 0 && value <= 34 ? value : GuestEio;
}
std::int64_t CopyLinkTarget(const std::string& target, char* buffer, std::size_t size) {
const auto count = std::min(size, target.size());
if (count != 0 && buffer == nullptr) return Fail(GuestEfault);
if (count != 0) std::memcpy(buffer, target.data(), count);
return static_cast<std::int64_t>(count);
}
std::int64_t ReadNativeLink(const std::filesystem::path& native, char* buffer, std::size_t size) {
#ifdef _WIN32
const DWORD attributes = GetFileAttributesW(native.wstring().c_str());
if (attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0) {
std::string target;
if (!File::WindowsSymbolicLink::ReadRelative(native, target, "readlink")) {
NotImplemented_nid_no_patch("readlink of a Windows reparse point other than a relative symbolic link");
return -1;
}
return CopyLinkTarget(target, buffer, size);
}
#endif
std::error_code error;
const auto status = std::filesystem::symlink_status(native, error);
if (error) return Fail(GuestError(error));
if (status.type() == std::filesystem::file_type::not_found) return Fail(GuestEnoent);
if (!std::filesystem::is_symlink(status)) return Fail(GuestEinval);
const auto target = std::filesystem::read_symlink(native, error).generic_string();
if (error) return Fail(GuestError(error));
return CopyLinkTarget(target, buffer, size);
}
bool HasLongComponent(std::string_view path) {
for (std::size_t start = 0; start < path.size();) {
const auto end = std::min(path.find('/', start), path.size());
if (end - start > GuestNameMax) {
if (start == 0) return true;
std::filesystem::path prefix;
if (ResolvePathError_nid_no_patch(std::string(path.substr(0, start)).c_str(), prefix)) return false;
std::error_code error;
return std::filesystem::is_directory(prefix, error);
}
start = end + 1;
}
return false;
}
bool HasNonDirectoryPrefix(const std::filesystem::path& native) {
std::error_code error;
for (auto prefix = native.parent_path(); prefix.has_relative_path(); prefix = prefix.parent_path()) {
const auto status = std::filesystem::status(prefix, error);
if (std::filesystem::exists(status)) return !std::filesystem::is_directory(status);
}
return false;
}
bool IsDotOrDotDot(std::string_view path) {
while (!path.empty() && (path.back() == '/' || path.back() == '\\')) path.remove_suffix(1);
const auto separator = path.find_last_of("/\\");
const auto component = path.substr(separator == std::string_view::npos ? 0 : separator + 1);
return component == "." || component == "..";
}
int RenamePath(const std::filesystem::path& source, const std::filesystem::path& destination) {
namespace fs = std::filesystem;
std::error_code error;
const auto sourceStatus = fs::symlink_status(source, error);
if (error && error != std::errc::no_such_file_or_directory) return FilesystemError(error);
if (sourceStatus.type() == fs::file_type::not_found)
return HasNonDirectoryPrefix(source) ? GuestEnotdir : GuestEnoent;
const auto parent = destination.parent_path().empty() ? fs::path(".") : destination.parent_path();
error.clear();
const auto parentStatus = fs::status(parent, error);
if (error) return FilesystemError(error);
if (!fs::exists(parentStatus))
return HasNonDirectoryPrefix(destination) ? GuestEnotdir : GuestEnoent;
if (!fs::is_directory(parentStatus)) return GuestEnotdir;
error.clear();
const auto destinationStatus = fs::symlink_status(destination, error);
if (error && error != std::errc::no_such_file_or_directory) return FilesystemError(error);
const bool destinationExists = destinationStatus.type() != fs::file_type::not_found;
const bool sourceIsDirectory = fs::is_directory(sourceStatus);
const bool destinationIsDirectory = destinationExists && fs::is_directory(destinationStatus);
bool sameFile = false;
if (destinationExists) {
error.clear();
sameFile = fs::equivalent(source, destination, error);
if (error == std::errc::no_such_file_or_directory) error.clear();
if (error) return FilesystemError(error);
if (!sameFile && sourceIsDirectory && !destinationIsDirectory) return GuestEnotdir;
if (!sameFile && !sourceIsDirectory && destinationIsDirectory) return GuestEisdir;
}
if (sourceIsDirectory && !sameFile) {
error.clear();
const auto canonicalSource = fs::weakly_canonical(source, error);
if (error) return FilesystemError(error);
const auto canonicalDestination = fs::weakly_canonical(destination, error);
if (error) return FilesystemError(error);
const auto relative = canonicalDestination.lexically_relative(canonicalSource);
if (!relative.empty() && *relative.begin() != "..") return GuestEinval;
}
if (destinationExists && !sameFile && destinationIsDirectory) {
error.clear();
if (!fs::is_empty(destination, error)) {
return error ? FilesystemError(error) : GuestEnotempty;
}
}
#ifdef _WIN32
if (!sameFile && sourceIsDirectory && destinationIsDirectory) {
error.clear();
if (!fs::remove(destination, error)) return error ? FilesystemError(error) : GuestEio;
}
#endif
error.clear();
fs::rename(source, destination, error);
return error ? FilesystemError(error) : 0;
}
}
extern "C" {
int APS5_VABI fork_nid_postfix(void) {
NotImplemented_nid_no_patch("fork: child processes");
return -1;
}
int APS5_VABI setsid_nid_postfix(void) {
return static_cast<int>(Fail(GuestEperm));
}
std::uint16_t APS5_VABI umask_nid_postfix(std::uint16_t mask) {
return File::ExchangeCreationMask_nid_no_patch(mask);
}
int APS5_VABI _fstatfs_nid_postfix(int d, void* buf);
int APS5_VABI fstatfs_nid_postfix(int d, void* buf) {
return _fstatfs_nid_postfix(d, buf);
}
std::int64_t APS5_VABI readlink_nid_postfix(const char* path, char* buffer, std::size_t size) {
if (path == nullptr) return Fail(GuestEfault);
if (*path == '\0') return Fail(GuestEnoent);
if (size > static_cast<std::size_t>(std::numeric_limits<std::int64_t>::max())) return Fail(GuestEinval);
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return Fail(error);
return ReadNativeLink(native, buffer, size);
}
std::int64_t APS5_VABI readlinkat_nid_postfix(int fd, const char* path, char* buffer, std::size_t size) {
if (path == nullptr) return Fail(GuestEfault);
if (*path == '\0') return Fail(GuestEnoent);
if (size > static_cast<std::size_t>(std::numeric_limits<std::int64_t>::max())) return Fail(GuestEinval);
if (fd == GuestAtFdcwd || *path == '/') return readlink_nid_postfix(path, buffer, size);
int error = 0;
const char* base = GetDirectoryDescriptorPath_nid_no_patch(fd, &error);
if (base == nullptr) return Fail(error);
return ReadNativeLink(std::filesystem::path(reinterpret_cast<const char8_t*>(base)) /
std::filesystem::path(reinterpret_cast<const char8_t*>(path)), buffer, size);
}
std::int64_t APS5_VABI pathconf_nid_postfix(const char* path, int name) {
if (path == nullptr) return Fail(GuestEfault);
if (*path == '\0') return Fail(GuestEnoent);
if (std::strlen(path) >= GuestPathMax || HasLongComponent(path)) return Fail(GuestEnametoolong);
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return Fail(error);
std::error_code error;
const auto status = std::filesystem::status(native, error);
if (error) return Fail(GuestError(error));
if (!std::filesystem::exists(status)) return Fail(GuestEnoent);
switch (name) {
case PcLinkMax: return 32767;
case PcNameMax: return static_cast<std::int64_t>(GuestNameMax);
case PcPathMax: return static_cast<std::int64_t>(GuestPathMax);
case PcPipeBuf: return 512;
case PcChownRestricted: return 1;
case PcNoTrunc: return 1;
case PcFileSizeBits: return 64;
case PcRecMaxXferSize: return -1;
case PcSymlinkMax: return static_cast<std::int64_t>(GuestPathMax);
case PcPrioIo: return 0;
case PcSyncIo: return 0;
case PcMaxCanon:
case PcMaxInput:
case PcVdisable:
case PcAllocSizeMin:
case PcRecIncrXferSize:
case PcRecMinXferSize:
case PcRecXferAlign:
case PcMinHoleSize:
case PcAsyncIo:
case PcAclExtended:
case PcAclPathMax:
case PcMacPresent:
case PcAclNfs4:
throw std::runtime_error("pathconf: name " + std::to_string(name) + " depends on the console filesystem or kernel configuration");
default: return Fail(GuestEinval);
}
}
int APS5_VABI link_nid_postfix(const char* existing, const char* created) {
if (existing == nullptr || created == nullptr) return static_cast<int>(Fail(GuestEfault));
if (*existing == '\0' || *created == '\0') return static_cast<int>(Fail(GuestEnoent));
std::filesystem::path source, destination;
if (const int error = ResolvePathError_nid_no_patch(existing, source)) return static_cast<int>(Fail(error));
if (const int error = ResolvePathError_nid_no_patch(created, destination)) return static_cast<int>(Fail(error));
std::error_code error;
const auto status = std::filesystem::status(source, error);
if (!std::filesystem::exists(status)) return static_cast<int>(Fail(GuestEnoent));
if (std::filesystem::is_directory(status)) return static_cast<int>(Fail(GuestEperm));
if (std::filesystem::exists(std::filesystem::symlink_status(destination, error))) return static_cast<int>(Fail(GuestEexist));
if (!std::filesystem::is_directory(destination.parent_path(), error)) return static_cast<int>(Fail(GuestEnoent));
const auto target = std::filesystem::is_symlink(source, error) ? std::filesystem::canonical(source, error) : source;
if (!error) std::filesystem::create_hard_link(target, destination, error);
if (error == std::errc::cross_device_link) return static_cast<int>(Fail(GuestExdev));
if (error) throw std::system_error(error, "link");
RecordWrittenPath_nid_no_patch(destination);
return 0;
}
int APS5_VABI fchdir_nid_postfix(int fd) {
int error = 0;
const char* path = GetDirectoryDescriptorPath_nid_no_patch(fd, &error);
if (path == nullptr) return static_cast<int>(Fail(error));
#ifdef __linux__
if (::faccessat(fd, ".", X_OK, AT_EACCESS) != 0) return static_cast<int>(Fail(errno));
#endif
error = SetWorkingDirectory_nid_no_patch(path);
if (error != 0) return static_cast<int>(Fail(error));
return 0;
}
int APS5_VABI chroot_nid_postfix(const char* path) {
if (path == nullptr) return static_cast<int>(Fail(GuestEfault));
if (*path == '\0') return static_cast<int>(Fail(GuestEnoent));
if (std::strlen(path) >= GuestPathMax || HasLongComponent(path)) return static_cast<int>(Fail(GuestEnametoolong));
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return static_cast<int>(Fail(error));
std::error_code error;
const auto status = std::filesystem::status(native, error);
if (status.type() == std::filesystem::file_type::not_found) return static_cast<int>(Fail(HasNonDirectoryPrefix(native) ? GuestEnotdir : GuestEnoent));
if (error) return static_cast<int>(Fail(GuestError(error)));
if (!std::filesystem::is_directory(status)) return static_cast<int>(Fail(GuestEnotdir));
const bool root = std::filesystem::equivalent(native, ResolvePath_nid_no_patch("/"), error);
if (error) return static_cast<int>(Fail(GuestError(error)));
if (root) return 0;
throw std::runtime_error(std::string("chroot: changing the root directory to ") + path + " is not supported, the guest path resolver keeps a fixed root");
}
int APS5_VABI symlink_nid_postfix(const char* target, const char* created) {
if (target == nullptr || created == nullptr) return static_cast<int>(Fail(GuestEfault));
if (*target == '\0' || *created == '\0') return static_cast<int>(Fail(GuestEnoent));
if (*target == '/') {
NotImplemented_nid_no_patch("symlink: absolute targets");
return -1;
}
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(created, native)) return static_cast<int>(Fail(error));
std::error_code error;
#ifdef _WIN32
if (GetFileAttributesW(native.wstring().c_str()) != INVALID_FILE_ATTRIBUTES) return static_cast<int>(Fail(GuestEexist));
#else
if (std::filesystem::exists(std::filesystem::symlink_status(native, error))) return static_cast<int>(Fail(GuestEexist));
#endif
if (!std::filesystem::is_directory(native.parent_path(), error)) return static_cast<int>(Fail(GuestEnoent));
const std::filesystem::path contents(target);
const bool directory = std::filesystem::is_directory(native.parent_path() / contents, error);
#ifdef _WIN32
DWORD flags = SYMBOLIC_LINK_FLAG_ALLOW_UNPRIVILEGED_CREATE;
if (directory) flags |= SYMBOLIC_LINK_FLAG_DIRECTORY;
if (!CreateSymbolicLinkW(native.wstring().c_str(), std::filesystem::path(contents).make_preferred().wstring().c_str(), flags)) {
const DWORD code = GetLastError();
if (code == ERROR_PRIVILEGE_NOT_HELD) return static_cast<int>(Fail(GuestEperm));
if (code == ERROR_ALREADY_EXISTS || code == ERROR_FILE_EXISTS) return static_cast<int>(Fail(GuestEexist));
if (code == ERROR_PATH_NOT_FOUND) return static_cast<int>(Fail(GuestEnoent));
throw std::system_error(static_cast<int>(code), std::system_category(), "symlink");
}
#else
if (directory)
std::filesystem::create_directory_symlink(contents, native, error);
else
std::filesystem::create_symlink(contents, native, error);
if (error) throw std::system_error(error, "symlink");
#endif
RecordWrittenPath_nid_no_patch(native);
return 0;
}
int APS5_VABI open_nid_postfix(const char* path, int flags, int mode);
int APS5_VABI openat_nid_postfix(int fd, const char* path, int flags, ...) {
int mode = 0;
if (flags & SCE_KERNEL_O_CREAT) {
#ifdef _WIN32
__builtin_sysv_va_list arguments;
__builtin_sysv_va_start(arguments, flags);
mode = __builtin_va_arg(arguments, int);
__builtin_sysv_va_end(arguments);
#else
std::va_list arguments;
va_start(arguments, flags);
mode = va_arg(arguments, int);
va_end(arguments);
#endif
}
if (path == nullptr) return static_cast<int>(Fail(GuestEfault));
if (*path == '\0') return static_cast<int>(Fail(GuestEnoent));
if (fd == GuestAtFdcwd || *path == '/') return open_nid_postfix(path, flags, mode);
const int result = OpenInDirectory_nid_no_patch(fd, path, flags, static_cast<std::uint16_t>(mode));
if (result < 0) return static_cast<int>(Fail(result & 0xffff));
return result;
}
int APS5_VABI mkdir_nid_postfix(const char* path, std::uint16_t mode);
int APS5_VABI mkdirat_nid_postfix(int fd, const char* path, std::uint16_t mode) {
if (path == nullptr) return static_cast<int>(Fail(GuestEfault));
if (*path == '\0') return static_cast<int>(Fail(GuestEnoent));
if (fd == GuestAtFdcwd || *path == '/') return mkdir_nid_postfix(path, mode);
if (std::strlen(path) >= GuestPathMax || HasLongComponent(path)) return static_cast<int>(Fail(GuestEnametoolong));
int error = 0;
const char* base = GetDirectoryDescriptorPath_nid_no_patch(fd, &error);
if (base == nullptr) return static_cast<int>(Fail(error));
auto native = std::filesystem::path(reinterpret_cast<const char8_t*>(base)) /
std::filesystem::path(reinterpret_cast<const char8_t*>(path));
while (!native.has_filename() && native.has_relative_path()) native = native.parent_path();
#ifdef _WIN32
if (!CreateDirectoryW(native.wstring().c_str(), nullptr)) {
const DWORD code = GetLastError();
if (code == ERROR_ALREADY_EXISTS || code == ERROR_FILE_EXISTS) return static_cast<int>(Fail(GuestEexist));
if (code == ERROR_ACCESS_DENIED) return static_cast<int>(Fail(GuestEperm));
if (code == ERROR_FILENAME_EXCED_RANGE) return static_cast<int>(Fail(GuestEnametoolong));
if (code == ERROR_PATH_NOT_FOUND) {
if (HasNonDirectoryPrefix(native)) return static_cast<int>(Fail(GuestEnotdir));
return static_cast<int>(Fail(GuestEnoent));
}
return static_cast<int>(Fail(GuestEio));
}
#else
if (::mkdir(native.c_str(), static_cast<mode_t>(mode & 0777)) != 0) {
const int nativeError = errno;
if ((nativeError == ENOENT || nativeError == ENOTDIR) && HasNonDirectoryPrefix(native))
return static_cast<int>(Fail(GuestEnotdir));
return static_cast<int>(Fail(GuestError(std::error_code(nativeError, std::generic_category()))));
}
#endif
RecordWrittenPath_nid_no_patch(native);
return 0;
}
int APS5_VABI fchmodat_nid_postfix(int fd, const char* path, int mode, int flag) {
if (flag != 0 && flag != GuestAtSymlinkNofollow) return static_cast<int>(Fail(GuestEinval));
if (path == nullptr) return static_cast<int>(Fail(GuestEfault));
if (*path == '\0') return static_cast<int>(Fail(GuestEnoent));
std::filesystem::path native;
if (fd == GuestAtFdcwd || *path == '/') {
if (const int error = ResolvePathError_nid_no_patch(path, native)) return static_cast<int>(Fail(error));
} else {
int error = 0;
const char* base = GetDirectoryDescriptorPath_nid_no_patch(fd, &error);
if (base == nullptr) return static_cast<int>(Fail(error));
native = std::filesystem::path(reinterpret_cast<const char8_t*>(base)) / std::filesystem::path(reinterpret_cast<const char8_t*>(path));
}
if (flag == GuestAtSymlinkNofollow) {
#ifdef _WIN32
const DWORD attributes = GetFileAttributesW(native.wstring().c_str());
const bool symbolic = attributes != INVALID_FILE_ATTRIBUTES && (attributes & FILE_ATTRIBUTE_REPARSE_POINT) != 0;
#else
std::error_code status;
const bool symbolic = std::filesystem::is_symlink(std::filesystem::symlink_status(native, status));
#endif
if (symbolic) {
NotImplemented_nid_no_patch("fchmodat: AT_SYMLINK_NOFOLLOW on a symbolic link");
return -1;
}
}
#ifdef _WIN32
if (::_wchmod(native.wstring().c_str(), mode & 07777) != 0) return static_cast<int>(Fail(GuestFileError(errno)));
#else
if (::chmod(native.c_str(), static_cast<mode_t>(mode & 07777)) != 0) return static_cast<int>(Fail(GuestFileError(errno)));
#endif
RecordWrittenPath_nid_no_patch(native);
return 0;
}
int APS5_VABI unlink_nid_postfix(const char* path);
int APS5_VABI rmdir_nid_postfix(const char* path);
int APS5_VABI unlinkat_nid_postfix(int fd, const char* path, int flag) {
if (flag & ~GuestAtRemovedir) return static_cast<int>(Fail(GuestEinval));
if (path == nullptr) return static_cast<int>(Fail(GuestEfault));
if (*path == '\0') return static_cast<int>(Fail(GuestEnoent));
if (fd == GuestAtFdcwd || *path == '/') return (flag & GuestAtRemovedir) ? rmdir_nid_postfix(path) : unlink_nid_postfix(path);
if (std::strlen(path) >= GuestPathMax || HasLongComponent(path)) return static_cast<int>(Fail(GuestEnametoolong));
int error = 0;
const char* base = GetDirectoryDescriptorPath_nid_no_patch(fd, &error);
if (base == nullptr) return static_cast<int>(Fail(error));
auto native = std::filesystem::path(reinterpret_cast<const char8_t*>(base)) /
std::filesystem::path(reinterpret_cast<const char8_t*>(path));
const bool trailingSlash = !native.has_filename() && native.has_relative_path();
while (!native.has_filename() && native.has_relative_path()) native = native.parent_path();
std::error_code statusError;
const auto symlinkStatus = std::filesystem::symlink_status(native, statusError);
if (flag & GuestAtRemovedir) {
if (IsDotOrDotDot(path)) return static_cast<int>(Fail(GuestEinval));
if (std::filesystem::is_symlink(symlinkStatus)) return static_cast<int>(Fail(GuestEnotdir));
if (!std::filesystem::is_directory(symlinkStatus)) {
if (HasNonDirectoryPrefix(native) || std::filesystem::exists(symlinkStatus))
return static_cast<int>(Fail(GuestEnotdir));
return static_cast<int>(Fail(GuestEnoent));
}
if (!std::filesystem::is_empty(native, statusError)) return static_cast<int>(Fail(GuestEnotempty));
#ifdef _WIN32
struct _stat64 status{};
const bool unlocked = ::_wstat64(native.c_str(), &status) == 0 && !(status.st_mode & _S_IWRITE) &&
::_wchmod(native.c_str(), _S_IREAD | _S_IWRITE) == 0;
if (!std::filesystem::remove(native, statusError)) {
if (unlocked) ::_wchmod(native.c_str(), _S_IREAD);
return static_cast<int>(Fail(GuestEio));
}
#else
if (::rmdir(native.c_str()) != 0) {
const int nativeError = errno;
if ((nativeError == ENOENT || nativeError == ENOTDIR) && HasNonDirectoryPrefix(native))
return static_cast<int>(Fail(GuestEnotdir));
return static_cast<int>(Fail(GuestError(std::error_code(nativeError, std::generic_category()))));
}
#endif
} else {
if (trailingSlash) {
if (std::filesystem::is_directory(symlinkStatus)) return static_cast<int>(Fail(GuestEperm));
if (std::filesystem::exists(symlinkStatus) || HasNonDirectoryPrefix(native))
return static_cast<int>(Fail(GuestEnotdir));
return static_cast<int>(Fail(GuestEnoent));
}
if (std::filesystem::is_directory(symlinkStatus)) return static_cast<int>(Fail(GuestEperm));
#ifdef _WIN32
struct _stat64 status{};
const bool unlocked = ::_wstat64(native.c_str(), &status) == 0 && (status.st_mode & _S_IFMT) == _S_IFREG &&
!(status.st_mode & _S_IWRITE) && ::_wchmod(native.c_str(), _S_IREAD | _S_IWRITE) == 0;
if (::_wunlink(native.c_str()) != 0) {
const int nativeError = errno;
if (unlocked) ::_wchmod(native.c_str(), _S_IREAD);
if (HasNonDirectoryPrefix(native)) return static_cast<int>(Fail(GuestEnotdir));
return static_cast<int>(Fail(GuestError(std::error_code(nativeError, std::generic_category()))));
}
#else
if (::unlink(native.c_str()) != 0) {
const int nativeError = errno;
if ((nativeError == ENOENT || nativeError == ENOTDIR) && HasNonDirectoryPrefix(native))
return static_cast<int>(Fail(GuestEnotdir));
return static_cast<int>(Fail(GuestError(std::error_code(nativeError, std::generic_category()))));
}
#endif
}
RecordWrittenPath_nid_no_patch(native);
return 0;
}
int APS5_VABI renameat_nid_postfix(int oldfd, const char* from, int newfd, const char* to) {
if (from == nullptr || to == nullptr) return static_cast<int>(Fail(GuestEfault));
if (*from == '\0' || *to == '\0') return static_cast<int>(Fail(GuestEnoent));
if ((oldfd == GuestAtFdcwd || *from == '/') && (newfd == GuestAtFdcwd || *to == '/'))
return RenameGuestPath_nid_no_patch(from, to);
if (std::strlen(from) >= GuestPathMax || HasLongComponent(from) ||
std::strlen(to) >= GuestPathMax || HasLongComponent(to))
return static_cast<int>(Fail(GuestEnametoolong));
if (IsDotOrDotDot(from) || IsDotOrDotDot(to)) return static_cast<int>(Fail(GuestEinval));
std::filesystem::path source;
if (oldfd == GuestAtFdcwd || *from == '/') {
if (const int error = ResolvePathError_nid_no_patch(from, source)) return static_cast<int>(Fail(error));
} else {
int error = 0;
const char* base = GetDirectoryDescriptorPath_nid_no_patch(oldfd, &error);
if (base == nullptr) return static_cast<int>(Fail(error));
source = std::filesystem::path(reinterpret_cast<const char8_t*>(base)) /
std::filesystem::path(reinterpret_cast<const char8_t*>(from));
}
std::filesystem::path destination;
if (newfd == GuestAtFdcwd || *to == '/') {
if (const int error = ResolvePathError_nid_no_patch(to, destination)) return static_cast<int>(Fail(error));
} else {
int error = 0;
const char* base = GetDirectoryDescriptorPath_nid_no_patch(newfd, &error);
if (base == nullptr) return static_cast<int>(Fail(error));
destination = std::filesystem::path(reinterpret_cast<const char8_t*>(base)) /
std::filesystem::path(reinterpret_cast<const char8_t*>(to));
}
const std::string_view fromView(from);
const std::string_view toView(to);
const bool oldTrailingSlash = !fromView.empty() && fromView.back() == '/';
const bool newTrailingSlash = !toView.empty() && toView.back() == '/';
while (!source.has_filename() && source.has_relative_path()) source = source.parent_path();
while (!destination.has_filename() && destination.has_relative_path()) destination = destination.parent_path();
std::error_code statusError;
const auto sourceStatus = std::filesystem::symlink_status(source, statusError);
if (oldTrailingSlash) {
if (sourceStatus.type() == std::filesystem::file_type::not_found)
return static_cast<int>(Fail(HasNonDirectoryPrefix(source) ? GuestEnotdir : GuestEnoent));
if (!std::filesystem::is_directory(sourceStatus)) return static_cast<int>(Fail(GuestEnotdir));
}
if (newTrailingSlash && sourceStatus.type() != std::filesystem::file_type::not_found &&
!std::filesystem::is_directory(sourceStatus))
return static_cast<int>(Fail(GuestEnotdir));
const int error = RenamePath(source, destination);
if (error) return static_cast<int>(Fail(error));
RecordWrittenPath_nid_no_patch(source);
RecordWrittenPath_nid_no_patch(destination);
return 0;
}
}