NTFileSystemAdapter.Query.cs 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213
  1. /* Copyright (C) 2014-2018 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.IO;
  10. using Utilities;
  11. namespace SMBLibrary
  12. {
  13. public partial class NTFileSystemAdapter
  14. {
  15. public NTStatus GetFileInformation(out FileInformation result, object handle, FileInformationClass informationClass)
  16. {
  17. FileHandle fileHandle = (FileHandle)handle;
  18. string path = fileHandle.Path;
  19. FileSystemEntry entry;
  20. try
  21. {
  22. entry = m_fileSystem.GetEntry(path);
  23. }
  24. catch (Exception ex)
  25. {
  26. NTStatus status = ToNTStatus(ex);
  27. Log(Severity.Verbose, "GetFileInformation on '{0}' failed. {1}", path, status);
  28. result = null;
  29. return status;
  30. }
  31. switch (informationClass)
  32. {
  33. case FileInformationClass.FileBasicInformation:
  34. {
  35. FileBasicInformation information = new FileBasicInformation();
  36. information.CreationTime = entry.CreationTime;
  37. information.LastAccessTime = entry.LastAccessTime;
  38. information.LastWriteTime = entry.LastWriteTime;
  39. information.ChangeTime = entry.LastWriteTime;
  40. information.FileAttributes = GetFileAttributes(entry);
  41. result = information;
  42. return NTStatus.STATUS_SUCCESS;
  43. }
  44. case FileInformationClass.FileStandardInformation:
  45. {
  46. FileStandardInformation information = new FileStandardInformation();
  47. information.AllocationSize = (long)GetAllocationSize(entry.Size);
  48. information.EndOfFile = (long)entry.Size;
  49. information.Directory = entry.IsDirectory;
  50. information.DeletePending = fileHandle.DeleteOnClose;
  51. result = information;
  52. return NTStatus.STATUS_SUCCESS;
  53. }
  54. case FileInformationClass.FileInternalInformation:
  55. {
  56. FileInternalInformation information = new FileInternalInformation();
  57. result = information;
  58. return NTStatus.STATUS_SUCCESS;
  59. }
  60. case FileInformationClass.FileEaInformation:
  61. {
  62. FileEaInformation information = new FileEaInformation();
  63. information.EaSize = 0;
  64. result = information;
  65. return NTStatus.STATUS_SUCCESS;
  66. }
  67. case FileInformationClass.FileAccessInformation:
  68. {
  69. result = null;
  70. return NTStatus.STATUS_NOT_IMPLEMENTED;
  71. }
  72. case FileInformationClass.FileNameInformation:
  73. {
  74. FileNameInformation information = new FileNameInformation();
  75. information.FileName = entry.Name;
  76. result = information;
  77. return NTStatus.STATUS_SUCCESS;
  78. }
  79. case FileInformationClass.FilePositionInformation:
  80. {
  81. result = null;
  82. return NTStatus.STATUS_NOT_IMPLEMENTED;
  83. }
  84. case FileInformationClass.FileFullEaInformation:
  85. {
  86. result = null;
  87. return NTStatus.STATUS_NOT_IMPLEMENTED;
  88. }
  89. case FileInformationClass.FileModeInformation:
  90. {
  91. result = null;
  92. return NTStatus.STATUS_NOT_IMPLEMENTED;
  93. }
  94. case FileInformationClass.FileAlignmentInformation:
  95. {
  96. result = null;
  97. return NTStatus.STATUS_NOT_IMPLEMENTED;
  98. }
  99. case FileInformationClass.FileAllInformation:
  100. {
  101. FileAllInformation information = new FileAllInformation();
  102. information.BasicInformation.CreationTime = entry.CreationTime;
  103. information.BasicInformation.LastAccessTime = entry.LastAccessTime;
  104. information.BasicInformation.LastWriteTime = entry.LastWriteTime;
  105. information.BasicInformation.ChangeTime = entry.LastWriteTime;
  106. information.BasicInformation.FileAttributes = GetFileAttributes(entry);
  107. information.StandardInformation.AllocationSize = (long)GetAllocationSize(entry.Size);
  108. information.StandardInformation.EndOfFile = (long)entry.Size;
  109. information.StandardInformation.Directory = entry.IsDirectory;
  110. information.StandardInformation.DeletePending = fileHandle.DeleteOnClose;
  111. information.NameInformation.FileName = entry.Name;
  112. result = information;
  113. return NTStatus.STATUS_SUCCESS;
  114. }
  115. case FileInformationClass.FileAlternateNameInformation:
  116. {
  117. // If there is no alternate name Windows will return STATUS_OBJECT_NAME_NOT_FOUND
  118. result = null;
  119. return NTStatus.STATUS_OBJECT_NAME_NOT_FOUND;
  120. }
  121. case FileInformationClass.FileStreamInformation:
  122. {
  123. // This information class is used to enumerate the data streams of a file or a directory.
  124. // A buffer of FileStreamInformation data elements is returned by the server.
  125. FileStreamInformation information = new FileStreamInformation();
  126. List<KeyValuePair<string, ulong>> dataStreams = m_fileSystem.ListDataStreams(fileHandle.Path);
  127. foreach (KeyValuePair<string, ulong> dataStream in dataStreams)
  128. {
  129. FileStreamEntry streamEntry = new FileStreamEntry();
  130. ulong streamSize = dataStream.Value;
  131. streamEntry.StreamSize = (long)streamSize;
  132. streamEntry.StreamAllocationSize = (long)GetAllocationSize(streamSize);
  133. streamEntry.StreamName = dataStream.Key;
  134. information.Entries.Add(streamEntry);
  135. }
  136. result = information;
  137. return NTStatus.STATUS_SUCCESS;
  138. }
  139. case FileInformationClass.FilePipeInformation:
  140. {
  141. result = null;
  142. return NTStatus.STATUS_NOT_IMPLEMENTED;
  143. }
  144. case FileInformationClass.FilePipeLocalInformation:
  145. {
  146. result = null;
  147. return NTStatus.STATUS_NOT_IMPLEMENTED;
  148. }
  149. case FileInformationClass.FilePipeRemoteInformation:
  150. {
  151. result = null;
  152. return NTStatus.STATUS_NOT_IMPLEMENTED;
  153. }
  154. case FileInformationClass.FileCompressionInformation:
  155. {
  156. result = null;
  157. return NTStatus.STATUS_NOT_IMPLEMENTED;
  158. }
  159. case FileInformationClass.FileNetworkOpenInformation:
  160. {
  161. FileNetworkOpenInformation information = new FileNetworkOpenInformation();
  162. information.CreationTime = entry.CreationTime;
  163. information.LastAccessTime = entry.LastAccessTime;
  164. information.LastWriteTime = entry.LastWriteTime;
  165. information.ChangeTime = entry.LastWriteTime;
  166. information.AllocationSize = (long)GetAllocationSize(entry.Size);
  167. information.EndOfFile = (long)entry.Size;
  168. information.FileAttributes = GetFileAttributes(entry);
  169. result = information;
  170. return NTStatus.STATUS_SUCCESS;
  171. }
  172. case FileInformationClass.FileAttributeTagInformation:
  173. {
  174. result = null;
  175. return NTStatus.STATUS_NOT_IMPLEMENTED;
  176. }
  177. default:
  178. result = null;
  179. return NTStatus.STATUS_INVALID_INFO_CLASS;
  180. }
  181. }
  182. public static FileAttributes GetFileAttributes(FileSystemEntry entry)
  183. {
  184. FileAttributes attributes = 0;
  185. if (entry.IsHidden)
  186. {
  187. attributes |= FileAttributes.Hidden;
  188. }
  189. if (entry.IsReadonly)
  190. {
  191. attributes |= FileAttributes.ReadOnly;
  192. }
  193. if (entry.IsArchived)
  194. {
  195. attributes |= FileAttributes.Archive;
  196. }
  197. if (entry.IsDirectory)
  198. {
  199. attributes |= FileAttributes.Directory;
  200. }
  201. if (attributes == 0)
  202. {
  203. attributes = FileAttributes.Normal;
  204. }
  205. return attributes;
  206. }
  207. }
  208. }