This repository has been archived by the owner on Sep 7, 2023. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 32
/
CrossPlatLock.cs
83 lines (73 loc) · 3.33 KB
/
CrossPlatLock.cs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT License.
using System;
using System.Diagnostics;
using System.IO;
using System.Text;
using System.Threading;
#if ADAL
namespace Microsoft.Identity.Client.Extensions.Adal
#elif MSAL
namespace Microsoft.Identity.Client.Extensions.Msal
#else // WEB
namespace Microsoft.Identity.Client.Extensions.Web
#endif
{
/// <summary>
/// A cross-process lock that works on all platforms.
/// It is important to note that this lock is not thread safe !
/// </summary>
internal sealed class CrossPlatLock : IDisposable
{
internal const int LockfileRetryDelayDefault = 100;
internal const int LockfileRetryCountDefault = 60000 / LockfileRetryDelayDefault;
private FileStream _lockFileStream;
public CrossPlatLock(string lockfilePath, int lockFileRetryDelay = LockfileRetryDelayDefault, int lockFileRetryCount = LockfileRetryCountDefault)
{
Exception exception = null;
FileStream fileStream = null;
// Create lock file dir if it doesn't already exist
Directory.CreateDirectory(Path.GetDirectoryName(lockfilePath));
for (int tryCount = 0; tryCount < lockFileRetryCount; tryCount++)
{
try
{
// We are using the file locking to synchronize the store, do not allow multiple writers or readers for the file.
const int defaultBufferSize = 4096;
var fileShare = FileShare.None;
if (SharedUtilities.IsWindowsPlatform())
{
// This is so that Windows can offer read due to the granularity of the locking. Unix will not
// lock with FileShare.Read. Read access on Windows is only for debugging purposes and will not
// affect the functionality.
//
// See: https://github.com/dotnet/coreclr/blob/98472784f82cee7326a58e0c4acf77714cdafe03/src/System.Private.CoreLib/shared/System/IO/FileStream.Unix.cs#L74-L89
fileShare = FileShare.Read;
}
fileStream = new FileStream(lockfilePath, FileMode.OpenOrCreate, FileAccess.ReadWrite, fileShare, defaultBufferSize, FileOptions.DeleteOnClose);
using (var writer = new StreamWriter(fileStream, Encoding.UTF8, defaultBufferSize, leaveOpen: true))
{
writer.WriteLine($"{Process.GetCurrentProcess().Id} {Process.GetCurrentProcess().ProcessName}");
}
break;
}
catch (IOException ex)
{
exception = ex;
Thread.Sleep(lockFileRetryDelay);
}
catch (UnauthorizedAccessException ex)
{
exception = ex;
Thread.Sleep(lockFileRetryCount);
}
}
_lockFileStream = fileStream ?? throw new InvalidOperationException("Could not get access to the shared lock file.", exception);
}
public void Dispose()
{
_lockFileStream?.Dispose();
_lockFileStream = null;
}
}
}