122 lines
5.3 KiB
C++
122 lines
5.3 KiB
C++
#pragma once
|
|
|
|
#include "current_files.hpp"
|
|
#include <filesystem>
|
|
#include <fstream>
|
|
|
|
namespace stock_base {
|
|
|
|
inline bool archive_name(const std::string& name) {
|
|
return name.starts_with("data.") && name.size() >= 8 &&
|
|
name.find_first_not_of("0123456789", 5) == std::string::npos;
|
|
}
|
|
|
|
inline bool index_name(const std::string& name) {
|
|
return name.size() == 14 && name[0] == '0' && name.ends_with(".idx") &&
|
|
name.substr(0, 10).find_first_not_of("0123456789abcdef") == std::string::npos;
|
|
}
|
|
|
|
// Recognize only damage that rules out a usable alternate CASC index. Leave
|
|
// structurally plausible indexes with a different SHA in their current layout.
|
|
inline bool truncated_index(const std::filesystem::path& path) {
|
|
std::error_code ec;
|
|
const auto size = std::filesystem::file_size(path, ec);
|
|
if (ec) return false;
|
|
if (size < 40) return true;
|
|
unsigned char head[40]{};
|
|
std::ifstream input(path, std::ios::binary);
|
|
if (!input.read(reinterpret_cast<char*>(head), sizeof head)) return false;
|
|
if (head[0] != 16 || head[1] || head[2] || head[3] || head[8] != 7 ||
|
|
head[12] != 4 || head[13] != 5 || head[14] != 9 || head[15] != 30) return false;
|
|
const auto extent = uint32_t(head[32]) | uint32_t(head[33]) << 8 |
|
|
uint32_t(head[34]) << 16 | uint32_t(head[35]) << 24;
|
|
return uint64_t(extent) + sizeof head > size;
|
|
}
|
|
|
|
// Matches must check the file's actual size and SHA against its signed install row.
|
|
// When the caller independently identifies a missing base marker, all 16 selected
|
|
// indexes must still be present and exact. A build key alone is insufficient.
|
|
template<class Files, class Matches>
|
|
std::set<std::string> repairable(const Files& files, const std::filesystem::path& data,
|
|
Matches matches, bool require_marker = true) {
|
|
using File = typename Files::value_type;
|
|
constexpr char prefix[] = "data/data/";
|
|
const File* marker = nullptr;
|
|
std::map<std::string, const File*> rows;
|
|
std::map<std::string, std::string> selected;
|
|
std::set<std::string> archives;
|
|
for (const auto& file : files) {
|
|
auto key = current_files::path_key(file.path);
|
|
if (key == ".build.info") {
|
|
if (marker) return {};
|
|
marker = &file;
|
|
}
|
|
if (!key.starts_with(prefix)) continue;
|
|
auto name = key.substr(sizeof(prefix)-1);
|
|
if (name.find('/') != std::string::npos) continue;
|
|
if (name.ends_with(".idx") && !index_name(name)) return {};
|
|
if (!archive_name(name) && !index_name(name) && !name.ends_with(".lru") && name != "shmem") continue;
|
|
if (!rows.emplace(name, &file).second) return {};
|
|
if (archive_name(name)) archives.insert(name);
|
|
if (index_name(name)) {
|
|
auto& newest = selected[name.substr(0, 2)];
|
|
newest = std::max(newest, name);
|
|
}
|
|
}
|
|
if (!marker || selected.size() != 16 || archives.empty() || (require_marker && !matches(*marker))) return {};
|
|
|
|
std::set<std::string> present;
|
|
bool other_older_index = false;
|
|
std::error_code ec;
|
|
std::filesystem::directory_iterator entries(data, ec), end;
|
|
for (; !ec && entries != end; entries.increment(ec)) {
|
|
auto name = current_files::path_key(entries->path().filename().string());
|
|
if (name.ends_with(".idx")) {
|
|
if (!index_name(name) || name > selected.at(name.substr(0, 2))) return {};
|
|
if (!rows.count(name)) other_older_index = true;
|
|
} else if (archive_name(name) && !archives.count(name)) {
|
|
return {};
|
|
}
|
|
if (!rows.count(name)) continue;
|
|
auto status = entries->symlink_status(ec);
|
|
if (ec || !std::filesystem::is_regular_file(status) || !present.insert(name).second) return {};
|
|
}
|
|
if (ec) return {};
|
|
|
|
size_t witnessed = 0;
|
|
std::set<std::string> truncated;
|
|
for (const auto& [bucket, name] : selected) {
|
|
if (!present.count(name)) continue;
|
|
if (matches(*rows.at(name))) ++witnessed;
|
|
else if (truncated_index(data / name)) truncated.insert(name);
|
|
else return {};
|
|
}
|
|
std::set<std::string> eligible;
|
|
if (witnessed == 16) {
|
|
// All active indexes select the stock physical offsets. Archives can
|
|
// be restored even when truncated, corrupted, or entirely absent.
|
|
for (const auto& [name, file] : rows) eligible.insert(prefix + name);
|
|
return eligible;
|
|
}
|
|
|
|
// Missing selected indexes remove some of the physical-layout evidence.
|
|
// Fill absences only if every surviving signed archive and index agrees.
|
|
// With no remaining selected index, require the entire signed archive set.
|
|
if (!require_marker || other_older_index) return {};
|
|
size_t found_archives = 0;
|
|
for (const auto& name : present) {
|
|
if (archive_name(name)) {
|
|
if (!matches(*rows.at(name))) return {};
|
|
++found_archives;
|
|
} else if (index_name(name) && name != selected.at(name.substr(0, 2)) && !matches(*rows.at(name))) {
|
|
return {};
|
|
}
|
|
}
|
|
if (!found_archives || ((!witnessed || !truncated.empty()) && found_archives != archives.size())) return {};
|
|
for (const auto& [name, file] : rows)
|
|
if (!present.count(name) || truncated.count(name)) eligible.insert(prefix + name);
|
|
return eligible;
|
|
}
|
|
|
|
} // namespace stock_base
|