Nie możesz wybrać więcej, niż 25 tematów Tematy muszą się zaczynać od litery lub cyfry, mogą zawierać myślniki ('-') i mogą mieć do 35 znaków.
 
 

130 wiersze
7.4 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.Diagnostics.CodeAnalysis;
  23. using System.Runtime.InteropServices;
  24. using System.Security;
  25. namespace Alphaleonis.Win32.Filesystem
  26. {
  27. public static partial class File
  28. {
  29. #region CreateHardlink
  30. /// <summary>[AlphaFS] Establishes a hard link between an existing file and a new file. This function is only supported on the NTFS file system, and only for files, not directories.</summary>
  31. /// <param name="fileName">The name of the new file. This parameter cannot specify the name of a directory.</param>
  32. /// <param name="existingFileName">The name of the existing file. This parameter cannot specify the name of a directory.</param>
  33. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  34. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Hardlink")]
  35. [SecurityCritical]
  36. public static void CreateHardlink(string fileName, string existingFileName, PathFormat pathFormat)
  37. {
  38. CreateHardlinkCore(null, fileName, existingFileName, pathFormat);
  39. }
  40. /// <summary>[AlphaFS] Establishes a hard link between an existing file and a new file. This function is only supported on the NTFS file system, and only for files, not directories.</summary>
  41. /// <param name="fileName">The name of the new file. This parameter cannot specify the name of a directory.</param>
  42. /// <param name="existingFileName">The name of the existing file. This parameter cannot specify the name of a directory.</param>
  43. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Hardlink")]
  44. [SecurityCritical]
  45. public static void CreateHardlink(string fileName, string existingFileName)
  46. {
  47. CreateHardlinkCore(null, fileName, existingFileName, PathFormat.RelativePath);
  48. }
  49. /// <summary>
  50. /// [AlphaFS] Establishes a hard link between an existing file and a new file. This function is only supported on the NTFS file system,
  51. /// and only for files, not directories.
  52. /// </summary>
  53. /// <param name="transaction">The transaction.</param>
  54. /// <param name="fileName">The name of the new file. This parameter cannot specify the name of a directory.</param>
  55. /// <param name="existingFileName">The name of the existing file. This parameter cannot specify the name of a directory.</param>
  56. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  57. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Hardlink")]
  58. [SecurityCritical]
  59. public static void CreateHardlinkTransacted(KernelTransaction transaction, string fileName, string existingFileName, PathFormat pathFormat)
  60. {
  61. CreateHardlinkCore(transaction, fileName, existingFileName, pathFormat);
  62. }
  63. /// <summary>
  64. /// [AlphaFS] Establishes a hard link between an existing file and a new file. This function is only supported on the NTFS file system,
  65. /// and only for files, not directories.
  66. /// </summary>
  67. /// <param name="transaction">The transaction.</param>
  68. /// <param name="fileName">The name of the new file. This parameter cannot specify the name of a directory.</param>
  69. /// <param name="existingFileName">The name of the existing file. This parameter cannot specify the name of a directory.</param>
  70. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Hardlink")]
  71. [SecurityCritical]
  72. public static void CreateHardlinkTransacted(KernelTransaction transaction, string fileName, string existingFileName)
  73. {
  74. CreateHardlinkCore(transaction, fileName, existingFileName, PathFormat.RelativePath);
  75. }
  76. #endregion // CreateHardlink
  77. #region Internal Methods
  78. /// <summary>Establish a hard link between an existing file and a new file. This function
  79. /// is only supported on the NTFS file system, and only for files, not directories.
  80. /// </summary>
  81. /// <exception cref="NotSupportedException"/>
  82. /// <param name="transaction">The transaction.</param>
  83. /// <param name="fileName">The name of the new file. This parameter cannot specify the name of a directory.</param>
  84. /// <param name="existingFileName">The name of the existing file. This parameter cannot specify the name of a directory.</param>
  85. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  86. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "Hardlink")]
  87. [SecurityCritical]
  88. internal static void CreateHardlinkCore(KernelTransaction transaction, string fileName, string existingFileName, PathFormat pathFormat)
  89. {
  90. const GetFullPathOptions options = GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck;
  91. string fileNameLp = Path.GetExtendedLengthPathCore(transaction, fileName, pathFormat, options);
  92. string existingFileNameLp = Path.GetExtendedLengthPathCore(transaction, existingFileName, pathFormat, options);
  93. if (!(transaction == null || !NativeMethods.IsAtLeastWindowsVista
  94. // CreateHardLink() / CreateHardLinkTransacted()
  95. // In the ANSI version of this function, the name is limited to MAX_PATH characters.
  96. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
  97. // 2013-01-13: MSDN does not confirm LongPath usage but a Unicode version of this function exists.
  98. ? NativeMethods.CreateHardLink(fileNameLp, existingFileNameLp, IntPtr.Zero)
  99. : NativeMethods.CreateHardLinkTransacted(fileNameLp, existingFileNameLp, IntPtr.Zero, transaction.SafeHandle)))
  100. {
  101. int lastError = Marshal.GetLastWin32Error();
  102. switch ((uint)lastError)
  103. {
  104. case Win32Errors.ERROR_INVALID_FUNCTION:
  105. throw new NotSupportedException(Resources.HardLinks_Not_Supported);
  106. default:
  107. NativeError.ThrowException(lastError, fileNameLp, existingFileName);
  108. break;
  109. }
  110. }
  111. }
  112. #endregion // Internal Methods
  113. }
  114. }