diff options
author | Simon Brandhof <simon.brandhof@gmail.com> | 2013-02-11 15:23:47 +0100 |
---|---|---|
committer | Simon Brandhof <simon.brandhof@gmail.com> | 2013-02-11 15:24:01 +0100 |
commit | 7c10d83bf2d14506f108621c1024c78f2fb4c2f9 (patch) | |
tree | d6d56d67741d64420d8ff481e5df10fa9944eca4 /sonar-home/src | |
parent | 4cf1785b3790ca8c31973cea68662a0192b5230a (diff) | |
download | sonarqube-7c10d83bf2d14506f108621c1024c78f2fb4c2f9.tar.gz sonarqube-7c10d83bf2d14506f108621c1024c78f2fb4c2f9.zip |
SONAR-2291 fix instability regression when temp dir is not in the same drive partition than cache
Diffstat (limited to 'sonar-home/src')
-rw-r--r-- | sonar-home/src/main/java/org/sonar/home/cache/FileCache.java | 59 |
1 files changed, 40 insertions, 19 deletions
diff --git a/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java b/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java index 5e57d82a4a0..1fd9fef7ba3 100644 --- a/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java +++ b/sonar-home/src/main/java/org/sonar/home/cache/FileCache.java @@ -26,6 +26,7 @@ import javax.annotation.CheckForNull; import java.io.File; import java.io.IOException; +import java.util.Random; /** * This class is responsible for managing Sonar batch file cache. You can put file into cache and @@ -34,23 +35,18 @@ import java.io.IOException; */ public class FileCache { - private final File dir; + private static final int TEMP_FILE_ATTEMPTS = 1000; + + private final File dir, tmpDir; private final FileHashes hashes; private final Log log; FileCache(File dir, Log log, FileHashes fileHashes) { - this.dir = dir; this.hashes = fileHashes; this.log = log; - if (!dir.exists()) { - log.debug(String.format("Create cache directory: %s", dir.getAbsolutePath())); - try { - FileUtils.forceMkdir(dir); - } catch (IOException e) { - throw new IllegalStateException("Unable to create cache directory " + dir.getAbsolutePath(), e); - } - } + this.dir = createDir(dir, log, "user cache"); log.info(String.format("User cache: %s", dir.getAbsolutePath())); + this.tmpDir = createDir(new File(dir, "_tmp"), log, "temp dir"); } public static FileCache create(File dir, Log log) { @@ -84,7 +80,7 @@ public class FileCache { File hashDir = hashDir(hash); File targetFile = new File(hashDir, filename); if (!targetFile.exists()) { - File tempFile = newTempFile(filename); + File tempFile = newTempFile(); download(downloader, filename, tempFile); String downloadedHash = hashes.of(tempFile); if (!hash.equals(downloadedHash)) { @@ -105,14 +101,6 @@ public class FileCache { } } - private File newTempFile(String filename) { - try { - return File.createTempFile(filename, ".tmp"); - } catch (IOException e) { - throw new IllegalStateException("Fail to create temp file", e); - } - } - private void renameQuietly(File sourceFile, File targetFile) { boolean rename = sourceFile.renameTo(targetFile); if (!rename) { @@ -140,4 +128,37 @@ public class FileCache { throw new IllegalStateException("Fail to create cache directory: " + hashDir, e); } } + + private File newTempFile() { + String baseName = System.currentTimeMillis() + "-"; + Random random = new Random(); + for (int counter = 0; counter < TEMP_FILE_ATTEMPTS; counter++) { + try { + String filename = baseName + random.nextInt(1000); + File tempFile = new File(tmpDir, filename); + if (tempFile.createNewFile()) { + return tempFile; + } + } catch (IOException e) { + // ignore except the last try + if (counter == TEMP_FILE_ATTEMPTS - 1) { + throw new IllegalStateException(); + } + } + } + throw new IllegalStateException("Fail to create temporary file in " + tmpDir); + } + + private File createDir(File dir, Log log, String debugTitle) { + if (!dir.isDirectory() || !dir.exists()) { + log.debug("Create : " + dir.getAbsolutePath()); + try { + FileUtils.forceMkdir(dir); + } catch (IOException e) { + throw new IllegalStateException("Unable to create " + debugTitle + dir.getAbsolutePath(), e); + } + } + return dir; + } + } |