Single-file and directory snapshot reads judge the format stamp before parsing the body; a flush heals an empty or truncated file

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-22 17:30:57 +02:00
commit 39e8ab5efd
5 changed files with 157 additions and 31 deletions

View file

@ -3542,39 +3542,34 @@ namespace GameCult.Caching
// A plain flush replaces the file without merging it, so under the lock it first reads the disk the way attach and
// commit do: a store this runtime does not read (a newer format, or a header the reader refuses) is refused and left
// as it is, and so is a read that fails on I/O. A file that merely will not parse (empty, corrupt) is not a newer
// store, and the flush proceeds as it always has, so it can still heal a damaged store.
// as it is, and so is a file that cannot be read (an I/O or access error). A file that is read but will not parse
// (empty, truncated, corrupt) is not a newer store, and the flush proceeds as it always has, so it can still heal.
private void RefuseToRewriteAStoreItCannotRead()
{
var bytes = ReadFileBytes();
if (bytes == null)
return;
try
{
ReadSnapshot();
DeserializeSnapshot(bytes);
}
catch (NotSupportedException)
{
throw;
}
catch (IOException)
{
throw;
}
catch (UnauthorizedAccessException)
{
throw;
}
catch (Exception)
{
}
}
private CultPersistedStoreSnapshot? ReadSnapshot()
private byte[]? ReadFileBytes()
{
FileInfo.Refresh();
if (!FileInfo.Exists)
return null;
try
{
return DeserializeSnapshot(ReadAllBytesShared(FileInfo.FullName));
return ReadAllBytesShared(FileInfo.FullName);
}
catch (FileNotFoundException)
{
@ -3582,6 +3577,12 @@ namespace GameCult.Caching
}
}
private CultPersistedStoreSnapshot? ReadSnapshot()
{
var bytes = ReadFileBytes();
return bytes == null ? null : DeserializeSnapshot(bytes);
}
private void WriteSnapshot(IEnumerable<CultPersistedRecord> records, IEnumerable<CultSchemaCatalogEntry> catalog)
{
var ordered = records.OrderBy(record => record.Key, StringComparer.Ordinal).ToArray();