Esempio n. 1
0
        internal static IEnumerable <string> GetModulePath(ExecutionContext context)
        {
            List <string> stringList          = new List <string>();
            string        environmentVariable = Environment.GetEnvironmentVariable("PSMODULEPATH");

            if (environmentVariable == null)
            {
                ModuleIntrinsics.SetModulePath();
                environmentVariable = Environment.GetEnvironmentVariable("PSMODULEPATH");
            }
            if (environmentVariable.Trim().Length == 0)
            {
                return((IEnumerable <string>)stringList);
            }
            string str1 = environmentVariable;

            char[] separator = new char[1] {
                ';'
            };
            foreach (string pattern in str1.Split(separator, StringSplitOptions.RemoveEmptyEntries))
            {
                try
                {
                    ProviderInfo         provider = (ProviderInfo)null;
                    IEnumerable <string> providerPathFromPsPath = (IEnumerable <string>)context.SessionState.Path.GetResolvedProviderPathFromPSPath(WildcardPattern.Escape(pattern), out provider);
                    if (provider.NameEquals(context.ProviderNames.FileSystem))
                    {
                        foreach (string str2 in providerPathFromPsPath)
                        {
                            stringList.Add(str2);
                        }
                    }
                }
                catch (ItemNotFoundException ex)
                {
                }
            }
            return((IEnumerable <string>)stringList);
        }
Esempio n. 2
0
        private string ResolvePSPath(string path)
        {
            string       str      = (string)null;
            ProviderInfo provider = (ProviderInfo)null;

            try
            {
                string providerPath = this._context.LocationGlobber.GetProviderPath(path, out provider);
                if (provider.NameEquals(this._context.ProviderNames.FileSystem))
                {
                    str = providerPath;
                    CommandDiscovery.discoveryTracer.WriteLine("The relative path was resolved to: {0}", (object)str);
                }
                else
                {
                    CommandDiscovery.discoveryTracer.TraceError("The relative path was not a file system path. {0}", (object)path);
                }
            }
            catch (InvalidOperationException ex)
            {
                CommandDiscovery.discoveryTracer.TraceError("The home path was not specified for the provider. {0}", (object)path);
            }
            catch (ProviderInvocationException ex)
            {
                CommandDiscovery.discoveryTracer.TraceError("While resolving the path, \"{0}\", an error was encountered by the provider: {1}", (object)path, (object)ex.Message);
            }
            catch (ItemNotFoundException ex)
            {
                CommandDiscovery.discoveryTracer.TraceError("The path does not exist: {0}", (object)path);
            }
            catch (DriveNotFoundException ex)
            {
                CommandDiscovery.discoveryTracer.TraceError("The drive does not exist: {0}", (object)ex.ItemName);
            }
            CommandSearcher.tracer.WriteLine("result = {0}", (object)str);
            return(str);
        }
Esempio n. 3
0
 internal static string GetProviderQualifiedPath(string path, ProviderInfo provider)
 {
     if (path == null)
     {
         throw PSTraceSource.NewArgumentNullException("path");
     }
     if (provider == null)
     {
         throw PSTraceSource.NewArgumentNullException("provider");
     }
     string str = path;
     bool flag = false;
     int index = path.IndexOf("::", StringComparison.Ordinal);
     if (index != -1)
     {
         string providerName = path.Substring(0, index);
         if (provider.NameEquals(providerName))
         {
             flag = true;
         }
     }
     if (!flag)
     {
         str = string.Format(CultureInfo.InvariantCulture, "{0}{1}{2}", new object[] { provider.FullName, "::", path });
     }
     tracer.WriteLine("result = {0}", new object[] { str });
     return str;
 }
Esempio n. 4
0
        } // NewProvider

        private ProviderInfo ProviderExists(ProviderInfo provider)
        {
            List<ProviderInfo> matchingProviders = null;

            if (Providers.TryGetValue(provider.Name, out matchingProviders))
            {
                foreach (ProviderInfo possibleMatch in matchingProviders)
                {
                    if (provider.NameEquals(possibleMatch.FullName))
                    {
                        return possibleMatch;
                    }
                }
            }
            return null;
        }
