Skip to content

Commit

Permalink
Delete temporary directory after exit
Browse files Browse the repository at this point in the history
  • Loading branch information
MarcMil authored and mickaelistria committed Apr 12, 2021
1 parent 50b680f commit 5feb937
Showing 1 changed file with 30 additions and 0 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,11 @@
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.nio.file.FileVisitResult;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.SimpleFileVisitor;
import java.nio.file.attribute.BasicFileAttributes;

import org.codehaus.plexus.component.annotations.Component;
import org.codehaus.plexus.component.annotations.Requirement;
Expand Down Expand Up @@ -193,6 +198,31 @@ private String copyToTempFolder(File configDir) throws IOException {
if (!(equinoxTmp.delete() && equinoxTmp.mkdirs())) {
throw new IOException("Could not create temp dir " + equinoxTmp);
}

final Path directory = equinoxTmp.toPath();
Runtime.getRuntime().addShutdownHook(new Thread(new Runnable() {
@Override
public void run() {
try {
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;
}
});
} catch (IOException e) {
getLogger().warn("Could not delete temporary directory " + directory, e);
}
}
}));

File tempConfigDir = new File(equinoxTmp, "config");
if (!(tempConfigDir.mkdirs())) {
throw new IOException("Could not create temp config dir " + tempConfigDir);
Expand Down

0 comments on commit 5feb937

Please sign in to comment.