SmbComOpenAndX.cs 5.1 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. 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 = Config.GetInt("jcifs.smb.client.OpenAndX.ReadAndX"
  38. , 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, int access, int flags, ServerMessageBlock
  47. andx) : base(andx)
  48. {
  49. // flags (not the same as flags constructor argument)
  50. // Access Mode Encoding for desiredAccess
  51. // bit 12
  52. // bit 14
  53. // flags is NOT the same as flags member
  54. Path = fileName;
  55. Command = SmbComOpenAndx;
  56. DesiredAccess = access & 0x3;
  57. if (DesiredAccess == 0x3)
  58. {
  59. DesiredAccess = 0x2;
  60. }
  61. DesiredAccess |= SharingDenyNone;
  62. DesiredAccess &= ~0x1;
  63. // Win98 doesn't like GENERIC_READ ?! -- get Access Denied.
  64. // searchAttributes
  65. SearchAttributes = SmbConstants.AttrDirectory | SmbConstants.AttrHidden | SmbConstants.AttrSystem;
  66. // fileAttributes
  67. FileAttributes = 0;
  68. // openFunction
  69. if ((flags & SmbFile.OTrunc) == SmbFile.OTrunc)
  70. {
  71. // truncate the file
  72. if ((flags & SmbFile.OCreat) == SmbFile.OCreat)
  73. {
  74. // create it if necessary
  75. OpenFunction = OpenFnTrunc | OpenFnCreate;
  76. }
  77. else
  78. {
  79. OpenFunction = OpenFnTrunc;
  80. }
  81. }
  82. else
  83. {
  84. // don't truncate the file
  85. if ((flags & SmbFile.OCreat) == SmbFile.OCreat)
  86. {
  87. // create it if necessary
  88. if ((flags & SmbFile.OExcl) == SmbFile.OExcl)
  89. {
  90. // fail if already exists
  91. OpenFunction = OpenFnCreate | OpenFnFailIfExists;
  92. }
  93. else
  94. {
  95. OpenFunction = OpenFnCreate | OpenFnOpen;
  96. }
  97. }
  98. else
  99. {
  100. OpenFunction = OpenFnOpen;
  101. }
  102. }
  103. }
  104. internal override int GetBatchLimit(byte command)
  105. {
  106. return command == SmbComReadAndx ? BatchLimit : 0;
  107. }
  108. internal override int WriteParameterWordsWireFormat(byte[] dst, int dstIndex)
  109. {
  110. int start = dstIndex;
  111. WriteInt2(flags, dst, dstIndex);
  112. dstIndex += 2;
  113. WriteInt2(DesiredAccess, dst, dstIndex);
  114. dstIndex += 2;
  115. WriteInt2(SearchAttributes, dst, dstIndex);
  116. dstIndex += 2;
  117. WriteInt2(FileAttributes, dst, dstIndex);
  118. dstIndex += 2;
  119. CreationTime = 0;
  120. WriteInt4(CreationTime, dst, dstIndex);
  121. dstIndex += 4;
  122. WriteInt2(OpenFunction, dst, dstIndex);
  123. dstIndex += 2;
  124. WriteInt4(AllocationSize, dst, dstIndex);
  125. dstIndex += 4;
  126. for (int i = 0; i < 8; i++)
  127. {
  128. dst[dstIndex++] = 0x00;
  129. }
  130. return dstIndex - start;
  131. }
  132. internal override int WriteBytesWireFormat(byte[] dst, int dstIndex)
  133. {
  134. int start = dstIndex;
  135. if (UseUnicode)
  136. {
  137. dst[dstIndex++] = (byte)('\0');
  138. }
  139. dstIndex += WriteString(Path, dst, dstIndex);
  140. return dstIndex - start;
  141. }
  142. internal override int ReadParameterWordsWireFormat(byte[] buffer, int bufferIndex
  143. )
  144. {
  145. return 0;
  146. }
  147. internal override int ReadBytesWireFormat(byte[] buffer, int bufferIndex)
  148. {
  149. return 0;
  150. }
  151. public override string ToString()
  152. {
  153. return "SmbComOpenAndX[" + base.ToString() + ",flags=0x" + Hexdump.ToHexString
  154. (flags, 2) + ",desiredAccess=0x" + Hexdump.ToHexString(DesiredAccess, 4) + ",searchAttributes=0x"
  155. + Hexdump.ToHexString(SearchAttributes, 4) + ",fileAttributes=0x" + Hexdump.ToHexString
  156. (FileAttributes, 4) + ",creationTime=" + Extensions.CreateDate(CreationTime
  157. ) + ",openFunction=0x" + Hexdump.ToHexString(OpenFunction, 2) + ",allocationSize="
  158. + AllocationSize + ",fileName=" + Path + "]";
  159. }
  160. }
  161. }