Beispiel #1
0
 protected override void RemoveItem(string path, bool recurse)
 {
     if (string.IsNullOrEmpty(path))
     {
         throw PSTraceSource.NewArgumentException("path");
     }
     try
     {
         path = NormalizePath(path);
         bool flag = false;
         FileSystemProviderRemoveItemDynamicParameters dynamicParameters = null;
         if (base.DynamicParameters != null)
         {
             dynamicParameters = base.DynamicParameters as FileSystemProviderRemoveItemDynamicParameters;
             if (dynamicParameters != null)
             {
                 if ((dynamicParameters.Stream != null) && (dynamicParameters.Stream.Length > 0))
                 {
                     flag = true;
                 }
                 else
                 {
                     int index = path.IndexOf(':');
                     int startIndex = path.IndexOf(':', index + 1);
                     if (startIndex > 0)
                     {
                         string str = path.Substring(startIndex + 1);
                         path = path.Remove(startIndex);
                         flag = true;
                         dynamicParameters = new FileSystemProviderRemoveItemDynamicParameters {
                             Stream = new string[] { str }
                         };
                     }
                 }
             }
         }
         bool isContainer = false;
         FileSystemInfo fileSystemInfo = GetFileSystemInfo(path, ref isContainer);
         if (fileSystemInfo == null)
         {
             Exception exception = new IOException(StringUtil.Format(FileSystemProviderStrings.ItemDoesNotExist, path));
             base.WriteError(new ErrorRecord(exception, "ItemDoesNotExist", ErrorCategory.ObjectNotFound, path));
         }
         else if (!flag && isContainer)
         {
             this.RemoveDirectoryInfoItem((DirectoryInfo) fileSystemInfo, recurse, (bool) base.Force, true);
         }
         else if (flag)
         {
             foreach (string str3 in dynamicParameters.Stream)
             {
                 WildcardPattern pattern = new WildcardPattern(str3, WildcardOptions.CultureInvariant | WildcardOptions.IgnoreCase);
                 bool flag3 = false;
                 foreach (AlternateStreamData data in AlternateDataStreamUtilities.GetStreams(fileSystemInfo.FullName))
                 {
                     if (pattern.IsMatch(data.Stream))
                     {
                         flag3 = true;
                         string target = string.Format(CultureInfo.InvariantCulture, FileSystemProviderStrings.StreamAction, new object[] { data.Stream, fileSystemInfo.FullName });
                         if (base.ShouldProcess(target))
                         {
                             AlternateDataStreamUtilities.DeleteFileStream(fileSystemInfo.FullName, data.Stream);
                         }
                     }
                 }
                 if (!WildcardPattern.ContainsWildcardCharacters(str3) && !flag3)
                 {
                     Exception exception2 = new FileNotFoundException(StringUtil.Format(FileSystemProviderStrings.AlternateDataStreamNotFound, str3, fileSystemInfo.FullName), fileSystemInfo.FullName);
                     base.WriteError(new ErrorRecord(exception2, "AlternateDataStreamNotFound", ErrorCategory.ObjectNotFound, path));
                 }
             }
         }
         else
         {
             this.RemoveFileInfoItem((FileInfo) fileSystemInfo, (bool) base.Force);
         }
     }
     catch (IOException exception3)
     {
         base.WriteError(new ErrorRecord(exception3, "RemoveItemIOError", ErrorCategory.WriteError, path));
     }
     catch (UnauthorizedAccessException exception4)
     {
         base.WriteError(new ErrorRecord(exception4, "RemoveItemUnauthorizedAccessError", ErrorCategory.PermissionDenied, path));
     }
 }
        } // CreateIntermediateDirectories


        #endregion NewItem

        #region RemoveItem

        /// <summary>
        /// Removes the specified file or directory.
        /// </summary>
        /// 
        /// <param name="path">
        /// The full path to the file or directory to be removed.
        /// </param>
        /// 
        /// <param name="recurse">
        /// Specifies if the operation should also remove child items.
        /// </param>
        /// 
        /// <exception cref="System.ArgumentException">
        ///     path is null or empty.
        /// </exception>
        protected override void RemoveItem(string path, bool recurse)
        {
            if (String.IsNullOrEmpty(path))
            {
                throw PSTraceSource.NewArgumentException("path");
            }

            try
            {
                path = NormalizePath(path);

                bool removeStreams = false;
                FileSystemProviderRemoveItemDynamicParameters dynamicParameters = null;

                if (DynamicParameters != null)
                {
                    dynamicParameters = DynamicParameters as FileSystemProviderRemoveItemDynamicParameters;
                    if (dynamicParameters != null)
                    {
                        if ((dynamicParameters.Stream != null) && (dynamicParameters.Stream.Length > 0))
                        {
                            removeStreams = true;
                        }
                        else
                        {
                            // See if they've used the inline stream syntax. They have more than one colon.
                            int firstColon = path.IndexOf(':');
                            int secondColon = path.IndexOf(':', firstColon + 1);
                            if (secondColon > 0)
                            {
                                string streamName = path.Substring(secondColon + 1);
                                path = path.Remove(secondColon);

                                removeStreams = true;
                                dynamicParameters = new FileSystemProviderRemoveItemDynamicParameters();
                                dynamicParameters.Stream = new string[] { streamName };
                            }
                        }
                    }
                }

                bool iscontainer = false;
                FileSystemInfo fsinfo = GetFileSystemInfo(path, ref iscontainer);
                if (fsinfo == null)
                {
                    String error = StringUtil.Format(FileSystemProviderStrings.ItemDoesNotExist, path);
                    Exception e = new IOException(error);
                    WriteError(new ErrorRecord(e, "ItemDoesNotExist", ErrorCategory.ObjectNotFound, path));
                    return;
                }

                if ((!removeStreams) && iscontainer)
                {
                    RemoveDirectoryInfoItem((DirectoryInfo)fsinfo, recurse, Force, true);
                }
                else
                {
                    // If we want to remove the file streams, retrieve them and remove them.
                    if (removeStreams)
                    {
                        foreach (string desiredStream in dynamicParameters.Stream)
                        {
                            // See that it matches the name specified
                            WildcardPattern p = WildcardPattern.Get(desiredStream, WildcardOptions.IgnoreCase | WildcardOptions.CultureInvariant);
                            bool foundStream = false;

                            foreach (AlternateStreamData stream in AlternateDataStreamUtilities.GetStreams(fsinfo.FullName))
                            {
                                if (!p.IsMatch(stream.Stream)) { continue; }
                                foundStream = true;

                                string action = String.Format(
                                    CultureInfo.InvariantCulture,
                                    FileSystemProviderStrings.StreamAction,
                                    stream.Stream, fsinfo.FullName);
                                if (ShouldProcess(action))
                                {
                                    AlternateDataStreamUtilities.DeleteFileStream(fsinfo.FullName, stream.Stream);
                                }
                            }

                            if ((!WildcardPattern.ContainsWildcardCharacters(desiredStream)) && (!foundStream))
                            {
                                string errorMessage = StringUtil.Format(
                                    FileSystemProviderStrings.AlternateDataStreamNotFound, desiredStream, fsinfo.FullName);
                                Exception e = new FileNotFoundException(errorMessage, fsinfo.FullName);

                                WriteError(new ErrorRecord(
                                    e,
                                    "AlternateDataStreamNotFound",
                                    ErrorCategory.ObjectNotFound,
                                    path));
                            }
                        }
                    }
                    else
                    {
                        RemoveFileInfoItem((FileInfo)fsinfo, Force);
                    }
                }
            }
            catch (IOException exception)
            {
                //IOException contains specific message about the error occured and so no need for errordetails.
                WriteError(new ErrorRecord(exception, "RemoveItemIOError", ErrorCategory.WriteError, path));
            }
            catch (UnauthorizedAccessException accessException)
            {
                WriteError(new ErrorRecord(accessException, "RemoveItemUnauthorizedAccessError", ErrorCategory.PermissionDenied, path));
            }
        } // RemoveItem