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 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
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 (("-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 ("-e" "none") ("%h")))
151 (tramp-async-args (("-q")))
152 (tramp-remote-shell "/bin/sh")
153 (tramp-remote-shell-args ("-c"))
154 (tramp-copy-program "sftp")
155 (tramp-copy-args ("%c"))))
157 (add-to-list 'tramp-methods
159 (tramp-login-program "ssh")
160 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
161 ("-e" "none") ("%h")))
162 (tramp-async-args (("-q")))
163 (tramp-remote-shell "/bin/sh")
164 (tramp-remote-shell-args ("-c"))
165 (tramp-copy-program "rsync")
166 (tramp-copy-args (("-t" "%k") ("-r")))
167 (tramp-copy-env (("RSYNC_RSH") ("ssh" "%c")))
168 (tramp-copy-keep-date t)
169 (tramp-copy-keep-tmpfile t)
170 (tramp-copy-recursive t)))
172 (add-to-list 'tramp-methods
174 (tramp-login-program "rsh")
175 (tramp-login-args (("%h") ("-l" "%u")))
176 (tramp-remote-shell "/bin/sh")
177 (tramp-remote-shell-args ("-c"))))
179 (add-to-list 'tramp-methods
181 (tramp-login-program "remsh")
182 (tramp-login-args (("%h") ("-l" "%u")))
183 (tramp-remote-shell "/bin/sh")
184 (tramp-remote-shell-args ("-c"))))
186 (add-to-list 'tramp-methods
188 (tramp-login-program "ssh")
189 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
190 ("-e" "none") ("%h")))
191 (tramp-async-args (("-q")))
192 (tramp-remote-shell "/bin/sh")
193 (tramp-remote-shell-args ("-c"))
194 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
195 ("-o" "UserKnownHostsFile=/dev/null")
196 ("-o" "StrictHostKeyChecking=no")))
197 (tramp-default-port 22)))
199 (add-to-list 'tramp-methods
201 (tramp-login-program "ssh")
202 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
203 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
204 (tramp-async-args (("-q")))
205 (tramp-remote-shell "/bin/sh")
206 (tramp-remote-shell-args ("-c"))
207 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
208 ("-o" "UserKnownHostsFile=/dev/null")
209 ("-o" "StrictHostKeyChecking=no")))
210 (tramp-default-port 22)))
212 (add-to-list 'tramp-methods
214 (tramp-login-program "telnet")
215 (tramp-login-args (("%h") ("%p")))
216 (tramp-remote-shell "/bin/sh")
217 (tramp-remote-shell-args ("-c"))
218 (tramp-default-port 23)))
220 (add-to-list 'tramp-methods
222 (tramp-login-program "su")
223 (tramp-login-args (("-") ("%u")))
224 (tramp-remote-shell "/bin/sh")
225 (tramp-remote-shell-args ("-c"))))
227 (add-to-list 'tramp-methods
229 (tramp-login-program "sudo")
230 (tramp-login-args (("-u" "%u") ("-s") ("-H") ("-p" "Password:")))
231 (tramp-remote-shell "/bin/sh")
232 (tramp-remote-shell-args ("-c"))))
234 (add-to-list 'tramp-methods
236 (tramp-login-program "ksu")
237 (tramp-login-args (("%u") ("-q")))
238 (tramp-remote-shell "/bin/sh")
239 (tramp-remote-shell-args ("-c"))))
241 (add-to-list 'tramp-methods
243 (tramp-login-program "krlogin")
244 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
245 (tramp-remote-shell "/bin/sh")
246 (tramp-remote-shell-args ("-c"))))
248 (add-to-list 'tramp-methods
250 (tramp-login-program "plink")
251 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
252 (tramp-remote-shell "/bin/sh")
253 (tramp-remote-shell-args ("-c"))
254 (tramp-default-port 22)))
256 (add-to-list 'tramp-methods
258 (tramp-login-program "plink")
259 ;; ("%h") must be a single element, see
260 ;; `tramp-compute-multi-hops'.
261 (tramp-login-args (("-load") ("%h") ("-t")
263 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
265 tramp-initial-end-of-output))
267 (tramp-remote-shell "/bin/sh")
268 (tramp-remote-shell-args ("-c"))))
270 (add-to-list 'tramp-methods
272 (tramp-login-program "plink")
273 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
274 (tramp-remote-shell "/bin/sh")
275 (tramp-remote-shell-args ("-c"))
276 (tramp-copy-program "pscp")
277 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-scp") ("-p" "%k")
279 (tramp-copy-keep-date t)
280 (tramp-copy-recursive t)
281 (tramp-default-port 22)))
283 (add-to-list 'tramp-methods
285 (tramp-login-program "plink")
286 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
287 (tramp-remote-shell "/bin/sh")
288 (tramp-remote-shell-args ("-c"))
289 (tramp-copy-program "pscp")
290 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-sftp") ("-p" "%k")
292 (tramp-copy-keep-date t)
293 (tramp-copy-recursive t)))
295 (add-to-list 'tramp-methods
297 (tramp-login-program "fsh")
298 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
299 (tramp-remote-shell "/bin/sh")
300 (tramp-remote-shell-args ("-i") ("-c"))
301 (tramp-copy-program "fcp")
302 (tramp-copy-args (("-p" "%k")))
303 (tramp-copy-keep-date t)))
306 (add-to-list 'tramp-default-method-alist
307 `(,tramp-local-host-regexp "\\`root\\'" "su"))
310 (add-to-list 'tramp-default-user-alist
311 `(,(concat "\\`" (regexp-opt '("su" "sudo" "ksu")) "\\'")
313 ;; Do not add "ssh" based methods, otherwise ~/.ssh/config would be ignored.
314 ;; Do not add "plink" based methods, they ask interactively for the user.
316 (add-to-list 'tramp-default-user-alist
319 (regexp-opt '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
321 nil ,(user-login-name)))
324 (defconst tramp-completion-function-alist-rsh
325 '((tramp-parse-rhosts "/etc/hosts.equiv")
326 (tramp-parse-rhosts "~/.rhosts"))
327 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
330 (defconst tramp-completion-function-alist-ssh
331 '((tramp-parse-rhosts "/etc/hosts.equiv")
332 (tramp-parse-rhosts "/etc/shosts.equiv")
333 (tramp-parse-shosts "/etc/ssh_known_hosts")
334 (tramp-parse-sconfig "/etc/ssh_config")
335 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
336 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
337 (tramp-parse-rhosts "~/.rhosts")
338 (tramp-parse-rhosts "~/.shosts")
339 (tramp-parse-shosts "~/.ssh/known_hosts")
340 (tramp-parse-sconfig "~/.ssh/config")
341 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
342 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
343 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
346 (defconst tramp-completion-function-alist-telnet
347 '((tramp-parse-hosts "/etc/hosts"))
348 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
351 (defconst tramp-completion-function-alist-su
352 '((tramp-parse-passwd "/etc/passwd"))
353 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
356 (defconst tramp-completion-function-alist-putty
358 ,(if (memq system-type '(windows-nt))
359 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"
360 "~/.putty/sessions")))
361 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty sessions.")
364 (eval-after-load 'tramp
366 (tramp-set-completion-function "rcp" tramp-completion-function-alist-rsh)
367 (tramp-set-completion-function "remcp" tramp-completion-function-alist-rsh)
368 (tramp-set-completion-function "scp" tramp-completion-function-alist-ssh)
369 (tramp-set-completion-function "scpx" tramp-completion-function-alist-ssh)
370 (tramp-set-completion-function "sftp" tramp-completion-function-alist-ssh)
371 (tramp-set-completion-function "rsync" tramp-completion-function-alist-ssh)
372 (tramp-set-completion-function "rsh" tramp-completion-function-alist-rsh)
373 (tramp-set-completion-function "remsh" tramp-completion-function-alist-rsh)
374 (tramp-set-completion-function "ssh" tramp-completion-function-alist-ssh)
375 (tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh)
376 (tramp-set-completion-function
377 "telnet" tramp-completion-function-alist-telnet)
378 (tramp-set-completion-function "su" tramp-completion-function-alist-su)
379 (tramp-set-completion-function "sudo" tramp-completion-function-alist-su)
380 (tramp-set-completion-function "ksu" tramp-completion-function-alist-su)
381 (tramp-set-completion-function
382 "krlogin" tramp-completion-function-alist-rsh)
383 (tramp-set-completion-function "plink" tramp-completion-function-alist-ssh)
384 (tramp-set-completion-function
385 "plinkx" tramp-completion-function-alist-putty)
386 (tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh)
387 (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh)))
389 ;; "getconf PATH" yields:
390 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
391 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
392 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
393 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
396 (defcustom tramp-remote-path
397 '(tramp-default-remote-path "/bin" "/usr/bin" "/sbin" "/usr/sbin"
398 "/usr/local/bin" "/usr/local/sbin" "/local/bin" "/local/freeware/bin"
399 "/local/gnu/bin" "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin"
400 "/opt/bin" "/opt/sbin" "/opt/local/bin")
401 "List of directories to search for executables on remote host.
402 For every remote host, this variable will be set buffer local,
403 keeping the list of existing directories on that host.
405 You can use `~' in this list, but when searching for a shell which groks
406 tilde expansion, all directory names starting with `~' will be ignored.
408 `Default Directories' represent the list of directories given by
409 the command \"getconf PATH\". It is recommended to use this
410 entry on top of this list, because these are the default
411 directories for POSIX compatible commands. On remote hosts which
412 do not offer the getconf command (like cygwin), the value
413 \"/bin:/usr/bin\" is used instead of.
415 `Private Directories' are the settings of the $PATH environment,
416 as given in your `~/.profile'."
418 :type '(repeat (choice
419 (const :tag "Default Directories" tramp-default-remote-path)
420 (const :tag "Private Directories" tramp-own-remote-path)
421 (string :tag "Directory"))))
424 (defcustom tramp-remote-process-environment
425 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "TMOUT=0" "LC_ALL=C"
426 ,(format "TERM=%s" tramp-terminal-type)
427 "EMACS=t" ;; Deprecated.
428 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version)
429 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH=" "PAGER=\"\""
430 "autocorrect=" "correct=")
431 "List of environment variables to be set on the remote host.
433 Each element should be a string of the form ENVVARNAME=VALUE. An
434 entry ENVVARNAME= disables the corresponding environment variable,
435 which might have been set in the init files like ~/.profile.
437 Special handling is applied to the PATH environment, which should
438 not be set here. Instead, it should be set via `tramp-remote-path'."
440 :type '(repeat string))
442 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
443 "Alist specifying extra arguments to pass to the remote shell.
444 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
445 matching the shell file name and ARGS is a string specifying the
448 This variable is only used when Tramp needs to start up another shell
449 for tilde expansion. The extra arguments should typically prevent the
450 shell from reading its init file."
452 ;; This might be the wrong way to test whether the widget type
453 ;; `alist' is available. Who knows the right way to test it?
454 :type (if (get 'alist 'widget-type)
455 '(alist :key-type string :value-type string)
456 '(repeat (cons string string))))
458 (defconst tramp-actions-before-shell
459 '((tramp-login-prompt-regexp tramp-action-login)
460 (tramp-password-prompt-regexp tramp-action-password)
461 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
462 (shell-prompt-pattern tramp-action-succeed)
463 (tramp-shell-prompt-pattern tramp-action-succeed)
464 (tramp-yesno-prompt-regexp tramp-action-yesno)
465 (tramp-yn-prompt-regexp tramp-action-yn)
466 (tramp-terminal-prompt-regexp tramp-action-terminal)
467 (tramp-process-alive-regexp tramp-action-process-alive))
468 "List of pattern/action pairs.
469 Whenever a pattern matches, the corresponding action is performed.
470 Each item looks like (PATTERN ACTION).
472 The PATTERN should be a symbol, a variable. The value of this
473 variable gives the regular expression to search for. Note that the
474 regexp must match at the end of the buffer, \"\\'\" is implicitly
477 The ACTION should also be a symbol, but a function. When the
478 corresponding PATTERN matches, the ACTION function is called.")
480 (defconst tramp-actions-copy-out-of-band
481 '((tramp-password-prompt-regexp tramp-action-password)
482 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
483 (tramp-copy-failed-regexp tramp-action-permission-denied)
484 (tramp-process-alive-regexp tramp-action-out-of-band))
485 "List of pattern/action pairs.
486 This list is used for copying/renaming with out-of-band methods.
488 See `tramp-actions-before-shell' for more info.")
490 (defconst tramp-uudecode
491 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
494 "Shell function to implement `uudecode' to standard output.
495 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
496 for this or `uudecode -p', but some systems don't, and for them
497 we have this shell function.")
499 (defconst tramp-perl-file-truename
502 use Cwd \"realpath\";
505 my ($volume, @dirs) = @_;
506 my $real = realpath(File::Spec->catpath(
507 $volume, File::Spec->catdir(@dirs), \"\"));
509 my ($vol, $dir) = File::Spec->splitpath($real, 1);
510 return ($vol, File::Spec->splitdir($dir));
513 my $last = pop(@dirs);
514 ($volume, @dirs) = recursive($volume, @dirs);
516 return ($volume, @dirs);
520 $result = realpath($ARGV[0]);
522 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
523 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
525 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
528 if ($ARGV[0] =~ /\\/$/) {
529 $result = $result . \"/\";
532 print \"\\\"$result\\\"\\n\";
533 ' \"$1\" 2>/dev/null"
534 "Perl script to produce output suitable for use with `file-truename'
535 on the remote file system.
536 Escape sequence %s is replaced with name of Perl binary.
537 This string is passed to `format', so percent characters need to be doubled.")
539 (defconst tramp-perl-file-name-all-completions
549 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
550 @files = readdir(d); closedir(d);
551 foreach $f (@files) {
552 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
553 if (-d \"$ARGV[0]/$f\") {
562 ' \"$1\" \"$2\" \"$3\" 2>/dev/null"
563 "Perl script to produce output suitable for use with
564 `file-name-all-completions' on the remote file system. Escape
565 sequence %s is replaced with name of Perl binary. This string is
566 passed to `format', so percent characters need to be doubled.")
568 ;; Perl script to implement `file-attributes' in a Lisp `read'able
569 ;; output. If you are hacking on this, note that you get *no* output
570 ;; unless this spits out a complete line, including the '\n' at the
572 ;; The device number is returned as "-1", because there will be a virtual
573 ;; device number set in `tramp-sh-handle-file-attributes'.
574 (defconst tramp-perl-file-attributes
576 @stat = lstat($ARGV[0]);
581 if (($stat[2] & 0170000) == 0120000)
583 $type = readlink($ARGV[0]);
584 $type = \"\\\"$type\\\"\";
586 elsif (($stat[2] & 0170000) == 040000)
594 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
595 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
597 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
602 $stat[8] >> 16 & 0xffff,
604 $stat[9] >> 16 & 0xffff,
606 $stat[10] >> 16 & 0xffff,
610 $stat[1] >> 16 & 0xffff,
612 );' \"$1\" \"$2\" 2>/dev/null"
613 "Perl script to produce output suitable for use with `file-attributes'
614 on the remote file system.
615 Escape sequence %s is replaced with name of Perl binary.
616 This string is passed to `format', so percent characters need to be doubled.")
618 (defconst tramp-perl-directory-files-and-attributes
620 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
621 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
622 @list = readdir(DIR);
626 for($i = 0; $i < $n; $i++)
628 $filename = $list[$i];
629 @stat = lstat($filename);
630 if (($stat[2] & 0170000) == 0120000)
632 $type = readlink($filename);
633 $type = \"\\\"$type\\\"\";
635 elsif (($stat[2] & 0170000) == 040000)
643 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
644 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
646 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
652 $stat[8] >> 16 & 0xffff,
654 $stat[9] >> 16 & 0xffff,
656 $stat[10] >> 16 & 0xffff,
660 $stat[1] >> 16 & 0xffff,
662 $stat[0] >> 16 & 0xffff,
665 printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
666 "Perl script implementing `directory-files-attributes' as Lisp `read'able
668 Escape sequence %s is replaced with name of Perl binary.
669 This string is passed to `format', so percent characters need to be doubled.")
671 ;; These two use base64 encoding.
672 (defconst tramp-perl-encode-with-module
673 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
674 "Perl program to use for encoding a file.
675 Escape sequence %s is replaced with name of Perl binary.
676 This string is passed to `format', so percent characters need to be doubled.
677 This implementation requires the MIME::Base64 Perl module to be installed
678 on the remote host.")
680 (defconst tramp-perl-decode-with-module
681 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
682 "Perl program to use for decoding a file.
683 Escape sequence %s is replaced with name of Perl binary.
684 This string is passed to `format', so percent characters need to be doubled.
685 This implementation requires the MIME::Base64 Perl module to be installed
686 on the remote host.")
688 (defconst tramp-perl-encode
690 # This script contributed by Juanma Barranquero <lektu@terra.es>.
691 # Copyright (C) 2002-2013 Free Software Foundation, Inc.
696 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
697 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
701 # We read in chunks of 54 bytes, to generate output lines
702 # of 72 chars (plus end of line)
703 while (read STDIN, $data, 54) {
706 # Only for the last chunk, and only if did not fill the last three-byte packet
708 my $mod = length($data) %% 3;
709 $pad = q(=) x (3 - $mod) if $mod;
712 # Not the fastest method, but it is simple: unpack to binary string, split
713 # by groups of 6 bits and convert back from binary to byte; then map into
714 # the translation table
718 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
722 "Perl program to use for encoding a file.
723 Escape sequence %s is replaced with name of Perl binary.
724 This string is passed to `format', so percent characters need to be doubled.")
726 (defconst tramp-perl-decode
728 # This script contributed by Juanma Barranquero <lektu@terra.es>.
729 # Copyright (C) 2002-2013 Free Software Foundation, Inc.
734 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
735 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
738 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
742 # We are going to accumulate into $pending to accept any line length
743 # (we do not check they are <= 76 chars as the RFC says)
746 while (my $data = <STDIN>) {
749 # If we find one or two =, we have reached the end and
750 # any following data is to be discarded
751 my $finished = $data =~ s/(==?).*/$1/;
754 my $len = length($pending);
755 my $chunk = substr($pending, 0, $len & ~3);
756 $pending = substr($pending, $len & ~3 + 1);
758 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
759 # split in 8-bit chunks and convert back to char.
762 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
766 "Perl program to use for decoding a file.
767 Escape sequence %s is replaced with name of Perl binary.
768 This string is passed to `format', so percent characters need to be doubled.")
770 (defconst tramp-perl-pack
771 "%s -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
772 "Perl program to use for encoding a file.
773 Escape sequence %s is replaced with name of Perl binary.")
775 (defconst tramp-perl-unpack
776 "%s -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"
777 "Perl program to use for decoding a file.
778 Escape sequence %s is replaced with name of Perl binary.")
780 (defconst tramp-vc-registered-read-file-names
783 if %s \"$file\"; then
784 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
786 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
788 if %s \"$file\"; then
789 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
791 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
795 "Script to check existence of VC related files.
796 It must be send formatted with two strings; the tests for file
797 existence, and file readability. Input shall be read via
798 here-document, otherwise the command could exceed maximum length
801 ;; New handlers should be added here. The following operations can be
802 ;; handled using the normal primitives: file-name-sans-versions,
804 (defconst tramp-sh-file-name-handler-alist
805 '((load . tramp-handle-load)
806 (make-symbolic-link . tramp-sh-handle-make-symbolic-link)
807 (file-name-as-directory . tramp-handle-file-name-as-directory)
808 (file-name-directory . tramp-handle-file-name-directory)
809 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
810 (file-truename . tramp-sh-handle-file-truename)
811 (file-exists-p . tramp-sh-handle-file-exists-p)
812 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
813 (file-directory-p . tramp-sh-handle-file-directory-p)
814 (file-executable-p . tramp-sh-handle-file-executable-p)
815 (file-readable-p . tramp-sh-handle-file-readable-p)
816 (file-regular-p . tramp-handle-file-regular-p)
817 (file-symlink-p . tramp-handle-file-symlink-p)
818 (file-writable-p . tramp-sh-handle-file-writable-p)
819 (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p)
820 (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p)
821 (file-attributes . tramp-sh-handle-file-attributes)
822 (file-modes . tramp-handle-file-modes)
823 (directory-files . tramp-handle-directory-files)
824 (directory-files-and-attributes
825 . tramp-sh-handle-directory-files-and-attributes)
826 (file-name-all-completions . tramp-sh-handle-file-name-all-completions)
827 (file-name-completion . tramp-handle-file-name-completion)
828 (add-name-to-file . tramp-sh-handle-add-name-to-file)
829 (copy-file . tramp-sh-handle-copy-file)
830 (copy-directory . tramp-sh-handle-copy-directory)
831 (rename-file . tramp-sh-handle-rename-file)
832 (set-file-modes . tramp-sh-handle-set-file-modes)
833 (set-file-times . tramp-sh-handle-set-file-times)
834 (make-directory . tramp-sh-handle-make-directory)
835 (delete-directory . tramp-sh-handle-delete-directory)
836 (delete-file . tramp-sh-handle-delete-file)
837 (directory-file-name . tramp-handle-directory-file-name)
838 ;; `executable-find' is not official yet.
839 (executable-find . tramp-sh-handle-executable-find)
840 (start-file-process . tramp-sh-handle-start-file-process)
841 (process-file . tramp-sh-handle-process-file)
842 (shell-command . tramp-handle-shell-command)
843 (insert-directory . tramp-sh-handle-insert-directory)
844 (expand-file-name . tramp-sh-handle-expand-file-name)
845 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
846 (file-local-copy . tramp-sh-handle-file-local-copy)
847 (file-remote-p . tramp-handle-file-remote-p)
848 (insert-file-contents . tramp-handle-insert-file-contents)
849 (insert-file-contents-literally
850 . tramp-sh-handle-insert-file-contents-literally)
851 (write-region . tramp-sh-handle-write-region)
852 (find-backup-file-name . tramp-handle-find-backup-file-name)
853 (make-auto-save-file-name . tramp-sh-handle-make-auto-save-file-name)
854 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
855 (dired-compress-file . tramp-sh-handle-dired-compress-file)
856 (dired-recursive-delete-directory
857 . tramp-sh-handle-dired-recursive-delete-directory)
858 (dired-uncache . tramp-handle-dired-uncache)
859 (set-visited-file-modtime . tramp-sh-handle-set-visited-file-modtime)
860 (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime)
861 (file-selinux-context . tramp-sh-handle-file-selinux-context)
862 (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context)
863 (file-acl . tramp-sh-handle-file-acl)
864 (set-file-acl . tramp-sh-handle-set-file-acl)
865 (vc-registered . tramp-sh-handle-vc-registered))
866 "Alist of handler functions.
867 Operations not mentioned here will be handled by the normal Emacs functions.")
869 ;; This must be the last entry, because `identity' always matches.
871 (add-to-list 'tramp-foreign-file-name-handler-alist
872 '(identity . tramp-sh-file-name-handler) 'append)
874 ;;; File Name Handler Functions:
876 (defun tramp-sh-handle-make-symbolic-link
877 (filename linkname &optional ok-if-already-exists)
878 "Like `make-symbolic-link' for Tramp files.
879 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
880 the symlink. If LINKNAME is a Tramp file, only the localname component is
881 used as the target of the symlink.
883 If LINKNAME is a Tramp file and the localname component is relative, then
884 it is expanded first, before the localname component is taken. Note that
885 this can give surprising results if the user/host for the source and
886 target of the symlink differ."
887 (with-parsed-tramp-file-name linkname l
888 (let ((ln (tramp-get-remote-ln l))
889 (cwd (tramp-run-real-handler
890 'file-name-directory (list l-localname))))
894 "Making a symbolic link. ln(1) does not exist on the remote host."))
896 ;; Do the 'confirm if exists' thing.
897 (when (file-exists-p linkname)
899 (if (or (null ok-if-already-exists) ; not allowed to exist
900 (and (numberp ok-if-already-exists)
903 "File %s already exists; make it a link anyway? "
906 l 'file-already-exists "File %s already exists" l-localname)
907 (delete-file linkname)))
909 ;; If FILENAME is a Tramp name, use just the localname component.
910 (when (tramp-tramp-file-p filename)
912 (tramp-file-name-localname
913 (tramp-dissect-file-name (expand-file-name filename)))))
915 (tramp-flush-file-property l (file-name-directory l-localname))
916 (tramp-flush-file-property l l-localname)
918 ;; Right, they are on the same host, regardless of user, method,
919 ;; etc. We now make the link on the remote machine. This will
920 ;; occur as the user that FILENAME belongs to.
921 (tramp-send-command-and-check
924 "cd %s && %s -sf %s %s"
925 (tramp-shell-quote-argument cwd)
927 (tramp-shell-quote-argument filename)
928 (tramp-shell-quote-argument l-localname))
931 (defun tramp-sh-handle-file-truename (filename &optional counter prev-dirs)
932 "Like `file-truename' for Tramp files."
933 (with-parsed-tramp-file-name (expand-file-name filename) nil
934 (tramp-make-tramp-file-name method user host
935 (with-tramp-file-property v localname "file-truename"
936 (let ((result nil)) ; result steps in reverse order
937 (tramp-message v 4 "Finding true name for `%s'" filename)
939 ;; Use GNU readlink --canonicalize-missing where available.
940 ((tramp-get-remote-readlink v)
942 (tramp-send-command-and-read
944 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
945 (tramp-get-remote-readlink v)
946 (tramp-shell-quote-argument localname)))))
948 ;; Use Perl implementation.
949 ((and (tramp-get-remote-perl v)
950 (tramp-get-connection-property v "perl-file-spec" nil)
951 (tramp-get-connection-property v "perl-cwd-realpath" nil))
952 (tramp-maybe-send-script
953 v tramp-perl-file-truename "tramp_perl_file_truename")
955 (tramp-send-command-and-read
957 (format "tramp_perl_file_truename %s"
958 (tramp-shell-quote-argument localname)))))
960 ;; Do it yourself. We bind `directory-sep-char' here for
961 ;; XEmacs on Windows, which would otherwise use backslash.
962 (t (let* ((directory-sep-char ?/)
963 (steps (tramp-compat-split-string localname "/"))
964 (localnamedir (tramp-run-real-handler
965 'file-name-as-directory (list localname)))
966 (is-dir (string= localname localnamedir))
969 ;; Don't make the following value larger than
970 ;; necessary. People expect an error message in
971 ;; a timely fashion when something is wrong;
972 ;; otherwise they might think that Emacs is hung.
973 ;; Of course, correctness has to come first.
976 (while (and steps (< numchase numchase-limit))
977 (setq thisstep (pop steps))
981 (append '("") (reverse result) (list thisstep))
984 (nth 0 (file-attributes
985 (tramp-make-tramp-file-name
992 (cond ((string= "." thisstep)
993 (tramp-message v 5 "Ignoring step `.'"))
994 ((string= ".." thisstep)
995 (tramp-message v 5 "Processing step `..'")
997 ((stringp symlink-target)
998 ;; It's a symlink, follow it.
1000 v 5 "Follow symlink to %s" symlink-target)
1001 (setq numchase (1+ numchase))
1002 (when (file-name-absolute-p symlink-target)
1004 ;; If the symlink was absolute, we'll get a
1005 ;; string like "/user@host:/some/target";
1006 ;; extract the "/some/target" part from it.
1007 (when (tramp-tramp-file-p symlink-target)
1008 (unless (tramp-equal-remote filename symlink-target)
1011 "Symlink target `%s' on wrong host"
1013 (setq symlink-target localname))
1015 (append (tramp-compat-split-string
1020 (setq result (cons thisstep result)))))
1021 (when (>= numchase numchase-limit)
1024 "Maximum number (%d) of symlinks exceeded" numchase-limit))
1025 (setq result (reverse result))
1026 ;; Combine list to form string.
1029 (mapconcat 'identity (cons "" result) "/")
1032 (or (string= "" result)
1033 (not (string= (substring result -1) "/"))))
1034 (setq result (concat result "/"))))))
1036 (tramp-message v 4 "True name of `%s' is `%s'" localname result)
1041 (defun tramp-sh-handle-file-exists-p (filename)
1042 "Like `file-exists-p' for Tramp files."
1043 (with-parsed-tramp-file-name filename nil
1044 (with-tramp-file-property v localname "file-exists-p"
1045 (or (not (null (tramp-get-file-property
1046 v localname "file-attributes-integer" nil)))
1047 (not (null (tramp-get-file-property
1048 v localname "file-attributes-string" nil)))
1049 (tramp-send-command-and-check
1053 (tramp-get-file-exists-command v)
1054 (tramp-shell-quote-argument localname)))))))
1056 (defun tramp-sh-handle-file-attributes (filename &optional id-format)
1057 "Like `file-attributes' for Tramp files."
1058 (unless id-format (setq id-format 'integer))
1059 ;; Don't modify `last-coding-system-used' by accident.
1060 (let ((last-coding-system-used last-coding-system-used))
1061 (with-parsed-tramp-file-name (expand-file-name filename) nil
1062 (with-tramp-file-property
1063 v localname (format "file-attributes-%s" id-format)
1065 (tramp-convert-file-attributes
1069 ((tramp-get-remote-stat v)
1070 (tramp-do-file-attributes-with-stat v localname id-format))
1071 ((tramp-get-remote-perl v)
1072 (tramp-do-file-attributes-with-perl v localname id-format))
1074 ;; The scripts could fail, for example with huge file size.
1075 (tramp-do-file-attributes-with-ls v localname id-format))))))))
1077 (defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
1078 "Implement `file-attributes' for Tramp files using the ls(1) command."
1080 res-inode res-filemodes res-numlinks
1081 res-uid res-gid res-size res-symlink-target)
1082 (tramp-message vec 5 "file attributes with ls: %s" localname)
1085 (format "(%s %s || %s -h %s) && %s %s %s"
1086 (tramp-get-file-exists-command vec)
1087 (tramp-shell-quote-argument localname)
1088 (tramp-get-test-command vec)
1089 (tramp-shell-quote-argument localname)
1090 (tramp-get-ls-command vec)
1091 (if (eq id-format 'integer) "-ildn" "-ild")
1092 (tramp-shell-quote-argument localname)))
1093 ;; parse `ls -l' output ...
1094 (with-current-buffer (tramp-get-buffer vec)
1095 (when (> (buffer-size) 0)
1096 (goto-char (point-min))
1100 (read (current-buffer))
1101 (invalid-read-syntax
1102 (when (and (equal (cadr err)
1103 "Integer constant overflow in reader")
1105 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1107 (let* ((big (read (substring (car (cddr err)) 0
1108 (match-beginning 1))))
1109 (small (read (match-string 1 (car (cddr err)))))
1110 (twiddle (/ small 65536)))
1111 (cons (+ big twiddle)
1112 (- small (* twiddle 65536))))))))
1113 ;; ... file mode flags
1114 (setq res-filemodes (symbol-name (read (current-buffer))))
1116 (setq res-numlinks (read (current-buffer)))
1118 (setq res-uid (read (current-buffer)))
1119 (setq res-gid (read (current-buffer)))
1120 (if (eq id-format 'integer)
1122 (unless (numberp res-uid) (setq res-uid -1))
1123 (unless (numberp res-gid) (setq res-gid -1)))
1125 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
1126 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
1128 (setq res-size (read (current-buffer)))
1129 ;; From the file modes, figure out other stuff.
1130 (setq symlinkp (eq ?l (aref res-filemodes 0)))
1131 (setq dirp (eq ?d (aref res-filemodes 0)))
1132 ;; if symlink, find out file name pointed to
1134 (search-forward "-> ")
1135 (setq res-symlink-target (buffer-substring (point) (point-at-eol))))
1136 ;; return data gathered
1138 ;; 0. t for directory, string (name linked to) for symbolic
1140 (or dirp res-symlink-target)
1141 ;; 1. Number of links to file.
1147 ;; 4. Last access time, as a list of integers. Normally this
1148 ;; would be in the same format as `current-time', but the
1149 ;; subseconds part is not currently implemented, and (0 0)
1150 ;; denotes an unknown time.
1151 ;; 5. Last modification time, likewise.
1152 ;; 6. Last status change time, likewise.
1153 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1154 ;; 7. Size in bytes (-1, if number is out of range).
1156 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1158 ;; 9. t if file's gid would change if file were deleted and
1159 ;; recreated. Will be set in `tramp-convert-file-attributes'
1161 ;; 10. inode number.
1163 ;; 11. Device number. Will be replaced by a virtual device number.
1167 (defun tramp-do-file-attributes-with-perl
1168 (vec localname &optional id-format)
1169 "Implement `file-attributes' for Tramp files using a Perl script."
1170 (tramp-message vec 5 "file attributes with perl: %s" localname)
1171 (tramp-maybe-send-script
1172 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
1173 (tramp-send-command-and-read
1175 (format "tramp_perl_file_attributes %s %s"
1176 (tramp-shell-quote-argument localname) id-format)))
1178 (defun tramp-do-file-attributes-with-stat
1179 (vec localname &optional id-format)
1180 "Implement `file-attributes' for Tramp files using stat(1) command."
1181 (tramp-message vec 5 "file attributes with stat: %s" localname)
1182 (tramp-send-command-and-read
1185 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
1186 ;; parse correctly the sequence "((". Therefore, we add a space.
1187 "( (%s %s || %s -h %s) && %s -c '((\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)' %s || echo nil)"
1188 (tramp-get-file-exists-command vec)
1189 (tramp-shell-quote-argument localname)
1190 (tramp-get-test-command vec)
1191 (tramp-shell-quote-argument localname)
1192 (tramp-get-remote-stat vec)
1193 (if (eq id-format 'integer) "%ue0" "\"%U\"")
1194 (if (eq id-format 'integer) "%ge0" "\"%G\"")
1195 (tramp-shell-quote-argument localname))))
1197 (defun tramp-sh-handle-set-visited-file-modtime (&optional time-list)
1198 "Like `set-visited-file-modtime' for Tramp files."
1199 (unless (buffer-file-name)
1200 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1203 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
1204 (let ((f (buffer-file-name))
1206 (with-parsed-tramp-file-name f nil
1207 (let* ((remote-file-name-inhibit-cache t)
1208 (attr (file-attributes f))
1209 ;; '(-1 65535) means file doesn't exists yet.
1210 (modtime (or (nth 5 attr) '(-1 65535))))
1211 (when (boundp 'last-coding-system-used)
1212 (setq coding-system-used (symbol-value 'last-coding-system-used)))
1213 ;; We use '(0 0) as a don't-know value. See also
1214 ;; `tramp-do-file-attributes-with-ls'.
1215 (if (not (equal modtime '(0 0)))
1216 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
1220 (format "%s -ild %s"
1221 (tramp-get-ls-command v)
1222 (tramp-shell-quote-argument localname)))
1223 (setq attr (buffer-substring (point)
1224 (progn (end-of-line) (point)))))
1225 (tramp-set-file-property
1226 v localname "visited-file-modtime-ild" attr))
1227 (when (boundp 'last-coding-system-used)
1228 (set 'last-coding-system-used coding-system-used))
1231 ;; This function makes the same assumption as
1232 ;; `tramp-sh-handle-set-visited-file-modtime'.
1233 (defun tramp-sh-handle-verify-visited-file-modtime (buf)
1234 "Like `verify-visited-file-modtime' for Tramp files.
1235 At the time `verify-visited-file-modtime' calls this function, we
1236 already know that the buffer is visiting a file and that
1237 `visited-file-modtime' does not return 0. Do not call this
1238 function directly, unless those two cases are already taken care
1240 (with-current-buffer buf
1241 (let ((f (buffer-file-name)))
1242 ;; There is no file visiting the buffer, or the buffer has no
1243 ;; recorded last modification time, or there is no established
1246 (eq (visited-file-modtime) 0)
1247 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
1249 (with-parsed-tramp-file-name f nil
1250 (let* ((remote-file-name-inhibit-cache t)
1251 (attr (file-attributes f))
1252 (modtime (nth 5 attr))
1253 (mt (visited-file-modtime)))
1256 ;; File exists, and has a known modtime.
1257 ((and attr (not (equal modtime '(0 0))))
1258 (< (abs (tramp-time-diff
1260 ;; For compatibility, deal with both the old
1261 ;; (HIGH . LOW) and the new (HIGH LOW) return
1262 ;; values of `visited-file-modtime'.
1264 (list (car mt) (cdr mt))
1267 ;; Modtime has the don't know value.
1271 (format "%s -ild %s"
1272 (tramp-get-ls-command v)
1273 (tramp-shell-quote-argument localname)))
1274 (with-current-buffer (tramp-get-buffer v)
1275 (setq attr (buffer-substring
1276 (point) (progn (end-of-line) (point)))))
1279 (tramp-get-file-property
1280 v localname "visited-file-modtime-ild" "")))
1281 ;; If file does not exist, say it is not modified if and
1282 ;; only if that agrees with the buffer's record.
1283 (t (equal mt '(-1 65535))))))))))
1285 (defun tramp-sh-handle-set-file-modes (filename mode)
1286 "Like `set-file-modes' for Tramp files."
1287 (with-parsed-tramp-file-name filename nil
1288 (tramp-flush-file-property v localname)
1289 ;; FIXME: extract the proper text from chmod's stderr.
1290 (tramp-barf-unless-okay
1292 (format "chmod %s %s"
1293 (tramp-compat-decimal-to-octal mode)
1294 (tramp-shell-quote-argument localname))
1295 "Error while changing file's mode %s" filename)))
1297 (defun tramp-sh-handle-set-file-times (filename &optional time)
1298 "Like `set-file-times' for Tramp files."
1299 (if (file-remote-p filename)
1300 (with-parsed-tramp-file-name filename nil
1301 (tramp-flush-file-property v localname)
1302 (let ((time (if (or (null time) (equal time '(0 0)))
1305 ;; With GNU Emacs, `format-time-string' has an optional
1306 ;; parameter UNIVERSAL. This is preferred, because we
1307 ;; could handle the case when the remote host is located
1308 ;; in a different time zone as the local host.
1309 (utc (not (featurep 'xemacs))))
1310 (tramp-send-command-and-check
1311 v (format "%s touch -t %s %s"
1312 (if utc "env TZ=UTC" "")
1314 (format-time-string "%Y%m%d%H%M.%S" time t)
1315 (format-time-string "%Y%m%d%H%M.%S" time))
1316 (tramp-shell-quote-argument localname)))))
1318 ;; We handle also the local part, because in older Emacsen,
1319 ;; without `set-file-times', this function is an alias for this.
1320 ;; We are local, so we don't need the UTC settings.
1323 "touch" nil nil nil "-t"
1324 (format-time-string "%Y%m%d%H%M.%S" time)
1325 (tramp-shell-quote-argument filename)))))
1327 (defun tramp-set-file-uid-gid (filename &optional uid gid)
1328 "Set the ownership for FILENAME.
1329 If UID and GID are provided, these values are used; otherwise uid
1330 and gid of the corresponding user is taken. Both parameters must
1331 be non-negative integers."
1332 ;; Modern Unices allow chown only for root. So we might need
1333 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1334 ;; working with su(do)? when it is needed, so it shall succeed in
1335 ;; the majority of cases.
1336 ;; Don't modify `last-coding-system-used' by accident.
1337 (let ((last-coding-system-used last-coding-system-used))
1338 (if (file-remote-p filename)
1339 (with-parsed-tramp-file-name filename nil
1340 (if (and (zerop (user-uid)) (tramp-local-host-p v))
1341 ;; If we are root on the local host, we can do it directly.
1342 (tramp-set-file-uid-gid localname uid gid)
1343 (let ((uid (or (and (natnump uid) uid)
1344 (tramp-get-remote-uid v 'integer)))
1345 (gid (or (and (natnump gid) gid)
1346 (tramp-get-remote-gid v 'integer))))
1349 "chown %d:%d %s" uid gid
1350 (tramp-shell-quote-argument localname))))))
1352 ;; We handle also the local part, because there doesn't exist
1353 ;; `set-file-uid-gid'. On W32 "chown" might not work.
1354 (let ((uid (or (and (natnump uid) uid) (tramp-get-local-uid 'integer)))
1355 (gid (or (and (natnump gid) gid) (tramp-get-local-gid 'integer))))
1358 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
1360 (defun tramp-remote-selinux-p (vec)
1361 "Check, whether SELINUX is enabled on the remote host."
1362 (with-tramp-connection-property
1363 (tramp-get-connection-process vec) "selinux-p"
1364 (let ((result (tramp-find-executable
1365 vec "getenforce" (tramp-get-remote-path vec) t t)))
1368 (tramp-send-command-and-read
1369 vec (format "echo \\\"`%S`\\\"" result))
1372 (defun tramp-sh-handle-file-selinux-context (filename)
1373 "Like `file-selinux-context' for Tramp files."
1374 (with-parsed-tramp-file-name filename nil
1375 (with-tramp-file-property v localname "file-selinux-context"
1376 (let ((context '(nil nil nil nil))
1377 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1378 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1379 (when (and (tramp-remote-selinux-p v)
1380 (tramp-send-command-and-check
1383 (tramp-get-ls-command v)
1384 (tramp-shell-quote-argument localname))))
1385 (with-current-buffer (tramp-get-connection-buffer v)
1386 (goto-char (point-min))
1387 (when (re-search-forward regexp (point-at-eol) t)
1388 (setq context (list (match-string 1) (match-string 2)
1389 (match-string 3) (match-string 4))))))
1390 ;; Return the context.
1393 (defun tramp-sh-handle-set-file-selinux-context (filename context)
1394 "Like `set-file-selinux-context' for Tramp files."
1395 (with-parsed-tramp-file-name filename nil
1396 (if (and (consp context)
1397 (tramp-remote-selinux-p v)
1398 (tramp-send-command-and-check
1399 v (format "chcon %s %s %s %s %s"
1400 (if (stringp (nth 0 context))
1401 (format "--user=%s" (nth 0 context)) "")
1402 (if (stringp (nth 1 context))
1403 (format "--role=%s" (nth 1 context)) "")
1404 (if (stringp (nth 2 context))
1405 (format "--type=%s" (nth 2 context)) "")
1406 (if (stringp (nth 3 context))
1407 (format "--range=%s" (nth 3 context)) "")
1408 (tramp-shell-quote-argument localname))))
1410 (tramp-set-file-property v localname "file-selinux-context" context)
1412 (tramp-set-file-property v localname "file-selinux-context" 'undef)
1415 (defun tramp-remote-acl-p (vec)
1416 "Check, whether ACL is enabled on the remote host."
1417 (with-tramp-connection-property (tramp-get-connection-process vec) "acl-p"
1418 (tramp-send-command-and-check vec "getfacl /")))
1420 (defun tramp-sh-handle-file-acl (filename)
1421 "Like `file-acl' for Tramp files."
1422 (with-parsed-tramp-file-name filename nil
1423 (with-tramp-file-property v localname "file-acl"
1424 (when (and (tramp-remote-acl-p v)
1425 (tramp-send-command-and-check
1427 "getfacl -ac %s 2>/dev/null"
1428 (tramp-shell-quote-argument localname))))
1429 (with-current-buffer (tramp-get-connection-buffer v)
1430 (goto-char (point-max))
1431 (delete-blank-lines)
1432 (when (> (point-max) (point-min))
1433 (tramp-compat-funcall
1434 'substring-no-properties (buffer-string))))))))
1436 (defun tramp-sh-handle-set-file-acl (filename acl-string)
1437 "Like `set-file-acl' for Tramp files."
1438 (with-parsed-tramp-file-name (expand-file-name filename) nil
1439 (if (and (stringp acl-string) (tramp-remote-acl-p v)
1442 v (format "setfacl --set-file=- %s <<'EOF'\n%s\nEOF\n"
1443 (tramp-shell-quote-argument localname) acl-string))
1444 (tramp-send-command-and-check v nil)))
1447 (tramp-set-file-property v localname "file-acl" acl-string)
1449 ;; In case of errors, we return `nil'.
1450 (tramp-set-file-property v localname "file-acl-string" 'undef)
1453 ;; Simple functions using the `test' command.
1455 (defun tramp-sh-handle-file-executable-p (filename)
1456 "Like `file-executable-p' for Tramp files."
1457 (with-parsed-tramp-file-name filename nil
1458 (with-tramp-file-property v localname "file-executable-p"
1459 ;; Examine `file-attributes' cache to see if request can be
1460 ;; satisfied without remote operation.
1461 (or (tramp-check-cached-permissions v ?x)
1462 (tramp-run-test "-x" filename)))))
1464 (defun tramp-sh-handle-file-readable-p (filename)
1465 "Like `file-readable-p' for Tramp files."
1466 (with-parsed-tramp-file-name filename nil
1467 (with-tramp-file-property v localname "file-readable-p"
1468 ;; Examine `file-attributes' cache to see if request can be
1469 ;; satisfied without remote operation.
1470 (or (tramp-check-cached-permissions v ?r)
1471 (tramp-run-test "-r" filename)))))
1473 ;; When the remote shell is started, it looks for a shell which groks
1474 ;; tilde expansion. Here, we assume that all shells which grok tilde
1475 ;; expansion will also provide a `test' command which groks `-nt' (for
1476 ;; newer than). If this breaks, tell me about it and I'll try to do
1477 ;; something smarter about it.
1478 (defun tramp-sh-handle-file-newer-than-file-p (file1 file2)
1479 "Like `file-newer-than-file-p' for Tramp files."
1480 (cond ((not (file-exists-p file1))
1482 ((not (file-exists-p file2))
1484 ;; We are sure both files exist at this point.
1487 ;; We try to get the mtime of both files. If they are not
1488 ;; equal to the "dont-know" value, then we subtract the times
1489 ;; and obtain the result.
1490 (let ((fa1 (file-attributes file1))
1491 (fa2 (file-attributes file2)))
1492 (if (and (not (equal (nth 5 fa1) '(0 0)))
1493 (not (equal (nth 5 fa2) '(0 0))))
1494 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
1495 ;; If one of them is the dont-know value, then we can
1496 ;; still try to run a shell command on the remote host.
1497 ;; However, this only works if both files are Tramp
1498 ;; files and both have the same method, same user, same
1500 (unless (tramp-equal-remote file1 file2)
1501 (with-parsed-tramp-file-name
1502 (if (tramp-tramp-file-p file1) file1 file2) nil
1505 "Files %s and %s must have same method, user, host"
1507 (with-parsed-tramp-file-name file1 nil
1509 (tramp-get-test-nt-command v) file1 file2))))))))
1511 ;; Functions implemented using the basic functions above.
1513 (defun tramp-sh-handle-file-directory-p (filename)
1514 "Like `file-directory-p' for Tramp files."
1515 (with-parsed-tramp-file-name filename nil
1516 ;; `file-directory-p' is used as predicate for filename completion.
1517 ;; Sometimes, when a connection is not established yet, it is
1518 ;; desirable to return t immediately for "/method:foo:". It can
1519 ;; be expected that this is always a directory.
1520 (or (zerop (length localname))
1521 (with-tramp-file-property v localname "file-directory-p"
1522 (tramp-run-test "-d" filename)))))
1524 (defun tramp-sh-handle-file-writable-p (filename)
1525 "Like `file-writable-p' for Tramp files."
1526 (with-parsed-tramp-file-name filename nil
1527 (with-tramp-file-property v localname "file-writable-p"
1528 (if (file-exists-p filename)
1529 ;; Examine `file-attributes' cache to see if request can be
1530 ;; satisfied without remote operation.
1531 (or (tramp-check-cached-permissions v ?w)
1532 (tramp-run-test "-w" filename))
1533 ;; If file doesn't exist, check if directory is writable.
1534 (and (tramp-run-test "-d" (file-name-directory filename))
1535 (tramp-run-test "-w" (file-name-directory filename)))))))
1537 (defun tramp-sh-handle-file-ownership-preserved-p (filename &optional group)
1538 "Like `file-ownership-preserved-p' for Tramp files."
1539 (with-parsed-tramp-file-name filename nil
1540 (with-tramp-file-property v localname "file-ownership-preserved-p"
1541 (let ((attributes (file-attributes filename)))
1542 ;; Return t if the file doesn't exist, since it's true that no
1543 ;; information would be lost by an (attempted) delete and create.
1544 (or (null attributes)
1546 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer))
1548 (= (nth 3 attributes) (tramp-get-remote-gid v 'integer)))))))))
1550 ;; Directory listings.
1552 (defun tramp-sh-handle-directory-files-and-attributes
1553 (directory &optional full match nosort id-format)
1554 "Like `directory-files-and-attributes' for Tramp files."
1555 (unless id-format (setq id-format 'integer))
1556 (when (file-directory-p directory)
1557 (setq directory (expand-file-name directory))
1560 (with-parsed-tramp-file-name directory nil
1561 (with-tramp-file-property
1563 (format "directory-files-and-attributes-%s" id-format)
1568 (tramp-convert-file-attributes v (cdr x))))
1570 ((tramp-get-remote-stat v)
1571 (tramp-do-directory-files-and-attributes-with-stat
1572 v localname id-format))
1573 ((tramp-get-remote-perl v)
1574 (tramp-do-directory-files-and-attributes-with-perl
1575 v localname id-format)))))))))
1579 (setq item (pop temp))
1580 (when (or (null match) (string-match match (car item)))
1582 (setcar item (expand-file-name (car item) directory)))
1583 (push item result)))
1587 (sort result (lambda (x y) (string< (car x) (car y))))))))
1589 (defun tramp-do-directory-files-and-attributes-with-perl
1590 (vec localname &optional id-format)
1591 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1592 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
1593 (tramp-maybe-send-script
1594 vec tramp-perl-directory-files-and-attributes
1595 "tramp_perl_directory_files_and_attributes")
1597 (tramp-send-command-and-read
1599 (format "tramp_perl_directory_files_and_attributes %s %s"
1600 (tramp-shell-quote-argument localname) id-format))))
1601 (when (stringp object) (tramp-error vec 'file-error object))
1604 (defun tramp-do-directory-files-and-attributes-with-stat
1605 (vec localname &optional id-format)
1606 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1607 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
1608 (tramp-send-command-and-read
1612 ;; We must care about filenames with spaces, or starting with
1613 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1614 ;; but it does not work on all remote systems. Therefore, we
1615 ;; quote the filenames via sed.
1616 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | "
1618 "'(\"%%n\" (\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)'"
1619 " 2>/dev/null); echo \")\"")
1620 (tramp-shell-quote-argument localname)
1621 (tramp-get-ls-command vec)
1622 (tramp-get-remote-stat vec)
1623 (if (eq id-format 'integer) "%ue0" "\"%U\"")
1624 (if (eq id-format 'integer) "%ge0" "\"%G\""))))
1626 ;; This function should return "foo/" for directories and "bar" for
1628 (defun tramp-sh-handle-file-name-all-completions (filename directory)
1629 "Like `file-name-all-completions' for Tramp files."
1630 (unless (save-match-data (string-match "/" filename))
1631 (with-parsed-tramp-file-name (expand-file-name directory) nil
1638 ;; Try cache entries for filename, filename with last
1639 ;; character removed, filename with last two characters
1640 ;; removed, ..., and finally the empty string - all
1641 ;; concatenated to the local directory name.
1642 (let ((remote-file-name-inhibit-cache
1643 (or remote-file-name-inhibit-cache
1644 tramp-completion-reread-directory-timeout)))
1646 ;; This is inefficient for very long filenames, pity
1647 ;; `reduce' is not available...
1654 (tramp-get-file-property
1656 (concat localname (substring filename 0 x))
1657 "file-name-all-completions"
1659 (when cache-hit (list cache-hit))))
1660 ;; We cannot use a length of 0, because file properties
1661 ;; for "foo" and "foo/" are identical.
1662 (tramp-compat-number-sequence (length filename) 1 -1)))))
1664 ;; Cache expired or no matching cache entry found so we need
1665 ;; to perform a remote operation.
1667 ;; Get a list of directories and files, including reliably
1668 ;; tagging the directories with a trailing '/'. Because I
1669 ;; rock. --daniel@danann.net
1671 ;; Changed to perform `cd' in the same remote op and only
1672 ;; get entries starting with `filename'. Capture any `cd'
1673 ;; error messages. Ensure any `cd' and `echo' aliases are
1677 (if (tramp-get-remote-perl v)
1679 (tramp-maybe-send-script
1680 v tramp-perl-file-name-all-completions
1681 "tramp_perl_file_name_all_completions")
1682 (format "tramp_perl_file_name_all_completions %s %s %d"
1683 (tramp-shell-quote-argument localname)
1684 (tramp-shell-quote-argument filename)
1686 ;; `read-file-name-completion-ignore-case'
1687 ;; is introduced with Emacs 22.1.
1689 'read-file-name-completion-ignore-case)
1690 'read-file-name-completion-ignore-case
1691 'completion-ignore-case))
1695 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
1696 ;; `ls' with wildcard might fail with `Argument
1697 ;; list too long' error in some corner cases; if
1698 ;; `ls' fails after `cd' succeeded, chances are
1699 ;; that's the case, so let's retry without
1700 ;; wildcard. This will return "too many" entries
1701 ;; but that isn't harmful.
1702 " || %s -a 2>/dev/null)"
1703 " | while read f; do"
1704 " if %s -d \"$f\" 2>/dev/null;"
1705 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1706 " && \\echo ok) || \\echo fail")
1707 (tramp-shell-quote-argument localname)
1708 (tramp-get-ls-command v)
1709 ;; When `filename' is empty, just `ls' without
1710 ;; filename argument is more efficient than `ls *'
1711 ;; for very large directories and might avoid the
1712 ;; `Argument list too long' error.
1714 ;; With and only with wildcard, we need to add
1715 ;; `-d' to prevent `ls' from descending into
1717 (if (zerop (length filename))
1719 (concat (tramp-shell-quote-argument filename) "* -d"))
1720 (tramp-get-ls-command v)
1721 (tramp-get-test-command v))))
1723 ;; Now grab the output.
1724 (with-current-buffer (tramp-get-buffer v)
1725 (goto-char (point-max))
1727 ;; Check result code, found in last line of output.
1729 (if (looking-at "^fail$")
1731 ;; Grab error message from line before last line
1732 ;; (it was put there by `cd 2>&1').
1736 "tramp-sh-handle-file-name-all-completions: %s"
1737 (buffer-substring (point) (point-at-eol))))
1738 ;; For peace of mind, if buffer doesn't end in `fail'
1739 ;; then it should end in `ok'. If neither are in the
1740 ;; buffer something went seriously wrong on the remote
1742 (unless (looking-at "^ok$")
1746 tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
1747 (tramp-shell-quote-argument localname) (buffer-string))))
1749 (while (zerop (forward-line -1))
1750 (push (buffer-substring (point) (point-at-eol)) result)))
1752 ;; Because the remote op went through OK we know the
1753 ;; directory we `cd'-ed to exists.
1754 (tramp-set-file-property v localname "file-exists-p" t)
1756 ;; Because the remote op went through OK we know every
1757 ;; file listed by `ls' exists.
1758 (mapc (lambda (entry)
1759 (tramp-set-file-property
1760 v (concat localname entry) "file-exists-p" t))
1763 ;; Store result in the cache.
1764 (tramp-set-file-property
1765 v (concat localname filename)
1766 "file-name-all-completions" result))))))))
1770 (defun tramp-sh-handle-add-name-to-file
1771 (filename newname &optional ok-if-already-exists)
1772 "Like `add-name-to-file' for Tramp files."
1773 (unless (tramp-equal-remote filename newname)
1774 (with-parsed-tramp-file-name
1775 (if (tramp-tramp-file-p filename) filename newname) nil
1778 "add-name-to-file: %s"
1779 "only implemented for same method, same user, same host")))
1780 (with-parsed-tramp-file-name filename v1
1781 (with-parsed-tramp-file-name newname v2
1782 (let ((ln (when v1 (tramp-get-remote-ln v1))))
1783 (when (and (not ok-if-already-exists)
1784 (file-exists-p newname)
1785 (not (numberp ok-if-already-exists))
1788 "File %s already exists; make it a new name anyway? "
1792 "add-name-to-file: file %s already exists" newname))
1793 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1794 (tramp-flush-file-property v2 v2-localname)
1795 (tramp-barf-unless-okay
1797 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
1798 (tramp-shell-quote-argument v2-localname))
1799 "error with add-name-to-file, see buffer `%s' for details"
1802 (defun tramp-sh-handle-copy-file
1803 (filename newname &optional ok-if-already-exists keep-date
1804 preserve-uid-gid preserve-extended-attributes)
1805 "Like `copy-file' for Tramp files."
1806 (setq filename (expand-file-name filename))
1807 (setq newname (expand-file-name newname))
1809 ;; At least one file a Tramp file?
1810 ((or (tramp-tramp-file-p filename)
1811 (tramp-tramp-file-p newname))
1812 (tramp-do-copy-or-rename-file
1813 'copy filename newname ok-if-already-exists keep-date
1814 preserve-uid-gid preserve-extended-attributes))
1816 (preserve-extended-attributes
1817 (tramp-run-real-handler
1819 (list filename newname ok-if-already-exists keep-date
1820 preserve-uid-gid preserve-extended-attributes)))
1822 (tramp-run-real-handler
1824 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
1826 (tramp-run-real-handler
1827 'copy-file (list filename newname ok-if-already-exists keep-date)))))
1829 (defun tramp-sh-handle-copy-directory
1830 (dirname newname &optional keep-date parents copy-contents)
1831 "Like `copy-directory' for Tramp files."
1832 (let ((t1 (tramp-tramp-file-p dirname))
1833 (t2 (tramp-tramp-file-p newname)))
1834 (with-parsed-tramp-file-name (if t1 dirname newname) nil
1835 (if (and (tramp-get-method-parameter method 'tramp-copy-recursive)
1836 ;; When DIRNAME and NEWNAME are remote, they must have
1838 (or (null t1) (null t2)
1840 (tramp-file-name-method (tramp-dissect-file-name dirname))
1841 (tramp-file-name-method (tramp-dissect-file-name newname)))))
1842 ;; scp or rsync DTRT.
1844 (setq dirname (directory-file-name (expand-file-name dirname))
1845 newname (directory-file-name (expand-file-name newname)))
1846 (if (and (file-directory-p newname)
1847 (not (string-equal (file-name-nondirectory dirname)
1848 (file-name-nondirectory newname))))
1851 (file-name-nondirectory dirname) newname)))
1852 (if (not (file-directory-p (file-name-directory newname)))
1853 (make-directory (file-name-directory newname) parents))
1854 (tramp-do-copy-or-rename-file-out-of-band
1855 'copy dirname newname keep-date))
1856 ;; We must do it file-wise.
1857 (tramp-run-real-handler
1858 'copy-directory (list dirname newname keep-date parents)))
1860 ;; When newname did exist, we have wrong cached values.
1862 (with-parsed-tramp-file-name newname nil
1863 (tramp-flush-file-property v (file-name-directory localname))
1864 (tramp-flush-file-property v localname))))))
1866 (defun tramp-sh-handle-rename-file
1867 (filename newname &optional ok-if-already-exists)
1868 "Like `rename-file' for Tramp files."
1869 ;; Check if both files are local -- invoke normal rename-file.
1870 ;; Otherwise, use Tramp from local system.
1871 (setq filename (expand-file-name filename))
1872 (setq newname (expand-file-name newname))
1873 ;; At least one file a Tramp file?
1874 (if (or (tramp-tramp-file-p filename)
1875 (tramp-tramp-file-p newname))
1876 (tramp-do-copy-or-rename-file
1877 'rename filename newname ok-if-already-exists t t)
1878 (tramp-run-real-handler
1879 'rename-file (list filename newname ok-if-already-exists))))
1881 (defun tramp-do-copy-or-rename-file
1882 (op filename newname &optional ok-if-already-exists keep-date
1883 preserve-uid-gid preserve-extended-attributes)
1884 "Copy or rename a remote file.
1885 OP must be `copy' or `rename' and indicates the operation to perform.
1886 FILENAME specifies the file to copy or rename, NEWNAME is the name of
1887 the new file (for copy) or the new name of the file (for rename).
1888 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
1889 KEEP-DATE means to make sure that NEWNAME has the same timestamp
1890 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
1891 the uid and gid if both files are on the same host.
1892 PRESERVE-EXTENDED-ATTRIBUTES activates selinux and acl commands.
1894 This function is invoked by `tramp-sh-handle-copy-file' and
1895 `tramp-sh-handle-rename-file'. It is an error if OP is neither
1896 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
1898 (unless (memq op '(copy rename))
1899 (error "Unknown operation `%s', must be `copy' or `rename'" op))
1900 (let ((t1 (tramp-tramp-file-p filename))
1901 (t2 (tramp-tramp-file-p newname))
1902 (length (nth 7 (file-attributes (file-truename filename))))
1903 (attributes (and preserve-extended-attributes
1904 (apply 'file-extended-attributes (list filename))))
1907 (with-parsed-tramp-file-name (if t1 filename newname) nil
1908 (when (and (not ok-if-already-exists) (file-exists-p newname))
1910 v 'file-already-exists "File %s already exists" newname))
1912 (with-tramp-progress-reporter
1913 v 0 (format "%s %s to %s"
1914 (if (eq op 'copy) "Copying" "Renaming")
1918 ;; Both are Tramp files.
1920 (with-parsed-tramp-file-name filename v1
1921 (with-parsed-tramp-file-name newname v2
1923 ;; Shortcut: if method, host, user are the same for
1924 ;; both files, we invoke `cp' or `mv' on the remote
1926 ((tramp-equal-remote filename newname)
1927 (tramp-do-copy-or-rename-file-directly
1929 ok-if-already-exists keep-date preserve-uid-gid))
1931 ;; Try out-of-band operation.
1933 (tramp-method-out-of-band-p v1 length)
1934 (tramp-method-out-of-band-p v2 length))
1935 (tramp-do-copy-or-rename-file-out-of-band
1936 op filename newname keep-date))
1938 ;; No shortcut was possible. So we copy the file
1939 ;; first. If the operation was `rename', we go back
1940 ;; and delete the original file (if the copy was
1941 ;; successful). The approach is simple-minded: we
1942 ;; create a new buffer, insert the contents of the
1943 ;; source file into it, then write out the buffer to
1944 ;; the target file. The advantage is that it doesn't
1945 ;; matter which filename handlers are used for the
1946 ;; source and target file.
1948 (tramp-do-copy-or-rename-file-via-buffer
1949 op filename newname keep-date))))))
1951 ;; One file is a Tramp file, the other one is local.
1954 ;; Fast track on local machine.
1955 ((tramp-local-host-p v)
1956 (tramp-do-copy-or-rename-file-directly
1958 ok-if-already-exists keep-date preserve-uid-gid))
1960 ;; If the Tramp file has an out-of-band method, the
1961 ;; corresponding copy-program can be invoked.
1962 ((tramp-method-out-of-band-p v length)
1963 (tramp-do-copy-or-rename-file-out-of-band
1964 op filename newname keep-date))
1966 ;; Use the inline method via a Tramp buffer.
1967 (t (tramp-do-copy-or-rename-file-via-buffer
1968 op filename newname keep-date))))
1971 ;; One of them must be a Tramp file.
1972 (error "Tramp implementation says this cannot happen")))
1974 ;; Handle `preserve-extended-attributes'. We ignore possible
1975 ;; errors, because ACL strings could be incompatible.
1978 (apply 'set-file-extended-attributes (list newname attributes))))
1980 ;; In case of `rename', we must flush the cache of the source file.
1981 (when (and t1 (eq op 'rename))
1982 (with-parsed-tramp-file-name filename v1
1983 (tramp-flush-file-property v1 (file-name-directory localname))
1984 (tramp-flush-file-property v1 localname)))
1986 ;; When newname did exist, we have wrong cached values.
1988 (with-parsed-tramp-file-name newname v2
1989 (tramp-flush-file-property v2 (file-name-directory localname))
1990 (tramp-flush-file-property v2 localname)))))))
1992 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
1993 "Use an Emacs buffer to copy or rename a file.
1994 First arg OP is either `copy' or `rename' and indicates the operation.
1995 FILENAME is the source file, NEWNAME the target file.
1996 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
1998 ;; We must disable multibyte, because binary data shall not be
2000 (set-buffer-multibyte nil)
2001 (let ((coding-system-for-read 'binary)
2002 (jka-compr-inhibit t))
2003 (insert-file-contents-literally filename))
2004 ;; We don't want the target file to be compressed, so we let-bind
2005 ;; `jka-compr-inhibit' to t.
2006 (let ((coding-system-for-write 'binary)
2007 (jka-compr-inhibit t))
2008 (write-region (point-min) (point-max) newname)))
2009 ;; KEEP-DATE handling.
2010 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
2012 (set-file-modes newname (tramp-default-file-modes filename))
2013 ;; If the operation was `rename', delete the original file.
2014 (unless (eq op 'copy) (delete-file filename)))
2016 (defun tramp-do-copy-or-rename-file-directly
2017 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
2018 "Invokes `cp' or `mv' on the remote system.
2019 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2020 respectively. FILENAME specifies the file to copy or rename,
2021 NEWNAME is the name of the new file (for copy) or the new name of
2022 the file (for rename). Both files must reside on the same host.
2023 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2024 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2025 the uid and gid from FILENAME."
2026 (let ((t1 (tramp-tramp-file-p filename))
2027 (t2 (tramp-tramp-file-p newname))
2028 (file-times (nth 5 (file-attributes filename)))
2029 (file-modes (tramp-default-file-modes filename)))
2030 (with-parsed-tramp-file-name (if t1 filename newname) nil
2031 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
2032 ((eq op 'copy) "cp -f")
2033 ((eq op 'rename) "mv -f")
2036 "Unknown operation `%s', must be `copy' or `rename'"
2040 (tramp-file-name-handler 'file-remote-p filename 'localname)
2044 (tramp-file-name-handler 'file-remote-p newname 'localname)
2046 (prefix (file-remote-p (if t1 filename newname)))
2050 ;; Both files are on a remote host, with same user.
2053 (tramp-send-command-and-check
2054 v (format "%s %s %s" cmd
2055 (tramp-shell-quote-argument localname1)
2056 (tramp-shell-quote-argument localname2))))
2057 (with-current-buffer (tramp-get-buffer v)
2058 (goto-char (point-min))
2062 ;; Mask cp -f error.
2064 tramp-operation-not-permitted-regexp nil t))
2066 (tramp-error-with-buffer
2068 "Copying directly failed, see buffer `%s' for details."
2071 ;; We are on the local host.
2074 ;; We can do it directly.
2075 ((let (file-name-handler-alist)
2076 (and (file-readable-p localname1)
2077 (file-writable-p (file-name-directory localname2))
2078 (or (file-directory-p localname2)
2079 (file-writable-p localname2))))
2081 (tramp-compat-copy-file
2082 localname1 localname2 ok-if-already-exists
2083 keep-date preserve-uid-gid)
2084 (tramp-run-real-handler
2085 'rename-file (list localname1 localname2 ok-if-already-exists))))
2087 ;; We can do it directly with `tramp-send-command'
2088 ((and (file-readable-p (concat prefix localname1))
2090 (file-name-directory (concat prefix localname2)))
2091 (or (file-directory-p (concat prefix localname2))
2092 (file-writable-p (concat prefix localname2))))
2093 (tramp-do-copy-or-rename-file-directly
2094 op (concat prefix localname1) (concat prefix localname2)
2095 ok-if-already-exists keep-date t)
2096 ;; We must change the ownership to the local user.
2097 (tramp-set-file-uid-gid
2098 (concat prefix localname2)
2099 (tramp-get-local-uid 'integer)
2100 (tramp-get-local-gid 'integer)))
2102 ;; We need a temporary file in between.
2104 ;; Create the temporary file.
2105 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
2110 (tramp-barf-unless-okay
2113 (tramp-shell-quote-argument localname1)
2114 (tramp-shell-quote-argument tmpfile))
2115 "Copying directly failed, see buffer `%s' for details."
2116 (tramp-get-buffer v))
2117 ;; We must change the ownership as remote user.
2118 ;; Since this does not work reliable, we also
2119 ;; give read permissions.
2121 (concat prefix tmpfile)
2122 (tramp-compat-octal-to-decimal "0777"))
2123 (tramp-set-file-uid-gid
2124 (concat prefix tmpfile)
2125 (tramp-get-local-uid 'integer)
2126 (tramp-get-local-gid 'integer)))
2129 (tramp-compat-copy-file
2130 localname1 tmpfile t
2131 keep-date preserve-uid-gid)
2132 (tramp-run-real-handler
2134 (list localname1 tmpfile t)))
2135 ;; We must change the ownership as local user.
2136 ;; Since this does not work reliable, we also
2137 ;; give read permissions.
2139 tmpfile (tramp-compat-octal-to-decimal "0777"))
2140 (tramp-set-file-uid-gid
2142 (tramp-get-remote-uid v 'integer)
2143 (tramp-get-remote-gid v 'integer))))
2145 ;; Move the temporary file to its destination.
2148 (tramp-barf-unless-okay
2151 (tramp-shell-quote-argument tmpfile)
2152 (tramp-shell-quote-argument localname2))
2153 "Copying directly failed, see buffer `%s' for details."
2154 (tramp-get-buffer v)))
2156 (tramp-run-real-handler
2158 (list tmpfile localname2 ok-if-already-exists)))))
2161 (ignore-errors (delete-file tmpfile)))))))))
2163 ;; Set the time and mode. Mask possible errors.
2166 (set-file-times newname file-times)
2167 (set-file-modes newname file-modes))))))
2169 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
2170 "Invoke rcp program to copy.
2171 The method used must be an out-of-band method."
2172 (let* ((t1 (tramp-tramp-file-p filename))
2173 (t2 (tramp-tramp-file-p newname))
2174 (orig-vec (tramp-dissect-file-name (if t1 filename newname)))
2175 copy-program copy-args copy-env copy-keep-date port spec
2176 options source target)
2178 (with-parsed-tramp-file-name (if t1 filename newname) nil
2181 ;; Both are Tramp files. We shall optimize it when the
2182 ;; methods for filename and newname are the same.
2183 (let* ((dir-flag (file-directory-p filename))
2184 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
2188 (file-name-nondirectory newname) tmpfile)))
2191 (tramp-do-copy-or-rename-file-out-of-band
2192 op filename tmpfile keep-date)
2193 (tramp-do-copy-or-rename-file-out-of-band
2194 'rename tmpfile newname keep-date))
2198 (tramp-compat-delete-directory
2199 (expand-file-name ".." tmpfile) 'recursive)
2200 (delete-file tmpfile)))))
2202 ;; Set variables for computing the prompt for reading
2204 (setq tramp-current-method (tramp-file-name-method v)
2205 tramp-current-user (or (tramp-file-name-user v)
2206 (tramp-get-connection-property
2208 tramp-current-host (tramp-file-name-real-host v))
2210 ;; Expand hops. Might be necessary for gateway methods.
2211 (setq v (car (tramp-compute-multi-hops v)))
2212 (aset v 3 localname)
2214 ;; Check which ones of source and target are Tramp files.
2215 (setq source (if t1 (tramp-make-copy-program-file-name v) filename)
2217 (if (and (file-directory-p filename)
2219 (file-name-nondirectory filename)
2220 (file-name-nondirectory newname)))
2221 'file-name-directory
2223 (if t2 (tramp-make-copy-program-file-name v) newname)))
2225 ;; Check for host and port number. We cannot use
2226 ;; `tramp-file-name-port', because this returns also
2227 ;; `tramp-default-port', which might clash with settings in
2229 (setq host (tramp-file-name-host v)
2231 (when (string-match tramp-host-with-port-regexp host)
2232 (setq port (string-to-number (match-string 2 host))
2233 host (string-to-number (match-string 1 host))))
2235 ;; Check for user. There might be an interactive setting.
2236 (setq user (or (tramp-file-name-user v)
2237 (tramp-get-connection-property v "login-as" nil)))
2239 ;; Compose copy command.
2240 (setq host (or host "")
2243 spec (format-spec-make
2244 ?t (tramp-get-connection-property
2245 (tramp-get-connection-process v) "temp-file" ""))
2246 options (format-spec tramp-ssh-controlmaster-options spec)
2247 spec (format-spec-make
2248 ?h host ?u user ?p port ?c options
2249 ?k (if keep-date " " ""))
2250 copy-program (tramp-get-method-parameter
2251 method 'tramp-copy-program)
2252 copy-keep-date (tramp-get-method-parameter
2253 method 'tramp-copy-keep-date)
2256 ;; " " has either been a replacement of "%k" (when
2257 ;; keep-date argument is non-nil), or a replacement
2258 ;; for the whole keep-date sublist.
2262 (tramp-get-method-parameter method 'tramp-copy-args)
2267 (let ((y (mapcar (lambda (z) (format-spec z spec)) x)))
2268 (if (member "" y) '(" ") y))))))
2274 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
2275 (unless (member "" x) (mapconcat 'identity x " ")))
2276 (tramp-get-method-parameter method 'tramp-copy-env))))
2278 ;; Check for program.
2279 (unless (let ((default-directory
2280 (tramp-compat-temporary-file-directory)))
2281 (executable-find copy-program))
2283 v 'file-error "Cannot find copy program: %s" copy-program))
2287 ;; The default directory must be remote.
2288 (let ((default-directory
2289 (file-name-directory (if t1 filename newname)))
2290 (process-environment (copy-sequence process-environment)))
2291 ;; Set the transfer process properties.
2292 (tramp-set-connection-property
2293 v "process-name" (buffer-name (current-buffer)))
2294 (tramp-set-connection-property
2295 v "process-buffer" (current-buffer))
2298 orig-vec 6 "%s=\"%s\"" (car copy-env) (cadr copy-env))
2299 (setenv (pop copy-env) (pop copy-env)))
2301 ;; Use an asynchronous process. By this, password can
2302 ;; be handled. The default directory must be local, in
2303 ;; order to apply the correct `copy-program'. We don't
2304 ;; set a timeout, because the copying of large files can
2305 ;; last longer than 60 secs.
2306 (let ((p (let ((default-directory
2307 (tramp-compat-temporary-file-directory)))
2308 (apply 'start-process-shell-command
2309 (tramp-get-connection-name v)
2310 (tramp-get-connection-buffer v)
2315 (shell-quote-argument source)
2316 (shell-quote-argument target)
2317 "&&" "echo" "tramp_exit_status" "0"
2318 "||" "echo" "tramp_exit_status" "1"))))))
2321 (mapconcat 'identity (process-command p) " "))
2322 (tramp-compat-set-process-query-on-exit-flag p nil)
2323 (tramp-process-actions
2324 p v nil tramp-actions-copy-out-of-band)
2326 ;; Check the return code.
2327 (goto-char (point-max))
2329 (re-search-backward "tramp_exit_status [0-9]+" nil t)
2331 orig-vec 'file-error
2332 "Couldn't find exit status of `%s'" (process-command p)))
2333 (skip-chars-forward "^ ")
2334 (unless (zerop (read (current-buffer)))
2337 orig-vec 'file-error
2338 "Error copying: `%s'"
2339 (buffer-substring (point-min) (point-at-eol))))))
2341 ;; Reset the transfer process properties.
2342 (tramp-message orig-vec 6 "\n%s" (buffer-string))
2343 (tramp-set-connection-property v "process-name" nil)
2344 (tramp-set-connection-property v "process-buffer" nil)))
2346 ;; Handle KEEP-DATE argument.
2347 (when (and keep-date (not copy-keep-date))
2348 (set-file-times newname (nth 5 (file-attributes filename))))
2351 (unless (and keep-date copy-keep-date)
2353 (set-file-modes newname (tramp-default-file-modes filename)))))
2355 ;; If the operation was `rename', delete the original file.
2356 (unless (eq op 'copy)
2357 (if (file-regular-p filename)
2358 (delete-file filename)
2359 (tramp-compat-delete-directory filename 'recursive))))))
2361 (defun tramp-sh-handle-make-directory (dir &optional parents)
2362 "Like `make-directory' for Tramp files."
2363 (setq dir (expand-file-name dir))
2364 (with-parsed-tramp-file-name dir nil
2365 (tramp-flush-directory-property v (file-name-directory localname))
2367 (tramp-barf-unless-okay
2369 (if parents "mkdir -p" "mkdir")
2370 (tramp-shell-quote-argument localname))
2371 "Couldn't make directory %s" dir))))
2373 (defun tramp-sh-handle-delete-directory (directory &optional recursive)
2374 "Like `delete-directory' for Tramp files."
2375 (setq directory (expand-file-name directory))
2376 (with-parsed-tramp-file-name directory nil
2377 (tramp-flush-file-property v (file-name-directory localname))
2378 (tramp-flush-directory-property v localname)
2379 (tramp-barf-unless-okay
2381 (if recursive "rm -rf" "rmdir")
2382 (tramp-shell-quote-argument localname))
2383 "Couldn't delete %s" directory)))
2385 (defun tramp-sh-handle-delete-file (filename &optional trash)
2386 "Like `delete-file' for Tramp files."
2387 (setq filename (expand-file-name filename))
2388 (with-parsed-tramp-file-name filename nil
2389 (tramp-flush-file-property v (file-name-directory localname))
2390 (tramp-flush-file-property v localname)
2391 (tramp-barf-unless-okay
2393 (or (and trash (tramp-get-remote-trash v)) "rm -f")
2394 (tramp-shell-quote-argument localname))
2395 "Couldn't delete %s" filename)))
2399 ;; CCC: This does not seem to be enough. Something dies when
2400 ;; we try and delete two directories under Tramp :/
2401 (defun tramp-sh-handle-dired-recursive-delete-directory (filename)
2402 "Recursively delete the directory given.
2403 This is like `dired-recursive-delete-directory' for Tramp files."
2404 (with-parsed-tramp-file-name filename nil
2405 ;; Run a shell command 'rm -r <localname>'.
2406 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
2407 (unless (file-exists-p filename)
2408 (tramp-error v 'file-error "No such directory: %s" filename))
2409 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>).
2412 (format "rm -rf %s" (tramp-shell-quote-argument localname))
2413 ;; Don't read the output, do it explicitly.
2415 ;; Wait for the remote system to return to us...
2416 ;; This might take a while, allow it plenty of time.
2417 (tramp-wait-for-output (tramp-get-connection-process v) 120)
2418 ;; Make sure that it worked...
2419 (tramp-flush-file-property v (file-name-directory localname))
2420 (tramp-flush-directory-property v localname)
2421 (and (file-exists-p filename)
2423 v 'file-error "Failed to recursively delete %s" filename))))
2425 (defun tramp-sh-handle-dired-compress-file (file &rest ok-flag)
2426 "Like `dired-compress-file' for Tramp files."
2427 ;; OK-FLAG is valid for XEmacs only, but not implemented.
2428 ;; Code stolen mainly from dired-aux.el.
2429 (with-parsed-tramp-file-name file nil
2430 (tramp-flush-file-property v localname)
2433 (if (not (featurep 'xemacs))
2435 (symbol-value 'dired-compress-file-suffixes)
2436 ;; XEmacs has `dired-compression-method-alist', which is
2437 ;; transformed into `dired-compress-file-suffixes' structure.
2440 (list (concat (regexp-quote (nth 1 x)) "\\'")
2442 (mapconcat 'identity (nth 3 x) " ")))
2443 (symbol-value 'dired-compression-method-alist))))
2445 ;; See if any suffix rule matches this file name.
2447 (let (case-fold-search)
2448 (if (string-match (car (car suffixes)) localname)
2449 (setq suffix (car suffixes) suffixes nil))
2450 (setq suffixes (cdr suffixes))))
2452 (cond ((file-symlink-p file)
2454 ((and suffix (nth 2 suffix))
2455 ;; We found an uncompression rule.
2456 (with-tramp-progress-reporter
2457 v 0 (format "Uncompressing %s" file)
2458 (when (tramp-send-command-and-check
2459 v (concat (nth 2 suffix) " "
2460 (tramp-shell-quote-argument localname)))
2461 ;; `dired-remove-file' is not defined in XEmacs.
2462 (tramp-compat-funcall 'dired-remove-file file)
2463 (string-match (car suffix) file)
2464 (concat (substring file 0 (match-beginning 0))))))
2466 ;; We don't recognize the file as compressed, so compress it.
2468 (with-tramp-progress-reporter v 0 (format "Compressing %s" file)
2469 (when (tramp-send-command-and-check
2470 v (concat "gzip -f "
2471 (tramp-shell-quote-argument localname)))
2472 ;; `dired-remove-file' is not defined in XEmacs.
2473 (tramp-compat-funcall 'dired-remove-file file)
2474 (cond ((file-exists-p (concat file ".gz"))
2475 (concat file ".gz"))
2476 ((file-exists-p (concat file ".z"))
2480 (defun tramp-sh-handle-insert-directory
2481 (filename switches &optional wildcard full-directory-p)
2482 "Like `insert-directory' for Tramp files."
2483 (setq filename (expand-file-name filename))
2484 (with-parsed-tramp-file-name filename nil
2485 (if (and (featurep 'ls-lisp)
2486 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
2487 (tramp-run-real-handler
2488 'insert-directory (list filename switches wildcard full-directory-p))
2489 (when (stringp switches)
2490 (setq switches (split-string switches)))
2491 (when (and (member "--dired" switches)
2492 (not (tramp-get-ls-command-with-dired v)))
2493 (setq switches (delete "--dired" switches)))
2495 (setq wildcard (tramp-run-real-handler
2496 'file-name-nondirectory (list localname)))
2497 (setq localname (tramp-run-real-handler
2498 'file-name-directory (list localname))))
2499 (unless full-directory-p
2500 (setq switches (add-to-list 'switches "-d" 'append)))
2501 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
2503 (setq switches (concat switches " " wildcard)))
2505 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2506 switches filename (if wildcard "yes" "no")
2507 (if full-directory-p "yes" "no"))
2508 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2509 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2510 ;; "--dired" returns byte positions. Therefore, the file names
2511 ;; must be encoded, which is guaranteed by "LC_ALL=en_US.utf8
2513 (if full-directory-p
2516 (format "env LC_ALL=en_US.utf8 LC_CTYPE='' %s %s %s 2>/dev/null"
2517 (tramp-get-ls-command v)
2521 (tramp-shell-quote-argument (concat localname ".")))))
2522 (tramp-barf-unless-okay
2524 (format "cd %s" (tramp-shell-quote-argument
2525 (tramp-run-real-handler
2526 'file-name-directory (list localname))))
2528 (tramp-shell-quote-argument
2529 (tramp-run-real-handler 'file-name-directory (list localname))))
2532 (format "env LC_ALL=en_US.utf8 LC_CTYPE='' %s %s %s 2>/dev/null"
2533 (tramp-get-ls-command v)
2537 (tramp-run-real-handler
2538 'file-name-nondirectory (list localname)))))
2540 (tramp-shell-quote-argument
2541 (tramp-run-real-handler
2542 'file-name-nondirectory (list localname)))))))
2543 (let ((beg (point)))
2544 ;; We cannot use `insert-buffer-substring' because the Tramp
2545 ;; buffer changes its contents before insertion due to calling
2546 ;; `expand-file' and alike.
2548 (with-current-buffer (tramp-get-buffer v)
2551 ;; Check for "--dired" output.
2553 (when (looking-at "//SUBDIRED//")
2555 (when (looking-at "//DIRED//\\s-+")
2556 (let ((databeg (match-end 0))
2557 (end (point-at-eol)))
2558 ;; Now read the numeric positions of file names.
2560 (while (< (point) end)
2561 (let ((start (+ beg (read (current-buffer))))
2562 (end (+ beg (read (current-buffer)))))
2563 (if (memq (char-after end) '(?\n ?\ ))
2564 ;; End is followed by \n or by " -> ".
2565 (put-text-property start end 'dired-filename t))))))
2566 ;; Remove trailing lines.
2567 (goto-char (point-at-bol))
2568 (while (looking-at "//")
2570 (delete-region (match-beginning 0) (point)))
2572 ;; Some busyboxes are reluctant to discard colors.
2573 (unless (string-match "color" (tramp-get-connection-property v "ls" ""))
2575 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
2576 (replace-match "")))
2578 ;; Decode the output, it could be multibyte.
2579 (decode-coding-region
2581 (or file-name-coding-system
2582 (and (boundp 'default-file-name-coding-system)
2583 (symbol-value 'default-file-name-coding-system))))
2585 ;; The inserted file could be from somewhere else.
2586 (when (and (not wildcard) (not full-directory-p))
2587 (goto-char (point-max))
2588 (when (file-symlink-p filename)
2589 (goto-char (search-backward "->" beg 'noerror)))
2591 (if (zerop (length (file-name-nondirectory filename)))
2593 (file-name-nondirectory filename))
2595 (replace-match (file-relative-name filename) t))
2597 (goto-char (point-max))))))
2599 ;; Canonicalization of file names.
2601 (defun tramp-sh-handle-expand-file-name (name &optional dir)
2602 "Like `expand-file-name' for Tramp files.
2603 If the localname part of the given filename starts with \"/../\" then
2604 the result will be a local, non-Tramp, filename."
2605 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
2606 (setq dir (or dir default-directory "/"))
2607 ;; Unless NAME is absolute, concat DIR and NAME.
2608 (unless (file-name-absolute-p name)
2609 (setq name (concat (file-name-as-directory dir) name)))
2610 ;; If NAME is not a Tramp file, run the real handler.
2611 (if (not (tramp-connectable-p name))
2612 (tramp-run-real-handler 'expand-file-name (list name nil))
2614 (with-parsed-tramp-file-name name nil
2615 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
2616 (setq localname (concat "~/" localname)))
2617 ;; Tilde expansion if necessary. This needs a shell which
2618 ;; groks tilde expansion! The function `tramp-find-shell' is
2619 ;; supposed to find such a shell on the remote host. Please
2620 ;; tell me about it when this doesn't work on your system.
2621 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
2622 (let ((uname (match-string 1 localname))
2623 (fname (match-string 2 localname)))
2624 ;; We cannot simply apply "~/", because under sudo "~/" is
2625 ;; expanded to the local user home directory but to the
2626 ;; root home directory. On the other hand, using always
2627 ;; the default user name for tilde expansion is not
2628 ;; appropriate either, because ssh and companions might
2629 ;; use a user name from the config file.
2630 (when (and (string-equal uname "~")
2631 (string-match "\\`su\\(do\\)?\\'" method))
2632 (setq uname (concat uname user)))
2634 (with-tramp-connection-property v uname
2636 v (format "cd %s; pwd" (tramp-shell-quote-argument uname)))
2637 (with-current-buffer (tramp-get-buffer v)
2638 (goto-char (point-min))
2639 (buffer-substring (point) (point-at-eol)))))
2640 (setq localname (concat uname fname))))
2641 ;; There might be a double slash, for example when "~/"
2642 ;; expands to "/". Remove this.
2643 (while (string-match "//" localname)
2644 (setq localname (replace-match "/" t t localname)))
2645 ;; No tilde characters in file name, do normal
2646 ;; `expand-file-name' (this does "/./" and "/../"). We bind
2647 ;; `directory-sep-char' here for XEmacs on Windows, which would
2648 ;; otherwise use backslash. `default-directory' is bound,
2649 ;; because on Windows there would be problems with UNC shares or
2651 (let ((directory-sep-char ?/)
2652 (default-directory (tramp-compat-temporary-file-directory)))
2653 (tramp-make-tramp-file-name
2655 (tramp-drop-volume-letter
2656 (tramp-run-real-handler
2657 'expand-file-name (list localname)))
2660 ;;; Remote commands:
2662 (defun tramp-sh-handle-executable-find (command)
2663 "Like `executable-find' for Tramp files."
2664 (with-parsed-tramp-file-name default-directory nil
2665 (tramp-find-executable v command (tramp-get-remote-path v) t)))
2667 (defun tramp-process-sentinel (proc event)
2668 "Flush file caches."
2669 (unless (memq (process-status proc) '(run open))
2670 (let ((vec (tramp-get-connection-property proc "vector" nil)))
2672 (tramp-message vec 5 "Sentinel called: `%s' `%s'" proc event)
2673 (tramp-flush-connection-property proc)
2674 (tramp-flush-directory-property vec "")))))
2676 ;; We use BUFFER also as connection buffer during setup. Because of
2677 ;; this, its original contents must be saved, and restored once
2678 ;; connection has been setup.
2679 (defun tramp-sh-handle-start-file-process (name buffer program &rest args)
2680 "Like `start-file-process' for Tramp files."
2681 (with-parsed-tramp-file-name default-directory nil
2682 ;; When PROGRAM is nil, we just provide a tty.
2684 (when (stringp program)
2685 (format "cd %s; exec env PS1=%s %s"
2686 (tramp-shell-quote-argument localname)
2687 ;; Use a human-friendly prompt, for example for `shell'.
2688 (tramp-shell-quote-argument
2690 (file-remote-p default-directory)
2691 tramp-initial-end-of-output))
2692 (mapconcat 'tramp-shell-quote-argument
2693 (cons program args) " "))))
2694 (tramp-process-connection-type
2695 (or (null program) tramp-process-connection-type))
2696 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
2699 ;; We do not want to raise an error when
2700 ;; `start-file-process' has been started several time in
2701 ;; `eshell' and friends.
2702 (tramp-current-connection nil))
2705 ;; BUFFER can be nil. We use a temporary buffer.
2706 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
2707 (while (get-process name1)
2708 ;; NAME must be unique as process name.
2710 name1 (format "%s<%d>" name i)))
2712 ;; Set the new process properties.
2713 (tramp-set-connection-property v "process-name" name)
2714 (tramp-set-connection-property v "process-buffer" buffer)
2716 (with-current-buffer (tramp-get-connection-buffer v)
2718 ;; We catch this event. Otherwise, `start-process' could
2719 ;; be called on the local host.
2722 ;; Activate narrowing in order to save BUFFER
2723 ;; contents. Clear also the modification time;
2724 ;; otherwise we might be interrupted by
2725 ;; `verify-visited-file-modtime'.
2726 (let ((buffer-undo-list t)
2727 (buffer-read-only nil)
2729 (clear-visited-file-modtime)
2730 (narrow-to-region (point-max) (point-max))
2731 ;; We call `tramp-maybe-open-connection', in order
2732 ;; to cleanup the prompt afterwards.
2734 (tramp-maybe-open-connection v)
2736 (delete-region mark (point))
2737 (narrow-to-region (point-max) (point-max))
2740 ;; Send the command.
2741 (tramp-send-command v command nil t) ; nooutput
2742 ;; Check, whether a pty is associated.
2743 (unless (tramp-compat-process-get
2744 (tramp-get-connection-process v) 'remote-tty)
2747 "pty association is not supported for `%s'" name))))
2748 (let ((p (tramp-get-connection-process v)))
2749 ;; Set query flag and process marker for this
2750 ;; process. We ignore errors, because the process
2751 ;; could have finished already.
2753 (tramp-compat-set-process-query-on-exit-flag p t)
2754 (set-marker (process-mark p) (point)))
2759 (if (string-match tramp-temp-buffer-name (buffer-name))
2761 (set-process-buffer (tramp-get-connection-process v) nil)
2762 (kill-buffer (current-buffer)))
2763 (set-buffer-modified-p bmp))
2764 (tramp-set-connection-property v "process-name" nil)
2765 (tramp-set-connection-property v "process-buffer" nil))))))
2767 (defun tramp-sh-handle-process-file
2768 (program &optional infile destination display &rest args)
2769 "Like `process-file' for Tramp files."
2770 ;; The implementation is not complete yet.
2771 (when (and (numberp destination) (zerop destination))
2772 (error "Implementation does not handle immediate return"))
2774 (with-parsed-tramp-file-name default-directory nil
2775 (let (command input tmpinput stderr tmpstderr outbuf ret)
2777 (setq command (mapconcat 'tramp-shell-quote-argument
2778 (cons program args) " "))
2781 (setq input "/dev/null")
2782 (setq infile (expand-file-name infile))
2783 (if (tramp-equal-remote default-directory infile)
2784 ;; INFILE is on the same remote host.
2785 (setq input (with-parsed-tramp-file-name infile nil localname))
2786 ;; INFILE must be copied to remote host.
2787 (setq input (tramp-make-tramp-temp-file v)
2788 tmpinput (tramp-make-tramp-file-name method user host input))
2789 (copy-file infile tmpinput t)))
2790 (when input (setq command (format "%s <%s" command input)))
2792 ;; Determine output.
2795 ((bufferp destination)
2796 (setq outbuf destination))
2798 ((stringp destination)
2799 (setq outbuf (get-buffer-create destination)))
2800 ;; (REAL-DESTINATION ERROR-DESTINATION)
2801 ((consp destination)
2804 ((bufferp (car destination))
2805 (setq outbuf (car destination)))
2806 ((stringp (car destination))
2807 (setq outbuf (get-buffer-create (car destination))))
2809 (setq outbuf (current-buffer))))
2812 ((stringp (cadr destination))
2813 (setcar (cdr destination) (expand-file-name (cadr destination)))
2814 (if (tramp-equal-remote default-directory (cadr destination))
2815 ;; stderr is on the same remote host.
2816 (setq stderr (with-parsed-tramp-file-name
2817 (cadr destination) nil localname))
2818 ;; stderr must be copied to remote host. The temporary
2819 ;; file must be deleted after execution.
2820 (setq stderr (tramp-make-tramp-temp-file v)
2821 tmpstderr (tramp-make-tramp-file-name
2822 method user host stderr))))
2823 ;; stderr to be discarded.
2824 ((null (cadr destination))
2825 (setq stderr "/dev/null"))))
2828 (setq outbuf (current-buffer))))
2829 (when stderr (setq command (format "%s 2>%s" command stderr)))
2831 ;; Send the command. It might not return in time, so we protect
2832 ;; it. Call it in a subshell, in order to preserve working
2837 (if (tramp-send-command-and-check
2838 v (format "\\cd %s; %s"
2839 (tramp-shell-quote-argument localname)
2843 ;; We should show the output anyway.
2845 (with-current-buffer outbuf
2847 (with-current-buffer (tramp-get-connection-buffer v)
2849 (when display (display-buffer outbuf))))
2850 ;; When the user did interrupt, we should do it also. We use
2851 ;; return code -1 as marker.
2853 (kill-buffer (tramp-get-connection-buffer v))
2857 (kill-buffer (tramp-get-connection-buffer v))
2860 ;; Provide error file.
2861 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
2863 ;; Cleanup. We remove all file cache values for the connection,
2864 ;; because the remote process could have changed them.
2865 (when tmpinput (delete-file tmpinput))
2867 ;; `process-file-side-effects' has been introduced with GNU
2868 ;; Emacs 23.2. If set to `nil', no remote file will be changed
2869 ;; by `program'. If it doesn't exist, we assume its default
2871 (unless (and (boundp 'process-file-side-effects)
2872 (not (symbol-value 'process-file-side-effects)))
2873 (tramp-flush-directory-property v ""))
2875 ;; Return exit status.
2880 (defun tramp-sh-handle-file-local-copy (filename)
2881 "Like `file-local-copy' for Tramp files."
2882 (with-parsed-tramp-file-name filename nil
2883 (unless (file-exists-p filename)
2886 "Cannot make local copy of non-existing file `%s'" filename))
2888 (let* ((size (nth 7 (file-attributes (file-truename filename))))
2889 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
2890 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
2891 (tmpfile (tramp-compat-make-temp-file filename)))
2895 ;; `copy-file' handles direct copy and out-of-band methods.
2896 ((or (tramp-local-host-p v)
2897 (tramp-method-out-of-band-p v size))
2898 (copy-file filename tmpfile t t))
2900 ;; Use inline encoding for file transfer.
2903 (with-tramp-progress-reporter
2905 (format "Encoding remote file `%s' with `%s'" filename rem-enc)
2906 (tramp-barf-unless-okay
2907 v (format rem-enc (tramp-shell-quote-argument localname))
2908 "Encoding remote file failed"))
2910 (with-tramp-progress-reporter
2911 v 3 (format "Decoding local file `%s' with `%s'"
2913 (if (functionp loc-dec)
2914 ;; If local decoding is a function, we call it.
2915 ;; We must disable multibyte, because
2916 ;; `uudecode-decode-region' doesn't handle it
2919 (set-buffer-multibyte nil)
2920 (insert-buffer-substring (tramp-get-buffer v))
2921 (funcall loc-dec (point-min) (point-max))
2922 ;; Unset `file-name-handler-alist'. Otherwise,
2923 ;; epa-file gets confused.
2924 (let (file-name-handler-alist
2925 (coding-system-for-write 'binary))
2926 (write-region (point-min) (point-max) tmpfile)))
2928 ;; If tramp-decoding-function is not defined for this
2929 ;; method, we invoke tramp-decoding-command instead.
2930 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
2931 ;; Unset `file-name-handler-alist'. Otherwise,
2932 ;; epa-file gets confused.
2933 (let (file-name-handler-alist
2934 (coding-system-for-write 'binary))
2935 (with-current-buffer (tramp-get-buffer v)
2936 (write-region (point-min) (point-max) tmpfile2)))
2938 (tramp-call-local-coding-command
2939 loc-dec tmpfile2 tmpfile)
2940 (delete-file tmpfile2)))))
2942 ;; Set proper permissions.
2943 (set-file-modes tmpfile (tramp-default-file-modes filename))
2944 ;; Set local user ownership.
2945 (tramp-set-file-uid-gid tmpfile)))
2947 ;; Oops, I don't know what to do.
2949 v 'file-error "Wrong method specification for `%s'" method)))
2953 (delete-file tmpfile)
2954 (signal (car err) (cdr err))))
2956 (run-hooks 'tramp-handle-file-local-copy-hook)
2959 ;; This is needed for XEmacs only. Code stolen from files.el.
2960 (defun tramp-sh-handle-insert-file-contents-literally
2961 (filename &optional visit beg end replace)
2962 "Like `insert-file-contents-literally' for Tramp files."
2963 (let ((format-alist nil)
2964 (after-insert-file-functions nil)
2965 (coding-system-for-read 'no-conversion)
2966 (coding-system-for-write 'no-conversion)
2967 (find-buffer-file-type-function
2968 (if (fboundp 'find-buffer-file-type)
2969 (symbol-function 'find-buffer-file-type)
2971 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler))
2972 (inhibit-file-name-operation 'insert-file-contents))
2975 (fset 'find-buffer-file-type (lambda (filename) t))
2976 (insert-file-contents filename visit beg end replace))
2978 (if find-buffer-file-type-function
2979 (fset 'find-buffer-file-type find-buffer-file-type-function)
2980 (fmakunbound 'find-buffer-file-type)))))
2982 (defun tramp-sh-handle-make-auto-save-file-name ()
2983 "Like `make-auto-save-file-name' for Tramp files.
2984 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
2985 (let ((tramp-auto-save-directory tramp-auto-save-directory)
2987 (tramp-subst-strs-in-string
2994 (buffer-file-name))))
2995 ;; File name must be unique. This is ensured with Emacs 22 (see
2996 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
2997 ;; all other cases we must do it ourselves.
2998 (when (boundp 'auto-save-file-name-transforms)
3001 (when (and (string-match (car x) buffer-file-name)
3002 (not (car (cddr x))))
3003 (setq tramp-auto-save-directory
3004 (or tramp-auto-save-directory
3005 (tramp-compat-temporary-file-directory)))))
3006 (symbol-value 'auto-save-file-name-transforms)))
3007 ;; Create directory.
3008 (when tramp-auto-save-directory
3009 (setq buffer-file-name
3010 (expand-file-name buffer-file-name tramp-auto-save-directory))
3011 (unless (file-exists-p tramp-auto-save-directory)
3012 (make-directory tramp-auto-save-directory t)))
3013 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3014 ;; it is not a magic file name operation (since Emacs 22).
3015 ;; We must deactivate it temporarily.
3016 (if (not (ad-is-active 'make-auto-save-file-name))
3017 (tramp-run-real-handler 'make-auto-save-file-name nil)
3019 (ad-deactivate 'make-auto-save-file-name)
3021 (tramp-run-real-handler 'make-auto-save-file-name nil)
3022 (ad-activate 'make-auto-save-file-name)))))
3024 ;; CCC grok LOCKNAME
3025 (defun tramp-sh-handle-write-region
3026 (start end filename &optional append visit lockname confirm)
3027 "Like `write-region' for Tramp files."
3028 (setq filename (expand-file-name filename))
3029 (with-parsed-tramp-file-name filename nil
3030 ;; Following part commented out because we don't know what to do about
3031 ;; file locking, and it does not appear to be a problem to ignore it.
3032 ;; Ange-ftp ignores it, too.
3033 ;; (when (and lockname (stringp lockname))
3034 ;; (setq lockname (expand-file-name lockname)))
3035 ;; (unless (or (eq lockname nil)
3036 ;; (string= lockname filename))
3038 ;; "tramp-sh-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3040 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
3041 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
3042 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
3043 (tramp-error v 'file-error "File not overwritten")))
3045 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
3046 (tramp-get-remote-uid v 'integer)))
3047 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
3048 (tramp-get-remote-gid v 'integer))))
3050 (if (and (tramp-local-host-p v)
3051 ;; `file-writable-p' calls `file-expand-file-name'. We
3052 ;; cannot use `tramp-run-real-handler' therefore.
3053 (let (file-name-handler-alist)
3055 (file-writable-p (file-name-directory localname))
3056 (or (file-directory-p localname)
3057 (file-writable-p localname)))))
3058 ;; Short track: if we are on the local host, we can run directly.
3059 (tramp-run-real-handler
3061 (list start end localname append 'no-message lockname confirm))
3063 (let* ((modes (save-excursion (tramp-default-file-modes filename)))
3064 ;; We use this to save the value of
3065 ;; `last-coding-system-used' after writing the tmp
3066 ;; file. At the end of the function, we set
3067 ;; `last-coding-system-used' to this saved value. This
3068 ;; way, any intermediary coding systems used while
3069 ;; talking to the remote shell or suchlike won't hose
3070 ;; this variable. This approach was snarfed from
3073 ;; Write region into a tmp file. This isn't really
3074 ;; needed if we use an encoding function, but currently
3075 ;; we use it always because this makes the logic
3076 ;; simpler. We must also set `temporary-file-directory',
3077 ;; because it could point to a remote directory.
3078 (temporary-file-directory
3079 (tramp-compat-temporary-file-directory))
3080 (tmpfile (or tramp-temp-buffer-file-name
3081 (tramp-compat-make-temp-file filename))))
3083 ;; If `append' is non-nil, we copy the file locally, and let
3084 ;; the native `write-region' implementation do the job.
3085 (when append (copy-file filename tmpfile 'ok))
3087 ;; We say `no-message' here because we don't want the
3088 ;; visited file modtime data to be clobbered from the temp
3089 ;; file. We call `set-visited-file-modtime' ourselves later
3090 ;; on. We must ensure that `file-coding-system-alist'
3091 ;; matches `tmpfile'.
3092 (let (file-name-handler-alist
3093 (file-coding-system-alist
3094 (tramp-find-file-name-coding-system-alist filename tmpfile)))
3096 (tramp-run-real-handler
3098 (list start end tmpfile append 'no-message lockname confirm))
3100 (setq tramp-temp-buffer-file-name nil)
3101 (delete-file tmpfile)
3102 (signal (car err) (cdr err))))
3104 ;; Now, `last-coding-system-used' has the right value. Remember it.
3105 (when (boundp 'last-coding-system-used)
3106 (setq coding-system-used
3107 (symbol-value 'last-coding-system-used))))
3109 ;; The permissions of the temporary file should be set. If
3110 ;; filename does not exist (eq modes nil) it has been
3111 ;; renamed to the backup file. This case `save-buffer'
3112 ;; handles permissions.
3113 ;; Ensure that it is still readable.
3117 (logior (or modes 0) (tramp-compat-octal-to-decimal "0400"))))
3119 ;; This is a bit lengthy due to the different methods
3120 ;; possible for file transfer. First, we check whether the
3121 ;; method uses an rcp program. If so, we call it.
3122 ;; Otherwise, both encoding and decoding command must be
3123 ;; specified. However, if the method _also_ specifies an
3124 ;; encoding function, then that is used for encoding the
3125 ;; contents of the tmp file.
3126 (let* ((size (nth 7 (file-attributes tmpfile)))
3127 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
3128 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
3130 ;; `copy-file' handles direct copy and out-of-band methods.
3131 ((or (tramp-local-host-p v)
3132 (tramp-method-out-of-band-p v size))
3133 (if (and (not (stringp start))
3134 (= (or end (point-max)) (point-max))
3135 (= (or start (point-min)) (point-min))
3136 (tramp-get-method-parameter
3137 method 'tramp-copy-keep-tmpfile))
3139 (setq tramp-temp-buffer-file-name tmpfile)
3141 ;; We keep the local file for performance
3142 ;; reasons, useful for "rsync".
3143 (copy-file tmpfile filename t)
3145 (setq tramp-temp-buffer-file-name nil)
3146 (delete-file tmpfile)
3147 (signal (car err) (cdr err)))))
3148 (setq tramp-temp-buffer-file-name nil)
3149 ;; Don't rename, in order to keep context in SELinux.
3151 (copy-file tmpfile filename t)
3152 (delete-file tmpfile))))
3154 ;; Use inline file transfer.
3159 (set-buffer-multibyte nil)
3160 ;; Use encoding function or command.
3161 (with-tramp-progress-reporter
3162 v 3 (format "Encoding local file `%s' using `%s'"
3164 (if (functionp loc-enc)
3165 ;; The following `let' is a workaround for
3166 ;; the base64.el that comes with pgnus-0.84.
3167 ;; If both of the following conditions are
3168 ;; satisfied, it tries to write to a local
3169 ;; file in default-directory, but at this
3170 ;; point, default-directory is remote.
3171 ;; (`call-process-region' can't write to
3172 ;; remote files, it seems.) The file in
3173 ;; question is a tmp file anyway.
3174 (let ((coding-system-for-read 'binary)
3176 (tramp-compat-temporary-file-directory)))
3177 (insert-file-contents-literally tmpfile)
3178 (funcall loc-enc (point-min) (point-max)))
3180 (unless (zerop (tramp-call-local-coding-command
3184 (concat "Cannot write to `%s', "
3185 "local encoding command `%s' failed")
3186 filename loc-enc))))
3188 ;; Send buffer into remote decoding command which
3189 ;; writes to remote file. Because this happens on
3190 ;; the remote host, we cannot use the function.
3191 (with-tramp-progress-reporter
3192 v 3 (format "Decoding remote file `%s' using `%s'"
3194 (goto-char (point-max))
3195 (unless (bolp) (newline))
3199 (concat rem-dec " <<'EOF'\n%sEOF")
3200 (tramp-shell-quote-argument localname)
3202 (tramp-barf-unless-okay
3204 "Couldn't write region to `%s', decode using `%s' failed"
3206 ;; When `file-precious-flag' is set, the region is
3207 ;; written to a temporary file. Check that the
3208 ;; checksum is equal to that from the local tmpfile.
3209 (when file-precious-flag
3212 ;; cksum runs locally, if possible.
3213 (zerop (tramp-call-process "cksum" tmpfile t))
3214 ;; cksum runs remotely.
3215 (tramp-send-command-and-check
3218 "cksum <%s" (tramp-shell-quote-argument localname)))
3219 ;; ... they are different.
3223 (with-current-buffer (tramp-get-buffer v)
3227 (concat "Couldn't write region to `%s',"
3228 " decode using `%s' failed")
3229 filename rem-dec)))))
3232 (delete-file tmpfile)))
3234 ;; That's not expected.
3238 (concat "Method `%s' should specify both encoding and "
3239 "decoding command or an rcp program")
3242 ;; Make `last-coding-system-used' have the right value.
3243 (when coding-system-used
3244 (set 'last-coding-system-used coding-system-used))))
3246 (tramp-flush-file-property v (file-name-directory localname))
3247 (tramp-flush-file-property v localname)
3249 ;; We must protect `last-coding-system-used', now we have set it
3250 ;; to its correct value.
3251 (let (last-coding-system-used (need-chown t))
3252 ;; Set file modification time.
3253 (when (or (eq visit t) (stringp visit))
3254 (let ((file-attr (tramp-compat-file-attributes filename 'integer)))
3255 (set-visited-file-modtime
3256 ;; We must pass modtime explicitly, because filename can
3257 ;; be different from (buffer-file-name), f.e. if
3258 ;; `file-precious-flag' is set.
3260 (when (and (= (nth 2 file-attr) uid)
3261 (= (nth 3 file-attr) gid))
3262 (setq need-chown nil))))
3264 ;; Set the ownership.
3266 (tramp-set-file-uid-gid filename uid gid))
3267 (when (or (eq visit t) (null visit) (stringp visit))
3268 (tramp-message v 0 "Wrote %s" filename))
3269 (run-hooks 'tramp-handle-write-region-hook)))))
3271 (defvar tramp-vc-registered-file-names nil
3272 "List used to collect file names, which are checked during `vc-registered'.")
3274 ;; VC backends check for the existence of various different special
3275 ;; files. This is very time consuming, because every single check
3276 ;; requires a remote command (the file cache must be invalidated).
3277 ;; Therefore, we apply a kind of optimization. We install the file
3278 ;; name handler `tramp-vc-file-name-handler', which does nothing but
3279 ;; remembers all file names for which `file-exists-p' or
3280 ;; `file-readable-p' has been applied. A first run of `vc-registered'
3281 ;; is performed. Afterwards, a script is applied for all collected
3282 ;; file names, using just one remote command. The result of this
3283 ;; script is used to fill the file cache with actual values. Now we
3284 ;; can reset the file name handlers, and we make a second run of
3285 ;; `vc-registered', which returns the expected result without sending
3286 ;; any other remote command.
3287 (defun tramp-sh-handle-vc-registered (file)
3288 "Like `vc-registered' for Tramp files."
3289 (tramp-compat-with-temp-message ""
3290 (with-parsed-tramp-file-name file nil
3291 (with-tramp-progress-reporter
3292 v 3 (format "Checking `vc-registered' for %s" file)
3294 ;; There could be new files, created by the vc backend. We
3295 ;; cannot reuse the old cache entries, therefore.
3296 (let (tramp-vc-registered-file-names
3297 (remote-file-name-inhibit-cache (current-time))
3298 (file-name-handler-alist
3299 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
3301 ;; Here we collect only file names, which need an operation.
3302 (ignore-errors (tramp-run-real-handler 'vc-registered (list file)))
3303 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
3305 ;; Send just one command, in order to fill the cache.
3306 (when tramp-vc-registered-file-names
3307 (tramp-maybe-send-script
3309 (format tramp-vc-registered-read-file-names
3310 (tramp-get-file-exists-command v)
3311 (format "%s -r" (tramp-get-test-command v)))
3312 "tramp_vc_registered_read_file_names")
3316 (tramp-send-command-and-read
3319 "tramp_vc_registered_read_file_names <<'EOF'\n%s\nEOF\n"
3320 (mapconcat 'tramp-shell-quote-argument
3321 tramp-vc-registered-file-names
3324 (tramp-set-file-property
3325 v (car elt) (cadr elt) (cadr (cdr elt))))))
3327 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3328 ;; calls shall be answered from the file cache. We unset
3329 ;; `process-file-side-effects' in order to keep the cache when
3330 ;; `process-file' calls appear.
3331 (let (process-file-side-effects)
3332 (tramp-run-real-handler 'vc-registered (list file)))))))
3334 ;;;###tramp-autoload
3335 (defun tramp-sh-file-name-handler (operation &rest args)
3336 "Invoke remote-shell Tramp file name handler.
3337 Fall back to normal file name handler if no Tramp handler exists."
3338 (when (and tramp-locked (not tramp-locker))
3339 (setq tramp-locked nil)
3340 (signal 'file-error (list "Forbidden reentrant call of Tramp")))
3341 (let ((tl tramp-locked))
3344 (setq tramp-locked t)
3345 (let ((tramp-locker t))
3347 (let ((fn (assoc operation tramp-sh-file-name-handler-alist)))
3349 (apply (cdr fn) args)
3350 (tramp-run-real-handler operation args))))))
3351 (setq tramp-locked tl))))
3353 (defun tramp-vc-file-name-handler (operation &rest args)
3354 "Invoke special file name handler, which collects files to be handled."
3357 (tramp-replace-environment-variables
3358 (apply 'tramp-file-name-for-operation operation args)))
3359 (fn (assoc operation tramp-sh-file-name-handler-alist)))
3360 (with-parsed-tramp-file-name filename nil
3362 ;; That's what we want: file names, for which checks are
3363 ;; applied. We assume that VC uses only `file-exists-p' and
3364 ;; `file-readable-p' checks; otherwise we must extend the
3365 ;; list. We do not perform any action, but return nil, in
3366 ;; order to keep `vc-registered' running.
3367 ((and fn (memq operation '(file-exists-p file-readable-p)))
3368 (add-to-list 'tramp-vc-registered-file-names localname 'append)
3370 ;; `process-file' and `start-file-process' shall be ignored.
3371 ((and fn (eq operation 'process-file) 0))
3372 ((and fn (eq operation 'start-file-process) nil))
3373 ;; Tramp file name handlers like `expand-file-name'. They
3375 (fn (save-match-data (apply (cdr fn) args)))
3376 ;; Default file name handlers, we don't care.
3377 (t (tramp-run-real-handler operation args)))))))
3379 ;;; Internal Functions:
3381 (defun tramp-maybe-send-script (vec script name)
3382 "Define in remote shell function NAME implemented as SCRIPT.
3383 Only send the definition if it has not already been done."
3384 ;; We cannot let-bind (tramp-get-connection-process vec) because it
3386 (let ((scripts (tramp-get-connection-property
3387 (tramp-get-connection-process vec) "scripts" nil)))
3388 (unless (member name scripts)
3389 (with-tramp-progress-reporter vec 5 (format "Sending script `%s'" name)
3390 ;; The script could contain a call of Perl. This is masked with `%s'.
3391 (when (and (string-match "%s" script)
3392 (not (tramp-get-remote-perl vec)))
3393 (tramp-error vec 'file-error "No Perl available on remote host"))
3394 (tramp-barf-unless-okay
3396 (format "%s () {\n%s\n}" name
3397 (format script (tramp-get-remote-perl vec)))
3398 "Script %s sending failed" name)
3399 (tramp-set-connection-property
3400 (tramp-get-connection-process vec) "scripts" (cons name scripts))))))
3402 (defun tramp-set-auto-save ()
3403 (when (and ;; ange-ftp has its own auto-save mechanism
3404 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
3405 'tramp-sh-file-name-handler)
3407 (auto-save-mode 1)))
3408 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
3409 (add-hook 'tramp-unload-hook
3411 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
3413 (defun tramp-run-test (switch filename)
3414 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3415 Returns the exit code of the `test' program."
3416 (with-parsed-tramp-file-name filename nil
3417 (tramp-send-command-and-check
3421 (tramp-get-test-command v)
3423 (tramp-shell-quote-argument localname)))))
3425 (defun tramp-run-test2 (format-string file1 file2)
3426 "Run `test'-like program on the remote system, given FILE1, FILE2.
3427 FORMAT-STRING contains the program name, switches, and place holders.
3428 Returns the exit code of the `test' program. Barfs if the methods,
3429 hosts, or files, disagree."
3430 (unless (tramp-equal-remote file1 file2)
3431 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
3434 "tramp-run-test2 only implemented for same method, user, host")))
3435 (with-parsed-tramp-file-name file1 v1
3436 (with-parsed-tramp-file-name file1 v2
3437 (tramp-send-command-and-check
3439 (format format-string
3440 (tramp-shell-quote-argument v1-localname)
3441 (tramp-shell-quote-argument v2-localname))))))
3443 (defun tramp-find-executable
3444 (vec progname dirlist &optional ignore-tilde ignore-path)
3445 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3446 First arg VEC specifies the connection, PROGNAME is the program
3447 to search for, and DIRLIST gives the list of directories to
3448 search. If IGNORE-TILDE is non-nil, directory names starting
3449 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3452 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3454 This function expects to be in the right *tramp* buffer."
3455 (with-current-buffer (tramp-get-connection-buffer vec)
3457 ;; Check whether the executable is in $PATH. "which(1)" does not
3458 ;; report always a correct error code; therefore we check the
3459 ;; number of words it returns.
3461 (tramp-send-command vec (format "which \\%s | wc -w" progname))
3462 (goto-char (point-min))
3463 (if (looking-at "^\\s-*1$")
3464 (setq result (concat "\\" progname))))
3467 ;; Remove all ~/foo directories from dirlist. In XEmacs,
3468 ;; `remove' is in CL, and we want to avoid CL dependencies.
3471 (setq d (car dirlist))
3472 (setq dirlist (cdr dirlist))
3473 (unless (char-equal ?~ (aref d 0))
3474 (setq newdl (cons d newdl))))
3475 (setq dirlist (nreverse newdl))))
3478 (format (concat "while read d; "
3479 "do if test -x $d/%s -a -f $d/%s; "
3480 "then echo tramp_executable $d/%s; "
3481 "break; fi; done <<'EOF'\n"
3483 progname progname progname (mapconcat 'identity dirlist "\n")))
3484 (goto-char (point-max))
3485 (when (search-backward "tramp_executable " nil t)
3486 (skip-chars-forward "^ ")
3487 (skip-chars-forward " ")
3488 (setq result (buffer-substring (point) (point-at-eol)))))
3491 (defun tramp-set-remote-path (vec)
3492 "Sets the remote environment PATH to existing directories.
3493 I.e., for each directory in `tramp-remote-path', it is tested
3494 whether it exists and if so, it is added to the environment
3496 (tramp-message vec 5 (format "Setting $PATH environment variable"))
3498 vec (format "PATH=%s; export PATH"
3499 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
3501 ;; ------------------------------------------------------------
3502 ;; -- Communication with external shell --
3503 ;; ------------------------------------------------------------
3505 (defun tramp-find-file-exists-command (vec)
3506 "Find a command on the remote host for checking if a file exists.
3507 Here, we are looking for a command which has zero exit status if the
3508 file exists and nonzero exit status otherwise."
3509 (let ((existing "/")
3511 (tramp-shell-quote-argument "/ this file does not exist "))
3513 ;; The algorithm is as follows: we try a list of several commands.
3514 ;; For each command, we first run `$cmd /' -- this should return
3515 ;; true, as the root directory always exists. And then we run
3516 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
3517 ;; does not exist. This should return false. We use the first
3518 ;; command we find that seems to work.
3519 ;; The list of commands to try is as follows:
3520 ;; `ls -d' This works on most systems, but NetBSD 1.4
3521 ;; has a bug: `ls' always returns zero exit
3522 ;; status, even for files which don't exist.
3523 ;; `test -e' Some Bourne shells have a `test' builtin
3524 ;; which does not know the `-e' option.
3525 ;; `/bin/test -e' For those, the `test' binary on disk normally
3526 ;; provides the option. Alas, the binary
3527 ;; is sometimes `/bin/test' and sometimes it's
3529 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3532 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
3533 (tramp-send-command-and-check
3534 vec (format "%s %s" result existing))
3535 (not (tramp-send-command-and-check
3536 vec (format "%s %s" result nonexistent)))))
3538 (and (setq result "/bin/test -e")
3539 (tramp-send-command-and-check
3540 vec (format "%s %s" result existing))
3541 (not (tramp-send-command-and-check
3542 vec (format "%s %s" result nonexistent)))))
3544 (and (setq result "/usr/bin/test -e")
3545 (tramp-send-command-and-check
3546 vec (format "%s %s" result existing))
3547 (not (tramp-send-command-and-check
3548 vec (format "%s %s" result nonexistent)))))
3550 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
3551 (tramp-send-command-and-check
3552 vec (format "%s %s" result existing))
3553 (not (tramp-send-command-and-check
3554 vec (format "%s %s" result nonexistent))))))
3556 vec 'file-error "Couldn't find command to check if file exists"))
3559 (defun tramp-open-shell (vec shell)
3560 "Opens shell SHELL."
3561 (with-tramp-progress-reporter
3562 vec 5 (format "Opening remote shell `%s'" shell)
3563 ;; Find arguments for this shell.
3564 (let ((tramp-end-of-output tramp-initial-end-of-output)
3565 (alist tramp-sh-extra-args)
3567 (while (and alist (null extra-args))
3568 (setq item (pop alist))
3569 (when (string-match (car item) shell)
3570 (setq extra-args (cdr item))))
3573 "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s"
3574 (tramp-shell-quote-argument tramp-end-of-output)
3575 shell (or extra-args ""))
3577 (tramp-set-connection-property
3578 (tramp-get-connection-process vec) "remote-shell" shell)
3581 vec (format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output)) t)
3582 (tramp-send-command vec "PS2=''" t)
3583 (tramp-send-command vec "PS3=''" t)
3584 (tramp-send-command vec "PROMPT_COMMAND=''" t)))
3586 (defun tramp-find-shell (vec)
3587 "Opens a shell on the remote host which groks tilde expansion."
3588 (with-current-buffer (tramp-get-buffer vec)
3589 (let ((default-shell
3591 (tramp-get-connection-property
3592 (tramp-get-connection-process vec) "remote-shell" nil)
3593 (tramp-get-method-parameter
3594 (tramp-file-name-method vec) 'tramp-remote-shell)))
3597 (with-tramp-connection-property vec "remote-shell"
3598 ;; CCC: "root" does not exist always, see QNAP 459.
3599 ;; Which check could we apply instead?
3600 (tramp-send-command vec "echo ~root" t)
3601 (if (or (string-match "^~root$" (buffer-string))
3602 ;; The default shell (ksh93) of OpenSolaris and
3603 ;; Solaris is buggy. We've got reports for
3604 ;; "SunOS 5.10" and "SunOS 5.11" so far.
3605 (string-match (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3606 (tramp-get-connection-property
3609 (or (tramp-find-executable
3610 vec "bash" (tramp-get-remote-path vec) t t)
3611 (tramp-find-executable
3612 vec "ksh" (tramp-get-remote-path vec) t t)
3613 ;; Maybe it works at least for some other commands.
3619 "Couldn't find a remote shell which groks tilde "
3620 "expansion, using `%s'")
3625 ;; Open a new shell if needed.
3626 (unless (string-equal shell default-shell)
3628 vec 5 "Starting remote shell `%s' for tilde expansion" shell)
3629 (tramp-open-shell vec shell)))))
3631 ;; Utility functions.
3633 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
3634 "Wait for shell prompt and barf if none appears.
3635 Looks at process PROC to see if a shell prompt appears in TIMEOUT
3636 seconds. If not, it produces an error message with the given ERROR-ARGS."
3638 (tramp-wait-for-regexp
3641 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
3642 (apply 'tramp-error-with-buffer nil proc 'file-error error-args)))
3644 (defun tramp-open-connection-setup-interactive-shell (proc vec)
3645 "Set up an interactive shell.
3646 Mainly sets the prompt and the echo correctly. PROC is the shell
3647 process to set up. VEC specifies the connection."
3648 (let ((tramp-end-of-output tramp-initial-end-of-output))
3649 ;; It is useful to set the prompt in the following command because
3650 ;; some people have a setting for $PS1 which /bin/sh doesn't know
3651 ;; about and thus /bin/sh will display a strange prompt. For
3652 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
3653 ;; display the current working directory but /bin/sh will display
3654 ;; a dollar sign. The following command line sets $PS1 to a sane
3655 ;; value, and works under Bourne-ish shells as well as csh-like
3656 ;; shells. Daniel Pittman reports that the unusual positioning of
3657 ;; the single quotes makes it work under `rc', too. We also unset
3658 ;; the variable $ENV because that is read by some sh
3659 ;; implementations (eg, bash when called as sh) on startup; this
3660 ;; way, we avoid the startup file clobbering $PS1. $PROMPT_COMMAND
3661 ;; is another way to set the prompt in /bin/bash, it must be
3662 ;; discarded as well.
3665 (or (tramp-get-connection-property vec "remote-shell" nil)
3666 (tramp-get-method-parameter
3667 (tramp-file-name-method vec) 'tramp-remote-shell)))
3670 (tramp-message vec 5 "Setting up remote shell environment")
3671 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
3672 ;; Check whether the echo has really been disabled. Some
3673 ;; implementations, like busybox of embedded GNU/Linux, don't
3674 ;; support disabling.
3675 (tramp-send-command vec "echo foo" t)
3676 (with-current-buffer (process-buffer proc)
3677 (goto-char (point-min))
3678 (when (looking-at "echo foo")
3679 (tramp-set-connection-property proc "remote-echo" t)
3680 (tramp-message vec 5 "Remote echo still on. Ok.")
3681 ;; Make sure backspaces and their echo are enabled and no line
3682 ;; width magic interferes with them.
3683 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
3685 (tramp-message vec 5 "Setting shell prompt")
3687 vec (format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output)) t)
3688 (tramp-send-command vec "PS2=''" t)
3689 (tramp-send-command vec "PS3=''" t)
3690 (tramp-send-command vec "PROMPT_COMMAND=''" t)
3692 ;; Try to set up the coding system correctly.
3693 ;; CCC this can't be the right way to do it. Hm.
3694 (tramp-message vec 5 "Determining coding system")
3695 (tramp-send-command vec "echo foo ; echo bar" t)
3696 (with-current-buffer (process-buffer proc)
3697 (goto-char (point-min))
3698 (if (featurep 'mule)
3699 ;; Use MULE to select the right EOL convention for communicating
3700 ;; with the process.
3701 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc)
3702 (cons 'undecided 'undecided)))
3703 cs-decode cs-encode)
3704 (when (symbolp cs) (setq cs (cons cs cs)))
3705 (setq cs-decode (car cs))
3706 (setq cs-encode (cdr cs))
3707 (unless cs-decode (setq cs-decode 'undecided))
3708 (unless cs-encode (setq cs-encode 'undecided))
3709 (setq cs-encode (tramp-compat-coding-system-change-eol-conversion
3711 (when (search-forward "\r" nil t)
3712 (setq cs-decode (tramp-compat-coding-system-change-eol-conversion
3714 (tramp-compat-funcall
3715 'set-buffer-process-coding-system cs-decode cs-encode)
3717 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
3718 ;; Look for ^M and do something useful if found.
3719 (when (search-forward "\r" nil t)
3720 ;; We have found a ^M but cannot frob the process coding system
3721 ;; because we're running on a non-MULE Emacs. Let's try
3723 (tramp-send-command vec "stty -onlcr" t))))
3725 (tramp-send-command vec "set +o vi +o emacs" t)
3727 ;; Check whether the output of "uname -sr" has been changed. If
3728 ;; yes, this is a strong indication that we must expire all
3729 ;; connection properties. We start again with
3730 ;; `tramp-maybe-open-connection', it will be caught there.
3731 (tramp-message vec 5 "Checking system information")
3732 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
3734 (tramp-set-connection-property
3736 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
3737 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
3741 "Connection reset, because remote host changed from `%s' to `%s'"
3742 old-uname new-uname)
3743 (throw 'uname-changed (tramp-maybe-open-connection vec))))
3745 ;; Check whether the remote host suffers from buggy
3746 ;; `send-process-string'. This is known for FreeBSD (see comment in
3747 ;; `send_process', file process.c). I've tested sending 624 bytes
3748 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
3749 ;; this host type is detected locally. It cannot handle remote
3751 (with-tramp-connection-property proc "chunksize"
3753 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
3757 vec 5 "Checking remote host type for `send-process-string' bug")
3759 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
3762 ;; Set remote PATH variable.
3763 (tramp-set-remote-path vec)
3765 ;; Search for a good shell before searching for a command which
3766 ;; checks if a file exists. This is done because Tramp wants to use
3767 ;; "test foo; echo $?" to check if various conditions hold, and
3768 ;; there are buggy /bin/sh implementations which don't execute the
3769 ;; "echo $?" part if the "test" part has an error. In particular,
3770 ;; the OpenSolaris /bin/sh is a problem. There are also other
3771 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
3772 ;; in function declarations, or changing HISTFILE in place.
3773 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
3775 (tramp-find-shell vec)
3777 ;; Disable unexpected output.
3778 (tramp-send-command vec "mesg n; biff n" t)
3780 ;; IRIX64 bash expands "!" even when in single quotes. This
3781 ;; destroys our shell functions, we must disable it. See
3782 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
3783 (when (string-match "^IRIX64" (tramp-get-connection-property vec "uname" ""))
3784 (tramp-send-command vec "set +H" t))
3786 ;; On BSD-like systems, ?\t is expanded to spaces. Suppress this.
3787 (when (string-match "BSD\\|Darwin"
3788 (tramp-get-connection-property vec "uname" ""))
3789 (tramp-send-command vec "stty -oxtabs" t))
3791 ;; Set `remote-tty' process property.
3792 (let ((tty (tramp-send-command-and-read vec "echo \\\"`tty`\\\"" 'noerror)))
3793 (unless (zerop (length tty))
3794 (tramp-compat-process-put proc 'remote-tty tty)))
3796 ;; Dump stty settings in the traces.
3797 (when (>= tramp-verbose 9)
3798 (tramp-send-command vec "stty -a" t))
3800 ;; Set the environment.
3801 (tramp-message vec 5 "Setting default environment")
3803 (let ((env (copy-sequence tramp-remote-process-environment))
3806 (setq item (tramp-compat-split-string (car env) "="))
3807 (setcdr item (mapconcat 'identity (cdr item) "="))
3808 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
3810 vec (format "%s=%s; export %s" (car item) (cdr item) (car item)) t)
3811 (push (car item) unset))
3812 (setq env (cdr env)))
3815 vec (format "unset %s" (mapconcat 'identity unset " ")) t))))
3817 ;; Old text from documentation of tramp-methods:
3818 ;; Using a uuencode/uudecode inline method is discouraged, please use one
3819 ;; of the base64 methods instead since base64 encoding is much more
3820 ;; reliable and the commands are more standardized between the different
3821 ;; Unix versions. But if you can't use base64 for some reason, please
3822 ;; note that the default uudecode command does not work well for some
3823 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
3824 ;; the following command for uudecode:
3826 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
3828 ;; For Irix, no solution is known yet.
3830 (autoload 'uudecode-decode-region "uudecode")
3832 (defconst tramp-local-coding-commands
3833 `((b64 base64-encode-region base64-decode-region)
3834 (uu tramp-uuencode-region uudecode-decode-region)
3835 (pack ,(format tramp-perl-pack "perl") ,(format tramp-perl-unpack "perl")))
3836 "List of local coding commands for inline transfer.
3837 Each item is a list that looks like this:
3839 \(FORMAT ENCODING DECODING\)
3841 FORMAT is symbol describing the encoding/decoding format. It can be
3842 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3844 ENCODING and DECODING can be strings, giving commands, or symbols,
3845 giving functions. If they are strings, then they can contain
3846 the \"%s\" format specifier. If that specifier is present, the input
3847 filename will be put into the command line at that spot. If the
3848 specifier is not present, the input should be read from standard
3851 If they are functions, they will be called with two arguments, start
3852 and end of region, and are expected to replace the region contents
3853 with the encoded or decoded results, respectively.")
3855 (defconst tramp-remote-coding-commands
3856 '((b64 "base64" "base64 -d -i")
3857 ;; "-i" is more robust with older base64 from GNU coreutils.
3858 ;; However, I don't know whether all base64 versions do supports
3860 (b64 "base64" "base64 -d")
3861 (b64 "mimencode -b" "mimencode -u -b")
3862 (b64 "mmencode -b" "mmencode -u -b")
3863 (b64 "recode data..base64" "recode base64..data")
3864 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
3865 (b64 tramp-perl-encode tramp-perl-decode)
3866 (uu "uuencode xxx" "uudecode -o /dev/stdout" "test -c /dev/stdout")
3867 (uu "uuencode xxx" "uudecode -o -")
3868 (uu "uuencode xxx" "uudecode -p")
3869 (uu "uuencode xxx" tramp-uudecode)
3870 (pack tramp-perl-pack tramp-perl-unpack))
3871 "List of remote coding commands for inline transfer.
3872 Each item is a list that looks like this:
3874 \(FORMAT ENCODING DECODING [TEST]\)
3876 FORMAT is symbol describing the encoding/decoding format. It can be
3877 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3879 ENCODING and DECODING can be strings, giving commands, or symbols,
3880 giving variables. If they are strings, then they can contain
3881 the \"%s\" format specifier. If that specifier is present, the input
3882 filename will be put into the command line at that spot. If the
3883 specifier is not present, the input should be read from standard
3886 If they are variables, this variable is a string containing a Perl
3887 implementation for this functionality. This Perl program will be transferred
3888 to the remote host, and it is available as shell function with the same name.
3890 The optional TEST command can be used for further tests, whether
3891 ENCODING and DECODING are applicable.")
3893 (defun tramp-find-inline-encoding (vec)
3894 "Find an inline transfer encoding that works.
3895 Goes through the list `tramp-local-coding-commands' and
3896 `tramp-remote-coding-commands'."
3898 (let ((local-commands tramp-local-coding-commands)
3900 (p (tramp-get-connection-process vec))
3901 loc-enc loc-dec rem-enc rem-dec rem-test litem ritem found)
3902 (while (and local-commands (not found))
3903 (setq litem (pop local-commands))
3904 (catch 'wont-work-local
3905 (let ((format (nth 0 litem))
3906 (remote-commands tramp-remote-coding-commands))
3907 (setq loc-enc (nth 1 litem))
3908 (setq loc-dec (nth 2 litem))
3909 ;; If the local encoder or decoder is a string, the
3910 ;; corresponding command has to work locally.
3911 (if (not (stringp loc-enc))
3913 vec 5 "Checking local encoding function `%s'" loc-enc)
3915 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
3916 (unless (zerop (tramp-call-local-coding-command
3918 (throw 'wont-work-local nil)))
3919 (if (not (stringp loc-dec))
3921 vec 5 "Checking local decoding function `%s'" loc-dec)
3923 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
3924 (unless (zerop (tramp-call-local-coding-command
3926 (throw 'wont-work-local nil)))
3927 ;; Search for remote coding commands with the same format
3928 (while (and remote-commands (not found))
3929 (setq ritem (pop remote-commands))
3930 (catch 'wont-work-remote
3931 (when (equal format (nth 0 ritem))
3932 (setq rem-enc (nth 1 ritem))
3933 (setq rem-dec (nth 2 ritem))
3934 (setq rem-test (nth 3 ritem))
3935 ;; Check the remote test command if exists.
3936 (when (stringp rem-test)
3938 vec 5 "Checking remote test command `%s'" rem-test)
3939 (unless (tramp-send-command-and-check vec rem-test t)
3940 (throw 'wont-work-remote nil)))
3941 ;; Check if remote encoding and decoding commands can be
3942 ;; called remotely with null input and output. This makes
3943 ;; sure there are no syntax errors and the command is really
3944 ;; found. Note that we do not redirect stdout to /dev/null,
3945 ;; for two reasons: when checking the decoding command, we
3946 ;; actually check the output it gives. And also, when
3947 ;; redirecting "mimencode" output to /dev/null, then as root
3948 ;; it might change the permissions of /dev/null!
3949 (when (not (stringp rem-enc))
3950 (let ((name (symbol-name rem-enc)))
3951 (while (string-match (regexp-quote "-") name)
3952 (setq name (replace-match "_" nil t name)))
3953 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
3954 (setq rem-enc name)))
3957 "Checking remote encoding command `%s' for sanity" rem-enc)
3958 (unless (tramp-send-command-and-check
3959 vec (format "%s </dev/null" rem-enc) t)
3960 (throw 'wont-work-remote nil))
3962 (when (not (stringp rem-dec))
3963 (let ((name (symbol-name rem-dec)))
3964 (while (string-match (regexp-quote "-") name)
3965 (setq name (replace-match "_" nil t name)))
3966 (tramp-maybe-send-script vec (symbol-value rem-dec) name)
3967 (setq rem-dec name)))
3970 "Checking remote decoding command `%s' for sanity" rem-dec)
3971 (unless (tramp-send-command-and-check
3973 (format "echo %s | %s | %s" magic rem-enc rem-dec)
3975 (throw 'wont-work-remote nil))
3977 (with-current-buffer (tramp-get-buffer vec)
3978 (goto-char (point-min))
3979 (unless (looking-at (regexp-quote magic))
3980 (throw 'wont-work-remote nil)))
3982 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
3983 (setq rem-enc (nth 1 ritem))
3984 (setq rem-dec (nth 2 ritem))
3985 (setq found t)))))))
3987 ;; Did we find something?
3990 vec 'file-error "Couldn't find an inline transfer encoding"))
3992 ;; Set connection properties. Since the commands are risky (due
3993 ;; to output direction), we cache them in the process cache.
3994 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
3995 (tramp-set-connection-property p "local-encoding" loc-enc)
3996 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
3997 (tramp-set-connection-property p "local-decoding" loc-dec)
3998 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
3999 (tramp-set-connection-property p "remote-encoding" rem-enc)
4000 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
4001 (tramp-set-connection-property p "remote-decoding" rem-dec))))
4003 (defun tramp-call-local-coding-command (cmd input output)
4004 "Call the local encoding or decoding command.
4005 If CMD contains \"%s\", provide input file INPUT there in command.
4006 Otherwise, INPUT is passed via standard input.
4007 INPUT can also be nil which means `/dev/null'.
4008 OUTPUT can be a string (which specifies a filename), or t (which
4009 means standard output and thus the current buffer), or nil (which
4012 tramp-encoding-shell
4013 (when (and input (not (string-match "%s" cmd))) input)
4014 (if (eq output t) t nil)
4016 tramp-encoding-command-switch
4018 (if (string-match "%s" cmd) (format cmd input) cmd)
4019 (if (stringp output) (concat " >" output) ""))))
4021 (defconst tramp-inline-compress-commands
4022 '(("gzip" "gzip -d")
4023 ("bzip2" "bzip2 -d")
4025 ("compress" "compress -d"))
4026 "List of compress and decompress commands for inline transfer.
4027 Each item is a list that looks like this:
4029 \(COMPRESS DECOMPRESS\)
4031 COMPRESS or DECOMPRESS are strings with the respective commands.")
4033 (defun tramp-find-inline-compress (vec)
4034 "Find an inline transfer compress command that works.
4035 Goes through the list `tramp-inline-compress-commands'."
4037 (let ((commands tramp-inline-compress-commands)
4039 (p (tramp-get-connection-process vec))
4040 item compress decompress found)
4041 (while (and commands (not found))
4043 (setq item (pop commands)
4044 compress (nth 0 item)
4045 decompress (nth 1 item))
4048 "Checking local compress commands `%s', `%s' for sanity"
4049 compress decompress)
4052 (tramp-call-local-coding-command
4054 ;; Windows shells need the program file name after
4055 ;; the pipe symbol be quoted if they use forward
4056 ;; slashes as directory separators.
4057 (if (memq system-type '(windows-nt))
4058 "echo %s | \"%s\" | \"%s\""
4059 "echo %s | %s | %s")
4060 magic compress decompress) nil nil))
4064 "Checking remote compress commands `%s', `%s' for sanity"
4065 compress decompress)
4066 (unless (tramp-send-command-and-check
4067 vec (format "echo %s | %s | %s" magic compress decompress) t)
4071 ;; Did we find something?
4074 ;; Set connection properties. Since the commands are
4075 ;; risky (due to output direction), we cache them in the
4078 vec 5 "Using inline transfer compress command `%s'" compress)
4079 (tramp-set-connection-property p "inline-compress" compress)
4081 vec 5 "Using inline transfer decompress command `%s'" decompress)
4082 (tramp-set-connection-property p "inline-decompress" decompress))
4084 (tramp-set-connection-property p "inline-compress" nil)
4085 (tramp-set-connection-property p "inline-decompress" nil)
4087 vec 2 "Couldn't find an inline transfer compress command")))))
4089 (defvar tramp-gw-tunnel-method)
4090 (defvar tramp-gw-socks-method)
4092 (defun tramp-compute-multi-hops (vec)
4093 "Expands VEC according to `tramp-default-proxies-alist'.
4094 Gateway hops are already opened."
4095 (let ((target-alist `(,vec))
4096 (hops (or (tramp-file-name-hop vec) ""))
4100 ;; Ad-hoc proxy definitions.
4101 (dolist (proxy (reverse (split-string hops tramp-postfix-hop-regexp 'omit)))
4102 (let ((user (tramp-file-name-user item))
4103 (host (tramp-file-name-host item))
4105 tramp-prefix-format proxy tramp-postfix-host-format)))
4107 vec 5 "Add proxy (\"%s\" \"%s\" \"%s\")"
4108 (and (stringp host) (regexp-quote host))
4109 (and (stringp user) (regexp-quote user))
4113 'tramp-default-proxies-alist
4114 (list (and (stringp host) (regexp-quote host))
4115 (and (stringp user) (regexp-quote user))
4117 (setq item (tramp-dissect-file-name proxy))))
4118 ;; Save the new value.
4119 (when (and hops tramp-save-ad-hoc-proxies)
4120 (customize-save-variable
4121 'tramp-default-proxies-alist tramp-default-proxies-alist))
4123 ;; Look for proxy hosts to be passed.
4124 (setq choices tramp-default-proxies-alist)
4126 (setq item (pop choices)
4127 proxy (eval (nth 2 item)))
4130 (string-match (or (eval (nth 0 item)) "")
4131 (or (tramp-file-name-host (car target-alist)) ""))
4133 (string-match (or (eval (nth 1 item)) "")
4134 (or (tramp-file-name-user (car target-alist)) "")))
4136 ;; No more hops needed.
4138 ;; Replace placeholders.
4143 ?u (or (tramp-file-name-user (car target-alist)) "")
4144 ?h (or (tramp-file-name-host (car target-alist)) ""))))
4145 (with-parsed-tramp-file-name proxy l
4147 (add-to-list 'target-alist l)
4148 ;; Start next search.
4149 (setq choices tramp-default-proxies-alist)))))
4152 (when (and (boundp 'tramp-gw-tunnel-method) (boundp 'tramp-gw-socks-method)
4155 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method)
4156 (tramp-file-name-method (car target-alist))))
4157 (let ((gw (pop target-alist))
4158 (hop (pop target-alist)))
4159 ;; Is the method prepared for gateways?
4160 (unless (tramp-file-name-port hop)
4163 "Connection `%s' is not supported for gateway access." hop))
4164 ;; Open the gateway connection.
4168 (tramp-file-name-method hop) (tramp-file-name-user hop)
4169 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop) nil nil))
4170 ;; For the password prompt, we need the correct values.
4171 ;; Therefore, we must remember the gateway vector. But we
4172 ;; cannot do it as connection property, because it shouldn't
4173 ;; be persistent. And we have no started process yet either.
4174 (tramp-set-file-property (car target-alist) "" "gateway" hop)))
4176 ;; Foreign and out-of-band methods are not supported for multi-hops.
4177 (when (cdr target-alist)
4178 (setq choices target-alist)
4180 (setq item (pop choices))
4184 (tramp-get-method-parameter
4185 (tramp-file-name-method item) 'tramp-login-program))
4186 (tramp-get-method-parameter
4187 (tramp-file-name-method item) 'tramp-copy-program))
4190 "Method `%s' is not supported for multi-hops."
4191 (tramp-file-name-method item)))))
4193 ;; In case the host name is not used for the remote shell
4194 ;; command, the user could be misguided by applying a random
4196 (let* ((v (car target-alist))
4197 (method (tramp-file-name-method v))
4198 (host (tramp-file-name-host v)))
4201 ;; There are multi-hops.
4203 ;; The host name is used for the remote shell command.
4205 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
4206 ;; The host is local. We cannot use `tramp-local-host-p'
4207 ;; here, because it opens a connection as well.
4208 (string-match tramp-local-host-regexp host))
4211 "Host `%s' looks like a remote host, `%s' can only use the local host"
4217 (defvar tramp-current-connection nil
4218 "Last connection timestamp.")
4220 (defun tramp-maybe-open-connection (vec)
4221 "Maybe open a connection VEC.
4222 Does not do anything if a connection is already open, but re-opens the
4223 connection if a previous connection has died for some reason."
4224 (catch 'uname-changed
4225 (let ((p (tramp-get-connection-process vec))
4226 (process-name (tramp-get-connection-property vec "process-name" nil))
4227 (process-environment (copy-sequence process-environment))
4228 (pos (with-current-buffer (tramp-get-connection-buffer vec) (point))))
4230 ;; If Tramp opens the same connection within a short time frame,
4231 ;; there is a problem. We shall signal this.
4232 (unless (or (and p (processp p) (memq (process-status p) '(run open)))
4233 (not (equal (butlast (append vec nil))
4234 (car tramp-current-connection)))
4236 (current-time) (cdr tramp-current-connection))
4237 (or tramp-connection-min-time-diff 0)))
4238 (throw 'suppress 'suppress))
4240 ;; If too much time has passed since last command was sent, look
4241 ;; whether process is still alive. If it isn't, kill it. When
4242 ;; using ssh, it can sometimes happen that the remote end has
4243 ;; hung up but the local ssh client doesn't recognize this until
4244 ;; it tries to send some data to the remote end. So that's why
4245 ;; we try to send a command from time to time, then look again
4246 ;; whether the process is really alive.
4248 (when (and (> (tramp-time-diff
4250 (tramp-get-connection-property
4251 p "last-cmd-time" '(0 0 0)))
4253 p (processp p) (memq (process-status p) '(run open)))
4254 (tramp-send-command vec "echo are you awake" t t)
4255 (unless (and (memq (process-status p) '(run open))
4256 (tramp-wait-for-output p 10))
4257 ;; The error will be caught locally.
4258 (tramp-error vec 'file-error "Awake did fail")))
4263 ;; New connection must be opened.
4265 (unless (and p (processp p) (memq (process-status p) '(run open)))
4267 ;; We call `tramp-get-buffer' in order to get a debug
4268 ;; buffer for messages from the beginning.
4269 (tramp-get-buffer vec)
4271 ;; If `non-essential' is non-nil, don't reopen a new connection.
4272 (when (and (boundp 'non-essential) (symbol-value 'non-essential))
4273 (throw 'non-essential 'non-essential))
4275 (with-tramp-progress-reporter
4277 (if (zerop (length (tramp-file-name-user vec)))
4278 (format "Opening connection for %s using %s"
4279 (tramp-file-name-host vec)
4280 (tramp-file-name-method vec))
4281 (format "Opening connection for %s@%s using %s"
4282 (tramp-file-name-user vec)
4283 (tramp-file-name-host vec)
4284 (tramp-file-name-method vec)))
4286 ;; Start new process.
4287 (when (and p (processp p))
4289 (setenv "TERM" tramp-terminal-type)
4290 (setenv "LC_ALL" "C")
4291 (setenv "PROMPT_COMMAND")
4292 (setenv "PS1" tramp-initial-end-of-output)
4293 (let* ((target-alist (tramp-compute-multi-hops vec))
4294 ;; We will apply `tramp-ssh-controlmaster-options'
4295 ;; only for the first hop.
4296 (options tramp-ssh-controlmaster-options)
4297 (process-connection-type tramp-process-connection-type)
4298 (process-adaptive-read-buffering nil)
4299 (coding-system-for-read nil)
4300 ;; This must be done in order to avoid our file
4302 (p (let ((default-directory
4303 (tramp-compat-temporary-file-directory)))
4306 (tramp-get-connection-name vec)
4307 (tramp-get-connection-buffer vec)
4308 (if tramp-encoding-command-interactive
4309 (list tramp-encoding-shell
4310 tramp-encoding-command-interactive)
4311 (list tramp-encoding-shell))))))
4313 ;; Set sentinel and query flag.
4314 (tramp-set-connection-property p "vector" vec)
4315 (set-process-sentinel p 'tramp-process-sentinel)
4316 (tramp-compat-set-process-query-on-exit-flag p nil)
4317 (setq tramp-current-connection
4318 (cons (butlast (append vec nil)) (current-time))
4319 tramp-current-host (system-name))
4322 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
4324 ;; Check whether process is alive.
4325 (tramp-barf-if-no-shell-prompt
4327 "Couldn't find local shell prompt %s" tramp-encoding-shell)
4329 ;; Now do all the connections as specified.
4331 (let* ((hop (car target-alist))
4332 (l-method (tramp-file-name-method hop))
4333 (l-user (tramp-file-name-user hop))
4334 (l-host (tramp-file-name-host hop))
4337 (tramp-get-method-parameter
4338 l-method 'tramp-login-program))
4340 (tramp-get-method-parameter
4341 l-method 'tramp-login-args))
4343 (tramp-get-method-parameter
4344 l-method 'tramp-async-args))
4346 (tramp-get-method-parameter l-method 'tramp-gw-args))
4347 (gw (tramp-get-file-property hop "" "gateway" nil))
4348 (g-method (and gw (tramp-file-name-method gw)))
4349 (g-user (and gw (tramp-file-name-user gw)))
4350 (g-host (and gw (tramp-file-name-real-host gw)))
4351 (command login-program)
4352 ;; We don't create the temporary file. In
4353 ;; fact, it is just a prefix for the
4354 ;; ControlPath option of ssh; the real
4355 ;; temporary file has another name, and it is
4356 ;; created and protected by ssh. It is also
4357 ;; removed by ssh when the connection is
4358 ;; closed. The temporary file name is cached
4359 ;; in the main connection process, therefore
4360 ;; we cannot use `tramp-get-connection-process'.
4362 (with-tramp-connection-property
4363 (get-process (tramp-buffer-name vec)) "temp-file"
4366 tramp-temp-name-prefix
4367 (tramp-compat-temporary-file-directory)))))
4370 ;; Add arguments for asynchronous processes.
4371 (when (and process-name async-args)
4372 (setq login-args (append async-args login-args)))
4374 ;; Add gateway arguments if necessary.
4375 (when (and gw gw-args)
4376 (setq login-args (append gw-args login-args)))
4378 ;; Check for port number. Until now, there's no
4379 ;; need for handling like method, user, host.
4380 (when (string-match tramp-host-with-port-regexp l-host)
4381 (setq l-port (match-string 2 l-host)
4382 l-host (match-string 1 l-host)))
4384 ;; Check, whether there is a restricted shell.
4385 (dolist (elt tramp-restricted-shell-hosts-alist)
4386 (when (string-match elt tramp-current-host)
4389 ;; Set variables for computing the prompt for
4390 ;; reading password. They can also be derived
4392 (setq tramp-current-method (or g-method l-method)
4393 tramp-current-user (or g-user l-user)
4394 tramp-current-host (or g-host l-host))
4396 ;; Replace login-args place holders.
4398 l-host (or l-host "")
4399 l-user (or l-user "")
4400 l-port (or l-port "")
4401 spec (format-spec-make ?t tmpfile)
4402 options (format-spec options spec)
4403 spec (format-spec-make
4404 ?h l-host ?u l-user ?p l-port ?c options)
4407 ;; We do not want to see the trailing local
4408 ;; prompt in `start-file-process'.
4409 (unless r-shell "exec ")
4413 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
4414 (unless (member "" x) (mapconcat 'identity x " ")))
4416 ;; Local shell could be a Windows COMSPEC. It
4417 ;; doesn't know the ";" syntax, but we must exit
4418 ;; always for `start-file-process'. It could
4419 ;; also be a restricted shell, which does not
4421 (when r-shell " && exit || exit")))
4423 ;; Send the command.
4424 (tramp-message vec 3 "Sending command `%s'" command)
4425 (tramp-send-command vec command t t)
4426 (tramp-process-actions
4427 p vec pos tramp-actions-before-shell 60)
4429 vec 3 "Found remote shell prompt on `%s'" l-host))
4432 target-alist (cdr target-alist)))
4434 ;; Make initial shell settings.
4435 (tramp-open-connection-setup-interactive-shell p vec))))
4437 ;; When the user did interrupt, we must cleanup.
4440 ;; Propagate the quit signal.
4441 (signal (car err) (cdr err)))))))
4443 (defun tramp-send-command (vec command &optional neveropen nooutput)
4444 "Send the COMMAND to connection VEC.
4445 Erases temporary buffer before sending the command. If optional
4446 arg NEVEROPEN is non-nil, never try to open the connection. This
4447 is meant to be used from `tramp-maybe-open-connection' only. The
4448 function waits for output unless NOOUTPUT is set."
4449 (unless neveropen (tramp-maybe-open-connection vec))
4450 (let ((p (tramp-get-connection-process vec)))
4451 (when (tramp-get-connection-property p "remote-echo" nil)
4452 ;; We mark the command string that it can be erased in the output buffer.
4453 (tramp-set-connection-property p "check-remote-echo" t)
4454 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
4455 ;; Some busyboxes tend to close the connection when we use the
4456 ;; following syntax for here-documents. This we cannot test; it
4457 ;; shall be set via `tramp-connection-properties'.
4458 (when (and (string-match "<<'EOF'" command)
4459 (not (tramp-get-connection-property vec "busybox" nil)))
4460 ;; Unset $PS1 when using here documents, in order to avoid
4461 ;; multiple prompts.
4462 (setq command (concat "(PS1= ; " command "\n)")))
4463 ;; Send the command.
4464 (tramp-message vec 6 "%s" command)
4465 (tramp-send-string vec command)
4466 (unless nooutput (tramp-wait-for-output p))))
4468 (defun tramp-wait-for-output (proc &optional timeout)
4469 "Wait for output from remote command."
4470 (unless (buffer-live-p (process-buffer proc))
4471 (delete-process proc)
4472 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
4473 (with-current-buffer (process-buffer proc)
4474 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
4475 ;; be leading escape sequences, which must be ignored.
4476 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
4477 ;; Sometimes, the commands do not return a newline but a
4478 ;; null byte before the shell prompt, for example "git
4479 ;; ls-files -c -z ...".
4480 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
4481 (found (tramp-wait-for-regexp proc timeout regexp1)))
4483 (let (buffer-read-only)
4484 ;; A simple-minded busybox has sent " ^H" sequences.
4486 (goto-char (point-min))
4487 (when (re-search-forward "^\\(.\b\\)+$" (point-at-eol) t)
4489 (delete-region (point-min) (point)))
4490 ;; Delete the prompt.
4491 (goto-char (point-max))
4492 (re-search-backward regexp nil t)
4493 (delete-region (point) (point-max)))
4497 "[[Remote prompt `%s' not found in %d secs]]"
4498 tramp-end-of-output timeout)
4501 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
4502 ;; Return value is whether end-of-output sentinel was found.
4505 (defun tramp-send-command-and-check
4506 (vec command &optional subshell dont-suppress-err)
4507 "Run COMMAND and check its exit status.
4508 Sends `echo $?' along with the COMMAND for checking the exit status. If
4509 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
4511 If the optional argument SUBSHELL is non-nil, the command is
4512 executed in a subshell, ie surrounded by parentheses. If
4513 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
4516 (concat (if subshell "( " "")
4518 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
4519 "echo tramp_exit_status $?"
4520 (if subshell " )" "")))
4521 (with-current-buffer (tramp-get-connection-buffer vec)
4522 (goto-char (point-max))
4523 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
4525 vec 'file-error "Couldn't find exit status of `%s'" command))
4526 (skip-chars-forward "^ ")
4528 (zerop (read (current-buffer)))
4529 (let (buffer-read-only)
4530 (delete-region (match-beginning 0) (point-max))))))
4532 (defun tramp-barf-unless-okay (vec command fmt &rest args)
4533 "Run COMMAND, check exit status, throw error if exit status not okay.
4534 Similar to `tramp-send-command-and-check' but accepts two more arguments
4535 FMT and ARGS which are passed to `error'."
4536 (or (tramp-send-command-and-check vec command)
4537 (apply 'tramp-error vec 'file-error fmt args)))
4539 (defun tramp-send-command-and-read (vec command &optional noerror)
4540 "Run COMMAND and return the output, which must be a Lisp expression.
4541 In case there is no valid Lisp expression and NOERROR is nil, it
4544 (tramp-send-command-and-check vec command)
4545 (tramp-barf-unless-okay
4546 vec command "`%s' returns with error" command))
4547 (with-current-buffer (tramp-get-connection-buffer vec)
4548 ;; Read the expression.
4549 (goto-char (point-min))
4551 (prog1 (read (current-buffer))
4553 (when (re-search-forward "\\S-" (point-at-eol) t)
4555 (error (unless noerror
4558 "`%s' does not return a valid Lisp expression: `%s'"
4559 command (buffer-string))))))))
4561 (defun tramp-convert-file-attributes (vec attr)
4562 "Convert `file-attributes' ATTR generated by perl script, stat or ls.
4563 Convert file mode bits to string and set virtual device number.
4566 ;; Remove color escape sequences from symlink.
4567 (when (stringp (car attr))
4568 (while (string-match tramp-color-escape-sequence-regexp (car attr))
4569 (setcar attr (replace-match "" nil nil (car attr)))))
4570 ;; Convert uid and gid. Use -1 as indication of unusable value.
4571 (when (and (numberp (nth 2 attr)) (< (nth 2 attr) 0))
4572 (setcar (nthcdr 2 attr) -1))
4573 (when (and (floatp (nth 2 attr))
4574 (<= (nth 2 attr) (tramp-compat-most-positive-fixnum)))
4575 (setcar (nthcdr 2 attr) (round (nth 2 attr))))
4576 (when (and (numberp (nth 3 attr)) (< (nth 3 attr) 0))
4577 (setcar (nthcdr 3 attr) -1))
4578 (when (and (floatp (nth 3 attr))
4579 (<= (nth 3 attr) (tramp-compat-most-positive-fixnum)))
4580 (setcar (nthcdr 3 attr) (round (nth 3 attr))))
4581 ;; Convert last access time.
4582 (unless (listp (nth 4 attr))
4583 (setcar (nthcdr 4 attr)
4584 (list (floor (nth 4 attr) 65536)
4585 (floor (mod (nth 4 attr) 65536)))))
4586 ;; Convert last modification time.
4587 (unless (listp (nth 5 attr))
4588 (setcar (nthcdr 5 attr)
4589 (list (floor (nth 5 attr) 65536)
4590 (floor (mod (nth 5 attr) 65536)))))
4591 ;; Convert last status change time.
4592 (unless (listp (nth 6 attr))
4593 (setcar (nthcdr 6 attr)
4594 (list (floor (nth 6 attr) 65536)
4595 (floor (mod (nth 6 attr) 65536)))))
4596 ;; Convert file size.
4597 (when (< (nth 7 attr) 0)
4598 (setcar (nthcdr 7 attr) -1))
4599 (when (and (floatp (nth 7 attr))
4600 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
4601 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
4602 ;; Convert file mode bits to string.
4603 (unless (stringp (nth 8 attr))
4604 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
4605 (when (stringp (car attr))
4606 (aset (nth 8 attr) 0 ?l)))
4607 ;; Convert directory indication bit.
4608 (when (string-match "^d" (nth 8 attr))
4610 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
4611 (when (consp (car attr))
4612 (if (and (stringp (caar attr))
4613 (string-match ".+ -> .\\(.+\\)." (caar attr)))
4614 (setcar attr (match-string 1 (caar attr)))
4616 ;; Set file's gid change bit.
4617 (setcar (nthcdr 9 attr)
4618 (if (numberp (nth 3 attr))
4619 (not (= (nth 3 attr)
4620 (tramp-get-remote-gid vec 'integer)))
4623 (tramp-get-remote-gid vec 'string)))))
4625 (unless (listp (nth 10 attr))
4626 (setcar (nthcdr 10 attr)
4628 (cons (floor (nth 10 attr) 65536)
4629 (floor (mod (nth 10 attr) 65536)))
4630 ;; Inodes can be incredible huge. We must hide this.
4631 (error (tramp-get-inode vec)))))
4632 ;; Set virtual device number.
4633 (setcar (nthcdr 11 attr)
4634 (tramp-get-device vec))
4637 (defun tramp-shell-case-fold (string)
4638 "Converts STRING to shell glob pattern which ignores case."
4641 (if (equal (downcase c) (upcase c))
4643 (format "[%c%c]" (downcase c) (upcase c))))
4647 (defun tramp-make-copy-program-file-name (vec)
4648 "Create a file name suitable to be passed to `rcp' and workalikes."
4649 (let ((user (tramp-file-name-user vec))
4650 (host (tramp-file-name-real-host vec))
4651 (localname (tramp-shell-quote-argument
4652 (tramp-file-name-localname vec))))
4653 (if (not (zerop (length user)))
4654 (format "%s@%s:%s" user host localname)
4655 (format "%s:%s" host localname))))
4657 (defun tramp-method-out-of-band-p (vec size)
4658 "Return t if this is an out-of-band method, nil otherwise."
4660 ;; It shall be an out-of-band method.
4661 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
4662 ;; There must be a size, otherwise the file doesn't exist.
4664 ;; Either the file size is large enough, or (in rare cases) there
4665 ;; does not exist a remote encoding.
4666 (or (null tramp-copy-size-limit)
4667 (> size tramp-copy-size-limit)
4668 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
4670 ;; Variables local to connection.
4672 (defun tramp-get-remote-path (vec)
4673 (with-tramp-connection-property
4674 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
4675 ;; cache the result for the session only. Otherwise, the result
4676 ;; is cached persistently.
4677 (if (memq 'tramp-own-remote-path tramp-remote-path)
4678 (tramp-get-connection-process vec)
4681 (let* ((remote-path (copy-tree tramp-remote-path))
4682 (elt1 (memq 'tramp-default-remote-path remote-path))
4683 (elt2 (memq 'tramp-own-remote-path remote-path))
4684 (default-remote-path
4687 (tramp-send-command-and-read
4688 vec "echo \\\"`getconf PATH 2>/dev/null`\\\"" 'noerror)
4689 ;; Default if "getconf" is not available.
4693 "`getconf PATH' not successful, using default value \"%s\"."
4699 (tramp-send-command-and-read vec "echo \\\"$PATH\\\"")
4702 vec 3 "$PATH not set, ignoring `tramp-own-remote-path'.")
4705 ;; Replace place holder `tramp-default-remote-path'.
4709 (tramp-compat-split-string default-remote-path ":")
4711 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
4713 ;; Replace place holder `tramp-own-remote-path'.
4717 (tramp-compat-split-string own-remote-path ":")
4719 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
4721 ;; Remove double entries.
4722 (setq elt1 remote-path)
4724 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
4726 (setq elt1 (cdr elt1)))
4728 ;; Remove non-existing directories.
4736 (tramp-make-tramp-file-name
4737 (tramp-file-name-method vec)
4738 (tramp-file-name-user vec)
4739 (tramp-file-name-host vec)
4744 (defun tramp-get-ls-command (vec)
4745 (with-tramp-connection-property vec "ls"
4746 (tramp-message vec 5 "Finding a suitable `ls' command")
4749 (dolist (cmd '("ls" "gnuls" "gls"))
4750 (let ((dl (tramp-get-remote-path vec))
4752 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
4753 ;; Check parameters. On busybox, "ls" output coloring is
4754 ;; enabled by default sometimes. So we try to disable it
4755 ;; when possible. $LS_COLORING is not supported there.
4756 ;; Some "ls" versions are sensible wrt the order of
4757 ;; arguments, they fail when "-al" is after the
4758 ;; "--color=never" argument (for example on FreeBSD).
4759 (when (tramp-send-command-and-check
4760 vec (format "%s -lnd /" result))
4761 (when (tramp-send-command-and-check
4763 "%s --color=never -al /dev/null" result))
4764 (setq result (concat result " --color=never")))
4765 (throw 'ls-found result))
4766 (setq dl (cdr dl))))))
4767 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
4769 (defun tramp-get-ls-command-with-dired (vec)
4771 (with-tramp-connection-property vec "ls-dired"
4772 (tramp-message vec 5 "Checking, whether `ls --dired' works")
4773 ;; Some "ls" versions are sensible wrt the order of arguments,
4774 ;; they fail when "-al" is after the "--dired" argument (for
4775 ;; example on FreeBSD).
4776 (tramp-send-command-and-check
4777 vec (format "%s --dired -al /dev/null" (tramp-get-ls-command vec))))))
4779 (defun tramp-get-test-command (vec)
4780 (with-tramp-connection-property vec "test"
4781 (tramp-message vec 5 "Finding a suitable `test' command")
4782 (if (tramp-send-command-and-check vec "test 0")
4784 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
4786 (defun tramp-get-test-nt-command (vec)
4787 ;; Does `test A -nt B' work? Use abominable `find' construct if it
4788 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
4789 ;; for otherwise the shell crashes.
4790 (with-tramp-connection-property vec "test-nt"
4794 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
4795 (with-current-buffer (tramp-get-buffer vec)
4796 (goto-char (point-min))
4797 (when (looking-at (regexp-quote tramp-end-of-output))
4798 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
4803 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
4804 (tramp-get-test-command vec)))
4805 "tramp_test_nt %s %s"))))
4807 (defun tramp-get-file-exists-command (vec)
4808 (with-tramp-connection-property vec "file-exists"
4809 (tramp-message vec 5 "Finding command to check if file exists")
4810 (tramp-find-file-exists-command vec)))
4812 (defun tramp-get-remote-ln (vec)
4813 (with-tramp-connection-property vec "ln"
4814 (tramp-message vec 5 "Finding a suitable `ln' command")
4815 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
4817 (defun tramp-get-remote-perl (vec)
4818 (with-tramp-connection-property vec "perl"
4819 (tramp-message vec 5 "Finding a suitable `perl' command")
4821 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
4822 (tramp-find-executable
4823 vec "perl" (tramp-get-remote-path vec)))))
4824 ;; We must check also for some Perl modules.
4826 (with-tramp-connection-property vec "perl-file-spec"
4827 (tramp-send-command-and-check
4828 vec (format "%s -e 'use File::Spec;'" result)))
4829 (with-tramp-connection-property vec "perl-cwd-realpath"
4830 (tramp-send-command-and-check
4831 vec (format "%s -e 'use Cwd \"realpath\";'" result))))
4834 (defun tramp-get-remote-stat (vec)
4835 (with-tramp-connection-property vec "stat"
4836 (tramp-message vec 5 "Finding a suitable `stat' command")
4837 (let ((result (tramp-find-executable
4838 vec "stat" (tramp-get-remote-path vec)))
4840 ;; Check whether stat(1) returns usable syntax. "%s" does not
4841 ;; work on older AIX systems.
4844 (tramp-send-command-and-read
4845 vec (format "%s -c '(\"%%N\" %%s)' /" result) 'noerror))
4846 (unless (and (listp tmp) (stringp (car tmp))
4847 (string-match "^./.$" (car tmp))
4848 (integerp (cadr tmp)))
4852 (defun tramp-get-remote-readlink (vec)
4853 (with-tramp-connection-property vec "readlink"
4854 (tramp-message vec 5 "Finding a suitable `readlink' command")
4855 (let ((result (tramp-find-executable
4856 vec "readlink" (tramp-get-remote-path vec))))
4858 (tramp-send-command-and-check
4859 vec (format "%s --canonicalize-missing /" result)))
4862 (defun tramp-get-remote-trash (vec)
4863 (with-tramp-connection-property vec "trash"
4864 (tramp-message vec 5 "Finding a suitable `trash' command")
4865 (tramp-find-executable vec "trash" (tramp-get-remote-path vec))))
4867 (defun tramp-get-remote-id (vec)
4868 (with-tramp-connection-property vec "id"
4869 (tramp-message vec 5 "Finding POSIX `id' command")
4872 (let ((dl (tramp-get-remote-path vec))
4874 (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
4875 ;; Check POSIX parameter.
4876 (when (tramp-send-command-and-check vec (format "%s -u" result))
4877 (throw 'id-found result))
4878 (setq dl (cdr dl)))))
4879 (tramp-error vec 'file-error "Couldn't find a POSIX `id' command"))))
4881 (defun tramp-get-remote-uid (vec id-format)
4882 (with-tramp-connection-property vec (format "uid-%s" id-format)
4883 (let ((res (tramp-send-command-and-read
4885 (format "%s -u%s %s"
4886 (tramp-get-remote-id vec)
4887 (if (equal id-format 'integer) "" "n")
4888 (if (equal id-format 'integer)
4889 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
4890 ;; The command might not always return a number.
4891 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
4893 (defun tramp-get-remote-gid (vec id-format)
4894 (with-tramp-connection-property vec (format "gid-%s" id-format)
4895 (let ((res (tramp-send-command-and-read
4897 (format "%s -g%s %s"
4898 (tramp-get-remote-id vec)
4899 (if (equal id-format 'integer) "" "n")
4900 (if (equal id-format 'integer)
4901 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
4902 ;; The command might not always return a number.
4903 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
4905 ;; Some predefined connection properties.
4906 (defun tramp-get-inline-compress (vec prop size)
4907 "Return the compress command related to PROP.
4908 PROP is either `inline-compress' or `inline-decompress'. SIZE is
4909 the length of the file to be compressed.
4911 If no corresponding command is found, nil is returned."
4912 (when (and (integerp tramp-inline-compress-start-size)
4913 (> size tramp-inline-compress-start-size))
4914 (with-tramp-connection-property (tramp-get-connection-process vec) prop
4915 (tramp-find-inline-compress vec)
4916 (tramp-get-connection-property
4917 (tramp-get-connection-process vec) prop nil))))
4919 (defun tramp-get-inline-coding (vec prop size)
4920 "Return the coding command related to PROP.
4921 PROP is either `remote-encoding', `remote-decoding',
4922 `local-encoding' or `local-decoding'.
4924 SIZE is the length of the file to be coded. Depending on SIZE,
4925 compression might be applied.
4927 If no corresponding command is found, nil is returned.
4928 Otherwise, either a string is returned which contains a `%s' mark
4929 to be used for the respective input or output file; or a Lisp
4930 function cell is returned to be applied on a buffer."
4931 ;; We must catch the errors, because we want to return `nil', when
4932 ;; no inline coding is found.
4935 (with-tramp-connection-property
4936 (tramp-get-connection-process vec) prop
4937 (tramp-find-inline-encoding vec)
4938 (tramp-get-connection-property
4939 (tramp-get-connection-process vec) prop nil)))
4940 (prop1 (if (string-match "encoding" prop)
4941 "inline-compress" "inline-decompress"))
4943 ;; The connection property might have been cached. So we must
4944 ;; send the script to the remote side - maybe.
4945 (when (and coding (symbolp coding) (string-match "remote" prop))
4946 (let ((name (symbol-name coding)))
4947 (while (string-match (regexp-quote "-") name)
4948 (setq name (replace-match "_" nil t name)))
4949 (tramp-maybe-send-script vec (symbol-value coding) name)
4950 (setq coding name)))
4952 ;; Check for the `compress' command.
4953 (setq compress (tramp-get-inline-compress vec prop1 size))
4954 ;; Return the value.
4956 ((and compress (symbolp coding))
4957 (if (string-match "decompress" prop1)
4960 (let ((coding-system-for-write 'binary)
4961 (coding-system-for-read 'binary))
4963 'call-process-region (point-min) (point-max)
4964 (car (split-string ,compress)) t t nil
4965 (cdr (split-string ,compress)))))
4967 (let ((coding-system-for-write 'binary)
4968 (coding-system-for-read 'binary))
4970 'call-process-region beg end
4971 (car (split-string ,compress)) t t nil
4972 (cdr (split-string ,compress))))
4973 (,coding (point-min) (point-max)))))
4976 ((and compress (string-match "decoding" prop))
4978 ;; Windows shells need the program file name after
4979 ;; the pipe symbol be quoted if they use forward
4980 ;; slashes as directory separators.
4982 ((and (string-match "local" prop)
4983 (memq system-type '(windows-nt)))
4985 ((string-match "local" prop) "(%s | %s)")
4986 (t "(%s | %s >%%s)"))
4990 ;; Windows shells need the program file name after
4991 ;; the pipe symbol be quoted if they use forward
4992 ;; slashes as directory separators.
4993 (if (and (string-match "local" prop)
4994 (memq system-type '(windows-nt)))
4995 "(%s <%%s | \"%s\")"
4998 ((string-match "decoding" prop)
5000 ((string-match "local" prop) (format "%s" coding))
5001 (t (format "%s >%%s" coding))))
5003 (format "%s <%%s" coding)))))))
5005 (add-hook 'tramp-unload-hook
5007 (unload-feature 'tramp-sh 'force)))
5013 ;; * Don't use globbing for directories with many files, as this is
5014 ;; likely to produce long command lines, and some shells choke on
5015 ;; long command lines.
5016 ;; * Make it work for different encodings, and for different file name
5017 ;; encodings, too. (Daniel Pittman)
5018 ;; * Don't search for perl5 and perl. Instead, only search for perl and
5019 ;; then look if it's the right version (with `perl -v').
5020 ;; * When editing a remote CVS controlled file as a different user, VC
5021 ;; gets confused about the file locking status. Try to find out why
5022 ;; the workaround doesn't work.
5023 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5024 ;; until the last but one hop via `start-file-process'. Apply it
5025 ;; also for ftp and smb.
5026 ;; * WIBNI if we had a command "trampclient"? If I was editing in
5027 ;; some shell with root privileges, it would be nice if I could
5029 ;; trampclient filename.c
5030 ;; as an editor, and the _current_ shell would connect to an Emacs
5031 ;; server and would be used in an existing non-privileged Emacs
5032 ;; session for doing the editing in question.
5033 ;; That way, I need not tell Emacs my password again and be afraid
5034 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
5035 ;; once display a just typed password in the context of a keyboard
5036 ;; sequence prompt for a question immediately following in a shell
5037 ;; script run within Emacs -- nasty).
5038 ;; And if I have some ssh session running to a different computer,
5039 ;; having the possibility of passing a local file there to a local
5040 ;; Emacs session (in case I can arrange for a connection back) would
5042 ;; Likely the corresponding Tramp server should not allow the
5043 ;; equivalent of the emacsclient -eval option in order to make this
5044 ;; reasonably unproblematic. And maybe trampclient should have some
5045 ;; way of passing credentials, like by using an SSL socket or
5046 ;; something. (David Kastrup)
5047 ;; * Reconnect directly to a compliant shell without first going
5048 ;; through the user's default shell. (Pete Forman)
5049 ;; * How can I interrupt the remote process with a signal
5050 ;; (interrupt-process seems not to work)? (Markus Triska)
5051 ;; * Avoid the local shell entirely for starting remote processes. If
5052 ;; so, I think even a signal, when delivered directly to the local
5053 ;; SSH instance, would correctly be propagated to the remote process
5054 ;; automatically; possibly SSH would have to be started with
5055 ;; "-t". (Markus Triska)
5056 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5057 ;; isn't on the remote host. (Mark A. Hershberger)
5058 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
5059 ;; * Optimize out-of-band copying when both methods are scp-like (not
5061 ;; * Keep a second connection open for out-of-band methods like scp or
5063 ;; * Try telnet+curl as new method. It might be useful for busybox,
5064 ;; without built-in uuencode/uudecode.
5066 ;;; tramp-sh.el ends here