tramp-sh.el 217 KB
Newer Older
1 2
;;; tramp-sh.el --- Tramp access functions for (s)sh-like connections

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 1998-2016 Free Software Foundation, Inc.
4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30

;; (copyright statements below in code to be updated with the above notice)

;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
;;         Michael Albinus <michael.albinus@gmx.de>
;; Keywords: comm, processes
;; Package: tramp

;; This file is part of GNU Emacs.

;; GNU Emacs is free software: you can redistribute it and/or modify
;; it under the terms of the GNU General Public License as published by
;; the Free Software Foundation, either version 3 of the License, or
;; (at your option) any later version.

;; GNU Emacs is distributed in the hope that it will be useful,
;; but WITHOUT ANY WARRANTY; without even the implied warranty of
;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
;; GNU General Public License for more details.

;; You should have received a copy of the GNU General Public License
;; along with GNU Emacs.  If not, see <http://www.gnu.org/licenses/>.

;;; Code:

(require 'tramp)

Michael Albinus's avatar
Michael Albinus committed
31 32 33
;; Pacify byte-compiler.
(eval-when-compile
  (require 'cl)
Michael Albinus's avatar
Michael Albinus committed
34 35 36
  (require 'dired))
(defvar tramp-gw-tunnel-method)
(defvar tramp-gw-socks-method)
37 38 39 40
(defvar vc-handled-backends)
(defvar vc-bzr-program)
(defvar vc-git-program)
(defvar vc-hg-program)
41

42
;;;###tramp-autoload
43
(defcustom tramp-inline-compress-start-size 4096
44
  "The minimum size of compressing where inline transfer.
Paul Eggert's avatar
Paul Eggert committed
45
When inline transfer, compress transferred data of file
46 47 48 49 50
whose size is this value or above (up to `tramp-copy-size-limit').
If it is nil, no compression at all will be applied."
  :group 'tramp
  :type '(choice (const nil) integer))

51
;;;###tramp-autoload
52
(defcustom tramp-copy-size-limit 10240
Michael Albinus's avatar
Michael Albinus committed
53 54 55
  "The maximum file size where inline copying is preferred over an \
out-of-the-band copy.
If it is nil, out-of-the-band copy will be used without a check."
56 57 58 59 60
  :group 'tramp
  :type '(choice (const nil) integer))

;;;###tramp-autoload
(defcustom tramp-terminal-type "dumb"
61
  "Value of TERM environment variable for logging in to remote host.
62 63 64 65 66 67
Because Tramp wants to parse the output of the remote shell, it is easily
confused by ANSI color escape sequences and suchlike.  Often, shell init
files conditionalize this setup based on the TERM environment variable."
  :group 'tramp
  :type 'string)

68
;;;###tramp-autoload
69
(defcustom tramp-histfile-override ".tramp_history"
70
  "When invoking a shell, override the HISTFILE with this value.
71 72 73
When setting to a string, it redirects the shell history to that
file.  Be careful when setting to \"/dev/null\"; this might
result in undesired results when using \"bash\" as shell.
74

75 76 77 78 79
The value t, the default value, unsets any setting of HISTFILE,
and sets both HISTFILESIZE and HISTSIZE to 0.  If you set this
variable to nil, however, the *override* is disabled, so the
history will go to the default storage location,
e.g. \"$HOME/.sh_history\"."
80
  :group 'tramp
81
  :version "25.1"
82
  :type '(choice (const :tag "Do not override HISTFILE" nil)
83
                 (const :tag "Unset HISTFILE" t)
84 85
                 (string :tag "Redirect to a file")))

86
;;;###tramp-autoload
87 88 89
(defconst tramp-color-escape-sequence-regexp "\e[[;0-9]+m"
  "Escape sequences produced by the \"ls\" command.")

90
;; ksh on OpenBSD 4.5 requires that $PS1 contains a `#' character for
91
;; root users.  It uses the `$' character for other users.  In order
92
;; to guarantee a proper prompt, we use "#$ " for the prompt.
93 94 95 96 97 98

(defvar tramp-end-of-output
  (format
   "///%s#$"
   (md5 (concat (prin1-to-string process-environment) (current-time-string))))
  "String used to recognize end of output.
99
The `$' character at the end is quoted; the string cannot be
100 101 102 103 104 105
detected as prompt when being sent on echoing hosts, therefore.")

;;;###tramp-autoload
(defconst tramp-initial-end-of-output "#$ "
  "Prompt when establishing a connection.")

106 107 108
(defconst tramp-end-of-heredoc (md5 tramp-end-of-output)
  "String used to recognize end of heredoc strings.")

109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129
;;;###tramp-autoload
(defcustom tramp-use-ssh-controlmaster-options t
  "Whether to use `tramp-ssh-controlmaster-options'."
  :group 'tramp
  :version "24.4"
  :type 'boolean)

(defvar tramp-ssh-controlmaster-options nil
  "Which ssh Control* arguments to use.

If it is a string, it should have the form
\"-o ControlMaster=auto -o ControlPath='tramp.%%r@%%h:%%p'
-o ControlPersist=no\".  Percent characters in the ControlPath
spec must be doubled, because the string is used as format string.

Otherwise, it will be auto-detected by Tramp, if
`tramp-use-ssh-controlmaster-options' is non-nil.  The value
depends on the installed local ssh version.

The string is used in `tramp-methods'.")

130 131 132 133 134 135
;; Initialize `tramp-methods' with the supported methods.
;;;###tramp-autoload
(add-to-list 'tramp-methods
  '("rcp"
    (tramp-login-program        "rsh")
    (tramp-login-args           (("%h") ("-l" "%u")))
136
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
137
    (tramp-remote-shell-login   ("-l"))
138
    (tramp-remote-shell-args    ("-c"))
139
    (tramp-copy-program         "rcp")
Michael Albinus's avatar
Michael Albinus committed
140
    (tramp-copy-args            (("-p" "%k") ("-r")))
141 142 143 144 145 146 147
    (tramp-copy-keep-date       t)
    (tramp-copy-recursive       t)))
;;;###tramp-autoload
(add-to-list 'tramp-methods
  '("remcp"
    (tramp-login-program        "remsh")
    (tramp-login-args           (("%h") ("-l" "%u")))
148
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
149
    (tramp-remote-shell-login   ("-l"))
150
    (tramp-remote-shell-args    ("-c"))
151
    (tramp-copy-program         "rcp")
Michael Albinus's avatar
Michael Albinus committed
152
    (tramp-copy-args            (("-p" "%k")))
153 154
    (tramp-copy-keep-date       t)))
;;;###tramp-autoload
155
(add-to-list 'tramp-methods
156
  '("scp"
157
    (tramp-login-program        "ssh")
158
    (tramp-login-args           (("-l" "%u") ("-p" "%p") ("%c")
159
				 ("-e" "none") ("%h")))
160
    (tramp-async-args           (("-q")))
161
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
162
    (tramp-remote-shell-login   ("-l"))
163
    (tramp-remote-shell-args    ("-c"))
164
    (tramp-copy-program         "scp")
165
    (tramp-copy-args            (("-P" "%p") ("-p" "%k") ("-q") ("-r") ("%c")))
166 167 168 169 170 171 172 173
    (tramp-copy-keep-date       t)
    (tramp-copy-recursive       t)
    (tramp-gw-args              (("-o" "GlobalKnownHostsFile=/dev/null")
				 ("-o" "UserKnownHostsFile=/dev/null")
				 ("-o" "StrictHostKeyChecking=no")))
    (tramp-default-port         22)))
;;;###tramp-autoload
(add-to-list 'tramp-methods
174
  '("scpx"
175
    (tramp-login-program        "ssh")
176
    (tramp-login-args           (("-l" "%u") ("-p" "%p") ("%c")
177
				 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
178
    (tramp-async-args           (("-q")))
179
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
180
    (tramp-remote-shell-login   ("-l"))
181
    (tramp-remote-shell-args    ("-c"))
182
    (tramp-copy-program         "scp")
183 184
    (tramp-copy-args            (("-P" "%p") ("-p" "%k")
				 ("-q") ("-r") ("%c")))
185
    (tramp-copy-keep-date       t)
186
    (tramp-copy-recursive       t)
187 188 189 190 191 192
    (tramp-gw-args              (("-o" "GlobalKnownHostsFile=/dev/null")
				 ("-o" "UserKnownHostsFile=/dev/null")
				 ("-o" "StrictHostKeyChecking=no")))
    (tramp-default-port         22)))
;;;###tramp-autoload
(add-to-list 'tramp-methods
193
  '("rsync"
194
    (tramp-login-program        "ssh")
195
    (tramp-login-args           (("-l" "%u") ("-p" "%p") ("%c")
196 197
				 ("-e" "none") ("%h")))
    (tramp-async-args           (("-q")))
198
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
199
    (tramp-remote-shell-login   ("-l"))
200
    (tramp-remote-shell-args    ("-c"))
201
    (tramp-copy-program         "rsync")
Michael Albinus's avatar
Michael Albinus committed
202
    (tramp-copy-args            (("-t" "%k") ("-r")))
203
    (tramp-copy-env             (("RSYNC_RSH") ("ssh" "%c")))
204 205 206 207 208 209 210 211
    (tramp-copy-keep-date       t)
    (tramp-copy-keep-tmpfile    t)
    (tramp-copy-recursive       t)))
;;;###tramp-autoload
(add-to-list 'tramp-methods
  '("rsh"
    (tramp-login-program        "rsh")
    (tramp-login-args           (("%h") ("-l" "%u")))
212
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
213
    (tramp-remote-shell-login   ("-l"))
214
    (tramp-remote-shell-args    ("-c"))))
215 216 217 218 219
;;;###tramp-autoload
(add-to-list 'tramp-methods
  '("remsh"
    (tramp-login-program        "remsh")
    (tramp-login-args           (("%h") ("-l" "%u")))
220
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
221
    (tramp-remote-shell-login   ("-l"))
222
    (tramp-remote-shell-args    ("-c"))))
223 224
;;;###tramp-autoload
(add-to-list 'tramp-methods
225
  '("ssh"
226
    (tramp-login-program        "ssh")
227
    (tramp-login-args           (("-l" "%u") ("-p" "%p") ("%c")
228
				 ("-e" "none") ("%h")))
229
    (tramp-async-args           (("-q")))
230
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
231
    (tramp-remote-shell-login   ("-l"))
232
    (tramp-remote-shell-args    ("-c"))
233 234 235 236 237 238
    (tramp-gw-args              (("-o" "GlobalKnownHostsFile=/dev/null")
				 ("-o" "UserKnownHostsFile=/dev/null")
				 ("-o" "StrictHostKeyChecking=no")))
    (tramp-default-port         22)))
;;;###tramp-autoload
(add-to-list 'tramp-methods
239
  '("sshx"
240
    (tramp-login-program        "ssh")
241
    (tramp-login-args           (("-l" "%u") ("-p" "%p") ("%c")
242
				 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
243
    (tramp-async-args           (("-q")))
244
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
245
    (tramp-remote-shell-login   ("-l"))
246
    (tramp-remote-shell-args    ("-c"))
247 248 249 250 251 252 253 254
    (tramp-gw-args              (("-o" "GlobalKnownHostsFile=/dev/null")
				 ("-o" "UserKnownHostsFile=/dev/null")
				 ("-o" "StrictHostKeyChecking=no")))
    (tramp-default-port         22)))
;;;###tramp-autoload
(add-to-list 'tramp-methods
  '("telnet"
    (tramp-login-program        "telnet")
255
    (tramp-login-args           (("%h") ("%p") ("2>/dev/null")))
256
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
257
    (tramp-remote-shell-login   ("-l"))
258
    (tramp-remote-shell-args    ("-c"))
259 260
    (tramp-default-port         23)))
;;;###tramp-autoload
261 262 263 264 265
(add-to-list 'tramp-methods
  '("nc"
    (tramp-login-program        "telnet")
    (tramp-login-args           (("%h") ("%p") ("2>/dev/null")))
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
266
    (tramp-remote-shell-login   ("-l"))
267 268 269 270 271 272 273 274
    (tramp-remote-shell-args    ("-c"))
    (tramp-copy-program         "nc")
    ;; We use "-v" for better error tracking.
    (tramp-copy-args            (("-w" "1") ("-v") ("%h") ("%r")))
    (tramp-remote-copy-program  "nc")
    ;; We use "-p" as required for newer busyboxes.  For older
    ;; busybox/nc versions, the value must be (("-l") ("%r")).  This
    ;; can be achieved by tweaking `tramp-connection-properties'.
275
    (tramp-remote-copy-args     (("-l") ("-p" "%r") ("2>/dev/null")))
276 277
    (tramp-default-port         23)))
;;;###tramp-autoload
278 279 280 281
(add-to-list 'tramp-methods
  '("su"
    (tramp-login-program        "su")
    (tramp-login-args           (("-") ("%u")))
282
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
283
    (tramp-remote-shell-login   ("-l"))
284 285
    (tramp-remote-shell-args    ("-c"))
    (tramp-connection-timeout   10)))
286 287 288 289
;;;###tramp-autoload
(add-to-list 'tramp-methods
  '("sudo"
    (tramp-login-program        "sudo")
290 291 292 293
    ;; The password template must be masked.  Otherwise, it could be
    ;; interpreted as password prompt if the remote host echoes the command.
    (tramp-login-args           (("-u" "%u") ("-s") ("-H")
				 ("-p" "P\"\"a\"\"s\"\"s\"\"w\"\"o\"\"r\"\"d\"\":")))
294 295
    ;; Local $SHELL could be a nasty one, like zsh or fish.  Let's override it.
    (tramp-login-env            (("SHELL") ("/bin/sh")))
296
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
297
    (tramp-remote-shell-login   ("-l"))
298 299
    (tramp-remote-shell-args    ("-c"))
    (tramp-connection-timeout   10)))
300
;;;###tramp-autoload
301 302 303 304
(add-to-list 'tramp-methods
  '("ksu"
    (tramp-login-program        "ksu")
    (tramp-login-args           (("%u") ("-q")))
305
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
306
    (tramp-remote-shell-login   ("-l"))
307 308
    (tramp-remote-shell-args    ("-c"))
    (tramp-connection-timeout   10)))
309
;;;###tramp-autoload
310 311 312 313
(add-to-list 'tramp-methods
  '("krlogin"
    (tramp-login-program        "krlogin")
    (tramp-login-args           (("%h") ("-l" "%u") ("-x")))
314
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
315
    (tramp-remote-shell-login   ("-l"))
316
    (tramp-remote-shell-args    ("-c"))))
317 318
;;;###tramp-autoload
(add-to-list 'tramp-methods
319
  `("plink"
320
    (tramp-login-program        "plink")
321 322 323 324 325 326 327 328
    ;; ("%h") must be a single element, see `tramp-compute-multi-hops'.
    (tramp-login-args           (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
				 ("%h") ("\"")
				 (,(format
				    "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
				    tramp-terminal-type
				    tramp-initial-end-of-output))
				 ("/bin/sh") ("\"")))
329
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
330
    (tramp-remote-shell-login   ("-l"))
331
    (tramp-remote-shell-args    ("-c"))
332 333 334 335 336
    (tramp-default-port         22)))
;;;###tramp-autoload
(add-to-list 'tramp-methods
  `("plinkx"
    (tramp-login-program        "plink")
337
    (tramp-login-args           (("-load") ("%h") ("-t") ("\"")
338 339 340 341
				 (,(format
				    "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
				    tramp-terminal-type
				    tramp-initial-end-of-output))
342
				 ("/bin/sh") ("\"")))
343
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
344
    (tramp-remote-shell-login   ("-l"))
345
    (tramp-remote-shell-args    ("-c"))))
346 347
;;;###tramp-autoload
(add-to-list 'tramp-methods
348
  `("pscp"
349
    (tramp-login-program        "plink")
350 351 352 353 354 355 356
    (tramp-login-args           (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
				 ("%h") ("\"")
				 (,(format
				    "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
				    tramp-terminal-type
				    tramp-initial-end-of-output))
				 ("/bin/sh") ("\"")))
357
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
358
    (tramp-remote-shell-login   ("-l"))
359
    (tramp-remote-shell-args    ("-c"))
360
    (tramp-copy-program         "pscp")
361
    (tramp-copy-args            (("-l" "%u") ("-P" "%p") ("-scp") ("-p" "%k")
362
				 ("-q") ("-r")))
363
    (tramp-copy-keep-date       t)
364
    (tramp-copy-recursive       t)
365 366 367
    (tramp-default-port         22)))
;;;###tramp-autoload
(add-to-list 'tramp-methods
368
  `("psftp"
369
    (tramp-login-program        "plink")
370 371 372 373 374 375 376
    (tramp-login-args           (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
				 ("%h") ("\"")
				 (,(format
				    "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
				    tramp-terminal-type
				    tramp-initial-end-of-output))
				 ("/bin/sh") ("\"")))
377
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
378
    (tramp-remote-shell-login   ("-l"))
379
    (tramp-remote-shell-args    ("-c"))
380
    (tramp-copy-program         "pscp")
381
    (tramp-copy-args            (("-l" "%u") ("-P" "%p") ("-sftp") ("-p" "%k")
382 383
				 ("-q")))
    (tramp-copy-keep-date       t)))
384 385 386 387 388
;;;###tramp-autoload
(add-to-list 'tramp-methods
  '("fcp"
    (tramp-login-program        "fsh")
    (tramp-login-args           (("%h") ("-l" "%u") ("sh" "-i")))
389
    (tramp-remote-shell         "/bin/sh")
Michael Albinus's avatar
Michael Albinus committed
390
    (tramp-remote-shell-login   ("-l"))
391
    (tramp-remote-shell-args    ("-i") ("-c"))
392
    (tramp-copy-program         "fcp")
Michael Albinus's avatar
Michael Albinus committed
393
    (tramp-copy-args            (("-p" "%k")))
394 395
    (tramp-copy-keep-date       t)))

396
;;;###tramp-autoload
397 398 399
(add-to-list 'tramp-default-method-alist
	     `(,tramp-local-host-regexp "\\`root\\'" "su"))

400
;;;###tramp-autoload
401
(add-to-list 'tramp-default-user-alist
402 403
	     `(,(concat "\\`" (regexp-opt '("su" "sudo" "ksu")) "\\'")
	       nil "root"))
