aboutsummaryrefslogtreecommitdiffstats
path: root/src
diff options
context:
space:
mode:
authorJulien HENRY <julien.henry@sonarsource.com>2013-01-04 09:22:26 +0100
committerJulien HENRY <julien.henry@sonarsource.com>2013-01-04 09:22:26 +0100
commit2d09cba4cac8802e4858144ca63c79039cd07cff (patch)
treed3a88027e44d6079ba633f2f73d7dae9a87da1fe /src
parentbbea8ed02e8503360ac4d1808ec7fe700e5267ec (diff)
downloadsonar-scanner-cli-2d09cba4cac8802e4858144ca63c79039cd07cff.tar.gz
sonar-scanner-cli-2d09cba4cac8802e4858144ca63c79039cd07cff.zip
SONARPLUGINS-1233 Delete the Sonar working/temporary directory before starting analysis
Duplicate code from commons-io as we don't want dependency in Sonar Runner.
Diffstat (limited to 'src')
-rw-r--r--src/main/java/org/sonar/runner/Runner.java87
1 files changed, 84 insertions, 3 deletions
diff --git a/src/main/java/org/sonar/runner/Runner.java b/src/main/java/org/sonar/runner/Runner.java
index 964412e..865d7a9 100644
--- a/src/main/java/org/sonar/runner/Runner.java
+++ b/src/main/java/org/sonar/runner/Runner.java
@@ -19,9 +19,9 @@
*/
package org.sonar.runner;
-import org.apache.commons.io.FileUtils;
-
import java.io.File;
+import java.io.FileNotFoundException;
+import java.io.IOException;
import java.lang.reflect.Constructor;
import java.lang.reflect.InvocationTargetException;
import java.lang.reflect.Method;
@@ -177,10 +177,91 @@ public final class Runner {
else {
workDir = defineCustomizedWorkDir(new File(customWorkDir));
}
- FileUtils.deleteQuietly(workDir);
+ deleteQuietly(workDir);
return workDir;
}
+ /**
+ * Duplicated from Commons IO because we don't want dependencies in Sonar Runner
+ */
+ private static boolean deleteQuietly(File file) {
+ if (file == null) {
+ return false;
+ }
+ try {
+ if (file.isDirectory()) {
+ cleanDirectory(file);
+ }
+ } catch (Exception ignored) {
+ }
+
+ try {
+ return file.delete();
+ } catch (Exception ignored) {
+ return false;
+ }
+ }
+
+ private static void cleanDirectory(File directory) throws IOException {
+ if (!directory.exists()) {
+ String message = directory + " does not exist";
+ throw new IllegalArgumentException(message);
+ }
+
+ if (!directory.isDirectory()) {
+ String message = directory + " is not a directory";
+ throw new IllegalArgumentException(message);
+ }
+
+ File[] files = directory.listFiles();
+ if (files == null) { // null if security restricted
+ throw new IOException("Failed to list contents of " + directory);
+ }
+
+ IOException exception = null;
+ for (File file : files) {
+ try {
+ forceDelete(file);
+ } catch (IOException ioe) {
+ exception = ioe;
+ }
+ }
+
+ if (null != exception) {
+ throw exception;
+ }
+ }
+
+ private static void forceDelete(File file) throws IOException {
+ if (file.isDirectory()) {
+ deleteDirectory(file);
+ } else {
+ boolean filePresent = file.exists();
+ if (!file.delete()) {
+ if (!filePresent) {
+ throw new FileNotFoundException("File does not exist: " + file);
+ }
+ String message =
+ "Unable to delete file: " + file;
+ throw new IOException(message);
+ }
+ }
+ }
+
+ private static void deleteDirectory(File directory) throws IOException {
+ if (!directory.exists()) {
+ return;
+ }
+
+ cleanDirectory(directory);
+
+ if (!directory.delete()) {
+ String message =
+ "Unable to delete directory " + directory + ".";
+ throw new IOException(message);
+ }
+ }
+
private File defineCustomizedWorkDir(File customWorkDir) {
if (customWorkDir.isAbsolute()) {
return customWorkDir;