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"))
226 (tramp-connection-timeout 10)))
228 (add-to-list 'tramp-methods
230 (tramp-login-program "sudo")
231 (tramp-login-args (("-u" "%u") ("-s") ("-H") ("-p" "Password:")))
232 (tramp-remote-shell "/bin/sh")
233 (tramp-remote-shell-args ("-c"))
234 (tramp-connection-timeout 10)))
236 (add-to-list 'tramp-methods
238 (tramp-login-program "ksu")
239 (tramp-login-args (("%u") ("-q")))
240 (tramp-remote-shell "/bin/sh")
241 (tramp-remote-shell-args ("-c"))
242 (tramp-connection-timeout 10)))
244 (add-to-list 'tramp-methods
246 (tramp-login-program "krlogin")
247 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
248 (tramp-remote-shell "/bin/sh")
249 (tramp-remote-shell-args ("-c"))))
251 (add-to-list 'tramp-methods
253 (tramp-login-program "plink")
254 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
255 (tramp-remote-shell "/bin/sh")
256 (tramp-remote-shell-args ("-c"))
257 (tramp-default-port 22)))
259 (add-to-list 'tramp-methods
261 (tramp-login-program "plink")
262 ;; ("%h") must be a single element, see
263 ;; `tramp-compute-multi-hops'.
264 (tramp-login-args (("-load") ("%h") ("-t")
266 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
268 tramp-initial-end-of-output
))
270 (tramp-remote-shell "/bin/sh")
271 (tramp-remote-shell-args ("-c"))))
273 (add-to-list 'tramp-methods
275 (tramp-login-program "plink")
276 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
277 (tramp-remote-shell "/bin/sh")
278 (tramp-remote-shell-args ("-c"))
279 (tramp-copy-program "pscp")
280 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-scp") ("-p" "%k")
282 (tramp-copy-keep-date t
)
283 (tramp-copy-recursive t
)
284 (tramp-default-port 22)))
286 (add-to-list 'tramp-methods
288 (tramp-login-program "plink")
289 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
290 (tramp-remote-shell "/bin/sh")
291 (tramp-remote-shell-args ("-c"))
292 (tramp-copy-program "pscp")
293 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-sftp") ("-p" "%k")
295 (tramp-copy-keep-date t
)
296 (tramp-copy-recursive t
)))
298 (add-to-list 'tramp-methods
300 (tramp-login-program "fsh")
301 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
302 (tramp-remote-shell "/bin/sh")
303 (tramp-remote-shell-args ("-i") ("-c"))
304 (tramp-copy-program "fcp")
305 (tramp-copy-args (("-p" "%k")))
306 (tramp-copy-keep-date t
)))
309 (add-to-list 'tramp-default-method-alist
310 `(,tramp-local-host-regexp
"\\`root\\'" "su"))
313 (add-to-list 'tramp-default-user-alist
314 `(,(concat "\\`" (regexp-opt '("su" "sudo" "ksu")) "\\'")
316 ;; Do not add "ssh" based methods, otherwise ~/.ssh/config would be ignored.
317 ;; Do not add "plink" based methods, they ask interactively for the user.
319 (add-to-list 'tramp-default-user-alist
322 (regexp-opt '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
324 nil
,(user-login-name)))
327 (defconst tramp-completion-function-alist-rsh
328 '((tramp-parse-rhosts "/etc/hosts.equiv")
329 (tramp-parse-rhosts "~/.rhosts"))
330 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
333 (defconst tramp-completion-function-alist-ssh
334 '((tramp-parse-rhosts "/etc/hosts.equiv")
335 (tramp-parse-rhosts "/etc/shosts.equiv")
336 (tramp-parse-shosts "/etc/ssh_known_hosts")
337 (tramp-parse-sconfig "/etc/ssh_config")
338 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
339 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
340 (tramp-parse-rhosts "~/.rhosts")
341 (tramp-parse-rhosts "~/.shosts")
342 (tramp-parse-shosts "~/.ssh/known_hosts")
343 (tramp-parse-sconfig "~/.ssh/config")
344 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
345 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
346 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
349 (defconst tramp-completion-function-alist-telnet
350 '((tramp-parse-hosts "/etc/hosts"))
351 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
354 (defconst tramp-completion-function-alist-su
355 '((tramp-parse-passwd "/etc/passwd"))
356 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
359 (defconst tramp-completion-function-alist-putty
361 ,(if (memq system-type
'(windows-nt))
362 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"
363 "~/.putty/sessions")))
364 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty sessions.")
367 (eval-after-load 'tramp
369 (tramp-set-completion-function "rcp" tramp-completion-function-alist-rsh
)
370 (tramp-set-completion-function "remcp" tramp-completion-function-alist-rsh
)
371 (tramp-set-completion-function "scp" tramp-completion-function-alist-ssh
)
372 (tramp-set-completion-function "scpx" tramp-completion-function-alist-ssh
)
373 (tramp-set-completion-function "sftp" tramp-completion-function-alist-ssh
)
374 (tramp-set-completion-function "rsync" tramp-completion-function-alist-ssh
)
375 (tramp-set-completion-function "rsh" tramp-completion-function-alist-rsh
)
376 (tramp-set-completion-function "remsh" tramp-completion-function-alist-rsh
)
377 (tramp-set-completion-function "ssh" tramp-completion-function-alist-ssh
)
378 (tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh
)
379 (tramp-set-completion-function
380 "telnet" tramp-completion-function-alist-telnet
)
381 (tramp-set-completion-function "su" tramp-completion-function-alist-su
)
382 (tramp-set-completion-function "sudo" tramp-completion-function-alist-su
)
383 (tramp-set-completion-function "ksu" tramp-completion-function-alist-su
)
384 (tramp-set-completion-function
385 "krlogin" tramp-completion-function-alist-rsh
)
386 (tramp-set-completion-function "plink" tramp-completion-function-alist-ssh
)
387 (tramp-set-completion-function
388 "plinkx" tramp-completion-function-alist-putty
)
389 (tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh
)
390 (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh
)))
392 ;; "getconf PATH" yields:
393 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
394 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
395 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
396 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
399 (defcustom tramp-remote-path
400 '(tramp-default-remote-path "/bin" "/usr/bin" "/sbin" "/usr/sbin"
401 "/usr/local/bin" "/usr/local/sbin" "/local/bin" "/local/freeware/bin"
402 "/local/gnu/bin" "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin"
403 "/opt/bin" "/opt/sbin" "/opt/local/bin")
404 "List of directories to search for executables on remote host.
405 For every remote host, this variable will be set buffer local,
406 keeping the list of existing directories on that host.
408 You can use `~' in this list, but when searching for a shell which groks
409 tilde expansion, all directory names starting with `~' will be ignored.
411 `Default Directories' represent the list of directories given by
412 the command \"getconf PATH\". It is recommended to use this
413 entry on top of this list, because these are the default
414 directories for POSIX compatible commands. On remote hosts which
415 do not offer the getconf command (like cygwin), the value
416 \"/bin:/usr/bin\" is used instead of.
418 `Private Directories' are the settings of the $PATH environment,
419 as given in your `~/.profile'."
421 :type
'(repeat (choice
422 (const :tag
"Default Directories" tramp-default-remote-path
)
423 (const :tag
"Private Directories" tramp-own-remote-path
)
424 (string :tag
"Directory"))))
427 (defcustom tramp-remote-process-environment
428 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "TMOUT=0" "LC_ALL=C"
429 ,(format "TERM=%s" tramp-terminal-type
)
430 "EMACS=t" ;; Deprecated.
431 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version
)
432 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH=" "PAGER=\"\""
433 "autocorrect=" "correct=")
434 "List of environment variables to be set on the remote host.
436 Each element should be a string of the form ENVVARNAME=VALUE. An
437 entry ENVVARNAME= disables the corresponding environment variable,
438 which might have been set in the init files like ~/.profile.
440 Special handling is applied to the PATH environment, which should
441 not be set here. Instead, it should be set via `tramp-remote-path'."
443 :type
'(repeat string
))
445 (defcustom tramp-sh-extra-args
'(("/bash\\'" .
"-norc -noprofile"))
446 "Alist specifying extra arguments to pass to the remote shell.
447 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
448 matching the shell file name and ARGS is a string specifying the
451 This variable is only used when Tramp needs to start up another shell
452 for tilde expansion. The extra arguments should typically prevent the
453 shell from reading its init file."
455 ;; This might be the wrong way to test whether the widget type
456 ;; `alist' is available. Who knows the right way to test it?
457 :type
(if (get 'alist
'widget-type
)
458 '(alist :key-type string
:value-type string
)
459 '(repeat (cons string string
))))
461 (defconst tramp-actions-before-shell
462 '((tramp-login-prompt-regexp tramp-action-login
)
463 (tramp-password-prompt-regexp tramp-action-password
)
464 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
465 (shell-prompt-pattern tramp-action-succeed
)
466 (tramp-shell-prompt-pattern tramp-action-succeed
)
467 (tramp-yesno-prompt-regexp tramp-action-yesno
)
468 (tramp-yn-prompt-regexp tramp-action-yn
)
469 (tramp-terminal-prompt-regexp tramp-action-terminal
)
470 (tramp-process-alive-regexp tramp-action-process-alive
))
471 "List of pattern/action pairs.
472 Whenever a pattern matches, the corresponding action is performed.
473 Each item looks like (PATTERN ACTION).
475 The PATTERN should be a symbol, a variable. The value of this
476 variable gives the regular expression to search for. Note that the
477 regexp must match at the end of the buffer, \"\\'\" is implicitly
480 The ACTION should also be a symbol, but a function. When the
481 corresponding PATTERN matches, the ACTION function is called.")
483 (defconst tramp-actions-copy-out-of-band
484 '((tramp-password-prompt-regexp tramp-action-password
)
485 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
486 (tramp-copy-failed-regexp tramp-action-permission-denied
)
487 (tramp-process-alive-regexp tramp-action-out-of-band
))
488 "List of pattern/action pairs.
489 This list is used for copying/renaming with out-of-band methods.
491 See `tramp-actions-before-shell' for more info.")
493 (defconst tramp-uudecode
494 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
497 "Shell function to implement `uudecode' to standard output.
498 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
499 for this or `uudecode -p', but some systems don't, and for them
500 we have this shell function.")
502 (defconst tramp-perl-file-truename
505 use Cwd \"realpath\";
508 my ($volume, @dirs) = @_;
509 my $real = realpath(File::Spec->catpath(
510 $volume, File::Spec->catdir(@dirs), \"\"));
512 my ($vol, $dir) = File::Spec->splitpath($real, 1);
513 return ($vol, File::Spec->splitdir($dir));
516 my $last = pop(@dirs);
517 ($volume, @dirs) = recursive($volume, @dirs);
519 return ($volume, @dirs);
523 $result = realpath($ARGV[0]);
525 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
526 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
528 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
531 if ($ARGV[0] =~ /\\/$/) {
532 $result = $result . \"/\";
535 print \"\\\"$result\\\"\\n\";
536 ' \"$1\" 2>/dev/null"
537 "Perl script to produce output suitable for use with `file-truename'
538 on the remote file system.
539 Escape sequence %s is replaced with name of Perl binary.
540 This string is passed to `format', so percent characters need to be doubled.")
542 (defconst tramp-perl-file-name-all-completions
552 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
553 @files = readdir(d); closedir(d);
554 foreach $f (@files) {
555 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
556 if (-d \"$ARGV[0]/$f\") {
565 ' \"$1\" \"$2\" \"$3\" 2>/dev/null"
566 "Perl script to produce output suitable for use with
567 `file-name-all-completions' on the remote file system. Escape
568 sequence %s is replaced with name of Perl binary. This string is
569 passed to `format', so percent characters need to be doubled.")
571 ;; Perl script to implement `file-attributes' in a Lisp `read'able
572 ;; output. If you are hacking on this, note that you get *no* output
573 ;; unless this spits out a complete line, including the '\n' at the
575 ;; The device number is returned as "-1", because there will be a virtual
576 ;; device number set in `tramp-sh-handle-file-attributes'.
577 (defconst tramp-perl-file-attributes
579 @stat = lstat($ARGV[0]);
584 if (($stat[2] & 0170000) == 0120000)
586 $type = readlink($ARGV[0]);
587 $type = \"\\\"$type\\\"\";
589 elsif (($stat[2] & 0170000) == 040000)
597 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
598 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
600 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
605 $stat[8] >> 16 & 0xffff,
607 $stat[9] >> 16 & 0xffff,
609 $stat[10] >> 16 & 0xffff,
613 $stat[1] >> 16 & 0xffff,
615 );' \"$1\" \"$2\" 2>/dev/null"
616 "Perl script to produce output suitable for use with `file-attributes'
617 on the remote file system.
618 Escape sequence %s is replaced with name of Perl binary.
619 This string is passed to `format', so percent characters need to be doubled.")
621 (defconst tramp-perl-directory-files-and-attributes
623 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
624 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
625 @list = readdir(DIR);
629 for($i = 0; $i < $n; $i++)
631 $filename = $list[$i];
632 @stat = lstat($filename);
633 if (($stat[2] & 0170000) == 0120000)
635 $type = readlink($filename);
636 $type = \"\\\"$type\\\"\";
638 elsif (($stat[2] & 0170000) == 040000)
646 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
647 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
649 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
655 $stat[8] >> 16 & 0xffff,
657 $stat[9] >> 16 & 0xffff,
659 $stat[10] >> 16 & 0xffff,
663 $stat[1] >> 16 & 0xffff,
665 $stat[0] >> 16 & 0xffff,
668 printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
669 "Perl script implementing `directory-files-attributes' as Lisp `read'able
671 Escape sequence %s is replaced with name of Perl binary.
672 This string is passed to `format', so percent characters need to be doubled.")
674 ;; These two use base64 encoding.
675 (defconst tramp-perl-encode-with-module
676 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
677 "Perl program to use for encoding a file.
678 Escape sequence %s is replaced with name of Perl binary.
679 This string is passed to `format', so percent characters need to be doubled.
680 This implementation requires the MIME::Base64 Perl module to be installed
681 on the remote host.")
683 (defconst tramp-perl-decode-with-module
684 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
685 "Perl program to use for decoding a file.
686 Escape sequence %s is replaced with name of Perl binary.
687 This string is passed to `format', so percent characters need to be doubled.
688 This implementation requires the MIME::Base64 Perl module to be installed
689 on the remote host.")
691 (defconst tramp-perl-encode
693 # This script contributed by Juanma Barranquero <lektu@terra.es>.
694 # Copyright (C) 2002-2013 Free Software Foundation, Inc.
699 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
700 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
704 # We read in chunks of 54 bytes, to generate output lines
705 # of 72 chars (plus end of line)
706 while (read STDIN, $data, 54) {
709 # Only for the last chunk, and only if did not fill the last three-byte packet
711 my $mod = length($data) %% 3;
712 $pad = q(=) x (3 - $mod) if $mod;
715 # Not the fastest method, but it is simple: unpack to binary string, split
716 # by groups of 6 bits and convert back from binary to byte; then map into
717 # the translation table
721 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
725 "Perl program to use for encoding a file.
726 Escape sequence %s is replaced with name of Perl binary.
727 This string is passed to `format', so percent characters need to be doubled.")
729 (defconst tramp-perl-decode
731 # This script contributed by Juanma Barranquero <lektu@terra.es>.
732 # Copyright (C) 2002-2013 Free Software Foundation, Inc.
737 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
738 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
741 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
745 # We are going to accumulate into $pending to accept any line length
746 # (we do not check they are <= 76 chars as the RFC says)
749 while (my $data = <STDIN>) {
752 # If we find one or two =, we have reached the end and
753 # any following data is to be discarded
754 my $finished = $data =~ s/(==?).*/$1/;
757 my $len = length($pending);
758 my $chunk = substr($pending, 0, $len & ~3);
759 $pending = substr($pending, $len & ~3 + 1);
761 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
762 # split in 8-bit chunks and convert back to char.
765 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
769 "Perl program to use for decoding a file.
770 Escape sequence %s is replaced with name of Perl binary.
771 This string is passed to `format', so percent characters need to be doubled.")
773 (defconst tramp-perl-pack
774 "%s -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
775 "Perl program to use for encoding a file.
776 Escape sequence %s is replaced with name of Perl binary.")
778 (defconst tramp-perl-unpack
779 "%s -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"
780 "Perl program to use for decoding a file.
781 Escape sequence %s is replaced with name of Perl binary.")
783 (defconst tramp-vc-registered-read-file-names
786 if %s \"$file\"; then
787 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
789 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
791 if %s \"$file\"; then
792 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
794 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
798 "Script to check existence of VC related files.
799 It must be send formatted with two strings; the tests for file
800 existence, and file readability. Input shall be read via
801 here-document, otherwise the command could exceed maximum length
804 ;; New handlers should be added here. The following operations can be
805 ;; handled using the normal primitives: file-name-sans-versions,
807 (defconst tramp-sh-file-name-handler-alist
808 '((load . tramp-handle-load
)
809 (make-symbolic-link . tramp-sh-handle-make-symbolic-link
)
810 (file-name-as-directory . tramp-handle-file-name-as-directory
)
811 (file-name-directory . tramp-handle-file-name-directory
)
812 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
813 (file-truename . tramp-sh-handle-file-truename
)
814 (file-exists-p . tramp-sh-handle-file-exists-p
)
815 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p
)
816 (file-directory-p . tramp-sh-handle-file-directory-p
)
817 (file-executable-p . tramp-sh-handle-file-executable-p
)
818 (file-readable-p . tramp-sh-handle-file-readable-p
)
819 (file-regular-p . tramp-handle-file-regular-p
)
820 (file-symlink-p . tramp-handle-file-symlink-p
)
821 (file-writable-p . tramp-sh-handle-file-writable-p
)
822 (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p
)
823 (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p
)
824 (file-attributes . tramp-sh-handle-file-attributes
)
825 (file-modes . tramp-handle-file-modes
)
826 (directory-files . tramp-handle-directory-files
)
827 (directory-files-and-attributes
828 . tramp-sh-handle-directory-files-and-attributes
)
829 (file-name-all-completions . tramp-sh-handle-file-name-all-completions
)
830 (file-name-completion . tramp-handle-file-name-completion
)
831 (add-name-to-file . tramp-sh-handle-add-name-to-file
)
832 (copy-file . tramp-sh-handle-copy-file
)
833 (copy-directory . tramp-sh-handle-copy-directory
)
834 (rename-file . tramp-sh-handle-rename-file
)
835 (set-file-modes . tramp-sh-handle-set-file-modes
)
836 (set-file-times . tramp-sh-handle-set-file-times
)
837 (make-directory . tramp-sh-handle-make-directory
)
838 (delete-directory . tramp-sh-handle-delete-directory
)
839 (delete-file . tramp-sh-handle-delete-file
)
840 (directory-file-name . tramp-handle-directory-file-name
)
841 ;; `executable-find' is not official yet.
842 (executable-find . tramp-sh-handle-executable-find
)
843 (start-file-process . tramp-sh-handle-start-file-process
)
844 (process-file . tramp-sh-handle-process-file
)
845 (shell-command . tramp-handle-shell-command
)
846 (insert-directory . tramp-sh-handle-insert-directory
)
847 (expand-file-name . tramp-sh-handle-expand-file-name
)
848 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
849 (file-local-copy . tramp-sh-handle-file-local-copy
)
850 (file-remote-p . tramp-handle-file-remote-p
)
851 (insert-file-contents . tramp-handle-insert-file-contents
)
852 (insert-file-contents-literally
853 . tramp-sh-handle-insert-file-contents-literally
)
854 (write-region . tramp-sh-handle-write-region
)
855 (find-backup-file-name . tramp-handle-find-backup-file-name
)
856 (make-auto-save-file-name . tramp-sh-handle-make-auto-save-file-name
)
857 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
858 (dired-compress-file . tramp-sh-handle-dired-compress-file
)
859 (dired-recursive-delete-directory
860 . tramp-sh-handle-dired-recursive-delete-directory
)
861 (dired-uncache . tramp-handle-dired-uncache
)
862 (set-visited-file-modtime . tramp-sh-handle-set-visited-file-modtime
)
863 (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime
)
864 (file-selinux-context . tramp-sh-handle-file-selinux-context
)
865 (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context
)
866 (file-acl . tramp-sh-handle-file-acl
)
867 (set-file-acl . tramp-sh-handle-set-file-acl
)
868 (vc-registered . tramp-sh-handle-vc-registered
)
869 (file-notify-add-watch . tramp-sh-handle-file-notify-add-watch
)
870 (file-notify-rm-watch . tramp-sh-handle-file-notify-rm-watch
))
871 "Alist of handler functions.
872 Operations not mentioned here will be handled by the normal Emacs functions.")
874 ;; This must be the last entry, because `identity' always matches.
876 (add-to-list 'tramp-foreign-file-name-handler-alist
877 '(identity . tramp-sh-file-name-handler
) 'append
)
879 ;;; File Name Handler Functions:
881 (defun tramp-sh-handle-make-symbolic-link
882 (filename linkname
&optional ok-if-already-exists
)
883 "Like `make-symbolic-link' for Tramp files.
884 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
885 the symlink. If LINKNAME is a Tramp file, only the localname component is
886 used as the target of the symlink.
888 If LINKNAME is a Tramp file and the localname component is relative, then
889 it is expanded first, before the localname component is taken. Note that
890 this can give surprising results if the user/host for the source and
891 target of the symlink differ."
892 (with-parsed-tramp-file-name linkname l
893 (let ((ln (tramp-get-remote-ln l
))
894 (cwd (tramp-run-real-handler
895 'file-name-directory
(list l-localname
))))
899 "Making a symbolic link. ln(1) does not exist on the remote host."))
901 ;; Do the 'confirm if exists' thing.
902 (when (file-exists-p linkname
)
904 (if (or (null ok-if-already-exists
) ; not allowed to exist
905 (and (numberp ok-if-already-exists
)
908 "File %s already exists; make it a link anyway? "
911 l
'file-already-exists
"File %s already exists" l-localname
)
912 (delete-file linkname
)))
914 ;; If FILENAME is a Tramp name, use just the localname component.
915 (when (tramp-tramp-file-p filename
)
917 (tramp-file-name-localname
918 (tramp-dissect-file-name (expand-file-name filename
)))))
920 (tramp-flush-file-property l
(file-name-directory l-localname
))
921 (tramp-flush-file-property l l-localname
)
923 ;; Right, they are on the same host, regardless of user, method,
924 ;; etc. We now make the link on the remote machine. This will
925 ;; occur as the user that FILENAME belongs to.
926 (tramp-send-command-and-check
929 "cd %s && %s -sf %s %s"
930 (tramp-shell-quote-argument cwd
)
932 (tramp-shell-quote-argument filename
)
933 (tramp-shell-quote-argument l-localname
))
936 (defun tramp-sh-handle-file-truename (filename &optional counter prev-dirs
)
937 "Like `file-truename' for Tramp files."
938 (with-parsed-tramp-file-name (expand-file-name filename
) nil
939 (tramp-make-tramp-file-name method user host
940 (with-tramp-file-property v localname
"file-truename"
941 (let ((result nil
)) ; result steps in reverse order
942 (tramp-message v
4 "Finding true name for `%s'" filename
)
944 ;; Use GNU readlink --canonicalize-missing where available.
945 ((tramp-get-remote-readlink v
)
947 (tramp-send-command-and-read
949 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
950 (tramp-get-remote-readlink v
)
951 (tramp-shell-quote-argument localname
)))))
953 ;; Use Perl implementation.
954 ((and (tramp-get-remote-perl v
)
955 (tramp-get-connection-property v
"perl-file-spec" nil
)
956 (tramp-get-connection-property v
"perl-cwd-realpath" nil
))
957 (tramp-maybe-send-script
958 v tramp-perl-file-truename
"tramp_perl_file_truename")
960 (tramp-send-command-and-read
962 (format "tramp_perl_file_truename %s"
963 (tramp-shell-quote-argument localname
)))))
965 ;; Do it yourself. We bind `directory-sep-char' here for
966 ;; XEmacs on Windows, which would otherwise use backslash.
967 (t (let* ((directory-sep-char ?
/)
968 (steps (tramp-compat-split-string localname
"/"))
969 (localnamedir (tramp-run-real-handler
970 'file-name-as-directory
(list localname
)))
971 (is-dir (string= localname localnamedir
))
974 ;; Don't make the following value larger than
975 ;; necessary. People expect an error message in
976 ;; a timely fashion when something is wrong;
977 ;; otherwise they might think that Emacs is hung.
978 ;; Of course, correctness has to come first.
981 (while (and steps
(< numchase numchase-limit
))
982 (setq thisstep
(pop steps
))
986 (append '("") (reverse result
) (list thisstep
))
989 (nth 0 (file-attributes
990 (tramp-make-tramp-file-name
997 (cond ((string= "." thisstep
)
998 (tramp-message v
5 "Ignoring step `.'"))
999 ((string= ".." thisstep
)
1000 (tramp-message v
5 "Processing step `..'")
1002 ((stringp symlink-target
)
1003 ;; It's a symlink, follow it.
1005 v
5 "Follow symlink to %s" symlink-target
)
1006 (setq numchase
(1+ numchase
))
1007 (when (file-name-absolute-p symlink-target
)
1009 ;; If the symlink was absolute, we'll get a
1010 ;; string like "/user@host:/some/target";
1011 ;; extract the "/some/target" part from it.
1012 (when (tramp-tramp-file-p symlink-target
)
1013 (unless (tramp-equal-remote filename symlink-target
)
1016 "Symlink target `%s' on wrong host"
1018 (setq symlink-target localname
))
1020 (append (tramp-compat-split-string
1025 (setq result
(cons thisstep result
)))))
1026 (when (>= numchase numchase-limit
)
1029 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
1030 (setq result
(reverse result
))
1031 ;; Combine list to form string.
1034 (mapconcat 'identity
(cons "" result
) "/")
1037 (or (string= "" result
)
1038 (not (string= (substring result -
1) "/"))))
1039 (setq result
(concat result
"/"))))))
1041 (tramp-message v
4 "True name of `%s' is `%s'" localname result
)
1046 (defun tramp-sh-handle-file-exists-p (filename)
1047 "Like `file-exists-p' for Tramp files."
1048 (with-parsed-tramp-file-name filename nil
1049 (with-tramp-file-property v localname
"file-exists-p"
1050 (or (not (null (tramp-get-file-property
1051 v localname
"file-attributes-integer" nil
)))
1052 (not (null (tramp-get-file-property
1053 v localname
"file-attributes-string" nil
)))
1054 (tramp-send-command-and-check
1058 (tramp-get-file-exists-command v
)
1059 (tramp-shell-quote-argument localname
)))))))
1061 (defun tramp-sh-handle-file-attributes (filename &optional id-format
)
1062 "Like `file-attributes' for Tramp files."
1063 (unless id-format
(setq id-format
'integer
))
1064 ;; Don't modify `last-coding-system-used' by accident.
1065 (let ((last-coding-system-used last-coding-system-used
))
1066 (with-parsed-tramp-file-name (expand-file-name filename
) nil
1067 (with-tramp-file-property
1068 v localname
(format "file-attributes-%s" id-format
)
1070 (tramp-convert-file-attributes
1074 ((tramp-get-remote-stat v
)
1075 (tramp-do-file-attributes-with-stat v localname id-format
))
1076 ((tramp-get-remote-perl v
)
1077 (tramp-do-file-attributes-with-perl v localname id-format
))
1079 ;; The scripts could fail, for example with huge file size.
1080 (tramp-do-file-attributes-with-ls v localname id-format
))))))))
1082 (defun tramp-do-file-attributes-with-ls (vec localname
&optional id-format
)
1083 "Implement `file-attributes' for Tramp files using the ls(1) command."
1085 res-inode res-filemodes res-numlinks
1086 res-uid res-gid res-size res-symlink-target
)
1087 (tramp-message vec
5 "file attributes with ls: %s" localname
)
1090 (format "(%s %s || %s -h %s) && %s %s %s"
1091 (tramp-get-file-exists-command vec
)
1092 (tramp-shell-quote-argument localname
)
1093 (tramp-get-test-command vec
)
1094 (tramp-shell-quote-argument localname
)
1095 (tramp-get-ls-command vec
)
1096 (if (eq id-format
'integer
) "-ildn" "-ild")
1097 (tramp-shell-quote-argument localname
)))
1098 ;; parse `ls -l' output ...
1099 (with-current-buffer (tramp-get-buffer vec
)
1100 (when (> (buffer-size) 0)
1101 (goto-char (point-min))
1105 (read (current-buffer))
1106 (invalid-read-syntax
1107 (when (and (equal (cadr err
)
1108 "Integer constant overflow in reader")
1110 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1112 (let* ((big (read (substring (car (cddr err
)) 0
1113 (match-beginning 1))))
1114 (small (read (match-string 1 (car (cddr err
)))))
1115 (twiddle (/ small
65536)))
1116 (cons (+ big twiddle
)
1117 (- small
(* twiddle
65536))))))))
1118 ;; ... file mode flags
1119 (setq res-filemodes
(symbol-name (read (current-buffer))))
1121 (setq res-numlinks
(read (current-buffer)))
1123 (setq res-uid
(read (current-buffer)))
1124 (setq res-gid
(read (current-buffer)))
1125 (if (eq id-format
'integer
)
1127 (unless (numberp res-uid
) (setq res-uid -
1))
1128 (unless (numberp res-gid
) (setq res-gid -
1)))
1130 (unless (stringp res-uid
) (setq res-uid
(symbol-name res-uid
)))
1131 (unless (stringp res-gid
) (setq res-gid
(symbol-name res-gid
)))))
1133 (setq res-size
(read (current-buffer)))
1134 ;; From the file modes, figure out other stuff.
1135 (setq symlinkp
(eq ?l
(aref res-filemodes
0)))
1136 (setq dirp
(eq ?d
(aref res-filemodes
0)))
1137 ;; if symlink, find out file name pointed to
1139 (search-forward "-> ")
1140 (setq res-symlink-target
(buffer-substring (point) (point-at-eol))))
1141 ;; return data gathered
1143 ;; 0. t for directory, string (name linked to) for symbolic
1145 (or dirp res-symlink-target
)
1146 ;; 1. Number of links to file.
1152 ;; 4. Last access time, as a list of integers. Normally this
1153 ;; would be in the same format as `current-time', but the
1154 ;; subseconds part is not currently implemented, and (0 0)
1155 ;; denotes an unknown time.
1156 ;; 5. Last modification time, likewise.
1157 ;; 6. Last status change time, likewise.
1158 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1159 ;; 7. Size in bytes (-1, if number is out of range).
1161 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1163 ;; 9. t if file's gid would change if file were deleted and
1164 ;; recreated. Will be set in `tramp-convert-file-attributes'
1166 ;; 10. inode number.
1168 ;; 11. Device number. Will be replaced by a virtual device number.
1172 (defun tramp-do-file-attributes-with-perl
1173 (vec localname
&optional id-format
)
1174 "Implement `file-attributes' for Tramp files using a Perl script."
1175 (tramp-message vec
5 "file attributes with perl: %s" localname
)
1176 (tramp-maybe-send-script
1177 vec tramp-perl-file-attributes
"tramp_perl_file_attributes")
1178 (tramp-send-command-and-read
1180 (format "tramp_perl_file_attributes %s %s"
1181 (tramp-shell-quote-argument localname
) id-format
)))
1183 (defun tramp-do-file-attributes-with-stat
1184 (vec localname
&optional id-format
)
1185 "Implement `file-attributes' for Tramp files using stat(1) command."
1186 (tramp-message vec
5 "file attributes with stat: %s" localname
)
1187 (tramp-send-command-and-read
1190 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
1191 ;; parse correctly the sequence "((". Therefore, we add a space.
1192 "( (%s %s || %s -h %s) && %s -c '((\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)' %s || echo nil)"
1193 (tramp-get-file-exists-command vec
)
1194 (tramp-shell-quote-argument localname
)
1195 (tramp-get-test-command vec
)
1196 (tramp-shell-quote-argument localname
)
1197 (tramp-get-remote-stat vec
)
1198 (if (eq id-format
'integer
) "%ue0" "\"%U\"")
1199 (if (eq id-format
'integer
) "%ge0" "\"%G\"")
1200 (tramp-shell-quote-argument localname
))))
1202 (defun tramp-sh-handle-set-visited-file-modtime (&optional time-list
)
1203 "Like `set-visited-file-modtime' for Tramp files."
1204 (unless (buffer-file-name)
1205 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1208 (tramp-run-real-handler 'set-visited-file-modtime
(list time-list
))
1209 (let ((f (buffer-file-name))
1211 (with-parsed-tramp-file-name f nil
1212 (let* ((remote-file-name-inhibit-cache t
)
1213 (attr (file-attributes f
))
1214 ;; '(-1 65535) means file doesn't exists yet.
1215 (modtime (or (nth 5 attr
) '(-1 65535))))
1216 (when (boundp 'last-coding-system-used
)
1217 (setq coding-system-used
(symbol-value 'last-coding-system-used
)))
1218 ;; We use '(0 0) as a don't-know value. See also
1219 ;; `tramp-do-file-attributes-with-ls'.
1220 (if (not (equal modtime
'(0 0)))
1221 (tramp-run-real-handler 'set-visited-file-modtime
(list modtime
))
1225 (format "%s -ild %s"
1226 (tramp-get-ls-command v
)
1227 (tramp-shell-quote-argument localname
)))
1228 (setq attr
(buffer-substring (point)
1229 (progn (end-of-line) (point)))))
1230 (tramp-set-file-property
1231 v localname
"visited-file-modtime-ild" attr
))
1232 (when (boundp 'last-coding-system-used
)
1233 (set 'last-coding-system-used coding-system-used
))
1236 ;; This function makes the same assumption as
1237 ;; `tramp-sh-handle-set-visited-file-modtime'.
1238 (defun tramp-sh-handle-verify-visited-file-modtime (buf)
1239 "Like `verify-visited-file-modtime' for Tramp files.
1240 At the time `verify-visited-file-modtime' calls this function, we
1241 already know that the buffer is visiting a file and that
1242 `visited-file-modtime' does not return 0. Do not call this
1243 function directly, unless those two cases are already taken care
1245 (with-current-buffer buf
1246 (let ((f (buffer-file-name)))
1247 ;; There is no file visiting the buffer, or the buffer has no
1248 ;; recorded last modification time, or there is no established
1251 (eq (visited-file-modtime) 0)
1252 (not (tramp-file-name-handler 'file-remote-p f nil
'connected
)))
1254 (with-parsed-tramp-file-name f nil
1255 (let* ((remote-file-name-inhibit-cache t
)
1256 (attr (file-attributes f
))
1257 (modtime (nth 5 attr
))
1258 (mt (visited-file-modtime)))
1261 ;; File exists, and has a known modtime.
1262 ((and attr
(not (equal modtime
'(0 0))))
1263 (< (abs (tramp-time-diff
1265 ;; For compatibility, deal with both the old
1266 ;; (HIGH . LOW) and the new (HIGH LOW) return
1267 ;; values of `visited-file-modtime'.
1269 (list (car mt
) (cdr mt
))
1272 ;; Modtime has the don't know value.
1276 (format "%s -ild %s"
1277 (tramp-get-ls-command v
)
1278 (tramp-shell-quote-argument localname
)))
1279 (with-current-buffer (tramp-get-buffer v
)
1280 (setq attr
(buffer-substring
1281 (point) (progn (end-of-line) (point)))))
1284 (tramp-get-file-property
1285 v localname
"visited-file-modtime-ild" "")))
1286 ;; If file does not exist, say it is not modified if and
1287 ;; only if that agrees with the buffer's record.
1288 (t (equal mt
'(-1 65535))))))))))
1290 (defun tramp-sh-handle-set-file-modes (filename mode
)
1291 "Like `set-file-modes' for Tramp files."
1292 (with-parsed-tramp-file-name filename nil
1293 (tramp-flush-file-property v localname
)
1294 ;; FIXME: extract the proper text from chmod's stderr.
1295 (tramp-barf-unless-okay
1297 (format "chmod %s %s"
1298 (tramp-compat-decimal-to-octal mode
)
1299 (tramp-shell-quote-argument localname
))
1300 "Error while changing file's mode %s" filename
)))
1302 (defun tramp-sh-handle-set-file-times (filename &optional time
)
1303 "Like `set-file-times' for Tramp files."
1304 (if (file-remote-p filename
)
1305 (with-parsed-tramp-file-name filename nil
1306 (tramp-flush-file-property v localname
)
1307 (let ((time (if (or (null time
) (equal time
'(0 0)))
1310 ;; With GNU Emacs, `format-time-string' has an optional
1311 ;; parameter UNIVERSAL. This is preferred, because we
1312 ;; could handle the case when the remote host is located
1313 ;; in a different time zone as the local host.
1314 (utc (not (featurep 'xemacs
))))
1315 (tramp-send-command-and-check
1316 v
(format "%s touch -t %s %s"
1317 (if utc
"env TZ=UTC" "")
1319 (format-time-string "%Y%m%d%H%M.%S" time t
)
1320 (format-time-string "%Y%m%d%H%M.%S" time
))
1321 (tramp-shell-quote-argument localname
)))))
1323 ;; We handle also the local part, because in older Emacsen,
1324 ;; without `set-file-times', this function is an alias for this.
1325 ;; We are local, so we don't need the UTC settings.
1328 "touch" nil nil nil
"-t"
1329 (format-time-string "%Y%m%d%H%M.%S" time
)
1330 (tramp-shell-quote-argument filename
)))))
1332 (defun tramp-set-file-uid-gid (filename &optional uid gid
)
1333 "Set the ownership for FILENAME.
1334 If UID and GID are provided, these values are used; otherwise uid
1335 and gid of the corresponding user is taken. Both parameters must
1336 be non-negative integers."
1337 ;; Modern Unices allow chown only for root. So we might need
1338 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1339 ;; working with su(do)? when it is needed, so it shall succeed in
1340 ;; the majority of cases.
1341 ;; Don't modify `last-coding-system-used' by accident.
1342 (let ((last-coding-system-used last-coding-system-used
))
1343 (if (file-remote-p filename
)
1344 (with-parsed-tramp-file-name filename nil
1345 (if (and (zerop (user-uid)) (tramp-local-host-p v
))
1346 ;; If we are root on the local host, we can do it directly.
1347 (tramp-set-file-uid-gid localname uid gid
)
1348 (let ((uid (or (and (natnump uid
) uid
)
1349 (tramp-get-remote-uid v
'integer
)))
1350 (gid (or (and (natnump gid
) gid
)
1351 (tramp-get-remote-gid v
'integer
))))
1354 "chown %d:%d %s" uid gid
1355 (tramp-shell-quote-argument localname
))))))
1357 ;; We handle also the local part, because there doesn't exist
1358 ;; `set-file-uid-gid'. On W32 "chown" might not work.
1359 (let ((uid (or (and (natnump uid
) uid
) (tramp-get-local-uid 'integer
)))
1360 (gid (or (and (natnump gid
) gid
) (tramp-get-local-gid 'integer
))))
1363 (format "%d:%d" uid gid
) (tramp-shell-quote-argument filename
))))))
1365 (defun tramp-remote-selinux-p (vec)
1366 "Check, whether SELINUX is enabled on the remote host."
1367 (with-tramp-connection-property
1368 (tramp-get-connection-process vec
) "selinux-p"
1369 (let ((result (tramp-find-executable
1370 vec
"getenforce" (tramp-get-remote-path vec
) t t
)))
1373 (tramp-send-command-and-read
1374 vec
(format "echo \\\"`%S`\\\"" result
))
1377 (defun tramp-sh-handle-file-selinux-context (filename)
1378 "Like `file-selinux-context' for Tramp files."
1379 (with-parsed-tramp-file-name filename nil
1380 (with-tramp-file-property v localname
"file-selinux-context"
1381 (let ((context '(nil nil nil nil
))
1382 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1383 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1384 (when (and (tramp-remote-selinux-p v
)
1385 (tramp-send-command-and-check
1388 (tramp-get-ls-command v
)
1389 (tramp-shell-quote-argument localname
))))
1390 (with-current-buffer (tramp-get-connection-buffer v
)
1391 (goto-char (point-min))
1392 (when (re-search-forward regexp
(point-at-eol) t
)
1393 (setq context
(list (match-string 1) (match-string 2)
1394 (match-string 3) (match-string 4))))))
1395 ;; Return the context.
1398 (defun tramp-sh-handle-set-file-selinux-context (filename context
)
1399 "Like `set-file-selinux-context' for Tramp files."
1400 (with-parsed-tramp-file-name filename nil
1401 (if (and (consp context
)
1402 (tramp-remote-selinux-p v
)
1403 (tramp-send-command-and-check
1404 v
(format "chcon %s %s %s %s %s"
1405 (if (stringp (nth 0 context
))
1406 (format "--user=%s" (nth 0 context
)) "")
1407 (if (stringp (nth 1 context
))
1408 (format "--role=%s" (nth 1 context
)) "")
1409 (if (stringp (nth 2 context
))
1410 (format "--type=%s" (nth 2 context
)) "")
1411 (if (stringp (nth 3 context
))
1412 (format "--range=%s" (nth 3 context
)) "")
1413 (tramp-shell-quote-argument localname
))))
1415 (tramp-set-file-property v localname
"file-selinux-context" context
)
1417 (tramp-set-file-property v localname
"file-selinux-context" 'undef
)
1420 (defun tramp-remote-acl-p (vec)
1421 "Check, whether ACL is enabled on the remote host."
1422 (with-tramp-connection-property (tramp-get-connection-process vec
) "acl-p"
1423 (tramp-send-command-and-check vec
"getfacl /")))
1425 (defun tramp-sh-handle-file-acl (filename)
1426 "Like `file-acl' for Tramp files."
1427 (with-parsed-tramp-file-name filename nil
1428 (with-tramp-file-property v localname
"file-acl"
1429 (when (and (tramp-remote-acl-p v
)
1430 (tramp-send-command-and-check
1432 "getfacl -ac %s 2>/dev/null"
1433 (tramp-shell-quote-argument localname
))))
1434 (with-current-buffer (tramp-get-connection-buffer v
)
1435 (goto-char (point-max))
1436 (delete-blank-lines)
1437 (when (> (point-max) (point-min))
1438 (tramp-compat-funcall
1439 'substring-no-properties
(buffer-string))))))))
1441 (defun tramp-sh-handle-set-file-acl (filename acl-string
)
1442 "Like `set-file-acl' for Tramp files."
1443 (with-parsed-tramp-file-name (expand-file-name filename
) nil
1444 (if (and (stringp acl-string
) (tramp-remote-acl-p v
)
1447 v
(format "setfacl --set-file=- %s <<'EOF'\n%s\nEOF\n"
1448 (tramp-shell-quote-argument localname
) acl-string
))
1449 (tramp-send-command-and-check v nil
)))
1452 (tramp-set-file-property v localname
"file-acl" acl-string
)
1454 ;; In case of errors, we return `nil'.
1455 (tramp-set-file-property v localname
"file-acl-string" 'undef
)
1458 ;; Simple functions using the `test' command.
1460 (defun tramp-sh-handle-file-executable-p (filename)
1461 "Like `file-executable-p' for Tramp files."
1462 (with-parsed-tramp-file-name filename nil
1463 (with-tramp-file-property v localname
"file-executable-p"
1464 ;; Examine `file-attributes' cache to see if request can be
1465 ;; satisfied without remote operation.
1466 (or (tramp-check-cached-permissions v ?x
)
1467 (tramp-run-test "-x" filename
)))))
1469 (defun tramp-sh-handle-file-readable-p (filename)
1470 "Like `file-readable-p' for Tramp files."
1471 (with-parsed-tramp-file-name filename nil
1472 (with-tramp-file-property v localname
"file-readable-p"
1473 ;; Examine `file-attributes' cache to see if request can be
1474 ;; satisfied without remote operation.
1475 (or (tramp-check-cached-permissions v ?r
)
1476 (tramp-run-test "-r" filename
)))))
1478 ;; When the remote shell is started, it looks for a shell which groks
1479 ;; tilde expansion. Here, we assume that all shells which grok tilde
1480 ;; expansion will also provide a `test' command which groks `-nt' (for
1481 ;; newer than). If this breaks, tell me about it and I'll try to do
1482 ;; something smarter about it.
1483 (defun tramp-sh-handle-file-newer-than-file-p (file1 file2
)
1484 "Like `file-newer-than-file-p' for Tramp files."
1485 (cond ((not (file-exists-p file1
))
1487 ((not (file-exists-p file2
))
1489 ;; We are sure both files exist at this point.
1492 ;; We try to get the mtime of both files. If they are not
1493 ;; equal to the "dont-know" value, then we subtract the times
1494 ;; and obtain the result.
1495 (let ((fa1 (file-attributes file1
))
1496 (fa2 (file-attributes file2
)))
1497 (if (and (not (equal (nth 5 fa1
) '(0 0)))
1498 (not (equal (nth 5 fa2
) '(0 0))))
1499 (> 0 (tramp-time-diff (nth 5 fa2
) (nth 5 fa1
)))
1500 ;; If one of them is the dont-know value, then we can
1501 ;; still try to run a shell command on the remote host.
1502 ;; However, this only works if both files are Tramp
1503 ;; files and both have the same method, same user, same
1505 (unless (tramp-equal-remote file1 file2
)
1506 (with-parsed-tramp-file-name
1507 (if (tramp-tramp-file-p file1
) file1 file2
) nil
1510 "Files %s and %s must have same method, user, host"
1512 (with-parsed-tramp-file-name file1 nil
1514 (tramp-get-test-nt-command v
) file1 file2
))))))))
1516 ;; Functions implemented using the basic functions above.
1518 (defun tramp-sh-handle-file-directory-p (filename)
1519 "Like `file-directory-p' for Tramp files."
1520 (with-parsed-tramp-file-name filename nil
1521 ;; `file-directory-p' is used as predicate for filename completion.
1522 ;; Sometimes, when a connection is not established yet, it is
1523 ;; desirable to return t immediately for "/method:foo:". It can
1524 ;; be expected that this is always a directory.
1525 (or (zerop (length localname
))
1526 (with-tramp-file-property v localname
"file-directory-p"
1527 (tramp-run-test "-d" filename
)))))
1529 (defun tramp-sh-handle-file-writable-p (filename)
1530 "Like `file-writable-p' for Tramp files."
1531 (with-parsed-tramp-file-name filename nil
1532 (with-tramp-file-property v localname
"file-writable-p"
1533 (if (file-exists-p filename
)
1534 ;; Examine `file-attributes' cache to see if request can be
1535 ;; satisfied without remote operation.
1536 (or (tramp-check-cached-permissions v ?w
)
1537 (tramp-run-test "-w" filename
))
1538 ;; If file doesn't exist, check if directory is writable.
1539 (and (tramp-run-test "-d" (file-name-directory filename
))
1540 (tramp-run-test "-w" (file-name-directory filename
)))))))
1542 (defun tramp-sh-handle-file-ownership-preserved-p (filename &optional group
)
1543 "Like `file-ownership-preserved-p' for Tramp files."
1544 (with-parsed-tramp-file-name filename nil
1545 (with-tramp-file-property v localname
"file-ownership-preserved-p"
1546 (let ((attributes (file-attributes filename
)))
1547 ;; Return t if the file doesn't exist, since it's true that no
1548 ;; information would be lost by an (attempted) delete and create.
1549 (or (null attributes
)
1551 (= (nth 2 attributes
) (tramp-get-remote-uid v
'integer
))
1553 (= (nth 3 attributes
) (tramp-get-remote-gid v
'integer
)))))))))
1555 ;; Directory listings.
1557 (defun tramp-sh-handle-directory-files-and-attributes
1558 (directory &optional full match nosort id-format
)
1559 "Like `directory-files-and-attributes' for Tramp files."
1560 (unless id-format
(setq id-format
'integer
))
1561 (when (file-directory-p directory
)
1562 (setq directory
(expand-file-name directory
))
1565 (with-parsed-tramp-file-name directory nil
1566 (with-tramp-file-property
1568 (format "directory-files-and-attributes-%s" id-format
)
1573 (tramp-convert-file-attributes v
(cdr x
))))
1575 ((tramp-get-remote-stat v
)
1576 (tramp-do-directory-files-and-attributes-with-stat
1577 v localname id-format
))
1578 ((tramp-get-remote-perl v
)
1579 (tramp-do-directory-files-and-attributes-with-perl
1580 v localname id-format
)))))))))
1584 (setq item
(pop temp
))
1585 (when (or (null match
) (string-match match
(car item
)))
1587 (setcar item
(expand-file-name (car item
) directory
)))
1588 (push item result
)))
1592 (sort result
(lambda (x y
) (string< (car x
) (car y
))))))))
1594 (defun tramp-do-directory-files-and-attributes-with-perl
1595 (vec localname
&optional id-format
)
1596 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1597 (tramp-message vec
5 "directory-files-and-attributes with perl: %s" localname
)
1598 (tramp-maybe-send-script
1599 vec tramp-perl-directory-files-and-attributes
1600 "tramp_perl_directory_files_and_attributes")
1602 (tramp-send-command-and-read
1604 (format "tramp_perl_directory_files_and_attributes %s %s"
1605 (tramp-shell-quote-argument localname
) id-format
))))
1606 (when (stringp object
) (tramp-error vec
'file-error object
))
1609 (defun tramp-do-directory-files-and-attributes-with-stat
1610 (vec localname
&optional id-format
)
1611 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1612 (tramp-message vec
5 "directory-files-and-attributes with stat: %s" localname
)
1613 (tramp-send-command-and-read
1617 ;; We must care about filenames with spaces, or starting with
1618 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1619 ;; but it does not work on all remote systems. Therefore, we
1620 ;; quote the filenames via sed.
1621 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | "
1623 "'(\"%%n\" (\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)'"
1624 " 2>/dev/null); echo \")\"")
1625 (tramp-shell-quote-argument localname
)
1626 (tramp-get-ls-command vec
)
1627 (tramp-get-remote-stat vec
)
1628 (if (eq id-format
'integer
) "%ue0" "\"%U\"")
1629 (if (eq id-format
'integer
) "%ge0" "\"%G\""))))
1631 ;; This function should return "foo/" for directories and "bar" for
1633 (defun tramp-sh-handle-file-name-all-completions (filename directory
)
1634 "Like `file-name-all-completions' for Tramp files."
1635 (unless (save-match-data (string-match "/" filename
))
1636 (with-parsed-tramp-file-name (expand-file-name directory
) nil
1643 ;; Try cache entries for filename, filename with last
1644 ;; character removed, filename with last two characters
1645 ;; removed, ..., and finally the empty string - all
1646 ;; concatenated to the local directory name.
1647 (let ((remote-file-name-inhibit-cache
1648 (or remote-file-name-inhibit-cache
1649 tramp-completion-reread-directory-timeout
)))
1651 ;; This is inefficient for very long filenames, pity
1652 ;; `reduce' is not available...
1659 (tramp-get-file-property
1661 (concat localname
(substring filename
0 x
))
1662 "file-name-all-completions"
1664 (when cache-hit
(list cache-hit
))))
1665 ;; We cannot use a length of 0, because file properties
1666 ;; for "foo" and "foo/" are identical.
1667 (tramp-compat-number-sequence (length filename
) 1 -
1)))))
1669 ;; Cache expired or no matching cache entry found so we need
1670 ;; to perform a remote operation.
1672 ;; Get a list of directories and files, including reliably
1673 ;; tagging the directories with a trailing '/'. Because I
1674 ;; rock. --daniel@danann.net
1676 ;; Changed to perform `cd' in the same remote op and only
1677 ;; get entries starting with `filename'. Capture any `cd'
1678 ;; error messages. Ensure any `cd' and `echo' aliases are
1682 (if (tramp-get-remote-perl v
)
1684 (tramp-maybe-send-script
1685 v tramp-perl-file-name-all-completions
1686 "tramp_perl_file_name_all_completions")
1687 (format "tramp_perl_file_name_all_completions %s %s %d"
1688 (tramp-shell-quote-argument localname
)
1689 (tramp-shell-quote-argument filename
)
1691 ;; `read-file-name-completion-ignore-case'
1692 ;; is introduced with Emacs 22.1.
1694 'read-file-name-completion-ignore-case
)
1695 'read-file-name-completion-ignore-case
1696 'completion-ignore-case
))
1700 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
1701 ;; `ls' with wildcard might fail with `Argument
1702 ;; list too long' error in some corner cases; if
1703 ;; `ls' fails after `cd' succeeded, chances are
1704 ;; that's the case, so let's retry without
1705 ;; wildcard. This will return "too many" entries
1706 ;; but that isn't harmful.
1707 " || %s -a 2>/dev/null)"
1708 " | while read f; do"
1709 " if %s -d \"$f\" 2>/dev/null;"
1710 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1711 " && \\echo ok) || \\echo fail")
1712 (tramp-shell-quote-argument localname
)
1713 (tramp-get-ls-command v
)
1714 ;; When `filename' is empty, just `ls' without
1715 ;; filename argument is more efficient than `ls *'
1716 ;; for very large directories and might avoid the
1717 ;; `Argument list too long' error.
1719 ;; With and only with wildcard, we need to add
1720 ;; `-d' to prevent `ls' from descending into
1722 (if (zerop (length filename
))
1724 (concat (tramp-shell-quote-argument filename
) "* -d"))
1725 (tramp-get-ls-command v
)
1726 (tramp-get-test-command v
))))
1728 ;; Now grab the output.
1729 (with-current-buffer (tramp-get-buffer v
)
1730 (goto-char (point-max))
1732 ;; Check result code, found in last line of output.
1734 (if (looking-at "^fail$")
1736 ;; Grab error message from line before last line
1737 ;; (it was put there by `cd 2>&1').
1741 "tramp-sh-handle-file-name-all-completions: %s"
1742 (buffer-substring (point) (point-at-eol))))
1743 ;; For peace of mind, if buffer doesn't end in `fail'
1744 ;; then it should end in `ok'. If neither are in the
1745 ;; buffer something went seriously wrong on the remote
1747 (unless (looking-at "^ok$")
1751 tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
1752 (tramp-shell-quote-argument localname
) (buffer-string))))
1754 (while (zerop (forward-line -
1))
1755 (push (buffer-substring (point) (point-at-eol)) result
)))
1757 ;; Because the remote op went through OK we know the
1758 ;; directory we `cd'-ed to exists.
1759 (tramp-set-file-property v localname
"file-exists-p" t
)
1761 ;; Because the remote op went through OK we know every
1762 ;; file listed by `ls' exists.
1763 (mapc (lambda (entry)
1764 (tramp-set-file-property
1765 v
(concat localname entry
) "file-exists-p" t
))
1768 ;; Store result in the cache.
1769 (tramp-set-file-property
1770 v
(concat localname filename
)
1771 "file-name-all-completions" result
))))))))
1775 (defun tramp-sh-handle-add-name-to-file
1776 (filename newname
&optional ok-if-already-exists
)
1777 "Like `add-name-to-file' for Tramp files."
1778 (unless (tramp-equal-remote filename newname
)
1779 (with-parsed-tramp-file-name
1780 (if (tramp-tramp-file-p filename
) filename newname
) nil
1783 "add-name-to-file: %s"
1784 "only implemented for same method, same user, same host")))
1785 (with-parsed-tramp-file-name filename v1
1786 (with-parsed-tramp-file-name newname v2
1787 (let ((ln (when v1
(tramp-get-remote-ln v1
))))
1788 (when (and (not ok-if-already-exists
)
1789 (file-exists-p newname
)
1790 (not (numberp ok-if-already-exists
))
1793 "File %s already exists; make it a new name anyway? "
1797 "add-name-to-file: file %s already exists" newname
))
1798 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
1799 (tramp-flush-file-property v2 v2-localname
)
1800 (tramp-barf-unless-okay
1802 (format "%s %s %s" ln
(tramp-shell-quote-argument v1-localname
)
1803 (tramp-shell-quote-argument v2-localname
))
1804 "error with add-name-to-file, see buffer `%s' for details"
1807 (defun tramp-sh-handle-copy-file
1808 (filename newname
&optional ok-if-already-exists keep-date
1809 preserve-uid-gid preserve-extended-attributes
)
1810 "Like `copy-file' for Tramp files."
1811 (setq filename
(expand-file-name filename
))
1812 (setq newname
(expand-file-name newname
))
1814 ;; At least one file a Tramp file?
1815 ((or (tramp-tramp-file-p filename
)
1816 (tramp-tramp-file-p newname
))
1817 (tramp-do-copy-or-rename-file
1818 'copy filename newname ok-if-already-exists keep-date
1819 preserve-uid-gid preserve-extended-attributes
))
1821 (preserve-extended-attributes
1822 (tramp-run-real-handler
1824 (list filename newname ok-if-already-exists keep-date
1825 preserve-uid-gid preserve-extended-attributes
)))
1827 (tramp-run-real-handler
1829 (list filename newname ok-if-already-exists keep-date preserve-uid-gid
)))
1831 (tramp-run-real-handler
1832 'copy-file
(list filename newname ok-if-already-exists keep-date
)))))
1834 (defun tramp-sh-handle-copy-directory
1835 (dirname newname
&optional keep-date parents copy-contents
)
1836 "Like `copy-directory' for Tramp files."
1837 (let ((t1 (tramp-tramp-file-p dirname
))
1838 (t2 (tramp-tramp-file-p newname
)))
1839 (with-parsed-tramp-file-name (if t1 dirname newname
) nil
1840 (if (and (tramp-get-method-parameter method
'tramp-copy-recursive
)
1841 ;; When DIRNAME and NEWNAME are remote, they must have
1843 (or (null t1
) (null t2
)
1845 (tramp-file-name-method (tramp-dissect-file-name dirname
))
1846 (tramp-file-name-method (tramp-dissect-file-name newname
)))))
1847 ;; scp or rsync DTRT.
1849 (setq dirname
(directory-file-name (expand-file-name dirname
))
1850 newname
(directory-file-name (expand-file-name newname
)))
1851 (if (and (file-directory-p newname
)
1852 (not (string-equal (file-name-nondirectory dirname
)
1853 (file-name-nondirectory newname
))))
1856 (file-name-nondirectory dirname
) newname
)))
1857 (if (not (file-directory-p (file-name-directory newname
)))
1858 (make-directory (file-name-directory newname
) parents
))
1859 (tramp-do-copy-or-rename-file-out-of-band
1860 'copy dirname newname keep-date
))
1861 ;; We must do it file-wise.
1862 (tramp-run-real-handler
1863 'copy-directory
(list dirname newname keep-date parents
)))
1865 ;; When newname did exist, we have wrong cached values.
1867 (with-parsed-tramp-file-name newname nil
1868 (tramp-flush-file-property v
(file-name-directory localname
))
1869 (tramp-flush-file-property v localname
))))))
1871 (defun tramp-sh-handle-rename-file
1872 (filename newname
&optional ok-if-already-exists
)
1873 "Like `rename-file' for Tramp files."
1874 ;; Check if both files are local -- invoke normal rename-file.
1875 ;; Otherwise, use Tramp from local system.
1876 (setq filename
(expand-file-name filename
))
1877 (setq newname
(expand-file-name newname
))
1878 ;; At least one file a Tramp file?
1879 (if (or (tramp-tramp-file-p filename
)
1880 (tramp-tramp-file-p newname
))
1881 (tramp-do-copy-or-rename-file
1882 'rename filename newname ok-if-already-exists t t
)
1883 (tramp-run-real-handler
1884 'rename-file
(list filename newname ok-if-already-exists
))))
1886 (defun tramp-do-copy-or-rename-file
1887 (op filename newname
&optional ok-if-already-exists keep-date
1888 preserve-uid-gid preserve-extended-attributes
)
1889 "Copy or rename a remote file.
1890 OP must be `copy' or `rename' and indicates the operation to perform.
1891 FILENAME specifies the file to copy or rename, NEWNAME is the name of
1892 the new file (for copy) or the new name of the file (for rename).
1893 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
1894 KEEP-DATE means to make sure that NEWNAME has the same timestamp
1895 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
1896 the uid and gid if both files are on the same host.
1897 PRESERVE-EXTENDED-ATTRIBUTES activates selinux and acl commands.
1899 This function is invoked by `tramp-sh-handle-copy-file' and
1900 `tramp-sh-handle-rename-file'. It is an error if OP is neither
1901 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
1903 (unless (memq op
'(copy rename
))
1904 (error "Unknown operation `%s', must be `copy' or `rename'" op
))
1905 (let ((t1 (tramp-tramp-file-p filename
))
1906 (t2 (tramp-tramp-file-p newname
))
1907 (length (nth 7 (file-attributes (file-truename filename
))))
1908 (attributes (and preserve-extended-attributes
1909 (apply 'file-extended-attributes
(list filename
))))
1912 (with-parsed-tramp-file-name (if t1 filename newname
) nil
1913 (when (and (not ok-if-already-exists
) (file-exists-p newname
))
1915 v
'file-already-exists
"File %s already exists" newname
))
1917 (with-tramp-progress-reporter
1918 v
0 (format "%s %s to %s"
1919 (if (eq op
'copy
) "Copying" "Renaming")
1923 ;; Both are Tramp files.
1925 (with-parsed-tramp-file-name filename v1
1926 (with-parsed-tramp-file-name newname v2
1928 ;; Shortcut: if method, host, user are the same for
1929 ;; both files, we invoke `cp' or `mv' on the remote
1931 ((tramp-equal-remote filename newname
)
1932 (tramp-do-copy-or-rename-file-directly
1934 ok-if-already-exists keep-date preserve-uid-gid
))
1936 ;; Try out-of-band operation.
1938 (tramp-method-out-of-band-p v1 length
)
1939 (tramp-method-out-of-band-p v2 length
))
1940 (tramp-do-copy-or-rename-file-out-of-band
1941 op filename newname keep-date
))
1943 ;; No shortcut was possible. So we copy the file
1944 ;; first. If the operation was `rename', we go back
1945 ;; and delete the original file (if the copy was
1946 ;; successful). The approach is simple-minded: we
1947 ;; create a new buffer, insert the contents of the
1948 ;; source file into it, then write out the buffer to
1949 ;; the target file. The advantage is that it doesn't
1950 ;; matter which filename handlers are used for the
1951 ;; source and target file.
1953 (tramp-do-copy-or-rename-file-via-buffer
1954 op filename newname keep-date
))))))
1956 ;; One file is a Tramp file, the other one is local.
1959 ;; Fast track on local machine.
1960 ((tramp-local-host-p v
)
1961 (tramp-do-copy-or-rename-file-directly
1963 ok-if-already-exists keep-date preserve-uid-gid
))
1965 ;; If the Tramp file has an out-of-band method, the
1966 ;; corresponding copy-program can be invoked.
1967 ((tramp-method-out-of-band-p v length
)
1968 (tramp-do-copy-or-rename-file-out-of-band
1969 op filename newname keep-date
))
1971 ;; Use the inline method via a Tramp buffer.
1972 (t (tramp-do-copy-or-rename-file-via-buffer
1973 op filename newname keep-date
))))
1976 ;; One of them must be a Tramp file.
1977 (error "Tramp implementation says this cannot happen")))
1979 ;; Handle `preserve-extended-attributes'. We ignore possible
1980 ;; errors, because ACL strings could be incompatible.
1983 (apply 'set-file-extended-attributes
(list newname attributes
))))
1985 ;; In case of `rename', we must flush the cache of the source file.
1986 (when (and t1
(eq op
'rename
))
1987 (with-parsed-tramp-file-name filename v1
1988 (tramp-flush-file-property v1
(file-name-directory localname
))
1989 (tramp-flush-file-property v1 localname
)))
1991 ;; When newname did exist, we have wrong cached values.
1993 (with-parsed-tramp-file-name newname v2
1994 (tramp-flush-file-property v2
(file-name-directory localname
))
1995 (tramp-flush-file-property v2 localname
)))))))
1997 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date
)
1998 "Use an Emacs buffer to copy or rename a file.
1999 First arg OP is either `copy' or `rename' and indicates the operation.
2000 FILENAME is the source file, NEWNAME the target file.
2001 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2003 ;; We must disable multibyte, because binary data shall not be
2005 (set-buffer-multibyte nil
)
2006 (let ((coding-system-for-read 'binary
)
2007 (jka-compr-inhibit t
))
2008 (insert-file-contents-literally filename
))
2009 ;; We don't want the target file to be compressed, so we let-bind
2010 ;; `jka-compr-inhibit' to t.
2011 (let ((coding-system-for-write 'binary
)
2012 (jka-compr-inhibit t
))
2013 (write-region (point-min) (point-max) newname
)))
2014 ;; KEEP-DATE handling.
2015 (when keep-date
(set-file-times newname
(nth 5 (file-attributes filename
))))
2017 (set-file-modes newname
(tramp-default-file-modes filename
))
2018 ;; If the operation was `rename', delete the original file.
2019 (unless (eq op
'copy
) (delete-file filename
)))
2021 (defun tramp-do-copy-or-rename-file-directly
2022 (op filename newname ok-if-already-exists keep-date preserve-uid-gid
)
2023 "Invokes `cp' or `mv' on the remote system.
2024 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2025 respectively. FILENAME specifies the file to copy or rename,
2026 NEWNAME is the name of the new file (for copy) or the new name of
2027 the file (for rename). Both files must reside on the same host.
2028 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2029 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2030 the uid and gid from FILENAME."
2031 (let ((t1 (tramp-tramp-file-p filename
))
2032 (t2 (tramp-tramp-file-p newname
))
2033 (file-times (nth 5 (file-attributes filename
)))
2034 (file-modes (tramp-default-file-modes filename
)))
2035 (with-parsed-tramp-file-name (if t1 filename newname
) nil
2036 (let* ((cmd (cond ((and (eq op
'copy
) preserve-uid-gid
) "cp -f -p")
2037 ((eq op
'copy
) "cp -f")
2038 ((eq op
'rename
) "mv -f")
2041 "Unknown operation `%s', must be `copy' or `rename'"
2045 (tramp-file-name-handler 'file-remote-p filename
'localname
)
2049 (tramp-file-name-handler 'file-remote-p newname
'localname
)
2051 (prefix (file-remote-p (if t1 filename newname
)))
2055 ;; Both files are on a remote host, with same user.
2058 (tramp-send-command-and-check
2059 v
(format "%s %s %s" cmd
2060 (tramp-shell-quote-argument localname1
)
2061 (tramp-shell-quote-argument localname2
))))
2062 (with-current-buffer (tramp-get-buffer v
)
2063 (goto-char (point-min))
2067 ;; Mask cp -f error.
2069 tramp-operation-not-permitted-regexp nil t
))
2071 (tramp-error-with-buffer
2073 "Copying directly failed, see buffer `%s' for details."
2076 ;; We are on the local host.
2079 ;; We can do it directly.
2080 ((let (file-name-handler-alist)
2081 (and (file-readable-p localname1
)
2082 (file-writable-p (file-name-directory localname2
))
2083 (or (file-directory-p localname2
)
2084 (file-writable-p localname2
))))
2086 (tramp-compat-copy-file
2087 localname1 localname2 ok-if-already-exists
2088 keep-date preserve-uid-gid
)
2089 (tramp-run-real-handler
2090 'rename-file
(list localname1 localname2 ok-if-already-exists
))))
2092 ;; We can do it directly with `tramp-send-command'
2093 ((and (file-readable-p (concat prefix localname1
))
2095 (file-name-directory (concat prefix localname2
)))
2096 (or (file-directory-p (concat prefix localname2
))
2097 (file-writable-p (concat prefix localname2
))))
2098 (tramp-do-copy-or-rename-file-directly
2099 op
(concat prefix localname1
) (concat prefix localname2
)
2100 ok-if-already-exists keep-date t
)
2101 ;; We must change the ownership to the local user.
2102 (tramp-set-file-uid-gid
2103 (concat prefix localname2
)
2104 (tramp-get-local-uid 'integer
)
2105 (tramp-get-local-gid 'integer
)))
2107 ;; We need a temporary file in between.
2109 ;; Create the temporary file.
2110 (let ((tmpfile (tramp-compat-make-temp-file localname1
)))
2115 (tramp-barf-unless-okay
2118 (tramp-shell-quote-argument localname1
)
2119 (tramp-shell-quote-argument tmpfile
))
2120 "Copying directly failed, see buffer `%s' for details."
2121 (tramp-get-buffer v
))
2122 ;; We must change the ownership as remote user.
2123 ;; Since this does not work reliable, we also
2124 ;; give read permissions.
2126 (concat prefix tmpfile
)
2127 (tramp-compat-octal-to-decimal "0777"))
2128 (tramp-set-file-uid-gid
2129 (concat prefix tmpfile
)
2130 (tramp-get-local-uid 'integer
)
2131 (tramp-get-local-gid 'integer
)))
2134 (tramp-compat-copy-file
2135 localname1 tmpfile t
2136 keep-date preserve-uid-gid
)
2137 (tramp-run-real-handler
2139 (list localname1 tmpfile t
)))
2140 ;; We must change the ownership as local user.
2141 ;; Since this does not work reliable, we also
2142 ;; give read permissions.
2144 tmpfile
(tramp-compat-octal-to-decimal "0777"))
2145 (tramp-set-file-uid-gid
2147 (tramp-get-remote-uid v
'integer
)
2148 (tramp-get-remote-gid v
'integer
))))
2150 ;; Move the temporary file to its destination.
2153 (tramp-barf-unless-okay
2156 (tramp-shell-quote-argument tmpfile
)
2157 (tramp-shell-quote-argument localname2
))
2158 "Copying directly failed, see buffer `%s' for details."
2159 (tramp-get-buffer v
)))
2161 (tramp-run-real-handler
2163 (list tmpfile localname2 ok-if-already-exists
)))))
2166 (ignore-errors (delete-file tmpfile
)))))))))
2168 ;; Set the time and mode. Mask possible errors.
2171 (set-file-times newname file-times
)
2172 (set-file-modes newname file-modes
))))))
2174 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date
)
2175 "Invoke rcp program to copy.
2176 The method used must be an out-of-band method."
2177 (let* ((t1 (tramp-tramp-file-p filename
))
2178 (t2 (tramp-tramp-file-p newname
))
2179 (orig-vec (tramp-dissect-file-name (if t1 filename newname
)))
2180 copy-program copy-args copy-env copy-keep-date port spec
2181 options source target
)
2183 (with-parsed-tramp-file-name (if t1 filename newname
) nil
2186 ;; Both are Tramp files. We shall optimize it when the
2187 ;; methods for filename and newname are the same.
2188 (let* ((dir-flag (file-directory-p filename
))
2189 (tmpfile (tramp-compat-make-temp-file localname dir-flag
)))
2193 (file-name-nondirectory newname
) tmpfile
)))
2196 (tramp-do-copy-or-rename-file-out-of-band
2197 op filename tmpfile keep-date
)
2198 (tramp-do-copy-or-rename-file-out-of-band
2199 'rename tmpfile newname keep-date
))
2203 (tramp-compat-delete-directory
2204 (expand-file-name ".." tmpfile
) 'recursive
)
2205 (delete-file tmpfile
)))))
2207 ;; Set variables for computing the prompt for reading
2209 (setq tramp-current-method
(tramp-file-name-method v
)
2210 tramp-current-user
(or (tramp-file-name-user v
)
2211 (tramp-get-connection-property
2213 tramp-current-host
(tramp-file-name-real-host v
))
2215 ;; Expand hops. Might be necessary for gateway methods.
2216 (setq v
(car (tramp-compute-multi-hops v
)))
2217 (aset v
3 localname
)
2219 ;; Check which ones of source and target are Tramp files.
2220 (setq source
(if t1
(tramp-make-copy-program-file-name v
) filename
)
2222 (if (and (file-directory-p filename
)
2224 (file-name-nondirectory filename
)
2225 (file-name-nondirectory newname
)))
2226 'file-name-directory
2228 (if t2
(tramp-make-copy-program-file-name v
) newname
)))
2230 ;; Check for host and port number. We cannot use
2231 ;; `tramp-file-name-port', because this returns also
2232 ;; `tramp-default-port', which might clash with settings in
2234 (setq host
(tramp-file-name-host v
)
2236 (when (string-match tramp-host-with-port-regexp host
)
2237 (setq port
(string-to-number (match-string 2 host
))
2238 host
(string-to-number (match-string 1 host
))))
2240 ;; Check for user. There might be an interactive setting.
2241 (setq user
(or (tramp-file-name-user v
)
2242 (tramp-get-connection-property v
"login-as" nil
)))
2244 ;; Compose copy command.
2245 (setq host
(or host
"")
2248 spec
(format-spec-make
2249 ?t
(tramp-get-connection-property
2250 (tramp-get-connection-process v
) "temp-file" ""))
2251 options
(format-spec tramp-ssh-controlmaster-options spec
)
2252 spec
(format-spec-make
2253 ?h host ?u user ?p port ?c options
2254 ?k
(if keep-date
" " ""))
2255 copy-program
(tramp-get-method-parameter
2256 method
'tramp-copy-program
)
2257 copy-keep-date
(tramp-get-method-parameter
2258 method
'tramp-copy-keep-date
)
2261 ;; " " has either been a replacement of "%k" (when
2262 ;; keep-date argument is non-nil), or a replacement
2263 ;; for the whole keep-date sublist.
2267 (tramp-get-method-parameter method
'tramp-copy-args
)
2272 (let ((y (mapcar (lambda (z) (format-spec z spec
)) x
)))
2273 (if (member "" y
) '(" ") y
))))))
2279 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
2280 (unless (member "" x
) (mapconcat 'identity x
" ")))
2281 (tramp-get-method-parameter method
'tramp-copy-env
))))
2283 ;; Check for program.
2284 (unless (let ((default-directory
2285 (tramp-compat-temporary-file-directory)))
2286 (executable-find copy-program
))
2288 v
'file-error
"Cannot find copy program: %s" copy-program
))
2292 ;; The default directory must be remote.
2293 (let ((default-directory
2294 (file-name-directory (if t1 filename newname
)))
2295 (process-environment (copy-sequence process-environment
)))
2296 ;; Set the transfer process properties.
2297 (tramp-set-connection-property
2298 v
"process-name" (buffer-name (current-buffer)))
2299 (tramp-set-connection-property
2300 v
"process-buffer" (current-buffer))
2303 orig-vec
6 "%s=\"%s\"" (car copy-env
) (cadr copy-env
))
2304 (setenv (pop copy-env
) (pop copy-env
)))
2306 ;; Use an asynchronous process. By this, password can
2307 ;; be handled. The default directory must be local, in
2308 ;; order to apply the correct `copy-program'. We don't
2309 ;; set a timeout, because the copying of large files can
2310 ;; last longer than 60 secs.
2311 (let ((p (let ((default-directory
2312 (tramp-compat-temporary-file-directory)))
2313 (apply 'start-process-shell-command
2314 (tramp-get-connection-name v
)
2315 (tramp-get-connection-buffer v
)
2320 (shell-quote-argument source
)
2321 (shell-quote-argument target
)
2322 "&&" "echo" "tramp_exit_status" "0"
2323 "||" "echo" "tramp_exit_status" "1"))))))
2326 (mapconcat 'identity
(process-command p
) " "))
2327 (tramp-compat-set-process-query-on-exit-flag p nil
)
2328 (tramp-process-actions
2329 p v nil tramp-actions-copy-out-of-band
)
2331 ;; Check the return code.
2332 (goto-char (point-max))
2334 (re-search-backward "tramp_exit_status [0-9]+" nil t
)
2336 orig-vec
'file-error
2337 "Couldn't find exit status of `%s'" (process-command p
)))
2338 (skip-chars-forward "^ ")
2339 (unless (zerop (read (current-buffer)))
2342 orig-vec
'file-error
2343 "Error copying: `%s'"
2344 (buffer-substring (point-min) (point-at-eol))))))
2346 ;; Reset the transfer process properties.
2347 (tramp-message orig-vec
6 "\n%s" (buffer-string))
2348 (tramp-set-connection-property v
"process-name" nil
)
2349 (tramp-set-connection-property v
"process-buffer" nil
)))
2351 ;; Handle KEEP-DATE argument.
2352 (when (and keep-date
(not copy-keep-date
))
2353 (set-file-times newname
(nth 5 (file-attributes filename
))))
2356 (unless (and keep-date copy-keep-date
)
2358 (set-file-modes newname
(tramp-default-file-modes filename
)))))
2360 ;; If the operation was `rename', delete the original file.
2361 (unless (eq op
'copy
)
2362 (if (file-regular-p filename
)
2363 (delete-file filename
)
2364 (tramp-compat-delete-directory filename
'recursive
))))))
2366 (defun tramp-sh-handle-make-directory (dir &optional parents
)
2367 "Like `make-directory' for Tramp files."
2368 (setq dir
(expand-file-name dir
))
2369 (with-parsed-tramp-file-name dir nil
2370 (tramp-flush-directory-property v
(file-name-directory localname
))
2372 (tramp-barf-unless-okay
2374 (if parents
"mkdir -p" "mkdir")
2375 (tramp-shell-quote-argument localname
))
2376 "Couldn't make directory %s" dir
))))
2378 (defun tramp-sh-handle-delete-directory (directory &optional recursive
)
2379 "Like `delete-directory' for Tramp files."
2380 (setq directory
(expand-file-name directory
))
2381 (with-parsed-tramp-file-name directory nil
2382 (tramp-flush-file-property v
(file-name-directory localname
))
2383 (tramp-flush-directory-property v localname
)
2384 (tramp-barf-unless-okay
2386 (if recursive
"rm -rf" "rmdir")
2387 (tramp-shell-quote-argument localname
))
2388 "Couldn't delete %s" directory
)))
2390 (defun tramp-sh-handle-delete-file (filename &optional trash
)
2391 "Like `delete-file' for Tramp files."
2392 (setq filename
(expand-file-name filename
))
2393 (with-parsed-tramp-file-name filename nil
2394 (tramp-flush-file-property v
(file-name-directory localname
))
2395 (tramp-flush-file-property v localname
)
2396 (tramp-barf-unless-okay
2398 (or (and trash
(tramp-get-remote-trash v
)) "rm -f")
2399 (tramp-shell-quote-argument localname
))
2400 "Couldn't delete %s" filename
)))
2404 ;; CCC: This does not seem to be enough. Something dies when
2405 ;; we try and delete two directories under Tramp :/
2406 (defun tramp-sh-handle-dired-recursive-delete-directory (filename)
2407 "Recursively delete the directory given.
2408 This is like `dired-recursive-delete-directory' for Tramp files."
2409 (with-parsed-tramp-file-name filename nil
2410 ;; Run a shell command 'rm -r <localname>'.
2411 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
2412 (unless (file-exists-p filename
)
2413 (tramp-error v
'file-error
"No such directory: %s" filename
))
2414 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>).
2417 (format "rm -rf %s" (tramp-shell-quote-argument localname
))
2418 ;; Don't read the output, do it explicitly.
2420 ;; Wait for the remote system to return to us...
2421 ;; This might take a while, allow it plenty of time.
2422 (tramp-wait-for-output (tramp-get-connection-process v
) 120)
2423 ;; Make sure that it worked...
2424 (tramp-flush-file-property v
(file-name-directory localname
))
2425 (tramp-flush-directory-property v localname
)
2426 (and (file-exists-p filename
)
2428 v
'file-error
"Failed to recursively delete %s" filename
))))
2430 (defun tramp-sh-handle-dired-compress-file (file &rest ok-flag
)
2431 "Like `dired-compress-file' for Tramp files."
2432 ;; OK-FLAG is valid for XEmacs only, but not implemented.
2433 ;; Code stolen mainly from dired-aux.el.
2434 (with-parsed-tramp-file-name file nil
2435 (tramp-flush-file-property v localname
)
2438 (if (not (featurep 'xemacs
))
2440 (symbol-value 'dired-compress-file-suffixes
)
2441 ;; XEmacs has `dired-compression-method-alist', which is
2442 ;; transformed into `dired-compress-file-suffixes' structure.
2445 (list (concat (regexp-quote (nth 1 x
)) "\\'")
2447 (mapconcat 'identity
(nth 3 x
) " ")))
2448 (symbol-value 'dired-compression-method-alist
))))
2450 ;; See if any suffix rule matches this file name.
2452 (let (case-fold-search)
2453 (if (string-match (car (car suffixes
)) localname
)
2454 (setq suffix
(car suffixes
) suffixes nil
))
2455 (setq suffixes
(cdr suffixes
))))
2457 (cond ((file-symlink-p file
)
2459 ((and suffix
(nth 2 suffix
))
2460 ;; We found an uncompression rule.
2461 (with-tramp-progress-reporter
2462 v
0 (format "Uncompressing %s" file
)
2463 (when (tramp-send-command-and-check
2464 v
(concat (nth 2 suffix
) " "
2465 (tramp-shell-quote-argument localname
)))
2466 ;; `dired-remove-file' is not defined in XEmacs.
2467 (tramp-compat-funcall 'dired-remove-file file
)
2468 (string-match (car suffix
) file
)
2469 (concat (substring file
0 (match-beginning 0))))))
2471 ;; We don't recognize the file as compressed, so compress it.
2473 (with-tramp-progress-reporter v
0 (format "Compressing %s" file
)
2474 (when (tramp-send-command-and-check
2475 v
(concat "gzip -f "
2476 (tramp-shell-quote-argument localname
)))
2477 ;; `dired-remove-file' is not defined in XEmacs.
2478 (tramp-compat-funcall 'dired-remove-file file
)
2479 (cond ((file-exists-p (concat file
".gz"))
2480 (concat file
".gz"))
2481 ((file-exists-p (concat file
".z"))
2485 (defun tramp-sh-handle-insert-directory
2486 (filename switches
&optional wildcard full-directory-p
)
2487 "Like `insert-directory' for Tramp files."
2488 (setq filename
(expand-file-name filename
))
2489 (with-parsed-tramp-file-name filename nil
2490 (if (and (featurep 'ls-lisp
)
2491 (not (symbol-value 'ls-lisp-use-insert-directory-program
)))
2492 (tramp-run-real-handler
2493 'insert-directory
(list filename switches wildcard full-directory-p
))
2494 (when (stringp switches
)
2495 (setq switches
(split-string switches
)))
2496 (when (and (member "--dired" switches
)
2497 (not (tramp-get-ls-command-with-dired v
)))
2498 (setq switches
(delete "--dired" switches
)))
2500 (setq wildcard
(tramp-run-real-handler
2501 'file-name-nondirectory
(list localname
)))
2502 (setq localname
(tramp-run-real-handler
2503 'file-name-directory
(list localname
))))
2504 (unless full-directory-p
2505 (setq switches
(add-to-list 'switches
"-d" 'append
)))
2506 (setq switches
(mapconcat 'tramp-shell-quote-argument switches
" "))
2508 (setq switches
(concat switches
" " wildcard
)))
2510 v
4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2511 switches filename
(if wildcard
"yes" "no")
2512 (if full-directory-p
"yes" "no"))
2513 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2514 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2515 ;; "--dired" returns byte positions. Therefore, the file names
2516 ;; must be encoded, which is guaranteed by "LC_ALL=en_US.utf8
2518 (if full-directory-p
2521 (format "env LC_ALL=en_US.utf8 LC_CTYPE='' %s %s %s 2>/dev/null"
2522 (tramp-get-ls-command v
)
2526 (tramp-shell-quote-argument (concat localname
".")))))
2527 (tramp-barf-unless-okay
2529 (format "cd %s" (tramp-shell-quote-argument
2530 (tramp-run-real-handler
2531 'file-name-directory
(list localname
))))
2533 (tramp-shell-quote-argument
2534 (tramp-run-real-handler 'file-name-directory
(list localname
))))
2537 (format "env LC_ALL=en_US.utf8 LC_CTYPE='' %s %s %s 2>/dev/null"
2538 (tramp-get-ls-command v
)
2542 (tramp-run-real-handler
2543 'file-name-nondirectory
(list localname
)))))
2545 (tramp-shell-quote-argument
2546 (tramp-run-real-handler
2547 'file-name-nondirectory
(list localname
)))))))
2548 (let ((beg (point)))
2549 ;; We cannot use `insert-buffer-substring' because the Tramp
2550 ;; buffer changes its contents before insertion due to calling
2551 ;; `expand-file' and alike.
2553 (with-current-buffer (tramp-get-buffer v
)
2556 ;; Check for "--dired" output.
2558 (when (looking-at "//SUBDIRED//")
2560 (when (looking-at "//DIRED//\\s-+")
2561 (let ((databeg (match-end 0))
2562 (end (point-at-eol)))
2563 ;; Now read the numeric positions of file names.
2565 (while (< (point) end
)
2566 (let ((start (+ beg
(read (current-buffer))))
2567 (end (+ beg
(read (current-buffer)))))
2568 (if (memq (char-after end
) '(?
\n ?\
))
2569 ;; End is followed by \n or by " -> ".
2570 (put-text-property start end
'dired-filename t
))))))
2571 ;; Remove trailing lines.
2572 (goto-char (point-at-bol))
2573 (while (looking-at "//")
2575 (delete-region (match-beginning 0) (point)))
2577 ;; Some busyboxes are reluctant to discard colors.
2578 (unless (string-match "color" (tramp-get-connection-property v
"ls" ""))
2580 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
2581 (replace-match "")))
2583 ;; Decode the output, it could be multibyte.
2584 (decode-coding-region
2586 (or file-name-coding-system
2587 (and (boundp 'default-file-name-coding-system
)
2588 (symbol-value 'default-file-name-coding-system
))))
2590 ;; The inserted file could be from somewhere else.
2591 (when (and (not wildcard
) (not full-directory-p
))
2592 (goto-char (point-max))
2593 (when (file-symlink-p filename
)
2594 (goto-char (search-backward "->" beg
'noerror
)))
2596 (if (zerop (length (file-name-nondirectory filename
)))
2598 (file-name-nondirectory filename
))
2600 (replace-match (file-relative-name filename
) t
))
2602 (goto-char (point-max))))))
2604 ;; Canonicalization of file names.
2606 (defun tramp-sh-handle-expand-file-name (name &optional dir
)
2607 "Like `expand-file-name' for Tramp files.
2608 If the localname part of the given filename starts with \"/../\" then
2609 the result will be a local, non-Tramp, filename."
2610 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
2611 (setq dir
(or dir default-directory
"/"))
2612 ;; Unless NAME is absolute, concat DIR and NAME.
2613 (unless (file-name-absolute-p name
)
2614 (setq name
(concat (file-name-as-directory dir
) name
)))
2615 ;; If NAME is not a Tramp file, run the real handler.
2616 (if (not (tramp-connectable-p name
))
2617 (tramp-run-real-handler 'expand-file-name
(list name nil
))
2619 (with-parsed-tramp-file-name name nil
2620 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
2621 (setq localname
(concat "~/" localname
)))
2622 ;; Tilde expansion if necessary. This needs a shell which
2623 ;; groks tilde expansion! The function `tramp-find-shell' is
2624 ;; supposed to find such a shell on the remote host. Please
2625 ;; tell me about it when this doesn't work on your system.
2626 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname
)
2627 (let ((uname (match-string 1 localname
))
2628 (fname (match-string 2 localname
)))
2629 ;; We cannot simply apply "~/", because under sudo "~/" is
2630 ;; expanded to the local user home directory but to the
2631 ;; root home directory. On the other hand, using always
2632 ;; the default user name for tilde expansion is not
2633 ;; appropriate either, because ssh and companions might
2634 ;; use a user name from the config file.
2635 (when (and (string-equal uname
"~")
2636 (string-match "\\`su\\(do\\)?\\'" method
))
2637 (setq uname
(concat uname user
)))
2639 (with-tramp-connection-property v uname
2641 v
(format "cd %s; pwd" (tramp-shell-quote-argument uname
)))
2642 (with-current-buffer (tramp-get-buffer v
)
2643 (goto-char (point-min))
2644 (buffer-substring (point) (point-at-eol)))))
2645 (setq localname
(concat uname fname
))))
2646 ;; There might be a double slash, for example when "~/"
2647 ;; expands to "/". Remove this.
2648 (while (string-match "//" localname
)
2649 (setq localname
(replace-match "/" t t localname
)))
2650 ;; No tilde characters in file name, do normal
2651 ;; `expand-file-name' (this does "/./" and "/../"). We bind
2652 ;; `directory-sep-char' here for XEmacs on Windows, which would
2653 ;; otherwise use backslash. `default-directory' is bound,
2654 ;; because on Windows there would be problems with UNC shares or
2656 (let ((directory-sep-char ?
/)
2657 (default-directory (tramp-compat-temporary-file-directory)))
2658 (tramp-make-tramp-file-name
2660 (tramp-drop-volume-letter
2661 (tramp-run-real-handler
2662 'expand-file-name
(list localname
)))
2665 ;;; Remote commands:
2667 (defun tramp-sh-handle-executable-find (command)
2668 "Like `executable-find' for Tramp files."
2669 (with-parsed-tramp-file-name default-directory nil
2670 (tramp-find-executable v command
(tramp-get-remote-path v
) t
)))
2672 (defun tramp-process-sentinel (proc event
)
2673 "Flush file caches."
2674 (unless (memq (process-status proc
) '(run open
))
2675 (let ((vec (tramp-get-connection-property proc
"vector" nil
)))
2677 (tramp-message vec
5 "Sentinel called: `%S' `%s'" proc event
)
2678 (tramp-flush-connection-property proc
)
2679 (tramp-flush-directory-property vec
"")))))
2681 ;; We use BUFFER also as connection buffer during setup. Because of
2682 ;; this, its original contents must be saved, and restored once
2683 ;; connection has been setup.
2684 (defun tramp-sh-handle-start-file-process (name buffer program
&rest args
)
2685 "Like `start-file-process' for Tramp files."
2686 (with-parsed-tramp-file-name default-directory nil
2687 ;; When PROGRAM is nil, we just provide a tty.
2689 (when (stringp program
)
2690 (format "cd %s; exec env PS1=%s %s"
2691 (tramp-shell-quote-argument localname
)
2692 ;; Use a human-friendly prompt, for example for `shell'.
2693 (tramp-shell-quote-argument
2695 (file-remote-p default-directory
)
2696 tramp-initial-end-of-output
))
2697 (mapconcat 'tramp-shell-quote-argument
2698 (cons program args
) " "))))
2699 (tramp-process-connection-type
2700 (or (null program
) tramp-process-connection-type
))
2701 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
2704 ;; We do not want to raise an error when
2705 ;; `start-file-process' has been started several time in
2706 ;; `eshell' and friends.
2707 (tramp-current-connection nil
))
2710 ;; BUFFER can be nil. We use a temporary buffer.
2711 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
2712 (while (get-process name1
)
2713 ;; NAME must be unique as process name.
2715 name1
(format "%s<%d>" name i
)))
2717 ;; Set the new process properties.
2718 (tramp-set-connection-property v
"process-name" name
)
2719 (tramp-set-connection-property v
"process-buffer" buffer
)
2721 (with-current-buffer (tramp-get-connection-buffer v
)
2723 ;; We catch this event. Otherwise, `start-process' could
2724 ;; be called on the local host.
2727 ;; Activate narrowing in order to save BUFFER
2728 ;; contents. Clear also the modification time;
2729 ;; otherwise we might be interrupted by
2730 ;; `verify-visited-file-modtime'.
2731 (let ((buffer-undo-list t
)
2732 (buffer-read-only nil
)
2734 (clear-visited-file-modtime)
2735 (narrow-to-region (point-max) (point-max))
2736 ;; We call `tramp-maybe-open-connection', in order
2737 ;; to cleanup the prompt afterwards.
2739 (tramp-maybe-open-connection v
)
2741 (delete-region mark
(point))
2742 (narrow-to-region (point-max) (point-max))
2745 ;; Send the command.
2746 (tramp-send-command v command nil t
) ; nooutput
2747 ;; Check, whether a pty is associated.
2748 (unless (tramp-compat-process-get
2749 (tramp-get-connection-process v
) 'remote-tty
)
2752 "pty association is not supported for `%s'" name
))))
2753 (let ((p (tramp-get-connection-process v
)))
2754 ;; Set query flag and process marker for this
2755 ;; process. We ignore errors, because the process
2756 ;; could have finished already.
2758 (tramp-compat-set-process-query-on-exit-flag p t
)
2759 (set-marker (process-mark p
) (point)))
2764 (if (string-match tramp-temp-buffer-name
(buffer-name))
2766 (set-process-buffer (tramp-get-connection-process v
) nil
)
2767 (kill-buffer (current-buffer)))
2768 (set-buffer-modified-p bmp
))
2769 (tramp-set-connection-property v
"process-name" nil
)
2770 (tramp-set-connection-property v
"process-buffer" nil
))))))
2772 (defun tramp-sh-handle-process-file
2773 (program &optional infile destination display
&rest args
)
2774 "Like `process-file' for Tramp files."
2775 ;; The implementation is not complete yet.
2776 (when (and (numberp destination
) (zerop destination
))
2777 (error "Implementation does not handle immediate return"))
2779 (with-parsed-tramp-file-name default-directory nil
2780 (let (command input tmpinput stderr tmpstderr outbuf ret
)
2782 (setq command
(mapconcat 'tramp-shell-quote-argument
2783 (cons program args
) " "))
2786 (setq input
"/dev/null")
2787 (setq infile
(expand-file-name infile
))
2788 (if (tramp-equal-remote default-directory infile
)
2789 ;; INFILE is on the same remote host.
2790 (setq input
(with-parsed-tramp-file-name infile nil localname
))
2791 ;; INFILE must be copied to remote host.
2792 (setq input
(tramp-make-tramp-temp-file v
)
2793 tmpinput
(tramp-make-tramp-file-name method user host input
))
2794 (copy-file infile tmpinput t
)))
2795 (when input
(setq command
(format "%s <%s" command input
)))
2797 ;; Determine output.
2800 ((bufferp destination
)
2801 (setq outbuf destination
))
2803 ((stringp destination
)
2804 (setq outbuf
(get-buffer-create destination
)))
2805 ;; (REAL-DESTINATION ERROR-DESTINATION)
2806 ((consp destination
)
2809 ((bufferp (car destination
))
2810 (setq outbuf
(car destination
)))
2811 ((stringp (car destination
))
2812 (setq outbuf
(get-buffer-create (car destination
))))
2814 (setq outbuf
(current-buffer))))
2817 ((stringp (cadr destination
))
2818 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
2819 (if (tramp-equal-remote default-directory
(cadr destination
))
2820 ;; stderr is on the same remote host.
2821 (setq stderr
(with-parsed-tramp-file-name
2822 (cadr destination
) nil localname
))
2823 ;; stderr must be copied to remote host. The temporary
2824 ;; file must be deleted after execution.
2825 (setq stderr
(tramp-make-tramp-temp-file v
)
2826 tmpstderr
(tramp-make-tramp-file-name
2827 method user host stderr
))))
2828 ;; stderr to be discarded.
2829 ((null (cadr destination
))
2830 (setq stderr
"/dev/null"))))
2833 (setq outbuf
(current-buffer))))
2834 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
2836 ;; Send the command. It might not return in time, so we protect
2837 ;; it. Call it in a subshell, in order to preserve working
2842 (if (tramp-send-command-and-check
2843 v
(format "\\cd %s; %s"
2844 (tramp-shell-quote-argument localname
)
2848 ;; We should show the output anyway.
2850 (with-current-buffer outbuf
2852 (with-current-buffer (tramp-get-connection-buffer v
)
2854 (when display
(display-buffer outbuf
))))
2855 ;; When the user did interrupt, we should do it also. We use
2856 ;; return code -1 as marker.
2858 (kill-buffer (tramp-get-connection-buffer v
))
2862 (kill-buffer (tramp-get-connection-buffer v
))
2865 ;; Provide error file.
2866 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
2868 ;; Cleanup. We remove all file cache values for the connection,
2869 ;; because the remote process could have changed them.
2870 (when tmpinput
(delete-file tmpinput
))
2872 ;; `process-file-side-effects' has been introduced with GNU
2873 ;; Emacs 23.2. If set to `nil', no remote file will be changed
2874 ;; by `program'. If it doesn't exist, we assume its default
2876 (unless (and (boundp 'process-file-side-effects
)
2877 (not (symbol-value 'process-file-side-effects
)))
2878 (tramp-flush-directory-property v
""))
2880 ;; Return exit status.
2885 (defun tramp-sh-handle-file-local-copy (filename)
2886 "Like `file-local-copy' for Tramp files."
2887 (with-parsed-tramp-file-name filename nil
2888 (unless (file-exists-p filename
)
2891 "Cannot make local copy of non-existing file `%s'" filename
))
2893 (let* ((size (nth 7 (file-attributes (file-truename filename
))))
2894 (rem-enc (tramp-get-inline-coding v
"remote-encoding" size
))
2895 (loc-dec (tramp-get-inline-coding v
"local-decoding" size
))
2896 (tmpfile (tramp-compat-make-temp-file filename
)))
2900 ;; `copy-file' handles direct copy and out-of-band methods.
2901 ((or (tramp-local-host-p v
)
2902 (tramp-method-out-of-band-p v size
))
2903 (copy-file filename tmpfile t t
))
2905 ;; Use inline encoding for file transfer.
2908 (with-tramp-progress-reporter
2910 (format "Encoding remote file `%s' with `%s'" filename rem-enc
)
2911 (tramp-barf-unless-okay
2912 v
(format rem-enc
(tramp-shell-quote-argument localname
))
2913 "Encoding remote file failed"))
2915 (with-tramp-progress-reporter
2916 v
3 (format "Decoding local file `%s' with `%s'"
2918 (if (functionp loc-dec
)
2919 ;; If local decoding is a function, we call it.
2920 ;; We must disable multibyte, because
2921 ;; `uudecode-decode-region' doesn't handle it
2924 (set-buffer-multibyte nil
)
2925 (insert-buffer-substring (tramp-get-buffer v
))
2926 (funcall loc-dec
(point-min) (point-max))
2927 ;; Unset `file-name-handler-alist'. Otherwise,
2928 ;; epa-file gets confused.
2929 (let (file-name-handler-alist
2930 (coding-system-for-write 'binary
))
2931 (write-region (point-min) (point-max) tmpfile
)))
2933 ;; If tramp-decoding-function is not defined for this
2934 ;; method, we invoke tramp-decoding-command instead.
2935 (let ((tmpfile2 (tramp-compat-make-temp-file filename
)))
2936 ;; Unset `file-name-handler-alist'. Otherwise,
2937 ;; epa-file gets confused.
2938 (let (file-name-handler-alist
2939 (coding-system-for-write 'binary
))
2940 (with-current-buffer (tramp-get-buffer v
)
2941 (write-region (point-min) (point-max) tmpfile2
)))
2943 (tramp-call-local-coding-command
2944 loc-dec tmpfile2 tmpfile
)
2945 (delete-file tmpfile2
)))))
2947 ;; Set proper permissions.
2948 (set-file-modes tmpfile
(tramp-default-file-modes filename
))
2949 ;; Set local user ownership.
2950 (tramp-set-file-uid-gid tmpfile
)))
2952 ;; Oops, I don't know what to do.
2954 v
'file-error
"Wrong method specification for `%s'" method
)))
2958 (delete-file tmpfile
)
2959 (signal (car err
) (cdr err
))))
2961 (run-hooks 'tramp-handle-file-local-copy-hook
)
2964 ;; This is needed for XEmacs only. Code stolen from files.el.
2965 (defun tramp-sh-handle-insert-file-contents-literally
2966 (filename &optional visit beg end replace
)
2967 "Like `insert-file-contents-literally' for Tramp files."
2968 (let ((format-alist nil
)
2969 (after-insert-file-functions nil
)
2970 (coding-system-for-read 'no-conversion
)
2971 (coding-system-for-write 'no-conversion
)
2972 (find-buffer-file-type-function
2973 (if (fboundp 'find-buffer-file-type
)
2974 (symbol-function 'find-buffer-file-type
)
2976 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler
))
2977 (inhibit-file-name-operation 'insert-file-contents
))
2980 (fset 'find-buffer-file-type
(lambda (filename) t
))
2981 (insert-file-contents filename visit beg end replace
))
2983 (if find-buffer-file-type-function
2984 (fset 'find-buffer-file-type find-buffer-file-type-function
)
2985 (fmakunbound 'find-buffer-file-type
)))))
2987 (defun tramp-sh-handle-make-auto-save-file-name ()
2988 "Like `make-auto-save-file-name' for Tramp files.
2989 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
2990 (let ((tramp-auto-save-directory tramp-auto-save-directory
)
2992 (tramp-subst-strs-in-string
2999 (buffer-file-name))))
3000 ;; File name must be unique. This is ensured with Emacs 22 (see
3001 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
3002 ;; all other cases we must do it ourselves.
3003 (when (boundp 'auto-save-file-name-transforms
)
3006 (when (and (string-match (car x
) buffer-file-name
)
3007 (not (car (cddr x
))))
3008 (setq tramp-auto-save-directory
3009 (or tramp-auto-save-directory
3010 (tramp-compat-temporary-file-directory)))))
3011 (symbol-value 'auto-save-file-name-transforms
)))
3012 ;; Create directory.
3013 (when tramp-auto-save-directory
3014 (setq buffer-file-name
3015 (expand-file-name buffer-file-name tramp-auto-save-directory
))
3016 (unless (file-exists-p tramp-auto-save-directory
)
3017 (make-directory tramp-auto-save-directory t
)))
3018 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3019 ;; it is not a magic file name operation (since Emacs 22).
3020 ;; We must deactivate it temporarily.
3021 (if (not (ad-is-active 'make-auto-save-file-name
))
3022 (tramp-run-real-handler 'make-auto-save-file-name nil
)
3024 (ad-deactivate 'make-auto-save-file-name
)
3026 (tramp-run-real-handler 'make-auto-save-file-name nil
)
3027 (ad-activate 'make-auto-save-file-name
)))))
3029 ;; CCC grok LOCKNAME
3030 (defun tramp-sh-handle-write-region
3031 (start end filename
&optional append visit lockname confirm
)
3032 "Like `write-region' for Tramp files."
3033 (setq filename
(expand-file-name filename
))
3034 (with-parsed-tramp-file-name filename nil
3035 ;; Following part commented out because we don't know what to do about
3036 ;; file locking, and it does not appear to be a problem to ignore it.
3037 ;; Ange-ftp ignores it, too.
3038 ;; (when (and lockname (stringp lockname))
3039 ;; (setq lockname (expand-file-name lockname)))
3040 ;; (unless (or (eq lockname nil)
3041 ;; (string= lockname filename))
3043 ;; "tramp-sh-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3045 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
3046 (when (and (not (featurep 'xemacs
)) confirm
(file-exists-p filename
))
3047 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename
))
3048 (tramp-error v
'file-error
"File not overwritten")))
3050 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename
'integer
))
3051 (tramp-get-remote-uid v
'integer
)))
3052 (gid (or (nth 3 (tramp-compat-file-attributes filename
'integer
))
3053 (tramp-get-remote-gid v
'integer
))))
3055 (if (and (tramp-local-host-p v
)
3056 ;; `file-writable-p' calls `file-expand-file-name'. We
3057 ;; cannot use `tramp-run-real-handler' therefore.
3058 (let (file-name-handler-alist)
3060 (file-writable-p (file-name-directory localname
))
3061 (or (file-directory-p localname
)
3062 (file-writable-p localname
)))))
3063 ;; Short track: if we are on the local host, we can run directly.
3064 (tramp-run-real-handler
3066 (list start end localname append
'no-message lockname confirm
))
3068 (let* ((modes (save-excursion (tramp-default-file-modes filename
)))
3069 ;; We use this to save the value of
3070 ;; `last-coding-system-used' after writing the tmp
3071 ;; file. At the end of the function, we set
3072 ;; `last-coding-system-used' to this saved value. This
3073 ;; way, any intermediary coding systems used while
3074 ;; talking to the remote shell or suchlike won't hose
3075 ;; this variable. This approach was snarfed from
3078 ;; Write region into a tmp file. This isn't really
3079 ;; needed if we use an encoding function, but currently
3080 ;; we use it always because this makes the logic
3081 ;; simpler. We must also set `temporary-file-directory',
3082 ;; because it could point to a remote directory.
3083 (temporary-file-directory
3084 (tramp-compat-temporary-file-directory))
3085 (tmpfile (or tramp-temp-buffer-file-name
3086 (tramp-compat-make-temp-file filename
))))
3088 ;; If `append' is non-nil, we copy the file locally, and let
3089 ;; the native `write-region' implementation do the job.
3090 (when append
(copy-file filename tmpfile
'ok
))
3092 ;; We say `no-message' here because we don't want the
3093 ;; visited file modtime data to be clobbered from the temp
3094 ;; file. We call `set-visited-file-modtime' ourselves later
3095 ;; on. We must ensure that `file-coding-system-alist'
3096 ;; matches `tmpfile'.
3097 (let (file-name-handler-alist
3098 (file-coding-system-alist
3099 (tramp-find-file-name-coding-system-alist filename tmpfile
)))
3101 (tramp-run-real-handler
3103 (list start end tmpfile append
'no-message lockname confirm
))
3105 (setq tramp-temp-buffer-file-name nil
)
3106 (delete-file tmpfile
)
3107 (signal (car err
) (cdr err
))))
3109 ;; Now, `last-coding-system-used' has the right value. Remember it.
3110 (when (boundp 'last-coding-system-used
)
3111 (setq coding-system-used
3112 (symbol-value 'last-coding-system-used
))))
3114 ;; The permissions of the temporary file should be set. If
3115 ;; filename does not exist (eq modes nil) it has been
3116 ;; renamed to the backup file. This case `save-buffer'
3117 ;; handles permissions.
3118 ;; Ensure that it is still readable.
3122 (logior (or modes
0) (tramp-compat-octal-to-decimal "0400"))))
3124 ;; This is a bit lengthy due to the different methods
3125 ;; possible for file transfer. First, we check whether the
3126 ;; method uses an rcp program. If so, we call it.
3127 ;; Otherwise, both encoding and decoding command must be
3128 ;; specified. However, if the method _also_ specifies an
3129 ;; encoding function, then that is used for encoding the
3130 ;; contents of the tmp file.
3131 (let* ((size (nth 7 (file-attributes tmpfile
)))
3132 (rem-dec (tramp-get-inline-coding v
"remote-decoding" size
))
3133 (loc-enc (tramp-get-inline-coding v
"local-encoding" size
)))
3135 ;; `copy-file' handles direct copy and out-of-band methods.
3136 ((or (tramp-local-host-p v
)
3137 (tramp-method-out-of-band-p v size
))
3138 (if (and (not (stringp start
))
3139 (= (or end
(point-max)) (point-max))
3140 (= (or start
(point-min)) (point-min))
3141 (tramp-get-method-parameter
3142 method
'tramp-copy-keep-tmpfile
))
3144 (setq tramp-temp-buffer-file-name tmpfile
)
3146 ;; We keep the local file for performance
3147 ;; reasons, useful for "rsync".
3148 (copy-file tmpfile filename t
)
3150 (setq tramp-temp-buffer-file-name nil
)
3151 (delete-file tmpfile
)
3152 (signal (car err
) (cdr err
)))))
3153 (setq tramp-temp-buffer-file-name nil
)
3154 ;; Don't rename, in order to keep context in SELinux.
3156 (copy-file tmpfile filename t
)
3157 (delete-file tmpfile
))))
3159 ;; Use inline file transfer.
3164 (set-buffer-multibyte nil
)
3165 ;; Use encoding function or command.
3166 (with-tramp-progress-reporter
3167 v
3 (format "Encoding local file `%s' using `%s'"
3169 (if (functionp loc-enc
)
3170 ;; The following `let' is a workaround for
3171 ;; the base64.el that comes with pgnus-0.84.
3172 ;; If both of the following conditions are
3173 ;; satisfied, it tries to write to a local
3174 ;; file in default-directory, but at this
3175 ;; point, default-directory is remote.
3176 ;; (`call-process-region' can't write to
3177 ;; remote files, it seems.) The file in
3178 ;; question is a tmp file anyway.
3179 (let ((coding-system-for-read 'binary
)
3181 (tramp-compat-temporary-file-directory)))
3182 (insert-file-contents-literally tmpfile
)
3183 (funcall loc-enc
(point-min) (point-max)))
3185 (unless (zerop (tramp-call-local-coding-command
3189 (concat "Cannot write to `%s', "
3190 "local encoding command `%s' failed")
3191 filename loc-enc
))))
3193 ;; Send buffer into remote decoding command which
3194 ;; writes to remote file. Because this happens on
3195 ;; the remote host, we cannot use the function.
3196 (with-tramp-progress-reporter
3197 v
3 (format "Decoding remote file `%s' using `%s'"
3199 (goto-char (point-max))
3200 (unless (bolp) (newline))
3204 (concat rem-dec
" <<'EOF'\n%sEOF")
3205 (tramp-shell-quote-argument localname
)
3207 (tramp-barf-unless-okay
3209 "Couldn't write region to `%s', decode using `%s' failed"
3211 ;; When `file-precious-flag' is set, the region is
3212 ;; written to a temporary file. Check that the
3213 ;; checksum is equal to that from the local tmpfile.
3214 (when file-precious-flag
3217 ;; cksum runs locally, if possible.
3218 (zerop (tramp-call-process "cksum" tmpfile t
))
3219 ;; cksum runs remotely.
3220 (tramp-send-command-and-check
3223 "cksum <%s" (tramp-shell-quote-argument localname
)))
3224 ;; ... they are different.
3228 (with-current-buffer (tramp-get-buffer v
)
3232 (concat "Couldn't write region to `%s',"
3233 " decode using `%s' failed")
3234 filename rem-dec
)))))
3237 (delete-file tmpfile
)))
3239 ;; That's not expected.
3243 (concat "Method `%s' should specify both encoding and "
3244 "decoding command or an rcp program")
3247 ;; Make `last-coding-system-used' have the right value.
3248 (when coding-system-used
3249 (set 'last-coding-system-used coding-system-used
))))
3251 (tramp-flush-file-property v
(file-name-directory localname
))
3252 (tramp-flush-file-property v localname
)
3254 ;; We must protect `last-coding-system-used', now we have set it
3255 ;; to its correct value.
3256 (let (last-coding-system-used (need-chown t
))
3257 ;; Set file modification time.
3258 (when (or (eq visit t
) (stringp visit
))
3259 (let ((file-attr (tramp-compat-file-attributes filename
'integer
)))
3260 (set-visited-file-modtime
3261 ;; We must pass modtime explicitly, because filename can
3262 ;; be different from (buffer-file-name), f.e. if
3263 ;; `file-precious-flag' is set.
3265 (when (and (= (nth 2 file-attr
) uid
)
3266 (= (nth 3 file-attr
) gid
))
3267 (setq need-chown nil
))))
3269 ;; Set the ownership.
3271 (tramp-set-file-uid-gid filename uid gid
))
3272 (when (or (eq visit t
) (null visit
) (stringp visit
))
3273 (tramp-message v
0 "Wrote %s" filename
))
3274 (run-hooks 'tramp-handle-write-region-hook
)))))
3276 (defvar tramp-vc-registered-file-names nil
3277 "List used to collect file names, which are checked during `vc-registered'.")
3279 ;; VC backends check for the existence of various different special
3280 ;; files. This is very time consuming, because every single check
3281 ;; requires a remote command (the file cache must be invalidated).
3282 ;; Therefore, we apply a kind of optimization. We install the file
3283 ;; name handler `tramp-vc-file-name-handler', which does nothing but
3284 ;; remembers all file names for which `file-exists-p' or
3285 ;; `file-readable-p' has been applied. A first run of `vc-registered'
3286 ;; is performed. Afterwards, a script is applied for all collected
3287 ;; file names, using just one remote command. The result of this
3288 ;; script is used to fill the file cache with actual values. Now we
3289 ;; can reset the file name handlers, and we make a second run of
3290 ;; `vc-registered', which returns the expected result without sending
3291 ;; any other remote command.
3292 (defun tramp-sh-handle-vc-registered (file)
3293 "Like `vc-registered' for Tramp files."
3294 (tramp-compat-with-temp-message ""
3295 (with-parsed-tramp-file-name file nil
3296 (with-tramp-progress-reporter
3297 v
3 (format "Checking `vc-registered' for %s" file
)
3299 ;; There could be new files, created by the vc backend. We
3300 ;; cannot reuse the old cache entries, therefore.
3301 (let (tramp-vc-registered-file-names
3302 (remote-file-name-inhibit-cache (current-time))
3303 (file-name-handler-alist
3304 `((,tramp-file-name-regexp . tramp-vc-file-name-handler
))))
3306 ;; Here we collect only file names, which need an operation.
3307 (ignore-errors (tramp-run-real-handler 'vc-registered
(list file
)))
3308 (tramp-message v
10 "\n%s" tramp-vc-registered-file-names
)
3310 ;; Send just one command, in order to fill the cache.
3311 (when tramp-vc-registered-file-names
3312 (tramp-maybe-send-script
3314 (format tramp-vc-registered-read-file-names
3315 (tramp-get-file-exists-command v
)
3316 (format "%s -r" (tramp-get-test-command v
)))
3317 "tramp_vc_registered_read_file_names")
3321 (tramp-send-command-and-read
3324 "tramp_vc_registered_read_file_names <<'EOF'\n%s\nEOF\n"
3325 (mapconcat 'tramp-shell-quote-argument
3326 tramp-vc-registered-file-names
3329 (tramp-set-file-property
3330 v
(car elt
) (cadr elt
) (cadr (cdr elt
))))))
3332 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3333 ;; calls shall be answered from the file cache. We unset
3334 ;; `process-file-side-effects' in order to keep the cache when
3335 ;; `process-file' calls appear.
3336 (let (process-file-side-effects)
3338 (tramp-run-real-handler 'vc-registered
(list file
))))))))
3340 ;;;###tramp-autoload
3341 (defun tramp-sh-file-name-handler (operation &rest args
)
3342 "Invoke remote-shell Tramp file name handler.
3343 Fall back to normal file name handler if no Tramp handler exists."
3344 (when (and tramp-locked
(not tramp-locker
))
3345 (setq tramp-locked nil
)
3346 (signal 'file-error
(list "Forbidden reentrant call of Tramp")))
3347 (let ((tl tramp-locked
))
3350 (setq tramp-locked t
)
3351 (let ((tramp-locker t
))
3353 (let ((fn (assoc operation tramp-sh-file-name-handler-alist
)))
3355 (apply (cdr fn
) args
)
3356 (tramp-run-real-handler operation args
))))))
3357 (setq tramp-locked tl
))))
3359 (defun tramp-vc-file-name-handler (operation &rest args
)
3360 "Invoke special file name handler, which collects files to be handled."
3363 (tramp-replace-environment-variables
3364 (apply 'tramp-file-name-for-operation operation args
)))
3365 (fn (assoc operation tramp-sh-file-name-handler-alist
)))
3366 (with-parsed-tramp-file-name filename nil
3368 ;; That's what we want: file names, for which checks are
3369 ;; applied. We assume that VC uses only `file-exists-p' and
3370 ;; `file-readable-p' checks; otherwise we must extend the
3371 ;; list. We do not perform any action, but return nil, in
3372 ;; order to keep `vc-registered' running.
3373 ((and fn
(memq operation
'(file-exists-p file-readable-p
)))
3374 (add-to-list 'tramp-vc-registered-file-names localname
'append
)
3376 ;; `process-file' and `start-file-process' shall be ignored.
3377 ((and fn
(eq operation
'process-file
) 0))
3378 ((and fn
(eq operation
'start-file-process
) nil
))
3379 ;; Tramp file name handlers like `expand-file-name'. They
3381 (fn (save-match-data (apply (cdr fn
) args
)))
3382 ;; Default file name handlers, we don't care.
3383 (t (tramp-run-real-handler operation args
)))))))
3385 (defun tramp-sh-handle-file-notify-add-watch (file-name flags callback
)
3386 "Like `file-notify-add-watch' for Tramp files."
3387 (setq file-name
(expand-file-name file-name
))
3388 (with-parsed-tramp-file-name file-name nil
3389 (let* ((default-directory (file-name-directory file-name
))
3390 command events filter p
)
3392 ;; gvfs-monitor-dir.
3393 ((setq command
(tramp-get-remote-gvfs-monitor-dir v
))
3394 (setq filter
'tramp-sh-file-gvfs-monitor-dir-process-filter
3395 p
(start-file-process
3396 "gvfs-monitor-dir" (generate-new-buffer " *gvfs-monitor-dir*")
3397 command localname
)))
3399 ((setq command
(tramp-get-remote-inotifywait v
))
3400 (setq filter
'tramp-sh-file-inotifywait-process-filter
3403 ((and (memq 'change flags
) (memq 'attribute-change flags
))
3404 "create,modify,move,delete,attrib")
3405 ((memq 'change flags
) "create,modify,move,delete")
3406 ((memq 'attribute-change flags
) "attrib"))
3407 p
(start-file-process
3408 "inotifywait" (generate-new-buffer " *inotifywait*")
3409 command
"-mq" "-e" events localname
)))
3412 v
'file-notify-error
3413 "No file notification program found on %s"
3414 (file-remote-p file-name
))))
3415 ;; Return the process object as watch-descriptor.
3416 (if (not (processp p
))
3418 v
'file-notify-error
"`%s' failed to start on remote host" command
)
3419 (tramp-compat-set-process-query-on-exit-flag p nil
)
3420 (set-process-filter p filter
)
3423 (defun tramp-sh-file-gvfs-monitor-dir-process-filter (proc string
)
3424 "Read output from \"gvfs-monitor-dir\" and add corresponding file-notify events."
3425 (let ((remote-prefix
3426 (with-current-buffer (process-buffer proc
)
3427 (file-remote-p default-directory
)))
3428 (rest-string (tramp-compat-process-get proc
'rest-string
)))
3430 (tramp-message proc
10 (format "Previous string:\n%s" rest-string
)))
3431 (tramp-message proc
6 (format "%S\n%s" proc string
))
3432 (setq string
(concat rest-string string
)
3433 ;; Attribute change is returned in unused wording.
3434 string
(replace-regexp-in-string
3435 "ATTRIB CHANGED" "ATTRIBUTE_CHANGED" string
))
3437 (while (string-match
3439 "Directory Monitor Event:[\n\r]+"
3440 "Child = \\([^\n\r]+\\)[\n\r]+"
3441 "\\(Other = \\([^\n\r]+\\)[\n\r]+\\)?"
3442 "Event = \\([^[:blank:]]+\\)[\n\r]+")
3448 (replace-regexp-in-string
3449 "_" "-" (downcase (match-string 4 string
))))
3450 ;; File names are returned as absolute paths. We must
3451 ;; add the remote prefix.
3452 (concat remote-prefix
(match-string 1 string
))
3453 (when (match-string 3 string
)
3454 (concat remote-prefix
(match-string 3 string
))))))
3455 (setq string
(replace-match "" nil nil string
))
3456 ;; Usually, we would add an Emacs event now. Unfortunately,
3457 ;; `unread-command-events' does not accept several events at
3458 ;; once. Therefore, we apply the callback directly.
3459 (tramp-compat-funcall 'file-notify-callback object
)))
3461 ;; Save rest of the string.
3462 (when (zerop (length string
)) (setq string nil
))
3463 (when string
(tramp-message proc
10 (format "Rest string:\n%s" string
)))
3464 (tramp-compat-process-put proc
'rest-string string
)))
3466 (defun tramp-sh-file-inotifywait-process-filter (proc string
)
3467 "Read output from \"inotifywait\" and add corresponding file-notify events."
3468 (tramp-message proc
6 (format "%S\n%s" proc string
))
3469 (dolist (line (split-string string
"[\n\r]+" 'omit-nulls
))
3470 ;; Check, whether there is a problem.
3473 (concat "^[^[:blank:]]+"
3474 "[[:blank:]]+\\([^[:blank:]]+\\)+"
3475 "\\([[:blank:]]+\\([^\n\r]+\\)\\)?")
3477 (tramp-error proc
'file-notify-error
"%s" line
))
3484 (intern-soft (replace-regexp-in-string "_" "-" (downcase x
))))
3485 (split-string (match-string 1 line
) "," 'omit-nulls
))
3486 (match-string 3 line
))))
3487 ;; Usually, we would add an Emacs event now. Unfortunately,
3488 ;; `unread-command-events' does not accept several events at
3489 ;; once. Therefore, we apply the callback directly.
3490 (tramp-compat-funcall 'file-notify-callback object
))))
3492 (defvar file-notify-descriptors
)
3493 (defun tramp-sh-handle-file-notify-rm-watch (proc)
3494 "Like `file-notify-rm-watch' for Tramp files."
3495 ;; The descriptor must be a process object.
3496 (unless (and (processp proc
) (gethash proc file-notify-descriptors
))
3497 (tramp-error proc
'file-notify-error
"Not a valid descriptor %S" proc
))
3498 (tramp-message proc
6 (format "Kill %S" proc
))
3499 (kill-process proc
))
3501 ;;; Internal Functions:
3503 (defun tramp-maybe-send-script (vec script name
)
3504 "Define in remote shell function NAME implemented as SCRIPT.
3505 Only send the definition if it has not already been done."
3506 ;; We cannot let-bind (tramp-get-connection-process vec) because it
3508 (let ((scripts (tramp-get-connection-property
3509 (tramp-get-connection-process vec
) "scripts" nil
)))
3510 (unless (member name scripts
)
3511 (with-tramp-progress-reporter vec
5 (format "Sending script `%s'" name
)
3512 ;; The script could contain a call of Perl. This is masked with `%s'.
3513 (when (and (string-match "%s" script
)
3514 (not (tramp-get-remote-perl vec
)))
3515 (tramp-error vec
'file-error
"No Perl available on remote host"))
3516 (tramp-barf-unless-okay
3518 (format "%s () {\n%s\n}" name
3519 (format script
(tramp-get-remote-perl vec
)))
3520 "Script %s sending failed" name
)
3521 (tramp-set-connection-property
3522 (tramp-get-connection-process vec
) "scripts" (cons name scripts
))))))
3524 (defun tramp-set-auto-save ()
3525 (when (and ;; ange-ftp has its own auto-save mechanism
3526 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
3527 'tramp-sh-file-name-handler
)
3529 (auto-save-mode 1)))
3530 (add-hook 'find-file-hooks
'tramp-set-auto-save t
)
3531 (add-hook 'tramp-unload-hook
3533 (remove-hook 'find-file-hooks
'tramp-set-auto-save
)))
3535 (defun tramp-run-test (switch filename
)
3536 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3537 Returns the exit code of the `test' program."
3538 (with-parsed-tramp-file-name filename nil
3539 (tramp-send-command-and-check
3543 (tramp-get-test-command v
)
3545 (tramp-shell-quote-argument localname
)))))
3547 (defun tramp-run-test2 (format-string file1 file2
)
3548 "Run `test'-like program on the remote system, given FILE1, FILE2.
3549 FORMAT-STRING contains the program name, switches, and place holders.
3550 Returns the exit code of the `test' program. Barfs if the methods,
3551 hosts, or files, disagree."
3552 (unless (tramp-equal-remote file1 file2
)
3553 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1
) file1 file2
) nil
3556 "tramp-run-test2 only implemented for same method, user, host")))
3557 (with-parsed-tramp-file-name file1 v1
3558 (with-parsed-tramp-file-name file1 v2
3559 (tramp-send-command-and-check
3561 (format format-string
3562 (tramp-shell-quote-argument v1-localname
)
3563 (tramp-shell-quote-argument v2-localname
))))))
3565 (defun tramp-find-executable
3566 (vec progname dirlist
&optional ignore-tilde ignore-path
)
3567 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3568 First arg VEC specifies the connection, PROGNAME is the program
3569 to search for, and DIRLIST gives the list of directories to
3570 search. If IGNORE-TILDE is non-nil, directory names starting
3571 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3574 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3576 This function expects to be in the right *tramp* buffer."
3577 (with-current-buffer (tramp-get-connection-buffer vec
)
3579 ;; Check whether the executable is in $PATH. "which(1)" does not
3580 ;; report always a correct error code; therefore we check the
3581 ;; number of words it returns.
3583 (tramp-send-command vec
(format "which \\%s | wc -w" progname
))
3584 (goto-char (point-min))
3585 (if (looking-at "^\\s-*1$")
3586 (setq result
(concat "\\" progname
))))
3589 ;; Remove all ~/foo directories from dirlist. In XEmacs,
3590 ;; `remove' is in CL, and we want to avoid CL dependencies.
3593 (setq d
(car dirlist
))
3594 (setq dirlist
(cdr dirlist
))
3595 (unless (char-equal ?~
(aref d
0))
3596 (setq newdl
(cons d newdl
))))
3597 (setq dirlist
(nreverse newdl
))))
3600 (format (concat "while read d; "
3601 "do if test -x $d/%s -a -f $d/%s; "
3602 "then echo tramp_executable $d/%s; "
3603 "break; fi; done <<'EOF'\n"
3605 progname progname progname
(mapconcat 'identity dirlist
"\n")))
3606 (goto-char (point-max))
3607 (when (search-backward "tramp_executable " nil t
)
3608 (skip-chars-forward "^ ")
3609 (skip-chars-forward " ")
3610 (setq result
(buffer-substring (point) (point-at-eol)))))
3613 (defun tramp-set-remote-path (vec)
3614 "Sets the remote environment PATH to existing directories.
3615 I.e., for each directory in `tramp-remote-path', it is tested
3616 whether it exists and if so, it is added to the environment
3618 (tramp-message vec
5 (format "Setting $PATH environment variable"))
3620 vec
(format "PATH=%s; export PATH"
3621 (mapconcat 'identity
(tramp-get-remote-path vec
) ":"))))
3623 ;; ------------------------------------------------------------
3624 ;; -- Communication with external shell --
3625 ;; ------------------------------------------------------------
3627 (defun tramp-find-file-exists-command (vec)
3628 "Find a command on the remote host for checking if a file exists.
3629 Here, we are looking for a command which has zero exit status if the
3630 file exists and nonzero exit status otherwise."
3631 (let ((existing "/")
3633 (tramp-shell-quote-argument "/ this file does not exist "))
3635 ;; The algorithm is as follows: we try a list of several commands.
3636 ;; For each command, we first run `$cmd /' -- this should return
3637 ;; true, as the root directory always exists. And then we run
3638 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
3639 ;; does not exist. This should return false. We use the first
3640 ;; command we find that seems to work.
3641 ;; The list of commands to try is as follows:
3642 ;; `ls -d' This works on most systems, but NetBSD 1.4
3643 ;; has a bug: `ls' always returns zero exit
3644 ;; status, even for files which don't exist.
3645 ;; `test -e' Some Bourne shells have a `test' builtin
3646 ;; which does not know the `-e' option.
3647 ;; `/bin/test -e' For those, the `test' binary on disk normally
3648 ;; provides the option. Alas, the binary
3649 ;; is sometimes `/bin/test' and sometimes it's
3651 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3654 (and (setq result
(format "%s -e" (tramp-get-test-command vec
)))
3655 (tramp-send-command-and-check
3656 vec
(format "%s %s" result existing
))
3657 (not (tramp-send-command-and-check
3658 vec
(format "%s %s" result nonexistent
)))))
3660 (and (setq result
"/bin/test -e")
3661 (tramp-send-command-and-check
3662 vec
(format "%s %s" result existing
))
3663 (not (tramp-send-command-and-check
3664 vec
(format "%s %s" result nonexistent
)))))
3666 (and (setq result
"/usr/bin/test -e")
3667 (tramp-send-command-and-check
3668 vec
(format "%s %s" result existing
))
3669 (not (tramp-send-command-and-check
3670 vec
(format "%s %s" result nonexistent
)))))
3672 (and (setq result
(format "%s -d" (tramp-get-ls-command vec
)))
3673 (tramp-send-command-and-check
3674 vec
(format "%s %s" result existing
))
3675 (not (tramp-send-command-and-check
3676 vec
(format "%s %s" result nonexistent
))))))
3678 vec
'file-error
"Couldn't find command to check if file exists"))
3681 (defun tramp-open-shell (vec shell
)
3682 "Opens shell SHELL."
3683 (with-tramp-progress-reporter
3684 vec
5 (format "Opening remote shell `%s'" shell
)
3685 ;; Find arguments for this shell.
3686 (let ((tramp-end-of-output tramp-initial-end-of-output
)
3687 (alist tramp-sh-extra-args
)
3689 (while (and alist
(null extra-args
))
3690 (setq item
(pop alist
))
3691 (when (string-match (car item
) shell
)
3692 (setq extra-args
(cdr item
))))
3695 "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s"
3696 (tramp-shell-quote-argument tramp-end-of-output
)
3697 shell
(or extra-args
""))
3699 (tramp-set-connection-property
3700 (tramp-get-connection-process vec
) "remote-shell" shell
)
3703 vec
(format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output
)) t
)
3704 (tramp-send-command vec
"PS2=''" t
)
3705 (tramp-send-command vec
"PS3=''" t
)
3706 (tramp-send-command vec
"PROMPT_COMMAND=''" t
)))
3708 (defun tramp-find-shell (vec)
3709 "Opens a shell on the remote host which groks tilde expansion."
3710 (with-current-buffer (tramp-get-buffer vec
)
3711 (let ((default-shell
3713 (tramp-get-connection-property
3714 (tramp-get-connection-process vec
) "remote-shell" nil
)
3715 (tramp-get-method-parameter
3716 (tramp-file-name-method vec
) 'tramp-remote-shell
)))
3719 (with-tramp-connection-property vec
"remote-shell"
3720 ;; CCC: "root" does not exist always, see QNAP 459.
3721 ;; Which check could we apply instead?
3722 (tramp-send-command vec
"echo ~root" t
)
3723 (if (or (string-match "^~root$" (buffer-string))
3724 ;; The default shell (ksh93) of OpenSolaris and
3725 ;; Solaris is buggy. We've got reports for
3726 ;; "SunOS 5.10" and "SunOS 5.11" so far.
3727 (string-match (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3728 (tramp-get-connection-property
3731 (or (tramp-find-executable
3732 vec
"bash" (tramp-get-remote-path vec
) t t
)
3733 (tramp-find-executable
3734 vec
"ksh" (tramp-get-remote-path vec
) t t
)
3735 ;; Maybe it works at least for some other commands.
3741 "Couldn't find a remote shell which groks tilde "
3742 "expansion, using `%s'")
3747 ;; Open a new shell if needed.
3748 (unless (string-equal shell default-shell
)
3750 vec
5 "Starting remote shell `%s' for tilde expansion" shell
)
3751 (tramp-open-shell vec shell
)))))
3753 ;; Utility functions.
3755 (defun tramp-barf-if-no-shell-prompt (proc timeout
&rest error-args
)
3756 "Wait for shell prompt and barf if none appears.
3757 Looks at process PROC to see if a shell prompt appears in TIMEOUT
3758 seconds. If not, it produces an error message with the given ERROR-ARGS."
3759 (let ((vec (tramp-get-connection-property proc
"vector" nil
)))
3761 (tramp-wait-for-regexp
3764 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern
))
3766 (delete-process proc
)
3767 (apply 'tramp-error-with-buffer
3768 (tramp-get-connection-buffer vec
) vec
'file-error error-args
)))))
3770 (defun tramp-open-connection-setup-interactive-shell (proc vec
)
3771 "Set up an interactive shell.
3772 Mainly sets the prompt and the echo correctly. PROC is the shell
3773 process to set up. VEC specifies the connection."
3774 (let ((tramp-end-of-output tramp-initial-end-of-output
))
3775 ;; It is useful to set the prompt in the following command because
3776 ;; some people have a setting for $PS1 which /bin/sh doesn't know
3777 ;; about and thus /bin/sh will display a strange prompt. For
3778 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
3779 ;; display the current working directory but /bin/sh will display
3780 ;; a dollar sign. The following command line sets $PS1 to a sane
3781 ;; value, and works under Bourne-ish shells as well as csh-like
3782 ;; shells. Daniel Pittman reports that the unusual positioning of
3783 ;; the single quotes makes it work under `rc', too. We also unset
3784 ;; the variable $ENV because that is read by some sh
3785 ;; implementations (eg, bash when called as sh) on startup; this
3786 ;; way, we avoid the startup file clobbering $PS1. $PROMPT_COMMAND
3787 ;; is another way to set the prompt in /bin/bash, it must be
3788 ;; discarded as well.
3791 (or (tramp-get-connection-property vec
"remote-shell" nil
)
3792 (tramp-get-method-parameter
3793 (tramp-file-name-method vec
) 'tramp-remote-shell
)))
3796 (tramp-message vec
5 "Setting up remote shell environment")
3797 (tramp-send-command vec
"stty -inlcr -echo kill '^U' erase '^H'" t
)
3798 ;; Check whether the echo has really been disabled. Some
3799 ;; implementations, like busybox of embedded GNU/Linux, don't
3800 ;; support disabling.
3801 (tramp-send-command vec
"echo foo" t
)
3802 (with-current-buffer (process-buffer proc
)
3803 (goto-char (point-min))
3804 (when (looking-at "echo foo")
3805 (tramp-set-connection-property proc
"remote-echo" t
)
3806 (tramp-message vec
5 "Remote echo still on. Ok.")
3807 ;; Make sure backspaces and their echo are enabled and no line
3808 ;; width magic interferes with them.
3809 (tramp-send-command vec
"stty icanon erase ^H cols 32767" t
))))
3811 (tramp-message vec
5 "Setting shell prompt")
3813 vec
(format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output
)) t
)
3814 (tramp-send-command vec
"PS2=''" t
)
3815 (tramp-send-command vec
"PS3=''" t
)
3816 (tramp-send-command vec
"PROMPT_COMMAND=''" t
)
3818 ;; Try to set up the coding system correctly.
3819 ;; CCC this can't be the right way to do it. Hm.
3820 (tramp-message vec
5 "Determining coding system")
3821 (tramp-send-command vec
"echo foo ; echo bar" t
)
3822 (with-current-buffer (process-buffer proc
)
3823 (goto-char (point-min))
3824 (if (featurep 'mule
)
3825 ;; Use MULE to select the right EOL convention for communicating
3826 ;; with the process.
3827 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc
)
3828 (cons 'undecided
'undecided
)))
3829 cs-decode cs-encode
)
3830 (when (symbolp cs
) (setq cs
(cons cs cs
)))
3831 (setq cs-decode
(car cs
))
3832 (setq cs-encode
(cdr cs
))
3833 (unless cs-decode
(setq cs-decode
'undecided
))
3834 (unless cs-encode
(setq cs-encode
'undecided
))
3835 (setq cs-encode
(tramp-compat-coding-system-change-eol-conversion
3837 (when (search-forward "\r" nil t
)
3838 (setq cs-decode
(tramp-compat-coding-system-change-eol-conversion
3840 (tramp-compat-funcall
3841 'set-buffer-process-coding-system cs-decode cs-encode
)
3843 vec
5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode
))
3844 ;; Look for ^M and do something useful if found.
3845 (when (search-forward "\r" nil t
)
3846 ;; We have found a ^M but cannot frob the process coding system
3847 ;; because we're running on a non-MULE Emacs. Let's try
3849 (tramp-send-command vec
"stty -onlcr" t
))))
3851 (tramp-send-command vec
"set +o vi +o emacs" t
)
3853 ;; Check whether the output of "uname -sr" has been changed. If
3854 ;; yes, this is a strong indication that we must expire all
3855 ;; connection properties. We start again with
3856 ;; `tramp-maybe-open-connection', it will be caught there.
3857 (tramp-message vec
5 "Checking system information")
3858 (let ((old-uname (tramp-get-connection-property vec
"uname" nil
))
3860 (tramp-set-connection-property
3862 (tramp-send-command-and-read vec
"echo \\\"`uname -sr`\\\""))))
3863 (when (and (stringp old-uname
) (not (string-equal old-uname new-uname
)))
3867 "Connection reset, because remote host changed from `%s' to `%s'"
3868 old-uname new-uname
)
3869 (throw 'uname-changed
(tramp-maybe-open-connection vec
))))
3871 ;; Check whether the remote host suffers from buggy
3872 ;; `send-process-string'. This is known for FreeBSD (see comment in
3873 ;; `send_process', file process.c). I've tested sending 624 bytes
3874 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
3875 ;; this host type is detected locally. It cannot handle remote
3877 (with-tramp-connection-property proc
"chunksize"
3879 ((and (integerp tramp-chunksize
) (> tramp-chunksize
0))
3883 vec
5 "Checking remote host type for `send-process-string' bug")
3885 "^FreeBSD" (tramp-get-connection-property vec
"uname" ""))
3888 ;; Set remote PATH variable.
3889 (tramp-set-remote-path vec
)
3891 ;; Search for a good shell before searching for a command which
3892 ;; checks if a file exists. This is done because Tramp wants to use
3893 ;; "test foo; echo $?" to check if various conditions hold, and
3894 ;; there are buggy /bin/sh implementations which don't execute the
3895 ;; "echo $?" part if the "test" part has an error. In particular,
3896 ;; the OpenSolaris /bin/sh is a problem. There are also other
3897 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
3898 ;; in function declarations, or changing HISTFILE in place.
3899 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
3901 (tramp-find-shell vec
)
3903 ;; Disable unexpected output.
3904 (tramp-send-command vec
"mesg n; biff n" t
)
3906 ;; IRIX64 bash expands "!" even when in single quotes. This
3907 ;; destroys our shell functions, we must disable it. See
3908 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
3909 (when (string-match "^IRIX64" (tramp-get-connection-property vec
"uname" ""))
3910 (tramp-send-command vec
"set +H" t
))
3912 ;; On BSD-like systems, ?\t is expanded to spaces. Suppress this.
3913 (when (string-match "BSD\\|Darwin"
3914 (tramp-get-connection-property vec
"uname" ""))
3915 (tramp-send-command vec
"stty -oxtabs" t
))
3917 ;; Set `remote-tty' process property.
3918 (let ((tty (tramp-send-command-and-read vec
"echo \\\"`tty`\\\"" 'noerror
)))
3919 (unless (zerop (length tty
))
3920 (tramp-compat-process-put proc
'remote-tty tty
)))
3922 ;; Dump stty settings in the traces.
3923 (when (>= tramp-verbose
9)
3924 (tramp-send-command vec
"stty -a" t
))
3926 ;; Set the environment.
3927 (tramp-message vec
5 "Setting default environment")
3929 (let ((env (copy-sequence tramp-remote-process-environment
))
3932 (setq item
(tramp-compat-split-string (car env
) "="))
3933 (setcdr item
(mapconcat 'identity
(cdr item
) "="))
3934 (if (and (stringp (cdr item
)) (not (string-equal (cdr item
) "")))
3936 vec
(format "%s=%s; export %s" (car item
) (cdr item
) (car item
)) t
)
3937 (push (car item
) unset
))
3938 (setq env
(cdr env
)))
3941 vec
(format "unset %s" (mapconcat 'identity unset
" ")) t
))))
3943 ;; Old text from documentation of tramp-methods:
3944 ;; Using a uuencode/uudecode inline method is discouraged, please use one
3945 ;; of the base64 methods instead since base64 encoding is much more
3946 ;; reliable and the commands are more standardized between the different
3947 ;; Unix versions. But if you can't use base64 for some reason, please
3948 ;; note that the default uudecode command does not work well for some
3949 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
3950 ;; the following command for uudecode:
3952 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
3954 ;; For Irix, no solution is known yet.
3956 (autoload 'uudecode-decode-region
"uudecode")
3958 (defconst tramp-local-coding-commands
3959 `((b64 base64-encode-region base64-decode-region
)
3960 (uu tramp-uuencode-region uudecode-decode-region
)
3961 (pack ,(format tramp-perl-pack
"perl") ,(format tramp-perl-unpack
"perl")))
3962 "List of local coding commands for inline transfer.
3963 Each item is a list that looks like this:
3965 \(FORMAT ENCODING DECODING\)
3967 FORMAT is symbol describing the encoding/decoding format. It can be
3968 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3970 ENCODING and DECODING can be strings, giving commands, or symbols,
3971 giving functions. If they are strings, then they can contain
3972 the \"%s\" format specifier. If that specifier is present, the input
3973 filename will be put into the command line at that spot. If the
3974 specifier is not present, the input should be read from standard
3977 If they are functions, they will be called with two arguments, start
3978 and end of region, and are expected to replace the region contents
3979 with the encoded or decoded results, respectively.")
3981 (defconst tramp-remote-coding-commands
3982 '((b64 "base64" "base64 -d -i")
3983 ;; "-i" is more robust with older base64 from GNU coreutils.
3984 ;; However, I don't know whether all base64 versions do supports
3986 (b64 "base64" "base64 -d")
3987 (b64 "mimencode -b" "mimencode -u -b")
3988 (b64 "mmencode -b" "mmencode -u -b")
3989 (b64 "recode data..base64" "recode base64..data")
3990 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module
)
3991 (b64 tramp-perl-encode tramp-perl-decode
)
3992 (uu "uuencode xxx" "uudecode -o /dev/stdout" "test -c /dev/stdout")
3993 (uu "uuencode xxx" "uudecode -o -")
3994 (uu "uuencode xxx" "uudecode -p")
3995 (uu "uuencode xxx" tramp-uudecode
)
3996 (pack tramp-perl-pack tramp-perl-unpack
))
3997 "List of remote coding commands for inline transfer.
3998 Each item is a list that looks like this:
4000 \(FORMAT ENCODING DECODING [TEST]\)
4002 FORMAT is symbol describing the encoding/decoding format. It can be
4003 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
4005 ENCODING and DECODING can be strings, giving commands, or symbols,
4006 giving variables. If they are strings, then they can contain
4007 the \"%s\" format specifier. If that specifier is present, the input
4008 filename will be put into the command line at that spot. If the
4009 specifier is not present, the input should be read from standard
4012 If they are variables, this variable is a string containing a Perl
4013 implementation for this functionality. This Perl program will be transferred
4014 to the remote host, and it is available as shell function with the same name.
4016 The optional TEST command can be used for further tests, whether
4017 ENCODING and DECODING are applicable.")
4019 (defun tramp-find-inline-encoding (vec)
4020 "Find an inline transfer encoding that works.
4021 Goes through the list `tramp-local-coding-commands' and
4022 `tramp-remote-coding-commands'."
4024 (let ((local-commands tramp-local-coding-commands
)
4026 (p (tramp-get-connection-process vec
))
4027 loc-enc loc-dec rem-enc rem-dec rem-test litem ritem found
)
4028 (while (and local-commands
(not found
))
4029 (setq litem
(pop local-commands
))
4030 (catch 'wont-work-local
4031 (let ((format (nth 0 litem
))
4032 (remote-commands tramp-remote-coding-commands
))
4033 (setq loc-enc
(nth 1 litem
))
4034 (setq loc-dec
(nth 2 litem
))
4035 ;; If the local encoder or decoder is a string, the
4036 ;; corresponding command has to work locally.
4037 (if (not (stringp loc-enc
))
4039 vec
5 "Checking local encoding function `%s'" loc-enc
)
4041 vec
5 "Checking local encoding command `%s' for sanity" loc-enc
)
4042 (unless (zerop (tramp-call-local-coding-command
4044 (throw 'wont-work-local nil
)))
4045 (if (not (stringp loc-dec
))
4047 vec
5 "Checking local decoding function `%s'" loc-dec
)
4049 vec
5 "Checking local decoding command `%s' for sanity" loc-dec
)
4050 (unless (zerop (tramp-call-local-coding-command
4052 (throw 'wont-work-local nil
)))
4053 ;; Search for remote coding commands with the same format
4054 (while (and remote-commands
(not found
))
4055 (setq ritem
(pop remote-commands
))
4056 (catch 'wont-work-remote
4057 (when (equal format
(nth 0 ritem
))
4058 (setq rem-enc
(nth 1 ritem
))
4059 (setq rem-dec
(nth 2 ritem
))
4060 (setq rem-test
(nth 3 ritem
))
4061 ;; Check the remote test command if exists.
4062 (when (stringp rem-test
)
4064 vec
5 "Checking remote test command `%s'" rem-test
)
4065 (unless (tramp-send-command-and-check vec rem-test t
)
4066 (throw 'wont-work-remote nil
)))
4067 ;; Check if remote encoding and decoding commands can be
4068 ;; called remotely with null input and output. This makes
4069 ;; sure there are no syntax errors and the command is really
4070 ;; found. Note that we do not redirect stdout to /dev/null,
4071 ;; for two reasons: when checking the decoding command, we
4072 ;; actually check the output it gives. And also, when
4073 ;; redirecting "mimencode" output to /dev/null, then as root
4074 ;; it might change the permissions of /dev/null!
4075 (when (not (stringp rem-enc
))
4076 (let ((name (symbol-name rem-enc
)))
4077 (while (string-match (regexp-quote "-") name
)
4078 (setq name
(replace-match "_" nil t name
)))
4079 (tramp-maybe-send-script vec
(symbol-value rem-enc
) name
)
4080 (setq rem-enc name
)))
4083 "Checking remote encoding command `%s' for sanity" rem-enc
)
4084 (unless (tramp-send-command-and-check
4085 vec
(format "%s </dev/null" rem-enc
) t
)
4086 (throw 'wont-work-remote nil
))
4088 (when (not (stringp rem-dec
))
4089 (let ((name (symbol-name rem-dec
)))
4090 (while (string-match (regexp-quote "-") name
)
4091 (setq name
(replace-match "_" nil t name
)))
4092 (tramp-maybe-send-script vec
(symbol-value rem-dec
) name
)
4093 (setq rem-dec name
)))
4096 "Checking remote decoding command `%s' for sanity" rem-dec
)
4097 (unless (tramp-send-command-and-check
4099 (format "echo %s | %s | %s" magic rem-enc rem-dec
)
4101 (throw 'wont-work-remote nil
))
4103 (with-current-buffer (tramp-get-buffer vec
)
4104 (goto-char (point-min))
4105 (unless (looking-at (regexp-quote magic
))
4106 (throw 'wont-work-remote nil
)))
4108 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
4109 (setq rem-enc
(nth 1 ritem
))
4110 (setq rem-dec
(nth 2 ritem
))
4111 (setq found t
)))))))
4113 ;; Did we find something?
4116 vec
'file-error
"Couldn't find an inline transfer encoding"))
4118 ;; Set connection properties. Since the commands are risky (due
4119 ;; to output direction), we cache them in the process cache.
4120 (tramp-message vec
5 "Using local encoding `%s'" loc-enc
)
4121 (tramp-set-connection-property p
"local-encoding" loc-enc
)
4122 (tramp-message vec
5 "Using local decoding `%s'" loc-dec
)
4123 (tramp-set-connection-property p
"local-decoding" loc-dec
)
4124 (tramp-message vec
5 "Using remote encoding `%s'" rem-enc
)
4125 (tramp-set-connection-property p
"remote-encoding" rem-enc
)
4126 (tramp-message vec
5 "Using remote decoding `%s'" rem-dec
)
4127 (tramp-set-connection-property p
"remote-decoding" rem-dec
))))
4129 (defun tramp-call-local-coding-command (cmd input output
)
4130 "Call the local encoding or decoding command.
4131 If CMD contains \"%s\", provide input file INPUT there in command.
4132 Otherwise, INPUT is passed via standard input.
4133 INPUT can also be nil which means `/dev/null'.
4134 OUTPUT can be a string (which specifies a filename), or t (which
4135 means standard output and thus the current buffer), or nil (which
4138 tramp-encoding-shell
4139 (when (and input
(not (string-match "%s" cmd
))) input
)
4140 (if (eq output t
) t nil
)
4142 tramp-encoding-command-switch
4144 (if (string-match "%s" cmd
) (format cmd input
) cmd
)
4145 (if (stringp output
) (concat " >" output
) ""))))
4147 (defconst tramp-inline-compress-commands
4148 '(("gzip" "gzip -d")
4149 ("bzip2" "bzip2 -d")
4151 ("compress" "compress -d"))
4152 "List of compress and decompress commands for inline transfer.
4153 Each item is a list that looks like this:
4155 \(COMPRESS DECOMPRESS\)
4157 COMPRESS or DECOMPRESS are strings with the respective commands.")
4159 (defun tramp-find-inline-compress (vec)
4160 "Find an inline transfer compress command that works.
4161 Goes through the list `tramp-inline-compress-commands'."
4163 (let ((commands tramp-inline-compress-commands
)
4165 (p (tramp-get-connection-process vec
))
4166 item compress decompress found
)
4167 (while (and commands
(not found
))
4169 (setq item
(pop commands
)
4170 compress
(nth 0 item
)
4171 decompress
(nth 1 item
))
4174 "Checking local compress commands `%s', `%s' for sanity"
4175 compress decompress
)
4178 (tramp-call-local-coding-command
4180 ;; Windows shells need the program file name after
4181 ;; the pipe symbol be quoted if they use forward
4182 ;; slashes as directory separators.
4183 (if (memq system-type
'(windows-nt))
4184 "echo %s | \"%s\" | \"%s\""
4185 "echo %s | %s | %s")
4186 magic compress decompress
) nil nil
))
4190 "Checking remote compress commands `%s', `%s' for sanity"
4191 compress decompress
)
4192 (unless (tramp-send-command-and-check
4193 vec
(format "echo %s | %s | %s" magic compress decompress
) t
)
4197 ;; Did we find something?
4200 ;; Set connection properties. Since the commands are
4201 ;; risky (due to output direction), we cache them in the
4204 vec
5 "Using inline transfer compress command `%s'" compress
)
4205 (tramp-set-connection-property p
"inline-compress" compress
)
4207 vec
5 "Using inline transfer decompress command `%s'" decompress
)
4208 (tramp-set-connection-property p
"inline-decompress" decompress
))
4210 (tramp-set-connection-property p
"inline-compress" nil
)
4211 (tramp-set-connection-property p
"inline-decompress" nil
)
4213 vec
2 "Couldn't find an inline transfer compress command")))))
4215 (defvar tramp-gw-tunnel-method
)
4216 (defvar tramp-gw-socks-method
)
4218 (defun tramp-compute-multi-hops (vec)
4219 "Expands VEC according to `tramp-default-proxies-alist'.
4220 Gateway hops are already opened."
4221 (let ((target-alist `(,vec
))
4222 (hops (or (tramp-file-name-hop vec
) ""))
4226 ;; Ad-hoc proxy definitions.
4227 (dolist (proxy (reverse (split-string hops tramp-postfix-hop-regexp
'omit
)))
4228 (let ((user (tramp-file-name-user item
))
4229 (host (tramp-file-name-host item
))
4231 tramp-prefix-format proxy tramp-postfix-host-format
)))
4233 vec
5 "Add proxy (\"%s\" \"%s\" \"%s\")"
4234 (and (stringp host
) (regexp-quote host
))
4235 (and (stringp user
) (regexp-quote user
))
4239 'tramp-default-proxies-alist
4240 (list (and (stringp host
) (regexp-quote host
))
4241 (and (stringp user
) (regexp-quote user
))
4243 (setq item
(tramp-dissect-file-name proxy
))))
4244 ;; Save the new value.
4245 (when (and hops tramp-save-ad-hoc-proxies
)
4246 (customize-save-variable
4247 'tramp-default-proxies-alist tramp-default-proxies-alist
))
4249 ;; Look for proxy hosts to be passed.
4250 (setq choices tramp-default-proxies-alist
)
4252 (setq item
(pop choices
)
4253 proxy
(eval (nth 2 item
)))
4256 (string-match (or (eval (nth 0 item
)) "")
4257 (or (tramp-file-name-host (car target-alist
)) ""))
4259 (string-match (or (eval (nth 1 item
)) "")
4260 (or (tramp-file-name-user (car target-alist
)) "")))
4262 ;; No more hops needed.
4264 ;; Replace placeholders.
4269 ?u
(or (tramp-file-name-user (car target-alist
)) "")
4270 ?h
(or (tramp-file-name-host (car target-alist
)) ""))))
4271 (with-parsed-tramp-file-name proxy l
4273 (add-to-list 'target-alist l
)
4274 ;; Start next search.
4275 (setq choices tramp-default-proxies-alist
)))))
4278 (when (and (boundp 'tramp-gw-tunnel-method
) (boundp 'tramp-gw-socks-method
)
4281 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method
)
4282 (tramp-file-name-method (car target-alist
))))
4283 (let ((gw (pop target-alist
))
4284 (hop (pop target-alist
)))
4285 ;; Is the method prepared for gateways?
4286 (unless (tramp-file-name-port hop
)
4289 "Connection `%s' is not supported for gateway access." hop
))
4290 ;; Open the gateway connection.
4294 (tramp-file-name-method hop
) (tramp-file-name-user hop
)
4295 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop
) nil nil
))
4296 ;; For the password prompt, we need the correct values.
4297 ;; Therefore, we must remember the gateway vector. But we
4298 ;; cannot do it as connection property, because it shouldn't
4299 ;; be persistent. And we have no started process yet either.
4300 (tramp-set-file-property (car target-alist
) "" "gateway" hop
)))
4302 ;; Foreign and out-of-band methods are not supported for multi-hops.
4303 (when (cdr target-alist
)
4304 (setq choices target-alist
)
4306 (setq item
(pop choices
))
4310 (tramp-get-method-parameter
4311 (tramp-file-name-method item
) 'tramp-login-program
))
4312 (tramp-get-method-parameter
4313 (tramp-file-name-method item
) 'tramp-copy-program
))
4316 "Method `%s' is not supported for multi-hops."
4317 (tramp-file-name-method item
)))))
4319 ;; In case the host name is not used for the remote shell
4320 ;; command, the user could be misguided by applying a random
4322 (let* ((v (car target-alist
))
4323 (method (tramp-file-name-method v
))
4324 (host (tramp-file-name-host v
)))
4327 ;; There are multi-hops.
4329 ;; The host name is used for the remote shell command.
4331 '("%h") (tramp-get-method-parameter method
'tramp-login-args
))
4332 ;; The host is local. We cannot use `tramp-local-host-p'
4333 ;; here, because it opens a connection as well.
4334 (string-match tramp-local-host-regexp host
))
4337 "Host `%s' looks like a remote host, `%s' can only use the local host"
4343 (defun tramp-maybe-open-connection (vec)
4344 "Maybe open a connection VEC.
4345 Does not do anything if a connection is already open, but re-opens the
4346 connection if a previous connection has died for some reason."
4347 (catch 'uname-changed
4348 (let ((p (tramp-get-connection-process vec
))
4349 (process-name (tramp-get-connection-property vec
"process-name" nil
))
4350 (process-environment (copy-sequence process-environment
))
4351 (pos (with-current-buffer (tramp-get-connection-buffer vec
) (point))))
4353 ;; If Tramp opens the same connection within a short time frame,
4354 ;; there is a problem. We shall signal this.
4355 (unless (or (and p
(processp p
) (memq (process-status p
) '(run open
)))
4356 (not (equal (butlast (append vec nil
) 2)
4357 (car tramp-current-connection
)))
4359 (current-time) (cdr tramp-current-connection
))
4360 (or tramp-connection-min-time-diff
0)))
4361 (throw 'suppress
'suppress
))
4363 ;; If too much time has passed since last command was sent, look
4364 ;; whether process is still alive. If it isn't, kill it. When
4365 ;; using ssh, it can sometimes happen that the remote end has
4366 ;; hung up but the local ssh client doesn't recognize this until
4367 ;; it tries to send some data to the remote end. So that's why
4368 ;; we try to send a command from time to time, then look again
4369 ;; whether the process is really alive.
4371 (when (and (> (tramp-time-diff
4373 (tramp-get-connection-property
4374 p
"last-cmd-time" '(0 0 0)))
4376 p
(processp p
) (memq (process-status p
) '(run open
)))
4377 (tramp-send-command vec
"echo are you awake" t t
)
4378 (unless (and (memq (process-status p
) '(run open
))
4379 (tramp-wait-for-output p
10))
4380 ;; The error will be caught locally.
4381 (tramp-error vec
'file-error
"Awake did fail")))
4386 ;; New connection must be opened.
4388 (unless (and p
(processp p
) (memq (process-status p
) '(run open
)))
4390 ;; We call `tramp-get-buffer' in order to get a debug
4391 ;; buffer for messages from the beginning.
4392 (tramp-get-buffer vec
)
4394 ;; If `non-essential' is non-nil, don't reopen a new connection.
4395 (when (and (boundp 'non-essential
) (symbol-value 'non-essential
))
4396 (throw 'non-essential
'non-essential
))
4398 (with-tramp-progress-reporter
4400 (if (zerop (length (tramp-file-name-user vec
)))
4401 (format "Opening connection for %s using %s"
4402 (tramp-file-name-host vec
)
4403 (tramp-file-name-method vec
))
4404 (format "Opening connection for %s@%s using %s"
4405 (tramp-file-name-user vec
)
4406 (tramp-file-name-host vec
)
4407 (tramp-file-name-method vec
)))
4409 ;; Start new process.
4410 (when (and p
(processp p
))
4412 (setenv "TERM" tramp-terminal-type
)
4413 (setenv "LC_ALL" "C")
4414 (setenv "PROMPT_COMMAND")
4415 (setenv "PS1" tramp-initial-end-of-output
)
4416 (let* ((target-alist (tramp-compute-multi-hops vec
))
4417 ;; We will apply `tramp-ssh-controlmaster-options'
4418 ;; only for the first hop.
4419 (options tramp-ssh-controlmaster-options
)
4420 (process-connection-type tramp-process-connection-type
)
4421 (process-adaptive-read-buffering nil
)
4422 (coding-system-for-read nil
)
4423 ;; This must be done in order to avoid our file
4425 (p (let ((default-directory
4426 (tramp-compat-temporary-file-directory)))
4429 (tramp-get-connection-name vec
)
4430 (tramp-get-connection-buffer vec
)
4431 (if tramp-encoding-command-interactive
4432 (list tramp-encoding-shell
4433 tramp-encoding-command-interactive
)
4434 (list tramp-encoding-shell
))))))
4436 ;; Set sentinel and query flag.
4437 (tramp-set-connection-property p
"vector" vec
)
4438 (set-process-sentinel p
'tramp-process-sentinel
)
4439 (tramp-compat-set-process-query-on-exit-flag p nil
)
4440 (setq tramp-current-connection
4441 (cons (butlast (append vec nil
) 2) (current-time))
4442 tramp-current-host
(system-name))
4445 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
4447 ;; Check whether process is alive.
4448 (tramp-barf-if-no-shell-prompt
4450 "Couldn't find local shell prompt for %s" tramp-encoding-shell
)
4452 ;; Now do all the connections as specified.
4454 (let* ((hop (car target-alist
))
4455 (l-method (tramp-file-name-method hop
))
4456 (l-user (tramp-file-name-user hop
))
4457 (l-host (tramp-file-name-host hop
))
4460 (tramp-get-method-parameter
4461 l-method
'tramp-login-program
))
4463 (tramp-get-method-parameter
4464 l-method
'tramp-login-args
))
4466 (tramp-get-method-parameter
4467 l-method
'tramp-async-args
))
4469 (tramp-get-method-parameter
4470 l-method
'tramp-connection-timeout
))
4472 (tramp-get-method-parameter l-method
'tramp-gw-args
))
4473 (gw (tramp-get-file-property hop
"" "gateway" nil
))
4474 (g-method (and gw
(tramp-file-name-method gw
)))
4475 (g-user (and gw
(tramp-file-name-user gw
)))
4476 (g-host (and gw
(tramp-file-name-real-host gw
)))
4477 (command login-program
)
4478 ;; We don't create the temporary file. In
4479 ;; fact, it is just a prefix for the
4480 ;; ControlPath option of ssh; the real
4481 ;; temporary file has another name, and it is
4482 ;; created and protected by ssh. It is also
4483 ;; removed by ssh when the connection is
4484 ;; closed. The temporary file name is cached
4485 ;; in the main connection process, therefore
4486 ;; we cannot use `tramp-get-connection-process'.
4488 (with-tramp-connection-property
4489 (get-process (tramp-buffer-name vec
)) "temp-file"
4492 tramp-temp-name-prefix
4493 (tramp-compat-temporary-file-directory)))))
4496 ;; Add arguments for asynchronous processes.
4497 (when (and process-name async-args
)
4498 (setq login-args
(append async-args login-args
)))
4500 ;; Add gateway arguments if necessary.
4501 (when (and gw gw-args
)
4502 (setq login-args
(append gw-args login-args
)))
4504 ;; Check for port number. Until now, there's no
4505 ;; need for handling like method, user, host.
4506 (when (string-match tramp-host-with-port-regexp l-host
)
4507 (setq l-port
(match-string 2 l-host
)
4508 l-host
(match-string 1 l-host
)))
4510 ;; Check, whether there is a restricted shell.
4511 (dolist (elt tramp-restricted-shell-hosts-alist
)
4512 (when (string-match elt tramp-current-host
)
4515 ;; Set variables for computing the prompt for
4516 ;; reading password. They can also be derived
4518 (setq tramp-current-method
(or g-method l-method
)
4519 tramp-current-user
(or g-user l-user
)
4520 tramp-current-host
(or g-host l-host
))
4522 ;; Replace login-args place holders.
4524 l-host
(or l-host
"")
4525 l-user
(or l-user
"")
4526 l-port
(or l-port
"")
4527 spec
(format-spec-make ?t tmpfile
)
4528 options
(format-spec options spec
)
4529 spec
(format-spec-make
4530 ?h l-host ?u l-user ?p l-port ?c options
)
4533 ;; We do not want to see the trailing local
4534 ;; prompt in `start-file-process'.
4535 (unless r-shell
"exec ")
4539 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
4540 (unless (member "" x
) (mapconcat 'identity x
" ")))
4542 ;; Local shell could be a Windows COMSPEC. It
4543 ;; doesn't know the ";" syntax, but we must exit
4544 ;; always for `start-file-process'. It could
4545 ;; also be a restricted shell, which does not
4547 (when r-shell
" && exit || exit")))
4549 ;; Send the command.
4550 (tramp-message vec
3 "Sending command `%s'" command
)
4551 (tramp-send-command vec command t t
)
4552 (tramp-process-actions
4553 p vec pos tramp-actions-before-shell
4554 (or connection-timeout tramp-connection-timeout
))
4556 vec
3 "Found remote shell prompt on `%s'" l-host
))
4559 target-alist
(cdr target-alist
)))
4561 ;; Make initial shell settings.
4562 (tramp-open-connection-setup-interactive-shell p vec
))))
4564 ;; When the user did interrupt, we must cleanup.
4567 ;; Propagate the quit signal.
4568 (signal (car err
) (cdr err
)))))))
4570 (defun tramp-send-command (vec command
&optional neveropen nooutput
)
4571 "Send the COMMAND to connection VEC.
4572 Erases temporary buffer before sending the command. If optional
4573 arg NEVEROPEN is non-nil, never try to open the connection. This
4574 is meant to be used from `tramp-maybe-open-connection' only. The
4575 function waits for output unless NOOUTPUT is set."
4576 (unless neveropen
(tramp-maybe-open-connection vec
))
4577 (let ((p (tramp-get-connection-process vec
)))
4578 (when (tramp-get-connection-property p
"remote-echo" nil
)
4579 ;; We mark the command string that it can be erased in the output buffer.
4580 (tramp-set-connection-property p
"check-remote-echo" t
)
4581 (setq command
(format "%s%s%s" tramp-echo-mark command tramp-echo-mark
)))
4582 ;; Some busyboxes tend to close the connection when we use the
4583 ;; following syntax for here-documents. This we cannot test; it
4584 ;; shall be set via `tramp-connection-properties'.
4585 (when (and (string-match "<<'EOF'" command
)
4586 (not (tramp-get-connection-property vec
"busybox" nil
)))
4587 ;; Unset $PS1 when using here documents, in order to avoid
4588 ;; multiple prompts.
4589 (setq command
(concat "(PS1= ; " command
"\n)")))
4590 ;; Send the command.
4591 (tramp-message vec
6 "%s" command
)
4592 (tramp-send-string vec command
)
4593 (unless nooutput
(tramp-wait-for-output p
))))
4595 (defun tramp-wait-for-output (proc &optional timeout
)
4596 "Wait for output from remote command."
4597 (unless (buffer-live-p (process-buffer proc
))
4598 (delete-process proc
)
4599 (tramp-error proc
'file-error
"Process `%s' not available, try again" proc
))
4600 (with-current-buffer (process-buffer proc
)
4601 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
4602 ;; be leading escape sequences, which must be ignored.
4603 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output
)))
4604 ;; Sometimes, the commands do not return a newline but a
4605 ;; null byte before the shell prompt, for example "git
4606 ;; ls-files -c -z ...".
4607 (regexp1 (format "\\(^\\|\000\\)%s" regexp
))
4608 (found (tramp-wait-for-regexp proc timeout regexp1
)))
4610 (let (buffer-read-only)
4611 ;; A simple-minded busybox has sent " ^H" sequences.
4613 (goto-char (point-min))
4614 (when (re-search-forward "^\\(.\b\\)+$" (point-at-eol) t
)
4616 (delete-region (point-min) (point)))
4617 ;; Delete the prompt.
4618 (goto-char (point-max))
4619 (re-search-backward regexp nil t
)
4620 (delete-region (point) (point-max)))
4624 "[[Remote prompt `%s' not found in %d secs]]"
4625 tramp-end-of-output timeout
)
4628 "[[Remote prompt `%s' not found]]" tramp-end-of-output
)))
4629 ;; Return value is whether end-of-output sentinel was found.
4632 (defun tramp-send-command-and-check
4633 (vec command
&optional subshell dont-suppress-err
)
4634 "Run COMMAND and check its exit status.
4635 Sends `echo $?' along with the COMMAND for checking the exit status. If
4636 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
4638 If the optional argument SUBSHELL is non-nil, the command is
4639 executed in a subshell, ie surrounded by parentheses. If
4640 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
4643 (concat (if subshell
"( " "")
4645 (if command
(if dont-suppress-err
"; " " 2>/dev/null; ") "")
4646 "echo tramp_exit_status $?"
4647 (if subshell
" )" "")))
4648 (with-current-buffer (tramp-get-connection-buffer vec
)
4649 (goto-char (point-max))
4650 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
4652 vec
'file-error
"Couldn't find exit status of `%s'" command
))
4653 (skip-chars-forward "^ ")
4655 (zerop (read (current-buffer)))
4656 (let (buffer-read-only)
4657 (delete-region (match-beginning 0) (point-max))))))
4659 (defun tramp-barf-unless-okay (vec command fmt
&rest args
)
4660 "Run COMMAND, check exit status, throw error if exit status not okay.
4661 Similar to `tramp-send-command-and-check' but accepts two more arguments
4662 FMT and ARGS which are passed to `error'."
4663 (or (tramp-send-command-and-check vec command
)
4664 (apply 'tramp-error vec
'file-error fmt args
)))
4666 (defun tramp-send-command-and-read (vec command
&optional noerror
)
4667 "Run COMMAND and return the output, which must be a Lisp expression.
4668 In case there is no valid Lisp expression and NOERROR is nil, it
4671 (tramp-send-command-and-check vec command
)
4672 (tramp-barf-unless-okay
4673 vec command
"`%s' returns with error" command
))
4674 (with-current-buffer (tramp-get-connection-buffer vec
)
4675 ;; Read the expression.
4676 (goto-char (point-min))
4678 (prog1 (read (current-buffer))
4680 (when (re-search-forward "\\S-" (point-at-eol) t
)
4682 (error (unless noerror
4685 "`%s' does not return a valid Lisp expression: `%s'"
4686 command
(buffer-string))))))))
4688 (defun tramp-convert-file-attributes (vec attr
)
4689 "Convert `file-attributes' ATTR generated by perl script, stat or ls.
4690 Convert file mode bits to string and set virtual device number.
4693 ;; Remove color escape sequences from symlink.
4694 (when (stringp (car attr
))
4695 (while (string-match tramp-color-escape-sequence-regexp
(car attr
))
4696 (setcar attr
(replace-match "" nil nil
(car attr
)))))
4697 ;; Convert uid and gid. Use -1 as indication of unusable value.
4698 (when (and (numberp (nth 2 attr
)) (< (nth 2 attr
) 0))
4699 (setcar (nthcdr 2 attr
) -
1))
4700 (when (and (floatp (nth 2 attr
))
4701 (<= (nth 2 attr
) (tramp-compat-most-positive-fixnum)))
4702 (setcar (nthcdr 2 attr
) (round (nth 2 attr
))))
4703 (when (and (numberp (nth 3 attr
)) (< (nth 3 attr
) 0))
4704 (setcar (nthcdr 3 attr
) -
1))
4705 (when (and (floatp (nth 3 attr
))
4706 (<= (nth 3 attr
) (tramp-compat-most-positive-fixnum)))
4707 (setcar (nthcdr 3 attr
) (round (nth 3 attr
))))
4708 ;; Convert last access time.
4709 (unless (listp (nth 4 attr
))
4710 (setcar (nthcdr 4 attr
)
4711 (list (floor (nth 4 attr
) 65536)
4712 (floor (mod (nth 4 attr
) 65536)))))
4713 ;; Convert last modification time.
4714 (unless (listp (nth 5 attr
))
4715 (setcar (nthcdr 5 attr
)
4716 (list (floor (nth 5 attr
) 65536)
4717 (floor (mod (nth 5 attr
) 65536)))))
4718 ;; Convert last status change time.
4719 (unless (listp (nth 6 attr
))
4720 (setcar (nthcdr 6 attr
)
4721 (list (floor (nth 6 attr
) 65536)
4722 (floor (mod (nth 6 attr
) 65536)))))
4723 ;; Convert file size.
4724 (when (< (nth 7 attr
) 0)
4725 (setcar (nthcdr 7 attr
) -
1))
4726 (when (and (floatp (nth 7 attr
))
4727 (<= (nth 7 attr
) (tramp-compat-most-positive-fixnum)))
4728 (setcar (nthcdr 7 attr
) (round (nth 7 attr
))))
4729 ;; Convert file mode bits to string.
4730 (unless (stringp (nth 8 attr
))
4731 (setcar (nthcdr 8 attr
) (tramp-file-mode-from-int (nth 8 attr
)))
4732 (when (stringp (car attr
))
4733 (aset (nth 8 attr
) 0 ?l
)))
4734 ;; Convert directory indication bit.
4735 (when (string-match "^d" (nth 8 attr
))
4737 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
4738 (when (consp (car attr
))
4739 (if (and (stringp (caar attr
))
4740 (string-match ".+ -> .\\(.+\\)." (caar attr
)))
4741 (setcar attr
(match-string 1 (caar attr
)))
4743 ;; Set file's gid change bit.
4744 (setcar (nthcdr 9 attr
)
4745 (if (numberp (nth 3 attr
))
4746 (not (= (nth 3 attr
)
4747 (tramp-get-remote-gid vec
'integer
)))
4750 (tramp-get-remote-gid vec
'string
)))))
4752 (unless (listp (nth 10 attr
))
4753 (setcar (nthcdr 10 attr
)
4755 (cons (floor (nth 10 attr
) 65536)
4756 (floor (mod (nth 10 attr
) 65536)))
4757 ;; Inodes can be incredible huge. We must hide this.
4758 (error (tramp-get-inode vec
)))))
4759 ;; Set virtual device number.
4760 (setcar (nthcdr 11 attr
)
4761 (tramp-get-device vec
))
4764 (defun tramp-shell-case-fold (string)
4765 "Converts STRING to shell glob pattern which ignores case."
4768 (if (equal (downcase c
) (upcase c
))
4770 (format "[%c%c]" (downcase c
) (upcase c
))))
4774 (defun tramp-make-copy-program-file-name (vec)
4775 "Create a file name suitable to be passed to `rcp' and workalikes."
4776 (let ((user (tramp-file-name-user vec
))
4777 (host (tramp-file-name-real-host vec
))
4778 (localname (tramp-shell-quote-argument
4779 (tramp-file-name-localname vec
))))
4780 (if (not (zerop (length user
)))
4781 (format "%s@%s:%s" user host localname
)
4782 (format "%s:%s" host localname
))))
4784 (defun tramp-method-out-of-band-p (vec size
)
4785 "Return t if this is an out-of-band method, nil otherwise."
4787 ;; It shall be an out-of-band method.
4788 (tramp-get-method-parameter (tramp-file-name-method vec
) 'tramp-copy-program
)
4789 ;; There must be a size, otherwise the file doesn't exist.
4791 ;; Either the file size is large enough, or (in rare cases) there
4792 ;; does not exist a remote encoding.
4793 (or (null tramp-copy-size-limit
)
4794 (> size tramp-copy-size-limit
)
4795 (null (tramp-get-inline-coding vec
"remote-encoding" size
)))))
4797 ;; Variables local to connection.
4799 (defun tramp-get-remote-path (vec)
4800 (with-tramp-connection-property
4801 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
4802 ;; cache the result for the session only. Otherwise, the result
4803 ;; is cached persistently.
4804 (if (memq 'tramp-own-remote-path tramp-remote-path
)
4805 (tramp-get-connection-process vec
)
4808 (let* ((remote-path (copy-tree tramp-remote-path
))
4809 (elt1 (memq 'tramp-default-remote-path remote-path
))
4810 (elt2 (memq 'tramp-own-remote-path remote-path
))
4811 (default-remote-path
4814 (tramp-send-command-and-read
4815 vec
"echo \\\"`getconf PATH 2>/dev/null`\\\"" 'noerror
)
4816 ;; Default if "getconf" is not available.
4820 "`getconf PATH' not successful, using default value \"%s\"."
4826 (tramp-send-command-and-read vec
"echo \\\"$PATH\\\"")
4829 vec
3 "$PATH not set, ignoring `tramp-own-remote-path'.")
4832 ;; Replace place holder `tramp-default-remote-path'.
4836 (tramp-compat-split-string default-remote-path
":")
4838 (setq remote-path
(delq 'tramp-default-remote-path remote-path
)))
4840 ;; Replace place holder `tramp-own-remote-path'.
4844 (tramp-compat-split-string own-remote-path
":")
4846 (setq remote-path
(delq 'tramp-own-remote-path remote-path
)))
4848 ;; Remove double entries.
4849 (setq elt1 remote-path
)
4851 (while (and (car elt1
) (setq elt2
(member (car elt1
) (cdr elt1
))))
4853 (setq elt1
(cdr elt1
)))
4855 ;; Remove non-existing directories.
4863 (tramp-make-tramp-file-name
4864 (tramp-file-name-method vec
)
4865 (tramp-file-name-user vec
)
4866 (tramp-file-name-host vec
)
4871 (defun tramp-get-ls-command (vec)
4872 (with-tramp-connection-property vec
"ls"
4873 (tramp-message vec
5 "Finding a suitable `ls' command")
4876 (dolist (cmd '("ls" "gnuls" "gls"))
4877 (let ((dl (tramp-get-remote-path vec
))
4879 (while (and dl
(setq result
(tramp-find-executable vec cmd dl t t
)))
4880 ;; Check parameters. On busybox, "ls" output coloring is
4881 ;; enabled by default sometimes. So we try to disable it
4882 ;; when possible. $LS_COLORING is not supported there.
4883 ;; Some "ls" versions are sensible wrt the order of
4884 ;; arguments, they fail when "-al" is after the
4885 ;; "--color=never" argument (for example on FreeBSD).
4886 (when (tramp-send-command-and-check
4887 vec
(format "%s -lnd /" result
))
4888 (when (tramp-send-command-and-check
4890 "%s --color=never -al /dev/null" result
))
4891 (setq result
(concat result
" --color=never")))
4892 (throw 'ls-found result
))
4893 (setq dl
(cdr dl
))))))
4894 (tramp-error vec
'file-error
"Couldn't find a proper `ls' command"))))
4896 (defun tramp-get-ls-command-with-dired (vec)
4898 (with-tramp-connection-property vec
"ls-dired"
4899 (tramp-message vec
5 "Checking, whether `ls --dired' works")
4900 ;; Some "ls" versions are sensible wrt the order of arguments,
4901 ;; they fail when "-al" is after the "--dired" argument (for
4902 ;; example on FreeBSD).
4903 (tramp-send-command-and-check
4904 vec
(format "%s --dired -al /dev/null" (tramp-get-ls-command vec
))))))
4906 (defun tramp-get-test-command (vec)
4907 (with-tramp-connection-property vec
"test"
4908 (tramp-message vec
5 "Finding a suitable `test' command")
4909 (if (tramp-send-command-and-check vec
"test 0")
4911 (tramp-find-executable vec
"test" (tramp-get-remote-path vec
)))))
4913 (defun tramp-get-test-nt-command (vec)
4914 ;; Does `test A -nt B' work? Use abominable `find' construct if it
4915 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
4916 ;; for otherwise the shell crashes.
4917 (with-tramp-connection-property vec
"test-nt"
4921 vec
(format "( %s / -nt / )" (tramp-get-test-command vec
)))
4922 (with-current-buffer (tramp-get-buffer vec
)
4923 (goto-char (point-min))
4924 (when (looking-at (regexp-quote tramp-end-of-output
))
4925 (format "%s %%s -nt %%s" (tramp-get-test-command vec
)))))
4930 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
4931 (tramp-get-test-command vec
)))
4932 "tramp_test_nt %s %s"))))
4934 (defun tramp-get-file-exists-command (vec)
4935 (with-tramp-connection-property vec
"file-exists"
4936 (tramp-message vec
5 "Finding command to check if file exists")
4937 (tramp-find-file-exists-command vec
)))
4939 (defun tramp-get-remote-ln (vec)
4940 (with-tramp-connection-property vec
"ln"
4941 (tramp-message vec
5 "Finding a suitable `ln' command")
4942 (tramp-find-executable vec
"ln" (tramp-get-remote-path vec
))))
4944 (defun tramp-get-remote-perl (vec)
4945 (with-tramp-connection-property vec
"perl"
4946 (tramp-message vec
5 "Finding a suitable `perl' command")
4948 (or (tramp-find-executable vec
"perl5" (tramp-get-remote-path vec
))
4949 (tramp-find-executable
4950 vec
"perl" (tramp-get-remote-path vec
)))))
4951 ;; We must check also for some Perl modules.
4953 (with-tramp-connection-property vec
"perl-file-spec"
4954 (tramp-send-command-and-check
4955 vec
(format "%s -e 'use File::Spec;'" result
)))
4956 (with-tramp-connection-property vec
"perl-cwd-realpath"
4957 (tramp-send-command-and-check
4958 vec
(format "%s -e 'use Cwd \"realpath\";'" result
))))
4961 (defun tramp-get-remote-stat (vec)
4962 (with-tramp-connection-property vec
"stat"
4963 (tramp-message vec
5 "Finding a suitable `stat' command")
4964 (let ((result (tramp-find-executable
4965 vec
"stat" (tramp-get-remote-path vec
)))
4967 ;; Check whether stat(1) returns usable syntax. "%s" does not
4968 ;; work on older AIX systems.
4971 (tramp-send-command-and-read
4972 vec
(format "%s -c '(\"%%N\" %%s)' /" result
) 'noerror
))
4973 (unless (and (listp tmp
) (stringp (car tmp
))
4974 (string-match "^./.$" (car tmp
))
4975 (integerp (cadr tmp
)))
4979 (defun tramp-get-remote-readlink (vec)
4980 (with-tramp-connection-property vec
"readlink"
4981 (tramp-message vec
5 "Finding a suitable `readlink' command")
4982 (let ((result (tramp-find-executable
4983 vec
"readlink" (tramp-get-remote-path vec
))))
4985 (tramp-send-command-and-check
4986 vec
(format "%s --canonicalize-missing /" result
)))
4989 (defun tramp-get-remote-trash (vec)
4990 (with-tramp-connection-property vec
"trash"
4991 (tramp-message vec
5 "Finding a suitable `trash' command")
4992 (tramp-find-executable vec
"trash" (tramp-get-remote-path vec
))))
4994 (defun tramp-get-remote-gvfs-monitor-dir (vec)
4995 (with-tramp-connection-property vec
"gvfs-monitor-dir"
4996 (tramp-message vec
5 "Finding a suitable `gvfs-monitor-dir' command")
4997 (tramp-find-executable
4998 vec
"gvfs-monitor-dir" (tramp-get-remote-path vec
) t t
)))
5000 (defun tramp-get-remote-inotifywait (vec)
5001 (with-tramp-connection-property vec
"inotifywait"
5002 (tramp-message vec
5 "Finding a suitable `inotifywait' command")
5003 (tramp-find-executable vec
"inotifywait" (tramp-get-remote-path vec
) t t
)))
5005 (defun tramp-get-remote-id (vec)
5006 (with-tramp-connection-property vec
"id"
5007 (tramp-message vec
5 "Finding POSIX `id' command")
5010 (let ((dl (tramp-get-remote-path vec
))
5012 (while (and dl
(setq result
(tramp-find-executable vec
"id" dl t t
)))
5013 ;; Check POSIX parameter.
5014 (when (tramp-send-command-and-check vec
(format "%s -u" result
))
5015 (throw 'id-found result
))
5016 (setq dl
(cdr dl
)))))
5017 (tramp-error vec
'file-error
"Couldn't find a POSIX `id' command"))))
5019 (defun tramp-get-remote-uid (vec id-format
)
5020 (with-tramp-connection-property vec
(format "uid-%s" id-format
)
5021 (let ((res (tramp-send-command-and-read
5023 (format "%s -u%s %s"
5024 (tramp-get-remote-id vec
)
5025 (if (equal id-format
'integer
) "" "n")
5026 (if (equal id-format
'integer
)
5027 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
5028 ;; The command might not always return a number.
5029 (if (and (equal id-format
'integer
) (not (integerp res
))) -
1 res
))))
5031 (defun tramp-get-remote-gid (vec id-format
)
5032 (with-tramp-connection-property vec
(format "gid-%s" id-format
)
5033 (let ((res (tramp-send-command-and-read
5035 (format "%s -g%s %s"
5036 (tramp-get-remote-id vec
)
5037 (if (equal id-format
'integer
) "" "n")
5038 (if (equal id-format
'integer
)
5039 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
5040 ;; The command might not always return a number.
5041 (if (and (equal id-format
'integer
) (not (integerp res
))) -
1 res
))))
5043 ;; Some predefined connection properties.
5044 (defun tramp-get-inline-compress (vec prop size
)
5045 "Return the compress command related to PROP.
5046 PROP is either `inline-compress' or `inline-decompress'. SIZE is
5047 the length of the file to be compressed.
5049 If no corresponding command is found, nil is returned."
5050 (when (and (integerp tramp-inline-compress-start-size
)
5051 (> size tramp-inline-compress-start-size
))
5052 (with-tramp-connection-property (tramp-get-connection-process vec
) prop
5053 (tramp-find-inline-compress vec
)
5054 (tramp-get-connection-property
5055 (tramp-get-connection-process vec
) prop nil
))))
5057 (defun tramp-get-inline-coding (vec prop size
)
5058 "Return the coding command related to PROP.
5059 PROP is either `remote-encoding', `remote-decoding',
5060 `local-encoding' or `local-decoding'.
5062 SIZE is the length of the file to be coded. Depending on SIZE,
5063 compression might be applied.
5065 If no corresponding command is found, nil is returned.
5066 Otherwise, either a string is returned which contains a `%s' mark
5067 to be used for the respective input or output file; or a Lisp
5068 function cell is returned to be applied on a buffer."
5069 ;; We must catch the errors, because we want to return `nil', when
5070 ;; no inline coding is found.
5073 (with-tramp-connection-property
5074 (tramp-get-connection-process vec
) prop
5075 (tramp-find-inline-encoding vec
)
5076 (tramp-get-connection-property
5077 (tramp-get-connection-process vec
) prop nil
)))
5078 (prop1 (if (string-match "encoding" prop
)
5079 "inline-compress" "inline-decompress"))
5081 ;; The connection property might have been cached. So we must
5082 ;; send the script to the remote side - maybe.
5083 (when (and coding
(symbolp coding
) (string-match "remote" prop
))
5084 (let ((name (symbol-name coding
)))
5085 (while (string-match (regexp-quote "-") name
)
5086 (setq name
(replace-match "_" nil t name
)))
5087 (tramp-maybe-send-script vec
(symbol-value coding
) name
)
5088 (setq coding name
)))
5090 ;; Check for the `compress' command.
5091 (setq compress
(tramp-get-inline-compress vec prop1 size
))
5092 ;; Return the value.
5094 ((and compress
(symbolp coding
))
5095 (if (string-match "decompress" prop1
)
5098 (let ((coding-system-for-write 'binary
)
5099 (coding-system-for-read 'binary
))
5101 'call-process-region
(point-min) (point-max)
5102 (car (split-string ,compress
)) t t nil
5103 (cdr (split-string ,compress
)))))
5105 (let ((coding-system-for-write 'binary
)
5106 (coding-system-for-read 'binary
))
5108 'call-process-region beg end
5109 (car (split-string ,compress
)) t t nil
5110 (cdr (split-string ,compress
))))
5111 (,coding
(point-min) (point-max)))))
5114 ((and compress
(string-match "decoding" prop
))
5116 ;; Windows shells need the program file name after
5117 ;; the pipe symbol be quoted if they use forward
5118 ;; slashes as directory separators.
5120 ((and (string-match "local" prop
)
5121 (memq system-type
'(windows-nt)))
5123 ((string-match "local" prop
) "(%s | %s)")
5124 (t "(%s | %s >%%s)"))
5128 ;; Windows shells need the program file name after
5129 ;; the pipe symbol be quoted if they use forward
5130 ;; slashes as directory separators.
5131 (if (and (string-match "local" prop
)
5132 (memq system-type
'(windows-nt)))
5133 "(%s <%%s | \"%s\")"
5136 ((string-match "decoding" prop
)
5138 ((string-match "local" prop
) (format "%s" coding
))
5139 (t (format "%s >%%s" coding
))))
5141 (format "%s <%%s" coding
)))))))
5143 (add-hook 'tramp-unload-hook
5145 (unload-feature 'tramp-sh
'force
)))
5151 ;; * Don't use globbing for directories with many files, as this is
5152 ;; likely to produce long command lines, and some shells choke on
5153 ;; long command lines.
5154 ;; * Make it work for different encodings, and for different file name
5155 ;; encodings, too. (Daniel Pittman)
5156 ;; * Don't search for perl5 and perl. Instead, only search for perl and
5157 ;; then look if it's the right version (with `perl -v').
5158 ;; * When editing a remote CVS controlled file as a different user, VC
5159 ;; gets confused about the file locking status. Try to find out why
5160 ;; the workaround doesn't work.
5161 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5162 ;; until the last but one hop via `start-file-process'. Apply it
5163 ;; also for ftp and smb.
5164 ;; * WIBNI if we had a command "trampclient"? If I was editing in
5165 ;; some shell with root privileges, it would be nice if I could
5167 ;; trampclient filename.c
5168 ;; as an editor, and the _current_ shell would connect to an Emacs
5169 ;; server and would be used in an existing non-privileged Emacs
5170 ;; session for doing the editing in question.
5171 ;; That way, I need not tell Emacs my password again and be afraid
5172 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
5173 ;; once display a just typed password in the context of a keyboard
5174 ;; sequence prompt for a question immediately following in a shell
5175 ;; script run within Emacs -- nasty).
5176 ;; And if I have some ssh session running to a different computer,
5177 ;; having the possibility of passing a local file there to a local
5178 ;; Emacs session (in case I can arrange for a connection back) would
5180 ;; Likely the corresponding Tramp server should not allow the
5181 ;; equivalent of the emacsclient -eval option in order to make this
5182 ;; reasonably unproblematic. And maybe trampclient should have some
5183 ;; way of passing credentials, like by using an SSL socket or
5184 ;; something. (David Kastrup)
5185 ;; * Reconnect directly to a compliant shell without first going
5186 ;; through the user's default shell. (Pete Forman)
5187 ;; * How can I interrupt the remote process with a signal
5188 ;; (interrupt-process seems not to work)? (Markus Triska)
5189 ;; * Avoid the local shell entirely for starting remote processes. If
5190 ;; so, I think even a signal, when delivered directly to the local
5191 ;; SSH instance, would correctly be propagated to the remote process
5192 ;; automatically; possibly SSH would have to be started with
5193 ;; "-t". (Markus Triska)
5194 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5195 ;; isn't on the remote host. (Mark A. Hershberger)
5196 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
5197 ;; * Optimize out-of-band copying when both methods are scp-like (not
5199 ;; * Keep a second connection open for out-of-band methods like scp or
5201 ;; * Try telnet+curl as new method. It might be useful for busybox,
5202 ;; without built-in uuencode/uudecode.
5204 ;;; tramp-sh.el ends here