< Summary - Results for net8.0, Release

Information
Class: LockCheck.LockManager
Assembly: LockCheck
File(s): D:\a\LockCheck\LockCheck\src\LockCheck\LockManager.cs
Tag: 117_11660770947
Line coverage
41%
Covered lines: 13
Uncovered lines: 18
Coverable lines: 31
Total lines: 120
Line coverage: 41.9%
Branch coverage
40%
Covered branches: 13
Total branches: 32
Branch coverage: 40.6%
Method coverage

Feature is only available for sponsors

Upgrade to PRO version

Metrics

MethodBranch coverage Crap Score Cyclomatic complexity Line coverage
GetLockingProcessInfos(...)54.17%55.862461.9%
GetKnownCriticalProcesses()0%2040%
GetAllProcesses()0%2040%

File(s)

D:\a\LockCheck\LockCheck\src\LockCheck\LockManager.cs

#LineLine coverage
 1using System;
 2using System.Collections.Generic;
 3using System.Runtime.InteropServices;
 4using LockCheck.Windows;
 5#if NET
 6using LockCheck.Linux;
 7#endif
 8
 9
 10namespace LockCheck;
 11
 12/// <summary>
 13/// Retrieves information about locked files and directories.
 14/// </summary>
 15public 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    {
 233        if (paths == null)
 234            throw new ArgumentNullException(nameof(paths));
 35
 236        HashSet<ProcessInfo> processInfos = [];
 237        List<string>? directories = (features & LockManagerFeatures.CheckDirectories) != 0 ? [] : null;
 38
 239        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
 40        {
 241            if ((features & LockManagerFeatures.UseLowLevelApi) != 0)
 42            {
 243                processInfos = NtDll.GetLockingProcessInfos(paths, ref directories);
 44            }
 45            else
 46            {
 247                processInfos = RestartManager.GetLockingProcessInfos(paths, ref directories);
 48            }
 49
 250            if (directories?.Count > 0)
 51            {
 252                var matches = NtDll.GetProcessesByWorkingDirectory(directories);
 253                foreach (var match in matches)
 54                {
 255                    processInfos.Add(match.Value);
 56                }
 57            }
 58        }
 59#if NET
 60        // Linux sources are only build when building for .NET, not for .NET Framework.
 061        else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
 62        {
 063            processInfos = ProcFileSystem.GetLockingProcessInfos(paths, ref directories);
 64
 065            if (directories?.Count > 0)
 66            {
 067                var matches = ProcFileSystem.GetProcessesByWorkingDirectory(directories);
 068                foreach (var match in matches)
 69                {
 070                    processInfos.Add(match.Value);
 71                }
 72            }
 73        }
 74#endif
 75        else
 76        {
 077            if ((features & LockManagerFeatures.ThrowIfNotSupported) != 0)
 78            {
 079                throw new PlatformNotSupportedException("Current OS platform is not supported");
 80            }
 81        }
 82
 283        return processInfos;
 84    }
 85
 86    public static IEnumerable<string> GetKnownCriticalProcesses()
 87    {
 088        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
 89        {
 090            return Windows.NativeMethods.GetKnownCriticalProcesses();
 91        }
 92#if NET
 93        // Linux sources are only build when building for .NET, not for .NET Framework.
 094        else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
 95        {
 096            return Linux.NativeMethods.GetKnownCriticalProcesses();
 97        }
 98#endif
 099        return [];
 100    }
 101
 102    public static IEnumerable<IProcessDetails> GetAllProcesses()
 103    {
 0104        if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows))
 105        {
 0106            return NtDll.GetAllProcesses();
 107        }
 108#if NET
 109        // Linux sources are only build when building for .NET, not for .NET Framework.
 0110        else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux))
 111        {
 0112            return ProcFileSystem.GetAllProcesses();
 113        }
 114#endif
 115        else
 116        {
 0117            throw new PlatformNotSupportedException("Current OS platform is not supported");
 118        }
 119    }
 120}