mirror of
https://github.com/NationalSecurityAgency/ghidra.git
synced 2025-10-05 02:39:44 +02:00
GT-2700 handle invalid filename characters during batch import
This commit is contained in:
parent
0ddd33978e
commit
f68ccd6115
1 changed files with 23 additions and 11 deletions
|
@ -28,6 +28,7 @@ import ghidra.app.util.importer.MessageLog;
|
||||||
import ghidra.app.util.opinion.LoadSpec;
|
import ghidra.app.util.opinion.LoadSpec;
|
||||||
import ghidra.formats.gfilesystem.*;
|
import ghidra.formats.gfilesystem.*;
|
||||||
import ghidra.framework.model.*;
|
import ghidra.framework.model.*;
|
||||||
|
import ghidra.framework.store.local.LocalFileSystem;
|
||||||
import ghidra.plugin.importer.ImporterUtilities;
|
import ghidra.plugin.importer.ImporterUtilities;
|
||||||
import ghidra.plugin.importer.ProgramMappingService;
|
import ghidra.plugin.importer.ProgramMappingService;
|
||||||
import ghidra.plugins.importer.batch.*;
|
import ghidra.plugins.importer.batch.*;
|
||||||
|
@ -63,14 +64,13 @@ public class ImportBatchTask extends Task {
|
||||||
* instance.
|
* instance.
|
||||||
* <p>
|
* <p>
|
||||||
* @param batchInfo {@link BatchInfo} state object
|
* @param batchInfo {@link BatchInfo} state object
|
||||||
* @param destFolder {@link DomainFolder} where to place imported files.
|
* @param destFolder {@link DomainFolder} where to place imported files
|
||||||
* @param programManager {@link ProgramManager} to use when opening newly imported files, null ok.
|
* @param programManager {@link ProgramManager} to use when opening newly imported files, null ok
|
||||||
* each file is imported, null ok.
|
|
||||||
* @param stripLeading boolean true if each import source's leading path should be omitted
|
* @param stripLeading boolean true if each import source's leading path should be omitted
|
||||||
* when creating the destination project folder path.
|
* when creating the destination project folder path.
|
||||||
* @param stripAllContainerPath boolean true if each imported file's parent container
|
* @param stripAllContainerPath boolean true if each imported file's parent container
|
||||||
* source path should be completely omitted when creating the destination project folder path.
|
* source path should be completely omitted when creating the destination project folder path.
|
||||||
* (the imported file's path within its container is still used).
|
* (the imported file's path within its container is still used)
|
||||||
*/
|
*/
|
||||||
public ImportBatchTask(BatchInfo batchInfo, DomainFolder destFolder,
|
public ImportBatchTask(BatchInfo batchInfo, DomainFolder destFolder,
|
||||||
ProgramManager programManager, boolean stripLeading, boolean stripAllContainerPath) {
|
ProgramManager programManager, boolean stripLeading, boolean stripAllContainerPath) {
|
||||||
|
@ -145,10 +145,10 @@ public class ImportBatchTask extends Task {
|
||||||
Object consumer = new Object();
|
Object consumer = new Object();
|
||||||
try {
|
try {
|
||||||
MessageLog messageLog = new MessageLog();
|
MessageLog messageLog = new MessageLog();
|
||||||
List<DomainObject> importedObjects =
|
List<DomainObject> importedObjects = loadSpec.getLoader().load(byteProvider,
|
||||||
loadSpec.getLoader().load(byteProvider, destInfo.second, destInfo.first,
|
fixupProjectFilename(destInfo.second), destInfo.first, loadSpec,
|
||||||
loadSpec, getOptionsFor(batchLoadConfig, loadSpec, byteProvider),
|
getOptionsFor(batchLoadConfig, loadSpec, byteProvider), messageLog, consumer,
|
||||||
messageLog, consumer, monitor);
|
monitor);
|
||||||
|
|
||||||
// TODO: accumulate batch results
|
// TODO: accumulate batch results
|
||||||
if (importedObjects != null) {
|
if (importedObjects != null) {
|
||||||
|
@ -177,6 +177,16 @@ public class ImportBatchTask extends Task {
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
private String fixupProjectFilename(String filename) {
|
||||||
|
// replaces any invalid characters with underscores
|
||||||
|
StringBuilder sb = new StringBuilder();
|
||||||
|
for (int i = 0; i < filename.length(); i++) {
|
||||||
|
char ch = filename.charAt(i);
|
||||||
|
sb.append(LocalFileSystem.isValidNameCharacter(ch) ? ch : '_');
|
||||||
|
}
|
||||||
|
return sb.toString();
|
||||||
|
}
|
||||||
|
|
||||||
private void releaseAll(List<DomainObject> importedObjects, Object consumer) {
|
private void releaseAll(List<DomainObject> importedObjects, Object consumer) {
|
||||||
for (DomainObject obj : importedObjects) {
|
for (DomainObject obj : importedObjects) {
|
||||||
if (obj.isUsedBy(consumer)) {
|
if (obj.isUsedBy(consumer)) {
|
||||||
|
@ -199,8 +209,9 @@ public class ImportBatchTask extends Task {
|
||||||
ImporterUtilities.setProgramProperties(program, appInfo.getFSRL(), monitor);
|
ImporterUtilities.setProgramProperties(program, appInfo.getFSRL(), monitor);
|
||||||
|
|
||||||
if (programManager != null && totalObjsImported < MAX_PROGRAMS_TO_OPEN) {
|
if (programManager != null && totalObjsImported < MAX_PROGRAMS_TO_OPEN) {
|
||||||
programManager.openProgram(program, totalObjsImported == 0
|
programManager.openProgram(program,
|
||||||
? ProgramManager.OPEN_CURRENT : ProgramManager.OPEN_VISIBLE);
|
totalObjsImported == 0 ? ProgramManager.OPEN_CURRENT
|
||||||
|
: ProgramManager.OPEN_VISIBLE);
|
||||||
}
|
}
|
||||||
|
|
||||||
ProgramMappingService.createAssociation(appInfo.getFSRL(), program);
|
ProgramMappingService.createAssociation(appInfo.getFSRL(), program);
|
||||||
|
@ -238,7 +249,8 @@ public class ImportBatchTask extends Task {
|
||||||
int leadEnd = Math.min(filename, userSrcPath.length());
|
int leadEnd = Math.min(filename, userSrcPath.length());
|
||||||
String leading = (leadStart < filename) ? fullPath.substring(leadStart, leadEnd) : "";
|
String leading = (leadStart < filename) ? fullPath.substring(leadStart, leadEnd) : "";
|
||||||
String containerPath = container < filename && !stripInteriorContainerPath
|
String containerPath = container < filename && !stripInteriorContainerPath
|
||||||
? fullPath.substring(container, filename) : "";
|
? fullPath.substring(container, filename)
|
||||||
|
: "";
|
||||||
String filenameStr = fullPath.substring(filename);
|
String filenameStr = fullPath.substring(filename);
|
||||||
String result = FSUtilities.appendPath(leading, containerPath, filenameStr);
|
String result = FSUtilities.appendPath(leading, containerPath, filenameStr);
|
||||||
return result;
|
return result;
|
||||||
|
|
Loading…
Add table
Add a link
Reference in a new issue