Browse Source

Minor code refactoring

Tal Aloni 8 years ago
parent
commit
a23d2d86f9

+ 11 - 32
SMBLibrary/Server/Helpers/NTFileSystemHelper.Find.cs

@@ -13,34 +13,8 @@ namespace SMBLibrary.Server
 {
     public partial class NTFileSystemHelper
     {
-        // Filename pattern examples:
-        // '\Directory' - Get the directory entry
-        // '\Directory\*' - List the directory files
-        // '\Directory\s*' - List the directory files starting with s (cmd.exe will use this syntax when entering 's' and hitting tab for autocomplete)
-        // '\Directory\<.inf' (Update driver will use this syntax)
-        // '\Directory\exefile"*' (cmd.exe will use this syntax when entering an exe without its extension, explorer will use this opening a directory from the run menu)
-        /// <param name="fileNamePattern">The filename pattern to search for. This field MAY contain wildcard characters</param>
-        /// <returns>null if the path does not exist</returns>
-        /// <exception cref="System.UnauthorizedAccessException"></exception>
-        public static NTStatus FindEntries(out List<FileSystemEntry> entries, IFileSystem fileSystem, string fileNamePattern)
-        {
-            int separatorIndex = fileNamePattern.LastIndexOf('\\');
-            if (separatorIndex >= 0)
-            {
-                string path = fileNamePattern.Substring(0, separatorIndex + 1);
-                string expression = fileNamePattern.Substring(separatorIndex + 1);
-                return FindEntries(out entries, fileSystem, path, expression);
-            }
-            else
-            {
-                entries = null;
-                return NTStatus.STATUS_INVALID_PARAMETER;
-            }
-        }
-
-        /// <param name="expression">Expression as described in [MS-FSA] 2.1.4.4</param>
-        /// <returns>null if the path does not exist</returns>
-        public static NTStatus FindEntries(out List<FileSystemEntry> entries, IFileSystem fileSystem, string path, string expression)
+        /// <param name="fileName">Expression as described in [MS-FSA] 2.1.4.4</param>
+        public static NTStatus FindEntries(out List<FileSystemEntry> entries, IFileSystem fileSystem, string path, string fileName)
         {
             entries = null;
             FileSystemEntry entry = fileSystem.GetEntry(path);
@@ -49,12 +23,17 @@ namespace SMBLibrary.Server
                 return NTStatus.STATUS_NO_SUCH_FILE;
             }
 
-            if (expression == String.Empty)
+            if (!entry.IsDirectory)
+            {
+                return NTStatus.STATUS_INVALID_PARAMETER;
+            }
+
+            if (fileName == String.Empty)
             {
                 return NTStatus.STATUS_INVALID_PARAMETER;
             }
 
-            bool findExactName = !ContainsWildcardCharacters(expression);
+            bool findExactName = !ContainsWildcardCharacters(fileName);
 
             if (!findExactName)
             {
@@ -68,7 +47,7 @@ namespace SMBLibrary.Server
                     return status; ;
                 }
 
-                entries = GetFiltered(entries, expression);
+                entries = GetFiltered(entries, fileName);
 
                 // Windows will return "." and ".." when enumerating directory files.
                 // The SMB1 / SMB2 specifications mandate that when zero entries are found, the server SHOULD / MUST return STATUS_NO_SUCH_FILE.
@@ -84,7 +63,7 @@ namespace SMBLibrary.Server
             else
             {
                 path = FileSystem.GetDirectoryPath(path);
-                entry = fileSystem.GetEntry(path + expression);
+                entry = fileSystem.GetEntry(path + fileName);
                 if (entry == null)
                 {
                     return NTStatus.STATUS_NO_SUCH_FILE;

+ 24 - 0
SMBLibrary/Server/SMB1/SMB1FileSystemHelper.Find.cs

@@ -14,6 +14,30 @@ namespace SMBLibrary.Server.SMB1
 {
     public partial class SMB1FileSystemHelper
     {
+        // Filename pattern examples:
+        // '\Directory' - Get the directory entry
+        // '\Directory\*' - List the directory files
+        // '\Directory\s*' - List the directory files starting with s (cmd.exe will use this syntax when entering 's' and hitting tab for autocomplete)
+        // '\Directory\<.inf' (Update driver will use this syntax)
+        // '\Directory\exefile"*' (cmd.exe will use this syntax when entering an exe without its extension, explorer will use this opening a directory from the run menu)
+        /// <param name="fileNamePattern">The filename pattern to search for. This field MAY contain wildcard characters</param>
+        /// <exception cref="System.UnauthorizedAccessException"></exception>
+        public static NTStatus FindEntries(out List<FileSystemEntry> entries, IFileSystem fileSystem, string fileNamePattern)
+        {
+            int separatorIndex = fileNamePattern.LastIndexOf('\\');
+            if (separatorIndex >= 0)
+            {
+                string path = fileNamePattern.Substring(0, separatorIndex + 1);
+                string fileName = fileNamePattern.Substring(separatorIndex + 1);
+                return NTFileSystemHelper.FindEntries(out entries, fileSystem, path, fileName);
+            }
+            else
+            {
+                entries = null;
+                return NTStatus.STATUS_INVALID_PARAMETER;
+            }
+        }
+
         /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
         public static FindInformationList GetFindInformationList(List<FileSystemEntry> entries, FindInformationLevel informationLevel, bool isUnicode, bool returnResumeKeys, int maxLength)
         {

+ 1 - 1
SMBLibrary/Server/SMB1/Transaction2SubcommandHelper.cs

@@ -22,7 +22,7 @@ namespace SMBLibrary.Server.SMB1
             string fileNamePattern = subcommand.FileName;
 
             List<FileSystemEntry> entries;
-            NTStatus searchStatus = NTFileSystemHelper.FindEntries(out entries, fileSystem, fileNamePattern);
+            NTStatus searchStatus = SMB1FileSystemHelper.FindEntries(out entries, fileSystem, fileNamePattern);
             if (searchStatus != NTStatus.STATUS_SUCCESS)
             {
                 state.LogToServer(Severity.Verbose, "FindFirst2: Searched for '{0}', NTStatus: {1}", fileNamePattern, searchStatus.ToString());