138 lines
5.5 KiB
C++
138 lines
5.5 KiB
C++
#pragma once
|
|
#include <algorithm>
|
|
#include <cstdint>
|
|
#include <map>
|
|
#include <set>
|
|
#include <stdexcept>
|
|
#include <string>
|
|
#include <vector>
|
|
|
|
namespace current_files {
|
|
inline std::string path_key(std::string path) {
|
|
for (char& ch : path) {
|
|
if (ch == '\\') ch = '/';
|
|
else if (ch >= 'A' && ch <= 'Z') ch += 'a' - 'A';
|
|
}
|
|
return path;
|
|
}
|
|
|
|
// Declarations use canonical manifest paths; older PUT and install rows can use
|
|
// either Windows separator. A final slash declares only files below that directory.
|
|
class RetiredPaths {
|
|
std::set<std::string> paths;
|
|
public:
|
|
bool empty() const { return paths.empty(); }
|
|
|
|
bool add(const std::string& path) {
|
|
if (path.empty() || path.find_first_of(R"(<>:"\|?*)") != std::string::npos ||
|
|
std::any_of(path.begin(),path.end(),[](unsigned char ch){ return ch < 0x21 || ch > 0x7e; }))
|
|
return false;
|
|
const auto key = path_key(path);
|
|
for (size_t start = 0; start < key.size();) {
|
|
const auto slash = key.find('/',start);
|
|
const auto part = key.substr(start,slash - start);
|
|
if (part.empty() || part.back() == '.') return false;
|
|
if (start == 0 && (part == "data" || part == "_patch" || part == ".build.info" ||
|
|
part == "octoupdater.exe" || part == "octoupdater.cfg")) return false;
|
|
const auto stem = part.substr(0,part.find('.'));
|
|
if (stem == "con" || stem == "prn" || stem == "aux" || stem == "nul" ||
|
|
stem == "conin$" || stem == "conout$" ||
|
|
(stem.size() == 4 && (stem.starts_with("com") || stem.starts_with("lpt")) &&
|
|
stem[3] >= '1' && stem[3] <= '9')) return false;
|
|
if (slash == std::string::npos) break;
|
|
start = slash + 1;
|
|
}
|
|
paths.insert(key);
|
|
return true;
|
|
}
|
|
|
|
bool matches(const std::string& file) const {
|
|
if (empty()) return false;
|
|
// Historical rows can use Windows aliases that resolve to the same
|
|
// installed file. Normalize for comparison only; never resolve "..".
|
|
const auto folded = path_key(file);
|
|
std::string key;
|
|
for (size_t start = 0; start < folded.size();) {
|
|
auto slash = folded.find('/',start);
|
|
auto part = folded.substr(start,slash - start);
|
|
if (part != "..") part.erase(part.find_last_not_of(" .") + 1);
|
|
if (!part.empty()) {
|
|
if (!key.empty()) key += '/';
|
|
key += part;
|
|
}
|
|
if (slash == std::string::npos) break;
|
|
start = slash + 1;
|
|
}
|
|
if (paths.contains(key)) return true;
|
|
for (auto slash = key.find('/'); slash != std::string::npos; slash = key.find('/',slash+1)) {
|
|
if (paths.contains(key.substr(0,slash+1))) return true;
|
|
}
|
|
return false;
|
|
}
|
|
};
|
|
|
|
struct Output {
|
|
std::string path, sha, blob, step;
|
|
uint64_t size, blob_size;
|
|
};
|
|
using Outputs = std::map<std::string, Output>;
|
|
constexpr uint64_t unknown_size = UINT64_MAX; // ACTIVATE has no decoded size field
|
|
|
|
template<class Steps>
|
|
Outputs file_outputs(const Steps& steps, const std::set<std::string>& applied_ids) {
|
|
Outputs out;
|
|
for (const auto& step : steps) {
|
|
if (step.kind != "FILES" || !applied_ids.count(step.id)) continue;
|
|
for (const auto& v : step.lines) if (v.at(0) == "PUT") {
|
|
out[path_key(v.at(1))] = {v[1], v.at(2), v.at(4), step.id,
|
|
std::stoull(v.at(3)), std::stoull(v.at(5))};
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
template<class Steps>
|
|
std::vector<const typename Steps::value_type*> lineage(const Steps& steps, std::string active_key) {
|
|
using Step = typename Steps::value_type;
|
|
std::map<std::string, const Step*> children;
|
|
for (const auto& step : steps) {
|
|
if (step.kind == "STORE" || (step.kind == "CONTENT" && !children.count(step.child)))
|
|
children[step.child] = &step;
|
|
}
|
|
std::vector<const Step*> chain;
|
|
std::set<std::string> seen;
|
|
while (children.count(active_key)) {
|
|
if (!seen.insert(active_key).second) throw std::runtime_error("cyclic signed build history");
|
|
const auto* step = children.at(active_key);
|
|
chain.push_back(step);
|
|
active_key = step->parent;
|
|
}
|
|
std::reverse(chain.begin(), chain.end());
|
|
return chain;
|
|
}
|
|
|
|
template<class Steps>
|
|
Outputs content_outputs(const Steps& steps, const std::string& active_key) {
|
|
Outputs out;
|
|
for (const auto* step : lineage(steps, active_key)) {
|
|
for (const auto& v : step->lines) {
|
|
if (v.at(0) == "ACTIVATE") {
|
|
out[path_key(v.at(1))] = {v[1], v.at(3), v.at(4), step->id, unknown_size, std::stoull(v.at(5))};
|
|
} else if (v[0] == "CONF" || (v[0] == "FILE" && path_key(v.at(1)).rfind("data/data/",0) != 0)) {
|
|
if (v[0] == "FILE" && v.at(6) != "zstd")
|
|
throw std::runtime_error("repair has no standalone payload for " + v[1]);
|
|
out[path_key(v.at(1))] = {v[1], v.at(2), v.at(4), step->id,
|
|
std::stoull(v.at(3)), std::stoull(v.at(5))};
|
|
}
|
|
}
|
|
}
|
|
return out;
|
|
}
|
|
|
|
inline bool preserve_base_path(const std::string& key) {
|
|
return key == ".build.info" || key == "octoupdater.cfg" || key.rfind("_patch/",0) == 0 ||
|
|
key.rfind("data/data/",0) == 0 || key.rfind("_classic_beta_/wtf/",0) == 0 ||
|
|
key.rfind("_classic_beta_/cache/",0) == 0 || key.rfind("_classic_beta_/logs/",0) == 0;
|
|
}
|
|
}
|