From 9f883022a43058eb4b51ebf880c0dfd6dccc856c Mon Sep 17 00:00:00 2001 From: Ryan Kurtz Date: Thu, 16 May 2024 06:43:27 -0400 Subject: [PATCH] GP-4563: Support for searching for libraries inside a GFileSystem --- .../Base/data/base.icons.theme.properties | 1 + .../FileSystemBrowserPlugin.html | 13 +++ .../importer/LibrarySearchPathManager.java | 43 +++++++- .../opinion/AbstractLibrarySupportLoader.java | 101 ++++++++---------- .../plugins/fsbrowser/FSBActionManager.java | 37 ++++++- .../plugins/fsbrowser/ImageManager.java | 1 + .../pathmanager/PathnameTablePanel.java | 6 +- 7 files changed, 140 insertions(+), 62 deletions(-) diff --git a/Ghidra/Features/Base/data/base.icons.theme.properties b/Ghidra/Features/Base/data/base.icons.theme.properties index 19323dd6015..648f2fdb88c 100644 --- a/Ghidra/Features/Base/data/base.icons.theme.properties +++ b/Ghidra/Features/Base/data/base.icons.theme.properties @@ -366,6 +366,7 @@ icon.plugin.fsbrowser.import = images/famfamfam_silk_icons_v013/application_get. icon.plugin.fsbrowser.ios = images/famfamfam_silk_icons_v013/phone.png icon.plugin.fsbrowser.open.all = images/famfamfam_silk_icons_v013/application_cascade.png icon.plugin.fsbrowser.list.mounted = downArrow.png +icon.plugin.fsbrowser.library = images/imported_bookmark.gif icon.base.util.fixed.bit.size.field = icon.pulldown diff --git a/Ghidra/Features/Base/src/main/help/help/topics/FileSystemBrowserPlugin/FileSystemBrowserPlugin.html b/Ghidra/Features/Base/src/main/help/help/topics/FileSystemBrowserPlugin/FileSystemBrowserPlugin.html index 6c49462fa84..91ce4338b4c 100644 --- a/Ghidra/Features/Base/src/main/help/help/topics/FileSystemBrowserPlugin/FileSystemBrowserPlugin.html +++ b/Ghidra/Features/Base/src/main/help/help/topics/FileSystemBrowserPlugin/FileSystemBrowserPlugin.html @@ -90,6 +90,12 @@

Batch Import

using the Batch Import dialog.

+

Add To Program

+ +
+

Adds the selected file into the currently active program.

+
+

Export

@@ -103,6 +109,13 @@

Export All

Recursively copies the contents of a selected folder to a directory you select on your local computer.

+ +

Add Library Search Path

+ +
+

Adds the currently selected file or folder to the list of library search paths which + is used during program import.

+

View As Image

diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/importer/LibrarySearchPathManager.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/importer/LibrarySearchPathManager.java index 0b0ab10c81b..739e70ca65e 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/importer/LibrarySearchPathManager.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/importer/LibrarySearchPathManager.java @@ -15,9 +15,16 @@ */ package ghidra.app.util.importer; +import java.io.File; +import java.io.IOException; +import java.net.MalformedURLException; import java.util.*; +import ghidra.formats.gfilesystem.FSRL; +import ghidra.formats.gfilesystem.FileSystemService; import ghidra.framework.Platform; +import ghidra.util.exception.CancelledException; +import ghidra.util.task.TaskMonitor; /** * A simple class for managing the library search path @@ -61,11 +68,39 @@ public static String[] getLibraryPaths() { } /** - * Returns an array of directories to search for libraries - * @return a list of directories to search for libraries + * Returns a {@link List} of {@link FSRL}s to search for libraries + * @param log The log + * @param monitor A cancellable monitor + * @return a {@link List} of {@link FSRL}s to search for libraries + * @throws CancelledException if the user cancelled the operation */ - public static List getLibraryPathsList() { - return new ArrayList<>(pathList); + public static List getLibraryFsrlList(MessageLog log, TaskMonitor monitor) + throws CancelledException { + FileSystemService fsService = FileSystemService.getInstance(); + List fsrlList = new ArrayList<>(); + for (String path : pathList) { + monitor.checkCancelled(); + path = path.trim(); + FSRL fsrl = null; + try { + fsrl = FSRL.fromString(path); + } + catch (MalformedURLException e) { + try { + File f = new File(path); + if (f.exists() && f.isAbsolute()) { + fsrl = fsService.getLocalFSRL(f.getCanonicalFile()); + } + } + catch (IOException e2) { + log.appendException(e2); + } + } + if (fsrl != null) { + fsrlList.add(fsrl); + } + } + return fsrlList; } /** diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractLibrarySupportLoader.java b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractLibrarySupportLoader.java index 6e08264c117..482f68a82a9 100644 --- a/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractLibrarySupportLoader.java +++ b/Ghidra/Features/Base/src/main/java/ghidra/app/util/opinion/AbstractLibrarySupportLoader.java @@ -17,9 +17,11 @@ import java.io.File; import java.io.IOException; -import java.nio.file.*; +import java.nio.file.InvalidPathException; +import java.nio.file.Path; import java.util.*; import java.util.stream.Collectors; +import java.util.stream.Stream; import org.apache.commons.io.FilenameUtils; import org.apache.commons.lang3.ObjectUtils; @@ -584,16 +586,13 @@ else if (!customSearchPaths.isEmpty() || !localSearchPaths.isEmpty() || if (!success) { release(loadedPrograms, consumer); } - for (FileSystemSearchPath fsSearchPath : localSearchPaths) { - if (!fsSearchPath.fsRef().isClosed()) { - fsSearchPath.fsRef().close(); - } - } - for (FileSystemSearchPath fsSearchPath : systemSearchPaths) { - if (!fsSearchPath.fsRef().isClosed()) { - fsSearchPath.fsRef().close(); - } - } + Stream.of(customSearchPaths, localSearchPaths, systemSearchPaths) + .flatMap(Collection::stream) + .forEach(fsSearchPath -> { + if (!fsSearchPath.fsRef().isClosed()) { + fsSearchPath.fsRef().close(); + } + }); } } @@ -1061,9 +1060,10 @@ protected record FileSystemSearchPath(FileSystemRef fsRef, Path fsPath) {} * @param monitor A cancelable task monitor * @return A {@link List} of priority-ordered custom {@link FileSystemSearchPath}s used to * search for libraries + * @throws CancelledException if the user cancelled the load */ protected List getCustomLibrarySearchPaths(ByteProvider provider, - List