Exemple #1
0
        } // ClearContentDynamicParameters

        /// <summary>
        /// Calls the provider to get the clear-content dynamic parameters
        /// </summary>
        ///
        /// <param name="providerInstance">
        /// The instance of the provider to call
        /// </param>
        ///
        /// <param name="path">
        /// The path to pass to the provider.
        /// </param>
        ///
        /// <param name="context">
        /// The context the command is executing under.
        /// </param>
        ///
        /// <returns>
        /// The dynamic parameter object returned by the provider.
        /// </returns>
        ///
        /// <exception cref="NotSupportedException">
        /// If the <paramref name="providerInstance"/> does not support this operation.
        /// </exception>
        ///
        /// <exception cref="PipelineStoppedException">
        /// If the pipeline is being stopped while executing the command.
        /// </exception>
        ///
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        ///
        private object ClearContentDynamicParameters(
            CmdletProvider providerInstance,
            string path,
            CmdletProviderContext context)
        {
            // All parameters should have been validated by caller
            Dbg.Diagnostics.Assert(
                providerInstance != null,
                "Caller should validate providerInstance before calling this method");

            Dbg.Diagnostics.Assert(
                path != null,
                "Caller should validate path before calling this method");

            Dbg.Diagnostics.Assert(
                context != null,
                "Caller should validate context before calling this method");

            object result = null;

            try
            {
                result = providerInstance.ClearContentDynamicParameters(path, context);
            }
            catch (NotSupportedException)
            {
                throw;
            }
            catch (LoopFlowException)
            {
                throw;
            }
            catch (PipelineStoppedException)
            {
                throw;
            }
            catch (ActionPreferenceStopException)
            {
                throw;
            }
            catch (Exception e) // Catch-all OK, 3rd party callout.
            {
                throw NewProviderInvocationException(
                          "ClearContentDynamicParametersProviderException",
                          SessionStateStrings.ClearContentDynamicParametersProviderException,
                          providerInstance.ProviderInfo,
                          path,
                          e);
            }
            return(result);
        } // ClearContentDynamicParameters
Exemple #2
0
        /// <summary>
        /// Gets the security descriptor from the specified item.
        /// </summary>
        ///
        /// <param name="type">
        /// The type of the item which corresponds to the security
        /// descriptor that we want to create.
        /// </param>
        ///
        /// <param name="providerInstance">
        /// The type of the item which corresponds to the security
        /// descriptor that we want to create.
        /// </param>
        ///
        /// <param name="sections">
        /// Specifies the parts of a security descriptor to retrieve.
        /// </param>
        ///
        /// <returns>
        /// Nothing. The security descriptor for the item at the specified type is
        /// written to the context.
        /// </returns>
        ///
        internal ObjectSecurity NewSecurityDescriptorOfType(
            CmdletProvider providerInstance,
            string type,
            AccessControlSections sections)
        {
            ObjectSecurity sd = null;

            if (type == null)
            {
                throw PSTraceSource.NewArgumentNullException("type");
            }

            if (providerInstance == null)
            {
                throw PSTraceSource.NewArgumentNullException("providerInstance");
            }

            // This just verifies that the provider supports the interface.

            ISecurityDescriptorCmdletProvider sdProvider =
                GetPermissionProviderInstance(providerInstance);

            try
            {
                sd = sdProvider.NewSecurityDescriptorOfType(type,
                                                            sections);
            }
            catch (LoopFlowException)
            {
                throw;
            }
            catch (PipelineStoppedException)
            {
                throw;
            }
            catch (ActionPreferenceStopException)
            {
                throw;
            }
            catch (Exception e) // Catch-all OK, 3rd party callout.
            {
                throw NewProviderInvocationException(
                          "NewSecurityDescriptorProviderException",
                          SessionStateStrings.GetSecurityDescriptorProviderException,
                          providerInstance.ProviderInfo,
                          type,
                          e);
            }

            return(sd);
        } // NewSecurityDescriptorOfType
 public FileSystemContentReaderWriter(
     string path,
     FileMode mode,
     FileAccess access,
     FileShare share,
     string delimiter,
     Encoding encoding,
     bool waitForChanges,
     CmdletProvider provider)
     : this(path, mode, access, share, encoding, false, waitForChanges, provider)
 {
     this.delimiter      = delimiter;
     this.usingDelimiter = true;
 }
