Team Ai
Datasetpublic

MegaBites-AI/Windows-powershell

sourceHugging Facemitupdated 6mo agoView on Hugging Face
0likes308downloads
ContentCmdletProviderInterfaces.cs595 linesDownload Raw Back to engine
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections.ObjectModel;5using System.Management.Automation.Provider;6 7using Dbg = System.Management.Automation;8 9namespace System.Management.Automation10{11    /// <summary>12    /// Exposes the Content nouns to the Cmdlet Providers to the Cmdlet base class. The methods of this class13    /// use the providers to perform operations.14    /// </summary>15    public sealed class ContentCmdletProviderIntrinsics16    {17        #region Constructors18 19        /// <summary>20        /// Hide the default constructor since we always require an instance of SessionState.21        /// </summary>22        private ContentCmdletProviderIntrinsics()23        {24            Dbg.Diagnostics.Assert(25                false,26                "This constructor should never be called. Only the constructor that takes an instance of SessionState should be called.");27        }28 29        /// <summary>30        /// Constructs a facade over the "real" session state API.31        /// </summary>32        /// <param name="cmdlet">33        /// An instance of the cmdlet.34        /// </param>35        /// <exception cref="ArgumentNullException">36        /// If <paramref name="cmdlet"/> is null.37        /// </exception>38        internal ContentCmdletProviderIntrinsics(Cmdlet cmdlet)39        {40            if (cmdlet == null)41            {42                throw PSTraceSource.NewArgumentNullException(nameof(cmdlet));43            }44 45            _cmdlet = cmdlet;46            _sessionState = cmdlet.Context.EngineSessionState;47        }48 49        /// <summary>50        /// Constructs a facade over the "real" session state API.51        /// </summary>52        /// <param name="sessionState">53        /// An instance of the sessionState.54        /// </param>55        /// <exception cref="ArgumentNullException">56        /// If <paramref name="sessionState"/> is null.57        /// </exception>58        internal ContentCmdletProviderIntrinsics(SessionStateInternal sessionState)59        {60            if (sessionState == null)61            {62                throw PSTraceSource.NewArgumentNullException(nameof(sessionState));63            }64 65            _sessionState = sessionState;66        }67 68        #endregion Constructors69 70        #region Public methods71 72        #region GetContentReader73 74        /// <summary>75        /// Gets the content reader for the item at the specified path.76        /// </summary>77        /// <param name="path">78        /// The path to the item to get the content reader for.79        /// </param>80        /// <returns>81        /// The IContentReader for the item(s) at the specified path.82        /// </returns>83        /// <exception cref="ArgumentNullException">84        /// If <paramref name="path"/> is null.85        /// </exception>86        /// <exception cref="ProviderNotFoundException">87        /// If the <paramref name="path"/> refers to a provider that could not be found.88        /// </exception>89        /// <exception cref="DriveNotFoundException">90        /// If the <paramref name="path"/> refers to a drive that could not be found.91        /// </exception>92        /// <exception cref="ItemNotFoundException">93        /// If <paramref name="path"/> does not contain glob characters and94        /// could not be found.95        /// </exception>96        /// <exception cref="NotSupportedException">97        /// If the provider that the <paramref name="path"/> refers to does98        /// not support this operation.99        /// </exception>100        /// <exception cref="ProviderInvocationException">101        /// If the provider threw an exception.102        /// </exception>103        public Collection<IContentReader> GetReader(string path)104        {105            Dbg.Diagnostics.Assert(106                _sessionState != null,107                "The only constructor for this class should always set the sessionState field");108 109            // Parameter validation is done in the session state object110 111            return _sessionState.GetContentReader(new string[] { path }, false, false);112        }113 114        /// <summary>115        /// Gets the content reader for the item at the specified path.116        /// </summary>117        /// <param name="path">118        /// The path(s) to the item(s) to get the content reader for.119        /// </param>120        /// <param name="force">121        /// Passed on to providers to force operations.122        /// </param>123        /// <param name="literalPath">124        /// If true, globbing is not done on paths.125        /// </param>126        /// <returns>127        /// The IContentReader for the item(s) at the specified path.128        /// </returns>129        /// <exception cref="ArgumentNullException">130        /// If <paramref name="path"/> is null.131        /// </exception>132        /// <exception cref="ProviderNotFoundException">133        /// If the <paramref name="path"/> refers to a provider that could not be found.134        /// </exception>135        /// <exception cref="DriveNotFoundException">136        /// If the <paramref name="path"/> refers to a drive that could not be found.137        /// </exception>138        /// <exception cref="ItemNotFoundException">139        /// If <paramref name="path"/> does not contain glob characters and140        /// could not be found.141        /// </exception>142        /// <exception cref="NotSupportedException">143        /// If the provider that the <paramref name="path"/> refers to does144        /// not support this operation.145        /// </exception>146        /// <exception cref="ProviderInvocationException">147        /// If the provider threw an exception.148        /// </exception>149        public Collection<IContentReader> GetReader(string[] path, bool force, bool literalPath)150        {151            Dbg.Diagnostics.Assert(152                _sessionState != null,153                "The only constructor for this class should always set the sessionState field");154 155            // Parameter validation is done in the session state object156 157            return _sessionState.GetContentReader(path, force, literalPath);158        }159 160        /// <summary>161        /// </summary>162        /// <param name="path">163        /// </param>164        /// <param name="context">165        /// </param>166        /// <returns>167        /// </returns>168        /// <exception cref="ArgumentNullException">169        /// If <paramref name="path"/> is null.170        /// </exception>171        /// <exception cref="ProviderNotFoundException">172        /// If the <paramref name="path"/> refers to a provider that could not be found.173        /// </exception>174        /// <exception cref="DriveNotFoundException">175        /// If the <paramref name="path"/> refers to a drive that could not be found.176        /// </exception>177        /// <exception cref="ItemNotFoundException">178        /// If <paramref name="path"/> does not contain glob characters and179        /// could not be found.180        /// </exception>181        /// <exception cref="NotSupportedException">182        /// If the provider that the <paramref name="path"/> refers to does183        /// not support this operation.184        /// </exception>185        /// <exception cref="ProviderInvocationException">186        /// If the provider threw an exception.187        /// </exception>188        internal Collection<IContentReader> GetReader(189            string path,190            CmdletProviderContext context)191        {192            Dbg.Diagnostics.Assert(193                _sessionState != null,194                "The only constructor for this class should always set the sessionState field");195 196            // Parameter validation is done in the session state object197 198            return _sessionState.GetContentReader(new string[] { path }, context);199        }200 201        /// <summary>202        /// Gets the dynamic parameters for the get-content cmdlet.203        /// </summary>204        /// <param name="path">205        /// The path to the item if it was specified on the command line.206        /// </param>207        /// <param name="context">208        /// The context which the core command is running.209        /// </param>210        /// <returns>211        /// An object that has properties and fields decorated with212        /// parsing attributes similar to a cmdlet class.213        /// </returns>214        /// <exception cref="ProviderNotFoundException">215        /// If the <paramref name="path"/> refers to a provider that could not be found.216        /// </exception>217        /// <exception cref="DriveNotFoundException">218        /// If the <paramref name="path"/> refers to a drive that could not be found.219        /// </exception>220        /// <exception cref="ItemNotFoundException">221        /// If <paramref name="path"/> does not contain glob characters and222        /// could not be found.223        /// </exception>224        /// <exception cref="NotSupportedException">225        /// If the provider that the <paramref name="path"/> refers to does226        /// not support this operation.227        /// </exception>228        /// <exception cref="ProviderInvocationException">229        /// If the provider threw an exception.230        /// </exception>231        internal object GetContentReaderDynamicParameters(232            string path,233            CmdletProviderContext context)234        {235            Dbg.Diagnostics.Assert(236                _sessionState != null,237                "The only constructor for this class should always set the sessionState field");238 239            // Parameter validation is done in the session state object240 241            return _sessionState.GetContentReaderDynamicParameters(path, context);242        }243 244        #endregion GetContentReader245 246        #region GetContentWriter247 248        /// <summary>249        /// Gets the content writer for the item(s) at the specified path.250        /// </summary>251        /// <param name="path">252        /// The path to the item(s) to get the content writer for.253        /// </param>254        /// <returns>255        /// The IContentWriter for the item(s) at the specified path.256        /// </returns>257        /// <exception cref="ArgumentNullException">258        /// If <paramref name="path"/> is null.259        /// </exception>260        /// <exception cref="ProviderNotFoundException">261        /// If the <paramref name="path"/> refers to a provider that could not be found.262        /// </exception>263        /// <exception cref="DriveNotFoundException">264        /// If the <paramref name="path"/> refers to a drive that could not be found.265        /// </exception>266        /// <exception cref="ItemNotFoundException">267        /// If <paramref name="path"/> does not contain glob characters and268        /// could not be found.269        /// </exception>270        /// <exception cref="NotSupportedException">271        /// If the provider that the <paramref name="path"/> refers to does272        /// not support this operation.273        /// </exception>274        /// <exception cref="ProviderInvocationException">275        /// If the provider threw an exception.276        /// </exception>277        public Collection<IContentWriter> GetWriter(string path)278        {279            Dbg.Diagnostics.Assert(280                _sessionState != null,281                "The only constructor for this class should always set the sessionState field");282 283            // Parameter validation is done in the session state object284 285            return _sessionState.GetContentWriter(new string[] { path }, false, false);286        }287 288        /// <summary>289        /// Gets the content writer for the item(s) at the specified path.290        /// </summary>291        /// <param name="path">292        /// The path(s) to the item(s) to get the content writer for.293        /// </param>294        /// <param name="force">295        /// Passed on to providers to force operations.296        /// </param>297        /// <param name="literalPath">298        /// If true, globbing is not done on paths.299        /// </param>300        /// <returns>301        /// The IContentWriter for the item(s) at the specified path.302        /// </returns>303        /// <exception cref="ArgumentNullException">304        /// If <paramref name="path"/> is null.305        /// </exception>306        /// <exception cref="ProviderNotFoundException">307        /// If the <paramref name="path"/> refers to a provider that could not be found.308        /// </exception>309        /// <exception cref="DriveNotFoundException">310        /// If the <paramref name="path"/> refers to a drive that could not be found.311        /// </exception>312        /// <exception cref="ItemNotFoundException">313        /// If <paramref name="path"/> does not contain glob characters and314        /// could not be found.315        /// </exception>316        /// <exception cref="NotSupportedException">317        /// If the provider that the <paramref name="path"/> refers to does318        /// not support this operation.319        /// </exception>320        /// <exception cref="ProviderInvocationException">321        /// If the provider threw an exception.322        /// </exception>323        public Collection<IContentWriter> GetWriter(string[] path, bool force, bool literalPath)324        {325            Dbg.Diagnostics.Assert(326                _sessionState != null,327                "The only constructor for this class should always set the sessionState field");328 329            // Parameter validation is done in the session state object330 331            return _sessionState.GetContentWriter(path, force, literalPath);332        }333 334        /// <summary>335        /// </summary>336        /// <param name="path">337        /// </param>338        /// <param name="context">339        /// </param>340        /// <returns>341        /// </returns>342        /// <exception cref="ArgumentNullException">343        /// If <paramref name="path"/> is null.344        /// </exception>345        /// <exception cref="ProviderNotFoundException">346        /// If the <paramref name="path"/> refers to a provider that could not be found.347        /// </exception>348        /// <exception cref="DriveNotFoundException">349        /// If the <paramref name="path"/> refers to a drive that could not be found.350        /// </exception>351        /// <exception cref="ItemNotFoundException">352        /// If <paramref name="path"/> does not contain glob characters and353        /// could not be found.354        /// </exception>355        /// <exception cref="NotSupportedException">356        /// If the provider that the <paramref name="path"/> refers to does357        /// not support this operation.358        /// </exception>359        /// <exception cref="ProviderInvocationException">360        /// If the provider threw an exception.361        /// </exception>362        internal Collection<IContentWriter> GetWriter(363            string path,364            CmdletProviderContext context)365        {366            Dbg.Diagnostics.Assert(367                _sessionState != null,368                "The only constructor for this class should always set the sessionState field");369 370            // Parameter validation is done in the session state object371 372            return _sessionState.GetContentWriter(new string[] { path }, context);373        }374 375        /// <summary>376        /// Gets the dynamic parameters for the set-content and add-content cmdlet.377        /// </summary>378        /// <param name="path">379        /// The path to the item if it was specified on the command line.380        /// </param>381        /// <param name="context">382        /// The context which the core command is running.383        /// </param>384        /// <returns>385        /// An object that has properties and fields decorated with386        /// parsing attributes similar to a cmdlet class.387        /// </returns>388        /// <exception cref="ProviderNotFoundException">389        /// If the <paramref name="path"/> refers to a provider that could not be found.390        /// </exception>391        /// <exception cref="DriveNotFoundException">392        /// If the <paramref name="path"/> refers to a drive that could not be found.393        /// </exception>394        /// <exception cref="ItemNotFoundException">395        /// If <paramref name="path"/> does not contain glob characters and396        /// could not be found.397        /// </exception>398        /// <exception cref="NotSupportedException">399        /// If the provider that the <paramref name="path"/> refers to does400        /// not support this operation.401        /// </exception>402        /// <exception cref="ProviderInvocationException">403        /// If the provider threw an exception.404        /// </exception>405        internal object GetContentWriterDynamicParameters(406            string path,407            CmdletProviderContext context)408        {409            Dbg.Diagnostics.Assert(410                _sessionState != null,411                "The only constructor for this class should always set the sessionState field");412 413            // Parameter validation is done in the session state object414 415            return _sessionState.GetContentWriterDynamicParameters(path, context);416        }417 418        #endregion GetContentWriter419 420        #region ClearContent421 422        /// <summary>423        /// Clears the content from the item(s) specified by the path.424        /// </summary>425        /// <param name="path">426        /// The path to the item(s) to clear the content from.427        /// </param>428        /// <exception cref="ArgumentNullException">429        /// If <paramref name="path"/> is null.430        /// </exception>431        /// <exception cref="ProviderNotFoundException">432        /// If the <paramref name="path"/> refers to a provider that could not be found.433        /// </exception>434        /// <exception cref="DriveNotFoundException">435        /// If the <paramref name="path"/> refers to a drive that could not be found.436        /// </exception>437        /// <exception cref="ItemNotFoundException">438        /// If <paramref name="path"/> does not contain glob characters and439        /// could not be found.440        /// </exception>441        /// <exception cref="NotSupportedException">442        /// If the provider that the <paramref name="path"/> refers to does443        /// not support this operation.444        /// </exception>445        /// <exception cref="ProviderInvocationException">446        /// If the provider threw an exception.447        /// </exception>448        public void Clear(string path)449        {450            Dbg.Diagnostics.Assert(451                _sessionState != null,452                "The only constructor for this class should always set the sessionState field");453 454            // Parameter validation is done in the session state object455 456            _sessionState.ClearContent(new string[] { path }, false, false);457        }458 459        /// <summary>460        /// Clears the content from the item(s) specified by the path.461        /// </summary>462        /// <param name="path">463        /// The path(s) to the item(s) to clear the content from.464        /// </param>465        /// <param name="force">466        /// Passed on to providers to force operations.467        /// </param>468        /// <param name="literalPath">469        /// If true, globbing is not done on paths.470        /// </param>471        /// <exception cref="ArgumentNullException">472        /// If <paramref name="path"/> is null.473        /// </exception>474        /// <exception cref="ProviderNotFoundException">475        /// If the <paramref name="path"/> refers to a provider that could not be found.476        /// </exception>477        /// <exception cref="DriveNotFoundException">478        /// If the <paramref name="path"/> refers to a drive that could not be found.479        /// </exception>480        /// <exception cref="ItemNotFoundException">481        /// If <paramref name="path"/> does not contain glob characters and482        /// could not be found.483        /// </exception>484        /// <exception cref="NotSupportedException">485        /// If the provider that the <paramref name="path"/> refers to does486        /// not support this operation.487        /// </exception>488        /// <exception cref="ProviderInvocationException">489        /// If the provider threw an exception.490        /// </exception>491        public void Clear(string[] path, bool force, bool literalPath)492        {493            Dbg.Diagnostics.Assert(494                _sessionState != null,495                "The only constructor for this class should always set the sessionState field");496 497            // Parameter validation is done in the session state object498 499            _sessionState.ClearContent(path, force, literalPath);500        }501 502        /// <summary>503        /// Clears the content from the specified item(s)504        /// </summary>505        /// <param name="path">506        /// The path to the item(s) to clear the content from.507        /// </param>508        /// <param name="context">509        /// The context under which the command is running.510        /// </param>511        /// <exception cref="ArgumentNullException">512        /// If <paramref name="path"/> is null.513        /// </exception>514        /// <exception cref="ProviderNotFoundException">515        /// If the <paramref name="path"/> refers to a provider that could not be found.516        /// </exception>517        /// <exception cref="DriveNotFoundException">518        /// If the <paramref name="path"/> refers to a drive that could not be found.519        /// </exception>520        /// <exception cref="ItemNotFoundException">521        /// If <paramref name="path"/> does not contain glob characters and522        /// could not be found.523        /// </exception>524        /// <exception cref="NotSupportedException">525        /// If the provider that the <paramref name="path"/> refers to does526        /// not support this operation.527        /// </exception>528        /// <exception cref="ProviderInvocationException">529        /// If the provider threw an exception.530        /// </exception>531        internal void Clear(string path, CmdletProviderContext context)532        {533            Dbg.Diagnostics.Assert(534                _sessionState != null,535                "The only constructor for this class should always set the sessionState field");536 537            // Parameter validation is done in the session state object538 539            _sessionState.ClearContent(new string[] { path }, context);540        }541 542        /// <summary>543        /// Gets the dynamic parameters for the clear-content cmdlet.544        /// </summary>545        /// <param name="path">546        /// The path to the item if it was specified on the command line.547        /// </param>548        /// <param name="context">549        /// The context which the core command is running.550        /// </param>551        /// <returns>552        /// An object that has properties and fields decorated with553        /// parsing attributes similar to a cmdlet class.554        /// </returns>555        /// <exception cref="ProviderNotFoundException">556        /// If the <paramref name="path"/> refers to a provider that could not be found.557        /// </exception>558        /// <exception cref="DriveNotFoundException">559        /// If the <paramref name="path"/> refers to a drive that could not be found.560        /// </exception>561        /// <exception cref="ItemNotFoundException">562        /// If <paramref name="path"/> does not contain glob characters and563        /// could not be found.564        /// </exception>565        /// <exception cref="NotSupportedException">566        /// If the provider that the <paramref name="path"/> refers to does567        /// not support this operation.568        /// </exception>569        /// <exception cref="ProviderInvocationException">570        /// If the provider threw an exception.571        /// </exception>572        internal object ClearContentDynamicParameters(string path, CmdletProviderContext context)573        {574            Dbg.Diagnostics.Assert(575                _sessionState != null,576                "The only constructor for this class should always set the sessionState field");577 578            // Parameter validation is done in the session state object579 580            return _sessionState.ClearContentDynamicParameters(path, context);581        }582 583        #endregion ClearContent584 585        #endregion Public methods586 587        #region private data588 589        private readonly Cmdlet _cmdlet;590        private readonly SessionStateInternal _sessionState;591 592        #endregion private data593    }594}595