SMB1FileSystemHelper.Query.cs 9.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194
  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 partial class SMB1FileSystemHelper
  15. {
  16. public static NTStatus GetFileInformation(out QueryInformation result, FileSystemEntry entry, bool deletePending, QueryInformationLevel informationLevel)
  17. {
  18. switch (informationLevel)
  19. {
  20. case QueryInformationLevel.SMB_INFO_STANDARD:
  21. {
  22. QueryInfoStandard information = new QueryInfoStandard();
  23. information.CreationDateTime = entry.CreationTime;
  24. information.LastAccessDateTime = entry.LastAccessTime;
  25. information.LastWriteDateTime = entry.LastWriteTime;
  26. information.FileDataSize = (uint)Math.Min(entry.Size, UInt32.MaxValue);
  27. information.AllocationSize = (uint)Math.Min(NTFileSystemHelper.GetAllocationSize(entry.Size), UInt32.MaxValue);
  28. result = information;
  29. return NTStatus.STATUS_SUCCESS;
  30. }
  31. case QueryInformationLevel.SMB_INFO_QUERY_EA_SIZE:
  32. {
  33. QueryEASize information = new QueryEASize();
  34. information.CreationDateTime = entry.CreationTime;
  35. information.LastAccessDateTime = entry.LastAccessTime;
  36. information.LastWriteDateTime = entry.LastWriteTime;
  37. information.FileDataSize = (uint)Math.Min(entry.Size, UInt32.MaxValue);
  38. information.AllocationSize = (uint)Math.Min(NTFileSystemHelper.GetAllocationSize(entry.Size), UInt32.MaxValue);
  39. information.Attributes = GetFileAttributes(entry);
  40. information.EASize = 0;
  41. result = information;
  42. return NTStatus.STATUS_SUCCESS;
  43. }
  44. case QueryInformationLevel.SMB_INFO_QUERY_EAS_FROM_LIST:
  45. {
  46. result = null;
  47. return NTStatus.STATUS_NOT_IMPLEMENTED;
  48. }
  49. case QueryInformationLevel.SMB_INFO_QUERY_ALL_EAS:
  50. {
  51. result = null;
  52. return NTStatus.STATUS_NOT_IMPLEMENTED;
  53. }
  54. case QueryInformationLevel.SMB_INFO_IS_NAME_VALID:
  55. {
  56. result = null;
  57. return NTStatus.STATUS_NOT_IMPLEMENTED;
  58. }
  59. case QueryInformationLevel.SMB_QUERY_FILE_BASIC_INFO:
  60. {
  61. QueryFileBasicInfo information = new QueryFileBasicInfo();
  62. information.CreationDateTime = entry.CreationTime;
  63. information.LastAccessDateTime = entry.LastAccessTime;
  64. information.LastWriteDateTime = entry.LastWriteTime;
  65. information.LastChangeTime = entry.LastWriteTime;
  66. information.ExtFileAttributes = GetExtendedFileAttributes(entry);
  67. result = information;
  68. return NTStatus.STATUS_SUCCESS;
  69. }
  70. case QueryInformationLevel.SMB_QUERY_FILE_STANDARD_INFO:
  71. {
  72. QueryFileStandardInfo information = new QueryFileStandardInfo();
  73. information.AllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  74. information.EndOfFile = entry.Size;
  75. information.DeletePending = deletePending;
  76. information.Directory = entry.IsDirectory;
  77. result = information;
  78. return NTStatus.STATUS_SUCCESS;
  79. }
  80. case QueryInformationLevel.SMB_QUERY_FILE_EA_INFO:
  81. {
  82. QueryFileExtendedAttributeInfo information = new QueryFileExtendedAttributeInfo();
  83. information.EASize = 0;
  84. result = information;
  85. return NTStatus.STATUS_SUCCESS;
  86. }
  87. case QueryInformationLevel.SMB_QUERY_FILE_NAME_INFO:
  88. {
  89. QueryFileNameInfo information = new QueryFileNameInfo();
  90. information.FileName = entry.Name;
  91. result = information;
  92. return NTStatus.STATUS_SUCCESS;
  93. }
  94. case QueryInformationLevel.SMB_QUERY_FILE_ALL_INFO:
  95. {
  96. QueryFileAllInfo information = new QueryFileAllInfo();
  97. information.CreationDateTime = entry.CreationTime;
  98. information.LastAccessDateTime = entry.LastAccessTime;
  99. information.LastWriteDateTime = entry.LastWriteTime;
  100. information.ExtFileAttributes = GetExtendedFileAttributes(entry);
  101. information.LastChangeTime = entry.LastWriteTime;
  102. information.AllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  103. information.EndOfFile = entry.Size;
  104. information.DeletePending = deletePending;
  105. information.Directory = entry.IsDirectory;
  106. information.EASize = 0;
  107. information.FileName = entry.Name;
  108. result = information;
  109. return NTStatus.STATUS_SUCCESS;
  110. }
  111. case QueryInformationLevel.SMB_QUERY_FILE_ALT_NAME_INFO:
  112. {
  113. QueryFileAltNameInfo information = new QueryFileAltNameInfo();
  114. information.FileName = NTFileSystemHelper.GetShortName(entry.Name);
  115. result = information;
  116. return NTStatus.STATUS_SUCCESS;
  117. }
  118. case QueryInformationLevel.SMB_QUERY_FILE_STREAM_INFO:
  119. {
  120. QueryFileStreamInfo information = new QueryFileStreamInfo();
  121. information.StreamSize = entry.Size;
  122. information.StreamAllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  123. information.StreamName = "::$DATA";
  124. result = information;
  125. return NTStatus.STATUS_SUCCESS;
  126. }
  127. case QueryInformationLevel.SMB_QUERY_FILE_COMPRESSION_INFO:
  128. {
  129. QueryFileCompressionInfo information = new QueryFileCompressionInfo();
  130. information.CompressionFormat = CompressionFormat.COMPRESSION_FORMAT_NONE;
  131. result = information;
  132. return NTStatus.STATUS_SUCCESS;
  133. }
  134. default:
  135. {
  136. result = null;
  137. return NTStatus.STATUS_OS2_INVALID_LEVEL;
  138. }
  139. }
  140. }
  141. public static SMBFileAttributes GetFileAttributes(FileSystemEntry entry)
  142. {
  143. SMBFileAttributes attributes = SMBFileAttributes.Normal;
  144. if (entry.IsHidden)
  145. {
  146. attributes |= SMBFileAttributes.Hidden;
  147. }
  148. if (entry.IsReadonly)
  149. {
  150. attributes |= SMBFileAttributes.ReadOnly;
  151. }
  152. if (entry.IsArchived)
  153. {
  154. attributes |= SMBFileAttributes.Archive;
  155. }
  156. if (entry.IsDirectory)
  157. {
  158. attributes |= SMBFileAttributes.Directory;
  159. }
  160. return attributes;
  161. }
  162. public static ExtendedFileAttributes GetExtendedFileAttributes(FileSystemEntry entry)
  163. {
  164. ExtendedFileAttributes attributes = 0;
  165. if (entry.IsHidden)
  166. {
  167. attributes |= ExtendedFileAttributes.Hidden;
  168. }
  169. if (entry.IsReadonly)
  170. {
  171. attributes |= ExtendedFileAttributes.Readonly;
  172. }
  173. if (entry.IsArchived)
  174. {
  175. attributes |= ExtendedFileAttributes.Archive;
  176. }
  177. if (entry.IsDirectory)
  178. {
  179. attributes |= ExtendedFileAttributes.Directory;
  180. }
  181. if ((uint)attributes == 0)
  182. {
  183. attributes = ExtendedFileAttributes.Normal;
  184. }
  185. return attributes;
  186. }
  187. }
  188. }