Exemple #4
0
        } // GetSecurityDescriptor

        private void GetSecurityDescriptor(
            CmdletProvider providerInstance,
            string path,
            AccessControlSections sections,
            CmdletProviderContext context)
        {
            // All parameters should have been validated by caller
            Diagnostics.Assert(
                providerInstance != null,
                "Caller should validate providerInstance before calling this method");

            Diagnostics.Assert(
                path != null,
                "Caller should validate path before calling this method");

            Diagnostics.Assert(
                context != null,
                "Caller should validate context before calling this method");

            // This just verifies that the provider supports the interface.

            GetPermissionProviderInstance(providerInstance);

            try
            {
                providerInstance.GetSecurityDescriptor(path, sections, context);
            }
            catch (LoopFlowException)
            {
                throw;
            }
            catch (PipelineStoppedException)
            {
                throw;
            }
            catch (ActionPreferenceStopException)
            {
                throw;
            }
            catch (Exception e) // Catch-all OK, 3rd party callout.
            {
                CommandProcessorBase.CheckForSevereException(e);
                throw NewProviderInvocationException(
                          "GetSecurityDescriptorProviderException",
                          SessionStateStrings.GetSecurityDescriptorProviderException,
                          providerInstance.ProviderInfo,
                          path,
                          e);
            }
        } // GetSecurityDescriptor
Exemple #5
0
        } // GetProperty

        /// <summary>
        /// Gets the property from the item at the specified path.
        /// </summary>
        ///
        /// <param name="providerInstance">
        /// The provider instance to use.
        /// </param>
        ///
        /// <param name="path">
        /// The path to the item if it was specified on the command line.
        /// </param>
        ///
        /// <param name="providerSpecificPickList">
        /// The names of the properties to get.
        /// </param>
        ///
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        ///
        /// <exception cref="NotSupportedException">
        /// If the <paramref name="providerInstance"/> does not support this operation.
        /// </exception>
        ///
        /// <exception cref="PipelineStoppedException">
        /// If the pipeline is being stopped while executing the command.
        /// </exception>
        ///
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        ///
        private void GetPropertyPrivate(
            CmdletProvider providerInstance,
            string path,
            Collection <string> providerSpecificPickList,
            CmdletProviderContext context)
        {
            // All parameters should have been validated by caller
            Dbg.Diagnostics.Assert(
                providerInstance != null,
                "Caller should validate providerInstance before calling this method");

            Dbg.Diagnostics.Assert(
                path != null,
                "Caller should validate path before calling this method");

            Dbg.Diagnostics.Assert(
                context != null,
                "Caller should validate context before calling this method");

            try
            {
                providerInstance.GetProperty(path, providerSpecificPickList, context);
            }
            catch (NotSupportedException)
            {
                throw;
            }
            catch (LoopFlowException)
            {
                throw;
            }
            catch (ActionPreferenceStopException)
            {
                throw;
            }
            catch (PipelineStoppedException)
            {
                throw;
            }
            catch (Exception e) // Catch-all OK, 3rd party callout.
            {
                CommandProcessorBase.CheckForSevereException(e);
                throw NewProviderInvocationException(
                          "GetPropertyProviderException",
                          SessionStateStrings.GetPropertyProviderException,
                          providerInstance.ProviderInfo,
                          path,
                          e);
            }
        } // GetPropertyPrivate
