Ejemplo n.º 1
0
		/// <exception cref="NSch.SftpException"></exception>
		public virtual void Mkdir(string path)
		{
			try
			{
				path = RemoteAbsolutePath(path);
				SendMKDIR(Util.Str2byte(path, fEncoding), null);
				ChannelHeader header = new ChannelHeader(this);
				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, string.Empty);
				}
				int i = buf.GetInt();
				if (i == SSH_FX_OK)
				{
					return;
				}
				ThrowStatusError(buf, i);
			}
			catch (Exception e)
			{
				if (e is SftpException)
				{
					throw (SftpException)e;
				}
				if (e is Exception)
				{
					throw new SftpException(SSH_FX_FAILURE, string.Empty, (Exception)e);
				}
				throw new SftpException(SSH_FX_FAILURE, string.Empty);
			}
		}
Ejemplo n.º 2
0
		/// <exception cref="NSch.SftpException"></exception>
		private SftpATTRS _lstat(string path)
		{
			try
			{
				SendLSTAT(Util.Str2byte(path, fEncoding));
				ChannelHeader header = new ChannelHeader(this);
				header = Header(buf, header);
				int length = header.length;
				int type = header.type;
				Fill(buf, length);
				if (type != SSH_FXP_ATTRS)
				{
					if (type == SSH_FXP_STATUS)
					{
						int i = buf.GetInt();
						ThrowStatusError(buf, i);
					}
					throw new SftpException(SSH_FX_FAILURE, string.Empty);
				}
				SftpATTRS attr = SftpATTRS.GetATTR(buf);
				return attr;
			}
			catch (Exception e)
			{
				if (e is SftpException)
				{
					throw (SftpException)e;
				}
				if (e is Exception)
				{
					throw new SftpException(SSH_FX_FAILURE, string.Empty, (Exception)e);
				}
				throw new SftpException(SSH_FX_FAILURE, string.Empty);
			}
		}
Ejemplo n.º 3
0
			/// <exception cref="System.IO.IOException"></exception>
			internal virtual void Cancel(ChannelHeader header, Buffer buf)
			{
				int _count = this.count;
				for (int i = 0; i < _count; i++)
				{
					header = this._enclosing.Header(buf, header);
					int length = header.length;
					this.Get(header.rid);
					this._enclosing.Skip(length);
				}
			}
Ejemplo n.º 4
0
		/// <exception cref="NSch.SftpException"></exception>
		public virtual void Rmdir(string path)
		{
			try
			{
				path = RemoteAbsolutePath(path);
				ArrayList v = Glob_remote(path);
				int vsize = v.Count;
				ChannelHeader header = new ChannelHeader(this);
				for (int j = 0; j < vsize; j++)
				{
					path = (string)(v[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, string.Empty);
					}
					int i = buf.GetInt();
					if (i != SSH_FX_OK)
					{
						ThrowStatusError(buf, i);
					}
				}
			}
			catch (Exception e)
			{
				if (e is SftpException)
				{
					throw (SftpException)e;
				}
				if (e is Exception)
				{
					throw new SftpException(SSH_FX_FAILURE, string.Empty, (Exception)e);
				}
				throw new SftpException(SSH_FX_FAILURE, string.Empty);
			}
		}
