Changeset 328
- Timestamp:
- 4/1/2008 11:03:26 AM (5 years ago)
- File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
branches/eraser6/DefaultPlugins/EraseFirstLast16KB.cs
r280 r328 4 4 5 5 using Eraser.Manager; 6 using Eraser.Util; 6 7 using System.IO; 8 using System.Threading; 7 9 8 10 namespace Eraser.DefaultPlugins … … 12 14 public override string Name 13 15 { 14 get { return "First/last 16KB Erasure"; }16 get { return S._("First/last 16KB Erasure"); } 15 17 } 16 18 … … 50 52 //less. 51 53 if (erasureLength != long.MaxValue) 52 throw new ArgumentException( "The amount of data erased should not be " +53 "limited, since this is a self-limiting erasure method.") ;54 throw new ArgumentException(S._("The amount of data erased should not be " + 55 "limited, since this is a self-limiting erasure method.")); 54 56 55 ErasureMethod method = Method; 57 try 58 { 59 //Acquire a lock on the cached method object and set it if unset. 60 cachedMethodSemaphore.WaitOne(); 61 if (cachedMethod == null) 62 cachedMethod = Method; 56 63 57 //If the target stream is shorter than 16kb, just forward it to the default 58 //function. 59 if (strm.Length < dataSize) 64 //If the target stream is shorter than 16kb, just forward it to the default 65 //function. 66 if (strm.Length < dataSize) 67 { 68 cachedMethod.Erase(strm, erasureLength, prng, callback); 69 return; 70 } 71 72 //Seek to the beginning and write 16kb. 73 strm.Seek(0, SeekOrigin.Begin); 74 cachedMethod.Erase(strm, dataSize, prng, callback); 75 76 //Seek to the end - 16kb, and write. 77 strm.Seek(-dataSize, SeekOrigin.End); 78 cachedMethod.Erase(strm, long.MaxValue, prng, callback); 79 } 80 finally 60 81 { 61 method.Erase(strm, erasureLength, prng, callback); 62 return; 82 //Check if no other threads are using the cached erasure method. 83 if (cachedMethodSemaphore.Release() == int.MaxValue - 1) 84 //If so, we no longer need to cache. 85 cachedMethod = null; 63 86 } 64 65 //Seek to the beginning and write 16kb.66 strm.Seek(0, SeekOrigin.Begin);67 method.Erase(strm, dataSize, prng, callback);68 69 //Seek to the end - 16kb, and write.70 strm.Seek(-dataSize, SeekOrigin.End);71 method.Erase(strm, long.MaxValue, prng, callback);72 87 } 73 88 … … 76 91 get 77 92 { 78 //Try to retrieve the default erasure method. 93 //If there are threads locking the cached method variable just 94 //return that 95 if (cachedMethod != null) 96 return cachedMethod; 97 98 //Try to retrieve the set erasure method 79 99 ErasureMethod defaultMethod = ErasureMethodManager.GetInstance( 80 ManagerLibrary.Instance.Settings.DefaultFileErasureMethod);100 (Guid)DefaultPlugin.Settings["FL16Method"]); 81 101 82 //If we are the default, use the default pseudorandom pass. 83 if (defaultMethod.GUID == GUID) 84 defaultMethod = ErasureMethodManager.GetInstance(new Guid( 85 "{BF8BA267-231A-4085-9BF9-204DE65A6641}")); 102 //If we have no default or we are the default then throw an exception 103 if (defaultMethod == null || defaultMethod.GUID == GUID) 104 throw new InvalidOperationException(S._("The First/last 16KB erasure method " + 105 "requires another erasure method to erase the file.\n\nThis can " + 106 "be set in the Plugin Settings dialog.")); 86 107 87 108 return defaultMethod; … … 93 114 /// </summary> 94 115 private const long dataSize = 16 * 1024; 116 117 /// <summary> 118 /// The cached erasure method. This is used when erasing so that the properties 119 /// of this class is not inconsistent in the event that the method of erasure 120 /// changes while executing. 121 /// </summary> 122 ErasureMethod cachedMethod = null; 123 124 /// <summary> 125 /// Tracks the number of erasures locking the cached method variable. 126 /// </summary> 127 Semaphore cachedMethodSemaphore = new Semaphore(int.MaxValue, int.MaxValue); 95 128 } 96 129 }
Note: See TracChangeset
for help on using the changeset viewer.
