/* 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.IO;
using System.Linq;
using System.Security;
namespace Alphaleonis.Win32.Filesystem
{
partial class Directory
{
/// [AlphaFS] Gets the properties of the particular directory without following any symbolic links or mount points.
/// Properties include aggregated info from of each encountered file system object, plus additional ones: Total, File, Size and Error.
/// Total: is the total number of enumerated objects.
/// File: is the total number of files. File is considered when object is neither nor .
/// Size: is the total size of enumerated objects.
/// Error: is the total number of errors encountered during enumeration.
///
/// A dictionary mapping the keys mentioned above to their respective aggregated values.
/// Directory: is an object which has attribute without one.
///
///
///
///
///
///
/// The target directory.
[SecurityCritical]
public static Dictionary GetProperties(string path)
{
return GetPropertiesCore(null, path, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.RelativePath);
}
/// [AlphaFS] Gets the properties of the particular directory without following any symbolic links or mount points.
/// Properties include aggregated info from of each encountered file system object, plus additional ones: Total, File, Size and Error.
/// Total: is the total number of enumerated objects.
/// File: is the total number of files. File is considered when object is neither nor .
/// Size: is the total size of enumerated objects.
/// Error: is the total number of errors encountered during enumeration.
///
/// A dictionary mapping the keys mentioned above to their respective aggregated values.
/// Directory: is an object which has attribute without one.
///
///
///
///
///
///
/// The target directory.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
public static Dictionary GetProperties(string path, PathFormat pathFormat)
{
return GetPropertiesCore(null, path, DirectoryEnumerationOptions.FilesAndFolders, pathFormat);
}
/// [AlphaFS] Gets the properties of the particular directory without following any symbolic links or mount points.
/// Properties include aggregated info from of each encountered file system object, plus additional ones: Total, File, Size and Error.
/// Total: is the total number of enumerated objects.
/// File: is the total number of files. File is considered when object is neither nor .
/// Size: is the total size of enumerated objects.
/// Error: is the total number of errors encountered during enumeration.
///
/// A dictionary mapping the keys mentioned above to their respective aggregated values.
/// Directory: is an object which has attribute without one.
///
///
///
///
///
///
/// The target directory.
/// flags that specify how the directory is to be enumerated.
[SecurityCritical]
public static Dictionary GetProperties(string path, DirectoryEnumerationOptions options)
{
return GetPropertiesCore(null, path, options, PathFormat.RelativePath);
}
/// [AlphaFS] Gets the properties of the particular directory without following any symbolic links or mount points.
/// Properties include aggregated info from of each encountered file system object, plus additional ones: Total, File, Size and Error.
/// Total: is the total number of enumerated objects.
/// File: is the total number of files. File is considered when object is neither nor .
/// Size: is the total size of enumerated objects.
/// Error: is the total number of errors encountered during enumeration.
///
/// A dictionary mapping the keys mentioned above to their respective aggregated values.
/// Directory: is an object which has attribute without one.
///
///
///
///
///
///
/// The target directory.
/// flags that specify how the directory is to be enumerated.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
public static Dictionary GetProperties(string path, DirectoryEnumerationOptions options, PathFormat pathFormat)
{
return GetPropertiesCore(null, path, options, pathFormat);
}
#region Transactional
/// [AlphaFS] Gets the properties of the particular directory without following any symbolic links or mount points.
/// Properties include aggregated info from of each encountered file system object, plus additional ones: Total, File, Size and Error.
/// Total: is the total number of enumerated objects.
/// File: is the total number of files. File is considered when object is neither nor .
/// Size: is the total size of enumerated objects.
/// Error: is the total number of errors encountered during enumeration.
///
/// A dictionary mapping the keys mentioned above to their respective aggregated values.
/// Directory: is an object which has attribute without one.
///
///
///
///
///
///
/// The transaction.
/// The target directory.
[SecurityCritical]
public static Dictionary GetPropertiesTransacted(KernelTransaction transaction, string path)
{
return GetPropertiesCore(transaction, path, DirectoryEnumerationOptions.FilesAndFolders, PathFormat.RelativePath);
}
/// [AlphaFS] Gets the properties of the particular directory without following any symbolic links or mount points.
/// Properties include aggregated info from of each encountered file system object, plus additional ones: Total, File, Size and Error.
/// Total: is the total number of enumerated objects.
/// File: is the total number of files. File is considered when object is neither nor .
/// Size: is the total size of enumerated objects.
/// Error: is the total number of errors encountered during enumeration.
///
/// A dictionary mapping the keys mentioned above to their respective aggregated values.
/// Directory: is an object which has attribute without one.
///
///
///
///
///
///
/// The transaction.
/// The target directory.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
public static Dictionary GetPropertiesTransacted(KernelTransaction transaction, string path, PathFormat pathFormat)
{
return GetPropertiesCore(transaction, path, DirectoryEnumerationOptions.FilesAndFolders, pathFormat);
}
/// [AlphaFS] Gets the properties of the particular directory without following any symbolic links or mount points.
/// Properties include aggregated info from of each encountered file system object, plus additional ones: Total, File, Size and Error.
/// Total: is the total number of enumerated objects.
/// File: is the total number of files. File is considered when object is neither nor .
/// Size: is the total size of enumerated objects.
/// Error: is the total number of errors encountered during enumeration.
///
/// A dictionary mapping the keys mentioned above to their respective aggregated values.
/// Directory: is an object which has attribute without one.
///
///
///
///
///
///
/// The transaction.
/// The target directory.
/// flags that specify how the directory is to be enumerated.
[SecurityCritical]
public static Dictionary GetPropertiesTransacted(KernelTransaction transaction, string path, DirectoryEnumerationOptions options)
{
return GetPropertiesCore(transaction, path, options, PathFormat.RelativePath);
}
/// [AlphaFS] Gets the properties of the particular directory without following any symbolic links or mount points.
/// Properties include aggregated info from of each encountered file system object, plus additional ones: Total, File, Size and Error.
/// Total: is the total number of enumerated objects.
/// File: is the total number of files. File is considered when object is neither nor .
/// Size: is the total size of enumerated objects.
/// Error: is the total number of errors encountered during enumeration.
///
/// A dictionary mapping the keys mentioned above to their respective aggregated values.
/// Directory: is an object which has attribute without one.
///
///
///
///
///
///
/// The transaction.
/// The target directory.
/// flags that specify how the directory is to be enumerated.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
public static Dictionary GetPropertiesTransacted(KernelTransaction transaction, string path, DirectoryEnumerationOptions options, PathFormat pathFormat)
{
return GetPropertiesCore(transaction, path, options, pathFormat);
}
#endregion // Transactional
#region Internal Methods
/// [AlphaFS] Gets the properties of the particular directory without following any symbolic links or mount points.
/// Properties include aggregated info from of each encountered file system object, plus additional ones: Total, File, Size and Error.
/// Total: is the total number of enumerated objects.
/// File: is the total number of files. File is considered when object is neither nor .
/// Size: is the total size of enumerated objects.
/// Error: is the total number of errors encountered during enumeration.
///
/// A dictionary mapping the keys mentioned above to their respective aggregated values.
/// Directory: is an object which has attribute without one.
///
///
///
///
///
///
/// The transaction.
/// The target directory.
/// flags that specify how the directory is to be enumerated.
/// Indicates the format of the path parameter(s).
[SecurityCritical]
internal static Dictionary GetPropertiesCore(KernelTransaction transaction, string path, DirectoryEnumerationOptions options, PathFormat pathFormat)
{
long total = 0;
long size = 0;
const string propFile = "File";
const string propTotal = "Total";
const string propSize = "Size";
var typeOfAttrs = typeof(FileAttributes);
var attributes = Enum.GetValues(typeOfAttrs);
var props = Enum.GetNames(typeOfAttrs).OrderBy(attrs => attrs).ToDictionary(name => name, name => 0);
var pathLp = Path.GetExtendedLengthPathCore(transaction, path, pathFormat, GetFullPathOptions.RemoveTrailingDirectorySeparator | GetFullPathOptions.FullCheck);
foreach (var fsei in EnumerateFileSystemEntryInfosCore(transaction, pathLp, Path.WildcardStarMatchAll, options, PathFormat.LongFullPath))
{
total++;
if (!fsei.IsDirectory)
size += fsei.FileSize;
var fsei1 = fsei;
foreach (var attributeMarker in attributes.Cast().Where(attributeMarker => (fsei1.Attributes & attributeMarker) != 0))
props[((attributeMarker & FileAttributes.Directory) != 0 ? FileAttributes.Directory : attributeMarker).ToString()]++;
}
// Adjust regular files count.
props.Add(propFile, total - props[FileAttributes.Directory.ToString()] - props[FileAttributes.ReparsePoint.ToString()]);
props.Add(propTotal, total);
props.Add(propSize, size);
return props;
}
#endregion // Internal Methods
}
}