source: trunk/EraserDll/FileLockResolver.cpp @ 52

Revision 52, 4.7 KB checked in by lowjoel, 7 years ago (diff)

-Removed the duplicate FileLockResolver? class
-Bump the beta number

  • Property svn:eol-style set to native
  • Property svn:keywords set to Id
Line 
1// FileLockResolver.cpp
2// $Id$
3//
4// Eraser. Secure data removal. For Windows.
5// Copyright © 1997-2001  Sami Tolvanen (sami@tolvanen.com).
6// Copyright © 2001-2006  Garrett Trant (support@heidi.ie).
7// Copyright © 2007 The Eraser Project.
8//
9// This program is free software; you can redistribute it and/or
10// modify it under the terms of the GNU General Public License
11// as published by the Free Software Foundation; either version 2
12// of the License, or (at your option) any later version.
13//
14// This program is distributed in the hope that it will be useful,
15// but WITHOUT ANY WARRANTY; without even the implied warranty of
16// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
17// GNU General Public License for more details.
18//
19// You should have received a copy of the GNU General Public License
20// along with this program; if not, write to the Free Software
21// Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA
22// 02111-1307, USA.
23
24#include "stdafx.h"
25#include "FileLockResolver.h"
26#include "..\Launcher\Launcher.h"
27#include <fstream>
28#include <string>
29#include <iterator>
30#include <atlbase.h>
31
32#define LOCKED_FILE_LIST_NAME _T("lock")
33#define RUNONCE  "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce"
34#define LAUNCHER "Eraserl.exe"
35
36CFileLockResolver::CFileLockResolver(BOOL askUser)
37: m_bAskUser(askUser), m_hHandle(ERASER_INVALID_CONTEXT)
38{
39
40}
41
42CFileLockResolver::CFileLockResolver(ERASER_HANDLE h, BOOL askUser)
43: m_bAskUser(askUser)
44{
45    SetHandle(h);
46}
47
48void CFileLockResolver::SetHandle(ERASER_HANDLE h)
49{
50    m_hHandle = h;
51    eraserSetErrorHandler(h, ErrorHandler, this);
52}
53
54CFileLockResolver::~CFileLockResolver(void)
55{
56    Close();
57}
58
59struct 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};
81
82struct FileData
83{
84    std::string name;
85    int method;
86    unsigned int passes;
87
88    FileData()
89    {
90    }
91
92    FileData(const std::string& fname, int m, unsigned int pass)
93        :name(fname), method(m), passes(pass)
94    {
95    }
96
97    void read(std::istream& is)
98    {
99        is >> std::noskipws;
100        std::getline(is, name);
101    }
102
103    void write(std::ostream& os) const
104    {
105        os << std::noskipws;
106        os << name << std::endl;
107    }
108};
109
110std::ostream& operator<< (std::ostream& os, const FileData& data)
111{
112    data.write(os);
113    return os;
114}
115std::istream& operator>> (std::istream& is, FileData& data)
116{
117    data.read(is);
118    return is;
119}
120
121void CFileLockResolver::HandleError(LPCTSTR szFileName, DWORD dwErrorCode, int em, unsigned int passes)
122{
123    if (ERROR_LOCK_VIOLATION == dwErrorCode
124        || ERROR_DRIVE_LOCKED == dwErrorCode
125        || ERROR_LOCKED == dwErrorCode
126        || ERROR_SHARING_VIOLATION == dwErrorCode)
127    {
128        if (TRUE == m_bAskUser)
129        {
130            if (IDYES == AfxGetMainWnd()->MessageBox(CString("The file ") +
131                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))
133            {
134                static PathHelper path(m_strLockFileList);
135                std::ofstream os(m_strLockFileList, std::ios_base::out | std::ios_base::app);       
136                os << FileData(szFileName, em, passes);
137            }
138        }
139    }
140}
141
142void CFileLockResolver::Resolve(LPCTSTR szFileName, CStringArray& ar)
143{
144    std::ifstream is(szFileName);
145    if (is.fail())
146        throw std::runtime_error("Unable to resolve locked files. Can't open file list");
147
148    while (!is.eof()) 
149    {
150        FileData data;
151        is >> data;
152        if (!data.name.empty())
153            ar.Add(data.name.c_str());
154    }
155    is.close();
156    DeleteFile(szFileName);
157}
158
159DWORD CFileLockResolver::ErrorHandler(LPCTSTR szFileName, DWORD dwErrorCode, void* ctx, void* param)
160{
161    CFileLockResolver* self(static_cast<CFileLockResolver*>(param));
162    CEraserContext* ectx(static_cast<CEraserContext* >(ctx));
163    self->HandleError(szFileName, dwErrorCode, ectx->m_lpmMethod->m_nMethodID, ectx->m_lpmMethod->m_nPasses);
164    return 0UL;
165}
166
167void CFileLockResolver::Close()
168{
169    eraserSetErrorHandler(m_hHandle, NULL, NULL);
170
171    if (m_strLockFileList.IsEmpty())
172        return;
173
174    CString strPath;
175    PathHelper(strPath, true);
176    strPath = CString("\"") + strPath + LAUNCHER + "\" " + szResolveLock;
177    strPath += " \"" + m_strLockFileList + "\"";
178
179    extern bool no_registry;
180    if (!no_registry)
181    {
182        CRegKey key;
183        if (ERROR_SUCCESS == key.Open(HKEY_LOCAL_MACHINE, RUNONCE))
184        {
185            key.SetStringValue(LAUNCHER, strPath);
186            m_strLockFileList = "";
187        }
188    }
189}
Note: See TracBrowser for help on using the repository browser.