source: trunk/EraserDll/FileLockResolver.cpp @ 46

Revision 46, 4.6 KB checked in by lowjoel, 7 years ago (diff)

-Refactored the File Lock Resolver code a bit
-The Launcher must terminate if the erase-on-restart failed
-Fixed a few stuff in the README

  • 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#include "stdafx.h"
24#include "FileLockResolver.h"
25#include "..\Launcher\Launcher.h"
26#include <fstream>
27#include <string>
28#include <iterator>
29#include <atlbase.h>
30
31#define LOCKED_FILE_LIST_NAME _T("lock")
32#define RUNONCE  "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce"
33#define LAUNCHER "Eraserl.exe"
34
35CFileLockResolver::CFileLockResolver(BOOL askUser)
36: m_bAskUser(askUser), m_hHandle(ERASER_INVALID_CONTEXT)
37{
38
39}
40
41CFileLockResolver::CFileLockResolver(ERASER_HANDLE h, BOOL askUser)
42: m_bAskUser(askUser)
43{
44    SetHandle(h);
45}
46
47void CFileLockResolver::SetHandle(ERASER_HANDLE h)
48{
49    m_hHandle = h;
50    eraserSetErrorHandler(h, ErrorHandler, this);
51}
52
53CFileLockResolver::~CFileLockResolver(void)
54{
55    Close();
56}
57
58struct PathHelper
59{
60    CString& m_strLockFile;
61    PathHelper(CString& lockFile, bool path_only = false)
62        :m_strLockFile(lockFile)
63    {
64        char fullname[MAX_PATH];
65        char filename[MAX_PATH];
66        char extension[MAX_PATH];
67        char pathname[MAX_PATH];
68        char drive[10];
69
70        GetModuleFileName(AfxGetInstanceHandle(),fullname,sizeof (fullname));
71        _splitpath(fullname,drive, pathname, filename, extension); 
72
73        m_strLockFile = drive;
74        m_strLockFile += pathname;
75        if (path_only )
76            return;
77        m_strLockFile.Format("%s%s%d.%s", drive, pathname, time(0), LOCKED_FILE_LIST_NAME);
78    }
79};
80
81struct FileData
82{
83    std::string name;
84    int method;
85    unsigned int passes;
86
87    FileData()
88    {
89    }
90
91    FileData(const std::string& fname, int m, unsigned int pass)
92        :name(fname), method(m), passes(pass)
93    {
94    }
95
96    void read(std::istream& is)
97    {
98        is >> std::noskipws;
99        std::getline(is, name);
100    }
101
102    void write(std::ostream& os) const
103    {
104        os << std::noskipws;
105        os << name << std::endl;
106    }
107};
108
109std::ostream& operator<< (std::ostream& os, const FileData& data)
110{
111    data.write(os);
112    return os;
113}
114std::istream& operator>> (std::istream& is, FileData& data)
115{
116    data.read(is);
117    return is;
118}
119
120void CFileLockResolver::HandleError(LPCTSTR szFileName, DWORD dwErrorCode, int em, unsigned int passes)
121{
122    if (ERROR_LOCK_VIOLATION == dwErrorCode
123        || ERROR_DRIVE_LOCKED == dwErrorCode
124        || ERROR_LOCKED == dwErrorCode
125        || ERROR_SHARING_VIOLATION == dwErrorCode)
126    {
127        if (TRUE == m_bAskUser)
128        {
129            if (IDYES == AfxGetMainWnd()->MessageBox(CString("The file ") +
130                szFileName + "\nis locked by another process. Do you want to Erase the file after " +
131                "you restart your computer?", "File Access Denied", MB_YESNO | MB_ICONQUESTION))
132            {
133                static PathHelper path(m_strLockFileList);
134                std::ofstream os(m_strLockFileList, std::ios_base::out | std::ios_base::app);       
135                os << FileData(szFileName, em, passes);
136            }
137        }
138    }
139}
140
141void CFileLockResolver::Resolve(LPCTSTR szFileName, CStringArray& ar)
142{
143    std::ifstream is(szFileName);
144    if (is.fail())
145        throw std::runtime_error("Unable to resolve locked files. Can't open file list");
146
147    while (!is.eof()) 
148    {
149        FileData data;
150        is >> data;
151        if (!data.name.empty())
152            ar.Add(data.name.c_str());
153    }
154    is.close();
155    DeleteFile(szFileName);
156}
157
158DWORD CFileLockResolver::ErrorHandler(LPCTSTR szFileName, DWORD dwErrorCode, void* ctx, void* param)
159{
160    CFileLockResolver* self(static_cast<CFileLockResolver*>(param));
161    CEraserContext* ectx(static_cast<CEraserContext* >(ctx));
162    self->HandleError(szFileName, dwErrorCode, ectx->m_lpmMethod->m_nMethodID, ectx->m_lpmMethod->m_nPasses);
163    return 0UL;
164}
165
166void CFileLockResolver::Close()
167{
168    eraserSetErrorHandler(m_hHandle, NULL, NULL);
169
170    if (m_strLockFileList.IsEmpty())
171        return;
172
173    CString strPath;
174    PathHelper(strPath, true);
175    strPath = CString("\"") + strPath + LAUNCHER + "\" " + szResolveLock;
176    strPath += " \"" + m_strLockFileList + "\"";
177
178    extern bool no_registry;
179    if (!no_registry)
180    {
181        CRegKey key;
182        if (ERROR_SUCCESS == key.Open(HKEY_LOCAL_MACHINE, RUNONCE))
183        {
184            key.SetStringValue(LAUNCHER, strPath);
185            m_strLockFileList = "";
186        }
187    }
188}
Note: See TracBrowser for help on using the repository browser.