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
|
||||
public void deconfigure(WebAppContext context) throws Exception
|
||||
{
|
||||
//if we're not persisting the temp dir contents delete it
|
||||
if (!context.isPersistTempDirectory() && context.getTempDirectory() != null && context.getTempDirectory().exists())
|
||||
File tempDirectory = context.getTempDirectory();
|
||||
|
||||
// if we're not persisting the temp dir contents delete it
|
||||
if (!context.isPersistTempDirectory() && tempDirectory != null && tempDirectory.exists())
|
||||
{
|
||||
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
|
||||
//by not using File.createTempFile, which appends random digits
|
||||
tmpDir = new File(parent, temp);
|
||||
configureTempDirectory(tmpDir, context);
|
||||
}
|
||||
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.deleteOnExit();
|
||||
ensureTempDirUsable(tmpDir);
|
||||
}
|
||||
configureTempDirectory(tmpDir, context);
|
||||
|
||||
if (LOG.isDebugEnabled())
|
||||
LOG.debug("Set temp dir " + tmpDir);
|
||||
|
@ -522,6 +526,12 @@ public class WebInfConfiguration extends AbstractConfiguration
|
|||
if (dir == null)
|
||||
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 (!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
|
||||
if (!dir.canWrite() || !dir.isDirectory())
|
||||
throw new IllegalStateException("Temp dir " + dir + " not useable: writeable=" + dir.canWrite() + ", dir=" + dir.isDirectory());
|
||||
|
|
Loading…
Reference in New Issue