diff options
author | Sven Gothel <[email protected]> | 2011-11-29 08:15:11 +0100 |
---|---|---|
committer | Sven Gothel <[email protected]> | 2011-11-29 08:15:11 +0100 |
commit | 1cdbadd0d2ad5509d970bed2fc388327ec2c4521 (patch) | |
tree | 09dd4c3821fd187df8659e35e684287238ae754c /src/java/com/jogamp/common/util/IOUtil.java | |
parent | 9596ef22ae2e8ed0622697afdd3e7a1a1c2934ec (diff) |
Fix TempJarCache's Multi-User Bug (Reported by Martin Hegedus)
It turns out that Java's File mkdir() only makes the directory writable for the current user,
I have missed this fact. Great catch.
1. Fix TempJarCache.isInitialized(): Return false if not successfully initialized.
It merely returned if it has passed 'initSingleton()' and ignored the staticInitError.
2. Fix TempFileCache pattern of determining the temp base directory
We cannot just use a static directory name, due to the multi user environment
and user write permissions on File.mkdir().
IOUtil has a new 'getTempDir(..)' methods, which iterates through integers [000000-999999]
until a writeable directory could be found or created.
TempFileCache initializes this temp base dir in the static block ensuring
the value is final for the JVM / ClassLoader.
Updated comments/docs in TempFileCache.
Diffstat (limited to 'src/java/com/jogamp/common/util/IOUtil.java')
-rw-r--r-- | src/java/com/jogamp/common/util/IOUtil.java | 53 |
1 files changed, 53 insertions, 0 deletions
diff --git a/src/java/com/jogamp/common/util/IOUtil.java b/src/java/com/jogamp/common/util/IOUtil.java index 1f0b2ed..1e81c8e 100644 --- a/src/java/com/jogamp/common/util/IOUtil.java +++ b/src/java/com/jogamp/common/util/IOUtil.java @@ -532,4 +532,57 @@ public class IOUtil { } return tmpRoot; } + + /** + * This methods finds [and creates] a temporary directory: + * <pre> + * for(tempBaseDir = tempRootDir + tmpDirPrefix + _ + [000000-999999]) { + * if(tempBaseDir.isDirectory()) { + * if(tempBaseDir.canWrite()) { + * return tempBaseDir; + * } + * } else { + * tempBaseDir.mkdir(); + * return tempBaseDir; + * } + * } + * </pre> + * The <code>tempRootDir</code> is retrieved by {@link #getTempRoot()}. + * <p> + * The iteration through [000000-999999] ensures that the code is multi-user save. + * </p> + * @param tmpDirPrefix + * @return a temporary directory, writable by this user + * @throws IOException + * @throws SecurityException + */ + public static File getTempDir(String tmpDirPrefix) + throws IOException, SecurityException + { + final File tempRoot = IOUtil.getTempRoot(); + + for(int i = 0; i<=999999; i++) { + final String tmpDirSuffix = String.format("_%06d", i); // 6 digits for iteration + final File tmpBaseDir = new File(tempRoot, tmpDirPrefix+tmpDirSuffix); + if (tmpBaseDir.isDirectory()) { + // existing directory + if(tmpBaseDir.canWrite()) { + // can write - OK + return tmpBaseDir; + } + // not writable, hence used by another user - continue + } else { + // non existing directory, create and validate it + tmpBaseDir.mkdir(); + if (!tmpBaseDir.isDirectory()) { + throw new IOException("Cannot create temp base directory " + tmpBaseDir); + } + if(!tmpBaseDir.canWrite()) { + throw new IOException("Cannot write to created temp base directory " + tmpBaseDir); + } + return tmpBaseDir; // created and writable - OK + } + } + throw new IOException("Could not create temp directory @ "+tempRoot.getAbsolutePath()+tmpDirPrefix+"_*"); + } } |