Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 3 additions & 1 deletion binaryninjaapi.h
Original file line number Diff line number Diff line change
Expand Up @@ -2878,6 +2878,7 @@ namespace BinaryNinja {

Ref<Project> GetProject() const;
std::string GetPathOnDisk() const;
std::string GetPathInProject() const;
bool ExistsOnDisk() const;
std::string GetName() const;
std::string GetDescription() const;
Expand Down Expand Up @@ -2937,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);

Expand Down
2 changes: 2 additions & 0 deletions binaryninjacore.h
Original file line number Diff line number Diff line change
Expand Up @@ -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);
Expand All @@ -4037,6 +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(const BNProjectFile* file);
BINARYNINJACOREAPI bool BNProjectFileExistsOnDisk(BNProjectFile* file);
BINARYNINJACOREAPI char* BNProjectFileGetName(BNProjectFile* file);
BINARYNINJACOREAPI bool BNProjectFileSetName(BNProjectFile* file, const char* name);
Expand Down
13 changes: 11 additions & 2 deletions examples/triage/fileinfo.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -72,8 +72,17 @@ FileInfoWidget::FileInfoWidget(QWidget* parent, BinaryViewRef bv)
this->m_layout->setVerticalSpacing(1);

const auto view = bv->GetParentView() ? bv->GetParentView() : bv;
const auto filePath = bv->GetFile()->GetOriginalFilename();
this->addCopyableField("Path: ", filePath.c_str());

const auto file = bv->GetFile();
const auto filePath = file->GetOriginalFilename();
this->addCopyableField("Path on disk: ", filePath.c_str());

// If triage view is opened from a project, show both actual filepath and path relative to project
if (const auto fileProjectRef = file->GetProjectFile())
{
const auto projectFilePath = file->GetProjectFile()->GetPathInProject();
this->addCopyableField("Path in project: ", projectFilePath.c_str());
}

const auto fileSize = QString::number(view->GetLength(), 16).prepend("0x");
this->addCopyableField("Size: ", fileSize);
Expand Down
29 changes: 28 additions & 1 deletion project.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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);
Expand Down Expand Up @@ -552,6 +571,14 @@ std::string ProjectFile::GetPathOnDisk() const
return result;
}

std::string ProjectFile::GetPathInProject() const
{
char* path = BNProjectFileGetPathInProject(m_object);
std::string result = path;
BNFreeString(path);
return result;
}


bool ProjectFile::ExistsOnDisk() const
{
Expand Down
53 changes: 53 additions & 0 deletions python/project.py
Original file line number Diff line number Diff line change
Expand Up @@ -190,6 +190,22 @@ def export(self, dest: AsPath) -> bool:
"""
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:
"""
Expand Down Expand Up @@ -650,6 +666,43 @@ def get_file_by_id(self, id: str) -> Optional[ProjectFile]:
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
Expand Down
3 changes: 2 additions & 1 deletion rust/tests/binary_view.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
Loading