Ghidra is a software reverse engineering (SRE) framework
GP-0 correct backward compatibility issue for project owner
ghidra1ba5fcdf4ede4
diff --git a/Ghidra/Framework/Project/src/main/java/ghidra/framework/data/DefaultProjectData.java b/Ghidra/Framework/Project/src/main/java/ghidra/framework/data/DefaultProjectData.javaindex ed7aca1356..4f9fe14078 100644--- a/Ghidra/Framework/Project/src/main/java/ghidra/framework/data/DefaultProjectData.java+++ b/Ghidra/Framework/Project/src/main/java/ghidra/framework/data/DefaultProjectData.java@@ -116,11 +116,11 @@ public class DefaultProjectData implements ProjectData {try {init(false, isInWritableProject);if (resetOwner) {- owner = SystemUtilities.getUserName();+ owner = getUserName();properties.putString(OWNER, owner);properties.writeState();}- else if (isInWritableProject && !SystemUtilities.getUserName().equals(owner)) {+ else if (isInWritableProject && !isOwner(owner)) {if (owner == null) {throw new NotOwnerException("Older projects may only be opened as a View.\n" +"You must first create a new project or open an existing current project, \n" +@@ -185,7 +185,7 @@ public class DefaultProjectData implements ProjectData {DefaultProjectData(LocalFileSystem fileSystem, FileSystem versionedFileSystem)throws IOException {this.localStorageLocator = new ProjectLocator(null, "Test");- owner = SystemUtilities.getUserName();+ owner = getUserName();boolean success = false;try {synchronized (fileSystem) {@@ -217,6 +217,34 @@ public class DefaultProjectData implements ProjectData {}}+ private boolean isOwner(String name) {+ // Tolerate user name using either the new or old format+ return SystemUtilities.getUserName().equals(name) || getUserName().equals(name);+ }++ /**+ * Get user name in a format consistent with the older {@link SystemUtilities#getUserName()}+ * implementation. This is done to ensure we can still recognize the OWNER of older+ * projects.+ *+ * @return current user name using legacy formatting.+ */+ private String getUserName() {+ String uname = System.getProperty("user.name");++ // Remove the spaces since some operating systems allow+ // spaces and some do not, Java's File class doesn't+ String userName = uname;+ if (uname.indexOf(" ") >= 0) {+ userName = "";+ StringTokenizer tokens = new StringTokenizer(uname, " ", false);+ while (tokens.hasMoreTokens()) {+ userName += tokens.nextToken();+ }+ }+ return userName;+ }+/*** Read the contents of the project properties file to include the following values if relavent:* {@value #OWNER}, {@value #SERVER_NAME}, {@value #REPOSITORY_NAME}, {@value #PORT_NUMBER}@@ -276,7 +304,7 @@ public class DefaultProjectData implements ProjectData {throw new ReadOnlyException("Project " + localStorageLocator.getName() + " is read-only");}- owner = properties.getString(OWNER, SystemUtilities.getUserName());+ owner = properties.getString(OWNER, getUserName());}else {owner = "<unknown>"; // Unknown owner@@ -298,7 +326,7 @@ public class DefaultProjectData implements ProjectData {}if (!properties.exists()) {- owner = SystemUtilities.getUserName();+ owner = getUserName();properties.putString(OWNER, owner);properties.writeState();}diff --git a/Ghidra/Framework/Utility/src/main/java/ghidra/util/SystemUtilities.java b/Ghidra/Framework/Utility/src/main/java/ghidra/util/SystemUtilities.javaindex e6d084f185..f1b1c8b449 100644--- a/Ghidra/Framework/Utility/src/main/java/ghidra/util/SystemUtilities.java+++ b/Ghidra/Framework/Utility/src/main/java/ghidra/util/SystemUtilities.java@@ -100,33 +100,41 @@ public class SystemUtilities {}/**- * Get the user that is running the ghidra application. This name may be modified to+ * Clean the specified user name to eliminate any spaces or leading domain name+ * which may be present (e.g., "MyDomain\John Doe" becomes "JohnDoe").+ * @param name user name string to be cleaned-up+ * @return the clean user name+ */+ public static String getCleanUserName(String name) {+ String uname = name;+ // Remove the spaces since some operating systems allow+ // spaces and some do not, Java's File class doesn't+ StringBuilder nameBuf = new StringBuilder();+ if (uname.indexOf(" ") >= 0) {+ StringTokenizer tokens = new StringTokenizer(uname, " ", false);+ while (tokens.hasMoreTokens()) {+ nameBuf.append(tokens.nextToken());+ }+ uname = nameBuf.toString();+ }++ // Remove leading Domain Name if present+ int slashIndex = uname.lastIndexOf('\\');+ if (slashIndex >= 0) {+ uname = uname.substring(slashIndex + 1);+ }+ return uname;+ }++ /**+ * Get the user that is running the application. This name may be modified to* eliminate any spaces or leading domain name which may be present in Java's- * {@code user.name} system property.+ * {@code user.name} system property (see {@link #getCleanUserName(String)}).* @return the user name*/public static String getUserName() {if (userName == null) {- String uname = System.getProperty("user.name");-- // Remove the spaces since some operating systems allow- // spaces and some do not, Java's File class doesn't- StringBuilder nameBuf = new StringBuilder();- if (uname.indexOf(" ") >= 0) {- StringTokenizer tokens = new StringTokenizer(uname, " ", false);- while (tokens.hasMoreTokens()) {- nameBuf.append(tokens.nextToken());- }- uname = nameBuf.toString();- }-- // Remove leading Domain Name if present- int slashIndex = uname.lastIndexOf('\\');- if (slashIndex >= 0) {- uname = uname.substring(slashIndex + 1);- }-- userName = uname;+ userName = getCleanUserName(System.getProperty("user.name"));}return userName;}