Ignore:
Timestamp:
3/12/2008 5:34:32 AM (7 years ago)
Author:
lowjoel
Message:

-Change the RemoveFile? and RemoveFolder? functions to be static since they don't need the this reference
-Factor the random filename generator code out to a separate function
-Guard all MoveTo? files with an exception handler since antivirus programs may lock the file temporally

File:
1 edited

Legend:

Unmodified
Added
Removed
  • branches/eraser6/Manager/DirectExecutor.cs

    r251 r260  
    361361                { 
    362362                    //Set the end of the stream after the wrap-round the cluster size 
    363                     uint clusterSize = Drives.GetDriveClusterSize(info.Directory.Root.FullName); 
     363                    uint clusterSize = Drive.GetClusterSize(info.Directory.Root.FullName); 
    364364                    long roundUpFileLength = strm.Length % clusterSize; 
    365365                    if (roundUpFileLength != 0) 
     
    419419        /// </summary> 
    420420        /// <param name="info">The FileInfo object representing the file.</param> 
    421         private void RemoveFile(FileInfo info) 
     421        private static void RemoveFile(FileInfo info) 
    422422        { 
    423423            //Set the date of the file to be invalid to prevent forensic 
     
    431431            for (int i = 0; i < FilenameErasePasses; ++i) 
    432432            { 
    433                 //Get a random file name 
    434                 PRNG prng = PRNGManager.GetInstance(Globals.Settings.ActivePRNG); 
    435                 byte[] newFileNameAry = new byte[info.Name.Length]; 
    436                 prng.NextBytes(newFileNameAry); 
    437  
    438                 //Validate the name 
    439                 const string validFileNameChars = "0123456789abcdefghijklmnopqrs" + 
    440                     "tuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    441                 for (int j = 0, k = newFileNameAry.Length; j < k; ++j) 
    442                     newFileNameAry[j] = (byte)validFileNameChars[ 
    443                         (int)newFileNameAry[j] % validFileNameChars.Length]; 
    444  
    445433                //Rename the file. 
    446434                string newPath = info.DirectoryName + Path.DirectorySeparatorChar + 
    447                     (new System.Text.UTF8Encoding()).GetString(newFileNameAry); 
    448                 info.MoveTo(newPath); 
     435                    GetRandomFileName(info.Name.Length); 
     436 
     437                //Try to rename the file. If it fails, it is probably due to another 
     438                //process locking the file. Defer, then rename again. 
     439                try 
     440                { 
     441                    info.MoveTo(newPath); 
     442                } 
     443                catch (IOException) 
     444                { 
     445                    Thread.Sleep(100); 
     446                    --i; 
     447                } 
    449448            } 
    450449 
     
    453452        } 
    454453 
    455         private void RemoveFolder(DirectoryInfo info) 
     454        private static void RemoveFolder(DirectoryInfo info) 
    456455        { 
    457456            foreach (FileInfo file in info.GetFiles()) 
     
    463462            for (int i = 0; i < FilenameErasePasses; ++i) 
    464463            { 
    465                 //Get a random file name 
    466                 PRNG prng = PRNGManager.GetInstance(Globals.Settings.ActivePRNG); 
    467                 byte[] newFileNameAry = new byte[info.Name.Length]; 
    468                 prng.NextBytes(newFileNameAry); 
    469  
    470                 //Validate the name 
    471                 const string validFileNameChars = "0123456789abcdefghijklmnopqrs" + 
    472                     "tuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ"; 
    473                 for (int j = 0, k = newFileNameAry.Length; j < k; ++j) 
    474                     newFileNameAry[j] = (byte)validFileNameChars[ 
    475                         (int)newFileNameAry[j] % validFileNameChars.Length]; 
    476  
    477464                //Rename the folder. 
    478465                string newPath = info.Parent.FullName + Path.DirectorySeparatorChar + 
    479                     (new System.Text.UTF8Encoding()).GetString(newFileNameAry); 
     466                    GetRandomFileName(info.Name.Length); 
    480467 
    481468                //Try to rename the file. If it fails, it is probably due to another 
     
    497484 
    498485        /// <summary> 
     486        /// Generates a random file name with the given length. 
     487        /// </summary> 
     488        /// <param name="length">The length of the file name to generate.</param> 
     489        /// <returns>A random file name.</returns> 
     490        private static string GetRandomFileName(int length) 
     491        { 
     492            //Get a random file name 
     493            PRNG prng = PRNGManager.GetInstance(Globals.Settings.ActivePRNG); 
     494            byte[] newFileNameAry = new byte[length]; 
     495            prng.NextBytes(newFileNameAry); 
     496 
     497            //Validate the name 
     498            string validFileNameChars = "0123456789abcdefghijklmnopqrstuvwxyz" + 
     499                "ABCDEFGHIJKLMNOPQRSTUVWXYZ _+=-()[]{}',`~!"; 
     500            for (int j = 0, k = newFileNameAry.Length; j < k; ++j) 
     501                newFileNameAry[j] = (byte)validFileNameChars[ 
     502                    (int)newFileNameAry[j] % validFileNameChars.Length]; 
     503 
     504            return new System.Text.UTF8Encoding().GetString(newFileNameAry); 
     505        } 
     506 
     507        /// <summary> 
    499508        /// The thread object. 
    500509        /// </summary> 
Note: See TracChangeset for help on using the changeset viewer.