source: branches/eraser6/DefaultPlugins/EraseFirstLast16KB.cs @ 334

Revision 334, 2.8 KB checked in by lowjoel, 6 years ago (diff)

Changed the way erasure methods work. Since they are customizable, instances should be created when one is required instead of getting the plugin to create one for us. This allows for settings to persist across a use of the method. This mainly fixes problems with the FL16KB method where users may change the erasure method used when a task is executing.

Line 
1using System;
2using System.Collections.Generic;
3using System.Text;
4
5using Eraser.Manager;
6using Eraser.Util;
7using System.IO;
8using System.Threading;
9
10namespace Eraser.DefaultPlugins
11{
12    class FirstLast16KB : ErasureMethod
13    {
14        public FirstLast16KB()
15        {
16            //Try to retrieve the set erasure method
17            if (DefaultPlugin.Settings.ContainsKey("FL16Method"))
18                method = ErasureMethodManager.GetInstance(
19                    (Guid)DefaultPlugin.Settings["FL16Method"]);
20            else
21                method = ErasureMethodManager.GetInstance(
22                    ManagerLibrary.Instance.Settings.DefaultFileErasureMethod);
23
24            //If we have no default or we are the default then throw an exception
25            if (method == null || method.GUID == GUID)
26                throw new InvalidOperationException(S._("The First/last 16KB erasure method " +
27                    "requires another erasure method to erase the file.\n\nThis must " +
28                    "be set in the Plugin Settings dialog."));
29        }
30
31        public override string Name
32        {
33            get { return S._("First/last 16KB Erasure"); }
34        }
35
36        public override int Passes
37        {
38            get { return 0; } //Variable number, depending on defaults.
39        }
40
41        public override Guid GUID
42        {
43            get { return new Guid("{0C2E07BF-0207-49a3-ADE8-46F9E1499C01}"); }
44        }
45
46        public override long CalculateEraseDataSize(List<string> paths, long targetSize)
47        {
48            //Amount of data required to be written.
49            long amountToWrite = 0;
50            if (paths == null)
51            {
52                if (targetSize <= dataSize)
53                    amountToWrite = targetSize;
54                else
55                    amountToWrite = dataSize * 2;
56            }
57            else
58                amountToWrite = paths.Count * dataSize * 2;
59
60            //The final amount has to be multiplied by the number of passes.
61            return amountToWrite * method.Passes;
62        }
63
64        public override void Erase(Stream strm, long erasureLength, PRNG prng,
65            OnProgress callback)
66        {
67            //Make sure that the erasureLength passed in here is the maximum value
68            //for the size of long, since we don't want to write extra or write
69            //less.
70            if (erasureLength != long.MaxValue)
71                throw new ArgumentException(S._("The amount of data erased should not be " +
72                    "limited, since this is a self-limiting erasure method."));
73
74            //If the target stream is shorter than 16kb, just forward it to the default
75            //function.
76            if (strm.Length < dataSize)
77            {
78                method.Erase(strm, erasureLength, prng, callback);
79                return;
80            }
81
82            //Seek to the beginning and write 16kb.
83            strm.Seek(0, SeekOrigin.Begin);
84            method.Erase(strm, dataSize, prng, callback);
85
86            //Seek to the end - 16kb, and write.
87            strm.Seek(-dataSize, SeekOrigin.End);
88            method.Erase(strm, long.MaxValue, prng, callback);
89        }
90
91        /// <summary>
92        /// The amount of data to be erased from the header and the end of the file.
93        /// </summary>
94        private const long dataSize = 16 * 1024;
95
96        private ErasureMethod method;
97    }
98}
Note: See TracBrowser for help on using the repository browser.