Nelze vybrat více než 25 témat Téma musí začínat písmenem nebo číslem, může obsahovat pomlčky („-“) a může být dlouhé až 35 znaků.
 
 

135 řádky
7.6 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;
  22. using System.Runtime.InteropServices;
  23. using System.Security;
  24. namespace Alphaleonis.Win32.Filesystem
  25. {
  26. public static partial class File
  27. {
  28. #region GetCompressedSize
  29. /// <summary>[AlphaFS] Retrieves the actual number of bytes of disk storage used to store a specified file.</summary>
  30. /// <remarks>
  31. /// If the file is located on a volume that supports compression and the file is compressed, the value obtained is the compressed size
  32. /// of the specified file. If the file is located on a volume that supports sparse files and the file is a sparse file, the value
  33. /// obtained is the sparse size of the specified file.
  34. /// </remarks>
  35. /// <param name="path"><para>The name of the file.</para></param>
  36. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  37. /// <returns>The actual number of bytes of disk storage used to store the specified file.</returns>
  38. [SecurityCritical]
  39. public static long GetCompressedSize(string path, PathFormat pathFormat)
  40. {
  41. return GetCompressedSizeCore(null, path, pathFormat);
  42. }
  43. /// <summary>[AlphaFS] Retrieves the actual number of bytes of disk storage used to store a specified file.</summary>
  44. /// <remarks>
  45. /// If the file is located on a volume that supports compression and the file is compressed, the value obtained is the compressed size
  46. /// of the specified file. If the file is located on a volume that supports sparse files and the file is a sparse file, the value
  47. /// obtained is the sparse size of the specified file.
  48. /// </remarks>
  49. /// <param name="path"><para>The name of the file.</para></param>
  50. /// <returns>The actual number of bytes of disk storage used to store the specified file.</returns>
  51. [SecurityCritical]
  52. public static long GetCompressedSize(string path)
  53. {
  54. return GetCompressedSizeCore(null, path, PathFormat.RelativePath);
  55. }
  56. /// <summary>
  57. /// [AlphaFS] Retrieves the actual number of bytes of disk storage used to store a specified file as part of a transaction. If the file
  58. /// is located on a volume that supports compression and the file is compressed, the value obtained is the compressed size of the
  59. /// specified file. If the file is located on a volume that supports sparse files and the file is a sparse file, the value obtained is
  60. /// the sparse size of the specified file.
  61. /// </summary>
  62. /// <param name="transaction">The transaction.</param>
  63. /// <param name="path"><para>The name of the file.</para></param>
  64. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  65. /// <returns>The actual number of bytes of disk storage used to store the specified file.</returns>
  66. [SecurityCritical]
  67. public static long GetCompressedSizeTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
  68. {
  69. return GetCompressedSizeCore(transaction, path, pathFormat);
  70. }
  71. /// <summary>
  72. /// [AlphaFS] Retrieves the actual number of bytes of disk storage used to store a specified file as part of a transaction. If the file
  73. /// is located on a volume that supports compression and the file is compressed, the value obtained is the compressed size of the
  74. /// specified file. If the file is located on a volume that supports sparse files and the file is a sparse file, the value obtained is
  75. /// the sparse size of the specified file.
  76. /// </summary>
  77. /// <param name="transaction">The transaction.</param>
  78. /// <param name="path"><para>The name of the file.</para></param>
  79. /// <returns>The actual number of bytes of disk storage used to store the specified file.</returns>
  80. [SecurityCritical]
  81. public static long GetCompressedSizeTransacted(KernelTransaction transaction, string path)
  82. {
  83. return GetCompressedSizeCore(transaction, path, PathFormat.RelativePath);
  84. }
  85. #endregion // GetCompressedSize
  86. #region Internal Methods
  87. /// <summary>Retrieves the actual number of bytes of disk storage used to store a
  88. /// specified file as part of a transaction. If the file is located on a volume that supports compression and the file is compressed,
  89. /// the value obtained is the compressed size of the specified file. If the file is located on a volume that supports sparse files and
  90. /// the file is a sparse file, the value obtained is the sparse size of the specified file.
  91. /// </summary>
  92. /// <exception cref="ArgumentNullException"/>
  93. /// <param name="transaction">The transaction.</param>
  94. /// <param name="path"><para>The name of the file.</para></param>
  95. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  96. /// <returns>The actual number of bytes of disk storage used to store the specified file.</returns>
  97. [SecurityCritical]
  98. internal static long GetCompressedSizeCore(KernelTransaction transaction, string path, PathFormat pathFormat)
  99. {
  100. if (pathFormat != PathFormat.LongFullPath && Utils.IsNullOrWhiteSpace(path))
  101. throw new ArgumentNullException("path");
  102. string pathLp = Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck);
  103. uint fileSizeHigh;
  104. uint fileSizeLow = transaction == null || !NativeMethods.IsAtLeastWindowsVista
  105. // GetCompressedFileSize() / GetCompressedFileSizeTransacted()
  106. // In the ANSI version of this function, the name is limited to 248 characters.
  107. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
  108. // 2013-01-13: MSDN does not confirm LongPath usage but a Unicode version of this function exists.
  109. ? NativeMethods.GetCompressedFileSize(pathLp, out fileSizeHigh)
  110. : NativeMethods.GetCompressedFileSizeTransacted(pathLp, out fileSizeHigh, transaction.SafeHandle);
  111. // If the function fails, and lpFileSizeHigh is NULL, the return value is INVALID_FILE_SIZE.
  112. if (fileSizeLow == Win32Errors.ERROR_INVALID_FILE_SIZE && fileSizeHigh == 0)
  113. NativeError.ThrowException(Marshal.GetLastWin32Error(), pathLp);
  114. return NativeMethods.ToLong(fileSizeHigh, fileSizeLow);
  115. }
  116. #endregion // Internal Methods
  117. }
  118. }