| 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 | |
|---|
| 32 | CFileLockResolver::CFileLockResolver(BOOL askUser) |
|---|
| 33 | : m_bAskUser(askUser), m_hHandle(ERASER_INVALID_CONTEXT) |
|---|
| 34 | { |
|---|
| 35 | |
|---|
| 36 | } |
|---|
| 37 | CFileLockResolver::CFileLockResolver(ERASER_HANDLE h, BOOL askUser) |
|---|
| 38 | : m_bAskUser(askUser) |
|---|
| 39 | { |
|---|
| 40 | SetHandle(h); |
|---|
| 41 | } |
|---|
| 42 | |
|---|
| 43 | void |
|---|
| 44 | CFileLockResolver::SetHandle(ERASER_HANDLE h) |
|---|
| 45 | { |
|---|
| 46 | m_hHandle = h; |
|---|
| 47 | eraserSetErrorHandler(h, ErrorHandler, this); |
|---|
| 48 | } |
|---|
| 49 | |
|---|
| 50 | CFileLockResolver::~CFileLockResolver(void) |
|---|
| 51 | { |
|---|
| 52 | Close(); |
|---|
| 53 | } |
|---|
| 54 | struct 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 | }; |
|---|
| 81 | struct 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 | }; |
|---|
| 111 | std::ostream& operator << (std::ostream& os, const FileData& data) |
|---|
| 112 | { |
|---|
| 113 | data.write(os); |
|---|
| 114 | return os; |
|---|
| 115 | } |
|---|
| 116 | std::istream& operator >> (std::istream& is, FileData& data) |
|---|
| 117 | { |
|---|
| 118 | data.read(is); |
|---|
| 119 | return is; |
|---|
| 120 | } |
|---|
| 121 | void |
|---|
| 122 | CFileLockResolver::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 | } |
|---|
| 157 | |
|---|
| 158 | void |
|---|
| 159 | CFileLockResolver::Resolve(LPCTSTR /*szFileName*/) |
|---|
| 160 | { |
|---|
| 161 | |
|---|
| 162 | } |
|---|
| 163 | |
|---|
| 164 | void |
|---|
| 165 | CFileLockResolver::Resolve(LPCTSTR szFileName, CStringArray& ar) |
|---|
| 166 | { |
|---|
| 167 | std::ifstream is(szFileName); |
|---|
| 168 | if (is.fail()) |
|---|
| 169 | throw std::runtime_error("Unable to resolve locked files. Can't open file list"); |
|---|
| 170 | |
|---|
| 171 | while (!is.eof()) |
|---|
| 172 | { |
|---|
| 173 | FileData data; |
|---|
| 174 | is >> data; |
|---|
| 175 | if (!data.name.empty()) |
|---|
| 176 | ar.Add(data.name.c_str()); |
|---|
| 177 | } |
|---|
| 178 | is.close(); |
|---|
| 179 | DeleteFile(szFileName); |
|---|
| 180 | } |
|---|
| 181 | DWORD |
|---|
| 182 | CFileLockResolver::ErrorHandler(LPCTSTR szFileName, DWORD dwErrorCode, void* ctx, void* param) |
|---|
| 183 | { |
|---|
| 184 | CFileLockResolver* self(static_cast<CFileLockResolver*>(param)); |
|---|
| 185 | CEraserContext* ectx(static_cast<CEraserContext* >(ctx)); |
|---|
| 186 | self->HandleError(szFileName, dwErrorCode, ectx->m_lpmMethod->m_nMethodID, ectx->m_lpmMethod->m_nPasses); |
|---|
| 187 | return 0UL; |
|---|
| 188 | } |
|---|
| 189 | |
|---|
| 190 | void |
|---|
| 191 | CFileLockResolver::Close() |
|---|
| 192 | { |
|---|
| 193 | eraserSetErrorHandler(m_hHandle, NULL, NULL); |
|---|
| 194 | |
|---|
| 195 | if (m_strLockFileList.IsEmpty()) |
|---|
| 196 | return; |
|---|
| 197 | |
|---|
| 198 | CString strPath; |
|---|
| 199 | PathHelper(strPath, true); |
|---|
| 200 | strPath = CString("\"") + strPath ; |
|---|
| 201 | strPath += LAUNCHER; |
|---|
| 202 | strPath += "\" -rl \""; |
|---|
| 203 | strPath += m_strLockFileList + "\""; |
|---|
| 204 | |
|---|
| 205 | extern bool no_registry; |
|---|
| 206 | if (!no_registry) |
|---|
| 207 | { |
|---|
| 208 | CRegKey key; |
|---|
| 209 | if (ERROR_SUCCESS == key.Open(HKEY_LOCAL_MACHINE, RUNONCE)) |
|---|
| 210 | { |
|---|
| 211 | key.SetStringValue(LAUNCHER, strPath); |
|---|
| 212 | m_strLockFileList = ""; |
|---|
| 213 | } |
|---|
| 214 | } |
|---|
| 215 | } |
|---|