| 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 | |
|---|
| 22 | using System; |
|---|
| 23 | using System.Collections.Generic; |
|---|
| 24 | using System.Linq; |
|---|
| 25 | using System.Text; |
|---|
| 26 | |
|---|
| 27 | using System.Runtime.Serialization; |
|---|
| 28 | using System.Runtime.InteropServices; |
|---|
| 29 | using System.IO; |
|---|
| 30 | |
|---|
| 31 | using Eraser.Manager; |
|---|
| 32 | using Eraser.Util; |
|---|
| 33 | |
|---|
| 34 | namespace Eraser.DefaultPlugins |
|---|
| 35 | { |
|---|
| 36 | [Serializable] |
|---|
| 37 | [Guid("A1FA7354-0258-4903-88E9-0D31FC5F8D51")] |
|---|
| 38 | public class RecycleBinErasureTarget : FileSystemObjectErasureTarget |
|---|
| 39 | { |
|---|
| 40 | #region Serialization code |
|---|
| 41 | protected RecycleBinErasureTarget(SerializationInfo info, StreamingContext context) |
|---|
| 42 | : base(info, context) |
|---|
| 43 | { |
|---|
| 44 | } |
|---|
| 45 | #endregion |
|---|
| 46 | |
|---|
| 47 | public RecycleBinErasureTarget() |
|---|
| 48 | { |
|---|
| 49 | } |
|---|
| 50 | |
|---|
| 51 | public override Guid Guid |
|---|
| 52 | { |
|---|
| 53 | get { return GetType().GUID; } |
|---|
| 54 | } |
|---|
| 55 | |
|---|
| 56 | public override IErasureTargetConfigurer Configurer |
|---|
| 57 | { |
|---|
| 58 | get { return null; } |
|---|
| 59 | } |
|---|
| 60 | |
|---|
| 61 | internal override List<string> GetPaths(out long totalSize) |
|---|
| 62 | { |
|---|
| 63 | totalSize = 0; |
|---|
| 64 | List<string> result = new List<string>(); |
|---|
| 65 | string[] rootDirectory = new string[] { |
|---|
| 66 | "$RECYCLE.BIN", |
|---|
| 67 | "RECYCLER" |
|---|
| 68 | }; |
|---|
| 69 | |
|---|
| 70 | foreach (DriveInfo drive in DriveInfo.GetDrives()) |
|---|
| 71 | { |
|---|
| 72 | foreach (string rootDir in rootDirectory) |
|---|
| 73 | { |
|---|
| 74 | DirectoryInfo dir = new DirectoryInfo( |
|---|
| 75 | System.IO.Path.Combine( |
|---|
| 76 | System.IO.Path.Combine(drive.Name, rootDir), |
|---|
| 77 | System.Security.Principal.WindowsIdentity.GetCurrent(). |
|---|
| 78 | User.ToString())); |
|---|
| 79 | if (!dir.Exists) |
|---|
| 80 | continue; |
|---|
| 81 | |
|---|
| 82 | GetRecyclerFiles(dir, result, ref totalSize); |
|---|
| 83 | } |
|---|
| 84 | } |
|---|
| 85 | |
|---|
| 86 | return result; |
|---|
| 87 | } |
|---|
| 88 | |
|---|
| 89 | /// <summary> |
|---|
| 90 | /// Retrieves all files within this folder, without exclusions. |
|---|
| 91 | /// </summary> |
|---|
| 92 | /// <param name="info">The DirectoryInfo object representing the folder to traverse.</param> |
|---|
| 93 | /// <param name="paths">The list of files to store path information in.</param> |
|---|
| 94 | /// <param name="totalSize">Receives the total size of the files.</param> |
|---|
| 95 | private void GetRecyclerFiles(DirectoryInfo info, List<string> paths, |
|---|
| 96 | ref long totalSize) |
|---|
| 97 | { |
|---|
| 98 | try |
|---|
| 99 | { |
|---|
| 100 | foreach (FileInfo fileInfo in info.GetFiles()) |
|---|
| 101 | { |
|---|
| 102 | if (!fileInfo.Exists || (fileInfo.Attributes & FileAttributes.ReparsePoint) != 0) |
|---|
| 103 | continue; |
|---|
| 104 | |
|---|
| 105 | long adsSize = 0; |
|---|
| 106 | GetPathADSes(paths, out adsSize, fileInfo.FullName); |
|---|
| 107 | totalSize += adsSize; |
|---|
| 108 | totalSize += fileInfo.Length; |
|---|
| 109 | paths.Add(fileInfo.FullName); |
|---|
| 110 | } |
|---|
| 111 | |
|---|
| 112 | foreach (DirectoryInfo directoryInfo in info.GetDirectories()) |
|---|
| 113 | if ((directoryInfo.Attributes & FileAttributes.ReparsePoint) == 0) |
|---|
| 114 | GetRecyclerFiles(directoryInfo, paths, ref totalSize); |
|---|
| 115 | } |
|---|
| 116 | catch (UnauthorizedAccessException e) |
|---|
| 117 | { |
|---|
| 118 | Logger.Log(e.Message, LogLevel.Error); |
|---|
| 119 | } |
|---|
| 120 | } |
|---|
| 121 | |
|---|
| 122 | /// <summary> |
|---|
| 123 | /// Retrieves the text to display representing this task. |
|---|
| 124 | /// </summary> |
|---|
| 125 | public override string UIText |
|---|
| 126 | { |
|---|
| 127 | get |
|---|
| 128 | { |
|---|
| 129 | return S._("Recycle Bin"); |
|---|
| 130 | } |
|---|
| 131 | } |
|---|
| 132 | |
|---|
| 133 | public override void Execute() |
|---|
| 134 | { |
|---|
| 135 | Progress = new SteppedProgressManager(); |
|---|
| 136 | try |
|---|
| 137 | { |
|---|
| 138 | base.Execute(); |
|---|
| 139 | |
|---|
| 140 | ProgressManager step = new ProgressManager(); |
|---|
| 141 | Progress.Steps.Add(new SteppedProgressManagerStep(step, |
|---|
| 142 | 0.0f, )); |
|---|
| 143 | OnProgressChanged(this, new ProgressChangedEventArgs(step, |
|---|
| 144 | new TaskProgressChangedEventArgs(string.Empty, 0, 0))); |
|---|
| 145 | |
|---|
| 146 | RecycleBin.Empty(EmptyRecycleBinOptions.NoConfirmation | |
|---|
| 147 | EmptyRecycleBinOptions.NoProgressUI | EmptyRecycleBinOptions.NoSound); |
|---|
| 148 | } |
|---|
| 149 | finally |
|---|
| 150 | { |
|---|
| 151 | Progress = null; |
|---|
| 152 | } |
|---|
| 153 | } |
|---|
| 154 | } |
|---|
| 155 | } |
|---|