Class FileSystemIndexHelper<METADATATYPE>

  • Type Parameters:
    METADATATYPE - the filesystem specific native file object that the user of this class wants to be able to correlate with Ghidra GFile instances.

    public class FileSystemIndexHelper<METADATATYPE>
    extends java.lang.Object
    A helper class used by GFilesystem implementors to track mappings between GFile instances and the underlying container filesystem's native file objects.

    Threadsafe after initial use of storeFile() by the owning filesystem.

    This class also provides filename 'unique-ifying' (per directory) where an auto-incrementing number will be added to a file's filename if it is not unique in the directory.

    • Field Detail

      • directoryToListing

        protected java.util.Map<GFile,​java.util.Map<java.lang.String,​GFile>> directoryToListing
    • Constructor Detail

      • FileSystemIndexHelper

        public FileSystemIndexHelper​(GFileSystem fs,
                                     FSRLRoot fsFSRL)
        Creates a new FileSystemIndexHelper for the specified GFileSystem.

        A "root" directory GFile will be auto-created for the filesystem.

        Parameters:
        fs - the GFileSystem that this index will be for.
        fsFSRL - the fsrl of the filesystem itself. (this parameter is explicitly passed here so there is no possibility of trying to call back to the fs's GFileSystem.getFSRL() on a half-constructed filesystem.)
    • Method Detail

      • getRootDir

        public GFile getRootDir()
        Gets the root GFile object for this filesystem index.
        Returns:
        root GFile object.
      • clear

        public void clear()
        Removes all file info from this index.
      • getFileCount

        public int getFileCount()
        Number of files in this index.
        Returns:
        number of file in this index.
      • getMetadata

        public METADATATYPE getMetadata​(GFile f)
        Gets the opaque filesystem specific blob that was associated with the specified file.
        Parameters:
        f - GFile to look for.
        Returns:
        Filesystem specific blob associated with the specified file, or null if not found.
      • getListing

        public java.util.List<GFile> getListing​(GFile directory)
        Mirror's GFileSystem.getListing(GFile) interface.
        Parameters:
        directory - GFile directory to get the list of child files that have been added to this index, null means root directory.
        Returns:
        List of GFile files that are in the specified directory, never null.
      • lookup

        public GFile lookup​(java.lang.String path)
        Mirror's GFileSystem.lookup(String) interface.
        Parameters:
        path - path and filename of a file to find.
        Returns:
        GFile instance or null if no file was added to the index at that path.
      • storeFile

        public GFileImpl storeFile​(java.lang.String path,
                                   int fileIndex,
                                   boolean isDirectory,
                                   long length,
                                   METADATATYPE fileInfo)
        Creates and stores a file entry into in-memory indexes.

        The string path will be normalized to forward slashes before being split into directory components.

        Filenames that are not unique in their directory will have a "[nnn]" suffix added to the resultant GFile name, where nnn is the file's order of occurrence in the container file.

        Parameters:
        path - string path and filename of the file being added to the index. Back slashes are normalized to forward slashes.
        fileIndex - the filesystem specific unique index for this file, or -1 if not available.
        isDirectory - boolean true if the new file is a directory
        length - number of bytes in the file or -1 if not known or directory.
        fileInfo - opaque blob that will be stored and associated with the new GFile instance.
        Returns:
        new GFile instance.
      • storeFileWithParent

        public GFile storeFileWithParent​(java.lang.String filename,
                                         GFile parent,
                                         int fileIndex,
                                         boolean isDirectory,
                                         long length,
                                         METADATATYPE fileInfo)
        Creates and stores a file entry into in-memory indexes.

        Use this when you already know the parent directory GFile object.

        Filenames that are not unique in their directory will have a "[nnn]" suffix added to the resultant GFile name, where nnn is the file's order of occurrence in the container file.

        Parameters:
        filename - the new file's name
        parent - the new file's parent directory
        fileIndex - the filesystem specific unique index for this file, or -1 if not available.
        isDirectory - boolean true if the new file is a directory
        length - number of bytes in the file or -1 if not known or directory.
        fileInfo - opaque blob that will be stored and associated with the new GFile instance.
        Returns:
        new GFile instance.
      • getDirectoryContents

        protected java.util.Map<java.lang.String,​GFile> getDirectoryContents​(GFile directoryFile,
                                                                                   boolean createIfMissing)
        Returns a string->GFile map that holds the contents of a single directory.
        Parameters:
        directoryFile -
        Returns:
      • lookupParent

        protected GFile lookupParent​(java.lang.String[] nameparts)
        Walks a list of names of directories in nameparts (stopping prior to the last element) starting at the root of the filesystem and returns the final directory.

        Directories in a path that have not been encountered before (ie. a file's path references a directory that hasn't been mentioned yet as its own file entry) will have a stub entry GFile created for them.

        Superfluous slashes in the original filename (ie. name/sub//subafter_extra_slash) will be represented as empty string elements in the nameparts array and will be skipped as if they were not there.

        Parameters:
        nameparts -
        Returns:
      • createNewFile

        protected GFileImpl createNewFile​(GFile parentFile,
                                          java.lang.String name,
                                          boolean isDirectory,
                                          long size,
                                          METADATATYPE metadata)
        Creates a new GFile instance, using per-filesystem custom logic.

        Parameters:
        parentFile - the parent file of the new instance. Never null.
        name - the name of the file
        isDirectory - is this is file or directory?
        size - length of the file data
        metadata - filesystem specific BLOB that may have data that this method needs to create the new GFile instance. Can be null if this method is being called to create a missing directory that was referenced in a filename.
        Returns:
        new GFileImpl instance
      • toString

        public java.lang.String toString()
        Overrides:
        toString in class java.lang.Object