/* Copyright (C) 2008-2016 Peter Palotas, Jeffrey Jangli, Alexandr Normuradov
*
* Permission is hereby granted, free of charge, to any person obtaining a copy
* of this software and associated documentation files (the "Software"), to deal
* in the Software without restriction, including without limitation the rights
* to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
* copies of the Software, and to permit persons to whom the Software is
* furnished to do so, subject to the following conditions:
*
* The above copyright notice and this permission notice shall be included in
* all copies or substantial portions of the Software.
*
* THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
* IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
* FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
* AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
* LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
* OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN
* THE SOFTWARE.
*/
using System;
using System.Collections.Generic;
using System.Globalization;
using System.IO;
using System.Linq;
using System.Security;
namespace Alphaleonis.Win32.Filesystem
{
partial class Directory
{
/// [AlphaFS] Deletes empty subdirectories from the specified directory.
///
///
///
///
///
///
///
/// The name of the directory to remove empty subdirectories from.
[SecurityCritical]
public static void DeleteEmptySubdirectories(string path)
{
DeleteEmptySubdirectoriesCore(null, null, path, false, false, PathFormat.RelativePath);
}
/// [AlphaFS] Deletes empty subdirectories from the specified directory.
///
///
///
///
///
///
///
/// The name of the directory to remove empty subdirectories from.
/// deletes empty subdirectories from this directory and its subdirectories.
[SecurityCritical]
public static void DeleteEmptySubdirectories(string path, bool recursive)
{
DeleteEmptySubdirectoriesCore(null, null, path, recursive, false, PathFormat.RelativePath);
}
/// [AlphaFS] Deletes empty subdirectories from the specified directory.
///
///
///
///
///
///
///
/// The name of the directory to remove empty subdirectories from.
/// deletes empty subdirectories from this directory and its subdirectories.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
public static void DeleteEmptySubdirectories(string path, bool recursive, PathFormat pathFormat)
{
DeleteEmptySubdirectoriesCore(null, null, path, recursive, false, pathFormat);
}
/// [AlphaFS] Deletes empty subdirectories from the specified directory.
///
///
///
///
///
///
///
/// The name of the directory to remove empty subdirectories from.
/// deletes empty subdirectories from this directory and its subdirectories.
/// overrides read only of empty directories.
[SecurityCritical]
public static void DeleteEmptySubdirectories(string path, bool recursive, bool ignoreReadOnly)
{
DeleteEmptySubdirectoriesCore(null, null, path, recursive, ignoreReadOnly, PathFormat.RelativePath);
}
/// [AlphaFS] Deletes empty subdirectories from the specified directory.
///
///
///
///
///
///
///
/// The name of the directory to remove empty subdirectories from.
/// deletes empty subdirectories from this directory and its subdirectories.
/// overrides read only of empty directories.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
public static void DeleteEmptySubdirectories(string path, bool recursive, bool ignoreReadOnly, PathFormat pathFormat)
{
DeleteEmptySubdirectoriesCore(null, null, path, recursive, ignoreReadOnly, pathFormat);
}
#region Transactional
/// [AlphaFS] Deletes empty subdirectories from the specified directory.
///
///
///
///
///
///
///
/// The transaction.
/// The name of the directory to remove empty subdirectories from.
[SecurityCritical]
public static void DeleteEmptySubdirectoriesTransacted(KernelTransaction transaction, string path)
{
DeleteEmptySubdirectoriesCore(null, transaction, path, false, false, PathFormat.RelativePath);
}
/// [AlphaFS] Deletes empty subdirectories from the specified directory.
///
///
///
///
///
///
///
/// The transaction.
/// The name of the directory to remove empty subdirectories from.
/// deletes empty subdirectories from this directory and its subdirectories.
[SecurityCritical]
public static void DeleteEmptySubdirectoriesTransacted(KernelTransaction transaction, string path, bool recursive)
{
DeleteEmptySubdirectoriesCore(null, transaction, path, recursive, false, PathFormat.RelativePath);
}
/// [AlphaFS] Deletes empty subdirectories from the specified directory.
///
///
///
///
///
///
///
/// The transaction.
/// The name of the directory to remove empty subdirectories from.
/// deletes empty subdirectories from this directory and its subdirectories.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
public static void DeleteEmptySubdirectoriesTransacted(KernelTransaction transaction, string path, bool recursive, PathFormat pathFormat)
{
DeleteEmptySubdirectoriesCore(null, transaction, path, recursive, false, pathFormat);
}
/// [AlphaFS] Deletes empty subdirectories from the specified directory.
///
///
///
///
///
///
///
/// The transaction.
/// The name of the directory to remove empty subdirectories from.
/// deletes empty subdirectories from this directory and its subdirectories.
/// overrides read only of empty directories.
[SecurityCritical]
public static void DeleteEmptySubdirectoriesTransacted(KernelTransaction transaction, string path, bool recursive, bool ignoreReadOnly)
{
DeleteEmptySubdirectoriesCore(null, transaction, path, recursive, ignoreReadOnly, PathFormat.RelativePath);
}
/// [AlphaFS] Deletes empty subdirectories from the specified directory.
///
///
///
///
///
///
///
/// The transaction.
/// The name of the directory to remove empty subdirectories from.
/// deletes empty subdirectories from this directory and its subdirectories.
/// overrides read only of empty directories.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
public static void DeleteEmptySubdirectoriesTransacted(KernelTransaction transaction, string path, bool recursive, bool ignoreReadOnly, PathFormat pathFormat)
{
DeleteEmptySubdirectoriesCore(null, transaction, path, recursive, ignoreReadOnly, pathFormat);
}
#endregion // Transactional
#region Internal Methods
/// [AlphaFS] Delete empty subdirectories from the specified directory.
///
///
///
///
///
///
///
/// A FileSystemEntryInfo instance. Use either or , not both.
/// The transaction.
/// The name of the directory to remove empty subdirectories from. Use either or , not both.
/// deletes empty subdirectories from this directory and its subdirectories.
/// overrides read only of empty directories.
/// When indicates the method is called externally.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
internal static void DeleteEmptySubdirectoriesCore0(FileSystemEntryInfo fsEntryInfo, KernelTransaction transaction, string path, bool recursive, bool ignoreReadOnly, bool initialize, PathFormat pathFormat)
{
#region Setup
if (fsEntryInfo == null)
{
if (pathFormat == PathFormat.RelativePath)
Path.CheckSupportedPathFormat(path, true, true);
if (!File.ExistsCore(true, transaction, path, pathFormat))
NativeError.ThrowException(Win32Errors.ERROR_PATH_NOT_FOUND, path);
fsEntryInfo = File.GetFileSystemEntryInfoCore(true, transaction, Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck) , false, pathFormat);
if (fsEntryInfo == null)
return;
}
#endregion // Setup
// Ensure path is a directory.
if (!fsEntryInfo.IsDirectory)
throw new IOException(string.Format(CultureInfo.CurrentCulture, Resources.Target_Directory_Is_A_File, fsEntryInfo.LongFullPath));
var dirEnumOptions = DirectoryEnumerationOptions.Folders | DirectoryEnumerationOptions.SkipReparsePoints;
if (recursive)
dirEnumOptions |= DirectoryEnumerationOptions.Recursive;
foreach (var fsei in EnumerateFileSystemEntryInfosCore(transaction, fsEntryInfo.LongFullPath, Path.WildcardStarMatchAll, dirEnumOptions, PathFormat.LongFullPath))
DeleteEmptySubdirectoriesCore0(fsei, transaction, null, recursive, ignoreReadOnly, false, PathFormat.LongFullPath);
if (!EnumerateFileSystemEntryInfosCore(transaction, fsEntryInfo.LongFullPath, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.LongFullPath).Any())
{
// Prevent deleting path itself.
if (!initialize)
DeleteDirectoryCore(fsEntryInfo, transaction, null, false, ignoreReadOnly, true, PathFormat.LongFullPath);
}
}
/// [AlphaFS] Delete empty subdirectories from the specified directory.
///
///
///
///
///
///
///
/// A FileSystemEntryInfo instance. Use either or , not both.
/// The transaction.
/// The name of the directory to remove empty subdirectories from. Use either or , not both.
/// deletes empty subdirectories from this directory and its subdirectories.
/// overrides read only of empty directories.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
internal static void DeleteEmptySubdirectoriesCore(FileSystemEntryInfo fsEntryInfo, KernelTransaction transaction, string path, bool recursive, bool ignoreReadOnly, PathFormat pathFormat)
{
#region Setup
if (fsEntryInfo == null)
{
if (pathFormat == PathFormat.RelativePath)
Path.CheckSupportedPathFormat(path, true, true);
if (!File.ExistsCore(true, transaction, path, pathFormat))
NativeError.ThrowException(Win32Errors.ERROR_PATH_NOT_FOUND, path);
fsEntryInfo = File.GetFileSystemEntryInfoCore(true, transaction, Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.TrimEnd | GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck), false, pathFormat);
if (fsEntryInfo == null)
return;
}
#endregion // Setup
// Ensure path is a directory.
if (!fsEntryInfo.IsDirectory)
throw new IOException(string.Format(CultureInfo.CurrentCulture, Resources.Target_Directory_Is_A_File, fsEntryInfo.LongFullPath));
var dirs = new Stack(1000);
dirs.Push(fsEntryInfo.LongFullPath);
while (dirs.Count > 0)
{
foreach (var fsei in EnumerateFileSystemEntryInfosCore(transaction, dirs.Pop(), Path.WildcardStarMatchAll, DirectoryEnumerationOptions.Folders | DirectoryEnumerationOptions.ContinueOnException, PathFormat.LongFullPath))
{
// Ensure the directory is empty.
if (!EnumerateFileSystemEntryInfosCore(transaction, fsei.LongFullPath, Path.WildcardStarMatchAll, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.LongFullPath).Any())
DeleteDirectoryCore(fsei, transaction, null, false, ignoreReadOnly, true, PathFormat.LongFullPath);
else if (recursive)
dirs.Push(fsei.LongFullPath);
}
}
}
#endregion // Internal Methods
}
}