| 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.Text; |
|---|
| 25 | |
|---|
| 26 | using System.IO; |
|---|
| 27 | using Eraser.Manager; |
|---|
| 28 | using Eraser.Util; |
|---|
| 29 | |
|---|
| 30 | namespace Eraser.DefaultPlugins |
|---|
| 31 | { |
|---|
| 32 | /// <summary> |
|---|
| 33 | /// Provides functions to handle erasures specific to FAT volumes. |
|---|
| 34 | /// </summary> |
|---|
| 35 | public abstract class FatFileSystem : WindowsFileSystem |
|---|
| 36 | { |
|---|
| 37 | public override void EraseOldFileSystemResidentFiles(VolumeInfo volume, |
|---|
| 38 | DirectoryInfo tempDirectory, ErasureMethod method, |
|---|
| 39 | FileSystemEntriesEraseProgress callback) |
|---|
| 40 | { |
|---|
| 41 | //Nothing to be done here. FAT doesn't store files in its FAT. |
|---|
| 42 | } |
|---|
| 43 | |
|---|
| 44 | public override void EraseFileSystemObject(StreamInfo info, ErasureMethod method, |
|---|
| 45 | ErasureMethodProgressFunction callback) |
|---|
| 46 | { |
|---|
| 47 | //Create the file stream, and call the erasure method to write to |
|---|
| 48 | //the stream. |
|---|
| 49 | long fileArea = GetFileArea(info.FullName); |
|---|
| 50 | using (FileStream strm = info.Open(FileMode.Open, FileAccess.Write, |
|---|
| 51 | FileShare.None, FileOptions.WriteThrough)) |
|---|
| 52 | { |
|---|
| 53 | //Set the end of the stream after the wrap-round the cluster size |
|---|
| 54 | strm.SetLength(fileArea); |
|---|
| 55 | |
|---|
| 56 | //If the stream is empty, there's nothing to overwrite. Continue |
|---|
| 57 | //to the next entry |
|---|
| 58 | if (strm.Length != 0) |
|---|
| 59 | { |
|---|
| 60 | //Then erase the file. |
|---|
| 61 | method.Erase(strm, long.MaxValue, |
|---|
| 62 | PrngManager.GetInstance(ManagerLibrary.Settings.ActivePrng), |
|---|
| 63 | callback |
|---|
| 64 | ); |
|---|
| 65 | } |
|---|
| 66 | |
|---|
| 67 | //Set the length of the file to 0. |
|---|
| 68 | strm.Seek(0, SeekOrigin.Begin); |
|---|
| 69 | strm.SetLength(0); |
|---|
| 70 | } |
|---|
| 71 | } |
|---|
| 72 | |
|---|
| 73 | public override void EraseDirectoryStructures(VolumeInfo info, |
|---|
| 74 | FileSystemEntriesEraseProgress callback) |
|---|
| 75 | { |
|---|
| 76 | using (FileStream stream = info.Open(FileAccess.ReadWrite, FileShare.ReadWrite)) |
|---|
| 77 | { |
|---|
| 78 | int directoriesCleaned = 0; |
|---|
| 79 | FatApi api = GetFatApi(info, stream); |
|---|
| 80 | HashSet<uint> eraseQueueClusters = new HashSet<uint>(); |
|---|
| 81 | List<FatDirectoryEntry> eraseQueue = new List<FatDirectoryEntry>(); |
|---|
| 82 | { |
|---|
| 83 | FatDirectoryEntry entry = api.LoadDirectory(string.Empty); |
|---|
| 84 | eraseQueue.Add(entry); |
|---|
| 85 | eraseQueueClusters.Add(entry.Cluster); |
|---|
| 86 | } |
|---|
| 87 | |
|---|
| 88 | using (VolumeLock volumeLock = info.LockVolume(stream)) |
|---|
| 89 | { |
|---|
| 90 | while (eraseQueue.Count != 0) |
|---|
| 91 | { |
|---|
| 92 | if (callback != null) |
|---|
| 93 | callback(directoriesCleaned, directoriesCleaned + eraseQueue.Count); |
|---|
| 94 | |
|---|
| 95 | FatDirectoryBase currentDir = api.LoadDirectory(eraseQueue[0].FullName); |
|---|
| 96 | eraseQueue.RemoveAt(0); |
|---|
| 97 | |
|---|
| 98 | //Queue the subfolders in this directory |
|---|
| 99 | foreach (KeyValuePair<string, FatDirectoryEntry> entry in currentDir.Items) |
|---|
| 100 | if (entry.Value.EntryType == FatDirectoryEntryType.Directory) |
|---|
| 101 | { |
|---|
| 102 | //Check that we don't have the same cluster queued twice (e.g. for |
|---|
| 103 | //long/8.3 file names) |
|---|
| 104 | if (eraseQueueClusters.Contains(entry.Value.Cluster)) |
|---|
| 105 | continue; |
|---|
| 106 | |
|---|
| 107 | eraseQueueClusters.Add(entry.Value.Cluster); |
|---|
| 108 | eraseQueue.Add(entry.Value); |
|---|
| 109 | } |
|---|
| 110 | |
|---|
| 111 | currentDir.ClearDeletedEntries(); |
|---|
| 112 | ++directoriesCleaned; |
|---|
| 113 | } |
|---|
| 114 | } |
|---|
| 115 | } |
|---|
| 116 | } |
|---|
| 117 | |
|---|
| 118 | protected override DateTime MinTimestamp |
|---|
| 119 | { |
|---|
| 120 | get |
|---|
| 121 | { |
|---|
| 122 | return new DateTime(1980, 1, 1, 0, 0, 0); |
|---|
| 123 | } |
|---|
| 124 | } |
|---|
| 125 | |
|---|
| 126 | /// <summary> |
|---|
| 127 | /// Gets the FAT API to use to interface with the disk. |
|---|
| 128 | /// </summary> |
|---|
| 129 | protected abstract FatApi GetFatApi(VolumeInfo info, FileStream stream); |
|---|
| 130 | } |
|---|
| 131 | |
|---|
| 132 | public class Fat12FileSystem : FatFileSystem |
|---|
| 133 | { |
|---|
| 134 | public override bool Supports(string fileSystemName) |
|---|
| 135 | { |
|---|
| 136 | if (fileSystemName == "FAT12") |
|---|
| 137 | return true; |
|---|
| 138 | return false; |
|---|
| 139 | } |
|---|
| 140 | |
|---|
| 141 | protected override FatApi GetFatApi(VolumeInfo info, FileStream stream) |
|---|
| 142 | { |
|---|
| 143 | return new Fat12Api(info, stream); |
|---|
| 144 | } |
|---|
| 145 | } |
|---|
| 146 | |
|---|
| 147 | public class Fat16FileSystem : FatFileSystem |
|---|
| 148 | { |
|---|
| 149 | public override bool Supports(string fileSystemName) |
|---|
| 150 | { |
|---|
| 151 | if (fileSystemName == "FAT16") |
|---|
| 152 | return true; |
|---|
| 153 | return false; |
|---|
| 154 | } |
|---|
| 155 | |
|---|
| 156 | protected override FatApi GetFatApi(VolumeInfo info, FileStream stream) |
|---|
| 157 | { |
|---|
| 158 | return new Fat16Api(info, stream); |
|---|
| 159 | } |
|---|
| 160 | } |
|---|
| 161 | |
|---|
| 162 | public class Fat32FileSystem : FatFileSystem |
|---|
| 163 | { |
|---|
| 164 | public override bool Supports(string fileSystemName) |
|---|
| 165 | { |
|---|
| 166 | if (fileSystemName == "FAT32") |
|---|
| 167 | return true; |
|---|
| 168 | return false; |
|---|
| 169 | } |
|---|
| 170 | |
|---|
| 171 | protected override FatApi GetFatApi(VolumeInfo info, FileStream stream) |
|---|
| 172 | { |
|---|
| 173 | return new Fat32Api(info, stream); |
|---|
| 174 | } |
|---|
| 175 | } |
|---|
| 176 | } |
|---|