source: trunk/eraser/Eraser.DefaultPlugins/ErasureTargets/FolderErasureTarget.cs @ 2149

Revision 2149, 6.7 KB checked in by lowjoel, 4 years ago (diff)

Modified all dependent code to compile after the change in the previous revision.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008-2010 The Eraser Project
4 * Original Author: Joel Low <lowjoel@users.sourceforge.net>
5 * Modified By:
6 *
7 * This file is part of Eraser.
8 *
9 * Eraser is free software: you can redistribute it and/or modify it under the
10 * terms of the GNU General Public License as published by the Free Software
11 * Foundation, either version 3 of the License, or (at your option) any later
12 * version.
13 *
14 * Eraser is distributed in the hope that it will be useful, but WITHOUT ANY
15 * WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS FOR
16 * A PARTICULAR PURPOSE. See the GNU General Public License for more details.
17 *
18 * A copy of the GNU General Public License can be found at
19 * <http://www.gnu.org/licenses/>.
20 */
21
22using System;
23using System.Collections.Generic;
24using System.Linq;
25using System.Text;
26
27using System.Text.RegularExpressions;
28using System.Runtime.Serialization;
29using System.Runtime.InteropServices;
30using System.Security.Permissions;
31using System.IO;
32
33using Eraser.Manager;
34using Eraser.Util;
35
36namespace Eraser.DefaultPlugins
37{
38    /// <summary>
39    /// Represents a folder and its files which are to be erased.
40    /// </summary>
41    [Serializable]
42    [Guid("F50B0A44-3AB1-4cab-B81E-1713AC3D28C9")]
43    public class FolderErasureTarget : FileSystemObjectErasureTarget
44    {
45        #region Serialization code
46        protected FolderErasureTarget(SerializationInfo info, StreamingContext context)
47            : base(info, context)
48        {
49            IncludeMask = (string)info.GetValue("IncludeMask", typeof(string));
50            ExcludeMask = (string)info.GetValue("ExcludeMask", typeof(string));
51            DeleteIfEmpty = (bool)info.GetValue("DeleteIfEmpty", typeof(bool));
52        }
53
54        [SecurityPermission(SecurityAction.Demand, SerializationFormatter = true)]
55        public override void GetObjectData(SerializationInfo info, StreamingContext context)
56        {
57            base.GetObjectData(info, context);
58            info.AddValue("IncludeMask", IncludeMask);
59            info.AddValue("ExcludeMask", ExcludeMask);
60            info.AddValue("DeleteIfEmpty", DeleteIfEmpty);
61        }
62        #endregion
63
64        /// <summary>
65        /// Constructor.
66        /// </summary>
67        public FolderErasureTarget()
68        {
69            IncludeMask = string.Empty;
70            ExcludeMask = string.Empty;
71            DeleteIfEmpty = true;
72        }
73
74        public override Guid Guid
75        {
76            get { return GetType().GUID; }
77        }
78
79        public override string Name
80        {
81            get { return S._("Files in Folder"); }
82        }
83
84        public override IErasureTargetConfigurer Configurer
85        {
86            get { return new FolderErasureTargetConfigurer(); }
87        }
88
89        protected override List<StreamInfo> GetPaths()
90        {
91            //Get a list to hold all the resulting streams.
92            List<StreamInfo> result = new List<StreamInfo>();
93
94            //Open the root of the search, including every file matching the pattern
95            DirectoryInfo dir = new DirectoryInfo(Path);
96
97            //List recursively all the files which match the include pattern.
98            FileInfo[] files = GetFiles(dir);
99
100            //Then exclude each file and finalize the list and total file size
101            Regex includePattern = string.IsNullOrEmpty(IncludeMask) ? null :
102                new Regex(
103                    Regex.Escape(ExcludeMask).Replace("\\*", ".*").Replace("\\?", "."),
104                    RegexOptions.IgnoreCase | RegexOptions.Compiled);
105            Regex excludePattern = string.IsNullOrEmpty(ExcludeMask) ? null :
106                new Regex(
107                    Regex.Escape(ExcludeMask).Replace("\\*", ".*").Replace("\\?", "."),
108                    RegexOptions.IgnoreCase | RegexOptions.Compiled);
109            foreach (FileInfo file in files)
110            {
111                //Check that the file is included
112                if (includePattern != null && !includePattern.Match(file.FullName).Success)
113                    continue;
114
115                //Check that the file is not excluded
116                if (excludePattern != null && excludePattern.Match(file.FullName).Success)
117                    continue;
118
119                //Add the size of the file and its alternate data streams
120                result.AddRange(GetPathADSes(file));
121
122                //And the file itself
123                result.Add(new StreamInfo(file.FullName));
124            }
125
126            //Return the filtered list.
127            return result;
128        }
129
130        /// <summary>
131        /// A wildcard expression stating the condition for the set of files to include.
132        /// The include mask is applied before the exclude mask is applied. If this value
133        /// is empty, all files and folders within the folder specified is included.
134        /// </summary>
135        public string IncludeMask { get; set; }
136
137        /// <summary>
138        /// A wildcard expression stating the condition for removing files from the set
139        /// of included files. If this value is omitted, all files and folders extracted
140        /// by the inclusion mask is erased.
141        /// </summary>
142        public string ExcludeMask { get; set; }
143
144        /// <summary>
145        /// Determines if Eraser should delete the folder after the erase process.
146        /// </summary>
147        public bool DeleteIfEmpty { get; set; }
148
149        public override void Execute()
150        {
151            Progress = new SteppedProgressManager();
152
153            try
154            {
155                base.Execute();
156
157                //Remove the contents of the folder, deleting the folder if it is empty
158                //at the end of it.
159                EraseFolder();
160            }
161            finally
162            {
163                Progress = null;
164            }
165        }
166
167        /// <summary>
168        /// Erases the folder after all files have been deleted. This folder does not
169        /// delete folders which have files within it.
170        /// </summary>
171        private void EraseFolder()
172        {
173            //Update the progress to show that folders are being removed.
174            ProgressManager step = new ProgressManager();
175            Progress.Steps.Add(new SteppedProgressManagerStep(step,
176                0.0f, S._("Removing folders...")));
177
178            //Remove all subfolders which are empty.
179            FileSystem fsManager = ManagerLibrary.Instance.FileSystemRegistrar[
180                VolumeInfo.FromMountPoint(Path)];
181            DirectoryInfo directory = new DirectoryInfo(Path);
182                foreach (DirectoryInfo subDir in directory.GetDirectories())
183                    EraseFolder(subDir, step);
184
185            if (DeleteIfEmpty)
186            {
187                //See if this is the root of a volume.
188                bool isVolumeRoot = directory.Parent == null;
189                foreach (VolumeInfo volume in VolumeInfo.Volumes)
190                    foreach (DirectoryInfo mountPoint in volume.MountPoints)
191                        if (directory.FullName == mountPoint.FullName)
192                            isVolumeRoot = true;
193
194                //If the folder is a mount point, then don't delete it. If it isn't,
195                //search for files under the folder to see if it is empty.
196                if (!isVolumeRoot && directory.Exists &&
197                    directory.GetFiles("*", SearchOption.AllDirectories).Length == 0)
198                {
199                    fsManager.DeleteFolder(directory);
200                }
201            }
202        }
203
204        private void EraseFolder(DirectoryInfo info, ProgressManager progress)
205        {
206            foreach (DirectoryInfo subDir in info.GetDirectories())
207                EraseFolder(subDir, progress);
208            OnProgressChanged(this, new ProgressChangedEventArgs(progress,
209                new TaskProgressChangedEventArgs(info.FullName, 0, 0)));
210
211            FileSystemInfo[] files = info.GetFileSystemInfos();
212            if (files.Length == 0)
213            {
214                ManagerLibrary.Instance.FileSystemRegistrar[
215                    VolumeInfo.FromMountPoint(Path)].DeleteFolder(info);
216            }
217        }
218    }
219}
Note: See TracBrowser for help on using the repository browser.