コード例 #1
0
 public InputStreamGet(
     ChannelSftp sftp,
     byte[] handle,
     SftpProgressMonitor monitor)
 {
     this.sftp    = sftp;
     this.handle  = handle;
     this.monitor = monitor;
 }
コード例 #2
0
 internal OutputStreamPut(ChannelSftp sftp,
     byte[] handle,
     long[] _offset,
     SftpProgressMonitor monitor)
     : base()
 {
     this.sftp=sftp;
     this.handle=handle;
     this._offset=_offset;
     this.monitor=monitor;
 }
コード例 #3
0
 internal OutputStreamPut
     (ChannelSftp sftp,
     byte[] handle,
     long[] _offset,
     SftpProgressMonitor monitor) : base()
 {
     this.sftp    = sftp;
     this.handle  = handle;
     this._offset = _offset;
     this.monitor = monitor;
 }
コード例 #4
0
ファイル: ChannelSftp.cs プロジェクト: joconno4/MediaPortal-2
		public InputStream get(String src, SftpProgressMonitor monitor) 
		{ //throws SftpException{
			return get(src, monitor, OVERWRITE);
		}
コード例 #5
0
ファイル: ChannelSftp.cs プロジェクト: joconno4/MediaPortal-2
		///tamir: updated to jcsh-0.1.30
		private void _get(String src, OutputStream dst,
			SftpProgressMonitor monitor, int mode, long skip) 
		{ //throws SftpException{
			//System.out.println("_get: "+src+", "+dst);
			//try
			//{
				sendOPENR(src.getBytes());
				
				
				Header _header=new Header();
				_header=header(buf, _header);
				int length=_header.length;
				int type=_header.type;
				
				buf.rewind();
				
				fill(buf.buffer, 0, length);

				if(type!=SSH_FXP_STATUS && type!=SSH_FXP_HANDLE)
				{
					//System.Console.WriteLine("Type is "+type);
					throw new SftpException(SSH_FX_FAILURE, "Type is "+type);
				}

				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;
			loop:
				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;
					
					int i;
					if(type==SSH_FXP_STATUS)
					{
						buf.rewind();
						fill(buf.buffer, 0, length);
						i=buf.getInt();   
						if(i==SSH_FX_EOF)
						{
							goto BREAK;
						}
						throwStatusError(buf, i);
					}
					
					if(type!=SSH_FXP_DATA)
					{
						goto BREAK;
					}

					buf.rewind();
					fill(buf.buffer, 0, 4); length-=4;
					i=buf.getInt();   // length of data 
					int foo=i;
					while(foo>0)
					{
						int bar=foo;
						if(bar>buf.buffer.Length)
						{
							bar=buf.buffer.Length;
						}
						i=io.ins.read(buf.buffer, 0, bar);
						if(i<0)
						{
							goto BREAK;
						}
						int data_len=i;
						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=io.ins.read(buf.buffer, 
										0, 
										(buf.buffer.Length<foo?buf.buffer.Length:foo));
									if(i<=0) break;
									foo-=i;
								}
								goto BREAK;
							}
						}
					}
					//System.out.println("length: "+length);  // length should be 0
				}
			BREAK:
				dst.flush();

				if(monitor!=null)monitor.end();
				_sendCLOSE(handle, _header);
			//}
			//catch(Exception e)
			//{
			//	//System.Console.WriteLine(e);
			//	if(e is SftpException) throw (SftpException)e;
			//	throw new SftpException(SSH_FX_FAILURE, "");
			//}
		}
