Esempio n. 1
0
    public static void GetLockingProcesses_WhenGivenEmptyFileInfoCollection_ReturnsEmptyResult()
    {
        var arg    = Array.Empty <FileInfo>();
        var result = RestartManager.GetLockingProcesses(arg);

        Assert.That(result, Is.Empty);
    }
Esempio n. 2
0
        public static void GetLockingProcesses_WhenGivenEmptyFileInfoCollection_ReturnsEmptyResult()
        {
            var arg    = new List <FileInfo>();
            var result = RestartManager.GetLockingProcesses(arg);

            Assert.AreEqual(0, result.Count);
        }
Esempio n. 3
0
        public static void GetLockingProcesses_WhenLockingOnPathAndGivenDirectory_ReturnsCorrectProcess()
        {
            var tmpFilePath = Path.GetTempFileName();
            var tmpDirPath  = Path.GetDirectoryName(tmpFilePath);

            tmpDirPath = Path.Combine(tmpDirPath, Guid.NewGuid().ToString());

            var tmpDir = new DirectoryInfo(tmpDirPath);

            tmpDir.Create();
            var tmpDirFile = Path.Combine(tmpDirPath, Path.GetFileName(tmpFilePath));

            File.Move(tmpFilePath, tmpDirFile);

            using (var file = File.Open(tmpDirFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
            {
                var lockingProcs = RestartManager.GetLockingProcesses(tmpDir);
                var process      = Process.GetCurrentProcess();

                var lockingId = lockingProcs.Single().ProcessId;
                var currentId = process.Id;

                Assert.AreEqual(currentId, lockingId);
            }

            tmpDir.Delete(true);
        }
Esempio n. 4
0
    public static void GetLockingProcesses_WhenLockingOnPathAndGivenString_ReturnsNonEmptyLockingSet()
    {
        using var tmpPath = new TemporaryFile();
        using var _       = tmpPath.FileInfo.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
        var lockingProcs = RestartManager.GetLockingProcesses(tmpPath.FilePath);

        Assert.That(lockingProcs, Is.Not.Empty);
    }
Esempio n. 5
0
    public static void GetLockingProcesses_WhenLockingOnPath_ReturnsCorrectNumberOfLocks()
    {
        using var tmpPath = new TemporaryFile();
        using var _       = tmpPath.FileInfo.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
        var lockingProcs = RestartManager.GetLockingProcesses(tmpPath.FileInfo);

        Assert.That(lockingProcs, Has.One.Items);
    }
Esempio n. 6
0
    /// <summary>
    /// Retrieves the set of processes that are currently locking the file (if any).
    /// </summary>
    /// <param name="fileInfo">A file to test.</param>
    /// <returns>A set of processes that hold a lock on <paramref name="fileInfo"/>.</returns>
    /// <exception cref="ArgumentNullException"><paramref name="fileInfo"/> is <c>null</c>.</exception>
    public static IReadOnlyCollection <IProcessInfo> GetLockingProcesses(this FileInfo fileInfo)
    {
        if (fileInfo == null)
        {
            throw new ArgumentNullException(nameof(fileInfo));
        }

        return(RestartManager.GetLockingProcesses(fileInfo.FullName));
    }
Esempio n. 7
0
    public static void GetLockingProcesses_WhenLockingOnPathAndGivenDirectory_ReturnsNonEmptyLockingSet()
    {
        using var tmpDir = new TemporaryDirectory();
        var tmpFile = new FileInfo(Path.Combine(tmpDir.DirectoryPath, Path.GetRandomFileName()));

        using var _ = tmpFile.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
        var lockingProcs = RestartManager.GetLockingProcesses(tmpDir.DirectoryInfo);

        Assert.That(lockingProcs, Is.Not.Empty);
    }
Esempio n. 8
0
        public static void GetLockingProcesses_WhenLockingOnPath_ReturnsCorrectNumberOfLocks()
        {
            var tmpPath = new FileInfo(Path.GetTempFileName());

            using (var file = tmpPath.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
            {
                var lockingProcs = RestartManager.GetLockingProcesses(tmpPath);
                Assert.AreEqual(1, lockingProcs.Count);
            }

            tmpPath.Delete();
        }
Esempio n. 9
0
        public static void GetLockingProcesses_WhenLockingOnPathAndGivenString_ReturnsNonEmptyLockingSet()
        {
            var tmpPath = Path.GetTempFileName();

            using (var file = File.Open(tmpPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
            {
                var lockingProcs = RestartManager.GetLockingProcesses(tmpPath);
                Assert.IsTrue(lockingProcs.Count > 0);
            }

            File.Delete(tmpPath);
        }
Esempio n. 10
0
    public static void GetLockingProcesses_WhenLockingOnPathAndGivenString_ReturnsCorrectProcess()
    {
        using var tmpPath = new TemporaryFile();
        using var _       = tmpPath.FileInfo.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
        var lockingProcs = RestartManager.GetLockingProcesses(tmpPath.FilePath);
        var process      = Process.GetCurrentProcess();

        var lockingId = lockingProcs.Single().ProcessId;
        var currentId = process.Id;

        Assert.That(currentId, Is.EqualTo(lockingId));
    }
Esempio n. 11
0
    /// <summary>
    /// Determines whether the file is locked by any process.
    /// </summary>
    /// <param name="fileInfo">A file to test.</param>
    /// <returns><c>true</c> if any processes hold a lock on the file, otherwise <c>false</c>.</returns>
    /// <exception cref="ArgumentNullException"><paramref name="fileInfo"/> is <c>null</c>.</exception>
    public static bool IsFileLocked(this FileInfo fileInfo)
    {
        if (fileInfo == null)
        {
            throw new ArgumentNullException(nameof(fileInfo));
        }

        if (!Platform.SupportsRestartManager)
        {
            return(IsSimpleFileLocked(fileInfo));
        }

        return(RestartManager.GetLockingProcesses(fileInfo.FullName).Count > 0);
    }
Esempio n. 12
0
    public static void GetLockingProcesses_WhenLockingOnPathAndGivenDirectory_ReturnsCorrectProcess()
    {
        using var tmpDir = new TemporaryDirectory();
        var tmpFile = new FileInfo(Path.Combine(tmpDir.DirectoryPath, Path.GetRandomFileName()));

        using var _ = tmpFile.Open(FileMode.OpenOrCreate, FileAccess.Write, FileShare.None);
        var lockingProcs = RestartManager.GetLockingProcesses(tmpDir.DirectoryInfo);
        var process      = Process.GetCurrentProcess();

        var lockingId = lockingProcs.Single().ProcessId;
        var currentId = process.Id;

        Assert.That(currentId, Is.EqualTo(lockingId));
    }
Esempio n. 13
0
    /// <summary>
    /// When an exception is thrown due to a lock held on a file, this method will rethrow an exception with more information on which files were locked and which processes were holding the lock.
    /// </summary>
    /// <param name="exception">The exception that was thrown due to a lock held on a file.</param>
    /// <param name="fileNames">A collection of file paths that could have been locked upon.</param>
    /// <returns><c>false</c> if the exception was not held due to a lock on a file, or if there are no locked files.</returns>
    /// <exception cref="ArgumentNullException"><paramref name="exception"/> or <paramref name="fileNames"/> is <c>null</c>.</exception>
    /// <exception cref="ArgumentException">An entry of the <paramref name="fileNames"/> collection is <c>null</c>.</exception>
    public static bool RethrowWithLockingInformation(this Exception exception, IEnumerable <string> fileNames)
    {
        if (exception == null)
        {
            throw new ArgumentNullException(nameof(exception));
        }
        if (fileNames == null)
        {
            throw new ArgumentNullException(nameof(fileNames));
        }
        if (fileNames.Any(f => f == null))
        {
            throw new ArgumentException("A null filename was provided.", nameof(fileNames));
        }

        if (exception is not IOException ioex || !ioex.IsFileLocked())
        {
            return(false);
        }

        var lockers = RestartManager.GetLockingProcesses(fileNames);

        if (lockers.Count == 0)
        {
            return(false);
        }

        const int max     = 10;
        var       builder = new StringBuilder();

        builder.Append(exception.Message);
        builder.Append(' ');

        var message = FormatLockingMessage(lockers, fileNames, max);

        builder.Append(message);

        // Unable to set HResult *and* InnerException via public methods/ctors.
        // Must use reflection to set the HResult while using the ctor to set the InnerException.
        // Nasty but necessary.
        var ex      = new IOException(builder.ToString(), exception);
        var hresult = Marshal.GetHRForException(exception);

        SetHResultMethod?.Invoke(ex, new object[] { hresult });

        throw ex;
    }
Esempio n. 14
0
        public static void GetLockingProcesses_WhenLockingOnPathAndGivenString_ReturnsCorrectProcess()
        {
            var tmpPath = Path.GetTempFileName();

            using (var file = File.Open(tmpPath, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
            {
                var lockingProcs = RestartManager.GetLockingProcesses(tmpPath);
                var process      = Process.GetCurrentProcess();

                var lockingId = lockingProcs.Single().ProcessId;
                var currentId = process.Id;

                Assert.AreEqual(currentId, lockingId);
            }

            File.Delete(tmpPath);
        }
Esempio n. 15
0
        public static void GetLockingProcesses_WhenLockingOnPathAndGivenDirectory_ReturnsNonEmptyLockingSet()
        {
            var tmpFilePath = Path.GetTempFileName();
            var tmpDirPath  = Path.GetDirectoryName(tmpFilePath);

            tmpDirPath = Path.Combine(tmpDirPath, Guid.NewGuid().ToString());

            var tmpDir = new DirectoryInfo(tmpDirPath);

            tmpDir.Create();
            var tmpDirFile = Path.Combine(tmpDirPath, Path.GetFileName(tmpFilePath));

            File.Move(tmpFilePath, tmpDirFile);

            using (var file = File.Open(tmpDirFile, FileMode.OpenOrCreate, FileAccess.Write, FileShare.None))
            {
                var lockingProcs = RestartManager.GetLockingProcesses(tmpDir);
                Assert.IsTrue(lockingProcs.Count > 0);
            }

            tmpDir.Delete(true);
        }
Esempio n. 16
0
 public static void GetLockingProcesses_WhenGivenNullDirectoryInfoArgs_ThrowsArgNullException()
 {
     Assert.Throws <ArgumentNullException>(() => RestartManager.GetLockingProcesses((DirectoryInfo)null));
 }
Esempio n. 17
0
 public static void GetLockingProcesses_WhenGivenNullFileInfoArgs_ThrowsArgNullException()
 {
     Assert.Throws <ArgumentNullException>(() => RestartManager.GetLockingProcesses((IEnumerable <FileInfo>)null));
 }
Esempio n. 18
0
 public static void GetLockingProcesses_WhenGivenNullStringArgs_ThrowsArgNullException()
 {
     Assert.That(() => RestartManager.GetLockingProcesses((IEnumerable <string>)null), Throws.ArgumentNullException);
 }