Changeset 55


Ignore:
Timestamp:
10/13/07 11:12:51 (7 years ago)
Author:
lowjoel
Message:

Rewrote bits of the file lock resolver to be more efficient.

Location:
trunk/EraserDll
Files:
2 edited

Legend:

Unmodified
Added
Removed
  • trunk/EraserDll/FileLockResolver.cpp

    r52 r55  
    3535 
    3636CFileLockResolver::CFileLockResolver(BOOL askUser) 
    37 : m_bAskUser(askUser), m_hHandle(ERASER_INVALID_CONTEXT) 
     37: m_bAskUser(askUser), m_hHandle(ERASER_INVALID_CONTEXT), 
     38  m_iMethod(0), m_iPasses(0) 
    3839{ 
    3940 
     
    4142 
    4243CFileLockResolver::CFileLockResolver(ERASER_HANDLE h, BOOL askUser) 
    43 : m_bAskUser(askUser) 
     44: m_bAskUser(askUser), m_iMethod(0), m_iPasses(0) 
    4445{ 
    4546    SetHandle(h); 
     47} 
     48 
     49CFileLockResolver::~CFileLockResolver(void) 
     50{ 
     51    Close(); 
    4652} 
    4753 
     
    5258} 
    5359 
    54 CFileLockResolver::~CFileLockResolver(void) 
    55 { 
    56     Close(); 
    57 } 
    58  
    59 struct PathHelper 
    60 { 
    61     CString& m_strLockFile; 
    62     PathHelper(CString& lockFile, bool path_only = false) 
    63         :m_strLockFile(lockFile) 
    64     { 
    65         char fullname[MAX_PATH]; 
    66         char filename[MAX_PATH]; 
    67         char extension[MAX_PATH]; 
    68         char pathname[MAX_PATH]; 
    69         char drive[10]; 
    70  
    71         GetModuleFileName(AfxGetInstanceHandle(),fullname,sizeof (fullname)); 
    72         _splitpath(fullname,drive, pathname, filename, extension);  
    73  
    74         m_strLockFile = drive; 
    75         m_strLockFile += pathname; 
    76         if (path_only ) 
    77             return; 
    78         m_strLockFile.Format("%s%s%d.%s", drive, pathname, time(0), LOCKED_FILE_LIST_NAME); 
    79     } 
    80 }; 
     60CString CFileLockResolver::GetLockFilePath(bool path_only) 
     61{ 
     62    // Retrieve the path to the current binary 
     63    char fullname[MAX_PATH]; 
     64    GetModuleFileName(AfxGetInstanceHandle(), fullname, sizeof(fullname)); 
     65 
     66    // Then separate the path into its constituent parts 
     67    char filename[MAX_PATH]; 
     68    char extension[MAX_PATH]; 
     69    char pathname[MAX_PATH]; 
     70    char drive[10]; 
     71    _splitpath(fullname, drive, pathname, filename, extension);  
     72 
     73    // Then generate the path which we want 
     74    CString result; 
     75    if (path_only) 
     76        result.Format("%s%s", drive, pathname); 
     77    else 
     78        result.Format("%s%s%d.%s", drive, pathname, time(0), LOCKED_FILE_LIST_NAME); 
     79    return result; 
     80} 
    8181 
    8282struct FileData 
     
    9191 
    9292    FileData(const std::string& fname, int m, unsigned int pass) 
    93         :name(fname), method(m), passes(pass) 
     93        : name(fname), method(m), passes(pass) 
    9494    { 
    9595    } 
     
    9999        is >> std::noskipws; 
    100100        std::getline(is, name); 
     101        is >> method >> passes; 
    101102    } 
    102103 
     
    104105    { 
    105106        os << std::noskipws; 
    106         os << name << std::endl; 
     107        os << name << std::endl << method << passes; 
    107108    } 
    108109}; 
     
    113114    return os; 
    114115} 
     116 
    115117std::istream& operator>> (std::istream& is, FileData& data) 
    116118{ 
     
    119121} 
    120122 
    121 void CFileLockResolver::HandleError(LPCTSTR szFileName, DWORD dwErrorCode, int em, unsigned int passes) 
     123void CFileLockResolver::HandleError(LPCTSTR szFileName, DWORD dwErrorCode, int method, unsigned int passes) 
    122124{ 
    123125    if (ERROR_LOCK_VIOLATION == dwErrorCode  
     
    128130        if (TRUE == m_bAskUser) 
    129131        { 
    130             if (IDYES == AfxGetMainWnd()->MessageBox(CString("The file ") + 
     132            if (IDYES == AfxMessageBox(CString("The file ") + 
    131133                szFileName + "\nis locked by another process. Do you want to Erase the file after " + 
    132                 "you restart your computer?", "File Access Denied", MB_YESNO | MB_ICONQUESTION | MB_TASKMODAL)) 
     134                "you restart your computer?", MB_YESNO | MB_ICONQUESTION)) 
    133135            { 
    134                 static PathHelper path(m_strLockFileList); 
     136                if (m_strLockFileList.IsEmpty()) 
     137                    m_strLockFileList = GetLockFilePath(); 
    135138                std::ofstream os(m_strLockFileList, std::ios_base::out | std::ios_base::app);        
    136                 os << FileData(szFileName, em, passes); 
     139                os << FileData(szFileName, method, passes); 
     140 
     141                ASSERT(m_iMethod == 0 || m_iMethod == method); 
     142                ASSERT(m_iPasses == 0 || m_iPasses == passes); 
     143                m_iMethod = method; 
     144                m_iPasses = passes; 
    137145            } 
    138146        } 
     
    168176{ 
    169177    eraserSetErrorHandler(m_hHandle, NULL, NULL); 
    170  
    171178    if (m_strLockFileList.IsEmpty()) 
    172179        return; 
    173180 
    174     CString strPath; 
    175     PathHelper(strPath, true); 
    176     strPath = CString("\"") + strPath + LAUNCHER + "\" " + szResolveLock; 
    177     strPath += " \"" + m_strLockFileList + "\""; 
     181    //Using the method and the passes, generate a command line that will do the same thing as it does now. 
     182    CString method; 
     183    switch (m_iMethod) 
     184    { 
     185    case GUTMANN_METHOD_ID: 
     186        method = "Gutmann"; 
     187        break; 
     188    case DOD_METHOD_ID: 
     189        method = "DoD"; 
     190        break; 
     191    case DOD_E_METHOD_ID: 
     192        method = "DoD_E"; 
     193        break; 
     194    case RANDOM_METHOD_ID: 
     195        method.Format("Random %d", m_iPasses); 
     196        break; 
     197    case FL2KB_METHOD_ID: 
     198        method = "First_Last2k"; 
     199        break; 
     200    case SCHNEIER_METHOD_ID: 
     201        method = "Schneider"; 
     202        break; 
     203    } 
     204 
     205    CString strPath(CString("\"") + LAUNCHER + "\" " + szResolveLock + " \"" + 
     206        m_strLockFileList + "\" -method " + method); 
    178207 
    179208    extern bool no_registry; 
     
    185214            key.SetStringValue(LAUNCHER, strPath); 
    186215            m_strLockFileList = ""; 
     216            m_iMethod = 0; 
     217            m_iPasses = 0; 
    187218        } 
    188219    } 
  • trunk/EraserDll/FileLockResolver.h

    r35 r55  
    4545    CString m_strLockFileList; 
    4646    ERASER_HANDLE m_hHandle; 
     47    int m_iMethod; 
     48    unsigned m_iPasses; 
    4749private: 
     50    static CString GetLockFilePath(bool path_only = false); 
    4851    void HandleError(LPCTSTR szFileName, DWORD dwErrorCode, int method, unsigned int passes); 
    4952    static DWORD ErrorHandler(LPCTSTR szFileName, DWORD dwErrorCode, void* ctx, void* param); 
Note: See TracChangeset for help on using the changeset viewer.