Esempio n. 5
0
        /// <summary>
        /// Initializes a provider by loading the assembly, creating an instance of the
        /// provider, calling its start method followed by the InitializeDefaultDrives method. The
        /// Drives that are returned from the InitializeDefaultDrives method are then mounted.
        /// </summary>
        /// 
        /// <param name="providerInstance">
        /// An instance of the provider to use for the initialization.
        /// </param>
        /// 
        /// <param name="provider">
        /// The provider to be initialized.
        /// </param>
        /// 
        /// <param name="context">
        /// The context under which the initialization is occurring. If this parameter is not
        /// null, errors will be written to the WriteError method of the context.
        /// </param>
        /// 
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="provider"/> or <paramref name="context"/> is null.
        /// </exception>
        /// 
        /// <exception cref="NotSupportedException">
        /// If the provider is not a DriveCmdletProvider.
        /// </exception>
        /// 
        /// <exception cref="SessionStateException">
        /// If a drive already exists for the name of one of the drives the
        /// provider tries to add.
        /// </exception>
        /// 
        /// <exception cref="SessionStateOverflowException">
        /// If the provider tries to add default drives which exceed the maximum
        /// limit for the number of drives in the current scope.
        /// </exception>
        /// 
        internal void InitializeProvider(
            Provider.CmdletProvider providerInstance,
            ProviderInfo provider,
            CmdletProviderContext context)
        {
            if (provider == null)
            {
                throw PSTraceSource.NewArgumentNullException("provider");
            }

            if (context == null)
            {
                context = new CmdletProviderContext(this.ExecutionContext);
            }

            // Initialize the provider so that it can add any drives
            // that it needs.

            List<PSDriveInfo> newDrives = new List<PSDriveInfo>();
            DriveCmdletProvider driveProvider =
                GetDriveProviderInstance(providerInstance);

            if (driveProvider != null)
            {
                try
                {
                    Collection<PSDriveInfo> drives = driveProvider.InitializeDefaultDrives(context);
                    if (drives != null && drives.Count > 0)
                    {
                        newDrives.AddRange(drives);
                        ProvidersCurrentWorkingDrive[provider] = drives[0];
                    }
                }
                catch (LoopFlowException)
                {
                    throw;
                }
                catch (PipelineStoppedException)
                {
                    throw;
                }
                catch (ActionPreferenceStopException)
                {
                    throw;
                }
                catch (Exception e) // Catch-all OK, 3rd party callout
                {
                    CommandProcessorBase.CheckForSevereException(e);
                    ProviderInvocationException providerException =
                        NewProviderInvocationException(
                            "InitializeDefaultDrivesException",
                            SessionStateStrings.InitializeDefaultDrivesException,
                            provider,
                            String.Empty,
                            e);

                    context.WriteError(
                        new ErrorRecord(
                            providerException,
                            "InitializeDefaultDrivesException",
                            ErrorCategory.InvalidOperation,
                            provider));
                }
            }

            if (newDrives != null && newDrives.Count > 0)
            {
                // Add the drives. 

                foreach (PSDriveInfo newDrive in newDrives)
                {
                    if (newDrive == null)
                    {
                        continue;
                    }

                    // Only mount drives for the current provider

                    if (!provider.NameEquals(newDrive.Provider.FullName))
                    {
                        continue;
                    }

                    try
                    {
                        PSDriveInfo validatedNewDrive = ValidateDriveWithProvider(driveProvider, newDrive, context, false);

                        if (validatedNewDrive != null)
                        {
                            // Since providers are global then the drives created
                            // through InitializeDefaultDrives should also be global.

                            GlobalScope.NewDrive(validatedNewDrive);
                        }
                    }
                    catch (SessionStateException exception)
                    {
                        context.WriteError(exception.ErrorRecord);
                    }
                } // foreach (drive in newDrives)
            }
        } // InitializeProvider
