InfoHelper.cs 18 KB

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