| 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 <fstream> |
|---|
| 26 | #include <string> |
|---|
| 27 | #include <iterator> |
|---|
| 28 | #include <atlbase.h> |
|---|
| 29 | |
|---|
| 30 | #define LOCKED_FILE_LIST_NAME _T("lock") |
|---|
| 31 | #define RUNONCE "Software\\Microsoft\\Windows\\CurrentVersion\\RunOnce" |
|---|
| 32 | #define LAUNCHER "Eraserl.exe" |
|---|
| 33 | |
|---|
| 34 | CFileLockResolver::CFileLockResolver(BOOL askUser) |
|---|
| 35 | : m_bAskUser(askUser), m_hHandle(ERASER_INVALID_CONTEXT) |
|---|
| 36 | { |
|---|
| 37 | |
|---|
| 38 | } |
|---|
| 39 | |
|---|
| 40 | CFileLockResolver::CFileLockResolver(ERASER_HANDLE h, BOOL askUser) |
|---|
| 41 | : m_bAskUser(askUser) |
|---|
| 42 | { |
|---|
| 43 | SetHandle(h); |
|---|
| 44 | } |
|---|
| 45 | |
|---|
| 46 | void CFileLockResolver::SetHandle(ERASER_HANDLE h) |
|---|
| 47 | { |
|---|
| 48 | m_hHandle = h; |
|---|
| 49 | eraserSetErrorHandler(h, ErrorHandler, this); |
|---|
| 50 | } |
|---|
| 51 | |
|---|
| 52 | CFileLockResolver::~CFileLockResolver(void) |
|---|
| 53 | { |
|---|
| 54 | Close(); |
|---|
| 55 | } |
|---|
| 56 | |
|---|
| 57 | struct PathHelper |
|---|
| 58 | { |
|---|
| 59 | CString& m_strLockFile; |
|---|
| 60 | PathHelper(CString& lockFile, bool path_only = false) |
|---|
| 61 | :m_strLockFile(lockFile) |
|---|
| 62 | { |
|---|
| 63 | char fullname[MAX_PATH]; |
|---|
| 64 | char filename[MAX_PATH]; |
|---|
| 65 | char extension[MAX_PATH]; |
|---|
| 66 | char pathname[MAX_PATH]; |
|---|
| 67 | char drive[10]; |
|---|
| 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 | struct FileData |
|---|
| 81 | { |
|---|
| 82 | std::string name; |
|---|
| 83 | int method; |
|---|
| 84 | unsigned int passes; |
|---|
| 85 | |
|---|
| 86 | FileData() |
|---|
| 87 | { |
|---|
| 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 | is >> std::noskipws; |
|---|
| 98 | std::getline(is, name); |
|---|
| 99 | } |
|---|
| 100 | |
|---|
| 101 | void write(std::ostream& os) const |
|---|
| 102 | { |
|---|
| 103 | os << std::noskipws; |
|---|
| 104 | os << name << std::endl; |
|---|
| 105 | } |
|---|
| 106 | }; |
|---|
| 107 | |
|---|
| 108 | std::ostream& operator<< (std::ostream& os, const FileData& data) |
|---|
| 109 | { |
|---|
| 110 | data.write(os); |
|---|
| 111 | return os; |
|---|
| 112 | } |
|---|
| 113 | std::istream& operator>> (std::istream& is, FileData& data) |
|---|
| 114 | { |
|---|
| 115 | data.read(is); |
|---|
| 116 | return is; |
|---|
| 117 | } |
|---|
| 118 | |
|---|
| 119 | void CFileLockResolver::HandleError(LPCTSTR szFileName, DWORD dwErrorCode, int em, unsigned int passes) |
|---|
| 120 | { |
|---|
| 121 | if (ERROR_LOCK_VIOLATION == dwErrorCode |
|---|
| 122 | || ERROR_DRIVE_LOCKED == dwErrorCode |
|---|
| 123 | || ERROR_LOCKED == dwErrorCode |
|---|
| 124 | || ERROR_SHARING_VIOLATION == dwErrorCode) |
|---|
| 125 | { |
|---|
| 126 | bool needResolve = true; |
|---|
| 127 | if (TRUE == m_bAskUser ) |
|---|
| 128 | { |
|---|
| 129 | needResolve = (IDYES == AfxGetMainWnd()->MessageBox(CString("The file ") + |
|---|
| 130 | szFileName + "is 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 | |
|---|
| 134 | if (needResolve) |
|---|
| 135 | { |
|---|
| 136 | static PathHelper path(m_strLockFileList); |
|---|
| 137 | std::ofstream os(m_strLockFileList, std::ios_base::out | std::ios_base::app); |
|---|
| 138 | os << FileData(szFileName, em, passes); |
|---|
| 139 | } |
|---|
| 140 | } |
|---|
| 141 | } |
|---|
| 142 | |
|---|
| 143 | void CFileLockResolver::Resolve(LPCTSTR szFileName, CStringArray& ar) |
|---|
| 144 | { |
|---|
| 145 | std::ifstream is(szFileName); |
|---|
| 146 | if (is.fail()) |
|---|
| 147 | throw std::runtime_error("Unable to resolve locked files. Can't open file list"); |
|---|
| 148 | |
|---|
| 149 | while (!is.eof()) |
|---|
| 150 | { |
|---|
| 151 | FileData data; |
|---|
| 152 | is >> data; |
|---|
| 153 | if (!data.name.empty()) |
|---|
| 154 | ar.Add(data.name.c_str()); |
|---|
| 155 | } |
|---|
| 156 | is.close(); |
|---|
| 157 | DeleteFile(szFileName); |
|---|
| 158 | } |
|---|
| 159 | |
|---|
| 160 | DWORD CFileLockResolver::ErrorHandler(LPCTSTR szFileName, DWORD dwErrorCode, void* ctx, void* param) |
|---|
| 161 | { |
|---|
| 162 | CFileLockResolver* self(static_cast<CFileLockResolver*>(param)); |
|---|
| 163 | CEraserContext* ectx(static_cast<CEraserContext* >(ctx)); |
|---|
| 164 | self->HandleError(szFileName, dwErrorCode, ectx->m_lpmMethod->m_nMethodID, ectx->m_lpmMethod->m_nPasses); |
|---|
| 165 | return 0UL; |
|---|
| 166 | } |
|---|
| 167 | |
|---|
| 168 | void CFileLockResolver::Close() |
|---|
| 169 | { |
|---|
| 170 | eraserSetErrorHandler(m_hHandle, NULL, NULL); |
|---|
| 171 | |
|---|
| 172 | if (m_strLockFileList.IsEmpty()) |
|---|
| 173 | return; |
|---|
| 174 | |
|---|
| 175 | CString strPath; |
|---|
| 176 | PathHelper(strPath, true); |
|---|
| 177 | strPath = CString("\"") + strPath ; |
|---|
| 178 | strPath += LAUNCHER; |
|---|
| 179 | strPath += "\" -rl \""; |
|---|
| 180 | strPath += m_strLockFileList + "\""; |
|---|
| 181 | |
|---|
| 182 | extern bool no_registry; |
|---|
| 183 | if (!no_registry) |
|---|
| 184 | { |
|---|
| 185 | CRegKey key; |
|---|
| 186 | if (ERROR_SUCCESS == key.Open(HKEY_LOCAL_MACHINE, RUNONCE)) |
|---|
| 187 | { |
|---|
| 188 | key.SetStringValue(LAUNCHER, strPath); |
|---|
| 189 | m_strLockFileList = ""; |
|---|
| 190 | } |
|---|
| 191 | } |
|---|
| 192 | } |
|---|