// Copyright (c) Microsoft Corporation. // Licensed under the MIT License. using System.Collections.ObjectModel; using Dbg = System.Management.Automation; namespace System.Management.Automation { /// /// Exposes the Children noun of the Cmdlet Providers to the Cmdlet base class. The methods of this class /// use the providers to perform operations. /// public sealed class ChildItemCmdletProviderIntrinsics { #region Constructors /// /// Hide the default constructor since we always require an instance of SessionState. /// private ChildItemCmdletProviderIntrinsics() { Dbg.Diagnostics.Assert( false, "This constructor should never be called. Only the constructor that takes an instance of SessionState should be called."); } /// /// Constructs a facade over the "real" session state API. /// /// /// An instance of the cmdlet that this class is acting as a facade for. /// internal ChildItemCmdletProviderIntrinsics(Cmdlet cmdlet) { if (cmdlet == null) { throw PSTraceSource.NewArgumentNullException(nameof(cmdlet)); } _cmdlet = cmdlet; _sessionState = cmdlet.Context.EngineSessionState; } /// /// Constructs a facade over the "real" session state API. /// /// /// An instance of the "real" session state. /// /// /// If is null. /// internal ChildItemCmdletProviderIntrinsics(SessionStateInternal sessionState) { if (sessionState == null) { throw PSTraceSource.NewArgumentNullException(nameof(sessionState)); } _sessionState = sessionState; } #endregion Constructors #region Public methods #region GetChildItems /// /// Gets the child items of the container at the given path. /// /// /// The path to the item to retrieve. It may be a drive or provider-qualified path and may include /// glob characters. /// /// /// If true, gets all the children in all the sub-containers of the specified /// container. If false, only gets the immediate children of the specified /// container. /// /// /// The children of the container at the specified path. The type of the objects returned are /// determined by the provider that supports the given path. /// /// /// If or is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If does not contain glob characters and /// could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// public Collection Get(string path, bool recurse) { Dbg.Diagnostics.Assert( _sessionState != null, "The only constructor for this class should always set the sessionState field"); // Parameter validation is done in the session state object return _sessionState.GetChildItems(new string[] { path }, recurse, uint.MaxValue, false, false); } /// /// Gets the child items of the container at the given path(s). /// /// /// The path(s) to the item(s) to retrieve. They may be drive or provider-qualified paths and may include /// glob characters. /// /// /// If true, gets all the children in all the sub-containers of the specified /// container. If false, only gets the immediate children of the specified /// container. /// /// /// Limits the depth of recursion; uint.MaxValue performs full recursion. /// /// /// Passed on to providers to force operations. /// /// /// If true, globbing is not done on paths. /// /// /// The children of the container at the specified path. The type of the objects returned are /// determined by the provider that supports the given path. /// /// /// If or is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If does not contain glob characters and /// could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// public Collection Get(string[] path, bool recurse, uint depth, bool force, bool literalPath) { Dbg.Diagnostics.Assert( _sessionState != null, "The only constructor for this class should always set the sessionState field"); // Parameter validation is done in the session state object return _sessionState.GetChildItems(path, recurse, depth, force, literalPath); } /// /// Gets the child items of the container at the given path(s). /// /// /// The path(s) to the item(s) to retrieve. They may be drive or provider-qualified paths and may include /// glob characters. /// /// /// If true, gets all the children in all the sub-containers of the specified /// container. If false, only gets the immediate children of the specified /// container. /// /// /// Passed on to providers to force operations. /// /// /// If true, globbing is not done on paths. /// /// /// The children of the container at the specified path. The type of the objects returned are /// determined by the provider that supports the given path. /// /// /// If or is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If does not contain glob characters and /// could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// public Collection Get(string[] path, bool recurse, bool force, bool literalPath) { Dbg.Diagnostics.Assert( _sessionState != null, "The only constructor for this class should always set the sessionState field"); // Parameter validation is done in the session state object return this.Get(path, recurse, uint.MaxValue, force, literalPath); } /// /// Gets the child items of the container at the given path. /// /// /// The path to the item to retrieve. It may be a drive or provider-qualified path and may include /// glob characters. /// /// /// If true, gets all the children in all the sub-containers of the specified /// container. If false, only gets the immediate children of the specified /// container. /// /// /// Limits the depth of recursion; uint.MaxValue performs full recursion. /// /// /// The context under which the command is running. /// /// /// Nothing. The children of the container at the specified path are written to the context. /// /// /// If or is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If does not contain glob characters and /// could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// internal void Get( string path, bool recurse, uint depth, CmdletProviderContext context) { Dbg.Diagnostics.Assert( _sessionState != null, "The only constructor for this class should always set the sessionState field"); // Parameter validation is done in the session state object _sessionState.GetChildItems(path, recurse, depth, context); } /// /// Gets the dynamic parameters for the get-childitem cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// If true, gets all the children in all the sub-containers of the specified /// container. If false, only gets the immediate children of the specified /// container. /// /// /// The context which the core command is running. /// /// /// An object that has properties and fields decorated with /// parsing attributes similar to a cmdlet class. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If does not contain glob characters and /// could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// internal object GetChildItemsDynamicParameters( string path, bool recurse, CmdletProviderContext context) { Dbg.Diagnostics.Assert( _sessionState != null, "The only constructor for this class should always set the sessionState field"); // Parameter validation is done in the session state object return _sessionState.GetChildItemsDynamicParameters(path, recurse, context); } #endregion GetChildItems #region GetChildNames /// /// Gets the child names of the container at the given path. /// /// /// The path to the item to retrieve. It may be a drive or provider-qualified path and may include /// glob characters. /// /// /// Determines if all containers should be returned or only those containers that match the /// filter(s). /// /// /// If true, gets all the relative paths of all the children /// in all the sub-containers of the specified /// container. If false, only gets the immediate child names of the specified /// container. /// /// /// The children of the container at the specified path. The type of the objects returned are /// determined by the provider that supports the given path. /// /// /// If or is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If does not contain glob characters and /// could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// public Collection GetNames( string path, ReturnContainers returnContainers, bool recurse) { Dbg.Diagnostics.Assert( _sessionState != null, "The only constructor for this class should always set the sessionState field"); // Parameter validation is done in the session state object return _sessionState.GetChildNames(new string[] { path }, returnContainers, recurse, uint.MaxValue, false, false); } /// /// Gets the child names of the container at the given path. /// /// /// The path(s) to the item(s) to retrieve. They may be drive or provider-qualified paths and may include /// glob characters. /// /// /// Determines if all containers should be returned or only those containers that match the /// filter(s). /// /// /// If true, gets all the relative paths of all the children /// in all the sub-containers of the specified /// container. If false, only gets the immediate child names of the specified /// container. /// /// /// Passed on to providers to force operations. /// /// /// If true, globbing is not done on paths. /// /// /// The children of the container at the specified path. The type of the objects returned are /// determined by the provider that supports the given path. /// /// /// If or is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If does not contain glob characters and /// could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// public Collection GetNames( string[] path, ReturnContainers returnContainers, bool recurse, bool force, bool literalPath) { Dbg.Diagnostics.Assert( _sessionState != null, "The only constructor for this class should always set the sessionState field"); return _sessionState.GetChildNames(path, returnContainers, recurse, uint.MaxValue, force, literalPath); } /// /// Gets the child names of the container at the given path. /// /// /// The path(s) to the item(s) to retrieve. They may be drive or provider-qualified paths and may include /// glob characters. /// /// /// Determines if all containers should be returned or only those containers that match the /// filter(s). /// /// /// If true, gets all the relative paths of all the children /// in all the sub-containers of the specified /// container. If false, only gets the immediate child names of the specified /// container. /// /// /// Limits the depth of recursion; uint.MaxValue performs full recursion. /// /// /// Passed on to providers to force operations. /// /// /// If true, globbing is not done on paths. /// /// /// The children of the container at the specified path. The type of the objects returned are /// determined by the provider that supports the given path. /// /// /// If or is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If does not contain glob characters and /// could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// public Collection GetNames( string[] path, ReturnContainers returnContainers, bool recurse, uint depth, bool force, bool literalPath) { Dbg.Diagnostics.Assert( _sessionState != null, "The only constructor for this class should always set the sessionState field"); return _sessionState.GetChildNames(path, returnContainers, recurse, depth, force, literalPath); } /// /// Gets the child names of the container at the given path. /// /// /// The path to the item to retrieve. It may be a drive or provider-qualified path and may include /// glob characters. /// /// /// Determines if all containers should be returned or only those containers that match the /// filter(s). /// /// /// If true, gets all the relative paths of all the children /// in all the sub-containers of the specified /// container. If false, only gets the immediate child names of the specified /// container. /// /// /// Limits the depth of recursion; uint.MaxValue performs full recursion. /// /// /// The context under which the command is running. /// /// /// Nothing. The names of the children of the specified container are written to the context. /// /// /// If or is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If does not contain glob characters and /// could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// internal void GetNames( string path, ReturnContainers returnContainers, bool recurse, uint depth, CmdletProviderContext context) { Dbg.Diagnostics.Assert( _sessionState != null, "The only constructor for this class should always set the sessionState field"); // Parameter validation is done in the session state object _sessionState.GetChildNames(path, returnContainers, recurse, depth, context); } /// /// Gets the dynamic parameters for the get-childitem -name cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The context which the core command is running. /// /// /// An object that has properties and fields decorated with /// parsing attributes similar to a cmdlet class. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If does not contain glob characters and /// could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// internal object GetChildNamesDynamicParameters( string path, CmdletProviderContext context) { Dbg.Diagnostics.Assert( _sessionState != null, "The only constructor for this class should always set the sessionState field"); // Parameter validation is done in the session state object return _sessionState.GetChildNamesDynamicParameters(path, context); } #endregion GetChildNames #region HasChildItems /// /// Determines if an item at the given path has children. /// /// /// The path to the item to determine if it has children. It may be a drive or provider-qualified path and may include /// glob characters. /// /// /// True if the item at the specified path has children. False otherwise. /// /// /// If is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If does not contain glob characters and /// could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// public bool HasChild(string path) { Dbg.Diagnostics.Assert( _sessionState != null, "The only constructor for this class should always set the sessionState field"); // Parameter validation is done in the session state object return _sessionState.HasChildItems(path, false, false); } /// /// Determines if an item at the given path has children. /// /// /// The path to the item to determine if it has children. It may be a drive or provider-qualified path and may include /// glob characters. /// /// /// Passed on to providers to force operations. /// /// /// If true, globbing is not done on paths. /// /// /// True if the item at the specified path has children. False otherwise. /// /// /// If is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If does not contain glob characters and /// could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// public bool HasChild(string path, bool force, bool literalPath) { Dbg.Diagnostics.Assert( _sessionState != null, "The only constructor for this class should always set the sessionState field"); // Parameter validation is done in the session state object return _sessionState.HasChildItems(path, force, literalPath); } /// /// Determines if an item at the given path has children. /// /// /// The path to the item to determine if it has children. It may be a drive or provider-qualified path and may include /// glob characters. /// /// /// The context under which the command is running. /// /// /// True if the item at the specified path has children. False otherwise. /// /// /// If is null. /// /// /// If the refers to a provider that could not be found. /// /// /// If the refers to a drive that could not be found. /// /// /// If does not contain glob characters and /// could not be found. /// /// /// If the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// internal bool HasChild( string path, CmdletProviderContext context) { Dbg.Diagnostics.Assert( _sessionState != null, "The only constructor for this class should always set the sessionState field"); // Parameter validation is done in the session state object return _sessionState.HasChildItems(path, context); } #endregion HasChildItems #endregion Public methods #region private data private readonly Cmdlet _cmdlet; private readonly SessionStateInternal _sessionState; #endregion private data } /// /// This enum determines which types of containers are returned from some of /// the provider methods. /// public enum ReturnContainers { /// /// Only containers that match the filter(s) are returned. /// ReturnMatchingContainers, /// /// All containers are returned even if they don't match the filter(s). /// ReturnAllContainers } }