1 ;;; tramp-sh.el --- Tramp access functions for (s)sh-like connections
3 ;; Copyright (C) 1998-2013 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
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/>.
29 (eval-when-compile (require 'cl)) ; ignore-errors
32 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
33 ;; not sure at all that this is the right way to do it, but let's hope
34 ;; it works for now, and wait for a guru to point out the Right Way to
37 ;; (unless (fboundp 'dired-insert-set-properties)
38 ;; (fset 'dired-insert-set-properties 'ignore)))
39 ;; Gerd suggests this:
40 (eval-when-compile (require 'dired))
41 ;; Note that dired is required at run-time, too, when it is needed.
42 ;; It is only needed on XEmacs for the function
43 ;; `dired-insert-set-properties'.
45 (defcustom tramp-inline-compress-start-size 4096
46 "The minimum size of compressing where inline transfer.
47 When inline transfer, compress transferred data of file
48 whose size is this value or above (up to `tramp-copy-size-limit').
49 If it is nil, no compression at all will be applied."
51 :type '(choice (const nil) integer))
53 (defcustom tramp-copy-size-limit 10240
54 "The maximum file size where inline copying is preferred over an \
56 If it is nil, out-of-the-band copy will be used without a check."
58 :type '(choice (const nil) integer))
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."
69 (defconst tramp-color-escape-sequence-regexp "\e[[;0-9]+m"
70 "Escape sequences produced by the \"ls\" command.")
72 ;; ksh on OpenBSD 4.5 requires that $PS1 contains a `#' character for
73 ;; root users. It uses the `$' character for other users. In order
74 ;; to guarantee a proper prompt, we use "#$ " for the prompt.
76 (defvar tramp-end-of-output
79 (md5 (concat (prin1-to-string process-environment) (current-time-string))))
80 "String used to recognize end of output.
81 The '$' character at the end is quoted; the string cannot be
82 detected as prompt when being sent on echoing hosts, therefore.")
85 (defconst tramp-initial-end-of-output "#$ "
86 "Prompt when establishing a connection.")
88 ;; Initialize `tramp-methods' with the supported methods.
90 (add-to-list 'tramp-methods
92 (tramp-login-program "rsh")
93 (tramp-login-args (("%h") ("-l" "%u")))
94 (tramp-remote-shell "/bin/sh")
95 (tramp-remote-shell-args ("-c"))
96 (tramp-copy-program "rcp")
97 (tramp-copy-args (("-p" "%k") ("-r")))
98 (tramp-copy-keep-date t)
99 (tramp-copy-recursive t)))
101 (add-to-list 'tramp-methods
103 (tramp-login-program "remsh")
104 (tramp-login-args (("%h") ("-l" "%u")))
105 (tramp-remote-shell "/bin/sh")
106 (tramp-remote-shell-args ("-c"))
107 (tramp-copy-program "rcp")
108 (tramp-copy-args (("-p" "%k")))
109 (tramp-copy-keep-date t)))
111 (add-to-list 'tramp-methods
113 (tramp-login-program "ssh")
114 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
115 ("-e" "none") ("%h")))
116 (tramp-async-args (("-q")))
117 (tramp-remote-shell "/bin/sh")
118 (tramp-remote-shell-args ("-c"))
119 (tramp-copy-program "scp")
120 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r") ("%c")))
121 (tramp-copy-keep-date t)
122 (tramp-copy-recursive t)
123 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
124 ("-o" "UserKnownHostsFile=/dev/null")
125 ("-o" "StrictHostKeyChecking=no")))
126 (tramp-default-port 22)))
128 (add-to-list 'tramp-methods
130 (tramp-login-program "ssh")
131 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
132 ("-1") ("-e" "none") ("%h")))
133 (tramp-async-args (("-q")))
134 (tramp-remote-shell "/bin/sh")
135 (tramp-remote-shell-args ("-c"))
136 (tramp-copy-program "scp")
137 (tramp-copy-args (("-1") ("-P" "%p") ("-p" "%k")
138 ("-q") ("-r") ("%c")))
139 (tramp-copy-keep-date t)
140 (tramp-copy-recursive t)
141 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
142 ("-o" "UserKnownHostsFile=/dev/null")
143 ("-o" "StrictHostKeyChecking=no")))
144 (tramp-default-port 22)))
146 (add-to-list 'tramp-methods
148 (tramp-login-program "ssh")
149 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
150 ("-2") ("-e" "none") ("%h")))
151 (tramp-async-args (("-q")))
152 (tramp-remote-shell "/bin/sh")
153 (tramp-remote-shell-args ("-c"))
154 (tramp-copy-program "scp")
155 (tramp-copy-args (("-2") ("-P" "%p") ("-p" "%k")
156 ("-q") ("-r") ("%c")))
157 (tramp-copy-keep-date t)
158 (tramp-copy-recursive t)
159 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
160 ("-o" "UserKnownHostsFile=/dev/null")
161 ("-o" "StrictHostKeyChecking=no")))
162 (tramp-default-port 22)))
164 (add-to-list 'tramp-methods
166 (tramp-login-program "ssh")
167 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
168 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
169 (tramp-async-args (("-q")))
170 (tramp-remote-shell "/bin/sh")
171 (tramp-remote-shell-args ("-c"))
172 (tramp-copy-program "scp")
173 (tramp-copy-args (("-P" "%p") ("-p" "%k")
174 ("-q") ("-r") ("%c")))
175 (tramp-copy-keep-date t)
176 (tramp-copy-recursive t)
177 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
178 ("-o" "UserKnownHostsFile=/dev/null")
179 ("-o" "StrictHostKeyChecking=no")))
180 (tramp-default-port 22)))
182 (add-to-list 'tramp-methods
184 (tramp-login-program "ssh")
185 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
186 ("-e" "none") ("%h")))
187 (tramp-async-args (("-q")))
188 (tramp-remote-shell "/bin/sh")
189 (tramp-remote-shell-args ("-c"))
190 (tramp-copy-program "sftp")
191 (tramp-copy-args ("%c"))))
193 (add-to-list 'tramp-methods
195 (tramp-login-program "ssh")
196 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
197 ("-e" "none") ("%h")))
198 (tramp-async-args (("-q")))
199 (tramp-remote-shell "/bin/sh")
200 (tramp-remote-shell-args ("-c"))
201 (tramp-copy-program "rsync")
202 (tramp-copy-args (("-t" "%k") ("-r")))
203 (tramp-copy-env (("RSYNC_RSH") ("ssh" "%c")))
204 (tramp-copy-keep-date t)
205 (tramp-copy-keep-tmpfile t)
206 (tramp-copy-recursive t)))
208 (add-to-list 'tramp-methods
210 (tramp-login-program "rsh")
211 (tramp-login-args (("%h") ("-l" "%u")))
212 (tramp-remote-shell "/bin/sh")
213 (tramp-remote-shell-args ("-c"))))
215 (add-to-list 'tramp-methods
217 (tramp-login-program "remsh")
218 (tramp-login-args (("%h") ("-l" "%u")))
219 (tramp-remote-shell "/bin/sh")
220 (tramp-remote-shell-args ("-c"))))
222 (add-to-list 'tramp-methods
224 (tramp-login-program "ssh")
225 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
226 ("-e" "none") ("%h")))
227 (tramp-async-args (("-q")))
228 (tramp-remote-shell "/bin/sh")
229 (tramp-remote-shell-args ("-c"))
230 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
231 ("-o" "UserKnownHostsFile=/dev/null")
232 ("-o" "StrictHostKeyChecking=no")))
233 (tramp-default-port 22)))
235 (add-to-list 'tramp-methods
237 (tramp-login-program "ssh")
238 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
239 ("-1") ("-e" "none") ("%h")))
240 (tramp-async-args (("-q")))
241 (tramp-remote-shell "/bin/sh")
242 (tramp-remote-shell-args ("-c"))
243 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
244 ("-o" "UserKnownHostsFile=/dev/null")
245 ("-o" "StrictHostKeyChecking=no")))
246 (tramp-default-port 22)))
248 (add-to-list 'tramp-methods
250 (tramp-login-program "ssh")
251 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
252 ("-2") ("-e" "none") ("%h")))
253 (tramp-async-args (("-q")))
254 (tramp-remote-shell "/bin/sh")
255 (tramp-remote-shell-args ("-c"))
256 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
257 ("-o" "UserKnownHostsFile=/dev/null")
258 ("-o" "StrictHostKeyChecking=no")))
259 (tramp-default-port 22)))
261 (add-to-list 'tramp-methods
263 (tramp-login-program "ssh")
264 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
265 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
266 (tramp-async-args (("-q")))
267 (tramp-remote-shell "/bin/sh")
268 (tramp-remote-shell-args ("-c"))
269 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
270 ("-o" "UserKnownHostsFile=/dev/null")
271 ("-o" "StrictHostKeyChecking=no")))
272 (tramp-default-port 22)))
274 (add-to-list 'tramp-methods
276 (tramp-login-program "telnet")
277 (tramp-login-args (("%h") ("%p")))
278 (tramp-remote-shell "/bin/sh")
279 (tramp-remote-shell-args ("-c"))
280 (tramp-default-port 23)))
282 (add-to-list 'tramp-methods
284 (tramp-login-program "su")
285 (tramp-login-args (("-") ("%u")))
286 (tramp-remote-shell "/bin/sh")
287 (tramp-remote-shell-args ("-c"))))
289 (add-to-list 'tramp-methods
291 (tramp-login-program "sudo")
292 (tramp-login-args (("-u" "%u") ("-s") ("-H") ("-p" "Password:")))
293 (tramp-remote-shell "/bin/sh")
294 (tramp-remote-shell-args ("-c"))))
296 (add-to-list 'tramp-methods
298 (tramp-login-program "ksu")
299 (tramp-login-args (("%u") ("-q")))
300 (tramp-remote-shell "/bin/sh")
301 (tramp-remote-shell-args ("-c"))))
303 (add-to-list 'tramp-methods
305 (tramp-login-program "krlogin")
306 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
307 (tramp-remote-shell "/bin/sh")
308 (tramp-remote-shell-args ("-c"))))
310 (add-to-list 'tramp-methods
312 (tramp-login-program "plink")
313 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
314 (tramp-remote-shell "/bin/sh")
315 (tramp-remote-shell-args ("-c"))
316 (tramp-default-port 22)))
318 (add-to-list 'tramp-methods
320 (tramp-login-program "plink")
321 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-1" "-ssh") ("%h")))
322 (tramp-remote-shell "/bin/sh")
323 (tramp-remote-shell-args ("-c"))
324 (tramp-default-port 22)))
326 (add-to-list 'tramp-methods
328 (tramp-login-program "plink")
329 ;; ("%h") must be a single element, see
330 ;; `tramp-compute-multi-hops'.
331 (tramp-login-args (("-load") ("%h") ("-t")
333 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
335 tramp-initial-end-of-output))
337 (tramp-remote-shell "/bin/sh")
338 (tramp-remote-shell-args ("-c"))))
340 (add-to-list 'tramp-methods
342 (tramp-login-program "plink")
343 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
344 (tramp-remote-shell "/bin/sh")
345 (tramp-remote-shell-args ("-c"))
346 (tramp-copy-program "pscp")
347 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-scp") ("-p" "%k")
349 (tramp-copy-keep-date t)
350 (tramp-copy-recursive t)
351 (tramp-default-port 22)))
353 (add-to-list 'tramp-methods
355 (tramp-login-program "plink")
356 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
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")
362 (tramp-copy-keep-date t)
363 (tramp-copy-recursive t)))
365 (add-to-list 'tramp-methods
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)))
376 (add-to-list 'tramp-default-method-alist
377 `(,tramp-local-host-regexp "\\`root\\'" "su"))
380 (add-to-list 'tramp-default-user-alist
381 `(,(concat "\\`" (regexp-opt '("su" "sudo" "ksu")) "\\'")
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.
386 (add-to-list 'tramp-default-user-alist
389 (regexp-opt '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
391 nil ,(user-login-name)))
394 (defconst tramp-completion-function-alist-rsh
395 '((tramp-parse-rhosts "/etc/hosts.equiv")
396 (tramp-parse-rhosts "~/.rhosts"))
397 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
400 (defconst tramp-completion-function-alist-ssh
401 '((tramp-parse-rhosts "/etc/hosts.equiv")
402 (tramp-parse-rhosts "/etc/shosts.equiv")
403 (tramp-parse-shosts "/etc/ssh_known_hosts")
404 (tramp-parse-sconfig "/etc/ssh_config")
405 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
406 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
407 (tramp-parse-rhosts "~/.rhosts")
408 (tramp-parse-rhosts "~/.shosts")
409 (tramp-parse-shosts "~/.ssh/known_hosts")
410 (tramp-parse-sconfig "~/.ssh/config")
411 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
412 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
413 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
416 (defconst tramp-completion-function-alist-telnet
417 '((tramp-parse-hosts "/etc/hosts"))
418 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
421 (defconst tramp-completion-function-alist-su
422 '((tramp-parse-passwd "/etc/passwd"))
423 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
426 (defconst tramp-completion-function-alist-putty
428 ,(if (memq system-type '(windows-nt))
429 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"
430 "~/.putty/sessions")))
431 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty sessions.")
434 (eval-after-load 'tramp
436 (tramp-set-completion-function "rcp" tramp-completion-function-alist-rsh)
437 (tramp-set-completion-function "remcp" tramp-completion-function-alist-rsh)
438 (tramp-set-completion-function "scp" tramp-completion-function-alist-ssh)
439 (tramp-set-completion-function "scp1" tramp-completion-function-alist-ssh)
440 (tramp-set-completion-function "scp2" tramp-completion-function-alist-ssh)
441 (tramp-set-completion-function "scpx" tramp-completion-function-alist-ssh)
442 (tramp-set-completion-function "sftp" tramp-completion-function-alist-ssh)
443 (tramp-set-completion-function "rsync" tramp-completion-function-alist-ssh)
444 (tramp-set-completion-function "rsh" tramp-completion-function-alist-rsh)
445 (tramp-set-completion-function "remsh" tramp-completion-function-alist-rsh)
446 (tramp-set-completion-function "ssh" tramp-completion-function-alist-ssh)
447 (tramp-set-completion-function "ssh1" tramp-completion-function-alist-ssh)
448 (tramp-set-completion-function "ssh2" tramp-completion-function-alist-ssh)
449 (tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh)
450 (tramp-set-completion-function
451 "telnet" tramp-completion-function-alist-telnet)
452 (tramp-set-completion-function "su" tramp-completion-function-alist-su)
453 (tramp-set-completion-function "sudo" tramp-completion-function-alist-su)
454 (tramp-set-completion-function "ksu" tramp-completion-function-alist-su)
455 (tramp-set-completion-function
456 "krlogin" tramp-completion-function-alist-rsh)
457 (tramp-set-completion-function "plink" tramp-completion-function-alist-ssh)
458 (tramp-set-completion-function
459 "plink1" tramp-completion-function-alist-ssh)
460 (tramp-set-completion-function
461 "plinkx" tramp-completion-function-alist-putty)
462 (tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh)
463 (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh)))
465 ;; "getconf PATH" yields:
466 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
467 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
468 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
469 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
472 (defcustom tramp-remote-path
473 '(tramp-default-remote-path "/bin" "/usr/bin" "/sbin" "/usr/sbin"
474 "/usr/local/bin" "/usr/local/sbin" "/local/bin" "/local/freeware/bin"
475 "/local/gnu/bin" "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin"
476 "/opt/bin" "/opt/sbin" "/opt/local/bin")
477 "List of directories to search for executables on remote host.
478 For every remote host, this variable will be set buffer local,
479 keeping the list of existing directories on that host.
481 You can use `~' in this list, but when searching for a shell which groks
482 tilde expansion, all directory names starting with `~' will be ignored.
484 `Default Directories' represent the list of directories given by
485 the command \"getconf PATH\". It is recommended to use this
486 entry on top of this list, because these are the default
487 directories for POSIX compatible commands. On remote hosts which
488 do not offer the getconf command (like cygwin), the value
489 \"/bin:/usr/bin\" is used instead of.
491 `Private Directories' are the settings of the $PATH environment,
492 as given in your `~/.profile'."
494 :type '(repeat (choice
495 (const :tag "Default Directories" tramp-default-remote-path)
496 (const :tag "Private Directories" tramp-own-remote-path)
497 (string :tag "Directory"))))
500 (defcustom tramp-remote-process-environment
501 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "LC_ALL=C"
502 ,(format "TERM=%s" tramp-terminal-type)
503 "EMACS=t" ;; Deprecated.
504 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version)
505 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH=" "PAGER=\"\""
506 "autocorrect=" "correct=")
507 "List of environment variables to be set on the remote host.
509 Each element should be a string of the form ENVVARNAME=VALUE. An
510 entry ENVVARNAME= disables the corresponding environment variable,
511 which might have been set in the init files like ~/.profile.
513 Special handling is applied to the PATH environment, which should
514 not be set here. Instead, it should be set via `tramp-remote-path'."
516 :type '(repeat string))
518 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
519 "Alist specifying extra arguments to pass to the remote shell.
520 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
521 matching the shell file name and ARGS is a string specifying the
524 This variable is only used when Tramp needs to start up another shell
525 for tilde expansion. The extra arguments should typically prevent the
526 shell from reading its init file."
528 ;; This might be the wrong way to test whether the widget type
529 ;; `alist' is available. Who knows the right way to test it?
530 :type (if (get 'alist 'widget-type)
531 '(alist :key-type string :value-type string)
532 '(repeat (cons string string))))
534 (defconst tramp-actions-before-shell
535 '((tramp-login-prompt-regexp tramp-action-login)
536 (tramp-password-prompt-regexp tramp-action-password)
537 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
538 (shell-prompt-pattern tramp-action-succeed)
539 (tramp-shell-prompt-pattern tramp-action-succeed)
540 (tramp-yesno-prompt-regexp tramp-action-yesno)
541 (tramp-yn-prompt-regexp tramp-action-yn)
542 (tramp-terminal-prompt-regexp tramp-action-terminal)
543 (tramp-process-alive-regexp tramp-action-process-alive))
544 "List of pattern/action pairs.
545 Whenever a pattern matches, the corresponding action is performed.
546 Each item looks like (PATTERN ACTION).
548 The PATTERN should be a symbol, a variable. The value of this
549 variable gives the regular expression to search for. Note that the
550 regexp must match at the end of the buffer, \"\\'\" is implicitly
553 The ACTION should also be a symbol, but a function. When the
554 corresponding PATTERN matches, the ACTION function is called.")
556 (defconst tramp-actions-copy-out-of-band
557 '((tramp-password-prompt-regexp tramp-action-password)
558 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
559 (tramp-copy-failed-regexp tramp-action-permission-denied)
560 (tramp-process-alive-regexp tramp-action-out-of-band))
561 "List of pattern/action pairs.
562 This list is used for copying/renaming with out-of-band methods.
564 See `tramp-actions-before-shell' for more info.")
566 (defconst tramp-uudecode
567 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
570 "Shell function to implement `uudecode' to standard output.
571 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
572 for this or `uudecode -p', but some systems don't, and for them
573 we have this shell function.")
575 (defconst tramp-perl-file-truename
578 use Cwd \"realpath\";
581 my ($volume, @dirs) = @_;
582 my $real = realpath(File::Spec->catpath(
583 $volume, File::Spec->catdir(@dirs), \"\"));
585 my ($vol, $dir) = File::Spec->splitpath($real, 1);
586 return ($vol, File::Spec->splitdir($dir));
589 my $last = pop(@dirs);
590 ($volume, @dirs) = recursive($volume, @dirs);
592 return ($volume, @dirs);
596 $result = realpath($ARGV[0]);
598 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
599 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
601 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
604 if ($ARGV[0] =~ /\\/$/) {
605 $result = $result . \"/\";
608 print \"\\\"$result\\\"\\n\";
609 ' \"$1\" 2>/dev/null"
610 "Perl script to produce output suitable for use with `file-truename'
611 on the remote file system.
612 Escape sequence %s is replaced with name of Perl binary.
613 This string is passed to `format', so percent characters need to be doubled.")
615 (defconst tramp-perl-file-name-all-completions
625 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
626 @files = readdir(d); closedir(d);
627 foreach $f (@files) {
628 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
629 if (-d \"$ARGV[0]/$f\") {
638 ' \"$1\" \"$2\" \"$3\" 2>/dev/null"
639 "Perl script to produce output suitable for use with
640 `file-name-all-completions' on the remote file system. Escape
641 sequence %s is replaced with name of Perl binary. This string is
642 passed to `format', so percent characters need to be doubled.")
644 ;; Perl script to implement `file-attributes' in a Lisp `read'able
645 ;; output. If you are hacking on this, note that you get *no* output
646 ;; unless this spits out a complete line, including the '\n' at the
648 ;; The device number is returned as "-1", because there will be a virtual
649 ;; device number set in `tramp-sh-handle-file-attributes'.
650 (defconst tramp-perl-file-attributes
652 @stat = lstat($ARGV[0]);
657 if (($stat[2] & 0170000) == 0120000)
659 $type = readlink($ARGV[0]);
660 $type = \"\\\"$type\\\"\";
662 elsif (($stat[2] & 0170000) == 040000)
670 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
671 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
673 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
678 $stat[8] >> 16 & 0xffff,
680 $stat[9] >> 16 & 0xffff,
682 $stat[10] >> 16 & 0xffff,
686 $stat[1] >> 16 & 0xffff,
688 );' \"$1\" \"$2\" 2>/dev/null"
689 "Perl script to produce output suitable for use with `file-attributes'
690 on the remote file system.
691 Escape sequence %s is replaced with name of Perl binary.
692 This string is passed to `format', so percent characters need to be doubled.")
694 (defconst tramp-perl-directory-files-and-attributes
696 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
697 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
698 @list = readdir(DIR);
702 for($i = 0; $i < $n; $i++)
704 $filename = $list[$i];
705 @stat = lstat($filename);
706 if (($stat[2] & 0170000) == 0120000)
708 $type = readlink($filename);
709 $type = \"\\\"$type\\\"\";
711 elsif (($stat[2] & 0170000) == 040000)
719 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
720 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
722 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
728 $stat[8] >> 16 & 0xffff,
730 $stat[9] >> 16 & 0xffff,
732 $stat[10] >> 16 & 0xffff,
736 $stat[1] >> 16 & 0xffff,
738 $stat[0] >> 16 & 0xffff,
741 printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
742 "Perl script implementing `directory-files-attributes' as Lisp `read'able
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
766 # This script contributed by Juanma Barranquero <lektu@terra.es>.
767 # Copyright (C) 2002-2013 Free Software Foundation, Inc.
772 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
773 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
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) {
782 # Only for the last chunk, and only if did not fill the last three-byte packet
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
794 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
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
804 # This script contributed by Juanma Barranquero <lektu@terra.es>.
805 # Copyright (C) 2002-2013 Free Software Foundation, Inc.
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;
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)
822 while (my $data = <STDIN>) {
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/;
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.
838 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
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-vc-registered-read-file-names
849 if %s \"$file\"; then
850 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
852 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
854 if %s \"$file\"; then
855 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
857 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
861 "Script to check existence of VC related files.
862 It must be send formatted with two strings; the tests for file
863 existence, and file readability. Input shall be read via
864 here-document, otherwise the command could exceed maximum length
867 (defconst tramp-file-mode-type-map
868 '((0 . "-") ; Normal file (SVID-v2 and XPG2)
870 (2 . "c") ; character device
871 (3 . "m") ; multiplexed character device (v7)
872 (4 . "d") ; directory
873 (5 . "?") ; Named special file (XENIX)
874 (6 . "b") ; block device
875 (7 . "?") ; multiplexed block device (v7)
876 (8 . "-") ; regular file
877 (9 . "n") ; network special file (HP-UX)
879 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
881 (13 . "D") ; door special (Solaris)
882 (14 . "w")) ; whiteout (BSD)
883 "A list of file types returned from the `stat' system call.
884 This is used to map a mode number to a permission string.")
886 ;; New handlers should be added here. The following operations can be
887 ;; handled using the normal primitives: file-name-sans-versions,
889 (defconst tramp-sh-file-name-handler-alist
890 '((load . tramp-handle-load)
891 (make-symbolic-link . tramp-sh-handle-make-symbolic-link)
892 (file-name-as-directory . tramp-handle-file-name-as-directory)
893 (file-name-directory . tramp-handle-file-name-directory)
894 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
895 (file-truename . tramp-sh-handle-file-truename)
896 (file-exists-p . tramp-sh-handle-file-exists-p)
897 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
898 (file-directory-p . tramp-sh-handle-file-directory-p)
899 (file-executable-p . tramp-sh-handle-file-executable-p)
900 (file-readable-p . tramp-sh-handle-file-readable-p)
901 (file-regular-p . tramp-handle-file-regular-p)
902 (file-symlink-p . tramp-handle-file-symlink-p)
903 (file-writable-p . tramp-sh-handle-file-writable-p)
904 (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p)
905 (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p)
906 (file-attributes . tramp-sh-handle-file-attributes)
907 (file-modes . tramp-handle-file-modes)
908 (directory-files . tramp-handle-directory-files)
909 (directory-files-and-attributes
910 . tramp-sh-handle-directory-files-and-attributes)
911 (file-name-all-completions . tramp-sh-handle-file-name-all-completions)
912 (file-name-completion . tramp-handle-file-name-completion)
913 (add-name-to-file . tramp-sh-handle-add-name-to-file)
914 (copy-file . tramp-sh-handle-copy-file)
915 (copy-directory . tramp-sh-handle-copy-directory)
916 (rename-file . tramp-sh-handle-rename-file)
917 (set-file-modes . tramp-sh-handle-set-file-modes)
918 (set-file-times . tramp-sh-handle-set-file-times)
919 (make-directory . tramp-sh-handle-make-directory)
920 (delete-directory . tramp-sh-handle-delete-directory)
921 (delete-file . tramp-sh-handle-delete-file)
922 (directory-file-name . tramp-handle-directory-file-name)
923 ;; `executable-find' is not official yet.
924 (executable-find . tramp-sh-handle-executable-find)
925 (start-file-process . tramp-sh-handle-start-file-process)
926 (process-file . tramp-sh-handle-process-file)
927 (shell-command . tramp-handle-shell-command)
928 (insert-directory . tramp-sh-handle-insert-directory)
929 (expand-file-name . tramp-sh-handle-expand-file-name)
930 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
931 (file-local-copy . tramp-sh-handle-file-local-copy)
932 (file-remote-p . tramp-handle-file-remote-p)
933 (insert-file-contents . tramp-handle-insert-file-contents)
934 (insert-file-contents-literally
935 . tramp-sh-handle-insert-file-contents-literally)
936 (write-region . tramp-sh-handle-write-region)
937 (find-backup-file-name . tramp-handle-find-backup-file-name)
938 (make-auto-save-file-name . tramp-sh-handle-make-auto-save-file-name)
939 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
940 (dired-compress-file . tramp-sh-handle-dired-compress-file)
941 (dired-recursive-delete-directory
942 . tramp-sh-handle-dired-recursive-delete-directory)
943 (dired-uncache . tramp-handle-dired-uncache)
944 (set-visited-file-modtime . tramp-sh-handle-set-visited-file-modtime)
945 (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime)
946 (file-selinux-context . tramp-sh-handle-file-selinux-context)
947 (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context)
948 (file-acl . tramp-sh-handle-file-acl)
949 (set-file-acl . tramp-sh-handle-set-file-acl)
950 (vc-registered . tramp-sh-handle-vc-registered))
951 "Alist of handler functions.
952 Operations not mentioned here will be handled by the normal Emacs functions.")
954 ;; This must be the last entry, because `identity' always matches.
956 (add-to-list 'tramp-foreign-file-name-handler-alist
957 '(identity . tramp-sh-file-name-handler) 'append)
959 ;;; File Name Handler Functions:
961 (defun tramp-sh-handle-make-symbolic-link
962 (filename linkname &optional ok-if-already-exists)
963 "Like `make-symbolic-link' for Tramp files.
964 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
965 the symlink. If LINKNAME is a Tramp file, only the localname component is
966 used as the target of the symlink.
968 If LINKNAME is a Tramp file and the localname component is relative, then
969 it is expanded first, before the localname component is taken. Note that
970 this can give surprising results if the user/host for the source and
971 target of the symlink differ."
972 (with-parsed-tramp-file-name linkname l
973 (let ((ln (tramp-get-remote-ln l))
974 (cwd (tramp-run-real-handler
975 'file-name-directory (list l-localname))))
979 "Making a symbolic link. ln(1) does not exist on the remote host."))
981 ;; Do the 'confirm if exists' thing.
982 (when (file-exists-p linkname)
984 (if (or (null ok-if-already-exists) ; not allowed to exist
985 (and (numberp ok-if-already-exists)
988 "File %s already exists; make it a link anyway? "
991 l 'file-already-exists "File %s already exists" l-localname)
992 (delete-file linkname)))
994 ;; If FILENAME is a Tramp name, use just the localname component.
995 (when (tramp-tramp-file-p filename)
997 (tramp-file-name-localname
998 (tramp-dissect-file-name (expand-file-name filename)))))
1000 (tramp-flush-file-property l (file-name-directory l-localname))
1001 (tramp-flush-file-property l l-localname)
1003 ;; Right, they are on the same host, regardless of user, method,
1004 ;; etc. We now make the link on the remote machine. This will
1005 ;; occur as the user that FILENAME belongs to.
1006 (tramp-send-command-and-check
1009 "cd %s && %s -sf %s %s"
1010 (tramp-shell-quote-argument cwd)
1012 (tramp-shell-quote-argument filename)
1013 (tramp-shell-quote-argument l-localname))
1016 (defun tramp-sh-handle-file-truename (filename &optional counter prev-dirs)
1017 "Like `file-truename' for Tramp files."
1018 (with-parsed-tramp-file-name (expand-file-name filename) nil
1019 (tramp-make-tramp-file-name method user host
1020 (with-tramp-file-property v localname "file-truename"
1021 (let ((result nil)) ; result steps in reverse order
1022 (tramp-message v 4 "Finding true name for `%s'" filename)
1024 ;; Use GNU readlink --canonicalize-missing where available.
1025 ((tramp-get-remote-readlink v)
1027 (tramp-send-command-and-read
1029 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
1030 (tramp-get-remote-readlink v)
1031 (tramp-shell-quote-argument localname)))))
1033 ;; Use Perl implementation.
1034 ((and (tramp-get-remote-perl v)
1035 (tramp-get-connection-property v "perl-file-spec" nil)
1036 (tramp-get-connection-property v "perl-cwd-realpath" nil))
1037 (tramp-maybe-send-script
1038 v tramp-perl-file-truename "tramp_perl_file_truename")
1040 (tramp-send-command-and-read
1042 (format "tramp_perl_file_truename %s"
1043 (tramp-shell-quote-argument localname)))))
1045 ;; Do it yourself. We bind `directory-sep-char' here for
1046 ;; XEmacs on Windows, which would otherwise use backslash.
1047 (t (let* ((directory-sep-char ?/)
1048 (steps (tramp-compat-split-string localname "/"))
1049 (localnamedir (tramp-run-real-handler
1050 'file-name-as-directory (list localname)))
1051 (is-dir (string= localname localnamedir))
1054 ;; Don't make the following value larger than
1055 ;; necessary. People expect an error message in
1056 ;; a timely fashion when something is wrong;
1057 ;; otherwise they might think that Emacs is hung.
1058 ;; Of course, correctness has to come first.
1061 (while (and steps (< numchase numchase-limit))
1062 (setq thisstep (pop steps))
1065 (mapconcat 'identity
1066 (append '("") (reverse result) (list thisstep))
1068 (setq symlink-target
1069 (nth 0 (file-attributes
1070 (tramp-make-tramp-file-name
1072 (mapconcat 'identity
1077 (cond ((string= "." thisstep)
1078 (tramp-message v 5 "Ignoring step `.'"))
1079 ((string= ".." thisstep)
1080 (tramp-message v 5 "Processing step `..'")
1082 ((stringp symlink-target)
1083 ;; It's a symlink, follow it.
1085 v 5 "Follow symlink to %s" symlink-target)
1086 (setq numchase (1+ numchase))
1087 (when (file-name-absolute-p symlink-target)
1089 ;; If the symlink was absolute, we'll get a
1090 ;; string like "/user@host:/some/target";
1091 ;; extract the "/some/target" part from it.
1092 (when (tramp-tramp-file-p symlink-target)
1093 (unless (tramp-equal-remote filename symlink-target)
1096 "Symlink target `%s' on wrong host"
1098 (setq symlink-target localname))
1100 (append (tramp-compat-split-string
1105 (setq result (cons thisstep result)))))
1106 (when (>= numchase numchase-limit)
1109 "Maximum number (%d) of symlinks exceeded" numchase-limit))
1110 (setq result (reverse result))
1111 ;; Combine list to form string.
1114 (mapconcat 'identity (cons "" result) "/")
1117 (or (string= "" result)
1118 (not (string= (substring result -1) "/"))))
1119 (setq result (concat result "/"))))))
1121 (tramp-message v 4 "True name of `%s' is `%s'" localname result)
1126 (defun tramp-sh-handle-file-exists-p (filename)
1127 "Like `file-exists-p' for Tramp files."
1128 (with-parsed-tramp-file-name filename nil
1129 (with-tramp-file-property v localname "file-exists-p"
1130 (or (not (null (tramp-get-file-property
1131 v localname "file-attributes-integer" nil)))
1132 (not (null (tramp-get-file-property
1133 v localname "file-attributes-string" nil)))
1134 (tramp-send-command-and-check
1138 (tramp-get-file-exists-command v)
1139 (tramp-shell-quote-argument localname)))))))
1141 (defun tramp-sh-handle-file-attributes (filename &optional id-format)
1142 "Like `file-attributes' for Tramp files."
1143 (unless id-format (setq id-format 'integer))
1144 ;; Don't modify `last-coding-system-used' by accident.
1145 (let ((last-coding-system-used last-coding-system-used))
1146 (with-parsed-tramp-file-name (expand-file-name filename) nil
1147 (with-tramp-file-property
1148 v localname (format "file-attributes-%s" id-format)
1150 (tramp-convert-file-attributes
1154 ((tramp-get-remote-stat v)
1155 (tramp-do-file-attributes-with-stat v localname id-format))
1156 ((tramp-get-remote-perl v)
1157 (tramp-do-file-attributes-with-perl v localname id-format))
1159 ;; The scripts could fail, for example with huge file size.
1160 (tramp-do-file-attributes-with-ls v localname id-format))))))))
1162 (defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
1163 "Implement `file-attributes' for Tramp files using the ls(1) command."
1165 res-inode res-filemodes res-numlinks
1166 res-uid res-gid res-size res-symlink-target)
1167 (tramp-message vec 5 "file attributes with ls: %s" localname)
1170 (format "(%s %s || %s -h %s) && %s %s %s"
1171 (tramp-get-file-exists-command vec)
1172 (tramp-shell-quote-argument localname)
1173 (tramp-get-test-command vec)
1174 (tramp-shell-quote-argument localname)
1175 (tramp-get-ls-command vec)
1176 (if (eq id-format 'integer) "-ildn" "-ild")
1177 (tramp-shell-quote-argument localname)))
1178 ;; parse `ls -l' output ...
1179 (with-current-buffer (tramp-get-buffer vec)
1180 (when (> (buffer-size) 0)
1181 (goto-char (point-min))
1185 (read (current-buffer))
1186 (invalid-read-syntax
1187 (when (and (equal (cadr err)
1188 "Integer constant overflow in reader")
1190 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1192 (let* ((big (read (substring (car (cddr err)) 0
1193 (match-beginning 1))))
1194 (small (read (match-string 1 (car (cddr err)))))
1195 (twiddle (/ small 65536)))
1196 (cons (+ big twiddle)
1197 (- small (* twiddle 65536))))))))
1198 ;; ... file mode flags
1199 (setq res-filemodes (symbol-name (read (current-buffer))))
1201 (setq res-numlinks (read (current-buffer)))
1203 (setq res-uid (read (current-buffer)))
1204 (setq res-gid (read (current-buffer)))
1205 (if (eq id-format 'integer)
1207 (unless (numberp res-uid) (setq res-uid -1))
1208 (unless (numberp res-gid) (setq res-gid -1)))
1210 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
1211 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
1213 (setq res-size (read (current-buffer)))
1214 ;; From the file modes, figure out other stuff.
1215 (setq symlinkp (eq ?l (aref res-filemodes 0)))
1216 (setq dirp (eq ?d (aref res-filemodes 0)))
1217 ;; if symlink, find out file name pointed to
1219 (search-forward "-> ")
1220 (setq res-symlink-target (buffer-substring (point) (point-at-eol))))
1221 ;; return data gathered
1223 ;; 0. t for directory, string (name linked to) for symbolic
1225 (or dirp res-symlink-target)
1226 ;; 1. Number of links to file.
1232 ;; 4. Last access time, as a list of integers. Normally this
1233 ;; would be in the same format as `current-time', but the
1234 ;; subseconds part is not currently implemented, and (0 0)
1235 ;; denotes an unknown time.
1236 ;; 5. Last modification time, likewise.
1237 ;; 6. Last status change time, likewise.
1238 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1239 ;; 7. Size in bytes (-1, if number is out of range).
1241 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1243 ;; 9. t if file's gid would change if file were deleted and
1244 ;; recreated. Will be set in `tramp-convert-file-attributes'
1246 ;; 10. inode number.
1248 ;; 11. Device number. Will be replaced by a virtual device number.
1252 (defun tramp-do-file-attributes-with-perl
1253 (vec localname &optional id-format)
1254 "Implement `file-attributes' for Tramp files using a Perl script."
1255 (tramp-message vec 5 "file attributes with perl: %s" localname)
1256 (tramp-maybe-send-script
1257 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
1258 (tramp-send-command-and-read
1260 (format "tramp_perl_file_attributes %s %s"
1261 (tramp-shell-quote-argument localname) id-format)))
1263 (defun tramp-do-file-attributes-with-stat
1264 (vec localname &optional id-format)
1265 "Implement `file-attributes' for Tramp files using stat(1) command."
1266 (tramp-message vec 5 "file attributes with stat: %s" localname)
1267 (tramp-send-command-and-read
1270 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
1271 ;; parse correctly the sequence "((". Therefore, we add a space.
1272 "( (%s %s || %s -h %s) && %s -c '((\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)' %s || echo nil)"
1273 (tramp-get-file-exists-command vec)
1274 (tramp-shell-quote-argument localname)
1275 (tramp-get-test-command vec)
1276 (tramp-shell-quote-argument localname)
1277 (tramp-get-remote-stat vec)
1278 (if (eq id-format 'integer) "%ue0" "\"%U\"")
1279 (if (eq id-format 'integer) "%ge0" "\"%G\"")
1280 (tramp-shell-quote-argument localname))))
1282 (defun tramp-sh-handle-set-visited-file-modtime (&optional time-list)
1283 "Like `set-visited-file-modtime' for Tramp files."
1284 (unless (buffer-file-name)
1285 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1288 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
1289 (let ((f (buffer-file-name))
1291 (with-parsed-tramp-file-name f nil
1292 (let* ((remote-file-name-inhibit-cache t)
1293 (attr (file-attributes f))
1294 ;; '(-1 65535) means file doesn't exists yet.
1295 (modtime (or (nth 5 attr) '(-1 65535))))
1296 (when (boundp 'last-coding-system-used)
1297 (setq coding-system-used (symbol-value 'last-coding-system-used)))
1298 ;; We use '(0 0) as a don't-know value. See also
1299 ;; `tramp-do-file-attributes-with-ls'.
1300 (if (not (equal modtime '(0 0)))
1301 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
1305 (format "%s -ild %s"
1306 (tramp-get-ls-command v)
1307 (tramp-shell-quote-argument localname)))
1308 (setq attr (buffer-substring (point)
1309 (progn (end-of-line) (point)))))
1310 (tramp-set-file-property
1311 v localname "visited-file-modtime-ild" attr))
1312 (when (boundp 'last-coding-system-used)
1313 (set 'last-coding-system-used coding-system-used))
1316 ;; This function makes the same assumption as
1317 ;; `tramp-sh-handle-set-visited-file-modtime'.
1318 (defun tramp-sh-handle-verify-visited-file-modtime (buf)
1319 "Like `verify-visited-file-modtime' for Tramp files.
1320 At the time `verify-visited-file-modtime' calls this function, we
1321 already know that the buffer is visiting a file and that
1322 `visited-file-modtime' does not return 0. Do not call this
1323 function directly, unless those two cases are already taken care
1325 (with-current-buffer buf
1326 (let ((f (buffer-file-name)))
1327 ;; There is no file visiting the buffer, or the buffer has no
1328 ;; recorded last modification time, or there is no established
1331 (eq (visited-file-modtime) 0)
1332 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
1334 (with-parsed-tramp-file-name f nil
1335 (let* ((remote-file-name-inhibit-cache t)
1336 (attr (file-attributes f))
1337 (modtime (nth 5 attr))
1338 (mt (visited-file-modtime)))
1341 ;; File exists, and has a known modtime.
1342 ((and attr (not (equal modtime '(0 0))))
1343 (< (abs (tramp-time-diff
1345 ;; For compatibility, deal with both the old
1346 ;; (HIGH . LOW) and the new (HIGH LOW) return
1347 ;; values of `visited-file-modtime'.
1349 (list (car mt) (cdr mt))
1352 ;; Modtime has the don't know value.
1356 (format "%s -ild %s"
1357 (tramp-get-ls-command v)
1358 (tramp-shell-quote-argument localname)))
1359 (with-current-buffer (tramp-get-buffer v)
1360 (setq attr (buffer-substring
1361 (point) (progn (end-of-line) (point)))))
1364 (tramp-get-file-property
1365 v localname "visited-file-modtime-ild" "")))
1366 ;; If file does not exist, say it is not modified if and
1367 ;; only if that agrees with the buffer's record.
1368 (t (equal mt '(-1 65535))))))))))
1370 (defun tramp-sh-handle-set-file-modes (filename mode)
1371 "Like `set-file-modes' for Tramp files."
1372 (with-parsed-tramp-file-name filename nil
1373 (tramp-flush-file-property v localname)
1374 ;; FIXME: extract the proper text from chmod's stderr.
1375 (tramp-barf-unless-okay
1377 (format "chmod %s %s"
1378 (tramp-compat-decimal-to-octal mode)
1379 (tramp-shell-quote-argument localname))
1380 "Error while changing file's mode %s" filename)))
1382 (defun tramp-sh-handle-set-file-times (filename &optional time)
1383 "Like `set-file-times' for Tramp files."
1384 (if (file-remote-p filename)
1385 (with-parsed-tramp-file-name filename nil
1386 (tramp-flush-file-property v localname)
1387 (let ((time (if (or (null time) (equal time '(0 0)))
1390 ;; With GNU Emacs, `format-time-string' has an optional
1391 ;; parameter UNIVERSAL. This is preferred, because we
1392 ;; could handle the case when the remote host is located
1393 ;; in a different time zone as the local host.
1394 (utc (not (featurep 'xemacs))))
1395 (tramp-send-command-and-check
1396 v (format "%s touch -t %s %s"
1397 (if utc "env TZ=UTC" "")
1399 (format-time-string "%Y%m%d%H%M.%S" time t)
1400 (format-time-string "%Y%m%d%H%M.%S" time))
1401 (tramp-shell-quote-argument localname)))))
1403 ;; We handle also the local part, because in older Emacsen,
1404 ;; without `set-file-times', this function is an alias for this.
1405 ;; We are local, so we don't need the UTC settings.
1407 (tramp-compat-call-process
1408 "touch" nil nil nil "-t"
1409 (format-time-string "%Y%m%d%H%M.%S" time)
1410 (tramp-shell-quote-argument filename)))))
1412 (defun tramp-set-file-uid-gid (filename &optional uid gid)
1413 "Set the ownership for FILENAME.
1414 If UID and GID are provided, these values are used; otherwise uid
1415 and gid of the corresponding user is taken. Both parameters must
1416 be non-negative integers."
1417 ;; Modern Unices allow chown only for root. So we might need
1418 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1419 ;; working with su(do)? when it is needed, so it shall succeed in
1420 ;; the majority of cases.
1421 ;; Don't modify `last-coding-system-used' by accident.
1422 (let ((last-coding-system-used last-coding-system-used))
1423 (if (file-remote-p filename)
1424 (with-parsed-tramp-file-name filename nil
1425 (if (and (zerop (user-uid)) (tramp-local-host-p v))
1426 ;; If we are root on the local host, we can do it directly.
1427 (tramp-set-file-uid-gid localname uid gid)
1428 (let ((uid (or (and (natnump uid) uid)
1429 (tramp-get-remote-uid v 'integer)))
1430 (gid (or (and (natnump gid) gid)
1431 (tramp-get-remote-gid v 'integer))))
1434 "chown %d:%d %s" uid gid
1435 (tramp-shell-quote-argument localname))))))
1437 ;; We handle also the local part, because there doesn't exist
1438 ;; `set-file-uid-gid'. On W32 "chown" might not work.
1439 (let ((uid (or (and (natnump uid) uid) (tramp-get-local-uid 'integer)))
1440 (gid (or (and (natnump gid) gid) (tramp-get-local-gid 'integer))))
1441 (tramp-compat-call-process
1443 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
1445 (defun tramp-remote-selinux-p (vec)
1446 "Check, whether SELINUX is enabled on the remote host."
1447 (with-tramp-connection-property
1448 (tramp-get-connection-process vec) "selinux-p"
1449 (let ((result (tramp-find-executable
1450 vec "getenforce" (tramp-get-remote-path vec) t t)))
1453 (tramp-send-command-and-read
1454 vec (format "echo \\\"`%S`\\\"" result))
1457 (defun tramp-sh-handle-file-selinux-context (filename)
1458 "Like `file-selinux-context' for Tramp files."
1459 (with-parsed-tramp-file-name filename nil
1460 (with-tramp-file-property v localname "file-selinux-context"
1461 (let ((context '(nil nil nil nil))
1462 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1463 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1464 (when (and (tramp-remote-selinux-p v)
1465 (tramp-send-command-and-check
1468 (tramp-get-ls-command v)
1469 (tramp-shell-quote-argument localname))))
1470 (with-current-buffer (tramp-get-connection-buffer v)
1471 (goto-char (point-min))
1472 (when (re-search-forward regexp (point-at-eol) t)
1473 (setq context (list (match-string 1) (match-string 2)
1474 (match-string 3) (match-string 4))))))
1475 ;; Return the context.
1478 (defun tramp-sh-handle-set-file-selinux-context (filename context)
1479 "Like `set-file-selinux-context' for Tramp files."
1480 (with-parsed-tramp-file-name filename nil
1481 (if (and (consp context)
1482 (tramp-remote-selinux-p v)
1483 (tramp-send-command-and-check
1484 v (format "chcon %s %s %s %s %s"
1485 (if (stringp (nth 0 context))
1486 (format "--user=%s" (nth 0 context)) "")
1487 (if (stringp (nth 1 context))
1488 (format "--role=%s" (nth 1 context)) "")
1489 (if (stringp (nth 2 context))
1490 (format "--type=%s" (nth 2 context)) "")
1491 (if (stringp (nth 3 context))
1492 (format "--range=%s" (nth 3 context)) "")
1493 (tramp-shell-quote-argument localname))))
1495 (tramp-set-file-property v localname "file-selinux-context" context)
1497 (tramp-set-file-property v localname "file-selinux-context" 'undef)
1500 (defun tramp-remote-acl-p (vec)
1501 "Check, whether ACL is enabled on the remote host."
1502 (with-tramp-connection-property (tramp-get-connection-process vec) "acl-p"
1503 (tramp-send-command-and-check vec "getfacl /")))
1505 (defun tramp-sh-handle-file-acl (filename)
1506 "Like `file-acl' for Tramp files."
1507 (with-parsed-tramp-file-name filename nil
1508 (with-tramp-file-property v localname "file-acl"
1509 (when (and (tramp-remote-acl-p v)
1510 (tramp-send-command-and-check
1512 "getfacl -ac %s 2>/dev/null"
1513 (tramp-shell-quote-argument localname))))
1514 (with-current-buffer (tramp-get-connection-buffer v)
1515 (goto-char (point-max))
1516 (delete-blank-lines)
1517 (when (> (point-max) (point-min))
1518 (tramp-compat-funcall
1519 'substring-no-properties (buffer-string))))))))
1521 (defun tramp-sh-handle-set-file-acl (filename acl-string)
1522 "Like `set-file-acl' for Tramp files."
1523 (with-parsed-tramp-file-name (expand-file-name filename) nil
1524 (if (and (stringp acl-string) (tramp-remote-acl-p v)
1527 v (format "setfacl --set-file=- %s <<'EOF'\n%s\nEOF\n"
1528 (tramp-shell-quote-argument localname) acl-string))
1529 (tramp-send-command-and-check v nil)))
1532 (tramp-set-file-property v localname "file-acl" acl-string)
1534 ;; In case of errors, we return `nil'.
1535 (tramp-set-file-property v localname "file-acl-string" 'undef)
1538 ;; Simple functions using the `test' command.
1540 (defun tramp-sh-handle-file-executable-p (filename)
1541 "Like `file-executable-p' for Tramp files."
1542 (with-parsed-tramp-file-name filename nil
1543 (with-tramp-file-property v localname "file-executable-p"
1544 ;; Examine `file-attributes' cache to see if request can be
1545 ;; satisfied without remote operation.
1546 (or (tramp-check-cached-permissions v ?x)
1547 (tramp-run-test "-x" filename)))))
1549 (defun tramp-sh-handle-file-readable-p (filename)
1550 "Like `file-readable-p' for Tramp files."
1551 (with-parsed-tramp-file-name filename nil
1552 (with-tramp-file-property v localname "file-readable-p"
1553 ;; Examine `file-attributes' cache to see if request can be
1554 ;; satisfied without remote operation.
1555 (or (tramp-check-cached-permissions v ?r)
1556 (tramp-run-test "-r" filename)))))
1558 ;; When the remote shell is started, it looks for a shell which groks
1559 ;; tilde expansion. Here, we assume that all shells which grok tilde
1560 ;; expansion will also provide a `test' command which groks `-nt' (for
1561 ;; newer than). If this breaks, tell me about it and I'll try to do
1562 ;; something smarter about it.
1563 (defun tramp-sh-handle-file-newer-than-file-p (file1 file2)
1564 "Like `file-newer-than-file-p' for Tramp files."
1565 (cond ((not (file-exists-p file1))
1567 ((not (file-exists-p file2))
1569 ;; We are sure both files exist at this point.
1572 ;; We try to get the mtime of both files. If they are not
1573 ;; equal to the "dont-know" value, then we subtract the times
1574 ;; and obtain the result.
1575 (let ((fa1 (file-attributes file1))
1576 (fa2 (file-attributes file2)))
1577 (if (and (not (equal (nth 5 fa1) '(0 0)))
1578 (not (equal (nth 5 fa2) '(0 0))))
1579 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
1580 ;; If one of them is the dont-know value, then we can
1581 ;; still try to run a shell command on the remote host.
1582 ;; However, this only works if both files are Tramp
1583 ;; files and both have the same method, same user, same
1585 (unless (tramp-equal-remote file1 file2)
1586 (with-parsed-tramp-file-name
1587 (if (tramp-tramp-file-p file1) file1 file2) nil
1590 "Files %s and %s must have same method, user, host"
1592 (with-parsed-tramp-file-name file1 nil
1594 (tramp-get-test-nt-command v) file1 file2))))))))
1596 ;; Functions implemented using the basic functions above.
1598 (defun tramp-sh-handle-file-directory-p (filename)
1599 "Like `file-directory-p' for Tramp files."
1600 (with-parsed-tramp-file-name filename nil
1601 ;; `file-directory-p' is used as predicate for filename completion.
1602 ;; Sometimes, when a connection is not established yet, it is
1603 ;; desirable to return t immediately for "/method:foo:". It can
1604 ;; be expected that this is always a directory.
1605 (or (zerop (length localname))
1606 (with-tramp-file-property v localname "file-directory-p"
1607 (tramp-run-test "-d" filename)))))
1609 (defun tramp-sh-handle-file-writable-p (filename)
1610 "Like `file-writable-p' for Tramp files."
1611 (with-parsed-tramp-file-name filename nil
1612 (with-tramp-file-property v localname "file-writable-p"
1613 (if (file-exists-p filename)
1614 ;; Examine `file-attributes' cache to see if request can be
1615 ;; satisfied without remote operation.
1616 (or (tramp-check-cached-permissions v ?w)
1617 (tramp-run-test "-w" filename))
1618 ;; If file doesn't exist, check if directory is writable.
1619 (and (tramp-run-test "-d" (file-name-directory filename))
1620 (tramp-run-test "-w" (file-name-directory filename)))))))
1622 (defun tramp-sh-handle-file-ownership-preserved-p (filename &optional group)
1623 "Like `file-ownership-preserved-p' for Tramp files."
1624 (with-parsed-tramp-file-name filename nil
1625 (with-tramp-file-property v localname "file-ownership-preserved-p"
1626 (let ((attributes (file-attributes filename)))
1627 ;; Return t if the file doesn't exist, since it's true that no
1628 ;; information would be lost by an (attempted) delete and create.
1629 (or (null attributes)
1631 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer))
1633 (= (nth 3 attributes) (tramp-get-remote-gid v 'integer)))))))))
1635 ;; Directory listings.
1637 (defun tramp-sh-handle-directory-files-and-attributes
1638 (directory &optional full match nosort id-format)
1639 "Like `directory-files-and-attributes' for Tramp files."
1640 (unless id-format (setq id-format 'integer))
1641 (when (file-directory-p directory)
1642 (setq directory (expand-file-name directory))
1645 (with-parsed-tramp-file-name directory nil
1646 (with-tramp-file-property
1648 (format "directory-files-and-attributes-%s" id-format)
1653 (tramp-convert-file-attributes v (cdr x))))
1655 ((tramp-get-remote-stat v)
1656 (tramp-do-directory-files-and-attributes-with-stat
1657 v localname id-format))
1658 ((tramp-get-remote-perl v)
1659 (tramp-do-directory-files-and-attributes-with-perl
1660 v localname id-format)))))))))
1664 (setq item (pop temp))
1665 (when (or (null match) (string-match match (car item)))
1667 (setcar item (expand-file-name (car item) directory)))
1668 (push item result)))
1672 (sort result (lambda (x y) (string< (car x) (car y))))))))
1674 (defun tramp-do-directory-files-and-attributes-with-perl
1675 (vec localname &optional id-format)
1676 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1677 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
1678 (tramp-maybe-send-script
1679 vec tramp-perl-directory-files-and-attributes
1680 "tramp_perl_directory_files_and_attributes")
1682 (tramp-send-command-and-read
1684 (format "tramp_perl_directory_files_and_attributes %s %s"
1685 (tramp-shell-quote-argument localname) id-format))))
1686 (when (stringp object) (tramp-error vec 'file-error object))
1689 (defun tramp-do-directory-files-and-attributes-with-stat
1690 (vec localname &optional id-format)
1691 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1692 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
1693 (tramp-send-command-and-read
1697 ;; We must care about filenames with spaces, or starting with
1698 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1699 ;; but it does not work on all remote systems. Therefore, we
1700 ;; quote the filenames via sed.
1701 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | "
1703 "'(\"%%n\" (\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)'"
1704 " 2>/dev/null); echo \")\"")
1705 (tramp-shell-quote-argument localname)
1706 (tramp-get-ls-command vec)
1707 (tramp-get-remote-stat vec)
1708 (if (eq id-format 'integer) "%ue0" "\"%U\"")
1709 (if (eq id-format 'integer) "%ge0" "\"%G\""))))
1711 ;; This function should return "foo/" for directories and "bar" for
1713 (defun tramp-sh-handle-file-name-all-completions (filename directory)
1714 "Like `file-name-all-completions' for Tramp files."
1715 (unless (save-match-data (string-match "/" filename))
1716 (with-parsed-tramp-file-name (expand-file-name directory) nil
1723 ;; Try cache entries for filename, filename with last
1724 ;; character removed, filename with last two characters
1725 ;; removed, ..., and finally the empty string - all
1726 ;; concatenated to the local directory name.
1727 (let ((remote-file-name-inhibit-cache
1728 (or remote-file-name-inhibit-cache
1729 tramp-completion-reread-directory-timeout)))
1731 ;; This is inefficient for very long filenames, pity
1732 ;; `reduce' is not available...
1739 (tramp-get-file-property
1741 (concat localname (substring filename 0 x))
1742 "file-name-all-completions"
1744 (when cache-hit (list cache-hit))))
1745 ;; We cannot use a length of 0, because file properties
1746 ;; for "foo" and "foo/" are identical.
1747 (tramp-compat-number-sequence (length filename) 1 -1)))))
1749 ;; Cache expired or no matching cache entry found so we need
1750 ;; to perform a remote operation.
1752 ;; Get a list of directories and files, including reliably
1753 ;; tagging the directories with a trailing '/'. Because I
1754 ;; rock. --daniel@danann.net
1756 ;; Changed to perform `cd' in the same remote op and only
1757 ;; get entries starting with `filename'. Capture any `cd'
1758 ;; error messages. Ensure any `cd' and `echo' aliases are
1762 (if (tramp-get-remote-perl v)
1764 (tramp-maybe-send-script
1765 v tramp-perl-file-name-all-completions
1766 "tramp_perl_file_name_all_completions")
1767 (format "tramp_perl_file_name_all_completions %s %s %d"
1768 (tramp-shell-quote-argument localname)
1769 (tramp-shell-quote-argument filename)
1771 ;; `read-file-name-completion-ignore-case'
1772 ;; is introduced with Emacs 22.1.
1774 'read-file-name-completion-ignore-case)
1775 'read-file-name-completion-ignore-case
1776 'completion-ignore-case))
1780 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
1781 ;; `ls' with wildcard might fail with `Argument
1782 ;; list too long' error in some corner cases; if
1783 ;; `ls' fails after `cd' succeeded, chances are
1784 ;; that's the case, so let's retry without
1785 ;; wildcard. This will return "too many" entries
1786 ;; but that isn't harmful.
1787 " || %s -a 2>/dev/null)"
1788 " | while read f; do"
1789 " if %s -d \"$f\" 2>/dev/null;"
1790 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1791 " && \\echo ok) || \\echo fail")
1792 (tramp-shell-quote-argument localname)
1793 (tramp-get-ls-command v)
1794 ;; When `filename' is empty, just `ls' without
1795 ;; filename argument is more efficient than `ls *'
1796 ;; for very large directories and might avoid the
1797 ;; `Argument list too long' error.
1799 ;; With and only with wildcard, we need to add
1800 ;; `-d' to prevent `ls' from descending into
1802 (if (zerop (length filename))
1804 (concat (tramp-shell-quote-argument filename) "* -d"))
1805 (tramp-get-ls-command v)
1806 (tramp-get-test-command v))))
1808 ;; Now grab the output.
1809 (with-current-buffer (tramp-get-buffer v)
1810 (goto-char (point-max))
1812 ;; Check result code, found in last line of output.
1814 (if (looking-at "^fail$")
1816 ;; Grab error message from line before last line
1817 ;; (it was put there by `cd 2>&1').
1821 "tramp-sh-handle-file-name-all-completions: %s"
1822 (buffer-substring (point) (point-at-eol))))
1823 ;; For peace of mind, if buffer doesn't end in `fail'
1824 ;; then it should end in `ok'. If neither are in the
1825 ;; buffer something went seriously wrong on the remote
1827 (unless (looking-at "^ok$")
1831 tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
1832 (tramp-shell-quote-argument localname) (buffer-string))))
1834 (while (zerop (forward-line -1))
1835 (push (buffer-substring (point) (point-at-eol)) result)))
1837 ;; Because the remote op went through OK we know the
1838 ;; directory we `cd'-ed to exists.
1839 (tramp-set-file-property v localname "file-exists-p" t)
1841 ;; Because the remote op went through OK we know every
1842 ;; file listed by `ls' exists.
1843 (mapc (lambda (entry)
1844 (tramp-set-file-property
1845 v (concat localname entry) "file-exists-p" t))
1848 ;; Store result in the cache.
1849 (tramp-set-file-property
1850 v (concat localname filename)
1851 "file-name-all-completions" result))))))))
1855 (defun tramp-sh-handle-add-name-to-file
1856 (filename newname &optional ok-if-already-exists)
1857 "Like `add-name-to-file' for Tramp files."
1858 (unless (tramp-equal-remote filename newname)
1859 (with-parsed-tramp-file-name
1860 (if (tramp-tramp-file-p filename) filename newname) nil
1863 "add-name-to-file: %s"
1864 "only implemented for same method, same user, same host")))
1865 (with-parsed-tramp-file-name filename v1
1866 (with-parsed-tramp-file-name newname v2
1867 (let ((ln (when v1 (tramp-get-remote-ln v1))))
1868 (when (and (not ok-if-already-exists)
1869 (file-exists-p newname)
1870 (not (numberp ok-if-already-exists))
1873 "File %s already exists; make it a new name anyway? "
1877 "add-name-to-file: file %s already exists" newname))
1878 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1879 (tramp-flush-file-property v2 v2-localname)
1880 (tramp-barf-unless-okay
1882 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
1883 (tramp-shell-quote-argument v2-localname))
1884 "error with add-name-to-file, see buffer `%s' for details"
1887 (defun tramp-sh-handle-copy-file
1888 (filename newname &optional ok-if-already-exists keep-date
1889 preserve-uid-gid preserve-extended-attributes)
1890 "Like `copy-file' for Tramp files."
1891 (setq filename (expand-file-name filename))
1892 (setq newname (expand-file-name newname))
1894 ;; At least one file a Tramp file?
1895 ((or (tramp-tramp-file-p filename)
1896 (tramp-tramp-file-p newname))
1897 (tramp-do-copy-or-rename-file
1898 'copy filename newname ok-if-already-exists keep-date
1899 preserve-uid-gid preserve-extended-attributes))
1901 (preserve-extended-attributes
1902 (tramp-run-real-handler
1904 (list filename newname ok-if-already-exists keep-date
1905 preserve-uid-gid preserve-extended-attributes)))
1907 (tramp-run-real-handler
1909 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
1911 (tramp-run-real-handler
1912 'copy-file (list filename newname ok-if-already-exists keep-date)))))
1914 (defun tramp-sh-handle-copy-directory
1915 (dirname newname &optional keep-date parents copy-contents)
1916 "Like `copy-directory' for Tramp files."
1917 (let ((t1 (tramp-tramp-file-p dirname))
1918 (t2 (tramp-tramp-file-p newname)))
1919 (with-parsed-tramp-file-name (if t1 dirname newname) nil
1920 (if (and (tramp-get-method-parameter method 'tramp-copy-recursive)
1921 ;; When DIRNAME and NEWNAME are remote, they must have
1923 (or (null t1) (null t2)
1925 (tramp-file-name-method (tramp-dissect-file-name dirname))
1926 (tramp-file-name-method (tramp-dissect-file-name newname)))))
1927 ;; scp or rsync DTRT.
1929 (setq dirname (directory-file-name (expand-file-name dirname))
1930 newname (directory-file-name (expand-file-name newname)))
1931 (if (and (file-directory-p newname)
1932 (not (string-equal (file-name-nondirectory dirname)
1933 (file-name-nondirectory newname))))
1936 (file-name-nondirectory dirname) newname)))
1937 (if (not (file-directory-p (file-name-directory newname)))
1938 (make-directory (file-name-directory newname) parents))
1939 (tramp-do-copy-or-rename-file-out-of-band
1940 'copy dirname newname keep-date))
1941 ;; We must do it file-wise.
1942 (tramp-run-real-handler
1943 'copy-directory (list dirname newname keep-date parents)))
1945 ;; When newname did exist, we have wrong cached values.
1947 (with-parsed-tramp-file-name newname nil
1948 (tramp-flush-file-property v (file-name-directory localname))
1949 (tramp-flush-file-property v localname))))))
1951 (defun tramp-sh-handle-rename-file
1952 (filename newname &optional ok-if-already-exists)
1953 "Like `rename-file' for Tramp files."
1954 ;; Check if both files are local -- invoke normal rename-file.
1955 ;; Otherwise, use Tramp from local system.
1956 (setq filename (expand-file-name filename))
1957 (setq newname (expand-file-name newname))
1958 ;; At least one file a Tramp file?
1959 (if (or (tramp-tramp-file-p filename)
1960 (tramp-tramp-file-p newname))
1961 (tramp-do-copy-or-rename-file
1962 'rename filename newname ok-if-already-exists t t)
1963 (tramp-run-real-handler
1964 'rename-file (list filename newname ok-if-already-exists))))
1966 (defun tramp-do-copy-or-rename-file
1967 (op filename newname &optional ok-if-already-exists keep-date
1968 preserve-uid-gid preserve-extended-attributes)
1969 "Copy or rename a remote file.
1970 OP must be `copy' or `rename' and indicates the operation to perform.
1971 FILENAME specifies the file to copy or rename, NEWNAME is the name of
1972 the new file (for copy) or the new name of the file (for rename).
1973 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
1974 KEEP-DATE means to make sure that NEWNAME has the same timestamp
1975 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
1976 the uid and gid if both files are on the same host.
1977 PRESERVE-EXTENDED-ATTRIBUTES activates selinux and acl commands.
1979 This function is invoked by `tramp-sh-handle-copy-file' and
1980 `tramp-sh-handle-rename-file'. It is an error if OP is neither
1981 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
1983 (unless (memq op '(copy rename))
1984 (error "Unknown operation `%s', must be `copy' or `rename'" op))
1985 (let ((t1 (tramp-tramp-file-p filename))
1986 (t2 (tramp-tramp-file-p newname))
1987 (length (nth 7 (file-attributes (file-truename filename))))
1988 (attributes (and preserve-extended-attributes
1989 (apply 'file-extended-attributes (list filename))))
1992 (with-parsed-tramp-file-name (if t1 filename newname) nil
1993 (when (and (not ok-if-already-exists) (file-exists-p newname))
1995 v 'file-already-exists "File %s already exists" newname))
1997 (with-tramp-progress-reporter
1998 v 0 (format "%s %s to %s"
1999 (if (eq op 'copy) "Copying" "Renaming")
2003 ;; Both are Tramp files.
2005 (with-parsed-tramp-file-name filename v1
2006 (with-parsed-tramp-file-name newname v2
2008 ;; Shortcut: if method, host, user are the same for
2009 ;; both files, we invoke `cp' or `mv' on the remote
2011 ((tramp-equal-remote filename newname)
2012 (tramp-do-copy-or-rename-file-directly
2014 ok-if-already-exists keep-date preserve-uid-gid))
2016 ;; Try out-of-band operation.
2018 (tramp-method-out-of-band-p v1 length)
2019 (tramp-method-out-of-band-p v2 length))
2020 (tramp-do-copy-or-rename-file-out-of-band
2021 op filename newname keep-date))
2023 ;; No shortcut was possible. So we copy the file
2024 ;; first. If the operation was `rename', we go back
2025 ;; and delete the original file (if the copy was
2026 ;; successful). The approach is simple-minded: we
2027 ;; create a new buffer, insert the contents of the
2028 ;; source file into it, then write out the buffer to
2029 ;; the target file. The advantage is that it doesn't
2030 ;; matter which filename handlers are used for the
2031 ;; source and target file.
2033 (tramp-do-copy-or-rename-file-via-buffer
2034 op filename newname keep-date))))))
2036 ;; One file is a Tramp file, the other one is local.
2039 ;; Fast track on local machine.
2040 ((tramp-local-host-p v)
2041 (tramp-do-copy-or-rename-file-directly
2043 ok-if-already-exists keep-date preserve-uid-gid))
2045 ;; If the Tramp file has an out-of-band method, the
2046 ;; corresponding copy-program can be invoked.
2047 ((tramp-method-out-of-band-p v length)
2048 (tramp-do-copy-or-rename-file-out-of-band
2049 op filename newname keep-date))
2051 ;; Use the inline method via a Tramp buffer.
2052 (t (tramp-do-copy-or-rename-file-via-buffer
2053 op filename newname keep-date))))
2056 ;; One of them must be a Tramp file.
2057 (error "Tramp implementation says this cannot happen")))
2059 ;; Handle `preserve-extended-attributes'. We ignore possible
2060 ;; errors, because ACL strings could be incompatible.
2063 (apply 'set-file-extended-attributes (list newname attributes))))
2065 ;; In case of `rename', we must flush the cache of the source file.
2066 (when (and t1 (eq op 'rename))
2067 (with-parsed-tramp-file-name filename v1
2068 (tramp-flush-file-property v1 (file-name-directory localname))
2069 (tramp-flush-file-property v1 localname)))
2071 ;; When newname did exist, we have wrong cached values.
2073 (with-parsed-tramp-file-name newname v2
2074 (tramp-flush-file-property v2 (file-name-directory localname))
2075 (tramp-flush-file-property v2 localname)))))))
2077 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
2078 "Use an Emacs buffer to copy or rename a file.
2079 First arg OP is either `copy' or `rename' and indicates the operation.
2080 FILENAME is the source file, NEWNAME the target file.
2081 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2083 ;; We must disable multibyte, because binary data shall not be
2085 (set-buffer-multibyte nil)
2086 (let ((coding-system-for-read 'binary)
2087 (jka-compr-inhibit t))
2088 (insert-file-contents-literally filename))
2089 ;; We don't want the target file to be compressed, so we let-bind
2090 ;; `jka-compr-inhibit' to t.
2091 (let ((coding-system-for-write 'binary)
2092 (jka-compr-inhibit t))
2093 (write-region (point-min) (point-max) newname)))
2094 ;; KEEP-DATE handling.
2095 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
2097 (set-file-modes newname (tramp-default-file-modes filename))
2098 ;; If the operation was `rename', delete the original file.
2099 (unless (eq op 'copy) (delete-file filename)))
2101 (defun tramp-do-copy-or-rename-file-directly
2102 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
2103 "Invokes `cp' or `mv' on the remote system.
2104 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2105 respectively. FILENAME specifies the file to copy or rename,
2106 NEWNAME is the name of the new file (for copy) or the new name of
2107 the file (for rename). Both files must reside on the same host.
2108 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2109 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2110 the uid and gid from FILENAME."
2111 (let ((t1 (tramp-tramp-file-p filename))
2112 (t2 (tramp-tramp-file-p newname))
2113 (file-times (nth 5 (file-attributes filename)))
2114 (file-modes (tramp-default-file-modes filename)))
2115 (with-parsed-tramp-file-name (if t1 filename newname) nil
2116 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
2117 ((eq op 'copy) "cp -f")
2118 ((eq op 'rename) "mv -f")
2121 "Unknown operation `%s', must be `copy' or `rename'"
2125 (tramp-file-name-handler 'file-remote-p filename 'localname)
2129 (tramp-file-name-handler 'file-remote-p newname 'localname)
2131 (prefix (file-remote-p (if t1 filename newname)))
2135 ;; Both files are on a remote host, with same user.
2138 (tramp-send-command-and-check
2139 v (format "%s %s %s" cmd
2140 (tramp-shell-quote-argument localname1)
2141 (tramp-shell-quote-argument localname2))))
2142 (with-current-buffer (tramp-get-buffer v)
2143 (goto-char (point-min))
2147 ;; Mask cp -f error.
2149 tramp-operation-not-permitted-regexp nil t))
2151 (tramp-error-with-buffer
2153 "Copying directly failed, see buffer `%s' for details."
2156 ;; We are on the local host.
2159 ;; We can do it directly.
2160 ((let (file-name-handler-alist)
2161 (and (file-readable-p localname1)
2162 (file-writable-p (file-name-directory localname2))
2163 (or (file-directory-p localname2)
2164 (file-writable-p localname2))))
2166 (tramp-compat-copy-file
2167 localname1 localname2 ok-if-already-exists
2168 keep-date preserve-uid-gid)
2169 (tramp-run-real-handler
2170 'rename-file (list localname1 localname2 ok-if-already-exists))))
2172 ;; We can do it directly with `tramp-send-command'
2173 ((and (file-readable-p (concat prefix localname1))
2175 (file-name-directory (concat prefix localname2)))
2176 (or (file-directory-p (concat prefix localname2))
2177 (file-writable-p (concat prefix localname2))))
2178 (tramp-do-copy-or-rename-file-directly
2179 op (concat prefix localname1) (concat prefix localname2)
2180 ok-if-already-exists keep-date t)
2181 ;; We must change the ownership to the local user.
2182 (tramp-set-file-uid-gid
2183 (concat prefix localname2)
2184 (tramp-get-local-uid 'integer)
2185 (tramp-get-local-gid 'integer)))
2187 ;; We need a temporary file in between.
2189 ;; Create the temporary file.
2190 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
2195 (tramp-barf-unless-okay
2198 (tramp-shell-quote-argument localname1)
2199 (tramp-shell-quote-argument tmpfile))
2200 "Copying directly failed, see buffer `%s' for details."
2201 (tramp-get-buffer v))
2202 ;; We must change the ownership as remote user.
2203 ;; Since this does not work reliable, we also
2204 ;; give read permissions.
2206 (concat prefix tmpfile)
2207 (tramp-compat-octal-to-decimal "0777"))
2208 (tramp-set-file-uid-gid
2209 (concat prefix tmpfile)
2210 (tramp-get-local-uid 'integer)
2211 (tramp-get-local-gid 'integer)))
2214 (tramp-compat-copy-file
2215 localname1 tmpfile t
2216 keep-date preserve-uid-gid)
2217 (tramp-run-real-handler
2219 (list localname1 tmpfile t)))
2220 ;; We must change the ownership as local user.
2221 ;; Since this does not work reliable, we also
2222 ;; give read permissions.
2224 tmpfile (tramp-compat-octal-to-decimal "0777"))
2225 (tramp-set-file-uid-gid
2227 (tramp-get-remote-uid v 'integer)
2228 (tramp-get-remote-gid v 'integer))))
2230 ;; Move the temporary file to its destination.
2233 (tramp-barf-unless-okay
2236 (tramp-shell-quote-argument tmpfile)
2237 (tramp-shell-quote-argument localname2))
2238 "Copying directly failed, see buffer `%s' for details."
2239 (tramp-get-buffer v)))
2241 (tramp-run-real-handler
2243 (list tmpfile localname2 ok-if-already-exists)))))
2246 (ignore-errors (delete-file tmpfile)))))))))
2248 ;; Set the time and mode. Mask possible errors.
2251 (set-file-times newname file-times)
2252 (set-file-modes newname file-modes))))))
2254 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
2255 "Invoke rcp program to copy.
2256 The method used must be an out-of-band method."
2257 (let* ((t1 (tramp-tramp-file-p filename))
2258 (t2 (tramp-tramp-file-p newname))
2259 (orig-vec (tramp-dissect-file-name (if t1 filename newname)))
2260 copy-program copy-args copy-env copy-keep-date port spec
2261 options source target)
2263 (with-parsed-tramp-file-name (if t1 filename newname) nil
2266 ;; Both are Tramp files. We shall optimize it when the
2267 ;; methods for filename and newname are the same.
2268 (let* ((dir-flag (file-directory-p filename))
2269 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
2273 (file-name-nondirectory newname) tmpfile)))
2276 (tramp-do-copy-or-rename-file-out-of-band
2277 op filename tmpfile keep-date)
2278 (tramp-do-copy-or-rename-file-out-of-band
2279 'rename tmpfile newname keep-date))
2283 (tramp-compat-delete-directory
2284 (expand-file-name ".." tmpfile) 'recursive)
2285 (delete-file tmpfile)))))
2287 ;; Set variables for computing the prompt for reading
2289 (setq tramp-current-method (tramp-file-name-method v)
2290 tramp-current-user (or (tramp-file-name-user v)
2291 (tramp-get-connection-property
2293 tramp-current-host (tramp-file-name-real-host v))
2295 ;; Expand hops. Might be necessary for gateway methods.
2296 (setq v (car (tramp-compute-multi-hops v)))
2297 (aset v 3 localname)
2299 ;; Check which ones of source and target are Tramp files.
2300 (setq source (if t1 (tramp-make-copy-program-file-name v) filename)
2302 (if (and (file-directory-p filename)
2304 (file-name-nondirectory filename)
2305 (file-name-nondirectory newname)))
2306 'file-name-directory
2308 (if t2 (tramp-make-copy-program-file-name v) newname)))
2310 ;; Check for host and port number. We cannot use
2311 ;; `tramp-file-name-port', because this returns also
2312 ;; `tramp-default-port', which might clash with settings in
2314 (setq host (tramp-file-name-host v)
2316 (when (string-match tramp-host-with-port-regexp host)
2317 (setq port (string-to-number (match-string 2 host))
2318 host (string-to-number (match-string 1 host))))
2320 ;; Check for user. There might be an interactive setting.
2321 (setq user (or (tramp-file-name-user v)
2322 (tramp-get-connection-property v "login-as" nil)))
2324 ;; Compose copy command.
2325 (setq host (or host "")
2328 spec (format-spec-make
2329 ?t (tramp-get-connection-property
2330 (tramp-get-connection-process v) "temp-file" ""))
2331 options (format-spec tramp-ssh-controlmaster-options spec)
2332 spec (format-spec-make
2333 ?h host ?u user ?p port ?c options
2334 ?k (if keep-date " " ""))
2335 copy-program (tramp-get-method-parameter
2336 method 'tramp-copy-program)
2337 copy-keep-date (tramp-get-method-parameter
2338 method 'tramp-copy-keep-date)
2341 ;; " " has either been a replacement of "%k" (when
2342 ;; keep-date argument is non-nil), or a replacement
2343 ;; for the whole keep-date sublist.
2347 (tramp-get-method-parameter method 'tramp-copy-args)
2352 (let ((y (mapcar (lambda (z) (format-spec z spec)) x)))
2353 (if (member "" y) '(" ") y))))))
2359 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
2360 (unless (member "" x) (mapconcat 'identity x " ")))
2361 (tramp-get-method-parameter method 'tramp-copy-env))))
2363 ;; Check for program.
2364 (unless (let ((default-directory
2365 (tramp-compat-temporary-file-directory)))
2366 (executable-find copy-program))
2368 v 'file-error "Cannot find copy program: %s" copy-program))
2372 ;; The default directory must be remote.
2373 (let ((default-directory
2374 (file-name-directory (if t1 filename newname)))
2375 (process-environment (copy-sequence process-environment)))
2376 ;; Set the transfer process properties.
2377 (tramp-set-connection-property
2378 v "process-name" (buffer-name (current-buffer)))
2379 (tramp-set-connection-property
2380 v "process-buffer" (current-buffer))
2383 orig-vec 6 "%s=\"%s\"" (car copy-env) (cadr copy-env))
2384 (setenv (pop copy-env) (pop copy-env)))
2386 ;; Use an asynchronous process. By this, password can
2387 ;; be handled. The default directory must be local, in
2388 ;; order to apply the correct `copy-program'. We don't
2389 ;; set a timeout, because the copying of large files can
2390 ;; last longer than 60 secs.
2391 (let ((p (let ((default-directory
2392 (tramp-compat-temporary-file-directory)))
2393 (apply 'start-process-shell-command
2394 (tramp-get-connection-name v)
2395 (tramp-get-connection-buffer v)
2400 (shell-quote-argument source)
2401 (shell-quote-argument target)
2402 "&&" "echo" "tramp_exit_status" "0"
2403 "||" "echo" "tramp_exit_status" "1"))))))
2406 (mapconcat 'identity (process-command p) " "))
2407 (tramp-compat-set-process-query-on-exit-flag p nil)
2408 (tramp-process-actions
2409 p v nil tramp-actions-copy-out-of-band)
2411 ;; Check the return code.
2412 (goto-char (point-max))
2414 (re-search-backward "tramp_exit_status [0-9]+" nil t)
2416 orig-vec 'file-error
2417 "Couldn't find exit status of `%s'" (process-command p)))
2418 (skip-chars-forward "^ ")
2419 (unless (zerop (read (current-buffer)))
2422 orig-vec 'file-error
2423 "Error copying: `%s'"
2424 (buffer-substring (point-min) (point-at-eol))))))
2426 ;; Reset the transfer process properties.
2427 (tramp-message orig-vec 6 "\n%s" (buffer-string))
2428 (tramp-set-connection-property v "process-name" nil)
2429 (tramp-set-connection-property v "process-buffer" nil)))
2431 ;; Handle KEEP-DATE argument.
2432 (when (and keep-date (not copy-keep-date))
2433 (set-file-times newname (nth 5 (file-attributes filename))))
2436 (unless (and keep-date copy-keep-date)
2438 (set-file-modes newname (tramp-default-file-modes filename)))))
2440 ;; If the operation was `rename', delete the original file.
2441 (unless (eq op 'copy)
2442 (if (file-regular-p filename)
2443 (delete-file filename)
2444 (tramp-compat-delete-directory filename 'recursive))))))
2446 (defun tramp-sh-handle-make-directory (dir &optional parents)
2447 "Like `make-directory' for Tramp files."
2448 (setq dir (expand-file-name dir))
2449 (with-parsed-tramp-file-name dir nil
2450 (tramp-flush-directory-property v (file-name-directory localname))
2452 (tramp-barf-unless-okay
2454 (if parents "mkdir -p" "mkdir")
2455 (tramp-shell-quote-argument localname))
2456 "Couldn't make directory %s" dir))))
2458 (defun tramp-sh-handle-delete-directory (directory &optional recursive)
2459 "Like `delete-directory' for Tramp files."
2460 (setq directory (expand-file-name directory))
2461 (with-parsed-tramp-file-name directory nil
2462 (tramp-flush-file-property v (file-name-directory localname))
2463 (tramp-flush-directory-property v localname)
2464 (tramp-barf-unless-okay
2466 (if recursive "rm -rf" "rmdir")
2467 (tramp-shell-quote-argument localname))
2468 "Couldn't delete %s" directory)))
2470 (defun tramp-sh-handle-delete-file (filename &optional trash)
2471 "Like `delete-file' for Tramp files."
2472 (setq filename (expand-file-name filename))
2473 (with-parsed-tramp-file-name filename nil
2474 (tramp-flush-file-property v (file-name-directory localname))
2475 (tramp-flush-file-property v localname)
2476 (tramp-barf-unless-okay
2478 (or (and trash (tramp-get-remote-trash v)) "rm -f")
2479 (tramp-shell-quote-argument localname))
2480 "Couldn't delete %s" filename)))
2484 ;; CCC: This does not seem to be enough. Something dies when
2485 ;; we try and delete two directories under Tramp :/
2486 (defun tramp-sh-handle-dired-recursive-delete-directory (filename)
2487 "Recursively delete the directory given.
2488 This is like `dired-recursive-delete-directory' for Tramp files."
2489 (with-parsed-tramp-file-name filename nil
2490 ;; Run a shell command 'rm -r <localname>'.
2491 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
2492 (unless (file-exists-p filename)
2493 (tramp-error v 'file-error "No such directory: %s" filename))
2494 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>).
2497 (format "rm -rf %s" (tramp-shell-quote-argument localname))
2498 ;; Don't read the output, do it explicitly.
2500 ;; Wait for the remote system to return to us...
2501 ;; This might take a while, allow it plenty of time.
2502 (tramp-wait-for-output (tramp-get-connection-process v) 120)
2503 ;; Make sure that it worked...
2504 (tramp-flush-file-property v (file-name-directory localname))
2505 (tramp-flush-directory-property v localname)
2506 (and (file-exists-p filename)
2508 v 'file-error "Failed to recursively delete %s" filename))))
2510 (defun tramp-sh-handle-dired-compress-file (file &rest ok-flag)
2511 "Like `dired-compress-file' for Tramp files."
2512 ;; OK-FLAG is valid for XEmacs only, but not implemented.
2513 ;; Code stolen mainly from dired-aux.el.
2514 (with-parsed-tramp-file-name file nil
2515 (tramp-flush-file-property v localname)
2518 (if (not (featurep 'xemacs))
2520 (symbol-value 'dired-compress-file-suffixes)
2521 ;; XEmacs has `dired-compression-method-alist', which is
2522 ;; transformed into `dired-compress-file-suffixes' structure.
2525 (list (concat (regexp-quote (nth 1 x)) "\\'")
2527 (mapconcat 'identity (nth 3 x) " ")))
2528 (symbol-value 'dired-compression-method-alist))))
2530 ;; See if any suffix rule matches this file name.
2532 (let (case-fold-search)
2533 (if (string-match (car (car suffixes)) localname)
2534 (setq suffix (car suffixes) suffixes nil))
2535 (setq suffixes (cdr suffixes))))
2537 (cond ((file-symlink-p file)
2539 ((and suffix (nth 2 suffix))
2540 ;; We found an uncompression rule.
2541 (with-tramp-progress-reporter
2542 v 0 (format "Uncompressing %s" file)
2543 (when (tramp-send-command-and-check
2544 v (concat (nth 2 suffix) " "
2545 (tramp-shell-quote-argument localname)))
2546 ;; `dired-remove-file' is not defined in XEmacs.
2547 (tramp-compat-funcall 'dired-remove-file file)
2548 (string-match (car suffix) file)
2549 (concat (substring file 0 (match-beginning 0))))))
2551 ;; We don't recognize the file as compressed, so compress it.
2553 (with-tramp-progress-reporter v 0 (format "Compressing %s" file)
2554 (when (tramp-send-command-and-check
2555 v (concat "gzip -f "
2556 (tramp-shell-quote-argument localname)))
2557 ;; `dired-remove-file' is not defined in XEmacs.
2558 (tramp-compat-funcall 'dired-remove-file file)
2559 (cond ((file-exists-p (concat file ".gz"))
2560 (concat file ".gz"))
2561 ((file-exists-p (concat file ".z"))
2565 (defun tramp-sh-handle-insert-directory
2566 (filename switches &optional wildcard full-directory-p)
2567 "Like `insert-directory' for Tramp files."
2568 (setq filename (expand-file-name filename))
2569 (with-parsed-tramp-file-name filename nil
2570 (if (and (featurep 'ls-lisp)
2571 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
2572 (tramp-run-real-handler
2573 'insert-directory (list filename switches wildcard full-directory-p))
2574 (when (stringp switches)
2575 (setq switches (split-string switches)))
2576 (when (and (member "--dired" switches)
2577 (not (tramp-get-ls-command-with-dired v)))
2578 (setq switches (delete "--dired" switches)))
2580 (setq wildcard (tramp-run-real-handler
2581 'file-name-nondirectory (list localname)))
2582 (setq localname (tramp-run-real-handler
2583 'file-name-directory (list localname))))
2584 (unless full-directory-p
2585 (setq switches (add-to-list 'switches "-d" 'append)))
2586 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
2588 (setq switches (concat switches " " wildcard)))
2590 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2591 switches filename (if wildcard "yes" "no")
2592 (if full-directory-p "yes" "no"))
2593 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2594 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2595 ;; "--dired" returns byte positions. Therefore, the file names
2596 ;; must be encoded, which is guaranteed by "LC_ALL=en_US.UTF8
2598 (if full-directory-p
2601 (format "env LC_ALL=en_US.UTF8 LC_CTYPE='' %s %s %s 2>/dev/null"
2602 (tramp-get-ls-command v)
2606 (tramp-shell-quote-argument (concat localname ".")))))
2607 (tramp-barf-unless-okay
2609 (format "cd %s" (tramp-shell-quote-argument
2610 (tramp-run-real-handler
2611 'file-name-directory (list localname))))
2613 (tramp-shell-quote-argument
2614 (tramp-run-real-handler 'file-name-directory (list localname))))
2617 (format "env LC_ALL=en_US.UTF8 LC_CTYPE='' %s %s %s"
2618 (tramp-get-ls-command v)
2622 (tramp-run-real-handler
2623 'file-name-nondirectory (list localname)))))
2625 (tramp-shell-quote-argument
2626 (tramp-run-real-handler
2627 'file-name-nondirectory (list localname)))))))
2628 (let ((beg (point)))
2629 ;; We cannot use `insert-buffer-substring' because the Tramp
2630 ;; buffer changes its contents before insertion due to calling
2631 ;; `expand-file' and alike.
2633 (with-current-buffer (tramp-get-buffer v)
2636 ;; Check for "--dired" output.
2638 (when (looking-at "//SUBDIRED//")
2640 (when (looking-at "//DIRED//\\s-+")
2641 (let ((databeg (match-end 0))
2642 (end (point-at-eol)))
2643 ;; Now read the numeric positions of file names.
2645 (while (< (point) end)
2646 (let ((start (+ beg (read (current-buffer))))
2647 (end (+ beg (read (current-buffer)))))
2648 (if (memq (char-after end) '(?\n ?\ ))
2649 ;; End is followed by \n or by " -> ".
2650 (put-text-property start end 'dired-filename t))))))
2651 ;; Remove trailing lines.
2652 (goto-char (point-at-bol))
2653 (while (looking-at "//")
2655 (delete-region (match-beginning 0) (point)))
2657 ;; Some busyboxes are reluctant to discard colors.
2658 (unless (string-match "color" (tramp-get-connection-property v "ls" ""))
2660 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
2661 (replace-match "")))
2663 ;; Decode the output, it could be multibyte.
2664 (decode-coding-region
2666 (or file-name-coding-system default-file-name-coding-system))
2668 ;; The inserted file could be from somewhere else.
2669 (when (and (not wildcard) (not full-directory-p))
2670 (goto-char (point-max))
2671 (when (file-symlink-p filename)
2672 (goto-char (search-backward "->" beg 'noerror)))
2674 (if (zerop (length (file-name-nondirectory filename)))
2676 (file-name-nondirectory filename))
2678 (replace-match (file-relative-name filename) t))
2680 (goto-char (point-max))))))
2682 ;; Canonicalization of file names.
2684 (defun tramp-sh-handle-expand-file-name (name &optional dir)
2685 "Like `expand-file-name' for Tramp files.
2686 If the localname part of the given filename starts with \"/../\" then
2687 the result will be a local, non-Tramp, filename."
2688 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
2689 (setq dir (or dir default-directory "/"))
2690 ;; Unless NAME is absolute, concat DIR and NAME.
2691 (unless (file-name-absolute-p name)
2692 (setq name (concat (file-name-as-directory dir) name)))
2693 ;; If NAME is not a Tramp file, run the real handler.
2694 (if (not (tramp-connectable-p name))
2695 (tramp-run-real-handler 'expand-file-name (list name nil))
2697 (with-parsed-tramp-file-name name nil
2698 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
2699 (setq localname (concat "~/" localname)))
2700 ;; Tilde expansion if necessary. This needs a shell which
2701 ;; groks tilde expansion! The function `tramp-find-shell' is
2702 ;; supposed to find such a shell on the remote host. Please
2703 ;; tell me about it when this doesn't work on your system.
2704 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
2705 (let ((uname (match-string 1 localname))
2706 (fname (match-string 2 localname)))
2707 ;; We cannot simply apply "~/", because under sudo "~/" is
2708 ;; expanded to the local user home directory but to the
2709 ;; root home directory. On the other hand, using always
2710 ;; the default user name for tilde expansion is not
2711 ;; appropriate either, because ssh and companions might
2712 ;; use a user name from the config file.
2713 (when (and (string-equal uname "~")
2714 (string-match "\\`su\\(do\\)?\\'" method))
2715 (setq uname (concat uname user)))
2717 (with-tramp-connection-property v uname
2719 v (format "cd %s; pwd" (tramp-shell-quote-argument uname)))
2720 (with-current-buffer (tramp-get-buffer v)
2721 (goto-char (point-min))
2722 (buffer-substring (point) (point-at-eol)))))
2723 (setq localname (concat uname fname))))
2724 ;; There might be a double slash, for example when "~/"
2725 ;; expands to "/". Remove this.
2726 (while (string-match "//" localname)
2727 (setq localname (replace-match "/" t t localname)))
2728 ;; No tilde characters in file name, do normal
2729 ;; `expand-file-name' (this does "/./" and "/../"). We bind
2730 ;; `directory-sep-char' here for XEmacs on Windows, which would
2731 ;; otherwise use backslash. `default-directory' is bound,
2732 ;; because on Windows there would be problems with UNC shares or
2734 (let ((directory-sep-char ?/)
2735 (default-directory (tramp-compat-temporary-file-directory)))
2736 (tramp-make-tramp-file-name
2738 (tramp-drop-volume-letter
2739 (tramp-run-real-handler
2740 'expand-file-name (list localname)))
2743 ;;; Remote commands:
2745 (defun tramp-sh-handle-executable-find (command)
2746 "Like `executable-find' for Tramp files."
2747 (with-parsed-tramp-file-name default-directory nil
2748 (tramp-find-executable v command (tramp-get-remote-path v) t)))
2750 (defun tramp-process-sentinel (proc event)
2751 "Flush file caches."
2752 (unless (memq (process-status proc) '(run open))
2753 (let ((vec (tramp-get-connection-property proc "vector" nil)))
2755 (tramp-message vec 5 "Sentinel called: `%s' `%s'" proc event)
2756 (tramp-flush-connection-property proc)
2757 (tramp-flush-directory-property vec "")))))
2759 ;; We use BUFFER also as connection buffer during setup. Because of
2760 ;; this, its original contents must be saved, and restored once
2761 ;; connection has been setup.
2762 (defun tramp-sh-handle-start-file-process (name buffer program &rest args)
2763 "Like `start-file-process' for Tramp files."
2764 (with-parsed-tramp-file-name default-directory nil
2765 ;; When PROGRAM is nil, we just provide a tty.
2767 (when (stringp program)
2768 (format "cd %s; exec env PS1=%s %s"
2769 (tramp-shell-quote-argument localname)
2770 ;; Use a human-friendly prompt, for example for `shell'.
2771 (tramp-shell-quote-argument
2773 (file-remote-p default-directory)
2774 tramp-initial-end-of-output))
2775 (mapconcat 'tramp-shell-quote-argument
2776 (cons program args) " "))))
2777 (tramp-process-connection-type
2778 (or (null program) tramp-process-connection-type))
2779 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
2782 ;; We do not want to raise an error when
2783 ;; `start-file-process' has been started several time in
2784 ;; `eshell' and friends.
2785 (tramp-current-connection nil))
2788 ;; BUFFER can be nil. We use a temporary buffer.
2789 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
2790 (while (get-process name1)
2791 ;; NAME must be unique as process name.
2793 name1 (format "%s<%d>" name i)))
2795 ;; Set the new process properties.
2796 (tramp-set-connection-property v "process-name" name)
2797 (tramp-set-connection-property v "process-buffer" buffer)
2799 (with-current-buffer (tramp-get-connection-buffer v)
2801 ;; We catch this event. Otherwise, `start-process' could
2802 ;; be called on the local host.
2805 ;; Activate narrowing in order to save BUFFER
2806 ;; contents. Clear also the modification time;
2807 ;; otherwise we might be interrupted by
2808 ;; `verify-visited-file-modtime'.
2809 (let ((buffer-undo-list t)
2810 (buffer-read-only nil)
2812 (clear-visited-file-modtime)
2813 (narrow-to-region (point-max) (point-max))
2814 ;; We call `tramp-maybe-open-connection', in order
2815 ;; to cleanup the prompt afterwards.
2817 (tramp-maybe-open-connection v)
2819 (delete-region mark (point))
2820 (narrow-to-region (point-max) (point-max))
2823 ;; Send the command.
2824 (tramp-send-command v command nil t) ; nooutput
2825 ;; Check, whether a pty is associated.
2826 (unless (tramp-compat-process-get
2827 (tramp-get-connection-process v) 'remote-tty)
2830 "pty association is not supported for `%s'" name))))
2831 (let ((p (tramp-get-connection-process v)))
2832 ;; Set query flag and process marker for this
2833 ;; process. We ignore errors, because the process
2834 ;; could have finished already.
2836 (tramp-compat-set-process-query-on-exit-flag p t)
2837 (set-marker (process-mark p) (point)))
2842 (if (string-match tramp-temp-buffer-name (buffer-name))
2844 (set-process-buffer (tramp-get-connection-process v) nil)
2845 (kill-buffer (current-buffer)))
2846 (set-buffer-modified-p bmp))
2847 (tramp-set-connection-property v "process-name" nil)
2848 (tramp-set-connection-property v "process-buffer" nil))))))
2850 (defun tramp-sh-handle-process-file
2851 (program &optional infile destination display &rest args)
2852 "Like `process-file' for Tramp files."
2853 ;; The implementation is not complete yet.
2854 (when (and (numberp destination) (zerop destination))
2855 (error "Implementation does not handle immediate return"))
2857 (with-parsed-tramp-file-name default-directory nil
2858 (let (command input tmpinput stderr tmpstderr outbuf ret)
2860 (setq command (mapconcat 'tramp-shell-quote-argument
2861 (cons program args) " "))
2864 (setq input "/dev/null")
2865 (setq infile (expand-file-name infile))
2866 (if (tramp-equal-remote default-directory infile)
2867 ;; INFILE is on the same remote host.
2868 (setq input (with-parsed-tramp-file-name infile nil localname))
2869 ;; INFILE must be copied to remote host.
2870 (setq input (tramp-make-tramp-temp-file v)
2871 tmpinput (tramp-make-tramp-file-name method user host input))
2872 (copy-file infile tmpinput t)))
2873 (when input (setq command (format "%s <%s" command input)))
2875 ;; Determine output.
2878 ((bufferp destination)
2879 (setq outbuf destination))
2881 ((stringp destination)
2882 (setq outbuf (get-buffer-create destination)))
2883 ;; (REAL-DESTINATION ERROR-DESTINATION)
2884 ((consp destination)
2887 ((bufferp (car destination))
2888 (setq outbuf (car destination)))
2889 ((stringp (car destination))
2890 (setq outbuf (get-buffer-create (car destination))))
2892 (setq outbuf (current-buffer))))
2895 ((stringp (cadr destination))
2896 (setcar (cdr destination) (expand-file-name (cadr destination)))
2897 (if (tramp-equal-remote default-directory (cadr destination))
2898 ;; stderr is on the same remote host.
2899 (setq stderr (with-parsed-tramp-file-name
2900 (cadr destination) nil localname))
2901 ;; stderr must be copied to remote host. The temporary
2902 ;; file must be deleted after execution.
2903 (setq stderr (tramp-make-tramp-temp-file v)
2904 tmpstderr (tramp-make-tramp-file-name
2905 method user host stderr))))
2906 ;; stderr to be discarded.
2907 ((null (cadr destination))
2908 (setq stderr "/dev/null"))))
2911 (setq outbuf (current-buffer))))
2912 (when stderr (setq command (format "%s 2>%s" command stderr)))
2914 ;; Send the command. It might not return in time, so we protect
2915 ;; it. Call it in a subshell, in order to preserve working
2920 (if (tramp-send-command-and-check
2921 v (format "\\cd %s; %s"
2922 (tramp-shell-quote-argument localname)
2926 ;; We should show the output anyway.
2928 (with-current-buffer outbuf
2930 (with-current-buffer (tramp-get-connection-buffer v)
2932 (when display (display-buffer outbuf))))
2933 ;; When the user did interrupt, we should do it also. We use
2934 ;; return code -1 as marker.
2936 (kill-buffer (tramp-get-connection-buffer v))
2940 (kill-buffer (tramp-get-connection-buffer v))
2943 ;; Provide error file.
2944 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
2946 ;; Cleanup. We remove all file cache values for the connection,
2947 ;; because the remote process could have changed them.
2948 (when tmpinput (delete-file tmpinput))
2950 ;; `process-file-side-effects' has been introduced with GNU
2951 ;; Emacs 23.2. If set to `nil', no remote file will be changed
2952 ;; by `program'. If it doesn't exist, we assume its default
2954 (unless (and (boundp 'process-file-side-effects)
2955 (not (symbol-value 'process-file-side-effects)))
2956 (tramp-flush-directory-property v ""))
2958 ;; Return exit status.
2963 (defun tramp-sh-handle-file-local-copy (filename)
2964 "Like `file-local-copy' for Tramp files."
2965 (with-parsed-tramp-file-name filename nil
2966 (unless (file-exists-p filename)
2969 "Cannot make local copy of non-existing file `%s'" filename))
2971 (let* ((size (nth 7 (file-attributes (file-truename filename))))
2972 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
2973 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
2974 (tmpfile (tramp-compat-make-temp-file filename)))
2978 ;; `copy-file' handles direct copy and out-of-band methods.
2979 ((or (tramp-local-host-p v)
2980 (tramp-method-out-of-band-p v size))
2981 (copy-file filename tmpfile t t))
2983 ;; Use inline encoding for file transfer.
2986 (with-tramp-progress-reporter
2987 v 3 (format "Encoding remote file %s" filename)
2988 (tramp-barf-unless-okay
2989 v (format rem-enc (tramp-shell-quote-argument localname))
2990 "Encoding remote file failed"))
2992 (if (functionp loc-dec)
2993 ;; If local decoding is a function, we call it. We
2994 ;; must disable multibyte, because
2995 ;; `uudecode-decode-region' doesn't handle it
2998 (set-buffer-multibyte nil)
2999 (insert-buffer-substring (tramp-get-buffer v))
3000 (with-tramp-progress-reporter
3001 v 3 (format "Decoding remote file %s with function %s"
3003 (funcall loc-dec (point-min) (point-max))
3004 ;; Unset `file-name-handler-alist'. Otherwise,
3005 ;; epa-file gets confused.
3006 (let (file-name-handler-alist
3007 (coding-system-for-write 'binary))
3008 (write-region (point-min) (point-max) tmpfile))))
3010 ;; If tramp-decoding-function is not defined for this
3011 ;; method, we invoke tramp-decoding-command instead.
3012 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
3013 ;; Unset `file-name-handler-alist'. Otherwise,
3014 ;; epa-file gets confused.
3015 (let (file-name-handler-alist
3016 (coding-system-for-write 'binary))
3017 (write-region (point-min) (point-max) tmpfile2))
3018 (with-tramp-progress-reporter
3019 v 3 (format "Decoding remote file %s with command %s"
3022 (tramp-call-local-coding-command
3023 loc-dec tmpfile2 tmpfile)
3024 (delete-file tmpfile2)))))
3026 ;; Set proper permissions.
3027 (set-file-modes tmpfile (tramp-default-file-modes filename))
3028 ;; Set local user ownership.
3029 (tramp-set-file-uid-gid tmpfile)))
3031 ;; Oops, I don't know what to do.
3033 v 'file-error "Wrong method specification for `%s'" method)))
3037 (delete-file tmpfile)
3038 (signal (car err) (cdr err))))
3040 (run-hooks 'tramp-handle-file-local-copy-hook)
3043 ;; This is needed for XEmacs only. Code stolen from files.el.
3044 (defun tramp-sh-handle-insert-file-contents-literally
3045 (filename &optional visit beg end replace)
3046 "Like `insert-file-contents-literally' for Tramp files."
3047 (let ((format-alist nil)
3048 (after-insert-file-functions nil)
3049 (coding-system-for-read 'no-conversion)
3050 (coding-system-for-write 'no-conversion)
3051 (find-buffer-file-type-function
3052 (if (fboundp 'find-buffer-file-type)
3053 (symbol-function 'find-buffer-file-type)
3055 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler))
3056 (inhibit-file-name-operation 'insert-file-contents))
3059 (fset 'find-buffer-file-type (lambda (filename) t))
3060 (insert-file-contents filename visit beg end replace))
3062 (if find-buffer-file-type-function
3063 (fset 'find-buffer-file-type find-buffer-file-type-function)
3064 (fmakunbound 'find-buffer-file-type)))))
3066 (defun tramp-sh-handle-make-auto-save-file-name ()
3067 "Like `make-auto-save-file-name' for Tramp files.
3068 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
3069 (let ((tramp-auto-save-directory tramp-auto-save-directory)
3071 (tramp-subst-strs-in-string
3078 (buffer-file-name))))
3079 ;; File name must be unique. This is ensured with Emacs 22 (see
3080 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
3081 ;; all other cases we must do it ourselves.
3082 (when (boundp 'auto-save-file-name-transforms)
3085 (when (and (string-match (car x) buffer-file-name)
3086 (not (car (cddr x))))
3087 (setq tramp-auto-save-directory
3088 (or tramp-auto-save-directory
3089 (tramp-compat-temporary-file-directory)))))
3090 (symbol-value 'auto-save-file-name-transforms)))
3091 ;; Create directory.
3092 (when tramp-auto-save-directory
3093 (setq buffer-file-name
3094 (expand-file-name buffer-file-name tramp-auto-save-directory))
3095 (unless (file-exists-p tramp-auto-save-directory)
3096 (make-directory tramp-auto-save-directory t)))
3097 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3098 ;; it is not a magic file name operation (since Emacs 22).
3099 ;; We must deactivate it temporarily.
3100 (if (not (ad-is-active 'make-auto-save-file-name))
3101 (tramp-run-real-handler 'make-auto-save-file-name nil)
3103 (ad-deactivate 'make-auto-save-file-name)
3105 (tramp-run-real-handler 'make-auto-save-file-name nil)
3106 (ad-activate 'make-auto-save-file-name)))))
3108 ;; CCC grok LOCKNAME
3109 (defun tramp-sh-handle-write-region
3110 (start end filename &optional append visit lockname confirm)
3111 "Like `write-region' for Tramp files."
3112 (setq filename (expand-file-name filename))
3113 (with-parsed-tramp-file-name filename nil
3114 ;; Following part commented out because we don't know what to do about
3115 ;; file locking, and it does not appear to be a problem to ignore it.
3116 ;; Ange-ftp ignores it, too.
3117 ;; (when (and lockname (stringp lockname))
3118 ;; (setq lockname (expand-file-name lockname)))
3119 ;; (unless (or (eq lockname nil)
3120 ;; (string= lockname filename))
3122 ;; "tramp-sh-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3124 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
3125 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
3126 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
3127 (tramp-error v 'file-error "File not overwritten")))
3129 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
3130 (tramp-get-remote-uid v 'integer)))
3131 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
3132 (tramp-get-remote-gid v 'integer))))
3134 (if (and (tramp-local-host-p v)
3135 ;; `file-writable-p' calls `file-expand-file-name'. We
3136 ;; cannot use `tramp-run-real-handler' therefore.
3137 (let (file-name-handler-alist)
3139 (file-writable-p (file-name-directory localname))
3140 (or (file-directory-p localname)
3141 (file-writable-p localname)))))
3142 ;; Short track: if we are on the local host, we can run directly.
3143 (tramp-run-real-handler
3145 (list start end localname append 'no-message lockname confirm))
3147 (let* ((modes (save-excursion (tramp-default-file-modes filename)))
3148 ;; We use this to save the value of
3149 ;; `last-coding-system-used' after writing the tmp
3150 ;; file. At the end of the function, we set
3151 ;; `last-coding-system-used' to this saved value. This
3152 ;; way, any intermediary coding systems used while
3153 ;; talking to the remote shell or suchlike won't hose
3154 ;; this variable. This approach was snarfed from
3157 ;; Write region into a tmp file. This isn't really
3158 ;; needed if we use an encoding function, but currently
3159 ;; we use it always because this makes the logic
3160 ;; simpler. We must also set `temporary-file-directory',
3161 ;; because it could point to a remote directory.
3162 (temporary-file-directory
3163 (tramp-compat-temporary-file-directory))
3164 (tmpfile (or tramp-temp-buffer-file-name
3165 (tramp-compat-make-temp-file filename))))
3167 ;; If `append' is non-nil, we copy the file locally, and let
3168 ;; the native `write-region' implementation do the job.
3169 (when append (copy-file filename tmpfile 'ok))
3171 ;; We say `no-message' here because we don't want the
3172 ;; visited file modtime data to be clobbered from the temp
3173 ;; file. We call `set-visited-file-modtime' ourselves later
3174 ;; on. We must ensure that `file-coding-system-alist'
3175 ;; matches `tmpfile'.
3176 (let (file-name-handler-alist
3177 (file-coding-system-alist
3178 (tramp-find-file-name-coding-system-alist filename tmpfile)))
3180 (tramp-run-real-handler
3182 (list start end tmpfile append 'no-message lockname confirm))
3184 (setq tramp-temp-buffer-file-name nil)
3185 (delete-file tmpfile)
3186 (signal (car err) (cdr err))))
3188 ;; Now, `last-coding-system-used' has the right value. Remember it.
3189 (when (boundp 'last-coding-system-used)
3190 (setq coding-system-used
3191 (symbol-value 'last-coding-system-used))))
3193 ;; The permissions of the temporary file should be set. If
3194 ;; filename does not exist (eq modes nil) it has been
3195 ;; renamed to the backup file. This case `save-buffer'
3196 ;; handles permissions.
3197 ;; Ensure that it is still readable.
3201 (logior (or modes 0) (tramp-compat-octal-to-decimal "0400"))))
3203 ;; This is a bit lengthy due to the different methods
3204 ;; possible for file transfer. First, we check whether the
3205 ;; method uses an rcp program. If so, we call it.
3206 ;; Otherwise, both encoding and decoding command must be
3207 ;; specified. However, if the method _also_ specifies an
3208 ;; encoding function, then that is used for encoding the
3209 ;; contents of the tmp file.
3210 (let* ((size (nth 7 (file-attributes tmpfile)))
3211 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
3212 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
3214 ;; `copy-file' handles direct copy and out-of-band methods.
3215 ((or (tramp-local-host-p v)
3216 (tramp-method-out-of-band-p v size))
3217 (if (and (not (stringp start))
3218 (= (or end (point-max)) (point-max))
3219 (= (or start (point-min)) (point-min))
3220 (tramp-get-method-parameter
3221 method 'tramp-copy-keep-tmpfile))
3223 (setq tramp-temp-buffer-file-name tmpfile)
3225 ;; We keep the local file for performance
3226 ;; reasons, useful for "rsync".
3227 (copy-file tmpfile filename t)
3229 (setq tramp-temp-buffer-file-name nil)
3230 (delete-file tmpfile)
3231 (signal (car err) (cdr err)))))
3232 (setq tramp-temp-buffer-file-name nil)
3233 ;; Don't rename, in order to keep context in SELinux.
3235 (copy-file tmpfile filename t)
3236 (delete-file tmpfile))))
3238 ;; Use inline file transfer.
3243 (set-buffer-multibyte nil)
3244 ;; Use encoding function or command.
3245 (if (functionp loc-enc)
3246 (with-tramp-progress-reporter
3247 v 3 (format "Encoding region using function `%s'"
3249 (let ((coding-system-for-read 'binary))
3250 (insert-file-contents-literally tmpfile))
3251 ;; The following `let' is a workaround for the
3252 ;; base64.el that comes with pgnus-0.84. If
3253 ;; both of the following conditions are
3254 ;; satisfied, it tries to write to a local
3255 ;; file in default-directory, but at this
3256 ;; point, default-directory is remote.
3257 ;; (`call-process-region' can't write to
3258 ;; remote files, it seems.) The file in
3259 ;; question is a tmp file anyway.
3260 (let ((default-directory
3261 (tramp-compat-temporary-file-directory)))
3262 (funcall loc-enc (point-min) (point-max))))
3264 (with-tramp-progress-reporter
3265 v 3 (format "Encoding region using command `%s'"
3267 (unless (zerop (tramp-call-local-coding-command
3271 (concat "Cannot write to `%s', "
3272 "local encoding command `%s' failed")
3273 filename loc-enc))))
3275 ;; Send buffer into remote decoding command which
3276 ;; writes to remote file. Because this happens on
3277 ;; the remote host, we cannot use the function.
3278 (with-tramp-progress-reporter
3280 (format "Decoding region into remote file %s" filename)
3281 (goto-char (point-max))
3282 (unless (bolp) (newline))
3286 (concat rem-dec " <<'EOF'\n%sEOF")
3287 (tramp-shell-quote-argument localname)
3289 (tramp-barf-unless-okay
3291 "Couldn't write region to `%s', decode using `%s' failed"
3293 ;; When `file-precious-flag' is set, the region is
3294 ;; written to a temporary file. Check that the
3295 ;; checksum is equal to that from the local tmpfile.
3296 (when file-precious-flag
3299 ;; cksum runs locally, if possible.
3300 (zerop (tramp-compat-call-process "cksum" tmpfile t))
3301 ;; cksum runs remotely.
3302 (tramp-send-command-and-check
3305 "cksum <%s" (tramp-shell-quote-argument localname)))
3306 ;; ... they are different.
3310 (with-current-buffer (tramp-get-buffer v)
3314 (concat "Couldn't write region to `%s',"
3315 " decode using `%s' failed")
3316 filename rem-dec)))))
3319 (delete-file tmpfile)))
3321 ;; That's not expected.
3325 (concat "Method `%s' should specify both encoding and "
3326 "decoding command or an rcp program")
3329 ;; Make `last-coding-system-used' have the right value.
3330 (when coding-system-used
3331 (set 'last-coding-system-used coding-system-used))))
3333 (tramp-flush-file-property v (file-name-directory localname))
3334 (tramp-flush-file-property v localname)
3336 ;; We must protect `last-coding-system-used', now we have set it
3337 ;; to its correct value.
3338 (let (last-coding-system-used (need-chown t))
3339 ;; Set file modification time.
3340 (when (or (eq visit t) (stringp visit))
3341 (let ((file-attr (tramp-compat-file-attributes filename 'integer)))
3342 (set-visited-file-modtime
3343 ;; We must pass modtime explicitly, because filename can
3344 ;; be different from (buffer-file-name), f.e. if
3345 ;; `file-precious-flag' is set.
3347 (when (and (= (nth 2 file-attr) uid)
3348 (= (nth 3 file-attr) gid))
3349 (setq need-chown nil))))
3351 ;; Set the ownership.
3353 (tramp-set-file-uid-gid filename uid gid))
3354 (when (or (eq visit t) (null visit) (stringp visit))
3355 (tramp-message v 0 "Wrote %s" filename))
3356 (run-hooks 'tramp-handle-write-region-hook)))))
3358 (defvar tramp-vc-registered-file-names nil
3359 "List used to collect file names, which are checked during `vc-registered'.")
3361 ;; VC backends check for the existence of various different special
3362 ;; files. This is very time consuming, because every single check
3363 ;; requires a remote command (the file cache must be invalidated).
3364 ;; Therefore, we apply a kind of optimization. We install the file
3365 ;; name handler `tramp-vc-file-name-handler', which does nothing but
3366 ;; remembers all file names for which `file-exists-p' or
3367 ;; `file-readable-p' has been applied. A first run of `vc-registered'
3368 ;; is performed. Afterwards, a script is applied for all collected
3369 ;; file names, using just one remote command. The result of this
3370 ;; script is used to fill the file cache with actual values. Now we
3371 ;; can reset the file name handlers, and we make a second run of
3372 ;; `vc-registered', which returns the expected result without sending
3373 ;; any other remote command.
3374 (defun tramp-sh-handle-vc-registered (file)
3375 "Like `vc-registered' for Tramp files."
3376 (tramp-compat-with-temp-message ""
3377 (with-parsed-tramp-file-name file nil
3378 (with-tramp-progress-reporter
3379 v 3 (format "Checking `vc-registered' for %s" file)
3381 ;; There could be new files, created by the vc backend. We
3382 ;; cannot reuse the old cache entries, therefore.
3383 (let (tramp-vc-registered-file-names
3384 (remote-file-name-inhibit-cache (current-time))
3385 (file-name-handler-alist
3386 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
3388 ;; Here we collect only file names, which need an operation.
3389 (ignore-errors (tramp-run-real-handler 'vc-registered (list file)))
3390 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
3392 ;; Send just one command, in order to fill the cache.
3393 (when tramp-vc-registered-file-names
3394 (tramp-maybe-send-script
3396 (format tramp-vc-registered-read-file-names
3397 (tramp-get-file-exists-command v)
3398 (format "%s -r" (tramp-get-test-command v)))
3399 "tramp_vc_registered_read_file_names")
3403 (tramp-send-command-and-read
3406 "tramp_vc_registered_read_file_names <<'EOF'\n%s\nEOF\n"
3407 (mapconcat 'tramp-shell-quote-argument
3408 tramp-vc-registered-file-names
3411 (tramp-set-file-property
3412 v (car elt) (cadr elt) (cadr (cdr elt))))))
3414 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3415 ;; calls shall be answered from the file cache. We unset
3416 ;; `process-file-side-effects' in order to keep the cache when
3417 ;; `process-file' calls appear.
3418 (let (process-file-side-effects)
3419 (tramp-run-real-handler 'vc-registered (list file)))))))
3421 ;;;###tramp-autoload
3422 (defun tramp-sh-file-name-handler (operation &rest args)
3423 "Invoke remote-shell Tramp file name handler.
3424 Fall back to normal file name handler if no Tramp handler exists."
3425 (when (and tramp-locked (not tramp-locker))
3426 (setq tramp-locked nil)
3427 (signal 'file-error (list "Forbidden reentrant call of Tramp")))
3428 (let ((tl tramp-locked))
3431 (setq tramp-locked t)
3432 (let ((tramp-locker t))
3434 (let ((fn (assoc operation tramp-sh-file-name-handler-alist)))
3436 (apply (cdr fn) args)
3437 (tramp-run-real-handler operation args))))))
3438 (setq tramp-locked tl))))
3440 (defun tramp-vc-file-name-handler (operation &rest args)
3441 "Invoke special file name handler, which collects files to be handled."
3444 (tramp-replace-environment-variables
3445 (apply 'tramp-file-name-for-operation operation args)))
3446 (fn (assoc operation tramp-sh-file-name-handler-alist)))
3447 (with-parsed-tramp-file-name filename nil
3449 ;; That's what we want: file names, for which checks are
3450 ;; applied. We assume that VC uses only `file-exists-p' and
3451 ;; `file-readable-p' checks; otherwise we must extend the
3452 ;; list. We do not perform any action, but return nil, in
3453 ;; order to keep `vc-registered' running.
3454 ((and fn (memq operation '(file-exists-p file-readable-p)))
3455 (add-to-list 'tramp-vc-registered-file-names localname 'append)
3457 ;; `process-file' and `start-file-process' shall be ignored.
3458 ((and fn (eq operation 'process-file) 0))
3459 ((and fn (eq operation 'start-file-process) nil))
3460 ;; Tramp file name handlers like `expand-file-name'. They
3462 (fn (save-match-data (apply (cdr fn) args)))
3463 ;; Default file name handlers, we don't care.
3464 (t (tramp-run-real-handler operation args)))))))
3466 ;;; Internal Functions:
3468 (defun tramp-maybe-send-script (vec script name)
3469 "Define in remote shell function NAME implemented as SCRIPT.
3470 Only send the definition if it has not already been done."
3471 ;; We cannot let-bind (tramp-get-connection-process vec) because it
3473 (let ((scripts (tramp-get-connection-property
3474 (tramp-get-connection-process vec) "scripts" nil)))
3475 (unless (member name scripts)
3476 (with-tramp-progress-reporter vec 5 (format "Sending script `%s'" name)
3477 ;; The script could contain a call of Perl. This is masked with `%s'.
3478 (tramp-barf-unless-okay
3480 (format "%s () {\n%s\n}" name
3481 (format script (tramp-get-remote-perl vec)))
3482 "Script %s sending failed" name)
3483 (tramp-set-connection-property
3484 (tramp-get-connection-process vec) "scripts" (cons name scripts))))))
3486 (defun tramp-set-auto-save ()
3487 (when (and ;; ange-ftp has its own auto-save mechanism
3488 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
3489 'tramp-sh-file-name-handler)
3491 (auto-save-mode 1)))
3492 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
3493 (add-hook 'tramp-unload-hook
3495 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
3497 (defun tramp-run-test (switch filename)
3498 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3499 Returns the exit code of the `test' program."
3500 (with-parsed-tramp-file-name filename nil
3501 (tramp-send-command-and-check
3505 (tramp-get-test-command v)
3507 (tramp-shell-quote-argument localname)))))
3509 (defun tramp-run-test2 (format-string file1 file2)
3510 "Run `test'-like program on the remote system, given FILE1, FILE2.
3511 FORMAT-STRING contains the program name, switches, and place holders.
3512 Returns the exit code of the `test' program. Barfs if the methods,
3513 hosts, or files, disagree."
3514 (unless (tramp-equal-remote file1 file2)
3515 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
3518 "tramp-run-test2 only implemented for same method, user, host")))
3519 (with-parsed-tramp-file-name file1 v1
3520 (with-parsed-tramp-file-name file1 v2
3521 (tramp-send-command-and-check
3523 (format format-string
3524 (tramp-shell-quote-argument v1-localname)
3525 (tramp-shell-quote-argument v2-localname))))))
3527 (defun tramp-find-executable
3528 (vec progname dirlist &optional ignore-tilde ignore-path)
3529 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3530 First arg VEC specifies the connection, PROGNAME is the program
3531 to search for, and DIRLIST gives the list of directories to
3532 search. If IGNORE-TILDE is non-nil, directory names starting
3533 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3536 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3538 This function expects to be in the right *tramp* buffer."
3539 (with-current-buffer (tramp-get-connection-buffer vec)
3541 ;; Check whether the executable is in $PATH. "which(1)" does not
3542 ;; report always a correct error code; therefore we check the
3543 ;; number of words it returns.
3545 (tramp-send-command vec (format "which \\%s | wc -w" progname))
3546 (goto-char (point-min))
3547 (if (looking-at "^\\s-*1$")
3548 (setq result (concat "\\" progname))))
3551 ;; Remove all ~/foo directories from dirlist. In XEmacs,
3552 ;; `remove' is in CL, and we want to avoid CL dependencies.
3555 (setq d (car dirlist))
3556 (setq dirlist (cdr dirlist))
3557 (unless (char-equal ?~ (aref d 0))
3558 (setq newdl (cons d newdl))))
3559 (setq dirlist (nreverse newdl))))
3562 (format (concat "while read d; "
3563 "do if test -x $d/%s -a -f $d/%s; "
3564 "then echo tramp_executable $d/%s; "
3565 "break; fi; done <<'EOF'\n"
3567 progname progname progname (mapconcat 'identity dirlist "\n")))
3568 (goto-char (point-max))
3569 (when (search-backward "tramp_executable " nil t)
3570 (skip-chars-forward "^ ")
3571 (skip-chars-forward " ")
3572 (setq result (buffer-substring (point) (point-at-eol)))))
3575 (defun tramp-set-remote-path (vec)
3576 "Sets the remote environment PATH to existing directories.
3577 I.e., for each directory in `tramp-remote-path', it is tested
3578 whether it exists and if so, it is added to the environment
3580 (tramp-message vec 5 (format "Setting $PATH environment variable"))
3582 vec (format "PATH=%s; export PATH"
3583 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
3585 ;; ------------------------------------------------------------
3586 ;; -- Communication with external shell --
3587 ;; ------------------------------------------------------------
3589 (defun tramp-find-file-exists-command (vec)
3590 "Find a command on the remote host for checking if a file exists.
3591 Here, we are looking for a command which has zero exit status if the
3592 file exists and nonzero exit status otherwise."
3593 (let ((existing "/")
3595 (tramp-shell-quote-argument "/ this file does not exist "))
3597 ;; The algorithm is as follows: we try a list of several commands.
3598 ;; For each command, we first run `$cmd /' -- this should return
3599 ;; true, as the root directory always exists. And then we run
3600 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
3601 ;; does not exist. This should return false. We use the first
3602 ;; command we find that seems to work.
3603 ;; The list of commands to try is as follows:
3604 ;; `ls -d' This works on most systems, but NetBSD 1.4
3605 ;; has a bug: `ls' always returns zero exit
3606 ;; status, even for files which don't exist.
3607 ;; `test -e' Some Bourne shells have a `test' builtin
3608 ;; which does not know the `-e' option.
3609 ;; `/bin/test -e' For those, the `test' binary on disk normally
3610 ;; provides the option. Alas, the binary
3611 ;; is sometimes `/bin/test' and sometimes it's
3613 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3616 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
3617 (tramp-send-command-and-check
3618 vec (format "%s %s" result existing))
3619 (not (tramp-send-command-and-check
3620 vec (format "%s %s" result nonexistent)))))
3622 (and (setq result "/bin/test -e")
3623 (tramp-send-command-and-check
3624 vec (format "%s %s" result existing))
3625 (not (tramp-send-command-and-check
3626 vec (format "%s %s" result nonexistent)))))
3628 (and (setq result "/usr/bin/test -e")
3629 (tramp-send-command-and-check
3630 vec (format "%s %s" result existing))
3631 (not (tramp-send-command-and-check
3632 vec (format "%s %s" result nonexistent)))))
3634 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
3635 (tramp-send-command-and-check
3636 vec (format "%s %s" result existing))
3637 (not (tramp-send-command-and-check
3638 vec (format "%s %s" result nonexistent))))))
3640 vec 'file-error "Couldn't find command to check if file exists"))
3643 (defun tramp-open-shell (vec shell)
3644 "Opens shell SHELL."
3645 (with-tramp-progress-reporter
3646 vec 5 (format "Opening remote shell `%s'" shell)
3647 ;; Find arguments for this shell.
3648 (let ((tramp-end-of-output tramp-initial-end-of-output)
3649 (alist tramp-sh-extra-args)
3651 (while (and alist (null extra-args))
3652 (setq item (pop alist))
3653 (when (string-match (car item) shell)
3654 (setq extra-args (cdr item))))
3657 "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s"
3658 (tramp-shell-quote-argument tramp-end-of-output)
3659 shell (or extra-args ""))
3661 (tramp-set-connection-property
3662 (tramp-get-connection-process vec) "remote-shell" shell)
3665 vec (format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output)) t)
3666 (tramp-send-command vec "PS2=''" t)
3667 (tramp-send-command vec "PS3=''" t)
3668 (tramp-send-command vec "PROMPT_COMMAND=''" t)))
3670 (defun tramp-find-shell (vec)
3671 "Opens a shell on the remote host which groks tilde expansion."
3672 (with-current-buffer (tramp-get-buffer vec)
3673 (let ((default-shell
3675 (tramp-get-connection-property
3676 (tramp-get-connection-process vec) "remote-shell" nil)
3677 (tramp-get-method-parameter
3678 (tramp-file-name-method vec) 'tramp-remote-shell)))
3681 (with-tramp-connection-property vec "remote-shell"
3682 ;; CCC: "root" does not exist always, see QNAP 459.
3683 ;; Which check could we apply instead?
3684 (tramp-send-command vec "echo ~root" t)
3685 (if (or (string-match "^~root$" (buffer-string))
3686 ;; The default shell (ksh93) of OpenSolaris and
3687 ;; Solaris is buggy. We've got reports for
3688 ;; "SunOS 5.10" and "SunOS 5.11" so far.
3689 (string-match (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3690 (tramp-get-connection-property
3693 (or (tramp-find-executable
3694 vec "bash" (tramp-get-remote-path vec) t t)
3695 (tramp-find-executable
3696 vec "ksh" (tramp-get-remote-path vec) t t)
3697 ;; Maybe it works at least for some other commands.
3703 "Couldn't find a remote shell which groks tilde "
3704 "expansion, using `%s'")
3709 ;; Open a new shell if needed.
3710 (unless (string-equal shell default-shell)
3712 vec 5 "Starting remote shell `%s' for tilde expansion" shell)
3713 (tramp-open-shell vec shell))
3715 ;; Busyboxes tend to behave strange. We check for the existence.
3716 (with-tramp-connection-property vec "busybox"
3717 (tramp-send-command vec (format "%s --version" shell) t)
3718 (let ((case-fold-search t))
3719 (and (string-match "busybox" (buffer-string)) t))))))
3721 ;; Utility functions.
3723 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
3724 "Wait for shell prompt and barf if none appears.
3725 Looks at process PROC to see if a shell prompt appears in TIMEOUT
3726 seconds. If not, it produces an error message with the given ERROR-ARGS."
3728 (tramp-wait-for-regexp
3731 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
3732 (apply 'tramp-error-with-buffer nil proc 'file-error error-args)))
3734 (defun tramp-open-connection-setup-interactive-shell (proc vec)
3735 "Set up an interactive shell.
3736 Mainly sets the prompt and the echo correctly. PROC is the shell
3737 process to set up. VEC specifies the connection."
3738 (let ((tramp-end-of-output tramp-initial-end-of-output))
3739 ;; It is useful to set the prompt in the following command because
3740 ;; some people have a setting for $PS1 which /bin/sh doesn't know
3741 ;; about and thus /bin/sh will display a strange prompt. For
3742 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
3743 ;; display the current working directory but /bin/sh will display
3744 ;; a dollar sign. The following command line sets $PS1 to a sane
3745 ;; value, and works under Bourne-ish shells as well as csh-like
3746 ;; shells. Daniel Pittman reports that the unusual positioning of
3747 ;; the single quotes makes it work under `rc', too. We also unset
3748 ;; the variable $ENV because that is read by some sh
3749 ;; implementations (eg, bash when called as sh) on startup; this
3750 ;; way, we avoid the startup file clobbering $PS1. $PROMPT_COMMAND
3751 ;; is another way to set the prompt in /bin/bash, it must be
3752 ;; discarded as well.
3755 (or (tramp-get-connection-property vec "remote-shell" nil)
3756 (tramp-get-method-parameter
3757 (tramp-file-name-method vec) 'tramp-remote-shell)))
3760 (tramp-message vec 5 "Setting up remote shell environment")
3761 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
3762 ;; Check whether the echo has really been disabled. Some
3763 ;; implementations, like busybox of embedded GNU/Linux, don't
3764 ;; support disabling.
3765 (tramp-send-command vec "echo foo" t)
3766 (with-current-buffer (process-buffer proc)
3767 (goto-char (point-min))
3768 (when (looking-at "echo foo")
3769 (tramp-set-connection-property proc "remote-echo" t)
3770 (tramp-message vec 5 "Remote echo still on. Ok.")
3771 ;; Make sure backspaces and their echo are enabled and no line
3772 ;; width magic interferes with them.
3773 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
3775 (tramp-message vec 5 "Setting shell prompt")
3777 vec (format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output)) t)
3778 (tramp-send-command vec "PS2=''" t)
3779 (tramp-send-command vec "PS3=''" t)
3780 (tramp-send-command vec "PROMPT_COMMAND=''" t)
3782 ;; Try to set up the coding system correctly.
3783 ;; CCC this can't be the right way to do it. Hm.
3784 (tramp-message vec 5 "Determining coding system")
3785 (tramp-send-command vec "echo foo ; echo bar" t)
3786 (with-current-buffer (process-buffer proc)
3787 (goto-char (point-min))
3788 (if (featurep 'mule)
3789 ;; Use MULE to select the right EOL convention for communicating
3790 ;; with the process.
3791 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc)
3792 (cons 'undecided 'undecided)))
3793 cs-decode cs-encode)
3794 (when (symbolp cs) (setq cs (cons cs cs)))
3795 (setq cs-decode (car cs))
3796 (setq cs-encode (cdr cs))
3797 (unless cs-decode (setq cs-decode 'undecided))
3798 (unless cs-encode (setq cs-encode 'undecided))
3799 (setq cs-encode (tramp-compat-coding-system-change-eol-conversion
3801 (when (search-forward "\r" nil t)
3802 (setq cs-decode (tramp-compat-coding-system-change-eol-conversion
3804 (tramp-compat-funcall
3805 'set-buffer-process-coding-system cs-decode cs-encode)
3807 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
3808 ;; Look for ^M and do something useful if found.
3809 (when (search-forward "\r" nil t)
3810 ;; We have found a ^M but cannot frob the process coding system
3811 ;; because we're running on a non-MULE Emacs. Let's try
3813 (tramp-send-command vec "stty -onlcr" t))))
3815 (tramp-send-command vec "set +o vi +o emacs" t)
3817 ;; Check whether the output of "uname -sr" has been changed. If
3818 ;; yes, this is a strong indication that we must expire all
3819 ;; connection properties. We start again with
3820 ;; `tramp-maybe-open-connection', it will be caught there.
3821 (tramp-message vec 5 "Checking system information")
3822 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
3824 (tramp-set-connection-property
3826 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
3827 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
3831 "Connection reset, because remote host changed from `%s' to `%s'"
3832 old-uname new-uname)
3833 (throw 'uname-changed (tramp-maybe-open-connection vec))))
3835 ;; Check whether the remote host suffers from buggy
3836 ;; `send-process-string'. This is known for FreeBSD (see comment in
3837 ;; `send_process', file process.c). I've tested sending 624 bytes
3838 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
3839 ;; this host type is detected locally. It cannot handle remote
3841 (with-tramp-connection-property proc "chunksize"
3843 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
3847 vec 5 "Checking remote host type for `send-process-string' bug")
3849 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
3852 ;; Set remote PATH variable.
3853 (tramp-set-remote-path vec)
3855 ;; Search for a good shell before searching for a command which
3856 ;; checks if a file exists. This is done because Tramp wants to use
3857 ;; "test foo; echo $?" to check if various conditions hold, and
3858 ;; there are buggy /bin/sh implementations which don't execute the
3859 ;; "echo $?" part if the "test" part has an error. In particular,
3860 ;; the OpenSolaris /bin/sh is a problem. There are also other
3861 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
3862 ;; in function declarations, or changing HISTFILE in place.
3863 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
3865 (tramp-find-shell vec)
3867 ;; Disable unexpected output.
3868 (tramp-send-command vec "mesg n; biff n" t)
3870 ;; IRIX64 bash expands "!" even when in single quotes. This
3871 ;; destroys our shell functions, we must disable it. See
3872 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
3873 (when (string-match "^IRIX64" (tramp-get-connection-property vec "uname" ""))
3874 (tramp-send-command vec "set +H" t))
3876 ;; On BSD-like systems, ?\t is expanded to spaces. Suppress this.
3877 (when (string-match "BSD\\|Darwin"
3878 (tramp-get-connection-property vec "uname" ""))
3879 (tramp-send-command vec "stty -oxtabs" t))
3881 ;; Set `remote-tty' process property.
3882 (let ((tty (tramp-send-command-and-read vec "echo \\\"`tty`\\\"" 'noerror)))
3883 (unless (zerop (length tty))
3884 (tramp-compat-process-put proc 'remote-tty tty)))
3886 ;; Dump stty settings in the traces.
3887 (when (>= tramp-verbose 9)
3888 (tramp-send-command vec "stty -a" t))
3890 ;; Set the environment.
3891 (tramp-message vec 5 "Setting default environment")
3893 (let ((env (copy-sequence tramp-remote-process-environment))
3896 (setq item (tramp-compat-split-string (car env) "="))
3897 (setcdr item (mapconcat 'identity (cdr item) "="))
3898 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
3900 vec (format "%s=%s; export %s" (car item) (cdr item) (car item)) t)
3901 (push (car item) unset))
3902 (setq env (cdr env)))
3905 vec (format "unset %s" (mapconcat 'identity unset " ")) t))))
3907 ;; CCC: We should either implement a Perl version of base64 encoding
3908 ;; and decoding. Then we just use that in the last item. The other
3909 ;; alternative is to use the Perl version of UU encoding. But then
3910 ;; we need a Lisp version of uuencode.
3912 ;; Old text from documentation of tramp-methods:
3913 ;; Using a uuencode/uudecode inline method is discouraged, please use one
3914 ;; of the base64 methods instead since base64 encoding is much more
3915 ;; reliable and the commands are more standardized between the different
3916 ;; Unix versions. But if you can't use base64 for some reason, please
3917 ;; note that the default uudecode command does not work well for some
3918 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
3919 ;; the following command for uudecode:
3921 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
3923 ;; For Irix, no solution is known yet.
3925 (autoload 'uudecode-decode-region "uudecode")
3927 (defconst tramp-local-coding-commands
3928 '((b64 base64-encode-region base64-decode-region)
3929 (uu tramp-uuencode-region uudecode-decode-region)
3931 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
3932 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
3933 "List of local coding commands for inline transfer.
3934 Each item is a list that looks like this:
3936 \(FORMAT ENCODING DECODING\)
3938 FORMAT is symbol describing the encoding/decoding format. It can be
3939 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3941 ENCODING and DECODING can be strings, giving commands, or symbols,
3942 giving functions. If they are strings, then they can contain
3943 the \"%s\" format specifier. If that specifier is present, the input
3944 filename will be put into the command line at that spot. If the
3945 specifier is not present, the input should be read from standard
3948 If they are functions, they will be called with two arguments, start
3949 and end of region, and are expected to replace the region contents
3950 with the encoded or decoded results, respectively.")
3952 (defconst tramp-remote-coding-commands
3953 '((b64 "base64" "base64 -d -i")
3954 ;; "-i" is more robust with older base64 from GNU coreutils.
3955 ;; However, I don't know whether all base64 versions do supports
3957 (b64 "base64" "base64 -d")
3958 (b64 "mimencode -b" "mimencode -u -b")
3959 (b64 "mmencode -b" "mmencode -u -b")
3960 (b64 "recode data..base64" "recode base64..data")
3961 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
3962 (b64 tramp-perl-encode tramp-perl-decode)
3963 (uu "uuencode xxx" "uudecode -o /dev/stdout" "test -c /dev/stdout")
3964 (uu "uuencode xxx" "uudecode -o -")
3965 (uu "uuencode xxx" "uudecode -p")
3966 (uu "uuencode xxx" tramp-uudecode)
3968 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
3969 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
3970 "List of remote coding commands for inline transfer.
3971 Each item is a list that looks like this:
3973 \(FORMAT ENCODING DECODING [TEST]\)
3975 FORMAT is symbol describing the encoding/decoding format. It can be
3976 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3978 ENCODING and DECODING can be strings, giving commands, or symbols,
3979 giving variables. If they are strings, then they can contain
3980 the \"%s\" format specifier. If that specifier is present, the input
3981 filename will be put into the command line at that spot. If the
3982 specifier is not present, the input should be read from standard
3985 If they are variables, this variable is a string containing a Perl
3986 implementation for this functionality. This Perl program will be transferred
3987 to the remote host, and it is available as shell function with the same name.
3989 The optional TEST command can be used for further tests, whether
3990 ENCODING and DECODING are applicable.")
3992 (defun tramp-find-inline-encoding (vec)
3993 "Find an inline transfer encoding that works.
3994 Goes through the list `tramp-local-coding-commands' and
3995 `tramp-remote-coding-commands'."
3997 (let ((local-commands tramp-local-coding-commands)
3999 (p (tramp-get-connection-process vec))
4000 loc-enc loc-dec rem-enc rem-dec rem-test litem ritem found)
4001 (while (and local-commands (not found))
4002 (setq litem (pop local-commands))
4003 (catch 'wont-work-local
4004 (let ((format (nth 0 litem))
4005 (remote-commands tramp-remote-coding-commands))
4006 (setq loc-enc (nth 1 litem))
4007 (setq loc-dec (nth 2 litem))
4008 ;; If the local encoder or decoder is a string, the
4009 ;; corresponding command has to work locally.
4010 (if (not (stringp loc-enc))
4012 vec 5 "Checking local encoding function `%s'" loc-enc)
4014 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
4015 (unless (zerop (tramp-call-local-coding-command
4017 (throw 'wont-work-local nil)))
4018 (if (not (stringp loc-dec))
4020 vec 5 "Checking local decoding function `%s'" loc-dec)
4022 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
4023 (unless (zerop (tramp-call-local-coding-command
4025 (throw 'wont-work-local nil)))
4026 ;; Search for remote coding commands with the same format
4027 (while (and remote-commands (not found))
4028 (setq ritem (pop remote-commands))
4029 (catch 'wont-work-remote
4030 (when (equal format (nth 0 ritem))
4031 (setq rem-enc (nth 1 ritem))
4032 (setq rem-dec (nth 2 ritem))
4033 (setq rem-test (nth 3 ritem))
4034 ;; Check the remote test command if exists.
4035 (when (stringp rem-test)
4037 vec 5 "Checking remote test command `%s'" rem-test)
4038 (unless (tramp-send-command-and-check vec rem-test t)
4039 (throw 'wont-work-remote nil)))
4040 ;; Check if remote encoding and decoding commands can be
4041 ;; called remotely with null input and output. This makes
4042 ;; sure there are no syntax errors and the command is really
4043 ;; found. Note that we do not redirect stdout to /dev/null,
4044 ;; for two reasons: when checking the decoding command, we
4045 ;; actually check the output it gives. And also, when
4046 ;; redirecting "mimencode" output to /dev/null, then as root
4047 ;; it might change the permissions of /dev/null!
4048 (when (not (stringp rem-enc))
4049 (let ((name (symbol-name rem-enc)))
4050 (while (string-match (regexp-quote "-") name)
4051 (setq name (replace-match "_" nil t name)))
4052 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
4053 (setq rem-enc name)))
4056 "Checking remote encoding command `%s' for sanity" rem-enc)
4057 (unless (tramp-send-command-and-check
4058 vec (format "%s </dev/null" rem-enc) t)
4059 (throw 'wont-work-remote nil))
4061 (when (not (stringp rem-dec))
4062 (let ((name (symbol-name rem-dec)))
4063 (while (string-match (regexp-quote "-") name)
4064 (setq name (replace-match "_" nil t name)))
4065 (tramp-maybe-send-script vec (symbol-value rem-dec) name)
4066 (setq rem-dec name)))
4069 "Checking remote decoding command `%s' for sanity" rem-dec)
4070 (unless (tramp-send-command-and-check
4072 (format "echo %s | %s | %s" magic rem-enc rem-dec)
4074 (throw 'wont-work-remote nil))
4076 (with-current-buffer (tramp-get-buffer vec)
4077 (goto-char (point-min))
4078 (unless (looking-at (regexp-quote magic))
4079 (throw 'wont-work-remote nil)))
4081 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
4082 (setq rem-enc (nth 1 ritem))
4083 (setq rem-dec (nth 2 ritem))
4084 (setq found t)))))))
4086 ;; Did we find something?
4089 vec 'file-error "Couldn't find an inline transfer encoding"))
4091 ;; Set connection properties. Since the commands are risky (due
4092 ;; to output direction), we cache them in the process cache.
4093 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
4094 (tramp-set-connection-property p "local-encoding" loc-enc)
4095 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
4096 (tramp-set-connection-property p "local-decoding" loc-dec)
4097 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
4098 (tramp-set-connection-property p "remote-encoding" rem-enc)
4099 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
4100 (tramp-set-connection-property p "remote-decoding" rem-dec))))
4102 (defun tramp-call-local-coding-command (cmd input output)
4103 "Call the local encoding or decoding command.
4104 If CMD contains \"%s\", provide input file INPUT there in command.
4105 Otherwise, INPUT is passed via standard input.
4106 INPUT can also be nil which means `/dev/null'.
4107 OUTPUT can be a string (which specifies a filename), or t (which
4108 means standard output and thus the current buffer), or nil (which
4110 (tramp-compat-call-process
4111 tramp-encoding-shell
4112 (when (and input (not (string-match "%s" cmd))) input)
4113 (if (eq output t) t nil)
4115 tramp-encoding-command-switch
4117 (if (string-match "%s" cmd) (format cmd input) cmd)
4118 (if (stringp output) (concat "> " output) ""))))
4120 (defconst tramp-inline-compress-commands
4121 '(("gzip" "gzip -d")
4122 ("bzip2" "bzip2 -d")
4124 ("compress" "compress -d"))
4125 "List of compress and decompress commands for inline transfer.
4126 Each item is a list that looks like this:
4128 \(COMPRESS DECOMPRESS\)
4130 COMPRESS or DECOMPRESS are strings with the respective commands.")
4132 (defun tramp-find-inline-compress (vec)
4133 "Find an inline transfer compress command that works.
4134 Goes through the list `tramp-inline-compress-commands'."
4136 (let ((commands tramp-inline-compress-commands)
4138 (p (tramp-get-connection-process vec))
4139 item compress decompress found)
4140 (while (and commands (not found))
4142 (setq item (pop commands)
4143 compress (nth 0 item)
4144 decompress (nth 1 item))
4147 "Checking local compress command `%s', `%s' for sanity"
4148 compress decompress)
4151 (tramp-call-local-coding-command
4153 ;; Windows shells need the program file name after
4154 ;; the pipe symbol be quoted if they use forward
4155 ;; slashes as directory separators.
4156 (if (memq system-type '(windows-nt))
4157 "echo %s | \"%s\" | \"%s\""
4158 "echo %s | %s | %s")
4159 magic compress decompress) nil nil))
4163 "Checking remote compress command `%s', `%s' for sanity"
4164 compress decompress)
4165 (unless (tramp-send-command-and-check
4166 vec (format "echo %s | %s | %s" magic compress decompress) t)
4170 ;; Did we find something?
4173 ;; Set connection properties. Since the commands are
4174 ;; risky (due to output direction), we cache them in the
4177 vec 5 "Using inline transfer compress command `%s'" compress)
4178 (tramp-set-connection-property p "inline-compress" compress)
4180 vec 5 "Using inline transfer decompress command `%s'" decompress)
4181 (tramp-set-connection-property p "inline-decompress" decompress))
4183 (tramp-set-connection-property p "inline-compress" nil)
4184 (tramp-set-connection-property p "inline-decompress" nil)
4186 vec 2 "Couldn't find an inline transfer compress command")))))
4188 (defvar tramp-gw-tunnel-method)
4189 (defvar tramp-gw-socks-method)
4191 (defun tramp-compute-multi-hops (vec)
4192 "Expands VEC according to `tramp-default-proxies-alist'.
4193 Gateway hops are already opened."
4194 (let ((target-alist `(,vec))
4195 (hops (or (tramp-file-name-hop vec) ""))
4199 ;; Ad-hoc proxy definitions.
4200 (dolist (proxy (reverse (split-string hops tramp-postfix-hop-regexp 'omit)))
4201 (let ((user (tramp-file-name-user item))
4202 (host (tramp-file-name-host item))
4204 tramp-prefix-format proxy tramp-postfix-host-format)))
4206 vec 5 "Add proxy (\"%s\" \"%s\" \"%s\")"
4207 (and (stringp host) (regexp-quote host))
4208 (and (stringp user) (regexp-quote user))
4212 'tramp-default-proxies-alist
4213 (list (and (stringp host) (regexp-quote host))
4214 (and (stringp user) (regexp-quote user))
4216 (setq item (tramp-dissect-file-name proxy))))
4217 ;; Save the new value.
4218 (when (and hops tramp-save-ad-hoc-proxies)
4219 (customize-save-variable
4220 'tramp-default-proxies-alist tramp-default-proxies-alist))
4222 ;; Look for proxy hosts to be passed.
4223 (setq choices tramp-default-proxies-alist)
4225 (setq item (pop choices)
4226 proxy (eval (nth 2 item)))
4229 (string-match (or (eval (nth 0 item)) "")
4230 (or (tramp-file-name-host (car target-alist)) ""))
4232 (string-match (or (eval (nth 1 item)) "")
4233 (or (tramp-file-name-user (car target-alist)) "")))
4235 ;; No more hops needed.
4237 ;; Replace placeholders.
4242 ?u (or (tramp-file-name-user (car target-alist)) "")
4243 ?h (or (tramp-file-name-host (car target-alist)) ""))))
4244 (with-parsed-tramp-file-name proxy l
4246 (add-to-list 'target-alist l)
4247 ;; Start next search.
4248 (setq choices tramp-default-proxies-alist)))))
4251 (when (and (boundp 'tramp-gw-tunnel-method) (boundp 'tramp-gw-socks-method)
4254 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method)
4255 (tramp-file-name-method (car target-alist))))
4256 (let ((gw (pop target-alist))
4257 (hop (pop target-alist)))
4258 ;; Is the method prepared for gateways?
4259 (unless (tramp-file-name-port hop)
4262 "Connection `%s' is not supported for gateway access." hop))
4263 ;; Open the gateway connection.
4267 (tramp-file-name-method hop) (tramp-file-name-user hop)
4268 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop) nil nil))
4269 ;; For the password prompt, we need the correct values.
4270 ;; Therefore, we must remember the gateway vector. But we
4271 ;; cannot do it as connection property, because it shouldn't
4272 ;; be persistent. And we have no started process yet either.
4273 (tramp-set-file-property (car target-alist) "" "gateway" hop)))
4275 ;; Foreign and out-of-band methods are not supported for multi-hops.
4276 (when (cdr target-alist)
4277 (setq choices target-alist)
4279 (setq item (pop choices))
4283 (tramp-get-method-parameter
4284 (tramp-file-name-method item) 'tramp-login-program))
4285 (tramp-get-method-parameter
4286 (tramp-file-name-method item) 'tramp-copy-program))
4289 "Method `%s' is not supported for multi-hops."
4290 (tramp-file-name-method item)))))
4292 ;; In case the host name is not used for the remote shell
4293 ;; command, the user could be misguided by applying a random
4295 (let* ((v (car target-alist))
4296 (method (tramp-file-name-method v))
4297 (host (tramp-file-name-host v)))
4300 ;; There are multi-hops.
4302 ;; The host name is used for the remote shell command.
4304 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
4305 ;; The host is local. We cannot use `tramp-local-host-p'
4306 ;; here, because it opens a connection as well.
4307 (string-match tramp-local-host-regexp host))
4310 "Host `%s' looks like a remote host, `%s' can only use the local host"
4316 (defvar tramp-current-connection nil
4317 "Last connection timestamp.")
4319 (defun tramp-maybe-open-connection (vec)
4320 "Maybe open a connection VEC.
4321 Does not do anything if a connection is already open, but re-opens the
4322 connection if a previous connection has died for some reason."
4323 (catch 'uname-changed
4324 (let ((p (tramp-get-connection-process vec))
4325 (process-name (tramp-get-connection-property vec "process-name" nil))
4326 (process-environment (copy-sequence process-environment))
4327 (pos (with-current-buffer (tramp-get-connection-buffer vec) (point))))
4329 ;; If Tramp opens the same connection within a short time frame,
4330 ;; there is a problem. We shall signal this.
4331 (unless (or (and p (processp p) (memq (process-status p) '(run open)))
4332 (not (equal (butlast (append vec nil))
4333 (car tramp-current-connection)))
4335 (current-time) (cdr tramp-current-connection))
4336 (or tramp-connection-min-time-diff 0)))
4337 (throw 'suppress 'suppress))
4339 ;; If too much time has passed since last command was sent, look
4340 ;; whether process is still alive. If it isn't, kill it. When
4341 ;; using ssh, it can sometimes happen that the remote end has
4342 ;; hung up but the local ssh client doesn't recognize this until
4343 ;; it tries to send some data to the remote end. So that's why
4344 ;; we try to send a command from time to time, then look again
4345 ;; whether the process is really alive.
4347 (when (and (> (tramp-time-diff
4349 (tramp-get-connection-property
4350 p "last-cmd-time" '(0 0 0)))
4352 p (processp p) (memq (process-status p) '(run open)))
4353 (tramp-send-command vec "echo are you awake" t t)
4354 (unless (and (memq (process-status p) '(run open))
4355 (tramp-wait-for-output p 10))
4356 ;; The error will be caught locally.
4357 (tramp-error vec 'file-error "Awake did fail")))
4362 ;; New connection must be opened.
4364 (unless (and p (processp p) (memq (process-status p) '(run open)))
4366 ;; We call `tramp-get-buffer' in order to get a debug
4367 ;; buffer for messages from the beginning.
4368 (tramp-get-buffer vec)
4370 ;; If `non-essential' is non-nil, don't reopen a new connection.
4371 (when (and (boundp 'non-essential) (symbol-value 'non-essential))
4372 (throw 'non-essential 'non-essential))
4374 (with-tramp-progress-reporter
4376 (if (zerop (length (tramp-file-name-user vec)))
4377 (format "Opening connection for %s using %s"
4378 (tramp-file-name-host vec)
4379 (tramp-file-name-method vec))
4380 (format "Opening connection for %s@%s using %s"
4381 (tramp-file-name-user vec)
4382 (tramp-file-name-host vec)
4383 (tramp-file-name-method vec)))
4385 ;; Start new process.
4386 (when (and p (processp p))
4388 (setenv "TERM" tramp-terminal-type)
4389 (setenv "LC_ALL" "C")
4390 (setenv "PROMPT_COMMAND")
4391 (setenv "PS1" tramp-initial-end-of-output)
4392 (let* ((target-alist (tramp-compute-multi-hops vec))
4393 ;; We will apply `tramp-ssh-controlmaster-options'
4394 ;; only for the first hop.
4395 (options tramp-ssh-controlmaster-options)
4396 (process-connection-type tramp-process-connection-type)
4397 (process-adaptive-read-buffering nil)
4398 (coding-system-for-read nil)
4399 ;; This must be done in order to avoid our file
4401 (p (let ((default-directory
4402 (tramp-compat-temporary-file-directory)))
4405 (tramp-get-connection-name vec)
4406 (tramp-get-connection-buffer vec)
4407 (if tramp-encoding-command-interactive
4408 (list tramp-encoding-shell
4409 tramp-encoding-command-interactive)
4410 (list tramp-encoding-shell))))))
4412 ;; Set sentinel and query flag.
4413 (tramp-set-connection-property p "vector" vec)
4414 (set-process-sentinel p 'tramp-process-sentinel)
4415 (tramp-compat-set-process-query-on-exit-flag p nil)
4416 (setq tramp-current-connection
4417 (cons (butlast (append vec nil)) (current-time))
4418 tramp-current-host (system-name))
4421 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
4423 ;; Check whether process is alive.
4424 (tramp-barf-if-no-shell-prompt
4426 "Couldn't find local shell prompt %s" tramp-encoding-shell)
4428 ;; Now do all the connections as specified.
4430 (let* ((hop (car target-alist))
4431 (l-method (tramp-file-name-method hop))
4432 (l-user (tramp-file-name-user hop))
4433 (l-host (tramp-file-name-host hop))
4436 (tramp-get-method-parameter
4437 l-method 'tramp-login-program))
4439 (tramp-get-method-parameter
4440 l-method 'tramp-login-args))
4442 (tramp-get-method-parameter
4443 l-method 'tramp-async-args))
4445 (tramp-get-method-parameter l-method 'tramp-gw-args))
4446 (gw (tramp-get-file-property hop "" "gateway" nil))
4447 (g-method (and gw (tramp-file-name-method gw)))
4448 (g-user (and gw (tramp-file-name-user gw)))
4449 (g-host (and gw (tramp-file-name-real-host gw)))
4450 (command login-program)
4451 ;; We don't create the temporary file. In
4452 ;; fact, it is just a prefix for the
4453 ;; ControlPath option of ssh; the real
4454 ;; temporary file has another name, and it is
4455 ;; created and protected by ssh. It is also
4456 ;; removed by ssh when the connection is
4457 ;; closed. The temporary file name is cached
4458 ;; in the main connection process, therefore
4459 ;; we cannot use `tramp-get-connection-process'.
4461 (with-tramp-connection-property
4462 (get-process (tramp-buffer-name vec)) "temp-file"
4465 tramp-temp-name-prefix
4466 (tramp-compat-temporary-file-directory)))))
4469 ;; Add arguments for asynchronous processes.
4470 (when (and process-name async-args)
4471 (setq login-args (append async-args login-args)))
4473 ;; Add gateway arguments if necessary.
4474 (when (and gw gw-args)
4475 (setq login-args (append gw-args login-args)))
4477 ;; Check for port number. Until now, there's no
4478 ;; need for handling like method, user, host.
4479 (when (string-match tramp-host-with-port-regexp l-host)
4480 (setq l-port (match-string 2 l-host)
4481 l-host (match-string 1 l-host)))
4483 ;; Check, whether there is a restricted shell.
4484 (dolist (elt tramp-restricted-shell-hosts-alist)
4485 (when (string-match elt tramp-current-host)
4488 ;; Set variables for computing the prompt for
4489 ;; reading password. They can also be derived
4491 (setq tramp-current-method (or g-method l-method)
4492 tramp-current-user (or g-user l-user)
4493 tramp-current-host (or g-host l-host))
4495 ;; Replace login-args place holders.
4497 l-host (or l-host "")
4498 l-user (or l-user "")
4499 l-port (or l-port "")
4500 spec (format-spec-make ?t tmpfile)
4501 options (format-spec options spec)
4502 spec (format-spec-make
4503 ?h l-host ?u l-user ?p l-port ?c options)
4506 ;; We do not want to see the trailing local
4507 ;; prompt in `start-file-process'.
4508 (unless r-shell "exec ")
4512 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
4513 (unless (member "" x) (mapconcat 'identity x " ")))
4515 ;; Local shell could be a Windows COMSPEC. It
4516 ;; doesn't know the ";" syntax, but we must exit
4517 ;; always for `start-file-process'. It could
4518 ;; also be a restricted shell, which does not
4520 (when r-shell " && exit || exit")))
4522 ;; Send the command.
4523 (tramp-message vec 3 "Sending command `%s'" command)
4524 (tramp-send-command vec command t t)
4525 (tramp-process-actions
4526 p vec pos tramp-actions-before-shell 60)
4528 vec 3 "Found remote shell prompt on `%s'" l-host))
4531 target-alist (cdr target-alist)))
4533 ;; Make initial shell settings.
4534 (tramp-open-connection-setup-interactive-shell p vec))))
4536 ;; When the user did interrupt, we must cleanup.
4539 ;; Propagate the quit signal.
4540 (signal (car err) (cdr err)))))))
4542 (defun tramp-send-command (vec command &optional neveropen nooutput)
4543 "Send the COMMAND to connection VEC.
4544 Erases temporary buffer before sending the command. If optional
4545 arg NEVEROPEN is non-nil, never try to open the connection. This
4546 is meant to be used from `tramp-maybe-open-connection' only. The
4547 function waits for output unless NOOUTPUT is set."
4548 (unless neveropen (tramp-maybe-open-connection vec))
4549 (let ((p (tramp-get-connection-process vec)))
4550 (when (tramp-get-connection-property p "remote-echo" nil)
4551 ;; We mark the command string that it can be erased in the output buffer.
4552 (tramp-set-connection-property p "check-remote-echo" t)
4553 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
4554 (when (and (string-match "<<'EOF'" command)
4555 (not (tramp-get-connection-property vec "busybox" nil)))
4556 ;; Unset $PS1 when using here documents, in order to avoid
4557 ;; multiple prompts.
4558 (setq command (concat "(PS1= ; " command "\n)")))
4559 ;; Send the command.
4560 (tramp-message vec 6 "%s" command)
4561 (tramp-send-string vec command)
4562 (unless nooutput (tramp-wait-for-output p))))
4564 (defun tramp-wait-for-output (proc &optional timeout)
4565 "Wait for output from remote command."
4566 (unless (buffer-live-p (process-buffer proc))
4567 (delete-process proc)
4568 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
4569 (with-current-buffer (process-buffer proc)
4570 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
4571 ;; be leading escape sequences, which must be ignored.
4572 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
4573 ;; Sometimes, the commands do not return a newline but a
4574 ;; null byte before the shell prompt, for example "git
4575 ;; ls-files -c -z ...".
4576 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
4577 (found (tramp-wait-for-regexp proc timeout regexp1)))
4579 (let (buffer-read-only)
4580 ;; A simple-minded busybox has sent " ^H" sequences.
4582 (goto-char (point-min))
4583 (when (re-search-forward "^\\(.\b\\)+$" (point-at-eol) t)
4585 (delete-region (point-min) (point)))
4586 ;; Delete the prompt.
4587 (goto-char (point-max))
4588 (re-search-backward regexp nil t)
4589 (delete-region (point) (point-max)))
4593 "[[Remote prompt `%s' not found in %d secs]]"
4594 tramp-end-of-output timeout)
4597 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
4598 ;; Return value is whether end-of-output sentinel was found.
4601 (defun tramp-send-command-and-check
4602 (vec command &optional subshell dont-suppress-err)
4603 "Run COMMAND and check its exit status.
4604 Sends `echo $?' along with the COMMAND for checking the exit status. If
4605 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
4607 If the optional argument SUBSHELL is non-nil, the command is
4608 executed in a subshell, ie surrounded by parentheses. If
4609 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
4612 (concat (if subshell "( " "")
4614 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
4615 "echo tramp_exit_status $?"
4616 (if subshell " )" "")))
4617 (with-current-buffer (tramp-get-connection-buffer vec)
4618 (goto-char (point-max))
4619 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
4621 vec 'file-error "Couldn't find exit status of `%s'" command))
4622 (skip-chars-forward "^ ")
4624 (zerop (read (current-buffer)))
4625 (let (buffer-read-only)
4626 (delete-region (match-beginning 0) (point-max))))))
4628 (defun tramp-barf-unless-okay (vec command fmt &rest args)
4629 "Run COMMAND, check exit status, throw error if exit status not okay.
4630 Similar to `tramp-send-command-and-check' but accepts two more arguments
4631 FMT and ARGS which are passed to `error'."
4632 (or (tramp-send-command-and-check vec command)
4633 (apply 'tramp-error vec 'file-error fmt args)))
4635 (defun tramp-send-command-and-read (vec command &optional noerror)
4636 "Run COMMAND and return the output, which must be a Lisp expression.
4637 In case there is no valid Lisp expression and NOERROR is nil, it
4640 (tramp-send-command-and-check vec command)
4641 (tramp-barf-unless-okay
4642 vec command "`%s' returns with error" command))
4643 (with-current-buffer (tramp-get-connection-buffer vec)
4644 ;; Read the expression.
4645 (goto-char (point-min))
4647 (prog1 (read (current-buffer))
4649 (when (re-search-forward "\\S-" (point-at-eol) t)
4651 (error (unless noerror
4654 "`%s' does not return a valid Lisp expression: `%s'"
4655 command (buffer-string))))))))
4657 (defun tramp-convert-file-attributes (vec attr)
4658 "Convert `file-attributes' ATTR generated by perl script, stat or ls.
4659 Convert file mode bits to string and set virtual device number.
4662 ;; Remove color escape sequences from symlink.
4663 (when (stringp (car attr))
4664 (while (string-match tramp-color-escape-sequence-regexp (car attr))
4665 (setcar attr (replace-match "" nil nil (car attr)))))
4666 ;; Convert uid and gid. Use -1 as indication of unusable value.
4667 (when (and (numberp (nth 2 attr)) (< (nth 2 attr) 0))
4668 (setcar (nthcdr 2 attr) -1))
4669 (when (and (floatp (nth 2 attr))
4670 (<= (nth 2 attr) (tramp-compat-most-positive-fixnum)))
4671 (setcar (nthcdr 2 attr) (round (nth 2 attr))))
4672 (when (and (numberp (nth 3 attr)) (< (nth 3 attr) 0))
4673 (setcar (nthcdr 3 attr) -1))
4674 (when (and (floatp (nth 3 attr))
4675 (<= (nth 3 attr) (tramp-compat-most-positive-fixnum)))
4676 (setcar (nthcdr 3 attr) (round (nth 3 attr))))
4677 ;; Convert last access time.
4678 (unless (listp (nth 4 attr))
4679 (setcar (nthcdr 4 attr)
4680 (list (floor (nth 4 attr) 65536)
4681 (floor (mod (nth 4 attr) 65536)))))
4682 ;; Convert last modification time.
4683 (unless (listp (nth 5 attr))
4684 (setcar (nthcdr 5 attr)
4685 (list (floor (nth 5 attr) 65536)
4686 (floor (mod (nth 5 attr) 65536)))))
4687 ;; Convert last status change time.
4688 (unless (listp (nth 6 attr))
4689 (setcar (nthcdr 6 attr)
4690 (list (floor (nth 6 attr) 65536)
4691 (floor (mod (nth 6 attr) 65536)))))
4692 ;; Convert file size.
4693 (when (< (nth 7 attr) 0)
4694 (setcar (nthcdr 7 attr) -1))
4695 (when (and (floatp (nth 7 attr))
4696 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
4697 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
4698 ;; Convert file mode bits to string.
4699 (unless (stringp (nth 8 attr))
4700 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
4701 (when (stringp (car attr))
4702 (aset (nth 8 attr) 0 ?l)))
4703 ;; Convert directory indication bit.
4704 (when (string-match "^d" (nth 8 attr))
4706 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
4707 (when (consp (car attr))
4708 (if (and (stringp (caar attr))
4709 (string-match ".+ -> .\\(.+\\)." (caar attr)))
4710 (setcar attr (match-string 1 (caar attr)))
4712 ;; Set file's gid change bit.
4713 (setcar (nthcdr 9 attr)
4714 (if (numberp (nth 3 attr))
4715 (not (= (nth 3 attr)
4716 (tramp-get-remote-gid vec 'integer)))
4719 (tramp-get-remote-gid vec 'string)))))
4721 (unless (listp (nth 10 attr))
4722 (setcar (nthcdr 10 attr)
4724 (cons (floor (nth 10 attr) 65536)
4725 (floor (mod (nth 10 attr) 65536)))
4726 ;; Inodes can be incredible huge. We must hide this.
4727 (error (tramp-get-inode vec)))))
4728 ;; Set virtual device number.
4729 (setcar (nthcdr 11 attr)
4730 (tramp-get-device vec))
4733 (defun tramp-check-cached-permissions (vec access)
4734 "Check `file-attributes' caches for VEC.
4735 Return t if according to the cache access type ACCESS is known to
4741 ((eq ?x access) 3))))
4742 (dolist (suffix '("string" "integer") result)
4748 (tramp-get-file-property
4749 vec (tramp-file-name-localname vec)
4750 (concat "file-attributes-" suffix) nil))
4752 (tramp-get-connection-property
4753 vec (concat "uid-" suffix) nil))
4755 (tramp-get-connection-property
4756 vec (concat "gid-" suffix) nil)))
4761 (eq t (car file-attr))
4762 (null (car file-attr)))
4764 ;; World accessible.
4765 (eq access (aref (nth 8 file-attr) (+ offset 6)))
4766 ;; User accessible and owned by user.
4768 (eq access (aref (nth 8 file-attr) offset))
4769 (equal remote-uid (nth 2 file-attr)))
4770 ;; Group accessible and owned by user's
4773 (eq access (aref (nth 8 file-attr) (+ offset 3)))
4774 (equal remote-gid (nth 3 file-attr)))))))))))
4776 (defun tramp-file-mode-from-int (mode)
4777 "Turn an integer representing a file mode into an ls(1)-like string."
4779 (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
4780 (user (logand (lsh mode -6) 7))
4781 (group (logand (lsh mode -3) 7))
4782 (other (logand (lsh mode -0) 7))
4783 (suid (> (logand (lsh mode -9) 4) 0))
4784 (sgid (> (logand (lsh mode -9) 2) 0))
4785 (sticky (> (logand (lsh mode -9) 1) 0)))
4786 (setq user (tramp-file-mode-permissions user suid "s"))
4787 (setq group (tramp-file-mode-permissions group sgid "s"))
4788 (setq other (tramp-file-mode-permissions other sticky "t"))
4789 (concat type user group other)))
4791 (defun tramp-file-mode-permissions (perm suid suid-text)
4792 "Convert a permission bitset into a string.
4793 This is used internally by `tramp-file-mode-from-int'."
4794 (let ((r (> (logand perm 4) 0))
4795 (w (> (logand perm 2) 0))
4796 (x (> (logand perm 1) 0)))
4797 (concat (or (and r "r") "-")
4798 (or (and w "w") "-")
4799 (or (and suid x suid-text) ; suid, execute
4800 (and suid (upcase suid-text)) ; suid, !execute
4801 (and x "x") "-")))) ; !suid
4803 (defun tramp-shell-case-fold (string)
4804 "Converts STRING to shell glob pattern which ignores case."
4807 (if (equal (downcase c) (upcase c))
4809 (format "[%c%c]" (downcase c) (upcase c))))
4813 (defun tramp-make-copy-program-file-name (vec)
4814 "Create a file name suitable to be passed to `rcp' and workalikes."
4815 (let ((user (tramp-file-name-user vec))
4816 (host (tramp-file-name-real-host vec))
4817 (localname (tramp-shell-quote-argument
4818 (tramp-file-name-localname vec))))
4819 (if (not (zerop (length user)))
4820 (format "%s@%s:%s" user host localname)
4821 (format "%s:%s" host localname))))
4823 (defun tramp-method-out-of-band-p (vec size)
4824 "Return t if this is an out-of-band method, nil otherwise."
4826 ;; It shall be an out-of-band method.
4827 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
4828 ;; There must be a size, otherwise the file doesn't exist.
4830 ;; Either the file size is large enough, or (in rare cases) there
4831 ;; does not exist a remote encoding.
4832 (or (null tramp-copy-size-limit)
4833 (> size tramp-copy-size-limit)
4834 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
4836 ;; Variables local to connection.
4838 (defun tramp-get-remote-path (vec)
4839 (with-tramp-connection-property
4840 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
4841 ;; cache the result for the session only. Otherwise, the result
4842 ;; is cached persistently.
4843 (if (memq 'tramp-own-remote-path tramp-remote-path)
4844 (tramp-get-connection-process vec)
4847 (let* ((remote-path (copy-tree tramp-remote-path))
4848 (elt1 (memq 'tramp-default-remote-path remote-path))
4849 (elt2 (memq 'tramp-own-remote-path remote-path))
4850 (default-remote-path
4853 (tramp-send-command-and-read
4854 vec "echo \\\"`getconf PATH 2>/dev/null`\\\"" 'noerror)
4855 ;; Default if "getconf" is not available.
4859 "`getconf PATH' not successful, using default value \"%s\"."
4865 (tramp-send-command-and-read vec "echo \\\"$PATH\\\"")
4868 vec 3 "$PATH not set, ignoring `tramp-own-remote-path'.")
4871 ;; Replace place holder `tramp-default-remote-path'.
4875 (tramp-compat-split-string default-remote-path ":")
4877 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
4879 ;; Replace place holder `tramp-own-remote-path'.
4883 (tramp-compat-split-string own-remote-path ":")
4885 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
4887 ;; Remove double entries.
4888 (setq elt1 remote-path)
4890 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
4892 (setq elt1 (cdr elt1)))
4894 ;; Remove non-existing directories.
4902 (tramp-make-tramp-file-name
4903 (tramp-file-name-method vec)
4904 (tramp-file-name-user vec)
4905 (tramp-file-name-host vec)
4910 (defun tramp-get-ls-command (vec)
4911 (with-tramp-connection-property vec "ls"
4912 (tramp-message vec 5 "Finding a suitable `ls' command")
4915 (dolist (cmd '("ls" "gnuls" "gls"))
4916 (let ((dl (tramp-get-remote-path vec))
4918 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
4919 ;; Check parameters. On busybox, "ls" output coloring is
4920 ;; enabled by default sometimes. So we try to disable it
4921 ;; when possible. $LS_COLORING is not supported there.
4922 ;; Some "ls" versions are sensible wrt the order of
4923 ;; arguments, they fail when "-al" is after the
4924 ;; "--color=never" argument (for example on FreeBSD).
4925 (when (tramp-send-command-and-check
4926 vec (format "%s -lnd /" result))
4927 (when (tramp-send-command-and-check
4929 "%s --color=never -al /dev/null" result))
4930 (setq result (concat result " --color=never")))
4931 (throw 'ls-found result))
4932 (setq dl (cdr dl))))))
4933 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
4935 (defun tramp-get-ls-command-with-dired (vec)
4937 (with-tramp-connection-property vec "ls-dired"
4938 (tramp-message vec 5 "Checking, whether `ls --dired' works")
4939 ;; Some "ls" versions are sensible wrt the order of arguments,
4940 ;; they fail when "-al" is after the "--dired" argument (for
4941 ;; example on FreeBSD).
4942 (tramp-send-command-and-check
4943 vec (format "%s --dired -al /dev/null" (tramp-get-ls-command vec))))))
4945 (defun tramp-get-test-command (vec)
4946 (with-tramp-connection-property vec "test"
4947 (tramp-message vec 5 "Finding a suitable `test' command")
4948 (if (tramp-send-command-and-check vec "test 0")
4950 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
4952 (defun tramp-get-test-nt-command (vec)
4953 ;; Does `test A -nt B' work? Use abominable `find' construct if it
4954 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
4955 ;; for otherwise the shell crashes.
4956 (with-tramp-connection-property vec "test-nt"
4960 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
4961 (with-current-buffer (tramp-get-buffer vec)
4962 (goto-char (point-min))
4963 (when (looking-at (regexp-quote tramp-end-of-output))
4964 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
4969 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
4970 (tramp-get-test-command vec)))
4971 "tramp_test_nt %s %s"))))
4973 (defun tramp-get-file-exists-command (vec)
4974 (with-tramp-connection-property vec "file-exists"
4975 (tramp-message vec 5 "Finding command to check if file exists")
4976 (tramp-find-file-exists-command vec)))
4978 (defun tramp-get-remote-ln (vec)
4979 (with-tramp-connection-property vec "ln"
4980 (tramp-message vec 5 "Finding a suitable `ln' command")
4981 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
4983 (defun tramp-get-remote-perl (vec)
4984 (with-tramp-connection-property vec "perl"
4985 (tramp-message vec 5 "Finding a suitable `perl' command")
4987 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
4988 (tramp-find-executable
4989 vec "perl" (tramp-get-remote-path vec)))))
4990 ;; We must check also for some Perl modules.
4992 (with-tramp-connection-property vec "perl-file-spec"
4993 (tramp-send-command-and-check
4994 vec (format "%s -e 'use File::Spec;'" result)))
4995 (with-tramp-connection-property vec "perl-cwd-realpath"
4996 (tramp-send-command-and-check
4997 vec (format "%s -e 'use Cwd \"realpath\";'" result))))
5000 (defun tramp-get-remote-stat (vec)
5001 (with-tramp-connection-property vec "stat"
5002 (tramp-message vec 5 "Finding a suitable `stat' command")
5003 (let ((result (tramp-find-executable
5004 vec "stat" (tramp-get-remote-path vec)))
5006 ;; Check whether stat(1) returns usable syntax. "%s" does not
5007 ;; work on older AIX systems.
5010 (tramp-send-command-and-read
5011 vec (format "%s -c '(\"%%N\" %%s)' /" result) 'noerror))
5012 (unless (and (listp tmp) (stringp (car tmp))
5013 (string-match "^./.$" (car tmp))
5014 (integerp (cadr tmp)))
5018 (defun tramp-get-remote-readlink (vec)
5019 (with-tramp-connection-property vec "readlink"
5020 (tramp-message vec 5 "Finding a suitable `readlink' command")
5021 (let ((result (tramp-find-executable
5022 vec "readlink" (tramp-get-remote-path vec))))
5024 (tramp-send-command-and-check
5025 vec (format "%s --canonicalize-missing /" result)))
5028 (defun tramp-get-remote-trash (vec)
5029 (with-tramp-connection-property vec "trash"
5030 (tramp-message vec 5 "Finding a suitable `trash' command")
5031 (tramp-find-executable vec "trash" (tramp-get-remote-path vec))))
5033 (defun tramp-get-remote-id (vec)
5034 (with-tramp-connection-property vec "id"
5035 (tramp-message vec 5 "Finding POSIX `id' command")
5038 (let ((dl (tramp-get-remote-path vec))
5040 (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
5041 ;; Check POSIX parameter.
5042 (when (tramp-send-command-and-check vec (format "%s -u" result))
5043 (throw 'id-found result))
5044 (setq dl (cdr dl)))))
5045 (tramp-error vec 'file-error "Couldn't find a POSIX `id' command"))))
5047 (defun tramp-get-remote-uid (vec id-format)
5048 (with-tramp-connection-property vec (format "uid-%s" id-format)
5049 (let ((res (tramp-send-command-and-read
5051 (format "%s -u%s %s"
5052 (tramp-get-remote-id vec)
5053 (if (equal id-format 'integer) "" "n")
5054 (if (equal id-format 'integer)
5055 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
5056 ;; The command might not always return a number.
5057 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
5059 (defun tramp-get-remote-gid (vec id-format)
5060 (with-tramp-connection-property vec (format "gid-%s" id-format)
5061 (let ((res (tramp-send-command-and-read
5063 (format "%s -g%s %s"
5064 (tramp-get-remote-id vec)
5065 (if (equal id-format 'integer) "" "n")
5066 (if (equal id-format 'integer)
5067 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
5068 ;; The command might not always return a number.
5069 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
5071 (defun tramp-get-local-uid (id-format)
5072 (if (equal id-format 'integer) (user-uid) (user-login-name)))
5074 (defun tramp-get-local-gid (id-format)
5075 (if (and (fboundp 'group-gid) (equal id-format 'integer))
5076 (tramp-compat-funcall 'group-gid)
5077 (nth 3 (tramp-compat-file-attributes "~/" id-format))))
5079 ;; Some predefined connection properties.
5080 (defun tramp-get-inline-compress (vec prop size)
5081 "Return the compress command related to PROP.
5082 PROP is either `inline-compress' or `inline-decompress'. SIZE is
5083 the length of the file to be compressed.
5085 If no corresponding command is found, nil is returned."
5086 (when (and (integerp tramp-inline-compress-start-size)
5087 (> size tramp-inline-compress-start-size))
5088 (with-tramp-connection-property (tramp-get-connection-process vec) prop
5089 (tramp-find-inline-compress vec)
5090 (tramp-get-connection-property
5091 (tramp-get-connection-process vec) prop nil))))
5093 (defun tramp-get-inline-coding (vec prop size)
5094 "Return the coding command related to PROP.
5095 PROP is either `remote-encoding', `remote-decoding',
5096 `local-encoding' or `local-decoding'.
5098 SIZE is the length of the file to be coded. Depending on SIZE,
5099 compression might be applied.
5101 If no corresponding command is found, nil is returned.
5102 Otherwise, either a string is returned which contains a `%s' mark
5103 to be used for the respective input or output file; or a Lisp
5104 function cell is returned to be applied on a buffer."
5105 ;; We must catch the errors, because we want to return `nil', when
5106 ;; no inline coding is found.
5109 (with-tramp-connection-property
5110 (tramp-get-connection-process vec) prop
5111 (tramp-find-inline-encoding vec)
5112 (tramp-get-connection-property
5113 (tramp-get-connection-process vec) prop nil)))
5114 (prop1 (if (string-match "encoding" prop)
5115 "inline-compress" "inline-decompress"))
5117 ;; The connection property might have been cached. So we must
5118 ;; send the script to the remote side - maybe.
5119 (when (and coding (symbolp coding) (string-match "remote" prop))
5120 (let ((name (symbol-name coding)))
5121 (while (string-match (regexp-quote "-") name)
5122 (setq name (replace-match "_" nil t name)))
5123 (tramp-maybe-send-script vec (symbol-value coding) name)
5124 (setq coding name)))
5126 ;; Check for the `compress' command.
5127 (setq compress (tramp-get-inline-compress vec prop1 size))
5128 ;; Return the value.
5130 ((and compress (symbolp coding))
5131 (if (string-match "decompress" prop1)
5134 (let ((coding-system-for-write 'binary)
5135 (coding-system-for-read 'binary))
5137 'call-process-region (point-min) (point-max)
5138 (car (split-string ,compress)) t t nil
5139 (cdr (split-string ,compress)))))
5141 (let ((coding-system-for-write 'binary)
5142 (coding-system-for-read 'binary))
5144 'call-process-region beg end
5145 (car (split-string ,compress)) t t nil
5146 (cdr (split-string ,compress))))
5147 (,coding (point-min) (point-max)))))
5150 ((and compress (string-match "decoding" prop))
5152 ;; Windows shells need the program file name after
5153 ;; the pipe symbol be quoted if they use forward
5154 ;; slashes as directory separators.
5155 (if (and (string-match "local" prop)
5156 (memq system-type '(windows-nt)))
5157 "(%s | \"%s\" >%%s)"
5162 ;; Windows shells need the program file name after
5163 ;; the pipe symbol be quoted if they use forward
5164 ;; slashes as directory separators.
5165 (if (and (string-match "local" prop)
5166 (memq system-type '(windows-nt)))
5167 "(%s <%%s | \"%s\")"
5170 ((string-match "decoding" prop)
5171 (format "%s >%%s" coding))
5173 (format "%s <%%s" coding)))))))
5175 (add-hook 'tramp-unload-hook
5177 (unload-feature 'tramp-sh 'force)))
5183 ;; * Don't use globbing for directories with many files, as this is
5184 ;; likely to produce long command lines, and some shells choke on
5185 ;; long command lines.
5186 ;; * Make it work for different encodings, and for different file name
5187 ;; encodings, too. (Daniel Pittman)
5188 ;; * Don't search for perl5 and perl. Instead, only search for perl and
5189 ;; then look if it's the right version (with `perl -v').
5190 ;; * When editing a remote CVS controlled file as a different user, VC
5191 ;; gets confused about the file locking status. Try to find out why
5192 ;; the workaround doesn't work.
5193 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5194 ;; until the last but one hop via `start-file-process'. Apply it
5195 ;; also for ftp and smb.
5196 ;; * WIBNI if we had a command "trampclient"? If I was editing in
5197 ;; some shell with root privileges, it would be nice if I could
5199 ;; trampclient filename.c
5200 ;; as an editor, and the _current_ shell would connect to an Emacs
5201 ;; server and would be used in an existing non-privileged Emacs
5202 ;; session for doing the editing in question.
5203 ;; That way, I need not tell Emacs my password again and be afraid
5204 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
5205 ;; once display a just typed password in the context of a keyboard
5206 ;; sequence prompt for a question immediately following in a shell
5207 ;; script run within Emacs -- nasty).
5208 ;; And if I have some ssh session running to a different computer,
5209 ;; having the possibility of passing a local file there to a local
5210 ;; Emacs session (in case I can arrange for a connection back) would
5212 ;; Likely the corresponding Tramp server should not allow the
5213 ;; equivalent of the emacsclient -eval option in order to make this
5214 ;; reasonably unproblematic. And maybe trampclient should have some
5215 ;; way of passing credentials, like by using an SSL socket or
5216 ;; something. (David Kastrup)
5217 ;; * Reconnect directly to a compliant shell without first going
5218 ;; through the user's default shell. (Pete Forman)
5219 ;; * How can I interrupt the remote process with a signal
5220 ;; (interrupt-process seems not to work)? (Markus Triska)
5221 ;; * Avoid the local shell entirely for starting remote processes. If
5222 ;; so, I think even a signal, when delivered directly to the local
5223 ;; SSH instance, would correctly be propagated to the remote process
5224 ;; automatically; possibly SSH would have to be started with
5225 ;; "-t". (Markus Triska)
5226 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5227 ;; isn't on the remote host. (Mark A. Hershberger)
5228 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
5229 ;; * Optimize out-of-band copying when both methods are scp-like (not
5231 ;; * Keep a second connection open for out-of-band methods like scp or
5233 ;; * Try telnet+curl as new method. It might be useful for busybox,
5234 ;; without built-in uuencode/uudecode.
5236 ;;; tramp-sh.el ends here