SMB1FileSystemHelper.Query.cs 6.9 KB

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