source: branches/eraser6/pluginsRewrite/Eraser.Plugins/ExtensionPoints/FileSystem.cs @ 2360

Revision 2360, 9.9 KB checked in by lowjoel, 19 months ago (diff)

Move the CopyPlausibleDeniabilityFile? function from the FileSystem? class to the FileSystemObjectErasureTarget? base class, since that's the only place this setting should be needed.

Line 
1/*
2 * $Id: FileSystem.cs 2161 2010-06-13 02:30:16Z lowjoel $
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.IO;
28
29using Eraser.Util;
30
31namespace Eraser.Plugins.ExtensionPoints
32{
33    /// <summary>
34    /// Provides functions to handle erasures specfic to file systems.
35    /// </summary>
36    public abstract class FileSystem : IRegisterable
37    {
38        /// <summary>
39        /// Generates a random file name with the given length.
40        /// </summary>
41        /// <remarks>The generated file name is guaranteed not to exist.</remarks>
42        /// <param name="info">The directory to generate the file name in. This
43        /// parameter can be null to indicate merely a random file name</param>
44        /// <param name="length">The length of the file name to generate.</param>
45        /// <returns>A full path to a file containing random file name.</returns>
46        public static string GenerateRandomFileName(DirectoryInfo info, int length)
47        {
48            //Get the PRNG we are going to use
49            Prng prng = Host.Instance.Prngs.ActivePrng;
50
51            //Initialsie the base name, if any.
52            string resultPrefix = info == null ? string.Empty : info.FullName +
53                Path.DirectorySeparatorChar;
54
55            //Variables to store the intermediates.
56            byte[] resultAry = new byte[length];
57            string result = string.Empty;
58            List<string> prohibitedFileNames = new List<string>(new string[] {
59                "CON", "PRN", "AUX", "NUL", "COM1", "COM2", "COM3", "COM4",
60                "COM5", "COM6", "COM7", "COM8", "COM9", "LPT1", "LPT2", "LPT3",
61                "LPT4", "LPT5", "LPT6", "LPT7", "LPT8", "LPT9"
62            });
63
64            do
65            {
66                prng.NextBytes(resultAry);
67
68                //Validate the name
69                string validFileNameChars = "0123456789abcdefghijklmnopqrstuvwxyz" +
70                    "ABCDEFGHIJKLMNOPQRSTUVWXYZ _+=-()[]{}',`~!";
71                for (int j = 0, k = resultAry.Length; j < k; ++j)
72                {
73                    resultAry[j] = (byte)validFileNameChars[
74                        (int)resultAry[j] % validFileNameChars.Length];
75
76                    if (j == 0 || j == k - 1)
77                        //The first or last character cannot be whitespace
78                        while (Char.IsWhiteSpace((char)resultAry[j]))
79                            resultAry[j] = (byte)validFileNameChars[
80                                (int)resultAry[j] % validFileNameChars.Length];
81                }
82
83                result = Encoding.UTF8.GetString(resultAry);
84            }
85            while (info != null &&
86                prohibitedFileNames.IndexOf(Path.GetFileNameWithoutExtension(result)) != -1 ||
87                (Directory.Exists(resultPrefix + result) || File.Exists(resultPrefix + result)));
88            return resultPrefix + result;
89        }
90
91        /// <summary>
92        /// Gets a random file from within the provided directory.
93        /// </summary>
94        /// <param name="info">The directory to get a random file name from.</param>
95        /// <returns>A string containing the full path to the file.</returns>
96        public static string GetRandomFile(DirectoryInfo info)
97        {
98            //First retrieve the list of files and folders in the provided directory.
99            FileSystemInfo[] entries = null;
100            try
101            {
102                entries = info.GetFileSystemInfos();
103            }
104            catch (DirectoryNotFoundException)
105            {
106                return string.Empty;
107            }
108            if (entries.Length == 0)
109                return string.Empty;
110
111            //Find a random entry.
112            Prng prng = Host.Instance.Prngs.ActivePrng;
113            string result = string.Empty;
114            while (result.Length == 0)
115            {
116                int index = prng.Next(entries.Length - 1);
117                if (entries[index] is DirectoryInfo)
118                    result = GetRandomFile((DirectoryInfo)entries[index]);
119                else
120                    result = ((FileInfo)entries[index]).FullName;
121            }
122
123            return result;
124        }
125
126        /// <summary>
127        /// The Guid of the current filesystem.
128        /// </summary>
129        public abstract Guid Guid
130        {
131            get;
132        }
133
134        /// <summary>
135        /// The name of the current filesystem.
136        /// </summary>
137        public abstract string Name
138        {
139            get;
140        }
141
142        /// <summary>
143        /// Resets the created, modified, accessed and last update times for the given
144        /// <paramref name="info"/>.
145        /// </summary>
146        /// <param name="info">The file to reset times.</param>
147        public abstract void ResetFileTimes(FileSystemInfo info);
148
149        /// <summary>
150        /// Securely deletes the file reference from the directory structures
151        /// as well as resetting the Date Created, Date Accessed and Date Modified
152        /// records.
153        /// </summary>
154        /// <param name="info">The file to delete.</param>
155        public abstract void DeleteFile(FileInfo info);
156
157        /// <summary>
158        /// Securely deletes the folder reference from the directory structures
159        /// as well as all subfolders and files, resetting the Date Created, Date
160        /// Accessed and Date Modified records.
161        /// </summary>
162        /// <param name="info">The folder to delete</param>
163        /// <param name="recursive">True if the folder and all its subfolders and
164        /// files to be securely deleted.</param>
165        public abstract void DeleteFolder(DirectoryInfo info, bool recursive);
166
167        /// <seealso cref="DeleteFolder"/>
168        /// <param name="info">The folder to delete.</param>
169        public void DeleteFolder(DirectoryInfo info)
170        {
171            DeleteFolder(info, true);
172        }
173
174        /// <summary>
175        /// The function prototype for cluster tip search progress callbacks. This is
176        /// called when the cluster tips are being searched.
177        /// </summary>
178        /// <param name="currentPath">The directory being searched</param>
179        public delegate void ClusterTipsSearchProgress(string currentPath);
180
181        /// <summary>
182        /// The function prototype for cluster tip erasure callbacks. This is called when
183        /// the cluster tips are being erased.
184        /// </summary>
185        /// <param name="currentFile">The current file index being erased.</param>
186        /// <param name="totalFiles">The total number of files to be erased.</param>
187        /// <param name="currentFilePath">The path to the current file being erased.</param>
188        public delegate void ClusterTipsEraseProgress(int currentFile, int totalFiles,
189            string currentFilePath);
190
191        /// <summary>
192        /// The prototype of callbacks handling the file system table erase progress.
193        /// </summary>
194        /// <param name="currentFile">The current file being erased.</param>
195        /// <param name="totalFiles">The estimated number of files that must be
196        /// erased.</param>
197        public delegate void FileSystemEntriesEraseProgress(int currentFile, int totalFiles);
198
199        /// <summary>
200        /// Erases all file cluster tips in the given volume.
201        /// </summary>
202        /// <param name="info">The volume to search for file cluster tips and erase them.</param>
203        /// <param name="method">The erasure method being employed.</param>
204        /// <param name="log">The log manager instance that tracks log messages.</param>
205        /// <param name="searchCallback">The callback function for search progress.</param>
206        /// <param name="eraseCallback">The callback function for erasure progress.</param>
207        public abstract void EraseClusterTips(VolumeInfo info, ErasureMethod method,
208            ClusterTipsSearchProgress searchCallback, ClusterTipsEraseProgress eraseCallback);
209
210        /// <summary>
211        /// Erases old file system table-resident files. This creates small one-byte
212        /// files until disk is full. This will erase unused space which was used for
213        /// files resident in the file system table.
214        /// </summary>
215        /// <param name="volume">The directory information structure containing
216        /// the path to store the temporary one-byte files. The file system table
217        /// of that drive will be erased.</param>
218        /// <param name="tempDirectory">The directory structure containing the path
219        /// to store temporary files used for resident file cleaning.</param>
220        /// <param name="method">The method used to erase the files.</param>
221        public abstract void EraseOldFileSystemResidentFiles(VolumeInfo volume,
222            DirectoryInfo tempDirectory, ErasureMethod method,
223            FileSystemEntriesEraseProgress callback);
224
225        /// <summary>
226        /// Erases the unused space in the main filesystem structures by creating,
227        /// files until the table grows.
228        ///
229        /// This will overwrite unused portions of the table which were previously
230        /// used to store file entries.
231        /// </summary>
232        /// <param name="info">The directory information structure containing
233        /// the path to store the temporary files.</param>
234        /// <param name="callback">The callback function to handle the progress
235        /// of the file system entry erasure.</param>
236        public abstract void EraseDirectoryStructures(VolumeInfo info,
237            FileSystemEntriesEraseProgress callback);
238
239        /// <summary>
240        /// Erases the file system object from the drive.
241        /// </summary>
242        /// <param name="info"></param>
243        public abstract void EraseFileSystemObject(StreamInfo info, ErasureMethod method,
244            ErasureMethod.ErasureMethodProgressFunction callback);
245
246        //TODO: This is supposed to be in VolumeInfo!
247        /// <summary>
248        /// Retrieves the size of the file on disk, calculated by the amount of
249        /// clusters allocated by it.
250        /// </summary>
251        /// <param name="streamInfo">The Stream to get the area for.</param>
252        /// <returns>The area of the file.</returns>
253        public abstract long GetFileArea(StreamInfo filePath);
254
255        /// <summary>
256        /// The number of times file names are renamed to erase the file name from
257        /// the file system table.
258        /// </summary>
259        public const int FileNameErasePasses = 7;
260
261        /// <summary>
262        /// The maximum number of times Eraser tries to erase a file/folder before
263        /// it gives up.
264        /// </summary>
265        public const int FileNameEraseTries = 50;
266    }
267}
Note: See TracBrowser for help on using the repository browser.