internal OutputStreamPut(ChannelSftp sftp, byte[] handle, long[] offset, SftpProgressMonitor monitor)
     : base()
 {
     m_sftp = sftp;
     m_handle = handle;
     m_offset = offset;
     m_monitor = monitor;
 }
Exemple #2
0
 private void RunRemoteToLocal()
 {
     getTransferItem("R2L", (item) =>
     {
         if (item != null)
         {
             transfering = true;
             try
             {
                 if (File.Exists(item.LocalPath))
                 {
                     this.BeginInvoke((MethodInvoker) delegate()
                     {
                         DialogResult dr = MessageBox.Show(this, item.LocalPath + "已存在,是否覆盖?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                         if (dr == System.Windows.Forms.DialogResult.Yes)
                         {
                             WriteLog("R2L", item.Id, item.LocalPath, item.RemotePath);
                             SftpProgressMonitor monitor = rightForm.TransfersToLocal(item.Id, item.LocalPath, item.RemotePath, ChannelSftp.OVERWRITE, new ProgressDelegate(TransfersFileProgress), new TransfersEndDelegate(TransfersFileEnd));
                         }
                         else
                         {
                             ChangeTransferItemStatus("R2L", item.Id, TransferStatus.Cancel);
                         }
                     });
                 }
                 else
                 {
                     FileInfo file = new FileInfo(item.LocalPath);
                     if (!file.Directory.Exists)
                     {
                         Utils.createParentDir(file.Directory);
                     }
                     WriteLog("R2L", item.Id, item.LocalPath, item.RemotePath);
                     SftpProgressMonitor monitor = rightForm.TransfersToLocal(item.Id, item.LocalPath, item.RemotePath, ChannelSftp.OVERWRITE, new ProgressDelegate(TransfersFileProgress), new TransfersEndDelegate(TransfersFileEnd));
                 }
             }
             catch (Exception ex)
             {
                 logger.Error("传输文件的到服务器时异常:" + ex.Message, ex);
                 ChangeTransferItemStatus("R2L", item.Id, TransferStatus.Failed);
             }
         }
         else
         {
             remoteToLocalRun = false;
         }
     });
 }
Exemple #3
0
 private void RunLocalToRemote()
 {
     getTransferItem("L2R", (item) => {
         if (item != null)
         {
             transfering = true;
             try
             {
                 if (RemoteExist(item.RemotePath, item.Name))
                 {
                     this.BeginInvoke((MethodInvoker) delegate()
                     {
                         DialogResult dr = MessageBox.Show(this, item.RemotePath + "已存在,是否覆盖?", "警告", MessageBoxButtons.YesNo, MessageBoxIcon.Warning);
                         if (dr == System.Windows.Forms.DialogResult.Yes)
                         {
                             WriteLog("L2R", item.Id, item.LocalPath, item.RemotePath);
                             SftpProgressMonitor monitor = rightForm.TransfersToRemote(item.Id, item.LocalPath, item.RemotePath, ChannelSftp.OVERWRITE, new ProgressDelegate(TransfersFileProgress), new TransfersEndDelegate(TransfersFileEnd));
                         }
                         else
                         {
                             ChangeTransferItemStatus("L2R", item.Id, TransferStatus.Cancel);
                         }
                     });
                 }
                 else
                 {
                     createRemoteDir(item.RemotePath, () =>
                     {
                         WriteLog("L2R", item.Id, item.LocalPath, item.RemotePath);
                         SftpProgressMonitor monitor = rightForm.TransfersToRemote(item.Id, item.LocalPath, item.RemotePath, ChannelSftp.OVERWRITE, new ProgressDelegate(TransfersFileProgress), new TransfersEndDelegate(TransfersFileEnd));
                     });
                 }
             }
             catch (Exception ex)
             {
                 logger.Error("传输文件的到服务器时异常:" + ex.Message);
                 ChangeTransferItemStatus("L2R", item.Id, TransferStatus.Failed);
             }
         }
         else
         {
             localToRemoteRun = false;
         }
     });
 }
Exemple #4
0
 public OutputStream put(string dst, SftpProgressMonitor monitor, ChannelSftpModes mode)
 {
     return put(dst, monitor, mode, 0);
 }
Exemple #5
0
        public void put(InputStream src, string dst, SftpProgressMonitor monitor, ChannelSftpModes mode)
        {
            try
            {
                dst = remoteAbsolutePath(dst);
                List<string> v = glob_remote(dst);
                int vsize = v.Count;
                if (vsize != 1)
                {
                    if (vsize == 0)
                    {
                        if (isPattern(dst))
                            throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, dst);
                        else
                            dst = Util.Unquote(dst);
                    }
                    throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, v.ToString());
                }
                else
                    dst = v[0];

                if (isRemoteDir(dst))
                    throw new SftpException(ChannelSftpResult.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(ChannelSftpResult.SSH_FX_FAILURE, e.ToString());
            }
        }
