Merge pull request #245 from RomainROCH/codex/fix-windows-directory-close

fix(libkernel): clear directory metadata on close
This commit is contained in:
boykopovar
2026-10-02 22:21:30 +03:00
committed by GitHub
3 changed files with 78 additions and 0 deletions
+11
View File
@@ -216,5 +216,16 @@ if(BUILD_TESTING)
target_include_directories(guest_directory_entry_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_directory_entry_tests PRIVATE libkernel libc)
add_test(NAME guest_directory_entries COMMAND guest_directory_entry_tests)
else()
add_test_executable(guest_directory_close_tests ${LIBS_INCLUDE_DIR}/tests/GuestDirectoryClose.cpp)
target_include_directories(guest_directory_close_tests PRIVATE ${LIBS_INCLUDE_DIR})
target_link_libraries(guest_directory_close_tests PRIVATE libkernel libc)
configure_windows_unwind(guest_directory_close_tests)
add_test(NAME guest_directory_close COMMAND guest_directory_close_tests)
set_tests_properties(guest_directory_close PROPERTIES TIMEOUT 15 ENVIRONMENT_MODIFICATION
"PATH=path_list_prepend:$<TARGET_FILE_DIR:libkernel>")
if(MINGW)
target_compile_options(guest_directory_close_tests PRIVATE -fasynchronous-unwind-tables)
endif()
endif()
endif()
@@ -148,6 +148,7 @@ int APS5_VABI chmod_nid_postfix(const char* path, int mode) {
int APS5_VABI close_nid_postfix(int d) {
if (d >= GuestSockets::FirstDescriptor) return GuestSockets::Close(d);
#ifdef _WIN32
File::ForgetDirectoryDescriptor(d);
return _close(d);
#else
return ::close(d);
+66
View File
@@ -0,0 +1,66 @@
#include "prx/libc/include/general/VabiMacros.hpp"
#include "prx/libkernel/File/include/FileFlags.hpp"
#include <array>
#include <chrono>
#include <cstdint>
#include <cstdio>
#include <cstdlib>
#include <filesystem>
#include <fstream>
#include <string>
extern "C" {
int APS5_VABI sceKernelOpen(const char*, int, std::uint16_t);
int APS5_VABI sceKernelGetdents(int, char*, int);
int APS5_VABI close_nid_postfix(int);
int APS5_VABI _close_nid_postfix(int);
}
static void Check(bool value, int line) {
if (!value) {
std::fprintf(stderr, "Directory close check failed at line %d\n", line);
std::abort();
}
}
#define Require(value) Check((value), __LINE__)
static constexpr int ErrorEnotdir = static_cast<int>(0x80020014u);
static void VerifyCloseReuse(const std::filesystem::path& root, const char* scenario,
bool useUnderscoreClose) {
const auto directoryPath = root / scenario;
Require(std::filesystem::create_directory(directoryPath));
const auto filePath = directoryPath / "entry.txt";
{
std::ofstream file(filePath, std::ios::binary);
Require(file.is_open());
file << "entry";
Require(static_cast<bool>(file));
}
const int directory = sceKernelOpen(directoryPath.string().c_str(),
SCE_KERNEL_O_RDONLY | SCE_KERNEL_O_DIRECTORY, 0);
Require(directory >= 0);
Require((useUnderscoreClose ? _close_nid_postfix(directory) : close_nid_postfix(directory)) == 0);
const int file = sceKernelOpen(filePath.string().c_str(), SCE_KERNEL_O_RDONLY, 0);
Require(file >= 0);
Require(file == directory);
std::array<char, 256> entries{};
Require(sceKernelGetdents(file, entries.data(), static_cast<int>(entries.size())) == ErrorEnotdir);
Require((useUnderscoreClose ? _close_nid_postfix(file) : close_nid_postfix(file)) == 0);
}
int main() {
const auto root = std::filesystem::temp_directory_path() /
("anyps5-directory-close-" +
std::to_string(std::chrono::steady_clock::now().time_since_epoch().count()));
Require(std::filesystem::create_directory(root));
VerifyCloseReuse(root, "close", false);
VerifyCloseReuse(root, "underscore-close", true);
std::filesystem::remove_all(root);
}