Skip to content

Commit ba5fcdf

Browse files
committed
GP-0 correct backward compatibility issue for project owner
1 parent f64c38e commit ba5fcdf

File tree

2 files changed

+63
-27
lines changed

2 files changed

+63
-27
lines changed

Ghidra/Framework/Project/src/main/java/ghidra/framework/data/DefaultProjectData.java

Lines changed: 33 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -116,11 +116,11 @@ public DefaultProjectData(ProjectLocator localStorageLocator, boolean isInWritab
116116
try {
117117
init(false, isInWritableProject);
118118
if (resetOwner) {
119-
owner = SystemUtilities.getUserName();
119+
owner = getUserName();
120120
properties.putString(OWNER, owner);
121121
properties.writeState();
122122
}
123-
else if (isInWritableProject && !SystemUtilities.getUserName().equals(owner)) {
123+
else if (isInWritableProject && !isOwner(owner)) {
124124
if (owner == null) {
125125
throw new NotOwnerException("Older projects may only be opened as a View.\n" +
126126
"You must first create a new project or open an existing current project, \n" +
@@ -185,7 +185,7 @@ public DefaultProjectData(ProjectLocator localStorageLocator, RepositoryAdapter
185185
DefaultProjectData(LocalFileSystem fileSystem, FileSystem versionedFileSystem)
186186
throws IOException {
187187
this.localStorageLocator = new ProjectLocator(null, "Test");
188-
owner = SystemUtilities.getUserName();
188+
owner = getUserName();
189189
boolean success = false;
190190
try {
191191
synchronized (fileSystem) {
@@ -217,6 +217,34 @@ private void initVersionedFSListener() throws IOException {
217217
}
218218
}
219219

220+
private boolean isOwner(String name) {
221+
// Tolerate user name using either the new or old format
222+
return SystemUtilities.getUserName().equals(name) || getUserName().equals(name);
223+
}
224+
225+
/**
226+
* Get user name in a format consistent with the older {@link SystemUtilities#getUserName()}
227+
* implementation. This is done to ensure we can still recognize the OWNER of older
228+
* projects.
229+
*
230+
* @return current user name using legacy formatting.
231+
*/
232+
private String getUserName() {
233+
String uname = System.getProperty("user.name");
234+
235+
// Remove the spaces since some operating systems allow
236+
// spaces and some do not, Java's File class doesn't
237+
String userName = uname;
238+
if (uname.indexOf(" ") >= 0) {
239+
userName = "";
240+
StringTokenizer tokens = new StringTokenizer(uname, " ", false);
241+
while (tokens.hasMoreTokens()) {
242+
userName += tokens.nextToken();
243+
}
244+
}
245+
return userName;
246+
}
247+
220248
/**
221249
* Read the contents of the project properties file to include the following values if relavent:
222250
* {@value #OWNER}, {@value #SERVER_NAME}, {@value #REPOSITORY_NAME}, {@value #PORT_NUMBER}
@@ -276,7 +304,7 @@ private void init(boolean create, boolean isInWritableProject)
276304
throw new ReadOnlyException(
277305
"Project " + localStorageLocator.getName() + " is read-only");
278306
}
279-
owner = properties.getString(OWNER, SystemUtilities.getUserName());
307+
owner = properties.getString(OWNER, getUserName());
280308
}
281309
else {
282310
owner = "<unknown>"; // Unknown owner
@@ -298,7 +326,7 @@ private void initLock(boolean creatingProject) throws LockException, IOException
298326
}
299327

300328
if (!properties.exists()) {
301-
owner = SystemUtilities.getUserName();
329+
owner = getUserName();
302330
properties.putString(OWNER, owner);
303331
properties.writeState();
304332
}

Ghidra/Framework/Utility/src/main/java/ghidra/util/SystemUtilities.java

Lines changed: 30 additions & 22 deletions
Original file line numberDiff line numberDiff line change
@@ -100,33 +100,41 @@ private static boolean checkForDevelopmentMode() {
100100
}
101101

102102
/**
103-
* Get the user that is running the ghidra application. This name may be modified to
103+
* Clean the specified user name to eliminate any spaces or leading domain name
104+
* which may be present (e.g., "MyDomain\John Doe" becomes "JohnDoe").
105+
* @param name user name string to be cleaned-up
106+
* @return the clean user name
107+
*/
108+
public static String getCleanUserName(String name) {
109+
String uname = name;
110+
// Remove the spaces since some operating systems allow
111+
// spaces and some do not, Java's File class doesn't
112+
StringBuilder nameBuf = new StringBuilder();
113+
if (uname.indexOf(" ") >= 0) {
114+
StringTokenizer tokens = new StringTokenizer(uname, " ", false);
115+
while (tokens.hasMoreTokens()) {
116+
nameBuf.append(tokens.nextToken());
117+
}
118+
uname = nameBuf.toString();
119+
}
120+
121+
// Remove leading Domain Name if present
122+
int slashIndex = uname.lastIndexOf('\\');
123+
if (slashIndex >= 0) {
124+
uname = uname.substring(slashIndex + 1);
125+
}
126+
return uname;
127+
}
128+
129+
/**
130+
* Get the user that is running the application. This name may be modified to
104131
* eliminate any spaces or leading domain name which may be present in Java's
105-
* {@code user.name} system property.
132+
* {@code user.name} system property (see {@link #getCleanUserName(String)}).
106133
* @return the user name
107134
*/
108135
public static String getUserName() {
109136
if (userName == null) {
110-
String uname = System.getProperty("user.name");
111-
112-
// Remove the spaces since some operating systems allow
113-
// spaces and some do not, Java's File class doesn't
114-
StringBuilder nameBuf = new StringBuilder();
115-
if (uname.indexOf(" ") >= 0) {
116-
StringTokenizer tokens = new StringTokenizer(uname, " ", false);
117-
while (tokens.hasMoreTokens()) {
118-
nameBuf.append(tokens.nextToken());
119-
}
120-
uname = nameBuf.toString();
121-
}
122-
123-
// Remove leading Domain Name if present
124-
int slashIndex = uname.lastIndexOf('\\');
125-
if (slashIndex >= 0) {
126-
uname = uname.substring(slashIndex + 1);
127-
}
128-
129-
userName = uname;
137+
userName = getCleanUserName(System.getProperty("user.name"));
130138
}
131139
return userName;
132140
}

0 commit comments

Comments
 (0)