fs::file: implement release_handle

cleanup
This commit is contained in:
DH 2025-03-02 19:44:55 +03:00 committed by Megamouse
parent 95d0cb18e4
commit d766baef12
5 changed files with 48 additions and 24 deletions

View file

@ -397,7 +397,7 @@ namespace fs
#ifdef _WIN32 #ifdef _WIN32
class windows_file final : public file_base class windows_file final : public file_base
{ {
const HANDLE m_handle; HANDLE m_handle;
atomic_t<u64> m_pos; atomic_t<u64> m_pos;
public: public:
@ -409,7 +409,10 @@ namespace fs
~windows_file() override ~windows_file() override
{ {
CloseHandle(m_handle); if (m_handle != nullptr)
{
CloseHandle(m_handle);
}
} }
stat_t get_stat() override stat_t get_stat() override
@ -592,11 +595,16 @@ namespace fs
std::memcpy(id.data.data(), &info, sizeof(info)); std::memcpy(id.data.data(), &info, sizeof(info));
return id; return id;
} }
void release() override
{
m_handle = nullptr;
}
}; };
#else #else
class unix_file final : public file_base class unix_file final : public file_base
{ {
const int m_fd; int m_fd;
public: public:
unix_file(int fd) unix_file(int fd)
@ -606,7 +614,10 @@ namespace fs
~unix_file() override ~unix_file() override
{ {
::close(m_fd); if (m_fd >= 0)
{
::close(m_fd);
}
} }
stat_t get_stat() override stat_t get_stat() override
@ -768,6 +779,11 @@ namespace fs
return result; return result;
} }
void release() override
{
m_fd = -1;
}
}; };
#endif #endif
} }
@ -1685,21 +1701,19 @@ fs::file::file(const std::string& path, bs_t<open_mode> mode)
} }
fs::file fs::file::from_native_handle(native_handle handle)
{
fs::file result;
#ifdef _WIN32 #ifdef _WIN32
fs::file fs::file::from_native_handle(void *handle)
{
fs::file result;
result.m_file = std::make_unique<windows_file>((const HANDLE)handle); result.m_file = std::make_unique<windows_file>((const HANDLE)handle);
return result;
}
#else #else
fs::file fs::file::from_native_handle(int fd) result.m_file = std::make_unique<unix_file>(handle);
{ #endif
fs::file result;
result.m_file = std::make_unique<unix_file>(fd);
return result; return result;
} }
#endif
fs::file::file(const void* ptr, usz size) fs::file::file(const void* ptr, usz size)
{ {

View file

@ -114,6 +114,9 @@ namespace fs
virtual native_handle get_handle(); virtual native_handle get_handle();
virtual file_id get_id(); virtual file_id get_id();
virtual u64 write_gather(const iovec_clone* buffers, u64 buf_count); virtual u64 write_gather(const iovec_clone* buffers, u64 buf_count);
virtual void release()
{
}
}; };
// Directory entry (TODO) // Directory entry (TODO)
@ -251,11 +254,7 @@ namespace fs
// Open file with specified mode // Open file with specified mode
explicit file(const std::string& path, bs_t<open_mode> mode = ::fs::read); explicit file(const std::string& path, bs_t<open_mode> mode = ::fs::read);
#ifdef _WIN32 static file from_native_handle(native_handle handle);
static file from_native_handle(void *handle);
#else
static file from_native_handle(int fd);
#endif
// Open memory for read // Open memory for read
explicit file(const void* ptr, usz size); explicit file(const void* ptr, usz size);
@ -286,9 +285,17 @@ namespace fs
m_file = std::move(ptr); m_file = std::move(ptr);
} }
void release_handle()
{
if (m_file)
{
release()->release();
}
}
std::unique_ptr<file_base> release() std::unique_ptr<file_base> release()
{ {
return std::move(m_file); return std::exchange(m_file, nullptr);
} }
// Change file size (possibly appending zero bytes) // Change file size (possibly appending zero bytes)

View file

@ -709,12 +709,9 @@ bool VerifyEDATHeaderWithKLicense(const fs::file& input, const std::string& inpu
return false; 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) if (npd_out)
{ {
memcpy(npd_out, &NPD, sizeof(NPD_HEADER)); *npd_out = NPD;
} }
return true; return true;

View file

@ -356,6 +356,11 @@ public:
void abort_extract(); void abort_extract();
fs::file &file()
{
return m_file;
}
private: private:
bool read_header(); bool read_header();
bool read_metadata(); bool read_metadata();

View file

@ -59,6 +59,7 @@ class pup_object
public: public:
pup_object(fs::file&& file); pup_object(fs::file&& file);
fs::file &file() { return m_file; }
explicit operator pup_error() const { return m_error; } explicit operator pup_error() const { return m_error; }
const std::string& get_formatted_error() const { return m_formatted_error; } const std::string& get_formatted_error() const { return m_formatted_error; }