Trans2QueryFSInformationResponse.cs 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190
  1. // This code is derived from jcifs smb client library <jcifs at samba dot org>
  2. // Ported by J. Arturo <webmaster at komodosoft dot net>
  3. //
  4. // This library is free software; you can redistribute it and/or
  5. // modify it under the terms of the GNU Lesser General Public
  6. // License as published by the Free Software Foundation; either
  7. // version 2.1 of the License, or (at your option) any later version.
  8. //
  9. // This library is distributed in the hope that it will be useful,
  10. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  11. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  12. // Lesser General Public License for more details.
  13. //
  14. // You should have received a copy of the GNU Lesser General Public
  15. // License along with this library; if not, write to the Free Software
  16. // Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
  17. namespace SharpCifs.Smb
  18. {
  19. internal class Trans2QueryFsInformationResponse : SmbComTransactionResponse
  20. {
  21. internal const int SMB_INFO_ALLOCATION = 1;
  22. internal const int SmbQueryFsSizeInfo = unchecked(0x103);
  23. internal const int SmbFsFullSizeInformation = 1007;
  24. internal class SmbInfoAllocation : IAllocInfo
  25. {
  26. internal long Alloc;
  27. internal long Free;
  28. internal int SectPerAlloc;
  29. internal int BytesPerSect;
  30. // information levels
  31. // Also handles SmbQueryFSSizeInfo
  32. public virtual long GetCapacity()
  33. {
  34. return Alloc * SectPerAlloc * BytesPerSect;
  35. }
  36. public virtual long GetFree()
  37. {
  38. return Free * SectPerAlloc * BytesPerSect;
  39. }
  40. public override string ToString()
  41. {
  42. return "SmbInfoAllocation["
  43. + "alloc=" + Alloc
  44. + ",free=" + Free
  45. + ",sectPerAlloc=" + SectPerAlloc
  46. + ",bytesPerSect=" + BytesPerSect + "]";
  47. }
  48. internal SmbInfoAllocation(Trans2QueryFsInformationResponse enclosing)
  49. {
  50. this._enclosing = enclosing;
  51. }
  52. private readonly Trans2QueryFsInformationResponse _enclosing;
  53. }
  54. private int _informationLevel;
  55. internal IAllocInfo Info;
  56. internal Trans2QueryFsInformationResponse(int informationLevel)
  57. {
  58. this._informationLevel = informationLevel;
  59. Command = SmbComTransaction2;
  60. SubCommand = Smb.SmbComTransaction.Trans2QueryFsInformation;
  61. }
  62. internal override int WriteSetupWireFormat(byte[] dst, int dstIndex)
  63. {
  64. return 0;
  65. }
  66. internal override int WriteParametersWireFormat(byte[] dst, int dstIndex)
  67. {
  68. return 0;
  69. }
  70. internal override int WriteDataWireFormat(byte[] dst, int dstIndex)
  71. {
  72. return 0;
  73. }
  74. internal override int ReadSetupWireFormat(byte[] buffer, int bufferIndex, int len)
  75. {
  76. return 0;
  77. }
  78. internal override int ReadParametersWireFormat(byte[] buffer,
  79. int bufferIndex,
  80. int len)
  81. {
  82. return 0;
  83. }
  84. internal override int ReadDataWireFormat(byte[] buffer, int bufferIndex, int len)
  85. {
  86. switch (_informationLevel)
  87. {
  88. case SMB_INFO_ALLOCATION:
  89. {
  90. return ReadSmbInfoAllocationWireFormat(buffer, bufferIndex);
  91. }
  92. case SmbQueryFsSizeInfo:
  93. {
  94. return ReadSmbQueryFsSizeInfoWireFormat(buffer, bufferIndex);
  95. }
  96. case SmbFsFullSizeInformation:
  97. {
  98. return ReadFsFullSizeInformationWireFormat(buffer, bufferIndex);
  99. }
  100. default:
  101. {
  102. return 0;
  103. }
  104. }
  105. }
  106. internal virtual int ReadSmbInfoAllocationWireFormat(byte[] buffer, int bufferIndex)
  107. {
  108. int start = bufferIndex;
  109. SmbInfoAllocation info = new SmbInfoAllocation(this);
  110. bufferIndex += 4;
  111. // skip idFileSystem
  112. info.SectPerAlloc = ReadInt4(buffer, bufferIndex);
  113. bufferIndex += 4;
  114. info.Alloc = ReadInt4(buffer, bufferIndex);
  115. bufferIndex += 4;
  116. info.Free = ReadInt4(buffer, bufferIndex);
  117. bufferIndex += 4;
  118. info.BytesPerSect = ReadInt2(buffer, bufferIndex);
  119. bufferIndex += 4;
  120. this.Info = info;
  121. return bufferIndex - start;
  122. }
  123. internal virtual int ReadSmbQueryFsSizeInfoWireFormat(byte[] buffer, int bufferIndex)
  124. {
  125. int start = bufferIndex;
  126. SmbInfoAllocation info = new SmbInfoAllocation(this);
  127. info.Alloc = ReadInt8(buffer, bufferIndex);
  128. bufferIndex += 8;
  129. info.Free = ReadInt8(buffer, bufferIndex);
  130. bufferIndex += 8;
  131. info.SectPerAlloc = ReadInt4(buffer, bufferIndex);
  132. bufferIndex += 4;
  133. info.BytesPerSect = ReadInt4(buffer, bufferIndex);
  134. bufferIndex += 4;
  135. this.Info = info;
  136. return bufferIndex - start;
  137. }
  138. internal virtual int ReadFsFullSizeInformationWireFormat(byte[] buffer,
  139. int bufferIndex)
  140. {
  141. int start = bufferIndex;
  142. SmbInfoAllocation info = new SmbInfoAllocation(this);
  143. // Read total allocation units.
  144. info.Alloc = ReadInt8(buffer, bufferIndex);
  145. bufferIndex += 8;
  146. // read caller available allocation units
  147. info.Free = ReadInt8(buffer, bufferIndex);
  148. bufferIndex += 8;
  149. // skip actual free units
  150. bufferIndex += 8;
  151. info.SectPerAlloc = ReadInt4(buffer, bufferIndex);
  152. bufferIndex += 4;
  153. info.BytesPerSect = ReadInt4(buffer, bufferIndex);
  154. bufferIndex += 4;
  155. this.Info = info;
  156. return bufferIndex - start;
  157. }
  158. public override string ToString()
  159. {
  160. return "Trans2QueryFSInformationResponse[" + base.ToString() + "]";
  161. }
  162. }
  163. }