Exemple #6
0
        internal string Combine(CmdletProvider provider, string parent, string child, ProviderRuntime runtime)
        {
            CmdletProvider.VerifyType <ContainerCmdletProvider>(provider); // throws if it's not
            var navigationPorivder = provider as NavigationCmdletProvider;

            // for a container provider, this is always just the child string (yep, this is PS behavior)
            if (navigationPorivder == null)
            {
                return(child);
            }

            // otherwise use the NavigationCmdletProvider's MakePath method
            return(navigationPorivder.MakePath(parent, child, runtime));
        }
        private CommandInfo GetNextFromPath()
        {
            CommandInfo commandInfo = (CommandInfo)null;

            CommandDiscovery.discoveryTracer.WriteLine("The name appears to be a qualified path: {0}", (object)this.commandName);
            CommandDiscovery.discoveryTracer.WriteLine("Trying to resolve the path as an PSPath", new object[0]);
            ProviderInfo        provider   = (ProviderInfo)null;
            Collection <string> collection = new Collection <string>();

            try
            {
                CmdletProvider providerInstance = (CmdletProvider)null;
                collection = this._context.LocationGlobber.GetGlobbedProviderPathsFromMonadPath(this.commandName, false, out provider, out providerInstance);
            }
            catch (ItemNotFoundException ex)
            {
                CommandDiscovery.discoveryTracer.TraceError("The path could not be found: {0}", (object)this.commandName);
            }
            catch (DriveNotFoundException ex)
            {
                CommandDiscovery.discoveryTracer.TraceError("A drive could not be found for the path: {0}", (object)this.commandName);
            }
            catch (ProviderNotFoundException ex)
            {
                CommandDiscovery.discoveryTracer.TraceError("A provider could not be found for the path: {0}", (object)this.commandName);
            }
            catch (InvalidOperationException ex)
            {
                CommandDiscovery.discoveryTracer.TraceError("The path specified a home directory, but the provider home directory was not set. {0}", (object)this.commandName);
            }
            catch (ProviderInvocationException ex)
            {
                CommandDiscovery.discoveryTracer.TraceError("The provider associated with the path '{0}' encountered an error: {1}", (object)this.commandName, (object)ex.Message);
            }
            catch (PSNotSupportedException ex)
            {
                CommandDiscovery.discoveryTracer.TraceError("The provider associated with the path '{0}' does not implement ContainerCmdletProvider", (object)this.commandName);
            }
            if (collection.Count > 1)
            {
                CommandDiscovery.discoveryTracer.TraceError("The path resolved to more than one result so this path cannot be used.");
            }
            else if (collection.Count == 1 && File.Exists(collection[0]))
            {
                string path = collection[0];
                CommandDiscovery.discoveryTracer.WriteLine("Path resolved to: {0}", (object)path);
                commandInfo = this.GetInfoFromPath(path);
            }
            return(commandInfo);
        }
Exemple #8
0
        /// <summary>
        /// Sets item at the specified path.
        /// </summary>
        /// <param name="providerInstance">
        /// The provider instance to use.
        /// </param>
        /// <param name="path">
        /// The path to the item if it was specified on the command line.
        /// </param>
        /// <param name="value">
        /// The value of the item.
        /// </param>
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// <exception cref="NotSupportedException">
        /// If the <paramref name="providerInstance"/> does not support this operation.
        /// </exception>
        /// <exception cref="PipelineStoppedException">
        /// If the pipeline is being stopped while executing the command.
        /// </exception>
        /// <exception cref="ProviderInvocationException">
        /// If the provider threw an exception.
        /// </exception>
        private void SetItem(
            CmdletProvider providerInstance,
            string path,
            object value,
            CmdletProviderContext context)
        {
            // All parameters should have been validated by caller
            Dbg.Diagnostics.Assert(
                providerInstance != null,
                "Caller should validate providerInstance before calling this method");

            Dbg.Diagnostics.Assert(
                path != null,
                "Caller should validate path before calling this method");

            Dbg.Diagnostics.Assert(
                context != null,
                "Caller should validate context before calling this method");

            ItemCmdletProvider itemCmdletProvider =
                GetItemProviderInstance(providerInstance);

            try
            {
                itemCmdletProvider.SetItem(path, value, context);
            }
            catch (LoopFlowException)
            {
                throw;
            }
            catch (PipelineStoppedException)
            {
                throw;
            }
            catch (ActionPreferenceStopException)
            {
                throw;
            }
            catch (Exception e) // Catch-all OK, 3rd party callout.
            {
                throw NewProviderInvocationException(
                          "SetItemProviderException",
                          SessionStateStrings.SetItemProviderException,
                          itemCmdletProvider.ProviderInfo,
                          path,
                          e);
            }
        }
