/********************************************************************++ Copyright (c) Microsoft Corporation. All rights reserved. --********************************************************************/ using System; 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 IContentCmdletProvider accessors #region GetContentReader /// /// Gets the content reader for the specified item. /// /// /// /// The path(s) to the item(s) to get the content reader for. /// /// /// /// Passed on to providers to force operations. /// /// /// /// If true, globbing is not done on paths. /// /// /// /// The content readers for all items that the path resolves to. /// /// /// /// 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 GetContentReader(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; Collection results = GetContentReader(paths, context); context.ThrowFirstErrorOrDoNothing(); return results; } // GetContentReader /// /// Gets the content reader for the specified item. /// /// /// /// The path(s) to the item(s) to get the content reader from. /// /// /// /// The context which the core command is running. /// /// /// /// The content readers for all items that the path resolves to. /// /// /// /// 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 Collection GetContentReader( string[] paths, CmdletProviderContext context) { if (paths == null) { throw PSTraceSource.NewArgumentNullException("paths"); } ProviderInfo provider = null; CmdletProvider providerInstance = null; Collection results = new Collection(); 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) { IContentReader reader = GetContentReaderPrivate(providerInstance, providerPath, context); if (reader != null) { results.Add(reader); } context.ThrowFirstErrorOrDoNothing(true); } } return results; } // GetContentReader /// /// Gets the content reader for 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 IContentReader GetContentReaderPrivate( 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"); IContentReader result = null; try { result = providerInstance.GetContentReader(path, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { CommandProcessorBase.CheckForSevereException(e); throw NewProviderInvocationException( "GetContentReaderProviderException", SessionStateStrings.GetContentReaderProviderException, providerInstance.ProviderInfo, path, e); } return result; } // GetContentReaderPrivate /// /// Gets the dynamic parameters for the get-content 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 GetContentReaderDynamicParameters( 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 GetContentReaderDynamicParameters(providerInstance, providerPaths[0], newContext); } return null; } // GetContentReaderDynamicParameters /// /// Gets the dynamic parameters for the get-content 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 GetContentReaderDynamicParameters( 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"); object result = null; try { result = providerInstance.GetContentReaderDynamicParameters(path, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { CommandProcessorBase.CheckForSevereException(e); throw NewProviderInvocationException( "GetContentReaderDynamicParametersProviderException", SessionStateStrings.GetContentReaderDynamicParametersProviderException, providerInstance.ProviderInfo, path, e); } return result; } // GetContentReaderDynamicParameters #endregion GetContentReader #region GetContentWriter /// /// Gets the content writer for the specified item. /// /// /// /// The path(s) to the item(s) to get the content writer for. /// /// /// /// Passed on to providers to force operations. /// /// /// /// If true, globbing is not done on paths. /// /// /// /// The content writers for all items that the path resolves to. /// /// /// /// 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 GetContentWriter(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; Collection results = GetContentWriter(paths, context); context.ThrowFirstErrorOrDoNothing(); return results; } // GetContentWriter /// /// Gets the content writer for the specified item. /// /// /// /// The path(s) to the item(s) to get the content writer from. /// /// /// /// The context which the core command is running. /// /// /// /// The content writers for all items that the path resolves to. /// /// /// /// 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 Collection GetContentWriter( string[] paths, CmdletProviderContext context) { if (paths == null) { throw PSTraceSource.NewArgumentNullException("paths"); } ProviderInfo provider = null; CmdletProvider providerInstance = null; Collection results = new Collection(); foreach (string path in paths) { if (path == null) { throw PSTraceSource.NewArgumentNullException("paths"); } Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, true, context, out provider, out providerInstance); foreach (string providerPath in providerPaths) { IContentWriter result = GetContentWriterPrivate(providerInstance, providerPath, context); if (result != null) { results.Add(result); } } } return results; } // GetContentWriter /// /// Gets the content writer for 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 IContentWriter GetContentWriterPrivate( 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"); IContentWriter result = null; try { result = providerInstance.GetContentWriter(path, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { CommandProcessorBase.CheckForSevereException(e); throw NewProviderInvocationException( "GetContentWriterProviderException", SessionStateStrings.GetContentWriterProviderException, providerInstance.ProviderInfo, path, e); } return result; } // GetContentWriterPrivate /// /// Gets the dynamic parameters for the set-content and add-content 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 GetContentWriterDynamicParameters( 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 GetContentWriterDynamicParameters(providerInstance, providerPaths[0], newContext); } return null; } // GetContentWriterDynamicParameters /// /// Gets the dynamic parameters for the set-content and add-content 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 GetContentWriterDynamicParameters( 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"); object result = null; try { result = providerInstance.GetContentWriterDynamicParameters(path, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { CommandProcessorBase.CheckForSevereException(e); throw NewProviderInvocationException ( "GetContentWriterDynamicParametersProviderException", SessionStateStrings.GetContentWriterDynamicParametersProviderException, providerInstance.ProviderInfo, path, e); } return result; } // GetContentWriterDynamicParameters #endregion GetContentWriter #region ClearContent /// /// Clears all the content from the specified item. /// /// /// /// The path(s) to the item(s) to clear the content from. /// /// /// /// Passed on to providers to force operations. /// /// /// /// If true, globbing is not done on paths. /// /// /// /// 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 ClearContent(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; ClearContent(paths, context); context.ThrowFirstErrorOrDoNothing(); } // ClearContent /// /// Clears all of the content from the specified item. /// /// /// /// The path to the item to clear the content from. /// /// /// /// 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 ClearContent( 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) { PSTraceSource.NewArgumentNullException("paths"); } Collection providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, false, context, out provider, out providerInstance); foreach (string providerPath in providerPaths) { ClearContentPrivate(providerInstance, providerPath, context); } } } // ClearContent /// /// Clears the content from 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 ClearContentPrivate( 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"); try { providerInstance.ClearContent(path, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { CommandProcessorBase.CheckForSevereException(e); throw NewProviderInvocationException( "ClearContentProviderException", SessionStateStrings.ClearContentProviderException, providerInstance.ProviderInfo, path, e); } } // ClearContentPrivate /// /// Gets the dynamic parameters for the clear-content 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 ClearContentDynamicParameters( 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 ClearContentDynamicParameters(providerInstance, providerPaths[0], newContext); } return null; } // ClearContentDynamicParameters /// /// Calls the provider to get the clear-content dynamic parameters /// /// /// /// The instance of the provider to call /// /// /// /// The path to pass to the provider. /// /// /// /// The context the command is executing under. /// /// /// /// The dynamic parameter object returned by the provider. /// /// /// /// 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 ClearContentDynamicParameters( 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"); object result = null; try { result = providerInstance.ClearContentDynamicParameters(path, context); } catch (NotSupportedException) { throw; } catch (LoopFlowException) { throw; } catch (PipelineStoppedException) { throw; } catch (ActionPreferenceStopException) { throw; } catch (Exception e) // Catch-all OK, 3rd party callout. { CommandProcessorBase.CheckForSevereException(e); throw NewProviderInvocationException( "ClearContentDynamicParametersProviderException", SessionStateStrings.ClearContentDynamicParametersProviderException, providerInstance.ProviderInfo, path, e); } return result; } // ClearContentDynamicParameters #endregion ClearContent #endregion IContentCmdletProvider accessors } // SessionStateInternal class } #pragma warning restore 56500