1 ;;; tramp-sh.el --- Tramp access functions for (s)sh-like connections
3 ;; Copyright (C) 1998-2014 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/>.
31 ;; Pacify byte-compiler.
35 (defvar directory-sep-char
)
36 (defvar tramp-gw-tunnel-method
)
37 (defvar tramp-gw-socks-method
)
38 (defvar vc-handled-backends
)
39 (defvar vc-bzr-program
)
40 (defvar vc-git-program
)
41 (defvar vc-hg-program
)
43 (defcustom tramp-inline-compress-start-size
4096
44 "The minimum size of compressing where inline transfer.
45 When inline transfer, compress transferred data of file
46 whose size is this value or above (up to `tramp-copy-size-limit').
47 If it is nil, no compression at all will be applied."
49 :type
'(choice (const nil
) integer
))
51 (defcustom tramp-copy-size-limit
10240
52 "The maximum file size where inline copying is preferred over an \
54 If it is nil, out-of-the-band copy will be used without a check."
56 :type
'(choice (const nil
) integer
))
59 (defcustom tramp-terminal-type
"dumb"
60 "Value of TERM environment variable for logging in to remote host.
61 Because Tramp wants to parse the output of the remote shell, it is easily
62 confused by ANSI color escape sequences and suchlike. Often, shell init
63 files conditionalize this setup based on the TERM environment variable."
68 (defconst tramp-color-escape-sequence-regexp
"\e[[;0-9]+m"
69 "Escape sequences produced by the \"ls\" command.")
71 ;; ksh on OpenBSD 4.5 requires that $PS1 contains a `#' character for
72 ;; root users. It uses the `$' character for other users. In order
73 ;; to guarantee a proper prompt, we use "#$ " for the prompt.
75 (defvar tramp-end-of-output
78 (md5 (concat (prin1-to-string process-environment
) (current-time-string))))
79 "String used to recognize end of output.
80 The '$' character at the end is quoted; the string cannot be
81 detected as prompt when being sent on echoing hosts, therefore.")
84 (defconst tramp-initial-end-of-output
"#$ "
85 "Prompt when establishing a connection.")
87 (defconst tramp-end-of-heredoc
(md5 tramp-end-of-output
)
88 "String used to recognize end of heredoc strings.")
90 ;; Initialize `tramp-methods' with the supported methods.
92 (add-to-list 'tramp-methods
94 (tramp-login-program "rsh")
95 (tramp-login-args (("%h") ("-l" "%u")))
96 (tramp-remote-shell "/bin/sh")
97 (tramp-remote-shell-args ("-c"))
98 (tramp-copy-program "rcp")
99 (tramp-copy-args (("-p" "%k") ("-r")))
100 (tramp-copy-keep-date t
)
101 (tramp-copy-recursive t
)))
103 (add-to-list 'tramp-methods
105 (tramp-login-program "remsh")
106 (tramp-login-args (("%h") ("-l" "%u")))
107 (tramp-remote-shell "/bin/sh")
108 (tramp-remote-shell-args ("-c"))
109 (tramp-copy-program "rcp")
110 (tramp-copy-args (("-p" "%k")))
111 (tramp-copy-keep-date t
)))
113 (add-to-list 'tramp-methods
115 (tramp-login-program "ssh")
116 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
117 ("-e" "none") ("%h")))
118 (tramp-async-args (("-q")))
119 (tramp-remote-shell "/bin/sh")
120 (tramp-remote-shell-args ("-c"))
121 (tramp-copy-program "scp")
122 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r") ("%c")))
123 (tramp-copy-keep-date t
)
124 (tramp-copy-recursive t
)
125 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
126 ("-o" "UserKnownHostsFile=/dev/null")
127 ("-o" "StrictHostKeyChecking=no")))
128 (tramp-default-port 22)))
130 (add-to-list 'tramp-methods
132 (tramp-login-program "ssh")
133 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
134 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
135 (tramp-async-args (("-q")))
136 (tramp-remote-shell "/bin/sh")
137 (tramp-remote-shell-args ("-c"))
138 (tramp-copy-program "scp")
139 (tramp-copy-args (("-P" "%p") ("-p" "%k")
140 ("-q") ("-r") ("%c")))
141 (tramp-copy-keep-date t
)
142 (tramp-copy-recursive t
)
143 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
144 ("-o" "UserKnownHostsFile=/dev/null")
145 ("-o" "StrictHostKeyChecking=no")))
146 (tramp-default-port 22)))
148 (add-to-list 'tramp-methods
150 (tramp-login-program "ssh")
151 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
152 ("-e" "none") ("%h")))
153 (tramp-async-args (("-q")))
154 (tramp-remote-shell "/bin/sh")
155 (tramp-remote-shell-args ("-c"))
156 (tramp-copy-program "rsync")
157 (tramp-copy-args (("-t" "%k") ("-r")))
158 (tramp-copy-env (("RSYNC_RSH") ("ssh" "%c")))
159 (tramp-copy-keep-date t
)
160 (tramp-copy-keep-tmpfile t
)
161 (tramp-copy-recursive t
)))
163 (add-to-list 'tramp-methods
165 (tramp-login-program "rsh")
166 (tramp-login-args (("%h") ("-l" "%u")))
167 (tramp-remote-shell "/bin/sh")
168 (tramp-remote-shell-args ("-c"))))
170 (add-to-list 'tramp-methods
172 (tramp-login-program "remsh")
173 (tramp-login-args (("%h") ("-l" "%u")))
174 (tramp-remote-shell "/bin/sh")
175 (tramp-remote-shell-args ("-c"))))
177 (add-to-list 'tramp-methods
179 (tramp-login-program "ssh")
180 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
181 ("-e" "none") ("%h")))
182 (tramp-async-args (("-q")))
183 (tramp-remote-shell "/bin/sh")
184 (tramp-remote-shell-args ("-c"))
185 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
186 ("-o" "UserKnownHostsFile=/dev/null")
187 ("-o" "StrictHostKeyChecking=no")))
188 (tramp-default-port 22)))
190 (add-to-list 'tramp-methods
192 (tramp-login-program "ssh")
193 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
194 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
195 (tramp-async-args (("-q")))
196 (tramp-remote-shell "/bin/sh")
197 (tramp-remote-shell-args ("-c"))
198 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
199 ("-o" "UserKnownHostsFile=/dev/null")
200 ("-o" "StrictHostKeyChecking=no")))
201 (tramp-default-port 22)))
203 (add-to-list 'tramp-methods
205 (tramp-login-program "telnet")
206 (tramp-login-args (("%h") ("%p") ("2>/dev/null")))
207 (tramp-remote-shell "/bin/sh")
208 (tramp-remote-shell-args ("-c"))
209 (tramp-default-port 23)))
211 (add-to-list 'tramp-methods
213 (tramp-login-program "telnet")
214 (tramp-login-args (("%h") ("%p") ("2>/dev/null")))
215 (tramp-remote-shell "/bin/sh")
216 (tramp-remote-shell-args ("-c"))
217 (tramp-copy-program "nc")
218 ;; We use "-v" for better error tracking.
219 (tramp-copy-args (("-w" "1") ("-v") ("%h") ("%r")))
220 (tramp-remote-copy-program "nc")
221 ;; We use "-p" as required for newer busyboxes. For older
222 ;; busybox/nc versions, the value must be (("-l") ("%r")). This
223 ;; can be achieved by tweaking `tramp-connection-properties'.
224 (tramp-remote-copy-args (("-l") ("-p" "%r")))
225 (tramp-default-port 23)))
227 (add-to-list 'tramp-methods
229 (tramp-login-program "su")
230 (tramp-login-args (("-") ("%u")))
231 (tramp-remote-shell "/bin/sh")
232 (tramp-remote-shell-args ("-c"))
233 (tramp-connection-timeout 10)))
235 (add-to-list 'tramp-methods
237 (tramp-login-program "sudo")
238 (tramp-login-args (("-u" "%u") ("-s") ("-H") ("-p" "Password:")))
239 ;; Local $SHELL could be a nasty one, like zsh or fish. Let's override it.
240 (tramp-login-env (("SHELL") ("/bin/sh")))
241 (tramp-remote-shell "/bin/sh")
242 (tramp-remote-shell-args ("-c"))
243 (tramp-connection-timeout 10)))
245 (add-to-list 'tramp-methods
247 (tramp-login-program "ksu")
248 (tramp-login-args (("%u") ("-q")))
249 (tramp-remote-shell "/bin/sh")
250 (tramp-remote-shell-args ("-c"))
251 (tramp-connection-timeout 10)))
253 (add-to-list 'tramp-methods
255 (tramp-login-program "krlogin")
256 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
257 (tramp-remote-shell "/bin/sh")
258 (tramp-remote-shell-args ("-c"))))
260 (add-to-list 'tramp-methods
262 (tramp-login-program "plink")
263 ;; ("%h") must be a single element, see `tramp-compute-multi-hops'.
264 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
267 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
269 tramp-initial-end-of-output
))
271 (tramp-remote-shell "/bin/sh")
272 (tramp-remote-shell-args ("-c"))
273 (tramp-default-port 22)))
275 (add-to-list 'tramp-methods
277 (tramp-login-program "plink")
278 (tramp-login-args (("-load") ("%h") ("-t") ("\"")
280 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
282 tramp-initial-end-of-output
))
284 (tramp-remote-shell "/bin/sh")
285 (tramp-remote-shell-args ("-c"))))
287 (add-to-list 'tramp-methods
289 (tramp-login-program "plink")
290 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
293 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
295 tramp-initial-end-of-output
))
297 (tramp-remote-shell "/bin/sh")
298 (tramp-remote-shell-args ("-c"))
299 (tramp-copy-program "pscp")
300 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-scp") ("-p" "%k")
302 (tramp-copy-keep-date t
)
303 (tramp-copy-recursive t
)
304 (tramp-default-port 22)))
306 (add-to-list 'tramp-methods
308 (tramp-login-program "plink")
309 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
312 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
314 tramp-initial-end-of-output
))
316 (tramp-remote-shell "/bin/sh")
317 (tramp-remote-shell-args ("-c"))
318 (tramp-copy-program "pscp")
319 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-sftp") ("-p" "%k")
321 (tramp-copy-keep-date t
)
322 (tramp-copy-recursive t
)))
324 (add-to-list 'tramp-methods
326 (tramp-login-program "fsh")
327 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
328 (tramp-remote-shell "/bin/sh")
329 (tramp-remote-shell-args ("-i") ("-c"))
330 (tramp-copy-program "fcp")
331 (tramp-copy-args (("-p" "%k")))
332 (tramp-copy-keep-date t
)))
335 (add-to-list 'tramp-default-method-alist
336 `(,tramp-local-host-regexp
"\\`root\\'" "su"))
339 (add-to-list 'tramp-default-user-alist
340 `(,(concat "\\`" (regexp-opt '("su" "sudo" "ksu")) "\\'")
342 ;; Do not add "ssh" based methods, otherwise ~/.ssh/config would be ignored.
343 ;; Do not add "plink" based methods, they ask interactively for the user.
345 (add-to-list 'tramp-default-user-alist
349 '("rcp" "remcp" "rsh" "telnet" "nc" "krlogin" "fcp"))
351 nil
,(user-login-name)))
354 (defconst tramp-completion-function-alist-rsh
355 '((tramp-parse-rhosts "/etc/hosts.equiv")
356 (tramp-parse-rhosts "~/.rhosts"))
357 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
360 (defconst tramp-completion-function-alist-ssh
361 '((tramp-parse-rhosts "/etc/hosts.equiv")
362 (tramp-parse-rhosts "/etc/shosts.equiv")
363 (tramp-parse-shosts "/etc/ssh_known_hosts")
364 (tramp-parse-sconfig "/etc/ssh_config")
365 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
366 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
367 (tramp-parse-rhosts "~/.rhosts")
368 (tramp-parse-rhosts "~/.shosts")
369 (tramp-parse-shosts "~/.ssh/known_hosts")
370 (tramp-parse-sconfig "~/.ssh/config")
371 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
372 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
373 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
376 (defconst tramp-completion-function-alist-telnet
377 '((tramp-parse-hosts "/etc/hosts"))
378 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
381 (defconst tramp-completion-function-alist-su
382 '((tramp-parse-passwd "/etc/passwd"))
383 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
386 (defconst tramp-completion-function-alist-putty
388 ,(if (memq system-type
'(windows-nt))
389 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"
390 "~/.putty/sessions")))
391 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty sessions.")
394 (eval-after-load 'tramp
396 (tramp-set-completion-function "rcp" tramp-completion-function-alist-rsh
)
397 (tramp-set-completion-function "remcp" tramp-completion-function-alist-rsh
)
398 (tramp-set-completion-function "scp" tramp-completion-function-alist-ssh
)
399 (tramp-set-completion-function "scpx" tramp-completion-function-alist-ssh
)
400 (tramp-set-completion-function "rsync" tramp-completion-function-alist-ssh
)
401 (tramp-set-completion-function "rsh" tramp-completion-function-alist-rsh
)
402 (tramp-set-completion-function "remsh" tramp-completion-function-alist-rsh
)
403 (tramp-set-completion-function "ssh" tramp-completion-function-alist-ssh
)
404 (tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh
)
405 (tramp-set-completion-function
406 "telnet" tramp-completion-function-alist-telnet
)
407 (tramp-set-completion-function "nc" tramp-completion-function-alist-telnet
)
408 (tramp-set-completion-function "su" tramp-completion-function-alist-su
)
409 (tramp-set-completion-function "sudo" tramp-completion-function-alist-su
)
410 (tramp-set-completion-function "ksu" tramp-completion-function-alist-su
)
411 (tramp-set-completion-function
412 "krlogin" tramp-completion-function-alist-rsh
)
413 (tramp-set-completion-function "plink" tramp-completion-function-alist-ssh
)
414 (tramp-set-completion-function
415 "plinkx" tramp-completion-function-alist-putty
)
416 (tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh
)
417 (tramp-set-completion-function "psftp" tramp-completion-function-alist-ssh
)
418 (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh
)))
420 ;; "getconf PATH" yields:
421 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
422 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
423 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
424 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
427 (defcustom tramp-remote-path
428 '(tramp-default-remote-path "/bin" "/usr/bin" "/sbin" "/usr/sbin"
429 "/usr/local/bin" "/usr/local/sbin" "/local/bin" "/local/freeware/bin"
430 "/local/gnu/bin" "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin"
431 "/opt/bin" "/opt/sbin" "/opt/local/bin")
432 "List of directories to search for executables on remote host.
433 For every remote host, this variable will be set buffer local,
434 keeping the list of existing directories on that host.
436 You can use `~' in this list, but when searching for a shell which groks
437 tilde expansion, all directory names starting with `~' will be ignored.
439 `Default Directories' represent the list of directories given by
440 the command \"getconf PATH\". It is recommended to use this
441 entry on top of this list, because these are the default
442 directories for POSIX compatible commands. On remote hosts which
443 do not offer the getconf command (like cygwin), the value
444 \"/bin:/usr/bin\" is used instead of.
446 `Private Directories' are the settings of the $PATH environment,
447 as given in your `~/.profile'."
449 :type
'(repeat (choice
450 (const :tag
"Default Directories" tramp-default-remote-path
)
451 (const :tag
"Private Directories" tramp-own-remote-path
)
452 (string :tag
"Directory"))))
455 (defcustom tramp-remote-process-environment
456 `("TMOUT=0" "LC_CTYPE=''"
457 ,(format "TERM=%s" tramp-terminal-type
)
458 "EMACS=t" ;; Deprecated.
459 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version
)
460 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH=" "PAGER=\"\""
461 "autocorrect=" "correct=")
462 "List of environment variables to be set on the remote host.
464 Each element should be a string of the form ENVVARNAME=VALUE. An
465 entry ENVVARNAME= disables the corresponding environment variable,
466 which might have been set in the init files like ~/.profile.
468 Special handling is applied to the PATH environment, which should
469 not be set here. Instead, it should be set via `tramp-remote-path'."
472 :type
'(repeat string
))
474 (defcustom tramp-sh-extra-args
'(("/bash\\'" .
"-norc -noprofile"))
475 "Alist specifying extra arguments to pass to the remote shell.
476 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
477 matching the shell file name and ARGS is a string specifying the
480 This variable is only used when Tramp needs to start up another shell
481 for tilde expansion. The extra arguments should typically prevent the
482 shell from reading its init file."
484 ;; This might be the wrong way to test whether the widget type
485 ;; `alist' is available. Who knows the right way to test it?
486 :type
(if (get 'alist
'widget-type
)
487 '(alist :key-type string
:value-type string
)
488 '(repeat (cons string string
))))
490 (defconst tramp-actions-before-shell
491 '((tramp-login-prompt-regexp tramp-action-login
)
492 (tramp-password-prompt-regexp tramp-action-password
)
493 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
494 (shell-prompt-pattern tramp-action-succeed
)
495 (tramp-shell-prompt-pattern tramp-action-succeed
)
496 (tramp-yesno-prompt-regexp tramp-action-yesno
)
497 (tramp-yn-prompt-regexp tramp-action-yn
)
498 (tramp-terminal-prompt-regexp tramp-action-terminal
)
499 (tramp-process-alive-regexp tramp-action-process-alive
))
500 "List of pattern/action pairs.
501 Whenever a pattern matches, the corresponding action is performed.
502 Each item looks like (PATTERN ACTION).
504 The PATTERN should be a symbol, a variable. The value of this
505 variable gives the regular expression to search for. Note that the
506 regexp must match at the end of the buffer, \"\\'\" is implicitly
509 The ACTION should also be a symbol, but a function. When the
510 corresponding PATTERN matches, the ACTION function is called.")
512 (defconst tramp-actions-copy-out-of-band
513 '((tramp-password-prompt-regexp tramp-action-password
)
514 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
515 (tramp-copy-failed-regexp tramp-action-permission-denied
)
516 (tramp-process-alive-regexp tramp-action-out-of-band
))
517 "List of pattern/action pairs.
518 This list is used for copying/renaming with out-of-band methods.
520 See `tramp-actions-before-shell' for more info.")
522 (defconst tramp-uudecode
523 "(echo begin 600 %t; tail -n +2) | uudecode
526 "Shell function to implement `uudecode' to standard output.
527 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
528 for this or `uudecode -p', but some systems don't, and for them
529 we have this shell function.")
531 (defconst tramp-perl-file-truename
534 use Cwd \"realpath\";
537 my ($volume, @dirs) = @_;
538 my $real = realpath(File::Spec->catpath(
539 $volume, File::Spec->catdir(@dirs), \"\"));
541 my ($vol, $dir) = File::Spec->splitpath($real, 1);
542 return ($vol, File::Spec->splitdir($dir));
545 my $last = pop(@dirs);
546 ($volume, @dirs) = recursive($volume, @dirs);
548 return ($volume, @dirs);
552 $result = realpath($ARGV[0]);
554 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
555 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
557 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
560 if ($ARGV[0] =~ /\\/$/) {
561 $result = $result . \"/\";
564 print \"\\\"$result\\\"\\n\";
565 ' \"$1\" 2>/dev/null"
566 "Perl script to produce output suitable for use with `file-truename'
567 on the remote file system.
568 Escape sequence %s is replaced with name of Perl binary.
569 This string is passed to `format', so percent characters need to be doubled.")
571 (defconst tramp-perl-file-name-all-completions
581 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
582 @files = readdir(d); closedir(d);
583 foreach $f (@files) {
584 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
585 if (-d \"$ARGV[0]/$f\") {
594 ' \"$1\" \"$2\" \"$3\" 2>/dev/null"
595 "Perl script to produce output suitable for use with
596 `file-name-all-completions' on the remote file system. Escape
597 sequence %s is replaced with name of Perl binary. This string is
598 passed to `format', so percent characters need to be doubled.")
600 ;; Perl script to implement `file-attributes' in a Lisp `read'able
601 ;; output. If you are hacking on this, note that you get *no* output
602 ;; unless this spits out a complete line, including the '\n' at the
604 ;; The device number is returned as "-1", because there will be a virtual
605 ;; device number set in `tramp-sh-handle-file-attributes'.
606 (defconst tramp-perl-file-attributes
608 @stat = lstat($ARGV[0]);
613 if (($stat[2] & 0170000) == 0120000)
615 $type = readlink($ARGV[0]);
616 $type = \"\\\"$type\\\"\";
618 elsif (($stat[2] & 0170000) == 040000)
626 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
627 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
629 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
634 $stat[8] >> 16 & 0xffff,
636 $stat[9] >> 16 & 0xffff,
638 $stat[10] >> 16 & 0xffff,
642 $stat[1] >> 16 & 0xffff,
644 );' \"$1\" \"$2\" 2>/dev/null"
645 "Perl script to produce output suitable for use with `file-attributes'
646 on the remote file system.
647 Escape sequence %s is replaced with name of Perl binary.
648 This string is passed to `format', so percent characters need to be doubled.")
650 (defconst tramp-perl-directory-files-and-attributes
652 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
653 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
654 @list = readdir(DIR);
658 for($i = 0; $i < $n; $i++)
660 $filename = $list[$i];
661 @stat = lstat($filename);
662 if (($stat[2] & 0170000) == 0120000)
664 $type = readlink($filename);
665 $type = \"\\\"$type\\\"\";
667 elsif (($stat[2] & 0170000) == 040000)
675 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
676 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
678 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
684 $stat[8] >> 16 & 0xffff,
686 $stat[9] >> 16 & 0xffff,
688 $stat[10] >> 16 & 0xffff,
692 $stat[1] >> 16 & 0xffff,
694 $stat[0] >> 16 & 0xffff,
697 printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
698 "Perl script implementing `directory-files-attributes' as Lisp `read'able
700 Escape sequence %s is replaced with name of Perl binary.
701 This string is passed to `format', so percent characters need to be doubled.")
703 ;; These two use base64 encoding.
704 (defconst tramp-perl-encode-with-module
705 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
706 "Perl program to use for encoding a file.
707 Escape sequence %s is replaced with name of Perl binary.
708 This string is passed to `format', so percent characters need to be doubled.
709 This implementation requires the MIME::Base64 Perl module to be installed
710 on the remote host.")
712 (defconst tramp-perl-decode-with-module
713 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
714 "Perl program to use for decoding a file.
715 Escape sequence %s is replaced with name of Perl binary.
716 This string is passed to `format', so percent characters need to be doubled.
717 This implementation requires the MIME::Base64 Perl module to be installed
718 on the remote host.")
720 (defconst tramp-perl-encode
722 # This script contributed by Juanma Barranquero <lektu@terra.es>.
723 # Copyright (C) 2002-2014 Free Software Foundation, Inc.
728 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
729 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
733 # We read in chunks of 54 bytes, to generate output lines
734 # of 72 chars (plus end of line)
735 while (read STDIN, $data, 54) {
738 # Only for the last chunk, and only if did not fill the last three-byte packet
740 my $mod = length($data) %% 3;
741 $pad = q(=) x (3 - $mod) if $mod;
744 # Not the fastest method, but it is simple: unpack to binary string, split
745 # by groups of 6 bits and convert back from binary to byte; then map into
746 # the translation table
750 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
754 "Perl program to use for encoding a file.
755 Escape sequence %s is replaced with name of Perl binary.
756 This string is passed to `format', so percent characters need to be doubled.")
758 (defconst tramp-perl-decode
760 # This script contributed by Juanma Barranquero <lektu@terra.es>.
761 # Copyright (C) 2002-2014 Free Software Foundation, Inc.
766 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
767 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
770 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
774 # We are going to accumulate into $pending to accept any line length
775 # (we do not check they are <= 76 chars as the RFC says)
778 while (my $data = <STDIN>) {
781 # If we find one or two =, we have reached the end and
782 # any following data is to be discarded
783 my $finished = $data =~ s/(==?).*/$1/;
786 my $len = length($pending);
787 my $chunk = substr($pending, 0, $len & ~3);
788 $pending = substr($pending, $len & ~3 + 1);
790 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
791 # split in 8-bit chunks and convert back to char.
794 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
798 "Perl program to use for decoding a file.
799 Escape sequence %s is replaced with name of Perl binary.
800 This string is passed to `format', so percent characters need to be doubled.")
802 (defconst tramp-perl-pack
803 "%s -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
804 "Perl program to use for encoding a file.
805 Escape sequence %s is replaced with name of Perl binary.")
807 (defconst tramp-perl-unpack
808 "%s -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"
809 "Perl program to use for decoding a file.
810 Escape sequence %s is replaced with name of Perl binary.")
812 (defconst tramp-vc-registered-read-file-names
815 if %s \"$file\"; then
816 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
818 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
820 if %s \"$file\"; then
821 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
823 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
827 "Script to check existence of VC related files.
828 It must be send formatted with two strings; the tests for file
829 existence, and file readability. Input shall be read via
830 here-document, otherwise the command could exceed maximum length
833 ;; New handlers should be added here.
834 (defconst tramp-sh-file-name-handler-alist
835 '(;; `access-file' performed by default handler.
836 (add-name-to-file . tramp-sh-handle-add-name-to-file
)
837 ;; `byte-compiler-base-file-name' performed by default handler.
838 (copy-directory . tramp-sh-handle-copy-directory
)
839 (copy-file . tramp-sh-handle-copy-file
)
840 (delete-directory . tramp-sh-handle-delete-directory
)
841 (delete-file . tramp-sh-handle-delete-file
)
842 ;; `diff-latest-backup-file' performed by default handler.
843 (directory-file-name . tramp-handle-directory-file-name
)
844 (directory-files . tramp-handle-directory-files
)
845 (directory-files-and-attributes
846 . tramp-sh-handle-directory-files-and-attributes
)
847 ;; `dired-call-process' performed by default handler.
848 (dired-compress-file . tramp-sh-handle-dired-compress-file
)
849 (dired-recursive-delete-directory
850 . tramp-sh-handle-dired-recursive-delete-directory
)
851 (dired-uncache . tramp-handle-dired-uncache
)
852 (expand-file-name . tramp-sh-handle-expand-file-name
)
853 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p
)
854 (file-acl . tramp-sh-handle-file-acl
)
855 (file-attributes . tramp-sh-handle-file-attributes
)
856 (file-directory-p . tramp-sh-handle-file-directory-p
)
857 ;; `file-equal-p' performed by default handler.
858 (file-executable-p . tramp-sh-handle-file-executable-p
)
859 (file-exists-p . tramp-sh-handle-file-exists-p
)
860 ;; `file-in-directory-p' performed by default handler.
861 (file-local-copy . tramp-sh-handle-file-local-copy
)
862 (file-modes . tramp-handle-file-modes
)
863 (file-name-all-completions . tramp-sh-handle-file-name-all-completions
)
864 (file-name-as-directory . tramp-handle-file-name-as-directory
)
865 (file-name-completion . tramp-handle-file-name-completion
)
866 (file-name-directory . tramp-handle-file-name-directory
)
867 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
868 ;; `file-name-sans-versions' performed by default handler.
869 (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p
)
870 (file-notify-add-watch . tramp-sh-handle-file-notify-add-watch
)
871 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch
)
872 (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p
)
873 (file-readable-p . tramp-sh-handle-file-readable-p
)
874 (file-regular-p . tramp-handle-file-regular-p
)
875 (file-remote-p . tramp-handle-file-remote-p
)
876 (file-selinux-context . tramp-sh-handle-file-selinux-context
)
877 (file-symlink-p . tramp-handle-file-symlink-p
)
878 (file-truename . tramp-sh-handle-file-truename
)
879 (file-writable-p . tramp-sh-handle-file-writable-p
)
880 (find-backup-file-name . tramp-handle-find-backup-file-name
)
881 ;; `find-file-noselect' performed by default handler.
882 ;; `get-file-buffer' performed by default handler.
883 (insert-directory . tramp-sh-handle-insert-directory
)
884 (insert-file-contents . tramp-handle-insert-file-contents
)
885 (insert-file-contents-literally
886 . tramp-sh-handle-insert-file-contents-literally
)
887 (load . tramp-handle-load
)
888 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name
)
889 (make-directory . tramp-sh-handle-make-directory
)
890 (make-symbolic-link . tramp-sh-handle-make-symbolic-link
)
891 (process-file . tramp-sh-handle-process-file
)
892 (rename-file . tramp-sh-handle-rename-file
)
893 (set-file-acl . tramp-sh-handle-set-file-acl
)
894 (set-file-modes . tramp-sh-handle-set-file-modes
)
895 (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context
)
896 (set-file-times . tramp-sh-handle-set-file-times
)
897 (set-visited-file-modtime . tramp-sh-handle-set-visited-file-modtime
)
898 (shell-command . tramp-handle-shell-command
)
899 (start-file-process . tramp-sh-handle-start-file-process
)
900 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
901 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
902 (vc-registered . tramp-sh-handle-vc-registered
)
903 (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime
)
904 (write-region . tramp-sh-handle-write-region
))
905 "Alist of handler functions.
906 Operations not mentioned here will be handled by the normal Emacs functions.")
908 ;; This must be the last entry, because `identity' always matches.
910 (add-to-list 'tramp-foreign-file-name-handler-alist
911 '(identity . tramp-sh-file-name-handler
) 'append
)
913 ;;; File Name Handler Functions:
915 (defun tramp-sh-handle-make-symbolic-link
916 (filename linkname
&optional ok-if-already-exists
)
917 "Like `make-symbolic-link' for Tramp files.
918 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
919 the symlink. If LINKNAME is a Tramp file, only the localname component is
920 used as the target of the symlink.
922 If LINKNAME is a Tramp file and the localname component is relative, then
923 it is expanded first, before the localname component is taken. Note that
924 this can give surprising results if the user/host for the source and
925 target of the symlink differ."
926 (with-parsed-tramp-file-name linkname l
927 (let ((ln (tramp-get-remote-ln l
))
928 (cwd (tramp-run-real-handler
929 'file-name-directory
(list l-localname
))))
933 "Making a symbolic link. ln(1) does not exist on the remote host."))
935 ;; Do the 'confirm if exists' thing.
936 (when (file-exists-p linkname
)
938 (if (or (null ok-if-already-exists
) ; not allowed to exist
939 (and (numberp ok-if-already-exists
)
942 "File %s already exists; make it a link anyway? "
945 l
'file-already-exists
"File %s already exists" l-localname
)
946 (delete-file linkname
)))
948 ;; If FILENAME is a Tramp name, use just the localname component.
949 (when (tramp-tramp-file-p filename
)
951 (tramp-file-name-localname
952 (tramp-dissect-file-name (expand-file-name filename
)))))
954 (tramp-flush-file-property l
(file-name-directory l-localname
))
955 (tramp-flush-file-property l l-localname
)
957 ;; Right, they are on the same host, regardless of user, method,
958 ;; etc. We now make the link on the remote machine. This will
959 ;; occur as the user that FILENAME belongs to.
960 (tramp-send-command-and-check
963 "cd %s && %s -sf %s %s"
964 (tramp-shell-quote-argument cwd
)
966 (tramp-shell-quote-argument filename
)
967 (tramp-shell-quote-argument l-localname
))
970 (defun tramp-sh-handle-file-truename (filename)
971 "Like `file-truename' for Tramp files."
974 (with-parsed-tramp-file-name (expand-file-name filename
) nil
975 (tramp-make-tramp-file-name
977 (with-tramp-file-property v localname
"file-truename"
978 (let ((result nil
)) ; result steps in reverse order
979 (tramp-message v
4 "Finding true name for `%s'" filename
)
981 ;; Use GNU readlink --canonicalize-missing where available.
982 ((tramp-get-remote-readlink v
)
983 (tramp-send-command-and-check
985 (format "%s --canonicalize-missing %s"
986 (tramp-get-remote-readlink v
)
987 (tramp-shell-quote-argument localname
)))
988 (with-current-buffer (tramp-get-connection-buffer v
)
989 (goto-char (point-min))
990 (setq result
(buffer-substring (point-min) (point-at-eol)))))
992 ;; Use Perl implementation.
993 ((and (tramp-get-remote-perl v
)
994 (tramp-get-connection-property v
"perl-file-spec" nil
)
995 (tramp-get-connection-property v
"perl-cwd-realpath" nil
))
996 (tramp-maybe-send-script
997 v tramp-perl-file-truename
"tramp_perl_file_truename")
999 (tramp-send-command-and-read
1001 (format "tramp_perl_file_truename %s"
1002 (tramp-shell-quote-argument localname
)))))
1004 ;; Do it yourself. We bind `directory-sep-char' here for
1005 ;; XEmacs on Windows, which would otherwise use backslash.
1006 (t (let* ((directory-sep-char ?
/)
1007 (steps (tramp-compat-split-string localname
"/"))
1008 (localnamedir (tramp-run-real-handler
1009 'file-name-as-directory
(list localname
)))
1010 (is-dir (string= localname localnamedir
))
1013 ;; Don't make the following value larger than
1014 ;; necessary. People expect an error message in
1015 ;; a timely fashion when something is wrong;
1016 ;; otherwise they might think that Emacs is hung.
1017 ;; Of course, correctness has to come first.
1020 (while (and steps
(< numchase numchase-limit
))
1021 (setq thisstep
(pop steps
))
1024 (mapconcat 'identity
1025 (append '("") (reverse result
) (list thisstep
))
1027 (setq symlink-target
1028 (nth 0 (file-attributes
1029 (tramp-make-tramp-file-name
1031 (mapconcat 'identity
1036 (cond ((string= "." thisstep
)
1037 (tramp-message v
5 "Ignoring step `.'"))
1038 ((string= ".." thisstep
)
1039 (tramp-message v
5 "Processing step `..'")
1041 ((stringp symlink-target
)
1042 ;; It's a symlink, follow it.
1044 v
5 "Follow symlink to %s" symlink-target
)
1045 (setq numchase
(1+ numchase
))
1046 (when (file-name-absolute-p symlink-target
)
1048 ;; If the symlink was absolute, we'll get a
1049 ;; string like "/user@host:/some/target";
1050 ;; extract the "/some/target" part from it.
1051 (when (tramp-tramp-file-p symlink-target
)
1052 (unless (tramp-equal-remote filename symlink-target
)
1055 "Symlink target `%s' on wrong host"
1057 (setq symlink-target localname
))
1059 (append (tramp-compat-split-string
1064 (setq result
(cons thisstep result
)))))
1065 (when (>= numchase numchase-limit
)
1068 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
1069 (setq result
(reverse result
))
1070 ;; Combine list to form string.
1073 (mapconcat 'identity
(cons "" result
) "/")
1076 (or (string= "" result
)
1077 (not (string= (substring result -
1) "/"))))
1078 (setq result
(concat result
"/"))))))
1080 (tramp-message v
4 "True name of `%s' is `%s'" localname result
)
1083 ;; Preserve trailing "/".
1084 (if (string-equal (file-name-nondirectory filename
) "") "/" "")))
1088 (defun tramp-sh-handle-file-exists-p (filename)
1089 "Like `file-exists-p' for Tramp files."
1090 (with-parsed-tramp-file-name filename nil
1091 (with-tramp-file-property v localname
"file-exists-p"
1092 (or (not (null (tramp-get-file-property
1093 v localname
"file-attributes-integer" nil
)))
1094 (not (null (tramp-get-file-property
1095 v localname
"file-attributes-string" nil
)))
1096 (tramp-send-command-and-check
1100 (tramp-get-file-exists-command v
)
1101 (tramp-shell-quote-argument localname
)))))))
1103 (defun tramp-sh-handle-file-attributes (filename &optional id-format
)
1104 "Like `file-attributes' for Tramp files."
1105 (unless id-format
(setq id-format
'integer
))
1106 ;; Don't modify `last-coding-system-used' by accident.
1107 (let ((last-coding-system-used last-coding-system-used
))
1108 (with-parsed-tramp-file-name (expand-file-name filename
) nil
1109 (with-tramp-file-property
1110 v localname
(format "file-attributes-%s" id-format
)
1112 (tramp-convert-file-attributes
1116 ((tramp-get-remote-stat v
)
1117 (tramp-do-file-attributes-with-stat v localname id-format
))
1118 ((tramp-get-remote-perl v
)
1119 (tramp-do-file-attributes-with-perl v localname id-format
))
1121 ;; The scripts could fail, for example with huge file size.
1122 (tramp-do-file-attributes-with-ls v localname id-format
))))))))
1124 (defun tramp-do-file-attributes-with-ls (vec localname
&optional id-format
)
1125 "Implement `file-attributes' for Tramp files using the ls(1) command."
1127 res-inode res-filemodes res-numlinks
1128 res-uid res-gid res-size res-symlink-target
)
1129 (tramp-message vec
5 "file attributes with ls: %s" localname
)
1132 (format "(%s %s || %s -h %s) && %s %s %s"
1133 (tramp-get-file-exists-command vec
)
1134 (tramp-shell-quote-argument localname
)
1135 (tramp-get-test-command vec
)
1136 (tramp-shell-quote-argument localname
)
1137 (tramp-get-ls-command vec
)
1138 (if (eq id-format
'integer
) "-ildn" "-ild")
1139 (tramp-shell-quote-argument localname
)))
1140 ;; parse `ls -l' output ...
1141 (with-current-buffer (tramp-get-buffer vec
)
1142 (when (> (buffer-size) 0)
1143 (goto-char (point-min))
1147 (read (current-buffer))
1148 (invalid-read-syntax
1149 (when (and (equal (cadr err
)
1150 "Integer constant overflow in reader")
1152 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1154 (let* ((big (read (substring (car (cddr err
)) 0
1155 (match-beginning 1))))
1156 (small (read (match-string 1 (car (cddr err
)))))
1157 (twiddle (/ small
65536)))
1158 (cons (+ big twiddle
)
1159 (- small
(* twiddle
65536))))))))
1160 ;; ... file mode flags
1161 (setq res-filemodes
(symbol-name (read (current-buffer))))
1163 (setq res-numlinks
(read (current-buffer)))
1165 (setq res-uid
(read (current-buffer)))
1166 (setq res-gid
(read (current-buffer)))
1167 (if (eq id-format
'integer
)
1169 (unless (numberp res-uid
) (setq res-uid -
1))
1170 (unless (numberp res-gid
) (setq res-gid -
1)))
1172 (unless (stringp res-uid
) (setq res-uid
(symbol-name res-uid
)))
1173 (unless (stringp res-gid
) (setq res-gid
(symbol-name res-gid
)))))
1175 (setq res-size
(read (current-buffer)))
1176 ;; From the file modes, figure out other stuff.
1177 (setq symlinkp
(eq ?l
(aref res-filemodes
0)))
1178 (setq dirp
(eq ?d
(aref res-filemodes
0)))
1179 ;; if symlink, find out file name pointed to
1181 (search-forward "-> ")
1182 (setq res-symlink-target
(buffer-substring (point) (point-at-eol))))
1183 ;; return data gathered
1185 ;; 0. t for directory, string (name linked to) for symbolic
1187 (or dirp res-symlink-target
)
1188 ;; 1. Number of links to file.
1194 ;; 4. Last access time, as a list of integers. Normally this
1195 ;; would be in the same format as `current-time', but the
1196 ;; subseconds part is not currently implemented, and (0 0)
1197 ;; denotes an unknown time.
1198 ;; 5. Last modification time, likewise.
1199 ;; 6. Last status change time, likewise.
1200 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1201 ;; 7. Size in bytes (-1, if number is out of range).
1203 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1205 ;; 9. t if file's gid would change if file were deleted and
1206 ;; recreated. Will be set in `tramp-convert-file-attributes'
1208 ;; 10. inode number.
1210 ;; 11. Device number. Will be replaced by a virtual device number.
1214 (defun tramp-do-file-attributes-with-perl
1215 (vec localname
&optional id-format
)
1216 "Implement `file-attributes' for Tramp files using a Perl script."
1217 (tramp-message vec
5 "file attributes with perl: %s" localname
)
1218 (tramp-maybe-send-script
1219 vec tramp-perl-file-attributes
"tramp_perl_file_attributes")
1220 (tramp-send-command-and-read
1222 (format "tramp_perl_file_attributes %s %s"
1223 (tramp-shell-quote-argument localname
) id-format
)))
1225 (defun tramp-do-file-attributes-with-stat
1226 (vec localname
&optional id-format
)
1227 "Implement `file-attributes' for Tramp files using stat(1) command."
1228 (tramp-message vec
5 "file attributes with stat: %s" localname
)
1229 (tramp-send-command-and-read
1232 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
1233 ;; parse correctly the sequence "((". Therefore, we add a space.
1234 "( (%s %s || %s -h %s) && %s -c '((\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)' %s || echo nil)"
1235 (tramp-get-file-exists-command vec
)
1236 (tramp-shell-quote-argument localname
)
1237 (tramp-get-test-command vec
)
1238 (tramp-shell-quote-argument localname
)
1239 (tramp-get-remote-stat vec
)
1240 (if (eq id-format
'integer
) "%ue0" "\"%U\"")
1241 (if (eq id-format
'integer
) "%ge0" "\"%G\"")
1242 (tramp-shell-quote-argument localname
))))
1244 (defun tramp-sh-handle-set-visited-file-modtime (&optional time-list
)
1245 "Like `set-visited-file-modtime' for Tramp files."
1246 (unless (buffer-file-name)
1247 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1250 (tramp-run-real-handler 'set-visited-file-modtime
(list time-list
))
1251 (let ((f (buffer-file-name))
1253 (with-parsed-tramp-file-name f nil
1254 (let* ((remote-file-name-inhibit-cache t
)
1255 (attr (file-attributes f
))
1256 ;; '(-1 65535) means file doesn't exists yet.
1257 (modtime (or (nth 5 attr
) '(-1 65535))))
1258 (when (boundp 'last-coding-system-used
)
1259 (setq coding-system-used
(symbol-value 'last-coding-system-used
)))
1260 ;; We use '(0 0) as a don't-know value. See also
1261 ;; `tramp-do-file-attributes-with-ls'.
1262 (if (not (equal modtime
'(0 0)))
1263 (tramp-run-real-handler 'set-visited-file-modtime
(list modtime
))
1267 (format "%s -ild %s"
1268 (tramp-get-ls-command v
)
1269 (tramp-shell-quote-argument localname
)))
1270 (setq attr
(buffer-substring (point) (point-at-eol))))
1271 (tramp-set-file-property
1272 v localname
"visited-file-modtime-ild" attr
))
1273 (when (boundp 'last-coding-system-used
)
1274 (set 'last-coding-system-used coding-system-used
))
1277 ;; This function makes the same assumption as
1278 ;; `tramp-sh-handle-set-visited-file-modtime'.
1279 (defun tramp-sh-handle-verify-visited-file-modtime (&optional buf
)
1280 "Like `verify-visited-file-modtime' for Tramp files.
1281 At the time `verify-visited-file-modtime' calls this function, we
1282 already know that the buffer is visiting a file and that
1283 `visited-file-modtime' does not return 0. Do not call this
1284 function directly, unless those two cases are already taken care
1286 (with-current-buffer (or buf
(current-buffer))
1287 (let ((f (buffer-file-name)))
1288 ;; There is no file visiting the buffer, or the buffer has no
1289 ;; recorded last modification time, or there is no established
1292 (eq (visited-file-modtime) 0)
1293 (not (tramp-file-name-handler 'file-remote-p f nil
'connected
)))
1295 (with-parsed-tramp-file-name f nil
1296 (let* ((remote-file-name-inhibit-cache t
)
1297 (attr (file-attributes f
))
1298 (modtime (nth 5 attr
))
1299 (mt (visited-file-modtime)))
1302 ;; File exists, and has a known modtime.
1303 ((and attr
(not (equal modtime
'(0 0))))
1304 (< (abs (tramp-time-diff
1306 ;; For compatibility, deal with both the old
1307 ;; (HIGH . LOW) and the new (HIGH LOW) return
1308 ;; values of `visited-file-modtime'.
1310 (list (car mt
) (cdr mt
))
1313 ;; Modtime has the don't know value.
1317 (format "%s -ild %s"
1318 (tramp-get-ls-command v
)
1319 (tramp-shell-quote-argument localname
)))
1320 (with-current-buffer (tramp-get-buffer v
)
1321 (setq attr
(buffer-substring (point) (point-at-eol))))
1324 (tramp-get-file-property
1325 v localname
"visited-file-modtime-ild" "")))
1326 ;; If file does not exist, say it is not modified if and
1327 ;; only if that agrees with the buffer's record.
1328 (t (equal mt
'(-1 65535))))))))))
1330 (defun tramp-sh-handle-set-file-modes (filename mode
)
1331 "Like `set-file-modes' for Tramp files."
1332 (with-parsed-tramp-file-name filename nil
1333 (tramp-flush-file-property v localname
)
1334 ;; FIXME: extract the proper text from chmod's stderr.
1335 (tramp-barf-unless-okay
1337 (format "chmod %s %s"
1338 (tramp-compat-decimal-to-octal mode
)
1339 (tramp-shell-quote-argument localname
))
1340 "Error while changing file's mode %s" filename
)))
1342 (defun tramp-sh-handle-set-file-times (filename &optional time
)
1343 "Like `set-file-times' for Tramp files."
1344 (if (tramp-tramp-file-p filename
)
1345 (with-parsed-tramp-file-name filename nil
1346 (when (tramp-get-remote-touch v
)
1347 (tramp-flush-file-property v localname
)
1348 (let ((time (if (or (null time
) (equal time
'(0 0)))
1351 ;; With GNU Emacs, `format-time-string' has an
1352 ;; optional parameter UNIVERSAL. This is preferred,
1353 ;; because we could handle the case when the remote
1354 ;; host is located in a different time zone as the
1356 (utc (not (featurep 'xemacs
))))
1357 (tramp-send-command-and-check
1360 (if utc
"env TZ=UTC" "")
1361 (tramp-get-remote-touch v
)
1362 (if (tramp-get-connection-property v
"touch-t" nil
)
1365 (format-time-string "%Y%m%d%H%M.%S" time t
)
1366 (format-time-string "%Y%m%d%H%M.%S" time
)))
1368 (tramp-shell-quote-argument localname
))))))
1370 ;; We handle also the local part, because in older Emacsen,
1371 ;; without `set-file-times', this function is an alias for this.
1372 ;; We are local, so we don't need the UTC settings.
1375 nil
"touch" nil nil nil
"-t"
1376 (format-time-string "%Y%m%d%H%M.%S" time
)
1377 (tramp-shell-quote-argument filename
)))))
1379 (defun tramp-set-file-uid-gid (filename &optional uid gid
)
1380 "Set the ownership for FILENAME.
1381 If UID and GID are provided, these values are used; otherwise uid
1382 and gid of the corresponding user is taken. Both parameters must
1383 be non-negative integers."
1384 ;; Modern Unices allow chown only for root. So we might need
1385 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1386 ;; working with su(do)? when it is needed, so it shall succeed in
1387 ;; the majority of cases.
1388 ;; Don't modify `last-coding-system-used' by accident.
1389 (let ((last-coding-system-used last-coding-system-used
))
1390 (if (tramp-tramp-file-p filename
)
1391 (with-parsed-tramp-file-name filename nil
1392 (if (and (zerop (user-uid)) (tramp-local-host-p v
))
1393 ;; If we are root on the local host, we can do it directly.
1394 (tramp-set-file-uid-gid localname uid gid
)
1395 (let ((uid (or (and (natnump uid
) uid
)
1396 (tramp-get-remote-uid v
'integer
)))
1397 (gid (or (and (natnump gid
) gid
)
1398 (tramp-get-remote-gid v
'integer
))))
1401 "chown %d:%d %s" uid gid
1402 (tramp-shell-quote-argument localname
))))))
1404 ;; We handle also the local part, because there doesn't exist
1405 ;; `set-file-uid-gid'. On W32 "chown" might not work.
1406 (let ((uid (or (and (natnump uid
) uid
) (tramp-get-local-uid 'integer
)))
1407 (gid (or (and (natnump gid
) gid
) (tramp-get-local-gid 'integer
))))
1409 nil
"chown" nil nil nil
1410 (format "%d:%d" uid gid
) (tramp-shell-quote-argument filename
))))))
1412 (defun tramp-remote-selinux-p (vec)
1413 "Check, whether SELINUX is enabled on the remote host."
1414 (with-tramp-connection-property
1415 (tramp-get-connection-process vec
) "selinux-p"
1416 (let ((result (tramp-find-executable
1417 vec
"getenforce" (tramp-get-remote-path vec
) t t
)))
1420 (tramp-send-command-and-read
1421 vec
(format "echo \\\"`%S`\\\"" result
))
1424 (defun tramp-sh-handle-file-selinux-context (filename)
1425 "Like `file-selinux-context' for Tramp files."
1426 (with-parsed-tramp-file-name filename nil
1427 (with-tramp-file-property v localname
"file-selinux-context"
1428 (let ((context '(nil nil nil nil
))
1429 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1430 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1431 (when (and (tramp-remote-selinux-p v
)
1432 (tramp-send-command-and-check
1435 (tramp-get-ls-command v
)
1436 (tramp-shell-quote-argument localname
))))
1437 (with-current-buffer (tramp-get-connection-buffer v
)
1438 (goto-char (point-min))
1439 (when (re-search-forward regexp
(point-at-eol) t
)
1440 (setq context
(list (match-string 1) (match-string 2)
1441 (match-string 3) (match-string 4))))))
1442 ;; Return the context.
1445 (defun tramp-sh-handle-set-file-selinux-context (filename context
)
1446 "Like `set-file-selinux-context' for Tramp files."
1447 (with-parsed-tramp-file-name filename nil
1448 (if (and (consp context
)
1449 (tramp-remote-selinux-p v
)
1450 (tramp-send-command-and-check
1451 v
(format "chcon %s %s %s %s %s"
1452 (if (stringp (nth 0 context
))
1453 (format "--user=%s" (nth 0 context
)) "")
1454 (if (stringp (nth 1 context
))
1455 (format "--role=%s" (nth 1 context
)) "")
1456 (if (stringp (nth 2 context
))
1457 (format "--type=%s" (nth 2 context
)) "")
1458 (if (stringp (nth 3 context
))
1459 (format "--range=%s" (nth 3 context
)) "")
1460 (tramp-shell-quote-argument localname
))))
1462 (tramp-set-file-property v localname
"file-selinux-context" context
)
1464 (tramp-set-file-property v localname
"file-selinux-context" 'undef
)
1467 (defun tramp-remote-acl-p (vec)
1468 "Check, whether ACL is enabled on the remote host."
1469 (with-tramp-connection-property (tramp-get-connection-process vec
) "acl-p"
1470 (tramp-send-command-and-check vec
"getfacl /")))
1472 (defun tramp-sh-handle-file-acl (filename)
1473 "Like `file-acl' for Tramp files."
1474 (with-parsed-tramp-file-name filename nil
1475 (with-tramp-file-property v localname
"file-acl"
1476 (when (and (tramp-remote-acl-p v
)
1477 (tramp-send-command-and-check
1479 "getfacl -ac %s 2>/dev/null"
1480 (tramp-shell-quote-argument localname
))))
1481 (with-current-buffer (tramp-get-connection-buffer v
)
1482 (goto-char (point-max))
1483 (delete-blank-lines)
1484 (when (> (point-max) (point-min))
1485 (tramp-compat-funcall
1486 'substring-no-properties
(buffer-string))))))))
1488 (defun tramp-sh-handle-set-file-acl (filename acl-string
)
1489 "Like `set-file-acl' for Tramp files."
1490 (with-parsed-tramp-file-name (expand-file-name filename
) nil
1491 (if (and (stringp acl-string
) (tramp-remote-acl-p v
)
1494 v
(format "setfacl --set-file=- %s <<'%s'\n%s\n%s\n"
1495 (tramp-shell-quote-argument localname
)
1496 tramp-end-of-heredoc
1498 tramp-end-of-heredoc
))
1499 (tramp-send-command-and-check v nil
)))
1502 (tramp-set-file-property v localname
"file-acl" acl-string
)
1504 ;; In case of errors, we return `nil'.
1505 (tramp-set-file-property v localname
"file-acl-string" 'undef
)
1508 ;; Simple functions using the `test' command.
1510 (defun tramp-sh-handle-file-executable-p (filename)
1511 "Like `file-executable-p' for Tramp files."
1512 (with-parsed-tramp-file-name filename nil
1513 (with-tramp-file-property v localname
"file-executable-p"
1514 ;; Examine `file-attributes' cache to see if request can be
1515 ;; satisfied without remote operation.
1516 (or (tramp-check-cached-permissions v ?x
)
1517 (tramp-run-test "-x" filename
)))))
1519 (defun tramp-sh-handle-file-readable-p (filename)
1520 "Like `file-readable-p' for Tramp files."
1521 (with-parsed-tramp-file-name filename nil
1522 (with-tramp-file-property v localname
"file-readable-p"
1523 ;; Examine `file-attributes' cache to see if request can be
1524 ;; satisfied without remote operation.
1525 (or (tramp-check-cached-permissions v ?r
)
1526 (tramp-run-test "-r" filename
)))))
1528 ;; When the remote shell is started, it looks for a shell which groks
1529 ;; tilde expansion. Here, we assume that all shells which grok tilde
1530 ;; expansion will also provide a `test' command which groks `-nt' (for
1531 ;; newer than). If this breaks, tell me about it and I'll try to do
1532 ;; something smarter about it.
1533 (defun tramp-sh-handle-file-newer-than-file-p (file1 file2
)
1534 "Like `file-newer-than-file-p' for Tramp files."
1535 (cond ((not (file-exists-p file1
))
1537 ((not (file-exists-p file2
))
1539 ;; We are sure both files exist at this point.
1542 ;; We try to get the mtime of both files. If they are not
1543 ;; equal to the "dont-know" value, then we subtract the times
1544 ;; and obtain the result.
1545 (let ((fa1 (file-attributes file1
))
1546 (fa2 (file-attributes file2
)))
1547 (if (and (not (equal (nth 5 fa1
) '(0 0)))
1548 (not (equal (nth 5 fa2
) '(0 0))))
1549 (> 0 (tramp-time-diff (nth 5 fa2
) (nth 5 fa1
)))
1550 ;; If one of them is the dont-know value, then we can
1551 ;; still try to run a shell command on the remote host.
1552 ;; However, this only works if both files are Tramp
1553 ;; files and both have the same method, same user, same
1555 (unless (tramp-equal-remote file1 file2
)
1556 (with-parsed-tramp-file-name
1557 (if (tramp-tramp-file-p file1
) file1 file2
) nil
1560 "Files %s and %s must have same method, user, host"
1562 (with-parsed-tramp-file-name file1 nil
1564 (tramp-get-test-nt-command v
) file1 file2
))))))))
1566 ;; Functions implemented using the basic functions above.
1568 (defun tramp-sh-handle-file-directory-p (filename)
1569 "Like `file-directory-p' for Tramp files."
1570 (with-parsed-tramp-file-name filename nil
1571 ;; `file-directory-p' is used as predicate for file name completion.
1572 ;; Sometimes, when a connection is not established yet, it is
1573 ;; desirable to return t immediately for "/method:foo:". It can
1574 ;; be expected that this is always a directory.
1575 (or (zerop (length localname
))
1576 (with-tramp-file-property v localname
"file-directory-p"
1577 (tramp-run-test "-d" filename
)))))
1579 (defun tramp-sh-handle-file-writable-p (filename)
1580 "Like `file-writable-p' for Tramp files."
1581 (with-parsed-tramp-file-name filename nil
1582 (with-tramp-file-property v localname
"file-writable-p"
1583 (if (file-exists-p filename
)
1584 ;; Examine `file-attributes' cache to see if request can be
1585 ;; satisfied without remote operation.
1586 (or (tramp-check-cached-permissions v ?w
)
1587 (tramp-run-test "-w" filename
))
1588 ;; If file doesn't exist, check if directory is writable.
1589 (and (tramp-run-test "-d" (file-name-directory filename
))
1590 (tramp-run-test "-w" (file-name-directory filename
)))))))
1592 (defun tramp-sh-handle-file-ownership-preserved-p (filename &optional group
)
1593 "Like `file-ownership-preserved-p' for Tramp files."
1594 (with-parsed-tramp-file-name filename nil
1595 (with-tramp-file-property v localname
"file-ownership-preserved-p"
1596 (let ((attributes (file-attributes filename
)))
1597 ;; Return t if the file doesn't exist, since it's true that no
1598 ;; information would be lost by an (attempted) delete and create.
1599 (or (null attributes
)
1601 (= (nth 2 attributes
) (tramp-get-remote-uid v
'integer
))
1603 (= (nth 3 attributes
) (tramp-get-remote-gid v
'integer
)))))))))
1605 ;; Directory listings.
1607 (defun tramp-sh-handle-directory-files-and-attributes
1608 (directory &optional full match nosort id-format
)
1609 "Like `directory-files-and-attributes' for Tramp files."
1610 (if (with-parsed-tramp-file-name directory nil
1611 (not (or (tramp-get-remote-stat v
) (tramp-get-remote-perl v
))))
1612 (tramp-handle-directory-files-and-attributes
1613 directory full match nosort id-format
)
1616 (unless id-format
(setq id-format
'integer
))
1617 (when (file-directory-p directory
)
1618 (setq directory
(expand-file-name directory
))
1621 (with-parsed-tramp-file-name directory nil
1622 (with-tramp-file-property
1624 (format "directory-files-and-attributes-%s" id-format
)
1629 (tramp-convert-file-attributes v
(cdr x
))))
1631 ((tramp-get-remote-stat v
)
1632 (tramp-do-directory-files-and-attributes-with-stat
1633 v localname id-format
))
1634 ((tramp-get-remote-perl v
)
1635 (tramp-do-directory-files-and-attributes-with-perl
1636 v localname id-format
)))))))))
1640 (setq item
(pop temp
))
1641 (when (or (null match
) (string-match match
(car item
)))
1643 (setcar item
(expand-file-name (car item
) directory
)))
1644 (push item result
)))
1648 (sort result
(lambda (x y
) (string< (car x
) (car y
)))))))))
1650 (defun tramp-do-directory-files-and-attributes-with-perl
1651 (vec localname
&optional id-format
)
1652 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1653 (tramp-message vec
5 "directory-files-and-attributes with perl: %s" localname
)
1654 (tramp-maybe-send-script
1655 vec tramp-perl-directory-files-and-attributes
1656 "tramp_perl_directory_files_and_attributes")
1658 (tramp-send-command-and-read
1660 (format "tramp_perl_directory_files_and_attributes %s %s"
1661 (tramp-shell-quote-argument localname
) id-format
))))
1662 (when (stringp object
) (tramp-error vec
'file-error object
))
1665 (defun tramp-do-directory-files-and-attributes-with-stat
1666 (vec localname
&optional id-format
)
1667 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1668 (tramp-message vec
5 "directory-files-and-attributes with stat: %s" localname
)
1669 (tramp-send-command-and-read
1673 ;; We must care about file names with spaces, or starting with
1674 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1675 ;; but it does not work on all remote systems. Therefore, we
1676 ;; quote the file names via sed.
1677 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | "
1679 "'(\"%%n\" (\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)'"
1680 " 2>/dev/null); echo \")\"")
1681 (tramp-shell-quote-argument localname
)
1682 (tramp-get-ls-command vec
)
1683 (tramp-get-remote-stat vec
)
1684 (if (eq id-format
'integer
) "%ue0" "\"%U\"")
1685 (if (eq id-format
'integer
) "%ge0" "\"%G\""))))
1687 ;; This function should return "foo/" for directories and "bar" for
1689 (defun tramp-sh-handle-file-name-all-completions (filename directory
)
1690 "Like `file-name-all-completions' for Tramp files."
1691 (unless (save-match-data (string-match "/" filename
))
1692 (with-parsed-tramp-file-name (expand-file-name directory
) nil
1699 ;; Try cache entries for `filename', `filename' with last
1700 ;; character removed, `filename' with last two characters
1701 ;; removed, ..., and finally the empty string - all
1702 ;; concatenated to the local directory name.
1703 (let ((remote-file-name-inhibit-cache
1704 (or remote-file-name-inhibit-cache
1705 tramp-completion-reread-directory-timeout
)))
1707 ;; This is inefficient for very long file names, pity
1708 ;; `reduce' is not available...
1715 (tramp-get-file-property
1717 (concat localname
(substring filename
0 x
))
1718 "file-name-all-completions"
1720 (when cache-hit
(list cache-hit
))))
1721 ;; We cannot use a length of 0, because file properties
1722 ;; for "foo" and "foo/" are identical.
1723 (tramp-compat-number-sequence (length filename
) 1 -
1)))))
1725 ;; Cache expired or no matching cache entry found so we need
1726 ;; to perform a remote operation.
1728 ;; Get a list of directories and files, including reliably
1729 ;; tagging the directories with a trailing '/'. Because I
1730 ;; rock. --daniel@danann.net
1732 ;; Changed to perform `cd' in the same remote op and only
1733 ;; get entries starting with `filename'. Capture any `cd'
1734 ;; error messages. Ensure any `cd' and `echo' aliases are
1738 (if (tramp-get-remote-perl v
)
1740 (tramp-maybe-send-script
1741 v tramp-perl-file-name-all-completions
1742 "tramp_perl_file_name_all_completions")
1743 (format "tramp_perl_file_name_all_completions %s %s %d"
1744 (tramp-shell-quote-argument localname
)
1745 (tramp-shell-quote-argument filename
)
1747 ;; `read-file-name-completion-ignore-case'
1748 ;; is introduced with Emacs 22.1.
1750 'read-file-name-completion-ignore-case
)
1751 'read-file-name-completion-ignore-case
1752 'completion-ignore-case
))
1756 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
1757 ;; `ls' with wildcard might fail with `Argument
1758 ;; list too long' error in some corner cases; if
1759 ;; `ls' fails after `cd' succeeded, chances are
1760 ;; that's the case, so let's retry without
1761 ;; wildcard. This will return "too many" entries
1762 ;; but that isn't harmful.
1763 " || %s -a 2>/dev/null)"
1764 " | while IFS= read f; do"
1765 " if %s -d \"$f\" 2>/dev/null;"
1766 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1767 " && \\echo ok) || \\echo fail")
1768 (tramp-shell-quote-argument localname
)
1769 (tramp-get-ls-command v
)
1770 ;; When `filename' is empty, just `ls' without
1771 ;; `filename' argument is more efficient than `ls *'
1772 ;; for very large directories and might avoid the
1773 ;; `Argument list too long' error.
1775 ;; With and only with wildcard, we need to add
1776 ;; `-d' to prevent `ls' from descending into
1778 (if (zerop (length filename
))
1780 (concat (tramp-shell-quote-argument filename
) "* -d"))
1781 (tramp-get-ls-command v
)
1782 (tramp-get-test-command v
))))
1784 ;; Now grab the output.
1785 (with-current-buffer (tramp-get-buffer v
)
1786 (goto-char (point-max))
1788 ;; Check result code, found in last line of output.
1790 (if (looking-at "^fail$")
1792 ;; Grab error message from line before last line
1793 ;; (it was put there by `cd 2>&1').
1797 "tramp-sh-handle-file-name-all-completions: %s"
1798 (buffer-substring (point) (point-at-eol))))
1799 ;; For peace of mind, if buffer doesn't end in `fail'
1800 ;; then it should end in `ok'. If neither are in the
1801 ;; buffer something went seriously wrong on the remote
1803 (unless (looking-at "^ok$")
1807 tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
1808 (tramp-shell-quote-argument localname
) (buffer-string))))
1810 (while (zerop (forward-line -
1))
1811 (push (buffer-substring (point) (point-at-eol)) result
)))
1813 ;; Because the remote op went through OK we know the
1814 ;; directory we `cd'-ed to exists.
1815 (tramp-set-file-property v localname
"file-exists-p" t
)
1817 ;; Because the remote op went through OK we know every
1818 ;; file listed by `ls' exists.
1819 (mapc (lambda (entry)
1820 (tramp-set-file-property
1821 v
(concat localname entry
) "file-exists-p" t
))
1824 ;; Store result in the cache.
1825 (tramp-set-file-property
1826 v
(concat localname filename
)
1827 "file-name-all-completions" result
))))))))
1831 (defun tramp-sh-handle-add-name-to-file
1832 (filename newname
&optional ok-if-already-exists
)
1833 "Like `add-name-to-file' for Tramp files."
1834 (unless (tramp-equal-remote filename newname
)
1835 (with-parsed-tramp-file-name
1836 (if (tramp-tramp-file-p filename
) filename newname
) nil
1839 "add-name-to-file: %s"
1840 "only implemented for same method, same user, same host")))
1841 (with-parsed-tramp-file-name filename v1
1842 (with-parsed-tramp-file-name newname v2
1843 (let ((ln (when v1
(tramp-get-remote-ln v1
))))
1844 (when (and (numberp ok-if-already-exists
)
1845 (file-exists-p newname
)
1848 "File %s already exists; make it a new name anyway? "
1851 v2
'file-error
"add-name-to-file: file %s already exists" newname
))
1852 (when ok-if-already-exists
(setq ln
(concat ln
" -f")))
1853 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
1854 (tramp-flush-file-property v2 v2-localname
)
1855 (tramp-barf-unless-okay
1857 (format "%s %s %s" ln
1858 (tramp-shell-quote-argument v1-localname
)
1859 (tramp-shell-quote-argument v2-localname
))
1860 "error with add-name-to-file, see buffer `%s' for details"
1863 (defun tramp-sh-handle-copy-file
1864 (filename newname
&optional ok-if-already-exists keep-date
1865 preserve-uid-gid preserve-extended-attributes
)
1866 "Like `copy-file' for Tramp files."
1867 (setq filename
(expand-file-name filename
))
1868 (setq newname
(expand-file-name newname
))
1870 ;; At least one file a Tramp file?
1871 ((or (tramp-tramp-file-p filename
)
1872 (tramp-tramp-file-p newname
))
1873 (tramp-do-copy-or-rename-file
1874 'copy filename newname ok-if-already-exists keep-date
1875 preserve-uid-gid preserve-extended-attributes
))
1877 (preserve-extended-attributes
1878 (tramp-run-real-handler
1880 (list filename newname ok-if-already-exists keep-date
1881 preserve-uid-gid preserve-extended-attributes
)))
1883 (tramp-run-real-handler
1885 (list filename newname ok-if-already-exists keep-date preserve-uid-gid
)))
1887 (tramp-run-real-handler
1888 'copy-file
(list filename newname ok-if-already-exists keep-date
)))))
1890 (defun tramp-sh-handle-copy-directory
1891 (dirname newname
&optional keep-date parents copy-contents
)
1892 "Like `copy-directory' for Tramp files."
1893 (let ((t1 (tramp-tramp-file-p dirname
))
1894 (t2 (tramp-tramp-file-p newname
)))
1895 (with-parsed-tramp-file-name (if t1 dirname newname
) nil
1896 (if (and (not copy-contents
)
1897 (tramp-get-method-parameter method
'tramp-copy-recursive
)
1898 ;; When DIRNAME and NEWNAME are remote, they must have
1900 (or (null t1
) (null t2
)
1902 (tramp-file-name-method (tramp-dissect-file-name dirname
))
1903 (tramp-file-name-method
1904 (tramp-dissect-file-name newname
)))))
1905 ;; scp or rsync DTRT.
1907 (setq dirname
(directory-file-name (expand-file-name dirname
))
1908 newname
(directory-file-name (expand-file-name newname
)))
1909 (if (and (file-directory-p newname
)
1910 (not (string-equal (file-name-nondirectory dirname
)
1911 (file-name-nondirectory newname
))))
1914 (file-name-nondirectory dirname
) newname
)))
1915 (if (not (file-directory-p (file-name-directory newname
)))
1916 (make-directory (file-name-directory newname
) parents
))
1917 (tramp-do-copy-or-rename-file-out-of-band
1918 'copy dirname newname keep-date
))
1919 ;; We must do it file-wise.
1920 (tramp-run-real-handler
1923 (list dirname newname keep-date parents copy-contents
)
1924 (list dirname newname keep-date parents
))))
1926 ;; When newname did exist, we have wrong cached values.
1928 (with-parsed-tramp-file-name newname nil
1929 (tramp-flush-file-property v
(file-name-directory localname
))
1930 (tramp-flush-file-property v localname
))))))
1932 (defun tramp-sh-handle-rename-file
1933 (filename newname
&optional ok-if-already-exists
)
1934 "Like `rename-file' for Tramp files."
1935 ;; Check if both files are local -- invoke normal rename-file.
1936 ;; Otherwise, use Tramp from local system.
1937 (setq filename
(expand-file-name filename
))
1938 (setq newname
(expand-file-name newname
))
1939 ;; At least one file a Tramp file?
1940 (if (or (tramp-tramp-file-p filename
)
1941 (tramp-tramp-file-p newname
))
1942 (tramp-do-copy-or-rename-file
1943 'rename filename newname ok-if-already-exists t t
)
1944 (tramp-run-real-handler
1945 'rename-file
(list filename newname ok-if-already-exists
))))
1947 (defun tramp-do-copy-or-rename-file
1948 (op filename newname
&optional ok-if-already-exists keep-date
1949 preserve-uid-gid preserve-extended-attributes
)
1950 "Copy or rename a remote file.
1951 OP must be `copy' or `rename' and indicates the operation to perform.
1952 FILENAME specifies the file to copy or rename, NEWNAME is the name of
1953 the new file (for copy) or the new name of the file (for rename).
1954 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
1955 KEEP-DATE means to make sure that NEWNAME has the same timestamp
1956 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
1957 the uid and gid if both files are on the same host.
1958 PRESERVE-EXTENDED-ATTRIBUTES activates selinux and acl commands.
1960 This function is invoked by `tramp-sh-handle-copy-file' and
1961 `tramp-sh-handle-rename-file'. It is an error if OP is neither
1962 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
1964 (unless (memq op
'(copy rename
))
1965 (error "Unknown operation `%s', must be `copy' or `rename'" op
))
1966 (let ((t1 (tramp-tramp-file-p filename
))
1967 (t2 (tramp-tramp-file-p newname
))
1968 (length (nth 7 (file-attributes (file-truename filename
))))
1969 (attributes (and preserve-extended-attributes
1970 (apply 'file-extended-attributes
(list filename
)))))
1972 (with-parsed-tramp-file-name (if t1 filename newname
) nil
1973 (when (and (not ok-if-already-exists
) (file-exists-p newname
))
1975 v
'file-already-exists
"File %s already exists" newname
))
1977 (with-tramp-progress-reporter
1978 v
0 (format "%s %s to %s"
1979 (if (eq op
'copy
) "Copying" "Renaming")
1983 ;; Both are Tramp files.
1985 (with-parsed-tramp-file-name filename v1
1986 (with-parsed-tramp-file-name newname v2
1988 ;; Shortcut: if method, host, user are the same for
1989 ;; both files, we invoke `cp' or `mv' on the remote
1991 ((tramp-equal-remote filename newname
)
1992 (tramp-do-copy-or-rename-file-directly
1994 ok-if-already-exists keep-date preserve-uid-gid
))
1996 ;; Try out-of-band operation.
1998 (tramp-method-out-of-band-p v1 length
)
1999 (tramp-method-out-of-band-p v2 length
))
2000 (tramp-do-copy-or-rename-file-out-of-band
2001 op filename newname keep-date
))
2003 ;; No shortcut was possible. So we copy the file
2004 ;; first. If the operation was `rename', we go back
2005 ;; and delete the original file (if the copy was
2006 ;; successful). The approach is simple-minded: we
2007 ;; create a new buffer, insert the contents of the
2008 ;; source file into it, then write out the buffer to
2009 ;; the target file. The advantage is that it doesn't
2010 ;; matter which file name handlers are used for the
2011 ;; source and target file.
2013 (tramp-do-copy-or-rename-file-via-buffer
2014 op filename newname keep-date
))))))
2016 ;; One file is a Tramp file, the other one is local.
2019 ;; Fast track on local machine.
2020 ((tramp-local-host-p v
)
2021 (tramp-do-copy-or-rename-file-directly
2023 ok-if-already-exists keep-date preserve-uid-gid
))
2025 ;; If the Tramp file has an out-of-band method, the
2026 ;; corresponding copy-program can be invoked.
2027 ((tramp-method-out-of-band-p v length
)
2028 (tramp-do-copy-or-rename-file-out-of-band
2029 op filename newname keep-date
))
2031 ;; Use the inline method via a Tramp buffer.
2032 (t (tramp-do-copy-or-rename-file-via-buffer
2033 op filename newname keep-date
))))
2036 ;; One of them must be a Tramp file.
2037 (error "Tramp implementation says this cannot happen")))
2039 ;; Handle `preserve-extended-attributes'. We ignore possible
2040 ;; errors, because ACL strings could be incompatible.
2043 (apply 'set-file-extended-attributes
(list newname attributes
))))
2045 ;; In case of `rename', we must flush the cache of the source file.
2046 (when (and t1
(eq op
'rename
))
2047 (with-parsed-tramp-file-name filename v1
2048 (tramp-flush-file-property v1
(file-name-directory v1-localname
))
2049 (tramp-flush-file-property v1 v1-localname
)))
2051 ;; When newname did exist, we have wrong cached values.
2053 (with-parsed-tramp-file-name newname v2
2054 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
2055 (tramp-flush-file-property v2 v2-localname
)))))))
2057 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date
)
2058 "Use an Emacs buffer to copy or rename a file.
2059 First arg OP is either `copy' or `rename' and indicates the operation.
2060 FILENAME is the source file, NEWNAME the target file.
2061 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2063 ;; We must disable multibyte, because binary data shall not be
2064 ;; converted. We remove `tramp-file-name-handler' from
2065 ;; `inhibit-file-name-handlers'; otherwise the file name handler
2066 ;; for `insert-file-contents' might be deactivated in some corner
2068 (set-buffer-multibyte nil
)
2069 (let ((coding-system-for-read 'binary
)
2070 (jka-compr-inhibit t
)
2071 (inhibit-file-name-handlers
2072 (remq 'tramp-file-name-handler inhibit-file-name-handlers
)))
2073 (insert-file-contents-literally filename
))
2074 ;; We don't want the target file to be compressed, so we let-bind
2075 ;; `jka-compr-inhibit' to t.
2076 (let ((coding-system-for-write 'binary
)
2077 (jka-compr-inhibit t
))
2078 (write-region (point-min) (point-max) newname nil
'no-message
)))
2079 ;; KEEP-DATE handling.
2080 (when keep-date
(set-file-times newname
(nth 5 (file-attributes filename
))))
2082 (set-file-modes newname
(tramp-default-file-modes filename
))
2083 ;; If the operation was `rename', delete the original file.
2084 (unless (eq op
'copy
) (delete-file filename
)))
2086 (defun tramp-do-copy-or-rename-file-directly
2087 (op filename newname ok-if-already-exists keep-date preserve-uid-gid
)
2088 "Invokes `cp' or `mv' on the remote system.
2089 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2090 respectively. FILENAME specifies the file to copy or rename,
2091 NEWNAME is the name of the new file (for copy) or the new name of
2092 the file (for rename). Both files must reside on the same host.
2093 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2094 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2095 the uid and gid from FILENAME."
2096 (let ((t1 (tramp-tramp-file-p filename
))
2097 (t2 (tramp-tramp-file-p newname
))
2098 (file-times (nth 5 (file-attributes filename
)))
2099 (file-modes (tramp-default-file-modes filename
)))
2100 (with-parsed-tramp-file-name (if t1 filename newname
) nil
2101 (let* ((cmd (cond ((and (eq op
'copy
) preserve-uid-gid
) "cp -f -p")
2102 ((eq op
'copy
) "cp -f")
2103 ((eq op
'rename
) "mv -f")
2106 "Unknown operation `%s', must be `copy' or `rename'"
2110 (tramp-file-name-handler 'file-remote-p filename
'localname
)
2114 (tramp-file-name-handler 'file-remote-p newname
'localname
)
2116 (prefix (file-remote-p (if t1 filename newname
)))
2120 ;; Both files are on a remote host, with same user.
2123 (tramp-send-command-and-check
2124 v
(format "%s %s %s" cmd
2125 (tramp-shell-quote-argument localname1
)
2126 (tramp-shell-quote-argument localname2
))))
2127 (with-current-buffer (tramp-get-buffer v
)
2128 (goto-char (point-min))
2132 ;; Mask cp -f error.
2134 tramp-operation-not-permitted-regexp nil t
))
2136 (tramp-error-with-buffer
2138 "Copying directly failed, see buffer `%s' for details."
2141 ;; We are on the local host.
2144 ;; We can do it directly.
2145 ((let (file-name-handler-alist)
2146 (and (file-readable-p localname1
)
2147 ;; No sticky bit when renaming.
2151 (file-modes (file-name-directory localname1
))
2152 (tramp-compat-octal-to-decimal "1000"))))
2153 (file-writable-p (file-name-directory localname2
))
2154 (or (file-directory-p localname2
)
2155 (file-writable-p localname2
))))
2157 (tramp-compat-copy-file
2158 localname1 localname2 ok-if-already-exists
2159 keep-date preserve-uid-gid
)
2160 (tramp-run-real-handler
2161 'rename-file
(list localname1 localname2 ok-if-already-exists
))))
2163 ;; We can do it directly with `tramp-send-command'
2164 ((and (file-readable-p (concat prefix localname1
))
2166 (file-name-directory (concat prefix localname2
)))
2167 (or (file-directory-p (concat prefix localname2
))
2168 (file-writable-p (concat prefix localname2
))))
2169 (tramp-do-copy-or-rename-file-directly
2170 op
(concat prefix localname1
) (concat prefix localname2
)
2171 ok-if-already-exists keep-date t
)
2172 ;; We must change the ownership to the local user.
2173 (tramp-set-file-uid-gid
2174 (concat prefix localname2
)
2175 (tramp-get-local-uid 'integer
)
2176 (tramp-get-local-gid 'integer
)))
2178 ;; We need a temporary file in between.
2180 ;; Create the temporary file.
2181 (let ((tmpfile (tramp-compat-make-temp-file localname1
)))
2186 (tramp-barf-unless-okay
2189 (tramp-shell-quote-argument localname1
)
2190 (tramp-shell-quote-argument tmpfile
))
2191 "Copying directly failed, see buffer `%s' for details."
2192 (tramp-get-buffer v
))
2193 ;; We must change the ownership as remote user.
2194 ;; Since this does not work reliable, we also
2195 ;; give read permissions.
2197 (concat prefix tmpfile
)
2198 (tramp-compat-octal-to-decimal "0777"))
2199 (tramp-set-file-uid-gid
2200 (concat prefix tmpfile
)
2201 (tramp-get-local-uid 'integer
)
2202 (tramp-get-local-gid 'integer
)))
2205 (tramp-compat-copy-file
2206 localname1 tmpfile t
2207 keep-date preserve-uid-gid
)
2208 (tramp-run-real-handler
2210 (list localname1 tmpfile t
)))
2211 ;; We must change the ownership as local user.
2212 ;; Since this does not work reliable, we also
2213 ;; give read permissions.
2215 tmpfile
(tramp-compat-octal-to-decimal "0777"))
2216 (tramp-set-file-uid-gid
2218 (tramp-get-remote-uid v
'integer
)
2219 (tramp-get-remote-gid v
'integer
))))
2221 ;; Move the temporary file to its destination.
2224 (tramp-barf-unless-okay
2227 (tramp-shell-quote-argument tmpfile
)
2228 (tramp-shell-quote-argument localname2
))
2229 "Copying directly failed, see buffer `%s' for details."
2230 (tramp-get-buffer v
)))
2232 (tramp-run-real-handler
2234 (list tmpfile localname2 ok-if-already-exists
)))))
2237 (ignore-errors (delete-file tmpfile
)))))))))
2239 ;; Set the time and mode. Mask possible errors.
2242 (set-file-times newname file-times
)
2243 (set-file-modes newname file-modes
))))))
2245 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date
)
2246 "Invoke `scp' program to copy.
2247 The method used must be an out-of-band method."
2248 (let* ((t1 (tramp-tramp-file-p filename
))
2249 (t2 (tramp-tramp-file-p newname
))
2250 (orig-vec (tramp-dissect-file-name (if t1 filename newname
)))
2251 copy-program copy-args copy-env copy-keep-date port listener spec
2252 options source target remote-copy-program remote-copy-args
)
2254 (with-parsed-tramp-file-name (if t1 filename newname
) nil
2257 ;; Both are Tramp files. We shall optimize it when the
2258 ;; methods for FILENAME and NEWNAME are the same.
2259 (let* ((dir-flag (file-directory-p filename
))
2260 (tmpfile (tramp-compat-make-temp-file localname dir-flag
)))
2264 (file-name-nondirectory newname
) tmpfile
)))
2267 (tramp-do-copy-or-rename-file-out-of-band
2268 op filename tmpfile keep-date
)
2269 (tramp-do-copy-or-rename-file-out-of-band
2270 'rename tmpfile newname keep-date
))
2274 (tramp-compat-delete-directory
2275 (expand-file-name ".." tmpfile
) 'recursive
)
2276 (delete-file tmpfile
)))))
2278 ;; Set variables for computing the prompt for reading
2280 (setq tramp-current-method
(tramp-file-name-method v
)
2281 tramp-current-user
(or (tramp-file-name-user v
)
2282 (tramp-get-connection-property
2284 tramp-current-host
(tramp-file-name-real-host v
))
2286 ;; Expand hops. Might be necessary for gateway methods.
2287 (setq v
(car (tramp-compute-multi-hops v
)))
2288 (aset v
3 localname
)
2290 ;; Check which ones of source and target are Tramp files.
2292 (tramp-make-copy-program-file-name v
)
2293 (shell-quote-argument filename
))
2295 (if (and (file-directory-p filename
)
2297 (file-name-nondirectory filename
)
2298 (file-name-nondirectory newname
)))
2299 'file-name-directory
2302 (tramp-make-copy-program-file-name v
)
2303 (shell-quote-argument newname
))))
2305 ;; Check for host and port number. We cannot use
2306 ;; `tramp-file-name-port', because this returns also
2307 ;; `tramp-default-port', which might clash with settings in
2309 (setq host
(tramp-file-name-host v
)
2311 (when (string-match tramp-host-with-port-regexp host
)
2312 (setq port
(string-to-number (match-string 2 host
))
2313 host
(string-to-number (match-string 1 host
))))
2315 ;; Check for user. There might be an interactive setting.
2316 (setq user
(or (tramp-file-name-user v
)
2317 (tramp-get-connection-property v
"login-as" nil
)))
2319 ;; Check for listener port.
2320 (when (tramp-get-method-parameter method
'tramp-remote-copy-args
)
2321 (setq listener
(number-to-string (+ 50000 (random 10000))))
2323 (zerop (tramp-call-process v
"nc" nil nil nil
"-z" host listener
))
2324 (setq listener
(number-to-string (+ 50000 (random 10000))))))
2326 ;; Compose copy command.
2327 (setq host
(or host
"")
2330 spec
(format-spec-make
2331 ?t
(tramp-get-connection-property
2332 (tramp-get-connection-process v
) "temp-file" ""))
2333 options
(format-spec
2334 (if tramp-use-ssh-controlmaster-options
2335 tramp-ssh-controlmaster-options
"")
2337 spec
(format-spec-make
2338 ?h host ?u user ?p port ?r listener ?c options
2339 ?k
(if keep-date
" " ""))
2340 copy-program
(tramp-get-method-parameter
2341 method
'tramp-copy-program
)
2342 copy-keep-date
(tramp-get-method-parameter
2343 method
'tramp-copy-keep-date
)
2347 ;; " " has either been a replacement of "%k" (when
2348 ;; keep-date argument is non-nil), or a replacement
2349 ;; for the whole keep-date sublist.
2353 (tramp-get-method-parameter method
'tramp-copy-args
)
2358 (let ((y (mapcar (lambda (z) (format-spec z spec
)) x
)))
2359 (if (member "" y
) '(" ") y
))))))
2366 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
2367 (unless (member "" x
) (mapconcat 'identity x
" ")))
2368 (tramp-get-method-parameter method
'tramp-copy-env
)))
2371 (tramp-get-method-parameter method
'tramp-remote-copy-program
))
2376 (tramp-get-connection-property v
"remote-copy-args" nil
)
2377 (tramp-get-method-parameter method
'tramp-remote-copy-args
)))
2378 (setq remote-copy-args
2381 (let ((y (mapcar (lambda (z) (format-spec z spec
)) x
)))
2382 (if (member "" y
) '(" ") y
)))))
2384 ;; Check for local copy program.
2385 (unless (executable-find copy-program
)
2387 v
'file-error
"Cannot find local copy program: %s" copy-program
))
2389 ;; Install listener on the remote side. The prompt must be
2390 ;; consumed later on, when the process does not listen anymore.
2391 (when remote-copy-program
2392 (unless (with-tramp-connection-property
2393 v
(concat "remote-copy-program-" remote-copy-program
)
2394 (tramp-find-executable
2395 v remote-copy-program
(tramp-get-remote-path v
)))
2398 "Cannot find remote listener: %s" remote-copy-program
))
2399 (setq remote-copy-program
2403 (list remote-copy-program
) remote-copy-args
2404 (list (if t1
(concat "<" source
) (concat ">" target
)) "&"))
2406 (tramp-send-command v remote-copy-program
)
2410 "Listener process not running on remote host: `%s'"
2411 remote-copy-program
))
2412 (tramp-send-command v
(format "netstat -l | grep -q :%s" listener
))
2413 (while (not (tramp-send-command-and-check v nil
))
2415 v
(format "netstat -l | grep -q :%s" listener
)))))
2419 ;; The default directory must be remote.
2420 (let ((default-directory
2421 (file-name-directory (if t1 filename newname
)))
2422 (process-environment (copy-sequence process-environment
)))
2423 ;; Set the transfer process properties.
2424 (tramp-set-connection-property
2425 v
"process-name" (buffer-name (current-buffer)))
2426 (tramp-set-connection-property
2427 v
"process-buffer" (current-buffer))
2430 orig-vec
6 "%s=\"%s\"" (car copy-env
) (cadr copy-env
))
2431 (setenv (pop copy-env
) (pop copy-env
)))
2436 (if remote-copy-program
2437 (list (if t1
(concat ">" target
) (concat "<" source
)))
2438 (list source target
))))
2440 ;; Use an asynchronous process. By this, password can
2441 ;; be handled. We don't set a timeout, because the
2442 ;; copying of large files can last longer than 60
2444 (let ((p (apply 'start-process-shell-command
2445 (tramp-get-connection-name v
)
2446 (tramp-get-connection-buffer v
)
2450 (list "&&" "echo" "tramp_exit_status" "0"
2451 "||" "echo" "tramp_exit_status" "1")))))
2454 (mapconcat 'identity
(process-command p
) " "))
2455 (tramp-set-connection-property p
"vector" orig-vec
)
2456 (tramp-compat-set-process-query-on-exit-flag p nil
)
2457 (tramp-process-actions
2458 p v nil tramp-actions-copy-out-of-band
)
2460 ;; Check the return code.
2461 (goto-char (point-max))
2463 (re-search-backward "tramp_exit_status [0-9]+" nil t
)
2465 orig-vec
'file-error
2466 "Couldn't find exit status of `%s'"
2467 (mapconcat 'identity
(process-command p
) " ")))
2468 (skip-chars-forward "^ ")
2469 (unless (zerop (read (current-buffer)))
2472 orig-vec
'file-error
2473 "Error copying: `%s'"
2474 (buffer-substring (point-min) (point-at-eol))))))
2476 ;; Reset the transfer process properties.
2477 (tramp-set-connection-property v
"process-name" nil
)
2478 (tramp-set-connection-property v
"process-buffer" nil
)
2479 ;; Clear the remote prompt.
2480 (when (and remote-copy-program
2481 (not (tramp-send-command-and-check v nil
)))
2482 ;; Houston, we have a problem! Likely, the listener is
2483 ;; still running, so let's clear everything (but the
2484 ;; cached password).
2485 (tramp-cleanup-connection v
'keep-debug
'keep-password
))))
2487 ;; Handle KEEP-DATE argument.
2488 (when (and keep-date
(not copy-keep-date
))
2489 (set-file-times newname
(nth 5 (file-attributes filename
))))
2492 (unless (and keep-date copy-keep-date
)
2494 (set-file-modes newname
(tramp-default-file-modes filename
)))))
2496 ;; If the operation was `rename', delete the original file.
2497 (unless (eq op
'copy
)
2498 (if (file-regular-p filename
)
2499 (delete-file filename
)
2500 (tramp-compat-delete-directory filename
'recursive
))))))
2502 (defun tramp-sh-handle-make-directory (dir &optional parents
)
2503 "Like `make-directory' for Tramp files."
2504 (setq dir
(expand-file-name dir
))
2505 (with-parsed-tramp-file-name dir nil
2506 (tramp-flush-directory-property v
(file-name-directory localname
))
2508 (tramp-barf-unless-okay
2510 (if parents
"mkdir -p" "mkdir")
2511 (tramp-shell-quote-argument localname
))
2512 "Couldn't make directory %s" dir
))))
2514 (defun tramp-sh-handle-delete-directory (directory &optional recursive
)
2515 "Like `delete-directory' for Tramp files."
2516 (setq directory
(expand-file-name directory
))
2517 (with-parsed-tramp-file-name directory nil
2518 (tramp-flush-file-property v
(file-name-directory localname
))
2519 (tramp-flush-directory-property v localname
)
2520 (tramp-barf-unless-okay
2522 (if recursive
"rm -rf" "rmdir")
2523 (tramp-shell-quote-argument localname
))
2524 "Couldn't delete %s" directory
)))
2526 (defun tramp-sh-handle-delete-file (filename &optional trash
)
2527 "Like `delete-file' for Tramp files."
2528 (setq filename
(expand-file-name filename
))
2529 (with-parsed-tramp-file-name filename nil
2530 (tramp-flush-file-property v
(file-name-directory localname
))
2531 (tramp-flush-file-property v localname
)
2532 (tramp-barf-unless-okay
2534 (or (and trash
(tramp-get-remote-trash v
)) "rm -f")
2535 (tramp-shell-quote-argument localname
))
2536 "Couldn't delete %s" filename
)))
2540 ;; CCC: This does not seem to be enough. Something dies when
2541 ;; we try and delete two directories under Tramp :/
2542 (defun tramp-sh-handle-dired-recursive-delete-directory (filename)
2543 "Recursively delete the directory given.
2544 This is like `dired-recursive-delete-directory' for Tramp files."
2545 (with-parsed-tramp-file-name filename nil
2546 ;; Run a shell command 'rm -r <localname>'.
2547 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
2548 (unless (file-exists-p filename
)
2549 (tramp-error v
'file-error
"No such directory: %s" filename
))
2550 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>).
2553 (format "rm -rf %s" (tramp-shell-quote-argument localname
))
2554 ;; Don't read the output, do it explicitly.
2556 ;; Wait for the remote system to return to us...
2557 ;; This might take a while, allow it plenty of time.
2558 (tramp-wait-for-output (tramp-get-connection-process v
) 120)
2559 ;; Make sure that it worked...
2560 (tramp-flush-file-property v
(file-name-directory localname
))
2561 (tramp-flush-directory-property v localname
)
2562 (and (file-exists-p filename
)
2564 v
'file-error
"Failed to recursively delete %s" filename
))))
2566 (defun tramp-sh-handle-dired-compress-file (file &rest _ok-flag
)
2567 "Like `dired-compress-file' for Tramp files."
2568 ;; OK-FLAG is valid for XEmacs only, but not implemented.
2569 ;; Code stolen mainly from dired-aux.el.
2570 (with-parsed-tramp-file-name file nil
2571 (tramp-flush-file-property v localname
)
2574 (if (not (featurep 'xemacs
))
2576 (symbol-value 'dired-compress-file-suffixes
)
2577 ;; XEmacs has `dired-compression-method-alist', which is
2578 ;; transformed into `dired-compress-file-suffixes' structure.
2581 (list (concat (regexp-quote (nth 1 x
)) "\\'")
2583 (mapconcat 'identity
(nth 3 x
) " ")))
2584 (symbol-value 'dired-compression-method-alist
))))
2586 ;; See if any suffix rule matches this file name.
2588 (let (case-fold-search)
2589 (if (string-match (car (car suffixes
)) localname
)
2590 (setq suffix
(car suffixes
) suffixes nil
))
2591 (setq suffixes
(cdr suffixes
))))
2593 (cond ((file-symlink-p file
)
2595 ((and suffix
(nth 2 suffix
))
2596 ;; We found an uncompression rule.
2597 (with-tramp-progress-reporter
2598 v
0 (format "Uncompressing %s" file
)
2599 (when (tramp-send-command-and-check
2600 v
(concat (nth 2 suffix
) " "
2601 (tramp-shell-quote-argument localname
)))
2602 ;; `dired-remove-file' is not defined in XEmacs.
2603 (tramp-compat-funcall 'dired-remove-file file
)
2604 (string-match (car suffix
) file
)
2605 (concat (substring file
0 (match-beginning 0))))))
2607 ;; We don't recognize the file as compressed, so compress it.
2609 (with-tramp-progress-reporter v
0 (format "Compressing %s" file
)
2610 (when (tramp-send-command-and-check
2611 v
(concat "gzip -f "
2612 (tramp-shell-quote-argument localname
)))
2613 ;; `dired-remove-file' is not defined in XEmacs.
2614 (tramp-compat-funcall 'dired-remove-file file
)
2615 (cond ((file-exists-p (concat file
".gz"))
2616 (concat file
".gz"))
2617 ((file-exists-p (concat file
".z"))
2621 (defun tramp-sh-handle-insert-directory
2622 (filename switches
&optional wildcard full-directory-p
)
2623 "Like `insert-directory' for Tramp files."
2624 (setq filename
(expand-file-name filename
))
2625 (unless switches
(setq switches
""))
2626 (with-parsed-tramp-file-name filename nil
2627 (if (and (featurep 'ls-lisp
)
2628 (not (symbol-value 'ls-lisp-use-insert-directory-program
)))
2629 (tramp-handle-insert-directory
2630 filename switches wildcard full-directory-p
)
2631 (when (stringp switches
)
2632 (setq switches
(split-string switches
)))
2633 (when (and (member "--dired" switches
)
2634 (not (tramp-get-ls-command-with-dired v
)))
2635 (setq switches
(delete "--dired" switches
)))
2637 (setq wildcard
(tramp-run-real-handler
2638 'file-name-nondirectory
(list localname
)))
2639 (setq localname
(tramp-run-real-handler
2640 'file-name-directory
(list localname
))))
2641 (unless (or full-directory-p
(member "-d" switches
))
2642 (setq switches
(append switches
'("-d"))))
2643 (setq switches
(mapconcat 'tramp-shell-quote-argument switches
" "))
2645 (setq switches
(concat switches
" " wildcard
)))
2647 v
4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2648 switches filename
(if wildcard
"yes" "no")
2649 (if full-directory-p
"yes" "no"))
2650 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2651 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2652 (if full-directory-p
2655 (format "%s %s %s 2>/dev/null"
2656 (tramp-get-ls-command v
)
2660 (tramp-shell-quote-argument (concat localname
".")))))
2661 (tramp-barf-unless-okay
2663 (format "cd %s" (tramp-shell-quote-argument
2664 (tramp-run-real-handler
2665 'file-name-directory
(list localname
))))
2667 (tramp-shell-quote-argument
2668 (tramp-run-real-handler 'file-name-directory
(list localname
))))
2671 (format "%s %s %s 2>/dev/null"
2672 (tramp-get-ls-command v
)
2676 (tramp-run-real-handler
2677 'file-name-nondirectory
(list localname
)))))
2679 (tramp-shell-quote-argument
2680 (tramp-run-real-handler
2681 'file-name-nondirectory
(list localname
)))))))
2684 (let ((beg (point)))
2685 (narrow-to-region (point) (point))
2686 ;; We cannot use `insert-buffer-substring' because the Tramp
2687 ;; buffer changes its contents before insertion due to calling
2688 ;; `expand-file' and alike.
2690 (with-current-buffer (tramp-get-buffer v
)
2693 ;; Check for "--dired" output.
2695 (when (looking-at "//SUBDIRED//")
2697 (when (looking-at "//DIRED//\\s-+")
2698 (let ((databeg (match-end 0))
2699 (end (point-at-eol)))
2700 ;; Now read the numeric positions of file names.
2702 (while (< (point) end
)
2703 (let ((start (+ beg
(read (current-buffer))))
2704 (end (+ beg
(read (current-buffer)))))
2705 (if (memq (char-after end
) '(?
\n ?\
))
2706 ;; End is followed by \n or by " -> ".
2707 (put-text-property start end
'dired-filename t
))))))
2708 ;; Remove trailing lines.
2709 (goto-char (point-at-bol))
2710 (while (looking-at "//")
2712 (delete-region (match-beginning 0) (point)))
2714 ;; Some busyboxes are reluctant to discard colors.
2716 (string-match "color" (tramp-get-connection-property v
"ls" ""))
2718 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
2719 (replace-match "")))
2721 ;; Decode the output, it could be multibyte.
2722 (decode-coding-region
2724 (or file-name-coding-system
2725 (and (boundp 'default-file-name-coding-system
)
2726 (symbol-value 'default-file-name-coding-system
))))
2728 ;; The inserted file could be from somewhere else.
2729 (when (and (not wildcard
) (not full-directory-p
))
2730 (goto-char (point-max))
2731 (when (file-symlink-p filename
)
2732 (goto-char (search-backward "->" beg
'noerror
)))
2734 (if (zerop (length (file-name-nondirectory filename
)))
2736 (file-name-nondirectory filename
))
2738 (replace-match (file-relative-name filename
) t
))
2740 (goto-char (point-max)))))))
2742 ;; Canonicalization of file names.
2744 (defun tramp-sh-handle-expand-file-name (name &optional dir
)
2745 "Like `expand-file-name' for Tramp files.
2746 If the localname part of the given file name starts with \"/../\" then
2747 the result will be a local, non-Tramp, file name."
2748 ;; If DIR is not given, use `default-directory' or "/".
2749 (setq dir
(or dir default-directory
"/"))
2750 ;; Unless NAME is absolute, concat DIR and NAME.
2751 (unless (file-name-absolute-p name
)
2752 (setq name
(concat (file-name-as-directory dir
) name
)))
2753 ;; If NAME is not a Tramp file, run the real handler.
2754 (if (not (tramp-connectable-p name
))
2755 (tramp-run-real-handler 'expand-file-name
(list name nil
))
2757 (with-parsed-tramp-file-name name nil
2758 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
2759 (setq localname
(concat "~/" localname
)))
2760 ;; Tilde expansion if necessary. This needs a shell which
2761 ;; groks tilde expansion! The function `tramp-find-shell' is
2762 ;; supposed to find such a shell on the remote host. Please
2763 ;; tell me about it when this doesn't work on your system.
2764 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname
)
2765 (let ((uname (match-string 1 localname
))
2766 (fname (match-string 2 localname
)))
2767 ;; We cannot simply apply "~/", because under sudo "~/" is
2768 ;; expanded to the local user home directory but to the
2769 ;; root home directory. On the other hand, using always
2770 ;; the default user name for tilde expansion is not
2771 ;; appropriate either, because ssh and companions might
2772 ;; use a user name from the config file.
2773 (when (and (string-equal uname
"~")
2774 (string-match "\\`su\\(do\\)?\\'" method
))
2775 (setq uname
(concat uname user
)))
2777 (with-tramp-connection-property v uname
2779 v
(format "cd %s; pwd" (tramp-shell-quote-argument uname
)))
2780 (with-current-buffer (tramp-get-buffer v
)
2781 (goto-char (point-min))
2782 (buffer-substring (point) (point-at-eol)))))
2783 (setq localname
(concat uname fname
))))
2784 ;; There might be a double slash, for example when "~/"
2785 ;; expands to "/". Remove this.
2786 (while (string-match "//" localname
)
2787 (setq localname
(replace-match "/" t t localname
)))
2788 ;; No tilde characters in file name, do normal
2789 ;; `expand-file-name' (this does "/./" and "/../"). We bind
2790 ;; `directory-sep-char' here for XEmacs on Windows, which would
2791 ;; otherwise use backslash. `default-directory' is bound,
2792 ;; because on Windows there would be problems with UNC shares or
2794 (let ((directory-sep-char ?
/)
2795 (default-directory (tramp-compat-temporary-file-directory)))
2796 (tramp-make-tramp-file-name
2798 (tramp-drop-volume-letter
2799 (tramp-run-real-handler
2800 'expand-file-name
(list localname
)))
2803 ;;; Remote commands:
2805 (defun tramp-process-sentinel (proc event
)
2806 "Flush file caches."
2807 (unless (memq (process-status proc
) '(run open
))
2808 (let ((vec (tramp-get-connection-property proc
"vector" nil
)))
2810 (tramp-message vec
5 "Sentinel called: `%S' `%s'" proc event
)
2811 (tramp-flush-connection-property proc
)
2812 (tramp-flush-directory-property vec
"")))))
2814 ;; We use BUFFER also as connection buffer during setup. Because of
2815 ;; this, its original contents must be saved, and restored once
2816 ;; connection has been setup.
2817 (defun tramp-sh-handle-start-file-process (name buffer program
&rest args
)
2818 "Like `start-file-process' for Tramp files."
2819 (with-parsed-tramp-file-name (expand-file-name default-directory
) nil
2820 (let* (;; When PROGRAM matches "*sh", and the first arg is "-c",
2821 ;; it might be that the arguments exceed the command line
2822 ;; length. Therefore, we modify the command.
2823 (heredoc (and (stringp program
)
2824 (string-match "sh$" program
)
2825 (string-equal "-c" (car args
))
2826 (= (length args
) 2)))
2827 ;; When PROGRAM is nil, we just provide a tty.
2828 (args (if (not heredoc
) args
2830 (while (and (< i
(length (cadr args
)))
2831 (string-match " " (cadr args
) i
))
2834 (list (replace-match " \\\\\n" nil nil
(cadr args
))))
2835 (setq i
(+ i
250))))
2838 (when (stringp program
)
2839 (format "cd %s && exec %s env PS1=%s %s"
2840 (tramp-shell-quote-argument localname
)
2841 (if heredoc
(format "<<'%s'" tramp-end-of-heredoc
) "")
2842 ;; Use a human-friendly prompt, for example for `shell'.
2843 (tramp-shell-quote-argument
2845 (file-remote-p default-directory
)
2846 tramp-initial-end-of-output
))
2848 (format "%s\n(\n%s\n) </dev/tty\n%s"
2849 program
(car args
) tramp-end-of-heredoc
)
2850 (mapconcat 'tramp-shell-quote-argument
2851 (cons program args
) " ")))))
2852 (tramp-process-connection-type
2853 (or (null program
) tramp-process-connection-type
))
2854 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
2857 ;; We do not want to raise an error when
2858 ;; `start-file-process' has been started several time in
2859 ;; `eshell' and friends.
2860 (tramp-current-connection nil
))
2863 ;; BUFFER can be nil. We use a temporary buffer.
2864 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
2865 (while (get-process name1
)
2866 ;; NAME must be unique as process name.
2868 name1
(format "%s<%d>" name i
)))
2870 ;; Set the new process properties.
2871 (tramp-set-connection-property v
"process-name" name
)
2872 (tramp-set-connection-property v
"process-buffer" buffer
)
2874 (with-current-buffer (tramp-get-connection-buffer v
)
2876 ;; We catch this event. Otherwise, `start-process' could
2877 ;; be called on the local host.
2880 ;; Activate narrowing in order to save BUFFER
2881 ;; contents. Clear also the modification time;
2882 ;; otherwise we might be interrupted by
2883 ;; `verify-visited-file-modtime'.
2884 (let ((buffer-undo-list t
)
2885 (buffer-read-only nil
)
2887 (clear-visited-file-modtime)
2888 (narrow-to-region (point-max) (point-max))
2889 ;; We call `tramp-maybe-open-connection', in order
2890 ;; to cleanup the prompt afterwards.
2892 (tramp-maybe-open-connection v
)
2894 (delete-region mark
(point))
2895 (narrow-to-region (point-max) (point-max))
2898 ;; Send the command.
2899 (tramp-send-command v command nil t
) ; nooutput
2900 ;; Check, whether a pty is associated.
2901 (unless (tramp-compat-process-get
2902 (tramp-get-connection-process v
) 'remote-tty
)
2905 "pty association is not supported for `%s'" name
))))
2906 (let ((p (tramp-get-connection-process v
)))
2907 ;; Set query flag and process marker for this
2908 ;; process. We ignore errors, because the process
2909 ;; could have finished already.
2911 (tramp-compat-set-process-query-on-exit-flag p t
)
2912 (set-marker (process-mark p
) (point)))
2917 (if (string-match tramp-temp-buffer-name
(buffer-name))
2919 (set-process-buffer (tramp-get-connection-process v
) nil
)
2920 (kill-buffer (current-buffer)))
2921 (set-buffer-modified-p bmp
))
2922 (tramp-set-connection-property v
"process-name" nil
)
2923 (tramp-set-connection-property v
"process-buffer" nil
))))))
2925 (defun tramp-sh-handle-process-file
2926 (program &optional infile destination display
&rest args
)
2927 "Like `process-file' for Tramp files."
2928 ;; The implementation is not complete yet.
2929 (when (and (numberp destination
) (zerop destination
))
2930 (error "Implementation does not handle immediate return"))
2932 (with-parsed-tramp-file-name default-directory nil
2933 (let (command input tmpinput stderr tmpstderr outbuf ret
)
2935 (setq command
(mapconcat 'tramp-shell-quote-argument
2936 (cons program args
) " "))
2939 (setq input
"/dev/null")
2940 (setq infile
(expand-file-name infile
))
2941 (if (tramp-equal-remote default-directory infile
)
2942 ;; INFILE is on the same remote host.
2943 (setq input
(with-parsed-tramp-file-name infile nil localname
))
2944 ;; INFILE must be copied to remote host.
2945 (setq input
(tramp-make-tramp-temp-file v
)
2946 tmpinput
(tramp-make-tramp-file-name method user host input
))
2947 (copy-file infile tmpinput t
)))
2948 (when input
(setq command
(format "%s <%s" command input
)))
2950 ;; Determine output.
2953 ((bufferp destination
)
2954 (setq outbuf destination
))
2956 ((stringp destination
)
2957 (setq outbuf
(get-buffer-create destination
)))
2958 ;; (REAL-DESTINATION ERROR-DESTINATION)
2959 ((consp destination
)
2962 ((bufferp (car destination
))
2963 (setq outbuf
(car destination
)))
2964 ((stringp (car destination
))
2965 (setq outbuf
(get-buffer-create (car destination
))))
2967 (setq outbuf
(current-buffer))))
2970 ((stringp (cadr destination
))
2971 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
2972 (if (tramp-equal-remote default-directory
(cadr destination
))
2973 ;; stderr is on the same remote host.
2974 (setq stderr
(with-parsed-tramp-file-name
2975 (cadr destination
) nil localname
))
2976 ;; stderr must be copied to remote host. The temporary
2977 ;; file must be deleted after execution.
2978 (setq stderr
(tramp-make-tramp-temp-file v
)
2979 tmpstderr
(tramp-make-tramp-file-name
2980 method user host stderr
))))
2981 ;; stderr to be discarded.
2982 ((null (cadr destination
))
2983 (setq stderr
"/dev/null"))))
2986 (setq outbuf
(current-buffer))))
2987 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
2989 ;; Send the command. It might not return in time, so we protect
2990 ;; it. Call it in a subshell, in order to preserve working
2995 (if (tramp-send-command-and-check
2996 v
(format "\\cd %s; %s"
2997 (tramp-shell-quote-argument localname
)
3001 ;; We should add the output anyway.
3003 (with-current-buffer outbuf
3005 (with-current-buffer (tramp-get-connection-buffer v
)
3007 (when (and display
(get-buffer-window outbuf t
)) (redisplay))))
3008 ;; When the user did interrupt, we should do it also. We use
3009 ;; return code -1 as marker.
3011 (kill-buffer (tramp-get-connection-buffer v
))
3015 (kill-buffer (tramp-get-connection-buffer v
))
3018 ;; Provide error file.
3019 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
3021 ;; Cleanup. We remove all file cache values for the connection,
3022 ;; because the remote process could have changed them.
3023 (when tmpinput
(delete-file tmpinput
))
3025 ;; `process-file-side-effects' has been introduced with GNU
3026 ;; Emacs 23.2. If set to `nil', no remote file will be changed
3027 ;; by `program'. If it doesn't exist, we assume its default
3029 (unless (and (boundp 'process-file-side-effects
)
3030 (not (symbol-value 'process-file-side-effects
)))
3031 (tramp-flush-directory-property v
""))
3033 ;; Return exit status.
3038 (defun tramp-sh-handle-file-local-copy (filename)
3039 "Like `file-local-copy' for Tramp files."
3040 (with-parsed-tramp-file-name filename nil
3041 (unless (file-exists-p filename
)
3044 "Cannot make local copy of non-existing file `%s'" filename
))
3046 (let* ((size (nth 7 (file-attributes (file-truename filename
))))
3047 (rem-enc (tramp-get-inline-coding v
"remote-encoding" size
))
3048 (loc-dec (tramp-get-inline-coding v
"local-decoding" size
))
3049 (tmpfile (tramp-compat-make-temp-file filename
)))
3053 ;; `copy-file' handles direct copy and out-of-band methods.
3054 ((or (tramp-local-host-p v
)
3055 (tramp-method-out-of-band-p v size
))
3056 (copy-file filename tmpfile t t
))
3058 ;; Use inline encoding for file transfer.
3061 (with-tramp-progress-reporter
3063 (format "Encoding remote file `%s' with `%s'" filename rem-enc
)
3064 (tramp-barf-unless-okay
3065 v
(format rem-enc
(tramp-shell-quote-argument localname
))
3066 "Encoding remote file failed"))
3068 (with-tramp-progress-reporter
3069 v
3 (format "Decoding local file `%s' with `%s'"
3071 (if (functionp loc-dec
)
3072 ;; If local decoding is a function, we call it.
3073 ;; We must disable multibyte, because
3074 ;; `uudecode-decode-region' doesn't handle it
3077 (set-buffer-multibyte nil
)
3078 (insert-buffer-substring (tramp-get-buffer v
))
3079 (funcall loc-dec
(point-min) (point-max))
3080 ;; Unset `file-name-handler-alist'. Otherwise,
3081 ;; epa-file gets confused.
3082 (let (file-name-handler-alist
3083 (coding-system-for-write 'binary
))
3085 (point-min) (point-max) tmpfile nil
'no-message
)))
3087 ;; If tramp-decoding-function is not defined for this
3088 ;; method, we invoke tramp-decoding-command instead.
3089 (let ((tmpfile2 (tramp-compat-make-temp-file filename
)))
3090 ;; Unset `file-name-handler-alist'. Otherwise,
3091 ;; epa-file gets confused.
3092 (let (file-name-handler-alist
3093 (coding-system-for-write 'binary
))
3094 (with-current-buffer (tramp-get-buffer v
)
3096 (point-min) (point-max) tmpfile2 nil
'no-message
)))
3098 (tramp-call-local-coding-command
3099 loc-dec tmpfile2 tmpfile
)
3100 (delete-file tmpfile2
)))))
3102 ;; Set proper permissions.
3103 (set-file-modes tmpfile
(tramp-default-file-modes filename
))
3104 ;; Set local user ownership.
3105 (tramp-set-file-uid-gid tmpfile
)))
3107 ;; Oops, I don't know what to do.
3109 v
'file-error
"Wrong method specification for `%s'" method
)))
3113 (delete-file tmpfile
)
3114 (signal (car err
) (cdr err
))))
3116 (run-hooks 'tramp-handle-file-local-copy-hook
)
3119 ;; This is needed for XEmacs only. Code stolen from files.el.
3120 (defun tramp-sh-handle-insert-file-contents-literally
3121 (filename &optional visit beg end replace
)
3122 "Like `insert-file-contents-literally' for Tramp files."
3123 (let ((format-alist nil
)
3124 (after-insert-file-functions nil
)
3125 (coding-system-for-read 'no-conversion
)
3126 (coding-system-for-write 'no-conversion
)
3127 (find-buffer-file-type-function
3128 (if (fboundp 'find-buffer-file-type
)
3129 (symbol-function 'find-buffer-file-type
)
3131 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler
))
3132 (inhibit-file-name-operation 'insert-file-contents
))
3135 (fset 'find-buffer-file-type
(lambda (_filename) t
))
3136 (insert-file-contents filename visit beg end replace
))
3138 (if find-buffer-file-type-function
3139 (fset 'find-buffer-file-type find-buffer-file-type-function
)
3140 (fmakunbound 'find-buffer-file-type
)))))
3142 ;; CCC grok LOCKNAME
3143 (defun tramp-sh-handle-write-region
3144 (start end filename
&optional append visit lockname confirm
)
3145 "Like `write-region' for Tramp files."
3146 (setq filename
(expand-file-name filename
))
3147 (with-parsed-tramp-file-name filename nil
3148 ;; Following part commented out because we don't know what to do about
3149 ;; file locking, and it does not appear to be a problem to ignore it.
3150 ;; Ange-ftp ignores it, too.
3151 ;; (when (and lockname (stringp lockname))
3152 ;; (setq lockname (expand-file-name lockname)))
3153 ;; (unless (or (eq lockname nil)
3154 ;; (string= lockname filename))
3156 ;; "tramp-sh-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3158 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
3159 (when (and (not (featurep 'xemacs
)) confirm
(file-exists-p filename
))
3160 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename
))
3161 (tramp-error v
'file-error
"File not overwritten")))
3163 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename
'integer
))
3164 (tramp-get-remote-uid v
'integer
)))
3165 (gid (or (nth 3 (tramp-compat-file-attributes filename
'integer
))
3166 (tramp-get-remote-gid v
'integer
))))
3168 (if (and (tramp-local-host-p v
)
3169 ;; `file-writable-p' calls `file-expand-file-name'. We
3170 ;; cannot use `tramp-run-real-handler' therefore.
3171 (let (file-name-handler-alist)
3173 (file-writable-p (file-name-directory localname
))
3174 (or (file-directory-p localname
)
3175 (file-writable-p localname
)))))
3176 ;; Short track: if we are on the local host, we can run directly.
3177 (tramp-run-real-handler
3179 (list start end localname append
'no-message lockname confirm
))
3181 (let* ((modes (save-excursion (tramp-default-file-modes filename
)))
3182 ;; We use this to save the value of
3183 ;; `last-coding-system-used' after writing the tmp
3184 ;; file. At the end of the function, we set
3185 ;; `last-coding-system-used' to this saved value. This
3186 ;; way, any intermediary coding systems used while
3187 ;; talking to the remote shell or suchlike won't hose
3188 ;; this variable. This approach was snarfed from
3191 ;; Write region into a tmp file. This isn't really
3192 ;; needed if we use an encoding function, but currently
3193 ;; we use it always because this makes the logic
3194 ;; simpler. We must also set `temporary-file-directory',
3195 ;; because it could point to a remote directory.
3196 (temporary-file-directory
3197 (tramp-compat-temporary-file-directory))
3198 (tmpfile (or tramp-temp-buffer-file-name
3199 (tramp-compat-make-temp-file filename
))))
3201 ;; If `append' is non-nil, we copy the file locally, and let
3202 ;; the native `write-region' implementation do the job.
3203 (when append
(copy-file filename tmpfile
'ok
))
3205 ;; We say `no-message' here because we don't want the
3206 ;; visited file modtime data to be clobbered from the temp
3207 ;; file. We call `set-visited-file-modtime' ourselves later
3208 ;; on. We must ensure that `file-coding-system-alist'
3209 ;; matches `tmpfile'.
3210 (let (file-name-handler-alist
3211 (file-coding-system-alist
3212 (tramp-find-file-name-coding-system-alist filename tmpfile
)))
3214 (tramp-run-real-handler
3216 (list start end tmpfile append
'no-message lockname confirm
))
3218 (setq tramp-temp-buffer-file-name nil
)
3219 (delete-file tmpfile
)
3220 (signal (car err
) (cdr err
))))
3222 ;; Now, `last-coding-system-used' has the right value. Remember it.
3223 (when (boundp 'last-coding-system-used
)
3224 (setq coding-system-used
3225 (symbol-value 'last-coding-system-used
))))
3227 ;; The permissions of the temporary file should be set. If
3228 ;; FILENAME does not exist (eq modes nil) it has been
3229 ;; renamed to the backup file. This case `save-buffer'
3230 ;; handles permissions.
3231 ;; Ensure that it is still readable.
3235 (logior (or modes
0) (tramp-compat-octal-to-decimal "0400"))))
3237 ;; This is a bit lengthy due to the different methods
3238 ;; possible for file transfer. First, we check whether the
3239 ;; method uses an scp program. If so, we call it.
3240 ;; Otherwise, both encoding and decoding command must be
3241 ;; specified. However, if the method _also_ specifies an
3242 ;; encoding function, then that is used for encoding the
3243 ;; contents of the tmp file.
3244 (let* ((size (nth 7 (file-attributes tmpfile
)))
3245 (rem-dec (tramp-get-inline-coding v
"remote-decoding" size
))
3246 (loc-enc (tramp-get-inline-coding v
"local-encoding" size
)))
3248 ;; `copy-file' handles direct copy and out-of-band methods.
3249 ((or (tramp-local-host-p v
)
3250 (tramp-method-out-of-band-p v size
))
3251 (if (and (not (stringp start
))
3252 (= (or end
(point-max)) (point-max))
3253 (= (or start
(point-min)) (point-min))
3254 (tramp-get-method-parameter
3255 method
'tramp-copy-keep-tmpfile
))
3257 (setq tramp-temp-buffer-file-name tmpfile
)
3259 ;; We keep the local file for performance
3260 ;; reasons, useful for "rsync".
3261 (copy-file tmpfile filename t
)
3263 (setq tramp-temp-buffer-file-name nil
)
3264 (delete-file tmpfile
)
3265 (signal (car err
) (cdr err
)))))
3266 (setq tramp-temp-buffer-file-name nil
)
3267 ;; Don't rename, in order to keep context in SELinux.
3269 (copy-file tmpfile filename t
)
3270 (delete-file tmpfile
))))
3272 ;; Use inline file transfer.
3277 (set-buffer-multibyte nil
)
3278 ;; Use encoding function or command.
3279 (with-tramp-progress-reporter
3280 v
3 (format "Encoding local file `%s' using `%s'"
3282 (if (functionp loc-enc
)
3283 ;; The following `let' is a workaround for
3284 ;; the base64.el that comes with pgnus-0.84.
3285 ;; If both of the following conditions are
3286 ;; satisfied, it tries to write to a local
3287 ;; file in default-directory, but at this
3288 ;; point, default-directory is remote.
3289 ;; (`call-process-region' can't write to
3290 ;; remote files, it seems.) The file in
3291 ;; question is a tmp file anyway.
3292 (let ((coding-system-for-read 'binary
)
3294 (tramp-compat-temporary-file-directory)))
3295 (insert-file-contents-literally tmpfile
)
3296 (funcall loc-enc
(point-min) (point-max)))
3298 (unless (zerop (tramp-call-local-coding-command
3302 (concat "Cannot write to `%s', "
3303 "local encoding command `%s' failed")
3304 filename loc-enc
))))
3306 ;; Send buffer into remote decoding command which
3307 ;; writes to remote file. Because this happens on
3308 ;; the remote host, we cannot use the function.
3309 (with-tramp-progress-reporter
3310 v
3 (format "Decoding remote file `%s' using `%s'"
3312 (goto-char (point-max))
3313 (unless (bolp) (newline))
3317 (concat rem-dec
" <<'%s'\n%s%s")
3318 (tramp-shell-quote-argument localname
)
3319 tramp-end-of-heredoc
3321 tramp-end-of-heredoc
))
3322 (tramp-barf-unless-okay
3324 "Couldn't write region to `%s', decode using `%s' failed"
3326 ;; When `file-precious-flag' is set, the region is
3327 ;; written to a temporary file. Check that the
3328 ;; checksum is equal to that from the local tmpfile.
3329 (when file-precious-flag
3332 ;; cksum runs locally, if possible.
3333 (zerop (tramp-call-process v
"cksum" tmpfile t
))
3334 ;; cksum runs remotely.
3335 (tramp-send-command-and-check
3338 "cksum <%s" (tramp-shell-quote-argument localname
)))
3339 ;; ... they are different.
3343 (with-current-buffer (tramp-get-buffer v
)
3347 (concat "Couldn't write region to `%s',"
3348 " decode using `%s' failed")
3349 filename rem-dec
)))))
3352 (delete-file tmpfile
)))
3354 ;; That's not expected.
3358 (concat "Method `%s' should specify both encoding and "
3359 "decoding command or an scp program")
3362 ;; Make `last-coding-system-used' have the right value.
3363 (when coding-system-used
3364 (set 'last-coding-system-used coding-system-used
))))
3366 (tramp-flush-file-property v
(file-name-directory localname
))
3367 (tramp-flush-file-property v localname
)
3369 ;; We must protect `last-coding-system-used', now we have set it
3370 ;; to its correct value.
3371 (let (last-coding-system-used (need-chown t
))
3372 ;; Set file modification time.
3373 (when (or (eq visit t
) (stringp visit
))
3374 (let ((file-attr (tramp-compat-file-attributes filename
'integer
)))
3375 (set-visited-file-modtime
3376 ;; We must pass modtime explicitly, because FILENAME can
3377 ;; be different from (buffer-file-name), f.e. if
3378 ;; `file-precious-flag' is set.
3380 (when (and (= (nth 2 file-attr
) uid
)
3381 (= (nth 3 file-attr
) gid
))
3382 (setq need-chown nil
))))
3384 ;; Set the ownership.
3386 (tramp-set-file-uid-gid filename uid gid
))
3387 (when (or (eq visit t
) (null visit
) (stringp visit
))
3388 (tramp-message v
0 "Wrote %s" filename
))
3389 (run-hooks 'tramp-handle-write-region-hook
)))))
3391 (defvar tramp-vc-registered-file-names nil
3392 "List used to collect file names, which are checked during `vc-registered'.")
3394 ;; VC backends check for the existence of various different special
3395 ;; files. This is very time consuming, because every single check
3396 ;; requires a remote command (the file cache must be invalidated).
3397 ;; Therefore, we apply a kind of optimization. We install the file
3398 ;; name handler `tramp-vc-file-name-handler', which does nothing but
3399 ;; remembers all file names for which `file-exists-p' or
3400 ;; `file-readable-p' has been applied. A first run of `vc-registered'
3401 ;; is performed. Afterwards, a script is applied for all collected
3402 ;; file names, using just one remote command. The result of this
3403 ;; script is used to fill the file cache with actual values. Now we
3404 ;; can reset the file name handlers, and we make a second run of
3405 ;; `vc-registered', which returns the expected result without sending
3406 ;; any other remote command.
3407 (defun tramp-sh-handle-vc-registered (file)
3408 "Like `vc-registered' for Tramp files."
3409 (tramp-compat-with-temp-message ""
3410 (with-parsed-tramp-file-name file nil
3411 (with-tramp-progress-reporter
3412 v
3 (format "Checking `vc-registered' for %s" file
)
3414 ;; There could be new files, created by the vc backend. We
3415 ;; cannot reuse the old cache entries, therefore. In
3416 ;; `tramp-get-file-property', `remote-file-name-inhibit-cache'
3417 ;; could also be a timestamp as `current-time' returns. This
3418 ;; means invalidate all cache entries with an older timestamp.
3419 (let (tramp-vc-registered-file-names
3420 (remote-file-name-inhibit-cache (current-time))
3421 (file-name-handler-alist
3422 `((,tramp-file-name-regexp . tramp-vc-file-name-handler
))))
3424 ;; Here we collect only file names, which need an operation.
3425 (ignore-errors (tramp-run-real-handler 'vc-registered
(list file
)))
3426 (tramp-message v
10 "\n%s" tramp-vc-registered-file-names
)
3428 ;; Send just one command, in order to fill the cache.
3429 (when tramp-vc-registered-file-names
3430 (tramp-maybe-send-script
3432 (format tramp-vc-registered-read-file-names
3433 (tramp-get-file-exists-command v
)
3434 (format "%s -r" (tramp-get-test-command v
)))
3435 "tramp_vc_registered_read_file_names")
3440 ;; We cannot use `tramp-send-command-and-read',
3441 ;; because this does not cooperate well with
3442 ;; heredoc documents.
3446 "tramp_vc_registered_read_file_names <<'%s'\n%s\n%s\n"
3447 tramp-end-of-heredoc
3448 (mapconcat 'tramp-shell-quote-argument
3449 tramp-vc-registered-file-names
3451 tramp-end-of-heredoc
))
3452 (with-current-buffer (tramp-get-connection-buffer v
)
3453 ;; Read the expression.
3454 (goto-char (point-min))
3455 (read (current-buffer)))))
3457 (tramp-set-file-property
3458 v
(car elt
) (cadr elt
) (cadr (cdr elt
))))))
3460 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3461 ;; calls shall be answered from the file cache. We unset
3462 ;; `process-file-side-effects' and `remote-file-name-inhibit-cache'
3463 ;; in order to keep the cache.
3464 (let ((vc-handled-backends vc-handled-backends
)
3465 remote-file-name-inhibit-cache process-file-side-effects
)
3466 ;; Reduce `vc-handled-backends' in order to minimize process calls.
3467 (when (and (memq 'Bzr vc-handled-backends
)
3468 (boundp 'vc-bzr-program
)
3469 (not (with-tramp-connection-property v vc-bzr-program
3470 (tramp-find-executable
3471 v vc-bzr-program
(tramp-get-remote-path v
)))))
3472 (setq vc-handled-backends
(remq 'Bzr vc-handled-backends
)))
3473 (when (and (memq 'Git vc-handled-backends
)
3474 (boundp 'vc-git-program
)
3475 (not (with-tramp-connection-property v vc-git-program
3476 (tramp-find-executable
3477 v vc-git-program
(tramp-get-remote-path v
)))))
3478 (setq vc-handled-backends
(remq 'Git vc-handled-backends
)))
3479 (when (and (memq 'Hg vc-handled-backends
)
3480 (boundp 'vc-hg-program
)
3481 (not (with-tramp-connection-property v vc-hg-program
3482 (tramp-find-executable
3483 v vc-hg-program
(tramp-get-remote-path v
)))))
3484 (setq vc-handled-backends
(remq 'Hg vc-handled-backends
)))
3487 (tramp-run-real-handler 'vc-registered
(list file
))))))))
3489 ;;;###tramp-autoload
3490 (defun tramp-sh-file-name-handler (operation &rest args
)
3491 "Invoke remote-shell Tramp file name handler.
3492 Fall back to normal file name handler if no Tramp handler exists."
3493 (when (and tramp-locked
(not tramp-locker
))
3494 (setq tramp-locked nil
)
3496 (car-safe tramp-current-connection
) 'file-error
3497 "Forbidden reentrant call of Tramp"))
3498 (let ((tl tramp-locked
))
3499 (setq tramp-locked t
)
3501 (let ((tramp-locker t
))
3503 (let ((fn (assoc operation tramp-sh-file-name-handler-alist
)))
3505 (apply (cdr fn
) args
)
3506 (tramp-run-real-handler operation args
)))))
3507 (setq tramp-locked tl
))))
3509 (defun tramp-vc-file-name-handler (operation &rest args
)
3510 "Invoke special file name handler, which collects files to be handled."
3513 (tramp-replace-environment-variables
3514 (apply 'tramp-file-name-for-operation operation args
)))
3515 (fn (assoc operation tramp-sh-file-name-handler-alist
)))
3516 (with-parsed-tramp-file-name filename nil
3518 ;; That's what we want: file names, for which checks are
3519 ;; applied. We assume that VC uses only `file-exists-p' and
3520 ;; `file-readable-p' checks; otherwise we must extend the
3521 ;; list. We do not perform any action, but return nil, in
3522 ;; order to keep `vc-registered' running.
3523 ((and fn
(memq operation
'(file-exists-p file-readable-p
)))
3524 (add-to-list 'tramp-vc-registered-file-names localname
'append
)
3526 ;; `process-file' and `start-file-process' shall be ignored.
3527 ((and fn
(eq operation
'process-file
) 0))
3528 ((and fn
(eq operation
'start-file-process
) nil
))
3529 ;; Tramp file name handlers like `expand-file-name'. They
3531 (fn (save-match-data (apply (cdr fn
) args
)))
3532 ;; Default file name handlers, we don't care.
3533 (t (tramp-run-real-handler operation args
)))))))
3535 (defun tramp-sh-handle-file-notify-add-watch (file-name flags _callback
)
3536 "Like `file-notify-add-watch' for Tramp files."
3537 (setq file-name
(expand-file-name file-name
))
3538 (with-parsed-tramp-file-name file-name nil
3539 (let* ((default-directory (file-name-directory file-name
))
3540 command events filter p sequence
)
3542 ;; gvfs-monitor-dir.
3543 ((setq command
(tramp-get-remote-gvfs-monitor-dir v
))
3544 (setq filter
'tramp-sh-file-gvfs-monitor-dir-process-filter
3545 sequence
`(,command
,localname
)))
3547 ((setq command
(tramp-get-remote-inotifywait v
))
3548 (setq filter
'tramp-sh-file-inotifywait-process-filter
3551 ((and (memq 'change flags
) (memq 'attribute-change flags
))
3552 "create,modify,move,delete,attrib")
3553 ((memq 'change flags
) "create,modify,move,delete")
3554 ((memq 'attribute-change flags
) "attrib"))
3555 sequence
`(,command
"-mq" "-e" ,events
,localname
)))
3558 v
'file-notify-error
3559 "No file notification program found on %s"
3560 (file-remote-p file-name
))))
3564 (file-name-nondirectory command
)
3565 (generate-new-buffer
3566 (format " *%s*" (file-name-nondirectory command
)))
3568 ;; Return the process object as watch-descriptor.
3569 (if (not (processp p
))
3571 v
'file-notify-error
3572 "`%s' failed to start on remote host"
3573 (mapconcat 'identity sequence
" "))
3574 (tramp-message v
6 "Run `%s', %S" (mapconcat 'identity sequence
" ") p
)
3575 (tramp-set-connection-property p
"vector" v
)
3576 (tramp-compat-set-process-query-on-exit-flag p nil
)
3577 (set-process-filter p filter
)
3580 (defun tramp-sh-file-gvfs-monitor-dir-process-filter (proc string
)
3581 "Read output from \"gvfs-monitor-dir\" and add corresponding file-notify events."
3582 (let ((remote-prefix
3583 (with-current-buffer (process-buffer proc
)
3584 (file-remote-p default-directory
)))
3585 (rest-string (tramp-compat-process-get proc
'rest-string
)))
3587 (tramp-message proc
10 "Previous string:\n%s" rest-string
))
3588 (tramp-message proc
6 "%S\n%s" proc string
)
3589 (setq string
(concat rest-string string
)
3590 ;; Attribute change is returned in unused wording.
3591 string
(tramp-compat-replace-regexp-in-string
3592 "ATTRIB CHANGED" "ATTRIBUTE_CHANGED" string
))
3594 (while (string-match
3596 "Directory Monitor Event:[\n\r]+"
3597 "Child = \\([^\n\r]+\\)[\n\r]+"
3598 "\\(Other = \\([^\n\r]+\\)[\n\r]+\\)?"
3599 "Event = \\([^[:blank:]]+\\)[\n\r]+")
3605 (tramp-compat-replace-regexp-in-string
3606 "_" "-" (downcase (match-string 4 string
))))
3607 ;; File names are returned as absolute paths. We must
3608 ;; add the remote prefix.
3609 (concat remote-prefix
(match-string 1 string
))
3610 (when (match-string 3 string
)
3611 (concat remote-prefix
(match-string 3 string
))))))
3612 (setq string
(replace-match "" nil nil string
))
3613 ;; Usually, we would add an Emacs event now. Unfortunately,
3614 ;; `unread-command-events' does not accept several events at
3615 ;; once. Therefore, we apply the callback directly.
3616 (tramp-compat-funcall 'file-notify-callback object
)))
3618 ;; Save rest of the string.
3619 (when (zerop (length string
)) (setq string nil
))
3620 (when string
(tramp-message proc
10 "Rest string:\n%s" string
))
3621 (tramp-compat-process-put proc
'rest-string string
)))
3623 (defun tramp-sh-file-inotifywait-process-filter (proc string
)
3624 "Read output from \"inotifywait\" and add corresponding file-notify events."
3625 (tramp-message proc
6 "%S\n%s" proc string
)
3626 (dolist (line (split-string string
"[\n\r]+" 'omit-nulls
))
3627 ;; Check, whether there is a problem.
3630 (concat "^[^[:blank:]]+"
3631 "[[:blank:]]+\\([^[:blank:]]+\\)+"
3632 "\\([[:blank:]]+\\([^\n\r]+\\)\\)?")
3634 (tramp-error proc
'file-notify-error
"%s" line
))
3642 (tramp-compat-replace-regexp-in-string "_" "-" (downcase x
))))
3643 (split-string (match-string 1 line
) "," 'omit-nulls
))
3644 (match-string 3 line
))))
3645 ;; Usually, we would add an Emacs event now. Unfortunately,
3646 ;; `unread-command-events' does not accept several events at
3647 ;; once. Therefore, we apply the callback directly.
3648 (tramp-compat-funcall 'file-notify-callback object
))))
3650 ;;; Internal Functions:
3652 (defun tramp-maybe-send-script (vec script name
)
3653 "Define in remote shell function NAME implemented as SCRIPT.
3654 Only send the definition if it has not already been done."
3655 ;; We cannot let-bind (tramp-get-connection-process vec) because it
3657 (let ((scripts (tramp-get-connection-property
3658 (tramp-get-connection-process vec
) "scripts" nil
)))
3659 (unless (member name scripts
)
3660 (with-tramp-progress-reporter vec
5 (format "Sending script `%s'" name
)
3661 ;; The script could contain a call of Perl. This is masked with `%s'.
3662 (when (and (string-match "%s" script
)
3663 (not (tramp-get-remote-perl vec
)))
3664 (tramp-error vec
'file-error
"No Perl available on remote host"))
3665 (tramp-barf-unless-okay
3667 (format "%s () {\n%s\n}" name
3668 (format script
(tramp-get-remote-perl vec
)))
3669 "Script %s sending failed" name
)
3670 (tramp-set-connection-property
3671 (tramp-get-connection-process vec
) "scripts" (cons name scripts
))))))
3673 (defun tramp-set-auto-save ()
3674 (when (and ;; ange-ftp has its own auto-save mechanism
3675 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
3676 'tramp-sh-file-name-handler
)
3678 (auto-save-mode 1)))
3679 (add-hook 'find-file-hooks
'tramp-set-auto-save t
)
3680 (add-hook 'tramp-unload-hook
3682 (remove-hook 'find-file-hooks
'tramp-set-auto-save
)))
3684 (defun tramp-run-test (switch filename
)
3685 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3686 Returns the exit code of the `test' program."
3687 (with-parsed-tramp-file-name filename nil
3688 (tramp-send-command-and-check
3692 (tramp-get-test-command v
)
3694 (tramp-shell-quote-argument localname
)))))
3696 (defun tramp-run-test2 (format-string file1 file2
)
3697 "Run `test'-like program on the remote system, given FILE1, FILE2.
3698 FORMAT-STRING contains the program name, switches, and place holders.
3699 Returns the exit code of the `test' program. Barfs if the methods,
3700 hosts, or files, disagree."
3701 (unless (tramp-equal-remote file1 file2
)
3702 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1
) file1 file2
) nil
3705 "tramp-run-test2 only implemented for same method, user, host")))
3706 (with-parsed-tramp-file-name file1 v1
3707 (with-parsed-tramp-file-name file1 v2
3708 (tramp-send-command-and-check
3710 (format format-string
3711 (tramp-shell-quote-argument v1-localname
)
3712 (tramp-shell-quote-argument v2-localname
))))))
3714 (defun tramp-find-executable
3715 (vec progname dirlist
&optional ignore-tilde ignore-path
)
3716 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3717 First arg VEC specifies the connection, PROGNAME is the program
3718 to search for, and DIRLIST gives the list of directories to
3719 search. If IGNORE-TILDE is non-nil, directory names starting
3720 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3723 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3725 This function expects to be in the right *tramp* buffer."
3726 (with-current-buffer (tramp-get-connection-buffer vec
)
3728 ;; Check whether the executable is in $PATH. "which(1)" does not
3729 ;; report always a correct error code; therefore we check the
3730 ;; number of words it returns. "SunOS 5.10" (and maybe "SunOS
3731 ;; 5.11") have problems with this command, we disable the call
3733 (unless (or ignore-path
3735 (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3736 (tramp-get-connection-property vec
"uname" "")))
3737 (tramp-send-command vec
(format "which \\%s | wc -w" progname
))
3738 (goto-char (point-min))
3739 (if (looking-at "^\\s-*1$")
3740 (setq result
(concat "\\" progname
))))
3743 ;; Remove all ~/foo directories from dirlist. In XEmacs,
3744 ;; `remove' is in CL, and we want to avoid CL dependencies.
3747 (setq d
(car dirlist
))
3748 (setq dirlist
(cdr dirlist
))
3749 (unless (char-equal ?~
(aref d
0))
3750 (setq newdl
(cons d newdl
))))
3751 (setq dirlist
(nreverse newdl
))))
3754 (format (concat "while read d; "
3755 "do if test -x $d/%s -a -f $d/%s; "
3756 "then echo tramp_executable $d/%s; "
3757 "break; fi; done <<'%s'\n"
3759 progname progname progname
3760 tramp-end-of-heredoc
3761 (mapconcat 'identity dirlist
"\n")
3762 tramp-end-of-heredoc
))
3763 (goto-char (point-max))
3764 (when (search-backward "tramp_executable " nil t
)
3765 (skip-chars-forward "^ ")
3766 (skip-chars-forward " ")
3767 (setq result
(buffer-substring (point) (point-at-eol)))))
3770 (defun tramp-set-remote-path (vec)
3771 "Sets the remote environment PATH to existing directories.
3772 I.e., for each directory in `tramp-remote-path', it is tested
3773 whether it exists and if so, it is added to the environment
3775 (tramp-message vec
5 "Setting $PATH environment variable")
3777 vec
(format "PATH=%s; export PATH"
3778 (mapconcat 'identity
(tramp-get-remote-path vec
) ":"))))
3780 ;; ------------------------------------------------------------
3781 ;; -- Communication with external shell --
3782 ;; ------------------------------------------------------------
3784 (defun tramp-find-file-exists-command (vec)
3785 "Find a command on the remote host for checking if a file exists.
3786 Here, we are looking for a command which has zero exit status if the
3787 file exists and nonzero exit status otherwise."
3788 (let ((existing "/")
3790 (tramp-shell-quote-argument "/ this file does not exist "))
3792 ;; The algorithm is as follows: we try a list of several commands.
3793 ;; For each command, we first run `$cmd /' -- this should return
3794 ;; true, as the root directory always exists. And then we run
3795 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
3796 ;; does not exist. This should return false. We use the first
3797 ;; command we find that seems to work.
3798 ;; The list of commands to try is as follows:
3799 ;; `ls -d' This works on most systems, but NetBSD 1.4
3800 ;; has a bug: `ls' always returns zero exit
3801 ;; status, even for files which don't exist.
3802 ;; `test -e' Some Bourne shells have a `test' builtin
3803 ;; which does not know the `-e' option.
3804 ;; `/bin/test -e' For those, the `test' binary on disk normally
3805 ;; provides the option. Alas, the binary
3806 ;; is sometimes `/bin/test' and sometimes it's
3808 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3811 (and (setq result
(format "%s -e" (tramp-get-test-command vec
)))
3812 (tramp-send-command-and-check
3813 vec
(format "%s %s" result existing
))
3814 (not (tramp-send-command-and-check
3815 vec
(format "%s %s" result nonexistent
)))))
3817 (and (setq result
"/bin/test -e")
3818 (tramp-send-command-and-check
3819 vec
(format "%s %s" result existing
))
3820 (not (tramp-send-command-and-check
3821 vec
(format "%s %s" result nonexistent
)))))
3823 (and (setq result
"/usr/bin/test -e")
3824 (tramp-send-command-and-check
3825 vec
(format "%s %s" result existing
))
3826 (not (tramp-send-command-and-check
3827 vec
(format "%s %s" result nonexistent
)))))
3829 (and (setq result
(format "%s -d" (tramp-get-ls-command vec
)))
3830 (tramp-send-command-and-check
3831 vec
(format "%s %s" result existing
))
3832 (not (tramp-send-command-and-check
3833 vec
(format "%s %s" result nonexistent
))))))
3835 vec
'file-error
"Couldn't find command to check if file exists"))
3838 (defun tramp-open-shell (vec shell
)
3839 "Opens shell SHELL."
3840 (with-tramp-progress-reporter
3841 vec
5 (format "Opening remote shell `%s'" shell
)
3842 ;; Find arguments for this shell.
3843 (let ((alist tramp-sh-extra-args
)
3845 (while (and alist
(null extra-args
))
3846 (setq item
(pop alist
))
3847 (when (string-match (car item
) shell
)
3848 (setq extra-args
(cdr item
))))
3851 "exec env ENV='' HISTFILE=/dev/null PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s"
3852 (tramp-shell-quote-argument tramp-end-of-output
)
3853 shell
(or extra-args
""))
3855 (tramp-set-connection-property
3856 (tramp-get-connection-process vec
) "remote-shell" shell
)))
3858 (defun tramp-find-shell (vec)
3859 "Opens a shell on the remote host which groks tilde expansion."
3860 (with-current-buffer (tramp-get-buffer vec
)
3861 (let ((default-shell
3863 (tramp-get-connection-property
3864 (tramp-get-connection-process vec
) "remote-shell" nil
)
3865 (tramp-get-method-parameter
3866 (tramp-file-name-method vec
) 'tramp-remote-shell
)))
3869 (with-tramp-connection-property vec
"remote-shell"
3870 ;; CCC: "root" does not exist always, see QNAP 459.
3871 ;; Which check could we apply instead?
3872 (tramp-send-command vec
"echo ~root" t
)
3873 (if (or (string-match "^~root$" (buffer-string))
3874 ;; The default shell (ksh93) of OpenSolaris and
3875 ;; Solaris is buggy. We've got reports for
3876 ;; "SunOS 5.10" and "SunOS 5.11" so far.
3877 (string-match (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3878 (tramp-get-connection-property
3881 (or (tramp-find-executable
3882 vec
"bash" (tramp-get-remote-path vec
) t t
)
3883 (tramp-find-executable
3884 vec
"ksh" (tramp-get-remote-path vec
) t t
)
3885 ;; Maybe it works at least for some other commands.
3891 "Couldn't find a remote shell which groks tilde "
3892 "expansion, using `%s'")
3897 ;; Open a new shell if needed.
3898 (unless (string-equal shell default-shell
)
3900 vec
5 "Starting remote shell `%s' for tilde expansion" shell
)
3901 (tramp-open-shell vec shell
)))))
3903 ;; Utility functions.
3905 (defun tramp-barf-if-no-shell-prompt (proc timeout
&rest error-args
)
3906 "Wait for shell prompt and barf if none appears.
3907 Looks at process PROC to see if a shell prompt appears in TIMEOUT
3908 seconds. If not, it produces an error message with the given ERROR-ARGS."
3909 (let ((vec (tramp-get-connection-property proc
"vector" nil
)))
3911 (tramp-wait-for-regexp
3914 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern
))
3916 (delete-process proc
)
3917 (apply 'tramp-error-with-buffer
3918 (tramp-get-connection-buffer vec
) vec
'file-error error-args
)))))
3920 (defun tramp-open-connection-setup-interactive-shell (proc vec
)
3921 "Set up an interactive shell.
3922 Mainly sets the prompt and the echo correctly. PROC is the shell
3923 process to set up. VEC specifies the connection."
3924 (let ((tramp-end-of-output tramp-initial-end-of-output
))
3925 ;; It is useful to set the prompt in the following command because
3926 ;; some people have a setting for $PS1 which /bin/sh doesn't know
3927 ;; about and thus /bin/sh will display a strange prompt. For
3928 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
3929 ;; display the current working directory but /bin/sh will display
3930 ;; a dollar sign. The following command line sets $PS1 to a sane
3931 ;; value, and works under Bourne-ish shells as well as csh-like
3932 ;; shells. Daniel Pittman reports that the unusual positioning of
3933 ;; the single quotes makes it work under `rc', too. We also unset
3934 ;; the variable $ENV because that is read by some sh
3935 ;; implementations (eg, bash when called as sh) on startup; this
3936 ;; way, we avoid the startup file clobbering $PS1. $PROMPT_COMMAND
3937 ;; is another way to set the prompt in /bin/bash, it must be
3938 ;; discarded as well.
3941 (or (tramp-get-connection-property vec
"remote-shell" nil
)
3942 (tramp-get-method-parameter
3943 (tramp-file-name-method vec
) 'tramp-remote-shell
)))
3946 (tramp-message vec
5 "Setting up remote shell environment")
3947 (tramp-send-command vec
"stty -inlcr -echo kill '^U' erase '^H'" t
)
3948 ;; Check whether the echo has really been disabled. Some
3949 ;; implementations, like busybox of embedded GNU/Linux, don't
3950 ;; support disabling.
3951 (tramp-send-command vec
"echo foo" t
)
3952 (with-current-buffer (process-buffer proc
)
3953 (goto-char (point-min))
3954 (when (looking-at "echo foo")
3955 (tramp-set-connection-property proc
"remote-echo" t
)
3956 (tramp-message vec
5 "Remote echo still on. Ok.")
3957 ;; Make sure backspaces and their echo are enabled and no line
3958 ;; width magic interferes with them.
3959 (tramp-send-command vec
"stty icanon erase ^H cols 32767" t
))))
3961 (tramp-message vec
5 "Setting shell prompt")
3963 vec
(format "PS1=%s PS2='' PS3='' PROMPT_COMMAND=''"
3964 (tramp-shell-quote-argument tramp-end-of-output
)) t
)
3966 ;; Try to set up the coding system correctly.
3967 ;; CCC this can't be the right way to do it. Hm.
3968 (tramp-message vec
5 "Determining coding system")
3969 (with-current-buffer (process-buffer proc
)
3970 (if (featurep 'mule
)
3971 ;; Use MULE to select the right EOL convention for communicating
3972 ;; with the process.
3973 (let ((cs (or (when (string-match
3974 "utf8" (or (tramp-get-remote-locale vec
) ""))
3975 (cons 'utf-8
'utf-8
))
3976 (tramp-compat-funcall 'process-coding-system proc
)
3977 (cons 'undecided
'undecided
)))
3978 cs-decode cs-encode
)
3979 (when (symbolp cs
) (setq cs
(cons cs cs
)))
3980 (setq cs-decode
(car cs
))
3981 (setq cs-encode
(cdr cs
))
3982 (unless cs-decode
(setq cs-decode
'undecided
))
3983 (unless cs-encode
(setq cs-encode
'undecided
))
3984 (setq cs-encode
(tramp-compat-coding-system-change-eol-conversion
3986 (tramp-send-command vec
"echo foo ; echo bar" t
)
3987 (goto-char (point-min))
3988 (when (search-forward "\r" nil t
)
3989 (setq cs-decode
(tramp-compat-coding-system-change-eol-conversion
3991 (tramp-compat-funcall
3992 'set-buffer-process-coding-system cs-decode cs-encode
)
3994 vec
5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode
))
3995 ;; Look for ^M and do something useful if found.
3996 (when (search-forward "\r" nil t
)
3997 ;; We have found a ^M but cannot frob the process coding system
3998 ;; because we're running on a non-MULE Emacs. Let's try
4000 (tramp-send-command vec
"stty -onlcr" t
))))
4002 (tramp-send-command vec
"set +o vi +o emacs" t
)
4004 ;; Check whether the output of "uname -sr" has been changed. If
4005 ;; yes, this is a strong indication that we must expire all
4006 ;; connection properties. We start again with
4007 ;; `tramp-maybe-open-connection', it will be caught there.
4008 (tramp-message vec
5 "Checking system information")
4009 (let ((old-uname (tramp-get-connection-property vec
"uname" nil
))
4011 (tramp-set-connection-property
4013 (tramp-send-command-and-read vec
"echo \\\"`uname -sr`\\\""))))
4014 (when (and (stringp old-uname
) (not (string-equal old-uname new-uname
)))
4017 "Connection reset, because remote host changed from `%s' to `%s'"
4018 old-uname new-uname
)
4019 ;; We want to keep the password.
4020 (tramp-cleanup-connection vec t t
)
4021 (throw 'uname-changed
(tramp-maybe-open-connection vec
))))
4023 ;; Check whether the remote host suffers from buggy
4024 ;; `send-process-string'. This is known for FreeBSD (see comment in
4025 ;; `send_process', file process.c). I've tested sending 624 bytes
4026 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
4027 ;; this host type is detected locally. It cannot handle remote
4029 (with-tramp-connection-property proc
"chunksize"
4031 ((and (integerp tramp-chunksize
) (> tramp-chunksize
0))
4035 vec
5 "Checking remote host type for `send-process-string' bug")
4037 "^FreeBSD" (tramp-get-connection-property vec
"uname" ""))
4040 ;; Set remote PATH variable.
4041 (tramp-set-remote-path vec
)
4043 ;; Search for a good shell before searching for a command which
4044 ;; checks if a file exists. This is done because Tramp wants to use
4045 ;; "test foo; echo $?" to check if various conditions hold, and
4046 ;; there are buggy /bin/sh implementations which don't execute the
4047 ;; "echo $?" part if the "test" part has an error. In particular,
4048 ;; the OpenSolaris /bin/sh is a problem. There are also other
4049 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
4050 ;; in function declarations, or changing HISTFILE in place.
4051 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
4053 (tramp-find-shell vec
)
4055 ;; Disable unexpected output.
4056 (tramp-send-command vec
"mesg n; biff n" t
)
4058 ;; IRIX64 bash expands "!" even when in single quotes. This
4059 ;; destroys our shell functions, we must disable it. See
4060 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
4061 (when (string-match "^IRIX64" (tramp-get-connection-property vec
"uname" ""))
4062 (tramp-send-command vec
"set +H" t
))
4064 ;; On BSD-like systems, ?\t is expanded to spaces. Suppress this.
4065 (when (string-match "BSD\\|Darwin"
4066 (tramp-get-connection-property vec
"uname" ""))
4067 (tramp-send-command vec
"stty -oxtabs" t
))
4069 ;; Set `remote-tty' process property.
4070 (let ((tty (tramp-send-command-and-read vec
"echo \\\"`tty`\\\"" 'noerror
)))
4071 (unless (zerop (length tty
))
4072 (tramp-compat-process-put proc
'remote-tty tty
)))
4074 ;; Dump stty settings in the traces.
4075 (when (>= tramp-verbose
9)
4076 (tramp-send-command vec
"stty -a" t
))
4078 ;; Set the environment.
4079 (tramp-message vec
5 "Setting default environment")
4081 (let ((env (append (when (tramp-get-remote-locale vec
) ; Discard `(nil)'.
4082 `(,(tramp-get-remote-locale vec
)))
4083 (copy-sequence tramp-remote-process-environment
)))
4086 (setq item
(tramp-compat-split-string (car env
) "="))
4087 (setcdr item
(mapconcat 'identity
(cdr item
) "="))
4088 (if (and (stringp (cdr item
)) (not (string-equal (cdr item
) "")))
4089 (push (format "%s %s" (car item
) (cdr item
)) vars
)
4090 (push (car item
) unset
))
4091 (setq env
(cdr env
)))
4095 (format "while read var val; do export $var=$val; done <<'%s'\n%s\n%s"
4096 tramp-end-of-heredoc
4097 (mapconcat 'identity vars
"\n")
4098 tramp-end-of-heredoc
)
4102 vec
(format "unset %s" (mapconcat 'identity unset
" ")) t
))))
4104 ;; Old text from documentation of tramp-methods:
4105 ;; Using a uuencode/uudecode inline method is discouraged, please use one
4106 ;; of the base64 methods instead since base64 encoding is much more
4107 ;; reliable and the commands are more standardized between the different
4108 ;; Unix versions. But if you can't use base64 for some reason, please
4109 ;; note that the default uudecode command does not work well for some
4110 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
4111 ;; the following command for uudecode:
4113 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
4115 ;; For Irix, no solution is known yet.
4117 (autoload 'uudecode-decode-region
"uudecode")
4119 (defconst tramp-local-coding-commands
4120 `((b64 base64-encode-region base64-decode-region
)
4121 (uu tramp-uuencode-region uudecode-decode-region
)
4122 (pack ,(format tramp-perl-pack
"perl") ,(format tramp-perl-unpack
"perl")))
4123 "List of local coding commands for inline transfer.
4124 Each item is a list that looks like this:
4126 \(FORMAT ENCODING DECODING\)
4128 FORMAT is symbol describing the encoding/decoding format. It can be
4129 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
4131 ENCODING and DECODING can be strings, giving commands, or symbols,
4132 giving functions. If they are strings, then they can contain
4133 the \"%s\" format specifier. If that specifier is present, the input
4134 file name will be put into the command line at that spot. If the
4135 specifier is not present, the input should be read from standard
4138 If they are functions, they will be called with two arguments, start
4139 and end of region, and are expected to replace the region contents
4140 with the encoded or decoded results, respectively.")
4142 (defconst tramp-remote-coding-commands
4143 '((b64 "base64" "base64 -d -i")
4144 ;; "-i" is more robust with older base64 from GNU coreutils.
4145 ;; However, I don't know whether all base64 versions do supports
4147 (b64 "base64" "base64 -d")
4148 (b64 "mimencode -b" "mimencode -u -b")
4149 (b64 "mmencode -b" "mmencode -u -b")
4150 (b64 "recode data..base64" "recode base64..data")
4151 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module
)
4152 (b64 tramp-perl-encode tramp-perl-decode
)
4153 (uu "uuencode xxx" "uudecode -o /dev/stdout" "test -c /dev/stdout")
4154 (uu "uuencode xxx" "uudecode -o -")
4155 (uu "uuencode xxx" "uudecode -p")
4156 (uu "uuencode xxx" tramp-uudecode
)
4157 (pack tramp-perl-pack tramp-perl-unpack
))
4158 "List of remote coding commands for inline transfer.
4159 Each item is a list that looks like this:
4161 \(FORMAT ENCODING DECODING [TEST]\)
4163 FORMAT is a symbol describing the encoding/decoding format. It can be
4164 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
4166 ENCODING and DECODING can be strings, giving commands, or symbols,
4167 giving variables. If they are strings, then they can contain
4168 the \"%s\" format specifier. If that specifier is present, the input
4169 file name will be put into the command line at that spot. If the
4170 specifier is not present, the input should be read from standard
4173 If they are variables, this variable is a string containing a
4174 Perl or Shell implementation for this functionality. This
4175 program will be transferred to the remote host, and it is
4176 available as shell function with the same name. A \"%t\" format
4177 specifier in the variable value denotes a temporary file.
4179 The optional TEST command can be used for further tests, whether
4180 ENCODING and DECODING are applicable.")
4182 (defun tramp-find-inline-encoding (vec)
4183 "Find an inline transfer encoding that works.
4184 Goes through the list `tramp-local-coding-commands' and
4185 `tramp-remote-coding-commands'."
4187 (let ((local-commands tramp-local-coding-commands
)
4189 (p (tramp-get-connection-process vec
))
4190 loc-enc loc-dec rem-enc rem-dec rem-test litem ritem found
)
4191 (while (and local-commands
(not found
))
4192 (setq litem
(pop local-commands
))
4193 (catch 'wont-work-local
4194 (let ((format (nth 0 litem
))
4195 (remote-commands tramp-remote-coding-commands
))
4196 (setq loc-enc
(nth 1 litem
))
4197 (setq loc-dec
(nth 2 litem
))
4198 ;; If the local encoder or decoder is a string, the
4199 ;; corresponding command has to work locally.
4200 (if (not (stringp loc-enc
))
4202 vec
5 "Checking local encoding function `%s'" loc-enc
)
4204 vec
5 "Checking local encoding command `%s' for sanity" loc-enc
)
4205 (unless (zerop (tramp-call-local-coding-command
4207 (throw 'wont-work-local nil
)))
4208 (if (not (stringp loc-dec
))
4210 vec
5 "Checking local decoding function `%s'" loc-dec
)
4212 vec
5 "Checking local decoding command `%s' for sanity" loc-dec
)
4213 (unless (zerop (tramp-call-local-coding-command
4215 (throw 'wont-work-local nil
)))
4216 ;; Search for remote coding commands with the same format
4217 (while (and remote-commands
(not found
))
4218 (setq ritem
(pop remote-commands
))
4219 (catch 'wont-work-remote
4220 (when (equal format
(nth 0 ritem
))
4221 (setq rem-enc
(nth 1 ritem
))
4222 (setq rem-dec
(nth 2 ritem
))
4223 (setq rem-test
(nth 3 ritem
))
4224 ;; Check the remote test command if exists.
4225 (when (stringp rem-test
)
4227 vec
5 "Checking remote test command `%s'" rem-test
)
4228 (unless (tramp-send-command-and-check vec rem-test t
)
4229 (throw 'wont-work-remote nil
)))
4230 ;; Check if remote perl exists when necessary.
4231 (when (and (not (stringp rem-enc
))
4232 (not (tramp-get-remote-perl vec
)))
4233 (throw 'wont-work-remote nil
))
4234 ;; Check if remote encoding and decoding commands can be
4235 ;; called remotely with null input and output. This makes
4236 ;; sure there are no syntax errors and the command is really
4237 ;; found. Note that we do not redirect stdout to /dev/null,
4238 ;; for two reasons: when checking the decoding command, we
4239 ;; actually check the output it gives. And also, when
4240 ;; redirecting "mimencode" output to /dev/null, then as root
4241 ;; it might change the permissions of /dev/null!
4242 (when (not (stringp rem-enc
))
4243 (let ((name (symbol-name rem-enc
)))
4244 (while (string-match (regexp-quote "-") name
)
4245 (setq name
(replace-match "_" nil t name
)))
4246 (tramp-maybe-send-script vec
(symbol-value rem-enc
) name
)
4247 (setq rem-enc name
)))
4250 "Checking remote encoding command `%s' for sanity" rem-enc
)
4251 (unless (tramp-send-command-and-check
4252 vec
(format "%s </dev/null" rem-enc
) t
)
4253 (throw 'wont-work-remote nil
))
4255 (when (not (stringp rem-dec
))
4256 (let ((name (symbol-name rem-dec
))
4257 (value (symbol-value rem-dec
))
4259 (while (string-match (regexp-quote "-") name
)
4260 (setq name
(replace-match "_" nil t name
)))
4261 (when (string-match "\\(^\\|[^%]\\)%t" value
)
4265 tramp-temp-name-prefix
4266 (tramp-get-remote-tmpdir vec
)))
4272 (tramp-file-name-handler
4273 'file-remote-p tmpfile
'localname
)))))
4274 (tramp-maybe-send-script vec value name
)
4275 (setq rem-dec name
)))
4278 "Checking remote decoding command `%s' for sanity" rem-dec
)
4279 (unless (tramp-send-command-and-check
4281 (format "echo %s | %s | %s" magic rem-enc rem-dec
)
4283 (throw 'wont-work-remote nil
))
4285 (with-current-buffer (tramp-get-buffer vec
)
4286 (goto-char (point-min))
4287 (unless (looking-at (regexp-quote magic
))
4288 (throw 'wont-work-remote nil
)))
4290 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
4291 (setq rem-enc
(nth 1 ritem
))
4292 (setq rem-dec
(nth 2 ritem
))
4293 (setq found t
)))))))
4296 ;; Set connection properties. Since the commands are risky
4297 ;; (due to output direction), we cache them in the process cache.
4298 (tramp-message vec
5 "Using local encoding `%s'" loc-enc
)
4299 (tramp-set-connection-property p
"local-encoding" loc-enc
)
4300 (tramp-message vec
5 "Using local decoding `%s'" loc-dec
)
4301 (tramp-set-connection-property p
"local-decoding" loc-dec
)
4302 (tramp-message vec
5 "Using remote encoding `%s'" rem-enc
)
4303 (tramp-set-connection-property p
"remote-encoding" rem-enc
)
4304 (tramp-message vec
5 "Using remote decoding `%s'" rem-dec
)
4305 (tramp-set-connection-property p
"remote-decoding" rem-dec
)))))
4307 (defun tramp-call-local-coding-command (cmd input output
)
4308 "Call the local encoding or decoding command.
4309 If CMD contains \"%s\", provide input file INPUT there in command.
4310 Otherwise, INPUT is passed via standard input.
4311 INPUT can also be nil which means `/dev/null'.
4312 OUTPUT can be a string (which specifies a file name), or t (which
4313 means standard output and thus the current buffer), or nil (which
4316 nil tramp-encoding-shell
4317 (when (and input
(not (string-match "%s" cmd
))) input
)
4318 (if (eq output t
) t nil
)
4320 tramp-encoding-command-switch
4322 (if (string-match "%s" cmd
) (format cmd input
) cmd
)
4323 (if (stringp output
) (concat " >" output
) ""))))
4325 (defconst tramp-inline-compress-commands
4326 '(("gzip" "gzip -d")
4327 ("bzip2" "bzip2 -d")
4329 ("compress" "compress -d"))
4330 "List of compress and decompress commands for inline transfer.
4331 Each item is a list that looks like this:
4333 \(COMPRESS DECOMPRESS\)
4335 COMPRESS or DECOMPRESS are strings with the respective commands.")
4337 (defun tramp-find-inline-compress (vec)
4338 "Find an inline transfer compress command that works.
4339 Goes through the list `tramp-inline-compress-commands'."
4341 (let ((commands tramp-inline-compress-commands
)
4343 (p (tramp-get-connection-process vec
))
4344 item compress decompress found
)
4345 (while (and commands
(not found
))
4347 (setq item
(pop commands
)
4348 compress
(nth 0 item
)
4349 decompress
(nth 1 item
))
4352 "Checking local compress commands `%s', `%s' for sanity"
4353 compress decompress
)
4356 (tramp-call-local-coding-command
4358 ;; Windows shells need the program file name after
4359 ;; the pipe symbol be quoted if they use forward
4360 ;; slashes as directory separators.
4361 (if (memq system-type
'(windows-nt))
4362 "echo %s | \"%s\" | \"%s\""
4363 "echo %s | %s | %s")
4364 magic compress decompress
) nil nil
))
4368 "Checking remote compress commands `%s', `%s' for sanity"
4369 compress decompress
)
4370 (unless (tramp-send-command-and-check
4371 vec
(format "echo %s | %s | %s" magic compress decompress
) t
)
4375 ;; Did we find something?
4378 ;; Set connection properties. Since the commands are
4379 ;; risky (due to output direction), we cache them in the
4382 vec
5 "Using inline transfer compress command `%s'" compress
)
4383 (tramp-set-connection-property p
"inline-compress" compress
)
4385 vec
5 "Using inline transfer decompress command `%s'" decompress
)
4386 (tramp-set-connection-property p
"inline-decompress" decompress
))
4388 (tramp-set-connection-property p
"inline-compress" nil
)
4389 (tramp-set-connection-property p
"inline-decompress" nil
)
4391 vec
2 "Couldn't find an inline transfer compress command")))))
4393 (defun tramp-compute-multi-hops (vec)
4394 "Expands VEC according to `tramp-default-proxies-alist'.
4395 Gateway hops are already opened."
4396 (let ((target-alist `(,vec
))
4397 (hops (or (tramp-file-name-hop vec
) ""))
4401 ;; Ad-hoc proxy definitions.
4402 (dolist (proxy (reverse (split-string hops tramp-postfix-hop-regexp
'omit
)))
4403 (let ((user (tramp-file-name-user item
))
4404 (host (tramp-file-name-host item
))
4406 tramp-prefix-format proxy tramp-postfix-host-format
)))
4408 vec
5 "Add proxy (\"%s\" \"%s\" \"%s\")"
4409 (and (stringp host
) (regexp-quote host
))
4410 (and (stringp user
) (regexp-quote user
))
4414 'tramp-default-proxies-alist
4415 (list (and (stringp host
) (regexp-quote host
))
4416 (and (stringp user
) (regexp-quote user
))
4418 (setq item
(tramp-dissect-file-name proxy
))))
4419 ;; Save the new value.
4420 (when (and hops tramp-save-ad-hoc-proxies
)
4421 (customize-save-variable
4422 'tramp-default-proxies-alist tramp-default-proxies-alist
))
4424 ;; Look for proxy hosts to be passed.
4425 (setq choices tramp-default-proxies-alist
)
4427 (setq item
(pop choices
)
4428 proxy
(eval (nth 2 item
)))
4431 (string-match (or (eval (nth 0 item
)) "")
4432 (or (tramp-file-name-host (car target-alist
)) ""))
4434 (string-match (or (eval (nth 1 item
)) "")
4435 (or (tramp-file-name-user (car target-alist
)) "")))
4437 ;; No more hops needed.
4439 ;; Replace placeholders.
4444 ?u
(or (tramp-file-name-user (car target-alist
)) "")
4445 ?h
(or (tramp-file-name-host (car target-alist
)) ""))))
4446 (with-parsed-tramp-file-name proxy l
4448 (push l target-alist
)
4449 ;; Start next search.
4450 (setq choices tramp-default-proxies-alist
)))))
4453 (when (and (boundp 'tramp-gw-tunnel-method
) (boundp 'tramp-gw-socks-method
)
4456 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method
)
4457 (tramp-file-name-method (car target-alist
))))
4458 (let ((gw (pop target-alist
))
4459 (hop (pop target-alist
)))
4460 ;; Is the method prepared for gateways?
4461 (unless (tramp-file-name-port hop
)
4464 "Connection `%s' is not supported for gateway access." hop
))
4465 ;; Open the gateway connection.
4468 (tramp-file-name-method hop
) (tramp-file-name-user hop
)
4469 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop
) nil nil
)
4471 ;; For the password prompt, we need the correct values.
4472 ;; Therefore, we must remember the gateway vector. But we
4473 ;; cannot do it as connection property, because it shouldn't
4474 ;; be persistent. And we have no started process yet either.
4475 (tramp-set-file-property (car target-alist
) "" "gateway" hop
)))
4477 ;; Foreign and out-of-band methods are not supported for multi-hops.
4478 (when (cdr target-alist
)
4479 (setq choices target-alist
)
4481 (setq item
(pop choices
))
4485 (tramp-get-method-parameter
4486 (tramp-file-name-method item
) 'tramp-login-program
))
4487 (tramp-get-method-parameter
4488 (tramp-file-name-method item
) 'tramp-copy-program
))
4491 "Method `%s' is not supported for multi-hops."
4492 (tramp-file-name-method item
)))))
4494 ;; In case the host name is not used for the remote shell
4495 ;; command, the user could be misguided by applying a random
4497 (let* ((v (car target-alist
))
4498 (method (tramp-file-name-method v
))
4499 (host (tramp-file-name-host v
)))
4502 ;; There are multi-hops.
4504 ;; The host name is used for the remote shell command.
4506 '("%h") (tramp-get-method-parameter method
'tramp-login-args
))
4507 ;; The host is local. We cannot use `tramp-local-host-p'
4508 ;; here, because it opens a connection as well.
4509 (string-match tramp-local-host-regexp host
))
4512 "Host `%s' looks like a remote host, `%s' can only use the local host"
4518 (defun tramp-maybe-open-connection (vec)
4519 "Maybe open a connection VEC.
4520 Does not do anything if a connection is already open, but re-opens the
4521 connection if a previous connection has died for some reason."
4522 (tramp-check-proper-method-and-host vec
)
4524 (let ((p (tramp-get-connection-process vec
))
4525 (process-name (tramp-get-connection-property vec
"process-name" nil
))
4526 (process-environment (copy-sequence process-environment
))
4527 (pos (with-current-buffer (tramp-get-connection-buffer vec
) (point))))
4529 ;; If Tramp opens the same connection within a short time frame,
4530 ;; there is a problem. We shall signal this.
4531 (unless (or (and p
(processp p
) (memq (process-status p
) '(run open
)))
4532 (not (equal (butlast (append vec nil
) 2)
4533 (car tramp-current-connection
)))
4535 (current-time) (cdr tramp-current-connection
))
4536 (or tramp-connection-min-time-diff
0)))
4537 (throw 'suppress
'suppress
))
4539 ;; If too much time has passed since last command was sent, look
4540 ;; whether process is still alive. If it isn't, kill it. When
4541 ;; using ssh, it can sometimes happen that the remote end has hung
4542 ;; up but the local ssh client doesn't recognize this until it
4543 ;; tries to send some data to the remote end. So that's why we
4544 ;; try to send a command from time to time, then look again
4545 ;; whether the process is really alive.
4547 (when (and (> (tramp-time-diff
4549 (tramp-get-connection-property
4550 p
"last-cmd-time" '(0 0 0)))
4552 p
(processp p
) (memq (process-status p
) '(run open
)))
4553 (tramp-send-command vec
"echo are you awake" t t
)
4554 (unless (and (memq (process-status p
) '(run open
))
4555 (tramp-wait-for-output p
10))
4556 ;; The error will be caught locally.
4557 (tramp-error vec
'file-error
"Awake did fail")))
4559 (tramp-cleanup-connection vec t
)
4562 ;; New connection must be opened.
4564 (unless (and p
(processp p
) (memq (process-status p
) '(run open
)))
4566 ;; If `non-essential' is non-nil, don't reopen a new connection.
4567 (when (and (boundp 'non-essential
) (symbol-value 'non-essential
))
4568 (throw 'non-essential
'non-essential
))
4570 (with-tramp-progress-reporter
4572 (if (zerop (length (tramp-file-name-user vec
)))
4573 (format "Opening connection for %s using %s"
4574 (tramp-file-name-host vec
)
4575 (tramp-file-name-method vec
))
4576 (format "Opening connection for %s@%s using %s"
4577 (tramp-file-name-user vec
)
4578 (tramp-file-name-host vec
)
4579 (tramp-file-name-method vec
)))
4581 (catch 'uname-changed
4582 ;; Start new process.
4583 (when (and p
(processp p
))
4585 (setenv "TERM" tramp-terminal-type
)
4586 (setenv "LC_ALL" "en_US.utf8")
4587 (setenv "HISTFILE" "/dev/null")
4588 (setenv "PROMPT_COMMAND")
4589 (setenv "PS1" tramp-initial-end-of-output
)
4590 (let* ((target-alist (tramp-compute-multi-hops vec
))
4591 ;; We will apply `tramp-ssh-controlmaster-options'
4592 ;; only for the first hop.
4593 (options (if tramp-use-ssh-controlmaster-options
4594 tramp-ssh-controlmaster-options
""))
4595 (process-connection-type tramp-process-connection-type
)
4596 (process-adaptive-read-buffering nil
)
4597 (coding-system-for-read nil
)
4598 ;; This must be done in order to avoid our file
4600 (p (let ((default-directory
4601 (tramp-compat-temporary-file-directory)))
4604 (tramp-get-connection-name vec
)
4605 (tramp-get-connection-buffer vec
)
4606 (if tramp-encoding-command-interactive
4607 (list tramp-encoding-shell
4608 tramp-encoding-command-interactive
)
4609 (list tramp-encoding-shell
))))))
4611 ;; Set sentinel and query flag.
4612 (tramp-set-connection-property p
"vector" vec
)
4613 (set-process-sentinel p
'tramp-process-sentinel
)
4614 (tramp-compat-set-process-query-on-exit-flag p nil
)
4615 (setq tramp-current-connection
4616 (cons (butlast (append vec nil
) 2) (current-time))
4617 tramp-current-host
(system-name))
4620 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
4622 ;; Check whether process is alive.
4623 (tramp-barf-if-no-shell-prompt
4625 "Couldn't find local shell prompt for %s" tramp-encoding-shell
)
4627 ;; Now do all the connections as specified.
4629 (let* ((hop (car target-alist
))
4630 (l-method (tramp-file-name-method hop
))
4631 (l-user (tramp-file-name-user hop
))
4632 (l-host (tramp-file-name-host hop
))
4635 (tramp-get-method-parameter
4636 l-method
'tramp-login-program
))
4638 (tramp-get-method-parameter
4639 l-method
'tramp-login-args
))
4641 (tramp-get-method-parameter
4642 l-method
'tramp-login-env
))
4644 (tramp-get-method-parameter
4645 l-method
'tramp-async-args
))
4647 (tramp-get-method-parameter
4648 l-method
'tramp-connection-timeout
))
4650 (tramp-get-method-parameter l-method
'tramp-gw-args
))
4651 (gw (tramp-get-file-property hop
"" "gateway" nil
))
4652 (g-method (and gw
(tramp-file-name-method gw
)))
4653 (g-user (and gw
(tramp-file-name-user gw
)))
4654 (g-host (and gw
(tramp-file-name-real-host gw
)))
4655 (command login-program
)
4656 ;; We don't create the temporary file. In
4657 ;; fact, it is just a prefix for the
4658 ;; ControlPath option of ssh; the real
4659 ;; temporary file has another name, and it is
4660 ;; created and protected by ssh. It is also
4661 ;; removed by ssh when the connection is
4662 ;; closed. The temporary file name is cached
4663 ;; in the main connection process, therefore
4664 ;; we cannot use `tramp-get-connection-process'.
4666 (with-tramp-connection-property
4667 (get-process (tramp-buffer-name vec
)) "temp-file"
4670 tramp-temp-name-prefix
4671 (tramp-compat-temporary-file-directory)))))
4674 ;; Add arguments for asynchronous processes.
4675 (when (and process-name async-args
)
4676 (setq login-args
(append async-args login-args
)))
4678 ;; Add gateway arguments if necessary.
4679 (when (and gw gw-args
)
4680 (setq login-args
(append gw-args login-args
)))
4682 ;; Check for port number. Until now, there's no
4683 ;; need for handling like method, user, host.
4684 (when (string-match tramp-host-with-port-regexp l-host
)
4685 (setq l-port
(match-string 2 l-host
)
4686 l-host
(match-string 1 l-host
)))
4688 ;; Check, whether there is a restricted shell.
4689 (dolist (elt tramp-restricted-shell-hosts-alist
)
4690 (when (string-match elt tramp-current-host
)
4693 ;; Set variables for computing the prompt for
4694 ;; reading password. They can also be derived
4696 (setq tramp-current-method
(or g-method l-method
)
4697 tramp-current-user
(or g-user l-user
)
4698 tramp-current-host
(or g-host l-host
))
4700 ;; Add login environment.
4706 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
4707 (unless (member "" x
) (mapconcat 'identity x
" ")))
4714 (tramp-shell-quote-argument (pop login-env
))
4716 (setq command
(concat "env " command
)))
4718 ;; Replace `login-args' place holders.
4720 l-host
(or l-host
"")
4721 l-user
(or l-user
"")
4722 l-port
(or l-port
"")
4723 spec
(format-spec-make ?t tmpfile
)
4724 options
(format-spec options spec
)
4725 spec
(format-spec-make
4726 ?h l-host ?u l-user ?p l-port ?c options
)
4729 ;; We do not want to see the trailing local
4730 ;; prompt in `start-file-process'.
4731 (unless r-shell
"exec ")
4735 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
4736 (unless (member "" x
) (mapconcat 'identity x
" ")))
4738 ;; Local shell could be a Windows COMSPEC. It
4739 ;; doesn't know the ";" syntax, but we must exit
4740 ;; always for `start-file-process'. It could
4741 ;; also be a restricted shell, which does not
4743 (when r-shell
" && exit || exit")))
4745 ;; Send the command.
4746 (tramp-message vec
3 "Sending command `%s'" command
)
4747 (tramp-send-command vec command t t
)
4748 (tramp-process-actions
4749 p vec pos tramp-actions-before-shell
4750 (or connection-timeout tramp-connection-timeout
))
4752 vec
3 "Found remote shell prompt on `%s'" l-host
))
4755 target-alist
(cdr target-alist
)))
4757 ;; Make initial shell settings.
4758 (tramp-open-connection-setup-interactive-shell p vec
)))))
4760 ;; When the user did interrupt, we must cleanup.
4762 (tramp-cleanup-connection vec t
)
4763 ;; Propagate the quit signal.
4764 (signal (car err
) (cdr err
))))))
4766 (defun tramp-send-command (vec command
&optional neveropen nooutput
)
4767 "Send the COMMAND to connection VEC.
4768 Erases temporary buffer before sending the command. If optional
4769 arg NEVEROPEN is non-nil, never try to open the connection. This
4770 is meant to be used from `tramp-maybe-open-connection' only. The
4771 function waits for output unless NOOUTPUT is set."
4772 (unless neveropen
(tramp-maybe-open-connection vec
))
4773 (let ((p (tramp-get-connection-process vec
)))
4774 (when (tramp-get-connection-property p
"remote-echo" nil
)
4775 ;; We mark the command string that it can be erased in the output buffer.
4776 (tramp-set-connection-property p
"check-remote-echo" t
)
4777 ;; If we put `tramp-echo-mark' after a trailing newline (which
4778 ;; is assumed to be unquoted) `tramp-send-string' doesn't see
4779 ;; that newline and adds `tramp-rsh-end-of-line' right after
4780 ;; `tramp-echo-mark', so the remote shell sees two consecutive
4781 ;; trailing line endings and sends two prompts after executing
4782 ;; the command, which confuses `tramp-wait-for-output'.
4783 (when (and (not (string= command
""))
4784 (string-equal (substring command -
1) "\n"))
4785 (setq command
(substring command
0 -
1)))
4786 ;; No need to restore a trailing newline here since `tramp-send-string'
4787 ;; makes sure that the string ends in `tramp-rsh-end-of-line', anyway.
4788 (setq command
(format "%s%s%s" tramp-echo-mark command tramp-echo-mark
)))
4789 ;; Send the command.
4790 (tramp-message vec
6 "%s" command
)
4791 (tramp-send-string vec command
)
4792 (unless nooutput
(tramp-wait-for-output p
))))
4794 (defun tramp-wait-for-output (proc &optional timeout
)
4795 "Wait for output from remote command."
4796 (unless (buffer-live-p (process-buffer proc
))
4797 (delete-process proc
)
4798 (tramp-error proc
'file-error
"Process `%s' not available, try again" proc
))
4799 (with-current-buffer (process-buffer proc
)
4800 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
4801 ;; be leading escape sequences, which must be ignored.
4802 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output
)))
4803 ;; Sometimes, the commands do not return a newline but a
4804 ;; null byte before the shell prompt, for example "git
4805 ;; ls-files -c -z ...".
4806 (regexp1 (format "\\(^\\|\000\\)%s" regexp
))
4807 (found (tramp-wait-for-regexp proc timeout regexp1
)))
4809 (let (buffer-read-only)
4810 ;; A simple-minded busybox has sent " ^H" sequences.
4812 (goto-char (point-min))
4813 (when (re-search-forward "^\\(.\b\\)+$" (point-at-eol) t
)
4815 (delete-region (point-min) (point)))
4816 ;; Delete the prompt.
4817 (goto-char (point-max))
4818 (re-search-backward regexp nil t
)
4819 (delete-region (point) (point-max)))
4823 "[[Remote prompt `%s' not found in %d secs]]"
4824 tramp-end-of-output timeout
)
4827 "[[Remote prompt `%s' not found]]" tramp-end-of-output
)))
4828 ;; Return value is whether end-of-output sentinel was found.
4831 (defun tramp-send-command-and-check
4832 (vec command
&optional subshell dont-suppress-err
)
4833 "Run COMMAND and check its exit status.
4834 Sends `echo $?' along with the COMMAND for checking the exit status.
4835 If COMMAND is nil, just sends `echo $?'. Returns `t' if the exit
4836 status is 0, and `nil' otherwise.
4838 If the optional argument SUBSHELL is non-nil, the command is
4839 executed in a subshell, ie surrounded by parentheses. If
4840 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
4843 (concat (if subshell
"( " "")
4845 (if command
(if dont-suppress-err
"; " " 2>/dev/null; ") "")
4846 "echo tramp_exit_status $?"
4847 (if subshell
" )" "")))
4848 (with-current-buffer (tramp-get-connection-buffer vec
)
4849 (goto-char (point-max))
4850 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
4852 vec
'file-error
"Couldn't find exit status of `%s'" command
))
4853 (skip-chars-forward "^ ")
4855 (zerop (read (current-buffer)))
4856 (let (buffer-read-only)
4857 (delete-region (match-beginning 0) (point-max))))))
4859 (defun tramp-barf-unless-okay (vec command fmt
&rest args
)
4860 "Run COMMAND, check exit status, throw error if exit status not okay.
4861 Similar to `tramp-send-command-and-check' but accepts two more arguments
4862 FMT and ARGS which are passed to `error'."
4863 (or (tramp-send-command-and-check vec command
)
4864 (apply 'tramp-error vec
'file-error fmt args
)))
4866 (defun tramp-send-command-and-read (vec command
&optional noerror
)
4867 "Run COMMAND and return the output, which must be a Lisp expression.
4868 In case there is no valid Lisp expression and NOERROR is nil, it
4871 (tramp-send-command-and-check vec command
)
4872 (tramp-barf-unless-okay
4873 vec command
"`%s' returns with error" command
))
4874 (with-current-buffer (tramp-get-connection-buffer vec
)
4875 ;; Read the expression.
4876 (goto-char (point-min))
4878 (prog1 (read (current-buffer))
4880 (when (re-search-forward "\\S-" (point-at-eol) t
)
4882 (error (unless noerror
4885 "`%s' does not return a valid Lisp expression: `%s'"
4886 command
(buffer-string))))))))
4888 (defun tramp-convert-file-attributes (vec attr
)
4889 "Convert `file-attributes' ATTR generated by perl script, stat or ls.
4890 Convert file mode bits to string and set virtual device number.
4893 ;; Remove color escape sequences from symlink.
4894 (when (stringp (car attr
))
4895 (while (string-match tramp-color-escape-sequence-regexp
(car attr
))
4896 (setcar attr
(replace-match "" nil nil
(car attr
)))))
4897 ;; Convert uid and gid. Use -1 as indication of unusable value.
4898 (when (and (numberp (nth 2 attr
)) (< (nth 2 attr
) 0))
4899 (setcar (nthcdr 2 attr
) -
1))
4900 (when (and (floatp (nth 2 attr
))
4901 (<= (nth 2 attr
) (tramp-compat-most-positive-fixnum)))
4902 (setcar (nthcdr 2 attr
) (round (nth 2 attr
))))
4903 (when (and (numberp (nth 3 attr
)) (< (nth 3 attr
) 0))
4904 (setcar (nthcdr 3 attr
) -
1))
4905 (when (and (floatp (nth 3 attr
))
4906 (<= (nth 3 attr
) (tramp-compat-most-positive-fixnum)))
4907 (setcar (nthcdr 3 attr
) (round (nth 3 attr
))))
4908 ;; Convert last access time.
4909 (unless (listp (nth 4 attr
))
4910 (setcar (nthcdr 4 attr
)
4911 (list (floor (nth 4 attr
) 65536)
4912 (floor (mod (nth 4 attr
) 65536)))))
4913 ;; Convert last modification time.
4914 (unless (listp (nth 5 attr
))
4915 (setcar (nthcdr 5 attr
)
4916 (list (floor (nth 5 attr
) 65536)
4917 (floor (mod (nth 5 attr
) 65536)))))
4918 ;; Convert last status change time.
4919 (unless (listp (nth 6 attr
))
4920 (setcar (nthcdr 6 attr
)
4921 (list (floor (nth 6 attr
) 65536)
4922 (floor (mod (nth 6 attr
) 65536)))))
4923 ;; Convert file size.
4924 (when (< (nth 7 attr
) 0)
4925 (setcar (nthcdr 7 attr
) -
1))
4926 (when (and (floatp (nth 7 attr
))
4927 (<= (nth 7 attr
) (tramp-compat-most-positive-fixnum)))
4928 (setcar (nthcdr 7 attr
) (round (nth 7 attr
))))
4929 ;; Convert file mode bits to string.
4930 (unless (stringp (nth 8 attr
))
4931 (setcar (nthcdr 8 attr
) (tramp-file-mode-from-int (nth 8 attr
)))
4932 (when (stringp (car attr
))
4933 (aset (nth 8 attr
) 0 ?l
)))
4934 ;; Convert directory indication bit.
4935 (when (string-match "^d" (nth 8 attr
))
4937 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
4938 (when (consp (car attr
))
4939 (if (and (stringp (caar attr
))
4940 (string-match ".+ -> .\\(.+\\)." (caar attr
)))
4941 (setcar attr
(match-string 1 (caar attr
)))
4943 ;; Set file's gid change bit.
4944 (setcar (nthcdr 9 attr
)
4945 (if (numberp (nth 3 attr
))
4946 (not (= (nth 3 attr
)
4947 (tramp-get-remote-gid vec
'integer
)))
4950 (tramp-get-remote-gid vec
'string
)))))
4952 (unless (listp (nth 10 attr
))
4953 (setcar (nthcdr 10 attr
)
4955 (cons (floor (nth 10 attr
) 65536)
4956 (floor (mod (nth 10 attr
) 65536)))
4957 ;; Inodes can be incredible huge. We must hide this.
4958 (error (tramp-get-inode vec
)))))
4959 ;; Set virtual device number.
4960 (setcar (nthcdr 11 attr
)
4961 (tramp-get-device vec
))
4964 (defun tramp-shell-case-fold (string)
4965 "Converts STRING to shell glob pattern which ignores case."
4968 (if (equal (downcase c
) (upcase c
))
4970 (format "[%c%c]" (downcase c
) (upcase c
))))
4974 (defun tramp-make-copy-program-file-name (vec)
4975 "Create a file name suitable to be passed to `scp' or `nc' and workalikes."
4976 (let ((method (tramp-file-name-method vec
))
4977 (user (tramp-file-name-user vec
))
4978 (host (tramp-file-name-real-host vec
))
4979 (localname (tramp-shell-quote-argument
4980 (tramp-file-name-localname vec
))))
4982 ((tramp-get-method-parameter method
'tramp-remote-copy-program
)
4984 ((not (zerop (length user
)))
4985 (shell-quote-argument (format "%s@%s:%s" user host localname
)))
4986 (t (shell-quote-argument (format "%s:%s" host localname
))))))
4988 (defun tramp-method-out-of-band-p (vec size
)
4989 "Return t if this is an out-of-band method, nil otherwise."
4991 ;; It shall be an out-of-band method.
4992 (tramp-get-method-parameter (tramp-file-name-method vec
) 'tramp-copy-program
)
4993 ;; There must be a size, otherwise the file doesn't exist.
4995 ;; Either the file size is large enough, or (in rare cases) there
4996 ;; does not exist a remote encoding.
4997 (or (null tramp-copy-size-limit
)
4998 (> size tramp-copy-size-limit
)
4999 (null (tramp-get-inline-coding vec
"remote-encoding" size
)))))
5001 ;; Variables local to connection.
5003 (defun tramp-get-remote-path (vec)
5004 (with-tramp-connection-property
5005 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
5006 ;; cache the result for the session only. Otherwise, the result
5007 ;; is cached persistently.
5008 (if (memq 'tramp-own-remote-path tramp-remote-path
)
5009 (tramp-get-connection-process vec
)
5012 (let* ((remote-path (copy-tree tramp-remote-path
))
5013 (elt1 (memq 'tramp-default-remote-path remote-path
))
5014 (elt2 (memq 'tramp-own-remote-path remote-path
))
5015 (default-remote-path
5018 (tramp-send-command-and-read
5019 vec
"echo \\\"`getconf PATH 2>/dev/null`\\\"" 'noerror
)
5020 ;; Default if "getconf" is not available.
5024 "`getconf PATH' not successful, using default value \"%s\"."
5030 (tramp-send-command-and-read vec
"echo \\\"$PATH\\\"")
5033 vec
3 "$PATH not set, ignoring `tramp-own-remote-path'.")
5036 ;; Replace place holder `tramp-default-remote-path'.
5040 (tramp-compat-split-string (or default-remote-path
"") ":")
5042 (setq remote-path
(delq 'tramp-default-remote-path remote-path
)))
5044 ;; Replace place holder `tramp-own-remote-path'.
5048 (tramp-compat-split-string (or own-remote-path
"") ":")
5050 (setq remote-path
(delq 'tramp-own-remote-path remote-path
)))
5052 ;; Remove double entries.
5053 (setq elt1 remote-path
)
5055 (while (and (car elt1
) (setq elt2
(member (car elt1
) (cdr elt1
))))
5057 (setq elt1
(cdr elt1
)))
5059 ;; Remove non-existing directories.
5067 (tramp-make-tramp-file-name
5068 (tramp-file-name-method vec
)
5069 (tramp-file-name-user vec
)
5070 (tramp-file-name-host vec
)
5075 (defun tramp-get-remote-locale (vec)
5076 (with-tramp-connection-property vec
"locale"
5077 (tramp-send-command vec
"locale -a")
5078 (let ((candidates '("en_US.utf8" "C.utf8" "C"))
5080 (with-current-buffer (tramp-get-connection-buffer vec
)
5082 (goto-char (point-min))
5083 (if (string-match (concat "^" (car candidates
) "$") (buffer-string))
5084 (setq locale
(car candidates
)
5086 (setq candidates
(cdr candidates
)))))
5088 (when locale
(format "LC_ALL=%s" locale
)))))
5090 (defun tramp-get-ls-command (vec)
5091 (with-tramp-connection-property vec
"ls"
5092 (tramp-message vec
5 "Finding a suitable `ls' command")
5095 (dolist (cmd '("ls" "gnuls" "gls"))
5096 (let ((dl (tramp-get-remote-path vec
))
5098 (while (and dl
(setq result
(tramp-find-executable vec cmd dl t t
)))
5099 ;; Check parameters. On busybox, "ls" output coloring is
5100 ;; enabled by default sometimes. So we try to disable it
5101 ;; when possible. $LS_COLORING is not supported there.
5102 ;; Some "ls" versions are sensible wrt the order of
5103 ;; arguments, they fail when "-al" is after the
5104 ;; "--color=never" argument (for example on FreeBSD).
5105 (when (tramp-send-command-and-check
5106 vec
(format "%s -lnd /" result
))
5107 (when (tramp-send-command-and-check
5109 "%s --color=never -al /dev/null" result
))
5110 (setq result
(concat result
" --color=never")))
5111 (throw 'ls-found result
))
5112 (setq dl
(cdr dl
))))))
5113 (tramp-error vec
'file-error
"Couldn't find a proper `ls' command"))))
5115 (defun tramp-get-ls-command-with-dired (vec)
5117 (with-tramp-connection-property vec
"ls-dired"
5118 (tramp-message vec
5 "Checking, whether `ls --dired' works")
5119 ;; Some "ls" versions are sensible wrt the order of arguments,
5120 ;; they fail when "-al" is after the "--dired" argument (for
5121 ;; example on FreeBSD).
5122 (tramp-send-command-and-check
5123 vec
(format "%s --dired -al /dev/null" (tramp-get-ls-command vec
))))))
5125 (defun tramp-get-test-command (vec)
5126 (with-tramp-connection-property vec
"test"
5127 (tramp-message vec
5 "Finding a suitable `test' command")
5128 (if (tramp-send-command-and-check vec
"test 0")
5130 (tramp-find-executable vec
"test" (tramp-get-remote-path vec
)))))
5132 (defun tramp-get-test-nt-command (vec)
5133 ;; Does `test A -nt B' work? Use abominable `find' construct if it
5134 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
5135 ;; for otherwise the shell crashes.
5136 (with-tramp-connection-property vec
"test-nt"
5140 vec
(format "( %s / -nt / )" (tramp-get-test-command vec
)))
5141 (with-current-buffer (tramp-get-buffer vec
)
5142 (goto-char (point-min))
5143 (when (looking-at (regexp-quote tramp-end-of-output
))
5144 (format "%s %%s -nt %%s" (tramp-get-test-command vec
)))))
5149 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
5150 (tramp-get-test-command vec
)))
5151 "tramp_test_nt %s %s"))))
5153 (defun tramp-get-file-exists-command (vec)
5154 (with-tramp-connection-property vec
"file-exists"
5155 (tramp-message vec
5 "Finding command to check if file exists")
5156 (tramp-find-file-exists-command vec
)))
5158 (defun tramp-get-remote-ln (vec)
5159 (with-tramp-connection-property vec
"ln"
5160 (tramp-message vec
5 "Finding a suitable `ln' command")
5161 (tramp-find-executable vec
"ln" (tramp-get-remote-path vec
))))
5163 (defun tramp-get-remote-perl (vec)
5164 (with-tramp-connection-property vec
"perl"
5165 (tramp-message vec
5 "Finding a suitable `perl' command")
5167 (or (tramp-find-executable vec
"perl5" (tramp-get-remote-path vec
))
5168 (tramp-find-executable
5169 vec
"perl" (tramp-get-remote-path vec
)))))
5170 ;; We must check also for some Perl modules.
5172 (with-tramp-connection-property vec
"perl-file-spec"
5173 (tramp-send-command-and-check
5174 vec
(format "%s -e 'use File::Spec;'" result
)))
5175 (with-tramp-connection-property vec
"perl-cwd-realpath"
5176 (tramp-send-command-and-check
5177 vec
(format "%s -e 'use Cwd \"realpath\";'" result
))))
5180 (defun tramp-get-remote-stat (vec)
5181 (with-tramp-connection-property vec
"stat"
5182 (tramp-message vec
5 "Finding a suitable `stat' command")
5183 (let ((result (tramp-find-executable
5184 vec
"stat" (tramp-get-remote-path vec
)))
5186 ;; Check whether stat(1) returns usable syntax. "%s" does not
5187 ;; work on older AIX systems.
5190 (tramp-send-command-and-read
5191 vec
(format "%s -c '(\"%%N\" %%s)' /" result
) 'noerror
))
5192 (unless (and (listp tmp
) (stringp (car tmp
))
5193 (string-match "^./.$" (car tmp
))
5194 (integerp (cadr tmp
)))
5198 (defun tramp-get-remote-readlink (vec)
5199 (with-tramp-connection-property vec
"readlink"
5200 (tramp-message vec
5 "Finding a suitable `readlink' command")
5201 (let ((result (tramp-find-executable
5202 vec
"readlink" (tramp-get-remote-path vec
))))
5204 (tramp-send-command-and-check
5205 vec
(format "%s --canonicalize-missing /" result
)))
5208 (defun tramp-get-remote-trash (vec)
5209 (with-tramp-connection-property vec
"trash"
5210 (tramp-message vec
5 "Finding a suitable `trash' command")
5211 (tramp-find-executable vec
"trash" (tramp-get-remote-path vec
))))
5213 (defun tramp-get-remote-touch (vec)
5214 (with-tramp-connection-property vec
"touch"
5215 (tramp-message vec
5 "Finding a suitable `touch' command")
5216 (let ((result (tramp-find-executable
5217 vec
"touch" (tramp-get-remote-path vec
)))
5221 tramp-temp-name-prefix
(tramp-get-remote-tmpdir vec
)))))
5222 ;; Busyboxes do support the "-t" option only when they have been
5223 ;; built with the DESKTOP config option. Let's check it.
5225 (tramp-set-connection-property
5227 (tramp-send-command-and-check
5232 (format-time-string "%Y%m%d%H%M.%S")
5233 (tramp-file-name-handler 'file-remote-p tmpfile
'localname
))))
5234 (delete-file tmpfile
))
5237 (defun tramp-get-remote-gvfs-monitor-dir (vec)
5238 (with-tramp-connection-property vec
"gvfs-monitor-dir"
5239 (tramp-message vec
5 "Finding a suitable `gvfs-monitor-dir' command")
5240 (tramp-find-executable
5241 vec
"gvfs-monitor-dir" (tramp-get-remote-path vec
) t t
)))
5243 (defun tramp-get-remote-inotifywait (vec)
5244 (with-tramp-connection-property vec
"inotifywait"
5245 (tramp-message vec
5 "Finding a suitable `inotifywait' command")
5246 (tramp-find-executable vec
"inotifywait" (tramp-get-remote-path vec
) t t
)))
5248 (defun tramp-get-remote-id (vec)
5249 (with-tramp-connection-property vec
"id"
5250 (tramp-message vec
5 "Finding POSIX `id' command")
5252 (dolist (cmd '("id" "gid"))
5253 (let ((dl (tramp-get-remote-path vec
))
5255 (while (and dl
(setq result
(tramp-find-executable vec cmd dl t t
)))
5256 ;; Check POSIX parameter.
5257 (when (tramp-send-command-and-check vec
(format "%s -u" result
))
5258 (throw 'id-found result
))
5259 (setq dl
(cdr dl
))))))))
5261 (defun tramp-get-remote-uid-with-id (vec id-format
)
5262 (tramp-send-command-and-read
5264 (format "%s -u%s %s"
5265 (tramp-get-remote-id vec
)
5266 (if (equal id-format
'integer
) "" "n")
5267 (if (equal id-format
'integer
)
5268 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/"))))
5270 (defun tramp-get-remote-uid-with-perl (vec id-format
)
5271 (tramp-send-command-and-read
5273 (format "%s -le '%s'"
5274 (tramp-get-remote-perl vec
)
5275 (if (equal id-format
'integer
)
5277 "print \"\\\"\", scalar getpwuid($>), \"\\\"\""))))
5279 (defun tramp-get-remote-python (vec)
5280 (with-tramp-connection-property vec
"python"
5281 (tramp-message vec
5 "Finding a suitable `python' command")
5282 (or (tramp-find-executable vec
"python" (tramp-get-remote-path vec
))
5283 (tramp-find-executable vec
"python2" (tramp-get-remote-path vec
))
5284 (tramp-find-executable vec
"python3" (tramp-get-remote-path vec
)))))
5286 (defun tramp-get-remote-uid-with-python (vec id-format
)
5287 (tramp-send-command-and-read
5289 (format "%s -c \"%s\""
5290 (tramp-get-remote-python vec
)
5291 (if (equal id-format
'integer
)
5292 "import os; print (os.getuid())"
5293 "import os, pwd; print ('\\\"' + pwd.getpwuid(os.getuid())[0] + '\\\"')"))))
5295 (defun tramp-get-remote-uid (vec id-format
)
5296 (with-tramp-connection-property vec
(format "uid-%s" id-format
)
5300 ((tramp-get-remote-id vec
)
5301 (tramp-get-remote-uid-with-id vec id-format
))
5302 ((tramp-get-remote-perl vec
)
5303 (tramp-get-remote-uid-with-perl vec id-format
))
5304 ((tramp-get-remote-python vec
)
5305 (tramp-get-remote-uid-with-python vec id-format
))))))
5306 ;; Ensure there is a valid result.
5308 ((and (equal id-format
'integer
) (not (integerp res
))) -
1)
5309 ((and (equal id-format
'string
) (not (stringp res
))) "UNKNOWN")
5312 (defun tramp-get-remote-gid-with-id (vec id-format
)
5313 (tramp-send-command-and-read
5315 (format "%s -g%s %s"
5316 (tramp-get-remote-id vec
)
5317 (if (equal id-format
'integer
) "" "n")
5318 (if (equal id-format
'integer
)
5319 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/"))))
5321 (defun tramp-get-remote-gid-with-perl (vec id-format
)
5322 (tramp-send-command-and-read
5324 (format "%s -le '%s'"
5325 (tramp-get-remote-perl vec
)
5326 (if (equal id-format
'integer
)
5327 "print ($)=~/(\\d+)/)"
5328 "print \"\\\"\", scalar getgrgid($)), \"\\\"\""))))
5330 (defun tramp-get-remote-gid-with-python (vec id-format
)
5331 (tramp-send-command-and-read
5333 (format "%s -c \"%s\""
5334 (tramp-get-remote-python vec
)
5335 (if (equal id-format
'integer
)
5336 "import os; print (os.getgid())"
5337 "import os, grp; print ('\\\"' + grp.getgrgid(os.getgid())[0] + '\\\"')"))))
5339 (defun tramp-get-remote-gid (vec id-format
)
5340 (with-tramp-connection-property vec
(format "gid-%s" id-format
)
5344 ((tramp-get-remote-id vec
)
5345 (tramp-get-remote-gid-with-id vec id-format
))
5346 ((tramp-get-remote-perl vec
)
5347 (tramp-get-remote-gid-with-perl vec id-format
))
5348 ((tramp-get-remote-python vec
)
5349 (tramp-get-remote-gid-with-python vec id-format
))))))
5350 ;; Ensure there is a valid result.
5352 ((and (equal id-format
'integer
) (not (integerp res
))) -
1)
5353 ((and (equal id-format
'string
) (not (stringp res
))) "UNKNOWN")
5356 ;; Some predefined connection properties.
5357 (defun tramp-get-inline-compress (vec prop size
)
5358 "Return the compress command related to PROP.
5359 PROP is either `inline-compress' or `inline-decompress'. SIZE is
5360 the length of the file to be compressed.
5362 If no corresponding command is found, nil is returned."
5363 (when (and (integerp tramp-inline-compress-start-size
)
5364 (> size tramp-inline-compress-start-size
))
5365 (with-tramp-connection-property (tramp-get-connection-process vec
) prop
5366 (tramp-find-inline-compress vec
)
5367 (tramp-get-connection-property
5368 (tramp-get-connection-process vec
) prop nil
))))
5370 (defun tramp-get-inline-coding (vec prop size
)
5371 "Return the coding command related to PROP.
5372 PROP is either `remote-encoding', `remote-decoding',
5373 `local-encoding' or `local-decoding'.
5375 SIZE is the length of the file to be coded. Depending on SIZE,
5376 compression might be applied.
5378 If no corresponding command is found, nil is returned.
5379 Otherwise, either a string is returned which contains a `%s' mark
5380 to be used for the respective input or output file; or a Lisp
5381 function cell is returned to be applied on a buffer."
5382 ;; We must catch the errors, because we want to return `nil', when
5383 ;; no inline coding is found.
5386 (with-tramp-connection-property
5387 (tramp-get-connection-process vec
) prop
5388 (tramp-find-inline-encoding vec
)
5389 (tramp-get-connection-property
5390 (tramp-get-connection-process vec
) prop nil
)))
5391 (prop1 (if (string-match "encoding" prop
)
5392 "inline-compress" "inline-decompress"))
5394 ;; The connection property might have been cached. So we must
5395 ;; send the script to the remote side - maybe.
5396 (when (and coding
(symbolp coding
) (string-match "remote" prop
))
5397 (let ((name (symbol-name coding
)))
5398 (while (string-match (regexp-quote "-") name
)
5399 (setq name
(replace-match "_" nil t name
)))
5400 (tramp-maybe-send-script vec
(symbol-value coding
) name
)
5401 (setq coding name
)))
5403 ;; Check for the `compress' command.
5404 (setq compress
(tramp-get-inline-compress vec prop1 size
))
5405 ;; Return the value.
5407 ((and compress
(symbolp coding
))
5408 (if (string-match "decompress" prop1
)
5411 (let ((coding-system-for-write 'binary
)
5412 (coding-system-for-read 'binary
))
5414 'call-process-region
(point-min) (point-max)
5415 (car (split-string ,compress
)) t t nil
5416 (cdr (split-string ,compress
)))))
5418 (let ((coding-system-for-write 'binary
)
5419 (coding-system-for-read 'binary
))
5421 'call-process-region beg end
5422 (car (split-string ,compress
)) t t nil
5423 (cdr (split-string ,compress
))))
5424 (,coding
(point-min) (point-max)))))
5427 ((and compress
(string-match "decoding" prop
))
5429 ;; Windows shells need the program file name after
5430 ;; the pipe symbol be quoted if they use forward
5431 ;; slashes as directory separators.
5433 ((and (string-match "local" prop
)
5434 (memq system-type
'(windows-nt)))
5436 ((string-match "local" prop
) "(%s | %s)")
5437 (t "(%s | %s >%%s)"))
5441 ;; Windows shells need the program file name after
5442 ;; the pipe symbol be quoted if they use forward
5443 ;; slashes as directory separators.
5444 (if (and (string-match "local" prop
)
5445 (memq system-type
'(windows-nt)))
5446 "(%s <%%s | \"%s\")"
5449 ((string-match "decoding" prop
)
5451 ((string-match "local" prop
) (format "%s" coding
))
5452 (t (format "%s >%%s" coding
))))
5454 (format "%s <%%s" coding
)))))))
5456 (add-hook 'tramp-unload-hook
5458 (unload-feature 'tramp-sh
'force
)))
5464 ;; * Don't use globbing for directories with many files, as this is
5465 ;; likely to produce long command lines, and some shells choke on
5466 ;; long command lines.
5467 ;; * Make it work for different encodings, and for different file name
5468 ;; encodings, too. (Daniel Pittman)
5469 ;; * Don't search for perl5 and perl. Instead, only search for perl and
5470 ;; then look if it's the right version (with `perl -v').
5471 ;; * When editing a remote CVS controlled file as a different user, VC
5472 ;; gets confused about the file locking status. Try to find out why
5473 ;; the workaround doesn't work.
5474 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5475 ;; until the last but one hop via `start-file-process'. Apply it
5476 ;; also for ftp and smb.
5477 ;; * WIBNI if we had a command "trampclient"? If I was editing in
5478 ;; some shell with root privileges, it would be nice if I could
5480 ;; trampclient filename.c
5481 ;; as an editor, and the _current_ shell would connect to an Emacs
5482 ;; server and would be used in an existing non-privileged Emacs
5483 ;; session for doing the editing in question.
5484 ;; That way, I need not tell Emacs my password again and be afraid
5485 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
5486 ;; once display a just typed password in the context of a keyboard
5487 ;; sequence prompt for a question immediately following in a shell
5488 ;; script run within Emacs -- nasty).
5489 ;; And if I have some ssh session running to a different computer,
5490 ;; having the possibility of passing a local file there to a local
5491 ;; Emacs session (in case I can arrange for a connection back) would
5493 ;; Likely the corresponding Tramp server should not allow the
5494 ;; equivalent of the emacsclient -eval option in order to make this
5495 ;; reasonably unproblematic. And maybe trampclient should have some
5496 ;; way of passing credentials, like by using an SSL socket or
5497 ;; something. (David Kastrup)
5498 ;; * Reconnect directly to a compliant shell without first going
5499 ;; through the user's default shell. (Pete Forman)
5500 ;; * How can I interrupt the remote process with a signal
5501 ;; (interrupt-process seems not to work)? (Markus Triska)
5502 ;; * Avoid the local shell entirely for starting remote processes. If
5503 ;; so, I think even a signal, when delivered directly to the local
5504 ;; SSH instance, would correctly be propagated to the remote process
5505 ;; automatically; possibly SSH would have to be started with
5506 ;; "-t". (Markus Triska)
5507 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5508 ;; isn't on the remote host. (Mark A. Hershberger)
5509 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
5510 ;; * Optimize out-of-band copying when both methods are scp-like (not
5512 ;; * Keep a second connection open for out-of-band methods like scp or
5515 ;;; tramp-sh.el ends here