am: Fix too much file handle cause memory leak

This commit is contained in:
R-YaTian 2024-09-29 18:06:20 +08:00 committed by GitHub
parent 7d00f47c5e
commit c1e8600e94
No known key found for this signature in database
GPG key ID: B5690EEEBB952194

View file

@ -194,6 +194,8 @@ Result CIAFile::WriteTitleMetadata() {
// TODO: Correct error code. // TODO: Correct error code.
return FileSys::ResultFileNotFound; return FileSys::ResultFileNotFound;
} }
if (content_count > 255)
file.Close();
} }
if (container.GetTitleMetadata().HasEncryptedContent()) { if (container.GetTitleMetadata().HasEncryptedContent()) {
@ -224,6 +226,8 @@ ResultVal<std::size_t> CIAFile::WriteContentData(u64 offset, std::size_t length,
// has been written since we might get a written buffer which contains multiple .app // has been written since we might get a written buffer which contains multiple .app
// contents or only part of a larger .app's contents. // contents or only part of a larger .app's contents.
const u64 offset_max = offset + length; const u64 offset_max = offset + length;
if (content_written.size() > 255)
content_files.clear();
for (std::size_t i = 0; i < content_written.size(); i++) { for (std::size_t i = 0; i < content_written.size(); i++) {
if (content_written[i] < container.GetContentSize(i)) { if (content_written[i] < container.GetContentSize(i)) {
// The size, minimum unwritten offset, and maximum unwritten offset of this content // The size, minimum unwritten offset, and maximum unwritten offset of this content
@ -243,7 +247,9 @@ ResultVal<std::size_t> CIAFile::WriteContentData(u64 offset, std::size_t length,
// Since the incoming TMD has already been written, we can use GetTitleContentPath // Since the incoming TMD has already been written, we can use GetTitleContentPath
// to get the content paths to write to. // to get the content paths to write to.
FileSys::TitleMetadata tmd = container.GetTitleMetadata(); FileSys::TitleMetadata tmd = container.GetTitleMetadata();
auto& file = content_files[i]; auto path = GetTitleContentPath(media_type, tmd.GetTitleID(), i, is_update);
auto& file = content_written.size() > 255 ? content_files.emplace_back(path, "ab+")
: content_files[i];
std::vector<u8> temp(buffer + (range_min - offset), std::vector<u8> temp(buffer + (range_min - offset),
buffer + (range_min - offset) + available_to_write); buffer + (range_min - offset) + available_to_write);
@ -259,6 +265,8 @@ ResultVal<std::size_t> CIAFile::WriteContentData(u64 offset, std::size_t length,
content_written[i] += available_to_write; content_written[i] += available_to_write;
LOG_DEBUG(Service_AM, "Wrote {:x} to content {}, total {:x}", available_to_write, i, LOG_DEBUG(Service_AM, "Wrote {:x} to content {}, total {:x}", available_to_write, i,
content_written[i]); content_written[i]);
if (content_written.size() > 255)
file.Close();
} }
} }