xref: /AOO41X/main/javainstaller2/src/JavaSetup/org/openoffice/setup/Util/InfoDir.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.Util;
29 
30 import org.openoffice.setup.InstallData;
31 import org.openoffice.setup.ResourceManager;
32 import java.io.File;
33 import java.util.Vector;
34 
35 public class InfoDir {
36 
37     private InfoDir() {
38     }
39 
40     static private String copySourceFile(String fileName) {
41         InstallData data = InstallData.getInstance();
42         File jarFile = data.getJarFilePath();
43         String destFile = null;
44 
45         if ( jarFile != null ) {
46             String sourceDir = jarFile.getParent();
47             File sourceFileFile = new File(sourceDir, fileName);
48             String sourceFile = sourceFileFile.getPath();
49 
50             // String jarFileName = jarFile.getName();
51             File destDir = new File(data.getInstallDefaultDir(), data.getProductDir());
52             File destFileFile = new File(destDir, fileName);
53             destFile = destFileFile.getPath();
54 
55             boolean success = SystemManager.copy(sourceFile, destFile);
56         }
57 
58         return destFile;
59     }
60 
61     static private void copyInstallDirectoryWithExtension(File destBaseDir, String subDirName, String fileExtension) {
62         InstallData data = InstallData.getInstance();
63         File sourceDir = data.getInfoRoot(subDirName);
64         if ( sourceDir != null ) {
65             File destDir = new File(destBaseDir, subDirName);
66             destDir.mkdir();
67             SystemManager.copyAllFiles(sourceDir, destDir, fileExtension);
68         }
69     }
70 
71     static private void copyInstallDirectoryWithExtension(File destBaseDir, String subDirName, String fileExtension, String unixRights) {
72         InstallData data = InstallData.getInstance();
73         File sourceDir = data.getInfoRoot(subDirName);
74         if ( sourceDir != null ) {
75             File destDir = new File(destBaseDir, subDirName);
76             destDir.mkdir();
77             SystemManager.copyAllFiles(sourceDir, destDir, fileExtension);
78             SystemManager.setUnixPrivilegesDirectory(destDir, fileExtension, unixRights);
79         }
80     }
81 
82     static private void copyInstallDirectoryDoubleSubdir(File destBaseDir, String dir1, String dir2) {
83         InstallData data = InstallData.getInstance();
84         File sourceDir1 = data.getInfoRoot(dir1);
85         File sourceDir = new File(sourceDir1, dir2);
86 
87         destBaseDir.mkdir();
88         File destDir1 = new File(destBaseDir, dir1);
89         destDir1.mkdir();
90         File destDir = new File(destDir1, dir2);
91         destDir.mkdir();
92 
93         SystemManager.copyAllFiles(sourceDir, destDir);
94     }
95 
96     static private File createUninstallDir() {
97         InstallData data = InstallData.getInstance();
98         File baseDir = new File(data.getInstallDefaultDir(), data.getProductDir());
99         baseDir = new File(baseDir, data.getUninstallDirName());
100         baseDir.mkdir();
101         return baseDir;
102     }
103 
104     static private void copyGetUidSoFile(File dir) {
105         InstallData data = InstallData.getInstance();
106         String uidFileSource = data.getGetUidPath();
107         if ( uidFileSource != null ) {
108             // Copying the "getuid.so" file into installation
109             String fileName = "getuid.so";
110             File destFile = new File(dir, fileName);
111             String uidFileDest = destFile.getPath();
112             boolean success = SystemManager.copy(uidFileSource, uidFileDest);
113             data.setGetUidPath(uidFileDest);
114         }
115     }
116 
117     static private void copyJreFile(File dir) {
118         InstallData data = InstallData.getInstance();
119         String jrefilename = System.getProperty("JRE_FILE");
120 
121         if ( jrefilename != null ) {
122             // For Solaris, JRE_FILE can already contain the complete path.
123             // Otherwise it contains only the filename
124             File jreFile = new File(jrefilename);
125 
126             if ( ! jreFile.exists()) {
127                 jreFile = new File(data.getPackagePath(), jrefilename);
128             }
129 
130             if ( jreFile.exists() ) {
131                 String jreFileSource = jreFile.getPath();
132                 File destDir = new File(dir, "jre");
133                 destDir.mkdir();
134                 String onlyFileName = jreFile.getName();
135                 File destFile = new File(destDir, onlyFileName);
136 
137                 // In maintenance mode the file already exists
138                 if ( ! destFile.exists() ) {
139                     String jreFileDest = destFile.getPath();
140                     boolean success = SystemManager.copy(jreFileSource, jreFileDest);
141                 }
142             }
143         }
144     }
145 
146     static private void moveAdminFiles(File dir) {
147         InstallData data = InstallData.getInstance();
148 
149         if ( data.getAdminFileNameReloc() != null ) {
150             File sourceFile = new File(data.getAdminFileNameReloc());
151             String fileName = sourceFile.getName();
152             File destFile = new File(dir, fileName);
153             boolean success = SystemManager.copy(sourceFile.getPath(), destFile.getPath());
154             data.setAdminFileNameReloc(destFile.getPath());
155             sourceFile.delete();
156         }
157 
158         if ( data.getAdminFileNameRelocNoDepends() != null ) {
159             File sourceFile = new File(data.getAdminFileNameRelocNoDepends());
160             String fileName = sourceFile.getName();
161             File destFile = new File(dir, fileName);
162             boolean success = SystemManager.copy(sourceFile.getPath(), destFile.getPath());
163             data.setAdminFileNameRelocNoDepends(destFile.getPath());
164             sourceFile.delete();
165         }
166 
167         if ( data.getAdminFileNameNoReloc() != null ) {
168             File sourceFile = new File(data.getAdminFileNameNoReloc());
169             String fileName = sourceFile.getName();
170             File destFile = new File(dir, fileName);
171             boolean success = SystemManager.copy(sourceFile.getPath(), destFile.getPath());
172             data.setAdminFileNameNoReloc(destFile.getPath());
173             sourceFile.delete();
174         }
175 
176         if ( data.getAdminFileNameNoRelocNoDepends() != null ) {
177             File sourceFile = new File(data.getAdminFileNameNoRelocNoDepends());
178             String fileName = sourceFile.getName();
179             File destFile = new File(dir, fileName);
180             boolean success = SystemManager.copy(sourceFile.getPath(), destFile.getPath());
181             data.setAdminFileNameNoRelocNoDepends(destFile.getPath());
182             sourceFile.delete();
183         }
184     }
185 
186     static private void createInfoFile(File dir) {
187         Vector fileContent = new Vector();
188         String line = null;
189         InstallData data = InstallData.getInstance();
190 
191         line = "PackagePath=" + data.getPackagePath();
192         fileContent.add(line);
193         line = "InstallationPrivileges=" + data.getInstallationPrivileges();
194         fileContent.add(line);
195         line = "AdminFileReloc=" + data.getAdminFileNameReloc();
196         fileContent.add(line);
197         line = "AdminFileRelocNoDepends=" + data.getAdminFileNameRelocNoDepends();
198         fileContent.add(line);
199         line = "AdminFileNoReloc=" + data.getAdminFileNameNoReloc();
200         fileContent.add(line);
201         line = "AdminFileNoRelocNoDepends=" + data.getAdminFileNameNoRelocNoDepends();
202         fileContent.add(line);
203         line = "InstallationDir=" + data.getInstallDir();
204         fileContent.add(line);
205         line = "DatabasePath=" + data.getDatabasePath();
206         fileContent.add(line);
207         line = "GetUidFile=" + data.getGetUidPath();
208         fileContent.add(line);
209 
210         String infoFileName = "infoFile";
211         File infoFile = new File(dir, infoFileName);
212         SystemManager.saveCharFileVector(infoFile.getPath(), fileContent);
213     }
214 
215     static private void removeSpecialFiles() {
216         InstallData data = InstallData.getInstance();
217         File jarFile = data.getJarFilePath();
218         SystemManager.deleteFile(jarFile);
219 
220         String jarFilePath = jarFile.getParent();
221         File setupFile = new File(jarFilePath, "setup");
222         SystemManager.deleteFile(setupFile);
223 
224         if ( ! data.getAdminFileNameReloc().equals("null") ) {
225             SystemManager.deleteFile(new File(data.getAdminFileNameReloc()));
226         }
227 
228         if ( ! data.getAdminFileNameRelocNoDepends().equals("null") ) {
229             SystemManager.deleteFile(new File(data.getAdminFileNameRelocNoDepends()));
230         }
231 
232         if ( ! data.getAdminFileNameNoReloc().equals("null") ) {
233             SystemManager.deleteFile(new File(data.getAdminFileNameNoReloc()));
234         }
235 
236         if ( ! data.getAdminFileNameNoRelocNoDepends().equals("null") ) {
237             SystemManager.deleteFile(new File(data.getAdminFileNameNoRelocNoDepends()));
238         }
239 
240         if ( ! data.getGetUidPath().equals("null") ) {
241             SystemManager.deleteFile(new File(data.getGetUidPath()));
242         }
243     }
244 
245     static private void removeInforootSubdir(String dir1, String dir2) {
246         InstallData data = InstallData.getInstance();
247         File subdir1 = data.getInfoRoot(dir1);
248         File subdir2 = new File(subdir1, dir2);
249         if (subdir2 != null) {
250             if ( subdir2.exists() ) {
251                 SystemManager.removeDirectory(subdir2);
252             }
253         }
254     }
255 
256     static private void removeInforootSubdir(String dir) {
257         InstallData data = InstallData.getInstance();
258         File subdir = data.getInfoRoot(dir);
259         if (subdir != null) {
260         	if ( subdir.exists() ) {
261                 SystemManager.removeDirectory(subdir);
262             }
263         }
264     }
265 
266     static private void removeInforoot() {
267         InstallData data = InstallData.getInstance();
268         SystemManager.removeDirectory(data.getInfoRoot());
269     }
270 
271     static public void prepareUninstallation() {
272         // additional tasks for uninstallation
273         // Directory destDir has to exist!
274         InstallData data = InstallData.getInstance();
275         File destDir = new File(data.getInstallDefaultDir(), data.getProductDir());
276         boolean directoryExists = true;
277 
278         if ( ! destDir.exists() ) {
279             try {
280                 directoryExists = SystemManager.create_directory(destDir.getPath());
281             }
282             catch (SecurityException ex) {
283                 String message = ResourceManager.getString("String_ChooseDirectory_No_Write_Access") + ": " + destDir.getPath();
284                 String title = ResourceManager.getString("String_Error");
285                 Informer.showErrorMessage(message, title);
286             }
287         }
288 
289         if ( directoryExists ) {
290             String setupPath = copySourceFile("setup");
291             SystemManager.setUnixPrivileges(setupPath, "775");
292             File jarFile = data.getJarFilePath();
293             copySourceFile(jarFile.getName());
294 
295             File uninstallDir = createUninstallDir();
296             copyInstallDirectoryWithExtension(uninstallDir, "xpd", "xpd");
297             copyInstallDirectoryWithExtension(uninstallDir, "html", "html");
298             copyInstallDirectoryWithExtension(uninstallDir, "images", "gif");
299             copyInstallDirectoryDoubleSubdir(uninstallDir, "html", "images");
300             copyGetUidSoFile(uninstallDir);
301             copyJreFile(uninstallDir);
302             moveAdminFiles(uninstallDir);
303             createInfoFile(uninstallDir);
304         }
305     }
306 
307     static public void removeUninstallationFiles() {
308         // removing selected File
309         removeSpecialFiles();
310         // removing directories html/images, html and xpd
311         removeInforootSubdir("html", "images");
312         removeInforootSubdir("html");
313         removeInforootSubdir("xpd");
314         removeInforootSubdir("images");
315         removeInforootSubdir("jre");
316         removeInforoot();
317     }
318 
319 }
320