source: branches/eraser6/EraserService/Eraser.DefaultPlugins/ErasureTargets/RecycleBinErasureTarget.cs @ 2737

Revision 2737, 5.4 KB checked in by lowjoel, 2 years ago (diff)

Updated to r2734 of trunk.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Author Date Id Rev URL
Line 
1/*
2 * $Id$
3 * Copyright 2008-2012 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.Runtime.Serialization;
28using System.Runtime.InteropServices;
29using System.IO;
30
31using Eraser.Util;
32using Eraser.Plugins;
33using Eraser.Plugins.ExtensionPoints;
34using Microsoft.Win32;
35
36namespace Eraser.DefaultPlugins
37{
38    [Serializable]
39    [Guid("A1FA7354-0258-4903-88E9-0D31FC5F8D51")]
40    public class RecycleBinErasureTarget : FileSystemObjectErasureTarget
41    {
42        #region Serialization code
43        protected RecycleBinErasureTarget(SerializationInfo info, StreamingContext context)
44            : base(info, context)
45        {
46        }
47        #endregion
48
49        public RecycleBinErasureTarget()
50        {
51        }
52
53        public override Guid Guid
54        {
55            get { return GetType().GUID; }
56        }
57
58        public override string Name
59        {
60            get { return S._("Recycle Bin"); }
61        }
62
63        /// <summary>
64        /// Retrieves the text to display representing this task.
65        /// </summary>
66        public override string ToString()
67        {
68            return S._("Recycle Bin");
69        }
70
71        public override IErasureTargetConfigurer Configurer
72        {
73            get { return new RecycleBinErasureTargetConfigurer(); }
74        }
75
76        protected override List<StreamInfo> GetPaths()
77        {
78            List<DirectoryInfo> directories = new List<DirectoryInfo>();
79            string[] rootDirectory = new string[] {
80                    "$RECYCLE.BIN",
81                    "RECYCLER",
82                    "RECYCLED"
83                };
84            string userSid = System.Security.Principal.WindowsIdentity.GetCurrent().
85                User.ToString();
86
87            //First try to get the recycle bin on each of of the physical volumes we have
88            foreach (VolumeInfo volume in VolumeInfo.Volumes)
89            {
90                if (!volume.IsMounted)
91                    continue;
92
93                foreach (string rootDir in rootDirectory)
94                {
95                    //First get the global recycle bin for the current drive
96                    string recycleBinPath = System.IO.Path.Combine(
97                        volume.MountPoints[0].FullName, rootDir);
98                    if (!Directory.Exists(recycleBinPath))
99                        continue;
100
101                    //Try to see if we can get the user's own recycle bin
102                    if (Directory.Exists(System.IO.Path.Combine(recycleBinPath, userSid)))
103                        recycleBinPath = System.IO.Path.Combine(recycleBinPath, userSid);
104
105                    directories.Add(new DirectoryInfo(recycleBinPath));
106                }
107            }
108
109            //Then try the Shell's known folders for Vista and later
110            using (RegistryKey key = Registry.CurrentUser.OpenSubKey(
111                "Software\\Microsoft\\Windows\\CurrentVersion\\Explorer\\BitBucket\\KnownFolder"))
112            {
113                if (key != null)
114                {
115                    string[] knownFolders = key.GetSubKeyNames();
116                    foreach (string stringGuid in knownFolders)
117                    {
118                        Guid guid = new Guid(stringGuid);
119                        DirectoryInfo info = Shell.KnownFolderIDs.GetPath(guid);
120
121                        if (info == null)
122                            continue;
123
124                        foreach (string rootDir in rootDirectory)
125                        {
126                            //Known folders belong to the current user, so they do not store
127                            //objects in a folder with the user's SID
128                            string recycleBinPath = System.IO.Path.Combine(info.FullName, rootDir);
129                            if (!Directory.Exists(recycleBinPath))
130                                continue;
131
132                            directories.Add(new DirectoryInfo(recycleBinPath));
133                        }
134                    }
135                }
136            }
137
138            //Then get all the files in each of the directories
139            List<StreamInfo> result = new List<StreamInfo>();
140            foreach (DirectoryInfo directory in directories)
141                foreach (FileInfo file in GetFiles(directory))
142                {
143                    try
144                    {
145                        //Add the ADSes
146                        result.AddRange(GetPathADSes(file));
147
148                        //Then the file itself
149                        result.Add(new StreamInfo(file.FullName));
150                    }
151                    catch (FileNotFoundException)
152                    {
153                        Logger.Log(S._("The file {0} was not erased because it was deleted " +
154                            "before it could be erased.", file.FullName), LogLevel.Information);
155                    }
156                    catch (DirectoryNotFoundException)
157                    {
158                        Logger.Log(S._("The file {0} was not erased because the containing " +
159                            "directory was deleted before it could be erased.", file.FullName),
160                            LogLevel.Information);
161                    }
162                    catch (SharingViolationException)
163                    {
164                        Logger.Log(S._("Could not list the Alternate Data Streams for file {0} " +
165                            "because the file is being used by another process. The file will not " +
166                            "be erased.", file.FullName), LogLevel.Error);
167                    }
168                }
169
170            return result;
171        }
172
173        public override void Execute()
174        {
175            Progress = new SteppedProgressManager();
176
177            try
178            {
179                base.Execute();
180
181                //Empty the contents of the Recycle Bin
182                EmptyRecycleBin();
183            }
184            finally
185            {
186                Progress = null;
187            }
188        }
189
190        private void EmptyRecycleBin()
191        {
192            ProgressManager progress = new ProgressManager();
193            Progress.Steps.Add(new SteppedProgressManagerStep(progress,
194                0.0f, S._("Emptying recycle bin...")));
195
196            RecycleBin.Empty(EmptyRecycleBinOptions.NoConfirmation |
197                EmptyRecycleBinOptions.NoProgressUI | EmptyRecycleBinOptions.NoSound);
198        }
199    }
200}
Note: See TracBrowser for help on using the repository browser.