Skip to content

Commit

Permalink
fix: use directory stream for temp dir cleanup
Browse files Browse the repository at this point in the history
Closes: #198
  • Loading branch information
gotson committed Aug 24, 2022
1 parent 3fb1a21 commit 5387c7a
Showing 1 changed file with 27 additions and 24 deletions.
51 changes: 27 additions & 24 deletions src/main/java/org/sqlite/SQLiteJDBCLoader.java
Original file line number Diff line number Diff line change
Expand Up @@ -35,6 +35,7 @@
import java.util.List;
import java.util.Properties;
import java.util.UUID;
import java.util.stream.Stream;
import org.sqlite.util.OSInfo;
import org.sqlite.util.StringUtils;

Expand All @@ -51,6 +52,7 @@
*/
public class SQLiteJDBCLoader {

private static final String LOCK_EXT = ".lck";
private static boolean extracted = false;

/**
Expand All @@ -76,29 +78,30 @@ private static File getTempDir() {
* Deleted old native libraries e.g. on Windows the DLL file is not removed on VM-Exit (bug #80)
*/
static void cleanup() {
String tempFolder = getTempDir().getAbsolutePath();
File dir = new File(tempFolder);

File[] nativeLibFiles =
dir.listFiles(
new FilenameFilter() {
private final String searchPattern = "sqlite-" + getVersion();

public boolean accept(File dir, String name) {
return name.startsWith(searchPattern) && !name.endsWith(".lck");
}
});
if (nativeLibFiles != null) {
for (File nativeLibFile : nativeLibFiles) {
File lckFile = new File(nativeLibFile.getAbsolutePath() + ".lck");
if (!lckFile.exists()) {
try {
nativeLibFile.delete();
} catch (SecurityException e) {
System.err.println("Failed to delete old native lib" + e.getMessage());
}
}
}
String searchPattern = "sqlite-" + getVersion();

try (Stream<Path> dirList = Files.list(getTempDir().toPath())) {
dirList.filter(
path ->
!path.getFileName().toString().endsWith(LOCK_EXT)
&& path.getFileName()
.toString()
.startsWith(searchPattern))
.forEach(
nativeLib -> {
Path lckFile = Paths.get(nativeLib + LOCK_EXT);
if (Files.notExists(lckFile)) {
try {
Files.delete(nativeLib);
} catch (Exception e) {
System.err.println(
"Failed to delete old native lib: "
+ e.getMessage());
}
}
});
} catch (IOException e) {
System.err.println("Failed to open directory: " + e.getMessage());
}
}

Expand Down Expand Up @@ -195,7 +198,7 @@ private static boolean extractAndLoadLibraryFile(
String uuid = UUID.randomUUID().toString();
String extractedLibFileName =
String.format("sqlite-%s-%s-%s", getVersion(), uuid, libraryFileName);
String extractedLckFileName = extractedLibFileName + ".lck";
String extractedLckFileName = extractedLibFileName + LOCK_EXT;

Path extractedLibFile = Paths.get(targetFolder, extractedLibFileName);
Path extractedLckFile = Paths.get(targetFolder, extractedLckFileName);
Expand Down

0 comments on commit 5387c7a

Please sign in to comment.