public void LockInformationAvailable(LockManagerFeatures features) { var process = Process.GetCurrentProcess(); TestHelper.CreateLockSituation((_, fileName) => { var processInfos = LockManager.GetLockingProcessInfos(new[] { fileName }, features).ToList(); Assert.AreEqual(1, processInfos.Count); Assert.AreEqual(process.Id, processInfos[0].ProcessId); Assert.AreEqual(process.SessionId, processInfos[0].SessionId); Assert.AreEqual(process.StartTime, processInfos[0].StartTime); Assert.IsNotNull(processInfos[0].ApplicationName); Assert.AreEqual(processInfos[0].ExecutableFullPath?.ToLowerInvariant(), process.MainModule.FileName.ToLowerInvariant()); // Might contain domain, computername, etc. in SAM form StringAssert.Contains(processInfos[0].Owner?.ToLowerInvariant(), Environment.UserName.ToLowerInvariant()); // Might have an .exe suffix or not. StringAssert.Contains(processInfos[0].ExecutableName?.ToLowerInvariant(), process.ProcessName.ToLowerInvariant()); }); }
public void RethrownExceptionContainsInformation(LockManagerFeatures features) { TestHelper.CreateLockSituation((ex, fileName) => { var processInfos = LockManager.GetLockingProcessInfos(new[] { fileName }, features).ToList(); Assert.AreEqual(1, processInfos.Count); // Sanity, has been tested in LockManagerTests var expectedMessageContents = new StringBuilder(); ProcessInfo.Format(expectedMessageContents, processInfos, new[] { fileName }); try { bool result = ex.RethrowWithLockingInformation(fileName, features); Assert.IsTrue(result); } catch (Exception re) { StringAssert.Contains(re.Message, expectedMessageContents.ToString()); Assert.AreEqual(ex.HResult, re.HResult); } }); }
public static IEnumerable <ProcessInfo> GetLockingProcessInfos(string[] paths, LockManagerFeatures features = default) { if (RuntimeInformation.IsOSPlatform(OSPlatform.Windows)) { if ((features & LockManagerFeatures.UseLowLevelApi) != 0) { return(NtDll.GetLockingProcessInfos(paths)); } return(RestartManager.GetLockingProcessInfos(paths)); } else if (RuntimeInformation.IsOSPlatform(OSPlatform.Linux)) { return(ProcFileSystem.GetLockingProcessInfos(paths)); } else { if ((features & LockManagerFeatures.ThrowIfNotSupported) != 0) { throw new NotSupportedException("Current OS platform is not supported"); } return(Enumerable.Empty <ProcessInfo>()); } }
public static bool RethrowWithLockingInformation(this Exception ex, string[] fileNames, LockManagerFeatures features = default) { var ioex = ex as IOException; if (ioex != null && ioex.IsFileLocked()) { // It is a race to get the lockers, while they are still there. So do this as early as possible. var lockers = LockManager.GetLockingProcessInfos(fileNames, features).ToList(); if (lockers.Any()) { const int max = 10; var sb = new StringBuilder(); sb.Append(ex.Message); sb.Append(" "); ProcessInfo.Format(sb, lockers, fileNames, max); var exception = new IOException(sb.ToString(), ex); #if NET472 if (s_setErrorCodeMethod.Value != null) { s_setErrorCodeMethod.Value.Invoke(exception, new object[] { ex.HResult }); } #endif #if NETCOREAPP exception.HResult = ex.HResult; #endif throw exception; } } return(false); }
public static bool RethrowWithLockingInformation(this Exception ex, string fileName, LockManagerFeatures features = default) { return(RethrowWithLockingInformation(ex, new[] { fileName }, features)); }