public Collection <PSObject> Rename(string path, string newName, bool force) { var runtime = new ProviderRuntime(SessionState, force, true); Rename(path, newName, runtime); return(runtime.ThrowFirstErrorOrReturnResults()); }
public void Remove(string driveName, bool force, string scope) { var runtime = new ProviderRuntime(_scope.SessionState); runtime.Force = new SwitchParameter(force); Remove(driveName, scope, runtime); }
public Collection <PSObject> Move(string[] path, string destination, bool force, bool literalPath) { var runtime = new ProviderRuntime(SessionState, force, literalPath); Move(path, destination, runtime); return(runtime.ThrowFirstErrorOrReturnResults()); }
internal bool Exists(string path, ProviderRuntime runtime) { CmdletProvider provider; var globbedPaths = Globber.GetGlobbedProviderPaths(path, runtime, false, out provider); var itemProvider = provider as ItemCmdletProvider; // we assume that in a low level CmdletProvider all items exists. Not sure about this, but I don't want to // break existing functionality if (itemProvider == null) { return(true); } foreach (var p in globbedPaths) { var exists = false; try { exists = itemProvider.ItemExists(p, runtime); } catch (Exception e) { HandleCmdletProviderInvocationException(e); } if (exists) { return(true); } } return(false); }
void CopyContainerToContainer(ContainerCmdletProvider provider, string srcPath, string destPath, bool recurse, CopyContainers copyContainers, ProviderRuntime runtime) { // the "usual" case: if we don't use recursion (empty container is copied) or we want to maintain the // original hierarchy if (!recurse || copyContainers.Equals(CopyContainers.CopyTargetContainer)) { provider.CopyItem(srcPath, destPath, recurse, runtime); return; } // Otherwise we want a flat-hierachy copy of a folder (because copyContainers is CopyChildrenOfTargetContainer) // Make sure recurse is set if (!recurse) { var error = new PSArgumentException("Cannot copy container to existing leaf", "CopyContainerItemToLeafError", ErrorCategory.InvalidArgument).ErrorRecord; runtime.WriteError(error); return; } // otherwise do the flat copy. To do this: get all child names (recursively) and invoke copying without recursion var childNames = ChildItem.GetNames(srcPath, ReturnContainers.ReturnMatchingContainers, true); foreach (var child in childNames) { var childPath = Path.Combine(provider, srcPath, child, runtime); provider.CopyItem(childPath, destPath, false, runtime); } }
private Collection <T> GlobAndCollect <T>(IList <string> paths, ProviderRuntime runtime, Func <string, IContentCmdletProvider, T> method) { var returnCollection = new Collection <T>(); foreach (var curPath in paths) { CmdletProvider provider; var globbedPaths = Globber.GetGlobbedProviderPaths(curPath, runtime, false, out provider); var contentProvider = CmdletProvider.As <IContentCmdletProvider>(provider); provider.ProviderRuntime = runtime; // make sure the runtime is set foreach (var p in globbedPaths) { try { returnCollection.Add(method(p, contentProvider)); } catch (Exception e) { HandleCmdletProviderInvocationException(e); } } } return(returnCollection); }
internal void Remove(string[] path, bool recurse, ProviderRuntime runtime) { GlobAndInvoke <ContainerCmdletProvider>(path, runtime, (curPath, provider) => { // TODO: I think Powershell checks whether we are currently in the path we want to remove // (or a subpath). Check this and throw an error if it's true if (provider.HasChildItems(curPath, runtime) && !recurse) { // TODO: I think Powershell invokes ShouldContinue here and asks whether to remove // items recursively or not. We should somehow do this too. Maybe by getting // access to runtime._cmdlet, or by implementing a wrapper function in ProviderRuntime var msg = String.Format("The item at path '{0}' has child items. Use recursion to remove it", curPath); var invOpEx = new PSInvalidOperationException(msg, "CannotRemoveItemWithChildrenWithoutRecursion", ErrorCategory.InvalidOperation, null); // FIXME: In this case, Powershell does throw a CmdletInvocationException. Maybe because // this check is done directly inside the Remove-Item cmdlet, or maybe it only // happens if ShouldContinue doesn't work in a non-interactive environment. // Anyway, it feels right that the work is done here and we will simply throw this // kind of exception for compatability. Maybe when the TODO before is approach we should // keep in mind that this kind of exception is required to be thrown throw new CmdletInvocationException(invOpEx.Message, invOpEx); } provider.RemoveItem(curPath, recurse, runtime); } ); }
internal PathInfo SetLocation(string path, ProviderRuntime runtime) { if (path == null) { throw new PSArgumentException("Path is null", "SetLocationPathNull", ErrorCategory.InvalidArgument); } ProviderInfo pinfo; path = Globber.GetProviderSpecificPath(path, runtime, out pinfo); var provider = _sessionState.Provider.GetInstance(pinfo); var containerProvider = CmdletProvider.As <ContainerCmdletProvider>(provider); var itemIntrinsics = new ItemCmdletProviderIntrinsics(_sessionState); if (!itemIntrinsics.Exists(path, runtime) || !itemIntrinsics.IsContainer(containerProvider, path, runtime)) { throw new PSArgumentException("The path does not exist or is not a container", "SetLocationInvalidPath", ErrorCategory.InvalidArgument); } if (provider is FileSystemProvider) { // TODO: really? I think PS doesn't do this System.Environment.CurrentDirectory = path; } var curDrive = runtime.PSDriveInfo; curDrive.CurrentLocation = path; _sessionStateGlobal.CurrentDrive = curDrive; return(new PathInfo(curDrive, path, _sessionState)); }
public void Remove(string[] path, bool recurse, bool force, bool literalPath) { var runtime = new ProviderRuntime(SessionState, force, literalPath); Remove(path, recurse, runtime); runtime.ThrowFirstErrorOrContinue(); }
private void ManuallyGetChildItemsFromContainer(ContainerCmdletProvider provider, string path, bool recurse, IncludeExcludeFilter filter, ProviderRuntime runtime) { // we deal with a container: get all child items (all containers if we recurse) Dictionary <string, bool> matches = null; // When a provider specific filter is set, and we need to recurse, we need to check recurse into all // containers, but just get those that match the internal filter. Therefore we construct a lookup dict. // Looking up in a dictionary whether or not the itemis a match should be faster than using a list // If there is no filter, then ReturnAllContainers and ReturnMatchingContainers don't differ if (!String.IsNullOrEmpty(runtime.Filter)) { matches = GetValidChildNames(provider, path, ReturnContainers.ReturnMatchingContainers, runtime).ToDictionary(c => c, c => true); } var childNames = GetValidChildNames(provider, path, ReturnContainers.ReturnAllContainers, runtime); foreach (var childName in childNames) { var childPath = Path.Combine(provider, path, childName, runtime); // if the filter accepts the child (leaf or container) and it's potentially a filter match, get it if (filter.Accepts(childName) && (matches == null || matches.ContainsKey(childName))) { provider.GetItem(childPath, runtime); } // if we need to recurse and deal with a container, dive into it if (recurse && Item.IsContainer(childPath, runtime)) { ManuallyGetChildItemsFromContainer(provider, childPath, true, filter, runtime); } } }
public Collection <PSObject> Get(string[] path, bool recurse, bool force, bool literalPath) { var runtime = new ProviderRuntime(SessionState, force, literalPath); Get(path, recurse, runtime); return(runtime.ThrowFirstErrorOrReturnResults()); }
private void Remove(ProviderInfo info, ExecutionContext executionContext) { var runtime = new ProviderRuntime(executionContext.SessionState); //remove all drives. TODO: I think _providers[name].Drive foreach (var drive in _sessionState.RootSessionState.Drive.GetAllForProvider(info.FullName)) { //remove from all scopes, sub-scopes might created a new drive using this provider _sessionState.RootSessionState.Drive.RemoveAtAllScopes(drive, runtime); } //now also stop and remove var inst = _providerInstances[info]; inst.Stop(runtime); _providerInstances.Remove(info); var list = _providers[info.Name]; if (list == null) // shouldn't happen, but who knows { return; } list.Remove(info); }
public Collection <PSObject> New(string[] paths, string name, string itemTypeName, object content, bool force) { var runtime = new ProviderRuntime(SessionState, force, true); New(paths, name, itemTypeName, content, runtime); return(runtime.ThrowFirstErrorOrReturnResults()); }
internal void Get(string[] path, ProviderRuntime runtime) { GlobAndInvoke <ItemCmdletProvider>(path, runtime, (curPath, provider) => { provider.GetItem(curPath, runtime); } ); }
public string ParseChildName(string path) { var runtime = new ProviderRuntime(_sessionState); var res = ParseChildName(path, runtime); runtime.ThrowFirstErrorOrContinue(); return(res); }
public PathInfo SetLocation(string path) { var runtime = new ProviderRuntime(_sessionState); var res = SetLocation(path, runtime); runtime.ThrowFirstErrorOrContinue(); return(res); }
public void Invoke(string[] path, bool literalPath) { var runtime = new ProviderRuntime(SessionState); runtime.AvoidGlobbing = literalPath; Invoke(path, runtime); runtime.ThrowFirstErrorOrContinue(); }
public bool IsContainer(string path) { var runtime = new ProviderRuntime(SessionState); bool result = IsContainer(path, runtime); runtime.ThrowFirstErrorOrContinue(); return(result); }
public Collection <PathInfo> GetResolvedPSPathFromPSPath(string path) { var runtime = new ProviderRuntime(_sessionState); var res = GetResolvedPSPathFromPSPath(new [] { path }, runtime); runtime.ThrowFirstErrorOrContinue(); return(res); }
public Collection <PSObject> Copy(string[] path, string destinationPath, bool recurse, CopyContainers copyContainers, bool force, bool literalPath) { var runtime = new ProviderRuntime(SessionState, force, literalPath); Copy(path, destinationPath, recurse, copyContainers, runtime); return(runtime.ThrowFirstErrorOrReturnResults()); }
public bool Exists(string path, bool force, bool literalPath) { var runtime = new ProviderRuntime(SessionState, force, literalPath); bool result = Exists(path, runtime); runtime.ThrowFirstErrorOrContinue(); return(result); }
public string ParseParent(string path, string root) { var runtime = new ProviderRuntime(_sessionState); var res = ParseParent(path, root, runtime); runtime.ThrowFirstErrorOrContinue(); return(res); }
public string NormalizeRelativePath(string path, string basePath) { var runtime = new ProviderRuntime(_sessionState); var res = NormalizeRelativePath(path, basePath, runtime); runtime.ThrowFirstErrorOrContinue(); return(res); }
public void WriteItemObject(object item, Path path, bool isContainer) { PSObject psObject = GetItemAsPSObject(item, path); PSNoteProperty member = new PSNoteProperty("PSIsContainer", isContainer); psObject.Properties.Add(member); ProviderRuntime.WriteObject(psObject); }
public bool HasChild(string path, bool force, bool literalPath) { var runtime = new ProviderRuntime(SessionState, force, literalPath); var res = HasChild(path, runtime); runtime.ThrowFirstErrorOrContinue(); return(res); }
private ProviderRuntime CreateProviderRuntime() { var runtime = new ProviderRuntime(this); runtime.Include = Include == null ? new Collection <string>() : new Collection <string>(Include.ToList()); runtime.Exclude = Exclude == null ? new Collection <string>() : new Collection <string>(Exclude.ToList()); runtime.AvoidGlobbing = AvoidWildcardExpansion; return(runtime); }
internal string Combine(string parent, string child, ProviderRuntime runtime) { ProviderInfo providerInfo; parent = new PathGlobber(_sessionState).GetProviderSpecificPath(parent, runtime, out providerInfo); var provider = _sessionStateGlobal.Provider.GetInstance(providerInfo); return(Combine(provider, parent, child, runtime)); }
internal string ParseParent(string path, string root, ProviderRuntime runtime) { ProviderInfo info; path = Globber.GetProviderSpecificPath(path, runtime, out info); var provider = _sessionState.Provider.GetInstance(info); return(ParseParent(provider, path, root, runtime)); }
internal Object NewDriveDynamicParameters(string providerName, ProviderRuntime runtime) { var driveProvider = _scope.SessionState.Provider.GetInstance(providerName) as DriveCmdletProvider; if (driveProvider == null) { throw new ArgumentException("The provider is not a drive provider"); } return(driveProvider.NewDriveDynamicParameters(runtime)); }
internal string ParseChildName(CmdletProvider provider, string path, ProviderRuntime runtime) { var navProvider = provider as NavigationCmdletProvider; if (navProvider == null) { return(path); } return(navProvider.GetChildName(path, runtime)); }