| package org.eclipse.epp.installer.internal.linux.core; |
| |
| import java.io.File; |
| import java.io.FileInputStream; |
| import java.io.FileOutputStream; |
| import java.io.IOException; |
| import java.util.ArrayList; |
| import java.util.List; |
| import java.util.Properties; |
| |
| import org.eclipse.core.runtime.IPath; |
| import org.eclipse.core.runtime.Path; |
| import org.eclipse.epp.installer.core.IPlatform; |
| import org.eclipse.epp.installer.core.Platform; |
| |
| |
| public class Markers { |
| |
| /** |
| * Represent makers registry key. |
| * |
| */ |
| public static class MarkerKey { |
| /** |
| * Key Path. |
| */ |
| private File keyPath; |
| |
| private String keyName; |
| |
| MarkerKey(File path, String keyName) { |
| this.keyPath = path; |
| this.keyName = keyName; |
| } |
| |
| public boolean isValid() { |
| if (!keyPath.exists()) { |
| return keyPath.mkdirs(); |
| } |
| return true; |
| } |
| public boolean isExist() { |
| if( isValid()) { |
| File keyFile = new File(this.keyPath, this.keyName); |
| return keyFile.exists(); |
| } |
| return false; |
| } |
| |
| /** |
| * Set specified value into value. @see Properties for more information about value format. |
| * @param valueName. |
| * @param value. |
| * @return <code>false</code> if value can't be setted. |
| * @throws IOException if file operations exception occured. |
| */ |
| public boolean setValue(String valueName, String value) |
| throws IOException { |
| if (!isValid()) { |
| return false; |
| } |
| File keyFile = new File(this.keyPath, this.keyName); |
| Properties properties = new Properties(); |
| if (keyFile.exists()) { |
| FileInputStream fis = new FileInputStream(keyFile); |
| if (fis != null) { |
| properties.load(fis); |
| } else { |
| return false; |
| } |
| } |
| properties.setProperty(valueName, value); |
| FileOutputStream fos = new FileOutputStream(keyFile); |
| if (fos == null) { |
| return false; |
| } |
| properties.store(fos, null); |
| |
| return true; |
| } |
| |
| public String getValue( String valueName ) throws IOException { |
| if (!isValid()) { |
| return null; |
| } |
| File keyFile = new File(this.keyPath, this.keyName); |
| Properties properties = new Properties(); |
| if (keyFile.exists()) { |
| FileInputStream fis = new FileInputStream(keyFile); |
| if (fis != null) { |
| properties.load(fis); |
| } else { |
| return null; |
| } |
| } |
| if( properties.containsKey(valueName) ) { |
| return properties.getProperty(valueName); |
| } |
| return null; |
| } |
| |
| public boolean removeValue(String valueName) throws IOException { |
| if (!isValid()) { |
| return false; |
| } |
| File keyFile = new File(this.keyPath, this.keyName); |
| Properties properties = new Properties(); |
| if (keyFile.exists()) { |
| FileInputStream fis = new FileInputStream(keyFile); |
| if (fis != null) { |
| properties.load(fis); |
| } else { |
| return false; |
| } |
| } |
| if( properties.containsKey(valueName)) { |
| properties.remove(valueName); |
| } |
| else { |
| return true; |
| } |
| FileOutputStream fos = new FileOutputStream(keyFile); |
| if (fos == null) { |
| return false; |
| } |
| properties.store(fos, null); |
| |
| return true; |
| } |
| } |
| |
| /** |
| * Opens root key value. |
| * |
| * @return |
| */ |
| public static MarkerKey openKey(String root, String keyName) { |
| MarkerKey key = new MarkerKey(new File( getRoot() + root), keyName); |
| if (key.isValid()) { |
| return key; |
| } |
| return null; |
| } |
| |
| public static String getRoot() { |
| |
| if(canWrite(new File("/etc/"))) return "/etc/"; |
| else return System.getProperty("user.home"); |
| } |
| |
| public static MarkerKey[] openKeys(String root) { |
| File keyPath = new File( getRoot() + root); |
| if( !keyPath.exists() ) { |
| return null; |
| } |
| File[] files = keyPath.listFiles(); |
| |
| List keys = new ArrayList(); |
| |
| for( int i = 0; i < files.length; ++ i ) { |
| File file = files[i]; |
| if( file.isFile() ) { |
| keys.add( new MarkerKey(keyPath, file.getName())); |
| } |
| } |
| MarkerKey[] keysAsArray = new MarkerKey[keys.size()]; |
| keys.toArray(keysAsArray); |
| return keysAsArray; |
| } |
| |
| public static boolean removeKey(String root) { |
| File file = new File( getRoot() + root ); |
| if (file.exists()) { |
| return file.delete(); |
| } |
| return true; |
| } |
| |
| private static boolean canRead(File file) { |
| IPlatform platform = Platform.getPlatform(); |
| if (platform != null) { |
| return platform.canRead(file); |
| } |
| // in case there is no platform implementation |
| return file.canRead(); |
| } |
| |
| private static boolean canWrite(File file) { |
| IPlatform platform = Platform.getPlatform(); |
| if (platform != null) { |
| return platform.canWrite(file); |
| } |
| // in case there is no platform implementation |
| return file.canWrite(); |
| } |
| |
| //permissions |
| public static boolean checkWriteRootPermissions(String root) { |
| IPath path = new Path(getRoot() + root); |
| path = path.makeAbsolute(); |
| for (int i = 0; i <= path.segmentCount(); ++i) { |
| IPath segment = path.removeLastSegments(i); |
| //System.out.println("Checking:" + segment); |
| File pathFile = segment.toFile(); |
| if (pathFile.exists()) { |
| if (!pathFile.isDirectory()) { |
| return false; |
| } |
| if (!canWrite(pathFile)) { |
| return false; |
| } |
| if (!canRead(pathFile)) { |
| return false; |
| } |
| break; |
| } |
| } |
| return true; |
| } |
| public static String checkWriteRootPermissionsAt(String root) { |
| IPath path = new Path(getRoot() + root); |
| path = path.makeAbsolute(); |
| for (int i = 0; i <= path.segmentCount(); ++i) { |
| IPath segment = path.removeLastSegments(i); |
| //System.out.println("Checking:" + segment); |
| File pathFile = segment.toFile(); |
| if (pathFile.exists()) { |
| if (!pathFile.isDirectory()) { |
| return segment.toOSString(); |
| } |
| if (!canWrite(pathFile)) { |
| return segment.toOSString(); |
| } |
| if (!canRead(pathFile)) { |
| return segment.toOSString(); |
| } |
| break; |
| } |
| } |
| return ""; |
| } |
| } |