コード例 #6
0
ファイル: ChannelSftp.cs プロジェクト: akrisiun/SharpSSH
        public void get(String remoteAbsolutePath, String localAbsolutePath, SftpProgressMonitor monitor, int mode)
        {
            remoteAbsolutePath = this.remoteAbsolutePath(remoteAbsolutePath);
            localAbsolutePath = this.localAbsolutePath(localAbsolutePath);

            try
            {
                ArrayList files = glob_remote(remoteAbsolutePath);

                if (files.Count == 0)
                {
                    throw new SftpException(SSH_FX_NO_SUCH_FILE, "No such file");
                }

                bool copyingToDirectory = new File(localAbsolutePath).isDirectory();

                // if we're not copying to a directory but we're copying multiple files, there's a problem
                if (false == copyingToDirectory && files.Count > 1)
                {
                    throw new SftpException(SSH_FX_FAILURE, "Copying multiple files, but destination is missing or is a file.");
                }

                // if the given local path doesn't end with a '\' or other file separator, add one
                if (!localAbsolutePath.EndsWith(file_separator))
                {
                    localAbsolutePath += file_separator;
                }

                for (int j = 0; j < files.Count; j++)
                {
                    String sourceFile = (String)(files[j]);

                    SftpATTRS attr = GetPathAttributes(sourceFile);

                    // get information on the current file
                    if (attr.isDir())
                    {
                        // right now it's not able to get a directory
                        throw new SftpException(SSH_FX_FAILURE, "not supported to get directory " + sourceFile);
                    }

                    String destinationPath = null;

                    if (copyingToDirectory)
                    {
                        StringBuilder destinationSb = new StringBuilder(localAbsolutePath);

                        // find the last file separator character
                        int i = sourceFile.LastIndexOf(file_separatorc);

                        // basically we're appending just the filename
                        if (i == -1)
                            destinationSb.Append(sourceFile);
                        else
                            destinationSb.Append(sourceFile.Substring(i + 1));

                        destinationPath = destinationSb.ToString();
                    }
                    else
                    {
                        destinationPath = localAbsolutePath;
                    }

                    if (mode == RESUME)
                    {
                        long sizeOfSourceFile = attr.getSize();

                        long sizeOfDestinationFile = new File(destinationPath).Length();

                        // this means we already copied more data than is available. fail
                        if (sizeOfDestinationFile > sizeOfSourceFile)
                        {
                            throw new SftpException(SSH_FX_FAILURE, "failed to resume for " + destinationPath);
                        }

                        // if the sizes are equal, we're gravy
                        if (sizeOfDestinationFile == sizeOfSourceFile)
                        {
                            return;
                        }
                    }

                    if (monitor != null)
                    {
                        monitor.init(SftpProgressMonitor.GET, sourceFile, destinationPath, attr.getSize());

                        if (mode == RESUME)
                        {
                            monitor.count(new File(destinationPath).Length());
                        }
                    }

                    // create the output stream, and append if it's in overwrite mode
                    FileOutputStream fileOutputStream = new FileOutputStream(destinationPath, mode == OVERWRITE);

                    _get(sourceFile, fileOutputStream, monitor, mode, new File(destinationPath).Length());

                    fileOutputStream.Close();
                }
            }
            catch (SftpException)
            {
                throw;
            }
            catch (Exception)
            {
                throw new SftpException(SSH_FX_FAILURE, "");
            }
        }
コード例 #7
0
ファイル: ChannelSftp.cs プロジェクト: joconno4/MediaPortal-2
		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 is SftpException) throw (SftpException)e;
      throw new SftpException(SSH_FX_FAILURE, e.toString());
    }
		}
コード例 #8
0
ファイル: ChannelSftp.cs プロジェクト: akrisiun/SharpSSH
        public void get(String remoteAbsolutePath, OutputStream dst, SftpProgressMonitor monitor, int mode, long skip)
        {
            try
            {
                remoteAbsolutePath = this.remoteAbsolutePath(remoteAbsolutePath);
                ArrayList v = glob_remote(remoteAbsolutePath);

                if (v.Count != 1)
                {
                    throw new SftpException(SSH_FX_FAILURE, v.ToString());
                }

                remoteAbsolutePath = (String)(v[0]);

                if (monitor != null)
                {
                    SftpATTRS attr = GetPathAttributes(remoteAbsolutePath);
                    monitor.init(SftpProgressMonitor.GET, remoteAbsolutePath, "??", attr.getSize());
                    if (mode == RESUME)
                    {
                        monitor.count(skip);
                    }
                }

                _get(remoteAbsolutePath, dst, monitor, mode, skip);
            }
            catch (Exception e)
            {
                if (e is SftpException) throw (SftpException)e;
                throw new SftpException(SSH_FX_FAILURE, "");
            }
        }
