diff --git a/Utilities/File.cpp b/Utilities/File.cpp index 0d5d5dd13c..c5fecc9d14 100644 --- a/Utilities/File.cpp +++ b/Utilities/File.cpp @@ -397,7 +397,7 @@ namespace fs #ifdef _WIN32 class windows_file final : public file_base { - const HANDLE m_handle; + HANDLE m_handle; atomic_t m_pos; public: @@ -409,7 +409,10 @@ namespace fs ~windows_file() override { - CloseHandle(m_handle); + if (m_handle != nullptr) + { + CloseHandle(m_handle); + } } stat_t get_stat() override @@ -592,11 +595,16 @@ namespace fs std::memcpy(id.data.data(), &info, sizeof(info)); return id; } + + void release() override + { + m_handle = nullptr; + } }; #else class unix_file final : public file_base { - const int m_fd; + int m_fd; public: unix_file(int fd) @@ -606,7 +614,10 @@ namespace fs ~unix_file() override { - ::close(m_fd); + if (m_fd >= 0) + { + ::close(m_fd); + } } stat_t get_stat() override @@ -768,6 +779,11 @@ namespace fs return result; } + + void release() override + { + m_fd = -1; + } }; #endif } @@ -1685,21 +1701,19 @@ fs::file::file(const std::string& path, bs_t mode) } + +fs::file fs::file::from_native_handle(native_handle handle) +{ + fs::file result; + #ifdef _WIN32 -fs::file fs::file::from_native_handle(void *handle) -{ - fs::file result; result.m_file = std::make_unique((const HANDLE)handle); - return result; -} #else -fs::file fs::file::from_native_handle(int fd) -{ - fs::file result; - result.m_file = std::make_unique(fd); + result.m_file = std::make_unique(handle); +#endif + return result; } -#endif fs::file::file(const void* ptr, usz size) { diff --git a/Utilities/File.h b/Utilities/File.h index 2ba32f7982..a5a4c53d75 100644 --- a/Utilities/File.h +++ b/Utilities/File.h @@ -114,6 +114,9 @@ namespace fs virtual native_handle get_handle(); virtual file_id get_id(); virtual u64 write_gather(const iovec_clone* buffers, u64 buf_count); + virtual void release() + { + } }; // Directory entry (TODO) @@ -251,11 +254,7 @@ namespace fs // Open file with specified mode explicit file(const std::string& path, bs_t mode = ::fs::read); -#ifdef _WIN32 - static file from_native_handle(void *handle); -#else - static file from_native_handle(int fd); -#endif + static file from_native_handle(native_handle handle); // Open memory for read explicit file(const void* ptr, usz size); @@ -286,9 +285,17 @@ namespace fs m_file = std::move(ptr); } + void release_handle() + { + if (m_file) + { + release()->release(); + } + } + std::unique_ptr release() { - return std::move(m_file); + return std::exchange(m_file, nullptr); } // Change file size (possibly appending zero bytes) diff --git a/rpcs3/Crypto/unedat.cpp b/rpcs3/Crypto/unedat.cpp index 985b2fc74c..1ffaa33fc3 100644 --- a/rpcs3/Crypto/unedat.cpp +++ b/rpcs3/Crypto/unedat.cpp @@ -709,12 +709,9 @@ bool VerifyEDATHeaderWithKLicense(const fs::file& input, const std::string& inpu return false; } - std::string_view sv{NPD.content_id, std::size(NPD.content_id)}; - sv = sv.substr(0, sv.find_first_of('\0')); - if (npd_out) { - memcpy(npd_out, &NPD, sizeof(NPD_HEADER)); + *npd_out = NPD; } return true; diff --git a/rpcs3/Crypto/unpkg.h b/rpcs3/Crypto/unpkg.h index 1062592e52..d253561509 100644 --- a/rpcs3/Crypto/unpkg.h +++ b/rpcs3/Crypto/unpkg.h @@ -356,6 +356,11 @@ public: void abort_extract(); + fs::file &file() + { + return m_file; + } + private: bool read_header(); bool read_metadata(); diff --git a/rpcs3/Loader/PUP.h b/rpcs3/Loader/PUP.h index 286375039c..0732738d5a 100644 --- a/rpcs3/Loader/PUP.h +++ b/rpcs3/Loader/PUP.h @@ -59,6 +59,7 @@ class pup_object public: pup_object(fs::file&& file); + fs::file &file() { return m_file; } explicit operator pup_error() const { return m_error; } const std::string& get_formatted_error() const { return m_formatted_error; }