1 /*************************************************************************
2  *
3  * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
4  *
5  * Copyright 2000, 2010 Oracle and/or its affiliates.
6  *
7  * OpenOffice.org - a multi-platform office productivity suite
8  *
9  * This file is part of OpenOffice.org.
10  *
11  * OpenOffice.org is free software: you can redistribute it and/or modify
12  * it under the terms of the GNU Lesser General Public License version 3
13  * only, as published by the Free Software Foundation.
14  *
15  * OpenOffice.org is distributed in the hope that it will be useful,
16  * but WITHOUT ANY WARRANTY; without even the implied warranty of
17  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
18  * GNU Lesser General Public License version 3 for more details
19  * (a copy is included in the LICENSE file that accompanied this code).
20  *
21  * You should have received a copy of the GNU Lesser General Public License
22  * version 3 along with OpenOffice.org.  If not, see
23  * <http://www.openoffice.org/license.html>
24  * for a copy of the LGPLv3 License.
25  *
26  ************************************************************************/
27 
28 #include "sal/config.h"
29 
30 #include <cstddef>
31 #include <new>
32 #include <string.h> // <cstring> not supported by old MSC versions
33 
34 #define WIN32_LEAN_AND_MEAN
35 #if defined _MSC_VER
36 #pragma warning(push, 1)
37 #endif
38 #include <windows.h>
39 #include <msiquery.h>
40 #include <shellapi.h>
41 #if defined _MSC_VER
42 #pragma warning(pop)
43 #endif
44 
45 #include "boost/scoped_array.hpp"
46 
47 #define LCL_LENGTH0(s) (sizeof (s) / sizeof *(s))
48 #define LCL_STRING0(s) (s), LCL_LENGTH0(s)
49 
50 namespace {
51 
52 enum Status { STATUS_NO, STATUS_YES, STATUS_ERROR };
53 
54 Status fileExists(wchar_t const * path) {
55     return GetFileAttributesW(path) == INVALID_FILE_ATTRIBUTES
56         ? GetLastError() == ERROR_FILE_NOT_FOUND ? STATUS_NO : STATUS_ERROR
57         : STATUS_YES;
58 }
59 
60 wchar_t * getProperty(
61     MSIHANDLE install, wchar_t const * name, wchar_t const * suffix,
62     std::size_t suffixLength, wchar_t ** end = NULL)
63 {
64     DWORD n = 0;
65     UINT err = MsiGetPropertyW(install, name, L"", &n);
66     if (err != ERROR_SUCCESS && err != ERROR_MORE_DATA) {
67         return NULL;
68     }
69     DWORD n2 = n + suffixLength; //TODO: overflow
70     wchar_t * data = new(std::nothrow) wchar_t[n2];
71     if (data == NULL) {
72         return NULL;
73     }
74     if (MsiGetPropertyW(install, name, data, &n2) != ERROR_SUCCESS || n2 != n) {
75         delete[] data;
76         return NULL;
77     }
78     memcpy(data + n, suffix, suffixLength * sizeof (wchar_t)); //TODO: overflow
79     if (end != NULL) {
80         *end = data + n + suffixLength;
81     }
82     return data;
83 }
84 
85 }
86 
87 extern "C" UINT __stdcall copyEditionData(MSIHANDLE install) {
88     boost::scoped_array<wchar_t> from(
89         getProperty(install, L"SourceDir", LCL_STRING0(L"edition\0")));
90     if (!from) {
91         return ERROR_INSTALL_FAILURE;
92     }
93     Status stat = fileExists(from.get());
94     if (stat == STATUS_ERROR) {
95         return ERROR_INSTALL_FAILURE;
96     }
97     if (stat == STATUS_NO) {
98         return ERROR_SUCCESS;
99     }
100     wchar_t * end;
101     boost::scoped_array<wchar_t> to(
102         getProperty(
103             install, L"INSTALLLOCATION",
104             LCL_STRING0(L"program\\edition\0"), &end));
105     if (!to) {
106         return ERROR_INSTALL_FAILURE;
107     }
108     stat = fileExists(to.get());
109     if (stat == STATUS_ERROR) {
110         return ERROR_INSTALL_FAILURE;
111     }
112     if (stat == STATUS_YES) {
113         SHFILEOPSTRUCTW opDelete = {
114             NULL, FO_DELETE, to.get(), NULL, FOF_NOCONFIRMATION | FOF_SILENT,
115             FALSE, NULL, NULL }; //TODO: non-NULL hwnd
116         if (SHFileOperationW(&opDelete) != 0) {
117             return ERROR_INSTALL_FAILURE;
118         }
119     }
120     *(end - LCL_LENGTH0(L"\\edition\0")) = L'\0';
121     *(end - LCL_LENGTH0(L"\\edition\0") + 1) = L'\0';
122     SHFILEOPSTRUCTW opCopy = {
123         NULL, FO_COPY, from.get(), to.get(),
124         FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_SILENT, FALSE, NULL,
125         NULL }; //TODO: non-NULL hwnd
126     if (SHFileOperationW(&opCopy) != 0) {
127         return ERROR_INSTALL_FAILURE;
128     }
129     return ERROR_SUCCESS;
130 }
131