tramp-sh.el 229 KB
Newer Older
Michael Albinus's avatar
Michael Albinus committed
1
;;; tramp-sh.el --- Tramp access functions for (s)sh-like connections  -*- lexical-binding:t -*-
2

Paul Eggert's avatar
Paul Eggert committed
3
;; Copyright (C) 1998-2019 Free Software Foundation, Inc.
4 5 6 7 8

;; (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>
9
;; Maintainer: Michael Albinus <michael.albinus@gmx.de>
10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
;; 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
26
;; along with GNU Emacs.  If not, see <https://www.gnu.org/licenses/>.
27 28 29

;;; Code:

30
(eval-when-compile (require 'cl-lib))
31 32
(require 'tramp)

Michael Albinus's avatar
Michael Albinus committed
33 34
;; Pacify byte-compiler.
(eval-when-compile
Michael Albinus's avatar
Michael Albinus committed
35
  (require 'dired))
Michael Albinus's avatar
Michael Albinus committed
36 37 38

(declare-function dired-remove-file "dired-aux")
(defvar dired-compress-file-suffixes)
39 40 41 42
(defvar vc-handled-backends)
(defvar vc-bzr-program)
(defvar vc-git-program)
(defvar vc-hg-program)
43 44

(defcustom tramp-inline-compress-start-size 4096
45
  "The minimum size of compressing where inline transfer.
Paul Eggert's avatar
Paul Eggert committed
46
When inline transfer, compress transferred data of file
47 48 49
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
Michael Albinus's avatar
Michael Albinus committed
50
  :type '(choice (const nil) integer))
51 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
  :group 'tramp
Michael Albinus's avatar
Michael Albinus committed
57
  :type '(choice (const nil) integer))
58 59 60

;;;###tramp-autoload
(defcustom tramp-terminal-type "dumb"
61
  "Value of TERM environment variable for logging in to remote host.
62 63 64 65
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
Michael Albinus's avatar
Michael Albinus committed
66
  :type 'string)
67

Michael Albinus's avatar
Michael Albinus committed
68
(defcustom tramp-histfile-override "~/.tramp_history"
69
  "When invoking a shell, override the HISTFILE with this value.
70 71 72
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.
73

Michael Albinus's avatar
Michael Albinus committed
74 75 76 77
The value t 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\"."
78
  :group 'tramp
Michael Albinus's avatar
Michael Albinus committed
79
  :version "25.2"
80
  :type '(choice (const :tag "Do not override HISTFILE" nil)
81
                 (const :tag "Unset HISTFILE" t)
Michael Albinus's avatar
Michael Albinus committed
82
                 (string :tag "Redirect to a file")))
83

84
;;;###tramp-autoload
Michael Albinus's avatar
Michael Albinus committed
85 86 87 88 89
(defconst tramp-display-escape-sequence-regexp "\e[[;0-9]+m"
  "Terminal control escape sequences for display attributes.")

(defconst tramp-device-escape-sequence-regexp "\e[[0-9]+n"
  "Terminal control escape sequences for device status.")
90

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

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

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

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

110 111 112 113
(defcustom tramp-use-ssh-controlmaster-options t
  "Whether to use `tramp-ssh-controlmaster-options'."
  :group 'tramp
  :version "24.4"
Michael Albinus's avatar
Michael Albinus committed
114
  :type 'boolean)
115 116 117 118 119

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

If it is a string, it should have the form
120
\"-o ControlMaster=auto -o ControlPath=\\='tramp.%%r@%%h:%%p\\='
121 122 123 124 125 126 127 128 129
-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
;; Initialize `tramp-methods' with the supported methods.
;;;###tramp-autoload
132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387
(tramp--with-startup
 (add-to-list 'tramp-methods
              '("rcp"
                (tramp-login-program        "rsh")
                (tramp-login-args           (("%h") ("-l" "%u")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))
                (tramp-copy-program         "rcp")
                (tramp-copy-args            (("-p" "%k") ("-r")))
                (tramp-copy-keep-date       t)
                (tramp-copy-recursive       t)))
 (add-to-list 'tramp-methods
              '("remcp"
                (tramp-login-program        "remsh")
                (tramp-login-args           (("%h") ("-l" "%u")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))
                (tramp-copy-program         "rcp")
                (tramp-copy-args            (("-p" "%k")))
                (tramp-copy-keep-date       t)))
 (add-to-list 'tramp-methods
              '("scp"
                (tramp-login-program        "ssh")
                (tramp-login-args           (("-l" "%u") ("-p" "%p") ("%c")
				             ("-e" "none") ("%h")))
                (tramp-async-args           (("-q")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))
                (tramp-copy-program         "scp")
                (tramp-copy-args            (("-P" "%p") ("-p" "%k") ("-q") ("-r") ("%c")))
                (tramp-copy-keep-date       t)
                (tramp-copy-recursive       t)))
 (add-to-list 'tramp-methods
              '("scpx"
                (tramp-login-program        "ssh")
                (tramp-login-args           (("-l" "%u") ("-p" "%p") ("%c")
				             ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
                (tramp-async-args           (("-q")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))
                (tramp-copy-program         "scp")
                (tramp-copy-args            (("-P" "%p") ("-p" "%k")
				             ("-q") ("-r") ("%c")))
                (tramp-copy-keep-date       t)
                (tramp-copy-recursive       t)))
 (add-to-list 'tramp-methods
              '("rsync"
                (tramp-login-program        "ssh")
                (tramp-login-args           (("-l" "%u") ("-p" "%p") ("%c")
				             ("-e" "none") ("%h")))
                (tramp-async-args           (("-q")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))
                (tramp-copy-program         "rsync")
                (tramp-copy-args            (("-t" "%k") ("-p") ("-r") ("-s") ("-c")))
                (tramp-copy-env             (("RSYNC_RSH") ("ssh" "%c")))
                (tramp-copy-keep-date       t)
                (tramp-copy-keep-tmpfile    t)
                (tramp-copy-recursive       t)))
 (add-to-list 'tramp-methods
              '("rsh"
                (tramp-login-program        "rsh")
                (tramp-login-args           (("%h") ("-l" "%u")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))))
 (add-to-list 'tramp-methods
              '("remsh"
                (tramp-login-program        "remsh")
                (tramp-login-args           (("%h") ("-l" "%u")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))))
 (add-to-list 'tramp-methods
              '("ssh"
                (tramp-login-program        "ssh")
                (tramp-login-args           (("-l" "%u") ("-p" "%p") ("%c")
				             ("-e" "none") ("%h")))
                (tramp-async-args           (("-q")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))))
 (add-to-list 'tramp-methods
              '("sshx"
                (tramp-login-program        "ssh")
                (tramp-login-args           (("-l" "%u") ("-p" "%p") ("%c")
				             ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
                (tramp-async-args           (("-q")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))))
 (add-to-list 'tramp-methods
              '("telnet"
                (tramp-login-program        "telnet")
                (tramp-login-args           (("%h") ("%p") ("2>/dev/null")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))))
 (add-to-list 'tramp-methods
              '("nc"
                (tramp-login-program        "telnet")
                (tramp-login-args           (("%h") ("%p") ("2>/dev/null")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (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'.
                (tramp-remote-copy-args     (("-l") ("-p" "%r") ("2>/dev/null")))))
 (add-to-list 'tramp-methods
              '("su"
                (tramp-login-program        "su")
                (tramp-login-args           (("-") ("%u")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))
                (tramp-connection-timeout   10)))
 (add-to-list 'tramp-methods
              '("sg"
                (tramp-login-program        "sg")
                (tramp-login-args           (("-") ("%u")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-args    ("-c"))
                (tramp-connection-timeout   10)))
 (add-to-list 'tramp-methods
              '("sudo"
                (tramp-login-program        "sudo")
                ;; 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\"\":")))
                ;; Local $SHELL could be a nasty one, like zsh or fish.  Let's override it.
                (tramp-login-env            (("SHELL") ("/bin/sh")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))
                (tramp-connection-timeout   10)
                (tramp-session-timeout      300)))
 (add-to-list 'tramp-methods
              '("doas"
                (tramp-login-program        "doas")
                (tramp-login-args           (("-u" "%u") ("-s")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-args    ("-c"))
                (tramp-connection-timeout   10)
                (tramp-session-timeout      300)))
 (add-to-list 'tramp-methods
              '("ksu"
                (tramp-login-program        "ksu")
                (tramp-login-args           (("%u") ("-q")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))
                (tramp-connection-timeout   10)))
 (add-to-list 'tramp-methods
              '("krlogin"
                (tramp-login-program        "krlogin")
                (tramp-login-args           (("%h") ("-l" "%u") ("-x")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))))
 (add-to-list 'tramp-methods
              `("plink"
                (tramp-login-program        "plink")
                (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") ("\"")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))))
 (add-to-list 'tramp-methods
              `("plinkx"
                (tramp-login-program        "plink")
                (tramp-login-args           (("-load") ("%h") ("-t") ("\"")
				             (,(format
				                "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
				                tramp-terminal-type
				                tramp-initial-end-of-output))
				             ("/bin/sh") ("\"")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))))
 (add-to-list 'tramp-methods
              `("pscp"
                (tramp-login-program        "plink")
                (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") ("\"")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))
                (tramp-copy-program         "pscp")
                (tramp-copy-args            (("-l" "%u") ("-P" "%p") ("-scp") ("-p" "%k")
				             ("-q") ("-r")))
                (tramp-copy-keep-date       t)
                (tramp-copy-recursive       t)))
 (add-to-list 'tramp-methods
              `("psftp"
                (tramp-login-program        "plink")
                (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") ("\"")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-c"))
                (tramp-copy-program         "pscp")
                (tramp-copy-args            (("-l" "%u") ("-P" "%p") ("-sftp") ("-p" "%k")
				             ("-q")))
                (tramp-copy-keep-date       t)))
 (add-to-list 'tramp-methods
              '("fcp"
                (tramp-login-program        "fsh")
                (tramp-login-args           (("%h") ("-l" "%u") ("sh" "-i")))
                (tramp-remote-shell         "/bin/sh")
                (tramp-remote-shell-login   ("-l"))
                (tramp-remote-shell-args    ("-i") ("-c"))
                (tramp-copy-program         "fcp")
                (tramp-copy-args            (("-p" "%k")))
                (tramp-copy-keep-date       t)))

 (add-to-list 'tramp-default-method-alist
	      `(,tramp-local-host-regexp "\\`root\\'" "su"))

 (add-to-list 'tramp-default-user-alist
	      `(,(concat "\\`" (regexp-opt '("su" "sudo" "doas" "ksu")) "\\'")
	        nil "root"))
 ;; Do not add "ssh" based methods, otherwise ~/.ssh/config would be ignored.
 ;; Do not add "plink" based methods, they ask interactively for the user.
 (add-to-list 'tramp-default-user-alist
	      `(,(concat
		  "\\`"
		  (regexp-opt
		   '("rcp" "remcp" "rsh" "telnet" "nc" "krlogin" "fcp"))
		  "\\'")
	        nil ,(user-login-name))))
