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