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, ""); } }
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 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()); } }
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, ""); } }