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

Revision 1360, 4.2 KB checked in by lowjoel, 5 years ago (diff)

Eraser's still under development, so update the copyright notice.

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