src/Pure/General/ssh.scala
author wenzelm
Mon, 10 Oct 2016 21:44:54 +0200
changeset 64134 57581e4026fe
parent 64133 e8407039b572
child 64135 865dda40e1cc
permissions -rw-r--r--
proper support for exec channel (see also bash.scala);
Ignore whitespace changes - Everywhere: Within whitespace: At end of lines:
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     1
/*  Title:      Pure/General/ssh.scala
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     2
    Author:     Makarius
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     3
64124
818265654e60 tuned comment;
wenzelm
parents: 64123
diff changeset
     4
SSH client based on JSch (see also http://www.jcraft.com/jsch/examples).
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     5
*/
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     6
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     7
package isabelle
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     8
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     9
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
    10
import java.io.{InputStream, OutputStream, ByteArrayOutputStream}
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
    11
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
    12
import scala.collection.{mutable, JavaConversions}
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
    13
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    14
import com.jcraft.jsch.{JSch, Logger => JSch_Logger, Session => JSch_Session,
64132
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
    15
  OpenSSHConfig, UserInfo, Channel => JSch_Channel, ChannelExec, ChannelSftp, SftpATTRS}
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    16
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    17
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    18
object SSH
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    19
{
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    20
  /* init */
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    21
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    22
  def init(options: Options): SSH =
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    23
  {
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    24
    val config_dir = Path.explode(options.string("ssh_config_dir"))
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    25
    if (!config_dir.is_dir) error("Bad ssh config directory: " + config_dir)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    26
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    27
    val jsch = new JSch
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    28
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    29
    val config_file = Path.explode(options.string("ssh_config_file"))
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    30
    if (config_file.is_file)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    31
      jsch.setConfigRepository(OpenSSHConfig.parseFile(File.platform_path(config_file)))
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    32
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    33
    val known_hosts = config_dir + Path.explode("known_hosts")
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    34
    if (!known_hosts.is_file) known_hosts.file.createNewFile
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    35
    jsch.setKnownHosts(File.platform_path(known_hosts))
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    36
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    37
    val identity_files =
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    38
      Library.space_explode(':', options.string("ssh_identity_files")).map(Path.explode(_))
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    39
    for (identity_file <- identity_files if identity_file.is_file)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    40
      jsch.addIdentity(File.platform_path(identity_file))
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    41
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    42
    new SSH(options, jsch)
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    43
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    44
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    45
  def connect_timeout(options: Options): Int =
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    46
    options.seconds("ssh_connect_timeout").ms.toInt
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    47
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    48
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    49
  /* logging */
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    50
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    51
  def logging(verbose: Boolean = true, debug: Boolean = false)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    52
  {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    53
    JSch.setLogger(if (verbose) new Logger(debug) else null)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    54
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    55
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    56
  private class Logger(debug: Boolean) extends JSch_Logger
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    57
  {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    58
    def isEnabled(level: Int): Boolean = level != JSch_Logger.DEBUG || debug
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    59
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    60
    def log(level: Int, msg: String)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    61
    {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    62
      level match {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    63
        case JSch_Logger.ERROR | JSch_Logger.FATAL => Output.error_message(msg)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    64
        case JSch_Logger.WARN => Output.warning(msg)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    65
        case _ => Output.writeln(msg)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    66
      }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    67
    }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    68
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    69
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    70
64128
wenzelm
parents: 64127
diff changeset
    71
  /* user info */
wenzelm
parents: 64127
diff changeset
    72
wenzelm
parents: 64127
diff changeset
    73
  object No_User_Info extends UserInfo
wenzelm
parents: 64127
diff changeset
    74
  {
wenzelm
parents: 64127
diff changeset
    75
    def getPassphrase: String = null
wenzelm
parents: 64127
diff changeset
    76
    def getPassword: String = null
wenzelm
parents: 64127
diff changeset
    77
    def promptPassword(msg: String): Boolean = false
wenzelm
parents: 64127
diff changeset
    78
    def promptPassphrase(msg: String): Boolean = false
wenzelm
parents: 64127
diff changeset
    79
    def promptYesNo(msg: String): Boolean = false
wenzelm
parents: 64127
diff changeset
    80
    def showMessage(msg: String): Unit = Output.writeln(msg)
wenzelm
parents: 64127
diff changeset
    81
  }
wenzelm
parents: 64127
diff changeset
    82
wenzelm
parents: 64127
diff changeset
    83
64132
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
    84
  /* channel */
64129
fce8b7c746b4 support for remote command execution;
wenzelm
parents: 64128
diff changeset
    85
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
    86
  class Channel[C <: JSch_Channel] private[SSH](
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
    87
    val session: Session, val kind: String, val options: Options, val channel: C)
64129
fce8b7c746b4 support for remote command execution;
wenzelm
parents: 64128
diff changeset
    88
  {
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    89
    override def toString: String = kind + " " + session.toString
64129
fce8b7c746b4 support for remote command execution;
wenzelm
parents: 64128
diff changeset
    90
fce8b7c746b4 support for remote command execution;
wenzelm
parents: 64128
diff changeset
    91
    def close { channel.disconnect }
fce8b7c746b4 support for remote command execution;
wenzelm
parents: 64128
diff changeset
    92
  }
fce8b7c746b4 support for remote command execution;
wenzelm
parents: 64128
diff changeset
    93
64132
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
    94
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
    95
  /* exec channel */
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
    96
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
    97
  private val exec_wait_delay = Time.seconds(0.3)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
    98
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
    99
  class Exec private[SSH](
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   100
    session: Session, kind: String, options: Options, channel: ChannelExec)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   101
    extends Channel[ChannelExec](session, kind, options, channel)
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   102
  {
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   103
    def kill(signal: String) { channel.sendSignal(signal) }
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   104
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   105
    val exit_status: Future[Int] =
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   106
      Future.thread("ssh_wait") {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   107
        while (!channel.isClosed) Thread.sleep(exec_wait_delay.ms)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   108
        channel.getExitStatus
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   109
      }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   110
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   111
    val stdin: OutputStream = channel.getOutputStream
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   112
    val stdout: InputStream = channel.getInputStream
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   113
    val stderr: InputStream = channel.getErrStream
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   114
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   115
    // after preparing streams
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   116
    channel.connect(connect_timeout(options))
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   117
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   118
    def result(
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   119
      progress_stdout: String => Unit = (_: String) => (),
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   120
      progress_stderr: String => Unit = (_: String) => (),
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   121
      strict: Boolean = true): Process_Result =
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   122
    {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   123
      stdin.close
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   124
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   125
      def read_lines(stream: InputStream, progress: String => Unit): List[String] =
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   126
      {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   127
        val result = new mutable.ListBuffer[String]
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   128
        val line_buffer = new ByteArrayOutputStream(100)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   129
        def line_flush()
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   130
        {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   131
          val line = line_buffer.toString(UTF8.charset_name)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   132
          progress(line)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   133
          result += line
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   134
          line_buffer.reset
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   135
        }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   136
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   137
        var c = 0
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   138
        var finished = false
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   139
        while (!finished) {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   140
          while ({ c = stream.read; c != -1 && c != 10 }) line_buffer.write(c)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   141
          if (c == 10) line_flush()
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   142
          else if (channel.isClosed) {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   143
            if (line_buffer.size > 0) line_flush()
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   144
            finished = true
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   145
          }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   146
          else Thread.sleep(exec_wait_delay.ms)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   147
        }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   148
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   149
        result.toList
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   150
      }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   151
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   152
      val out_lines = Future.thread("ssh_stdout") { read_lines(stdout, progress_stdout) }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   153
      val err_lines = Future.thread("ssh_stderr") { read_lines(stderr, progress_stderr) }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   154
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   155
      def terminate()
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   156
      {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   157
        channel.disconnect
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   158
        out_lines.join
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   159
        err_lines.join
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   160
        exit_status.join
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   161
      }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   162
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   163
      val rc =
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   164
        try { exit_status.join }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   165
        catch { case Exn.Interrupt() => terminate(); Exn.Interrupt.return_code }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   166
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   167
      if (strict && rc == Exn.Interrupt.return_code) throw Exn.Interrupt()
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   168
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   169
      Process_Result(rc, out_lines.join, err_lines.join)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   170
    }
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   171
  }
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   172
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   173
64132
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   174
  /* Sftp channel */
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   175
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   176
  type Attrs = SftpATTRS
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   177
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   178
  sealed case class Dir_Entry(name: String, attrs: Attrs)
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   179
  {
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   180
    def is_file: Boolean = attrs.isReg
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   181
    def is_dir: Boolean = attrs.isDir
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   182
  }
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   183
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   184
  class Sftp private[SSH](
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   185
    session: Session, kind: String, options: Options, channel: ChannelSftp)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   186
    extends Channel[ChannelSftp](session, kind, options, channel)
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   187
  {
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   188
    channel.connect(connect_timeout(options))
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   189
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   190
    def home: String = channel.getHome()
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   191
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   192
    def chmod(permissions: Int, remote_path: String) { channel.chmod(permissions, remote_path) }
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   193
    def mv(remote_path1: String, remote_path2: String): Unit =
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   194
      channel.rename(remote_path1, remote_path2)
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   195
    def rm(remote_path: String) { channel.rm(remote_path) }
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   196
    def mkdir(remote_path: String) { channel.mkdir(remote_path) }
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   197
    def rmdir(remote_path: String) { channel.rmdir(remote_path) }
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   198
64132
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   199
    def stat(remote_path: String): Dir_Entry =
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   200
      Dir_Entry(remote_path, channel.stat(remote_path))
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   201
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   202
    def read_dir(remote_path: String): List[Dir_Entry] =
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   203
    {
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   204
      val dir = channel.ls(remote_path)
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   205
      (for {
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   206
        i <- (0 until dir.size).iterator
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   207
        a = dir.get(i).asInstanceOf[AnyRef]
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   208
        name = Untyped.get[String](a, "filename")
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   209
        attrs = Untyped.get[Attrs](a, "attrs")
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   210
        if name != "." && name != ".."
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   211
      } yield Dir_Entry(name, attrs)).toList
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   212
    }
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   213
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   214
    def find_files(remote_path: String, pred: Dir_Entry => Boolean = _ => true): List[Dir_Entry] =
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   215
    {
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   216
      def find(dir: String): List[Dir_Entry] =
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   217
        read_dir(dir).flatMap(entry =>
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   218
          {
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   219
            val file = dir + "/" + entry.name
64133
e8407039b572 proper hierarchic names;
wenzelm
parents: 64132
diff changeset
   220
            if (entry.is_dir) find(file)
e8407039b572 proper hierarchic names;
wenzelm
parents: 64132
diff changeset
   221
            else if (pred(entry)) List(entry.copy(name = file))
e8407039b572 proper hierarchic names;
wenzelm
parents: 64132
diff changeset
   222
            else Nil
64132
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   223
          })
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   224
      find(remote_path)
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   225
    }
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   226
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   227
    def open_input(remote_path: String): InputStream = channel.get(remote_path)
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   228
    def open_output(remote_path: String): OutputStream = channel.put(remote_path)
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   229
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   230
    def read_file(remote_path: String, local_path: Path): Unit =
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   231
      channel.get(remote_path, File.platform_path(local_path))
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   232
    def read_bytes(remote_path: String): Bytes =
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   233
      using(open_input(remote_path))(Bytes.read_stream(_))
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   234
    def read(remote_path: String): String =
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   235
      using(open_input(remote_path))(File.read_stream(_))
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   236
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   237
    def write_file(remote_path: String, local_path: Path): Unit =
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   238
      channel.put(File.platform_path(local_path), remote_path)
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   239
    def write_bytes(remote_path: String, bytes: Bytes): Unit =
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   240
      using(open_output(remote_path))(bytes.write_stream(_))
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   241
    def write(remote_path: String, text: String): Unit =
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   242
      using(open_output(remote_path))(stream => Bytes(text).write_stream(stream))
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   243
  }
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   244
64129
fce8b7c746b4 support for remote command execution;
wenzelm
parents: 64128
diff changeset
   245
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   246
  /* session */
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   247
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
   248
  class Session private[SSH](val session_options: Options, val session: JSch_Session)
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   249
  {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   250
    override def toString: String =
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   251
      (if (session.getUserName == null) "" else session.getUserName + "@") +
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   252
      (if (session.getHost == null) "" else session.getHost) +
64126
42bcd207598d connect session by default;
wenzelm
parents: 64125
diff changeset
   253
      (if (session.getPort == 22) "" else ":" + session.getPort) +
42bcd207598d connect session by default;
wenzelm
parents: 64125
diff changeset
   254
      (if (session.isConnected) "" else " (disconnected)")
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   255
64126
42bcd207598d connect session by default;
wenzelm
parents: 64125
diff changeset
   256
    def close { session.disconnect }
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   257
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   258
    def exec(command: String, options: Options = session_options): Exec =
64129
fce8b7c746b4 support for remote command execution;
wenzelm
parents: 64128
diff changeset
   259
    {
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
   260
      val kind = "exec"
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
   261
      val channel = session.openChannel(kind).asInstanceOf[ChannelExec]
64129
fce8b7c746b4 support for remote command execution;
wenzelm
parents: 64128
diff changeset
   262
      channel.setCommand(command)
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   263
      new Exec(this, kind, options, channel)
64129
fce8b7c746b4 support for remote command execution;
wenzelm
parents: 64128
diff changeset
   264
    }
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   265
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   266
    def sftp(options: Options = session_options): Sftp =
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
   267
    {
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
   268
      val kind = "sftp"
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
   269
      val channel = session.openChannel(kind).asInstanceOf[ChannelSftp]
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   270
      new Sftp(this, kind, options, channel)
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
   271
    }
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   272
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   273
}
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   274
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
   275
class SSH private(val options: Options, val jsch: JSch)
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   276
{
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
   277
  def open_session(host: String, port: Int = 22, user: String = null): SSH.Session =
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   278
  {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   279
    val session = jsch.getSession(user, host, port)
64125
a034dac5ca3c clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents: 64124
diff changeset
   280
a034dac5ca3c clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents: 64124
diff changeset
   281
    session.setUserInfo(SSH.No_User_Info)
a034dac5ca3c clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents: 64124
diff changeset
   282
    session.setConfig("MaxAuthTries", "3")
a034dac5ca3c clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents: 64124
diff changeset
   283
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
   284
    if (options.bool("ssh_compression")) {
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   285
      session.setConfig("compression.s2c", "zlib@openssh.com,zlib,none")
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   286
      session.setConfig("compression.c2s", "zlib@openssh.com,zlib,none")
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   287
      session.setConfig("compression_level", "9")
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   288
    }
64125
a034dac5ca3c clarified (hardwired!) default (see also jEdit/FTP);
wenzelm
parents: 64124
diff changeset
   289
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
   290
    session.connect(SSH.connect_timeout(options))
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
   291
    new SSH.Session(options, session)
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   292
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   293
}