xref: /aoo4110/main/tools/bootstrp/inimgr.cxx (revision b1cdbd2c)
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 // MARKER(update_precomp.py): autogen include statement, do not remove
25 #include "precompiled_tools.hxx"
26 #if !defined( UNX )
27 #include <direct.h>
28 #else
29 #include <sys/stat.h>
30 #endif
31 #include <stdlib.h>
32 #include <stdio.h>
33 
34 
35 #include "bootstrp/inimgr.hxx"
36 #include "bootstrp/appdef.hxx"
37 
38 /****************************************************************************/
IniManager(ByteString & rDir,ByteString & rLocalDir)39 IniManager::IniManager( ByteString &rDir, ByteString &rLocalDir )
40 /****************************************************************************/
41 			: bUpdate( sal_True )
42 {
43 	sLocalPath = ByteString( getenv( "LOCALINI" ));
44 	if ( !sLocalPath.Len())
45 		sLocalPath = rLocalDir;
46 
47 	sGlobalDir = rDir;
48 #if !defined( UNX ) && !defined( OS2 )
49 	mkdir(( char * ) sLocalPath.GetBuffer());
50 #else
51 	mkdir( sLocalPath.GetBuffer() ,00777 );
52 #endif
53 }
54 
55 /****************************************************************************/
IniManager(ByteString & rDir)56 IniManager::IniManager( ByteString &rDir )
57 /****************************************************************************/
58 			: bUpdate( sal_True )
59 {
60 	sLocalPath = GetLocalIni();
61 	sGlobalDir = rDir;
62 #if !defined( UNX ) && !defined( OS2 )
63 	mkdir(( char * ) sLocalPath.GetBuffer());
64 #else
65 	mkdir( sLocalPath.GetBuffer() ,00777 );
66 #endif
67 }
68 
69 /****************************************************************************/
IniManager()70 IniManager::IniManager()
71 /****************************************************************************/
72 			: bUpdate( sal_True )
73 {
74 	sLocalPath = GetLocalIni();
75 
76 #if !defined( UNX ) && !defined( OS2 )
77 	mkdir(( char * ) sLocalPath.GetBuffer());
78 #else
79 	mkdir( sLocalPath.GetBuffer(), 00777 );
80 #endif
81 
82 	sGlobalDir = GetGlobalIni();
83 }
84 
85 /****************************************************************************/
ToLocal(ByteString & rPath)86 ByteString IniManager::ToLocal( ByteString &rPath )
87 /****************************************************************************/
88 {
89 	ByteString sTmp( rPath );
90 #if !defined( UNX )
91 	ByteString sUnc( _INI_UNC );
92 	sUnc.ToUpperAscii();
93 	ByteString sOldUnc( _INI_UNC_OLD );
94 	sOldUnc.ToUpperAscii();
95 	sTmp.ToUpperAscii();
96 
97 	sTmp.SearchAndReplace( sUnc, _INI_DRV );
98 	sTmp.SearchAndReplace( sOldUnc, _INI_DRV );
99 	sTmp.ToUpperAscii();
100 
101 	ByteString sIni( sGlobalDir );
102 	sIni.ToUpperAscii();
103 
104 	sTmp.SearchAndReplace( sIni, sLocalPath );
105 
106 	while ( sTmp.SearchAndReplace( "\\\\", "\\" ) != STRING_NOTFOUND ) ;
107 #else
108 	sTmp.SearchAndReplace( sGlobalDir, sLocalPath );
109 
110 	ByteString sOldGlobalDir( GetIniRootOld() );
111 	sTmp.SearchAndReplace( sOldGlobalDir, sLocalPath );
112 
113 	while ( sTmp.SearchAndReplace( "//", "/" ) != STRING_NOTFOUND ) ;
114 #endif
115 
116 	return sTmp;
117 }
118 
119 /****************************************************************************/
GetLocalIni()120 ByteString IniManager::GetLocalIni()
121 /****************************************************************************/
122 {
123 	ByteString sLocalPath = ByteString( getenv( "LOCALINI" ));
124 
125 	if ( !sLocalPath.Len()) {
126 #ifdef UNX
127 		ByteString sLocal( getenv( "HOME" ));
128 		sLocal += ByteString( "/localini" );
129 #else
130 		ByteString sLocal( getenv( "TMP" ));
131 		sLocal += ByteString( "\\localini" );
132 #endif
133 
134 		sLocalPath = sLocal;
135 	}
136 
137 	return sLocalPath;
138 }
139 
140 /****************************************************************************/
GetGlobalIni()141 ByteString IniManager::GetGlobalIni()
142 /****************************************************************************/
143 {
144 	ByteString sGlobalPath = ByteString( GetEnv( "GLOBALINI" ));
145 
146 	if ( !sGlobalPath.Len())
147 		sGlobalPath = ByteString( _INIROOT );
148 
149 	return sGlobalPath;
150 }
151 
152 /****************************************************************************/
ForceUpdate()153 void IniManager::ForceUpdate()
154 /****************************************************************************/
155 {
156 	UniString sUniGlobalDir( sGlobalDir, gsl_getSystemTextEncoding());
157 	DirEntry aPath( UniString( sGlobalDir, gsl_getSystemTextEncoding()));
158 	Dir aDir( aPath, FSYS_KIND_DIR | FSYS_KIND_FILE);
159 
160 #ifndef UNX
161 	sLocalPath.EraseTrailingChars( '\\' );
162 	sLocalPath += "\\";
163 #else
164 	sLocalPath.EraseTrailingChars( '/' );
165 	sLocalPath += "/";
166 #endif
167 
168 	for ( sal_uInt16 i=0; i < aDir.Count(); i++ ) {
169 		ByteString sEntry( aDir[i].GetName(), gsl_getSystemTextEncoding());
170 		if (( sEntry != "." ) &&
171 			( sEntry != ".." ))
172 		{
173 			if ( !FileStat( aDir[i] ).IsKind( FSYS_KIND_DIR )) {
174 				ByteString sSrc( aDir[i].GetFull(), gsl_getSystemTextEncoding());
175 				ByteString sDestination( sLocalPath );
176 				sDestination += sEntry;
177 
178 				UniString sUniDestination( sDestination, gsl_getSystemTextEncoding());
179 				DirEntry aDestEntry( sUniDestination );
180 				FileStat aDestStat( aDestEntry );
181 				FileStat aSrcStat( aDir[i] );
182 
183 				if (( !aDestEntry.Exists() ) ||
184 					( aSrcStat.IsYounger( aDestStat )))
185 				{
186 					FileCopier aFileCopier( aDir[ i ], aDestEntry );
187 					aFileCopier.Execute();
188 
189 					while ( !aDestEntry.Exists())
190 						aFileCopier.Execute();
191 				}
192 			}
193 		}
194 	}
195 }
196 
197 /****************************************************************************/
Update()198 void IniManager::Update()
199 /****************************************************************************/
200 {
201 	if ( bUpdate )
202 	{
203 		ForceUpdate();
204 		bUpdate = sal_False;
205 	}
206 }
207