SmbFileOutputStream.cs 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335
  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 System.IO;
  18. using SharpCifs.Util.Sharpen;
  19. namespace SharpCifs.Smb
  20. {
  21. /// <summary>This <code>OutputStream</code> can write bytes to a file on an SMB file server.
  22. /// </summary>
  23. /// <remarks>This <code>OutputStream</code> can write bytes to a file on an SMB file server.
  24. /// </remarks>
  25. public class SmbFileOutputStream : OutputStream
  26. {
  27. private SmbFile _file;
  28. private bool _append;
  29. private bool _useNtSmbs;
  30. private int _openFlags;
  31. private int _access;
  32. private int _writeSize;
  33. private long _fp;
  34. private byte[] _tmp = new byte[1];
  35. private SmbComWriteAndX _reqx;
  36. private SmbComWriteAndXResponse _rspx;
  37. private SmbComWrite _req;
  38. private SmbComWriteResponse _rsp;
  39. /// <summary>
  40. /// Creates an
  41. /// <see cref="System.IO.OutputStream">System.IO.OutputStream</see>
  42. /// for writing to a file
  43. /// on an SMB server addressed by the URL parameter. See
  44. /// <see cref="SmbFile">SmbFile</see>
  45. /// for a detailed description and examples of
  46. /// the smb URL syntax.
  47. /// </summary>
  48. /// <param name="url">An smb URL string representing the file to write to</param>
  49. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  50. /// <exception cref="System.UriFormatException"></exception>
  51. /// <exception cref="UnknownHostException"></exception>
  52. public SmbFileOutputStream(string url) : this(url, false)
  53. {
  54. }
  55. /// <summary>
  56. /// Creates an
  57. /// <see cref="System.IO.OutputStream">System.IO.OutputStream</see>
  58. /// for writing bytes to a file on
  59. /// an SMB server represented by the
  60. /// <see cref="SmbFile">SmbFile</see>
  61. /// parameter. See
  62. /// <see cref="SmbFile">SmbFile</see>
  63. /// for a detailed description and examples of
  64. /// the smb URL syntax.
  65. /// </summary>
  66. /// <param name="file">An <code>SmbFile</code> specifying the file to write to</param>
  67. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  68. /// <exception cref="System.UriFormatException"></exception>
  69. /// <exception cref="UnknownHostException"></exception>
  70. public SmbFileOutputStream(SmbFile file) : this(file, false)
  71. {
  72. }
  73. /// <summary>
  74. /// Creates an
  75. /// <see cref="System.IO.OutputStream">System.IO.OutputStream</see>
  76. /// for writing bytes to a file on an
  77. /// SMB server addressed by the URL parameter. See
  78. /// <see cref="SmbFile">SmbFile</see>
  79. /// for a detailed description and examples of the smb URL syntax. If the
  80. /// second argument is <code>true</code>, then bytes will be written to the
  81. /// end of the file rather than the beginning.
  82. /// </summary>
  83. /// <param name="url">An smb URL string representing the file to write to</param>
  84. /// <param name="append">Append to the end of file</param>
  85. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  86. /// <exception cref="System.UriFormatException"></exception>
  87. /// <exception cref="UnknownHostException"></exception>
  88. public SmbFileOutputStream(string url, bool append) : this(new SmbFile(url), append
  89. )
  90. {
  91. }
  92. /// <summary>
  93. /// Creates an
  94. /// <see cref="System.IO.OutputStream">System.IO.OutputStream</see>
  95. /// for writing bytes to a file
  96. /// on an SMB server addressed by the <code>SmbFile</code> parameter. See
  97. /// <see cref="SmbFile">SmbFile</see>
  98. /// for a detailed description and examples of
  99. /// the smb URL syntax. If the second argument is <code>true</code>, then
  100. /// bytes will be written to the end of the file rather than the beginning.
  101. /// </summary>
  102. /// <param name="file">An <code>SmbFile</code> representing the file to write to</param>
  103. /// <param name="append">Append to the end of file</param>
  104. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  105. /// <exception cref="System.UriFormatException"></exception>
  106. /// <exception cref="UnknownHostException"></exception>
  107. public SmbFileOutputStream(SmbFile file, bool append) : this(file, append, append
  108. ? SmbFile.OCreat | SmbFile.OWronly | SmbFile.OAppend : SmbFile.OCreat | SmbFile
  109. .OWronly | SmbFile.OTrunc)
  110. {
  111. }
  112. /// <summary>
  113. /// Creates an
  114. /// <see cref="System.IO.OutputStream">System.IO.OutputStream</see>
  115. /// for writing bytes to a file
  116. /// on an SMB server addressed by the <code>SmbFile</code> parameter. See
  117. /// <see cref="SmbFile">SmbFile</see>
  118. /// for a detailed description and examples of
  119. /// the smb URL syntax.
  120. /// <p>
  121. /// The second parameter specifies how the file should be shared. If
  122. /// <code>SmbFile.FILE_NO_SHARE</code> is specified the client will
  123. /// have exclusive access to the file. An additional open command
  124. /// from jCIFS or another application will fail with the "file is being
  125. /// accessed by another process" error. The <code>FILE_SHARE_READ</code>,
  126. /// <code>FILE_SHARE_WRITE</code>, and <code>FILE_SHARE_DELETE</code> may be
  127. /// combined with the bitwise OR '|' to specify that other peocesses may read,
  128. /// write, and/or delete the file while the jCIFS user has the file open.
  129. /// </summary>
  130. /// <param name="url">An smb URL representing the file to write to</param>
  131. /// <param name="shareAccess">File sharing flag: <code>SmbFile.FILE_NOSHARE</code> or any combination of <code>SmbFile.FILE_READ</code>, <code>SmbFile.FILE_WRITE</code>, and <code>SmbFile.FILE_DELETE</code>
  132. /// </param>
  133. /// <exception cref="Jcifs.Smb.SmbException"></exception>
  134. /// <exception cref="System.UriFormatException"></exception>
  135. /// <exception cref="UnknownHostException"></exception>
  136. public SmbFileOutputStream(string url, int shareAccess) : this(new SmbFile(url, string.Empty
  137. , null, shareAccess), false)
  138. {
  139. }
  140. /// <exception cref="SharpCifs.Smb.SmbException"></exception>
  141. /// <exception cref="System.UriFormatException"></exception>
  142. /// <exception cref="UnknownHostException"></exception>
  143. internal SmbFileOutputStream(SmbFile file, bool append, int openFlags)
  144. {
  145. this._file = file;
  146. this._append = append;
  147. this._openFlags = openFlags;
  148. _access = ((int)(((uint)openFlags) >> 16)) & 0xFFFF;
  149. if (append)
  150. {
  151. try
  152. {
  153. _fp = file.Length();
  154. }
  155. catch (SmbAuthException sae)
  156. {
  157. throw;
  158. }
  159. catch (SmbException)
  160. {
  161. _fp = 0L;
  162. }
  163. }
  164. if (file is SmbNamedPipe && file.Unc.StartsWith("\\pipe\\"))
  165. {
  166. file.Unc = Runtime.Substring(file.Unc, 5);
  167. file.Send(new TransWaitNamedPipe("\\pipe" + file.Unc), new TransWaitNamedPipeResponse
  168. ());
  169. }
  170. file.Open(openFlags, _access | SmbConstants.FileWriteData, SmbFile.AttrNormal,
  171. 0);
  172. this._openFlags &= ~(SmbFile.OCreat | SmbFile.OTrunc);
  173. _writeSize = file.Tree.Session.transport.SndBufSize - 70;
  174. _useNtSmbs = file.Tree.Session.transport.HasCapability(SmbConstants.CapNtSmbs
  175. );
  176. if (_useNtSmbs)
  177. {
  178. _reqx = new SmbComWriteAndX();
  179. _rspx = new SmbComWriteAndXResponse();
  180. }
  181. else
  182. {
  183. _req = new SmbComWrite();
  184. _rsp = new SmbComWriteResponse();
  185. }
  186. }
  187. /// <summary>
  188. /// Closes this output stream and releases any system resources associated
  189. /// with it.
  190. /// </summary>
  191. /// <remarks>
  192. /// Closes this output stream and releases any system resources associated
  193. /// with it.
  194. /// </remarks>
  195. /// <exception cref="System.IO.IOException">if a network error occurs</exception>
  196. public override void Close()
  197. {
  198. _file.Close();
  199. _tmp = null;
  200. }
  201. /// <summary>Writes the specified byte to this file output stream.</summary>
  202. /// <remarks>Writes the specified byte to this file output stream.</remarks>
  203. /// <exception cref="System.IO.IOException">if a network error occurs</exception>
  204. public override void Write(int b)
  205. {
  206. _tmp[0] = unchecked((byte)b);
  207. Write(_tmp, 0, 1);
  208. }
  209. /// <summary>
  210. /// Writes b.length bytes from the specified byte array to this
  211. /// file output stream.
  212. /// </summary>
  213. /// <remarks>
  214. /// Writes b.length bytes from the specified byte array to this
  215. /// file output stream.
  216. /// </remarks>
  217. /// <exception cref="System.IO.IOException">if a network error occurs</exception>
  218. public override void Write(byte[] b)
  219. {
  220. Write(b, 0, b.Length);
  221. }
  222. public virtual bool IsOpen()
  223. {
  224. return _file.IsOpen();
  225. }
  226. /// <exception cref="System.IO.IOException"></exception>
  227. internal virtual void EnsureOpen()
  228. {
  229. // ensure file is open
  230. if (_file.IsOpen() == false)
  231. {
  232. _file.Open(_openFlags, _access | SmbConstants.FileWriteData, SmbFile.AttrNormal,
  233. 0);
  234. if (_append)
  235. {
  236. _fp = _file.Length();
  237. }
  238. }
  239. }
  240. /// <summary>
  241. /// Writes len bytes from the specified byte array starting at
  242. /// offset off to this file output stream.
  243. /// </summary>
  244. /// <remarks>
  245. /// Writes len bytes from the specified byte array starting at
  246. /// offset off to this file output stream.
  247. /// </remarks>
  248. /// <param name="b">The array</param>
  249. /// <exception cref="System.IO.IOException">if a network error occurs</exception>
  250. public override void Write(byte[] b, int off, int len)
  251. {
  252. if (_file.IsOpen() == false && _file is SmbNamedPipe)
  253. {
  254. _file.Send(new TransWaitNamedPipe("\\pipe" + _file.Unc), new TransWaitNamedPipeResponse
  255. ());
  256. }
  257. WriteDirect(b, off, len, 0);
  258. }
  259. /// <summary>Just bypasses TransWaitNamedPipe - used by DCERPC bind.</summary>
  260. /// <remarks>Just bypasses TransWaitNamedPipe - used by DCERPC bind.</remarks>
  261. /// <exception cref="System.IO.IOException"></exception>
  262. public virtual void WriteDirect(byte[] b, int off, int len, int flags)
  263. {
  264. if (len <= 0)
  265. {
  266. return;
  267. }
  268. if (_tmp == null)
  269. {
  270. throw new IOException("Bad file descriptor");
  271. }
  272. EnsureOpen();
  273. /*if (file.log.level >= 4)
  274. {
  275. file.log.WriteLine("write: fid=" + file.fid + ",off=" + off + ",len=" + len);
  276. }*/
  277. int w;
  278. do
  279. {
  280. w = len > _writeSize ? _writeSize : len;
  281. if (_useNtSmbs)
  282. {
  283. _reqx.SetParam(_file.Fid, _fp, len - w, b, off, w);
  284. if ((flags & 1) != 0)
  285. {
  286. _reqx.SetParam(_file.Fid, _fp, len, b, off, w);
  287. _reqx.WriteMode = 0x8;
  288. }
  289. else
  290. {
  291. _reqx.WriteMode = 0;
  292. }
  293. _file.Send(_reqx, _rspx);
  294. _fp += _rspx.Count;
  295. len -= (int)_rspx.Count;
  296. off += (int)_rspx.Count;
  297. }
  298. else
  299. {
  300. _req.SetParam(_file.Fid, _fp, len - w, b, off, w);
  301. _fp += _rsp.Count;
  302. len -= (int)_rsp.Count;
  303. off += (int)_rsp.Count;
  304. _file.Send(_req, _rsp);
  305. }
  306. }
  307. while (len > 0);
  308. }
  309. }
  310. }