1 /*************************************************************************
3 * DO NOT ALTER OR REMOVE COPYRIGHT NOTICES OR THIS FILE HEADER.
5 * Copyright 2008 by Sun Microsystems, Inc.
7 * OpenOffice.org - a multi-platform office productivity suite
9 * $RCSfile: checkdirectory.cxx,v $
12 * This file is part of OpenOffice.org.
14 * OpenOffice.org is free software: you can redistribute it and/or modify
15 * it under the terms of the GNU Lesser General Public License version 3
16 * only, as published by the Free Software Foundation.
18 * OpenOffice.org is distributed in the hope that it will be useful,
19 * but WITHOUT ANY WARRANTY; without even the implied warranty of
20 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 * GNU Lesser General Public License version 3 for more details
22 * (a copy is included in the LICENSE file that accompanied this code).
24 * You should have received a copy of the GNU Lesser General Public License
25 * version 3 along with OpenOffice.org. If not, see
26 * <http://www.openoffice.org/license.html>
27 * for a copy of the LGPLv3 License.
29 ************************************************************************/
31 #define _WIN32_WINNT 0x0401
34 #pragma warning(push, 1) /* disable warnings within system headers */
36 #define WIN32_LEAN_AND_MEAN
48 #define _tstring wstring
50 #define _tstring string
58 #include <systools/win32/uwinapi.h>
59 #include <../tools/seterror.hxx>
61 //----------------------------------------------------------
63 inline void OutputDebugStringFormat( LPCSTR pFormat
, ... )
68 va_start( args
, pFormat
);
69 StringCchVPrintfA( buffer
, sizeof(buffer
), pFormat
, args
);
70 OutputDebugStringA( buffer
);
73 static inline void OutputDebugStringFormat( LPCSTR
, ... )
79 static std::_tstring
GetMsiProperty( MSIHANDLE handle
, const std::_tstring
& sProperty
)
82 TCHAR szDummy
[1] = TEXT("");
85 if ( MsiGetProperty( handle
, sProperty
.c_str(), szDummy
, &nChars
) == ERROR_MORE_DATA
)
87 DWORD nBytes
= ++nChars
* sizeof(TCHAR
);
88 LPTSTR buffer
= reinterpret_cast<LPTSTR
>(_alloca(nBytes
));
89 ZeroMemory( buffer
, nBytes
);
90 MsiGetProperty(handle
, sProperty
.c_str(), buffer
, &nChars
);
97 static BOOL
RemoveCompleteDirectory( std::_tstring sPath
)
99 bool bDirectoryRemoved
= true;
102 std::_tstring sPattern
= sPath
+ TEXT("\\") + TEXT("*.*");
103 WIN32_FIND_DATA aFindData
;
105 // Finding all content in sPath
107 HANDLE hFindContent
= FindFirstFile( sPattern
.c_str(), &aFindData
);
109 if ( hFindContent
!= INVALID_HANDLE_VALUE
)
111 bool fNextFile
= false;
112 std::_tstring sCurrentDir
= TEXT(".");
113 std::_tstring sParentDir
= TEXT("..");
117 std::_tstring sFileName
= aFindData
.cFileName
;
119 mystr
= "Current short file: " + sFileName
;
120 // MessageBox(NULL, mystr.c_str(), "Current Content", MB_OK);
122 if (( strcmp(sFileName
.c_str(),sCurrentDir
.c_str()) != 0 ) &&
123 ( strcmp(sFileName
.c_str(),sParentDir
.c_str()) != 0 ))
125 std::_tstring sCompleteFileName
= sPath
+ TEXT("\\") + sFileName
;
127 if ( aFindData
.dwFileAttributes
== FILE_ATTRIBUTE_DIRECTORY
)
129 bool fSuccess
= RemoveCompleteDirectory(sCompleteFileName
);
132 mystr
= "Successfully removed content of dir " + sCompleteFileName
;
133 // MessageBox(NULL, mystr.c_str(), "Removed Directory", MB_OK);
137 mystr
= "An error occured during removing content of " + sCompleteFileName
;
138 // MessageBox(NULL, mystr.c_str(), "Error removing directory", MB_OK);
143 bool fSuccess
= DeleteFile( sCompleteFileName
.c_str() );
146 mystr
= "Successfully removed file " + sCompleteFileName
;
147 // MessageBox(NULL, mystr.c_str(), "Removed File", MB_OK);
151 mystr
= "An error occured during removal of file " + sCompleteFileName
;
152 // MessageBox(NULL, mystr.c_str(), "Error removing file", MB_OK);
157 fNextFile
= FindNextFile( hFindContent
, &aFindData
);
159 } while ( fNextFile
);
161 FindClose( hFindContent
);
163 // empty directory can be removed now
164 // RemoveDirectory is only successful, if the last handle to the directory is closed
165 // -> first removing content -> closing handle -> remove empty directory
167 bool fRemoveDirSuccess
= RemoveDirectory(sPath
.c_str());
169 if ( fRemoveDirSuccess
)
171 mystr
= "Successfully removed dir " + sPath
;
172 // MessageBox(NULL, mystr.c_str(), "Removed Directory", MB_OK);
176 mystr
= "An error occured during removal of empty directory " + sPath
;
177 // MessageBox(NULL, mystr.c_str(), "Error removing directory", MB_OK);
178 bDirectoryRemoved
= false;
182 return bDirectoryRemoved
;
187 extern "C" UINT __stdcall
RenamePrgFolder( MSIHANDLE handle
)
189 std::_tstring sOfficeInstallPath
= GetMsiProperty(handle
, TEXT("OFFICEINSTALLLOCATION"));
191 std::_tstring sRenameSrc
= sOfficeInstallPath
+ TEXT("program");
192 std::_tstring sRenameDst
= sOfficeInstallPath
+ TEXT("program_old");
194 // MessageBox(NULL, sRenameSrc.c_str(), "OFFICEINSTALLLOCATION", MB_OK);
196 bool bSuccess
= MoveFile( sRenameSrc
.c_str(), sRenameDst
.c_str() );
199 TCHAR sAppend
[2] = TEXT("0");
200 for ( int i
= 0; i
< 10; i
++ )
202 sRenameDst
= sOfficeInstallPath
+ TEXT("program_old") + sAppend
;
203 bSuccess
= MoveFile( sRenameSrc
.c_str(), sRenameDst
.c_str() );
212 MessageBox(NULL
, "Renaming folder failed", "RenamePrgFolder", MB_OK
);
214 MessageBox(NULL
, "Renaming folder successful", "RenamePrgFolder", MB_OK
);
217 return ERROR_SUCCESS
;
220 extern "C" UINT __stdcall
RemovePrgFolder( MSIHANDLE handle
)
222 std::_tstring sOfficeInstallPath
= GetMsiProperty(handle
, TEXT("OFFICEINSTALLLOCATION"));
223 std::_tstring sRemoveDir
= sOfficeInstallPath
+ TEXT("program_old");
225 // MessageBox(NULL, sRemoveDir.c_str(), "REMOVING OLD DIR", MB_OK);
227 bool bSuccess
= RemoveCompleteDirectory( sRemoveDir
);
229 TCHAR sAppend
[2] = TEXT("0");
230 for ( int i
= 0; i
< 10; i
++ )
232 sRemoveDir
= sOfficeInstallPath
+ TEXT("program_old") + sAppend
;
233 bSuccess
= RemoveCompleteDirectory( sRemoveDir
);
239 MessageBox(NULL
, "Removing folder successful", "RemovePrgFolder", MB_OK
);
241 MessageBox(NULL
, "Removing folder failed", "RemovePrgFolder", MB_OK
);
244 return ERROR_SUCCESS
;