Issue #5480 - Safer temp directory management
Signed-off-by: Joakim Erdfelt <joakim.erdfelt@gmail.com>
This commit is contained in:
parent
6be6fae291
commit
c7ae5b50ab
|
@ -354,8 +354,10 @@ public class WebInfConfiguration extends AbstractConfiguration
|
||||||
@Override
|
@Override
|
||||||
public void deconfigure(WebAppContext context) throws Exception
|
public void deconfigure(WebAppContext context) throws Exception
|
||||||
{
|
{
|
||||||
//if we're not persisting the temp dir contents delete it
|
File tempDirectory = context.getTempDirectory();
|
||||||
if (!context.isPersistTempDirectory() && context.getTempDirectory() != null && context.getTempDirectory().exists())
|
|
||||||
|
// if we're not persisting the temp dir contents delete it
|
||||||
|
if (!context.isPersistTempDirectory() && tempDirectory != null && tempDirectory.exists())
|
||||||
{
|
{
|
||||||
IO.delete(context.getTempDirectory());
|
IO.delete(context.getTempDirectory());
|
||||||
}
|
}
|
||||||
|
@ -504,13 +506,15 @@ public class WebInfConfiguration extends AbstractConfiguration
|
||||||
//if it is to be persisted, make sure it will be the same name
|
//if it is to be persisted, make sure it will be the same name
|
||||||
//by not using File.createTempFile, which appends random digits
|
//by not using File.createTempFile, which appends random digits
|
||||||
tmpDir = new File(parent, temp);
|
tmpDir = new File(parent, temp);
|
||||||
|
configureTempDirectory(tmpDir, context);
|
||||||
}
|
}
|
||||||
else
|
else
|
||||||
{
|
{
|
||||||
//ensure file will always be unique by appending random digits
|
// ensure dir will always be unique by having classlib generate random path name
|
||||||
tmpDir = Files.createTempDirectory(parent.toPath(), temp).toFile();
|
tmpDir = Files.createTempDirectory(parent.toPath(), temp).toFile();
|
||||||
|
tmpDir.deleteOnExit();
|
||||||
|
ensureTempDirUsable(tmpDir);
|
||||||
}
|
}
|
||||||
configureTempDirectory(tmpDir, context);
|
|
||||||
|
|
||||||
if (LOG.isDebugEnabled())
|
if (LOG.isDebugEnabled())
|
||||||
LOG.debug("Set temp dir " + tmpDir);
|
LOG.debug("Set temp dir " + tmpDir);
|
||||||
|
@ -522,6 +526,12 @@ public class WebInfConfiguration extends AbstractConfiguration
|
||||||
if (dir == null)
|
if (dir == null)
|
||||||
throw new IllegalArgumentException("Null temp dir");
|
throw new IllegalArgumentException("Null temp dir");
|
||||||
|
|
||||||
|
// if dir exists and we don't want it persisted, delete it
|
||||||
|
if (!context.isPersistTempDirectory() && dir.exists() && !IO.delete(dir))
|
||||||
|
{
|
||||||
|
throw new IllegalStateException("Failed to delete temp dir " + dir);
|
||||||
|
}
|
||||||
|
|
||||||
// if it doesn't exist make it
|
// if it doesn't exist make it
|
||||||
if (!dir.exists())
|
if (!dir.exists())
|
||||||
{
|
{
|
||||||
|
@ -531,6 +541,14 @@ public class WebInfConfiguration extends AbstractConfiguration
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
if (!context.isPersistTempDirectory())
|
||||||
|
dir.deleteOnExit();
|
||||||
|
|
||||||
|
ensureTempDirUsable(dir);
|
||||||
|
}
|
||||||
|
|
||||||
|
private void ensureTempDirUsable(File dir)
|
||||||
|
{
|
||||||
// is it useable
|
// is it useable
|
||||||
if (!dir.canWrite() || !dir.isDirectory())
|
if (!dir.canWrite() || !dir.isDirectory())
|
||||||
throw new IllegalStateException("Temp dir " + dir + " not useable: writeable=" + dir.canWrite() + ", dir=" + dir.isDirectory());
|
throw new IllegalStateException("Temp dir " + dir + " not useable: writeable=" + dir.canWrite() + ", dir=" + dir.isDirectory());
|
||||||
|
|
Loading…
Reference in New Issue