388

389
;;;###tramp-autoload
390 391 392 393 394
(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.")

395
;;;###tramp-autoload
396 397 398 399 400 401 402 403 404 405 406 407 408 409 410
(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.")

411
;;;###tramp-autoload
412 413 414 415
(defconst tramp-completion-function-alist-telnet
  '((tramp-parse-hosts "/etc/hosts"))
  "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")

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

Michael Albinus's avatar
Michael Albinus committed
421 422 423 424 425
;;;###tramp-autoload
(defconst tramp-completion-function-alist-sg
  '((tramp-parse-etc-group "/etc/group"))
  "Default list of (FUNCTION FILE) pairs to be examined for sg methods.")

426
;;;###tramp-autoload
427
(defconst tramp-completion-function-alist-putty
Michael Albinus's avatar
Michael Albinus committed
428 429 430 431
  `((tramp-parse-putty
     ,(if (memq system-type '(windows-nt))
	  "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"
	"~/.putty/sessions")))
Michael Albinus's avatar
Michael Albinus committed
432
 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty sessions.")
433

434
;;;###tramp-autoload
435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460
(tramp--with-startup
 (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)
 (tramp-set-completion-function "nc" tramp-completion-function-alist-telnet)
 (tramp-set-completion-function "su" tramp-completion-function-alist-su)
 (tramp-set-completion-function "sudo" tramp-completion-function-alist-su)
 (tramp-set-completion-function "doas" tramp-completion-function-alist-su)
 (tramp-set-completion-function "ksu" tramp-completion-function-alist-su)
 (tramp-set-completion-function "sg" tramp-completion-function-alist-sg)
 (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)
 (tramp-set-completion-function "psftp" tramp-completion-function-alist-ssh)
 (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh))
461 462 463 464

;; "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
Michael Albinus's avatar
Michael Albinus committed
465
;; GNU/Linux (Debian, Suse, RHEL): /bin:/usr/bin
466
;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
467
;; Darwin: /usr/bin:/bin:/usr/sbin:/sbin
468
;; IRIX64: /usr/bin
469
;; QNAP QTS: ---
Michael Albinus's avatar
Michael Albinus committed
470
;;;###tramp-autoload
471
(defcustom tramp-remote-path
472 473 474
  '(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
475
    "/opt/bin" "/opt/sbin" "/opt/local/bin")
476
  "List of directories to search for executables on remote host.
477 478 479 480 481 482 483 484
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
485
entry on head of this list, because these are the default
486 487
directories for POSIX compatible commands.  On remote hosts which
do not offer the getconf command (like cygwin), the value
488
\"/bin:/usr/bin\" is used instead.  This entry is represented in
489
the list by the special value `tramp-default-remote-path'.
490 491

`Private Directories' are the settings of the $PATH environment,
492 493
as given in your `~/.profile'.  This entry is represented in
the list by the special value `tramp-own-remote-path'."
494 495 496 497
  :group 'tramp
  :type '(repeat (choice
		  (const :tag "Default Directories" tramp-default-remote-path)
		  (const :tag "Private Directories" tramp-own-remote-path)
Michael Albinus's avatar
Michael Albinus committed
498
		  (string :tag "Directory"))))
499

Michael Albinus's avatar
Michael Albinus committed
500
;;;###tramp-autoload
501
(defcustom tramp-remote-process-environment
Daniel Pittman's avatar
Daniel Pittman committed
502
  '("ENV=''" "TMOUT=0" "LC_CTYPE=''"
503
    "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH=" "PAGER=cat"
504
    "autocorrect=" "correct=")
505
  "List of environment variables to be set on the remote host.
506 507

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

Daniel Pittman's avatar
Daniel Pittman committed
511 512 513 514 515 516 517 518
Special handling is applied to some environment variables,
which should not be set here:

The PATH environment variable should be set via `tramp-remote-path'.

The TERM environment variable should be set via `tramp-terminal-type'.

The INSIDE_EMACS environment variable will automatically be set
Michael Albinus's avatar
Michael Albinus committed
519
based on the Tramp and Emacs versions, and should not be set here."
520
  :group 'tramp
521
  :version "26.1"
Michael Albinus's avatar
Michael Albinus committed
522
  :type '(repeat string))
523

Michael Albinus's avatar
Michael Albinus committed
524
;;;###tramp-autoload
525
(defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
526
  "Alist specifying extra arguments to pass to the remote shell.
527 528 529 530 531 532 533 534
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
Michael Albinus's avatar
Michael Albinus committed
535
  :type '(alist :key-type regexp :value-type string))
536 537 538 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

(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
570 571 572
  "(echo begin 600 %t; tail -n +2) | uudecode
cat %t
rm -f %t"
573 574 575 576 577 578 579 580 581 582
  "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\";

583 584
sub myrealpath {
    my ($file) = @_;
585
    return realpath($file) if (-e $file || -l $file);
586 587
}

588 589
sub recursive {
    my ($volume, @dirs) = @_;
590
    my $real = myrealpath(File::Spec->catpath(
591 592 593 594 595 596 597 598 599 600 601 602 603
                   $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);
    }
}

604
$result = myrealpath($ARGV[0]);
605 606 607 608 609 610 611
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), \"\");
}

612
$result =~ s/\"/\\\\\"/g;
613 614 615 616 617 618 619 620
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
Michael Albinus's avatar
Michael Albinus committed
621
  "%s -e '
622 623 624
opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
@files = readdir(d); closedir(d);
foreach $f (@files) {
Michael Albinus's avatar
Michael Albinus committed
625 626 627 628 629
 if (-d \"$ARGV[0]/$f\") {
  print \"$f/\\n\";
 }
 else {
  print \"$f\\n\";
630 631 632
 }
}
print \"ok\\n\"
Michael Albinus's avatar
Michael Albinus committed
633
' \"$1\" 2>/dev/null"
634 635 636 637 638 639 640 641 642 643
  "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
644
;; device number set in `tramp-sh-handle-file-attributes'.
645 646 647 648 649 650 651 652 653 654
(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
655
    $type =~ s/\"/\\\\\"/g;
656 657 658 659 660 661 662 663 664 665 666 667 668
    $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(
Michael Albinus's avatar
Michael Albinus committed
669
    \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u %%u t %%u -1)\\n\",
670 671 672 673 674 675 676 677 678 679 680 681
    $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],
Michael Albinus's avatar
Michael Albinus committed
682
    $stat[1]
683 684 685 686 687 688 689 690 691 692 693 694 695 696 697 698 699 700 701 702 703
);' \"$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
704
        $type =~ s/\"/\\\\\"/g;
705 706 707 708 709 710 711 712 713 714 715 716
        $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
717
    $filename =~ s/\"/\\\\\"/g;
718 719 720 721 722 723 724 725 726 727 728 729 730 731 732 733 734 735 736 737 738 739 740 741 742 743 744 745 746 747 748 749 750 751 752 753 754 755 756 757 758 759 760 761 762 763
    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
764
# Copyright (C) 2002-2019 Free Software Foundation, Inc.
765 766 767 768 769 770 771
use strict;

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

# We read in chunks of 54 bytes, to generate output lines
# of 72 chars (plus end of line)
776
while (read STDIN, $data, 54) {
777 778 779 780 781 782 783 784 785 786 787 788 789 790 791 792 793 794 795 796 797 798 799 800 801
    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
802
# Copyright (C) 2002-2019 Free Software Foundation, Inc.
803 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 829 830 831 832 833 834 835 836 837 838 839 840 841 842
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.")

843 844 845 846 847 848 849 850 851 852
(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.")

853 854 855 856 857 858 859 860 861 862 863 864 865 866 867 868 869 870 871 872 873 874 875 876 877 878 879 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
(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'.")

919 920 921
(defconst tramp-vc-registered-read-file-names
  "echo \"(\"
while read file; do
Michael Albinus's avatar
Michael Albinus committed
922
    quoted=`echo \"$file\" | sed -e \"s/\\\"/\\\\\\\\\\\\\\\\\\\"/\"`
923
    if %s \"$file\"; then
Michael Albinus's avatar
Michael Albinus committed
924
	echo \"(\\\"$quoted\\\" \\\"file-exists-p\\\" t)\"
925
    else
Michael Albinus's avatar
Michael Albinus committed
926
	echo \"(\\\"$quoted\\\" \\\"file-exists-p\\\" nil)\"
927 928
    fi
    if %s \"$file\"; then
Michael Albinus's avatar
Michael Albinus committed
929
	echo \"(\\\"$quoted\\\" \\\"file-readable-p\\\" t)\"
930
    else
Michael Albinus's avatar
Michael Albinus committed
931
	echo \"(\\\"$quoted\\\" \\\"file-readable-p\\\" nil)\"
932 933 934 935 936 937 938 939 940
    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
941
;; New handlers should be added here.
Michael Albinus's avatar
Michael Albinus committed
942
;;;###tramp-autoload
943
(defconst tramp-sh-file-name-handler-alist
944
  '((access-file . tramp-handle-access-file)
945
    (add-name-to-file . tramp-sh-handle-add-name-to-file)
Michael Albinus's avatar
Michael Albinus committed
946
    ;; `byte-compiler-base-file-name' performed by default handler.
947
    (copy-directory . tramp-sh-handle-copy-directory)
Michael Albinus's avatar
Michael Albinus committed
948
    (copy-file . tramp-sh-handle-copy-file)
949 950
    (delete-directory . tramp-sh-handle-delete-directory)
    (delete-file . tramp-sh-handle-delete-file)
Michael Albinus's avatar
Michael Albinus committed
951
    ;; `diff-latest-backup-file' performed by default handler.
952
    (directory-file-name . tramp-handle-directory-file-name)
Michael Albinus's avatar
Michael Albinus committed
953 954 955 956 957
    (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)
958
    (exec-path . tramp-sh-handle-exec-path)
959
    (expand-file-name . tramp-sh-handle-expand-file-name)
Michael Albinus's avatar
Michael Albinus committed
960 961 962 963
    (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)
964
    (file-equal-p . tramp-handle-file-equal-p)
Michael Albinus's avatar
Michael Albinus committed
965 966
    (file-executable-p . tramp-sh-handle-file-executable-p)
    (file-exists-p . tramp-sh-handle-file-exists-p)
967
    (file-in-directory-p . tramp-handle-file-in-directory-p)
968
    (file-local-copy . tramp-sh-handle-file-local-copy)
Michael Albinus's avatar
Michael Albinus committed
969 970 971
    (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)
972
    (file-name-case-insensitive-p . tramp-handle-file-name-case-insensitive-p)
Michael Albinus's avatar
Michael Albinus committed
973 974 975 976 977 978 979
    (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)
980
    (file-notify-valid-p . tramp-handle-file-notify-valid-p)
Michael Albinus's avatar
Michael Albinus committed
981 982 983
    (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)
984
    (file-remote-p . tramp-handle-file-remote-p)
Michael Albinus's avatar
Michael Albinus committed
985 986
    (file-selinux-context . tramp-sh-handle-file-selinux-context)
    (file-symlink-p . tramp-handle-file-symlink-p)
987
    (file-system-info . tramp-sh-handle-file-system-info)
Michael Albinus's avatar
Michael Albinus committed
988 989 990 991 992
    (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)
    ;; `get-file-buffer' performed by default handler.
    (insert-directory . tramp-sh-handle-insert-directory)
993
    (insert-file-contents . tramp-handle-insert-file-contents)
Michael Albinus's avatar
Michael Albinus committed
994
    (load . tramp-handle-load)
995
    (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
Michael Albinus's avatar
Michael Albinus committed
996
    (make-directory . tramp-sh-handle-make-directory)
Michael Albinus's avatar
Michael Albinus committed
997
    ;; `make-directory-internal' performed by default handler.
998
    (make-nearby-temp-file . tramp-handle-make-nearby-temp-file)
999
    (make-process . tramp-sh-handle-make-process)
Michael Albinus's avatar
Michael Albinus committed
1000 1001 1002
    (make-symbolic-link . tramp-sh-handle-make-symbolic-link)
    (process-file . tramp-sh-handle-process-file)
    (rename-file . tramp-sh-handle-rename-file)
1003
    (set-file-acl . tramp-sh-handle-set-file-acl)
Michael Albinus's avatar
Michael Albinus committed
1004 1005 1006 1007 1008
    (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)
1009
    (start-file-process . tramp-handle-start-file-process)
Michael Albinus's avatar
Michael Albinus committed
1010
    (substitute-in-file-name . tramp-handle-substitute-in-file-name)
1011
    (temporary-file-directory . tramp-handle-temporary-file-directory)
Michael Albinus's avatar
Michael Albinus committed
1012
    (tramp-set-file-uid-gid . tramp-sh-handle-set-file-uid-gid)
1013
    (unhandled-file-name-directory . ignore)
1014
    (vc-registered . tramp-sh-handle-vc-registered)
Michael Albinus's avatar
Michael Albinus committed
1015 1016
    (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime)
    (write-region . tramp-sh-handle-write-region))
1017 1018 1019 1020 1021
  "Alist of handler functions.
Operations not mentioned here will be handled by the normal Emacs functions.")

;;; File Name Handler Functions:

1022
(defun tramp-sh-handle-make-symbolic-link
Michael Albinus's avatar
Michael Albinus committed
1023
    (target linkname &optional ok-if-already-exists)
1024
  "Like `make-symbolic-link' for Tramp files.
Michael Albinus's avatar
Michael Albinus committed
1025 1026 1027 1028 1029
If TARGET is a non-Tramp file, it is used verbatim as the target
of the symlink.  If TARGET is a Tramp file, only the localname
component is used as the target of the symlink."
  (if (not (tramp-tramp-file-p (expand-file-name linkname)))
      (tramp-run-real-handler
1030
       #'make-symbolic-link (list target linkname ok-if-already-exists))
Michael Albinus's avatar
Michael Albinus committed
1031 1032

    (with-parsed-tramp-file-name linkname nil
1033 1034 1035 1036 1037 1038 1039 1040 1041 1042 1043 1044 1045 1046 1047
      ;; If TARGET is a Tramp name, use just the localname component.
      (when (and (tramp-tramp-file-p target)
		 (tramp-file-name-equal-p v (tramp-dissect-file-name target)))
	(setq target
	      (tramp-file-name-localname
	       (tramp-dissect-file-name (expand-file-name target)))))

      ;; If TARGET is still remote, quote it.
      (if (tramp-tramp-file-p target)
	  (make-symbolic-link
	   (let (file-name-handler-alist) (tramp-compat-file-name-quote target))
	   linkname ok-if-already-exists)

	(let ((ln (tramp-get-remote-ln v))
	      (cwd (tramp-run-real-handler
1048
		    #'file-name-directory (list localname))))
1049 1050 1051
	  (unless ln
	    (tramp-error
	     v 'file-error
Michael Albinus's avatar
Michael Albinus committed
1052 1053
	   "Making a symbolic link.  ln(1) does not exist on the remote host."))

1054 1055 1056 1057 1058 1059 1060 1061 1062 1063 1064 1065 1066
	  ;; 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? "
			    localname)))))
		(tramp-error v 'file-already-exists localname)
	      (delete-file linkname)))

1067 1068
	  (tramp-flush-file-properties v (file-name-directory localname))
	  (tramp-flush-file-properties v localname)
1069 1070 1071 1072 1073 1074 1075 1076 1077 1078 1079 1080 1081 1082 1083 1084 1085

	  ;; 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 TARGET belongs to.
	  (and (tramp-send-command-and-check
		v (format "cd %s" (tramp-shell-quote-argument cwd)))
               (tramp-send-command-and-check
		v (format
		   "%s -sf %s %s" ln
		   (tramp-shell-quote-argument target)
		   ;; 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 localname)))))))))))
1086

1087
(defun tramp-sh-handle-file-truename (filename)
1088
  "Like `file-truename' for Tramp files."
1089 1090 1091
   ;; Preserve trailing "/".
  (funcall
   (if (string-equal (file-name-nondirectory filename) "")
1092
       #'file-name-as-directory #'identity)
1093 1094
   (with-parsed-tramp-file-name (expand-file-name filename) nil
     (tramp-make-tramp-file-name
1095
      v
1096
      (with-tramp-file-property v localname "file-truename"
1097
	(let ((result nil)			; result steps in reverse order
1098 1099
	      (quoted (tramp-compat-file-name-quoted-p localname))
	      (localname (tramp-compat-file-name-unquote localname)))
1100
	  (tramp-message v 4 "Finding true name for `%s'" filename)
1101 1102 1103
	  (cond
	   ;; Use GNU readlink --canonicalize-missing where available.
	   ((tramp-get-remote-readlink v)
1104 1105 1106 1107 1108 1109 1110
	    (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))
1111
	      (setq result (buffer-substring (point-min) (point-at-eol)))))
1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123 1124

	   ;; 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)))))

1125 1126
	   ;; Do it yourself.
	   (t (let ((steps (split-string localname "/" 'omit))
1127 1128 1129 1130 1131 1132 1133 1134 1135
		    (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)
1136 1137 1138 1139
		(while (and steps (< numchase numchase-limit))
		  (setq thisstep (pop steps))
		  (tramp-message
		   v 5 "Check %s"
1140
		   (mapconcat #'identity
1141 1142 1143
			      (append '("") (reverse result) (list thisstep))
			      "/"))
		  (setq symlink-target
1144 1145 1146
			(tramp-compat-file-attribute-type
			 (file-attributes
			  (tramp-make-tramp-file-name
1147
			   v
1148
			   (mapconcat #'identity
1149 1150 1151
				      (append '("")
					      (reverse result)
					      (list thisstep))
1152 1153
				      "/")
			   'nohop))))
1154
		  (cond ((string= "." thisstep)
1155
			 (tramp-message v 5 "Ignoring step `.'"))
1156
			((string= ".." thisstep)
1157
			 (tramp-message v 5 "Processing step `..'")
1158 1159 1160 1161 1162 1163 1164 1165 1166
			 (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))
			 (setq steps
1167 1168
			       (append
				(split-string symlink-target "/" 'omit)	steps)))
1169 1170 1171 1172 1173 1174 1175 1176 1177 1178 1179
			(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
1180
			  (mapconcat #'identity (cons "" result) "/")
1181
			"/"))
1182 1183
		(when (string= "" result)
		  (setq result "/")))))
1184 1185 1186 1187 1188 1189 1190

	  ;; Detect cycle.
	  (when (and (file-symlink-p filename)
		     (string-equal result localname))
	    (tramp-error
	     v 'file-error
	     "Apparent cycle of symbolic links for %s" filename))
Michael Albinus's avatar
Michael Albinus committed
1191 1192 1193 1194 1195
	  ;; If the resulting localname looks remote, we must quote it
	  ;; for security reasons.
	  (when (or quoted (file-remote-p result))
	    (let (file-name-handler-alist)
	      (setq result (tramp-compat-file-name-quote result))))
1196
	  (tramp-message v 4 "True name of `%s' is `%s'" localname result)
1197 1198
	  result))
      'nohop))))
1199 1200 1201

;; Basic functions.

1202
(defun tramp-sh-handle-file-exists-p (filename)
1203 1204
  "Like `file-exists-p' for Tramp files."
  (with-parsed-tramp-file-name filename nil
1205
    (with-tramp-file-property v localname "file-exists-p"
1206 1207 1208 1209 1210 1211 1212 1213 1214 1215 1216
      (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)))))))

1217
(defun tramp-sh-handle-file-attributes (filename &optional id-format)
1218 1219
  "Like `file-attributes' for Tramp files."
  (unless id-format (setq id-format 'integer))
1220 1221 1222 1223 1224 1225
  (ignore-errors
    ;; Don't modify `last-coding-system-used' by accident.
    (let ((last-coding-system-used last-coding-system-used))
      (with-parsed-tramp-file-name (expand-file-name filename) nil
	(with-tramp-file-property
	    v localname (format "file-attributes-%s" id-format)
1226 1227 1228 1229 1230 1231 1232 1233 1234 1235 1236
	  (tramp-convert-file-attributes
	   v
	   (or
	    (cond
	     ((tramp-get-remote-stat v)
	      (tramp-do-file-attributes-with-stat v localname id-format))
	     ((tramp-get-remote-perl v)
	      (tramp-do-file-attributes-with-perl v localname id-format))
	     (t nil))
	    ;; The scripts could fail, for example with huge file size.
	    (tramp-do-file-attributes-with-ls v localname id-format))))))))
1237

1238 1239 1240 1241 1242 1243 1244
(defun tramp-sh--quoting-style-options (vec)
  (or
   (tramp-get-ls-command-with
    vec "--quoting-style=literal --show-control-chars")
   (tramp-get-ls-command-with vec "-w")
   ""))

1245 1246 1247 1248 1249 1250
(defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
  "Implement `file-attributes' for Tramp files using the ls(1) command."
  (let (symlinkp dirp
		 res-inode res-filemodes res-numlinks
		 res-uid res-gid res-size res-symlink-target)
    (tramp-message vec 5 "file attributes with ls: %s" localname)
1251
    ;; We cannot send all three commands combined, it could exceed
1252
    ;; NAME_MAX or PATH_MAX.  Happened on macOS, for example.
1253 1254 1255 1256 1257 1258 1259 1260 1261 1262 1263 1264 1265 1266 1267 1268 1269
    (when (or (tramp-send-command-and-check
               vec
               (format "%s %s"
                       (tramp-get-file-exists-command vec)
                       (tramp-shell-quote-argument localname)))
              (tramp-send-command-and-check
               vec
               (format "%s -h %s"
                       (tramp-get-test-command vec)
                       (tramp-shell-quote-argument localname))))
      (tramp-send-command
       vec
       (format "%s %s %s %s"
               (tramp-get-ls-command vec)
               (if (eq id-format 'integer) "-ildn" "-ild")
               ;; On systems which have no quoting style, file names
               ;; with special characters could fail.
1270
               (tramp-sh--quoting-style-options vec)
1271 1272 1273 1274 1275 1276
               (tramp-shell-quote-argument localname)))
      ;; Parse `ls -l' output ...
      (with-current-buffer (tramp-get-buffer vec)
        (when (> (buffer-size) 0)
          (goto-char (point-min))
          ;; ... inode
1277
          (setq res-inode (read (current-buffer)))
1278 1279 1280 1281 1282 1283 1284 1285 1286
          ;; ... file mode flags
          (setq res-filemodes (symbol-name (read (current-buffer))))
          ;; ... number links
          (setq res-numlinks (read (current-buffer)))
          ;; ... uid and gid
          (setq res-uid (read (current-buffer)))
          (setq res-gid (read (current-buffer)))
          (if (eq id-format 'integer)
              (progn
Michael Albinus's avatar
Michael Albinus committed
1287 1288 1289 1290
                (unless (numberp res-uid)
		  (setq res-uid tramp-unknown-id-integer))
                (unless (numberp res-gid)
		  (setq res-gid tramp-unknown-id-integer)))
1291
            (progn
1292 1293 1294 1295 1296 1297 1298 1299 1300 1301 1302
              (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
              (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
          ;; ... size
          (setq res-size (read (current-buffer)))
          ;; From the file modes, figure out other stuff.
          (setq symlinkp (eq ?l (aref res-filemodes 0)))
          (setq dirp (eq ?d (aref res-filemodes 0)))
          ;; If symlink, find out file name pointed to.
          (when symlinkp
            (search-forward "-> ")
            (setq res-symlink-target
1303
                  (if (looking-at-p "\"")
1304 1305 1306 1307 1308 1309 1310 1311 1312 1313 1314 1315 1316
                      (read (current-buffer))
                    (buffer-substring (point) (point-at-eol)))))
          ;; Return data gathered.
          (list
           ;; 0. t for directory, string (name linked to) for symbolic
           ;; link, or nil.
           (or dirp res-symlink-target)
           ;; 1. Number of links to file.
           res-numlinks
           ;; 2. File uid.
           res-uid
           ;; 3. File gid.
           res-gid
1317 1318 1319 1320
           ;; 4. Last access time.
           ;; 5. Last modification time.
           ;; 6. Last status change time.
           tramp-time-dont-know tramp-time-dont-know tramp-time-dont-know
1321 1322 1323 1324 1325 1326 1327 1328 1329 1330
           ;; 7. Size in bytes (-1, if number is out of range).
           res-size
           ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
           res-filemodes
           ;; 9. t if file's gid would change if file were deleted and
           ;; recreated.  Will be set in `tramp-convert-file-attributes'.
           t
           ;; 10. Inode number.
           res-inode
           ;; 11. Device number.  Will be replaced by a virtual device number.
1331
           -1))))))
1332 1333 1334 1335 1336 1337 1338 1339 1340 1341 1342 1343 1344 1345 1346 1347 1348 1349 1350

(defun tramp-do-file-attributes-with-perl
  (vec localname &optional id-format)
  "Implement `file-attributes' for Tramp files using a Perl script."
  (tramp-message vec 5 "file attributes with perl: %s" localname)
  (tramp-maybe-send-script
   vec tramp-perl-file-attributes "tramp_perl_file_attributes")
  (tramp-send-command-and-read
   vec
   (format "tramp_perl_file_attributes %s %s"
	   (tramp-shell-quote-argument localname) id-format)))

(defun tramp-do-file-attributes-with-stat
  (vec localname &optional id-format)
  "Implement `file-attributes' for Tramp files using stat(1) command."
  (tramp-message vec 5 "file attributes with stat: %s" localname)
  (tramp-send-command-and-read
   vec
   (format
1351 1352 1353 1354 1355 1356 1357 1358 1359 1360
    (eval-when-compile
      (concat
       ;; On Opsware, pdksh (which is the true name of ksh there)
       ;; doesn't parse correctly the sequence "((".  Therefore, we
       ;; add a space.  Apostrophes in the stat output are masked as
       ;; `tramp-stat-marker', in order to make a proper shell escape
       ;; of them in file names.
       "( (%s %s || %s -h %s) && (%s -c "
       "'((%s%%N%s) %%h %s %s %%X %%Y %%Z %%s %s%%A%s t %%i -1)' "
       "%s | sed -e 's/\"/\\\\\"/g' -e 's/%s/\"/g') || echo nil)"))
1361 1362 1363 1364 1365
    (tramp-get-file-exists-command vec)
    (tramp-shell-quote-argument localname)
    (tramp-get-test-command vec)
    (tramp-shell-quote-argument localname)
    (tramp-get-remote-stat vec)
1366 1367
    tramp-stat-marker tramp-stat-marker
    (if (eq id-format 'integer)
1368 1369
	"%u"
      (eval-when-compile (concat tramp-stat-marker "%U" tramp-stat-marker)))
1370
    (if (eq id-format 'integer)
1371 1372
	"%g"
      (eval-when-compile (concat tramp-stat-marker "%G" tramp-stat-marker)))
1373 1374 1375
    tramp-stat-marker tramp-stat-marker
    (tramp-shell-quote-argument localname)
    tramp-stat-quoted-marker)))
1376

1377
(defun tramp-sh-handle-set-visited-file-modtime (&optional time-list)
1378 1379
  "Like `set-visited-file-modtime' for Tramp files."
  (unless (buffer-file-name)
1380
    (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1381 1382
	   (buffer-name)))
  (if time-list
1383
      (tramp-run-real-handler #'set-visited-file-modtime (list time-list))
1384 1385 1386
    (let ((f (buffer-file-name))
	  coding-system-used)
      (with-parsed-tramp-file-name f nil
1387 1388
	(let* ((remote-file-name-inhibit-cache t)
	       (attr (file-attributes f))
1389
	       (modtime (or (tramp-compat-file-attribute-modification-time attr)
1390
			    tramp-time-doesnt-exist)))
1391
	  (setq coding-system-used last-coding-system-used)
1392
	  (if (not (tramp-compat-time-equal-p modtime tramp-time-dont-know))
1393
	      (tramp-run-real-handler #'set-visited-file-modtime (list modtime))
1394 1395 1396 1397 1398 1399
	    (progn
	      (tramp-send-command
	       v
	       (format "%s -ild %s"
		       (tramp-get-ls-command v)
		       (tramp-shell-quote-argument localname)))
1400
	      (setq attr (buffer-substring (point) (point-at-eol))))
1401 1402
	    (tramp-set-file-property
	     v localname "visited-file-modtime-ild" attr))
1403
	  (setq last-coding-system-used coding-system-used)
1404 1405 1406
	  nil)))))

;; This function makes the same assumption as
1407
;; `tramp-sh-handle-set-visited-file-modtime'.
1408
(defun tramp-sh-handle-verify-visited-file-modtime (&optional buf)
1409 1410 1411 1412 1413 1414
  "Like `verify-visited-file-modtime' for Tramp files.
At the time `verify-visited-file-modtime' calls this function, we
already know that the buffer is visiting a file and that
`visited-file-modtime' does not return 0.  Do not call this
function directly, unless those two cases are already taken care
of."
1415
  (with-current-buffer (or buf (current-buffer))
1416 1417 1418 1419 1420
    (let ((f (buffer-file-name)))
      ;; There is no file visiting the buffer, or the buffer has no
      ;; recorded last modification time, or there is no established
      ;; connection.
      (if (or (not f)
1421
	      (zerop (float-time (visited-file-modtime)))
1422
	      (not (file-remote-p f nil 'connected)))
1423 1424
	  t
	(with-parsed-tramp-file-name f nil
1425 1426
	  (let* ((remote-file-name-inhibit-cache t)
		 (attr (file-attributes f))
1427
		 (modtime (tramp-compat-file-attribute-modification-time attr))
1428 1429 1430 1431
		 (mt (visited-file-modtime)))

	    (cond
	     ;; File exists, and has a known modtime.
1432 1433 1434 1435
	     ((and attr
		   (not
		    (tramp-compat-time-equal-p modtime tramp-time-dont-know)))
	      (< (abs (tramp-time-diff modtime mt)) 2))
1436 1437 1438 1439 1440 1441 1442 1443
	     ;; Modtime has the don't know value.
	     (attr
	      (tramp-send-command
	       v
	       (format "%s -ild %s"
		       (tramp-get-ls-command v)
		       (tramp-shell-quote-argument localname)))
	      (with-current-buffer (tramp-get-buffer v)
1444
		(setq attr (buffer-substring (point) (point-at-eol))))
1445 1446 1447 1448 1449 1450
	      (equal
	       attr
	       (tramp-get-file-property
		v localname "visited-file-modtime-ild" "")))
	     ;; If file does not exist, say it is not modified if and
	     ;; only if that agrees with the buffer's record.
1451
	     (t (tramp-compat-time-equal-p mt tramp-time-doesnt-exist)))))))))
1452

1453
(defun tramp-sh-handle-set-file-modes (filename mode)
1454 1455
  "Like `set-file-modes' for Tramp files."
  (with-parsed-tramp-file-name filename nil
1456 1457
    (tramp-flush-file-properties v (file-name-directory localname))
    (tramp-flush-file-properties v localname)
1458 1459 1460
    ;; FIXME: extract the proper text from chmod's stderr.
    (tramp-barf-unless-okay
     v
1461
     (format "chmod %o %s" mode (tramp-shell-quote-argument localname))
1462
     "Error while changing file's mode %s" filename)))
1463

1464
(defun tramp-sh-handle-set-file-times (filename &optional time)
1465
  "Like `set-file-times' for Tramp files."
1466 1467
  (with-parsed-tramp-file-name filename nil
    (when (tramp-get-remote-touch v)
1468 1469
      (tramp-flush-file-properties v (file-name-directory localname))
      (tramp-flush-file-properties v localname)
1470 1471 1472 1473 1474 1475
      (let ((time
	     (if (or (null time)
		     (tramp-compat-time-equal-p time tramp-time-doesnt-exist)
		     (tramp-compat-time-equal-p time tramp-time-dont-know))
		 (current-time)
	       time)))
1476 1477 1478 1479 1480 1481 1482 1483
	(tramp-send-command-and-check
	 v (format
	    "env TZ=UTC %s %s %s"
	    (tramp-get-remote-touch v)
	    (if (tramp-get-connection-property v "touch-t" nil)
		(format "-t %s" (format-time-string "%Y%m%d%H%M.%S" time t))
	      "")
	    (tramp-shell-quote-argument localname)))))))
1484

Michael Albinus's avatar
Michael Albinus committed
1485 1486
(defun tramp-sh-handle-set-file-uid-gid (filename &optional uid gid)
  "Like `tramp-set-file-uid-gid' for Tramp files."
1487 1488 1489 1490 1491
  ;; Modern Unices allow chown only for root.  So we might need
  ;; another implementation, see `dired-do-chown'.  OTOH, it is mostly
  ;; working with su(do)? when it is needed, so it shall succeed in
  ;; the majority of cases.
  ;; Don't modify `last-coding-system-used' by accident.
1492
  (let ((last-coding-system-used last-coding-system-used))
Michael Albinus's avatar