📄 channelsftp.cs
字号:
if(e is SftpException) throw (SftpException)e;
throw new SftpException(SSH_FX_FAILURE, "");
}
}
public SftpATTRS stat(String path)
{
try
{
if(!path.StartsWith("/")){ path=cwd+"/"+path; }
sendSTAT(Util.getBytes(path));
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_ATTRS)
{
if(type==SSH_FXP_STATUS)
{
buf.getInt();
i=buf.getInt();
throwStatusError(buf, i);
}
throw new SftpException(SSH_FX_FAILURE, "");
}
buf.getInt();
SftpATTRS attr=SftpATTRS.getATTR(buf);
return attr;
}
catch(Exception e)
{
if(e is SftpException) throw (SftpException)e;
throw new SftpException(SSH_FX_FAILURE, "");
}
//return null;
}
public SftpATTRS lstat(String path)
{
try
{
if(!path.StartsWith("/")){ path=cwd+"/"+path; }
sendLSTAT(Util.getBytes(path));
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_ATTRS)
{
if(type==SSH_FXP_STATUS)
{
buf.getInt();
i=buf.getInt();
throwStatusError(buf, i);
}
throw new SftpException(SSH_FX_FAILURE, "");
}
buf.getInt();
SftpATTRS attr=SftpATTRS.getATTR(buf);
return attr;
}
catch(Exception e)
{
if(e is SftpException) throw (SftpException)e;
throw new SftpException(SSH_FX_FAILURE, "");
}
}
public void setStat(String path, SftpATTRS attr)
{
try
{
if(!path.StartsWith("/")){ path=cwd+"/"+path; }
ArrayList v=glob_remote(path);
for(int j=0; j<v.Count; j++)
{
path=(String)(v[j]);
_setStat(path, attr);
}
}
catch(Exception e)
{
if(e is SftpException) throw (SftpException)e;
throw new SftpException(SSH_FX_FAILURE, "");
}
}
private void _setStat(String path, SftpATTRS attr)
{
try
{
sendSETSTAT(Util.getBytes(path), attr);
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)
{
throw new SftpException(SSH_FX_FAILURE, "");
}
buf.getInt();
i=buf.getInt();
if(i!=SSH_FX_OK)
{
throwStatusError(buf, i);
}
}
catch(Exception e)
{
if(e is SftpException) throw (SftpException)e;
throw new SftpException(SSH_FX_FAILURE, "");
}
}
public String pwd(){ return cwd; }
public String lpwd(){ return lcwd; }
public String version(){ return _version; }
private bool checkStatus()
{
buf.rewind();
io.ins.Read(buf.buffer, 0, buf.buffer.Length);
int length=buf.getInt();
int type=buf.getByte();
if(type!=SSH_FXP_STATUS)
{
throw new SftpException(SSH_FX_FAILURE, "");
}
buf.getInt();
int i=buf.getInt();
if(i!=SSH_FX_OK)
{
throwStatusError(buf, i);
//System.out.println("getInt="+buf.getInt());
}
return true;
}
private void _sendCLOSE(byte[] handle)
{
sendCLOSE(handle);
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)
{
throw new SftpException(SSH_FX_FAILURE, "");
}
buf.getInt();
i=buf.getInt();
if(i==SSH_FX_OK) return;
throwStatusError(buf, i);
}
private void sendINIT()
{
packet.reset();
putHEAD(SSH_FXP_INIT, 5);
buf.putInt(3); // version 3
session.write(packet, this, 5+4);
}
private void sendREALPATH(byte[] path)
{
sendPacketPath(SSH_FXP_REALPATH, path);
}
private void sendSTAT(byte[] path)
{
sendPacketPath(SSH_FXP_STAT, path);
}
private void sendLSTAT(byte[] path)
{
sendPacketPath(SSH_FXP_LSTAT, path);
}
private void sendFSTAT(byte[] handle)
{
sendPacketPath(SSH_FXP_FSTAT, handle);
}
private void sendSETSTAT(byte[] path, SftpATTRS attr)
{
packet.reset();
putHEAD(SSH_FXP_SETSTAT, 9+path.Length+attr.length());
buf.putInt(count++);
buf.putString(path); // path
attr.dump(buf);
session.write(packet, this, 9+path.Length+attr.length()+4);
}
private void sendREMOVE(byte[] path)
{
sendPacketPath(SSH_FXP_REMOVE, path);
}
private void sendMKDIR(byte[] path, SftpATTRS attr)
{
packet.reset();
putHEAD(SSH_FXP_MKDIR, 9+path.Length+(attr!=null?attr.length():4));
buf.putInt(count++);
buf.putString(path); // path
if(attr!=null) attr.dump(buf);
else buf.putInt(0);
session.write(packet, this, 9+path.Length+(attr!=null?attr.length():4)+4);
}
private void sendRMDIR(byte[] path)
{
sendPacketPath(SSH_FXP_RMDIR, path);
}
private void sendSYMLINK(byte[] p1, byte[] p2)
{
sendPacketPath(SSH_FXP_SYMLINK, p1, p2);
}
private void sendREADLINK(byte[] path)
{
sendPacketPath(SSH_FXP_READLINK, path);
}
private void sendOPENDIR(byte[] path)
{
sendPacketPath(SSH_FXP_OPENDIR, path);
}
private void sendREADDIR(byte[] path)
{
sendPacketPath(SSH_FXP_READDIR, path);
}
private void sendRENAME(byte[] p1, byte[] p2)
{
sendPacketPath(SSH_FXP_RENAME, p1, p2);
}
private void sendCLOSE(byte[] path)
{
sendPacketPath(SSH_FXP_CLOSE, path);
}
private void sendOPENR(byte[] path)
{
sendOPEN(path, SSH_FXF_READ);
}
private void sendOPENW(byte[] path)
{
sendOPEN(path, SSH_FXF_WRITE|SSH_FXF_CREAT|SSH_FXF_TRUNC);
}
private void sendOPENA(byte[] path)
{
sendOPEN(path, SSH_FXF_WRITE|/*SSH_FXF_APPEND|*/SSH_FXF_CREAT);
}
private void sendOPEN(byte[] path, int mode)
{
packet.reset();
putHEAD(SSH_FXP_OPEN, 17+path.Length);
buf.putInt(count++);
buf.putString(path);
buf.putInt(mode);
buf.putInt(0); // attrs
session.write(packet, this, 17+path.Length+4);
}
private void sendPacketPath(byte fxp, byte[] path)
{
packet.reset();
putHEAD(fxp, 9+path.Length);
buf.putInt(count++);
buf.putString(path); // path
session.write(packet, this, 9+path.Length+4);
}
private void sendPacketPath(byte fxp, byte[] p1, byte[] p2)
{
packet.reset();
putHEAD(fxp, 13+p1.Length+p2.Length);
buf.putInt(count++);
buf.putString(p1);
buf.putString(p2);
session.write(packet, this, 13+p1.Length+p2.Length+4);
}
private void sendWRITE(byte[] handle, long offset,
byte[] data, int start, int length)
{
packet.reset();
putHEAD(SSH_FXP_WRITE, 21+handle.Length+length);
buf.putInt(count++);
buf.putString(handle);
buf.putLong(offset);
buf.putString(data, start, length);
session.write(packet, this, 21+handle.Length+length+4);
}
private void sendREAD(byte[] handle, long offset, int length)
{
packet.reset();
putHEAD(SSH_FXP_READ, 21+handle.Length);
buf.putInt(count++);
buf.putString(handle);
buf.putLong(offset);
buf.putInt(length);
session.write(packet, this, 21+handle.Length+4);
}
private void putHEAD(byte type, int length)
{
buf.putByte((byte)Session.SSH_MSG_CHANNEL_DATA);
buf.putInt(recipient);
buf.putInt(length+4);
buf.putInt(length);
buf.putByte(type);
}
private ArrayList glob_remote(String _path)
{
//System.out.println("glob_remote: "+_path);
ArrayList v=new ArrayList();
byte[] path=Util.getBytes(_path);
int i=path.Length-1;
while(i>=0){if(path[i]=='*' || path[i]=='?')break;i--;}
if(i<0){ v.Add(_path); return v;}
while(i>=0){if(path[i]=='/')break;i--;}
if(i<0){ v.Add(_path); return v;}
byte[] dir;
if(i==0){dir=new byte[]{(byte)'/'};}
else
{
dir=new byte[i];
Array.Copy(path, 0, dir, 0, i);
}
//System.out.println("dir: "+Util.getString(dir));
byte[] pattern=new byte[path.Length-i-1];
Array.Copy(path, i+1, pattern, 0, pattern.Length);
//System.out.println("file: "+Util.getString(pattern));
sendOPENDIR(dir);
buf.rewind();
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
while(true)
{
sendREADDIR(handle);
buf.rewind();
i=io.ins.Read(buf.buffer, 0, buf.buffer.Length);
buf.index=i;
length=buf.getInt();
length=length-(i-4);
type=buf.getByte();
if(type!=SSH_FXP_STATUS && type!=SSH_FXP_NAME)
{
throw new SftpException(SSH_FX_FAILURE, "");
}
if(type==SSH_FXP_STATUS)
{
break;
}
buf.getInt();
int count=buf.getInt();
byte[] str;
int flags;
while(count>0)
{
if(length>0)
{
buf.shift();
i=io.ins.Read(buf.buffer, buf.index, buf.buffer.Length-buf.index);
if(i<=0)break;
buf.index+=i;
length-=i;
}
byte[] filename=buf.getString();
//System.out.println("filename: "+Util.getString(filename));
str=buf.getString();
SftpATTRS attrs=SftpATTRS.getATTR(buf);
if(Util.glob(pattern, filename))
{
v.Add(Util.getString(dir)+"/"+Util.getString(filename));
}
count--;
}
}
sendCLOSE(handle);
buf.rewind();
i=io.ins.Read(buf.buffer, 0, buf.buffer.Length);
length=buf.getInt();
type=buf.getByte();
if(type!=SSH_FXP_STATUS)
{
throw new SftpException(SSH_FX_FAILURE, "");
}
buf.getInt();
i=buf.getInt();
if(i==SSH_FX_OK) return v;
return null;
}
private ArrayList glob_local(String _path)
{
//System.out.println("glob_local: "+_path);
ArrayList v=new ArrayList();
byte[] path=Util.getBytes(_path);
int i=path.Length-1;
while(i>=0){if(path[i]=='*' || path[i]=='?')break;i--;}
if(i<0){ v.Add(_path); return v;}
while(i>=0){if(path[i]==file_separatorc)break;i--;}
if(i<0){ v.Add(_path); return v;}
byte[] dir;
if(i==0){dir=new byte[]{(byte)file_separatorc};}
else
{
dir=new byte[i];
Array.Copy(path, 0, dir, 0, i);
}
byte[] pattern=new byte[path.Length-i-1];
Array.Copy(path, i+1, pattern, 0, pattern.Length);
//System.out.println("dir: "+Util.getString(dir)+" pattern: "+Util.getString(pattern));
try
{
ArrayList children;
children=new ArrayList(Directory.GetDirectories( Util.getString(dir) ));
children.AddRange( Directory.GetFiles(Util.getString(dir)) );
for(int j=0; j<children.Count; j++)
{
//System.out.println("children: "+children[j]);
string child = (string)children[j];
if(Util.glob(pattern, Util.getBytes(child)))
{
v.Add(Util.getString(dir)+file_separator+child);
}
}
}
catch(Exception e)
{
}
return v;
}
private void throwStatusError(Buffer buf, int i)
{
if(server_version>=3)
{
byte[] str=buf.getString();
//byte[] tag=buf.getString();
throw new SftpException(i, Util.getString(str));
}
else
{
throw new SftpException(i, "Failure");
}
}
private static bool isLocalAbsolutePath(String path)
{
return Path.IsPathRooted(path);
}
/*
public void finalize() throws Throwable{
super.finalize();
}
*/
public override void disconnect()
{
//waitForRunningThreadFinish(10000);
clearRunningThreads();
base.disconnect();
}
private ArrayList threadList=null;
[MethodImpl(MethodImplOptions.Synchronized)]
protected void addRunningThread(Thread thread)
{
if(threadList==null)threadList=new ArrayList();
threadList.Add(thread);
}
[MethodImpl(MethodImplOptions.Synchronized)]
protected void clearRunningThreads()
{
if(threadList==null)return;
for(int t=0;t<threadList.Count;t++)
{
Thread thread=(Thread)threadList[t];
if(thread!=null)
if(thread.IsAlive)
thread.Interrupt();
}
threadList.Clear();
}
private bool isPattern(String path)
{
return path.IndexOf("*")!=-1 || path.IndexOf("?")!=-1;
}
public class LsEntry
{
private String filename;
private String longname;
private SftpATTRS attrs;
internal LsEntry(String filename, String longname, SftpATTRS attrs)
{
setFilename(filename);
setLongname(longname);
setAttrs(attrs);
}
public String getFilename(){return filename;}
void setFilename(String filename){this.filename = filename;}
public String getLongname(){return longname;}
void setLongname(String longname){this.longname = longname;}
public SftpATTRS getAttrs(){return attrs;}
void setAttrs(SftpATTRS attrs) {this.attrs = attrs;}
public override String ToString(){ return longname; }
}
}
}
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -