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.

Path.GetFinalPathNameByHandle.cs 8.3 KiB

7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  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 Alphaleonis.Win32.Security;
  22. using Microsoft.Win32.SafeHandles;
  23. using System;
  24. using System.Diagnostics;
  25. using System.Diagnostics.CodeAnalysis;
  26. using System.Linq;
  27. using System.Runtime.InteropServices;
  28. using System.Security;
  29. using System.Text;
  30. namespace Alphaleonis.Win32.Filesystem
  31. {
  32. public static partial class Path
  33. {
  34. /// <summary>[AlphaFS] Retrieves the final path for the specified file, formatted as <see cref="FinalPathFormats"/>.</summary>
  35. /// <returns>The final path as a string.</returns>
  36. /// <remarks>
  37. /// A final path is the path that is returned when a path is fully resolved. For example, for a symbolic link named "C:\tmp\mydir" that
  38. /// points to "D:\yourdir", the final path would be "D:\yourdir".
  39. /// </remarks>
  40. /// <param name="handle">Then handle to a <see cref="SafeFileHandle"/> instance.</param>
  41. [SecurityCritical]
  42. public static string GetFinalPathNameByHandle(SafeFileHandle handle)
  43. {
  44. return GetFinalPathNameByHandleCore(handle, FinalPathFormats.None);
  45. }
  46. /// <summary>[AlphaFS] Retrieves the final path for the specified file, formatted as <see cref="FinalPathFormats"/>.</summary>
  47. /// <returns>The final path as a string.</returns>
  48. /// <remarks>
  49. /// A final path is the path that is returned when a path is fully resolved. For example, for a symbolic link named "C:\tmp\mydir" that
  50. /// points to "D:\yourdir", the final path would be "D:\yourdir".
  51. /// </remarks>
  52. /// <param name="handle">Then handle to a <see cref="SafeFileHandle"/> instance.</param>
  53. /// <param name="finalPath">The final path, formatted as <see cref="FinalPathFormats"/></param>
  54. [SecurityCritical]
  55. public static string GetFinalPathNameByHandle(SafeFileHandle handle, FinalPathFormats finalPath)
  56. {
  57. return GetFinalPathNameByHandleCore(handle, finalPath);
  58. }
  59. /// <summary>Retrieves the final path for the specified file, formatted as <see cref="FinalPathFormats"/>.</summary>
  60. /// <returns>The final path as a string.</returns>
  61. /// <remarks>
  62. /// A final path is the path that is returned when a path is fully resolved. For example, for a symbolic link named "C:\tmp\mydir" that
  63. /// points to "D:\yourdir", the final path would be "D:\yourdir". The string that is returned by this function uses the
  64. /// <see cref="LongPathPrefix"/> syntax.
  65. /// </remarks>
  66. /// <param name="handle">Then handle to a <see cref="SafeFileHandle"/> instance.</param>
  67. /// <param name="finalPath">The final path, formatted as <see cref="FinalPathFormats"/></param>
  68. [SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "Alphaleonis.Win32.Filesystem.NativeMethods.GetMappedFileName(System.IntPtr,Alphaleonis.Win32.SafeGlobalMemoryBufferHandle,System.Text.StringBuilder,System.UInt32)")]
  69. [SuppressMessage("Microsoft.Usage", "CA1806:DoNotIgnoreMethodResults", MessageId = "Alphaleonis.Win32.Filesystem.NativeMethods.GetMappedFileName(System.IntPtr,Alphaleonis.Win32.Security.SafeLocalMemoryBufferHandle,System.Text.StringBuilder,System.UInt32)")]
  70. [SecurityCritical]
  71. internal static string GetFinalPathNameByHandleCore(SafeFileHandle handle, FinalPathFormats finalPath)
  72. {
  73. NativeMethods.IsValidHandle(handle);
  74. var buffer = new StringBuilder(NativeMethods.MaxPathUnicode);
  75. using (new NativeMethods.ChangeErrorMode(NativeMethods.ErrorMode.FailCriticalErrors))
  76. {
  77. if (NativeMethods.IsAtLeastWindowsVista)
  78. {
  79. if (NativeMethods.GetFinalPathNameByHandle(handle, buffer, (uint) buffer.Capacity, finalPath) == Win32Errors.ERROR_SUCCESS)
  80. NativeError.ThrowException(Marshal.GetLastWin32Error());
  81. return buffer.ToString();
  82. }
  83. }
  84. #region Older OperatingSystem
  85. // Obtaining a File Name From a File Handle
  86. // http://msdn.microsoft.com/en-us/library/aa366789%28VS.85%29.aspx
  87. // Be careful when using GetFileSizeEx to check the size of hFile handle of an unknown "File" type object.
  88. // This is more towards returning a filename from a file handle. If the handle is a named pipe handle it seems to hang the thread.
  89. // Check for: FileTypes.DiskFile
  90. // Can't map a 0 byte file.
  91. long fileSizeHi;
  92. if (!NativeMethods.GetFileSizeEx(handle, out fileSizeHi))
  93. if (fileSizeHi == 0)
  94. return string.Empty;
  95. // PAGE_READONLY
  96. // Allows views to be mapped for read-only or copy-on-write access. An attempt to write to a specific region results in an access violation.
  97. // The file handle that the hFile parameter specifies must be created with the GENERIC_READ access right.
  98. // PageReadOnly = 0x02,
  99. using (SafeFileHandle handle2 = NativeMethods.CreateFileMapping(handle, null, 2, 0, 1, null))
  100. {
  101. NativeMethods.IsValidHandle(handle, Marshal.GetLastWin32Error());
  102. // FILE_MAP_READ
  103. // Read = 4
  104. using (SafeLocalMemoryBufferHandle pMem = NativeMethods.MapViewOfFile(handle2, 4, 0, 0, (UIntPtr)1))
  105. {
  106. if (NativeMethods.IsValidHandle(pMem, Marshal.GetLastWin32Error()))
  107. if (NativeMethods.GetMappedFileName(Process.GetCurrentProcess().Handle, pMem, buffer, (uint)buffer.Capacity))
  108. NativeMethods.UnmapViewOfFile(pMem);
  109. }
  110. }
  111. // Default output from GetMappedFileName(): "\Device\HarddiskVolumeX\path\filename.ext"
  112. string dosDevice = buffer.Length > 0 ? buffer.ToString() : string.Empty;
  113. // Select output format.
  114. switch (finalPath)
  115. {
  116. // As-is: "\Device\HarddiskVolumeX\path\filename.ext"
  117. case FinalPathFormats.VolumeNameNT:
  118. return dosDevice;
  119. // To: "\path\filename.ext"
  120. case FinalPathFormats.VolumeNameNone:
  121. return DosDeviceToDosPath(dosDevice, string.Empty);
  122. // To: "\\?\Volume{GUID}\path\filename.ext"
  123. case FinalPathFormats.VolumeNameGuid:
  124. string dosPath = DosDeviceToDosPath(dosDevice, null);
  125. if (!Utils.IsNullOrWhiteSpace(dosPath))
  126. {
  127. string path = GetSuffixedDirectoryNameWithoutRootCore(null, dosPath);
  128. string driveLetter = RemoveTrailingDirectorySeparator(GetPathRoot(dosPath, false), false);
  129. string file = GetFileName(dosPath, true);
  130. if (!Utils.IsNullOrWhiteSpace(file))
  131. foreach (string drive in Directory.EnumerateLogicalDrivesCore(false, false).Select(drv => drv.Name).Where(drv => driveLetter.Equals(RemoveTrailingDirectorySeparator(drv, false), StringComparison.OrdinalIgnoreCase)))
  132. return CombineCore(false, Volume.GetUniqueVolumeNameForPath(drive), path, file);
  133. }
  134. break;
  135. }
  136. // To: "\\?\C:\path\filename.ext"
  137. return Utils.IsNullOrWhiteSpace(dosDevice)
  138. ? string.Empty
  139. : LongPathPrefix + DosDeviceToDosPath(dosDevice, null);
  140. #endregion // Older OperatingSystem
  141. }
  142. }
  143. }