source: trunk/eraser6/Eraser.DefaultPlugins/ErasureMethods/FirstLast16KB.cs @ 1485

Revision 1485, 4.4 KB checked in by lowjoel, 4 years ago (diff)

Forward-port r1475 through r1484 to trunk.

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1/*
2 * $Id$
3 * Copyright 2008-2009 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;
25
26using Eraser.Manager;
27using Eraser.Util;
28using System.IO;
29using System.Threading;
30using System.Windows.Forms;
31
32namespace Eraser.DefaultPlugins
33{
34    sealed class FirstLast16KB : ErasureMethod
35    {
36        public FirstLast16KB()
37        {
38            try
39            {
40                //Try to retrieve the set erasure method
41                if (DefaultPlugin.Settings.FL16Method != Guid.Empty)
42                    method = ErasureMethodManager.GetInstance(
43                        DefaultPlugin.Settings.FL16Method);
44                else if (ManagerLibrary.Settings.DefaultFileErasureMethod != Guid)
45                    method = ErasureMethodManager.GetInstance(
46                        ManagerLibrary.Settings.DefaultFileErasureMethod);
47                else
48                    method = ErasureMethodManager.GetInstance(new Gutmann().Guid);
49            }
50            catch (ErasureMethodNotFoundException)
51            {
52                MessageBox.Show(S._("The First/last 16KB erasure method " +
53                    "requires another erasure method to erase the file.\n\nThis must " +
54                    "be set in the Plugin Settings dialog."), Name, MessageBoxButtons.OK,
55                    MessageBoxIcon.Error, MessageBoxDefaultButton.Button1,
56                    S.IsRightToLeft(null) ? MessageBoxOptions.RtlReading : 0);
57            }
58        }
59
60        public override string Name
61        {
62            get { return S._("First/last 16KB Erasure"); }
63        }
64
65        public override int Passes
66        {
67            get { return 0; } //Variable number, depending on defaults.
68        }
69
70        public override Guid Guid
71        {
72            get { return new Guid("{0C2E07BF-0207-49a3-ADE8-46F9E1499C01}"); }
73        }
74
75        public override long CalculateEraseDataSize(ICollection<string> paths, long targetSize)
76        {
77            //If we have no default or we are the default then throw an exception
78            if (method == null || method.Guid == Guid)
79                throw new InvalidOperationException(S._("The First/last 16KB erasure method " +
80                    "requires another erasure method to erase the file.\n\nThis must " +
81                    "be set in the Plugin Settings dialog."));
82
83            //Amount of data required to be written.
84            long amountToWrite = 0;
85            if (paths == null)
86            {
87                if (targetSize <= dataSize)
88                    amountToWrite = targetSize;
89                else
90                    amountToWrite = dataSize * 2;
91            }
92            else
93                amountToWrite = paths.Count * dataSize * 2;
94
95            //The final amount has to be multiplied by the number of passes.
96            return amountToWrite * method.Passes;
97        }
98
99        public override void Erase(Stream strm, long erasureLength, Prng prng,
100            ErasureMethodProgressFunction callback)
101        {
102            //If we have no default or we are the default then throw an exception
103            if (method == null || method.Guid == Guid)
104                throw new InvalidOperationException(S._("The First/last 16KB erasure method " +
105                    "requires another erasure method to erase the file.\n\nThis must " +
106                    "be set in the Plugin Settings dialog."));
107
108            //Make sure that the erasureLength passed in here is the maximum value
109            //for the size of long, since we don't want to write extra or write
110            //less.
111            if (erasureLength != long.MaxValue)
112                throw new ArgumentException(S._("The amount of data erased should not be " +
113                    "limited, since this is a self-limiting erasure method."));
114
115            //If the target stream is shorter than 16kb, just forward it to the default
116            //function.
117            if (strm.Length < dataSize)
118            {
119                method.Erase(strm, erasureLength, prng, callback);
120                return;
121            }
122
123            //Seek to the beginning and write 16kb.
124            strm.Seek(0, SeekOrigin.Begin);
125            method.Erase(strm, dataSize, prng, callback);
126
127            //Seek to the end - 16kb, and write.
128            strm.Seek(-dataSize, SeekOrigin.End);
129            method.Erase(strm, long.MaxValue, prng, callback);
130        }
131
132        /// <summary>
133        /// The amount of data to be erased from the header and the end of the file.
134        /// </summary>
135        private const long dataSize = 16 * 1024;
136
137        private ErasureMethod method;
138    }
139}
Note: See TracBrowser for help on using the repository browser.