src/Pure/General/ssh.scala
author wenzelm
Sat, 14 Mar 2020 13:44:52 +0100
changeset 71549 319599ba28cf
parent 71383 8313dca6dee9
child 71562 794c8b0ad8f1
permissions -rw-r--r--
more robust hg_url; clarified signature;
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
71358
ec48da635e6c unused;
wenzelm
parents: 71307
diff changeset
    12
import scala.collection.mutable
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
    13
64222
184e3a932778 clarified signature;
wenzelm
parents: 64191
diff changeset
    14
import com.jcraft.jsch.{JSch, Logger => JSch_Logger, Session => JSch_Session, SftpException,
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
{
64185
wenzelm
parents: 64166
diff changeset
    20
  /* target machine: user@host syntax */
64141
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    21
64185
wenzelm
parents: 64166
diff changeset
    22
  object Target
64141
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    23
  {
71307
wenzelm
parents: 69427
diff changeset
    24
    val User_Host = "^([^@]+)@(.+)$".r
64141
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    25
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    26
    def parse(s: String): (String, String) =
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    27
      s match {
71307
wenzelm
parents: 69427
diff changeset
    28
        case User_Host(user, host) => (user, host)
64141
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    29
        case _ => ("", s)
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    30
      }
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    31
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    32
    def unapplySeq(s: String): Option[List[String]] =
64185
wenzelm
parents: 64166
diff changeset
    33
      parse(s) match {
wenzelm
parents: 64166
diff changeset
    34
        case (_, "") => None
wenzelm
parents: 64166
diff changeset
    35
        case (user, host) => Some(List(user, host))
wenzelm
parents: 64166
diff changeset
    36
      }
64141
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    37
  }
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    38
64142
954451356017 proper type for Library.using;
wenzelm
parents: 64141
diff changeset
    39
  val default_port = 22
67745
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
    40
  def make_port(port: Int): Int = if (port > 0) port else default_port
64142
954451356017 proper type for Library.using;
wenzelm
parents: 64141
diff changeset
    41
71549
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
    42
  def port_suffix(port: Int): String =
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
    43
    if (port == default_port) "" else ":" + port
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
    44
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
    45
  def user_prefix(user: String): String =
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
    46
    proper_string(user) match {
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
    47
      case None => ""
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
    48
      case Some(name) => name + "@"
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
    49
    }
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
    50
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    51
  def connect_timeout(options: Options): Int =
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    52
    options.seconds("ssh_connect_timeout").ms.toInt
64141
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    53
64325
47e03cb99274 prevent sporadic disconnection;
wenzelm
parents: 64306
diff changeset
    54
  def alive_interval(options: Options): Int =
47e03cb99274 prevent sporadic disconnection;
wenzelm
parents: 64306
diff changeset
    55
    options.seconds("ssh_alive_interval").ms.toInt
47e03cb99274 prevent sporadic disconnection;
wenzelm
parents: 64306
diff changeset
    56
67273
c573cfb2c407 more robust connection: prefer ServerAliveCountMax=3 (ssh default) instead of 1 (jsch default);
wenzelm
parents: 67067
diff changeset
    57
  def alive_count_max(options: Options): Int =
c573cfb2c407 more robust connection: prefer ServerAliveCountMax=3 (ssh default) instead of 1 (jsch default);
wenzelm
parents: 67067
diff changeset
    58
    options.int("ssh_alive_count_max")
c573cfb2c407 more robust connection: prefer ServerAliveCountMax=3 (ssh default) instead of 1 (jsch default);
wenzelm
parents: 67067
diff changeset
    59
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    60
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    61
  /* init context */
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    62
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    63
  def init_context(options: Options): Context =
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    64
  {
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    65
    val config_dir = Path.explode(options.string("ssh_config_dir"))
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    66
    if (!config_dir.is_dir) error("Bad ssh config directory: " + config_dir)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    67
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    68
    val jsch = new JSch
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    69
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    70
    val config_file = Path.explode(options.string("ssh_config_file"))
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    71
    if (config_file.is_file)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    72
      jsch.setConfigRepository(OpenSSHConfig.parseFile(File.platform_path(config_file)))
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    73
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    74
    val known_hosts = config_dir + Path.explode("known_hosts")
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    75
    if (!known_hosts.is_file) known_hosts.file.createNewFile
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    76
    jsch.setKnownHosts(File.platform_path(known_hosts))
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    77
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
    78
    val identity_files =
66923
wenzelm
parents: 66570
diff changeset
    79
      space_explode(':', options.string("ssh_identity_files")).map(Path.explode(_))
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    80
    for (identity_file <- identity_files if identity_file.is_file)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    81
      jsch.addIdentity(File.platform_path(identity_file))
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    82
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    83
    new Context(options, jsch)
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    84
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    85
67745
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
    86
  def open_session(options: Options, host: String, user: String = "", port: Int = 0,
67771
3b91c21dcb00 tuned signature;
wenzelm
parents: 67770
diff changeset
    87
      proxy_host: String = "", proxy_user: String = "", proxy_port: Int = 0,
3b91c21dcb00 tuned signature;
wenzelm
parents: 67770
diff changeset
    88
      permissive: Boolean = false): Session =
67745
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
    89
    init_context(options).open_session(host = host, user = user, port = port,
67771
3b91c21dcb00 tuned signature;
wenzelm
parents: 67770
diff changeset
    90
      proxy_host = proxy_host, proxy_user = proxy_user, proxy_port = proxy_port,
3b91c21dcb00 tuned signature;
wenzelm
parents: 67770
diff changeset
    91
      permissive = permissive)
67067
02729ced9b1e tuned signature;
wenzelm
parents: 67066
diff changeset
    92
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    93
  class Context private[SSH](val options: Options, val jsch: JSch)
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    94
  {
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    95
    def update_options(new_options: Options): Context = new Context(new_options, jsch)
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
    96
71549
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
    97
    private def connect_session(host: String, user: String = "", port: Int = 0,
67770
25f3a278df3d support for permissive connections, for odd situations where host keys are not accepted;
wenzelm
parents: 67745
diff changeset
    98
      host_key_permissive: Boolean = false,
71549
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
    99
      nominal_host: String = "",
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   100
      nominal_user: String = "",
67770
25f3a278df3d support for permissive connections, for odd situations where host keys are not accepted;
wenzelm
parents: 67745
diff changeset
   101
      on_close: () => Unit = () => ()): Session =
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
   102
    {
71383
8313dca6dee9 misc tuning, following hint by IntelliJ;
wenzelm
parents: 71358
diff changeset
   103
      val session = jsch.getSession(proper_string(user).orNull, host, make_port(port))
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
   104
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
   105
      session.setUserInfo(No_User_Info)
64325
47e03cb99274 prevent sporadic disconnection;
wenzelm
parents: 64306
diff changeset
   106
      session.setServerAliveInterval(alive_interval(options))
67273
c573cfb2c407 more robust connection: prefer ServerAliveCountMax=3 (ssh default) instead of 1 (jsch default);
wenzelm
parents: 67067
diff changeset
   107
      session.setServerAliveCountMax(alive_count_max(options))
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
   108
      session.setConfig("MaxAuthTries", "3")
67770
25f3a278df3d support for permissive connections, for odd situations where host keys are not accepted;
wenzelm
parents: 67745
diff changeset
   109
      if (host_key_permissive) session.setConfig("StrictHostKeyChecking", "no")
71549
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   110
      if (nominal_host != "") session.setHostKeyAlias(nominal_host)
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
   111
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
   112
      if (options.bool("ssh_compression")) {
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
   113
        session.setConfig("compression.s2c", "zlib@openssh.com,zlib,none")
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
   114
        session.setConfig("compression.c2s", "zlib@openssh.com,zlib,none")
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
   115
        session.setConfig("compression_level", "9")
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
   116
      }
67745
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   117
      session.connect(connect_timeout(options))
71549
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   118
      new Session(options, session, on_close,
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   119
        proper_string(nominal_host) getOrElse host,
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   120
        proper_string(nominal_user) getOrElse user)
67745
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   121
    }
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
   122
67745
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   123
    def open_session(host: String, user: String = "", port: Int = 0,
67770
25f3a278df3d support for permissive connections, for odd situations where host keys are not accepted;
wenzelm
parents: 67745
diff changeset
   124
      proxy_host: String = "", proxy_user: String = "", proxy_port: Int = 0,
25f3a278df3d support for permissive connections, for odd situations where host keys are not accepted;
wenzelm
parents: 67745
diff changeset
   125
      permissive: Boolean = false): Session =
67745
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   126
    {
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   127
      if (proxy_host == "") connect_session(host = host, user = user, port = port)
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   128
      else {
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   129
        val proxy = connect_session(host = proxy_host, port = proxy_port, user = proxy_user)
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   130
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   131
        val fw =
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   132
          try { proxy.port_forwarding(remote_host = host, remote_port = make_port(port)) }
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   133
          catch { case exn: Throwable => proxy.close; throw exn }
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   134
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   135
        try {
67770
25f3a278df3d support for permissive connections, for odd situations where host keys are not accepted;
wenzelm
parents: 67745
diff changeset
   136
          connect_session(host = fw.local_host, port = fw.local_port,
71549
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   137
            host_key_permissive = permissive,
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   138
            nominal_host = host, nominal_user = user, user = user,
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   139
            on_close = () => { fw.close; proxy.close })
67745
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   140
        }
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   141
        catch { case exn: Throwable => fw.close; proxy.close; throw exn }
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   142
      }
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
   143
    }
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
   144
  }
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
   145
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   146
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   147
  /* logging */
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   148
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   149
  def logging(verbose: Boolean = true, debug: Boolean = false)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   150
  {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   151
    JSch.setLogger(if (verbose) new Logger(debug) else null)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   152
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   153
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   154
  private class Logger(debug: Boolean) extends JSch_Logger
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   155
  {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   156
    def isEnabled(level: Int): Boolean = level != JSch_Logger.DEBUG || debug
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   157
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   158
    def log(level: Int, msg: String)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   159
    {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   160
      level match {
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   161
        case JSch_Logger.ERROR | JSch_Logger.FATAL => Output.error_message(msg)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   162
        case JSch_Logger.WARN => Output.warning(msg)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   163
        case _ => Output.writeln(msg)
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   164
      }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   165
    }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   166
  }
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   167
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   168
64128
wenzelm
parents: 64127
diff changeset
   169
  /* user info */
wenzelm
parents: 64127
diff changeset
   170
wenzelm
parents: 64127
diff changeset
   171
  object No_User_Info extends UserInfo
wenzelm
parents: 64127
diff changeset
   172
  {
wenzelm
parents: 64127
diff changeset
   173
    def getPassphrase: String = null
wenzelm
parents: 64127
diff changeset
   174
    def getPassword: String = null
wenzelm
parents: 64127
diff changeset
   175
    def promptPassword(msg: String): Boolean = false
wenzelm
parents: 64127
diff changeset
   176
    def promptPassphrase(msg: String): Boolean = false
wenzelm
parents: 64127
diff changeset
   177
    def promptYesNo(msg: String): Boolean = false
wenzelm
parents: 64127
diff changeset
   178
    def showMessage(msg: String): Unit = Output.writeln(msg)
wenzelm
parents: 64127
diff changeset
   179
  }
wenzelm
parents: 64127
diff changeset
   180
wenzelm
parents: 64127
diff changeset
   181
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   182
  /* port forwarding */
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   183
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   184
  object Port_Forwarding
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   185
  {
65636
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   186
    def open(ssh: Session, ssh_close: Boolean,
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   187
      local_host: String, local_port: Int, remote_host: String, remote_port: Int): Port_Forwarding =
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   188
    {
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   189
      val port = ssh.session.setPortForwardingL(local_host, local_port, remote_host, remote_port)
65636
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   190
      new Port_Forwarding(ssh, ssh_close, local_host, port, remote_host, remote_port)
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   191
    }
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   192
  }
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   193
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   194
  class Port_Forwarding private[SSH](
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   195
    ssh: SSH.Session,
65636
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   196
    ssh_close: Boolean,
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   197
    val local_host: String,
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   198
    val local_port: Int,
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   199
    val remote_host: String,
69393
ed0824ef337e static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents: 69303
diff changeset
   200
    val remote_port: Int) extends AutoCloseable
65010
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   201
  {
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   202
    override def toString: String =
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   203
      local_host + ":" + local_port + ":" + remote_host + ":" + remote_port
a27e9908dcf7 clarified signature;
wenzelm
parents: 65009
diff changeset
   204
65636
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   205
    def close()
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   206
    {
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   207
      ssh.session.delPortForwardingL(local_host, local_port)
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   208
      if (ssh_close) ssh.close()
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   209
    }
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   210
  }
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   211
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   212
64191
wenzelm
parents: 64190
diff changeset
   213
  /* Sftp channel */
wenzelm
parents: 64190
diff changeset
   214
wenzelm
parents: 64190
diff changeset
   215
  type Attrs = SftpATTRS
wenzelm
parents: 64190
diff changeset
   216
69302
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   217
  sealed case class Dir_Entry(name: String, is_dir: Boolean)
64191
wenzelm
parents: 64190
diff changeset
   218
  {
69300
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 67771
diff changeset
   219
    def is_file: Boolean = !is_dir
64191
wenzelm
parents: 64190
diff changeset
   220
  }
wenzelm
parents: 64190
diff changeset
   221
wenzelm
parents: 64190
diff changeset
   222
64132
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   223
  /* exec channel */
c2594513687b more Sftp operations;
wenzelm
parents: 64131
diff changeset
   224
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   225
  private val exec_wait_delay = Time.seconds(0.3)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   226
69393
ed0824ef337e static type for Library.using: avoid Java 11 warnings on "Illegal reflective access";
wenzelm
parents: 69303
diff changeset
   227
  class Exec private[SSH](session: Session, channel: ChannelExec) extends AutoCloseable
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   228
  {
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   229
    override def toString: String = "exec " + session.toString
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   230
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   231
    def close() { channel.disconnect }
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   232
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   233
    val exit_status: Future[Int] =
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   234
      Future.thread("ssh_wait") {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   235
        while (!channel.isClosed) Thread.sleep(exec_wait_delay.ms)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   236
        channel.getExitStatus
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   237
      }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   238
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   239
    val stdin: OutputStream = channel.getOutputStream
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   240
    val stdout: InputStream = channel.getInputStream
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   241
    val stderr: InputStream = channel.getErrStream
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   242
64166
44925cf6ded1 tuned signature;
wenzelm
parents: 64142
diff changeset
   243
    // connect after preparing streams
44925cf6ded1 tuned signature;
wenzelm
parents: 64142
diff changeset
   244
    channel.connect(connect_timeout(session.options))
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   245
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   246
    def result(
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   247
      progress_stdout: String => Unit = (_: String) => (),
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   248
      progress_stderr: String => Unit = (_: String) => (),
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   249
      strict: Boolean = true): Process_Result =
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   250
    {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   251
      stdin.close
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   252
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   253
      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
   254
      {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   255
        val result = new mutable.ListBuffer[String]
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   256
        val line_buffer = new ByteArrayOutputStream(100)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   257
        def line_flush()
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   258
        {
64326
ff3c383b9163 extra trim_line for the sake of Windows;
wenzelm
parents: 64325
diff changeset
   259
          val line = Library.trim_line(line_buffer.toString(UTF8.charset_name))
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   260
          progress(line)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   261
          result += line
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   262
          line_buffer.reset
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   263
        }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   264
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   265
        var c = 0
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   266
        var finished = false
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   267
        while (!finished) {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   268
          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
   269
          if (c == 10) line_flush()
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   270
          else if (channel.isClosed) {
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   271
            if (line_buffer.size > 0) line_flush()
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   272
            finished = true
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   273
          }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   274
          else Thread.sleep(exec_wait_delay.ms)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   275
        }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   276
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   277
        result.toList
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   278
      }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   279
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   280
      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
   281
      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
   282
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   283
      def terminate()
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   284
      {
64136
7c5191489457 close more thoroughly;
wenzelm
parents: 64135
diff changeset
   285
        close
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   286
        out_lines.join
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   287
        err_lines.join
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   288
        exit_status.join
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   289
      }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   290
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   291
      val rc =
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   292
        try { exit_status.join }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   293
        catch { case Exn.Interrupt() => terminate(); Exn.Interrupt.return_code }
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   294
64136
7c5191489457 close more thoroughly;
wenzelm
parents: 64135
diff changeset
   295
      close
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   296
      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
   297
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   298
      Process_Result(rc, out_lines.join, err_lines.join)
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   299
    }
64131
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   300
  }
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   301
f01fca58e0a5 more specific channels;
wenzelm
parents: 64130
diff changeset
   302
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   303
  /* session */
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   304
67745
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   305
  class Session private[SSH](
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   306
    val options: Options,
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   307
    val session: JSch_Session,
71549
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   308
    on_close: () => Unit,
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   309
    val nominal_host: String,
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   310
    val nominal_user: String) extends System with AutoCloseable
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   311
  {
67745
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   312
    def update_options(new_options: Options): Session =
71549
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   313
      new Session(new_options, session, on_close, nominal_host, nominal_user)
64166
44925cf6ded1 tuned signature;
wenzelm
parents: 64142
diff changeset
   314
64347
602483aa7818 support for URL notation;
wenzelm
parents: 64326
diff changeset
   315
    def host: String = if (session.getHost == null) "" else session.getHost
71549
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   316
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   317
    override def hg_url: String =
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   318
      "ssh://" + user_prefix(nominal_user) + nominal_host + "/"
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   319
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   320
    override def prefix: String =
71549
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   321
      user_prefix(session.getUserName) + host + port_suffix(session.getPort) + ":"
64347
602483aa7818 support for URL notation;
wenzelm
parents: 64326
diff changeset
   322
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   323
    override def toString: String =
71549
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   324
      user_prefix(session.getUserName) + host + port_suffix(session.getPort) +
319599ba28cf more robust hg_url;
wenzelm
parents: 71383
diff changeset
   325
      (if (session.isConnected) "" else " (disconnected)")
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   326
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   327
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   328
    /* port forwarding */
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   329
65636
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   330
    def port_forwarding(
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   331
        remote_port: Int, remote_host: String = "localhost",
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   332
        local_port: Int = 0, local_host: String = "localhost",
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   333
        ssh_close: Boolean = false): Port_Forwarding =
df804cdba5f9 ssh_close for proper termination after use of database;
wenzelm
parents: 65594
diff changeset
   334
      Port_Forwarding.open(this, ssh_close, local_host, local_port, remote_host, remote_port)
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   335
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   336
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   337
    /* sftp channel */
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   338
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   339
    val sftp: ChannelSftp = session.openChannel("sftp").asInstanceOf[ChannelSftp]
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   340
    sftp.connect(connect_timeout(options))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   341
67745
d83efbe52438 support for proxy connection, similar to ProxyCommand in ssh config;
wenzelm
parents: 67273
diff changeset
   342
    def close() { sftp.disconnect; session.disconnect; on_close() }
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   343
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   344
    val settings: Map[String, String] =
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   345
    {
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   346
      val home = sftp.getHome
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   347
      Map("HOME" -> home, "USER_HOME" -> home)
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   348
    }
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   349
    override def expand_path(path: Path): Path = path.expand_env(settings)
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   350
    def remote_path(path: Path): String = expand_path(path).implode
67066
1645cef7a49c proper ssh.bash_path;
wenzelm
parents: 66923
diff changeset
   351
    override def bash_path(path: Path): String = Bash.string(remote_path(path))
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   352
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   353
    def chmod(permissions: Int, path: Path): Unit = sftp.chmod(permissions, remote_path(path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   354
    def mv(path1: Path, path2: Path): Unit = sftp.rename(remote_path(path1), remote_path(path2))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   355
    def rm(path: Path): Unit = sftp.rm(remote_path(path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   356
    def mkdir(path: Path): Unit = sftp.mkdir(remote_path(path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   357
    def rmdir(path: Path): Unit = sftp.rmdir(remote_path(path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   358
69302
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   359
    private def test_entry(path: Path, as_dir: Boolean): Boolean =
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   360
      try {
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   361
        val is_dir = sftp.stat(remote_path(path)).isDir
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   362
        if (as_dir) is_dir else !is_dir
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   363
      }
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   364
      catch { case _: SftpException => false }
69300
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 67771
diff changeset
   365
69302
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   366
    override def is_dir(path: Path): Boolean = test_entry(path, true)
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   367
    override def is_file(path: Path): Boolean = test_entry(path, false)
69300
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 67771
diff changeset
   368
69301
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   369
    def is_link(path: Path): Boolean =
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   370
      try { sftp.lstat(remote_path(path)).isLink }
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   371
      catch { case _: SftpException => false }
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   372
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   373
    override def mkdirs(path: Path): Unit =
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   374
      if (!is_dir(path)) {
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   375
        execute(
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   376
          "perl -e \"use File::Path make_path; make_path('" + remote_path(path) + "');\"")
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   377
        if (!is_dir(path)) error("Failed to create directory: " + quote(remote_path(path)))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   378
      }
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   379
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   380
    def read_dir(path: Path): List[Dir_Entry] =
64191
wenzelm
parents: 64190
diff changeset
   381
    {
69300
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 67771
diff changeset
   382
      if (!is_dir(path)) error("No such directory: " + path.toString)
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 67771
diff changeset
   383
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 67771
diff changeset
   384
      val dir_name = remote_path(path)
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 67771
diff changeset
   385
      val dir = sftp.ls(dir_name)
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   386
      (for {
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   387
        i <- (0 until dir.size).iterator
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   388
        a = dir.get(i).asInstanceOf[AnyRef]
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   389
        name = Untyped.get[String](a, "filename")
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   390
        attrs = Untyped.get[Attrs](a, "attrs")
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   391
        if name != "." && name != ".."
69302
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   392
      }
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   393
      yield {
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   394
        Dir_Entry(name,
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   395
          if (attrs.isLink) {
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   396
            try { sftp.stat(dir_name + "/" + name).isDir }
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   397
            catch { case _: SftpException => false }
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   398
          }
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   399
          else attrs.isDir)
69427
ff2f39a221d4 clarified operations: uniform sorting of results;
wenzelm
parents: 69393
diff changeset
   400
      }).toList.sortBy(_.name)
64191
wenzelm
parents: 64190
diff changeset
   401
    }
64135
865dda40e1cc provide execute operation, similar to Isabelle_System.bash;
wenzelm
parents: 64134
diff changeset
   402
69301
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   403
    def find_files(
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   404
      start: Path,
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   405
      pred: Path => Boolean = _ => true,
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   406
      include_dirs: Boolean = false,
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   407
      follow_links: Boolean = false): List[Path] =
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   408
    {
69301
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   409
      val result = new mutable.ListBuffer[Path]
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   410
      def check(path: Path) { if (pred(path)) result += path }
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   411
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   412
      def find(dir: Path)
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   413
      {
69303
51d8b4dbc61b more uniform wrt. File.find_files;
wenzelm
parents: 69302
diff changeset
   414
        if (include_dirs) check(dir)
69301
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   415
        if (follow_links || !is_link(dir)) {
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   416
          for (entry <- read_dir(dir)) {
69302
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   417
            val path = dir + Path.basic(entry.name)
5faf57207a9b proper use of stat() vs. lstat() (for symlinks);
wenzelm
parents: 69301
diff changeset
   418
            if (entry.is_file) check(path) else find(path)
69301
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   419
          }
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   420
        }
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   421
      }
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   422
      if (is_file(start)) check(start) else find(start)
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   423
f6c17b9e1104 more uniform find_files, notably for symlinks;
wenzelm
parents: 69300
diff changeset
   424
      result.toList
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   425
    }
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   426
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   427
    def open_input(path: Path): InputStream = sftp.get(remote_path(path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   428
    def open_output(path: Path): OutputStream = sftp.put(remote_path(path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   429
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   430
    def read_file(path: Path, local_path: Path): Unit =
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   431
      sftp.get(remote_path(path), File.platform_path(local_path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   432
    def read_bytes(path: Path): Bytes = using(open_input(path))(Bytes.read_stream(_))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   433
    def read(path: Path): String = using(open_input(path))(File.read_stream(_))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   434
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   435
    def write_file(path: Path, local_path: Path): Unit =
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   436
      sftp.put(File.platform_path(local_path), remote_path(path))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   437
    def write_bytes(path: Path, bytes: Bytes): Unit =
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   438
      using(open_output(path))(bytes.write_stream(_))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   439
    def write(path: Path, text: String): Unit =
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   440
      using(open_output(path))(stream => Bytes(text).write_stream(stream))
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   441
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   442
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   443
    /* exec channel */
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   444
64166
44925cf6ded1 tuned signature;
wenzelm
parents: 64142
diff changeset
   445
    def exec(command: String): Exec =
64129
fce8b7c746b4 support for remote command execution;
wenzelm
parents: 64128
diff changeset
   446
    {
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   447
      val channel = session.openChannel("exec").asInstanceOf[ChannelExec]
64190
c62b99e3ec07 provide USER_HOME, such that symbolic Path.explode("~") can be used remotely;
wenzelm
parents: 64185
diff changeset
   448
      channel.setCommand("export USER_HOME=\"$HOME\"\n" + command)
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   449
      new Exec(this, channel)
64129
fce8b7c746b4 support for remote command execution;
wenzelm
parents: 64128
diff changeset
   450
    }
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   451
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   452
    override def execute(command: String,
64191
wenzelm
parents: 64190
diff changeset
   453
        progress_stdout: String => Unit = (_: String) => (),
wenzelm
parents: 64190
diff changeset
   454
        progress_stderr: String => Unit = (_: String) => (),
wenzelm
parents: 64190
diff changeset
   455
        strict: Boolean = true): Process_Result =
wenzelm
parents: 64190
diff changeset
   456
      exec(command).result(progress_stdout, progress_stderr, strict)
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   457
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   458
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   459
    /* tmp dirs */
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   460
64306
7b6dc1b36f20 tuned signature, in accordance to Isabelle_System;
wenzelm
parents: 64304
diff changeset
   461
    def rm_tree(dir: Path): Unit = rm_tree(remote_path(dir))
7b6dc1b36f20 tuned signature, in accordance to Isabelle_System;
wenzelm
parents: 64304
diff changeset
   462
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   463
    def rm_tree(remote_dir: String): Unit =
64304
96bc94c87a81 clarified modules;
wenzelm
parents: 64258
diff changeset
   464
      execute("rm -r -f " + Bash.string(remote_dir)).check
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   465
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   466
    def tmp_dir(): String =
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   467
      execute("mktemp -d -t tmp.XXXXXXXXXX").check.out
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   468
64233
ef6f7e8a018c clarified signature: more static types;
wenzelm
parents: 64228
diff changeset
   469
    def with_tmp_dir[A](body: Path => A): A =
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   470
    {
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   471
      val remote_dir = tmp_dir()
64233
ef6f7e8a018c clarified signature: more static types;
wenzelm
parents: 64228
diff changeset
   472
      try { body(Path.explode(remote_dir)) } finally { rm_tree(remote_dir) }
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   473
    }
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   474
  }
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   475
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   476
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   477
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   478
  /* system operations */
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   479
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   480
  trait System
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   481
  {
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   482
    def hg_url: String = ""
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   483
    def prefix: String = ""
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   484
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   485
    def expand_path(path: Path): Path = path.expand
67066
1645cef7a49c proper ssh.bash_path;
wenzelm
parents: 66923
diff changeset
   486
    def bash_path(path: Path): String = File.bash_path(path)
69300
8b6ab9989bcd is_file/is_dir/read_dir: more uniform treatment of errors and boundary cases, notably for symlinks in ssh;
wenzelm
parents: 67771
diff changeset
   487
    def is_dir(path: Path): Boolean = path.is_dir
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   488
    def is_file(path: Path): Boolean = path.is_file
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   489
    def mkdirs(path: Path): Unit = Isabelle_System.mkdirs(path)
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   490
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   491
    def execute(command: String,
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   492
        progress_stdout: String => Unit = (_: String) => (),
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   493
        progress_stderr: String => Unit = (_: String) => (),
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   494
        strict: Boolean = true): Process_Result =
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   495
      Isabelle_System.bash(command, progress_stdout = progress_stdout,
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   496
        progress_stderr = progress_stderr, strict = strict)
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   497
  }
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   498
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   499
  object Local extends System
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   500
}