404
;; Do not add "ssh" based methods, otherwise ~/.ssh/config would be ignored.
405
;; Do not add "plink" based methods, they ask interactively for the user.
406
;;;###tramp-autoload
407
(add-to-list 'tramp-default-user-alist
408 409
	     `(,(concat
		 "\\`"
410 411
		 (regexp-opt
		  '("rcp" "remcp" "rsh" "telnet" "nc" "krlogin" "fcp"))
412
		 "\\'")
413 414
	       nil ,(user-login-name)))

415
;;;###tramp-autoload
416 417 418 419 420
(defconst tramp-completion-function-alist-rsh
  '((tramp-parse-rhosts "/etc/hosts.equiv")
    (tramp-parse-rhosts "~/.rhosts"))
  "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")

421
;;;###tramp-autoload
422 423 424 425 426 427 428 429 430 431 432 433 434 435 436
(defconst tramp-completion-function-alist-ssh
  '((tramp-parse-rhosts      "/etc/hosts.equiv")
    (tramp-parse-rhosts      "/etc/shosts.equiv")
    (tramp-parse-shosts      "/etc/ssh_known_hosts")
    (tramp-parse-sconfig     "/etc/ssh_config")
    (tramp-parse-shostkeys   "/etc/ssh2/hostkeys")
    (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
    (tramp-parse-rhosts      "~/.rhosts")
    (tramp-parse-rhosts      "~/.shosts")
    (tramp-parse-shosts      "~/.ssh/known_hosts")
    (tramp-parse-sconfig     "~/.ssh/config")
    (tramp-parse-shostkeys   "~/.ssh2/hostkeys")
    (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
  "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")

437
;;;###tramp-autoload
438 439 440 441
(defconst tramp-completion-function-alist-telnet
  '((tramp-parse-hosts "/etc/hosts"))
  "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")

442
;;;###tramp-autoload
443 444 445 446
(defconst tramp-completion-function-alist-su
  '((tramp-parse-passwd "/etc/passwd"))
  "Default list of (FUNCTION FILE) pairs to be examined for su methods.")

447
;;;###tramp-autoload
448
(defconst tramp-completion-function-alist-putty
Michael Albinus's avatar
Michael Albinus committed
449 450 451 452 453
  `((tramp-parse-putty
     ,(if (memq system-type '(windows-nt))
	  "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"
	"~/.putty/sessions")))
  "Default list of (FUNCTION REGISTRY) pairs to be examined for putty sessions.")
454

455 456 457 458 459 460 461 462 463 464 465 466 467 468
;;;###tramp-autoload
(eval-after-load 'tramp
  '(progn
     (tramp-set-completion-function "rcp" tramp-completion-function-alist-rsh)
     (tramp-set-completion-function "remcp" tramp-completion-function-alist-rsh)
     (tramp-set-completion-function "scp" tramp-completion-function-alist-ssh)
     (tramp-set-completion-function "scpx" tramp-completion-function-alist-ssh)
     (tramp-set-completion-function "rsync" tramp-completion-function-alist-ssh)
     (tramp-set-completion-function "rsh" tramp-completion-function-alist-rsh)
     (tramp-set-completion-function "remsh" tramp-completion-function-alist-rsh)
     (tramp-set-completion-function "ssh" tramp-completion-function-alist-ssh)
     (tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh)
     (tramp-set-completion-function
      "telnet" tramp-completion-function-alist-telnet)
469
     (tramp-set-completion-function "nc" tramp-completion-function-alist-telnet)
470 471 472 473 474 475 476 477 478
     (tramp-set-completion-function "su" tramp-completion-function-alist-su)
     (tramp-set-completion-function "sudo" tramp-completion-function-alist-su)
     (tramp-set-completion-function "ksu" tramp-completion-function-alist-su)
     (tramp-set-completion-function
      "krlogin" tramp-completion-function-alist-rsh)
     (tramp-set-completion-function "plink" tramp-completion-function-alist-ssh)
     (tramp-set-completion-function
      "plinkx" tramp-completion-function-alist-putty)
     (tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh)
479
     (tramp-set-completion-function "psftp" tramp-completion-function-alist-ssh)
480
     (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh)))
481 482 483 484 485 486

;; "getconf PATH" yields:
;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
;; GNU/Linux (Debian, Suse): /bin:/usr/bin
;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
487
;; Darwin: /usr/bin:/bin:/usr/sbin:/sbin
488
;; IRIX64: /usr/bin
489
;;;###tramp-autoload
490
(defcustom tramp-remote-path
491 492 493
  '(tramp-default-remote-path "/bin" "/usr/bin" "/sbin" "/usr/sbin"
    "/usr/local/bin" "/usr/local/sbin" "/local/bin" "/local/freeware/bin"
    "/local/gnu/bin" "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin"
Michael Albinus's avatar
Michael Albinus committed
494
    "/opt/bin" "/opt/sbin" "/opt/local/bin")
495
  "List of directories to search for executables on remote host.
496 497 498 499 500 501 502 503 504
For every remote host, this variable will be set buffer local,
keeping the list of existing directories on that host.

You can use `~' in this list, but when searching for a shell which groks
tilde expansion, all directory names starting with `~' will be ignored.

`Default Directories' represent the list of directories given by
the command \"getconf PATH\".  It is recommended to use this
entry on top of this list, because these are the default
505 506 507
directories for POSIX compatible commands.  On remote hosts which
do not offer the getconf command (like cygwin), the value
\"/bin:/usr/bin\" is used instead of.
508 509 510 511 512 513 514 515 516

`Private Directories' are the settings of the $PATH environment,
as given in your `~/.profile'."
  :group 'tramp
  :type '(repeat (choice
		  (const :tag "Default Directories" tramp-default-remote-path)
		  (const :tag "Private Directories" tramp-own-remote-path)
		  (string :tag "Directory"))))

