Files
AnyPS5/core/libs/prx/libScePosixForWebKit/Filesystem.cpp
T
Alex WhitehouseandClaude Opus 5.5 0ed07ade91 feat(libScePosixForWebKit): implement mkostemp
Implement mkostemp following FreeBSD 11.4 mktemp.c, for WebKit's
FileSystemPOSIX openTemporaryFile, which calls mkostemp(..., O_CLOEXEC).
Flags outside O_APPEND, O_DIRECT, O_SHLOCK, O_EXLOCK, O_SYNC and O_CLOEXEC
return EINVAL before the template is read. O_APPEND, O_SYNC and O_CLOEXEC
pass through guest open; O_CLOEXEC also clears native handle inheritance.
O_SHLOCK and O_EXLOCK take a guest flock after creation, exclusive winning;
a lock failure closes the descriptor and leaves the file, as FreeBSD does.
O_DIRECT throws, since FreeBSD's cache semantics have no proven host
equivalent.

mkstemp and mkostemp now share one temporary-file loop in TemporaryFile.cpp,
which starts with mktemp.c's notice; mkstemp's behaviour is unchanged.

Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
2026-11-06 22:31:57 +00:00

52 lines
1.4 KiB
C++

#include "prx/libkernel/include/Api.hpp"
#include "prx/libc/include/Api.hpp"
#include "prx/libScePosixForWebKit/include/Api.hpp"
#include "prx/libc/include/GuestArena.hpp"
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libkernel/File/include/FileFlags.hpp"
#include "prx/libkernel/Socket/include/SocketRuntime.hpp"
#include <cerrno>
#include <cstring>
#ifdef _WIN32
#include <windows.h>
#include <io.h>
extern "C" _invalid_parameter_handler _set_thread_local_invalid_parameter_handler(_invalid_parameter_handler);
#else
#include <unistd.h>
#endif
namespace {
int Failure(int error) {
*__error_nid_postfix() = error;
return -1;
}
#ifdef _WIN32
void IgnoreInvalidParameter(const wchar_t*, const wchar_t*, const wchar_t*, unsigned int, std::uintptr_t) {}
#endif
}
extern "C" int APS5_VABI isatty_nid_postfix(int descriptor) {
if (GuestSocketIsOpen_nid_no_patch(descriptor)) {
Failure(25);
return 0;
}
#ifdef _WIN32
const auto previous = _set_thread_local_invalid_parameter_handler(IgnoreInvalidParameter);
const auto handle = reinterpret_cast<HANDLE>(::_get_osfhandle(descriptor));
_set_thread_local_invalid_parameter_handler(previous);
if (handle == INVALID_HANDLE_VALUE) {
Failure(9);
return 0;
}
DWORD mode = 0;
if (::GetConsoleMode(handle, &mode)) return 1;
Failure(25);
#else
if (::isatty(descriptor)) return 1;
Failure(errno == EBADF ? 9 : 25);
#endif
return 0;
}