src/Pure/General/ssh.scala
author wenzelm
Thu, 28 Mar 2024 16:27:36 +0100
changeset 80055 42bc8ab751c1
parent 79635 8d2539a13502
child 80187 b8918a5a669e
permissions -rw-r--r--
clarified modules: more official Sessions.notable_groups;
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
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
     4
SSH client on OpenSSH command-line tools, preferably with connection
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
     5
multiplexing, but this does not work on Windows.
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     6
*/
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     7
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     8
package isabelle
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
     9
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    10
73909
1d0d9772fff0 tuned imports;
wenzelm
parents: 73897
diff changeset
    11
import java.util.{Map => JMap}
76164
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
    12
import java.io.{File => JFile}
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    13
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    14
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74306
diff changeset
    15
object SSH {
76170
5912209b4fb6 clarified modules;
wenzelm
parents: 76169
diff changeset
    16
  /* client command */
5912209b4fb6 clarified modules;
wenzelm
parents: 76169
diff changeset
    17
5912209b4fb6 clarified modules;
wenzelm
parents: 76169
diff changeset
    18
  def client_command(port: Int = 0, control_path: String = ""): String =
5912209b4fb6 clarified modules;
wenzelm
parents: 76169
diff changeset
    19
    if (control_path.isEmpty || control_path == Bash.string(control_path)) {
5912209b4fb6 clarified modules;
wenzelm
parents: 76169
diff changeset
    20
      "ssh" +
5912209b4fb6 clarified modules;
wenzelm
parents: 76169
diff changeset
    21
        (if (port > 0) " -p " + port else "") +
77369
wenzelm
parents: 77130
diff changeset
    22
        if_proper(control_path, " -o ControlPath=" + control_path)
76170
5912209b4fb6 clarified modules;
wenzelm
parents: 76169
diff changeset
    23
    }
5912209b4fb6 clarified modules;
wenzelm
parents: 76169
diff changeset
    24
    else error ("Malformed SSH control socket: " + quote(control_path))
5912209b4fb6 clarified modules;
wenzelm
parents: 76169
diff changeset
    25
5912209b4fb6 clarified modules;
wenzelm
parents: 76169
diff changeset
    26
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    27
  /* OpenSSH configuration and command-line */
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    28
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    29
  // see https://linux.die.net/man/5/ssh_config
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    30
  object Config {
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    31
    def entry(x: String, y: String): String = x + "=" + y
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    32
    def entry(x: String, y: Int): String = entry(x, y.toString)
76165
cf469736000c proper time values in seconds;
wenzelm
parents: 76164
diff changeset
    33
    def entry(x: String, y: Long): String = entry(x, y.toString)
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    34
    def entry(x: String, y: Boolean): String = entry(x, if (y) "yes" else "no")
64141
79cd4be708fb support user@host syntax;
wenzelm
parents: 64137
diff changeset
    35
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    36
    def make(options: Options,
76131
8b695e59db3f clarified default: do not override port from ssh_config, which could be different from 22;
wenzelm
parents: 76130
diff changeset
    37
      port: Int = 0,
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    38
      user: String = "",
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    39
      control_master: Boolean = false,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    40
      control_path: String = ""
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    41
    ): List[String] = {
76168
aab9bb081f01 clarified options;
wenzelm
parents: 76167
diff changeset
    42
      val ssh_batch_mode = options.bool("ssh_batch_mode")
76167
e517a38dc0e6 clarified options;
wenzelm
parents: 76166
diff changeset
    43
      val ssh_compression = options.bool("ssh_compression")
e517a38dc0e6 clarified options;
wenzelm
parents: 76166
diff changeset
    44
      val ssh_alive_interval = options.real("ssh_alive_interval").round
e517a38dc0e6 clarified options;
wenzelm
parents: 76166
diff changeset
    45
      val ssh_alive_count_max = options.int("ssh_alive_count_max")
e517a38dc0e6 clarified options;
wenzelm
parents: 76166
diff changeset
    46
76168
aab9bb081f01 clarified options;
wenzelm
parents: 76167
diff changeset
    47
      List(
aab9bb081f01 clarified options;
wenzelm
parents: 76167
diff changeset
    48
        entry("BatchMode", ssh_batch_mode),
aab9bb081f01 clarified options;
wenzelm
parents: 76167
diff changeset
    49
        entry("Compression", ssh_compression)) :::
76167
e517a38dc0e6 clarified options;
wenzelm
parents: 76166
diff changeset
    50
      (if (ssh_alive_interval >= 0) List(entry("ServerAliveInterval", ssh_alive_interval)) else Nil) :::
e517a38dc0e6 clarified options;
wenzelm
parents: 76166
diff changeset
    51
      (if (ssh_alive_count_max >= 0) List(entry("ServerAliveCountMax", ssh_alive_count_max)) else Nil) :::
76131
8b695e59db3f clarified default: do not override port from ssh_config, which could be different from 22;
wenzelm
parents: 76130
diff changeset
    52
      (if (port > 0) List(entry("Port", port)) else Nil) :::
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    53
      (if (user.nonEmpty) List(entry("User", user)) else Nil) :::
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    54
      (if (control_master) List("ControlMaster=yes", "ControlPersist=yes") else Nil) :::
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    55
      (if (control_path.nonEmpty) List("ControlPath=" + control_path) else Nil)
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    56
    }
64325
47e03cb99274 prevent sporadic disconnection;
wenzelm
parents: 64306
diff changeset
    57
76147
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
    58
    def option(entry: String): String = "-o " + Bash.string(entry)
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
    59
    def option(x: String, y: String): String = option(entry(x, y))
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
    60
    def option(x: String, y: Int): String = option(entry(x, y))
76165
cf469736000c proper time values in seconds;
wenzelm
parents: 76164
diff changeset
    61
    def option(x: String, y: Long): String = option(entry(x, y))
76147
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
    62
    def option(x: String, y: Boolean): String = option(entry(x, y))
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
    63
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
    64
    def command(command: String, config: List[String]): String =
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
    65
      Bash.string(command) + config.map(entry => " " + option(entry)).mkString
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    66
  }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    67
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    68
  def sftp_string(str: String): String = {
76150
5c971c7fc807 more robust;
wenzelm
parents: 76149
diff changeset
    69
    val special = "[]?*\\{} \"'"
5c971c7fc807 more robust;
wenzelm
parents: 76149
diff changeset
    70
    if (str.isEmpty) "\"\""
5c971c7fc807 more robust;
wenzelm
parents: 76149
diff changeset
    71
    else if (str.exists(special.contains)) {
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    72
      val res = new StringBuilder
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    73
      for (c <- str) {
76150
5c971c7fc807 more robust;
wenzelm
parents: 76149
diff changeset
    74
        if (special.contains(c)) res += '\\'
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    75
        res += c
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    76
      }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    77
      res.toString()
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    78
    }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    79
    else str
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    80
  }
67273
c573cfb2c407 more robust connection: prefer ServerAliveCountMax=3 (ssh default) instead of 1 (jsch default);
wenzelm
parents: 67067
diff changeset
    81
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
    82
78425
62d7ef1da441 clarified signature;
wenzelm
parents: 78347
diff changeset
    83
  /* local host (not "localhost") */
62d7ef1da441 clarified signature;
wenzelm
parents: 78347
diff changeset
    84
62d7ef1da441 clarified signature;
wenzelm
parents: 78347
diff changeset
    85
  val LOCAL = "local"
62d7ef1da441 clarified signature;
wenzelm
parents: 78347
diff changeset
    86
62d7ef1da441 clarified signature;
wenzelm
parents: 78347
diff changeset
    87
  def is_local(host: String): Boolean = host.isEmpty || host == LOCAL
62d7ef1da441 clarified signature;
wenzelm
parents: 78347
diff changeset
    88
62d7ef1da441 clarified signature;
wenzelm
parents: 78347
diff changeset
    89
  def print_local(host: String): String = if (is_local(host)) LOCAL else host
62d7ef1da441 clarified signature;
wenzelm
parents: 78347
diff changeset
    90
62d7ef1da441 clarified signature;
wenzelm
parents: 78347
diff changeset
    91
76115
f17393e21388 clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents: 76100
diff changeset
    92
  /* open session */
f17393e21388 clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents: 76100
diff changeset
    93
f17393e21388 clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents: 76100
diff changeset
    94
  def open_session(
f17393e21388 clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents: 76100
diff changeset
    95
    options: Options,
f17393e21388 clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents: 76100
diff changeset
    96
    host: String,
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
    97
    port: Int = 0,
79633
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
    98
    user: String = "",
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
    99
    user_home: String = ""
76115
f17393e21388 clarified signature: discontinue somewhat pointless SSH.Context;
wenzelm
parents: 76100
diff changeset
   100
  ): Session = {
78583
8f11794211ef tuned error;
wenzelm
parents: 78425
diff changeset
   101
    if (is_local(host)) error("Illegal SSH host name " + quote(host))
78425
62d7ef1da441 clarified signature;
wenzelm
parents: 78347
diff changeset
   102
76148
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   103
    val multiplex = options.bool("ssh_multiplexing") && !Platform.is_windows
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   104
    val (control_master, control_path) =
76161
d556db0b7256 tuned names;
wenzelm
parents: 76159
diff changeset
   105
      if (multiplex) (true, Isabelle_System.tmp_file("ssh", initialized = false).getPath)
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   106
      else (false, "")
79633
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   107
    new Session(options, host, port, user, user_home, control_master, control_path)
64257
9d51ac055cec tuned signature;
wenzelm
parents: 64256
diff changeset
   108
  }
64130
e17c211a0bb6 clarified treatment of options;
wenzelm
parents: 64129
diff changeset
   109
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   110
  class Session private[SSH](
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   111
    val options: Options,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   112
    val host: String,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   113
    val port: Int,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   114
    val user: String,
79633
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   115
    user_home0: String,
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   116
    control_master: Boolean,
76136
1bb677cceea4 let rsync re-use ssh connection via control path;
wenzelm
parents: 76133
diff changeset
   117
    val control_path: String
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   118
  ) extends System {
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   119
    ssh =>
64128
wenzelm
parents: 64127
diff changeset
   120
78346
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   121
    override def ssh_session: Option[Session] = Some(ssh)
77782
127d077cccfe clarified signature: avoid object-oriented "dispatch";
wenzelm
parents: 77761
diff changeset
   122
76133
c5fd7947f585 tuned signature;
wenzelm
parents: 76132
diff changeset
   123
    def port_suffix: String = if (port > 0) ":" + port else ""
c5fd7947f585 tuned signature;
wenzelm
parents: 76132
diff changeset
   124
    def user_prefix: String = if (user.nonEmpty) user + "@" else ""
c5fd7947f585 tuned signature;
wenzelm
parents: 76132
diff changeset
   125
c5fd7947f585 tuned signature;
wenzelm
parents: 76132
diff changeset
   126
    override def toString: String = user_prefix + host + port_suffix
77761
04a250facd44 tuned output;
wenzelm
parents: 77760
diff changeset
   127
    override def print: String = " (ssh " + toString + ")"
76132
2bb6eb6df6c2 proper port for Mercurial;
wenzelm
parents: 76131
diff changeset
   128
    override def hg_url: String = "ssh://" + toString + "/"
77783
fb61887c069a clarified underlying SSH session of "isabelle hg_sync" and "isabelle sync";
wenzelm
parents: 77782
diff changeset
   129
    override def client_command: String =
fb61887c069a clarified underlying SSH session of "isabelle hg_sync" and "isabelle sync";
wenzelm
parents: 77782
diff changeset
   130
      SSH.client_command(port = port, control_path = control_path)
76133
c5fd7947f585 tuned signature;
wenzelm
parents: 76132
diff changeset
   131
    override def rsync_prefix: String = user_prefix + host + ":"
64191
wenzelm
parents: 64190
diff changeset
   132
wenzelm
parents: 64190
diff changeset
   133
76147
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   134
    /* local ssh commands */
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   135
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   136
    def run_command(command: String,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   137
      master: Boolean = false,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   138
      opts: String = "",
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   139
      args: String = "",
76164
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   140
      cwd: JFile = null,
77092
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   141
      redirect: Boolean = false,
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   142
      progress_stdout: String => Unit = (_: String) => (),
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   143
      progress_stderr: String => Unit = (_: String) => (),
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74306
diff changeset
   144
      strict: Boolean = true
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74306
diff changeset
   145
    ): Process_Result = {
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   146
      val config =
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   147
        Config.make(options, port = port, user = user,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   148
          control_master = master, control_path = control_path)
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   149
      val cmd =
76147
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   150
        Config.command(command, config) +
77369
wenzelm
parents: 77130
diff changeset
   151
        if_proper(opts, " " + opts) +
wenzelm
parents: 77130
diff changeset
   152
        if_proper(args, " -- " + args)
77092
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   153
      Isabelle_System.bash(cmd, cwd = cwd,
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   154
        redirect = redirect,
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   155
        progress_stdout = progress_stdout,
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   156
        progress_stderr = progress_stderr,
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   157
        strict = strict)
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   158
    }
64134
57581e4026fe proper support for exec channel (see also bash.scala);
wenzelm
parents: 64133
diff changeset
   159
76164
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   160
    def run_sftp(
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   161
      script: String,
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   162
      init: Path => Unit = _ => (),
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   163
      exit: Path => Unit = _ => ()
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   164
    ): Process_Result = {
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   165
      Isabelle_System.with_tmp_dir("ssh") { dir =>
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   166
        init(dir)
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   167
        File.write(dir + Path.explode("script"), script)
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   168
        val result =
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   169
          run_command("sftp", opts = "-b script", args = Bash.string(host), cwd = dir.file).check
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   170
        exit(dir)
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   171
        result
76116
c4dc343fdbcb clarified signature: avoid exposure of JSch types;
wenzelm
parents: 76115
diff changeset
   172
      }
c4dc343fdbcb clarified signature: avoid exposure of JSch types;
wenzelm
parents: 76115
diff changeset
   173
    }
65009
eda9366bbfac remote database access via ssh port forwarding;
wenzelm
parents: 64347
diff changeset
   174
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   175
    def run_ssh(master: Boolean = false, opts: String = "", args: String = ""): Process_Result = {
77369
wenzelm
parents: 77130
diff changeset
   176
      val args1 = Bash.string(host) + if_proper(args, " " + args)
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   177
      run_command("ssh", master = master, opts = opts, args = args1)
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   178
    }
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   179
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   180
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   181
    /* init and exit */
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   182
79633
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   183
    override val home: String = {
76154
dfddb80fc515 more robust: do not assume Bash syntax while testing for it;
wenzelm
parents: 76151
diff changeset
   184
      run_ssh(master = control_master, args = "printenv HOME \";\" printenv SHELL").check.out_lines
dfddb80fc515 more robust: do not assume Bash syntax while testing for it;
wenzelm
parents: 76151
diff changeset
   185
      match {
79633
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   186
        case List(home, shell) =>
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   187
          if (shell.endsWith("/bash")) home
76149
ccc748255342 more robust: Bash.string operations require remote bash;
wenzelm
parents: 76148
diff changeset
   188
          else {
ccc748255342 more robust: Bash.string operations require remote bash;
wenzelm
parents: 76148
diff changeset
   189
            error("Bad SHELL for " + quote(toString) + " -- expected GNU bash, but found " + shell)
ccc748255342 more robust: Bash.string operations require remote bash;
wenzelm
parents: 76148
diff changeset
   190
          }
ccc748255342 more robust: Bash.string operations require remote bash;
wenzelm
parents: 76148
diff changeset
   191
        case _ => error("Malformed remote environment for " + quote(toString))
ccc748255342 more robust: Bash.string operations require remote bash;
wenzelm
parents: 76148
diff changeset
   192
      }
76151
21492610ae5b proper treatment of complex multi-line script;
wenzelm
parents: 76150
diff changeset
   193
    }
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   194
79633
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   195
    override val user_home: String = {
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   196
      val path1 =
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   197
        try { Path.explode(home).expand_env(Isabelle_System.No_Env) }
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   198
        catch { case ERROR(msg) => error(msg + " -- in SSH HOME") }
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   199
      val path2 =
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   200
        try { Path.explode(user_home0).expand_env(Isabelle_System.No_Env) }
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   201
        catch { case ERROR(msg) => error(msg + "-- in SSH USER_HOME") }
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   202
      (path1 + path2).implode
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   203
    }
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   204
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   205
    val settings: Isabelle_System.Settings = {
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   206
      case "HOME" => home
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   207
      case "USER_HOME" => user_home
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   208
      case _ => ""
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   209
    }
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   210
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   211
    override def close(): Unit = {
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   212
      if (control_path.nonEmpty) run_ssh(opts = "-O exit").check
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   213
    }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   214
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   215
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   216
    /* remote commands */
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   217
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   218
    override def execute(cmd_line: String,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   219
      progress_stdout: String => Unit = (_: String) => (),
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   220
      progress_stderr: String => Unit = (_: String) => (),
77092
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   221
      redirect: Boolean = false,
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   222
      settings: Boolean = true,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   223
      strict: Boolean = true
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   224
    ): Process_Result = {
79633
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   225
      val script = Isabelle_System.export_env(user_home = user_home) + cmd_line
77077
c2e8ba15a10a discontinued adhoc change of environment (from c62b99e3ec07), which has been mostly superseded by expand_path / remote_path (from ef6f7e8a018c);
wenzelm
parents: 77076
diff changeset
   226
      run_command("ssh",
79633
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   227
        args = Bash.string(host) + " " + Bash.string(script),
77077
c2e8ba15a10a discontinued adhoc change of environment (from c62b99e3ec07), which has been mostly superseded by expand_path / remote_path (from ef6f7e8a018c);
wenzelm
parents: 77076
diff changeset
   228
        progress_stdout = progress_stdout,
c2e8ba15a10a discontinued adhoc change of environment (from c62b99e3ec07), which has been mostly superseded by expand_path / remote_path (from ef6f7e8a018c);
wenzelm
parents: 77076
diff changeset
   229
        progress_stderr = progress_stderr,
77092
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   230
        redirect = redirect,
77077
c2e8ba15a10a discontinued adhoc change of environment (from c62b99e3ec07), which has been mostly superseded by expand_path / remote_path (from ef6f7e8a018c);
wenzelm
parents: 77076
diff changeset
   231
        strict = strict)
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   232
    }
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   233
77054
3bb374ac31b3 support remote download_file;
wenzelm
parents: 76241
diff changeset
   234
    override def download_file(
3bb374ac31b3 support remote download_file;
wenzelm
parents: 76241
diff changeset
   235
      url_name: String,
3bb374ac31b3 support remote download_file;
wenzelm
parents: 76241
diff changeset
   236
      file: Path,
3bb374ac31b3 support remote download_file;
wenzelm
parents: 76241
diff changeset
   237
      progress: Progress = new Progress
3bb374ac31b3 support remote download_file;
wenzelm
parents: 76241
diff changeset
   238
    ): Unit = {
3bb374ac31b3 support remote download_file;
wenzelm
parents: 76241
diff changeset
   239
      val cmd_line =
3bb374ac31b3 support remote download_file;
wenzelm
parents: 76241
diff changeset
   240
        File.read(Path.explode("~~/lib/scripts/download_file")) + "\n" +
3bb374ac31b3 support remote download_file;
wenzelm
parents: 76241
diff changeset
   241
          "download_file " + Bash.string(url_name) + " " + bash_path(file)
3bb374ac31b3 support remote download_file;
wenzelm
parents: 76241
diff changeset
   242
      execute(cmd_line,
77509
3bc49507bae5 clarified treatment of "verbose" messages, e.g. Progress.theory();
wenzelm
parents: 77369
diff changeset
   243
        progress_stdout = progress.echo(_),
3bc49507bae5 clarified treatment of "verbose" messages, e.g. Progress.theory();
wenzelm
parents: 77369
diff changeset
   244
        progress_stderr = progress.echo(_)).check
77054
3bb374ac31b3 support remote download_file;
wenzelm
parents: 76241
diff changeset
   245
    }
3bb374ac31b3 support remote download_file;
wenzelm
parents: 76241
diff changeset
   246
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   247
    override lazy val isabelle_platform: Isabelle_Platform = Isabelle_Platform(ssh = Some(ssh))
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   248
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   249
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   250
    /* remote file-system */
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   251
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   252
    override def expand_path(path: Path): Path = path.expand_env(settings)
77076
4471dbb3b7a0 more operations;
wenzelm
parents: 77059
diff changeset
   253
    override def absolute_path(path: Path): Path = {
4471dbb3b7a0 more operations;
wenzelm
parents: 77059
diff changeset
   254
      val path1 = expand_path(path)
79633
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   255
      if (path1.is_absolute) path1 else Path.explode(home) + path1
77076
4471dbb3b7a0 more operations;
wenzelm
parents: 77059
diff changeset
   256
    }
4471dbb3b7a0 more operations;
wenzelm
parents: 77059
diff changeset
   257
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   258
    def remote_path(path: Path): String = expand_path(path).implode
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   259
67066
1645cef7a49c proper ssh.bash_path;
wenzelm
parents: 66923
diff changeset
   260
    override def bash_path(path: Path): String = Bash.string(remote_path(path))
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   261
    def sftp_path(path: Path): String = sftp_string(remote_path(path))
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   262
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   263
    override def is_dir(path: Path): Boolean = run_ssh(args = "test -d " + bash_path(path)).ok
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   264
    override def is_file(path: Path): Boolean = run_ssh(args = "test -f " + bash_path(path)).ok
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
   265
77092
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   266
    override def delete(path: Path): Unit = {
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   267
      val cmd = if (is_dir(path)) "rmdir" else if (is_file(path)) "rm" else ""
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   268
      if (cmd.nonEmpty) run_sftp(cmd + " " + sftp_path(path))
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   269
    }
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   270
78161
4b1b7cbb3e9a clarified signature;
wenzelm
parents: 78158
diff changeset
   271
    override def restrict(path: Path): Unit =
4b1b7cbb3e9a clarified signature;
wenzelm
parents: 78158
diff changeset
   272
      if (!execute("chmod g-rwx,o-rwx " + bash_path(path)).ok) {
4b1b7cbb3e9a clarified signature;
wenzelm
parents: 78158
diff changeset
   273
        error("Failed to change permissions of " + quote(remote_path(path)))
4b1b7cbb3e9a clarified signature;
wenzelm
parents: 78158
diff changeset
   274
      }
4b1b7cbb3e9a clarified signature;
wenzelm
parents: 78158
diff changeset
   275
78298
3b0f8f1010f2 clarified signature, with subtle change of semantics (amending 8b5a2e4b16d4);
wenzelm
parents: 78161
diff changeset
   276
    override def set_executable(path: Path, reset: Boolean = false): Unit =
3b0f8f1010f2 clarified signature, with subtle change of semantics (amending 8b5a2e4b16d4);
wenzelm
parents: 78161
diff changeset
   277
      if (!execute("chmod a" + (if (reset) "-" else "+") + "x " + bash_path(path)).ok) {
77092
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   278
        error("Failed to change executable status of " + quote(remote_path(path)))
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   279
      }
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   280
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74306
diff changeset
   281
    override def make_directory(path: Path): Path = {
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   282
      if (!execute("mkdir -p " + bash_path(path)).ok) {
76118
e8e3b60d8ecd clarified operation: avoid perl;
wenzelm
parents: 76117
diff changeset
   283
        error("Failed to create directory: " + quote(remote_path(path)))
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   284
      }
72376
04bce3478688 clarified signature;
wenzelm
parents: 72375
diff changeset
   285
      path
04bce3478688 clarified signature;
wenzelm
parents: 72375
diff changeset
   286
    }
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   287
77059
422c57b75b17 support remote operations;
wenzelm
parents: 77054
diff changeset
   288
    override def copy_file(src: Path, dst: Path): Unit = {
422c57b75b17 support remote operations;
wenzelm
parents: 77054
diff changeset
   289
      val direct = if (is_dir(dst)) "/." else ""
422c57b75b17 support remote operations;
wenzelm
parents: 77054
diff changeset
   290
      if (!execute("cp -a " + bash_path(src) + " " + bash_path(dst) + direct).ok) {
422c57b75b17 support remote operations;
wenzelm
parents: 77054
diff changeset
   291
        error("Failed to copy file " + expand_path(src) + " to " +
422c57b75b17 support remote operations;
wenzelm
parents: 77054
diff changeset
   292
          expand_path(dst) + " (ssh " + toString + ")")
422c57b75b17 support remote operations;
wenzelm
parents: 77054
diff changeset
   293
      }
422c57b75b17 support remote operations;
wenzelm
parents: 77054
diff changeset
   294
    }
422c57b75b17 support remote operations;
wenzelm
parents: 77054
diff changeset
   295
77096
940a6cb734fd more operations for SSH.System;
wenzelm
parents: 77092
diff changeset
   296
    override def read_dir(path: Path): List[String] =
76241
aa6ce2e51e6c proper base names;
wenzelm
parents: 76240
diff changeset
   297
      run_sftp("@cd " + sftp_path(path) + "\n@ls -1 -a").out_lines.flatMap(s =>
aa6ce2e51e6c proper base names;
wenzelm
parents: 76240
diff changeset
   298
        if (s == "." || s == "..") None
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   299
        else Some(Library.perhaps_unprefix("./", s)))
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   300
76164
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   301
    private def get_file[A](path: Path, f: Path => A): A = {
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   302
      var result: Option[A] = None
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   303
      run_sftp("get -p " + sftp_path(path) + " local",
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   304
        exit = dir => result = Some(f(dir + Path.explode("local"))))
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   305
      result.get
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   306
    }
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   307
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   308
    private def put_file(path: Path, f: Path => Unit): Unit =
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   309
      run_sftp("put -p local " + sftp_path(path),
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   310
        init = dir => f(dir + Path.explode("local")))
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   311
73634
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   312
    override def read_file(path: Path, local_path: Path): Unit =
76164
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   313
      get_file(path, Isabelle_System.copy_file(_, local_path))
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   314
    override def read_bytes(path: Path): Bytes =
76164
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   315
      get_file(path, Bytes.read)
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   316
    override def read(path: Path): String =
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   317
      get_file(path, File.read)
64256
c3197aeae90b simplified SSH.Session: sftp channel is always open and its operations provided by the main interface;
wenzelm
parents: 64254
diff changeset
   318
73634
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   319
    override def write_file(path: Path, local_path: Path): Unit =
76164
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   320
      put_file(path, Isabelle_System.copy_file(local_path, _))
77092
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   321
    override def write_bytes(path: Path, bytes: Bytes): Unit =
76164
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   322
      put_file(path, Bytes.write(_, bytes))
77092
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   323
    override def write(path: Path, text: String): Unit =
76164
5e8bc80df6b3 clarified run_sftp: avoid platform_path via careful use of tmp_dir, to support both Windows and Cygwin ssh;
wenzelm
parents: 76163
diff changeset
   324
      put_file(path, File.write(_, text))
72338
54871a086193 formal platform information, notably for ssh;
wenzelm
parents: 71780
diff changeset
   325
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   326
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   327
    /* tmp dirs */
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   328
77092
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   329
    override def rm_tree(dir: Path): Unit = rm_tree(remote_path(dir))
64306
7b6dc1b36f20 tuned signature, in accordance to Isabelle_System;
wenzelm
parents: 64304
diff changeset
   330
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   331
    def rm_tree(remote_dir: String): Unit =
64304
96bc94c87a81 clarified modules;
wenzelm
parents: 64258
diff changeset
   332
      execute("rm -r -f " + Bash.string(remote_dir)).check
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   333
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   334
    def tmp_dir(): String =
76163
9df6f51ebf45 more robust, notably for macOS (see also ff92d6edff2c);
wenzelm
parents: 76161
diff changeset
   335
      execute("mktemp -d /tmp/ssh-XXXXXXXXXXXX").check.out
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   336
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74306
diff changeset
   337
    override def with_tmp_dir[A](body: Path => A): A = {
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   338
      val remote_dir = tmp_dir()
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   339
      try { body(Path.explode(remote_dir)) }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   340
      finally { rm_tree(remote_dir) }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   341
    }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   342
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   343
78345
545da61f5989 clarified signature;
wenzelm
parents: 78341
diff changeset
   344
    /* open server on remote host */
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   345
78345
545da61f5989 clarified signature;
wenzelm
parents: 78341
diff changeset
   346
    def open_server(
78339
f8a553a21423 tuned signature;
wenzelm
parents: 78298
diff changeset
   347
      remote_port: Int = 0,
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   348
      remote_host: String = "localhost",
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   349
      local_port: Int = 0,
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   350
      local_host: String = "localhost",
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   351
      ssh_close: Boolean = false
78345
545da61f5989 clarified signature;
wenzelm
parents: 78341
diff changeset
   352
    ): Server = {
78340
5790e48f7573 clarified signature: more uniform SSH.Port_Forwarding;
wenzelm
parents: 78339
diff changeset
   353
      val forward_host = local_host
5790e48f7573 clarified signature: more uniform SSH.Port_Forwarding;
wenzelm
parents: 78339
diff changeset
   354
      val forward_port = if (local_port > 0) local_port else Isabelle_System.local_port()
5790e48f7573 clarified signature: more uniform SSH.Port_Forwarding;
wenzelm
parents: 78339
diff changeset
   355
      val forward = List(forward_host, forward_port, remote_host, remote_port).mkString(":")
76147
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   356
      val forward_option = "-L " + Bash.string(forward)
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   357
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   358
      val cancel: () => Unit =
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   359
        if (control_path.nonEmpty) {
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   360
          run_ssh(opts = forward_option + " -O forward").check
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   361
          () => run_ssh(opts = forward_option + " -O cancel")  // permissive
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   362
        }
76148
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   363
        else {
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   364
          val result = Synchronized[Exn.Result[Boolean]](Exn.Res(false))
78345
545da61f5989 clarified signature;
wenzelm
parents: 78341
diff changeset
   365
          val thread = Isabelle_Thread.fork("ssh_server") {
76148
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   366
            val opts =
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   367
              forward_option +
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   368
                " " + Config.option("SessionType", "none") +
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   369
                " " + Config.option("PermitLocalCommand", true) +
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   370
                " " + Config.option("LocalCommand", "pwd")
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   371
            try {
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   372
              run_command("ssh", opts = opts, args = Bash.string(host),
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   373
                progress_stdout = _ => result.change(_ => Exn.Res(true))).check
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   374
            }
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   375
            catch { case exn: Throwable => result.change(_ => Exn.Exn(exn)) }
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   376
          }
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   377
          result.guarded_access {
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   378
            case res@Exn.Res(ok) => if (ok) Some((), res) else None
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   379
            case Exn.Exn(exn) => throw exn
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   380
          }
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   381
          () => thread.interrupt()
769ebb139a32 support port forwarding without multiplexing (for the sake of Windows);
wenzelm
parents: 76147
diff changeset
   382
        }
76147
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   383
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   384
      val shutdown_hook =
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   385
        Isabelle_System.create_shutdown_hook { cancel() }
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   386
78346
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   387
      new Server(forward_host, forward_port, ssh) {
76147
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   388
        override def toString: String = forward
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   389
        override def close(): Unit = {
76147
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   390
          cancel()
75f0fc965539 misc tuning and clarification;
wenzelm
parents: 76145
diff changeset
   391
          Isabelle_System.remove_shutdown_hook(shutdown_hook)
76122
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   392
          if (ssh_close) ssh.close()
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   393
        }
b8f26c20d3b1 ssh client via regular OpenSSH tools, with authentic use of .ssh/config (notably proxy configuration);
wenzelm
parents: 76119
diff changeset
   394
      }
64137
e9b3d9c1bc5a support for remote tmp dirs;
wenzelm
parents: 64136
diff changeset
   395
    }
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   396
  }
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   397
78346
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   398
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   399
  /* server port forwarding */
78340
5790e48f7573 clarified signature: more uniform SSH.Port_Forwarding;
wenzelm
parents: 78339
diff changeset
   400
78346
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   401
  def open_server(
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   402
    options: Options,
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   403
    host: String,
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   404
    port: Int = 0,
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   405
    user: String = "",
79633
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   406
    user_home: String = "",
78346
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   407
    remote_port: Int = 0,
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   408
    remote_host: String = "localhost",
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   409
    local_port: Int = 0,
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   410
    local_host: String = "localhost"
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   411
  ): Server = {
79633
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   412
    val ssh = open_session(options, host, port = port, user = user, user_home = user_home)
78346
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   413
    try {
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   414
      ssh.open_server(remote_port = remote_port, remote_host = remote_host,
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   415
        local_port = local_port, local_host = local_host, ssh_close = true)
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   416
    }
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   417
    catch { case exn: Throwable => ssh.close(); throw exn }
78341
5f14f1290a88 clarified signature;
wenzelm
parents: 78340
diff changeset
   418
  }
5f14f1290a88 clarified signature;
wenzelm
parents: 78340
diff changeset
   419
78345
545da61f5989 clarified signature;
wenzelm
parents: 78341
diff changeset
   420
  def local_server(port: Int = 0, host: String = "localhost"): Server =
545da61f5989 clarified signature;
wenzelm
parents: 78341
diff changeset
   421
    new Local_Server(host, port)
78341
5f14f1290a88 clarified signature;
wenzelm
parents: 78340
diff changeset
   422
78345
545da61f5989 clarified signature;
wenzelm
parents: 78341
diff changeset
   423
  val no_server: Server = new No_Server
76116
c4dc343fdbcb clarified signature: avoid exposure of JSch types;
wenzelm
parents: 76115
diff changeset
   424
78346
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   425
  class Server private[SSH](
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   426
    val host: String,
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   427
    val port: Int,
78347
72fc2ff08e07 clarified signature: more operations;
wenzelm
parents: 78346
diff changeset
   428
    val ssh_system: System
78346
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   429
  ) extends AutoCloseable {
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   430
    def defined: Boolean = host.nonEmpty && port > 0
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   431
    override def close(): Unit = ()
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   432
  }
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   433
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   434
  class Local_Server private[SSH](host: String, port: Int)
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   435
  extends Server(host, port, Local) {
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   436
    override def toString: String = if_proper(host, host + ":") + port
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   437
  }
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   438
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   439
  class No_Server extends Server("", 0, Local) {
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   440
    override def toString: String = "0"
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   441
  }
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   442
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   443
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   444
  /* system operations */
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   445
77783
fb61887c069a clarified underlying SSH session of "isabelle hg_sync" and "isabelle sync";
wenzelm
parents: 77782
diff changeset
   446
  def open_system(
fb61887c069a clarified underlying SSH session of "isabelle hg_sync" and "isabelle sync";
wenzelm
parents: 77782
diff changeset
   447
    options: Options,
fb61887c069a clarified underlying SSH session of "isabelle hg_sync" and "isabelle sync";
wenzelm
parents: 77782
diff changeset
   448
    host: String,
fb61887c069a clarified underlying SSH session of "isabelle hg_sync" and "isabelle sync";
wenzelm
parents: 77782
diff changeset
   449
    port: Int = 0,
79633
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   450
    user: String = "",
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   451
    user_home: String = ""
77783
fb61887c069a clarified underlying SSH session of "isabelle hg_sync" and "isabelle sync";
wenzelm
parents: 77782
diff changeset
   452
  ): System = {
79635
8d2539a13502 more robust;
wenzelm
parents: 79633
diff changeset
   453
    if (is_local(host)) {
8d2539a13502 more robust;
wenzelm
parents: 79633
diff changeset
   454
      if (user_home.isEmpty) Local
8d2539a13502 more robust;
wenzelm
parents: 79633
diff changeset
   455
      else error("Illegal user home for local host")
8d2539a13502 more robust;
wenzelm
parents: 79633
diff changeset
   456
    }
79633
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   457
    else open_session(options, host = host, port = port, user = user, user_home = user_home)
77783
fb61887c069a clarified underlying SSH session of "isabelle hg_sync" and "isabelle sync";
wenzelm
parents: 77782
diff changeset
   458
  }
fb61887c069a clarified underlying SSH session of "isabelle hg_sync" and "isabelle sync";
wenzelm
parents: 77782
diff changeset
   459
75393
87ebf5a50283 clarified formatting, for the sake of scala3;
wenzelm
parents: 74306
diff changeset
   460
  trait System extends AutoCloseable {
78346
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   461
    def ssh_session: Option[Session]
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   462
    def is_local: Boolean = ssh_session.isEmpty
77782
127d077cccfe clarified signature: avoid object-oriented "dispatch";
wenzelm
parents: 77761
diff changeset
   463
79633
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   464
    def home: String = ""
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   465
    def user_home: String = ""
c59231722f10 support explicit USER_HOME within SSH session;
wenzelm
parents: 78924
diff changeset
   466
73634
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   467
    def close(): Unit = ()
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   468
78425
62d7ef1da441 clarified signature;
wenzelm
parents: 78347
diff changeset
   469
    override def toString: String = LOCAL
77761
04a250facd44 tuned output;
wenzelm
parents: 77760
diff changeset
   470
    def print: String = ""
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   471
    def hg_url: String = ""
77783
fb61887c069a clarified underlying SSH session of "isabelle hg_sync" and "isabelle sync";
wenzelm
parents: 77782
diff changeset
   472
    def client_command: String = ""
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   473
75500
57e292106d71 more operations;
wenzelm
parents: 75480
diff changeset
   474
    def rsync_prefix: String = ""
75517
292d7a9dc8a3 proper operation on String, not Path;
wenzelm
parents: 75513
diff changeset
   475
    def rsync_path(path: Path): String = rsync_prefix + expand_path(path).implode
75500
57e292106d71 more operations;
wenzelm
parents: 75480
diff changeset
   476
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   477
    def expand_path(path: Path): Path = path.expand
77076
4471dbb3b7a0 more operations;
wenzelm
parents: 77059
diff changeset
   478
    def absolute_path(path: Path): Path = path.absolute
67066
1645cef7a49c proper ssh.bash_path;
wenzelm
parents: 66923
diff changeset
   479
    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
   480
    def is_dir(path: Path): Boolean = path.is_dir
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   481
    def is_file(path: Path): Boolean = path.is_file
77092
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   482
    def delete(path: Path): Unit = path.file.delete
78161
4b1b7cbb3e9a clarified signature;
wenzelm
parents: 78158
diff changeset
   483
    def restrict(path: Path): Unit = File.restrict(path)
78298
3b0f8f1010f2 clarified signature, with subtle change of semantics (amending 8b5a2e4b16d4);
wenzelm
parents: 78161
diff changeset
   484
    def set_executable(path: Path, reset: Boolean = false): Unit =
3b0f8f1010f2 clarified signature, with subtle change of semantics (amending 8b5a2e4b16d4);
wenzelm
parents: 78161
diff changeset
   485
      File.set_executable(path, reset = reset)
72376
04bce3478688 clarified signature;
wenzelm
parents: 72375
diff changeset
   486
    def make_directory(path: Path): Path = Isabelle_System.make_directory(path)
77092
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   487
    def rm_tree(dir: Path): Unit = Isabelle_System.rm_tree(dir)
73634
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   488
    def with_tmp_dir[A](body: Path => A): A = Isabelle_System.with_tmp_dir("tmp")(body)
77096
940a6cb734fd more operations for SSH.System;
wenzelm
parents: 77092
diff changeset
   489
    def read_dir(path: Path): List[String] = File.read_dir(path)
77059
422c57b75b17 support remote operations;
wenzelm
parents: 77054
diff changeset
   490
    def copy_file(path1: Path, path2: Path): Unit = Isabelle_System.copy_file(path1, path2)
73634
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   491
    def read_file(path1: Path, path2: Path): Unit = Isabelle_System.copy_file(path1, path2)
75513
36316c6a3fc2 clarified signature: more operations;
wenzelm
parents: 75500
diff changeset
   492
    def read_bytes(path: Path): Bytes = Bytes.read(path)
36316c6a3fc2 clarified signature: more operations;
wenzelm
parents: 75500
diff changeset
   493
    def read(path: Path): String = File.read(path)
77092
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   494
    def write_file(path1: Path, path2: Path): Unit = Isabelle_System.copy_file(path2, path1)
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   495
    def write_bytes(path: Path, bytes: Bytes): Unit = Bytes.write(path, bytes)
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   496
    def write(path: Path, text: String): Unit = File.write(path, text)
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   497
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   498
    def execute(command: String,
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   499
        progress_stdout: String => Unit = (_: String) => (),
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   500
        progress_stderr: String => Unit = (_: String) => (),
77092
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   501
        redirect: Boolean = false,
73634
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   502
        settings: Boolean = true,
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   503
        strict: Boolean = true): Process_Result =
73634
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   504
      Isabelle_System.bash(command,
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   505
        progress_stdout = progress_stdout,
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   506
        progress_stderr = progress_stderr,
77092
4d9f3d1e1749 more operations for SSH.System;
wenzelm
parents: 77079
diff changeset
   507
        redirect = redirect,
73634
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   508
        env = if (settings) Isabelle_System.settings() else null,
c88faa1e09e1 support local build_heaps;
wenzelm
parents: 73367
diff changeset
   509
        strict = strict)
72338
54871a086193 formal platform information, notably for ssh;
wenzelm
parents: 71780
diff changeset
   510
77760
34178d26a360 more SSH operations;
wenzelm
parents: 77509
diff changeset
   511
    def new_directory(path: Path): Path =
34178d26a360 more SSH operations;
wenzelm
parents: 77509
diff changeset
   512
      if (is_dir(path)) error("Directory already exists: " + absolute_path(path))
34178d26a360 more SSH operations;
wenzelm
parents: 77509
diff changeset
   513
      else make_directory(path)
34178d26a360 more SSH operations;
wenzelm
parents: 77509
diff changeset
   514
77054
3bb374ac31b3 support remote download_file;
wenzelm
parents: 76241
diff changeset
   515
    def download_file(url_name: String, file: Path, progress: Progress = new Progress): Unit =
3bb374ac31b3 support remote download_file;
wenzelm
parents: 76241
diff changeset
   516
      Isabelle_System.download_file(url_name, file, progress = progress)
3bb374ac31b3 support remote download_file;
wenzelm
parents: 76241
diff changeset
   517
72340
676066aa4798 clarified signature;
wenzelm
parents: 72338
diff changeset
   518
    def isabelle_platform: Isabelle_Platform = Isabelle_Platform()
77130
2b8cf3b94cde more operations;
wenzelm
parents: 77096
diff changeset
   519
78610
fd1fec53665b clarified signature: prefer enum types;
wenzelm
parents: 78583
diff changeset
   520
    def isabelle_platform_family: Platform.Family =
77130
2b8cf3b94cde more operations;
wenzelm
parents: 77096
diff changeset
   521
      Platform.Family.parse(isabelle_platform.ISABELLE_PLATFORM_FAMILY)
66570
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   522
  }
9af879e222cc clarified signature;
wenzelm
parents: 65717
diff changeset
   523
77782
127d077cccfe clarified signature: avoid object-oriented "dispatch";
wenzelm
parents: 77761
diff changeset
   524
  object Local extends System {
78346
9c2e273d2f0d clarified signature: more operations;
wenzelm
parents: 78345
diff changeset
   525
    override def ssh_session: Option[Session] = None
77782
127d077cccfe clarified signature: avoid object-oriented "dispatch";
wenzelm
parents: 77761
diff changeset
   526
  }
64123
a967b5a07f92 support for SSH in Isabelle/Scala;
wenzelm
parents:
diff changeset
   527
}