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); } }
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(); }
protected override void ProcessRecord() { CopyContainers copyContainer; CmdletProviderContext cmdletProviderContext = this.CmdletProviderContext; cmdletProviderContext.PassThru = this.PassThru; string[] strArrays = this.paths; for (int i = 0; i < (int)strArrays.Length; i++) { string str = strArrays[i]; object[] destination = new object[2]; destination[0] = str; destination[1] = this.Destination; CoreCommandBase.tracer.WriteLine("Copy {0} to {1}", destination); try { if (this.Container) { copyContainer = CopyContainers.CopyTargetContainer; } else { copyContainer = CopyContainers.CopyChildrenOfTargetContainer; } CopyContainers copyContainer1 = copyContainer; base.InvokeProvider.Item.Copy(str, this.Destination, this.Recurse, copyContainer1, cmdletProviderContext); } catch (PSNotSupportedException pSNotSupportedException1) { PSNotSupportedException pSNotSupportedException = pSNotSupportedException1; base.WriteError(new ErrorRecord(pSNotSupportedException.ErrorRecord, pSNotSupportedException)); } catch (DriveNotFoundException driveNotFoundException1) { DriveNotFoundException driveNotFoundException = driveNotFoundException1; base.WriteError(new ErrorRecord(driveNotFoundException.ErrorRecord, driveNotFoundException)); } catch (ProviderNotFoundException providerNotFoundException1) { ProviderNotFoundException providerNotFoundException = providerNotFoundException1; base.WriteError(new ErrorRecord(providerNotFoundException.ErrorRecord, providerNotFoundException)); } catch (ItemNotFoundException itemNotFoundException1) { ItemNotFoundException itemNotFoundException = itemNotFoundException1; base.WriteError(new ErrorRecord(itemNotFoundException.ErrorRecord, itemNotFoundException)); } } }
internal void Copy(string[] path, string destinationPath, bool recurse, CopyContainers copyContainers, ProviderRuntime runtime) { ProviderInfo destinationProvider; var destRuntime = new ProviderRuntime(runtime); var destination = Globber.GetProviderSpecificPath(destinationPath, destRuntime, out destinationProvider); // make sure we don't use the version of IsContainer that globs, or we will have unnecessary provider callbacks var destProvider = destinationProvider.CreateInstance() as ContainerCmdletProvider; // it's okay to be null var destIsContainer = IsContainer(destProvider, destination, destRuntime); GlobAndInvoke <ContainerCmdletProvider>(path, runtime, (curPath, provider) => { if (!runtime.PSDriveInfo.Provider.Equals(destinationProvider)) { var msg = "The source cannot be copied to the destination, because they're not from the same provider"; var error = new PSArgumentException(msg, "CopyItemSourceAndDestinationNotSameProvider", ErrorCategory.InvalidArgument); runtime.WriteError(error.ErrorRecord); return; } // Affected by #trailingSeparatorAmbiguity // PS would make sure the trailing slash of curPath is removed // check if src is a container if (IsContainer(provider, curPath, runtime)) { // if we copy a container to another, invoke a special method for this if (destIsContainer) { CopyContainerToContainer(provider, curPath, destination, recurse, copyContainers, runtime); return; } // otherwise the destination doesn't exist or is a leaf. Copying a container to a leaf doesn't work if (Exists(destination, destRuntime)) { var error = new PSArgumentException("Cannot copy container to existing leaf", "CopyContainerItemToLeafError", ErrorCategory.InvalidArgument).ErrorRecord; runtime.WriteError(error); return; } // otherwise we just proceed as normal } // either leaf to leaf, leaf to container, or container to not-existing (i.e. copy the container) provider.CopyItem(curPath, destination, recurse, runtime); } ); }
public Collection <PSObject> Copy(string[] path, string destinationPath, bool recurse, CopyContainers copyContainers, bool force, bool literalPath) { return(this.sessionState.CopyItem(path, destinationPath, recurse, copyContainers, force, literalPath)); }
internal void Copy(string path, string destinationPath, bool recurse, CopyContainers copyContainers, CmdletProviderContext context) { this.sessionState.CopyItem(new string[] { path }, destinationPath, recurse, copyContainers, context); }
public Collection <PSObject> Copy(string path, string destinationPath, bool recurse, CopyContainers copyContainers) { return(this.sessionState.CopyItem(new string[] { path }, destinationPath, recurse, copyContainers, false, false)); }
public Collection<PSObject> Copy(string[] path, string destinationPath, bool recurse, CopyContainers copyContainers, bool force, bool literalPath) { return this.sessionState.CopyItem(path, destinationPath, recurse, copyContainers, force, literalPath); }
} // CopyItem /// <summary> /// Copy item at the specified path /// </summary> /// /// <param name="path"> /// The path to the item to copy. It may be a drive or provider-qualified path and may include /// glob characters. /// </param> /// /// <param name="destinationPath"> /// The path to copy the item to. /// </param> /// /// <param name="recurse"> /// If true, copies all the children in all the sub-containers of the specified /// container. If false, only copies the specified item. /// </param> /// /// <param name="copyContainers"> /// Determines how the source container is used in the copy operation. /// </param> /// /// <param name="context"> /// The context under which the command is running. /// </param> /// /// <returns> /// Nothing. The item(s) that were copied are written to the context. /// </returns> /// /// <exception cref="ArgumentNullException"> /// If <paramref name="path"/> is null. /// </exception> /// /// <exception cref="ProviderNotFoundException"> /// If the <paramref name="path"/> refers to a provider that could not be found. /// </exception> /// /// <exception cref="DriveNotFoundException"> /// If the <paramref name="path"/> refers to a drive that could not be found. /// </exception> /// /// <exception cref="ItemNotFoundException"> /// If <paramref name="path"/> does not contain glob characters and /// could not be found. /// </exception> /// /// <exception cref="NotSupportedException"> /// If the provider that the <paramref name="path"/> refers to does /// not support this operation. /// </exception> /// /// <exception cref="ProviderInvocationException"> /// If the provider threw an exception. /// </exception> internal void Copy( string path, string destinationPath, bool recurse, CopyContainers copyContainers, CmdletProviderContext context) { Dbg.Diagnostics.Assert( _sessionState != null, "The only constructor for this class should always set the sessionState field"); // Parameter validation is done in the session state object _sessionState.CopyItem(new string[] { path }, destinationPath, recurse, copyContainers, context); } // CopyItem
public Collection<PSObject> Copy(string path, string destinationPath, bool recurse, CopyContainers copyContainers) { return Copy(new [] { path }, destinationPath, recurse, copyContainers, false, false); }
internal void CopyItem(string[] paths, string copyPath, bool recurse, CopyContainers copyContainers, CmdletProviderContext context) { if (paths == null) { throw PSTraceSource.NewArgumentNullException("paths"); } if (copyPath == null) { copyPath = string.Empty; } PSDriveInfo drive = null; ProviderInfo info2 = null; string path = this.Globber.GetProviderPath(copyPath, context, out info2, out drive); tracer.WriteLine("providerDestinationPath = {0}", new object[] { path }); ProviderInfo info3 = null; CmdletProvider providerInstance = null; foreach (string str2 in paths) { if (str2 == null) { throw PSTraceSource.NewArgumentNullException("paths"); } Collection<string> targetObject = this.Globber.GetGlobbedProviderPathsFromMonadPath(str2, false, context, out info3, out providerInstance); if (info3 != info2) { ArgumentException exception = PSTraceSource.NewArgumentException("path", "SessionStateStrings", "CopyItemSourceAndDestinationNotSameProvider", new object[0]); context.WriteError(new ErrorRecord(exception, "CopyItemSourceAndDestinationNotSameProvider", ErrorCategory.InvalidArgument, targetObject)); break; } bool flag = this.IsItemContainer(providerInstance, path, context); tracer.WriteLine("destinationIsContainer = {0}", new object[] { flag }); foreach (string str3 in targetObject) { if (context.Stopping) { break; } bool flag2 = this.IsItemContainer(providerInstance, str3, context); tracer.WriteLine("sourcIsContainer = {0}", new object[] { flag2 }); if (flag2) { if (flag) { if (!recurse && (copyContainers == CopyContainers.CopyChildrenOfTargetContainer)) { Exception exception2 = PSTraceSource.NewArgumentException("path", "SessionStateStrings", "CopyContainerToContainerWithoutRecurseOrContainer", new object[0]); context.WriteError(new ErrorRecord(exception2, "CopyContainerToContainerWithoutRecurseOrContainer", ErrorCategory.InvalidArgument, str3)); } else if (recurse && (copyContainers == CopyContainers.CopyChildrenOfTargetContainer)) { this.CopyRecurseToSingleContainer(providerInstance, str3, path, context); } else { this.CopyItem(providerInstance, str3, path, recurse, context); } } else if (this.ItemExists(providerInstance, path, context)) { Exception exception3 = PSTraceSource.NewArgumentException("path", "SessionStateStrings", "CopyContainerItemToLeafError", new object[0]); context.WriteError(new ErrorRecord(exception3, "CopyContainerItemToLeafError", ErrorCategory.InvalidArgument, str3)); } else { this.CopyItem(providerInstance, str3, path, recurse, context); } } else { this.CopyItem(providerInstance, str3, path, recurse, context); } } } }
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 Collection <PSObject> Copy(string path, string destinationPath, bool recurse, CopyContainers copyContainers) { return(Copy(new [] { path }, destinationPath, recurse, copyContainers, false, false)); }
public Collection<PSObject> Copy(string path, string destinationPath, bool recurse, CopyContainers copyContainers) { throw new NotImplementedException(); }
} // CopyItem /// <summary> /// Copies an item at the specified path to an item at the <paramref name="copyPath" />. /// </summary> /// /// <param name="paths"> /// The path(s) of the item(s) to copy. /// </param> /// /// <param name="copyPath"> /// The path of the item to copy to. /// </param> /// /// <param name="recurse"> /// Tells the provider to recurse sub-containers when copying. /// </param> /// /// <param name="copyContainers"> /// Determines how the source container is used in the copy operation. /// </param> /// /// <param name="context"> /// The context which the core command is running. /// </param> /// /// <exception cref="ArgumentNullException"> /// If <paramref name="path"/> is null. /// </exception> /// /// <exception cref="ProviderNotFoundException"> /// If the <paramref name="path"/> refers to a provider that could not be found. /// </exception> /// /// <exception cref="DriveNotFoundException"> /// If the <paramref name="path"/> refers to a drive that could not be found. /// </exception> /// /// <exception cref="NotSupportedException"> /// If the provider that the <paramref name="path"/> refers to does /// not support this operation. /// </exception> /// /// <exception cref="ProviderInvocationException"> /// If the provider threw an exception. /// </exception> /// /// <exception cref="ItemNotFoundException"> /// If <paramref name="path"/> does not contain glob characters and /// could not be found. /// </exception> /// internal void CopyItem( string[] paths, string copyPath, bool recurse, CopyContainers copyContainers, CmdletProviderContext context) { if (paths == null) { throw PSTraceSource.NewArgumentNullException("paths"); } if (copyPath == null) { copyPath = String.Empty; } // Get the provider specific path for the destination PSDriveInfo unusedDrive = null; ProviderInfo destinationProvider = null; Microsoft.PowerShell.Commands.CopyItemDynamicParameters dynamicParams = context.DynamicParameters as Microsoft.PowerShell.Commands.CopyItemDynamicParameters; bool destinationIsRemote = false; bool sourceIsRemote = false; string providerDestinationPath; Runspaces.PSSession session = null; if (dynamicParams != null) { if (dynamicParams.FromSession != null) { sourceIsRemote = true; session = dynamicParams.FromSession; } if (dynamicParams.ToSession != null) { destinationIsRemote = true; session = dynamicParams.ToSession; } } if (sourceIsRemote && destinationIsRemote) { context.WriteError(new ErrorRecord( new ArgumentException( String.Format(System.Globalization.CultureInfo.InvariantCulture, SessionStateStrings.CopyItemFromSessionToSession, "FromSession", "ToSession")), "InvalidInput", ErrorCategory.InvalidArgument, dynamicParams)); return; } // Validate that the session is available and the language mode is not set to ConstrainedLanguage or NoLanguage. PSLanguageMode? remoteLanguageMode = null; if (sourceIsRemote || destinationIsRemote) { if (!isValidSession(session, context, out remoteLanguageMode)) { return; } } if (!destinationIsRemote) { providerDestinationPath = Globber.GetProviderPath( copyPath, context, out destinationProvider, out unusedDrive); } else { // Validate remote destination path providerDestinationPath = copyPath; if (String.IsNullOrEmpty(providerDestinationPath)) { context.WriteError(new ErrorRecord( new ArgumentNullException( String.Format( System.Globalization.CultureInfo.InvariantCulture, SessionStateStrings.CopyItemRemotelyPathIsNullOrEmpty, "Destination")), "CopyItemRemoteDestinationIsNullOrEmpty", ErrorCategory.InvalidArgument, providerDestinationPath)); return; } string root = ValidateRemotePathAndGetRoot(providerDestinationPath, session, context, remoteLanguageMode, false); if (root == null) { return; } } s_tracer.WriteLine("providerDestinationPath = {0}", providerDestinationPath); ProviderInfo provider = null; CmdletProvider providerInstance = null; foreach (string path in paths) { if (path == null) { throw PSTraceSource.NewArgumentNullException("paths"); } Collection<string> providerPaths; if (sourceIsRemote) { // Get the root for the remote path string root = ValidateRemotePathAndGetRoot(path, session, context, remoteLanguageMode, true); if (root == null) { return; } providerInstance = ExecutionContext.SessionState.Internal.GetProviderInstance("FileSystem"); providerPaths = new Collection<string>(); providerPaths.Add(path); } else { providerPaths = Globber.GetGlobbedProviderPathsFromMonadPath( path, false, context, out provider, out providerInstance); } // Ensure that the source and destination are the same provider. Copying between // providers is not supported if (!sourceIsRemote && !destinationIsRemote && provider != destinationProvider) { ArgumentException argException = PSTraceSource.NewArgumentException( "path", SessionStateStrings.CopyItemSourceAndDestinationNotSameProvider); context.WriteError( new ErrorRecord( argException, "CopyItemSourceAndDestinationNotSameProvider", ErrorCategory.InvalidArgument, providerPaths)); return; } bool destinationIsContainer = false; if (!destinationIsRemote) { destinationIsContainer = IsItemContainer( providerInstance, providerDestinationPath, context); s_tracer.WriteLine("destinationIsContainer = {0}", destinationIsContainer); } foreach (string providerPath in providerPaths) { // Making sure to obey the StopProcessing. if (context.Stopping) { return; } if (sourceIsRemote || destinationIsRemote) { CopyItem( providerInstance, providerPath, providerDestinationPath, recurse, context); continue; } bool sourceIsContainer = IsItemContainer( providerInstance, providerPath, context); s_tracer.WriteLine("sourcIsContainer = {0}", sourceIsContainer); if (sourceIsContainer) { if (destinationIsContainer) { if (!recurse && copyContainers == CopyContainers.CopyChildrenOfTargetContainer) { // Copying a container to a container without the -container or -recurse // switch is an error. Exception e = PSTraceSource.NewArgumentException( "path", SessionStateStrings.CopyContainerToContainerWithoutRecurseOrContainer); context.WriteError(new ErrorRecord( e, "CopyContainerToContainerWithoutRecurseOrContainer", ErrorCategory.InvalidArgument, providerPath)); } else if (recurse && copyContainers == CopyContainers.CopyChildrenOfTargetContainer) { // Copy all the leaf items to a single container CopyRecurseToSingleContainer( providerInstance, providerPath, providerDestinationPath, context); } else { // Call the provider to do a recurse copy of all the items CopyItem( providerInstance, providerPath, providerDestinationPath, recurse, context); } } else { // Since we know the destination isn't a container, check to // see if it exists. if (ItemExists(providerInstance, providerDestinationPath, context)) { // Since the item exists and is not a container it must // be a leaf. Copying a container to a leaf is an error Exception e = PSTraceSource.NewArgumentException( "path", SessionStateStrings.CopyContainerItemToLeafError); context.WriteError(new ErrorRecord( e, "CopyContainerItemToLeafError", ErrorCategory.InvalidArgument, providerPath)); } else { // Copy the container to a non-existing path CopyItem( providerInstance, providerPath, providerDestinationPath, recurse, context); } } } else { // Copy a leaf to the destination CopyItem( providerInstance, providerPath, providerDestinationPath, recurse, context); } } } } // CopyItem
internal Collection<PSObject> CopyItem(string[] paths, string copyPath, bool recurse, CopyContainers copyContainers, bool force, bool literalPath) { if (paths == null) { throw PSTraceSource.NewArgumentNullException("paths"); } if (copyPath == null) { copyPath = string.Empty; } CmdletProviderContext context = new CmdletProviderContext(this.ExecutionContext) { Force = force, SuppressWildcardExpansion = literalPath }; this.CopyItem(paths, copyPath, recurse, copyContainers, context); context.ThrowFirstErrorOrDoNothing(); return context.GetAccumulatedObjects(); }
internal void Copy(string[] path, string destinationPath, bool recurse, CopyContainers copyContainers, ProviderRuntime runtime) { ProviderInfo destinationProvider; var destRuntime = new ProviderRuntime(runtime); var destination = Globber.GetProviderSpecificPath(destinationPath, destRuntime, out destinationProvider); // make sure we don't use the version of IsContainer that globs, or we will have unnecessary provider callbacks var destProvider = destinationProvider.CreateInstance() as ContainerCmdletProvider; // it's okay to be null var destIsContainer = IsContainer(destProvider, destination, destRuntime); GlobAndInvoke<ContainerCmdletProvider>(path, runtime, (curPath, provider) => { if (!runtime.PSDriveInfo.Provider.Equals(destinationProvider)) { var msg = "The source cannot be copied to the destination, because they're not from the same provider"; var error = new PSArgumentException(msg, "CopyItemSourceAndDestinationNotSameProvider", ErrorCategory.InvalidArgument); runtime.WriteError(error.ErrorRecord); return; } // Affected by #trailingSeparatorAmbiguity // PS would make sure the trailing slash of curPath is removed // check if src is a container if (IsContainer(provider, curPath, runtime)) { // if we copy a container to another, invoke a special method for this if (destIsContainer) { CopyContainerToContainer(provider, curPath, destination, recurse, copyContainers, runtime); return; } // otherwise the destination doesn't exist or is a leaf. Copying a container to a leaf doesn't work if (Exists(destination, destRuntime)) { var error = new PSArgumentException("Cannot copy container to existing leaf", "CopyContainerItemToLeafError", ErrorCategory.InvalidArgument).ErrorRecord; runtime.WriteError(error); return; } // otherwise we just proceed as normal } // either leaf to leaf, leaf to container, or container to not-existing (i.e. copy the container) provider.CopyItem(curPath, destination, recurse, runtime); } ); }
public Collection<PSObject> Copy(string path, string destinationPath, bool recurse, CopyContainers copyContainers) { return this.sessionState.CopyItem(new string[] { path }, destinationPath, recurse, copyContainers, false, false); }
public Collection <PSObject> Copy(string path, string destinationPath, bool recurse, CopyContainers copyContainers) { throw new NotImplementedException(); }
} // CopyItem /// <summary> /// Copy item at the specified path /// </summary> /// /// <param name="path"> /// The path(s) to the item(s) to copy. They may be a drive or provider-qualified path and may include /// glob characters. /// </param> /// /// <param name="destinationPath"> /// The path to copy the item to. /// </param> /// /// <param name="recurse"> /// If true, copies all the children in all the sub-containers of the specified /// container. If false, only copies the specified item. /// </param> /// /// <param name="copyContainers"> /// Determines how the source container is used in the copy operation. /// </param> /// /// <param name="force"> /// Passed on to providers to force operations. /// </param> /// /// <param name="literalPath"> /// If true, globbing is not done on paths. /// </param> /// /// <returns> /// The item(s) that were copied. /// </returns> /// /// <exception cref="ArgumentNullException"> /// If <paramref name="path"/> is null. /// </exception> /// /// <exception cref="ProviderNotFoundException"> /// If the <paramref name="path"/> refers to a provider that could not be found. /// </exception> /// /// <exception cref="DriveNotFoundException"> /// If the <paramref name="path"/> refers to a drive that could not be found. /// </exception> /// /// <exception cref="ItemNotFoundException"> /// If <paramref name="path"/> does not contain glob characters and /// could not be found. /// </exception> /// /// <exception cref="NotSupportedException"> /// If the provider that the <paramref name="path"/> refers to does /// not support this operation. /// </exception> /// /// <exception cref="ProviderInvocationException"> /// If the provider threw an exception. /// </exception> public Collection<PSObject> Copy( string[] path, string destinationPath, bool recurse, CopyContainers copyContainers, bool force, bool literalPath) { Dbg.Diagnostics.Assert( _sessionState != null, "The only constructor for this class should always set the sessionState field"); // Parameter validation is done in the session state object return _sessionState.CopyItem(path, destinationPath, recurse, copyContainers, force, literalPath); } // CopyItem