Esempio n. 6
0
        } // RemoveDriveQualifier

        /// <summary>
        /// Given an Msh path, returns a provider-qualified path.
        /// No globbing or relative path character expansion is done.
        /// </summary>
        ///
        /// <param name="path">
        /// The path to get the drive qualified path from.
        /// </param>
        ///
        /// <param name="provider">
        /// The provider the path should be qualified with.
        /// </param>
        ///
        /// <returns>
        /// A drive-qualified absolute Msh path.
        /// </returns>
        ///
        /// <exception cref="ArgumentNullException">
        /// If <paramref name="path"/> or <paramref name="provider"/> is null.
        /// </exception>
        /// 
        internal static string GetProviderQualifiedPath(string path, ProviderInfo provider)
        {
            if (path == null)
            {
                throw PSTraceSource.NewArgumentNullException("path");
            }


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

            string result = path;
            bool pathResolved = false;

            // Check to see if the path is already provider qualified

            int providerSeparatorIndex = path.IndexOf("::", StringComparison.Ordinal);
            if (providerSeparatorIndex != -1)
            {
                string possibleProvider = path.Substring(0, providerSeparatorIndex);

                if (provider.NameEquals(possibleProvider))
                {
                    pathResolved = true;
                }
            }

            if (!pathResolved)
            {
                result =
                    String.Format(
                        System.Globalization.CultureInfo.InvariantCulture,
                        "{0}{1}{2}",
                        provider.FullName,
                        StringLiterals.ProviderPathSeparator,
                        path);
            }

            return result;
        } // GetProviderQualifiedPath
Esempio n. 7
0
 private ProviderInfo ProviderExists(ProviderInfo provider)
 {
     List<ProviderInfo> list = null;
     if (this.Providers.TryGetValue(provider.Name, out list))
     {
         foreach (ProviderInfo info in list)
         {
             if (provider.NameEquals(info.FullName))
             {
                 return info;
             }
         }
     }
     return null;
 }
Esempio n. 8
0
 internal void InitializeProvider(CmdletProvider providerInstance, ProviderInfo provider, CmdletProviderContext context)
 {
     if (provider == null)
     {
         throw PSTraceSource.NewArgumentNullException("provider");
     }
     if (context == null)
     {
         context = new CmdletProviderContext(this.ExecutionContext);
     }
     List<PSDriveInfo> list = new List<PSDriveInfo>();
     DriveCmdletProvider driveProviderInstance = GetDriveProviderInstance(providerInstance);
     if (driveProviderInstance != null)
     {
         try
         {
             Collection<PSDriveInfo> collection = driveProviderInstance.InitializeDefaultDrives(context);
             if ((collection != null) && (collection.Count > 0))
             {
                 list.AddRange(collection);
                 this.ProvidersCurrentWorkingDrive[provider] = collection[0];
             }
         }
         catch (LoopFlowException)
         {
             throw;
         }
         catch (PipelineStoppedException)
         {
             throw;
         }
         catch (ActionPreferenceStopException)
         {
             throw;
         }
         catch (Exception exception)
         {
             CommandProcessorBase.CheckForSevereException(exception);
             ProviderInvocationException exception2 = this.NewProviderInvocationException("InitializeDefaultDrivesException", SessionStateStrings.InitializeDefaultDrivesException, provider, string.Empty, exception);
             context.WriteError(new ErrorRecord(exception2, "InitializeDefaultDrivesException", ErrorCategory.InvalidOperation, provider));
         }
     }
     if ((list != null) && (list.Count > 0))
     {
         foreach (PSDriveInfo info in list)
         {
             if ((info != null) && provider.NameEquals(info.Provider.FullName))
             {
                 try
                 {
                     PSDriveInfo newDrive = this.ValidateDriveWithProvider(driveProviderInstance, info, context, false);
                     if (newDrive != null)
                     {
                         this._globalScope.NewDrive(newDrive);
                     }
                 }
                 catch (SessionStateException exception3)
                 {
                     context.WriteError(exception3.ErrorRecord);
                 }
             }
         }
     }
 }
