Skip to content

Commit aab4142

Browse files
committed
Updates to only use local store dir
1 parent e9941fd commit aab4142

File tree

3 files changed

+25
-74
lines changed

3 files changed

+25
-74
lines changed

lib/msf/core/modules/metadata/store.rb

Lines changed: 19 additions & 64 deletions
Original file line numberDiff line numberDiff line change
@@ -127,18 +127,21 @@ def load_cache_from_file_store
127127
}
128128
end
129129

130-
# This method uses a per-file CRC32 cache to avoid recalculating checksums for files that have not changed.
131-
# It loads the cache, checks each file's mtime and size, and only recalculates the CRC32 if needed.
130+
# This method checks if the current module and library files match the cached checksum.
131+
# It uses a per-file CRC32 cache to avoid recalculating checksums for files that haven't changed.
132+
# If no cache exists, it will create one in the user's directory.
132133
#
133134
# @return [Boolean] True if the current checksum matches the cached one
134135
def self.valid_checksum?
135136
current_checksum = get_current_checksum
136-
137-
get_store_cache_path
138-
ensure_cache_file_exists(current_checksum)
139-
140137
cached_sha = get_cached_checksum
141138

139+
# If no cached checksum exists, create the cache file with current checksum
140+
if cached_sha.nil?
141+
create_or_update_cache_file(get_cache_path, current_checksum)
142+
return false
143+
end
144+
142145
checksums_match?(current_checksum, cached_sha)
143146
end
144147

@@ -218,18 +221,12 @@ def self.get_per_file_cache_path
218221
File.join(Msf::Config.config_directory, 'store', 'per_file_metadata_cache.json')
219222
end
220223

221-
# Get the path to the cache store file
222-
# @return [String] Path to the cache store file
223-
def self.get_store_cache_path
224+
# Get the path to the cache file
225+
# @return [String] Path to the cache file
226+
def self.get_cache_path
224227
File.join(Msf::Config.config_directory, "store", CacheMetaDataFile)
225228
end
226229

227-
# Get the path to the DB cache file
228-
# @return [String] Path to the DB cache file
229-
def self.get_db_cache_path
230-
File.join(Msf::Config.install_root, "db", CacheMetaDataFile)
231-
end
232-
233230
# Load the per-file cache from disk
234231
# @param [String] cache_file Path to the cache file
235232
# @return [Hash] The loaded cache or an empty hash if the file doesn't exist
@@ -272,47 +269,17 @@ def self.create_or_update_cache_file(file_path, checksum)
272269
File.write(file_path, JSON.pretty_generate(cache_content))
273270
end
274271

275-
# Ensure the db cache file exists, creating it if necessary
276-
# @param [String] current_checksum The current checksum to use if creating a new cache file
277-
# @return [void]
278-
def self.ensure_cache_file_exists(current_checksum)
279-
# Path to the DB cache file
280-
cache_db_path = get_db_cache_path
281-
282-
# Only create the db cache file if it doesn't exist
283-
# The user's cache file (~/.msf4/store/cache_metadata_base.json) should only be created when changes are made
284-
unless File.exist?(cache_db_path)
285-
# Ensure directory exists
286-
FileUtils.mkdir_p(File.dirname(cache_db_path))
287-
cache_content = {
288-
"checksum" => {
289-
"crc32" => current_checksum
290-
}
291-
}
292-
File.write(cache_db_path, JSON.pretty_generate(cache_content))
293-
end
294-
end
295-
296-
# Get the cached checksum value without creating any new files
272+
# Get the cached checksum value from the user's cache file
297273
# @return [String, nil] The cached checksum value or nil if no cache exists
298274
def self.get_cached_checksum
299-
cache_store_path = get_store_cache_path
300-
cache_db_path = get_db_cache_path
275+
cache_path = get_cache_path
301276

302-
# First try user's cache file
303-
if File.exist?(cache_store_path)
304-
cache_content = JSON.parse(File.read(cache_store_path))
277+
if File.exist?(cache_path)
278+
cache_content = JSON.parse(File.read(cache_path))
305279
return cache_content.dig('checksum', 'crc32')
306280
end
307281

308-
# Fall back to db cache file
309-
if File.exist?(cache_db_path)
310-
cache_content = JSON.parse(File.read(cache_db_path))
311-
return cache_content.dig('checksum', 'crc32')
312-
end
313-
314-
# If neither exists, return nil to trigger a cache rebuild
315-
# This allows the build process to work with neither file present
282+
# If no cache exists, return nil to trigger creation of a new cache file
316283
nil
317284
end
318285

@@ -321,19 +288,7 @@ def self.get_cached_checksum
321288
# @param [String] current_checksum The current checksum to store in the cache
322289
# @return [void]
323290
def self.update_cache_checksum(current_checksum)
324-
cache_store_path = get_store_cache_path
325-
cache_db_path = get_db_cache_path
326-
327-
if File.exist?(cache_store_path)
328-
# Update the existing user cache file
329-
create_or_update_cache_file(cache_store_path, current_checksum)
330-
elsif File.exist?(cache_db_path)
331-
# Copy the DB cache file to the user's directory and update it
332-
FileUtils.cp(cache_db_path, cache_store_path)
333-
create_or_update_cache_file(cache_store_path, current_checksum)
334-
else
335-
# Create a new cache file if neither exists
336-
create_or_update_cache_file(cache_store_path, current_checksum)
337-
end
291+
cache_path = get_cache_path
292+
create_or_update_cache_file(cache_path, current_checksum)
338293
end
339294
end

lib/msf/ui/console/driver.rb

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -163,19 +163,16 @@ def initialize(prompt = DefaultPrompt, prompt_char = DefaultPromptChar, opts = {
163163
self.framework.init_module_paths(module_paths: opts['ModulePath'], defer_module_loads: opts['DeferModuleLoads'])
164164
end
165165

166-
# If the module cache is invalid, we need to invalidate it and update the cache.
167-
# We will also need to set 'DeferModuleLoads' to false so that the modules are reloaded.
168-
unless Msf::Modules::Metadata::Store.valid_checksum?
169-
# Get the current checksum and update the cache with it
166+
has_modified_metasploit_files = !Msf::Modules::Metadata::Store.valid_checksum?
167+
168+
# Update the cache checksum if files have been modified
169+
if has_modified_metasploit_files
170170
current_checksum = Msf::Modules::Metadata::Store.get_current_checksum
171171
Msf::Modules::Metadata::Store.update_cache_checksum(current_checksum)
172-
173-
framework.threads.spawn("ModuleCacheRebuild", true) do
174-
framework.modules.refresh_cache_from_module_files
175-
end
176172
end
177173

178-
unless opts['DeferModuleLoads']
174+
# Refresh module cache if modules are modified, or we're not deferring loads
175+
if has_modified_metasploit_files || !opts['DeferModuleLoads']
179176
framework.threads.spawn("ModuleCacheRebuild", true) do
180177
framework.modules.refresh_cache_from_module_files
181178
end

tools/automation/cache/build_new_cache.sh

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,6 @@
11
#!/bin/sh -ex
22
bundle install
33
rm -f db/modules_metadata_base.json
4-
rm -f db/cache_metadata_base.json
54
git ls-files modules/ -z | xargs -0 -n1 -P `nproc` -I{} -- git log -1 --format="%ai {}" {} | while read -r udate utime utz ufile ; do
65
touch -d "$udate $utime" $ufile
76
done

0 commit comments

Comments
 (0)