Class FileCache
- java.lang.Object
-
- ghidra.formats.gfilesystem.FileCache
-
public class FileCache extends java.lang.ObjectFile caching implementation.Caches files based on a hash of the contents of the file.
Files are retrieved using the hash string.Cached files are stored in a file with a name that is the hex encoded value of the hash. Cached files are organized into a nested directory structure to prevent overwhelming a single directory with thousands of files.
Nested directory structure is based on the file's name: File: AABBCCDDEEFF... Directory (2 level nesting): AA/BB/AABBCCDDEEFF...
Cache size is not bounded.
Cache maint is done during startup if interval since last maint has been exceeded
No file data is maintained in memory.
No file is moved or removed from the cache after being added (except during startup) as there is no use count or reference tracking of the files.
-
-
Field Summary
Fields Modifier and Type Field Description static intMD5_HEXSTR_LEN
-
Method Summary
All Methods Instance Methods Concrete Methods Modifier and Type Method Description FileCacheEntryaddFile(java.io.File f, TaskMonitor monitor)Adds aFileto the cache, returning aFileCacheEntry.FileCacheEntryaddStream(java.io.InputStream is, TaskMonitor monitor)Adds a contents of a stream to the cache, returning the md5 identifier of the stream.FileCacheEntrygetFile(java.lang.String md5)Returns aFileCacheEntryfor the matching file, based on its MD5, or NULL if there is no matching file.intgetFileAddCount()Number of files added to this cache.intgetFileReUseCount()Number of times a file-add was a no-op and the contents were already present in the cache.longgetMaxFileAgeMS()How old (in milliseconds) files must be before being aged-off during cache maintenance.longgetStorageEstimateBytes()Estimate of the number of bytes in the cache.voidpurge()Deletes all stored files from this file cache that are under a "NN" two hex digit nesting dir.FileCacheEntrypushStream(DerivedFilePushProducer pusher, TaskMonitor monitor)Adds a file to the cache, using a 'pusher' strategy where the producer is given aOutputStreamto write to.java.lang.StringtoString()
-
-
-
Field Detail
-
MD5_HEXSTR_LEN
public static final int MD5_HEXSTR_LEN
- See Also:
- Constant Field Values
-
-
Constructor Detail
-
FileCache
public FileCache(java.io.File cacheDir) throws java.io.IOExceptionCreates a newFileCacheinstance where files are stored under the specifiedcacheDir- Parameters:
cacheDir- where to store the files- Throws:
java.io.IOException- if there was a problem creating subdirectories under cacheDir or when pruning expired files.
-
-
Method Detail
-
purge
public void purge()
Deletes all stored files from this file cache that are under a "NN" two hex digit nesting dir.Will cause other processes which are accessing or updating the cache to error.
-
addFile
public FileCacheEntry addFile(java.io.File f, TaskMonitor monitor) throws java.io.IOException, CancelledException
Adds aFileto the cache, returning aFileCacheEntry.- Parameters:
f-Fileto add to cache.monitor-TaskMonitorto monitor for cancel and to update progress.- Returns:
FileCacheEntrywith new File and md5.- Throws:
java.io.IOException- if errorCancelledException- if canceled
-
getFile
public FileCacheEntry getFile(java.lang.String md5)
Returns aFileCacheEntryfor the matching file, based on its MD5, or NULL if there is no matching file.Tweaks the file's last modified time to implement a LRU.
- Parameters:
md5- md5 string.- Returns:
FileCacheEntrywith a File and it's md5 string ornullif no matching file exists in cache.
-
addStream
public FileCacheEntry addStream(java.io.InputStream is, TaskMonitor monitor) throws java.io.IOException, CancelledException
Adds a contents of a stream to the cache, returning the md5 identifier of the stream.The stream is copied into a temp file in the cacheDir/new directory while its md5 is calculated. The temp file is then moved into its final location based on the md5 of the stream: AA/BB/CC/AABBCCDDEEFF....
The monitor progress is updated with the number of bytes that are being copied. No message or maximum is set.
- Parameters:
is-InputStreamto add to the cache. Not closed when done.monitor-TaskMonitorthat will be checked for canceling and updating progress.- Returns:
FileCacheEntrywith file info and md5, never null.- Throws:
java.io.IOException- if errorCancelledException- if canceled
-
pushStream
public FileCacheEntry pushStream(DerivedFilePushProducer pusher, TaskMonitor monitor) throws java.io.IOException, CancelledException
Adds a file to the cache, using a 'pusher' strategy where the producer is given aOutputStreamto write to.Unbeknownst to the producer, but knownst to us, the outputstream is really a
HashingOutputStreamthat will allow us to get the MD5 hash when the producer is finished pushing.- Parameters:
pusher- functional callback that will accept anOutputStreamand write to it.(os) -> { os.write(.....); }monitor-TaskMonitorthat will be checked for cancel and updated with file io progress.- Returns:
- a new
FileCacheEntrywith the newly added cache file's File and MD5, never null. - Throws:
java.io.IOException- if an IO errorCancelledException- if the user cancels
-
toString
public java.lang.String toString()
- Overrides:
toStringin classjava.lang.Object
-
getFileAddCount
public int getFileAddCount()
Number of files added to this cache.- Returns:
- Number of files added to this cache
-
getFileReUseCount
public int getFileReUseCount()
Number of times a file-add was a no-op and the contents were already present in the cache.- Returns:
- Number of times a file-add was a no-op and the contents were already present in the cache.
-
getStorageEstimateBytes
public long getStorageEstimateBytes()
Estimate of the number of bytes in the cache.- Returns:
- estimate of the number of bytes in the cache - could be very wrong
-
getMaxFileAgeMS
public long getMaxFileAgeMS()
How old (in milliseconds) files must be before being aged-off during cache maintenance.- Returns:
- Max cache file age in milliseconds.
-
-