Exemple #1
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 #2
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());
            }
        }
Exemple #3
0
        public override void start()
        {
            try
            {

                PipedOutputStream pos = new PipedOutputStream();
                m_io.setOutputStream(pos);
                PipedInputStream pis = new ChannelPipedInputStream(pos, 32 * 1024);
                m_io.setInputStream(pis);

                Request request = new RequestSftp();
                request.request(m_session, this);

                m_buffer = new Buffer(m_rmpsize);
                m_packet = new Packet(m_buffer);
                int i = 0;
                int length;
                int type;
                byte[] str;

                // send SSH_FXP_INIT
                sendINIT();

                // receive SSH_FXP_VERSION
                Header header = new Header();
                header = fillHeader(m_buffer, header);
                length = header.Length;
                if (length > MAX_MSG_LENGTH)
                    throw new SftpException(ChannelSftpResult.SSH_FX_FAILURE, "Received message is too long: " + length);

                type = header.HeaderType;             // 2 -> SSH_FXP_VERSION
                m_server_version = header.RID;
                skip(length);

                // send SSH_FXP_REALPATH
                sendREALPATH(Util.getBytesUTF8("."));

                // receive SSH_FXP_NAME
                header = fillHeader(m_buffer, header);
                length = header.Length;
                type = header.HeaderType;           // 104 -> SSH_FXP_NAME
                m_buffer.rewind();
                fill(m_buffer.m_buffer, 0, length);
                i = m_buffer.getInt();              // count
                str = m_buffer.getString();         // filename
                m_home = m_cwd = Util.getStringUTF8(str);
                str = m_buffer.getString();         // logname
                // SftpATTRS.getATTR(buf);			// attrs
                m_lcwd = new File(".").CanonicalPath;
            }
            catch (Exception e)
            {
                if (e is JSchException)
                    throw (JSchException)e;
                throw new JSchException(e.ToString());
            }
        }