Esempio n. 9
0
 private string GetProviderRootFromSpecifiedRoot(string root, ProviderInfo provider)
 {
     string str = root;
     SessionState state = new SessionState(this._context.TopLevelSessionState);
     Collection<string> resolvedProviderPathFromPSPath = null;
     ProviderInfo info = null;
     try
     {
         resolvedProviderPathFromPSPath = state.Path.GetResolvedProviderPathFromPSPath(root, out info);
         if (((resolvedProviderPathFromPSPath != null) && (resolvedProviderPathFromPSPath.Count == 1)) && provider.NameEquals(info.FullName))
         {
             ProviderIntrinsics intrinsics = new ProviderIntrinsics(this);
             if (intrinsics.Item.Exists(root))
             {
                 str = resolvedProviderPathFromPSPath[0];
             }
         }
     }
     catch (LoopFlowException)
     {
         throw;
     }
     catch (PipelineStoppedException)
     {
         throw;
     }
     catch (ActionPreferenceStopException)
     {
         throw;
     }
     catch (System.Management.Automation.DriveNotFoundException)
     {
     }
     catch (ProviderNotFoundException)
     {
     }
     catch (ItemNotFoundException)
     {
     }
     catch (NotSupportedException)
     {
     }
     catch (InvalidOperationException)
     {
     }
     catch (ProviderInvocationException)
     {
     }
     catch (ArgumentException)
     {
     }
     return str;
 }
Esempio n. 10
0
        /// <summary>
        /// Tries to resolve the drive root as an MSH path. If it successfully resolves
        /// to a single path then the resolved provider internal path is returned. If it
        /// does not resolve to a single MSH path the root is returned as it was passed.
        /// </summary>
        /// 
        /// <param name="root">
        /// The root path of the drive to be resolved.
        /// </param>
        /// 
        /// <param name="provider">
        /// The provider that should be used when resolving the path.
        /// </param>
        /// 
        /// <returns>
        /// The new root path of the drive.
        /// </returns>
        /// 
        private string GetProviderRootFromSpecifiedRoot(string root, ProviderInfo provider)
        {
            Dbg.Diagnostics.Assert(
                root != null,
                "Caller should have verified the root");

            Dbg.Diagnostics.Assert(
                provider != null,
                "Caller should have verified the provider");

            string result = root;

            SessionState sessionState = new SessionState(ExecutionContext.TopLevelSessionState);
            Collection<string> resolvedPaths = null;
            ProviderInfo resolvedProvider = null;

            try
            {
                // First try to resolve the root as an MSH path

                resolvedPaths =
                    sessionState.Path.GetResolvedProviderPathFromPSPath(root, out resolvedProvider);

                // If a single path was resolved...

                if (resolvedPaths != null &&
                    resolvedPaths.Count == 1)
                {
                    // and the provider used to resolve the path, 
                    // matches the one specified by the drive...

                    if (provider.NameEquals(resolvedProvider.FullName))
                    {
                        // and the item exists

                        ProviderIntrinsics providerIntrinsics =
                            new ProviderIntrinsics(this);

                        if (providerIntrinsics.Item.Exists(root))
                        {
                            // then use the resolved path as the root of the drive

                            result = resolvedPaths[0];
                        }
                    }
                }
            }
            catch (LoopFlowException)
            {
                throw;
            }
            catch (PipelineStoppedException)
            {
                throw;
            }
            catch (ActionPreferenceStopException)
            {
                throw;
            }
            // If any of the following exceptions are thrown we assume that
            // the path is a file system path not an MSH path and try
            // to create the drive with that root.
            catch (DriveNotFoundException)
            {
            }
            catch (ProviderNotFoundException)
            {
            }
            catch (ItemNotFoundException)
            {
            }
            catch (NotSupportedException)
            {
            }
            catch (InvalidOperationException)
            {
            }
            catch (ProviderInvocationException)
            {
            }
            catch (ArgumentException)
            {
            }

            return result;
        } // GetProviderRootFromSpecifiedRoot