xref: /AOO41X/main/javainstaller2/src/JavaSetup/org/openoffice/setup/Util/SystemManager.java (revision 67e470dafe1997e73f56ff7ff4878983707e3e07)
1*9a1eeea9SAndrew Rist /**************************************************************
2cdf0e10cSrcweir  *
3*9a1eeea9SAndrew Rist  * Licensed to the Apache Software Foundation (ASF) under one
4*9a1eeea9SAndrew Rist  * or more contributor license agreements.  See the NOTICE file
5*9a1eeea9SAndrew Rist  * distributed with this work for additional information
6*9a1eeea9SAndrew Rist  * regarding copyright ownership.  The ASF licenses this file
7*9a1eeea9SAndrew Rist  * to you under the Apache License, Version 2.0 (the
8*9a1eeea9SAndrew Rist  * "License"); you may not use this file except in compliance
9*9a1eeea9SAndrew Rist  * with the License.  You may obtain a copy of the License at
10cdf0e10cSrcweir  *
11*9a1eeea9SAndrew Rist  *   http://www.apache.org/licenses/LICENSE-2.0
12cdf0e10cSrcweir  *
13*9a1eeea9SAndrew Rist  * Unless required by applicable law or agreed to in writing,
14*9a1eeea9SAndrew Rist  * software distributed under the License is distributed on an
15*9a1eeea9SAndrew Rist  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
16*9a1eeea9SAndrew Rist  * KIND, either express or implied.  See the License for the
17*9a1eeea9SAndrew Rist  * specific language governing permissions and limitations
18*9a1eeea9SAndrew Rist  * under the License.
19cdf0e10cSrcweir  *
20*9a1eeea9SAndrew Rist  *************************************************************/
21*9a1eeea9SAndrew Rist 
22*9a1eeea9SAndrew Rist 
23cdf0e10cSrcweir 
24cdf0e10cSrcweir package org.openoffice.setup.Util;
25cdf0e10cSrcweir 
26cdf0e10cSrcweir import java.io.BufferedInputStream;
27cdf0e10cSrcweir import java.io.BufferedOutputStream;
28cdf0e10cSrcweir import java.io.BufferedReader;
29cdf0e10cSrcweir import java.io.File;
30cdf0e10cSrcweir import java.io.FileInputStream;
31cdf0e10cSrcweir import java.io.FileNotFoundException;
32cdf0e10cSrcweir import java.io.FileOutputStream;
33cdf0e10cSrcweir import java.io.FileWriter;
34cdf0e10cSrcweir import java.io.IOException;
35cdf0e10cSrcweir import java.io.InputStreamReader;
36cdf0e10cSrcweir import java.net.URI;
37cdf0e10cSrcweir import java.net.URL;
38cdf0e10cSrcweir import java.util.HashMap;
39cdf0e10cSrcweir import java.util.Properties;
40cdf0e10cSrcweir import java.util.Vector;
41cdf0e10cSrcweir 
42cdf0e10cSrcweir public class SystemManager {
43cdf0e10cSrcweir 
SystemManager()44cdf0e10cSrcweir     private SystemManager() {
45cdf0e10cSrcweir     }
46cdf0e10cSrcweir 
47cdf0e10cSrcweir     /* the installation root is where the classes reside */
getJarFilePath()48cdf0e10cSrcweir     static public File getJarFilePath() {
49cdf0e10cSrcweir 
50cdf0e10cSrcweir         File jarFile = null;
51cdf0e10cSrcweir 
52cdf0e10cSrcweir         try {
53cdf0e10cSrcweir             Class c  = Class.forName("org.openoffice.setup.ResourceManager");
54cdf0e10cSrcweir             URL url  = c.getResource("setupfiles.properties");
55cdf0e10cSrcweir 
56cdf0e10cSrcweir             String urlString = url.toString();
57cdf0e10cSrcweir 
58cdf0e10cSrcweir             if (urlString.startsWith("jar:")) {
59cdf0e10cSrcweir                 /* ResourceManager.class resides in a jar file. Strip it down to the "file:" part */
60cdf0e10cSrcweir                 urlString = urlString.substring(4, urlString.lastIndexOf("!"));
61cdf0e10cSrcweir                 jarFile = new File(new URI(urlString));
62cdf0e10cSrcweir             }
63cdf0e10cSrcweir 
64cdf0e10cSrcweir         } catch (Exception ex) {
65cdf0e10cSrcweir             /* handle URISyntaxException and ClassNotFoundException */
66cdf0e10cSrcweir             ex.printStackTrace();
67cdf0e10cSrcweir             System.exit(1);
68cdf0e10cSrcweir         }
69cdf0e10cSrcweir 
70cdf0e10cSrcweir         if ( jarFile != null ) {
71cdf0e10cSrcweir             System.err.println("Jar file: " + jarFile.getPath());
72cdf0e10cSrcweir         } else {
73cdf0e10cSrcweir             System.err.println("No jar file used for installation!");
74cdf0e10cSrcweir         }
75cdf0e10cSrcweir 
76cdf0e10cSrcweir         return jarFile;
77cdf0e10cSrcweir     }
78cdf0e10cSrcweir 
79cdf0e10cSrcweir     /* the installation root is where the classes reside */
getResourceRoot()80cdf0e10cSrcweir     static public File getResourceRoot() {
81cdf0e10cSrcweir 
82cdf0e10cSrcweir         File dir = null;
83cdf0e10cSrcweir 
84cdf0e10cSrcweir         try {
85cdf0e10cSrcweir             Class c  = Class.forName("org.openoffice.setup.ResourceManager");
86cdf0e10cSrcweir             URL url  = c.getResource("setupfiles.properties");
87cdf0e10cSrcweir 
88cdf0e10cSrcweir             String urlString = url.toString();
89cdf0e10cSrcweir 
90cdf0e10cSrcweir             if (urlString.startsWith("jar:")) {
91cdf0e10cSrcweir                 /* ResourceManager.class resides in a jar file. Strip it down to the "file:" part */
92cdf0e10cSrcweir                 urlString = urlString.substring(4, urlString.lastIndexOf("!"));
93cdf0e10cSrcweir             } else {
94cdf0e10cSrcweir                 /* ResourceManager.class resides in a directory tree. */
95cdf0e10cSrcweir                 urlString = urlString.substring(0, urlString.lastIndexOf("/org/openoffice/setup/setupfiles.properties"));
96cdf0e10cSrcweir             }
97cdf0e10cSrcweir 
98cdf0e10cSrcweir             dir = new File(new URI(urlString));
99cdf0e10cSrcweir             dir = dir.getParentFile();
100cdf0e10cSrcweir 
101cdf0e10cSrcweir         } catch (Exception ex) {
102cdf0e10cSrcweir             /* handle URISyntaxException and ClassNotFoundException */
103cdf0e10cSrcweir             ex.printStackTrace();
104cdf0e10cSrcweir             System.exit(1);
105cdf0e10cSrcweir         }
106cdf0e10cSrcweir         // }
107cdf0e10cSrcweir 
108cdf0e10cSrcweir         if ( dir != null ) {
109cdf0e10cSrcweir             // System.err.println("Resource Root: " + dir.getPath());
110cdf0e10cSrcweir         } else {
111cdf0e10cSrcweir             System.err.println("No resource root found!");
112cdf0e10cSrcweir         }
113cdf0e10cSrcweir 
114cdf0e10cSrcweir         return dir;
115cdf0e10cSrcweir     }
116cdf0e10cSrcweir 
getPackagePath(String subdir)117cdf0e10cSrcweir     static public String getPackagePath(String subdir) {
118cdf0e10cSrcweir 
119cdf0e10cSrcweir         String path = null;
120cdf0e10cSrcweir 
121cdf0e10cSrcweir         File dir = getResourceRoot();
122cdf0e10cSrcweir         if (dir != null) {
123cdf0e10cSrcweir             // System.err.println("Resource root: " + dir.getPath());
124cdf0e10cSrcweir             dir = new File(dir, subdir);
125cdf0e10cSrcweir             if (! dir.exists()) {
126cdf0e10cSrcweir                 System.err.println("Error: Directory \"" + subdir + "\" does not exist at resouce root");
127cdf0e10cSrcweir             } else {
128cdf0e10cSrcweir                 path = dir.getPath();
129cdf0e10cSrcweir             }
130cdf0e10cSrcweir         }
131cdf0e10cSrcweir 
132cdf0e10cSrcweir         if ( path != null ) {
133cdf0e10cSrcweir             if ( ! path.endsWith("/")) {
134cdf0e10cSrcweir                 path = path + "/";
135cdf0e10cSrcweir             }
136cdf0e10cSrcweir         }
137cdf0e10cSrcweir 
138cdf0e10cSrcweir         if ( path != null ) {
139cdf0e10cSrcweir             System.err.println("Path to packages: " + path);
140cdf0e10cSrcweir         } else {
141cdf0e10cSrcweir             System.err.println("No path to packages found!");
142cdf0e10cSrcweir         }
143cdf0e10cSrcweir 
144cdf0e10cSrcweir         return path;
145cdf0e10cSrcweir     }
146cdf0e10cSrcweir 
find_file(String fileName)147cdf0e10cSrcweir     static public boolean find_file(String fileName) {
148cdf0e10cSrcweir         boolean found = false;
149cdf0e10cSrcweir         File file = new File(fileName);
150cdf0e10cSrcweir         found = file.exists();
151cdf0e10cSrcweir         return found;
152cdf0e10cSrcweir     }
153cdf0e10cSrcweir 
exists_directory(String directory)154cdf0e10cSrcweir     static public boolean exists_directory(String directory) {
155cdf0e10cSrcweir         File dir = new File(directory);
156cdf0e10cSrcweir         return dir.exists();
157cdf0e10cSrcweir     }
158cdf0e10cSrcweir 
create_directory(String directory)159cdf0e10cSrcweir     static public boolean create_directory(String directory) throws SecurityException {
160cdf0e10cSrcweir         boolean created = false;
161cdf0e10cSrcweir         File dir = new File(directory);
162cdf0e10cSrcweir         try {
163cdf0e10cSrcweir             created = dir.mkdirs();
164cdf0e10cSrcweir         }
165cdf0e10cSrcweir         catch (SecurityException ex) {
166cdf0e10cSrcweir             throw ex;
167cdf0e10cSrcweir         }
168cdf0e10cSrcweir 
169cdf0e10cSrcweir         return created;
170cdf0e10cSrcweir     }
171cdf0e10cSrcweir 
getParentDirectory(String dir)172cdf0e10cSrcweir     static public String getParentDirectory(String dir) {
173cdf0e10cSrcweir         File installFile = new File(dir);
174cdf0e10cSrcweir         String parentDir = installFile.getParent();
175cdf0e10cSrcweir         if ( parentDir == null ) {
176cdf0e10cSrcweir             parentDir = "/";
177cdf0e10cSrcweir         }
178cdf0e10cSrcweir         return parentDir;
179cdf0e10cSrcweir     }
180cdf0e10cSrcweir 
getInstallationPrivileges()181cdf0e10cSrcweir     static public String getInstallationPrivileges() {
182cdf0e10cSrcweir 
183cdf0e10cSrcweir         String type = "";
184cdf0e10cSrcweir         String user = java.lang.System.getProperty("user.name");
185cdf0e10cSrcweir         // System.out.println("UserHome: " + java.lang.System.getProperty("user.home"));
186cdf0e10cSrcweir 
187cdf0e10cSrcweir         if ( user.equalsIgnoreCase("root")) {
188cdf0e10cSrcweir             type = "root";
189cdf0e10cSrcweir             System.err.println("Root privileges");
190cdf0e10cSrcweir         } else {
191cdf0e10cSrcweir             type = "user";
192cdf0e10cSrcweir             System.err.println("User privileges");
193cdf0e10cSrcweir         }
194cdf0e10cSrcweir 
195cdf0e10cSrcweir         return type;
196cdf0e10cSrcweir     }
197cdf0e10cSrcweir 
isUserInstallation()198cdf0e10cSrcweir     static public boolean isUserInstallation() {
199cdf0e10cSrcweir 
200cdf0e10cSrcweir         boolean isUserInstallation = false;
201cdf0e10cSrcweir         String user = java.lang.System.getProperty("user.name");
202cdf0e10cSrcweir 
203cdf0e10cSrcweir         if ( user.equalsIgnoreCase("root")) {
204cdf0e10cSrcweir             isUserInstallation = false;
205cdf0e10cSrcweir             System.err.println("Root privileges");
206cdf0e10cSrcweir         } else {
207cdf0e10cSrcweir             isUserInstallation = true;
208cdf0e10cSrcweir             System.err.println("User privileges");
209cdf0e10cSrcweir         }
210cdf0e10cSrcweir 
211cdf0e10cSrcweir         return isUserInstallation;
212cdf0e10cSrcweir     }
213cdf0e10cSrcweir 
isRootInstallation()214cdf0e10cSrcweir     static public boolean isRootInstallation() {
215cdf0e10cSrcweir 
216cdf0e10cSrcweir         boolean isRootInstallation = false;
217cdf0e10cSrcweir         String user = java.lang.System.getProperty("user.name");
218cdf0e10cSrcweir 
219cdf0e10cSrcweir         if ( user.equalsIgnoreCase("root")) {
220cdf0e10cSrcweir             isRootInstallation = true;
221cdf0e10cSrcweir         } else {
222cdf0e10cSrcweir             isRootInstallation = false;
223cdf0e10cSrcweir         }
224cdf0e10cSrcweir 
225cdf0e10cSrcweir         return isRootInstallation;
226cdf0e10cSrcweir     }
227cdf0e10cSrcweir 
getOSType()228cdf0e10cSrcweir     static public String getOSType() {
229cdf0e10cSrcweir         String osVersion = java.lang.System.getProperty("os.name");
230cdf0e10cSrcweir         System.err.println("OS: " + osVersion);
231cdf0e10cSrcweir         return osVersion;
232cdf0e10cSrcweir     }
233cdf0e10cSrcweir 
getOSArchitecture()234cdf0e10cSrcweir     static public String getOSArchitecture() {
235cdf0e10cSrcweir         String osArchitecture = java.lang.System.getProperty("os.arch");
236cdf0e10cSrcweir         System.out.println("OSArchitecture: " + osArchitecture);
237cdf0e10cSrcweir         return osArchitecture;
238cdf0e10cSrcweir     }
239cdf0e10cSrcweir 
getOSVersion()240cdf0e10cSrcweir     static public String getOSVersion() {
241cdf0e10cSrcweir         String osVersion = java.lang.System.getProperty("os.version");
242cdf0e10cSrcweir         System.out.println("OSVersion: " + osVersion);
243cdf0e10cSrcweir         return osVersion;
244cdf0e10cSrcweir     }
245cdf0e10cSrcweir 
getEnvironmentHashMap()246cdf0e10cSrcweir     static public HashMap getEnvironmentHashMap() {
247cdf0e10cSrcweir         // readonly map from getenv()
248cdf0e10cSrcweir         // System.getenv only supported in Java 1.5, properties have to be set in shell script
249cdf0e10cSrcweir         // Map map = java.lang.System.getenv();
250cdf0e10cSrcweir         // HashMap myMap = new HashMap(map);
251cdf0e10cSrcweir         Properties props = System.getProperties();
252cdf0e10cSrcweir         HashMap myMap = new HashMap(props);
253cdf0e10cSrcweir         return myMap;
254cdf0e10cSrcweir     }
255cdf0e10cSrcweir 
dumpStringArray(String[] myStringArray)256cdf0e10cSrcweir     static public void dumpStringArray(String[] myStringArray) {
257cdf0e10cSrcweir         for (int i = 0; i < myStringArray.length; i++) {
258cdf0e10cSrcweir             System.out.println(myStringArray[i]);
259cdf0e10cSrcweir         }
260cdf0e10cSrcweir     }
261cdf0e10cSrcweir 
dumpFile(String baseFileName, String dumpFileName)262cdf0e10cSrcweir     static public void dumpFile(String baseFileName, String dumpFileName) {
263cdf0e10cSrcweir         Vector fileContent = readCharFileVector(baseFileName);
264cdf0e10cSrcweir         saveCharFileVector(dumpFileName, fileContent);
265cdf0e10cSrcweir     }
266cdf0e10cSrcweir 
readCharFileVector(String fileName)267cdf0e10cSrcweir     static public Vector readCharFileVector(String fileName) {
268cdf0e10cSrcweir         Vector fileContent = new Vector();
269cdf0e10cSrcweir 
270cdf0e10cSrcweir         File file = new File(fileName);
271cdf0e10cSrcweir         if ( file.exists()) {
272cdf0e10cSrcweir             try {
273cdf0e10cSrcweir                 FileInputStream fs = new FileInputStream(file);
274cdf0e10cSrcweir                 BufferedReader bs = new BufferedReader(new InputStreamReader(fs));
275cdf0e10cSrcweir                 String zeile;
276cdf0e10cSrcweir                 while((zeile = bs.readLine())!=null) {
277cdf0e10cSrcweir                     fileContent.addElement(zeile);
278cdf0e10cSrcweir                 }
279cdf0e10cSrcweir             }
280cdf0e10cSrcweir             catch (IOException e) {
281cdf0e10cSrcweir                 System.out.println(e);
282cdf0e10cSrcweir             }
283cdf0e10cSrcweir         } else {
284cdf0e10cSrcweir             System.out.println( "Error: File not found: " +  fileName);
285cdf0e10cSrcweir         }
286cdf0e10cSrcweir 
287cdf0e10cSrcweir         return fileContent;
288cdf0e10cSrcweir     }
289cdf0e10cSrcweir 
290cdf0e10cSrcweir 
saveCharFileVector(String fileName, Vector fileContent)291cdf0e10cSrcweir     static public void saveCharFileVector(String fileName, Vector fileContent) {
292cdf0e10cSrcweir         FileWriter fw = null;
293cdf0e10cSrcweir         try
294cdf0e10cSrcweir         {
295cdf0e10cSrcweir             fw = new FileWriter(fileName);
296cdf0e10cSrcweir             String fileContentStr = "";
297cdf0e10cSrcweir             for (int i = 0; i < fileContent.size() ; i++) {
298cdf0e10cSrcweir                 fileContentStr = fileContentStr + fileContent.get(i) + "\n";
299cdf0e10cSrcweir                 // System.out.println(fileContent.get(i));
300cdf0e10cSrcweir             }
301cdf0e10cSrcweir             fw.write(fileContentStr);
302cdf0e10cSrcweir         }
303cdf0e10cSrcweir         catch ( IOException e ) {
304cdf0e10cSrcweir             System.out.println( "Could not create file: " +  fileName);
305cdf0e10cSrcweir         }
306cdf0e10cSrcweir         finally {
307cdf0e10cSrcweir             try {
308cdf0e10cSrcweir                 if ( fw != null ) fw.close();
309cdf0e10cSrcweir             } catch (IOException e) {}
310cdf0e10cSrcweir         }
311cdf0e10cSrcweir     }
312cdf0e10cSrcweir 
copyAllFiles(File source, File dest)313cdf0e10cSrcweir     static public void copyAllFiles(File source, File dest) {
314cdf0e10cSrcweir         File[] file = source.listFiles();
315cdf0e10cSrcweir         if (file != null) {
316cdf0e10cSrcweir             for (int i = 0; i < file.length; i++) {
317cdf0e10cSrcweir                 copy(file[i].getPath(), dest.getPath());
318cdf0e10cSrcweir             }
319cdf0e10cSrcweir         }
320cdf0e10cSrcweir     }
321cdf0e10cSrcweir 
copyAllFiles(File source, File dest, String ext)322cdf0e10cSrcweir     static public void copyAllFiles(File source, File dest, String ext) {
323cdf0e10cSrcweir         File[] file = source.listFiles(new FileExtensionFilter(ext));
324cdf0e10cSrcweir         if (file != null) {
325cdf0e10cSrcweir             for (int i = 0; i < file.length; i++) {
326cdf0e10cSrcweir                 copy(file[i].getPath(), dest.getPath());
327cdf0e10cSrcweir             }
328cdf0e10cSrcweir         }
329cdf0e10cSrcweir     }
330cdf0e10cSrcweir 
331cdf0e10cSrcweir     // second parameter can be a complete file name or an existing directory
copy(String source, String dest)332cdf0e10cSrcweir     static public boolean copy(String source, String dest) {
333cdf0e10cSrcweir 
334cdf0e10cSrcweir         // is the second parameter a file name or a directory?
335cdf0e10cSrcweir         File dir = new File(dest);
336cdf0e10cSrcweir         if ( dir.isDirectory() ) {
337cdf0e10cSrcweir             File sourceFile = new File(source);
338cdf0e10cSrcweir             String fileName = sourceFile.getName();
339cdf0e10cSrcweir             File destFile = new File(dest, fileName);
340cdf0e10cSrcweir             dest = destFile.getPath();
341cdf0e10cSrcweir         }
342cdf0e10cSrcweir 
343cdf0e10cSrcweir         boolean file_copied = false;
344cdf0e10cSrcweir         FileInputStream fis;
345cdf0e10cSrcweir         BufferedInputStream bis;
346cdf0e10cSrcweir         FileOutputStream fos;
347cdf0e10cSrcweir         BufferedOutputStream bos;
348cdf0e10cSrcweir         byte[] b;
349cdf0e10cSrcweir         try {
350cdf0e10cSrcweir             fis = new FileInputStream(source);
351cdf0e10cSrcweir             fos = new FileOutputStream(dest);
352cdf0e10cSrcweir         } catch (FileNotFoundException ex) {
353cdf0e10cSrcweir             throw new Error("File not found");
354cdf0e10cSrcweir         }
355cdf0e10cSrcweir         // put file into buffer
356cdf0e10cSrcweir         bis = new BufferedInputStream(fis);
357cdf0e10cSrcweir         bos = new BufferedOutputStream(fos);
358cdf0e10cSrcweir         try { // read file, write and close
359cdf0e10cSrcweir             b = new byte[bis.available()];
360cdf0e10cSrcweir             bis.read(b);
361cdf0e10cSrcweir             bos.write(b);
362cdf0e10cSrcweir             bis.close();
363cdf0e10cSrcweir             bos.close();
364cdf0e10cSrcweir             file_copied = true;
365cdf0e10cSrcweir         } catch (IOException e) {
366cdf0e10cSrcweir             System.out.println("Dateien wurden nicht kopiert!");
367cdf0e10cSrcweir         }
368cdf0e10cSrcweir 
369cdf0e10cSrcweir         return file_copied;
370cdf0e10cSrcweir     }
371cdf0e10cSrcweir 
deleteFile(File file)372cdf0e10cSrcweir     static public boolean deleteFile(File file) {
373cdf0e10cSrcweir         boolean success = false;
374cdf0e10cSrcweir         if ( file.exists() && file != null ) {
375cdf0e10cSrcweir             success = file.delete();
376cdf0e10cSrcweir         }
377cdf0e10cSrcweir         return success;
378cdf0e10cSrcweir     }
379cdf0e10cSrcweir 
createDirectory(File dir)380cdf0e10cSrcweir     static public boolean createDirectory(File dir) throws SecurityException {
381cdf0e10cSrcweir         boolean created = false;
382cdf0e10cSrcweir         try {
383cdf0e10cSrcweir             created = dir.mkdirs();
384cdf0e10cSrcweir         }
385cdf0e10cSrcweir         catch (SecurityException ex) {
386cdf0e10cSrcweir             throw ex;
387cdf0e10cSrcweir         }
388cdf0e10cSrcweir 
389cdf0e10cSrcweir         return created;
390cdf0e10cSrcweir     }
391cdf0e10cSrcweir 
removeDirectory(File dir)392cdf0e10cSrcweir     static public void removeDirectory(File dir) {
393cdf0e10cSrcweir         if ( dir.exists() && dir.isDirectory() ) {
394cdf0e10cSrcweir             File[] file = dir.listFiles();
395cdf0e10cSrcweir             if (file != null) {
396cdf0e10cSrcweir                 for (int i = 0; i < file.length; i++) {
397cdf0e10cSrcweir                     deleteFile(file[i]);
398cdf0e10cSrcweir                 }
399cdf0e10cSrcweir             }
400cdf0e10cSrcweir             dir.delete();
401cdf0e10cSrcweir         }
402cdf0e10cSrcweir     }
403cdf0e10cSrcweir 
logModuleStates()404cdf0e10cSrcweir     static public boolean logModuleStates() {
405cdf0e10cSrcweir         boolean logStates = false;
406cdf0e10cSrcweir         // System.getenv only supported in Java 1.5, property set in shell script
407cdf0e10cSrcweir         // String logStatesEnv = System.getenv("LOG_MODULE_STATES");
408cdf0e10cSrcweir         String logStatesEnv = System.getProperty("LOG_MODULE_STATES");
409cdf0e10cSrcweir 
410cdf0e10cSrcweir         if ( logStatesEnv != null ) {
411cdf0e10cSrcweir             logStates = true;
412cdf0e10cSrcweir         }
413cdf0e10cSrcweir 
414cdf0e10cSrcweir         return logStates;
415cdf0e10cSrcweir     }
416cdf0e10cSrcweir 
setUnixPrivileges(String fileName, String unixRights)417cdf0e10cSrcweir     static public void setUnixPrivileges(String fileName, String unixRights) {
418cdf0e10cSrcweir         // String command = "chmod " + unixRights + " " + fileName;
419cdf0e10cSrcweir         String[] commandArray = new String[3];
420cdf0e10cSrcweir         commandArray[0] = "chmod";
421cdf0e10cSrcweir         commandArray[1] = unixRights;
422cdf0e10cSrcweir         commandArray[2] = fileName;
423cdf0e10cSrcweir         int value = ExecuteProcess.executeProcessReturnValue(commandArray);
424cdf0e10cSrcweir     }
425cdf0e10cSrcweir 
setUnixPrivilegesDirectory(File directoryName, String ext, String unixRights)426cdf0e10cSrcweir     static public void setUnixPrivilegesDirectory(File directoryName, String ext, String unixRights) {
427cdf0e10cSrcweir         File[] file = directoryName.listFiles(new FileExtensionFilter(ext));
428cdf0e10cSrcweir         if (file != null) {
429cdf0e10cSrcweir             for (int i = 0; i < file.length; i++) {
430cdf0e10cSrcweir                 setUnixPrivileges(file[i].getPath(), unixRights);
431cdf0e10cSrcweir             }
432cdf0e10cSrcweir         }
433cdf0e10cSrcweir     }
434cdf0e10cSrcweir 
calculateDiscSpace(String directory)435cdf0e10cSrcweir     static public int calculateDiscSpace(String directory) {
436cdf0e10cSrcweir         String command = "df -k " + directory;
437cdf0e10cSrcweir         String[] commandArray = new String[3];
438cdf0e10cSrcweir         commandArray[0] = "df";
439cdf0e10cSrcweir         commandArray[1] = "-k";
440cdf0e10cSrcweir         commandArray[2] = directory;
441cdf0e10cSrcweir 
442cdf0e10cSrcweir         int size = 0;
443cdf0e10cSrcweir         Vector returnVector = new Vector();
444cdf0e10cSrcweir         Vector returnErrorVector = new Vector();
445cdf0e10cSrcweir         int returnValue = ExecuteProcess.executeProcessReturnVector(commandArray, returnVector, returnErrorVector);
446cdf0e10cSrcweir         if ( returnValue == 0) {
447cdf0e10cSrcweir             int max = returnVector.size();
448cdf0e10cSrcweir             if ( max > 0 ) {
449cdf0e10cSrcweir                 String returnLine = (String) returnVector.get(max-1);
450cdf0e10cSrcweir 
451cdf0e10cSrcweir                 // The fourth value is the available disc space (if the first value is a path)
452cdf0e10cSrcweir                 // Otherwise it can also be the third value, if the first is not a path.
453cdf0e10cSrcweir                 // If the first value is not a path, the string starts with white spaces.
454cdf0e10cSrcweir 
455cdf0e10cSrcweir                 int position = 3;
456cdf0e10cSrcweir                 if ( returnLine.startsWith(" ")) {
457cdf0e10cSrcweir                     position = 2;
458cdf0e10cSrcweir                 }
459cdf0e10cSrcweir 
460cdf0e10cSrcweir                 returnLine = returnLine.trim();
461cdf0e10cSrcweir                 String[] returnArray = returnLine.split("\\s+");
462cdf0e10cSrcweir 
463cdf0e10cSrcweir                 if ( returnArray.length > 3 ) {
464cdf0e10cSrcweir                     String sizeString = returnArray[position];
465cdf0e10cSrcweir 
466cdf0e10cSrcweir                     // Special handling for very large hard discs that cannot be converted to int
467cdf0e10cSrcweir                     if ( sizeString.length() >= Integer.toString(Integer.MAX_VALUE).length() ) {
468cdf0e10cSrcweir                     	sizeString = Integer.toString(Integer.MAX_VALUE);
469cdf0e10cSrcweir                     }
470cdf0e10cSrcweir 
471cdf0e10cSrcweir                     // Converting from String to int
472cdf0e10cSrcweir                     size = Integer.parseInt(sizeString);
473cdf0e10cSrcweir                 }
474cdf0e10cSrcweir             }
475cdf0e10cSrcweir         }
476cdf0e10cSrcweir 
477cdf0e10cSrcweir         return size;
478cdf0e10cSrcweir     }
479cdf0e10cSrcweir 
480cdf0e10cSrcweir }
481