25개 이상의 토픽을 선택하실 수 없습니다. Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.
 
 

126 lines
5.5 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.Diagnostics.CodeAnalysis;
  23. using System.IO;
  24. using System.Security;
  25. using System.Security.AccessControl;
  26. namespace Alphaleonis.Win32.Filesystem
  27. {
  28. public static partial class File
  29. {
  30. #region GetSize
  31. /// <summary>[AlphaFS] Retrieves the file size, in bytes to store a specified file.</summary>
  32. /// <param name="path">The path to the file.</param>
  33. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  34. /// <returns>The file size, in bytes.</returns>
  35. [SecurityCritical]
  36. public static long GetSize(string path, PathFormat pathFormat)
  37. {
  38. return GetSizeCore(null, null, path, pathFormat);
  39. }
  40. /// <summary>[AlphaFS] Retrieves the file size, in bytes to store a specified file.</summary>
  41. /// <param name="path">The path to the file.</param>
  42. /// <returns>The file size, in bytes.</returns>
  43. [SecurityCritical]
  44. public static long GetSize(string path)
  45. {
  46. return GetSizeCore(null, null, path, PathFormat.RelativePath);
  47. }
  48. /// <summary>[AlphaFS] Retrieves the file size, in bytes to store a specified file.</summary>
  49. /// <param name="handle">The <see cref="SafeFileHandle"/> to the file.</param>
  50. /// <returns>The file size, in bytes.</returns>
  51. [SecurityCritical]
  52. public static long GetSize(SafeFileHandle handle)
  53. {
  54. return GetSizeCore(null, handle, null, PathFormat.LongFullPath);
  55. }
  56. /// <summary>[AlphaFS] Retrieves the file size, in bytes to store a specified file.</summary>
  57. /// <param name="transaction">The transaction.</param>
  58. /// <param name="path">The path to the file.</param>
  59. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  60. /// <returns>The number of bytes of disk storage used to store the specified file.</returns>
  61. [SecurityCritical]
  62. public static long GetSizeTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  63. {
  64. return GetSizeCore(transaction, null, path, pathFormat);
  65. }
  66. /// <summary>[AlphaFS] Retrieves the file size, in bytes to store a specified file.</summary>
  67. /// <param name="transaction">The transaction.</param>
  68. /// <param name="path">The path to the file.</param>
  69. /// <returns>The number of bytes of disk storage used to store the specified file.</returns>
  70. [SecurityCritical]
  71. public static long GetSizeTransacted(KernelTransaction transaction, string path)
  72. {
  73. return GetSizeCore(transaction, null, path, PathFormat.RelativePath);
  74. }
  75. #endregion // GetSize
  76. #region Internal Methods
  77. /// <summary>Retrieves the file size, in bytes to store a specified file.</summary>
  78. /// <remarks>Use either <paramref name="path"/> or <paramref name="safeHandle"/>, not both.</remarks>
  79. /// <param name="transaction">The transaction.</param>
  80. /// <param name="safeHandle">The <see cref="SafeFileHandle"/> to the file.</param>
  81. /// <param name="path">The path to the file.</param>
  82. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  83. /// <returns>The number of bytes of disk storage used to store the specified file.</returns>
  84. [SuppressMessage("Microsoft.Reliability", "CA2000:Dispose objects before losing scope")]
  85. [SecurityCritical]
  86. internal static long GetSizeCore(KernelTransaction transaction, SafeFileHandle safeHandle, string path, PathFormat pathFormat)
  87. {
  88. bool callerHandle = safeHandle != null;
  89. if (!callerHandle)
  90. {
  91. string pathLp = Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck);
  92. safeHandle = CreateFileCore(transaction, pathLp, ExtendedFileAttributes.Normal, null, FileMode.Open, FileSystemRights.ReadData, FileShare.Read, true, PathFormat.LongFullPath);
  93. }
  94. long fileSize;
  95. try
  96. {
  97. NativeMethods.GetFileSizeEx(safeHandle, out fileSize);
  98. }
  99. finally
  100. {
  101. // Handle is ours, dispose.
  102. if (!callerHandle && safeHandle != null)
  103. safeHandle.Close();
  104. }
  105. return fileSize;
  106. }
  107. #endregion // Internal Methods
  108. }
  109. }