Exemple #9
0
        protected ItemContentReaderWriterBase(CmdletProvider provider, Item item,
                                              FileSystemCmdletProviderEncoding encoding, bool raw)
        {
            Item     = item;
            Provider = provider;
            Raw      = raw;

            if (encoding == FileSystemCmdletProviderEncoding.Unknown)
            {
                encoding = FileSystemCmdletProviderEncoding.Byte;
            }
            Encoding = new FileSystemContentWriterDynamicParameters()
            {
                Encoding = encoding
            }.EncodingType;
        }
        private CmdletProvider AddProvider(ProviderInfo providerInfo)
        {
            CmdletProvider provider = providerInfo.CreateInstance();

            provider.Start(providerInfo, new ProviderRuntime(_executionContext));
            provider.SetProviderInfo(providerInfo);

            // Cache the Provider's Info
            if (!_providers.ContainsKey(providerInfo.Name))
            {
                _providers.Add(providerInfo.Name, new List <ProviderInfo>());
            }
            _providers[providerInfo.Name].Add(providerInfo);

            return(provider);
        }
Exemple #11
0
        public Context(IContext context, object dynamicParameters)
        {
            Context c = context as Context;

            if (null == c)
            {
                throw new ArgumentException("the context provided is of an incompatible type");
            }

            _provider           = c._provider;
            _drive              = c._drive;
            _path               = c._path;
            _pathProcessor      = c._pathProcessor;
            DynamicParameters   = dynamicParameters;
            _recurse            = c.Recurse;
            PathTopologyVersion = c.PathTopologyVersion;
        }
Exemple #12
0
 public ItemContentWriter(CmdletProvider provider, Item item, string path, FileSystemCmdletProviderEncoding encoding, string extension, bool raw, bool fileBased, bool versioned, string language)
     : base(provider, item, encoding, raw)
 {
     if (Encoding != null && !Raw)
     {
         encoder = Encoding.GetEncoder();
     }
     if (!string.IsNullOrEmpty(extension))
     {
         this.extension = extension.StartsWith(".") ? extension : "." + extension;
     }
     this.fileBased = fileBased;
     this.path      = path;
     this.versioned = versioned;
     this.language  = language ?? Item?.Language?.Name;
     database       = Item?.Database?.Name ?? PathUtilities.GetDrive(path, "master");
 }
Exemple #13
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));
        }
        } // GetContentReader

        /// <summary>
        /// Gets the content reader for the specified item.
        /// </summary>
        /// <param name="paths">
        /// The path(s) to the item(s) to get the content reader from.
        /// </param>
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// <returns>
        /// The content readers for all items that the path resolves to.
        /// </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="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 Collection <IContentReader> GetContentReader(
            string[] paths,
            CmdletProviderContext context)
        {
            if (paths == null)
            {
                throw PSTraceSource.NewArgumentNullException("paths");
            }

            ProviderInfo   provider         = null;
            CmdletProvider providerInstance = null;

            Collection <IContentReader> results = new Collection <IContentReader>();

            foreach (string path in paths)
            {
                if (path == null)
                {
                    throw PSTraceSource.NewArgumentNullException("paths");
                }

                Collection <string> providerPaths =
                    Globber.GetGlobbedProviderPathsFromMonadPath(
                        path,
                        false,
                        context,
                        out provider,
                        out providerInstance);

                foreach (string providerPath in providerPaths)
                {
                    IContentReader reader = GetContentReaderPrivate(providerInstance, providerPath, context);

                    if (reader != null)
                    {
                        results.Add(reader);
                    }

                    context.ThrowFirstErrorOrDoNothing(true);
                }
            }

            return(results);
        } // GetContentReader
