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