InfoHelper.cs 17 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332
  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 System.Text;
  10. using SMBLibrary.SMB1;
  11. using Utilities;
  12. namespace SMBLibrary.Server.SMB1
  13. {
  14. public class InfoHelper
  15. {
  16. internal static FindInformation FromFileSystemEntry(FileSystemEntry entry, FindInformationLevel informationLevel, bool isUnicode, bool returnResumeKeys)
  17. {
  18. switch (informationLevel)
  19. {
  20. case FindInformationLevel.SMB_INFO_STANDARD:
  21. {
  22. FindInfoStandard result = new FindInfoStandard(returnResumeKeys);
  23. result.CreationDateTime = entry.CreationTime;
  24. result.LastAccessDateTime = entry.LastAccessTime;
  25. result.LastWriteDateTime = entry.LastWriteTime;
  26. result.FileDataSize = (uint)Math.Min(entry.Size, UInt32.MaxValue);
  27. result.AllocationSize = (uint)Math.Min(NTFileSystemHelper.GetAllocationSize(entry.Size), UInt32.MaxValue);
  28. result.Attributes = GetFileAttributes(entry);
  29. result.FileName = entry.Name;
  30. return result;
  31. }
  32. case FindInformationLevel.SMB_INFO_QUERY_EA_SIZE:
  33. {
  34. FindInfoQueryEASize result = new FindInfoQueryEASize(returnResumeKeys);
  35. result.CreationDateTime = entry.CreationTime;
  36. result.LastAccessDateTime = entry.LastAccessTime;
  37. result.LastWriteDateTime = entry.LastWriteTime;
  38. result.FileDataSize = (uint)Math.Min(entry.Size, UInt32.MaxValue);
  39. result.AllocationSize = (uint)Math.Min(NTFileSystemHelper.GetAllocationSize(entry.Size), UInt32.MaxValue);
  40. result.Attributes = GetFileAttributes(entry);
  41. result.EASize = 0;
  42. result.FileName = entry.Name;
  43. return result;
  44. }
  45. case FindInformationLevel.SMB_INFO_QUERY_EAS_FROM_LIST:
  46. {
  47. FindInfoQueryExtendedAttributesFromList result = new FindInfoQueryExtendedAttributesFromList(returnResumeKeys);
  48. result.CreationDateTime = entry.CreationTime;
  49. result.LastAccessDateTime = entry.LastAccessTime;
  50. result.LastWriteDateTime = entry.LastWriteTime;
  51. result.FileDataSize = (uint)Math.Min(entry.Size, UInt32.MaxValue);
  52. result.AllocationSize = (uint)Math.Min(NTFileSystemHelper.GetAllocationSize(entry.Size), UInt32.MaxValue);
  53. result.Attributes = GetFileAttributes(entry);
  54. result.ExtendedAttributeList = new FullExtendedAttributeList();
  55. return result;
  56. }
  57. case FindInformationLevel.SMB_FIND_FILE_DIRECTORY_INFO:
  58. {
  59. FindFileDirectoryInfo result = new FindFileDirectoryInfo();
  60. result.CreationTime = entry.CreationTime;
  61. result.LastAccessTime = entry.LastAccessTime;
  62. result.LastWriteTime = entry.LastWriteTime;
  63. result.LastAttrChangeTime = entry.LastWriteTime;
  64. result.EndOfFile = entry.Size;
  65. result.AllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  66. result.ExtFileAttributes = GetExtendedFileAttributes(entry);
  67. result.FileName = entry.Name;
  68. return result;
  69. }
  70. case FindInformationLevel.SMB_FIND_FILE_FULL_DIRECTORY_INFO:
  71. {
  72. FindFileFullDirectoryInfo result = new FindFileFullDirectoryInfo();
  73. result.CreationTime = entry.CreationTime;
  74. result.LastAccessTime = entry.LastAccessTime;
  75. result.LastWriteTime = entry.LastWriteTime;
  76. result.LastAttrChangeTime = entry.LastWriteTime;
  77. result.EndOfFile = entry.Size;
  78. result.AllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  79. result.ExtFileAttributes = GetExtendedFileAttributes(entry);
  80. result.FileName = entry.Name;
  81. return result;
  82. }
  83. case FindInformationLevel.SMB_FIND_FILE_NAMES_INFO:
  84. {
  85. FindFileNamesInfo result = new FindFileNamesInfo();
  86. result.FileName = entry.Name;
  87. return result;
  88. }
  89. case FindInformationLevel.SMB_FIND_FILE_BOTH_DIRECTORY_INFO:
  90. {
  91. FindFileBothDirectoryInfo result = new FindFileBothDirectoryInfo();
  92. result.CreationTime = entry.CreationTime;
  93. result.LastAccessTime = entry.LastAccessTime;
  94. result.LastWriteTime = entry.LastWriteTime;
  95. result.LastChangeTime = entry.LastWriteTime;
  96. result.EndOfFile = entry.Size;
  97. result.AllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  98. result.ExtFileAttributes = GetExtendedFileAttributes(entry);
  99. result.ShortName = NTFileSystemHelper.GetShortName(entry.Name);
  100. result.FileName = entry.Name;
  101. return result;
  102. }
  103. default:
  104. {
  105. throw new UnsupportedInformationLevelException();
  106. }
  107. }
  108. }
  109. internal static QueryInformation FromFileSystemEntry(FileSystemEntry entry, bool deletePending, QueryInformationLevel informationLevel)
  110. {
  111. switch (informationLevel)
  112. {
  113. case QueryInformationLevel.SMB_INFO_STANDARD:
  114. {
  115. QueryInfoStandard result = new QueryInfoStandard();
  116. result.CreationDateTime = entry.CreationTime;
  117. result.LastAccessDateTime = entry.LastAccessTime;
  118. result.LastWriteDateTime = entry.LastWriteTime;
  119. result.FileDataSize = (uint)Math.Min(entry.Size, UInt32.MaxValue);
  120. result.AllocationSize = (uint)Math.Min(NTFileSystemHelper.GetAllocationSize(entry.Size), UInt32.MaxValue);
  121. return result;
  122. }
  123. case QueryInformationLevel.SMB_INFO_QUERY_EA_SIZE:
  124. {
  125. QueryEASize result = new QueryEASize();
  126. result.CreationDateTime = entry.CreationTime;
  127. result.LastAccessDateTime = entry.LastAccessTime;
  128. result.LastWriteDateTime = entry.LastWriteTime;
  129. result.FileDataSize = (uint)Math.Min(entry.Size, UInt32.MaxValue);
  130. result.AllocationSize = (uint)Math.Min(NTFileSystemHelper.GetAllocationSize(entry.Size), UInt32.MaxValue);
  131. result.Attributes = GetFileAttributes(entry);
  132. result.EASize = 0;
  133. return result;
  134. }
  135. case QueryInformationLevel.SMB_INFO_QUERY_EAS_FROM_LIST:
  136. {
  137. throw new NotImplementedException();
  138. }
  139. case QueryInformationLevel.SMB_INFO_QUERY_ALL_EAS:
  140. {
  141. throw new NotImplementedException();
  142. }
  143. case QueryInformationLevel.SMB_INFO_IS_NAME_VALID:
  144. {
  145. throw new NotImplementedException();
  146. }
  147. case QueryInformationLevel.SMB_QUERY_FILE_BASIC_INFO:
  148. {
  149. QueryFileBasicInfo result = new QueryFileBasicInfo();
  150. result.CreationDateTime = entry.CreationTime;
  151. result.LastAccessDateTime = entry.LastAccessTime;
  152. result.LastWriteDateTime = entry.LastWriteTime;
  153. result.LastChangeTime = entry.LastWriteTime;
  154. result.ExtFileAttributes = GetExtendedFileAttributes(entry);
  155. return result;
  156. }
  157. case QueryInformationLevel.SMB_QUERY_FILE_STANDARD_INFO:
  158. {
  159. QueryFileStandardInfo result = new QueryFileStandardInfo();
  160. result.AllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  161. result.EndOfFile = entry.Size;
  162. result.DeletePending = deletePending;
  163. result.Directory = entry.IsDirectory;
  164. return result;
  165. }
  166. case QueryInformationLevel.SMB_QUERY_FILE_EA_INFO:
  167. {
  168. QueryFileExtendedAttributeInfo result = new QueryFileExtendedAttributeInfo();
  169. result.EASize = 0;
  170. return result;
  171. }
  172. case QueryInformationLevel.SMB_QUERY_FILE_NAME_INFO:
  173. {
  174. QueryFileNameInfo result = new QueryFileNameInfo();
  175. result.FileName = entry.Name;
  176. return result;
  177. }
  178. case QueryInformationLevel.SMB_QUERY_FILE_ALL_INFO:
  179. {
  180. QueryFileAllInfo result = new QueryFileAllInfo();
  181. result.CreationDateTime = entry.CreationTime;
  182. result.LastAccessDateTime = entry.LastAccessTime;
  183. result.LastWriteDateTime = entry.LastWriteTime;
  184. result.ExtFileAttributes = GetExtendedFileAttributes(entry);
  185. result.LastChangeTime = entry.LastWriteTime;
  186. result.AllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  187. result.EndOfFile = entry.Size;
  188. result.DeletePending = deletePending;
  189. result.Directory = entry.IsDirectory;
  190. result.EASize = 0;
  191. result.FileName = entry.Name;
  192. return result;
  193. }
  194. case QueryInformationLevel.SMB_QUERY_FILE_ALT_NAME_INFO:
  195. {
  196. QueryFileAltNameInfo result = new QueryFileAltNameInfo();
  197. result.FileName = NTFileSystemHelper.GetShortName(entry.Name);
  198. return result;
  199. }
  200. case QueryInformationLevel.SMB_QUERY_FILE_STREAM_INFO:
  201. {
  202. QueryFileStreamInfo result = new QueryFileStreamInfo();
  203. result.StreamSize = entry.Size;
  204. result.StreamAllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  205. result.StreamName = "::$DATA";
  206. return result;
  207. }
  208. case QueryInformationLevel.SMB_QUERY_FILE_COMPRESSION_INFO:
  209. {
  210. QueryFileCompressionInfo result = new QueryFileCompressionInfo();
  211. result.CompressionFormat = CompressionFormat.COMPRESSION_FORMAT_NONE;
  212. return result;
  213. }
  214. default:
  215. {
  216. throw new UnsupportedInformationLevelException();
  217. }
  218. }
  219. }
  220. internal static QueryFSInformation GetFSInformation(QueryFSInformationLevel informationLevel, IFileSystem fileSystem)
  221. {
  222. switch (informationLevel)
  223. {
  224. case QueryFSInformationLevel.SMB_INFO_ALLOCATION:
  225. {
  226. QueryFSInfoAllocation result = new QueryFSInfoAllocation();
  227. result.FileSystemID = 0;
  228. result.SectorUnit = NTFileSystemHelper.ClusterSize / NTFileSystemHelper.BytesPerSector;
  229. result.UnitsTotal = (uint)Math.Min(fileSystem.Size / NTFileSystemHelper.ClusterSize, UInt32.MaxValue);
  230. result.UnitsAvailable = (uint)Math.Min(fileSystem.FreeSpace / NTFileSystemHelper.ClusterSize, UInt32.MaxValue);
  231. result.Sector = NTFileSystemHelper.BytesPerSector;
  232. return result;
  233. }
  234. case QueryFSInformationLevel.SMB_INFO_VOLUME:
  235. {
  236. QueryFSInfoVolume result = new QueryFSInfoVolume();
  237. result.VolumeLabel = String.Empty;
  238. result.VolumeSerialNumber = 0;
  239. return result;
  240. }
  241. case QueryFSInformationLevel.SMB_QUERY_FS_VOLUME_INFO:
  242. {
  243. QueryFSVolumeInfo result = new QueryFSVolumeInfo();
  244. result.VolumeCreationTime = DateTime.Now;
  245. return result;
  246. }
  247. case QueryFSInformationLevel.SMB_QUERY_FS_SIZE_INFO:
  248. {
  249. QueryFSSizeInfo result = new QueryFSSizeInfo();
  250. result.TotalAllocationUnits = (ulong)(fileSystem.Size / NTFileSystemHelper.ClusterSize);
  251. result.TotalFreeAllocationUnits = (ulong)(fileSystem.FreeSpace / NTFileSystemHelper.ClusterSize);
  252. result.BytesPerSector = NTFileSystemHelper.BytesPerSector;
  253. result.SectorsPerAllocationUnit = NTFileSystemHelper.ClusterSize / NTFileSystemHelper.BytesPerSector;
  254. return result;
  255. }
  256. case QueryFSInformationLevel.SMB_QUERY_FS_DEVICE_INFO:
  257. {
  258. QueryFSDeviceInfo result = new QueryFSDeviceInfo();
  259. result.DeviceCharacteristics = DeviceCharacteristics.FILE_DEVICE_IS_MOUNTED;
  260. result.DeviceType = DeviceType.FILE_DEVICE_DISK;
  261. return result;
  262. }
  263. case QueryFSInformationLevel.SMB_QUERY_FS_ATTRIBUTE_INFO:
  264. {
  265. QueryFSAttibuteInfo result = new QueryFSAttibuteInfo();
  266. result.FileSystemAttributes = FileSystemAttributes.FILE_UNICODE_ON_DISK;
  267. result.MaxFileNameLengthInBytes = 255;
  268. result.FileSystemName = fileSystem.Name;
  269. return result;
  270. }
  271. default:
  272. {
  273. throw new UnsupportedInformationLevelException();
  274. }
  275. }
  276. }
  277. public static SMBFileAttributes GetFileAttributes(FileSystemEntry entry)
  278. {
  279. SMBFileAttributes attributes = SMBFileAttributes.Normal;
  280. if (entry.IsHidden)
  281. {
  282. attributes |= SMBFileAttributes.Hidden;
  283. }
  284. if (entry.IsReadonly)
  285. {
  286. attributes |= SMBFileAttributes.ReadOnly;
  287. }
  288. if (entry.IsArchived)
  289. {
  290. attributes |= SMBFileAttributes.Archive;
  291. }
  292. if (entry.IsDirectory)
  293. {
  294. attributes |= SMBFileAttributes.Directory;
  295. }
  296. return attributes;
  297. }
  298. public static ExtendedFileAttributes GetExtendedFileAttributes(FileSystemEntry entry)
  299. {
  300. ExtendedFileAttributes attributes = 0;
  301. if (entry.IsHidden)
  302. {
  303. attributes |= ExtendedFileAttributes.Hidden;
  304. }
  305. if (entry.IsReadonly)
  306. {
  307. attributes |= ExtendedFileAttributes.Readonly;
  308. }
  309. if (entry.IsArchived)
  310. {
  311. attributes |= ExtendedFileAttributes.Archive;
  312. }
  313. if (entry.IsDirectory)
  314. {
  315. attributes |= ExtendedFileAttributes.Directory;
  316. }
  317. if ((uint)attributes == 0)
  318. {
  319. attributes = ExtendedFileAttributes.Normal;
  320. }
  321. return attributes;
  322. }
  323. }
  324. }