Record the previous generation in the index selector
CURRENT moves to schema 3 with an explicit previous file name. Restore accepts schemas 1-3 (migrating 2), validates the recorded name as a plain file name and recovers from it when the current generation is missing. Registered index tests cover the migration, recovery through a renamed previous generation and rejection of an escaping name.
This commit is contained in:
1 parent
4b534c61ae
commit
75ff3d4a9d
3 files changed
+57
-5
No files matched your search
@@ -61,6 +61,17 @@ start coordinate only, so ranges are zero-width until an end is available.
|
|||||||
LSP adapters and index/rename/debugging integration remain open. `vbs test`
|
LSP adapters and index/rename/debugging integration remain open. `vbs test`
|
||||||
passed all 17 native suites; CMake passed the reflection and trace suites.
|
passed all 17 native suites; CMake passed the reflection and trace suites.
|
||||||
|
|
||||||
|
## December 6 index selector schema 3
|
||||||
|
|
||||||
|
`CURRENT` is now schema 3 (`{"schema":3,"file":...,"previous":...}`), so the
|
||||||
|
recovery generation name is recorded rather than hardcoded. Restore still
|
||||||
|
accepts schema 1 (bare name) and schema 2 (migrating it to 3), validates that the
|
||||||
|
recorded previous name is a plain file name (rejecting separators and `..`), and
|
||||||
|
recovers from it when the current file is missing. Registered `vls_index_tests`
|
||||||
|
grew to 29 cases, including recovery through a renamed previous generation and
|
||||||
|
rejection of an escaping name. All 18 native suites and all 18 CMake tests
|
||||||
|
passed.
|
||||||
|
|
||||||
## December 5 persistent index snapshot migration
|
## December 5 persistent index snapshot migration
|
||||||
|
|
||||||
The symbol-index snapshot is now schema 2 with a canonical payload digest
|
The symbol-index snapshot is now schema 2 with a canonical payload digest
|
||||||
|
|||||||
@@ -660,7 +660,8 @@ bool SymbolIndex::save_generation(const fs::path &root) {
|
|||||||
const auto temporary = root / "CURRENT.tmp";
|
const auto temporary = root / "CURRENT.tmp";
|
||||||
{
|
{
|
||||||
std::ofstream stream{temporary, std::ios::binary | std::ios::trunc};
|
std::ofstream stream{temporary, std::ios::binary | std::ios::trunc};
|
||||||
stream << "{\"schema\":2,\"file\":\"snapshot.json\"}\n";
|
stream << "{\"schema\":3,\"file\":\"snapshot.json\","
|
||||||
|
"\"previous\":\"snapshot.prev.json\"}\n";
|
||||||
}
|
}
|
||||||
fs::rename(temporary, root / "CURRENT", error);
|
fs::rename(temporary, root / "CURRENT", error);
|
||||||
return !error;
|
return !error;
|
||||||
@@ -675,6 +676,7 @@ SymbolIndex::restore_generation(const fs::path &root) {
|
|||||||
const auto legacy = root / "index.json";
|
const auto legacy = root / "index.json";
|
||||||
fs::path selected;
|
fs::path selected;
|
||||||
bool migrate_selector{};
|
bool migrate_selector{};
|
||||||
|
std::string previous_name{"snapshot.prev.json"};
|
||||||
if (fs::exists(current, error)) {
|
if (fs::exists(current, error)) {
|
||||||
std::ifstream stream{current, std::ios::binary};
|
std::ifstream stream{current, std::ios::binary};
|
||||||
std::string line(256, '\0');
|
std::string line(256, '\0');
|
||||||
@@ -693,9 +695,20 @@ SymbolIndex::restore_generation(const fs::path &root) {
|
|||||||
const auto *schema = fields ? P::find_member(*fields, "schema") : nullptr;
|
const auto *schema = fields ? P::find_member(*fields, "schema") : nullptr;
|
||||||
const auto *name = fields ? P::as_string(P::find_member(*fields, "file")) : nullptr;
|
const auto *name = fields ? P::as_string(P::find_member(*fields, "file")) : nullptr;
|
||||||
const auto *value = schema ? std::get_if<std::int64_t>(&schema->value) : nullptr;
|
const auto *value = schema ? std::get_if<std::int64_t>(&schema->value) : nullptr;
|
||||||
if (!fields || !name || !value || *value != 2)
|
if (!fields || !name || !value || (*value != 2 && *value != 3))
|
||||||
return std::unexpected("unsupported index generation selector");
|
return std::unexpected("unsupported index generation selector");
|
||||||
file = *name;
|
file = *name;
|
||||||
|
migrate_selector = *value == 2;
|
||||||
|
if (*value == 3) {
|
||||||
|
if (const auto *recorded = P::find_member(*fields, "previous")) {
|
||||||
|
const auto *recorded_name = P::as_string(recorded);
|
||||||
|
if (!recorded_name || recorded_name->empty() || recorded_name->size() > 256 ||
|
||||||
|
recorded_name->find('/') != std::string::npos ||
|
||||||
|
recorded_name->find("..") != std::string::npos)
|
||||||
|
return std::unexpected("unsupported index generation selector");
|
||||||
|
previous_name = *recorded_name;
|
||||||
|
}
|
||||||
|
}
|
||||||
}
|
}
|
||||||
if (file != "snapshot.json")
|
if (file != "snapshot.json")
|
||||||
return std::unexpected("unsupported index generation selector");
|
return std::unexpected("unsupported index generation selector");
|
||||||
@@ -709,7 +722,7 @@ SymbolIndex::restore_generation(const fs::path &root) {
|
|||||||
if (!result)
|
if (!result)
|
||||||
return result;
|
return result;
|
||||||
if (result->status != LoadStatus::loaded) {
|
if (result->status != LoadStatus::loaded) {
|
||||||
const auto previous = root / "snapshot.prev.json";
|
const auto previous = root / previous_name;
|
||||||
if (fs::exists(previous, error)) {
|
if (fs::exists(previous, error)) {
|
||||||
result = restore(previous);
|
result = restore(previous);
|
||||||
// Keep CURRENT consistent: the recovered generation becomes current.
|
// Keep CURRENT consistent: the recovered generation becomes current.
|
||||||
@@ -721,7 +734,8 @@ SymbolIndex::restore_generation(const fs::path &root) {
|
|||||||
if (result && result->status == LoadStatus::loaded && (migrate_selector || result->migrated)) {
|
if (result && result->status == LoadStatus::loaded && (migrate_selector || result->migrated)) {
|
||||||
const auto temporary = root / "CURRENT.tmp";
|
const auto temporary = root / "CURRENT.tmp";
|
||||||
std::ofstream stream{temporary, std::ios::binary | std::ios::trunc};
|
std::ofstream stream{temporary, std::ios::binary | std::ios::trunc};
|
||||||
stream << "{\"schema\":2,\"file\":\"snapshot.json\"}\n";
|
stream << "{\"schema\":3,\"file\":\"snapshot.json\","
|
||||||
|
"\"previous\":\"snapshot.prev.json\"}\n";
|
||||||
stream.close();
|
stream.close();
|
||||||
fs::rename(temporary, current, error);
|
fs::rename(temporary, current, error);
|
||||||
// Persist the upgraded snapshot format; the previous generation is
|
// Persist the upgraded snapshot format; the previous generation is
|
||||||
|
|||||||
@@ -305,7 +305,7 @@ VOID_TEST(SymbolIndex, generation_selector_migrates_from_the_legacy_format) {
|
|||||||
expect(restored->status == SymbolIndex::LoadStatus::loaded);
|
expect(restored->status == SymbolIndex::LoadStatus::loaded);
|
||||||
std::ifstream selector{root / "CURRENT", std::ios::binary};
|
std::ifstream selector{root / "CURRENT", std::ios::binary};
|
||||||
const std::string text{std::istreambuf_iterator<char>{selector}, {}};
|
const std::string text{std::istreambuf_iterator<char>{selector}, {}};
|
||||||
expect(text.find("\"schema\":2") != std::string::npos);
|
expect(text.find("\"schema\":3") != std::string::npos);
|
||||||
{
|
{
|
||||||
std::ofstream stream{root / "CURRENT", std::ios::binary | std::ios::trunc};
|
std::ofstream stream{root / "CURRENT", std::ios::binary | std::ios::trunc};
|
||||||
stream << "future.json\n";
|
stream << "future.json\n";
|
||||||
@@ -502,3 +502,30 @@ VOID_TEST(SymbolIndex, rejects_an_oversized_signature) {
|
|||||||
signed_record.signature = std::string(400, 'x');
|
signed_record.signature = std::string(400, 'x');
|
||||||
expect(!index.publish(context("debug:unit", {signed_record})));
|
expect(!index.publish(context("debug:unit", {signed_record})));
|
||||||
}
|
}
|
||||||
|
|
||||||
|
VOID_TEST(SymbolIndex, selector_records_the_previous_generation_name) {
|
||||||
|
Void::FileSystem::TempDir temp{"vls_symbol_index_"};
|
||||||
|
const auto root = temp.path() / "index";
|
||||||
|
SymbolIndex index;
|
||||||
|
require(index.publish(context("debug:a", {record("a", "/project/a.cpp", 1)})));
|
||||||
|
require(index.save_generation(root));
|
||||||
|
require(index.publish(context("debug:b", {record("b", "/project/b.cpp", 2)})));
|
||||||
|
require(index.save_generation(root));
|
||||||
|
fs::rename(root / "snapshot.prev.json", root / "older.json");
|
||||||
|
{
|
||||||
|
std::ofstream stream{root / "CURRENT", std::ios::binary | std::ios::trunc};
|
||||||
|
stream << R"({"schema":3,"file":"snapshot.json","previous":"older.json"})";
|
||||||
|
}
|
||||||
|
fs::remove(root / "snapshot.json");
|
||||||
|
SymbolIndex recovered;
|
||||||
|
const auto restored = recovered.restore_generation(root);
|
||||||
|
require(restored.has_value());
|
||||||
|
expect(restored->status == SymbolIndex::LoadStatus::loaded);
|
||||||
|
expect(count(recovered.find({.name = "a"})) == 1);
|
||||||
|
{
|
||||||
|
std::ofstream stream{root / "CURRENT", std::ios::binary | std::ios::trunc};
|
||||||
|
stream << R"({"schema":3,"file":"snapshot.json","previous":"../escape.json"})";
|
||||||
|
}
|
||||||
|
SymbolIndex rejected;
|
||||||
|
expect(!rejected.restore_generation(root).has_value());
|
||||||
|
}
|
||||||
Reference in new issue
Block a user