aboutsummaryrefslogtreecommitdiffstats
path: root/sonar-application
diff options
context:
space:
mode:
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-02-04 17:21:40 +0100
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>2016-02-05 17:02:10 +0100
commit035f7bdfe2b9bf7992ccd4fed456de98d007b305 (patch)
tree61300c3e7d11c30c32884b50853278f6320fc34e /sonar-application
parent4741428f2476010bfbdb36650f4f5de194410c16 (diff)
downloadsonarqube-035f7bdfe2b9bf7992ccd4fed456de98d007b305.tar.gz
sonarqube-035f7bdfe2b9bf7992ccd4fed456de98d007b305.zip
SONAR-7154 never delete temp dir, only delete its content if exists
Diffstat (limited to 'sonar-application')
-rw-r--r--sonar-application/src/main/java/org/sonar/application/AppFileSystem.java17
-rw-r--r--sonar-application/src/test/java/org/sonar/application/AppFileSystemTest.java16
2 files changed, 23 insertions, 10 deletions
diff --git a/sonar-application/src/main/java/org/sonar/application/AppFileSystem.java b/sonar-application/src/main/java/org/sonar/application/AppFileSystem.java
index 32f563737b2..b71ba43faae 100644
--- a/sonar-application/src/main/java/org/sonar/application/AppFileSystem.java
+++ b/sonar-application/src/main/java/org/sonar/application/AppFileSystem.java
@@ -26,7 +26,7 @@ import org.slf4j.LoggerFactory;
import org.sonar.process.Props;
import org.sonar.process.monitor.FileSystem;
-import static org.apache.commons.io.FileUtils.deleteQuietly;
+import static org.apache.commons.io.FileUtils.cleanDirectory;
import static org.apache.commons.io.FileUtils.forceMkdir;
import static org.sonar.process.ProcessProperties.PATH_DATA;
import static org.sonar.process.ProcessProperties.PATH_HOME;
@@ -67,9 +67,9 @@ public class AppFileSystem implements FileSystem {
if (!initialized) {
throw new IllegalStateException("method verifyProps must be called first");
}
- ensureDirectoryExists(props, PATH_DATA);
- ensureDirectoryExists(props, PATH_WEB);
- ensureDirectoryExists(props, PATH_LOGS);
+ createDirectory(props, PATH_DATA);
+ createDirectory(props, PATH_WEB);
+ createDirectory(props, PATH_LOGS);
createOrCleanDirectory(props, PATH_TEMP);
}
@@ -89,7 +89,7 @@ public class AppFileSystem implements FileSystem {
return d;
}
- private static boolean ensureDirectoryExists(Props props, String propKey) throws IOException {
+ private static boolean createDirectory(Props props, String propKey) throws IOException {
File dir = props.nonNullValueAsFile(propKey);
if (dir.exists()) {
ensureIsNotAFile(propKey, dir);
@@ -111,10 +111,9 @@ public class AppFileSystem implements FileSystem {
private static void createOrCleanDirectory(Props props, String propKey) throws IOException {
File dir = props.nonNullValueAsFile(propKey);
- LOG.info("Deleting and/or creating temp directory {}", dir.getAbsolutePath());
- if (!ensureDirectoryExists(props, propKey)) {
- deleteQuietly(dir);
- forceMkdir(dir);
+ LOG.info("Cleaning and/or creating temp directory {}", dir.getAbsolutePath());
+ if (!createDirectory(props, propKey)) {
+ cleanDirectory(dir);
}
}
}
diff --git a/sonar-application/src/test/java/org/sonar/application/AppFileSystemTest.java b/sonar-application/src/test/java/org/sonar/application/AppFileSystemTest.java
index baac93fa203..62c9725d68d 100644
--- a/sonar-application/src/test/java/org/sonar/application/AppFileSystemTest.java
+++ b/sonar-application/src/test/java/org/sonar/application/AppFileSystemTest.java
@@ -21,7 +21,12 @@ package org.sonar.application;
import java.io.File;
import java.io.IOException;
+import java.nio.file.Files;
+import java.nio.file.Path;
+import java.nio.file.Paths;
+import java.nio.file.attribute.BasicFileAttributes;
import java.util.Properties;
+import javax.annotation.CheckForNull;
import org.junit.Before;
import org.junit.Rule;
import org.junit.Test;
@@ -135,9 +140,10 @@ public class AppFileSystemTest {
}
@Test
- public void reset_delete_temp_dir_if_already_exists() throws Exception {
+ public void reset_deletes_content_of_temp_dir_but_not_temp_dir_itself_if_it_already_exists() throws Exception {
File tempDir = new File(homeDir, DEFAULT_TEMP_DIR_NAME);
assertThat(tempDir.mkdir()).isTrue();
+ Object tempDirKey = getFileKey(tempDir);
File fileInTempDir = new File(tempDir, "someFile.txt");
assertThat(fileInTempDir.createNewFile()).isTrue();
@@ -147,6 +153,14 @@ public class AppFileSystemTest {
assertThat(tempDir).exists();
assertThat(fileInTempDir).doesNotExist();
+ assertThat(getFileKey(tempDir)).isEqualTo(tempDirKey);
+ }
+
+ @CheckForNull
+ private static Object getFileKey(File fileInTempDir) throws IOException {
+ Path path = Paths.get(fileInTempDir.toURI());
+ BasicFileAttributes attrs = Files.readAttributes(path, BasicFileAttributes.class);
+ return attrs.fileKey();
}
@Test