| 1 | // FileHelper.cpp |
|---|
| 2 | // |
|---|
| 3 | // Eraser. Secure data removal. For Windows. |
|---|
| 4 | // Copyright © 1997-2001 Sami Tolvanen (sami@tolvanen.com). |
|---|
| 5 | // |
|---|
| 6 | // This program is free software; you can redistribute it and/or |
|---|
| 7 | // modify it under the terms of the GNU General Public License |
|---|
| 8 | // as published by the Free Software Foundation; either version 2 |
|---|
| 9 | // of the License, or (at your option) any later version. |
|---|
| 10 | // |
|---|
| 11 | // This program is distributed in the hope that it will be useful, |
|---|
| 12 | // but WITHOUT ANY WARRANTY; without even the implied warranty of |
|---|
| 13 | // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
|---|
| 14 | // GNU General Public License for more details. |
|---|
| 15 | // |
|---|
| 16 | // You should have received a copy of the GNU General Public License |
|---|
| 17 | // along with this program; if not, write to the Free Software |
|---|
| 18 | // Foundation, Inc., 59 Temple Place - Suite 330, Boston, MA |
|---|
| 19 | // 02111-1307, USA. |
|---|
| 20 | |
|---|
| 21 | #include "stdafx.h" |
|---|
| 22 | #include "..\EraserDll\EraserDll.h" |
|---|
| 23 | #include "FileHelper.h" |
|---|
| 24 | const LPCTSTR pprefix = "\\\\?"; |
|---|
| 25 | |
|---|
| 26 | void |
|---|
| 27 | findMatchingFiles2(CString strSearch, CStringArray& saFiles, BOOL bSubFolders /*=FALSE*/) |
|---|
| 28 | { |
|---|
| 29 | HANDLE hFind, hFolder; |
|---|
| 30 | WIN32_FIND_DATA wfdData, wfdFolder; |
|---|
| 31 | |
|---|
| 32 | CString strFolder; |
|---|
| 33 | CString strBaseFolder; |
|---|
| 34 | TCHAR szDrive[_MAX_DRIVE]; |
|---|
| 35 | TCHAR szFolder[_MAX_PATH]; |
|---|
| 36 | |
|---|
| 37 | // get the root folder |
|---|
| 38 | _splitpath((LPCTSTR)strSearch, szDrive, szFolder, NULL, NULL); |
|---|
| 39 | |
|---|
| 40 | strBaseFolder = szDrive; |
|---|
| 41 | strBaseFolder += szFolder; |
|---|
| 42 | |
|---|
| 43 | if (strBaseFolder.IsEmpty()) { |
|---|
| 44 | if (GetCurrentDirectory(_MAX_PATH, szFolder) != 0) { |
|---|
| 45 | strBaseFolder = szFolder; |
|---|
| 46 | if (strBaseFolder[strBaseFolder.GetLength() - 1] != '\\') { |
|---|
| 47 | strBaseFolder += "\\"; |
|---|
| 48 | } |
|---|
| 49 | strSearch = strBaseFolder + strSearch; |
|---|
| 50 | } |
|---|
| 51 | } |
|---|
| 52 | //strBaseFolder = pprefix + strBaseFolder; |
|---|
| 53 | // the search pattern |
|---|
| 54 | //if (strSearch.Find('\\') > 0) { |
|---|
| 55 | |
|---|
| 56 | if (strSearch.ReverseFind('\\') > 0) { |
|---|
| 57 | //strSearch = strSearch.Left( strSearch.ReverseFind('\\')+1 ); -> this will return the path |
|---|
| 58 | strSearch = strSearch.Right(strSearch.GetLength() - 1 - |
|---|
| 59 | strSearch.ReverseFind('\\')); |
|---|
| 60 | } |
|---|
| 61 | |
|---|
| 62 | // browse through all files (and directories) |
|---|
| 63 | hFolder = FindFirstFile((LPCTSTR)(strBaseFolder + "*"), &wfdFolder); |
|---|
| 64 | |
|---|
| 65 | if (hFolder != INVALID_HANDLE_VALUE) { |
|---|
| 66 | hFind = FindFirstFile((LPCTSTR)(strBaseFolder + strSearch), &wfdData); |
|---|
| 67 | |
|---|
| 68 | // process the current folder first |
|---|
| 69 | if (hFind != INVALID_HANDLE_VALUE) { |
|---|
| 70 | do { |
|---|
| 71 | if (!bitSet(wfdData.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY)) { |
|---|
| 72 | saFiles.Add(strBaseFolder + wfdData.cFileName); |
|---|
| 73 | } |
|---|
| 74 | } while (FindNextFile(hFind, &wfdData)); |
|---|
| 75 | |
|---|
| 76 | VERIFY(FindClose(hFind)); |
|---|
| 77 | } |
|---|
| 78 | |
|---|
| 79 | // go through the subfolders then |
|---|
| 80 | if (bSubFolders) { |
|---|
| 81 | while (FindNextFile(hFolder, &wfdFolder)) { |
|---|
| 82 | if (bitSet(wfdFolder.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY) && |
|---|
| 83 | IS_SUBFOLDER(wfdFolder.cFileName)) { |
|---|
| 84 | // found one |
|---|
| 85 | strFolder = strBaseFolder + wfdFolder.cFileName; |
|---|
| 86 | strFolder += "\\"; |
|---|
| 87 | // recursive |
|---|
| 88 | findMatchingFiles(strFolder + strSearch, saFiles, bSubFolders); |
|---|
| 89 | } |
|---|
| 90 | } |
|---|
| 91 | } |
|---|
| 92 | VERIFY(FindClose(hFolder)); |
|---|
| 93 | } |
|---|
| 94 | } |
|---|
| 95 | |
|---|
| 96 | void |
|---|
| 97 | findMatchingFiles(CString strSearch, CStringArray& saFiles, BOOL bSubFolders /*=FALSE*/) { |
|---|
| 98 | strSearch.Replace('/', '\\'); |
|---|
| 99 | int i = strSearch.FindOneOf("*?"); |
|---|
| 100 | int j = -1; |
|---|
| 101 | if (i != -1) { |
|---|
| 102 | j = strSearch.Find('\\', i); |
|---|
| 103 | } |
|---|
| 104 | if((i == -1) || (j == -1)) { |
|---|
| 105 | // fallback if wildcard not found or if wildcard isn't in the middle of path |
|---|
| 106 | findMatchingFiles2(strSearch, saFiles, bSubFolders); |
|---|
| 107 | return; |
|---|
| 108 | } |
|---|
| 109 | int k = strSearch.Left(i).ReverseFind('\\'); |
|---|
| 110 | if(k == -1) |
|---|
| 111 | k = 0; |
|---|
| 112 | |
|---|
| 113 | WIN32_FIND_DATA wfdData; |
|---|
| 114 | HANDLE hFolder = FindFirstFile(strSearch.Left(j), &wfdData); |
|---|
| 115 | if (hFolder != INVALID_HANDLE_VALUE) { |
|---|
| 116 | do { |
|---|
| 117 | if (!bitSet(wfdData.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY)) |
|---|
| 118 | continue; |
|---|
| 119 | if(!strcmp(wfdData.cFileName, ".") || !strcmp(wfdData.cFileName, "..")) |
|---|
| 120 | continue; |
|---|
| 121 | CString newStrSearch = strSearch.Left(k) + "\\" + wfdData.cFileName + strSearch.Mid(j); |
|---|
| 122 | findMatchingFiles(newStrSearch, saFiles, bSubFolders); |
|---|
| 123 | } while(FindNextFile(hFolder, &wfdData)); |
|---|
| 124 | VERIFY(FindClose(hFolder)); |
|---|
| 125 | } |
|---|
| 126 | } |
|---|
| 127 | |
|---|
| 128 | BOOL |
|---|
| 129 | parseDirectory(LPCTSTR szDirectory, CStringArray& saFiles, CStringArray& saDirectories, |
|---|
| 130 | BOOL bSubDirectories, LPDWORD pdwFiles /*=0*/, LPDWORD pdwDirectories /*=0*/) |
|---|
| 131 | { |
|---|
| 132 | HANDLE hFind; |
|---|
| 133 | WIN32_FIND_DATA wfdData; |
|---|
| 134 | CString strDirectory(szDirectory); |
|---|
| 135 | |
|---|
| 136 | if (!strDirectory.IsEmpty()) { |
|---|
| 137 | if (pdwDirectories) { |
|---|
| 138 | try { |
|---|
| 139 | (*pdwDirectories)++; |
|---|
| 140 | } catch (...) { |
|---|
| 141 | ASSERT(0); |
|---|
| 142 | } |
|---|
| 143 | } |
|---|
| 144 | |
|---|
| 145 | if (strDirectory[strDirectory.GetLength() - 1] != '\\') { |
|---|
| 146 | strDirectory += "\\"; |
|---|
| 147 | } |
|---|
| 148 | |
|---|
| 149 | // add current folder to the beginning of the list |
|---|
| 150 | // --> subfolders will be removed first |
|---|
| 151 | saDirectories.InsertAt(0, strDirectory); |
|---|
| 152 | |
|---|
| 153 | hFind = FindFirstFile((LPCTSTR)(strDirectory + "*"), &wfdData); |
|---|
| 154 | |
|---|
| 155 | if (hFind != INVALID_HANDLE_VALUE) { |
|---|
| 156 | do { |
|---|
| 157 | if (bitSet(wfdData.dwFileAttributes, FILE_ATTRIBUTE_DIRECTORY)) { |
|---|
| 158 | // skip "." and ".." |
|---|
| 159 | if (!bSubDirectories || ISNT_SUBFOLDER(wfdData.cFileName)) { |
|---|
| 160 | continue; |
|---|
| 161 | } |
|---|
| 162 | |
|---|
| 163 | // recursive |
|---|
| 164 | parseDirectory((LPCTSTR)(strDirectory + wfdData.cFileName), |
|---|
| 165 | saFiles, |
|---|
| 166 | saDirectories, |
|---|
| 167 | bSubDirectories, |
|---|
| 168 | pdwFiles, |
|---|
| 169 | pdwDirectories); |
|---|
| 170 | } else { |
|---|
| 171 | saFiles.Add(strDirectory + wfdData.cFileName); |
|---|
| 172 | if (pdwFiles) { |
|---|
| 173 | try { |
|---|
| 174 | (*pdwFiles)++; |
|---|
| 175 | } catch (...) { |
|---|
| 176 | ASSERT(0); |
|---|
| 177 | } |
|---|
| 178 | } |
|---|
| 179 | } |
|---|
| 180 | } while (FindNextFile(hFind, &wfdData)); |
|---|
| 181 | |
|---|
| 182 | VERIFY(FindClose(hFind)); |
|---|
| 183 | |
|---|
| 184 | return TRUE; |
|---|
| 185 | } |
|---|
| 186 | } |
|---|
| 187 | |
|---|
| 188 | return FALSE; |
|---|
| 189 | } |
|---|
| 190 | |
|---|
| 191 | CString findPattern(LPCTSTR szPath,CString& strBefore, CString& strAfter) |
|---|
| 192 | { |
|---|
| 193 | CString strPath = szPath; |
|---|
| 194 | CString strPattern = _T(""); |
|---|
| 195 | int iGearPos = 0; |
|---|
| 196 | int iWhatPos = 0; |
|---|
| 197 | int iStart = -1; |
|---|
| 198 | strBefore=_T(""); |
|---|
| 199 | strAfter=_T(""); |
|---|
| 200 | if (strPath.IsEmpty()) return strPattern; |
|---|
| 201 | iGearPos=strPath.Find('*'); |
|---|
| 202 | iWhatPos=strPath.Find('?'); |
|---|
| 203 | iStart = iGearPos>-1?iGearPos:iStart; |
|---|
| 204 | iStart = iWhatPos>-1?iWhatPos:iStart; |
|---|
| 205 | if (iGearPos>-1&&iWhatPos>-1) iStart = iGearPos<iWhatPos?iGearPos:iWhatPos; |
|---|
| 206 | if (iStart == -1) { |
|---|
| 207 | strBefore = strPath; |
|---|
| 208 | return "*"; |
|---|
| 209 | } |
|---|
| 210 | iStart = (strPath.Left(iStart)).ReverseFind('\\'); |
|---|
| 211 | strBefore = strPath.Left(iStart)+"\\"; |
|---|
| 212 | strPattern=strPath.Right(strPath.GetLength() - iStart-1); |
|---|
| 213 | if ((iStart = strPattern.Find('\\')) != -1) |
|---|
| 214 | { |
|---|
| 215 | strAfter = "\\" + strPattern.Right(strPattern.GetLength()-iStart-1); |
|---|
| 216 | //if (strAfter.IsEmpty()) strAfter = "\\"; |
|---|
| 217 | strPattern = strPattern.Left(iStart); |
|---|
| 218 | } |
|---|
| 219 | return strPattern; |
|---|
| 220 | } |
|---|
| 221 | |
|---|
| 222 | bool PatternMatch(const char* s, const char* mask) |
|---|
| 223 | { |
|---|
| 224 | |
|---|
| 225 | const char* cp=0; |
|---|
| 226 | const char* mp=0; |
|---|
| 227 | for (; *s&&*mask!='*'; mask++,s++) if (*mask!=*s&&*mask!='?') return 0; |
|---|
| 228 | for (;;) { |
|---|
| 229 | if (!*s) { while (*mask=='*') mask++; return !*mask; } |
|---|
| 230 | if (*mask=='*') { if (!*++mask) return 1; mp=mask; cp=s+1; continue; } |
|---|
| 231 | if (*mask==*s||*mask=='?') { mask++, s++; continue; } |
|---|
| 232 | mask=mp; s=cp++; |
|---|
| 233 | } |
|---|
| 234 | } |
|---|