コード例 #1
0
        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);
            }
        }
コード例 #2
0
        internal Collection <PSObject> GetChildItems(string path, bool recurse, ProviderRuntime providerRuntime)
        {
            if (string.IsNullOrEmpty(path))
            {
                path = CurrentLocation.Path;
            }

            CmdletProvider provider = GetProviderByPath(path);

            if ((path != null) && (ItemExists(provider, path, providerRuntime)))
            {
                if (IsItemContainer(provider, path, providerRuntime))
                {
                    ContainerCmdletProvider containerProvider = provider as ContainerCmdletProvider;

                    if (containerProvider != null)
                    {
                        containerProvider.GetChildItems(path, recurse, providerRuntime);
                    }
                }
                else
                {
                    ItemCmdletProvider itemProvider = provider as ItemCmdletProvider;

                    if (itemProvider != null)
                    {
                        itemProvider.GetItem(path, providerRuntime);
                    }
                }
            }

            return(providerRuntime.RetreiveAllProviderData());
        }
コード例 #3
0
        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);
                }
            }
        }
コード例 #4
0
 private void GetItemOrChildItems(ContainerCmdletProvider provider, string path, bool recurse, ProviderRuntime runtime)
 {
     // If path identifies a container it gets all child items (recursively or not). Otherwise it returns the leaf
     if (Item.IsContainer(provider, path, runtime))
     {
         provider.GetChildItems(path, recurse, runtime);
         return;
     }
     provider.GetItem(path, runtime);
 }
コード例 #5
0
        internal List <string> GetValidChildNames(ContainerCmdletProvider provider, string providerPath, ReturnContainers returnContainers, ProviderRuntime runtime)
        {
            var subRuntime = new ProviderRuntime(runtime);

            subRuntime.PassThru = false; // so we can catch the results
            provider.GetChildNames(providerPath, returnContainers, subRuntime);
            var results = subRuntime.ThrowFirstErrorOrReturnResults();

            return((from c in results
                    where c.BaseObject is string
                    select((string)c.BaseObject)).ToList());
        }
コード例 #6
0
        internal bool IsContainer(ContainerCmdletProvider provider, string path, ProviderRuntime runtime)
        {
            var navProvider = provider as NavigationCmdletProvider;

            // path is an expanded path to a single location. no globbing and filtering is performed
            if (navProvider != null)
            {
                return(navProvider.IsItemContainer(path, runtime));
            }
            // otherwise it's just a ContainerCmdletProvider. It doesn't support hierarchies,
            // only drives can be containers
            // an empty path means "root" path in a drive
            return(path.Length == 0 || path.Equals(runtime.PSDriveInfo.Root));
        }
コード例 #7
0
        private void ManuallyGetChildItems(ContainerCmdletProvider provider, string path, bool recurse,
                                           IncludeExcludeFilter filter, ProviderRuntime runtime)
        {
            // recursively get child names of containers or just the current child if the filter accepts it
            if (recurse && Item.IsContainer(path, runtime))
            {
                ManuallyGetChildItemsFromContainer(provider, path, recurse, filter, runtime);
                return;
            }
            var childName = Path.ParseChildName(provider, path, runtime);

            if (filter.Accepts(childName))
            {
                provider.GetItem(path, runtime);
            }
        }
コード例 #8
0
ファイル: PathGlobber.cs プロジェクト: zhuyue1314/Pash
        private Stack <string> PathToStack(ContainerCmdletProvider provider, string path, ProviderRuntime runtime)
        {
            var componentStack = new Stack <string>();

            while (!String.IsNullOrEmpty(path))
            {
                var child = Path.ParseChildName(provider, path, runtime);
                componentStack.Push(child);
                var parentPath = Path.ParseParent(provider, path, "", runtime);
                if (parentPath.Equals(path))
                {
                    throw new PSInvalidOperationException("Provider's implementation of GetParentPath is inconsistent",
                                                          "ParentOfPathIsPath", ErrorCategory.InvalidResult);
                }
                path = parentPath;
            }
            return(componentStack);
        }
コード例 #9
0
        private void ManuallyGetChildNames(ContainerCmdletProvider provider, string providerPath, string relativePath,
                                           ReturnContainers returnContainers, bool recurse, IncludeExcludeFilter filter, ProviderRuntime runtime)
        {
            // Affected by #trailingSeparatorAmbiguity
            // Sometimes, PS removes or appends a trailing slash to the providerPath
            // E.g. when the recurse == true, there is a trailing slash, but not when recurse == false.
            // As it calls the method with the slash being appended and not appended, PS doesn't seem to make
            // promises to the provider implementation whether or not the path has a trailing slash
            var childNames = GetValidChildNames(provider, providerPath, returnContainers, runtime);

            foreach (var childName in childNames)
            {
                // add the child only if the filter accepts it
                if (!filter.Accepts(childName))
                {
                    continue;
                }
                var path = Path.Combine(provider, relativePath, childName, runtime);
                runtime.WriteObject(path);
            }
            // check if we need to handle this recursively
            if (!recurse)
            {
                return;
            }
            // okay, we should use recursion, so get all child containers and call this function again
            childNames = GetValidChildNames(provider, providerPath, ReturnContainers.ReturnAllContainers, runtime);
            foreach (var childName in childNames)
            {
                var providerChildPath = Path.Combine(provider, providerPath, childName, runtime);
                if (Item.IsContainer(providerChildPath, runtime))
                {
                    // recursive call wirth child's provider path and relative path
                    var relativeChildPath = Path.Combine(provider, relativePath, childName, runtime);
                    ManuallyGetChildNames(provider, providerChildPath, relativeChildPath, returnContainers,
                                          true, filter, runtime);
                }
            }
        }