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" "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-vc-registered-read-file-names
773 if %s \"$file\"; then
774 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
776 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
778 if %s \"$file\"; then
779 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
781 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
785 "Script to check existence of VC related files.
786 It must be send formatted with two strings; the tests for file
787 existence, and file readability. Input shall be read via
788 here-document, otherwise the command could exceed maximum length
791 (defconst tramp-file-mode-type-map
792 '((0 . "-") ; Normal file (SVID-v2 and XPG2)
794 (2 . "c") ; character device
795 (3 . "m") ; multiplexed character device (v7)
796 (4 . "d") ; directory
797 (5 . "?") ; Named special file (XENIX)
798 (6 . "b") ; block device
799 (7 . "?") ; multiplexed block device (v7)
800 (8 . "-") ; regular file
801 (9 . "n") ; network special file (HP-UX)
803 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
805 (13 . "D") ; door special (Solaris)
806 (14 . "w")) ; whiteout (BSD)
807 "A list of file types returned from the `stat' system call.
808 This is used to map a mode number to a permission string.")
810 ;; New handlers should be added here. The following operations can be
811 ;; handled using the normal primitives: file-name-sans-versions,
813 (defconst tramp-sh-file-name-handler-alist
814 '((load . tramp-handle-load)
815 (make-symbolic-link . tramp-sh-handle-make-symbolic-link)
816 (file-name-as-directory . tramp-handle-file-name-as-directory)
817 (file-name-directory . tramp-handle-file-name-directory)
818 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
819 (file-truename . tramp-sh-handle-file-truename)
820 (file-exists-p . tramp-sh-handle-file-exists-p)
821 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
822 (file-directory-p . tramp-sh-handle-file-directory-p)
823 (file-executable-p . tramp-sh-handle-file-executable-p)
824 (file-readable-p . tramp-sh-handle-file-readable-p)
825 (file-regular-p . tramp-handle-file-regular-p)
826 (file-symlink-p . tramp-handle-file-symlink-p)
827 (file-writable-p . tramp-sh-handle-file-writable-p)
828 (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p)
829 (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p)
830 (file-attributes . tramp-sh-handle-file-attributes)
831 (file-modes . tramp-handle-file-modes)
832 (directory-files . tramp-handle-directory-files)
833 (directory-files-and-attributes
834 . tramp-sh-handle-directory-files-and-attributes)
835 (file-name-all-completions . tramp-sh-handle-file-name-all-completions)
836 (file-name-completion . tramp-handle-file-name-completion)
837 (add-name-to-file . tramp-sh-handle-add-name-to-file)
838 (copy-file . tramp-sh-handle-copy-file)
839 (copy-directory . tramp-sh-handle-copy-directory)
840 (rename-file . tramp-sh-handle-rename-file)
841 (set-file-modes . tramp-sh-handle-set-file-modes)
842 (set-file-times . tramp-sh-handle-set-file-times)
843 (make-directory . tramp-sh-handle-make-directory)
844 (delete-directory . tramp-sh-handle-delete-directory)
845 (delete-file . tramp-sh-handle-delete-file)
846 (directory-file-name . tramp-handle-directory-file-name)
847 ;; `executable-find' is not official yet.
848 (executable-find . tramp-sh-handle-executable-find)
849 (start-file-process . tramp-sh-handle-start-file-process)
850 (process-file . tramp-sh-handle-process-file)
851 (shell-command . tramp-handle-shell-command)
852 (insert-directory . tramp-sh-handle-insert-directory)
853 (expand-file-name . tramp-sh-handle-expand-file-name)
854 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
855 (file-local-copy . tramp-sh-handle-file-local-copy)
856 (file-remote-p . tramp-handle-file-remote-p)
857 (insert-file-contents . tramp-handle-insert-file-contents)
858 (insert-file-contents-literally
859 . tramp-sh-handle-insert-file-contents-literally)
860 (write-region . tramp-sh-handle-write-region)
861 (find-backup-file-name . tramp-handle-find-backup-file-name)
862 (make-auto-save-file-name . tramp-sh-handle-make-auto-save-file-name)
863 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
864 (dired-compress-file . tramp-sh-handle-dired-compress-file)
865 (dired-recursive-delete-directory
866 . tramp-sh-handle-dired-recursive-delete-directory)
867 (dired-uncache . tramp-handle-dired-uncache)
868 (set-visited-file-modtime . tramp-sh-handle-set-visited-file-modtime)
869 (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime)
870 (file-selinux-context . tramp-sh-handle-file-selinux-context)
871 (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context)
872 (file-acl . tramp-sh-handle-file-acl)
873 (set-file-acl . tramp-sh-handle-set-file-acl)
874 (vc-registered . tramp-sh-handle-vc-registered))
875 "Alist of handler functions.
876 Operations not mentioned here will be handled by the normal Emacs functions.")
878 ;; This must be the last entry, because `identity' always matches.
880 (add-to-list 'tramp-foreign-file-name-handler-alist
881 '(identity . tramp-sh-file-name-handler) 'append)
883 ;;; File Name Handler Functions:
885 (defun tramp-sh-handle-make-symbolic-link
886 (filename linkname &optional ok-if-already-exists)
887 "Like `make-symbolic-link' for Tramp files.
888 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
889 the symlink. If LINKNAME is a Tramp file, only the localname component is
890 used as the target of the symlink.
892 If LINKNAME is a Tramp file and the localname component is relative, then
893 it is expanded first, before the localname component is taken. Note that
894 this can give surprising results if the user/host for the source and
895 target of the symlink differ."
896 (with-parsed-tramp-file-name linkname l
897 (let ((ln (tramp-get-remote-ln l))
898 (cwd (tramp-run-real-handler
899 'file-name-directory (list l-localname))))
903 "Making a symbolic link. ln(1) does not exist on the remote host."))
905 ;; Do the 'confirm if exists' thing.
906 (when (file-exists-p linkname)
908 (if (or (null ok-if-already-exists) ; not allowed to exist
909 (and (numberp ok-if-already-exists)
912 "File %s already exists; make it a link anyway? "
915 l 'file-already-exists "File %s already exists" l-localname)
916 (delete-file linkname)))
918 ;; If FILENAME is a Tramp name, use just the localname component.
919 (when (tramp-tramp-file-p filename)
921 (tramp-file-name-localname
922 (tramp-dissect-file-name (expand-file-name filename)))))
924 (tramp-flush-file-property l (file-name-directory l-localname))
925 (tramp-flush-file-property l l-localname)
927 ;; Right, they are on the same host, regardless of user, method,
928 ;; etc. We now make the link on the remote machine. This will
929 ;; occur as the user that FILENAME belongs to.
930 (tramp-send-command-and-check
933 "cd %s && %s -sf %s %s"
934 (tramp-shell-quote-argument cwd)
936 (tramp-shell-quote-argument filename)
937 (tramp-shell-quote-argument l-localname))
940 (defun tramp-sh-handle-file-truename (filename &optional counter prev-dirs)
941 "Like `file-truename' for Tramp files."
942 (with-parsed-tramp-file-name (expand-file-name filename) nil
943 (tramp-make-tramp-file-name method user host
944 (with-tramp-file-property v localname "file-truename"
945 (let ((result nil)) ; result steps in reverse order
946 (tramp-message v 4 "Finding true name for `%s'" filename)
948 ;; Use GNU readlink --canonicalize-missing where available.
949 ((tramp-get-remote-readlink v)
951 (tramp-send-command-and-read
953 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
954 (tramp-get-remote-readlink v)
955 (tramp-shell-quote-argument localname)))))
957 ;; Use Perl implementation.
958 ((and (tramp-get-remote-perl v)
959 (tramp-get-connection-property v "perl-file-spec" nil)
960 (tramp-get-connection-property v "perl-cwd-realpath" nil))
961 (tramp-maybe-send-script
962 v tramp-perl-file-truename "tramp_perl_file_truename")
964 (tramp-send-command-and-read
966 (format "tramp_perl_file_truename %s"
967 (tramp-shell-quote-argument localname)))))
969 ;; Do it yourself. We bind `directory-sep-char' here for
970 ;; XEmacs on Windows, which would otherwise use backslash.
971 (t (let* ((directory-sep-char ?/)
972 (steps (tramp-compat-split-string localname "/"))
973 (localnamedir (tramp-run-real-handler
974 'file-name-as-directory (list localname)))
975 (is-dir (string= localname localnamedir))
978 ;; Don't make the following value larger than
979 ;; necessary. People expect an error message in
980 ;; a timely fashion when something is wrong;
981 ;; otherwise they might think that Emacs is hung.
982 ;; Of course, correctness has to come first.
985 (while (and steps (< numchase numchase-limit))
986 (setq thisstep (pop steps))
990 (append '("") (reverse result) (list thisstep))
993 (nth 0 (file-attributes
994 (tramp-make-tramp-file-name
1001 (cond ((string= "." thisstep)
1002 (tramp-message v 5 "Ignoring step `.'"))
1003 ((string= ".." thisstep)
1004 (tramp-message v 5 "Processing step `..'")
1006 ((stringp symlink-target)
1007 ;; It's a symlink, follow it.
1009 v 5 "Follow symlink to %s" symlink-target)
1010 (setq numchase (1+ numchase))
1011 (when (file-name-absolute-p symlink-target)
1013 ;; If the symlink was absolute, we'll get a
1014 ;; string like "/user@host:/some/target";
1015 ;; extract the "/some/target" part from it.
1016 (when (tramp-tramp-file-p symlink-target)
1017 (unless (tramp-equal-remote filename symlink-target)
1020 "Symlink target `%s' on wrong host"
1022 (setq symlink-target localname))
1024 (append (tramp-compat-split-string
1029 (setq result (cons thisstep result)))))
1030 (when (>= numchase numchase-limit)
1033 "Maximum number (%d) of symlinks exceeded" numchase-limit))
1034 (setq result (reverse result))
1035 ;; Combine list to form string.
1038 (mapconcat 'identity (cons "" result) "/")
1041 (or (string= "" result)
1042 (not (string= (substring result -1) "/"))))
1043 (setq result (concat result "/"))))))
1045 (tramp-message v 4 "True name of `%s' is `%s'" localname result)
1050 (defun tramp-sh-handle-file-exists-p (filename)
1051 "Like `file-exists-p' for Tramp files."
1052 (with-parsed-tramp-file-name filename nil
1053 (with-tramp-file-property v localname "file-exists-p"
1054 (or (not (null (tramp-get-file-property
1055 v localname "file-attributes-integer" nil)))
1056 (not (null (tramp-get-file-property
1057 v localname "file-attributes-string" nil)))
1058 (tramp-send-command-and-check
1062 (tramp-get-file-exists-command v)
1063 (tramp-shell-quote-argument localname)))))))
1065 (defun tramp-sh-handle-file-attributes (filename &optional id-format)
1066 "Like `file-attributes' for Tramp files."
1067 (unless id-format (setq id-format 'integer))
1068 ;; Don't modify `last-coding-system-used' by accident.
1069 (let ((last-coding-system-used last-coding-system-used))
1070 (with-parsed-tramp-file-name (expand-file-name filename) nil
1071 (with-tramp-file-property
1072 v localname (format "file-attributes-%s" id-format)
1074 (tramp-convert-file-attributes
1078 ((tramp-get-remote-stat v)
1079 (tramp-do-file-attributes-with-stat v localname id-format))
1080 ((tramp-get-remote-perl v)
1081 (tramp-do-file-attributes-with-perl v localname id-format))
1083 ;; The scripts could fail, for example with huge file size.
1084 (tramp-do-file-attributes-with-ls v localname id-format))))))))
1086 (defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
1087 "Implement `file-attributes' for Tramp files using the ls(1) command."
1089 res-inode res-filemodes res-numlinks
1090 res-uid res-gid res-size res-symlink-target)
1091 (tramp-message vec 5 "file attributes with ls: %s" localname)
1094 (format "(%s %s || %s -h %s) && %s %s %s"
1095 (tramp-get-file-exists-command vec)
1096 (tramp-shell-quote-argument localname)
1097 (tramp-get-test-command vec)
1098 (tramp-shell-quote-argument localname)
1099 (tramp-get-ls-command vec)
1100 (if (eq id-format 'integer) "-ildn" "-ild")
1101 (tramp-shell-quote-argument localname)))
1102 ;; parse `ls -l' output ...
1103 (with-current-buffer (tramp-get-buffer vec)
1104 (when (> (buffer-size) 0)
1105 (goto-char (point-min))
1109 (read (current-buffer))
1110 (invalid-read-syntax
1111 (when (and (equal (cadr err)
1112 "Integer constant overflow in reader")
1114 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1116 (let* ((big (read (substring (car (cddr err)) 0
1117 (match-beginning 1))))
1118 (small (read (match-string 1 (car (cddr err)))))
1119 (twiddle (/ small 65536)))
1120 (cons (+ big twiddle)
1121 (- small (* twiddle 65536))))))))
1122 ;; ... file mode flags
1123 (setq res-filemodes (symbol-name (read (current-buffer))))
1125 (setq res-numlinks (read (current-buffer)))
1127 (setq res-uid (read (current-buffer)))
1128 (setq res-gid (read (current-buffer)))
1129 (if (eq id-format 'integer)
1131 (unless (numberp res-uid) (setq res-uid -1))
1132 (unless (numberp res-gid) (setq res-gid -1)))
1134 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
1135 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
1137 (setq res-size (read (current-buffer)))
1138 ;; From the file modes, figure out other stuff.
1139 (setq symlinkp (eq ?l (aref res-filemodes 0)))
1140 (setq dirp (eq ?d (aref res-filemodes 0)))
1141 ;; if symlink, find out file name pointed to
1143 (search-forward "-> ")
1144 (setq res-symlink-target (buffer-substring (point) (point-at-eol))))
1145 ;; return data gathered
1147 ;; 0. t for directory, string (name linked to) for symbolic
1149 (or dirp res-symlink-target)
1150 ;; 1. Number of links to file.
1156 ;; 4. Last access time, as a list of integers. Normally this
1157 ;; would be in the same format as `current-time', but the
1158 ;; subseconds part is not currently implemented, and (0 0)
1159 ;; denotes an unknown time.
1160 ;; 5. Last modification time, likewise.
1161 ;; 6. Last status change time, likewise.
1162 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1163 ;; 7. Size in bytes (-1, if number is out of range).
1165 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1167 ;; 9. t if file's gid would change if file were deleted and
1168 ;; recreated. Will be set in `tramp-convert-file-attributes'
1170 ;; 10. inode number.
1172 ;; 11. Device number. Will be replaced by a virtual device number.
1176 (defun tramp-do-file-attributes-with-perl
1177 (vec localname &optional id-format)
1178 "Implement `file-attributes' for Tramp files using a Perl script."
1179 (tramp-message vec 5 "file attributes with perl: %s" localname)
1180 (tramp-maybe-send-script
1181 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
1182 (tramp-send-command-and-read
1184 (format "tramp_perl_file_attributes %s %s"
1185 (tramp-shell-quote-argument localname) id-format)))
1187 (defun tramp-do-file-attributes-with-stat
1188 (vec localname &optional id-format)
1189 "Implement `file-attributes' for Tramp files using stat(1) command."
1190 (tramp-message vec 5 "file attributes with stat: %s" localname)
1191 (tramp-send-command-and-read
1194 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
1195 ;; parse correctly the sequence "((". Therefore, we add a space.
1196 "( (%s %s || %s -h %s) && %s -c '((\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)' %s || echo nil)"
1197 (tramp-get-file-exists-command vec)
1198 (tramp-shell-quote-argument localname)
1199 (tramp-get-test-command vec)
1200 (tramp-shell-quote-argument localname)
1201 (tramp-get-remote-stat vec)
1202 (if (eq id-format 'integer) "%ue0" "\"%U\"")
1203 (if (eq id-format 'integer) "%ge0" "\"%G\"")
1204 (tramp-shell-quote-argument localname))))
1206 (defun tramp-sh-handle-set-visited-file-modtime (&optional time-list)
1207 "Like `set-visited-file-modtime' for Tramp files."
1208 (unless (buffer-file-name)
1209 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1212 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
1213 (let ((f (buffer-file-name))
1215 (with-parsed-tramp-file-name f nil
1216 (let* ((remote-file-name-inhibit-cache t)
1217 (attr (file-attributes f))
1218 ;; '(-1 65535) means file doesn't exists yet.
1219 (modtime (or (nth 5 attr) '(-1 65535))))
1220 (when (boundp 'last-coding-system-used)
1221 (setq coding-system-used (symbol-value 'last-coding-system-used)))
1222 ;; We use '(0 0) as a don't-know value. See also
1223 ;; `tramp-do-file-attributes-with-ls'.
1224 (if (not (equal modtime '(0 0)))
1225 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
1229 (format "%s -ild %s"
1230 (tramp-get-ls-command v)
1231 (tramp-shell-quote-argument localname)))
1232 (setq attr (buffer-substring (point)
1233 (progn (end-of-line) (point)))))
1234 (tramp-set-file-property
1235 v localname "visited-file-modtime-ild" attr))
1236 (when (boundp 'last-coding-system-used)
1237 (set 'last-coding-system-used coding-system-used))
1240 ;; This function makes the same assumption as
1241 ;; `tramp-sh-handle-set-visited-file-modtime'.
1242 (defun tramp-sh-handle-verify-visited-file-modtime (buf)
1243 "Like `verify-visited-file-modtime' for Tramp files.
1244 At the time `verify-visited-file-modtime' calls this function, we
1245 already know that the buffer is visiting a file and that
1246 `visited-file-modtime' does not return 0. Do not call this
1247 function directly, unless those two cases are already taken care
1249 (with-current-buffer buf
1250 (let ((f (buffer-file-name)))
1251 ;; There is no file visiting the buffer, or the buffer has no
1252 ;; recorded last modification time, or there is no established
1255 (eq (visited-file-modtime) 0)
1256 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
1258 (with-parsed-tramp-file-name f nil
1259 (let* ((remote-file-name-inhibit-cache t)
1260 (attr (file-attributes f))
1261 (modtime (nth 5 attr))
1262 (mt (visited-file-modtime)))
1265 ;; File exists, and has a known modtime.
1266 ((and attr (not (equal modtime '(0 0))))
1267 (< (abs (tramp-time-diff
1269 ;; For compatibility, deal with both the old
1270 ;; (HIGH . LOW) and the new (HIGH LOW) return
1271 ;; values of `visited-file-modtime'.
1273 (list (car mt) (cdr mt))
1276 ;; Modtime has the don't know value.
1280 (format "%s -ild %s"
1281 (tramp-get-ls-command v)
1282 (tramp-shell-quote-argument localname)))
1283 (with-current-buffer (tramp-get-buffer v)
1284 (setq attr (buffer-substring
1285 (point) (progn (end-of-line) (point)))))
1288 (tramp-get-file-property
1289 v localname "visited-file-modtime-ild" "")))
1290 ;; If file does not exist, say it is not modified if and
1291 ;; only if that agrees with the buffer's record.
1292 (t (equal mt '(-1 65535))))))))))
1294 (defun tramp-sh-handle-set-file-modes (filename mode)
1295 "Like `set-file-modes' for Tramp files."
1296 (with-parsed-tramp-file-name filename nil
1297 (tramp-flush-file-property v localname)
1298 ;; FIXME: extract the proper text from chmod's stderr.
1299 (tramp-barf-unless-okay
1301 (format "chmod %s %s"
1302 (tramp-compat-decimal-to-octal mode)
1303 (tramp-shell-quote-argument localname))
1304 "Error while changing file's mode %s" filename)))
1306 (defun tramp-sh-handle-set-file-times (filename &optional time)
1307 "Like `set-file-times' for Tramp files."
1308 (if (file-remote-p filename)
1309 (with-parsed-tramp-file-name filename nil
1310 (tramp-flush-file-property v localname)
1311 (let ((time (if (or (null time) (equal time '(0 0)))
1314 ;; With GNU Emacs, `format-time-string' has an optional
1315 ;; parameter UNIVERSAL. This is preferred, because we
1316 ;; could handle the case when the remote host is located
1317 ;; in a different time zone as the local host.
1318 (utc (not (featurep 'xemacs))))
1319 (tramp-send-command-and-check
1320 v (format "%s touch -t %s %s"
1321 (if utc "env TZ=UTC" "")
1323 (format-time-string "%Y%m%d%H%M.%S" time t)
1324 (format-time-string "%Y%m%d%H%M.%S" time))
1325 (tramp-shell-quote-argument localname)))))
1327 ;; We handle also the local part, because in older Emacsen,
1328 ;; without `set-file-times', this function is an alias for this.
1329 ;; We are local, so we don't need the UTC settings.
1331 (tramp-compat-call-process
1332 "touch" nil nil nil "-t"
1333 (format-time-string "%Y%m%d%H%M.%S" time)
1334 (tramp-shell-quote-argument filename)))))
1336 (defun tramp-set-file-uid-gid (filename &optional uid gid)
1337 "Set the ownership for FILENAME.
1338 If UID and GID are provided, these values are used; otherwise uid
1339 and gid of the corresponding user is taken. Both parameters must
1340 be non-negative integers."
1341 ;; Modern Unices allow chown only for root. So we might need
1342 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1343 ;; working with su(do)? when it is needed, so it shall succeed in
1344 ;; the majority of cases.
1345 ;; Don't modify `last-coding-system-used' by accident.
1346 (let ((last-coding-system-used last-coding-system-used))
1347 (if (file-remote-p filename)
1348 (with-parsed-tramp-file-name filename nil
1349 (if (and (zerop (user-uid)) (tramp-local-host-p v))
1350 ;; If we are root on the local host, we can do it directly.
1351 (tramp-set-file-uid-gid localname uid gid)
1352 (let ((uid (or (and (natnump uid) uid)
1353 (tramp-get-remote-uid v 'integer)))
1354 (gid (or (and (natnump gid) gid)
1355 (tramp-get-remote-gid v 'integer))))
1358 "chown %d:%d %s" uid gid
1359 (tramp-shell-quote-argument localname))))))
1361 ;; We handle also the local part, because there doesn't exist
1362 ;; `set-file-uid-gid'. On W32 "chown" might not work.
1363 (let ((uid (or (and (natnump uid) uid) (tramp-get-local-uid 'integer)))
1364 (gid (or (and (natnump gid) gid) (tramp-get-local-gid 'integer))))
1365 (tramp-compat-call-process
1367 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
1369 (defun tramp-remote-selinux-p (vec)
1370 "Check, whether SELINUX is enabled on the remote host."
1371 (with-tramp-connection-property
1372 (tramp-get-connection-process vec) "selinux-p"
1373 (let ((result (tramp-find-executable
1374 vec "getenforce" (tramp-get-remote-path vec) t t)))
1377 (tramp-send-command-and-read
1378 vec (format "echo \\\"`%S`\\\"" result))
1381 (defun tramp-sh-handle-file-selinux-context (filename)
1382 "Like `file-selinux-context' for Tramp files."
1383 (with-parsed-tramp-file-name filename nil
1384 (with-tramp-file-property v localname "file-selinux-context"
1385 (let ((context '(nil nil nil nil))
1386 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1387 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1388 (when (and (tramp-remote-selinux-p v)
1389 (tramp-send-command-and-check
1392 (tramp-get-ls-command v)
1393 (tramp-shell-quote-argument localname))))
1394 (with-current-buffer (tramp-get-connection-buffer v)
1395 (goto-char (point-min))
1396 (when (re-search-forward regexp (point-at-eol) t)
1397 (setq context (list (match-string 1) (match-string 2)
1398 (match-string 3) (match-string 4))))))
1399 ;; Return the context.
1402 (defun tramp-sh-handle-set-file-selinux-context (filename context)
1403 "Like `set-file-selinux-context' for Tramp files."
1404 (with-parsed-tramp-file-name filename nil
1405 (if (and (consp context)
1406 (tramp-remote-selinux-p v)
1407 (tramp-send-command-and-check
1408 v (format "chcon %s %s %s %s %s"
1409 (if (stringp (nth 0 context))
1410 (format "--user=%s" (nth 0 context)) "")
1411 (if (stringp (nth 1 context))
1412 (format "--role=%s" (nth 1 context)) "")
1413 (if (stringp (nth 2 context))
1414 (format "--type=%s" (nth 2 context)) "")
1415 (if (stringp (nth 3 context))
1416 (format "--range=%s" (nth 3 context)) "")
1417 (tramp-shell-quote-argument localname))))
1419 (tramp-set-file-property v localname "file-selinux-context" context)
1421 (tramp-set-file-property v localname "file-selinux-context" 'undef)
1424 (defun tramp-remote-acl-p (vec)
1425 "Check, whether ACL is enabled on the remote host."
1426 (with-tramp-connection-property (tramp-get-connection-process vec) "acl-p"
1427 (tramp-send-command-and-check vec "getfacl /")))
1429 (defun tramp-sh-handle-file-acl (filename)
1430 "Like `file-acl' for Tramp files."
1431 (with-parsed-tramp-file-name filename nil
1432 (with-tramp-file-property v localname "file-acl"
1433 (when (and (tramp-remote-acl-p v)
1434 (tramp-send-command-and-check
1436 "getfacl -ac %s 2>/dev/null"
1437 (tramp-shell-quote-argument localname))))
1438 (with-current-buffer (tramp-get-connection-buffer v)
1439 (goto-char (point-max))
1440 (delete-blank-lines)
1441 (when (> (point-max) (point-min))
1442 (tramp-compat-funcall
1443 'substring-no-properties (buffer-string))))))))
1445 (defun tramp-sh-handle-set-file-acl (filename acl-string)
1446 "Like `set-file-acl' for Tramp files."
1447 (with-parsed-tramp-file-name (expand-file-name filename) nil
1448 (if (and (stringp acl-string) (tramp-remote-acl-p v)
1451 v (format "setfacl --set-file=- %s <<'EOF'\n%s\nEOF\n"
1452 (tramp-shell-quote-argument localname) acl-string))
1453 (tramp-send-command-and-check v nil)))
1456 (tramp-set-file-property v localname "file-acl" acl-string)
1458 ;; In case of errors, we return `nil'.
1459 (tramp-set-file-property v localname "file-acl-string" 'undef)
1462 ;; Simple functions using the `test' command.
1464 (defun tramp-sh-handle-file-executable-p (filename)
1465 "Like `file-executable-p' for Tramp files."
1466 (with-parsed-tramp-file-name filename nil
1467 (with-tramp-file-property v localname "file-executable-p"
1468 ;; Examine `file-attributes' cache to see if request can be
1469 ;; satisfied without remote operation.
1470 (or (tramp-check-cached-permissions v ?x)
1471 (tramp-run-test "-x" filename)))))
1473 (defun tramp-sh-handle-file-readable-p (filename)
1474 "Like `file-readable-p' for Tramp files."
1475 (with-parsed-tramp-file-name filename nil
1476 (with-tramp-file-property v localname "file-readable-p"
1477 ;; Examine `file-attributes' cache to see if request can be
1478 ;; satisfied without remote operation.
1479 (or (tramp-check-cached-permissions v ?r)
1480 (tramp-run-test "-r" filename)))))
1482 ;; When the remote shell is started, it looks for a shell which groks
1483 ;; tilde expansion. Here, we assume that all shells which grok tilde
1484 ;; expansion will also provide a `test' command which groks `-nt' (for
1485 ;; newer than). If this breaks, tell me about it and I'll try to do
1486 ;; something smarter about it.
1487 (defun tramp-sh-handle-file-newer-than-file-p (file1 file2)
1488 "Like `file-newer-than-file-p' for Tramp files."
1489 (cond ((not (file-exists-p file1))
1491 ((not (file-exists-p file2))
1493 ;; We are sure both files exist at this point.
1496 ;; We try to get the mtime of both files. If they are not
1497 ;; equal to the "dont-know" value, then we subtract the times
1498 ;; and obtain the result.
1499 (let ((fa1 (file-attributes file1))
1500 (fa2 (file-attributes file2)))
1501 (if (and (not (equal (nth 5 fa1) '(0 0)))
1502 (not (equal (nth 5 fa2) '(0 0))))
1503 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
1504 ;; If one of them is the dont-know value, then we can
1505 ;; still try to run a shell command on the remote host.
1506 ;; However, this only works if both files are Tramp
1507 ;; files and both have the same method, same user, same
1509 (unless (tramp-equal-remote file1 file2)
1510 (with-parsed-tramp-file-name
1511 (if (tramp-tramp-file-p file1) file1 file2) nil
1514 "Files %s and %s must have same method, user, host"
1516 (with-parsed-tramp-file-name file1 nil
1518 (tramp-get-test-nt-command v) file1 file2))))))))
1520 ;; Functions implemented using the basic functions above.
1522 (defun tramp-sh-handle-file-directory-p (filename)
1523 "Like `file-directory-p' for Tramp files."
1524 (with-parsed-tramp-file-name filename nil
1525 ;; `file-directory-p' is used as predicate for filename completion.
1526 ;; Sometimes, when a connection is not established yet, it is
1527 ;; desirable to return t immediately for "/method:foo:". It can
1528 ;; be expected that this is always a directory.
1529 (or (zerop (length localname))
1530 (with-tramp-file-property v localname "file-directory-p"
1531 (tramp-run-test "-d" filename)))))
1533 (defun tramp-sh-handle-file-writable-p (filename)
1534 "Like `file-writable-p' for Tramp files."
1535 (with-parsed-tramp-file-name filename nil
1536 (with-tramp-file-property v localname "file-writable-p"
1537 (if (file-exists-p filename)
1538 ;; Examine `file-attributes' cache to see if request can be
1539 ;; satisfied without remote operation.
1540 (or (tramp-check-cached-permissions v ?w)
1541 (tramp-run-test "-w" filename))
1542 ;; If file doesn't exist, check if directory is writable.
1543 (and (tramp-run-test "-d" (file-name-directory filename))
1544 (tramp-run-test "-w" (file-name-directory filename)))))))
1546 (defun tramp-sh-handle-file-ownership-preserved-p (filename &optional group)
1547 "Like `file-ownership-preserved-p' for Tramp files."
1548 (with-parsed-tramp-file-name filename nil
1549 (with-tramp-file-property v localname "file-ownership-preserved-p"
1550 (let ((attributes (file-attributes filename)))
1551 ;; Return t if the file doesn't exist, since it's true that no
1552 ;; information would be lost by an (attempted) delete and create.
1553 (or (null attributes)
1555 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer))
1557 (= (nth 3 attributes) (tramp-get-remote-gid v 'integer)))))))))
1559 ;; Directory listings.
1561 (defun tramp-sh-handle-directory-files-and-attributes
1562 (directory &optional full match nosort id-format)
1563 "Like `directory-files-and-attributes' for Tramp files."
1564 (unless id-format (setq id-format 'integer))
1565 (when (file-directory-p directory)
1566 (setq directory (expand-file-name directory))
1569 (with-parsed-tramp-file-name directory nil
1570 (with-tramp-file-property
1572 (format "directory-files-and-attributes-%s" id-format)
1577 (tramp-convert-file-attributes v (cdr x))))
1579 ((tramp-get-remote-stat v)
1580 (tramp-do-directory-files-and-attributes-with-stat
1581 v localname id-format))
1582 ((tramp-get-remote-perl v)
1583 (tramp-do-directory-files-and-attributes-with-perl
1584 v localname id-format)))))))))
1588 (setq item (pop temp))
1589 (when (or (null match) (string-match match (car item)))
1591 (setcar item (expand-file-name (car item) directory)))
1592 (push item result)))
1596 (sort result (lambda (x y) (string< (car x) (car y))))))))
1598 (defun tramp-do-directory-files-and-attributes-with-perl
1599 (vec localname &optional id-format)
1600 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1601 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
1602 (tramp-maybe-send-script
1603 vec tramp-perl-directory-files-and-attributes
1604 "tramp_perl_directory_files_and_attributes")
1606 (tramp-send-command-and-read
1608 (format "tramp_perl_directory_files_and_attributes %s %s"
1609 (tramp-shell-quote-argument localname) id-format))))
1610 (when (stringp object) (tramp-error vec 'file-error object))
1613 (defun tramp-do-directory-files-and-attributes-with-stat
1614 (vec localname &optional id-format)
1615 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1616 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
1617 (tramp-send-command-and-read
1621 ;; We must care about filenames with spaces, or starting with
1622 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1623 ;; but it does not work on all remote systems. Therefore, we
1624 ;; quote the filenames via sed.
1625 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | "
1627 "'(\"%%n\" (\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)'"
1628 " 2>/dev/null); echo \")\"")
1629 (tramp-shell-quote-argument localname)
1630 (tramp-get-ls-command vec)
1631 (tramp-get-remote-stat vec)
1632 (if (eq id-format 'integer) "%ue0" "\"%U\"")
1633 (if (eq id-format 'integer) "%ge0" "\"%G\""))))
1635 ;; This function should return "foo/" for directories and "bar" for
1637 (defun tramp-sh-handle-file-name-all-completions (filename directory)
1638 "Like `file-name-all-completions' for Tramp files."
1639 (unless (save-match-data (string-match "/" filename))
1640 (with-parsed-tramp-file-name (expand-file-name directory) nil
1647 ;; Try cache entries for filename, filename with last
1648 ;; character removed, filename with last two characters
1649 ;; removed, ..., and finally the empty string - all
1650 ;; concatenated to the local directory name.
1651 (let ((remote-file-name-inhibit-cache
1652 (or remote-file-name-inhibit-cache
1653 tramp-completion-reread-directory-timeout)))
1655 ;; This is inefficient for very long filenames, pity
1656 ;; `reduce' is not available...
1663 (tramp-get-file-property
1665 (concat localname (substring filename 0 x))
1666 "file-name-all-completions"
1668 (when cache-hit (list cache-hit))))
1669 ;; We cannot use a length of 0, because file properties
1670 ;; for "foo" and "foo/" are identical.
1671 (tramp-compat-number-sequence (length filename) 1 -1)))))
1673 ;; Cache expired or no matching cache entry found so we need
1674 ;; to perform a remote operation.
1676 ;; Get a list of directories and files, including reliably
1677 ;; tagging the directories with a trailing '/'. Because I
1678 ;; rock. --daniel@danann.net
1680 ;; Changed to perform `cd' in the same remote op and only
1681 ;; get entries starting with `filename'. Capture any `cd'
1682 ;; error messages. Ensure any `cd' and `echo' aliases are
1686 (if (tramp-get-remote-perl v)
1688 (tramp-maybe-send-script
1689 v tramp-perl-file-name-all-completions
1690 "tramp_perl_file_name_all_completions")
1691 (format "tramp_perl_file_name_all_completions %s %s %d"
1692 (tramp-shell-quote-argument localname)
1693 (tramp-shell-quote-argument filename)
1695 ;; `read-file-name-completion-ignore-case'
1696 ;; is introduced with Emacs 22.1.
1698 'read-file-name-completion-ignore-case)
1699 'read-file-name-completion-ignore-case
1700 'completion-ignore-case))
1704 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
1705 ;; `ls' with wildcard might fail with `Argument
1706 ;; list too long' error in some corner cases; if
1707 ;; `ls' fails after `cd' succeeded, chances are
1708 ;; that's the case, so let's retry without
1709 ;; wildcard. This will return "too many" entries
1710 ;; but that isn't harmful.
1711 " || %s -a 2>/dev/null)"
1712 " | while read f; do"
1713 " if %s -d \"$f\" 2>/dev/null;"
1714 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1715 " && \\echo ok) || \\echo fail")
1716 (tramp-shell-quote-argument localname)
1717 (tramp-get-ls-command v)
1718 ;; When `filename' is empty, just `ls' without
1719 ;; filename argument is more efficient than `ls *'
1720 ;; for very large directories and might avoid the
1721 ;; `Argument list too long' error.
1723 ;; With and only with wildcard, we need to add
1724 ;; `-d' to prevent `ls' from descending into
1726 (if (zerop (length filename))
1728 (concat (tramp-shell-quote-argument filename) "* -d"))
1729 (tramp-get-ls-command v)
1730 (tramp-get-test-command v))))
1732 ;; Now grab the output.
1733 (with-current-buffer (tramp-get-buffer v)
1734 (goto-char (point-max))
1736 ;; Check result code, found in last line of output.
1738 (if (looking-at "^fail$")
1740 ;; Grab error message from line before last line
1741 ;; (it was put there by `cd 2>&1').
1745 "tramp-sh-handle-file-name-all-completions: %s"
1746 (buffer-substring (point) (point-at-eol))))
1747 ;; For peace of mind, if buffer doesn't end in `fail'
1748 ;; then it should end in `ok'. If neither are in the
1749 ;; buffer something went seriously wrong on the remote
1751 (unless (looking-at "^ok$")
1755 tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
1756 (tramp-shell-quote-argument localname) (buffer-string))))
1758 (while (zerop (forward-line -1))
1759 (push (buffer-substring (point) (point-at-eol)) result)))
1761 ;; Because the remote op went through OK we know the
1762 ;; directory we `cd'-ed to exists.
1763 (tramp-set-file-property v localname "file-exists-p" t)
1765 ;; Because the remote op went through OK we know every
1766 ;; file listed by `ls' exists.
1767 (mapc (lambda (entry)
1768 (tramp-set-file-property
1769 v (concat localname entry) "file-exists-p" t))
1772 ;; Store result in the cache.
1773 (tramp-set-file-property
1774 v (concat localname filename)
1775 "file-name-all-completions" result))))))))
1779 (defun tramp-sh-handle-add-name-to-file
1780 (filename newname &optional ok-if-already-exists)
1781 "Like `add-name-to-file' for Tramp files."
1782 (unless (tramp-equal-remote filename newname)
1783 (with-parsed-tramp-file-name
1784 (if (tramp-tramp-file-p filename) filename newname) nil
1787 "add-name-to-file: %s"
1788 "only implemented for same method, same user, same host")))
1789 (with-parsed-tramp-file-name filename v1
1790 (with-parsed-tramp-file-name newname v2
1791 (let ((ln (when v1 (tramp-get-remote-ln v1))))
1792 (when (and (not ok-if-already-exists)
1793 (file-exists-p newname)
1794 (not (numberp ok-if-already-exists))
1797 "File %s already exists; make it a new name anyway? "
1801 "add-name-to-file: file %s already exists" newname))
1802 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1803 (tramp-flush-file-property v2 v2-localname)
1804 (tramp-barf-unless-okay
1806 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
1807 (tramp-shell-quote-argument v2-localname))
1808 "error with add-name-to-file, see buffer `%s' for details"
1811 (defun tramp-sh-handle-copy-file
1812 (filename newname &optional ok-if-already-exists keep-date
1813 preserve-uid-gid preserve-extended-attributes)
1814 "Like `copy-file' for Tramp files."
1815 (setq filename (expand-file-name filename))
1816 (setq newname (expand-file-name newname))
1818 ;; At least one file a Tramp file?
1819 ((or (tramp-tramp-file-p filename)
1820 (tramp-tramp-file-p newname))
1821 (tramp-do-copy-or-rename-file
1822 'copy filename newname ok-if-already-exists keep-date
1823 preserve-uid-gid preserve-extended-attributes))
1825 (preserve-extended-attributes
1826 (tramp-run-real-handler
1828 (list filename newname ok-if-already-exists keep-date
1829 preserve-uid-gid preserve-extended-attributes)))
1831 (tramp-run-real-handler
1833 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
1835 (tramp-run-real-handler
1836 'copy-file (list filename newname ok-if-already-exists keep-date)))))
1838 (defun tramp-sh-handle-copy-directory
1839 (dirname newname &optional keep-date parents copy-contents)
1840 "Like `copy-directory' for Tramp files."
1841 (let ((t1 (tramp-tramp-file-p dirname))
1842 (t2 (tramp-tramp-file-p newname)))
1843 (with-parsed-tramp-file-name (if t1 dirname newname) nil
1844 (if (and (tramp-get-method-parameter method 'tramp-copy-recursive)
1845 ;; When DIRNAME and NEWNAME are remote, they must have
1847 (or (null t1) (null t2)
1849 (tramp-file-name-method (tramp-dissect-file-name dirname))
1850 (tramp-file-name-method (tramp-dissect-file-name newname)))))
1851 ;; scp or rsync DTRT.
1853 (setq dirname (directory-file-name (expand-file-name dirname))
1854 newname (directory-file-name (expand-file-name newname)))
1855 (if (and (file-directory-p newname)
1856 (not (string-equal (file-name-nondirectory dirname)
1857 (file-name-nondirectory newname))))
1860 (file-name-nondirectory dirname) newname)))
1861 (if (not (file-directory-p (file-name-directory newname)))
1862 (make-directory (file-name-directory newname) parents))
1863 (tramp-do-copy-or-rename-file-out-of-band
1864 'copy dirname newname keep-date))
1865 ;; We must do it file-wise.
1866 (tramp-run-real-handler
1867 'copy-directory (list dirname newname keep-date parents)))
1869 ;; When newname did exist, we have wrong cached values.
1871 (with-parsed-tramp-file-name newname nil
1872 (tramp-flush-file-property v (file-name-directory localname))
1873 (tramp-flush-file-property v localname))))))
1875 (defun tramp-sh-handle-rename-file
1876 (filename newname &optional ok-if-already-exists)
1877 "Like `rename-file' for Tramp files."
1878 ;; Check if both files are local -- invoke normal rename-file.
1879 ;; Otherwise, use Tramp from local system.
1880 (setq filename (expand-file-name filename))
1881 (setq newname (expand-file-name newname))
1882 ;; At least one file a Tramp file?
1883 (if (or (tramp-tramp-file-p filename)
1884 (tramp-tramp-file-p newname))
1885 (tramp-do-copy-or-rename-file
1886 'rename filename newname ok-if-already-exists t t)
1887 (tramp-run-real-handler
1888 'rename-file (list filename newname ok-if-already-exists))))
1890 (defun tramp-do-copy-or-rename-file
1891 (op filename newname &optional ok-if-already-exists keep-date
1892 preserve-uid-gid preserve-extended-attributes)
1893 "Copy or rename a remote file.
1894 OP must be `copy' or `rename' and indicates the operation to perform.
1895 FILENAME specifies the file to copy or rename, NEWNAME is the name of
1896 the new file (for copy) or the new name of the file (for rename).
1897 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
1898 KEEP-DATE means to make sure that NEWNAME has the same timestamp
1899 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
1900 the uid and gid if both files are on the same host.
1901 PRESERVE-EXTENDED-ATTRIBUTES activates selinux and acl commands.
1903 This function is invoked by `tramp-sh-handle-copy-file' and
1904 `tramp-sh-handle-rename-file'. It is an error if OP is neither
1905 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
1907 (unless (memq op '(copy rename))
1908 (error "Unknown operation `%s', must be `copy' or `rename'" op))
1909 (let ((t1 (tramp-tramp-file-p filename))
1910 (t2 (tramp-tramp-file-p newname))
1911 (length (nth 7 (file-attributes (file-truename filename))))
1912 (attributes (and preserve-extended-attributes
1913 (apply 'file-extended-attributes (list filename))))
1916 (with-parsed-tramp-file-name (if t1 filename newname) nil
1917 (when (and (not ok-if-already-exists) (file-exists-p newname))
1919 v 'file-already-exists "File %s already exists" newname))
1921 (with-tramp-progress-reporter
1922 v 0 (format "%s %s to %s"
1923 (if (eq op 'copy) "Copying" "Renaming")
1927 ;; Both are Tramp files.
1929 (with-parsed-tramp-file-name filename v1
1930 (with-parsed-tramp-file-name newname v2
1932 ;; Shortcut: if method, host, user are the same for
1933 ;; both files, we invoke `cp' or `mv' on the remote
1935 ((tramp-equal-remote filename newname)
1936 (tramp-do-copy-or-rename-file-directly
1938 ok-if-already-exists keep-date preserve-uid-gid))
1940 ;; Try out-of-band operation.
1942 (tramp-method-out-of-band-p v1 length)
1943 (tramp-method-out-of-band-p v2 length))
1944 (tramp-do-copy-or-rename-file-out-of-band
1945 op filename newname keep-date))
1947 ;; No shortcut was possible. So we copy the file
1948 ;; first. If the operation was `rename', we go back
1949 ;; and delete the original file (if the copy was
1950 ;; successful). The approach is simple-minded: we
1951 ;; create a new buffer, insert the contents of the
1952 ;; source file into it, then write out the buffer to
1953 ;; the target file. The advantage is that it doesn't
1954 ;; matter which filename handlers are used for the
1955 ;; source and target file.
1957 (tramp-do-copy-or-rename-file-via-buffer
1958 op filename newname keep-date))))))
1960 ;; One file is a Tramp file, the other one is local.
1963 ;; Fast track on local machine.
1964 ((tramp-local-host-p v)
1965 (tramp-do-copy-or-rename-file-directly
1967 ok-if-already-exists keep-date preserve-uid-gid))
1969 ;; If the Tramp file has an out-of-band method, the
1970 ;; corresponding copy-program can be invoked.
1971 ((tramp-method-out-of-band-p v length)
1972 (tramp-do-copy-or-rename-file-out-of-band
1973 op filename newname keep-date))
1975 ;; Use the inline method via a Tramp buffer.
1976 (t (tramp-do-copy-or-rename-file-via-buffer
1977 op filename newname keep-date))))
1980 ;; One of them must be a Tramp file.
1981 (error "Tramp implementation says this cannot happen")))
1983 ;; Handle `preserve-extended-attributes'. We ignore possible
1984 ;; errors, because ACL strings could be incompatible.
1987 (apply 'set-file-extended-attributes (list newname attributes))))
1989 ;; In case of `rename', we must flush the cache of the source file.
1990 (when (and t1 (eq op 'rename))
1991 (with-parsed-tramp-file-name filename v1
1992 (tramp-flush-file-property v1 (file-name-directory localname))
1993 (tramp-flush-file-property v1 localname)))
1995 ;; When newname did exist, we have wrong cached values.
1997 (with-parsed-tramp-file-name newname v2
1998 (tramp-flush-file-property v2 (file-name-directory localname))
1999 (tramp-flush-file-property v2 localname)))))))
2001 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
2002 "Use an Emacs buffer to copy or rename a file.
2003 First arg OP is either `copy' or `rename' and indicates the operation.
2004 FILENAME is the source file, NEWNAME the target file.
2005 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2007 ;; We must disable multibyte, because binary data shall not be
2009 (set-buffer-multibyte nil)
2010 (let ((coding-system-for-read 'binary)
2011 (jka-compr-inhibit t))
2012 (insert-file-contents-literally filename))
2013 ;; We don't want the target file to be compressed, so we let-bind
2014 ;; `jka-compr-inhibit' to t.
2015 (let ((coding-system-for-write 'binary)
2016 (jka-compr-inhibit t))
2017 (write-region (point-min) (point-max) newname)))
2018 ;; KEEP-DATE handling.
2019 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
2021 (set-file-modes newname (tramp-default-file-modes filename))
2022 ;; If the operation was `rename', delete the original file.
2023 (unless (eq op 'copy) (delete-file filename)))
2025 (defun tramp-do-copy-or-rename-file-directly
2026 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
2027 "Invokes `cp' or `mv' on the remote system.
2028 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2029 respectively. FILENAME specifies the file to copy or rename,
2030 NEWNAME is the name of the new file (for copy) or the new name of
2031 the file (for rename). Both files must reside on the same host.
2032 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2033 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2034 the uid and gid from FILENAME."
2035 (let ((t1 (tramp-tramp-file-p filename))
2036 (t2 (tramp-tramp-file-p newname))
2037 (file-times (nth 5 (file-attributes filename)))
2038 (file-modes (tramp-default-file-modes filename)))
2039 (with-parsed-tramp-file-name (if t1 filename newname) nil
2040 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
2041 ((eq op 'copy) "cp -f")
2042 ((eq op 'rename) "mv -f")
2045 "Unknown operation `%s', must be `copy' or `rename'"
2049 (tramp-file-name-handler 'file-remote-p filename 'localname)
2053 (tramp-file-name-handler 'file-remote-p newname 'localname)
2055 (prefix (file-remote-p (if t1 filename newname)))
2059 ;; Both files are on a remote host, with same user.
2062 (tramp-send-command-and-check
2063 v (format "%s %s %s" cmd
2064 (tramp-shell-quote-argument localname1)
2065 (tramp-shell-quote-argument localname2))))
2066 (with-current-buffer (tramp-get-buffer v)
2067 (goto-char (point-min))
2071 ;; Mask cp -f error.
2073 tramp-operation-not-permitted-regexp nil t))
2075 (tramp-error-with-buffer
2077 "Copying directly failed, see buffer `%s' for details."
2080 ;; We are on the local host.
2083 ;; We can do it directly.
2084 ((let (file-name-handler-alist)
2085 (and (file-readable-p localname1)
2086 (file-writable-p (file-name-directory localname2))
2087 (or (file-directory-p localname2)
2088 (file-writable-p localname2))))
2090 (tramp-compat-copy-file
2091 localname1 localname2 ok-if-already-exists
2092 keep-date preserve-uid-gid)
2093 (tramp-run-real-handler
2094 'rename-file (list localname1 localname2 ok-if-already-exists))))
2096 ;; We can do it directly with `tramp-send-command'
2097 ((and (file-readable-p (concat prefix localname1))
2099 (file-name-directory (concat prefix localname2)))
2100 (or (file-directory-p (concat prefix localname2))
2101 (file-writable-p (concat prefix localname2))))
2102 (tramp-do-copy-or-rename-file-directly
2103 op (concat prefix localname1) (concat prefix localname2)
2104 ok-if-already-exists keep-date t)
2105 ;; We must change the ownership to the local user.
2106 (tramp-set-file-uid-gid
2107 (concat prefix localname2)
2108 (tramp-get-local-uid 'integer)
2109 (tramp-get-local-gid 'integer)))
2111 ;; We need a temporary file in between.
2113 ;; Create the temporary file.
2114 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
2119 (tramp-barf-unless-okay
2122 (tramp-shell-quote-argument localname1)
2123 (tramp-shell-quote-argument tmpfile))
2124 "Copying directly failed, see buffer `%s' for details."
2125 (tramp-get-buffer v))
2126 ;; We must change the ownership as remote user.
2127 ;; Since this does not work reliable, we also
2128 ;; give read permissions.
2130 (concat prefix tmpfile)
2131 (tramp-compat-octal-to-decimal "0777"))
2132 (tramp-set-file-uid-gid
2133 (concat prefix tmpfile)
2134 (tramp-get-local-uid 'integer)
2135 (tramp-get-local-gid 'integer)))
2138 (tramp-compat-copy-file
2139 localname1 tmpfile t
2140 keep-date preserve-uid-gid)
2141 (tramp-run-real-handler
2143 (list localname1 tmpfile t)))
2144 ;; We must change the ownership as local user.
2145 ;; Since this does not work reliable, we also
2146 ;; give read permissions.
2148 tmpfile (tramp-compat-octal-to-decimal "0777"))
2149 (tramp-set-file-uid-gid
2151 (tramp-get-remote-uid v 'integer)
2152 (tramp-get-remote-gid v 'integer))))
2154 ;; Move the temporary file to its destination.
2157 (tramp-barf-unless-okay
2160 (tramp-shell-quote-argument tmpfile)
2161 (tramp-shell-quote-argument localname2))
2162 "Copying directly failed, see buffer `%s' for details."
2163 (tramp-get-buffer v)))
2165 (tramp-run-real-handler
2167 (list tmpfile localname2 ok-if-already-exists)))))
2170 (ignore-errors (delete-file tmpfile)))))))))
2172 ;; Set the time and mode. Mask possible errors.
2175 (set-file-times newname file-times)
2176 (set-file-modes newname file-modes))))))
2178 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
2179 "Invoke rcp program to copy.
2180 The method used must be an out-of-band method."
2181 (let* ((t1 (tramp-tramp-file-p filename))
2182 (t2 (tramp-tramp-file-p newname))
2183 (orig-vec (tramp-dissect-file-name (if t1 filename newname)))
2184 copy-program copy-args copy-env copy-keep-date port spec
2185 options source target)
2187 (with-parsed-tramp-file-name (if t1 filename newname) nil
2190 ;; Both are Tramp files. We shall optimize it when the
2191 ;; methods for filename and newname are the same.
2192 (let* ((dir-flag (file-directory-p filename))
2193 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
2197 (file-name-nondirectory newname) tmpfile)))
2200 (tramp-do-copy-or-rename-file-out-of-band
2201 op filename tmpfile keep-date)
2202 (tramp-do-copy-or-rename-file-out-of-band
2203 'rename tmpfile newname keep-date))
2207 (tramp-compat-delete-directory
2208 (expand-file-name ".." tmpfile) 'recursive)
2209 (delete-file tmpfile)))))
2211 ;; Set variables for computing the prompt for reading
2213 (setq tramp-current-method (tramp-file-name-method v)
2214 tramp-current-user (or (tramp-file-name-user v)
2215 (tramp-get-connection-property
2217 tramp-current-host (tramp-file-name-real-host v))
2219 ;; Expand hops. Might be necessary for gateway methods.
2220 (setq v (car (tramp-compute-multi-hops v)))
2221 (aset v 3 localname)
2223 ;; Check which ones of source and target are Tramp files.
2224 (setq source (if t1 (tramp-make-copy-program-file-name v) filename)
2226 (if (and (file-directory-p filename)
2228 (file-name-nondirectory filename)
2229 (file-name-nondirectory newname)))
2230 'file-name-directory
2232 (if t2 (tramp-make-copy-program-file-name v) newname)))
2234 ;; Check for host and port number. We cannot use
2235 ;; `tramp-file-name-port', because this returns also
2236 ;; `tramp-default-port', which might clash with settings in
2238 (setq host (tramp-file-name-host v)
2240 (when (string-match tramp-host-with-port-regexp host)
2241 (setq port (string-to-number (match-string 2 host))
2242 host (string-to-number (match-string 1 host))))
2244 ;; Check for user. There might be an interactive setting.
2245 (setq user (or (tramp-file-name-user v)
2246 (tramp-get-connection-property v "login-as" nil)))
2248 ;; Compose copy command.
2249 (setq host (or host "")
2252 spec (format-spec-make
2253 ?t (tramp-get-connection-property
2254 (tramp-get-connection-process v) "temp-file" ""))
2255 options (format-spec tramp-ssh-controlmaster-options spec)
2256 spec (format-spec-make
2257 ?h host ?u user ?p port ?c options
2258 ?k (if keep-date " " ""))
2259 copy-program (tramp-get-method-parameter
2260 method 'tramp-copy-program)
2261 copy-keep-date (tramp-get-method-parameter
2262 method 'tramp-copy-keep-date)
2265 ;; " " has either been a replacement of "%k" (when
2266 ;; keep-date argument is non-nil), or a replacement
2267 ;; for the whole keep-date sublist.
2271 (tramp-get-method-parameter method 'tramp-copy-args)
2276 (let ((y (mapcar (lambda (z) (format-spec z spec)) x)))
2277 (if (member "" y) '(" ") y))))))
2283 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
2284 (unless (member "" x) (mapconcat 'identity x " ")))
2285 (tramp-get-method-parameter method 'tramp-copy-env))))
2287 ;; Check for program.
2288 (unless (let ((default-directory
2289 (tramp-compat-temporary-file-directory)))
2290 (executable-find copy-program))
2292 v 'file-error "Cannot find copy program: %s" copy-program))
2296 ;; The default directory must be remote.
2297 (let ((default-directory
2298 (file-name-directory (if t1 filename newname)))
2299 (process-environment (copy-sequence process-environment)))
2300 ;; Set the transfer process properties.
2301 (tramp-set-connection-property
2302 v "process-name" (buffer-name (current-buffer)))
2303 (tramp-set-connection-property
2304 v "process-buffer" (current-buffer))
2307 orig-vec 6 "%s=\"%s\"" (car copy-env) (cadr copy-env))
2308 (setenv (pop copy-env) (pop copy-env)))
2310 ;; Use an asynchronous process. By this, password can
2311 ;; be handled. The default directory must be local, in
2312 ;; order to apply the correct `copy-program'. We don't
2313 ;; set a timeout, because the copying of large files can
2314 ;; last longer than 60 secs.
2315 (let ((p (let ((default-directory
2316 (tramp-compat-temporary-file-directory)))
2317 (apply 'start-process-shell-command
2318 (tramp-get-connection-name v)
2319 (tramp-get-connection-buffer v)
2324 (shell-quote-argument source)
2325 (shell-quote-argument target)
2326 "&&" "echo" "tramp_exit_status" "0"
2327 "||" "echo" "tramp_exit_status" "1"))))))
2330 (mapconcat 'identity (process-command p) " "))
2331 (tramp-compat-set-process-query-on-exit-flag p nil)
2332 (tramp-process-actions
2333 p v nil tramp-actions-copy-out-of-band)
2335 ;; Check the return code.
2336 (goto-char (point-max))
2338 (re-search-backward "tramp_exit_status [0-9]+" nil t)
2340 orig-vec 'file-error
2341 "Couldn't find exit status of `%s'" (process-command p)))
2342 (skip-chars-forward "^ ")
2343 (unless (zerop (read (current-buffer)))
2346 orig-vec 'file-error
2347 "Error copying: `%s'"
2348 (buffer-substring (point-min) (point-at-eol))))))
2350 ;; Reset the transfer process properties.
2351 (tramp-message orig-vec 6 "\n%s" (buffer-string))
2352 (tramp-set-connection-property v "process-name" nil)
2353 (tramp-set-connection-property v "process-buffer" nil)))
2355 ;; Handle KEEP-DATE argument.
2356 (when (and keep-date (not copy-keep-date))
2357 (set-file-times newname (nth 5 (file-attributes filename))))
2360 (unless (and keep-date copy-keep-date)
2362 (set-file-modes newname (tramp-default-file-modes filename)))))
2364 ;; If the operation was `rename', delete the original file.
2365 (unless (eq op 'copy)
2366 (if (file-regular-p filename)
2367 (delete-file filename)
2368 (tramp-compat-delete-directory filename 'recursive))))))
2370 (defun tramp-sh-handle-make-directory (dir &optional parents)
2371 "Like `make-directory' for Tramp files."
2372 (setq dir (expand-file-name dir))
2373 (with-parsed-tramp-file-name dir nil
2374 (tramp-flush-directory-property v (file-name-directory localname))
2376 (tramp-barf-unless-okay
2378 (if parents "mkdir -p" "mkdir")
2379 (tramp-shell-quote-argument localname))
2380 "Couldn't make directory %s" dir))))
2382 (defun tramp-sh-handle-delete-directory (directory &optional recursive)
2383 "Like `delete-directory' for Tramp files."
2384 (setq directory (expand-file-name directory))
2385 (with-parsed-tramp-file-name directory nil
2386 (tramp-flush-file-property v (file-name-directory localname))
2387 (tramp-flush-directory-property v localname)
2388 (tramp-barf-unless-okay
2390 (if recursive "rm -rf" "rmdir")
2391 (tramp-shell-quote-argument localname))
2392 "Couldn't delete %s" directory)))
2394 (defun tramp-sh-handle-delete-file (filename &optional trash)
2395 "Like `delete-file' for Tramp files."
2396 (setq filename (expand-file-name filename))
2397 (with-parsed-tramp-file-name filename nil
2398 (tramp-flush-file-property v (file-name-directory localname))
2399 (tramp-flush-file-property v localname)
2400 (tramp-barf-unless-okay
2402 (or (and trash (tramp-get-remote-trash v)) "rm -f")
2403 (tramp-shell-quote-argument localname))
2404 "Couldn't delete %s" filename)))
2408 ;; CCC: This does not seem to be enough. Something dies when
2409 ;; we try and delete two directories under Tramp :/
2410 (defun tramp-sh-handle-dired-recursive-delete-directory (filename)
2411 "Recursively delete the directory given.
2412 This is like `dired-recursive-delete-directory' for Tramp files."
2413 (with-parsed-tramp-file-name filename nil
2414 ;; Run a shell command 'rm -r <localname>'.
2415 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
2416 (unless (file-exists-p filename)
2417 (tramp-error v 'file-error "No such directory: %s" filename))
2418 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>).
2421 (format "rm -rf %s" (tramp-shell-quote-argument localname))
2422 ;; Don't read the output, do it explicitly.
2424 ;; Wait for the remote system to return to us...
2425 ;; This might take a while, allow it plenty of time.
2426 (tramp-wait-for-output (tramp-get-connection-process v) 120)
2427 ;; Make sure that it worked...
2428 (tramp-flush-file-property v (file-name-directory localname))
2429 (tramp-flush-directory-property v localname)
2430 (and (file-exists-p filename)
2432 v 'file-error "Failed to recursively delete %s" filename))))
2434 (defun tramp-sh-handle-dired-compress-file (file &rest ok-flag)
2435 "Like `dired-compress-file' for Tramp files."
2436 ;; OK-FLAG is valid for XEmacs only, but not implemented.
2437 ;; Code stolen mainly from dired-aux.el.
2438 (with-parsed-tramp-file-name file nil
2439 (tramp-flush-file-property v localname)
2442 (if (not (featurep 'xemacs))
2444 (symbol-value 'dired-compress-file-suffixes)
2445 ;; XEmacs has `dired-compression-method-alist', which is
2446 ;; transformed into `dired-compress-file-suffixes' structure.
2449 (list (concat (regexp-quote (nth 1 x)) "\\'")
2451 (mapconcat 'identity (nth 3 x) " ")))
2452 (symbol-value 'dired-compression-method-alist))))
2454 ;; See if any suffix rule matches this file name.
2456 (let (case-fold-search)
2457 (if (string-match (car (car suffixes)) localname)
2458 (setq suffix (car suffixes) suffixes nil))
2459 (setq suffixes (cdr suffixes))))
2461 (cond ((file-symlink-p file)
2463 ((and suffix (nth 2 suffix))
2464 ;; We found an uncompression rule.
2465 (with-tramp-progress-reporter
2466 v 0 (format "Uncompressing %s" file)
2467 (when (tramp-send-command-and-check
2468 v (concat (nth 2 suffix) " "
2469 (tramp-shell-quote-argument localname)))
2470 ;; `dired-remove-file' is not defined in XEmacs.
2471 (tramp-compat-funcall 'dired-remove-file file)
2472 (string-match (car suffix) file)
2473 (concat (substring file 0 (match-beginning 0))))))
2475 ;; We don't recognize the file as compressed, so compress it.
2477 (with-tramp-progress-reporter v 0 (format "Compressing %s" file)
2478 (when (tramp-send-command-and-check
2479 v (concat "gzip -f "
2480 (tramp-shell-quote-argument localname)))
2481 ;; `dired-remove-file' is not defined in XEmacs.
2482 (tramp-compat-funcall 'dired-remove-file file)
2483 (cond ((file-exists-p (concat file ".gz"))
2484 (concat file ".gz"))
2485 ((file-exists-p (concat file ".z"))
2489 (defun tramp-sh-handle-insert-directory
2490 (filename switches &optional wildcard full-directory-p)
2491 "Like `insert-directory' for Tramp files."
2492 (setq filename (expand-file-name filename))
2493 (with-parsed-tramp-file-name filename nil
2494 (if (and (featurep 'ls-lisp)
2495 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
2496 (tramp-run-real-handler
2497 'insert-directory (list filename switches wildcard full-directory-p))
2498 (when (stringp switches)
2499 (setq switches (split-string switches)))
2500 (when (and (member "--dired" switches)
2501 (not (tramp-get-ls-command-with-dired v)))
2502 (setq switches (delete "--dired" switches)))
2504 (setq wildcard (tramp-run-real-handler
2505 'file-name-nondirectory (list localname)))
2506 (setq localname (tramp-run-real-handler
2507 'file-name-directory (list localname))))
2508 (unless full-directory-p
2509 (setq switches (add-to-list 'switches "-d" 'append)))
2510 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
2512 (setq switches (concat switches " " wildcard)))
2514 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2515 switches filename (if wildcard "yes" "no")
2516 (if full-directory-p "yes" "no"))
2517 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2518 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2519 ;; "--dired" returns byte positions. Therefore, the file names
2520 ;; must be encoded, which is guaranteed by "LC_ALL=en_US.utf8
2522 (if full-directory-p
2525 (format "env LC_ALL=en_US.utf8 LC_CTYPE='' %s %s %s 2>/dev/null"
2526 (tramp-get-ls-command v)
2530 (tramp-shell-quote-argument (concat localname ".")))))
2531 (tramp-barf-unless-okay
2533 (format "cd %s" (tramp-shell-quote-argument
2534 (tramp-run-real-handler
2535 'file-name-directory (list localname))))
2537 (tramp-shell-quote-argument
2538 (tramp-run-real-handler 'file-name-directory (list localname))))
2541 (format "env LC_ALL=en_US.utf8 LC_CTYPE='' %s %s %s 2>/dev/null"
2542 (tramp-get-ls-command v)
2546 (tramp-run-real-handler
2547 'file-name-nondirectory (list localname)))))
2549 (tramp-shell-quote-argument
2550 (tramp-run-real-handler
2551 'file-name-nondirectory (list localname)))))))
2552 (let ((beg (point)))
2553 ;; We cannot use `insert-buffer-substring' because the Tramp
2554 ;; buffer changes its contents before insertion due to calling
2555 ;; `expand-file' and alike.
2557 (with-current-buffer (tramp-get-buffer v)
2560 ;; Check for "--dired" output.
2562 (when (looking-at "//SUBDIRED//")
2564 (when (looking-at "//DIRED//\\s-+")
2565 (let ((databeg (match-end 0))
2566 (end (point-at-eol)))
2567 ;; Now read the numeric positions of file names.
2569 (while (< (point) end)
2570 (let ((start (+ beg (read (current-buffer))))
2571 (end (+ beg (read (current-buffer)))))
2572 (if (memq (char-after end) '(?\n ?\ ))
2573 ;; End is followed by \n or by " -> ".
2574 (put-text-property start end 'dired-filename t))))))
2575 ;; Remove trailing lines.
2576 (goto-char (point-at-bol))
2577 (while (looking-at "//")
2579 (delete-region (match-beginning 0) (point)))
2581 ;; Some busyboxes are reluctant to discard colors.
2582 (unless (string-match "color" (tramp-get-connection-property v "ls" ""))
2584 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
2585 (replace-match "")))
2587 ;; Decode the output, it could be multibyte.
2588 (decode-coding-region
2590 (or file-name-coding-system default-file-name-coding-system))
2592 ;; The inserted file could be from somewhere else.
2593 (when (and (not wildcard) (not full-directory-p))
2594 (goto-char (point-max))
2595 (when (file-symlink-p filename)
2596 (goto-char (search-backward "->" beg 'noerror)))
2598 (if (zerop (length (file-name-nondirectory filename)))
2600 (file-name-nondirectory filename))
2602 (replace-match (file-relative-name filename) t))
2604 (goto-char (point-max))))))
2606 ;; Canonicalization of file names.
2608 (defun tramp-sh-handle-expand-file-name (name &optional dir)
2609 "Like `expand-file-name' for Tramp files.
2610 If the localname part of the given filename starts with \"/../\" then
2611 the result will be a local, non-Tramp, filename."
2612 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
2613 (setq dir (or dir default-directory "/"))
2614 ;; Unless NAME is absolute, concat DIR and NAME.
2615 (unless (file-name-absolute-p name)
2616 (setq name (concat (file-name-as-directory dir) name)))
2617 ;; If NAME is not a Tramp file, run the real handler.
2618 (if (not (tramp-connectable-p name))
2619 (tramp-run-real-handler 'expand-file-name (list name nil))
2621 (with-parsed-tramp-file-name name nil
2622 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
2623 (setq localname (concat "~/" localname)))
2624 ;; Tilde expansion if necessary. This needs a shell which
2625 ;; groks tilde expansion! The function `tramp-find-shell' is
2626 ;; supposed to find such a shell on the remote host. Please
2627 ;; tell me about it when this doesn't work on your system.
2628 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
2629 (let ((uname (match-string 1 localname))
2630 (fname (match-string 2 localname)))
2631 ;; We cannot simply apply "~/", because under sudo "~/" is
2632 ;; expanded to the local user home directory but to the
2633 ;; root home directory. On the other hand, using always
2634 ;; the default user name for tilde expansion is not
2635 ;; appropriate either, because ssh and companions might
2636 ;; use a user name from the config file.
2637 (when (and (string-equal uname "~")
2638 (string-match "\\`su\\(do\\)?\\'" method))
2639 (setq uname (concat uname user)))
2641 (with-tramp-connection-property v uname
2643 v (format "cd %s; pwd" (tramp-shell-quote-argument uname)))
2644 (with-current-buffer (tramp-get-buffer v)
2645 (goto-char (point-min))
2646 (buffer-substring (point) (point-at-eol)))))
2647 (setq localname (concat uname fname))))
2648 ;; There might be a double slash, for example when "~/"
2649 ;; expands to "/". Remove this.
2650 (while (string-match "//" localname)
2651 (setq localname (replace-match "/" t t localname)))
2652 ;; No tilde characters in file name, do normal
2653 ;; `expand-file-name' (this does "/./" and "/../"). We bind
2654 ;; `directory-sep-char' here for XEmacs on Windows, which would
2655 ;; otherwise use backslash. `default-directory' is bound,
2656 ;; because on Windows there would be problems with UNC shares or
2658 (let ((directory-sep-char ?/)
2659 (default-directory (tramp-compat-temporary-file-directory)))
2660 (tramp-make-tramp-file-name
2662 (tramp-drop-volume-letter
2663 (tramp-run-real-handler
2664 'expand-file-name (list localname)))
2667 ;;; Remote commands:
2669 (defun tramp-sh-handle-executable-find (command)
2670 "Like `executable-find' for Tramp files."
2671 (with-parsed-tramp-file-name default-directory nil
2672 (tramp-find-executable v command (tramp-get-remote-path v) t)))
2674 (defun tramp-process-sentinel (proc event)
2675 "Flush file caches."
2676 (unless (memq (process-status proc) '(run open))
2677 (let ((vec (tramp-get-connection-property proc "vector" nil)))
2679 (tramp-message vec 5 "Sentinel called: `%s' `%s'" proc event)
2680 (tramp-flush-connection-property proc)
2681 (tramp-flush-directory-property vec "")))))
2683 ;; We use BUFFER also as connection buffer during setup. Because of
2684 ;; this, its original contents must be saved, and restored once
2685 ;; connection has been setup.
2686 (defun tramp-sh-handle-start-file-process (name buffer program &rest args)
2687 "Like `start-file-process' for Tramp files."
2688 (with-parsed-tramp-file-name default-directory nil
2689 ;; When PROGRAM is nil, we just provide a tty.
2691 (when (stringp program)
2692 (format "cd %s; exec env PS1=%s %s"
2693 (tramp-shell-quote-argument localname)
2694 ;; Use a human-friendly prompt, for example for `shell'.
2695 (tramp-shell-quote-argument
2697 (file-remote-p default-directory)
2698 tramp-initial-end-of-output))
2699 (mapconcat 'tramp-shell-quote-argument
2700 (cons program args) " "))))
2701 (tramp-process-connection-type
2702 (or (null program) tramp-process-connection-type))
2703 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
2706 ;; We do not want to raise an error when
2707 ;; `start-file-process' has been started several time in
2708 ;; `eshell' and friends.
2709 (tramp-current-connection nil))
2712 ;; BUFFER can be nil. We use a temporary buffer.
2713 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
2714 (while (get-process name1)
2715 ;; NAME must be unique as process name.
2717 name1 (format "%s<%d>" name i)))
2719 ;; Set the new process properties.
2720 (tramp-set-connection-property v "process-name" name)
2721 (tramp-set-connection-property v "process-buffer" buffer)
2723 (with-current-buffer (tramp-get-connection-buffer v)
2725 ;; We catch this event. Otherwise, `start-process' could
2726 ;; be called on the local host.
2729 ;; Activate narrowing in order to save BUFFER
2730 ;; contents. Clear also the modification time;
2731 ;; otherwise we might be interrupted by
2732 ;; `verify-visited-file-modtime'.
2733 (let ((buffer-undo-list t)
2734 (buffer-read-only nil)
2736 (clear-visited-file-modtime)
2737 (narrow-to-region (point-max) (point-max))
2738 ;; We call `tramp-maybe-open-connection', in order
2739 ;; to cleanup the prompt afterwards.
2741 (tramp-maybe-open-connection v)
2743 (delete-region mark (point))
2744 (narrow-to-region (point-max) (point-max))
2747 ;; Send the command.
2748 (tramp-send-command v command nil t) ; nooutput
2749 ;; Check, whether a pty is associated.
2750 (unless (tramp-compat-process-get
2751 (tramp-get-connection-process v) 'remote-tty)
2754 "pty association is not supported for `%s'" name))))
2755 (let ((p (tramp-get-connection-process v)))
2756 ;; Set query flag and process marker for this
2757 ;; process. We ignore errors, because the process
2758 ;; could have finished already.
2760 (tramp-compat-set-process-query-on-exit-flag p t)
2761 (set-marker (process-mark p) (point)))
2766 (if (string-match tramp-temp-buffer-name (buffer-name))
2768 (set-process-buffer (tramp-get-connection-process v) nil)
2769 (kill-buffer (current-buffer)))
2770 (set-buffer-modified-p bmp))
2771 (tramp-set-connection-property v "process-name" nil)
2772 (tramp-set-connection-property v "process-buffer" nil))))))
2774 (defun tramp-sh-handle-process-file
2775 (program &optional infile destination display &rest args)
2776 "Like `process-file' for Tramp files."
2777 ;; The implementation is not complete yet.
2778 (when (and (numberp destination) (zerop destination))
2779 (error "Implementation does not handle immediate return"))
2781 (with-parsed-tramp-file-name default-directory nil
2782 (let (command input tmpinput stderr tmpstderr outbuf ret)
2784 (setq command (mapconcat 'tramp-shell-quote-argument
2785 (cons program args) " "))
2788 (setq input "/dev/null")
2789 (setq infile (expand-file-name infile))
2790 (if (tramp-equal-remote default-directory infile)
2791 ;; INFILE is on the same remote host.
2792 (setq input (with-parsed-tramp-file-name infile nil localname))
2793 ;; INFILE must be copied to remote host.
2794 (setq input (tramp-make-tramp-temp-file v)
2795 tmpinput (tramp-make-tramp-file-name method user host input))
2796 (copy-file infile tmpinput t)))
2797 (when input (setq command (format "%s <%s" command input)))
2799 ;; Determine output.
2802 ((bufferp destination)
2803 (setq outbuf destination))
2805 ((stringp destination)
2806 (setq outbuf (get-buffer-create destination)))
2807 ;; (REAL-DESTINATION ERROR-DESTINATION)
2808 ((consp destination)
2811 ((bufferp (car destination))
2812 (setq outbuf (car destination)))
2813 ((stringp (car destination))
2814 (setq outbuf (get-buffer-create (car destination))))
2816 (setq outbuf (current-buffer))))
2819 ((stringp (cadr destination))
2820 (setcar (cdr destination) (expand-file-name (cadr destination)))
2821 (if (tramp-equal-remote default-directory (cadr destination))
2822 ;; stderr is on the same remote host.
2823 (setq stderr (with-parsed-tramp-file-name
2824 (cadr destination) nil localname))
2825 ;; stderr must be copied to remote host. The temporary
2826 ;; file must be deleted after execution.
2827 (setq stderr (tramp-make-tramp-temp-file v)
2828 tmpstderr (tramp-make-tramp-file-name
2829 method user host stderr))))
2830 ;; stderr to be discarded.
2831 ((null (cadr destination))
2832 (setq stderr "/dev/null"))))
2835 (setq outbuf (current-buffer))))
2836 (when stderr (setq command (format "%s 2>%s" command stderr)))
2838 ;; Send the command. It might not return in time, so we protect
2839 ;; it. Call it in a subshell, in order to preserve working
2844 (if (tramp-send-command-and-check
2845 v (format "\\cd %s; %s"
2846 (tramp-shell-quote-argument localname)
2850 ;; We should show the output anyway.
2852 (with-current-buffer outbuf
2854 (with-current-buffer (tramp-get-connection-buffer v)
2856 (when display (display-buffer outbuf))))
2857 ;; When the user did interrupt, we should do it also. We use
2858 ;; return code -1 as marker.
2860 (kill-buffer (tramp-get-connection-buffer v))
2864 (kill-buffer (tramp-get-connection-buffer v))
2867 ;; Provide error file.
2868 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
2870 ;; Cleanup. We remove all file cache values for the connection,
2871 ;; because the remote process could have changed them.
2872 (when tmpinput (delete-file tmpinput))
2874 ;; `process-file-side-effects' has been introduced with GNU
2875 ;; Emacs 23.2. If set to `nil', no remote file will be changed
2876 ;; by `program'. If it doesn't exist, we assume its default
2878 (unless (and (boundp 'process-file-side-effects)
2879 (not (symbol-value 'process-file-side-effects)))
2880 (tramp-flush-directory-property v ""))
2882 ;; Return exit status.
2887 (defun tramp-sh-handle-file-local-copy (filename)
2888 "Like `file-local-copy' for Tramp files."
2889 (with-parsed-tramp-file-name filename nil
2890 (unless (file-exists-p filename)
2893 "Cannot make local copy of non-existing file `%s'" filename))
2895 (let* ((size (nth 7 (file-attributes (file-truename filename))))
2896 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
2897 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
2898 (tmpfile (tramp-compat-make-temp-file filename)))
2902 ;; `copy-file' handles direct copy and out-of-band methods.
2903 ((or (tramp-local-host-p v)
2904 (tramp-method-out-of-band-p v size))
2905 (copy-file filename tmpfile t t))
2907 ;; Use inline encoding for file transfer.
2910 (with-tramp-progress-reporter
2911 v 3 (format "Encoding remote file %s" filename)
2912 (tramp-barf-unless-okay
2913 v (format rem-enc (tramp-shell-quote-argument localname))
2914 "Encoding remote file failed"))
2916 (if (functionp loc-dec)
2917 ;; If local decoding is a function, we call it. We
2918 ;; must disable multibyte, because
2919 ;; `uudecode-decode-region' doesn't handle it
2922 (set-buffer-multibyte nil)
2923 (insert-buffer-substring (tramp-get-buffer v))
2924 (with-tramp-progress-reporter
2925 v 3 (format "Decoding remote file %s with function %s"
2927 (funcall loc-dec (point-min) (point-max))
2928 ;; Unset `file-name-handler-alist'. Otherwise,
2929 ;; epa-file gets confused.
2930 (let (file-name-handler-alist
2931 (coding-system-for-write 'binary))
2932 (write-region (point-min) (point-max) tmpfile))))
2934 ;; If tramp-decoding-function is not defined for this
2935 ;; method, we invoke tramp-decoding-command instead.
2936 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
2937 ;; Unset `file-name-handler-alist'. Otherwise,
2938 ;; epa-file gets confused.
2939 (let (file-name-handler-alist
2940 (coding-system-for-write 'binary))
2941 (write-region (point-min) (point-max) tmpfile2))
2942 (with-tramp-progress-reporter
2943 v 3 (format "Decoding remote file %s with command %s"
2946 (tramp-call-local-coding-command
2947 loc-dec tmpfile2 tmpfile)
2948 (delete-file tmpfile2)))))
2950 ;; Set proper permissions.
2951 (set-file-modes tmpfile (tramp-default-file-modes filename))
2952 ;; Set local user ownership.
2953 (tramp-set-file-uid-gid tmpfile)))
2955 ;; Oops, I don't know what to do.
2957 v 'file-error "Wrong method specification for `%s'" method)))
2961 (delete-file tmpfile)
2962 (signal (car err) (cdr err))))
2964 (run-hooks 'tramp-handle-file-local-copy-hook)
2967 ;; This is needed for XEmacs only. Code stolen from files.el.
2968 (defun tramp-sh-handle-insert-file-contents-literally
2969 (filename &optional visit beg end replace)
2970 "Like `insert-file-contents-literally' for Tramp files."
2971 (let ((format-alist nil)
2972 (after-insert-file-functions nil)
2973 (coding-system-for-read 'no-conversion)
2974 (coding-system-for-write 'no-conversion)
2975 (find-buffer-file-type-function
2976 (if (fboundp 'find-buffer-file-type)
2977 (symbol-function 'find-buffer-file-type)
2979 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler))
2980 (inhibit-file-name-operation 'insert-file-contents))
2983 (fset 'find-buffer-file-type (lambda (filename) t))
2984 (insert-file-contents filename visit beg end replace))
2986 (if find-buffer-file-type-function
2987 (fset 'find-buffer-file-type find-buffer-file-type-function)
2988 (fmakunbound 'find-buffer-file-type)))))
2990 (defun tramp-sh-handle-make-auto-save-file-name ()
2991 "Like `make-auto-save-file-name' for Tramp files.
2992 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
2993 (let ((tramp-auto-save-directory tramp-auto-save-directory)
2995 (tramp-subst-strs-in-string
3002 (buffer-file-name))))
3003 ;; File name must be unique. This is ensured with Emacs 22 (see
3004 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
3005 ;; all other cases we must do it ourselves.
3006 (when (boundp 'auto-save-file-name-transforms)
3009 (when (and (string-match (car x) buffer-file-name)
3010 (not (car (cddr x))))
3011 (setq tramp-auto-save-directory
3012 (or tramp-auto-save-directory
3013 (tramp-compat-temporary-file-directory)))))
3014 (symbol-value 'auto-save-file-name-transforms)))
3015 ;; Create directory.
3016 (when tramp-auto-save-directory
3017 (setq buffer-file-name
3018 (expand-file-name buffer-file-name tramp-auto-save-directory))
3019 (unless (file-exists-p tramp-auto-save-directory)
3020 (make-directory tramp-auto-save-directory t)))
3021 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3022 ;; it is not a magic file name operation (since Emacs 22).
3023 ;; We must deactivate it temporarily.
3024 (if (not (ad-is-active 'make-auto-save-file-name))
3025 (tramp-run-real-handler 'make-auto-save-file-name nil)
3027 (ad-deactivate 'make-auto-save-file-name)
3029 (tramp-run-real-handler 'make-auto-save-file-name nil)
3030 (ad-activate 'make-auto-save-file-name)))))
3032 ;; CCC grok LOCKNAME
3033 (defun tramp-sh-handle-write-region
3034 (start end filename &optional append visit lockname confirm)
3035 "Like `write-region' for Tramp files."
3036 (setq filename (expand-file-name filename))
3037 (with-parsed-tramp-file-name filename nil
3038 ;; Following part commented out because we don't know what to do about
3039 ;; file locking, and it does not appear to be a problem to ignore it.
3040 ;; Ange-ftp ignores it, too.
3041 ;; (when (and lockname (stringp lockname))
3042 ;; (setq lockname (expand-file-name lockname)))
3043 ;; (unless (or (eq lockname nil)
3044 ;; (string= lockname filename))
3046 ;; "tramp-sh-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3048 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
3049 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
3050 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
3051 (tramp-error v 'file-error "File not overwritten")))
3053 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
3054 (tramp-get-remote-uid v 'integer)))
3055 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
3056 (tramp-get-remote-gid v 'integer))))
3058 (if (and (tramp-local-host-p v)
3059 ;; `file-writable-p' calls `file-expand-file-name'. We
3060 ;; cannot use `tramp-run-real-handler' therefore.
3061 (let (file-name-handler-alist)
3063 (file-writable-p (file-name-directory localname))
3064 (or (file-directory-p localname)
3065 (file-writable-p localname)))))
3066 ;; Short track: if we are on the local host, we can run directly.
3067 (tramp-run-real-handler
3069 (list start end localname append 'no-message lockname confirm))
3071 (let* ((modes (save-excursion (tramp-default-file-modes filename)))
3072 ;; We use this to save the value of
3073 ;; `last-coding-system-used' after writing the tmp
3074 ;; file. At the end of the function, we set
3075 ;; `last-coding-system-used' to this saved value. This
3076 ;; way, any intermediary coding systems used while
3077 ;; talking to the remote shell or suchlike won't hose
3078 ;; this variable. This approach was snarfed from
3081 ;; Write region into a tmp file. This isn't really
3082 ;; needed if we use an encoding function, but currently
3083 ;; we use it always because this makes the logic
3084 ;; simpler. We must also set `temporary-file-directory',
3085 ;; because it could point to a remote directory.
3086 (temporary-file-directory
3087 (tramp-compat-temporary-file-directory))
3088 (tmpfile (or tramp-temp-buffer-file-name
3089 (tramp-compat-make-temp-file filename))))
3091 ;; If `append' is non-nil, we copy the file locally, and let
3092 ;; the native `write-region' implementation do the job.
3093 (when append (copy-file filename tmpfile 'ok))
3095 ;; We say `no-message' here because we don't want the
3096 ;; visited file modtime data to be clobbered from the temp
3097 ;; file. We call `set-visited-file-modtime' ourselves later
3098 ;; on. We must ensure that `file-coding-system-alist'
3099 ;; matches `tmpfile'.
3100 (let (file-name-handler-alist
3101 (file-coding-system-alist
3102 (tramp-find-file-name-coding-system-alist filename tmpfile)))
3104 (tramp-run-real-handler
3106 (list start end tmpfile append 'no-message lockname confirm))
3108 (setq tramp-temp-buffer-file-name nil)
3109 (delete-file tmpfile)
3110 (signal (car err) (cdr err))))
3112 ;; Now, `last-coding-system-used' has the right value. Remember it.
3113 (when (boundp 'last-coding-system-used)
3114 (setq coding-system-used
3115 (symbol-value 'last-coding-system-used))))
3117 ;; The permissions of the temporary file should be set. If
3118 ;; filename does not exist (eq modes nil) it has been
3119 ;; renamed to the backup file. This case `save-buffer'
3120 ;; handles permissions.
3121 ;; Ensure that it is still readable.
3125 (logior (or modes 0) (tramp-compat-octal-to-decimal "0400"))))
3127 ;; This is a bit lengthy due to the different methods
3128 ;; possible for file transfer. First, we check whether the
3129 ;; method uses an rcp program. If so, we call it.
3130 ;; Otherwise, both encoding and decoding command must be
3131 ;; specified. However, if the method _also_ specifies an
3132 ;; encoding function, then that is used for encoding the
3133 ;; contents of the tmp file.
3134 (let* ((size (nth 7 (file-attributes tmpfile)))
3135 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
3136 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
3138 ;; `copy-file' handles direct copy and out-of-band methods.
3139 ((or (tramp-local-host-p v)
3140 (tramp-method-out-of-band-p v size))
3141 (if (and (not (stringp start))
3142 (= (or end (point-max)) (point-max))
3143 (= (or start (point-min)) (point-min))
3144 (tramp-get-method-parameter
3145 method 'tramp-copy-keep-tmpfile))
3147 (setq tramp-temp-buffer-file-name tmpfile)
3149 ;; We keep the local file for performance
3150 ;; reasons, useful for "rsync".
3151 (copy-file tmpfile filename t)
3153 (setq tramp-temp-buffer-file-name nil)
3154 (delete-file tmpfile)
3155 (signal (car err) (cdr err)))))
3156 (setq tramp-temp-buffer-file-name nil)
3157 ;; Don't rename, in order to keep context in SELinux.
3159 (copy-file tmpfile filename t)
3160 (delete-file tmpfile))))
3162 ;; Use inline file transfer.
3167 (set-buffer-multibyte nil)
3168 ;; Use encoding function or command.
3169 (if (functionp loc-enc)
3170 (with-tramp-progress-reporter
3171 v 3 (format "Encoding region using function `%s'"
3173 (let ((coding-system-for-read 'binary))
3174 (insert-file-contents-literally tmpfile))
3175 ;; The following `let' is a workaround for the
3176 ;; base64.el that comes with pgnus-0.84. If
3177 ;; both of the following conditions are
3178 ;; satisfied, it tries to write to a local
3179 ;; file in default-directory, but at this
3180 ;; point, default-directory is remote.
3181 ;; (`call-process-region' can't write to
3182 ;; remote files, it seems.) The file in
3183 ;; question is a tmp file anyway.
3184 (let ((default-directory
3185 (tramp-compat-temporary-file-directory)))
3186 (funcall loc-enc (point-min) (point-max))))
3188 (with-tramp-progress-reporter
3189 v 3 (format "Encoding region using command `%s'"
3191 (unless (zerop (tramp-call-local-coding-command
3195 (concat "Cannot write to `%s', "
3196 "local encoding command `%s' failed")
3197 filename loc-enc))))
3199 ;; Send buffer into remote decoding command which
3200 ;; writes to remote file. Because this happens on
3201 ;; the remote host, we cannot use the function.
3202 (with-tramp-progress-reporter
3204 (format "Decoding region into remote file %s" filename)
3205 (goto-char (point-max))
3206 (unless (bolp) (newline))
3210 (concat rem-dec " <<'EOF'\n%sEOF")
3211 (tramp-shell-quote-argument localname)
3213 (tramp-barf-unless-okay
3215 "Couldn't write region to `%s', decode using `%s' failed"
3217 ;; When `file-precious-flag' is set, the region is
3218 ;; written to a temporary file. Check that the
3219 ;; checksum is equal to that from the local tmpfile.
3220 (when file-precious-flag
3223 ;; cksum runs locally, if possible.
3224 (zerop (tramp-compat-call-process "cksum" tmpfile t))
3225 ;; cksum runs remotely.
3226 (tramp-send-command-and-check
3229 "cksum <%s" (tramp-shell-quote-argument localname)))
3230 ;; ... they are different.
3234 (with-current-buffer (tramp-get-buffer v)
3238 (concat "Couldn't write region to `%s',"
3239 " decode using `%s' failed")
3240 filename rem-dec)))))
3243 (delete-file tmpfile)))
3245 ;; That's not expected.
3249 (concat "Method `%s' should specify both encoding and "
3250 "decoding command or an rcp program")
3253 ;; Make `last-coding-system-used' have the right value.
3254 (when coding-system-used
3255 (set 'last-coding-system-used coding-system-used))))
3257 (tramp-flush-file-property v (file-name-directory localname))
3258 (tramp-flush-file-property v localname)
3260 ;; We must protect `last-coding-system-used', now we have set it
3261 ;; to its correct value.
3262 (let (last-coding-system-used (need-chown t))
3263 ;; Set file modification time.
3264 (when (or (eq visit t) (stringp visit))
3265 (let ((file-attr (tramp-compat-file-attributes filename 'integer)))
3266 (set-visited-file-modtime
3267 ;; We must pass modtime explicitly, because filename can
3268 ;; be different from (buffer-file-name), f.e. if
3269 ;; `file-precious-flag' is set.
3271 (when (and (= (nth 2 file-attr) uid)
3272 (= (nth 3 file-attr) gid))
3273 (setq need-chown nil))))
3275 ;; Set the ownership.
3277 (tramp-set-file-uid-gid filename uid gid))
3278 (when (or (eq visit t) (null visit) (stringp visit))
3279 (tramp-message v 0 "Wrote %s" filename))
3280 (run-hooks 'tramp-handle-write-region-hook)))))
3282 (defvar tramp-vc-registered-file-names nil
3283 "List used to collect file names, which are checked during `vc-registered'.")
3285 ;; VC backends check for the existence of various different special
3286 ;; files. This is very time consuming, because every single check
3287 ;; requires a remote command (the file cache must be invalidated).
3288 ;; Therefore, we apply a kind of optimization. We install the file
3289 ;; name handler `tramp-vc-file-name-handler', which does nothing but
3290 ;; remembers all file names for which `file-exists-p' or
3291 ;; `file-readable-p' has been applied. A first run of `vc-registered'
3292 ;; is performed. Afterwards, a script is applied for all collected
3293 ;; file names, using just one remote command. The result of this
3294 ;; script is used to fill the file cache with actual values. Now we
3295 ;; can reset the file name handlers, and we make a second run of
3296 ;; `vc-registered', which returns the expected result without sending
3297 ;; any other remote command.
3298 (defun tramp-sh-handle-vc-registered (file)
3299 "Like `vc-registered' for Tramp files."
3300 (tramp-compat-with-temp-message ""
3301 (with-parsed-tramp-file-name file nil
3302 (with-tramp-progress-reporter
3303 v 3 (format "Checking `vc-registered' for %s" file)
3305 ;; There could be new files, created by the vc backend. We
3306 ;; cannot reuse the old cache entries, therefore.
3307 (let (tramp-vc-registered-file-names
3308 (remote-file-name-inhibit-cache (current-time))
3309 (file-name-handler-alist
3310 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
3312 ;; Here we collect only file names, which need an operation.
3313 (ignore-errors (tramp-run-real-handler 'vc-registered (list file)))
3314 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
3316 ;; Send just one command, in order to fill the cache.
3317 (when tramp-vc-registered-file-names
3318 (tramp-maybe-send-script
3320 (format tramp-vc-registered-read-file-names
3321 (tramp-get-file-exists-command v)
3322 (format "%s -r" (tramp-get-test-command v)))
3323 "tramp_vc_registered_read_file_names")
3327 (tramp-send-command-and-read
3330 "tramp_vc_registered_read_file_names <<'EOF'\n%s\nEOF\n"
3331 (mapconcat 'tramp-shell-quote-argument
3332 tramp-vc-registered-file-names
3335 (tramp-set-file-property
3336 v (car elt) (cadr elt) (cadr (cdr elt))))))
3338 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3339 ;; calls shall be answered from the file cache. We unset
3340 ;; `process-file-side-effects' in order to keep the cache when
3341 ;; `process-file' calls appear.
3342 (let (process-file-side-effects)
3343 (tramp-run-real-handler 'vc-registered (list file)))))))
3345 ;;;###tramp-autoload
3346 (defun tramp-sh-file-name-handler (operation &rest args)
3347 "Invoke remote-shell Tramp file name handler.
3348 Fall back to normal file name handler if no Tramp handler exists."
3349 (when (and tramp-locked (not tramp-locker))
3350 (setq tramp-locked nil)
3351 (signal 'file-error (list "Forbidden reentrant call of Tramp")))
3352 (let ((tl tramp-locked))
3355 (setq tramp-locked t)
3356 (let ((tramp-locker t))
3358 (let ((fn (assoc operation tramp-sh-file-name-handler-alist)))
3360 (apply (cdr fn) args)
3361 (tramp-run-real-handler operation args))))))
3362 (setq tramp-locked tl))))
3364 (defun tramp-vc-file-name-handler (operation &rest args)
3365 "Invoke special file name handler, which collects files to be handled."
3368 (tramp-replace-environment-variables
3369 (apply 'tramp-file-name-for-operation operation args)))
3370 (fn (assoc operation tramp-sh-file-name-handler-alist)))
3371 (with-parsed-tramp-file-name filename nil
3373 ;; That's what we want: file names, for which checks are
3374 ;; applied. We assume that VC uses only `file-exists-p' and
3375 ;; `file-readable-p' checks; otherwise we must extend the
3376 ;; list. We do not perform any action, but return nil, in
3377 ;; order to keep `vc-registered' running.
3378 ((and fn (memq operation '(file-exists-p file-readable-p)))
3379 (add-to-list 'tramp-vc-registered-file-names localname 'append)
3381 ;; `process-file' and `start-file-process' shall be ignored.
3382 ((and fn (eq operation 'process-file) 0))
3383 ((and fn (eq operation 'start-file-process) nil))
3384 ;; Tramp file name handlers like `expand-file-name'. They
3386 (fn (save-match-data (apply (cdr fn) args)))
3387 ;; Default file name handlers, we don't care.
3388 (t (tramp-run-real-handler operation args)))))))
3390 ;;; Internal Functions:
3392 (defun tramp-maybe-send-script (vec script name)
3393 "Define in remote shell function NAME implemented as SCRIPT.
3394 Only send the definition if it has not already been done."
3395 ;; We cannot let-bind (tramp-get-connection-process vec) because it
3397 (let ((scripts (tramp-get-connection-property
3398 (tramp-get-connection-process vec) "scripts" nil)))
3399 (unless (member name scripts)
3400 (with-tramp-progress-reporter vec 5 (format "Sending script `%s'" name)
3401 ;; The script could contain a call of Perl. This is masked with `%s'.
3402 (tramp-barf-unless-okay
3404 (format "%s () {\n%s\n}" name
3405 (format script (tramp-get-remote-perl vec)))
3406 "Script %s sending failed" name)
3407 (tramp-set-connection-property
3408 (tramp-get-connection-process vec) "scripts" (cons name scripts))))))
3410 (defun tramp-set-auto-save ()
3411 (when (and ;; ange-ftp has its own auto-save mechanism
3412 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
3413 'tramp-sh-file-name-handler)
3415 (auto-save-mode 1)))
3416 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
3417 (add-hook 'tramp-unload-hook
3419 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
3421 (defun tramp-run-test (switch filename)
3422 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3423 Returns the exit code of the `test' program."
3424 (with-parsed-tramp-file-name filename nil
3425 (tramp-send-command-and-check
3429 (tramp-get-test-command v)
3431 (tramp-shell-quote-argument localname)))))
3433 (defun tramp-run-test2 (format-string file1 file2)
3434 "Run `test'-like program on the remote system, given FILE1, FILE2.
3435 FORMAT-STRING contains the program name, switches, and place holders.
3436 Returns the exit code of the `test' program. Barfs if the methods,
3437 hosts, or files, disagree."
3438 (unless (tramp-equal-remote file1 file2)
3439 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
3442 "tramp-run-test2 only implemented for same method, user, host")))
3443 (with-parsed-tramp-file-name file1 v1
3444 (with-parsed-tramp-file-name file1 v2
3445 (tramp-send-command-and-check
3447 (format format-string
3448 (tramp-shell-quote-argument v1-localname)
3449 (tramp-shell-quote-argument v2-localname))))))
3451 (defun tramp-find-executable
3452 (vec progname dirlist &optional ignore-tilde ignore-path)
3453 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3454 First arg VEC specifies the connection, PROGNAME is the program
3455 to search for, and DIRLIST gives the list of directories to
3456 search. If IGNORE-TILDE is non-nil, directory names starting
3457 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3460 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3462 This function expects to be in the right *tramp* buffer."
3463 (with-current-buffer (tramp-get-connection-buffer vec)
3465 ;; Check whether the executable is in $PATH. "which(1)" does not
3466 ;; report always a correct error code; therefore we check the
3467 ;; number of words it returns.
3469 (tramp-send-command vec (format "which \\%s | wc -w" progname))
3470 (goto-char (point-min))
3471 (if (looking-at "^\\s-*1$")
3472 (setq result (concat "\\" progname))))
3475 ;; Remove all ~/foo directories from dirlist. In XEmacs,
3476 ;; `remove' is in CL, and we want to avoid CL dependencies.
3479 (setq d (car dirlist))
3480 (setq dirlist (cdr dirlist))
3481 (unless (char-equal ?~ (aref d 0))
3482 (setq newdl (cons d newdl))))
3483 (setq dirlist (nreverse newdl))))
3486 (format (concat "while read d; "
3487 "do if test -x $d/%s -a -f $d/%s; "
3488 "then echo tramp_executable $d/%s; "
3489 "break; fi; done <<'EOF'\n"
3491 progname progname progname (mapconcat 'identity dirlist "\n")))
3492 (goto-char (point-max))
3493 (when (search-backward "tramp_executable " nil t)
3494 (skip-chars-forward "^ ")
3495 (skip-chars-forward " ")
3496 (setq result (buffer-substring (point) (point-at-eol)))))
3499 (defun tramp-set-remote-path (vec)
3500 "Sets the remote environment PATH to existing directories.
3501 I.e., for each directory in `tramp-remote-path', it is tested
3502 whether it exists and if so, it is added to the environment
3504 (tramp-message vec 5 (format "Setting $PATH environment variable"))
3506 vec (format "PATH=%s; export PATH"
3507 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
3509 ;; ------------------------------------------------------------
3510 ;; -- Communication with external shell --
3511 ;; ------------------------------------------------------------
3513 (defun tramp-find-file-exists-command (vec)
3514 "Find a command on the remote host for checking if a file exists.
3515 Here, we are looking for a command which has zero exit status if the
3516 file exists and nonzero exit status otherwise."
3517 (let ((existing "/")
3519 (tramp-shell-quote-argument "/ this file does not exist "))
3521 ;; The algorithm is as follows: we try a list of several commands.
3522 ;; For each command, we first run `$cmd /' -- this should return
3523 ;; true, as the root directory always exists. And then we run
3524 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
3525 ;; does not exist. This should return false. We use the first
3526 ;; command we find that seems to work.
3527 ;; The list of commands to try is as follows:
3528 ;; `ls -d' This works on most systems, but NetBSD 1.4
3529 ;; has a bug: `ls' always returns zero exit
3530 ;; status, even for files which don't exist.
3531 ;; `test -e' Some Bourne shells have a `test' builtin
3532 ;; which does not know the `-e' option.
3533 ;; `/bin/test -e' For those, the `test' binary on disk normally
3534 ;; provides the option. Alas, the binary
3535 ;; is sometimes `/bin/test' and sometimes it's
3537 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3540 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
3541 (tramp-send-command-and-check
3542 vec (format "%s %s" result existing))
3543 (not (tramp-send-command-and-check
3544 vec (format "%s %s" result nonexistent)))))
3546 (and (setq result "/bin/test -e")
3547 (tramp-send-command-and-check
3548 vec (format "%s %s" result existing))
3549 (not (tramp-send-command-and-check
3550 vec (format "%s %s" result nonexistent)))))
3552 (and (setq result "/usr/bin/test -e")
3553 (tramp-send-command-and-check
3554 vec (format "%s %s" result existing))
3555 (not (tramp-send-command-and-check
3556 vec (format "%s %s" result nonexistent)))))
3558 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
3559 (tramp-send-command-and-check
3560 vec (format "%s %s" result existing))
3561 (not (tramp-send-command-and-check
3562 vec (format "%s %s" result nonexistent))))))
3564 vec 'file-error "Couldn't find command to check if file exists"))
3567 (defun tramp-open-shell (vec shell)
3568 "Opens shell SHELL."
3569 (with-tramp-progress-reporter
3570 vec 5 (format "Opening remote shell `%s'" shell)
3571 ;; Find arguments for this shell.
3572 (let ((tramp-end-of-output tramp-initial-end-of-output)
3573 (alist tramp-sh-extra-args)
3575 (while (and alist (null extra-args))
3576 (setq item (pop alist))
3577 (when (string-match (car item) shell)
3578 (setq extra-args (cdr item))))
3581 "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s"
3582 (tramp-shell-quote-argument tramp-end-of-output)
3583 shell (or extra-args ""))
3585 (tramp-set-connection-property
3586 (tramp-get-connection-process vec) "remote-shell" shell)
3589 vec (format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output)) t)
3590 (tramp-send-command vec "PS2=''" t)
3591 (tramp-send-command vec "PS3=''" t)
3592 (tramp-send-command vec "PROMPT_COMMAND=''" t)))
3594 (defun tramp-find-shell (vec)
3595 "Opens a shell on the remote host which groks tilde expansion."
3596 (with-current-buffer (tramp-get-buffer vec)
3597 (let ((default-shell
3599 (tramp-get-connection-property
3600 (tramp-get-connection-process vec) "remote-shell" nil)
3601 (tramp-get-method-parameter
3602 (tramp-file-name-method vec) 'tramp-remote-shell)))
3605 (with-tramp-connection-property vec "remote-shell"
3606 ;; CCC: "root" does not exist always, see QNAP 459.
3607 ;; Which check could we apply instead?
3608 (tramp-send-command vec "echo ~root" t)
3609 (if (or (string-match "^~root$" (buffer-string))
3610 ;; The default shell (ksh93) of OpenSolaris and
3611 ;; Solaris is buggy. We've got reports for
3612 ;; "SunOS 5.10" and "SunOS 5.11" so far.
3613 (string-match (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3614 (tramp-get-connection-property
3617 (or (tramp-find-executable
3618 vec "bash" (tramp-get-remote-path vec) t t)
3619 (tramp-find-executable
3620 vec "ksh" (tramp-get-remote-path vec) t t)
3621 ;; Maybe it works at least for some other commands.
3627 "Couldn't find a remote shell which groks tilde "
3628 "expansion, using `%s'")
3633 ;; Open a new shell if needed.
3634 (unless (string-equal shell default-shell)
3636 vec 5 "Starting remote shell `%s' for tilde expansion" shell)
3637 (tramp-open-shell vec shell))
3639 ;; Busyboxes tend to behave strange. We check for the existence.
3640 (with-tramp-connection-property vec "busybox"
3641 (tramp-send-command vec (format "%s --version" shell) t)
3642 (let ((case-fold-search t))
3643 (and (string-match "busybox" (buffer-string)) t))))))
3645 ;; Utility functions.
3647 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
3648 "Wait for shell prompt and barf if none appears.
3649 Looks at process PROC to see if a shell prompt appears in TIMEOUT
3650 seconds. If not, it produces an error message with the given ERROR-ARGS."
3652 (tramp-wait-for-regexp
3655 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
3656 (apply 'tramp-error-with-buffer nil proc 'file-error error-args)))
3658 (defun tramp-open-connection-setup-interactive-shell (proc vec)
3659 "Set up an interactive shell.
3660 Mainly sets the prompt and the echo correctly. PROC is the shell
3661 process to set up. VEC specifies the connection."
3662 (let ((tramp-end-of-output tramp-initial-end-of-output))
3663 ;; It is useful to set the prompt in the following command because
3664 ;; some people have a setting for $PS1 which /bin/sh doesn't know
3665 ;; about and thus /bin/sh will display a strange prompt. For
3666 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
3667 ;; display the current working directory but /bin/sh will display
3668 ;; a dollar sign. The following command line sets $PS1 to a sane
3669 ;; value, and works under Bourne-ish shells as well as csh-like
3670 ;; shells. Daniel Pittman reports that the unusual positioning of
3671 ;; the single quotes makes it work under `rc', too. We also unset
3672 ;; the variable $ENV because that is read by some sh
3673 ;; implementations (eg, bash when called as sh) on startup; this
3674 ;; way, we avoid the startup file clobbering $PS1. $PROMPT_COMMAND
3675 ;; is another way to set the prompt in /bin/bash, it must be
3676 ;; discarded as well.
3679 (or (tramp-get-connection-property vec "remote-shell" nil)
3680 (tramp-get-method-parameter
3681 (tramp-file-name-method vec) 'tramp-remote-shell)))
3684 (tramp-message vec 5 "Setting up remote shell environment")
3685 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
3686 ;; Check whether the echo has really been disabled. Some
3687 ;; implementations, like busybox of embedded GNU/Linux, don't
3688 ;; support disabling.
3689 (tramp-send-command vec "echo foo" t)
3690 (with-current-buffer (process-buffer proc)
3691 (goto-char (point-min))
3692 (when (looking-at "echo foo")
3693 (tramp-set-connection-property proc "remote-echo" t)
3694 (tramp-message vec 5 "Remote echo still on. Ok.")
3695 ;; Make sure backspaces and their echo are enabled and no line
3696 ;; width magic interferes with them.
3697 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
3699 (tramp-message vec 5 "Setting shell prompt")
3701 vec (format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output)) t)
3702 (tramp-send-command vec "PS2=''" t)
3703 (tramp-send-command vec "PS3=''" t)
3704 (tramp-send-command vec "PROMPT_COMMAND=''" t)
3706 ;; Try to set up the coding system correctly.
3707 ;; CCC this can't be the right way to do it. Hm.
3708 (tramp-message vec 5 "Determining coding system")
3709 (tramp-send-command vec "echo foo ; echo bar" t)
3710 (with-current-buffer (process-buffer proc)
3711 (goto-char (point-min))
3712 (if (featurep 'mule)
3713 ;; Use MULE to select the right EOL convention for communicating
3714 ;; with the process.
3715 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc)
3716 (cons 'undecided 'undecided)))
3717 cs-decode cs-encode)
3718 (when (symbolp cs) (setq cs (cons cs cs)))
3719 (setq cs-decode (car cs))
3720 (setq cs-encode (cdr cs))
3721 (unless cs-decode (setq cs-decode 'undecided))
3722 (unless cs-encode (setq cs-encode 'undecided))
3723 (setq cs-encode (tramp-compat-coding-system-change-eol-conversion
3725 (when (search-forward "\r" nil t)
3726 (setq cs-decode (tramp-compat-coding-system-change-eol-conversion
3728 (tramp-compat-funcall
3729 'set-buffer-process-coding-system cs-decode cs-encode)
3731 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
3732 ;; Look for ^M and do something useful if found.
3733 (when (search-forward "\r" nil t)
3734 ;; We have found a ^M but cannot frob the process coding system
3735 ;; because we're running on a non-MULE Emacs. Let's try
3737 (tramp-send-command vec "stty -onlcr" t))))
3739 (tramp-send-command vec "set +o vi +o emacs" t)
3741 ;; Check whether the output of "uname -sr" has been changed. If
3742 ;; yes, this is a strong indication that we must expire all
3743 ;; connection properties. We start again with
3744 ;; `tramp-maybe-open-connection', it will be caught there.
3745 (tramp-message vec 5 "Checking system information")
3746 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
3748 (tramp-set-connection-property
3750 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
3751 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
3755 "Connection reset, because remote host changed from `%s' to `%s'"
3756 old-uname new-uname)
3757 (throw 'uname-changed (tramp-maybe-open-connection vec))))
3759 ;; Check whether the remote host suffers from buggy
3760 ;; `send-process-string'. This is known for FreeBSD (see comment in
3761 ;; `send_process', file process.c). I've tested sending 624 bytes
3762 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
3763 ;; this host type is detected locally. It cannot handle remote
3765 (with-tramp-connection-property proc "chunksize"
3767 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
3771 vec 5 "Checking remote host type for `send-process-string' bug")
3773 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
3776 ;; Set remote PATH variable.
3777 (tramp-set-remote-path vec)
3779 ;; Search for a good shell before searching for a command which
3780 ;; checks if a file exists. This is done because Tramp wants to use
3781 ;; "test foo; echo $?" to check if various conditions hold, and
3782 ;; there are buggy /bin/sh implementations which don't execute the
3783 ;; "echo $?" part if the "test" part has an error. In particular,
3784 ;; the OpenSolaris /bin/sh is a problem. There are also other
3785 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
3786 ;; in function declarations, or changing HISTFILE in place.
3787 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
3789 (tramp-find-shell vec)
3791 ;; Disable unexpected output.
3792 (tramp-send-command vec "mesg n; biff n" t)
3794 ;; IRIX64 bash expands "!" even when in single quotes. This
3795 ;; destroys our shell functions, we must disable it. See
3796 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
3797 (when (string-match "^IRIX64" (tramp-get-connection-property vec "uname" ""))
3798 (tramp-send-command vec "set +H" t))
3800 ;; On BSD-like systems, ?\t is expanded to spaces. Suppress this.
3801 (when (string-match "BSD\\|Darwin"
3802 (tramp-get-connection-property vec "uname" ""))
3803 (tramp-send-command vec "stty -oxtabs" t))
3805 ;; Set `remote-tty' process property.
3806 (let ((tty (tramp-send-command-and-read vec "echo \\\"`tty`\\\"" 'noerror)))
3807 (unless (zerop (length tty))
3808 (tramp-compat-process-put proc 'remote-tty tty)))
3810 ;; Dump stty settings in the traces.
3811 (when (>= tramp-verbose 9)
3812 (tramp-send-command vec "stty -a" t))
3814 ;; Set the environment.
3815 (tramp-message vec 5 "Setting default environment")
3817 (let ((env (copy-sequence tramp-remote-process-environment))
3820 (setq item (tramp-compat-split-string (car env) "="))
3821 (setcdr item (mapconcat 'identity (cdr item) "="))
3822 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
3824 vec (format "%s=%s; export %s" (car item) (cdr item) (car item)) t)
3825 (push (car item) unset))
3826 (setq env (cdr env)))
3829 vec (format "unset %s" (mapconcat 'identity unset " ")) t))))
3831 ;; CCC: We should either implement a Perl version of base64 encoding
3832 ;; and decoding. Then we just use that in the last item. The other
3833 ;; alternative is to use the Perl version of UU encoding. But then
3834 ;; we need a Lisp version of uuencode.
3836 ;; Old text from documentation of tramp-methods:
3837 ;; Using a uuencode/uudecode inline method is discouraged, please use one
3838 ;; of the base64 methods instead since base64 encoding is much more
3839 ;; reliable and the commands are more standardized between the different
3840 ;; Unix versions. But if you can't use base64 for some reason, please
3841 ;; note that the default uudecode command does not work well for some
3842 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
3843 ;; the following command for uudecode:
3845 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
3847 ;; For Irix, no solution is known yet.
3849 (autoload 'uudecode-decode-region "uudecode")
3851 (defconst tramp-local-coding-commands
3852 '((b64 base64-encode-region base64-decode-region)
3853 (uu tramp-uuencode-region uudecode-decode-region)
3855 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
3856 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
3857 "List of local coding commands for inline transfer.
3858 Each item is a list that looks like this:
3860 \(FORMAT ENCODING DECODING\)
3862 FORMAT is symbol describing the encoding/decoding format. It can be
3863 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3865 ENCODING and DECODING can be strings, giving commands, or symbols,
3866 giving functions. If they are strings, then they can contain
3867 the \"%s\" format specifier. If that specifier is present, the input
3868 filename will be put into the command line at that spot. If the
3869 specifier is not present, the input should be read from standard
3872 If they are functions, they will be called with two arguments, start
3873 and end of region, and are expected to replace the region contents
3874 with the encoded or decoded results, respectively.")
3876 (defconst tramp-remote-coding-commands
3877 '((b64 "base64" "base64 -d -i")
3878 ;; "-i" is more robust with older base64 from GNU coreutils.
3879 ;; However, I don't know whether all base64 versions do supports
3881 (b64 "base64" "base64 -d")
3882 (b64 "mimencode -b" "mimencode -u -b")
3883 (b64 "mmencode -b" "mmencode -u -b")
3884 (b64 "recode data..base64" "recode base64..data")
3885 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
3886 (b64 tramp-perl-encode tramp-perl-decode)
3887 (uu "uuencode xxx" "uudecode -o /dev/stdout" "test -c /dev/stdout")
3888 (uu "uuencode xxx" "uudecode -o -")
3889 (uu "uuencode xxx" "uudecode -p")
3890 (uu "uuencode xxx" tramp-uudecode)
3892 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
3893 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
3894 "List of remote coding commands for inline transfer.
3895 Each item is a list that looks like this:
3897 \(FORMAT ENCODING DECODING [TEST]\)
3899 FORMAT is symbol describing the encoding/decoding format. It can be
3900 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3902 ENCODING and DECODING can be strings, giving commands, or symbols,
3903 giving variables. If they are strings, then they can contain
3904 the \"%s\" format specifier. If that specifier is present, the input
3905 filename will be put into the command line at that spot. If the
3906 specifier is not present, the input should be read from standard
3909 If they are variables, this variable is a string containing a Perl
3910 implementation for this functionality. This Perl program will be transferred
3911 to the remote host, and it is available as shell function with the same name.
3913 The optional TEST command can be used for further tests, whether
3914 ENCODING and DECODING are applicable.")
3916 (defun tramp-find-inline-encoding (vec)
3917 "Find an inline transfer encoding that works.
3918 Goes through the list `tramp-local-coding-commands' and
3919 `tramp-remote-coding-commands'."
3921 (let ((local-commands tramp-local-coding-commands)
3923 (p (tramp-get-connection-process vec))
3924 loc-enc loc-dec rem-enc rem-dec rem-test litem ritem found)
3925 (while (and local-commands (not found))
3926 (setq litem (pop local-commands))
3927 (catch 'wont-work-local
3928 (let ((format (nth 0 litem))
3929 (remote-commands tramp-remote-coding-commands))
3930 (setq loc-enc (nth 1 litem))
3931 (setq loc-dec (nth 2 litem))
3932 ;; If the local encoder or decoder is a string, the
3933 ;; corresponding command has to work locally.
3934 (if (not (stringp loc-enc))
3936 vec 5 "Checking local encoding function `%s'" loc-enc)
3938 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
3939 (unless (zerop (tramp-call-local-coding-command
3941 (throw 'wont-work-local nil)))
3942 (if (not (stringp loc-dec))
3944 vec 5 "Checking local decoding function `%s'" loc-dec)
3946 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
3947 (unless (zerop (tramp-call-local-coding-command
3949 (throw 'wont-work-local nil)))
3950 ;; Search for remote coding commands with the same format
3951 (while (and remote-commands (not found))
3952 (setq ritem (pop remote-commands))
3953 (catch 'wont-work-remote
3954 (when (equal format (nth 0 ritem))
3955 (setq rem-enc (nth 1 ritem))
3956 (setq rem-dec (nth 2 ritem))
3957 (setq rem-test (nth 3 ritem))
3958 ;; Check the remote test command if exists.
3959 (when (stringp rem-test)
3961 vec 5 "Checking remote test command `%s'" rem-test)
3962 (unless (tramp-send-command-and-check vec rem-test t)
3963 (throw 'wont-work-remote nil)))
3964 ;; Check if remote encoding and decoding commands can be
3965 ;; called remotely with null input and output. This makes
3966 ;; sure there are no syntax errors and the command is really
3967 ;; found. Note that we do not redirect stdout to /dev/null,
3968 ;; for two reasons: when checking the decoding command, we
3969 ;; actually check the output it gives. And also, when
3970 ;; redirecting "mimencode" output to /dev/null, then as root
3971 ;; it might change the permissions of /dev/null!
3972 (when (not (stringp rem-enc))
3973 (let ((name (symbol-name rem-enc)))
3974 (while (string-match (regexp-quote "-") name)
3975 (setq name (replace-match "_" nil t name)))
3976 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
3977 (setq rem-enc name)))
3980 "Checking remote encoding command `%s' for sanity" rem-enc)
3981 (unless (tramp-send-command-and-check
3982 vec (format "%s </dev/null" rem-enc) t)
3983 (throw 'wont-work-remote nil))
3985 (when (not (stringp rem-dec))
3986 (let ((name (symbol-name rem-dec)))
3987 (while (string-match (regexp-quote "-") name)
3988 (setq name (replace-match "_" nil t name)))
3989 (tramp-maybe-send-script vec (symbol-value rem-dec) name)
3990 (setq rem-dec name)))
3993 "Checking remote decoding command `%s' for sanity" rem-dec)
3994 (unless (tramp-send-command-and-check
3996 (format "echo %s | %s | %s" magic rem-enc rem-dec)
3998 (throw 'wont-work-remote nil))
4000 (with-current-buffer (tramp-get-buffer vec)
4001 (goto-char (point-min))
4002 (unless (looking-at (regexp-quote magic))
4003 (throw 'wont-work-remote nil)))
4005 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
4006 (setq rem-enc (nth 1 ritem))
4007 (setq rem-dec (nth 2 ritem))
4008 (setq found t)))))))
4010 ;; Did we find something?
4013 vec 'file-error "Couldn't find an inline transfer encoding"))
4015 ;; Set connection properties. Since the commands are risky (due
4016 ;; to output direction), we cache them in the process cache.
4017 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
4018 (tramp-set-connection-property p "local-encoding" loc-enc)
4019 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
4020 (tramp-set-connection-property p "local-decoding" loc-dec)
4021 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
4022 (tramp-set-connection-property p "remote-encoding" rem-enc)
4023 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
4024 (tramp-set-connection-property p "remote-decoding" rem-dec))))
4026 (defun tramp-call-local-coding-command (cmd input output)
4027 "Call the local encoding or decoding command.
4028 If CMD contains \"%s\", provide input file INPUT there in command.
4029 Otherwise, INPUT is passed via standard input.
4030 INPUT can also be nil which means `/dev/null'.
4031 OUTPUT can be a string (which specifies a filename), or t (which
4032 means standard output and thus the current buffer), or nil (which
4034 (tramp-compat-call-process
4035 tramp-encoding-shell
4036 (when (and input (not (string-match "%s" cmd))) input)
4037 (if (eq output t) t nil)
4039 tramp-encoding-command-switch
4041 (if (string-match "%s" cmd) (format cmd input) cmd)
4042 (if (stringp output) (concat "> " output) ""))))
4044 (defconst tramp-inline-compress-commands
4045 '(("gzip" "gzip -d")
4046 ("bzip2" "bzip2 -d")
4048 ("compress" "compress -d"))
4049 "List of compress and decompress commands for inline transfer.
4050 Each item is a list that looks like this:
4052 \(COMPRESS DECOMPRESS\)
4054 COMPRESS or DECOMPRESS are strings with the respective commands.")
4056 (defun tramp-find-inline-compress (vec)
4057 "Find an inline transfer compress command that works.
4058 Goes through the list `tramp-inline-compress-commands'."
4060 (let ((commands tramp-inline-compress-commands)
4062 (p (tramp-get-connection-process vec))
4063 item compress decompress found)
4064 (while (and commands (not found))
4066 (setq item (pop commands)
4067 compress (nth 0 item)
4068 decompress (nth 1 item))
4071 "Checking local compress command `%s', `%s' for sanity"
4072 compress decompress)
4075 (tramp-call-local-coding-command
4077 ;; Windows shells need the program file name after
4078 ;; the pipe symbol be quoted if they use forward
4079 ;; slashes as directory separators.
4080 (if (memq system-type '(windows-nt))
4081 "echo %s | \"%s\" | \"%s\""
4082 "echo %s | %s | %s")
4083 magic compress decompress) nil nil))
4087 "Checking remote compress command `%s', `%s' for sanity"
4088 compress decompress)
4089 (unless (tramp-send-command-and-check
4090 vec (format "echo %s | %s | %s" magic compress decompress) t)
4094 ;; Did we find something?
4097 ;; Set connection properties. Since the commands are
4098 ;; risky (due to output direction), we cache them in the
4101 vec 5 "Using inline transfer compress command `%s'" compress)
4102 (tramp-set-connection-property p "inline-compress" compress)
4104 vec 5 "Using inline transfer decompress command `%s'" decompress)
4105 (tramp-set-connection-property p "inline-decompress" decompress))
4107 (tramp-set-connection-property p "inline-compress" nil)
4108 (tramp-set-connection-property p "inline-decompress" nil)
4110 vec 2 "Couldn't find an inline transfer compress command")))))
4112 (defvar tramp-gw-tunnel-method)
4113 (defvar tramp-gw-socks-method)
4115 (defun tramp-compute-multi-hops (vec)
4116 "Expands VEC according to `tramp-default-proxies-alist'.
4117 Gateway hops are already opened."
4118 (let ((target-alist `(,vec))
4119 (hops (or (tramp-file-name-hop vec) ""))
4123 ;; Ad-hoc proxy definitions.
4124 (dolist (proxy (reverse (split-string hops tramp-postfix-hop-regexp 'omit)))
4125 (let ((user (tramp-file-name-user item))
4126 (host (tramp-file-name-host item))
4128 tramp-prefix-format proxy tramp-postfix-host-format)))
4130 vec 5 "Add proxy (\"%s\" \"%s\" \"%s\")"
4131 (and (stringp host) (regexp-quote host))
4132 (and (stringp user) (regexp-quote user))
4136 'tramp-default-proxies-alist
4137 (list (and (stringp host) (regexp-quote host))
4138 (and (stringp user) (regexp-quote user))
4140 (setq item (tramp-dissect-file-name proxy))))
4141 ;; Save the new value.
4142 (when (and hops tramp-save-ad-hoc-proxies)
4143 (customize-save-variable
4144 'tramp-default-proxies-alist tramp-default-proxies-alist))
4146 ;; Look for proxy hosts to be passed.
4147 (setq choices tramp-default-proxies-alist)
4149 (setq item (pop choices)
4150 proxy (eval (nth 2 item)))
4153 (string-match (or (eval (nth 0 item)) "")
4154 (or (tramp-file-name-host (car target-alist)) ""))
4156 (string-match (or (eval (nth 1 item)) "")
4157 (or (tramp-file-name-user (car target-alist)) "")))
4159 ;; No more hops needed.
4161 ;; Replace placeholders.
4166 ?u (or (tramp-file-name-user (car target-alist)) "")
4167 ?h (or (tramp-file-name-host (car target-alist)) ""))))
4168 (with-parsed-tramp-file-name proxy l
4170 (add-to-list 'target-alist l)
4171 ;; Start next search.
4172 (setq choices tramp-default-proxies-alist)))))
4175 (when (and (boundp 'tramp-gw-tunnel-method) (boundp 'tramp-gw-socks-method)
4178 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method)
4179 (tramp-file-name-method (car target-alist))))
4180 (let ((gw (pop target-alist))
4181 (hop (pop target-alist)))
4182 ;; Is the method prepared for gateways?
4183 (unless (tramp-file-name-port hop)
4186 "Connection `%s' is not supported for gateway access." hop))
4187 ;; Open the gateway connection.
4191 (tramp-file-name-method hop) (tramp-file-name-user hop)
4192 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop) nil nil))
4193 ;; For the password prompt, we need the correct values.
4194 ;; Therefore, we must remember the gateway vector. But we
4195 ;; cannot do it as connection property, because it shouldn't
4196 ;; be persistent. And we have no started process yet either.
4197 (tramp-set-file-property (car target-alist) "" "gateway" hop)))
4199 ;; Foreign and out-of-band methods are not supported for multi-hops.
4200 (when (cdr target-alist)
4201 (setq choices target-alist)
4203 (setq item (pop choices))
4207 (tramp-get-method-parameter
4208 (tramp-file-name-method item) 'tramp-login-program))
4209 (tramp-get-method-parameter
4210 (tramp-file-name-method item) 'tramp-copy-program))
4213 "Method `%s' is not supported for multi-hops."
4214 (tramp-file-name-method item)))))
4216 ;; In case the host name is not used for the remote shell
4217 ;; command, the user could be misguided by applying a random
4219 (let* ((v (car target-alist))
4220 (method (tramp-file-name-method v))
4221 (host (tramp-file-name-host v)))
4224 ;; There are multi-hops.
4226 ;; The host name is used for the remote shell command.
4228 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
4229 ;; The host is local. We cannot use `tramp-local-host-p'
4230 ;; here, because it opens a connection as well.
4231 (string-match tramp-local-host-regexp host))
4234 "Host `%s' looks like a remote host, `%s' can only use the local host"
4240 (defvar tramp-current-connection nil
4241 "Last connection timestamp.")
4243 (defun tramp-maybe-open-connection (vec)
4244 "Maybe open a connection VEC.
4245 Does not do anything if a connection is already open, but re-opens the
4246 connection if a previous connection has died for some reason."
4247 (catch 'uname-changed
4248 (let ((p (tramp-get-connection-process vec))
4249 (process-name (tramp-get-connection-property vec "process-name" nil))
4250 (process-environment (copy-sequence process-environment))
4251 (pos (with-current-buffer (tramp-get-connection-buffer vec) (point))))
4253 ;; If Tramp opens the same connection within a short time frame,
4254 ;; there is a problem. We shall signal this.
4255 (unless (or (and p (processp p) (memq (process-status p) '(run open)))
4256 (not (equal (butlast (append vec nil))
4257 (car tramp-current-connection)))
4259 (current-time) (cdr tramp-current-connection))
4260 (or tramp-connection-min-time-diff 0)))
4261 (throw 'suppress 'suppress))
4263 ;; If too much time has passed since last command was sent, look
4264 ;; whether process is still alive. If it isn't, kill it. When
4265 ;; using ssh, it can sometimes happen that the remote end has
4266 ;; hung up but the local ssh client doesn't recognize this until
4267 ;; it tries to send some data to the remote end. So that's why
4268 ;; we try to send a command from time to time, then look again
4269 ;; whether the process is really alive.
4271 (when (and (> (tramp-time-diff
4273 (tramp-get-connection-property
4274 p "last-cmd-time" '(0 0 0)))
4276 p (processp p) (memq (process-status p) '(run open)))
4277 (tramp-send-command vec "echo are you awake" t t)
4278 (unless (and (memq (process-status p) '(run open))
4279 (tramp-wait-for-output p 10))
4280 ;; The error will be caught locally.
4281 (tramp-error vec 'file-error "Awake did fail")))
4286 ;; New connection must be opened.
4288 (unless (and p (processp p) (memq (process-status p) '(run open)))
4290 ;; We call `tramp-get-buffer' in order to get a debug
4291 ;; buffer for messages from the beginning.
4292 (tramp-get-buffer vec)
4294 ;; If `non-essential' is non-nil, don't reopen a new connection.
4295 (when (and (boundp 'non-essential) (symbol-value 'non-essential))
4296 (throw 'non-essential 'non-essential))
4298 (with-tramp-progress-reporter
4300 (if (zerop (length (tramp-file-name-user vec)))
4301 (format "Opening connection for %s using %s"
4302 (tramp-file-name-host vec)
4303 (tramp-file-name-method vec))
4304 (format "Opening connection for %s@%s using %s"
4305 (tramp-file-name-user vec)
4306 (tramp-file-name-host vec)
4307 (tramp-file-name-method vec)))
4309 ;; Start new process.
4310 (when (and p (processp p))
4312 (setenv "TERM" tramp-terminal-type)
4313 (setenv "LC_ALL" "C")
4314 (setenv "PROMPT_COMMAND")
4315 (setenv "PS1" tramp-initial-end-of-output)
4316 (let* ((target-alist (tramp-compute-multi-hops vec))
4317 ;; We will apply `tramp-ssh-controlmaster-options'
4318 ;; only for the first hop.
4319 (options tramp-ssh-controlmaster-options)
4320 (process-connection-type tramp-process-connection-type)
4321 (process-adaptive-read-buffering nil)
4322 (coding-system-for-read nil)
4323 ;; This must be done in order to avoid our file
4325 (p (let ((default-directory
4326 (tramp-compat-temporary-file-directory)))
4329 (tramp-get-connection-name vec)
4330 (tramp-get-connection-buffer vec)
4331 (if tramp-encoding-command-interactive
4332 (list tramp-encoding-shell
4333 tramp-encoding-command-interactive)
4334 (list tramp-encoding-shell))))))
4336 ;; Set sentinel and query flag.
4337 (tramp-set-connection-property p "vector" vec)
4338 (set-process-sentinel p 'tramp-process-sentinel)
4339 (tramp-compat-set-process-query-on-exit-flag p nil)
4340 (setq tramp-current-connection
4341 (cons (butlast (append vec nil)) (current-time))
4342 tramp-current-host (system-name))
4345 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
4347 ;; Check whether process is alive.
4348 (tramp-barf-if-no-shell-prompt
4350 "Couldn't find local shell prompt %s" tramp-encoding-shell)
4352 ;; Now do all the connections as specified.
4354 (let* ((hop (car target-alist))
4355 (l-method (tramp-file-name-method hop))
4356 (l-user (tramp-file-name-user hop))
4357 (l-host (tramp-file-name-host hop))
4360 (tramp-get-method-parameter
4361 l-method 'tramp-login-program))
4363 (tramp-get-method-parameter
4364 l-method 'tramp-login-args))
4366 (tramp-get-method-parameter
4367 l-method 'tramp-async-args))
4369 (tramp-get-method-parameter l-method 'tramp-gw-args))
4370 (gw (tramp-get-file-property hop "" "gateway" nil))
4371 (g-method (and gw (tramp-file-name-method gw)))
4372 (g-user (and gw (tramp-file-name-user gw)))
4373 (g-host (and gw (tramp-file-name-real-host gw)))
4374 (command login-program)
4375 ;; We don't create the temporary file. In
4376 ;; fact, it is just a prefix for the
4377 ;; ControlPath option of ssh; the real
4378 ;; temporary file has another name, and it is
4379 ;; created and protected by ssh. It is also
4380 ;; removed by ssh when the connection is
4381 ;; closed. The temporary file name is cached
4382 ;; in the main connection process, therefore
4383 ;; we cannot use `tramp-get-connection-process'.
4385 (with-tramp-connection-property
4386 (get-process (tramp-buffer-name vec)) "temp-file"
4389 tramp-temp-name-prefix
4390 (tramp-compat-temporary-file-directory)))))
4393 ;; Add arguments for asynchronous processes.
4394 (when (and process-name async-args)
4395 (setq login-args (append async-args login-args)))
4397 ;; Add gateway arguments if necessary.
4398 (when (and gw gw-args)
4399 (setq login-args (append gw-args login-args)))
4401 ;; Check for port number. Until now, there's no
4402 ;; need for handling like method, user, host.
4403 (when (string-match tramp-host-with-port-regexp l-host)
4404 (setq l-port (match-string 2 l-host)
4405 l-host (match-string 1 l-host)))
4407 ;; Check, whether there is a restricted shell.
4408 (dolist (elt tramp-restricted-shell-hosts-alist)
4409 (when (string-match elt tramp-current-host)
4412 ;; Set variables for computing the prompt for
4413 ;; reading password. They can also be derived
4415 (setq tramp-current-method (or g-method l-method)
4416 tramp-current-user (or g-user l-user)
4417 tramp-current-host (or g-host l-host))
4419 ;; Replace login-args place holders.
4421 l-host (or l-host "")
4422 l-user (or l-user "")
4423 l-port (or l-port "")
4424 spec (format-spec-make ?t tmpfile)
4425 options (format-spec options spec)
4426 spec (format-spec-make
4427 ?h l-host ?u l-user ?p l-port ?c options)
4430 ;; We do not want to see the trailing local
4431 ;; prompt in `start-file-process'.
4432 (unless r-shell "exec ")
4436 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
4437 (unless (member "" x) (mapconcat 'identity x " ")))
4439 ;; Local shell could be a Windows COMSPEC. It
4440 ;; doesn't know the ";" syntax, but we must exit
4441 ;; always for `start-file-process'. It could
4442 ;; also be a restricted shell, which does not
4444 (when r-shell " && exit || exit")))
4446 ;; Send the command.
4447 (tramp-message vec 3 "Sending command `%s'" command)
4448 (tramp-send-command vec command t t)
4449 (tramp-process-actions
4450 p vec pos tramp-actions-before-shell 60)
4452 vec 3 "Found remote shell prompt on `%s'" l-host))
4455 target-alist (cdr target-alist)))
4457 ;; Make initial shell settings.
4458 (tramp-open-connection-setup-interactive-shell p vec))))
4460 ;; When the user did interrupt, we must cleanup.
4463 ;; Propagate the quit signal.
4464 (signal (car err) (cdr err)))))))
4466 (defun tramp-send-command (vec command &optional neveropen nooutput)
4467 "Send the COMMAND to connection VEC.
4468 Erases temporary buffer before sending the command. If optional
4469 arg NEVEROPEN is non-nil, never try to open the connection. This
4470 is meant to be used from `tramp-maybe-open-connection' only. The
4471 function waits for output unless NOOUTPUT is set."
4472 (unless neveropen (tramp-maybe-open-connection vec))
4473 (let ((p (tramp-get-connection-process vec)))
4474 (when (tramp-get-connection-property p "remote-echo" nil)
4475 ;; We mark the command string that it can be erased in the output buffer.
4476 (tramp-set-connection-property p "check-remote-echo" t)
4477 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
4478 (when (and (string-match "<<'EOF'" command)
4479 (not (tramp-get-connection-property vec "busybox" nil)))
4480 ;; Unset $PS1 when using here documents, in order to avoid
4481 ;; multiple prompts.
4482 (setq command (concat "(PS1= ; " command "\n)")))
4483 ;; Send the command.
4484 (tramp-message vec 6 "%s" command)
4485 (tramp-send-string vec command)
4486 (unless nooutput (tramp-wait-for-output p))))
4488 (defun tramp-wait-for-output (proc &optional timeout)
4489 "Wait for output from remote command."
4490 (unless (buffer-live-p (process-buffer proc))
4491 (delete-process proc)
4492 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
4493 (with-current-buffer (process-buffer proc)
4494 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
4495 ;; be leading escape sequences, which must be ignored.
4496 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
4497 ;; Sometimes, the commands do not return a newline but a
4498 ;; null byte before the shell prompt, for example "git
4499 ;; ls-files -c -z ...".
4500 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
4501 (found (tramp-wait-for-regexp proc timeout regexp1)))
4503 (let (buffer-read-only)
4504 ;; A simple-minded busybox has sent " ^H" sequences.
4506 (goto-char (point-min))
4507 (when (re-search-forward "^\\(.\b\\)+$" (point-at-eol) t)
4509 (delete-region (point-min) (point)))
4510 ;; Delete the prompt.
4511 (goto-char (point-max))
4512 (re-search-backward regexp nil t)
4513 (delete-region (point) (point-max)))
4517 "[[Remote prompt `%s' not found in %d secs]]"
4518 tramp-end-of-output timeout)
4521 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
4522 ;; Return value is whether end-of-output sentinel was found.
4525 (defun tramp-send-command-and-check
4526 (vec command &optional subshell dont-suppress-err)
4527 "Run COMMAND and check its exit status.
4528 Sends `echo $?' along with the COMMAND for checking the exit status. If
4529 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
4531 If the optional argument SUBSHELL is non-nil, the command is
4532 executed in a subshell, ie surrounded by parentheses. If
4533 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
4536 (concat (if subshell "( " "")
4538 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
4539 "echo tramp_exit_status $?"
4540 (if subshell " )" "")))
4541 (with-current-buffer (tramp-get-connection-buffer vec)
4542 (goto-char (point-max))
4543 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
4545 vec 'file-error "Couldn't find exit status of `%s'" command))
4546 (skip-chars-forward "^ ")
4548 (zerop (read (current-buffer)))
4549 (let (buffer-read-only)
4550 (delete-region (match-beginning 0) (point-max))))))
4552 (defun tramp-barf-unless-okay (vec command fmt &rest args)
4553 "Run COMMAND, check exit status, throw error if exit status not okay.
4554 Similar to `tramp-send-command-and-check' but accepts two more arguments
4555 FMT and ARGS which are passed to `error'."
4556 (or (tramp-send-command-and-check vec command)
4557 (apply 'tramp-error vec 'file-error fmt args)))
4559 (defun tramp-send-command-and-read (vec command &optional noerror)
4560 "Run COMMAND and return the output, which must be a Lisp expression.
4561 In case there is no valid Lisp expression and NOERROR is nil, it
4564 (tramp-send-command-and-check vec command)
4565 (tramp-barf-unless-okay
4566 vec command "`%s' returns with error" command))
4567 (with-current-buffer (tramp-get-connection-buffer vec)
4568 ;; Read the expression.
4569 (goto-char (point-min))
4571 (prog1 (read (current-buffer))
4573 (when (re-search-forward "\\S-" (point-at-eol) t)
4575 (error (unless noerror
4578 "`%s' does not return a valid Lisp expression: `%s'"
4579 command (buffer-string))))))))
4581 (defun tramp-convert-file-attributes (vec attr)
4582 "Convert `file-attributes' ATTR generated by perl script, stat or ls.
4583 Convert file mode bits to string and set virtual device number.
4586 ;; Remove color escape sequences from symlink.
4587 (when (stringp (car attr))
4588 (while (string-match tramp-color-escape-sequence-regexp (car attr))
4589 (setcar attr (replace-match "" nil nil (car attr)))))
4590 ;; Convert uid and gid. Use -1 as indication of unusable value.
4591 (when (and (numberp (nth 2 attr)) (< (nth 2 attr) 0))
4592 (setcar (nthcdr 2 attr) -1))
4593 (when (and (floatp (nth 2 attr))
4594 (<= (nth 2 attr) (tramp-compat-most-positive-fixnum)))
4595 (setcar (nthcdr 2 attr) (round (nth 2 attr))))
4596 (when (and (numberp (nth 3 attr)) (< (nth 3 attr) 0))
4597 (setcar (nthcdr 3 attr) -1))
4598 (when (and (floatp (nth 3 attr))
4599 (<= (nth 3 attr) (tramp-compat-most-positive-fixnum)))
4600 (setcar (nthcdr 3 attr) (round (nth 3 attr))))
4601 ;; Convert last access time.
4602 (unless (listp (nth 4 attr))
4603 (setcar (nthcdr 4 attr)
4604 (list (floor (nth 4 attr) 65536)
4605 (floor (mod (nth 4 attr) 65536)))))
4606 ;; Convert last modification time.
4607 (unless (listp (nth 5 attr))
4608 (setcar (nthcdr 5 attr)
4609 (list (floor (nth 5 attr) 65536)
4610 (floor (mod (nth 5 attr) 65536)))))
4611 ;; Convert last status change time.
4612 (unless (listp (nth 6 attr))
4613 (setcar (nthcdr 6 attr)
4614 (list (floor (nth 6 attr) 65536)
4615 (floor (mod (nth 6 attr) 65536)))))
4616 ;; Convert file size.
4617 (when (< (nth 7 attr) 0)
4618 (setcar (nthcdr 7 attr) -1))
4619 (when (and (floatp (nth 7 attr))
4620 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
4621 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
4622 ;; Convert file mode bits to string.
4623 (unless (stringp (nth 8 attr))
4624 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
4625 (when (stringp (car attr))
4626 (aset (nth 8 attr) 0 ?l)))
4627 ;; Convert directory indication bit.
4628 (when (string-match "^d" (nth 8 attr))
4630 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
4631 (when (consp (car attr))
4632 (if (and (stringp (caar attr))
4633 (string-match ".+ -> .\\(.+\\)." (caar attr)))
4634 (setcar attr (match-string 1 (caar attr)))
4636 ;; Set file's gid change bit.
4637 (setcar (nthcdr 9 attr)
4638 (if (numberp (nth 3 attr))
4639 (not (= (nth 3 attr)
4640 (tramp-get-remote-gid vec 'integer)))
4643 (tramp-get-remote-gid vec 'string)))))
4645 (unless (listp (nth 10 attr))
4646 (setcar (nthcdr 10 attr)
4648 (cons (floor (nth 10 attr) 65536)
4649 (floor (mod (nth 10 attr) 65536)))
4650 ;; Inodes can be incredible huge. We must hide this.
4651 (error (tramp-get-inode vec)))))
4652 ;; Set virtual device number.
4653 (setcar (nthcdr 11 attr)
4654 (tramp-get-device vec))
4657 (defun tramp-check-cached-permissions (vec access)
4658 "Check `file-attributes' caches for VEC.
4659 Return t if according to the cache access type ACCESS is known to
4665 ((eq ?x access) 3))))
4666 (dolist (suffix '("string" "integer") result)
4672 (tramp-get-file-property
4673 vec (tramp-file-name-localname vec)
4674 (concat "file-attributes-" suffix) nil))
4676 (tramp-get-connection-property
4677 vec (concat "uid-" suffix) nil))
4679 (tramp-get-connection-property
4680 vec (concat "gid-" suffix) nil)))
4685 (eq t (car file-attr))
4686 (null (car file-attr)))
4688 ;; World accessible.
4689 (eq access (aref (nth 8 file-attr) (+ offset 6)))
4690 ;; User accessible and owned by user.
4692 (eq access (aref (nth 8 file-attr) offset))
4693 (equal remote-uid (nth 2 file-attr)))
4694 ;; Group accessible and owned by user's
4697 (eq access (aref (nth 8 file-attr) (+ offset 3)))
4698 (equal remote-gid (nth 3 file-attr)))))))))))
4700 (defun tramp-file-mode-from-int (mode)
4701 "Turn an integer representing a file mode into an ls(1)-like string."
4703 (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
4704 (user (logand (lsh mode -6) 7))
4705 (group (logand (lsh mode -3) 7))
4706 (other (logand (lsh mode -0) 7))
4707 (suid (> (logand (lsh mode -9) 4) 0))
4708 (sgid (> (logand (lsh mode -9) 2) 0))
4709 (sticky (> (logand (lsh mode -9) 1) 0)))
4710 (setq user (tramp-file-mode-permissions user suid "s"))
4711 (setq group (tramp-file-mode-permissions group sgid "s"))
4712 (setq other (tramp-file-mode-permissions other sticky "t"))
4713 (concat type user group other)))
4715 (defun tramp-file-mode-permissions (perm suid suid-text)
4716 "Convert a permission bitset into a string.
4717 This is used internally by `tramp-file-mode-from-int'."
4718 (let ((r (> (logand perm 4) 0))
4719 (w (> (logand perm 2) 0))
4720 (x (> (logand perm 1) 0)))
4721 (concat (or (and r "r") "-")
4722 (or (and w "w") "-")
4723 (or (and suid x suid-text) ; suid, execute
4724 (and suid (upcase suid-text)) ; suid, !execute
4725 (and x "x") "-")))) ; !suid
4727 (defun tramp-shell-case-fold (string)
4728 "Converts STRING to shell glob pattern which ignores case."
4731 (if (equal (downcase c) (upcase c))
4733 (format "[%c%c]" (downcase c) (upcase c))))
4737 (defun tramp-make-copy-program-file-name (vec)
4738 "Create a file name suitable to be passed to `rcp' and workalikes."
4739 (let ((user (tramp-file-name-user vec))
4740 (host (tramp-file-name-real-host vec))
4741 (localname (tramp-shell-quote-argument
4742 (tramp-file-name-localname vec))))
4743 (if (not (zerop (length user)))
4744 (format "%s@%s:%s" user host localname)
4745 (format "%s:%s" host localname))))
4747 (defun tramp-method-out-of-band-p (vec size)
4748 "Return t if this is an out-of-band method, nil otherwise."
4750 ;; It shall be an out-of-band method.
4751 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
4752 ;; There must be a size, otherwise the file doesn't exist.
4754 ;; Either the file size is large enough, or (in rare cases) there
4755 ;; does not exist a remote encoding.
4756 (or (null tramp-copy-size-limit)
4757 (> size tramp-copy-size-limit)
4758 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
4760 ;; Variables local to connection.
4762 (defun tramp-get-remote-path (vec)
4763 (with-tramp-connection-property
4764 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
4765 ;; cache the result for the session only. Otherwise, the result
4766 ;; is cached persistently.
4767 (if (memq 'tramp-own-remote-path tramp-remote-path)
4768 (tramp-get-connection-process vec)
4771 (let* ((remote-path (copy-tree tramp-remote-path))
4772 (elt1 (memq 'tramp-default-remote-path remote-path))
4773 (elt2 (memq 'tramp-own-remote-path remote-path))
4774 (default-remote-path
4777 (tramp-send-command-and-read
4778 vec "echo \\\"`getconf PATH 2>/dev/null`\\\"" 'noerror)
4779 ;; Default if "getconf" is not available.
4783 "`getconf PATH' not successful, using default value \"%s\"."
4789 (tramp-send-command-and-read vec "echo \\\"$PATH\\\"")
4792 vec 3 "$PATH not set, ignoring `tramp-own-remote-path'.")
4795 ;; Replace place holder `tramp-default-remote-path'.
4799 (tramp-compat-split-string default-remote-path ":")
4801 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
4803 ;; Replace place holder `tramp-own-remote-path'.
4807 (tramp-compat-split-string own-remote-path ":")
4809 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
4811 ;; Remove double entries.
4812 (setq elt1 remote-path)
4814 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
4816 (setq elt1 (cdr elt1)))
4818 ;; Remove non-existing directories.
4826 (tramp-make-tramp-file-name
4827 (tramp-file-name-method vec)
4828 (tramp-file-name-user vec)
4829 (tramp-file-name-host vec)
4834 (defun tramp-get-ls-command (vec)
4835 (with-tramp-connection-property vec "ls"
4836 (tramp-message vec 5 "Finding a suitable `ls' command")
4839 (dolist (cmd '("ls" "gnuls" "gls"))
4840 (let ((dl (tramp-get-remote-path vec))
4842 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
4843 ;; Check parameters. On busybox, "ls" output coloring is
4844 ;; enabled by default sometimes. So we try to disable it
4845 ;; when possible. $LS_COLORING is not supported there.
4846 ;; Some "ls" versions are sensible wrt the order of
4847 ;; arguments, they fail when "-al" is after the
4848 ;; "--color=never" argument (for example on FreeBSD).
4849 (when (tramp-send-command-and-check
4850 vec (format "%s -lnd /" result))
4851 (when (tramp-send-command-and-check
4853 "%s --color=never -al /dev/null" result))
4854 (setq result (concat result " --color=never")))
4855 (throw 'ls-found result))
4856 (setq dl (cdr dl))))))
4857 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
4859 (defun tramp-get-ls-command-with-dired (vec)
4861 (with-tramp-connection-property vec "ls-dired"
4862 (tramp-message vec 5 "Checking, whether `ls --dired' works")
4863 ;; Some "ls" versions are sensible wrt the order of arguments,
4864 ;; they fail when "-al" is after the "--dired" argument (for
4865 ;; example on FreeBSD).
4866 (tramp-send-command-and-check
4867 vec (format "%s --dired -al /dev/null" (tramp-get-ls-command vec))))))
4869 (defun tramp-get-test-command (vec)
4870 (with-tramp-connection-property vec "test"
4871 (tramp-message vec 5 "Finding a suitable `test' command")
4872 (if (tramp-send-command-and-check vec "test 0")
4874 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
4876 (defun tramp-get-test-nt-command (vec)
4877 ;; Does `test A -nt B' work? Use abominable `find' construct if it
4878 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
4879 ;; for otherwise the shell crashes.
4880 (with-tramp-connection-property vec "test-nt"
4884 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
4885 (with-current-buffer (tramp-get-buffer vec)
4886 (goto-char (point-min))
4887 (when (looking-at (regexp-quote tramp-end-of-output))
4888 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
4893 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
4894 (tramp-get-test-command vec)))
4895 "tramp_test_nt %s %s"))))
4897 (defun tramp-get-file-exists-command (vec)
4898 (with-tramp-connection-property vec "file-exists"
4899 (tramp-message vec 5 "Finding command to check if file exists")
4900 (tramp-find-file-exists-command vec)))
4902 (defun tramp-get-remote-ln (vec)
4903 (with-tramp-connection-property vec "ln"
4904 (tramp-message vec 5 "Finding a suitable `ln' command")
4905 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
4907 (defun tramp-get-remote-perl (vec)
4908 (with-tramp-connection-property vec "perl"
4909 (tramp-message vec 5 "Finding a suitable `perl' command")
4911 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
4912 (tramp-find-executable
4913 vec "perl" (tramp-get-remote-path vec)))))
4914 ;; We must check also for some Perl modules.
4916 (with-tramp-connection-property vec "perl-file-spec"
4917 (tramp-send-command-and-check
4918 vec (format "%s -e 'use File::Spec;'" result)))
4919 (with-tramp-connection-property vec "perl-cwd-realpath"
4920 (tramp-send-command-and-check
4921 vec (format "%s -e 'use Cwd \"realpath\";'" result))))
4924 (defun tramp-get-remote-stat (vec)
4925 (with-tramp-connection-property vec "stat"
4926 (tramp-message vec 5 "Finding a suitable `stat' command")
4927 (let ((result (tramp-find-executable
4928 vec "stat" (tramp-get-remote-path vec)))
4930 ;; Check whether stat(1) returns usable syntax. "%s" does not
4931 ;; work on older AIX systems.
4934 (tramp-send-command-and-read
4935 vec (format "%s -c '(\"%%N\" %%s)' /" result) 'noerror))
4936 (unless (and (listp tmp) (stringp (car tmp))
4937 (string-match "^./.$" (car tmp))
4938 (integerp (cadr tmp)))
4942 (defun tramp-get-remote-readlink (vec)
4943 (with-tramp-connection-property vec "readlink"
4944 (tramp-message vec 5 "Finding a suitable `readlink' command")
4945 (let ((result (tramp-find-executable
4946 vec "readlink" (tramp-get-remote-path vec))))
4948 (tramp-send-command-and-check
4949 vec (format "%s --canonicalize-missing /" result)))
4952 (defun tramp-get-remote-trash (vec)
4953 (with-tramp-connection-property vec "trash"
4954 (tramp-message vec 5 "Finding a suitable `trash' command")
4955 (tramp-find-executable vec "trash" (tramp-get-remote-path vec))))
4957 (defun tramp-get-remote-id (vec)
4958 (with-tramp-connection-property vec "id"
4959 (tramp-message vec 5 "Finding POSIX `id' command")
4962 (let ((dl (tramp-get-remote-path vec))
4964 (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
4965 ;; Check POSIX parameter.
4966 (when (tramp-send-command-and-check vec (format "%s -u" result))
4967 (throw 'id-found result))
4968 (setq dl (cdr dl)))))
4969 (tramp-error vec 'file-error "Couldn't find a POSIX `id' command"))))
4971 (defun tramp-get-remote-uid (vec id-format)
4972 (with-tramp-connection-property vec (format "uid-%s" id-format)
4973 (let ((res (tramp-send-command-and-read
4975 (format "%s -u%s %s"
4976 (tramp-get-remote-id vec)
4977 (if (equal id-format 'integer) "" "n")
4978 (if (equal id-format 'integer)
4979 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
4980 ;; The command might not always return a number.
4981 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
4983 (defun tramp-get-remote-gid (vec id-format)
4984 (with-tramp-connection-property vec (format "gid-%s" id-format)
4985 (let ((res (tramp-send-command-and-read
4987 (format "%s -g%s %s"
4988 (tramp-get-remote-id vec)
4989 (if (equal id-format 'integer) "" "n")
4990 (if (equal id-format 'integer)
4991 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
4992 ;; The command might not always return a number.
4993 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
4995 (defun tramp-get-local-uid (id-format)
4996 (if (equal id-format 'integer) (user-uid) (user-login-name)))
4998 (defun tramp-get-local-gid (id-format)
4999 (if (and (fboundp 'group-gid) (equal id-format 'integer))
5000 (tramp-compat-funcall 'group-gid)
5001 (nth 3 (tramp-compat-file-attributes "~/" id-format))))
5003 ;; Some predefined connection properties.
5004 (defun tramp-get-inline-compress (vec prop size)
5005 "Return the compress command related to PROP.
5006 PROP is either `inline-compress' or `inline-decompress'. SIZE is
5007 the length of the file to be compressed.
5009 If no corresponding command is found, nil is returned."
5010 (when (and (integerp tramp-inline-compress-start-size)
5011 (> size tramp-inline-compress-start-size))
5012 (with-tramp-connection-property (tramp-get-connection-process vec) prop
5013 (tramp-find-inline-compress vec)
5014 (tramp-get-connection-property
5015 (tramp-get-connection-process vec) prop nil))))
5017 (defun tramp-get-inline-coding (vec prop size)
5018 "Return the coding command related to PROP.
5019 PROP is either `remote-encoding', `remote-decoding',
5020 `local-encoding' or `local-decoding'.
5022 SIZE is the length of the file to be coded. Depending on SIZE,
5023 compression might be applied.
5025 If no corresponding command is found, nil is returned.
5026 Otherwise, either a string is returned which contains a `%s' mark
5027 to be used for the respective input or output file; or a Lisp
5028 function cell is returned to be applied on a buffer."
5029 ;; We must catch the errors, because we want to return `nil', when
5030 ;; no inline coding is found.
5033 (with-tramp-connection-property
5034 (tramp-get-connection-process vec) prop
5035 (tramp-find-inline-encoding vec)
5036 (tramp-get-connection-property
5037 (tramp-get-connection-process vec) prop nil)))
5038 (prop1 (if (string-match "encoding" prop)
5039 "inline-compress" "inline-decompress"))
5041 ;; The connection property might have been cached. So we must
5042 ;; send the script to the remote side - maybe.
5043 (when (and coding (symbolp coding) (string-match "remote" prop))
5044 (let ((name (symbol-name coding)))
5045 (while (string-match (regexp-quote "-") name)
5046 (setq name (replace-match "_" nil t name)))
5047 (tramp-maybe-send-script vec (symbol-value coding) name)
5048 (setq coding name)))
5050 ;; Check for the `compress' command.
5051 (setq compress (tramp-get-inline-compress vec prop1 size))
5052 ;; Return the value.
5054 ((and compress (symbolp coding))
5055 (if (string-match "decompress" prop1)
5058 (let ((coding-system-for-write 'binary)
5059 (coding-system-for-read 'binary))
5061 'call-process-region (point-min) (point-max)
5062 (car (split-string ,compress)) t t nil
5063 (cdr (split-string ,compress)))))
5065 (let ((coding-system-for-write 'binary)
5066 (coding-system-for-read 'binary))
5068 'call-process-region beg end
5069 (car (split-string ,compress)) t t nil
5070 (cdr (split-string ,compress))))
5071 (,coding (point-min) (point-max)))))
5074 ((and compress (string-match "decoding" prop))
5076 ;; Windows shells need the program file name after
5077 ;; the pipe symbol be quoted if they use forward
5078 ;; slashes as directory separators.
5079 (if (and (string-match "local" prop)
5080 (memq system-type '(windows-nt)))
5081 "(%s | \"%s\" >%%s)"
5086 ;; Windows shells need the program file name after
5087 ;; the pipe symbol be quoted if they use forward
5088 ;; slashes as directory separators.
5089 (if (and (string-match "local" prop)
5090 (memq system-type '(windows-nt)))
5091 "(%s <%%s | \"%s\")"
5094 ((string-match "decoding" prop)
5095 (format "%s >%%s" coding))
5097 (format "%s <%%s" coding)))))))
5099 (add-hook 'tramp-unload-hook
5101 (unload-feature 'tramp-sh 'force)))
5107 ;; * Don't use globbing for directories with many files, as this is
5108 ;; likely to produce long command lines, and some shells choke on
5109 ;; long command lines.
5110 ;; * Make it work for different encodings, and for different file name
5111 ;; encodings, too. (Daniel Pittman)
5112 ;; * Don't search for perl5 and perl. Instead, only search for perl and
5113 ;; then look if it's the right version (with `perl -v').
5114 ;; * When editing a remote CVS controlled file as a different user, VC
5115 ;; gets confused about the file locking status. Try to find out why
5116 ;; the workaround doesn't work.
5117 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5118 ;; until the last but one hop via `start-file-process'. Apply it
5119 ;; also for ftp and smb.
5120 ;; * WIBNI if we had a command "trampclient"? If I was editing in
5121 ;; some shell with root privileges, it would be nice if I could
5123 ;; trampclient filename.c
5124 ;; as an editor, and the _current_ shell would connect to an Emacs
5125 ;; server and would be used in an existing non-privileged Emacs
5126 ;; session for doing the editing in question.
5127 ;; That way, I need not tell Emacs my password again and be afraid
5128 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
5129 ;; once display a just typed password in the context of a keyboard
5130 ;; sequence prompt for a question immediately following in a shell
5131 ;; script run within Emacs -- nasty).
5132 ;; And if I have some ssh session running to a different computer,
5133 ;; having the possibility of passing a local file there to a local
5134 ;; Emacs session (in case I can arrange for a connection back) would
5136 ;; Likely the corresponding Tramp server should not allow the
5137 ;; equivalent of the emacsclient -eval option in order to make this
5138 ;; reasonably unproblematic. And maybe trampclient should have some
5139 ;; way of passing credentials, like by using an SSL socket or
5140 ;; something. (David Kastrup)
5141 ;; * Reconnect directly to a compliant shell without first going
5142 ;; through the user's default shell. (Pete Forman)
5143 ;; * How can I interrupt the remote process with a signal
5144 ;; (interrupt-process seems not to work)? (Markus Triska)
5145 ;; * Avoid the local shell entirely for starting remote processes. If
5146 ;; so, I think even a signal, when delivered directly to the local
5147 ;; SSH instance, would correctly be propagated to the remote process
5148 ;; automatically; possibly SSH would have to be started with
5149 ;; "-t". (Markus Triska)
5150 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5151 ;; isn't on the remote host. (Mark A. Hershberger)
5152 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
5153 ;; * Optimize out-of-band copying when both methods are scp-like (not
5155 ;; * Keep a second connection open for out-of-band methods like scp or
5157 ;; * Try telnet+curl as new method. It might be useful for busybox,
5158 ;; without built-in uuencode/uudecode.
5160 ;;; tramp-sh.el ends here