SmbComOpenAndX.cs 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197
  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. using SharpCifs.Util;
  18. using SharpCifs.Util.Sharpen;
  19. namespace SharpCifs.Smb
  20. {
  21. internal class SmbComOpenAndX : AndXServerMessageBlock
  22. {
  23. private const int FlagsReturnAdditionalInfo = 0x01;
  24. private const int FlagsRequestOplock = 0x02;
  25. private const int FlagsRequestBatchOplock = 0x04;
  26. private const int SharingCompatibility = 0x00;
  27. private const int SharingDenyReadWriteExecute = 0x10;
  28. private const int SharingDenyWrite = 0x20;
  29. private const int SharingDenyReadExecute = 0x30;
  30. private const int SharingDenyNone = 0x40;
  31. private const int DoNotCache = 0x1000;
  32. private const int WriteThrough = 0x4000;
  33. private const int OpenFnCreate = 0x10;
  34. private const int OpenFnFailIfExists = 0x00;
  35. private const int OpenFnOpen = 0x01;
  36. private const int OpenFnTrunc = 0x02;
  37. private static readonly int BatchLimit
  38. = Config.GetInt("jcifs.smb.client.OpenAndX.ReadAndX", 1);
  39. internal int flags;
  40. internal int DesiredAccess;
  41. internal int SearchAttributes;
  42. internal int FileAttributes;
  43. internal int CreationTime;
  44. internal int OpenFunction;
  45. internal int AllocationSize;
  46. internal SmbComOpenAndX(string fileName,
  47. int access,
  48. int flags,
  49. ServerMessageBlock andx) : base(andx)
  50. {
  51. // flags (not the same as flags constructor argument)
  52. // Access Mode Encoding for desiredAccess
  53. // bit 12
  54. // bit 14
  55. // flags is NOT the same as flags member
  56. Path = fileName;
  57. Command = SmbComOpenAndx;
  58. DesiredAccess = access & 0x3;
  59. if (DesiredAccess == 0x3)
  60. {
  61. DesiredAccess = 0x2;
  62. }
  63. DesiredAccess |= SharingDenyNone;
  64. DesiredAccess &= ~0x1;
  65. // Win98 doesn't like GENERIC_READ ?! -- get Access Denied.
  66. // searchAttributes
  67. SearchAttributes = SmbConstants.AttrDirectory
  68. | SmbConstants.AttrHidden
  69. | SmbConstants.AttrSystem;
  70. // fileAttributes
  71. FileAttributes = 0;
  72. // openFunction
  73. if ((flags & SmbFile.OTrunc) == SmbFile.OTrunc)
  74. {
  75. // truncate the file
  76. if ((flags & SmbFile.OCreat) == SmbFile.OCreat)
  77. {
  78. // create it if necessary
  79. OpenFunction = OpenFnTrunc | OpenFnCreate;
  80. }
  81. else
  82. {
  83. OpenFunction = OpenFnTrunc;
  84. }
  85. }
  86. else
  87. {
  88. // don't truncate the file
  89. if ((flags & SmbFile.OCreat) == SmbFile.OCreat)
  90. {
  91. // create it if necessary
  92. if ((flags & SmbFile.OExcl) == SmbFile.OExcl)
  93. {
  94. // fail if already exists
  95. OpenFunction = OpenFnCreate | OpenFnFailIfExists;
  96. }
  97. else
  98. {
  99. OpenFunction = OpenFnCreate | OpenFnOpen;
  100. }
  101. }
  102. else
  103. {
  104. OpenFunction = OpenFnOpen;
  105. }
  106. }
  107. }
  108. internal override int GetBatchLimit(byte command)
  109. {
  110. return command == SmbComReadAndx ? BatchLimit : 0;
  111. }
  112. internal override int WriteParameterWordsWireFormat(byte[] dst, int dstIndex)
  113. {
  114. int start = dstIndex;
  115. WriteInt2(flags, dst, dstIndex);
  116. dstIndex += 2;
  117. WriteInt2(DesiredAccess, dst, dstIndex);
  118. dstIndex += 2;
  119. WriteInt2(SearchAttributes, dst, dstIndex);
  120. dstIndex += 2;
  121. WriteInt2(FileAttributes, dst, dstIndex);
  122. dstIndex += 2;
  123. CreationTime = 0;
  124. WriteInt4(CreationTime, dst, dstIndex);
  125. dstIndex += 4;
  126. WriteInt2(OpenFunction, dst, dstIndex);
  127. dstIndex += 2;
  128. WriteInt4(AllocationSize, dst, dstIndex);
  129. dstIndex += 4;
  130. for (int i = 0; i < 8; i++)
  131. {
  132. dst[dstIndex++] = 0x00;
  133. }
  134. return dstIndex - start;
  135. }
  136. internal override int WriteBytesWireFormat(byte[] dst, int dstIndex)
  137. {
  138. int start = dstIndex;
  139. if (UseUnicode)
  140. {
  141. dst[dstIndex++] = (byte)('\0');
  142. }
  143. dstIndex += WriteString(Path, dst, dstIndex);
  144. return dstIndex - start;
  145. }
  146. internal override int ReadParameterWordsWireFormat(byte[] buffer, int bufferIndex)
  147. {
  148. return 0;
  149. }
  150. internal override int ReadBytesWireFormat(byte[] buffer, int bufferIndex)
  151. {
  152. return 0;
  153. }
  154. public override string ToString()
  155. {
  156. return "SmbComOpenAndX["
  157. + base.ToString()
  158. + ",flags=0x" + Hexdump.ToHexString(flags, 2)
  159. + ",desiredAccess=0x" + Hexdump.ToHexString(DesiredAccess, 4)
  160. + ",searchAttributes=0x" + Hexdump.ToHexString(SearchAttributes, 4)
  161. + ",fileAttributes=0x" + Hexdump.ToHexString(FileAttributes, 4)
  162. + ",creationTime=" + Extensions.CreateDate(CreationTime)
  163. + ",openFunction=0x" + Hexdump.ToHexString(OpenFunction, 2)
  164. + ",allocationSize=" + AllocationSize
  165. + ",fileName=" + Path + "]";
  166. }
  167. }
  168. }