示例#1
0
        public override int Read(byte[] b, int off, int len)
        {
            if (position == size)
            {
                return(0);
            }

            if (position >= size)
            {
                throw new EndOfStreamException("End of file.");
            }

            int num = (int)(size - position > len ? len : size - position);

            int r = source.Read(b, off, num);

            if (r == 0)
            {
                throw new EndOfStreamException("Unexpected EOF.");
            }

            position += num;

            if (position == size)
            {
                engine.WaitForResponse();
                engine.WriteOk();
            }

            return(r);
        }
示例#2
0
        /// <summary>
        /// Transfer the local file or directory to the remote SSH server.
        /// </summary>
        /// <remarks>
        /// When transfering a directory the entire contents are recursively copied to the remote
        /// server. If for example you copy the local directory "C:\scp" and specify the remote
        /// folder "." then a folder called scp will be created on the remote filesystem with all
        /// the contents of the local directory.
        ///
        /// When transfering files, you can either specify the remote directory or an alternative
        /// name and path for the file.
        /// </remarks>
        /// <example>
        /// [This example assumes parameter ssh is an authenticated instance of SSHClient]
        /// <code>
        /// // Create an SCPClient from an authenticated SSHClient instance
        /// SCPClient scp = new SCPClient(ssh);
        ///
        /// // Copy the contents of a local folder to a remote folder
        /// scp.Put(new FileInfo("C:\\workspace"), ".");
        ///
        /// // Copy a file from the local machime to a remote folder
        /// scp.Put(new FileInfo("C:\\workspace\\readme.txt"), ".");
        ///
        /// // Copy a file from the local machine and rename on the remote machine
        /// scp.Put(new FileInfo("C:\\workspace\\readme.txt"), "sometext.txt");
        /// </code>
        /// </example>
        /// <param name="localfile">The name of a local file or directory to copy.</param>
        /// <param name="remotefile">The location to place the copied file or directory</param>
        /// <exception cref="Maverick.SSH.SSHException"/>
        public void Put(FileInfo localfile, String remotefile)
        {
            if (!File.Exists(localfile.FullName) && !Directory.Exists(localfile.FullName))
            {
                throw new FileNotFoundException(localfile.FullName + " does not exist as either a file or directory!");
            }

            bool recursive = Directory.Exists(localfile.FullName);

            try
            {
                String cmd = "scp " +
                             (Directory.Exists(localfile.FullName) ? "-d " : "")
                             + "-t "
                             + (recursive ? "-r " : "")
                             + remotefile;
#if DEBUG
                System.Diagnostics.Trace.WriteLine("Creating SCPEngine with command: " + cmd);
#endif
                SCPEngine scp = new SCPEngine(this, cmd, ssh.OpenSessionChannel());

                try
                {
#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Waiting for response to command");
#endif
                    scp.WaitForResponse();

#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Got response, writing file " + localfile.FullName + " to " + remotefile + "");
#endif
                    scp.WriteFileToRemote(localfile, recursive);

#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Write complete");
#endif
                }
                finally
                {
                    scp.Close();
                }
            }
            catch (Exception ex)
            {
                throw ProcessException(ex);
            }
        }
示例#3
0
        /// <summary>
        /// Transfer the local file or directory to the remote SSH server.
        /// </summary>
        /// <remarks>
        /// When transfering a directory the entire contents are recursively copied to the remote
        /// server. If for example you copy the local directory "C:\scp" and specify the remote
        /// folder "." then a folder called scp will be created on the remote filesystem with all
        /// the contents of the local directory.
        ///
        /// When transfering files, you can either specify the remote directory or an alternative
        /// name and path for the file.
        /// </remarks>
        /// <example>
        /// [This example assumes parameter ssh is an authenticated instance of SSHClient]
        /// <code>
        /// // Create an SCPClient from an authenticated SSHClient instance
        /// SCPClient scp = new SCPClient(ssh);
        ///
        /// // Copy the contents of a local folder to a remote folder
        /// scp.Put(new FileInfo("C:\\workspace"), ".");
        ///
        /// // Copy a file from the local machime to a remote folder
        /// scp.Put(new FileInfo("C:\\workspace\\readme.txt"), ".");
        ///
        /// // Copy a file from the local machine and rename on the remote machine
        /// scp.Put(new FileInfo("C:\\workspace\\readme.txt"), "sometext.txt");
        /// </code>
        /// </example>
        /// <param name="stream">A stream that contains the file data; the stream must report its full length in order to be transfered correctly.</param>
        /// <param name="remotefile">The location to place the copied file or directory</param>
        /// <exception cref="Maverick.SSH.SSHException"/>
        public void Put(Stream stream, String remotefile)
        {
            try
            {
                String cmd = "scp "
                             + "-t "
                             + remotefile;
#if DEBUG
                System.Diagnostics.Trace.WriteLine("Creating SCPEngine with command: " + cmd);
#endif
                SCPEngine scp = new SCPEngine(this, cmd, ssh.OpenSessionChannel());

                try
                {
#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Waiting for response to command");
#endif
                    scp.WaitForResponse();

#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Got response, writing file " + remotefile + " to " + remotefile);
#endif
                    scp.WriteStreamToRemote(stream, stream.Length, remotefile);

#if DEBUG
                    System.Diagnostics.Trace.WriteLine("Write complete");
#endif
                }
                finally
                {
                    scp.Close();
                }
            }
            catch (Exception ex)
            {
                throw ProcessException(ex);
            }
        }