QueryInformationHelper.cs 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240
  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 QueryInformationHelper
  14. {
  15. /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
  16. public static FileInformationClass ToFileInformationClass(QueryInformationLevel informationLevel)
  17. {
  18. switch (informationLevel)
  19. {
  20. case QueryInformationLevel.SMB_QUERY_FILE_BASIC_INFO:
  21. return FileInformationClass.FileBasicInformation;
  22. case QueryInformationLevel.SMB_QUERY_FILE_STANDARD_INFO:
  23. return FileInformationClass.FileStandardInformation;
  24. case QueryInformationLevel.SMB_QUERY_FILE_EA_INFO:
  25. return FileInformationClass.FileEaInformation;
  26. case QueryInformationLevel.SMB_QUERY_FILE_NAME_INFO:
  27. return FileInformationClass.FileNameInformation;
  28. case QueryInformationLevel.SMB_QUERY_FILE_ALL_INFO:
  29. return FileInformationClass.FileAllInformation;
  30. case QueryInformationLevel.SMB_QUERY_FILE_ALT_NAME_INFO:
  31. return FileInformationClass.FileAlternateNameInformation;
  32. case QueryInformationLevel.SMB_QUERY_FILE_STREAM_INFO:
  33. return FileInformationClass.FileStreamInformation;
  34. case QueryInformationLevel.SMB_QUERY_FILE_COMPRESSION_INFO:
  35. return FileInformationClass.FileCompressionInformation;
  36. default:
  37. throw new UnsupportedInformationLevelException();
  38. }
  39. }
  40. public static QueryInformation FromFileInformation(FileInformation fileInformation)
  41. {
  42. if (fileInformation is FileBasicInformation)
  43. {
  44. FileBasicInformation fileBasicInfo = (FileBasicInformation)fileInformation;
  45. QueryFileBasicInfo result = new QueryFileBasicInfo();
  46. result.CreationTime = fileBasicInfo.CreationTime;
  47. result.LastAccessTime = fileBasicInfo.LastAccessTime;
  48. result.LastWriteTime = fileBasicInfo.LastWriteTime;
  49. result.LastChangeTime = fileBasicInfo.ChangeTime;
  50. result.ExtFileAttributes = (ExtendedFileAttributes)fileBasicInfo.FileAttributes;
  51. return result;
  52. }
  53. else if (fileInformation is FileStandardInformation)
  54. {
  55. FileStandardInformation fileStandardInfo = (FileStandardInformation)fileInformation;
  56. QueryFileStandardInfo result = new QueryFileStandardInfo();
  57. result.AllocationSize = fileStandardInfo.AllocationSize;
  58. result.EndOfFile = fileStandardInfo.EndOfFile;
  59. result.DeletePending = fileStandardInfo.DeletePending;
  60. result.Directory = fileStandardInfo.Directory;
  61. return result;
  62. }
  63. else if (fileInformation is FileEaInformation)
  64. {
  65. FileEaInformation fileEAInfo = (FileEaInformation)fileInformation;
  66. QueryFileEaInfo result = new QueryFileEaInfo();
  67. result.EaSize = fileEAInfo.EaSize;
  68. return result;
  69. }
  70. else if (fileInformation is FileNameInformation)
  71. {
  72. FileNameInformation fileNameInfo = (FileNameInformation)fileInformation;
  73. QueryFileNameInfo result = new QueryFileNameInfo();
  74. result.FileName = fileNameInfo.FileName;
  75. return result;
  76. }
  77. else if (fileInformation is FileAllInformation)
  78. {
  79. FileAllInformation fileAllInfo = (FileAllInformation)fileInformation;
  80. QueryFileAllInfo result = new QueryFileAllInfo();
  81. result.CreationTime = fileAllInfo.BasicInformation.CreationTime;
  82. result.LastAccessTime = fileAllInfo.BasicInformation.LastAccessTime;
  83. result.LastWriteTime = fileAllInfo.BasicInformation.LastWriteTime;
  84. result.LastChangeTime = fileAllInfo.BasicInformation.ChangeTime;
  85. result.ExtFileAttributes = (ExtendedFileAttributes)fileAllInfo.BasicInformation.FileAttributes;
  86. result.AllocationSize = fileAllInfo.StandardInformation.AllocationSize;
  87. result.EndOfFile = fileAllInfo.StandardInformation.EndOfFile;
  88. result.DeletePending = fileAllInfo.StandardInformation.DeletePending;
  89. result.Directory = fileAllInfo.StandardInformation.Directory;
  90. result.EaSize = fileAllInfo.EaInformation.EaSize;
  91. result.FileName = fileAllInfo.NameInformation.FileName;
  92. return result;
  93. }
  94. else if (fileInformation is FileAlternateNameInformation)
  95. {
  96. FileAlternateNameInformation fileAltNameInfo = (FileAlternateNameInformation)fileInformation;
  97. QueryFileAltNameInfo result = new QueryFileAltNameInfo();
  98. result.FileName = fileAltNameInfo.FileName;
  99. return result;
  100. }
  101. else if (fileInformation is FileStreamInformation)
  102. {
  103. FileStreamInformation fileStreamInfo = (FileStreamInformation)fileInformation;
  104. QueryFileStreamInfo result = new QueryFileStreamInfo();
  105. result.Entries.AddRange(fileStreamInfo.Entries);
  106. return result;
  107. }
  108. else if (fileInformation is FileCompressionInformation)
  109. {
  110. FileCompressionInformation fileCompressionInfo = (FileCompressionInformation)fileInformation;
  111. QueryFileCompressionInfo result = new QueryFileCompressionInfo();
  112. result.CompressedFileSize = fileCompressionInfo.CompressedFileSize;
  113. result.CompressionFormat = fileCompressionInfo.CompressionFormat;
  114. result.CompressionUnitShift = fileCompressionInfo.CompressionUnitShift;
  115. result.ChunkShift = fileCompressionInfo.ChunkShift;
  116. result.ClusterShift = fileCompressionInfo.ClusterShift;
  117. result.Reserved = fileCompressionInfo.Reserved;
  118. return result;
  119. }
  120. else
  121. {
  122. throw new NotImplementedException();
  123. }
  124. }
  125. /// <exception cref="SMBLibrary.UnsupportedInformationLevelException"></exception>
  126. public static QueryInformationLevel ToFileInformationLevel(FileInformationClass informationClass)
  127. {
  128. switch (informationClass)
  129. {
  130. case FileInformationClass.FileBasicInformation:
  131. return QueryInformationLevel.SMB_QUERY_FILE_BASIC_INFO;
  132. case FileInformationClass.FileStandardInformation:
  133. return QueryInformationLevel.SMB_QUERY_FILE_STANDARD_INFO;
  134. case FileInformationClass.FileEaInformation:
  135. return QueryInformationLevel.SMB_QUERY_FILE_EA_INFO;
  136. case FileInformationClass.FileNameInformation:
  137. return QueryInformationLevel.SMB_QUERY_FILE_NAME_INFO;
  138. case FileInformationClass.FileAllInformation:
  139. return QueryInformationLevel.SMB_QUERY_FILE_ALL_INFO;
  140. case FileInformationClass.FileAlternateNameInformation:
  141. return QueryInformationLevel.SMB_QUERY_FILE_ALT_NAME_INFO;
  142. case FileInformationClass.FileStreamInformation:
  143. return QueryInformationLevel.SMB_QUERY_FILE_STREAM_INFO;
  144. case FileInformationClass.FileCompressionInformation:
  145. return QueryInformationLevel.SMB_QUERY_FILE_COMPRESSION_INFO;
  146. default:
  147. throw new UnsupportedInformationLevelException();
  148. }
  149. }
  150. public static FileInformation ToFileInformation(QueryInformation queryInformation)
  151. {
  152. if (queryInformation is QueryFileBasicInfo)
  153. {
  154. QueryFileBasicInfo queryFileBasicInfo = (QueryFileBasicInfo)queryInformation;
  155. FileBasicInformation result = new FileBasicInformation();
  156. result.CreationTime = queryFileBasicInfo.CreationTime;
  157. result.LastAccessTime = queryFileBasicInfo.LastAccessTime;
  158. result.LastWriteTime = queryFileBasicInfo.LastWriteTime;
  159. result.ChangeTime = queryFileBasicInfo.LastChangeTime;
  160. result.FileAttributes = (FileAttributes)queryFileBasicInfo.ExtFileAttributes;
  161. return result;
  162. }
  163. else if (queryInformation is QueryFileStandardInfo)
  164. {
  165. QueryFileStandardInfo queryFileStandardInfo = (QueryFileStandardInfo)queryInformation;
  166. FileStandardInformation result = new FileStandardInformation();
  167. result.AllocationSize = queryFileStandardInfo.AllocationSize;
  168. result.EndOfFile = queryFileStandardInfo.EndOfFile;
  169. result.DeletePending = queryFileStandardInfo.DeletePending;
  170. result.Directory = queryFileStandardInfo.Directory;
  171. return result;
  172. }
  173. else if (queryInformation is QueryFileEaInfo)
  174. {
  175. QueryFileEaInfo queryFileEaInfo = (QueryFileEaInfo)queryInformation;
  176. FileEaInformation result = new FileEaInformation();
  177. result.EaSize = queryFileEaInfo.EaSize;
  178. return result;
  179. }
  180. else if (queryInformation is QueryFileNameInfo)
  181. {
  182. QueryFileNameInfo queryFileNameInfo = (QueryFileNameInfo)queryInformation;
  183. FileNameInformation result = new FileNameInformation();
  184. result.FileName = queryFileNameInfo.FileName;
  185. return result;
  186. }
  187. else if (queryInformation is QueryFileAllInfo)
  188. {
  189. QueryFileAllInfo queryFileAllInfo = (QueryFileAllInfo)queryInformation;
  190. FileAllInformation result = new FileAllInformation();
  191. result.BasicInformation.CreationTime = queryFileAllInfo.CreationTime;
  192. result.BasicInformation.LastAccessTime = queryFileAllInfo.LastAccessTime;
  193. result.BasicInformation.LastWriteTime = queryFileAllInfo.LastWriteTime;
  194. result.BasicInformation.ChangeTime = queryFileAllInfo.LastChangeTime;
  195. result.BasicInformation.FileAttributes = (FileAttributes)queryFileAllInfo.ExtFileAttributes;
  196. result.StandardInformation.AllocationSize = queryFileAllInfo.AllocationSize;
  197. result.StandardInformation.EndOfFile = queryFileAllInfo.EndOfFile;
  198. result.StandardInformation.DeletePending = queryFileAllInfo.DeletePending;
  199. result.StandardInformation.Directory = queryFileAllInfo.Directory;
  200. result.EaInformation.EaSize = queryFileAllInfo.EaSize;
  201. result.NameInformation.FileName = queryFileAllInfo.FileName;
  202. return result;
  203. }
  204. else if (queryInformation is QueryFileAltNameInfo)
  205. {
  206. QueryFileAltNameInfo queryFileAltNameInfo = (QueryFileAltNameInfo)queryInformation;
  207. FileAlternateNameInformation result = new FileAlternateNameInformation();
  208. result.FileName = queryFileAltNameInfo.FileName;
  209. return result;
  210. }
  211. else if (queryInformation is QueryFileStreamInfo)
  212. {
  213. QueryFileStreamInfo queryFileStreamInfo = (QueryFileStreamInfo)queryInformation;
  214. FileStreamInformation result = new FileStreamInformation();
  215. result.Entries.AddRange(queryFileStreamInfo.Entries);
  216. return result;
  217. }
  218. else if (queryInformation is QueryFileCompressionInfo)
  219. {
  220. QueryFileCompressionInfo queryFileCompressionInfo = (QueryFileCompressionInfo)queryInformation;
  221. FileCompressionInformation result = new FileCompressionInformation();
  222. result.CompressedFileSize = queryFileCompressionInfo.CompressedFileSize;
  223. result.CompressionFormat = queryFileCompressionInfo.CompressionFormat;
  224. result.CompressionUnitShift = queryFileCompressionInfo.CompressionUnitShift;
  225. result.ChunkShift = queryFileCompressionInfo.ChunkShift;
  226. result.ClusterShift = queryFileCompressionInfo.ClusterShift;
  227. result.Reserved = queryFileCompressionInfo.Reserved;
  228. return result;
  229. }
  230. else
  231. {
  232. throw new NotImplementedException();
  233. }
  234. }
  235. }
  236. }