1 ;;; tramp-sh.el --- Tramp access functions for (s)sh-like connections
3 ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
5 ;; (copyright statements below in code to be updated with the above notice)
7 ;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
8 ;; Michael Albinus <michael.albinus@gmx.de>
9 ;; Keywords: comm, processes
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 (eval-when-compile (require 'cl
)) ; ignore-errors
32 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
33 ;; not sure at all that this is the right way to do it, but let's hope
34 ;; it works for now, and wait for a guru to point out the Right Way to
37 ;; (unless (fboundp 'dired-insert-set-properties)
38 ;; (fset 'dired-insert-set-properties 'ignore)))
39 ;; Gerd suggests this:
40 (eval-when-compile (require 'dired
))
41 ;; Note that dired is required at run-time, too, when it is needed.
42 ;; It is only needed on XEmacs for the function
43 ;; `dired-insert-set-properties'.
45 (defcustom tramp-inline-compress-start-size
4096
46 "*The minimum size of compressing where inline transfer.
47 When inline transfer, compress transferred data of file
48 whose size is this value or above (up to `tramp-copy-size-limit').
49 If it is nil, no compression at all will be applied."
51 :type
'(choice (const nil
) integer
))
53 (defcustom tramp-copy-size-limit
10240
54 "*The maximum file size where inline copying is preferred over an out-of-the-band copy.
55 If it is nil, inline out-of-the-band copy will be used without a check."
57 :type
'(choice (const nil
) integer
))
60 (defcustom tramp-terminal-type
"dumb"
61 "*Value of TERM environment variable for logging in to remote host.
62 Because Tramp wants to parse the output of the remote shell, it is easily
63 confused by ANSI color escape sequences and suchlike. Often, shell init
64 files conditionalize this setup based on the TERM environment variable."
68 (defconst tramp-color-escape-sequence-regexp
"\e[[;0-9]+m"
69 "Escape sequences produced by the \"ls\" command.")
71 ;; ksh on OpenBSD 4.5 requires that $PS1 contains a `#' character for
72 ;; root users. It uses the `$' character for other users. In order
73 ;; to guarantee a proper prompt, we use "#$ " for the prompt.
75 (defvar tramp-end-of-output
78 (md5 (concat (prin1-to-string process-environment
) (current-time-string))))
79 "String used to recognize end of output.
80 The '$' character at the end is quoted; the string cannot be
81 detected as prompt when being sent on echoing hosts, therefore.")
84 (defconst tramp-initial-end-of-output
"#$ "
85 "Prompt when establishing a connection.")
87 ;; Initialize `tramp-methods' with the supported methods.
89 (add-to-list 'tramp-methods
91 (tramp-login-program "rsh")
92 (tramp-login-args (("%h") ("-l" "%u")))
93 (tramp-remote-shell "/bin/sh")
94 (tramp-remote-shell-args ("-c"))
95 (tramp-copy-program "rcp")
96 (tramp-copy-args (("-p" "%k") ("-r")))
97 (tramp-copy-keep-date t
)
98 (tramp-copy-recursive t
)))
100 (add-to-list 'tramp-methods
102 (tramp-login-program "remsh")
103 (tramp-login-args (("%h") ("-l" "%u")))
104 (tramp-remote-shell "/bin/sh")
105 (tramp-remote-shell-args ("-c"))
106 (tramp-copy-program "rcp")
107 (tramp-copy-args (("-p" "%k")))
108 (tramp-copy-keep-date t
)))
110 (add-to-list 'tramp-methods
112 (tramp-login-program "ssh")
113 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
114 (tramp-async-args (("-q")))
115 (tramp-remote-shell "/bin/sh")
116 (tramp-remote-shell-args ("-c"))
117 (tramp-copy-program "scp")
118 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r")))
119 (tramp-copy-keep-date t
)
120 (tramp-copy-recursive t
)
121 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
122 ("-o" "UserKnownHostsFile=/dev/null")
123 ("-o" "StrictHostKeyChecking=no")))
124 (tramp-default-port 22)))
126 (add-to-list 'tramp-methods
128 (tramp-login-program "ssh")
129 (tramp-login-args (("-l" "%u") ("-p" "%p")
130 ("-1") ("-e" "none") ("%h")))
131 (tramp-async-args (("-q")))
132 (tramp-remote-shell "/bin/sh")
133 (tramp-remote-shell-args ("-c"))
134 (tramp-copy-program "scp")
135 (tramp-copy-args (("-1") ("-P" "%p") ("-p" "%k") ("-q") ("-r")))
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")
147 ("-2") ("-e" "none") ("%h")))
148 (tramp-async-args (("-q")))
149 (tramp-remote-shell "/bin/sh")
150 (tramp-remote-shell-args ("-c"))
151 (tramp-copy-program "scp")
152 (tramp-copy-args (("-2") ("-P" "%p") ("-p" "%k") ("-q") ("-r")))
153 (tramp-copy-keep-date t
)
154 (tramp-copy-recursive t
)
155 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
156 ("-o" "UserKnownHostsFile=/dev/null")
157 ("-o" "StrictHostKeyChecking=no")))
158 (tramp-default-port 22)))
160 (add-to-list 'tramp-methods
162 (tramp-login-program "ssh")
163 (tramp-login-args (("-l" "%u") ("-p" "%p")
164 ("-o" "ControlPath=%t.%%r@%%h:%%p")
165 ("-o" "ControlMaster=yes")
166 ("-e" "none") ("%h")))
167 (tramp-async-args (("-q")))
168 (tramp-remote-shell "/bin/sh")
169 (tramp-remote-shell-args ("-c"))
170 (tramp-copy-program "scp")
171 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r")
172 ("-o" "ControlPath=%t.%%r@%%h:%%p")
173 ("-o" "ControlMaster=auto")))
174 (tramp-copy-keep-date t
)
175 (tramp-copy-recursive t
)
176 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
177 ("-o" "UserKnownHostsFile=/dev/null")
178 ("-o" "StrictHostKeyChecking=no")))
179 (tramp-default-port 22)))
181 (add-to-list 'tramp-methods
183 (tramp-login-program "ssh")
184 (tramp-login-args (("-l" "%u") ("-p" "%p")
185 ("-e" "none") ("-t" "-t")
187 (tramp-async-args (("-q")))
188 (tramp-remote-shell "/bin/sh")
189 (tramp-remote-shell-args ("-c"))
190 (tramp-copy-program "scp")
191 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r")))
192 (tramp-copy-keep-date t
)
193 (tramp-copy-recursive t
)
194 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
195 ("-o" "UserKnownHostsFile=/dev/null")
196 ("-o" "StrictHostKeyChecking=no")))
197 (tramp-default-port 22)))
199 (add-to-list 'tramp-methods
201 (tramp-login-program "ssh")
202 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
203 (tramp-async-args (("-q")))
204 (tramp-remote-shell "/bin/sh")
205 (tramp-remote-shell-args ("-c"))
206 (tramp-copy-program "sftp")))
208 (add-to-list 'tramp-methods
210 (tramp-login-program "ssh")
211 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
212 (tramp-async-args (("-q")))
213 (tramp-remote-shell "/bin/sh")
214 (tramp-remote-shell-args ("-c"))
215 (tramp-copy-program "rsync")
216 (tramp-copy-args (("-e" "ssh") ("-t" "%k") ("-r")))
217 (tramp-copy-keep-date t
)
218 (tramp-copy-keep-tmpfile t
)
219 (tramp-copy-recursive t
)))
221 (add-to-list 'tramp-methods
223 (tramp-login-program "ssh")
224 (tramp-login-args (("-l" "%u") ("-p" "%p")
225 ("-o" "ControlPath=%t.%%r@%%h:%%p")
226 ("-o" "ControlMaster=yes")
227 ("-e" "none") ("%h")))
228 (tramp-async-args (("-q")))
229 (tramp-remote-shell "/bin/sh")
230 (tramp-remote-shell-args ("-c"))
231 (tramp-copy-program "rsync")
232 (tramp-copy-args (("-t" "%k") ("-r")))
233 (tramp-copy-env (("RSYNC_RSH")
236 " -o ControlPath=%t.%%r@%%h:%%p"
237 " -o ControlMaster=auto"))))
238 (tramp-copy-keep-date t
)
239 (tramp-copy-keep-tmpfile t
)
240 (tramp-copy-recursive t
)))
242 (add-to-list 'tramp-methods
244 (tramp-login-program "rsh")
245 (tramp-login-args (("%h") ("-l" "%u")))
246 (tramp-remote-shell "/bin/sh")
247 (tramp-remote-shell-args ("-c"))))
249 (add-to-list 'tramp-methods
251 (tramp-login-program "remsh")
252 (tramp-login-args (("%h") ("-l" "%u")))
253 (tramp-remote-shell "/bin/sh")
254 (tramp-remote-shell-args ("-c"))))
256 (add-to-list 'tramp-methods
258 (tramp-login-program "ssh")
259 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
260 (tramp-async-args (("-q")))
261 (tramp-remote-shell "/bin/sh")
262 (tramp-remote-shell-args ("-c"))
263 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
264 ("-o" "UserKnownHostsFile=/dev/null")
265 ("-o" "StrictHostKeyChecking=no")))
266 (tramp-default-port 22)))
268 (add-to-list 'tramp-methods
270 (tramp-login-program "ssh")
271 (tramp-login-args (("-l" "%u") ("-p" "%p")
272 ("-1") ("-e" "none") ("%h")))
273 (tramp-async-args (("-q")))
274 (tramp-remote-shell "/bin/sh")
275 (tramp-remote-shell-args ("-c"))
276 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
277 ("-o" "UserKnownHostsFile=/dev/null")
278 ("-o" "StrictHostKeyChecking=no")))
279 (tramp-default-port 22)))
281 (add-to-list 'tramp-methods
283 (tramp-login-program "ssh")
284 (tramp-login-args (("-l" "%u") ("-p" "%p")
285 ("-2") ("-e" "none") ("%h")))
286 (tramp-async-args (("-q")))
287 (tramp-remote-shell "/bin/sh")
288 (tramp-remote-shell-args ("-c"))
289 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
290 ("-o" "UserKnownHostsFile=/dev/null")
291 ("-o" "StrictHostKeyChecking=no")))
292 (tramp-default-port 22)))
294 (add-to-list 'tramp-methods
296 (tramp-login-program "ssh")
297 (tramp-login-args (("-l" "%u") ("-p" "%p")
298 ("-e" "none") ("-t" "-t")
300 (tramp-async-args (("-q")))
301 (tramp-remote-shell "/bin/sh")
302 (tramp-remote-shell-args ("-c"))
303 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
304 ("-o" "UserKnownHostsFile=/dev/null")
305 ("-o" "StrictHostKeyChecking=no")))
306 (tramp-default-port 22)))
308 (add-to-list 'tramp-methods
310 (tramp-login-program "telnet")
311 (tramp-login-args (("%h") ("%p")))
312 (tramp-remote-shell "/bin/sh")
313 (tramp-remote-shell-args ("-c"))
314 (tramp-default-port 23)))
316 (add-to-list 'tramp-methods
318 (tramp-login-program "su")
319 (tramp-login-args (("-") ("%u")))
320 (tramp-remote-shell "/bin/sh")
321 (tramp-remote-shell-args ("-c"))))
323 (add-to-list 'tramp-methods
325 (tramp-login-program "sudo")
326 (tramp-login-args (("-u" "%u") ("-s") ("-H") ("-p" "Password:")))
327 (tramp-remote-shell "/bin/sh")
328 (tramp-remote-shell-args ("-c"))))
330 (add-to-list 'tramp-methods
332 (tramp-login-program "ksu")
333 (tramp-login-args (("%u") ("-q")))
334 (tramp-remote-shell "/bin/sh")
335 (tramp-remote-shell-args ("-c"))))
337 (add-to-list 'tramp-methods
339 (tramp-login-program "krlogin")
340 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
341 (tramp-remote-shell "/bin/sh")
342 (tramp-remote-shell-args ("-c"))))
344 (add-to-list 'tramp-methods
346 (tramp-login-program "plink")
347 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
348 (tramp-remote-shell "/bin/sh")
349 (tramp-remote-shell-args ("-c"))
350 (tramp-password-end-of-line "xy") ;see docstring for "xy"
351 (tramp-default-port 22)))
353 (add-to-list 'tramp-methods
355 (tramp-login-program "plink")
356 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-1" "-ssh") ("%h")))
357 (tramp-remote-shell "/bin/sh")
358 (tramp-remote-shell-args ("-c"))
359 (tramp-password-end-of-line "xy") ;see docstring for "xy"
360 (tramp-default-port 22)))
362 (add-to-list 'tramp-methods
364 (tramp-login-program "plink")
365 ;; ("%h") must be a single element, see
366 ;; `tramp-compute-multi-hops'.
367 (tramp-login-args (("-load") ("%h") ("-t")
369 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
371 tramp-initial-end-of-output
))
373 (tramp-remote-shell "/bin/sh")
374 (tramp-remote-shell-args ("-c"))))
376 (add-to-list 'tramp-methods
378 (tramp-login-program "plink")
379 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
380 (tramp-remote-shell "/bin/sh")
381 (tramp-remote-shell-args ("-c"))
382 (tramp-copy-program "pscp")
383 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-scp") ("-p" "%k")
385 (tramp-copy-keep-date t
)
386 (tramp-copy-recursive t
)
387 (tramp-password-end-of-line "xy") ;see docstring for "xy"
388 (tramp-default-port 22)))
390 (add-to-list 'tramp-methods
392 (tramp-login-program "plink")
393 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
394 (tramp-remote-shell "/bin/sh")
395 (tramp-remote-shell-args ("-c"))
396 (tramp-copy-program "pscp")
397 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-sftp") ("-p" "%k")
399 (tramp-copy-keep-date t
)
400 (tramp-copy-recursive t
)
401 (tramp-password-end-of-line "xy"))) ;see docstring for "xy"
403 (add-to-list 'tramp-methods
405 (tramp-login-program "fsh")
406 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
407 (tramp-remote-shell "/bin/sh")
408 (tramp-remote-shell-args ("-i") ("-c"))
409 (tramp-copy-program "fcp")
410 (tramp-copy-args (("-p" "%k")))
411 (tramp-copy-keep-date t
)))
414 (add-to-list 'tramp-default-method-alist
415 `(,tramp-local-host-regexp
"\\`root\\'" "su"))
418 (add-to-list 'tramp-default-user-alist
419 `(,(concat "\\`" (regexp-opt '("su" "sudo" "ksu")) "\\'")
421 ;; Do not add "ssh" based methods, otherwise ~/.ssh/config would be ignored.
422 ;; Do not add "plink" based methods, they ask interactively for the user.
424 (add-to-list 'tramp-default-user-alist
427 (regexp-opt '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
429 nil
,(user-login-name)))
432 (defconst tramp-completion-function-alist-rsh
433 '((tramp-parse-rhosts "/etc/hosts.equiv")
434 (tramp-parse-rhosts "~/.rhosts"))
435 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
438 (defconst tramp-completion-function-alist-ssh
439 '((tramp-parse-rhosts "/etc/hosts.equiv")
440 (tramp-parse-rhosts "/etc/shosts.equiv")
441 (tramp-parse-shosts "/etc/ssh_known_hosts")
442 (tramp-parse-sconfig "/etc/ssh_config")
443 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
444 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
445 (tramp-parse-rhosts "~/.rhosts")
446 (tramp-parse-rhosts "~/.shosts")
447 (tramp-parse-shosts "~/.ssh/known_hosts")
448 (tramp-parse-sconfig "~/.ssh/config")
449 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
450 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
451 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
454 (defconst tramp-completion-function-alist-telnet
455 '((tramp-parse-hosts "/etc/hosts"))
456 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
459 (defconst tramp-completion-function-alist-su
460 '((tramp-parse-passwd "/etc/passwd"))
461 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
464 (defconst tramp-completion-function-alist-putty
466 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"))
467 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty methods.")
470 (eval-after-load 'tramp
472 (tramp-set-completion-function "rcp" tramp-completion-function-alist-rsh
)
473 (tramp-set-completion-function "remcp" tramp-completion-function-alist-rsh
)
474 (tramp-set-completion-function "scp" tramp-completion-function-alist-ssh
)
475 (tramp-set-completion-function "scp1" tramp-completion-function-alist-ssh
)
476 (tramp-set-completion-function "scp2" tramp-completion-function-alist-ssh
)
477 (tramp-set-completion-function "scpc" tramp-completion-function-alist-ssh
)
478 (tramp-set-completion-function "scpx" tramp-completion-function-alist-ssh
)
479 (tramp-set-completion-function "sftp" tramp-completion-function-alist-ssh
)
480 (tramp-set-completion-function "rsync" tramp-completion-function-alist-ssh
)
481 (tramp-set-completion-function
482 "rsyncc" tramp-completion-function-alist-ssh
)
483 (tramp-set-completion-function "rsh" tramp-completion-function-alist-rsh
)
484 (tramp-set-completion-function "remsh" tramp-completion-function-alist-rsh
)
485 (tramp-set-completion-function "ssh" tramp-completion-function-alist-ssh
)
486 (tramp-set-completion-function "ssh1" tramp-completion-function-alist-ssh
)
487 (tramp-set-completion-function "ssh2" tramp-completion-function-alist-ssh
)
488 (tramp-set-completion-function
489 "ssh1_old" tramp-completion-function-alist-ssh
)
490 (tramp-set-completion-function
491 "ssh2_old" tramp-completion-function-alist-ssh
)
492 (tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh
)
493 (tramp-set-completion-function
494 "telnet" tramp-completion-function-alist-telnet
)
495 (tramp-set-completion-function "su" tramp-completion-function-alist-su
)
496 (tramp-set-completion-function "sudo" tramp-completion-function-alist-su
)
497 (tramp-set-completion-function "ksu" tramp-completion-function-alist-su
)
498 (tramp-set-completion-function
499 "krlogin" tramp-completion-function-alist-rsh
)
500 (tramp-set-completion-function "plink" tramp-completion-function-alist-ssh
)
501 (tramp-set-completion-function
502 "plink1" tramp-completion-function-alist-ssh
)
503 (tramp-set-completion-function
504 "plinkx" tramp-completion-function-alist-putty
)
505 (tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh
)
506 (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh
)))
508 ;; "getconf PATH" yields:
509 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
510 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
511 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
512 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
515 (defcustom tramp-remote-path
516 '(tramp-default-remote-path "/bin" "/usr/bin" "/usr/sbin" "/usr/local/bin"
517 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
518 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
519 "*List of directories to search for executables on remote host.
520 For every remote host, this variable will be set buffer local,
521 keeping the list of existing directories on that host.
523 You can use `~' in this list, but when searching for a shell which groks
524 tilde expansion, all directory names starting with `~' will be ignored.
526 `Default Directories' represent the list of directories given by
527 the command \"getconf PATH\". It is recommended to use this
528 entry on top of this list, because these are the default
529 directories for POSIX compatible commands. On remote hosts which
530 do not offer the getconf command (like cygwin), the value
531 \"/bin:/usr/bin\" is used instead of.
533 `Private Directories' are the settings of the $PATH environment,
534 as given in your `~/.profile'."
536 :type
'(repeat (choice
537 (const :tag
"Default Directories" tramp-default-remote-path
)
538 (const :tag
"Private Directories" tramp-own-remote-path
)
539 (string :tag
"Directory"))))
541 (defcustom tramp-remote-process-environment
542 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "LC_ALL=C"
543 ,(format "TERM=%s" tramp-terminal-type
)
544 "EMACS=t" ;; Deprecated.
545 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version
)
546 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH=" "PAGER=\"\""
547 "autocorrect=" "correct=")
549 "*List of environment variables to be set on the remote host.
551 Each element should be a string of the form ENVVARNAME=VALUE. An
552 entry ENVVARNAME= disables the corresponding environment variable,
553 which might have been set in the init files like ~/.profile.
555 Special handling is applied to the PATH environment, which should
556 not be set here. Instead, it should be set via `tramp-remote-path'."
558 :type
'(repeat string
))
560 (defcustom tramp-sh-extra-args
'(("/bash\\'" .
"-norc -noprofile"))
561 "*Alist specifying extra arguments to pass to the remote shell.
562 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
563 matching the shell file name and ARGS is a string specifying the
566 This variable is only used when Tramp needs to start up another shell
567 for tilde expansion. The extra arguments should typically prevent the
568 shell from reading its init file."
570 ;; This might be the wrong way to test whether the widget type
571 ;; `alist' is available. Who knows the right way to test it?
572 :type
(if (get 'alist
'widget-type
)
573 '(alist :key-type string
:value-type string
)
574 '(repeat (cons string string
))))
576 (defconst tramp-actions-before-shell
577 '((tramp-login-prompt-regexp tramp-action-login
)
578 (tramp-password-prompt-regexp tramp-action-password
)
579 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
580 (shell-prompt-pattern tramp-action-succeed
)
581 (tramp-shell-prompt-pattern tramp-action-succeed
)
582 (tramp-yesno-prompt-regexp tramp-action-yesno
)
583 (tramp-yn-prompt-regexp tramp-action-yn
)
584 (tramp-terminal-prompt-regexp tramp-action-terminal
)
585 (tramp-process-alive-regexp tramp-action-process-alive
))
586 "List of pattern/action pairs.
587 Whenever a pattern matches, the corresponding action is performed.
588 Each item looks like (PATTERN ACTION).
590 The PATTERN should be a symbol, a variable. The value of this
591 variable gives the regular expression to search for. Note that the
592 regexp must match at the end of the buffer, \"\\'\" is implicitly
595 The ACTION should also be a symbol, but a function. When the
596 corresponding PATTERN matches, the ACTION function is called.")
598 (defconst tramp-actions-copy-out-of-band
599 '((tramp-password-prompt-regexp tramp-action-password
)
600 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
601 (tramp-copy-failed-regexp tramp-action-permission-denied
)
602 (tramp-process-alive-regexp tramp-action-out-of-band
))
603 "List of pattern/action pairs.
604 This list is used for copying/renaming with out-of-band methods.
606 See `tramp-actions-before-shell' for more info.")
608 (defconst tramp-uudecode
609 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
612 "Shell function to implement `uudecode' to standard output.
613 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
614 for this or `uudecode -p', but some systems don't, and for them
615 we have this shell function.")
617 (defconst tramp-perl-file-truename
620 use Cwd \"realpath\";
623 my ($volume, @dirs) = @_;
624 my $real = realpath(File::Spec->catpath(
625 $volume, File::Spec->catdir(@dirs), \"\"));
627 my ($vol, $dir) = File::Spec->splitpath($real, 1);
628 return ($vol, File::Spec->splitdir($dir));
631 my $last = pop(@dirs);
632 ($volume, @dirs) = recursive($volume, @dirs);
634 return ($volume, @dirs);
638 $result = realpath($ARGV[0]);
640 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
641 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
643 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
646 if ($ARGV[0] =~ /\\/$/) {
647 $result = $result . \"/\";
650 print \"\\\"$result\\\"\\n\";
651 ' \"$1\" 2>/dev/null"
652 "Perl script to produce output suitable for use with `file-truename'
653 on the remote file system.
654 Escape sequence %s is replaced with name of Perl binary.
655 This string is passed to `format', so percent characters need to be doubled.")
657 (defconst tramp-perl-file-name-all-completions
667 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
668 @files = readdir(d); closedir(d);
669 foreach $f (@files) {
670 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
671 if (-d \"$ARGV[0]/$f\") {
680 ' \"$1\" \"$2\" \"$3\" 2>/dev/null"
681 "Perl script to produce output suitable for use with
682 `file-name-all-completions' on the remote file system. Escape
683 sequence %s is replaced with name of Perl binary. This string is
684 passed to `format', so percent characters need to be doubled.")
686 ;; Perl script to implement `file-attributes' in a Lisp `read'able
687 ;; output. If you are hacking on this, note that you get *no* output
688 ;; unless this spits out a complete line, including the '\n' at the
690 ;; The device number is returned as "-1", because there will be a virtual
691 ;; device number set in `tramp-sh-handle-file-attributes'.
692 (defconst tramp-perl-file-attributes
694 @stat = lstat($ARGV[0]);
699 if (($stat[2] & 0170000) == 0120000)
701 $type = readlink($ARGV[0]);
702 $type = \"\\\"$type\\\"\";
704 elsif (($stat[2] & 0170000) == 040000)
712 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
713 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
715 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
720 $stat[8] >> 16 & 0xffff,
722 $stat[9] >> 16 & 0xffff,
724 $stat[10] >> 16 & 0xffff,
728 $stat[1] >> 16 & 0xffff,
730 );' \"$1\" \"$2\" 2>/dev/null"
731 "Perl script to produce output suitable for use with `file-attributes'
732 on the remote file system.
733 Escape sequence %s is replaced with name of Perl binary.
734 This string is passed to `format', so percent characters need to be doubled.")
736 (defconst tramp-perl-directory-files-and-attributes
738 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
739 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
740 @list = readdir(DIR);
744 for($i = 0; $i < $n; $i++)
746 $filename = $list[$i];
747 @stat = lstat($filename);
748 if (($stat[2] & 0170000) == 0120000)
750 $type = readlink($filename);
751 $type = \"\\\"$type\\\"\";
753 elsif (($stat[2] & 0170000) == 040000)
761 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
762 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
764 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
770 $stat[8] >> 16 & 0xffff,
772 $stat[9] >> 16 & 0xffff,
774 $stat[10] >> 16 & 0xffff,
778 $stat[1] >> 16 & 0xffff,
780 $stat[0] >> 16 & 0xffff,
783 printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
784 "Perl script implementing `directory-files-attributes' as Lisp `read'able
786 Escape sequence %s is replaced with name of Perl binary.
787 This string is passed to `format', so percent characters need to be doubled.")
789 ;; These two use base64 encoding.
790 (defconst tramp-perl-encode-with-module
791 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
792 "Perl program to use for encoding a file.
793 Escape sequence %s is replaced with name of Perl binary.
794 This string is passed to `format', so percent characters need to be doubled.
795 This implementation requires the MIME::Base64 Perl module to be installed
796 on the remote host.")
798 (defconst tramp-perl-decode-with-module
799 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
800 "Perl program to use for decoding a file.
801 Escape sequence %s is replaced with name of Perl binary.
802 This string is passed to `format', so percent characters need to be doubled.
803 This implementation requires the MIME::Base64 Perl module to be installed
804 on the remote host.")
806 (defconst tramp-perl-encode
808 # This script contributed by Juanma Barranquero <lektu@terra.es>.
809 # Copyright (C) 2002-2012 Free Software Foundation, Inc.
814 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
815 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
820 # We read in chunks of 54 bytes, to generate output lines
821 # of 72 chars (plus end of line)
824 while (my $data = <STDIN>) {
827 # Only for the last chunk, and only if did not fill the last three-byte packet
829 my $mod = length($data) %% 3;
830 $pad = q(=) x (3 - $mod) if $mod;
833 # Not the fastest method, but it is simple: unpack to binary string, split
834 # by groups of 6 bits and convert back from binary to byte; then map into
835 # the translation table
839 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
843 "Perl program to use for encoding a file.
844 Escape sequence %s is replaced with name of Perl binary.
845 This string is passed to `format', so percent characters need to be doubled.")
847 (defconst tramp-perl-decode
849 # This script contributed by Juanma Barranquero <lektu@terra.es>.
850 # Copyright (C) 2002-2012 Free Software Foundation, Inc.
855 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
856 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
859 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
863 # We are going to accumulate into $pending to accept any line length
864 # (we do not check they are <= 76 chars as the RFC says)
867 while (my $data = <STDIN>) {
870 # If we find one or two =, we have reached the end and
871 # any following data is to be discarded
872 my $finished = $data =~ s/(==?).*/$1/;
875 my $len = length($pending);
876 my $chunk = substr($pending, 0, $len & ~3);
877 $pending = substr($pending, $len & ~3 + 1);
879 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
880 # split in 8-bit chunks and convert back to char.
883 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
887 "Perl program to use for decoding a file.
888 Escape sequence %s is replaced with name of Perl binary.
889 This string is passed to `format', so percent characters need to be doubled.")
891 (defconst tramp-vc-registered-read-file-names
894 if %s \"$file\"; then
895 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
897 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
899 if %s \"$file\"; then
900 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
902 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
906 "Script to check existence of VC related files.
907 It must be send formatted with two strings; the tests for file
908 existence, and file readability. Input shall be read via
909 here-document, otherwise the command could exceed maximum length
912 (defconst tramp-file-mode-type-map
913 '((0 .
"-") ; Normal file (SVID-v2 and XPG2)
915 (2 .
"c") ; character device
916 (3 .
"m") ; multiplexed character device (v7)
917 (4 .
"d") ; directory
918 (5 .
"?") ; Named special file (XENIX)
919 (6 .
"b") ; block device
920 (7 .
"?") ; multiplexed block device (v7)
921 (8 .
"-") ; regular file
922 (9 .
"n") ; network special file (HP-UX)
924 (11 .
"?") ; ACL shadow inode (Solaris, not userspace)
926 (13 .
"D") ; door special (Solaris)
927 (14 .
"w")) ; whiteout (BSD)
928 "A list of file types returned from the `stat' system call.
929 This is used to map a mode number to a permission string.")
931 ;; New handlers should be added here. The following operations can be
932 ;; handled using the normal primitives: file-name-sans-versions,
934 (defconst tramp-sh-file-name-handler-alist
935 '((load . tramp-handle-load
)
936 (make-symbolic-link . tramp-sh-handle-make-symbolic-link
)
937 (file-name-as-directory . tramp-handle-file-name-as-directory
)
938 (file-name-directory . tramp-handle-file-name-directory
)
939 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
940 (file-truename . tramp-sh-handle-file-truename
)
941 (file-exists-p . tramp-sh-handle-file-exists-p
)
942 (file-directory-p . tramp-sh-handle-file-directory-p
)
943 (file-executable-p . tramp-sh-handle-file-executable-p
)
944 (file-readable-p . tramp-sh-handle-file-readable-p
)
945 (file-regular-p . tramp-handle-file-regular-p
)
946 (file-symlink-p . tramp-handle-file-symlink-p
)
947 (file-writable-p . tramp-sh-handle-file-writable-p
)
948 (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p
)
949 (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p
)
950 (file-attributes . tramp-sh-handle-file-attributes
)
951 (file-modes . tramp-handle-file-modes
)
952 (directory-files . tramp-handle-directory-files
)
953 (directory-files-and-attributes
954 . tramp-sh-handle-directory-files-and-attributes
)
955 (file-name-all-completions . tramp-sh-handle-file-name-all-completions
)
956 (file-name-completion . tramp-handle-file-name-completion
)
957 (add-name-to-file . tramp-sh-handle-add-name-to-file
)
958 (copy-file . tramp-sh-handle-copy-file
)
959 (copy-directory . tramp-sh-handle-copy-directory
)
960 (rename-file . tramp-sh-handle-rename-file
)
961 (set-file-modes . tramp-sh-handle-set-file-modes
)
962 (set-file-times . tramp-sh-handle-set-file-times
)
963 (make-directory . tramp-sh-handle-make-directory
)
964 (delete-directory . tramp-sh-handle-delete-directory
)
965 (delete-file . tramp-sh-handle-delete-file
)
966 (directory-file-name . tramp-handle-directory-file-name
)
967 ;; `executable-find' is not official yet.
968 (executable-find . tramp-sh-handle-executable-find
)
969 (start-file-process . tramp-sh-handle-start-file-process
)
970 (process-file . tramp-sh-handle-process-file
)
971 (shell-command . tramp-handle-shell-command
)
972 (insert-directory . tramp-sh-handle-insert-directory
)
973 (expand-file-name . tramp-sh-handle-expand-file-name
)
974 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
975 (file-local-copy . tramp-sh-handle-file-local-copy
)
976 (file-remote-p . tramp-handle-file-remote-p
)
977 (insert-file-contents . tramp-handle-insert-file-contents
)
978 (insert-file-contents-literally
979 . tramp-sh-handle-insert-file-contents-literally
)
980 (write-region . tramp-sh-handle-write-region
)
981 (find-backup-file-name . tramp-handle-find-backup-file-name
)
982 (make-auto-save-file-name . tramp-sh-handle-make-auto-save-file-name
)
983 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
984 (dired-compress-file . tramp-sh-handle-dired-compress-file
)
985 (dired-recursive-delete-directory
986 . tramp-sh-handle-dired-recursive-delete-directory
)
987 (dired-uncache . tramp-handle-dired-uncache
)
988 (set-visited-file-modtime . tramp-sh-handle-set-visited-file-modtime
)
989 (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime
)
990 (file-selinux-context . tramp-sh-handle-file-selinux-context
)
991 (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context
)
992 (vc-registered . tramp-sh-handle-vc-registered
))
993 "Alist of handler functions.
994 Operations not mentioned here will be handled by the normal Emacs functions.")
996 ;; This must be the last entry, because `identity' always matches.
998 (add-to-list 'tramp-foreign-file-name-handler-alist
999 '(identity . tramp-sh-file-name-handler
) 'append
)
1001 ;;; File Name Handler Functions:
1003 (defun tramp-sh-handle-make-symbolic-link
1004 (filename linkname
&optional ok-if-already-exists
)
1005 "Like `make-symbolic-link' for Tramp files.
1006 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1007 the symlink. If LINKNAME is a Tramp file, only the localname component is
1008 used as the target of the symlink.
1010 If LINKNAME is a Tramp file and the localname component is relative, then
1011 it is expanded first, before the localname component is taken. Note that
1012 this can give surprising results if the user/host for the source and
1013 target of the symlink differ."
1014 (with-parsed-tramp-file-name linkname l
1015 (let ((ln (tramp-get-remote-ln l
))
1016 (cwd (tramp-run-real-handler
1017 'file-name-directory
(list l-localname
))))
1021 "Making a symbolic link. ln(1) does not exist on the remote host."))
1023 ;; Do the 'confirm if exists' thing.
1024 (when (file-exists-p linkname
)
1026 (if (or (null ok-if-already-exists
) ; not allowed to exist
1027 (and (numberp ok-if-already-exists
)
1030 "File %s already exists; make it a link anyway? "
1033 l
'file-already-exists
"File %s already exists" l-localname
)
1034 (delete-file linkname
)))
1036 ;; If FILENAME is a Tramp name, use just the localname component.
1037 (when (tramp-tramp-file-p filename
)
1039 (tramp-file-name-localname
1040 (tramp-dissect-file-name (expand-file-name filename
)))))
1042 (tramp-flush-file-property l
(file-name-directory l-localname
))
1043 (tramp-flush-file-property l l-localname
)
1045 ;; Right, they are on the same host, regardless of user, method,
1046 ;; etc. We now make the link on the remote machine. This will
1047 ;; occur as the user that FILENAME belongs to.
1048 (tramp-send-command-and-check
1051 "cd %s && %s -sf %s %s"
1052 (tramp-shell-quote-argument cwd
)
1054 (tramp-shell-quote-argument filename
)
1055 (tramp-shell-quote-argument l-localname
))
1058 (defun tramp-sh-handle-file-truename (filename &optional counter prev-dirs
)
1059 "Like `file-truename' for Tramp files."
1060 (with-parsed-tramp-file-name (expand-file-name filename
) nil
1061 (tramp-make-tramp-file-name method user host
1062 (with-file-property v localname
"file-truename"
1063 (let ((result nil
)) ; result steps in reverse order
1064 (tramp-message v
4 "Finding true name for `%s'" filename
)
1066 ;; Use GNU readlink --canonicalize-missing where available.
1067 ((tramp-get-remote-readlink v
)
1069 (tramp-send-command-and-read
1071 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
1072 (tramp-get-remote-readlink v
)
1073 (tramp-shell-quote-argument localname
)))))
1075 ;; Use Perl implementation.
1076 ((and (tramp-get-remote-perl v
)
1077 (tramp-get-connection-property v
"perl-file-spec" nil
)
1078 (tramp-get-connection-property v
"perl-cwd-realpath" nil
))
1079 (tramp-maybe-send-script
1080 v tramp-perl-file-truename
"tramp_perl_file_truename")
1082 (tramp-send-command-and-read
1084 (format "tramp_perl_file_truename %s"
1085 (tramp-shell-quote-argument localname
)))))
1087 ;; Do it yourself. We bind `directory-sep-char' here for
1088 ;; XEmacs on Windows, which would otherwise use backslash.
1089 (t (let* ((directory-sep-char ?
/)
1090 (steps (tramp-compat-split-string localname
"/"))
1091 (localnamedir (tramp-run-real-handler
1092 'file-name-as-directory
(list localname
)))
1093 (is-dir (string= localname localnamedir
))
1096 ;; Don't make the following value larger than
1097 ;; necessary. People expect an error message in
1098 ;; a timely fashion when something is wrong;
1099 ;; otherwise they might think that Emacs is hung.
1100 ;; Of course, correctness has to come first.
1103 (while (and steps
(< numchase numchase-limit
))
1104 (setq thisstep
(pop steps
))
1107 (mapconcat 'identity
1108 (append '("") (reverse result
) (list thisstep
))
1110 (setq symlink-target
1111 (nth 0 (file-attributes
1112 (tramp-make-tramp-file-name
1114 (mapconcat 'identity
1119 (cond ((string= "." thisstep
)
1120 (tramp-message v
5 "Ignoring step `.'"))
1121 ((string= ".." thisstep
)
1122 (tramp-message v
5 "Processing step `..'")
1124 ((stringp symlink-target
)
1125 ;; It's a symlink, follow it.
1127 v
5 "Follow symlink to %s" symlink-target
)
1128 (setq numchase
(1+ numchase
))
1129 (when (file-name-absolute-p symlink-target
)
1131 ;; If the symlink was absolute, we'll get a
1132 ;; string like "/user@host:/some/target";
1133 ;; extract the "/some/target" part from it.
1134 (when (tramp-tramp-file-p symlink-target
)
1135 (unless (tramp-equal-remote filename symlink-target
)
1138 "Symlink target `%s' on wrong host"
1140 (setq symlink-target localname
))
1142 (append (tramp-compat-split-string
1147 (setq result
(cons thisstep result
)))))
1148 (when (>= numchase numchase-limit
)
1151 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
1152 (setq result
(reverse result
))
1153 ;; Combine list to form string.
1156 (mapconcat 'identity
(cons "" result
) "/")
1159 (or (string= "" result
)
1160 (not (string= (substring result -
1) "/"))))
1161 (setq result
(concat result
"/"))))))
1163 (tramp-message v
4 "True name of `%s' is `%s'" localname result
)
1168 (defun tramp-sh-handle-file-exists-p (filename)
1169 "Like `file-exists-p' for Tramp files."
1170 (with-parsed-tramp-file-name filename nil
1171 (with-file-property v localname
"file-exists-p"
1172 (or (not (null (tramp-get-file-property
1173 v localname
"file-attributes-integer" nil
)))
1174 (not (null (tramp-get-file-property
1175 v localname
"file-attributes-string" nil
)))
1176 (tramp-send-command-and-check
1180 (tramp-get-file-exists-command v
)
1181 (tramp-shell-quote-argument localname
)))))))
1183 ;; CCC: This should check for an error condition and signal failure
1184 ;; when something goes wrong.
1185 ;; Daniel Pittman <daniel@danann.net>
1186 (defun tramp-sh-handle-file-attributes (filename &optional id-format
)
1187 "Like `file-attributes' for Tramp files."
1188 (unless id-format
(setq id-format
'integer
))
1189 ;; Don't modify `last-coding-system-used' by accident.
1190 (let ((last-coding-system-used last-coding-system-used
))
1191 (with-parsed-tramp-file-name (expand-file-name filename
) nil
1192 (with-file-property v localname
(format "file-attributes-%s" id-format
)
1194 (tramp-convert-file-attributes
1198 ((tramp-get-remote-stat v
)
1199 (tramp-do-file-attributes-with-stat v localname id-format
))
1200 ((tramp-get-remote-perl v
)
1201 (tramp-do-file-attributes-with-perl v localname id-format
))
1203 ;; The scripts could fail, for example with huge file size.
1204 (tramp-do-file-attributes-with-ls v localname id-format
))))))))
1206 (defun tramp-do-file-attributes-with-ls (vec localname
&optional id-format
)
1207 "Implement `file-attributes' for Tramp files using the ls(1) command."
1209 res-inode res-filemodes res-numlinks
1210 res-uid res-gid res-size res-symlink-target
)
1211 (tramp-message vec
5 "file attributes with ls: %s" localname
)
1214 (format "(%s %s || %s -h %s) && %s %s %s"
1215 (tramp-get-file-exists-command vec
)
1216 (tramp-shell-quote-argument localname
)
1217 (tramp-get-test-command vec
)
1218 (tramp-shell-quote-argument localname
)
1219 (tramp-get-ls-command vec
)
1220 (if (eq id-format
'integer
) "-ildn" "-ild")
1221 (tramp-shell-quote-argument localname
)))
1222 ;; parse `ls -l' output ...
1223 (with-current-buffer (tramp-get-buffer vec
)
1224 (when (> (buffer-size) 0)
1225 (goto-char (point-min))
1229 (read (current-buffer))
1230 (invalid-read-syntax
1231 (when (and (equal (cadr err
)
1232 "Integer constant overflow in reader")
1234 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1236 (let* ((big (read (substring (car (cddr err
)) 0
1237 (match-beginning 1))))
1238 (small (read (match-string 1 (car (cddr err
)))))
1239 (twiddle (/ small
65536)))
1240 (cons (+ big twiddle
)
1241 (- small
(* twiddle
65536))))))))
1242 ;; ... file mode flags
1243 (setq res-filemodes
(symbol-name (read (current-buffer))))
1245 (setq res-numlinks
(read (current-buffer)))
1247 (setq res-uid
(read (current-buffer)))
1248 (setq res-gid
(read (current-buffer)))
1249 (if (eq id-format
'integer
)
1251 (unless (numberp res-uid
) (setq res-uid -
1))
1252 (unless (numberp res-gid
) (setq res-gid -
1)))
1254 (unless (stringp res-uid
) (setq res-uid
(symbol-name res-uid
)))
1255 (unless (stringp res-gid
) (setq res-gid
(symbol-name res-gid
)))))
1257 (setq res-size
(read (current-buffer)))
1258 ;; From the file modes, figure out other stuff.
1259 (setq symlinkp
(eq ?l
(aref res-filemodes
0)))
1260 (setq dirp
(eq ?d
(aref res-filemodes
0)))
1261 ;; if symlink, find out file name pointed to
1263 (search-forward "-> ")
1264 (setq res-symlink-target
(buffer-substring (point) (point-at-eol))))
1265 ;; return data gathered
1267 ;; 0. t for directory, string (name linked to) for symbolic
1269 (or dirp res-symlink-target
)
1270 ;; 1. Number of links to file.
1276 ;; 4. Last access time, as a list of two integers. First
1277 ;; integer has high-order 16 bits of time, second has low 16
1279 ;; 5. Last modification time, likewise.
1280 ;; 6. Last status change time, likewise.
1281 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1282 ;; 7. Size in bytes (-1, if number is out of range).
1284 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1286 ;; 9. t if file's gid would change if file were deleted and
1287 ;; recreated. Will be set in `tramp-convert-file-attributes'
1289 ;; 10. inode number.
1291 ;; 11. Device number. Will be replaced by a virtual device number.
1295 (defun tramp-do-file-attributes-with-perl
1296 (vec localname
&optional id-format
)
1297 "Implement `file-attributes' for Tramp files using a Perl script."
1298 (tramp-message vec
5 "file attributes with perl: %s" localname
)
1299 (tramp-maybe-send-script
1300 vec tramp-perl-file-attributes
"tramp_perl_file_attributes")
1301 (tramp-send-command-and-read
1303 (format "tramp_perl_file_attributes %s %s"
1304 (tramp-shell-quote-argument localname
) id-format
)))
1306 (defun tramp-do-file-attributes-with-stat
1307 (vec localname
&optional id-format
)
1308 "Implement `file-attributes' for Tramp files using stat(1) command."
1309 (tramp-message vec
5 "file attributes with stat: %s" localname
)
1310 (tramp-send-command-and-read
1313 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
1314 ;; parse correctly the sequence "((". Therefore, we add a space.
1315 "( (%s %s || %s -h %s) && %s -c '((\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)' %s || echo nil)"
1316 (tramp-get-file-exists-command vec
)
1317 (tramp-shell-quote-argument localname
)
1318 (tramp-get-test-command vec
)
1319 (tramp-shell-quote-argument localname
)
1320 (tramp-get-remote-stat vec
)
1321 (if (eq id-format
'integer
) "%u" "\"%U\"")
1322 (if (eq id-format
'integer
) "%g" "\"%G\"")
1323 (tramp-shell-quote-argument localname
))))
1325 (defun tramp-sh-handle-set-visited-file-modtime (&optional time-list
)
1326 "Like `set-visited-file-modtime' for Tramp files."
1327 (unless (buffer-file-name)
1328 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1331 (tramp-run-real-handler 'set-visited-file-modtime
(list time-list
))
1332 (let ((f (buffer-file-name))
1334 (with-parsed-tramp-file-name f nil
1335 (let* ((attr (file-attributes f
))
1336 ;; '(-1 65535) means file doesn't exists yet.
1337 (modtime (or (nth 5 attr
) '(-1 65535))))
1338 (when (boundp 'last-coding-system-used
)
1339 (setq coding-system-used
(symbol-value 'last-coding-system-used
)))
1340 ;; We use '(0 0) as a don't-know value. See also
1341 ;; `tramp-do-file-attributes-with-ls'.
1342 (if (not (equal modtime
'(0 0)))
1343 (tramp-run-real-handler 'set-visited-file-modtime
(list modtime
))
1347 (format "%s -ild %s"
1348 (tramp-get-ls-command v
)
1349 (tramp-shell-quote-argument localname
)))
1350 (setq attr
(buffer-substring (point)
1351 (progn (end-of-line) (point)))))
1352 (tramp-set-file-property
1353 v localname
"visited-file-modtime-ild" attr
))
1354 (when (boundp 'last-coding-system-used
)
1355 (set 'last-coding-system-used coding-system-used
))
1358 ;; This function makes the same assumption as
1359 ;; `tramp-sh-handle-set-visited-file-modtime'.
1360 (defun tramp-sh-handle-verify-visited-file-modtime (buf)
1361 "Like `verify-visited-file-modtime' for Tramp files.
1362 At the time `verify-visited-file-modtime' calls this function, we
1363 already know that the buffer is visiting a file and that
1364 `visited-file-modtime' does not return 0. Do not call this
1365 function directly, unless those two cases are already taken care
1367 (with-current-buffer buf
1368 (let ((f (buffer-file-name)))
1369 ;; There is no file visiting the buffer, or the buffer has no
1370 ;; recorded last modification time, or there is no established
1373 (eq (visited-file-modtime) 0)
1374 (not (tramp-file-name-handler 'file-remote-p f nil
'connected
)))
1376 (with-parsed-tramp-file-name f nil
1377 (let* ((remote-file-name-inhibit-cache t
)
1378 (attr (file-attributes f
))
1379 (modtime (nth 5 attr
))
1380 (mt (visited-file-modtime)))
1383 ;; File exists, and has a known modtime.
1384 ((and attr
(not (equal modtime
'(0 0))))
1385 (< (abs (tramp-time-diff
1387 ;; For compatibility, deal with both the old
1388 ;; (HIGH . LOW) and the new (HIGH LOW) return
1389 ;; values of `visited-file-modtime'.
1391 (list (car mt
) (cdr mt
))
1394 ;; Modtime has the don't know value.
1398 (format "%s -ild %s"
1399 (tramp-get-ls-command v
)
1400 (tramp-shell-quote-argument localname
)))
1401 (with-current-buffer (tramp-get-buffer v
)
1402 (setq attr
(buffer-substring
1403 (point) (progn (end-of-line) (point)))))
1406 (tramp-get-file-property
1407 v localname
"visited-file-modtime-ild" "")))
1408 ;; If file does not exist, say it is not modified if and
1409 ;; only if that agrees with the buffer's record.
1410 (t (equal mt
'(-1 65535))))))))))
1412 (defun tramp-sh-handle-set-file-modes (filename mode
)
1413 "Like `set-file-modes' for Tramp files."
1414 (with-parsed-tramp-file-name filename nil
1415 (tramp-flush-file-property v localname
)
1416 ;; FIXME: extract the proper text from chmod's stderr.
1417 (tramp-barf-unless-okay
1419 (format "chmod %s %s"
1420 (tramp-compat-decimal-to-octal mode
)
1421 (tramp-shell-quote-argument localname
))
1422 "Error while changing file's mode %s" filename
)))
1424 (defun tramp-sh-handle-set-file-times (filename &optional time
)
1425 "Like `set-file-times' for Tramp files."
1426 (if (file-remote-p filename
)
1427 (with-parsed-tramp-file-name filename nil
1428 (tramp-flush-file-property v localname
)
1429 (let ((time (if (or (null time
) (equal time
'(0 0)))
1432 ;; With GNU Emacs, `format-time-string' has an optional
1433 ;; parameter UNIVERSAL. This is preferred, because we
1434 ;; could handle the case when the remote host is located
1435 ;; in a different time zone as the local host.
1436 (utc (not (featurep 'xemacs
))))
1437 (tramp-send-command-and-check
1438 v
(format "%s touch -t %s %s"
1439 (if utc
"TZ=UTC; export TZ;" "")
1441 (format-time-string "%Y%m%d%H%M.%S" time t
)
1442 (format-time-string "%Y%m%d%H%M.%S" time
))
1443 (tramp-shell-quote-argument localname
)))))
1445 ;; We handle also the local part, because in older Emacsen,
1446 ;; without `set-file-times', this function is an alias for this.
1447 ;; We are local, so we don't need the UTC settings.
1449 (tramp-compat-call-process
1450 "touch" nil nil nil
"-t"
1451 (format-time-string "%Y%m%d%H%M.%S" time
)
1452 (tramp-shell-quote-argument filename
)))))
1454 (defun tramp-set-file-uid-gid (filename &optional uid gid
)
1455 "Set the ownership for FILENAME.
1456 If UID and GID are provided, these values are used; otherwise uid
1457 and gid of the corresponding user is taken. Both parameters must be integers."
1458 ;; Modern Unices allow chown only for root. So we might need
1459 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1460 ;; working with su(do)? when it is needed, so it shall succeed in
1461 ;; the majority of cases.
1462 ;; Don't modify `last-coding-system-used' by accident.
1463 (let ((last-coding-system-used last-coding-system-used
))
1464 (if (file-remote-p filename
)
1465 (with-parsed-tramp-file-name filename nil
1466 (if (and (zerop (user-uid)) (tramp-local-host-p v
))
1467 ;; If we are root on the local host, we can do it directly.
1468 (tramp-set-file-uid-gid localname uid gid
)
1469 (let ((uid (or (and (integerp uid
) uid
)
1470 (tramp-get-remote-uid v
'integer
)))
1471 (gid (or (and (integerp gid
) gid
)
1472 (tramp-get-remote-gid v
'integer
))))
1475 "chown %d:%d %s" uid gid
1476 (tramp-shell-quote-argument localname
))))))
1478 ;; We handle also the local part, because there doesn't exist
1479 ;; `set-file-uid-gid'. On W32 "chown" might not work.
1480 (let ((uid (or (and (integerp uid
) uid
) (tramp-get-local-uid 'integer
)))
1481 (gid (or (and (integerp gid
) gid
) (tramp-get-local-gid 'integer
))))
1482 (tramp-compat-call-process
1484 (format "%d:%d" uid gid
) (tramp-shell-quote-argument filename
))))))
1486 (defun tramp-remote-selinux-p (vec)
1487 "Check, whether SELINUX is enabled on the remote host."
1488 (with-connection-property (tramp-get-connection-process vec
) "selinux-p"
1489 (let ((result (tramp-find-executable
1490 vec
"getenforce" (tramp-get-remote-path vec
) t t
)))
1493 (tramp-send-command-and-read
1494 vec
(format "echo \\\"`%S`\\\"" result
))
1497 (defun tramp-sh-handle-file-selinux-context (filename)
1498 "Like `file-selinux-context' for Tramp files."
1499 (with-parsed-tramp-file-name filename nil
1500 (with-file-property v localname
"file-selinux-context"
1501 (let ((context '(nil nil nil nil
))
1502 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1503 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1504 (when (and (tramp-remote-selinux-p v
)
1505 (tramp-send-command-and-check
1508 (tramp-get-ls-command v
)
1509 (tramp-shell-quote-argument localname
))))
1510 (with-current-buffer (tramp-get-connection-buffer v
)
1511 (goto-char (point-min))
1512 (when (re-search-forward regexp
(point-at-eol) t
)
1513 (setq context
(list (match-string 1) (match-string 2)
1514 (match-string 3) (match-string 4))))))
1515 ;; Return the context.
1518 (defun tramp-sh-handle-set-file-selinux-context (filename context
)
1519 "Like `set-file-selinux-context' for Tramp files."
1520 (with-parsed-tramp-file-name filename nil
1521 (if (and (consp context
)
1522 (tramp-remote-selinux-p v
)
1523 (tramp-send-command-and-check
1524 v
(format "chcon %s %s %s %s %s"
1525 (if (stringp (nth 0 context
))
1526 (format "--user=%s" (nth 0 context
)) "")
1527 (if (stringp (nth 1 context
))
1528 (format "--role=%s" (nth 1 context
)) "")
1529 (if (stringp (nth 2 context
))
1530 (format "--type=%s" (nth 2 context
)) "")
1531 (if (stringp (nth 3 context
))
1532 (format "--range=%s" (nth 3 context
)) "")
1533 (tramp-shell-quote-argument localname
))))
1534 (tramp-set-file-property v localname
"file-selinux-context" context
)
1535 (tramp-set-file-property v localname
"file-selinux-context" 'undef
)))
1536 ;; We always return nil.
1539 ;; Simple functions using the `test' command.
1541 (defun tramp-sh-handle-file-executable-p (filename)
1542 "Like `file-executable-p' for Tramp files."
1543 (with-parsed-tramp-file-name filename nil
1544 (with-file-property v localname
"file-executable-p"
1545 ;; Examine `file-attributes' cache to see if request can be
1546 ;; satisfied without remote operation.
1547 (or (tramp-check-cached-permissions v ?x
)
1548 (tramp-run-test "-x" filename
)))))
1550 (defun tramp-sh-handle-file-readable-p (filename)
1551 "Like `file-readable-p' for Tramp files."
1552 (with-parsed-tramp-file-name filename nil
1553 (with-file-property v localname
"file-readable-p"
1554 ;; Examine `file-attributes' cache to see if request can be
1555 ;; satisfied without remote operation.
1556 (or (tramp-check-cached-permissions v ?r
)
1557 (tramp-run-test "-r" filename
)))))
1559 ;; When the remote shell is started, it looks for a shell which groks
1560 ;; tilde expansion. Here, we assume that all shells which grok tilde
1561 ;; expansion will also provide a `test' command which groks `-nt' (for
1562 ;; newer than). If this breaks, tell me about it and I'll try to do
1563 ;; something smarter about it.
1564 (defun tramp-sh-handle-file-newer-than-file-p (file1 file2
)
1565 "Like `file-newer-than-file-p' for Tramp files."
1566 (cond ((not (file-exists-p file1
))
1568 ((not (file-exists-p file2
))
1570 ;; We are sure both files exist at this point.
1573 ;; We try to get the mtime of both files. If they are not
1574 ;; equal to the "dont-know" value, then we subtract the times
1575 ;; and obtain the result.
1576 (let ((fa1 (file-attributes file1
))
1577 (fa2 (file-attributes file2
)))
1578 (if (and (not (equal (nth 5 fa1
) '(0 0)))
1579 (not (equal (nth 5 fa2
) '(0 0))))
1580 (> 0 (tramp-time-diff (nth 5 fa2
) (nth 5 fa1
)))
1581 ;; If one of them is the dont-know value, then we can
1582 ;; still try to run a shell command on the remote host.
1583 ;; However, this only works if both files are Tramp
1584 ;; files and both have the same method, same user, same
1586 (unless (tramp-equal-remote file1 file2
)
1587 (with-parsed-tramp-file-name
1588 (if (tramp-tramp-file-p file1
) file1 file2
) nil
1591 "Files %s and %s must have same method, user, host"
1593 (with-parsed-tramp-file-name file1 nil
1595 (tramp-get-test-nt-command v
) file1 file2
))))))))
1597 ;; Functions implemented using the basic functions above.
1599 (defun tramp-sh-handle-file-directory-p (filename)
1600 "Like `file-directory-p' for Tramp files."
1601 (with-parsed-tramp-file-name filename nil
1602 ;; `file-directory-p' is used as predicate for filename completion.
1603 ;; Sometimes, when a connection is not established yet, it is
1604 ;; desirable to return t immediately for "/method:foo:". It can
1605 ;; be expected that this is always a directory.
1606 (or (zerop (length localname
))
1607 (with-file-property v localname
"file-directory-p"
1608 (tramp-run-test "-d" filename
)))))
1610 (defun tramp-sh-handle-file-writable-p (filename)
1611 "Like `file-writable-p' for Tramp files."
1612 (with-parsed-tramp-file-name filename nil
1613 (with-file-property v localname
"file-writable-p"
1614 (if (file-exists-p filename
)
1615 ;; Examine `file-attributes' cache to see if request can be
1616 ;; satisfied without remote operation.
1617 (or (tramp-check-cached-permissions v ?w
)
1618 (tramp-run-test "-w" filename
))
1619 ;; If file doesn't exist, check if directory is writable.
1620 (and (tramp-run-test "-d" (file-name-directory filename
))
1621 (tramp-run-test "-w" (file-name-directory filename
)))))))
1623 (defun tramp-sh-handle-file-ownership-preserved-p (filename)
1624 "Like `file-ownership-preserved-p' for Tramp files."
1625 (with-parsed-tramp-file-name filename nil
1626 (with-file-property v localname
"file-ownership-preserved-p"
1627 (let ((attributes (file-attributes filename
)))
1628 ;; Return t if the file doesn't exist, since it's true that no
1629 ;; information would be lost by an (attempted) delete and create.
1630 (or (null attributes
)
1631 (= (nth 2 attributes
) (tramp-get-remote-uid v
'integer
)))))))
1633 ;; Directory listings.
1635 (defun tramp-sh-handle-directory-files-and-attributes
1636 (directory &optional full match nosort id-format
)
1637 "Like `directory-files-and-attributes' for Tramp files."
1638 (unless id-format
(setq id-format
'integer
))
1639 (when (file-directory-p directory
)
1640 (setq directory
(expand-file-name directory
))
1643 (with-parsed-tramp-file-name directory nil
1646 (format "directory-files-and-attributes-%s" id-format
)
1651 (tramp-convert-file-attributes v
(cdr x
))))
1653 ((tramp-get-remote-stat v
)
1654 (tramp-do-directory-files-and-attributes-with-stat
1655 v localname id-format
))
1656 ((tramp-get-remote-perl v
)
1657 (tramp-do-directory-files-and-attributes-with-perl
1658 v localname id-format
)))))))))
1662 (setq item
(pop temp
))
1663 (when (or (null match
) (string-match match
(car item
)))
1665 (setcar item
(expand-file-name (car item
) directory
)))
1666 (push item result
)))
1670 (sort result
(lambda (x y
) (string< (car x
) (car y
))))))))
1672 (defun tramp-do-directory-files-and-attributes-with-perl
1673 (vec localname
&optional id-format
)
1674 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1675 (tramp-message vec
5 "directory-files-and-attributes with perl: %s" localname
)
1676 (tramp-maybe-send-script
1677 vec tramp-perl-directory-files-and-attributes
1678 "tramp_perl_directory_files_and_attributes")
1680 (tramp-send-command-and-read
1682 (format "tramp_perl_directory_files_and_attributes %s %s"
1683 (tramp-shell-quote-argument localname
) id-format
))))
1684 (when (stringp object
) (tramp-error vec
'file-error object
))
1687 (defun tramp-do-directory-files-and-attributes-with-stat
1688 (vec localname
&optional id-format
)
1689 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1690 (tramp-message vec
5 "directory-files-and-attributes with stat: %s" localname
)
1691 (tramp-send-command-and-read
1695 ;; We must care about filenames with spaces, or starting with
1696 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1697 ;; but it does not work on all remote systems. Therefore, we
1698 ;; quote the filenames via sed.
1699 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | xargs "
1700 "%s -c '(\"%%n\" (\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)'); "
1702 (tramp-shell-quote-argument localname
)
1703 (tramp-get-ls-command vec
)
1704 (tramp-get-remote-stat vec
)
1705 (if (eq id-format
'integer
) "%u" "\"%U\"")
1706 (if (eq id-format
'integer
) "%g" "\"%G\""))))
1708 ;; This function should return "foo/" for directories and "bar" for
1710 (defun tramp-sh-handle-file-name-all-completions (filename directory
)
1711 "Like `file-name-all-completions' for Tramp files."
1712 (unless (save-match-data (string-match "/" filename
))
1713 (with-parsed-tramp-file-name (expand-file-name directory
) nil
1720 ;; Try cache entries for filename, filename with last
1721 ;; character removed, filename with last two characters
1722 ;; removed, ..., and finally the empty string - all
1723 ;; concatenated to the local directory name.
1724 (let ((remote-file-name-inhibit-cache
1725 (or remote-file-name-inhibit-cache
1726 tramp-completion-reread-directory-timeout
)))
1728 ;; This is inefficient for very long filenames, pity
1729 ;; `reduce' is not available...
1736 (tramp-get-file-property
1738 (concat localname
(substring filename
0 x
))
1739 "file-name-all-completions"
1741 (when cache-hit
(list cache-hit
))))
1742 ;; We cannot use a length of 0, because file properties
1743 ;; for "foo" and "foo/" are identical.
1744 (tramp-compat-number-sequence (length filename
) 1 -
1)))))
1746 ;; Cache expired or no matching cache entry found so we need
1747 ;; to perform a remote operation.
1749 ;; Get a list of directories and files, including reliably
1750 ;; tagging the directories with a trailing '/'. Because I
1751 ;; rock. --daniel@danann.net
1753 ;; Changed to perform `cd' in the same remote op and only
1754 ;; get entries starting with `filename'. Capture any `cd'
1755 ;; error messages. Ensure any `cd' and `echo' aliases are
1759 (if (tramp-get-remote-perl v
)
1761 (tramp-maybe-send-script
1762 v tramp-perl-file-name-all-completions
1763 "tramp_perl_file_name_all_completions")
1764 (format "tramp_perl_file_name_all_completions %s %s %d"
1765 (tramp-shell-quote-argument localname
)
1766 (tramp-shell-quote-argument filename
)
1768 ;; `read-file-name-completion-ignore-case'
1769 ;; is introduced with Emacs 22.1.
1771 'read-file-name-completion-ignore-case
)
1772 'read-file-name-completion-ignore-case
1773 'completion-ignore-case
))
1777 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
1778 ;; `ls' with wildcard might fail with `Argument
1779 ;; list too long' error in some corner cases; if
1780 ;; `ls' fails after `cd' succeeded, chances are
1781 ;; that's the case, so let's retry without
1782 ;; wildcard. This will return "too many" entries
1783 ;; but that isn't harmful.
1784 " || %s -a 2>/dev/null)"
1785 " | while read f; do"
1786 " if %s -d \"$f\" 2>/dev/null;"
1787 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1788 " && \\echo ok) || \\echo fail")
1789 (tramp-shell-quote-argument localname
)
1790 (tramp-get-ls-command v
)
1791 ;; When `filename' is empty, just `ls' without
1792 ;; filename argument is more efficient than `ls *'
1793 ;; for very large directories and might avoid the
1794 ;; `Argument list too long' error.
1796 ;; With and only with wildcard, we need to add
1797 ;; `-d' to prevent `ls' from descending into
1799 (if (zerop (length filename
))
1801 (concat (tramp-shell-quote-argument filename
) "* -d"))
1802 (tramp-get-ls-command v
)
1803 (tramp-get-test-command v
))))
1805 ;; Now grab the output.
1806 (with-current-buffer (tramp-get-buffer v
)
1807 (goto-char (point-max))
1809 ;; Check result code, found in last line of output.
1811 (if (looking-at "^fail$")
1813 ;; Grab error message from line before last line
1814 ;; (it was put there by `cd 2>&1').
1818 "tramp-sh-handle-file-name-all-completions: %s"
1819 (buffer-substring (point) (point-at-eol))))
1820 ;; For peace of mind, if buffer doesn't end in `fail'
1821 ;; then it should end in `ok'. If neither are in the
1822 ;; buffer something went seriously wrong on the remote
1824 (unless (looking-at "^ok$")
1828 tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
1829 (tramp-shell-quote-argument localname
) (buffer-string))))
1831 (while (zerop (forward-line -
1))
1832 (push (buffer-substring (point) (point-at-eol)) result
)))
1834 ;; Because the remote op went through OK we know the
1835 ;; directory we `cd'-ed to exists.
1836 (tramp-set-file-property v localname
"file-exists-p" t
)
1838 ;; Because the remote op went through OK we know every
1839 ;; file listed by `ls' exists.
1840 (mapc (lambda (entry)
1841 (tramp-set-file-property
1842 v
(concat localname entry
) "file-exists-p" t
))
1845 ;; Store result in the cache.
1846 (tramp-set-file-property
1847 v
(concat localname filename
)
1848 "file-name-all-completions" result
))))))))
1852 (defun tramp-sh-handle-add-name-to-file
1853 (filename newname
&optional ok-if-already-exists
)
1854 "Like `add-name-to-file' for Tramp files."
1855 (unless (tramp-equal-remote filename newname
)
1856 (with-parsed-tramp-file-name
1857 (if (tramp-tramp-file-p filename
) filename newname
) nil
1860 "add-name-to-file: %s"
1861 "only implemented for same method, same user, same host")))
1862 (with-parsed-tramp-file-name filename v1
1863 (with-parsed-tramp-file-name newname v2
1864 (let ((ln (when v1
(tramp-get-remote-ln v1
))))
1865 (when (and (not ok-if-already-exists
)
1866 (file-exists-p newname
)
1867 (not (numberp ok-if-already-exists
))
1870 "File %s already exists; make it a new name anyway? "
1874 "add-name-to-file: file %s already exists" newname
))
1875 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
1876 (tramp-flush-file-property v2 v2-localname
)
1877 (tramp-barf-unless-okay
1879 (format "%s %s %s" ln
(tramp-shell-quote-argument v1-localname
)
1880 (tramp-shell-quote-argument v2-localname
))
1881 "error with add-name-to-file, see buffer `%s' for details"
1884 (defun tramp-sh-handle-copy-file
1885 (filename newname
&optional ok-if-already-exists keep-date
1886 preserve-uid-gid preserve-selinux-context
)
1887 "Like `copy-file' for Tramp files."
1888 (setq filename
(expand-file-name filename
))
1889 (setq newname
(expand-file-name newname
))
1891 ;; At least one file a Tramp file?
1892 ((or (tramp-tramp-file-p filename
)
1893 (tramp-tramp-file-p newname
))
1894 (tramp-do-copy-or-rename-file
1895 'copy filename newname ok-if-already-exists keep-date
1896 preserve-uid-gid preserve-selinux-context
))
1898 (preserve-selinux-context
1899 (tramp-run-real-handler
1901 (list filename newname ok-if-already-exists keep-date
1902 preserve-uid-gid preserve-selinux-context
)))
1904 (tramp-run-real-handler
1906 (list filename newname ok-if-already-exists keep-date preserve-uid-gid
)))
1908 (tramp-run-real-handler
1909 'copy-file
(list filename newname ok-if-already-exists keep-date
)))))
1911 (defun tramp-sh-handle-copy-directory
1912 (dirname newname
&optional keep-date parents copy-contents
)
1913 "Like `copy-directory' for Tramp files."
1914 (let ((t1 (tramp-tramp-file-p dirname
))
1915 (t2 (tramp-tramp-file-p newname
)))
1916 (with-parsed-tramp-file-name (if t1 dirname newname
) nil
1917 (if (and (tramp-get-method-parameter method
'tramp-copy-recursive
)
1918 ;; When DIRNAME and NEWNAME are remote, they must have
1920 (or (null t1
) (null t2
)
1922 (tramp-file-name-method (tramp-dissect-file-name dirname
))
1923 (tramp-file-name-method (tramp-dissect-file-name newname
)))))
1924 ;; scp or rsync DTRT.
1926 (setq dirname
(directory-file-name (expand-file-name dirname
))
1927 newname
(directory-file-name (expand-file-name newname
)))
1928 (if (and (file-directory-p newname
)
1929 (not (string-equal (file-name-nondirectory dirname
)
1930 (file-name-nondirectory newname
))))
1933 (file-name-nondirectory dirname
) newname
)))
1934 (if (not (file-directory-p (file-name-directory newname
)))
1935 (make-directory (file-name-directory newname
) parents
))
1936 (tramp-do-copy-or-rename-file-out-of-band
1937 'copy dirname newname keep-date
))
1938 ;; We must do it file-wise.
1939 (tramp-run-real-handler
1940 'copy-directory
(list dirname newname keep-date parents
)))
1942 ;; When newname did exist, we have wrong cached values.
1944 (with-parsed-tramp-file-name newname nil
1945 (tramp-flush-file-property v
(file-name-directory localname
))
1946 (tramp-flush-file-property v localname
))))))
1948 (defun tramp-sh-handle-rename-file
1949 (filename newname
&optional ok-if-already-exists
)
1950 "Like `rename-file' for Tramp files."
1951 ;; Check if both files are local -- invoke normal rename-file.
1952 ;; Otherwise, use Tramp from local system.
1953 (setq filename
(expand-file-name filename
))
1954 (setq newname
(expand-file-name newname
))
1955 ;; At least one file a Tramp file?
1956 (if (or (tramp-tramp-file-p filename
)
1957 (tramp-tramp-file-p newname
))
1958 (tramp-do-copy-or-rename-file
1959 'rename filename newname ok-if-already-exists t t
)
1960 (tramp-run-real-handler
1961 'rename-file
(list filename newname ok-if-already-exists
))))
1963 (defun tramp-do-copy-or-rename-file
1964 (op filename newname
&optional ok-if-already-exists keep-date
1965 preserve-uid-gid preserve-selinux-context
)
1966 "Copy or rename a remote file.
1967 OP must be `copy' or `rename' and indicates the operation to perform.
1968 FILENAME specifies the file to copy or rename, NEWNAME is the name of
1969 the new file (for copy) or the new name of the file (for rename).
1970 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
1971 KEEP-DATE means to make sure that NEWNAME has the same timestamp
1972 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
1973 the uid and gid if both files are on the same host.
1974 PRESERVE-SELINUX-CONTEXT activates selinux commands.
1976 This function is invoked by `tramp-sh-handle-copy-file' and
1977 `tramp-sh-handle-rename-file'. It is an error if OP is neither
1978 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
1980 (unless (memq op
'(copy rename
))
1981 (error "Unknown operation `%s', must be `copy' or `rename'" op
))
1982 (let ((t1 (tramp-tramp-file-p filename
))
1983 (t2 (tramp-tramp-file-p newname
))
1984 (context (and preserve-selinux-context
1985 (apply 'file-selinux-context
(list filename
))))
1988 (with-parsed-tramp-file-name (if t1 filename newname
) nil
1989 (when (and (not ok-if-already-exists
) (file-exists-p newname
))
1991 v
'file-already-exists
"File %s already exists" newname
))
1993 (tramp-with-progress-reporter
1994 v
0 (format "%s %s to %s"
1995 (if (eq op
'copy
) "Copying" "Renaming")
1999 ;; Both are Tramp files.
2001 (with-parsed-tramp-file-name filename v1
2002 (with-parsed-tramp-file-name newname v2
2004 ;; Shortcut: if method, host, user are the same for
2005 ;; both files, we invoke `cp' or `mv' on the remote
2007 ((tramp-equal-remote filename newname
)
2008 (tramp-do-copy-or-rename-file-directly
2010 ok-if-already-exists keep-date preserve-uid-gid
))
2012 ;; Try out-of-band operation.
2013 ((tramp-method-out-of-band-p
2014 v1
(nth 7 (file-attributes (file-truename filename
))))
2015 (tramp-do-copy-or-rename-file-out-of-band
2016 op filename newname keep-date
))
2018 ;; No shortcut was possible. So we copy the file
2019 ;; first. If the operation was `rename', we go back
2020 ;; and delete the original file (if the copy was
2021 ;; successful). The approach is simple-minded: we
2022 ;; create a new buffer, insert the contents of the
2023 ;; source file into it, then write out the buffer to
2024 ;; the target file. The advantage is that it doesn't
2025 ;; matter which filename handlers are used for the
2026 ;; source and target file.
2028 (tramp-do-copy-or-rename-file-via-buffer
2029 op filename newname keep-date
))))))
2031 ;; One file is a Tramp file, the other one is local.
2034 ;; Fast track on local machine.
2035 ((tramp-local-host-p v
)
2036 (tramp-do-copy-or-rename-file-directly
2038 ok-if-already-exists keep-date preserve-uid-gid
))
2040 ;; If the Tramp file has an out-of-band method, the
2041 ;; corresponding copy-program can be invoked.
2042 ((tramp-method-out-of-band-p
2043 v
(nth 7 (file-attributes (file-truename filename
))))
2044 (tramp-do-copy-or-rename-file-out-of-band
2045 op filename newname keep-date
))
2047 ;; Use the inline method via a Tramp buffer.
2048 (t (tramp-do-copy-or-rename-file-via-buffer
2049 op filename newname keep-date
))))
2052 ;; One of them must be a Tramp file.
2053 (error "Tramp implementation says this cannot happen")))
2055 ;; Handle `preserve-selinux-context'.
2056 (when context
(apply 'set-file-selinux-context
(list newname context
)))
2058 ;; In case of `rename', we must flush the cache of the source file.
2059 (when (and t1
(eq op
'rename
))
2060 (with-parsed-tramp-file-name filename v1
2061 (tramp-flush-file-property v1
(file-name-directory localname
))
2062 (tramp-flush-file-property v1 localname
)))
2064 ;; When newname did exist, we have wrong cached values.
2066 (with-parsed-tramp-file-name newname v2
2067 (tramp-flush-file-property v2
(file-name-directory localname
))
2068 (tramp-flush-file-property v2 localname
)))))))
2070 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date
)
2071 "Use an Emacs buffer to copy or rename a file.
2072 First arg OP is either `copy' or `rename' and indicates the operation.
2073 FILENAME is the source file, NEWNAME the target file.
2074 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2076 ;; We must disable multibyte, because binary data shall not be
2078 (set-buffer-multibyte nil
)
2079 (let ((coding-system-for-read 'binary
)
2080 (jka-compr-inhibit t
))
2081 (insert-file-contents-literally filename
))
2082 ;; We don't want the target file to be compressed, so we let-bind
2083 ;; `jka-compr-inhibit' to t.
2084 (let ((coding-system-for-write 'binary
)
2085 (jka-compr-inhibit t
))
2086 (write-region (point-min) (point-max) newname
)))
2087 ;; KEEP-DATE handling.
2088 (when keep-date
(set-file-times newname
(nth 5 (file-attributes filename
))))
2090 (set-file-modes newname
(tramp-default-file-modes filename
))
2091 ;; If the operation was `rename', delete the original file.
2092 (unless (eq op
'copy
) (delete-file filename
)))
2094 (defun tramp-do-copy-or-rename-file-directly
2095 (op filename newname ok-if-already-exists keep-date preserve-uid-gid
)
2096 "Invokes `cp' or `mv' on the remote system.
2097 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2098 respectively. FILENAME specifies the file to copy or rename,
2099 NEWNAME is the name of the new file (for copy) or the new name of
2100 the file (for rename). Both files must reside on the same host.
2101 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2102 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2103 the uid and gid from FILENAME."
2104 (let ((t1 (tramp-tramp-file-p filename
))
2105 (t2 (tramp-tramp-file-p newname
))
2106 (file-times (nth 5 (file-attributes filename
)))
2107 (file-modes (tramp-default-file-modes filename
)))
2108 (with-parsed-tramp-file-name (if t1 filename newname
) nil
2109 (let* ((cmd (cond ((and (eq op
'copy
) preserve-uid-gid
) "cp -f -p")
2110 ((eq op
'copy
) "cp -f")
2111 ((eq op
'rename
) "mv -f")
2114 "Unknown operation `%s', must be `copy' or `rename'"
2118 (tramp-file-name-handler 'file-remote-p filename
'localname
)
2122 (tramp-file-name-handler 'file-remote-p newname
'localname
)
2124 (prefix (file-remote-p (if t1 filename newname
)))
2128 ;; Both files are on a remote host, with same user.
2131 (tramp-send-command-and-check
2132 v
(format "%s %s %s" cmd
2133 (tramp-shell-quote-argument localname1
)
2134 (tramp-shell-quote-argument localname2
))))
2135 (with-current-buffer (tramp-get-buffer v
)
2136 (goto-char (point-min))
2140 ;; Mask cp -f error.
2142 tramp-operation-not-permitted-regexp nil t
))
2144 (tramp-error-with-buffer
2146 "Copying directly failed, see buffer `%s' for details."
2149 ;; We are on the local host.
2152 ;; We can do it directly.
2153 ((let (file-name-handler-alist)
2154 (and (file-readable-p localname1
)
2155 (file-writable-p (file-name-directory localname2
))
2156 (or (file-directory-p localname2
)
2157 (file-writable-p localname2
))))
2159 (tramp-compat-copy-file
2160 localname1 localname2 ok-if-already-exists
2161 keep-date preserve-uid-gid
)
2162 (tramp-run-real-handler
2163 'rename-file
(list localname1 localname2 ok-if-already-exists
))))
2165 ;; We can do it directly with `tramp-send-command'
2166 ((and (file-readable-p (concat prefix localname1
))
2168 (file-name-directory (concat prefix localname2
)))
2169 (or (file-directory-p (concat prefix localname2
))
2170 (file-writable-p (concat prefix localname2
))))
2171 (tramp-do-copy-or-rename-file-directly
2172 op
(concat prefix localname1
) (concat prefix localname2
)
2173 ok-if-already-exists keep-date t
)
2174 ;; We must change the ownership to the local user.
2175 (tramp-set-file-uid-gid
2176 (concat prefix localname2
)
2177 (tramp-get-local-uid 'integer
)
2178 (tramp-get-local-gid 'integer
)))
2180 ;; We need a temporary file in between.
2182 ;; Create the temporary file.
2183 (let ((tmpfile (tramp-compat-make-temp-file localname1
)))
2188 (tramp-barf-unless-okay
2191 (tramp-shell-quote-argument localname1
)
2192 (tramp-shell-quote-argument tmpfile
))
2193 "Copying directly failed, see buffer `%s' for details."
2194 (tramp-get-buffer v
))
2195 ;; We must change the ownership as remote user.
2196 ;; Since this does not work reliable, we also
2197 ;; give read permissions.
2199 (concat prefix tmpfile
)
2200 (tramp-compat-octal-to-decimal "0777"))
2201 (tramp-set-file-uid-gid
2202 (concat prefix tmpfile
)
2203 (tramp-get-local-uid 'integer
)
2204 (tramp-get-local-gid 'integer
)))
2207 (tramp-compat-copy-file
2208 localname1 tmpfile t
2209 keep-date preserve-uid-gid
)
2210 (tramp-run-real-handler
2212 (list localname1 tmpfile t
)))
2213 ;; We must change the ownership as local user.
2214 ;; Since this does not work reliable, we also
2215 ;; give read permissions.
2217 tmpfile
(tramp-compat-octal-to-decimal "0777"))
2218 (tramp-set-file-uid-gid
2220 (tramp-get-remote-uid v
'integer
)
2221 (tramp-get-remote-gid v
'integer
))))
2223 ;; Move the temporary file to its destination.
2226 (tramp-barf-unless-okay
2229 (tramp-shell-quote-argument tmpfile
)
2230 (tramp-shell-quote-argument localname2
))
2231 "Copying directly failed, see buffer `%s' for details."
2232 (tramp-get-buffer v
)))
2234 (tramp-run-real-handler
2236 (list tmpfile localname2 ok-if-already-exists
)))))
2239 (ignore-errors (delete-file tmpfile
)))))))))
2241 ;; Set the time and mode. Mask possible errors.
2244 (set-file-times newname file-times
)
2245 (set-file-modes newname file-modes
))))))
2247 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date
)
2248 "Invoke rcp program to copy.
2249 The method used must be an out-of-band method."
2250 (let* ((t1 (tramp-tramp-file-p filename
))
2251 (t2 (tramp-tramp-file-p newname
))
2252 (orig-vec (tramp-dissect-file-name (if t1 filename newname
)))
2253 copy-program copy-args copy-env copy-keep-date port spec
2256 (with-parsed-tramp-file-name (if t1 filename newname
) nil
2259 ;; Both are Tramp files. We shall optimize it when the
2260 ;; methods for filename and newname are the same.
2261 (let* ((dir-flag (file-directory-p filename
))
2262 (tmpfile (tramp-compat-make-temp-file localname dir-flag
)))
2266 (file-name-nondirectory newname
) tmpfile
)))
2269 (tramp-do-copy-or-rename-file-out-of-band
2270 op filename tmpfile keep-date
)
2271 (tramp-do-copy-or-rename-file-out-of-band
2272 'rename tmpfile newname keep-date
))
2276 (tramp-compat-delete-directory
2277 (expand-file-name ".." tmpfile
) 'recursive
)
2278 (delete-file tmpfile
)))))
2280 ;; Set variables for computing the prompt for reading
2282 (setq tramp-current-method
(tramp-file-name-method v
)
2283 tramp-current-user
(or (tramp-file-name-user v
)
2284 (tramp-get-connection-property
2286 tramp-current-host
(tramp-file-name-real-host v
))
2288 ;; Expand hops. Might be necessary for gateway methods.
2289 (setq v
(car (tramp-compute-multi-hops v
)))
2290 (aset v
3 localname
)
2292 ;; Check which ones of source and target are Tramp files.
2293 (setq source
(if t1
(tramp-make-copy-program-file-name v
) filename
)
2295 (if (and (file-directory-p filename
)
2297 (file-name-nondirectory filename
)
2298 (file-name-nondirectory newname
)))
2299 'file-name-directory
2301 (if t2
(tramp-make-copy-program-file-name v
) newname
)))
2303 ;; Check for host and port number. We cannot use
2304 ;; `tramp-file-name-port', because this returns also
2305 ;; `tramp-default-port', which might clash with settings in
2307 (setq host
(tramp-file-name-host v
)
2309 (when (string-match tramp-host-with-port-regexp host
)
2310 (setq port
(string-to-number (match-string 2 host
))
2311 host
(string-to-number (match-string 1 host
))))
2313 ;; Check for user. There might be an interactive setting.
2314 (setq user
(or (tramp-file-name-user v
)
2315 (tramp-get-connection-property v
"login-as" nil
)))
2317 ;; Compose copy command.
2318 (setq host
(or host
"")
2321 spec
(format-spec-make
2322 ?h host ?u user ?p port
2323 ?t
(tramp-get-connection-property
2324 (tramp-get-connection-process v
) "temp-file" "")
2325 ?k
(if keep-date
" " ""))
2326 copy-program
(tramp-get-method-parameter
2327 method
'tramp-copy-program
)
2328 copy-keep-date
(tramp-get-method-parameter
2329 method
'tramp-copy-keep-date
)
2332 ;; " " has either been a replacement of "%k" (when
2333 ;; keep-date argument is non-nil), or a replacement
2334 ;; for the whole keep-date sublist.
2338 (tramp-get-method-parameter method
'tramp-copy-args
)
2343 (let ((y (mapcar (lambda (z) (format-spec z spec
)) x
)))
2344 (if (member "" y
) '(" ") y
))))))
2350 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
2351 (unless (member "" x
) (mapconcat 'identity x
" ")))
2352 (tramp-get-method-parameter method
'tramp-copy-env
))))
2354 ;; Check for program.
2355 (unless (let ((default-directory
2356 (tramp-compat-temporary-file-directory)))
2357 (executable-find copy-program
))
2359 v
'file-error
"Cannot find copy program: %s" copy-program
))
2363 ;; The default directory must be remote.
2364 (let ((default-directory
2365 (file-name-directory (if t1 filename newname
)))
2366 (process-environment (copy-sequence process-environment
)))
2367 ;; Set the transfer process properties.
2368 (tramp-set-connection-property
2369 v
"process-name" (buffer-name (current-buffer)))
2370 (tramp-set-connection-property
2371 v
"process-buffer" (current-buffer))
2374 orig-vec
5 "%s=\"%s\"" (car copy-env
) (cadr copy-env
))
2375 (setenv (pop copy-env
) (pop copy-env
)))
2377 ;; Use an asynchronous process. By this, password can
2378 ;; be handled. The default directory must be local, in
2379 ;; order to apply the correct `copy-program'. We don't
2380 ;; set a timeout, because the copying of large files can
2381 ;; last longer than 60 secs.
2382 (let ((p (let ((default-directory
2383 (tramp-compat-temporary-file-directory)))
2384 (apply 'start-process
2385 (tramp-get-connection-name v
)
2386 (tramp-get-connection-buffer v
)
2388 (append copy-args
(list source target
))))))
2391 (mapconcat 'identity
(process-command p
) " "))
2392 (tramp-compat-set-process-query-on-exit-flag p nil
)
2393 (tramp-process-actions
2394 p v nil tramp-actions-copy-out-of-band
)))
2396 ;; Reset the transfer process properties.
2397 (tramp-message orig-vec
6 "%s" (buffer-string))
2398 (tramp-set-connection-property v
"process-name" nil
)
2399 (tramp-set-connection-property v
"process-buffer" nil
)))
2401 ;; Handle KEEP-DATE argument.
2402 (when (and keep-date
(not copy-keep-date
))
2403 (set-file-times newname
(nth 5 (file-attributes filename
))))
2406 (unless (and keep-date copy-keep-date
)
2408 (set-file-modes newname
(tramp-default-file-modes filename
)))))
2410 ;; If the operation was `rename', delete the original file.
2411 (unless (eq op
'copy
)
2412 (if (file-regular-p filename
)
2413 (delete-file filename
)
2414 (tramp-compat-delete-directory filename
'recursive
))))))
2416 (defun tramp-sh-handle-make-directory (dir &optional parents
)
2417 "Like `make-directory' for Tramp files."
2418 (setq dir
(expand-file-name dir
))
2419 (with-parsed-tramp-file-name dir nil
2420 (tramp-flush-directory-property v
(file-name-directory localname
))
2422 (tramp-barf-unless-okay
2424 (if parents
"mkdir -p" "mkdir")
2425 (tramp-shell-quote-argument localname
))
2426 "Couldn't make directory %s" dir
))))
2428 (defun tramp-sh-handle-delete-directory (directory &optional recursive
)
2429 "Like `delete-directory' for Tramp files."
2430 (setq directory
(expand-file-name directory
))
2431 (with-parsed-tramp-file-name directory nil
2432 (tramp-flush-file-property v
(file-name-directory localname
))
2433 (tramp-flush-directory-property v localname
)
2434 (tramp-barf-unless-okay
2436 (if recursive
"rm -rf" "rmdir")
2437 (tramp-shell-quote-argument localname
))
2438 "Couldn't delete %s" directory
)))
2440 (defun tramp-sh-handle-delete-file (filename &optional trash
)
2441 "Like `delete-file' for Tramp files."
2442 (setq filename
(expand-file-name filename
))
2443 (with-parsed-tramp-file-name filename nil
2444 (tramp-flush-file-property v
(file-name-directory localname
))
2445 (tramp-flush-file-property v localname
)
2446 (tramp-barf-unless-okay
2448 (or (and trash
(tramp-get-remote-trash v
)) "rm -f")
2449 (tramp-shell-quote-argument localname
))
2450 "Couldn't delete %s" filename
)))
2454 ;; CCC: This does not seem to be enough. Something dies when
2455 ;; we try and delete two directories under Tramp :/
2456 (defun tramp-sh-handle-dired-recursive-delete-directory (filename)
2457 "Recursively delete the directory given.
2458 This is like `dired-recursive-delete-directory' for Tramp files."
2459 (with-parsed-tramp-file-name filename nil
2460 ;; Run a shell command 'rm -r <localname>'
2461 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
2462 (unless (file-exists-p filename
)
2463 (tramp-error v
'file-error
"No such directory: %s" filename
))
2464 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
2467 (format "rm -rf %s" (tramp-shell-quote-argument localname
))
2468 ;; Don't read the output, do it explicitly.
2470 ;; Wait for the remote system to return to us...
2471 ;; This might take a while, allow it plenty of time.
2472 (tramp-wait-for-output (tramp-get-connection-process v
) 120)
2473 ;; Make sure that it worked...
2474 (tramp-flush-file-property v
(file-name-directory localname
))
2475 (tramp-flush-directory-property v localname
)
2476 (and (file-exists-p filename
)
2478 v
'file-error
"Failed to recursively delete %s" filename
))))
2480 (defun tramp-sh-handle-dired-compress-file (file &rest ok-flag
)
2481 "Like `dired-compress-file' for Tramp files."
2482 ;; OK-FLAG is valid for XEmacs only, but not implemented.
2483 ;; Code stolen mainly from dired-aux.el.
2484 (with-parsed-tramp-file-name file nil
2485 (tramp-flush-file-property v localname
)
2488 (if (not (featurep 'xemacs
))
2490 (symbol-value 'dired-compress-file-suffixes
)
2491 ;; XEmacs has `dired-compression-method-alist', which is
2492 ;; transformed into `dired-compress-file-suffixes' structure.
2495 (list (concat (regexp-quote (nth 1 x
)) "\\'")
2497 (mapconcat 'identity
(nth 3 x
) " ")))
2498 (symbol-value 'dired-compression-method-alist
))))
2500 ;; See if any suffix rule matches this file name.
2502 (let (case-fold-search)
2503 (if (string-match (car (car suffixes
)) localname
)
2504 (setq suffix
(car suffixes
) suffixes nil
))
2505 (setq suffixes
(cdr suffixes
))))
2507 (cond ((file-symlink-p file
)
2509 ((and suffix
(nth 2 suffix
))
2510 ;; We found an uncompression rule.
2511 (tramp-with-progress-reporter
2512 v
0 (format "Uncompressing %s" file
)
2513 (when (tramp-send-command-and-check
2514 v
(concat (nth 2 suffix
) " "
2515 (tramp-shell-quote-argument localname
)))
2516 ;; `dired-remove-file' is not defined in XEmacs.
2517 (tramp-compat-funcall 'dired-remove-file file
)
2518 (string-match (car suffix
) file
)
2519 (concat (substring file
0 (match-beginning 0))))))
2521 ;; We don't recognize the file as compressed, so compress it.
2523 (tramp-with-progress-reporter v
0 (format "Compressing %s" file
)
2524 (when (tramp-send-command-and-check
2525 v
(concat "gzip -f "
2526 (tramp-shell-quote-argument localname
)))
2527 ;; `dired-remove-file' is not defined in XEmacs.
2528 (tramp-compat-funcall 'dired-remove-file file
)
2529 (cond ((file-exists-p (concat file
".gz"))
2530 (concat file
".gz"))
2531 ((file-exists-p (concat file
".z"))
2535 (defun tramp-sh-handle-insert-directory
2536 (filename switches
&optional wildcard full-directory-p
)
2537 "Like `insert-directory' for Tramp files."
2538 (setq filename
(expand-file-name filename
))
2539 (with-parsed-tramp-file-name filename nil
2540 (if (and (featurep 'ls-lisp
)
2541 (not (symbol-value 'ls-lisp-use-insert-directory-program
)))
2542 (tramp-run-real-handler
2543 'insert-directory
(list filename switches wildcard full-directory-p
))
2544 (when (stringp switches
)
2545 (setq switches
(split-string switches
)))
2546 (when (and (member "--dired" switches
)
2547 (not (tramp-get-ls-command-with-dired v
)))
2548 (setq switches
(delete "--dired" switches
)))
2550 (setq wildcard
(tramp-run-real-handler
2551 'file-name-nondirectory
(list localname
)))
2552 (setq localname
(tramp-run-real-handler
2553 'file-name-directory
(list localname
))))
2554 (unless full-directory-p
2555 (setq switches
(add-to-list 'switches
"-d" 'append
)))
2556 (setq switches
(mapconcat 'tramp-shell-quote-argument switches
" "))
2558 (setq switches
(concat switches
" " wildcard
)))
2560 v
4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2561 switches filename
(if wildcard
"yes" "no")
2562 (if full-directory-p
"yes" "no"))
2563 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2564 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2565 (if full-directory-p
2568 (format "%s %s %s 2>/dev/null"
2569 (tramp-get-ls-command v
)
2573 (tramp-shell-quote-argument (concat localname
".")))))
2574 (tramp-barf-unless-okay
2576 (format "cd %s" (tramp-shell-quote-argument
2577 (tramp-run-real-handler
2578 'file-name-directory
(list localname
))))
2580 (tramp-shell-quote-argument
2581 (tramp-run-real-handler 'file-name-directory
(list localname
))))
2585 (tramp-get-ls-command v
)
2589 (tramp-run-real-handler
2590 'file-name-nondirectory
(list localname
)))))
2592 (tramp-shell-quote-argument
2593 (tramp-run-real-handler
2594 'file-name-nondirectory
(list localname
)))))))
2595 (let ((beg (point)))
2596 ;; We cannot use `insert-buffer-substring' because the Tramp
2597 ;; buffer changes its contents before insertion due to calling
2598 ;; `expand-file' and alike.
2600 (with-current-buffer (tramp-get-buffer v
)
2603 ;; Check for "--dired" output.
2605 (when (looking-at "//SUBDIRED//")
2607 (when (looking-at "//DIRED//\\s-+")
2608 (let ((databeg (match-end 0))
2609 (end (point-at-eol)))
2610 ;; Now read the numeric positions of file names.
2612 (while (< (point) end
)
2613 (let ((start (+ beg
(read (current-buffer))))
2614 (end (+ beg
(read (current-buffer)))))
2615 (if (memq (char-after end
) '(?
\n ?\
))
2616 ;; End is followed by \n or by " -> ".
2617 (put-text-property start end
'dired-filename t
))))))
2618 ;; Remove trailing lines.
2619 (goto-char (point-at-bol))
2620 (while (looking-at "//")
2622 (delete-region (match-beginning 0) (point)))
2624 ;; Some busyboxes are reluctant to discard colors.
2625 (unless (string-match "color" (tramp-get-connection-property v
"ls" ""))
2627 (while (re-search-forward tramp-color-escape-sequence-regexp nil t
)
2628 (replace-match "")))
2630 ;; The inserted file could be from somewhere else.
2631 (when (and (not wildcard
) (not full-directory-p
))
2632 (goto-char (point-max))
2633 (when (file-symlink-p filename
)
2634 (goto-char (search-backward "->" beg
'noerror
)))
2636 (if (zerop (length (file-name-nondirectory filename
)))
2638 (file-name-nondirectory filename
))
2640 (replace-match (file-relative-name filename
) t
))
2642 (goto-char (point-max))))))
2644 ;; Canonicalization of file names.
2646 (defun tramp-sh-handle-expand-file-name (name &optional dir
)
2647 "Like `expand-file-name' for Tramp files.
2648 If the localname part of the given filename starts with \"/../\" then
2649 the result will be a local, non-Tramp, filename."
2650 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
2651 (setq dir
(or dir default-directory
"/"))
2652 ;; Unless NAME is absolute, concat DIR and NAME.
2653 (unless (file-name-absolute-p name
)
2654 (setq name
(concat (file-name-as-directory dir
) name
)))
2655 ;; If NAME is not a Tramp file, run the real handler.
2656 (if (not (tramp-connectable-p name
))
2657 (tramp-run-real-handler 'expand-file-name
(list name nil
))
2659 (with-parsed-tramp-file-name name nil
2660 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
2661 (setq localname
(concat "~/" localname
)))
2662 ;; Tilde expansion if necessary. This needs a shell which
2663 ;; groks tilde expansion! The function `tramp-find-shell' is
2664 ;; supposed to find such a shell on the remote host. Please
2665 ;; tell me about it when this doesn't work on your system.
2666 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname
)
2667 (let ((uname (match-string 1 localname
))
2668 (fname (match-string 2 localname
)))
2669 ;; We cannot simply apply "~/", because under sudo "~/" is
2670 ;; expanded to the local user home directory but to the
2671 ;; root home directory. On the other hand, using always
2672 ;; the default user name for tilde expansion is not
2673 ;; appropriate either, because ssh and companions might
2674 ;; use a user name from the config file.
2675 (when (and (string-equal uname
"~")
2676 (string-match "\\`su\\(do\\)?\\'" method
))
2677 (setq uname
(concat uname user
)))
2679 (with-connection-property v uname
2681 v
(format "cd %s; pwd" (tramp-shell-quote-argument uname
)))
2682 (with-current-buffer (tramp-get-buffer v
)
2683 (goto-char (point-min))
2684 (buffer-substring (point) (point-at-eol)))))
2685 (setq localname
(concat uname fname
))))
2686 ;; There might be a double slash, for example when "~/"
2687 ;; expands to "/". Remove this.
2688 (while (string-match "//" localname
)
2689 (setq localname
(replace-match "/" t t localname
)))
2690 ;; No tilde characters in file name, do normal
2691 ;; `expand-file-name' (this does "/./" and "/../"). We bind
2692 ;; `directory-sep-char' here for XEmacs on Windows, which would
2693 ;; otherwise use backslash. `default-directory' is bound,
2694 ;; because on Windows there would be problems with UNC shares or
2696 (let ((directory-sep-char ?
/)
2697 (default-directory (tramp-compat-temporary-file-directory)))
2698 (tramp-make-tramp-file-name
2700 (tramp-drop-volume-letter
2701 (tramp-run-real-handler
2702 'expand-file-name
(list localname
))))))))
2704 ;;; Remote commands:
2706 (defun tramp-sh-handle-executable-find (command)
2707 "Like `executable-find' for Tramp files."
2708 (with-parsed-tramp-file-name default-directory nil
2709 (tramp-find-executable v command
(tramp-get-remote-path v
) t
)))
2711 (defun tramp-process-sentinel (proc event
)
2712 "Flush file caches."
2713 (unless (memq (process-status proc
) '(run open
))
2714 (let ((vec (tramp-get-connection-property proc
"vector" nil
)))
2716 (tramp-message vec
5 "Sentinel called: `%s' `%s'" proc event
)
2717 (tramp-flush-connection-property proc
)
2718 (tramp-flush-directory-property vec
"")))))
2720 ;; We use BUFFER also as connection buffer during setup. Because of
2721 ;; this, its original contents must be saved, and restored once
2722 ;; connection has been setup.
2723 (defun tramp-sh-handle-start-file-process (name buffer program
&rest args
)
2724 "Like `start-file-process' for Tramp files."
2725 (with-parsed-tramp-file-name default-directory nil
2726 ;; When PROGRAM is nil, we just provide a tty.
2728 (when (stringp program
)
2729 (format "cd %s; exec env PS1=%s %s"
2730 (tramp-shell-quote-argument localname
)
2731 ;; Use a human-friendly prompt, for example for `shell'.
2732 (tramp-shell-quote-argument
2734 (file-remote-p default-directory
)
2735 tramp-initial-end-of-output
))
2736 (mapconcat 'tramp-shell-quote-argument
2737 (cons program args
) " "))))
2738 (tramp-process-connection-type
2739 (or (null program
) tramp-process-connection-type
))
2740 (bmp (and (buffer-live-p buffer
) (buffer-modified-p buffer
)))
2747 ;; BUFFER can be nil. We use a temporary buffer.
2748 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
2749 (while (get-process name1
)
2750 ;; NAME must be unique as process name.
2752 name1
(format "%s<%d>" name i
)))
2754 ;; Set the new process properties.
2755 (tramp-set-connection-property v
"process-name" name
)
2756 (tramp-set-connection-property v
"process-buffer" buffer
)
2757 ;; Activate narrowing in order to save BUFFER contents.
2758 ;; Clear also the modification time; otherwise we might
2759 ;; be interrupted by `verify-visited-file-modtime'.
2760 (with-current-buffer (tramp-get-connection-buffer v
)
2761 (let ((buffer-undo-list t
))
2762 (clear-visited-file-modtime)
2763 (narrow-to-region (point-max) (point-max))
2765 ;; Send the command.
2766 (tramp-send-command v command nil t
) ; nooutput
2767 ;; Check, whether a pty is associated.
2768 (tramp-maybe-open-connection v
)
2769 (unless (tramp-compat-process-get
2770 (tramp-get-connection-process v
) 'remote-tty
)
2773 "pty association is not supported for `%s'" name
)))))
2774 (let ((p (tramp-get-connection-process v
)))
2775 ;; Set query flag for this process.
2776 (tramp-compat-set-process-query-on-exit-flag p t
)
2780 (with-current-buffer (tramp-get-connection-buffer v
)
2781 (if (string-match tramp-temp-buffer-name
(buffer-name))
2783 (set-process-buffer (tramp-get-connection-process v
) nil
)
2784 (kill-buffer (current-buffer)))
2785 (set-buffer-modified-p bmp
)))
2786 (tramp-set-connection-property v
"process-name" nil
)
2787 (tramp-set-connection-property v
"process-buffer" nil
)))))
2789 (defun tramp-sh-handle-process-file
2790 (program &optional infile destination display
&rest args
)
2791 "Like `process-file' for Tramp files."
2792 ;; The implementation is not complete yet.
2793 (when (and (numberp destination
) (zerop destination
))
2794 (error "Implementation does not handle immediate return"))
2796 (with-parsed-tramp-file-name default-directory nil
2797 (let (command input tmpinput stderr tmpstderr outbuf ret
)
2799 (setq command
(mapconcat 'tramp-shell-quote-argument
2800 (cons program args
) " "))
2803 (setq input
"/dev/null")
2804 (setq infile
(expand-file-name infile
))
2805 (if (tramp-equal-remote default-directory infile
)
2806 ;; INFILE is on the same remote host.
2807 (setq input
(with-parsed-tramp-file-name infile nil localname
))
2808 ;; INFILE must be copied to remote host.
2809 (setq input
(tramp-make-tramp-temp-file v
)
2810 tmpinput
(tramp-make-tramp-file-name method user host input
))
2811 (copy-file infile tmpinput t
)))
2812 (when input
(setq command
(format "%s <%s" command input
)))
2814 ;; Determine output.
2817 ((bufferp destination
)
2818 (setq outbuf destination
))
2820 ((stringp destination
)
2821 (setq outbuf
(get-buffer-create destination
)))
2822 ;; (REAL-DESTINATION ERROR-DESTINATION)
2823 ((consp destination
)
2826 ((bufferp (car destination
))
2827 (setq outbuf
(car destination
)))
2828 ((stringp (car destination
))
2829 (setq outbuf
(get-buffer-create (car destination
))))
2831 (setq outbuf
(current-buffer))))
2834 ((stringp (cadr destination
))
2835 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
2836 (if (tramp-equal-remote default-directory
(cadr destination
))
2837 ;; stderr is on the same remote host.
2838 (setq stderr
(with-parsed-tramp-file-name
2839 (cadr destination
) nil localname
))
2840 ;; stderr must be copied to remote host. The temporary
2841 ;; file must be deleted after execution.
2842 (setq stderr
(tramp-make-tramp-temp-file v
)
2843 tmpstderr
(tramp-make-tramp-file-name
2844 method user host stderr
))))
2845 ;; stderr to be discarded.
2846 ((null (cadr destination
))
2847 (setq stderr
"/dev/null"))))
2850 (setq outbuf
(current-buffer))))
2851 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
2853 ;; Send the command. It might not return in time, so we protect
2854 ;; it. Call it in a subshell, in order to preserve working
2859 (if (tramp-send-command-and-check
2860 v
(format "\\cd %s; %s"
2861 (tramp-shell-quote-argument localname
)
2865 ;; We should show the output anyway.
2867 (with-current-buffer outbuf
2869 (with-current-buffer (tramp-get-connection-buffer v
)
2871 (when display
(display-buffer outbuf
))))
2872 ;; When the user did interrupt, we should do it also. We use
2873 ;; return code -1 as marker.
2875 (kill-buffer (tramp-get-connection-buffer v
))
2879 (kill-buffer (tramp-get-connection-buffer v
))
2882 ;; Provide error file.
2883 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
2885 ;; Cleanup. We remove all file cache values for the connection,
2886 ;; because the remote process could have changed them.
2887 (when tmpinput
(delete-file tmpinput
))
2889 ;; `process-file-side-effects' has been introduced with GNU
2890 ;; Emacs 23.2. If set to `nil', no remote file will be changed
2891 ;; by `program'. If it doesn't exist, we assume its default
2893 (unless (and (boundp 'process-file-side-effects
)
2894 (not (symbol-value 'process-file-side-effects
)))
2895 (tramp-flush-directory-property v
""))
2897 ;; Return exit status.
2902 (defun tramp-sh-handle-call-process-region
2903 (start end program
&optional delete buffer display
&rest args
)
2904 "Like `call-process-region' for Tramp files."
2905 (let ((tmpfile (tramp-compat-make-temp-file "")))
2906 (write-region start end tmpfile
)
2907 (when delete
(delete-region start end
))
2909 (apply 'call-process program tmpfile buffer display args
)
2910 (delete-file tmpfile
))))
2912 (defun tramp-sh-handle-file-local-copy (filename)
2913 "Like `file-local-copy' for Tramp files."
2914 (with-parsed-tramp-file-name filename nil
2915 (unless (file-exists-p filename
)
2918 "Cannot make local copy of non-existing file `%s'" filename
))
2920 (let* ((size (nth 7 (file-attributes (file-truename filename
))))
2921 (rem-enc (tramp-get-inline-coding v
"remote-encoding" size
))
2922 (loc-dec (tramp-get-inline-coding v
"local-decoding" size
))
2923 (tmpfile (tramp-compat-make-temp-file filename
)))
2927 ;; `copy-file' handles direct copy and out-of-band methods.
2928 ((or (tramp-local-host-p v
)
2929 (tramp-method-out-of-band-p v size
))
2930 (copy-file filename tmpfile t t
))
2932 ;; Use inline encoding for file transfer.
2935 (tramp-with-progress-reporter
2936 v
3 (format "Encoding remote file %s" filename
)
2937 (tramp-barf-unless-okay
2938 v
(format rem-enc
(tramp-shell-quote-argument localname
))
2939 "Encoding remote file failed"))
2941 (if (functionp loc-dec
)
2942 ;; If local decoding is a function, we call it. We
2943 ;; must disable multibyte, because
2944 ;; `uudecode-decode-region' doesn't handle it
2947 (set-buffer-multibyte nil
)
2948 (insert-buffer-substring (tramp-get-buffer v
))
2949 (tramp-with-progress-reporter
2950 v
3 (format "Decoding remote file %s with function %s"
2952 (funcall loc-dec
(point-min) (point-max))
2953 ;; Unset `file-name-handler-alist'. Otherwise,
2954 ;; epa-file gets confused.
2955 (let (file-name-handler-alist
2956 (coding-system-for-write 'binary
))
2957 (write-region (point-min) (point-max) tmpfile
))))
2959 ;; If tramp-decoding-function is not defined for this
2960 ;; method, we invoke tramp-decoding-command instead.
2961 (let ((tmpfile2 (tramp-compat-make-temp-file filename
)))
2962 ;; Unset `file-name-handler-alist'. Otherwise,
2963 ;; epa-file gets confused.
2964 (let (file-name-handler-alist
2965 (coding-system-for-write 'binary
))
2966 (write-region (point-min) (point-max) tmpfile2
))
2967 (tramp-with-progress-reporter
2968 v
3 (format "Decoding remote file %s with command %s"
2971 (tramp-call-local-coding-command
2972 loc-dec tmpfile2 tmpfile
)
2973 (delete-file tmpfile2
)))))
2975 ;; Set proper permissions.
2976 (set-file-modes tmpfile
(tramp-default-file-modes filename
))
2977 ;; Set local user ownership.
2978 (tramp-set-file-uid-gid tmpfile
)))
2980 ;; Oops, I don't know what to do.
2982 v
'file-error
"Wrong method specification for `%s'" method
)))
2986 (delete-file tmpfile
)
2987 (signal (car err
) (cdr err
))))
2989 (run-hooks 'tramp-handle-file-local-copy-hook
)
2992 ;; This is needed for XEmacs only. Code stolen from files.el.
2993 (defun tramp-sh-handle-insert-file-contents-literally
2994 (filename &optional visit beg end replace
)
2995 "Like `insert-file-contents-literally' for Tramp files."
2996 (let ((format-alist nil
)
2997 (after-insert-file-functions nil
)
2998 (coding-system-for-read 'no-conversion
)
2999 (coding-system-for-write 'no-conversion
)
3000 (find-buffer-file-type-function
3001 (if (fboundp 'find-buffer-file-type
)
3002 (symbol-function 'find-buffer-file-type
)
3004 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler
))
3005 (inhibit-file-name-operation 'insert-file-contents
))
3008 (fset 'find-buffer-file-type
(lambda (filename) t
))
3009 (insert-file-contents filename visit beg end replace
))
3011 (if find-buffer-file-type-function
3012 (fset 'find-buffer-file-type find-buffer-file-type-function
)
3013 (fmakunbound 'find-buffer-file-type
)))))
3015 (defun tramp-sh-handle-make-auto-save-file-name ()
3016 "Like `make-auto-save-file-name' for Tramp files.
3017 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
3018 (let ((tramp-auto-save-directory tramp-auto-save-directory
)
3020 (tramp-subst-strs-in-string
3027 (buffer-file-name))))
3028 ;; File name must be unique. This is ensured with Emacs 22 (see
3029 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
3030 ;; all other cases we must do it ourselves.
3031 (when (boundp 'auto-save-file-name-transforms
)
3034 (when (and (string-match (car x
) buffer-file-name
)
3035 (not (car (cddr x
))))
3036 (setq tramp-auto-save-directory
3037 (or tramp-auto-save-directory
3038 (tramp-compat-temporary-file-directory)))))
3039 (symbol-value 'auto-save-file-name-transforms
)))
3040 ;; Create directory.
3041 (when tramp-auto-save-directory
3042 (setq buffer-file-name
3043 (expand-file-name buffer-file-name tramp-auto-save-directory
))
3044 (unless (file-exists-p tramp-auto-save-directory
)
3045 (make-directory tramp-auto-save-directory t
)))
3046 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3047 ;; it is not a magic file name operation (since Emacs 22).
3048 ;; We must deactivate it temporarily.
3049 (if (not (ad-is-active 'make-auto-save-file-name
))
3050 (tramp-run-real-handler 'make-auto-save-file-name nil
)
3052 (ad-deactivate 'make-auto-save-file-name
)
3054 (tramp-run-real-handler 'make-auto-save-file-name nil
)
3055 (ad-activate 'make-auto-save-file-name
)))))
3057 ;; CCC grok LOCKNAME
3058 (defun tramp-sh-handle-write-region
3059 (start end filename
&optional append visit lockname confirm
)
3060 "Like `write-region' for Tramp files."
3061 (setq filename
(expand-file-name filename
))
3062 (with-parsed-tramp-file-name filename nil
3063 ;; Following part commented out because we don't know what to do about
3064 ;; file locking, and it does not appear to be a problem to ignore it.
3065 ;; Ange-ftp ignores it, too.
3066 ;; (when (and lockname (stringp lockname))
3067 ;; (setq lockname (expand-file-name lockname)))
3068 ;; (unless (or (eq lockname nil)
3069 ;; (string= lockname filename))
3071 ;; "tramp-sh-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3073 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
3074 (when (and (not (featurep 'xemacs
)) confirm
(file-exists-p filename
))
3075 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename
))
3076 (tramp-error v
'file-error
"File not overwritten")))
3078 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename
'integer
))
3079 (tramp-get-remote-uid v
'integer
)))
3080 (gid (or (nth 3 (tramp-compat-file-attributes filename
'integer
))
3081 (tramp-get-remote-gid v
'integer
))))
3083 (if (and (tramp-local-host-p v
)
3084 ;; `file-writable-p' calls `file-expand-file-name'. We
3085 ;; cannot use `tramp-run-real-handler' therefore.
3086 (let (file-name-handler-alist)
3088 (file-writable-p (file-name-directory localname
))
3089 (or (file-directory-p localname
)
3090 (file-writable-p localname
)))))
3091 ;; Short track: if we are on the local host, we can run directly.
3092 (tramp-run-real-handler
3094 (list start end localname append
'no-message lockname confirm
))
3096 (let ((modes (save-excursion (tramp-default-file-modes filename
)))
3097 ;; We use this to save the value of
3098 ;; `last-coding-system-used' after writing the tmp
3099 ;; file. At the end of the function, we set
3100 ;; `last-coding-system-used' to this saved value. This
3101 ;; way, any intermediary coding systems used while
3102 ;; talking to the remote shell or suchlike won't hose
3103 ;; this variable. This approach was snarfed from
3106 ;; Write region into a tmp file. This isn't really
3107 ;; needed if we use an encoding function, but currently
3108 ;; we use it always because this makes the logic
3110 (tmpfile (or tramp-temp-buffer-file-name
3111 (tramp-compat-make-temp-file filename
))))
3113 ;; If `append' is non-nil, we copy the file locally, and let
3114 ;; the native `write-region' implementation do the job.
3115 (when append
(copy-file filename tmpfile
'ok
))
3117 ;; We say `no-message' here because we don't want the
3118 ;; visited file modtime data to be clobbered from the temp
3119 ;; file. We call `set-visited-file-modtime' ourselves later
3120 ;; on. We must ensure that `file-coding-system-alist'
3121 ;; matches `tmpfile'.
3122 (let (file-name-handler-alist
3123 (file-coding-system-alist
3124 (tramp-find-file-name-coding-system-alist filename tmpfile
)))
3126 (tramp-run-real-handler
3128 (list start end tmpfile append
'no-message lockname confirm
))
3130 (setq tramp-temp-buffer-file-name nil
)
3131 (delete-file tmpfile
)
3132 (signal (car err
) (cdr err
))))
3134 ;; Now, `last-coding-system-used' has the right value. Remember it.
3135 (when (boundp 'last-coding-system-used
)
3136 (setq coding-system-used
3137 (symbol-value 'last-coding-system-used
))))
3139 ;; The permissions of the temporary file should be set. If
3140 ;; filename does not exist (eq modes nil) it has been
3141 ;; renamed to the backup file. This case `save-buffer'
3142 ;; handles permissions.
3143 ;; Ensure that it is still readable.
3147 (logior (or modes
0) (tramp-compat-octal-to-decimal "0400"))))
3149 ;; This is a bit lengthy due to the different methods
3150 ;; possible for file transfer. First, we check whether the
3151 ;; method uses an rcp program. If so, we call it.
3152 ;; Otherwise, both encoding and decoding command must be
3153 ;; specified. However, if the method _also_ specifies an
3154 ;; encoding function, then that is used for encoding the
3155 ;; contents of the tmp file.
3156 (let* ((size (nth 7 (file-attributes tmpfile
)))
3157 (rem-dec (tramp-get-inline-coding v
"remote-decoding" size
))
3158 (loc-enc (tramp-get-inline-coding v
"local-encoding" size
)))
3160 ;; `copy-file' handles direct copy and out-of-band methods.
3161 ((or (tramp-local-host-p v
)
3162 (tramp-method-out-of-band-p v size
))
3163 (if (and (not (stringp start
))
3164 (= (or end
(point-max)) (point-max))
3165 (= (or start
(point-min)) (point-min))
3166 (tramp-get-method-parameter
3167 method
'tramp-copy-keep-tmpfile
))
3169 (setq tramp-temp-buffer-file-name tmpfile
)
3171 ;; We keep the local file for performance
3172 ;; reasons, useful for "rsync".
3173 (copy-file tmpfile filename t
)
3175 (setq tramp-temp-buffer-file-name nil
)
3176 (delete-file tmpfile
)
3177 (signal (car err
) (cdr err
)))))
3178 (setq tramp-temp-buffer-file-name nil
)
3179 ;; Don't rename, in order to keep context in SELinux.
3181 (copy-file tmpfile filename t
)
3182 (delete-file tmpfile
))))
3184 ;; Use inline file transfer.
3189 (set-buffer-multibyte nil
)
3190 ;; Use encoding function or command.
3191 (if (functionp loc-enc
)
3192 (tramp-with-progress-reporter
3193 v
3 (format "Encoding region using function `%s'"
3195 (let ((coding-system-for-read 'binary
))
3196 (insert-file-contents-literally tmpfile
))
3197 ;; The following `let' is a workaround for the
3198 ;; base64.el that comes with pgnus-0.84. If
3199 ;; both of the following conditions are
3200 ;; satisfied, it tries to write to a local
3201 ;; file in default-directory, but at this
3202 ;; point, default-directory is remote.
3203 ;; (`call-process-region' can't write to
3204 ;; remote files, it seems.) The file in
3205 ;; question is a tmp file anyway.
3206 (let ((default-directory
3207 (tramp-compat-temporary-file-directory)))
3208 (funcall loc-enc
(point-min) (point-max))))
3210 (tramp-with-progress-reporter
3211 v
3 (format "Encoding region using command `%s'"
3213 (unless (zerop (tramp-call-local-coding-command
3217 (concat "Cannot write to `%s', "
3218 "local encoding command `%s' failed")
3219 filename loc-enc
))))
3221 ;; Send buffer into remote decoding command which
3222 ;; writes to remote file. Because this happens on
3223 ;; the remote host, we cannot use the function.
3224 (tramp-with-progress-reporter
3226 (format "Decoding region into remote file %s" filename
)
3227 (goto-char (point-max))
3228 (unless (bolp) (newline))
3232 (concat rem-dec
" <<'EOF'\n%sEOF")
3233 (tramp-shell-quote-argument localname
)
3235 (tramp-barf-unless-okay
3237 "Couldn't write region to `%s', decode using `%s' failed"
3239 ;; When `file-precious-flag' is set, the region is
3240 ;; written to a temporary file. Check that the
3241 ;; checksum is equal to that from the local tmpfile.
3242 (when file-precious-flag
3245 ;; cksum runs locally, if possible.
3246 (zerop (tramp-compat-call-process "cksum" tmpfile t
))
3247 ;; cksum runs remotely.
3248 (tramp-send-command-and-check
3251 "cksum <%s" (tramp-shell-quote-argument localname
)))
3252 ;; ... they are different.
3256 (with-current-buffer (tramp-get-buffer v
)
3260 (concat "Couldn't write region to `%s',"
3261 " decode using `%s' failed")
3262 filename rem-dec
)))))
3265 (delete-file tmpfile
)))
3267 ;; That's not expected.
3271 (concat "Method `%s' should specify both encoding and "
3272 "decoding command or an rcp program")
3275 ;; Make `last-coding-system-used' have the right value.
3276 (when coding-system-used
3277 (set 'last-coding-system-used coding-system-used
))))
3279 (tramp-flush-file-property v
(file-name-directory localname
))
3280 (tramp-flush-file-property v localname
)
3282 ;; We must protect `last-coding-system-used', now we have set it
3283 ;; to its correct value.
3284 (let (last-coding-system-used (need-chown t
))
3285 ;; Set file modification time.
3286 (when (or (eq visit t
) (stringp visit
))
3287 (let ((file-attr (file-attributes filename
)))
3288 (set-visited-file-modtime
3289 ;; We must pass modtime explicitly, because filename can
3290 ;; be different from (buffer-file-name), f.e. if
3291 ;; `file-precious-flag' is set.
3293 (when (and (eq (nth 2 file-attr
) uid
)
3294 (eq (nth 3 file-attr
) gid
))
3295 (setq need-chown nil
))))
3297 ;; Set the ownership.
3299 (tramp-set-file-uid-gid filename uid gid
))
3300 (when (or (eq visit t
) (null visit
) (stringp visit
))
3301 (tramp-message v
0 "Wrote %s" filename
))
3302 (run-hooks 'tramp-handle-write-region-hook
)))))
3304 (defvar tramp-vc-registered-file-names nil
3305 "List used to collect file names, which are checked during `vc-registered'.")
3307 ;; VC backends check for the existence of various different special
3308 ;; files. This is very time consuming, because every single check
3309 ;; requires a remote command (the file cache must be invalidated).
3310 ;; Therefore, we apply a kind of optimization. We install the file
3311 ;; name handler `tramp-vc-file-name-handler', which does nothing but
3312 ;; remembers all file names for which `file-exists-p' or
3313 ;; `file-readable-p' has been applied. A first run of `vc-registered'
3314 ;; is performed. Afterwards, a script is applied for all collected
3315 ;; file names, using just one remote command. The result of this
3316 ;; script is used to fill the file cache with actual values. Now we
3317 ;; can reset the file name handlers, and we make a second run of
3318 ;; `vc-registered', which returns the expected result without sending
3319 ;; any other remote command.
3320 (defun tramp-sh-handle-vc-registered (file)
3321 "Like `vc-registered' for Tramp files."
3322 (tramp-compat-with-temp-message ""
3323 (with-parsed-tramp-file-name file nil
3324 (tramp-with-progress-reporter
3325 v
3 (format "Checking `vc-registered' for %s" file
)
3327 ;; There could be new files, created by the vc backend. We
3328 ;; cannot reuse the old cache entries, therefore.
3329 (let (tramp-vc-registered-file-names
3330 (remote-file-name-inhibit-cache (current-time))
3331 (file-name-handler-alist
3332 `((,tramp-file-name-regexp . tramp-vc-file-name-handler
))))
3334 ;; Here we collect only file names, which need an operation.
3335 (tramp-run-real-handler 'vc-registered
(list file
))
3336 (tramp-message v
10 "\n%s" tramp-vc-registered-file-names
)
3338 ;; Send just one command, in order to fill the cache.
3339 (when tramp-vc-registered-file-names
3340 (tramp-maybe-send-script
3342 (format tramp-vc-registered-read-file-names
3343 (tramp-get-file-exists-command v
)
3344 (format "%s -r" (tramp-get-test-command v
)))
3345 "tramp_vc_registered_read_file_names")
3349 (tramp-send-command-and-read
3352 "tramp_vc_registered_read_file_names <<'EOF'\n%s\nEOF\n"
3353 (mapconcat 'tramp-shell-quote-argument
3354 tramp-vc-registered-file-names
3357 (tramp-set-file-property
3358 v
(car elt
) (cadr elt
) (cadr (cdr elt
))))))
3360 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3361 ;; calls shall be answered from the file cache. We unset
3362 ;; `process-file-side-effects' in order to keep the cache when
3363 ;; `process-file' calls appear.
3364 (let (process-file-side-effects)
3365 (tramp-run-real-handler 'vc-registered
(list file
)))))))
3367 ;;;###tramp-autoload
3368 (defun tramp-sh-file-name-handler (operation &rest args
)
3369 "Invoke remote-shell Tramp file name handler.
3370 Fall back to normal file name handler if no Tramp handler exists."
3371 (when (and tramp-locked
(not tramp-locker
))
3372 (setq tramp-locked nil
)
3373 (signal 'file-error
(list "Forbidden reentrant call of Tramp")))
3374 (let ((tl tramp-locked
))
3377 (setq tramp-locked t
)
3378 (let ((tramp-locker t
))
3380 (let ((fn (assoc operation tramp-sh-file-name-handler-alist
)))
3382 (apply (cdr fn
) args
)
3383 (tramp-run-real-handler operation args
))))))
3384 (setq tramp-locked tl
))))
3386 (defun tramp-vc-file-name-handler (operation &rest args
)
3387 "Invoke special file name handler, which collects files to be handled."
3390 (tramp-replace-environment-variables
3391 (apply 'tramp-file-name-for-operation operation args
)))
3392 (fn (assoc operation tramp-sh-file-name-handler-alist
)))
3393 (with-parsed-tramp-file-name filename nil
3395 ;; That's what we want: file names, for which checks are
3396 ;; applied. We assume that VC uses only `file-exists-p' and
3397 ;; `file-readable-p' checks; otherwise we must extend the
3398 ;; list. We do not perform any action, but return nil, in
3399 ;; order to keep `vc-registered' running.
3400 ((and fn
(memq operation
'(file-exists-p file-readable-p
)))
3401 (add-to-list 'tramp-vc-registered-file-names localname
'append
)
3403 ;; Tramp file name handlers like `expand-file-name'. They
3406 (save-match-data (apply (cdr fn
) args
)))
3407 ;; Default file name handlers, we don't care.
3408 (t (tramp-run-real-handler operation args
)))))))
3410 ;;; Internal Functions:
3412 (defun tramp-maybe-send-script (vec script name
)
3413 "Define in remote shell function NAME implemented as SCRIPT.
3414 Only send the definition if it has not already been done."
3415 ;; We cannot let-bind (tramp-get-connection-process vec) because it
3417 (let ((scripts (tramp-get-connection-property
3418 (tramp-get-connection-process vec
) "scripts" nil
)))
3419 (unless (member name scripts
)
3420 (tramp-with-progress-reporter vec
5 (format "Sending script `%s'" name
)
3421 ;; The script could contain a call of Perl. This is masked with `%s'.
3422 (tramp-barf-unless-okay
3424 (format "%s () {\n%s\n}" name
3425 (format script
(tramp-get-remote-perl vec
)))
3426 "Script %s sending failed" name
)
3427 (tramp-set-connection-property
3428 (tramp-get-connection-process vec
) "scripts" (cons name scripts
))))))
3430 (defun tramp-set-auto-save ()
3431 (when (and ;; ange-ftp has its own auto-save mechanism
3432 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
3433 'tramp-sh-file-name-handler
)
3435 (auto-save-mode 1)))
3436 (add-hook 'find-file-hooks
'tramp-set-auto-save t
)
3437 (add-hook 'tramp-unload-hook
3439 (remove-hook 'find-file-hooks
'tramp-set-auto-save
)))
3441 (defun tramp-run-test (switch filename
)
3442 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3443 Returns the exit code of the `test' program."
3444 (with-parsed-tramp-file-name filename nil
3445 (tramp-send-command-and-check
3449 (tramp-get-test-command v
)
3451 (tramp-shell-quote-argument localname
)))))
3453 (defun tramp-run-test2 (format-string file1 file2
)
3454 "Run `test'-like program on the remote system, given FILE1, FILE2.
3455 FORMAT-STRING contains the program name, switches, and place holders.
3456 Returns the exit code of the `test' program. Barfs if the methods,
3457 hosts, or files, disagree."
3458 (unless (tramp-equal-remote file1 file2
)
3459 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1
) file1 file2
) nil
3462 "tramp-run-test2 only implemented for same method, user, host")))
3463 (with-parsed-tramp-file-name file1 v1
3464 (with-parsed-tramp-file-name file1 v2
3465 (tramp-send-command-and-check
3467 (format format-string
3468 (tramp-shell-quote-argument v1-localname
)
3469 (tramp-shell-quote-argument v2-localname
))))))
3471 (defun tramp-find-executable
3472 (vec progname dirlist
&optional ignore-tilde ignore-path
)
3473 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3474 First arg VEC specifies the connection, PROGNAME is the program
3475 to search for, and DIRLIST gives the list of directories to
3476 search. If IGNORE-TILDE is non-nil, directory names starting
3477 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3480 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3482 This function expects to be in the right *tramp* buffer."
3483 (with-current-buffer (tramp-get-connection-buffer vec
)
3485 ;; Check whether the executable is in $PATH. "which(1)" does not
3486 ;; report always a correct error code; therefore we check the
3487 ;; number of words it returns.
3489 (tramp-send-command vec
(format "which \\%s | wc -w" progname
))
3490 (goto-char (point-min))
3491 (if (looking-at "^\\s-*1$")
3492 (setq result
(concat "\\" progname
))))
3495 ;; Remove all ~/foo directories from dirlist. In XEmacs,
3496 ;; `remove' is in CL, and we want to avoid CL dependencies.
3499 (setq d
(car dirlist
))
3500 (setq dirlist
(cdr dirlist
))
3501 (unless (char-equal ?~
(aref d
0))
3502 (setq newdl
(cons d newdl
))))
3503 (setq dirlist
(nreverse newdl
))))
3506 (format (concat "while read d; "
3507 "do if test -x $d/%s -a -f $d/%s; "
3508 "then echo tramp_executable $d/%s; "
3509 "break; fi; done <<'EOF'\n"
3511 progname progname progname
(mapconcat 'identity dirlist
"\n")))
3512 (goto-char (point-max))
3513 (when (search-backward "tramp_executable " nil t
)
3514 (skip-chars-forward "^ ")
3515 (skip-chars-forward " ")
3516 (setq result
(buffer-substring (point) (point-at-eol)))))
3519 (defun tramp-set-remote-path (vec)
3520 "Sets the remote environment PATH to existing directories.
3521 I.e., for each directory in `tramp-remote-path', it is tested
3522 whether it exists and if so, it is added to the environment
3524 (tramp-message vec
5 (format "Setting $PATH environment variable"))
3526 vec
(format "PATH=%s; export PATH"
3527 (mapconcat 'identity
(tramp-get-remote-path vec
) ":"))))
3529 ;; ------------------------------------------------------------
3530 ;; -- Communication with external shell --
3531 ;; ------------------------------------------------------------
3533 (defun tramp-find-file-exists-command (vec)
3534 "Find a command on the remote host for checking if a file exists.
3535 Here, we are looking for a command which has zero exit status if the
3536 file exists and nonzero exit status otherwise."
3537 (let ((existing "/")
3539 (tramp-shell-quote-argument "/ this file does not exist "))
3541 ;; The algorithm is as follows: we try a list of several commands.
3542 ;; For each command, we first run `$cmd /' -- this should return
3543 ;; true, as the root directory always exists. And then we run
3544 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
3545 ;; does not exist. This should return false. We use the first
3546 ;; command we find that seems to work.
3547 ;; The list of commands to try is as follows:
3548 ;; `ls -d' This works on most systems, but NetBSD 1.4
3549 ;; has a bug: `ls' always returns zero exit
3550 ;; status, even for files which don't exist.
3551 ;; `test -e' Some Bourne shells have a `test' builtin
3552 ;; which does not know the `-e' option.
3553 ;; `/bin/test -e' For those, the `test' binary on disk normally
3554 ;; provides the option. Alas, the binary
3555 ;; is sometimes `/bin/test' and sometimes it's
3557 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3559 (and (setq result
(format "%s -e" (tramp-get-test-command vec
)))
3560 (tramp-send-command-and-check
3561 vec
(format "%s %s" result existing
))
3562 (not (tramp-send-command-and-check
3563 vec
(format "%s %s" result nonexistent
))))
3564 (and (setq result
"/bin/test -e")
3565 (tramp-send-command-and-check
3566 vec
(format "%s %s" result existing
))
3567 (not (tramp-send-command-and-check
3568 vec
(format "%s %s" result nonexistent
))))
3569 (and (setq result
"/usr/bin/test -e")
3570 (tramp-send-command-and-check
3571 vec
(format "%s %s" result existing
))
3572 (not (tramp-send-command-and-check
3573 vec
(format "%s %s" result nonexistent
))))
3574 (and (setq result
(format "%s -d" (tramp-get-ls-command vec
)))
3575 (tramp-send-command-and-check
3576 vec
(format "%s %s" result existing
))
3577 (not (tramp-send-command-and-check
3578 vec
(format "%s %s" result nonexistent
)))))
3580 vec
'file-error
"Couldn't find command to check if file exists"))
3583 (defun tramp-open-shell (vec shell
)
3584 "Opens shell SHELL."
3585 (tramp-with-progress-reporter
3586 vec
5 (format "Opening remote shell `%s'" shell
)
3587 ;; Find arguments for this shell.
3588 (let ((tramp-end-of-output tramp-initial-end-of-output
)
3589 (alist tramp-sh-extra-args
)
3591 (while (and alist
(null extra-args
))
3592 (setq item
(pop alist
))
3593 (when (string-match (car item
) shell
)
3594 (setq extra-args
(cdr item
))))
3595 (when extra-args
(setq shell
(concat shell
" " extra-args
)))
3597 vec
(format "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s"
3598 (tramp-shell-quote-argument tramp-end-of-output
) shell
)
3602 vec
(format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output
)) t
)
3603 (tramp-send-command vec
"PS2=''" t
)
3604 (tramp-send-command vec
"PS3=''" t
)
3605 (tramp-send-command vec
"PROMPT_COMMAND=''" t
)))
3607 (defun tramp-find-shell (vec)
3608 "Opens a shell on the remote host which groks tilde expansion."
3609 (unless (tramp-get-connection-property vec
"remote-shell" nil
)
3611 (with-current-buffer (tramp-get-buffer vec
)
3612 (tramp-send-command vec
"echo ~root" t
)
3614 ((or (string-match "^~root$" (buffer-string))
3615 ;; The default shell (ksh93) of OpenSolaris and Solaris
3616 ;; is buggy. We've got reports for "SunOS 5.10" and
3617 ;; "SunOS 5.11" so far.
3618 (string-match (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3619 (tramp-get-connection-property vec
"uname" "")))
3621 (or (tramp-find-executable
3622 vec
"bash" (tramp-get-remote-path vec
) t t
)
3623 (tramp-find-executable
3624 vec
"ksh" (tramp-get-remote-path vec
) t t
)))
3628 "Couldn't find a shell which groks tilde expansion"))
3630 vec
5 "Starting remote shell `%s' for tilde expansion"
3631 (tramp-set-connection-property vec
"remote-shell" shell
))
3632 (tramp-open-shell vec shell
))
3635 vec
5 "Remote `%s' groks tilde expansion, good"
3636 (tramp-set-connection-property
3638 (tramp-get-method-parameter
3639 (tramp-file-name-method vec
) 'tramp-remote-shell
)))))))))
3641 ;; Utility functions.
3643 (defun tramp-barf-if-no-shell-prompt (proc timeout
&rest error-args
)
3644 "Wait for shell prompt and barf if none appears.
3645 Looks at process PROC to see if a shell prompt appears in TIMEOUT
3646 seconds. If not, it produces an error message with the given ERROR-ARGS."
3648 (tramp-wait-for-regexp
3651 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern
))
3652 (apply 'tramp-error-with-buffer nil proc
'file-error error-args
)))
3654 (defun tramp-open-connection-setup-interactive-shell (proc vec
)
3655 "Set up an interactive shell.
3656 Mainly sets the prompt and the echo correctly. PROC is the shell
3657 process to set up. VEC specifies the connection."
3658 (let ((tramp-end-of-output tramp-initial-end-of-output
))
3659 ;; It is useful to set the prompt in the following command because
3660 ;; some people have a setting for $PS1 which /bin/sh doesn't know
3661 ;; about and thus /bin/sh will display a strange prompt. For
3662 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
3663 ;; display the current working directory but /bin/sh will display
3664 ;; a dollar sign. The following command line sets $PS1 to a sane
3665 ;; value, and works under Bourne-ish shells as well as csh-like
3666 ;; shells. Daniel Pittman reports that the unusual positioning of
3667 ;; the single quotes makes it work under `rc', too. We also unset
3668 ;; the variable $ENV because that is read by some sh
3669 ;; implementations (eg, bash when called as sh) on startup; this
3670 ;; way, we avoid the startup file clobbering $PS1. $PROMPT_COMMAND
3671 ;; is another way to set the prompt in /bin/bash, it must be
3672 ;; discarded as well.
3675 (tramp-get-method-parameter
3676 (tramp-file-name-method vec
) 'tramp-remote-shell
))
3679 (tramp-message vec
5 "Setting up remote shell environment")
3680 (tramp-send-command vec
"stty -inlcr -echo kill '^U' erase '^H'" t
)
3681 ;; Check whether the echo has really been disabled. Some
3682 ;; implementations, like busybox of embedded GNU/Linux, don't
3683 ;; support disabling.
3684 (tramp-send-command vec
"echo foo" t
)
3685 (with-current-buffer (process-buffer proc
)
3686 (goto-char (point-min))
3687 (when (looking-at "echo foo")
3688 (tramp-set-connection-property proc
"remote-echo" t
)
3689 (tramp-message vec
5 "Remote echo still on. Ok.")
3690 ;; Make sure backspaces and their echo are enabled and no line
3691 ;; width magic interferes with them.
3692 (tramp-send-command vec
"stty icanon erase ^H cols 32767" t
))))
3694 (tramp-message vec
5 "Setting shell prompt")
3696 vec
(format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output
)) t
)
3697 (tramp-send-command vec
"PS2=''" t
)
3698 (tramp-send-command vec
"PS3=''" t
)
3699 (tramp-send-command vec
"PROMPT_COMMAND=''" t
)
3701 ;; Try to set up the coding system correctly.
3702 ;; CCC this can't be the right way to do it. Hm.
3703 (tramp-message vec
5 "Determining coding system")
3704 (tramp-send-command vec
"echo foo ; echo bar" t
)
3705 (with-current-buffer (process-buffer proc
)
3706 (goto-char (point-min))
3707 (if (featurep 'mule
)
3708 ;; Use MULE to select the right EOL convention for communicating
3709 ;; with the process.
3710 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc
)
3711 (cons 'undecided
'undecided
)))
3712 cs-decode cs-encode
)
3713 (when (symbolp cs
) (setq cs
(cons cs cs
)))
3714 (setq cs-decode
(car cs
))
3715 (setq cs-encode
(cdr cs
))
3716 (unless cs-decode
(setq cs-decode
'undecided
))
3717 (unless cs-encode
(setq cs-encode
'undecided
))
3718 (setq cs-encode
(tramp-compat-coding-system-change-eol-conversion
3720 (when (search-forward "\r" nil t
)
3721 (setq cs-decode
(tramp-compat-coding-system-change-eol-conversion
3723 (tramp-compat-funcall
3724 'set-buffer-process-coding-system cs-decode cs-encode
)
3726 vec
5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode
))
3727 ;; Look for ^M and do something useful if found.
3728 (when (search-forward "\r" nil t
)
3729 ;; We have found a ^M but cannot frob the process coding system
3730 ;; because we're running on a non-MULE Emacs. Let's try
3732 (tramp-send-command vec
"stty -onlcr" t
))))
3734 (tramp-send-command vec
"set +o vi +o emacs" t
)
3736 ;; Check whether the output of "uname -sr" has been changed. If
3737 ;; yes, this is a strong indication that we must expire all
3738 ;; connection properties. We start again with
3739 ;; `tramp-maybe-open-connection', it will be caught there.
3740 (tramp-message vec
5 "Checking system information")
3741 (let ((old-uname (tramp-get-connection-property vec
"uname" nil
))
3743 (tramp-set-connection-property
3745 (tramp-send-command-and-read vec
"echo \\\"`uname -sr`\\\""))))
3746 (when (and (stringp old-uname
) (not (string-equal old-uname new-uname
)))
3747 (with-current-buffer (tramp-get-debug-buffer vec
)
3748 ;; Keep the debug buffer.
3750 (generate-new-buffer-name tramp-temp-buffer-name
) 'unique
)
3751 (tramp-cleanup-connection vec
)
3752 (if (= (point-min) (point-max))
3754 (rename-buffer (tramp-debug-buffer-name vec
) 'unique
))
3755 ;; We call `tramp-get-buffer' in order to keep the debug buffer.
3756 (tramp-get-buffer vec
)
3759 "Connection reset, because remote host changed from `%s' to `%s'"
3760 old-uname new-uname
)
3761 (throw 'uname-changed
(tramp-maybe-open-connection vec
)))))
3763 ;; Check whether the remote host suffers from buggy
3764 ;; `send-process-string'. This is known for FreeBSD (see comment in
3765 ;; `send_process', file process.c). I've tested sending 624 bytes
3766 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
3767 ;; this host type is detected locally. It cannot handle remote
3769 (with-connection-property proc
"chunksize"
3771 ((and (integerp tramp-chunksize
) (> tramp-chunksize
0))
3775 vec
5 "Checking remote host type for `send-process-string' bug")
3777 "^FreeBSD" (tramp-get-connection-property vec
"uname" ""))
3780 ;; Set remote PATH variable.
3781 (tramp-set-remote-path vec
)
3783 ;; Search for a good shell before searching for a command which
3784 ;; checks if a file exists. This is done because Tramp wants to use
3785 ;; "test foo; echo $?" to check if various conditions hold, and
3786 ;; there are buggy /bin/sh implementations which don't execute the
3787 ;; "echo $?" part if the "test" part has an error. In particular,
3788 ;; the OpenSolaris /bin/sh is a problem. There are also other
3789 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
3790 ;; in function declarations, or changing HISTFILE in place.
3791 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
3793 (tramp-find-shell vec
)
3795 ;; Disable unexpected output.
3796 (tramp-send-command vec
"mesg n; biff n" t
)
3798 ;; Busyboxes tend to behave strange. We check for the existence.
3799 (with-connection-property vec
"busybox"
3803 "%s --version" (tramp-get-connection-property vec
"remote-shell" "echo"))
3805 (with-current-buffer (process-buffer proc
)
3806 (let ((case-fold-search t
))
3807 (and (string-match "busybox" (buffer-string)) t
))))
3809 ;; IRIX64 bash expands "!" even when in single quotes. This
3810 ;; destroys our shell functions, we must disable it. See
3811 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
3812 (when (string-match "^IRIX64" (tramp-get-connection-property vec
"uname" ""))
3813 (tramp-send-command vec
"set +H" t
))
3815 ;; On BSD-like systems, ?\t is expanded to spaces. Suppress this.
3816 (when (string-match "BSD\\|Darwin"
3817 (tramp-get-connection-property vec
"uname" ""))
3818 (tramp-send-command vec
"stty -oxtabs" t
))
3820 ;; Set `remote-tty' process property.
3821 (let ((tty (tramp-send-command-and-read vec
"echo \\\"`tty`\\\"" 'noerror
)))
3822 (unless (zerop (length tty
))
3823 (tramp-compat-process-put proc
'remote-tty tty
)))
3825 ;; Dump stty settings in the traces.
3826 (when (>= tramp-verbose
9)
3827 (tramp-send-command vec
"stty -a" t
))
3829 ;; Set the environment.
3830 (tramp-message vec
5 "Setting default environment")
3832 (let ((env (copy-sequence tramp-remote-process-environment
))
3835 (setq item
(tramp-compat-split-string (car env
) "="))
3836 (setcdr item
(mapconcat 'identity
(cdr item
) "="))
3837 (if (and (stringp (cdr item
)) (not (string-equal (cdr item
) "")))
3839 vec
(format "%s=%s; export %s" (car item
) (cdr item
) (car item
)) t
)
3840 (push (car item
) unset
))
3841 (setq env
(cdr env
)))
3844 vec
(format "unset %s" (mapconcat 'identity unset
" ")) t
))))
3846 ;; CCC: We should either implement a Perl version of base64 encoding
3847 ;; and decoding. Then we just use that in the last item. The other
3848 ;; alternative is to use the Perl version of UU encoding. But then
3849 ;; we need a Lisp version of uuencode.
3851 ;; Old text from documentation of tramp-methods:
3852 ;; Using a uuencode/uudecode inline method is discouraged, please use one
3853 ;; of the base64 methods instead since base64 encoding is much more
3854 ;; reliable and the commands are more standardized between the different
3855 ;; Unix versions. But if you can't use base64 for some reason, please
3856 ;; note that the default uudecode command does not work well for some
3857 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
3858 ;; the following command for uudecode:
3860 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
3862 ;; For Irix, no solution is known yet.
3864 (autoload 'uudecode-decode-region
"uudecode")
3866 (defconst tramp-local-coding-commands
3867 '((b64 base64-encode-region base64-decode-region
)
3868 (uu tramp-uuencode-region uudecode-decode-region
)
3870 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
3871 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
3872 "List of local coding commands for inline transfer.
3873 Each item is a list that looks like this:
3875 \(FORMAT ENCODING DECODING\)
3877 FORMAT is symbol describing the encoding/decoding format. It can be
3878 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3880 ENCODING and DECODING can be strings, giving commands, or symbols,
3881 giving functions. If they are strings, then they can contain
3882 the \"%s\" format specifier. If that specifier is present, the input
3883 filename will be put into the command line at that spot. If the
3884 specifier is not present, the input should be read from standard
3887 If they are functions, they will be called with two arguments, start
3888 and end of region, and are expected to replace the region contents
3889 with the encoded or decoded results, respectively.")
3891 (defconst tramp-remote-coding-commands
3892 '((b64 "base64" "base64 -d -i")
3893 ;; "-i" is more robust with older base64 from GNU coreutils.
3894 ;; However, I don't know whether all base64 versions do supports
3896 (b64 "base64" "base64 -d")
3897 (b64 "mimencode -b" "mimencode -u -b")
3898 (b64 "mmencode -b" "mmencode -u -b")
3899 (b64 "recode data..base64" "recode base64..data")
3900 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module
)
3901 (b64 tramp-perl-encode tramp-perl-decode
)
3902 (uu "uuencode xxx" "uudecode -o /dev/stdout")
3903 (uu "uuencode xxx" "uudecode -o -")
3904 (uu "uuencode xxx" "uudecode -p")
3905 (uu "uuencode xxx" tramp-uudecode
)
3907 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
3908 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
3909 "List of remote coding commands for inline transfer.
3910 Each item is a list that looks like this:
3912 \(FORMAT ENCODING DECODING\)
3914 FORMAT is symbol describing the encoding/decoding format. It can be
3915 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3917 ENCODING and DECODING can be strings, giving commands, or symbols,
3918 giving variables. If they are strings, then they can contain
3919 the \"%s\" format specifier. If that specifier is present, the input
3920 filename will be put into the command line at that spot. If the
3921 specifier is not present, the input should be read from standard
3924 If they are variables, this variable is a string containing a Perl
3925 implementation for this functionality. This Perl program will be transferred
3926 to the remote host, and it is available as shell function with the same name.")
3928 (defun tramp-find-inline-encoding (vec)
3929 "Find an inline transfer encoding that works.
3930 Goes through the list `tramp-local-coding-commands' and
3931 `tramp-remote-coding-commands'."
3933 (let ((local-commands tramp-local-coding-commands
)
3935 loc-enc loc-dec rem-enc rem-dec litem ritem found
)
3936 (while (and local-commands
(not found
))
3937 (setq litem
(pop local-commands
))
3938 (catch 'wont-work-local
3939 (let ((format (nth 0 litem
))
3940 (remote-commands tramp-remote-coding-commands
))
3941 (setq loc-enc
(nth 1 litem
))
3942 (setq loc-dec
(nth 2 litem
))
3943 ;; If the local encoder or decoder is a string, the
3944 ;; corresponding command has to work locally.
3945 (if (not (stringp loc-enc
))
3947 vec
5 "Checking local encoding function `%s'" loc-enc
)
3949 vec
5 "Checking local encoding command `%s' for sanity" loc-enc
)
3950 (unless (zerop (tramp-call-local-coding-command
3952 (throw 'wont-work-local nil
)))
3953 (if (not (stringp loc-dec
))
3955 vec
5 "Checking local decoding function `%s'" loc-dec
)
3957 vec
5 "Checking local decoding command `%s' for sanity" loc-dec
)
3958 (unless (zerop (tramp-call-local-coding-command
3960 (throw 'wont-work-local nil
)))
3961 ;; Search for remote coding commands with the same format
3962 (while (and remote-commands
(not found
))
3963 (setq ritem
(pop remote-commands
))
3964 (catch 'wont-work-remote
3965 (when (equal format
(nth 0 ritem
))
3966 (setq rem-enc
(nth 1 ritem
))
3967 (setq rem-dec
(nth 2 ritem
))
3968 ;; Check if remote encoding and decoding commands can be
3969 ;; called remotely with null input and output. This makes
3970 ;; sure there are no syntax errors and the command is really
3971 ;; found. Note that we do not redirect stdout to /dev/null,
3972 ;; for two reasons: when checking the decoding command, we
3973 ;; actually check the output it gives. And also, when
3974 ;; redirecting "mimencode" output to /dev/null, then as root
3975 ;; it might change the permissions of /dev/null!
3976 (when (not (stringp rem-enc
))
3977 (let ((name (symbol-name rem-enc
)))
3978 (while (string-match (regexp-quote "-") name
)
3979 (setq name
(replace-match "_" nil t name
)))
3980 (tramp-maybe-send-script vec
(symbol-value rem-enc
) name
)
3981 (setq rem-enc name
)))
3984 "Checking remote encoding command `%s' for sanity" rem-enc
)
3985 (unless (tramp-send-command-and-check
3986 vec
(format "%s </dev/null" rem-enc
) t
)
3987 (throw 'wont-work-remote nil
))
3989 (when (not (stringp rem-dec
))
3990 (let ((name (symbol-name rem-dec
)))
3991 (while (string-match (regexp-quote "-") name
)
3992 (setq name
(replace-match "_" nil t name
)))
3993 (tramp-maybe-send-script vec
(symbol-value rem-dec
) name
)
3994 (setq rem-dec name
)))
3997 "Checking remote decoding command `%s' for sanity" rem-dec
)
3998 (unless (tramp-send-command-and-check
4000 (format "echo %s | %s | %s" magic rem-enc rem-dec
)
4002 (throw 'wont-work-remote nil
))
4004 (with-current-buffer (tramp-get-buffer vec
)
4005 (goto-char (point-min))
4006 (unless (looking-at (regexp-quote magic
))
4007 (throw 'wont-work-remote nil
)))
4009 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
4010 (setq rem-enc
(nth 1 ritem
))
4011 (setq rem-dec
(nth 2 ritem
))
4012 (setq found t
)))))))
4014 ;; Did we find something?
4017 vec
'file-error
"Couldn't find an inline transfer encoding"))
4019 ;; Set connection properties.
4020 (tramp-message vec
5 "Using local encoding `%s'" loc-enc
)
4021 (tramp-set-connection-property vec
"local-encoding" loc-enc
)
4022 (tramp-message vec
5 "Using local decoding `%s'" loc-dec
)
4023 (tramp-set-connection-property vec
"local-decoding" loc-dec
)
4024 (tramp-message vec
5 "Using remote encoding `%s'" rem-enc
)
4025 (tramp-set-connection-property vec
"remote-encoding" rem-enc
)
4026 (tramp-message vec
5 "Using remote decoding `%s'" rem-dec
)
4027 (tramp-set-connection-property vec
"remote-decoding" rem-dec
))))
4029 (defun tramp-call-local-coding-command (cmd input output
)
4030 "Call the local encoding or decoding command.
4031 If CMD contains \"%s\", provide input file INPUT there in command.
4032 Otherwise, INPUT is passed via standard input.
4033 INPUT can also be nil which means `/dev/null'.
4034 OUTPUT can be a string (which specifies a filename), or t (which
4035 means standard output and thus the current buffer), or nil (which
4037 (tramp-compat-call-process
4038 tramp-encoding-shell
4039 (when (and input
(not (string-match "%s" cmd
))) input
)
4040 (if (eq output t
) t nil
)
4042 tramp-encoding-command-switch
4044 (if (string-match "%s" cmd
) (format cmd input
) cmd
)
4045 (if (stringp output
) (concat "> " output
) ""))))
4047 (defconst tramp-inline-compress-commands
4048 '(("gzip" "gzip -d")
4049 ("bzip2" "bzip2 -d")
4051 ("compress" "compress -d"))
4052 "List of compress and decompress commands for inline transfer.
4053 Each item is a list that looks like this:
4055 \(COMPRESS DECOMPRESS\)
4057 COMPRESS or DECOMPRESS are strings with the respective commands.")
4059 (defun tramp-find-inline-compress (vec)
4060 "Find an inline transfer compress command that works.
4061 Goes through the list `tramp-inline-compress-commands'."
4063 (let ((commands tramp-inline-compress-commands
)
4065 item compress decompress
4067 (while (and commands
(not found
))
4069 (setq item
(pop commands
)
4070 compress
(nth 0 item
)
4071 decompress
(nth 1 item
))
4074 "Checking local compress command `%s', `%s' for sanity"
4075 compress decompress
)
4078 (tramp-call-local-coding-command
4080 ;; Windows shells need the program file name after
4081 ;; the pipe symbol be quoted if they use forward
4082 ;; slashes as directory separators.
4083 (if (memq system-type
'(windows-nt))
4084 "echo %s | \"%s\" | \"%s\""
4085 "echo %s | %s | %s")
4086 magic compress decompress
) nil nil
))
4090 "Checking remote compress command `%s', `%s' for sanity"
4091 compress decompress
)
4092 (unless (tramp-send-command-and-check
4093 vec
(format "echo %s | %s | %s" magic compress decompress
) t
)
4097 ;; Did we find something?
4100 ;; Set connection properties.
4102 vec
5 "Using inline transfer compress command `%s'" compress
)
4103 (tramp-set-connection-property vec
"inline-compress" compress
)
4105 vec
5 "Using inline transfer decompress command `%s'" decompress
)
4106 (tramp-set-connection-property vec
"inline-decompress" decompress
))
4108 (tramp-set-connection-property vec
"inline-compress" nil
)
4109 (tramp-set-connection-property vec
"inline-decompress" nil
)
4111 vec
2 "Couldn't find an inline transfer compress command")))))
4113 (defun tramp-compute-multi-hops (vec)
4114 "Expands VEC according to `tramp-default-proxies-alist'.
4115 Gateway hops are already opened."
4116 (let ((target-alist `(,vec
))
4117 (choices tramp-default-proxies-alist
)
4120 ;; Look for proxy hosts to be passed.
4122 (setq item
(pop choices
)
4123 proxy
(eval (nth 2 item
)))
4126 (string-match (or (eval (nth 0 item
)) "")
4127 (or (tramp-file-name-host (car target-alist
)) ""))
4129 (string-match (or (eval (nth 1 item
)) "")
4130 (or (tramp-file-name-user (car target-alist
)) "")))
4132 ;; No more hops needed.
4134 ;; Replace placeholders.
4139 ?u
(or (tramp-file-name-user (car target-alist
)) "")
4140 ?h
(or (tramp-file-name-host (car target-alist
)) ""))))
4141 (with-parsed-tramp-file-name proxy l
4143 (add-to-list 'target-alist l
)
4144 ;; Start next search.
4145 (setq choices tramp-default-proxies-alist
)))))
4150 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method
)
4151 (tramp-file-name-method (car target-alist
)))
4152 (let ((gw (pop target-alist
))
4153 (hop (pop target-alist
)))
4154 ;; Is the method prepared for gateways?
4155 (unless (tramp-file-name-port hop
)
4158 "Connection `%s' is not supported for gateway access." hop
))
4159 ;; Open the gateway connection.
4163 (tramp-file-name-method hop
) (tramp-file-name-user hop
)
4164 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop
) nil
))
4165 ;; For the password prompt, we need the correct values.
4166 ;; Therefore, we must remember the gateway vector. But we
4167 ;; cannot do it as connection property, because it shouldn't
4168 ;; be persistent. And we have no started process yet either.
4169 (tramp-set-file-property (car target-alist
) "" "gateway" hop
)))
4171 ;; Foreign and out-of-band methods are not supported for multi-hops.
4172 (when (cdr target-alist
)
4173 (setq choices target-alist
)
4175 (setq item
(pop choices
))
4179 (tramp-get-method-parameter
4180 (tramp-file-name-method item
) 'tramp-login-program
))
4181 (tramp-get-method-parameter
4182 (tramp-file-name-method item
) 'tramp-copy-program
))
4185 "Method `%s' is not supported for multi-hops."
4186 (tramp-file-name-method item
)))))
4188 ;; In case the host name is not used for the remote shell
4189 ;; command, the user could be misguided by applying a random
4191 (let* ((v (car target-alist
))
4192 (method (tramp-file-name-method v
))
4193 (host (tramp-file-name-host v
)))
4196 ;; There are multi-hops.
4198 ;; The host name is used for the remote shell command.
4200 '("%h") (tramp-get-method-parameter method
'tramp-login-args
))
4201 ;; The host is local. We cannot use `tramp-local-host-p'
4202 ;; here, because it opens a connection as well.
4203 (string-match tramp-local-host-regexp host
))
4206 "Host `%s' looks like a remote host, `%s' can only use the local host"
4212 (defun tramp-maybe-open-connection (vec)
4213 "Maybe open a connection VEC.
4214 Does not do anything if a connection is already open, but re-opens the
4215 connection if a previous connection has died for some reason."
4216 (catch 'uname-changed
4217 (let ((p (tramp-get-connection-process vec
))
4218 (process-name (tramp-get-connection-property vec
"process-name" nil
))
4219 (process-environment (copy-sequence process-environment
))
4220 (pos (with-current-buffer (tramp-get-connection-buffer vec
) (point))))
4222 ;; If too much time has passed since last command was sent, look
4223 ;; whether process is still alive. If it isn't, kill it. When
4224 ;; using ssh, it can sometimes happen that the remote end has
4225 ;; hung up but the local ssh client doesn't recognize this until
4226 ;; it tries to send some data to the remote end. So that's why
4227 ;; we try to send a command from time to time, then look again
4228 ;; whether the process is really alive.
4230 (when (and (> (tramp-time-diff
4232 (tramp-get-connection-property
4233 p
"last-cmd-time" '(0 0 0)))
4235 p
(processp p
) (memq (process-status p
) '(run open
)))
4236 (tramp-send-command vec
"echo are you awake" t t
)
4237 (unless (and (memq (process-status p
) '(run open
))
4238 (tramp-wait-for-output p
10))
4239 ;; The error will be caught locally.
4240 (tramp-error vec
'file-error
"Awake did fail")))
4242 (tramp-flush-connection-property vec
)
4243 (tramp-flush-connection-property p
)
4247 ;; New connection must be opened.
4249 (unless (and p
(processp p
) (memq (process-status p
) '(run open
)))
4251 ;; We call `tramp-get-buffer' in order to get a debug
4252 ;; buffer for messages from the beginning.
4253 (tramp-get-buffer vec
)
4254 (tramp-with-progress-reporter
4256 (if (zerop (length (tramp-file-name-user vec
)))
4257 (format "Opening connection for %s using %s"
4258 (tramp-file-name-host vec
)
4259 (tramp-file-name-method vec
))
4260 (format "Opening connection for %s@%s using %s"
4261 (tramp-file-name-user vec
)
4262 (tramp-file-name-host vec
)
4263 (tramp-file-name-method vec
)))
4265 ;; Start new process.
4266 (when (and p
(processp p
))
4268 (setenv "TERM" tramp-terminal-type
)
4269 (setenv "LC_ALL" "C")
4270 (setenv "PROMPT_COMMAND")
4271 (setenv "PS1" tramp-initial-end-of-output
)
4272 (let* ((target-alist (tramp-compute-multi-hops vec
))
4273 (process-connection-type tramp-process-connection-type
)
4274 (process-adaptive-read-buffering nil
)
4275 (coding-system-for-read nil
)
4276 ;; This must be done in order to avoid our file
4278 (p (let ((default-directory
4279 (tramp-compat-temporary-file-directory)))
4282 (tramp-get-connection-name vec
)
4283 (tramp-get-connection-buffer vec
)
4284 (if tramp-encoding-command-interactive
4285 (list tramp-encoding-shell
4286 tramp-encoding-command-interactive
)
4287 (list tramp-encoding-shell
))))))
4289 ;; Set sentinel and query flag.
4290 (tramp-set-connection-property p
"vector" vec
)
4291 (set-process-sentinel p
'tramp-process-sentinel
)
4292 (tramp-compat-set-process-query-on-exit-flag p nil
)
4295 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
4297 ;; Check whether process is alive.
4298 (tramp-barf-if-no-shell-prompt
4300 "Couldn't find local shell prompt %s" tramp-encoding-shell
)
4302 ;; Now do all the connections as specified.
4304 (let* ((hop (car target-alist
))
4305 (l-method (tramp-file-name-method hop
))
4306 (l-user (tramp-file-name-user hop
))
4307 (l-host (tramp-file-name-host hop
))
4310 (tramp-get-method-parameter
4311 l-method
'tramp-login-program
))
4313 (tramp-get-method-parameter
4314 l-method
'tramp-login-args
))
4316 (tramp-get-method-parameter
4317 l-method
'tramp-async-args
))
4319 (tramp-get-method-parameter l-method
'tramp-gw-args
))
4320 (gw (tramp-get-file-property hop
"" "gateway" nil
))
4321 (g-method (and gw
(tramp-file-name-method gw
)))
4322 (g-user (and gw
(tramp-file-name-user gw
)))
4323 (g-host (and gw
(tramp-file-name-real-host gw
)))
4324 (command login-program
)
4325 ;; We don't create the temporary file. In
4326 ;; fact, it is just a prefix for the
4327 ;; ControlPath option of ssh; the real
4328 ;; temporary file has another name, and it is
4329 ;; created and protected by ssh. It is also
4330 ;; removed by ssh when the connection is
4333 (tramp-set-connection-property
4337 tramp-temp-name-prefix
4338 (tramp-compat-temporary-file-directory)))))
4341 ;; Add arguments for asynchronous processes.
4342 (when (and process-name async-args
)
4343 (setq login-args
(append async-args login-args
)))
4345 ;; Add gateway arguments if necessary.
4346 (when (and gw gw-args
)
4347 (setq login-args
(append gw-args login-args
)))
4349 ;; Check for port number. Until now, there's no
4350 ;; need for handling like method, user, host.
4351 (when (string-match tramp-host-with-port-regexp l-host
)
4352 (setq l-port
(match-string 2 l-host
)
4353 l-host
(match-string 1 l-host
)))
4355 ;; Set variables for computing the prompt for
4356 ;; reading password. They can also be derived
4358 (setq tramp-current-method
(or g-method l-method
)
4359 tramp-current-user
(or g-user l-user
)
4360 tramp-current-host
(or g-host l-host
))
4362 ;; Replace login-args place holders.
4364 l-host
(or l-host
"")
4365 l-user
(or l-user
"")
4366 l-port
(or l-port
"")
4367 spec
(format-spec-make
4368 ?h l-host ?u l-user ?p l-port ?t tmpfile
)
4371 ;; We do not want to see the trailing local
4372 ;; prompt in `start-file-process'.
4373 (unless (memq system-type
'(windows-nt)) "exec ")
4377 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
4378 (unless (member "" x
) (mapconcat 'identity x
" ")))
4380 ;; Local shell could be a Windows COMSPEC. It
4381 ;; doesn't know the ";" syntax, but we must exit
4382 ;; always for `start-file-process'. "exec" does
4384 (if (memq system-type
'(windows-nt)) " && exit || exit")))
4386 ;; Send the command.
4387 (tramp-message vec
3 "Sending command `%s'" command
)
4388 (tramp-send-command vec command t t
)
4389 (tramp-process-actions
4390 p vec pos tramp-actions-before-shell
60)
4392 vec
3 "Found remote shell prompt on `%s'" l-host
))
4394 (setq target-alist
(cdr target-alist
)))
4396 ;; Make initial shell settings.
4397 (tramp-open-connection-setup-interactive-shell p vec
))))
4399 ;; When the user did interrupt, we must cleanup.
4401 (let ((p (tramp-get-connection-process vec
)))
4402 (when (and p
(processp p
))
4403 (tramp-flush-connection-property vec
)
4404 (tramp-flush-connection-property p
)
4405 (delete-process p
)))
4406 ;; Propagate the quit signal.
4407 (signal (car err
) (cdr err
)))))))
4409 (defun tramp-send-command (vec command
&optional neveropen nooutput
)
4410 "Send the COMMAND to connection VEC.
4411 Erases temporary buffer before sending the command. If optional
4412 arg NEVEROPEN is non-nil, never try to open the connection. This
4413 is meant to be used from `tramp-maybe-open-connection' only. The
4414 function waits for output unless NOOUTPUT is set."
4415 (unless neveropen
(tramp-maybe-open-connection vec
))
4416 (let ((p (tramp-get-connection-process vec
)))
4417 (when (tramp-get-connection-property p
"remote-echo" nil
)
4418 ;; We mark the command string that it can be erased in the output buffer.
4419 (tramp-set-connection-property p
"check-remote-echo" t
)
4420 (setq command
(format "%s%s%s" tramp-echo-mark command tramp-echo-mark
)))
4421 (when (and (string-match "<<'EOF'" command
)
4422 (not (tramp-get-connection-property vec
"busybox" nil
)))
4423 ;; Unset $PS1 when using here documents, in order to avoid
4424 ;; multiple prompts.
4425 (setq command
(concat "(PS1= ; " command
"\n)")))
4426 ;; Send the command.
4427 (tramp-message vec
6 "%s" command
)
4428 (tramp-send-string vec command
)
4429 (unless nooutput
(tramp-wait-for-output p
))))
4431 (defun tramp-wait-for-output (proc &optional timeout
)
4432 "Wait for output from remote command."
4433 (unless (buffer-live-p (process-buffer proc
))
4434 (delete-process proc
)
4435 (tramp-error proc
'file-error
"Process `%s' not available, try again" proc
))
4436 (with-current-buffer (process-buffer proc
)
4437 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
4438 ;; be leading escape sequences, which must be ignored.
4439 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output
)))
4440 ;; Sometimes, the commands do not return a newline but a
4441 ;; null byte before the shell prompt, for example "git
4442 ;; ls-files -c -z ...".
4443 (regexp1 (format "\\(^\\|\000\\)%s" regexp
))
4444 (found (tramp-wait-for-regexp proc timeout regexp1
)))
4446 (let (buffer-read-only)
4447 ;; A simple-minded busybox has sent " ^H" sequences.
4449 (goto-char (point-min))
4450 (when (re-search-forward "^\\(.\b\\)+$" (point-at-eol) t
)
4452 (delete-region (point-min) (point)))
4453 ;; Delete the prompt.
4454 (goto-char (point-max))
4455 (re-search-backward regexp nil t
)
4456 (delete-region (point) (point-max)))
4460 "[[Remote prompt `%s' not found in %d secs]]"
4461 tramp-end-of-output timeout
)
4464 "[[Remote prompt `%s' not found]]" tramp-end-of-output
)))
4465 ;; Return value is whether end-of-output sentinel was found.
4468 (defun tramp-send-command-and-check
4469 (vec command
&optional subshell dont-suppress-err
)
4470 "Run COMMAND and check its exit status.
4471 Sends `echo $?' along with the COMMAND for checking the exit status. If
4472 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
4474 If the optional argument SUBSHELL is non-nil, the command is
4475 executed in a subshell, ie surrounded by parentheses. If
4476 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
4479 (concat (if subshell
"( " "")
4481 (if command
(if dont-suppress-err
"; " " 2>/dev/null; ") "")
4482 "echo tramp_exit_status $?"
4483 (if subshell
" )" "")))
4484 (with-current-buffer (tramp-get-connection-buffer vec
)
4485 (goto-char (point-max))
4486 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
4488 vec
'file-error
"Couldn't find exit status of `%s'" command
))
4489 (skip-chars-forward "^ ")
4491 (zerop (read (current-buffer)))
4492 (let (buffer-read-only)
4493 (delete-region (match-beginning 0) (point-max))))))
4495 (defun tramp-barf-unless-okay (vec command fmt
&rest args
)
4496 "Run COMMAND, check exit status, throw error if exit status not okay.
4497 Similar to `tramp-send-command-and-check' but accepts two more arguments
4498 FMT and ARGS which are passed to `error'."
4499 (or (tramp-send-command-and-check vec command
)
4500 (apply 'tramp-error vec
'file-error fmt args
)))
4502 (defun tramp-send-command-and-read (vec command
&optional noerror
)
4503 "Run COMMAND and return the output, which must be a Lisp expression.
4504 In case there is no valid Lisp expression and NOERROR is nil, it
4507 (tramp-send-command-and-check vec command
)
4508 (tramp-barf-unless-okay
4509 vec command
"`%s' returns with error" command
))
4510 (with-current-buffer (tramp-get-connection-buffer vec
)
4511 ;; Read the expression.
4512 (goto-char (point-min))
4514 (prog1 (read (current-buffer))
4516 (when (re-search-forward "\\S-" (point-at-eol) t
)
4518 (error (unless noerror
4521 "`%s' does not return a valid Lisp expression: `%s'"
4522 command
(buffer-string))))))))
4524 (defun tramp-convert-file-attributes (vec attr
)
4525 "Convert file-attributes ATTR generated by perl script, stat or ls.
4526 Convert file mode bits to string and set virtual device number.
4529 ;; Remove color escape sequences from symlink.
4530 (when (stringp (car attr
))
4531 (while (string-match tramp-color-escape-sequence-regexp
(car attr
))
4532 (setcar attr
(replace-match "" nil nil
(car attr
)))))
4533 ;; Convert last access time.
4534 (unless (listp (nth 4 attr
))
4535 (setcar (nthcdr 4 attr
)
4536 (list (floor (nth 4 attr
) 65536)
4537 (floor (mod (nth 4 attr
) 65536)))))
4538 ;; Convert last modification time.
4539 (unless (listp (nth 5 attr
))
4540 (setcar (nthcdr 5 attr
)
4541 (list (floor (nth 5 attr
) 65536)
4542 (floor (mod (nth 5 attr
) 65536)))))
4543 ;; Convert last status change time.
4544 (unless (listp (nth 6 attr
))
4545 (setcar (nthcdr 6 attr
)
4546 (list (floor (nth 6 attr
) 65536)
4547 (floor (mod (nth 6 attr
) 65536)))))
4548 ;; Convert file size.
4549 (when (< (nth 7 attr
) 0)
4550 (setcar (nthcdr 7 attr
) -
1))
4551 (when (and (floatp (nth 7 attr
))
4552 (<= (nth 7 attr
) (tramp-compat-most-positive-fixnum)))
4553 (setcar (nthcdr 7 attr
) (round (nth 7 attr
))))
4554 ;; Convert file mode bits to string.
4555 (unless (stringp (nth 8 attr
))
4556 (setcar (nthcdr 8 attr
) (tramp-file-mode-from-int (nth 8 attr
)))
4557 (when (stringp (car attr
))
4558 (aset (nth 8 attr
) 0 ?l
)))
4559 ;; Convert directory indication bit.
4560 (when (string-match "^d" (nth 8 attr
))
4562 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
4563 (when (consp (car attr
))
4564 (if (and (stringp (caar attr
))
4565 (string-match ".+ -> .\\(.+\\)." (caar attr
)))
4566 (setcar attr
(match-string 1 (caar attr
)))
4568 ;; Set file's gid change bit.
4569 (setcar (nthcdr 9 attr
)
4570 (if (numberp (nth 3 attr
))
4571 (not (= (nth 3 attr
)
4572 (tramp-get-remote-gid vec
'integer
)))
4575 (tramp-get-remote-gid vec
'string
)))))
4577 (unless (listp (nth 10 attr
))
4578 (setcar (nthcdr 10 attr
)
4580 (cons (floor (nth 10 attr
) 65536)
4581 (floor (mod (nth 10 attr
) 65536)))
4582 ;; Inodes can be incredible huge. We must hide this.
4583 (error (tramp-get-inode vec
)))))
4584 ;; Set virtual device number.
4585 (setcar (nthcdr 11 attr
)
4586 (tramp-get-device vec
))
4589 (defun tramp-check-cached-permissions (vec access
)
4590 "Check `file-attributes' caches for VEC.
4591 Return t if according to the cache access type ACCESS is known to
4597 ((eq ?x access
) 3))))
4598 (dolist (suffix '("string" "integer") result
)
4604 (tramp-get-file-property
4605 vec
(tramp-file-name-localname vec
)
4606 (concat "file-attributes-" suffix
) nil
))
4608 (tramp-get-connection-property
4609 vec
(concat "uid-" suffix
) nil
))
4611 (tramp-get-connection-property
4612 vec
(concat "gid-" suffix
) nil
)))
4617 (eq t
(car file-attr
))
4618 (null (car file-attr
)))
4620 ;; World accessible.
4621 (eq access
(aref (nth 8 file-attr
) (+ offset
6)))
4622 ;; User accessible and owned by user.
4624 (eq access
(aref (nth 8 file-attr
) offset
))
4625 (equal remote-uid
(nth 2 file-attr
)))
4626 ;; Group accessible and owned by user's
4629 (eq access
(aref (nth 8 file-attr
) (+ offset
3)))
4630 (equal remote-gid
(nth 3 file-attr
)))))))))))
4632 (defun tramp-file-mode-from-int (mode)
4633 "Turn an integer representing a file mode into an ls(1)-like string."
4635 (assoc (logand (lsh mode -
12) 15) tramp-file-mode-type-map
)))
4636 (user (logand (lsh mode -
6) 7))
4637 (group (logand (lsh mode -
3) 7))
4638 (other (logand (lsh mode -
0) 7))
4639 (suid (> (logand (lsh mode -
9) 4) 0))
4640 (sgid (> (logand (lsh mode -
9) 2) 0))
4641 (sticky (> (logand (lsh mode -
9) 1) 0)))
4642 (setq user
(tramp-file-mode-permissions user suid
"s"))
4643 (setq group
(tramp-file-mode-permissions group sgid
"s"))
4644 (setq other
(tramp-file-mode-permissions other sticky
"t"))
4645 (concat type user group other
)))
4647 (defun tramp-file-mode-permissions (perm suid suid-text
)
4648 "Convert a permission bitset into a string.
4649 This is used internally by `tramp-file-mode-from-int'."
4650 (let ((r (> (logand perm
4) 0))
4651 (w (> (logand perm
2) 0))
4652 (x (> (logand perm
1) 0)))
4653 (concat (or (and r
"r") "-")
4654 (or (and w
"w") "-")
4655 (or (and suid x suid-text
) ; suid, execute
4656 (and suid
(upcase suid-text
)) ; suid, !execute
4657 (and x
"x") "-")))) ; !suid
4659 (defun tramp-shell-case-fold (string)
4660 "Converts STRING to shell glob pattern which ignores case."
4663 (if (equal (downcase c
) (upcase c
))
4665 (format "[%c%c]" (downcase c
) (upcase c
))))
4669 (defun tramp-make-copy-program-file-name (vec)
4670 "Create a file name suitable to be passed to `rcp' and workalikes."
4671 (let ((user (tramp-file-name-user vec
))
4672 (host (tramp-file-name-real-host vec
))
4673 (localname (tramp-shell-quote-argument
4674 (tramp-file-name-localname vec
))))
4675 (if (not (zerop (length user
)))
4676 (format "%s@%s:%s" user host localname
)
4677 (format "%s:%s" host localname
))))
4679 (defun tramp-method-out-of-band-p (vec size
)
4680 "Return t if this is an out-of-band method, nil otherwise."
4682 ;; It shall be an out-of-band method.
4683 (tramp-get-method-parameter (tramp-file-name-method vec
) 'tramp-copy-program
)
4684 ;; There must be a size, otherwise the file doesn't exist.
4686 ;; Either the file size is large enough, or (in rare cases) there
4687 ;; does not exist a remote encoding.
4688 (or (null tramp-copy-size-limit
)
4689 (> size tramp-copy-size-limit
)
4690 (null (tramp-get-inline-coding vec
"remote-encoding" size
)))))
4692 ;; Variables local to connection.
4694 (defun tramp-get-remote-path (vec)
4695 (with-connection-property
4696 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
4697 ;; cache the result for the session only. Otherwise, the result
4698 ;; is cached persistently.
4699 (if (memq 'tramp-own-remote-path tramp-remote-path
)
4700 (tramp-get-connection-process vec
)
4703 (let* ((remote-path (copy-tree tramp-remote-path
))
4704 (elt1 (memq 'tramp-default-remote-path remote-path
))
4705 (elt2 (memq 'tramp-own-remote-path remote-path
))
4706 (default-remote-path
4709 (tramp-send-command-and-read
4710 vec
"echo \\\"`getconf PATH 2>/dev/null`\\\"" 'noerror
)
4711 ;; Default if "getconf" is not available.
4715 "`getconf PATH' not successful, using default value \"%s\"."
4721 (tramp-send-command-and-read vec
"echo \\\"$PATH\\\"")
4724 vec
3 "$PATH not set, ignoring `tramp-own-remote-path'.")
4727 ;; Replace place holder `tramp-default-remote-path'.
4731 (tramp-compat-split-string default-remote-path
":")
4733 (setq remote-path
(delq 'tramp-default-remote-path remote-path
)))
4735 ;; Replace place holder `tramp-own-remote-path'.
4739 (tramp-compat-split-string own-remote-path
":")
4741 (setq remote-path
(delq 'tramp-own-remote-path remote-path
)))
4743 ;; Remove double entries.
4744 (setq elt1 remote-path
)
4746 (while (and (car elt1
) (setq elt2
(member (car elt1
) (cdr elt1
))))
4748 (setq elt1
(cdr elt1
)))
4750 ;; Remove non-existing directories.
4758 (tramp-make-tramp-file-name
4759 (tramp-file-name-method vec
)
4760 (tramp-file-name-user vec
)
4761 (tramp-file-name-host vec
)
4766 (defun tramp-get-ls-command (vec)
4767 (with-connection-property vec
"ls"
4768 (tramp-message vec
5 "Finding a suitable `ls' command")
4771 (dolist (cmd '("ls" "gnuls" "gls"))
4772 (let ((dl (tramp-get-remote-path vec
))
4774 (while (and dl
(setq result
(tramp-find-executable vec cmd dl t t
)))
4775 ;; Check parameters. On busybox, "ls" output coloring is
4776 ;; enabled by default sometimes. So we try to disable it
4777 ;; when possible. $LS_COLORING is not supported there.
4778 ;; Some "ls" versions are sensible wrt the order of
4779 ;; arguments, they fail when "-al" is after the
4780 ;; "--color=never" argument (for example on FreeBSD).
4781 (when (tramp-send-command-and-check
4782 vec
(format "%s -lnd /" result
))
4783 (when (tramp-send-command-and-check
4785 "%s --color=never -al /dev/null" result
))
4786 (setq result
(concat result
" --color=never")))
4787 (throw 'ls-found result
))
4788 (setq dl
(cdr dl
))))))
4789 (tramp-error vec
'file-error
"Couldn't find a proper `ls' command"))))
4791 (defun tramp-get-ls-command-with-dired (vec)
4793 (with-connection-property vec
"ls-dired"
4794 (tramp-message vec
5 "Checking, whether `ls --dired' works")
4795 ;; Some "ls" versions are sensible wrt the order of arguments,
4796 ;; they fail when "-al" is after the "--dired" argument (for
4797 ;; example on FreeBSD).
4798 (tramp-send-command-and-check
4799 vec
(format "%s --dired -al /dev/null" (tramp-get-ls-command vec
))))))
4801 (defun tramp-get-test-command (vec)
4802 (with-connection-property vec
"test"
4803 (tramp-message vec
5 "Finding a suitable `test' command")
4804 (if (tramp-send-command-and-check vec
"test 0")
4806 (tramp-find-executable vec
"test" (tramp-get-remote-path vec
)))))
4808 (defun tramp-get-test-nt-command (vec)
4809 ;; Does `test A -nt B' work? Use abominable `find' construct if it
4810 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
4811 ;; for otherwise the shell crashes.
4812 (with-connection-property vec
"test-nt"
4816 vec
(format "( %s / -nt / )" (tramp-get-test-command vec
)))
4817 (with-current-buffer (tramp-get-buffer vec
)
4818 (goto-char (point-min))
4819 (when (looking-at (regexp-quote tramp-end-of-output
))
4820 (format "%s %%s -nt %%s" (tramp-get-test-command vec
)))))
4825 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
4826 (tramp-get-test-command vec
)))
4827 "tramp_test_nt %s %s"))))
4829 (defun tramp-get-file-exists-command (vec)
4830 (with-connection-property vec
"file-exists"
4831 (tramp-message vec
5 "Finding command to check if file exists")
4832 (tramp-find-file-exists-command vec
)))
4834 (defun tramp-get-remote-ln (vec)
4835 (with-connection-property vec
"ln"
4836 (tramp-message vec
5 "Finding a suitable `ln' command")
4837 (tramp-find-executable vec
"ln" (tramp-get-remote-path vec
))))
4839 (defun tramp-get-remote-perl (vec)
4840 (with-connection-property vec
"perl"
4841 (tramp-message vec
5 "Finding a suitable `perl' command")
4843 (or (tramp-find-executable vec
"perl5" (tramp-get-remote-path vec
))
4844 (tramp-find-executable
4845 vec
"perl" (tramp-get-remote-path vec
)))))
4846 ;; We must check also for some Perl modules.
4848 (with-connection-property vec
"perl-file-spec"
4849 (tramp-send-command-and-check
4850 vec
(format "%s -e 'use File::Spec;'" result
)))
4851 (with-connection-property vec
"perl-cwd-realpath"
4852 (tramp-send-command-and-check
4853 vec
(format "%s -e 'use Cwd \"realpath\";'" result
))))
4856 (defun tramp-get-remote-stat (vec)
4857 (with-connection-property vec
"stat"
4858 (tramp-message vec
5 "Finding a suitable `stat' command")
4859 (let ((result (tramp-find-executable
4860 vec
"stat" (tramp-get-remote-path vec
)))
4862 ;; Check whether stat(1) returns usable syntax. "%s" does not
4863 ;; work on older AIX systems.
4866 (tramp-send-command-and-read
4867 vec
(format "%s -c '(\"%%N\" %%s)' /" result
) 'noerror
))
4868 (unless (and (listp tmp
) (stringp (car tmp
))
4869 (string-match "^./.$" (car tmp
))
4870 (integerp (cadr tmp
)))
4874 (defun tramp-get-remote-readlink (vec)
4875 (with-connection-property vec
"readlink"
4876 (tramp-message vec
5 "Finding a suitable `readlink' command")
4877 (let ((result (tramp-find-executable
4878 vec
"readlink" (tramp-get-remote-path vec
))))
4880 (tramp-send-command-and-check
4881 vec
(format "%s --canonicalize-missing /" result
)))
4884 (defun tramp-get-remote-trash (vec)
4885 (with-connection-property vec
"trash"
4886 (tramp-message vec
5 "Finding a suitable `trash' command")
4887 (tramp-find-executable vec
"trash" (tramp-get-remote-path vec
))))
4889 (defun tramp-get-remote-id (vec)
4890 (with-connection-property vec
"id"
4891 (tramp-message vec
5 "Finding POSIX `id' command")
4894 (let ((dl (tramp-get-remote-path vec
))
4896 (while (and dl
(setq result
(tramp-find-executable vec
"id" dl t t
)))
4897 ;; Check POSIX parameter.
4898 (when (tramp-send-command-and-check vec
(format "%s -u" result
))
4899 (throw 'id-found result
))
4900 (setq dl
(cdr dl
)))))
4901 (tramp-error vec
'file-error
"Couldn't find a POSIX `id' command"))))
4903 (defun tramp-get-remote-uid (vec id-format
)
4904 (with-connection-property vec
(format "uid-%s" id-format
)
4905 (let ((res (tramp-send-command-and-read
4907 (format "%s -u%s %s"
4908 (tramp-get-remote-id vec
)
4909 (if (equal id-format
'integer
) "" "n")
4910 (if (equal id-format
'integer
)
4911 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
4912 ;; The command might not always return a number.
4913 (if (and (equal id-format
'integer
) (not (integerp res
))) -
1 res
))))
4915 (defun tramp-get-remote-gid (vec id-format
)
4916 (with-connection-property vec
(format "gid-%s" id-format
)
4917 (let ((res (tramp-send-command-and-read
4919 (format "%s -g%s %s"
4920 (tramp-get-remote-id vec
)
4921 (if (equal id-format
'integer
) "" "n")
4922 (if (equal id-format
'integer
)
4923 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
4924 ;; The command might not always return a number.
4925 (if (and (equal id-format
'integer
) (not (integerp res
))) -
1 res
))))
4927 (defun tramp-get-local-uid (id-format)
4928 (if (equal id-format
'integer
) (user-uid) (user-login-name)))
4930 (defun tramp-get-local-gid (id-format)
4931 (nth 3 (tramp-compat-file-attributes "~/" id-format
)))
4933 ;; Some predefined connection properties.
4934 (defun tramp-get-inline-compress (vec prop size
)
4935 "Return the compress command related to PROP.
4936 PROP is either `inline-compress' or `inline-decompress'. SIZE is
4937 the length of the file to be compressed.
4939 If no corresponding command is found, nil is returned."
4940 (when (and (integerp tramp-inline-compress-start-size
)
4941 (> size tramp-inline-compress-start-size
))
4942 (with-connection-property vec prop
4943 (tramp-find-inline-compress vec
)
4944 (tramp-get-connection-property vec prop nil
))))
4946 (defun tramp-get-inline-coding (vec prop size
)
4947 "Return the coding command related to PROP.
4948 PROP is either `remote-encoding', `remote-decoding',
4949 `local-encoding' or `local-decoding'.
4951 SIZE is the length of the file to be coded. Depending on SIZE,
4952 compression might be applied.
4954 If no corresponding command is found, nil is returned.
4955 Otherwise, either a string is returned which contains a `%s' mark
4956 to be used for the respective input or output file; or a Lisp
4957 function cell is returned to be applied on a buffer."
4958 ;; We must catch the errors, because we want to return `nil', when
4959 ;; no inline coding is found.
4962 (with-connection-property vec prop
4963 (tramp-find-inline-encoding vec
)
4964 (tramp-get-connection-property vec prop nil
)))
4965 (prop1 (if (string-match "encoding" prop
)
4966 "inline-compress" "inline-decompress"))
4968 ;; The connection property might have been cached. So we must
4969 ;; send the script to the remote side - maybe.
4970 (when (and coding
(symbolp coding
) (string-match "remote" prop
))
4971 (let ((name (symbol-name coding
)))
4972 (while (string-match (regexp-quote "-") name
)
4973 (setq name
(replace-match "_" nil t name
)))
4974 (tramp-maybe-send-script vec
(symbol-value coding
) name
)
4975 (setq coding name
)))
4977 ;; Check for the `compress' command.
4978 (setq compress
(tramp-get-inline-compress vec prop1 size
))
4979 ;; Return the value.
4981 ((and compress
(symbolp coding
))
4982 (if (string-match "decompress" prop1
)
4985 (let ((coding-system-for-write 'binary
)
4986 (coding-system-for-read 'binary
))
4988 'call-process-region
(point-min) (point-max)
4989 (car (split-string ,compress
)) t t nil
4990 (cdr (split-string ,compress
)))))
4992 (let ((coding-system-for-write 'binary
)
4993 (coding-system-for-read 'binary
))
4995 'call-process-region beg end
4996 (car (split-string ,compress
)) t t nil
4997 (cdr (split-string ,compress
))))
4998 (,coding
(point-min) (point-max)))))
5001 ((and compress
(string-match "decoding" prop
))
5003 ;; Windows shells need the program file name after
5004 ;; the pipe symbol be quoted if they use forward
5005 ;; slashes as directory separators.
5006 (if (and (string-match "local" prop
)
5007 (memq system-type
'(windows-nt)))
5008 "(%s | \"%s\" >%%s)"
5013 ;; Windows shells need the program file name after
5014 ;; the pipe symbol be quoted if they use forward
5015 ;; slashes as directory separators.
5016 (if (and (string-match "local" prop
)
5017 (memq system-type
'(windows-nt)))
5018 "(%s <%%s | \"%s\")"
5021 ((string-match "decoding" prop
)
5022 (format "%s >%%s" coding
))
5024 (format "%s <%%s" coding
)))))))
5026 ;;; Integration of eshell.el:
5029 (defvar eshell-path-env
))
5031 ;; eshell.el keeps the path in `eshell-path-env'. We must change it
5032 ;; when `default-directory' points to another host.
5033 (defun tramp-eshell-directory-change ()
5034 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
5035 (setq eshell-path-env
5036 (if (file-remote-p default-directory
)
5037 (with-parsed-tramp-file-name default-directory nil
5040 (tramp-get-remote-path v
)
5044 (eval-after-load "esh-util"
5046 (tramp-eshell-directory-change)
5047 (add-hook 'eshell-directory-change-hook
5048 'tramp-eshell-directory-change
)
5049 (add-hook 'tramp-unload-hook
5051 (remove-hook 'eshell-directory-change-hook
5052 'tramp-eshell-directory-change
)))))
5054 (add-hook 'tramp-unload-hook
5056 (unload-feature 'tramp-sh
'force
)))
5062 ;; * Don't use globbing for directories with many files, as this is
5063 ;; likely to produce long command lines, and some shells choke on
5064 ;; long command lines.
5065 ;; * Make it work for different encodings, and for different file name
5066 ;; encodings, too. (Daniel Pittman)
5067 ;; * Don't search for perl5 and perl. Instead, only search for perl and
5068 ;; then look if it's the right version (with `perl -v').
5069 ;; * When editing a remote CVS controlled file as a different user, VC
5070 ;; gets confused about the file locking status. Try to find out why
5071 ;; the workaround doesn't work.
5072 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5073 ;; until the last but one hop via `start-file-process'. Apply it
5074 ;; also for ftp and smb.
5075 ;; * WIBNI if we had a command "trampclient"? If I was editing in
5076 ;; some shell with root privileges, it would be nice if I could
5078 ;; trampclient filename.c
5079 ;; as an editor, and the _current_ shell would connect to an Emacs
5080 ;; server and would be used in an existing non-privileged Emacs
5081 ;; session for doing the editing in question.
5082 ;; That way, I need not tell Emacs my password again and be afraid
5083 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
5084 ;; once display a just typed password in the context of a keyboard
5085 ;; sequence prompt for a question immediately following in a shell
5086 ;; script run within Emacs -- nasty).
5087 ;; And if I have some ssh session running to a different computer,
5088 ;; having the possibility of passing a local file there to a local
5089 ;; Emacs session (in case I can arrange for a connection back) would
5091 ;; Likely the corresponding Tramp server should not allow the
5092 ;; equivalent of the emacsclient -eval option in order to make this
5093 ;; reasonably unproblematic. And maybe trampclient should have some
5094 ;; way of passing credentials, like by using an SSL socket or
5095 ;; something. (David Kastrup)
5096 ;; * Reconnect directly to a compliant shell without first going
5097 ;; through the user's default shell. (Pete Forman)
5098 ;; * How can I interrupt the remote process with a signal
5099 ;; (interrupt-process seems not to work)? (Markus Triska)
5100 ;; * Avoid the local shell entirely for starting remote processes. If
5101 ;; so, I think even a signal, when delivered directly to the local
5102 ;; SSH instance, would correctly be propagated to the remote process
5103 ;; automatically; possibly SSH would have to be started with
5104 ;; "-t". (Markus Triska)
5105 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5106 ;; isn't on the remote host. (Mark A. Hershberger)
5107 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
5108 ;; * Optimize out-of-band copying when both methods are scp-like (not
5110 ;; * Keep a second connection open for out-of-band methods like scp or
5112 ;; * Try telnet+curl as new method. It might be useful for busybox,
5113 ;; without built-in uuencode/uudecode.
5115 ;;; tramp-sh.el ends here