Ejemplo n.º 1
0
		/// <exception cref="NSch.SftpException"></exception>
		public virtual void Get(string src, string dst, SftpProgressMonitor monitor, int 
			mode)
		{
			// System.out.println("get: "+src+" "+dst);
			src = RemoteAbsolutePath(src);
			dst = LocalAbsolutePath(dst);
			try
			{
				ArrayList v = Glob_remote(src);
				int vsize = v.Count;
				if (vsize == 0)
				{
					throw new SftpException(SSH_FX_NO_SUCH_FILE, "No such file");
				}
				FilePath dstFile = new FilePath(dst);
				bool isDstDir = dstFile.IsDirectory();
				StringBuilder dstsb = null;
				if (isDstDir)
				{
					if (!dst.EndsWith(file_separator))
					{
						dst += file_separator;
					}
					dstsb = new StringBuilder(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[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(Sharpen.Runtime.Substring(_src, 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 FilePath(_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 FilePath(_dst).Length());
						}
					}
					FileOutputStream fos = null;
					try
					{
						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 FilePath(_dst).Length());
					}
					finally
					{
						if (fos != null)
						{
							fos.Close();
						}
					}
				}
			}
			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>
		public virtual void Put(string src, string dst, SftpProgressMonitor monitor, int 
			mode)
		{
			src = LocalAbsolutePath(src);
			dst = RemoteAbsolutePath(dst);
			try
			{
				ArrayList v = Glob_remote(dst);
				int vsize = v.Count;
				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[0]);
				}
				bool isRemoteDir = IsRemoteDir(dst);
				v = Glob_local(src);
				vsize = v.Count;
				StringBuilder dstsb = null;
				if (isRemoteDir)
				{
					if (!dst.EndsWith("/"))
					{
						dst += "/";
					}
					dstsb = new StringBuilder(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[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(Sharpen.Runtime.Substring(_src, 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)
						{
						}
						//System.err.println(eee);
						long size_of_src = new FilePath(_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 FilePath(_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 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.º 3
0
		/// <exception cref="NSch.SftpException"></exception>
		public virtual void Get(string src, OutputStream dst, SftpProgressMonitor monitor
			, int mode, long skip)
		{
			//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 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.º 4
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.º 5
0
		/// <exception cref="NSch.SftpException"></exception>
		public virtual void Put(InputStream src, string dst, SftpProgressMonitor monitor, 
			int mode)
		{
			try
			{
				((Channel.MyPipedInputStream)io_in).UpdateReadSide();
				dst = RemoteAbsolutePath(dst);
				ArrayList v = Glob_remote(dst);
				int vsize = v.Count;
				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[0]);
				}
				if (IsRemoteDir(dst))
				{
					throw new SftpException(SSH_FX_FAILURE, dst + " is a directory");
				}
				if (monitor != null)
				{
					monitor.Init(SftpProgressMonitor.PUT, "-", dst, SftpProgressMonitor.UNKNOWN_SIZE);
				}
				_put(src, dst, monitor, mode);
			}
			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());
			}
		}