From 86019f825db516d8d5349c271f702dfe69b5fead Mon Sep 17 00:00:00 2001 From: Mason Reed Date: Thu, 10 Apr 2025 00:53:06 -0400 Subject: [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. --- view/sharedcache/core/SharedCacheBuilder.h | 44 ++++++++++++++++++++++++++++++ 1 file changed, 44 insertions(+) create mode 100644 view/sharedcache/core/SharedCacheBuilder.h (limited to 'view/sharedcache/core/SharedCacheBuilder.h') 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 m_view; + BinaryNinja::Ref 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 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 view); + + SharedCache& GetCache() { return m_cache; }; + std::set 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 folder); +}; -- cgit v1.3.1