From: simonbrandhof Date: Tue, 21 Dec 2010 12:19:33 +0000 (+0000) Subject: fix deletion of batch temp directories on MS Windows X-Git-Tag: 2.6~268 X-Git-Url: https://source.dussan.org/?a=commitdiff_plain;h=eb28861955f2823b4b2e92c4a59683d16d0b8dbb;p=sonarqube.git fix deletion of batch temp directories on MS Windows --- diff --git a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempDirectories.java b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempDirectories.java index 9e820ed40b2..80ab6940d41 100644 --- a/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempDirectories.java +++ b/sonar-batch/src/main/java/org/sonar/batch/bootstrap/TempDirectories.java @@ -21,22 +21,33 @@ package org.sonar.batch.bootstrap; import com.google.common.collect.Maps; import org.apache.commons.io.FileUtils; +import org.apache.commons.io.filefilter.AgeFileFilter; +import org.apache.commons.io.filefilter.AndFileFilter; +import org.apache.commons.io.filefilter.DirectoryFileFilter; +import org.apache.commons.io.filefilter.PrefixFileFilter; import org.apache.commons.lang.StringUtils; import org.slf4j.LoggerFactory; import org.sonar.api.utils.SonarException; import org.sonar.api.utils.TempFileUtils; import java.io.File; +import java.io.FileFilter; import java.io.IOException; +import java.util.Arrays; import java.util.Map; public final class TempDirectories { + public static final String DIR_PREFIX = "sonar-batch"; + + // this timeout must be greater than the longest analysis + public static final int AGE_BEFORE_DELETION = 24 * 60 * 60 * 1000; + private File rootDir; private Map directoriesByKey = Maps.newHashMap(); public TempDirectories() throws IOException { - this.rootDir = TempFileUtils.createTempDirectory("sonar-batch"); + this.rootDir = TempFileUtils.createTempDirectory(DIR_PREFIX); LoggerFactory.getLogger(getClass()).debug("Temporary directory: " + rootDir.getAbsolutePath()); } @@ -76,9 +87,18 @@ public final class TempDirectories { */ public void stop() { directoriesByKey.clear(); - LoggerFactory.getLogger(getClass()).debug("Delete temporary directory: " + rootDir.getAbsolutePath()); - // it probably does not work on MS Windows and Sun JVM. URLClassLoader locks JARs and resources. - FileUtils.deleteQuietly(rootDir); + LoggerFactory.getLogger(getClass()).debug("Delete temporary directories"); + + // Deleting temp directory does not work on MS Windows and Sun JVM because URLClassLoader locks JARs and resources. + // The workaround is that sonar deletes orphans itself. + rootDir.setLastModified(System.currentTimeMillis() - AGE_BEFORE_DELETION - 60 * 60 * 1000); // older than AGE_BEFORE_DELETION to be sure that the current dir is deleted on mac and linux. + + File[] directoriesToDelete = rootDir.getParentFile().listFiles((FileFilter) new AndFileFilter(Arrays.asList( + DirectoryFileFilter.DIRECTORY, new PrefixFileFilter(DIR_PREFIX), new AgeFileFilter(System.currentTimeMillis() - AGE_BEFORE_DELETION)))); + for (File dir : directoriesToDelete) { + LoggerFactory.getLogger(getClass()).debug("Delete temporary directory: " + dir); + FileUtils.deleteQuietly(dir); + } } }