2020-05-21 23:02:04 +02:00
|
|
|
// _ _ _ ____ _ _____
|
|
|
|
|
// / \ _ __ ___ | |__ (_)/ ___| | |_ ___ __ _ _ __ ___ | ___|__ _ _ __ _ __ ___
|
|
|
|
|
// / _ \ | '__|/ __|| '_ \ | |\___ \ | __|/ _ \ / _` || '_ ` _ \ | |_ / _` || '__|| '_ ` _ \
|
|
|
|
|
// / ___ \ | | | (__ | | | || | ___) || |_| __/| (_| || | | | | || _|| (_| || | | | | | | |
|
|
|
|
|
// /_/ \_\|_| \___||_| |_||_||____/ \__|\___| \__,_||_| |_| |_||_| \__,_||_| |_| |_| |_|
|
|
|
|
|
// |
|
2021-01-03 22:24:22 +01:00
|
|
|
// Copyright 2015-2021 Łukasz "JustArchi" Domeradzki
|
2020-05-21 23:02:04 +02:00
|
|
|
// Contact: JustArchi@JustArchi.net
|
|
|
|
|
// |
|
|
|
|
|
// Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
|
// you may not use this file except in compliance with the License.
|
|
|
|
|
// You may obtain a copy of the License at
|
|
|
|
|
// |
|
|
|
|
|
// http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
|
// |
|
|
|
|
|
// Unless required by applicable law or agreed to in writing, software
|
|
|
|
|
// distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
|
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
|
// See the License for the specific language governing permissions and
|
|
|
|
|
// limitations under the License.
|
|
|
|
|
|
|
|
|
|
using System;
|
|
|
|
|
using System.Diagnostics;
|
|
|
|
|
using System.IO;
|
2020-11-10 23:22:57 +01:00
|
|
|
using System.Runtime.InteropServices;
|
2020-05-21 23:02:04 +02:00
|
|
|
using System.Security.AccessControl;
|
|
|
|
|
using System.Threading;
|
|
|
|
|
using System.Threading.Tasks;
|
|
|
|
|
|
|
|
|
|
namespace ArchiSteamFarm.Helpers {
|
2020-11-16 00:52:43 +01:00
|
|
|
internal sealed class CrossProcessFileBasedSemaphore : ICrossProcessSemaphore, IDisposable {
|
2020-05-21 23:09:53 +02:00
|
|
|
private const ushort SpinLockDelay = 1000; // In milliseconds
|
2020-05-21 23:02:04 +02:00
|
|
|
|
|
|
|
|
private readonly string FilePath;
|
2020-11-14 22:37:00 +01:00
|
|
|
private readonly SemaphoreSlim LocalSemaphore = new(1, 1);
|
2020-05-21 23:02:04 +02:00
|
|
|
|
2020-08-22 21:41:01 +02:00
|
|
|
private FileStream? FileLock;
|
2020-05-21 23:02:04 +02:00
|
|
|
|
2020-08-22 21:41:01 +02:00
|
|
|
internal CrossProcessFileBasedSemaphore(string name) {
|
2020-05-21 23:02:04 +02:00
|
|
|
if (string.IsNullOrEmpty(name)) {
|
|
|
|
|
throw new ArgumentNullException(nameof(name));
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FilePath = Path.Combine(Path.GetTempPath(), SharedInfo.ASF, name);
|
|
|
|
|
|
|
|
|
|
EnsureFileExists();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
public void Dispose() {
|
|
|
|
|
LocalSemaphore.Dispose();
|
|
|
|
|
|
|
|
|
|
FileLock?.Dispose();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
void ICrossProcessSemaphore.Release() {
|
|
|
|
|
lock (LocalSemaphore) {
|
|
|
|
|
if (FileLock == null) {
|
2020-11-11 16:51:31 +01:00
|
|
|
throw new InvalidOperationException(nameof(FileLock));
|
2020-05-21 23:02:04 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
FileLock.Dispose();
|
|
|
|
|
FileLock = null;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
LocalSemaphore.Release();
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async Task ICrossProcessSemaphore.WaitAsync() {
|
|
|
|
|
await LocalSemaphore.WaitAsync().ConfigureAwait(false);
|
|
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
|
|
try {
|
2020-05-21 23:08:02 +02:00
|
|
|
while (true) {
|
|
|
|
|
try {
|
|
|
|
|
lock (LocalSemaphore) {
|
|
|
|
|
if (FileLock != null) {
|
2020-11-11 16:51:31 +01:00
|
|
|
throw new InvalidOperationException(nameof(FileLock));
|
2020-05-21 23:08:02 +02:00
|
|
|
}
|
2020-05-21 23:02:04 +02:00
|
|
|
|
2020-05-21 23:08:02 +02:00
|
|
|
EnsureFileExists();
|
2020-05-21 23:02:04 +02:00
|
|
|
|
|
|
|
|
FileLock = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
|
|
|
|
|
success = true;
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
2020-05-21 23:08:02 +02:00
|
|
|
} catch (IOException) {
|
|
|
|
|
await Task.Delay(SpinLockDelay).ConfigureAwait(false);
|
2020-05-21 23:02:04 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (!success) {
|
|
|
|
|
LocalSemaphore.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
async Task<bool> ICrossProcessSemaphore.WaitAsync(int millisecondsTimeout) {
|
|
|
|
|
Stopwatch stopwatch = Stopwatch.StartNew();
|
|
|
|
|
|
|
|
|
|
if (!await LocalSemaphore.WaitAsync(millisecondsTimeout).ConfigureAwait(false)) {
|
|
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
bool success = false;
|
|
|
|
|
|
|
|
|
|
try {
|
2020-05-22 10:45:48 +02:00
|
|
|
stopwatch.Stop();
|
|
|
|
|
|
2020-05-21 23:02:04 +02:00
|
|
|
millisecondsTimeout -= (int) stopwatch.ElapsedMilliseconds;
|
|
|
|
|
|
|
|
|
|
if (millisecondsTimeout <= 0) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 10:45:48 +02:00
|
|
|
while (true) {
|
|
|
|
|
try {
|
|
|
|
|
lock (LocalSemaphore) {
|
|
|
|
|
if (FileLock != null) {
|
2020-11-11 16:51:31 +01:00
|
|
|
throw new InvalidOperationException(nameof(FileLock));
|
2020-05-22 10:45:48 +02:00
|
|
|
}
|
2020-05-21 23:02:04 +02:00
|
|
|
|
2020-05-22 10:45:48 +02:00
|
|
|
EnsureFileExists();
|
2020-05-21 23:08:02 +02:00
|
|
|
|
2020-05-22 10:45:48 +02:00
|
|
|
FileLock = new FileStream(FilePath, FileMode.OpenOrCreate, FileAccess.Read, FileShare.None);
|
|
|
|
|
success = true;
|
2020-05-21 23:02:04 +02:00
|
|
|
|
2020-05-22 10:45:48 +02:00
|
|
|
return true;
|
2020-05-21 23:02:04 +02:00
|
|
|
}
|
2020-05-22 10:45:48 +02:00
|
|
|
} catch (IOException) {
|
|
|
|
|
if (millisecondsTimeout <= SpinLockDelay) {
|
|
|
|
|
return false;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
await Task.Delay(SpinLockDelay).ConfigureAwait(false);
|
|
|
|
|
millisecondsTimeout -= SpinLockDelay;
|
2020-05-21 23:08:02 +02:00
|
|
|
}
|
2020-05-21 23:02:04 +02:00
|
|
|
}
|
|
|
|
|
} finally {
|
|
|
|
|
if (!success) {
|
|
|
|
|
LocalSemaphore.Release();
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
private void EnsureFileExists() {
|
2020-05-22 10:35:07 +02:00
|
|
|
if (File.Exists(FilePath)) {
|
2020-05-21 23:02:04 +02:00
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-08-22 21:41:01 +02:00
|
|
|
string? directoryPath = Path.GetDirectoryName(FilePath);
|
2020-05-21 23:31:07 +02:00
|
|
|
|
|
|
|
|
if (string.IsNullOrEmpty(directoryPath)) {
|
|
|
|
|
ASF.ArchiLogger.LogNullError(nameof(directoryPath));
|
|
|
|
|
|
|
|
|
|
return;
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-22 10:35:07 +02:00
|
|
|
if (!Directory.Exists(directoryPath)) {
|
2020-08-23 20:45:24 +02:00
|
|
|
Directory.CreateDirectory(directoryPath!);
|
2020-05-21 23:31:07 +02:00
|
|
|
|
2020-11-10 23:22:57 +01:00
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
|
2020-11-14 22:37:00 +01:00
|
|
|
DirectoryInfo directoryInfo = new(directoryPath!);
|
2020-05-21 23:31:07 +02:00
|
|
|
|
2020-06-16 10:04:51 +02:00
|
|
|
try {
|
2020-11-14 22:37:00 +01:00
|
|
|
DirectorySecurity directorySecurity = new(directoryPath!, AccessControlSections.All);
|
2020-06-16 10:04:51 +02:00
|
|
|
|
|
|
|
|
directoryInfo.SetAccessControl(directorySecurity);
|
|
|
|
|
} catch (PrivilegeNotHeldException e) {
|
|
|
|
|
// Non-critical, user might have no rights to manage the resource
|
|
|
|
|
ASF.ArchiLogger.LogGenericDebuggingException(e);
|
|
|
|
|
}
|
2020-11-10 23:22:57 +01:00
|
|
|
} else {
|
|
|
|
|
OS.UnixSetFileAccess(directoryPath!, OS.EUnixPermission.Combined777);
|
2020-05-21 23:31:07 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-05-21 23:02:04 +02:00
|
|
|
try {
|
2021-05-07 23:56:45 +02:00
|
|
|
#pragma warning disable CA1508 // False positive, FileStream is not null here indeed, but using clause is needed for dispose
|
2020-05-21 23:02:04 +02:00
|
|
|
using (new FileStream(FilePath, FileMode.CreateNew)) { }
|
2021-05-07 23:56:45 +02:00
|
|
|
#pragma warning restore CA1508 // False positive, FileStream is not null here indeed, but using clause is needed for dispose
|
2020-05-21 23:02:04 +02:00
|
|
|
|
2020-11-10 23:22:57 +01:00
|
|
|
if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) {
|
2020-11-14 22:37:00 +01:00
|
|
|
FileInfo fileInfo = new(FilePath);
|
2020-05-21 23:02:04 +02:00
|
|
|
|
2020-06-16 10:04:51 +02:00
|
|
|
try {
|
2020-11-14 22:37:00 +01:00
|
|
|
FileSecurity fileSecurity = new(FilePath, AccessControlSections.All);
|
2020-06-16 10:04:51 +02:00
|
|
|
|
|
|
|
|
fileInfo.SetAccessControl(fileSecurity);
|
|
|
|
|
} catch (PrivilegeNotHeldException e) {
|
|
|
|
|
// Non-critical, user might have no rights to manage the resource
|
|
|
|
|
ASF.ArchiLogger.LogGenericDebuggingException(e);
|
|
|
|
|
}
|
2020-11-10 23:22:57 +01:00
|
|
|
} else {
|
|
|
|
|
OS.UnixSetFileAccess(FilePath, OS.EUnixPermission.Combined777);
|
2020-05-21 23:02:04 +02:00
|
|
|
}
|
|
|
|
|
} catch (IOException) {
|
|
|
|
|
// Ignored, if the file was already created in the meantime by another instance, this is fine
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
}
|