C#: a write through a live link at the store path is refused; read-under-lock pinned (store-link-cs) (#151)
This commit is contained in:
parent
3a8de542de
commit
73e2131d9a
8 changed files with 674 additions and 32 deletions
|
|
@ -5132,7 +5132,7 @@ namespace GameCult.Caching
|
|||
var attributes = FileInfo.Attributes;
|
||||
if ((int)attributes == -1)
|
||||
return null;
|
||||
if ((attributes & FileAttributes.Directory) != 0 && (attributes & FileAttributes.ReparsePoint) == 0)
|
||||
if ((attributes & FileAttributes.Directory) != 0 && Directory.Exists(FileInfo.FullName))
|
||||
throw new IOException("The store path is a directory, not a store file.");
|
||||
try
|
||||
{
|
||||
|
|
@ -5154,25 +5154,27 @@ namespace GameCult.Caching
|
|||
}
|
||||
|
||||
// Something is at the path even when the path cannot be opened: a link keeps the reparse-point attribute whether or not its
|
||||
// target is there. A path with nothing at it reports no attributes at all (-1).
|
||||
private bool PathIsALink()
|
||||
{
|
||||
FileInfo.Refresh();
|
||||
var attributes = FileInfo.Attributes;
|
||||
return (int)attributes != -1 && (attributes & FileAttributes.ReparsePoint) != 0;
|
||||
}
|
||||
// target is there.
|
||||
private bool PathIsALink() => CultStoreLinks.IsLink(FileInfo.FullName);
|
||||
|
||||
// A write never goes through a link at the store path: replacing it would silently detach the store from its link.
|
||||
private void RefuseLinkedStore() => CultStoreLinks.Refuse(FileInfo.FullName);
|
||||
|
||||
// The lock is a sidecar opened exclusively, which excludes other handles in this process and in others alike.
|
||||
// Every write takes the lock first, so the link is refused here twice: before the lock file or anything else is made, and
|
||||
// again once the lock is held, because a link can be put at the path while this writer waited on another's lock.
|
||||
private FileStream? AcquireLock(bool wait)
|
||||
{
|
||||
RefuseLinkedStore();
|
||||
Directory.CreateDirectory(FileInfo.DirectoryName!);
|
||||
var lockPath = FileInfo.FullName + ".lock";
|
||||
var started = Stopwatch.StartNew();
|
||||
while (true)
|
||||
{
|
||||
FileStream held;
|
||||
try
|
||||
{
|
||||
return new FileStream(lockPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, bufferSize: 1);
|
||||
held = new FileStream(lockPath, FileMode.OpenOrCreate, FileAccess.ReadWrite, FileShare.None, bufferSize: 1);
|
||||
}
|
||||
catch (IOException) when (!wait)
|
||||
{
|
||||
|
|
@ -5181,7 +5183,20 @@ namespace GameCult.Caching
|
|||
catch (IOException) when (started.Elapsed < TimeSpan.FromSeconds(30))
|
||||
{
|
||||
Thread.Sleep(10);
|
||||
continue;
|
||||
}
|
||||
|
||||
try
|
||||
{
|
||||
RefuseLinkedStore();
|
||||
}
|
||||
catch
|
||||
{
|
||||
held.Dispose();
|
||||
throw;
|
||||
}
|
||||
|
||||
return held;
|
||||
}
|
||||
}
|
||||
|
||||
|
|
|
|||
Loading…
Reference in a new issue