Browse Source

Minor refactoring

Tal Aloni 8 years ago
parent
commit
859e939cff

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

@@ -14,6 +14,24 @@ namespace SMBLibrary.Server.SMB1
 {
     public partial class SMB1FileSystemHelper
     {
+        /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
+        public static FindInformationList GetFindInformationList(List<FileSystemEntry> entries, FindInformationLevel informationLevel, bool isUnicode, bool returnResumeKeys, int maxLength)
+        {
+            FindInformationList result = new FindInformationList();
+            for (int index = 0; index < entries.Count; index++)
+            {
+                FindInformation infoEntry = GetFindInformation(entries[index], informationLevel, isUnicode, returnResumeKeys);
+                result.Add(infoEntry);
+                if (result.GetLength(isUnicode) > maxLength)
+                {
+                    result.RemoveAt(result.Count - 1);
+                    break;
+                }
+            }
+            return result;
+        }
+
+        /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
         public static FindInformation GetFindInformation(FileSystemEntry entry, FindInformationLevel informationLevel, bool isUnicode, bool returnResumeKeys)
         {
             switch (informationLevel)

+ 8 - 28
SMBLibrary/Server/SMB1/Transaction2SubcommandHelper.cs

@@ -29,6 +29,7 @@ namespace SMBLibrary.Server.SMB1
                 header.Status = searchStatus;
                 return null;
             }
+            // We ignore SearchAttributes
             state.LogToServer(Severity.Verbose, "FindFirst2: Searched for '{0}', found {1} matching entries", fileNamePattern, entries.Count);
 
             // [MS-CIFS] If no matching entries are found, the server SHOULD fail the request with STATUS_NO_SUCH_FILE.
@@ -40,18 +41,9 @@ namespace SMBLibrary.Server.SMB1
 
             bool returnResumeKeys = (subcommand.Flags & FindFlags.SMB_FIND_RETURN_RESUME_KEYS) > 0;
             int entriesToReturn = Math.Min(subcommand.SearchCount, entries.Count);
-            // We ignore SearchAttributes
-            FindInformationList findInformationList = new FindInformationList();
-            for (int index = 0; index < entriesToReturn; index++)
-            {
-                FindInformation infoEntry = SMB1FileSystemHelper.GetFindInformation(entries[index], subcommand.InformationLevel, header.UnicodeFlag, returnResumeKeys);
-                findInformationList.Add(infoEntry);
-                if (findInformationList.GetLength(header.UnicodeFlag) > state.GetMaxDataCount(header.PID))
-                {
-                    findInformationList.RemoveAt(findInformationList.Count - 1);
-                    break;
-                }
-            }
+            List<FileSystemEntry> temp = entries.GetRange(0, entriesToReturn);
+            int maxLength = (int)state.GetMaxDataCount(header.PID).Value;
+            FindInformationList findInformationList = SMB1FileSystemHelper.GetFindInformationList(temp, subcommand.InformationLevel, header.UnicodeFlag, returnResumeKeys, maxLength);
             int returnCount = findInformationList.Count;
             Transaction2FindFirst2Response response = new Transaction2FindFirst2Response();
             response.SetFindInformationList(findInformationList, header.UnicodeFlag);
@@ -88,22 +80,10 @@ namespace SMBLibrary.Server.SMB1
             }
 
             bool returnResumeKeys = (subcommand.Flags & FindFlags.SMB_FIND_RETURN_RESUME_KEYS) > 0;
-            FindInformationList findInformationList = new FindInformationList();
-            for (int index = openSearch.EnumerationLocation; index < openSearch.Entries.Count; index++)
-            {
-                FindInformation infoEntry = SMB1FileSystemHelper.GetFindInformation(openSearch.Entries[index], subcommand.InformationLevel, header.UnicodeFlag, returnResumeKeys);
-                findInformationList.Add(infoEntry);
-                if (findInformationList.GetLength(header.UnicodeFlag) > state.GetMaxDataCount(header.PID))
-                {
-                    findInformationList.RemoveAt(findInformationList.Count - 1);
-                    break;
-                }
-
-                if (findInformationList.Count == subcommand.SearchCount)
-                {
-                    break;
-                }
-            }
+            int maxLength = (int)state.GetMaxDataCount(header.PID).Value;
+            int maxCount = Math.Min(openSearch.Entries.Count - openSearch.EnumerationLocation, subcommand.SearchCount);
+            List<FileSystemEntry> temp = openSearch.Entries.GetRange(openSearch.EnumerationLocation, maxCount);
+            FindInformationList findInformationList = SMB1FileSystemHelper.GetFindInformationList(temp, subcommand.InformationLevel, header.UnicodeFlag, returnResumeKeys, maxLength);
             int returnCount = findInformationList.Count;
             Transaction2FindNext2Response response = new Transaction2FindNext2Response();
             response.SetFindInformationList(findInformationList, header.UnicodeFlag);