Skip to content

Commit

Permalink
BUG: Lucene.Net.Store.SharingNativeFSLock: Allow other processes (suc…
Browse files Browse the repository at this point in the history
…h as RAMDirectory) to read the lock file, only set file sharing to None in IsLocked() to provoke the sharing exception. Fixes apache#356.
  • Loading branch information
NightOwl888 committed Sep 27, 2020
1 parent 37d7ffd commit 061be01
Showing 1 changed file with 13 additions and 4 deletions.
17 changes: 13 additions & 4 deletions src/Lucene.Net/Store/NativeFSLockFactory.cs
Original file line number Diff line number Diff line change
Expand Up @@ -139,7 +139,7 @@ private static bool LoadIsFileStreamLockingPlatform()

return FileSupport.GetFileIOExceptionHResult(provokeException: (fileName) =>
{
using (var lockStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None, 1, FileOptions.None))
using (var lockStream = new FileStream(fileName, FileMode.OpenOrCreate, FileAccess.Write, FileShare.Read, 1, FileOptions.None))
// Try to get an exclusive lock on the file - this should throw an IOException with the current platform's HResult value for FileShare violation
using (var stream = new FileStream(fileName, FileMode.Open, FileAccess.Write, FileShare.None, 1, FileOptions.None))
{
Expand Down Expand Up @@ -288,7 +288,8 @@ public override bool Obtain()
var success = false;
try
{
channel = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.None);
// LUCENENET: Allow read access for the RAMDirectory to be able to copy the lock file.
channel = new FileStream(path, FileMode.Create, FileAccess.Write, FileShare.Read);

success = true;
}
Expand Down Expand Up @@ -408,7 +409,7 @@ public override bool IsLocked()

public override string ToString()
{
return "{nameof(FallbackNativeFSLock)}@{path}";
return $"{nameof(FallbackNativeFSLock)}@{path}";
}
}

Expand Down Expand Up @@ -454,7 +455,15 @@ private FileStream GetLockFileStream(FileMode mode)
throw new IOException("Found regular file where directory expected: " + lockDir.FullName);
}

return new FileStream(path, mode, FileAccess.Write, FileShare.None, 1, mode == FileMode.Open ? FileOptions.None : FileOptions.DeleteOnClose);
return new FileStream(
path,
mode,
FileAccess.Write,
// LUCENENET: Allow read access of OpenOrCreate for the RAMDirectory to be able to copy the lock file.
// For the Open case, set to FileShare.None to force a file share exception in IsLocked().
share: mode == FileMode.Open ? FileShare.None : FileShare.Read,
bufferSize: 1,
options: mode == FileMode.Open ? FileOptions.None : FileOptions.DeleteOnClose);
}

public override bool Obtain()
Expand Down

0 comments on commit 061be01

Please sign in to comment.