/// <exception cref="NSch.SftpException"></exception> public virtual void _put(InputStream src, string dst, SftpProgressMonitor monitor , int mode) { try { byte[] dstb = Util.Str2byte(dst, fEncoding); long skip = 0; if (mode == RESUME || mode == APPEND) { try { SftpATTRS attr = _stat(dstb); skip = attr.GetSize(); } catch (Exception) { } } //System.err.println(eee); if (mode == RESUME && skip > 0) { long skipped = src.Skip(skip); if (skipped < skip) { throw new SftpException(SSH_FX_FAILURE, "failed to resume for " + dst); } } if (mode == OVERWRITE) { SendOPENW(dstb); } else { SendOPENA(dstb); } ChannelHeader header = new ChannelHeader(this); header = Header(buf, header); int length = header.length; int type = header.type; Fill(buf, length); if (type != SSH_FXP_STATUS && type != SSH_FXP_HANDLE) { throw new SftpException(SSH_FX_FAILURE, "invalid type=" + type); } if (type == SSH_FXP_STATUS) { int i = buf.GetInt(); ThrowStatusError(buf, i); } byte[] handle = buf.GetString(); // handle byte[] data = null; bool dontcopy = true; if (!dontcopy) { data = new byte[buf.buffer.Length - (5 + 13 + 21 + handle.Length + 32 + 20)]; } // padding and mac long offset = 0; if (mode == RESUME || mode == APPEND) { offset += skip; } int startid = seq; int _ackid = seq; int ackcount = 0; while (true) { int nread = 0; int s = 0; int datalen = 0; int count = 0; if (!dontcopy) { datalen = data.Length - s; } else { data = buf.buffer; s = 5 + 13 + 21 + handle.Length; datalen = buf.buffer.Length - s - 32 - 20; } do { // padding and mac nread = src.Read(data, s, datalen); if (nread > 0) { s += nread; datalen -= nread; count += nread; } } while (datalen > 0 && nread > 0); if (count <= 0) { break; } int _i = count; while (_i > 0) { _i -= SendWRITE(handle, offset, data, 0, _i); if ((seq - 1) == startid || io_in.Available() >= 1024) { while (io_in.Available() > 0) { if (CheckStatus(ackid, header)) { _ackid = ackid[0]; if (startid > _ackid || _ackid > seq - 1) { if (_ackid == seq) { System.Console.Error.WriteLine("ack error: startid=" + startid + " seq=" + seq + " _ackid=" + _ackid); } else { //throw new SftpException(SSH_FX_FAILURE, "ack error:"); throw new SftpException(SSH_FX_FAILURE, "ack error: startid=" + startid + " seq=" + seq + " _ackid=" + _ackid); } } ackcount++; } else { break; } } } } offset += count; if (monitor != null && !monitor.Count(count)) { break; } } int _ackcount = seq - startid; while (_ackcount > ackcount) { if (!CheckStatus(null, header)) { break; } ackcount++; } if (monitor != null) { monitor.End(); } _sendCLOSE(handle, header); } catch (Exception e) { if (e is SftpException) { throw (SftpException)e; } if (e is Exception) { throw new SftpException(SSH_FX_FAILURE, e.ToString(), (Exception)e); } throw new SftpException(SSH_FX_FAILURE, e.ToString()); } }
/// <exception cref="NSch.SftpException"></exception> public virtual void _put(InputStream src, string dst, SftpProgressMonitor monitor , int mode) { try { ((Channel.MyPipedInputStream)io_in).UpdateReadSide(); byte[] dstb = Util.Str2byte(dst, fEncoding); long skip = 0; if (mode == RESUME || mode == APPEND) { try { SftpATTRS attr = _stat(dstb); skip = attr.GetSize(); } catch (Exception) { } } //System.err.println(eee); if (mode == RESUME && skip > 0) { long skipped = src.Skip(skip); if (skipped < skip) { throw new SftpException(SSH_FX_FAILURE, "failed to resume for " + dst); } } if (mode == OVERWRITE) { SendOPENW(dstb); } else { SendOPENA(dstb); } ChannelHeader header = new ChannelHeader(this); header = Header(buf, header); int length = header.length; int type = header.type; Fill(buf, length); if (type != SSH_FXP_STATUS && type != SSH_FXP_HANDLE) { throw new SftpException(SSH_FX_FAILURE, "invalid type=" + type); } if (type == SSH_FXP_STATUS) { int i = buf.GetInt(); ThrowStatusError(buf, i); } byte[] handle = buf.GetString(); // handle byte[] data = null; bool dontcopy = true; if (!dontcopy) { // This case will not work anymore. data = new byte[obuf.buffer.Length - (5 + 13 + 21 + handle.Length + Session.buffer_margin )]; } long offset = 0; if (mode == RESUME || mode == APPEND) { offset += skip; } int startid = seq; int ackcount = 0; int _s = 0; int _datalen = 0; if (!dontcopy) { // This case will not work anymore. _datalen = data.Length; } else { data = obuf.buffer; _s = 5 + 13 + 21 + handle.Length; _datalen = obuf.buffer.Length - _s - Session.buffer_margin; } int bulk_requests = rq.Size(); while (true) { int nread = 0; int count = 0; int s = _s; int datalen = _datalen; do { nread = src.Read(data, s, datalen); if (nread > 0) { s += nread; datalen -= nread; count += nread; } } while (datalen > 0 && nread > 0); if (count <= 0) { break; } int foo = count; while (foo > 0) { if ((seq - 1) == startid || ((seq - startid) - ackcount) >= bulk_requests) { while (((seq - startid) - ackcount) >= bulk_requests) { if (this.rwsize >= foo) { break; } if (CheckStatus(ackid, header)) { int _ackid = ackid[0]; if (startid > _ackid || _ackid > seq - 1) { if (_ackid == seq) { System.Console.Error.WriteLine("ack error: startid=" + startid + " seq=" + seq + " _ackid=" + _ackid); } else { throw new SftpException(SSH_FX_FAILURE, "ack error: startid=" + startid + " seq=" + seq + " _ackid=" + _ackid); } } ackcount++; } else { break; } } } foo -= SendWRITE(handle, offset, data, 0, foo); } offset += count; if (monitor != null && !monitor.Count(count)) { break; } } int _ackcount = seq - startid; while (_ackcount > ackcount) { if (!CheckStatus(null, header)) { break; } ackcount++; } if (monitor != null) { monitor.End(); } _sendCLOSE(handle, header); } catch (Exception e) { if (e is SftpException) { throw (SftpException)e; } if (e is Exception) { throw new SftpException(SSH_FX_FAILURE, e.ToString(), (Exception)e); } throw new SftpException(SSH_FX_FAILURE, e.ToString()); } }
/// <exception cref="NSch.SftpException"></exception> private void _get(string src, OutputStream dst, SftpProgressMonitor monitor, int mode, long skip) { //System.err.println("_get: "+src+", "+dst); byte[] srcb = Util.Str2byte(src, fEncoding); try { SendOPENR(srcb); ChannelHeader header = new ChannelHeader(this); header = Header(buf, header); int length = header.length; int type = header.type; Fill(buf, length); if (type != SSH_FXP_STATUS && type != SSH_FXP_HANDLE) { throw new SftpException(SSH_FX_FAILURE, string.Empty); } if (type == SSH_FXP_STATUS) { int i = buf.GetInt(); ThrowStatusError(buf, i); } byte[] handle = buf.GetString(); // filename long offset = 0; if (mode == RESUME) { offset += skip; } int request_len = 0; while (true) { request_len = buf.buffer.Length - 13; if (server_version == 0) { request_len = 1024; } SendREAD(handle, offset, request_len); header = Header(buf, header); length = header.length; type = header.type; if (type == SSH_FXP_STATUS) { Fill(buf, length); int i = buf.GetInt(); if (i == SSH_FX_EOF) { goto loop_break; } ThrowStatusError(buf, i); } if (type != SSH_FXP_DATA) { goto loop_break; } buf.Rewind(); Fill(buf.buffer, 0, 4); length -= 4; int i_1 = buf.GetInt(); // length of data int foo = i_1; while (foo > 0) { int bar = foo; if (bar > buf.buffer.Length) { bar = buf.buffer.Length; } i_1 = io_in.Read(buf.buffer, 0, bar); if (i_1 < 0) { goto loop_break; } int data_len = i_1; dst.Write(buf.buffer, 0, data_len); offset += data_len; foo -= data_len; if (monitor != null) { if (!monitor.Count(data_len)) { while (foo > 0) { i_1 = io_in.Read(buf.buffer, 0, (buf.buffer.Length < foo ? buf.buffer.Length : foo )); if (i_1 <= 0) { break; } foo -= i_1; } goto loop_break; } } } loop_continue: ; } loop_break: ; //System.err.println("length: "+length); // length should be 0 dst.Flush(); if (monitor != null) { monitor.End(); } _sendCLOSE(handle, header); } catch (Exception e) { if (e is SftpException) { throw (SftpException)e; } if (e is Exception) { throw new SftpException(SSH_FX_FAILURE, string.Empty, (Exception)e); } throw new SftpException(SSH_FX_FAILURE, string.Empty); } }
/// <exception cref="NSch.SftpException"></exception> private void _get(string src, OutputStream dst, SftpProgressMonitor monitor, int mode, long skip) { //System.err.println("_get: "+src+", "+dst); byte[] srcb = Util.Str2byte(src, fEncoding); try { SendOPENR(srcb); ChannelHeader header = new ChannelHeader(this); header = Header(buf, header); int length = header.length; int type = header.type; Fill(buf, length); if (type != SSH_FXP_STATUS && type != SSH_FXP_HANDLE) { throw new SftpException(SSH_FX_FAILURE, string.Empty); } if (type == SSH_FXP_STATUS) { int i = buf.GetInt(); ThrowStatusError(buf, i); } byte[] handle = buf.GetString(); // filename long offset = 0; if (mode == RESUME) { offset += skip; } int request_max = 1; rq.Init(); long request_offset = offset; int request_len = buf.buffer.Length - 13; if (server_version == 0) { request_len = 1024; } while (true) { while (rq.Count() < request_max) { SendREAD(handle, request_offset, request_len, rq); request_offset += request_len; } header = Header(buf, header); length = header.length; type = header.type; ChannelSftp.RequestQueue.Request rr = rq.Get(header.rid); if (type == SSH_FXP_STATUS) { Fill(buf, length); int i = buf.GetInt(); if (i == SSH_FX_EOF) { goto loop_break; } ThrowStatusError(buf, i); } if (type != SSH_FXP_DATA) { goto loop_break; } buf.Rewind(); Fill(buf.buffer, 0, 4); length -= 4; int length_of_data = buf.GetInt(); // length of data int optional_data = length - length_of_data; int foo = length_of_data; while (foo > 0) { int bar = foo; if (bar > buf.buffer.Length) { bar = buf.buffer.Length; } int data_len = io_in.Read(buf.buffer, 0, bar); if (data_len < 0) { goto loop_break; } dst.Write(buf.buffer, 0, data_len); offset += data_len; foo -= data_len; if (monitor != null) { if (!monitor.Count(data_len)) { Skip(foo); if (optional_data > 0) { Skip(optional_data); } goto loop_break; } } } //System.err.println("length: "+length); // length should be 0 if (optional_data > 0) { Skip(optional_data); } if (length_of_data < rr.length) { // rq.Cancel(header, buf); SendREAD(handle, rr.offset + length_of_data, (int)(rr.length - length_of_data), rq ); request_offset = rr.offset + rr.length; } if (request_max < rq.Size()) { request_max++; } loop_continue: ; } loop_break: ; dst.Flush(); if (monitor != null) { monitor.End(); } rq.Cancel(header, buf); _sendCLOSE(handle, header); } catch (Exception e) { if (e is SftpException) { throw (SftpException)e; } if (e is Exception) { throw new SftpException(SSH_FX_FAILURE, string.Empty, (Exception)e); } throw new SftpException(SSH_FX_FAILURE, string.Empty); } }