aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-home/src/main
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2014-03-25 11:32:42 +0100
committerJulien HENRY <julien.henry@sonarsource.com>2014-03-25 11:33:22 +0100
commitc2bc9f0c49b4128399ae5146d33b1e612359618b (patch)
tree7750b598b07219a511e6611b517ae51cb6f5cf54 /sonar-home/src/main
parent74342ad049a98c9a2cf4621373bedee22736c612 (diff)
downloadsonarqube-c2bc9f0c49b4128399ae5146d33b1e612359618b.tar.gz
sonarqube-c2bc9f0c49b4128399ae5146d33b1e612359618b.zip
SONAR-5062 New implementation of concurrent safe unzip using FileLock
Diffstat (limited to 'sonar-home/src/main')
-rw-r--r--sonar-home/src/main/java/org/sonar/home/cache/FileCache.java24
1 files changed, 16 insertions, 8 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 a02eefc6d3e..37a81e17fcf 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
@@ -19,7 +19,6 @@
*/
package org.sonar.home.cache;
-import org.apache.commons.io.FileExistsException;
import org.apache.commons.io.FileUtils;
import org.sonar.api.utils.ZipUtils;
import org.sonar.home.log.Log;
@@ -27,6 +26,7 @@ import org.sonar.home.log.Log;
import javax.annotation.CheckForNull;
import java.io.File;
+import java.io.FileOutputStream;
import java.io.IOException;
import java.util.Random;
import java.util.zip.ZipEntry;
@@ -185,16 +185,24 @@ public class FileCache {
public File unzip(File cachedFile) throws IOException {
String filename = cachedFile.getName();
File destDir = new File(cachedFile.getParentFile(), filename + "_unzip");
+ File lockFile = new File(cachedFile.getParentFile(), filename + "_unzip.lock");
if (!destDir.exists()) {
- File tempDir = createTempDir();
- ZipUtils.unzip(cachedFile, tempDir, new LibFilter());
+ FileOutputStream out = new FileOutputStream(lockFile);
try {
- // Recheck in case of concurrent processes
- if (!destDir.exists()) {
- FileUtils.moveDirectory(tempDir, destDir);
+ java.nio.channels.FileLock lock = out.getChannel().lock();
+ try {
+ // Recheck in case of concurrent processes
+ if (!destDir.exists()) {
+ File tempDir = createTempDir();
+ ZipUtils.unzip(cachedFile, tempDir, new LibFilter());
+ FileUtils.moveDirectory(tempDir, destDir);
+ }
+ } finally {
+ lock.release();
}
- } catch (FileExistsException e) {
- // Ignore as is certainly means a concurrent process has unziped the same file
+ } finally {
+ out.close();
+ FileUtils.deleteQuietly(lockFile);
}
}
return destDir;