Skip to content

Commit

Permalink
Delete datalog tempdir on exit
Browse files Browse the repository at this point in the history
  • Loading branch information
Dmitry Ivanov committed Aug 14, 2024
1 parent 28ead79 commit 6161400
Showing 1 changed file with 33 additions and 3 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -30,9 +30,8 @@
import org.sosy_lab.common.configuration.InvalidConfigurationException;

import java.io.*;
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.nio.file.*;
import java.nio.file.attribute.BasicFileAttributes;
import java.util.*;
import java.util.function.Predicate;
import java.util.stream.Stream;
Expand Down Expand Up @@ -477,8 +476,39 @@ private void writeAnalysis(String name, Writer may, Writer must) {
}
}

private void deleteOnExit(Path path) {
Runtime.getRuntime().addShutdownHook(new java.lang.Thread(){
@Override
public void run() {
if (!Files.exists(path)) {
return;
}
try {
Files.walkFileTree(path, new SimpleFileVisitor<>() {
@Override
public FileVisitResult postVisitDirectory(Path dir, IOException exc)
throws IOException {
Files.deleteIfExists(dir);
return super.postVisitDirectory(dir, exc);
}

@Override
public FileVisitResult visitFile(Path file, BasicFileAttributes attrs)
throws IOException {
Files.deleteIfExists(file);
return super.visitFile(file, attrs);
}
});
} catch (IOException e) {
throw new RuntimeException(e);
}
}
});
}

private Path writeDatalog(StringBuilder datalogProgram) throws IOException {
tempDir = Files.createTempDirectory("datalog");
deleteOnExit(tempDir);
String tempDirPath = tempDir.toString();
logger.info("using temp dir: " + tempDir);
try (InputStream libIn = getClass().getResourceAsStream("/" + LIB_NAME)) {
Expand Down

0 comments on commit 6161400

Please sign in to comment.