Merge remote-tracking branch

'origin/GP-6585_ghidracadabra_PR-8179_gemesa_bsim-dropdatabase'
(Closes #8178, Closes #8179)
This commit is contained in:
Ryan Kurtz
2026-03-26 04:52:45 -04:00
3 changed files with 76 additions and 2 deletions

View File

@@ -269,6 +269,7 @@
bsim createdatabase <bsimURL> <config_template> [--name|-n "<name>"] [--owner|-o "<owner>"] [--description|-d "<text>"] [--nocallgraph]
bsim setmetadata <bsimURL> [--name|-n "<name>"] [--owner|-o "<owner>"] [--description|-d "<text>"]
bsim getmetadata <bsimURL>
bsim dropdatabase <bsimURL> [--force]
bsim addexecategory <bsimURL> <category_name> [--date]
bsim addfunctiontag <bsimURL> <tag_name>
bsim dropindex <bsimURL>
@@ -354,6 +355,17 @@
relationships between ingested functions. Default is to store call relationships.</P>
</DD>
<DT><SPAN class="term"><SPAN class=
"bold"><STRONG>dropdatabase</STRONG></SPAN></SPAN></DT>
<DD>
<P>Deletes the specified repository. A BSim server URL is required. The database name
is taken from the path element of the URL.
<P><SPAN class="command"><STRONG>--force</STRONG></SPAN> - skips the confirmation
dialog and deletes the database. Default is to ask for confirmation.</P>
</DD>
<DT><SPAN class="term"><SPAN class=
"bold"><STRONG>setmetadata</STRONG></SPAN></SPAN></DT>

View File

@@ -57,6 +57,7 @@ public class BSimLaunchable implements GhidraLaunchable {
* bsim commands
*/
private static final String COMMAND_CREATE_DATABASE = defineCommand("createdatabase");
private static final String COMMAND_DROP_DATABASE = defineCommand("dropdatabase");
private static final String COMMAND_SET_METADATA = defineCommand("setmetadata");
private static final String COMMAND_GET_METADATA = defineCommand("getmetadata");
private static final String COMMAND_ADD_EXE_CATEGORY = defineCommand("addexecategory");
@@ -112,6 +113,7 @@ public class BSimLaunchable implements GhidraLaunchable {
private static final String PRINT_SELF_SIGNIFICANCE_OPTION = "--printselfsig";
private static final String CALL_GRAPH_OPTION = "--callgraph";
private static final String PRINT_JUST_EXE_OPTION = "--printjustexe";
private static final String DROP_DATABASE_FORCE_OPTION = "--force";
private static final Map<String, String> SHORTCUT_OPTION_MAP = new HashMap<>();
static {
@@ -135,6 +137,7 @@ public class BSimLaunchable implements GhidraLaunchable {
// Populate ALLOWED_OPTION_MAP for each command
private static final Set<String> CREATE_DATABASE_OPTIONS =
Set.of(NAME_OPTION, OWNER_OPTION, DESCRIPTION_OPTION, NO_CALLGRAPH_OPTION);
private static final Set<String> DROP_DATABASE_OPTIONS = Set.of(DROP_DATABASE_FORCE_OPTION);
private static final Set<String> COMMIT_SIGS_OPTIONS =
Set.of(OVERRIDE_OPTION, MD5_OPTION); // url requires override param
private static final Set<String> COMMIT_UPDATES_OPTIONS = Set.of();
@@ -165,6 +168,7 @@ public class BSimLaunchable implements GhidraLaunchable {
private static final Map<String, Set<String>> ALLOWED_OPTION_MAP = new HashMap<>();
static {
ALLOWED_OPTION_MAP.put(COMMAND_CREATE_DATABASE, CREATE_DATABASE_OPTIONS);
ALLOWED_OPTION_MAP.put(COMMAND_DROP_DATABASE, DROP_DATABASE_OPTIONS);
ALLOWED_OPTION_MAP.put(COMMAND_SET_METADATA, SET_METADATA_OPTIONS);
ALLOWED_OPTION_MAP.put(COMMAND_GET_METADATA, GET_METADATA_OPTIONS);
ALLOWED_OPTION_MAP.put(COMMAND_ADD_EXE_CATEGORY, ADD_EXE_CATEGORY_OPTIONS);
@@ -210,7 +214,7 @@ public class BSimLaunchable implements GhidraLaunchable {
}
private BulkSignatures getBulkSignatures()
throws IllegalArgumentException, MalformedURLException {
throws IllegalArgumentException {
BSimServerInfo serverInfo = null;
if (bsimURL != null) {
serverInfo = new BSimServerInfo(bsimURL);
@@ -408,6 +412,10 @@ public class BSimLaunchable implements GhidraLaunchable {
bsimURL = BSimClientFactory.deriveBSimURL(urlstring);
doCreateDatabase(subParams);
}
else if (COMMAND_DROP_DATABASE.equals(command)) {
bsimURL = BSimClientFactory.deriveBSimURL(urlstring);
doDropDatabase(subParams);
}
else if (COMMAND_SET_METADATA.equals(command)) {
bsimURL = BSimClientFactory.deriveBSimURL(urlstring);
doInstallMetadata(subParams);
@@ -541,6 +549,37 @@ public class BSimLaunchable implements GhidraLaunchable {
}
}
/**
* Drops the current BSim database.
*
* @param params the command-line parameters
* @throws IOException if there's an error establishing the database connection
*/
private void doDropDatabase(List<String> params) throws IOException {
if (!confirmDrop()) {
return;
}
try (BulkSignatures bsim = getBulkSignatures()) {
bsim.dropDatabase();
}
}
private boolean confirmDrop() throws IOException {
if (booleanOptions.contains(DROP_DATABASE_FORCE_OPTION)) {
return true;
}
System.out.print("Are you sure you want to drop the database? (y/n): ");
try (InputStreamReader isReader = new InputStreamReader(System.in);
BufferedReader bReader = new BufferedReader(isReader)) {
String input = bReader.readLine();
if (input != null && input.equalsIgnoreCase("y")) {
return true;
}
}
System.out.println("Database NOT dropped");
return false;
}
private void doGenerateSigs(List<String> params, TaskMonitor monitor)
throws Exception, CancelledException {
// concurrent --bsim and --config option use already checked
@@ -877,7 +916,7 @@ public class BSimLaunchable implements GhidraLaunchable {
* @param params the command-line params
* @throws IOException if there's an error establishing the database connection
*/
private void doPrintMetadata(List<String> params) throws IOException, LSHException {
private void doPrintMetadata(List<String> params) throws IOException {
try (BulkSignatures bsim = getBulkSignatures()) {
bsim.printMetadata();
@@ -980,6 +1019,7 @@ public class BSimLaunchable implements GhidraLaunchable {
System.err.println("\n" +
"USAGE: bsim [command] required-args... [OPTIONS...]\n" +
" createdatabase <bsimURL> <config_template> [--name|-n \"<name>\"] [--owner|-o \"<owner>\"] [--description|-d \"<text>\"] [--nocallgraph]\n" +
" dropdatabase <bsimURL> [--force]\n" +
" setmetadata <bsimURL> [--name|-n \"<name>\"] [--owner|-o \"<owner>\"] [--description|-d \"<text>\"]\n" +
" getmetadata <bsimURL>\n" +
" addexecategory <bsimURL> <category_name> [--date]\n" +

View File

@@ -451,6 +451,28 @@ public class BulkSignatures implements AutoCloseable {
}
}
/**
* Drops the current BSim database.
*
* @throws IOException if there's an error establishing the database connection
*/
public void dropDatabase() throws IOException {
establishQueryServerConnection(false); // Set up client configuration
querydb.close(); // Database can't be in use when we try to drop it
DropDatabase command = new DropDatabase();
command.databaseName = bsimServerInfo.getDBName();
ResponseDropDatabase response = command.execute(querydb);
if (response == null) {
throw new IOException("Unable to drop database: " + querydb.getLastError().message);
}
if (response.dropSuccessful) {
Msg.info(this, "Successfully dropped database \"" + command.databaseName + "\"");
}
else {
Msg.error(this, "Unable to drop database: " + response.errorMessage);
}
}
/**
* Adds function signatures from the specified project to the BSim database
* @param ghidraURL ghidra repository from which to pull files for signature generation