Ejemplo n.º 5
0
		/// <exception cref="System.Exception"></exception>
		private ArrayList Glob_remote(string _path)
		{
			ArrayList v = new ArrayList();
			int i = 0;
			int foo = _path.LastIndexOf('/');
			if (foo < 0)
			{
				// it is not absolute path.
				v.Add(Util.Unquote(_path));
				return v;
			}
			string dir = Sharpen.Runtime.Substring(_path, 0, ((foo == 0) ? 1 : foo));
			string _pattern = Sharpen.Runtime.Substring(_path, foo + 1);
			dir = Util.Unquote(dir);
			byte[] pattern = null;
			byte[][] _pattern_utf8 = new byte[1][];
			bool pattern_has_wildcard = IsPattern(_pattern, _pattern_utf8);
			if (!pattern_has_wildcard)
			{
				if (!dir.Equals("/"))
				{
					dir += "/";
				}
				v.Add(dir + Util.Unquote(_pattern));
				return v;
			}
			pattern = _pattern_utf8[0];
			SendOPENDIR(Util.Str2byte(dir, fEncoding));
			ChannelHeader header = new ChannelHeader(this);
			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, string.Empty);
			}
			if (type == SSH_FXP_STATUS)
			{
				i = buf.GetInt();
				ThrowStatusError(buf, i);
			}
			byte[] handle = buf.GetString();
			// filename
			string pdir = null;
			// parent directory
			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, string.Empty);
				}
				if (type == SSH_FXP_STATUS)
				{
					Fill(buf, length);
					break;
				}
				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);
						i = io_in.Read(buf.buffer, buf.index, j);
						if (i <= 0)
						{
							break;
						}
						buf.index += i;
						length -= i;
					}
					byte[] filename = buf.GetString();
					//System.err.println("filename: "+new String(filename));
					if (server_version <= 3)
					{
						str = buf.GetString();
					}
					// longname
					SftpATTRS attrs = SftpATTRS.GetATTR(buf);
					byte[] _filename = filename;
					string f = null;
					bool found = false;
					if (!fEncoding_is_utf8)
					{
						f = Util.Byte2str(filename, fEncoding);
						_filename = Util.Str2byte(f, UTF8);
					}
					found = Util.Glob(pattern, _filename);
					if (found)
					{
						if (f == null)
						{
							f = Util.Byte2str(filename, fEncoding);
						}
						if (pdir == null)
						{
							pdir = dir;
							if (!pdir.EndsWith("/"))
							{
								pdir += "/";
							}
						}
						v.Add(pdir + f);
					}
					count--;
				}
			}
			if (_sendCLOSE(handle, header))
			{
				return v;
			}
			return null;
		}
Ejemplo n.º 6
0
		/// <exception cref="NSch.SftpException"></exception>
		public virtual void _put(InputStream src, string dst, SftpProgressMonitor monitor
			, int mode)
		{
			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)
					{
					}
				}
				//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);
				}
				ChannelHeader header = new ChannelHeader(this);
				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;
				bool 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;
					}
					do
					{
						// padding and mac
						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.Console.Error.WriteLine("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 is SftpException)
				{
					throw (SftpException)e;
				}
				if (e is Exception)
				{
					throw new SftpException(SSH_FX_FAILURE, e.ToString(), (Exception)e);
				}
				throw new SftpException(SSH_FX_FAILURE, e.ToString());
			}
		}
Ejemplo n.º 7
0
		/// <exception cref="NSch.SftpException"></exception>
		public virtual string Readlink(string path)
		{
			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));
				ChannelHeader header = new ChannelHeader(this);
				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, string.Empty);
				}
				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_1 = buf.GetInt();
				ThrowStatusError(buf, i_1);
			}
			catch (Exception e)
			{
				if (e is SftpException)
				{
					throw (SftpException)e;
				}
				if (e is Exception)
				{
					throw new SftpException(SSH_FX_FAILURE, string.Empty, (Exception)e);
				}
				throw new SftpException(SSH_FX_FAILURE, string.Empty);
			}
			return null;
		}
Ejemplo n.º 8
0
		/// <exception cref="System.IO.IOException"></exception>
		/// <exception cref="NSch.SftpException"></exception>
		private bool CheckStatus(int[] ackid, ChannelHeader header)
		{
			header = Header(buf, header);
			int length = header.length;
			int type = header.type;
			if (ackid != null)
			{
				ackid[0] = header.rid;
			}
			Fill(buf, length);
			if (type != SSH_FXP_STATUS)
			{
				throw new SftpException(SSH_FX_FAILURE, string.Empty);
			}
			int i = buf.GetInt();
			if (i != SSH_FX_OK)
			{
				ThrowStatusError(buf, i);
			}
			return true;
		}
