diff options
author | Denis Lila <[email protected]> | 2011-03-15 11:11:07 -0400 |
---|---|---|
committer | Denis Lila <[email protected]> | 2011-03-15 11:11:07 -0400 |
commit | e3baa1c7d05508cb1b9e522f0157a0cf62491b42 (patch) | |
tree | 54fb6340fd61a22d469207668c3d1f80edec3355 | |
parent | 20e7ad77b70507d5f53ef945147751a7e41e86fe (diff) |
Handling error code returning functions.
-rw-r--r-- | ChangeLog | 24 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/Launcher.java | 2 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/cache/CacheDirectory.java | 4 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/cache/CacheUtil.java | 2 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java | 3 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/controlpanel/CachePane.java | 3 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/resources/Messages.properties | 7 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java | 4 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/services/XPersistenceService.java | 8 | ||||
-rw-r--r-- | netx/net/sourceforge/jnlp/util/FileUtils.java | 77 |
10 files changed, 116 insertions, 18 deletions
@@ -1,3 +1,27 @@ +2011-03-15 Denis Lila <[email protected]> + + * netx/net/sourceforge/jnlp/Launcher.java + (markNetxRunning): Throw exception if directories can't be created. + * netx/net/sourceforge/jnlp/cache/CacheDirectory.java + (cleanParent): Print error message if file can't be deleted. + * netx/net/sourceforge/jnlp/cache/CacheUtil.java + (getCacheFile): Throw exception if directories can't be created. + * netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java + (save): Throw exception if directories can't be created. + * netx/net/sourceforge/jnlp/controlpanel/CachePane.java + (createButtonPanel): Print error message if file can't be deleted. + * netx/net/sourceforge/jnlp/resources/Messages.properties + Added messages. + * netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java + (initializeStreams): Throw exception if directories can't be created. + * netx/net/sourceforge/jnlp/services/XPersistenceService.java + (create, get): Throw exception if directories can't be created. + (delete): Print error message if file can't be deleted. + * netx/net/sourceforge/jnlp/util/FileUtils.java + (createRestrictedFile): Throw exception if file permissions can't be + changed. + (createParentDir, deleteWithErrMesg): new functions. + 2011-03-15 Omair Majid <[email protected]> * Makefile.am (LAUNCHER_BOOTCLASSPATH, PLUGIN_BOOTCLASSPATH) diff --git a/netx/net/sourceforge/jnlp/Launcher.java b/netx/net/sourceforge/jnlp/Launcher.java index ec0f7eb..efd3c4a 100644 --- a/netx/net/sourceforge/jnlp/Launcher.java +++ b/netx/net/sourceforge/jnlp/Launcher.java @@ -709,7 +709,7 @@ public class Launcher { File netxRunningFile = new File(JNLPRuntime.getConfiguration() .getProperty(DeploymentConfiguration.KEY_USER_NETX_RUNNING_FILE)); if (!netxRunningFile.exists()) { - netxRunningFile.getParentFile().mkdirs(); + FileUtils.createParentDir(netxRunningFile); FileUtils.createRestrictedFile(netxRunningFile, true); FileOutputStream fos = new FileOutputStream(netxRunningFile); try { diff --git a/netx/net/sourceforge/jnlp/cache/CacheDirectory.java b/netx/net/sourceforge/jnlp/cache/CacheDirectory.java index 9468ba2..538449d 100644 --- a/netx/net/sourceforge/jnlp/cache/CacheDirectory.java +++ b/netx/net/sourceforge/jnlp/cache/CacheDirectory.java @@ -39,6 +39,8 @@ package net.sourceforge.jnlp.cache; import java.io.File; import java.util.ArrayList; +import net.sourceforge.jnlp.util.FileUtils; + public class CacheDirectory { /** * Get the structure of directory for keeping track of the protocol and @@ -103,7 +105,7 @@ public class CacheDirectory { if (parent.getParent() == null) return; // Don't delete the root. if (parent.getChildren().size() == 0) { - parent.getFile().delete(); + FileUtils.deleteWithErrMesg(parent.getFile()); parent.getParent().removeChild(parent); cleanParent(parent); } diff --git a/netx/net/sourceforge/jnlp/cache/CacheUtil.java b/netx/net/sourceforge/jnlp/cache/CacheUtil.java index 3e0a966..bb88325 100644 --- a/netx/net/sourceforge/jnlp/cache/CacheUtil.java +++ b/netx/net/sourceforge/jnlp/cache/CacheUtil.java @@ -291,7 +291,7 @@ public class CacheUtil { String cacheDir = JNLPRuntime.getConfiguration() .getProperty(DeploymentConfiguration.KEY_USER_CACHE_DIR); File localFile = urlToPath(source, cacheDir); - localFile.getParentFile().mkdirs(); + FileUtils.createParentDir(localFile); return localFile; } catch (Exception ex) { diff --git a/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java b/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java index ded5923..8261749 100644 --- a/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java +++ b/netx/net/sourceforge/jnlp/config/DeploymentConfiguration.java @@ -38,6 +38,7 @@ import java.util.Set; import javax.naming.ConfigurationException; import net.sourceforge.jnlp.runtime.JNLPRuntime; +import net.sourceforge.jnlp.util.FileUtils; /** * Manages the various properties and configuration related to deployment. @@ -518,7 +519,7 @@ public final class DeploymentConfiguration { } } - userPropertiesFile.getParentFile().mkdirs(); + FileUtils.createParentDir(userPropertiesFile); OutputStream out = new BufferedOutputStream(new FileOutputStream(userPropertiesFile)); try { toSave.store(out, DEPLOYMENT_COMMENT); diff --git a/netx/net/sourceforge/jnlp/controlpanel/CachePane.java b/netx/net/sourceforge/jnlp/controlpanel/CachePane.java index b9c9cff..4c97897 100644 --- a/netx/net/sourceforge/jnlp/controlpanel/CachePane.java +++ b/netx/net/sourceforge/jnlp/controlpanel/CachePane.java @@ -43,6 +43,7 @@ import net.sourceforge.jnlp.cache.CacheDirectory; import net.sourceforge.jnlp.cache.DirectoryNode; import net.sourceforge.jnlp.config.DeploymentConfiguration; import net.sourceforge.jnlp.runtime.Translator; +import net.sourceforge.jnlp.util.FileUtils; public class CachePane extends JPanel { @@ -132,7 +133,7 @@ public class CachePane extends JPanel { DirectoryNode fileNode = ((DirectoryNode) cacheTable.getModel().getValueAt(modelRow, 0)); if (fileNode.getFile().delete()) { fileNode.getParent().removeChild(fileNode); - fileNode.getInfoFile().delete(); + FileUtils.deleteWithErrMesg(fileNode.getInfoFile()); ((DefaultTableModel) cacheTable.getModel()).removeRow(modelRow); cacheTable.getSelectionModel().setSelectionInterval(row, row); CacheDirectory.cleanParent(fileNode); diff --git a/netx/net/sourceforge/jnlp/resources/Messages.properties b/netx/net/sourceforge/jnlp/resources/Messages.properties index a002bc3..55717b3 100644 --- a/netx/net/sourceforge/jnlp/resources/Messages.properties +++ b/netx/net/sourceforge/jnlp/resources/Messages.properties @@ -135,6 +135,13 @@ RShutdown=This exception to prevent shutdown of JVM, but the process has been te RExitTaken=Exit class already set and caller is not exit class.
RCantReplaceSM=Changing the SecurityManager is not allowed.
RCantCreateFile=Cant create file {0}
+RCantDeleteFile=Cant delete file {0}
+RRemoveRPermFailed=Removing read permission on file {0} failed
+RRemoveWPermFailed=Removing write permissions on file {0} failed
+RRemoveXPermFailed=Removing execute permissions on file {0} failed
+RGetRPermFailed=Acquiring read permissions on file {0} failed
+RGetWPermFailed=Acquiring write permissions on file {0} failed
+RGetXPermFailed=Acquiring execute permissions on file {0} failed
RCantCreateDir=Cant create directory {0}
RCantRename=Cant rename {0} to {0}
RDenyStopped=Stopped applications have no permissions.
diff --git a/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java b/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java index b5acb17..7aeea90 100644 --- a/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java +++ b/netx/net/sourceforge/jnlp/runtime/JNLPRuntime.java @@ -277,10 +277,10 @@ public class JNLPRuntime { try { File errFile = new File(logDir, JNLPRuntime.STDERR_FILE); - errFile.getParentFile().mkdirs(); + FileUtils.createParentDir(errFile); FileUtils.createRestrictedFile(errFile, true); File outFile = new File(logDir, JNLPRuntime.STDOUT_FILE); - outFile.getParentFile().mkdirs(); + FileUtils.createParentDir(outFile); FileUtils.createRestrictedFile(outFile, true); if (redirectStreams) { diff --git a/netx/net/sourceforge/jnlp/services/XPersistenceService.java b/netx/net/sourceforge/jnlp/services/XPersistenceService.java index be98d12..4da8336 100644 --- a/netx/net/sourceforge/jnlp/services/XPersistenceService.java +++ b/netx/net/sourceforge/jnlp/services/XPersistenceService.java @@ -93,7 +93,8 @@ class XPersistenceService implements PersistenceService { checkLocation(location); File file = toCacheFile(location); - file.getParentFile().mkdirs(); + FileUtils.createParentDir(file, "Persistence store for " + + location.toString()); if (file.exists()) throw new IOException("File already exists."); @@ -110,7 +111,7 @@ class XPersistenceService implements PersistenceService { public void delete(URL location) throws MalformedURLException, IOException { checkLocation(location); - toCacheFile(location).delete(); + FileUtils.deleteWithErrMesg(toCacheFile(location), " tocache"); } /** @@ -124,7 +125,8 @@ class XPersistenceService implements PersistenceService { if (!file.exists()) throw new FileNotFoundException("Persistence store for " + location.toString() + " is not found."); - file.getParentFile().mkdirs(); + FileUtils.createParentDir(file, "Persistence store for " + + location.toString()); return (FileContents) ServiceUtil.createPrivilegedProxy(FileContents.class, new XFileContents(file)); } diff --git a/netx/net/sourceforge/jnlp/util/FileUtils.java b/netx/net/sourceforge/jnlp/util/FileUtils.java index d8e33d4..a682fa2 100644 --- a/netx/net/sourceforge/jnlp/util/FileUtils.java +++ b/netx/net/sourceforge/jnlp/util/FileUtils.java @@ -96,6 +96,59 @@ public final class FileUtils { } /** + * Tries to create the ancestor directories of file f. Throws + * an IOException if it can't be created (but not if it was + * already there). + * @param f + * @param eMsg - the message to use for the exception. null + * if the file name is to be used. + * @throws IOException if the directory can't be created and doesn't exist. + */ + public static void createParentDir(File f, String eMsg) throws IOException { + File parent = f.getParentFile(); + if (!parent.isDirectory() && !parent.mkdirs()) { + throw new IOException(R("RCantCreateDir", + eMsg == null ? parent : eMsg)); + } + } + + /** + * Tries to create the ancestor directories of file f. Throws + * an IOException if it can't be created (but not if it was + * already there). + * @param f + * @throws IOException if the directory can't be created and doesn't exist. + */ + public static void createParentDir(File f) throws IOException { + createParentDir(f, null); + } + + /** + * Tries to delete file f. If the file exists but couldn't be deleted, + * print an error message to stderr with the file name, or eMsg if eMsg + * is not null. + * @param f the file to be deleted + * @param eMsg the message to print on failure (or null to print the + * the file name). + */ + public static void deleteWithErrMesg(File f, String eMsg) { + if (f.exists()) { + if (!f.delete()) { + System.err.println(R("RCantDeleteFile", eMsg == null ? f : eMsg)); + } + } + } + + /** + * Tries to delete file f. If the file exists but couldn't be deleted, + * print an error message to stderr with the file name. + * @param f the file to be deleted + */ + public static void deleteWithErrMesg(File f) { + deleteWithErrMesg(f, null); + } + + /** * Creates a new file or directory with minimum permissions. The file is not * readable or writable by anyone other than the owner. If writeableByOnwer * is false, even the owner can not write to it. If isDir is true, then the @@ -120,21 +173,29 @@ public final class FileUtils { } // remove all permissions - tempFile.setExecutable(false, false); - tempFile.setReadable(false, false); - tempFile.setWritable(false, false); + if (!tempFile.setExecutable(false, false)) { + throw new IOException(R("RRemoveXPermFailed", tempFile)); + } + if (!tempFile.setReadable(false, false)) { + throw new IOException(R("RRemoveRPermFailed", tempFile)); + } + if (!tempFile.setWritable(false, false)) { + throw new IOException(R("RRemoveWPermFailed", tempFile)); + } // allow owner to read - tempFile.setReadable(true, true); + if (!tempFile.setReadable(true, true)) { + throw new IOException(R("RGetRPermFailed", tempFile)); + } // allow owner to write - if (writableByOwner) { - tempFile.setWritable(true, true); + if (writableByOwner && !tempFile.setWritable(true, true)) { + throw new IOException(R("RGetWPermFailed", tempFile)); } // allow owner to enter directories - if (isDir) { - tempFile.setExecutable(true, true); + if (isDir && !tempFile.setExecutable(true, true)) { + throw new IOException(R("RGetXPermFailed", tempFile)); } // rename this file. Unless the file is moved/renamed, any program that |