Exemple #6
0
 public void put(InputStream src, string dst, SftpProgressMonitor monitor)
 {
     put(src, dst, monitor, ChannelSftpModes.OVERWRITE);
 }
Exemple #7
0
        public void put(string src, string dst, SftpProgressMonitor monitor, ChannelSftpModes mode)
        {
            src = localAbsolutePath(src);
            dst = remoteAbsolutePath(dst);

            try
            {
                List<string> v = glob_remote(dst);
                int vsize = v.Count;
                if (vsize != 1)
                {
                    if (vsize == 0)
                    {
                        if (isPattern(dst))
                            throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, dst);
                        else
                            dst = Util.Unquote(dst);
                    }
                    throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, v.ToString());
                }
                else
                    dst = 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(ChannelSftpResult.SSH_FX_FAILURE, "Copying multiple files, but destination is missing or a file.");

                for (int j = 0; j < vsize; j++)
                {
                    string _src = v[j];
                    string _dst = null;
                    if (_isRemoteDir)
                    {
                        int i = _src.LastIndexOf(m_file_separator_char);
                        if (i == -1)
                            dstsb.Append(_src);
                        else
                            dstsb.Append(_src.Substring(i + 1));
                        _dst = dstsb.ToString();
                        dstsb.Remove(dst.Length, _dst.Length - dst.Length);
                    }
                    else
                        _dst = dst;

                    long size_of_dst = 0;
                    if (mode == ChannelSftpModes.RESUME)
                    {
                        try
                        {
                            SftpATTRS attr = execStat(_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(ChannelSftpResult.SSH_FX_FAILURE, "failed to resume for " + _dst);
                        if (size_of_src == size_of_dst)
                            return;
                    }

                    if (monitor != null)
                    {
                        monitor.Init(SftpProgressMonitor.SfrpOperation.PUT, _src, _dst,
                                     (new File(_src)).Length);
                        if (mode == ChannelSftpModes.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(ChannelSftpResult.SSH_FX_FAILURE, e.ToString());
            }
        }
 public runnable2(ChannelSftp channel, PipedOutputStream pos, String _src, SftpProgressMonitor monitor, int mode, Exception[] closed)
 {
     this.channel= channel;
     this.pos=pos;
     this._src=_src;
     this.monitor=monitor;
     this.mode=mode;
     this.closed=closed;
 }
Exemple #9
0
            internal OutputStreamPut(ChannelSftp sftp,
				byte[] handle,
				long[] _offset,
				SftpProgressMonitor monitor)
                : base()
            {
                this.sftp=sftp;
                this.handle=handle;
                this._offset=_offset;
                this.monitor=monitor;
            }
Exemple #10
0
 public void get(string src, OutputStream dst, SftpProgressMonitor monitor)
 {
     get(src, dst, monitor, ChannelSftpModes.OVERWRITE, 0);
 }
Exemple #11
0
        public void get(string src, string dst, SftpProgressMonitor monitor, ChannelSftpModes mode)
        {
            src = remoteAbsolutePath(src);
            dst = localAbsolutePath(dst);

            try
            {
                List<string> v = glob_remote(src);
                int vsize = v.Count;
                if (vsize == 0)
                    throw new SftpException(ChannelSftpResult.SSH_FX_NO_SUCH_FILE, "No such file");

                File dstFile = new File(dst);
                bool isDstDir = dstFile.IsDirectory;
                StringBuilder dstsb = null;
                if (isDstDir)
                {
                    if (!dst.EndsWith(m_file_separator))
                        dst += m_file_separator;
                    dstsb = new StringBuilder(dst);
                }
                else if (vsize > 1)
                    throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, "Copying multiple files, but destination is missing or a file.");

                for (int j = 0; j < vsize; j++)
                {
                    string _src = v[j];

                    SftpATTRS attr = execStat(_src);
                    if (attr.isDir())
                        throw new SftpException(ChannelSftpResult.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.Remove(dst.Length, _dst.Length - dst.Length);
                    }
                    else
                        _dst = dst;

                    if (mode == ChannelSftpModes.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(ChannelSftpResult.SSH_FX_FAILURE, "failed to resume for " + _dst);
                        if (size_of_dst == size_of_src)
                            return;
                    }

                    if (monitor != null)
                    {
                        monitor.Init(SftpProgressMonitor.SfrpOperation.GET, _src, _dst, attr.getSize());
                        if (mode == ChannelSftpModes.RESUME)
                            monitor.Count(new File(_dst).Length);
                    }

                    FileOutputStream fos = null;
                    if (mode == ChannelSftpModes.OVERWRITE)
                        fos = new FileOutputStream(_dst);
                    else
                        fos = new FileOutputStream(_dst, true); // append

                    execGet(_src, fos, monitor, mode, new File(_dst).Length);
                    fos.close();
                }
            }
            catch (Exception e)
            {
                if (e is SftpException) throw (SftpException)e;
                throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, "");
            }
        }
Exemple #12
0
 public void get(string src, string dst, SftpProgressMonitor monitor)
 {
     get(src, dst, monitor, ChannelSftpModes.OVERWRITE);
 }
Exemple #13
0
 public InputStreamGet(ChannelSftp sftp, byte[] handle, SftpProgressMonitor monitor)
 {
     m_sftp = sftp;
     m_handle = handle;
     m_monitor = monitor;
 }
Exemple #14
0
        private void _put(InputStream src, string dst, SftpProgressMonitor monitor, ChannelSftpModes mode)
        {
            try
            {
                long skip = 0;
                if (mode == ChannelSftpModes.RESUME || mode == ChannelSftpModes.APPEND)
                {
                    try
                    {
                        SftpATTRS attr = execStat(dst);
                        skip = attr.getSize();
                    }
                    catch (Exception)
                    { }
                }
                if (mode == ChannelSftpModes.RESUME && skip > 0)
                {
                    long skipped = src.Skip(skip);
                    if (skipped < skip)
                        throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, "failed to resume for " + dst);
                }
                if (mode == ChannelSftpModes.OVERWRITE)
                    sendOPENW(Util.getBytesUTF8(dst));
                else
                    sendOPENA(Util.getBytesUTF8(dst));

                Header header = new Header();
                header = fillHeader(m_buffer, header);
                int length = header.Length;
                int type = header.HeaderType;
                m_buffer.rewind();
                fill(m_buffer.m_buffer, 0, length);

                if (type != SSH_FXP_STATUS && type != SSH_FXP_HANDLE)
                    throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, "invalid type=" + type);

                if (type == SSH_FXP_STATUS)
                {
                    int i = m_buffer.getInt();
                    throwStatusError(m_buffer, i);
                }
                byte[] handle = m_buffer.getString();	// filename
                byte[] data = null;

                bool dontcopy = true;

                if (!dontcopy)
                {
                    data = new byte[m_buffer.m_buffer.Length
                                    - (5 + 13 + 21 + handle.Length
                                    + 32 + 20 // padding and mac
                                    )
                        ];
                }

                long offset = 0;
                if (mode == ChannelSftpModes.RESUME || mode == ChannelSftpModes.APPEND)
                    offset += skip;

                int startid = m_seq;
                int _ackid = m_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 = m_buffer.m_buffer;
                        s = 5 + 13 + 21 + handle.Length;
                        datalen = m_buffer.m_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 ((m_seq - 1) == startid ||
                           m_io.m_ins.Available() >= 1024)
                        {
                            while (m_io.m_ins.Available() > 0)
                            {
                                if (checkStatus(m_ackid, header))
                                {
                                    _ackid = m_ackid[0];
                                    if (startid > _ackid || _ackid > m_seq - 1)
                                    {
                                        if (_ackid != m_seq)
                                            throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, "ack error: startid=" + startid + " seq=" + m_seq + " _ackid=" + _ackid);
                                    }
                                    ackcount++;
                                }
                                else
                                    break;
                            }
                        }
                    }
                    offset += count;
                    if (monitor != null && !monitor.Count(count))
                    {
                        break;
                    }
                }
                int _ackcount = m_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;
                throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, e.ToString());
            }
        }
