Example #1
0
        /// <summary>
        /// Overridden method to create a new drive.
        /// </summary>
        /// <param name="drive">The template drive info.</param>
        /// <returns>The new drive info.</returns>
        protected override PSDriveInfo NewDrive(PSDriveInfo drive)
        {
            if (drive == null)
            {
                WriteError(new ErrorRecord(
                               new ArgumentNullException(nameof(drive)),
                               "NullDrive",
                               ErrorCategory.InvalidArgument,
                               null));

                return(null);
            }

            if (string.IsNullOrWhiteSpace(drive.Root) && (!drive.Root.StartsWith(GLOBAL_ROOT) || !drive.Root.StartsWith(NAMESPACE_ROOT)))
            {
                WriteError(new ErrorRecord(
                               new ArgumentNullException("drive.Root"),
                               "InvalidRoot",
                               ErrorCategory.InvalidArgument,
                               null));

                return(null);
            }

            try
            {
                if (drive.Root.StartsWith(NAMESPACE_ROOT))
                {
                    using (var descriptor = BoundaryDescriptor.CreateFromString(drive.Root.Substring(NAMESPACE_ROOT.Length)))
                    {
                        using (NtDirectory dir = NtDirectory.OpenPrivateNamespace(descriptor))
                        {
                            ObjectManagerPSDriveInfo objmgr_drive = new ObjectManagerPSDriveInfo(dir.Duplicate(), drive);
                            return(objmgr_drive);
                        }
                    }
                }
                else
                {
                    using (NtDirectory root = NtDirectory.Open(@"\"))
                    {
                        using (NtDirectory dir = NtDirectory.Open(drive.Root.Substring(GLOBAL_ROOT.Length), root, DirectoryAccessRights.MaximumAllowed))
                        {
                            ObjectManagerPSDriveInfo objmgr_drive = new ObjectManagerPSDriveInfo(dir.Duplicate(), drive);
                            return(objmgr_drive);
                        }
                    }
                }
            }
            catch (NtException ex)
            {
                WriteError(new ErrorRecord(
                               ex,
                               "NoRoot",
                               ErrorCategory.PermissionDenied,
                               drive));
                return(null);
            }
        }
        protected override PSDriveInfo NewDrive(PSDriveInfo drive)
        {
            if (drive == null)
            {
                WriteError(new ErrorRecord(
                               new ArgumentNullException("drive"),
                               "NullDrive",
                               ErrorCategory.InvalidArgument,
                               null));

                return(null);
            }

            if (String.IsNullOrWhiteSpace(drive.Root))
            {
                WriteError(new ErrorRecord(
                               new ArgumentException("drive.Root"),
                               "NoRoot",
                               ErrorCategory.InvalidArgument,
                               drive));

                return(null);
            }

            try
            {
                using (NtDirectory dir = NtDirectory.Open(drive.Root))
                {
                    ObjectManagerPSDriveInfo objmgr_drive = new ObjectManagerPSDriveInfo(dir.Duplicate(), drive);
                    return(objmgr_drive);
                }
            }
            catch (NtException ex)
            {
                WriteError(new ErrorRecord(
                               ex,
                               "NoRoot",
                               ErrorCategory.PermissionDenied,
                               drive));
                return(null);
            }
        }
        /// <summary>
        /// Overridden method to remove a drive.
        /// </summary>
        /// <param name="drive">The drive to remove.</param>
        /// <returns>The removed drive.</returns>
        protected override PSDriveInfo RemoveDrive(PSDriveInfo drive)
        {
            if (drive == null)
            {
                WriteError(new ErrorRecord(
                               new ArgumentNullException("drive"),
                               "NullDrive",
                               ErrorCategory.InvalidArgument,
                               drive));

                return(null);
            }

            ObjectManagerPSDriveInfo objmgr_drive = drive as ObjectManagerPSDriveInfo;

            if (objmgr_drive == null)
            {
                return(null);
            }

            objmgr_drive.DirectoryRoot.Close();

            return(objmgr_drive);
        }
        /// <summary>
        /// Overridden method to create a new drive.
        /// </summary>
        /// <param name="drive">The template drive info.</param>
        /// <returns>The new drive info.</returns>
        protected override PSDriveInfo NewDrive(PSDriveInfo drive)
        {
            if (drive == null)
            {
                WriteError(new ErrorRecord(
                           new ArgumentNullException("drive"),
                           "NullDrive",
                           ErrorCategory.InvalidArgument,
                           null));

                return null;
            }

            if (String.IsNullOrWhiteSpace(drive.Root) && (!drive.Root.StartsWith(GLOBAL_ROOT) || !drive.Root.StartsWith(NAMESPACE_ROOT)))
            {
                WriteError(new ErrorRecord(
                           new ArgumentNullException("drive.Root"),
                           "InvalidRoot",
                           ErrorCategory.InvalidArgument,
                           null));

                return null;
            }

            try
            {
                if (drive.Root.StartsWith(NAMESPACE_ROOT))
                {
                    using (NtDirectory dir = NtDirectory.OpenPrivateNamespace(BoundaryDescriptor.CreateFromString(drive.Root.Substring(NAMESPACE_ROOT.Length))))
                    {
                        ObjectManagerPSDriveInfo objmgr_drive = new ObjectManagerPSDriveInfo(dir.Duplicate(), drive);
                        return objmgr_drive;
                    }
                }
                else
                {
                    using (NtDirectory root = NtDirectory.Open(@"\"))
                    {
                        using (NtDirectory dir = NtDirectory.Open(drive.Root.Substring(GLOBAL_ROOT.Length), root, DirectoryAccessRights.MaximumAllowed))
                        {
                            ObjectManagerPSDriveInfo objmgr_drive = new ObjectManagerPSDriveInfo(dir.Duplicate(), drive);
                            return objmgr_drive;
                        }
                    }
                }
            }
            catch (NtException ex)
            {
                WriteError(new ErrorRecord(
                ex,
                "NoRoot",
                ErrorCategory.PermissionDenied,
                drive));
                return null;
            }
        }
        protected override PSDriveInfo NewDrive(PSDriveInfo drive)
        {
            if (drive == null)
            {
                WriteError(new ErrorRecord(
                           new ArgumentNullException("drive"),
                           "NullDrive",
                           ErrorCategory.InvalidArgument,
                           null));

                return null;
            }

            if (String.IsNullOrWhiteSpace(drive.Root))
            {
                WriteError(new ErrorRecord(
                           new ArgumentException("drive.Root"),
                           "NoRoot",
                           ErrorCategory.InvalidArgument,
                           drive));

                return null;
            }

            try
            {
                using (NtDirectory dir = NtDirectory.Open(drive.Root))
                {
                    ObjectManagerPSDriveInfo objmgr_drive = new ObjectManagerPSDriveInfo(dir.Duplicate(), drive);
                    return objmgr_drive;
                }
            }
            catch (NtException ex)
            {
                WriteError(new ErrorRecord(
                ex,
                "NoRoot",
                ErrorCategory.PermissionDenied,
                drive));
                return null;
            }
        }