From 035f7bdfe2b9bf7992ccd4fed456de98d007b305 Mon Sep 17 00:00:00 2001 From: =?utf8?q?S=C3=A9bastien=20Lesaint?= Date: Thu, 4 Feb 2016 17:21:40 +0100 Subject: [PATCH] SONAR-7154 never delete temp dir, only delete its content if exists --- .../org/sonar/application/AppFileSystem.java | 17 ++++++++--------- .../sonar/application/AppFileSystemTest.java | 16 +++++++++++++++- 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 -- 2.39.5