Changeset 328


Ignore:
Timestamp:
4/1/2008 11:03:26 AM (7 years ago)
Author:
lowjoel
Message:

The FirstLast? 16KB erasure method now allows explicit setting of the erasure method to use, since the FL16kb method is merely a filter. This is held across tasks for as long a task is running.

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/DefaultPlugins/EraseFirstLast16KB.cs

    r280 r328  
    44 
    55using Eraser.Manager; 
     6using Eraser.Util; 
    67using System.IO; 
     8using System.Threading; 
    79 
    810namespace Eraser.DefaultPlugins 
     
    1214        public override string Name 
    1315        { 
    14             get { return "First/last 16KB Erasure"; } 
     16            get { return S._("First/last 16KB Erasure"); } 
    1517        } 
    1618 
     
    5052            //less. 
    5153            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.")); 
    5456 
    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; 
    5663 
    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 
    6081            { 
    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; 
    6386            } 
    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); 
    7287        } 
    7388 
     
    7691            get 
    7792            { 
    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 
    7999                ErasureMethod defaultMethod = ErasureMethodManager.GetInstance( 
    80                     ManagerLibrary.Instance.Settings.DefaultFileErasureMethod); 
     100                    (Guid)DefaultPlugin.Settings["FL16Method"]); 
    81101 
    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.")); 
    86107 
    87108                return defaultMethod; 
     
    93114        /// </summary> 
    94115        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); 
    95128    } 
    96129} 
Note: See TracChangeset for help on using the changeset viewer.