Não pode escolher mais do que 25 tópicos Os tópicos devem começar com uma letra ou um número, podem incluir traços ('-') e podem ter até 35 caracteres.

File.EnumerateHardlinks.cs 8.0 KiB

há 7 anos
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170
  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.Collections.Generic;
  23. using System.Diagnostics.CodeAnalysis;
  24. using System.Runtime.InteropServices;
  25. using System.Security;
  26. using System.Text;
  27. namespace Alphaleonis.Win32.Filesystem
  28. {
  29. public static partial class File
  30. {
  31. #region EnumerateHardlinks
  32. /// <summary>[AlphaFS] Creates an enumeration of all the hard links to the specified <paramref name="path"/>.</summary>
  33. /// <param name="path">The name of the file.</param>
  34. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  35. /// <returns>An enumerable collection of <see cref="string"/> of all the hard links to the specified <paramref name="path"/></returns>
  36. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Hardlinks")]
  37. [SecurityCritical]
  38. public static IEnumerable<string> EnumerateHardlinks(string path, PathFormat pathFormat)
  39. {
  40. return EnumerateHardlinksCore(null, path, pathFormat);
  41. }
  42. /// <summary>[AlphaFS] Creates an enumeration of all the hard links to the specified <paramref name="path"/>.</summary>
  43. /// <param name="path">The name of the file.</param>
  44. /// <returns>An enumerable collection of <see cref="string"/> of all the hard links to the specified <paramref name="path"/></returns>
  45. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Hardlinks")]
  46. [SecurityCritical]
  47. public static IEnumerable<string> EnumerateHardlinks(string path)
  48. {
  49. return EnumerateHardlinksCore(null, path, PathFormat.RelativePath);
  50. }
  51. /// <summary>[AlphaFS] Creates an enumeration of all the hard links to the specified <paramref name="path"/>.</summary>
  52. /// <param name="transaction">The transaction.</param>
  53. /// <param name="path">The name of the file.</param>
  54. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  55. /// <returns>An enumerable collection of <see cref="string"/> of all the hard links to the specified <paramref name="path"/></returns>
  56. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Hardlinks")]
  57. [SecurityCritical]
  58. public static IEnumerable<string> EnumerateHardlinksTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  59. {
  60. return EnumerateHardlinksCore(transaction, path, pathFormat);
  61. }
  62. /// <summary>[AlphaFS] Creates an enumeration of all the hard links to the specified <paramref name="path"/>.</summary>
  63. /// <param name="transaction">The transaction.</param>
  64. /// <param name="path">The name of the file.</param>
  65. /// <returns>An enumerable collection of <see cref="string"/> of all the hard links to the specified <paramref name="path"/></returns>
  66. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Hardlinks")]
  67. [SecurityCritical]
  68. public static IEnumerable<string> EnumerateHardlinksTransacted(KernelTransaction transaction, string path)
  69. {
  70. return EnumerateHardlinksCore(transaction, path, PathFormat.RelativePath);
  71. }
  72. #endregion // EnumerateHardlinks
  73. #region Internal Methods
  74. /// <summary>[AlphaFS] Creates an enumeration of all the hard links to the specified <paramref name="path"/>.</summary>
  75. /// <exception cref="PlatformNotSupportedException"/>
  76. /// <param name="transaction">The transaction.</param>
  77. /// <param name="path">The name of the file.</param>
  78. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  79. /// <returns>An enumerable collection of <see cref="string"/> of all the hard links to the specified <paramref name="path"/></returns>
  80. internal static IEnumerable<string> EnumerateHardlinksCore(KernelTransaction transaction, string path, PathFormat pathFormat)
  81. {
  82. if (!NativeMethods.IsAtLeastWindowsVista)
  83. throw new PlatformNotSupportedException(Resources.Requires_Windows_Vista_Or_Higher);
  84. string pathLp = Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck);
  85. // Default buffer length, will be extended if needed, although this should not happen.
  86. uint length = NativeMethods.MaxPathUnicode;
  87. StringBuilder builder = new StringBuilder((int)length);
  88. getFindFirstFileName:
  89. using (SafeFindFileHandle handle = transaction == null
  90. // FindFirstFileName() / FindFirstFileNameTransacted()
  91. // In the ANSI version of this function, the name is limited to MAX_PATH characters.
  92. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
  93. // 2013-01-13: MSDN does not confirm LongPath usage but a Unicode version of this function exists.
  94. ? NativeMethods.FindFirstFileName(pathLp, 0, out length, builder)
  95. : NativeMethods.FindFirstFileNameTransacted(pathLp, 0, out length, builder, transaction.SafeHandle))
  96. {
  97. int lastError = Marshal.GetLastWin32Error();
  98. if (handle.IsInvalid)
  99. {
  100. handle.Close();
  101. switch ((uint) lastError)
  102. {
  103. case Win32Errors.ERROR_MORE_DATA:
  104. builder = new StringBuilder((int) length);
  105. goto getFindFirstFileName;
  106. default:
  107. // If the function fails, the return value is INVALID_HANDLE_VALUE.
  108. NativeError.ThrowException(lastError, pathLp);
  109. break;
  110. }
  111. }
  112. yield return builder.ToString();
  113. //length = NativeMethods.MaxPathUnicode;
  114. //builder = new StringBuilder((int)length);
  115. do
  116. {
  117. while (!NativeMethods.FindNextFileName(handle, out length, builder))
  118. {
  119. lastError = Marshal.GetLastWin32Error();
  120. switch ((uint) lastError)
  121. {
  122. // We've reached the end of the enumeration.
  123. case Win32Errors.ERROR_HANDLE_EOF:
  124. yield break;
  125. case Win32Errors.ERROR_MORE_DATA:
  126. builder = new StringBuilder((int) length);
  127. continue;
  128. default:
  129. //If the function fails, the return value is zero (0).
  130. NativeError.ThrowException(lastError);
  131. break;
  132. }
  133. }
  134. yield return builder.ToString();
  135. } while (true);
  136. }
  137. }
  138. #endregion // Internal Methods
  139. }
  140. }