summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorAlexander Khosrowshahi <alexk@vector35.com>2025-07-03 13:53:40 -0400
committerAlexander Khosrowshahi <alexk@vector35.com>2025-07-03 13:53:40 -0400
commit533b8e546a00537d6945f5329decab4b74a5bf09 (patch)
treefc4ee45f61ded3eeaf746f8828b2cc0373dd31a9
parentdd5009bd17bc0d814c06d3a6962929084878f350 (diff)
Add get_files_by_path_in_project and get_path_in_project to C++ and python APIs
-rw-r--r--binaryninjaapi.h3
-rw-r--r--binaryninjacore.h3
-rw-r--r--project.cpp21
-rw-r--r--python/project.py53
-rw-r--r--rust/tests/binary_view.rs3
5 files changed, 79 insertions, 4 deletions
diff --git a/binaryninjaapi.h b/binaryninjaapi.h
index 0c86e026..2d21ef6d 100644
--- a/binaryninjaapi.h
+++ b/binaryninjaapi.h
@@ -2938,7 +2938,8 @@ namespace BinaryNinja {
Ref<ProjectFile> CreateFileUnsafe(const std::vector<uint8_t>& contents, Ref<ProjectFolder> folder, const std::string& name, const std::string& description, const std::string& id, int64_t creationTimestamp, const ProgressFunction& progressCallback = {});
std::vector<Ref<ProjectFile>> GetFiles() const;
Ref<ProjectFile> GetFileById(const std::string& id) const;
- Ref<ProjectFile> GetFileByPathOnDisk(const std::string& path);
+ Ref<ProjectFile> GetFileByPathOnDisk(const std::string& path) const;
+ std::vector<Ref<ProjectFile>> GetFilesByPathInProject(const std::string& path) const;
void PushFile(Ref<ProjectFile> file);
bool DeleteFile_(Ref<ProjectFile> file);
diff --git a/binaryninjacore.h b/binaryninjacore.h
index e25b1b8f..9e816a8a 100644
--- a/binaryninjacore.h
+++ b/binaryninjacore.h
@@ -4013,6 +4013,7 @@ extern "C"
BINARYNINJACOREAPI BNProjectFile** BNProjectGetFiles(BNProject* project, size_t* count);
BINARYNINJACOREAPI BNProjectFile* BNProjectGetFileById(BNProject* project, const char* id);
BINARYNINJACOREAPI BNProjectFile* BNProjectGetFileByPathOnDisk(BNProject* project, const char* path);
+ BINARYNINJACOREAPI BNProjectFile** BNProjectGetFilesByPathInProject(BNProject* project, const char* path, size_t* count);
BINARYNINJACOREAPI void BNProjectPushFile(BNProject* project, BNProjectFile* file);
BINARYNINJACOREAPI bool BNProjectDeleteFile(BNProject* project, BNProjectFile* file);
@@ -4037,7 +4038,7 @@ extern "C"
BINARYNINJACOREAPI void BNFreeProjectFile(BNProjectFile* file);
BINARYNINJACOREAPI void BNFreeProjectFileList(BNProjectFile** files, size_t count);
BINARYNINJACOREAPI char* BNProjectFileGetPathOnDisk(BNProjectFile* file);
- BINARYNINJACOREAPI char* BNProjectFileGetPathInProject(BNProjectFile* file);
+ BINARYNINJACOREAPI char* BNProjectFileGetPathInProject(const BNProjectFile* file);
BINARYNINJACOREAPI bool BNProjectFileExistsOnDisk(BNProjectFile* file);
BINARYNINJACOREAPI char* BNProjectFileGetName(BNProjectFile* file);
BINARYNINJACOREAPI bool BNProjectFileSetName(BNProjectFile* file, const char* name);
diff --git a/project.cpp b/project.cpp
index 6cb76a02..e795db61 100644
--- a/project.cpp
+++ b/project.cpp
@@ -477,7 +477,7 @@ Ref<ProjectFile> Project::GetFileById(const std::string& id) const
}
-Ref<ProjectFile> Project::GetFileByPathOnDisk(const std::string& path)
+Ref<ProjectFile> Project::GetFileByPathOnDisk(const std::string& path) const
{
BNProjectFile* file = BNProjectGetFileByPathOnDisk(m_object, path.c_str());
if (file == nullptr)
@@ -486,6 +486,25 @@ Ref<ProjectFile> Project::GetFileByPathOnDisk(const std::string& path)
}
+std::vector<Ref<ProjectFile>> Project::GetFilesByPathInProject(
+ const std::string& path
+) const
+{
+ size_t count;
+ BNProjectFile** files = BNProjectGetFilesByPathInProject(m_object, path.c_str(), &count);
+
+ std::vector<Ref<ProjectFile>> result;
+ result.reserve(count);
+ for (size_t i = 0; i < count; i++)
+ {
+ result.emplace_back(new ProjectFile(BNNewProjectFileReference(files[i])));
+ }
+
+ BNFreeProjectFileList(files, count);
+ return result;
+}
+
+
void Project::PushFile(Ref<ProjectFile> file)
{
BNProjectPushFile(m_object, file->m_object);
diff --git a/python/project.py b/python/project.py
index 4357f276..c8f87d2f 100644
--- a/python/project.py
+++ b/python/project.py
@@ -190,6 +190,22 @@ class ProjectFile:
"""
return core.BNProjectFileExport(self._handle, str(dest))
+ def get_path_on_disk(self) -> Optional[str]:
+ """
+ Get this file's path on disk
+
+ :return: The path on disk of the file or None
+ """
+ return core.BNProjectFileGetPathOnDisk(self._handle)
+
+ def get_path_in_project(self) -> Optional[str]:
+ """
+ Get this file's path in its parent project
+
+ :return: The path on disk of the file or None
+ """
+ return core.BNProjectFileGetPathInProject(self._handle)
+
class ProjectFolder:
"""
@@ -650,6 +666,43 @@ class Project:
file = ProjectFile(handle)
return file
+ def get_file_by_path_on_disk(self, path: str) -> Optional[ProjectFile]:
+ """
+ Retrieve a file in the project by its path on disk
+
+ :param path: Path of the file on the disk
+ :return: File with the requested path or None
+ """
+ handle = core.BNProjectGetFileByPathOnDisk(self._handle, path)
+ if handle is None:
+ return None
+ file = ProjectFile(handle)
+ return file
+
+ def get_files_by_path_in_project(self, path: str) -> List[ProjectFile]:
+ """
+ Retrieve a file(s) by path in the project
+ Note that files in a project can share names and paths within the project
+ but are uniquely identified by a disk path or id.
+
+ :param path: Path of the file(s) in the project, separate from their path on disk.
+ :return: List of files with the requested path
+ """
+ count = ctypes.c_size_t()
+ value = core.BNProjectGetFilesByPathInProject(self._handle, path, count)
+ if value is None:
+ raise ProjectException("Failed to get list of project files by path in project")
+ result = []
+ try:
+ for i in range(count.value):
+ file_handle = core.BNNewProjectFileReference(value[i])
+ if file_handle is None:
+ raise ProjectException("core.BNNewProjectFileReference returned None")
+ result.append(ProjectFile(file_handle))
+ return result
+ finally:
+ core.BNFreeProjectFileList(value, count.value)
+
def delete_file(self, file: ProjectFile) -> bool:
"""
Delete a file from the project
diff --git a/rust/tests/binary_view.rs b/rust/tests/binary_view.rs
index 2db96d61..5e8206e0 100644
--- a/rust/tests/binary_view.rs
+++ b/rust/tests/binary_view.rs
@@ -106,7 +106,8 @@ fn test_binary_tags() {
let view = binaryninja::load(out_dir.join("atox.obj")).expect("Failed to create view");
let tag_ty = view.create_tag_type("Test", "");
view.add_tag(0x0, &tag_ty, "t", false);
- view.tag_type_by_name("Test").expect("Failed to get tag type");
+ view.tag_type_by_name("Test")
+ .expect("Failed to get tag type");
}
// These are the target files present in OUT_DIR