Пример #1
0
        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));
        }
Пример #2
0
        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));
        }
Пример #3
0
        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);
            }
                                                    );
        }
Пример #4
0
        //internal string GetUnresolvedProviderPathFromPSPath(string path, ProviderRuntime runtime, out ProviderInfo provider, out PSDriveInfo drive);
        //internal bool IsCurrentLocationOrAncestor(string path, ProviderRuntime runtime);

        internal string NormalizeRelativePath(string path, string basePath, ProviderRuntime runtime)
        {
            // path shouldn't contain wildcards. They are *not* resolved
            ProviderInfo info;

            basePath = Globber.GetProviderSpecificPath(basePath, runtime, out info);
            path     = Globber.GetProviderSpecificPath(path, runtime, out info);
            var provider = _sessionState.Provider.GetInstance(info);

            CmdletProvider.VerifyType <ContainerCmdletProvider>(provider);
            var navProvider = provider as NavigationCmdletProvider;

            if (navProvider == null)
            {
                return(path);
            }
            return(navProvider.NormalizeRelativePath(path, basePath, runtime));
        }
Пример #5
0
        internal void Move(string[] path, string destinationPath, ProviderRuntime runtime)
        {
            ProviderInfo destinationProvider;
            var          destination = Globber.GetProviderSpecificPath(destinationPath, runtime, out destinationProvider);

            GlobAndInvoke <NavigationCmdletProvider>(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 (!runtime.PSDriveInfo.Provider.Equals(destinationProvider))
                {
                    var msg   = "The source cannot be moved to the destination, because they're not from the same provider";
                    var error = new PSArgumentException(msg, "MoveItemSourceAndDestinationNotSameProvider",
                                                        ErrorCategory.InvalidArgument);
                    runtime.WriteError(error.ErrorRecord);
                    return;
                }
                provider.MoveItem(curPath, destination, runtime);
            }
                                                     );
        }
Пример #6
0
        internal void New(string[] paths, string name, string type, object content, ProviderRuntime runtime)
        {
            var            validName = !String.IsNullOrEmpty(name);
            CmdletProvider provider;

            foreach (var path in paths)
            {
                Collection <string> resolvedPaths;
                // only allow globbing if name is used. otherwise it doesn't make sense
                if (validName)
                {
                    resolvedPaths = Globber.GetGlobbedProviderPaths(path, runtime, false, out provider);
                }
                else
                {
                    ProviderInfo providerInfo;
                    resolvedPaths = new Collection <string>();
                    resolvedPaths.Add(Globber.GetProviderSpecificPath(path, runtime, out providerInfo));
                    provider = SessionState.Provider.GetInstance(providerInfo);
                }
                var containerProvider = CmdletProvider.As <ContainerCmdletProvider>(provider);
                foreach (var curResolvedPath in resolvedPaths)
                {
                    var resPath = curResolvedPath;
                    if (validName)
                    {
                        resPath = Path.Combine(containerProvider, resPath, name, runtime);
                    }
                    try
                    {
                        containerProvider.NewItem(resPath, type, content, runtime);
                    }
                    catch (Exception e)
                    {
                        HandleCmdletProviderInvocationException(e);
                    }
                }
            }
        }