517
;;;###tramp-autoload
518
(defcustom tramp-remote-process-environment
Michael Albinus's avatar
Michael Albinus committed
519
  `("TMOUT=0" "LC_CTYPE=''"
520 521
    ,(format "TERM=%s" tramp-terminal-type)
    ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version)
522
    "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH=" "PAGER=cat"
523
    "autocorrect=" "correct=")
524
  "List of environment variables to be set on the remote host.
525 526

Each element should be a string of the form ENVVARNAME=VALUE.  An
Paul Eggert's avatar
Paul Eggert committed
527
entry ENVVARNAME= disables the corresponding environment variable,
528 529 530
which might have been set in the init files like ~/.profile.

Special handling is applied to the PATH environment, which should
531
not be set here. Instead, it should be set via `tramp-remote-path'."
532
  :group 'tramp
Michael Albinus's avatar
Michael Albinus committed
533
  :version "24.4"
534 535
  :type '(repeat string))

536
;;;###tramp-autoload
537
(defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
538
  "Alist specifying extra arguments to pass to the remote shell.
539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585
Entries are (REGEXP . ARGS) where REGEXP is a regular expression
matching the shell file name and ARGS is a string specifying the
arguments.

This variable is only used when Tramp needs to start up another shell
for tilde expansion.  The extra arguments should typically prevent the
shell from reading its init file."
  :group 'tramp
  ;; This might be the wrong way to test whether the widget type
  ;; `alist' is available.  Who knows the right way to test it?
  :type (if (get 'alist 'widget-type)
	    '(alist :key-type string :value-type string)
	  '(repeat (cons string string))))

(defconst tramp-actions-before-shell
  '((tramp-login-prompt-regexp tramp-action-login)
    (tramp-password-prompt-regexp tramp-action-password)
    (tramp-wrong-passwd-regexp tramp-action-permission-denied)
    (shell-prompt-pattern tramp-action-succeed)
    (tramp-shell-prompt-pattern tramp-action-succeed)
    (tramp-yesno-prompt-regexp tramp-action-yesno)
    (tramp-yn-prompt-regexp tramp-action-yn)
    (tramp-terminal-prompt-regexp tramp-action-terminal)
    (tramp-process-alive-regexp tramp-action-process-alive))
  "List of pattern/action pairs.
Whenever a pattern matches, the corresponding action is performed.
Each item looks like (PATTERN ACTION).

The PATTERN should be a symbol, a variable.  The value of this
variable gives the regular expression to search for.  Note that the
regexp must match at the end of the buffer, \"\\'\" is implicitly
appended to it.

The ACTION should also be a symbol, but a function.  When the
corresponding PATTERN matches, the ACTION function is called.")

(defconst tramp-actions-copy-out-of-band
  '((tramp-password-prompt-regexp tramp-action-password)
    (tramp-wrong-passwd-regexp tramp-action-permission-denied)
    (tramp-copy-failed-regexp tramp-action-permission-denied)
    (tramp-process-alive-regexp tramp-action-out-of-band))
  "List of pattern/action pairs.
This list is used for copying/renaming with out-of-band methods.

See `tramp-actions-before-shell' for more info.")

(defconst tramp-uudecode
Michael Albinus's avatar
Michael Albinus committed
586 587 588
  "(echo begin 600 %t; tail -n +2) | uudecode
cat %t
rm -f %t"
589 590 591 592 593 594 595 596 597 598
  "Shell function to implement `uudecode' to standard output.
Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
for this or `uudecode -p', but some systems don't, and for them
we have this shell function.")

(defconst tramp-perl-file-truename
  "%s -e '
use File::Spec;
use Cwd \"realpath\";

599 600 601 602 603
sub myrealpath {
    my ($file) = @_;
    return realpath($file) if -e $file;
}

604 605
sub recursive {
    my ($volume, @dirs) = @_;
606
    my $real = myrealpath(File::Spec->catpath(
607 608 609 610 611 612 613 614 615 616 617 618 619
                   $volume, File::Spec->catdir(@dirs), \"\"));
    if ($real) {
        my ($vol, $dir) = File::Spec->splitpath($real, 1);
        return ($vol, File::Spec->splitdir($dir));
    }
    else {
        my $last = pop(@dirs);
        ($volume, @dirs) = recursive($volume, @dirs);
        push(@dirs, $last);
        return ($volume, @dirs);
    }
}

620
$result = myrealpath($ARGV[0]);
621 622 623 624 625 626 627
if (!$result) {
    my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
    ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));

    $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
}

628
$result =~ s/\"/\\\\\"/g;
629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669
print \"\\\"$result\\\"\\n\";
' \"$1\" 2>/dev/null"
  "Perl script to produce output suitable for use with `file-truename'
on the remote file system.
Escape sequence %s is replaced with name of Perl binary.
This string is passed to `format', so percent characters need to be doubled.")

(defconst tramp-perl-file-name-all-completions
  "%s -e 'sub case {
 my $str = shift;
 if ($ARGV[2]) {
  return lc($str);
 }
 else {
  return $str;
 }
}
opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
@files = readdir(d); closedir(d);
foreach $f (@files) {
 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
  if (-d \"$ARGV[0]/$f\") {
   print \"$f/\\n\";
  }
  else {
   print \"$f\\n\";
  }
 }
}
print \"ok\\n\"
' \"$1\" \"$2\" \"$3\" 2>/dev/null"
  "Perl script to produce output suitable for use with
`file-name-all-completions' on the remote file system.  Escape
sequence %s is replaced with name of Perl binary.  This string is
passed to `format', so percent characters need to be doubled.")

;; Perl script to implement `file-attributes' in a Lisp `read'able
;; output.  If you are hacking on this, note that you get *no* output
;; unless this spits out a complete line, including the '\n' at the
;; end.
;; The device number is returned as "-1", because there will be a virtual
670
;; device number set in `tramp-sh-handle-file-attributes'.
671 672 673 674 675 676 677 678 679 680
(defconst tramp-perl-file-attributes
  "%s -e '
@stat = lstat($ARGV[0]);
if (!@stat) {
    print \"nil\\n\";
    exit 0;
}
if (($stat[2] & 0170000) == 0120000)
{
    $type = readlink($ARGV[0]);
Michael Albinus's avatar
Michael Albinus committed
681
    $type =~ s/\"/\\\\\"/g;
682 683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703 704 705 706 707 708 709 710 711 712 713 714 715 716 717 718 719 720 721 722 723 724 725 726 727 728 729 730
    $type = \"\\\"$type\\\"\";
}
elsif (($stat[2] & 0170000) == 040000)
{
    $type = \"t\";
}
else
{
    $type = \"nil\"
};
$uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
$gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
printf(
    \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
    $type,
    $stat[3],
    $uid,
    $gid,
    $stat[8] >> 16 & 0xffff,
    $stat[8] & 0xffff,
    $stat[9] >> 16 & 0xffff,
    $stat[9] & 0xffff,
    $stat[10] >> 16 & 0xffff,
    $stat[10] & 0xffff,
    $stat[7],
    $stat[2],
    $stat[1] >> 16 & 0xffff,
    $stat[1] & 0xffff
);' \"$1\" \"$2\" 2>/dev/null"
  "Perl script to produce output suitable for use with `file-attributes'
on the remote file system.
Escape sequence %s is replaced with name of Perl binary.
This string is passed to `format', so percent characters need to be doubled.")

(defconst tramp-perl-directory-files-and-attributes
  "%s -e '
chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
@list = readdir(DIR);
closedir(DIR);
$n = scalar(@list);
printf(\"(\\n\");
for($i = 0; $i < $n; $i++)
{
    $filename = $list[$i];
    @stat = lstat($filename);
    if (($stat[2] & 0170000) == 0120000)
    {
        $type = readlink($filename);
Michael Albinus's avatar
Michael Albinus committed
731
        $type =~ s/\"/\\\\\"/g;
732 733 734 735 736 737 738 739 740 741 742 743
        $type = \"\\\"$type\\\"\";
    }
    elsif (($stat[2] & 0170000) == 040000)
    {
        $type = \"t\";
    }
    else
    {
        $type = \"nil\"
    };
    $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
    $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
Michael Albinus's avatar
Michael Albinus committed
744
    $filename =~ s/\"/\\\\\"/g;
745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763 764 765 766 767 768 769 770 771 772 773 774 775 776 777 778 779 780 781 782 783 784 785 786 787 788 789 790
    printf(
        \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
        $filename,
        $type,
        $stat[3],
        $uid,
        $gid,
        $stat[8] >> 16 & 0xffff,
        $stat[8] & 0xffff,
        $stat[9] >> 16 & 0xffff,
        $stat[9] & 0xffff,
        $stat[10] >> 16 & 0xffff,
        $stat[10] & 0xffff,
        $stat[7],
        $stat[2],
        $stat[1] >> 16 & 0xffff,
        $stat[1] & 0xffff,
        $stat[0] >> 16 & 0xffff,
        $stat[0] & 0xffff);
}
printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
  "Perl script implementing `directory-files-attributes' as Lisp `read'able
output.
Escape sequence %s is replaced with name of Perl binary.
This string is passed to `format', so percent characters need to be doubled.")

;; These two use base64 encoding.
(defconst tramp-perl-encode-with-module
  "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
  "Perl program to use for encoding a file.
Escape sequence %s is replaced with name of Perl binary.
This string is passed to `format', so percent characters need to be doubled.
This implementation requires the MIME::Base64 Perl module to be installed
on the remote host.")

(defconst tramp-perl-decode-with-module
  "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
  "Perl program to use for decoding a file.
Escape sequence %s is replaced with name of Perl binary.
This string is passed to `format', so percent characters need to be doubled.
This implementation requires the MIME::Base64 Perl module to be installed
on the remote host.")

(defconst tramp-perl-encode
  "%s -e '
# This script contributed by Juanma Barranquero <lektu@terra.es>.
Paul Eggert's avatar
Paul Eggert committed
791
# Copyright (C) 2002-2016 Free Software Foundation, Inc.
792 793 794 795 796 797 798
use strict;

my %%trans = do {
    my $i = 0;
    map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
      split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
};
799
my $data;
800 801 802

# We read in chunks of 54 bytes, to generate output lines
# of 72 chars (plus end of line)
803
while (read STDIN, $data, 54) {
804 805 806 807 808 809 810 811 812 813 814 815 816 817 818 819 820 821 822 823 824 825 826 827 828
    my $pad = q();

    # Only for the last chunk, and only if did not fill the last three-byte packet
    if (eof) {
        my $mod = length($data) %% 3;
        $pad = q(=) x (3 - $mod) if $mod;
    }

    # Not the fastest method, but it is simple: unpack to binary string, split
    # by groups of 6 bits and convert back from binary to byte; then map into
    # the translation table
    print
      join q(),
        map($trans{$_},
            (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
              $pad,
                qq(\\n);
}' 2>/dev/null"
  "Perl program to use for encoding a file.
Escape sequence %s is replaced with name of Perl binary.
This string is passed to `format', so percent characters need to be doubled.")

(defconst tramp-perl-decode
  "%s -e '
# This script contributed by Juanma Barranquero <lektu@terra.es>.
Paul Eggert's avatar
Paul Eggert committed
829
# Copyright (C) 2002-2016 Free Software Foundation, Inc.
830 831 832 833 834 835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869
use strict;

my %%trans = do {
    my $i = 0;
    map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
      split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
};

my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;

binmode(\\*STDOUT);

# We are going to accumulate into $pending to accept any line length
# (we do not check they are <= 76 chars as the RFC says)
my $pending = q();

while (my $data = <STDIN>) {
    chomp $data;

    # If we find one or two =, we have reached the end and
    # any following data is to be discarded
    my $finished = $data =~ s/(==?).*/$1/;
    $pending .= $data;

    my $len = length($pending);
    my $chunk = substr($pending, 0, $len & ~3);
    $pending = substr($pending, $len & ~3 + 1);

    # Easy method: translate from chars to (pregenerated) six-bit packets, join,
    # split in 8-bit chunks and convert back to char.
    print join q(),
      map $bytes{$_},
        ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);

    last if $finished;
}' 2>/dev/null"
  "Perl program to use for decoding a file.
Escape sequence %s is replaced with name of Perl binary.
This string is passed to `format', so percent characters need to be doubled.")

870 871 872 873 874 875 876 877 878 879
(defconst tramp-perl-pack
  "%s -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
  "Perl program to use for encoding a file.
Escape sequence %s is replaced with name of Perl binary.")

(defconst tramp-perl-unpack
  "%s -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"
  "Perl program to use for decoding a file.
Escape sequence %s is replaced with name of Perl binary.")

880 881 882 883 884 885 886 887 888 889 890 891 892 893 894 895 896 897 898 899 900 901 902 903 904 905 906 907 908 909 910 911 912 913 914 915 916 917 918 919 920 921 922 923 924 925 926 927 928 929 930 931 932 933 934 935 936 937 938 939 940 941 942 943 944 945
(defconst tramp-awk-encode
  "od -v -t x1 -A n | busybox awk '\\
BEGIN {
  b64 = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"
  b16 = \"0123456789abcdef\"
}
{
  for (c=1; c<=length($0); c++) {
    d=index(b16, substr($0,c,1))
    if (d--) {
      for (b=1; b<=4; b++) {
        o=o*2+int(d/8); d=(d*2)%%16
        if (++obc==6) {
          printf substr(b64,o+1,1)
          if (++rc>75) { printf \"\\n\"; rc=0 }
          obc=0; o=0
        }
      }
    }
  }
}
END {
  if (obc) {
    tail=(obc==2) ? \"==\\n\" : \"=\\n\"
    while (obc++<6) { o=o*2 }
    printf \"%%c\", substr(b64,o+1,1)
  } else {
    tail=\"\\n\"
  }
  printf tail
}'"
  "Awk program to use for encoding a file.
This string is passed to `format', so percent characters need to be doubled.")

(defconst tramp-awk-decode
  "busybox awk '\\
BEGIN {
  b64 = \"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/\"
}
{
  for (i=1; i<=length($0); i++) {
    c=index(b64, substr($0,i,1))
    if(c--) {
      for(b=0; b<6; b++) {
        o=o*2+int(c/32); c=(c*2)%%64
        if(++obc==8) {
          if (o) {
            printf \"%%c\", o
          } else {
            system(\"dd if=/dev/zero bs=1 count=1 2>/dev/null\")
          }
          obc=0; o=0
        }
      }
    }
  }
}'"
  "Awk program to use for decoding a file.
This string is passed to `format', so percent characters need to be doubled.")

(defconst tramp-awk-coding-test
  "test -c /dev/zero && \
od -v -t x1 -A n </dev/null && \
busybox awk '{}' </dev/null"
  "Test command for checking `tramp-awk-encode' and `tramp-awk-decode'.")

946 947 948 949 950 951
(defconst tramp-stat-marker "/////"
  "Marker in stat commands for file attributes.")

(defconst tramp-stat-quoted-marker "\\/\\/\\/\\/\\/"
  "Quoted marker in stat commands for file attributes.")

952 953 954 955 956 957 958 959 960 961 962 963 964 965 966 967 968 969 970 971 972
(defconst tramp-vc-registered-read-file-names
  "echo \"(\"
while read file; do
    if %s \"$file\"; then
	echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
    else
	echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
    fi
    if %s \"$file\"; then
	echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
    else
	echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
    fi
done
echo \")\""
  "Script to check existence of VC related files.
It must be send formatted with two strings; the tests for file
existence, and file readability.  Input shall be read via
here-document, otherwise the command could exceed maximum length
of command line.")

Michael Albinus's avatar
Michael Albinus committed
973
;; New handlers should be added here.
974
(defconst tramp-sh-file-name-handler-alist
Michael Albinus's avatar
Michael Albinus committed
975
  '(;; `access-file' performed by default handler.
976
    (add-name-to-file . tramp-sh-handle-add-name-to-file)
Michael Albinus's avatar
Michael Albinus committed
977
    ;; `byte-compiler-base-file-name' performed by default handler.
978
    (copy-directory . tramp-sh-handle-copy-directory)
Michael Albinus's avatar
Michael Albinus committed
979
    (copy-file . tramp-sh-handle-copy-file)
980 981
    (delete-directory . tramp-sh-handle-delete-directory)
    (delete-file . tramp-sh-handle-delete-file)
Michael Albinus's avatar
Michael Albinus committed
982
    ;; `diff-latest-backup-file' performed by default handler.
983
    (directory-file-name . tramp-handle-directory-file-name)
Michael Albinus's avatar
Michael Albinus committed
984 985 986 987 988
    (directory-files . tramp-handle-directory-files)
    (directory-files-and-attributes
     . tramp-sh-handle-directory-files-and-attributes)
    (dired-compress-file . tramp-sh-handle-dired-compress-file)
    (dired-uncache . tramp-handle-dired-uncache)
989
    (expand-file-name . tramp-sh-handle-expand-file-name)
Michael Albinus's avatar
Michael Albinus committed
990 991 992 993
    (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
    (file-acl . tramp-sh-handle-file-acl)
    (file-attributes . tramp-sh-handle-file-attributes)
    (file-directory-p . tramp-sh-handle-file-directory-p)
994
    (file-equal-p . tramp-handle-file-equal-p)
Michael Albinus's avatar
Michael Albinus committed
995 996
    (file-executable-p . tramp-sh-handle-file-executable-p)
    (file-exists-p . tramp-sh-handle-file-exists-p)
997
    (file-in-directory-p . tramp-handle-file-in-directory-p)
998
    (file-local-copy . tramp-sh-handle-file-local-copy)
Michael Albinus's avatar
Michael Albinus committed
999 1000 1001 1002 1003 1004 1005 1006 1007 1008
    (file-modes . tramp-handle-file-modes)
    (file-name-all-completions . tramp-sh-handle-file-name-all-completions)
    (file-name-as-directory . tramp-handle-file-name-as-directory)
    (file-name-completion . tramp-handle-file-name-completion)
    (file-name-directory . tramp-handle-file-name-directory)
    (file-name-nondirectory . tramp-handle-file-name-nondirectory)
    ;; `file-name-sans-versions' performed by default handler.
    (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p)
    (file-notify-add-watch . tramp-sh-handle-file-notify-add-watch)
    (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
1009
    (file-notify-valid-p . tramp-handle-file-notify-valid-p)
Michael Albinus's avatar
Michael Albinus committed
1010 1011 1012
    (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p)
    (file-readable-p . tramp-sh-handle-file-readable-p)
    (file-regular-p . tramp-handle-file-regular-p)
1013
    (file-remote-p . tramp-handle-file-remote-p)
Michael Albinus's avatar
Michael Albinus committed
1014 1015 1016 1017 1018 1019 1020 1021
    (file-selinux-context . tramp-sh-handle-file-selinux-context)
    (file-symlink-p . tramp-handle-file-symlink-p)
    (file-truename . tramp-sh-handle-file-truename)
    (file-writable-p . tramp-sh-handle-file-writable-p)
    (find-backup-file-name . tramp-handle-find-backup-file-name)
    ;; `find-file-noselect' performed by default handler.
    ;; `get-file-buffer' performed by default handler.
    (insert-directory . tramp-sh-handle-insert-directory)
1022
    (insert-file-contents . tramp-handle-insert-file-contents)
Michael Albinus's avatar
Michael Albinus committed
1023
    (load . tramp-handle-load)
1024
    (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
Michael Albinus's avatar
Michael Albinus committed
1025 1026 1027 1028
    (make-directory . tramp-sh-handle-make-directory)
    (make-symbolic-link . tramp-sh-handle-make-symbolic-link)
    (process-file . tramp-sh-handle-process-file)
    (rename-file . tramp-sh-handle-rename-file)
1029
    (set-file-acl . tramp-sh-handle-set-file-acl)
Michael Albinus's avatar
Michael Albinus committed
1030 1031 1032 1033 1034 1035 1036
    (set-file-modes . tramp-sh-handle-set-file-modes)
    (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context)
    (set-file-times . tramp-sh-handle-set-file-times)
    (set-visited-file-modtime . tramp-sh-handle-set-visited-file-modtime)
    (shell-command . tramp-handle-shell-command)
    (start-file-process . tramp-sh-handle-start-file-process)
    (substitute-in-file-name . tramp-handle-substitute-in-file-name)
1037
    (unhandled-file-name-directory . ignore)
1038
    (vc-registered . tramp-sh-handle-vc-registered)
Michael Albinus's avatar
Michael Albinus committed
1039 1040
    (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime)
    (write-region . tramp-sh-handle-write-region))
1041 1042 1043 1044 1045 1046 1047 1048 1049 1050
  "Alist of handler functions.
Operations not mentioned here will be handled by the normal Emacs functions.")

;; This must be the last entry, because `identity' always matches.
;;;###tramp-autoload
(add-to-list 'tramp-foreign-file-name-handler-alist
	     '(identity . tramp-sh-file-name-handler) 'append)

;;; File Name Handler Functions:

1051
(defun tramp-sh-handle-make-symbolic-link
1052 1053 1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066 1067 1068 1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085 1086 1087 1088 1089 1090 1091 1092
  (filename linkname &optional ok-if-already-exists)
  "Like `make-symbolic-link' for Tramp files.
If LINKNAME is a non-Tramp file, it is used verbatim as the target of
the symlink.  If LINKNAME is a Tramp file, only the localname component is
used as the target of the symlink.

If LINKNAME is a Tramp file and the localname component is relative, then
it is expanded first, before the localname component is taken.  Note that
this can give surprising results if the user/host for the source and
target of the symlink differ."
  (with-parsed-tramp-file-name linkname l
    (let ((ln (tramp-get-remote-ln l))
	  (cwd (tramp-run-real-handler
		'file-name-directory (list l-localname))))
      (unless ln
	(tramp-error
	 l 'file-error
	 "Making a symbolic link.  ln(1) does not exist on the remote host."))

      ;; Do the 'confirm if exists' thing.
      (when (file-exists-p linkname)
	;; What to do?
	(if (or (null ok-if-already-exists) ; not allowed to exist
		(and (numberp ok-if-already-exists)
		     (not (yes-or-no-p
			   (format
			    "File %s already exists; make it a link anyway? "
			    l-localname)))))
	    (tramp-error
	     l 'file-already-exists "File %s already exists" l-localname)
	  (delete-file linkname)))

      ;; If FILENAME is a Tramp name, use just the localname component.
      (when (tramp-tramp-file-p filename)
	(setq filename
	      (tramp-file-name-localname
	       (tramp-dissect-file-name (expand-file-name filename)))))

      (tramp-flush-file-property l (file-name-directory l-localname))
      (tramp-flush-file-property l l-localname)

1093 1094 1095
      ;; Right, they are on the same host, regardless of user, method,
      ;; etc.  We now make the link on the remote machine. This will
      ;; occur as the user that FILENAME belongs to.
1096 1097 1098 1099 1100 1101 1102 1103 1104 1105 1106 1107 1108
      (and (tramp-send-command-and-check
            l (format "cd %s" (tramp-shell-quote-argument cwd)))
           (tramp-send-command-and-check
            l (format
               "%s -sf %s %s"
               ln
               (tramp-shell-quote-argument filename)
               ;; The command could exceed PATH_MAX, so we use
               ;; relative file names.  However, relative file names
               ;; could start with "-".  `tramp-shell-quote-argument'
               ;; does not handle this, we must do it ourselves.
               (tramp-shell-quote-argument
                (concat "./" (file-name-nondirectory l-localname)))))))))
1109

1110
(defun tramp-sh-handle-file-truename (filename)
1111
  "Like `file-truename' for Tramp files."
1112 1113 1114 1115 1116
  (format
   "%s%s"
   (with-parsed-tramp-file-name (expand-file-name filename) nil
     (tramp-make-tramp-file-name
      method user host
1117
      (with-tramp-file-property v localname "file-truename"
1118
	(let ((result nil))			; result steps in reverse order
1119
	  (tramp-message v 4 "Finding true name for `%s'" filename)
1120 1121 1122
	  (cond
	   ;; Use GNU readlink --canonicalize-missing where available.
	   ((tramp-get-remote-readlink v)
1123 1124 1125 1126 1127 1128 1129 1130
	    (tramp-send-command-and-check
	     v
	     (format "%s --canonicalize-missing %s"
		     (tramp-get-remote-readlink v)
		     (tramp-shell-quote-argument localname)))
	    (with-current-buffer (tramp-get-connection-buffer v)
	      (goto-char (point-min))
	      (setq result (buffer-substring (point-min) (point-at-eol)))))
1131 1132 1133 1134 1135 1136 1137 1138 1139 1140 1141 1142 1143

	   ;; Use Perl implementation.
	   ((and (tramp-get-remote-perl v)
		 (tramp-get-connection-property v "perl-file-spec" nil)
		 (tramp-get-connection-property v "perl-cwd-realpath" nil))
	    (tramp-maybe-send-script
	     v tramp-perl-file-truename "tramp_perl_file_truename")
	    (setq result
		  (tramp-send-command-and-read
		   v
		   (format "tramp_perl_file_truename %s"
			   (tramp-shell-quote-argument localname)))))

1144 1145
	   ;; Do it yourself.
	   (t (let ((steps (split-string localname "/" 'omit))
1146 1147 1148 1149 1150 1151 1152 1153 1154
		    (thisstep nil)
		    (numchase 0)
		    ;; Don't make the following value larger than
		    ;; necessary.  People expect an error message in a
		    ;; timely fashion when something is wrong;
		    ;; otherwise they might think that Emacs is hung.
		    ;; Of course, correctness has to come first.
		    (numchase-limit 20)
		    symlink-target)
1155 1156 1157 1158 1159 1160 1161 1162 1163 1164 1165 1166 1167 1168 1169 1170 1171
		(while (and steps (< numchase numchase-limit))
		  (setq thisstep (pop steps))
		  (tramp-message
		   v 5 "Check %s"
		   (mapconcat 'identity
			      (append '("") (reverse result) (list thisstep))
			      "/"))
		  (setq symlink-target
			(nth 0 (file-attributes
				(tramp-make-tramp-file-name
				 method user host
				 (mapconcat 'identity
					    (append '("")
						    (reverse result)
						    (list thisstep))
					    "/")))))
		  (cond ((string= "." thisstep)
1172
			 (tramp-message v 5 "Ignoring step `.'"))
1173
			((string= ".." thisstep)
1174
			 (tramp-message v 5 "Processing step `..'")
1175 1176 1177 1178 1179 1180 1181 1182 1183 1184 1185 1186 1187 1188 1189
			 (pop result))
			((stringp symlink-target)
			 ;; It's a symlink, follow it.
			 (tramp-message
			  v 5 "Follow symlink to %s" symlink-target)
			 (setq numchase (1+ numchase))
			 (when (file-name-absolute-p symlink-target)
			   (setq result nil))
			 ;; If the symlink was absolute, we'll get a
			 ;; string like "/user@host:/some/target";
			 ;; extract the "/some/target" part from it.
			 (when (tramp-tramp-file-p symlink-target)
			   (unless (tramp-equal-remote filename symlink-target)
			     (tramp-error
			      v 'file-error
1190
			      "Symlink target `%s' on wrong host"
1191 1192 1193
			      symlink-target))
			   (setq symlink-target localname))
			 (setq steps
1194 1195
			       (append
				(split-string symlink-target "/" 'omit)	steps)))
1196 1197 1198 1199 1200 1201 1202 1203 1204 1205 1206 1207 1208
			(t
			 ;; It's a file.
			 (setq result (cons thisstep result)))))
		(when (>= numchase numchase-limit)
		  (tramp-error
		   v 'file-error
		   "Maximum number (%d) of symlinks exceeded" numchase-limit))
		(setq result (reverse result))
		;; Combine list to form string.
		(setq result
		      (if result
			  (mapconcat 'identity (cons "" result) "/")
			"/"))
1209 1210
		(when (string= "" result)
		  (setq result "/")))))
1211

1212
	  (tramp-message v 4 "True name of `%s' is `%s'" localname result)
1213 1214 1215 1216
	  result))))

   ;; Preserve trailing "/".
   (if (string-equal (file-name-nondirectory filename) "") "/" "")))
1217 1218 1219

;; Basic functions.

1220
(defun tramp-sh-handle-file-exists-p (filename)
1221 1222
  "Like `file-exists-p' for Tramp files."
  (with-parsed-tramp-file-name filename nil
1223
    (with-tramp-file-property v localname "file-exists-p"
1224 1225 1226 1227 1228 1229 1230 1231 1232 1233 1234
      (or (not (null (tramp-get-file-property
                      v localname "file-attributes-integer" nil)))
          (not (null (tramp-get-file-property
                      v localname "file-attributes-string" nil)))
	  (tramp-send-command-and-check
	   v
	   (format
	    "%s %s"
	    (tramp-get-file-exists-command v)
	    (tramp-shell-quote-argument localname)))))))

1235
(defun tramp-sh-handle-file-attributes (filename &optional id-format)
1236 1237
  "Like `file-attributes' for Tramp files."
  (unless id-format (setq id-format 'integer))