Package ghidra.formats.gfilesystem
Class FSUtilities
- java.lang.Object
-
- ghidra.formats.gfilesystem.FSUtilities
-
public class FSUtilities extends java.lang.Object
-
-
Nested Class Summary
Nested Classes Modifier and Type Class Description static classFSUtilities.StreamCopyResult
-
Field Summary
Fields Modifier and Type Field Description static java.util.Comparator<GFile>GFILE_NAME_TYPE_COMPARATORSorts GFiles by type (directories segregated from files) and then by name, case-insensitive.static java.lang.StringSEPARATORstatic java.lang.StringSEPARATOR_CHARS
-
Constructor Summary
Constructors Constructor Description FSUtilities()
-
Method Summary
All Methods Static Methods Concrete Methods Modifier and Type Method Description static java.lang.StringappendPath(java.lang.String... paths)Concats path strings together, taking care to ensure that there is a correct path separator character between each part.static voiddisplayException(java.lang.Object originator, java.awt.Component parent, java.lang.String title, java.lang.String message, java.lang.Throwable throwable)Displays a filesystem relatedexceptionin the most user-friendly manner possible, even if we have to do some hacky things with helping the user with crypto problems.static java.lang.StringescapeDecode(java.lang.String s)Returns a decoded version of the input stream where "%nn" escape sequences are replaced with their actual characters, using UTF-8 decoding rules.static java.lang.StringescapeEncode(java.lang.String s)Returns a copy of the input string with FSRL problematic[1] characters escaped as "%nn" sequences, where nn are hexdigits specifying the numeric ascii value of that character.static java.lang.StringgetExtension(java.lang.String path, int extLevel)Returns the "extension" of the filename part of the path string.static java.lang.StringgetFileMD5(java.io.File f, TaskMonitor monitor)Calculate the MD5 of a file.static java.lang.StringgetFilesystemDescriptionFromClass(java.lang.Class<?> clazz)Returns the description value of theFileSystemInfoannotation attached to the specified class.static intgetFilesystemPriorityFromClass(java.lang.Class<?> clazz)Returns the priority value of theFileSystemInfoannotation attached to the specified class.static java.lang.StringgetFilesystemTypeFromClass(java.lang.Class<?> clazz)Returns the type value of theFileSystemInfoannotation attached to the specified class.static java.lang.StringgetSafeFilename(java.lang.String untrustedFilename)Best-effort of sanitizing an untrusted string that will be used to create a file on the user's local filesystem.static java.lang.StringgetStreamMD5(java.io.InputStream is, TaskMonitor monitor)Calculate the MD5 of a stream.static java.lang.StringinfoMapToString(java.util.Map<java.lang.String,java.lang.String> info)Converts a string -> string mapping into a "key: value" multi-line string.static booleanisSameFS(java.util.List<FSRL> fsrls)Returns true if all theFSRLs in the specified list are from the filesystem.static java.util.List<GFile>listFileSystem(GFileSystem fs, GFile dir, java.util.List<GFile> result, TaskMonitor taskMonitor)Returns a list of all files in a GFileSystem.static java.lang.StringnormalizeNativePath(java.lang.String path)Returns a copy of the string path that has been fixed to have correct slashes and a correct leading root slash '/'.static FSUtilities.StreamCopyResultstreamCopy(java.io.InputStream is, java.io.OutputStream os, TaskMonitor monitor)Copies a stream and calculates the md5 at the same time.
-
-
-
Field Detail
-
SEPARATOR_CHARS
public static final java.lang.String SEPARATOR_CHARS
- See Also:
- Constant Field Values
-
SEPARATOR
public static final java.lang.String SEPARATOR
- See Also:
- Constant Field Values
-
GFILE_NAME_TYPE_COMPARATOR
public static final java.util.Comparator<GFile> GFILE_NAME_TYPE_COMPARATOR
Sorts GFiles by type (directories segregated from files) and then by name, case-insensitive.
-
-
Method Detail
-
infoMapToString
public static java.lang.String infoMapToString(java.util.Map<java.lang.String,java.lang.String> info)
Converts a string -> string mapping into a "key: value" multi-line string.- Parameters:
info- map of string key to string value.- Returns:
- Multi-line string "key: value" string.
-
getSafeFilename
public static java.lang.String getSafeFilename(java.lang.String untrustedFilename)
Best-effort of sanitizing an untrusted string that will be used to create a file on the user's local filesystem.- Parameters:
untrustedFilename- filename string with possibly bad / hostile characters or sequences.- Returns:
- sanitized filename
-
escapeEncode
public static java.lang.String escapeEncode(java.lang.String s)
Returns a copy of the input string with FSRL problematic[1] characters escaped as "%nn" sequences, where nn are hexdigits specifying the numeric ascii value of that character.Characters that need more than a byte to encode will result in multiple "%nn" values that encode the necessary UTF8 codepoints.
[1] - non-ascii / unprintable / FSRL portion separation characters.
- Parameters:
s- string, or null.- Returns:
- string with problematic characters escaped as "%nn" sequences, or null if parameter was null.
-
escapeDecode
public static java.lang.String escapeDecode(java.lang.String s) throws java.net.MalformedURLExceptionReturns a decoded version of the input stream where "%nn" escape sequences are replaced with their actual characters, using UTF-8 decoding rules.- Parameters:
s- string with escape sequences in the form "%nn", or null.- Returns:
- string with all escape sequences replaced with native characters, or null if original parameter was null.
- Throws:
java.net.MalformedURLException- if bad escape sequence format.
-
listFileSystem
public static java.util.List<GFile> listFileSystem(GFileSystem fs, GFile dir, java.util.List<GFile> result, TaskMonitor taskMonitor) throws java.io.IOException, CancelledException
Returns a list of all files in a GFileSystem.- Parameters:
fs-GFileSystemto recursively query for all files.dir- theGFiledirectory to recurse intoresult-Listof GFiles where the results are accumulated into, or null to allocate a new List, returned as the result.taskMonitor-TaskMonitorthat will be checked for cancel.- Returns:
Listof accumulatedresults- Throws:
java.io.IOException- if io error during listing of directoriesCancelledException- if user cancels
-
getFilesystemTypeFromClass
public static java.lang.String getFilesystemTypeFromClass(java.lang.Class<?> clazz)
Returns the type value of theFileSystemInfoannotation attached to the specified class.- Parameters:
clazz- Class to query.- Returns:
- File system type string.
-
getFilesystemDescriptionFromClass
public static java.lang.String getFilesystemDescriptionFromClass(java.lang.Class<?> clazz)
Returns the description value of theFileSystemInfoannotation attached to the specified class.- Parameters:
clazz- Class to query.- Returns:
- File system description string.
-
getFilesystemPriorityFromClass
public static int getFilesystemPriorityFromClass(java.lang.Class<?> clazz)
Returns the priority value of theFileSystemInfoannotation attached to the specified class.- Parameters:
clazz- Class to query.- Returns:
- File system priority integer.
-
isSameFS
public static boolean isSameFS(java.util.List<FSRL> fsrls)
Returns true if all theFSRLs in the specified list are from the filesystem.- Parameters:
fsrls-ListofFSRLs.- Returns:
- boolean true if all are from same filesystem.
-
displayException
public static void displayException(java.lang.Object originator, java.awt.Component parent, java.lang.String title, java.lang.String message, java.lang.Throwable throwable)Displays a filesystem relatedexceptionin the most user-friendly manner possible, even if we have to do some hacky things with helping the user with crypto problems.- Parameters:
originator- a Logger instance, "this", or YourClass.classparent- a parent component used to center the dialog (or null if you don't have one)title- the title of the pop-up dialog (main subject of message)message- the details of the messagethrowable- the Throwable that describes the cause of the error
-
streamCopy
public static FSUtilities.StreamCopyResult streamCopy(java.io.InputStream is, java.io.OutputStream os, TaskMonitor monitor) throws java.io.IOException, CancelledException
Copies a stream and calculates the md5 at the same time.Does not close the passed-in InputStream or OutputStream.
- Parameters:
is-InputStreamto copy. NOTE: not closed by this method.os-OutputStreamto write to. NOTE: not closed by this method.- Returns:
FSUtilities.StreamCopyResultwith md5 and bytes copied count, never null.- Throws:
java.io.IOException- if errorCancelledException- if canceled
-
getStreamMD5
public static java.lang.String getStreamMD5(java.io.InputStream is, TaskMonitor monitor) throws java.io.IOException, CancelledExceptionCalculate the MD5 of a stream.- Parameters:
is-InputStreamto readmonitor-TaskMonitorto watch for cancel- Returns:
- md5 as a hex encoded string, never null.
- Throws:
java.io.IOException- if errorCancelledException- if cancelled
-
getFileMD5
public static java.lang.String getFileMD5(java.io.File f, TaskMonitor monitor) throws java.io.IOException, CancelledExceptionCalculate the MD5 of a file.- Parameters:
f-Fileto read.monitor-TaskMonitorto watch for cancel- Returns:
- md5 as a hex encoded string, never null.
- Throws:
java.io.IOException- if errorCancelledException- if cancelled
-
appendPath
public static java.lang.String appendPath(java.lang.String... paths)
Concats path strings together, taking care to ensure that there is a correct path separator character between each part.Handles forward or back slashes as path separator characters in the input, but only adds forward slashes when separating the path strings that need a separator.
- Parameters:
paths- vararg list of path strings, empty or null elements are ok and are skipped.- Returns:
- null if all params null, "" empty string if all are empty, or "path_element[1]/path_element[2]/.../path_element[N]" otherwise.
-
getExtension
public static java.lang.String getExtension(java.lang.String path, int extLevel)Returns the "extension" of the filename part of the path string.Ie. everything after the nth last '.' char in the filename, including that '.' character.
Using: "path/filename.ext1.ext2"
Gives:
- extLevel 1: ".ext2"
- extLevel 2: ".ext1.ext2"
- extLevel 3:
null
- Parameters:
path- path/filename.ext stringextLevel- number of ext levels; must be greater than 0- Returns:
- ".ext1" for "path/filename.notext.ext1" level 1, ".ext1.ext2" for "path/filename.ext1.ext2" level 2, etc. or null if there was no dot character
- Throws:
java.lang.IllegalArgumentException- if the given level is less than 1
-
normalizeNativePath
public static java.lang.String normalizeNativePath(java.lang.String path)
Returns a copy of the string path that has been fixed to have correct slashes and a correct leading root slash '/'.- Parameters:
path- String forward or backslash path- Returns:
- String path with all forward slashes and a leading root slash.
-
-