| | 1 | | using System; |
| | 2 | | using System.Collections.Generic; |
| | 3 | | using System.Runtime.InteropServices; |
| | 4 | | using LockCheck.Windows; |
| | 5 | | #if NET |
| | 6 | | using LockCheck.Linux; |
| | 7 | | #endif |
| | 8 | |
|
| | 9 | |
|
| | 10 | | namespace LockCheck; |
| | 11 | |
|
| | 12 | | /// <summary> |
| | 13 | | /// Retrieves information about locked files and directories. |
| | 14 | | /// </summary> |
| | 15 | | public static class LockManager |
| | 16 | | { |
| | 17 | | /// <summary> |
| | 18 | | /// Attempt to find processes that lock the specified paths. |
| | 19 | | /// </summary> |
| | 20 | | /// <param name="paths">The paths to check.</param> |
| | 21 | | /// <param name="features">Optional features</param> |
| | 22 | | /// <returns> |
| | 23 | | /// A list of processes that lock at least one of the specified paths. |
| | 24 | | /// </returns> |
| | 25 | | /// <exception cref="ArgumentNullException"><paramref name="paths"/> is <c>null</c>.</exception> |
| | 26 | | /// <exception cref="PlatformNotSupportedException"> |
| | 27 | | /// The current platform is not supported. This exception is only thrown, when the <paramref name="features"/> |
| | 28 | | /// includes the <see cref="LockManagerFeatures.ThrowIfNotSupported"/> flag. Otherwise the function will |
| | 29 | | /// simply return an empty enumeration when a platform is not supported. |
| | 30 | | /// </exception> |
| | 31 | | public static IEnumerable<ProcessInfo> GetLockingProcessInfos(string[] paths, LockManagerFeatures features = default |
| | 32 | | { |
| 2 | 33 | | if (paths == null) |
| 2 | 34 | | throw new ArgumentNullException(nameof(paths)); |
| | 35 | |
|
| 2 | 36 | | HashSet<ProcessInfo> processInfos = []; |
| 2 | 37 | | List<string>? directories = (features & LockManagerFeatures.CheckDirectories) != 0 ? [] : null; |
| | 38 | |
|
| 2 | 39 | | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| | 40 | | { |
| 2 | 41 | | if ((features & LockManagerFeatures.UseLowLevelApi) != 0) |
| | 42 | | { |
| 2 | 43 | | processInfos = NtDll.GetLockingProcessInfos(paths, ref directories); |
| | 44 | | } |
| | 45 | | else |
| | 46 | | { |
| 2 | 47 | | processInfos = RestartManager.GetLockingProcessInfos(paths, ref directories); |
| | 48 | | } |
| | 49 | |
|
| 2 | 50 | | if (directories?.Count > 0) |
| | 51 | | { |
| 2 | 52 | | var matches = NtDll.GetProcessesByWorkingDirectory(directories); |
| 2 | 53 | | foreach (var match in matches) |
| | 54 | | { |
| 2 | 55 | | processInfos.Add(match.Value); |
| | 56 | | } |
| | 57 | | } |
| | 58 | | } |
| | 59 | | #if NET |
| | 60 | | // Linux sources are only build when building for .NET, not for .NET Framework. |
| 0 | 61 | | else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) |
| | 62 | | { |
| 0 | 63 | | processInfos = ProcFileSystem.GetLockingProcessInfos(paths, ref directories); |
| | 64 | |
|
| 0 | 65 | | if (directories?.Count > 0) |
| | 66 | | { |
| 0 | 67 | | var matches = ProcFileSystem.GetProcessesByWorkingDirectory(directories); |
| 0 | 68 | | foreach (var match in matches) |
| | 69 | | { |
| 0 | 70 | | processInfos.Add(match.Value); |
| | 71 | | } |
| | 72 | | } |
| | 73 | | } |
| | 74 | | #endif |
| | 75 | | else |
| | 76 | | { |
| 0 | 77 | | if ((features & LockManagerFeatures.ThrowIfNotSupported) != 0) |
| | 78 | | { |
| 0 | 79 | | throw new PlatformNotSupportedException("Current OS platform is not supported"); |
| | 80 | | } |
| | 81 | | } |
| | 82 | |
|
| 2 | 83 | | return processInfos; |
| | 84 | | } |
| | 85 | |
|
| | 86 | | public static IEnumerable<string> GetKnownCriticalProcesses() |
| | 87 | | { |
| 0 | 88 | | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| | 89 | | { |
| 0 | 90 | | return Windows.NativeMethods.GetKnownCriticalProcesses(); |
| | 91 | | } |
| | 92 | | #if NET |
| | 93 | | // Linux sources are only build when building for .NET, not for .NET Framework. |
| 0 | 94 | | else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) |
| | 95 | | { |
| 0 | 96 | | return Linux.NativeMethods.GetKnownCriticalProcesses(); |
| | 97 | | } |
| | 98 | | #endif |
| 0 | 99 | | return []; |
| | 100 | | } |
| | 101 | |
|
| | 102 | | public static IEnumerable<IProcessDetails> GetAllProcesses() |
| | 103 | | { |
| 0 | 104 | | if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) |
| | 105 | | { |
| 0 | 106 | | return NtDll.GetAllProcesses(); |
| | 107 | | } |
| | 108 | | #if NET |
| | 109 | | // Linux sources are only build when building for .NET, not for .NET Framework. |
| 0 | 110 | | else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) |
| | 111 | | { |
| 0 | 112 | | return ProcFileSystem.GetAllProcesses(); |
| | 113 | | } |
| | 114 | | #endif |
| | 115 | | else |
| | 116 | | { |
| 0 | 117 | | throw new PlatformNotSupportedException("Current OS platform is not supported"); |
| | 118 | | } |
| | 119 | | } |
| | 120 | | } |