Exemple #15
0
        internal Collection <string> GetGlobbedProviderPaths(string path, ProviderRuntime runtime, bool itemMustExist,
                                                             out CmdletProvider provider)
        {
            var          results = new Collection <string>();
            ProviderInfo providerInfo;

            // get internal path, resolve home path and set provider info and drive info (if resolved)
            path     = GetProviderSpecificPath(path, runtime, out providerInfo);
            provider = _sessionState.Provider.GetInstance(providerInfo);

            if (!ShouldGlob(path, runtime))
            {
                // Although even ItemCmdletProvider supports ItemExists, PS doesn't seem
                // to throw errors when resolving paths with ItemProviders, only for ContainerProviders or higher
                // this behavior can be seen in the tests
                var containerProvider = provider as ContainerCmdletProvider;
                if (itemMustExist && containerProvider != null && !containerProvider.ItemExists(path, runtime))
                {
                    var msg = String.Format("An item with path {0} doesn't exist", path);
                    runtime.WriteError(new ItemNotFoundException(msg).ErrorRecord);
                    return(results);
                }
                results.Add(path);
                return(results);
            }

            if (providerInfo.Capabilities.HasFlag(ProviderCapabilities.ExpandWildcards))
            {
                var filter = new IncludeExcludeFilter(runtime.Include, runtime.Exclude, runtime.IgnoreFiltersForGlobbing);
                foreach (var expanded in CmdletProvider.As <ItemCmdletProvider>(provider).ExpandPath(path, runtime))
                {
                    if (filter.Accepts(expanded))
                    {
                        results.Add(expanded);
                    }
                }
            }
            else
            {
                results = BuiltInGlobbing(provider, path, runtime);
            }

            return(results);
        }
        /// <summary>
        /// Sets the specified properties on specified item.
        /// </summary>
        /// <param name="paths">
        /// The path(s) to the item(s) to set the properties on.
        /// </param>
        /// <param name="property">
        /// A property table containing the properties and values to be set on the object.
        /// </param>
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// <returns>
        /// Nothing. A PSObject is passed to the context for the properties on each item
        /// that were modified.
        /// </returns>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="path"/> or <paramref name="property"/> 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 SetProperty(
            string[] paths,
            PSObject property,
            CmdletProviderContext context)
        {
            if (paths == null)
            {
                throw PSTraceSource.NewArgumentNullException("paths");
            }

            if (property == null)
            {
                throw PSTraceSource.NewArgumentNullException("property");
            }

            foreach (string path in paths)
            {
                if (path == null)
                {
                    throw PSTraceSource.NewArgumentNullException("paths");
                }

                ProviderInfo   provider         = null;
                CmdletProvider providerInstance = null;

                Collection <string> providerPaths =
                    Globber.GetGlobbedProviderPathsFromMonadPath(
                        path,
                        false,
                        context,
                        out provider,
                        out providerInstance);

                if (providerPaths != null)
                {
                    foreach (string providerPath in providerPaths)
                    {
                        SetPropertyPrivate(providerInstance, providerPath, property, context);
                    }
                }
            }
        }
Exemple #17
0
        /// <summary>
        /// Gets the content writer for the specified item.
        /// </summary>
        /// <param name="paths">
        /// The path(s) to the item(s) to get the content writer from.
        /// </param>
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// <returns>
        /// The content writers for all items that the path resolves to.
        /// </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="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 Collection <IContentWriter> GetContentWriter(
            string[] paths,
            CmdletProviderContext context)
        {
            if (paths == null)
            {
                throw PSTraceSource.NewArgumentNullException(nameof(paths));
            }

            ProviderInfo   provider             = null;
            CmdletProvider providerInstance     = null;
            Collection <IContentWriter> results = new Collection <IContentWriter>();

            foreach (string path in paths)
            {
                if (path == null)
                {
                    throw PSTraceSource.NewArgumentNullException(nameof(paths));
                }

                Collection <string> providerPaths =
                    Globber.GetGlobbedProviderPathsFromMonadPath(
                        path,
                        true,
                        context,
                        out provider,
                        out providerInstance);

                foreach (string providerPath in providerPaths)
                {
                    IContentWriter result =
                        GetContentWriterPrivate(providerInstance, providerPath, context);

                    if (result != null)
                    {
                        results.Add(result);
                    }
                }
            }

            return(results);
        }
 internal void GlobAndInvoke <T>(IList <string> paths, ProviderRuntime runtime, Action <string, T> method) where T : CmdletProvider
 {
     foreach (var curPath in paths)
     {
         CmdletProvider provider;
         var            globbedPaths = Globber.GetGlobbedProviderPaths(curPath, runtime, out provider);
         var            itemProvider = CmdletProvider.As <T>(provider);
         foreach (var p in globbedPaths)
         {
             try
             {
                 method(p, itemProvider);
             }
             catch (Exception e)
             {
                 HandleCmdletProviderInvocationException(e);
             }
         }
     }
 }
