NTFileSystemHelper: Added IsFileNameInExpression helper method

This commit is contained in:
Tal Aloni 2017-01-18 00:42:31 +02:00
parent 67976270ca
commit bf9d519e9f

View file

@ -78,6 +78,25 @@ namespace SMBLibrary.Server
return entries; return entries;
} }
/// <param name="expression">Expression as described in [MS-FSA] 2.1.4.4</param>
private static List<FileSystemEntry> GetFiltered(List<FileSystemEntry> entries, string expression)
{
if (expression == "*")
{
return entries;
}
List<FileSystemEntry> result = new List<FileSystemEntry>();
foreach (FileSystemEntry entry in entries)
{
if (IsFileNameInExpression(entry.Name, expression))
{
result.Add(entry);
}
}
return result;
}
// [MS-FSA] 2.1.4.4 // [MS-FSA] 2.1.4.4
// The FileName is string compared with Expression using the following wildcard rules: // The FileName is string compared with Expression using the following wildcard rules:
// * (asterisk) Matches zero or more characters. // * (asterisk) Matches zero or more characters.
@ -85,55 +104,51 @@ namespace SMBLibrary.Server
// DOS_DOT (" quotation mark) Matches either a period or zero characters beyond the name string. // DOS_DOT (" quotation mark) Matches either a period or zero characters beyond the name string.
// DOS_QM (> greater than) Matches any single character or, upon encountering a period or end of name string, advances the expression to the end of the set of contiguous DOS_QMs. // DOS_QM (> greater than) Matches any single character or, upon encountering a period or end of name string, advances the expression to the end of the set of contiguous DOS_QMs.
// DOS_STAR (< less than) Matches zero or more characters until encountering and matching the final . in the name. // DOS_STAR (< less than) Matches zero or more characters until encountering and matching the final . in the name.
private static List<FileSystemEntry> GetFiltered(List<FileSystemEntry> entries, string searchPattern) private static bool IsFileNameInExpression(string fileName, string expression)
{ {
if (searchPattern == String.Empty || searchPattern == "*") if (expression == "*")
{ {
return entries; return true;
}
else if (expression.EndsWith("*")) // expression.Length > 1
{
string desiredFileNameStart = expression.Substring(0, expression.Length - 1);
bool findExactNameWithoutExtension = false;
if (desiredFileNameStart.EndsWith("\""))
{
findExactNameWithoutExtension = true;
desiredFileNameStart = desiredFileNameStart.Substring(0, desiredFileNameStart.Length - 1);
} }
List<FileSystemEntry> result = new List<FileSystemEntry>(); if (!findExactNameWithoutExtension)
if (searchPattern.EndsWith("*") && searchPattern.Length > 1)
{ {
string fileNameStart = searchPattern.Substring(0, searchPattern.Length - 1); if (fileName.StartsWith(desiredFileNameStart, StringComparison.InvariantCultureIgnoreCase))
bool exactNameWithoutExtensionMatch = false;
if (fileNameStart.EndsWith("\""))
{ {
exactNameWithoutExtensionMatch = true; return true;
fileNameStart = fileNameStart.Substring(0, fileNameStart.Length - 1);
}
foreach (FileSystemEntry entry in entries)
{
if (!exactNameWithoutExtensionMatch)
{
if (entry.Name.StartsWith(fileNameStart, StringComparison.InvariantCultureIgnoreCase))
{
result.Add(entry);
} }
} }
else else
{ {
if (entry.Name.StartsWith(fileNameStart + ".", StringComparison.InvariantCultureIgnoreCase) || if (fileName.StartsWith(desiredFileNameStart + ".", StringComparison.InvariantCultureIgnoreCase) ||
entry.Name.Equals(fileNameStart, StringComparison.InvariantCultureIgnoreCase)) fileName.Equals(desiredFileNameStart, StringComparison.InvariantCultureIgnoreCase))
{ {
result.Add(entry); return true;
} }
} }
} }
} else if (expression.StartsWith("<"))
else if (searchPattern.StartsWith("<"))
{ {
string fileNameEnd = searchPattern.Substring(1); string desiredFileNameEnd = expression.Substring(1);
foreach (FileSystemEntry entry in entries) if (fileName.EndsWith(desiredFileNameEnd, StringComparison.InvariantCultureIgnoreCase))
{ {
if (entry.Name.EndsWith(fileNameEnd, StringComparison.InvariantCultureIgnoreCase)) return true;
}
}
else if (String.Equals(fileName, expression, StringComparison.CurrentCultureIgnoreCase))
{ {
result.Add(entry); return true;
} }
} return false;
}
return result;
} }
} }
} }