aboutsummaryrefslogtreecommitdiffhomepage
diff options
context:
space:
mode:
authorGravatar Colin Cross <ccross@google.com>2017-02-23 01:44:09 +0000
committerGravatar Yue Gan <yueg@google.com>2017-02-23 11:32:31 +0000
commitc9333b4996679c7f7120ff6dad087790b34908f6 (patch)
treef942760b4c3744f58cc5a1c1951287475ab574b1
parent414d08c8c7b72d749fbf6a861c75f547f64fe54d (diff)
roll-forward of "Delete temporary directory on exit"
*** Reason for rollback *** Bazel build is fixed by making fields referenced from inner class final. Clean reapply of commit 9a05f3bc58f9845bf41f9f8031e97b09e5348855 with "Path directory" arguments changed to "final Path directory". *** Original change description *** Revert "Delete temporary directory on exit" This reverts commit 9a05f3bc58f9845bf41f9f8031e97b09e5348855. This change breaks Bazel: http://ci.bazel.io/job/bazel-tests/555/BAZEL_VERSION=HEAD,PLATFORM_NAME=windows-x86_64/console Fixes #2552 -- PiperOrigin-RevId: 148293996 MOS_MIGRATED_REVID=148293996
-rw-r--r--src/tools/android/java/com/google/devtools/build/android/desugar/Desugar.java43
1 files changed, 43 insertions, 0 deletions
diff --git a/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar.java b/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar.java
index 9fdd82988e..2d973de959 100644
--- a/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar.java
+++ b/src/tools/android/java/com/google/devtools/build/android/desugar/Desugar.java
@@ -27,9 +27,12 @@ import com.google.devtools.common.options.OptionsParser;
import java.io.BufferedOutputStream;
import java.io.IOException;
import java.io.InputStream;
+import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
+import java.nio.file.SimpleFileVisitor;
+import java.nio.file.attribute.BasicFileAttributes;
import java.util.Enumeration;
import java.util.List;
import java.util.Map;
@@ -124,6 +127,8 @@ class Desugar {
System.setProperty(
LambdaClassMaker.LAMBDA_METAFACTORY_DUMPER_PROPERTY, dumpDirectory.toString());
+ deleteTreeOnExit(dumpDirectory);
+
if (args.length == 1 && args[0].startsWith("@")) {
args = Files.readAllLines(Paths.get(args[0].substring(1)), ISO_8859_1).toArray(new String[0]);
}
@@ -288,4 +293,42 @@ class Desugar {
throw new ClassNotFoundException();
}
}
+
+ private static void deleteTreeOnExit(final Path directory) {
+ Thread shutdownHook =
+ new Thread() {
+ @Override
+ public void run() {
+ try {
+ deleteTree(directory);
+ } catch (IOException e) {
+ throw new RuntimeException("Failed to delete " + directory, e);
+ }
+ }
+ };
+ Runtime.getRuntime().addShutdownHook(shutdownHook);
+ }
+
+ /** Recursively delete a directory. */
+ private static void deleteTree(final Path directory) throws IOException {
+ if (directory.toFile().exists()) {
+ Files.walkFileTree(
+ directory,
+ new SimpleFileVisitor<Path>() {
+ @Override
+ public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
+ throws IOException {
+ Files.delete(file);
+ return FileVisitResult.CONTINUE;
+ }
+
+ @Override
+ public FileVisitResult postVisitDirectory(Path dir, IOException exc)
+ throws IOException {
+ Files.delete(dir);
+ return FileVisitResult.CONTINUE;
+ }
+ });
+ }
+ }
}