Ejemplo n.º 9
0
			/// <exception cref="System.IO.IOException"></exception>
			public override int Read(byte[] d, int s, int len)
			{
				if (this.closed)
				{
					return -1;
				}
				if (d == null)
				{
					throw new ArgumentNullException();
				}
				if (s < 0 || len < 0 || s + len > d.Length)
				{
					throw new IndexOutOfRangeException();
				}
				if (len == 0)
				{
					return 0;
				}
				if (this.rest_length > 0)
				{
					int foo = this.rest_length;
					if (foo > len)
					{
						foo = len;
					}
					System.Array.Copy(this.rest_byte, 0, d, s, foo);
					if (foo != this.rest_length)
					{
						System.Array.Copy(this.rest_byte, foo, this.rest_byte, 0, this.rest_length - foo);
					}
					if (monitor != null)
					{
						if (!monitor.Count(foo))
						{
							this.Close();
							return -1;
						}
					}
					this.rest_length -= foo;
					return foo;
				}
				if (this._enclosing.buf.buffer.Length - 13 < len)
				{
					len = this._enclosing.buf.buffer.Length - 13;
				}
				if (this._enclosing.server_version == 0 && len > 1024)
				{
					len = 1024;
				}
				try
				{
					this._enclosing.SendREAD(handle, this.offset, len);
				}
				catch (Exception)
				{
					throw new IOException("error");
				}
				this.header = this._enclosing.Header(this._enclosing.buf, this.header);
				this.rest_length = this.header.length;
				int type = this.header.type;
				int id = this.header.rid;
				if (type != ChannelSftp.SSH_FXP_STATUS && type != ChannelSftp.SSH_FXP_DATA)
				{
					throw new IOException("error");
				}
				if (type == ChannelSftp.SSH_FXP_STATUS)
				{
					this._enclosing.Fill(this._enclosing.buf, this.rest_length);
					int i = this._enclosing.buf.GetInt();
					this.rest_length = 0;
					if (i == ChannelSftp.SSH_FX_EOF)
					{
						this.Close();
						return -1;
					}
					throw new IOException("error");
				}
				this._enclosing.buf.Rewind();
				this._enclosing.Fill(this._enclosing.buf.buffer, 0, 4);
				int i_1 = this._enclosing.buf.GetInt();
				this.rest_length -= 4;
				this.offset += this.rest_length;
				int foo_1 = i_1;
				if (foo_1 > 0)
				{
					int bar = this.rest_length;
					if (bar > len)
					{
						bar = len;
					}
					i_1 = this._enclosing.io_in.Read(d, s, bar);
					if (i_1 < 0)
					{
						return -1;
					}
					this.rest_length -= i_1;
					if (this.rest_length > 0)
					{
						if (this.rest_byte.Length < this.rest_length)
						{
							this.rest_byte = new byte[this.rest_length];
						}
						int _s = 0;
						int _len = this.rest_length;
						int j;
						while (_len > 0)
						{
							j = this._enclosing.io_in.Read(this.rest_byte, _s, _len);
							if (j <= 0)
							{
								break;
							}
							_s += j;
							_len -= j;
						}
					}
					if (monitor != null)
					{
						if (!monitor.Count(i_1))
						{
							this.Close();
							return -1;
						}
					}
					return i_1;
				}
				return 0;
			}
Ejemplo n.º 10
0
		/// <exception cref="NSch.SftpException"></exception>
		public virtual ArrayList Ls(string path)
		{
			//System.out.println("ls: "+path);
			try
			{
				path = RemoteAbsolutePath(path);
				byte[] pattern = null;
				ArrayList v = new ArrayList();
				int foo = path.LastIndexOf('/');
				string dir = Sharpen.Runtime.Substring(path, 0, ((foo == 0) ? 1 : foo));
				string _pattern = Sharpen.Runtime.Substring(path, 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][];
				bool 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 (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));
				ChannelHeader header = new ChannelHeader(this);
				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, string.Empty);
				}
				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, string.Empty);
					}
					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);
						bool 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.Add(new ChannelSftp.LsEntry(this, f, l, attrs));
						}
						count--;
					}
				}
				_sendCLOSE(handle, header);
				return v;
			}
			catch (Exception e)
			{
				if (e is SftpException)
				{
					throw (SftpException)e;
				}
				if (e is Exception)
				{
					throw new SftpException(SSH_FX_FAILURE, string.Empty, (Exception)e);
				}
				throw new SftpException(SSH_FX_FAILURE, string.Empty);
			}
		}
