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 #define _WIN32_WINDOWS 0x0410
29 
30 #ifdef _MSC_VER
31 #pragma warning(push, 1) /* disable warnings within system headers */
32 #endif
33 #define WIN32_LEAN_AND_MEAN
34 #include <windows.h>
35 #include <msiquery.h>
36 #ifdef _MSC_VER
37 #pragma warning(pop)
38 #endif
39 
40 #include <malloc.h>
41 #include <assert.h>
42 
43 #ifdef UNICODE
44 #define _UNICODE
45 #define _tstring	wstring
46 #else
47 #define _tstring	string
48 #endif
49 #include <tchar.h>
50 #include <string>
51 #include <queue>
52 #include <stdio.h>
53 
54 #include <systools/win32/uwinapi.h>
55 #include <../tools/seterror.hxx>
56 
57 #define	WININIT_FILENAME	"wininit.ini"
58 #define RENAME_SECTION		"rename"
59 
60 #ifdef DEBUG
61 inline void OutputDebugStringFormat( LPCTSTR pFormat, ... )
62 {
63 	_TCHAR	buffer[1024];
64 	va_list	args;
65 
66 	va_start( args, pFormat );
67 	_vsntprintf( buffer, elementsof(buffer), pFormat, args );
68 	OutputDebugString( buffer );
69 }
70 #else
71 static inline void OutputDebugStringFormat( LPCTSTR, ... )
72 {
73 }
74 #endif
75 
76 static std::_tstring GetMsiProperty( MSIHANDLE handle, const std::_tstring& sProperty )
77 {
78 	std::_tstring	result;
79 	TCHAR	szDummy[1] = TEXT("");
80 	DWORD	nChars = 0;
81 
82 	if ( MsiGetProperty( handle, sProperty.c_str(), szDummy, &nChars ) == ERROR_MORE_DATA )
83 	{
84 		DWORD nBytes = ++nChars * sizeof(TCHAR);
85 		LPTSTR buffer = reinterpret_cast<LPTSTR>(_alloca(nBytes));
86 		ZeroMemory( buffer, nBytes );
87 		MsiGetProperty(handle, sProperty.c_str(), buffer, &nChars);
88 		result = buffer;
89 	}
90 
91 	return	result;
92 }
93 
94 static inline bool IsSetMsiProperty(MSIHANDLE handle, const std::_tstring& sProperty)
95 {
96 	std::_tstring value = GetMsiProperty(handle, sProperty);
97 	return (value.length() > 0);
98 }
99 
100 static inline void UnsetMsiProperty(MSIHANDLE handle, const std::_tstring& sProperty)
101 {
102 	MsiSetProperty(handle, sProperty.c_str(), NULL);
103 }
104 
105 static inline void SetMsiProperty(MSIHANDLE handle, const std::_tstring& sProperty)
106 {
107 	MsiSetProperty(handle, sProperty.c_str(), TEXT("1"));
108 }
109 
110 static BOOL MoveFileEx9x( LPCSTR lpExistingFileNameA, LPCSTR lpNewFileNameA, DWORD dwFlags )
111 {
112 	BOOL	fSuccess = FALSE;	// assume failure
113 
114 	// Windows 9x has a special mechanism to move files after reboot
115 
116 	if ( dwFlags & MOVEFILE_DELAY_UNTIL_REBOOT )
117 	{
118 		CHAR	szExistingFileNameA[MAX_PATH];
119 		CHAR	szNewFileNameA[MAX_PATH] = "NUL";
120 
121 		// Path names in WININIT.INI must be in short path name form
122 
123 		if (
124 			GetShortPathNameA( lpExistingFileNameA, szExistingFileNameA, MAX_PATH ) &&
125 			(!lpNewFileNameA || GetShortPathNameA( lpNewFileNameA, szNewFileNameA, MAX_PATH ))
126 			)
127 		{
128 			CHAR	szBuffer[32767];	// The buffer size must not exceed 32K
129 			DWORD	dwBufLen = GetPrivateProfileSectionA( RENAME_SECTION, szBuffer, elementsof(szBuffer), WININIT_FILENAME );
130 
131 			CHAR	szRename[MAX_PATH];	// This is enough for at most to times 67 chracters
132 			strcpy( szRename, szNewFileNameA );
133 			strcat( szRename, "=" );
134 			strcat( szRename, szExistingFileNameA );
135 			size_t	lnRename = strlen(szRename);
136 
137 			if ( dwBufLen + lnRename + 2 <= elementsof(szBuffer) )
138 			{
139 				CopyMemory( &szBuffer[dwBufLen], szRename, lnRename );
140 				szBuffer[dwBufLen + lnRename ] = 0;
141 				szBuffer[dwBufLen + lnRename + 1 ] = 0;
142 
143 				fSuccess = WritePrivateProfileSectionA( RENAME_SECTION, szBuffer, WININIT_FILENAME );
144 			}
145 			else
146 				SetLastError( ERROR_BUFFER_OVERFLOW );
147 		}
148 	}
149 	else
150 	{
151 
152 		fSuccess = MoveFileA( lpExistingFileNameA, lpNewFileNameA );
153 
154 		if ( !fSuccess && GetLastError() != ERROR_ACCESS_DENIED &&
155 			0 != (dwFlags & (MOVEFILE_COPY_ALLOWED | MOVEFILE_REPLACE_EXISTING)) )
156 		{
157 			BOOL	bFailIfExist = 0 == (dwFlags & MOVEFILE_REPLACE_EXISTING);
158 
159 			fSuccess = CopyFileA( lpExistingFileNameA, lpNewFileNameA, bFailIfExist );
160 
161 			if ( fSuccess )
162 				fSuccess = DeleteFileA( lpExistingFileNameA );
163 		}
164 
165 	}
166 
167 	return fSuccess;
168 }
169 
170 static BOOL MoveFileExImpl( LPCSTR lpExistingFileNameA, LPCSTR lpNewFileNameA, DWORD dwFlags )
171 {
172 	if ( 0 > ((LONG)GetVersion())) // High order bit indicates Win 9x
173 		return MoveFileEx9x( lpExistingFileNameA, lpNewFileNameA, dwFlags );
174 	else
175 		return MoveFileExA( lpExistingFileNameA, lpNewFileNameA, dwFlags );
176 }
177 
178 extern "C" UINT __stdcall IsOfficeRunning( MSIHANDLE handle )
179 {
180 	std::_tstring	sInstDir = GetMsiProperty( handle, TEXT("INSTALLLOCATION") );
181 	std::_tstring	sResourceDir = sInstDir + TEXT("Basis\\program\\resource\\");
182 	std::_tstring	sPattern = sResourceDir + TEXT("vcl*.res");
183 
184 	WIN32_FIND_DATA	aFindFileData;
185 	HANDLE	hFind = FindFirstFile( sPattern.c_str(), &aFindFileData );
186 
187 	if ( IsValidHandle(hFind) )
188 	{
189 		BOOL	fSuccess = false;
190 		bool	fRenameSucceeded;
191 
192 		do
193 		{
194 			std::_tstring	sResourceFile = sResourceDir + aFindFileData.cFileName;
195 			std::_tstring	sIntermediate = sResourceFile + TEXT(".tmp");
196 
197 			fRenameSucceeded = MoveFileExImpl( sResourceFile.c_str(), sIntermediate.c_str(), MOVEFILE_REPLACE_EXISTING );
198 			if ( fRenameSucceeded )
199 			{
200 				MoveFileExImpl( sIntermediate.c_str(), sResourceFile.c_str(), 0 );
201 				fSuccess = FindNextFile( hFind, &aFindFileData );
202 			}
203 		} while ( fSuccess && fRenameSucceeded );
204 
205 		if ( !fRenameSucceeded )
206         {
207 			MsiSetProperty(handle, TEXT("OFFICERUNS"), TEXT("1"));
208             SetMsiErrorCode( MSI_ERROR_OFFICE_IS_RUNNING );
209         }
210 
211 		FindClose( hFind );
212 	}
213 
214 	return ERROR_SUCCESS;
215 }
216 
217 
218 
219