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 package org.openoffice.setup.Installer;
25 
26 import org.openoffice.setup.InstallData;
27 import org.openoffice.setup.InstallerHelper.SolarisHelper;
28 import org.openoffice.setup.ResourceManager;
29 import org.openoffice.setup.SetupData.PackageDescription;
30 import org.openoffice.setup.Util.Converter;
31 import org.openoffice.setup.Util.ExecuteProcess;
32 import org.openoffice.setup.Util.Informer;
33 import org.openoffice.setup.Util.LogManager;
34 import org.openoffice.setup.Util.SystemManager;
35 import java.io.File;
36 import java.util.HashMap;
37 import java.util.Vector;
38 
39 public class SolarisInstaller extends Installer {
40 
41     SolarisHelper helper = new SolarisHelper();
42 
SolarisInstaller()43     public SolarisInstaller() {
44         super();
45     }
46 
defineDatabasePath()47     public void defineDatabasePath() {
48 
49         InstallData data = InstallData.getInstance();
50         String oldDatabasePath = data.getDatabasePath();
51         data.setDatabasePath(null);
52 
53         // Determining the database path (only for user installation).
54         // Important if a user installation is done into an existing
55         // user installation -> this methode can only be called after
56         // determination of installation directory.
57 
58         if ( data.isUserInstallation() ) {
59             String databasePath = helper.getSolarisDatabasePath(data); // the file does not need to exist!
60             data.setDatabasePath(databasePath);
61 
62             // If this is a new path to the database, then this database was
63             // not analyzed before (when going back in installation wizard)
64             if ( ! databasePath.equals(oldDatabasePath) ) {
65                 data.setDatabaseAnalyzed(false);
66                 data.setDatabaseQueried(false);
67             }
68         }
69     }
70 
preInstallationOngoing()71     public void preInstallationOngoing() {
72         // an admin file has to be created for user and for root installation
73         InstallData data = InstallData.getInstance();
74 
75         if ( data.isInstallationMode()) {
76             boolean makeRelocatableAdminFile = true;
77             boolean removeDepends = true;
78             helper.createAdminFile(makeRelocatableAdminFile, removeDepends);
79             helper.createAdminFile(makeRelocatableAdminFile, ! removeDepends);
80             helper.createAdminFile(! makeRelocatableAdminFile, removeDepends);
81             helper.createAdminFile(! makeRelocatableAdminFile, ! removeDepends);
82         }
83 
84         if ( data.isUserInstallation() ) {
85             // LD_PRELOAD_32 needs to be set for user installation
86             helper.setEnvironmentForUserInstall();
87             // Solaris 10 needs to have local temp directory for pkgadd
88             // createLocalTempDir();
89         }
90     }
91 
postInstallationOngoing()92     public void postInstallationOngoing() {
93         InstallData data = InstallData.getInstance();
94         // if ( data.isUserInstallation() ) {
95             // Solaris 10 needs to have local temp directory for pkgadd
96             // removeLocalTempDir();
97         // }
98 
99         helper.removeSolarisLockFile();
100     }
101 
postInstall(PackageDescription packageData)102     public void postInstall(PackageDescription packageData) {
103         InstallData data = InstallData.getInstance();
104 
105         if ( ! data.isAbortedInstallation() ) {
106             data.setStillRunning(true);
107             // Collecting information about installed packages
108             // Creating a list containing pairs of package names and rpm file names
109             // that has to be used during uninstallation.
110             helper.saveModulesLogFile(data);
111             data.setStillRunning(false);
112         }
113     }
114 
postUninstallationOngoing()115     public void postUninstallationOngoing() {
116         helper.removeSolarisLockFile();
117     }
118 
getChangeInstallDir(PackageDescription packageData)119     public String getChangeInstallDir(PackageDescription packageData) {
120         String installDir = null;
121         String packageName = packageData.getPackageName();
122 
123         if ( packageName != null ) {
124             String pkgCommand = "pkginfo -r" + " " + packageName;
125             String[] pkgCommandArray = new String[3];
126             pkgCommandArray[0] = "pkginfo";
127             pkgCommandArray[1] = "-r";
128             pkgCommandArray[2] = packageName;
129             Vector returnVector = new Vector();
130             Vector returnErrorVector = new Vector();
131             int returnValue = ExecuteProcess.executeProcessReturnVector(pkgCommandArray, returnVector, returnErrorVector);
132             String returnString = (String) returnVector.get(0);
133 
134             String log = pkgCommand + "<br><b>Returns: " + returnString + "</b><br>";
135             LogManager.addCommandsLogfileComment(log);
136 
137             installDir = returnString;
138         }
139 
140         return installDir;
141     }
142 
installPackage(PackageDescription packageData)143     public void installPackage(PackageDescription packageData) {
144         InstallData data = InstallData.getInstance();
145 
146         String log = "<br><b>Package: " + packageData.getName() + "</b>";
147         LogManager.addCommandsLogfileComment(log);
148 
149         String rootDir = data.getInstallDir();
150         String packagePath = data.getPackagePath();
151 
152         if (( packageData.getPkgSubdir() != null ) && ( ! packageData.getPkgSubdir().equals("") )) {
153             File completePackageFile = new File(packagePath, packageData.getPkgSubdir());
154             packagePath = completePackageFile.getPath();
155         }
156 
157         String packageName = packageData.getPackageName();
158 
159         if (( packageName.equals("")) || ( packageName == null )) {
160             log = "<b>No package name specified. Nothing to do</b>";
161             LogManager.addCommandsLogfileComment(log);
162         } else {
163             log = "<b>Package Name: " + packageName + "</b>";
164             LogManager.addCommandsLogfileComment(log);
165 
166             File completePackage = new File(packagePath, packageName);
167 
168             if ( completePackage.exists() ) {
169 
170                 String pkgCommand = "";
171                 String[] pkgCommandArray;
172                 String adminFileName = "";
173                 Vector returnVector = new Vector();
174                 Vector returnErrorVector = new Vector();
175                 int returnValue;
176 
177                 // is package relocatable or not?
178                 if ( packageData.isRelocatable() ) {
179                     adminFileName = data.getAdminFileNameReloc();
180                 } else {
181                     adminFileName = data.getAdminFileNameNoReloc();
182                 }
183 
184                 if ( data.isUserInstallation() ) {
185                     HashMap env = data.getShellEnvironment();
186                     String[] envStringArray = Converter.convertHashmapToStringArray(env);
187                     pkgCommand = "/usr/sbin/pkgadd -n -d " + packagePath + " -R " +
188                             rootDir + " -a " + adminFileName + " " + packageName;
189                     pkgCommandArray = new String[9];
190                     pkgCommandArray[0] = "/usr/sbin/pkgadd";
191                     pkgCommandArray[1] = "-n";
192                     pkgCommandArray[2] = "-d";
193                     pkgCommandArray[3] = packagePath;
194                     pkgCommandArray[4] = "-R";
195                     pkgCommandArray[5] = rootDir;
196                     pkgCommandArray[6] = "-a";
197                     pkgCommandArray[7] = adminFileName;
198                     pkgCommandArray[8] = packageName;
199                     returnValue = ExecuteProcess.executeProcessReturnVectorEnv(pkgCommandArray, envStringArray, returnVector, returnErrorVector);
200                 } else {
201                     // shifting of the installation root (-R) is only wanted for user installation
202                     pkgCommand = "/usr/sbin/pkgadd -n -d " + packagePath +
203                             " -a " + adminFileName + " " + packageName;
204                     pkgCommandArray = new String[7];
205                     pkgCommandArray[0] = "/usr/sbin/pkgadd";
206                     pkgCommandArray[1] = "-n";
207                     pkgCommandArray[2] = "-d";
208                     pkgCommandArray[3] = packagePath;
209                     pkgCommandArray[4] = "-a";
210                     pkgCommandArray[5] = adminFileName;
211                     pkgCommandArray[6] = packageName;
212                     returnValue = ExecuteProcess.executeProcessReturnVector(pkgCommandArray, returnVector, returnErrorVector);
213                 }
214 
215                 if ( returnValue == 0 ) {
216                     log = pkgCommand + "<br><b>Returns: " + returnValue + " Successful installation</b><br>";
217                     LogManager.addCommandsLogfileComment(log);
218                 } else {    // an error occured during installation
219                     if ( packageData.installCanFail() ) {
220                         log = pkgCommand + "<br><b>Returns: " + returnValue + " Problem during installation. Can be ignored.</b><br>";
221                         LogManager.addCommandsLogfileComment(log);
222                     } else {
223                         log = pkgCommand + "<br><b>Returns: " + returnValue + " Error during installation</b><br>";
224                         LogManager.addCommandsLogfileComment(log);
225                         System.err.println("Error during installation:");
226                         for (int i = 0; i < returnErrorVector.size(); i++) {
227                             LogManager.addCommandsLogfileComment((String)returnErrorVector.get(i));
228                             System.err.println(returnErrorVector.get(i));
229                         }
230                         data.setIsErrorInstallation(true);
231                     }
232                 }
233             }
234             else {
235                 log = "<b>Error: Did not find package " + packageName + "</b><br>";
236                 System.err.println(log);
237                 String message = ResourceManager.getString("String_File_Not_Found") + ": " + packageName;
238                 String title = ResourceManager.getString("String_Error");
239                 Informer.showErrorMessage(message, title);
240                 LogManager.addCommandsLogfileComment(log);
241                 data.setIsErrorInstallation(true);
242             }
243         }
244     }
245 
uninstallPackage(PackageDescription packageData)246     public void uninstallPackage(PackageDescription packageData) {
247         InstallData data = InstallData.getInstance();
248 
249         String log = "<br><b>Package: " + packageData.getName() + "</b>";
250         LogManager.addCommandsLogfileComment(log);
251 
252         String installRoot = data.getInstallDir();
253         String packageName = packageData.getPackageName();
254         String adminFileName = "";
255 
256         // is package relocatable or not?
257         if ( packageData.isRelocatable() ) {
258         	if ( packageData.ignoreDependsForUninstall() ) {    // Force removal of older packages during installation
259                 adminFileName = data.getAdminFileNameRelocNoDepends();
260             } else {
261                 adminFileName = data.getAdminFileNameReloc();
262             }
263         } else {
264         	if ( packageData.ignoreDependsForUninstall() ) {    // Force removal of older packages during installation
265                 adminFileName = data.getAdminFileNameNoRelocNoDepends();
266             } else {
267                 adminFileName = data.getAdminFileNameNoReloc();
268             }
269         }
270 
271         String pkgCommand = "";
272         String[] pkgCommandArray;
273         int returnValue;
274         Vector returnVector = new Vector();
275         Vector returnErrorVector = new Vector();
276 
277         if ( data.isUserInstallation() ) {
278             pkgCommand = "/usr/sbin/pkgrm -n " + "-R " + installRoot + " -a " + adminFileName + " " + packageName;
279             pkgCommandArray = new String[7];
280             pkgCommandArray[0] = "/usr/sbin/pkgrm";
281             pkgCommandArray[1] = "-n";
282             pkgCommandArray[2] = "-R";
283             pkgCommandArray[3] = installRoot;
284             pkgCommandArray[4] = "-a";
285             pkgCommandArray[5] = adminFileName;
286             pkgCommandArray[6] = packageName;
287             HashMap env = data.getShellEnvironment();
288             String[] envStringArray = Converter.convertHashmapToStringArray(env);
289             // SystemManager.dumpStringArray(envStringArray);
290             returnValue = ExecuteProcess.executeProcessReturnVectorEnv(pkgCommandArray, envStringArray, returnVector, returnErrorVector);
291         } else {
292             // shifting of the installation root (-R) is only wanted for user installation
293             pkgCommand = "/usr/sbin/pkgrm -n " + "-a " + adminFileName + " " + packageName;
294             pkgCommandArray = new String[5];
295             pkgCommandArray[0] = "/usr/sbin/pkgrm";
296             pkgCommandArray[1] = "-n";
297             pkgCommandArray[2] = "-a";
298             pkgCommandArray[3] = adminFileName;
299             pkgCommandArray[4] = packageName;
300             returnValue = ExecuteProcess.executeProcessReturnVector(pkgCommandArray, returnVector, returnErrorVector);
301         }
302 
303         if ( returnValue == 0 ) {
304             log = pkgCommand + "<br><b>Returns: " + returnValue + " Successful uninstallation</b><br>";
305             LogManager.addCommandsLogfileComment(log);
306         } else {    // an error occured during installation
307             if ( packageData.uninstallCanFail() ) {
308                 log = pkgCommand + "<br><b>Returns: " + returnValue + " Problem during uninstallation. Can be ignored.</b><br>";
309                 LogManager.addCommandsLogfileComment(log);
310             } else {
311                 log = pkgCommand + "<br><b>Returns: " + returnValue + " Error during uninstallation</b><br>";
312                 LogManager.addCommandsLogfileComment(log);
313                 System.err.println("Error during uninstallation:");
314                 for (int i = 0; i < returnErrorVector.size(); i++) {
315                     LogManager.addCommandsLogfileComment((String)returnErrorVector.get(i));
316                     System.err.println(returnErrorVector.get(i));
317                 }
318                 data.setIsErrorInstallation(true);
319             }
320         }
321     }
322 
isPackageNameInstalledClassic(String packageName, InstallData installData)323     public boolean isPackageNameInstalledClassic(String packageName, InstallData installData) {
324         String rootString = "";
325         String rootPath = null;
326         String pkgCommand;
327         String[] pkgCommandArray;
328         boolean useLocalRoot = false;
329         boolean isInstalled = false;
330 
331         if (installData.isUserInstallation()) {
332             rootPath = installData.getDatabasePath();
333         }
334 
335         if (( rootPath != null ) && (! rootPath.equals("null"))) {
336             rootString = "-R";
337             useLocalRoot = true;
338         }
339 
340         if (useLocalRoot) {
341             pkgCommand = "pkginfo " + rootString + " " + rootPath + " " + packageName;
342             pkgCommandArray = new String[4];
343             pkgCommandArray[0] = "pkginfo";
344             pkgCommandArray[1] = rootString;
345             pkgCommandArray[2] = rootPath;
346             pkgCommandArray[3] = packageName;
347         } else {
348             pkgCommand = "pkginfo " + packageName;
349             pkgCommandArray = new String[2];
350             pkgCommandArray[0] = "pkginfo";
351             pkgCommandArray[1] = packageName;
352         }
353 
354         // Vector returnVector = new Vector();
355         int returnValue = ExecuteProcess.executeProcessReturnValue(pkgCommandArray);
356 
357         if ( returnValue == 0 ) {
358             isInstalled = true;
359             String log = pkgCommand + "<br><b>Returns: " + returnValue + " Package is installed" + "</b><br>";
360             LogManager.addCommandsLogfileComment(log);
361         } else {
362             String log = pkgCommand + "<br><b>Returns: " + returnValue + " Package is not installed" + "</b><br>";
363             LogManager.addCommandsLogfileComment(log);
364         }
365 
366         return isInstalled;
367     }
368 
queryAllDatabase(InstallData installData)369     private void queryAllDatabase(InstallData installData) {
370 
371         String rootString = "";
372         String rootPath = null;
373         String pkgCommand;
374         String[] pkgCommandArray;
375         boolean useLocalRoot = false;
376         HashMap map = new HashMap();;
377 
378         if (installData.isUserInstallation()) {
379             rootPath = installData.getDatabasePath();
380         }
381 
382         if (( rootPath != null ) && (! rootPath.equals("null"))) {
383             rootString = "-R";
384             useLocalRoot = true;
385         }
386 
387         if (useLocalRoot) {
388             pkgCommand = "pkginfo " + rootString + " " + rootPath;
389             pkgCommandArray = new String[4];
390             pkgCommandArray[0] = "pkginfo";
391             pkgCommandArray[1] = "-x";
392             pkgCommandArray[2] = rootString;
393             pkgCommandArray[3] = rootPath;
394         } else {
395             pkgCommand = "pkginfo -x";
396             pkgCommandArray = new String[2];
397             pkgCommandArray[0] = "pkginfo";
398             pkgCommandArray[1] = "-x";
399         }
400 
401         Vector returnVector = new Vector();
402         Vector returnErrorVector = new Vector();
403         int returnValue = ExecuteProcess.executeProcessReturnVector(pkgCommandArray, returnVector, returnErrorVector);
404 
405         String log = pkgCommand + "<br><b>Returns: " + returnValue + "</b><br>";
406         LogManager.addCommandsLogfileComment(log);
407         String value = "1";
408 
409         if ( ! returnVector.isEmpty()) {
410             for (int i = 0; i < returnVector.size(); i++) {
411                 String onePackage = (String)returnVector.get(i);
412                 int pos1 = onePackage.indexOf(" ");
413                 String key = onePackage.substring(0, pos1);
414                 map.put(key, value);
415             }
416         }
417 
418         installData.setDatabaseQueried(true);
419         installData.setDatabaseMap(map);
420 
421     }
422 
isPackageNameInstalled(String packageName, InstallData installData)423     public boolean isPackageNameInstalled(String packageName, InstallData installData) {
424 
425         boolean isInstalled = false;
426 
427         HashMap map = null;
428         if ( ! installData.databaseQueried() ) {
429             queryAllDatabase(installData);
430         }
431 
432         map = installData.getDatabaseMap();
433 
434         if ( map.containsKey(packageName)) {
435             isInstalled = true;
436         }
437 
438         return isInstalled;
439     }
440 
isPackageInstalled(PackageDescription packageData, InstallData installData)441     public boolean isPackageInstalled(PackageDescription packageData, InstallData installData) {
442         boolean isInstalled = false;
443 
444         String packageName = packageData.getPackageName();
445 
446         if ( packageName.equals("")) {
447             packageName = null;
448         }
449 
450         if ( packageName != null ) {
451         	isInstalled = isPackageNameInstalled(packageName, installData);
452         }
453 
454         return isInstalled;
455     }
456 
isInstallSetPackageOlder(PackageDescription packageData, InstallData installData)457     public boolean isInstallSetPackageOlder(PackageDescription packageData, InstallData installData) {
458         boolean installSetPackageIsOlder = false;
459         boolean checkIfInstalledIsOlder = false;
460         installSetPackageIsOlder = findOlderPackage(packageData, installData, checkIfInstalledIsOlder);
461         return installSetPackageIsOlder;
462     }
463 
isInstalledPackageOlder(PackageDescription packageData, InstallData installData)464     public boolean isInstalledPackageOlder(PackageDescription packageData, InstallData installData) {
465         boolean installedPackageIsOlder = false;
466         boolean checkIfInstalledIsOlder = true;
467         installedPackageIsOlder = findOlderPackage(packageData, installData, checkIfInstalledIsOlder);
468         return installedPackageIsOlder;
469     }
470 
findOlderPackage(PackageDescription packageData, InstallData installData, boolean checkIfInstalledIsOlder)471     private boolean findOlderPackage(PackageDescription packageData, InstallData installData, boolean checkIfInstalledIsOlder) {
472 
473         boolean firstPackageIsOlder = false;
474         String packageName = packageData.getPackageName();
475         String log;
476 
477         if ( packageName.equals("")) {
478             packageName = null;
479         }
480 
481         if ( packageName != null ) {
482             String rootString = "";
483             String rootPath = null;
484             String pkgCommand;
485             String[] pkgCommandArray;
486             boolean useLocalRoot = false;
487 
488             if (installData.isUserInstallation()) {
489                 rootPath = installData.getDatabasePath();
490             }
491 
492             if (( rootPath != null ) && (! rootPath.equals("null"))) {
493                 rootString = "-R";
494                 useLocalRoot = true;
495             }
496 
497             if (useLocalRoot) {
498                 // String pkgCommand = "pkginfo -l" + rootString + " " + rootPath + " " + packageName + " | grep VERSION:";
499                 pkgCommand = "pkginfo -x " + rootString + " " + rootPath + " " + packageName;
500                 pkgCommandArray = new String[5];
501                 pkgCommandArray[0] = "pkginfo";
502                 pkgCommandArray[1] = "-x";
503                 pkgCommandArray[2] = rootString;
504                 pkgCommandArray[3] = rootPath;
505                 pkgCommandArray[4] = packageName;
506 
507             } else {
508                 // String pkgCommand = "pkginfo -l" + rootString + " " + rootPath + " " + packageName + " | grep VERSION:";
509                 pkgCommand = "pkginfo -x " + packageName;
510                 pkgCommandArray = new String[3];
511                 pkgCommandArray[0] = "pkginfo";
512                 pkgCommandArray[1] = "-x";
513                 pkgCommandArray[2] = packageName;
514             }
515 
516             Vector returnVector = new Vector();
517             Vector returnErrorVector = new Vector();
518 
519             int returnValue = ExecuteProcess.executeProcessReturnVector(pkgCommandArray, returnVector, returnErrorVector);
520 
521             log = pkgCommand + "<br><b>Returns:</b>";
522             LogManager.addCommandsLogfileComment(log);
523             for (int i = 0; i < returnVector.size(); i++) {
524                 log = "<b>" + returnVector.get(i) + "</b>";
525                 LogManager.addCommandsLogfileComment(log);
526             }
527 
528             // log = "<br>";
529             // LogManager.addCommandsLogfileComment(log);
530 
531             String installedPackageVersion = helper.getVersionString(returnVector);
532             String newPackageVersion = packageData.getPkgVersion();
533 
534             if ( ! installData.installedProductMinorSet() ) {
535                 int productMinor = helper.getInstalledMinor(installedPackageVersion);
536                 installData.setInstalledProductMinor(productMinor);
537                 installData.setInstalledProductMinorSet(true);
538             }
539 
540             if (( installedPackageVersion != null ) && ( newPackageVersion != null )) {
541             	if ( checkIfInstalledIsOlder ) {
542                     firstPackageIsOlder = helper.comparePackageVersions(installedPackageVersion, newPackageVersion);
543                 } else {
544                     firstPackageIsOlder = helper.comparePackageVersions(newPackageVersion, installedPackageVersion);
545                 }
546             }
547         }
548 
549         if ( checkIfInstalledIsOlder ) {
550             if ( firstPackageIsOlder ) {
551                 log = "<b>-> Installed package is older</b><br>";
552                 LogManager.addCommandsLogfileComment(log);
553             } else {
554                 log = "<b>-> Installed package is not older</b><br>";
555                 LogManager.addCommandsLogfileComment(log);
556             }
557         } else {
558             if ( firstPackageIsOlder ) {
559                 log = "<b>-> Package in installation set is older</b><br>";
560                 LogManager.addCommandsLogfileComment(log);
561             } else {
562                 log = "<b>-> Package in installation set is not older</b><br>";
563                 LogManager.addCommandsLogfileComment(log);
564             }
565         }
566 
567         return firstPackageIsOlder;
568     }
569 
570 }
571