Exemple #19
0
        private void AddDefaultDrives(CmdletProvider providerInstance, ProviderRuntime runtime)
        {
            DriveCmdletProvider driveProvider = providerInstance as DriveCmdletProvider;

            if (driveProvider == null)
            {
                return;
            }

            var drives = driveProvider.InitializeDefaultDrives(runtime);

            if (drives == null)
            {
                throw new PSInvalidOperationException("The default drive collection for this null!");
            }

            if (drives.Count == 0)
            {
                drives = providerInstance.GetDriveFromProviderInfo();
            }

            foreach (PSDriveInfo driveInfo in drives)
            {
                if (driveInfo == null)
                {
                    continue;
                }
                try
                {
                    // always to global scope
                    // use NewSkipInit because the dafault drives are inited
                    _sessionState.RootSessionState.Drive.NewSkipInit(driveInfo,
                                                                     SessionStateScope <PSDriveInfo> .ScopeSpecifiers.Global.ToString());
                }
                catch
                {
                    // TODO: What should we do if the drive name is not unique?
                    // => I guess overwrite the old one and write a warning (however this works from here)
                }
            }
        }
Exemple #20
0
        internal CmdletProvider CreateInstance()
        {
            object    obj2           = null;
            Exception innerException = null;

            try
            {
                obj2 = Activator.CreateInstance(this.ImplementingType);
            }
            catch (TargetInvocationException exception2)
            {
                innerException = exception2.InnerException;
            }
            catch (MissingMethodException)
            {
            }
            catch (MemberAccessException)
            {
            }
            catch (ArgumentException)
            {
            }
            if (obj2 == null)
            {
                ProviderNotFoundException exception3 = null;
                if (innerException != null)
                {
                    exception3 = new ProviderNotFoundException(this.Name, SessionStateCategory.CmdletProvider, "ProviderCtorException", SessionStateStrings.ProviderCtorException, new object[] { innerException.Message });
                }
                else
                {
                    exception3 = new ProviderNotFoundException(this.Name, SessionStateCategory.CmdletProvider, "ProviderNotFoundInAssembly", SessionStateStrings.ProviderNotFoundInAssembly, new object[0]);
                }
                throw exception3;
            }
            CmdletProvider provider = obj2 as CmdletProvider;

            provider.SetProviderInformation(this);
            return(provider);
        }
Exemple #21
0
        private void InitializeProvider(CmdletProvider providerInstance, ProviderInfo provider)
        {
            List <PSDriveInfo>  drives        = new List <PSDriveInfo>();
            DriveCmdletProvider driveProvider = providerInstance as DriveCmdletProvider;

            if (driveProvider != null)
            {
                Collection <PSDriveInfo> collection = driveProvider.DoInitializeDefaultDrives();
                if ((collection != null) && (collection.Count > 0))
                {
                    drives.AddRange(collection);
                    _providersCurrentDrive[provider] = collection[0];
                }
            }

            if (drives.Count > 0)
            {
                foreach (PSDriveInfo driveInfo in drives)
                {
                    if (driveInfo != null)
                    {
                        // TODO: need to set driveInfo.Root
                        // TODO:this should be automatically called when using DriveIntrinsics.New! #providerSupport
                        driveProvider.DoNewDrive(driveInfo);

                        try
                        {
                            //always to global scope
                            _executionContext.SessionState.Drive.New(driveInfo,
                                                                     SessionStateScope <PSDriveInfo> .ScopeSpecifiers.Global.ToString());
                        }
                        catch
                        {
                            // TODO: What should we do if the drive name is not unique?
                            // => I guess overwrite the old one and write a warning (however this works from here)
                        }
                    }
                }
            }
        }
        /// <summary>
        /// Clears the specified property in the specified item.
        /// </summary>
        /// <param name="paths">
        /// The path(s) to the item(s) to clear the property on.
        /// </param>
        /// <param name="propertyToClear">
        /// A property table containing the property to clear.
        /// </param>
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="path"/> or <paramref name="propertyToClear"/> 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 ClearProperty(
            string[] paths,
            Collection <string> propertyToClear,
            CmdletProviderContext context)
        {
            if (paths == null)
            {
                throw PSTraceSource.NewArgumentNullException(nameof(paths));
            }

            if (propertyToClear == null)
            {
                throw PSTraceSource.NewArgumentNullException(nameof(propertyToClear));
            }

            foreach (string path in paths)
            {
                if (path == null)
                {
                    throw PSTraceSource.NewArgumentNullException(nameof(paths));
                }

                ProviderInfo   provider         = null;
                CmdletProvider providerInstance = null;

                Collection <string> providerPaths =
                    Globber.GetGlobbedProviderPathsFromMonadPath(
                        path,
                        false,
                        context,
                        out provider,
                        out providerInstance);

                foreach (string providerPath in providerPaths)
                {
                    ClearPropertyPrivate(providerInstance, providerPath, propertyToClear, context);
                }
            }
        }
        internal CmdletProvider Add(ProviderInfo providerInfo, ExecutionContext executionContext)
        {
            CmdletProvider provider = providerInfo.CreateInstance();

            var runtime = new ProviderRuntime(executionContext.SessionState);

            providerInfo = provider.Start(providerInfo, runtime);
            provider.SetProviderInfo(providerInfo);

            // Cache the Provider's Info and instance
            if (!_providers.ContainsKey(providerInfo.Name))
            {
                _providers.Add(providerInfo.Name, new List <ProviderInfo>());
            }
            _providers[providerInfo.Name].Add(providerInfo);
            _providerInstances[providerInfo] = provider;

            // provider is added, default drives can be added
            AddDefaultDrives(provider, runtime);

            return(provider);
        }
