GT-3218 ignore system hidden dot directories and disallow project names

to start with a dot/period.
This commit is contained in:
ghidra1 2019-10-07 16:15:15 -04:00
parent 04f7366a62
commit 6ece3f4f4f
7 changed files with 82 additions and 24 deletions

View file

@ -121,7 +121,7 @@ public class RepositoryManager {
validateUser(currentUser);
if (!NamingUtilities.isValidName(name)) {
if (!NamingUtilities.isValidProjectName(name)) {
throw new IOException("Invalid repository name: " + name);
}
if (repositoryMap.containsKey(name)) {
@ -225,7 +225,10 @@ public class RepositoryManager {
}
/**
* Return array of users known to this manager.
* Get list of all users to be returned to specified currentUser.
* Anonymous user will be returned an empty list.
* @param currentUser current user
* @return array of users known to this manager.
*/
public synchronized String[] getAllUsers(String currentUser) throws IOException {
if (isAnonymousUser(currentUser)) {
@ -241,7 +244,7 @@ public class RepositoryManager {
}
/**
* Returns UserManager object
* @return UserManager object
*/
public UserManager getUserManager() {
return userMgr;

View file

@ -643,8 +643,10 @@ public class IndexedLocalFileSystem extends LocalFileSystem {
static int verifyIndexedFileStructure(File root) throws IndexReadException {
int itemCount = 0;
for (File f : root.listFiles()) {
if (f.isDirectory() && !isHiddenDirName(f.getName())) {
itemCount += verifyIndexedDirectory(f);
if (f.isDirectory()) {
if (!isHiddenDirName(f.getName())) {
itemCount += verifyIndexedDirectory(f);
}
}
else {
String fname = f.getName();

View file

@ -855,11 +855,15 @@ public abstract class LocalFileSystem implements FileSystem {
}
/**
* Determines if the specified storage name corresponds to a hidden name
* @param name
* @return true if name is a hidden name
* Determines if the specified storage directory name corresponds to a
* hidden directory (includes both system and application hidden directories).
* @param name directory name as it appears on storage file system.
* @return true if name is a hidden name, else false
*/
public static final boolean isHiddenDirName(String name) {
if (name.startsWith(".")) {
return true;
}
// odd number of prefix chars at start of name indicates hidden name
return (countHiddenDirPrefixChars(name) & 1) == 1;
}

View file

@ -17,11 +17,11 @@ package ghidra.util;
import java.util.Set;
import ghidra.framework.store.FileSystem;
import ghidra.framework.store.local.LocalFileSystem;
import util.CollectionUtils;
/**
* Utility class with static methods for validating names and converting
* Utility class with static methods for validating project file names and converting
* strings to numbers, etc.
*/
public final class NamingUtilities {
@ -39,15 +39,56 @@ public final class NamingUtilities {
}
/**
* tests whether the given string is a valid name.
* Tests whether the given string is a valid.
* Rules:
* <ul>
* <li>All characters must be a letter, digit (0..9), period, hyphen, underscore or space</li>
* <li>May not exceed a length of 60 characters</li>
* </ul>
* @param name name to validate
* @return true if specified name is valid, else false
* @deprecated method has been deprecated due to improper and widespread use.
* New methods include {@link NamingUtilities#isValidProjectName(String)} and
* {@link LocalFileSystem#testValidName(String,boolean)}.
*/
@Deprecated
public static boolean isValidName(String name) {
if (name == null) {
return false;
}
if (name.indexOf(FileSystem.SEPARATOR_CHAR) >= 0) {
if ((name.length() < 1) || (name.length() > MAX_NAME_LENGTH)) {
return false;
}
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
if (!Character.isLetterOrDigit(c) && !VALID_NAME_SET.contains(c)) {
return false;
}
}
return true;
}
/**
* Tests whether the given string is a valid project name.
* Rules:
* <ul>
* <li>Name may not start with period</li>
* <li>All characters must be a letter, digit (0..9), period, hyphen, underscore or space</li>
* <li>May not exceed a length of 60 characters</li>
* </ul>
* @param name name to validate
* @return true if specified name is valid, else false
*/
public static boolean isValidProjectName(String name) {
if (name == null) {
return false;
}
if (name.startsWith(".")) {
return false;
}
@ -69,12 +110,16 @@ public final class NamingUtilities {
* Find the invalid character in the given name.
* <p>
* This method should only be used with {@link #isValidName(String)}} and <b>not</b>
* {@link #isValidFileName(String);
* {@link #isValidProjectName(String)}
*
* @param name the name with an invalid character
* @return the invalid character or 0 if no invalid character can be found
* @see #isValidName(String)
* @deprecated this method may be removed in a subsequent release due to
* limited use and applicability (project names and project file names have
* different naming resrictions).
*/
@Deprecated
public static char findInvalidChar(String name) {
for (int i = 0; i < name.length(); i++) {
char c = name.charAt(i);
@ -94,6 +139,9 @@ public final class NamingUtilities {
* not case sensitive. Under Windows, Foo.exe and foo.exe represent
* the same filename. To fix this we mangle names first such that Foo.exe becomes
* _foo.exe.
*
* @param name name string to be mangled
* @return mangled name
*/
public static String mangle(String name) {
int len = name.length();
@ -120,14 +168,17 @@ public final class NamingUtilities {
* Performs the inverse of the mangle method. A string is returned such that
* all characters following a MANGLE_CHAR are converted to uppercase. Two MANGLE
* chars in a row are replace by a single MANGLE_CHAR.
*
* @param mangledName mangled name string
* @return demagle name
*/
public static String demangle(String name) {
int len = name.length();
public static String demangle(String mangledName) {
int len = mangledName.length();
StringBuffer buf = new StringBuffer(len);
boolean foundMangle = false;
for (int i = 0; i < len; i++) {
char c = name.charAt(i);
char c = mangledName.charAt(i);
if (foundMangle) {
foundMangle = false;
if (c == MANGLE_CHAR) {

View file

@ -507,7 +507,7 @@ public class FrontEndPlugin extends Plugin
"Cannot open '" + file.getName() + "' as a Ghidra Project");
continue;
}
if (!NamingUtilities.isValidName(filename)) {
if (!NamingUtilities.isValidProjectName(filename)) {
Msg.showError(getClass(), tool.getToolFrame(), "Invalid Project Name",
filename + " is not a valid project name");
continue;

View file

@ -86,9 +86,8 @@ public class RepositoryPanel extends AbstractWizardJPanel {
if (name.length() == 0) {
return false;
}
if (!NamingUtilities.isValidName(name)) {
panelManager.getWizardManager().setStatusMessage(
name + " contains invalid characters");
if (!NamingUtilities.isValidProjectName(name)) {
panelManager.getWizardManager().setStatusMessage("Invalid project repository name");
return false;
}
//
@ -218,9 +217,8 @@ public class RepositoryPanel extends AbstractWizardJPanel {
if (createRepButton.isSelected()) {
String name = nameField.getText();
if (name.length() != 0) {
if (!NamingUtilities.isValidName(name)) {
msg = name + " contains invalid characters";
if (!NamingUtilities.isValidProjectName(name)) {
msg = "Invalid project repository name";
}
else if (listModel.contains(name)) {
msg = name + " already exists";

View file

@ -271,7 +271,7 @@ class SelectProjectPanel extends AbstractWizardJPanel {
projectName =
projectName.substring(0, projectName.length() - PROJECT_EXTENSION.length());
}
if (projectName.length() == 0 || !NamingUtilities.isValidName(projectName)) {
if (projectName.length() == 0 || !NamingUtilities.isValidProjectName(projectName)) {
msg = "Please specify valid project name";
}
else {