| Method from com.jcraft.jsch.ChannelSftp Detail: |
public void _put(InputStream src,
String dst,
SftpProgressMonitor monitor,
int mode) throws SftpException {
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 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(dstb); }
else{ sendOPENA(dstb); }
Header header=new Header();
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;
boolean 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_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.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);
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, e.toString(), (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, e.toString());
}
}
|
public void cd(String path) throws SftpException {
try{
path=remoteAbsolutePath(path);
path=isUnique(path);
byte[] str=_realpath(path);
SftpATTRS attr=_stat(str);
if((attr.getFlags()&SftpATTRS.SSH_FILEXFER_ATTR_PERMISSIONS)==0){
throw new SftpException(SSH_FX_FAILURE,
"Can't change directory: "+path);
}
if(!attr.isDir()){
throw new SftpException(SSH_FX_FAILURE,
"Can't change directory: "+path);
}
setCwd(Util.byte2str(str, fEncoding));
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public void chgrp(int gid,
String path) throws SftpException {
try{
path=remoteAbsolutePath(path);
Vector v=glob_remote(path);
int vsize=v.size();
for(int j=0; j< vsize; j++){
path=(String)(v.elementAt(j));
SftpATTRS attr=_stat(path);
attr.setFLAGS(0);
attr.setUIDGID(attr.uid, gid);
_setStat(path, attr);
}
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public void chmod(int permissions,
String path) throws SftpException {
try{
path=remoteAbsolutePath(path);
Vector v=glob_remote(path);
int vsize=v.size();
for(int j=0; j< vsize; j++){
path=(String)(v.elementAt(j));
SftpATTRS attr=_stat(path);
attr.setFLAGS(0);
attr.setPERMISSIONS(permissions);
_setStat(path, attr);
}
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public void chown(int uid,
String path) throws SftpException {
try{
path=remoteAbsolutePath(path);
Vector v=glob_remote(path);
int vsize=v.size();
for(int j=0; j< vsize; j++){
path=(String)(v.elementAt(j));
SftpATTRS attr=_stat(path);
attr.setFLAGS(0);
attr.setUIDGID(uid, attr.gid);
_setStat(path, attr);
}
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public void disconnect() {
super.disconnect();
}
|
public void exit() {
disconnect();
}
|
public InputStream get(String src) throws SftpException {
return get(src, null, 0L);
}
|
public void get(String src,
String dst) throws SftpException {
get(src, dst, null, OVERWRITE);
}
|
public void get(String src,
OutputStream dst) throws SftpException {
get(src, dst, null, OVERWRITE, 0);
}
|
public InputStream get(String src,
SftpProgressMonitor monitor) throws SftpException {
return get(src, monitor, 0L);
}
|
public InputStream get(String src,
int mode) throws SftpException {
return get(src, null, 0L);
} Deprecated! This - method will be deleted in the future.
|
public void get(String src,
String dst,
SftpProgressMonitor monitor) throws SftpException {
get(src, dst, monitor, OVERWRITE);
}
|
public void get(String src,
OutputStream dst,
SftpProgressMonitor monitor) throws SftpException {
get(src, dst, monitor, OVERWRITE, 0);
}
|
public InputStream get(String src,
SftpProgressMonitor monitor,
int mode) throws SftpException {
return get(src, monitor, 0L);
} Deprecated! This - method will be deleted in the future.
|
public InputStream get(String src,
SftpProgressMonitor monitor,
long skip) throws SftpException {
src=remoteAbsolutePath(src);
try{
src=isUnique(src);
byte[] srcb=Util.str2byte(src, fEncoding);
SftpATTRS attr=_stat(srcb);
if(monitor!=null){
monitor.init(SftpProgressMonitor.GET, src, "??", attr.getSize());
}
sendOPENR(srcb);
Header header=new Header();
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, "");
}
if(type==SSH_FXP_STATUS){
int i=buf.getInt();
throwStatusError(buf, i);
}
final byte[] handle=buf.getString(); // handle
java.io.InputStream in=new java.io.InputStream(){
long offset=skip;
boolean closed=false;
int rest_length=0;
byte[] _data=new byte[1];
byte[] rest_byte=new byte[1024];
Header header=new Header();
public int read() throws java.io.IOException{
if(closed)return -1;
int i=read(_data, 0, 1);
if (i==-1) { return -1; }
else {
return _data[0]&0xff;
}
}
public int read(byte[] d) throws java.io.IOException{
if(closed)return -1;
return read(d, 0, d.length);
}
public int read(byte[] d, int s, int len) throws java.io.IOException{
if(closed)return -1;
if(d==null){throw new NullPointerException();}
if(s< 0 || len < 0 || s+len >d.length){
throw new IndexOutOfBoundsException();
}
if(len==0){ return 0; }
if(rest_length >0){
int foo=rest_length;
if(foo >len) foo=len;
System.arraycopy(rest_byte, 0, d, s, foo);
if(foo!=rest_length){
System.arraycopy(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(buf.buffer.length-13< len){
len=buf.buffer.length-13;
}
if(server_version==0 && len >1024){
len=1024;
}
try{sendREAD(handle, offset, len);}
catch(Exception e){ throw new IOException("error"); }
header=header(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 IOException("error");
}
if(type==SSH_FXP_STATUS){
fill(buf, rest_length);
int i=buf.getInt();
rest_length=0;
if(i==SSH_FX_EOF){
close();
return -1;
}
//throwStatusError(buf, i);
throw new IOException("error");
}
buf.rewind();
fill(buf.buffer, 0, 4);
int i=buf.getInt(); rest_length-=4;
offset+=rest_length;
int foo=i;
if(foo >0){
int bar=rest_length;
if(bar >len){
bar=len;
}
i=io_in.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=io_in.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 close() throws IOException{
if(closed)return;
closed=true;
if(monitor!=null)monitor.end();
try{_sendCLOSE(handle, header);}
catch(Exception e){throw new IOException("error");}
}
};
return in;
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public void get(String src,
String dst,
SftpProgressMonitor monitor,
int mode) throws SftpException {
// System.out.println("get: "+src+" "+dst);
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);
boolean 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 instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public void get(String src,
OutputStream dst,
SftpProgressMonitor monitor,
int mode,
long skip) throws SftpException {
//System.err.println("get: "+src+", "+dst);
try{
src=remoteAbsolutePath(src);
src=isUnique(src);
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 instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public String getExtension(String key) {
if(extensions==null)
return null;
return (String)extensions.get(key);
}
|
public String getHome() throws SftpException {
if(home==null){
try{
byte[] _home=_realpath("");
home=Util.byte2str(_home, fEncoding);
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
return home;
}
|
public int getServerVersion() throws SftpException {
if(!isConnected()){
throw new SftpException(SSH_FX_FAILURE, "The channel is not connected.");
}
return server_version;
}
|
void init() {
}
|
public void lcd(String path) throws SftpException {
path=localAbsolutePath(path);
if((new File(path)).isDirectory()){
try{
path=(new File(path)).getCanonicalPath();
}
catch(Exception e){}
lcwd=path;
return;
}
throw new SftpException(SSH_FX_NO_SUCH_FILE, "No such directory");
}
|
public String lpwd() {
return lcwd;
}
|
public Vector ls(String path) throws SftpException {
//System.out.println("ls: "+path);
try{
path=remoteAbsolutePath(path);
byte[] pattern=null;
java.util.Vector v=new java.util.Vector();
int foo=path.lastIndexOf('/");
String dir=path.substring(0, ((foo==0)?1:foo));
String _pattern=path.substring(foo+1);
dir=Util.unquote(dir);
// If pattern has included '*' or '?', we need to convert
// to UTF-8 string before globbing.
byte[][] _pattern_utf8=new byte[1][];
boolean pattern_has_wildcard=isPattern(_pattern, _pattern_utf8);
if(pattern_has_wildcard){
pattern=_pattern_utf8[0];
}
else{
String upath=Util.unquote(path);
//SftpATTRS attr=_lstat(upath);
SftpATTRS attr=_stat(upath);
if(attr.isDir()){
pattern=null;
dir=upath;
}
else{
/*
// If we can generage longname by ourself,
// we don't have to use openDIR.
String filename=Util.unquote(_pattern);
String longname=...
v.addElement(new LsEntry(filename, longname, attr));
return v;
*/
if(fEncoding_is_utf8){
pattern=_pattern_utf8[0];
pattern=Util.unquote(pattern);
}
else{
_pattern=Util.unquote(_pattern);
pattern=Util.str2byte(_pattern, fEncoding);
}
}
}
sendOPENDIR(Util.str2byte(dir, fEncoding));
Header header=new Header();
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, "");
}
if(type==SSH_FXP_STATUS){
int i=buf.getInt();
throwStatusError(buf, i);
}
byte[] handle=buf.getString(); // handle
while(true){
sendREADDIR(handle);
header=header(buf, header);
length=header.length;
type=header.type;
if(type!=SSH_FXP_STATUS && type!=SSH_FXP_NAME){
throw new SftpException(SSH_FX_FAILURE, "");
}
if(type==SSH_FXP_STATUS){
fill(buf, length);
int i=buf.getInt();
if(i==SSH_FX_EOF)
break;
throwStatusError(buf, i);
}
buf.rewind();
fill(buf.buffer, 0, 4); length-=4;
int count=buf.getInt();
byte[] str;
int flags;
buf.reset();
while(count >0){
if(length >0){
buf.shift();
int j=(buf.buffer.length >(buf.index+length)) ?
length :
(buf.buffer.length-buf.index);
int i=fill(buf.buffer, buf.index, j);
buf.index+=i;
length-=i;
}
byte[] filename=buf.getString();
byte[] longname=null;
if(server_version< =3){
longname=buf.getString();
}
SftpATTRS attrs=SftpATTRS.getATTR(buf);
boolean find=false;
String f=null;
if(pattern==null){
find=true;
}
else if(!pattern_has_wildcard){
find=Util.array_equals(pattern, filename);
}
else{
byte[] _filename=filename;
if(!fEncoding_is_utf8){
f=Util.byte2str(_filename, fEncoding);
_filename=Util.str2byte(f, UTF8);
}
find=Util.glob(pattern, _filename);
}
if(find){
if(f==null){
f=Util.byte2str(filename, fEncoding);
}
String l=null;
if(longname==null){
// TODO: we need to generate long name from attrs
// for the sftp protocol 4(and later).
l=attrs.toString()+" "+f;
}
else{
l=Util.byte2str(longname, fEncoding);
}
v.addElement(new LsEntry(f, l, attrs));
}
count--;
}
}
_sendCLOSE(handle, header);
if(v.size()==0){
throw new SftpException(SSH_FX_NO_SUCH_FILE, "No such file: "+path);
}
/*
if(v.size()==1 && pattern_has_wildcard){
LsEntry le=(LsEntry)v.elementAt(0);
if(le.getAttrs().isDir()){
String f=le.getFilename();
if(isPattern(f)){
f=Util.quote(f);
}
if(!dir.endsWith("/")){
dir+="/";
}
v=null;
return ls(dir+f);
}
}
*/
return v;
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public SftpATTRS lstat(String path) throws SftpException {
try{
path=remoteAbsolutePath(path);
path=isUnique(path);
return _lstat(path);
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public void mkdir(String path) throws SftpException {
try{
path=remoteAbsolutePath(path);
sendMKDIR(Util.str2byte(path, fEncoding), null);
Header header=new Header();
header=header(buf, header);
int length=header.length;
int type=header.type;
fill(buf, length);
if(type!=SSH_FXP_STATUS){
throw new SftpException(SSH_FX_FAILURE, "");
}
int i=buf.getInt();
if(i==SSH_FX_OK) return;
throwStatusError(buf, i);
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public OutputStream put(String dst) throws SftpException {
return put(dst, (SftpProgressMonitor)null, OVERWRITE);
}
|
public void put(String src,
String dst) throws SftpException {
put(src, dst, null, OVERWRITE);
}
|
public void put(InputStream src,
String dst) throws SftpException {
put(src, dst, null, OVERWRITE);
}
|
public OutputStream put(String dst,
int mode) throws SftpException {
return put(dst, (SftpProgressMonitor)null, mode);
}
|
public void put(String src,
String dst,
int mode) throws SftpException {
put(src, dst, null, mode);
}
|
public void put(String src,
String dst,
SftpProgressMonitor monitor) throws SftpException {
put(src, dst, monitor, OVERWRITE);
}
|
public void put(InputStream src,
String dst,
int mode) throws SftpException {
put(src, dst, null, mode);
}
|
public void put(InputStream src,
String dst,
SftpProgressMonitor monitor) throws SftpException {
put(src, dst, monitor, OVERWRITE);
}
|
public OutputStream put(String dst,
SftpProgressMonitor monitor,
int mode) throws SftpException {
return put(dst, monitor, mode, 0);
}
|
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));
}
boolean isRemoteDir=isRemoteDir(dst);
v=glob_local(src);
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 the 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(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;
}
//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) {
fis.close();
}
}
}
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, e.toString(), (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, e.toString());
}
}
|
public void put(InputStream src,
String dst,
SftpProgressMonitor monitor,
int mode) throws SftpException {
try{
dst=remoteAbsolutePath(dst);
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));
}
if(isRemoteDir(dst)){
throw new SftpException(SSH_FX_FAILURE, dst+" is a directory");
}
_put(src, dst, monitor, mode);
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, e.toString(), (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, e.toString());
}
}
|
public OutputStream put(String dst,
SftpProgressMonitor monitor,
int mode,
long offset) throws SftpException {
dst=remoteAbsolutePath(dst);
try{
dst=isUnique(dst);
if(isRemoteDir(dst)){
throw new SftpException(SSH_FX_FAILURE, dst+" is a directory");
}
byte[] dstb=Util.str2byte(dst, fEncoding);
long skip=0;
if(mode==RESUME || mode==APPEND){
try{
SftpATTRS attr=_stat(dstb);
skip=attr.getSize();
}
catch(Exception eee){
//System.err.println(eee);
}
}
if(mode==OVERWRITE){ sendOPENW(dstb); }
else{ sendOPENA(dstb); }
Header header=new Header();
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, "");
}
if(type==SSH_FXP_STATUS){
int i=buf.getInt();
throwStatusError(buf, i);
}
final byte[] handle=buf.getString(); // handle
if(mode==RESUME || mode==APPEND){
offset+=skip;
}
final long[] _offset=new long[1];
_offset[0]=offset;
OutputStream out = new OutputStream(){
private boolean init=true;
private boolean isClosed=false;
private int[] ackid=new int[1];
private int startid=0;
private int _ackid=0;
private int ackcount=0;
private int writecount=0;
private Header header=new Header();
public void write(byte[] d) throws java.io.IOException{
write(d, 0, d.length);
}
public void write(byte[] d, int s, int len) throws java.io.IOException{
if(init){
startid=seq;
_ackid=seq;
init=false;
}
if(isClosed){
throw new IOException("stream already closed");
}
try{
int _len=len;
while(_len >0){
int sent=sendWRITE(handle, _offset[0], d, s, _len);
writecount++;
_offset[0]+=sent;
s+=sent;
_len-=sent;
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){
throw new SftpException(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()); }
}
byte[] _data=new byte[1];
public void write(int foo) throws java.io.IOException{
_data[0]=(byte)foo;
write(_data, 0, 1);
}
public void flush() throws java.io.IOException{
if(isClosed){
throw new IOException("stream already closed");
}
if(!init){
try{
while(writecount >ackcount){
if(!checkStatus(null, header)){
break;
}
ackcount++;
}
}
catch(SftpException e){
throw new IOException(e.toString());
}
}
}
public void close() throws java.io.IOException{
if(isClosed){
return;
}
flush();
if(monitor!=null)monitor.end();
try{ _sendCLOSE(handle, header); }
catch(IOException e){ throw e; }
catch(Exception e){
throw new IOException(e.toString());
}
isClosed=true;
}
};
return out;
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public String pwd() throws SftpException {
return getCwd();
}
|
public void quit() {
disconnect();
}
|
public String readlink(String path) throws SftpException {
try{
if(server_version< 3){
throw new SftpException(SSH_FX_OP_UNSUPPORTED,
"The remote sshd is too old to support symlink operation.");
}
path=remoteAbsolutePath(path);
path=isUnique(path);
sendREADLINK(Util.str2byte(path, fEncoding));
Header header=new Header();
header=header(buf, header);
int length=header.length;
int type=header.type;
fill(buf, length);
if(type!=SSH_FXP_STATUS && type!=SSH_FXP_NAME){
throw new SftpException(SSH_FX_FAILURE, "");
}
if(type==SSH_FXP_NAME){
int count=buf.getInt(); // count
byte[] filename=null;
for(int i=0; i< count; i++){
filename=buf.getString();
if(server_version< =3){
byte[] longname=buf.getString();
}
SftpATTRS.getATTR(buf);
}
return Util.byte2str(filename, fEncoding);
}
int i=buf.getInt();
throwStatusError(buf, i);
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
return null;
}
|
public String realpath(String path) throws SftpException {
try{
byte[] _path=_realpath(remoteAbsolutePath(path));
return Util.byte2str(_path, fEncoding);
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public void rename(String oldpath,
String newpath) throws SftpException {
if(server_version< 2){
throw new SftpException(SSH_FX_OP_UNSUPPORTED,
"The remote sshd is too old to support rename operation.");
}
try{
oldpath=remoteAbsolutePath(oldpath);
newpath=remoteAbsolutePath(newpath);
oldpath=isUnique(oldpath);
Vector v=glob_remote(newpath);
int vsize=v.size();
if(vsize >=2){
throw new SftpException(SSH_FX_FAILURE, v.toString());
}
if(vsize==1){
newpath=(String)(v.elementAt(0));
}
else{ // vsize==0
if(isPattern(newpath))
throw new SftpException(SSH_FX_FAILURE, newpath);
newpath=Util.unquote(newpath);
}
sendRENAME(Util.str2byte(oldpath, fEncoding),
Util.str2byte(newpath, fEncoding));
Header header=new Header();
header=header(buf, header);
int length=header.length;
int type=header.type;
fill(buf, length);
if(type!=SSH_FXP_STATUS){
throw new SftpException(SSH_FX_FAILURE, "");
}
int i=buf.getInt();
if(i==SSH_FX_OK) return;
throwStatusError(buf, i);
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public void rm(String path) throws SftpException {
try{
path=remoteAbsolutePath(path);
Vector v=glob_remote(path);
int vsize=v.size();
Header header=new Header();
for(int j=0; j< vsize; j++){
path=(String)(v.elementAt(j));
sendREMOVE(Util.str2byte(path, fEncoding));
header=header(buf, header);
int length=header.length;
int type=header.type;
fill(buf, length);
if(type!=SSH_FXP_STATUS){
throw new SftpException(SSH_FX_FAILURE, "");
}
int i=buf.getInt();
if(i!=SSH_FX_OK){
throwStatusError(buf, i);
}
}
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public void rmdir(String path) throws SftpException {
try{
path=remoteAbsolutePath(path);
Vector v=glob_remote(path);
int vsize=v.size();
Header header=new Header();
for(int j=0; j< vsize; j++){
path=(String)(v.elementAt(j));
sendRMDIR(Util.str2byte(path, fEncoding));
header=header(buf, header);
int length=header.length;
int type=header.type;
fill(buf, length);
if(type!=SSH_FXP_STATUS){
throw new SftpException(SSH_FX_FAILURE, "");
}
int i=buf.getInt();
if(i!=SSH_FX_OK){
throwStatusError(buf, i);
}
}
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public void setFilenameEncoding(String encoding) throws SftpException {
int sversion=getServerVersion();
if(sversion > 3 &&
!encoding.equals(UTF8)){
throw new SftpException(SSH_FX_FAILURE,
"The encoding can not be changed for this sftp server.");
}
if(encoding.equals(UTF8)){
encoding=UTF8;
}
fEncoding=encoding;
fEncoding_is_utf8=fEncoding.equals(UTF8);
}
|
public void setMtime(String path,
int mtime) throws SftpException {
try{
path=remoteAbsolutePath(path);
Vector v=glob_remote(path);
int vsize=v.size();
for(int j=0; j< vsize; j++){
path=(String)(v.elementAt(j));
SftpATTRS attr=_stat(path);
attr.setFLAGS(0);
attr.setACMODTIME(attr.getATime(), mtime);
_setStat(path, attr);
}
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public void setStat(String path,
SftpATTRS attr) throws SftpException {
try{
path=remoteAbsolutePath(path);
Vector v=glob_remote(path);
int vsize=v.size();
for(int j=0; j< vsize; j++){
path=(String)(v.elementAt(j));
_setStat(path, attr);
}
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public void start() throws JSchException {
try{
PipedOutputStream pos=new PipedOutputStream();
io.setOutputStream(pos);
PipedInputStream pis=new MyPipedInputStream(pos, 32*1024);
io.setInputStream(pis);
io_in=io.in;
if(io_in==null){
throw new JSchException("channel is down");
}
Request request=new RequestSftp();
request.request(getSession(), this);
/*
System.err.println("lmpsize: "+lmpsize);
System.err.println("lwsize: "+lwsize);
System.err.println("rmpsize: "+rmpsize);
System.err.println("rwsize: "+rwsize);
*/
buf=new Buffer(rmpsize);
packet=new Packet(buf);
int i=0;
int length;
int type;
byte[] str;
// send SSH_FXP_INIT
sendINIT();
// receive SSH_FXP_VERSION
Header header=new Header();
header=header(buf, header);
length=header.length;
if(length > MAX_MSG_LENGTH){
throw new SftpException(SSH_FX_FAILURE,
"Received message is too long: " + length);
}
type=header.type; // 2 - > SSH_FXP_VERSION
server_version=header.rid;
//System.err.println("SFTP protocol server-version="+server_version);
if(length >0){
extensions=new java.util.Hashtable();
// extension data
fill(buf, length);
byte[] extension_name=null;
byte[] extension_data=null;
while(length >0){
extension_name=buf.getString();
length-=(4+extension_name.length);
extension_data=buf.getString();
length-=(4+extension_data.length);
extensions.put(new String(extension_name),
new String(extension_data));
}
}
lcwd=new File(".").getCanonicalPath();
}
catch(Exception e){
//System.err.println(e);
if(e instanceof JSchException) throw (JSchException)e;
if(e instanceof Throwable)
throw new JSchException(e.toString(), (Throwable)e);
throw new JSchException(e.toString());
}
}
|
public SftpATTRS stat(String path) throws SftpException {
try{
path=remoteAbsolutePath(path);
path=isUnique(path);
return _stat(path);
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
//return null;
}
|
public void symlink(String oldpath,
String newpath) throws SftpException {
if(server_version< 3){
throw new SftpException(SSH_FX_OP_UNSUPPORTED,
"The remote sshd is too old to support symlink operation.");
}
try{
oldpath=remoteAbsolutePath(oldpath);
newpath=remoteAbsolutePath(newpath);
oldpath=isUnique(oldpath);
if(isPattern(newpath)){
throw new SftpException(SSH_FX_FAILURE, newpath);
}
newpath=Util.unquote(newpath);
sendSYMLINK(Util.str2byte(oldpath, fEncoding),
Util.str2byte(newpath, fEncoding));
Header header=new Header();
header=header(buf, header);
int length=header.length;
int type=header.type;
fill(buf, length);
if(type!=SSH_FXP_STATUS){
throw new SftpException(SSH_FX_FAILURE, "");
}
int i=buf.getInt();
if(i==SSH_FX_OK) return;
throwStatusError(buf, i);
}
catch(Exception e){
if(e instanceof SftpException) throw (SftpException)e;
if(e instanceof Throwable)
throw new SftpException(SSH_FX_FAILURE, "", (Throwable)e);
throw new SftpException(SSH_FX_FAILURE, "");
}
}
|
public String version() {
return version;
}
|