public Stream get(String src, SftpProgressMonitor monitor, int mode) { if(mode==RESUME) { throw new SftpException(SSH_FX_FAILURE, "faile to resume from "+src); } if(!src.StartsWith("/")){ src=cwd+"/"+src; } try { ArrayList v=glob_remote(src); if(v.Count!=1) { throw new SftpException(SSH_FX_FAILURE, v.ToString()); } src=(String)(v[0]); SftpATTRS attr=stat(src); if(monitor!=null) { monitor.init(SftpProgressMonitor.GET, src, "??", attr.getSize()); } sendOPENR(Util.getBytes(src)); buf.rewind(); int i=io.ins.Read(buf.buffer, 0, buf.buffer.Length); int length=buf.getInt(); int type=buf.getByte(); if(type!=SSH_FXP_STATUS && type!=SSH_FXP_HANDLE) { throw new SftpException(SSH_FX_FAILURE, ""); } if(type==SSH_FXP_STATUS) { buf.getInt(); i=buf.getInt(); throwStatusError(buf, i); } buf.getInt(); byte[] handle=buf.getString(); // filename long[] _offset=new long[1]; //_offset[0]=0; int[] _server_version=new int[1]; _server_version[0]=server_version; InputStreamGet ins=new InputStreamGet(this, handle, _offset, _server_version, monitor); return ins; } 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) { if(!src.StartsWith("/")){ src=cwd+"/"+src; } // if(!dst.StartsWith("/")){ dst=lcwd+file_separator+dst; } if(!isLocalAbsolutePath(dst)){ dst=lcwd+file_separator+dst; } try { ArrayList v=glob_remote(src); if(v.Count==0) { throw new SftpException(SSH_FX_NO_SUCH_FILE, "No such file"); } for(int j=0; j<v.Count; j++) { String _dst=dst; String _src=(String)(v[j]); if(Directory.Exists(_dst)) { if(!_dst.EndsWith(file_separator)) { _dst+=file_separator; } int i=_src.LastIndexOf('/'); if(i==-1) _dst+=src; else _dst+=_src.Substring(i+1); } SftpATTRS attr=stat(_src); if(mode==RESUME) { long size_of_src=attr.getSize(); long size_of_dst=new 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.getSize()); if(mode==RESUME) { monitor.count(new FileInfo(_dst).Length); } } FileStream fos=null; if(mode==OVERWRITE) { fos=new FileStream(_dst, FileMode.Create); } else { fos=new FileStream(_dst, FileMode.Append); // append } _get(_src, fos, monitor, mode, new FileInfo(_dst).Length); fos.Close(); } } catch(Exception e) { if(e is SftpException) throw (SftpException)e; throw new SftpException(SSH_FX_FAILURE, ""); } }
public void get(String src, Stream dst, SftpProgressMonitor monitor, int mode, long skip) { //System.out.println("get: "+src+", "+dst); try { if(!src.StartsWith("/")){ src=cwd+"/"+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.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 put(String src, String dst, SftpProgressMonitor monitor, int mode) { // if(!src.StartsWith("/")){ src=lcwd+file_separator+src; } if(!isLocalAbsolutePath(src)){ src=lcwd+file_separator+src; } if(!dst.StartsWith("/")){ dst=cwd+"/"+dst; } //System.out.println("src: "+src+", "+dst); try { ArrayList v=glob_remote(dst); if(v.Count!=1) { throw new SftpException(SSH_FX_FAILURE, v.ToString()); } dst=(String)(v[0]); bool _isRemoteDir=isRemoteDir(dst); v=glob_local(src); //System.out.println("glob_local: "+v+" dst="+dst); for(int j=0; j<v.Count; j++) { String _src=(String)(v[j]); String _dst=dst; if(_isRemoteDir) { if(!_dst.EndsWith("/")) { _dst+="/"; } int i=_src.LastIndexOf(file_separatorc); if(i==-1) _dst+=_src; else _dst+=_src.Substring(i+1); } //System.out.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.out.println(eee); } long size_of_src=new 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 FileInfo(_src)).Length); if(mode==RESUME) { monitor.count(size_of_dst); } } FileStream fis=null; try { fis=File.OpenRead(_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()); } }