Exemple #24
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);
                    }
                }
            }
        }
 public FileSystemContentReaderWriter(string path, string streamName, FileMode mode, FileAccess access, FileShare share, Encoding encoding, bool usingByteEncoding, bool waitForChanges, CmdletProvider provider, bool isRawStream)
 {
     this.delimiter = "\n";
     if (string.IsNullOrEmpty(path))
     {
         throw PSTraceSource.NewArgumentNullException("path");
     }
     tracer.WriteLine("path = {0}", new object[] { path });
     tracer.WriteLine("mode = {0}", new object[] { mode });
     tracer.WriteLine("access = {0}", new object[] { access });
     this.path              = path;
     this.streamName        = streamName;
     this.mode              = mode;
     this.access            = access;
     this.share             = share;
     this.encoding          = encoding;
     this.usingByteEncoding = usingByteEncoding;
     this.waitForChanges    = waitForChanges;
     this.provider          = provider;
     this.isRawStream       = isRawStream;
     this.CreateStreams(path, streamName, mode, access, share, encoding);
 }
        /// <summary>
        /// Gets the security descriptor from the specified item.
        /// </summary>
        /// <param name="path">
        /// The path to the item to retrieve the security descriptor from.
        /// </param>
        /// <param name="sections">
        /// Specifies the parts of a security descriptor to retrieve.
        /// </param>
        /// <returns>
        /// Nothing. The security descriptor for the item at the specified path is
        /// written to the context.
        /// </returns>
        /// <exception cref="ItemNotFoundException">
        /// If <paramref name="path"/> does not contain glob characters and
        /// could not be found.
        /// </exception>
        internal ObjectSecurity NewSecurityDescriptorFromPath(
            string path,
            AccessControlSections sections)
        {
            ObjectSecurity sd = null;

            if (path == null)
            {
                throw PSTraceSource.NewArgumentNullException("path");
            }

            ProviderInfo provider = null;

            CmdletProvider      providerInstance = null;
            Collection <string> providerPaths    =
                Globber.GetGlobbedProviderPathsFromMonadPath(
                    path,
                    false,
                    out provider,
                    out providerInstance);

            //
            // path must resolve to exact 1 item,
            // any other case is an error
            //
            if (providerPaths.Count == 1)
            {
                sd = NewSecurityDescriptorFromPath(providerInstance,
                                                   providerPaths[0],
                                                   sections);
            }
            else
            {
                throw PSTraceSource.NewArgumentException("path");
            }

            return(sd);
        }
        /// <summary>
        /// Gets the dynamic parameters for the clear-itemproperty cmdlet.
        /// </summary>
        /// <param name="path">
        /// The path to the item if it was specified on the command line.
        /// </param>
        /// <param name="propertyToClear">
        /// A property table containing the property to clear.
        /// </param>
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        /// <returns>
        /// An object that has properties and fields decorated with
        /// parsing attributes similar to a cmdlet class.
        /// </returns>
        /// <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 object ClearPropertyDynamicParameters(
            string path,
            Collection <string> propertyToClear,
            CmdletProviderContext context)
        {
            if (path == null)
            {
                return(null);
            }

            ProviderInfo   provider         = null;
            CmdletProvider providerInstance = null;

            CmdletProviderContext newContext =
                new CmdletProviderContext(context);

            newContext.SetFilters(
                new Collection <string>(),
                new Collection <string>(),
                null);

            Collection <string> providerPaths =
                Globber.GetGlobbedProviderPathsFromMonadPath(
                    path,
                    true,
                    newContext,
                    out provider,
                    out providerInstance);

            if (providerPaths.Count > 0)
            {
                // Get the dynamic parameters for the first resolved path

                return(ClearPropertyDynamicParameters(providerInstance, providerPaths[0], propertyToClear, newContext));
            }

            return(null);
        }
