1 /************************************************************** 2 * 3 * Licensed to the Apache Software Foundation (ASF) under one 4 * or more contributor license agreements. See the NOTICE file 5 * distributed with this work for additional information 6 * regarding copyright ownership. The ASF licenses this file 7 * to you under the Apache License, Version 2.0 (the 8 * "License"); you may not use this file except in compliance 9 * with the License. You may obtain a copy of the License at 10 * 11 * http://www.apache.org/licenses/LICENSE-2.0 12 * 13 * Unless required by applicable law or agreed to in writing, 14 * software distributed under the License is distributed on an 15 * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY 16 * KIND, either express or implied. See the License for the 17 * specific language governing permissions and limitations 18 * under the License. 19 * 20 *************************************************************/ 21 22 23 24 #include "sal/config.h" 25 26 #include <cstddef> 27 #include <new> 28 #include <string.h> // <cstring> not supported by old MSC versions 29 30 #define WIN32_LEAN_AND_MEAN 31 #if defined _MSC_VER 32 #pragma warning(push, 1) 33 #endif 34 #include <windows.h> 35 #include <msiquery.h> 36 #include <shellapi.h> 37 #if defined _MSC_VER 38 #pragma warning(pop) 39 #endif 40 41 #define LCL_LENGTH0(s) (sizeof (s) / sizeof *(s)) 42 #define LCL_STRING0(s) (s), LCL_LENGTH0(s) 43 44 namespace { 45 46 enum Status { STATUS_NO, STATUS_YES, STATUS_ERROR }; 47 48 Status fileExists(wchar_t const * path) { 49 return GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES 50 ? GetLastError() == ERROR_FILE_NOT_FOUND ? STATUS_NO : STATUS_ERROR 51 : STATUS_YES; 52 } 53 54 wchar_t * getProperty( 55 MSIHANDLE install, wchar_t const * name, wchar_t const * suffix, 56 std::size_t suffixLength, wchar_t ** end = NULL) 57 { 58 DWORD n = 0; 59 UINT err = MsiGetPropertyW(install, name, L"", &n); 60 if (err != ERROR_SUCCESS && err != ERROR_MORE_DATA) { 61 return NULL; 62 } 63 DWORD n2 = n + suffixLength; //TODO: overflow 64 wchar_t * data = new(std::nothrow) wchar_t[n2]; 65 if (data == NULL) { 66 return NULL; 67 } 68 if (MsiGetPropertyW(install, name, data, &n2) != ERROR_SUCCESS || n2 != n) { 69 delete[] data; 70 return NULL; 71 } 72 memcpy(data + n, suffix, suffixLength * sizeof (wchar_t)); //TODO: overflow 73 if (end != NULL) { 74 *end = data + n + suffixLength; 75 } 76 return data; 77 } 78 79 } 80 81 namespace { 82 class scoped_wchar_t_array 83 { 84 private: 85 wchar_t* mpArray; 86 87 public: 88 scoped_wchar_t_array( wchar_t* pArray ) 89 : mpArray( pArray ) 90 {} 91 92 ~scoped_wchar_t_array() 93 { 94 if ( mpArray != 0 ) 95 { 96 delete[] mpArray; 97 } 98 } 99 100 wchar_t* get() 101 { 102 return mpArray; 103 } 104 }; 105 } 106 107 extern "C" UINT __stdcall copyEditionData(MSIHANDLE install) { 108 scoped_wchar_t_array from( 109 getProperty(install, L"SourceDir", LCL_STRING0(L"edition\0"))); 110 if (!from.get()) { 111 return ERROR_INSTALL_FAILURE; 112 } 113 Status stat = fileExists(from.get()); 114 if (stat == STATUS_ERROR) { 115 return ERROR_INSTALL_FAILURE; 116 } 117 if (stat == STATUS_NO) { 118 return ERROR_SUCCESS; 119 } 120 wchar_t * end; 121 scoped_wchar_t_array to( 122 getProperty( 123 install, L"INSTALLLOCATION", 124 LCL_STRING0(L"program\\edition\0"), &end)); 125 if (!to.get()) { 126 return ERROR_INSTALL_FAILURE; 127 } 128 stat = fileExists(to.get()); 129 if (stat == STATUS_ERROR) { 130 return ERROR_INSTALL_FAILURE; 131 } 132 if (stat == STATUS_YES) { 133 SHFILEOPSTRUCTW opDelete = { 134 NULL, FO_DELETE, to.get(), NULL, FOF_NOCONFIRMATION | FOF_SILENT, 135 FALSE, NULL, NULL }; //TODO: non-NULL hwnd 136 if (SHFileOperationW(&opDelete) != 0) { 137 return ERROR_INSTALL_FAILURE; 138 } 139 } 140 *(end - LCL_LENGTH0(L"\\edition\0")) = L'\0'; 141 *(end - LCL_LENGTH0(L"\\edition\0") + 1) = L'\0'; 142 SHFILEOPSTRUCTW opCopy = { 143 NULL, FO_COPY, from.get(), to.get(), 144 FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_SILENT, FALSE, NULL, 145 NULL }; //TODO: non-NULL hwnd 146 if (SHFileOperationW(&opCopy) != 0) { 147 return ERROR_INSTALL_FAILURE; 148 } 149 return ERROR_SUCCESS; 150 } 151