Exemple #15
0
        private void execGet(string src, OutputStream dst, SftpProgressMonitor monitor, ChannelSftpModes mode, long skip)
        {
            try
            {
                sendOPENR(Util.getBytesUTF8(src));

                Header header = fillHeader(m_buffer, new Header());
                int length = header.Length;
                int type = header.HeaderType;

                m_buffer.rewind();

                fill(m_buffer.m_buffer, 0, length);

                if (type != SSH_FXP_STATUS && type != SSH_FXP_HANDLE)
                    throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, "Type is " + type);

                if (type == SSH_FXP_STATUS)
                {
                    int i = m_buffer.getInt();
                    throwStatusError(m_buffer, i);
                }

                byte[] handle = m_buffer.getString();         // filename

                long offset = 0;
                if (mode == ChannelSftpModes.RESUME)
                    offset += skip;

                int request_len = 0;

                while (true)
                {

                    request_len = m_buffer.m_buffer.Length - 13;
                    if (m_server_version == 0) { request_len = 1024; }
                    sendREAD(handle, offset, request_len);

                    header = fillHeader(m_buffer, header);
                    length = header.Length;
                    type = header.HeaderType;

                    int i;
                    if (type == SSH_FXP_STATUS)
                    {
                        m_buffer.rewind();
                        fill(m_buffer.m_buffer, 0, length);
                        i = m_buffer.getInt();
                        if (i == (int)ChannelSftpResult.SSH_FX_EOF)
                            goto BREAK;

                        throwStatusError(m_buffer, i);
                    }

                    if (type != SSH_FXP_DATA)
                        goto BREAK;

                    m_buffer.rewind();
                    fill(m_buffer.m_buffer, 0, 4); length -= 4;
                    i = m_buffer.getInt();   // length of data
                    int foo = i;
                    while (foo > 0)
                    {
                        int bar = foo;
                        if (bar > m_buffer.m_buffer.Length)
                        {
                            bar = m_buffer.m_buffer.Length;
                        }
                        i = m_io.m_ins.Read(m_buffer.m_buffer, 0, bar);
                        if (i < 0)
                        {
                            goto BREAK;
                        }
                        int data_len = i;
                        dst.write(m_buffer.m_buffer, 0, data_len);

                        offset += data_len;
                        foo -= data_len;

                        if (monitor != null)
                        {
                            if (!monitor.Count(data_len))
                            {
                                while (foo > 0)
                                {
                                    i = m_io.m_ins.Read(
                                            m_buffer.m_buffer,
                                            0,
                                            (m_buffer.m_buffer.Length < foo ? m_buffer.m_buffer.Length : foo)
                                        );
                                    if (i <= 0)
                                        break;
                                    foo -= i;
                                }
                                goto BREAK;
                            }
                        }
                    }
                }
            BREAK:
                dst.flush();

                if (monitor != null) monitor.End();
                sendCLOSE(handle, header);
            }
            catch (Exception e)
            {
                if (e is SftpException)
                    throw (SftpException)e;
                throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, "");
            }
        }
