Skip to content

Ghidra is a software reverse engineering (SRE) framework

cleanup, fix tests where script deleted before getScriptInfo

Jason P. Leasure0f74dcb8be42
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusModel.java
index 60aac28769..9b41abba44 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusModel.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusModel.java
@@ -261,11 +261,10 @@ public class BundleStatusModel extends AbstractSortedTableModel<BundleStatus> {
fireTableRowsInserted(index, files.size() - 1);
}
- void remove(int[] selectedModelRows) {
- List<BundleStatus> toRemove = new ArrayList<>();
- for (int selectedRow : selectedModelRows) {
- toRemove.add(statuses.get(selectedRow));
- }
+ void remove(int[] modelRows) {
+ List<BundleStatus> toRemove = Arrays.stream(modelRows).mapToObj(statuses::get).collect(
+ Collectors.toUnmodifiableList());
+
for (BundleStatus status : toRemove) {
if (!status.isReadOnly()) {
statuses.remove(status);
@@ -273,7 +272,7 @@ public class BundleStatusModel extends AbstractSortedTableModel<BundleStatus> {
}
else {
Msg.showInfo(this, this.provider.getComponent(), "Unabled to remove path",
- "System path cannot be removed: " + status.toString());
+ "System path cannot be removed: " + status.getPath().toString());
}
}
fireTableDataChanged();
@@ -386,51 +385,41 @@ public class BundleStatusModel extends AbstractSortedTableModel<BundleStatus> {
fireTableDataChanged();
}
- /**
- * This is for testing only!
- *
- * insert path, marked editable and non-readonly
- * @param path the path to insert
- */
- public void insertPathForTesting(String path) {
- addNewStatus(new ResourceFile(path), true, false);
- }
-
- private ArrayList<BundleStatusListener> listeners = new ArrayList<>();
+ private ArrayList<BundleStatusListener> bundleStatusListeners = new ArrayList<>();
public void addListener(BundleStatusListener listener) {
- synchronized (listeners) {
- if (!listeners.contains(listener)) {
- listeners.add(listener);
+ synchronized (bundleStatusListeners) {
+ if (!bundleStatusListeners.contains(listener)) {
+ bundleStatusListeners.add(listener);
}
}
}
public void removeListener(BundleStatusListener listener) {
- synchronized (listeners) {
- listeners.remove(listener);
+ synchronized (bundleStatusListeners) {
+ bundleStatusListeners.remove(listener);
}
}
private void fireBundlesChanged() {
- synchronized (listeners) {
- for (BundleStatusListener listener : listeners) {
+ synchronized (bundleStatusListeners) {
+ for (BundleStatusListener listener : bundleStatusListeners) {
listener.bundlesChanged();
}
}
}
void fireBundleEnablementChanged(BundleStatus path, boolean newValue) {
- synchronized (listeners) {
- for (BundleStatusListener listener : listeners) {
+ synchronized (bundleStatusListeners) {
+ for (BundleStatusListener listener : bundleStatusListeners) {
listener.bundleEnablementChanged(path, newValue);
}
}
}
void fireBundleActivationChanged(BundleStatus path, boolean newValue) {
- synchronized (listeners) {
- for (BundleStatusListener listener : listeners) {
+ synchronized (bundleStatusListeners) {
+ for (BundleStatusListener listener : bundleStatusListeners) {
listener.bundleActivationChanged(path, newValue);
}
}
@@ -509,7 +498,7 @@ public class BundleStatusModel extends AbstractSortedTableModel<BundleStatus> {
}
/**
- * avoid generating events when nothing changes
+ * overridden to avoid generating events when nothing changed
*/
@Override
protected void sort(List<BundleStatus> data, TableSortingContext<BundleStatus> sortingContext) {
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusProvider.java
index 9470a855da..8378eebbe7 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/osgi/BundleStatusProvider.java
@@ -65,6 +65,7 @@ public class BundleStatusProvider extends ComponentProviderAdapter {
super(tool, "Bundle Status Manager", owner);
this.bundleHost = BundleHost.getInstance();
this.bundleStatusModel = new BundleStatusModel(this, bundleHost);
+
bundleStatusModel.addListener(new BundleStatusListener() {
@Override
public void bundleEnablementChanged(BundleStatus status, boolean enabled) {
@@ -93,7 +94,6 @@ public class BundleStatusProvider extends ComponentProviderAdapter {
this.fileChooser = null;
build();
- //getTool().addComponentProvider(this, false);
addToTool();
createActions();
}
@@ -194,7 +194,7 @@ public class BundleStatusProvider extends ComponentProviderAdapter {
action = new DockingAction("ActivateBundle", this.getName()) {
@Override
public void actionPerformed(ActionContext context) {
- startActivateSelectedTask();
+ doActivateBundle();
}
@Override
@@ -214,7 +214,7 @@ public class BundleStatusProvider extends ComponentProviderAdapter {
action = new DockingAction("DeactivateBundle", this.getName()) {
@Override
public void actionPerformed(ActionContext context) {
- startDeactivateSelectedTask();
+ doDeactivateBundle();
}
@Override
@@ -234,14 +234,7 @@ public class BundleStatusProvider extends ComponentProviderAdapter {
action = new DockingAction("CleanBundle", this.getName()) {
@Override
public void actionPerformed(ActionContext context) {
- int[] selectedModelRows = getSelectedModelRows();
- boolean anythingCleaned = false;
- for (BundleStatus o : bundleStatusModel.getRowObjects(selectedModelRows)) {
- anythingCleaned |= bundleHost.getGhidraBundle(o.getPath()).clean();
- }
- if (anythingCleaned) {
- AnimationUtils.shakeComponent(getComponent());
- }
+ doClean();
}
@Override
@@ -262,7 +255,7 @@ public class BundleStatusProvider extends ComponentProviderAdapter {
action = new DockingAction("AddBundle", this.getName()) {
@Override
public void actionPerformed(ActionContext context) {
- addBundlesAction();
+ showAddBundlesFileChooser();
}
@Override
@@ -284,7 +277,7 @@ public class BundleStatusProvider extends ComponentProviderAdapter {
action = new DockingAction("RemoveBundle", this.getName()) {
@Override
public void actionPerformed(ActionContext context) {
- removeBundlesAction();
+ doRemoveBundles();
}
@Override
@@ -316,7 +309,18 @@ public class BundleStatusProvider extends ComponentProviderAdapter {
return Arrays.stream(selectedRows).map(filterPanel::getModelRow).toArray();
}
- private void removeBundlesAction() {
+ private void doClean() {
+ int[] selectedModelRows = getSelectedModelRows();
+ boolean anythingCleaned = false;
+ for (BundleStatus o : bundleStatusModel.getRowObjects(selectedModelRows)) {
+ anythingCleaned |= bundleHost.getGhidraBundle(o.getPath()).clean();
+ }
+ if (anythingCleaned) {
+ AnimationUtils.shakeComponent(getComponent());
+ }
+ }
+
+ private void doRemoveBundles() {
int[] selectedModelRows = getSelectedModelRows();
if (selectedModelRows == null || selectedModelRows.length == 0) {
return;
@@ -326,7 +330,7 @@ public class BundleStatusProvider extends ComponentProviderAdapter {
bundleStatusTable.clearSelection();
}
- private void addBundlesAction() {
+ private void showAddBundlesFileChooser() {
if (fileChooser == null) {
fileChooser = new GhidraFileChooser(panel);
fileChooser.setMultiSelectionEnabled(true);
@@ -369,14 +373,7 @@ public class BundleStatusProvider extends ComponentProviderAdapter {
}
}
- /*
- @Override
- public ActionContext getActionContext(MouseEvent event) {
- return new ActionContext(this, bundleStatusTable.getSelectedRows(), bundleStatusTable);
- }
- */
-
- protected void startActivateSelectedTask() {
+ protected void doActivateBundle() {
ConsoleService console = getTool().getService(ConsoleService.class);
int[] selectedModelRows = getSelectedModelRows();
@@ -428,7 +425,7 @@ public class BundleStatusProvider extends ComponentProviderAdapter {
}, getComponent(), 1000);
}
- protected void startDeactivateSelectedTask() {
+ protected void doDeactivateBundle() {
ConsoleService console = getTool().getService(ConsoleService.class);
int[] selectedModelRows = getSelectedModelRows();
@@ -466,7 +463,7 @@ public class BundleStatusProvider extends ComponentProviderAdapter {
protected void startActivateDeactiveTask(BundleStatus status, boolean activate) {
status.setBusy(true);
- notifyTableRowChanged(bundleStatusModel.getRowIndex(status));
+ notifyTableRowChanged(status);
ConsoleService console = getTool().getService(ConsoleService.class);
new TaskLauncher(new Task((activate ? "Activating" : "Deactivating ") + " bundle...") {
@@ -493,7 +490,7 @@ public class BundleStatusProvider extends ComponentProviderAdapter {
}
finally {
status.setBusy(false);
- notifyTableRowChanged(bundleStatusModel.getRowIndex(status));
+ notifyTableRowChanged(status);
}
}
}, null, 1000);
@@ -503,12 +500,10 @@ public class BundleStatusProvider extends ComponentProviderAdapter {
return bundleStatusModel;
}
- public void notifyTableChanged() {
- bundleStatusTable.notifyTableChanged(new TableModelEvent(bundleStatusModel));
- }
-
- public void notifyTableRowChanged(int rowIndex) {
- bundleStatusTable.notifyTableChanged(new TableModelEvent(bundleStatusModel, rowIndex));
+ public void notifyTableRowChanged(BundleStatus status) {
+ int modelRowIndex = bundleStatusModel.getRowIndex(status);
+ int viewRowIndex = filterPanel.getViewRow(modelRowIndex);
+ bundleStatusTable.notifyTableChanged(new TableModelEvent(bundleStatusModel, viewRowIndex));
}
@Override
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/DraggableScriptTable.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/DraggableScriptTable.java
index e301edef32..8e8b6eee1b 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/DraggableScriptTable.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/DraggableScriptTable.java
@@ -31,8 +31,8 @@ public class DraggableScriptTable extends GhidraTable implements Draggable {
private final GhidraScriptComponentProvider provider;
/**
- * Constructs a new DraggableGhidraTable using the specified table model.
- * @param scriptModel the table model
+ * Constructs a new DraggableGhidraTable.
+ * @param provider the provider, from which getTableModel and getScriptAt are used
*/
public DraggableScriptTable(GhidraScriptComponentProvider provider) {
super(provider.getTableModel());
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java
index 8a1388940f..704281619d 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptComponentProvider.java
@@ -283,6 +283,17 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
return tableModel.getScriptAt(tableFilterPanel.getModelRow(rowIndex));
}
+ public List<ResourceFile> getScriptDirectories() {
+ return bundleStatusProvider.getModel().getEnabledPaths().stream().filter(
+ ResourceFile::isDirectory).collect(Collectors.toList());
+ }
+
+ public List<ResourceFile> getWritableScriptDirectories() {
+ BundleStatusModel m = bundleStatusProvider.getModel();
+ return m.getEnabledPaths().stream().filter(ResourceFile::isDirectory).filter(
+ m::isWriteable).collect(Collectors.toList());
+ }
+
boolean isEditorOpen(ResourceFile script) {
GhidraScriptEditorComponentProvider editor = editorMap.get(script);
return editor != null && plugin.getTool().isVisible(editor);
@@ -336,11 +347,6 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
}
}
- public List<ResourceFile> getScriptDirectories() {
- return bundleStatusProvider.getModel().getEnabledPaths().stream().filter(
- ResourceFile::isDirectory).collect(Collectors.toList());
- }
-
public void enableScriptDirectory(ResourceFile scriptDir) {
if (bundleStatusProvider.getModel().enablePath(scriptDir)) {
Msg.showInfo(this, getComponent(), "Script Path Added/Enabled",
@@ -454,7 +460,6 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
}
catch (ClassNotFoundException e) {
console.addErrorMessage("", "Unable to locate script class: " + scriptName);
- e.printStackTrace();
}
// show the error icon
@@ -521,8 +526,6 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
updateAvailableScriptFilesForAllPaths();
- tableModel.fireTableDataChanged();
-
trimUnusedTreeCategories();
scriptRoot.fireNodeStructureChanged(scriptRoot);
@@ -534,12 +537,8 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
private void updateAvailableScriptFilesForAllPaths() {
List<ResourceFile> scriptsToRemove = tableModel.getScripts();
List<ResourceFile> scriptAccumulator = new ArrayList<>();
- List<ResourceFile> bundlePaths = bundleStatusProvider.getModel().getEnabledPaths();
- for (ResourceFile bundlePath : bundlePaths) {
- if (bundlePath.isDirectory()) {
- updateAvailableScriptFilesForDirectory(scriptsToRemove, scriptAccumulator,
- bundlePath);
- }
+ for (ResourceFile bundlePath : getScriptDirectories()) {
+ updateAvailableScriptFilesForDirectory(scriptsToRemove, scriptAccumulator, bundlePath);
}
// note: do this after the loop to prevent a flurry of table model update events
@@ -587,6 +586,7 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
if (scriptAction != null) {
scriptAction.refresh();
}
+
}
}
@@ -606,9 +606,8 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
*/
// note: turn String[] to List<String> to use hashing
- Iterator<ScriptInfo> scripts = GhidraScriptUtil.getScriptInfoIterator();
Set<List<String>> categories = new HashSet<>();
- for (ScriptInfo info : CollectionUtils.asIterable(scripts)) {
+ for (ScriptInfo info : GhidraScriptUtil.getScriptInfoIterable()) {
String[] path = info.getCategory();
List<String> category = Arrays.asList(path);
for (int i = 1; i <= category.size(); i++) {
@@ -684,6 +683,12 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
return editor;
}
+ /**
+ * reassign an existing editor component
+ *
+ * @param oldScript who the editor is currently assigned to
+ * @param newScript the new script to assign it to
+ */
void switchEditor(ResourceFile oldScript, ResourceFile newScript) {
GhidraScriptEditorComponentProvider editor = editorMap.get(oldScript);
editorMap.put(newScript, editor);
@@ -1131,7 +1136,10 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
@Override
public boolean acceptsRow(ResourceFile script) {
- ScriptInfo info = GhidraScriptUtil.getScriptInfo(script);
+ ScriptInfo info = GhidraScriptUtil.getExistingScriptInfo(script);
+ if (info == null) {
+ return false;
+ }
String[] category = getSelectedCategoryPath();
if (category == null) { // root node
@@ -1170,10 +1178,4 @@ public class GhidraScriptComponentProvider extends ComponentProviderAdapter {
}
}
- public List<ResourceFile> getWritableScriptDirectories() {
- BundleStatusModel m = bundleStatusProvider.getModel();
- return m.getEnabledPaths().stream().filter(ResourceFile::isDirectory).filter(
- m::isWriteable).collect(Collectors.toList());
- }
-
}
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptTableModel.java b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptTableModel.java
index 9ea4eb50d4..f8d60858e2 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptTableModel.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/plugin/core/script/GhidraScriptTableModel.java
@@ -99,12 +99,13 @@ class GhidraScriptTableModel extends GDynamicColumnTableModel<ResourceFile, Obje
}
void insertScripts(List<ResourceFile> scriptFiles) {
+ int rowStart = scriptList.size();
for (ResourceFile script : scriptFiles) {
if (!scriptList.contains(script)) {
scriptList.add(script);
}
}
- fireTableDataChanged();
+ fireTableRowsInserted(rowStart, rowStart + scriptFiles.size()-1);
}
void removeScript(ResourceFile script) {
@@ -190,7 +191,7 @@ class GhidraScriptTableModel extends GDynamicColumnTableModel<ResourceFile, Obje
}
private class ScriptActionColumn
- extends AbstractDynamicTableColumn<ResourceFile, Boolean, Object> {
+ extends AbstractDynamicTableColumn<ResourceFile, Boolean, Object> {
@Override
public String getColumnDescription() {
@@ -318,7 +319,7 @@ class GhidraScriptTableModel extends GDynamicColumnTableModel<ResourceFile, Obje
}
private class DescriptionColumn
- extends AbstractDynamicTableColumn<ResourceFile, String, Object> {
+ extends AbstractDynamicTableColumn<ResourceFile, String, Object> {
@Override
public String getColumnName() {
@@ -339,10 +340,9 @@ class GhidraScriptTableModel extends GDynamicColumnTableModel<ResourceFile, Obje
}
private class KeyBindingColumn
- extends AbstractDynamicTableColumn<ResourceFile, KeyBindingsInfo, Object> {
+ extends AbstractDynamicTableColumn<ResourceFile, KeyBindingsInfo, Object> {
- private GColumnRenderer<KeyBindingsInfo> renderer =
- new AbstractGColumnRenderer<>() {
+ private GColumnRenderer<KeyBindingsInfo> renderer = new AbstractGColumnRenderer<>() {
@Override
public Component getTableCellRendererComponent(GTableCellRenderingData data) {
@@ -365,8 +365,7 @@ class GhidraScriptTableModel extends GDynamicColumnTableModel<ResourceFile, Obje
if (info.hasAction) {
component.setForeground(Color.BLACK);
- component.setToolTipText(
- "Keybinding for action in tool" + keybindingText);
+ component.setToolTipText("Keybinding for action in tool" + keybindingText);
}
else {
component.setForeground(Color.LIGHT_GRAY);
@@ -376,7 +375,7 @@ class GhidraScriptTableModel extends GDynamicColumnTableModel<ResourceFile, Obje
if (isSelected) {
Color selectedForegroundColor =
- (info.errorMessage != null) ? Color.PINK : Color.WHITE;
+ (info.errorMessage != null) ? Color.PINK : Color.WHITE;
component.setForeground(selectedForegroundColor);
}
return component;
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.java
index e928dc6d9d..779e4d0ac3 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/GhidraScriptUtil.java
@@ -21,13 +21,10 @@ import java.nio.file.Path;
import java.util.*;
import java.util.stream.Collectors;
-import org.apache.commons.collections4.map.LazyMap;
-
import generic.jar.ResourceFile;
import ghidra.framework.Application;
import ghidra.program.model.listing.Program;
import ghidra.util.Msg;
-import ghidra.util.SystemUtilities;
import ghidra.util.classfinder.ClassSearcher;
import ghidra.util.task.TaskMonitor;
@@ -39,7 +36,6 @@ public class GhidraScriptUtil {
private static final String SCRIPTS_SUBDIR_NAME = "ghidra_scripts";
private static final String DEV_SCRIPTS_SUBDIR_NAME = "developer_scripts";
- private static final String BIN_DIR_NAME = "bin";
private static List<GhidraScriptProvider> providers = null;
/**
@@ -51,8 +47,7 @@ public class GhidraScriptUtil {
private static Map<ResourceFile, ScriptInfo> scriptFileToInfoMap = new HashMap<>();
- private static Map<String, List<ResourceFile>> scriptNameToFilesMap =
- LazyMap.lazyMap(new HashMap<String, List<ResourceFile>>(), () -> new ArrayList<>());
+ private static Map<String, List<ResourceFile>> scriptNameToFilesMap = new HashMap<>();
static {
scriptBundlePaths = getSystemScriptPaths();
@@ -153,6 +148,14 @@ public class GhidraScriptUtil {
scriptBundlePaths = new ArrayList<>(newPaths);
}
+ public static List<ResourceFile> getAllScripts() {
+ List<ResourceFile> scriptList = new ArrayList<>();
+ for (ResourceFile dirPath : scriptBundlePaths) {
+ updateAvailableScriptFilesForDirectory(scriptList, dirPath);
+ }
+ return scriptList;
+ }
+
public static Path getOsgiDir() {
Path usersettings = Application.getUserSettingsDirectory().toPath();
return usersettings.resolve("osgi");
@@ -181,33 +184,6 @@ public class GhidraScriptUtil {
}
}
- /**
- * Returns a list of directories. Development directories differ from standard script
- * directories in that the former have a bin directory at a different location from
- * the latter, due to the setup of the development environment.
- *
- * @return Returns a list of directories
- */
- static Collection<ResourceFile> getDevelopmentScriptBinDirectories() {
- if (!SystemUtilities.isInDevelopmentMode() || SystemUtilities.isInTestingMode()) {
- return Collections.emptyList();
- }
-
- Set<ResourceFile> dirs = new HashSet<>();
- for (ResourceFile scriptDir : scriptBundlePaths) {
- //
- // Assumed structure of script dir path:
- // /some/path/Ghidra/Features/Module/ghidra_scripts
- //
- // Desired path:
- // /some/path/Ghidra/Features/Module/bin/scripts
-
- ResourceFile moduleDir = scriptDir.getParentFile();
- dirs.add(new ResourceFile(moduleDir, BIN_DIR_NAME + File.separator + "scripts"));
- }
- return dirs;
- }
-
/**
* clear ScriptInfo metadata cached by GhidraScriptUtil
*/
@@ -249,22 +225,29 @@ public class GhidraScriptUtil {
public static void removeMetadata(ResourceFile scriptFile) {
scriptFileToInfoMap.remove(scriptFile);
- Iterator<ResourceFile> iter = scriptNameToFilesMap.get(scriptFile.getName()).iterator();
- while (iter.hasNext()) {
- ResourceFile rFile = iter.next();
- if (scriptFile.equals(rFile)) {
- iter.remove();
- break;
+ String name = scriptFile.getName();
+ List<ResourceFile> files = scriptNameToFilesMap.get(name);
+ if (files != null) {
+ Iterator<ResourceFile> iter = files.iterator();
+ while (iter.hasNext()) {
+ ResourceFile rFile = iter.next();
+ if (scriptFile.equals(rFile)) {
+ iter.remove();
+ break;
+ }
+ }
+ if (files.isEmpty()) {
+ scriptNameToFilesMap.remove(name);
}
}
}
/**
- * Returns an iterator over all script info objects.
- * @return an iterator over all script info objects
+ * get all scripts
+ * @return an iterable over all script info objects
*/
- public static Iterator<ScriptInfo> getScriptInfoIterator() {
- return scriptFileToInfoMap.values().iterator();
+ public static Iterable<ScriptInfo> getScriptInfoIterable() {
+ return () -> scriptFileToInfoMap.values().iterator();
}
/**
@@ -284,7 +267,8 @@ public class GhidraScriptUtil {
scriptFileToInfoMap.put(scriptFile, info);
String name = scriptFile.getName();
- List<ResourceFile> matchingFiles = scriptNameToFilesMap.get(name);
+ List<ResourceFile> matchingFiles =
+ scriptNameToFilesMap.computeIfAbsent(name, (n) -> new ArrayList<>());
matchingFiles.add(scriptFile);
markAnyDuplicates(matchingFiles);
@@ -449,14 +433,6 @@ public class GhidraScriptUtil {
return getScriptInfo(file); // this will cache the created info
}
- public static List<ResourceFile> getAllScripts() {
- List<ResourceFile> scriptList = new ArrayList<>();
- for (ResourceFile dirPath : scriptBundlePaths) {
- updateAvailableScriptFilesForDirectory(scriptList, dirPath);
- }
- return scriptList;
- }
-
private static void updateAvailableScriptFilesForDirectory(List<ResourceFile> scriptAccumulator,
ResourceFile directory) {
ResourceFile[] files = directory.listFiles();
@@ -492,12 +468,16 @@ public class GhidraScriptUtil {
*/
public static ScriptInfo getExistingScriptInfo(String scriptName) {
List<ResourceFile> matchingFiles = scriptNameToFilesMap.get(scriptName);
- if (matchingFiles.isEmpty()) {
+ if (matchingFiles == null || matchingFiles.isEmpty()) {
return null;
}
return scriptFileToInfoMap.get(matchingFiles.get(0));
}
+ public static ScriptInfo getExistingScriptInfo(ResourceFile script) {
+ return scriptFileToInfoMap.get(script);
+ }
+
/**
* Runs the specified script with the specified state
*
@@ -537,7 +517,6 @@ public class GhidraScriptUtil {
*/
public static void refreshDuplicates() {
scriptNameToFilesMap.values().forEach(files -> {
-
boolean isDuplicate = files.size() > 1;
files.forEach(file -> scriptFileToInfoMap.get(file).setDuplicate(isDuplicate));
});
diff --git a/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java b/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java
index 7733c93d63..852c3027d0 100644
--- a/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java
+++ b/Ghidra/Features/Base/src/main/java/ghidra/app/script/JavaScriptProvider.java
@@ -61,6 +61,7 @@ public class JavaScriptProvider extends GhidraScriptProvider {
@Override
public GhidraScript getScriptInstance(ResourceFile sourceFile, PrintWriter writer)
throws ClassNotFoundException, InstantiationException, IllegalAccessException {
+ ScriptInfo info = GhidraScriptUtil.getScriptInfo(sourceFile);
try {
Class<?> clazz = loadClass(sourceFile, writer);
Object object;
@@ -75,13 +76,16 @@ public class JavaScriptProvider extends GhidraScriptProvider {
String message = "Not a valid Ghidra script: " + sourceFile.getName();
writer.println(message);
Msg.error(this, message);
+ info.setCompileErrors(true);
return null; // class is not GhidraScript
}
catch (ClassNotFoundException | InstantiationException | IllegalAccessException e) {
+ info.setCompileErrors(true);
throw e;
}
catch (Exception e) {
+ info.setCompileErrors(true);
throw new ClassNotFoundException("", e);
}
}
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/AbstractGhidraScriptMgrPluginTest.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/AbstractGhidraScriptMgrPluginTest.java
index bfecd10a33..b4ed0bca65 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/AbstractGhidraScriptMgrPluginTest.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/AbstractGhidraScriptMgrPluginTest.java
@@ -20,8 +20,7 @@ import static org.junit.Assert.*;
import java.awt.Window;
import java.io.*;
import java.lang.reflect.InvocationTargetException;
-import java.nio.file.Files;
-import java.nio.file.Path;
+import java.nio.file.*;
import java.util.*;
import java.util.concurrent.CountDownLatch;
import java.util.concurrent.TimeUnit;
@@ -170,6 +169,10 @@ public abstract class AbstractGhidraScriptMgrPluginTest
env.dispose();
}
+ static protected void wipe(ResourceFile path) throws IOException {
+ wipe(Paths.get(path.getAbsolutePath()));
+ }
+
static protected void wipe(Path path) throws IOException {
if (Files.exists(path)) {
for (Path p : (Iterable<Path>) Files.walk(path).sorted(
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin2Test.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin2Test.java
index 88fc7a46f2..893069bcdd 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin2Test.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin2Test.java
@@ -23,9 +23,10 @@ import org.apache.logging.log4j.*;
import org.apache.logging.log4j.core.config.Configurator;
import org.junit.Test;
+import docking.test.AbstractDockingTest;
import docking.widgets.table.SelectionManager;
import generic.jar.ResourceFile;
-import generic.test.AbstractGenericTest;
+import generic.test.AbstractGTest;
import ghidra.app.plugin.core.osgi.GhidraSourceBundle;
import ghidra.app.script.GhidraScriptUtil;
import ghidra.app.script.JavaScriptProvider;
@@ -211,7 +212,7 @@ public class GhidraScriptMgrPlugin2Test extends AbstractGhidraScriptMgrPluginTes
//
// create a user-defined directory
- File tempDir = new File(AbstractGenericTest.getTestDirectoryPath());
+ File tempDir = new File(AbstractGTest.getTestDirectoryPath());
File tempScriptDir = new File(tempDir, "TestScriptDir");
FileUtilities.deleteDir(tempScriptDir);
tempScriptDir.mkdir();
@@ -251,7 +252,7 @@ public class GhidraScriptMgrPlugin2Test extends AbstractGhidraScriptMgrPluginTes
chooseJavaProvider();
- SaveDialog sd = env.waitForDialogComponent(SaveDialog.class, MAX_TIME);
+ SaveDialog sd = AbstractDockingTest.waitForDialogComponent(SaveDialog.class);
pressButtonByText(sd, "OK");
refreshProvider();
diff --git a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin3Test.java b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin3Test.java
index f5ec0baafb..c9bf8a7bd8 100644
--- a/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin3Test.java
+++ b/Ghidra/Features/Base/src/test.slow/java/ghidra/app/plugin/core/script/GhidraScriptMgrPlugin3Test.java
@@ -281,13 +281,14 @@ public class GhidraScriptMgrPlugin3Test extends AbstractGhidraScriptMgrPluginTes
performAction(bundleStatusAction, false);
waitForSwing();
- final BundleStatusProvider bundleStatusProvider = waitForComponentProvider(BundleStatusProvider.class);
+ final BundleStatusProvider bundleStatusProvider =
+ waitForComponentProvider(BundleStatusProvider.class);
final ResourceFile dir = new ResourceFile(getTestDirectoryPath() + "/test_scripts");
dir.getFile(false).mkdirs();
SwingUtilities.invokeLater(() -> {
- bundleStatusProvider.getModel().insertPathForTesting(dir.getAbsolutePath());
+ bundleStatusProvider.getModel().enablePath(dir);
});
waitForSwing();
@@ -328,10 +329,11 @@ public class GhidraScriptMgrPlugin3Test extends AbstractGhidraScriptMgrPluginTes
assertTrue(newScript.exists());
assertNotNull(newScript);
- assertEquals(dir.getAbsolutePath(), newScript.getParentFile().getFile(false).getAbsolutePath());
+ assertEquals(dir.getAbsolutePath(),
+ newScript.getParentFile().getFile(false).getAbsolutePath());
- newScript.delete();
- dir.delete();
+ deleteFile(newScript);
+ deleteFile(dir);
waitForSwing();
}