Ejemplo n.º 11
0
		/// <exception cref="NSch.SftpException"></exception>
		public virtual InputStream Get(string src, SftpProgressMonitor monitor, long skip
			)
		{
			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);
				ChannelHeader header = new ChannelHeader(this);
				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, string.Empty);
				}
				if (type == SSH_FXP_STATUS)
				{
					int i = buf.GetInt();
					ThrowStatusError(buf, i);
				}
				byte[] handle = buf.GetString();
				// handle
				InputStream @in = new _InputStream_1034(this, skip, monitor, handle);
				//throwStatusError(buf, i);
				// ??
				return @in;
			}
			catch (Exception e)
			{
				if (e is SftpException)
				{
					throw (SftpException)e;
				}
				if (e is Exception)
				{
					throw new SftpException(SSH_FX_FAILURE, string.Empty, (Exception)e);
				}
				throw new SftpException(SSH_FX_FAILURE, string.Empty);
			}
		}
Ejemplo n.º 12
0
		/// <exception cref="NSch.SftpException"></exception>
		private void _get(string src, OutputStream dst, SftpProgressMonitor monitor, int 
			mode, long skip)
		{
			//System.err.println("_get: "+src+", "+dst);
			byte[] srcb = Util.Str2byte(src, fEncoding);
			try
			{
				SendOPENR(srcb);
				ChannelHeader header = new ChannelHeader(this);
				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, string.Empty);
				}
				if (type == SSH_FXP_STATUS)
				{
					int i = buf.GetInt();
					ThrowStatusError(buf, i);
				}
				byte[] handle = buf.GetString();
				// filename
				long offset = 0;
				if (mode == RESUME)
				{
					offset += skip;
				}
				int request_len = 0;
				while (true)
				{
					request_len = buf.buffer.Length - 13;
					if (server_version == 0)
					{
						request_len = 1024;
					}
					SendREAD(handle, offset, request_len);
					header = Header(buf, header);
					length = header.length;
					type = header.type;
					if (type == SSH_FXP_STATUS)
					{
						Fill(buf, length);
						int i = buf.GetInt();
						if (i == SSH_FX_EOF)
						{
							goto loop_break;
						}
						ThrowStatusError(buf, i);
					}
					if (type != SSH_FXP_DATA)
					{
						goto loop_break;
					}
					buf.Rewind();
					Fill(buf.buffer, 0, 4);
					length -= 4;
					int i_1 = buf.GetInt();
					// length of data 
					int foo = i_1;
					while (foo > 0)
					{
						int bar = foo;
						if (bar > buf.buffer.Length)
						{
							bar = buf.buffer.Length;
						}
						i_1 = io_in.Read(buf.buffer, 0, bar);
						if (i_1 < 0)
						{
							goto loop_break;
						}
						int data_len = i_1;
						dst.Write(buf.buffer, 0, data_len);
						offset += data_len;
						foo -= data_len;
						if (monitor != null)
						{
							if (!monitor.Count(data_len))
							{
								while (foo > 0)
								{
									i_1 = io_in.Read(buf.buffer, 0, (buf.buffer.Length < foo ? buf.buffer.Length : foo
										));
									if (i_1 <= 0)
									{
										break;
									}
									foo -= i_1;
								}
								goto loop_break;
							}
						}
					}
loop_continue: ;
				}
loop_break: ;
				//System.err.println("length: "+length);  // length should be 0
				dst.Flush();
				if (monitor != null)
				{
					monitor.End();
				}
				_sendCLOSE(handle, header);
			}
			catch (Exception e)
			{
				if (e is SftpException)
				{
					throw (SftpException)e;
				}
				if (e is Exception)
				{
					throw new SftpException(SSH_FX_FAILURE, string.Empty, (Exception)e);
				}
				throw new SftpException(SSH_FX_FAILURE, string.Empty);
			}
		}