Exemple #16
0
        public OutputStream put(string dst, SftpProgressMonitor monitor, ChannelSftpModes mode, long offset)
        {
            dst = remoteAbsolutePath(dst);
            try
            {
                List<string> list = glob_remote(dst);
                if (list.Count != 1)
                    throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, list.ToString());
                dst = list[0];
                if (isRemoteDir(dst))
                    throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, dst + " is a directory");

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

                if (mode == ChannelSftpModes.OVERWRITE)
                    sendOPENW(Util.getBytesUTF8(dst));
                else
                    sendOPENA(Util.getBytesUTF8(dst));

                Header header = new Header();
                header = fillHeader(m_buffer, header);
                int length = header.Length;
                int type = header.HeaderType;

                m_buffer.rewind();
                fill(m_buffer.m_buffer, 0, length);

                if (type != SSH_FXP_STATUS && type != SSH_FXP_HANDLE)
                    throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, "");

                if (type == SSH_FXP_STATUS)
                {
                    int i = m_buffer.getInt();
                    throwStatusError(m_buffer, i);
                }

                byte[] handle = m_buffer.getString();         // filename

                if (mode == ChannelSftpModes.RESUME || mode == ChannelSftpModes.APPEND)
                    offset += skip;

                long[] _offset = new long[1];
                _offset[0] = offset;
                return new OutputStreamPut(this, handle, _offset, monitor);
            }
            catch (Exception e)
            {
                if (e is SftpException)
                    throw (SftpException)e;
                throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, "");
            }
        }