コード例 #9
0
ファイル: ChannelSftp.cs プロジェクト: akrisiun/SharpSSH
        public OutputStream put(String dst, SftpProgressMonitor monitor, int mode, long offset)
        {
            dst = remoteAbsolutePath(dst);
            try
            {
                ArrayList v = glob_remote(dst);
                if (v.Count != 1)
                {
                    throw new SftpException(SSH_FX_FAILURE, v.ToString());
                }
                dst = (String)(v[0]);
                if (isRemoteDir(dst))
                {
                    throw new SftpException(SSH_FX_FAILURE, dst + " is a directory");
                }

                long skip = 0;
                if (mode == RESUME || mode == APPEND)
                {
                    try
                    {
                        SftpATTRS attr = stat(dst);
                        skip = attr.getSize();
                    }
                    catch (Exception) { }
                }

                if (mode == OVERWRITE)
                {
                    sendOPENW(dst.GetBytes());
                }
                else
                {
                    sendOPENA(dst.GetBytes());
                }

                Header _header = new Header();
                _header = header(buf, _header);
                int length = _header.length;
                int type = _header.type;

                buf.rewind();
                fill(buf.buffer, 0, 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(); // filename

                //long offset=0;
                if (mode == RESUME || mode == APPEND)
                {
                    offset += skip;
                }

                long[] _offset = new long[1];
                _offset[0] = offset;
                OutputStream outs = new OutputStreamPut(this, handle, _offset, monitor);

                return outs;
            }
            catch (Exception e)
            {
                if (e is SftpException) throw (SftpException)e;
                throw new SftpException(SSH_FX_FAILURE, "");
            }
        }
コード例 #10
0
ファイル: ChannelSftp.cs プロジェクト: joconno4/MediaPortal-2
		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));
				}

				//System.err.println("dst: "+dst);

				bool _isRemoteDir=isRemoteDir(dst);

				v=glob_local(src);
				//System.err.println("glob_local: "+v+" dst="+dst);
				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 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(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) 
						{
							//	    try{
							fis.close();
							//	    }catch(Exception ee){};
						}
					}
				}
			}
			catch(Exception e)
			{
				if(e is SftpException) throw (SftpException)e;
				throw new SftpException(SSH_FX_FAILURE, e.toString());
			}
		}
コード例 #11
0
ファイル: ChannelSftp.cs プロジェクト: akrisiun/SharpSSH
 public void put(InputStream src, String dst, SftpProgressMonitor monitor)
 {
     put(src, dst, monitor, OVERWRITE);
 }
コード例 #12
0
ファイル: ChannelSftp.cs プロジェクト: akrisiun/SharpSSH
 public OutputStream put(String dst, SftpProgressMonitor monitor, int mode)
 {
     return put(dst, monitor, mode, 0);
 }
コード例 #13
0
ファイル: ChannelSftp.cs プロジェクト: akrisiun/SharpSSH
        public 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 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 (i == -1) dstsb.Append(_src);
                        else dstsb.Append(_src.Substring(i + 1));
                        _dst = dstsb.ToString();
                        dstsb.Remove(dst.Length, _dst.Length);
                    }
                    else
                    {
                        _dst = dst;
                    }

                    long size_of_dst = 0;
                    if (mode == RESUME)
                    {
                        try
                        {
                            SftpATTRS attr = GetPathAttributes(_dst);
                            size_of_dst = attr.getSize();
                        }
                        catch (Exception) { }

                        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 is SftpException) throw (SftpException)e;
                throw new SftpException(SSH_FX_FAILURE, e.Message);
            }
        }
