From 92b70a3bf9d4657dccc5a5f2cffdd7789946ca14 Mon Sep 17 00:00:00 2001 From: Zach Hilman Date: Mon, 6 May 2019 18:47:27 -0400 Subject: [PATCH] boxcat: Use Etag header names for file digest --- src/core/file_sys/vfs_libzip.cpp | 24 ++++++++------------ src/core/hle/service/bcat/backend/boxcat.cpp | 21 +++++++++-------- 2 files changed, 21 insertions(+), 24 deletions(-) diff --git a/src/core/file_sys/vfs_libzip.cpp b/src/core/file_sys/vfs_libzip.cpp index 64f19a0ea..e34474ae0 100644 --- a/src/core/file_sys/vfs_libzip.cpp +++ b/src/core/file_sys/vfs_libzip.cpp @@ -15,25 +15,25 @@ VirtualDir ExtractZIP(VirtualFile file) { zip_error_t error{}; const auto data = file->ReadAllBytes(); - const auto src = zip_source_buffer_create(data.data(), data.size(), 0, &error); + std::unique_ptr src{ + zip_source_buffer_create(data.data(), data.size(), 0, &error), zip_source_free}; if (src == nullptr) return nullptr; - const auto zip = zip_open_from_source(src, 0, &error); + std::unique_ptr zip{zip_open_from_source(src.get(), 0, &error), + zip_discard}; if (zip == nullptr) return nullptr; std::shared_ptr out = std::make_shared(); - const auto num_entries = zip_get_num_entries(zip, 0); - if (num_entries == -1) - return nullptr; + const auto num_entries = zip_get_num_entries(zip.get(), 0); zip_stat_t stat{}; zip_stat_init(&stat); for (std::size_t i = 0; i < num_entries; ++i) { - const auto stat_res = zip_stat_index(zip, i, 0, &stat); + const auto stat_res = zip_stat_index(zip.get(), i, 0, &stat); if (stat_res == -1) return nullptr; @@ -41,15 +41,14 @@ VirtualDir ExtractZIP(VirtualFile file) { if (name.empty()) continue; - if (name[name.size() - 1] != '/') { - const auto file = zip_fopen_index(zip, i, 0); + if (name.back() != '/') { + std::unique_ptr file{ + zip_fopen_index(zip.get(), i, 0), zip_fclose}; std::vector buf(stat.size); - if (zip_fread(file, buf.data(), buf.size()) != buf.size()) + if (zip_fread(file.get(), buf.data(), buf.size()) != buf.size()) return nullptr; - zip_fclose(file); - const auto parts = FileUtil::SplitPathComponents(stat.name); const auto new_file = std::make_shared(buf, parts.back()); @@ -74,9 +73,6 @@ VirtualDir ExtractZIP(VirtualFile file) { } } - zip_source_close(src); - zip_close(zip); - return out; } diff --git a/src/core/hle/service/bcat/backend/boxcat.cpp b/src/core/hle/service/bcat/backend/boxcat.cpp index f37f92bf4..31d2e045c 100644 --- a/src/core/hle/service/bcat/backend/boxcat.cpp +++ b/src/core/hle/service/bcat/backend/boxcat.cpp @@ -111,18 +111,16 @@ public: DownloadResult DownloadDataZip() { return DownloadInternal(fmt::format(BOXCAT_PATHNAME_DATA, title_id), TIMEOUT_SECONDS, - "Boxcat-Data-Digest", "application/zip"); + "application/zip"); } DownloadResult DownloadLaunchParam() { return DownloadInternal(fmt::format(BOXCAT_PATHNAME_LAUNCHPARAM, title_id), - TIMEOUT_SECONDS / 3, "Boxcat-LaunchParam-Digest", - "application/octet-stream"); + TIMEOUT_SECONDS / 3, "application/octet-stream"); } private: DownloadResult DownloadInternal(const std::string& resolved_path, u32 timeout_seconds, - const std::string& digest_header_name, const std::string& content_type_name) { if (client == nullptr) { client = std::make_unique(BOXCAT_HOSTNAME, PORT, timeout_seconds); @@ -136,10 +134,13 @@ private: if (FileUtil::Exists(path)) { FileUtil::IOFile file{path, "rb"}; - std::vector bytes(file.GetSize()); - file.ReadBytes(bytes.data(), bytes.size()); - const auto digest = DigestFile(bytes); - headers.insert({digest_header_name, Common::HexArrayToString(digest, false)}); + if (file.IsOpen()) { + std::vector bytes(file.GetSize()); + file.ReadBytes(bytes.data(), bytes.size()); + const auto digest = DigestFile(bytes); + headers.insert( + {std::string("If-None-Match"), Common::HexArrayToString(digest, false)}); + } } const auto response = client->Get(resolved_path.c_str(), headers); @@ -227,7 +228,7 @@ void SynchronizeInternal(DirectoryGetter dir_getter, TitleIDVersion title, FileUtil::IOFile zip{zip_path, "rb"}; const auto size = zip.GetSize(); std::vector bytes(size); - if (size == 0 || zip.ReadBytes(bytes.data(), bytes.size()) != bytes.size()) { + if (!zip.IsOpen() || size == 0 || zip.ReadBytes(bytes.data(), bytes.size()) != bytes.size()) { LOG_ERROR(Service_BCAT, "Boxcat failed to read ZIP file at path '{}'!", zip_path); failure(); return; @@ -335,7 +336,7 @@ std::optional> Boxcat::GetLaunchParameter(TitleIDVersion title) FileUtil::IOFile bin{path, "rb"}; const auto size = bin.GetSize(); std::vector bytes(size); - if (size == 0 || bin.ReadBytes(bytes.data(), bytes.size()) != bytes.size()) { + if (!bin.IsOpen() || size == 0 || bin.ReadBytes(bytes.data(), bytes.size()) != bytes.size()) { LOG_ERROR(Service_BCAT, "Boxcat failed to read launch parameter binary at path '{}'!", path); return std::nullopt;