Inhibit `epa-file-handler' in Tramp
[emacs.git] / lisp / net / tramp-sh.el
blob55ac778858cb21edf72d6c14da76653779409e6e
1 ;;; tramp-sh.el --- Tramp access functions for (s)sh-like connections
3 ;; Copyright (C) 1998-2015 Free Software Foundation, Inc.
5 ;; (copyright statements below in code to be updated with the above notice)
7 ;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
8 ;; Michael Albinus <michael.albinus@gmx.de>
9 ;; Keywords: comm, processes
10 ;; Package: tramp
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Code:
29 (require 'tramp)
31 ;; Pacify byte-compiler.
32 (eval-when-compile
33 (require 'cl)
34 (require 'dired))
35 (defvar directory-sep-char)
36 (defvar tramp-gw-tunnel-method)
37 (defvar tramp-gw-socks-method)
38 (defvar vc-handled-backends)
39 (defvar vc-bzr-program)
40 (defvar vc-git-program)
41 (defvar vc-hg-program)
43 ;;;###tramp-autoload
44 (defcustom tramp-inline-compress-start-size 4096
45 "The minimum size of compressing where inline transfer.
46 When inline transfer, compress transferred data of file
47 whose size is this value or above (up to `tramp-copy-size-limit').
48 If it is nil, no compression at all will be applied."
49 :group 'tramp
50 :type '(choice (const nil) integer))
52 ;;;###tramp-autoload
53 (defcustom tramp-copy-size-limit 10240
54 "The maximum file size where inline copying is preferred over an \
55 out-of-the-band copy.
56 If it is nil, out-of-the-band copy will be used without a check."
57 :group 'tramp
58 :type '(choice (const nil) integer))
60 ;;;###tramp-autoload
61 (defcustom tramp-terminal-type "dumb"
62 "Value of TERM environment variable for logging in to remote host.
63 Because Tramp wants to parse the output of the remote shell, it is easily
64 confused by ANSI color escape sequences and suchlike. Often, shell init
65 files conditionalize this setup based on the TERM environment variable."
66 :group 'tramp
67 :type 'string)
69 ;;;###tramp-autoload
70 (defcustom tramp-histfile-override t
71 "When invoking a shell, override the HISTFILE with this value.
72 When setting to a string, it redirects the shell history to that
73 file. Be careful when setting to \"/dev/null\"; this might
74 result in undesired results when using \"bash\" as shell.
76 The value t, the default value, unsets any setting of HISTFILE,
77 and sets both HISTFILESIZE and HISTSIZE to 0. If you set this
78 variable to nil, however, the *override* is disabled, so the
79 history will go to the default storage location,
80 e.g. \"$HOME/.sh_history\"."
81 :group 'tramp
82 :version "25.1"
83 :type '(choice (const :tag "Do not override HISTFILE" nil)
84 (const :tag "Unset HISTFILE" t)
85 (string :tag "Redirect to a file")))
87 ;;;###tramp-autoload
88 (defconst tramp-color-escape-sequence-regexp "\e[[;0-9]+m"
89 "Escape sequences produced by the \"ls\" command.")
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.
95 (defvar tramp-end-of-output
96 (format
97 "///%s#$"
98 (md5 (concat (prin1-to-string process-environment) (current-time-string))))
99 "String used to recognize end of output.
100 The '$' character at the end is quoted; the string cannot be
101 detected as prompt when being sent on echoing hosts, therefore.")
103 ;;;###tramp-autoload
104 (defconst tramp-initial-end-of-output "#$ "
105 "Prompt when establishing a connection.")
107 (defconst tramp-end-of-heredoc (md5 tramp-end-of-output)
108 "String used to recognize end of heredoc strings.")
110 ;;;###tramp-autoload
111 (defcustom tramp-use-ssh-controlmaster-options t
112 "Whether to use `tramp-ssh-controlmaster-options'."
113 :group 'tramp
114 :version "24.4"
115 :type 'boolean)
117 (defvar tramp-ssh-controlmaster-options nil
118 "Which ssh Control* arguments to use.
120 If it is a string, it should have the form
121 \"-o ControlMaster=auto -o ControlPath='tramp.%%r@%%h:%%p'
122 -o ControlPersist=no\". Percent characters in the ControlPath
123 spec must be doubled, because the string is used as format string.
125 Otherwise, it will be auto-detected by Tramp, if
126 `tramp-use-ssh-controlmaster-options' is non-nil. The value
127 depends on the installed local ssh version.
129 The string is used in `tramp-methods'.")
131 ;; Initialize `tramp-methods' with the supported methods.
132 ;;;###tramp-autoload
133 (add-to-list 'tramp-methods
134 '("rcp"
135 (tramp-login-program "rsh")
136 (tramp-login-args (("%h") ("-l" "%u")))
137 (tramp-remote-shell "/bin/sh")
138 (tramp-remote-shell-args ("-c"))
139 (tramp-copy-program "rcp")
140 (tramp-copy-args (("-p" "%k") ("-r")))
141 (tramp-copy-keep-date t)
142 (tramp-copy-recursive t)))
143 ;;;###tramp-autoload
144 (add-to-list 'tramp-methods
145 '("remcp"
146 (tramp-login-program "remsh")
147 (tramp-login-args (("%h") ("-l" "%u")))
148 (tramp-remote-shell "/bin/sh")
149 (tramp-remote-shell-args ("-c"))
150 (tramp-copy-program "rcp")
151 (tramp-copy-args (("-p" "%k")))
152 (tramp-copy-keep-date t)))
153 ;;;###tramp-autoload
154 (add-to-list 'tramp-methods
155 '("scp"
156 (tramp-login-program "ssh")
157 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
158 ("-e" "none") ("%h")))
159 (tramp-async-args (("-q")))
160 (tramp-remote-shell "/bin/sh")
161 (tramp-remote-shell-args ("-c"))
162 (tramp-copy-program "scp")
163 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r") ("%c")))
164 (tramp-copy-keep-date t)
165 (tramp-copy-recursive t)
166 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
167 ("-o" "UserKnownHostsFile=/dev/null")
168 ("-o" "StrictHostKeyChecking=no")))
169 (tramp-default-port 22)))
170 ;;;###tramp-autoload
171 (add-to-list 'tramp-methods
172 '("scpx"
173 (tramp-login-program "ssh")
174 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
175 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
176 (tramp-async-args (("-q")))
177 (tramp-remote-shell "/bin/sh")
178 (tramp-remote-shell-args ("-c"))
179 (tramp-copy-program "scp")
180 (tramp-copy-args (("-P" "%p") ("-p" "%k")
181 ("-q") ("-r") ("%c")))
182 (tramp-copy-keep-date t)
183 (tramp-copy-recursive t)
184 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
185 ("-o" "UserKnownHostsFile=/dev/null")
186 ("-o" "StrictHostKeyChecking=no")))
187 (tramp-default-port 22)))
188 ;;;###tramp-autoload
189 (add-to-list 'tramp-methods
190 '("rsync"
191 (tramp-login-program "ssh")
192 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
193 ("-e" "none") ("%h")))
194 (tramp-async-args (("-q")))
195 (tramp-remote-shell "/bin/sh")
196 (tramp-remote-shell-args ("-c"))
197 (tramp-copy-program "rsync")
198 (tramp-copy-args (("-t" "%k") ("-r")))
199 (tramp-copy-env (("RSYNC_RSH") ("ssh" "%c")))
200 (tramp-copy-keep-date t)
201 (tramp-copy-keep-tmpfile t)
202 (tramp-copy-recursive t)))
203 ;;;###tramp-autoload
204 (add-to-list 'tramp-methods
205 '("rsh"
206 (tramp-login-program "rsh")
207 (tramp-login-args (("%h") ("-l" "%u")))
208 (tramp-remote-shell "/bin/sh")
209 (tramp-remote-shell-args ("-c"))))
210 ;;;###tramp-autoload
211 (add-to-list 'tramp-methods
212 '("remsh"
213 (tramp-login-program "remsh")
214 (tramp-login-args (("%h") ("-l" "%u")))
215 (tramp-remote-shell "/bin/sh")
216 (tramp-remote-shell-args ("-c"))))
217 ;;;###tramp-autoload
218 (add-to-list 'tramp-methods
219 '("ssh"
220 (tramp-login-program "ssh")
221 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
222 ("-e" "none") ("%h")))
223 (tramp-async-args (("-q")))
224 (tramp-remote-shell "/bin/sh")
225 (tramp-remote-shell-args ("-c"))
226 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
227 ("-o" "UserKnownHostsFile=/dev/null")
228 ("-o" "StrictHostKeyChecking=no")))
229 (tramp-default-port 22)))
230 ;;;###tramp-autoload
231 (add-to-list 'tramp-methods
232 '("sshx"
233 (tramp-login-program "ssh")
234 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
235 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
236 (tramp-async-args (("-q")))
237 (tramp-remote-shell "/bin/sh")
238 (tramp-remote-shell-args ("-c"))
239 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
240 ("-o" "UserKnownHostsFile=/dev/null")
241 ("-o" "StrictHostKeyChecking=no")))
242 (tramp-default-port 22)))
243 ;;;###tramp-autoload
244 (add-to-list 'tramp-methods
245 '("telnet"
246 (tramp-login-program "telnet")
247 (tramp-login-args (("%h") ("%p") ("2>/dev/null")))
248 (tramp-remote-shell "/bin/sh")
249 (tramp-remote-shell-args ("-c"))
250 (tramp-default-port 23)))
251 ;;;###tramp-autoload
252 (add-to-list 'tramp-methods
253 '("nc"
254 (tramp-login-program "telnet")
255 (tramp-login-args (("%h") ("%p") ("2>/dev/null")))
256 (tramp-remote-shell "/bin/sh")
257 (tramp-remote-shell-args ("-c"))
258 (tramp-copy-program "nc")
259 ;; We use "-v" for better error tracking.
260 (tramp-copy-args (("-w" "1") ("-v") ("%h") ("%r")))
261 (tramp-remote-copy-program "nc")
262 ;; We use "-p" as required for newer busyboxes. For older
263 ;; busybox/nc versions, the value must be (("-l") ("%r")). This
264 ;; can be achieved by tweaking `tramp-connection-properties'.
265 (tramp-remote-copy-args (("-l") ("-p" "%r")))
266 (tramp-default-port 23)))
267 ;;;###tramp-autoload
268 (add-to-list 'tramp-methods
269 '("su"
270 (tramp-login-program "su")
271 (tramp-login-args (("-") ("%u")))
272 (tramp-remote-shell "/bin/sh")
273 (tramp-remote-shell-args ("-c"))
274 (tramp-connection-timeout 10)))
275 ;;;###tramp-autoload
276 (add-to-list 'tramp-methods
277 '("sudo"
278 (tramp-login-program "sudo")
279 (tramp-login-args (("-u" "%u") ("-s") ("-H") ("-p" "Password:")))
280 ;; Local $SHELL could be a nasty one, like zsh or fish. Let's override it.
281 (tramp-login-env (("SHELL") ("/bin/sh")))
282 (tramp-remote-shell "/bin/sh")
283 (tramp-remote-shell-args ("-c"))
284 (tramp-connection-timeout 10)))
285 ;;;###tramp-autoload
286 (add-to-list 'tramp-methods
287 '("ksu"
288 (tramp-login-program "ksu")
289 (tramp-login-args (("%u") ("-q")))
290 (tramp-remote-shell "/bin/sh")
291 (tramp-remote-shell-args ("-c"))
292 (tramp-connection-timeout 10)))
293 ;;;###tramp-autoload
294 (add-to-list 'tramp-methods
295 '("krlogin"
296 (tramp-login-program "krlogin")
297 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
298 (tramp-remote-shell "/bin/sh")
299 (tramp-remote-shell-args ("-c"))))
300 ;;;###tramp-autoload
301 (add-to-list 'tramp-methods
302 `("plink"
303 (tramp-login-program "plink")
304 ;; ("%h") must be a single element, see `tramp-compute-multi-hops'.
305 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
306 ("%h") ("\"")
307 (,(format
308 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
309 tramp-terminal-type
310 tramp-initial-end-of-output))
311 ("/bin/sh") ("\"")))
312 (tramp-remote-shell "/bin/sh")
313 (tramp-remote-shell-args ("-c"))
314 (tramp-default-port 22)))
315 ;;;###tramp-autoload
316 (add-to-list 'tramp-methods
317 `("plinkx"
318 (tramp-login-program "plink")
319 (tramp-login-args (("-load") ("%h") ("-t") ("\"")
320 (,(format
321 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
322 tramp-terminal-type
323 tramp-initial-end-of-output))
324 ("/bin/sh") ("\"")))
325 (tramp-remote-shell "/bin/sh")
326 (tramp-remote-shell-args ("-c"))))
327 ;;;###tramp-autoload
328 (add-to-list 'tramp-methods
329 `("pscp"
330 (tramp-login-program "plink")
331 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
332 ("%h") ("\"")
333 (,(format
334 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
335 tramp-terminal-type
336 tramp-initial-end-of-output))
337 ("/bin/sh") ("\"")))
338 (tramp-remote-shell "/bin/sh")
339 (tramp-remote-shell-args ("-c"))
340 (tramp-copy-program "pscp")
341 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-scp") ("-p" "%k")
342 ("-q") ("-r")))
343 (tramp-copy-keep-date t)
344 (tramp-copy-recursive t)
345 (tramp-default-port 22)))
346 ;;;###tramp-autoload
347 (add-to-list 'tramp-methods
348 `("psftp"
349 (tramp-login-program "plink")
350 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
351 ("%h") ("\"")
352 (,(format
353 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
354 tramp-terminal-type
355 tramp-initial-end-of-output))
356 ("/bin/sh") ("\"")))
357 (tramp-remote-shell "/bin/sh")
358 (tramp-remote-shell-args ("-c"))
359 (tramp-copy-program "pscp")
360 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-sftp") ("-p" "%k")
361 ("-q") ("-r")))
362 (tramp-copy-keep-date t)
363 (tramp-copy-recursive t)))
364 ;;;###tramp-autoload
365 (add-to-list 'tramp-methods
366 '("fcp"
367 (tramp-login-program "fsh")
368 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
369 (tramp-remote-shell "/bin/sh")
370 (tramp-remote-shell-args ("-i") ("-c"))
371 (tramp-copy-program "fcp")
372 (tramp-copy-args (("-p" "%k")))
373 (tramp-copy-keep-date t)))
375 ;;;###tramp-autoload
376 (add-to-list 'tramp-default-method-alist
377 `(,tramp-local-host-regexp "\\`root\\'" "su"))
379 ;;;###tramp-autoload
380 (add-to-list 'tramp-default-user-alist
381 `(,(concat "\\`" (regexp-opt '("su" "sudo" "ksu")) "\\'")
382 nil "root"))
383 ;; Do not add "ssh" based methods, otherwise ~/.ssh/config would be ignored.
384 ;; Do not add "plink" based methods, they ask interactively for the user.
385 ;;;###tramp-autoload
386 (add-to-list 'tramp-default-user-alist
387 `(,(concat
388 "\\`"
389 (regexp-opt
390 '("rcp" "remcp" "rsh" "telnet" "nc" "krlogin" "fcp"))
391 "\\'")
392 nil ,(user-login-name)))
394 ;;;###tramp-autoload
395 (defconst tramp-completion-function-alist-rsh
396 '((tramp-parse-rhosts "/etc/hosts.equiv")
397 (tramp-parse-rhosts "~/.rhosts"))
398 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
400 ;;;###tramp-autoload
401 (defconst tramp-completion-function-alist-ssh
402 '((tramp-parse-rhosts "/etc/hosts.equiv")
403 (tramp-parse-rhosts "/etc/shosts.equiv")
404 (tramp-parse-shosts "/etc/ssh_known_hosts")
405 (tramp-parse-sconfig "/etc/ssh_config")
406 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
407 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
408 (tramp-parse-rhosts "~/.rhosts")
409 (tramp-parse-rhosts "~/.shosts")
410 (tramp-parse-shosts "~/.ssh/known_hosts")
411 (tramp-parse-sconfig "~/.ssh/config")
412 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
413 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
414 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
416 ;;;###tramp-autoload
417 (defconst tramp-completion-function-alist-telnet
418 '((tramp-parse-hosts "/etc/hosts"))
419 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
421 ;;;###tramp-autoload
422 (defconst tramp-completion-function-alist-su
423 '((tramp-parse-passwd "/etc/passwd"))
424 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
426 ;;;###tramp-autoload
427 (defconst tramp-completion-function-alist-putty
428 `((tramp-parse-putty
429 ,(if (memq system-type '(windows-nt))
430 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"
431 "~/.putty/sessions")))
432 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty sessions.")
434 ;;;###tramp-autoload
435 (eval-after-load 'tramp
436 '(progn
437 (tramp-set-completion-function "rcp" tramp-completion-function-alist-rsh)
438 (tramp-set-completion-function "remcp" tramp-completion-function-alist-rsh)
439 (tramp-set-completion-function "scp" tramp-completion-function-alist-ssh)
440 (tramp-set-completion-function "scpx" tramp-completion-function-alist-ssh)
441 (tramp-set-completion-function "rsync" tramp-completion-function-alist-ssh)
442 (tramp-set-completion-function "rsh" tramp-completion-function-alist-rsh)
443 (tramp-set-completion-function "remsh" tramp-completion-function-alist-rsh)
444 (tramp-set-completion-function "ssh" tramp-completion-function-alist-ssh)
445 (tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh)
446 (tramp-set-completion-function
447 "telnet" tramp-completion-function-alist-telnet)
448 (tramp-set-completion-function "nc" tramp-completion-function-alist-telnet)
449 (tramp-set-completion-function "su" tramp-completion-function-alist-su)
450 (tramp-set-completion-function "sudo" tramp-completion-function-alist-su)
451 (tramp-set-completion-function "ksu" tramp-completion-function-alist-su)
452 (tramp-set-completion-function
453 "krlogin" tramp-completion-function-alist-rsh)
454 (tramp-set-completion-function "plink" tramp-completion-function-alist-ssh)
455 (tramp-set-completion-function
456 "plinkx" tramp-completion-function-alist-putty)
457 (tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh)
458 (tramp-set-completion-function "psftp" tramp-completion-function-alist-ssh)
459 (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh)))
461 ;; "getconf PATH" yields:
462 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
463 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
464 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
465 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
466 ;; IRIX64: /usr/bin
467 ;;;###tramp-autoload
468 (defcustom tramp-remote-path
469 '(tramp-default-remote-path "/bin" "/usr/bin" "/sbin" "/usr/sbin"
470 "/usr/local/bin" "/usr/local/sbin" "/local/bin" "/local/freeware/bin"
471 "/local/gnu/bin" "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin"
472 "/opt/bin" "/opt/sbin" "/opt/local/bin")
473 "List of directories to search for executables on remote host.
474 For every remote host, this variable will be set buffer local,
475 keeping the list of existing directories on that host.
477 You can use `~' in this list, but when searching for a shell which groks
478 tilde expansion, all directory names starting with `~' will be ignored.
480 `Default Directories' represent the list of directories given by
481 the command \"getconf PATH\". It is recommended to use this
482 entry on top of this list, because these are the default
483 directories for POSIX compatible commands. On remote hosts which
484 do not offer the getconf command (like cygwin), the value
485 \"/bin:/usr/bin\" is used instead of.
487 `Private Directories' are the settings of the $PATH environment,
488 as given in your `~/.profile'."
489 :group 'tramp
490 :type '(repeat (choice
491 (const :tag "Default Directories" tramp-default-remote-path)
492 (const :tag "Private Directories" tramp-own-remote-path)
493 (string :tag "Directory"))))
495 ;;;###tramp-autoload
496 (defcustom tramp-remote-process-environment
497 `("TMOUT=0" "LC_CTYPE=''"
498 ,(format "TERM=%s" tramp-terminal-type)
499 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version)
500 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH=" "PAGER=cat"
501 "autocorrect=" "correct=")
502 "List of environment variables to be set on the remote host.
504 Each element should be a string of the form ENVVARNAME=VALUE. An
505 entry ENVVARNAME= disables the corresponding environment variable,
506 which might have been set in the init files like ~/.profile.
508 Special handling is applied to the PATH environment, which should
509 not be set here. Instead, it should be set via `tramp-remote-path'."
510 :group 'tramp
511 :version "24.4"
512 :type '(repeat string))
514 ;;;###tramp-autoload
515 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
516 "Alist specifying extra arguments to pass to the remote shell.
517 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
518 matching the shell file name and ARGS is a string specifying the
519 arguments.
521 This variable is only used when Tramp needs to start up another shell
522 for tilde expansion. The extra arguments should typically prevent the
523 shell from reading its init file."
524 :group 'tramp
525 ;; This might be the wrong way to test whether the widget type
526 ;; `alist' is available. Who knows the right way to test it?
527 :type (if (get 'alist 'widget-type)
528 '(alist :key-type string :value-type string)
529 '(repeat (cons string string))))
531 (defconst tramp-actions-before-shell
532 '((tramp-login-prompt-regexp tramp-action-login)
533 (tramp-password-prompt-regexp tramp-action-password)
534 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
535 (shell-prompt-pattern tramp-action-succeed)
536 (tramp-shell-prompt-pattern tramp-action-succeed)
537 (tramp-yesno-prompt-regexp tramp-action-yesno)
538 (tramp-yn-prompt-regexp tramp-action-yn)
539 (tramp-terminal-prompt-regexp tramp-action-terminal)
540 (tramp-process-alive-regexp tramp-action-process-alive))
541 "List of pattern/action pairs.
542 Whenever a pattern matches, the corresponding action is performed.
543 Each item looks like (PATTERN ACTION).
545 The PATTERN should be a symbol, a variable. The value of this
546 variable gives the regular expression to search for. Note that the
547 regexp must match at the end of the buffer, \"\\'\" is implicitly
548 appended to it.
550 The ACTION should also be a symbol, but a function. When the
551 corresponding PATTERN matches, the ACTION function is called.")
553 (defconst tramp-actions-copy-out-of-band
554 '((tramp-password-prompt-regexp tramp-action-password)
555 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
556 (tramp-copy-failed-regexp tramp-action-permission-denied)
557 (tramp-process-alive-regexp tramp-action-out-of-band))
558 "List of pattern/action pairs.
559 This list is used for copying/renaming with out-of-band methods.
561 See `tramp-actions-before-shell' for more info.")
563 (defconst tramp-uudecode
564 "(echo begin 600 %t; tail -n +2) | uudecode
565 cat %t
566 rm -f %t"
567 "Shell function to implement `uudecode' to standard output.
568 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
569 for this or `uudecode -p', but some systems don't, and for them
570 we have this shell function.")
572 (defconst tramp-perl-file-truename
573 "%s -e '
574 use File::Spec;
575 use Cwd \"realpath\";
577 sub recursive {
578 my ($volume, @dirs) = @_;
579 my $real = realpath(File::Spec->catpath(
580 $volume, File::Spec->catdir(@dirs), \"\"));
581 if ($real) {
582 my ($vol, $dir) = File::Spec->splitpath($real, 1);
583 return ($vol, File::Spec->splitdir($dir));
585 else {
586 my $last = pop(@dirs);
587 ($volume, @dirs) = recursive($volume, @dirs);
588 push(@dirs, $last);
589 return ($volume, @dirs);
593 $result = realpath($ARGV[0]);
594 if (!$result) {
595 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
596 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
598 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
601 if ($ARGV[0] =~ /\\/$/) {
602 $result = $result . \"/\";
605 print \"\\\"$result\\\"\\n\";
606 ' \"$1\" 2>/dev/null"
607 "Perl script to produce output suitable for use with `file-truename'
608 on the remote file system.
609 Escape sequence %s is replaced with name of Perl binary.
610 This string is passed to `format', so percent characters need to be doubled.")
612 (defconst tramp-perl-file-name-all-completions
613 "%s -e 'sub case {
614 my $str = shift;
615 if ($ARGV[2]) {
616 return lc($str);
618 else {
619 return $str;
622 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
623 @files = readdir(d); closedir(d);
624 foreach $f (@files) {
625 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
626 if (-d \"$ARGV[0]/$f\") {
627 print \"$f/\\n\";
629 else {
630 print \"$f\\n\";
634 print \"ok\\n\"
635 ' \"$1\" \"$2\" \"$3\" 2>/dev/null"
636 "Perl script to produce output suitable for use with
637 `file-name-all-completions' on the remote file system. Escape
638 sequence %s is replaced with name of Perl binary. This string is
639 passed to `format', so percent characters need to be doubled.")
641 ;; Perl script to implement `file-attributes' in a Lisp `read'able
642 ;; output. If you are hacking on this, note that you get *no* output
643 ;; unless this spits out a complete line, including the '\n' at the
644 ;; end.
645 ;; The device number is returned as "-1", because there will be a virtual
646 ;; device number set in `tramp-sh-handle-file-attributes'.
647 (defconst tramp-perl-file-attributes
648 "%s -e '
649 @stat = lstat($ARGV[0]);
650 if (!@stat) {
651 print \"nil\\n\";
652 exit 0;
654 if (($stat[2] & 0170000) == 0120000)
656 $type = readlink($ARGV[0]);
657 $type =~ s/\"/\\\\\"/g;
658 $type = \"\\\"$type\\\"\";
660 elsif (($stat[2] & 0170000) == 040000)
662 $type = \"t\";
664 else
666 $type = \"nil\"
668 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
669 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
670 printf(
671 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
672 $type,
673 $stat[3],
674 $uid,
675 $gid,
676 $stat[8] >> 16 & 0xffff,
677 $stat[8] & 0xffff,
678 $stat[9] >> 16 & 0xffff,
679 $stat[9] & 0xffff,
680 $stat[10] >> 16 & 0xffff,
681 $stat[10] & 0xffff,
682 $stat[7],
683 $stat[2],
684 $stat[1] >> 16 & 0xffff,
685 $stat[1] & 0xffff
686 );' \"$1\" \"$2\" 2>/dev/null"
687 "Perl script to produce output suitable for use with `file-attributes'
688 on the remote file system.
689 Escape sequence %s is replaced with name of Perl binary.
690 This string is passed to `format', so percent characters need to be doubled.")
692 (defconst tramp-perl-directory-files-and-attributes
693 "%s -e '
694 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
695 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
696 @list = readdir(DIR);
697 closedir(DIR);
698 $n = scalar(@list);
699 printf(\"(\\n\");
700 for($i = 0; $i < $n; $i++)
702 $filename = $list[$i];
703 @stat = lstat($filename);
704 if (($stat[2] & 0170000) == 0120000)
706 $type = readlink($filename);
707 $type =~ s/\"/\\\\\"/g;
708 $type = \"\\\"$type\\\"\";
710 elsif (($stat[2] & 0170000) == 040000)
712 $type = \"t\";
714 else
716 $type = \"nil\"
718 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
719 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
720 $filename =~ s/\"/\\\\\"/g;
721 printf(
722 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
723 $filename,
724 $type,
725 $stat[3],
726 $uid,
727 $gid,
728 $stat[8] >> 16 & 0xffff,
729 $stat[8] & 0xffff,
730 $stat[9] >> 16 & 0xffff,
731 $stat[9] & 0xffff,
732 $stat[10] >> 16 & 0xffff,
733 $stat[10] & 0xffff,
734 $stat[7],
735 $stat[2],
736 $stat[1] >> 16 & 0xffff,
737 $stat[1] & 0xffff,
738 $stat[0] >> 16 & 0xffff,
739 $stat[0] & 0xffff);
741 printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
742 "Perl script implementing `directory-files-attributes' as Lisp `read'able
743 output.
744 Escape sequence %s is replaced with name of Perl binary.
745 This string is passed to `format', so percent characters need to be doubled.")
747 ;; These two use base64 encoding.
748 (defconst tramp-perl-encode-with-module
749 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
750 "Perl program to use for encoding a file.
751 Escape sequence %s is replaced with name of Perl binary.
752 This string is passed to `format', so percent characters need to be doubled.
753 This implementation requires the MIME::Base64 Perl module to be installed
754 on the remote host.")
756 (defconst tramp-perl-decode-with-module
757 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
758 "Perl program to use for decoding a file.
759 Escape sequence %s is replaced with name of Perl binary.
760 This string is passed to `format', so percent characters need to be doubled.
761 This implementation requires the MIME::Base64 Perl module to be installed
762 on the remote host.")
764 (defconst tramp-perl-encode
765 "%s -e '
766 # This script contributed by Juanma Barranquero <lektu@terra.es>.
767 # Copyright (C) 2002-2015 Free Software Foundation, Inc.
768 use strict;
770 my %%trans = do {
771 my $i = 0;
772 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
773 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
775 my $data;
777 # We read in chunks of 54 bytes, to generate output lines
778 # of 72 chars (plus end of line)
779 while (read STDIN, $data, 54) {
780 my $pad = q();
782 # Only for the last chunk, and only if did not fill the last three-byte packet
783 if (eof) {
784 my $mod = length($data) %% 3;
785 $pad = q(=) x (3 - $mod) if $mod;
788 # Not the fastest method, but it is simple: unpack to binary string, split
789 # by groups of 6 bits and convert back from binary to byte; then map into
790 # the translation table
791 print
792 join q(),
793 map($trans{$_},
794 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
795 $pad,
796 qq(\\n);
797 }' 2>/dev/null"
798 "Perl program to use for encoding a file.
799 Escape sequence %s is replaced with name of Perl binary.
800 This string is passed to `format', so percent characters need to be doubled.")
802 (defconst tramp-perl-decode
803 "%s -e '
804 # This script contributed by Juanma Barranquero <lektu@terra.es>.
805 # Copyright (C) 2002-2015 Free Software Foundation, Inc.
806 use strict;
808 my %%trans = do {
809 my $i = 0;
810 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
811 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
814 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
816 binmode(\\*STDOUT);
818 # We are going to accumulate into $pending to accept any line length
819 # (we do not check they are <= 76 chars as the RFC says)
820 my $pending = q();
822 while (my $data = <STDIN>) {
823 chomp $data;
825 # If we find one or two =, we have reached the end and
826 # any following data is to be discarded
827 my $finished = $data =~ s/(==?).*/$1/;
828 $pending .= $data;
830 my $len = length($pending);
831 my $chunk = substr($pending, 0, $len & ~3);
832 $pending = substr($pending, $len & ~3 + 1);
834 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
835 # split in 8-bit chunks and convert back to char.
836 print join q(),
837 map $bytes{$_},
838 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
840 last if $finished;
841 }' 2>/dev/null"
842 "Perl program to use for decoding a file.
843 Escape sequence %s is replaced with name of Perl binary.
844 This string is passed to `format', so percent characters need to be doubled.")
846 (defconst tramp-perl-pack
847 "%s -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
848 "Perl program to use for encoding a file.
849 Escape sequence %s is replaced with name of Perl binary.")
851 (defconst tramp-perl-unpack
852 "%s -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"
853 "Perl program to use for decoding a file.
854 Escape sequence %s is replaced with name of Perl binary.")
856 (defconst tramp-vc-registered-read-file-names
857 "echo \"(\"
858 while read file; do
859 if %s \"$file\"; then
860 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
861 else
862 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
864 if %s \"$file\"; then
865 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
866 else
867 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
869 done
870 echo \")\""
871 "Script to check existence of VC related files.
872 It must be send formatted with two strings; the tests for file
873 existence, and file readability. Input shall be read via
874 here-document, otherwise the command could exceed maximum length
875 of command line.")
877 ;; New handlers should be added here.
878 (defconst tramp-sh-file-name-handler-alist
879 '(;; `access-file' performed by default handler.
880 (add-name-to-file . tramp-sh-handle-add-name-to-file)
881 ;; `byte-compiler-base-file-name' performed by default handler.
882 (copy-directory . tramp-sh-handle-copy-directory)
883 (copy-file . tramp-sh-handle-copy-file)
884 (delete-directory . tramp-sh-handle-delete-directory)
885 (delete-file . tramp-sh-handle-delete-file)
886 ;; `diff-latest-backup-file' performed by default handler.
887 (directory-file-name . tramp-handle-directory-file-name)
888 (directory-files . tramp-handle-directory-files)
889 (directory-files-and-attributes
890 . tramp-sh-handle-directory-files-and-attributes)
891 ;; `dired-call-process' performed by default handler.
892 (dired-compress-file . tramp-sh-handle-dired-compress-file)
893 (dired-recursive-delete-directory
894 . tramp-sh-handle-dired-recursive-delete-directory)
895 (dired-uncache . tramp-handle-dired-uncache)
896 (expand-file-name . tramp-sh-handle-expand-file-name)
897 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
898 (file-acl . tramp-sh-handle-file-acl)
899 (file-attributes . tramp-sh-handle-file-attributes)
900 (file-directory-p . tramp-sh-handle-file-directory-p)
901 ;; `file-equal-p' performed by default handler.
902 (file-executable-p . tramp-sh-handle-file-executable-p)
903 (file-exists-p . tramp-sh-handle-file-exists-p)
904 ;; `file-in-directory-p' performed by default handler.
905 (file-local-copy . tramp-sh-handle-file-local-copy)
906 (file-modes . tramp-handle-file-modes)
907 (file-name-all-completions . tramp-sh-handle-file-name-all-completions)
908 (file-name-as-directory . tramp-handle-file-name-as-directory)
909 (file-name-completion . tramp-handle-file-name-completion)
910 (file-name-directory . tramp-handle-file-name-directory)
911 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
912 ;; `file-name-sans-versions' performed by default handler.
913 (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p)
914 (file-notify-add-watch . tramp-sh-handle-file-notify-add-watch)
915 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
916 (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p)
917 (file-readable-p . tramp-sh-handle-file-readable-p)
918 (file-regular-p . tramp-handle-file-regular-p)
919 (file-remote-p . tramp-handle-file-remote-p)
920 (file-selinux-context . tramp-sh-handle-file-selinux-context)
921 (file-symlink-p . tramp-handle-file-symlink-p)
922 (file-truename . tramp-sh-handle-file-truename)
923 (file-writable-p . tramp-sh-handle-file-writable-p)
924 (find-backup-file-name . tramp-handle-find-backup-file-name)
925 ;; `find-file-noselect' performed by default handler.
926 ;; `get-file-buffer' performed by default handler.
927 (insert-directory . tramp-sh-handle-insert-directory)
928 (insert-file-contents . tramp-handle-insert-file-contents)
929 (insert-file-contents-literally
930 . tramp-sh-handle-insert-file-contents-literally)
931 (load . tramp-handle-load)
932 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
933 (make-directory . tramp-sh-handle-make-directory)
934 (make-symbolic-link . tramp-sh-handle-make-symbolic-link)
935 (process-file . tramp-sh-handle-process-file)
936 (rename-file . tramp-sh-handle-rename-file)
937 (set-file-acl . tramp-sh-handle-set-file-acl)
938 (set-file-modes . tramp-sh-handle-set-file-modes)
939 (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context)
940 (set-file-times . tramp-sh-handle-set-file-times)
941 (set-visited-file-modtime . tramp-sh-handle-set-visited-file-modtime)
942 (shell-command . tramp-handle-shell-command)
943 (start-file-process . tramp-sh-handle-start-file-process)
944 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
945 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
946 (vc-registered . tramp-sh-handle-vc-registered)
947 (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime)
948 (write-region . tramp-sh-handle-write-region))
949 "Alist of handler functions.
950 Operations not mentioned here will be handled by the normal Emacs functions.")
952 ;; This must be the last entry, because `identity' always matches.
953 ;;;###tramp-autoload
954 (add-to-list 'tramp-foreign-file-name-handler-alist
955 '(identity . tramp-sh-file-name-handler) 'append)
957 ;;; File Name Handler Functions:
959 (defun tramp-sh-handle-make-symbolic-link
960 (filename linkname &optional ok-if-already-exists)
961 "Like `make-symbolic-link' for Tramp files.
962 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
963 the symlink. If LINKNAME is a Tramp file, only the localname component is
964 used as the target of the symlink.
966 If LINKNAME is a Tramp file and the localname component is relative, then
967 it is expanded first, before the localname component is taken. Note that
968 this can give surprising results if the user/host for the source and
969 target of the symlink differ."
970 (with-parsed-tramp-file-name linkname l
971 (let ((ln (tramp-get-remote-ln l))
972 (cwd (tramp-run-real-handler
973 'file-name-directory (list l-localname))))
974 (unless ln
975 (tramp-error
976 l 'file-error
977 "Making a symbolic link. ln(1) does not exist on the remote host."))
979 ;; Do the 'confirm if exists' thing.
980 (when (file-exists-p linkname)
981 ;; What to do?
982 (if (or (null ok-if-already-exists) ; not allowed to exist
983 (and (numberp ok-if-already-exists)
984 (not (yes-or-no-p
985 (format
986 "File %s already exists; make it a link anyway? "
987 l-localname)))))
988 (tramp-error
989 l 'file-already-exists "File %s already exists" l-localname)
990 (delete-file linkname)))
992 ;; If FILENAME is a Tramp name, use just the localname component.
993 (when (tramp-tramp-file-p filename)
994 (setq filename
995 (tramp-file-name-localname
996 (tramp-dissect-file-name (expand-file-name filename)))))
998 (tramp-flush-file-property l (file-name-directory l-localname))
999 (tramp-flush-file-property l l-localname)
1001 ;; Right, they are on the same host, regardless of user, method,
1002 ;; etc. We now make the link on the remote machine. This will
1003 ;; occur as the user that FILENAME belongs to.
1004 (tramp-send-command-and-check
1006 (format
1007 "cd %s && %s -sf %s %s"
1008 (tramp-shell-quote-argument cwd)
1010 (tramp-shell-quote-argument filename)
1011 (tramp-shell-quote-argument l-localname))
1012 t))))
1014 (defun tramp-sh-handle-file-truename (filename)
1015 "Like `file-truename' for Tramp files."
1016 (format
1017 "%s%s"
1018 (with-parsed-tramp-file-name (expand-file-name filename) nil
1019 (tramp-make-tramp-file-name
1020 method user host
1021 (with-tramp-file-property v localname "file-truename"
1022 (let ((result nil)) ; result steps in reverse order
1023 (tramp-message v 4 "Finding true name for `%s'" filename)
1024 (cond
1025 ;; Use GNU readlink --canonicalize-missing where available.
1026 ((tramp-get-remote-readlink v)
1027 (tramp-send-command-and-check
1029 (format "%s --canonicalize-missing %s"
1030 (tramp-get-remote-readlink v)
1031 (tramp-shell-quote-argument localname)))
1032 (with-current-buffer (tramp-get-connection-buffer v)
1033 (goto-char (point-min))
1034 (setq result (buffer-substring (point-min) (point-at-eol)))))
1036 ;; Use Perl implementation.
1037 ((and (tramp-get-remote-perl v)
1038 (tramp-get-connection-property v "perl-file-spec" nil)
1039 (tramp-get-connection-property v "perl-cwd-realpath" nil))
1040 (tramp-maybe-send-script
1041 v tramp-perl-file-truename "tramp_perl_file_truename")
1042 (setq result
1043 (tramp-send-command-and-read
1045 (format "tramp_perl_file_truename %s"
1046 (tramp-shell-quote-argument localname)))))
1048 ;; Do it yourself. We bind `directory-sep-char' here for
1049 ;; XEmacs on Windows, which would otherwise use backslash.
1050 (t (let* ((directory-sep-char ?/)
1051 (steps (tramp-compat-split-string localname "/"))
1052 (localnamedir (tramp-run-real-handler
1053 'file-name-as-directory (list localname)))
1054 (is-dir (string= localname localnamedir))
1055 (thisstep nil)
1056 (numchase 0)
1057 ;; Don't make the following value larger than
1058 ;; necessary. People expect an error message in
1059 ;; a timely fashion when something is wrong;
1060 ;; otherwise they might think that Emacs is hung.
1061 ;; Of course, correctness has to come first.
1062 (numchase-limit 20)
1063 symlink-target)
1064 (while (and steps (< numchase numchase-limit))
1065 (setq thisstep (pop steps))
1066 (tramp-message
1067 v 5 "Check %s"
1068 (mapconcat 'identity
1069 (append '("") (reverse result) (list thisstep))
1070 "/"))
1071 (setq symlink-target
1072 (nth 0 (file-attributes
1073 (tramp-make-tramp-file-name
1074 method user host
1075 (mapconcat 'identity
1076 (append '("")
1077 (reverse result)
1078 (list thisstep))
1079 "/")))))
1080 (cond ((string= "." thisstep)
1081 (tramp-message v 5 "Ignoring step `.'"))
1082 ((string= ".." thisstep)
1083 (tramp-message v 5 "Processing step `..'")
1084 (pop result))
1085 ((stringp symlink-target)
1086 ;; It's a symlink, follow it.
1087 (tramp-message
1088 v 5 "Follow symlink to %s" symlink-target)
1089 (setq numchase (1+ numchase))
1090 (when (file-name-absolute-p symlink-target)
1091 (setq result nil))
1092 ;; If the symlink was absolute, we'll get a
1093 ;; string like "/user@host:/some/target";
1094 ;; extract the "/some/target" part from it.
1095 (when (tramp-tramp-file-p symlink-target)
1096 (unless (tramp-equal-remote filename symlink-target)
1097 (tramp-error
1098 v 'file-error
1099 "Symlink target `%s' on wrong host"
1100 symlink-target))
1101 (setq symlink-target localname))
1102 (setq steps
1103 (append (tramp-compat-split-string
1104 symlink-target "/")
1105 steps)))
1107 ;; It's a file.
1108 (setq result (cons thisstep result)))))
1109 (when (>= numchase numchase-limit)
1110 (tramp-error
1111 v 'file-error
1112 "Maximum number (%d) of symlinks exceeded" numchase-limit))
1113 (setq result (reverse result))
1114 ;; Combine list to form string.
1115 (setq result
1116 (if result
1117 (mapconcat 'identity (cons "" result) "/")
1118 "/"))
1119 (when (and is-dir
1120 (or (string= "" result)
1121 (not (string= (substring result -1) "/"))))
1122 (setq result (concat result "/"))))))
1124 (tramp-message v 4 "True name of `%s' is `%s'" localname result)
1125 result))))
1127 ;; Preserve trailing "/".
1128 (if (string-equal (file-name-nondirectory filename) "") "/" "")))
1130 ;; Basic functions.
1132 (defun tramp-sh-handle-file-exists-p (filename)
1133 "Like `file-exists-p' for Tramp files."
1134 (with-parsed-tramp-file-name filename nil
1135 (with-tramp-file-property v localname "file-exists-p"
1136 (or (not (null (tramp-get-file-property
1137 v localname "file-attributes-integer" nil)))
1138 (not (null (tramp-get-file-property
1139 v localname "file-attributes-string" nil)))
1140 (tramp-send-command-and-check
1142 (format
1143 "%s %s"
1144 (tramp-get-file-exists-command v)
1145 (tramp-shell-quote-argument localname)))))))
1147 (defun tramp-sh-handle-file-attributes (filename &optional id-format)
1148 "Like `file-attributes' for Tramp files."
1149 (unless id-format (setq id-format 'integer))
1150 (ignore-errors
1151 ;; Don't modify `last-coding-system-used' by accident.
1152 (let ((last-coding-system-used last-coding-system-used))
1153 (with-parsed-tramp-file-name (expand-file-name filename) nil
1154 (with-tramp-file-property
1155 v localname (format "file-attributes-%s" id-format)
1156 (save-excursion
1157 (tramp-convert-file-attributes
1160 (cond
1161 ((tramp-get-remote-stat v)
1162 (tramp-do-file-attributes-with-stat v localname id-format))
1163 ((tramp-get-remote-perl v)
1164 (tramp-do-file-attributes-with-perl v localname id-format))
1165 (t nil))
1166 ;; The scripts could fail, for example with huge file size.
1167 (tramp-do-file-attributes-with-ls v localname id-format)))))))))
1169 (defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
1170 "Implement `file-attributes' for Tramp files using the ls(1) command."
1171 (let (symlinkp dirp
1172 res-inode res-filemodes res-numlinks
1173 res-uid res-gid res-size res-symlink-target)
1174 (tramp-message vec 5 "file attributes with ls: %s" localname)
1175 (tramp-send-command
1177 (format "(%s %s || %s -h %s) && %s %s %s %s"
1178 (tramp-get-file-exists-command vec)
1179 (tramp-shell-quote-argument localname)
1180 (tramp-get-test-command vec)
1181 (tramp-shell-quote-argument localname)
1182 (tramp-get-ls-command vec)
1183 ;; On systems which have no quoting style, file names
1184 ;; with special characters could fail.
1185 (if (tramp-get-ls-command-with-quoting-style vec)
1186 "--quoting-style=c" "")
1187 (if (eq id-format 'integer) "-ildn" "-ild")
1188 (tramp-shell-quote-argument localname)))
1189 ;; Parse `ls -l' output ...
1190 (with-current-buffer (tramp-get-buffer vec)
1191 (when (> (buffer-size) 0)
1192 (goto-char (point-min))
1193 ;; ... inode
1194 (setq res-inode
1195 (condition-case err
1196 (read (current-buffer))
1197 (invalid-read-syntax
1198 (when (and (equal (cadr err)
1199 "Integer constant overflow in reader")
1200 (string-match
1201 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1202 (car (cddr err))))
1203 (let* ((big (read (substring (car (cddr err)) 0
1204 (match-beginning 1))))
1205 (small (read (match-string 1 (car (cddr err)))))
1206 (twiddle (/ small 65536)))
1207 (cons (+ big twiddle)
1208 (- small (* twiddle 65536))))))))
1209 ;; ... file mode flags
1210 (setq res-filemodes (symbol-name (read (current-buffer))))
1211 ;; ... number links
1212 (setq res-numlinks (read (current-buffer)))
1213 ;; ... uid and gid
1214 (setq res-uid (read (current-buffer)))
1215 (setq res-gid (read (current-buffer)))
1216 (if (eq id-format 'integer)
1217 (progn
1218 (unless (numberp res-uid) (setq res-uid -1))
1219 (unless (numberp res-gid) (setq res-gid -1)))
1220 (progn
1221 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
1222 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
1223 ;; ... size
1224 (setq res-size (read (current-buffer)))
1225 ;; From the file modes, figure out other stuff.
1226 (setq symlinkp (eq ?l (aref res-filemodes 0)))
1227 (setq dirp (eq ?d (aref res-filemodes 0)))
1228 ;; If symlink, find out file name pointed to.
1229 (when symlinkp
1230 (search-forward "-> ")
1231 (setq res-symlink-target
1232 (if (tramp-get-ls-command-with-quoting-style vec)
1233 (read (current-buffer))
1234 (buffer-substring (point) (point-at-eol)))))
1235 ;; Return data gathered.
1236 (list
1237 ;; 0. t for directory, string (name linked to) for symbolic
1238 ;; link, or nil.
1239 (or dirp res-symlink-target)
1240 ;; 1. Number of links to file.
1241 res-numlinks
1242 ;; 2. File uid.
1243 res-uid
1244 ;; 3. File gid.
1245 res-gid
1246 ;; 4. Last access time, as a list of integers. Normally this
1247 ;; would be in the same format as `current-time', but the
1248 ;; subseconds part is not currently implemented, and (0 0)
1249 ;; denotes an unknown time.
1250 ;; 5. Last modification time, likewise.
1251 ;; 6. Last status change time, likewise.
1252 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1253 ;; 7. Size in bytes (-1, if number is out of range).
1254 res-size
1255 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1256 res-filemodes
1257 ;; 9. t if file's gid would change if file were deleted and
1258 ;; recreated. Will be set in `tramp-convert-file-attributes'.
1260 ;; 10. Inode number.
1261 res-inode
1262 ;; 11. Device number. Will be replaced by a virtual device number.
1264 )))))
1266 (defun tramp-do-file-attributes-with-perl
1267 (vec localname &optional id-format)
1268 "Implement `file-attributes' for Tramp files using a Perl script."
1269 (tramp-message vec 5 "file attributes with perl: %s" localname)
1270 (tramp-maybe-send-script
1271 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
1272 (tramp-send-command-and-read
1274 (format "tramp_perl_file_attributes %s %s"
1275 (tramp-shell-quote-argument localname) id-format)))
1277 (defun tramp-do-file-attributes-with-stat
1278 (vec localname &optional id-format)
1279 "Implement `file-attributes' for Tramp files using stat(1) command."
1280 (tramp-message vec 5 "file attributes with stat: %s" localname)
1281 (tramp-send-command-and-read
1283 (format
1284 (concat
1285 ;; On Opsware, pdksh (which is the true name of ksh there)
1286 ;; doesn't parse correctly the sequence "((". Therefore, we add
1287 ;; a space. Apostrophes in the stat output are masked as "//",
1288 ;; in order to make a proper shell escape of them in file names.
1289 "( (%s %s || %s -h %s) && (%s -c "
1290 "'((//%%N//) %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 //%%A// t %%ie0 -1)' "
1291 "%s | sed -e 's/\"/\\\\\"/g' -e 's/\\/\\//\"/g') || echo nil)")
1292 (tramp-get-file-exists-command vec)
1293 (tramp-shell-quote-argument localname)
1294 (tramp-get-test-command vec)
1295 (tramp-shell-quote-argument localname)
1296 (tramp-get-remote-stat vec)
1297 (if (eq id-format 'integer) "%ue0" "//%U//")
1298 (if (eq id-format 'integer) "%ge0" "//%G//")
1299 (tramp-shell-quote-argument localname))))
1301 (defun tramp-sh-handle-set-visited-file-modtime (&optional time-list)
1302 "Like `set-visited-file-modtime' for Tramp files."
1303 (unless (buffer-file-name)
1304 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1305 (buffer-name)))
1306 (if time-list
1307 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
1308 (let ((f (buffer-file-name))
1309 coding-system-used)
1310 (with-parsed-tramp-file-name f nil
1311 (let* ((remote-file-name-inhibit-cache t)
1312 (attr (file-attributes f))
1313 ;; '(-1 65535) means file doesn't exists yet.
1314 (modtime (or (nth 5 attr) '(-1 65535))))
1315 (when (boundp 'last-coding-system-used)
1316 (setq coding-system-used (symbol-value 'last-coding-system-used)))
1317 ;; We use '(0 0) as a don't-know value. See also
1318 ;; `tramp-do-file-attributes-with-ls'.
1319 (if (not (equal modtime '(0 0)))
1320 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
1321 (progn
1322 (tramp-send-command
1324 (format "%s -ild %s"
1325 (tramp-get-ls-command v)
1326 (tramp-shell-quote-argument localname)))
1327 (setq attr (buffer-substring (point) (point-at-eol))))
1328 (tramp-set-file-property
1329 v localname "visited-file-modtime-ild" attr))
1330 (when (boundp 'last-coding-system-used)
1331 (set 'last-coding-system-used coding-system-used))
1332 nil)))))
1334 ;; This function makes the same assumption as
1335 ;; `tramp-sh-handle-set-visited-file-modtime'.
1336 (defun tramp-sh-handle-verify-visited-file-modtime (&optional buf)
1337 "Like `verify-visited-file-modtime' for Tramp files.
1338 At the time `verify-visited-file-modtime' calls this function, we
1339 already know that the buffer is visiting a file and that
1340 `visited-file-modtime' does not return 0. Do not call this
1341 function directly, unless those two cases are already taken care
1342 of."
1343 (with-current-buffer (or buf (current-buffer))
1344 (let ((f (buffer-file-name)))
1345 ;; There is no file visiting the buffer, or the buffer has no
1346 ;; recorded last modification time, or there is no established
1347 ;; connection.
1348 (if (or (not f)
1349 (eq (visited-file-modtime) 0)
1350 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
1352 (with-parsed-tramp-file-name f nil
1353 (let* ((remote-file-name-inhibit-cache t)
1354 (attr (file-attributes f))
1355 (modtime (nth 5 attr))
1356 (mt (visited-file-modtime)))
1358 (cond
1359 ;; File exists, and has a known modtime.
1360 ((and attr (not (equal modtime '(0 0))))
1361 (< (abs (tramp-time-diff
1362 modtime
1363 ;; For compatibility, deal with both the old
1364 ;; (HIGH . LOW) and the new (HIGH LOW) return
1365 ;; values of `visited-file-modtime'.
1366 (if (atom (cdr mt))
1367 (list (car mt) (cdr mt))
1368 mt)))
1370 ;; Modtime has the don't know value.
1371 (attr
1372 (tramp-send-command
1374 (format "%s -ild %s"
1375 (tramp-get-ls-command v)
1376 (tramp-shell-quote-argument localname)))
1377 (with-current-buffer (tramp-get-buffer v)
1378 (setq attr (buffer-substring (point) (point-at-eol))))
1379 (equal
1380 attr
1381 (tramp-get-file-property
1382 v localname "visited-file-modtime-ild" "")))
1383 ;; If file does not exist, say it is not modified if and
1384 ;; only if that agrees with the buffer's record.
1385 (t (equal mt '(-1 65535))))))))))
1387 (defun tramp-sh-handle-set-file-modes (filename mode)
1388 "Like `set-file-modes' for Tramp files."
1389 (with-parsed-tramp-file-name filename nil
1390 (tramp-flush-file-property v localname)
1391 ;; FIXME: extract the proper text from chmod's stderr.
1392 (tramp-barf-unless-okay
1394 (format "chmod %s %s"
1395 (tramp-compat-decimal-to-octal mode)
1396 (tramp-shell-quote-argument localname))
1397 "Error while changing file's mode %s" filename)))
1399 (defun tramp-sh-handle-set-file-times (filename &optional time)
1400 "Like `set-file-times' for Tramp files."
1401 (if (tramp-tramp-file-p filename)
1402 (with-parsed-tramp-file-name filename nil
1403 (when (tramp-get-remote-touch v)
1404 (tramp-flush-file-property v localname)
1405 (let ((time (if (or (null time) (equal time '(0 0)))
1406 (current-time)
1407 time))
1408 ;; With GNU Emacs, `format-time-string' has an
1409 ;; optional parameter UNIVERSAL. This is preferred,
1410 ;; because we could handle the case when the remote
1411 ;; host is located in a different time zone as the
1412 ;; local host.
1413 (utc (not (featurep 'xemacs))))
1414 (tramp-send-command-and-check
1415 v (format
1416 "%s %s %s %s"
1417 (if utc "env TZ=UTC" "")
1418 (tramp-get-remote-touch v)
1419 (if (tramp-get-connection-property v "touch-t" nil)
1420 (format "-t %s"
1421 (if utc
1422 (format-time-string "%Y%m%d%H%M.%S" time t)
1423 (format-time-string "%Y%m%d%H%M.%S" time)))
1425 (tramp-shell-quote-argument localname))))))
1427 ;; We handle also the local part, because in older Emacsen,
1428 ;; without `set-file-times', this function is an alias for this.
1429 ;; We are local, so we don't need the UTC settings.
1430 (zerop
1431 (tramp-call-process
1432 nil "touch" nil nil nil "-t"
1433 (format-time-string "%Y%m%d%H%M.%S" time)
1434 (tramp-shell-quote-argument filename)))))
1436 (defun tramp-set-file-uid-gid (filename &optional uid gid)
1437 "Set the ownership for FILENAME.
1438 If UID and GID are provided, these values are used; otherwise uid
1439 and gid of the corresponding user is taken. Both parameters must
1440 be non-negative integers."
1441 ;; Modern Unices allow chown only for root. So we might need
1442 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1443 ;; working with su(do)? when it is needed, so it shall succeed in
1444 ;; the majority of cases.
1445 ;; Don't modify `last-coding-system-used' by accident.
1446 (let ((last-coding-system-used last-coding-system-used))
1447 (if (tramp-tramp-file-p filename)
1448 (with-parsed-tramp-file-name filename nil
1449 (if (and (zerop (user-uid)) (tramp-local-host-p v))
1450 ;; If we are root on the local host, we can do it directly.
1451 (tramp-set-file-uid-gid localname uid gid)
1452 (let ((uid (or (and (natnump uid) uid)
1453 (tramp-get-remote-uid v 'integer)))
1454 (gid (or (and (natnump gid) gid)
1455 (tramp-get-remote-gid v 'integer))))
1456 (tramp-send-command
1457 v (format
1458 "chown %d:%d %s" uid gid
1459 (tramp-shell-quote-argument localname))))))
1461 ;; We handle also the local part, because there doesn't exist
1462 ;; `set-file-uid-gid'. On W32 "chown" might not work.
1463 (let ((uid (or (and (natnump uid) uid) (tramp-get-local-uid 'integer)))
1464 (gid (or (and (natnump gid) gid) (tramp-get-local-gid 'integer))))
1465 (tramp-call-process
1466 nil "chown" nil nil nil
1467 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
1469 (defun tramp-remote-selinux-p (vec)
1470 "Check, whether SELINUX is enabled on the remote host."
1471 (with-tramp-connection-property
1472 (tramp-get-connection-process vec) "selinux-p"
1473 (let ((result (tramp-find-executable
1474 vec "getenforce" (tramp-get-remote-path vec) t t)))
1475 (and result
1476 (string-equal
1477 (tramp-send-command-and-read
1478 vec (format "echo \\\"`%S`\\\"" result))
1479 "Enforcing")))))
1481 (defun tramp-sh-handle-file-selinux-context (filename)
1482 "Like `file-selinux-context' for Tramp files."
1483 (with-parsed-tramp-file-name filename nil
1484 (with-tramp-file-property v localname "file-selinux-context"
1485 (let ((context '(nil nil nil nil))
1486 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1487 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1488 (when (and (tramp-remote-selinux-p v)
1489 (tramp-send-command-and-check
1490 v (format
1491 "%s -d -Z %s"
1492 (tramp-get-ls-command v)
1493 (tramp-shell-quote-argument localname))))
1494 (with-current-buffer (tramp-get-connection-buffer v)
1495 (goto-char (point-min))
1496 (when (re-search-forward regexp (point-at-eol) t)
1497 (setq context (list (match-string 1) (match-string 2)
1498 (match-string 3) (match-string 4))))))
1499 ;; Return the context.
1500 context))))
1502 (defun tramp-sh-handle-set-file-selinux-context (filename context)
1503 "Like `set-file-selinux-context' for Tramp files."
1504 (with-parsed-tramp-file-name filename nil
1505 (if (and (consp context)
1506 (tramp-remote-selinux-p v)
1507 (tramp-send-command-and-check
1508 v (format "chcon %s %s %s %s %s"
1509 (if (stringp (nth 0 context))
1510 (format "--user=%s" (nth 0 context)) "")
1511 (if (stringp (nth 1 context))
1512 (format "--role=%s" (nth 1 context)) "")
1513 (if (stringp (nth 2 context))
1514 (format "--type=%s" (nth 2 context)) "")
1515 (if (stringp (nth 3 context))
1516 (format "--range=%s" (nth 3 context)) "")
1517 (tramp-shell-quote-argument localname))))
1518 (progn
1519 (tramp-set-file-property v localname "file-selinux-context" context)
1521 (tramp-set-file-property v localname "file-selinux-context" 'undef)
1522 nil)))
1524 (defun tramp-remote-acl-p (vec)
1525 "Check, whether ACL is enabled on the remote host."
1526 (with-tramp-connection-property (tramp-get-connection-process vec) "acl-p"
1527 (tramp-send-command-and-check vec "getfacl /")))
1529 (defun tramp-sh-handle-file-acl (filename)
1530 "Like `file-acl' for Tramp files."
1531 (with-parsed-tramp-file-name filename nil
1532 (with-tramp-file-property v localname "file-acl"
1533 (when (and (tramp-remote-acl-p v)
1534 (tramp-send-command-and-check
1535 v (format
1536 "getfacl -ac %s 2>/dev/null"
1537 (tramp-shell-quote-argument localname))))
1538 (with-current-buffer (tramp-get-connection-buffer v)
1539 (goto-char (point-max))
1540 (delete-blank-lines)
1541 (when (> (point-max) (point-min))
1542 (tramp-compat-funcall
1543 'substring-no-properties (buffer-string))))))))
1545 (defun tramp-sh-handle-set-file-acl (filename acl-string)
1546 "Like `set-file-acl' for Tramp files."
1547 (with-parsed-tramp-file-name (expand-file-name filename) nil
1548 (if (and (stringp acl-string) (tramp-remote-acl-p v)
1549 (progn
1550 (tramp-send-command
1551 v (format "setfacl --set-file=- %s <<'%s'\n%s\n%s\n"
1552 (tramp-shell-quote-argument localname)
1553 tramp-end-of-heredoc
1554 acl-string
1555 tramp-end-of-heredoc))
1556 (tramp-send-command-and-check v nil)))
1557 ;; Success.
1558 (progn
1559 (tramp-set-file-property v localname "file-acl" acl-string)
1561 ;; In case of errors, we return nil.
1562 (tramp-set-file-property v localname "file-acl-string" 'undef)
1563 nil)))
1565 ;; Simple functions using the `test' command.
1567 (defun tramp-sh-handle-file-executable-p (filename)
1568 "Like `file-executable-p' for Tramp files."
1569 (with-parsed-tramp-file-name filename nil
1570 (with-tramp-file-property v localname "file-executable-p"
1571 ;; Examine `file-attributes' cache to see if request can be
1572 ;; satisfied without remote operation.
1573 (or (tramp-check-cached-permissions v ?x)
1574 (tramp-run-test "-x" filename)))))
1576 (defun tramp-sh-handle-file-readable-p (filename)
1577 "Like `file-readable-p' for Tramp files."
1578 (with-parsed-tramp-file-name filename nil
1579 (with-tramp-file-property v localname "file-readable-p"
1580 ;; Examine `file-attributes' cache to see if request can be
1581 ;; satisfied without remote operation.
1582 (or (tramp-check-cached-permissions v ?r)
1583 (tramp-run-test "-r" filename)))))
1585 ;; When the remote shell is started, it looks for a shell which groks
1586 ;; tilde expansion. Here, we assume that all shells which grok tilde
1587 ;; expansion will also provide a `test' command which groks `-nt' (for
1588 ;; newer than). If this breaks, tell me about it and I'll try to do
1589 ;; something smarter about it.
1590 (defun tramp-sh-handle-file-newer-than-file-p (file1 file2)
1591 "Like `file-newer-than-file-p' for Tramp files."
1592 (cond ((not (file-exists-p file1))
1593 nil)
1594 ((not (file-exists-p file2))
1596 ;; We are sure both files exist at this point.
1598 (save-excursion
1599 ;; We try to get the mtime of both files. If they are not
1600 ;; equal to the "dont-know" value, then we subtract the times
1601 ;; and obtain the result.
1602 (let ((fa1 (file-attributes file1))
1603 (fa2 (file-attributes file2)))
1604 (if (and (not (equal (nth 5 fa1) '(0 0)))
1605 (not (equal (nth 5 fa2) '(0 0))))
1606 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
1607 ;; If one of them is the dont-know value, then we can
1608 ;; still try to run a shell command on the remote host.
1609 ;; However, this only works if both files are Tramp
1610 ;; files and both have the same method, same user, same
1611 ;; host.
1612 (unless (tramp-equal-remote file1 file2)
1613 (with-parsed-tramp-file-name
1614 (if (tramp-tramp-file-p file1) file1 file2) nil
1615 (tramp-error
1616 v 'file-error
1617 "Files %s and %s must have same method, user, host"
1618 file1 file2)))
1619 (with-parsed-tramp-file-name file1 nil
1620 (tramp-run-test2
1621 (tramp-get-test-nt-command v) file1 file2))))))))
1623 ;; Functions implemented using the basic functions above.
1625 (defun tramp-sh-handle-file-directory-p (filename)
1626 "Like `file-directory-p' for Tramp files."
1627 (with-parsed-tramp-file-name filename nil
1628 ;; `file-directory-p' is used as predicate for file name completion.
1629 ;; Sometimes, when a connection is not established yet, it is
1630 ;; desirable to return t immediately for "/method:foo:". It can
1631 ;; be expected that this is always a directory.
1632 (or (zerop (length localname))
1633 (with-tramp-file-property v localname "file-directory-p"
1634 (tramp-run-test "-d" filename)))))
1636 (defun tramp-sh-handle-file-writable-p (filename)
1637 "Like `file-writable-p' for Tramp files."
1638 (with-parsed-tramp-file-name filename nil
1639 (with-tramp-file-property v localname "file-writable-p"
1640 (if (file-exists-p filename)
1641 ;; Examine `file-attributes' cache to see if request can be
1642 ;; satisfied without remote operation.
1643 (or (tramp-check-cached-permissions v ?w)
1644 (tramp-run-test "-w" filename))
1645 ;; If file doesn't exist, check if directory is writable.
1646 (and (tramp-run-test "-d" (file-name-directory filename))
1647 (tramp-run-test "-w" (file-name-directory filename)))))))
1649 (defun tramp-sh-handle-file-ownership-preserved-p (filename &optional group)
1650 "Like `file-ownership-preserved-p' for Tramp files."
1651 (with-parsed-tramp-file-name filename nil
1652 (with-tramp-file-property v localname "file-ownership-preserved-p"
1653 (let ((attributes (file-attributes filename)))
1654 ;; Return t if the file doesn't exist, since it's true that no
1655 ;; information would be lost by an (attempted) delete and create.
1656 (or (null attributes)
1657 (and
1658 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer))
1659 (or (not group)
1660 (= (nth 3 attributes) (tramp-get-remote-gid v 'integer)))))))))
1662 ;; Directory listings.
1664 (defun tramp-sh-handle-directory-files-and-attributes
1665 (directory &optional full match nosort id-format)
1666 "Like `directory-files-and-attributes' for Tramp files."
1667 (unless id-format (setq id-format 'integer))
1668 (when (file-directory-p directory)
1669 (setq directory (expand-file-name directory))
1670 (let* ((temp
1671 (copy-tree
1672 (with-parsed-tramp-file-name directory nil
1673 (with-tramp-file-property
1674 v localname
1675 (format "directory-files-and-attributes-%s" id-format)
1676 (save-excursion
1677 (mapcar
1678 (lambda (x)
1679 (cons (car x)
1680 (tramp-convert-file-attributes v (cdr x))))
1682 (cond
1683 ((tramp-get-remote-stat v)
1684 (tramp-do-directory-files-and-attributes-with-stat
1685 v localname id-format))
1686 ((tramp-get-remote-perl v)
1687 (tramp-do-directory-files-and-attributes-with-perl
1688 v localname id-format))
1689 (t nil)))))))))
1690 result item)
1692 (while temp
1693 (setq item (pop temp))
1694 (when (or (null match) (string-match match (car item)))
1695 (when full
1696 (setcar item (expand-file-name (car item) directory)))
1697 (push item result)))
1699 (or (if nosort
1700 result
1701 (sort result (lambda (x y) (string< (car x) (car y)))))
1702 ;; The scripts could fail, for example with huge file size.
1703 (tramp-handle-directory-files-and-attributes
1704 directory full match nosort id-format)))))
1706 (defun tramp-do-directory-files-and-attributes-with-perl
1707 (vec localname &optional id-format)
1708 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1709 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
1710 (tramp-maybe-send-script
1711 vec tramp-perl-directory-files-and-attributes
1712 "tramp_perl_directory_files_and_attributes")
1713 (let ((object
1714 (tramp-send-command-and-read
1716 (format "tramp_perl_directory_files_and_attributes %s %s"
1717 (tramp-shell-quote-argument localname) id-format))))
1718 (when (stringp object) (tramp-error vec 'file-error object))
1719 object))
1721 (defun tramp-do-directory-files-and-attributes-with-stat
1722 (vec localname &optional id-format)
1723 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1724 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
1725 (tramp-send-command-and-read
1727 (format
1728 (concat
1729 ;; We must care about file names with spaces, or starting with
1730 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1731 ;; but it does not work on all remote systems. Apostrophes in
1732 ;; the stat output are masked as "//", in order to make a proper
1733 ;; shell escape of them in file names.
1734 "cd %s && echo \"(\"; (%s %s -a | "
1735 "xargs %s -c "
1736 "'(//%%n// (//%%N//) %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 //%%A// t %%ie0 -1)' "
1737 "-- 2>/dev/null | sed -e 's/\"/\\\\\"/g' -e 's/\\/\\//\"/g'); echo \")\"")
1738 (tramp-shell-quote-argument localname)
1739 (tramp-get-ls-command vec)
1740 ;; On systems which have no quoting style, file names with
1741 ;; special characters could fail.
1742 (if (tramp-get-ls-command-with-quoting-style vec)
1743 "--quoting-style=shell" "")
1744 (tramp-get-remote-stat vec)
1745 (if (eq id-format 'integer) "%ue0" "//%U//")
1746 (if (eq id-format 'integer) "%ge0" "//%G//"))))
1748 ;; This function should return "foo/" for directories and "bar" for
1749 ;; files.
1750 (defun tramp-sh-handle-file-name-all-completions (filename directory)
1751 "Like `file-name-all-completions' for Tramp files."
1752 (unless (save-match-data (string-match "/" filename))
1753 (with-parsed-tramp-file-name (expand-file-name directory) nil
1755 (all-completions
1756 filename
1757 (mapcar
1758 'list
1760 ;; Try cache entries for `filename', `filename' with last
1761 ;; character removed, `filename' with last two characters
1762 ;; removed, ..., and finally the empty string - all
1763 ;; concatenated to the local directory name.
1764 (let ((remote-file-name-inhibit-cache
1765 (or remote-file-name-inhibit-cache
1766 tramp-completion-reread-directory-timeout)))
1768 ;; This is inefficient for very long file names, pity
1769 ;; `reduce' is not available...
1770 (car
1771 (apply
1772 'append
1773 (mapcar
1774 (lambda (x)
1775 (let ((cache-hit
1776 (tramp-get-file-property
1778 (concat localname (substring filename 0 x))
1779 "file-name-all-completions"
1780 nil)))
1781 (when cache-hit (list cache-hit))))
1782 ;; We cannot use a length of 0, because file properties
1783 ;; for "foo" and "foo/" are identical.
1784 (tramp-compat-number-sequence (length filename) 1 -1)))))
1786 ;; Cache expired or no matching cache entry found so we need
1787 ;; to perform a remote operation.
1788 (let (result)
1789 ;; Get a list of directories and files, including reliably
1790 ;; tagging the directories with a trailing '/'. Because I
1791 ;; rock. --daniel@danann.net
1793 ;; Changed to perform `cd' in the same remote op and only
1794 ;; get entries starting with `filename'. Capture any `cd'
1795 ;; error messages. Ensure any `cd' and `echo' aliases are
1796 ;; ignored.
1797 (tramp-send-command
1799 (if (tramp-get-remote-perl v)
1800 (progn
1801 (tramp-maybe-send-script
1802 v tramp-perl-file-name-all-completions
1803 "tramp_perl_file_name_all_completions")
1804 (format "tramp_perl_file_name_all_completions %s %s %d"
1805 (tramp-shell-quote-argument localname)
1806 (tramp-shell-quote-argument filename)
1807 (if (symbol-value
1808 ;; `read-file-name-completion-ignore-case'
1809 ;; is introduced with Emacs 22.1.
1810 (if (boundp
1811 'read-file-name-completion-ignore-case)
1812 'read-file-name-completion-ignore-case
1813 'completion-ignore-case))
1814 1 0)))
1816 (format (concat
1817 "(cd %s 2>&1 && (%s -a %s 2>/dev/null"
1818 ;; `ls' with wildcard might fail with `Argument
1819 ;; list too long' error in some corner cases; if
1820 ;; `ls' fails after `cd' succeeded, chances are
1821 ;; that's the case, so let's retry without
1822 ;; wildcard. This will return "too many" entries
1823 ;; but that isn't harmful.
1824 " || %s -a 2>/dev/null)"
1825 " | while IFS= read f; do"
1826 " if %s -d \"$f\" 2>/dev/null;"
1827 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1828 " && \\echo ok) || \\echo fail")
1829 (tramp-shell-quote-argument localname)
1830 (tramp-get-ls-command v)
1831 ;; When `filename' is empty, just `ls' without
1832 ;; `filename' argument is more efficient than `ls *'
1833 ;; for very large directories and might avoid the
1834 ;; `Argument list too long' error.
1836 ;; With and only with wildcard, we need to add
1837 ;; `-d' to prevent `ls' from descending into
1838 ;; sub-directories.
1839 (if (zerop (length filename))
1841 (format "-d %s*" (tramp-shell-quote-argument filename)))
1842 (tramp-get-ls-command v)
1843 (tramp-get-test-command v))))
1845 ;; Now grab the output.
1846 (with-current-buffer (tramp-get-buffer v)
1847 (goto-char (point-max))
1849 ;; Check result code, found in last line of output.
1850 (forward-line -1)
1851 (if (looking-at "^fail$")
1852 (progn
1853 ;; Grab error message from line before last line
1854 ;; (it was put there by `cd 2>&1').
1855 (forward-line -1)
1856 (tramp-error
1857 v 'file-error
1858 "tramp-sh-handle-file-name-all-completions: %s"
1859 (buffer-substring (point) (point-at-eol))))
1860 ;; For peace of mind, if buffer doesn't end in `fail'
1861 ;; then it should end in `ok'. If neither are in the
1862 ;; buffer something went seriously wrong on the remote
1863 ;; side.
1864 (unless (looking-at "^ok$")
1865 (tramp-error
1866 v 'file-error
1868 tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
1869 (tramp-shell-quote-argument localname) (buffer-string))))
1871 (while (zerop (forward-line -1))
1872 (push (buffer-substring (point) (point-at-eol)) result)))
1874 ;; Because the remote op went through OK we know the
1875 ;; directory we `cd'-ed to exists.
1876 (tramp-set-file-property v localname "file-exists-p" t)
1878 ;; Because the remote op went through OK we know every
1879 ;; file listed by `ls' exists.
1880 (mapc (lambda (entry)
1881 (tramp-set-file-property
1882 v (concat localname entry) "file-exists-p" t))
1883 result)
1885 ;; Store result in the cache.
1886 (tramp-set-file-property
1887 v (concat localname filename)
1888 "file-name-all-completions" result))))))))
1890 ;; cp, mv and ln
1892 (defun tramp-sh-handle-add-name-to-file
1893 (filename newname &optional ok-if-already-exists)
1894 "Like `add-name-to-file' for Tramp files."
1895 (unless (tramp-equal-remote filename newname)
1896 (with-parsed-tramp-file-name
1897 (if (tramp-tramp-file-p filename) filename newname) nil
1898 (tramp-error
1899 v 'file-error
1900 "add-name-to-file: %s"
1901 "only implemented for same method, same user, same host")))
1902 (with-parsed-tramp-file-name filename v1
1903 (with-parsed-tramp-file-name newname v2
1904 (let ((ln (when v1 (tramp-get-remote-ln v1))))
1905 (when (and (numberp ok-if-already-exists)
1906 (file-exists-p newname)
1907 (yes-or-no-p
1908 (format
1909 "File %s already exists; make it a new name anyway? "
1910 newname)))
1911 (tramp-error
1912 v2 'file-error "add-name-to-file: file %s already exists" newname))
1913 (when ok-if-already-exists (setq ln (concat ln " -f")))
1914 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1915 (tramp-flush-file-property v2 v2-localname)
1916 (tramp-barf-unless-okay
1918 (format "%s %s %s" ln
1919 (tramp-shell-quote-argument v1-localname)
1920 (tramp-shell-quote-argument v2-localname))
1921 "error with add-name-to-file, see buffer `%s' for details"
1922 (buffer-name))))))
1924 (defun tramp-sh-handle-copy-file
1925 (filename newname &optional ok-if-already-exists keep-date
1926 preserve-uid-gid preserve-extended-attributes)
1927 "Like `copy-file' for Tramp files."
1928 (setq filename (expand-file-name filename))
1929 (setq newname (expand-file-name newname))
1930 (cond
1931 ;; At least one file a Tramp file?
1932 ((or (tramp-tramp-file-p filename)
1933 (tramp-tramp-file-p newname))
1934 (tramp-do-copy-or-rename-file
1935 'copy filename newname ok-if-already-exists keep-date
1936 preserve-uid-gid preserve-extended-attributes))
1937 ;; Compat section.
1938 (preserve-extended-attributes
1939 (tramp-run-real-handler
1940 'copy-file
1941 (list filename newname ok-if-already-exists keep-date
1942 preserve-uid-gid preserve-extended-attributes)))
1943 (preserve-uid-gid
1944 (tramp-run-real-handler
1945 'copy-file
1946 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
1948 (tramp-run-real-handler
1949 'copy-file (list filename newname ok-if-already-exists keep-date)))))
1951 (defun tramp-sh-handle-copy-directory
1952 (dirname newname &optional keep-date parents copy-contents)
1953 "Like `copy-directory' for Tramp files."
1954 (let ((t1 (tramp-tramp-file-p dirname))
1955 (t2 (tramp-tramp-file-p newname)))
1956 (with-parsed-tramp-file-name (if t1 dirname newname) nil
1957 (if (and (not copy-contents)
1958 (tramp-get-method-parameter method 'tramp-copy-recursive)
1959 ;; When DIRNAME and NEWNAME are remote, they must have
1960 ;; the same method.
1961 (or (null t1) (null t2)
1962 (string-equal
1963 (tramp-file-name-method (tramp-dissect-file-name dirname))
1964 (tramp-file-name-method
1965 (tramp-dissect-file-name newname)))))
1966 ;; scp or rsync DTRT.
1967 (progn
1968 (setq dirname (directory-file-name (expand-file-name dirname))
1969 newname (directory-file-name (expand-file-name newname)))
1970 (if (and (file-directory-p newname)
1971 (not (string-equal (file-name-nondirectory dirname)
1972 (file-name-nondirectory newname))))
1973 (setq newname
1974 (expand-file-name
1975 (file-name-nondirectory dirname) newname)))
1976 (if (not (file-directory-p (file-name-directory newname)))
1977 (make-directory (file-name-directory newname) parents))
1978 (tramp-do-copy-or-rename-file-out-of-band
1979 'copy dirname newname keep-date))
1980 ;; We must do it file-wise.
1981 (tramp-run-real-handler
1982 'copy-directory
1983 (if copy-contents
1984 (list dirname newname keep-date parents copy-contents)
1985 (list dirname newname keep-date parents))))
1987 ;; When newname did exist, we have wrong cached values.
1988 (when t2
1989 (with-parsed-tramp-file-name newname nil
1990 (tramp-flush-file-property v (file-name-directory localname))
1991 (tramp-flush-file-property v localname))))))
1993 (defun tramp-sh-handle-rename-file
1994 (filename newname &optional ok-if-already-exists)
1995 "Like `rename-file' for Tramp files."
1996 ;; Check if both files are local -- invoke normal rename-file.
1997 ;; Otherwise, use Tramp from local system.
1998 (setq filename (expand-file-name filename))
1999 (setq newname (expand-file-name newname))
2000 ;; At least one file a Tramp file?
2001 (if (or (tramp-tramp-file-p filename)
2002 (tramp-tramp-file-p newname))
2003 (tramp-do-copy-or-rename-file
2004 'rename filename newname ok-if-already-exists t t)
2005 (tramp-run-real-handler
2006 'rename-file (list filename newname ok-if-already-exists))))
2008 (defun tramp-do-copy-or-rename-file
2009 (op filename newname &optional ok-if-already-exists keep-date
2010 preserve-uid-gid preserve-extended-attributes)
2011 "Copy or rename a remote file.
2012 OP must be `copy' or `rename' and indicates the operation to perform.
2013 FILENAME specifies the file to copy or rename, NEWNAME is the name of
2014 the new file (for copy) or the new name of the file (for rename).
2015 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2016 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2017 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2018 the uid and gid if both files are on the same host.
2019 PRESERVE-EXTENDED-ATTRIBUTES activates selinux and acl commands.
2021 This function is invoked by `tramp-sh-handle-copy-file' and
2022 `tramp-sh-handle-rename-file'. It is an error if OP is neither
2023 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
2024 file names."
2025 (unless (memq op '(copy rename))
2026 (error "Unknown operation `%s', must be `copy' or `rename'" op))
2027 (let ((t1 (tramp-tramp-file-p filename))
2028 (t2 (tramp-tramp-file-p newname))
2029 (length (nth 7 (file-attributes (file-truename filename))))
2030 (attributes (and preserve-extended-attributes
2031 (apply 'file-extended-attributes (list filename)))))
2033 (with-parsed-tramp-file-name (if t1 filename newname) nil
2034 (when (and (not ok-if-already-exists) (file-exists-p newname))
2035 (tramp-error
2036 v 'file-already-exists "File %s already exists" newname))
2038 (with-tramp-progress-reporter
2039 v 0 (format "%s %s to %s"
2040 (if (eq op 'copy) "Copying" "Renaming")
2041 filename newname)
2043 (cond
2044 ;; Both are Tramp files.
2045 ((and t1 t2)
2046 (with-parsed-tramp-file-name filename v1
2047 (with-parsed-tramp-file-name newname v2
2048 (cond
2049 ;; Shortcut: if method, host, user are the same for
2050 ;; both files, we invoke `cp' or `mv' on the remote
2051 ;; host directly.
2052 ((tramp-equal-remote filename newname)
2053 (tramp-do-copy-or-rename-file-directly
2054 op filename newname
2055 ok-if-already-exists keep-date preserve-uid-gid))
2057 ;; Try out-of-band operation.
2058 ((and
2059 (tramp-method-out-of-band-p v1 length)
2060 (tramp-method-out-of-band-p v2 length))
2061 (tramp-do-copy-or-rename-file-out-of-band
2062 op filename newname keep-date))
2064 ;; No shortcut was possible. So we copy the file
2065 ;; first. If the operation was `rename', we go back
2066 ;; and delete the original file (if the copy was
2067 ;; successful). The approach is simple-minded: we
2068 ;; create a new buffer, insert the contents of the
2069 ;; source file into it, then write out the buffer to
2070 ;; the target file. The advantage is that it doesn't
2071 ;; matter which file name handlers are used for the
2072 ;; source and target file.
2074 (tramp-do-copy-or-rename-file-via-buffer
2075 op filename newname keep-date))))))
2077 ;; One file is a Tramp file, the other one is local.
2078 ((or t1 t2)
2079 (cond
2080 ;; Fast track on local machine.
2081 ((tramp-local-host-p v)
2082 (tramp-do-copy-or-rename-file-directly
2083 op filename newname
2084 ok-if-already-exists keep-date preserve-uid-gid))
2086 ;; If the Tramp file has an out-of-band method, the
2087 ;; corresponding copy-program can be invoked.
2088 ((tramp-method-out-of-band-p v length)
2089 (tramp-do-copy-or-rename-file-out-of-band
2090 op filename newname keep-date))
2092 ;; Use the inline method via a Tramp buffer.
2093 (t (tramp-do-copy-or-rename-file-via-buffer
2094 op filename newname keep-date))))
2097 ;; One of them must be a Tramp file.
2098 (error "Tramp implementation says this cannot happen")))
2100 ;; Handle `preserve-extended-attributes'. We ignore possible
2101 ;; errors, because ACL strings could be incompatible.
2102 (when attributes
2103 (ignore-errors
2104 (apply 'set-file-extended-attributes (list newname attributes))))
2106 ;; In case of `rename', we must flush the cache of the source file.
2107 (when (and t1 (eq op 'rename))
2108 (with-parsed-tramp-file-name filename v1
2109 (tramp-flush-file-property v1 (file-name-directory v1-localname))
2110 (tramp-flush-file-property v1 v1-localname)))
2112 ;; When newname did exist, we have wrong cached values.
2113 (when t2
2114 (with-parsed-tramp-file-name newname v2
2115 (tramp-flush-file-property v2 (file-name-directory v2-localname))
2116 (tramp-flush-file-property v2 v2-localname)))))))
2118 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
2119 "Use an Emacs buffer to copy or rename a file.
2120 First arg OP is either `copy' or `rename' and indicates the operation.
2121 FILENAME is the source file, NEWNAME the target file.
2122 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2123 ;; We must disable multibyte, because binary data shall not be
2124 ;; converted. We don't want the target file to be compressed, so we
2125 ;; let-bind `jka-compr-inhibit' to t. `epa-file-handler' shall not
2126 ;; be called either. We remove `tramp-file-name-handler' from
2127 ;; `inhibit-file-name-handlers'; otherwise the file name handler for
2128 ;; `insert-file-contents' might be deactivated in some corner cases.
2129 (let ((coding-system-for-read 'binary)
2130 (coding-system-for-write 'binary)
2131 (jka-compr-inhibit t)
2132 (inhibit-file-name-handlers
2133 (cons epa-file-handler
2134 . (remq 'tramp-file-name-handler inhibit-file-name-handlers))))
2135 (with-temp-file newname
2136 (set-buffer-multibyte nil)
2137 (insert-file-contents-literally filename)))
2138 ;; KEEP-DATE handling.
2139 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
2140 ;; Set the mode.
2141 (set-file-modes newname (tramp-default-file-modes filename))
2142 ;; If the operation was `rename', delete the original file.
2143 (unless (eq op 'copy) (delete-file filename)))
2145 (defun tramp-do-copy-or-rename-file-directly
2146 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
2147 "Invokes `cp' or `mv' on the remote system.
2148 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2149 respectively. FILENAME specifies the file to copy or rename,
2150 NEWNAME is the name of the new file (for copy) or the new name of
2151 the file (for rename). Both files must reside on the same host.
2152 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2153 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2154 the uid and gid from FILENAME."
2155 (let ((t1 (tramp-tramp-file-p filename))
2156 (t2 (tramp-tramp-file-p newname))
2157 (file-times (nth 5 (file-attributes filename)))
2158 (file-modes (tramp-default-file-modes filename)))
2159 (with-parsed-tramp-file-name (if t1 filename newname) nil
2160 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
2161 ((eq op 'copy) "cp -f")
2162 ((eq op 'rename) "mv -f")
2163 (t (tramp-error
2164 v 'file-error
2165 "Unknown operation `%s', must be `copy' or `rename'"
2166 op))))
2167 (localname1
2168 (if t1
2169 (tramp-file-name-handler 'file-remote-p filename 'localname)
2170 filename))
2171 (localname2
2172 (if t2
2173 (tramp-file-name-handler 'file-remote-p newname 'localname)
2174 newname))
2175 (prefix (file-remote-p (if t1 filename newname)))
2176 cmd-result)
2178 (cond
2179 ;; Both files are on a remote host, with same user.
2180 ((and t1 t2)
2181 (setq cmd-result
2182 (tramp-send-command-and-check
2183 v (format "%s %s %s" cmd
2184 (tramp-shell-quote-argument localname1)
2185 (tramp-shell-quote-argument localname2))))
2186 (with-current-buffer (tramp-get-buffer v)
2187 (goto-char (point-min))
2188 (unless
2190 (and keep-date
2191 ;; Mask cp -f error.
2192 (re-search-forward
2193 tramp-operation-not-permitted-regexp nil t))
2194 cmd-result)
2195 (tramp-error-with-buffer
2196 nil v 'file-error
2197 "Copying directly failed, see buffer `%s' for details."
2198 (buffer-name)))))
2200 ;; We are on the local host.
2201 ((or t1 t2)
2202 (cond
2203 ;; We can do it directly.
2204 ((let (file-name-handler-alist)
2205 (and (file-readable-p localname1)
2206 ;; No sticky bit when renaming.
2207 (or (eq op 'copy)
2208 (zerop
2209 (logand
2210 (file-modes (file-name-directory localname1))
2211 (tramp-compat-octal-to-decimal "1000"))))
2212 (file-writable-p (file-name-directory localname2))
2213 (or (file-directory-p localname2)
2214 (file-writable-p localname2))))
2215 (if (eq op 'copy)
2216 (tramp-compat-copy-file
2217 localname1 localname2 ok-if-already-exists
2218 keep-date preserve-uid-gid)
2219 (tramp-run-real-handler
2220 'rename-file (list localname1 localname2 ok-if-already-exists))))
2222 ;; We can do it directly with `tramp-send-command'
2223 ((and (file-readable-p (concat prefix localname1))
2224 (file-writable-p
2225 (file-name-directory (concat prefix localname2)))
2226 (or (file-directory-p (concat prefix localname2))
2227 (file-writable-p (concat prefix localname2))))
2228 (tramp-do-copy-or-rename-file-directly
2229 op (concat prefix localname1) (concat prefix localname2)
2230 ok-if-already-exists keep-date t)
2231 ;; We must change the ownership to the local user.
2232 (tramp-set-file-uid-gid
2233 (concat prefix localname2)
2234 (tramp-get-local-uid 'integer)
2235 (tramp-get-local-gid 'integer)))
2237 ;; We need a temporary file in between.
2239 ;; Create the temporary file.
2240 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
2241 (unwind-protect
2242 (progn
2243 (cond
2245 (tramp-barf-unless-okay
2246 v (format
2247 "%s %s %s" cmd
2248 (tramp-shell-quote-argument localname1)
2249 (tramp-shell-quote-argument tmpfile))
2250 "Copying directly failed, see buffer `%s' for details."
2251 (tramp-get-buffer v))
2252 ;; We must change the ownership as remote user.
2253 ;; Since this does not work reliable, we also
2254 ;; give read permissions.
2255 (set-file-modes
2256 (concat prefix tmpfile)
2257 (tramp-compat-octal-to-decimal "0777"))
2258 (tramp-set-file-uid-gid
2259 (concat prefix tmpfile)
2260 (tramp-get-local-uid 'integer)
2261 (tramp-get-local-gid 'integer)))
2263 (if (eq op 'copy)
2264 (tramp-compat-copy-file
2265 localname1 tmpfile t
2266 keep-date preserve-uid-gid)
2267 (tramp-run-real-handler
2268 'rename-file
2269 (list localname1 tmpfile t)))
2270 ;; We must change the ownership as local user.
2271 ;; Since this does not work reliable, we also
2272 ;; give read permissions.
2273 (set-file-modes
2274 tmpfile (tramp-compat-octal-to-decimal "0777"))
2275 (tramp-set-file-uid-gid
2276 tmpfile
2277 (tramp-get-remote-uid v 'integer)
2278 (tramp-get-remote-gid v 'integer))))
2280 ;; Move the temporary file to its destination.
2281 (cond
2283 (tramp-barf-unless-okay
2284 v (format
2285 "cp -f -p %s %s"
2286 (tramp-shell-quote-argument tmpfile)
2287 (tramp-shell-quote-argument localname2))
2288 "Copying directly failed, see buffer `%s' for details."
2289 (tramp-get-buffer v)))
2291 (tramp-run-real-handler
2292 'rename-file
2293 (list tmpfile localname2 ok-if-already-exists)))))
2295 ;; Save exit.
2296 (ignore-errors (delete-file tmpfile)))))))))
2298 ;; Set the time and mode. Mask possible errors.
2299 (ignore-errors
2300 (when keep-date
2301 (set-file-times newname file-times)
2302 (set-file-modes newname file-modes))))))
2304 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
2305 "Invoke `scp' program to copy.
2306 The method used must be an out-of-band method."
2307 (let* ((t1 (tramp-tramp-file-p filename))
2308 (t2 (tramp-tramp-file-p newname))
2309 (orig-vec (tramp-dissect-file-name (if t1 filename newname)))
2310 copy-program copy-args copy-env copy-keep-date port listener spec
2311 options source target remote-copy-program remote-copy-args)
2313 (with-parsed-tramp-file-name (if t1 filename newname) nil
2314 (if (and t1 t2)
2316 ;; Both are Tramp files. We shall optimize it when the
2317 ;; methods for FILENAME and NEWNAME are the same.
2318 (let* ((dir-flag (file-directory-p filename))
2319 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
2320 (if dir-flag
2321 (setq tmpfile
2322 (expand-file-name
2323 (file-name-nondirectory newname) tmpfile)))
2324 (unwind-protect
2325 (progn
2326 (tramp-do-copy-or-rename-file-out-of-band
2327 op filename tmpfile keep-date)
2328 (tramp-do-copy-or-rename-file-out-of-band
2329 'rename tmpfile newname keep-date))
2330 ;; Save exit.
2331 (ignore-errors
2332 (if dir-flag
2333 (tramp-compat-delete-directory
2334 (expand-file-name ".." tmpfile) 'recursive)
2335 (delete-file tmpfile)))))
2337 ;; Set variables for computing the prompt for reading
2338 ;; password.
2339 (setq tramp-current-method (tramp-file-name-method v)
2340 tramp-current-user (or (tramp-file-name-user v)
2341 (tramp-get-connection-property
2342 v "login-as" nil))
2343 tramp-current-host (tramp-file-name-real-host v))
2345 ;; Expand hops. Might be necessary for gateway methods.
2346 (setq v (car (tramp-compute-multi-hops v)))
2347 (aset v 3 localname)
2349 ;; Check which ones of source and target are Tramp files.
2350 (setq source (if t1
2351 (tramp-make-copy-program-file-name v)
2352 (shell-quote-argument filename))
2353 target (funcall
2354 (if (and (file-directory-p filename)
2355 (string-equal
2356 (file-name-nondirectory filename)
2357 (file-name-nondirectory newname)))
2358 'file-name-directory
2359 'identity)
2360 (if t2
2361 (tramp-make-copy-program-file-name v)
2362 (shell-quote-argument newname))))
2364 ;; Check for host and port number. We cannot use
2365 ;; `tramp-file-name-port', because this returns also
2366 ;; `tramp-default-port', which might clash with settings in
2367 ;; "~/.ssh/config".
2368 (setq host (tramp-file-name-host v)
2369 port "")
2370 (when (string-match tramp-host-with-port-regexp host)
2371 (setq port (string-to-number (match-string 2 host))
2372 host (string-to-number (match-string 1 host))))
2374 ;; Check for user. There might be an interactive setting.
2375 (setq user (or (tramp-file-name-user v)
2376 (tramp-get-connection-property v "login-as" nil)))
2378 ;; Check for listener port.
2379 (when (tramp-get-method-parameter method 'tramp-remote-copy-args)
2380 (setq listener (number-to-string (+ 50000 (random 10000))))
2381 (while
2382 (zerop (tramp-call-process v "nc" nil nil nil "-z" host listener))
2383 (setq listener (number-to-string (+ 50000 (random 10000))))))
2385 ;; Compose copy command.
2386 (setq host (or host "")
2387 user (or user "")
2388 port (or port "")
2389 spec (format-spec-make
2390 ?t (tramp-get-connection-property
2391 (tramp-get-connection-process v) "temp-file" ""))
2392 options (format-spec (tramp-ssh-controlmaster-options v) spec)
2393 spec (format-spec-make
2394 ?h host ?u user ?p port ?r listener ?c options
2395 ?k (if keep-date " " ""))
2396 copy-program (tramp-get-method-parameter
2397 method 'tramp-copy-program)
2398 copy-keep-date (tramp-get-method-parameter
2399 method 'tramp-copy-keep-date)
2401 copy-args
2402 (delete
2403 ;; " " has either been a replacement of "%k" (when
2404 ;; keep-date argument is non-nil), or a replacement
2405 ;; for the whole keep-date sublist.
2407 (dolist
2409 (tramp-get-method-parameter method 'tramp-copy-args)
2410 copy-args)
2411 (setq copy-args
2412 (append
2413 copy-args
2414 (let ((y (mapcar (lambda (z) (format-spec z spec)) x)))
2415 (if (member "" y) '(" ") y))))))
2417 copy-env
2418 (delq
2420 (mapcar
2421 (lambda (x)
2422 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
2423 (unless (member "" x) (mapconcat 'identity x " ")))
2424 (tramp-get-method-parameter method 'tramp-copy-env)))
2426 remote-copy-program
2427 (tramp-get-method-parameter method 'tramp-remote-copy-program))
2429 (dolist
2432 (tramp-get-connection-property v "remote-copy-args" nil)
2433 (tramp-get-method-parameter method 'tramp-remote-copy-args)))
2434 (setq remote-copy-args
2435 (append
2436 remote-copy-args
2437 (let ((y (mapcar (lambda (z) (format-spec z spec)) x)))
2438 (if (member "" y) '(" ") y)))))
2440 ;; Check for local copy program.
2441 (unless (executable-find copy-program)
2442 (tramp-error
2443 v 'file-error "Cannot find local copy program: %s" copy-program))
2445 ;; Install listener on the remote side. The prompt must be
2446 ;; consumed later on, when the process does not listen anymore.
2447 (when remote-copy-program
2448 (unless (with-tramp-connection-property
2449 v (concat "remote-copy-program-" remote-copy-program)
2450 (tramp-find-executable
2451 v remote-copy-program (tramp-get-remote-path v)))
2452 (tramp-error
2453 v 'file-error
2454 "Cannot find remote listener: %s" remote-copy-program))
2455 (setq remote-copy-program
2456 (mapconcat
2457 'identity
2458 (append
2459 (list remote-copy-program) remote-copy-args
2460 (list (if t1 (concat "<" source) (concat ">" target)) "&"))
2461 " "))
2462 (tramp-send-command v remote-copy-program)
2463 (with-timeout
2464 (1 (tramp-error
2465 v 'file-error
2466 "Listener process not running on remote host: `%s'"
2467 remote-copy-program))
2468 (tramp-send-command v (format "netstat -l | grep -q :%s" listener))
2469 (while (not (tramp-send-command-and-check v nil))
2470 (tramp-send-command
2471 v (format "netstat -l | grep -q :%s" listener)))))
2473 (with-temp-buffer
2474 (unwind-protect
2475 ;; The default directory must be remote.
2476 (let ((default-directory
2477 (file-name-directory (if t1 filename newname)))
2478 (process-environment (copy-sequence process-environment)))
2479 ;; Set the transfer process properties.
2480 (tramp-set-connection-property
2481 v "process-name" (buffer-name (current-buffer)))
2482 (tramp-set-connection-property
2483 v "process-buffer" (current-buffer))
2484 (while copy-env
2485 (tramp-message
2486 orig-vec 6 "%s=\"%s\"" (car copy-env) (cadr copy-env))
2487 (setenv (pop copy-env) (pop copy-env)))
2488 (setq
2489 copy-args
2490 (append
2491 copy-args
2492 (if remote-copy-program
2493 (list (if t1 (concat ">" target) (concat "<" source)))
2494 (list source target))))
2496 ;; Use an asynchronous process. By this, password can
2497 ;; be handled. We don't set a timeout, because the
2498 ;; copying of large files can last longer than 60
2499 ;; secs.
2500 (let ((p (apply 'start-process-shell-command
2501 (tramp-get-connection-name v)
2502 (tramp-get-connection-buffer v)
2503 copy-program
2504 (append
2505 copy-args
2506 (list "&&" "echo" "tramp_exit_status" "0"
2507 "||" "echo" "tramp_exit_status" "1")))))
2508 (tramp-message
2509 orig-vec 6 "%s"
2510 (mapconcat 'identity (process-command p) " "))
2511 (tramp-set-connection-property p "vector" orig-vec)
2512 (tramp-compat-set-process-query-on-exit-flag p nil)
2514 ;; We must adapt `tramp-local-end-of-line' for
2515 ;; sending the password.
2516 (let ((tramp-local-end-of-line tramp-rsh-end-of-line))
2517 (tramp-process-actions
2518 p v nil tramp-actions-copy-out-of-band))
2520 ;; Check the return code.
2521 (goto-char (point-max))
2522 (unless
2523 (re-search-backward "tramp_exit_status [0-9]+" nil t)
2524 (tramp-error
2525 orig-vec 'file-error
2526 "Couldn't find exit status of `%s'"
2527 (mapconcat 'identity (process-command p) " ")))
2528 (skip-chars-forward "^ ")
2529 (unless (zerop (read (current-buffer)))
2530 (forward-line -1)
2531 (tramp-error
2532 orig-vec 'file-error
2533 "Error copying: `%s'"
2534 (buffer-substring (point-min) (point-at-eol))))))
2536 ;; Reset the transfer process properties.
2537 (tramp-set-connection-property v "process-name" nil)
2538 (tramp-set-connection-property v "process-buffer" nil)
2539 ;; Clear the remote prompt.
2540 (when (and remote-copy-program
2541 (not (tramp-send-command-and-check v nil)))
2542 ;; Houston, we have a problem! Likely, the listener is
2543 ;; still running, so let's clear everything (but the
2544 ;; cached password).
2545 (tramp-cleanup-connection v 'keep-debug 'keep-password))))
2547 ;; Handle KEEP-DATE argument.
2548 (when (and keep-date (not copy-keep-date))
2549 (set-file-times newname (nth 5 (file-attributes filename))))
2551 ;; Set the mode.
2552 (unless (and keep-date copy-keep-date)
2553 (ignore-errors
2554 (set-file-modes newname (tramp-default-file-modes filename)))))
2556 ;; If the operation was `rename', delete the original file.
2557 (unless (eq op 'copy)
2558 (if (file-regular-p filename)
2559 (delete-file filename)
2560 (tramp-compat-delete-directory filename 'recursive))))))
2562 (defun tramp-sh-handle-make-directory (dir &optional parents)
2563 "Like `make-directory' for Tramp files."
2564 (setq dir (expand-file-name dir))
2565 (with-parsed-tramp-file-name dir nil
2566 (tramp-flush-directory-property v (file-name-directory localname))
2567 (save-excursion
2568 (tramp-barf-unless-okay
2569 v (format "%s %s"
2570 (if parents "mkdir -p" "mkdir")
2571 (tramp-shell-quote-argument localname))
2572 "Couldn't make directory %s" dir))))
2574 (defun tramp-sh-handle-delete-directory (directory &optional recursive)
2575 "Like `delete-directory' for Tramp files."
2576 (setq directory (expand-file-name directory))
2577 (with-parsed-tramp-file-name directory nil
2578 (tramp-flush-file-property v (file-name-directory localname))
2579 (tramp-flush-directory-property v localname)
2580 (tramp-barf-unless-okay
2581 v (format "cd / && %s %s"
2582 (if recursive "rm -rf" "rmdir")
2583 (tramp-shell-quote-argument localname))
2584 "Couldn't delete %s" directory)))
2586 (defun tramp-sh-handle-delete-file (filename &optional trash)
2587 "Like `delete-file' for Tramp files."
2588 (setq filename (expand-file-name filename))
2589 (with-parsed-tramp-file-name filename nil
2590 (tramp-flush-file-property v (file-name-directory localname))
2591 (tramp-flush-file-property v localname)
2592 (tramp-barf-unless-okay
2593 v (format "%s %s"
2594 (or (and trash (tramp-get-remote-trash v)) "rm -f")
2595 (tramp-shell-quote-argument localname))
2596 "Couldn't delete %s" filename)))
2598 ;; Dired.
2600 ;; CCC: This does not seem to be enough. Something dies when
2601 ;; we try and delete two directories under Tramp :/
2602 (defun tramp-sh-handle-dired-recursive-delete-directory (filename)
2603 "Recursively delete the directory given.
2604 This is like `dired-recursive-delete-directory' for Tramp files."
2605 (with-parsed-tramp-file-name filename nil
2606 ;; Run a shell command 'rm -r <localname>'.
2607 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
2608 (unless (file-exists-p filename)
2609 (tramp-error v 'file-error "No such directory: %s" filename))
2610 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>).
2611 (tramp-send-command
2613 (format "rm -rf %s" (tramp-shell-quote-argument localname))
2614 ;; Don't read the output, do it explicitly.
2615 nil t)
2616 ;; Wait for the remote system to return to us...
2617 ;; This might take a while, allow it plenty of time.
2618 (tramp-wait-for-output (tramp-get-connection-process v) 120)
2619 ;; Make sure that it worked...
2620 (tramp-flush-file-property v (file-name-directory localname))
2621 (tramp-flush-directory-property v localname)
2622 (and (file-exists-p filename)
2623 (tramp-error
2624 v 'file-error "Failed to recursively delete %s" filename))))
2626 (defun tramp-sh-handle-dired-compress-file (file &rest _ok-flag)
2627 "Like `dired-compress-file' for Tramp files."
2628 ;; OK-FLAG is valid for XEmacs only, but not implemented.
2629 ;; Code stolen mainly from dired-aux.el.
2630 (with-parsed-tramp-file-name file nil
2631 (tramp-flush-file-property v localname)
2632 (save-excursion
2633 (let ((suffixes
2634 (if (not (featurep 'xemacs))
2635 ;; Emacs case
2636 (symbol-value 'dired-compress-file-suffixes)
2637 ;; XEmacs has `dired-compression-method-alist', which is
2638 ;; transformed into `dired-compress-file-suffixes' structure.
2639 (mapcar
2640 (lambda (x)
2641 (list (concat (regexp-quote (nth 1 x)) "\\'")
2643 (mapconcat 'identity (nth 3 x) " ")))
2644 (symbol-value 'dired-compression-method-alist))))
2645 suffix)
2646 ;; See if any suffix rule matches this file name.
2647 (while suffixes
2648 (let (case-fold-search)
2649 (if (string-match (car (car suffixes)) localname)
2650 (setq suffix (car suffixes) suffixes nil))
2651 (setq suffixes (cdr suffixes))))
2653 (cond ((file-symlink-p file)
2654 nil)
2655 ((and suffix (nth 2 suffix))
2656 ;; We found an uncompression rule.
2657 (with-tramp-progress-reporter
2658 v 0 (format "Uncompressing %s" file)
2659 (when (tramp-send-command-and-check
2660 v (concat (nth 2 suffix) " "
2661 (tramp-shell-quote-argument localname)))
2662 ;; `dired-remove-file' is not defined in XEmacs.
2663 (tramp-compat-funcall 'dired-remove-file file)
2664 (string-match (car suffix) file)
2665 (concat (substring file 0 (match-beginning 0))))))
2667 ;; We don't recognize the file as compressed, so compress it.
2668 ;; Try gzip.
2669 (with-tramp-progress-reporter v 0 (format "Compressing %s" file)
2670 (when (tramp-send-command-and-check
2671 v (concat "gzip -f "
2672 (tramp-shell-quote-argument localname)))
2673 ;; `dired-remove-file' is not defined in XEmacs.
2674 (tramp-compat-funcall 'dired-remove-file file)
2675 (cond ((file-exists-p (concat file ".gz"))
2676 (concat file ".gz"))
2677 ((file-exists-p (concat file ".z"))
2678 (concat file ".z"))
2679 (t nil))))))))))
2681 (defun tramp-sh-handle-insert-directory
2682 (filename switches &optional wildcard full-directory-p)
2683 "Like `insert-directory' for Tramp files."
2684 (setq filename (expand-file-name filename))
2685 (unless switches (setq switches ""))
2686 (with-parsed-tramp-file-name filename nil
2687 (if (and (featurep 'ls-lisp)
2688 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
2689 (tramp-handle-insert-directory
2690 filename switches wildcard full-directory-p)
2691 (when (stringp switches)
2692 (setq switches (split-string switches)))
2693 (when (and (member "--dired" switches)
2694 (not (tramp-get-ls-command-with-dired v)))
2695 (setq switches (delete "--dired" switches)))
2696 (when wildcard
2697 (setq wildcard (tramp-run-real-handler
2698 'file-name-nondirectory (list localname)))
2699 (setq localname (tramp-run-real-handler
2700 'file-name-directory (list localname))))
2701 (unless (or full-directory-p (member "-d" switches))
2702 (setq switches (append switches '("-d"))))
2703 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
2704 (when wildcard
2705 (setq switches (concat switches " " wildcard)))
2706 (tramp-message
2707 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2708 switches filename (if wildcard "yes" "no")
2709 (if full-directory-p "yes" "no"))
2710 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2711 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2712 (if full-directory-p
2713 (tramp-send-command
2715 (format "%s %s %s 2>/dev/null"
2716 (tramp-get-ls-command v)
2717 switches
2718 (if wildcard
2719 localname
2720 (tramp-shell-quote-argument (concat localname ".")))))
2721 (tramp-barf-unless-okay
2723 (format "cd %s" (tramp-shell-quote-argument
2724 (tramp-run-real-handler
2725 'file-name-directory (list localname))))
2726 "Couldn't `cd %s'"
2727 (tramp-shell-quote-argument
2728 (tramp-run-real-handler 'file-name-directory (list localname))))
2729 (tramp-send-command
2731 (format "%s %s %s 2>/dev/null"
2732 (tramp-get-ls-command v)
2733 switches
2734 (if (or wildcard
2735 (zerop (length
2736 (tramp-run-real-handler
2737 'file-name-nondirectory (list localname)))))
2739 (tramp-shell-quote-argument
2740 (tramp-run-real-handler
2741 'file-name-nondirectory (list localname)))))))
2743 (save-restriction
2744 (let ((beg (point)))
2745 (narrow-to-region (point) (point))
2746 ;; We cannot use `insert-buffer-substring' because the Tramp
2747 ;; buffer changes its contents before insertion due to calling
2748 ;; `expand-file' and alike.
2749 (insert
2750 (with-current-buffer (tramp-get-buffer v)
2751 (buffer-string)))
2753 ;; Check for "--dired" output.
2754 (forward-line -2)
2755 (when (looking-at "//SUBDIRED//")
2756 (forward-line -1))
2757 (when (looking-at "//DIRED//\\s-+")
2758 (let ((databeg (match-end 0))
2759 (end (point-at-eol)))
2760 ;; Now read the numeric positions of file names.
2761 (goto-char databeg)
2762 (while (< (point) end)
2763 (let ((start (+ beg (read (current-buffer))))
2764 (end (+ beg (read (current-buffer)))))
2765 (if (memq (char-after end) '(?\n ?\ ))
2766 ;; End is followed by \n or by " -> ".
2767 (put-text-property start end 'dired-filename t))))))
2768 ;; Remove trailing lines.
2769 (goto-char (point-at-bol))
2770 (while (looking-at "//")
2771 (forward-line 1)
2772 (delete-region (match-beginning 0) (point)))
2774 ;; Some busyboxes are reluctant to discard colors.
2775 (unless
2776 (string-match "color" (tramp-get-connection-property v "ls" ""))
2777 (goto-char beg)
2778 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
2779 (replace-match "")))
2781 ;; Decode the output, it could be multibyte.
2782 (decode-coding-region
2783 beg (point-max)
2784 (or file-name-coding-system
2785 (and (boundp 'default-file-name-coding-system)
2786 (symbol-value 'default-file-name-coding-system))))
2788 ;; The inserted file could be from somewhere else.
2789 (when (and (not wildcard) (not full-directory-p))
2790 (goto-char (point-max))
2791 (when (file-symlink-p filename)
2792 (goto-char (search-backward "->" beg 'noerror)))
2793 (search-backward
2794 (if (zerop (length (file-name-nondirectory filename)))
2796 (file-name-nondirectory filename))
2797 beg 'noerror)
2798 (replace-match (file-relative-name filename) t))
2800 (goto-char (point-max)))))))
2802 ;; Canonicalization of file names.
2804 (defun tramp-sh-handle-expand-file-name (name &optional dir)
2805 "Like `expand-file-name' for Tramp files.
2806 If the localname part of the given file name starts with \"/../\" then
2807 the result will be a local, non-Tramp, file name."
2808 ;; If DIR is not given, use `default-directory' or "/".
2809 (setq dir (or dir default-directory "/"))
2810 ;; Unless NAME is absolute, concat DIR and NAME.
2811 (unless (file-name-absolute-p name)
2812 (setq name (concat (file-name-as-directory dir) name)))
2813 ;; If NAME is not a Tramp file, run the real handler.
2814 (if (not (tramp-connectable-p name))
2815 (tramp-run-real-handler 'expand-file-name (list name nil))
2816 ;; Dissect NAME.
2817 (with-parsed-tramp-file-name name nil
2818 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
2819 (setq localname (concat "~/" localname)))
2820 ;; Tilde expansion if necessary. This needs a shell which
2821 ;; groks tilde expansion! The function `tramp-find-shell' is
2822 ;; supposed to find such a shell on the remote host. Please
2823 ;; tell me about it when this doesn't work on your system.
2824 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
2825 (let ((uname (match-string 1 localname))
2826 (fname (match-string 2 localname)))
2827 ;; We cannot simply apply "~/", because under sudo "~/" is
2828 ;; expanded to the local user home directory but to the
2829 ;; root home directory. On the other hand, using always
2830 ;; the default user name for tilde expansion is not
2831 ;; appropriate either, because ssh and companions might
2832 ;; use a user name from the config file.
2833 (when (and (string-equal uname "~")
2834 (string-match "\\`su\\(do\\)?\\'" method))
2835 (setq uname (concat uname user)))
2836 (setq uname
2837 (with-tramp-connection-property v uname
2838 (tramp-send-command
2839 v (format "cd %s && pwd" (tramp-shell-quote-argument uname)))
2840 (with-current-buffer (tramp-get-buffer v)
2841 (goto-char (point-min))
2842 (buffer-substring (point) (point-at-eol)))))
2843 (setq localname (concat uname fname))))
2844 ;; There might be a double slash, for example when "~/"
2845 ;; expands to "/". Remove this.
2846 (while (string-match "//" localname)
2847 (setq localname (replace-match "/" t t localname)))
2848 ;; No tilde characters in file name, do normal
2849 ;; `expand-file-name' (this does "/./" and "/../"). We bind
2850 ;; `directory-sep-char' here for XEmacs on Windows, which would
2851 ;; otherwise use backslash. `default-directory' is bound,
2852 ;; because on Windows there would be problems with UNC shares or
2853 ;; Cygwin mounts.
2854 (let ((directory-sep-char ?/)
2855 (default-directory (tramp-compat-temporary-file-directory)))
2856 (tramp-make-tramp-file-name
2857 method user host
2858 (tramp-drop-volume-letter
2859 (tramp-run-real-handler
2860 'expand-file-name (list localname)))
2861 hop)))))
2863 ;;; Remote commands:
2865 (defun tramp-process-sentinel (proc event)
2866 "Flush file caches."
2867 (unless (memq (process-status proc) '(run open))
2868 (let ((vec (tramp-get-connection-property proc "vector" nil)))
2869 (when vec
2870 (tramp-message vec 5 "Sentinel called: `%S' `%s'" proc event)
2871 (tramp-flush-connection-property proc)
2872 (tramp-flush-directory-property vec "")))))
2874 ;; We use BUFFER also as connection buffer during setup. Because of
2875 ;; this, its original contents must be saved, and restored once
2876 ;; connection has been setup.
2877 (defun tramp-sh-handle-start-file-process (name buffer program &rest args)
2878 "Like `start-file-process' for Tramp files."
2879 (with-parsed-tramp-file-name (expand-file-name default-directory) nil
2880 (let* (;; When PROGRAM matches "*sh", and the first arg is "-c",
2881 ;; it might be that the arguments exceed the command line
2882 ;; length. Therefore, we modify the command.
2883 (heredoc (and (stringp program)
2884 (string-match "sh$" program)
2885 (string-equal "-c" (car args))
2886 (= (length args) 2)))
2887 ;; When PROGRAM is nil, we just provide a tty.
2888 (args (if (not heredoc) args
2889 (let ((i 250))
2890 (while (and (< i (length (cadr args)))
2891 (string-match " " (cadr args) i))
2892 (setcdr
2893 args
2894 (list (replace-match " \\\\\n" nil nil (cadr args))))
2895 (setq i (+ i 250))))
2896 (cdr args)))
2897 ;; Use a human-friendly prompt, for example for `shell'.
2898 (prompt (format "PS1=%s"
2899 (format "%s %s"
2900 (file-remote-p default-directory)
2901 tramp-initial-end-of-output)))
2902 ;; We use as environment the difference to toplevel
2903 ;; `process-environment'.
2905 (env
2906 (dolist
2907 (elt
2908 (cons prompt (nreverse (copy-sequence process-environment)))
2909 env)
2910 (or (member elt (default-toplevel-value 'process-environment))
2911 (setq env (cons elt env)))))
2912 (command
2913 (when (stringp program)
2914 (format "cd %s && exec %s env %s %s"
2915 (tramp-shell-quote-argument localname)
2916 (if heredoc (format "<<'%s'" tramp-end-of-heredoc) "")
2917 (mapconcat 'tramp-shell-quote-argument env " ")
2918 (if heredoc
2919 (format "%s\n(\n%s\n) </dev/tty\n%s"
2920 program (car args) tramp-end-of-heredoc)
2921 (mapconcat 'tramp-shell-quote-argument
2922 (cons program args) " ")))))
2923 (tramp-process-connection-type
2924 (or (null program) tramp-process-connection-type))
2925 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
2926 (name1 name)
2927 (i 0)
2928 ;; We do not want to raise an error when
2929 ;; `start-file-process' has been started several times in
2930 ;; `eshell' and friends.
2931 (tramp-current-connection nil))
2933 (unless buffer
2934 ;; BUFFER can be nil. We use a temporary buffer.
2935 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
2936 (while (get-process name1)
2937 ;; NAME must be unique as process name.
2938 (setq i (1+ i)
2939 name1 (format "%s<%d>" name i)))
2940 (setq name name1)
2941 ;; Set the new process properties.
2942 (tramp-set-connection-property v "process-name" name)
2943 (tramp-set-connection-property v "process-buffer" buffer)
2945 (with-current-buffer (tramp-get-connection-buffer v)
2946 (unwind-protect
2947 ;; We catch this event. Otherwise, `start-process' could
2948 ;; be called on the local host.
2949 (save-excursion
2950 (save-restriction
2951 ;; Activate narrowing in order to save BUFFER
2952 ;; contents. Clear also the modification time;
2953 ;; otherwise we might be interrupted by
2954 ;; `verify-visited-file-modtime'.
2955 (let ((buffer-undo-list t)
2956 (buffer-read-only nil)
2957 (mark (point-max)))
2958 (clear-visited-file-modtime)
2959 (narrow-to-region (point-max) (point-max))
2960 ;; We call `tramp-maybe-open-connection', in order
2961 ;; to cleanup the prompt afterwards.
2962 (catch 'suppress
2963 (tramp-maybe-open-connection v)
2964 (widen)
2965 (delete-region mark (point))
2966 (narrow-to-region (point-max) (point-max))
2967 ;; Now do it.
2968 (if command
2969 ;; Send the command.
2970 (tramp-send-command v command nil t) ; nooutput
2971 ;; Check, whether a pty is associated.
2972 (unless (tramp-compat-process-get
2973 (tramp-get-connection-process v) 'remote-tty)
2974 (tramp-error
2975 v 'file-error
2976 "pty association is not supported for `%s'" name))))
2977 (let ((p (tramp-get-connection-process v)))
2978 ;; Set query flag and process marker for this
2979 ;; process. We ignore errors, because the process
2980 ;; could have finished already.
2981 (ignore-errors
2982 (tramp-compat-set-process-query-on-exit-flag p t)
2983 (set-marker (process-mark p) (point)))
2984 ;; Return process.
2985 p))))
2987 ;; Save exit.
2988 (if (string-match tramp-temp-buffer-name (buffer-name))
2989 (ignore-errors
2990 (set-process-buffer (tramp-get-connection-process v) nil)
2991 (kill-buffer (current-buffer)))
2992 (set-buffer-modified-p bmp))
2993 (tramp-set-connection-property v "process-name" nil)
2994 (tramp-set-connection-property v "process-buffer" nil))))))
2996 (defun tramp-sh-handle-process-file
2997 (program &optional infile destination display &rest args)
2998 "Like `process-file' for Tramp files."
2999 ;; The implementation is not complete yet.
3000 (when (and (numberp destination) (zerop destination))
3001 (error "Implementation does not handle immediate return"))
3003 (with-parsed-tramp-file-name default-directory nil
3004 (let (command env input tmpinput stderr tmpstderr outbuf ret)
3005 ;; Compute command.
3006 (setq command (mapconcat 'tramp-shell-quote-argument
3007 (cons program args) " "))
3008 ;; We use as environment the difference to toplevel `process-environment'.
3009 (setq env
3010 (dolist (elt (nreverse (copy-sequence process-environment)) env)
3011 (or (member elt (default-toplevel-value 'process-environment))
3012 (setq env (cons elt env)))))
3013 (when env
3014 (setq command
3015 (format
3016 "env %s %s"
3017 (mapconcat 'tramp-shell-quote-argument env " ") command)))
3018 ;; Determine input.
3019 (if (null infile)
3020 (setq input "/dev/null")
3021 (setq infile (expand-file-name infile))
3022 (if (tramp-equal-remote default-directory infile)
3023 ;; INFILE is on the same remote host.
3024 (setq input (with-parsed-tramp-file-name infile nil localname))
3025 ;; INFILE must be copied to remote host.
3026 (setq input (tramp-make-tramp-temp-file v)
3027 tmpinput (tramp-make-tramp-file-name method user host input))
3028 (copy-file infile tmpinput t)))
3029 (when input (setq command (format "%s <%s" command input)))
3031 ;; Determine output.
3032 (cond
3033 ;; Just a buffer.
3034 ((bufferp destination)
3035 (setq outbuf destination))
3036 ;; A buffer name.
3037 ((stringp destination)
3038 (setq outbuf (get-buffer-create destination)))
3039 ;; (REAL-DESTINATION ERROR-DESTINATION)
3040 ((consp destination)
3041 ;; output.
3042 (cond
3043 ((bufferp (car destination))
3044 (setq outbuf (car destination)))
3045 ((stringp (car destination))
3046 (setq outbuf (get-buffer-create (car destination))))
3047 ((car destination)
3048 (setq outbuf (current-buffer))))
3049 ;; stderr.
3050 (cond
3051 ((stringp (cadr destination))
3052 (setcar (cdr destination) (expand-file-name (cadr destination)))
3053 (if (tramp-equal-remote default-directory (cadr destination))
3054 ;; stderr is on the same remote host.
3055 (setq stderr (with-parsed-tramp-file-name
3056 (cadr destination) nil localname))
3057 ;; stderr must be copied to remote host. The temporary
3058 ;; file must be deleted after execution.
3059 (setq stderr (tramp-make-tramp-temp-file v)
3060 tmpstderr (tramp-make-tramp-file-name
3061 method user host stderr))))
3062 ;; stderr to be discarded.
3063 ((null (cadr destination))
3064 (setq stderr "/dev/null"))))
3065 ;; 't
3066 (destination
3067 (setq outbuf (current-buffer))))
3068 (when stderr (setq command (format "%s 2>%s" command stderr)))
3070 ;; Send the command. It might not return in time, so we protect
3071 ;; it. Call it in a subshell, in order to preserve working
3072 ;; directory.
3073 (condition-case nil
3074 (unwind-protect
3075 (setq ret
3076 (if (tramp-send-command-and-check
3077 v (format "cd %s && %s"
3078 (tramp-shell-quote-argument localname)
3079 command)
3080 t t)
3081 0 1))
3082 ;; We should add the output anyway.
3083 (when outbuf
3084 (with-current-buffer outbuf
3085 (insert
3086 (with-current-buffer (tramp-get-connection-buffer v)
3087 (buffer-string))))
3088 (when (and display (get-buffer-window outbuf t)) (redisplay))))
3089 ;; When the user did interrupt, we should do it also. We use
3090 ;; return code -1 as marker.
3091 (quit
3092 (kill-buffer (tramp-get-connection-buffer v))
3093 (setq ret -1))
3094 ;; Handle errors.
3095 (error
3096 (kill-buffer (tramp-get-connection-buffer v))
3097 (setq ret 1)))
3099 ;; Provide error file.
3100 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
3102 ;; Cleanup. We remove all file cache values for the connection,
3103 ;; because the remote process could have changed them.
3104 (when tmpinput (delete-file tmpinput))
3106 ;; `process-file-side-effects' has been introduced with GNU
3107 ;; Emacs 23.2. If set to nil, no remote file will be changed
3108 ;; by `program'. If it doesn't exist, we assume its default
3109 ;; value t.
3110 (unless (and (boundp 'process-file-side-effects)
3111 (not (symbol-value 'process-file-side-effects)))
3112 (tramp-flush-directory-property v ""))
3114 ;; Return exit status.
3115 (if (equal ret -1)
3116 (keyboard-quit)
3117 ret))))
3119 (defun tramp-sh-handle-file-local-copy (filename)
3120 "Like `file-local-copy' for Tramp files."
3121 (with-parsed-tramp-file-name filename nil
3122 (unless (file-exists-p filename)
3123 (tramp-error
3124 v 'file-error
3125 "Cannot make local copy of non-existing file `%s'" filename))
3127 (let* ((size (nth 7 (file-attributes (file-truename filename))))
3128 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
3129 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
3130 (tmpfile (tramp-compat-make-temp-file filename)))
3132 (condition-case err
3133 (cond
3134 ;; `copy-file' handles direct copy and out-of-band methods.
3135 ((or (tramp-local-host-p v)
3136 (tramp-method-out-of-band-p v size))
3137 (copy-file filename tmpfile t t))
3139 ;; Use inline encoding for file transfer.
3140 (rem-enc
3141 (save-excursion
3142 (with-tramp-progress-reporter
3144 (format "Encoding remote file `%s' with `%s'" filename rem-enc)
3145 (tramp-barf-unless-okay
3146 v (format rem-enc (tramp-shell-quote-argument localname))
3147 "Encoding remote file failed"))
3149 (with-tramp-progress-reporter
3150 v 3 (format "Decoding local file `%s' with `%s'"
3151 tmpfile loc-dec)
3152 (if (functionp loc-dec)
3153 ;; If local decoding is a function, we call it.
3154 ;; We must disable multibyte, because
3155 ;; `uudecode-decode-region' doesn't handle it
3156 ;; correctly. Unset `file-name-handler-alist'.
3157 ;; Otherwise, epa-file gets confused.
3158 (let (file-name-handler-alist
3159 (coding-system-for-write 'binary))
3160 (with-temp-file tmpfile
3161 (set-buffer-multibyte nil)
3162 (insert-buffer-substring (tramp-get-buffer v))
3163 (funcall loc-dec (point-min) (point-max))))
3165 ;; If tramp-decoding-function is not defined for this
3166 ;; method, we invoke tramp-decoding-command instead.
3167 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
3168 ;; Unset `file-name-handler-alist'. Otherwise,
3169 ;; epa-file gets confused.
3170 (let (file-name-handler-alist
3171 (coding-system-for-write 'binary))
3172 (with-current-buffer (tramp-get-buffer v)
3173 (write-region
3174 (point-min) (point-max) tmpfile2 nil 'no-message)))
3175 (unwind-protect
3176 (tramp-call-local-coding-command
3177 loc-dec tmpfile2 tmpfile)
3178 (delete-file tmpfile2)))))
3180 ;; Set proper permissions.
3181 (set-file-modes tmpfile (tramp-default-file-modes filename))
3182 ;; Set local user ownership.
3183 (tramp-set-file-uid-gid tmpfile)))
3185 ;; Oops, I don't know what to do.
3186 (t (tramp-error
3187 v 'file-error "Wrong method specification for `%s'" method)))
3189 ;; Error handling.
3190 ((error quit)
3191 (delete-file tmpfile)
3192 (signal (car err) (cdr err))))
3194 (run-hooks 'tramp-handle-file-local-copy-hook)
3195 tmpfile)))
3197 ;; This is needed for XEmacs only. Code stolen from files.el.
3198 (defun tramp-sh-handle-insert-file-contents-literally
3199 (filename &optional visit beg end replace)
3200 "Like `insert-file-contents-literally' for Tramp files."
3201 (let ((format-alist nil)
3202 (after-insert-file-functions nil)
3203 (coding-system-for-read 'no-conversion)
3204 (coding-system-for-write 'no-conversion)
3205 (find-buffer-file-type-function
3206 (if (fboundp 'find-buffer-file-type)
3207 (symbol-function 'find-buffer-file-type)
3208 nil))
3209 (inhibit-file-name-handlers
3210 '(epa-file-handler image-file-handler jka-compr-handler))
3211 (inhibit-file-name-operation 'insert-file-contents))
3212 (unwind-protect
3213 (progn
3214 (fset 'find-buffer-file-type (lambda (_filename) t))
3215 (insert-file-contents filename visit beg end replace))
3216 ;; Save exit.
3217 (if find-buffer-file-type-function
3218 (fset 'find-buffer-file-type find-buffer-file-type-function)
3219 (fmakunbound 'find-buffer-file-type)))))
3221 ;; CCC grok LOCKNAME
3222 (defun tramp-sh-handle-write-region
3223 (start end filename &optional append visit lockname confirm)
3224 "Like `write-region' for Tramp files."
3225 (setq filename (expand-file-name filename))
3226 (with-parsed-tramp-file-name filename nil
3227 ;; Following part commented out because we don't know what to do about
3228 ;; file locking, and it does not appear to be a problem to ignore it.
3229 ;; Ange-ftp ignores it, too.
3230 ;; (when (and lockname (stringp lockname))
3231 ;; (setq lockname (expand-file-name lockname)))
3232 ;; (unless (or (eq lockname nil)
3233 ;; (string= lockname filename))
3234 ;; (error
3235 ;; "tramp-sh-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3237 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
3238 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
3239 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
3240 (tramp-error v 'file-error "File not overwritten")))
3242 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
3243 (tramp-get-remote-uid v 'integer)))
3244 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
3245 (tramp-get-remote-gid v 'integer))))
3247 (if (and (tramp-local-host-p v)
3248 ;; `file-writable-p' calls `file-expand-file-name'. We
3249 ;; cannot use `tramp-run-real-handler' therefore.
3250 (let (file-name-handler-alist)
3251 (and
3252 (file-writable-p (file-name-directory localname))
3253 (or (file-directory-p localname)
3254 (file-writable-p localname)))))
3255 ;; Short track: if we are on the local host, we can run directly.
3256 (tramp-run-real-handler
3257 'write-region
3258 (list start end localname append 'no-message lockname confirm))
3260 (let* ((modes (save-excursion (tramp-default-file-modes filename)))
3261 ;; We use this to save the value of
3262 ;; `last-coding-system-used' after writing the tmp
3263 ;; file. At the end of the function, we set
3264 ;; `last-coding-system-used' to this saved value. This
3265 ;; way, any intermediary coding systems used while
3266 ;; talking to the remote shell or suchlike won't hose
3267 ;; this variable. This approach was snarfed from
3268 ;; ange-ftp.el.
3269 coding-system-used
3270 ;; Write region into a tmp file. This isn't really
3271 ;; needed if we use an encoding function, but currently
3272 ;; we use it always because this makes the logic
3273 ;; simpler. We must also set `temporary-file-directory',
3274 ;; because it could point to a remote directory.
3275 (temporary-file-directory
3276 (tramp-compat-temporary-file-directory))
3277 (tmpfile (or tramp-temp-buffer-file-name
3278 (tramp-compat-make-temp-file filename))))
3280 ;; If `append' is non-nil, we copy the file locally, and let
3281 ;; the native `write-region' implementation do the job.
3282 (when append (copy-file filename tmpfile 'ok))
3284 ;; We say `no-message' here because we don't want the
3285 ;; visited file modtime data to be clobbered from the temp
3286 ;; file. We call `set-visited-file-modtime' ourselves later
3287 ;; on. We must ensure that `file-coding-system-alist'
3288 ;; matches `tmpfile'.
3289 (let (file-name-handler-alist
3290 (file-coding-system-alist
3291 (tramp-find-file-name-coding-system-alist filename tmpfile)))
3292 (condition-case err
3293 (tramp-run-real-handler
3294 'write-region
3295 (list start end tmpfile append 'no-message lockname confirm))
3296 ((error quit)
3297 (setq tramp-temp-buffer-file-name nil)
3298 (delete-file tmpfile)
3299 (signal (car err) (cdr err))))
3301 ;; Now, `last-coding-system-used' has the right value. Remember it.
3302 (when (boundp 'last-coding-system-used)
3303 (setq coding-system-used
3304 (symbol-value 'last-coding-system-used))))
3306 ;; The permissions of the temporary file should be set. If
3307 ;; FILENAME does not exist (eq modes nil) it has been
3308 ;; renamed to the backup file. This case `save-buffer'
3309 ;; handles permissions.
3310 ;; Ensure that it is still readable.
3311 (when modes
3312 (set-file-modes
3313 tmpfile
3314 (logior (or modes 0) (tramp-compat-octal-to-decimal "0400"))))
3316 ;; This is a bit lengthy due to the different methods
3317 ;; possible for file transfer. First, we check whether the
3318 ;; method uses an scp program. If so, we call it.
3319 ;; Otherwise, both encoding and decoding command must be
3320 ;; specified. However, if the method _also_ specifies an
3321 ;; encoding function, then that is used for encoding the
3322 ;; contents of the tmp file.
3323 (let* ((size (nth 7 (file-attributes tmpfile)))
3324 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
3325 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
3326 (cond
3327 ;; `copy-file' handles direct copy and out-of-band methods.
3328 ((or (tramp-local-host-p v)
3329 (tramp-method-out-of-band-p v size))
3330 (if (and (not (stringp start))
3331 (= (or end (point-max)) (point-max))
3332 (= (or start (point-min)) (point-min))
3333 (tramp-get-method-parameter
3334 method 'tramp-copy-keep-tmpfile))
3335 (progn
3336 (setq tramp-temp-buffer-file-name tmpfile)
3337 (condition-case err
3338 ;; We keep the local file for performance
3339 ;; reasons, useful for "rsync".
3340 (copy-file tmpfile filename t)
3341 ((error quit)
3342 (setq tramp-temp-buffer-file-name nil)
3343 (delete-file tmpfile)
3344 (signal (car err) (cdr err)))))
3345 (setq tramp-temp-buffer-file-name nil)
3346 ;; Don't rename, in order to keep context in SELinux.
3347 (unwind-protect
3348 (copy-file tmpfile filename t)
3349 (delete-file tmpfile))))
3351 ;; Use inline file transfer.
3352 (rem-dec
3353 ;; Encode tmpfile.
3354 (unwind-protect
3355 (with-temp-buffer
3356 (set-buffer-multibyte nil)
3357 ;; Use encoding function or command.
3358 (with-tramp-progress-reporter
3359 v 3 (format "Encoding local file `%s' using `%s'"
3360 tmpfile loc-enc)
3361 (if (functionp loc-enc)
3362 ;; The following `let' is a workaround for
3363 ;; the base64.el that comes with pgnus-0.84.
3364 ;; If both of the following conditions are
3365 ;; satisfied, it tries to write to a local
3366 ;; file in default-directory, but at this
3367 ;; point, default-directory is remote.
3368 ;; (`call-process-region' can't write to
3369 ;; remote files, it seems.) The file in
3370 ;; question is a tmp file anyway.
3371 (let ((coding-system-for-read 'binary)
3372 (default-directory
3373 (tramp-compat-temporary-file-directory)))
3374 (insert-file-contents-literally tmpfile)
3375 (funcall loc-enc (point-min) (point-max)))
3377 (unless (zerop (tramp-call-local-coding-command
3378 loc-enc tmpfile t))
3379 (tramp-error
3380 v 'file-error
3381 (concat "Cannot write to `%s', "
3382 "local encoding command `%s' failed")
3383 filename loc-enc))))
3385 ;; Send buffer into remote decoding command which
3386 ;; writes to remote file. Because this happens on
3387 ;; the remote host, we cannot use the function.
3388 (with-tramp-progress-reporter
3389 v 3 (format "Decoding remote file `%s' using `%s'"
3390 filename rem-dec)
3391 (goto-char (point-max))
3392 (unless (bolp) (newline))
3393 (tramp-send-command
3395 (format
3396 (concat rem-dec " <<'%s'\n%s%s")
3397 (tramp-shell-quote-argument localname)
3398 tramp-end-of-heredoc
3399 (buffer-string)
3400 tramp-end-of-heredoc))
3401 (tramp-barf-unless-okay
3402 v nil
3403 "Couldn't write region to `%s', decode using `%s' failed"
3404 filename rem-dec)
3405 ;; When `file-precious-flag' is set, the region is
3406 ;; written to a temporary file. Check that the
3407 ;; checksum is equal to that from the local tmpfile.
3408 (when file-precious-flag
3409 (erase-buffer)
3410 (and
3411 ;; cksum runs locally, if possible.
3412 (zerop (tramp-call-process v "cksum" tmpfile t))
3413 ;; cksum runs remotely.
3414 (tramp-send-command-and-check
3416 (format
3417 "cksum <%s" (tramp-shell-quote-argument localname)))
3418 ;; ... they are different.
3419 (not
3420 (string-equal
3421 (buffer-string)
3422 (with-current-buffer (tramp-get-buffer v)
3423 (buffer-string))))
3424 (tramp-error
3425 v 'file-error
3426 (concat "Couldn't write region to `%s',"
3427 " decode using `%s' failed")
3428 filename rem-dec)))))
3430 ;; Save exit.
3431 (delete-file tmpfile)))
3433 ;; That's not expected.
3435 (tramp-error
3436 v 'file-error
3437 (concat "Method `%s' should specify both encoding and "
3438 "decoding command or an scp program")
3439 method))))
3441 ;; Make `last-coding-system-used' have the right value.
3442 (when coding-system-used
3443 (set 'last-coding-system-used coding-system-used))))
3445 (tramp-flush-file-property v (file-name-directory localname))
3446 (tramp-flush-file-property v localname)
3448 ;; We must protect `last-coding-system-used', now we have set it
3449 ;; to its correct value.
3450 (let (last-coding-system-used (need-chown t))
3451 ;; Set file modification time.
3452 (when (or (eq visit t) (stringp visit))
3453 (let ((file-attr (tramp-compat-file-attributes filename 'integer)))
3454 (set-visited-file-modtime
3455 ;; We must pass modtime explicitly, because FILENAME can
3456 ;; be different from (buffer-file-name), f.e. if
3457 ;; `file-precious-flag' is set.
3458 (nth 5 file-attr))
3459 (when (and (= (nth 2 file-attr) uid)
3460 (= (nth 3 file-attr) gid))
3461 (setq need-chown nil))))
3463 ;; Set the ownership.
3464 (when need-chown
3465 (tramp-set-file-uid-gid filename uid gid))
3466 (when (or (eq visit t) (null visit) (stringp visit))
3467 (tramp-message v 0 "Wrote %s" filename))
3468 (run-hooks 'tramp-handle-write-region-hook)))))
3470 (defvar tramp-vc-registered-file-names nil
3471 "List used to collect file names, which are checked during `vc-registered'.")
3473 ;; VC backends check for the existence of various different special
3474 ;; files. This is very time consuming, because every single check
3475 ;; requires a remote command (the file cache must be invalidated).
3476 ;; Therefore, we apply a kind of optimization. We install the file
3477 ;; name handler `tramp-vc-file-name-handler', which does nothing but
3478 ;; remembers all file names for which `file-exists-p' or
3479 ;; `file-readable-p' has been applied. A first run of `vc-registered'
3480 ;; is performed. Afterwards, a script is applied for all collected
3481 ;; file names, using just one remote command. The result of this
3482 ;; script is used to fill the file cache with actual values. Now we
3483 ;; can reset the file name handlers, and we make a second run of
3484 ;; `vc-registered', which returns the expected result without sending
3485 ;; any other remote command.
3486 (defun tramp-sh-handle-vc-registered (file)
3487 "Like `vc-registered' for Tramp files."
3488 (tramp-compat-with-temp-message ""
3489 (with-parsed-tramp-file-name file nil
3490 (with-tramp-progress-reporter
3491 v 3 (format "Checking `vc-registered' for %s" file)
3493 ;; There could be new files, created by the vc backend. We
3494 ;; cannot reuse the old cache entries, therefore. In
3495 ;; `tramp-get-file-property', `remote-file-name-inhibit-cache'
3496 ;; could also be a timestamp as `current-time' returns. This
3497 ;; means invalidate all cache entries with an older timestamp.
3498 (let (tramp-vc-registered-file-names
3499 (remote-file-name-inhibit-cache (current-time))
3500 (file-name-handler-alist
3501 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
3503 ;; Here we collect only file names, which need an operation.
3504 (ignore-errors (tramp-run-real-handler 'vc-registered (list file)))
3505 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
3507 ;; Send just one command, in order to fill the cache.
3508 (when tramp-vc-registered-file-names
3509 (tramp-maybe-send-script
3511 (format tramp-vc-registered-read-file-names
3512 (tramp-get-file-exists-command v)
3513 (format "%s -r" (tramp-get-test-command v)))
3514 "tramp_vc_registered_read_file_names")
3516 (dolist
3517 (elt
3518 (ignore-errors
3519 ;; We cannot use `tramp-send-command-and-read',
3520 ;; because this does not cooperate well with
3521 ;; heredoc documents.
3522 (tramp-send-command
3524 (format
3525 "tramp_vc_registered_read_file_names <<'%s'\n%s\n%s\n"
3526 tramp-end-of-heredoc
3527 (mapconcat 'tramp-shell-quote-argument
3528 tramp-vc-registered-file-names
3529 "\n")
3530 tramp-end-of-heredoc))
3531 (with-current-buffer (tramp-get-connection-buffer v)
3532 ;; Read the expression.
3533 (goto-char (point-min))
3534 (read (current-buffer)))))
3536 (tramp-set-file-property
3537 v (car elt) (cadr elt) (cadr (cdr elt))))))
3539 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3540 ;; calls shall be answered from the file cache. We unset
3541 ;; `process-file-side-effects' and `remote-file-name-inhibit-cache'
3542 ;; in order to keep the cache.
3543 (let ((vc-handled-backends vc-handled-backends)
3544 remote-file-name-inhibit-cache process-file-side-effects)
3545 ;; Reduce `vc-handled-backends' in order to minimize process calls.
3546 (when (and (memq 'Bzr vc-handled-backends)
3547 (boundp 'vc-bzr-program)
3548 (not (with-tramp-connection-property v vc-bzr-program
3549 (tramp-find-executable
3550 v vc-bzr-program (tramp-get-remote-path v)))))
3551 (setq vc-handled-backends (remq 'Bzr vc-handled-backends)))
3552 (when (and (memq 'Git vc-handled-backends)
3553 (boundp 'vc-git-program)
3554 (not (with-tramp-connection-property v vc-git-program
3555 (tramp-find-executable
3556 v vc-git-program (tramp-get-remote-path v)))))
3557 (setq vc-handled-backends (remq 'Git vc-handled-backends)))
3558 (when (and (memq 'Hg vc-handled-backends)
3559 (boundp 'vc-hg-program)
3560 (not (with-tramp-connection-property v vc-hg-program
3561 (tramp-find-executable
3562 v vc-hg-program (tramp-get-remote-path v)))))
3563 (setq vc-handled-backends (remq 'Hg vc-handled-backends)))
3564 ;; Run.
3565 (ignore-errors
3566 (tramp-run-real-handler 'vc-registered (list file))))))))
3568 ;;;###tramp-autoload
3569 (defun tramp-sh-file-name-handler (operation &rest args)
3570 "Invoke remote-shell Tramp file name handler.
3571 Fall back to normal file name handler if no Tramp handler exists."
3572 (when (and tramp-locked (not tramp-locker))
3573 (setq tramp-locked nil)
3574 (tramp-error
3575 (car-safe tramp-current-connection) 'file-error
3576 "Forbidden reentrant call of Tramp"))
3577 (let ((tl tramp-locked))
3578 (setq tramp-locked t)
3579 (unwind-protect
3580 (let ((tramp-locker t))
3581 (save-match-data
3582 (let ((fn (assoc operation tramp-sh-file-name-handler-alist)))
3583 (if fn
3584 (apply (cdr fn) args)
3585 (tramp-run-real-handler operation args)))))
3586 (setq tramp-locked tl))))
3588 (defun tramp-vc-file-name-handler (operation &rest args)
3589 "Invoke special file name handler, which collects files to be handled."
3590 (save-match-data
3591 (let ((filename
3592 (tramp-replace-environment-variables
3593 (apply 'tramp-file-name-for-operation operation args)))
3594 (fn (assoc operation tramp-sh-file-name-handler-alist)))
3595 (with-parsed-tramp-file-name filename nil
3596 (cond
3597 ;; That's what we want: file names, for which checks are
3598 ;; applied. We assume that VC uses only `file-exists-p' and
3599 ;; `file-readable-p' checks; otherwise we must extend the
3600 ;; list. We do not perform any action, but return nil, in
3601 ;; order to keep `vc-registered' running.
3602 ((and fn (memq operation '(file-exists-p file-readable-p)))
3603 (add-to-list 'tramp-vc-registered-file-names localname 'append)
3604 nil)
3605 ;; `process-file' and `start-file-process' shall be ignored.
3606 ((and fn (eq operation 'process-file) 0))
3607 ((and fn (eq operation 'start-file-process) nil))
3608 ;; Tramp file name handlers like `expand-file-name'. They
3609 ;; must still work.
3610 (fn (save-match-data (apply (cdr fn) args)))
3611 ;; Default file name handlers, we don't care.
3612 (t (tramp-run-real-handler operation args)))))))
3614 (defun tramp-sh-handle-file-notify-add-watch (file-name flags _callback)
3615 "Like `file-notify-add-watch' for Tramp files."
3616 (setq file-name (expand-file-name file-name))
3617 (with-parsed-tramp-file-name file-name nil
3618 (let* ((default-directory (file-name-directory file-name))
3619 command events filter p sequence)
3620 (cond
3621 ;; gvfs-monitor-dir.
3622 ((setq command (tramp-get-remote-gvfs-monitor-dir v))
3623 (setq filter 'tramp-sh-file-gvfs-monitor-dir-process-filter
3624 sequence `(,command ,localname)))
3625 ;; inotifywait.
3626 ((setq command (tramp-get-remote-inotifywait v))
3627 (setq filter 'tramp-sh-file-inotifywait-process-filter
3628 events
3629 (cond
3630 ((and (memq 'change flags) (memq 'attribute-change flags))
3631 "create,modify,move,delete,attrib")
3632 ((memq 'change flags) "create,modify,move,delete")
3633 ((memq 'attribute-change flags) "attrib"))
3634 sequence `(,command "-mq" "-e" ,events ,localname)))
3635 ;; None.
3636 (t (tramp-error
3637 v 'file-notify-error
3638 "No file notification program found on %s"
3639 (file-remote-p file-name))))
3640 ;; Start process.
3641 (setq p (apply
3642 'start-file-process
3643 (file-name-nondirectory command)
3644 (generate-new-buffer
3645 (format " *%s*" (file-name-nondirectory command)))
3646 sequence))
3647 ;; Return the process object as watch-descriptor.
3648 (if (not (processp p))
3649 (tramp-error
3650 v 'file-notify-error
3651 "`%s' failed to start on remote host"
3652 (mapconcat 'identity sequence " "))
3653 (tramp-message v 6 "Run `%s', %S" (mapconcat 'identity sequence " ") p)
3654 (tramp-set-connection-property p "vector" v)
3655 (tramp-compat-set-process-query-on-exit-flag p nil)
3656 (set-process-filter p filter)
3657 p))))
3659 (defun tramp-sh-file-gvfs-monitor-dir-process-filter (proc string)
3660 "Read output from \"gvfs-monitor-dir\" and add corresponding file-notify events."
3661 (let ((remote-prefix
3662 (with-current-buffer (process-buffer proc)
3663 (file-remote-p default-directory)))
3664 (rest-string (tramp-compat-process-get proc 'rest-string)))
3665 (when rest-string
3666 (tramp-message proc 10 "Previous string:\n%s" rest-string))
3667 (tramp-message proc 6 "%S\n%s" proc string)
3668 (setq string (concat rest-string string)
3669 ;; Attribute change is returned in unused wording.
3670 string (tramp-compat-replace-regexp-in-string
3671 "ATTRIB CHANGED" "ATTRIBUTE_CHANGED" string))
3673 (while (string-match
3674 (concat "^[\n\r]*"
3675 "Directory Monitor Event:[\n\r]+"
3676 "Child = \\([^\n\r]+\\)[\n\r]+"
3677 "\\(Other = \\([^\n\r]+\\)[\n\r]+\\)?"
3678 "Event = \\([^[:blank:]]+\\)[\n\r]+")
3679 string)
3680 (let ((object
3681 (list
3682 proc
3683 (intern-soft
3684 (tramp-compat-replace-regexp-in-string
3685 "_" "-" (downcase (match-string 4 string))))
3686 ;; File names are returned as absolute paths. We must
3687 ;; add the remote prefix.
3688 (concat remote-prefix (match-string 1 string))
3689 (when (match-string 3 string)
3690 (concat remote-prefix (match-string 3 string))))))
3691 (setq string (replace-match "" nil nil string))
3692 ;; Usually, we would add an Emacs event now. Unfortunately,
3693 ;; `unread-command-events' does not accept several events at
3694 ;; once. Therefore, we apply the callback directly.
3695 (tramp-compat-funcall 'file-notify-callback object)))
3697 ;; Save rest of the string.
3698 (when (zerop (length string)) (setq string nil))
3699 (when string (tramp-message proc 10 "Rest string:\n%s" string))
3700 (tramp-compat-process-put proc 'rest-string string)))
3702 (defun tramp-sh-file-inotifywait-process-filter (proc string)
3703 "Read output from \"inotifywait\" and add corresponding file-notify events."
3704 (tramp-message proc 6 "%S\n%s" proc string)
3705 (dolist (line (split-string string "[\n\r]+" 'omit-nulls))
3706 ;; Check, whether there is a problem.
3707 (unless
3708 (string-match
3709 (concat "^[^[:blank:]]+"
3710 "[[:blank:]]+\\([^[:blank:]]+\\)+"
3711 "\\([[:blank:]]+\\([^\n\r]+\\)\\)?")
3712 line)
3713 (tramp-error proc 'file-notify-error "%s" line))
3715 (let ((object
3716 (list
3717 proc
3718 (mapcar
3719 (lambda (x)
3720 (intern-soft
3721 (tramp-compat-replace-regexp-in-string "_" "-" (downcase x))))
3722 (split-string (match-string 1 line) "," 'omit-nulls))
3723 (match-string 3 line))))
3724 ;; Usually, we would add an Emacs event now. Unfortunately,
3725 ;; `unread-command-events' does not accept several events at
3726 ;; once. Therefore, we apply the callback directly.
3727 (tramp-compat-funcall 'file-notify-callback object))))
3729 ;;; Internal Functions:
3731 (defun tramp-maybe-send-script (vec script name)
3732 "Define in remote shell function NAME implemented as SCRIPT.
3733 Only send the definition if it has not already been done."
3734 ;; We cannot let-bind (tramp-get-connection-process vec) because it
3735 ;; might be nil.
3736 (let ((scripts (tramp-get-connection-property
3737 (tramp-get-connection-process vec) "scripts" nil)))
3738 (unless (member name scripts)
3739 (with-tramp-progress-reporter vec 5 (format "Sending script `%s'" name)
3740 ;; In bash, leading TABs like in `tramp-vc-registered-read-file-names'
3741 ;; could result in unwanted command expansion. Avoid this.
3742 (setq script (tramp-compat-replace-regexp-in-string
3743 (make-string 1 ?\t) (make-string 8 ? ) script))
3744 ;; The script could contain a call of Perl. This is masked with `%s'.
3745 (when (and (string-match "%s" script)
3746 (not (tramp-get-remote-perl vec)))
3747 (tramp-error vec 'file-error "No Perl available on remote host"))
3748 (tramp-barf-unless-okay
3750 (format "%s () {\n%s\n}"
3751 name (format script (tramp-get-remote-perl vec)))
3752 "Script %s sending failed" name)
3753 (tramp-set-connection-property
3754 (tramp-get-connection-process vec) "scripts" (cons name scripts))))))
3756 (defun tramp-set-auto-save ()
3757 (when (and ;; ange-ftp has its own auto-save mechanism
3758 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
3759 'tramp-sh-file-name-handler)
3760 auto-save-default)
3761 (auto-save-mode 1)))
3762 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
3763 (add-hook 'tramp-unload-hook
3764 (lambda ()
3765 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
3767 (defun tramp-run-test (switch filename)
3768 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3769 Returns the exit code of the `test' program."
3770 (with-parsed-tramp-file-name filename nil
3771 (tramp-send-command-and-check
3773 (format
3774 "%s %s %s"
3775 (tramp-get-test-command v)
3776 switch
3777 (tramp-shell-quote-argument localname)))))
3779 (defun tramp-run-test2 (format-string file1 file2)
3780 "Run `test'-like program on the remote system, given FILE1, FILE2.
3781 FORMAT-STRING contains the program name, switches, and place holders.
3782 Returns the exit code of the `test' program. Barfs if the methods,
3783 hosts, or files, disagree."
3784 (unless (tramp-equal-remote file1 file2)
3785 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
3786 (tramp-error
3787 v 'file-error
3788 "tramp-run-test2 only implemented for same method, user, host")))
3789 (with-parsed-tramp-file-name file1 v1
3790 (with-parsed-tramp-file-name file1 v2
3791 (tramp-send-command-and-check
3793 (format format-string
3794 (tramp-shell-quote-argument v1-localname)
3795 (tramp-shell-quote-argument v2-localname))))))
3797 (defun tramp-find-executable
3798 (vec progname dirlist &optional ignore-tilde ignore-path)
3799 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3800 First arg VEC specifies the connection, PROGNAME is the program
3801 to search for, and DIRLIST gives the list of directories to
3802 search. If IGNORE-TILDE is non-nil, directory names starting
3803 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3804 only in DIRLIST.
3806 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3808 This function expects to be in the right *tramp* buffer."
3809 (with-current-buffer (tramp-get-connection-buffer vec)
3810 (let (result)
3811 ;; Check whether the executable is in $PATH. "which(1)" does not
3812 ;; report always a correct error code; therefore we check the
3813 ;; number of words it returns. "SunOS 5.10" (and maybe "SunOS
3814 ;; 5.11") have problems with this command, we disable the call
3815 ;; therefore.
3816 (unless (or ignore-path
3817 (string-match
3818 (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3819 (tramp-get-connection-property vec "uname" "")))
3820 (tramp-send-command vec (format "which \\%s | wc -w" progname))
3821 (goto-char (point-min))
3822 (if (looking-at "^\\s-*1$")
3823 (setq result (concat "\\" progname))))
3824 (unless result
3825 (when ignore-tilde
3826 ;; Remove all ~/foo directories from dirlist. In XEmacs,
3827 ;; `remove' is in CL, and we want to avoid CL dependencies.
3828 (let (newdl d)
3829 (while dirlist
3830 (setq d (car dirlist))
3831 (setq dirlist (cdr dirlist))
3832 (unless (char-equal ?~ (aref d 0))
3833 (setq newdl (cons d newdl))))
3834 (setq dirlist (nreverse newdl))))
3835 (tramp-send-command
3837 (format (concat "while read d; "
3838 "do if test -x $d/%s && test -f $d/%s; "
3839 "then echo tramp_executable $d/%s; "
3840 "break; fi; done <<'%s'\n"
3841 "%s\n%s")
3842 progname progname progname
3843 tramp-end-of-heredoc
3844 (mapconcat 'identity dirlist "\n")
3845 tramp-end-of-heredoc))
3846 (goto-char (point-max))
3847 (when (search-backward "tramp_executable " nil t)
3848 (skip-chars-forward "^ ")
3849 (skip-chars-forward " ")
3850 (setq result (buffer-substring (point) (point-at-eol)))))
3851 result)))
3853 (defun tramp-set-remote-path (vec)
3854 "Sets the remote environment PATH to existing directories.
3855 I.e., for each directory in `tramp-remote-path', it is tested
3856 whether it exists and if so, it is added to the environment
3857 variable PATH."
3858 (tramp-message vec 5 "Setting $PATH environment variable")
3859 (tramp-send-command
3860 vec (format "PATH=%s; export PATH"
3861 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
3863 ;; ------------------------------------------------------------
3864 ;; -- Communication with external shell --
3865 ;; ------------------------------------------------------------
3867 (defun tramp-find-file-exists-command (vec)
3868 "Find a command on the remote host for checking if a file exists.
3869 Here, we are looking for a command which has zero exit status if the
3870 file exists and nonzero exit status otherwise."
3871 (let ((existing "/")
3872 (nonexistent
3873 (tramp-shell-quote-argument "/ this file does not exist "))
3874 result)
3875 ;; The algorithm is as follows: we try a list of several commands.
3876 ;; For each command, we first run `$cmd /' -- this should return
3877 ;; true, as the root directory always exists. And then we run
3878 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
3879 ;; does not exist. This should return false. We use the first
3880 ;; command we find that seems to work.
3881 ;; The list of commands to try is as follows:
3882 ;; `ls -d' This works on most systems, but NetBSD 1.4
3883 ;; has a bug: `ls' always returns zero exit
3884 ;; status, even for files which don't exist.
3885 ;; `test -e' Some Bourne shells have a `test' builtin
3886 ;; which does not know the `-e' option.
3887 ;; `/bin/test -e' For those, the `test' binary on disk normally
3888 ;; provides the option. Alas, the binary
3889 ;; is sometimes `/bin/test' and sometimes it's
3890 ;; `/usr/bin/test'.
3891 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3892 (unless (or
3893 (ignore-errors
3894 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
3895 (tramp-send-command-and-check
3896 vec (format "%s %s" result existing))
3897 (not (tramp-send-command-and-check
3898 vec (format "%s %s" result nonexistent)))))
3899 (ignore-errors
3900 (and (setq result "/bin/test -e")
3901 (tramp-send-command-and-check
3902 vec (format "%s %s" result existing))
3903 (not (tramp-send-command-and-check
3904 vec (format "%s %s" result nonexistent)))))
3905 (ignore-errors
3906 (and (setq result "/usr/bin/test -e")
3907 (tramp-send-command-and-check
3908 vec (format "%s %s" result existing))
3909 (not (tramp-send-command-and-check
3910 vec (format "%s %s" result nonexistent)))))
3911 (ignore-errors
3912 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
3913 (tramp-send-command-and-check
3914 vec (format "%s %s" result existing))
3915 (not (tramp-send-command-and-check
3916 vec (format "%s %s" result nonexistent))))))
3917 (tramp-error
3918 vec 'file-error "Couldn't find command to check if file exists"))
3919 result))
3921 (defun tramp-open-shell (vec shell)
3922 "Opens shell SHELL."
3923 (with-tramp-progress-reporter
3924 vec 5 (format "Opening remote shell `%s'" shell)
3925 ;; Find arguments for this shell.
3926 (let ((alist tramp-sh-extra-args)
3927 item extra-args)
3928 (while (and alist (null extra-args))
3929 (setq item (pop alist))
3930 (when (string-match (car item) shell)
3931 (setq extra-args (cdr item))))
3932 ;; It is useful to set the prompt in the following command
3933 ;; because some people have a setting for $PS1 which /bin/sh
3934 ;; doesn't know about and thus /bin/sh will display a strange
3935 ;; prompt. For example, if $PS1 has "${CWD}" in the value, then
3936 ;; ksh will display the current working directory but /bin/sh
3937 ;; will display a dollar sign. The following command line sets
3938 ;; $PS1 to a sane value, and works under Bourne-ish shells as
3939 ;; well as csh-like shells. We also unset the variable $ENV
3940 ;; because that is read by some sh implementations (eg, bash
3941 ;; when called as sh) on startup; this way, we avoid the startup
3942 ;; file clobbering $PS1. $PROMPT_COMMAND is another way to set
3943 ;; the prompt in /bin/bash, it must be discarded as well.
3944 ;; $HISTFILE is set according to `tramp-histfile-override'.
3945 (tramp-send-command
3946 vec (format
3947 "exec env ENV='' %s PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s"
3948 (if (stringp tramp-histfile-override)
3949 (format "HISTFILE=%s"
3950 (tramp-shell-quote-argument tramp-histfile-override))
3951 (if tramp-histfile-override
3952 "HISTFILE='' HISTFILESIZE=0 HISTSIZE=0"
3953 ""))
3954 (tramp-shell-quote-argument tramp-end-of-output)
3955 shell (or extra-args ""))
3957 (tramp-set-connection-property
3958 (tramp-get-connection-process vec) "remote-shell" shell)))
3960 (defun tramp-find-shell (vec)
3961 "Opens a shell on the remote host which groks tilde expansion."
3962 (with-current-buffer (tramp-get-buffer vec)
3963 (let ((default-shell
3965 (tramp-get-connection-property
3966 (tramp-get-connection-process vec) "remote-shell" nil)
3967 (tramp-get-method-parameter
3968 (tramp-file-name-method vec) 'tramp-remote-shell)))
3969 shell)
3970 (setq shell
3971 (with-tramp-connection-property vec "remote-shell"
3972 ;; CCC: "root" does not exist always, see QNAP 459.
3973 ;; Which check could we apply instead?
3974 (tramp-send-command vec "echo ~root" t)
3975 (if (or (string-match "^~root$" (buffer-string))
3976 ;; The default shell (ksh93) of OpenSolaris and
3977 ;; Solaris is buggy. We've got reports for
3978 ;; "SunOS 5.10" and "SunOS 5.11" so far.
3979 (string-match (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3980 (tramp-get-connection-property
3981 vec "uname" "")))
3983 (or (tramp-find-executable
3984 vec "bash" (tramp-get-remote-path vec) t t)
3985 (tramp-find-executable
3986 vec "ksh" (tramp-get-remote-path vec) t t)
3987 ;; Maybe it works at least for some other commands.
3988 (prog1
3989 default-shell
3990 (tramp-message
3991 vec 2
3992 (concat
3993 "Couldn't find a remote shell which groks tilde "
3994 "expansion, using `%s'")
3995 default-shell)))
3997 default-shell)))
3999 ;; Open a new shell if needed.
4000 (unless (string-equal shell default-shell)
4001 (tramp-message
4002 vec 5 "Starting remote shell `%s' for tilde expansion" shell)
4003 (tramp-open-shell vec shell)))))
4005 ;; Utility functions.
4007 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
4008 "Wait for shell prompt and barf if none appears.
4009 Looks at process PROC to see if a shell prompt appears in TIMEOUT
4010 seconds. If not, it produces an error message with the given ERROR-ARGS."
4011 (let ((vec (tramp-get-connection-property proc "vector" nil)))
4012 (condition-case nil
4013 (tramp-wait-for-regexp
4014 proc timeout
4015 (format
4016 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
4017 (error
4018 (delete-process proc)
4019 (apply 'tramp-error-with-buffer
4020 (tramp-get-connection-buffer vec) vec 'file-error error-args)))))
4022 (defun tramp-open-connection-setup-interactive-shell (proc vec)
4023 "Set up an interactive shell.
4024 Mainly sets the prompt and the echo correctly. PROC is the shell
4025 process to set up. VEC specifies the connection."
4026 (let ((tramp-end-of-output tramp-initial-end-of-output))
4027 (tramp-open-shell
4029 (or (tramp-get-connection-property vec "remote-shell" nil)
4030 (tramp-get-method-parameter
4031 (tramp-file-name-method vec) 'tramp-remote-shell)))
4033 ;; Disable echo.
4034 (tramp-message vec 5 "Setting up remote shell environment")
4035 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
4036 ;; Check whether the echo has really been disabled. Some
4037 ;; implementations, like busybox of embedded GNU/Linux, don't
4038 ;; support disabling.
4039 (tramp-send-command vec "echo foo" t)
4040 (with-current-buffer (process-buffer proc)
4041 (goto-char (point-min))
4042 (when (looking-at "echo foo")
4043 (tramp-set-connection-property proc "remote-echo" t)
4044 (tramp-message vec 5 "Remote echo still on. Ok.")
4045 ;; Make sure backspaces and their echo are enabled and no line
4046 ;; width magic interferes with them.
4047 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
4049 (tramp-message vec 5 "Setting shell prompt")
4050 (tramp-send-command
4051 vec (format "PS1=%s PS2='' PS3='' PROMPT_COMMAND=''"
4052 (tramp-shell-quote-argument tramp-end-of-output)) t)
4054 ;; Try to set up the coding system correctly.
4055 ;; CCC this can't be the right way to do it. Hm.
4056 (tramp-message vec 5 "Determining coding system")
4057 (with-current-buffer (process-buffer proc)
4058 (if (featurep 'mule)
4059 ;; Use MULE to select the right EOL convention for communicating
4060 ;; with the process.
4061 (let ((cs (or (and (memq 'utf-8 (coding-system-list))
4062 (string-match "utf8" (tramp-get-remote-locale vec))
4063 (cons 'utf-8 'utf-8))
4064 (tramp-compat-funcall 'process-coding-system proc)
4065 (cons 'undecided 'undecided)))
4066 cs-decode cs-encode)
4067 (when (symbolp cs) (setq cs (cons cs cs)))
4068 (setq cs-decode (car cs))
4069 (setq cs-encode (cdr cs))
4070 (unless cs-decode (setq cs-decode 'undecided))
4071 (unless cs-encode (setq cs-encode 'undecided))
4072 (setq cs-encode (tramp-compat-coding-system-change-eol-conversion
4073 cs-encode 'unix))
4074 (tramp-send-command vec "echo foo ; echo bar" t)
4075 (goto-char (point-min))
4076 (when (search-forward "\r" nil t)
4077 (setq cs-decode (tramp-compat-coding-system-change-eol-conversion
4078 cs-decode 'dos)))
4079 (tramp-compat-funcall
4080 'set-buffer-process-coding-system cs-decode cs-encode)
4081 (tramp-message
4082 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
4083 ;; Look for ^M and do something useful if found.
4084 (when (search-forward "\r" nil t)
4085 ;; We have found a ^M but cannot frob the process coding system
4086 ;; because we're running on a non-MULE Emacs. Let's try
4087 ;; stty, instead.
4088 (tramp-send-command vec "stty -onlcr" t))))
4090 (tramp-send-command vec "set +o vi +o emacs" t)
4092 ;; Check whether the output of "uname -sr" has been changed. If
4093 ;; yes, this is a strong indication that we must expire all
4094 ;; connection properties. We start again with
4095 ;; `tramp-maybe-open-connection', it will be caught there.
4096 (tramp-message vec 5 "Checking system information")
4097 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
4098 (new-uname
4099 (tramp-set-connection-property
4100 vec "uname"
4101 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
4102 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
4103 (tramp-message
4104 vec 3
4105 "Connection reset, because remote host changed from `%s' to `%s'"
4106 old-uname new-uname)
4107 ;; We want to keep the password.
4108 (tramp-cleanup-connection vec t t)
4109 (throw 'uname-changed (tramp-maybe-open-connection vec))))
4111 ;; Check whether the remote host suffers from buggy
4112 ;; `send-process-string'. This is known for FreeBSD (see comment in
4113 ;; `send_process', file process.c). I've tested sending 624 bytes
4114 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
4115 ;; this host type is detected locally. It cannot handle remote
4116 ;; hosts, though.
4117 (with-tramp-connection-property proc "chunksize"
4118 (cond
4119 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
4120 tramp-chunksize)
4122 (tramp-message
4123 vec 5 "Checking remote host type for `send-process-string' bug")
4124 (if (string-match
4125 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
4126 500 0))))
4128 ;; Set remote PATH variable.
4129 (tramp-set-remote-path vec)
4131 ;; Search for a good shell before searching for a command which
4132 ;; checks if a file exists. This is done because Tramp wants to use
4133 ;; "test foo; echo $?" to check if various conditions hold, and
4134 ;; there are buggy /bin/sh implementations which don't execute the
4135 ;; "echo $?" part if the "test" part has an error. In particular,
4136 ;; the OpenSolaris /bin/sh is a problem. There are also other
4137 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
4138 ;; in function declarations, or changing HISTFILE in place.
4139 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
4140 ;; detected.
4141 (tramp-find-shell vec)
4143 ;; Disable unexpected output.
4144 (tramp-send-command vec "mesg n; biff n" t)
4146 ;; IRIX64 bash expands "!" even when in single quotes. This
4147 ;; destroys our shell functions, we must disable it. See
4148 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
4149 (when (string-match "^IRIX64" (tramp-get-connection-property vec "uname" ""))
4150 (tramp-send-command vec "set +H" t))
4152 ;; On BSD-like systems, ?\t is expanded to spaces. Suppress this.
4153 (when (string-match "BSD\\|Darwin"
4154 (tramp-get-connection-property vec "uname" ""))
4155 (tramp-send-command vec "stty -oxtabs" t))
4157 ;; Set `remote-tty' process property.
4158 (let ((tty (tramp-send-command-and-read vec "echo \\\"`tty`\\\"" 'noerror)))
4159 (unless (zerop (length tty))
4160 (tramp-compat-process-put proc 'remote-tty tty)))
4162 ;; Dump stty settings in the traces.
4163 (when (>= tramp-verbose 9)
4164 (tramp-send-command vec "stty -a" t))
4166 ;; Set the environment.
4167 (tramp-message vec 5 "Setting default environment")
4169 (let ((env (append `(,(tramp-get-remote-locale vec))
4170 (copy-sequence tramp-remote-process-environment)))
4171 unset vars item)
4172 (while env
4173 (setq item (tramp-compat-split-string (car env) "="))
4174 (setcdr item (mapconcat 'identity (cdr item) "="))
4175 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
4176 (push (format "%s %s" (car item) (cdr item)) vars)
4177 (push (car item) unset))
4178 (setq env (cdr env)))
4179 (when vars
4180 (tramp-send-command
4182 (format "while read var val; do export $var=$val; done <<'%s'\n%s\n%s"
4183 tramp-end-of-heredoc
4184 (mapconcat 'identity vars "\n")
4185 tramp-end-of-heredoc)
4187 (when unset
4188 (tramp-send-command
4189 vec (format "unset %s" (mapconcat 'identity unset " ")) t))))
4191 ;; Old text from documentation of tramp-methods:
4192 ;; Using a uuencode/uudecode inline method is discouraged, please use one
4193 ;; of the base64 methods instead since base64 encoding is much more
4194 ;; reliable and the commands are more standardized between the different
4195 ;; Unix versions. But if you can't use base64 for some reason, please
4196 ;; note that the default uudecode command does not work well for some
4197 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
4198 ;; the following command for uudecode:
4200 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
4202 ;; For Irix, no solution is known yet.
4204 (autoload 'uudecode-decode-region "uudecode")
4206 (defconst tramp-local-coding-commands
4207 `((b64 base64-encode-region base64-decode-region)
4208 (uu tramp-uuencode-region uudecode-decode-region)
4209 (pack ,(format tramp-perl-pack "perl") ,(format tramp-perl-unpack "perl")))
4210 "List of local coding commands for inline transfer.
4211 Each item is a list that looks like this:
4213 \(FORMAT ENCODING DECODING\)
4215 FORMAT is symbol describing the encoding/decoding format. It can be
4216 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
4218 ENCODING and DECODING can be strings, giving commands, or symbols,
4219 giving functions. If they are strings, then they can contain
4220 the \"%s\" format specifier. If that specifier is present, the input
4221 file name will be put into the command line at that spot. If the
4222 specifier is not present, the input should be read from standard
4223 input.
4225 If they are functions, they will be called with two arguments, start
4226 and end of region, and are expected to replace the region contents
4227 with the encoded or decoded results, respectively.")
4229 (defconst tramp-remote-coding-commands
4230 '((b64 "base64" "base64 -d -i")
4231 ;; "-i" is more robust with older base64 from GNU coreutils.
4232 ;; However, I don't know whether all base64 versions do supports
4233 ;; this option.
4234 (b64 "base64" "base64 -d")
4235 (b64 "mimencode -b" "mimencode -u -b")
4236 (b64 "mmencode -b" "mmencode -u -b")
4237 (b64 "recode data..base64" "recode base64..data")
4238 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
4239 (b64 tramp-perl-encode tramp-perl-decode)
4240 (uu "uuencode xxx" "uudecode -o /dev/stdout" "test -c /dev/stdout")
4241 (uu "uuencode xxx" "uudecode -o -")
4242 (uu "uuencode xxx" "uudecode -p")
4243 (uu "uuencode xxx" tramp-uudecode)
4244 (pack tramp-perl-pack tramp-perl-unpack))
4245 "List of remote coding commands for inline transfer.
4246 Each item is a list that looks like this:
4248 \(FORMAT ENCODING DECODING [TEST]\)
4250 FORMAT is a symbol describing the encoding/decoding format. It can be
4251 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
4253 ENCODING and DECODING can be strings, giving commands, or symbols,
4254 giving variables. If they are strings, then they can contain
4255 the \"%s\" format specifier. If that specifier is present, the input
4256 file name will be put into the command line at that spot. If the
4257 specifier is not present, the input should be read from standard
4258 input.
4260 If they are variables, this variable is a string containing a
4261 Perl or Shell implementation for this functionality. This
4262 program will be transferred to the remote host, and it is
4263 available as shell function with the same name. A \"%t\" format
4264 specifier in the variable value denotes a temporary file.
4266 The optional TEST command can be used for further tests, whether
4267 ENCODING and DECODING are applicable.")
4269 (defun tramp-find-inline-encoding (vec)
4270 "Find an inline transfer encoding that works.
4271 Goes through the list `tramp-local-coding-commands' and
4272 `tramp-remote-coding-commands'."
4273 (save-excursion
4274 (let ((local-commands tramp-local-coding-commands)
4275 (magic "xyzzy")
4276 (p (tramp-get-connection-process vec))
4277 loc-enc loc-dec rem-enc rem-dec rem-test litem ritem found)
4278 (while (and local-commands (not found))
4279 (setq litem (pop local-commands))
4280 (catch 'wont-work-local
4281 (let ((format (nth 0 litem))
4282 (remote-commands tramp-remote-coding-commands))
4283 (setq loc-enc (nth 1 litem))
4284 (setq loc-dec (nth 2 litem))
4285 ;; If the local encoder or decoder is a string, the
4286 ;; corresponding command has to work locally.
4287 (if (not (stringp loc-enc))
4288 (tramp-message
4289 vec 5 "Checking local encoding function `%s'" loc-enc)
4290 (tramp-message
4291 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
4292 (unless (zerop (tramp-call-local-coding-command
4293 loc-enc nil nil))
4294 (throw 'wont-work-local nil)))
4295 (if (not (stringp loc-dec))
4296 (tramp-message
4297 vec 5 "Checking local decoding function `%s'" loc-dec)
4298 (tramp-message
4299 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
4300 (unless (zerop (tramp-call-local-coding-command
4301 loc-dec nil nil))
4302 (throw 'wont-work-local nil)))
4303 ;; Search for remote coding commands with the same format
4304 (while (and remote-commands (not found))
4305 (setq ritem (pop remote-commands))
4306 (catch 'wont-work-remote
4307 (when (equal format (nth 0 ritem))
4308 (setq rem-enc (nth 1 ritem))
4309 (setq rem-dec (nth 2 ritem))
4310 (setq rem-test (nth 3 ritem))
4311 ;; Check the remote test command if exists.
4312 (when (stringp rem-test)
4313 (tramp-message
4314 vec 5 "Checking remote test command `%s'" rem-test)
4315 (unless (tramp-send-command-and-check vec rem-test t)
4316 (throw 'wont-work-remote nil)))
4317 ;; Check if remote perl exists when necessary.
4318 (when (and (not (stringp rem-enc))
4319 (not (tramp-get-remote-perl vec)))
4320 (throw 'wont-work-remote nil))
4321 ;; Check if remote encoding and decoding commands can be
4322 ;; called remotely with null input and output. This makes
4323 ;; sure there are no syntax errors and the command is really
4324 ;; found. Note that we do not redirect stdout to /dev/null,
4325 ;; for two reasons: when checking the decoding command, we
4326 ;; actually check the output it gives. And also, when
4327 ;; redirecting "mimencode" output to /dev/null, then as root
4328 ;; it might change the permissions of /dev/null!
4329 (when (not (stringp rem-enc))
4330 (let ((name (symbol-name rem-enc)))
4331 (while (string-match (regexp-quote "-") name)
4332 (setq name (replace-match "_" nil t name)))
4333 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
4334 (setq rem-enc name)))
4335 (tramp-message
4336 vec 5
4337 "Checking remote encoding command `%s' for sanity" rem-enc)
4338 (unless (tramp-send-command-and-check
4339 vec (format "%s </dev/null" rem-enc) t)
4340 (throw 'wont-work-remote nil))
4342 (when (not (stringp rem-dec))
4343 (let ((name (symbol-name rem-dec))
4344 (value (symbol-value rem-dec))
4345 tmpfile)
4346 (while (string-match (regexp-quote "-") name)
4347 (setq name (replace-match "_" nil t name)))
4348 (when (string-match "\\(^\\|[^%]\\)%t" value)
4349 (setq tmpfile
4350 (make-temp-name
4351 (expand-file-name
4352 tramp-temp-name-prefix
4353 (tramp-get-remote-tmpdir vec)))
4354 value
4355 (format-spec
4356 value
4357 (format-spec-make
4359 (tramp-file-name-handler
4360 'file-remote-p tmpfile 'localname)))))
4361 (tramp-maybe-send-script vec value name)
4362 (setq rem-dec name)))
4363 (tramp-message
4364 vec 5
4365 "Checking remote decoding command `%s' for sanity" rem-dec)
4366 (unless (tramp-send-command-and-check
4368 (format "echo %s | %s | %s" magic rem-enc rem-dec)
4370 (throw 'wont-work-remote nil))
4372 (with-current-buffer (tramp-get-buffer vec)
4373 (goto-char (point-min))
4374 (unless (looking-at (regexp-quote magic))
4375 (throw 'wont-work-remote nil)))
4377 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
4378 (setq rem-enc (nth 1 ritem))
4379 (setq rem-dec (nth 2 ritem))
4380 (setq found t)))))))
4382 (when found
4383 ;; Set connection properties. Since the commands are risky
4384 ;; (due to output direction), we cache them in the process cache.
4385 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
4386 (tramp-set-connection-property p "local-encoding" loc-enc)
4387 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
4388 (tramp-set-connection-property p "local-decoding" loc-dec)
4389 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
4390 (tramp-set-connection-property p "remote-encoding" rem-enc)
4391 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
4392 (tramp-set-connection-property p "remote-decoding" rem-dec)))))
4394 (defun tramp-call-local-coding-command (cmd input output)
4395 "Call the local encoding or decoding command.
4396 If CMD contains \"%s\", provide input file INPUT there in command.
4397 Otherwise, INPUT is passed via standard input.
4398 INPUT can also be nil which means `/dev/null'.
4399 OUTPUT can be a string (which specifies a file name), or t (which
4400 means standard output and thus the current buffer), or nil (which
4401 means discard it)."
4402 (tramp-call-process
4403 nil tramp-encoding-shell
4404 (when (and input (not (string-match "%s" cmd))) input)
4405 (if (eq output t) t nil)
4407 tramp-encoding-command-switch
4408 (concat
4409 (if (string-match "%s" cmd) (format cmd input) cmd)
4410 (if (stringp output) (concat " >" output) ""))))
4412 (defconst tramp-inline-compress-commands
4413 '(("gzip" "gzip -d")
4414 ("bzip2" "bzip2 -d")
4415 ("xz" "xz -d")
4416 ("compress" "compress -d"))
4417 "List of compress and decompress commands for inline transfer.
4418 Each item is a list that looks like this:
4420 \(COMPRESS DECOMPRESS\)
4422 COMPRESS or DECOMPRESS are strings with the respective commands.")
4424 (defun tramp-find-inline-compress (vec)
4425 "Find an inline transfer compress command that works.
4426 Goes through the list `tramp-inline-compress-commands'."
4427 (save-excursion
4428 (let ((commands tramp-inline-compress-commands)
4429 (magic "xyzzy")
4430 (p (tramp-get-connection-process vec))
4431 item compress decompress found)
4432 (while (and commands (not found))
4433 (catch 'next
4434 (setq item (pop commands)
4435 compress (nth 0 item)
4436 decompress (nth 1 item))
4437 (tramp-message
4438 vec 5
4439 "Checking local compress commands `%s', `%s' for sanity"
4440 compress decompress)
4441 (unless
4442 (zerop
4443 (tramp-call-local-coding-command
4444 (format
4445 ;; Windows shells need the program file name after
4446 ;; the pipe symbol be quoted if they use forward
4447 ;; slashes as directory separators.
4448 (if (memq system-type '(windows-nt))
4449 "echo %s | \"%s\" | \"%s\""
4450 "echo %s | %s | %s")
4451 magic compress decompress) nil nil))
4452 (throw 'next nil))
4453 (tramp-message
4454 vec 5
4455 "Checking remote compress commands `%s', `%s' for sanity"
4456 compress decompress)
4457 (unless (tramp-send-command-and-check
4458 vec (format "echo %s | %s | %s" magic compress decompress) t)
4459 (throw 'next nil))
4460 (setq found t)))
4462 ;; Did we find something?
4463 (if found
4464 (progn
4465 ;; Set connection properties. Since the commands are
4466 ;; risky (due to output direction), we cache them in the
4467 ;; process cache.
4468 (tramp-message
4469 vec 5 "Using inline transfer compress command `%s'" compress)
4470 (tramp-set-connection-property p "inline-compress" compress)
4471 (tramp-message
4472 vec 5 "Using inline transfer decompress command `%s'" decompress)
4473 (tramp-set-connection-property p "inline-decompress" decompress))
4475 (tramp-set-connection-property p "inline-compress" nil)
4476 (tramp-set-connection-property p "inline-decompress" nil)
4477 (tramp-message
4478 vec 2 "Couldn't find an inline transfer compress command")))))
4480 (defun tramp-compute-multi-hops (vec)
4481 "Expands VEC according to `tramp-default-proxies-alist'.
4482 Gateway hops are already opened."
4483 (let ((target-alist `(,vec))
4484 (hops (or (tramp-file-name-hop vec) ""))
4485 (item vec)
4486 choices proxy)
4488 ;; Ad-hoc proxy definitions.
4489 (dolist (proxy (reverse (split-string hops tramp-postfix-hop-regexp 'omit)))
4490 (let ((user (tramp-file-name-user item))
4491 (host (tramp-file-name-host item))
4492 (proxy (concat
4493 tramp-prefix-format proxy tramp-postfix-host-format)))
4494 (tramp-message
4495 vec 5 "Add proxy (\"%s\" \"%s\" \"%s\")"
4496 (and (stringp host) (regexp-quote host))
4497 (and (stringp user) (regexp-quote user))
4498 proxy)
4499 ;; Add the hop.
4500 (add-to-list
4501 'tramp-default-proxies-alist
4502 (list (and (stringp host) (regexp-quote host))
4503 (and (stringp user) (regexp-quote user))
4504 proxy))
4505 (setq item (tramp-dissect-file-name proxy))))
4506 ;; Save the new value.
4507 (when (and hops tramp-save-ad-hoc-proxies)
4508 (customize-save-variable
4509 'tramp-default-proxies-alist tramp-default-proxies-alist))
4511 ;; Look for proxy hosts to be passed.
4512 (setq choices tramp-default-proxies-alist)
4513 (while choices
4514 (setq item (pop choices)
4515 proxy (eval (nth 2 item)))
4516 (when (and
4517 ;; Host.
4518 (string-match (or (eval (nth 0 item)) "")
4519 (or (tramp-file-name-host (car target-alist)) ""))
4520 ;; User.
4521 (string-match (or (eval (nth 1 item)) "")
4522 (or (tramp-file-name-user (car target-alist)) "")))
4523 (if (null proxy)
4524 ;; No more hops needed.
4525 (setq choices nil)
4526 ;; Replace placeholders.
4527 (setq proxy
4528 (format-spec
4529 proxy
4530 (format-spec-make
4531 ?u (or (tramp-file-name-user (car target-alist)) "")
4532 ?h (or (tramp-file-name-host (car target-alist)) ""))))
4533 (with-parsed-tramp-file-name proxy l
4534 ;; Add the hop.
4535 (push l target-alist)
4536 ;; Start next search.
4537 (setq choices tramp-default-proxies-alist)))))
4539 ;; Handle gateways.
4540 (when (and (boundp 'tramp-gw-tunnel-method) (boundp 'tramp-gw-socks-method)
4541 (string-match
4542 (format
4543 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method)
4544 (tramp-file-name-method (car target-alist))))
4545 (let ((gw (pop target-alist))
4546 (hop (pop target-alist)))
4547 ;; Is the method prepared for gateways?
4548 (unless (tramp-file-name-port hop)
4549 (tramp-error
4550 vec 'file-error
4551 "Connection `%s' is not supported for gateway access." hop))
4552 ;; Open the gateway connection.
4553 (push
4554 (vector
4555 (tramp-file-name-method hop) (tramp-file-name-user hop)
4556 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop) nil nil)
4557 target-alist)
4558 ;; For the password prompt, we need the correct values.
4559 ;; Therefore, we must remember the gateway vector. But we
4560 ;; cannot do it as connection property, because it shouldn't
4561 ;; be persistent. And we have no started process yet either.
4562 (let ((tramp-verbose 0))
4563 (tramp-set-file-property (car target-alist) "" "gateway" hop))))
4565 ;; Foreign and out-of-band methods are not supported for multi-hops.
4566 (when (cdr target-alist)
4567 (setq choices target-alist)
4568 (while choices
4569 (setq item (pop choices))
4570 (when
4572 (not
4573 (tramp-get-method-parameter
4574 (tramp-file-name-method item) 'tramp-login-program))
4575 (tramp-get-method-parameter
4576 (tramp-file-name-method item) 'tramp-copy-program))
4577 (tramp-error
4578 vec 'file-error
4579 "Method `%s' is not supported for multi-hops."
4580 (tramp-file-name-method item)))))
4582 ;; In case the host name is not used for the remote shell
4583 ;; command, the user could be misguided by applying a random
4584 ;; host name.
4585 (let* ((v (car target-alist))
4586 (method (tramp-file-name-method v))
4587 (host (tramp-file-name-host v)))
4588 (unless
4590 ;; There are multi-hops.
4591 (cdr target-alist)
4592 ;; The host name is used for the remote shell command.
4593 (member
4594 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
4595 ;; The host is local. We cannot use `tramp-local-host-p'
4596 ;; here, because it opens a connection as well.
4597 (string-match tramp-local-host-regexp host))
4598 (tramp-error
4599 v 'file-error
4600 "Host `%s' looks like a remote host, `%s' can only use the local host"
4601 host method)))
4603 ;; Result.
4604 target-alist))
4606 (defun tramp-ssh-controlmaster-options (vec)
4607 "Return the Control* arguments of the local ssh."
4608 (cond
4609 ;; No options to be computed.
4610 ((or (null tramp-use-ssh-controlmaster-options)
4611 (null (assoc "%c" (tramp-get-method-parameter
4612 (tramp-file-name-method vec) 'tramp-login-args))))
4615 ;; There is already a value to be used.
4616 ((stringp tramp-ssh-controlmaster-options) tramp-ssh-controlmaster-options)
4618 ;; Determine the options.
4619 (t (setq tramp-ssh-controlmaster-options "")
4620 (let ((case-fold-search t))
4621 (ignore-errors
4622 (when (executable-find "ssh")
4623 (with-temp-buffer
4624 (tramp-call-process vec "ssh" nil t nil "-o" "ControlMaster")
4625 (goto-char (point-min))
4626 (when (search-forward-regexp "missing.+argument" nil t)
4627 (setq tramp-ssh-controlmaster-options "-o ControlMaster=auto")))
4628 (unless (zerop (length tramp-ssh-controlmaster-options))
4629 (with-temp-buffer
4630 ;; When we use a non-existing host name, we could run
4631 ;; into DNS timeouts. So we use "localhost" with an
4632 ;; improper port, expecting nobody runs sshd on the
4633 ;; telnet port.
4634 (tramp-call-process
4635 vec "ssh" nil t nil
4636 "-p" "23" "-o" "ControlPath=%C" "localhost")
4637 (goto-char (point-min))
4638 (setq tramp-ssh-controlmaster-options
4639 (if (search-forward-regexp "unknown.+key" nil t)
4640 (concat tramp-ssh-controlmaster-options
4641 " -o ControlPath='tramp.%%r@%%h:%%p'")
4642 (concat tramp-ssh-controlmaster-options
4643 " -o ControlPath='tramp.%%C'"))))
4644 (with-temp-buffer
4645 (tramp-call-process vec "ssh" nil t nil "-o" "ControlPersist")
4646 (goto-char (point-min))
4647 (when (search-forward-regexp "missing.+argument" nil t)
4648 (setq tramp-ssh-controlmaster-options
4649 (concat tramp-ssh-controlmaster-options
4650 " -o ControlPersist=no"))))))))
4651 tramp-ssh-controlmaster-options)))
4653 (defun tramp-maybe-open-connection (vec)
4654 "Maybe open a connection VEC.
4655 Does not do anything if a connection is already open, but re-opens the
4656 connection if a previous connection has died for some reason."
4657 (tramp-check-proper-method-and-host vec)
4659 (let ((p (tramp-get-connection-process vec))
4660 (process-name (tramp-get-connection-property vec "process-name" nil))
4661 (process-environment (copy-sequence process-environment))
4662 (pos (with-current-buffer (tramp-get-connection-buffer vec) (point))))
4664 ;; If Tramp opens the same connection within a short time frame,
4665 ;; there is a problem. We shall signal this.
4666 (unless (or (and p (processp p) (memq (process-status p) '(run open)))
4667 (not (equal (butlast (append vec nil) 2)
4668 (car tramp-current-connection)))
4669 (> (tramp-time-diff
4670 (current-time) (cdr tramp-current-connection))
4671 (or tramp-connection-min-time-diff 0)))
4672 (throw 'suppress 'suppress))
4674 ;; If too much time has passed since last command was sent, look
4675 ;; whether process is still alive. If it isn't, kill it. When
4676 ;; using ssh, it can sometimes happen that the remote end has hung
4677 ;; up but the local ssh client doesn't recognize this until it
4678 ;; tries to send some data to the remote end. So that's why we
4679 ;; try to send a command from time to time, then look again
4680 ;; whether the process is really alive.
4681 (condition-case nil
4682 (when (and (> (tramp-time-diff
4683 (current-time)
4684 (tramp-get-connection-property
4685 p "last-cmd-time" '(0 0 0)))
4687 p (processp p) (memq (process-status p) '(run open)))
4688 (tramp-send-command vec "echo are you awake" t t)
4689 (unless (and (memq (process-status p) '(run open))
4690 (tramp-wait-for-output p 10))
4691 ;; The error will be caught locally.
4692 (tramp-error vec 'file-error "Awake did fail")))
4693 (file-error
4694 (tramp-cleanup-connection vec t)
4695 (setq p nil)))
4697 ;; New connection must be opened.
4698 (condition-case err
4699 (unless (and p (processp p) (memq (process-status p) '(run open)))
4701 ;; If `non-essential' is non-nil, don't reopen a new connection.
4702 (when (and (boundp 'non-essential) (symbol-value 'non-essential))
4703 (throw 'non-essential 'non-essential))
4705 (with-tramp-progress-reporter
4706 vec 3
4707 (if (zerop (length (tramp-file-name-user vec)))
4708 (format "Opening connection for %s using %s"
4709 (tramp-file-name-host vec)
4710 (tramp-file-name-method vec))
4711 (format "Opening connection for %s@%s using %s"
4712 (tramp-file-name-user vec)
4713 (tramp-file-name-host vec)
4714 (tramp-file-name-method vec)))
4716 (catch 'uname-changed
4717 ;; Start new process.
4718 (when (and p (processp p))
4719 (delete-process p))
4720 (setenv "TERM" tramp-terminal-type)
4721 (setenv "LC_ALL" "en_US.utf8")
4722 (if (stringp tramp-histfile-override)
4723 (setenv "HISTFILE" tramp-histfile-override)
4724 (if tramp-histfile-override
4725 (progn
4726 (setenv "HISTFILE")
4727 (setenv "HISTFILESIZE" "0")
4728 (setenv "HISTSIZE" "0"))))
4729 (setenv "PROMPT_COMMAND")
4730 (setenv "PS1" tramp-initial-end-of-output)
4731 (let* ((target-alist (tramp-compute-multi-hops vec))
4732 ;; We will apply `tramp-ssh-controlmaster-options'
4733 ;; only for the first hop.
4734 (options (tramp-ssh-controlmaster-options vec))
4735 (process-connection-type tramp-process-connection-type)
4736 (process-adaptive-read-buffering nil)
4737 (coding-system-for-read nil)
4738 ;; This must be done in order to avoid our file
4739 ;; name handler.
4740 (p (let ((default-directory
4741 (tramp-compat-temporary-file-directory)))
4742 (apply
4743 'start-process
4744 (tramp-get-connection-name vec)
4745 (tramp-get-connection-buffer vec)
4746 (if tramp-encoding-command-interactive
4747 (list tramp-encoding-shell
4748 tramp-encoding-command-interactive)
4749 (list tramp-encoding-shell))))))
4751 ;; Set sentinel and query flag.
4752 (tramp-set-connection-property p "vector" vec)
4753 (set-process-sentinel p 'tramp-process-sentinel)
4754 (tramp-compat-set-process-query-on-exit-flag p nil)
4755 (setq tramp-current-connection
4756 (cons (butlast (append vec nil) 2) (current-time))
4757 tramp-current-host (system-name))
4759 (tramp-message
4760 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
4762 ;; Check whether process is alive.
4763 (tramp-barf-if-no-shell-prompt
4764 p 10
4765 "Couldn't find local shell prompt for %s" tramp-encoding-shell)
4767 ;; Now do all the connections as specified.
4768 (while target-alist
4769 (let* ((hop (car target-alist))
4770 (l-method (tramp-file-name-method hop))
4771 (l-user (tramp-file-name-user hop))
4772 (l-host (tramp-file-name-host hop))
4773 (l-port nil)
4774 (login-program
4775 (tramp-get-method-parameter
4776 l-method 'tramp-login-program))
4777 (login-args
4778 (tramp-get-method-parameter
4779 l-method 'tramp-login-args))
4780 (login-env
4781 (tramp-get-method-parameter
4782 l-method 'tramp-login-env))
4783 (async-args
4784 (tramp-get-method-parameter
4785 l-method 'tramp-async-args))
4786 (connection-timeout
4787 (tramp-get-method-parameter
4788 l-method 'tramp-connection-timeout))
4789 (gw-args
4790 (tramp-get-method-parameter l-method 'tramp-gw-args))
4791 (gw (let ((tramp-verbose 0))
4792 (tramp-get-file-property hop "" "gateway" nil)))
4793 (g-method (and gw (tramp-file-name-method gw)))
4794 (g-user (and gw (tramp-file-name-user gw)))
4795 (g-host (and gw (tramp-file-name-real-host gw)))
4796 (command login-program)
4797 ;; We don't create the temporary file. In
4798 ;; fact, it is just a prefix for the
4799 ;; ControlPath option of ssh; the real
4800 ;; temporary file has another name, and it is
4801 ;; created and protected by ssh. It is also
4802 ;; removed by ssh when the connection is
4803 ;; closed. The temporary file name is cached
4804 ;; in the main connection process, therefore
4805 ;; we cannot use `tramp-get-connection-process'.
4806 (tmpfile
4807 (with-tramp-connection-property
4808 (get-process (tramp-buffer-name vec)) "temp-file"
4809 (make-temp-name
4810 (expand-file-name
4811 tramp-temp-name-prefix
4812 (tramp-compat-temporary-file-directory)))))
4813 spec r-shell)
4815 ;; Add arguments for asynchronous processes.
4816 (when (and process-name async-args)
4817 (setq login-args (append async-args login-args)))
4819 ;; Add gateway arguments if necessary.
4820 (when gw
4821 (tramp-set-connection-property p "gateway" t)
4822 (when gw-args
4823 (setq login-args (append gw-args login-args))))
4825 ;; Check for port number. Until now, there's no
4826 ;; need for handling like method, user, host.
4827 (when (string-match tramp-host-with-port-regexp l-host)
4828 (setq l-port (match-string 2 l-host)
4829 l-host (match-string 1 l-host)))
4831 ;; Check, whether there is a restricted shell.
4832 (dolist (elt tramp-restricted-shell-hosts-alist)
4833 (when (string-match elt tramp-current-host)
4834 (setq r-shell t)))
4836 ;; Set variables for computing the prompt for
4837 ;; reading password. They can also be derived
4838 ;; from a gateway.
4839 (setq tramp-current-method (or g-method l-method)
4840 tramp-current-user (or g-user l-user)
4841 tramp-current-host (or g-host l-host))
4843 ;; Add login environment.
4844 (when login-env
4845 (setq
4846 login-env
4847 (mapcar
4848 (lambda (x)
4849 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
4850 (unless (member "" x) (mapconcat 'identity x " ")))
4851 login-env))
4852 (while login-env
4853 (setq command
4854 (format
4855 "%s=%s %s"
4856 (pop login-env)
4857 (tramp-shell-quote-argument (pop login-env))
4858 command)))
4859 (setq command (concat "env " command)))
4861 ;; Replace `login-args' place holders.
4862 (setq
4863 l-host (or l-host "")
4864 l-user (or l-user "")
4865 l-port (or l-port "")
4866 spec (format-spec-make ?t tmpfile)
4867 options (format-spec options spec)
4868 spec (format-spec-make
4869 ?h l-host ?u l-user ?p l-port ?c options)
4870 command
4871 (concat
4872 ;; We do not want to see the trailing local
4873 ;; prompt in `start-file-process'.
4874 (unless r-shell "exec ")
4875 command " "
4876 (mapconcat
4877 (lambda (x)
4878 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
4879 (unless (member "" x) (mapconcat 'identity x " ")))
4880 login-args " ")
4881 ;; Local shell could be a Windows COMSPEC. It
4882 ;; doesn't know the ";" syntax, but we must exit
4883 ;; always for `start-file-process'. It could
4884 ;; also be a restricted shell, which does not
4885 ;; allow "exec".
4886 (when r-shell " && exit || exit")))
4888 ;; Send the command.
4889 (tramp-message vec 3 "Sending command `%s'" command)
4890 (tramp-send-command vec command t t)
4891 (tramp-process-actions
4892 p vec pos tramp-actions-before-shell
4893 (or connection-timeout tramp-connection-timeout))
4894 (tramp-message
4895 vec 3 "Found remote shell prompt on `%s'" l-host))
4896 ;; Next hop.
4897 (setq options ""
4898 target-alist (cdr target-alist)))
4900 ;; Make initial shell settings.
4901 (tramp-open-connection-setup-interactive-shell p vec)))))
4903 ;; When the user did interrupt, we must cleanup.
4904 (quit
4905 (tramp-cleanup-connection vec t)
4906 ;; Propagate the quit signal.
4907 (signal (car err) (cdr err))))))
4909 (defun tramp-send-command (vec command &optional neveropen nooutput)
4910 "Send the COMMAND to connection VEC.
4911 Erases temporary buffer before sending the command. If optional
4912 arg NEVEROPEN is non-nil, never try to open the connection. This
4913 is meant to be used from `tramp-maybe-open-connection' only. The
4914 function waits for output unless NOOUTPUT is set."
4915 (unless neveropen (tramp-maybe-open-connection vec))
4916 (let ((p (tramp-get-connection-process vec)))
4917 (when (tramp-get-connection-property p "remote-echo" nil)
4918 ;; We mark the command string that it can be erased in the output buffer.
4919 (tramp-set-connection-property p "check-remote-echo" t)
4920 ;; If we put `tramp-echo-mark' after a trailing newline (which
4921 ;; is assumed to be unquoted) `tramp-send-string' doesn't see
4922 ;; that newline and adds `tramp-rsh-end-of-line' right after
4923 ;; `tramp-echo-mark', so the remote shell sees two consecutive
4924 ;; trailing line endings and sends two prompts after executing
4925 ;; the command, which confuses `tramp-wait-for-output'.
4926 (when (and (not (string= command ""))
4927 (string-equal (substring command -1) "\n"))
4928 (setq command (substring command 0 -1)))
4929 ;; No need to restore a trailing newline here since `tramp-send-string'
4930 ;; makes sure that the string ends in `tramp-rsh-end-of-line', anyway.
4931 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
4932 ;; Send the command.
4933 (tramp-message vec 6 "%s" command)
4934 (tramp-send-string vec command)
4935 (unless nooutput (tramp-wait-for-output p))))
4937 (defun tramp-wait-for-output (proc &optional timeout)
4938 "Wait for output from remote command."
4939 (unless (buffer-live-p (process-buffer proc))
4940 (delete-process proc)
4941 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
4942 (with-current-buffer (process-buffer proc)
4943 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
4944 ;; be leading escape sequences, which must be ignored.
4945 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
4946 ;; Sometimes, the commands do not return a newline but a
4947 ;; null byte before the shell prompt, for example "git
4948 ;; ls-files -c -z ...".
4949 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
4950 (found (tramp-wait-for-regexp proc timeout regexp1)))
4951 (if found
4952 (let (buffer-read-only)
4953 ;; A simple-minded busybox has sent " ^H" sequences.
4954 ;; Delete them.
4955 (goto-char (point-min))
4956 (when (re-search-forward "^\\(.\b\\)+$" (point-at-eol) t)
4957 (forward-line 1)
4958 (delete-region (point-min) (point)))
4959 ;; Delete the prompt.
4960 (goto-char (point-max))
4961 (re-search-backward regexp nil t)
4962 (delete-region (point) (point-max)))
4963 (if timeout
4964 (tramp-error
4965 proc 'file-error
4966 "[[Remote prompt `%s' not found in %d secs]]"
4967 tramp-end-of-output timeout)
4968 (tramp-error
4969 proc 'file-error
4970 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
4971 ;; Return value is whether end-of-output sentinel was found.
4972 found)))
4974 (defun tramp-send-command-and-check
4975 (vec command &optional subshell dont-suppress-err)
4976 "Run COMMAND and check its exit status.
4977 Sends `echo $?' along with the COMMAND for checking the exit status.
4978 If COMMAND is nil, just sends `echo $?'. Returns t if the exit
4979 status is 0, and nil otherwise.
4981 If the optional argument SUBSHELL is non-nil, the command is
4982 executed in a subshell, ie surrounded by parentheses. If
4983 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
4984 (tramp-send-command
4986 (concat (if subshell "( " "")
4987 command
4988 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
4989 "echo tramp_exit_status $?"
4990 (if subshell " )" "")))
4991 (with-current-buffer (tramp-get-connection-buffer vec)
4992 (goto-char (point-max))
4993 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
4994 (tramp-error
4995 vec 'file-error "Couldn't find exit status of `%s'" command))
4996 (skip-chars-forward "^ ")
4997 (prog1
4998 (zerop (read (current-buffer)))
4999 (let (buffer-read-only)
5000 (delete-region (match-beginning 0) (point-max))))))
5002 (defun tramp-barf-unless-okay (vec command fmt &rest args)
5003 "Run COMMAND, check exit status, throw error if exit status not okay.
5004 Similar to `tramp-send-command-and-check' but accepts two more arguments
5005 FMT and ARGS which are passed to `error'."
5006 (or (tramp-send-command-and-check vec command)
5007 (apply 'tramp-error vec 'file-error fmt args)))
5009 (defun tramp-send-command-and-read (vec command &optional noerror marker)
5010 "Run COMMAND and return the output, which must be a Lisp expression.
5011 If MARKER is a regexp, read the output after that string.
5012 In case there is no valid Lisp expression and NOERROR is nil, it
5013 raises an error."
5014 (when (if noerror
5015 (tramp-send-command-and-check vec command)
5016 (tramp-barf-unless-okay
5017 vec command "`%s' returns with error" command))
5018 (with-current-buffer (tramp-get-connection-buffer vec)
5019 (goto-char (point-min))
5020 ;; Read the marker.
5021 (when (stringp marker)
5022 (condition-case nil
5023 (re-search-forward marker)
5024 (error (unless noerror
5025 (tramp-error
5026 vec 'file-error
5027 "`%s' does not return the marker `%s': `%s'"
5028 command marker (buffer-string))))))
5029 ;; Read the expression.
5030 (condition-case nil
5031 (prog1 (read (current-buffer))
5032 ;; Error handling.
5033 (when (re-search-forward "\\S-" (point-at-eol) t)
5034 (error nil)))
5035 (error (unless noerror
5036 (tramp-error
5037 vec 'file-error
5038 "`%s' does not return a valid Lisp expression: `%s'"
5039 command (buffer-string))))))))
5041 (defun tramp-convert-file-attributes (vec attr)
5042 "Convert `file-attributes' ATTR generated by perl script, stat or ls.
5043 Convert file mode bits to string and set virtual device number.
5044 Return ATTR."
5045 (when attr
5046 ;; Remove color escape sequences from symlink.
5047 (when (stringp (car attr))
5048 (while (string-match tramp-color-escape-sequence-regexp (car attr))
5049 (setcar attr (replace-match "" nil nil (car attr)))))
5050 ;; Convert uid and gid. Use -1 as indication of unusable value.
5051 (when (and (numberp (nth 2 attr)) (< (nth 2 attr) 0))
5052 (setcar (nthcdr 2 attr) -1))
5053 (when (and (floatp (nth 2 attr))
5054 (<= (nth 2 attr) (tramp-compat-most-positive-fixnum)))
5055 (setcar (nthcdr 2 attr) (round (nth 2 attr))))
5056 (when (and (numberp (nth 3 attr)) (< (nth 3 attr) 0))
5057 (setcar (nthcdr 3 attr) -1))
5058 (when (and (floatp (nth 3 attr))
5059 (<= (nth 3 attr) (tramp-compat-most-positive-fixnum)))
5060 (setcar (nthcdr 3 attr) (round (nth 3 attr))))
5061 ;; Convert last access time.
5062 (unless (listp (nth 4 attr))
5063 (setcar (nthcdr 4 attr)
5064 (list (floor (nth 4 attr) 65536)
5065 (floor (mod (nth 4 attr) 65536)))))
5066 ;; Convert last modification time.
5067 (unless (listp (nth 5 attr))
5068 (setcar (nthcdr 5 attr)
5069 (list (floor (nth 5 attr) 65536)
5070 (floor (mod (nth 5 attr) 65536)))))
5071 ;; Convert last status change time.
5072 (unless (listp (nth 6 attr))
5073 (setcar (nthcdr 6 attr)
5074 (list (floor (nth 6 attr) 65536)
5075 (floor (mod (nth 6 attr) 65536)))))
5076 ;; Convert file size.
5077 (when (< (nth 7 attr) 0)
5078 (setcar (nthcdr 7 attr) -1))
5079 (when (and (floatp (nth 7 attr))
5080 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
5081 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
5082 ;; Convert file mode bits to string.
5083 (unless (stringp (nth 8 attr))
5084 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
5085 (when (stringp (car attr))
5086 (aset (nth 8 attr) 0 ?l)))
5087 ;; Convert directory indication bit.
5088 (when (string-match "^d" (nth 8 attr))
5089 (setcar attr t))
5090 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
5091 (when (consp (car attr))
5092 (if (and (stringp (caar attr))
5093 (string-match ".+ -> .\\(.+\\)." (caar attr)))
5094 (setcar attr (match-string 1 (caar attr)))
5095 (setcar attr nil)))
5096 ;; Set file's gid change bit.
5097 (setcar (nthcdr 9 attr)
5098 (if (numberp (nth 3 attr))
5099 (not (= (nth 3 attr)
5100 (tramp-get-remote-gid vec 'integer)))
5101 (not (string-equal
5102 (nth 3 attr)
5103 (tramp-get-remote-gid vec 'string)))))
5104 ;; Convert inode.
5105 (unless (listp (nth 10 attr))
5106 (setcar (nthcdr 10 attr)
5107 (condition-case nil
5108 (cons (floor (nth 10 attr) 65536)
5109 (floor (mod (nth 10 attr) 65536)))
5110 ;; Inodes can be incredible huge. We must hide this.
5111 (error (tramp-get-inode vec)))))
5112 ;; Set virtual device number.
5113 (setcar (nthcdr 11 attr)
5114 (tramp-get-device vec))
5115 attr))
5117 (defun tramp-shell-case-fold (string)
5118 "Converts STRING to shell glob pattern which ignores case."
5119 (mapconcat
5120 (lambda (c)
5121 (if (equal (downcase c) (upcase c))
5122 (vector c)
5123 (format "[%c%c]" (downcase c) (upcase c))))
5124 string
5125 ""))
5127 (defun tramp-make-copy-program-file-name (vec)
5128 "Create a file name suitable to be passed to `scp' or `nc' and workalikes."
5129 (let ((method (tramp-file-name-method vec))
5130 (user (tramp-file-name-user vec))
5131 (host (tramp-file-name-real-host vec))
5132 (localname (tramp-shell-quote-argument
5133 (tramp-file-name-localname vec))))
5134 (cond
5135 ((tramp-get-method-parameter method 'tramp-remote-copy-program)
5136 localname)
5137 ((not (zerop (length user)))
5138 (shell-quote-argument (format "%s@%s:%s" user host localname)))
5139 (t (shell-quote-argument (format "%s:%s" host localname))))))
5141 (defun tramp-method-out-of-band-p (vec size)
5142 "Return t if this is an out-of-band method, nil otherwise."
5143 (and
5144 ;; It shall be an out-of-band method.
5145 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
5146 ;; There must be a size, otherwise the file doesn't exist.
5147 (numberp size)
5148 ;; Either the file size is large enough, or (in rare cases) there
5149 ;; does not exist a remote encoding.
5150 (or (null tramp-copy-size-limit)
5151 (> size tramp-copy-size-limit)
5152 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
5154 ;; Variables local to connection.
5156 (defun tramp-get-remote-path (vec)
5157 (with-tramp-connection-property
5158 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
5159 ;; cache the result for the session only. Otherwise, the result
5160 ;; is cached persistently.
5161 (if (memq 'tramp-own-remote-path tramp-remote-path)
5162 (tramp-get-connection-process vec)
5163 vec)
5164 "remote-path"
5165 (let* ((remote-path (copy-tree tramp-remote-path))
5166 (elt1 (memq 'tramp-default-remote-path remote-path))
5167 (elt2 (memq 'tramp-own-remote-path remote-path))
5168 (default-remote-path
5169 (when elt1
5171 (tramp-send-command-and-read
5172 vec "echo \\\"`getconf PATH 2>/dev/null`\\\"" 'noerror)
5173 ;; Default if "getconf" is not available.
5174 (progn
5175 (tramp-message
5176 vec 3
5177 "`getconf PATH' not successful, using default value \"%s\"."
5178 "/bin:/usr/bin")
5179 "/bin:/usr/bin"))))
5180 (own-remote-path
5181 ;; The login shell could return more than just the $PATH
5182 ;; string. So we use `tramp-end-of-heredoc' as marker.
5183 (when elt2
5184 (tramp-send-command-and-read
5186 (format
5187 "%s -l %s 'echo %s \\\"$PATH\\\"'"
5188 (tramp-get-method-parameter
5189 (tramp-file-name-method vec) 'tramp-remote-shell)
5190 (mapconcat
5191 'identity
5192 (tramp-get-method-parameter
5193 (tramp-file-name-method vec) 'tramp-remote-shell-args)
5194 " ")
5195 (tramp-shell-quote-argument tramp-end-of-heredoc))
5196 nil (regexp-quote tramp-end-of-heredoc)))))
5198 ;; Replace place holder `tramp-default-remote-path'.
5199 (when elt1
5200 (setcdr elt1
5201 (append
5202 (tramp-compat-split-string (or default-remote-path "") ":")
5203 (cdr elt1)))
5204 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
5206 ;; Replace place holder `tramp-own-remote-path'.
5207 (when elt2
5208 (setcdr elt2
5209 (append
5210 (tramp-compat-split-string (or own-remote-path "") ":")
5211 (cdr elt2)))
5212 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
5214 ;; Remove double entries.
5215 (setq elt1 remote-path)
5216 (while (consp elt1)
5217 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
5218 (setcar elt2 nil))
5219 (setq elt1 (cdr elt1)))
5221 ;; Remove non-existing directories.
5222 (delq
5224 (mapcar
5225 (lambda (x)
5226 (and
5227 (stringp x)
5228 (file-directory-p
5229 (tramp-make-tramp-file-name
5230 (tramp-file-name-method vec)
5231 (tramp-file-name-user vec)
5232 (tramp-file-name-host vec)
5235 remote-path)))))
5237 (defun tramp-get-remote-locale (vec)
5238 (with-tramp-connection-property vec "locale"
5239 (tramp-send-command vec "locale -a")
5240 (let ((candidates '("en_US.utf8" "C.utf8"))
5241 locale)
5242 (with-current-buffer (tramp-get-connection-buffer vec)
5243 (while candidates
5244 (goto-char (point-min))
5245 (if (string-match (format "^%s\r?$" (regexp-quote (car candidates)))
5246 (buffer-string))
5247 (setq locale (car candidates)
5248 candidates nil)
5249 (setq candidates (cdr candidates)))))
5250 ;; Return value.
5251 (format "LC_ALL=%s" (or locale "C")))))
5253 (defun tramp-get-ls-command (vec)
5254 (with-tramp-connection-property vec "ls"
5255 (tramp-message vec 5 "Finding a suitable `ls' command")
5257 (catch 'ls-found
5258 (dolist (cmd '("ls" "gnuls" "gls"))
5259 (let ((dl (tramp-get-remote-path vec))
5260 result)
5261 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
5262 ;; Check parameters. On busybox, "ls" output coloring is
5263 ;; enabled by default sometimes. So we try to disable it
5264 ;; when possible. $LS_COLORING is not supported there.
5265 ;; Some "ls" versions are sensible wrt the order of
5266 ;; arguments, they fail when "-al" is after the
5267 ;; "--color=never" argument (for example on FreeBSD).
5268 (when (tramp-send-command-and-check
5269 vec (format "%s -lnd /" result))
5270 (when (tramp-send-command-and-check
5271 vec (format
5272 "%s --color=never -al /dev/null" result))
5273 (setq result (concat result " --color=never")))
5274 (throw 'ls-found result))
5275 (setq dl (cdr dl))))))
5276 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
5278 (defun tramp-get-ls-command-with-dired (vec)
5279 (save-match-data
5280 (with-tramp-connection-property vec "ls-dired"
5281 (tramp-message vec 5 "Checking, whether `ls --dired' works")
5282 ;; Some "ls" versions are sensible wrt the order of arguments,
5283 ;; they fail when "-al" is after the "--dired" argument (for
5284 ;; example on FreeBSD).
5285 (tramp-send-command-and-check
5286 vec (format "%s --dired -al /dev/null" (tramp-get-ls-command vec))))))
5288 (defun tramp-get-ls-command-with-quoting-style (vec)
5289 (save-match-data
5290 (with-tramp-connection-property vec "ls-quoting-style"
5291 (tramp-message vec 5 "Checking, whether `ls --quoting-style=shell' works")
5292 ;; Some "ls" versions are sensible wrt the order of arguments,
5293 ;; they fail when "-al" is after the "--dired" argument (for
5294 ;; example on FreeBSD).
5295 (tramp-send-command-and-check
5296 vec (format "%s --quoting-style=shell -al /dev/null"
5297 (tramp-get-ls-command vec))))))
5299 (defun tramp-get-test-command (vec)
5300 (with-tramp-connection-property vec "test"
5301 (tramp-message vec 5 "Finding a suitable `test' command")
5302 (if (tramp-send-command-and-check vec "test 0")
5303 "test"
5304 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
5306 (defun tramp-get-test-nt-command (vec)
5307 ;; Does `test A -nt B' work? Use abominable `find' construct if it
5308 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
5309 ;; for otherwise the shell crashes.
5310 (with-tramp-connection-property vec "test-nt"
5312 (progn
5313 (tramp-send-command
5314 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
5315 (with-current-buffer (tramp-get-buffer vec)
5316 (goto-char (point-min))
5317 (when (looking-at (regexp-quote tramp-end-of-output))
5318 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
5319 (progn
5320 (tramp-send-command
5322 (format
5323 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
5324 (tramp-get-test-command vec)))
5325 "tramp_test_nt %s %s"))))
5327 (defun tramp-get-file-exists-command (vec)
5328 (with-tramp-connection-property vec "file-exists"
5329 (tramp-message vec 5 "Finding command to check if file exists")
5330 (tramp-find-file-exists-command vec)))
5332 (defun tramp-get-remote-ln (vec)
5333 (with-tramp-connection-property vec "ln"
5334 (tramp-message vec 5 "Finding a suitable `ln' command")
5335 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
5337 (defun tramp-get-remote-perl (vec)
5338 (with-tramp-connection-property vec "perl"
5339 (tramp-message vec 5 "Finding a suitable `perl' command")
5340 (let ((result
5341 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
5342 (tramp-find-executable
5343 vec "perl" (tramp-get-remote-path vec)))))
5344 ;; We must check also for some Perl modules.
5345 (when result
5346 (with-tramp-connection-property vec "perl-file-spec"
5347 (tramp-send-command-and-check
5348 vec (format "%s -e 'use File::Spec;'" result)))
5349 (with-tramp-connection-property vec "perl-cwd-realpath"
5350 (tramp-send-command-and-check
5351 vec (format "%s -e 'use Cwd \"realpath\";'" result))))
5352 result)))
5354 (defun tramp-get-remote-stat (vec)
5355 (with-tramp-connection-property vec "stat"
5356 (tramp-message vec 5 "Finding a suitable `stat' command")
5357 (let ((result (tramp-find-executable
5358 vec "stat" (tramp-get-remote-path vec)))
5359 tmp)
5360 ;; Check whether stat(1) returns usable syntax. "%s" does not
5361 ;; work on older AIX systems.
5362 (when result
5363 (setq tmp
5364 (tramp-send-command-and-read
5365 vec (format "%s -c '(\"%%N\" %%s)' /" result) 'noerror))
5366 (unless (and (listp tmp) (stringp (car tmp))
5367 (string-match "^./.$" (car tmp))
5368 (integerp (cadr tmp)))
5369 (setq result nil)))
5370 result)))
5372 (defun tramp-get-remote-readlink (vec)
5373 (with-tramp-connection-property vec "readlink"
5374 (tramp-message vec 5 "Finding a suitable `readlink' command")
5375 (let ((result (tramp-find-executable
5376 vec "readlink" (tramp-get-remote-path vec))))
5377 (when (and result
5378 (tramp-send-command-and-check
5379 vec (format "%s --canonicalize-missing /" result)))
5380 result))))
5382 (defun tramp-get-remote-trash (vec)
5383 (with-tramp-connection-property vec "trash"
5384 (tramp-message vec 5 "Finding a suitable `trash' command")
5385 (tramp-find-executable vec "trash" (tramp-get-remote-path vec))))
5387 (defun tramp-get-remote-touch (vec)
5388 (with-tramp-connection-property vec "touch"
5389 (tramp-message vec 5 "Finding a suitable `touch' command")
5390 (let ((result (tramp-find-executable
5391 vec "touch" (tramp-get-remote-path vec)))
5392 (tmpfile
5393 (make-temp-name
5394 (expand-file-name
5395 tramp-temp-name-prefix (tramp-get-remote-tmpdir vec)))))
5396 ;; Busyboxes do support the "-t" option only when they have been
5397 ;; built with the DESKTOP config option. Let's check it.
5398 (when result
5399 (tramp-set-connection-property
5400 vec "touch-t"
5401 (tramp-send-command-and-check
5403 (format
5404 "%s -t %s %s"
5405 result
5406 (format-time-string "%Y%m%d%H%M.%S")
5407 (tramp-file-name-handler 'file-remote-p tmpfile 'localname))))
5408 (delete-file tmpfile))
5409 result)))
5411 (defun tramp-get-remote-gvfs-monitor-dir (vec)
5412 (with-tramp-connection-property vec "gvfs-monitor-dir"
5413 (tramp-message vec 5 "Finding a suitable `gvfs-monitor-dir' command")
5414 (tramp-find-executable
5415 vec "gvfs-monitor-dir" (tramp-get-remote-path vec) t t)))
5417 (defun tramp-get-remote-inotifywait (vec)
5418 (with-tramp-connection-property vec "inotifywait"
5419 (tramp-message vec 5 "Finding a suitable `inotifywait' command")
5420 (tramp-find-executable vec "inotifywait" (tramp-get-remote-path vec) t t)))
5422 (defun tramp-get-remote-id (vec)
5423 (with-tramp-connection-property vec "id"
5424 (tramp-message vec 5 "Finding POSIX `id' command")
5425 (catch 'id-found
5426 (dolist (cmd '("id" "gid"))
5427 (let ((dl (tramp-get-remote-path vec))
5428 result)
5429 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
5430 ;; Check POSIX parameter.
5431 (when (tramp-send-command-and-check vec (format "%s -u" result))
5432 (throw 'id-found result))
5433 (setq dl (cdr dl))))))))
5435 (defun tramp-get-remote-uid-with-id (vec id-format)
5436 (tramp-send-command-and-read
5438 (format "%s -u%s %s"
5439 (tramp-get-remote-id vec)
5440 (if (equal id-format 'integer) "" "n")
5441 (if (equal id-format 'integer)
5442 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/"))))
5444 (defun tramp-get-remote-uid-with-perl (vec id-format)
5445 (tramp-send-command-and-read
5447 (format "%s -le '%s'"
5448 (tramp-get-remote-perl vec)
5449 (if (equal id-format 'integer)
5450 "print $>"
5451 "print \"\\\"\", scalar getpwuid($>), \"\\\"\""))))
5453 (defun tramp-get-remote-python (vec)
5454 (with-tramp-connection-property vec "python"
5455 (tramp-message vec 5 "Finding a suitable `python' command")
5456 (or (tramp-find-executable vec "python" (tramp-get-remote-path vec))
5457 (tramp-find-executable vec "python2" (tramp-get-remote-path vec))
5458 (tramp-find-executable vec "python3" (tramp-get-remote-path vec)))))
5460 (defun tramp-get-remote-uid-with-python (vec id-format)
5461 (tramp-send-command-and-read
5463 (format "%s -c \"%s\""
5464 (tramp-get-remote-python vec)
5465 (if (equal id-format 'integer)
5466 "import os; print (os.getuid())"
5467 "import os, pwd; print ('\\\"' + pwd.getpwuid(os.getuid())[0] + '\\\"')"))))
5469 (defun tramp-get-remote-uid (vec id-format)
5470 (with-tramp-connection-property vec (format "uid-%s" id-format)
5471 (let ((res
5472 (ignore-errors
5473 (cond
5474 ((tramp-get-remote-id vec)
5475 (tramp-get-remote-uid-with-id vec id-format))
5476 ((tramp-get-remote-perl vec)
5477 (tramp-get-remote-uid-with-perl vec id-format))
5478 ((tramp-get-remote-python vec)
5479 (tramp-get-remote-uid-with-python vec id-format))))))
5480 ;; Ensure there is a valid result.
5481 (cond
5482 ((and (equal id-format 'integer) (not (integerp res))) -1)
5483 ((and (equal id-format 'string) (not (stringp res))) "UNKNOWN")
5484 (t res)))))
5486 (defun tramp-get-remote-gid-with-id (vec id-format)
5487 (tramp-send-command-and-read
5489 (format "%s -g%s %s"
5490 (tramp-get-remote-id vec)
5491 (if (equal id-format 'integer) "" "n")
5492 (if (equal id-format 'integer)
5493 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/"))))
5495 (defun tramp-get-remote-gid-with-perl (vec id-format)
5496 (tramp-send-command-and-read
5498 (format "%s -le '%s'"
5499 (tramp-get-remote-perl vec)
5500 (if (equal id-format 'integer)
5501 "print ($)=~/(\\d+)/)"
5502 "print \"\\\"\", scalar getgrgid($)), \"\\\"\""))))
5504 (defun tramp-get-remote-gid-with-python (vec id-format)
5505 (tramp-send-command-and-read
5507 (format "%s -c \"%s\""
5508 (tramp-get-remote-python vec)
5509 (if (equal id-format 'integer)
5510 "import os; print (os.getgid())"
5511 "import os, grp; print ('\\\"' + grp.getgrgid(os.getgid())[0] + '\\\"')"))))
5513 (defun tramp-get-remote-gid (vec id-format)
5514 (with-tramp-connection-property vec (format "gid-%s" id-format)
5515 (let ((res
5516 (ignore-errors
5517 (cond
5518 ((tramp-get-remote-id vec)
5519 (tramp-get-remote-gid-with-id vec id-format))
5520 ((tramp-get-remote-perl vec)
5521 (tramp-get-remote-gid-with-perl vec id-format))
5522 ((tramp-get-remote-python vec)
5523 (tramp-get-remote-gid-with-python vec id-format))))))
5524 ;; Ensure there is a valid result.
5525 (cond
5526 ((and (equal id-format 'integer) (not (integerp res))) -1)
5527 ((and (equal id-format 'string) (not (stringp res))) "UNKNOWN")
5528 (t res)))))
5530 ;; Some predefined connection properties.
5531 (defun tramp-get-inline-compress (vec prop size)
5532 "Return the compress command related to PROP.
5533 PROP is either `inline-compress' or `inline-decompress'. SIZE is
5534 the length of the file to be compressed.
5536 If no corresponding command is found, nil is returned."
5537 (when (and (integerp tramp-inline-compress-start-size)
5538 (> size tramp-inline-compress-start-size))
5539 (with-tramp-connection-property (tramp-get-connection-process vec) prop
5540 (tramp-find-inline-compress vec)
5541 (tramp-get-connection-property
5542 (tramp-get-connection-process vec) prop nil))))
5544 (defun tramp-get-inline-coding (vec prop size)
5545 "Return the coding command related to PROP.
5546 PROP is either `remote-encoding', `remote-decoding',
5547 `local-encoding' or `local-decoding'.
5549 SIZE is the length of the file to be coded. Depending on SIZE,
5550 compression might be applied.
5552 If no corresponding command is found, nil is returned.
5553 Otherwise, either a string is returned which contains a `%s' mark
5554 to be used for the respective input or output file; or a Lisp
5555 function cell is returned to be applied on a buffer."
5556 ;; We must catch the errors, because we want to return nil, when
5557 ;; no inline coding is found.
5558 (ignore-errors
5559 (let ((coding
5560 (with-tramp-connection-property
5561 (tramp-get-connection-process vec) prop
5562 (tramp-find-inline-encoding vec)
5563 (tramp-get-connection-property
5564 (tramp-get-connection-process vec) prop nil)))
5565 (prop1 (if (string-match "encoding" prop)
5566 "inline-compress" "inline-decompress"))
5567 compress)
5568 ;; The connection property might have been cached. So we must
5569 ;; send the script to the remote side - maybe.
5570 (when (and coding (symbolp coding) (string-match "remote" prop))
5571 (let ((name (symbol-name coding)))
5572 (while (string-match (regexp-quote "-") name)
5573 (setq name (replace-match "_" nil t name)))
5574 (tramp-maybe-send-script vec (symbol-value coding) name)
5575 (setq coding name)))
5576 (when coding
5577 ;; Check for the `compress' command.
5578 (setq compress (tramp-get-inline-compress vec prop1 size))
5579 ;; Return the value.
5580 (cond
5581 ((and compress (symbolp coding))
5582 (if (string-match "decompress" prop1)
5583 `(lambda (beg end)
5584 (,coding beg end)
5585 (let ((coding-system-for-write 'binary)
5586 (coding-system-for-read 'binary)
5587 (default-directory
5588 (tramp-compat-temporary-file-directory)))
5589 (apply
5590 'call-process-region (point-min) (point-max)
5591 (car (split-string ,compress)) t t nil
5592 (cdr (split-string ,compress)))))
5593 `(lambda (beg end)
5594 (let ((coding-system-for-write 'binary)
5595 (coding-system-for-read 'binary)
5596 (default-directory
5597 (tramp-compat-temporary-file-directory)))
5598 (apply
5599 'call-process-region beg end
5600 (car (split-string ,compress)) t t nil
5601 (cdr (split-string ,compress))))
5602 (,coding (point-min) (point-max)))))
5603 ((symbolp coding)
5604 coding)
5605 ((and compress (string-match "decoding" prop))
5606 (format
5607 ;; Windows shells need the program file name after
5608 ;; the pipe symbol be quoted if they use forward
5609 ;; slashes as directory separators.
5610 (cond
5611 ((and (string-match "local" prop)
5612 (memq system-type '(windows-nt)))
5613 "(%s | \"%s\")")
5614 ((string-match "local" prop) "(%s | %s)")
5615 (t "(%s | %s >%%s)"))
5616 coding compress))
5617 (compress
5618 (format
5619 ;; Windows shells need the program file name after
5620 ;; the pipe symbol be quoted if they use forward
5621 ;; slashes as directory separators.
5622 (if (and (string-match "local" prop)
5623 (memq system-type '(windows-nt)))
5624 "(%s <%%s | \"%s\")"
5625 "(%s <%%s | %s)")
5626 compress coding))
5627 ((string-match "decoding" prop)
5628 (cond
5629 ((string-match "local" prop) (format "%s" coding))
5630 (t (format "%s >%%s" coding))))
5632 (format "%s <%%s" coding)))))))
5634 (add-hook 'tramp-unload-hook
5635 (lambda ()
5636 (unload-feature 'tramp-sh 'force)))
5638 (provide 'tramp-sh)
5640 ;;; TODO:
5642 ;; * Don't use globbing for directories with many files, as this is
5643 ;; likely to produce long command lines, and some shells choke on
5644 ;; long command lines.
5645 ;; * Make it work for different encodings, and for different file name
5646 ;; encodings, too. (Daniel Pittman)
5647 ;; * Don't search for perl5 and perl. Instead, only search for perl and
5648 ;; then look if it's the right version (with `perl -v').
5649 ;; * When editing a remote CVS controlled file as a different user, VC
5650 ;; gets confused about the file locking status. Try to find out why
5651 ;; the workaround doesn't work.
5652 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5653 ;; until the last but one hop via `start-file-process'. Apply it
5654 ;; also for ftp and smb.
5655 ;; * WIBNI if we had a command "trampclient"? If I was editing in
5656 ;; some shell with root privileges, it would be nice if I could
5657 ;; just call
5658 ;; trampclient filename.c
5659 ;; as an editor, and the _current_ shell would connect to an Emacs
5660 ;; server and would be used in an existing non-privileged Emacs
5661 ;; session for doing the editing in question.
5662 ;; That way, I need not tell Emacs my password again and be afraid
5663 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
5664 ;; once display a just typed password in the context of a keyboard
5665 ;; sequence prompt for a question immediately following in a shell
5666 ;; script run within Emacs -- nasty).
5667 ;; And if I have some ssh session running to a different computer,
5668 ;; having the possibility of passing a local file there to a local
5669 ;; Emacs session (in case I can arrange for a connection back) would
5670 ;; be nice.
5671 ;; Likely the corresponding Tramp server should not allow the
5672 ;; equivalent of the emacsclient -eval option in order to make this
5673 ;; reasonably unproblematic. And maybe trampclient should have some
5674 ;; way of passing credentials, like by using an SSL socket or
5675 ;; something. (David Kastrup)
5676 ;; * Reconnect directly to a compliant shell without first going
5677 ;; through the user's default shell. (Pete Forman)
5678 ;; * How can I interrupt the remote process with a signal
5679 ;; (interrupt-process seems not to work)? (Markus Triska)
5680 ;; * Avoid the local shell entirely for starting remote processes. If
5681 ;; so, I think even a signal, when delivered directly to the local
5682 ;; SSH instance, would correctly be propagated to the remote process
5683 ;; automatically; possibly SSH would have to be started with
5684 ;; "-t". (Markus Triska)
5685 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5686 ;; isn't on the remote host. (Mark A. Hershberger)
5687 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
5688 ;; * Optimize out-of-band copying when both methods are scp-like (not
5689 ;; rsync).
5690 ;; * Keep a second connection open for out-of-band methods like scp or
5691 ;; rsync.
5693 ;;; tramp-sh.el ends here