GP-4176: GhidraDev can now handle both legacy user settings directories

as well as the new XDG layout
This commit is contained in:
Ryan Kurtz 2024-02-16 09:50:46 -05:00
parent fea1243894
commit 4fc9ccde28
7 changed files with 56 additions and 25 deletions

View file

@ -36,6 +36,7 @@ public class JavaConfig {
private String applicationName; // example: Ghidra
private String applicationVersion; // example: 9.0.1
private String applicationReleaseName; // example: PUBLIC, DEV, etc
private String applicationLayoutVersion; // example: 2
private int minSupportedJava;
private int maxSupportedJava;
private String compilerComplianceLevel;
@ -299,6 +300,8 @@ public class JavaConfig {
applicationVersion = getDefinedProperty(applicationProperties, "application.version");
applicationReleaseName =
getDefinedProperty(applicationProperties, "application.release.name");
applicationLayoutVersion =
getDefinedProperty(applicationProperties, "application.layout.version");
compilerComplianceLevel =
getDefinedProperty(applicationProperties, "application.java.compiler");
try {
@ -359,7 +362,7 @@ public class JavaConfig {
*/
private void initJavaHomeSaveFile(File installDir) throws FileNotFoundException {
boolean isDev = new File(installDir, "build.gradle").isFile();
String appName = applicationName.toLowerCase();
String appName = applicationName.replaceAll("\\s", "").toLowerCase();
String userSettingsDirName = appName + "_" + applicationVersion + "_" +
applicationReleaseName.replaceAll("\\s", "").toUpperCase();
@ -367,16 +370,6 @@ public class JavaConfig {
userSettingsDirName += "_location_" + installDir.getParentFile().getName();
}
File userSettingsDir = null;
// Look for XDG environment variable
String xdgConfigHomeDirStr = System.getenv("XDG_CONFIG_HOME");
if (xdgConfigHomeDirStr != null && !xdgConfigHomeDirStr.isEmpty()) {
userSettingsDir = new File(xdgConfigHomeDirStr, appName + "/" + userSettingsDirName);
javaHomeSaveFile = new File(userSettingsDir, JAVA_HOME_SAVE_NAME);
return;
}
// Ensure there is a user home directory (there definitely should be)
String userHomeDirPath = System.getProperty("user.home");
if (userHomeDirPath == null || userHomeDirPath.isEmpty()) {
@ -387,6 +380,24 @@ public class JavaConfig {
throw new FileNotFoundException("User home directory does not exist: " + userHomeDir);
}
File userSettingsDir = null;
// Handle legacy application layout
if (applicationLayoutVersion.equals("1")) {
userSettingsDir = new File(userHomeDir, "." + appName + "/." + userSettingsDirName);
javaHomeSaveFile = new File(userSettingsDir, JAVA_HOME_SAVE_NAME);
return;
}
// Look for XDG environment variable
String xdgConfigHomeDirStr = System.getenv("XDG_CONFIG_HOME");
if (xdgConfigHomeDirStr != null && !xdgConfigHomeDirStr.isEmpty()) {
userSettingsDir = new File(xdgConfigHomeDirStr, appName + "/" + userSettingsDirName);
javaHomeSaveFile = new File(userSettingsDir, JAVA_HOME_SAVE_NAME);
return;
}
// Look in current user settings directory
switch (JavaFinder.getCurrentPlatform()) {
case WINDOWS:
String localAppDataDirPath = System.getenv("APPDATA");
@ -408,6 +419,7 @@ public class JavaConfig {
throw new FileNotFoundException(
"Failed to find the user settings directory: Unsupported operating system.");
}
javaHomeSaveFile = new File(userSettingsDir, JAVA_HOME_SAVE_NAME);
}