]> source.dussan.org Git - sonarqube.git/commitdiff
SONAR-7154 never delete temp dir, only delete its content if exists 770/head
authorSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Thu, 4 Feb 2016 16:21:40 +0000 (17:21 +0100)
committerSébastien Lesaint <sebastien.lesaint@sonarsource.com>
Fri, 5 Feb 2016 16:02:10 +0000 (17:02 +0100)
sonar-application/src/main/java/org/sonar/application/AppFileSystem.java
sonar-application/src/test/java/org/sonar/application/AppFileSystemTest.java

index 32f563737b2ac7b8ceaa380c23e95f1acfbce28d..b71ba43faae460bfb56af79140ae58e60ec78136 100644 (file)
@@ -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);
     }
   }
 }
index baac93fa20326ecc8c5da91d2cf048b57b5e1531..62c9725d68de1e0a6e79c9d89500553aad02fa2e 100644 (file)
@@ -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