Mesh single-file helpers read and replace through the CultCache single-file store; a directory at the store path is an error

SingleFileBackingStore gains ReadDurable and ReplaceDurable (internal): CultMesh's read and write
helpers reach the one reader, <file>.lock and the unique-temp atomic replace through them. ReadDisk
decides absence from the path's attributes, so a directory at the path is an IOException, not an
empty store. Includes the directory clause that the spec lists as a separate step.
Builds with the previous commit's deletions; the tree before this commit does not build.

Co-Authored-By: Claude Sonnet 5.5 <noreply@anthropic.com>
This commit is contained in:
Meta Vens 2026-10-03 01:59:42 +02:00
commit 9852fa9367
2 changed files with 28 additions and 10 deletions

View file

@ -4009,12 +4009,30 @@ namespace GameCult.Caching
}
}
// CultMesh's single-file document helpers reach this store file only through these two members, so they share its one
// reader, its lock and its atomic replace. c2a-write-set replaces ReplaceDurable with ApplyWriteSet.
internal CultPersistedStoreSnapshot? ReadDurable() => Held(ReadSnapshot);
internal void ReplaceDurable(Func<CultPersistedStoreSnapshot?, CultPersistedStoreSnapshot> next) => Held(() =>
{
using (AcquireLock(wait: true))
{
var snapshot = next(ReadSnapshot());
Directory.CreateDirectory(FileInfo.DirectoryName!);
WriteSnapshotAtomically(FileInfo.FullName, SerializeSnapshot(snapshot));
}
});
private byte[]? ReadDisk()
{
FileInfo.Refresh();
// Only nothing at the path is an empty store. A link whose target is gone is a store that cannot be reached.
if (!FileInfo.Exists && !PathIsALink())
// Only nothing at the path is an empty store: a path with nothing at it reports no attributes at all (-1). A link whose
// target is gone is a store that cannot be reached, and a directory is not a store file.
var attributes = FileInfo.Attributes;
if ((int)attributes == -1)
return null;
if ((attributes & FileAttributes.Directory) != 0 && (attributes & FileAttributes.ReparsePoint) == 0)
throw new IOException("The store path is a directory, not a store file.");
try
{
return ReadAllBytesShared(FileInfo.FullName);

View file

@ -161,7 +161,7 @@ namespace GameCult.Mesh
if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("Value must be non-empty.", nameof(path));
if (string.IsNullOrWhiteSpace(expectedSchemaId)) throw new ArgumentException("Value must be non-empty.", nameof(expectedSchemaId));
var snapshot = ReadSingleFileSnapshot(path);
var snapshot = StoreAt(path).ReadDurable() ?? throw new FileNotFoundException($"CultCache document '{path}' does not exist.", path);
var record = snapshot.Records.SingleOrDefault(candidate => string.Equals(candidate.Key, key.Value, StringComparison.Ordinal))
?? throw new InvalidDataException($"CultCache document '{path}' does not contain record '{key.Value}'.");
@ -192,7 +192,7 @@ namespace GameCult.Mesh
if (string.IsNullOrWhiteSpace(path)) throw new ArgumentException("Value must be non-empty.", nameof(path));
if (string.IsNullOrWhiteSpace(expectedSchemaId)) throw new ArgumentException("Value must be non-empty.", nameof(expectedSchemaId));
var snapshot = ReadSingleFileSnapshot(path);
var snapshot = StoreAt(path).ReadDurable() ?? throw new FileNotFoundException($"CultCache document '{path}' does not exist.", path);
if (snapshot.Records.Length != 1)
{
throw new InvalidDataException(
@ -239,11 +239,10 @@ namespace GameCult.Mesh
throw new ArgumentException("Catalog entry must include a schema id.", nameof(catalogEntry));
payload ??= Array.Empty<byte>();
CultPersistedStoreSnapshot? existing = null;
// A typed write sees the document it replaces the file with, so its ids decide. A raw payload is opaque: the writer
// cannot see whether it holds ids, so a file already marked stays marked.
var snapshot = new CultPersistedStoreSnapshot
// cannot see whether it holds ids, so a file already marked stays marked. The store reads the file it replaces under
// its lock: one this runtime cannot read refuses the write instead of being overwritten.
StoreAt(path).ReplaceDurable(existing => new CultPersistedStoreSnapshot
{
FormatVersion = CacheBackingStore.HeaderFor(holdsIds, existing?.FormatVersion, wholeStore: contentKnown, directoryStore: false),
SchemaCatalog = new[] { catalogEntry },
@ -257,10 +256,11 @@ namespace GameCult.Mesh
Payload = payload
}
}
};
});
}
private static SingleFileMessagePackBackingStore StoreAt(string path) => new SingleFileMessagePackBackingStore(path);
private static bool PublishesSchema(CultSchemaCatalogEntry[] catalog, string schemaId)
{
return catalog.Any(entry =>