boxcat: Use Etag header names for file digest

This commit is contained in:
Zach Hilman 2019-05-06 18:47:27 -04:00
parent e8183f9ef0
commit 92b70a3bf9
2 changed files with 21 additions and 24 deletions

View file

@ -15,25 +15,25 @@ VirtualDir ExtractZIP(VirtualFile file) {
zip_error_t error{}; zip_error_t error{};
const auto data = file->ReadAllBytes(); const auto data = file->ReadAllBytes();
const auto src = zip_source_buffer_create(data.data(), data.size(), 0, &error); std::unique_ptr<zip_source_t, decltype(&zip_source_free)> src{
zip_source_buffer_create(data.data(), data.size(), 0, &error), zip_source_free};
if (src == nullptr) if (src == nullptr)
return nullptr; return nullptr;
const auto zip = zip_open_from_source(src, 0, &error); std::unique_ptr<zip_t, decltype(&zip_discard)> zip{zip_open_from_source(src.get(), 0, &error),
zip_discard};
if (zip == nullptr) if (zip == nullptr)
return nullptr; return nullptr;
std::shared_ptr<VectorVfsDirectory> out = std::make_shared<VectorVfsDirectory>(); std::shared_ptr<VectorVfsDirectory> out = std::make_shared<VectorVfsDirectory>();
const auto num_entries = zip_get_num_entries(zip, 0); const auto num_entries = zip_get_num_entries(zip.get(), 0);
if (num_entries == -1)
return nullptr;
zip_stat_t stat{}; zip_stat_t stat{};
zip_stat_init(&stat); zip_stat_init(&stat);
for (std::size_t i = 0; i < num_entries; ++i) { 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) if (stat_res == -1)
return nullptr; return nullptr;
@ -41,15 +41,14 @@ VirtualDir ExtractZIP(VirtualFile file) {
if (name.empty()) if (name.empty())
continue; continue;
if (name[name.size() - 1] != '/') { if (name.back() != '/') {
const auto file = zip_fopen_index(zip, i, 0); std::unique_ptr<zip_file_t, decltype(&zip_fclose)> file{
zip_fopen_index(zip.get(), i, 0), zip_fclose};
std::vector<u8> buf(stat.size); std::vector<u8> 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; return nullptr;
zip_fclose(file);
const auto parts = FileUtil::SplitPathComponents(stat.name); const auto parts = FileUtil::SplitPathComponents(stat.name);
const auto new_file = std::make_shared<VectorVfsFile>(buf, parts.back()); const auto new_file = std::make_shared<VectorVfsFile>(buf, parts.back());
@ -74,9 +73,6 @@ VirtualDir ExtractZIP(VirtualFile file) {
} }
} }
zip_source_close(src);
zip_close(zip);
return out; return out;
} }

View file

@ -111,18 +111,16 @@ public:
DownloadResult DownloadDataZip() { DownloadResult DownloadDataZip() {
return DownloadInternal(fmt::format(BOXCAT_PATHNAME_DATA, title_id), TIMEOUT_SECONDS, return DownloadInternal(fmt::format(BOXCAT_PATHNAME_DATA, title_id), TIMEOUT_SECONDS,
"Boxcat-Data-Digest", "application/zip"); "application/zip");
} }
DownloadResult DownloadLaunchParam() { DownloadResult DownloadLaunchParam() {
return DownloadInternal(fmt::format(BOXCAT_PATHNAME_LAUNCHPARAM, title_id), return DownloadInternal(fmt::format(BOXCAT_PATHNAME_LAUNCHPARAM, title_id),
TIMEOUT_SECONDS / 3, "Boxcat-LaunchParam-Digest", TIMEOUT_SECONDS / 3, "application/octet-stream");
"application/octet-stream");
} }
private: private:
DownloadResult DownloadInternal(const std::string& resolved_path, u32 timeout_seconds, DownloadResult DownloadInternal(const std::string& resolved_path, u32 timeout_seconds,
const std::string& digest_header_name,
const std::string& content_type_name) { const std::string& content_type_name) {
if (client == nullptr) { if (client == nullptr) {
client = std::make_unique<httplib::SSLClient>(BOXCAT_HOSTNAME, PORT, timeout_seconds); client = std::make_unique<httplib::SSLClient>(BOXCAT_HOSTNAME, PORT, timeout_seconds);
@ -136,10 +134,13 @@ private:
if (FileUtil::Exists(path)) { if (FileUtil::Exists(path)) {
FileUtil::IOFile file{path, "rb"}; FileUtil::IOFile file{path, "rb"};
std::vector<u8> bytes(file.GetSize()); if (file.IsOpen()) {
file.ReadBytes(bytes.data(), bytes.size()); std::vector<u8> bytes(file.GetSize());
const auto digest = DigestFile(bytes); file.ReadBytes(bytes.data(), bytes.size());
headers.insert({digest_header_name, Common::HexArrayToString(digest, false)}); 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); 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"}; FileUtil::IOFile zip{zip_path, "rb"};
const auto size = zip.GetSize(); const auto size = zip.GetSize();
std::vector<u8> bytes(size); std::vector<u8> 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); LOG_ERROR(Service_BCAT, "Boxcat failed to read ZIP file at path '{}'!", zip_path);
failure(); failure();
return; return;
@ -335,7 +336,7 @@ std::optional<std::vector<u8>> Boxcat::GetLaunchParameter(TitleIDVersion title)
FileUtil::IOFile bin{path, "rb"}; FileUtil::IOFile bin{path, "rb"};
const auto size = bin.GetSize(); const auto size = bin.GetSize();
std::vector<u8> bytes(size); std::vector<u8> 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 '{}'!", LOG_ERROR(Service_BCAT, "Boxcat failed to read launch parameter binary at path '{}'!",
path); path);
return std::nullopt; return std::nullopt;