Nevar pievienot vairāk kā 25 tēmas Tēmai ir jāsākas ar burtu vai ciparu, tā var saturēt domu zīmes ('-') un var būt līdz 35 simboliem gara.
 
 

142 rindas
8.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.Diagnostics.CodeAnalysis;
  23. using System.Runtime.InteropServices;
  24. using System.Security;
  25. namespace Alphaleonis.Win32.Filesystem
  26. {
  27. partial class File
  28. {
  29. #region CreateSymbolicLink
  30. /// <summary>[AlphaFS] Creates a symbolic link.</summary>
  31. /// <remarks>See <see cref="Alphaleonis.Win32.Security.Privilege.CreateSymbolicLink"/> to run this method in an elevated state.</remarks>
  32. /// <param name="symlinkFileName">The name of the target for the symbolic link to be created.</param>
  33. /// <param name="targetFileName">The symbolic link to be created.</param>
  34. /// <param name="targetType">Indicates whether the link target, <paramref name="targetFileName"/>, is a file or directory.</param>
  35. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  36. /// <exception cref="PlatformNotSupportedException"/>
  37. /// <exception>Several Exceptions possible.</exception>
  38. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "symlink")]
  39. [SecurityCritical]
  40. public static void CreateSymbolicLink(string symlinkFileName, string targetFileName, SymbolicLinkTarget targetType, PathFormat pathFormat)
  41. {
  42. CreateSymbolicLinkCore(null, symlinkFileName, targetFileName, targetType, pathFormat);
  43. }
  44. /// <summary>[AlphaFS] Creates a symbolic link.</summary>
  45. /// <remarks>See <see cref="Alphaleonis.Win32.Security.Privilege.CreateSymbolicLink"/> to run this method in an elevated state.</remarks>
  46. /// <param name="symlinkFileName">The name of the target for the symbolic link to be created.</param>
  47. /// <param name="targetFileName">The symbolic link to be created.</param>
  48. /// <param name="targetType">Indicates whether the link target, <paramref name="targetFileName"/>, is a file or directory.</param>
  49. /// <exception cref="PlatformNotSupportedException"/>
  50. /// <exception>Several Exceptions possible.</exception>
  51. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "symlink")]
  52. [SecurityCritical]
  53. public static void CreateSymbolicLink(string symlinkFileName, string targetFileName, SymbolicLinkTarget targetType)
  54. {
  55. CreateSymbolicLinkCore(null, symlinkFileName, targetFileName, targetType, PathFormat.RelativePath);
  56. }
  57. /// <summary>[AlphaFS] Creates a symbolic link.</summary>
  58. /// <remarks>See <see cref="Alphaleonis.Win32.Security.Privilege.CreateSymbolicLink"/> to run this method in an elevated state.</remarks>
  59. /// <param name="transaction">The transaction.</param>
  60. /// <param name="symlinkFileName">The name of the target for the symbolic link to be created.</param>
  61. /// <param name="targetFileName">The symbolic link to be created.</param>
  62. /// <param name="targetType">Indicates whether the link target, <paramref name="targetFileName"/>, is a file or directory.</param>
  63. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  64. /// <exception cref="PlatformNotSupportedException"/>
  65. /// <exception>Several Exceptions possible.</exception>
  66. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "symlink")]
  67. [SecurityCritical]
  68. public static void CreateSymbolicLinkTransacted(KernelTransaction transaction, string symlinkFileName, string targetFileName, SymbolicLinkTarget targetType, PathFormat pathFormat)
  69. {
  70. CreateSymbolicLinkCore(transaction, symlinkFileName, targetFileName, targetType, pathFormat);
  71. }
  72. /// <summary>[AlphaFS] Creates a symbolic link.</summary>
  73. /// <remarks>See <see cref="Alphaleonis.Win32.Security.Privilege.CreateSymbolicLink"/> to run this method in an elevated state.</remarks>
  74. /// <param name="transaction">The transaction.</param>
  75. /// <param name="symlinkFileName">The name of the target for the symbolic link to be created.</param>
  76. /// <param name="targetFileName">The symbolic link to be created.</param>
  77. /// <param name="targetType">Indicates whether the link target, <paramref name="targetFileName"/>, is a file or directory.</param>
  78. /// <exception cref="PlatformNotSupportedException"/>
  79. /// <exception>Several Exceptions possible.</exception>
  80. [SuppressMessage("Microsoft.Naming", "CA1704:IdentifiersShouldBeSpelledCorrectly", MessageId = "symlink")]
  81. [SecurityCritical]
  82. public static void CreateSymbolicLinkTransacted(KernelTransaction transaction, string symlinkFileName, string targetFileName, SymbolicLinkTarget targetType)
  83. {
  84. CreateSymbolicLinkCore(transaction, symlinkFileName, targetFileName, targetType, PathFormat.RelativePath);
  85. }
  86. #endregion // CreateSymbolicLink
  87. #region Internal Methods
  88. /// <summary>Creates a symbolic link.</summary>
  89. /// <remarks>See <see cref="Alphaleonis.Win32.Security.Privilege.CreateSymbolicLink"/> to run this method in an elevated state.</remarks>
  90. /// <param name="transaction">The transaction.</param>
  91. /// <param name="symlinkFileName">The name of the target for the symbolic link to be created.</param>
  92. /// <param name="targetFileName">The symbolic link to be created.</param>
  93. /// <param name="targetType">Indicates whether the link target, <paramref name="targetFileName"/>, is a file or directory.</param>
  94. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  95. /// <exception cref="PlatformNotSupportedException"/>
  96. /// <exception>Several Exceptions possible.</exception>
  97. [SecurityCritical]
  98. internal static void CreateSymbolicLinkCore(KernelTransaction transaction, string symlinkFileName, string targetFileName, SymbolicLinkTarget targetType, PathFormat pathFormat)
  99. {
  100. if (!NativeMethods.IsAtLeastWindowsVista)
  101. throw new PlatformNotSupportedException(Resources.Requires_Windows_Vista_Or_Higher);
  102. const GetFullPathOptions options = GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck;
  103. string symlinkFileNameLp = Path.GetExtendedLengthPathCore(transaction, symlinkFileName, pathFormat, options);
  104. string targetFileNameRp = Path.GetExtendedLengthPathCore(transaction, targetFileName, pathFormat, options);
  105. // Don't use long path notation, as it will be empty upon creation.
  106. targetFileNameRp = Path.GetRegularPathCore(targetFileNameRp, GetFullPathOptions.None, false);
  107. if (!(transaction == null
  108. // CreateSymbolicLink() / CreateSymbolicLinkTransacted()
  109. // In the ANSI version of this function, the name is limited to MAX_PATH characters.
  110. // To extend this limit to 32,767 wide characters, call the Unicode version of the function and prepend "\\?\" to the path.
  111. // 2014-02-14: MSDN does not confirm LongPath usage but a Unicode version of this function exists.
  112. // 2015-07-17: This function does not support long paths.
  113. ? NativeMethods.CreateSymbolicLink(symlinkFileNameLp, targetFileNameRp, targetType)
  114. : NativeMethods.CreateSymbolicLinkTransacted(symlinkFileNameLp, targetFileNameRp, targetType, transaction.SafeHandle)))
  115. {
  116. var lastError = Marshal.GetLastWin32Error();
  117. if (lastError != 0)
  118. NativeError.ThrowException(lastError, symlinkFileNameLp, targetFileNameRp);
  119. }
  120. }
  121. #endregion // Internal Methods
  122. }
  123. }