summaryrefslogtreecommitdiff
path: root/src
diff options
context:
space:
mode:
authorGravatar Morph2020-07-05 09:37:50 -0400
committerGravatar Morph2020-07-15 13:27:04 -0400
commita82fdea1ac84693288ed5656f3ff89ceaaaeea1b (patch)
treeb2b362dce4013d5254d06ccfa105ea83956d84e2 /src
parentMerge pull request #4342 from lioncash/endian (diff)
downloadyuzu-a82fdea1ac84693288ed5656f3ff89ceaaaeea1b.tar.gz
yuzu-a82fdea1ac84693288ed5656f3ff89ceaaaeea1b.tar.xz
yuzu-a82fdea1ac84693288ed5656f3ff89ceaaaeea1b.zip
registered_cache: Remove previous update/dlc if it exists on install
- This checks for and removes old updates or dlc based on title id. If a content meta nca exists within the registered cache, it will attempt to remove all the ncas associated with the content meta before installing a new update/dlc
Diffstat (limited to 'src')
-rw-r--r--src/core/file_sys/registered_cache.cpp86
-rw-r--r--src/core/file_sys/registered_cache.h10
2 files changed, 83 insertions, 13 deletions
diff --git a/src/core/file_sys/registered_cache.cpp b/src/core/file_sys/registered_cache.cpp
index 27c1b0233..39bfbdfd3 100644
--- a/src/core/file_sys/registered_cache.cpp
+++ b/src/core/file_sys/registered_cache.cpp
@@ -547,6 +547,57 @@ InstallResult RegisteredCache::InstallEntry(const XCI& xci, bool overwrite_if_ex
547 return InstallEntry(*xci.GetSecurePartitionNSP(), overwrite_if_exists, copy); 547 return InstallEntry(*xci.GetSecurePartitionNSP(), overwrite_if_exists, copy);
548} 548}
549 549
550bool RegisteredCache::RemoveExistingEntry(const u64 title_id) {
551 const auto delete_nca = [this](const NcaID& id) {
552 const auto path = GetRelativePathFromNcaID(id, false, true, false);
553
554 if (dir->GetFileRelative(path) == nullptr) {
555 return false;
556 }
557
558 Core::Crypto::SHA256Hash hash{};
559 mbedtls_sha256_ret(id.data(), id.size(), hash.data(), 0);
560 const auto dirname = fmt::format("000000{:02X}", hash[0]);
561
562 const auto dir2 = GetOrCreateDirectoryRelative(dir, dirname);
563
564 const auto res = dir2->DeleteFile(fmt::format("{}.nca", Common::HexToString(id, false)));
565
566 return res;
567 };
568
569 // Get the Content Provider
570 const auto& installed = Core::System::GetInstance().GetContentProvider();
571 // If an update exists, remove
572 if (installed.HasEntry(title_id, ContentRecordType::Meta)) {
573 LOG_INFO(Loader,
574 "Previous Update (v{}) for title_id={:016X} detected! Attempting to remove...",
575 installed.GetEntryVersion(title_id).value_or(0), title_id);
576 // Get all the ncas associated with the current update CNMT and delete them
577 const auto& meta_old_id =
578 GetNcaIDFromMetadata(title_id, ContentRecordType::Meta).value_or(NcaID{});
579 const auto& program_id =
580 GetNcaIDFromMetadata(title_id, ContentRecordType::Program).value_or(NcaID{});
581 const auto& data_id =
582 GetNcaIDFromMetadata(title_id, ContentRecordType::Data).value_or(NcaID{});
583 const auto& control_id =
584 GetNcaIDFromMetadata(title_id, ContentRecordType::Control).value_or(NcaID{});
585 const auto& html_id =
586 GetNcaIDFromMetadata(title_id, ContentRecordType::HtmlDocument).value_or(NcaID{});
587 const auto& legal_id =
588 GetNcaIDFromMetadata(title_id, ContentRecordType::LegalInformation).value_or(NcaID{});
589
590 delete_nca(meta_old_id);
591 delete_nca(program_id);
592 delete_nca(data_id);
593 delete_nca(control_id);
594 delete_nca(html_id);
595 delete_nca(legal_id);
596 return true;
597 }
598 return false;
599}
600
550InstallResult RegisteredCache::InstallEntry(const NSP& nsp, bool overwrite_if_exists, 601InstallResult RegisteredCache::InstallEntry(const NSP& nsp, bool overwrite_if_exists,
551 const VfsCopyFunction& copy) { 602 const VfsCopyFunction& copy) {
552 const auto ncas = nsp.GetNCAsCollapsed(); 603 const auto ncas = nsp.GetNCAsCollapsed();
@@ -560,31 +611,44 @@ InstallResult RegisteredCache::InstallEntry(const NSP& nsp, bool overwrite_if_ex
560 return InstallResult::ErrorMetaFailed; 611 return InstallResult::ErrorMetaFailed;
561 } 612 }
562 613
563 // Install Metadata File
564 const auto meta_id_raw = (*meta_iter)->GetName().substr(0, 32); 614 const auto meta_id_raw = (*meta_iter)->GetName().substr(0, 32);
565 const auto meta_id = Common::HexStringToArray<16>(meta_id_raw); 615 const auto meta_id = Common::HexStringToArray<16>(meta_id_raw);
566 616
617 const auto section0 = (*meta_iter)->GetSubdirectories()[0];
618 const auto cnmt_file = section0->GetFiles()[0];
619 const CNMT cnmt(cnmt_file);
620
621 // Get the title id stored within the CNMT
622 const auto title_id = cnmt.GetTitleID();
623 // Removes an entry if it exists
624 const auto result = RemoveExistingEntry(title_id);
625
626 // Install Metadata File
567 const auto res = RawInstallNCA(**meta_iter, copy, overwrite_if_exists, meta_id); 627 const auto res = RawInstallNCA(**meta_iter, copy, overwrite_if_exists, meta_id);
568 if (res != InstallResult::Success) 628 if (res != InstallResult::Success) {
569 return res; 629 return res;
630 }
570 631
571 // Install all the other NCAs 632 // Install all the other NCAs
572 const auto section0 = (*meta_iter)->GetSubdirectories()[0];
573 const auto cnmt_file = section0->GetFiles()[0];
574 const CNMT cnmt(cnmt_file);
575 for (const auto& record : cnmt.GetContentRecords()) { 633 for (const auto& record : cnmt.GetContentRecords()) {
576 // Ignore DeltaFragments, they are not useful to us 634 // Ignore DeltaFragments, they are not useful to us
577 if (record.type == ContentRecordType::DeltaFragment) 635 if (record.type == ContentRecordType::DeltaFragment) {
578 continue; 636 continue;
637 }
579 const auto nca = GetNCAFromNSPForID(nsp, record.nca_id); 638 const auto nca = GetNCAFromNSPForID(nsp, record.nca_id);
580 if (nca == nullptr) 639 if (nca == nullptr) {
581 return InstallResult::ErrorCopyFailed; 640 return InstallResult::ErrorCopyFailed;
641 }
582 const auto res2 = RawInstallNCA(*nca, copy, overwrite_if_exists, record.nca_id); 642 const auto res2 = RawInstallNCA(*nca, copy, overwrite_if_exists, record.nca_id);
583 if (res2 != InstallResult::Success) 643 if (res2 != InstallResult::Success) {
584 return res2; 644 return res2;
645 }
585 } 646 }
586 647
587 Refresh(); 648 Refresh();
649 if (result) {
650 return InstallResult::ErrorAlreadyExists;
651 }
588 return InstallResult::Success; 652 return InstallResult::Success;
589} 653}
590 654
@@ -610,8 +674,9 @@ InstallResult RegisteredCache::InstallEntry(const NCA& nca, TitleType type,
610 mbedtls_sha256_ret(data.data(), data.size(), c_rec.hash.data(), 0); 674 mbedtls_sha256_ret(data.data(), data.size(), c_rec.hash.data(), 0);
611 memcpy(&c_rec.nca_id, &c_rec.hash, 16); 675 memcpy(&c_rec.nca_id, &c_rec.hash, 16);
612 const CNMT new_cnmt(header, opt_header, {c_rec}, {}); 676 const CNMT new_cnmt(header, opt_header, {c_rec}, {});
613 if (!RawInstallYuzuMeta(new_cnmt)) 677 if (!RawInstallYuzuMeta(new_cnmt)) {
614 return InstallResult::ErrorMetaFailed; 678 return InstallResult::ErrorMetaFailed;
679 }
615 return RawInstallNCA(nca, copy, overwrite_if_exists, c_rec.nca_id); 680 return RawInstallNCA(nca, copy, overwrite_if_exists, c_rec.nca_id);
616} 681}
617 682
@@ -649,8 +714,9 @@ InstallResult RegisteredCache::RawInstallNCA(const NCA& nca, const VfsCopyFuncti
649 } 714 }
650 715
651 auto out = dir->CreateFileRelative(path); 716 auto out = dir->CreateFileRelative(path);
652 if (out == nullptr) 717 if (out == nullptr) {
653 return InstallResult::ErrorCopyFailed; 718 return InstallResult::ErrorCopyFailed;
719 }
654 return copy(in, out, VFS_RC_LARGE_COPY_BLOCK) ? InstallResult::Success 720 return copy(in, out, VFS_RC_LARGE_COPY_BLOCK) ? InstallResult::Success
655 : InstallResult::ErrorCopyFailed; 721 : InstallResult::ErrorCopyFailed;
656} 722}
diff --git a/src/core/file_sys/registered_cache.h b/src/core/file_sys/registered_cache.h
index f339cd17b..8598f0543 100644
--- a/src/core/file_sys/registered_cache.h
+++ b/src/core/file_sys/registered_cache.h
@@ -34,6 +34,7 @@ using VfsCopyFunction = std::function<bool(const VirtualFile&, const VirtualFile
34 34
35enum class InstallResult { 35enum class InstallResult {
36 Success, 36 Success,
37 OverwriteExisting,
37 ErrorAlreadyExists, 38 ErrorAlreadyExists,
38 ErrorCopyFailed, 39 ErrorCopyFailed,
39 ErrorMetaFailed, 40 ErrorMetaFailed,
@@ -132,9 +133,9 @@ public:
132 // Parsing function defines the conversion from raw file to NCA. If there are other steps 133 // Parsing function defines the conversion from raw file to NCA. If there are other steps
133 // besides creating the NCA from the file (e.g. NAX0 on SD Card), that should go in a custom 134 // besides creating the NCA from the file (e.g. NAX0 on SD Card), that should go in a custom
134 // parsing function. 135 // parsing function.
135 explicit RegisteredCache(VirtualDir dir, 136 explicit RegisteredCache(
136 ContentProviderParsingFunction parsing_function = 137 VirtualDir dir, ContentProviderParsingFunction parsing_function =
137 [](const VirtualFile& file, const NcaID& id) { return file; }); 138 [](const VirtualFile& file, const NcaID& id) { return file; });
138 ~RegisteredCache() override; 139 ~RegisteredCache() override;
139 140
140 void Refresh() override; 141 void Refresh() override;
@@ -154,6 +155,9 @@ public:
154 std::optional<TitleType> title_type = {}, std::optional<ContentRecordType> record_type = {}, 155 std::optional<TitleType> title_type = {}, std::optional<ContentRecordType> record_type = {},
155 std::optional<u64> title_id = {}) const override; 156 std::optional<u64> title_id = {}) const override;
156 157
158 // Removes an existing entry based on title id
159 bool RemoveExistingEntry(const u64 title_id);
160
157 // Raw copies all the ncas from the xci/nsp to the csache. Does some quick checks to make sure 161 // Raw copies all the ncas from the xci/nsp to the csache. Does some quick checks to make sure
158 // there is a meta NCA and all of them are accessible. 162 // there is a meta NCA and all of them are accessible.
159 InstallResult InstallEntry(const XCI& xci, bool overwrite_if_exists = false, 163 InstallResult InstallEntry(const XCI& xci, bool overwrite_if_exists = false,