Ви не можете вибрати більше 25 тем Теми мають розпочинатися з літери або цифри, можуть містити дефіси (-) і не повинні перевищувати 35 символів.

229 рядки
12 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.Diagnostics.CodeAnalysis;
  22. using System.IO;
  23. using System.Security;
  24. namespace Alphaleonis.Win32.Filesystem
  25. {
  26. public static partial class File
  27. {
  28. #region Public Methods
  29. /// <summary>Determines whether the specified file exists.</summary>
  30. /// <remarks>
  31. /// <para>MSDN: .NET 3.5+: Trailing spaces are removed from the end of the
  32. /// <paramref name="path"/> parameter before checking whether the directory exists.</para>
  33. /// <para>The Exists method returns <see langword="false"/> if any error occurs while trying to
  34. /// determine if the specified file exists.</para>
  35. /// <para>This can occur in situations that raise exceptions such as passing a file name with
  36. /// invalid characters or too many characters, a failing or missing disk, or if the caller does not have permission to read the
  37. /// file.</para>
  38. /// <para>The Exists method should not be used for path validation,
  39. /// this method merely checks if the file specified in path exists.</para>
  40. /// <para>Passing an invalid path to Exists returns false.</para>
  41. /// <para>Be aware that another process can potentially do something with the file in
  42. /// between the time you call the Exists method and perform another operation on the file, such as Delete.</para>
  43. /// </remarks>
  44. /// <param name="path">The file to check.</param>
  45. /// <returns>
  46. /// Returns <see langword="true"/> if the caller has the required permissions and
  47. /// <paramref name="path"/> contains the name of an existing file; otherwise,
  48. /// <see langword="false"/>
  49. /// </returns>
  50. [SecurityCritical]
  51. public static bool Exists(string path)
  52. {
  53. return ExistsCore(false, null, path, PathFormat.RelativePath);
  54. }
  55. /// <summary>[AlphaFS] Determines whether the specified file exists.</summary>
  56. /// <remarks>
  57. /// <para>MSDN: .NET 3.5+: Trailing spaces are removed from the end of the
  58. /// <paramref name="path"/> parameter before checking whether the directory exists.</para>
  59. /// <para>The Exists method returns <see langword="false"/> if any error occurs while trying to
  60. /// determine if the specified file exists.</para>
  61. /// <para>This can occur in situations that raise exceptions such as passing a file name with
  62. /// invalid characters or too many characters,</para>
  63. /// <para>a failing or missing disk, or if the caller does not have permission to read the
  64. /// file.</para>
  65. /// <para>The Exists method should not be used for path validation, this method merely checks
  66. /// if the file specified in path exists.</para>
  67. /// <para>Passing an invalid path to Exists returns false.</para>
  68. /// <para>Be aware that another process can potentially do something with the file in
  69. /// between the time you call the Exists method and perform another operation on the file, such
  70. /// as Delete.</para>
  71. /// </remarks>
  72. /// <param name="path">The file to check.</param>
  73. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  74. /// <returns>
  75. /// <para>Returns <see langword="true"/> if the caller has the required permissions and
  76. /// <paramref name="path"/> contains the name of an existing file; otherwise,
  77. /// <see langword="false"/></para>
  78. /// </returns>
  79. [SecurityCritical]
  80. public static bool Exists(string path, PathFormat pathFormat)
  81. {
  82. return ExistsCore(false, null, path, pathFormat);
  83. }
  84. #region Transactional
  85. /// <summary>
  86. /// [AlphaFS] Determines whether the specified file exists.
  87. /// </summary>
  88. /// <remarks>
  89. /// <para>MSDN: .NET 3.5+: Trailing spaces are removed from the end of the
  90. /// <paramref name="path"/> parameter before checking whether the directory exists.</para>
  91. /// <para>The Exists method returns <see langword="false"/> if any error occurs while trying to
  92. /// determine if the specified file exists.</para>
  93. /// <para>This can occur in situations that raise exceptions such as passing a file name with
  94. /// invalid characters or too many characters,</para>
  95. /// <para>a failing or missing disk, or if the caller does not have permission to read the
  96. /// file.</para>
  97. /// <para>The Exists method should not be used for path validation,</para>
  98. /// <para>this method merely checks if the file specified in path exists.</para>
  99. /// <para>Passing an invalid path to Exists returns false.</para>
  100. /// <para>Be aware that another process can potentially do something with the file in
  101. /// between</para>
  102. /// <para>the time you call the Exists method and perform another operation on the file, such
  103. /// as Delete.</para>
  104. /// </remarks>
  105. /// <param name="transaction">The transaction.</param>
  106. /// <param name="path">The file to check.</param>
  107. /// <returns>
  108. /// <para>Returns <see langword="true"/> if the caller has the required permissions</para>
  109. /// <para>and <paramref name="path"/> contains the name of an existing file; otherwise,
  110. /// <see langword="false"/></para>
  111. /// </returns>
  112. [SecurityCritical]
  113. public static bool ExistsTransacted(KernelTransaction transaction, string path)
  114. {
  115. return ExistsCore(false, transaction, path, PathFormat.RelativePath);
  116. }
  117. /// <summary>
  118. /// [AlphaFS] Determines whether the specified file exists.
  119. /// </summary>
  120. /// <remarks>
  121. /// <para>MSDN: .NET 3.5+: Trailing spaces are removed from the end of the
  122. /// <paramref name="path"/> parameter before checking whether the directory exists.</para>
  123. /// <para>The Exists method returns <see langword="false"/> if any error occurs while trying to
  124. /// determine if the specified file exists.</para>
  125. /// <para>This can occur in situations that raise exceptions such as passing a file name with
  126. /// invalid characters or too many characters,</para>
  127. /// <para>a failing or missing disk, or if the caller does not have permission to read the
  128. /// file.</para>
  129. /// <para>The Exists method should not be used for path validation,</para>
  130. /// <para>this method merely checks if the file specified in path exists.</para>
  131. /// <para>Passing an invalid path to Exists returns false.</para>
  132. /// <para>Be aware that another process can potentially do something with the file in
  133. /// between</para>
  134. /// <para>the time you call the Exists method and perform another operation on the file, such
  135. /// as Delete.</para>
  136. /// </remarks>
  137. /// <param name="transaction">The transaction.</param>
  138. /// <param name="path">The file to check.</param>
  139. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  140. /// <returns>
  141. /// <para>Returns <see langword="true"/> if the caller has the required permissions</para>
  142. /// <para>and <paramref name="path"/> contains the name of an existing file; otherwise,
  143. /// <see langword="false"/></para>
  144. /// </returns>
  145. [SecurityCritical]
  146. public static bool ExistsTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  147. {
  148. return ExistsCore(false, transaction, path, pathFormat);
  149. }
  150. #endregion // Transacted
  151. #endregion
  152. #region Internal Methods
  153. /// <summary>Determines whether the specified file or directory exists.</summary>
  154. /// <remarks>
  155. /// <para>MSDN: .NET 3.5+: Trailing spaces are removed from the end of the <paramref name="path"/> parameter before checking whether
  156. /// the directory exists.</para>
  157. /// <para>The Exists method returns <see langword="false"/> if any error occurs while trying to determine if the specified file
  158. /// exists.</para>
  159. /// <para>This can occur in situations that raise exceptions such as passing a file name with invalid characters or too many characters,
  160. /// </para>
  161. /// <para>a failing or missing disk, or if the caller does not have permission to read the file.</para>
  162. /// <para>The Exists method should not be used for path validation,
  163. /// this method merely checks if the file specified in path exists.</para>
  164. /// <para>Passing an invalid path to Exists returns false.</para>
  165. /// <para>Be aware that another process can potentially do something with the file in between
  166. /// the time you call the Exists method and perform another operation on the file, such as Delete.</para>
  167. /// </remarks>
  168. /// <param name="isFolder">Specifies that <paramref name="path"/> is a file or directory.</param>
  169. /// <param name="transaction">The transaction.</param>
  170. /// <param name="path">The file to check.</param>
  171. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  172. /// <returns>
  173. /// <para>Returns <see langword="true"/> if the caller has the required permissions</para>
  174. /// <para>and <paramref name="path"/> contains the name of an existing file or directory; otherwise, <see langword="false"/></para>
  175. /// </returns>
  176. [SuppressMessage("Microsoft.Design", "CA1031:DoNotCatchGeneralExceptionTypes")]
  177. [SecurityCritical]
  178. internal static bool ExistsCore(bool isFolder, KernelTransaction transaction, string path, PathFormat pathFormat)
  179. {
  180. // Will be caught later and be thrown as an ArgumentException or ArgumentNullException.
  181. // Let's take a shorter route, preventing an Exception from being thrown altogether.
  182. if (Utils.IsNullOrWhiteSpace(path))
  183. return false;
  184. // DriveInfo.IsReady() will fail.
  185. //
  186. //// After normalizing, check whether path ends in directory separator.
  187. //// Otherwise, FillAttributeInfoCore removes it and we may return a false positive.
  188. //string pathRp = Path.GetRegularPathCore(path, true, false, false, false);
  189. //if (pathRp.Length > 0 && Path.IsDVsc(pathRp[pathRp.Length - 1], false))
  190. // return false;
  191. try
  192. {
  193. string pathLp = Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.CheckInvalidPathChars | GetFullPathOptions.ContinueOnNonExist);
  194. var data = new NativeMethods.WIN32_FILE_ATTRIBUTE_DATA();
  195. int dataInitialised = FillAttributeInfoCore(transaction, pathLp, ref data, false, true);
  196. return (dataInitialised == Win32Errors.ERROR_SUCCESS &&
  197. data.dwFileAttributes != (FileAttributes) (-1) &&
  198. (isFolder
  199. ? (data.dwFileAttributes & FileAttributes.Directory) != 0
  200. : (data.dwFileAttributes & FileAttributes.Directory) == 0));
  201. }
  202. catch
  203. {
  204. return false;
  205. }
  206. }
  207. #endregion // Internal Methods
  208. }
  209. }