source: trunk/eraser/Eraser.DefaultPlugins/FileSystems/Fat.cs @ 2189

Revision 2189, 5.1 KB checked in by lowjoel, 4 years ago (diff)
  • Define a VolumeStream? class which is a FileStream? customised for raw volume access. This results in the VolumeLock? class being merged into the VolumeStream? class, automatically handling locking and unlocking of volumes.
  • Change all internal OpenHandle? functions to private, since they should not be callable from outside the class.
  • Perform error checking in OpenHandle? so that even callers of OpenHandle? gets error checking.
  • 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.Text;
25using System.Runtime.InteropServices;
26
27using System.IO;
28using Eraser.Manager;
29using Eraser.Util;
30
31namespace Eraser.DefaultPlugins
32{
33    /// <summary>
34    /// Provides functions to handle erasures specific to FAT volumes.
35    /// </summary>
36    public abstract class FatFileSystem : WindowsFileSystem
37    {
38        public override void EraseOldFileSystemResidentFiles(VolumeInfo volume,
39            DirectoryInfo tempDirectory, ErasureMethod method,
40            FileSystemEntriesEraseProgress callback)
41        {
42            //Nothing to be done here. FAT doesn't store files in its FAT.
43        }
44
45        public override void EraseFileSystemObject(StreamInfo info, ErasureMethod method,
46            ErasureMethodProgressFunction callback)
47        {
48            //Create the file stream, and call the erasure method to write to
49            //the stream.
50            long fileArea = GetFileArea(info);
51            using (FileStream strm = info.Open(FileMode.Open, FileAccess.Write,
52                FileShare.None, FileOptions.WriteThrough))
53            {
54                //Set the end of the stream after the wrap-round the cluster size
55                strm.SetLength(fileArea);
56
57                //If the stream is empty, there's nothing to overwrite. Continue
58                //to the next entry
59                if (strm.Length != 0)
60                {
61                    //Then erase the file.
62                    method.Erase(strm, long.MaxValue,
63                        ManagerLibrary.Instance.PrngRegistrar[ManagerLibrary.Settings.ActivePrng],
64                        callback
65                    );
66                }
67
68                //Set the length of the file to 0.
69                strm.Seek(0, SeekOrigin.Begin);
70                strm.SetLength(0);
71            }
72        }
73
74        public override void EraseDirectoryStructures(VolumeInfo info,
75            FileSystemEntriesEraseProgress callback)
76        {
77            using (FileStream stream = info.Open(FileAccess.ReadWrite, FileShare.ReadWrite))
78            {
79                int directoriesCleaned = 0;
80                FatApi api = GetFatApi(info, stream);
81                HashSet<uint> eraseQueueClusters = new HashSet<uint>();
82                List<FatDirectoryEntry> eraseQueue = new List<FatDirectoryEntry>();
83                {
84                    FatDirectoryEntry entry = api.LoadDirectory(string.Empty);
85                    eraseQueue.Add(entry);
86                    eraseQueueClusters.Add(entry.Cluster);
87                }
88
89                try
90                {
91                    while (eraseQueue.Count != 0)
92                    {
93                        if (callback != null)
94                            callback(directoriesCleaned, directoriesCleaned + eraseQueue.Count);
95
96                        FatDirectoryBase currentDir = api.LoadDirectory(eraseQueue[0].FullName);
97                        eraseQueue.RemoveAt(0);
98
99                        //Queue the subfolders in this directory
100                        foreach (KeyValuePair<string, FatDirectoryEntry> entry in currentDir.Items)
101                            if (entry.Value.EntryType == FatDirectoryEntryType.Directory)
102                            {
103                                //Check that we don't have the same cluster queued twice (e.g. for
104                                //long/8.3 file names)
105                                if (eraseQueueClusters.Contains(entry.Value.Cluster))
106                                    continue;
107
108                                eraseQueueClusters.Add(entry.Value.Cluster);
109                                eraseQueue.Add(entry.Value);
110                            }
111
112                        currentDir.ClearDeletedEntries();
113                        ++directoriesCleaned;
114                    }
115                }
116                catch (SharingViolationException)
117                {
118                    Logger.Log(S._("Could not erase directory entries on the volume {0} because " +
119                        "the volume is currently in use."));
120                }
121            }
122        }
123
124        protected override DateTime MinTimestamp
125        {
126            get
127            {
128                return new DateTime(1980, 1, 1, 0, 0, 0);
129            }
130        }
131
132        /// <summary>
133        ///  Gets the FAT API to use to interface with the disk.
134        /// </summary>
135        protected abstract FatApi GetFatApi(VolumeInfo info, FileStream stream);
136    }
137
138    [Guid("36C78D78-7EE4-4304-8068-10755651AF2D")]
139    public class Fat12FileSystem : FatFileSystem
140    {
141        public override Guid Guid
142        {
143            get { return GetType().GUID; }
144        }
145
146        public override string Name
147        {
148            get { return "FAT12"; }
149        }
150
151        protected override FatApi GetFatApi(VolumeInfo info, FileStream stream)
152        {
153            return new Fat12Api(info, stream);
154        }
155    }
156
157    [Guid("8C9DF746-1CD6-435d-8D04-3FE40A0A1C83")]
158    public class Fat16FileSystem : FatFileSystem
159    {
160        public override Guid Guid
161        {
162            get { return GetType().GUID; }
163        }
164
165        public override string Name
166        {
167            get { return "FAT16"; }
168        }
169
170        protected override FatApi GetFatApi(VolumeInfo info, FileStream stream)
171        {
172            return new Fat16Api(info, stream);
173        }
174    }
175
176    [Guid("1FCD66DC-179D-4402-8FF8-D19F74A4C398")]
177    public class Fat32FileSystem : FatFileSystem
178    {
179        public override Guid Guid
180        {
181            get { return GetType().GUID; }
182        }
183
184        public override string Name
185        {
186            get { return "FAT32"; }
187        }
188
189        protected override FatApi GetFatApi(VolumeInfo info, FileStream stream)
190        {
191            return new Fat32Api(info, stream);
192        }
193    }
194}
Note: See TracBrowser for help on using the repository browser.