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.

Directory.DeleteEmptySubdirectories.cs 18 KiB

7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
7 years ago
123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333
  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.Collections.Generic;
  23. using System.Globalization;
  24. using System.IO;
  25. using System.Linq;
  26. using System.Security;
  27. namespace Alphaleonis.Win32.Filesystem
  28. {
  29. partial class Directory
  30. {
  31. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  32. /// <exception cref="ArgumentException"/>
  33. /// <exception cref="ArgumentNullException"/>
  34. /// <exception cref="DirectoryNotFoundException"/>
  35. /// <exception cref="IOException"/>
  36. /// <exception cref="NotSupportedException"/>
  37. /// <exception cref="UnauthorizedAccessException"/>
  38. /// <exception cref="DirectoryReadOnlyException"/>
  39. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  40. [SecurityCritical]
  41. public static void DeleteEmptySubdirectories(string path)
  42. {
  43. DeleteEmptySubdirectoriesCore(null, null, path, false, false, PathFormat.RelativePath);
  44. }
  45. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  46. /// <exception cref="ArgumentException"/>
  47. /// <exception cref="ArgumentNullException"/>
  48. /// <exception cref="DirectoryNotFoundException"/>
  49. /// <exception cref="IOException"/>
  50. /// <exception cref="NotSupportedException"/>
  51. /// <exception cref="UnauthorizedAccessException"/>
  52. /// <exception cref="DirectoryReadOnlyException"/>
  53. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  54. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  55. [SecurityCritical]
  56. public static void DeleteEmptySubdirectories(string path, bool recursive)
  57. {
  58. DeleteEmptySubdirectoriesCore(null, null, path, recursive, false, PathFormat.RelativePath);
  59. }
  60. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  61. /// <exception cref="ArgumentException"/>
  62. /// <exception cref="ArgumentNullException"/>
  63. /// <exception cref="DirectoryNotFoundException"/>
  64. /// <exception cref="IOException"/>
  65. /// <exception cref="NotSupportedException"/>
  66. /// <exception cref="UnauthorizedAccessException"/>
  67. /// <exception cref="DirectoryReadOnlyException"/>
  68. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  69. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  70. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  71. [SecurityCritical]
  72. public static void DeleteEmptySubdirectories(string path, bool recursive, PathFormat pathFormat)
  73. {
  74. DeleteEmptySubdirectoriesCore(null, null, path, recursive, false, pathFormat);
  75. }
  76. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  77. /// <exception cref="ArgumentException"/>
  78. /// <exception cref="ArgumentNullException"/>
  79. /// <exception cref="DirectoryNotFoundException"/>
  80. /// <exception cref="IOException"/>
  81. /// <exception cref="NotSupportedException"/>
  82. /// <exception cref="UnauthorizedAccessException"/>
  83. /// <exception cref="DirectoryReadOnlyException"/>
  84. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  85. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  86. /// <param name="ignoreReadOnly"><see langword="true"/> overrides read only <see cref="FileAttributes"/> of empty directories.</param>
  87. [SecurityCritical]
  88. public static void DeleteEmptySubdirectories(string path, bool recursive, bool ignoreReadOnly)
  89. {
  90. DeleteEmptySubdirectoriesCore(null, null, path, recursive, ignoreReadOnly, PathFormat.RelativePath);
  91. }
  92. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  93. /// <exception cref="ArgumentException"/>
  94. /// <exception cref="ArgumentNullException"/>
  95. /// <exception cref="DirectoryNotFoundException"/>
  96. /// <exception cref="IOException"/>
  97. /// <exception cref="NotSupportedException"/>
  98. /// <exception cref="UnauthorizedAccessException"/>
  99. /// <exception cref="DirectoryReadOnlyException"/>
  100. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  101. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  102. /// <param name="ignoreReadOnly"><see langword="true"/> overrides read only <see cref="FileAttributes"/> of empty directories.</param>
  103. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  104. [SecurityCritical]
  105. public static void DeleteEmptySubdirectories(string path, bool recursive, bool ignoreReadOnly, PathFormat pathFormat)
  106. {
  107. DeleteEmptySubdirectoriesCore(null, null, path, recursive, ignoreReadOnly, pathFormat);
  108. }
  109. #region Transactional
  110. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  111. /// <exception cref="ArgumentException"/>
  112. /// <exception cref="ArgumentNullException"/>
  113. /// <exception cref="DirectoryNotFoundException"/>
  114. /// <exception cref="IOException"/>
  115. /// <exception cref="NotSupportedException"/>
  116. /// <exception cref="UnauthorizedAccessException"/>
  117. /// <exception cref="DirectoryReadOnlyException"/>
  118. /// <param name="transaction">The transaction.</param>
  119. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  120. [SecurityCritical]
  121. public static void DeleteEmptySubdirectoriesTransacted(KernelTransaction transaction, string path)
  122. {
  123. DeleteEmptySubdirectoriesCore(null, transaction, path, false, false, PathFormat.RelativePath);
  124. }
  125. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  126. /// <exception cref="ArgumentException"/>
  127. /// <exception cref="ArgumentNullException"/>
  128. /// <exception cref="DirectoryNotFoundException"/>
  129. /// <exception cref="IOException"/>
  130. /// <exception cref="NotSupportedException"/>
  131. /// <exception cref="UnauthorizedAccessException"/>
  132. /// <exception cref="DirectoryReadOnlyException"/>
  133. /// <param name="transaction">The transaction.</param>
  134. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  135. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  136. [SecurityCritical]
  137. public static void DeleteEmptySubdirectoriesTransacted(KernelTransaction transaction, string path, bool recursive)
  138. {
  139. DeleteEmptySubdirectoriesCore(null, transaction, path, recursive, false, PathFormat.RelativePath);
  140. }
  141. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  142. /// <exception cref="ArgumentException"/>
  143. /// <exception cref="ArgumentNullException"/>
  144. /// <exception cref="DirectoryNotFoundException"/>
  145. /// <exception cref="IOException"/>
  146. /// <exception cref="NotSupportedException"/>
  147. /// <exception cref="UnauthorizedAccessException"/>
  148. /// <exception cref="DirectoryReadOnlyException"/>
  149. /// <param name="transaction">The transaction.</param>
  150. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  151. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  152. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  153. [SecurityCritical]
  154. public static void DeleteEmptySubdirectoriesTransacted(KernelTransaction transaction, string path, bool recursive, PathFormat pathFormat)
  155. {
  156. DeleteEmptySubdirectoriesCore(null, transaction, path, recursive, false, pathFormat);
  157. }
  158. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  159. /// <exception cref="ArgumentException"/>
  160. /// <exception cref="ArgumentNullException"/>
  161. /// <exception cref="DirectoryNotFoundException"/>
  162. /// <exception cref="IOException"/>
  163. /// <exception cref="NotSupportedException"/>
  164. /// <exception cref="UnauthorizedAccessException"/>
  165. /// <exception cref="DirectoryReadOnlyException"/>
  166. /// <param name="transaction">The transaction.</param>
  167. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  168. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  169. /// <param name="ignoreReadOnly"><see langword="true"/> overrides read only <see cref="FileAttributes"/> of empty directories.</param>
  170. [SecurityCritical]
  171. public static void DeleteEmptySubdirectoriesTransacted(KernelTransaction transaction, string path, bool recursive, bool ignoreReadOnly)
  172. {
  173. DeleteEmptySubdirectoriesCore(null, transaction, path, recursive, ignoreReadOnly, PathFormat.RelativePath);
  174. }
  175. /// <summary>[AlphaFS] Deletes empty subdirectories from the specified directory.</summary>
  176. /// <exception cref="ArgumentException"/>
  177. /// <exception cref="ArgumentNullException"/>
  178. /// <exception cref="DirectoryNotFoundException"/>
  179. /// <exception cref="IOException"/>
  180. /// <exception cref="NotSupportedException"/>
  181. /// <exception cref="UnauthorizedAccessException"/>
  182. /// <exception cref="DirectoryReadOnlyException"/>
  183. /// <param name="transaction">The transaction.</param>
  184. /// <param name="path">The name of the directory to remove empty subdirectories from.</param>
  185. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  186. /// <param name="ignoreReadOnly"><see langword="true"/> overrides read only <see cref="FileAttributes"/> of empty directories.</param>
  187. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  188. [SecurityCritical]
  189. public static void DeleteEmptySubdirectoriesTransacted(KernelTransaction transaction, string path, bool recursive, bool ignoreReadOnly, PathFormat pathFormat)
  190. {
  191. DeleteEmptySubdirectoriesCore(null, transaction, path, recursive, ignoreReadOnly, pathFormat);
  192. }
  193. #endregion // Transactional
  194. #region Internal Methods
  195. /// <summary>[AlphaFS] Delete empty subdirectories from the specified directory.</summary>
  196. /// <exception cref="ArgumentException"/>
  197. /// <exception cref="ArgumentNullException"/>
  198. /// <exception cref="DirectoryNotFoundException"/>
  199. /// <exception cref="IOException"/>
  200. /// <exception cref="NotSupportedException"/>
  201. /// <exception cref="UnauthorizedAccessException"/>
  202. /// <exception cref="DirectoryReadOnlyException"/>
  203. /// <param name="fsEntryInfo">A FileSystemEntryInfo instance. Use either <paramref name="fsEntryInfo"/> or <paramref name="path"/>, not both.</param>
  204. /// <param name="transaction">The transaction.</param>
  205. /// <param name="path">The name of the directory to remove empty subdirectories from. Use either <paramref name="path"/> or <paramref name="fsEntryInfo"/>, not both.</param>
  206. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  207. /// <param name="ignoreReadOnly"><see langword="true"/> overrides read only <see cref="FileAttributes"/> of empty directories.</param>
  208. /// <param name="initialize">When <see langword="true"/> indicates the method is called externally.</param>
  209. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  210. [SecurityCritical]
  211. internal static void DeleteEmptySubdirectoriesCore0(FileSystemEntryInfo fsEntryInfo, KernelTransaction transaction, string path, bool recursive, bool ignoreReadOnly, bool initialize, PathFormat pathFormat)
  212. {
  213. #region Setup
  214. if (fsEntryInfo == null)
  215. {
  216. if (pathFormat == PathFormat.RelativePath)
  217. Path.CheckSupportedPathFormat(path, true, true);
  218. if (!File.ExistsCore(true, transaction, path, pathFormat))
  219. NativeError.ThrowException(Win32Errors.ERROR_PATH_NOT_FOUND, path);
  220. fsEntryInfo = File.GetFileSystemEntryInfoCore(true, transaction, Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck) , false, pathFormat);
  221. if (fsEntryInfo == null)
  222. return;
  223. }
  224. #endregion // Setup
  225. // Ensure path is a directory.
  226. if (!fsEntryInfo.IsDirectory)
  227. throw new IOException(string.Format(CultureInfo.CurrentCulture, Resources.Target_Directory_Is_A_File, fsEntryInfo.LongFullPath));
  228. var dirEnumOptions = DirectoryEnumerationOptions.Folders | DirectoryEnumerationOptions.SkipReparsePoints;
  229. if (recursive)
  230. dirEnumOptions |= DirectoryEnumerationOptions.Recursive;
  231. foreach (var fsei in EnumerateFileSystemEntryInfosCore<FileSystemEntryInfo>(transaction, fsEntryInfo.LongFullPath, Path.WildcardStarMatchAll, dirEnumOptions, PathFormat.LongFullPath))
  232. DeleteEmptySubdirectoriesCore0(fsei, transaction, null, recursive, ignoreReadOnly, false, PathFormat.LongFullPath);
  233. if (!EnumerateFileSystemEntryInfosCore<string>(transaction, fsEntryInfo.LongFullPath, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.LongFullPath).Any())
  234. {
  235. // Prevent deleting path itself.
  236. if (!initialize)
  237. DeleteDirectoryCore(fsEntryInfo, transaction, null, false, ignoreReadOnly, true, PathFormat.LongFullPath);
  238. }
  239. }
  240. /// <summary>[AlphaFS] Delete empty subdirectories from the specified directory.</summary>
  241. /// <exception cref="ArgumentException"/>
  242. /// <exception cref="ArgumentNullException"/>
  243. /// <exception cref="DirectoryNotFoundException"/>
  244. /// <exception cref="IOException"/>
  245. /// <exception cref="NotSupportedException"/>
  246. /// <exception cref="UnauthorizedAccessException"/>
  247. /// <exception cref="DirectoryReadOnlyException"/>
  248. /// <param name="fsEntryInfo">A FileSystemEntryInfo instance. Use either <paramref name="fsEntryInfo"/> or <paramref name="path"/>, not both.</param>
  249. /// <param name="transaction">The transaction.</param>
  250. /// <param name="path">The name of the directory to remove empty subdirectories from. Use either <paramref name="path"/> or <paramref name="fsEntryInfo"/>, not both.</param>
  251. /// <param name="recursive"><see langword="true"/> deletes empty subdirectories from this directory and its subdirectories.</param>
  252. /// <param name="ignoreReadOnly"><see langword="true"/> overrides read only <see cref="FileAttributes"/> of empty directories.</param>
  253. /// <param name="pathFormat">Indicates the format of the path parameter(s).</param>
  254. [SecurityCritical]
  255. internal static void DeleteEmptySubdirectoriesCore(FileSystemEntryInfo fsEntryInfo, KernelTransaction transaction, string path, bool recursive, bool ignoreReadOnly, PathFormat pathFormat)
  256. {
  257. #region Setup
  258. if (fsEntryInfo == null)
  259. {
  260. if (pathFormat == PathFormat.RelativePath)
  261. Path.CheckSupportedPathFormat(path, true, true);
  262. if (!File.ExistsCore(true, transaction, path, pathFormat))
  263. NativeError.ThrowException(Win32Errors.ERROR_PATH_NOT_FOUND, path);
  264. fsEntryInfo = File.GetFileSystemEntryInfoCore(true, transaction, Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck), false, pathFormat);
  265. if (fsEntryInfo == null)
  266. return;
  267. }
  268. #endregion // Setup
  269. // Ensure path is a directory.
  270. if (!fsEntryInfo.IsDirectory)
  271. throw new IOException(string.Format(CultureInfo.CurrentCulture, Resources.Target_Directory_Is_A_File, fsEntryInfo.LongFullPath));
  272. var dirs = new Stack<string>(1000);
  273. dirs.Push(fsEntryInfo.LongFullPath);
  274. while (dirs.Count > 0)
  275. {
  276. foreach (var fsei in EnumerateFileSystemEntryInfosCore<FileSystemEntryInfo>(transaction, dirs.Pop(), Path.WildcardStarMatchAll, DirectoryEnumerationOptions.Folders | DirectoryEnumerationOptions.ContinueOnException, PathFormat.LongFullPath))
  277. {
  278. // Ensure the directory is empty.
  279. if (!EnumerateFileSystemEntryInfosCore<string>(transaction, fsei.LongFullPath, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.LongFullPath).Any())
  280. DeleteDirectoryCore(fsei, transaction, null, false, ignoreReadOnly, true, PathFormat.LongFullPath);
  281. else if (recursive)
  282. dirs.Push(fsei.LongFullPath);
  283. }
  284. }
  285. }
  286. #endregion // Internal Methods
  287. }
  288. }