FindInformationHelper.cs 9.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164
  1. /* Copyright (C) 2014-2017 Tal Aloni <tal.aloni.il@gmail.com>. All rights reserved.
  2. *
  3. * You can redistribute this program and/or modify it under the terms of
  4. * the GNU Lesser Public License as published by the Free Software Foundation,
  5. * either version 3 of the License, or (at your option) any later version.
  6. */
  7. using System;
  8. using System.Collections.Generic;
  9. using SMBLibrary.SMB1;
  10. using Utilities;
  11. namespace SMBLibrary.SMB1
  12. {
  13. public class FindInformationHelper
  14. {
  15. /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
  16. public static FileInformationClass ToFileInformationClass(FindInformationLevel informationLevel)
  17. {
  18. switch (informationLevel)
  19. {
  20. case FindInformationLevel.SMB_FIND_FILE_DIRECTORY_INFO:
  21. return FileInformationClass.FileDirectoryInformation;
  22. case FindInformationLevel.SMB_FIND_FILE_FULL_DIRECTORY_INFO:
  23. return FileInformationClass.FileFullDirectoryInformation;
  24. case FindInformationLevel.SMB_FIND_FILE_NAMES_INFO:
  25. return FileInformationClass.FileNamesInformation;
  26. case FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
  27. return FileInformationClass.FileBothDirectoryInformation;
  28. case FindInformationLevel.SMB_FIND_FILE_ID_FULL_DIRECTORY_INFO:
  29. return FileInformationClass.FileIdFullDirectoryInformation;
  30. case FindInformationLevel.SMB_FIND_FILE_ID_BOTH_DIRECTORY_INFO:
  31. return FileInformationClass.FileIdBothDirectoryInformation;
  32. default:
  33. throw new UnsupportedInformationLevelException();
  34. }
  35. }
  36. /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
  37. public static FindInformationList ToFindInformationList(List<QueryDirectoryFileInformation> entries, bool isUnicode, int maxLength)
  38. {
  39. FindInformationList result = new FindInformationList();
  40. int pageLength = 0;
  41. for (int index = 0; index < entries.Count; index++)
  42. {
  43. FindInformation infoEntry = ToFindInformation(entries[index]);
  44. int entryLength = infoEntry.GetLength(isUnicode);
  45. if (pageLength + entryLength <= maxLength)
  46. {
  47. result.Add(infoEntry);
  48. pageLength += entryLength;
  49. }
  50. else
  51. {
  52. break;
  53. }
  54. }
  55. return result;
  56. }
  57. /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
  58. public static FindInformation ToFindInformation(QueryDirectoryFileInformation fileInformation)
  59. {
  60. if (fileInformation is FileDirectoryInformation)
  61. {
  62. FileDirectoryInformation fileDirectoryInfo = (FileDirectoryInformation)fileInformation;
  63. FindFileDirectoryInfo result = new FindFileDirectoryInfo();
  64. result.FileIndex = fileDirectoryInfo.FileIndex;
  65. result.CreationTime = fileDirectoryInfo.CreationTime;
  66. result.LastAccessTime = fileDirectoryInfo.LastAccessTime;
  67. result.LastWriteTime = fileDirectoryInfo.LastWriteTime;
  68. result.LastAttrChangeTime = fileDirectoryInfo.LastWriteTime;
  69. result.EndOfFile = fileDirectoryInfo.EndOfFile;
  70. result.AllocationSize = fileDirectoryInfo.AllocationSize;
  71. result.ExtFileAttributes = (ExtendedFileAttributes)fileDirectoryInfo.FileAttributes;
  72. result.FileName = fileDirectoryInfo.FileName;
  73. return result;
  74. }
  75. else if (fileInformation is FileFullDirectoryInformation)
  76. {
  77. FileFullDirectoryInformation fileFullDirectoryInfo = (FileFullDirectoryInformation)fileInformation;
  78. FindFileFullDirectoryInfo result = new FindFileFullDirectoryInfo();
  79. result.FileIndex = fileFullDirectoryInfo.FileIndex;
  80. result.CreationTime = fileFullDirectoryInfo.CreationTime;
  81. result.LastAccessTime = fileFullDirectoryInfo.LastAccessTime;
  82. result.LastWriteTime = fileFullDirectoryInfo.LastWriteTime;
  83. result.LastAttrChangeTime = fileFullDirectoryInfo.LastWriteTime;
  84. result.EndOfFile = fileFullDirectoryInfo.EndOfFile;
  85. result.AllocationSize = fileFullDirectoryInfo.AllocationSize;
  86. result.ExtFileAttributes = (ExtendedFileAttributes)fileFullDirectoryInfo.FileAttributes;
  87. result.EASize = fileFullDirectoryInfo.EaSize;
  88. result.FileName = fileFullDirectoryInfo.FileName;
  89. return result;
  90. }
  91. else if (fileInformation is FileNamesInformation)
  92. {
  93. FileNamesInformation fileNamesInfo = (FileNamesInformation)fileInformation;
  94. FindFileNamesInfo result = new FindFileNamesInfo();
  95. result.FileIndex = fileNamesInfo.FileIndex;
  96. result.FileName = fileNamesInfo.FileName;
  97. return result;
  98. }
  99. else if (fileInformation is FileBothDirectoryInformation)
  100. {
  101. FileBothDirectoryInformation fileBothDirectoryInfo = (FileBothDirectoryInformation)fileInformation;
  102. FindFileBothDirectoryInfo result = new FindFileBothDirectoryInfo();
  103. result.FileIndex = fileBothDirectoryInfo.FileIndex;
  104. result.CreationTime = fileBothDirectoryInfo.CreationTime;
  105. result.LastAccessTime = fileBothDirectoryInfo.LastAccessTime;
  106. result.LastWriteTime = fileBothDirectoryInfo.LastWriteTime;
  107. result.LastChangeTime = fileBothDirectoryInfo.LastWriteTime;
  108. result.EndOfFile = fileBothDirectoryInfo.EndOfFile;
  109. result.AllocationSize = fileBothDirectoryInfo.AllocationSize;
  110. result.ExtFileAttributes = (ExtendedFileAttributes)fileBothDirectoryInfo.FileAttributes;
  111. result.EASize = fileBothDirectoryInfo.EaSize;
  112. result.Reserved = fileBothDirectoryInfo.Reserved;
  113. result.ShortName = fileBothDirectoryInfo.ShortName;
  114. result.FileName = fileBothDirectoryInfo.FileName;
  115. return result;
  116. }
  117. else if (fileInformation is FileIdFullDirectoryInformation)
  118. {
  119. FileIdFullDirectoryInformation fileIDFullDirectoryInfo = (FileIdFullDirectoryInformation)fileInformation;
  120. FindFileIDFullDirectoryInfo result = new FindFileIDFullDirectoryInfo();
  121. result.FileIndex = fileIDFullDirectoryInfo.FileIndex;
  122. result.CreationTime = fileIDFullDirectoryInfo.CreationTime;
  123. result.LastAccessTime = fileIDFullDirectoryInfo.LastAccessTime;
  124. result.LastWriteTime = fileIDFullDirectoryInfo.LastWriteTime;
  125. result.LastAttrChangeTime = fileIDFullDirectoryInfo.LastWriteTime;
  126. result.EndOfFile = fileIDFullDirectoryInfo.EndOfFile;
  127. result.AllocationSize = fileIDFullDirectoryInfo.AllocationSize;
  128. result.ExtFileAttributes = (ExtendedFileAttributes)fileIDFullDirectoryInfo.FileAttributes;
  129. result.EASize = fileIDFullDirectoryInfo.EaSize;
  130. result.Reserved = fileIDFullDirectoryInfo.Reserved;
  131. result.FileID = fileIDFullDirectoryInfo.FileId;
  132. result.FileName = fileIDFullDirectoryInfo.FileName;
  133. return result;
  134. }
  135. else if (fileInformation is FileIdBothDirectoryInformation)
  136. {
  137. FileIdBothDirectoryInformation fileIDBothDirectoryInfo = (FileIdBothDirectoryInformation)fileInformation;
  138. FindFileIDBothDirectoryInfo result = new FindFileIDBothDirectoryInfo();
  139. result.FileIndex = fileIDBothDirectoryInfo.FileIndex;
  140. result.CreationTime = fileIDBothDirectoryInfo.CreationTime;
  141. result.LastAccessTime = fileIDBothDirectoryInfo.LastAccessTime;
  142. result.LastWriteTime = fileIDBothDirectoryInfo.LastWriteTime;
  143. result.LastChangeTime = fileIDBothDirectoryInfo.LastWriteTime;
  144. result.EndOfFile = fileIDBothDirectoryInfo.EndOfFile;
  145. result.AllocationSize = fileIDBothDirectoryInfo.AllocationSize;
  146. result.ExtFileAttributes = (ExtendedFileAttributes)fileIDBothDirectoryInfo.FileAttributes;
  147. result.EASize = fileIDBothDirectoryInfo.EaSize;
  148. result.Reserved = fileIDBothDirectoryInfo.Reserved1;
  149. result.ShortName = fileIDBothDirectoryInfo.ShortName;
  150. result.Reserved2 = fileIDBothDirectoryInfo.Reserved2;
  151. result.FileID = fileIDBothDirectoryInfo.FileId;
  152. result.FileName = fileIDBothDirectoryInfo.FileName;
  153. return result;
  154. }
  155. else
  156. {
  157. throw new NotImplementedException();
  158. }
  159. }
  160. }
  161. }