// Copyright (c) Microsoft Corporation. All rights reserved. // Licensed under the MIT License. using System.Collections.ObjectModel; using System.Management.Automation.Provider; using Dbg = System.Management.Automation; #pragma warning disable 1634, 1691 // Stops compiler from warning about unknown warnings #pragma warning disable 56500 namespace System.Management.Automation { /// /// Holds the state of a Monad Shell session. /// internal sealed partial class SessionStateInternal { #region ItemCmdletProvider accessors #region GetItem /// /// Gets the specified object. /// /// /// The path(s) to the object(s). They can be either a relative (most common) /// or absolute path. /// /// /// Passed on to providers to force operations. /// /// /// If true, globbing is not done on paths. /// /// /// The item at the specified path. /// /// /// 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 the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// internal Collection GetItem(string[] paths, bool force, bool literalPath) { if (paths == null) { throw PSTraceSource.NewArgumentNullException("paths"); } CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext); context.Force = force; context.SuppressWildcardExpansion = literalPath; GetItem(paths, context); context.ThrowFirstErrorOrDoNothing(); // Since there was not errors return the accumulated objects Collection results = context.GetAccumulatedObjects(); return results; } /// /// Gets the specified object. /// /// /// The path(s) to the object(s). They can be either a relative (most common) /// or absolute path. /// /// /// The context which the core command is running. /// /// /// Nothing is returned, but all objects should be written to the WriteObject /// method of the parameter. /// /// /// 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 the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal void GetItem( string[] paths, CmdletProviderContext context) { if (paths == null) { throw PSTraceSource.NewArgumentNullException("paths"); } ProviderInfo provider = null; CmdletProvider providerInstance = null; foreach (string path in paths) { if (path == null) { throw PSTraceSource.NewArgumentNullException("paths"); } Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, false, context, out provider, out providerInstance); foreach (string providerPath in providerPaths) { GetItemPrivate(providerInstance, providerPath, context); } } } /// /// Gets the item at the specified path. /// /// /// The provider instance to use. /// /// /// The path to the item if it was specified on the command line. /// /// /// The context which the core command is running. /// /// /// If the does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private void GetItemPrivate( CmdletProvider providerInstance, string path, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); ItemCmdletProvider itemCmdletProvider = GetItemProviderInstance(providerInstance); try { itemCmdletProvider.GetItem(path, context); } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "GetItemProviderException", SessionStateStrings.GetItemProviderException, itemCmdletProvider.ProviderInfo, path, e); } } /// /// Gets the dynamic parameters for the get-item 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 the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal object GetItemDynamicParameters(string path, CmdletProviderContext context) { if (path == null) { return null; } ProviderInfo provider = null; CmdletProvider providerInstance = null; CmdletProviderContext newContext = new CmdletProviderContext(context); newContext.SetFilters( new Collection(), new Collection(), null); Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, true, newContext, out provider, out providerInstance); if (providerPaths.Count > 0) { // Get the dynamic parameters for the first resolved path return GetItemDynamicParameters(providerInstance, providerPaths[0], newContext); } return null; } /// /// Gets the dynamic parameters for the get-item cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The instance of the provider to use. /// /// /// 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 does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private object GetItemDynamicParameters( CmdletProvider providerInstance, string path, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); ItemCmdletProvider itemCmdletProvider = GetItemProviderInstance(providerInstance); object result = null; try { result = itemCmdletProvider.GetItemDynamicParameters(path, context); } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "GetItemDynamicParametersProviderException", SessionStateStrings.GetItemDynamicParametersProviderException, itemCmdletProvider.ProviderInfo, path, e); } return result; } #endregion GetItem #region SetItem /// /// Gets the specified object. /// /// /// The path(s) to the object. It can be either a relative (most common) /// or absolute path. /// /// /// The new value for the item at the specified path. /// /// /// Passed on to providers to force operations. /// /// /// If true, globbing is not done on paths. /// /// /// The item that was modified at the specified path. /// /// /// 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 the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// internal Collection SetItem(string[] paths, object value, bool force, bool literalPath) { if (paths == null) { throw PSTraceSource.NewArgumentNullException("paths"); } CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext); context.Force = force; context.SuppressWildcardExpansion = literalPath; SetItem(paths, value, context); context.ThrowFirstErrorOrDoNothing(); // Since there was no errors return the accumulated objects return context.GetAccumulatedObjects(); } /// /// Sets the specified object to the specified value. /// /// /// The path(s) to the object. It can be either a relative (most common) /// or absolute path. /// /// /// The new value of the item at the specified path. /// /// /// The context which the core command is running. /// /// /// 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 the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal void SetItem( string[] paths, object value, CmdletProviderContext context) { if (paths == null) { throw PSTraceSource.NewArgumentNullException("paths"); } foreach (string path in paths) { if (path == null) { throw PSTraceSource.NewArgumentNullException("paths"); } ProviderInfo provider = null; CmdletProvider providerInstance = null; Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, true, context, out provider, out providerInstance); if (providerPaths != null) { foreach (string providerPath in providerPaths) { SetItem(providerInstance, providerPath, value, context); } } } } /// /// Sets item at the specified path. /// /// /// The provider instance to use. /// /// /// The path to the item if it was specified on the command line. /// /// /// The value of the item. /// /// /// The context which the core command is running. /// /// /// If the does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private void SetItem( CmdletProvider providerInstance, string path, object value, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); ItemCmdletProvider itemCmdletProvider = GetItemProviderInstance(providerInstance); try { itemCmdletProvider.SetItem(path, value, context); } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "SetItemProviderException", SessionStateStrings.SetItemProviderException, itemCmdletProvider.ProviderInfo, path, e); } } /// /// Gets the dynamic parameters for the set-item cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The new value of the item at the specified path. /// /// /// 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 the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal object SetItemDynamicParameters(string path, object value, CmdletProviderContext context) { if (path == null) { return null; } ProviderInfo provider = null; CmdletProvider providerInstance = null; CmdletProviderContext newContext = new CmdletProviderContext(context); newContext.SetFilters( new Collection(), new Collection(), null); Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, true, newContext, out provider, out providerInstance); if (providerPaths.Count > 0) { // Get the dynamic parameters for the first resolved path return SetItemDynamicParameters(providerInstance, providerPaths[0], value, newContext); } return null; } /// /// Gets the dynamic parameters for the set-item cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The instance of the provider to use. /// /// /// The value to be set. /// /// /// 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 does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private object SetItemDynamicParameters( CmdletProvider providerInstance, string path, object value, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); ItemCmdletProvider itemCmdletProvider = GetItemProviderInstance(providerInstance); object result = null; try { result = itemCmdletProvider.SetItemDynamicParameters(path, value, context); } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "SetItemDynamicParametersProviderException", SessionStateStrings.SetItemDynamicParametersProviderException, itemCmdletProvider.ProviderInfo, path, e); } return result; } #endregion SetItem #region ClearItem /// /// Clears the specified object. Depending on the provider that the path /// maps to, this could mean the properties and/or content and/or value is /// cleared. /// /// /// The path(s) to the object. It can be either a relative (most common) /// or absolute path. /// /// /// Passed on to providers to force operations. /// /// /// If true, globbing is not done on paths. /// /// /// The items that were cleared. /// /// /// If an error occurs that error will be thrown. /// /// /// 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 the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// internal Collection ClearItem(string[] paths, bool force, bool literalPath) { if (paths == null) { throw PSTraceSource.NewArgumentNullException("paths"); } CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext); context.Force = force; context.SuppressWildcardExpansion = literalPath; ClearItem(paths, context); context.ThrowFirstErrorOrDoNothing(); return context.GetAccumulatedObjects(); } /// /// Clears the specified item. Depending on the provider that the path /// maps to, this could mean the properties and/or content and/or value is /// cleared. /// /// /// The path(s) to the object. It can be either a relative (most common) /// or absolute path. /// /// /// The context which the core command is running. /// /// /// 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 the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal void ClearItem( string[] paths, CmdletProviderContext context) { if (paths == null) { throw PSTraceSource.NewArgumentNullException("paths"); } ProviderInfo provider = null; CmdletProvider providerInstance = null; foreach (string path in paths) { if (path == null) { throw PSTraceSource.NewArgumentNullException("paths"); } Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, false, context, out provider, out providerInstance); if (providerPaths != null) { foreach (string providerPath in providerPaths) { ClearItemPrivate(providerInstance, providerPath, context); } } } } /// /// Clears the item at the specified path. /// /// /// The provider instance to use. /// /// /// The path to the item if it was specified on the command line. /// /// /// The context which the core command is running. /// /// /// If the does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private void ClearItemPrivate( CmdletProvider providerInstance, string path, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); ItemCmdletProvider itemCmdletProvider = GetItemProviderInstance(providerInstance); try { itemCmdletProvider.ClearItem(path, context); } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "ClearItemProviderException", SessionStateStrings.ClearItemProviderException, itemCmdletProvider.ProviderInfo, path, e); } } /// /// Gets the dynamic parameters for the clear-item 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 the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal object ClearItemDynamicParameters(string path, CmdletProviderContext context) { if (path == null) { return null; } ProviderInfo provider = null; CmdletProvider providerInstance = null; CmdletProviderContext newContext = new CmdletProviderContext(context); newContext.SetFilters( new Collection(), new Collection(), null); Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, true, newContext, out provider, out providerInstance); if (providerPaths.Count > 0) { // Get the dynamic parameters for the first resolved path return ClearItemDynamicParameters(providerInstance, providerPaths[0], newContext); } return null; } /// /// Gets the dynamic parameters for the clear-item cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The instance of the provider to use. /// /// /// 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 does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private object ClearItemDynamicParameters( CmdletProvider providerInstance, string path, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); ItemCmdletProvider itemCmdletProvider = GetItemProviderInstance(providerInstance); object result = null; try { result = itemCmdletProvider.ClearItemDynamicParameters(path, context); } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "ClearItemProviderException", SessionStateStrings.ClearItemProviderException, itemCmdletProvider.ProviderInfo, path, e); } return result; } #endregion ClearItem #region InvokeDefaultAction /// /// Performs the default action on the specified item. The default action is /// determined by the provider. /// /// /// The path(s) to the object(s). They can be either a relative (most common) /// or absolute path(s). /// /// /// If true, globbing is not done on paths. /// /// /// If an error occurs that error will be thrown. /// /// /// 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 the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// internal void InvokeDefaultAction(string[] paths, bool literalPath) { if (paths == null) { throw PSTraceSource.NewArgumentNullException("paths"); } CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext); context.SuppressWildcardExpansion = literalPath; InvokeDefaultAction(paths, context); context.ThrowFirstErrorOrDoNothing(); } /// /// Performs the default action on the specified item. The default action /// is determined by the provider. /// /// /// The path(s) to the object(s). They can be either a relative (most common) /// or absolute paths. /// /// /// The context which the core command is running. /// /// /// 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 the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal void InvokeDefaultAction( string[] paths, CmdletProviderContext context) { if (paths == null) { throw PSTraceSource.NewArgumentNullException("paths"); } ProviderInfo provider = null; CmdletProvider providerInstance = null; foreach (string path in paths) { if (path == null) { throw PSTraceSource.NewArgumentNullException("paths"); } Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, false, context, out provider, out providerInstance); if (providerPaths != null) { foreach (string providerPath in providerPaths) { InvokeDefaultActionPrivate(providerInstance, providerPath, context); } } } } /// /// Invokes the default action on the item at the specified path. /// /// /// The provider instance to use. /// /// /// The path to the item if it was specified on the command line. /// /// /// The context which the core command is running. /// /// /// If the does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private void InvokeDefaultActionPrivate( CmdletProvider providerInstance, string path, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); ItemCmdletProvider itemCmdletProvider = GetItemProviderInstance(providerInstance); try { itemCmdletProvider.InvokeDefaultAction(path, context); } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "InvokeDefaultActionProviderException", SessionStateStrings.InvokeDefaultActionProviderException, itemCmdletProvider.ProviderInfo, path, e); } } /// /// Gets the dynamic parameters for the invoke-item 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 the provider that the refers to does /// not support this operation. /// /// /// If the provider threw an exception. /// /// /// If does not contain glob characters and /// could not be found. /// internal object InvokeDefaultActionDynamicParameters(string path, CmdletProviderContext context) { if (path == null) { return null; } ProviderInfo provider = null; CmdletProvider providerInstance = null; CmdletProviderContext newContext = new CmdletProviderContext(context); newContext.SetFilters( new Collection(), new Collection(), null); Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, true, newContext, out provider, out providerInstance); if (providerPaths.Count > 0) { // Get the dynamic parameters for the first resolved path return InvokeDefaultActionDynamicParameters(providerInstance, providerPaths[0], newContext); } return null; } /// /// Gets the dynamic parameters for the invoke-item cmdlet. /// /// /// The path to the item if it was specified on the command line. /// /// /// The instance of the provider to use. /// /// /// 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 does not support this operation. /// /// /// If the pipeline is being stopped while executing the command. /// /// /// If the provider threw an exception. /// private object InvokeDefaultActionDynamicParameters( CmdletProvider providerInstance, string path, CmdletProviderContext context) { // All parameters should have been validated by caller Dbg.Diagnostics.Assert( providerInstance != null, "Caller should validate providerInstance before calling this method"); Dbg.Diagnostics.Assert( path != null, "Caller should validate path before calling this method"); Dbg.Diagnostics.Assert( context != null, "Caller should validate context before calling this method"); ItemCmdletProvider itemCmdletProvider = GetItemProviderInstance(providerInstance); object result = null; try { result = itemCmdletProvider.InvokeDefaultActionDynamicParameters(path, context); } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { throw NewProviderInvocationException( "InvokeDefaultActionDynamicParametersProviderException", SessionStateStrings.InvokeDefaultActionDynamicParametersProviderException, itemCmdletProvider.ProviderInfo, path, e); } return result; } #endregion InvokeDefaultAction #endregion ItemCmdletProvider accessors } } #pragma warning restore 56500