Exemple #17
0
            public InputStreamGet( 
				ChannelSftp sftp,
				byte[] handle,
				long[] _offset,
				int[] _server_version,
				SftpProgressMonitor monitor)
            {
                this.sftp=sftp;
                this.handle=handle;
                this._offset=_offset;
                this._server_version=_server_version;
                this.monitor=monitor;
            }
Exemple #18
0
        public void get(string src, OutputStream dst, SftpProgressMonitor monitor, ChannelSftpModes mode, long skip)
        {
            try
            {
                src = remoteAbsolutePath(src);
                List<string> v = glob_remote(src);
                if (v.Count != 1)
                {
                    throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, v.ToString());
                }
                src = v[0];

                if (monitor != null)
                {
                    SftpATTRS attr = execStat(src);
                    monitor.Init(SftpProgressMonitor.SfrpOperation.GET, src, "??", attr.getSize());
                    if (mode == ChannelSftpModes.RESUME)
                        monitor.Count(skip);
                }
                execGet(src, dst, monitor, mode, skip);
            }
            catch (Exception e)
            {
                if (e is SftpException)
                    throw (SftpException)e;
                throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, "");
            }
        }
Exemple #19
0
 public InputStream Get(string src, SftpProgressMonitor monitor)
 {
     return Get(src, monitor, ChannelSftpModes.OVERWRITE);
 }
Exemple #20
0
        public InputStream Get(string src, SftpProgressMonitor monitor, ChannelSftpModes mode)
        {
            if (mode == ChannelSftpModes.RESUME)
                throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, "faile to resume from " + src);

            try
            {
                src = remoteAbsolutePath(src);
                List<string> v = glob_remote(src);
                if (v.Count != 1)
                    throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, v.ToString());

                src = v[0];
                SftpATTRS attr = execStat(src);

                if (monitor != null)
                    monitor.Init(SftpProgressMonitor.SfrpOperation.GET, src, "??", attr.getSize());

                sendOPENR(Util.getBytesUTF8(src));

                Header _header = new Header();
                _header = fillHeader(m_buffer, _header);
                int length = _header.Length;
                int type = _header.HeaderType;
                m_buffer.rewind();
                fill(m_buffer.m_buffer, 0, length);

                if (type != SSH_FXP_STATUS && type != SSH_FXP_HANDLE)
                {
                    throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, "");
                }
                if (type == SSH_FXP_STATUS)
                {
                    int i = m_buffer.getInt();
                    throwStatusError(m_buffer, i);
                }

                byte[] handle = m_buffer.getString();         // filename
                return new InputStreamGet(this, handle, monitor);
            }
            catch (Exception e)
            {
                if (e is SftpException) throw (SftpException)e;
                throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, "");
            }
        }
 public InputStreamGet( 
     ChannelSftp sftp,
     byte[] handle,
     SftpProgressMonitor monitor)
 {
     this.sftp=sftp;
     this.handle=handle;
     this.monitor=monitor;
 }
 public runnable1(ChannelSftp channel, PipedInputStream pis, String _dst, SftpProgressMonitor monitor, int mode)
 {
     this.channel= channel;
     this.pis=pis;
     this._dst=_dst;
     this.monitor=monitor;
     this.mode=mode;
 }