MCPcopy Create free account
hub / github.com/Vector35/binaryninja-api / GetPrimaryFilePath

Method GetPrimaryFilePath

view/sharedcache/core/SharedCacheView.cpp:976–1033  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

974}
975
976std::optional<std::string> SharedCacheView::GetPrimaryFilePath()
977{
978 auto viewFile = GetFile();
979 // 1. Try and get the primary file path using `GetOriginalFilename`.
980 auto primaryFilePath = viewFile->GetOriginalFilename();
981
982 // 2. If the original file name is not a usable file path then prompt the user to select one.
983 if (primaryFilePath.empty() || !std::filesystem::exists(primaryFilePath))
984 {
985 if (!GetOpenFileNameInput(primaryFilePath, "Please select the primary shared cache file"))
986 return std::nullopt;
987 SetPrimaryFileName(BaseFileName(primaryFilePath));
988 // Update so next load we don't need to prompt the user.
989 viewFile->SetOriginalFilename(primaryFilePath);
990 }
991
992 // 3. If we are not in a project, we can go ahead and return the file path, it does not need to be resolved from project.
993 auto primaryProjectFile = viewFile->GetProjectFile();
994 if (!primaryProjectFile)
995 return primaryFilePath;
996
997 auto project = primaryProjectFile->GetProject();
998 auto primaryProjectFileName = primaryProjectFile->GetName();
999 auto primaryProjectFilePath = primaryProjectFile->GetPathOnDisk();
1000
1001 // 4. If we are not a BNDB project file than we can return the path on disk as we are the primary file.
1002 if (primaryProjectFileName.find(".bndb") == std::string::npos)
1003 {
1004 // Set the primary file name to the project file name so on subsequent loads we can pick it up.
1005 SetPrimaryFileName(primaryProjectFileName);
1006 return primaryProjectFilePath;
1007 }
1008
1009 // 5. If we are a BNDB project file the path must be resolved from the file name.
1010 auto primaryProjectFileFolder = primaryProjectFile->GetFolder();
1011 for (const auto& pj : project->GetFiles())
1012 {
1013 // Skip files not in the same folder.
1014 if (!IsSameFolder(pj->GetFolder(), primaryProjectFileFolder))
1015 continue;
1016 // We are looking for the file with file name we stored in metadata.
1017 if (pj->GetName() != m_primaryFileName)
1018 continue;
1019 return pj->GetPathOnDisk();
1020 }
1021
1022 // 6. If we fail to resolve the project file given the `m_primaryFileName` than we fall back to asking the user.
1023 std::string newPrimaryFilePath;
1024 if (!GetOpenFileNameInput(newPrimaryFilePath, "Please select the primary shared cache file"))
1025 return std::nullopt;
1026
1027 // TODO: We likely want to verify that the project file exists in the same directory as the BNDB.
1028 // TODO: We currently require the database to exist in the same directory as the files.
1029 // Update the primary file name for later loads, otherwise we would keep prompting to select a file.
1030 primaryProjectFile = project->GetFileByPathOnDisk(newPrimaryFilePath);
1031 SetPrimaryFileName(primaryProjectFile->GetName());
1032 return newPrimaryFilePath;
1033}

Callers

nothing calls this directly

Calls 13

BaseFileNameFunction · 0.85
IsSameFolderFunction · 0.85
GetOriginalFilenameMethod · 0.80
SetOriginalFilenameMethod · 0.80
GetProjectFileMethod · 0.80
GetPathOnDiskMethod · 0.80
findMethod · 0.80
GetFileByPathOnDiskMethod · 0.80
emptyMethod · 0.45
GetProjectMethod · 0.45
GetNameMethod · 0.45
GetFolderMethod · 0.45

Tested by

no test coverage detected