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.
 
 

203 lines
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 System;
  22. using System.Globalization;
  23. using System.Security;
  24. namespace Alphaleonis.Win32.Filesystem
  25. {
  26. public static partial class Path
  27. {
  28. #region AddTrailingDirectorySeparator
  29. /// <summary>[AlphaFS] Adds a trailing <see cref="DirectorySeparatorChar"/> character to the string, when absent.</summary>
  30. /// <returns>A text string with a trailing <see cref="DirectorySeparatorChar"/> character. The function returns <see langword="null"/> when <paramref name="path"/> is <see langword="null"/>.</returns>
  31. /// <param name="path">A text string to which the trailing <see cref="DirectorySeparatorChar"/> is to be added, when absent.</param>
  32. [SecurityCritical]
  33. public static string AddTrailingDirectorySeparator(string path)
  34. {
  35. return AddTrailingDirectorySeparator(path, false);
  36. }
  37. /// <summary>[AlphaFS] Adds a trailing <see cref="DirectorySeparatorChar"/> or <see cref="AltDirectorySeparatorChar"/> character to the string, when absent.</summary>
  38. /// <returns>A text string with a trailing <see cref="DirectorySeparatorChar"/> or <see cref="AltDirectorySeparatorChar"/> character. The function returns <see langword="null"/> when <paramref name="path"/> is <see langword="null"/>.</returns>
  39. /// <param name="path">A text string to which the trailing <see cref="DirectorySeparatorChar"/> or <see cref="AltDirectorySeparatorChar"/> is to be added, when absent.</param>
  40. /// <param name="addAlternateSeparator">If <see langword="true"/> the <see cref="AltDirectorySeparatorChar"/> character will be added instead.</param>
  41. [SecurityCritical]
  42. public static string AddTrailingDirectorySeparator(string path, bool addAlternateSeparator)
  43. {
  44. return path == null
  45. ? null
  46. : (addAlternateSeparator
  47. ? ((!path.EndsWith(AltDirectorySeparatorChar.ToString(CultureInfo.CurrentCulture), StringComparison.OrdinalIgnoreCase))
  48. ? path + AltDirectorySeparatorChar
  49. : path)
  50. : ((!path.EndsWith(DirectorySeparatorChar.ToString(CultureInfo.CurrentCulture), StringComparison.OrdinalIgnoreCase))
  51. ? path + DirectorySeparatorChar
  52. : path));
  53. }
  54. #endregion // AddTrailingDirectorySeparator
  55. #region RemoveTrailingDirectorySeparator
  56. /// <summary>[AlphaFS] Removes the trailing <see cref="DirectorySeparatorChar"/> character from the string, when present.</summary>
  57. /// <returns>A text string where the trailing <see cref="DirectorySeparatorChar"/> character has been removed. The function returns <see langword="null"/> when <paramref name="path"/> is <see langword="null"/>.</returns>
  58. /// <param name="path">A text string from which the trailing <see cref="DirectorySeparatorChar"/> is to be removed, when present.</param>
  59. [SecurityCritical]
  60. public static string RemoveTrailingDirectorySeparator(string path)
  61. {
  62. return path == null ? null : path.TrimEnd(DirectorySeparatorChar, AltDirectorySeparatorChar);
  63. }
  64. /// <summary>[AlphaFS] Removes the trailing <see cref="DirectorySeparatorChar"/> or <see cref="AltDirectorySeparatorChar"/> character from the string, when present.</summary>
  65. /// <returns>A text string where the trailing <see cref="DirectorySeparatorChar"/> or <see cref="AltDirectorySeparatorChar"/> character has been removed. The function returns <see langword="null"/> when <paramref name="path"/> is <see langword="null"/>.</returns>
  66. /// <param name="path">A text string from which the trailing <see cref="DirectorySeparatorChar"/> or <see cref="AltDirectorySeparatorChar"/> is to be removed, when present.</param>
  67. /// <param name="removeAlternateSeparator">If <see langword="true"/> the trailing <see cref="AltDirectorySeparatorChar"/> character will be removed instead.</param>
  68. [SecurityCritical]
  69. public static string RemoveTrailingDirectorySeparator(string path, bool removeAlternateSeparator)
  70. {
  71. return path == null
  72. ? null
  73. : path.TrimEnd(removeAlternateSeparator ? AltDirectorySeparatorChar : DirectorySeparatorChar);
  74. }
  75. #endregion // RemoveTrailingDirectorySeparator
  76. #region GetSuffixedDirectoryName
  77. /// <summary>[AlphaFS] Returns the directory information for the specified <paramref name="path"/> with a trailing <see cref="DirectorySeparatorChar"/> character.</summary>
  78. /// <returns>
  79. /// <para>The suffixed directory information for the specified <paramref name="path"/> with a trailing <see cref="DirectorySeparatorChar"/> character,</para>
  80. /// <para>or <see langword="null"/> if <paramref name="path"/> is <see langword="null"/> or if <paramref name="path"/> denotes a root (such as "\", "C:", or * "\\server\share").</para>
  81. /// </returns>
  82. /// <remarks>This method is similar to calling Path.GetDirectoryName() + Path.AddTrailingDirectorySeparator()</remarks>
  83. /// <param name="path">The path.</param>
  84. [SecurityCritical]
  85. public static string GetSuffixedDirectoryName(string path)
  86. {
  87. return GetSuffixedDirectoryNameCore(null, path);
  88. }
  89. /// <summary>[AlphaFS] Returns the directory information for the specified <paramref name="path"/> with a trailing <see cref="DirectorySeparatorChar"/> character.</summary>
  90. /// <returns>
  91. /// <para>The suffixed directory information for the specified <paramref name="path"/> with a trailing <see cref="DirectorySeparatorChar"/> character,</para>
  92. /// <para>or <see langword="null"/> if <paramref name="path"/> is <see langword="null"/> or if <paramref name="path"/> denotes a root (such as "\", "C:", or * "\\server\share").</para>
  93. /// </returns>
  94. /// <remarks>This method is similar to calling Path.GetDirectoryName() + Path.AddTrailingDirectorySeparator()</remarks>
  95. /// <param name="transaction">The transaction.</param>
  96. /// <param name="path">The path.</param>
  97. [SecurityCritical]
  98. public static string GetSuffixedDirectoryNameTransacted(KernelTransaction transaction, string path)
  99. {
  100. return GetSuffixedDirectoryNameCore(transaction, path);
  101. }
  102. #endregion // GetSuffixedDirectoryName
  103. #region GetSuffixedDirectoryNameWithoutRoot
  104. /// <summary>[AlphaFS] Returns the directory information for the specified <paramref name="path"/> without the root and with a trailing <see cref="DirectorySeparatorChar"/> character.</summary>
  105. /// <returns>
  106. /// <para>The directory information for the specified <paramref name="path"/> without the root and with a trailing <see cref="DirectorySeparatorChar"/> character,</para>
  107. /// <para>or <see langword="null"/> if <paramref name="path"/> is <see langword="null"/> or if <paramref name="path"/> is <see langword="null"/>.</para>
  108. /// </returns>
  109. /// <param name="path">The path.</param>
  110. [SecurityCritical]
  111. public static string GetSuffixedDirectoryNameWithoutRoot(string path)
  112. {
  113. return GetSuffixedDirectoryNameWithoutRootCore(null, path);
  114. }
  115. /// <summary>[AlphaFS] Returns the directory information for the specified <paramref name="path"/> without the root and with a trailing <see cref="DirectorySeparatorChar"/> character.</summary>
  116. /// <returns>
  117. /// <para>The directory information for the specified <paramref name="path"/> without the root and with a trailing <see cref="DirectorySeparatorChar"/> character,</para>
  118. /// <para>or <see langword="null"/> if <paramref name="path"/> is <see langword="null"/> or if <paramref name="path"/> is <see langword="null"/>.</para>
  119. /// </returns>
  120. /// <param name="transaction">The transaction.</param>
  121. /// <param name="path">The path.</param>
  122. [SecurityCritical]
  123. public static string GetSuffixedDirectoryNameWithoutRootTransacted(KernelTransaction transaction, string path)
  124. {
  125. return GetSuffixedDirectoryNameWithoutRootCore(transaction, path);
  126. }
  127. #endregion // GetSuffixedDirectoryNameWithoutRoot
  128. #region Internal Methods
  129. /// <summary>Returns the directory information for the specified <paramref name="path"/> with a trailing <see cref="DirectorySeparatorChar"/> character.</summary>
  130. /// <returns>
  131. /// <para>The suffixed directory information for the specified <paramref name="path"/> with a trailing <see cref="DirectorySeparatorChar"/> character,</para>
  132. /// <para>or <see langword="null"/> if <paramref name="path"/> is <see langword="null"/> or if <paramref name="path"/> denotes a root (such as "\", "C:", or * "\\server\share").</para>
  133. /// </returns>
  134. /// <remarks>This method is similar to calling Path.GetDirectoryName() + Path.AddTrailingDirectorySeparator()</remarks>
  135. /// <param name="transaction">The transaction.</param>
  136. /// <param name="path">The path.</param>
  137. [SecurityCritical]
  138. private static string GetSuffixedDirectoryNameCore(KernelTransaction transaction, string path)
  139. {
  140. DirectoryInfo di = Directory.GetParentCore(transaction, path, PathFormat.RelativePath);
  141. return di != null && di.Parent != null && di.Name != null
  142. ? AddTrailingDirectorySeparator(CombineCore(false, di.Parent.FullName, di.Name), false)
  143. : null;
  144. }
  145. /// <summary>Returns the directory information for the specified <paramref name="path"/> without the root and with a trailing <see cref="DirectorySeparatorChar"/> character.</summary>
  146. /// <returns>
  147. /// <para>The directory information for the specified <paramref name="path"/> without the root and with a trailing <see cref="DirectorySeparatorChar"/> character,</para>
  148. /// <para>or <see langword="null"/> if <paramref name="path"/> is <see langword="null"/> or if <paramref name="path"/> is <see langword="null"/>.</para>
  149. /// </returns>
  150. /// <param name="transaction">The transaction.</param>
  151. /// <param name="path">The path.</param>
  152. [SecurityCritical]
  153. private static string GetSuffixedDirectoryNameWithoutRootCore(KernelTransaction transaction, string path)
  154. {
  155. DirectoryInfo di = Directory.GetParentCore(transaction, path, PathFormat.RelativePath);
  156. if (di == null || di.Parent == null)
  157. return null;
  158. DirectoryInfo tmp = di;
  159. string suffixedDirectoryNameWithoutRoot;
  160. do
  161. {
  162. suffixedDirectoryNameWithoutRoot = tmp.DisplayPath.Replace(di.Root.ToString(), string.Empty);
  163. if (tmp.Parent != null)
  164. tmp = di.Parent.Parent;
  165. } while (tmp != null && tmp.Root.Parent != null && tmp.Parent != null && !Utils.IsNullOrWhiteSpace(tmp.Parent.ToString()));
  166. return Utils.IsNullOrWhiteSpace(suffixedDirectoryNameWithoutRoot)
  167. ? null
  168. : AddTrailingDirectorySeparator(suffixedDirectoryNameWithoutRoot.TrimStart(DirectorySeparatorChar), false);
  169. // TrimStart() for network-drive, like: C$
  170. }
  171. #endregion // Internal Methods
  172. }
  173. }