MegaBites-AI/Windows-powershell
0308
1// Copyright (c) Microsoft Corporation.2// Licensed under the MIT License.3 4using System.Collections;5using System.Collections.ObjectModel;6using System.IO;7using System.Management.Automation.Internal;8using System.Management.Automation.Provider;9using System.Management.Automation.Runspaces;10using System.Reflection;11 12using Dbg = System.Management.Automation;13 14#pragma warning disable 1634, 1691 // Stops compiler from warning about unknown warnings15#pragma warning disable 5650016 17namespace System.Management.Automation18{19 /// <summary>20 /// Holds the state of a Monad Shell session.21 /// </summary>22 internal sealed partial class SessionStateInternal23 {24 #region ContainerCmdletProvider accessors25 26 #region ItemExists27 28 /// <summary>29 /// Determines if the monad virtual namespace path exists.30 /// </summary>31 /// <param name="path">32 /// The path to the object to determine if it exists.33 /// </param>34 /// <param name="force">35 /// Passed on to providers to force operations.36 /// </param>37 /// <param name="literalPath">38 /// If true, globbing is not done on paths.39 /// </param>40 /// <returns>41 /// true if the object specified by path exists, false otherwise.42 /// </returns>43 /// <exception cref="ArgumentNullException">44 /// If <paramref name="path"/> is null.45 /// </exception>46 /// <exception cref="ProviderNotFoundException">47 /// If the <paramref name="path"/> refers to a provider that could not be found.48 /// </exception>49 /// <exception cref="DriveNotFoundException">50 /// If the <paramref name="path"/> refers to a drive that could not be found.51 /// </exception>52 /// <exception cref="NotSupportedException">53 /// If the provider that the <paramref name="path"/> refers to does54 /// not support this operation.55 /// </exception>56 /// <exception cref="ProviderInvocationException">57 /// If the provider threw an exception.58 /// </exception>59 internal bool ItemExists(string path, bool force, bool literalPath)60 {61 if (path == null)62 {63 throw PSTraceSource.NewArgumentNullException(nameof(path));64 }65 66 CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext);67 context.Force = force;68 context.SuppressWildcardExpansion = literalPath;69 70 bool result = ItemExists(path, context);71 72 context.ThrowFirstErrorOrDoNothing();73 74 return result;75 }76 77 /// <summary>78 /// Determines if the monad virtual namespace path exists.79 /// </summary>80 /// <param name="path">81 /// The path to the object to determine if it exists.82 /// </param>83 /// <param name="context">84 /// The context which the core command is running.85 /// </param>86 /// <returns>87 /// true if the object specified by path exists, false otherwise.88 /// </returns>89 /// <exception cref="ArgumentNullException">90 /// If <paramref name="path"/> is null.91 /// </exception>92 /// <exception cref="ProviderNotFoundException">93 /// If the <paramref name="path"/> refers to a provider that could not be found.94 /// </exception>95 /// <exception cref="DriveNotFoundException">96 /// If the <paramref name="path"/> refers to a drive that could not be found.97 /// </exception>98 /// <exception cref="NotSupportedException">99 /// If the provider that the <paramref name="path"/> refers to does100 /// not support this operation.101 /// </exception>102 /// <exception cref="ProviderInvocationException">103 /// If the provider threw an exception.104 /// </exception>105 internal bool ItemExists(106 string path,107 CmdletProviderContext context)108 {109 if (path == null)110 {111 throw PSTraceSource.NewArgumentNullException(nameof(path));112 }113 114 ProviderInfo provider = null;115 CmdletProvider providerInstance = null;116 117 bool result = false;118 try119 {120 Collection<string> providerPaths =121 Globber.GetGlobbedProviderPathsFromMonadPath(122 path,123 true,124 context,125 out provider,126 out providerInstance);127 128 foreach (string providerPath in providerPaths)129 {130 result = ItemExists(providerInstance, providerPath, context);131 if (result)132 {133 break;134 }135 }136 }137 catch (ItemNotFoundException)138 {139 result = false;140 }141 142 return result;143 }144 145 /// <summary>146 /// Determines if the item at the specified path exists.147 /// </summary>148 /// <param name="providerInstance">149 /// The provider instance to use.150 /// </param>151 /// <param name="path">152 /// The path to the item if it was specified on the command line.153 /// </param>154 /// <param name="context">155 /// The context which the core command is running.156 /// </param>157 /// <exception cref="NotSupportedException">158 /// If the <paramref name="providerInstance"/> does not support this operation.159 /// </exception>160 /// <exception cref="PipelineStoppedException">161 /// If the pipeline is being stopped while executing the command.162 /// </exception>163 /// <exception cref="ProviderInvocationException">164 /// If the provider threw an exception.165 /// </exception>166 internal bool ItemExists(167 CmdletProvider providerInstance,168 string path,169 CmdletProviderContext context)170 {171 // All parameters should have been validated by caller172 Dbg.Diagnostics.Assert(173 providerInstance != null,174 "Caller should validate providerId before calling this method");175 176 Dbg.Diagnostics.Assert(177 path != null,178 "Caller should validate path before calling this method");179 180 Dbg.Diagnostics.Assert(181 context != null,182 "Caller should validate context before calling this method");183 184 ItemCmdletProvider itemCmdletProvider =185 GetItemProviderInstance(providerInstance);186 187 bool result = false;188 189 try190 {191 result = itemCmdletProvider.ItemExists(path, context);192 }193 catch (LoopFlowException)194 {195 throw;196 }197 catch (PipelineStoppedException)198 {199 throw;200 }201 catch (ActionPreferenceStopException)202 {203 throw;204 }205 catch (Exception e) // Catch-all OK, 3rd party callout.206 {207 throw NewProviderInvocationException(208 "ItemExistsProviderException",209 SessionStateStrings.ItemExistsProviderException,210 itemCmdletProvider.ProviderInfo,211 path,212 e);213 }214 215 return result;216 }217 218 /// <summary>219 /// Gets the dynamic parameters for the test-path cmdlet.220 /// </summary>221 /// <param name="path">222 /// The path to the item if it was specified on the command line.223 /// </param>224 /// <param name="context">225 /// The context which the core command is running.226 /// </param>227 /// <returns>228 /// An object that has properties and fields decorated with229 /// parsing attributes similar to a cmdlet class.230 /// </returns>231 /// <exception cref="ArgumentNullException">232 /// If <paramref name="path"/> is null.233 /// </exception>234 /// <exception cref="ProviderNotFoundException">235 /// If the <paramref name="path"/> refers to a provider that could not be found.236 /// </exception>237 /// <exception cref="DriveNotFoundException">238 /// If the <paramref name="path"/> refers to a drive that could not be found.239 /// </exception>240 /// <exception cref="NotSupportedException">241 /// If the provider that the <paramref name="path"/> refers to does242 /// not support this operation.243 /// </exception>244 /// <exception cref="ProviderInvocationException">245 /// If the provider threw an exception.246 /// </exception>247 /// <exception cref="ItemNotFoundException">248 /// If <paramref name="path"/> does not contain glob characters and249 /// could not be found.250 /// </exception>251 internal object ItemExistsDynamicParameters(string path, CmdletProviderContext context)252 {253 if (path == null)254 {255 throw PSTraceSource.NewArgumentNullException(nameof(path));256 }257 258 ProviderInfo provider = null;259 CmdletProvider providerInstance = null;260 261 CmdletProviderContext newContext =262 new CmdletProviderContext(context);263 newContext.SetFilters(264 new Collection<string>(),265 new Collection<string>(),266 null);267 268 Collection<string> providerPaths =269 Globber.GetGlobbedProviderPathsFromMonadPath(270 path,271 true,272 newContext,273 out provider,274 out providerInstance);275 276 if (providerPaths.Count > 0)277 {278 // Get the dynamic parameters for the first resolved path279 280 return ItemExistsDynamicParameters(providerInstance, providerPaths[0], newContext);281 }282 283 return null;284 }285 286 /// <summary>287 /// Gets the dynamic parameters for the test-path cmdlet.288 /// </summary>289 /// <param name="path">290 /// The path to the item if it was specified on the command line.291 /// </param>292 /// <param name="providerInstance">293 /// The instance of the provider to use.294 /// </param>295 /// <param name="context">296 /// The context which the core command is running.297 /// </param>298 /// <returns>299 /// An object that has properties and fields decorated with300 /// parsing attributes similar to a cmdlet class.301 /// </returns>302 /// <exception cref="NotSupportedException">303 /// If the <paramref name="providerInstance"/> does not support this operation.304 /// </exception>305 /// <exception cref="PipelineStoppedException">306 /// If the pipeline is being stopped while executing the command.307 /// </exception>308 /// <exception cref="ProviderInvocationException">309 /// If the provider threw an exception.310 /// </exception>311 private object ItemExistsDynamicParameters(312 CmdletProvider providerInstance,313 string path,314 CmdletProviderContext context)315 {316 // All parameters should have been validated by caller317 Dbg.Diagnostics.Assert(318 providerInstance != null,319 "Caller should validate providerInstance before calling this method");320 321 Dbg.Diagnostics.Assert(322 path != null,323 "Caller should validate path before calling this method");324 325 Dbg.Diagnostics.Assert(326 context != null,327 "Caller should validate context before calling this method");328 329 ContainerCmdletProvider containerCmdletProvider =330 GetContainerProviderInstance(providerInstance);331 332 object result = null;333 try334 {335 result = containerCmdletProvider.ItemExistsDynamicParameters(path, context);336 }337 catch (LoopFlowException)338 {339 throw;340 }341 catch (PipelineStoppedException)342 {343 throw;344 }345 catch (ActionPreferenceStopException)346 {347 throw;348 }349 catch (Exception e) // Catch-all OK, 3rd party callout.350 {351 throw NewProviderInvocationException(352 "ItemExistsDynamicParametersProviderException",353 SessionStateStrings.ItemExistsDynamicParametersProviderException,354 containerCmdletProvider.ProviderInfo,355 path,356 e);357 }358 359 return result;360 }361 362 #endregion Exists363 364 #region IsValidPath365 366 /// <summary>367 /// Determines if the MSH path is a syntactically and semantically valid path for the provider.368 /// </summary>369 /// <param name="path">370 /// The path to validate.371 /// </param>372 /// <returns>373 /// true if the object specified by path is syntactically and semantically valid, false otherwise.374 /// </returns>375 /// <exception cref="ArgumentNullException">376 /// If <paramref name="path"/> is null.377 /// </exception>378 /// <exception cref="ProviderNotFoundException">379 /// If the <paramref name="path"/> refers to a provider that could not be found.380 /// </exception>381 /// <exception cref="DriveNotFoundException">382 /// If the <paramref name="path"/> refers to a drive that could not be found.383 /// </exception>384 /// <exception cref="NotSupportedException">385 /// If the provider that the <paramref name="path"/> refers to does386 /// not support this operation.387 /// </exception>388 /// <exception cref="ProviderInvocationException">389 /// If the provider threw an exception.390 /// </exception>391 internal bool IsValidPath(string path)392 {393 if (path == null)394 {395 throw PSTraceSource.NewArgumentNullException(nameof(path));396 }397 398 CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext);399 400 bool result = IsValidPath(path, context);401 402 context.ThrowFirstErrorOrDoNothing();403 404 return result;405 }406 407 /// <summary>408 /// Determines if the MSH path is a syntactically and semantically valid path for the provider.409 /// </summary>410 /// <param name="path">411 /// The path to validate.412 /// </param>413 /// <param name="context">414 /// The context which the core command is running.415 /// </param>416 /// <returns>417 /// true if the object specified by path is syntactically and semantically valid, false otherwise.418 /// </returns>419 /// <exception cref="ArgumentNullException">420 /// If <paramref name="path"/> is null.421 /// </exception>422 /// <exception cref="ProviderNotFoundException">423 /// If the <paramref name="path"/> refers to a provider that could not be found.424 /// </exception>425 /// <exception cref="DriveNotFoundException">426 /// If the <paramref name="path"/> refers to a drive that could not be found.427 /// </exception>428 /// <exception cref="NotSupportedException">429 /// If the provider that the <paramref name="path"/> refers to does430 /// not support this operation.431 /// </exception>432 /// <exception cref="ProviderInvocationException">433 /// If the provider threw an exception.434 /// </exception>435 internal bool IsValidPath(436 string path,437 CmdletProviderContext context)438 {439 if (path == null)440 {441 throw PSTraceSource.NewArgumentNullException(nameof(path));442 }443 444 ProviderInfo provider = null;445 PSDriveInfo driveInfo = null;446 447 string providerPath =448 Globber.GetProviderPath(449 path,450 context,451 out provider,452 out driveInfo);453 454 ItemCmdletProvider providerInstance = GetItemProviderInstance(provider);455 456 return IsValidPath(providerInstance, providerPath, context);457 }458 459 /// <summary>460 /// Determines if the specified path is valid.461 /// </summary>462 /// <param name="providerInstance">463 /// The provider instance to use.464 /// </param>465 /// <param name="path">466 /// The path to the item if it was specified on the command line.467 /// </param>468 /// <param name="context">469 /// The context which the core command is running.470 /// </param>471 /// <exception cref="NotSupportedException">472 /// If the <paramref name="providerInstance"/> does not support this operation.473 /// </exception>474 /// <exception cref="PipelineStoppedException">475 /// If the pipeline is being stopped while executing the command.476 /// </exception>477 /// <exception cref="ProviderInvocationException">478 /// If the provider threw an exception.479 /// </exception>480 private bool IsValidPath(481 CmdletProvider providerInstance,482 string path,483 CmdletProviderContext context)484 {485 // All parameters should have been validated by caller486 Dbg.Diagnostics.Assert(487 providerInstance != null,488 "Caller should validate providerInstance before calling this method");489 490 Dbg.Diagnostics.Assert(491 path != null,492 "Caller should validate path before calling this method");493 494 Dbg.Diagnostics.Assert(495 context != null,496 "Caller should validate context before calling this method");497 498 ItemCmdletProvider itemCmdletProvider =499 GetItemProviderInstance(providerInstance);500 501 bool result = false;502 503 try504 {505 result = itemCmdletProvider.IsValidPath(path, context);506 }507 catch (LoopFlowException)508 {509 throw;510 }511 catch (PipelineStoppedException)512 {513 throw;514 }515 catch (ActionPreferenceStopException)516 {517 throw;518 }519 catch (Exception e) // Catch-all OK, 3rd party callout.520 {521 throw NewProviderInvocationException(522 "IsValidPathProviderException",523 SessionStateStrings.IsValidPathProviderException,524 itemCmdletProvider.ProviderInfo,525 path,526 e);527 }528 529 return result;530 }531 532 #endregion IsValidPath533 534 #region IsItemContainer535 536 /// <summary>537 /// Determines if the monad virtual namespace path is a container.538 /// </summary>539 /// <param name="path">540 /// The path to the object to determine if it is a container.541 /// </param>542 /// <returns>543 /// true if the object specified by path is a container, false otherwise.544 /// </returns>545 /// <exception cref="ArgumentNullException">546 /// If <paramref name="path"/> is null.547 /// </exception>548 /// <exception cref="ProviderNotFoundException">549 /// If the <paramref name="path"/> refers to a provider that could not be found.550 /// </exception>551 /// <exception cref="DriveNotFoundException">552 /// If the <paramref name="path"/> refers to a drive that could not be found.553 /// </exception>554 /// <exception cref="NotSupportedException">555 /// If the provider that the <paramref name="path"/> refers to does556 /// not support this operation.557 /// </exception>558 /// <exception cref="ProviderInvocationException">559 /// If the provider threw an exception.560 /// </exception>561 internal bool IsItemContainer(string path)562 {563 if (path == null)564 {565 throw PSTraceSource.NewArgumentNullException(nameof(path));566 }567 568 CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext);569 570 bool result = IsItemContainer(path, context);571 572 context.ThrowFirstErrorOrDoNothing();573 574 return result;575 }576 577 /// <summary>578 /// Determines if the monad virtual namespace path is a container.579 /// </summary>580 /// <param name="path">581 /// The path to the object to determine if it is a container.582 /// </param>583 /// <param name="context">584 /// The context which the core command is running.585 /// </param>586 /// <returns>587 /// true if the object specified by path is a container, false otherwise.588 /// </returns>589 /// <exception cref="ArgumentNullException">590 /// If <paramref name="path"/> is null.591 /// </exception>592 /// <exception cref="ProviderNotFoundException">593 /// If the <paramref name="path"/> refers to a provider that could not be found.594 /// </exception>595 /// <exception cref="DriveNotFoundException">596 /// If the <paramref name="path"/> refers to a drive that could not be found.597 /// </exception>598 /// <exception cref="NotSupportedException">599 /// If the provider that the <paramref name="path"/> refers to does600 /// not support this operation.601 /// </exception>602 /// <exception cref="ProviderInvocationException">603 /// If the provider threw an exception.604 /// </exception>605 internal bool IsItemContainer(606 string path,607 CmdletProviderContext context)608 {609 if (path == null)610 {611 throw PSTraceSource.NewArgumentNullException(nameof(path));612 }613 614 ProviderInfo provider = null;615 CmdletProvider providerInstance = null;616 617 bool result = false;618 try619 {620 Collection<string> providerPaths =621 Globber.GetGlobbedProviderPathsFromMonadPath(622 path,623 true,624 context,625 out provider,626 out providerInstance);627 628 foreach (string providerPath in providerPaths)629 {630 result = IsItemContainer(providerInstance, providerPath, context);631 if (!result)632 {633 break;634 }635 }636 }637 catch (ItemNotFoundException)638 {639 result = false;640 }641 642 return result;643 }644 645 /// <summary>646 /// Determines if the item at the specified path is a container.647 /// </summary>648 /// <param name="providerInstance">649 /// The provider instance to use.650 /// </param>651 /// <param name="path">652 /// The path to the item if it was specified on the command line.653 /// </param>654 /// <param name="context">655 /// The context which the core command is running.656 /// </param>657 /// <exception cref="NotSupportedException">658 /// If the <paramref name="providerInstance"/> does not support this operation.659 /// </exception>660 /// <exception cref="PipelineStoppedException">661 /// If the pipeline is being stopped while executing the command.662 /// </exception>663 /// <exception cref="ProviderInvocationException">664 /// If the provider threw an exception.665 /// </exception>666 private bool IsItemContainer(667 CmdletProvider providerInstance,668 string path,669 CmdletProviderContext context)670 {671 // All parameters should have been validated by caller672 Dbg.Diagnostics.Assert(673 providerInstance != null,674 "Caller should validate providerInstance before calling this method");675 676 Dbg.Diagnostics.Assert(677 path != null,678 "Caller should validate path before calling this method");679 680 Dbg.Diagnostics.Assert(681 context != null,682 "Caller should validate context before calling this method");683 684 bool result = false;685 686 NavigationCmdletProvider navigationCmdletProvider = null;687 688 try689 {690 // If it is a NavigationProvider instance then we691 // can ask the provider if the path is to a container.692 693 navigationCmdletProvider =694 GetNavigationProviderInstance(providerInstance, false);695 696 try697 {698 result = navigationCmdletProvider.IsItemContainer(path, context);699 }700 catch (LoopFlowException)701 {702 throw;703 }704 catch (PipelineStoppedException)705 {706 throw;707 }708 catch (ActionPreferenceStopException)709 {710 throw;711 }712 catch (Exception e) // Catch-all OK, 3rd party callout.713 {714 throw NewProviderInvocationException(715 "IsItemContainerProviderException",716 SessionStateStrings.IsItemContainerProviderException,717 navigationCmdletProvider.ProviderInfo,718 path,719 e);720 }721 }722 catch (NotSupportedException)723 {724 try725 {726 // If it is a ContainerProvider instance then the727 // root (empty path) is considered a container728 // but everything else is not.729 730 GetContainerProviderInstance(providerInstance);731 732 if (path.Length == 0)733 {734 result = true;735 }736 else737 {738 result = false;739 }740 }741 catch (NotSupportedException)742 {743 // If the provider is neither a NavigationProvider nor a744 // ContainerProvider then the path cannot be a container745 746 result = false;747 }748 }749 750 return result;751 }752 753 #endregion IsItemContainer754 755 #region RemoveItem756 757 /// <summary>758 /// Deletes the specified object.759 /// </summary>760 /// <param name="paths">761 /// A relative or absolute path to the object to be deleted.762 /// </param>763 /// <param name="recurse">764 /// The delete should occur in all sub-containers of the specified path.765 /// </param>766 /// <param name="force">767 /// Passed on to providers to force operations.768 /// </param>769 /// <param name="literalPath">770 /// If true, globbing is not done on paths.771 /// </param>772 /// <exception cref="ArgumentNullException">773 /// If <paramref name="path"/> is null.774 /// </exception>775 /// <exception cref="ProviderNotFoundException">776 /// If the <paramref name="path"/> refers to a provider that could not be found.777 /// </exception>778 /// <exception cref="DriveNotFoundException">779 /// If the <paramref name="path"/> refers to a drive that could not be found.780 /// </exception>781 /// <exception cref="NotSupportedException">782 /// If the provider that the <paramref name="path"/> refers to does783 /// not support this operation.784 /// </exception>785 /// <exception cref="ProviderInvocationException">786 /// If the provider threw an exception.787 /// </exception>788 internal void RemoveItem(string[] paths, bool recurse, bool force, bool literalPath)789 {790 if (paths == null)791 {792 throw PSTraceSource.NewArgumentNullException(nameof(paths));793 }794 795 CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext);796 context.Force = force;797 context.SuppressWildcardExpansion = literalPath;798 799 RemoveItem(paths, recurse, context);800 801 context.ThrowFirstErrorOrDoNothing();802 }803 804 /// <summary>805 /// Deletes the specified object.806 /// </summary>807 /// <param name="paths">808 /// A relative or absolute path to the object to be deleted.809 /// </param>810 /// <param name="recurse">811 /// The delete should occur in all sub-containers of the specified path.812 /// </param>813 /// <param name="context">814 /// The context which the core command is running.815 /// </param>816 /// <exception cref="ArgumentNullException">817 /// If <paramref name="path"/> is null.818 /// </exception>819 /// <exception cref="ProviderNotFoundException">820 /// If the <paramref name="path"/> refers to a provider that could not be found.821 /// </exception>822 /// <exception cref="DriveNotFoundException">823 /// If the <paramref name="path"/> refers to a drive that could not be found.824 /// </exception>825 /// <exception cref="NotSupportedException">826 /// If the provider that the <paramref name="path"/> refers to does827 /// not support this operation.828 /// </exception>829 /// <exception cref="ProviderInvocationException">830 /// If the provider threw an exception.831 /// </exception>832 /// <exception cref="ItemNotFoundException">833 /// If <paramref name="path"/> does not contain glob characters and834 /// could not be found.835 /// </exception>836 internal void RemoveItem(837 string[] paths,838 bool recurse,839 CmdletProviderContext context)840 {841 if (paths == null)842 {843 throw PSTraceSource.NewArgumentNullException(nameof(paths));844 }845 846 foreach (string path in paths)847 {848 if (path == null)849 {850 throw PSTraceSource.NewArgumentNullException(nameof(paths));851 }852 853 ProviderInfo provider = null;854 CmdletProvider providerInstance = null;855 856 Collection<string> providerPaths =857 Globber.GetGlobbedProviderPathsFromMonadPath(858 path,859 false,860 context,861 out provider,862 out providerInstance);863 864 foreach (string providerPath in providerPaths)865 {866 RemoveItem(providerInstance, providerPath, recurse, context);867 }868 }869 }870 871 /// <summary>872 /// Internal remove item method that just calls the provider directly without globbing.873 /// </summary>874 /// <param name="providerId">875 /// The name of the provider to use.876 /// </param>877 /// <param name="path">878 /// The path of the item to remove.879 /// </param>880 /// <param name="recurse">881 /// True if all items should be removed recursively.882 /// </param>883 /// <param name="context">884 /// The context under which the command is running.885 /// </param>886 /// <exception cref="ProviderNotFoundException">887 /// If the <paramref name="path"/> refers to a provider that could not be found.888 /// </exception>889 /// <exception cref="DriveNotFoundException">890 /// If the <paramref name="path"/> refers to a drive that could not be found.891 /// </exception>892 /// <exception cref="NotSupportedException">893 /// If the provider that the <paramref name="path"/> refers to does894 /// not support this operation.895 /// </exception>896 /// <exception cref="ProviderInvocationException">897 /// If the provider threw an exception.898 /// </exception>899 internal void RemoveItem(900 string providerId,901 string path,902 bool recurse,903 CmdletProviderContext context)904 {905 // All parameters should have been validated by caller906 Dbg.Diagnostics.Assert(907 providerId != null,908 "Caller should validate providerId before calling this method");909 910 Dbg.Diagnostics.Assert(911 path != null,912 "Caller should validate path before calling this method");913 914 Dbg.Diagnostics.Assert(915 context != null,916 "Caller should validate context before calling this method");917 918 CmdletProvider providerInstance = GetProviderInstance(providerId);919 RemoveItem(providerInstance, path, recurse, context);920 }921 922 /// <summary>923 /// Internal remove item method that just calls the provider directly without globbing.924 /// </summary>925 /// <param name="providerInstance">926 /// The instance of the provider to use.927 /// </param>928 /// <param name="path">929 /// The path of the item to remove.930 /// </param>931 /// <param name="recurse">932 /// True if all items should be removed recursively.933 /// </param>934 /// <param name="context">935 /// The context under which the command is running.936 /// </param>937 /// <exception cref="ProviderNotFoundException">938 /// If the <paramref name="path"/> refers to a provider that could not be found.939 /// </exception>940 /// <exception cref="DriveNotFoundException">941 /// If the <paramref name="path"/> refers to a drive that could not be found.942 /// </exception>943 /// <exception cref="NotSupportedException">944 /// If the provider that the <paramref name="path"/> refers to does945 /// not support this operation.946 /// </exception>947 /// <exception cref="ProviderInvocationException">948 /// If the provider threw an exception.949 /// </exception>950 internal void RemoveItem(951 CmdletProvider providerInstance,952 string path,953 bool recurse,954 CmdletProviderContext context)955 {956 // All parameters should have been validated by caller957 Dbg.Diagnostics.Assert(958 providerInstance != null,959 "Caller should validate providerInstance before calling this method");960 961 Dbg.Diagnostics.Assert(962 path != null,963 "Caller should validate path before calling this method");964 965 Dbg.Diagnostics.Assert(966 context != null,967 "Caller should validate context before calling this method");968 969 ContainerCmdletProvider containerCmdletProvider =970 GetContainerProviderInstance(providerInstance);971 972 try973 {974 if (context.HasIncludeOrExclude)975 {976 int childrenNotMatchingFilterCriteria = 0;977 978 // Use the new code path only if either Include or Exclude is specified979 // This will take care of all the child items.980 // This will also take care of the case where "path" is not a container.981 ProcessPathItems(providerInstance, path, recurse, context, out childrenNotMatchingFilterCriteria, ProcessMode.Delete, skipIsItemContainerCheck: false);982 983 // Now delete the container if it matches the filter(s)984 // and the container does not have any children.985 if (IsItemContainer(providerInstance, path, context))986 {987 string item = GetChildName(path, context, false);988 bool isIncludeMatch =989 SessionStateUtilities.MatchesAnyWildcardPattern(990 item,991 SessionStateUtilities.CreateWildcardsFromStrings(992 context.Include, WildcardOptions.IgnoreCase),993 true);994 995 if (isIncludeMatch)996 {997 if (!SessionStateUtilities.MatchesAnyWildcardPattern(998 item,999 SessionStateUtilities.CreateWildcardsFromStrings(1000 context.Exclude, WildcardOptions.IgnoreCase),1001 false))1002 {1003 // Earlier, we used to check if "path" has any child items remaining. If not, we remove "path".1004 // This does not work for some providers (for e.g. IIS provider) which do not support removing the child items1005 // So, instead of checking for any child items remaining (which are applicable to only those providers which support them - like File, Registry),1006 // we check if there are any items that were never intended to be deleted. If there are no such items, then, we can remove "path".1007 // WinBlue: 2899071008 if (childrenNotMatchingFilterCriteria == 0)1009 {1010 containerCmdletProvider.RemoveItem(path, false, context);1011 }1012 }1013 }1014 }1015 }1016 else1017 {1018 containerCmdletProvider.RemoveItem(path, recurse, context);1019 }1020 }1021 catch (LoopFlowException)1022 {1023 throw;1024 }1025 catch (PipelineStoppedException)1026 {1027 throw;1028 }1029 catch (ActionPreferenceStopException)1030 {1031 throw;1032 }1033 catch (Exception e) // Catch-all OK, 3rd party callout.1034 {1035 throw NewProviderInvocationException(1036 "RemoveItemProviderException",1037 SessionStateStrings.RemoveItemProviderException,1038 containerCmdletProvider.ProviderInfo,1039 path,1040 e);1041 }1042 }1043 1044 /// <summary>1045 /// Gets the dynamic parameters for the remove-item cmdlet.1046 /// </summary>1047 /// <param name="path">1048 /// The path to the item if it was specified on the command line.1049 /// </param>1050 /// <param name="recurse">1051 /// The delete should occur in all sub-containers of the specified path.1052 /// </param>1053 /// <param name="context">1054 /// The context which the core command is running.1055 /// </param>1056 /// <returns>1057 /// An object that has properties and fields decorated with1058 /// parsing attributes similar to a cmdlet class.1059 /// </returns>1060 /// <exception cref="ProviderNotFoundException">1061 /// If the <paramref name="path"/> refers to a provider that could not be found.1062 /// </exception>1063 /// <exception cref="DriveNotFoundException">1064 /// If the <paramref name="path"/> refers to a drive that could not be found.1065 /// </exception>1066 /// <exception cref="NotSupportedException">1067 /// If the provider that the <paramref name="path"/> refers to does1068 /// not support this operation.1069 /// </exception>1070 /// <exception cref="ProviderInvocationException">1071 /// If the provider threw an exception.1072 /// </exception>1073 /// <exception cref="ItemNotFoundException">1074 /// If <paramref name="path"/> does not contain glob characters and1075 /// could not be found.1076 /// </exception>1077 internal object RemoveItemDynamicParameters(1078 string path,1079 bool recurse,1080 CmdletProviderContext context)1081 {1082 if (path == null)1083 {1084 return null;1085 }1086 1087 ProviderInfo provider = null;1088 CmdletProvider providerInstance = null;1089 1090 CmdletProviderContext newContext =1091 new CmdletProviderContext(context);1092 newContext.SetFilters(1093 new Collection<string>(),1094 new Collection<string>(),1095 null);1096 1097 Collection<string> providerPaths =1098 Globber.GetGlobbedProviderPathsFromMonadPath(1099 path,1100 true,1101 newContext,1102 out provider,1103 out providerInstance);1104 1105 if (providerPaths.Count > 0)1106 {1107 // Get the dynamic parameters for the first resolved path1108 1109 return RemoveItemDynamicParameters(providerInstance, providerPaths[0], recurse, newContext);1110 }1111 1112 return null;1113 }1114 1115 /// <summary>1116 /// Gets the dynamic parameters for the remove-item cmdlet.1117 /// </summary>1118 /// <param name="path">1119 /// The path to the item if it was specified on the command line.1120 /// </param>1121 /// <param name="recurse">1122 /// If true, all items in the subtree should be removed.1123 /// </param>1124 /// <param name="providerInstance">1125 /// The instance of the provider to use.1126 /// </param>1127 /// <param name="context">1128 /// The context which the core command is running.1129 /// </param>1130 /// <returns>1131 /// An object that has properties and fields decorated with1132 /// parsing attributes similar to a cmdlet class.1133 /// </returns>1134 /// <exception cref="NotSupportedException">1135 /// If the <paramref name="providerInstance"/> does not support this operation.1136 /// </exception>1137 /// <exception cref="PipelineStoppedException">1138 /// If the pipeline is being stopped while executing the command.1139 /// </exception>1140 /// <exception cref="ProviderInvocationException">1141 /// If the provider threw an exception.1142 /// </exception>1143 private object RemoveItemDynamicParameters(1144 CmdletProvider providerInstance,1145 string path,1146 bool recurse,1147 CmdletProviderContext context)1148 {1149 // All parameters should have been validated by caller1150 Dbg.Diagnostics.Assert(1151 providerInstance != null,1152 "Caller should validate providerInstance before calling this method");1153 1154 Dbg.Diagnostics.Assert(1155 path != null,1156 "Caller should validate path before calling this method");1157 1158 Dbg.Diagnostics.Assert(1159 context != null,1160 "Caller should validate context before calling this method");1161 1162 ContainerCmdletProvider containerCmdletProvider =1163 GetContainerProviderInstance(providerInstance);1164 1165 object result = null;1166 try1167 {1168 result = containerCmdletProvider.RemoveItemDynamicParameters(path, recurse, context);1169 }1170 catch (LoopFlowException)1171 {1172 throw;1173 }1174 catch (PipelineStoppedException)1175 {1176 throw;1177 }1178 catch (ActionPreferenceStopException)1179 {1180 throw;1181 }1182 catch (Exception e) // Catch-all OK, 3rd party callout.1183 {1184 throw NewProviderInvocationException(1185 "RemoveItemProviderException",1186 SessionStateStrings.RemoveItemProviderException,1187 containerCmdletProvider.ProviderInfo,1188 path,1189 e);1190 }1191 1192 return result;1193 }1194 1195 #endregion RemoveItem1196 1197 #region GetChildItems1198 1199 /// <summary>1200 /// Gets the children of the specified item.