コード例 #14
0
ファイル: ChannelSftp.cs プロジェクト: akrisiun/SharpSSH
        public InputStream get(String remoteAbsolutePath, SftpProgressMonitor monitor, int mode)
        {
            if (mode == RESUME)
            {
                throw new SftpException(SSH_FX_FAILURE, "faile to resume from " + remoteAbsolutePath);
            }
            remoteAbsolutePath = this.remoteAbsolutePath(remoteAbsolutePath);
            try
            {
                ArrayList v = glob_remote(remoteAbsolutePath);
                if (v.Count != 1)
                {
                    throw new SftpException(SSH_FX_FAILURE, v.ToString());
                }
                remoteAbsolutePath = (String)(v[0]);

                SftpATTRS attr = GetPathAttributes(remoteAbsolutePath);
                if (monitor != null)
                {
                    monitor.init(SftpProgressMonitor.GET, remoteAbsolutePath, "??", attr.getSize());
                }

                sendOPENR(remoteAbsolutePath.GetBytes());

                Header _header = new Header();
                _header = header(buf, _header);
                int length = _header.length;
                int type = _header.type;
                buf.rewind();
                fill(buf.buffer, 0, 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(); // filename

                InputStream ins = new InputStreamGet(this, handle, monitor);
                return ins;
            }
            catch (Exception e)
            {
                if (e is SftpException) throw (SftpException)e;
                throw new SftpException(SSH_FX_FAILURE, "");
            }
        }
コード例 #15
0
ファイル: ChannelSftp.cs プロジェクト: akrisiun/SharpSSH
 public InputStream get(String remoteAbsolutePath, SftpProgressMonitor monitor)
 {
     return get(remoteAbsolutePath, monitor, OVERWRITE);
 }
コード例 #16
0
ファイル: ChannelSftp.cs プロジェクト: joconno4/MediaPortal-2
		public InputStream get(String src,  SftpProgressMonitor monitor,  int mode) 
		{ //throws SftpException{
			if(mode==RESUME)
			{
				throw new SftpException(SSH_FX_FAILURE, "faile to resume from "+src);
			}
			src=remoteAbsolutePath(src);
			try
			{
				Vector v=glob_remote(src);
				if(v.size()!=1)
				{
					throw new SftpException(SSH_FX_FAILURE, v.toString());
				}
				src=(String)(v.elementAt(0));

				SftpATTRS attr=_stat(src);
				if(monitor!=null)
				{
					monitor.init(SftpProgressMonitor.GET, src, "??", attr.getSize());
				}

				sendOPENR(src.getBytes());

				Header _header=new Header();
				_header=header(buf, _header);
				int length=_header.length;
				int type=_header.type;
				buf.rewind();
				fill(buf.buffer, 0, 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();         // filename

				java.io.InputStream ins=new InputStreamGet(this, handle, monitor);
				//  long offset=0;
				//  bool closed=false;
				//  int rest_length=0;
				//  byte[] _data=new byte[1];
				//  public int read() { //throws java.io.IOException{
				//    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{
				//    return read(d, 0, d.Length);
				//  }
				//  public int read(byte[] d, int s, int len) { //throws java.io.IOException{
				//    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;
				//      int i=io.ins.read(d, s, foo);
				//      if(i<0){
				//        throw new IOException("error");
				//      }
				//      rest_length-=i;
				//      return i;
				//    }

				//    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"); }

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

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

				//    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.ins.read(d, s, bar);
				//      if(i<0){
				//        return -1;
				//      }
				//      rest_length-=i;

				//      if(monitor!=null){
				//        if(!monitor.count(i)){
				//          return -1;
				//        }
				//      }
				//      return i;
				//    }
				//    return 0; // ??
				//  }
				//  public void close() { //throws IOException{
				//    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{_sendCLOSE(handle);}
				//    catch(Exception e){throw new IOException("error");}
				//  }
				//};
				return ins;
			}
			catch(Exception e)
			{
				if(e is SftpException) throw (SftpException)e;
				throw new SftpException(SSH_FX_FAILURE, "");
			}
		}
コード例 #17
0
        public Stream get(String src, SftpProgressMonitor monitor, int mode)
        {
            if(mode==RESUME)
            {
                throw new SftpException(SSH_FX_FAILURE, "faile to resume from "+src);
            }
            if(!src.StartsWith("/")){ src=cwd+"/"+src; }
            try
            {
                ArrayList v=glob_remote(src);
                if(v.Count!=1)
                {
                    throw new SftpException(SSH_FX_FAILURE, v.ToString());
                }
                src=(String)(v[0]);

                SftpATTRS attr=stat(src);

                PipedInputStream pis=new PipedInputStream();
                PipedOutputStream pos=new PipedOutputStream(pis);
                ChannelSftp channel=this;
                String _src=src;

                if(attr.getSize()<=0)
                {
                    try{ pos.Close(); }
                    catch{}//(Exception ee){}
                    return pis;
                }

                Exception[] closed=new Exception[1];
                closed[0]=null;
                runnable2 runnable2 = new runnable2(channel, pos, _src, monitor, mode, closed);
                (new Thread(new ThreadStart(runnable2.run))).Start();
                while(true)
                {
                    if(pis.available()!=0)break;
                    if(closed[0]!=null)
                    {
                        throw closed[0];
                    }
                    //	System.outs.println("pis wait");
                    Thread.Sleep(1000);
                }
                return pis;
            }
            catch(Exception e)
            {
                if(e is SftpException) throw (SftpException)e;
                throw new SftpException(SSH_FX_FAILURE, "");
            }
        }
コード例 #18
0
ファイル: ChannelSftp.cs プロジェクト: joconno4/MediaPortal-2
			public InputStreamGet( 
				ChannelSftp sftp,
				byte[] handle,
				SftpProgressMonitor monitor)
			{
				this.sftp=sftp;
				this.handle=handle;
				this.monitor=monitor;
			}
コード例 #19
0
ファイル: ChannelSftp.cs プロジェクト: joconno4/MediaPortal-2
		public void get(String src, String dst,
			SftpProgressMonitor monitor) 
		{ //throws SftpException{
			get(src, dst, monitor, OVERWRITE);
		}
コード例 #20
0
ファイル: ChannelSftp.cs プロジェクト: joconno4/MediaPortal-2
		public void put(InputStream src, String dst,
			SftpProgressMonitor monitor) 
		{ //throws SftpException{
			put(src, dst, monitor, OVERWRITE);
		}
コード例 #21
0
ファイル: ChannelSftp.cs プロジェクト: joconno4/MediaPortal-2
		public void get(String src, OutputStream dst,
			SftpProgressMonitor monitor) 
		{ //throws SftpException{
			get(src, dst, monitor, OVERWRITE, 0);
		}
コード例 #22
0
ファイル: ChannelSftp.cs プロジェクト: joconno4/MediaPortal-2
		private void _put(InputStream src, String dst, 
			SftpProgressMonitor monitor, int mode)
		{
			try
			{
				long skip=0;
				if(mode==RESUME || mode==APPEND)
				{
					try
					{
						SftpATTRS attr=_stat(dst);
						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(dst.getBytes()); }
				else{ sendOPENA(dst.getBytes()); }

				Header _header=new Header();
				_header=header(buf, _header);
				int length=_header.length;
				int type=_header.type;
				buf.rewind();
				fill(buf.buffer, 0, 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();         // filename
				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; // 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.ins.available()>=1024)
						{
							while(io.ins.available()>0)
							{
								if(checkStatus(ackid, _header))
								{
									_ackid=ackid[0];
									if(startid>_ackid || _ackid>seq-1)
									{
										if(_ackid==seq)
										{
											java.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);
				//System.err.println("start end "+startid+" "+endid);
			}
			catch(Exception e)
			{
				if(e is SftpException) throw (SftpException)e;
				throw new SftpException(SSH_FX_FAILURE, e.toString());
			}
		}
コード例 #23
0
ファイル: ChannelSftp.cs プロジェクト: akrisiun/SharpSSH
 public void get(String remoteAbsolutePath, OutputStream dst, SftpProgressMonitor monitor)
 {
     get(remoteAbsolutePath, dst, monitor, OVERWRITE, 0);
 }
コード例 #24
0
ファイル: ChannelSftp.cs プロジェクト: joconno4/MediaPortal-2
		public void get(String src, String dst,
			SftpProgressMonitor monitor, int mode)
		{
			//throws SftpException{
			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);
				bool 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 is SftpException) throw (SftpException)e;
				throw new SftpException(SSH_FX_FAILURE, "");
			}
		}
コード例 #25
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
			{
				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]);
				}

				//System.err.println("dst: "+dst);

				bool _isRemoteDir = isRemoteDir(dst);

				v = glob_local(src);
				//System.err.println("glob_local: "+v+" dst="+dst);
				vsize = v.Count;

				string dstsb = "";
				if (_isRemoteDir)
				{
					if (!dst.EndsWith("/"))
					{
						dst += "/";
					}
					dstsb = (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]);
					String _dst = null;
					if (_isRemoteDir)
					{
						int i = _src.LastIndexOf(file_separatorc);
						if (i == -1) dstsb += (_src);
						else dstsb += (_src.Substring(i + 1));
						_dst = dstsb;
						dstsb = dstsb.Remove(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.Size;
						}
						catch (Exception)
						{
							//System.err.println(eee);
						}

						long size_of_src = new System.IO.FileInfo(_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 System.IO.FileInfo(_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)
						{
							//	    try{
							fis.close();
							//	    }catch(Exception ee){};
						}
					}
				}
			}
			catch (Exception e)
			{
				if (e is SftpException) throw (SftpException)e;
				throw new SftpException(SSH_FX_FAILURE, e.ToString());
			}
		}
コード例 #26
0
ファイル: ChannelSftp.cs プロジェクト: joconno4/MediaPortal-2
		public void get(String src, OutputStream dst,
			SftpProgressMonitor monitor, int mode, long skip)
		{
			//throws SftpException{
			//try
			//{
				src=remoteAbsolutePath(src);
				Vector v=glob_remote(src);
				if(v.size()!=1)
				{
					throw new SftpException(SSH_FX_FAILURE, v.toString());
				}
				src=(String)(v.elementAt(0));

				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;
			//	throw new SftpException(SSH_FX_FAILURE, "");
			//}
		}
コード例 #27
0
		public void get(String src, String dst,
			SftpProgressMonitor monitor, int mode)
		{
			//throws SftpException{
			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");
				}

				bool isDstDir = System.IO.Directory.Exists(dst);
				string dstsb = "";
				if (isDstDir)
				{
					if (!dst.EndsWith(file_separator))
					{
						dst += file_separator;
					}
					dstsb = (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.IsDirectory)
					{
						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 += (_src);
						else dstsb += (_src.Substring(i + 1));
						_dst = dstsb;
						dstsb = dstsb.Remove(dst.Length, _dst.Length);
					}
					else
					{
						_dst = dst;
					}

					if (mode == RESUME)
					{
						long size_of_src = attr.Size;
						long size_of_dst = new System.IO.FileInfo(_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.Size);
						if (mode == RESUME)
						{
							monitor.count(new System.IO.FileInfo(_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 System.IO.FileInfo(_dst).Length);
					fos.close();
				}
			}
			catch (Exception e)
			{
				if (e is SftpException) throw (SftpException)e;
				throw new SftpException(SSH_FX_FAILURE, "");
			}
		}
コード例 #28
0
		public void get(String src, OutputStream dst,
			SftpProgressMonitor monitor, int mode, long skip)
		{
			//throws SftpException{
			try
			{
				src = RemoteAbsolutePath(src);
				ArrayList v = glob_remote(src);
				if (v.Count != 1)
				{
					throw new SftpException(SSH_FX_FAILURE, v.ToString());
				}
				src = (String)(v[0]);

				if (monitor != null)
				{
					SftpATTRS attr = _stat(src);
					monitor.init(SftpProgressMonitor.GET, src, "??", attr.Size);
					if (mode == RESUME)
					{
						monitor.count(skip);
					}
				}
				_get(src, dst, monitor, mode, skip);
			}
			catch (Exception e)
			{
				if (e is SftpException) throw (SftpException)e;
				throw new SftpException(SSH_FX_FAILURE, "");
			}
		}
コード例 #29
0
ファイル: ChannelSftp.cs プロジェクト: akrisiun/SharpSSH
 public void get(String remoteAbsolutePath, String localAbsolutePath, SftpProgressMonitor monitor)
 {
     get(remoteAbsolutePath, localAbsolutePath, monitor, OVERWRITE);
 }