diff options
author | Sean Hall <r.sean.hall@gmail.com> | 2021-08-03 15:41:02 -0500 |
---|---|---|
committer | Sean Hall <r.sean.hall@gmail.com> | 2021-08-04 10:03:57 -0500 |
commit | ed57d171f6fb6bb4e180696cc12caa568599566a (patch) | |
tree | c1e6cbe61440d44f3f11160512e12042ce7ddd2a /src/libs/dutil | |
parent | 5815688519a60e63e18c13dfe0908d76757cbc53 (diff) | |
download | wix-ed57d171f6fb6bb4e180696cc12caa568599566a.tar.gz wix-ed57d171f6fb6bb4e180696cc12caa568599566a.tar.bz2 wix-ed57d171f6fb6bb4e180696cc12caa568599566a.zip |
Get system TEMP path from the registry.
Also, correctly handle return codes from related path APIs.
Diffstat (limited to 'src/libs/dutil')
-rw-r--r-- | src/libs/dutil/WixToolset.DUtil/inc/pathutil.h | 16 | ||||
-rw-r--r-- | src/libs/dutil/WixToolset.DUtil/pathutil.cpp | 87 |
2 files changed, 103 insertions, 0 deletions
diff --git a/src/libs/dutil/WixToolset.DUtil/inc/pathutil.h b/src/libs/dutil/WixToolset.DUtil/inc/pathutil.h index 579b8454..0ae9f437 100644 --- a/src/libs/dutil/WixToolset.DUtil/inc/pathutil.h +++ b/src/libs/dutil/WixToolset.DUtil/inc/pathutil.h | |||
@@ -151,6 +151,22 @@ DAPI_(HRESULT) PathCreateTempDirectory( | |||
151 | ); | 151 | ); |
152 | 152 | ||
153 | /******************************************************************* | 153 | /******************************************************************* |
154 | PathGetTempPath - returns the path to the temp folder | ||
155 | that is backslash terminated. | ||
156 | *******************************************************************/ | ||
157 | DAPI_(HRESULT) PathGetTempPath( | ||
158 | __out_z LPWSTR* psczTempPath | ||
159 | ); | ||
160 | |||
161 | /******************************************************************* | ||
162 | PathGetSystemTempPath - returns the path to the system temp folder | ||
163 | that is backslash terminated. | ||
164 | *******************************************************************/ | ||
165 | DAPI_(HRESULT) PathGetSystemTempPath( | ||
166 | __out_z LPWSTR* psczSystemTempPath | ||
167 | ); | ||
168 | |||
169 | /******************************************************************* | ||
154 | PathGetKnownFolder - returns the path to a well-known shell folder | 170 | PathGetKnownFolder - returns the path to a well-known shell folder |
155 | 171 | ||
156 | *******************************************************************/ | 172 | *******************************************************************/ |
diff --git a/src/libs/dutil/WixToolset.DUtil/pathutil.cpp b/src/libs/dutil/WixToolset.DUtil/pathutil.cpp index 7c3cfe06..5fad519b 100644 --- a/src/libs/dutil/WixToolset.DUtil/pathutil.cpp +++ b/src/libs/dutil/WixToolset.DUtil/pathutil.cpp | |||
@@ -9,6 +9,7 @@ | |||
9 | #define PathExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__) | 9 | #define PathExitWithLastError(x, s, ...) ExitWithLastErrorSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__) |
10 | #define PathExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__) | 10 | #define PathExitOnFailure(x, s, ...) ExitOnFailureSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__) |
11 | #define PathExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__) | 11 | #define PathExitOnRootFailure(x, s, ...) ExitOnRootFailureSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__) |
12 | #define PathExitWithRootFailure(x, e, s, ...) ExitWithRootFailureSource(DUTIL_SOURCE_PATHUTIL, x, e, s, __VA_ARGS__) | ||
12 | #define PathExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__) | 13 | #define PathExitOnFailureDebugTrace(x, s, ...) ExitOnFailureDebugTraceSource(DUTIL_SOURCE_PATHUTIL, x, s, __VA_ARGS__) |
13 | #define PathExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_PATHUTIL, p, x, e, s, __VA_ARGS__) | 14 | #define PathExitOnNull(p, x, e, s, ...) ExitOnNullSource(DUTIL_SOURCE_PATHUTIL, p, x, e, s, __VA_ARGS__) |
14 | #define PathExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_PATHUTIL, p, x, s, __VA_ARGS__) | 15 | #define PathExitOnNullWithLastError(p, x, s, ...) ExitOnNullWithLastErrorSource(DUTIL_SOURCE_PATHUTIL, p, x, s, __VA_ARGS__) |
@@ -814,6 +815,92 @@ LExit: | |||
814 | } | 815 | } |
815 | 816 | ||
816 | 817 | ||
818 | DAPI_(HRESULT) PathGetTempPath( | ||
819 | __out_z LPWSTR* psczTempPath | ||
820 | ) | ||
821 | { | ||
822 | HRESULT hr = S_OK; | ||
823 | WCHAR wzTempPath[MAX_PATH + 1] = { }; | ||
824 | DWORD cch = 0; | ||
825 | |||
826 | cch = ::GetTempPathW(countof(wzTempPath), wzTempPath); | ||
827 | if (!cch) | ||
828 | { | ||
829 | PathExitWithLastError(hr, "Failed to GetTempPath."); | ||
830 | } | ||
831 | else if (cch >= countof(wzTempPath)) | ||
832 | { | ||
833 | PathExitWithRootFailure(hr, E_INSUFFICIENT_BUFFER, "TEMP directory path too long."); | ||
834 | } | ||
835 | |||
836 | hr = StrAllocString(psczTempPath, wzTempPath, cch); | ||
837 | PathExitOnFailure(hr, "Failed to copy TEMP directory path."); | ||
838 | |||
839 | LExit: | ||
840 | return hr; | ||
841 | } | ||
842 | |||
843 | |||
844 | DAPI_(HRESULT) PathGetSystemTempPath( | ||
845 | __out_z LPWSTR* psczSystemTempPath | ||
846 | ) | ||
847 | { | ||
848 | HRESULT hr = S_OK; | ||
849 | HKEY hKey = NULL; | ||
850 | WCHAR wzTempPath[MAX_PATH + 1] = { }; | ||
851 | DWORD cch = 0; | ||
852 | |||
853 | // There is no documented API to get system environment variables, so read them from the registry. | ||
854 | hr = RegOpen(HKEY_LOCAL_MACHINE, L"System\\CurrentControlSet\\Control\\Session Manager\\Environment", KEY_READ, &hKey); | ||
855 | if (E_FILENOTFOUND != hr) | ||
856 | { | ||
857 | PathExitOnFailure(hr, "Failed to open system environment registry key."); | ||
858 | |||
859 | // Follow documented precedence rules for TMP/TEMP from ::GetTempPath. | ||
860 | // TODO: values will be expanded with the current environment variables instead of the system environment variables. | ||
861 | hr = RegReadString(hKey, L"TMP", psczSystemTempPath); | ||
862 | if (E_FILENOTFOUND != hr) | ||
863 | { | ||
864 | PathExitOnFailure(hr, "Failed to get system TMP value."); | ||
865 | |||
866 | hr = PathBackslashTerminate(psczSystemTempPath); | ||
867 | PathExitOnFailure(hr, "Failed to backslash terminate system TMP value."); | ||
868 | |||
869 | ExitFunction(); | ||
870 | } | ||
871 | |||
872 | hr = RegReadString(hKey, L"TEMP", psczSystemTempPath); | ||
873 | if (E_FILENOTFOUND != hr) | ||
874 | { | ||
875 | PathExitOnFailure(hr, "Failed to get system TEMP value."); | ||
876 | |||
877 | hr = PathBackslashTerminate(psczSystemTempPath); | ||
878 | PathExitOnFailure(hr, "Failed to backslash terminate system TEMP value."); | ||
879 | |||
880 | ExitFunction(); | ||
881 | } | ||
882 | } | ||
883 | |||
884 | cch = ::GetSystemWindowsDirectoryW(wzTempPath, countof(wzTempPath)); | ||
885 | if (!cch) | ||
886 | { | ||
887 | PathExitWithLastError(hr, "Failed to get Windows directory path."); | ||
888 | } | ||
889 | else if (cch >= countof(wzTempPath)) | ||
890 | { | ||
891 | PathExitWithRootFailure(hr, E_INSUFFICIENT_BUFFER, "Windows directory path too long."); | ||
892 | } | ||
893 | |||
894 | hr = PathConcat(wzTempPath, L"TEMP\\", psczSystemTempPath); | ||
895 | PathExitOnFailure(hr, "Failed to concat Temp directory on Windows directory path."); | ||
896 | |||
897 | LExit: | ||
898 | ReleaseRegKey(hKey); | ||
899 | |||
900 | return hr; | ||
901 | } | ||
902 | |||
903 | |||
817 | DAPI_(HRESULT) PathGetKnownFolder( | 904 | DAPI_(HRESULT) PathGetKnownFolder( |
818 | __in int csidl, | 905 | __in int csidl, |
819 | __out LPWSTR* psczKnownFolder | 906 | __out LPWSTR* psczKnownFolder |