in csharp/NativeUtils/FileStream.cs [335:373]
private void LockFd(int fd, FileShare share, string path)
{
int sh = 0;
share = share & (FileShare.ReadWrite | FileShare.Delete);
// Better than nothing
// But preferably should take own access method into account
switch (share)
{
case FileShare.None:
sh = LOCK_EX;
break;
case FileShare.Read:
case FileShare.Write:
case FileShare.ReadWrite:
case FileShare.Delete:
sh = LOCK_SH;
break;
}
if (0 != sh && fd >= 0)
{
sh |= LOCK_NB;
if (flock(fd, sh) < 0)
{
var exc = new Win32Exception();
if (LogLevelLeast(DBG) && !path.Contains("lockfile"))
Log($"Lock fail: {path} : {fd} : {sh} : {exc.NativeErrorCode}");
if (EAGAIN == exc.NativeErrorCode)
throw new IOException($"File is locked: {path}");
throw new IOException($"Failed to take {(LOCK_EX == sh ? "Exclusive" : "Shared")} Lock for file: {path}, code: {exc.NativeErrorCode}");
}
if (LogLevelLeast(DBG))
Log($"Lock ok: {path} : {fd} : {sh}");
}
}