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;
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);
}
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);
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);
}
}
}
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;
}
@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();
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