Exemple #28
0
        } // GetProperties

        /// <summary>
        /// Gets the specified properties from the specified item.
        /// </summary>
        ///
        /// <param name="paths">
        /// The path(s) to the item(s) to get the properties from.
        /// </param>
        ///
        /// <param name="providerSpecificPickList">
        /// A list of the properties that the provider should return.
        /// </param>
        ///
        /// <param name="context">
        /// The context which the core command is running.
        /// </param>
        ///
        /// <returns>
        /// Nothing. A PSObject representing the properties should be 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="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 GetProperty(
            string[] paths,
            Collection <string> providerSpecificPickList,
            CmdletProviderContext context)
        {
            if (paths == null)
            {
                throw PSTraceSource.NewArgumentNullException("paths");
            }

            foreach (string path in paths)
            {
                if (path == null)
                {
                    throw PSTraceSource.NewArgumentNullException("paths");
                }

                ProviderInfo   provider         = null;
                CmdletProvider providerInstance = null;

                Collection <string> providerPaths =
                    Globber.GetGlobbedProviderPathsFromMonadPath(
                        path,
                        false,
                        context,
                        out provider,
                        out providerInstance);

                foreach (string providerPath in providerPaths)
                {
                    GetPropertyPrivate(
                        providerInstance,
                        providerPath,
                        providerSpecificPickList,
                        context);
                }
            }
        } // GetProperty
Exemple #29
0
        internal void Rename(string path, string newName, ProviderRuntime runtime)
        {
            CmdletProvider provider;
            var            globbed = Globber.GetGlobbedProviderPaths(path, runtime, out provider);

            if (globbed.Count != 1)
            {
                throw new PSArgumentException("Cannot rename more than one item", "MultipleItemsRename", ErrorCategory.InvalidArgument);
            }
            path = globbed[0];
            var containerProvider = CmdletProvider.As <ContainerCmdletProvider>(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
            try
            {
                containerProvider.RenameItem(path, newName, runtime);
            }
            catch (Exception e)
            {
                HandleCmdletProviderInvocationException(e);
            }
        }
        private void InitializeProvider(CmdletProvider providerInstance, ProviderInfo provider)
        {
            List <PSDriveInfo>  drives        = new List <PSDriveInfo>();
            DriveCmdletProvider driveProvider = providerInstance as DriveCmdletProvider;

            if (driveProvider != null)
            {
                Collection <PSDriveInfo> collection = driveProvider.DoInitializeDefaultDrives();
                if ((collection != null) && (collection.Count > 0))
                {
                    drives.AddRange(collection);
                    _providersCurrentDrive[provider] = collection[0];
                }
            }

            if (drives.Count > 0)
            {
                foreach (PSDriveInfo driveInfo in drives)
                {
                    if (driveInfo != null)
                    {
                        // TODO: need to set driveInfo.Root

                        driveProvider.DoNewDrive(driveInfo);

                        try
                        {
                            _drives.Add(driveInfo.Name, driveInfo);
                        }
                        catch
                        {
                            // TODO: What should we do if the drive name is not unique?
                        }
                    }
                }
            }
        }