GP-0: Removing unnecessary DMG NPE check, and some cleanup

(Closes #1740)
This commit is contained in:
Ryan Kurtz
2026-06-23 05:10:22 -04:00
parent 8711646775
commit a25f23b640
4 changed files with 176 additions and 180 deletions

View File

@@ -27,7 +27,7 @@ public class GByteProvider implements Closeable {
/** /**
* Constructs a byte provider using the specified file and permissions string * Constructs a byte provider using the specified file and permissions string
* @param file the file to open for random access * @param file the file to open for random access
* @param string indicating permissions used for open * @param permissions indicating permissions used for open
* @throws FileNotFoundException if the file does not exist * @throws FileNotFoundException if the file does not exist
*/ */
public GByteProvider(File file, String permissions) throws IOException { public GByteProvider(File file, String permissions) throws IOException {

View File

@@ -38,7 +38,8 @@ public class AttributesFileParser {
HFSPlusForkData attributes = volumeHeader.getAttributesFile(); HFSPlusForkData attributes = volumeHeader.getAttributesFile();
File attributesFile = writeVolumeHeaderFile( hfsxFileSystemView, attributes, prefix + "_" + "attributesFile" ); File attributesFile =
writeVolumeHeaderFile(hfsxFileSystemView, attributes, prefix + "_" + "attributesFile");
provider = new GByteProvider(attributesFile); provider = new GByteProvider(attributesFile);
@@ -60,7 +61,8 @@ public class AttributesFileParser {
try { try {
HFSCommonFSFile hfsFile = (HFSCommonFSFile) file; HFSCommonFSFile hfsFile = (HFSCommonFSFile) file;
CommonHFSCatalogFile catalogFile = hfsFile.getInternalCatalogFile(); CommonHFSCatalogFile catalogFile = hfsFile.getInternalCatalogFile();
CommonHFSCatalogFile.HFSPlusImplementation hfsPlusCatalogFile = (CommonHFSCatalogFile.HFSPlusImplementation)catalogFile; CommonHFSCatalogFile.HFSPlusImplementation hfsPlusCatalogFile =
(CommonHFSCatalogFile.HFSPlusImplementation) catalogFile;
HFSPlusCatalogFile underlying = hfsPlusCatalogFile.getUnderlying(); HFSPlusCatalogFile underlying = hfsPlusCatalogFile.getUnderlying();
HFSCatalogNodeID fileID = underlying.getFileID(); HFSCatalogNodeID fileID = underlying.getFileID();
return fileID.toInt(); return fileID.toInt();
@@ -74,24 +76,17 @@ public class AttributesFileParser {
HFSPlusForkData volumeHeaderFile, HFSPlusForkData volumeHeaderFile,
String volumeHeaderFileName) throws IOException { String volumeHeaderFileName) throws IOException {
if (volumeHeaderFile == null) {
return null;
}
File file = File.createTempFile("Ghidra_" + volumeHeaderFileName + "_", ".tmp"); File file = File.createTempFile("Ghidra_" + volumeHeaderFileName + "_", ".tmp");
file.deleteOnExit(); file.deleteOnExit();
OutputStream out = new FileOutputStream( file ); try (OutputStream out = new FileOutputStream(file)) {
try {
CommonHFSForkData fork = CommonHFSForkData.create(volumeHeaderFile); CommonHFSForkData fork = CommonHFSForkData.create(volumeHeaderFile);
hfsxFileSystemView.extractForkToStream( fork, fork.getBasicExtents(), out, new NullProgressMonitor() {} ); hfsxFileSystemView.extractForkToStream(fork, fork.getBasicExtents(), out,
} new NullProgressMonitor() {/*empty*/});
finally {
out.close();
} }
return file; return file;
} }
public DecmpfsHeader getDecmpfsHeader(FSFile file) throws IOException { public DecmpfsHeader getDecmpfsHeader(FSFile file) {
if (root == null) { if (root == null) {
return null; return null;
@@ -121,5 +116,4 @@ public class AttributesFileParser {
return null; return null;
} }
} }

View File

@@ -118,8 +118,8 @@ public class DmgFileReader implements Closeable {
FileSystemHandlerFactory factory = fsMajorType.createDefaultHandlerFactory(); FileSystemHandlerFactory factory = fsMajorType.createDefaultHandlerFactory();
if (factory.isSupported(StandardAttribute.CACHING_ENABLED)) { if (factory.isSupported(StandardAttribute.CACHING_ENABLED)) {
factory.getCreateAttributes(). factory.getCreateAttributes()
setBooleanAttribute(StandardAttribute.CACHING_ENABLED, .setBooleanAttribute(StandardAttribute.CACHING_ENABLED,
true); true);
} }
@@ -140,10 +140,13 @@ public class DmgFileReader implements Closeable {
rootFolders.add(fileSystemHandler.getRoot()); rootFolders.add(fileSystemHandler.getRoot());
if (fileSystemHandler instanceof HFSXFileSystemHandler) { if (fileSystemHandler instanceof HFSXFileSystemHandler) {
parser = new AttributesFileParser( (HFSXFileSystemHandler)fileSystemHandler, fileSystemHandler.getRoot( ).getName( ) ); parser = new AttributesFileParser((HFSXFileSystemHandler) fileSystemHandler,
fileSystemHandler.getRoot().getName());
} }
} else { }
System.err.println("UNKNOWN file system type. Can't Open filesystem. Suspect this is an APFS.\n"); else {
System.err.println(
"UNKNOWN file system type. Can't Open filesystem. Suspect this is an APFS.\n");
} }
} }
@@ -168,7 +171,8 @@ public class DmgFileReader implements Closeable {
FSFile fsFile = (FSFile) entry; FSFile fsFile = (FSFile) entry;
FSFork mainFork = fsFile.getMainFork(); FSFork mainFork = fsFile.getMainFork();
if (mainFork.getLength() > 0) { if (mainFork.getLength() > 0) {
ReadableRandomAccessStream mainForkStream = mainFork.getReadableRandomAccessStream(); ReadableRandomAccessStream mainForkStream =
mainFork.getReadableRandomAccessStream();
if (mainForkStream.length() != 0) { if (mainForkStream.length() != 0) {
return new DmgInputStream(mainForkStream); return new DmgInputStream(mainForkStream);
} }
@@ -176,7 +180,8 @@ public class DmgFileReader implements Closeable {
else if (mainFork.getLength() == 0) { else if (mainFork.getLength() == 0) {
FSFork resourceFork = fsFile.getForkByType(FSForkType.MACOS_RESOURCE); FSFork resourceFork = fsFile.getForkByType(FSForkType.MACOS_RESOURCE);
ReadableRandomAccessStream resourceForkStream = resourceFork.getReadableRandomAccessStream(); ReadableRandomAccessStream resourceForkStream =
resourceFork.getReadableRandomAccessStream();
if (parser == null) { if (parser == null) {
return null; return null;
@@ -211,7 +216,8 @@ public class DmgFileReader implements Closeable {
ReadableRandomAccessStream resourceForkStream, ReadableRandomAccessStream resourceForkStream,
int expectedLength) throws IOException { int expectedLength) throws IOException {
File tempFile = GFileUtilityMethods.writeTemporaryFile( new DmgInputStream( resourceForkStream ) ); File tempFile =
GFileUtilityMethods.writeTemporaryFile(new DmgInputStream(resourceForkStream));
System.err.println( System.err.println(
"dmg resource fork for " + entry.getName() + ": " + tempFile.getAbsolutePath()); "dmg resource fork for " + entry.getName() + ": " + tempFile.getAbsolutePath());
@@ -343,8 +349,11 @@ public class DmgFileReader implements Closeable {
} }
/** /**
* Returns the length of the given file system entry. * {@return the length of the given file system entry}
* <p>
* If the entry is actually a directory, then -1 is returned. * If the entry is actually a directory, then -1 is returned.
*
* @param entry the file system entry
*/ */
public long getLength(FSEntry entry) { public long getLength(FSEntry entry) {
if (entry != null && entry.isFile()) { if (entry != null && entry.isFile()) {
@@ -352,7 +361,6 @@ public class DmgFileReader implements Closeable {
if (mainFork.getLength() > 0) { if (mainFork.getLength() > 0) {
return mainFork.getLength(); return mainFork.getLength();
} }
try {
if (parser != null) { if (parser != null) {
DecmpfsHeader header = parser.getDecmpfsHeader(entry.asFile()); DecmpfsHeader header = parser.getDecmpfsHeader(entry.asFile());
if (header != null) { if (header != null) {
@@ -360,19 +368,17 @@ public class DmgFileReader implements Closeable {
} }
} }
} }
catch (IOException e) {
return 1;//TODO lookup valid length in DECMPFS
}
}
return -1; return -1;
} }
/** /**
* Convert path to string array. * {@return a path converted to a string array}
* * <p>
* For example, "/a/b/c.txt" will be converted to [ "a", "b", "c.txt" ]. * For example, "/a/b/c.txt" will be converted to [ "a", "b", "c.txt" ].
* * <p>
* Note: the "a" will be stripped because it corresponds to the file system handler. * Note: the "a" will be stripped because it corresponds to the file system handler.
*
* @param path the path
*/ */
public String[] convertPathToArrayAndStripFileSystemName(String path) { public String[] convertPathToArrayAndStripFileSystemName(String path) {
String[] splitPath = path.split("/"); String[] splitPath = path.split("/");
@@ -385,8 +391,11 @@ public class DmgFileReader implements Closeable {
} }
/** /**
* Returns the DMG file object for the corresponding path. * {@return the DMG file object for the corresponding path}
* <p>
* Path should contain the file system handler name. * Path should contain the file system handler name.
*
* @param path the path
*/ */
public FSEntry getFileByPath(String path) { public FSEntry getFileByPath(String path) {
if (path == null || path.equals("/")) {//ROOT if (path == null || path.equals("/")) {//ROOT

View File

@@ -3,7 +3,6 @@
*/ */
package mobiledevices.dmg.reader; package mobiledevices.dmg.reader;
import java.io.IOException;
import java.text.DateFormat; import java.text.DateFormat;
import java.util.ArrayList; import java.util.ArrayList;
import java.util.List; import java.util.List;
@@ -20,9 +19,7 @@ import mobiledevices.dmg.decmpfs.DecmpfsHeader;
import mobiledevices.dmg.hfsplus.AttributesFileParser; import mobiledevices.dmg.hfsplus.AttributesFileParser;
/** /**
*
* @see org.catacombae.hfsexplorer.gui.FSEntrySummaryPanel * @see org.catacombae.hfsexplorer.gui.FSEntrySummaryPanel
*
*/ */
class DmgInfoGenerator { class DmgInfoGenerator {
private DmgFileReader fileSystem; private DmgFileReader fileSystem;
@@ -60,16 +57,12 @@ class DmgInfoGenerator {
appendFileID(infoList, file); appendFileID(infoList, file);
if (parser != null) { if (parser != null) {
try {
DecmpfsHeader decmpfsHeader = parser.getDecmpfsHeader(file); DecmpfsHeader decmpfsHeader = parser.getDecmpfsHeader(file);
if (decmpfsHeader != null) { if (decmpfsHeader != null) {
infoList.add( infoList.add(
"Decmpfs Size: " + getSizeString(decmpfsHeader.getUncompressedSize())); "Decmpfs Size: " + getSizeString(decmpfsHeader.getUncompressedSize()));
} }
} }
catch (IOException e) {
}
}
} }
else if (entry instanceof FSFolder) { else if (entry instanceof FSFolder) {
FSFolder folder = (FSFolder) entry; FSFolder folder = (FSFolder) entry;
@@ -201,21 +194,21 @@ class DmgInfoGenerator {
} }
private void calculateFolderSize(FSFolder folder, ObjectContainer<Long> result) { private void calculateFolderSize(FSFolder folder, ObjectContainer<Long> result) {
for (FSEntry entry : folder.listEntries()) { for (FSEntry e : folder.listEntries()) {
if (entry instanceof FSFile) { if (e instanceof FSFile) {
Long value = result.o; Long value = result.o;
value += ((FSFile) entry).getMainFork().getLength(); value += ((FSFile) e).getMainFork().getLength();
result.o = value; result.o = value;
} }
else if (entry instanceof FSFolder) { else if (e instanceof FSFolder) {
calculateFolderSize((FSFolder) entry, result); calculateFolderSize((FSFolder) e, result);
} }
else if (entry instanceof FSLink) { else if (e instanceof FSLink) {
/* Do nothing. Symbolic link targets aren't part of the folder. */ /* Do nothing. Symbolic link targets aren't part of the folder. */
} }
else { else {
System.err.println("FSEntrySummaryPanel.calculateFolderSize():" + System.err.println("FSEntrySummaryPanel.calculateFolderSize():" +
" unexpected type " + entry.getClass()); " unexpected type " + e.getClass());
} }
} }
} }