public override void Write(byte[] d, int s, int len) { if (init) { startid = sftp.seq; _ackid = sftp.seq; init = false; } try { int _len = len; while (_len > 0) { int sent = sftp.sendWRITE(handle, _offset[0], d, s, _len); _offset[0] += sent; s += sent; _len -= sent; if ((sftp.seq - 1) == startid || sftp.io.ins.available() >= 1024) { while (sftp.io.ins.available() > 0) { if (sftp.checkStatus(ackid, header)) { _ackid = ackid[0]; if (startid > _ackid || _ackid > sftp.seq - 1) { throw new SftpException(ChannelSftp.SSH_FX_FAILURE, ""); } ackcount++; } else { break; } } } } if (monitor != null && !monitor.count(len)) { Close(); throw new IOException("canceled"); } } catch (IOException e) { throw e; } catch (Exception e) { throw new IOException(e.ToString()); } }
public void get(String src, OutputStream dst, SftpProgressMonitor monitor, int mode, long skip) { //throws SftpException{ //try //{ src=remoteAbsolutePath(src); Vector v=glob_remote(src); if(v.size()!=1) { throw new SftpException(SSH_FX_FAILURE, v.toString()); } src=(String)(v.elementAt(0)); if(monitor!=null) { SftpATTRS attr=_stat(src); monitor.init(SftpProgressMonitor.GET, src, "??", attr.getSize()); if(mode==RESUME) { monitor.count(skip); } } _get(src, dst, monitor, mode, skip); //} //catch(Exception e) //{ // if(e is SftpException) throw (SftpException)e; // throw new SftpException(SSH_FX_FAILURE, ""); //} }
public void get(String src, String dst, SftpProgressMonitor monitor, int mode) { //throws SftpException{ src=remoteAbsolutePath(src); dst=localAbsolutePath(dst); try { Vector v=glob_remote(src); int vsize=v.size(); if(vsize==0) { throw new SftpException(SSH_FX_NO_SUCH_FILE, "No such file"); } File dstFile=new File(dst); bool isDstDir=dstFile.isDirectory(); StringBuffer dstsb=null; if(isDstDir) { if(!dst.endsWith(file_separator)) { dst+=file_separator; } dstsb=new StringBuffer(dst); } else if(vsize>1) { throw new SftpException(SSH_FX_FAILURE, "Copying multiple files, but destination is missing or a file."); } for(int j=0; j<vsize; j++) { String _src=(String)(v.elementAt(j)); SftpATTRS attr=_stat(_src); if(attr.isDir()) { throw new SftpException(SSH_FX_FAILURE, "not supported to get directory "+_src); } String _dst=null; if(isDstDir) { int i=_src.lastIndexOf('/'); if(i==-1) dstsb.append(_src); else dstsb.append(_src.substring(i + 1)); _dst=dstsb.toString(); dstsb.delete(dst.length(), _dst.length()); } else { _dst=dst; } if(mode==RESUME) { long size_of_src=attr.getSize(); long size_of_dst=new File(_dst).length(); if(size_of_dst>size_of_src) { throw new SftpException(SSH_FX_FAILURE, "failed to resume for "+_dst); } if(size_of_dst==size_of_src) { return; } } if(monitor!=null) { monitor.init(SftpProgressMonitor.GET, _src, _dst, attr.getSize()); if(mode==RESUME) { monitor.count(new File(_dst).length()); } } FileOutputStream fos=null; if(mode==OVERWRITE) { fos=new FileOutputStream(_dst); } else { fos=new FileOutputStream(_dst, true); // append } //System.err.println("_get: "+_src+", "+_dst); _get(_src, fos, monitor, mode, new File(_dst).length()); fos.close(); } } catch(Exception e) { if(e is SftpException) throw (SftpException)e; throw new SftpException(SSH_FX_FAILURE, ""); } }
private void _put(InputStream src, String dst, SftpProgressMonitor monitor, int mode) { try { long skip=0; if(mode==RESUME || mode==APPEND) { try { SftpATTRS attr=_stat(dst); skip=attr.getSize(); } catch(Exception eee) { //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(dst.getBytes()); } else{ sendOPENA(dst.getBytes()); } Header _header=new Header(); _header=header(buf, _header); int length=_header.length; int type=_header.type; buf.rewind(); fill(buf.buffer, 0, 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(); // filename 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; // padding and mac } 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 _i=count; while(_i>0) { _i-=sendWRITE(handle, offset, data, 0, _i); if((seq-1)==startid || io.ins.available()>=1024) { while(io.ins.available()>0) { if(checkStatus(ackid, _header)) { _ackid=ackid[0]; if(startid>_ackid || _ackid>seq-1) { if(_ackid==seq) { java.System.err.println("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); //System.err.println("start end "+startid+" "+endid); } catch(Exception e) { if(e is SftpException) throw (SftpException)e; throw new SftpException(SSH_FX_FAILURE, e.toString()); } }
public void put(String src, String dst, SftpProgressMonitor monitor, int mode) { //throws SftpException{ src=localAbsolutePath(src); dst=remoteAbsolutePath(dst); //System.err.println("src: "+src+", "+dst); try { Vector v=glob_remote(dst); int vsize=v.size(); if(vsize!=1) { if(vsize==0) { if(isPattern(dst)) throw new SftpException(SSH_FX_FAILURE, dst); else dst=Util.unquote(dst); } throw new SftpException(SSH_FX_FAILURE, v.toString()); } else { dst=(String)(v.elementAt(0)); } //System.err.println("dst: "+dst); bool _isRemoteDir=isRemoteDir(dst); v=glob_local(src); //System.err.println("glob_local: "+v+" dst="+dst); vsize=v.size(); StringBuffer dstsb=null; if(_isRemoteDir) { if(!dst.endsWith("/")) { dst+="/"; } dstsb=new StringBuffer(dst); } else if(vsize>1) { throw new SftpException(SSH_FX_FAILURE, "Copying multiple files, but destination is missing or a file."); } for(int j=0; j<vsize; j++) { String _src=(String)(v.elementAt(j)); String _dst=null; if(_isRemoteDir) { int i=_src.lastIndexOf(file_separatorc); if(i==-1) dstsb.append(_src); else dstsb.append(_src.substring(i + 1)); _dst=dstsb.toString(); dstsb.delete(dst.length(), _dst.length()); } else { _dst=dst; } //System.err.println("_dst "+_dst); long size_of_dst=0; if(mode==RESUME) { try { SftpATTRS attr=_stat(_dst); size_of_dst=attr.getSize(); } catch(Exception eee) { //System.err.println(eee); } long size_of_src=new File(_src).length(); if(size_of_src<size_of_dst) { throw new SftpException(SSH_FX_FAILURE, "failed to resume for "+_dst); } if(size_of_src==size_of_dst) { return; } } if(monitor!=null) { monitor.init(SftpProgressMonitor.PUT, _src, _dst, (new File(_src)).length()); if(mode==RESUME) { monitor.count(size_of_dst); } } FileInputStream fis=null; try { fis=new FileInputStream(_src); _put(fis, _dst, monitor, mode); } finally { if(fis!=null) { // try{ fis.close(); // }catch(Exception ee){}; } } } } catch(Exception e) { if(e is SftpException) throw (SftpException)e; throw new SftpException(SSH_FX_FAILURE, e.toString()); } }
///tamir: updated to jcsh-0.1.30 private void _get(String src, OutputStream dst, SftpProgressMonitor monitor, int mode, long skip) { //throws SftpException{ //System.out.println("_get: "+src+", "+dst); //try //{ sendOPENR(src.getBytes()); Header _header=new Header(); _header=header(buf, _header); int length=_header.length; int type=_header.type; buf.rewind(); fill(buf.buffer, 0, length); if(type!=SSH_FXP_STATUS && type!=SSH_FXP_HANDLE) { //System.Console.WriteLine("Type is "+type); throw new SftpException(SSH_FX_FAILURE, "Type is "+type); } 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; loop: 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; int i; if(type==SSH_FXP_STATUS) { buf.rewind(); fill(buf.buffer, 0, length); i=buf.getInt(); if(i==SSH_FX_EOF) { goto BREAK; } throwStatusError(buf, i); } if(type!=SSH_FXP_DATA) { goto BREAK; } buf.rewind(); fill(buf.buffer, 0, 4); length-=4; i=buf.getInt(); // length of data int foo=i; while(foo>0) { int bar=foo; if(bar>buf.buffer.Length) { bar=buf.buffer.Length; } i=io.ins.read(buf.buffer, 0, bar); if(i<0) { goto BREAK; } int data_len=i; 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=io.ins.read(buf.buffer, 0, (buf.buffer.Length<foo?buf.buffer.Length:foo)); if(i<=0) break; foo-=i; } goto BREAK; } } } //System.out.println("length: "+length); // length should be 0 } BREAK: dst.flush(); if(monitor!=null)monitor.end(); _sendCLOSE(handle, _header); //} //catch(Exception e) //{ // //System.Console.WriteLine(e); // if(e is SftpException) throw (SftpException)e; // throw new SftpException(SSH_FX_FAILURE, ""); //} }
public void get(String src, OutputStream dst, SftpProgressMonitor monitor, int mode, long skip) { //throws SftpException{ try { src = RemoteAbsolutePath(src); ArrayList v = glob_remote(src); if (v.Count != 1) { throw new SftpException(SSH_FX_FAILURE, v.ToString()); } src = (String)(v[0]); if (monitor != null) { SftpATTRS attr = _stat(src); monitor.init(SftpProgressMonitor.GET, src, "??", attr.Size); if (mode == RESUME) { monitor.count(skip); } } _get(src, dst, monitor, mode, skip); } catch (Exception e) { if (e is SftpException) throw (SftpException)e; throw new SftpException(SSH_FX_FAILURE, ""); } }
public void get(String src, String dst, SftpProgressMonitor monitor, int mode) { //throws SftpException{ src = RemoteAbsolutePath(src); dst = localAbsolutePath(dst); try { ArrayList v = glob_remote(src); int vsize = v.Count; if (vsize == 0) { throw new SftpException(SSH_FX_NO_SUCH_FILE, "No such file"); } bool isDstDir = System.IO.Directory.Exists(dst); string dstsb = ""; if (isDstDir) { if (!dst.EndsWith(file_separator)) { dst += file_separator; } dstsb = (dst); } else if (vsize > 1) { throw new SftpException(SSH_FX_FAILURE, "Copying multiple files, but destination is missing or a file."); } for (int j = 0; j < vsize; j++) { String _src = (String)(v[j]); SftpATTRS attr = _stat(_src); if (attr.IsDirectory) { throw new SftpException(SSH_FX_FAILURE, "not supported to get directory " + _src); } String _dst = null; if (isDstDir) { int i = _src.LastIndexOf('/'); if (i == -1) dstsb += (_src); else dstsb += (_src.Substring(i + 1)); _dst = dstsb; dstsb = dstsb.Remove(dst.Length, _dst.Length); } else { _dst = dst; } if (mode == RESUME) { long size_of_src = attr.Size; long size_of_dst = new System.IO.FileInfo(_dst).Length; if (size_of_dst > size_of_src) { throw new SftpException(SSH_FX_FAILURE, "failed to resume for " + _dst); } if (size_of_dst == size_of_src) { return; } } if (monitor != null) { monitor.init(SftpProgressMonitor.GET, _src, _dst, attr.Size); if (mode == RESUME) { monitor.count(new System.IO.FileInfo(_dst).Length); } } FileOutputStream fos = null; if (mode == OVERWRITE) { fos = new FileOutputStream(_dst); } else { fos = new FileOutputStream(_dst, true); // append } //System.err.println("_get: "+_src+", "+_dst); _get(_src, fos, monitor, mode, new System.IO.FileInfo(_dst).Length); fos.close(); } } catch (Exception e) { if (e is SftpException) throw (SftpException)e; throw new SftpException(SSH_FX_FAILURE, ""); } }
public void put(String src, String dst, SftpProgressMonitor monitor, int mode) { //throws SftpException{ src = localAbsolutePath(src); dst = RemoteAbsolutePath(dst); //System.err.println("src: "+src+", "+dst); try { ArrayList v = glob_remote(dst); int vsize = v.Count; if (vsize != 1) { if (vsize == 0) { if (IsPattern(dst)) throw new SftpException(SSH_FX_FAILURE, dst); else dst = Util.unquote(dst); } throw new SftpException(SSH_FX_FAILURE, v.ToString()); } else { dst = (String)(v[0]); } //System.err.println("dst: "+dst); bool _isRemoteDir = isRemoteDir(dst); v = glob_local(src); //System.err.println("glob_local: "+v+" dst="+dst); vsize = v.Count; string dstsb = ""; if (_isRemoteDir) { if (!dst.EndsWith("/")) { dst += "/"; } dstsb = (dst); } else if (vsize > 1) { throw new SftpException(SSH_FX_FAILURE, "Copying multiple files, but destination is missing or a file."); } for (int j = 0; j < vsize; j++) { String _src = (String)(v[j]); String _dst = null; if (_isRemoteDir) { int i = _src.LastIndexOf(file_separatorc); if (i == -1) dstsb += (_src); else dstsb += (_src.Substring(i + 1)); _dst = dstsb; dstsb = dstsb.Remove(dst.Length, _dst.Length); } else { _dst = dst; } //System.err.println("_dst "+_dst); long size_of_dst = 0; if (mode == RESUME) { try { SftpATTRS attr = _stat(_dst); size_of_dst = attr.Size; } catch (Exception) { //System.err.println(eee); } long size_of_src = new System.IO.FileInfo(_src).Length; if (size_of_src < size_of_dst) { throw new SftpException(SSH_FX_FAILURE, "failed to resume for " + _dst); } if (size_of_src == size_of_dst) { return; } } if (monitor != null) { monitor.init( SftpProgressMonitor.PUT, _src, _dst, new System.IO.FileInfo(_src).Length ); if (mode == RESUME) { monitor.count(size_of_dst); } } FileInputStream fis = null; try { fis = new FileInputStream(_src); _put(fis, _dst, monitor, mode); } finally { if (fis != null) { // try{ fis.close(); // }catch(Exception ee){}; } } } } catch (Exception e) { if (e is SftpException) throw (SftpException)e; throw new SftpException(SSH_FX_FAILURE, e.ToString()); } }
public override int Read(byte[] d, int s, int len) { if (closed) { return(-1); } int i; int foo; if (d == null) { throw new System.NullReferenceException(); } if (s < 0 || len < 0 || s + len > d.Length) { throw new System.IndexOutOfRangeException(); } if (len == 0) { return(0); } if (rest_length > 0) { foo = rest_length; if (foo > len) { foo = len; } System.Array.Copy(rest_byte, 0, d, s, foo); if (foo != rest_length) { System.Array.Copy(rest_byte, foo, rest_byte, 0, rest_length - foo); } if (monitor != null) { if (!monitor.count(foo)) { close(); return(-1); } } rest_length -= foo; return(foo); } if (sftp.buf.buffer.Length - 13 < len) { len = sftp.buf.buffer.Length - 13; } if (sftp.ServerVersion == 0 && len > 1024) { len = 1024; } try { sftp.sendREAD(handle, offset, len); } catch (Exception) { throw new System.IO.IOException("error"); } header = sftp.ReadHeader(sftp.buf, header); rest_length = header.length; int type = header.type; int id = header.rid; if (type != SSH_FXP_STATUS && type != SSH_FXP_DATA) { throw new System.IO.IOException("error"); } if (type == SSH_FXP_STATUS) { sftp.buf.Rewind(); sftp.fill(sftp.buf.buffer, 0, rest_length); i = sftp.buf.ReadInt(); rest_length = 0; if (i == SSH_FX_EOF) { close(); return(-1); } //throwStatusError(buf, i); throw new System.IO.IOException("error"); } sftp.buf.Rewind(); sftp.fill(sftp.buf.buffer, 0, 4); i = sftp.buf.ReadInt(); rest_length -= 4; offset += rest_length; foo = i; if (foo > 0) { int bar = rest_length; if (bar > len) { bar = len; } i = sftp.io.ins.read(d, s, bar); if (i < 0) { return(-1); } rest_length -= i; if (rest_length > 0) { if (rest_byte.Length < rest_length) { rest_byte = new byte[rest_length]; } int _s = 0; int _len = rest_length; int j; while (_len > 0) { j = sftp.io.ins.read(rest_byte, _s, _len); if (j <= 0) { break; } _s += j; _len -= j; } } if (monitor != null) { if (!monitor.count(i)) { close(); return(-1); } } return(i); } return(0); // ?? }
public void put(String src, String dst, SftpProgressMonitor monitor, int mode) { src = localAbsolutePath(src); dst = remoteAbsolutePath(dst); try { ArrayList v = glob_remote(dst); int vsize = v.Count; if (vsize != 1) { if (vsize == 0) { if (isPattern(dst)) throw new SftpException(SSH_FX_FAILURE, dst); else dst = Util.unquote(dst); } throw new SftpException(SSH_FX_FAILURE, v.ToString()); } else { dst = (String)(v[0]); } bool _isRemoteDir = isRemoteDir(dst); v = glob_local(src); vsize = v.Count; StringBuilder dstsb = null; if (_isRemoteDir) { if (!dst.EndsWith("/")) { dst += "/"; } dstsb = new StringBuilder(dst); } else if (vsize > 1) { throw new SftpException(SSH_FX_FAILURE, "Copying multiple files, but destination is missing or a file."); } for (int j = 0; j < vsize; j++) { String _src = (String)(v[j]); String _dst = null; if (_isRemoteDir) { int i = _src.LastIndexOf(file_separatorc); if (i == -1) dstsb.Append(_src); else dstsb.Append(_src.Substring(i + 1)); _dst = dstsb.ToString(); dstsb.Remove(dst.Length, _dst.Length); } else { _dst = dst; } long size_of_dst = 0; if (mode == RESUME) { try { SftpATTRS attr = GetPathAttributes(_dst); size_of_dst = attr.getSize(); } catch (Exception) { } long size_of_src = new File(_src).Length(); if (size_of_src < size_of_dst) { throw new SftpException(SSH_FX_FAILURE, "failed to resume for " + _dst); } if (size_of_src == size_of_dst) { return; } } if (monitor != null) { monitor.init(SftpProgressMonitor.PUT, _src, _dst, (new File(_src)).Length()); if (mode == RESUME) { monitor.count(size_of_dst); } } FileInputStream fis = null; try { fis = new FileInputStream(_src); _put(fis, _dst, monitor, mode); } finally { if (fis != null) { fis.close(); } } } } catch (Exception e) { if (e is SftpException) throw (SftpException)e; throw new SftpException(SSH_FX_FAILURE, e.Message); } }
public void get(String remoteAbsolutePath, OutputStream dst, SftpProgressMonitor monitor, int mode, long skip) { try { remoteAbsolutePath = this.remoteAbsolutePath(remoteAbsolutePath); ArrayList v = glob_remote(remoteAbsolutePath); if (v.Count != 1) { throw new SftpException(SSH_FX_FAILURE, v.ToString()); } remoteAbsolutePath = (String)(v[0]); if (monitor != null) { SftpATTRS attr = GetPathAttributes(remoteAbsolutePath); monitor.init(SftpProgressMonitor.GET, remoteAbsolutePath, "??", attr.getSize()); if (mode == RESUME) { monitor.count(skip); } } _get(remoteAbsolutePath, dst, monitor, mode, skip); } catch (Exception e) { if (e is SftpException) throw (SftpException)e; throw new SftpException(SSH_FX_FAILURE, ""); } }
public void get(String remoteAbsolutePath, String localAbsolutePath, SftpProgressMonitor monitor, int mode) { remoteAbsolutePath = this.remoteAbsolutePath(remoteAbsolutePath); localAbsolutePath = this.localAbsolutePath(localAbsolutePath); try { ArrayList files = glob_remote(remoteAbsolutePath); if (files.Count == 0) { throw new SftpException(SSH_FX_NO_SUCH_FILE, "No such file"); } bool copyingToDirectory = new File(localAbsolutePath).isDirectory(); // if we're not copying to a directory but we're copying multiple files, there's a problem if (false == copyingToDirectory && files.Count > 1) { throw new SftpException(SSH_FX_FAILURE, "Copying multiple files, but destination is missing or is a file."); } // if the given local path doesn't end with a '\' or other file separator, add one if (!localAbsolutePath.EndsWith(file_separator)) { localAbsolutePath += file_separator; } for (int j = 0; j < files.Count; j++) { String sourceFile = (String)(files[j]); SftpATTRS attr = GetPathAttributes(sourceFile); // get information on the current file if (attr.isDir()) { // right now it's not able to get a directory throw new SftpException(SSH_FX_FAILURE, "not supported to get directory " + sourceFile); } String destinationPath = null; if (copyingToDirectory) { StringBuilder destinationSb = new StringBuilder(localAbsolutePath); // find the last file separator character int i = sourceFile.LastIndexOf(file_separatorc); // basically we're appending just the filename if (i == -1) destinationSb.Append(sourceFile); else destinationSb.Append(sourceFile.Substring(i + 1)); destinationPath = destinationSb.ToString(); } else { destinationPath = localAbsolutePath; } if (mode == RESUME) { long sizeOfSourceFile = attr.getSize(); long sizeOfDestinationFile = new File(destinationPath).Length(); // this means we already copied more data than is available. fail if (sizeOfDestinationFile > sizeOfSourceFile) { throw new SftpException(SSH_FX_FAILURE, "failed to resume for " + destinationPath); } // if the sizes are equal, we're gravy if (sizeOfDestinationFile == sizeOfSourceFile) { return; } } if (monitor != null) { monitor.init(SftpProgressMonitor.GET, sourceFile, destinationPath, attr.getSize()); if (mode == RESUME) { monitor.count(new File(destinationPath).Length()); } } // create the output stream, and append if it's in overwrite mode FileOutputStream fileOutputStream = new FileOutputStream(destinationPath, mode == OVERWRITE); _get(sourceFile, fileOutputStream, monitor, mode, new File(destinationPath).Length()); fileOutputStream.Close(); } } catch (SftpException) { throw; } catch (Exception) { throw new SftpException(SSH_FX_FAILURE, ""); } }