mirror of
https://github.com/Dark98/threeSD.git
synced 2026-07-03 00:38:58 +00:00
Use destructor to release file handle
Instead of relying on writing `file.reset()`s
This commit is contained in:
@@ -41,4 +41,5 @@ ScopeExitHelper<Func> ScopeExit(Func&& func) {
|
|||||||
* }
|
* }
|
||||||
* \endcode
|
* \endcode
|
||||||
*/
|
*/
|
||||||
#define SCOPE_EXIT(body) auto CONCAT2(scope_exit_helper_, __LINE__) = detail::ScopeExit([&]() body)
|
#define SCOPE_EXIT(body) \
|
||||||
|
auto CONCAT2(scope_exit_helper_, __LINE__) = ::detail::ScopeExit([&]() body)
|
||||||
|
|||||||
+13
-1
@@ -6,6 +6,7 @@
|
|||||||
#include "common/assert.h"
|
#include "common/assert.h"
|
||||||
#include "common/common_paths.h"
|
#include "common/common_paths.h"
|
||||||
#include "common/file_util.h"
|
#include "common/file_util.h"
|
||||||
|
#include "common/scope_exit.h"
|
||||||
#include "common/string_util.h"
|
#include "common/string_util.h"
|
||||||
#include "core/data_container.h"
|
#include "core/data_container.h"
|
||||||
#include "core/decryptor.h"
|
#include "core/decryptor.h"
|
||||||
@@ -60,7 +61,6 @@ bool SDMCImporter::Init() {
|
|||||||
}
|
}
|
||||||
|
|
||||||
decryptor = std::make_unique<SDMCDecryptor>(config.sdmc_path);
|
decryptor = std::make_unique<SDMCDecryptor>(config.sdmc_path);
|
||||||
cia_builder = std::make_unique<CIABuilder>();
|
|
||||||
|
|
||||||
// Load SDMC Title DB
|
// Load SDMC Title DB
|
||||||
{
|
{
|
||||||
@@ -694,6 +694,15 @@ bool SDMCImporter::BuildCIA(CIABuildType type, const ContentSpecifier& specifier
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
{
|
||||||
|
std::lock_guard lock{cia_builder_mutex};
|
||||||
|
cia_builder = std::make_unique<CIABuilder>();
|
||||||
|
}
|
||||||
|
SCOPE_EXIT({
|
||||||
|
std::lock_guard lock{cia_builder_mutex};
|
||||||
|
cia_builder.reset(); // To release file handles, etc
|
||||||
|
});
|
||||||
|
|
||||||
const bool ret = cia_builder->Init(type, destination, tmd, config,
|
const bool ret = cia_builder->Init(type, destination, tmd, config,
|
||||||
FileUtil::GetDirectoryTreeSize(physical_path), callback);
|
FileUtil::GetDirectoryTreeSize(physical_path), callback);
|
||||||
if (!ret) {
|
if (!ret) {
|
||||||
@@ -752,8 +761,11 @@ bool SDMCImporter::BuildCIA(CIABuildType type, const ContentSpecifier& specifier
|
|||||||
}
|
}
|
||||||
|
|
||||||
void SDMCImporter::AbortBuildCIA() {
|
void SDMCImporter::AbortBuildCIA() {
|
||||||
|
std::lock_guard lock{cia_builder_mutex};
|
||||||
|
if (cia_builder) {
|
||||||
cia_builder->Abort();
|
cia_builder->Abort();
|
||||||
}
|
}
|
||||||
|
}
|
||||||
|
|
||||||
void SDMCImporter::ListTitle(std::vector<ContentSpecifier>& out) const {
|
void SDMCImporter::ListTitle(std::vector<ContentSpecifier>& out) const {
|
||||||
const auto ProcessDirectory = [this, &out, &sdmc_path = config.sdmc_path](ContentType type,
|
const auto ProcessDirectory = [this, &out, &sdmc_path = config.sdmc_path](ContentType type,
|
||||||
|
|||||||
@@ -6,6 +6,7 @@
|
|||||||
|
|
||||||
#include <functional>
|
#include <functional>
|
||||||
#include <memory>
|
#include <memory>
|
||||||
|
#include <mutex>
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
#include <vector>
|
||||||
#include "common/common_types.h"
|
#include "common/common_types.h"
|
||||||
@@ -199,7 +200,9 @@ private:
|
|||||||
bool is_good{};
|
bool is_good{};
|
||||||
Config config;
|
Config config;
|
||||||
std::unique_ptr<SDMCDecryptor> decryptor;
|
std::unique_ptr<SDMCDecryptor> decryptor;
|
||||||
|
|
||||||
std::unique_ptr<CIABuilder> cia_builder;
|
std::unique_ptr<CIABuilder> cia_builder;
|
||||||
|
std::mutex cia_builder_mutex;
|
||||||
|
|
||||||
// The NCCH used to dump CXIs.
|
// The NCCH used to dump CXIs.
|
||||||
std::unique_ptr<NCCHContainer> dump_cxi_ncch;
|
std::unique_ptr<NCCHContainer> dump_cxi_ncch;
|
||||||
|
|||||||
@@ -67,7 +67,6 @@ bool CIABuilder::Init(CIABuildType type_, const std::string& destination, TitleM
|
|||||||
file = std::make_shared<HashedFile>(destination, "wb");
|
file = std::make_shared<HashedFile>(destination, "wb");
|
||||||
if (!*file) {
|
if (!*file) {
|
||||||
LOG_ERROR(Core, "Could not open file {}", destination);
|
LOG_ERROR(Core, "Could not open file {}", destination);
|
||||||
file.reset();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -82,7 +81,6 @@ bool CIABuilder::Init(CIABuildType type_, const std::string& destination, TitleM
|
|||||||
// Check for legit TMD
|
// Check for legit TMD
|
||||||
if (!tmd.VerifyHashes() || !tmd.ValidateSignature()) {
|
if (!tmd.VerifyHashes() || !tmd.ValidateSignature()) {
|
||||||
LOG_ERROR(Core, "TMD is not legit");
|
LOG_ERROR(Core, "TMD is not legit");
|
||||||
file.reset();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -95,14 +93,12 @@ bool CIABuilder::Init(CIABuildType type_, const std::string& destination, TitleM
|
|||||||
header.cert_size = CIA_CERT_SIZE;
|
header.cert_size = CIA_CERT_SIZE;
|
||||||
if (!WriteCert(config.certs_db_path)) {
|
if (!WriteCert(config.certs_db_path)) {
|
||||||
LOG_ERROR(Core, "Could not write cert to file {}", destination);
|
LOG_ERROR(Core, "Could not write cert to file {}", destination);
|
||||||
file.reset();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
// Ticket
|
// Ticket
|
||||||
ticket_offset = Common::AlignUp(cert_offset + header.cert_size, CIA_ALIGNMENT);
|
ticket_offset = Common::AlignUp(cert_offset + header.cert_size, CIA_ALIGNMENT);
|
||||||
if (!WriteTicket(config.ticket_db_path, config.enc_title_keys_bin_path)) {
|
if (!WriteTicket(config.ticket_db_path, config.enc_title_keys_bin_path)) {
|
||||||
file.reset();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -219,7 +215,6 @@ bool CIABuilder::WriteTicket(const std::string& ticket_db_path,
|
|||||||
file->Seek(ticket_offset, SEEK_SET);
|
file->Seek(ticket_offset, SEEK_SET);
|
||||||
if (!ticket.Save(*file)) {
|
if (!ticket.Save(*file)) {
|
||||||
LOG_ERROR(Core, "Could not write ticket");
|
LOG_ERROR(Core, "Could not write ticket");
|
||||||
file.reset();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
return true;
|
return true;
|
||||||
@@ -273,7 +268,6 @@ bool CIABuilder::AddContent(u16 content_id, NCCHContainer& ncch) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
if (ret != ResultStatus::Success) {
|
if (ret != ResultStatus::Success) {
|
||||||
file.reset();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
file->GetHash(tmd_chunk.hash.data());
|
file->GetHash(tmd_chunk.hash.data());
|
||||||
@@ -300,7 +294,6 @@ bool CIABuilder::AddContent(u16 content_id, NCCHContainer& ncch) {
|
|||||||
if (!decryptor.CryptAndWriteFile(ncch.file, ncch.file->GetSize(), file,
|
if (!decryptor.CryptAndWriteFile(ncch.file, ncch.file->GetSize(), file,
|
||||||
progress_callback)) {
|
progress_callback)) {
|
||||||
|
|
||||||
file.reset();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -314,7 +307,6 @@ bool CIABuilder::AddContent(u16 content_id, NCCHContainer& ncch) {
|
|||||||
}
|
}
|
||||||
if (!verified) {
|
if (!verified) {
|
||||||
LOG_ERROR(Core, "Hash dismatch for content {}", content_id);
|
LOG_ERROR(Core, "Hash dismatch for content {}", content_id);
|
||||||
file.reset();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -355,7 +347,6 @@ bool CIABuilder::Finalize() {
|
|||||||
file->Seek(0, SEEK_SET);
|
file->Seek(0, SEEK_SET);
|
||||||
if (file->WriteBytes(&header, sizeof(header)) != sizeof(header)) {
|
if (file->WriteBytes(&header, sizeof(header)) != sizeof(header)) {
|
||||||
LOG_ERROR(Core, "Failed to write header");
|
LOG_ERROR(Core, "Failed to write header");
|
||||||
file.reset();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -365,7 +356,6 @@ bool CIABuilder::Finalize() {
|
|||||||
}
|
}
|
||||||
file->Seek(tmd_offset, SEEK_SET);
|
file->Seek(tmd_offset, SEEK_SET);
|
||||||
if (tmd.Save(*file) != ResultStatus::Success) {
|
if (tmd.Save(*file) != ResultStatus::Success) {
|
||||||
file.reset();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -374,13 +364,11 @@ bool CIABuilder::Finalize() {
|
|||||||
file->Seek(written, SEEK_SET);
|
file->Seek(written, SEEK_SET);
|
||||||
if (file->WriteBytes(&meta, sizeof(meta)) != sizeof(meta)) {
|
if (file->WriteBytes(&meta, sizeof(meta)) != sizeof(meta)) {
|
||||||
LOG_ERROR(Core, "Failed to write meta");
|
LOG_ERROR(Core, "Failed to write meta");
|
||||||
file.reset();
|
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
callback(total_size, total_size);
|
callback(total_size, total_size);
|
||||||
file.reset();
|
|
||||||
return true;
|
return true;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
Reference in New Issue
Block a user