You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

119 lines
5.5 KiB

  1. /* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
  2. *
  3. * Permission is hereby granted, free of charge, to any person obtaining a copy
  4. * of this software and associated documentation files (the "Software"), to deal
  5. * in the Software without restriction, including without limitation the rights
  6. * to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
  7. * copies of the Software, and to permit persons to whom the Software is
  8. * furnished to do so, subject to the following conditions:
  9. *
  10. * The above copyright notice and this permission notice shall be included in
  11. * all copies or substantial portions of the Software.
  12. *
  13. * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
  14. * IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
  15. * FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
  16. * AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
  17. * LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
  18. * OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
  19. * THE SOFTWARE.
  20. */
  21. using System;
  22. using System.IO;
  23. using System.Runtime.InteropServices;
  24. using System.Security;
  25. namespace Alphaleonis.Win32.Filesystem
  26. {
  27. partial class File
  28. {
  29. /// <summary>[AlphaFS] Determines whether the specified file is in use (locked).</summary>
  30. /// <returns>Returns <see langword="true"/> if the specified file is in use (locked); otherwise, <see langword="false"/></returns>
  31. /// <exception cref="IOException"/>
  32. /// <exception cref="Exception"/>
  33. /// <param name="path">The file to check.</param>
  34. [SecurityCritical]
  35. public static bool IsLocked(string path)
  36. {
  37. return IsLockedCore(null, path, PathFormat.RelativePath);
  38. }
  39. /// <summary>[AlphaFS] Determines whether the specified file is in use (locked).</summary>
  40. /// <returns>Returns <see langword="true"/> if the specified file is in use (locked); otherwise, <see langword="false"/></returns>
  41. /// <exception cref="FileNotFoundException"></exception>
  42. /// <exception cref="IOException"/>
  43. /// <exception cref="Exception"/>
  44. /// <param name="path">The file to check.</param>
  45. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  46. [SecurityCritical]
  47. public static bool IsLocked(string path, PathFormat pathFormat)
  48. {
  49. return IsLockedCore(null, path, pathFormat);
  50. }
  51. #region Transactional
  52. /// <summary>[AlphaFS] Determines whether the specified file is in use (locked).</summary>
  53. /// <returns>Returns <see langword="true"/> if the specified file is in use (locked); otherwise, <see langword="false"/></returns>
  54. /// <exception cref="FileNotFoundException"></exception>
  55. /// <exception cref="IOException"/>
  56. /// <exception cref="Exception"/>
  57. /// <param name="transaction">The transaction.</param>
  58. /// <param name="path">The file to check.</param>
  59. [SecurityCritical]
  60. public static bool IsLockedTransacted(KernelTransaction transaction, string path)
  61. {
  62. return IsLockedCore(transaction, path, PathFormat.RelativePath);
  63. }
  64. /// <summary>[AlphaFS] Determines whether the specified file is in use (locked).</summary>
  65. /// <returns>Returns <see langword="true"/> if the specified file is in use (locked); otherwise, <see langword="false"/></returns>
  66. /// <exception cref="FileNotFoundException"></exception>
  67. /// <exception cref="IOException"/>
  68. /// <exception cref="Exception"/>
  69. /// <param name="transaction">The transaction.</param>
  70. /// <param name="path">The file to check.</param>
  71. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  72. [SecurityCritical]
  73. public static bool IsLockedTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  74. {
  75. return IsLockedCore(transaction, path, pathFormat);
  76. }
  77. #endregion // Transactional
  78. /// <summary>[AlphaFS] Determines whether the specified file is in use (locked).</summary>
  79. /// <returns>Returns <see langword="true"/> if the specified file is in use (locked); otherwise, <see langword="false"/></returns>
  80. /// <exception cref="FileNotFoundException"></exception>
  81. /// <exception cref="IOException"/>
  82. /// <exception cref="Exception"/>
  83. /// <param name="transaction">The transaction.</param>
  84. /// <param name="path">The file to check.</param>
  85. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  86. [SecurityCritical]
  87. internal static bool IsLockedCore(KernelTransaction transaction, string path, PathFormat pathFormat)
  88. {
  89. try
  90. {
  91. // Use FileAccess.Read since FileAccess.ReadWrite always fails when file is read-only.
  92. using (OpenCore(transaction, path, FileMode.Open, FileAccess.Read, FileShare.None, ExtendedFileAttributes.Normal, null, null, pathFormat)) {}
  93. }
  94. catch (IOException ex)
  95. {
  96. int lastError = Marshal.GetHRForException(ex) & NativeMethods.OverflowExceptionBitShift;
  97. if (lastError == Win32Errors.ERROR_SHARING_VIOLATION || lastError == Win32Errors.ERROR_LOCK_VIOLATION)
  98. return true;
  99. throw;
  100. }
  101. catch (Exception ex)
  102. {
  103. NativeError.ThrowException(Marshal.GetHRForException(ex) & NativeMethods.OverflowExceptionBitShift);
  104. }
  105. return false;
  106. }
  107. }
  108. }