xref: /AOO41X/main/javainstaller2/src/JavaSetup/org/openoffice/setup/InstallerHelper/SolarisHelper.java (revision cdf0e10c4e3984b49a9502b011690b615761d4a3)
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 package org.openoffice.setup.InstallerHelper;
29 
30 import org.openoffice.setup.InstallData;
31 import org.openoffice.setup.ResourceManager;
32 import org.openoffice.setup.SetupData.PackageDescription;
33 import org.openoffice.setup.Util.Controller;
34 import org.openoffice.setup.Util.Converter;
35 import org.openoffice.setup.Util.ExecuteProcess;
36 import org.openoffice.setup.Util.Informer;
37 import org.openoffice.setup.Util.LogManager;
38 import org.openoffice.setup.Util.SystemManager;
39 import java.io.File;
40 import java.util.HashMap;
41 import java.util.Vector;
42 
43 public class SolarisHelper {
44 
45     public SolarisHelper() {
46     }
47 
48     private void createLocalTempDir() {
49         String mktempString = "/usr/bin/mktemp";
50         File mktemp = new File(mktempString);
51         if ( mktemp.exists() ) {
52             // this is Solaris 10
53             LogManager.setCommandsHeaderLine("Preparing Solaris 10 user installation");
54             InstallData data = InstallData.getInstance();
55             String mktempCommand = mktempString + " -d";
56             String[] mktempCommandArray = new String[2];
57             mktempCommandArray[0] = mktempString;
58             mktempCommandArray[1] = "-d";
59             Vector returnVector = new Vector();
60             Vector returnErrorVector = new Vector();
61             int returnValue = ExecuteProcess.executeProcessReturnVector(mktempCommandArray, returnVector, returnErrorVector);
62             String uniqueLocalDir = (String) returnVector.get(0);
63 
64             data.setLocalTempPath(uniqueLocalDir);
65 
66             String log = mktempCommand + "<br><b>Returns: " + uniqueLocalDir + "</b><br>";
67             LogManager.addCommandsLogfileComment(log);
68 
69             String installRoot = data.getInstallDir();
70             File installRootTemp = new File(installRoot, "tmp");
71 
72             if ( installRootTemp.exists() ) {
73                 SystemManager.removeDirectory(installRootTemp);
74             }
75 
76             String linkCommand = "ln -s " + uniqueLocalDir + " " + installRootTemp.getPath();
77             String[] linkCommandArray = new String[4];
78             linkCommandArray[0] = "ln";
79             linkCommandArray[1] = "-s";
80             linkCommandArray[2] = uniqueLocalDir;
81             linkCommandArray[3] = installRootTemp.getPath();
82 
83             // Vector returnVector = new Vector();
84             returnValue = ExecuteProcess.executeProcessReturnValue(linkCommandArray);
85 
86             log = linkCommand + "<br><b>Returns: " + returnValue + "</b><br>";
87             LogManager.addCommandsLogfileComment(log);
88         }
89     }
90 
91     private void removeLocalTempDir() {
92         InstallData data = InstallData.getInstance();
93 
94         if ( data.getLocalTempPath() != null ) {
95             File installRootTemp = new File(data.getInstallDir(), "tmp");
96             if ( installRootTemp.exists() ) {
97                 installRootTemp.delete(); // removing the link
98                 SystemManager.createDirectory(installRootTemp);
99             }
100 
101             File localTemp = new File(data.getLocalTempPath());
102             if ( localTemp.exists() ) {
103                 SystemManager.removeDirectory(localTemp);
104             }
105         }
106     }
107 
108     private Vector getAdminFileContent(boolean relocatable, boolean rdepends) {
109 
110         Vector adminFile = new Vector();
111         InstallData data = InstallData.getInstance();
112 
113         // if ( relocatable ) {
114         //     String installDir =  data.getInstallDir();
115         //     // installDir = installDir.replace(" ", "\\ ");
116         //     String baseDirLine = "basedir=" + installDir;
117         //     adminFile.add(baseDirLine);
118         // }
119 
120         String mailLine = "mail=";
121         adminFile.add(mailLine);
122 
123         String conflictLine = "conflict=nochange";
124         if ( data.isUserInstallation() ) { conflictLine = "conflict=nochange"; }
125         adminFile.add(conflictLine);
126 
127         String runlevelLine = "runlevel=nocheck";
128         adminFile.add(runlevelLine);
129 
130         String setuidLine = "setuid=quit";
131         if ( data.isUserInstallation() ) { setuidLine = "setuid=nocheck"; }
132         adminFile.add(setuidLine);
133 
134         String actionLine = "action=nocheck";
135         adminFile.add(actionLine);
136 
137         String partialLine = "partial=quit";
138         if ( data.isUserInstallation() ) { partialLine = "partial=nocheck"; }
139         adminFile.add(partialLine);
140 
141         String instanceLine = "instance=unique";
142         adminFile.add(instanceLine);
143 
144         // String idependLine = "idepend=quit";
145         String idependLine = "idepend=nocheck";
146         // if ( data.isUserInstallation() ) { idependLine = "idepend=nocheck"; }
147         adminFile.add(idependLine);
148 
149         // String rdependLine = "rdepend=nocheck";
150         String rdependLine = "rdepend=quit";
151         if ( ! rdepends ) { rdependLine = "rdepend=nocheck"; }
152         if ( data.isUserInstallation() ) { rdependLine = "rdepend=nocheck"; }
153         adminFile.add(rdependLine);
154 
155         String spaceLine = "space=quit";
156         if ( data.isUserInstallation() ) { spaceLine = "space=nocheck"; }
157         adminFile.add(spaceLine);
158 
159         return adminFile;
160     }
161 
162     private String getMainVersion (String version) {
163         String mainVersion = null;
164 
165         int pos = version.indexOf(",");
166         if ( pos > -1 ) {
167             mainVersion = version.substring(0, pos);
168         }
169 
170         return mainVersion;
171     }
172 
173     private String getPackageRevision(String version) {
174         String revision = null;
175 
176         int pos = version.indexOf("=");
177         if ( pos > -1 ) {
178             revision = version.substring(pos + 1, version.length() );
179         }
180 
181         return revision;
182     }
183 
184     private Vector getVectorOfNumbers(String version) {
185         Vector numbers = new Vector();
186         int pos = -1;
187         // getting number from a string in format 2.0.0
188         do {
189             pos = version.indexOf(".");
190             if ( pos > -1 ) {
191                 String number = version.substring(0, pos);
192                 version = version.substring(pos + 1, version.length());
193                 numbers.add(number);
194             }
195         } while ( pos > -1 );
196 
197         numbers.add(version);
198         return numbers;
199     }
200 
201     private int getMinimum(int a, int b) {
202         int minimum;
203 
204         if ( a < b ) {
205              minimum = a;
206         } else {
207             minimum = b;
208         }
209 
210         return minimum;
211     }
212 
213     private String compareVersion(String firstVersion, String secondVersion) {
214         // comparing strings with syntax 2.0.0
215         String comparison = "bothPackagesAreEqual";
216         Vector firstVector = getVectorOfNumbers(firstVersion);
217         Vector secondVector = getVectorOfNumbers(secondVersion);
218 
219         int firstLength = firstVector.size();
220         int secondLength = secondVector.size();
221         int minimum = getMinimum(firstLength, secondLength);
222 
223         for (int i = 0; i < minimum; i++) {
224             String firstS = (String)firstVector.get(i);
225             String secondS = (String)secondVector.get(i);
226 
227             int first = Integer.parseInt(firstS);
228             int second_ = Integer.parseInt(secondS);
229 
230             if ( second_ > first ) {
231                 comparison = "firstPackageIsOlder";
232                 break;
233             } else if ( second_ < first ) {
234                 comparison = "secondPackageIsOlder";
235                 break;
236             }
237         }
238 
239         return comparison;
240     }
241 
242     public void saveModulesLogFile(InstallData data) {
243         if ( data.logModuleStates() ) {
244             Vector logContent = LogManager.getModulesLogFile();
245             File baseDir = new File(data.getInstallDefaultDir(), data.getProductDir());
246             File uninstallDir = new File(baseDir, data.getUninstallDirName());
247             File modulesLogFile = new File(uninstallDir, "moduleSettingsLog.txt");
248             // System.err.println("Saving file: " + modulesLogFile.getPath());
249             SystemManager.saveCharFileVector(modulesLogFile.getPath(), logContent);
250         }
251     }
252 
253     public void removeSolarisLockFile() {
254         String lockFileName = "/tmp/.ai.pkg.zone.lock-afdb66cf-1dd1-11b2-a049-000d560ddc3e";
255         File lockFile = new File(lockFileName);
256 
257         if ( lockFile.exists() ) {
258             // System.err.println("Found file: " + lockFileName);
259             boolean deleted = lockFile.delete();
260         }
261     }
262 
263     public String getSolarisDatabasePath(InstallData data) {
264         String databasePath = null;
265         databasePath = data.getInstallDir();
266         return databasePath;
267     }
268 
269     public void createAdminFile(boolean relocatable, boolean rdepends) {
270         InstallData data = InstallData.getInstance();
271         Vector removeFiles = data.getRemoveFiles();
272         String adminFileName = "";
273 
274         if ( relocatable ) {
275         	if ( rdepends ) {
276                 adminFileName = "adminFileReloc";
277             } else {
278                 adminFileName = "adminFileRelocNoDepends";
279             }
280         } else {
281         	if ( rdepends ) {
282                 adminFileName = "adminFileNoReloc";
283             } else {
284                 adminFileName = "adminFileNoRelocNoDepends";
285             }
286         }
287 
288         Vector fileContent = getAdminFileContent(relocatable, rdepends);
289         File adminFile = new File(data.getInstallDir(), adminFileName);
290         String completeAdminFileName = adminFile.getPath();
291 
292         if ( relocatable ) {
293         	if ( rdepends ) {
294                 data.setAdminFileNameReloc(completeAdminFileName);
295             } else {
296                 data.setAdminFileNameRelocNoDepends(completeAdminFileName);
297             }
298         } else {
299         	if ( rdepends ) {
300                 data.setAdminFileNameNoReloc(completeAdminFileName);
301             } else {
302                 data.setAdminFileNameNoRelocNoDepends(completeAdminFileName);
303             }
304         }
305 
306         if ( ! adminFile.exists() ) {
307             // only saving, if it did not exist
308             SystemManager.saveCharFileVector(completeAdminFileName, fileContent);
309             // only removing file in aborted installation, if it was created before
310             removeFiles.add(completeAdminFileName);
311         }
312 
313         // File dumpFile = new File(data.getInstallDir(), "dumpFile");
314         // String dumpFileName = dumpFile.getPath();
315         // SystemManager.dumpFile(adminFileName, dumpFileName);
316     }
317 
318     // for user installations an environment hashmap is used
319     public void setEnvironmentForUserInstall() {
320         InstallData data = InstallData.getInstance();
321         HashMap env = SystemManager.getEnvironmentHashMap();
322         env.put("LD_PRELOAD_32", data.getGetUidPath());
323         data.setShellEnvironment(env);
324     }
325 
326     public String getVersionString(Vector returnVector) {
327         String versionString = null;
328         String versionLine = null;
329 
330         for (int i = 0; i < returnVector.size(); i++) {
331             String line = (String) returnVector.get(i);
332             int pos = line.indexOf("REV=");
333             if ( pos > -1 ) {
334                 versionLine = line;
335                 break;
336             }
337         }
338 
339         if ( versionLine != null ) {
340             versionLine = versionLine.trim();
341             int pos = versionLine.lastIndexOf(" ");
342             versionString = versionLine.substring(pos + 1, versionLine.length());
343         }
344 
345         return versionString;
346     }
347 
348     public int getInstalledMinor(String version) {
349 
350         int minor = 0;
351 
352         int pos = version.indexOf(".");
353         if ( pos > -1 ) {
354             String reduced = version.substring(pos + 1, version.length());
355 
356             pos = reduced.indexOf(".");
357             if ( pos > -1 ) {
358                 reduced = reduced.substring(0, pos);
359                 minor = Integer.parseInt(reduced);
360             }
361         }
362 
363     	return minor;
364     }
365 
366     public boolean comparePackageVersions(String firstPackageVersion, String secondPackageVersion) {
367        // Analyzing strings: version, 2.0.0,REV=106.2005.05.26
368 
369         boolean firstPackageIsOlder = false;
370         String comparison = null;
371 
372         String firstPackageMainVersion = getMainVersion(firstPackageVersion); // 2.0.0
373         String secondPackageMainVersion = getMainVersion(secondPackageVersion); // 2.0.0
374 
375         if (( firstPackageMainVersion != null ) && ( secondPackageMainVersion != null )) {
376             comparison = compareVersion(firstPackageMainVersion, secondPackageMainVersion);
377         }
378 
379         if ( comparison.equals("firstPackageIsOlder") ) {
380             firstPackageIsOlder = true;
381         } else if ( comparison.equals("secondPackageIsOlder") ) {
382             firstPackageIsOlder = false;
383         } else if ( comparison.equals("bothPackagesAreEqual") ) {
384             String firstPackageRevision = getPackageRevision(firstPackageVersion); // 106.2005.05.26
385             String secondPackageRevision = getPackageRevision(secondPackageVersion); // 106.2005.05.26
386 
387             if (( firstPackageRevision != null ) && ( secondPackageRevision != null )) {
388                 comparison = compareVersion(firstPackageRevision, secondPackageRevision);
389                 if ( comparison.equals("firstPackageIsOlder") ) {
390                     firstPackageIsOlder = true;
391                 } else {
392                     firstPackageIsOlder = false;
393                 }
394             }
395         }
396 
397         // If version is equal, the patchlevel has to be analyzed
398         // -> pkginfo does not offer this information
399         // -> looking into file <root>/opt/var/sadm/pkg/<package>/pkginfo?
400 
401         return firstPackageIsOlder;
402     }
403 
404 }
405