source: trunk/EraserDll/FileLockResolver.cpp @ 4

Revision 4, 5.0 KB checked in by lowjoel, 7 years ago (diff)

Added the source files from EraserDll? (SVN migration commit 2)

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