| 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 | static HRESULT PathIsRemote(__in LPCWSTR pTargetFolder, __inout BOOL* fPathRemote); |
| 6 | static HRESULT PathIsRemovable(__in LPCWSTR pTargetFolder, __inout BOOL* fPathRemovable); |
| 7 | |
| 8 | /******************************************************************** |
| 9 | ValidatePath - Custom Action entry point |
| 10 | |
| 11 | ********************************************************************/ |
| 12 | UINT __stdcall ValidatePath(MSIHANDLE hInstall) |
| 13 | { |
| 14 | HRESULT hr = S_OK; |
| 15 | |
| 16 | LPWSTR pwszWixUIDir = NULL; |
| 17 | LPWSTR pwszInstallPath = NULL; |
| 18 | BOOL fInstallPathIsRemote = TRUE; |
| 19 | BOOL fInstallPathIsRemoveable = TRUE; |
| 20 | |
| 21 | hr = WcaInitialize(hInstall, "ValidatePath"); |
| 22 | ExitOnFailure(hr, "failed to initialize"); |
| 23 | |
| 24 | hr = WcaGetProperty(L"WIXUI_INSTALLDIR", &pwszWixUIDir); |
| 25 | ExitOnFailure(hr, "failed to get WixUI Installation Directory"); |
| 26 | |
| 27 | hr = WcaGetProperty(pwszWixUIDir, &pwszInstallPath); |
| 28 | ExitOnFailure(hr, "failed to get Installation Directory"); |
| 29 | |
| 30 | hr = PathIsRemote(pwszInstallPath, &fInstallPathIsRemote); |
| 31 | if (FAILED(hr)) |
| 32 | { |
| 33 | TraceError(hr, "Unable to determine if path is remote"); |
| 34 | //reset HR, as we need to continue and find out if is a UNC path |
| 35 | hr = S_OK; |
| 36 | } |
| 37 | |
| 38 | hr = PathIsRemovable(pwszInstallPath, &fInstallPathIsRemoveable); |
| 39 | if (FAILED(hr)) |
| 40 | { |
| 41 | TraceError(hr, "Unable to determine if path is removable"); |
| 42 | //reset HR, as we need to continue and find out if is a UNC path |
| 43 | hr = S_OK; |
| 44 | } |
| 45 | |
| 46 | // If the path does not point to a network drive, mapped drive, or removable drive, |
| 47 | // then set WIXUI_INSTALLDIR_VALID to "1" otherwise set it to 0 |
| 48 | BOOL fInstallPathIsUnc = ::PathIsUNCW(pwszInstallPath); |
| 49 | if (!fInstallPathIsUnc && !fInstallPathIsRemote && !fInstallPathIsRemoveable) |
| 50 | { |
| 51 | // path is valid |
| 52 | hr = WcaSetProperty(L"WIXUI_INSTALLDIR_VALID", L"1"); |
| 53 | ExitOnFailure(hr, "failed to set WIXUI_INSTALLDIR_VALID"); |
| 54 | } |
| 55 | else |
| 56 | { |
| 57 | // path is invalid; we can't log it because we're being called from a DoAction control event |
| 58 | // but we can at least call WcaLog to get it to write to the debugger from a debug build |
| 59 | WcaLog(LOGMSG_STANDARD, "Installation path %ls is invalid: it is %s UNC path, %s remote path, or %s path on a removable drive, and must be none of these.", |
| 60 | pwszInstallPath, fInstallPathIsUnc ? "a" : "not a", fInstallPathIsRemote ? "a" : "not a", fInstallPathIsRemoveable ? "a" : "not a"); |
| 61 | hr = WcaSetProperty(L"WIXUI_INSTALLDIR_VALID", L"0"); |
| 62 | ExitOnFailure(hr, "failed to set WIXUI_INSTALLDIR_VALID"); |
| 63 | } |
| 64 | |
| 65 | LExit: |
| 66 | ReleaseStr(pwszInstallPath); |
| 67 | ReleaseStr(pwszWixUIDir); |
| 68 | |
| 69 | return WcaFinalize(SUCCEEDED(hr) ? ERROR_SUCCESS : ERROR_INSTALL_FAILURE); |
| 70 | } |
| 71 | |
| 72 | /******************************************************************** |
| 73 | PathIsRemote - helper function for ValidatePath |
| 74 | |
| 75 | ********************************************************************/ |
| 76 | static HRESULT PathIsRemote(__in LPCWSTR pTargetFolder, __inout BOOL* fPathRemote) |
| 77 | { |
| 78 | HRESULT hr = S_OK; |
| 79 | LPWSTR pStrippedTargetFolder = NULL; |
| 80 | |
| 81 | hr = StrAllocString(&pStrippedTargetFolder, pTargetFolder, 0); |
| 82 | |
| 83 | // Terminate the path at the root |
| 84 | if (!::PathStripToRootW(pStrippedTargetFolder)) |
| 85 | { |
| 86 | hr = HRESULT_FROM_WIN32(ERROR_INVALID_DRIVE); |
| 87 | ExitOnFailure(hr, "failed to parse target folder"); |
| 88 | } |
| 89 | |
| 90 | UINT uResult = ::GetDriveTypeW(pStrippedTargetFolder); |
| 91 | |
| 92 | *fPathRemote = (DRIVE_REMOTE == uResult); |
| 93 | |
| 94 | LExit: |
| 95 | ReleaseStr(pStrippedTargetFolder); |
| 96 | |
| 97 | return hr; |
| 98 | } |
| 99 | |
| 100 | /******************************************************************** |
| 101 | PathIsRemovable - helper function for ValidatePath |
| 102 | |
| 103 | ********************************************************************/ |
| 104 | static HRESULT PathIsRemovable(__in LPCWSTR pTargetFolder, __inout BOOL* fPathRemovable) |
| 105 | { |
| 106 | HRESULT hr = S_OK; |
| 107 | LPWSTR pStrippedTargetFolder = NULL; |
| 108 | |
| 109 | hr = StrAllocString(&pStrippedTargetFolder, pTargetFolder, 0); |
| 110 | |
| 111 | // Terminate the path at the root |
| 112 | if (!::PathStripToRootW(pStrippedTargetFolder)) |
| 113 | { |
| 114 | hr = HRESULT_FROM_WIN32(ERROR_INVALID_DRIVE); |
| 115 | ExitOnFailure(hr, "failed to parse target folder"); |
| 116 | } |
| 117 | |
| 118 | UINT uResult = ::GetDriveTypeW(pStrippedTargetFolder); |
| 119 | |
| 120 | *fPathRemovable = ((DRIVE_CDROM == uResult) || (DRIVE_REMOVABLE == uResult) || (DRIVE_RAMDISK == uResult) || (DRIVE_UNKNOWN == uResult)); |
| 121 | |
| 122 | LExit: |
| 123 | ReleaseStr(pStrippedTargetFolder); |
| 124 | |
| 125 | return hr; |
| 126 | } |