|
|
@@ -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; |