Skip to content

Commit d766bae

Browse files
DHrpcs3Megamouse
authored andcommitted
fs::file: implement release_handle
cleanup
1 parent 95d0cb1 commit d766bae

File tree

5 files changed

+47
-23
lines changed

5 files changed

+47
-23
lines changed

Utilities/File.cpp

Lines changed: 27 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -397,7 +397,7 @@ namespace fs
397397
#ifdef _WIN32
398398
class windows_file final : public file_base
399399
{
400-
const HANDLE m_handle;
400+
HANDLE m_handle;
401401
atomic_t<u64> m_pos;
402402

403403
public:
@@ -409,7 +409,10 @@ namespace fs
409409

410410
~windows_file() override
411411
{
412-
CloseHandle(m_handle);
412+
if (m_handle != nullptr)
413+
{
414+
CloseHandle(m_handle);
415+
}
413416
}
414417

415418
stat_t get_stat() override
@@ -592,11 +595,16 @@ namespace fs
592595
std::memcpy(id.data.data(), &info, sizeof(info));
593596
return id;
594597
}
598+
599+
void release() override
600+
{
601+
m_handle = nullptr;
602+
}
595603
};
596604
#else
597605
class unix_file final : public file_base
598606
{
599-
const int m_fd;
607+
int m_fd;
600608

601609
public:
602610
unix_file(int fd)
@@ -606,7 +614,10 @@ namespace fs
606614

607615
~unix_file() override
608616
{
609-
::close(m_fd);
617+
if (m_fd >= 0)
618+
{
619+
::close(m_fd);
620+
}
610621
}
611622

612623
stat_t get_stat() override
@@ -768,6 +779,11 @@ namespace fs
768779

769780
return result;
770781
}
782+
783+
void release() override
784+
{
785+
m_fd = -1;
786+
}
771787
};
772788
#endif
773789
}
@@ -1685,21 +1701,19 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
16851701
}
16861702

16871703

1688-
#ifdef _WIN32
1689-
fs::file fs::file::from_native_handle(void *handle)
1704+
1705+
fs::file fs::file::from_native_handle(native_handle handle)
16901706
{
16911707
fs::file result;
1708+
1709+
#ifdef _WIN32
16921710
result.m_file = std::make_unique<windows_file>((const HANDLE)handle);
1693-
return result;
1694-
}
16951711
#else
1696-
fs::file fs::file::from_native_handle(int fd)
1697-
{
1698-
fs::file result;
1699-
result.m_file = std::make_unique<unix_file>(fd);
1712+
result.m_file = std::make_unique<unix_file>(handle);
1713+
#endif
1714+
17001715
return result;
17011716
}
1702-
#endif
17031717

17041718
fs::file::file(const void* ptr, usz size)
17051719
{

Utilities/File.h

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -114,6 +114,9 @@ namespace fs
114114
virtual native_handle get_handle();
115115
virtual file_id get_id();
116116
virtual u64 write_gather(const iovec_clone* buffers, u64 buf_count);
117+
virtual void release()
118+
{
119+
}
117120
};
118121

119122
// Directory entry (TODO)
@@ -251,11 +254,7 @@ namespace fs
251254
// Open file with specified mode
252255
explicit file(const std::string& path, bs_t<open_mode> mode = ::fs::read);
253256

254-
#ifdef _WIN32
255-
static file from_native_handle(void *handle);
256-
#else
257-
static file from_native_handle(int fd);
258-
#endif
257+
static file from_native_handle(native_handle handle);
259258

260259
// Open memory for read
261260
explicit file(const void* ptr, usz size);
@@ -286,9 +285,17 @@ namespace fs
286285
m_file = std::move(ptr);
287286
}
288287

288+
void release_handle()
289+
{
290+
if (m_file)
291+
{
292+
release()->release();
293+
}
294+
}
295+
289296
std::unique_ptr<file_base> release()
290297
{
291-
return std::move(m_file);
298+
return std::exchange(m_file, nullptr);
292299
}
293300

294301
// Change file size (possibly appending zero bytes)

rpcs3/Crypto/unedat.cpp

Lines changed: 1 addition & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -709,12 +709,9 @@ bool VerifyEDATHeaderWithKLicense(const fs::file& input, const std::string& inpu
709709
return false;
710710
}
711711

712-
std::string_view sv{NPD.content_id, std::size(NPD.content_id)};
713-
sv = sv.substr(0, sv.find_first_of('\0'));
714-
715712
if (npd_out)
716713
{
717-
memcpy(npd_out, &NPD, sizeof(NPD_HEADER));
714+
*npd_out = NPD;
718715
}
719716

720717
return true;

rpcs3/Crypto/unpkg.h

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -356,6 +356,11 @@ class package_reader
356356

357357
void abort_extract();
358358

359+
fs::file &file()
360+
{
361+
return m_file;
362+
}
363+
359364
private:
360365
bool read_header();
361366
bool read_metadata();

rpcs3/Loader/PUP.h

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -59,6 +59,7 @@ class pup_object
5959

6060
public:
6161
pup_object(fs::file&& file);
62+
fs::file &file() { return m_file; }
6263

6364
explicit operator pup_error() const { return m_error; }
6465
const std::string& get_formatted_error() const { return m_formatted_error; }

0 commit comments

Comments
 (0)