Repository navigation
Expand file tree
/
Copy pathOpen.cpp
More file actions
343 lines (328 loc) · 13.7 KB
/
Copy pathOpen.cpp
File metadata and controls
343 lines (328 loc) · 13.7 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
#include "prx/libkernel/File/include/FileFlags.hpp"
#include "prx/libkernel/File/include/NativeStat.hpp"
#include "prx/libc/include/General.hpp"
#include "prx/libc/include/FilesystemError.hpp"
#include "prx/libc/include/GuestArena.hpp"
#include "prx/libkernel/File/include/File.hpp"
#include "prx/libkernel/File/include/DirectoryDescriptor.hpp"
#include "prx/libkernel/File/include/FileLock.hpp"
#include "prx/libkernel/Socket/include/SocketRuntime.hpp"
#include "prx/libkernel/File/include/RandomDevice.hpp"
#include "prx/libkernel/KernelErrors.hpp"
#include "SceTypes.hpp"
#include <cerrno>
#include <cstdarg>
#include <limits>
#include <stdexcept>
#include <string>
#ifdef _WIN32
#include <fcntl.h>
#include <io.h>
#include <sys/stat.h>
#include <windows.h>
static int OpenError(DWORD error) {
switch (error) {
case ERROR_FILE_NOT_FOUND:
case ERROR_PATH_NOT_FOUND:
case ERROR_INVALID_DRIVE:
case ERROR_BAD_NETPATH:
case ERROR_BAD_NET_NAME:
case ERROR_BAD_PATHNAME:
case ERROR_FILENAME_EXCED_RANGE:
return ENOENT;
case ERROR_ACCESS_DENIED:
case ERROR_SHARING_VIOLATION:
case ERROR_LOCK_VIOLATION:
case ERROR_WRITE_PROTECT:
case ERROR_NETWORK_ACCESS_DENIED:
case ERROR_CANNOT_MAKE:
case ERROR_DRIVE_LOCKED:
return EACCES;
case ERROR_FILE_EXISTS:
case ERROR_ALREADY_EXISTS:
return EEXIST;
case ERROR_TOO_MANY_OPEN_FILES:
return EMFILE;
case ERROR_NOT_ENOUGH_MEMORY:
case ERROR_OUTOFMEMORY:
return ENOMEM;
case ERROR_DISK_FULL:
return ENOSPC;
default:
return EINVAL;
}
}
static int NativeOpen(const std::filesystem::path& p, int nativeFlags, std::uint16_t mode) {
const int access = nativeFlags & (_O_RDONLY | _O_WRONLY | _O_RDWR);
const DWORD desired = access == _O_RDONLY ? GENERIC_READ : access == _O_WRONLY ? GENERIC_WRITE : GENERIC_READ | GENERIC_WRITE;
DWORD disposition = OPEN_EXISTING;
if (nativeFlags & _O_CREAT) disposition = (nativeFlags & _O_EXCL) ? CREATE_NEW : (nativeFlags & _O_TRUNC) ? CREATE_ALWAYS : OPEN_ALWAYS;
else if (nativeFlags & _O_TRUNC) disposition = TRUNCATE_EXISTING;
const DWORD attributes = (nativeFlags & _O_CREAT) && !(mode & _S_IWRITE) ? FILE_ATTRIBUTE_READONLY : FILE_ATTRIBUTE_NORMAL;
SECURITY_ATTRIBUTES security{sizeof(SECURITY_ATTRIBUTES), nullptr, TRUE};
const auto handle = CreateFileW(p.c_str(), desired, FILE_SHARE_READ | FILE_SHARE_WRITE | FILE_SHARE_DELETE, &security, disposition, attributes, nullptr);
if (handle == INVALID_HANDLE_VALUE) {
errno = OpenError(GetLastError());
return -1;
}
const int descriptor = ::_open_osfhandle(reinterpret_cast<std::intptr_t>(handle), nativeFlags & (_O_APPEND | _O_RDONLY | _O_BINARY));
if (descriptor < 0) CloseHandle(handle);
return descriptor;
}
static std::int64_t NativeLseek(int fd, std::int64_t offset, int whence) {
return ::_lseeki64(fd, offset, whence);
}
static int NativeRead(int fd, void* buf, std::size_t n) {
if (n > static_cast<std::size_t>(std::numeric_limits<unsigned int>::max())) {
throw std::runtime_error("sceKernelRead: nbytes exceeds platform limit");
}
return ::_read(fd, buf, static_cast<unsigned int>(n));
}
static int NativeWrite(int fd, const void* buf, std::size_t n) {
if (n > static_cast<std::size_t>(std::numeric_limits<unsigned int>::max())) {
throw std::runtime_error("sceKernelWrite: nbytes exceeds platform limit");
}
return ::_write(fd, buf, static_cast<unsigned int>(n));
}
extern "C" _invalid_parameter_handler _set_thread_local_invalid_parameter_handler(_invalid_parameter_handler);
static void IgnoreInvalidParameter(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, std::uintptr_t) {}
static int NativeClose(int fd) {
const auto previous = _set_thread_local_invalid_parameter_handler(IgnoreInvalidParameter);
const int result = ::_close(fd);
_set_thread_local_invalid_parameter_handler(previous);
return result;
}
static int NativeUnlink(const std::filesystem::path& p) {
const auto path = p.wstring();
struct _stat64 status{};
const bool unlocked = ::_wstat64(path.c_str(), &status) == 0 && (status.st_mode & _S_IFMT) == _S_IFREG &&
!(status.st_mode & _S_IWRITE) && ::_wchmod(path.c_str(), _S_IREAD | _S_IWRITE) == 0;
if (::_wunlink(path.c_str()) == 0) return 0;
const int error = errno;
if (unlocked) ::_wchmod(path.c_str(), _S_IREAD);
errno = error;
return -1;
}
static int MapFlags(int sceFlags) {
int f = 0;
const int acc = sceFlags & SCE_KERNEL_O_ACCMODE;
if (acc == SCE_KERNEL_O_RDONLY) f |= _O_RDONLY;
else if (acc == SCE_KERNEL_O_WRONLY) f |= _O_WRONLY;
else if (acc == SCE_KERNEL_O_RDWR) f |= _O_RDWR;
else throw std::invalid_argument("sceKernelOpen: invalid access mode");
if (sceFlags & SCE_KERNEL_O_APPEND) f |= _O_APPEND;
if (sceFlags & SCE_KERNEL_O_CREAT) f |= _O_CREAT;
if (sceFlags & SCE_KERNEL_O_TRUNC) f |= _O_TRUNC;
if (sceFlags & SCE_KERNEL_O_EXCL) f |= _O_EXCL;
f |= _O_BINARY;
return f;
}
#else
#include <fcntl.h>
#include <unistd.h>
static int NativeOpen(const std::filesystem::path& p, int nativeFlags, std::uint16_t mode) {
return ::open(p.c_str(), nativeFlags, static_cast<mode_t>(mode));
}
static std::int64_t NativeLseek(int fd, std::int64_t offset, int whence) {
return ::lseek(fd, static_cast<off_t>(offset), whence);
}
static std::int64_t NativeRead(int fd, void* buf, std::size_t n) {
return ::read(fd, buf, n);
}
static std::int64_t NativeWrite(int fd, const void* buf, std::size_t n) {
return ::write(fd, buf, n);
}
static int NativeClose(int fd) { return ::close(fd); }
static int NativeUnlink(const std::filesystem::path& p) {
return ::unlink(p.c_str());
}
static int MapFlags(int sceFlags) {
int f = 0;
const int acc = sceFlags & SCE_KERNEL_O_ACCMODE;
if (acc == SCE_KERNEL_O_RDONLY) f |= O_RDONLY;
else if (acc == SCE_KERNEL_O_WRONLY) f |= O_WRONLY;
else if (acc == SCE_KERNEL_O_RDWR) f |= O_RDWR;
else throw std::invalid_argument("sceKernelOpen: invalid access mode");
if (sceFlags & SCE_KERNEL_O_APPEND) f |= O_APPEND;
if (sceFlags & SCE_KERNEL_O_CREAT) f |= O_CREAT;
if (sceFlags & SCE_KERNEL_O_TRUNC) f |= O_TRUNC;
if (sceFlags & SCE_KERNEL_O_EXCL) f |= O_EXCL;
if (sceFlags & SCE_KERNEL_O_SYNC) f |= O_SYNC;
if (sceFlags & SCE_KERNEL_O_DIRECTORY) f |= O_DIRECTORY;
return f;
}
#endif
extern "C" int* APS5_VABI __error_nid_postfix();
extern "C" int APS5_VABI fcntl_nid_postfix(int descriptor, int command, ...);
static constexpr int GuestSetFlags = 4;
static int SceErrorFromErrno(int error) {
constexpr int GuestEio = 5;
const int guest = error > 0 && error <= 34 ? error : GuestEio;
return static_cast<int>(0x80020000u | static_cast<unsigned>(guest));
}
static int SceIoErrorFromErrno(int error) {
int guest = SceErrorFromErrno(error) & 0xffff;
if (error == EAGAIN) guest = 35;
#ifdef EDEADLK
else if (error == EDEADLK) guest = 11;
#endif
#ifdef EDQUOT
else if (error == EDQUOT) guest = 69;
#endif
#ifdef EOVERFLOW
else if (error == EOVERFLOW) guest = 84;
#endif
return static_cast<int>(0x80020000u | static_cast<unsigned>(guest));
}
extern "C" {
int APS5_VABI sceKernelOpen(const char* path, int flags, std::uint16_t mode) {
if (path != nullptr && *path == '\0') return SCE_KERNEL_ERROR_ENOENT;
APS5_LOG_OUT("path=%s flags=0x%X nativeFlags=0x%X mode=0%o", path, flags, MapFlags(flags), mode);
if (path != nullptr && File::IsRandomDevicePath(path)) {
if ((flags & SCE_KERNEL_O_ACCMODE) != SCE_KERNEL_O_RDONLY) throw std::runtime_error(std::string(__func__) + ": writing to " + path + " is not implemented");
const int fd = File::OpenRandomDevice();
return fd < 0 ? SceErrorFromErrno(errno) : fd;
}
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return SceKernelError(error);
#ifdef _WIN32
if ((flags & (SCE_KERNEL_O_DIRECTORY | SCE_KERNEL_O_CREAT)) == SCE_KERNEL_O_DIRECTORY) {
std::error_code error;
const auto status = std::filesystem::status(native, error);
if (std::filesystem::exists(status) && !std::filesystem::is_directory(status)) return SceErrorFromErrno(ENOTDIR);
}
#endif
int fd = NativeOpen(native, MapFlags(flags), mode);
#ifdef _WIN32
if (fd < 0 && errno != ENOENT) {
std::error_code error;
if (std::filesystem::is_directory(native, error)) {
if ((flags & (SCE_KERNEL_O_CREAT | SCE_KERNEL_O_EXCL)) == (SCE_KERNEL_O_CREAT | SCE_KERNEL_O_EXCL)) errno = EEXIST;
else if ((flags & SCE_KERNEL_O_ACCMODE) != SCE_KERNEL_O_RDONLY || (flags & (SCE_KERNEL_O_CREAT | SCE_KERNEL_O_TRUNC))) errno = EISDIR;
else fd = File::OpenDirectoryDescriptor(native);
}
}
#endif
if (fd < 0) {
return SceErrorFromErrno(errno);
}
if ((flags & SCE_KERNEL_O_ACCMODE) != SCE_KERNEL_O_RDONLY || (flags & (SCE_KERNEL_O_CREAT | SCE_KERNEL_O_TRUNC)))
RecordWrittenPath_nid_no_patch(native);
return fd;
}
int APS5_VABI sceKernelClose(int d) {
File::ForgetRandomDevice(d);
#ifdef _WIN32
File::ForgetDirectoryDescriptor(d);
File::ForgetFileLock(d);
#endif
if (NativeClose(d) != 0) {
if (errno == EBADF) return SCE_KERNEL_ERROR_EBADF;
throw std::runtime_error(std::string(__func__) + ": close failed, fd=" + std::to_string(d) + ", errno=" + std::to_string(errno));
}
return 0;
}
std::int64_t APS5_VABI sceKernelRead(int d, void* buf, std::size_t nbytes) {
if (d >= GuestSockets::FirstDescriptor) {
const auto n = GuestSockets::Read(d, buf, nbytes);
return n < 0 ? SceKernelError(*__error_nid_postfix()) : n;
}
if (buf == nullptr) {
throw std::invalid_argument(std::string(__func__) + ": buf is null");
}
const GuestArena::HostWrite destination(buf, nbytes);
if (!destination.Open()) errno = EFAULT;
else if (File::ReadRandomDevice(d, buf, nbytes)) return static_cast<std::int64_t>(nbytes);
auto n = destination.Open() ? NativeRead(d, buf, nbytes) : -1;
if (n < 0) return SceIoErrorFromErrno(errno);
return static_cast<std::int64_t>(n);
}
std::int64_t APS5_VABI sceKernelWrite(int d, const void* buf, std::size_t nbytes) {
if (d >= GuestSockets::FirstDescriptor) {
const auto n = GuestSockets::Write(d, buf, nbytes);
return n < 0 ? SceKernelError(*__error_nid_postfix()) : n;
}
if (buf == nullptr) {
throw std::invalid_argument(std::string(__func__) + ": buf is null");
}
auto n = NativeWrite(d, buf, nbytes);
if (n < 0) return SceIoErrorFromErrno(errno);
return static_cast<std::int64_t>(n);
}
std::int64_t APS5_VABI sceKernelLseek(int d, std::int64_t offset, int whence) {
if (whence < 0 || whence > 2) {
throw std::invalid_argument(std::string(__func__) + ": invalid whence=" + std::to_string(whence));
}
#ifdef _WIN32
if (const auto directory = File::SeekDirectoryDescriptor(d, offset, whence)) {
if (*directory < 0) return SceIoErrorFromErrno(errno);
return *directory;
}
#endif
std::int64_t result = NativeLseek(d, offset, whence);
if (result < 0) return SceIoErrorFromErrno(errno);
return result;
}
int APS5_VABI sceKernelStat(const char* path, FileStat* sb) {
if (path == nullptr) {
throw std::invalid_argument(std::string(__func__) + ": path is null");
}
if (sb == nullptr) {
throw std::invalid_argument(std::string(__func__) + ": sb is null");
}
if (*path == '\0') return SceErrorFromErrno(2);
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return SceKernelError(error);
std::error_code error;
constexpr int GuestEnotdir = 20;
const auto status = std::filesystem::status(native, error);
if (error == std::errc::not_a_directory) return SceErrorFromErrno(GuestEnotdir);
if (!std::filesystem::exists(status)) {
return SceErrorFromErrno(2);
}
const std::string_view guestPath(path);
if (!guestPath.empty() && guestPath.back() == '/' && !std::filesystem::is_directory(status)) return SceErrorFromErrno(GuestEnotdir);
File::FillFileStat(native, sb);
return 0;
}
int APS5_VABI sceKernelUnlink(const char* path) {
if (path == nullptr) {
throw std::invalid_argument(std::string(__func__) + ": path is null");
}
if (*path == '\0') return SceErrorFromErrno(2);
std::filesystem::path native;
if (const int error = ResolvePathError_nid_no_patch(path, native)) return SceKernelError(error);
constexpr int GuestEperm = 1;
std::error_code error;
if (std::filesystem::is_directory(std::filesystem::symlink_status(native, error))) return SceErrorFromErrno(GuestEperm);
if (NativeUnlink(native) != 0) {
return SceErrorFromErrno(errno);
}
RecordWrittenPath_nid_no_patch(native);
return 0;
}
int APS5_VABI sceKernelFcntl(int d, int command, ...) {
if (d < GuestSockets::FirstDescriptor) {
if (!DescriptorIsOpen_nid_no_patch(d)) return SCE_KERNEL_ERROR_EBADF;
throw std::runtime_error(std::string(__func__) + ": file descriptors are not implemented, fd=" + std::to_string(d));
}
int result;
if (command == GuestSetFlags) {
#ifdef _WIN32
__builtin_sysv_va_list arguments;
__builtin_sysv_va_start(arguments, command);
const int flags = __builtin_va_arg(arguments, int);
__builtin_sysv_va_end(arguments);
#else
std::va_list arguments;
va_start(arguments, command);
const int flags = va_arg(arguments, int);
va_end(arguments);
#endif
result = fcntl_nid_postfix(d, command, flags);
} else {
result = fcntl_nid_postfix(d, command);
}
return result < 0 ? SceKernelError(*__error_nid_postfix()) : result;
}
}