SMB1FileSystemHelper.Query.cs 7.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165
  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_QUERY_ALL_EAS:
  21. {
  22. result = null;
  23. return NTStatus.STATUS_NOT_IMPLEMENTED;
  24. }
  25. case QueryInformationLevel.SMB_INFO_IS_NAME_VALID:
  26. {
  27. result = null;
  28. return NTStatus.STATUS_NOT_IMPLEMENTED;
  29. }
  30. case QueryInformationLevel.SMB_QUERY_FILE_BASIC_INFO:
  31. {
  32. QueryFileBasicInfo information = new QueryFileBasicInfo();
  33. information.CreationDateTime = entry.CreationTime;
  34. information.LastAccessDateTime = entry.LastAccessTime;
  35. information.LastWriteDateTime = entry.LastWriteTime;
  36. information.LastChangeTime = entry.LastWriteTime;
  37. information.ExtFileAttributes = GetExtendedFileAttributes(entry);
  38. result = information;
  39. return NTStatus.STATUS_SUCCESS;
  40. }
  41. case QueryInformationLevel.SMB_QUERY_FILE_STANDARD_INFO:
  42. {
  43. QueryFileStandardInfo information = new QueryFileStandardInfo();
  44. information.AllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  45. information.EndOfFile = entry.Size;
  46. information.DeletePending = deletePending;
  47. information.Directory = entry.IsDirectory;
  48. result = information;
  49. return NTStatus.STATUS_SUCCESS;
  50. }
  51. case QueryInformationLevel.SMB_QUERY_FILE_EA_INFO:
  52. {
  53. QueryFileExtendedAttributeInfo information = new QueryFileExtendedAttributeInfo();
  54. information.EASize = 0;
  55. result = information;
  56. return NTStatus.STATUS_SUCCESS;
  57. }
  58. case QueryInformationLevel.SMB_QUERY_FILE_NAME_INFO:
  59. {
  60. QueryFileNameInfo information = new QueryFileNameInfo();
  61. information.FileName = entry.Name;
  62. result = information;
  63. return NTStatus.STATUS_SUCCESS;
  64. }
  65. case QueryInformationLevel.SMB_QUERY_FILE_ALL_INFO:
  66. {
  67. QueryFileAllInfo information = new QueryFileAllInfo();
  68. information.CreationDateTime = entry.CreationTime;
  69. information.LastAccessDateTime = entry.LastAccessTime;
  70. information.LastWriteDateTime = entry.LastWriteTime;
  71. information.ExtFileAttributes = GetExtendedFileAttributes(entry);
  72. information.LastChangeTime = entry.LastWriteTime;
  73. information.AllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  74. information.EndOfFile = entry.Size;
  75. information.DeletePending = deletePending;
  76. information.Directory = entry.IsDirectory;
  77. information.EASize = 0;
  78. information.FileName = entry.Name;
  79. result = information;
  80. return NTStatus.STATUS_SUCCESS;
  81. }
  82. case QueryInformationLevel.SMB_QUERY_FILE_ALT_NAME_INFO:
  83. {
  84. QueryFileAltNameInfo information = new QueryFileAltNameInfo();
  85. information.FileName = NTFileSystemHelper.GetShortName(entry.Name);
  86. result = information;
  87. return NTStatus.STATUS_SUCCESS;
  88. }
  89. case QueryInformationLevel.SMB_QUERY_FILE_STREAM_INFO:
  90. {
  91. QueryFileStreamInfo information = new QueryFileStreamInfo();
  92. information.StreamSize = entry.Size;
  93. information.StreamAllocationSize = NTFileSystemHelper.GetAllocationSize(entry.Size);
  94. information.StreamName = "::$DATA";
  95. result = information;
  96. return NTStatus.STATUS_SUCCESS;
  97. }
  98. case QueryInformationLevel.SMB_QUERY_FILE_COMPRESSION_INFO:
  99. {
  100. QueryFileCompressionInfo information = new QueryFileCompressionInfo();
  101. information.CompressionFormat = CompressionFormat.COMPRESSION_FORMAT_NONE;
  102. result = information;
  103. return NTStatus.STATUS_SUCCESS;
  104. }
  105. default:
  106. {
  107. result = null;
  108. return NTStatus.STATUS_OS2_INVALID_LEVEL;
  109. }
  110. }
  111. }
  112. public static SMBFileAttributes GetFileAttributes(FileSystemEntry entry)
  113. {
  114. SMBFileAttributes attributes = SMBFileAttributes.Normal;
  115. if (entry.IsHidden)
  116. {
  117. attributes |= SMBFileAttributes.Hidden;
  118. }
  119. if (entry.IsReadonly)
  120. {
  121. attributes |= SMBFileAttributes.ReadOnly;
  122. }
  123. if (entry.IsArchived)
  124. {
  125. attributes |= SMBFileAttributes.Archive;
  126. }
  127. if (entry.IsDirectory)
  128. {
  129. attributes |= SMBFileAttributes.Directory;
  130. }
  131. return attributes;
  132. }
  133. public static ExtendedFileAttributes GetExtendedFileAttributes(FileSystemEntry entry)
  134. {
  135. ExtendedFileAttributes attributes = 0;
  136. if (entry.IsHidden)
  137. {
  138. attributes |= ExtendedFileAttributes.Hidden;
  139. }
  140. if (entry.IsReadonly)
  141. {
  142. attributes |= ExtendedFileAttributes.Readonly;
  143. }
  144. if (entry.IsArchived)
  145. {
  146. attributes |= ExtendedFileAttributes.Archive;
  147. }
  148. if (entry.IsDirectory)
  149. {
  150. attributes |= ExtendedFileAttributes.Directory;
  151. }
  152. if ((uint)attributes == 0)
  153. {
  154. attributes = ExtendedFileAttributes.Normal;
  155. }
  156. return attributes;
  157. }
  158. }
  159. }