Skip to content

Ghidra is a software reverse engineering (SRE) framework

GT-3343 - File Browser - move cache cleanup to daemon thread

dragonmachere089733f9cba
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileCache.java b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileCache.java
index 8a1c334cb8..f958a24f11 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileCache.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileCache.java
@@ -63,6 +63,7 @@ public class FileCache {
private final File cacheDir;
private final File newDir;
private final File lastMaintFile;
+ private FileCacheMaintenanceDaemon cleanDaemon;
private int fileAddCount;
private int fileReUseCount;
@@ -159,11 +160,12 @@ public class FileCache {
*/
private void performCacheMaintIfNeeded() throws IOException {
lastMaintTS = (lastMaintTS == 0) ? lastMaintFile.lastModified() : lastMaintTS;
- if (lastMaintTS + MAINT_INTERVAL_MS < System.currentTimeMillis()) {
- performCacheMaint();
- lastMaintTS = System.currentTimeMillis();
- FileUtilities.writeStringToFile(lastMaintFile, "Last maint run at " + (new Date()));
+ if (lastMaintTS + MAINT_INTERVAL_MS > System.currentTimeMillis()) {
+ return;
}
+
+ cleanDaemon = new FileCacheMaintenanceDaemon();
+ cleanDaemon.start();
}
/**
@@ -220,7 +222,7 @@ public class FileCache {
* <p>
* The stream is copied into a temp file in the cacheDir/new directory while its md5
* is calculated. The temp file is then moved into its final location
- * based on the md5 of the stream: AA/BB/CC/AABBCCDDEEFF....
+ * based on the md5 of the stream: AA/BB/AABBCCDDEEFF....
* <p>
* The monitor progress is updated with the number of bytes that are being copied. No
* message or maximum is set.
@@ -416,4 +418,35 @@ public class FileCache {
public long getMaxFileAgeMS() {
return MAX_FILE_AGE_MS;
}
+
+ /**
+ * Returns true if the background thread has been created to clean old cache files and the
+ * thread is still working
+ * @return true if removing expired cache files
+ */
+ boolean isCleaning() {
+ return cleanDaemon != null && cleanDaemon.isAlive();
+ }
+
+ private class FileCacheMaintenanceDaemon extends Thread {
+
+ FileCacheMaintenanceDaemon() {
+ setDaemon(true);
+ }
+
+ @Override
+ public void run() {
+
+ performCacheMaint();
+
+ // stamp the file after we finish, in case the VM stopped this daemon thread
+ lastMaintTS = System.currentTimeMillis();
+ try {
+ FileUtilities.writeStringToFile(lastMaintFile, "Last maint run at " + (new Date()));
+ }
+ catch (IOException e) {
+ Msg.error(this, "Unable to write file cache maintenance file: " + lastMaintFile, e);
+ }
+ }
+ }
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemService.java b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemService.java
index 731695e48f..af69bdf49e 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemService.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/formats/gfilesystem/FileSystemService.java
@@ -83,12 +83,21 @@ import ghidra.util.timer.GTimer;
public class FileSystemService {
private static int FSRL_INTERN_SIZE = 1000;
- private static class Singleton {
- private static final FileSystemService instance = new FileSystemService();
+ private static FileSystemService instance;
+
+ public static synchronized FileSystemService getInstance() {
+ if (instance == null) {
+ instance = new FileSystemService();
+ }
+ return instance;
}
- public static FileSystemService getInstance() {
- return Singleton.instance;
+ /**
+ * Returns true if this service has been loaded
+ * @return true if this service has been loaded
+ */
+ public static synchronized boolean isInitialized() {
+ return instance != null;
}
private final LocalFileSystem localFS = LocalFileSystem.makeGlobalRootFS();
@@ -296,8 +305,9 @@ public class FileSystemService {
if (containerFSRL.getMD5() == null) {
containerFSRL = containerFSRL.withMD5(cfi.md5);
}
- GFileSystem fs = FileSystemFactoryMgr.getInstance().mountFileSystem(
- fsFSRL.getProtocol(), containerFSRL, cfi.file, this, monitor);
+ GFileSystem fs = FileSystemFactoryMgr.getInstance()
+ .mountFileSystem(
+ fsFSRL.getProtocol(), containerFSRL, cfi.file, this, monitor);
ref = fs.getRefManager().create();
filesystemCache.add(fs);
}
@@ -394,8 +404,9 @@ public class FileSystemService {
* @return {@link FSRL} pointing to the same file, never null
*/
public FSRL getLocalFSRL(File f) {
- return localFS.getFSRL().withPath(
- FSUtilities.appendPath("/", FilenameUtils.separatorsToUnix(f.getPath())));
+ return localFS.getFSRL()
+ .withPath(
+ FSUtilities.appendPath("/", FilenameUtils.separatorsToUnix(f.getPath())));
}
/**
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/plugins/fsbrowser/FileSystemBrowserPlugin.java b/Ghidra/Features/Base/src/main/java/ghidra/plugins/fsbrowser/FileSystemBrowserPlugin.java
index cb4bc1e25a..7a2e9589a2 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/plugins/fsbrowser/FileSystemBrowserPlugin.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/plugins/fsbrowser/FileSystemBrowserPlugin.java
@@ -40,7 +40,7 @@ import ghidra.framework.plugintool.util.PluginStatus;
import ghidra.plugin.importer.ImporterUtilities;
import ghidra.plugin.importer.ProgramMappingService;
import ghidra.util.Msg;
-import ghidra.util.SystemUtilities;
+import ghidra.util.Swing;
import ghidra.util.exception.CancelledException;
import ghidra.util.filechooser.GhidraFileFilter;
import ghidra.util.task.TaskLauncher;
@@ -127,7 +127,7 @@ public class FileSystemBrowserPlugin extends Plugin implements FrontEndable, Pro
* @param show boolean true if the new browser component should be shown
*/
/* package */ void createNewFileSystemBrowser(FileSystemRef fsRef, boolean show) {
- SystemUtilities.runIfSwingOrPostSwingLater(() -> doCreateNewFileSystemBrowser(fsRef, show));
+ Swing.runIfSwingOrRunLater(() -> doCreateNewFileSystemBrowser(fsRef, show));
}
private void doCreateNewFileSystemBrowser(FileSystemRef fsRef, boolean show) {
@@ -157,7 +157,7 @@ public class FileSystemBrowserPlugin extends Plugin implements FrontEndable, Pro
* @param fsFSRL {@link FSRLRoot} of the filesystem to close.
*/
/* package */ void removeFileSystemBrowser(FSRLRoot fsFSRL) {
- SystemUtilities.runIfSwingOrPostSwingLater(() -> {
+ Swing.runIfSwingOrRunLater(() -> {
FileSystemBrowserComponentProvider fsbcp = currentBrowsers.get(fsFSRL);
if (fsbcp == null) {
return;
@@ -173,7 +173,7 @@ public class FileSystemBrowserPlugin extends Plugin implements FrontEndable, Pro
* Close all file system browser windows.
*/
private void removeAllFileSystemBrowsers() {
- SystemUtilities.runIfSwingOrPostSwingLater(() -> {
+ Swing.runIfSwingOrRunLater(() -> {
for (FileSystemBrowserComponentProvider fsbcp : new ArrayList<>(
currentBrowsers.values())) {
fsbcp.removeFromTool();
@@ -196,7 +196,9 @@ public class FileSystemBrowserPlugin extends Plugin implements FrontEndable, Pro
@Override
public void projectClosed(Project project) {
removeAllFileSystemBrowsers();
- FileSystemService.getInstance().closeUnusedFileSystems();
+ if (FileSystemService.isInitialized()) {
+ FileSystemService.getInstance().closeUnusedFileSystems();
+ }
}
@Override
@@ -252,15 +254,16 @@ public class FileSystemBrowserPlugin extends Plugin implements FrontEndable, Pro
private void doOpenFilesystem(FSRL containerFSRL, Component parent, TaskMonitor monitor) {
try {
monitor.setMessage("Probing " + containerFSRL.getName() + " for filesystems");
- FileSystemRef ref = FileSystemService.getInstance().probeFileForFilesystem(
- containerFSRL, monitor, FileSystemProbeConflictResolver.GUI_PICKER);
+ FileSystemRef ref = FileSystemService.getInstance()
+ .probeFileForFilesystem(
+ containerFSRL, monitor, FileSystemProbeConflictResolver.GUI_PICKER);
if (ref == null) {
Msg.showWarn(this, parent, "Open Filesystem",
"No filesystem provider for " + containerFSRL.getName());
return;
}
- SystemUtilities.runSwingLater(() -> {
+ Swing.runLater(() -> {
createNewFileSystemBrowser(ref, true);
});
}
@@ -275,7 +278,7 @@ public class FileSystemBrowserPlugin extends Plugin implements FrontEndable, Pro
* filesystem browser and then displays that filesystem in a new fsb browser.
*/
/* package */ void openFileSystem() {
- SystemUtilities.runSwingLater(() -> doOpenFileSystem());
+ Swing.runLater(this::doOpenFileSystem);
}
private void doOpenFileSystem() {
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/formats/gfilesystem/FileSystemServiceTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/formats/gfilesystem/FileSystemServiceTest.java
index 82d985abc6..b60d4b8d38 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/formats/gfilesystem/FileSystemServiceTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/formats/gfilesystem/FileSystemServiceTest.java
@@ -18,9 +18,7 @@ package ghidra.formats.gfilesystem;
import java.io.File;
import java.io.IOException;
-import org.junit.Assert;
-import org.junit.Before;
-import org.junit.Test;
+import org.junit.*;
import generic.test.AbstractGenericTest;
import ghidra.test.AbstractGhidraHeadedIntegrationTest;
@@ -75,5 +73,4 @@ public class FileSystemServiceTest extends AbstractGhidraHeadedIntegrationTest {
Assert.assertTrue(ioe.getMessage().contains("Exact file no longer exists"));
}
}
-
}
diff --git a/Ghidra/Features/Base/src/test/java/ghidra/formats/gfilesystem/FileCacheTest.java b/Ghidra/Features/Base/src/test/java/ghidra/formats/gfilesystem/FileCacheTest.java
index 67743c91db..17a77a1ba2 100644
--- a/Ghidra/Features/Base/src/test/java/ghidra/formats/gfilesystem/FileCacheTest.java
+++ b/Ghidra/Features/Base/src/test/java/ghidra/formats/gfilesystem/FileCacheTest.java
@@ -27,6 +27,7 @@ import utilities.util.FileUtilities;
public class FileCacheTest extends AbstractGenericTest {
+ private FileCache cache;
private File cacheDir;
private TaskMonitor monitor = TaskMonitor.DUMMY;
@@ -35,13 +36,19 @@ public class FileCacheTest extends AbstractGenericTest {
cacheDir = createTempDirectory("FileCacheTest");
}
+ @After
+ public void tearDown() {
+ // keep the cleaner thread from cross-contaminating tests
+ waitForCleanup();
+ }
+
public InputStream toIS(String s) {
return new ByteArrayInputStream(s.getBytes());
}
@Test
public void testPurge() throws IOException, CancelledException {
- FileCache cache = new FileCache(cacheDir);
+ cache = new FileCache(cacheDir);
FileCacheEntry cfi = cache.addStream(toIS("This is a test1"), monitor);
Assert.assertTrue(cfi.file.exists());
@@ -61,7 +68,8 @@ public class FileCacheTest extends AbstractGenericTest {
@Test
public void testAgeOff() throws IOException, CancelledException {
- FileCache cache = new FileCache(cacheDir);
+ cache = new FileCache(cacheDir);
+ waitForCleanup(); // don't let the cache delete the file we are about to create
FileCacheEntry cfi = cache.addStream(toIS("This is a test1"), monitor);
Assert.assertTrue(cfi.file.exists());
@@ -72,15 +80,15 @@ public class FileCacheTest extends AbstractGenericTest {
File lastMaintFile = new File(cacheDir, ".lastmaint");
lastMaintFile.delete();
- new FileCache(cacheDir);
+ cache = new FileCache(cacheDir);
// the file added before should have been purged by the startup of cache2
- Assert.assertFalse(cfi.file.exists());
+ waitForCondition(() -> cfi.file.exists());
}
@Test
public void testAddFile() throws IOException, CancelledException {
- FileCache cache = new FileCache(cacheDir);
+ cache = new FileCache(cacheDir);
File tmpFile = createTempFile("filecacheaddfile");
FileUtilities.writeStringToFile(tmpFile, "This is a test1");
@@ -93,7 +101,7 @@ public class FileCacheTest extends AbstractGenericTest {
@Test
public void testAddStream() throws IOException, CancelledException {
- FileCache cache = new FileCache(cacheDir);
+ cache = new FileCache(cacheDir);
FileCacheEntry fce = cache.addStream(toIS("This is a test1"), monitor);
Assert.assertTrue(fce.file.exists());
@@ -103,13 +111,18 @@ public class FileCacheTest extends AbstractGenericTest {
@Test
public void testAddStreamPush() throws IOException, CancelledException {
- FileCache cache = new FileCache(cacheDir);
+ cache = new FileCache(cacheDir);
FileCacheEntry fce = cache.pushStream((os) -> {
FileUtilities.copyStreamToStream(toIS("This is a test1"), os, monitor);
- } , monitor);
+ }, monitor);
Assert.assertTrue(fce.file.exists());
Assert.assertEquals("10428da10f5aa2793cb73c0b680e1621", fce.md5);
Assert.assertEquals(1, cache.getFileAddCount());
}
+
+ private void waitForCleanup() {
+ waitForCondition(() -> !cache.isCleaning());
+ }
+
}