From 2d09cba4cac8802e4858144ca63c79039cd07cff Mon Sep 17 00:00:00 2001 From: Julien HENRY Date: Fri, 4 Jan 2013 09:22:26 +0100 Subject: [PATCH] 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. --- src/main/java/org/sonar/runner/Runner.java | 87 +++++++++++++++++++++- 1 file 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; -- 2.39.5