Ejemplo n.º 13
0
		/// <exception cref="NSch.SftpException"></exception>
		public virtual void _put(InputStream src, string dst, SftpProgressMonitor monitor
			, int mode)
		{
			try
			{
				((Channel.MyPipedInputStream)io_in).UpdateReadSide();
				byte[] dstb = Util.Str2byte(dst, fEncoding);
				long skip = 0;
				if (mode == RESUME || mode == APPEND)
				{
					try
					{
						SftpATTRS attr = _stat(dstb);
						skip = attr.GetSize();
					}
					catch (Exception)
					{
					}
				}
				//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);
				}
				ChannelHeader header = new ChannelHeader(this);
				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;
				bool dontcopy = true;
				if (!dontcopy)
				{
					// This case will not work anymore.
					data = new byte[obuf.buffer.Length - (5 + 13 + 21 + handle.Length + Session.buffer_margin
						)];
				}
				long offset = 0;
				if (mode == RESUME || mode == APPEND)
				{
					offset += skip;
				}
				int startid = seq;
				int ackcount = 0;
				int _s = 0;
				int _datalen = 0;
				if (!dontcopy)
				{
					// This case will not work anymore.
					_datalen = data.Length;
				}
				else
				{
					data = obuf.buffer;
					_s = 5 + 13 + 21 + handle.Length;
					_datalen = obuf.buffer.Length - _s - Session.buffer_margin;
				}
				int bulk_requests = rq.Size();
				while (true)
				{
					int nread = 0;
					int count = 0;
					int s = _s;
					int datalen = _datalen;
					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 foo = count;
					while (foo > 0)
					{
						if ((seq - 1) == startid || ((seq - startid) - ackcount) >= bulk_requests)
						{
							while (((seq - startid) - ackcount) >= bulk_requests)
							{
								if (this.rwsize >= foo)
								{
									break;
								}
								if (CheckStatus(ackid, header))
								{
									int _ackid = ackid[0];
									if (startid > _ackid || _ackid > seq - 1)
									{
										if (_ackid == seq)
										{
											System.Console.Error.WriteLine("ack error: startid=" + startid + " seq=" + seq + 
												" _ackid=" + _ackid);
										}
										else
										{
											throw new SftpException(SSH_FX_FAILURE, "ack error: startid=" + startid + " seq="
												 + seq + " _ackid=" + _ackid);
										}
									}
									ackcount++;
								}
								else
								{
									break;
								}
							}
						}
						foo -= SendWRITE(handle, offset, data, 0, foo);
					}
					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 is SftpException)
				{
					throw (SftpException)e;
				}
				if (e is Exception)
				{
					throw new SftpException(SSH_FX_FAILURE, e.ToString(), (Exception)e);
				}
				throw new SftpException(SSH_FX_FAILURE, e.ToString());
			}
		}
Ejemplo n.º 14
0
		/// <exception cref="NSch.SftpException"></exception>
		/// <exception cref="System.IO.IOException"></exception>
		/// <exception cref="System.Exception"></exception>
		private byte[] _realpath(string path)
		{
			SendREALPATH(Util.Str2byte(path, fEncoding));
			ChannelHeader header = new ChannelHeader(this);
			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, string.Empty);
			}
			int i;
			if (type == SSH_FXP_STATUS)
			{
				i = buf.GetInt();
				ThrowStatusError(buf, i);
			}
			i = buf.GetInt();
			// count
			byte[] str = null;
			while (i-- > 0)
			{
				str = buf.GetString();
				// absolute path;
				if (server_version <= 3)
				{
					byte[] lname = buf.GetString();
				}
				// long filename
				SftpATTRS attr = SftpATTRS.GetATTR(buf);
			}
			// dummy attribute
			return str;
		}
Ejemplo n.º 15
0
		/// <exception cref="NSch.SftpException"></exception>
		public virtual void Symlink(string oldpath, string newpath)
		{
			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));
				ChannelHeader header = new ChannelHeader(this);
				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, string.Empty);
				}
				int i = buf.GetInt();
				if (i == SSH_FX_OK)
				{
					return;
				}
				ThrowStatusError(buf, i);
			}
			catch (Exception e)
			{
				if (e is SftpException)
				{
					throw (SftpException)e;
				}
				if (e is Exception)
				{
					throw new SftpException(SSH_FX_FAILURE, string.Empty, (Exception)e);
				}
				throw new SftpException(SSH_FX_FAILURE, string.Empty);
			}
		}
