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