| 1 | using System; |
|---|
| 2 | using System.Collections.Generic; |
|---|
| 3 | using System.Text; |
|---|
| 4 | using System.IO; |
|---|
| 5 | using System.Text.RegularExpressions; |
|---|
| 6 | using System.ComponentModel; |
|---|
| 7 | |
|---|
| 8 | namespace Eraser.Manager |
|---|
| 9 | { |
|---|
| 10 | /// <summary> |
|---|
| 11 | /// Deals with an erase task. |
|---|
| 12 | /// </summary> |
|---|
| 13 | public class Task |
|---|
| 14 | { |
|---|
| 15 | /// <summary> |
|---|
| 16 | /// Represents a generic target of erasure |
|---|
| 17 | /// </summary> |
|---|
| 18 | public abstract class ErasureTarget |
|---|
| 19 | { |
|---|
| 20 | /// <summary> |
|---|
| 21 | /// The method used for erasing the file. If the variable is equal to |
|---|
| 22 | /// ErasureMethodManager.Default then the default is queried for the |
|---|
| 23 | /// task type. |
|---|
| 24 | /// </summary> |
|---|
| 25 | public ErasureMethod Method |
|---|
| 26 | { |
|---|
| 27 | get { return method; } |
|---|
| 28 | set { method = value; } |
|---|
| 29 | } |
|---|
| 30 | |
|---|
| 31 | /// <summary> |
|---|
| 32 | /// Retrieves the text to display representing this task. |
|---|
| 33 | /// </summary> |
|---|
| 34 | public abstract string UIText |
|---|
| 35 | { |
|---|
| 36 | get; |
|---|
| 37 | } |
|---|
| 38 | |
|---|
| 39 | private ErasureMethod method = null; |
|---|
| 40 | } |
|---|
| 41 | |
|---|
| 42 | /// <summary> |
|---|
| 43 | /// Class representing a tangible object (file/folder) to be erased. |
|---|
| 44 | /// </summary> |
|---|
| 45 | public abstract class FilesystemObject : ErasureTarget |
|---|
| 46 | { |
|---|
| 47 | /// <summary> |
|---|
| 48 | /// Retrieves the list of files/folders to erase as a list. |
|---|
| 49 | /// </summary> |
|---|
| 50 | /// <returns></returns> |
|---|
| 51 | internal abstract List<string> GetPaths(); |
|---|
| 52 | |
|---|
| 53 | /// <summary> |
|---|
| 54 | /// The path to the file or folder referred to by this object. |
|---|
| 55 | /// </summary> |
|---|
| 56 | public string Path |
|---|
| 57 | { |
|---|
| 58 | get { return path; } |
|---|
| 59 | set { path = value; } |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | public override string UIText |
|---|
| 63 | { |
|---|
| 64 | get { return Path; } |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | private string path; |
|---|
| 68 | } |
|---|
| 69 | |
|---|
| 70 | /// <summary> |
|---|
| 71 | /// Class representing a unused space erase. |
|---|
| 72 | /// </summary> |
|---|
| 73 | public class UnusedSpace : ErasureTarget |
|---|
| 74 | { |
|---|
| 75 | public string Drive; |
|---|
| 76 | |
|---|
| 77 | public override string UIText |
|---|
| 78 | { |
|---|
| 79 | get { return string.Format("Unused disk space ({0})", Drive); } |
|---|
| 80 | } |
|---|
| 81 | } |
|---|
| 82 | |
|---|
| 83 | /// <summary> |
|---|
| 84 | /// Class representing a file to be erased. |
|---|
| 85 | /// </summary> |
|---|
| 86 | public class File : FilesystemObject |
|---|
| 87 | { |
|---|
| 88 | internal override List<string> GetPaths() |
|---|
| 89 | { |
|---|
| 90 | List<string> result = new List<string>(); |
|---|
| 91 | result.Add(Path); |
|---|
| 92 | return result; |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | /// <summary> |
|---|
| 97 | /// Represents a folder and its files which are to be erased. |
|---|
| 98 | /// </summary> |
|---|
| 99 | public class Folder : FilesystemObject |
|---|
| 100 | { |
|---|
| 101 | internal override List<string> GetPaths() |
|---|
| 102 | { |
|---|
| 103 | //Get a list to hold all the resulting paths. |
|---|
| 104 | List<string> result = new List<string>(); |
|---|
| 105 | |
|---|
| 106 | //Open the root of the search, including every file matching the pattern |
|---|
| 107 | DirectoryInfo dir = new DirectoryInfo(Path); |
|---|
| 108 | |
|---|
| 109 | //List recursively all the files which match the include pattern. |
|---|
| 110 | string includeMask = IncludeMask; |
|---|
| 111 | if (includeMask.Length == 0) |
|---|
| 112 | includeMask = "*.*"; |
|---|
| 113 | FileInfo[] files = dir.GetFiles(includeMask, SearchOption.AllDirectories); |
|---|
| 114 | |
|---|
| 115 | //Then exclude each file. |
|---|
| 116 | if (ExcludeMask.Length != 0) |
|---|
| 117 | { |
|---|
| 118 | string regex = Regex.Escape(ExcludeMask).Replace("\\*", ".*"). |
|---|
| 119 | Replace("\\?", "."); |
|---|
| 120 | Regex excludePattern = new Regex(regex, RegexOptions.IgnoreCase); |
|---|
| 121 | foreach (FileInfo file in files) |
|---|
| 122 | if (excludePattern.Matches(file.FullName).Count == 0) |
|---|
| 123 | result.Add(file.FullName); |
|---|
| 124 | } |
|---|
| 125 | else |
|---|
| 126 | foreach (FileInfo file in files) |
|---|
| 127 | result.Add(file.FullName); |
|---|
| 128 | |
|---|
| 129 | //Return the filtered list. |
|---|
| 130 | return result; |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | /// <summary> |
|---|
| 134 | /// A wildcard expression stating the condition for the set of files to include. |
|---|
| 135 | /// The include mask is applied before the exclude mask is applied. If this value |
|---|
| 136 | /// is empty, all files and folders within the folder specified is included. |
|---|
| 137 | /// </summary> |
|---|
| 138 | public string IncludeMask |
|---|
| 139 | { |
|---|
| 140 | get { return includeMask; } |
|---|
| 141 | set { includeMask = value; } |
|---|
| 142 | } |
|---|
| 143 | |
|---|
| 144 | /// <summary> |
|---|
| 145 | /// A wildcard expression stating the condition for removing files from the set |
|---|
| 146 | /// of included files. If this value is omitted, all files and folders extracted |
|---|
| 147 | /// by the inclusion mask is erased. |
|---|
| 148 | /// </summary> |
|---|
| 149 | public string ExcludeMask |
|---|
| 150 | { |
|---|
| 151 | get { return excludeMask; } |
|---|
| 152 | set { excludeMask = value; } |
|---|
| 153 | } |
|---|
| 154 | |
|---|
| 155 | /// <summary> |
|---|
| 156 | /// Determines if Eraser should delete the folder after the erase process. |
|---|
| 157 | /// </summary> |
|---|
| 158 | public bool DeleteIfEmpty |
|---|
| 159 | { |
|---|
| 160 | get { return deleteIfEmpty; } |
|---|
| 161 | set { deleteIfEmpty = value; } |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | private string includeMask; |
|---|
| 165 | private string excludeMask; |
|---|
| 166 | private bool deleteIfEmpty; |
|---|
| 167 | } |
|---|
| 168 | |
|---|
| 169 | /// <summary> |
|---|
| 170 | /// The unique identifier for this task. This ID will be persistent across |
|---|
| 171 | /// executions. |
|---|
| 172 | /// </summary> |
|---|
| 173 | public uint ID |
|---|
| 174 | { |
|---|
| 175 | get { return id; } |
|---|
| 176 | set { id = value; } |
|---|
| 177 | } |
|---|
| 178 | |
|---|
| 179 | /// <summary> |
|---|
| 180 | /// The name for this task. This is just an opaque value for the user to |
|---|
| 181 | /// recognize the task. |
|---|
| 182 | /// </summary> |
|---|
| 183 | public string Name |
|---|
| 184 | { |
|---|
| 185 | get { return name; } |
|---|
| 186 | set { name = value; } |
|---|
| 187 | } |
|---|
| 188 | |
|---|
| 189 | /// <summary> |
|---|
| 190 | /// The set of data to erase when this task is executed. |
|---|
| 191 | /// </summary> |
|---|
| 192 | public List<ErasureTarget> Entries |
|---|
| 193 | { |
|---|
| 194 | get { return entries; } |
|---|
| 195 | set { entries = value; } |
|---|
| 196 | } |
|---|
| 197 | |
|---|
| 198 | /// <summary> |
|---|
| 199 | /// The schedule for running the task. |
|---|
| 200 | /// </summary> |
|---|
| 201 | public Schedule Schedule |
|---|
| 202 | { |
|---|
| 203 | get { return schedule; } |
|---|
| 204 | set { schedule = value; } |
|---|
| 205 | } |
|---|
| 206 | |
|---|
| 207 | /// <summary> |
|---|
| 208 | /// Retrieves the log for this task. |
|---|
| 209 | /// </summary> |
|---|
| 210 | public List<LogEntry> Log |
|---|
| 211 | { |
|---|
| 212 | get |
|---|
| 213 | { |
|---|
| 214 | lock (log) |
|---|
| 215 | return log.GetRange(0, log.Count); |
|---|
| 216 | } |
|---|
| 217 | } |
|---|
| 218 | |
|---|
| 219 | /// <summary> |
|---|
| 220 | /// Logs the message and its associated information into the task Log. |
|---|
| 221 | /// </summary> |
|---|
| 222 | /// <param name="entry">The log entry structure representing the log |
|---|
| 223 | /// message.</param> |
|---|
| 224 | internal void LogEntry(LogEntry entry) |
|---|
| 225 | { |
|---|
| 226 | lock (log) |
|---|
| 227 | log.Add(entry); |
|---|
| 228 | } |
|---|
| 229 | |
|---|
| 230 | /// <summary> |
|---|
| 231 | /// The prototype for events handling the progress changed event. |
|---|
| 232 | /// </summary> |
|---|
| 233 | /// <param name="e">The new progress value.</param> |
|---|
| 234 | public delegate void ProgressEventFunction(TaskProgressEventArgs e); |
|---|
| 235 | |
|---|
| 236 | /// <summary> |
|---|
| 237 | /// The event object holding all event handlers. |
|---|
| 238 | /// </summary> |
|---|
| 239 | public event ProgressEventFunction ProgressChanged; |
|---|
| 240 | |
|---|
| 241 | /// <summary> |
|---|
| 242 | /// Broadcasts a ProgressChanged event. |
|---|
| 243 | /// </summary> |
|---|
| 244 | /// <param name="e">The new progress value.</param> |
|---|
| 245 | internal void OnProgressChanged(TaskProgressEventArgs e) |
|---|
| 246 | { |
|---|
| 247 | if (ProgressChanged != null) |
|---|
| 248 | ProgressChanged.Invoke(e); |
|---|
| 249 | } |
|---|
| 250 | |
|---|
| 251 | private uint id; |
|---|
| 252 | private string name; |
|---|
| 253 | private Schedule schedule = Schedule.RunNow; |
|---|
| 254 | private List<ErasureTarget> entries = new List<ErasureTarget>(); |
|---|
| 255 | private List<LogEntry> log = new List<LogEntry>(); |
|---|
| 256 | } |
|---|
| 257 | |
|---|
| 258 | /// <summary> |
|---|
| 259 | /// A Event argument object containing the progress of the task. |
|---|
| 260 | /// </summary> |
|---|
| 261 | public class TaskProgressEventArgs : EventArgs |
|---|
| 262 | { |
|---|
| 263 | /// <summary> |
|---|
| 264 | /// Constructor. |
|---|
| 265 | /// </summary> |
|---|
| 266 | /// <param name="overallProgress">The overall progress of the task.</param> |
|---|
| 267 | /// <param name="currentItemProgress">The progress for the individual |
|---|
| 268 | /// component of the task.</param> |
|---|
| 269 | public TaskProgressEventArgs(Task task, int overallProgress, |
|---|
| 270 | int currentItemProgress) |
|---|
| 271 | { |
|---|
| 272 | this.task = task; |
|---|
| 273 | this.overallProgress = overallProgress; |
|---|
| 274 | this.currentItemProgress = currentItemProgress; |
|---|
| 275 | } |
|---|
| 276 | |
|---|
| 277 | /// <summary> |
|---|
| 278 | /// The executing task. |
|---|
| 279 | /// </summary> |
|---|
| 280 | public Task Task |
|---|
| 281 | { |
|---|
| 282 | get { return task; } |
|---|
| 283 | } |
|---|
| 284 | |
|---|
| 285 | /// <summary> |
|---|
| 286 | /// A number from 0 to 100 detailing the overall progress of the task. |
|---|
| 287 | /// </summary> |
|---|
| 288 | public int OverallProgress |
|---|
| 289 | { |
|---|
| 290 | get { return overallProgress; } |
|---|
| 291 | } |
|---|
| 292 | |
|---|
| 293 | /// <summary> |
|---|
| 294 | /// A number from 0 to 100 detailing the overall progress of the item. |
|---|
| 295 | /// </summary> |
|---|
| 296 | public int CurrentItemProgress |
|---|
| 297 | { |
|---|
| 298 | get { return currentItemProgress; } |
|---|
| 299 | } |
|---|
| 300 | |
|---|
| 301 | /// <summary> |
|---|
| 302 | /// The file name of the item being erased. |
|---|
| 303 | /// </summary> |
|---|
| 304 | public string CurrentItemName |
|---|
| 305 | { |
|---|
| 306 | get { return currentItemName; } |
|---|
| 307 | } |
|---|
| 308 | |
|---|
| 309 | /// <summary> |
|---|
| 310 | /// The pass number of a multi-pass erasure method. |
|---|
| 311 | /// </summary> |
|---|
| 312 | public int CurrentPass |
|---|
| 313 | { |
|---|
| 314 | get { return currentPass; } |
|---|
| 315 | } |
|---|
| 316 | |
|---|
| 317 | /// <summary> |
|---|
| 318 | /// The total number of passes to complete before this erasure method is |
|---|
| 319 | /// completed. |
|---|
| 320 | /// </summary> |
|---|
| 321 | public int TotalPasses |
|---|
| 322 | { |
|---|
| 323 | get { return totalPasses; } |
|---|
| 324 | } |
|---|
| 325 | |
|---|
| 326 | private Task task; |
|---|
| 327 | internal int overallProgress; |
|---|
| 328 | internal int currentItemProgress; |
|---|
| 329 | internal string currentItemName; |
|---|
| 330 | internal int currentPass; |
|---|
| 331 | internal int totalPasses; |
|---|
| 332 | } |
|---|
| 333 | } |
|---|