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
)
39 (defcustom tramp-inline-compress-start-size
4096
40 "The minimum size of compressing where inline transfer.
41 When inline transfer, compress transferred data of file
42 whose size is this value or above (up to `tramp-copy-size-limit').
43 If it is nil, no compression at all will be applied."
45 :type
'(choice (const nil
) integer
))
47 (defcustom tramp-copy-size-limit
10240
48 "The maximum file size where inline copying is preferred over an \
50 If it is nil, out-of-the-band copy will be used without a check."
52 :type
'(choice (const nil
) integer
))
55 (defcustom tramp-terminal-type
"dumb"
56 "Value of TERM environment variable for logging in to remote host.
57 Because Tramp wants to parse the output of the remote shell, it is easily
58 confused by ANSI color escape sequences and suchlike. Often, shell init
59 files conditionalize this setup based on the TERM environment variable."
63 (defconst tramp-color-escape-sequence-regexp
"\e[[;0-9]+m"
64 "Escape sequences produced by the \"ls\" command.")
66 ;; ksh on OpenBSD 4.5 requires that $PS1 contains a `#' character for
67 ;; root users. It uses the `$' character for other users. In order
68 ;; to guarantee a proper prompt, we use "#$ " for the prompt.
70 (defvar tramp-end-of-output
73 (md5 (concat (prin1-to-string process-environment
) (current-time-string))))
74 "String used to recognize end of output.
75 The '$' character at the end is quoted; the string cannot be
76 detected as prompt when being sent on echoing hosts, therefore.")
79 (defconst tramp-initial-end-of-output
"#$ "
80 "Prompt when establishing a connection.")
82 (defconst tramp-end-of-heredoc
(md5 tramp-end-of-output
)
83 "String used to recognize end of heredoc strings.")
85 ;; Initialize `tramp-methods' with the supported methods.
87 (add-to-list 'tramp-methods
89 (tramp-login-program "rsh")
90 (tramp-login-args (("%h") ("-l" "%u")))
91 (tramp-remote-shell "/bin/sh")
92 (tramp-remote-shell-args ("-c"))
93 (tramp-copy-program "rcp")
94 (tramp-copy-args (("-p" "%k") ("-r")))
95 (tramp-copy-keep-date t
)
96 (tramp-copy-recursive t
)))
98 (add-to-list 'tramp-methods
100 (tramp-login-program "remsh")
101 (tramp-login-args (("%h") ("-l" "%u")))
102 (tramp-remote-shell "/bin/sh")
103 (tramp-remote-shell-args ("-c"))
104 (tramp-copy-program "rcp")
105 (tramp-copy-args (("-p" "%k")))
106 (tramp-copy-keep-date t
)))
108 (add-to-list 'tramp-methods
110 (tramp-login-program "ssh")
111 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
112 ("-e" "none") ("%h")))
113 (tramp-async-args (("-q")))
114 (tramp-remote-shell "/bin/sh")
115 (tramp-remote-shell-args ("-c"))
116 (tramp-copy-program "scp")
117 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r") ("%c")))
118 (tramp-copy-keep-date t
)
119 (tramp-copy-recursive t
)
120 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
121 ("-o" "UserKnownHostsFile=/dev/null")
122 ("-o" "StrictHostKeyChecking=no")))
123 (tramp-default-port 22)))
125 (add-to-list 'tramp-methods
127 (tramp-login-program "ssh")
128 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
129 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
130 (tramp-async-args (("-q")))
131 (tramp-remote-shell "/bin/sh")
132 (tramp-remote-shell-args ("-c"))
133 (tramp-copy-program "scp")
134 (tramp-copy-args (("-P" "%p") ("-p" "%k")
135 ("-q") ("-r") ("%c")))
136 (tramp-copy-keep-date t
)
137 (tramp-copy-recursive t
)
138 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
139 ("-o" "UserKnownHostsFile=/dev/null")
140 ("-o" "StrictHostKeyChecking=no")))
141 (tramp-default-port 22)))
143 (add-to-list 'tramp-methods
145 (tramp-login-program "ssh")
146 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
147 ("-e" "none") ("%h")))
148 (tramp-async-args (("-q")))
149 (tramp-remote-shell "/bin/sh")
150 (tramp-remote-shell-args ("-c"))
151 (tramp-copy-program "sftp")
152 (tramp-copy-args ("%c"))))
154 (add-to-list 'tramp-methods
156 (tramp-login-program "ssh")
157 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
158 ("-e" "none") ("%h")))
159 (tramp-async-args (("-q")))
160 (tramp-remote-shell "/bin/sh")
161 (tramp-remote-shell-args ("-c"))
162 (tramp-copy-program "rsync")
163 (tramp-copy-args (("-t" "%k") ("-r")))
164 (tramp-copy-env (("RSYNC_RSH") ("ssh" "%c")))
165 (tramp-copy-keep-date t
)
166 (tramp-copy-keep-tmpfile t
)
167 (tramp-copy-recursive t
)))
169 (add-to-list 'tramp-methods
171 (tramp-login-program "rsh")
172 (tramp-login-args (("%h") ("-l" "%u")))
173 (tramp-remote-shell "/bin/sh")
174 (tramp-remote-shell-args ("-c"))))
176 (add-to-list 'tramp-methods
178 (tramp-login-program "remsh")
179 (tramp-login-args (("%h") ("-l" "%u")))
180 (tramp-remote-shell "/bin/sh")
181 (tramp-remote-shell-args ("-c"))))
183 (add-to-list 'tramp-methods
185 (tramp-login-program "ssh")
186 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
187 ("-e" "none") ("%h")))
188 (tramp-async-args (("-q")))
189 (tramp-remote-shell "/bin/sh")
190 (tramp-remote-shell-args ("-c"))
191 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
192 ("-o" "UserKnownHostsFile=/dev/null")
193 ("-o" "StrictHostKeyChecking=no")))
194 (tramp-default-port 22)))
196 (add-to-list 'tramp-methods
198 (tramp-login-program "ssh")
199 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
200 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
201 (tramp-async-args (("-q")))
202 (tramp-remote-shell "/bin/sh")
203 (tramp-remote-shell-args ("-c"))
204 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
205 ("-o" "UserKnownHostsFile=/dev/null")
206 ("-o" "StrictHostKeyChecking=no")))
207 (tramp-default-port 22)))
209 (add-to-list 'tramp-methods
211 (tramp-login-program "telnet")
212 (tramp-login-args (("%h") ("%p")))
213 (tramp-remote-shell "/bin/sh")
214 (tramp-remote-shell-args ("-c"))
215 (tramp-default-port 23)))
217 (add-to-list 'tramp-methods
219 (tramp-login-program "su")
220 (tramp-login-args (("-") ("%u")))
221 (tramp-remote-shell "/bin/sh")
222 (tramp-remote-shell-args ("-c"))
223 (tramp-connection-timeout 10)))
225 (add-to-list 'tramp-methods
227 (tramp-login-program "sudo")
228 (tramp-login-args (("-u" "%u") ("-s") ("-H") ("-p" "Password:")))
229 (tramp-remote-shell "/bin/sh")
230 (tramp-remote-shell-args ("-c"))
231 (tramp-connection-timeout 10)))
233 (add-to-list 'tramp-methods
235 (tramp-login-program "ksu")
236 (tramp-login-args (("%u") ("-q")))
237 (tramp-remote-shell "/bin/sh")
238 (tramp-remote-shell-args ("-c"))
239 (tramp-connection-timeout 10)))
241 (add-to-list 'tramp-methods
243 (tramp-login-program "krlogin")
244 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
245 (tramp-remote-shell "/bin/sh")
246 (tramp-remote-shell-args ("-c"))))
248 (add-to-list 'tramp-methods
250 (tramp-login-program "plink")
251 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
252 (tramp-remote-shell "/bin/sh")
253 (tramp-remote-shell-args ("-c"))
254 (tramp-default-port 22)))
256 (add-to-list 'tramp-methods
258 (tramp-login-program "plink")
259 ;; ("%h") must be a single element, see
260 ;; `tramp-compute-multi-hops'.
261 (tramp-login-args (("-load") ("%h") ("-t")
263 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
265 tramp-initial-end-of-output
))
267 (tramp-remote-shell "/bin/sh")
268 (tramp-remote-shell-args ("-c"))))
270 (add-to-list 'tramp-methods
272 (tramp-login-program "plink")
273 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
274 (tramp-remote-shell "/bin/sh")
275 (tramp-remote-shell-args ("-c"))
276 (tramp-copy-program "pscp")
277 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-scp") ("-p" "%k")
279 (tramp-copy-keep-date t
)
280 (tramp-copy-recursive t
)
281 (tramp-default-port 22)))
283 (add-to-list 'tramp-methods
285 (tramp-login-program "plink")
286 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
287 (tramp-remote-shell "/bin/sh")
288 (tramp-remote-shell-args ("-c"))
289 (tramp-copy-program "pscp")
290 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-sftp") ("-p" "%k")
292 (tramp-copy-keep-date t
)
293 (tramp-copy-recursive t
)))
295 (add-to-list 'tramp-methods
297 (tramp-login-program "fsh")
298 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
299 (tramp-remote-shell "/bin/sh")
300 (tramp-remote-shell-args ("-i") ("-c"))
301 (tramp-copy-program "fcp")
302 (tramp-copy-args (("-p" "%k")))
303 (tramp-copy-keep-date t
)))
306 (add-to-list 'tramp-default-method-alist
307 `(,tramp-local-host-regexp
"\\`root\\'" "su"))
310 (add-to-list 'tramp-default-user-alist
311 `(,(concat "\\`" (regexp-opt '("su" "sudo" "ksu")) "\\'")
313 ;; Do not add "ssh" based methods, otherwise ~/.ssh/config would be ignored.
314 ;; Do not add "plink" based methods, they ask interactively for the user.
316 (add-to-list 'tramp-default-user-alist
319 (regexp-opt '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
321 nil
,(user-login-name)))
324 (defconst tramp-completion-function-alist-rsh
325 '((tramp-parse-rhosts "/etc/hosts.equiv")
326 (tramp-parse-rhosts "~/.rhosts"))
327 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
330 (defconst tramp-completion-function-alist-ssh
331 '((tramp-parse-rhosts "/etc/hosts.equiv")
332 (tramp-parse-rhosts "/etc/shosts.equiv")
333 (tramp-parse-shosts "/etc/ssh_known_hosts")
334 (tramp-parse-sconfig "/etc/ssh_config")
335 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
336 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
337 (tramp-parse-rhosts "~/.rhosts")
338 (tramp-parse-rhosts "~/.shosts")
339 (tramp-parse-shosts "~/.ssh/known_hosts")
340 (tramp-parse-sconfig "~/.ssh/config")
341 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
342 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
343 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
346 (defconst tramp-completion-function-alist-telnet
347 '((tramp-parse-hosts "/etc/hosts"))
348 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
351 (defconst tramp-completion-function-alist-su
352 '((tramp-parse-passwd "/etc/passwd"))
353 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
356 (defconst tramp-completion-function-alist-putty
358 ,(if (memq system-type
'(windows-nt))
359 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"
360 "~/.putty/sessions")))
361 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty sessions.")
364 (eval-after-load 'tramp
366 (tramp-set-completion-function "rcp" tramp-completion-function-alist-rsh
)
367 (tramp-set-completion-function "remcp" tramp-completion-function-alist-rsh
)
368 (tramp-set-completion-function "scp" tramp-completion-function-alist-ssh
)
369 (tramp-set-completion-function "scpx" tramp-completion-function-alist-ssh
)
370 (tramp-set-completion-function "sftp" tramp-completion-function-alist-ssh
)
371 (tramp-set-completion-function "rsync" tramp-completion-function-alist-ssh
)
372 (tramp-set-completion-function "rsh" tramp-completion-function-alist-rsh
)
373 (tramp-set-completion-function "remsh" tramp-completion-function-alist-rsh
)
374 (tramp-set-completion-function "ssh" tramp-completion-function-alist-ssh
)
375 (tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh
)
376 (tramp-set-completion-function
377 "telnet" tramp-completion-function-alist-telnet
)
378 (tramp-set-completion-function "su" tramp-completion-function-alist-su
)
379 (tramp-set-completion-function "sudo" tramp-completion-function-alist-su
)
380 (tramp-set-completion-function "ksu" tramp-completion-function-alist-su
)
381 (tramp-set-completion-function
382 "krlogin" tramp-completion-function-alist-rsh
)
383 (tramp-set-completion-function "plink" tramp-completion-function-alist-ssh
)
384 (tramp-set-completion-function
385 "plinkx" tramp-completion-function-alist-putty
)
386 (tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh
)
387 (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh
)))
389 ;; "getconf PATH" yields:
390 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
391 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
392 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
393 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
396 (defcustom tramp-remote-path
397 '(tramp-default-remote-path "/bin" "/usr/bin" "/sbin" "/usr/sbin"
398 "/usr/local/bin" "/usr/local/sbin" "/local/bin" "/local/freeware/bin"
399 "/local/gnu/bin" "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin"
400 "/opt/bin" "/opt/sbin" "/opt/local/bin")
401 "List of directories to search for executables on remote host.
402 For every remote host, this variable will be set buffer local,
403 keeping the list of existing directories on that host.
405 You can use `~' in this list, but when searching for a shell which groks
406 tilde expansion, all directory names starting with `~' will be ignored.
408 `Default Directories' represent the list of directories given by
409 the command \"getconf PATH\". It is recommended to use this
410 entry on top of this list, because these are the default
411 directories for POSIX compatible commands. On remote hosts which
412 do not offer the getconf command (like cygwin), the value
413 \"/bin:/usr/bin\" is used instead of.
415 `Private Directories' are the settings of the $PATH environment,
416 as given in your `~/.profile'."
418 :type
'(repeat (choice
419 (const :tag
"Default Directories" tramp-default-remote-path
)
420 (const :tag
"Private Directories" tramp-own-remote-path
)
421 (string :tag
"Directory"))))
424 (defcustom tramp-remote-process-environment
425 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "TMOUT=0" "LC_CTYPE=''"
426 ,(format "TERM=%s" tramp-terminal-type
)
427 "EMACS=t" ;; Deprecated.
428 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version
)
429 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH=" "PAGER=\"\""
430 "autocorrect=" "correct=")
431 "List of environment variables to be set on the remote host.
433 Each element should be a string of the form ENVVARNAME=VALUE. An
434 entry ENVVARNAME= disables the corresponding environment variable,
435 which might have been set in the init files like ~/.profile.
437 Special handling is applied to the PATH environment, which should
438 not be set here. Instead, it should be set via `tramp-remote-path'."
440 :type
'(repeat string
))
442 (defcustom tramp-sh-extra-args
'(("/bash\\'" .
"-norc -noprofile"))
443 "Alist specifying extra arguments to pass to the remote shell.
444 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
445 matching the shell file name and ARGS is a string specifying the
448 This variable is only used when Tramp needs to start up another shell
449 for tilde expansion. The extra arguments should typically prevent the
450 shell from reading its init file."
452 ;; This might be the wrong way to test whether the widget type
453 ;; `alist' is available. Who knows the right way to test it?
454 :type
(if (get 'alist
'widget-type
)
455 '(alist :key-type string
:value-type string
)
456 '(repeat (cons string string
))))
458 (defconst tramp-actions-before-shell
459 '((tramp-login-prompt-regexp tramp-action-login
)
460 (tramp-password-prompt-regexp tramp-action-password
)
461 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
462 (shell-prompt-pattern tramp-action-succeed
)
463 (tramp-shell-prompt-pattern tramp-action-succeed
)
464 (tramp-yesno-prompt-regexp tramp-action-yesno
)
465 (tramp-yn-prompt-regexp tramp-action-yn
)
466 (tramp-terminal-prompt-regexp tramp-action-terminal
)
467 (tramp-process-alive-regexp tramp-action-process-alive
))
468 "List of pattern/action pairs.
469 Whenever a pattern matches, the corresponding action is performed.
470 Each item looks like (PATTERN ACTION).
472 The PATTERN should be a symbol, a variable. The value of this
473 variable gives the regular expression to search for. Note that the
474 regexp must match at the end of the buffer, \"\\'\" is implicitly
477 The ACTION should also be a symbol, but a function. When the
478 corresponding PATTERN matches, the ACTION function is called.")
480 (defconst tramp-actions-copy-out-of-band
481 '((tramp-password-prompt-regexp tramp-action-password
)
482 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
483 (tramp-copy-failed-regexp tramp-action-permission-denied
)
484 (tramp-process-alive-regexp tramp-action-out-of-band
))
485 "List of pattern/action pairs.
486 This list is used for copying/renaming with out-of-band methods.
488 See `tramp-actions-before-shell' for more info.")
490 (defconst tramp-uudecode
491 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
494 "Shell function to implement `uudecode' to standard output.
495 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
496 for this or `uudecode -p', but some systems don't, and for them
497 we have this shell function.")
499 (defconst tramp-perl-file-truename
502 use Cwd \"realpath\";
505 my ($volume, @dirs) = @_;
506 my $real = realpath(File::Spec->catpath(
507 $volume, File::Spec->catdir(@dirs), \"\"));
509 my ($vol, $dir) = File::Spec->splitpath($real, 1);
510 return ($vol, File::Spec->splitdir($dir));
513 my $last = pop(@dirs);
514 ($volume, @dirs) = recursive($volume, @dirs);
516 return ($volume, @dirs);
520 $result = realpath($ARGV[0]);
522 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
523 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
525 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
528 if ($ARGV[0] =~ /\\/$/) {
529 $result = $result . \"/\";
532 print \"\\\"$result\\\"\\n\";
533 ' \"$1\" 2>/dev/null"
534 "Perl script to produce output suitable for use with `file-truename'
535 on the remote file system.
536 Escape sequence %s is replaced with name of Perl binary.
537 This string is passed to `format', so percent characters need to be doubled.")
539 (defconst tramp-perl-file-name-all-completions
549 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
550 @files = readdir(d); closedir(d);
551 foreach $f (@files) {
552 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
553 if (-d \"$ARGV[0]/$f\") {
562 ' \"$1\" \"$2\" \"$3\" 2>/dev/null"
563 "Perl script to produce output suitable for use with
564 `file-name-all-completions' on the remote file system. Escape
565 sequence %s is replaced with name of Perl binary. This string is
566 passed to `format', so percent characters need to be doubled.")
568 ;; Perl script to implement `file-attributes' in a Lisp `read'able
569 ;; output. If you are hacking on this, note that you get *no* output
570 ;; unless this spits out a complete line, including the '\n' at the
572 ;; The device number is returned as "-1", because there will be a virtual
573 ;; device number set in `tramp-sh-handle-file-attributes'.
574 (defconst tramp-perl-file-attributes
576 @stat = lstat($ARGV[0]);
581 if (($stat[2] & 0170000) == 0120000)
583 $type = readlink($ARGV[0]);
584 $type = \"\\\"$type\\\"\";
586 elsif (($stat[2] & 0170000) == 040000)
594 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
595 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
597 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
602 $stat[8] >> 16 & 0xffff,
604 $stat[9] >> 16 & 0xffff,
606 $stat[10] >> 16 & 0xffff,
610 $stat[1] >> 16 & 0xffff,
612 );' \"$1\" \"$2\" 2>/dev/null"
613 "Perl script to produce output suitable for use with `file-attributes'
614 on the remote file system.
615 Escape sequence %s is replaced with name of Perl binary.
616 This string is passed to `format', so percent characters need to be doubled.")
618 (defconst tramp-perl-directory-files-and-attributes
620 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
621 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
622 @list = readdir(DIR);
626 for($i = 0; $i < $n; $i++)
628 $filename = $list[$i];
629 @stat = lstat($filename);
630 if (($stat[2] & 0170000) == 0120000)
632 $type = readlink($filename);
633 $type = \"\\\"$type\\\"\";
635 elsif (($stat[2] & 0170000) == 040000)
643 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
644 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
646 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
652 $stat[8] >> 16 & 0xffff,
654 $stat[9] >> 16 & 0xffff,
656 $stat[10] >> 16 & 0xffff,
660 $stat[1] >> 16 & 0xffff,
662 $stat[0] >> 16 & 0xffff,
665 printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
666 "Perl script implementing `directory-files-attributes' as Lisp `read'able
668 Escape sequence %s is replaced with name of Perl binary.
669 This string is passed to `format', so percent characters need to be doubled.")
671 ;; These two use base64 encoding.
672 (defconst tramp-perl-encode-with-module
673 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
674 "Perl program to use for encoding a file.
675 Escape sequence %s is replaced with name of Perl binary.
676 This string is passed to `format', so percent characters need to be doubled.
677 This implementation requires the MIME::Base64 Perl module to be installed
678 on the remote host.")
680 (defconst tramp-perl-decode-with-module
681 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
682 "Perl program to use for decoding a file.
683 Escape sequence %s is replaced with name of Perl binary.
684 This string is passed to `format', so percent characters need to be doubled.
685 This implementation requires the MIME::Base64 Perl module to be installed
686 on the remote host.")
688 (defconst tramp-perl-encode
690 # This script contributed by Juanma Barranquero <lektu@terra.es>.
691 # Copyright (C) 2002-2014 Free Software Foundation, Inc.
696 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
697 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
701 # We read in chunks of 54 bytes, to generate output lines
702 # of 72 chars (plus end of line)
703 while (read STDIN, $data, 54) {
706 # Only for the last chunk, and only if did not fill the last three-byte packet
708 my $mod = length($data) %% 3;
709 $pad = q(=) x (3 - $mod) if $mod;
712 # Not the fastest method, but it is simple: unpack to binary string, split
713 # by groups of 6 bits and convert back from binary to byte; then map into
714 # the translation table
718 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
722 "Perl program to use for encoding a file.
723 Escape sequence %s is replaced with name of Perl binary.
724 This string is passed to `format', so percent characters need to be doubled.")
726 (defconst tramp-perl-decode
728 # This script contributed by Juanma Barranquero <lektu@terra.es>.
729 # Copyright (C) 2002-2014 Free Software Foundation, Inc.
734 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
735 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
738 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
742 # We are going to accumulate into $pending to accept any line length
743 # (we do not check they are <= 76 chars as the RFC says)
746 while (my $data = <STDIN>) {
749 # If we find one or two =, we have reached the end and
750 # any following data is to be discarded
751 my $finished = $data =~ s/(==?).*/$1/;
754 my $len = length($pending);
755 my $chunk = substr($pending, 0, $len & ~3);
756 $pending = substr($pending, $len & ~3 + 1);
758 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
759 # split in 8-bit chunks and convert back to char.
762 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
766 "Perl program to use for decoding a file.
767 Escape sequence %s is replaced with name of Perl binary.
768 This string is passed to `format', so percent characters need to be doubled.")
770 (defconst tramp-perl-pack
771 "%s -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
772 "Perl program to use for encoding a file.
773 Escape sequence %s is replaced with name of Perl binary.")
775 (defconst tramp-perl-unpack
776 "%s -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"
777 "Perl program to use for decoding a file.
778 Escape sequence %s is replaced with name of Perl binary.")
780 (defconst tramp-vc-registered-read-file-names
783 if %s \"$file\"; then
784 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
786 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
788 if %s \"$file\"; then
789 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
791 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
795 "Script to check existence of VC related files.
796 It must be send formatted with two strings; the tests for file
797 existence, and file readability. Input shall be read via
798 here-document, otherwise the command could exceed maximum length
801 ;; New handlers should be added here.
802 (defconst tramp-sh-file-name-handler-alist
803 '(;; `access-file' performed by default handler.
804 (add-name-to-file . tramp-sh-handle-add-name-to-file
)
805 ;; `byte-compiler-base-file-name' performed by default handler.
806 (copy-directory . tramp-sh-handle-copy-directory
)
807 (copy-file . tramp-sh-handle-copy-file
)
808 (delete-directory . tramp-sh-handle-delete-directory
)
809 (delete-file . tramp-sh-handle-delete-file
)
810 ;; `diff-latest-backup-file' performed by default handler.
811 (directory-file-name . tramp-handle-directory-file-name
)
812 (directory-files . tramp-handle-directory-files
)
813 (directory-files-and-attributes
814 . tramp-sh-handle-directory-files-and-attributes
)
815 ;; `dired-call-process' performed by default handler.
816 (dired-compress-file . tramp-sh-handle-dired-compress-file
)
817 (dired-recursive-delete-directory
818 . tramp-sh-handle-dired-recursive-delete-directory
)
819 (dired-uncache . tramp-handle-dired-uncache
)
820 (expand-file-name . tramp-sh-handle-expand-file-name
)
821 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p
)
822 (file-acl . tramp-sh-handle-file-acl
)
823 (file-attributes . tramp-sh-handle-file-attributes
)
824 (file-directory-p . tramp-sh-handle-file-directory-p
)
825 ;; `file-equal-p' performed by default handler.
826 (file-executable-p . tramp-sh-handle-file-executable-p
)
827 (file-exists-p . tramp-sh-handle-file-exists-p
)
828 ;; `file-in-directory-p' performed by default handler.
829 (file-local-copy . tramp-sh-handle-file-local-copy
)
830 (file-modes . tramp-handle-file-modes
)
831 (file-name-all-completions . tramp-sh-handle-file-name-all-completions
)
832 (file-name-as-directory . tramp-handle-file-name-as-directory
)
833 (file-name-completion . tramp-handle-file-name-completion
)
834 (file-name-directory . tramp-handle-file-name-directory
)
835 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
836 ;; `file-name-sans-versions' performed by default handler.
837 (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p
)
838 (file-notify-add-watch . tramp-sh-handle-file-notify-add-watch
)
839 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch
)
840 (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p
)
841 (file-readable-p . tramp-sh-handle-file-readable-p
)
842 (file-regular-p . tramp-handle-file-regular-p
)
843 (file-remote-p . tramp-handle-file-remote-p
)
844 (file-selinux-context . tramp-sh-handle-file-selinux-context
)
845 (file-symlink-p . tramp-handle-file-symlink-p
)
846 (file-truename . tramp-sh-handle-file-truename
)
847 (file-writable-p . tramp-sh-handle-file-writable-p
)
848 (find-backup-file-name . tramp-handle-find-backup-file-name
)
849 ;; `find-file-noselect' performed by default handler.
850 ;; `get-file-buffer' performed by default handler.
851 (insert-directory . tramp-sh-handle-insert-directory
)
852 (insert-file-contents . tramp-handle-insert-file-contents
)
853 (insert-file-contents-literally
854 . tramp-sh-handle-insert-file-contents-literally
)
855 (load . tramp-handle-load
)
856 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name
)
857 (make-directory . tramp-sh-handle-make-directory
)
858 (make-symbolic-link . tramp-sh-handle-make-symbolic-link
)
859 (process-file . tramp-sh-handle-process-file
)
860 (rename-file . tramp-sh-handle-rename-file
)
861 (set-file-acl . tramp-sh-handle-set-file-acl
)
862 (set-file-modes . tramp-sh-handle-set-file-modes
)
863 (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context
)
864 (set-file-times . tramp-sh-handle-set-file-times
)
865 (set-visited-file-modtime . tramp-sh-handle-set-visited-file-modtime
)
866 (shell-command . tramp-handle-shell-command
)
867 (start-file-process . tramp-sh-handle-start-file-process
)
868 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
869 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
870 (vc-registered . tramp-sh-handle-vc-registered
)
871 (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime
)
872 (write-region . tramp-sh-handle-write-region
))
873 "Alist of handler functions.
874 Operations not mentioned here will be handled by the normal Emacs functions.")
876 ;; This must be the last entry, because `identity' always matches.
878 (add-to-list 'tramp-foreign-file-name-handler-alist
879 '(identity . tramp-sh-file-name-handler
) 'append
)
881 ;;; File Name Handler Functions:
883 (defun tramp-sh-handle-make-symbolic-link
884 (filename linkname
&optional ok-if-already-exists
)
885 "Like `make-symbolic-link' for Tramp files.
886 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
887 the symlink. If LINKNAME is a Tramp file, only the localname component is
888 used as the target of the symlink.
890 If LINKNAME is a Tramp file and the localname component is relative, then
891 it is expanded first, before the localname component is taken. Note that
892 this can give surprising results if the user/host for the source and
893 target of the symlink differ."
894 (with-parsed-tramp-file-name linkname l
895 (let ((ln (tramp-get-remote-ln l
))
896 (cwd (tramp-run-real-handler
897 'file-name-directory
(list l-localname
))))
901 "Making a symbolic link. ln(1) does not exist on the remote host."))
903 ;; Do the 'confirm if exists' thing.
904 (when (file-exists-p linkname
)
906 (if (or (null ok-if-already-exists
) ; not allowed to exist
907 (and (numberp ok-if-already-exists
)
910 "File %s already exists; make it a link anyway? "
913 l
'file-already-exists
"File %s already exists" l-localname
)
914 (delete-file linkname
)))
916 ;; If FILENAME is a Tramp name, use just the localname component.
917 (when (tramp-tramp-file-p filename
)
919 (tramp-file-name-localname
920 (tramp-dissect-file-name (expand-file-name filename
)))))
922 (tramp-flush-file-property l
(file-name-directory l-localname
))
923 (tramp-flush-file-property l l-localname
)
925 ;; Right, they are on the same host, regardless of user, method,
926 ;; etc. We now make the link on the remote machine. This will
927 ;; occur as the user that FILENAME belongs to.
928 (tramp-send-command-and-check
931 "cd %s && %s -sf %s %s"
932 (tramp-shell-quote-argument cwd
)
934 (tramp-shell-quote-argument filename
)
935 (tramp-shell-quote-argument l-localname
))
938 (defun tramp-sh-handle-file-truename (filename)
939 "Like `file-truename' for Tramp files."
940 (with-parsed-tramp-file-name (expand-file-name filename
) nil
941 (tramp-make-tramp-file-name method user host
942 (with-tramp-file-property v localname
"file-truename"
943 (let ((result nil
)) ; result steps in reverse order
944 (tramp-message v
4 "Finding true name for `%s'" filename
)
946 ;; Use GNU readlink --canonicalize-missing where available.
947 ((tramp-get-remote-readlink v
)
949 (tramp-send-command-and-read
951 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
952 (tramp-get-remote-readlink v
)
953 (tramp-shell-quote-argument localname
)))))
955 ;; Use Perl implementation.
956 ((and (tramp-get-remote-perl v
)
957 (tramp-get-connection-property v
"perl-file-spec" nil
)
958 (tramp-get-connection-property v
"perl-cwd-realpath" nil
))
959 (tramp-maybe-send-script
960 v tramp-perl-file-truename
"tramp_perl_file_truename")
962 (tramp-send-command-and-read
964 (format "tramp_perl_file_truename %s"
965 (tramp-shell-quote-argument localname
)))))
967 ;; Do it yourself. We bind `directory-sep-char' here for
968 ;; XEmacs on Windows, which would otherwise use backslash.
969 (t (let* ((directory-sep-char ?
/)
970 (steps (tramp-compat-split-string localname
"/"))
971 (localnamedir (tramp-run-real-handler
972 'file-name-as-directory
(list localname
)))
973 (is-dir (string= localname localnamedir
))
976 ;; Don't make the following value larger than
977 ;; necessary. People expect an error message in
978 ;; a timely fashion when something is wrong;
979 ;; otherwise they might think that Emacs is hung.
980 ;; Of course, correctness has to come first.
983 (while (and steps
(< numchase numchase-limit
))
984 (setq thisstep
(pop steps
))
988 (append '("") (reverse result
) (list thisstep
))
991 (nth 0 (file-attributes
992 (tramp-make-tramp-file-name
999 (cond ((string= "." thisstep
)
1000 (tramp-message v
5 "Ignoring step `.'"))
1001 ((string= ".." thisstep
)
1002 (tramp-message v
5 "Processing step `..'")
1004 ((stringp symlink-target
)
1005 ;; It's a symlink, follow it.
1007 v
5 "Follow symlink to %s" symlink-target
)
1008 (setq numchase
(1+ numchase
))
1009 (when (file-name-absolute-p symlink-target
)
1011 ;; If the symlink was absolute, we'll get a
1012 ;; string like "/user@host:/some/target";
1013 ;; extract the "/some/target" part from it.
1014 (when (tramp-tramp-file-p symlink-target
)
1015 (unless (tramp-equal-remote filename symlink-target
)
1018 "Symlink target `%s' on wrong host"
1020 (setq symlink-target localname
))
1022 (append (tramp-compat-split-string
1027 (setq result
(cons thisstep result
)))))
1028 (when (>= numchase numchase-limit
)
1031 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
1032 (setq result
(reverse result
))
1033 ;; Combine list to form string.
1036 (mapconcat 'identity
(cons "" result
) "/")
1039 (or (string= "" result
)
1040 (not (string= (substring result -
1) "/"))))
1041 (setq result
(concat result
"/"))))))
1043 (tramp-message v
4 "True name of `%s' is `%s'" localname result
)
1048 (defun tramp-sh-handle-file-exists-p (filename)
1049 "Like `file-exists-p' for Tramp files."
1050 (with-parsed-tramp-file-name filename nil
1051 (with-tramp-file-property v localname
"file-exists-p"
1052 (or (not (null (tramp-get-file-property
1053 v localname
"file-attributes-integer" nil
)))
1054 (not (null (tramp-get-file-property
1055 v localname
"file-attributes-string" nil
)))
1056 (tramp-send-command-and-check
1060 (tramp-get-file-exists-command v
)
1061 (tramp-shell-quote-argument localname
)))))))
1063 (defun tramp-sh-handle-file-attributes (filename &optional id-format
)
1064 "Like `file-attributes' for Tramp files."
1065 (unless id-format
(setq id-format
'integer
))
1066 ;; Don't modify `last-coding-system-used' by accident.
1067 (let ((last-coding-system-used last-coding-system-used
))
1068 (with-parsed-tramp-file-name (expand-file-name filename
) nil
1069 (with-tramp-file-property
1070 v localname
(format "file-attributes-%s" id-format
)
1072 (tramp-convert-file-attributes
1076 ((tramp-get-remote-stat v
)
1077 (tramp-do-file-attributes-with-stat v localname id-format
))
1078 ((tramp-get-remote-perl v
)
1079 (tramp-do-file-attributes-with-perl v localname id-format
))
1081 ;; The scripts could fail, for example with huge file size.
1082 (tramp-do-file-attributes-with-ls v localname id-format
))))))))
1084 (defun tramp-do-file-attributes-with-ls (vec localname
&optional id-format
)
1085 "Implement `file-attributes' for Tramp files using the ls(1) command."
1087 res-inode res-filemodes res-numlinks
1088 res-uid res-gid res-size res-symlink-target
)
1089 (tramp-message vec
5 "file attributes with ls: %s" localname
)
1092 (format "(%s %s || %s -h %s) && %s %s %s"
1093 (tramp-get-file-exists-command vec
)
1094 (tramp-shell-quote-argument localname
)
1095 (tramp-get-test-command vec
)
1096 (tramp-shell-quote-argument localname
)
1097 (tramp-get-ls-command vec
)
1098 (if (eq id-format
'integer
) "-ildn" "-ild")
1099 (tramp-shell-quote-argument localname
)))
1100 ;; parse `ls -l' output ...
1101 (with-current-buffer (tramp-get-buffer vec
)
1102 (when (> (buffer-size) 0)
1103 (goto-char (point-min))
1107 (read (current-buffer))
1108 (invalid-read-syntax
1109 (when (and (equal (cadr err
)
1110 "Integer constant overflow in reader")
1112 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1114 (let* ((big (read (substring (car (cddr err
)) 0
1115 (match-beginning 1))))
1116 (small (read (match-string 1 (car (cddr err
)))))
1117 (twiddle (/ small
65536)))
1118 (cons (+ big twiddle
)
1119 (- small
(* twiddle
65536))))))))
1120 ;; ... file mode flags
1121 (setq res-filemodes
(symbol-name (read (current-buffer))))
1123 (setq res-numlinks
(read (current-buffer)))
1125 (setq res-uid
(read (current-buffer)))
1126 (setq res-gid
(read (current-buffer)))
1127 (if (eq id-format
'integer
)
1129 (unless (numberp res-uid
) (setq res-uid -
1))
1130 (unless (numberp res-gid
) (setq res-gid -
1)))
1132 (unless (stringp res-uid
) (setq res-uid
(symbol-name res-uid
)))
1133 (unless (stringp res-gid
) (setq res-gid
(symbol-name res-gid
)))))
1135 (setq res-size
(read (current-buffer)))
1136 ;; From the file modes, figure out other stuff.
1137 (setq symlinkp
(eq ?l
(aref res-filemodes
0)))
1138 (setq dirp
(eq ?d
(aref res-filemodes
0)))
1139 ;; if symlink, find out file name pointed to
1141 (search-forward "-> ")
1142 (setq res-symlink-target
(buffer-substring (point) (point-at-eol))))
1143 ;; return data gathered
1145 ;; 0. t for directory, string (name linked to) for symbolic
1147 (or dirp res-symlink-target
)
1148 ;; 1. Number of links to file.
1154 ;; 4. Last access time, as a list of integers. Normally this
1155 ;; would be in the same format as `current-time', but the
1156 ;; subseconds part is not currently implemented, and (0 0)
1157 ;; denotes an unknown time.
1158 ;; 5. Last modification time, likewise.
1159 ;; 6. Last status change time, likewise.
1160 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1161 ;; 7. Size in bytes (-1, if number is out of range).
1163 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1165 ;; 9. t if file's gid would change if file were deleted and
1166 ;; recreated. Will be set in `tramp-convert-file-attributes'
1168 ;; 10. inode number.
1170 ;; 11. Device number. Will be replaced by a virtual device number.
1174 (defun tramp-do-file-attributes-with-perl
1175 (vec localname
&optional id-format
)
1176 "Implement `file-attributes' for Tramp files using a Perl script."
1177 (tramp-message vec
5 "file attributes with perl: %s" localname
)
1178 (tramp-maybe-send-script
1179 vec tramp-perl-file-attributes
"tramp_perl_file_attributes")
1180 (tramp-send-command-and-read
1182 (format "tramp_perl_file_attributes %s %s"
1183 (tramp-shell-quote-argument localname
) id-format
)))
1185 (defun tramp-do-file-attributes-with-stat
1186 (vec localname
&optional id-format
)
1187 "Implement `file-attributes' for Tramp files using stat(1) command."
1188 (tramp-message vec
5 "file attributes with stat: %s" localname
)
1189 (tramp-send-command-and-read
1192 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
1193 ;; parse correctly the sequence "((". Therefore, we add a space.
1194 "( (%s %s || %s -h %s) && %s -c '((\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)' %s || echo nil)"
1195 (tramp-get-file-exists-command vec
)
1196 (tramp-shell-quote-argument localname
)
1197 (tramp-get-test-command vec
)
1198 (tramp-shell-quote-argument localname
)
1199 (tramp-get-remote-stat vec
)
1200 (if (eq id-format
'integer
) "%ue0" "\"%U\"")
1201 (if (eq id-format
'integer
) "%ge0" "\"%G\"")
1202 (tramp-shell-quote-argument localname
))))
1204 (defun tramp-sh-handle-set-visited-file-modtime (&optional time-list
)
1205 "Like `set-visited-file-modtime' for Tramp files."
1206 (unless (buffer-file-name)
1207 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1210 (tramp-run-real-handler 'set-visited-file-modtime
(list time-list
))
1211 (let ((f (buffer-file-name))
1213 (with-parsed-tramp-file-name f nil
1214 (let* ((remote-file-name-inhibit-cache t
)
1215 (attr (file-attributes f
))
1216 ;; '(-1 65535) means file doesn't exists yet.
1217 (modtime (or (nth 5 attr
) '(-1 65535))))
1218 (when (boundp 'last-coding-system-used
)
1219 (setq coding-system-used
(symbol-value 'last-coding-system-used
)))
1220 ;; We use '(0 0) as a don't-know value. See also
1221 ;; `tramp-do-file-attributes-with-ls'.
1222 (if (not (equal modtime
'(0 0)))
1223 (tramp-run-real-handler 'set-visited-file-modtime
(list modtime
))
1227 (format "%s -ild %s"
1228 (tramp-get-ls-command v
)
1229 (tramp-shell-quote-argument localname
)))
1230 (setq attr
(buffer-substring (point)
1231 (progn (end-of-line) (point)))))
1232 (tramp-set-file-property
1233 v localname
"visited-file-modtime-ild" attr
))
1234 (when (boundp 'last-coding-system-used
)
1235 (set 'last-coding-system-used coding-system-used
))
1238 ;; This function makes the same assumption as
1239 ;; `tramp-sh-handle-set-visited-file-modtime'.
1240 (defun tramp-sh-handle-verify-visited-file-modtime (&optional buf
)
1241 "Like `verify-visited-file-modtime' for Tramp files.
1242 At the time `verify-visited-file-modtime' calls this function, we
1243 already know that the buffer is visiting a file and that
1244 `visited-file-modtime' does not return 0. Do not call this
1245 function directly, unless those two cases are already taken care
1247 (with-current-buffer (or buf
(current-buffer))
1248 (let ((f (buffer-file-name)))
1249 ;; There is no file visiting the buffer, or the buffer has no
1250 ;; recorded last modification time, or there is no established
1253 (eq (visited-file-modtime) 0)
1254 (not (tramp-file-name-handler 'file-remote-p f nil
'connected
)))
1256 (with-parsed-tramp-file-name f nil
1257 (let* ((remote-file-name-inhibit-cache t
)
1258 (attr (file-attributes f
))
1259 (modtime (nth 5 attr
))
1260 (mt (visited-file-modtime)))
1263 ;; File exists, and has a known modtime.
1264 ((and attr
(not (equal modtime
'(0 0))))
1265 (< (abs (tramp-time-diff
1267 ;; For compatibility, deal with both the old
1268 ;; (HIGH . LOW) and the new (HIGH LOW) return
1269 ;; values of `visited-file-modtime'.
1271 (list (car mt
) (cdr mt
))
1274 ;; Modtime has the don't know value.
1278 (format "%s -ild %s"
1279 (tramp-get-ls-command v
)
1280 (tramp-shell-quote-argument localname
)))
1281 (with-current-buffer (tramp-get-buffer v
)
1282 (setq attr
(buffer-substring
1283 (point) (progn (end-of-line) (point)))))
1286 (tramp-get-file-property
1287 v localname
"visited-file-modtime-ild" "")))
1288 ;; If file does not exist, say it is not modified if and
1289 ;; only if that agrees with the buffer's record.
1290 (t (equal mt
'(-1 65535))))))))))
1292 (defun tramp-sh-handle-set-file-modes (filename mode
)
1293 "Like `set-file-modes' for Tramp files."
1294 (with-parsed-tramp-file-name filename nil
1295 (tramp-flush-file-property v localname
)
1296 ;; FIXME: extract the proper text from chmod's stderr.
1297 (tramp-barf-unless-okay
1299 (format "chmod %s %s"
1300 (tramp-compat-decimal-to-octal mode
)
1301 (tramp-shell-quote-argument localname
))
1302 "Error while changing file's mode %s" filename
)))
1304 (defun tramp-sh-handle-set-file-times (filename &optional time
)
1305 "Like `set-file-times' for Tramp files."
1306 (if (tramp-tramp-file-p filename
)
1307 (with-parsed-tramp-file-name filename nil
1308 (tramp-flush-file-property v localname
)
1309 (let ((time (if (or (null time
) (equal time
'(0 0)))
1312 ;; With GNU Emacs, `format-time-string' has an optional
1313 ;; parameter UNIVERSAL. This is preferred, because we
1314 ;; could handle the case when the remote host is located
1315 ;; in a different time zone as the local host.
1316 (utc (not (featurep 'xemacs
))))
1317 (tramp-send-command-and-check
1318 v
(format "%s touch -t %s %s"
1319 (if utc
"env TZ=UTC" "")
1321 (format-time-string "%Y%m%d%H%M.%S" time t
)
1322 (format-time-string "%Y%m%d%H%M.%S" time
))
1323 (tramp-shell-quote-argument localname
)))))
1325 ;; We handle also the local part, because in older Emacsen,
1326 ;; without `set-file-times', this function is an alias for this.
1327 ;; We are local, so we don't need the UTC settings.
1330 "touch" nil nil nil
"-t"
1331 (format-time-string "%Y%m%d%H%M.%S" time
)
1332 (tramp-shell-quote-argument filename
)))))
1334 (defun tramp-set-file-uid-gid (filename &optional uid gid
)
1335 "Set the ownership for FILENAME.
1336 If UID and GID are provided, these values are used; otherwise uid
1337 and gid of the corresponding user is taken. Both parameters must
1338 be non-negative integers."
1339 ;; Modern Unices allow chown only for root. So we might need
1340 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1341 ;; working with su(do)? when it is needed, so it shall succeed in
1342 ;; the majority of cases.
1343 ;; Don't modify `last-coding-system-used' by accident.
1344 (let ((last-coding-system-used last-coding-system-used
))
1345 (if (tramp-tramp-file-p filename
)
1346 (with-parsed-tramp-file-name filename nil
1347 (if (and (zerop (user-uid)) (tramp-local-host-p v
))
1348 ;; If we are root on the local host, we can do it directly.
1349 (tramp-set-file-uid-gid localname uid gid
)
1350 (let ((uid (or (and (natnump uid
) uid
)
1351 (tramp-get-remote-uid v
'integer
)))
1352 (gid (or (and (natnump gid
) gid
)
1353 (tramp-get-remote-gid v
'integer
))))
1356 "chown %d:%d %s" uid gid
1357 (tramp-shell-quote-argument localname
))))))
1359 ;; We handle also the local part, because there doesn't exist
1360 ;; `set-file-uid-gid'. On W32 "chown" might not work.
1361 (let ((uid (or (and (natnump uid
) uid
) (tramp-get-local-uid 'integer
)))
1362 (gid (or (and (natnump gid
) gid
) (tramp-get-local-gid 'integer
))))
1365 (format "%d:%d" uid gid
) (tramp-shell-quote-argument filename
))))))
1367 (defun tramp-remote-selinux-p (vec)
1368 "Check, whether SELINUX is enabled on the remote host."
1369 (with-tramp-connection-property
1370 (tramp-get-connection-process vec
) "selinux-p"
1371 (let ((result (tramp-find-executable
1372 vec
"getenforce" (tramp-get-remote-path vec
) t t
)))
1375 (tramp-send-command-and-read
1376 vec
(format "echo \\\"`%S`\\\"" result
))
1379 (defun tramp-sh-handle-file-selinux-context (filename)
1380 "Like `file-selinux-context' for Tramp files."
1381 (with-parsed-tramp-file-name filename nil
1382 (with-tramp-file-property v localname
"file-selinux-context"
1383 (let ((context '(nil nil nil nil
))
1384 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1385 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1386 (when (and (tramp-remote-selinux-p v
)
1387 (tramp-send-command-and-check
1390 (tramp-get-ls-command v
)
1391 (tramp-shell-quote-argument localname
))))
1392 (with-current-buffer (tramp-get-connection-buffer v
)
1393 (goto-char (point-min))
1394 (when (re-search-forward regexp
(point-at-eol) t
)
1395 (setq context
(list (match-string 1) (match-string 2)
1396 (match-string 3) (match-string 4))))))
1397 ;; Return the context.
1400 (defun tramp-sh-handle-set-file-selinux-context (filename context
)
1401 "Like `set-file-selinux-context' for Tramp files."
1402 (with-parsed-tramp-file-name filename nil
1403 (if (and (consp context
)
1404 (tramp-remote-selinux-p v
)
1405 (tramp-send-command-and-check
1406 v
(format "chcon %s %s %s %s %s"
1407 (if (stringp (nth 0 context
))
1408 (format "--user=%s" (nth 0 context
)) "")
1409 (if (stringp (nth 1 context
))
1410 (format "--role=%s" (nth 1 context
)) "")
1411 (if (stringp (nth 2 context
))
1412 (format "--type=%s" (nth 2 context
)) "")
1413 (if (stringp (nth 3 context
))
1414 (format "--range=%s" (nth 3 context
)) "")
1415 (tramp-shell-quote-argument localname
))))
1417 (tramp-set-file-property v localname
"file-selinux-context" context
)
1419 (tramp-set-file-property v localname
"file-selinux-context" 'undef
)
1422 (defun tramp-remote-acl-p (vec)
1423 "Check, whether ACL is enabled on the remote host."
1424 (with-tramp-connection-property (tramp-get-connection-process vec
) "acl-p"
1425 (tramp-send-command-and-check vec
"getfacl /")))
1427 (defun tramp-sh-handle-file-acl (filename)
1428 "Like `file-acl' for Tramp files."
1429 (with-parsed-tramp-file-name filename nil
1430 (with-tramp-file-property v localname
"file-acl"
1431 (when (and (tramp-remote-acl-p v
)
1432 (tramp-send-command-and-check
1434 "getfacl -ac %s 2>/dev/null"
1435 (tramp-shell-quote-argument localname
))))
1436 (with-current-buffer (tramp-get-connection-buffer v
)
1437 (goto-char (point-max))
1438 (delete-blank-lines)
1439 (when (> (point-max) (point-min))
1440 (tramp-compat-funcall
1441 'substring-no-properties
(buffer-string))))))))
1443 (defun tramp-sh-handle-set-file-acl (filename acl-string
)
1444 "Like `set-file-acl' for Tramp files."
1445 (with-parsed-tramp-file-name (expand-file-name filename
) nil
1446 (if (and (stringp acl-string
) (tramp-remote-acl-p v
)
1449 v
(format "setfacl --set-file=- %s <<'%s'\n%s\n%s\n"
1450 (tramp-shell-quote-argument localname
)
1451 tramp-end-of-heredoc
1453 tramp-end-of-heredoc
))
1454 (tramp-send-command-and-check v nil
)))
1457 (tramp-set-file-property v localname
"file-acl" acl-string
)
1459 ;; In case of errors, we return `nil'.
1460 (tramp-set-file-property v localname
"file-acl-string" 'undef
)
1463 ;; Simple functions using the `test' command.
1465 (defun tramp-sh-handle-file-executable-p (filename)
1466 "Like `file-executable-p' for Tramp files."
1467 (with-parsed-tramp-file-name filename nil
1468 (with-tramp-file-property v localname
"file-executable-p"
1469 ;; Examine `file-attributes' cache to see if request can be
1470 ;; satisfied without remote operation.
1471 (or (tramp-check-cached-permissions v ?x
)
1472 (tramp-run-test "-x" filename
)))))
1474 (defun tramp-sh-handle-file-readable-p (filename)
1475 "Like `file-readable-p' for Tramp files."
1476 (with-parsed-tramp-file-name filename nil
1477 (with-tramp-file-property v localname
"file-readable-p"
1478 ;; Examine `file-attributes' cache to see if request can be
1479 ;; satisfied without remote operation.
1480 (or (tramp-check-cached-permissions v ?r
)
1481 (tramp-run-test "-r" filename
)))))
1483 ;; When the remote shell is started, it looks for a shell which groks
1484 ;; tilde expansion. Here, we assume that all shells which grok tilde
1485 ;; expansion will also provide a `test' command which groks `-nt' (for
1486 ;; newer than). If this breaks, tell me about it and I'll try to do
1487 ;; something smarter about it.
1488 (defun tramp-sh-handle-file-newer-than-file-p (file1 file2
)
1489 "Like `file-newer-than-file-p' for Tramp files."
1490 (cond ((not (file-exists-p file1
))
1492 ((not (file-exists-p file2
))
1494 ;; We are sure both files exist at this point.
1497 ;; We try to get the mtime of both files. If they are not
1498 ;; equal to the "dont-know" value, then we subtract the times
1499 ;; and obtain the result.
1500 (let ((fa1 (file-attributes file1
))
1501 (fa2 (file-attributes file2
)))
1502 (if (and (not (equal (nth 5 fa1
) '(0 0)))
1503 (not (equal (nth 5 fa2
) '(0 0))))
1504 (> 0 (tramp-time-diff (nth 5 fa2
) (nth 5 fa1
)))
1505 ;; If one of them is the dont-know value, then we can
1506 ;; still try to run a shell command on the remote host.
1507 ;; However, this only works if both files are Tramp
1508 ;; files and both have the same method, same user, same
1510 (unless (tramp-equal-remote file1 file2
)
1511 (with-parsed-tramp-file-name
1512 (if (tramp-tramp-file-p file1
) file1 file2
) nil
1515 "Files %s and %s must have same method, user, host"
1517 (with-parsed-tramp-file-name file1 nil
1519 (tramp-get-test-nt-command v
) file1 file2
))))))))
1521 ;; Functions implemented using the basic functions above.
1523 (defun tramp-sh-handle-file-directory-p (filename)
1524 "Like `file-directory-p' for Tramp files."
1525 (with-parsed-tramp-file-name filename nil
1526 ;; `file-directory-p' is used as predicate for filename completion.
1527 ;; Sometimes, when a connection is not established yet, it is
1528 ;; desirable to return t immediately for "/method:foo:". It can
1529 ;; be expected that this is always a directory.
1530 (or (zerop (length localname
))
1531 (with-tramp-file-property v localname
"file-directory-p"
1532 (tramp-run-test "-d" filename
)))))
1534 (defun tramp-sh-handle-file-writable-p (filename)
1535 "Like `file-writable-p' for Tramp files."
1536 (with-parsed-tramp-file-name filename nil
1537 (with-tramp-file-property v localname
"file-writable-p"
1538 (if (file-exists-p filename
)
1539 ;; Examine `file-attributes' cache to see if request can be
1540 ;; satisfied without remote operation.
1541 (or (tramp-check-cached-permissions v ?w
)
1542 (tramp-run-test "-w" filename
))
1543 ;; If file doesn't exist, check if directory is writable.
1544 (and (tramp-run-test "-d" (file-name-directory filename
))
1545 (tramp-run-test "-w" (file-name-directory filename
)))))))
1547 (defun tramp-sh-handle-file-ownership-preserved-p (filename &optional group
)
1548 "Like `file-ownership-preserved-p' for Tramp files."
1549 (with-parsed-tramp-file-name filename nil
1550 (with-tramp-file-property v localname
"file-ownership-preserved-p"
1551 (let ((attributes (file-attributes filename
)))
1552 ;; Return t if the file doesn't exist, since it's true that no
1553 ;; information would be lost by an (attempted) delete and create.
1554 (or (null attributes
)
1556 (= (nth 2 attributes
) (tramp-get-remote-uid v
'integer
))
1558 (= (nth 3 attributes
) (tramp-get-remote-gid v
'integer
)))))))))
1560 ;; Directory listings.
1562 (defun tramp-sh-handle-directory-files-and-attributes
1563 (directory &optional full match nosort id-format
)
1564 "Like `directory-files-and-attributes' for Tramp files."
1565 (unless id-format
(setq id-format
'integer
))
1566 (when (file-directory-p directory
)
1567 (setq directory
(expand-file-name directory
))
1570 (with-parsed-tramp-file-name directory nil
1571 (with-tramp-file-property
1573 (format "directory-files-and-attributes-%s" id-format
)
1578 (tramp-convert-file-attributes v
(cdr x
))))
1580 ((tramp-get-remote-stat v
)
1581 (tramp-do-directory-files-and-attributes-with-stat
1582 v localname id-format
))
1583 ((tramp-get-remote-perl v
)
1584 (tramp-do-directory-files-and-attributes-with-perl
1585 v localname id-format
)))))))))
1589 (setq item
(pop temp
))
1590 (when (or (null match
) (string-match match
(car item
)))
1592 (setcar item
(expand-file-name (car item
) directory
)))
1593 (push item result
)))
1597 (sort result
(lambda (x y
) (string< (car x
) (car y
))))))))
1599 (defun tramp-do-directory-files-and-attributes-with-perl
1600 (vec localname
&optional id-format
)
1601 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1602 (tramp-message vec
5 "directory-files-and-attributes with perl: %s" localname
)
1603 (tramp-maybe-send-script
1604 vec tramp-perl-directory-files-and-attributes
1605 "tramp_perl_directory_files_and_attributes")
1607 (tramp-send-command-and-read
1609 (format "tramp_perl_directory_files_and_attributes %s %s"
1610 (tramp-shell-quote-argument localname
) id-format
))))
1611 (when (stringp object
) (tramp-error vec
'file-error object
))
1614 (defun tramp-do-directory-files-and-attributes-with-stat
1615 (vec localname
&optional id-format
)
1616 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1617 (tramp-message vec
5 "directory-files-and-attributes with stat: %s" localname
)
1618 (tramp-send-command-and-read
1622 ;; We must care about filenames with spaces, or starting with
1623 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1624 ;; but it does not work on all remote systems. Therefore, we
1625 ;; quote the filenames via sed.
1626 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | "
1628 "'(\"%%n\" (\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)'"
1629 " 2>/dev/null); echo \")\"")
1630 (tramp-shell-quote-argument localname
)
1631 (tramp-get-ls-command vec
)
1632 (tramp-get-remote-stat vec
)
1633 (if (eq id-format
'integer
) "%ue0" "\"%U\"")
1634 (if (eq id-format
'integer
) "%ge0" "\"%G\""))))
1636 ;; This function should return "foo/" for directories and "bar" for
1638 (defun tramp-sh-handle-file-name-all-completions (filename directory
)
1639 "Like `file-name-all-completions' for Tramp files."
1640 (unless (save-match-data (string-match "/" filename
))
1641 (with-parsed-tramp-file-name (expand-file-name directory
) nil
1648 ;; Try cache entries for filename, filename with last
1649 ;; character removed, filename with last two characters
1650 ;; removed, ..., and finally the empty string - all
1651 ;; concatenated to the local directory name.
1652 (let ((remote-file-name-inhibit-cache
1653 (or remote-file-name-inhibit-cache
1654 tramp-completion-reread-directory-timeout
)))
1656 ;; This is inefficient for very long filenames, pity
1657 ;; `reduce' is not available...
1664 (tramp-get-file-property
1666 (concat localname
(substring filename
0 x
))
1667 "file-name-all-completions"
1669 (when cache-hit
(list cache-hit
))))
1670 ;; We cannot use a length of 0, because file properties
1671 ;; for "foo" and "foo/" are identical.
1672 (tramp-compat-number-sequence (length filename
) 1 -
1)))))
1674 ;; Cache expired or no matching cache entry found so we need
1675 ;; to perform a remote operation.
1677 ;; Get a list of directories and files, including reliably
1678 ;; tagging the directories with a trailing '/'. Because I
1679 ;; rock. --daniel@danann.net
1681 ;; Changed to perform `cd' in the same remote op and only
1682 ;; get entries starting with `filename'. Capture any `cd'
1683 ;; error messages. Ensure any `cd' and `echo' aliases are
1687 (if (tramp-get-remote-perl v
)
1689 (tramp-maybe-send-script
1690 v tramp-perl-file-name-all-completions
1691 "tramp_perl_file_name_all_completions")
1692 (format "tramp_perl_file_name_all_completions %s %s %d"
1693 (tramp-shell-quote-argument localname
)
1694 (tramp-shell-quote-argument filename
)
1696 ;; `read-file-name-completion-ignore-case'
1697 ;; is introduced with Emacs 22.1.
1699 'read-file-name-completion-ignore-case
)
1700 'read-file-name-completion-ignore-case
1701 'completion-ignore-case
))
1705 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
1706 ;; `ls' with wildcard might fail with `Argument
1707 ;; list too long' error in some corner cases; if
1708 ;; `ls' fails after `cd' succeeded, chances are
1709 ;; that's the case, so let's retry without
1710 ;; wildcard. This will return "too many" entries
1711 ;; but that isn't harmful.
1712 " || %s -a 2>/dev/null)"
1713 " | while read f; do"
1714 " if %s -d \"$f\" 2>/dev/null;"
1715 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1716 " && \\echo ok) || \\echo fail")
1717 (tramp-shell-quote-argument localname
)
1718 (tramp-get-ls-command v
)
1719 ;; When `filename' is empty, just `ls' without
1720 ;; filename argument is more efficient than `ls *'
1721 ;; for very large directories and might avoid the
1722 ;; `Argument list too long' error.
1724 ;; With and only with wildcard, we need to add
1725 ;; `-d' to prevent `ls' from descending into
1727 (if (zerop (length filename
))
1729 (concat (tramp-shell-quote-argument filename
) "* -d"))
1730 (tramp-get-ls-command v
)
1731 (tramp-get-test-command v
))))
1733 ;; Now grab the output.
1734 (with-current-buffer (tramp-get-buffer v
)
1735 (goto-char (point-max))
1737 ;; Check result code, found in last line of output.
1739 (if (looking-at "^fail$")
1741 ;; Grab error message from line before last line
1742 ;; (it was put there by `cd 2>&1').
1746 "tramp-sh-handle-file-name-all-completions: %s"
1747 (buffer-substring (point) (point-at-eol))))
1748 ;; For peace of mind, if buffer doesn't end in `fail'
1749 ;; then it should end in `ok'. If neither are in the
1750 ;; buffer something went seriously wrong on the remote
1752 (unless (looking-at "^ok$")
1756 tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
1757 (tramp-shell-quote-argument localname
) (buffer-string))))
1759 (while (zerop (forward-line -
1))
1760 (push (buffer-substring (point) (point-at-eol)) result
)))
1762 ;; Because the remote op went through OK we know the
1763 ;; directory we `cd'-ed to exists.
1764 (tramp-set-file-property v localname
"file-exists-p" t
)
1766 ;; Because the remote op went through OK we know every
1767 ;; file listed by `ls' exists.
1768 (mapc (lambda (entry)
1769 (tramp-set-file-property
1770 v
(concat localname entry
) "file-exists-p" t
))
1773 ;; Store result in the cache.
1774 (tramp-set-file-property
1775 v
(concat localname filename
)
1776 "file-name-all-completions" result
))))))))
1780 (defun tramp-sh-handle-add-name-to-file
1781 (filename newname
&optional ok-if-already-exists
)
1782 "Like `add-name-to-file' for Tramp files."
1783 (unless (tramp-equal-remote filename newname
)
1784 (with-parsed-tramp-file-name
1785 (if (tramp-tramp-file-p filename
) filename newname
) nil
1788 "add-name-to-file: %s"
1789 "only implemented for same method, same user, same host")))
1790 (with-parsed-tramp-file-name filename v1
1791 (with-parsed-tramp-file-name newname v2
1792 (let ((ln (when v1
(tramp-get-remote-ln v1
))))
1793 (when (and (numberp ok-if-already-exists
)
1794 (file-exists-p newname
)
1797 "File %s already exists; make it a new name anyway? "
1800 v2
'file-error
"add-name-to-file: file %s already exists" newname
))
1801 (when ok-if-already-exists
(setq ln
(concat ln
" -f")))
1802 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
1803 (tramp-flush-file-property v2 v2-localname
)
1804 (tramp-barf-unless-okay
1806 (format "%s %s %s" ln
1807 (tramp-shell-quote-argument v1-localname
)
1808 (tramp-shell-quote-argument v2-localname
))
1809 "error with add-name-to-file, see buffer `%s' for details"
1812 (defun tramp-sh-handle-copy-file
1813 (filename newname
&optional ok-if-already-exists keep-date
1814 preserve-uid-gid preserve-extended-attributes
)
1815 "Like `copy-file' for Tramp files."
1816 (setq filename
(expand-file-name filename
))
1817 (setq newname
(expand-file-name newname
))
1819 ;; At least one file a Tramp file?
1820 ((or (tramp-tramp-file-p filename
)
1821 (tramp-tramp-file-p newname
))
1822 (tramp-do-copy-or-rename-file
1823 'copy filename newname ok-if-already-exists keep-date
1824 preserve-uid-gid preserve-extended-attributes
))
1826 (preserve-extended-attributes
1827 (tramp-run-real-handler
1829 (list filename newname ok-if-already-exists keep-date
1830 preserve-uid-gid preserve-extended-attributes
)))
1832 (tramp-run-real-handler
1834 (list filename newname ok-if-already-exists keep-date preserve-uid-gid
)))
1836 (tramp-run-real-handler
1837 'copy-file
(list filename newname ok-if-already-exists keep-date
)))))
1839 (defun tramp-sh-handle-copy-directory
1840 (dirname newname
&optional keep-date parents copy-contents
)
1841 "Like `copy-directory' for Tramp files."
1842 (let ((t1 (tramp-tramp-file-p dirname
))
1843 (t2 (tramp-tramp-file-p newname
)))
1844 (with-parsed-tramp-file-name (if t1 dirname newname
) nil
1845 (if (and (not copy-contents
)
1846 (tramp-get-method-parameter method
'tramp-copy-recursive
)
1847 ;; When DIRNAME and NEWNAME are remote, they must have
1849 (or (null t1
) (null t2
)
1851 (tramp-file-name-method (tramp-dissect-file-name dirname
))
1852 (tramp-file-name-method
1853 (tramp-dissect-file-name newname
)))))
1854 ;; scp or rsync DTRT.
1856 (setq dirname
(directory-file-name (expand-file-name dirname
))
1857 newname
(directory-file-name (expand-file-name newname
)))
1858 (if (and (file-directory-p newname
)
1859 (not (string-equal (file-name-nondirectory dirname
)
1860 (file-name-nondirectory newname
))))
1863 (file-name-nondirectory dirname
) newname
)))
1864 (if (not (file-directory-p (file-name-directory newname
)))
1865 (make-directory (file-name-directory newname
) parents
))
1866 (tramp-do-copy-or-rename-file-out-of-band
1867 'copy dirname newname keep-date
))
1868 ;; We must do it file-wise.
1869 (tramp-run-real-handler
1872 (list dirname newname keep-date parents copy-contents
)
1873 (list dirname newname keep-date parents
))))
1875 ;; When newname did exist, we have wrong cached values.
1877 (with-parsed-tramp-file-name newname nil
1878 (tramp-flush-file-property v
(file-name-directory localname
))
1879 (tramp-flush-file-property v localname
))))))
1881 (defun tramp-sh-handle-rename-file
1882 (filename newname
&optional ok-if-already-exists
)
1883 "Like `rename-file' for Tramp files."
1884 ;; Check if both files are local -- invoke normal rename-file.
1885 ;; Otherwise, use Tramp from local system.
1886 (setq filename
(expand-file-name filename
))
1887 (setq newname
(expand-file-name newname
))
1888 ;; At least one file a Tramp file?
1889 (if (or (tramp-tramp-file-p filename
)
1890 (tramp-tramp-file-p newname
))
1891 (tramp-do-copy-or-rename-file
1892 'rename filename newname ok-if-already-exists t t
)
1893 (tramp-run-real-handler
1894 'rename-file
(list filename newname ok-if-already-exists
))))
1896 (defun tramp-do-copy-or-rename-file
1897 (op filename newname
&optional ok-if-already-exists keep-date
1898 preserve-uid-gid preserve-extended-attributes
)
1899 "Copy or rename a remote file.
1900 OP must be `copy' or `rename' and indicates the operation to perform.
1901 FILENAME specifies the file to copy or rename, NEWNAME is the name of
1902 the new file (for copy) or the new name of the file (for rename).
1903 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
1904 KEEP-DATE means to make sure that NEWNAME has the same timestamp
1905 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
1906 the uid and gid if both files are on the same host.
1907 PRESERVE-EXTENDED-ATTRIBUTES activates selinux and acl commands.
1909 This function is invoked by `tramp-sh-handle-copy-file' and
1910 `tramp-sh-handle-rename-file'. It is an error if OP is neither
1911 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
1913 (unless (memq op
'(copy rename
))
1914 (error "Unknown operation `%s', must be `copy' or `rename'" op
))
1915 (let ((t1 (tramp-tramp-file-p filename
))
1916 (t2 (tramp-tramp-file-p newname
))
1917 (length (nth 7 (file-attributes (file-truename filename
))))
1918 (attributes (and preserve-extended-attributes
1919 (apply 'file-extended-attributes
(list filename
)))))
1921 (with-parsed-tramp-file-name (if t1 filename newname
) nil
1922 (when (and (not ok-if-already-exists
) (file-exists-p newname
))
1924 v
'file-already-exists
"File %s already exists" newname
))
1926 (with-tramp-progress-reporter
1927 v
0 (format "%s %s to %s"
1928 (if (eq op
'copy
) "Copying" "Renaming")
1932 ;; Both are Tramp files.
1934 (with-parsed-tramp-file-name filename v1
1935 (with-parsed-tramp-file-name newname v2
1937 ;; Shortcut: if method, host, user are the same for
1938 ;; both files, we invoke `cp' or `mv' on the remote
1940 ((tramp-equal-remote filename newname
)
1941 (tramp-do-copy-or-rename-file-directly
1943 ok-if-already-exists keep-date preserve-uid-gid
))
1945 ;; Try out-of-band operation.
1947 (tramp-method-out-of-band-p v1 length
)
1948 (tramp-method-out-of-band-p v2 length
))
1949 (tramp-do-copy-or-rename-file-out-of-band
1950 op filename newname keep-date
))
1952 ;; No shortcut was possible. So we copy the file
1953 ;; first. If the operation was `rename', we go back
1954 ;; and delete the original file (if the copy was
1955 ;; successful). The approach is simple-minded: we
1956 ;; create a new buffer, insert the contents of the
1957 ;; source file into it, then write out the buffer to
1958 ;; the target file. The advantage is that it doesn't
1959 ;; matter which filename handlers are used for the
1960 ;; source and target file.
1962 (tramp-do-copy-or-rename-file-via-buffer
1963 op filename newname keep-date
))))))
1965 ;; One file is a Tramp file, the other one is local.
1968 ;; Fast track on local machine.
1969 ((tramp-local-host-p v
)
1970 (tramp-do-copy-or-rename-file-directly
1972 ok-if-already-exists keep-date preserve-uid-gid
))
1974 ;; If the Tramp file has an out-of-band method, the
1975 ;; corresponding copy-program can be invoked.
1976 ((tramp-method-out-of-band-p v length
)
1977 (tramp-do-copy-or-rename-file-out-of-band
1978 op filename newname keep-date
))
1980 ;; Use the inline method via a Tramp buffer.
1981 (t (tramp-do-copy-or-rename-file-via-buffer
1982 op filename newname keep-date
))))
1985 ;; One of them must be a Tramp file.
1986 (error "Tramp implementation says this cannot happen")))
1988 ;; Handle `preserve-extended-attributes'. We ignore possible
1989 ;; errors, because ACL strings could be incompatible.
1992 (apply 'set-file-extended-attributes
(list newname attributes
))))
1994 ;; In case of `rename', we must flush the cache of the source file.
1995 (when (and t1
(eq op
'rename
))
1996 (with-parsed-tramp-file-name filename v1
1997 (tramp-flush-file-property v1
(file-name-directory v1-localname
))
1998 (tramp-flush-file-property v1 v1-localname
)))
2000 ;; When newname did exist, we have wrong cached values.
2002 (with-parsed-tramp-file-name newname v2
2003 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
2004 (tramp-flush-file-property v2 v2-localname
)))))))
2006 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date
)
2007 "Use an Emacs buffer to copy or rename a file.
2008 First arg OP is either `copy' or `rename' and indicates the operation.
2009 FILENAME is the source file, NEWNAME the target file.
2010 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2012 ;; We must disable multibyte, because binary data shall not be
2014 (set-buffer-multibyte nil
)
2015 (let ((coding-system-for-read 'binary
)
2016 (jka-compr-inhibit t
))
2017 (insert-file-contents-literally filename
))
2018 ;; We don't want the target file to be compressed, so we let-bind
2019 ;; `jka-compr-inhibit' to t.
2020 (let ((coding-system-for-write 'binary
)
2021 (jka-compr-inhibit t
))
2022 (write-region (point-min) (point-max) newname nil
'no-message
)))
2023 ;; KEEP-DATE handling.
2024 (when keep-date
(set-file-times newname
(nth 5 (file-attributes filename
))))
2026 (set-file-modes newname
(tramp-default-file-modes filename
))
2027 ;; If the operation was `rename', delete the original file.
2028 (unless (eq op
'copy
) (delete-file filename
)))
2030 (defun tramp-do-copy-or-rename-file-directly
2031 (op filename newname ok-if-already-exists keep-date preserve-uid-gid
)
2032 "Invokes `cp' or `mv' on the remote system.
2033 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2034 respectively. FILENAME specifies the file to copy or rename,
2035 NEWNAME is the name of the new file (for copy) or the new name of
2036 the file (for rename). Both files must reside on the same host.
2037 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2038 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2039 the uid and gid from FILENAME."
2040 (let ((t1 (tramp-tramp-file-p filename
))
2041 (t2 (tramp-tramp-file-p newname
))
2042 (file-times (nth 5 (file-attributes filename
)))
2043 (file-modes (tramp-default-file-modes filename
)))
2044 (with-parsed-tramp-file-name (if t1 filename newname
) nil
2045 (let* ((cmd (cond ((and (eq op
'copy
) preserve-uid-gid
) "cp -f -p")
2046 ((eq op
'copy
) "cp -f")
2047 ((eq op
'rename
) "mv -f")
2050 "Unknown operation `%s', must be `copy' or `rename'"
2054 (tramp-file-name-handler 'file-remote-p filename
'localname
)
2058 (tramp-file-name-handler 'file-remote-p newname
'localname
)
2060 (prefix (file-remote-p (if t1 filename newname
)))
2064 ;; Both files are on a remote host, with same user.
2067 (tramp-send-command-and-check
2068 v
(format "%s %s %s" cmd
2069 (tramp-shell-quote-argument localname1
)
2070 (tramp-shell-quote-argument localname2
))))
2071 (with-current-buffer (tramp-get-buffer v
)
2072 (goto-char (point-min))
2076 ;; Mask cp -f error.
2078 tramp-operation-not-permitted-regexp nil t
))
2080 (tramp-error-with-buffer
2082 "Copying directly failed, see buffer `%s' for details."
2085 ;; We are on the local host.
2088 ;; We can do it directly.
2089 ((let (file-name-handler-alist)
2090 (and (file-readable-p localname1
)
2091 (file-writable-p (file-name-directory localname2
))
2092 (or (file-directory-p localname2
)
2093 (file-writable-p localname2
))))
2095 (tramp-compat-copy-file
2096 localname1 localname2 ok-if-already-exists
2097 keep-date preserve-uid-gid
)
2098 (tramp-run-real-handler
2099 'rename-file
(list localname1 localname2 ok-if-already-exists
))))
2101 ;; We can do it directly with `tramp-send-command'
2102 ((and (file-readable-p (concat prefix localname1
))
2104 (file-name-directory (concat prefix localname2
)))
2105 (or (file-directory-p (concat prefix localname2
))
2106 (file-writable-p (concat prefix localname2
))))
2107 (tramp-do-copy-or-rename-file-directly
2108 op
(concat prefix localname1
) (concat prefix localname2
)
2109 ok-if-already-exists keep-date t
)
2110 ;; We must change the ownership to the local user.
2111 (tramp-set-file-uid-gid
2112 (concat prefix localname2
)
2113 (tramp-get-local-uid 'integer
)
2114 (tramp-get-local-gid 'integer
)))
2116 ;; We need a temporary file in between.
2118 ;; Create the temporary file.
2119 (let ((tmpfile (tramp-compat-make-temp-file localname1
)))
2124 (tramp-barf-unless-okay
2127 (tramp-shell-quote-argument localname1
)
2128 (tramp-shell-quote-argument tmpfile
))
2129 "Copying directly failed, see buffer `%s' for details."
2130 (tramp-get-buffer v
))
2131 ;; We must change the ownership as remote user.
2132 ;; Since this does not work reliable, we also
2133 ;; give read permissions.
2135 (concat prefix tmpfile
)
2136 (tramp-compat-octal-to-decimal "0777"))
2137 (tramp-set-file-uid-gid
2138 (concat prefix tmpfile
)
2139 (tramp-get-local-uid 'integer
)
2140 (tramp-get-local-gid 'integer
)))
2143 (tramp-compat-copy-file
2144 localname1 tmpfile t
2145 keep-date preserve-uid-gid
)
2146 (tramp-run-real-handler
2148 (list localname1 tmpfile t
)))
2149 ;; We must change the ownership as local user.
2150 ;; Since this does not work reliable, we also
2151 ;; give read permissions.
2153 tmpfile
(tramp-compat-octal-to-decimal "0777"))
2154 (tramp-set-file-uid-gid
2156 (tramp-get-remote-uid v
'integer
)
2157 (tramp-get-remote-gid v
'integer
))))
2159 ;; Move the temporary file to its destination.
2162 (tramp-barf-unless-okay
2165 (tramp-shell-quote-argument tmpfile
)
2166 (tramp-shell-quote-argument localname2
))
2167 "Copying directly failed, see buffer `%s' for details."
2168 (tramp-get-buffer v
)))
2170 (tramp-run-real-handler
2172 (list tmpfile localname2 ok-if-already-exists
)))))
2175 (ignore-errors (delete-file tmpfile
)))))))))
2177 ;; Set the time and mode. Mask possible errors.
2180 (set-file-times newname file-times
)
2181 (set-file-modes newname file-modes
))))))
2183 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date
)
2184 "Invoke rcp program to copy.
2185 The method used must be an out-of-band method."
2186 (let* ((t1 (tramp-tramp-file-p filename
))
2187 (t2 (tramp-tramp-file-p newname
))
2188 (orig-vec (tramp-dissect-file-name (if t1 filename newname
)))
2189 copy-program copy-args copy-env copy-keep-date port spec
2190 options source target
)
2192 (with-parsed-tramp-file-name (if t1 filename newname
) nil
2195 ;; Both are Tramp files. We shall optimize it when the
2196 ;; methods for filename and newname are the same.
2197 (let* ((dir-flag (file-directory-p filename
))
2198 (tmpfile (tramp-compat-make-temp-file localname dir-flag
)))
2202 (file-name-nondirectory newname
) tmpfile
)))
2205 (tramp-do-copy-or-rename-file-out-of-band
2206 op filename tmpfile keep-date
)
2207 (tramp-do-copy-or-rename-file-out-of-band
2208 'rename tmpfile newname keep-date
))
2212 (tramp-compat-delete-directory
2213 (expand-file-name ".." tmpfile
) 'recursive
)
2214 (delete-file tmpfile
)))))
2216 ;; Set variables for computing the prompt for reading
2218 (setq tramp-current-method
(tramp-file-name-method v
)
2219 tramp-current-user
(or (tramp-file-name-user v
)
2220 (tramp-get-connection-property
2222 tramp-current-host
(tramp-file-name-real-host v
))
2224 ;; Expand hops. Might be necessary for gateway methods.
2225 (setq v
(car (tramp-compute-multi-hops v
)))
2226 (aset v
3 localname
)
2228 ;; Check which ones of source and target are Tramp files.
2229 (setq source
(if t1
(tramp-make-copy-program-file-name v
) filename
)
2231 (if (and (file-directory-p filename
)
2233 (file-name-nondirectory filename
)
2234 (file-name-nondirectory newname
)))
2235 'file-name-directory
2237 (if t2
(tramp-make-copy-program-file-name v
) newname
)))
2239 ;; Check for host and port number. We cannot use
2240 ;; `tramp-file-name-port', because this returns also
2241 ;; `tramp-default-port', which might clash with settings in
2243 (setq host
(tramp-file-name-host v
)
2245 (when (string-match tramp-host-with-port-regexp host
)
2246 (setq port
(string-to-number (match-string 2 host
))
2247 host
(string-to-number (match-string 1 host
))))
2249 ;; Check for user. There might be an interactive setting.
2250 (setq user
(or (tramp-file-name-user v
)
2251 (tramp-get-connection-property v
"login-as" nil
)))
2253 ;; Compose copy command.
2254 (setq host
(or host
"")
2257 spec
(format-spec-make
2258 ?t
(tramp-get-connection-property
2259 (tramp-get-connection-process v
) "temp-file" ""))
2260 options
(format-spec
2261 (if tramp-use-ssh-controlmaster-options
2262 tramp-ssh-controlmaster-options
"")
2264 spec
(format-spec-make
2265 ?h host ?u user ?p port ?c options
2266 ?k
(if keep-date
" " ""))
2267 copy-program
(tramp-get-method-parameter
2268 method
'tramp-copy-program
)
2269 copy-keep-date
(tramp-get-method-parameter
2270 method
'tramp-copy-keep-date
)
2273 ;; " " has either been a replacement of "%k" (when
2274 ;; keep-date argument is non-nil), or a replacement
2275 ;; for the whole keep-date sublist.
2279 (tramp-get-method-parameter method
'tramp-copy-args
)
2284 (let ((y (mapcar (lambda (z) (format-spec z spec
)) x
)))
2285 (if (member "" y
) '(" ") y
))))))
2291 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
2292 (unless (member "" x
) (mapconcat 'identity x
" ")))
2293 (tramp-get-method-parameter method
'tramp-copy-env
))))
2295 ;; Check for program.
2296 (unless (executable-find copy-program
)
2298 v
'file-error
"Cannot find copy program: %s" copy-program
))
2302 ;; The default directory must be remote.
2303 (let ((default-directory
2304 (file-name-directory (if t1 filename newname
)))
2305 (process-environment (copy-sequence process-environment
)))
2306 ;; Set the transfer process properties.
2307 (tramp-set-connection-property
2308 v
"process-name" (buffer-name (current-buffer)))
2309 (tramp-set-connection-property
2310 v
"process-buffer" (current-buffer))
2313 orig-vec
6 "%s=\"%s\"" (car copy-env
) (cadr copy-env
))
2314 (setenv (pop copy-env
) (pop copy-env
)))
2316 ;; Use an asynchronous process. By this, password can
2317 ;; be handled. The default directory must be local, in
2318 ;; order to apply the correct `copy-program'. We don't
2319 ;; set a timeout, because the copying of large files can
2320 ;; last longer than 60 secs.
2321 (let ((p (let ((default-directory
2322 (tramp-compat-temporary-file-directory)))
2323 (apply 'start-process-shell-command
2324 (tramp-get-connection-name v
)
2325 (tramp-get-connection-buffer v
)
2330 (shell-quote-argument source
)
2331 (shell-quote-argument target
)
2332 "&&" "echo" "tramp_exit_status" "0"
2333 "||" "echo" "tramp_exit_status" "1"))))))
2336 (mapconcat 'identity
(process-command p
) " "))
2337 (tramp-set-connection-property p
"vector" orig-vec
)
2338 (tramp-compat-set-process-query-on-exit-flag p nil
)
2339 (tramp-process-actions
2340 p v nil tramp-actions-copy-out-of-band
)
2342 ;; Check the return code.
2343 (goto-char (point-max))
2345 (re-search-backward "tramp_exit_status [0-9]+" nil t
)
2347 orig-vec
'file-error
2348 "Couldn't find exit status of `%s'"
2349 (mapconcat 'identity
(process-command p
) " ")))
2350 (skip-chars-forward "^ ")
2351 (unless (zerop (read (current-buffer)))
2354 orig-vec
'file-error
2355 "Error copying: `%s'"
2356 (buffer-substring (point-min) (point-at-eol))))))
2358 ;; Reset the transfer process properties.
2359 (tramp-message orig-vec
6 "\n%s" (buffer-string))
2360 (tramp-set-connection-property v
"process-name" nil
)
2361 (tramp-set-connection-property v
"process-buffer" nil
)))
2363 ;; Handle KEEP-DATE argument.
2364 (when (and keep-date
(not copy-keep-date
))
2365 (set-file-times newname
(nth 5 (file-attributes filename
))))
2368 (unless (and keep-date copy-keep-date
)
2370 (set-file-modes newname
(tramp-default-file-modes filename
)))))
2372 ;; If the operation was `rename', delete the original file.
2373 (unless (eq op
'copy
)
2374 (if (file-regular-p filename
)
2375 (delete-file filename
)
2376 (tramp-compat-delete-directory filename
'recursive
))))))
2378 (defun tramp-sh-handle-make-directory (dir &optional parents
)
2379 "Like `make-directory' for Tramp files."
2380 (setq dir
(expand-file-name dir
))
2381 (with-parsed-tramp-file-name dir nil
2382 (tramp-flush-directory-property v
(file-name-directory localname
))
2384 (tramp-barf-unless-okay
2386 (if parents
"mkdir -p" "mkdir")
2387 (tramp-shell-quote-argument localname
))
2388 "Couldn't make directory %s" dir
))))
2390 (defun tramp-sh-handle-delete-directory (directory &optional recursive
)
2391 "Like `delete-directory' for Tramp files."
2392 (setq directory
(expand-file-name directory
))
2393 (with-parsed-tramp-file-name directory nil
2394 (tramp-flush-file-property v
(file-name-directory localname
))
2395 (tramp-flush-directory-property v localname
)
2396 (tramp-barf-unless-okay
2398 (if recursive
"rm -rf" "rmdir")
2399 (tramp-shell-quote-argument localname
))
2400 "Couldn't delete %s" directory
)))
2402 (defun tramp-sh-handle-delete-file (filename &optional trash
)
2403 "Like `delete-file' for Tramp files."
2404 (setq filename
(expand-file-name filename
))
2405 (with-parsed-tramp-file-name filename nil
2406 (tramp-flush-file-property v
(file-name-directory localname
))
2407 (tramp-flush-file-property v localname
)
2408 (tramp-barf-unless-okay
2410 (or (and trash
(tramp-get-remote-trash v
)) "rm -f")
2411 (tramp-shell-quote-argument localname
))
2412 "Couldn't delete %s" filename
)))
2416 ;; CCC: This does not seem to be enough. Something dies when
2417 ;; we try and delete two directories under Tramp :/
2418 (defun tramp-sh-handle-dired-recursive-delete-directory (filename)
2419 "Recursively delete the directory given.
2420 This is like `dired-recursive-delete-directory' for Tramp files."
2421 (with-parsed-tramp-file-name filename nil
2422 ;; Run a shell command 'rm -r <localname>'.
2423 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
2424 (unless (file-exists-p filename
)
2425 (tramp-error v
'file-error
"No such directory: %s" filename
))
2426 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>).
2429 (format "rm -rf %s" (tramp-shell-quote-argument localname
))
2430 ;; Don't read the output, do it explicitly.
2432 ;; Wait for the remote system to return to us...
2433 ;; This might take a while, allow it plenty of time.
2434 (tramp-wait-for-output (tramp-get-connection-process v
) 120)
2435 ;; Make sure that it worked...
2436 (tramp-flush-file-property v
(file-name-directory localname
))
2437 (tramp-flush-directory-property v localname
)
2438 (and (file-exists-p filename
)
2440 v
'file-error
"Failed to recursively delete %s" filename
))))
2442 (defun tramp-sh-handle-dired-compress-file (file &rest _ok-flag
)
2443 "Like `dired-compress-file' for Tramp files."
2444 ;; OK-FLAG is valid for XEmacs only, but not implemented.
2445 ;; Code stolen mainly from dired-aux.el.
2446 (with-parsed-tramp-file-name file nil
2447 (tramp-flush-file-property v localname
)
2450 (if (not (featurep 'xemacs
))
2452 (symbol-value 'dired-compress-file-suffixes
)
2453 ;; XEmacs has `dired-compression-method-alist', which is
2454 ;; transformed into `dired-compress-file-suffixes' structure.
2457 (list (concat (regexp-quote (nth 1 x
)) "\\'")
2459 (mapconcat 'identity
(nth 3 x
) " ")))
2460 (symbol-value 'dired-compression-method-alist
))))
2462 ;; See if any suffix rule matches this file name.
2464 (let (case-fold-search)
2465 (if (string-match (car (car suffixes
)) localname
)
2466 (setq suffix
(car suffixes
) suffixes nil
))
2467 (setq suffixes
(cdr suffixes
))))
2469 (cond ((file-symlink-p file
)
2471 ((and suffix
(nth 2 suffix
))
2472 ;; We found an uncompression rule.
2473 (with-tramp-progress-reporter
2474 v
0 (format "Uncompressing %s" file
)
2475 (when (tramp-send-command-and-check
2476 v
(concat (nth 2 suffix
) " "
2477 (tramp-shell-quote-argument localname
)))
2478 ;; `dired-remove-file' is not defined in XEmacs.
2479 (tramp-compat-funcall 'dired-remove-file file
)
2480 (string-match (car suffix
) file
)
2481 (concat (substring file
0 (match-beginning 0))))))
2483 ;; We don't recognize the file as compressed, so compress it.
2485 (with-tramp-progress-reporter v
0 (format "Compressing %s" file
)
2486 (when (tramp-send-command-and-check
2487 v
(concat "gzip -f "
2488 (tramp-shell-quote-argument localname
)))
2489 ;; `dired-remove-file' is not defined in XEmacs.
2490 (tramp-compat-funcall 'dired-remove-file file
)
2491 (cond ((file-exists-p (concat file
".gz"))
2492 (concat file
".gz"))
2493 ((file-exists-p (concat file
".z"))
2497 (defun tramp-sh-handle-insert-directory
2498 (filename switches
&optional wildcard full-directory-p
)
2499 "Like `insert-directory' for Tramp files."
2500 (setq filename
(expand-file-name filename
))
2501 (unless switches
(setq switches
""))
2502 (with-parsed-tramp-file-name filename nil
2503 (if (and (featurep 'ls-lisp
)
2504 (not (symbol-value 'ls-lisp-use-insert-directory-program
)))
2505 (tramp-run-real-handler
2506 'insert-directory
(list filename switches wildcard full-directory-p
))
2507 (when (stringp switches
)
2508 (setq switches
(split-string switches
)))
2509 (when (and (member "--dired" switches
)
2510 (not (tramp-get-ls-command-with-dired v
)))
2511 (setq switches
(delete "--dired" switches
)))
2513 (setq wildcard
(tramp-run-real-handler
2514 'file-name-nondirectory
(list localname
)))
2515 (setq localname
(tramp-run-real-handler
2516 'file-name-directory
(list localname
))))
2517 (unless (or full-directory-p
(member "-d" switches
))
2518 (setq switches
(append switches
'("-d"))))
2519 (setq switches
(mapconcat 'tramp-shell-quote-argument switches
" "))
2521 (setq switches
(concat switches
" " wildcard
)))
2523 v
4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2524 switches filename
(if wildcard
"yes" "no")
2525 (if full-directory-p
"yes" "no"))
2526 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2527 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2528 (if full-directory-p
2531 (format "%s %s %s 2>/dev/null"
2532 (tramp-get-ls-command v
)
2536 (tramp-shell-quote-argument (concat localname
".")))))
2537 (tramp-barf-unless-okay
2539 (format "cd %s" (tramp-shell-quote-argument
2540 (tramp-run-real-handler
2541 'file-name-directory
(list localname
))))
2543 (tramp-shell-quote-argument
2544 (tramp-run-real-handler 'file-name-directory
(list localname
))))
2547 (format "%s %s %s 2>/dev/null"
2548 (tramp-get-ls-command v
)
2552 (tramp-run-real-handler
2553 'file-name-nondirectory
(list localname
)))))
2555 (tramp-shell-quote-argument
2556 (tramp-run-real-handler
2557 'file-name-nondirectory
(list localname
)))))))
2560 (let ((beg (point)))
2561 (narrow-to-region (point) (point))
2562 ;; We cannot use `insert-buffer-substring' because the Tramp
2563 ;; buffer changes its contents before insertion due to calling
2564 ;; `expand-file' and alike.
2566 (with-current-buffer (tramp-get-buffer v
)
2569 ;; Check for "--dired" output.
2571 (when (looking-at "//SUBDIRED//")
2573 (when (looking-at "//DIRED//\\s-+")
2574 (let ((databeg (match-end 0))
2575 (end (point-at-eol)))
2576 ;; Now read the numeric positions of file names.
2578 (while (< (point) end
)
2579 (let ((start (+ beg
(read (current-buffer))))
2580 (end (+ beg
(read (current-buffer)))))
2581 (if (memq (char-after end
) '(?
\n ?\
))
2582 ;; End is followed by \n or by " -> ".
2583 (put-text-property start end
'dired-filename t
))))))
2584 ;; Remove trailing lines.
2585 (goto-char (point-at-bol))
2586 (while (looking-at "//")
2588 (delete-region (match-beginning 0) (point)))
2590 ;; Some busyboxes are reluctant to discard colors.
2591 (unless (string-match "color" (tramp-get-connection-property v
"ls" ""))
2593 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
2594 (replace-match "")))
2596 ;; Decode the output, it could be multibyte.
2597 (decode-coding-region
2599 (or file-name-coding-system
2600 (and (boundp 'default-file-name-coding-system
)
2601 (symbol-value 'default-file-name-coding-system
))))
2603 ;; The inserted file could be from somewhere else.
2604 (when (and (not wildcard
) (not full-directory-p
))
2605 (goto-char (point-max))
2606 (when (file-symlink-p filename
)
2607 (goto-char (search-backward "->" beg
'noerror
)))
2609 (if (zerop (length (file-name-nondirectory filename
)))
2611 (file-name-nondirectory filename
))
2613 (replace-match (file-relative-name filename
) t
))
2615 (goto-char (point-max)))))))
2617 ;; Canonicalization of file names.
2619 (defun tramp-sh-handle-expand-file-name (name &optional dir
)
2620 "Like `expand-file-name' for Tramp files.
2621 If the localname part of the given filename starts with \"/../\" then
2622 the result will be a local, non-Tramp, filename."
2623 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
2624 (setq dir
(or dir default-directory
"/"))
2625 ;; Unless NAME is absolute, concat DIR and NAME.
2626 (unless (file-name-absolute-p name
)
2627 (setq name
(concat (file-name-as-directory dir
) name
)))
2628 ;; If NAME is not a Tramp file, run the real handler.
2629 (if (not (tramp-connectable-p name
))
2630 (tramp-run-real-handler 'expand-file-name
(list name nil
))
2632 (with-parsed-tramp-file-name name nil
2633 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
2634 (setq localname
(concat "~/" localname
)))
2635 ;; Tilde expansion if necessary. This needs a shell which
2636 ;; groks tilde expansion! The function `tramp-find-shell' is
2637 ;; supposed to find such a shell on the remote host. Please
2638 ;; tell me about it when this doesn't work on your system.
2639 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname
)
2640 (let ((uname (match-string 1 localname
))
2641 (fname (match-string 2 localname
)))
2642 ;; We cannot simply apply "~/", because under sudo "~/" is
2643 ;; expanded to the local user home directory but to the
2644 ;; root home directory. On the other hand, using always
2645 ;; the default user name for tilde expansion is not
2646 ;; appropriate either, because ssh and companions might
2647 ;; use a user name from the config file.
2648 (when (and (string-equal uname
"~")
2649 (string-match "\\`su\\(do\\)?\\'" method
))
2650 (setq uname
(concat uname user
)))
2652 (with-tramp-connection-property v uname
2654 v
(format "cd %s; pwd" (tramp-shell-quote-argument uname
)))
2655 (with-current-buffer (tramp-get-buffer v
)
2656 (goto-char (point-min))
2657 (buffer-substring (point) (point-at-eol)))))
2658 (setq localname
(concat uname fname
))))
2659 ;; There might be a double slash, for example when "~/"
2660 ;; expands to "/". Remove this.
2661 (while (string-match "//" localname
)
2662 (setq localname
(replace-match "/" t t localname
)))
2663 ;; No tilde characters in file name, do normal
2664 ;; `expand-file-name' (this does "/./" and "/../"). We bind
2665 ;; `directory-sep-char' here for XEmacs on Windows, which would
2666 ;; otherwise use backslash. `default-directory' is bound,
2667 ;; because on Windows there would be problems with UNC shares or
2669 (let ((directory-sep-char ?
/)
2670 (default-directory (tramp-compat-temporary-file-directory)))
2671 (tramp-make-tramp-file-name
2673 (tramp-drop-volume-letter
2674 (tramp-run-real-handler
2675 'expand-file-name
(list localname
)))
2678 ;;; Remote commands:
2680 (defun tramp-process-sentinel (proc event
)
2681 "Flush file caches."
2682 (unless (memq (process-status proc
) '(run open
))
2683 (let ((vec (tramp-get-connection-property proc
"vector" nil
)))
2685 (tramp-message vec
5 "Sentinel called: `%S' `%s'" proc event
)
2686 (tramp-flush-connection-property proc
)
2687 (tramp-flush-directory-property vec
"")))))
2689 ;; We use BUFFER also as connection buffer during setup. Because of
2690 ;; this, its original contents must be saved, and restored once
2691 ;; connection has been setup.
2692 (defun tramp-sh-handle-start-file-process (name buffer program
&rest args
)
2693 "Like `start-file-process' for Tramp files."
2694 (with-parsed-tramp-file-name default-directory nil
2695 (let* (;; When PROGRAM matches "*sh", and the first arg is "-c",
2696 ;; it might be that the arguments exceed the command line
2697 ;; length. Therefore, we modify the command.
2698 (heredoc (and (stringp program
)
2699 (string-match "sh$" program
)
2700 (string-equal "-c" (car args
))
2701 (= (length args
) 2)))
2702 ;; When PROGRAM is nil, we just provide a tty.
2703 (args (if (not heredoc
) args
2705 (while (and (< i
(length (cadr args
)))
2706 (string-match " " (cadr args
) i
))
2709 (list (replace-match " \\\\\n" nil nil
(cadr args
))))
2710 (setq i
(+ i
250))))
2713 (when (stringp program
)
2714 (format "cd %s && exec %s env PS1=%s %s"
2715 (tramp-shell-quote-argument localname
)
2716 (if heredoc
(format "<<'%s'" tramp-end-of-heredoc
) "")
2717 ;; Use a human-friendly prompt, for example for `shell'.
2718 (tramp-shell-quote-argument
2720 (file-remote-p default-directory
)
2721 tramp-initial-end-of-output
))
2723 (format "%s\n(\n%s\n) </dev/tty\n%s"
2724 program
(car args
) tramp-end-of-heredoc
)
2725 (mapconcat 'tramp-shell-quote-argument
2726 (cons program args
) " ")))))
2727 (tramp-process-connection-type
2728 (or (null program
) tramp-process-connection-type
))
2729 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
2732 ;; We do not want to raise an error when
2733 ;; `start-file-process' has been started several time in
2734 ;; `eshell' and friends.
2735 (tramp-current-connection nil
))
2738 ;; BUFFER can be nil. We use a temporary buffer.
2739 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
2740 (while (get-process name1
)
2741 ;; NAME must be unique as process name.
2743 name1
(format "%s<%d>" name i
)))
2745 ;; Set the new process properties.
2746 (tramp-set-connection-property v
"process-name" name
)
2747 (tramp-set-connection-property v
"process-buffer" buffer
)
2749 (with-current-buffer (tramp-get-connection-buffer v
)
2751 ;; We catch this event. Otherwise, `start-process' could
2752 ;; be called on the local host.
2755 ;; Activate narrowing in order to save BUFFER
2756 ;; contents. Clear also the modification time;
2757 ;; otherwise we might be interrupted by
2758 ;; `verify-visited-file-modtime'.
2759 (let ((buffer-undo-list t
)
2760 (buffer-read-only nil
)
2762 (clear-visited-file-modtime)
2763 (narrow-to-region (point-max) (point-max))
2764 ;; We call `tramp-maybe-open-connection', in order
2765 ;; to cleanup the prompt afterwards.
2767 (tramp-maybe-open-connection v
)
2769 (delete-region mark
(point))
2770 (narrow-to-region (point-max) (point-max))
2773 ;; Send the command.
2774 (tramp-send-command v command nil t
) ; nooutput
2775 ;; Check, whether a pty is associated.
2776 (unless (tramp-compat-process-get
2777 (tramp-get-connection-process v
) 'remote-tty
)
2780 "pty association is not supported for `%s'" name
))))
2781 (let ((p (tramp-get-connection-process v
)))
2782 ;; Set query flag and process marker for this
2783 ;; process. We ignore errors, because the process
2784 ;; could have finished already.
2786 (tramp-compat-set-process-query-on-exit-flag p t
)
2787 (set-marker (process-mark p
) (point)))
2792 (if (string-match tramp-temp-buffer-name
(buffer-name))
2794 (set-process-buffer (tramp-get-connection-process v
) nil
)
2795 (kill-buffer (current-buffer)))
2796 (set-buffer-modified-p bmp
))
2797 (tramp-set-connection-property v
"process-name" nil
)
2798 (tramp-set-connection-property v
"process-buffer" nil
))))))
2800 (defun tramp-sh-handle-process-file
2801 (program &optional infile destination display
&rest args
)
2802 "Like `process-file' for Tramp files."
2803 ;; The implementation is not complete yet.
2804 (when (and (numberp destination
) (zerop destination
))
2805 (error "Implementation does not handle immediate return"))
2807 (with-parsed-tramp-file-name default-directory nil
2808 (let (command input tmpinput stderr tmpstderr outbuf ret
)
2810 (setq command
(mapconcat 'tramp-shell-quote-argument
2811 (cons program args
) " "))
2814 (setq input
"/dev/null")
2815 (setq infile
(expand-file-name infile
))
2816 (if (tramp-equal-remote default-directory infile
)
2817 ;; INFILE is on the same remote host.
2818 (setq input
(with-parsed-tramp-file-name infile nil localname
))
2819 ;; INFILE must be copied to remote host.
2820 (setq input
(tramp-make-tramp-temp-file v
)
2821 tmpinput
(tramp-make-tramp-file-name method user host input
))
2822 (copy-file infile tmpinput t
)))
2823 (when input
(setq command
(format "%s <%s" command input
)))
2825 ;; Determine output.
2828 ((bufferp destination
)
2829 (setq outbuf destination
))
2831 ((stringp destination
)
2832 (setq outbuf
(get-buffer-create destination
)))
2833 ;; (REAL-DESTINATION ERROR-DESTINATION)
2834 ((consp destination
)
2837 ((bufferp (car destination
))
2838 (setq outbuf
(car destination
)))
2839 ((stringp (car destination
))
2840 (setq outbuf
(get-buffer-create (car destination
))))
2842 (setq outbuf
(current-buffer))))
2845 ((stringp (cadr destination
))
2846 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
2847 (if (tramp-equal-remote default-directory
(cadr destination
))
2848 ;; stderr is on the same remote host.
2849 (setq stderr
(with-parsed-tramp-file-name
2850 (cadr destination
) nil localname
))
2851 ;; stderr must be copied to remote host. The temporary
2852 ;; file must be deleted after execution.
2853 (setq stderr
(tramp-make-tramp-temp-file v
)
2854 tmpstderr
(tramp-make-tramp-file-name
2855 method user host stderr
))))
2856 ;; stderr to be discarded.
2857 ((null (cadr destination
))
2858 (setq stderr
"/dev/null"))))
2861 (setq outbuf
(current-buffer))))
2862 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
2864 ;; Send the command. It might not return in time, so we protect
2865 ;; it. Call it in a subshell, in order to preserve working
2870 (if (tramp-send-command-and-check
2871 v
(format "\\cd %s; %s"
2872 (tramp-shell-quote-argument localname
)
2876 ;; We should show the output anyway.
2878 (with-current-buffer outbuf
2880 (with-current-buffer (tramp-get-connection-buffer v
)
2882 (when display
(display-buffer outbuf
))))
2883 ;; When the user did interrupt, we should do it also. We use
2884 ;; return code -1 as marker.
2886 (kill-buffer (tramp-get-connection-buffer v
))
2890 (kill-buffer (tramp-get-connection-buffer v
))
2893 ;; Provide error file.
2894 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
2896 ;; Cleanup. We remove all file cache values for the connection,
2897 ;; because the remote process could have changed them.
2898 (when tmpinput
(delete-file tmpinput
))
2900 ;; `process-file-side-effects' has been introduced with GNU
2901 ;; Emacs 23.2. If set to `nil', no remote file will be changed
2902 ;; by `program'. If it doesn't exist, we assume its default
2904 (unless (and (boundp 'process-file-side-effects
)
2905 (not (symbol-value 'process-file-side-effects
)))
2906 (tramp-flush-directory-property v
""))
2908 ;; Return exit status.
2913 (defun tramp-sh-handle-file-local-copy (filename)
2914 "Like `file-local-copy' for Tramp files."
2915 (with-parsed-tramp-file-name filename nil
2916 (unless (file-exists-p filename
)
2919 "Cannot make local copy of non-existing file `%s'" filename
))
2921 (let* ((size (nth 7 (file-attributes (file-truename filename
))))
2922 (rem-enc (tramp-get-inline-coding v
"remote-encoding" size
))
2923 (loc-dec (tramp-get-inline-coding v
"local-decoding" size
))
2924 (tmpfile (tramp-compat-make-temp-file filename
)))
2928 ;; `copy-file' handles direct copy and out-of-band methods.
2929 ((or (tramp-local-host-p v
)
2930 (tramp-method-out-of-band-p v size
))
2931 (copy-file filename tmpfile t t
))
2933 ;; Use inline encoding for file transfer.
2936 (with-tramp-progress-reporter
2938 (format "Encoding remote file `%s' with `%s'" filename rem-enc
)
2939 (tramp-barf-unless-okay
2940 v
(format rem-enc
(tramp-shell-quote-argument localname
))
2941 "Encoding remote file failed"))
2943 (with-tramp-progress-reporter
2944 v
3 (format "Decoding local file `%s' with `%s'"
2946 (if (functionp loc-dec
)
2947 ;; If local decoding is a function, we call it.
2948 ;; We must disable multibyte, because
2949 ;; `uudecode-decode-region' doesn't handle it
2952 (set-buffer-multibyte nil
)
2953 (insert-buffer-substring (tramp-get-buffer v
))
2954 (funcall loc-dec
(point-min) (point-max))
2955 ;; Unset `file-name-handler-alist'. Otherwise,
2956 ;; epa-file gets confused.
2957 (let (file-name-handler-alist
2958 (coding-system-for-write 'binary
))
2960 (point-min) (point-max) tmpfile nil
'no-message
)))
2962 ;; If tramp-decoding-function is not defined for this
2963 ;; method, we invoke tramp-decoding-command instead.
2964 (let ((tmpfile2 (tramp-compat-make-temp-file filename
)))
2965 ;; Unset `file-name-handler-alist'. Otherwise,
2966 ;; epa-file gets confused.
2967 (let (file-name-handler-alist
2968 (coding-system-for-write 'binary
))
2969 (with-current-buffer (tramp-get-buffer v
)
2971 (point-min) (point-max) tmpfile2 nil
'no-message
)))
2973 (tramp-call-local-coding-command
2974 loc-dec tmpfile2 tmpfile
)
2975 (delete-file tmpfile2
)))))
2977 ;; Set proper permissions.
2978 (set-file-modes tmpfile
(tramp-default-file-modes filename
))
2979 ;; Set local user ownership.
2980 (tramp-set-file-uid-gid tmpfile
)))
2982 ;; Oops, I don't know what to do.
2984 v
'file-error
"Wrong method specification for `%s'" method
)))
2988 (delete-file tmpfile
)
2989 (signal (car err
) (cdr err
))))
2991 (run-hooks 'tramp-handle-file-local-copy-hook
)
2994 ;; This is needed for XEmacs only. Code stolen from files.el.
2995 (defun tramp-sh-handle-insert-file-contents-literally
2996 (filename &optional visit beg end replace
)
2997 "Like `insert-file-contents-literally' for Tramp files."
2998 (let ((format-alist nil
)
2999 (after-insert-file-functions nil
)
3000 (coding-system-for-read 'no-conversion
)
3001 (coding-system-for-write 'no-conversion
)
3002 (find-buffer-file-type-function
3003 (if (fboundp 'find-buffer-file-type
)
3004 (symbol-function 'find-buffer-file-type
)
3006 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler
))
3007 (inhibit-file-name-operation 'insert-file-contents
))
3010 (fset 'find-buffer-file-type
(lambda (_filename) t
))
3011 (insert-file-contents filename visit beg end replace
))
3013 (if find-buffer-file-type-function
3014 (fset 'find-buffer-file-type find-buffer-file-type-function
)
3015 (fmakunbound 'find-buffer-file-type
)))))
3017 ;; CCC grok LOCKNAME
3018 (defun tramp-sh-handle-write-region
3019 (start end filename
&optional append visit lockname confirm
)
3020 "Like `write-region' for Tramp files."
3021 (setq filename
(expand-file-name filename
))
3022 (with-parsed-tramp-file-name filename nil
3023 ;; Following part commented out because we don't know what to do about
3024 ;; file locking, and it does not appear to be a problem to ignore it.
3025 ;; Ange-ftp ignores it, too.
3026 ;; (when (and lockname (stringp lockname))
3027 ;; (setq lockname (expand-file-name lockname)))
3028 ;; (unless (or (eq lockname nil)
3029 ;; (string= lockname filename))
3031 ;; "tramp-sh-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3033 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
3034 (when (and (not (featurep 'xemacs
)) confirm
(file-exists-p filename
))
3035 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename
))
3036 (tramp-error v
'file-error
"File not overwritten")))
3038 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename
'integer
))
3039 (tramp-get-remote-uid v
'integer
)))
3040 (gid (or (nth 3 (tramp-compat-file-attributes filename
'integer
))
3041 (tramp-get-remote-gid v
'integer
))))
3043 (if (and (tramp-local-host-p v
)
3044 ;; `file-writable-p' calls `file-expand-file-name'. We
3045 ;; cannot use `tramp-run-real-handler' therefore.
3046 (let (file-name-handler-alist)
3048 (file-writable-p (file-name-directory localname
))
3049 (or (file-directory-p localname
)
3050 (file-writable-p localname
)))))
3051 ;; Short track: if we are on the local host, we can run directly.
3052 (tramp-run-real-handler
3054 (list start end localname append
'no-message lockname confirm
))
3056 (let* ((modes (save-excursion (tramp-default-file-modes filename
)))
3057 ;; We use this to save the value of
3058 ;; `last-coding-system-used' after writing the tmp
3059 ;; file. At the end of the function, we set
3060 ;; `last-coding-system-used' to this saved value. This
3061 ;; way, any intermediary coding systems used while
3062 ;; talking to the remote shell or suchlike won't hose
3063 ;; this variable. This approach was snarfed from
3066 ;; Write region into a tmp file. This isn't really
3067 ;; needed if we use an encoding function, but currently
3068 ;; we use it always because this makes the logic
3069 ;; simpler. We must also set `temporary-file-directory',
3070 ;; because it could point to a remote directory.
3071 (temporary-file-directory
3072 (tramp-compat-temporary-file-directory))
3073 (tmpfile (or tramp-temp-buffer-file-name
3074 (tramp-compat-make-temp-file filename
))))
3076 ;; If `append' is non-nil, we copy the file locally, and let
3077 ;; the native `write-region' implementation do the job.
3078 (when append
(copy-file filename tmpfile
'ok
))
3080 ;; We say `no-message' here because we don't want the
3081 ;; visited file modtime data to be clobbered from the temp
3082 ;; file. We call `set-visited-file-modtime' ourselves later
3083 ;; on. We must ensure that `file-coding-system-alist'
3084 ;; matches `tmpfile'.
3085 (let (file-name-handler-alist
3086 (file-coding-system-alist
3087 (tramp-find-file-name-coding-system-alist filename tmpfile
)))
3089 (tramp-run-real-handler
3091 (list start end tmpfile append
'no-message lockname confirm
))
3093 (setq tramp-temp-buffer-file-name nil
)
3094 (delete-file tmpfile
)
3095 (signal (car err
) (cdr err
))))
3097 ;; Now, `last-coding-system-used' has the right value. Remember it.
3098 (when (boundp 'last-coding-system-used
)
3099 (setq coding-system-used
3100 (symbol-value 'last-coding-system-used
))))
3102 ;; The permissions of the temporary file should be set. If
3103 ;; filename does not exist (eq modes nil) it has been
3104 ;; renamed to the backup file. This case `save-buffer'
3105 ;; handles permissions.
3106 ;; Ensure that it is still readable.
3110 (logior (or modes
0) (tramp-compat-octal-to-decimal "0400"))))
3112 ;; This is a bit lengthy due to the different methods
3113 ;; possible for file transfer. First, we check whether the
3114 ;; method uses an rcp program. If so, we call it.
3115 ;; Otherwise, both encoding and decoding command must be
3116 ;; specified. However, if the method _also_ specifies an
3117 ;; encoding function, then that is used for encoding the
3118 ;; contents of the tmp file.
3119 (let* ((size (nth 7 (file-attributes tmpfile
)))
3120 (rem-dec (tramp-get-inline-coding v
"remote-decoding" size
))
3121 (loc-enc (tramp-get-inline-coding v
"local-encoding" size
)))
3123 ;; `copy-file' handles direct copy and out-of-band methods.
3124 ((or (tramp-local-host-p v
)
3125 (tramp-method-out-of-band-p v size
))
3126 (if (and (not (stringp start
))
3127 (= (or end
(point-max)) (point-max))
3128 (= (or start
(point-min)) (point-min))
3129 (tramp-get-method-parameter
3130 method
'tramp-copy-keep-tmpfile
))
3132 (setq tramp-temp-buffer-file-name tmpfile
)
3134 ;; We keep the local file for performance
3135 ;; reasons, useful for "rsync".
3136 (copy-file tmpfile filename t
)
3138 (setq tramp-temp-buffer-file-name nil
)
3139 (delete-file tmpfile
)
3140 (signal (car err
) (cdr err
)))))
3141 (setq tramp-temp-buffer-file-name nil
)
3142 ;; Don't rename, in order to keep context in SELinux.
3144 (copy-file tmpfile filename t
)
3145 (delete-file tmpfile
))))
3147 ;; Use inline file transfer.
3152 (set-buffer-multibyte nil
)
3153 ;; Use encoding function or command.
3154 (with-tramp-progress-reporter
3155 v
3 (format "Encoding local file `%s' using `%s'"
3157 (if (functionp loc-enc
)
3158 ;; The following `let' is a workaround for
3159 ;; the base64.el that comes with pgnus-0.84.
3160 ;; If both of the following conditions are
3161 ;; satisfied, it tries to write to a local
3162 ;; file in default-directory, but at this
3163 ;; point, default-directory is remote.
3164 ;; (`call-process-region' can't write to
3165 ;; remote files, it seems.) The file in
3166 ;; question is a tmp file anyway.
3167 (let ((coding-system-for-read 'binary
)
3169 (tramp-compat-temporary-file-directory)))
3170 (insert-file-contents-literally tmpfile
)
3171 (funcall loc-enc
(point-min) (point-max)))
3173 (unless (zerop (tramp-call-local-coding-command
3177 (concat "Cannot write to `%s', "
3178 "local encoding command `%s' failed")
3179 filename loc-enc
))))
3181 ;; Send buffer into remote decoding command which
3182 ;; writes to remote file. Because this happens on
3183 ;; the remote host, we cannot use the function.
3184 (with-tramp-progress-reporter
3185 v
3 (format "Decoding remote file `%s' using `%s'"
3187 (goto-char (point-max))
3188 (unless (bolp) (newline))
3192 (concat rem-dec
" <<'%s'\n%s%s")
3193 (tramp-shell-quote-argument localname
)
3194 tramp-end-of-heredoc
3196 tramp-end-of-heredoc
))
3197 (tramp-barf-unless-okay
3199 "Couldn't write region to `%s', decode using `%s' failed"
3201 ;; When `file-precious-flag' is set, the region is
3202 ;; written to a temporary file. Check that the
3203 ;; checksum is equal to that from the local tmpfile.
3204 (when file-precious-flag
3207 ;; cksum runs locally, if possible.
3208 (zerop (tramp-call-process "cksum" tmpfile t
))
3209 ;; cksum runs remotely.
3210 (tramp-send-command-and-check
3213 "cksum <%s" (tramp-shell-quote-argument localname
)))
3214 ;; ... they are different.
3218 (with-current-buffer (tramp-get-buffer v
)
3222 (concat "Couldn't write region to `%s',"
3223 " decode using `%s' failed")
3224 filename rem-dec
)))))
3227 (delete-file tmpfile
)))
3229 ;; That's not expected.
3233 (concat "Method `%s' should specify both encoding and "
3234 "decoding command or an rcp program")
3237 ;; Make `last-coding-system-used' have the right value.
3238 (when coding-system-used
3239 (set 'last-coding-system-used coding-system-used
))))
3241 (tramp-flush-file-property v
(file-name-directory localname
))
3242 (tramp-flush-file-property v localname
)
3244 ;; We must protect `last-coding-system-used', now we have set it
3245 ;; to its correct value.
3246 (let (last-coding-system-used (need-chown t
))
3247 ;; Set file modification time.
3248 (when (or (eq visit t
) (stringp visit
))
3249 (let ((file-attr (tramp-compat-file-attributes filename
'integer
)))
3250 (set-visited-file-modtime
3251 ;; We must pass modtime explicitly, because filename can
3252 ;; be different from (buffer-file-name), f.e. if
3253 ;; `file-precious-flag' is set.
3255 (when (and (= (nth 2 file-attr
) uid
)
3256 (= (nth 3 file-attr
) gid
))
3257 (setq need-chown nil
))))
3259 ;; Set the ownership.
3261 (tramp-set-file-uid-gid filename uid gid
))
3262 (when (or (eq visit t
) (null visit
) (stringp visit
))
3263 (tramp-message v
0 "Wrote %s" filename
))
3264 (run-hooks 'tramp-handle-write-region-hook
)))))
3266 (defvar tramp-vc-registered-file-names nil
3267 "List used to collect file names, which are checked during `vc-registered'.")
3269 ;; VC backends check for the existence of various different special
3270 ;; files. This is very time consuming, because every single check
3271 ;; requires a remote command (the file cache must be invalidated).
3272 ;; Therefore, we apply a kind of optimization. We install the file
3273 ;; name handler `tramp-vc-file-name-handler', which does nothing but
3274 ;; remembers all file names for which `file-exists-p' or
3275 ;; `file-readable-p' has been applied. A first run of `vc-registered'
3276 ;; is performed. Afterwards, a script is applied for all collected
3277 ;; file names, using just one remote command. The result of this
3278 ;; script is used to fill the file cache with actual values. Now we
3279 ;; can reset the file name handlers, and we make a second run of
3280 ;; `vc-registered', which returns the expected result without sending
3281 ;; any other remote command.
3282 (defun tramp-sh-handle-vc-registered (file)
3283 "Like `vc-registered' for Tramp files."
3284 (tramp-compat-with-temp-message ""
3285 (with-parsed-tramp-file-name file nil
3286 (with-tramp-progress-reporter
3287 v
3 (format "Checking `vc-registered' for %s" file
)
3289 ;; There could be new files, created by the vc backend. We
3290 ;; cannot reuse the old cache entries, therefore.
3291 (let (tramp-vc-registered-file-names
3292 (remote-file-name-inhibit-cache (current-time))
3293 (file-name-handler-alist
3294 `((,tramp-file-name-regexp . tramp-vc-file-name-handler
))))
3296 ;; Here we collect only file names, which need an operation.
3297 (ignore-errors (tramp-run-real-handler 'vc-registered
(list file
)))
3298 (tramp-message v
10 "\n%s" tramp-vc-registered-file-names
)
3300 ;; Send just one command, in order to fill the cache.
3301 (when tramp-vc-registered-file-names
3302 (tramp-maybe-send-script
3304 (format tramp-vc-registered-read-file-names
3305 (tramp-get-file-exists-command v
)
3306 (format "%s -r" (tramp-get-test-command v
)))
3307 "tramp_vc_registered_read_file_names")
3311 (tramp-send-command-and-read
3314 "tramp_vc_registered_read_file_names <<'%s'\n%s\n%s\n"
3315 tramp-end-of-heredoc
3316 (mapconcat 'tramp-shell-quote-argument
3317 tramp-vc-registered-file-names
3319 tramp-end-of-heredoc
)))
3321 (tramp-set-file-property
3322 v
(car elt
) (cadr elt
) (cadr (cdr elt
))))))
3324 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3325 ;; calls shall be answered from the file cache. We unset
3326 ;; `process-file-side-effects' in order to keep the cache when
3327 ;; `process-file' calls appear.
3328 (let (process-file-side-effects)
3330 (tramp-run-real-handler 'vc-registered
(list file
))))))))
3332 ;;;###tramp-autoload
3333 (defun tramp-sh-file-name-handler (operation &rest args
)
3334 "Invoke remote-shell Tramp file name handler.
3335 Fall back to normal file name handler if no Tramp handler exists."
3336 (when (and tramp-locked
(not tramp-locker
))
3337 (setq tramp-locked nil
)
3338 (signal 'file-error
(list "Forbidden reentrant call of Tramp")))
3339 (let ((tl tramp-locked
))
3342 (setq tramp-locked t
)
3343 (let ((tramp-locker t
))
3345 (let ((fn (assoc operation tramp-sh-file-name-handler-alist
)))
3347 (apply (cdr fn
) args
)
3348 (tramp-run-real-handler operation args
))))))
3349 (setq tramp-locked tl
))))
3351 (defun tramp-vc-file-name-handler (operation &rest args
)
3352 "Invoke special file name handler, which collects files to be handled."
3355 (tramp-replace-environment-variables
3356 (apply 'tramp-file-name-for-operation operation args
)))
3357 (fn (assoc operation tramp-sh-file-name-handler-alist
)))
3358 (with-parsed-tramp-file-name filename nil
3360 ;; That's what we want: file names, for which checks are
3361 ;; applied. We assume that VC uses only `file-exists-p' and
3362 ;; `file-readable-p' checks; otherwise we must extend the
3363 ;; list. We do not perform any action, but return nil, in
3364 ;; order to keep `vc-registered' running.
3365 ((and fn
(memq operation
'(file-exists-p file-readable-p
)))
3366 (add-to-list 'tramp-vc-registered-file-names localname
'append
)
3368 ;; `process-file' and `start-file-process' shall be ignored.
3369 ((and fn
(eq operation
'process-file
) 0))
3370 ((and fn
(eq operation
'start-file-process
) nil
))
3371 ;; Tramp file name handlers like `expand-file-name'. They
3373 (fn (save-match-data (apply (cdr fn
) args
)))
3374 ;; Default file name handlers, we don't care.
3375 (t (tramp-run-real-handler operation args
)))))))
3377 (defun tramp-sh-handle-file-notify-add-watch (file-name flags _callback
)
3378 "Like `file-notify-add-watch' for Tramp files."
3379 (setq file-name
(expand-file-name file-name
))
3380 (with-parsed-tramp-file-name file-name nil
3381 (let* ((default-directory (file-name-directory file-name
))
3382 command events filter p sequence
)
3384 ;; gvfs-monitor-dir.
3385 ((setq command
(tramp-get-remote-gvfs-monitor-dir v
))
3386 (setq filter
'tramp-sh-file-gvfs-monitor-dir-process-filter
3387 sequence
`(,command
,localname
)))
3389 ((setq command
(tramp-get-remote-inotifywait v
))
3390 (setq filter
'tramp-sh-file-inotifywait-process-filter
3393 ((and (memq 'change flags
) (memq 'attribute-change flags
))
3394 "create,modify,move,delete,attrib")
3395 ((memq 'change flags
) "create,modify,move,delete")
3396 ((memq 'attribute-change flags
) "attrib"))
3397 sequence
`(,command
"-mq" "-e" ,events
,localname
)))
3400 v
'file-notify-error
3401 "No file notification program found on %s"
3402 (file-remote-p file-name
))))
3406 (file-name-nondirectory command
)
3407 (generate-new-buffer
3408 (format " *%s*" (file-name-nondirectory command
)))
3410 ;; Return the process object as watch-descriptor.
3411 (if (not (processp p
))
3413 v
'file-notify-error
3414 "`%s' failed to start on remote host"
3415 (mapconcat 'identity sequence
" "))
3416 (tramp-message v
6 "Run `%s', %S" (mapconcat 'identity sequence
" ") p
)
3417 (tramp-set-connection-property p
"vector" v
)
3418 (tramp-compat-set-process-query-on-exit-flag p nil
)
3419 (set-process-filter p filter
)
3422 (defun tramp-sh-file-gvfs-monitor-dir-process-filter (proc string
)
3423 "Read output from \"gvfs-monitor-dir\" and add corresponding file-notify events."
3424 (let ((remote-prefix
3425 (with-current-buffer (process-buffer proc
)
3426 (file-remote-p default-directory
)))
3427 (rest-string (tramp-compat-process-get proc
'rest-string
)))
3429 (tramp-message proc
10 "Previous string:\n%s" rest-string
))
3430 (tramp-message proc
6 "%S\n%s" proc string
)
3431 (setq string
(concat rest-string string
)
3432 ;; Attribute change is returned in unused wording.
3433 string
(tramp-compat-replace-regexp-in-string
3434 "ATTRIB CHANGED" "ATTRIBUTE_CHANGED" string
))
3436 (while (string-match
3438 "Directory Monitor Event:[\n\r]+"
3439 "Child = \\([^\n\r]+\\)[\n\r]+"
3440 "\\(Other = \\([^\n\r]+\\)[\n\r]+\\)?"
3441 "Event = \\([^[:blank:]]+\\)[\n\r]+")
3447 (tramp-compat-replace-regexp-in-string
3448 "_" "-" (downcase (match-string 4 string
))))
3449 ;; File names are returned as absolute paths. We must
3450 ;; add the remote prefix.
3451 (concat remote-prefix
(match-string 1 string
))
3452 (when (match-string 3 string
)
3453 (concat remote-prefix
(match-string 3 string
))))))
3454 (setq string
(replace-match "" nil nil string
))
3455 ;; Usually, we would add an Emacs event now. Unfortunately,
3456 ;; `unread-command-events' does not accept several events at
3457 ;; once. Therefore, we apply the callback directly.
3458 (tramp-compat-funcall 'file-notify-callback object
)))
3460 ;; Save rest of the string.
3461 (when (zerop (length string
)) (setq string nil
))
3462 (when string
(tramp-message proc
10 "Rest string:\n%s" string
))
3463 (tramp-compat-process-put proc
'rest-string string
)))
3465 (defun tramp-sh-file-inotifywait-process-filter (proc string
)
3466 "Read output from \"inotifywait\" and add corresponding file-notify events."
3467 (tramp-message proc
6 "%S\n%s" proc string
)
3468 (dolist (line (split-string string
"[\n\r]+" 'omit-nulls
))
3469 ;; Check, whether there is a problem.
3472 (concat "^[^[:blank:]]+"
3473 "[[:blank:]]+\\([^[:blank:]]+\\)+"
3474 "\\([[:blank:]]+\\([^\n\r]+\\)\\)?")
3476 (tramp-error proc
'file-notify-error
"%s" line
))
3484 (tramp-compat-replace-regexp-in-string "_" "-" (downcase x
))))
3485 (split-string (match-string 1 line
) "," 'omit-nulls
))
3486 (match-string 3 line
))))
3487 ;; Usually, we would add an Emacs event now. Unfortunately,
3488 ;; `unread-command-events' does not accept several events at
3489 ;; once. Therefore, we apply the callback directly.
3490 (tramp-compat-funcall 'file-notify-callback object
))))
3492 ;;; Internal Functions:
3494 (defun tramp-maybe-send-script (vec script name
)
3495 "Define in remote shell function NAME implemented as SCRIPT.
3496 Only send the definition if it has not already been done."
3497 ;; We cannot let-bind (tramp-get-connection-process vec) because it
3499 (let ((scripts (tramp-get-connection-property
3500 (tramp-get-connection-process vec
) "scripts" nil
)))
3501 (unless (member name scripts
)
3502 (with-tramp-progress-reporter vec
5 (format "Sending script `%s'" name
)
3503 ;; The script could contain a call of Perl. This is masked with `%s'.
3504 (when (and (string-match "%s" script
)
3505 (not (tramp-get-remote-perl vec
)))
3506 (tramp-error vec
'file-error
"No Perl available on remote host"))
3507 (tramp-barf-unless-okay
3509 (format "%s () {\n%s\n}" name
3510 (format script
(tramp-get-remote-perl vec
)))
3511 "Script %s sending failed" name
)
3512 (tramp-set-connection-property
3513 (tramp-get-connection-process vec
) "scripts" (cons name scripts
))))))
3515 (defun tramp-set-auto-save ()
3516 (when (and ;; ange-ftp has its own auto-save mechanism
3517 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
3518 'tramp-sh-file-name-handler
)
3520 (auto-save-mode 1)))
3521 (add-hook 'find-file-hooks
'tramp-set-auto-save t
)
3522 (add-hook 'tramp-unload-hook
3524 (remove-hook 'find-file-hooks
'tramp-set-auto-save
)))
3526 (defun tramp-run-test (switch filename
)
3527 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3528 Returns the exit code of the `test' program."
3529 (with-parsed-tramp-file-name filename nil
3530 (tramp-send-command-and-check
3534 (tramp-get-test-command v
)
3536 (tramp-shell-quote-argument localname
)))))
3538 (defun tramp-run-test2 (format-string file1 file2
)
3539 "Run `test'-like program on the remote system, given FILE1, FILE2.
3540 FORMAT-STRING contains the program name, switches, and place holders.
3541 Returns the exit code of the `test' program. Barfs if the methods,
3542 hosts, or files, disagree."
3543 (unless (tramp-equal-remote file1 file2
)
3544 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1
) file1 file2
) nil
3547 "tramp-run-test2 only implemented for same method, user, host")))
3548 (with-parsed-tramp-file-name file1 v1
3549 (with-parsed-tramp-file-name file1 v2
3550 (tramp-send-command-and-check
3552 (format format-string
3553 (tramp-shell-quote-argument v1-localname
)
3554 (tramp-shell-quote-argument v2-localname
))))))
3556 (defun tramp-find-executable
3557 (vec progname dirlist
&optional ignore-tilde ignore-path
)
3558 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3559 First arg VEC specifies the connection, PROGNAME is the program
3560 to search for, and DIRLIST gives the list of directories to
3561 search. If IGNORE-TILDE is non-nil, directory names starting
3562 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3565 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3567 This function expects to be in the right *tramp* buffer."
3568 (with-current-buffer (tramp-get-connection-buffer vec
)
3570 ;; Check whether the executable is in $PATH. "which(1)" does not
3571 ;; report always a correct error code; therefore we check the
3572 ;; number of words it returns.
3574 (tramp-send-command vec
(format "which \\%s | wc -w" progname
))
3575 (goto-char (point-min))
3576 (if (looking-at "^\\s-*1$")
3577 (setq result
(concat "\\" progname
))))
3580 ;; Remove all ~/foo directories from dirlist. In XEmacs,
3581 ;; `remove' is in CL, and we want to avoid CL dependencies.
3584 (setq d
(car dirlist
))
3585 (setq dirlist
(cdr dirlist
))
3586 (unless (char-equal ?~
(aref d
0))
3587 (setq newdl
(cons d newdl
))))
3588 (setq dirlist
(nreverse newdl
))))
3591 (format (concat "while read d; "
3592 "do if test -x $d/%s -a -f $d/%s; "
3593 "then echo tramp_executable $d/%s; "
3594 "break; fi; done <<'%s'\n"
3596 progname progname progname
3597 tramp-end-of-heredoc
3598 (mapconcat 'identity dirlist
"\n")
3599 tramp-end-of-heredoc
))
3600 (goto-char (point-max))
3601 (when (search-backward "tramp_executable " nil t
)
3602 (skip-chars-forward "^ ")
3603 (skip-chars-forward " ")
3604 (setq result
(buffer-substring (point) (point-at-eol)))))
3607 (defun tramp-set-remote-path (vec)
3608 "Sets the remote environment PATH to existing directories.
3609 I.e., for each directory in `tramp-remote-path', it is tested
3610 whether it exists and if so, it is added to the environment
3612 (tramp-message vec
5 "Setting $PATH environment variable")
3614 vec
(format "PATH=%s; export PATH"
3615 (mapconcat 'identity
(tramp-get-remote-path vec
) ":"))))
3617 ;; ------------------------------------------------------------
3618 ;; -- Communication with external shell --
3619 ;; ------------------------------------------------------------
3621 (defun tramp-find-file-exists-command (vec)
3622 "Find a command on the remote host for checking if a file exists.
3623 Here, we are looking for a command which has zero exit status if the
3624 file exists and nonzero exit status otherwise."
3625 (let ((existing "/")
3627 (tramp-shell-quote-argument "/ this file does not exist "))
3629 ;; The algorithm is as follows: we try a list of several commands.
3630 ;; For each command, we first run `$cmd /' -- this should return
3631 ;; true, as the root directory always exists. And then we run
3632 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
3633 ;; does not exist. This should return false. We use the first
3634 ;; command we find that seems to work.
3635 ;; The list of commands to try is as follows:
3636 ;; `ls -d' This works on most systems, but NetBSD 1.4
3637 ;; has a bug: `ls' always returns zero exit
3638 ;; status, even for files which don't exist.
3639 ;; `test -e' Some Bourne shells have a `test' builtin
3640 ;; which does not know the `-e' option.
3641 ;; `/bin/test -e' For those, the `test' binary on disk normally
3642 ;; provides the option. Alas, the binary
3643 ;; is sometimes `/bin/test' and sometimes it's
3645 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3648 (and (setq result
(format "%s -e" (tramp-get-test-command vec
)))
3649 (tramp-send-command-and-check
3650 vec
(format "%s %s" result existing
))
3651 (not (tramp-send-command-and-check
3652 vec
(format "%s %s" result nonexistent
)))))
3654 (and (setq result
"/bin/test -e")
3655 (tramp-send-command-and-check
3656 vec
(format "%s %s" result existing
))
3657 (not (tramp-send-command-and-check
3658 vec
(format "%s %s" result nonexistent
)))))
3660 (and (setq result
"/usr/bin/test -e")
3661 (tramp-send-command-and-check
3662 vec
(format "%s %s" result existing
))
3663 (not (tramp-send-command-and-check
3664 vec
(format "%s %s" result nonexistent
)))))
3666 (and (setq result
(format "%s -d" (tramp-get-ls-command vec
)))
3667 (tramp-send-command-and-check
3668 vec
(format "%s %s" result existing
))
3669 (not (tramp-send-command-and-check
3670 vec
(format "%s %s" result nonexistent
))))))
3672 vec
'file-error
"Couldn't find command to check if file exists"))
3675 (defun tramp-open-shell (vec shell
)
3676 "Opens shell SHELL."
3677 (with-tramp-progress-reporter
3678 vec
5 (format "Opening remote shell `%s'" shell
)
3679 ;; Find arguments for this shell.
3680 (let ((tramp-end-of-output tramp-initial-end-of-output
)
3681 (alist tramp-sh-extra-args
)
3683 (while (and alist
(null extra-args
))
3684 (setq item
(pop alist
))
3685 (when (string-match (car item
) shell
)
3686 (setq extra-args
(cdr item
))))
3689 "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s"
3690 (tramp-shell-quote-argument tramp-end-of-output
)
3691 shell
(or extra-args
""))
3693 (tramp-set-connection-property
3694 (tramp-get-connection-process vec
) "remote-shell" shell
)
3697 vec
(format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output
)) t
)
3698 (tramp-send-command vec
"PS2=''" t
)
3699 (tramp-send-command vec
"PS3=''" t
)
3700 (tramp-send-command vec
"PROMPT_COMMAND=''" t
)))
3702 (defun tramp-find-shell (vec)
3703 "Opens a shell on the remote host which groks tilde expansion."
3704 (with-current-buffer (tramp-get-buffer vec
)
3705 (let ((default-shell
3707 (tramp-get-connection-property
3708 (tramp-get-connection-process vec
) "remote-shell" nil
)
3709 (tramp-get-method-parameter
3710 (tramp-file-name-method vec
) 'tramp-remote-shell
)))
3713 (with-tramp-connection-property vec
"remote-shell"
3714 ;; CCC: "root" does not exist always, see QNAP 459.
3715 ;; Which check could we apply instead?
3716 (tramp-send-command vec
"echo ~root" t
)
3717 (if (or (string-match "^~root$" (buffer-string))
3718 ;; The default shell (ksh93) of OpenSolaris and
3719 ;; Solaris is buggy. We've got reports for
3720 ;; "SunOS 5.10" and "SunOS 5.11" so far.
3721 (string-match (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3722 (tramp-get-connection-property
3725 (or (tramp-find-executable
3726 vec
"bash" (tramp-get-remote-path vec
) t t
)
3727 (tramp-find-executable
3728 vec
"ksh" (tramp-get-remote-path vec
) t t
)
3729 ;; Maybe it works at least for some other commands.
3735 "Couldn't find a remote shell which groks tilde "
3736 "expansion, using `%s'")
3741 ;; Open a new shell if needed.
3742 (unless (string-equal shell default-shell
)
3744 vec
5 "Starting remote shell `%s' for tilde expansion" shell
)
3745 (tramp-open-shell vec shell
)))))
3747 ;; Utility functions.
3749 (defun tramp-barf-if-no-shell-prompt (proc timeout
&rest error-args
)
3750 "Wait for shell prompt and barf if none appears.
3751 Looks at process PROC to see if a shell prompt appears in TIMEOUT
3752 seconds. If not, it produces an error message with the given ERROR-ARGS."
3753 (let ((vec (tramp-get-connection-property proc
"vector" nil
)))
3755 (tramp-wait-for-regexp
3758 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern
))
3760 (delete-process proc
)
3761 (apply 'tramp-error-with-buffer
3762 (tramp-get-connection-buffer vec
) vec
'file-error error-args
)))))
3764 (defun tramp-open-connection-setup-interactive-shell (proc vec
)
3765 "Set up an interactive shell.
3766 Mainly sets the prompt and the echo correctly. PROC is the shell
3767 process to set up. VEC specifies the connection."
3768 (let ((tramp-end-of-output tramp-initial-end-of-output
))
3769 ;; It is useful to set the prompt in the following command because
3770 ;; some people have a setting for $PS1 which /bin/sh doesn't know
3771 ;; about and thus /bin/sh will display a strange prompt. For
3772 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
3773 ;; display the current working directory but /bin/sh will display
3774 ;; a dollar sign. The following command line sets $PS1 to a sane
3775 ;; value, and works under Bourne-ish shells as well as csh-like
3776 ;; shells. Daniel Pittman reports that the unusual positioning of
3777 ;; the single quotes makes it work under `rc', too. We also unset
3778 ;; the variable $ENV because that is read by some sh
3779 ;; implementations (eg, bash when called as sh) on startup; this
3780 ;; way, we avoid the startup file clobbering $PS1. $PROMPT_COMMAND
3781 ;; is another way to set the prompt in /bin/bash, it must be
3782 ;; discarded as well.
3785 (or (tramp-get-connection-property vec
"remote-shell" nil
)
3786 (tramp-get-method-parameter
3787 (tramp-file-name-method vec
) 'tramp-remote-shell
)))
3790 (tramp-message vec
5 "Setting up remote shell environment")
3791 (tramp-send-command vec
"stty -inlcr -echo kill '^U' erase '^H'" t
)
3792 ;; Check whether the echo has really been disabled. Some
3793 ;; implementations, like busybox of embedded GNU/Linux, don't
3794 ;; support disabling.
3795 (tramp-send-command vec
"echo foo" t
)
3796 (with-current-buffer (process-buffer proc
)
3797 (goto-char (point-min))
3798 (when (looking-at "echo foo")
3799 (tramp-set-connection-property proc
"remote-echo" t
)
3800 (tramp-message vec
5 "Remote echo still on. Ok.")
3801 ;; Make sure backspaces and their echo are enabled and no line
3802 ;; width magic interferes with them.
3803 (tramp-send-command vec
"stty icanon erase ^H cols 32767" t
))))
3805 (tramp-message vec
5 "Setting shell prompt")
3807 vec
(format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output
)) t
)
3808 (tramp-send-command vec
"PS2=''" t
)
3809 (tramp-send-command vec
"PS3=''" t
)
3810 (tramp-send-command vec
"PROMPT_COMMAND=''" t
)
3812 ;; Try to set up the coding system correctly.
3813 ;; CCC this can't be the right way to do it. Hm.
3814 (tramp-message vec
5 "Determining coding system")
3815 (tramp-send-command vec
"echo foo ; echo bar" t
)
3816 (with-current-buffer (process-buffer proc
)
3817 (goto-char (point-min))
3818 (if (featurep 'mule
)
3819 ;; Use MULE to select the right EOL convention for communicating
3820 ;; with the process.
3821 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc
)
3822 (cons 'undecided
'undecided
)))
3823 cs-decode cs-encode
)
3824 (when (symbolp cs
) (setq cs
(cons cs cs
)))
3825 (setq cs-decode
(car cs
))
3826 (setq cs-encode
(cdr cs
))
3827 (unless cs-decode
(setq cs-decode
'undecided
))
3828 (unless cs-encode
(setq cs-encode
'undecided
))
3829 (setq cs-encode
(tramp-compat-coding-system-change-eol-conversion
3831 (when (search-forward "\r" nil t
)
3832 (setq cs-decode
(tramp-compat-coding-system-change-eol-conversion
3834 (tramp-compat-funcall
3835 'set-buffer-process-coding-system cs-decode cs-encode
)
3837 vec
5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode
))
3838 ;; Look for ^M and do something useful if found.
3839 (when (search-forward "\r" nil t
)
3840 ;; We have found a ^M but cannot frob the process coding system
3841 ;; because we're running on a non-MULE Emacs. Let's try
3843 (tramp-send-command vec
"stty -onlcr" t
))))
3845 (tramp-send-command vec
"set +o vi +o emacs" t
)
3847 ;; Check whether the output of "uname -sr" has been changed. If
3848 ;; yes, this is a strong indication that we must expire all
3849 ;; connection properties. We start again with
3850 ;; `tramp-maybe-open-connection', it will be caught there.
3851 (tramp-message vec
5 "Checking system information")
3852 (let ((old-uname (tramp-get-connection-property vec
"uname" nil
))
3854 (tramp-set-connection-property
3856 (tramp-send-command-and-read vec
"echo \\\"`uname -sr`\\\""))))
3857 (when (and (stringp old-uname
) (not (string-equal old-uname new-uname
)))
3860 "Connection reset, because remote host changed from `%s' to `%s'"
3861 old-uname new-uname
)
3862 ;; We want to keep the password.
3863 (tramp-cleanup-connection vec t t
)
3864 (throw 'uname-changed
(tramp-maybe-open-connection vec
))))
3866 ;; Check whether the remote host suffers from buggy
3867 ;; `send-process-string'. This is known for FreeBSD (see comment in
3868 ;; `send_process', file process.c). I've tested sending 624 bytes
3869 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
3870 ;; this host type is detected locally. It cannot handle remote
3872 (with-tramp-connection-property proc
"chunksize"
3874 ((and (integerp tramp-chunksize
) (> tramp-chunksize
0))
3878 vec
5 "Checking remote host type for `send-process-string' bug")
3880 "^FreeBSD" (tramp-get-connection-property vec
"uname" ""))
3883 ;; Set remote PATH variable.
3884 (tramp-set-remote-path vec
)
3886 ;; Search for a good shell before searching for a command which
3887 ;; checks if a file exists. This is done because Tramp wants to use
3888 ;; "test foo; echo $?" to check if various conditions hold, and
3889 ;; there are buggy /bin/sh implementations which don't execute the
3890 ;; "echo $?" part if the "test" part has an error. In particular,
3891 ;; the OpenSolaris /bin/sh is a problem. There are also other
3892 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
3893 ;; in function declarations, or changing HISTFILE in place.
3894 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
3896 (tramp-find-shell vec
)
3898 ;; Disable unexpected output.
3899 (tramp-send-command vec
"mesg n; biff n" t
)
3901 ;; IRIX64 bash expands "!" even when in single quotes. This
3902 ;; destroys our shell functions, we must disable it. See
3903 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
3904 (when (string-match "^IRIX64" (tramp-get-connection-property vec
"uname" ""))
3905 (tramp-send-command vec
"set +H" t
))
3907 ;; On BSD-like systems, ?\t is expanded to spaces. Suppress this.
3908 (when (string-match "BSD\\|Darwin"
3909 (tramp-get-connection-property vec
"uname" ""))
3910 (tramp-send-command vec
"stty -oxtabs" t
))
3912 ;; Set `remote-tty' process property.
3913 (let ((tty (tramp-send-command-and-read vec
"echo \\\"`tty`\\\"" 'noerror
)))
3914 (unless (zerop (length tty
))
3915 (tramp-compat-process-put proc
'remote-tty tty
)))
3917 ;; Dump stty settings in the traces.
3918 (when (>= tramp-verbose
9)
3919 (tramp-send-command vec
"stty -a" t
))
3921 ;; Set the environment.
3922 (tramp-message vec
5 "Setting default environment")
3924 (let ((env (append `(,(tramp-get-remote-locale vec
))
3925 (copy-sequence tramp-remote-process-environment
)))
3928 (setq item
(tramp-compat-split-string (car env
) "="))
3929 (setcdr item
(mapconcat 'identity
(cdr item
) "="))
3930 (if (and (stringp (cdr item
)) (not (string-equal (cdr item
) "")))
3932 vec
(format "%s=%s; export %s" (car item
) (cdr item
) (car item
)) t
)
3933 (push (car item
) unset
))
3934 (setq env
(cdr env
)))
3937 vec
(format "unset %s" (mapconcat 'identity unset
" ")) t
))))
3939 ;; Old text from documentation of tramp-methods:
3940 ;; Using a uuencode/uudecode inline method is discouraged, please use one
3941 ;; of the base64 methods instead since base64 encoding is much more
3942 ;; reliable and the commands are more standardized between the different
3943 ;; Unix versions. But if you can't use base64 for some reason, please
3944 ;; note that the default uudecode command does not work well for some
3945 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
3946 ;; the following command for uudecode:
3948 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
3950 ;; For Irix, no solution is known yet.
3952 (autoload 'uudecode-decode-region
"uudecode")
3954 (defconst tramp-local-coding-commands
3955 `((b64 base64-encode-region base64-decode-region
)
3956 (uu tramp-uuencode-region uudecode-decode-region
)
3957 (pack ,(format tramp-perl-pack
"perl") ,(format tramp-perl-unpack
"perl")))
3958 "List of local coding commands for inline transfer.
3959 Each item is a list that looks like this:
3961 \(FORMAT ENCODING DECODING\)
3963 FORMAT is symbol describing the encoding/decoding format. It can be
3964 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3966 ENCODING and DECODING can be strings, giving commands, or symbols,
3967 giving functions. If they are strings, then they can contain
3968 the \"%s\" format specifier. If that specifier is present, the input
3969 filename will be put into the command line at that spot. If the
3970 specifier is not present, the input should be read from standard
3973 If they are functions, they will be called with two arguments, start
3974 and end of region, and are expected to replace the region contents
3975 with the encoded or decoded results, respectively.")
3977 (defconst tramp-remote-coding-commands
3978 '((b64 "base64" "base64 -d -i")
3979 ;; "-i" is more robust with older base64 from GNU coreutils.
3980 ;; However, I don't know whether all base64 versions do supports
3982 (b64 "base64" "base64 -d")
3983 (b64 "mimencode -b" "mimencode -u -b")
3984 (b64 "mmencode -b" "mmencode -u -b")
3985 (b64 "recode data..base64" "recode base64..data")
3986 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module
)
3987 (b64 tramp-perl-encode tramp-perl-decode
)
3988 (uu "uuencode xxx" "uudecode -o /dev/stdout" "test -c /dev/stdout")
3989 (uu "uuencode xxx" "uudecode -o -")
3990 (uu "uuencode xxx" "uudecode -p")
3991 (uu "uuencode xxx" tramp-uudecode
)
3992 (pack tramp-perl-pack tramp-perl-unpack
))
3993 "List of remote coding commands for inline transfer.
3994 Each item is a list that looks like this:
3996 \(FORMAT ENCODING DECODING [TEST]\)
3998 FORMAT is symbol describing the encoding/decoding format. It can be
3999 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
4001 ENCODING and DECODING can be strings, giving commands, or symbols,
4002 giving variables. If they are strings, then they can contain
4003 the \"%s\" format specifier. If that specifier is present, the input
4004 filename will be put into the command line at that spot. If the
4005 specifier is not present, the input should be read from standard
4008 If they are variables, this variable is a string containing a Perl
4009 implementation for this functionality. This Perl program will be transferred
4010 to the remote host, and it is available as shell function with the same name.
4012 The optional TEST command can be used for further tests, whether
4013 ENCODING and DECODING are applicable.")
4015 (defun tramp-find-inline-encoding (vec)
4016 "Find an inline transfer encoding that works.
4017 Goes through the list `tramp-local-coding-commands' and
4018 `tramp-remote-coding-commands'."
4020 (let ((local-commands tramp-local-coding-commands
)
4022 (p (tramp-get-connection-process vec
))
4023 loc-enc loc-dec rem-enc rem-dec rem-test litem ritem found
)
4024 (while (and local-commands
(not found
))
4025 (setq litem
(pop local-commands
))
4026 (catch 'wont-work-local
4027 (let ((format (nth 0 litem
))
4028 (remote-commands tramp-remote-coding-commands
))
4029 (setq loc-enc
(nth 1 litem
))
4030 (setq loc-dec
(nth 2 litem
))
4031 ;; If the local encoder or decoder is a string, the
4032 ;; corresponding command has to work locally.
4033 (if (not (stringp loc-enc
))
4035 vec
5 "Checking local encoding function `%s'" loc-enc
)
4037 vec
5 "Checking local encoding command `%s' for sanity" loc-enc
)
4038 (unless (zerop (tramp-call-local-coding-command
4040 (throw 'wont-work-local nil
)))
4041 (if (not (stringp loc-dec
))
4043 vec
5 "Checking local decoding function `%s'" loc-dec
)
4045 vec
5 "Checking local decoding command `%s' for sanity" loc-dec
)
4046 (unless (zerop (tramp-call-local-coding-command
4048 (throw 'wont-work-local nil
)))
4049 ;; Search for remote coding commands with the same format
4050 (while (and remote-commands
(not found
))
4051 (setq ritem
(pop remote-commands
))
4052 (catch 'wont-work-remote
4053 (when (equal format
(nth 0 ritem
))
4054 (setq rem-enc
(nth 1 ritem
))
4055 (setq rem-dec
(nth 2 ritem
))
4056 (setq rem-test
(nth 3 ritem
))
4057 ;; Check the remote test command if exists.
4058 (when (stringp rem-test
)
4060 vec
5 "Checking remote test command `%s'" rem-test
)
4061 (unless (tramp-send-command-and-check vec rem-test t
)
4062 (throw 'wont-work-remote nil
)))
4063 ;; Check if remote encoding and decoding commands can be
4064 ;; called remotely with null input and output. This makes
4065 ;; sure there are no syntax errors and the command is really
4066 ;; found. Note that we do not redirect stdout to /dev/null,
4067 ;; for two reasons: when checking the decoding command, we
4068 ;; actually check the output it gives. And also, when
4069 ;; redirecting "mimencode" output to /dev/null, then as root
4070 ;; it might change the permissions of /dev/null!
4071 (when (not (stringp rem-enc
))
4072 (let ((name (symbol-name rem-enc
)))
4073 (while (string-match (regexp-quote "-") name
)
4074 (setq name
(replace-match "_" nil t name
)))
4075 (tramp-maybe-send-script vec
(symbol-value rem-enc
) name
)
4076 (setq rem-enc name
)))
4079 "Checking remote encoding command `%s' for sanity" rem-enc
)
4080 (unless (tramp-send-command-and-check
4081 vec
(format "%s </dev/null" rem-enc
) t
)
4082 (throw 'wont-work-remote nil
))
4084 (when (not (stringp rem-dec
))
4085 (let ((name (symbol-name rem-dec
)))
4086 (while (string-match (regexp-quote "-") name
)
4087 (setq name
(replace-match "_" nil t name
)))
4088 (tramp-maybe-send-script vec
(symbol-value rem-dec
) name
)
4089 (setq rem-dec name
)))
4092 "Checking remote decoding command `%s' for sanity" rem-dec
)
4093 (unless (tramp-send-command-and-check
4095 (format "echo %s | %s | %s" magic rem-enc rem-dec
)
4097 (throw 'wont-work-remote nil
))
4099 (with-current-buffer (tramp-get-buffer vec
)
4100 (goto-char (point-min))
4101 (unless (looking-at (regexp-quote magic
))
4102 (throw 'wont-work-remote nil
)))
4104 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
4105 (setq rem-enc
(nth 1 ritem
))
4106 (setq rem-dec
(nth 2 ritem
))
4107 (setq found t
)))))))
4109 ;; Did we find something?
4112 vec
'file-error
"Couldn't find an inline transfer encoding"))
4114 ;; Set connection properties. Since the commands are risky (due
4115 ;; to output direction), we cache them in the process cache.
4116 (tramp-message vec
5 "Using local encoding `%s'" loc-enc
)
4117 (tramp-set-connection-property p
"local-encoding" loc-enc
)
4118 (tramp-message vec
5 "Using local decoding `%s'" loc-dec
)
4119 (tramp-set-connection-property p
"local-decoding" loc-dec
)
4120 (tramp-message vec
5 "Using remote encoding `%s'" rem-enc
)
4121 (tramp-set-connection-property p
"remote-encoding" rem-enc
)
4122 (tramp-message vec
5 "Using remote decoding `%s'" rem-dec
)
4123 (tramp-set-connection-property p
"remote-decoding" rem-dec
))))
4125 (defun tramp-call-local-coding-command (cmd input output
)
4126 "Call the local encoding or decoding command.
4127 If CMD contains \"%s\", provide input file INPUT there in command.
4128 Otherwise, INPUT is passed via standard input.
4129 INPUT can also be nil which means `/dev/null'.
4130 OUTPUT can be a string (which specifies a filename), or t (which
4131 means standard output and thus the current buffer), or nil (which
4134 tramp-encoding-shell
4135 (when (and input
(not (string-match "%s" cmd
))) input
)
4136 (if (eq output t
) t nil
)
4138 tramp-encoding-command-switch
4140 (if (string-match "%s" cmd
) (format cmd input
) cmd
)
4141 (if (stringp output
) (concat " >" output
) ""))))
4143 (defconst tramp-inline-compress-commands
4144 '(("gzip" "gzip -d")
4145 ("bzip2" "bzip2 -d")
4147 ("compress" "compress -d"))
4148 "List of compress and decompress commands for inline transfer.
4149 Each item is a list that looks like this:
4151 \(COMPRESS DECOMPRESS\)
4153 COMPRESS or DECOMPRESS are strings with the respective commands.")
4155 (defun tramp-find-inline-compress (vec)
4156 "Find an inline transfer compress command that works.
4157 Goes through the list `tramp-inline-compress-commands'."
4159 (let ((commands tramp-inline-compress-commands
)
4161 (p (tramp-get-connection-process vec
))
4162 item compress decompress found
)
4163 (while (and commands
(not found
))
4165 (setq item
(pop commands
)
4166 compress
(nth 0 item
)
4167 decompress
(nth 1 item
))
4170 "Checking local compress commands `%s', `%s' for sanity"
4171 compress decompress
)
4174 (tramp-call-local-coding-command
4176 ;; Windows shells need the program file name after
4177 ;; the pipe symbol be quoted if they use forward
4178 ;; slashes as directory separators.
4179 (if (memq system-type
'(windows-nt))
4180 "echo %s | \"%s\" | \"%s\""
4181 "echo %s | %s | %s")
4182 magic compress decompress
) nil nil
))
4186 "Checking remote compress commands `%s', `%s' for sanity"
4187 compress decompress
)
4188 (unless (tramp-send-command-and-check
4189 vec
(format "echo %s | %s | %s" magic compress decompress
) t
)
4193 ;; Did we find something?
4196 ;; Set connection properties. Since the commands are
4197 ;; risky (due to output direction), we cache them in the
4200 vec
5 "Using inline transfer compress command `%s'" compress
)
4201 (tramp-set-connection-property p
"inline-compress" compress
)
4203 vec
5 "Using inline transfer decompress command `%s'" decompress
)
4204 (tramp-set-connection-property p
"inline-decompress" decompress
))
4206 (tramp-set-connection-property p
"inline-compress" nil
)
4207 (tramp-set-connection-property p
"inline-decompress" nil
)
4209 vec
2 "Couldn't find an inline transfer compress command")))))
4211 (defun tramp-compute-multi-hops (vec)
4212 "Expands VEC according to `tramp-default-proxies-alist'.
4213 Gateway hops are already opened."
4214 (let ((target-alist `(,vec
))
4215 (hops (or (tramp-file-name-hop vec
) ""))
4219 ;; Ad-hoc proxy definitions.
4220 (dolist (proxy (reverse (split-string hops tramp-postfix-hop-regexp
'omit
)))
4221 (let ((user (tramp-file-name-user item
))
4222 (host (tramp-file-name-host item
))
4224 tramp-prefix-format proxy tramp-postfix-host-format
)))
4226 vec
5 "Add proxy (\"%s\" \"%s\" \"%s\")"
4227 (and (stringp host
) (regexp-quote host
))
4228 (and (stringp user
) (regexp-quote user
))
4232 'tramp-default-proxies-alist
4233 (list (and (stringp host
) (regexp-quote host
))
4234 (and (stringp user
) (regexp-quote user
))
4236 (setq item
(tramp-dissect-file-name proxy
))))
4237 ;; Save the new value.
4238 (when (and hops tramp-save-ad-hoc-proxies
)
4239 (customize-save-variable
4240 'tramp-default-proxies-alist tramp-default-proxies-alist
))
4242 ;; Look for proxy hosts to be passed.
4243 (setq choices tramp-default-proxies-alist
)
4245 (setq item
(pop choices
)
4246 proxy
(eval (nth 2 item
)))
4249 (string-match (or (eval (nth 0 item
)) "")
4250 (or (tramp-file-name-host (car target-alist
)) ""))
4252 (string-match (or (eval (nth 1 item
)) "")
4253 (or (tramp-file-name-user (car target-alist
)) "")))
4255 ;; No more hops needed.
4257 ;; Replace placeholders.
4262 ?u
(or (tramp-file-name-user (car target-alist
)) "")
4263 ?h
(or (tramp-file-name-host (car target-alist
)) ""))))
4264 (with-parsed-tramp-file-name proxy l
4266 (push l target-alist
)
4267 ;; Start next search.
4268 (setq choices tramp-default-proxies-alist
)))))
4271 (when (and (boundp 'tramp-gw-tunnel-method
) (boundp 'tramp-gw-socks-method
)
4274 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method
)
4275 (tramp-file-name-method (car target-alist
))))
4276 (let ((gw (pop target-alist
))
4277 (hop (pop target-alist
)))
4278 ;; Is the method prepared for gateways?
4279 (unless (tramp-file-name-port hop
)
4282 "Connection `%s' is not supported for gateway access." hop
))
4283 ;; Open the gateway connection.
4286 (tramp-file-name-method hop
) (tramp-file-name-user hop
)
4287 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop
) nil nil
)
4289 ;; For the password prompt, we need the correct values.
4290 ;; Therefore, we must remember the gateway vector. But we
4291 ;; cannot do it as connection property, because it shouldn't
4292 ;; be persistent. And we have no started process yet either.
4293 (tramp-set-file-property (car target-alist
) "" "gateway" hop
)))
4295 ;; Foreign and out-of-band methods are not supported for multi-hops.
4296 (when (cdr target-alist
)
4297 (setq choices target-alist
)
4299 (setq item
(pop choices
))
4303 (tramp-get-method-parameter
4304 (tramp-file-name-method item
) 'tramp-login-program
))
4305 (tramp-get-method-parameter
4306 (tramp-file-name-method item
) 'tramp-copy-program
))
4309 "Method `%s' is not supported for multi-hops."
4310 (tramp-file-name-method item
)))))
4312 ;; In case the host name is not used for the remote shell
4313 ;; command, the user could be misguided by applying a random
4315 (let* ((v (car target-alist
))
4316 (method (tramp-file-name-method v
))
4317 (host (tramp-file-name-host v
)))
4320 ;; There are multi-hops.
4322 ;; The host name is used for the remote shell command.
4324 '("%h") (tramp-get-method-parameter method
'tramp-login-args
))
4325 ;; The host is local. We cannot use `tramp-local-host-p'
4326 ;; here, because it opens a connection as well.
4327 (string-match tramp-local-host-regexp host
))
4330 "Host `%s' looks like a remote host, `%s' can only use the local host"
4336 (defun tramp-maybe-open-connection (vec)
4337 "Maybe open a connection VEC.
4338 Does not do anything if a connection is already open, but re-opens the
4339 connection if a previous connection has died for some reason."
4340 (tramp-check-proper-method-and-host vec
)
4342 (let ((p (tramp-get-connection-process vec
))
4343 (process-name (tramp-get-connection-property vec
"process-name" nil
))
4344 (process-environment (copy-sequence process-environment
))
4345 (pos (with-current-buffer (tramp-get-connection-buffer vec
) (point))))
4347 ;; If Tramp opens the same connection within a short time frame,
4348 ;; there is a problem. We shall signal this.
4349 (unless (or (and p
(processp p
) (memq (process-status p
) '(run open
)))
4350 (not (equal (butlast (append vec nil
) 2)
4351 (car tramp-current-connection
)))
4353 (current-time) (cdr tramp-current-connection
))
4354 (or tramp-connection-min-time-diff
0)))
4355 (throw 'suppress
'suppress
))
4357 ;; If too much time has passed since last command was sent, look
4358 ;; whether process is still alive. If it isn't, kill it. When
4359 ;; using ssh, it can sometimes happen that the remote end has hung
4360 ;; up but the local ssh client doesn't recognize this until it
4361 ;; tries to send some data to the remote end. So that's why we
4362 ;; try to send a command from time to time, then look again
4363 ;; whether the process is really alive.
4365 (when (and (> (tramp-time-diff
4367 (tramp-get-connection-property
4368 p
"last-cmd-time" '(0 0 0)))
4370 p
(processp p
) (memq (process-status p
) '(run open
)))
4371 (tramp-send-command vec
"echo are you awake" t t
)
4372 (unless (and (memq (process-status p
) '(run open
))
4373 (tramp-wait-for-output p
10))
4374 ;; The error will be caught locally.
4375 (tramp-error vec
'file-error
"Awake did fail")))
4377 (tramp-cleanup-connection vec t
)
4380 ;; New connection must be opened.
4382 (unless (and p
(processp p
) (memq (process-status p
) '(run open
)))
4384 ;; If `non-essential' is non-nil, don't reopen a new connection.
4385 (when (and (boundp 'non-essential
) (symbol-value 'non-essential
))
4386 (throw 'non-essential
'non-essential
))
4388 (with-tramp-progress-reporter
4390 (if (zerop (length (tramp-file-name-user vec
)))
4391 (format "Opening connection for %s using %s"
4392 (tramp-file-name-host vec
)
4393 (tramp-file-name-method vec
))
4394 (format "Opening connection for %s@%s using %s"
4395 (tramp-file-name-user vec
)
4396 (tramp-file-name-host vec
)
4397 (tramp-file-name-method vec
)))
4399 (catch 'uname-changed
4400 ;; Start new process.
4401 (when (and p
(processp p
))
4403 (setenv "TERM" tramp-terminal-type
)
4404 (setenv "LC_ALL" "en_US.utf8")
4405 (setenv "PROMPT_COMMAND")
4406 (setenv "PS1" tramp-initial-end-of-output
)
4407 (let* ((target-alist (tramp-compute-multi-hops vec
))
4408 ;; We will apply `tramp-ssh-controlmaster-options'
4409 ;; only for the first hop.
4410 (options (if tramp-use-ssh-controlmaster-options
4411 tramp-ssh-controlmaster-options
""))
4412 (process-connection-type tramp-process-connection-type
)
4413 (process-adaptive-read-buffering nil
)
4414 (coding-system-for-read nil
)
4415 ;; This must be done in order to avoid our file
4417 (p (let ((default-directory
4418 (tramp-compat-temporary-file-directory)))
4421 (tramp-get-connection-name vec
)
4422 (tramp-get-connection-buffer vec
)
4423 (if tramp-encoding-command-interactive
4424 (list tramp-encoding-shell
4425 tramp-encoding-command-interactive
)
4426 (list tramp-encoding-shell
))))))
4428 ;; Set sentinel and query flag.
4429 (tramp-set-connection-property p
"vector" vec
)
4430 (set-process-sentinel p
'tramp-process-sentinel
)
4431 (tramp-compat-set-process-query-on-exit-flag p nil
)
4432 (setq tramp-current-connection
4433 (cons (butlast (append vec nil
) 2) (current-time))
4434 tramp-current-host
(system-name))
4437 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
4439 ;; Check whether process is alive.
4440 (tramp-barf-if-no-shell-prompt
4442 "Couldn't find local shell prompt for %s" tramp-encoding-shell
)
4444 ;; Now do all the connections as specified.
4446 (let* ((hop (car target-alist
))
4447 (l-method (tramp-file-name-method hop
))
4448 (l-user (tramp-file-name-user hop
))
4449 (l-host (tramp-file-name-host hop
))
4452 (tramp-get-method-parameter
4453 l-method
'tramp-login-program
))
4455 (tramp-get-method-parameter
4456 l-method
'tramp-login-args
))
4458 (tramp-get-method-parameter
4459 l-method
'tramp-async-args
))
4461 (tramp-get-method-parameter
4462 l-method
'tramp-connection-timeout
))
4464 (tramp-get-method-parameter l-method
'tramp-gw-args
))
4465 (gw (tramp-get-file-property hop
"" "gateway" nil
))
4466 (g-method (and gw
(tramp-file-name-method gw
)))
4467 (g-user (and gw
(tramp-file-name-user gw
)))
4468 (g-host (and gw
(tramp-file-name-real-host gw
)))
4469 (command login-program
)
4470 ;; We don't create the temporary file. In
4471 ;; fact, it is just a prefix for the
4472 ;; ControlPath option of ssh; the real
4473 ;; temporary file has another name, and it is
4474 ;; created and protected by ssh. It is also
4475 ;; removed by ssh when the connection is
4476 ;; closed. The temporary file name is cached
4477 ;; in the main connection process, therefore
4478 ;; we cannot use `tramp-get-connection-process'.
4480 (with-tramp-connection-property
4481 (get-process (tramp-buffer-name vec
)) "temp-file"
4484 tramp-temp-name-prefix
4485 (tramp-compat-temporary-file-directory)))))
4488 ;; Add arguments for asynchronous processes.
4489 (when (and process-name async-args
)
4490 (setq login-args
(append async-args login-args
)))
4492 ;; Add gateway arguments if necessary.
4493 (when (and gw gw-args
)
4494 (setq login-args
(append gw-args login-args
)))
4496 ;; Check for port number. Until now, there's no
4497 ;; need for handling like method, user, host.
4498 (when (string-match tramp-host-with-port-regexp l-host
)
4499 (setq l-port
(match-string 2 l-host
)
4500 l-host
(match-string 1 l-host
)))
4502 ;; Check, whether there is a restricted shell.
4503 (dolist (elt tramp-restricted-shell-hosts-alist
)
4504 (when (string-match elt tramp-current-host
)
4507 ;; Set variables for computing the prompt for
4508 ;; reading password. They can also be derived
4510 (setq tramp-current-method
(or g-method l-method
)
4511 tramp-current-user
(or g-user l-user
)
4512 tramp-current-host
(or g-host l-host
))
4514 ;; Replace `login-args' place holders.
4516 l-host
(or l-host
"")
4517 l-user
(or l-user
"")
4518 l-port
(or l-port
"")
4519 spec
(format-spec-make ?t tmpfile
)
4520 options
(format-spec options spec
)
4521 spec
(format-spec-make
4522 ?h l-host ?u l-user ?p l-port ?c options
)
4525 ;; We do not want to see the trailing local
4526 ;; prompt in `start-file-process'.
4527 (unless r-shell
"exec ")
4531 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
4532 (unless (member "" x
) (mapconcat 'identity x
" ")))
4534 ;; Local shell could be a Windows COMSPEC. It
4535 ;; doesn't know the ";" syntax, but we must exit
4536 ;; always for `start-file-process'. It could
4537 ;; also be a restricted shell, which does not
4539 (when r-shell
" && exit || exit")))
4541 ;; Send the command.
4542 (tramp-message vec
3 "Sending command `%s'" command
)
4543 (tramp-send-command vec command t t
)
4544 (tramp-process-actions
4545 p vec pos tramp-actions-before-shell
4546 (or connection-timeout tramp-connection-timeout
))
4548 vec
3 "Found remote shell prompt on `%s'" l-host
))
4551 target-alist
(cdr target-alist
)))
4553 ;; Make initial shell settings.
4554 (tramp-open-connection-setup-interactive-shell p vec
)))))
4556 ;; When the user did interrupt, we must cleanup.
4558 (tramp-cleanup-connection vec t
)
4559 ;; Propagate the quit signal.
4560 (signal (car err
) (cdr err
))))))
4562 (defun tramp-send-command (vec command
&optional neveropen nooutput
)
4563 "Send the COMMAND to connection VEC.
4564 Erases temporary buffer before sending the command. If optional
4565 arg NEVEROPEN is non-nil, never try to open the connection. This
4566 is meant to be used from `tramp-maybe-open-connection' only. The
4567 function waits for output unless NOOUTPUT is set."
4568 (unless neveropen
(tramp-maybe-open-connection vec
))
4569 (let ((p (tramp-get-connection-process vec
)))
4570 (when (tramp-get-connection-property p
"remote-echo" nil
)
4571 ;; We mark the command string that it can be erased in the output buffer.
4572 (tramp-set-connection-property p
"check-remote-echo" t
)
4573 (setq command
(format "%s%s%s" tramp-echo-mark command tramp-echo-mark
)))
4574 ;; Send the command.
4575 (tramp-message vec
6 "%s" command
)
4576 (tramp-send-string vec command
)
4577 (unless nooutput
(tramp-wait-for-output p
))))
4579 (defun tramp-wait-for-output (proc &optional timeout
)
4580 "Wait for output from remote command."
4581 (unless (buffer-live-p (process-buffer proc
))
4582 (delete-process proc
)
4583 (tramp-error proc
'file-error
"Process `%s' not available, try again" proc
))
4584 (with-current-buffer (process-buffer proc
)
4585 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
4586 ;; be leading escape sequences, which must be ignored.
4587 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output
)))
4588 ;; Sometimes, the commands do not return a newline but a
4589 ;; null byte before the shell prompt, for example "git
4590 ;; ls-files -c -z ...".
4591 (regexp1 (format "\\(^\\|\000\\)%s" regexp
))
4592 (found (tramp-wait-for-regexp proc timeout regexp1
)))
4594 (let (buffer-read-only)
4595 ;; A simple-minded busybox has sent " ^H" sequences.
4597 (goto-char (point-min))
4598 (when (re-search-forward "^\\(.\b\\)+$" (point-at-eol) t
)
4600 (delete-region (point-min) (point)))
4601 ;; Delete the prompt.
4602 (goto-char (point-max))
4603 (re-search-backward regexp nil t
)
4604 (delete-region (point) (point-max)))
4608 "[[Remote prompt `%s' not found in %d secs]]"
4609 tramp-end-of-output timeout
)
4612 "[[Remote prompt `%s' not found]]" tramp-end-of-output
)))
4613 ;; Return value is whether end-of-output sentinel was found.
4616 (defun tramp-send-command-and-check
4617 (vec command
&optional subshell dont-suppress-err
)
4618 "Run COMMAND and check its exit status.
4619 Sends `echo $?' along with the COMMAND for checking the exit status. If
4620 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
4622 If the optional argument SUBSHELL is non-nil, the command is
4623 executed in a subshell, ie surrounded by parentheses. If
4624 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
4627 (concat (if subshell
"( " "")
4629 (if command
(if dont-suppress-err
"; " " 2>/dev/null; ") "")
4630 "echo tramp_exit_status $?"
4631 (if subshell
" )" "")))
4632 (with-current-buffer (tramp-get-connection-buffer vec
)
4633 (goto-char (point-max))
4634 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
4636 vec
'file-error
"Couldn't find exit status of `%s'" command
))
4637 (skip-chars-forward "^ ")
4639 (zerop (read (current-buffer)))
4640 (let (buffer-read-only)
4641 (delete-region (match-beginning 0) (point-max))))))
4643 (defun tramp-barf-unless-okay (vec command fmt
&rest args
)
4644 "Run COMMAND, check exit status, throw error if exit status not okay.
4645 Similar to `tramp-send-command-and-check' but accepts two more arguments
4646 FMT and ARGS which are passed to `error'."
4647 (or (tramp-send-command-and-check vec command
)
4648 (apply 'tramp-error vec
'file-error fmt args
)))
4650 (defun tramp-send-command-and-read (vec command
&optional noerror
)
4651 "Run COMMAND and return the output, which must be a Lisp expression.
4652 In case there is no valid Lisp expression and NOERROR is nil, it
4655 (tramp-send-command-and-check vec command
)
4656 (tramp-barf-unless-okay
4657 vec command
"`%s' returns with error" command
))
4658 (with-current-buffer (tramp-get-connection-buffer vec
)
4659 ;; Read the expression.
4660 (goto-char (point-min))
4662 (prog1 (read (current-buffer))
4664 (when (re-search-forward "\\S-" (point-at-eol) t
)
4666 (error (unless noerror
4669 "`%s' does not return a valid Lisp expression: `%s'"
4670 command
(buffer-string))))))))
4672 (defun tramp-convert-file-attributes (vec attr
)
4673 "Convert `file-attributes' ATTR generated by perl script, stat or ls.
4674 Convert file mode bits to string and set virtual device number.
4677 ;; Remove color escape sequences from symlink.
4678 (when (stringp (car attr
))
4679 (while (string-match tramp-color-escape-sequence-regexp
(car attr
))
4680 (setcar attr
(replace-match "" nil nil
(car attr
)))))
4681 ;; Convert uid and gid. Use -1 as indication of unusable value.
4682 (when (and (numberp (nth 2 attr
)) (< (nth 2 attr
) 0))
4683 (setcar (nthcdr 2 attr
) -
1))
4684 (when (and (floatp (nth 2 attr
))
4685 (<= (nth 2 attr
) (tramp-compat-most-positive-fixnum)))
4686 (setcar (nthcdr 2 attr
) (round (nth 2 attr
))))
4687 (when (and (numberp (nth 3 attr
)) (< (nth 3 attr
) 0))
4688 (setcar (nthcdr 3 attr
) -
1))
4689 (when (and (floatp (nth 3 attr
))
4690 (<= (nth 3 attr
) (tramp-compat-most-positive-fixnum)))
4691 (setcar (nthcdr 3 attr
) (round (nth 3 attr
))))
4692 ;; Convert last access time.
4693 (unless (listp (nth 4 attr
))
4694 (setcar (nthcdr 4 attr
)
4695 (list (floor (nth 4 attr
) 65536)
4696 (floor (mod (nth 4 attr
) 65536)))))
4697 ;; Convert last modification time.
4698 (unless (listp (nth 5 attr
))
4699 (setcar (nthcdr 5 attr
)
4700 (list (floor (nth 5 attr
) 65536)
4701 (floor (mod (nth 5 attr
) 65536)))))
4702 ;; Convert last status change time.
4703 (unless (listp (nth 6 attr
))
4704 (setcar (nthcdr 6 attr
)
4705 (list (floor (nth 6 attr
) 65536)
4706 (floor (mod (nth 6 attr
) 65536)))))
4707 ;; Convert file size.
4708 (when (< (nth 7 attr
) 0)
4709 (setcar (nthcdr 7 attr
) -
1))
4710 (when (and (floatp (nth 7 attr
))
4711 (<= (nth 7 attr
) (tramp-compat-most-positive-fixnum)))
4712 (setcar (nthcdr 7 attr
) (round (nth 7 attr
))))
4713 ;; Convert file mode bits to string.
4714 (unless (stringp (nth 8 attr
))
4715 (setcar (nthcdr 8 attr
) (tramp-file-mode-from-int (nth 8 attr
)))
4716 (when (stringp (car attr
))
4717 (aset (nth 8 attr
) 0 ?l
)))
4718 ;; Convert directory indication bit.
4719 (when (string-match "^d" (nth 8 attr
))
4721 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
4722 (when (consp (car attr
))
4723 (if (and (stringp (caar attr
))
4724 (string-match ".+ -> .\\(.+\\)." (caar attr
)))
4725 (setcar attr
(match-string 1 (caar attr
)))
4727 ;; Set file's gid change bit.
4728 (setcar (nthcdr 9 attr
)
4729 (if (numberp (nth 3 attr
))
4730 (not (= (nth 3 attr
)
4731 (tramp-get-remote-gid vec
'integer
)))
4734 (tramp-get-remote-gid vec
'string
)))))
4736 (unless (listp (nth 10 attr
))
4737 (setcar (nthcdr 10 attr
)
4739 (cons (floor (nth 10 attr
) 65536)
4740 (floor (mod (nth 10 attr
) 65536)))
4741 ;; Inodes can be incredible huge. We must hide this.
4742 (error (tramp-get-inode vec
)))))
4743 ;; Set virtual device number.
4744 (setcar (nthcdr 11 attr
)
4745 (tramp-get-device vec
))
4748 (defun tramp-shell-case-fold (string)
4749 "Converts STRING to shell glob pattern which ignores case."
4752 (if (equal (downcase c
) (upcase c
))
4754 (format "[%c%c]" (downcase c
) (upcase c
))))
4758 (defun tramp-make-copy-program-file-name (vec)
4759 "Create a file name suitable to be passed to `rcp' and workalikes."
4760 (let ((user (tramp-file-name-user vec
))
4761 (host (tramp-file-name-real-host vec
))
4762 (localname (tramp-shell-quote-argument
4763 (tramp-file-name-localname vec
))))
4764 (if (not (zerop (length user
)))
4765 (format "%s@%s:%s" user host localname
)
4766 (format "%s:%s" host localname
))))
4768 (defun tramp-method-out-of-band-p (vec size
)
4769 "Return t if this is an out-of-band method, nil otherwise."
4771 ;; It shall be an out-of-band method.
4772 (tramp-get-method-parameter (tramp-file-name-method vec
) 'tramp-copy-program
)
4773 ;; There must be a size, otherwise the file doesn't exist.
4775 ;; Either the file size is large enough, or (in rare cases) there
4776 ;; does not exist a remote encoding.
4777 (or (null tramp-copy-size-limit
)
4778 (> size tramp-copy-size-limit
)
4779 (null (tramp-get-inline-coding vec
"remote-encoding" size
)))))
4781 ;; Variables local to connection.
4783 (defun tramp-get-remote-path (vec)
4784 (with-tramp-connection-property
4785 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
4786 ;; cache the result for the session only. Otherwise, the result
4787 ;; is cached persistently.
4788 (if (memq 'tramp-own-remote-path tramp-remote-path
)
4789 (tramp-get-connection-process vec
)
4792 (let* ((remote-path (copy-tree tramp-remote-path
))
4793 (elt1 (memq 'tramp-default-remote-path remote-path
))
4794 (elt2 (memq 'tramp-own-remote-path remote-path
))
4795 (default-remote-path
4798 (tramp-send-command-and-read
4799 vec
"echo \\\"`getconf PATH 2>/dev/null`\\\"" 'noerror
)
4800 ;; Default if "getconf" is not available.
4804 "`getconf PATH' not successful, using default value \"%s\"."
4810 (tramp-send-command-and-read vec
"echo \\\"$PATH\\\"")
4813 vec
3 "$PATH not set, ignoring `tramp-own-remote-path'.")
4816 ;; Replace place holder `tramp-default-remote-path'.
4820 (tramp-compat-split-string default-remote-path
":")
4822 (setq remote-path
(delq 'tramp-default-remote-path remote-path
)))
4824 ;; Replace place holder `tramp-own-remote-path'.
4828 (tramp-compat-split-string own-remote-path
":")
4830 (setq remote-path
(delq 'tramp-own-remote-path remote-path
)))
4832 ;; Remove double entries.
4833 (setq elt1 remote-path
)
4835 (while (and (car elt1
) (setq elt2
(member (car elt1
) (cdr elt1
))))
4837 (setq elt1
(cdr elt1
)))
4839 ;; Remove non-existing directories.
4847 (tramp-make-tramp-file-name
4848 (tramp-file-name-method vec
)
4849 (tramp-file-name-user vec
)
4850 (tramp-file-name-host vec
)
4855 (defun tramp-get-remote-locale (vec)
4856 (with-tramp-connection-property vec
"locale"
4857 (tramp-send-command vec
"locale -a")
4858 (let ((candidates '("en_US.utf8" "C.utf8" "C"))
4860 (with-current-buffer (tramp-get-connection-buffer vec
)
4862 (goto-char (point-min))
4863 (if (string-match (concat "^" (car candidates
) "$") (buffer-string))
4864 (setq locale
(car candidates
)
4866 (setq candidates
(cdr candidates
)))))
4868 (when locale
(format "LC_ALL=%s" locale
)))))
4870 (defun tramp-get-ls-command (vec)
4871 (with-tramp-connection-property vec
"ls"
4872 (tramp-message vec
5 "Finding a suitable `ls' command")
4875 (dolist (cmd '("ls" "gnuls" "gls"))
4876 (let ((dl (tramp-get-remote-path vec
))
4878 (while (and dl
(setq result
(tramp-find-executable vec cmd dl t t
)))
4879 ;; Check parameters. On busybox, "ls" output coloring is
4880 ;; enabled by default sometimes. So we try to disable it
4881 ;; when possible. $LS_COLORING is not supported there.
4882 ;; Some "ls" versions are sensible wrt the order of
4883 ;; arguments, they fail when "-al" is after the
4884 ;; "--color=never" argument (for example on FreeBSD).
4885 (when (tramp-send-command-and-check
4886 vec
(format "%s -lnd /" result
))
4887 (when (tramp-send-command-and-check
4889 "%s --color=never -al /dev/null" result
))
4890 (setq result
(concat result
" --color=never")))
4891 (throw 'ls-found result
))
4892 (setq dl
(cdr dl
))))))
4893 (tramp-error vec
'file-error
"Couldn't find a proper `ls' command"))))
4895 (defun tramp-get-ls-command-with-dired (vec)
4897 (with-tramp-connection-property vec
"ls-dired"
4898 (tramp-message vec
5 "Checking, whether `ls --dired' works")
4899 ;; Some "ls" versions are sensible wrt the order of arguments,
4900 ;; they fail when "-al" is after the "--dired" argument (for
4901 ;; example on FreeBSD).
4902 (tramp-send-command-and-check
4903 vec
(format "%s --dired -al /dev/null" (tramp-get-ls-command vec
))))))
4905 (defun tramp-get-test-command (vec)
4906 (with-tramp-connection-property vec
"test"
4907 (tramp-message vec
5 "Finding a suitable `test' command")
4908 (if (tramp-send-command-and-check vec
"test 0")
4910 (tramp-find-executable vec
"test" (tramp-get-remote-path vec
)))))
4912 (defun tramp-get-test-nt-command (vec)
4913 ;; Does `test A -nt B' work? Use abominable `find' construct if it
4914 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
4915 ;; for otherwise the shell crashes.
4916 (with-tramp-connection-property vec
"test-nt"
4920 vec
(format "( %s / -nt / )" (tramp-get-test-command vec
)))
4921 (with-current-buffer (tramp-get-buffer vec
)
4922 (goto-char (point-min))
4923 (when (looking-at (regexp-quote tramp-end-of-output
))
4924 (format "%s %%s -nt %%s" (tramp-get-test-command vec
)))))
4929 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
4930 (tramp-get-test-command vec
)))
4931 "tramp_test_nt %s %s"))))
4933 (defun tramp-get-file-exists-command (vec)
4934 (with-tramp-connection-property vec
"file-exists"
4935 (tramp-message vec
5 "Finding command to check if file exists")
4936 (tramp-find-file-exists-command vec
)))
4938 (defun tramp-get-remote-ln (vec)
4939 (with-tramp-connection-property vec
"ln"
4940 (tramp-message vec
5 "Finding a suitable `ln' command")
4941 (tramp-find-executable vec
"ln" (tramp-get-remote-path vec
))))
4943 (defun tramp-get-remote-perl (vec)
4944 (with-tramp-connection-property vec
"perl"
4945 (tramp-message vec
5 "Finding a suitable `perl' command")
4947 (or (tramp-find-executable vec
"perl5" (tramp-get-remote-path vec
))
4948 (tramp-find-executable
4949 vec
"perl" (tramp-get-remote-path vec
)))))
4950 ;; We must check also for some Perl modules.
4952 (with-tramp-connection-property vec
"perl-file-spec"
4953 (tramp-send-command-and-check
4954 vec
(format "%s -e 'use File::Spec;'" result
)))
4955 (with-tramp-connection-property vec
"perl-cwd-realpath"
4956 (tramp-send-command-and-check
4957 vec
(format "%s -e 'use Cwd \"realpath\";'" result
))))
4960 (defun tramp-get-remote-stat (vec)
4961 (with-tramp-connection-property vec
"stat"
4962 (tramp-message vec
5 "Finding a suitable `stat' command")
4963 (let ((result (tramp-find-executable
4964 vec
"stat" (tramp-get-remote-path vec
)))
4966 ;; Check whether stat(1) returns usable syntax. "%s" does not
4967 ;; work on older AIX systems.
4970 (tramp-send-command-and-read
4971 vec
(format "%s -c '(\"%%N\" %%s)' /" result
) 'noerror
))
4972 (unless (and (listp tmp
) (stringp (car tmp
))
4973 (string-match "^./.$" (car tmp
))
4974 (integerp (cadr tmp
)))
4978 (defun tramp-get-remote-readlink (vec)
4979 (with-tramp-connection-property vec
"readlink"
4980 (tramp-message vec
5 "Finding a suitable `readlink' command")
4981 (let ((result (tramp-find-executable
4982 vec
"readlink" (tramp-get-remote-path vec
))))
4984 (tramp-send-command-and-check
4985 vec
(format "%s --canonicalize-missing /" result
)))
4988 (defun tramp-get-remote-trash (vec)
4989 (with-tramp-connection-property vec
"trash"
4990 (tramp-message vec
5 "Finding a suitable `trash' command")
4991 (tramp-find-executable vec
"trash" (tramp-get-remote-path vec
))))
4993 (defun tramp-get-remote-gvfs-monitor-dir (vec)
4994 (with-tramp-connection-property vec
"gvfs-monitor-dir"
4995 (tramp-message vec
5 "Finding a suitable `gvfs-monitor-dir' command")
4996 (tramp-find-executable
4997 vec
"gvfs-monitor-dir" (tramp-get-remote-path vec
) t t
)))
4999 (defun tramp-get-remote-inotifywait (vec)
5000 (with-tramp-connection-property vec
"inotifywait"
5001 (tramp-message vec
5 "Finding a suitable `inotifywait' command")
5002 (tramp-find-executable vec
"inotifywait" (tramp-get-remote-path vec
) t t
)))
5004 (defun tramp-get-remote-id (vec)
5005 (with-tramp-connection-property vec
"id"
5006 (tramp-message vec
5 "Finding POSIX `id' command")
5008 (let ((dl (tramp-get-remote-path vec
))
5010 (while (and dl
(setq result
(tramp-find-executable vec
"id" dl t t
)))
5011 ;; Check POSIX parameter.
5012 (when (tramp-send-command-and-check vec
(format "%s -u" result
))
5013 (throw 'id-found result
))
5014 (setq dl
(cdr dl
)))))))
5016 (defun tramp-get-remote-uid-with-id (vec id-format
)
5017 (tramp-send-command-and-read
5019 (format "%s -u%s %s"
5020 (tramp-get-remote-id vec
)
5021 (if (equal id-format
'integer
) "" "n")
5022 (if (equal id-format
'integer
)
5023 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/"))))
5025 (defun tramp-get-remote-uid-with-perl (vec id-format
)
5026 (tramp-send-command-and-read
5028 (format "%s -le '%s'"
5029 (tramp-get-remote-perl vec
)
5030 (if (equal id-format
'integer
)
5032 "print \"\\\"\", scalar getpwuid($>), \"\\\"\""))))
5034 (defun tramp-get-remote-python (vec)
5035 (with-tramp-connection-property vec
"python"
5036 (tramp-message vec
5 "Finding a suitable `python' command")
5037 (tramp-find-executable vec
"python" (tramp-get-remote-path vec
))))
5039 (defun tramp-get-remote-uid-with-python (vec id-format
)
5040 (tramp-send-command-and-read
5042 (format "%s -c \"%s\""
5043 (tramp-get-remote-python vec
)
5044 (if (equal id-format
'integer
)
5045 "import os; print os.getuid()"
5046 "import os, pwd; print '\\\"' + pwd.getpwuid(os.getuid())[0] + '\\\"'"))))
5048 (defun tramp-get-remote-uid (vec id-format
)
5049 (with-tramp-connection-property vec
(format "uid-%s" id-format
)
5051 ((tramp-get-remote-id vec
)
5052 (tramp-get-remote-uid-with-id vec id-format
))
5053 ((tramp-get-remote-perl vec
)
5054 (tramp-get-remote-uid-with-perl vec id-format
))
5055 ((tramp-get-remote-python vec
)
5056 (tramp-get-remote-uid-with-python vec id-format
))
5058 vec
'file-error
"Cannot determine remote uid")))))
5059 ;; The command might not always return a number.
5060 (if (and (equal id-format
'integer
) (not (integerp res
))) -
1 res
))))
5062 (defun tramp-get-remote-gid-with-id (vec id-format
)
5063 (tramp-send-command-and-read
5065 (format "%s -g%s %s"
5066 (tramp-get-remote-id vec
)
5067 (if (equal id-format
'integer
) "" "n")
5068 (if (equal id-format
'integer
)
5069 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/"))))
5071 (defun tramp-get-remote-gid-with-perl (vec id-format
)
5072 (tramp-send-command-and-read
5074 (format "%s -le '%s'"
5075 (tramp-get-remote-perl vec
)
5076 (if (equal id-format
'integer
)
5077 "print ($)=~/(\\d+)/)"
5078 "print \"\\\"\", scalar getgrgid($)), \"\\\"\""))))
5080 (defun tramp-get-remote-gid-with-python (vec id-format
)
5081 (tramp-send-command-and-read
5083 (format "%s -c \"%s\""
5084 (tramp-get-remote-python vec
)
5085 (if (equal id-format
'integer
)
5086 "import os; print os.getgid()"
5087 "import os, grp; print '\\\"' + grp.getgrgid(os.getgid())[0] + '\\\"'"))))
5089 (defun tramp-get-remote-gid (vec id-format
)
5090 (with-tramp-connection-property vec
(format "gid-%s" id-format
)
5092 ((tramp-get-remote-id vec
)
5093 (tramp-get-remote-gid-with-id vec id-format
))
5094 ((tramp-get-remote-perl vec
)
5095 (tramp-get-remote-gid-with-perl vec id-format
))
5096 ((tramp-get-remote-python vec
)
5097 (tramp-get-remote-gid-with-python vec id-format
))
5099 vec
'file-error
"Cannot determine remote gid")))))
5100 ;; The command might not always return a number.
5101 (if (and (equal id-format
'integer
) (not (integerp res
))) -
1 res
))))
5103 ;; Some predefined connection properties.
5104 (defun tramp-get-inline-compress (vec prop size
)
5105 "Return the compress command related to PROP.
5106 PROP is either `inline-compress' or `inline-decompress'. SIZE is
5107 the length of the file to be compressed.
5109 If no corresponding command is found, nil is returned."
5110 (when (and (integerp tramp-inline-compress-start-size
)
5111 (> size tramp-inline-compress-start-size
))
5112 (with-tramp-connection-property (tramp-get-connection-process vec
) prop
5113 (tramp-find-inline-compress vec
)
5114 (tramp-get-connection-property
5115 (tramp-get-connection-process vec
) prop nil
))))
5117 (defun tramp-get-inline-coding (vec prop size
)
5118 "Return the coding command related to PROP.
5119 PROP is either `remote-encoding', `remote-decoding',
5120 `local-encoding' or `local-decoding'.
5122 SIZE is the length of the file to be coded. Depending on SIZE,
5123 compression might be applied.
5125 If no corresponding command is found, nil is returned.
5126 Otherwise, either a string is returned which contains a `%s' mark
5127 to be used for the respective input or output file; or a Lisp
5128 function cell is returned to be applied on a buffer."
5129 ;; We must catch the errors, because we want to return `nil', when
5130 ;; no inline coding is found.
5133 (with-tramp-connection-property
5134 (tramp-get-connection-process vec
) prop
5135 (tramp-find-inline-encoding vec
)
5136 (tramp-get-connection-property
5137 (tramp-get-connection-process vec
) prop nil
)))
5138 (prop1 (if (string-match "encoding" prop
)
5139 "inline-compress" "inline-decompress"))
5141 ;; The connection property might have been cached. So we must
5142 ;; send the script to the remote side - maybe.
5143 (when (and coding
(symbolp coding
) (string-match "remote" prop
))
5144 (let ((name (symbol-name coding
)))
5145 (while (string-match (regexp-quote "-") name
)
5146 (setq name
(replace-match "_" nil t name
)))
5147 (tramp-maybe-send-script vec
(symbol-value coding
) name
)
5148 (setq coding name
)))
5150 ;; Check for the `compress' command.
5151 (setq compress
(tramp-get-inline-compress vec prop1 size
))
5152 ;; Return the value.
5154 ((and compress
(symbolp coding
))
5155 (if (string-match "decompress" prop1
)
5158 (let ((coding-system-for-write 'binary
)
5159 (coding-system-for-read 'binary
))
5161 'call-process-region
(point-min) (point-max)
5162 (car (split-string ,compress
)) t t nil
5163 (cdr (split-string ,compress
)))))
5165 (let ((coding-system-for-write 'binary
)
5166 (coding-system-for-read 'binary
))
5168 'call-process-region beg end
5169 (car (split-string ,compress
)) t t nil
5170 (cdr (split-string ,compress
))))
5171 (,coding
(point-min) (point-max)))))
5174 ((and compress
(string-match "decoding" prop
))
5176 ;; Windows shells need the program file name after
5177 ;; the pipe symbol be quoted if they use forward
5178 ;; slashes as directory separators.
5180 ((and (string-match "local" prop
)
5181 (memq system-type
'(windows-nt)))
5183 ((string-match "local" prop
) "(%s | %s)")
5184 (t "(%s | %s >%%s)"))
5188 ;; Windows shells need the program file name after
5189 ;; the pipe symbol be quoted if they use forward
5190 ;; slashes as directory separators.
5191 (if (and (string-match "local" prop
)
5192 (memq system-type
'(windows-nt)))
5193 "(%s <%%s | \"%s\")"
5196 ((string-match "decoding" prop
)
5198 ((string-match "local" prop
) (format "%s" coding
))
5199 (t (format "%s >%%s" coding
))))
5201 (format "%s <%%s" coding
)))))))
5203 (add-hook 'tramp-unload-hook
5205 (unload-feature 'tramp-sh
'force
)))
5211 ;; * Don't use globbing for directories with many files, as this is
5212 ;; likely to produce long command lines, and some shells choke on
5213 ;; long command lines.
5214 ;; * Make it work for different encodings, and for different file name
5215 ;; encodings, too. (Daniel Pittman)
5216 ;; * Don't search for perl5 and perl. Instead, only search for perl and
5217 ;; then look if it's the right version (with `perl -v').
5218 ;; * When editing a remote CVS controlled file as a different user, VC
5219 ;; gets confused about the file locking status. Try to find out why
5220 ;; the workaround doesn't work.
5221 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5222 ;; until the last but one hop via `start-file-process'. Apply it
5223 ;; also for ftp and smb.
5224 ;; * WIBNI if we had a command "trampclient"? If I was editing in
5225 ;; some shell with root privileges, it would be nice if I could
5227 ;; trampclient filename.c
5228 ;; as an editor, and the _current_ shell would connect to an Emacs
5229 ;; server and would be used in an existing non-privileged Emacs
5230 ;; session for doing the editing in question.
5231 ;; That way, I need not tell Emacs my password again and be afraid
5232 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
5233 ;; once display a just typed password in the context of a keyboard
5234 ;; sequence prompt for a question immediately following in a shell
5235 ;; script run within Emacs -- nasty).
5236 ;; And if I have some ssh session running to a different computer,
5237 ;; having the possibility of passing a local file there to a local
5238 ;; Emacs session (in case I can arrange for a connection back) would
5240 ;; Likely the corresponding Tramp server should not allow the
5241 ;; equivalent of the emacsclient -eval option in order to make this
5242 ;; reasonably unproblematic. And maybe trampclient should have some
5243 ;; way of passing credentials, like by using an SSL socket or
5244 ;; something. (David Kastrup)
5245 ;; * Reconnect directly to a compliant shell without first going
5246 ;; through the user's default shell. (Pete Forman)
5247 ;; * How can I interrupt the remote process with a signal
5248 ;; (interrupt-process seems not to work)? (Markus Triska)
5249 ;; * Avoid the local shell entirely for starting remote processes. If
5250 ;; so, I think even a signal, when delivered directly to the local
5251 ;; SSH instance, would correctly be propagated to the remote process
5252 ;; automatically; possibly SSH would have to be started with
5253 ;; "-t". (Markus Triska)
5254 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5255 ;; isn't on the remote host. (Mark A. Hershberger)
5256 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
5257 ;; * Optimize out-of-band copying when both methods are scp-like (not
5259 ;; * Keep a second connection open for out-of-band methods like scp or
5261 ;; * Try telnet+curl as new method. It might be useful for busybox,
5262 ;; without built-in uuencode/uudecode.
5264 ;;; tramp-sh.el ends here