summaryrefslogtreecommitdiff
path: root/view/sharedcache/core/SharedCacheBuilder.h
diff options
context:
space:
mode:
authorMason Reed <mason@vector35.com>2025-04-10 00:53:06 -0400
committerMason Reed <mason@vector35.com>2025-04-10 14:38:09 -0400
commit86019f825db516d8d5349c271f702dfe69b5fead (patch)
treeec91007fa0c857bfd54bb3d1d78ff55daf083928 /view/sharedcache/core/SharedCacheBuilder.h
parentb00d315524610d8c66af27619e87648bd5d63942 (diff)
[SharedCache] Make the shared cache file lookup more straightforward
We already had separated this part out into the `CacheProcessor` but its more like a builder than anything, so this refactors that out into a `SharedCacheBuilder`. - Separate out the `CacheProcessor` and simplify its implementation - Move more implementation specific stuff to the shared cache view - Prepare for more validation of the shared cache to detect irregular or incomplete shared cache information - Deduplicate a lot of file vs. project file lookup stuff - Stop copying all images when getting the number of images to log - Allow user to select another directory to scan for shared cache files, might make this a warning instead to prevent users from relying on this behavior We will likely follow this commit up soon with better open behavior, maybe open with options can specify a list of cache entry files? Shouldn't be too much effort aside from making the UI modal for that.
Diffstat (limited to 'view/sharedcache/core/SharedCacheBuilder.h')
-rw-r--r--view/sharedcache/core/SharedCacheBuilder.h44
1 files changed, 44 insertions, 0 deletions
diff --git a/view/sharedcache/core/SharedCacheBuilder.h b/view/sharedcache/core/SharedCacheBuilder.h
new file mode 100644
index 00000000..4a3ed1c4
--- /dev/null
+++ b/view/sharedcache/core/SharedCacheBuilder.h
@@ -0,0 +1,44 @@
+#pragma once
+
+#include "binaryninjaapi.h"
+#include "SharedCache.h"
+
+// This constructs a Cache, give it a file path, and it will add all relevant cache entries.
+class SharedCacheBuilder
+{
+ BinaryNinja::Ref<BinaryNinja::BinaryView> m_view;
+ BinaryNinja::Ref<BinaryNinja::Logger> m_logger;
+
+ // When we call `AddFile` it will start populating this caches entries.
+ // This cache is what is returned via `Finalize`.
+ SharedCache m_cache;
+
+ // List of already processedFiles so we skip adding them again.
+ std::set<std::string> m_processedFiles;
+ // The base file name (i.e. "dyld_shared_cache_arm64e"), this is used to filter out non-relevant files.
+ std::string m_primaryFileName;
+
+public:
+ explicit SharedCacheBuilder(BinaryNinja::Ref<BinaryNinja::BinaryView> view);
+
+ SharedCache& GetCache() { return m_cache; };
+ std::set<std::string> GetProcessedFiles() { return m_processedFiles; };
+ std::string GetPrimaryFileName() { return m_primaryFileName; };
+
+ // Set the base file name used when filtering in `AddFile`.
+ void SetPrimaryFileName(const std::string& baseFileName) { m_primaryFileName = baseFileName; };
+
+ // Returns a shared cache that is ready for processing, this should include all the required shared cache entries.
+ SharedCache Finalize();
+
+ // Tries to add the file to the shared cache, if the file has already been processed or is not valid
+ // then false will be returned, true if the file was added to the shared cache. A file can only be added once.
+ bool AddFile(
+ const std::string& filePath, const std::string& fileName, CacheEntryType cacheType = CacheEntryType::Secondary);
+
+ // Process a directory on the file system.
+ size_t AddDirectory(const std::string& directoryPath);
+
+ // Process a directory in a project.
+ size_t AddProjectFolder(BinaryNinja::Ref<BinaryNinja::ProjectFolder> folder);
+};