try {
List<String> matches = globRemote(dst);
if( matches.size() != 1 ) {
if( matches.isEmpty() ) {
if( isPattern(dst) ) {
throw new SftpException(SSH_FX_FAILURE, "Invalid destination for put: "+dst);
} else {
dst = Util.unquote(dst);
}
}
throw new SftpException(SSH_FX_FAILURE, "Destination is not unique: "+matches);
} else {
dst = matches.get(0);
}
boolean isRemoteDir = isRemoteDir(dst);
matches = globLocal(src);
StringBuffer dstsb = null;
if( isRemoteDir ) {
if( !dst.endsWith("/") ) {
dst += "/";
}
dstsb = new StringBuffer(dst);
} else if( matches.size() > 1 ) {
throw new SftpException(SSH_FX_FAILURE, "Copying multiple files, but the destination is missing or a file.");
}
String _dst;
for( String _src : matches ) {
if( isRemoteDir ) {
int i = _src.lastIndexOf(File.separatorChar);
if( FS_IS_BS ) {
int ii = _src.lastIndexOf('/');
if( ii != -1 && ii > i ) {
i = ii;
}
}
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;
}
long sizeOfDest = 0;
if( mode == RESUME ) {
try {
sizeOfDest = _stat(_dst).getSize();
} catch(Exception eee) {
// TODO Error handling?
}
long sizeOfSrc = new File(_src).length();
if( sizeOfSrc < sizeOfDest ) {
throw new SftpException(SSH_FX_FAILURE, "failed to resume for " + _dst);
} else if( sizeOfSrc == sizeOfDest ) {
return;
}
}
if( monitor != null ) {
monitor.init(SftpProgressMonitor.PUT, _src, _dst, (new File(_src)).length());
if( mode == RESUME ) {
monitor.count(sizeOfDest);
}
}
FileInputStream fis = null;
try {
_put(fis = new FileInputStream(_src), _dst, monitor, mode);
} finally {
if( fis != null ) {
try { fis.close(); } catch(IOException ie) { /* Ignore error. */ }
}
}
}
} catch(SftpException e) {
throw e;
} catch(Exception e) {
throw new SftpException(SSH_FX_FAILURE, "Failed to put: "+src, e);
}
}