diff --git a/src/GameCult.Mesh/CultMeshSingleFileDocuments.cs b/src/GameCult.Mesh/CultMeshSingleFileDocuments.cs index 700b7a50..001edab5 100644 --- a/src/GameCult.Mesh/CultMeshSingleFileDocuments.cs +++ b/src/GameCult.Mesh/CultMeshSingleFileDocuments.cs @@ -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 (.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) diff --git a/tests/GameCult.Mesh.Tests/CultMeshSingleFileWriteGuardTests.cs b/tests/GameCult.Mesh.Tests/CultMeshSingleFileWriteGuardTests.cs new file mode 100644 index 00000000..f2dc6c9e --- /dev/null +++ b/tests/GameCult.Mesh.Tests/CultMeshSingleFileWriteGuardTests.cs @@ -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(() => 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(() => 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); + } +}