CultMesh single-file write reads what it replaces: refuses a newer-format, unreadable or larger store

The write replaced the file without reading it, so a 2.0 writer pointed at a store a newer
format stamped (or one holding other records) destroyed it.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-22 15:55:42 +02:00
commit 7678148c1a
2 changed files with 123 additions and 1 deletions

View file

@ -241,7 +241,45 @@ namespace GameCult.Mesh
if (!string.IsNullOrWhiteSpace(directory))
Directory.CreateDirectory(directory);
WriteFileAtomically(path, CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot));
// The write replaces the file, so it first reads what it would replace, under the lock a CultCache single-file
// store takes (<path>.lock): a store this runtime cannot read (a newer format, or damage) or one holding any
// record but this key is refused, never overwritten.
using (AcquireStoreLock(path))
{
RefuseToReplaceAStoreHoldingMore(path, key);
WriteFileAtomically(path, CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot));
}
}
private static void RefuseToReplaceAStoreHoldingMore(string path, CultRecordKey key)
{
if (!File.Exists(path) || new FileInfo(path).Length == 0)
return;
var existing = ReadSingleFileSnapshot(path);
var other = existing.Records.FirstOrDefault(record =>
record.Variant != null || !string.Equals(record.Key, key.Value, StringComparison.Ordinal));
if (other != null)
throw new InvalidOperationException(
$"CultCache document '{path}' holds record '{other.Key}' besides '{key.Value}'; a CultMesh single-file write " +
"replaces the whole file and would drop it. Write through a CultCache store instead.");
}
private static FileStream AcquireStoreLock(string path)
{
var lockPath = path + ".lock";
var started = System.Diagnostics.Stopwatch.StartNew();
while (true)
{
try
{
return new FileStream(lockPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, bufferSize: 1);
}
catch (IOException) when (started.Elapsed < TimeSpan.FromSeconds(30))
{
System.Threading.Thread.Sleep(10);
}
}
}
private static CultPersistedStoreSnapshot ReadSingleFileSnapshot(string path)

View file

@ -0,0 +1,84 @@
using System;
using System.IO;
using FluentAssertions;
using GameCult.Caching;
using GameCult.Caching.MessagePack;
using NUnit.Framework;
#nullable enable
namespace GameCult.Mesh.Tests;
// A CultMesh single-file write replaces the whole file, so it must read what it replaces first. A store stamped with a
// newer format version, one it cannot read, or one holding a record the write would drop is refused and left byte-identical.
public sealed class CultMeshSingleFileWriteGuardTests
{
private static readonly CultMeshSingleFileDocumentSchema Schema = new("sha256:guard", "guard.item", "1");
private string _root = null!;
private string _path = null!;
[SetUp]
public void SetUp()
{
_root = Path.Combine(Path.GetTempPath(), "cultmesh-write-guard", Guid.NewGuid().ToString("N"));
Directory.CreateDirectory(_root);
_path = Path.Combine(_root, "store.cc");
}
[TearDown]
public void TearDown()
{
if (Directory.Exists(_root)) Directory.Delete(_root, true);
}
private void Write(string key, byte payload) =>
CultMesh.WriteSingleFileDocumentPayload(_path, new CultRecordKey(key), Schema, null, new[] { payload });
[TestCase("cultcache.store.v3")]
[TestCase("cultcache.store.v9")]
public void WriteRefusesAStoreStampedWithANewerFormat(string newer)
{
Write("item:a", 1);
var snapshot = CultDocumentMessagePackSerialization.DeserializeSnapshot(File.ReadAllBytes(_path));
snapshot.FormatVersion = newer;
File.WriteAllBytes(_path, CultDocumentMessagePackSerialization.SerializeSnapshot(snapshot));
var before = File.ReadAllBytes(_path);
var error = Assert.Throws<NotSupportedException>(() => Write("item:a", 2))!;
error.Message.Should().Contain(newer);
File.ReadAllBytes(_path).Should().Equal(before);
}
[Test]
public void WriteRefusesAFileItCannotRead()
{
var garbage = new byte[] { 0xc1, 0xc1, 0xc1, 0xc1 };
File.WriteAllBytes(_path, garbage);
Assert.Catch(() => Write("item:a", 1));
File.ReadAllBytes(_path).Should().Equal(garbage);
}
[Test]
public void WriteRefusesToDropAnotherRecord()
{
Write("item:a", 1);
var before = File.ReadAllBytes(_path);
var error = Assert.Throws<InvalidOperationException>(() => Write("item:b", 2))!;
error.Message.Should().Contain("item:a");
File.ReadAllBytes(_path).Should().Equal(before);
}
[Test]
public void WriteReplacesTheSameRecordAndCreatesAMissingFile()
{
Write("item:a", 1);
Write("item:a", 2);
CultMesh.ReadSingleFileDocumentPayload(_path, new CultRecordKey("item:a"), Schema.SchemaId).Should().Equal(2);
}
}