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.
 
 

122 lines
5.9 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.Collections.Generic;
  22. using System.Runtime.InteropServices;
  23. using System.Security;
  24. namespace Alphaleonis.Win32.Filesystem
  25. {
  26. partial class File
  27. {
  28. /// <summary>[AlphaFS] Enumerates the streams of type :$DATA from the specified file.</summary>
  29. /// <param name="path">The path to the file to enumerate streams of.</param>
  30. /// <returns>The streams of type :$DATA in the specified file.</returns>
  31. [SecurityCritical]
  32. public static IEnumerable<AlternateDataStreamInfo> EnumerateAlternateDataStreams(string path)
  33. {
  34. return EnumerateAlternateDataStreamsCore(null, path, PathFormat.RelativePath);
  35. }
  36. /// <summary>[AlphaFS] Enumerates the streams of type :$DATA from the specified file.</summary>
  37. /// <param name="path">The path to the file to enumerate streams of.</param>
  38. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  39. /// <returns>The streams of type :$DATA in the specified file.</returns>
  40. [SecurityCritical]
  41. public static IEnumerable<AlternateDataStreamInfo> EnumerateAlternateDataStreams(string path, PathFormat pathFormat)
  42. {
  43. return EnumerateAlternateDataStreamsCore(null, path, pathFormat);
  44. }
  45. /// <summary>[AlphaFS] Enumerates the streams of type :$DATA from the specified file.</summary>
  46. /// <param name="transaction">The transaction.</param>
  47. /// <param name="path">The path to the file to enumerate streams of.</param>
  48. /// <returns>The streams of type :$DATA in the specified file.</returns>
  49. [SecurityCritical]
  50. public static IEnumerable<AlternateDataStreamInfo> EnumerateAlternateDataStreamsTransacted(KernelTransaction transaction, string path)
  51. {
  52. return EnumerateAlternateDataStreamsCore(transaction, path, PathFormat.RelativePath);
  53. }
  54. /// <summary>[AlphaFS] Enumerates the streams of type :$DATA from the specified file.</summary>
  55. /// <param name="transaction">The transaction.</param>
  56. /// <param name="path">The path to the file to enumerate streams of.</param>
  57. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  58. /// <returns>The streams of type :$DATA in the specified file.</returns>
  59. [SecurityCritical]
  60. public static IEnumerable<AlternateDataStreamInfo> EnumerateAlternateDataStreamsTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  61. {
  62. return EnumerateAlternateDataStreamsCore(transaction, path, pathFormat);
  63. }
  64. #region Internal Methods
  65. /// <summary>[AlphaFS] Enumerates the streams of type :$DATA from the specified file or directory.</summary>
  66. /// <param name="transaction">The transaction.</param>
  67. /// <param name="path">The path to the file or directory to enumerate streams of.</param>
  68. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  69. /// <returns>The streams of type :$DATA in the specified file or directory.</returns>
  70. internal static IEnumerable<AlternateDataStreamInfo> EnumerateAlternateDataStreamsCore(KernelTransaction transaction, string path, PathFormat pathFormat)
  71. {
  72. using (var buffer = new SafeGlobalMemoryBufferHandle(Marshal.SizeOf(typeof(NativeMethods.WIN32_FIND_STREAM_DATA))))
  73. {
  74. string pathLp = Path.GetExtendedLengthPathCore(transaction, path, pathFormat,
  75. GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.CheckInvalidPathChars |
  76. GetFullPathOptions.CheckAdditional);
  77. using (SafeFindFileHandle handle = transaction == null
  78. ? NativeMethods.FindFirstStreamW(pathLp, NativeMethods.STREAM_INFO_LEVELS.FindStreamInfoStandard, buffer, 0)
  79. : NativeMethods.FindFirstStreamTransactedW(pathLp, NativeMethods.STREAM_INFO_LEVELS.FindStreamInfoStandard, buffer, 0, transaction.SafeHandle))
  80. {
  81. int errorCode = Marshal.GetLastWin32Error();
  82. if (handle.IsInvalid)
  83. {
  84. if (errorCode == Win32Errors.ERROR_HANDLE_EOF)
  85. yield break;
  86. NativeError.ThrowException(errorCode);
  87. }
  88. while (true)
  89. {
  90. yield return new AlternateDataStreamInfo(pathLp, buffer.PtrToStructure<NativeMethods.WIN32_FIND_STREAM_DATA>(0));
  91. if (!NativeMethods.FindNextStreamW(handle, buffer))
  92. {
  93. int lastError = Marshal.GetLastWin32Error();
  94. if (lastError == Win32Errors.ERROR_HANDLE_EOF)
  95. break;
  96. NativeError.ThrowException(lastError, pathLp);
  97. }
  98. }
  99. }
  100. }
  101. }
  102. #endregion // Internal Methods
  103. }
  104. }