Ejemplo n.º 16
0
		/// <exception cref="NSch.SftpException"></exception>
		private void _setStat(string path, SftpATTRS attr)
		{
			try
			{
				SendSETSTAT(Util.Str2byte(path, fEncoding), attr);
				ChannelHeader header = new ChannelHeader(this);
				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, string.Empty);
				}
				int i = buf.GetInt();
				if (i != SSH_FX_OK)
				{
					ThrowStatusError(buf, i);
				}
			}
			catch (Exception e)
			{
				if (e is SftpException)
				{
					throw (SftpException)e;
				}
				if (e is Exception)
				{
					throw new SftpException(SSH_FX_FAILURE, string.Empty, (Exception)e);
				}
				throw new SftpException(SSH_FX_FAILURE, string.Empty);
			}
		}
Ejemplo n.º 17
0
		/// <exception cref="NSch.SftpException"></exception>
		public virtual void Rename(string oldpath, string newpath)
		{
			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);
				ArrayList v = Glob_remote(newpath);
				int vsize = v.Count;
				if (vsize >= 2)
				{
					throw new SftpException(SSH_FX_FAILURE, v.ToString());
				}
				if (vsize == 1)
				{
					newpath = (string)(v[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));
				ChannelHeader header = new ChannelHeader(this);
				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, string.Empty);
				}
				int i = buf.GetInt();
				if (i == SSH_FX_OK)
				{
					return;
				}
				ThrowStatusError(buf, i);
			}
			catch (Exception e)
			{
				if (e is SftpException)
				{
					throw (SftpException)e;
				}
				if (e is Exception)
				{
					throw new SftpException(SSH_FX_FAILURE, string.Empty, (Exception)e);
				}
				throw new SftpException(SSH_FX_FAILURE, string.Empty);
			}
		}
Ejemplo n.º 18
0
		/// <exception cref="System.Exception"></exception>
		private bool _sendCLOSE(byte[] handle, ChannelHeader header)
		{
			SendCLOSE(handle);
			return CheckStatus(null, header);
		}
Ejemplo n.º 19
0
		/// <exception cref="NSch.JSchException"></exception>
		public override void Start()
		{
			try
			{
				PipedOutputStream pos = new PipedOutputStream();
				io.SetOutputStream(pos);
				PipedInputStream pis = new Channel.MyPipedInputStream(this, 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.DoRequest(GetSession(), this);
				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
				ChannelHeader header = new ChannelHeader(this);
				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 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(Util.Byte2str(extension_name), Util.Byte2str(extension_data));
					}
				}
				lcwd = new FilePath(".").GetCanonicalPath();
			}
			catch (Exception e)
			{
				//System.err.println(e);
				if (e is JSchException)
				{
					throw (JSchException)e;
				}
				if (e is Exception)
				{
					throw new JSchException(e.ToString(), (Exception)e);
				}
				throw new JSchException(e.ToString());
			}
		}
Ejemplo n.º 20
0
		/// <exception cref="System.IO.IOException"></exception>
		private ChannelHeader Header(Buffer buf, ChannelHeader header)
		{
			buf.Rewind();
			int i = Fill(buf.buffer, 0, 9);
			header.length = buf.GetInt() - 5;
			header.type = buf.GetByte() & unchecked((int)(0xff));
			header.rid = buf.GetInt();
			return header;
		}
Ejemplo n.º 21
0
		private bool IsRemoteDir(string path)
		{
			try
			{
				SendSTAT(Util.Str2byte(path, fEncoding));
				ChannelHeader header = new ChannelHeader(this);
				header = Header(buf, header);
				int length = header.length;
				int type = header.type;
				Fill(buf, length);
				if (type != SSH_FXP_ATTRS)
				{
					return false;
				}
				SftpATTRS attr = SftpATTRS.GetATTR(buf);
				return attr.IsDir();
			}
			catch (Exception)
			{
			}
			return false;
		}
