@@ -127,18 +127,21 @@ def load_cache_from_file_store
127
127
}
128
128
end
129
129
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.
132
133
#
133
134
# @return [Boolean] True if the current checksum matches the cached one
134
135
def self . valid_checksum?
135
136
current_checksum = get_current_checksum
136
-
137
- get_store_cache_path
138
- ensure_cache_file_exists ( current_checksum )
139
-
140
137
cached_sha = get_cached_checksum
141
138
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
+
142
145
checksums_match? ( current_checksum , cached_sha )
143
146
end
144
147
@@ -218,18 +221,12 @@ def self.get_per_file_cache_path
218
221
File . join ( Msf ::Config . config_directory , 'store' , 'per_file_metadata_cache.json' )
219
222
end
220
223
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
224
227
File . join ( Msf ::Config . config_directory , "store" , CacheMetaDataFile )
225
228
end
226
229
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
-
233
230
# Load the per-file cache from disk
234
231
# @param [String] cache_file Path to the cache file
235
232
# @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)
272
269
File . write ( file_path , JSON . pretty_generate ( cache_content ) )
273
270
end
274
271
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
297
273
# @return [String, nil] The cached checksum value or nil if no cache exists
298
274
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
301
276
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 ) )
305
279
return cache_content . dig ( 'checksum' , 'crc32' )
306
280
end
307
281
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
316
283
nil
317
284
end
318
285
@@ -321,19 +288,7 @@ def self.get_cached_checksum
321
288
# @param [String] current_checksum The current checksum to store in the cache
322
289
# @return [void]
323
290
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 )
338
293
end
339
294
end
0 commit comments