Skip to content

Ghidra is a software reverse engineering (SRE) framework

GP-5444: PyGhidra open_program() now handles locked projects better

Ryan Kurtzabb65d1b8cd1
diff --git a/Ghidra/Features/PyGhidra/src/main/py/README.md b/Ghidra/Features/PyGhidra/src/main/py/README.md
index 3561e6379d..9af09ff411 100644
--- a/Ghidra/Features/PyGhidra/src/main/py/README.md
+++ b/Ghidra/Features/PyGhidra/src/main/py/README.md
@@ -306,6 +306,10 @@ import pdb # imports Python's pdb
import pdb_ # imports Ghidra's pdb
```
## Change History
+__2.0.2:__
+* [`pyghidra.open_program()`](#pyghidraopen_program) now properly throws an exception if the project
+ exists and is locked.
+
__2.0.1:__
* PyGhidra now respects the `application.settingsdir` property set in Ghidra's `launch.properties`
file.
diff --git a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/__init__.py b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/__init__.py
index 79dd7071a4..e33ceab124 100644
--- a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/__init__.py
+++ b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/__init__.py
@@ -13,7 +13,7 @@
# See the License for the specific language governing permissions and
# limitations under the License.
##
-__version__ = "2.0.1"
+__version__ = "2.0.2"
# stub for documentation and typing
# this is mostly to hide the function parameter
diff --git a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/core.py b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/core.py
index c971e72211..9674a4d075 100644
--- a/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/core.py
+++ b/Ghidra/Features/PyGhidra/src/main/py/src/pyghidra/core.py
@@ -85,7 +85,7 @@ def _setup_project(
) -> Tuple["GhidraProject", "Program"]:
from ghidra.base.project import GhidraProject
from java.lang import ClassLoader # type:ignore @UnresolvedImport
- from java.io import IOException # type:ignore @UnresolvedImport
+ from ghidra.framework.model import ProjectLocator # type:ignore @UnresolvedImport
if binary_path is not None:
binary_path = Path(binary_path)
if project_location:
@@ -95,7 +95,6 @@ def _setup_project(
if not project_name:
project_name = f"{binary_path.name}_ghidra"
project_location /= project_name
- project_location.mkdir(exist_ok=True, parents=True)
if isinstance(loader, str):
from java.lang import ClassNotFoundException # type:ignore @UnresolvedImport
@@ -113,13 +112,14 @@ def _setup_project(
# Open/Create project
program: "Program" = None
- try:
+ if ProjectLocator(project_location, project_name).exists():
project = GhidraProject.openProject(project_location, project_name, True)
- if binary_path is not None:
- if project.getRootFolder().getFile(binary_path.name):
- program = project.openProgram("/", binary_path.name, False)
- except IOException:
- project = GhidraProject.createProject(project_location, project_name, False)
+ else:
+ project_location.mkdir(exist_ok=True, parents=True)
+ project = GhidraProject.createProject(project_location, project_name, False)
+ if binary_path is not None:
+ if project.getRootFolder().getFile(binary_path.name):
+ program = project.openProgram("/", binary_path.name, False)
# NOTE: GhidraProject.importProgram behaves differently when a loader is provided
# loaderClass may not be null so we must use the correct method override