Ejemplo n.º 22
0
		/// <exception cref="NSch.SftpException"></exception>
		public virtual OutputStream Put(string dst, SftpProgressMonitor monitor, int mode
			, long offset)
		{
			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)
					{
					}
				}
				//System.err.println(eee);
				if (mode == OVERWRITE)
				{
					SendOPENW(dstb);
				}
				else
				{
					SendOPENA(dstb);
				}
				ChannelHeader header = new ChannelHeader(this);
				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, string.Empty);
				}
				if (type == SSH_FXP_STATUS)
				{
					int i = buf.GetInt();
					ThrowStatusError(buf, i);
				}
				byte[] handle = buf.GetString();
				// handle
				if (mode == RESUME || mode == APPEND)
				{
					offset += skip;
				}
				long[] _offset = new long[1];
				_offset[0] = offset;
				OutputStream @out = new _OutputStream_639(this, handle, _offset, monitor);
				return @out;
			}
			catch (Exception e)
			{
				if (e is SftpException)
				{
					throw (SftpException)e;
				}
				if (e is Exception)
				{
					throw new SftpException(SSH_FX_FAILURE, string.Empty, (Exception)e);
				}
				throw new SftpException(SSH_FX_FAILURE, string.Empty);
			}
		}
Ejemplo n.º 23
0
		/// <exception cref="NSch.SftpException"></exception>
		private void _get(string src, OutputStream dst, SftpProgressMonitor monitor, int 
			mode, long skip)
		{
			//System.err.println("_get: "+src+", "+dst);
			byte[] srcb = Util.Str2byte(src, fEncoding);
			try
			{
				SendOPENR(srcb);
				ChannelHeader header = new ChannelHeader(this);
				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, string.Empty);
				}
				if (type == SSH_FXP_STATUS)
				{
					int i = buf.GetInt();
					ThrowStatusError(buf, i);
				}
				byte[] handle = buf.GetString();
				// filename
				long offset = 0;
				if (mode == RESUME)
				{
					offset += skip;
				}
				int request_max = 1;
				rq.Init();
				long request_offset = offset;
				int request_len = buf.buffer.Length - 13;
				if (server_version == 0)
				{
					request_len = 1024;
				}
				while (true)
				{
					while (rq.Count() < request_max)
					{
						SendREAD(handle, request_offset, request_len, rq);
						request_offset += request_len;
					}
					header = Header(buf, header);
					length = header.length;
					type = header.type;
					ChannelSftp.RequestQueue.Request rr = rq.Get(header.rid);
					if (type == SSH_FXP_STATUS)
					{
						Fill(buf, length);
						int i = buf.GetInt();
						if (i == SSH_FX_EOF)
						{
							goto loop_break;
						}
						ThrowStatusError(buf, i);
					}
					if (type != SSH_FXP_DATA)
					{
						goto loop_break;
					}
					buf.Rewind();
					Fill(buf.buffer, 0, 4);
					length -= 4;
					int length_of_data = buf.GetInt();
					// length of data 
					int optional_data = length - length_of_data;
					int foo = length_of_data;
					while (foo > 0)
					{
						int bar = foo;
						if (bar > buf.buffer.Length)
						{
							bar = buf.buffer.Length;
						}
						int data_len = io_in.Read(buf.buffer, 0, bar);
						if (data_len < 0)
						{
							goto loop_break;
						}
						dst.Write(buf.buffer, 0, data_len);
						offset += data_len;
						foo -= data_len;
						if (monitor != null)
						{
							if (!monitor.Count(data_len))
							{
								Skip(foo);
								if (optional_data > 0)
								{
									Skip(optional_data);
								}
								goto loop_break;
							}
						}
					}
					//System.err.println("length: "+length);  // length should be 0
					if (optional_data > 0)
					{
						Skip(optional_data);
					}
					if (length_of_data < rr.length)
					{
						//
						rq.Cancel(header, buf);
						SendREAD(handle, rr.offset + length_of_data, (int)(rr.length - length_of_data), rq
							);
						request_offset = rr.offset + rr.length;
					}
					if (request_max < rq.Size())
					{
						request_max++;
					}
loop_continue: ;
				}
loop_break: ;
				dst.Flush();
				if (monitor != null)
				{
					monitor.End();
				}
				rq.Cancel(header, buf);
				_sendCLOSE(handle, header);
			}
			catch (Exception e)
			{
				if (e is SftpException)
				{
					throw (SftpException)e;
				}
				if (e is Exception)
				{
					throw new SftpException(SSH_FX_FAILURE, string.Empty, (Exception)e);
				}
				throw new SftpException(SSH_FX_FAILURE, string.Empty);
			}
		}