⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 channelsftp.cs

📁 Fireball.CodeEditor is an source code editor control derived from the best compona SyntaxBox Control
💻 CS
📖 第 1 页 / 共 4 页
字号:

				if(sftp.buf.buffer.Length-13<len)
				{
					len=sftp.buf.buffer.Length-13;
				}
				if(_server_version[0]==0 && len>1024)
				{
					len=1024; 
				}

				try{sftp.sendREAD(handle, _offset[0], len);}
				catch(Exception e){ throw new IOException("error"); }

				sftp.buf.rewind();
				int ii=sftp.io.ins.Read(sftp.buf.buffer, 0, 13);  // 4 + 1 + 4 + 4
				if(ii!=13)
				{ 
					throw new IOException("error");
				}

				rest_length=sftp.buf.getInt();
				int type=sftp.buf.getByte();  
				rest_length--;
				sftp.buf.getInt();        
				rest_length-=4;
				if(type!=SSH_FXP_STATUS && type!=SSH_FXP_DATA)
				{ 
					throw new IOException("error");
				}
				if(type==SSH_FXP_STATUS)
				{
					ii=sftp.buf.getInt();    
					rest_length-=4;
					sftp.io.ins.Read(sftp.buf.buffer, 13, rest_length);
					rest_length=0;
					if(ii==SSH_FX_EOF)
					{
						Close();
						return -1;
					}
					//throwStatusError(buf, i);
					throw new IOException("error");
				}

				ii=sftp.buf.getInt();    
				rest_length-=4;
				int goo=ii;
				if(goo>0)
				{
					int bar=rest_length;
					if(bar>len)
					{
						bar=len;
					}
					ii=sftp.io.ins.Read(d, s, bar);
					if(ii<0)
					{
						return -1;
					}
					int data_len=ii;
					rest_length-=data_len;
  
					if(monitor!=null)
					{
						if(!monitor.count(data_len))
						{
							return -1;
						}
					}
					_offset[0]+=data_len;
					return ii;
				}
				return 0; // ??
			}
			public override void Close() 
			{
				if(closed)return;
				closed=true;
				/*
					while(rest_length>0){
					  int foo=rest_length;
					  if(foo>buf.buffer.Length){
						foo=buf.buffer.Length;
					  }
					  io.ins.Read(buf.buffer, 0, foo);
					  rest_length-=foo;
					}
					*/
				if(monitor!=null)monitor.end();
				try{sftp._sendCLOSE(handle);}
				catch(Exception e){throw new IOException("error");}
			}
		}

		public ArrayList ls(String path) 
		{
			try
			{
				if(!path.StartsWith("/")){ path=cwd+"/"+path; }

				String dir=path;
				byte[] pattern=null;
				SftpATTRS attr=null;
				if(isPattern(dir) || 
					((attr=stat(dir))!=null && !attr.isDir()))
				{
					int foo=path.LastIndexOf('/');
					dir=path.Substring(0, ((foo==0)?1:foo));
					pattern=Util.getBytes( path.Substring(foo+1) );
				}
				sendOPENDIR(Util.getBytes(dir));

				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

				ArrayList v=new ArrayList();
				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)
					{ 
						/*
						buf.getInt();
						i=buf.getInt();
						System.out.println("i="+i);
						if(i==SSH_FX_EOF) break;
						byte[] str=buf.getString();
						throw new SftpException(i, Util.getString(str));
						*/
						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();
						String longname=Util.getString(str);
						// System.out.println("longname: "+longname);

						SftpATTRS attrs=SftpATTRS.getATTR(buf);
						if(pattern==null || Util.glob(pattern, filename))
						{
							//System.out.println(Util.getString(filename)+" |"+longname+"|");
							//  	    v.Add(longname);
							v.Add(new LsEntry(Util.getString(filename), longname, attrs));
						}

						count--; 
					}
				}
				_sendCLOSE(handle);
				return v;
			}
			catch(Exception e)
			{
				if(e is SftpException) throw (SftpException)e;
				throw new SftpException(SSH_FX_FAILURE, "");
			}
		}
		public void symlink(String oldpath, String newpath) 
		{
			if(server_version<3)
			{
				throw new SftpException(SSH_FX_FAILURE, 
					"The remote sshd is too old to support symlink operation.");
			}

			try
			{
				if(!oldpath.StartsWith("/")){ oldpath=cwd+"/"+oldpath; } 
				if(!newpath.StartsWith("/")){ newpath=cwd+"/"+newpath; } 

				ArrayList v=glob_remote(oldpath);
				if(v.Count!=1)
				{
					throw new SftpException(SSH_FX_FAILURE, v.ToString());
				}
				oldpath=(String)(v[0]);

				sendSYMLINK(Util.getBytes(oldpath), Util.getBytes(newpath));
				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);
			}
			catch(Exception e)
			{
				if(e is SftpException) throw (SftpException)e;
				throw new SftpException(SSH_FX_FAILURE, "");
			}
		}
		public void rename(String oldpath, String newpath) 
		{
			if(server_version<2)
			{
				throw new SftpException(SSH_FX_FAILURE, 
					"The remote sshd is too old to support rename operation.");
			}
			try
			{
				if(!oldpath.StartsWith("/")){ oldpath=cwd+"/"+oldpath; } 
				if(!newpath.StartsWith("/")){ newpath=cwd+"/"+newpath; } 

				ArrayList v=glob_remote(oldpath);
				if(v.Count!=1)
				{
					throw new SftpException(SSH_FX_FAILURE, v.ToString());
				}
				oldpath=(String)(v[0]);

				v=glob_remote(newpath);
				if(v.Count>=2)
				{
					throw new SftpException(SSH_FX_FAILURE, v.ToString());
				}
				if(v.Count==1)
				{
					newpath=(String)(v[0]);
				}

				sendRENAME(Util.getBytes(oldpath), Util.getBytes(newpath));
				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);
			}
			catch(Exception e)
			{
				if(e is SftpException) throw (SftpException)e;
				throw new SftpException(SSH_FX_FAILURE, "");
			}
		}
		public void rm(String path) 
		{
			try
			{
				if(!path.StartsWith("/")){ path=cwd+"/"+path; }
				ArrayList v=glob_remote(path);
				for(int j=0; j<v.Count; j++)
				{
					path=(String)(v[j]);
					sendREMOVE(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_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, "");
			}
		}
		private bool isRemoteDir(String path)
		{
			try
			{
				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){ return false; }
				buf.getInt();
				SftpATTRS attr=SftpATTRS.getATTR(buf);
				return attr.isDir();
			}
			catch(Exception e){}
			return false;
		}
		/*
		bool isRemoteDir(String path) {
		  SftpATTRS attr=stat(path);
		  return attr.isDir();
		}
		*/
		public void chgrp(int gid, String path) 
		{
			try
			{
				if(!path.StartsWith("/")){ path=cwd+"/"+path; }

				ArrayList v=glob_remote(path);
				for(int j=0; j<v.Count; j++)
				{
					path=(String)(v[j]);
					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)
					{
						throw new SftpException(SSH_FX_FAILURE, "");
					}
					buf.getInt();
					SftpATTRS attr=SftpATTRS.getATTR(buf);
					attr.setUIDGID(attr.uid, gid); 
					_setStat(path, attr);
				}
			}
			catch(Exception e)
			{
				if(e is SftpException) throw (SftpException)e;
				throw new SftpException(SSH_FX_FAILURE, "");
			}
		}
		public void chown(int uid, String path) 
		{
			try
			{
				if(!path.StartsWith("/")){ path=cwd+"/"+path; }

				ArrayList v=glob_remote(path);
				for(int j=0; j<v.Count; j++)
				{
					path=(String)(v[j]);

					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)
					{
						throw new SftpException(SSH_FX_FAILURE, "");
					}
					buf.getInt();
					SftpATTRS attr=SftpATTRS.getATTR(buf);
					attr.setUIDGID(uid, attr.gid); 
					_setStat(path, attr);
				}
			}
			catch(Exception e)
			{
				if(e is SftpException) throw (SftpException)e;
				throw new SftpException(SSH_FX_FAILURE, "");
			}
		}
		public void chmod(int permissions, String path) 
		{
			try
			{
				if(!path.StartsWith("/")){ path=cwd+"/"+path; }

				ArrayList v=glob_remote(path);
				for(int j=0; j<v.Count; j++)
				{
					path=(String)(v[j]);

					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)
					{
						throw new SftpException(SSH_FX_FAILURE, "");
					}
					buf.getInt();
					SftpATTRS attr=SftpATTRS.getATTR(buf);
					attr.setPERMISSIONS(permissions); 
					_setStat(path, attr);
				}
			}
			catch(Exception e)
			{
				if(e is SftpException) throw (SftpException)e;
				throw new SftpException(SSH_FX_FAILURE, "");
			}
		}
		public void setMtime(String path, int mtime) 
		{
			try
			{
				if(!path.StartsWith("/")){ path=cwd+"/"+path; }

				ArrayList v=glob_remote(path);
				for(int j=0; j<v.Count; j++)
				{
					path=(String)(v[j]);
					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)
					{
						throw new SftpException(SSH_FX_FAILURE, "");
					}
					buf.getInt();
					SftpATTRS attr=SftpATTRS.getATTR(buf);
					attr.setACMODTIME(attr.getATime(), mtime);
					_setStat(path, attr);
				}
			}
			catch(Exception e)
			{
				if(e is SftpException) throw (SftpException)e;
				throw new SftpException(SSH_FX_FAILURE, "");
			}
		}
		public void rmdir(String path) 
		{
			try
			{
				if(!path.StartsWith("/")){ path=cwd+"/"+path; }
				ArrayList v=glob_remote(path);
				for(int j=0; j<v.Count; j++)
				{
					path=(String)(v[j]);

					sendRMDIR(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_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 void mkdir(String path) 
		{
			try
			{
				if(!path.StartsWith("/")){ path=cwd+"/"+path; }
				sendMKDIR(Util.getBytes(path), null);
				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);
			}
			catch(Exception e)
			{

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -