| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | #include "precomp.h" |
| 4 | |
| 5 | // Whilst ::GetFileAttributesW might return FILE_ATTRIBUTE_INVALID for an invalid path, it's not a header defined variable. |
| 6 | // so define it here, but guard it to avoid redefining it if Microsoft change their mind |
| 7 | #ifndef FILE_ATTRIBUTE_INVALID |
| 8 | #define FILE_ATTRIBUTE_INVALID ((DWORD)-1) |
| 9 | #endif |
| 10 | |
| 11 | LPCWSTR vcsRemoveFolderExQuery = |
| 12 | L"SELECT `RemoveFolderEx`, `Component_`, `Property`, `InstallMode`, `Wix4RemoveFolderEx`.`Condition`, `Component`.`Attributes` " |
| 13 | L"FROM `Wix4RemoveFolderEx`,`Component` " |
| 14 | L"WHERE `Wix4RemoveFolderEx`.`Component_`=`Component`.`Component`"; |
| 15 | enum eRemoveFolderExQuery { rfqId = 1, rfqComponent, rfqProperty, rfqMode, rfqCondition, rfqComponentAttributes }; |
| 16 | |
| 17 | static HRESULT RecursePath( |
| 18 | __in_z LPCWSTR wzPath, |
| 19 | __in_z LPCWSTR wzId, |
| 20 | __in_z LPCWSTR wzComponent, |
| 21 | __in_z LPCWSTR wzProperty, |
| 22 | __in int iMode, |
| 23 | __in BOOL fDisableWow64Redirection, |
| 24 | __inout DWORD* pdwCounter, |
| 25 | __inout MSIHANDLE* phTable, |
| 26 | __inout MSIHANDLE* phColumns |
| 27 | ) |
| 28 | { |
| 29 | HRESULT hr = S_OK; |
| 30 | DWORD er; |
| 31 | LPWSTR sczSearch = NULL; |
| 32 | LPWSTR sczProperty = NULL; |
| 33 | HANDLE hFind = INVALID_HANDLE_VALUE; |
| 34 | WIN32_FIND_DATAW wfd; |
| 35 | LPWSTR sczNext = NULL; |
| 36 | |
| 37 | #ifdef _WIN64 |
| 38 | UNREFERENCED_PARAMETER(fDisableWow64Redirection); |
| 39 | #else |
| 40 | if (fDisableWow64Redirection) |
| 41 | { |
| 42 | hr = WcaDisableWow64FSRedirection(); |
| 43 | ExitOnFailure(hr, "Custom action was told to act on a 64-bit component, but was unable to disable filesystem redirection through the Wow64 API."); |
| 44 | } |
| 45 | #endif |
| 46 | |
| 47 | DWORD dwAttributes = ::GetFileAttributesW(wzPath); |
| 48 | if (FILE_ATTRIBUTE_INVALID == dwAttributes) |
| 49 | { |
| 50 | WcaLog(LOGMSG_STANDARD, "Path is invalid: %ls", wzPath); |
| 51 | ExitFunction(); |
| 52 | } |
| 53 | |
| 54 | // Do NOT follow junctions. |
| 55 | if (dwAttributes & FILE_ATTRIBUTE_REPARSE_POINT) |
| 56 | { |
| 57 | WcaLog(LOGMSG_STANDARD, "Path is a junction; skipping: %ls", wzPath); |
| 58 | ExitFunction(); |
| 59 | } |
| 60 | |
| 61 | // First recurse down to all the child directories. |
| 62 | hr = StrAllocFormatted(&sczSearch, L"%s*", wzPath); |
| 63 | ExitOnFailure(hr, "Failed to allocate file search string in path: %S", wzPath); |
| 64 | |
| 65 | hFind = ::FindFirstFileW(sczSearch, &wfd); |
| 66 | if (INVALID_HANDLE_VALUE == hFind) |
| 67 | { |
| 68 | er = ::GetLastError(); |
| 69 | if (ERROR_PATH_NOT_FOUND == er) |
| 70 | { |
| 71 | WcaLog(LOGMSG_STANDARD, "Search path not found: %ls; skipping", sczSearch); |
| 72 | ExitFunction1(hr = S_FALSE); |
| 73 | } |
| 74 | else |
| 75 | { |
| 76 | hr = HRESULT_FROM_WIN32(er); |
| 77 | } |
| 78 | ExitOnFailure(hr, "Failed to find all files in path: %S", wzPath); |
| 79 | } |
| 80 | |
| 81 | do |
| 82 | { |
| 83 | // Skip files and the dot directories. |
| 84 | if (FILE_ATTRIBUTE_DIRECTORY != (wfd.dwFileAttributes & FILE_ATTRIBUTE_DIRECTORY) || L'.' == wfd.cFileName[0] && (L'\0' == wfd.cFileName[1] || (L'.' == wfd.cFileName[1] && L'\0' == wfd.cFileName[2]))) |
| 85 | { |
| 86 | continue; |
| 87 | } |
| 88 | |
| 89 | hr = StrAllocFormatted(&sczNext, L"%s%s\\", wzPath, wfd.cFileName); |
| 90 | ExitOnFailure(hr, "Failed to concat filename '%S' to string: %S", wfd.cFileName, wzPath); |
| 91 | |
| 92 | // Don't re-disable redirection; if it was necessary, we've already done it. |
| 93 | hr = RecursePath(sczNext, wzId, wzComponent, wzProperty, iMode, FALSE, pdwCounter, phTable, phColumns); |
| 94 | ExitOnFailure(hr, "Failed to recurse path: %S", sczNext); |
| 95 | } while (::FindNextFileW(hFind, &wfd)); |
| 96 | |
| 97 | er = ::GetLastError(); |
| 98 | if (ERROR_NO_MORE_FILES == er) |
| 99 | { |
| 100 | hr = S_OK; |
| 101 | } |
| 102 | else |
| 103 | { |
| 104 | hr = HRESULT_FROM_WIN32(er); |
| 105 | ExitOnFailure(hr, "Failed while looping through files in directory: %S", wzPath); |
| 106 | } |
| 107 | |
| 108 | // Finally, set a property that points at our path. |
| 109 | hr = StrAllocFormatted(&sczProperty, L"_%s_%u", wzProperty, *pdwCounter); |
| 110 | ExitOnFailure(hr, "Failed to allocate Property for RemoveFile table with property: %S.", wzProperty); |
| 111 | |
| 112 | ++(*pdwCounter); |
| 113 | |
| 114 | hr = WcaSetProperty(sczProperty, wzPath); |
| 115 | ExitOnFailure(hr, "Failed to set Property: %S with path: %S", sczProperty, wzPath); |
| 116 | |
| 117 | // Add the row to remove any files and another row to remove the folder. |
| 118 | hr = WcaAddTempRecord(phTable, phColumns, L"RemoveFile", NULL, 1, 5, L"RfxFiles", wzComponent, L"*.*", sczProperty, iMode); |
| 119 | ExitOnFailure(hr, "Failed to add row to remove all files for Wix4RemoveFolderEx row: %ls under path: %ls", wzId, wzPath); |
| 120 | |
| 121 | hr = WcaAddTempRecord(phTable, phColumns, L"RemoveFile", NULL, 1, 5, L"RfxFolder", wzComponent, NULL, sczProperty, iMode); |
| 122 | ExitOnFailure(hr, "Failed to add row to remove folder for Wix4RemoveFolderEx row: %ls under path: %ls", wzId, wzPath); |
| 123 | |
| 124 | LExit: |
| 125 | if (INVALID_HANDLE_VALUE != hFind) |
| 126 | { |
| 127 | ::FindClose(hFind); |
| 128 | } |
| 129 | |
| 130 | #ifndef _WIN64 |
| 131 | if (fDisableWow64Redirection) |
| 132 | { |
| 133 | WcaRevertWow64FSRedirection(); |
| 134 | } |
| 135 | #endif |
| 136 | |
| 137 | ReleaseStr(sczNext); |
| 138 | ReleaseStr(sczProperty); |
| 139 | ReleaseStr(sczSearch); |
| 140 | return hr; |
| 141 | } |
| 142 | |
| 143 | extern "C" UINT WINAPI WixRemoveFoldersEx( |
| 144 | __in MSIHANDLE hInstall |
| 145 | ) |
| 146 | { |
| 147 | //AssertSz(FALSE, "debug WixRemoveFoldersEx"); |
| 148 | |
| 149 | HRESULT hr = S_OK; |
| 150 | PMSIHANDLE hView; |
| 151 | PMSIHANDLE hRec; |
| 152 | LPWSTR sczId = NULL; |
| 153 | LPWSTR sczComponent = NULL; |
| 154 | LPWSTR sczProperty = NULL; |
| 155 | LPWSTR sczCondition = NULL; |
| 156 | LPWSTR sczPath = NULL; |
| 157 | LPWSTR sczExpandedPath = NULL; |
| 158 | int iMode = 0; |
| 159 | int iComponentAttributes; |
| 160 | BOOL f64BitComponent = FALSE; |
| 161 | DWORD dwCounter = 0; |
| 162 | DWORD_PTR cchLen = 0; |
| 163 | MSIHANDLE hTable = NULL; |
| 164 | MSIHANDLE hColumns = NULL; |
| 165 | |
| 166 | hr = WcaInitialize(hInstall, "WixRemoveFoldersEx"); |
| 167 | ExitOnFailure(hr, "Failed to initialize WixRemoveFoldersEx."); |
| 168 | |
| 169 | #ifndef _WIN64 |
| 170 | WcaInitializeWow64(); |
| 171 | #endif |
| 172 | |
| 173 | // anything to do? |
| 174 | if (S_OK != WcaTableExists(L"Wix4RemoveFolderEx")) |
| 175 | { |
| 176 | WcaLog(LOGMSG_STANDARD, "Wix4RemoveFolderEx table doesn't exist, so there are no folders to remove."); |
| 177 | ExitFunction(); |
| 178 | } |
| 179 | |
| 180 | // query and loop through all the remove folders exceptions |
| 181 | hr = WcaOpenExecuteView(vcsRemoveFolderExQuery, &hView); |
| 182 | ExitOnFailure(hr, "Failed to open view on Wix4RemoveFolderEx table"); |
| 183 | |
| 184 | while (S_OK == (hr = WcaFetchRecord(hView, &hRec))) |
| 185 | { |
| 186 | hr = WcaGetRecordString(hRec, rfqId, &sczId); |
| 187 | ExitOnFailure(hr, "Failed to get remove folder identity."); |
| 188 | |
| 189 | hr = WcaGetRecordString(hRec, rfqCondition, &sczCondition); |
| 190 | ExitOnFailure(hr, "Failed to get remove folder condition."); |
| 191 | |
| 192 | if (sczCondition && *sczCondition) |
| 193 | { |
| 194 | MSICONDITION condition = ::MsiEvaluateConditionW(hInstall, sczCondition); |
| 195 | if (MSICONDITION_TRUE == condition) |
| 196 | { |
| 197 | WcaLog(LOGMSG_STANDARD, "True condition for row %S: %S; processing.", sczId, sczCondition); |
| 198 | } |
| 199 | else |
| 200 | { |
| 201 | WcaLog(LOGMSG_STANDARD, "False or invalid condition for row %S: %S; skipping.", sczId, sczCondition); |
| 202 | continue; |
| 203 | } |
| 204 | } |
| 205 | |
| 206 | hr = WcaGetRecordString(hRec, rfqComponent, &sczComponent); |
| 207 | ExitOnFailure(hr, "Failed to get remove folder component."); |
| 208 | |
| 209 | hr = WcaGetRecordString(hRec, rfqProperty, &sczProperty); |
| 210 | ExitOnFailure(hr, "Failed to get remove folder property."); |
| 211 | |
| 212 | hr = WcaGetRecordInteger(hRec, rfqMode, &iMode); |
| 213 | ExitOnFailure(hr, "Failed to get remove folder mode"); |
| 214 | |
| 215 | hr = WcaGetProperty(sczProperty, &sczPath); |
| 216 | ExitOnFailure(hr, "Failed to resolve remove folder property: %S for row: %S", sczProperty, sczId); |
| 217 | |
| 218 | hr = WcaGetRecordInteger(hRec, rfqComponentAttributes, &iComponentAttributes); |
| 219 | ExitOnFailure(hr, "failed to get component attributes for row: %ls", sczId); |
| 220 | |
| 221 | f64BitComponent = iComponentAttributes & msidbComponentAttributes64bit; |
| 222 | |
| 223 | // fail early if the property isn't set as you probably don't want your installers trying to delete SystemFolder |
| 224 | // StringCchLengthW succeeds only if the string is zero characters plus 1 for the terminating null |
| 225 | hr = ::StringCchLengthW(sczPath, 1, reinterpret_cast<UINT_PTR*>(&cchLen)); |
| 226 | if (SUCCEEDED(hr)) |
| 227 | { |
| 228 | ExitOnFailure(hr = E_INVALIDARG, "Missing folder property: %S for row: %S", sczProperty, sczId); |
| 229 | } |
| 230 | |
| 231 | hr = PathExpand(&sczExpandedPath, sczPath, PATH_EXPAND_ENVIRONMENT); |
| 232 | ExitOnFailure(hr, "Failed to expand path: %S for row: %S", sczPath, sczId); |
| 233 | |
| 234 | hr = PathBackslashTerminate(&sczExpandedPath); |
| 235 | ExitOnFailure(hr, "Failed to backslash-terminate path: %S", sczExpandedPath); |
| 236 | |
| 237 | WcaLog(LOGMSG_STANDARD, "Recursing path: %S for row: %S.", sczExpandedPath, sczId); |
| 238 | hr = RecursePath(sczExpandedPath, sczId, sczComponent, sczProperty, iMode, f64BitComponent, &dwCounter, &hTable, &hColumns); |
| 239 | ExitOnFailure(hr, "Failed while navigating path: %S for row: %S", sczPath, sczId); |
| 240 | } |
| 241 | |
| 242 | // reaching the end of the list is actually a good thing, not an error |
| 243 | if (E_NOMOREITEMS == hr) |
| 244 | { |
| 245 | hr = S_OK; |
| 246 | } |
| 247 | ExitOnFailure(hr, "Failure occurred while processing Wix4RemoveFolderEx table"); |
| 248 | |
| 249 | LExit: |
| 250 | #ifndef _WIN64 |
| 251 | WcaFinalizeWow64(); |
| 252 | #endif |
| 253 | |
| 254 | if (hColumns) |
| 255 | { |
| 256 | ::MsiCloseHandle(hColumns); |
| 257 | } |
| 258 | |
| 259 | if (hTable) |
| 260 | { |
| 261 | ::MsiCloseHandle(hTable); |
| 262 | } |
| 263 | |
| 264 | ReleaseStr(sczExpandedPath); |
| 265 | ReleaseStr(sczPath); |
| 266 | ReleaseStr(sczProperty); |
| 267 | ReleaseStr(sczComponent); |
| 268 | ReleaseStr(sczCondition); |
| 269 | ReleaseStr(sczId); |
| 270 | |
| 271 | DWORD er = SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE; |
| 272 | return WcaFinalize(er); |
| 273 | } |