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
* @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
*/
public GByteProvider(File file, String permissions) throws IOException {

View File

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

View File

@@ -118,8 +118,8 @@ public class DmgFileReader implements Closeable {
FileSystemHandlerFactory factory = fsMajorType.createDefaultHandlerFactory();
if (factory.isSupported(StandardAttribute.CACHING_ENABLED)) {
factory.getCreateAttributes().
setBooleanAttribute(StandardAttribute.CACHING_ENABLED,
factory.getCreateAttributes()
.setBooleanAttribute(StandardAttribute.CACHING_ENABLED,
true);
}
@@ -140,10 +140,13 @@ public class DmgFileReader implements Closeable {
rootFolders.add(fileSystemHandler.getRoot());
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;
FSFork mainFork = fsFile.getMainFork();
if (mainFork.getLength() > 0) {
ReadableRandomAccessStream mainForkStream = mainFork.getReadableRandomAccessStream();
ReadableRandomAccessStream mainForkStream =
mainFork.getReadableRandomAccessStream();
if (mainForkStream.length() != 0) {
return new DmgInputStream(mainForkStream);
}
@@ -176,7 +180,8 @@ public class DmgFileReader implements Closeable {
else if (mainFork.getLength() == 0) {
FSFork resourceFork = fsFile.getForkByType(FSForkType.MACOS_RESOURCE);
ReadableRandomAccessStream resourceForkStream = resourceFork.getReadableRandomAccessStream();
ReadableRandomAccessStream resourceForkStream =
resourceFork.getReadableRandomAccessStream();
if (parser == null) {
return null;
@@ -211,7 +216,8 @@ public class DmgFileReader implements Closeable {
ReadableRandomAccessStream resourceForkStream,
int expectedLength) throws IOException {
File tempFile = GFileUtilityMethods.writeTemporaryFile( new DmgInputStream( resourceForkStream ) );
File tempFile =
GFileUtilityMethods.writeTemporaryFile(new DmgInputStream(resourceForkStream));
System.err.println(
"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.
*
* @param entry the file system entry
*/
public long getLength(FSEntry entry) {
if (entry != null && entry.isFile()) {
@@ -352,7 +361,6 @@ public class DmgFileReader implements Closeable {
if (mainFork.getLength() > 0) {
return mainFork.getLength();
}
try {
if (parser != null) {
DecmpfsHeader header = parser.getDecmpfsHeader(entry.asFile());
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;
}
/**
* 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" ].
*
* <p>
* Note: the "a" will be stripped because it corresponds to the file system handler.
*
* @param path the path
*/
public String[] convertPathToArrayAndStripFileSystemName(String path) {
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.
*
* @param path the path
*/
public FSEntry getFileByPath(String path) {
if (path == null || path.equals("/")) {//ROOT

View File

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