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.
 
 

204 rivejä
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 Microsoft.Win32.SafeHandles;
  22. using System;
  23. using System.Diagnostics.CodeAnalysis;
  24. using System.IO;
  25. using System.Runtime.InteropServices;
  26. using System.Security;
  27. using System.Security.AccessControl;
  28. namespace Alphaleonis.Win32.Filesystem
  29. {
  30. public static partial class File
  31. {
  32. #region GetChangeTime
  33. /// <summary>[AlphaFS] Gets the change date and time of the specified file.</summary>
  34. /// <returns>A <see cref="System.DateTime"/> structure set to the change date and time for the specified file. This value is expressed in local time.</returns>
  35. /// <param name="path">The file for which to obtain creation date and time information.</param>
  36. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  37. [SecurityCritical]
  38. public static DateTime GetChangeTime(string path, PathFormat pathFormat)
  39. {
  40. return GetChangeTimeCore(false, null, null, path, false, pathFormat);
  41. }
  42. /// <summary>[AlphaFS] Gets the change date and time of the specified file.</summary>
  43. /// <returns>A <see cref="System.DateTime"/> structure set to the change date and time for the specified file. This value is expressed in local time.</returns>
  44. /// <param name="path">The file for which to obtain creation date and time information.</param>
  45. [SecurityCritical]
  46. public static DateTime GetChangeTime(string path)
  47. {
  48. return GetChangeTimeCore(false, null, null, path, false, PathFormat.RelativePath);
  49. }
  50. /// <summary>[AlphaFS] Gets the change date and time of the specified file.</summary>
  51. /// <returns>A <see cref="System.DateTime"/> structure set to the change date and time for the specified file. This value is expressed in local time.</returns>
  52. /// <param name="safeHandle">An open handle to the file or directory from which to retrieve information.</param>
  53. [SecurityCritical]
  54. public static DateTime GetChangeTime(SafeFileHandle safeHandle)
  55. {
  56. return GetChangeTimeCore(false, null, safeHandle, null, false, PathFormat.LongFullPath);
  57. }
  58. /// <summary>[AlphaFS] Gets the change date and time of the specified file.</summary>
  59. /// <returns>A <see cref="System.DateTime"/> structure set to the change date and time for the specified file. This value is expressed in local time.</returns>
  60. /// <param name="transaction">The transaction.</param>
  61. /// <param name="path">The file for which to obtain creation date and time information.</param>
  62. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  63. [SecurityCritical]
  64. public static DateTime GetChangeTimeTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  65. {
  66. return GetChangeTimeCore(false, transaction, null, path, false, pathFormat);
  67. }
  68. /// <summary>[AlphaFS] Gets the change date and time of the specified file.</summary>
  69. /// <returns>A <see cref="System.DateTime"/> structure set to the change date and time for the specified file. This value is expressed in local time.</returns>
  70. /// <param name="transaction">The transaction.</param>
  71. /// <param name="path">The file for which to obtain creation date and time information.</param>
  72. [SecurityCritical]
  73. public static DateTime GetChangeTimeTransacted(KernelTransaction transaction, string path)
  74. {
  75. return GetChangeTimeCore(false, transaction, null, path, false, PathFormat.RelativePath);
  76. }
  77. #endregion // GetChangeTime
  78. #region GetChangeTimeUtc
  79. /// <summary>[AlphaFS] Gets the change date and time, in Coordinated Universal Time (UTC) format, of the specified file.</summary>
  80. /// <returns>A <see cref="System.DateTime"/> structure set to the change date and time for the specified file. This value is expressed in UTC time.</returns>
  81. /// <param name="path">The file for which to obtain change date and time information, in Coordinated Universal Time (UTC) format.</param>
  82. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  83. [SecurityCritical]
  84. public static DateTime GetChangeTimeUtc(string path, PathFormat pathFormat)
  85. {
  86. return GetChangeTimeCore(false, null, null, path, true, pathFormat);
  87. }
  88. /// <summary>[AlphaFS] Gets the change date and time, in Coordinated Universal Time (UTC) format, of the specified file.</summary>
  89. /// <returns>A <see cref="System.DateTime"/> structure set to the change date and time for the specified file. This value is expressed in UTC time.</returns>
  90. /// <param name="path">The file for which to obtain change date and time information, in Coordinated Universal Time (UTC) format.</param>
  91. [SecurityCritical]
  92. public static DateTime GetChangeTimeUtc(string path)
  93. {
  94. return GetChangeTimeCore(false, null, null, path, true, PathFormat.RelativePath);
  95. }
  96. /// <summary>[AlphaFS] Gets the change date and time, in Coordinated Universal Time (UTC) format, of the specified file.</summary>
  97. /// <returns>A <see cref="System.DateTime"/> structure set to the change date and time for the specified file. This value is expressed in UTC time.</returns>
  98. /// <param name="safeHandle">An open handle to the file or directory from which to retrieve information.</param>
  99. [SecurityCritical]
  100. public static DateTime GetChangeTimeUtc(SafeFileHandle safeHandle)
  101. {
  102. return GetChangeTimeCore(false, null, safeHandle, null, true, PathFormat.LongFullPath);
  103. }
  104. /// <summary>[AlphaFS] Gets the change date and time, in Coordinated Universal Time (UTC) format, of the specified file.</summary>
  105. /// <returns>A <see cref="System.DateTime"/> structure set to the change date and time for the specified file. This value is expressed in UTC time.</returns>
  106. /// <param name="transaction">The transaction.</param>
  107. /// <param name="path">The file for which to obtain change date and time information, in Coordinated Universal Time (UTC) format.</param>
  108. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  109. [SecurityCritical]
  110. public static DateTime GetChangeTimeUtcTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  111. {
  112. return GetChangeTimeCore(false, transaction, null, path, true, pathFormat);
  113. }
  114. /// <summary>[AlphaFS] Gets the change date and time, in Coordinated Universal Time (UTC) format, of the specified file.</summary>
  115. /// <returns>A <see cref="System.DateTime"/> structure set to the change date and time for the specified file. This value is expressed in UTC time.</returns>
  116. /// <param name="transaction">The transaction.</param>
  117. /// <param name="path">The file for which to obtain change date and time information, in Coordinated Universal Time (UTC) format.</param>
  118. [SecurityCritical]
  119. public static DateTime GetChangeTimeUtcTransacted(KernelTransaction transaction, string path)
  120. {
  121. return GetChangeTimeCore(false, transaction, null, path, true, PathFormat.RelativePath);
  122. }
  123. #endregion // GetChangeTimeUtc
  124. #region Internal Methods
  125. /// <summary>Gets the change date and time of the specified file.</summary>
  126. /// <returns>A <see cref="System.DateTime"/> structure set to the change date and time for the specified file. This value is expressed in local time.</returns>
  127. /// <remarks><para>Use either <paramref name="path"/> or <paramref name="safeHandle"/>, not both.</para></remarks>
  128. /// <exception cref="ArgumentException"/>
  129. /// <exception cref="ArgumentNullException"/>
  130. /// <exception cref="PlatformNotSupportedException"/>
  131. /// <param name="isFolder">Specifies that <paramref name="path"/> is a file or directory.</param>
  132. /// <param name="transaction">The transaction.</param>
  133. /// <param name="safeHandle">An open handle to the file or directory from which to retrieve information.</param>
  134. /// <param name="path">The file or directory for which to obtain creation date and time information.</param>
  135. /// <param name="getUtc"><see langword="true"/> gets the Coordinated Universal Time (UTC), <see langword="false"/> gets the local time.</param>
  136. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  137. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope", Justification = "Disposing is under control.")]
  138. [SecurityCritical]
  139. internal static DateTime GetChangeTimeCore(bool isFolder, KernelTransaction transaction, SafeFileHandle safeHandle, string path, bool getUtc, PathFormat pathFormat)
  140. {
  141. if (!NativeMethods.IsAtLeastWindowsVista)
  142. throw new PlatformNotSupportedException(Resources.Requires_Windows_Vista_Or_Higher);
  143. bool callerHandle = safeHandle != null;
  144. if (!callerHandle)
  145. {
  146. if (pathFormat != PathFormat.LongFullPath && Utils.IsNullOrWhiteSpace(path))
  147. throw new ArgumentNullException("path");
  148. string pathLp = Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.CheckInvalidPathChars);
  149. safeHandle = CreateFileCore(transaction, pathLp, isFolder ? ExtendedFileAttributes.BackupSemantics : ExtendedFileAttributes.Normal, null, FileMode.Open, FileSystemRights.ReadData, FileShare.ReadWrite, true, PathFormat.LongFullPath);
  150. }
  151. try
  152. {
  153. NativeMethods.IsValidHandle(safeHandle);
  154. using (var safeBuffer = new SafeGlobalMemoryBufferHandle(IntPtr.Size + Marshal.SizeOf(typeof(NativeMethods.FILE_BASIC_INFO))))
  155. {
  156. NativeMethods.FILE_BASIC_INFO fbi;
  157. if (!NativeMethods.GetFileInformationByHandleEx_FileBasicInfo(safeHandle, NativeMethods.FileInfoByHandleClass.FileBasicInfo, out fbi, (uint)safeBuffer.Capacity))
  158. NativeError.ThrowException(Marshal.GetLastWin32Error());
  159. safeBuffer.StructureToPtr(fbi, true);
  160. NativeMethods.FILETIME changeTime = safeBuffer.PtrToStructure<NativeMethods.FILE_BASIC_INFO>(0).ChangeTime;
  161. return getUtc
  162. ? DateTime.FromFileTimeUtc(changeTime)
  163. : DateTime.FromFileTime(changeTime);
  164. }
  165. }
  166. finally
  167. {
  168. // Handle is ours, dispose.
  169. if (!callerHandle && safeHandle != null)
  170. safeHandle.Close();
  171. }
  172. }
  173. #endregion // Internal Methods
  174. }
  175. }