2019-04-29 00:42:05 +02:00
|
|
|
// Copyright 2019 yuzu emulator team
|
|
|
|
// Licensed under GPLv2 or any later version
|
|
|
|
// Refer to the license.txt file included.
|
|
|
|
|
|
|
|
#include <string>
|
2021-05-16 07:46:30 +02:00
|
|
|
|
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic push
|
|
|
|
#pragma GCC diagnostic ignored "-Wshadow"
|
|
|
|
#endif
|
2019-04-29 00:42:05 +02:00
|
|
|
#include <zip.h>
|
2021-05-16 07:46:30 +02:00
|
|
|
#ifdef __GNUC__
|
|
|
|
#pragma GCC diagnostic pop
|
|
|
|
#endif
|
|
|
|
|
2021-05-26 01:32:56 +02:00
|
|
|
#include "common/fs/path_util.h"
|
2019-04-29 00:42:05 +02:00
|
|
|
#include "common/logging/backend.h"
|
|
|
|
#include "core/file_sys/vfs.h"
|
|
|
|
#include "core/file_sys/vfs_libzip.h"
|
|
|
|
#include "core/file_sys/vfs_vector.h"
|
|
|
|
|
|
|
|
namespace FileSys {
|
|
|
|
|
|
|
|
VirtualDir ExtractZIP(VirtualFile file) {
|
|
|
|
zip_error_t error{};
|
|
|
|
|
|
|
|
const auto data = file->ReadAllBytes();
|
2019-05-14 00:51:02 +02:00
|
|
|
std::unique_ptr<zip_source_t, decltype(&zip_source_close)> src{
|
|
|
|
zip_source_buffer_create(data.data(), data.size(), 0, &error), zip_source_close};
|
2019-04-29 00:42:05 +02:00
|
|
|
if (src == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
2019-05-14 00:51:02 +02:00
|
|
|
std::unique_ptr<zip_t, decltype(&zip_close)> zip{zip_open_from_source(src.get(), 0, &error),
|
|
|
|
zip_close};
|
2019-04-29 00:42:05 +02:00
|
|
|
if (zip == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
std::shared_ptr<VectorVfsDirectory> out = std::make_shared<VectorVfsDirectory>();
|
|
|
|
|
2019-11-12 10:47:32 +01:00
|
|
|
const auto num_entries = static_cast<std::size_t>(zip_get_num_entries(zip.get(), 0));
|
2019-04-29 00:42:05 +02:00
|
|
|
|
|
|
|
zip_stat_t stat{};
|
|
|
|
zip_stat_init(&stat);
|
|
|
|
|
|
|
|
for (std::size_t i = 0; i < num_entries; ++i) {
|
2019-05-07 00:47:27 +02:00
|
|
|
const auto stat_res = zip_stat_index(zip.get(), i, 0, &stat);
|
2019-04-29 00:42:05 +02:00
|
|
|
if (stat_res == -1)
|
|
|
|
return nullptr;
|
|
|
|
|
|
|
|
const std::string name(stat.name);
|
|
|
|
if (name.empty())
|
|
|
|
continue;
|
|
|
|
|
2019-05-07 00:47:27 +02:00
|
|
|
if (name.back() != '/') {
|
2020-04-15 21:59:23 +02:00
|
|
|
std::unique_ptr<zip_file_t, decltype(&zip_fclose)> file2{
|
2019-05-07 00:47:27 +02:00
|
|
|
zip_fopen_index(zip.get(), i, 0), zip_fclose};
|
2019-04-29 00:42:05 +02:00
|
|
|
|
|
|
|
std::vector<u8> buf(stat.size);
|
2020-04-15 21:59:23 +02:00
|
|
|
if (zip_fread(file2.get(), buf.data(), buf.size()) != s64(buf.size()))
|
2019-04-29 00:42:05 +02:00
|
|
|
return nullptr;
|
|
|
|
|
2020-08-15 14:33:16 +02:00
|
|
|
const auto parts = Common::FS::SplitPathComponents(stat.name);
|
2019-04-29 00:42:05 +02:00
|
|
|
const auto new_file = std::make_shared<VectorVfsFile>(buf, parts.back());
|
|
|
|
|
|
|
|
std::shared_ptr<VectorVfsDirectory> dtrv = out;
|
|
|
|
for (std::size_t j = 0; j < parts.size() - 1; ++j) {
|
|
|
|
if (dtrv == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
const auto subdir = dtrv->GetSubdirectory(parts[j]);
|
|
|
|
if (subdir == nullptr) {
|
|
|
|
const auto temp = std::make_shared<VectorVfsDirectory>(
|
|
|
|
std::vector<VirtualFile>{}, std::vector<VirtualDir>{}, parts[j]);
|
|
|
|
dtrv->AddDirectory(temp);
|
|
|
|
dtrv = temp;
|
|
|
|
} else {
|
|
|
|
dtrv = std::dynamic_pointer_cast<VectorVfsDirectory>(subdir);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if (dtrv == nullptr)
|
|
|
|
return nullptr;
|
|
|
|
dtrv->AddFile(new_file);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return out;
|
|
|
|
}
|
|
|
|
|
|
|
|
} // namespace FileSys
|