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 #include "boost/scoped_array.hpp"
42 
43 #define LCL_LENGTH0(s) (sizeof (s) / sizeof *(s))
44 #define LCL_STRING0(s) (s), LCL_LENGTH0(s)
45 
46 namespace {
47 
48 enum Status { STATUS_NO, STATUS_YES, STATUS_ERROR };
49 
50 Status fileExists(wchar_t const * path) {
51     return GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES
52         ? GetLastError() == ERROR_FILE_NOT_FOUND ? STATUS_NO : STATUS_ERROR
53         : STATUS_YES;
54 }
55 
56 wchar_t * getProperty(
57     MSIHANDLE install, wchar_t const * name, wchar_t const * suffix,
58     std::size_t suffixLength, wchar_t ** end = NULL)
59 {
60     DWORD n = 0;
61     UINT err = MsiGetPropertyW(install, name, L"", &n);
62     if (err != ERROR_SUCCESS && err != ERROR_MORE_DATA) {
63         return NULL;
64     }
65     DWORD n2 = n + suffixLength; //TODO: overflow
66     wchar_t * data = new(std::nothrow) wchar_t[n2];
67     if (data == NULL) {
68         return NULL;
69     }
70     if (MsiGetPropertyW(install, name, data, &n2) != ERROR_SUCCESS || n2 != n) {
71         delete[] data;
72         return NULL;
73     }
74     memcpy(data + n, suffix, suffixLength * sizeof (wchar_t)); //TODO: overflow
75     if (end != NULL) {
76         *end = data + n + suffixLength;
77     }
78     return data;
79 }
80 
81 }
82 
83 extern "C" UINT __stdcall copyEditionData(MSIHANDLE install) {
84     boost::scoped_array<wchar_t> from(
85         getProperty(install, L"SourceDir", LCL_STRING0(L"edition\0")));
86     if (!from) {
87         return ERROR_INSTALL_FAILURE;
88     }
89     Status stat = fileExists(from.get());
90     if (stat == STATUS_ERROR) {
91         return ERROR_INSTALL_FAILURE;
92     }
93     if (stat == STATUS_NO) {
94         return ERROR_SUCCESS;
95     }
96     wchar_t * end;
97     boost::scoped_array<wchar_t> to(
98         getProperty(
99             install, L"INSTALLLOCATION",
100             LCL_STRING0(L"program\\edition\0"), &end));
101     if (!to) {
102         return ERROR_INSTALL_FAILURE;
103     }
104     stat = fileExists(to.get());
105     if (stat == STATUS_ERROR) {
106         return ERROR_INSTALL_FAILURE;
107     }
108     if (stat == STATUS_YES) {
109         SHFILEOPSTRUCTW opDelete = {
110             NULL, FO_DELETE, to.get(), NULL, FOF_NOCONFIRMATION | FOF_SILENT,
111             FALSE, NULL, NULL }; //TODO: non-NULL hwnd
112         if (SHFileOperationW(&opDelete) != 0) {
113             return ERROR_INSTALL_FAILURE;
114         }
115     }
116     *(end - LCL_LENGTH0(L"\\edition\0")) = L'\0';
117     *(end - LCL_LENGTH0(L"\\edition\0") + 1) = L'\0';
118     SHFILEOPSTRUCTW opCopy = {
119         NULL, FO_COPY, from.get(), to.get(),
120         FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_SILENT, FALSE, NULL,
121         NULL }; //TODO: non-NULL hwnd
122     if (SHFileOperationW(&opCopy) != 0) {
123         return ERROR_INSTALL_FAILURE;
124     }
125     return ERROR_SUCCESS;
126 }
127