1 ;;; tramp-sh.el --- Tramp access functions for (s)sh-like connections
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; (copyright statements below in code to be updated with the above notice)
8 ;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
9 ;; Michael Albinus <michael.albinus@gmx.de>
10 ;; Keywords: comm, processes
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software: you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation, either version 3 of the License, or
18 ;; (at your option) any later version.
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
30 (eval-when-compile (require 'cl
)) ; ignore-errors
34 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
35 ;; not sure at all that this is the right way to do it, but let's hope
36 ;; it works for now, and wait for a guru to point out the Right Way to
39 ;; (unless (fboundp 'dired-insert-set-properties)
40 ;; (fset 'dired-insert-set-properties 'ignore)))
41 ;; Gerd suggests this:
42 (eval-when-compile (require 'dired
))
43 ;; Note that dired is required at run-time, too, when it is needed.
44 ;; It is only needed on XEmacs for the function
45 ;; `dired-insert-set-properties'.
47 (defcustom tramp-inline-compress-start-size
4096
48 "*The minimum size of compressing where inline transfer.
49 When inline transfer, compress transfered data of file
50 whose size is this value or above (up to `tramp-copy-size-limit').
51 If it is nil, no compression at all will be applied."
53 :type
'(choice (const nil
) integer
))
55 (defcustom tramp-copy-size-limit
10240
56 "*The maximum file size where inline copying is preferred over an out-of-the-band copy.
57 If it is nil, inline out-of-the-band copy will be used without a check."
59 :type
'(choice (const nil
) integer
))
62 (defcustom tramp-terminal-type
"dumb"
63 "*Value of TERM environment variable for logging in to remote host.
64 Because Tramp wants to parse the output of the remote shell, it is easily
65 confused by ANSI color escape sequences and suchlike. Often, shell init
66 files conditionalize this setup based on the TERM environment variable."
70 ;; ksh on OpenBSD 4.5 requires, that $PS1 contains a `#' character for
71 ;; root users. It uses the `$' character for other users. In order
72 ;; to guarantee a proper prompt, we use "#$" for the prompt.
74 (defvar tramp-end-of-output
77 (md5 (concat (prin1-to-string process-environment
) (current-time-string))))
78 "String used to recognize end of output.
79 The '$' character at the end is quoted; the string cannot be
80 detected as prompt when being sent on echoing hosts, therefore.")
83 (defconst tramp-initial-end-of-output
"#$ "
84 "Prompt when establishing a connection.")
86 ;; Initialize `tramp-methods' with the supported methods.
88 (add-to-list 'tramp-methods
90 (tramp-login-program "rsh")
91 (tramp-login-args (("%h") ("-l" "%u")))
92 (tramp-remote-sh "/bin/sh")
93 (tramp-copy-program "rcp")
94 (tramp-copy-args (("-p" "%k") ("-r")))
95 (tramp-copy-keep-date t
)
96 (tramp-copy-recursive t
)))
98 (add-to-list 'tramp-methods
100 (tramp-login-program "remsh")
101 (tramp-login-args (("%h") ("-l" "%u")))
102 (tramp-remote-sh "/bin/sh")
103 (tramp-copy-program "rcp")
104 (tramp-copy-args (("-p" "%k")))
105 (tramp-copy-keep-date t
)))
109 '("scp" (tramp-login-program "ssh")
110 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
111 (tramp-async-args (("-q")))
112 (tramp-remote-sh "/bin/sh")
113 (tramp-copy-program "scp")
114 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r")))
115 (tramp-copy-keep-date t
)
116 (tramp-copy-recursive t
)
117 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
118 ("-o" "UserKnownHostsFile=/dev/null")
119 ("-o" "StrictHostKeyChecking=no")))
120 (tramp-default-port 22)))
122 (add-to-list 'tramp-methods
124 (tramp-login-program "ssh")
125 (tramp-login-args (("-l" "%u") ("-p" "%p")
126 ("-1") ("-e" "none") ("%h")))
127 (tramp-async-args (("-q")))
128 (tramp-remote-sh "/bin/sh")
129 (tramp-copy-program "scp")
130 (tramp-copy-args (("-1") ("-P" "%p") ("-p" "%k") ("-q") ("-r")))
131 (tramp-copy-keep-date t
)
132 (tramp-copy-recursive t
)
133 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
134 ("-o" "UserKnownHostsFile=/dev/null")
135 ("-o" "StrictHostKeyChecking=no")))
136 (tramp-default-port 22)))
138 (add-to-list 'tramp-methods
140 (tramp-login-program "ssh")
141 (tramp-login-args (("-l" "%u") ("-p" "%p")
142 ("-2") ("-e" "none") ("%h")))
143 (tramp-async-args (("-q")))
144 (tramp-remote-sh "/bin/sh")
145 (tramp-copy-program "scp")
146 (tramp-copy-args (("-2") ("-P" "%p") ("-p" "%k") ("-q") ("-r")))
147 (tramp-copy-keep-date t
)
148 (tramp-copy-recursive t
)
149 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
150 ("-o" "UserKnownHostsFile=/dev/null")
151 ("-o" "StrictHostKeyChecking=no")))
152 (tramp-default-port 22)))
154 (add-to-list 'tramp-methods
156 (tramp-login-program "ssh")
157 (tramp-login-args (("-l" "%u") ("-p" "%p")
158 ("-o" "ControlPath=%t.%%r@%%h:%%p")
159 ("-o" "ControlMaster=yes")
160 ("-e" "none") ("%h")))
161 (tramp-async-args (("-q")))
162 (tramp-remote-sh "/bin/sh")
163 (tramp-copy-program "scp")
164 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q")
165 ("-o" "ControlPath=%t.%%r@%%h:%%p")
166 ("-o" "ControlMaster=auto")))
167 (tramp-copy-keep-date t
)
168 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
169 ("-o" "UserKnownHostsFile=/dev/null")
170 ("-o" "StrictHostKeyChecking=no")))
171 (tramp-default-port 22)))
173 (add-to-list 'tramp-methods
175 (tramp-login-program "ssh")
176 (tramp-login-args (("-l" "%u") ("-p" "%p")
177 ("-e" "none") ("-t" "-t")
179 (tramp-async-args (("-q")))
180 (tramp-remote-sh "/bin/sh")
181 (tramp-copy-program "scp")
182 (tramp-copy-args (("-p" "%k")))
183 (tramp-copy-keep-date t
)
184 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
185 ("-o" "UserKnownHostsFile=/dev/null")
186 ("-o" "StrictHostKeyChecking=no")))
187 (tramp-default-port 22)))
189 (add-to-list 'tramp-methods
191 (tramp-login-program "ssh")
192 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
193 (tramp-async-args (("-q")))
194 (tramp-remote-sh "/bin/sh")
195 (tramp-copy-program "sftp")))
197 (add-to-list 'tramp-methods
199 (tramp-login-program "ssh")
200 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
201 (tramp-async-args (("-q")))
202 (tramp-remote-sh "/bin/sh")
203 (tramp-copy-program "rsync")
204 (tramp-copy-args (("-e" "ssh") ("-t" "%k") ("-r")))
205 (tramp-copy-keep-date t
)
206 (tramp-copy-keep-tmpfile t
)
207 (tramp-copy-recursive t
)))
209 (add-to-list 'tramp-methods
211 (tramp-login-program "ssh")
212 (tramp-login-args (("-l" "%u") ("-p" "%p")
213 ("-o" "ControlPath=%t.%%r@%%h:%%p")
214 ("-o" "ControlMaster=yes")
215 ("-e" "none") ("%h")))
216 (tramp-async-args (("-q")))
217 (tramp-remote-sh "/bin/sh")
218 (tramp-copy-program "rsync")
219 (tramp-copy-args (("-t" "%k") ("-r")))
220 (tramp-copy-env (("RSYNC_RSH")
223 " -o ControlPath=%t.%%r@%%h:%%p"
224 " -o ControlMaster=auto"))))
225 (tramp-copy-keep-date t
)
226 (tramp-copy-keep-tmpfile t
)
227 (tramp-copy-recursive t
)))
229 (add-to-list 'tramp-methods
231 (tramp-login-program "rsh")
232 (tramp-login-args (("%h") ("-l" "%u")))
233 (tramp-remote-sh "/bin/sh")))
235 (add-to-list 'tramp-methods
237 (tramp-login-program "remsh")
238 (tramp-login-args (("%h") ("-l" "%u")))
239 (tramp-remote-sh "/bin/sh")))
241 (add-to-list 'tramp-methods
243 (tramp-login-program "ssh")
244 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
245 (tramp-async-args (("-q")))
246 (tramp-remote-sh "/bin/sh")
247 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
248 ("-o" "UserKnownHostsFile=/dev/null")
249 ("-o" "StrictHostKeyChecking=no")))
250 (tramp-default-port 22)))
252 (add-to-list 'tramp-methods
254 (tramp-login-program "ssh")
255 (tramp-login-args (("-l" "%u") ("-p" "%p")
256 ("-1") ("-e" "none") ("%h")))
257 (tramp-async-args (("-q")))
258 (tramp-remote-sh "/bin/sh")
259 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
260 ("-o" "UserKnownHostsFile=/dev/null")
261 ("-o" "StrictHostKeyChecking=no")))
262 (tramp-default-port 22)))
264 (add-to-list 'tramp-methods
266 (tramp-login-program "ssh")
267 (tramp-login-args (("-l" "%u") ("-p" "%p")
268 ("-2") ("-e" "none") ("%h")))
269 (tramp-async-args (("-q")))
270 (tramp-remote-sh "/bin/sh")
271 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
272 ("-o" "UserKnownHostsFile=/dev/null")
273 ("-o" "StrictHostKeyChecking=no")))
274 (tramp-default-port 22)))
276 (add-to-list 'tramp-methods
278 (tramp-login-program "ssh")
279 (tramp-login-args (("-l" "%u") ("-p" "%p")
280 ("-e" "none") ("-t" "-t")
282 (tramp-async-args (("-q")))
283 (tramp-remote-sh "/bin/sh")
284 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
285 ("-o" "UserKnownHostsFile=/dev/null")
286 ("-o" "StrictHostKeyChecking=no")))
287 (tramp-default-port 22)))
289 (add-to-list 'tramp-methods
291 (tramp-login-program "telnet")
292 (tramp-login-args (("%h") ("%p")))
293 (tramp-remote-sh "/bin/sh")
294 (tramp-default-port 23)))
296 (add-to-list 'tramp-methods
298 (tramp-login-program "su")
299 (tramp-login-args (("-") ("%u")))
300 (tramp-remote-sh "/bin/sh")))
302 (add-to-list 'tramp-methods
304 (tramp-login-program "sudo")
305 (tramp-login-args (("-u" "%u") ("-s") ("-H") ("-p" "Password:")))
306 (tramp-remote-sh "/bin/sh")))
308 (add-to-list 'tramp-methods
310 (tramp-login-program "krlogin")
311 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
312 (tramp-remote-sh "/bin/sh")))
314 (add-to-list 'tramp-methods
316 (tramp-login-program "plink")
317 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
318 (tramp-remote-sh "/bin/sh")
319 (tramp-password-end-of-line "xy") ;see docstring for "xy"
320 (tramp-default-port 22)))
322 (add-to-list 'tramp-methods
324 (tramp-login-program "plink")
325 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-1" "-ssh") ("%h")))
326 (tramp-remote-sh "/bin/sh")
327 (tramp-password-end-of-line "xy") ;see docstring for "xy"
328 (tramp-default-port 22)))
330 (add-to-list 'tramp-methods
332 (tramp-login-program "plink")
333 ;; ("%h") must be a single element, see
334 ;; `tramp-compute-multi-hops'.
335 (tramp-login-args (("-load") ("%h") ("-t")
337 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
339 tramp-initial-end-of-output
))
341 (tramp-remote-sh "/bin/sh")))
343 (add-to-list 'tramp-methods
345 (tramp-login-program "plink")
346 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
347 (tramp-remote-sh "/bin/sh")
348 (tramp-copy-program "pscp")
349 (tramp-copy-args (("-P" "%p") ("-scp") ("-p" "%k")))
350 (tramp-copy-keep-date t
)
351 (tramp-password-end-of-line "xy") ;see docstring for "xy"
352 (tramp-default-port 22)))
354 (add-to-list 'tramp-methods
356 (tramp-login-program "plink")
357 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
358 (tramp-remote-sh "/bin/sh")
359 (tramp-copy-program "pscp")
360 (tramp-copy-args (("-P" "%p") ("-sftp") ("-p" "%k")))
361 (tramp-copy-keep-date t
)
362 (tramp-password-end-of-line "xy"))) ;see docstring for "xy"
364 (add-to-list 'tramp-methods
366 (tramp-login-program "fsh")
367 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
368 (tramp-remote-sh "/bin/sh -i")
369 (tramp-copy-program "fcp")
370 (tramp-copy-args (("-p" "%k")))
371 (tramp-copy-keep-date t
)))
373 (add-to-list 'tramp-default-method-alist
374 `(,tramp-local-host-regexp
"\\`root\\'" "su"))
376 (add-to-list 'tramp-default-user-alist
377 '("\\`su\\(do\\)?\\'" nil
"root"))
378 (add-to-list 'tramp-default-user-alist
379 `("\\`r\\(em\\)?\\(cp\\|sh\\)\\|telnet\\|plink1?\\'"
380 nil
,(user-login-name)))
382 (defconst tramp-completion-function-alist-rsh
383 '((tramp-parse-rhosts "/etc/hosts.equiv")
384 (tramp-parse-rhosts "~/.rhosts"))
385 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
387 (defconst tramp-completion-function-alist-ssh
388 '((tramp-parse-rhosts "/etc/hosts.equiv")
389 (tramp-parse-rhosts "/etc/shosts.equiv")
390 (tramp-parse-shosts "/etc/ssh_known_hosts")
391 (tramp-parse-sconfig "/etc/ssh_config")
392 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
393 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
394 (tramp-parse-rhosts "~/.rhosts")
395 (tramp-parse-rhosts "~/.shosts")
396 (tramp-parse-shosts "~/.ssh/known_hosts")
397 (tramp-parse-sconfig "~/.ssh/config")
398 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
399 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
400 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
402 (defconst tramp-completion-function-alist-telnet
403 '((tramp-parse-hosts "/etc/hosts"))
404 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
406 (defconst tramp-completion-function-alist-su
407 '((tramp-parse-passwd "/etc/passwd"))
408 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
410 (defconst tramp-completion-function-alist-putty
412 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"))
413 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty methods.")
415 (tramp-set-completion-function "rcp" tramp-completion-function-alist-rsh
)
416 (tramp-set-completion-function "remcp" tramp-completion-function-alist-rsh
)
417 (tramp-set-completion-function "scp" tramp-completion-function-alist-ssh
)
418 (tramp-set-completion-function "scp1" tramp-completion-function-alist-ssh
)
419 (tramp-set-completion-function "scp2" tramp-completion-function-alist-ssh
)
420 (tramp-set-completion-function "scpc" tramp-completion-function-alist-ssh
)
421 (tramp-set-completion-function "scpx" tramp-completion-function-alist-ssh
)
422 (tramp-set-completion-function "sftp" tramp-completion-function-alist-ssh
)
423 (tramp-set-completion-function "rsync" tramp-completion-function-alist-ssh
)
424 (tramp-set-completion-function "rsyncc" tramp-completion-function-alist-ssh
)
425 (tramp-set-completion-function "rsh" tramp-completion-function-alist-rsh
)
426 (tramp-set-completion-function "remsh" tramp-completion-function-alist-rsh
)
427 (tramp-set-completion-function "ssh" tramp-completion-function-alist-ssh
)
428 (tramp-set-completion-function "ssh1" tramp-completion-function-alist-ssh
)
429 (tramp-set-completion-function "ssh2" tramp-completion-function-alist-ssh
)
430 (tramp-set-completion-function "ssh1_old" tramp-completion-function-alist-ssh
)
431 (tramp-set-completion-function "ssh2_old" tramp-completion-function-alist-ssh
)
432 (tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh
)
433 (tramp-set-completion-function "telnet" tramp-completion-function-alist-telnet
)
434 (tramp-set-completion-function "su" tramp-completion-function-alist-su
)
435 (tramp-set-completion-function "sudo" tramp-completion-function-alist-su
)
436 (tramp-set-completion-function "krlogin" tramp-completion-function-alist-rsh
)
437 (tramp-set-completion-function "plink" tramp-completion-function-alist-ssh
)
438 (tramp-set-completion-function "plink1" tramp-completion-function-alist-ssh
)
439 (tramp-set-completion-function "plinkx" tramp-completion-function-alist-putty
)
440 (tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh
)
441 (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh
)
443 ;; "getconf PATH" yields:
444 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
445 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
446 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
447 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
449 (defcustom tramp-remote-path
450 '(tramp-default-remote-path "/usr/sbin" "/usr/local/bin"
451 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
452 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
453 "*List of directories to search for executables on remote host.
454 For every remote host, this variable will be set buffer local,
455 keeping the list of existing directories on that host.
457 You can use `~' in this list, but when searching for a shell which groks
458 tilde expansion, all directory names starting with `~' will be ignored.
460 `Default Directories' represent the list of directories given by
461 the command \"getconf PATH\". It is recommended to use this
462 entry on top of this list, because these are the default
463 directories for POSIX compatible commands.
465 `Private Directories' are the settings of the $PATH environment,
466 as given in your `~/.profile'."
468 :type
'(repeat (choice
469 (const :tag
"Default Directories" tramp-default-remote-path
)
470 (const :tag
"Private Directories" tramp-own-remote-path
)
471 (string :tag
"Directory"))))
473 (defcustom tramp-remote-process-environment
474 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "LC_ALL=C"
475 ,(format "TERM=%s" tramp-terminal-type
)
476 "EMACS=t" ;; Deprecated.
477 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version
)
478 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH="
479 "autocorrect=" "correct=")
481 "*List of environment variables to be set on the remote host.
483 Each element should be a string of the form ENVVARNAME=VALUE. An
484 entry ENVVARNAME= diables the corresponding environment variable,
485 which might have been set in the init files like ~/.profile.
487 Special handling is applied to the PATH environment, which should
488 not be set here. Instead of, it should be set via `tramp-remote-path'."
490 :type
'(repeat string
))
492 (defcustom tramp-sh-extra-args
'(("/bash\\'" .
"-norc -noprofile"))
493 "*Alist specifying extra arguments to pass to the remote shell.
494 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
495 matching the shell file name and ARGS is a string specifying the
498 This variable is only used when Tramp needs to start up another shell
499 for tilde expansion. The extra arguments should typically prevent the
500 shell from reading its init file."
502 ;; This might be the wrong way to test whether the widget type
503 ;; `alist' is available. Who knows the right way to test it?
504 :type
(if (get 'alist
'widget-type
)
505 '(alist :key-type string
:value-type string
)
506 '(repeat (cons string string
))))
508 (defconst tramp-actions-before-shell
509 '((tramp-login-prompt-regexp tramp-action-login
)
510 (tramp-password-prompt-regexp tramp-action-password
)
511 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
512 (shell-prompt-pattern tramp-action-succeed
)
513 (tramp-shell-prompt-pattern tramp-action-succeed
)
514 (tramp-yesno-prompt-regexp tramp-action-yesno
)
515 (tramp-yn-prompt-regexp tramp-action-yn
)
516 (tramp-terminal-prompt-regexp tramp-action-terminal
)
517 (tramp-process-alive-regexp tramp-action-process-alive
))
518 "List of pattern/action pairs.
519 Whenever a pattern matches, the corresponding action is performed.
520 Each item looks like (PATTERN ACTION).
522 The PATTERN should be a symbol, a variable. The value of this
523 variable gives the regular expression to search for. Note that the
524 regexp must match at the end of the buffer, \"\\'\" is implicitly
527 The ACTION should also be a symbol, but a function. When the
528 corresponding PATTERN matches, the ACTION function is called.")
530 (defconst tramp-actions-copy-out-of-band
531 '((tramp-password-prompt-regexp tramp-action-password
)
532 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
533 (tramp-copy-failed-regexp tramp-action-permission-denied
)
534 (tramp-process-alive-regexp tramp-action-out-of-band
))
535 "List of pattern/action pairs.
536 This list is used for copying/renaming with out-of-band methods.
538 See `tramp-actions-before-shell' for more info.")
540 (defconst tramp-uudecode
541 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
544 "Shell function to implement `uudecode' to standard output.
545 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
546 for this or `uudecode -p', but some systems don't, and for them
547 we have this shell function.")
549 (defconst tramp-perl-file-truename
552 use Cwd \"realpath\";
555 my ($volume, @dirs) = @_;
556 my $real = realpath(File::Spec->catpath(
557 $volume, File::Spec->catdir(@dirs), \"\"));
559 my ($vol, $dir) = File::Spec->splitpath($real, 1);
560 return ($vol, File::Spec->splitdir($dir));
563 my $last = pop(@dirs);
564 ($volume, @dirs) = recursive($volume, @dirs);
566 return ($volume, @dirs);
570 $result = realpath($ARGV[0]);
572 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
573 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
575 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
578 if ($ARGV[0] =~ /\\/$/) {
579 $result = $result . \"/\";
582 print \"\\\"$result\\\"\\n\";
583 ' \"$1\" 2>/dev/null"
584 "Perl script to produce output suitable for use with `file-truename'
585 on the remote file system.
586 Escape sequence %s is replaced with name of Perl binary.
587 This string is passed to `format', so percent characters need to be doubled.")
589 (defconst tramp-perl-file-name-all-completions
599 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
600 @files = readdir(d); closedir(d);
601 foreach $f (@files) {
602 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
603 if (-d \"$ARGV[0]/$f\") {
612 ' \"$1\" \"$2\" \"$3\" 2>/dev/null"
613 "Perl script to produce output suitable for use with
614 `file-name-all-completions' on the remote file system. Escape
615 sequence %s is replaced with name of Perl binary. This string is
616 passed to `format', so percent characters need to be doubled.")
618 ;; Perl script to implement `file-attributes' in a Lisp `read'able
619 ;; output. If you are hacking on this, note that you get *no* output
620 ;; unless this spits out a complete line, including the '\n' at the
622 ;; The device number is returned as "-1", because there will be a virtual
623 ;; device number set in `tramp-sh-handle-file-attributes'.
624 (defconst tramp-perl-file-attributes
626 @stat = lstat($ARGV[0]);
631 if (($stat[2] & 0170000) == 0120000)
633 $type = readlink($ARGV[0]);
634 $type = \"\\\"$type\\\"\";
636 elsif (($stat[2] & 0170000) == 040000)
644 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
645 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
647 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
652 $stat[8] >> 16 & 0xffff,
654 $stat[9] >> 16 & 0xffff,
656 $stat[10] >> 16 & 0xffff,
660 $stat[1] >> 16 & 0xffff,
662 );' \"$1\" \"$2\" 2>/dev/null"
663 "Perl script to produce output suitable for use with `file-attributes'
664 on the remote file system.
665 Escape sequence %s is replaced with name of Perl binary.
666 This string is passed to `format', so percent characters need to be doubled.")
668 (defconst tramp-perl-directory-files-and-attributes
670 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
671 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
672 @list = readdir(DIR);
676 for($i = 0; $i < $n; $i++)
678 $filename = $list[$i];
679 @stat = lstat($filename);
680 if (($stat[2] & 0170000) == 0120000)
682 $type = readlink($filename);
683 $type = \"\\\"$type\\\"\";
685 elsif (($stat[2] & 0170000) == 040000)
693 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
694 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
696 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
702 $stat[8] >> 16 & 0xffff,
704 $stat[9] >> 16 & 0xffff,
706 $stat[10] >> 16 & 0xffff,
710 $stat[1] >> 16 & 0xffff,
712 $stat[0] >> 16 & 0xffff,
715 printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
716 "Perl script implementing `directory-files-attributes' as Lisp `read'able
718 Escape sequence %s is replaced with name of Perl binary.
719 This string is passed to `format', so percent characters need to be doubled.")
721 ;; These two use base64 encoding.
722 (defconst tramp-perl-encode-with-module
723 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
724 "Perl program to use for encoding a file.
725 Escape sequence %s is replaced with name of Perl binary.
726 This string is passed to `format', so percent characters need to be doubled.
727 This implementation requires the MIME::Base64 Perl module to be installed
728 on the remote host.")
730 (defconst tramp-perl-decode-with-module
731 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
732 "Perl program to use for decoding a file.
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.
735 This implementation requires the MIME::Base64 Perl module to be installed
736 on the remote host.")
738 (defconst tramp-perl-encode
740 # This script contributed by Juanma Barranquero <lektu@terra.es>.
741 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
742 # Free Software Foundation, Inc.
747 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
748 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
753 # We read in chunks of 54 bytes, to generate output lines
754 # of 72 chars (plus end of line)
757 while (my $data = <STDIN>) {
760 # Only for the last chunk, and only if did not fill the last three-byte packet
762 my $mod = length($data) %% 3;
763 $pad = q(=) x (3 - $mod) if $mod;
766 # Not the fastest method, but it is simple: unpack to binary string, split
767 # by groups of 6 bits and convert back from binary to byte; then map into
768 # the translation table
772 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
776 "Perl program to use for encoding a file.
777 Escape sequence %s is replaced with name of Perl binary.
778 This string is passed to `format', so percent characters need to be doubled.")
780 (defconst tramp-perl-decode
782 # This script contributed by Juanma Barranquero <lektu@terra.es>.
783 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
784 # Free Software Foundation, Inc.
789 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
790 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
793 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
797 # We are going to accumulate into $pending to accept any line length
798 # (we do not check they are <= 76 chars as the RFC says)
801 while (my $data = <STDIN>) {
804 # If we find one or two =, we have reached the end and
805 # any following data is to be discarded
806 my $finished = $data =~ s/(==?).*/$1/;
809 my $len = length($pending);
810 my $chunk = substr($pending, 0, $len & ~3);
811 $pending = substr($pending, $len & ~3 + 1);
813 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
814 # split in 8-bit chunks and convert back to char.
817 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
821 "Perl program to use for decoding a file.
822 Escape sequence %s is replaced with name of Perl binary.
823 This string is passed to `format', so percent characters need to be doubled.")
825 (defconst tramp-vc-registered-read-file-names
828 if %s \"$file\"; then
829 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
831 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
833 if %s \"$file\"; then
834 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
836 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
840 "Script to check existence of VC related files.
841 It must be send formatted with two strings; the tests for file
842 existence, and file readability. Input shall be read via
843 here-document, otherwise the command could exceed maximum length
846 (defconst tramp-file-mode-type-map
847 '((0 .
"-") ; Normal file (SVID-v2 and XPG2)
849 (2 .
"c") ; character device
850 (3 .
"m") ; multiplexed character device (v7)
851 (4 .
"d") ; directory
852 (5 .
"?") ; Named special file (XENIX)
853 (6 .
"b") ; block device
854 (7 .
"?") ; multiplexed block device (v7)
855 (8 .
"-") ; regular file
856 (9 .
"n") ; network special file (HP-UX)
858 (11 .
"?") ; ACL shadow inode (Solaris, not userspace)
860 (13 .
"D") ; door special (Solaris)
861 (14 .
"w")) ; whiteout (BSD)
862 "A list of file types returned from the `stat' system call.
863 This is used to map a mode number to a permission string.")
865 ;; New handlers should be added here. The following operations can be
866 ;; handled using the normal primitives: file-name-sans-versions,
868 (defconst tramp-sh-file-name-handler-alist
869 '((load . tramp-handle-load
)
870 (make-symbolic-link . tramp-sh-handle-make-symbolic-link
)
871 (file-name-as-directory . tramp-handle-file-name-as-directory
)
872 (file-name-directory . tramp-handle-file-name-directory
)
873 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
874 (file-truename . tramp-sh-handle-file-truename
)
875 (file-exists-p . tramp-sh-handle-file-exists-p
)
876 (file-directory-p . tramp-sh-handle-file-directory-p
)
877 (file-executable-p . tramp-sh-handle-file-executable-p
)
878 (file-readable-p . tramp-sh-handle-file-readable-p
)
879 (file-regular-p . tramp-handle-file-regular-p
)
880 (file-symlink-p . tramp-handle-file-symlink-p
)
881 (file-writable-p . tramp-sh-handle-file-writable-p
)
882 (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p
)
883 (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p
)
884 (file-attributes . tramp-sh-handle-file-attributes
)
885 (file-modes . tramp-handle-file-modes
)
886 (directory-files . tramp-handle-directory-files
)
887 (directory-files-and-attributes
888 . tramp-sh-handle-directory-files-and-attributes
)
889 (file-name-all-completions . tramp-sh-handle-file-name-all-completions
)
890 (file-name-completion . tramp-handle-file-name-completion
)
891 (add-name-to-file . tramp-sh-handle-add-name-to-file
)
892 (copy-file . tramp-sh-handle-copy-file
)
893 (copy-directory . tramp-sh-handle-copy-directory
)
894 (rename-file . tramp-sh-handle-rename-file
)
895 (set-file-modes . tramp-sh-handle-set-file-modes
)
896 (set-file-times . tramp-sh-handle-set-file-times
)
897 (make-directory . tramp-sh-handle-make-directory
)
898 (delete-directory . tramp-sh-handle-delete-directory
)
899 (delete-file . tramp-sh-handle-delete-file
)
900 (directory-file-name . tramp-handle-directory-file-name
)
901 ;; `executable-find' is not official yet.
902 (executable-find . tramp-sh-handle-executable-find
)
903 (start-file-process . tramp-sh-handle-start-file-process
)
904 (process-file . tramp-sh-handle-process-file
)
905 (shell-command . tramp-sh-handle-shell-command
)
906 (insert-directory . tramp-sh-handle-insert-directory
)
907 (expand-file-name . tramp-sh-handle-expand-file-name
)
908 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
909 (file-local-copy . tramp-sh-handle-file-local-copy
)
910 (file-remote-p . tramp-handle-file-remote-p
)
911 (insert-file-contents . tramp-handle-insert-file-contents
)
912 (insert-file-contents-literally
913 . tramp-sh-handle-insert-file-contents-literally
)
914 (write-region . tramp-sh-handle-write-region
)
915 (find-backup-file-name . tramp-handle-find-backup-file-name
)
916 (make-auto-save-file-name . tramp-sh-handle-make-auto-save-file-name
)
917 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
918 (dired-compress-file . tramp-sh-handle-dired-compress-file
)
919 (dired-recursive-delete-directory
920 . tramp-sh-handle-dired-recursive-delete-directory
)
921 (dired-uncache . tramp-handle-dired-uncache
)
922 (set-visited-file-modtime . tramp-sh-handle-set-visited-file-modtime
)
923 (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime
)
924 (file-selinux-context . tramp-sh-handle-file-selinux-context
)
925 (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context
)
926 (vc-registered . tramp-sh-handle-vc-registered
))
927 "Alist of handler functions.
928 Operations not mentioned here will be handled by the normal Emacs functions.")
930 ;; This must be the last entry, because `identity' always matches.
932 (add-to-list 'tramp-foreign-file-name-handler-alist
933 '(identity . tramp-sh-file-name-handler
) 'append
)
935 ;;; File Name Handler Functions:
937 (defun tramp-sh-handle-make-symbolic-link
938 (filename linkname
&optional ok-if-already-exists
)
939 "Like `make-symbolic-link' for Tramp files.
940 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
941 the symlink. If LINKNAME is a Tramp file, only the localname component is
942 used as the target of the symlink.
944 If LINKNAME is a Tramp file and the localname component is relative, then
945 it is expanded first, before the localname component is taken. Note that
946 this can give surprising results if the user/host for the source and
947 target of the symlink differ."
948 (with-parsed-tramp-file-name linkname l
949 (let ((ln (tramp-get-remote-ln l
))
950 (cwd (tramp-run-real-handler
951 'file-name-directory
(list l-localname
))))
955 "Making a symbolic link. ln(1) does not exist on the remote host."))
957 ;; Do the 'confirm if exists' thing.
958 (when (file-exists-p linkname
)
960 (if (or (null ok-if-already-exists
) ; not allowed to exist
961 (and (numberp ok-if-already-exists
)
964 "File %s already exists; make it a link anyway? "
967 l
'file-already-exists
"File %s already exists" l-localname
)
968 (delete-file linkname
)))
970 ;; If FILENAME is a Tramp name, use just the localname component.
971 (when (tramp-tramp-file-p filename
)
973 (tramp-file-name-localname
974 (tramp-dissect-file-name (expand-file-name filename
)))))
976 (tramp-flush-file-property l
(file-name-directory l-localname
))
977 (tramp-flush-file-property l l-localname
)
979 ;; Right, they are on the same host, regardless of user, method, etc.
980 ;; We now make the link on the remote machine. This will occur as the user
981 ;; that FILENAME belongs to.
982 (tramp-send-command-and-check
985 "cd %s && %s -sf %s %s"
986 (tramp-shell-quote-argument cwd
)
988 (tramp-shell-quote-argument filename
)
989 (tramp-shell-quote-argument l-localname
))
992 (defun tramp-sh-handle-file-truename (filename &optional counter prev-dirs
)
993 "Like `file-truename' for Tramp files."
994 (with-parsed-tramp-file-name (expand-file-name filename
) nil
995 (with-file-property v localname
"file-truename"
996 (let ((result nil
)) ; result steps in reverse order
997 (tramp-message v
4 "Finding true name for `%s'" filename
)
999 ;; Use GNU readlink --canonicalize-missing where available.
1000 ((tramp-get-remote-readlink v
)
1002 (tramp-send-command-and-read
1004 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
1005 (tramp-get-remote-readlink v
)
1006 (tramp-shell-quote-argument localname
)))))
1008 ;; Use Perl implementation.
1009 ((and (tramp-get-remote-perl v
)
1010 (tramp-get-connection-property v
"perl-file-spec" nil
)
1011 (tramp-get-connection-property v
"perl-cwd-realpath" nil
))
1012 (tramp-maybe-send-script
1013 v tramp-perl-file-truename
"tramp_perl_file_truename")
1015 (tramp-send-command-and-read
1017 (format "tramp_perl_file_truename %s"
1018 (tramp-shell-quote-argument localname
)))))
1020 ;; Do it yourself. We bind `directory-sep-char' here for
1021 ;; XEmacs on Windows, which would otherwise use backslash.
1022 (t (let* ((directory-sep-char ?
/)
1023 (steps (tramp-compat-split-string localname
"/"))
1024 (localnamedir (tramp-run-real-handler
1025 'file-name-as-directory
(list localname
)))
1026 (is-dir (string= localname localnamedir
))
1029 ;; Don't make the following value larger than
1030 ;; necessary. People expect an error message in a
1031 ;; timely fashion when something is wrong;
1032 ;; otherwise they might think that Emacs is hung.
1033 ;; Of course, correctness has to come first.
1036 (while (and steps
(< numchase numchase-limit
))
1037 (setq thisstep
(pop steps
))
1040 (mapconcat 'identity
1041 (append '("") (reverse result
) (list thisstep
))
1043 (setq symlink-target
1044 (nth 0 (file-attributes
1045 (tramp-make-tramp-file-name
1047 (mapconcat 'identity
1052 (cond ((string= "." thisstep
)
1053 (tramp-message v
5 "Ignoring step `.'"))
1054 ((string= ".." thisstep
)
1055 (tramp-message v
5 "Processing step `..'")
1057 ((stringp symlink-target
)
1058 ;; It's a symlink, follow it.
1059 (tramp-message v
5 "Follow symlink to %s" symlink-target
)
1060 (setq numchase
(1+ numchase
))
1061 (when (file-name-absolute-p symlink-target
)
1063 ;; If the symlink was absolute, we'll get a string like
1064 ;; "/user@host:/some/target"; extract the
1065 ;; "/some/target" part from it.
1066 (when (tramp-tramp-file-p symlink-target
)
1067 (unless (tramp-equal-remote filename symlink-target
)
1070 "Symlink target `%s' on wrong host" symlink-target
))
1071 (setq symlink-target localname
))
1073 (append (tramp-compat-split-string
1078 (setq result
(cons thisstep result
)))))
1079 (when (>= numchase numchase-limit
)
1082 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
1083 (setq result
(reverse result
))
1084 ;; Combine list to form string.
1087 (mapconcat 'identity
(cons "" result
) "/")
1089 (when (and is-dir
(or (string= "" result
)
1090 (not (string= (substring result -
1) "/"))))
1091 (setq result
(concat result
"/"))))))
1093 (tramp-message v
4 "True name of `%s' is `%s'" filename result
)
1094 (tramp-make-tramp-file-name method user host result
)))))
1098 (defun tramp-sh-handle-file-exists-p (filename)
1099 "Like `file-exists-p' for Tramp files."
1100 (with-parsed-tramp-file-name filename nil
1101 (with-file-property v localname
"file-exists-p"
1102 (or (not (null (tramp-get-file-property
1103 v localname
"file-attributes-integer" nil
)))
1104 (not (null (tramp-get-file-property
1105 v localname
"file-attributes-string" nil
)))
1106 (tramp-send-command-and-check
1110 (tramp-get-file-exists-command v
)
1111 (tramp-shell-quote-argument localname
)))))))
1113 ;; CCC: This should check for an error condition and signal failure
1114 ;; when something goes wrong.
1115 ;; Daniel Pittman <daniel@danann.net>
1116 (defun tramp-sh-handle-file-attributes (filename &optional id-format
)
1117 "Like `file-attributes' for Tramp files."
1118 (unless id-format
(setq id-format
'integer
))
1119 ;; Don't modify `last-coding-system-used' by accident.
1120 (let ((last-coding-system-used last-coding-system-used
))
1121 (with-parsed-tramp-file-name (expand-file-name filename
) nil
1122 (with-file-property v localname
(format "file-attributes-%s" id-format
)
1124 (tramp-convert-file-attributes
1127 ((tramp-get-remote-stat v
)
1128 (tramp-do-file-attributes-with-stat v localname id-format
))
1129 ((tramp-get-remote-perl v
)
1130 (tramp-do-file-attributes-with-perl v localname id-format
))
1132 (tramp-do-file-attributes-with-ls v localname id-format
)))))))))
1134 (defun tramp-do-file-attributes-with-ls (vec localname
&optional id-format
)
1135 "Implement `file-attributes' for Tramp files using the ls(1) command."
1137 res-inode res-filemodes res-numlinks
1138 res-uid res-gid res-size res-symlink-target
)
1139 (tramp-message vec
5 "file attributes with ls: %s" localname
)
1142 (format "(%s %s || %s -h %s) && %s %s %s"
1143 (tramp-get-file-exists-command vec
)
1144 (tramp-shell-quote-argument localname
)
1145 (tramp-get-test-command vec
)
1146 (tramp-shell-quote-argument localname
)
1147 (tramp-get-ls-command vec
)
1148 (if (eq id-format
'integer
) "-ildn" "-ild")
1149 (tramp-shell-quote-argument localname
)))
1150 ;; parse `ls -l' output ...
1151 (with-current-buffer (tramp-get-buffer vec
)
1152 (when (> (buffer-size) 0)
1153 (goto-char (point-min))
1157 (read (current-buffer))
1158 (invalid-read-syntax
1159 (when (and (equal (cadr err
)
1160 "Integer constant overflow in reader")
1162 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1164 (let* ((big (read (substring (car (cddr err
)) 0
1165 (match-beginning 1))))
1166 (small (read (match-string 1 (car (cddr err
)))))
1167 (twiddle (/ small
65536)))
1168 (cons (+ big twiddle
)
1169 (- small
(* twiddle
65536))))))))
1170 ;; ... file mode flags
1171 (setq res-filemodes
(symbol-name (read (current-buffer))))
1173 (setq res-numlinks
(read (current-buffer)))
1175 (setq res-uid
(read (current-buffer)))
1176 (setq res-gid
(read (current-buffer)))
1177 (if (eq id-format
'integer
)
1179 (unless (numberp res-uid
) (setq res-uid -
1))
1180 (unless (numberp res-gid
) (setq res-gid -
1)))
1182 (unless (stringp res-uid
) (setq res-uid
(symbol-name res-uid
)))
1183 (unless (stringp res-gid
) (setq res-gid
(symbol-name res-gid
)))))
1185 (setq res-size
(read (current-buffer)))
1186 ;; From the file modes, figure out other stuff.
1187 (setq symlinkp
(eq ?l
(aref res-filemodes
0)))
1188 (setq dirp
(eq ?d
(aref res-filemodes
0)))
1189 ;; if symlink, find out file name pointed to
1191 (search-forward "-> ")
1192 (setq res-symlink-target
1193 (buffer-substring (point) (tramp-compat-line-end-position))))
1194 ;; return data gathered
1196 ;; 0. t for directory, string (name linked to) for symbolic
1198 (or dirp res-symlink-target
)
1199 ;; 1. Number of links to file.
1205 ;; 4. Last access time, as a list of two integers. First
1206 ;; integer has high-order 16 bits of time, second has low 16
1208 ;; 5. Last modification time, likewise.
1209 ;; 6. Last status change time, likewise.
1210 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1211 ;; 7. Size in bytes (-1, if number is out of range).
1213 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1215 ;; 9. t if file's gid would change if file were deleted and
1216 ;; recreated. Will be set in `tramp-convert-file-attributes'
1218 ;; 10. inode number.
1220 ;; 11. Device number. Will be replaced by a virtual device number.
1224 (defun tramp-do-file-attributes-with-perl
1225 (vec localname
&optional id-format
)
1226 "Implement `file-attributes' for Tramp files using a Perl script."
1227 (tramp-message vec
5 "file attributes with perl: %s" localname
)
1228 (tramp-maybe-send-script
1229 vec tramp-perl-file-attributes
"tramp_perl_file_attributes")
1230 (tramp-send-command-and-read
1232 (format "tramp_perl_file_attributes %s %s"
1233 (tramp-shell-quote-argument localname
) id-format
)))
1235 (defun tramp-do-file-attributes-with-stat
1236 (vec localname
&optional id-format
)
1237 "Implement `file-attributes' for Tramp files using stat(1) command."
1238 (tramp-message vec
5 "file attributes with stat: %s" localname
)
1239 (tramp-send-command-and-read
1242 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
1243 ;; parse correctly the sequence "((". Therefore, we add a space.
1244 "( (%s %s || %s -h %s) && %s -c '((\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)' %s || echo nil)"
1245 (tramp-get-file-exists-command vec
)
1246 (tramp-shell-quote-argument localname
)
1247 (tramp-get-test-command vec
)
1248 (tramp-shell-quote-argument localname
)
1249 (tramp-get-remote-stat vec
)
1250 (if (eq id-format
'integer
) "%u" "\"%U\"")
1251 (if (eq id-format
'integer
) "%g" "\"%G\"")
1252 (tramp-shell-quote-argument localname
))))
1254 (defun tramp-sh-handle-set-visited-file-modtime (&optional time-list
)
1255 "Like `set-visited-file-modtime' for Tramp files."
1256 (unless (buffer-file-name)
1257 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1260 (tramp-run-real-handler 'set-visited-file-modtime
(list time-list
))
1261 (let ((f (buffer-file-name))
1263 (with-parsed-tramp-file-name f nil
1264 (let* ((attr (file-attributes f
))
1265 ;; '(-1 65535) means file doesn't exists yet.
1266 (modtime (or (nth 5 attr
) '(-1 65535))))
1267 (when (boundp 'last-coding-system-used
)
1268 (setq coding-system-used
(symbol-value 'last-coding-system-used
)))
1269 ;; We use '(0 0) as a don't-know value. See also
1270 ;; `tramp-do-file-attributes-with-ls'.
1271 (if (not (equal modtime
'(0 0)))
1272 (tramp-run-real-handler 'set-visited-file-modtime
(list modtime
))
1276 (format "%s -ild %s"
1277 (tramp-get-ls-command v
)
1278 (tramp-shell-quote-argument localname
)))
1279 (setq attr
(buffer-substring (point)
1280 (progn (end-of-line) (point)))))
1281 (tramp-set-file-property
1282 v localname
"visited-file-modtime-ild" attr
))
1283 (when (boundp 'last-coding-system-used
)
1284 (set 'last-coding-system-used coding-system-used
))
1287 ;; This function makes the same assumption as
1288 ;; `tramp-sh-handle-set-visited-file-modtime'.
1289 (defun tramp-sh-handle-verify-visited-file-modtime (buf)
1290 "Like `verify-visited-file-modtime' for Tramp files.
1291 At the time `verify-visited-file-modtime' calls this function, we
1292 already know that the buffer is visiting a file and that
1293 `visited-file-modtime' does not return 0. Do not call this
1294 function directly, unless those two cases are already taken care
1296 (with-current-buffer buf
1297 (let ((f (buffer-file-name)))
1298 ;; There is no file visiting the buffer, or the buffer has no
1299 ;; recorded last modification time, or there is no established
1302 (eq (visited-file-modtime) 0)
1303 (not (tramp-file-name-handler 'file-remote-p f nil
'connected
)))
1305 (with-parsed-tramp-file-name f nil
1306 (let* ((remote-file-name-inhibit-cache t
)
1307 (attr (file-attributes f
))
1308 (modtime (nth 5 attr
))
1309 (mt (visited-file-modtime)))
1312 ;; File exists, and has a known modtime.
1313 ((and attr
(not (equal modtime
'(0 0))))
1314 (< (abs (tramp-time-diff
1316 ;; For compatibility, deal with both the old
1317 ;; (HIGH . LOW) and the new (HIGH LOW) return
1318 ;; values of `visited-file-modtime'.
1320 (list (car mt
) (cdr mt
))
1323 ;; Modtime has the don't know value.
1327 (format "%s -ild %s"
1328 (tramp-get-ls-command v
)
1329 (tramp-shell-quote-argument localname
)))
1330 (with-current-buffer (tramp-get-buffer v
)
1331 (setq attr
(buffer-substring
1332 (point) (progn (end-of-line) (point)))))
1335 (tramp-get-file-property
1336 v localname
"visited-file-modtime-ild" "")))
1337 ;; If file does not exist, say it is not modified if and
1338 ;; only if that agrees with the buffer's record.
1339 (t (equal mt
'(-1 65535))))))))))
1341 (defun tramp-sh-handle-set-file-modes (filename mode
)
1342 "Like `set-file-modes' for Tramp files."
1343 (with-parsed-tramp-file-name filename nil
1344 (tramp-flush-file-property v localname
)
1345 ;; FIXME: extract the proper text from chmod's stderr.
1346 (tramp-barf-unless-okay
1348 (format "chmod %s %s"
1349 (tramp-compat-decimal-to-octal mode
)
1350 (tramp-shell-quote-argument localname
))
1351 "Error while changing file's mode %s" filename
)))
1353 (defun tramp-sh-handle-set-file-times (filename &optional time
)
1354 "Like `set-file-times' for Tramp files."
1355 (if (file-remote-p filename
)
1356 (with-parsed-tramp-file-name filename nil
1357 (tramp-flush-file-property v localname
)
1358 (let ((time (if (or (null time
) (equal time
'(0 0)))
1361 ;; With GNU Emacs, `format-time-string' has an optional
1362 ;; parameter UNIVERSAL. This is preferred, because we
1363 ;; could handle the case when the remote host is located
1364 ;; in a different time zone as the local host.
1365 (utc (not (featurep 'xemacs
))))
1366 (tramp-send-command-and-check
1367 v
(format "%s touch -t %s %s"
1368 (if utc
"TZ=UTC; export TZ;" "")
1370 (format-time-string "%Y%m%d%H%M.%S" time t
)
1371 (format-time-string "%Y%m%d%H%M.%S" time
))
1372 (tramp-shell-quote-argument localname
)))))
1374 ;; We handle also the local part, because in older Emacsen,
1375 ;; without `set-file-times', this function is an alias for this.
1376 ;; We are local, so we don't need the UTC settings.
1378 (tramp-compat-call-process
1379 "touch" nil nil nil
"-t"
1380 (format-time-string "%Y%m%d%H%M.%S" time
)
1381 (tramp-shell-quote-argument filename
)))))
1383 (defun tramp-set-file-uid-gid (filename &optional uid gid
)
1384 "Set the ownership for FILENAME.
1385 If UID and GID are provided, these values are used; otherwise uid
1386 and gid of the corresponding user is taken. Both parameters must be integers."
1387 ;; Modern Unices allow chown only for root. So we might need
1388 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1389 ;; working with su(do)? when it is needed, so it shall succeed in
1390 ;; the majority of cases.
1391 ;; Don't modify `last-coding-system-used' by accident.
1392 (let ((last-coding-system-used last-coding-system-used
))
1393 (if (file-remote-p filename
)
1394 (with-parsed-tramp-file-name filename nil
1395 (if (and (zerop (user-uid)) (tramp-local-host-p v
))
1396 ;; If we are root on the local host, we can do it directly.
1397 (tramp-set-file-uid-gid localname uid gid
)
1398 (let ((uid (or (and (integerp uid
) uid
)
1399 (tramp-get-remote-uid v
'integer
)))
1400 (gid (or (and (integerp gid
) gid
)
1401 (tramp-get-remote-gid v
'integer
))))
1404 "chown %d:%d %s" uid gid
1405 (tramp-shell-quote-argument localname
))))))
1407 ;; We handle also the local part, because there doesn't exist
1408 ;; `set-file-uid-gid'. On W32 "chown" might not work.
1409 (let ((uid (or (and (integerp uid
) uid
) (tramp-get-local-uid 'integer
)))
1410 (gid (or (and (integerp gid
) gid
) (tramp-get-local-gid 'integer
))))
1411 (tramp-compat-call-process
1413 (format "%d:%d" uid gid
) (tramp-shell-quote-argument filename
))))))
1415 (defun tramp-remote-selinux-p (vec)
1416 "Check, whether SELINUX is enabled on the remote host."
1417 (with-connection-property (tramp-get-connection-process vec
) "selinux-p"
1418 (let ((result (tramp-find-executable
1419 vec
"getenforce" (tramp-get-remote-path vec
) t t
)))
1422 (tramp-send-command-and-read
1423 vec
(format "echo \\\"`%S`\\\"" result
))
1426 (defun tramp-sh-handle-file-selinux-context (filename)
1427 "Like `file-selinux-context' for Tramp files."
1428 (with-parsed-tramp-file-name filename nil
1429 (with-file-property v localname
"file-selinux-context"
1430 (let ((context '(nil nil nil nil
))
1431 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1432 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1433 (when (and (tramp-remote-selinux-p v
)
1434 (tramp-send-command-and-check
1437 (tramp-get-ls-command v
)
1438 (tramp-shell-quote-argument localname
))))
1439 (with-current-buffer (tramp-get-connection-buffer v
)
1440 (goto-char (point-min))
1441 (when (re-search-forward regexp
(tramp-compat-line-end-position) t
)
1442 (setq context
(list (match-string 1) (match-string 2)
1443 (match-string 3) (match-string 4))))))
1444 ;; Return the context.
1447 (defun tramp-sh-handle-set-file-selinux-context (filename context
)
1448 "Like `set-file-selinux-context' for Tramp files."
1449 (with-parsed-tramp-file-name filename nil
1450 (if (and (consp context
)
1451 (tramp-remote-selinux-p v
)
1452 (tramp-send-command-and-check
1453 v
(format "chcon %s %s %s %s %s"
1454 (if (stringp (nth 0 context
))
1455 (format "--user=%s" (nth 0 context
)) "")
1456 (if (stringp (nth 1 context
))
1457 (format "--role=%s" (nth 1 context
)) "")
1458 (if (stringp (nth 2 context
))
1459 (format "--type=%s" (nth 2 context
)) "")
1460 (if (stringp (nth 3 context
))
1461 (format "--range=%s" (nth 3 context
)) "")
1462 (tramp-shell-quote-argument localname
))))
1463 (tramp-set-file-property v localname
"file-selinux-context" context
)
1464 (tramp-set-file-property v localname
"file-selinux-context" 'undef
)))
1465 ;; We always return nil.
1468 ;; Simple functions using the `test' command.
1470 (defun tramp-sh-handle-file-executable-p (filename)
1471 "Like `file-executable-p' for Tramp files."
1472 (with-parsed-tramp-file-name filename nil
1473 (with-file-property v localname
"file-executable-p"
1474 ;; Examine `file-attributes' cache to see if request can be
1475 ;; satisfied without remote operation.
1476 (or (tramp-check-cached-permissions v ?x
)
1477 (tramp-run-test "-x" filename
)))))
1479 (defun tramp-sh-handle-file-readable-p (filename)
1480 "Like `file-readable-p' for Tramp files."
1481 (with-parsed-tramp-file-name filename nil
1482 (with-file-property v localname
"file-readable-p"
1483 ;; Examine `file-attributes' cache to see if request can be
1484 ;; satisfied without remote operation.
1485 (or (tramp-check-cached-permissions v ?r
)
1486 (tramp-run-test "-r" filename
)))))
1488 ;; When the remote shell is started, it looks for a shell which groks
1489 ;; tilde expansion. Here, we assume that all shells which grok tilde
1490 ;; expansion will also provide a `test' command which groks `-nt' (for
1491 ;; newer than). If this breaks, tell me about it and I'll try to do
1492 ;; something smarter about it.
1493 (defun tramp-sh-handle-file-newer-than-file-p (file1 file2
)
1494 "Like `file-newer-than-file-p' for Tramp files."
1495 (cond ((not (file-exists-p file1
))
1497 ((not (file-exists-p file2
))
1499 ;; We are sure both files exist at this point.
1502 ;; We try to get the mtime of both files. If they are not
1503 ;; equal to the "dont-know" value, then we subtract the times
1504 ;; and obtain the result.
1505 (let ((fa1 (file-attributes file1
))
1506 (fa2 (file-attributes file2
)))
1507 (if (and (not (equal (nth 5 fa1
) '(0 0)))
1508 (not (equal (nth 5 fa2
) '(0 0))))
1509 (> 0 (tramp-time-diff (nth 5 fa2
) (nth 5 fa1
)))
1510 ;; If one of them is the dont-know value, then we can
1511 ;; still try to run a shell command on the remote host.
1512 ;; However, this only works if both files are Tramp
1513 ;; files and both have the same method, same user, same
1515 (unless (tramp-equal-remote file1 file2
)
1516 (with-parsed-tramp-file-name
1517 (if (tramp-tramp-file-p file1
) file1 file2
) nil
1520 "Files %s and %s must have same method, user, host"
1522 (with-parsed-tramp-file-name file1 nil
1524 (tramp-get-test-nt-command v
) file1 file2
))))))))
1526 ;; Functions implemented using the basic functions above.
1528 (defun tramp-sh-handle-file-directory-p (filename)
1529 "Like `file-directory-p' for Tramp files."
1530 ;; Care must be taken that this function returns `t' for symlinks
1531 ;; pointing to directories. Surely the most obvious implementation
1532 ;; would be `test -d', but that returns false for such symlinks.
1533 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
1534 ;; I now think he's right. So we could be using `test -d', couldn't
1537 ;; Alternatives: `cd %s', `test -d %s'
1538 (with-parsed-tramp-file-name filename nil
1539 (with-file-property v localname
"file-directory-p"
1540 (tramp-run-test "-d" filename
))))
1542 (defun tramp-sh-handle-file-writable-p (filename)
1543 "Like `file-writable-p' for Tramp files."
1544 (with-parsed-tramp-file-name filename nil
1545 (with-file-property v localname
"file-writable-p"
1546 (if (file-exists-p filename
)
1547 ;; Examine `file-attributes' cache to see if request can be
1548 ;; satisfied without remote operation.
1549 (or (tramp-check-cached-permissions v ?w
)
1550 (tramp-run-test "-w" filename
))
1551 ;; If file doesn't exist, check if directory is writable.
1552 (and (tramp-run-test "-d" (file-name-directory filename
))
1553 (tramp-run-test "-w" (file-name-directory filename
)))))))
1555 (defun tramp-sh-handle-file-ownership-preserved-p (filename)
1556 "Like `file-ownership-preserved-p' for Tramp files."
1557 (with-parsed-tramp-file-name filename nil
1558 (with-file-property v localname
"file-ownership-preserved-p"
1559 (let ((attributes (file-attributes filename
)))
1560 ;; Return t if the file doesn't exist, since it's true that no
1561 ;; information would be lost by an (attempted) delete and create.
1562 (or (null attributes
)
1563 (= (nth 2 attributes
) (tramp-get-remote-uid v
'integer
)))))))
1565 ;; Directory listings.
1567 (defun tramp-sh-handle-directory-files-and-attributes
1568 (directory &optional full match nosort id-format
)
1569 "Like `directory-files-and-attributes' for Tramp files."
1570 (unless id-format
(setq id-format
'integer
))
1571 (when (file-directory-p directory
)
1572 (setq directory
(expand-file-name directory
))
1575 (with-parsed-tramp-file-name directory nil
1578 (format "directory-files-and-attributes-%s" id-format
)
1583 (tramp-convert-file-attributes v
(cdr x
))))
1585 ((tramp-get-remote-stat v
)
1586 (tramp-do-directory-files-and-attributes-with-stat
1587 v localname id-format
))
1588 ((tramp-get-remote-perl v
)
1589 (tramp-do-directory-files-and-attributes-with-perl
1590 v localname id-format
)))))))))
1594 (setq item
(pop temp
))
1595 (when (or (null match
) (string-match match
(car item
)))
1597 (setcar item
(expand-file-name (car item
) directory
)))
1598 (push item result
)))
1602 (sort result
(lambda (x y
) (string< (car x
) (car y
))))))))
1604 (defun tramp-do-directory-files-and-attributes-with-perl
1605 (vec localname
&optional id-format
)
1606 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1607 (tramp-message vec
5 "directory-files-and-attributes with perl: %s" localname
)
1608 (tramp-maybe-send-script
1609 vec tramp-perl-directory-files-and-attributes
1610 "tramp_perl_directory_files_and_attributes")
1612 (tramp-send-command-and-read
1614 (format "tramp_perl_directory_files_and_attributes %s %s"
1615 (tramp-shell-quote-argument localname
) id-format
))))
1616 (when (stringp object
) (tramp-error vec
'file-error object
))
1619 (defun tramp-do-directory-files-and-attributes-with-stat
1620 (vec localname
&optional id-format
)
1621 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1622 (tramp-message vec
5 "directory-files-and-attributes with stat: %s" localname
)
1623 (tramp-send-command-and-read
1627 ;; We must care about filenames with spaces, or starting with
1628 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1629 ;; but it does not work on all remote systems. Therefore, we
1630 ;; quote the filenames via sed.
1631 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | xargs "
1632 "%s -c '(\"%%n\" (\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)'); "
1634 (tramp-shell-quote-argument localname
)
1635 (tramp-get-ls-command vec
)
1636 (tramp-get-remote-stat vec
)
1637 (if (eq id-format
'integer
) "%u" "\"%U\"")
1638 (if (eq id-format
'integer
) "%g" "\"%G\""))))
1640 ;; This function should return "foo/" for directories and "bar" for
1642 (defun tramp-sh-handle-file-name-all-completions (filename directory
)
1643 "Like `file-name-all-completions' for Tramp files."
1644 (unless (save-match-data (string-match "/" filename
))
1645 (with-parsed-tramp-file-name (expand-file-name directory
) nil
1652 ;; Try cache entries for filename, filename with last
1653 ;; character removed, filename with last two characters
1654 ;; removed, ..., and finally the empty string - all
1655 ;; concatenated to the local directory name.
1656 (let ((remote-file-name-inhibit-cache
1657 (or remote-file-name-inhibit-cache
1658 tramp-completion-reread-directory-timeout
)))
1660 ;; This is inefficient for very long filenames, pity
1661 ;; `reduce' is not available...
1668 (tramp-get-file-property
1670 (concat localname
(substring filename
0 x
))
1671 "file-name-all-completions"
1673 (when cache-hit
(list cache-hit
))))
1674 (tramp-compat-number-sequence (length filename
) 0 -
1)))))
1676 ;; Cache expired or no matching cache entry found so we need
1677 ;; to perform a remote operation.
1679 ;; Get a list of directories and files, including reliably
1680 ;; tagging the directories with a trailing '/'. Because I
1681 ;; rock. --daniel@danann.net
1683 ;; Changed to perform `cd' in the same remote op and only
1684 ;; get entries starting with `filename'. Capture any `cd'
1685 ;; error messages. Ensure any `cd' and `echo' aliases are
1689 (if (tramp-get-remote-perl v
)
1691 (tramp-maybe-send-script
1692 v tramp-perl-file-name-all-completions
1693 "tramp_perl_file_name_all_completions")
1694 (format "tramp_perl_file_name_all_completions %s %s %d"
1695 (tramp-shell-quote-argument localname
)
1696 (tramp-shell-quote-argument filename
)
1698 ;; `read-file-name-completion-ignore-case'
1699 ;; is introduced with Emacs 22.1.
1701 'read-file-name-completion-ignore-case
)
1702 'read-file-name-completion-ignore-case
1703 'completion-ignore-case
))
1707 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
1708 ;; `ls' with wildcard might fail with `Argument
1709 ;; list too long' error in some corner cases; if
1710 ;; `ls' fails after `cd' succeeded, chances are
1711 ;; that's the case, so let's retry without
1712 ;; wildcard. This will return "too many" entries
1713 ;; but that isn't harmful.
1714 " || %s -a 2>/dev/null)"
1715 " | while read f; do"
1716 " if %s -d \"$f\" 2>/dev/null;"
1717 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1718 " && \\echo ok) || \\echo fail")
1719 (tramp-shell-quote-argument localname
)
1720 (tramp-get-ls-command v
)
1721 ;; When `filename' is empty, just `ls' without
1722 ;; filename argument is more efficient than `ls *'
1723 ;; for very large directories and might avoid the
1724 ;; `Argument list too long' error.
1726 ;; With and only with wildcard, we need to add
1727 ;; `-d' to prevent `ls' from descending into
1729 (if (zerop (length filename
))
1731 (concat (tramp-shell-quote-argument filename
) "* -d"))
1732 (tramp-get-ls-command v
)
1733 (tramp-get-test-command v
))))
1735 ;; Now grab the output.
1736 (with-current-buffer (tramp-get-buffer v
)
1737 (goto-char (point-max))
1739 ;; Check result code, found in last line of output
1741 (if (looking-at "^fail$")
1743 ;; Grab error message from line before last line
1744 ;; (it was put there by `cd 2>&1')
1748 "tramp-sh-handle-file-name-all-completions: %s"
1750 (point) (tramp-compat-line-end-position))))
1751 ;; For peace of mind, if buffer doesn't end in `fail'
1752 ;; then it should end in `ok'. If neither are in the
1753 ;; buffer something went seriously wrong on the remote
1755 (unless (looking-at "^ok$")
1759 tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
1760 (tramp-shell-quote-argument localname
) (buffer-string))))
1762 (while (zerop (forward-line -
1))
1763 (push (buffer-substring
1764 (point) (tramp-compat-line-end-position))
1767 ;; Because the remote op went through OK we know the
1768 ;; directory we `cd'-ed to exists
1769 (tramp-set-file-property
1770 v localname
"file-exists-p" t
)
1772 ;; Because the remote op went through OK we know every
1773 ;; file listed by `ls' exists.
1774 (mapc (lambda (entry)
1775 (tramp-set-file-property
1776 v
(concat localname entry
) "file-exists-p" t
))
1779 ;; Store result in the cache
1780 (tramp-set-file-property
1781 v
(concat localname filename
)
1782 "file-name-all-completions"
1787 (defun tramp-sh-handle-add-name-to-file
1788 (filename newname
&optional ok-if-already-exists
)
1789 "Like `add-name-to-file' for Tramp files."
1790 (unless (tramp-equal-remote filename newname
)
1791 (with-parsed-tramp-file-name
1792 (if (tramp-tramp-file-p filename
) filename newname
) nil
1795 "add-name-to-file: %s"
1796 "only implemented for same method, same user, same host")))
1797 (with-parsed-tramp-file-name filename v1
1798 (with-parsed-tramp-file-name newname v2
1799 (let ((ln (when v1
(tramp-get-remote-ln v1
))))
1800 (when (and (not ok-if-already-exists
)
1801 (file-exists-p newname
)
1802 (not (numberp ok-if-already-exists
))
1805 "File %s already exists; make it a new name anyway? "
1809 "add-name-to-file: file %s already exists" newname
))
1810 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
1811 (tramp-flush-file-property v2 v2-localname
)
1812 (tramp-barf-unless-okay
1814 (format "%s %s %s" ln
(tramp-shell-quote-argument v1-localname
)
1815 (tramp-shell-quote-argument v2-localname
))
1816 "error with add-name-to-file, see buffer `%s' for details"
1819 (defun tramp-sh-handle-copy-file
1820 (filename newname
&optional ok-if-already-exists keep-date
1821 preserve-uid-gid preserve-selinux-context
)
1822 "Like `copy-file' for Tramp files."
1823 (setq filename
(expand-file-name filename
))
1824 (setq newname
(expand-file-name newname
))
1826 ;; At least one file a Tramp file?
1827 ((or (tramp-tramp-file-p filename
)
1828 (tramp-tramp-file-p newname
))
1829 (tramp-do-copy-or-rename-file
1830 'copy filename newname ok-if-already-exists keep-date
1831 preserve-uid-gid preserve-selinux-context
))
1833 (preserve-selinux-context
1834 (tramp-run-real-handler
1836 (list filename newname ok-if-already-exists keep-date
1837 preserve-uid-gid preserve-selinux-context
)))
1839 (tramp-run-real-handler
1841 (list filename newname ok-if-already-exists keep-date preserve-uid-gid
)))
1843 (tramp-run-real-handler
1844 'copy-file
(list filename newname ok-if-already-exists keep-date
)))))
1846 (defun tramp-sh-handle-copy-directory
1847 (dirname newname
&optional keep-date parents
)
1848 "Like `copy-directory' for Tramp files."
1849 (let ((t1 (tramp-tramp-file-p dirname
))
1850 (t2 (tramp-tramp-file-p newname
)))
1851 (with-parsed-tramp-file-name (if t1 dirname newname
) nil
1852 (if (and (tramp-get-method-parameter method
'tramp-copy-recursive
)
1853 ;; When DIRNAME and NEWNAME are remote, they must have
1855 (or (null t1
) (null t2
)
1857 (tramp-file-name-method (tramp-dissect-file-name dirname
))
1858 (tramp-file-name-method (tramp-dissect-file-name newname
)))))
1859 ;; scp or rsync DTRT.
1861 (setq dirname
(directory-file-name (expand-file-name dirname
))
1862 newname
(directory-file-name (expand-file-name newname
)))
1863 (if (and (file-directory-p newname
)
1864 (not (string-equal (file-name-nondirectory dirname
)
1865 (file-name-nondirectory newname
))))
1868 (file-name-nondirectory dirname
) newname
)))
1869 (if (not (file-directory-p (file-name-directory newname
)))
1870 (make-directory (file-name-directory newname
) parents
))
1871 (tramp-do-copy-or-rename-file-out-of-band
1872 'copy dirname newname keep-date
))
1873 ;; We must do it file-wise.
1874 (tramp-run-real-handler
1875 'copy-directory
(list dirname newname keep-date parents
)))
1877 ;; When newname did exist, we have wrong cached values.
1879 (with-parsed-tramp-file-name newname nil
1880 (tramp-flush-file-property v
(file-name-directory localname
))
1881 (tramp-flush-file-property v localname
))))))
1883 (defun tramp-sh-handle-rename-file
1884 (filename newname
&optional ok-if-already-exists
)
1885 "Like `rename-file' for Tramp files."
1886 ;; Check if both files are local -- invoke normal rename-file.
1887 ;; Otherwise, use Tramp from local system.
1888 (setq filename
(expand-file-name filename
))
1889 (setq newname
(expand-file-name newname
))
1890 ;; At least one file a Tramp file?
1891 (if (or (tramp-tramp-file-p filename
)
1892 (tramp-tramp-file-p newname
))
1893 (tramp-do-copy-or-rename-file
1894 'rename filename newname ok-if-already-exists t t
)
1895 (tramp-run-real-handler
1896 'rename-file
(list filename newname ok-if-already-exists
))))
1898 (defun tramp-do-copy-or-rename-file
1899 (op filename newname
&optional ok-if-already-exists keep-date
1900 preserve-uid-gid preserve-selinux-context
)
1901 "Copy or rename a remote file.
1902 OP must be `copy' or `rename' and indicates the operation to perform.
1903 FILENAME specifies the file to copy or rename, NEWNAME is the name of
1904 the new file (for copy) or the new name of the file (for rename).
1905 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
1906 KEEP-DATE means to make sure that NEWNAME has the same timestamp
1907 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
1908 the uid and gid if both files are on the same host.
1909 PRESERVE-SELINUX-CONTEXT activates selinux commands.
1911 This function is invoked by `tramp-sh-handle-copy-file' and
1912 `tramp-sh-handle-rename-file'. It is an error if OP is neither
1913 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
1915 (unless (memq op
'(copy rename
))
1916 (error "Unknown operation `%s', must be `copy' or `rename'" op
))
1917 (let ((t1 (tramp-tramp-file-p filename
))
1918 (t2 (tramp-tramp-file-p newname
))
1919 (context (and preserve-selinux-context
1920 (apply 'file-selinux-context
(list filename
))))
1923 (with-parsed-tramp-file-name (if t1 filename newname
) nil
1924 (when (and (not ok-if-already-exists
) (file-exists-p newname
))
1926 v
'file-already-exists
"File %s already exists" newname
))
1928 (with-progress-reporter
1929 v
0 (format "%s %s to %s"
1930 (if (eq op
'copy
) "Copying" "Renaming")
1934 ;; Both are Tramp files.
1936 (with-parsed-tramp-file-name filename v1
1937 (with-parsed-tramp-file-name newname v2
1939 ;; Shortcut: if method, host, user are the same for
1940 ;; both files, we invoke `cp' or `mv' on the remote
1942 ((tramp-equal-remote filename newname
)
1943 (tramp-do-copy-or-rename-file-directly
1945 ok-if-already-exists keep-date preserve-uid-gid
))
1947 ;; Try out-of-band operation.
1948 ((tramp-method-out-of-band-p
1949 v1
(nth 7 (file-attributes filename
)))
1950 (tramp-do-copy-or-rename-file-out-of-band
1951 op filename newname keep-date
))
1953 ;; No shortcut was possible. So we copy the file
1954 ;; first. If the operation was `rename', we go back
1955 ;; and delete the original file (if the copy was
1956 ;; successful). The approach is simple-minded: we
1957 ;; create a new buffer, insert the contents of the
1958 ;; source file into it, then write out the buffer to
1959 ;; the target file. The advantage is that it doesn't
1960 ;; matter which filename handlers are used for the
1961 ;; source and target file.
1963 (tramp-do-copy-or-rename-file-via-buffer
1964 op filename newname keep-date
))))))
1966 ;; One file is a Tramp file, the other one is local.
1969 ;; Fast track on local machine.
1970 ((tramp-local-host-p v
)
1971 (tramp-do-copy-or-rename-file-directly
1973 ok-if-already-exists keep-date preserve-uid-gid
))
1975 ;; If the Tramp file has an out-of-band method, the
1976 ;; corresponding copy-program can be invoked.
1977 ((tramp-method-out-of-band-p v
(nth 7 (file-attributes filename
)))
1978 (tramp-do-copy-or-rename-file-out-of-band
1979 op filename newname keep-date
))
1981 ;; Use the inline method via a Tramp buffer.
1982 (t (tramp-do-copy-or-rename-file-via-buffer
1983 op filename newname keep-date
))))
1986 ;; One of them must be a Tramp file.
1987 (error "Tramp implementation says this cannot happen")))
1989 ;; Handle `preserve-selinux-context'.
1990 (when context
(apply 'set-file-selinux-context
(list newname context
)))
1992 ;; In case of `rename', we must flush the cache of the source file.
1993 (when (and t1
(eq op
'rename
))
1994 (with-parsed-tramp-file-name filename v1
1995 (tramp-flush-file-property v1
(file-name-directory localname
))
1996 (tramp-flush-file-property v1 localname
)))
1998 ;; When newname did exist, we have wrong cached values.
2000 (with-parsed-tramp-file-name newname v2
2001 (tramp-flush-file-property v2
(file-name-directory localname
))
2002 (tramp-flush-file-property v2 localname
)))))))
2004 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date
)
2005 "Use an Emacs buffer to copy or rename a file.
2006 First arg OP is either `copy' or `rename' and indicates the operation.
2007 FILENAME is the source file, NEWNAME the target file.
2008 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2010 ;; We must disable multibyte, because binary data shall not be
2012 (set-buffer-multibyte nil
)
2013 (let ((coding-system-for-read 'binary
)
2014 (jka-compr-inhibit t
))
2015 (insert-file-contents-literally filename
))
2016 ;; We don't want the target file to be compressed, so we let-bind
2017 ;; `jka-compr-inhibit' to t.
2018 (let ((coding-system-for-write 'binary
)
2019 (jka-compr-inhibit t
))
2020 (write-region (point-min) (point-max) newname
)))
2021 ;; KEEP-DATE handling.
2022 (when keep-date
(set-file-times newname
(nth 5 (file-attributes filename
))))
2024 (set-file-modes newname
(tramp-default-file-modes filename
))
2025 ;; If the operation was `rename', delete the original file.
2026 (unless (eq op
'copy
) (delete-file filename
)))
2028 (defun tramp-do-copy-or-rename-file-directly
2029 (op filename newname ok-if-already-exists keep-date preserve-uid-gid
)
2030 "Invokes `cp' or `mv' on the remote system.
2031 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2032 respectively. FILENAME specifies the file to copy or rename,
2033 NEWNAME is the name of the new file (for copy) or the new name of
2034 the file (for rename). Both files must reside on the same host.
2035 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2036 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2037 the uid and gid from FILENAME."
2038 (let ((t1 (tramp-tramp-file-p filename
))
2039 (t2 (tramp-tramp-file-p newname
))
2040 (file-times (nth 5 (file-attributes filename
)))
2041 (file-modes (tramp-default-file-modes filename
)))
2042 (with-parsed-tramp-file-name (if t1 filename newname
) nil
2043 (let* ((cmd (cond ((and (eq op
'copy
) preserve-uid-gid
) "cp -f -p")
2044 ((eq op
'copy
) "cp -f")
2045 ((eq op
'rename
) "mv -f")
2048 "Unknown operation `%s', must be `copy' or `rename'"
2052 (tramp-file-name-handler 'file-remote-p filename
'localname
)
2056 (tramp-file-name-handler 'file-remote-p newname
'localname
)
2058 (prefix (file-remote-p (if t1 filename newname
)))
2062 ;; Both files are on a remote host, with same user.
2065 (tramp-send-command-and-check
2066 v
(format "%s %s %s" cmd
2067 (tramp-shell-quote-argument localname1
)
2068 (tramp-shell-quote-argument localname2
))))
2069 (with-current-buffer (tramp-get-buffer v
)
2070 (goto-char (point-min))
2074 ;; Mask cp -f error.
2076 tramp-operation-not-permitted-regexp nil t
))
2078 (tramp-error-with-buffer
2080 "Copying directly failed, see buffer `%s' for details."
2083 ;; We are on the local host.
2086 ;; We can do it directly.
2087 ((let (file-name-handler-alist)
2088 (and (file-readable-p localname1
)
2089 (file-writable-p (file-name-directory localname2
))
2090 (or (file-directory-p localname2
)
2091 (file-writable-p localname2
))))
2093 (tramp-compat-copy-file
2094 localname1 localname2 ok-if-already-exists
2095 keep-date preserve-uid-gid
)
2096 (tramp-run-real-handler
2097 'rename-file
(list localname1 localname2 ok-if-already-exists
))))
2099 ;; We can do it directly with `tramp-send-command'
2100 ((and (file-readable-p (concat prefix localname1
))
2102 (file-name-directory (concat prefix localname2
)))
2103 (or (file-directory-p (concat prefix localname2
))
2104 (file-writable-p (concat prefix localname2
))))
2105 (tramp-do-copy-or-rename-file-directly
2106 op
(concat prefix localname1
) (concat prefix localname2
)
2107 ok-if-already-exists keep-date t
)
2108 ;; We must change the ownership to the local user.
2109 (tramp-set-file-uid-gid
2110 (concat prefix localname2
)
2111 (tramp-get-local-uid 'integer
)
2112 (tramp-get-local-gid 'integer
)))
2114 ;; We need a temporary file in between.
2116 ;; Create the temporary file.
2117 (let ((tmpfile (tramp-compat-make-temp-file localname1
)))
2122 (tramp-barf-unless-okay
2125 (tramp-shell-quote-argument localname1
)
2126 (tramp-shell-quote-argument tmpfile
))
2127 "Copying directly failed, see buffer `%s' for details."
2128 (tramp-get-buffer v
))
2129 ;; We must change the ownership as remote user.
2130 ;; Since this does not work reliable, we also
2131 ;; give read permissions.
2133 (concat prefix tmpfile
)
2134 (tramp-compat-octal-to-decimal "0777"))
2135 (tramp-set-file-uid-gid
2136 (concat prefix tmpfile
)
2137 (tramp-get-local-uid 'integer
)
2138 (tramp-get-local-gid 'integer
)))
2141 (tramp-compat-copy-file
2142 localname1 tmpfile t
2143 keep-date preserve-uid-gid
)
2144 (tramp-run-real-handler
2146 (list localname1 tmpfile t
)))
2147 ;; We must change the ownership as local user.
2148 ;; Since this does not work reliable, we also
2149 ;; give read permissions.
2151 tmpfile
(tramp-compat-octal-to-decimal "0777"))
2152 (tramp-set-file-uid-gid
2154 (tramp-get-remote-uid v
'integer
)
2155 (tramp-get-remote-gid v
'integer
))))
2157 ;; Move the temporary file to its destination.
2160 (tramp-barf-unless-okay
2163 (tramp-shell-quote-argument tmpfile
)
2164 (tramp-shell-quote-argument localname2
))
2165 "Copying directly failed, see buffer `%s' for details."
2166 (tramp-get-buffer v
)))
2168 (tramp-run-real-handler
2170 (list tmpfile localname2 ok-if-already-exists
)))))
2174 (delete-file tmpfile
)
2177 ;; Set the time and mode. Mask possible errors.
2180 (set-file-times newname file-times
)
2181 (set-file-modes newname file-modes
))
2184 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date
)
2185 "Invoke rcp program to copy.
2186 The method used must be an out-of-band method."
2187 (let ((t1 (tramp-tramp-file-p filename
))
2188 (t2 (tramp-tramp-file-p newname
))
2189 copy-program copy-args copy-env copy-keep-date port spec
2192 (with-parsed-tramp-file-name (if t1 filename newname
) nil
2195 ;; Both are Tramp files. We shall optimize it, when the
2196 ;; methods for filename and newname are the same.
2197 (let* ((dir-flag (file-directory-p filename
))
2198 (tmpfile (tramp-compat-make-temp-file localname dir-flag
)))
2202 (file-name-nondirectory newname
) tmpfile
)))
2205 (tramp-do-copy-or-rename-file-out-of-band
2206 op filename tmpfile keep-date
)
2207 (tramp-do-copy-or-rename-file-out-of-band
2208 'rename tmpfile newname keep-date
))
2212 (tramp-compat-delete-directory
2213 (expand-file-name ".." tmpfile
) 'recursive
)
2214 (delete-file tmpfile
))
2217 ;; Expand hops. Might be necessary for gateway methods.
2218 (setq v
(car (tramp-compute-multi-hops v
)))
2219 (aset v
3 localname
)
2221 ;; Check which ones of source and target are Tramp files.
2222 (setq source
(if t1
(tramp-make-copy-program-file-name v
) filename
)
2224 (if (and (file-directory-p filename
)
2226 (file-name-nondirectory filename
)
2227 (file-name-nondirectory newname
)))
2228 'file-name-directory
2230 (if t2
(tramp-make-copy-program-file-name v
) newname
)))
2232 ;; Check for port number. Until now, there's no need for handling
2233 ;; like method, user, host.
2234 (setq host
(tramp-file-name-real-host v
)
2235 port
(tramp-file-name-port v
)
2236 port
(or (and port
(number-to-string port
)) ""))
2238 ;; Compose copy command.
2239 (setq spec
(format-spec-make
2240 ?h host ?u user ?p port
2241 ?t
(tramp-get-connection-property
2242 (tramp-get-connection-process v
) "temp-file" "")
2243 ?k
(if keep-date
" " ""))
2244 copy-program
(tramp-get-method-parameter
2245 method
'tramp-copy-program
)
2246 copy-keep-date
(tramp-get-method-parameter
2247 method
'tramp-copy-keep-date
)
2255 ;; " " is indication for keep-date argument.
2256 (delete " " (mapcar (lambda (y) (format-spec y spec
)) x
)))
2257 (unless (member "" x
) (mapconcat 'identity x
" ")))
2258 (tramp-get-method-parameter method
'tramp-copy-args
)))
2264 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
2265 (unless (member "" x
) (mapconcat 'identity x
" ")))
2266 (tramp-get-method-parameter method
'tramp-copy-env
))))
2268 ;; Check for program.
2269 (when (and (fboundp 'executable-find
)
2270 (not (let ((default-directory
2271 (tramp-compat-temporary-file-directory)))
2272 (executable-find copy-program
))))
2274 v
'file-error
"Cannot find copy program: %s" copy-program
))
2276 ;; Set variables for computing the prompt for reading
2278 (setq tramp-current-method
(tramp-file-name-method v
)
2279 tramp-current-user
(tramp-file-name-user v
)
2280 tramp-current-host
(tramp-file-name-host v
))
2284 ;; The default directory must be remote.
2285 (let ((default-directory
2286 (file-name-directory (if t1 filename newname
)))
2287 (process-environment (copy-sequence process-environment
)))
2288 ;; Set the transfer process properties.
2289 (tramp-set-connection-property
2290 v
"process-name" (buffer-name (current-buffer)))
2291 (tramp-set-connection-property
2292 v
"process-buffer" (current-buffer))
2294 (tramp-message v
5 "%s=\"%s\"" (car copy-env
) (cadr copy-env
))
2295 (setenv (pop copy-env
) (pop copy-env
)))
2297 ;; Use an asynchronous process. By this, password can
2298 ;; be handled. The default directory must be local, in
2299 ;; order to apply the correct `copy-program'. We don't
2300 ;; set a timeout, because the copying of large files can
2301 ;; last longer than 60 secs.
2302 (let ((p (let ((default-directory
2303 (tramp-compat-temporary-file-directory)))
2304 (apply 'start-process
2305 (tramp-get-connection-property
2306 v
"process-name" nil
)
2307 (tramp-get-connection-property
2308 v
"process-buffer" nil
)
2310 (append copy-args
(list source target
))))))
2312 v
6 "%s" (mapconcat 'identity
(process-command p
) " "))
2313 (tramp-compat-set-process-query-on-exit-flag p nil
)
2314 (tramp-process-actions p v tramp-actions-copy-out-of-band
))))
2316 ;; Reset the transfer process properties.
2317 (tramp-set-connection-property v
"process-name" nil
)
2318 (tramp-set-connection-property v
"process-buffer" nil
))
2320 ;; Handle KEEP-DATE argument.
2321 (when (and keep-date
(not copy-keep-date
))
2322 (set-file-times newname
(nth 5 (file-attributes filename
))))
2325 (unless (and keep-date copy-keep-date
)
2327 (set-file-modes newname
(tramp-default-file-modes filename
)))))
2329 ;; If the operation was `rename', delete the original file.
2330 (unless (eq op
'copy
)
2331 (if (file-regular-p filename
)
2332 (delete-file filename
)
2333 (tramp-compat-delete-directory filename
'recursive
))))))
2335 (defun tramp-sh-handle-make-directory (dir &optional parents
)
2336 "Like `make-directory' for Tramp files."
2337 (setq dir
(expand-file-name dir
))
2338 (with-parsed-tramp-file-name dir nil
2339 (tramp-flush-directory-property v
(file-name-directory localname
))
2341 (tramp-barf-unless-okay
2343 (if parents
"mkdir -p" "mkdir")
2344 (tramp-shell-quote-argument localname
))
2345 "Couldn't make directory %s" dir
))))
2347 (defun tramp-sh-handle-delete-directory (directory &optional recursive
)
2348 "Like `delete-directory' for Tramp files."
2349 (setq directory
(expand-file-name directory
))
2350 (with-parsed-tramp-file-name directory nil
2351 (tramp-flush-file-property v
(file-name-directory localname
))
2352 (tramp-flush-directory-property v localname
)
2353 (tramp-barf-unless-okay
2355 (if recursive
"rm -rf" "rmdir")
2356 (tramp-shell-quote-argument localname
))
2357 "Couldn't delete %s" directory
)))
2359 (defun tramp-sh-handle-delete-file (filename &optional trash
)
2360 "Like `delete-file' for Tramp files."
2361 (setq filename
(expand-file-name filename
))
2362 (with-parsed-tramp-file-name filename nil
2363 (tramp-flush-file-property v
(file-name-directory localname
))
2364 (tramp-flush-file-property v localname
)
2365 (tramp-barf-unless-okay
2367 (or (and trash
(tramp-get-remote-trash v
)) "rm -f")
2368 (tramp-shell-quote-argument localname
))
2369 "Couldn't delete %s" filename
)))
2373 ;; CCC: This does not seem to be enough. Something dies when
2374 ;; we try and delete two directories under Tramp :/
2375 (defun tramp-sh-handle-dired-recursive-delete-directory (filename)
2376 "Recursively delete the directory given.
2377 This is like `dired-recursive-delete-directory' for Tramp files."
2378 (with-parsed-tramp-file-name filename nil
2379 ;; Run a shell command 'rm -r <localname>'
2380 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
2381 (unless (file-exists-p filename
)
2382 (tramp-error v
'file-error
"No such directory: %s" filename
))
2383 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
2386 (format "rm -rf %s" (tramp-shell-quote-argument localname
))
2387 ;; Don't read the output, do it explicitely.
2389 ;; Wait for the remote system to return to us...
2390 ;; This might take a while, allow it plenty of time.
2391 (tramp-wait-for-output (tramp-get-connection-process v
) 120)
2392 ;; Make sure that it worked...
2393 (tramp-flush-file-property v
(file-name-directory localname
))
2394 (tramp-flush-directory-property v localname
)
2395 (and (file-exists-p filename
)
2397 v
'file-error
"Failed to recursively delete %s" filename
))))
2399 (defun tramp-sh-handle-dired-compress-file (file &rest ok-flag
)
2400 "Like `dired-compress-file' for Tramp files."
2401 ;; OK-FLAG is valid for XEmacs only, but not implemented.
2402 ;; Code stolen mainly from dired-aux.el.
2403 (with-parsed-tramp-file-name file nil
2404 (tramp-flush-file-property v localname
)
2407 (if (not (featurep 'xemacs
))
2409 (symbol-value 'dired-compress-file-suffixes
)
2410 ;; XEmacs has `dired-compression-method-alist', which is
2411 ;; transformed into `dired-compress-file-suffixes' structure.
2414 (list (concat (regexp-quote (nth 1 x
)) "\\'")
2416 (mapconcat 'identity
(nth 3 x
) " ")))
2417 (symbol-value 'dired-compression-method-alist
))))
2419 ;; See if any suffix rule matches this file name.
2421 (let (case-fold-search)
2422 (if (string-match (car (car suffixes
)) localname
)
2423 (setq suffix
(car suffixes
) suffixes nil
))
2424 (setq suffixes
(cdr suffixes
))))
2426 (cond ((file-symlink-p file
)
2428 ((and suffix
(nth 2 suffix
))
2429 ;; We found an uncompression rule.
2430 (with-progress-reporter v
0 (format "Uncompressing %s" file
)
2431 (when (tramp-send-command-and-check
2432 v
(concat (nth 2 suffix
) " "
2433 (tramp-shell-quote-argument localname
)))
2434 ;; `dired-remove-file' is not defined in XEmacs.
2435 (tramp-compat-funcall 'dired-remove-file file
)
2436 (string-match (car suffix
) file
)
2437 (concat (substring file
0 (match-beginning 0))))))
2439 ;; We don't recognize the file as compressed, so compress it.
2441 (with-progress-reporter v
0 (format "Compressing %s" file
)
2442 (when (tramp-send-command-and-check
2443 v
(concat "gzip -f "
2444 (tramp-shell-quote-argument localname
)))
2445 ;; `dired-remove-file' is not defined in XEmacs.
2446 (tramp-compat-funcall 'dired-remove-file file
)
2447 (cond ((file-exists-p (concat file
".gz"))
2448 (concat file
".gz"))
2449 ((file-exists-p (concat file
".z"))
2453 (defun tramp-sh-handle-insert-directory
2454 (filename switches
&optional wildcard full-directory-p
)
2455 "Like `insert-directory' for Tramp files."
2456 (setq filename
(expand-file-name filename
))
2457 (with-parsed-tramp-file-name filename nil
2458 (if (and (featurep 'ls-lisp
)
2459 (not (symbol-value 'ls-lisp-use-insert-directory-program
)))
2460 (tramp-run-real-handler
2461 'insert-directory
(list filename switches wildcard full-directory-p
))
2462 (when (stringp switches
)
2463 (setq switches
(split-string switches
)))
2464 (when (and (member "--dired" switches
)
2465 (not (tramp-get-ls-command-with-dired v
)))
2466 (setq switches
(delete "--dired" switches
)))
2468 (setq wildcard
(tramp-run-real-handler
2469 'file-name-nondirectory
(list localname
)))
2470 (setq localname
(tramp-run-real-handler
2471 'file-name-directory
(list localname
))))
2472 (unless full-directory-p
2473 (setq switches
(add-to-list 'switches
"-d" 'append
)))
2474 (setq switches
(mapconcat 'tramp-shell-quote-argument switches
" "))
2476 (setq switches
(concat switches
" " wildcard
)))
2478 v
4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2479 switches filename
(if wildcard
"yes" "no")
2480 (if full-directory-p
"yes" "no"))
2481 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2482 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2483 (if full-directory-p
2486 (format "%s %s %s 2>/dev/null"
2487 (tramp-get-ls-command v
)
2491 (tramp-shell-quote-argument (concat localname
".")))))
2492 (tramp-barf-unless-okay
2494 (format "cd %s" (tramp-shell-quote-argument
2495 (tramp-run-real-handler
2496 'file-name-directory
(list localname
))))
2498 (tramp-shell-quote-argument
2499 (tramp-run-real-handler 'file-name-directory
(list localname
))))
2503 (tramp-get-ls-command v
)
2507 (tramp-run-real-handler
2508 'file-name-nondirectory
(list localname
)))))
2510 (tramp-shell-quote-argument
2511 (tramp-run-real-handler
2512 'file-name-nondirectory
(list localname
)))))))
2513 (let ((beg (point)))
2514 ;; We cannot use `insert-buffer-substring' because the Tramp
2515 ;; buffer changes its contents before insertion due to calling
2516 ;; `expand-file' and alike.
2518 (with-current-buffer (tramp-get-buffer v
)
2521 ;; Check for "--dired" output.
2523 (when (looking-at "//SUBDIRED//")
2525 (when (looking-at "//DIRED//\\s-+")
2526 (let ((databeg (match-end 0))
2527 (end (tramp-compat-line-end-position)))
2528 ;; Now read the numeric positions of file names.
2530 (while (< (point) end
)
2531 (let ((start (+ beg
(read (current-buffer))))
2532 (end (+ beg
(read (current-buffer)))))
2533 (if (memq (char-after end
) '(?
\n ?\
))
2534 ;; End is followed by \n or by " -> ".
2535 (put-text-property start end
'dired-filename t
))))))
2536 ;; Remove trailing lines.
2537 (goto-char (tramp-compat-line-beginning-position))
2538 (while (looking-at "//")
2540 (delete-region (match-beginning 0) (point)))
2542 ;; The inserted file could be from somewhere else.
2543 (when (and (not wildcard
) (not full-directory-p
))
2544 (goto-char (point-max))
2545 (when (file-symlink-p filename
)
2546 (goto-char (search-backward "->" beg
'noerror
)))
2548 (if (zerop (length (file-name-nondirectory filename
)))
2550 (file-name-nondirectory filename
))
2552 (replace-match (file-relative-name filename
) t
))
2554 (goto-char (point-max))))))
2556 ;; Canonicalization of file names.
2558 (defun tramp-sh-handle-expand-file-name (name &optional dir
)
2559 "Like `expand-file-name' for Tramp files.
2560 If the localname part of the given filename starts with \"/../\" then
2561 the result will be a local, non-Tramp, filename."
2562 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
2563 (setq dir
(or dir default-directory
"/"))
2564 ;; Unless NAME is absolute, concat DIR and NAME.
2565 (unless (file-name-absolute-p name
)
2566 (setq name
(concat (file-name-as-directory dir
) name
)))
2567 ;; If NAME is not a Tramp file, run the real handler.
2568 (if (not (tramp-connectable-p name
))
2569 (tramp-run-real-handler 'expand-file-name
(list name nil
))
2571 (with-parsed-tramp-file-name name nil
2572 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
2573 (setq localname
(concat "~/" localname
)))
2574 ;; Tilde expansion if necessary. This needs a shell which
2575 ;; groks tilde expansion! The function `tramp-find-shell' is
2576 ;; supposed to find such a shell on the remote host. Please
2577 ;; tell me about it when this doesn't work on your system.
2578 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname
)
2579 (let ((uname (match-string 1 localname
))
2580 (fname (match-string 2 localname
)))
2581 ;; We cannot simply apply "~/", because under sudo "~/" is
2582 ;; expanded to the local user home directory but to the
2583 ;; root home directory. On the other hand, using always
2584 ;; the default user name for tilde expansion is not
2585 ;; appropriate either, because ssh and companions might
2586 ;; use a user name from the config file.
2587 (when (and (string-equal uname
"~")
2588 (string-match "\\`su\\(do\\)?\\'" method
))
2589 (setq uname
(concat uname user
)))
2591 (with-connection-property v uname
2593 v
(format "cd %s; pwd" (tramp-shell-quote-argument uname
)))
2594 (with-current-buffer (tramp-get-buffer v
)
2595 (goto-char (point-min))
2597 (point) (tramp-compat-line-end-position)))))
2598 (setq localname
(concat uname fname
))))
2599 ;; There might be a double slash, for example when "~/"
2600 ;; expands to "/". Remove this.
2601 (while (string-match "//" localname
)
2602 (setq localname
(replace-match "/" t t localname
)))
2603 ;; No tilde characters in file name, do normal
2604 ;; `expand-file-name' (this does "/./" and "/../"). We bind
2605 ;; `directory-sep-char' here for XEmacs on Windows, which would
2606 ;; otherwise use backslash. `default-directory' is bound,
2607 ;; because on Windows there would be problems with UNC shares or
2609 (let ((directory-sep-char ?
/)
2610 (default-directory (tramp-compat-temporary-file-directory)))
2611 (tramp-make-tramp-file-name
2613 (tramp-drop-volume-letter
2614 (tramp-run-real-handler
2615 'expand-file-name
(list localname
))))))))
2617 ;;; Remote commands:
2619 (defun tramp-sh-handle-executable-find (command)
2620 "Like `executable-find' for Tramp files."
2621 (with-parsed-tramp-file-name default-directory nil
2622 (tramp-find-executable v command
(tramp-get-remote-path v
) t
)))
2624 (defun tramp-process-sentinel (proc event
)
2625 "Flush file caches."
2626 (unless (memq (process-status proc
) '(run open
))
2627 (let ((vec (tramp-get-connection-property proc
"vector" nil
)))
2629 (tramp-message vec
5 "Sentinel called: `%s' `%s'" proc event
)
2630 (tramp-flush-directory-property vec
"")))))
2632 ;; We use BUFFER also as connection buffer during setup. Because of
2633 ;; this, its original contents must be saved, and restored once
2634 ;; connection has been setup.
2635 (defun tramp-sh-handle-start-file-process (name buffer program
&rest args
)
2636 "Like `start-file-process' for Tramp files."
2637 (with-parsed-tramp-file-name default-directory nil
2639 ;; When PROGRAM is nil, we just provide a tty.
2641 (when (stringp program
)
2642 (format "cd %s; exec %s"
2643 (tramp-shell-quote-argument localname
)
2644 (mapconcat 'tramp-shell-quote-argument
2645 (cons program args
) " "))))
2646 (tramp-process-connection-type
2647 (or (null program
) tramp-process-connection-type
))
2651 ;; BUFFER can be nil. We use a temporary buffer.
2652 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
2653 (while (get-process name1
)
2654 ;; NAME must be unique as process name.
2656 name1
(format "%s<%d>" name i
)))
2658 ;; Set the new process properties.
2659 (tramp-set-connection-property v
"process-name" name
)
2660 (tramp-set-connection-property v
"process-buffer" buffer
)
2661 ;; Activate narrowing in order to save BUFFER contents.
2662 ;; Clear also the modification time; otherwise we might be
2663 ;; interrupted by `verify-visited-file-modtime'.
2664 (with-current-buffer (tramp-get-connection-buffer v
)
2665 (clear-visited-file-modtime)
2666 (narrow-to-region (point-max) (point-max)))
2668 ;; Send the command.
2669 (tramp-send-command v command nil t
) ; nooutput
2670 ;; Check, whether a pty is associated.
2671 (tramp-maybe-open-connection v
)
2672 (unless (tramp-compat-process-get
2673 (tramp-get-connection-process v
) 'remote-tty
)
2675 v
'file-error
"pty association is not supported for `%s'" name
)))
2676 (let ((p (tramp-get-connection-process v
)))
2677 ;; Set sentinel and query flag for this process.
2678 (tramp-set-connection-property p
"vector" v
)
2679 (set-process-sentinel p
'tramp-process-sentinel
)
2680 (tramp-compat-set-process-query-on-exit-flag p t
)
2684 (with-current-buffer (tramp-get-connection-buffer v
)
2685 (if (string-match tramp-temp-buffer-name
(buffer-name))
2687 (set-process-buffer (tramp-get-connection-process v
) nil
)
2688 (kill-buffer (current-buffer)))
2690 (goto-char (point-max))))
2691 (tramp-set-connection-property v
"process-name" nil
)
2692 (tramp-set-connection-property v
"process-buffer" nil
))))
2694 (defun tramp-sh-handle-process-file
2695 (program &optional infile destination display
&rest args
)
2696 "Like `process-file' for Tramp files."
2697 ;; The implementation is not complete yet.
2698 (when (and (numberp destination
) (zerop destination
))
2699 (error "Implementation does not handle immediate return"))
2701 (with-parsed-tramp-file-name default-directory nil
2702 (let (command input tmpinput stderr tmpstderr outbuf ret
)
2704 (setq command
(mapconcat 'tramp-shell-quote-argument
2705 (cons program args
) " "))
2708 (setq input
"/dev/null")
2709 (setq infile
(expand-file-name infile
))
2710 (if (tramp-equal-remote default-directory infile
)
2711 ;; INFILE is on the same remote host.
2712 (setq input
(with-parsed-tramp-file-name infile nil localname
))
2713 ;; INFILE must be copied to remote host.
2714 (setq input
(tramp-make-tramp-temp-file v
)
2715 tmpinput
(tramp-make-tramp-file-name method user host input
))
2716 (copy-file infile tmpinput t
)))
2717 (when input
(setq command
(format "%s <%s" command input
)))
2719 ;; Determine output.
2722 ((bufferp destination
)
2723 (setq outbuf destination
))
2725 ((stringp destination
)
2726 (setq outbuf
(get-buffer-create destination
)))
2727 ;; (REAL-DESTINATION ERROR-DESTINATION)
2728 ((consp destination
)
2731 ((bufferp (car destination
))
2732 (setq outbuf
(car destination
)))
2733 ((stringp (car destination
))
2734 (setq outbuf
(get-buffer-create (car destination
))))
2736 (setq outbuf
(current-buffer))))
2739 ((stringp (cadr destination
))
2740 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
2741 (if (tramp-equal-remote default-directory
(cadr destination
))
2742 ;; stderr is on the same remote host.
2743 (setq stderr
(with-parsed-tramp-file-name
2744 (cadr destination
) nil localname
))
2745 ;; stderr must be copied to remote host. The temporary
2746 ;; file must be deleted after execution.
2747 (setq stderr
(tramp-make-tramp-temp-file v
)
2748 tmpstderr
(tramp-make-tramp-file-name
2749 method user host stderr
))))
2750 ;; stderr to be discarded.
2751 ((null (cadr destination
))
2752 (setq stderr
"/dev/null"))))
2755 (setq outbuf
(current-buffer))))
2756 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
2758 ;; Send the command. It might not return in time, so we protect
2759 ;; it. Call it in a subshell, in order to preserve working
2764 (if (tramp-send-command-and-check
2765 v
(format "\\cd %s; %s"
2766 (tramp-shell-quote-argument localname
)
2770 ;; We should show the output anyway.
2772 (with-current-buffer outbuf
2774 (with-current-buffer (tramp-get-connection-buffer v
)
2776 (when display
(display-buffer outbuf
))))
2777 ;; When the user did interrupt, we should do it also. We use
2778 ;; return code -1 as marker.
2780 (kill-buffer (tramp-get-connection-buffer v
))
2784 (kill-buffer (tramp-get-connection-buffer v
))
2787 ;; Provide error file.
2788 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
2790 ;; Cleanup. We remove all file cache values for the connection,
2791 ;; because the remote process could have changed them.
2792 (when tmpinput
(delete-file tmpinput
))
2794 ;; `process-file-side-effects' has been introduced with GNU
2795 ;; Emacs 23.2. If set to `nil', no remote file will be changed
2796 ;; by `program'. If it doesn't exist, we assume its default
2798 (unless (and (boundp 'process-file-side-effects
)
2799 (not (symbol-value 'process-file-side-effects
)))
2800 (tramp-flush-directory-property v
""))
2802 ;; Return exit status.
2807 (defun tramp-sh-handle-call-process-region
2808 (start end program
&optional delete buffer display
&rest args
)
2809 "Like `call-process-region' for Tramp files."
2810 (let ((tmpfile (tramp-compat-make-temp-file "")))
2811 (write-region start end tmpfile
)
2812 (when delete
(delete-region start end
))
2814 (apply 'call-process program tmpfile buffer display args
)
2815 (delete-file tmpfile
))))
2817 (defun tramp-sh-handle-shell-command
2818 (command &optional output-buffer error-buffer
)
2819 "Like `shell-command' for Tramp files."
2820 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
2821 ;; We cannot use `shell-file-name' and `shell-command-switch',
2822 ;; they are variables of the local host.
2824 (tramp-get-method-parameter
2825 (tramp-file-name-method
2826 (tramp-dissect-file-name default-directory
))
2828 "-c" (substring command
0 asynchronous
)))
2832 ((bufferp output-buffer
) output-buffer
)
2833 ((stringp output-buffer
) (get-buffer-create output-buffer
))
2835 (setq current-buffer-p t
)
2837 (t (get-buffer-create
2839 "*Async Shell Command*"
2840 "*Shell Command Output*")))))
2843 ((bufferp error-buffer
) error-buffer
)
2844 ((stringp error-buffer
) (get-buffer-create error-buffer
))))
2846 (if (and (not asynchronous
) error-buffer
)
2847 (with-parsed-tramp-file-name default-directory nil
2848 (list output-buffer
(tramp-make-tramp-temp-file v
)))
2850 (p (get-buffer-process output-buffer
)))
2852 ;; Check whether there is another process running. Tramp does not
2853 ;; support 2 (asynchronous) processes in parallel.
2855 (if (yes-or-no-p "A command is running. Kill it? ")
2856 (ignore-errors (kill-process p
))
2857 (error "Shell command in progress")))
2859 (if current-buffer-p
2861 (barf-if-buffer-read-only)
2863 (with-current-buffer output-buffer
2864 (setq buffer-read-only nil
)
2867 (if (and (not current-buffer-p
) (integerp asynchronous
))
2870 (apply 'start-file-process
"*Async Shell*" buffer args
)
2872 (pop-to-buffer output-buffer
)
2873 (setq mode-line-process
'(":%s"))
2878 (apply 'process-file
(car args
) nil buffer nil
(cdr args
))
2879 ;; Insert error messages if they were separated.
2880 (when (listp buffer
)
2881 (with-current-buffer error-buffer
2882 (insert-file-contents (cadr buffer
)))
2883 (delete-file (cadr buffer
)))
2884 (if current-buffer-p
2885 ;; This is like exchange-point-and-mark, but doesn't
2886 ;; activate the mark. It is cleaner to avoid activation,
2887 ;; even though the command loop would deactivate the mark
2888 ;; because we inserted text.
2889 (goto-char (prog1 (mark t
)
2890 (set-marker (mark-marker) (point)
2892 ;; There's some output, display it.
2893 (when (with-current-buffer output-buffer
(> (point-max) (point-min)))
2894 (if (functionp 'display-message-or-buffer
)
2895 (tramp-compat-funcall 'display-message-or-buffer output-buffer
)
2896 (pop-to-buffer output-buffer
))))))))
2898 (defun tramp-sh-handle-file-local-copy (filename)
2899 "Like `file-local-copy' for Tramp files."
2900 (with-parsed-tramp-file-name filename nil
2901 (unless (file-exists-p filename
)
2904 "Cannot make local copy of non-existing file `%s'" filename
))
2906 (let* ((size (nth 7 (file-attributes filename
)))
2907 (rem-enc (tramp-get-inline-coding v
"remote-encoding" size
))
2908 (loc-dec (tramp-get-inline-coding v
"local-decoding" size
))
2909 (tmpfile (tramp-compat-make-temp-file filename
)))
2913 ;; `copy-file' handles direct copy and out-of-band methods.
2914 ((or (tramp-local-host-p v
)
2915 (tramp-method-out-of-band-p v size
))
2916 (copy-file filename tmpfile t t
))
2918 ;; Use inline encoding for file transfer.
2921 (with-progress-reporter
2922 v
3 (format "Encoding remote file %s" filename
)
2923 (tramp-barf-unless-okay
2924 v
(format rem-enc
(tramp-shell-quote-argument localname
))
2925 "Encoding remote file failed"))
2927 (if (functionp loc-dec
)
2928 ;; If local decoding is a function, we call it. We
2929 ;; must disable multibyte, because
2930 ;; `uudecode-decode-region' doesn't handle it
2933 (set-buffer-multibyte nil
)
2934 (insert-buffer-substring (tramp-get-buffer v
))
2935 (with-progress-reporter
2936 v
3 (format "Decoding remote file %s with function %s"
2938 (funcall loc-dec
(point-min) (point-max))
2939 ;; Unset `file-name-handler-alist'. Otherwise,
2940 ;; epa-file gets confused.
2941 (let (file-name-handler-alist
2942 (coding-system-for-write 'binary
))
2943 (write-region (point-min) (point-max) tmpfile
))))
2945 ;; If tramp-decoding-function is not defined for this
2946 ;; method, we invoke tramp-decoding-command instead.
2947 (let ((tmpfile2 (tramp-compat-make-temp-file filename
)))
2948 ;; Unset `file-name-handler-alist'. Otherwise,
2949 ;; epa-file gets confused.
2950 (let (file-name-handler-alist
2951 (coding-system-for-write 'binary
))
2952 (write-region (point-min) (point-max) tmpfile2
))
2953 (with-progress-reporter
2954 v
3 (format "Decoding remote file %s with command %s"
2957 (tramp-call-local-coding-command
2958 loc-dec tmpfile2 tmpfile
)
2959 (delete-file tmpfile2
)))))
2961 ;; Set proper permissions.
2962 (set-file-modes tmpfile
(tramp-default-file-modes filename
))
2963 ;; Set local user ownership.
2964 (tramp-set-file-uid-gid tmpfile
)))
2966 ;; Oops, I don't know what to do.
2968 v
'file-error
"Wrong method specification for `%s'" method
)))
2972 (delete-file tmpfile
)
2973 (signal (car err
) (cdr err
))))
2975 (run-hooks 'tramp-handle-file-local-copy-hook
)
2978 ;; This is needed for XEmacs only. Code stolen from files.el.
2979 (defun tramp-sh-handle-insert-file-contents-literally
2980 (filename &optional visit beg end replace
)
2981 "Like `insert-file-contents-literally' for Tramp files."
2982 (let ((format-alist nil
)
2983 (after-insert-file-functions nil
)
2984 (coding-system-for-read 'no-conversion
)
2985 (coding-system-for-write 'no-conversion
)
2986 (find-buffer-file-type-function
2987 (if (fboundp 'find-buffer-file-type
)
2988 (symbol-function 'find-buffer-file-type
)
2990 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler
))
2991 (inhibit-file-name-operation 'insert-file-contents
))
2994 (fset 'find-buffer-file-type
(lambda (filename) t
))
2995 (insert-file-contents filename visit beg end replace
))
2997 (if find-buffer-file-type-function
2998 (fset 'find-buffer-file-type find-buffer-file-type-function
)
2999 (fmakunbound 'find-buffer-file-type
)))))
3001 (defun tramp-sh-handle-make-auto-save-file-name ()
3002 "Like `make-auto-save-file-name' for Tramp files.
3003 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
3004 (let ((tramp-auto-save-directory tramp-auto-save-directory
)
3006 (tramp-subst-strs-in-string
3013 (buffer-file-name))))
3014 ;; File name must be unique. This is ensured with Emacs 22 (see
3015 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
3016 ;; all other cases we must do it ourselves.
3017 (when (boundp 'auto-save-file-name-transforms
)
3020 (when (and (string-match (car x
) buffer-file-name
)
3021 (not (car (cddr x
))))
3022 (setq tramp-auto-save-directory
3023 (or tramp-auto-save-directory
3024 (tramp-compat-temporary-file-directory)))))
3025 (symbol-value 'auto-save-file-name-transforms
)))
3026 ;; Create directory.
3027 (when tramp-auto-save-directory
3028 (setq buffer-file-name
3029 (expand-file-name buffer-file-name tramp-auto-save-directory
))
3030 (unless (file-exists-p tramp-auto-save-directory
)
3031 (make-directory tramp-auto-save-directory t
)))
3032 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3033 ;; it is not a magic file name operation (since Emacs 22).
3034 ;; We must deactivate it temporarily.
3035 (if (not (ad-is-active 'make-auto-save-file-name
))
3036 (tramp-run-real-handler 'make-auto-save-file-name nil
)
3038 (ad-deactivate 'make-auto-save-file-name
)
3040 (tramp-run-real-handler 'make-auto-save-file-name nil
)
3041 (ad-activate 'make-auto-save-file-name
)))))
3043 ;; CCC grok LOCKNAME
3044 (defun tramp-sh-handle-write-region
3045 (start end filename
&optional append visit lockname confirm
)
3046 "Like `write-region' for Tramp files."
3047 (setq filename
(expand-file-name filename
))
3048 (with-parsed-tramp-file-name filename nil
3049 ;; Following part commented out because we don't know what to do about
3050 ;; file locking, and it does not appear to be a problem to ignore it.
3051 ;; Ange-ftp ignores it, too.
3052 ;; (when (and lockname (stringp lockname))
3053 ;; (setq lockname (expand-file-name lockname)))
3054 ;; (unless (or (eq lockname nil)
3055 ;; (string= lockname filename))
3057 ;; "tramp-sh-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3059 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
3060 (when (and (not (featurep 'xemacs
)) confirm
(file-exists-p filename
))
3061 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename
))
3062 (tramp-error v
'file-error
"File not overwritten")))
3064 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename
'integer
))
3065 (tramp-get-remote-uid v
'integer
)))
3066 (gid (or (nth 3 (tramp-compat-file-attributes filename
'integer
))
3067 (tramp-get-remote-gid v
'integer
))))
3069 (if (and (tramp-local-host-p v
)
3070 ;; `file-writable-p' calls `file-expand-file-name'. We
3071 ;; cannot use `tramp-run-real-handler' therefore.
3072 (let (file-name-handler-alist)
3074 (file-writable-p (file-name-directory localname
))
3075 (or (file-directory-p localname
)
3076 (file-writable-p localname
)))))
3077 ;; Short track: if we are on the local host, we can run directly.
3078 (tramp-run-real-handler
3080 (list start end localname append
'no-message lockname confirm
))
3082 (let ((modes (save-excursion (tramp-default-file-modes filename
)))
3083 ;; We use this to save the value of
3084 ;; `last-coding-system-used' after writing the tmp
3085 ;; file. At the end of the function, we set
3086 ;; `last-coding-system-used' to this saved value. This
3087 ;; way, any intermediary coding systems used while
3088 ;; talking to the remote shell or suchlike won't hose
3089 ;; this variable. This approach was snarfed from
3092 ;; Write region into a tmp file. This isn't really
3093 ;; needed if we use an encoding function, but currently
3094 ;; we use it always because this makes the logic
3096 (tmpfile (or tramp-temp-buffer-file-name
3097 (tramp-compat-make-temp-file filename
))))
3099 ;; If `append' is non-nil, we copy the file locally, and let
3100 ;; the native `write-region' implementation do the job.
3101 (when append
(copy-file filename tmpfile
'ok
))
3103 ;; We say `no-message' here because we don't want the
3104 ;; visited file modtime data to be clobbered from the temp
3105 ;; file. We call `set-visited-file-modtime' ourselves later
3106 ;; on. We must ensure that `file-coding-system-alist'
3107 ;; matches `tmpfile'.
3108 (let (file-name-handler-alist
3109 (file-coding-system-alist
3110 (tramp-find-file-name-coding-system-alist filename tmpfile
)))
3112 (tramp-run-real-handler
3114 (list start end tmpfile append
'no-message lockname confirm
))
3116 (setq tramp-temp-buffer-file-name nil
)
3117 (delete-file tmpfile
)
3118 (signal (car err
) (cdr err
))))
3120 ;; Now, `last-coding-system-used' has the right value. Remember it.
3121 (when (boundp 'last-coding-system-used
)
3122 (setq coding-system-used
3123 (symbol-value 'last-coding-system-used
))))
3125 ;; The permissions of the temporary file should be set. If
3126 ;; filename does not exist (eq modes nil) it has been
3127 ;; renamed to the backup file. This case `save-buffer'
3128 ;; handles permissions.
3129 ;; Ensure, that it is still readable.
3133 (logior (or modes
0) (tramp-compat-octal-to-decimal "0400"))))
3135 ;; This is a bit lengthy due to the different methods
3136 ;; possible for file transfer. First, we check whether the
3137 ;; method uses an rcp program. If so, we call it.
3138 ;; Otherwise, both encoding and decoding command must be
3139 ;; specified. However, if the method _also_ specifies an
3140 ;; encoding function, then that is used for encoding the
3141 ;; contents of the tmp file.
3142 (let* ((size (nth 7 (file-attributes tmpfile
)))
3143 (rem-dec (tramp-get-inline-coding v
"remote-decoding" size
))
3144 (loc-enc (tramp-get-inline-coding v
"local-encoding" size
)))
3146 ;; `copy-file' handles direct copy and out-of-band methods.
3147 ((or (tramp-local-host-p v
)
3148 (tramp-method-out-of-band-p v size
))
3149 (if (and (not (stringp start
))
3150 (= (or end
(point-max)) (point-max))
3151 (= (or start
(point-min)) (point-min))
3152 (tramp-get-method-parameter
3153 method
'tramp-copy-keep-tmpfile
))
3155 (setq tramp-temp-buffer-file-name tmpfile
)
3157 ;; We keep the local file for performance
3158 ;; reasons, useful for "rsync".
3159 (copy-file tmpfile filename t
)
3161 (setq tramp-temp-buffer-file-name nil
)
3162 (delete-file tmpfile
)
3163 (signal (car err
) (cdr err
)))))
3164 (setq tramp-temp-buffer-file-name nil
)
3165 ;; Don't rename, in order to keep context in SELinux.
3167 (copy-file tmpfile filename t
)
3168 (delete-file tmpfile
))))
3170 ;; Use inline file transfer.
3175 (set-buffer-multibyte nil
)
3176 ;; Use encoding function or command.
3177 (if (functionp loc-enc
)
3178 (with-progress-reporter
3179 v
3 (format "Encoding region using function `%s'"
3181 (let ((coding-system-for-read 'binary
))
3182 (insert-file-contents-literally tmpfile
))
3183 ;; The following `let' is a workaround for the
3184 ;; base64.el that comes with pgnus-0.84. If
3185 ;; both of the following conditions are
3186 ;; satisfied, it tries to write to a local
3187 ;; file in default-directory, but at this
3188 ;; point, default-directory is remote.
3189 ;; (`call-process-region' can't write to
3190 ;; remote files, it seems.) The file in
3191 ;; question is a tmp file anyway.
3192 (let ((default-directory
3193 (tramp-compat-temporary-file-directory)))
3194 (funcall loc-enc
(point-min) (point-max))))
3196 (with-progress-reporter
3197 v
3 (format "Encoding region using command `%s'"
3199 (unless (zerop (tramp-call-local-coding-command
3203 (concat "Cannot write to `%s', "
3204 "local encoding command `%s' failed")
3205 filename loc-enc
))))
3207 ;; Send buffer into remote decoding command which
3208 ;; writes to remote file. Because this happens on
3209 ;; the remote host, we cannot use the function.
3210 (with-progress-reporter
3212 (format "Decoding region into remote file %s" filename
)
3213 (goto-char (point-max))
3214 (unless (bolp) (newline))
3218 (concat rem-dec
" <<'EOF'\n%sEOF")
3219 (tramp-shell-quote-argument localname
)
3221 (tramp-barf-unless-okay
3223 "Couldn't write region to `%s', decode using `%s' failed"
3225 ;; When `file-precious-flag' is set, the region is
3226 ;; written to a temporary file. Check that the
3227 ;; checksum is equal to that from the local tmpfile.
3228 (when file-precious-flag
3231 ;; cksum runs locally, if possible.
3232 (zerop (tramp-compat-call-process "cksum" tmpfile t
))
3233 ;; cksum runs remotely.
3234 (tramp-send-command-and-check
3237 "cksum <%s" (tramp-shell-quote-argument localname
)))
3238 ;; ... they are different.
3242 (with-current-buffer (tramp-get-buffer v
)
3246 (concat "Couldn't write region to `%s',"
3247 " decode using `%s' failed")
3248 filename rem-dec
)))))
3251 (delete-file tmpfile
)))
3253 ;; That's not expected.
3257 (concat "Method `%s' should specify both encoding and "
3258 "decoding command or an rcp program")
3261 ;; Make `last-coding-system-used' have the right value.
3262 (when coding-system-used
3263 (set 'last-coding-system-used coding-system-used
))))
3265 (tramp-flush-file-property v
(file-name-directory localname
))
3266 (tramp-flush-file-property v localname
)
3268 ;; We must protect `last-coding-system-used', now we have set it
3269 ;; to its correct value.
3270 (let (last-coding-system-used (need-chown t
))
3271 ;; Set file modification time.
3272 (when (or (eq visit t
) (stringp visit
))
3273 (let ((file-attr (file-attributes filename
)))
3274 (set-visited-file-modtime
3275 ;; We must pass modtime explicitely, because filename can
3276 ;; be different from (buffer-file-name), f.e. if
3277 ;; `file-precious-flag' is set.
3279 (when (and (eq (nth 2 file-attr
) uid
)
3280 (eq (nth 3 file-attr
) gid
))
3281 (setq need-chown nil
))))
3283 ;; Set the ownership.
3285 (tramp-set-file-uid-gid filename uid gid
))
3286 (when (or (eq visit t
) (null visit
) (stringp visit
))
3287 (tramp-message v
0 "Wrote %s" filename
))
3288 (run-hooks 'tramp-handle-write-region-hook
)))))
3290 (defvar tramp-vc-registered-file-names nil
3291 "List used to collect file names, which are checked during `vc-registered'.")
3293 ;; VC backends check for the existence of various different special
3294 ;; files. This is very time consuming, because every single check
3295 ;; requires a remote command (the file cache must be invalidated).
3296 ;; Therefore, we apply a kind of optimization. We install the file
3297 ;; name handler `tramp-vc-file-name-handler', which does nothing but
3298 ;; remembers all file names for which `file-exists-p' or
3299 ;; `file-readable-p' has been applied. A first run of `vc-registered'
3300 ;; is performed. Afterwards, a script is applied for all collected
3301 ;; file names, using just one remote command. The result of this
3302 ;; script is used to fill the file cache with actual values. Now we
3303 ;; can reset the file name handlers, and we make a second run of
3304 ;; `vc-registered', which returns the expected result without sending
3305 ;; any other remote command.
3306 (defun tramp-sh-handle-vc-registered (file)
3307 "Like `vc-registered' for Tramp files."
3308 (tramp-compat-with-temp-message ""
3309 (with-parsed-tramp-file-name file nil
3310 (with-progress-reporter
3311 v
3 (format "Checking `vc-registered' for %s" file
)
3313 ;; There could be new files, created by the vc backend. We
3314 ;; cannot reuse the old cache entries, therefore.
3315 (let (tramp-vc-registered-file-names
3316 (remote-file-name-inhibit-cache (current-time))
3317 (file-name-handler-alist
3318 `((,tramp-file-name-regexp . tramp-vc-file-name-handler
))))
3320 ;; Here we collect only file names, which need an operation.
3321 (tramp-run-real-handler 'vc-registered
(list file
))
3322 (tramp-message v
10 "\n%s" tramp-vc-registered-file-names
)
3324 ;; Send just one command, in order to fill the cache.
3325 (when tramp-vc-registered-file-names
3326 (tramp-maybe-send-script
3328 (format tramp-vc-registered-read-file-names
3329 (tramp-get-file-exists-command v
)
3330 (format "%s -r" (tramp-get-test-command v
)))
3331 "tramp_vc_registered_read_file_names")
3335 (tramp-send-command-and-read
3338 "tramp_vc_registered_read_file_names <<'EOF'\n%s\nEOF\n"
3339 (mapconcat 'tramp-shell-quote-argument
3340 tramp-vc-registered-file-names
3343 (tramp-set-file-property
3344 v
(car elt
) (cadr elt
) (cadr (cdr elt
))))))
3346 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3347 ;; calls shall be answered from the file cache. We unset
3348 ;; `process-file-side-effects' in order to keep the cache when
3349 ;; `process-file' calls appear.
3350 (let (process-file-side-effects)
3351 (tramp-run-real-handler 'vc-registered
(list file
)))))))
3353 ;;;###tramp-autoload
3354 (defun tramp-sh-file-name-handler (operation &rest args
)
3355 "Invoke remote-shell Tramp file name handler.
3356 Fall back to normal file name handler if no Tramp handler exists."
3357 (when (and tramp-locked
(not tramp-locker
))
3358 (setq tramp-locked nil
)
3359 (signal 'file-error
(list "Forbidden reentrant call of Tramp")))
3360 (let ((tl tramp-locked
))
3363 (setq tramp-locked t
)
3364 (let ((tramp-locker t
))
3366 (let ((fn (assoc operation tramp-sh-file-name-handler-alist
)))
3368 (apply (cdr fn
) args
)
3369 (tramp-run-real-handler operation args
))))))
3370 (setq tramp-locked tl
))))
3372 (defun tramp-vc-file-name-handler (operation &rest args
)
3373 "Invoke special file name handler, which collects files to be handled."
3376 (tramp-replace-environment-variables
3377 (apply 'tramp-file-name-for-operation operation args
)))
3378 (fn (assoc operation tramp-sh-file-name-handler-alist
)))
3379 (with-parsed-tramp-file-name filename nil
3381 ;; That's what we want: file names, for which checks are
3382 ;; applied. We assume, that VC uses only `file-exists-p' and
3383 ;; `file-readable-p' checks; otherwise we must extend the
3384 ;; list. We do not perform any action, but return nil, in
3385 ;; order to keep `vc-registered' running.
3386 ((and fn
(memq operation
'(file-exists-p file-readable-p
)))
3387 (add-to-list 'tramp-vc-registered-file-names localname
'append
)
3389 ;; Tramp file name handlers like `expand-file-name'. They
3392 (save-match-data (apply (cdr fn
) args
)))
3393 ;; Default file name handlers, we don't care.
3394 (t (tramp-run-real-handler operation args
)))))))
3396 ;;; Internal Functions:
3398 (defun tramp-maybe-send-script (vec script name
)
3399 "Define in remote shell function NAME implemented as SCRIPT.
3400 Only send the definition if it has not already been done."
3401 (let* ((p (tramp-get-connection-process vec
))
3402 (scripts (tramp-get-connection-property p
"scripts" nil
)))
3403 (unless (member name scripts
)
3404 (with-progress-reporter vec
5 (format "Sending script `%s'" name
)
3405 ;; The script could contain a call of Perl. This is masked with `%s'.
3406 (tramp-barf-unless-okay
3408 (format "%s () {\n%s\n}" name
3409 (format script
(tramp-get-remote-perl vec
)))
3410 "Script %s sending failed" name
)
3411 (tramp-set-connection-property p
"scripts" (cons name scripts
))))))
3413 (defun tramp-set-auto-save ()
3414 (when (and ;; ange-ftp has its own auto-save mechanism
3415 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
3416 'tramp-sh-file-name-handler
)
3418 (auto-save-mode 1)))
3419 (add-hook 'find-file-hooks
'tramp-set-auto-save t
)
3420 (add-hook 'tramp-unload-hook
3422 (remove-hook 'find-file-hooks
'tramp-set-auto-save
)))
3424 (defun tramp-run-test (switch filename
)
3425 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3426 Returns the exit code of the `test' program."
3427 (with-parsed-tramp-file-name filename nil
3428 (tramp-send-command-and-check
3432 (tramp-get-test-command v
)
3434 (tramp-shell-quote-argument localname
)))))
3436 (defun tramp-run-test2 (format-string file1 file2
)
3437 "Run `test'-like program on the remote system, given FILE1, FILE2.
3438 FORMAT-STRING contains the program name, switches, and place holders.
3439 Returns the exit code of the `test' program. Barfs if the methods,
3440 hosts, or files, disagree."
3441 (unless (tramp-equal-remote file1 file2
)
3442 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1
) file1 file2
) nil
3445 "tramp-run-test2 only implemented for same method, user, host")))
3446 (with-parsed-tramp-file-name file1 v1
3447 (with-parsed-tramp-file-name file1 v2
3448 (tramp-send-command-and-check
3450 (format format-string
3451 (tramp-shell-quote-argument v1-localname
)
3452 (tramp-shell-quote-argument v2-localname
))))))
3454 (defun tramp-find-executable
3455 (vec progname dirlist
&optional ignore-tilde ignore-path
)
3456 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3457 First arg VEC specifies the connection, PROGNAME is the program
3458 to search for, and DIRLIST gives the list of directories to
3459 search. If IGNORE-TILDE is non-nil, directory names starting
3460 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3463 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3465 This function expects to be in the right *tramp* buffer."
3466 (with-current-buffer (tramp-get-connection-buffer vec
)
3468 ;; Check whether the executable is in $PATH. "which(1)" does not
3469 ;; report always a correct error code; therefore we check the
3470 ;; number of words it returns.
3472 (tramp-send-command vec
(format "which \\%s | wc -w" progname
))
3473 (goto-char (point-min))
3474 (if (looking-at "^\\s-*1$")
3475 (setq result
(concat "\\" progname
))))
3478 ;; Remove all ~/foo directories from dirlist. In XEmacs,
3479 ;; `remove' is in CL, and we want to avoid CL dependencies.
3482 (setq d
(car dirlist
))
3483 (setq dirlist
(cdr dirlist
))
3484 (unless (char-equal ?~
(aref d
0))
3485 (setq newdl
(cons d newdl
))))
3486 (setq dirlist
(nreverse newdl
))))
3489 (format (concat "while read d; "
3490 "do if test -x $d/%s -a -f $d/%s; "
3491 "then echo tramp_executable $d/%s; "
3492 "break; fi; done <<'EOF'\n"
3494 progname progname progname
(mapconcat 'identity dirlist
"\n")))
3495 (goto-char (point-max))
3496 (when (search-backward "tramp_executable " nil t
)
3497 (skip-chars-forward "^ ")
3498 (skip-chars-forward " ")
3499 (setq result
(buffer-substring
3500 (point) (tramp-compat-line-end-position)))))
3503 (defun tramp-set-remote-path (vec)
3504 "Sets the remote environment PATH to existing directories.
3505 I.e., for each directory in `tramp-remote-path', it is tested
3506 whether it exists and if so, it is added to the environment
3508 (tramp-message vec
5 (format "Setting $PATH environment variable"))
3510 vec
(format "PATH=%s; export PATH"
3511 (mapconcat 'identity
(tramp-get-remote-path vec
) ":"))))
3513 ;; ------------------------------------------------------------
3514 ;; -- Communication with external shell --
3515 ;; ------------------------------------------------------------
3517 (defun tramp-find-file-exists-command (vec)
3518 "Find a command on the remote host for checking if a file exists.
3519 Here, we are looking for a command which has zero exit status if the
3520 file exists and nonzero exit status otherwise."
3521 (let ((existing "/")
3523 (tramp-shell-quote-argument "/ this file does not exist "))
3525 ;; The algorithm is as follows: we try a list of several commands.
3526 ;; For each command, we first run `$cmd /' -- this should return
3527 ;; true, as the root directory always exists. And then we run
3528 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
3529 ;; does not exist. This should return false. We use the first
3530 ;; command we find that seems to work.
3531 ;; The list of commands to try is as follows:
3532 ;; `ls -d' This works on most systems, but NetBSD 1.4
3533 ;; has a bug: `ls' always returns zero exit
3534 ;; status, even for files which don't exist.
3535 ;; `test -e' Some Bourne shells have a `test' builtin
3536 ;; which does not know the `-e' option.
3537 ;; `/bin/test -e' For those, the `test' binary on disk normally
3538 ;; provides the option. Alas, the binary
3539 ;; is sometimes `/bin/test' and sometimes it's
3541 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3543 (and (setq result
(format "%s -e" (tramp-get-test-command vec
)))
3544 (tramp-send-command-and-check
3545 vec
(format "%s %s" result existing
))
3546 (not (tramp-send-command-and-check
3547 vec
(format "%s %s" result nonexisting
))))
3548 (and (setq result
"/bin/test -e")
3549 (tramp-send-command-and-check
3550 vec
(format "%s %s" result existing
))
3551 (not (tramp-send-command-and-check
3552 vec
(format "%s %s" result nonexisting
))))
3553 (and (setq result
"/usr/bin/test -e")
3554 (tramp-send-command-and-check
3555 vec
(format "%s %s" result existing
))
3556 (not (tramp-send-command-and-check
3557 vec
(format "%s %s" result nonexisting
))))
3558 (and (setq result
(format "%s -d" (tramp-get-ls-command vec
)))
3559 (tramp-send-command-and-check
3560 vec
(format "%s %s" result existing
))
3561 (not (tramp-send-command-and-check
3562 vec
(format "%s %s" result nonexisting
)))))
3564 vec
'file-error
"Couldn't find command to check if file exists"))
3567 (defun tramp-open-shell (vec shell
)
3568 "Opens shell SHELL."
3569 (with-progress-reporter vec
5 (format "Opening remote shell `%s'" shell
)
3570 ;; Find arguments for this shell.
3571 (let ((tramp-end-of-output tramp-initial-end-of-output
)
3572 (alist tramp-sh-extra-args
)
3574 (while (and alist
(null extra-args
))
3575 (setq item
(pop alist
))
3576 (when (string-match (car item
) shell
)
3577 (setq extra-args
(cdr item
))))
3578 (when extra-args
(setq shell
(concat shell
" " extra-args
)))
3580 vec
(format "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s"
3581 (shell-quote-argument tramp-end-of-output
) shell
)
3585 vec
(format "PS1=%s" (shell-quote-argument tramp-end-of-output
)) t
)
3586 (tramp-send-command vec
"PS2=''" t
)
3587 (tramp-send-command vec
"PS3=''" t
)
3588 (tramp-send-command vec
"PROMPT_COMMAND=''" t
)))
3590 (defun tramp-find-shell (vec)
3591 "Opens a shell on the remote host which groks tilde expansion."
3592 (unless (tramp-get-connection-property vec
"remote-shell" nil
)
3594 (with-current-buffer (tramp-get-buffer vec
)
3595 (tramp-send-command vec
"echo ~root" t
)
3597 ((or (string-match "^~root$" (buffer-string))
3598 ;; The default shell (ksh93) of OpenSolaris is buggy.
3599 (string-equal (tramp-get-connection-property vec
"uname" "")
3602 (or (tramp-find-executable
3603 vec
"bash" (tramp-get-remote-path vec
) t t
)
3604 (tramp-find-executable
3605 vec
"ksh" (tramp-get-remote-path vec
) t t
)))
3609 "Couldn't find a shell which groks tilde expansion"))
3611 vec
5 "Starting remote shell `%s' for tilde expansion" shell
)
3612 (tramp-open-shell vec shell
))
3615 vec
5 "Remote `%s' groks tilde expansion, good"
3616 (tramp-set-connection-property
3618 (tramp-get-method-parameter
3619 (tramp-file-name-method vec
) 'tramp-remote-sh
)))))))))
3621 ;; Utility functions.
3623 (defun tramp-barf-if-no-shell-prompt (proc timeout
&rest error-args
)
3624 "Wait for shell prompt and barf if none appears.
3625 Looks at process PROC to see if a shell prompt appears in TIMEOUT
3626 seconds. If not, it produces an error message with the given ERROR-ARGS."
3628 (tramp-wait-for-regexp
3631 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern
))
3632 (apply 'tramp-error-with-buffer nil proc
'file-error error-args
)))
3634 (defun tramp-open-connection-setup-interactive-shell (proc vec
)
3635 "Set up an interactive shell.
3636 Mainly sets the prompt and the echo correctly. PROC is the shell
3637 process to set up. VEC specifies the connection."
3638 (let ((tramp-end-of-output tramp-initial-end-of-output
))
3639 ;; It is useful to set the prompt in the following command because
3640 ;; some people have a setting for $PS1 which /bin/sh doesn't know
3641 ;; about and thus /bin/sh will display a strange prompt. For
3642 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
3643 ;; display the current working directory but /bin/sh will display
3644 ;; a dollar sign. The following command line sets $PS1 to a sane
3645 ;; value, and works under Bourne-ish shells as well as csh-like
3646 ;; shells. Daniel Pittman reports that the unusual positioning of
3647 ;; the single quotes makes it work under `rc', too. We also unset
3648 ;; the variable $ENV because that is read by some sh
3649 ;; implementations (eg, bash when called as sh) on startup; this
3650 ;; way, we avoid the startup file clobbering $PS1. $PROMP_COMMAND
3651 ;; is another way to set the prompt in /bin/bash, it must be
3652 ;; discarded as well.
3655 (tramp-get-method-parameter (tramp-file-name-method vec
) 'tramp-remote-sh
))
3658 (tramp-message vec
5 "Setting up remote shell environment")
3659 (tramp-send-command vec
"stty -inlcr -echo kill '^U' erase '^H'" t
)
3660 ;; Check whether the echo has really been disabled. Some
3661 ;; implementations, like busybox of embedded GNU/Linux, don't
3662 ;; support disabling.
3663 (tramp-send-command vec
"echo foo" t
)
3664 (with-current-buffer (process-buffer proc
)
3665 (goto-char (point-min))
3666 (when (looking-at "echo foo")
3667 (tramp-set-connection-property proc
"remote-echo" t
)
3668 (tramp-message vec
5 "Remote echo still on. Ok.")
3669 ;; Make sure backspaces and their echo are enabled and no line
3670 ;; width magic interferes with them.
3671 (tramp-send-command vec
"stty icanon erase ^H cols 32767" t
))))
3673 (tramp-message vec
5 "Setting shell prompt")
3675 vec
(format "PS1=%s" (shell-quote-argument tramp-end-of-output
)) t
)
3676 (tramp-send-command vec
"PS2=''" t
)
3677 (tramp-send-command vec
"PS3=''" t
)
3678 (tramp-send-command vec
"PROMPT_COMMAND=''" t
)
3680 ;; Try to set up the coding system correctly.
3681 ;; CCC this can't be the right way to do it. Hm.
3682 (tramp-message vec
5 "Determining coding system")
3683 (tramp-send-command vec
"echo foo ; echo bar" t
)
3684 (with-current-buffer (process-buffer proc
)
3685 (goto-char (point-min))
3686 (if (featurep 'mule
)
3687 ;; Use MULE to select the right EOL convention for communicating
3688 ;; with the process.
3689 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc
)
3690 (cons 'undecided
'undecided
)))
3691 cs-decode cs-encode
)
3692 (when (symbolp cs
) (setq cs
(cons cs cs
)))
3693 (setq cs-decode
(car cs
))
3694 (setq cs-encode
(cdr cs
))
3695 (unless cs-decode
(setq cs-decode
'undecided
))
3696 (unless cs-encode
(setq cs-encode
'undecided
))
3697 (setq cs-encode
(tramp-compat-coding-system-change-eol-conversion
3699 (when (search-forward "\r" nil t
)
3700 (setq cs-decode
(tramp-compat-coding-system-change-eol-conversion
3702 (tramp-compat-funcall
3703 'set-buffer-process-coding-system cs-decode cs-encode
)
3705 vec
5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode
))
3706 ;; Look for ^M and do something useful if found.
3707 (when (search-forward "\r" nil t
)
3708 ;; We have found a ^M but cannot frob the process coding system
3709 ;; because we're running on a non-MULE Emacs. Let's try
3711 (tramp-send-command vec
"stty -onlcr" t
))))
3713 (tramp-send-command vec
"set +o vi +o emacs" t
)
3715 ;; Check whether the output of "uname -sr" has been changed. If
3716 ;; yes, this is a strong indication that we must expire all
3717 ;; connection properties. We start again with
3718 ;; `tramp-maybe-open-connection', it will be catched there.
3719 (tramp-message vec
5 "Checking system information")
3720 (let ((old-uname (tramp-get-connection-property vec
"uname" nil
))
3722 (tramp-set-connection-property
3724 (tramp-send-command-and-read vec
"echo \\\"`uname -sr`\\\""))))
3725 (when (and (stringp old-uname
) (not (string-equal old-uname new-uname
)))
3726 (with-current-buffer (tramp-get-debug-buffer vec
)
3727 ;; Keep the debug buffer.
3729 (generate-new-buffer-name tramp-temp-buffer-name
) 'unique
)
3730 (tramp-cleanup-connection vec
)
3731 (if (= (point-min) (point-max))
3733 (rename-buffer (tramp-debug-buffer-name vec
) 'unique
))
3734 ;; We call `tramp-get-buffer' in order to keep the debug buffer.
3735 (tramp-get-buffer vec
)
3738 "Connection reset, because remote host changed from `%s' to `%s'"
3739 old-uname new-uname
)
3740 (throw 'uname-changed
(tramp-maybe-open-connection vec
)))))
3742 ;; Check whether the remote host suffers from buggy
3743 ;; `send-process-string'. This is known for FreeBSD (see comment in
3744 ;; `send_process', file process.c). I've tested sending 624 bytes
3745 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
3746 ;; this host type is detected locally. It cannot handle remote
3748 (with-connection-property proc
"chunksize"
3750 ((and (integerp tramp-chunksize
) (> tramp-chunksize
0))
3754 vec
5 "Checking remote host type for `send-process-string' bug")
3756 "^FreeBSD" (tramp-get-connection-property vec
"uname" ""))
3759 ;; Set remote PATH variable.
3760 (tramp-set-remote-path vec
)
3762 ;; Search for a good shell before searching for a command which
3763 ;; checks if a file exists. This is done because Tramp wants to use
3764 ;; "test foo; echo $?" to check if various conditions hold, and
3765 ;; there are buggy /bin/sh implementations which don't execute the
3766 ;; "echo $?" part if the "test" part has an error. In particular,
3767 ;; the OpenSolaris /bin/sh is a problem. There are also other
3768 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
3769 ;; in function declarations, or changing HISTFILE in place.
3770 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
3772 (tramp-find-shell vec
)
3774 ;; Disable unexpected output.
3775 (tramp-send-command vec
"mesg n; biff n" t
)
3777 ;; IRIX64 bash expands "!" even when in single quotes. This
3778 ;; destroys our shell functions, we must disable it. See
3779 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
3780 (when (string-match "^IRIX64" (tramp-get-connection-property vec
"uname" ""))
3781 (tramp-send-command vec
"set +H" t
))
3783 ;; On BSD-like systems, ?\t is expanded to spaces. Suppress this.
3784 (when (string-match "BSD\\|Darwin"
3785 (tramp-get-connection-property vec
"uname" ""))
3786 (tramp-send-command vec
"stty -oxtabs" t
))
3788 ;; Set `remote-tty' process property.
3790 (let ((tty (tramp-send-command-and-read vec
"echo \\\"`tty`\\\"")))
3791 (unless (zerop (length tty
))
3792 (tramp-compat-process-put proc
'remote-tty tty
))))
3794 ;; Dump stty settings in the traces.
3795 (when (>= tramp-verbose
9)
3796 (tramp-send-command vec
"stty -a" t
))
3798 ;; Set the environment.
3799 (tramp-message vec
5 "Setting default environment")
3801 (let ((env (copy-sequence tramp-remote-process-environment
))
3804 (setq item
(tramp-compat-split-string (car env
) "="))
3805 (setcdr item
(mapconcat 'identity
(cdr item
) "="))
3806 (if (and (stringp (cdr item
)) (not (string-equal (cdr item
) "")))
3808 vec
(format "%s=%s; export %s" (car item
) (cdr item
) (car item
)) t
)
3809 (push (car item
) unset
))
3810 (setq env
(cdr env
)))
3813 vec
(format "unset %s" (mapconcat 'identity unset
" ")) t
))))
3815 ;; CCC: We should either implement a Perl version of base64 encoding
3816 ;; and decoding. Then we just use that in the last item. The other
3817 ;; alternative is to use the Perl version of UU encoding. But then
3818 ;; we need a Lisp version of uuencode.
3820 ;; Old text from documentation of tramp-methods:
3821 ;; Using a uuencode/uudecode inline method is discouraged, please use one
3822 ;; of the base64 methods instead since base64 encoding is much more
3823 ;; reliable and the commands are more standardized between the different
3824 ;; Unix versions. But if you can't use base64 for some reason, please
3825 ;; note that the default uudecode command does not work well for some
3826 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
3827 ;; the following command for uudecode:
3829 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
3831 ;; For Irix, no solution is known yet.
3833 (autoload 'uudecode-decode-region
"uudecode")
3835 (defconst tramp-local-coding-commands
3836 '((b64 base64-encode-region base64-decode-region
)
3837 (uu tramp-uuencode-region uudecode-decode-region
)
3839 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
3840 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
3841 "List of local coding commands for inline transfer.
3842 Each item is a list that looks like this:
3844 \(FORMAT ENCODING DECODING\)
3846 FORMAT is symbol describing the encoding/decoding format. It can be
3847 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3849 ENCODING and DECODING can be strings, giving commands, or symbols,
3850 giving functions. If they are strings, then they can contain
3851 the \"%s\" format specifier. If that specifier is present, the input
3852 filename will be put into the command line at that spot. If the
3853 specifier is not present, the input should be read from standard
3856 If they are functions, they will be called with two arguments, start
3857 and end of region, and are expected to replace the region contents
3858 with the encoded or decoded results, respectively.")
3860 (defconst tramp-remote-coding-commands
3861 '((b64 "base64" "base64 -d")
3862 (b64 "mimencode -b" "mimencode -u -b")
3863 (b64 "mmencode -b" "mmencode -u -b")
3864 (b64 "recode data..base64" "recode base64..data")
3865 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module
)
3866 (b64 tramp-perl-encode tramp-perl-decode
)
3867 (uu "uuencode xxx" "uudecode -o /dev/stdout")
3868 (uu "uuencode xxx" "uudecode -o -")
3869 (uu "uuencode xxx" "uudecode -p")
3870 (uu "uuencode xxx" tramp-uudecode
)
3872 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
3873 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
3874 "List of remote coding commands for inline transfer.
3875 Each item is a list that looks like this:
3877 \(FORMAT ENCODING DECODING\)
3879 FORMAT is symbol describing the encoding/decoding format. It can be
3880 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3882 ENCODING and DECODING can be strings, giving commands, or symbols,
3883 giving variables. If they are strings, then they can contain
3884 the \"%s\" format specifier. If that specifier is present, the input
3885 filename will be put into the command line at that spot. If the
3886 specifier is not present, the input should be read from standard
3889 If they are variables, this variable is a string containing a Perl
3890 implementation for this functionality. This Perl program will be transferred
3891 to the remote host, and it is available as shell function with the same name.")
3893 (defun tramp-find-inline-encoding (vec)
3894 "Find an inline transfer encoding that works.
3895 Goes through the list `tramp-local-coding-commands' and
3896 `tramp-remote-coding-commands'."
3898 (let ((local-commands tramp-local-coding-commands
)
3900 loc-enc loc-dec rem-enc rem-dec litem ritem found
)
3901 (while (and local-commands
(not found
))
3902 (setq litem
(pop local-commands
))
3903 (catch 'wont-work-local
3904 (let ((format (nth 0 litem
))
3905 (remote-commands tramp-remote-coding-commands
))
3906 (setq loc-enc
(nth 1 litem
))
3907 (setq loc-dec
(nth 2 litem
))
3908 ;; If the local encoder or decoder is a string, the
3909 ;; corresponding command has to work locally.
3910 (if (not (stringp loc-enc
))
3912 vec
5 "Checking local encoding function `%s'" loc-enc
)
3914 vec
5 "Checking local encoding command `%s' for sanity" loc-enc
)
3915 (unless (zerop (tramp-call-local-coding-command
3917 (throw 'wont-work-local nil
)))
3918 (if (not (stringp loc-dec
))
3920 vec
5 "Checking local decoding function `%s'" loc-dec
)
3922 vec
5 "Checking local decoding command `%s' for sanity" loc-dec
)
3923 (unless (zerop (tramp-call-local-coding-command
3925 (throw 'wont-work-local nil
)))
3926 ;; Search for remote coding commands with the same format
3927 (while (and remote-commands
(not found
))
3928 (setq ritem
(pop remote-commands
))
3929 (catch 'wont-work-remote
3930 (when (equal format
(nth 0 ritem
))
3931 (setq rem-enc
(nth 1 ritem
))
3932 (setq rem-dec
(nth 2 ritem
))
3933 ;; Check if remote encoding and decoding commands can be
3934 ;; called remotely with null input and output. This makes
3935 ;; sure there are no syntax errors and the command is really
3936 ;; found. Note that we do not redirect stdout to /dev/null,
3937 ;; for two reasons: when checking the decoding command, we
3938 ;; actually check the output it gives. And also, when
3939 ;; redirecting "mimencode" output to /dev/null, then as root
3940 ;; it might change the permissions of /dev/null!
3941 (when (not (stringp rem-enc
))
3942 (let ((name (symbol-name rem-enc
)))
3943 (while (string-match (regexp-quote "-") name
)
3944 (setq name
(replace-match "_" nil t name
)))
3945 (tramp-maybe-send-script vec
(symbol-value rem-enc
) name
)
3946 (setq rem-enc name
)))
3949 "Checking remote encoding command `%s' for sanity" rem-enc
)
3950 (unless (tramp-send-command-and-check
3951 vec
(format "%s </dev/null" rem-enc
) t
)
3952 (throw 'wont-work-remote nil
))
3954 (when (not (stringp rem-dec
))
3955 (let ((name (symbol-name rem-dec
)))
3956 (while (string-match (regexp-quote "-") name
)
3957 (setq name
(replace-match "_" nil t name
)))
3958 (tramp-maybe-send-script vec
(symbol-value rem-dec
) name
)
3959 (setq rem-dec name
)))
3962 "Checking remote decoding command `%s' for sanity" rem-dec
)
3963 (unless (tramp-send-command-and-check
3965 (format "echo %s | %s | %s" magic rem-enc rem-dec
)
3967 (throw 'wont-work-remote nil
))
3969 (with-current-buffer (tramp-get-buffer vec
)
3970 (goto-char (point-min))
3971 (unless (looking-at (regexp-quote magic
))
3972 (throw 'wont-work-remote nil
)))
3974 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
3975 (setq rem-enc
(nth 1 ritem
))
3976 (setq rem-dec
(nth 2 ritem
))
3977 (setq found t
)))))))
3979 ;; Did we find something?
3982 vec
'file-error
"Couldn't find an inline transfer encoding"))
3984 ;; Set connection properties.
3985 (tramp-message vec
5 "Using local encoding `%s'" loc-enc
)
3986 (tramp-set-connection-property vec
"local-encoding" loc-enc
)
3987 (tramp-message vec
5 "Using local decoding `%s'" loc-dec
)
3988 (tramp-set-connection-property vec
"local-decoding" loc-dec
)
3989 (tramp-message vec
5 "Using remote encoding `%s'" rem-enc
)
3990 (tramp-set-connection-property vec
"remote-encoding" rem-enc
)
3991 (tramp-message vec
5 "Using remote decoding `%s'" rem-dec
)
3992 (tramp-set-connection-property vec
"remote-decoding" rem-dec
))))
3994 (defun tramp-call-local-coding-command (cmd input output
)
3995 "Call the local encoding or decoding command.
3996 If CMD contains \"%s\", provide input file INPUT there in command.
3997 Otherwise, INPUT is passed via standard input.
3998 INPUT can also be nil which means `/dev/null'.
3999 OUTPUT can be a string (which specifies a filename), or t (which
4000 means standard output and thus the current buffer), or nil (which
4002 (tramp-compat-call-process
4003 tramp-encoding-shell
4004 (when (and input
(not (string-match "%s" cmd
))) input
)
4005 (if (eq output t
) t nil
)
4007 tramp-encoding-command-switch
4009 (if (string-match "%s" cmd
) (format cmd input
) cmd
)
4010 (if (stringp output
) (concat "> " output
) ""))))
4012 (defconst tramp-inline-compress-commands
4013 '(("gzip" "gzip -d")
4014 ("bzip2" "bzip2 -d")
4015 ("compress" "compress -d"))
4016 "List of compress and decompress commands for inline transfer.
4017 Each item is a list that looks like this:
4019 \(COMPRESS DECOMPRESS\)
4021 COMPRESS or DECOMPRESS are strings with the respective commands.")
4023 (defun tramp-find-inline-compress (vec)
4024 "Find an inline transfer compress command that works.
4025 Goes through the list `tramp-inline-compress-commands'."
4027 (let ((commands tramp-inline-compress-commands
)
4029 item compress decompress
4031 (while (and commands
(not found
))
4033 (setq item
(pop commands
)
4034 compress
(nth 0 item
)
4035 decompress
(nth 1 item
))
4038 "Checking local compress command `%s', `%s' for sanity"
4039 compress decompress
)
4040 (unless (zerop (tramp-call-local-coding-command
4041 (format "echo %s | %s | %s"
4042 magic compress decompress
) nil nil
))
4046 "Checking remote compress command `%s', `%s' for sanity"
4047 compress decompress
)
4048 (unless (tramp-send-command-and-check
4049 vec
(format "echo %s | %s | %s" magic compress decompress
) t
)
4053 ;; Did we find something?
4056 ;; Set connection properties.
4058 vec
5 "Using inline transfer compress command `%s'" compress
)
4059 (tramp-set-connection-property vec
"inline-compress" compress
)
4061 vec
5 "Using inline transfer decompress command `%s'" decompress
)
4062 (tramp-set-connection-property vec
"inline-decompress" decompress
))
4064 (tramp-set-connection-property vec
"inline-compress" nil
)
4065 (tramp-set-connection-property vec
"inline-decompress" nil
)
4067 vec
2 "Couldn't find an inline transfer compress command")))))
4069 (defun tramp-compute-multi-hops (vec)
4070 "Expands VEC according to `tramp-default-proxies-alist'.
4071 Gateway hops are already opened."
4072 (let ((target-alist `(,vec
))
4073 (choices tramp-default-proxies-alist
)
4076 ;; Look for proxy hosts to be passed.
4078 (setq item
(pop choices
)
4079 proxy
(eval (nth 2 item
)))
4082 (string-match (or (eval (nth 0 item
)) "")
4083 (or (tramp-file-name-host (car target-alist
)) ""))
4085 (string-match (or (eval (nth 1 item
)) "")
4086 (or (tramp-file-name-user (car target-alist
)) "")))
4088 ;; No more hops needed.
4090 ;; Replace placeholders.
4095 ?u
(or (tramp-file-name-user (car target-alist
)) "")
4096 ?h
(or (tramp-file-name-host (car target-alist
)) ""))))
4097 (with-parsed-tramp-file-name proxy l
4099 (add-to-list 'target-alist l
)
4100 ;; Start next search.
4101 (setq choices tramp-default-proxies-alist
)))))
4106 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method
)
4107 (tramp-file-name-method (car target-alist
)))
4108 (let ((gw (pop target-alist
))
4109 (hop (pop target-alist
)))
4110 ;; Is the method prepared for gateways?
4111 (unless (tramp-get-method-parameter
4112 (tramp-file-name-method hop
) 'tramp-default-port
)
4115 "Method `%s' is not supported for gateway access."
4116 (tramp-file-name-method hop
)))
4117 ;; Add default port if needed.
4120 tramp-host-with-port-regexp
(tramp-file-name-host hop
))
4123 (tramp-file-name-host hop
) tramp-prefix-port-format
4125 (tramp-get-method-parameter
4126 (tramp-file-name-method hop
) 'tramp-default-port
)))))
4127 ;; Open the gateway connection.
4131 (tramp-file-name-method hop
) (tramp-file-name-user hop
)
4132 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop
) nil
))
4133 ;; For the password prompt, we need the correct values.
4134 ;; Therefore, we must remember the gateway vector. But we
4135 ;; cannot do it as connection property, because it shouldn't
4136 ;; be persistent. And we have no started process yet either.
4137 (tramp-set-file-property (car target-alist
) "" "gateway" hop
)))
4139 ;; Foreign and out-of-band methods are not supported for multi-hops.
4140 (when (cdr target-alist
)
4141 (setq choices target-alist
)
4143 (setq item
(pop choices
))
4147 (tramp-get-method-parameter
4148 (tramp-file-name-method item
) 'tramp-login-program
))
4149 (tramp-get-method-parameter
4150 (tramp-file-name-method item
) 'tramp-copy-program
))
4153 "Method `%s' is not supported for multi-hops."
4154 (tramp-file-name-method item
)))))
4156 ;; In case the host name is not used for the remote shell
4157 ;; command, the user could be misguided by applying a random
4159 (let* ((v (car target-alist
))
4160 (method (tramp-file-name-method v
))
4161 (host (tramp-file-name-host v
)))
4164 ;; There are multi-hops.
4166 ;; The host name is used for the remote shell command.
4168 '("%h") (tramp-get-method-parameter method
'tramp-login-args
))
4169 ;; The host is local. We cannot use `tramp-local-host-p'
4170 ;; here, because it opens a connection as well.
4171 (string-match tramp-local-host-regexp host
))
4174 "Host `%s' looks like a remote host, `%s' can only use the local host"
4180 (defun tramp-maybe-open-connection (vec)
4181 "Maybe open a connection VEC.
4182 Does not do anything if a connection is already open, but re-opens the
4183 connection if a previous connection has died for some reason."
4184 (catch 'uname-changed
4185 (let ((p (tramp-get-connection-process vec
))
4186 (process-name (tramp-get-connection-property vec
"process-name" nil
))
4187 (process-environment (copy-sequence process-environment
)))
4189 ;; If too much time has passed since last command was sent, look
4190 ;; whether process is still alive. If it isn't, kill it. When
4191 ;; using ssh, it can sometimes happen that the remote end has
4192 ;; hung up but the local ssh client doesn't recognize this until
4193 ;; it tries to send some data to the remote end. So that's why
4194 ;; we try to send a command from time to time, then look again
4195 ;; whether the process is really alive.
4197 (when (and (> (tramp-time-diff
4199 (tramp-get-connection-property
4200 p
"last-cmd-time" '(0 0 0)))
4202 p
(processp p
) (memq (process-status p
) '(run open
)))
4203 (tramp-send-command vec
"echo are you awake" t t
)
4204 (unless (and (memq (process-status p
) '(run open
))
4205 (tramp-wait-for-output p
10))
4206 ;; The error will be catched locally.
4207 (tramp-error vec
'file-error
"Awake did fail")))
4209 (tramp-flush-connection-property vec
)
4210 (tramp-flush-connection-property p
)
4214 ;; New connection must be opened.
4215 (unless (and p
(processp p
) (memq (process-status p
) '(run open
)))
4217 ;; We call `tramp-get-buffer' in order to get a debug buffer for
4218 ;; messages from the beginning.
4219 (tramp-get-buffer vec
)
4220 (with-progress-reporter
4222 (if (zerop (length (tramp-file-name-user vec
)))
4223 (format "Opening connection for %s using %s"
4224 (tramp-file-name-host vec
)
4225 (tramp-file-name-method vec
))
4226 (format "Opening connection for %s@%s using %s"
4227 (tramp-file-name-user vec
)
4228 (tramp-file-name-host vec
)
4229 (tramp-file-name-method vec
)))
4231 ;; Start new process.
4232 (when (and p
(processp p
))
4234 (setenv "TERM" tramp-terminal-type
)
4235 (setenv "LC_ALL" "C")
4236 (setenv "PROMPT_COMMAND")
4237 (setenv "PS1" tramp-initial-end-of-output
)
4238 (let* ((target-alist (tramp-compute-multi-hops vec
))
4239 (process-connection-type tramp-process-connection-type
)
4240 (process-adaptive-read-buffering nil
)
4241 (coding-system-for-read nil
)
4242 ;; This must be done in order to avoid our file name handler.
4243 (p (let ((default-directory
4244 (tramp-compat-temporary-file-directory)))
4246 (or process-name
(tramp-buffer-name vec
))
4247 (tramp-get-connection-buffer vec
)
4248 tramp-encoding-shell
))))
4251 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
4253 ;; Check whether process is alive.
4254 (tramp-compat-set-process-query-on-exit-flag p nil
)
4255 (tramp-barf-if-no-shell-prompt
4256 p
60 "Couldn't find local shell prompt %s" tramp-encoding-shell
)
4258 ;; Now do all the connections as specified.
4260 (let* ((hop (car target-alist
))
4261 (l-method (tramp-file-name-method hop
))
4262 (l-user (tramp-file-name-user hop
))
4263 (l-host (tramp-file-name-host hop
))
4266 (tramp-get-method-parameter
4267 l-method
'tramp-login-program
))
4269 (tramp-get-method-parameter l-method
'tramp-login-args
))
4271 (tramp-get-method-parameter l-method
'tramp-async-args
))
4273 (tramp-get-method-parameter l-method
'tramp-gw-args
))
4274 (gw (tramp-get-file-property hop
"" "gateway" nil
))
4275 (g-method (and gw
(tramp-file-name-method gw
)))
4276 (g-user (and gw
(tramp-file-name-user gw
)))
4277 (g-host (and gw
(tramp-file-name-host gw
)))
4278 (command login-program
)
4279 ;; We don't create the temporary file. In fact,
4280 ;; it is just a prefix for the ControlPath option
4281 ;; of ssh; the real temporary file has another
4282 ;; name, and it is created and protected by ssh.
4283 ;; It is also removed by ssh, when the connection
4286 (tramp-set-connection-property
4290 tramp-temp-name-prefix
4291 (tramp-compat-temporary-file-directory)))))
4294 ;; Add arguments for asynchrononous processes.
4295 (when (and process-name async-args
)
4296 (setq login-args
(append async-args login-args
)))
4298 ;; Add gateway arguments if necessary.
4299 (when (and gw gw-args
)
4300 (setq login-args
(append gw-args login-args
)))
4302 ;; Check for port number. Until now, there's no need
4303 ;; for handling like method, user, host.
4304 (when (string-match tramp-host-with-port-regexp l-host
)
4305 (setq l-port
(match-string 2 l-host
)
4306 l-host
(match-string 1 l-host
)))
4308 ;; Set variables for computing the prompt for reading
4309 ;; password. They can also be derived from a gateway.
4310 (setq tramp-current-method
(or g-method l-method
)
4311 tramp-current-user
(or g-user l-user
)
4312 tramp-current-host
(or g-host l-host
))
4314 ;; Replace login-args place holders.
4316 l-host
(or l-host
"")
4317 l-user
(or l-user
"")
4318 l-port
(or l-port
"")
4319 spec
(format-spec-make
4320 ?h l-host ?u l-user ?p l-port ?t tmpfile
)
4323 ;; We do not want to see the trailing local prompt in
4324 ;; `start-file-process'.
4325 (unless (memq system-type
'(windows-nt)) "exec ")
4329 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
4330 (unless (member "" x
) (mapconcat 'identity x
" ")))
4332 ;; Local shell could be a Windows COMSPEC. It
4333 ;; doesn't know the ";" syntax, but we must exit
4334 ;; always for `start-file-process'. "exec" does not
4336 (if (memq system-type
'(windows-nt)) " && exit || exit")))
4338 ;; Send the command.
4339 (tramp-message vec
3 "Sending command `%s'" command
)
4340 (tramp-send-command vec command t t
)
4341 (tramp-process-actions p vec tramp-actions-before-shell
60)
4343 vec
3 "Found remote shell prompt on `%s'" l-host
))
4345 (setq target-alist
(cdr target-alist
)))
4347 ;; Make initial shell settings.
4348 (tramp-open-connection-setup-interactive-shell p vec
)))))))
4350 (defun tramp-send-command (vec command
&optional neveropen nooutput
)
4351 "Send the COMMAND to connection VEC.
4352 Erases temporary buffer before sending the command. If optional
4353 arg NEVEROPEN is non-nil, never try to open the connection. This
4354 is meant to be used from `tramp-maybe-open-connection' only. The
4355 function waits for output unless NOOUTPUT is set."
4356 (unless neveropen
(tramp-maybe-open-connection vec
))
4357 (let ((p (tramp-get-connection-process vec
)))
4358 (when (tramp-get-connection-property p
"remote-echo" nil
)
4359 ;; We mark the command string that it can be erased in the output buffer.
4360 (tramp-set-connection-property p
"check-remote-echo" t
)
4361 (setq command
(format "%s%s%s" tramp-echo-mark command tramp-echo-mark
)))
4362 (when (string-match "<<'EOF'" command
)
4363 ;; Unset $PS1 when using here documents, in order not to get
4365 (setq command
(concat "(PS1= ; " command
"\n)")))
4366 ;; Send the command.
4367 (tramp-message vec
6 "%s" command
)
4368 (tramp-send-string vec command
)
4369 (unless nooutput
(tramp-wait-for-output p
))))
4371 (defun tramp-wait-for-output (proc &optional timeout
)
4372 "Wait for output from remote command."
4373 (unless (buffer-live-p (process-buffer proc
))
4374 (delete-process proc
)
4375 (tramp-error proc
'file-error
"Process `%s' not available, try again" proc
))
4376 (with-current-buffer (process-buffer proc
)
4377 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
4378 ;; be leading escape sequences, which must be ignored.
4379 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output
)))
4380 ;; Sometimes, the commands do not return a newline but a
4381 ;; null byte before the shell prompt, for example "git
4382 ;; ls-files -c -z ...".
4383 (regexp1 (format "\\(^\\|\000\\)%s" regexp
))
4384 (found (tramp-wait-for-regexp proc timeout regexp1
)))
4386 (let (buffer-read-only)
4387 ;; A simple-minded busybox has sent " ^H" sequences.
4389 (goto-char (point-min))
4390 (when (re-search-forward
4391 "^\\(.\b\\)+$" (tramp-compat-line-end-position) t
)
4393 (delete-region (point-min) (point)))
4394 ;; Delete the prompt.
4395 (goto-char (point-max))
4396 (re-search-backward regexp nil t
)
4397 (delete-region (point) (point-max)))
4401 "[[Remote prompt `%s' not found in %d secs]]"
4402 tramp-end-of-output timeout
)
4405 "[[Remote prompt `%s' not found]]" tramp-end-of-output
)))
4406 ;; Return value is whether end-of-output sentinel was found.
4409 (defun tramp-send-command-and-check
4410 (vec command
&optional subshell dont-suppress-err
)
4411 "Run COMMAND and check its exit status.
4412 Sends `echo $?' along with the COMMAND for checking the exit status. If
4413 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
4415 If the optional argument SUBSHELL is non-nil, the command is
4416 executed in a subshell, ie surrounded by parentheses. If
4417 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
4420 (concat (if subshell
"( " "")
4422 (if command
(if dont-suppress-err
"; " " 2>/dev/null; ") "")
4423 "echo tramp_exit_status $?"
4424 (if subshell
" )" "")))
4425 (with-current-buffer (tramp-get-connection-buffer vec
)
4426 (goto-char (point-max))
4427 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
4429 vec
'file-error
"Couldn't find exit status of `%s'" command
))
4430 (skip-chars-forward "^ ")
4432 (zerop (read (current-buffer)))
4433 (let (buffer-read-only)
4434 (delete-region (match-beginning 0) (point-max))))))
4436 (defun tramp-barf-unless-okay (vec command fmt
&rest args
)
4437 "Run COMMAND, check exit status, throw error if exit status not okay.
4438 Similar to `tramp-send-command-and-check' but accepts two more arguments
4439 FMT and ARGS which are passed to `error'."
4440 (unless (tramp-send-command-and-check vec command
)
4441 (apply 'tramp-error vec
'file-error fmt args
)))
4443 (defun tramp-send-command-and-read (vec command
)
4444 "Run COMMAND and return the output, which must be a Lisp expression.
4445 In case there is no valid Lisp expression, it raises an error"
4446 (tramp-barf-unless-okay vec command
"`%s' returns with error" command
)
4447 (with-current-buffer (tramp-get-connection-buffer vec
)
4448 ;; Read the expression.
4449 (goto-char (point-min))
4451 (prog1 (read (current-buffer))
4453 (when (re-search-forward "\\S-" (tramp-compat-line-end-position) t
)
4457 "`%s' does not return a valid Lisp expression: `%s'"
4458 command
(buffer-string))))))
4460 (defun tramp-convert-file-attributes (vec attr
)
4461 "Convert file-attributes ATTR generated by perl script, stat or ls.
4462 Convert file mode bits to string and set virtual device number.
4465 ;; Convert last access time.
4466 (unless (listp (nth 4 attr
))
4467 (setcar (nthcdr 4 attr
)
4468 (list (floor (nth 4 attr
) 65536)
4469 (floor (mod (nth 4 attr
) 65536)))))
4470 ;; Convert last modification time.
4471 (unless (listp (nth 5 attr
))
4472 (setcar (nthcdr 5 attr
)
4473 (list (floor (nth 5 attr
) 65536)
4474 (floor (mod (nth 5 attr
) 65536)))))
4475 ;; Convert last status change time.
4476 (unless (listp (nth 6 attr
))
4477 (setcar (nthcdr 6 attr
)
4478 (list (floor (nth 6 attr
) 65536)
4479 (floor (mod (nth 6 attr
) 65536)))))
4480 ;; Convert file size.
4481 (when (< (nth 7 attr
) 0)
4482 (setcar (nthcdr 7 attr
) -
1))
4483 (when (and (floatp (nth 7 attr
))
4484 (<= (nth 7 attr
) (tramp-compat-most-positive-fixnum)))
4485 (setcar (nthcdr 7 attr
) (round (nth 7 attr
))))
4486 ;; Convert file mode bits to string.
4487 (unless (stringp (nth 8 attr
))
4488 (setcar (nthcdr 8 attr
) (tramp-file-mode-from-int (nth 8 attr
)))
4489 (when (stringp (car attr
))
4490 (aset (nth 8 attr
) 0 ?l
)))
4491 ;; Convert directory indication bit.
4492 (when (string-match "^d" (nth 8 attr
))
4494 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
4495 (when (consp (car attr
))
4496 (if (and (stringp (caar attr
))
4497 (string-match ".+ -> .\\(.+\\)." (caar attr
)))
4498 (setcar attr
(match-string 1 (caar attr
)))
4500 ;; Set file's gid change bit.
4501 (setcar (nthcdr 9 attr
)
4502 (if (numberp (nth 3 attr
))
4503 (not (= (nth 3 attr
)
4504 (tramp-get-remote-gid vec
'integer
)))
4507 (tramp-get-remote-gid vec
'string
)))))
4509 (unless (listp (nth 10 attr
))
4510 (setcar (nthcdr 10 attr
)
4512 (cons (floor (nth 10 attr
) 65536)
4513 (floor (mod (nth 10 attr
) 65536)))
4514 ;; Inodes can be incredible huge. We must hide this.
4515 (error (tramp-get-inode vec
)))))
4516 ;; Set virtual device number.
4517 (setcar (nthcdr 11 attr
)
4518 (tramp-get-device vec
))
4521 (defun tramp-check-cached-permissions (vec access
)
4522 "Check `file-attributes' caches for VEC.
4523 Return t if according to the cache access type ACCESS is known to
4529 ((eq ?x access
) 3))))
4530 (dolist (suffix '("string" "integer") result
)
4536 (tramp-get-file-property
4537 vec
(tramp-file-name-localname vec
)
4538 (concat "file-attributes-" suffix
) nil
))
4540 (tramp-get-connection-property
4541 vec
(concat "uid-" suffix
) nil
))
4543 (tramp-get-connection-property
4544 vec
(concat "gid-" suffix
) nil
)))
4549 (eq t
(car file-attr
))
4550 (null (car file-attr
)))
4552 ;; World accessible.
4553 (eq access
(aref (nth 8 file-attr
) (+ offset
6)))
4554 ;; User accessible and owned by user.
4556 (eq access
(aref (nth 8 file-attr
) offset
))
4557 (equal remote-uid
(nth 2 file-attr
)))
4558 ;; Group accessible and owned by user's
4561 (eq access
(aref (nth 8 file-attr
) (+ offset
3)))
4562 (equal remote-gid
(nth 3 file-attr
)))))))))))
4564 (defun tramp-file-mode-from-int (mode)
4565 "Turn an integer representing a file mode into an ls(1)-like string."
4567 (assoc (logand (lsh mode -
12) 15) tramp-file-mode-type-map
)))
4568 (user (logand (lsh mode -
6) 7))
4569 (group (logand (lsh mode -
3) 7))
4570 (other (logand (lsh mode -
0) 7))
4571 (suid (> (logand (lsh mode -
9) 4) 0))
4572 (sgid (> (logand (lsh mode -
9) 2) 0))
4573 (sticky (> (logand (lsh mode -
9) 1) 0)))
4574 (setq user
(tramp-file-mode-permissions user suid
"s"))
4575 (setq group
(tramp-file-mode-permissions group sgid
"s"))
4576 (setq other
(tramp-file-mode-permissions other sticky
"t"))
4577 (concat type user group other
)))
4579 (defun tramp-file-mode-permissions (perm suid suid-text
)
4580 "Convert a permission bitset into a string.
4581 This is used internally by `tramp-file-mode-from-int'."
4582 (let ((r (> (logand perm
4) 0))
4583 (w (> (logand perm
2) 0))
4584 (x (> (logand perm
1) 0)))
4585 (concat (or (and r
"r") "-")
4586 (or (and w
"w") "-")
4587 (or (and suid x suid-text
) ; suid, execute
4588 (and suid
(upcase suid-text
)) ; suid, !execute
4589 (and x
"x") "-")))) ; !suid
4591 (defun tramp-shell-case-fold (string)
4592 "Converts STRING to shell glob pattern which ignores case."
4595 (if (equal (downcase c
) (upcase c
))
4597 (format "[%c%c]" (downcase c
) (upcase c
))))
4601 (defun tramp-make-copy-program-file-name (vec)
4602 "Create a file name suitable to be passed to `rcp' and workalikes."
4603 (let ((user (tramp-file-name-user vec
))
4604 (host (tramp-file-name-real-host vec
))
4605 (localname (tramp-shell-quote-argument
4606 (tramp-file-name-localname vec
))))
4607 (if (not (zerop (length user
)))
4608 (format "%s@%s:%s" user host localname
)
4609 (format "%s:%s" host localname
))))
4611 (defun tramp-method-out-of-band-p (vec size
)
4612 "Return t if this is an out-of-band method, nil otherwise."
4614 ;; It shall be an out-of-band method.
4615 (tramp-get-method-parameter (tramp-file-name-method vec
) 'tramp-copy-program
)
4616 ;; Either the file size is large enough, or (in rare cases) there
4617 ;; does not exist a remote encoding.
4618 (or (null tramp-copy-size-limit
)
4619 (> size tramp-copy-size-limit
)
4620 (null (tramp-get-inline-coding vec
"remote-encoding" size
)))))
4622 ;; Variables local to connection.
4624 (defun tramp-get-remote-path (vec)
4625 (with-connection-property
4626 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
4627 ;; cache the result for the session only. Otherwise, the result
4628 ;; is cached persistently.
4629 (if (memq 'tramp-own-remote-path tramp-remote-path
)
4630 (tramp-get-connection-process vec
)
4633 (let* ((remote-path (copy-tree tramp-remote-path
))
4634 (elt1 (memq 'tramp-default-remote-path remote-path
))
4635 (elt2 (memq 'tramp-own-remote-path remote-path
))
4636 (default-remote-path
4639 (tramp-send-command-and-read
4640 vec
"echo \\\"`getconf PATH`\\\"")
4641 ;; Default if "getconf" is not available.
4645 "`getconf PATH' not successful, using default value \"%s\"."
4651 (tramp-send-command-and-read vec
"echo \\\"$PATH\\\"")
4652 ;; Default if "getconf" is not available.
4655 vec
3 "$PATH not set, ignoring `tramp-own-remote-path'.")
4658 ;; Replace place holder `tramp-default-remote-path'.
4662 (tramp-compat-split-string default-remote-path
":")
4664 (setq remote-path
(delq 'tramp-default-remote-path remote-path
)))
4666 ;; Replace place holder `tramp-own-remote-path'.
4670 (tramp-compat-split-string own-remote-path
":")
4672 (setq remote-path
(delq 'tramp-own-remote-path remote-path
)))
4674 ;; Remove double entries.
4675 (setq elt1 remote-path
)
4677 (while (and (car elt1
) (setq elt2
(member (car elt1
) (cdr elt1
))))
4679 (setq elt1
(cdr elt1
)))
4681 ;; Remove non-existing directories.
4689 (tramp-make-tramp-file-name
4690 (tramp-file-name-method vec
)
4691 (tramp-file-name-user vec
)
4692 (tramp-file-name-host vec
)
4697 (defun tramp-get-remote-tmpdir (vec)
4698 (with-connection-property vec
"tmp-directory"
4699 (let ((dir (tramp-shell-quote-argument "/tmp")))
4700 (if (and (tramp-send-command-and-check
4701 vec
(format "%s -d %s" (tramp-get-test-command vec
) dir
))
4702 (tramp-send-command-and-check
4703 vec
(format "%s -w %s" (tramp-get-test-command vec
) dir
)))
4705 (tramp-error vec
'file-error
"Directory %s not accessible" dir
)))))
4707 (defun tramp-get-ls-command (vec)
4708 (with-connection-property vec
"ls"
4709 (tramp-message vec
5 "Finding a suitable `ls' command")
4712 (dolist (cmd '("ls" "gnuls" "gls"))
4713 (let ((dl (tramp-get-remote-path vec
))
4715 (while (and dl
(setq result
(tramp-find-executable vec cmd dl t t
)))
4716 ;; Check parameters. On busybox, "ls" output coloring is
4717 ;; enabled by default sometimes. So we try to disable it
4718 ;; when possible. $LS_COLORING is not supported there.
4719 ;; Some "ls" versions are sensible wrt the order of
4720 ;; arguments, they fail when "-al" is after the
4721 ;; "--color=never" argument (for example on FreeBSD).
4722 (when (tramp-send-command-and-check
4723 vec
(format "%s -lnd /" result
))
4724 (when (tramp-send-command-and-check
4726 "%s --color=never -al /dev/null" result
))
4727 (setq result
(concat result
" --color=never")))
4728 (throw 'ls-found result
))
4729 (setq dl
(cdr dl
))))))
4730 (tramp-error vec
'file-error
"Couldn't find a proper `ls' command"))))
4732 (defun tramp-get-ls-command-with-dired (vec)
4734 (with-connection-property vec
"ls-dired"
4735 (tramp-message vec
5 "Checking, whether `ls --dired' works")
4736 ;; Some "ls" versions are sensible wrt the order of arguments,
4737 ;; they fail when "-al" is after the "--dired" argument (for
4738 ;; example on FreeBSD).
4739 (tramp-send-command-and-check
4740 vec
(format "%s --dired -al /dev/null" (tramp-get-ls-command vec
))))))
4742 (defun tramp-get-test-command (vec)
4743 (with-connection-property vec
"test"
4744 (tramp-message vec
5 "Finding a suitable `test' command")
4745 (if (tramp-send-command-and-check vec
"test 0")
4747 (tramp-find-executable vec
"test" (tramp-get-remote-path vec
)))))
4749 (defun tramp-get-test-nt-command (vec)
4750 ;; Does `test A -nt B' work? Use abominable `find' construct if it
4751 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
4752 ;; for otherwise the shell crashes.
4753 (with-connection-property vec
"test-nt"
4757 vec
(format "( %s / -nt / )" (tramp-get-test-command vec
)))
4758 (with-current-buffer (tramp-get-buffer vec
)
4759 (goto-char (point-min))
4760 (when (looking-at (regexp-quote tramp-end-of-output
))
4761 (format "%s %%s -nt %%s" (tramp-get-test-command vec
)))))
4766 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
4767 (tramp-get-test-command vec
)))
4768 "tramp_test_nt %s %s"))))
4770 (defun tramp-get-file-exists-command (vec)
4771 (with-connection-property vec
"file-exists"
4772 (tramp-message vec
5 "Finding command to check if file exists")
4773 (tramp-find-file-exists-command vec
)))
4775 (defun tramp-get-remote-ln (vec)
4776 (with-connection-property vec
"ln"
4777 (tramp-message vec
5 "Finding a suitable `ln' command")
4778 (tramp-find-executable vec
"ln" (tramp-get-remote-path vec
))))
4780 (defun tramp-get-remote-perl (vec)
4781 (with-connection-property vec
"perl"
4782 (tramp-message vec
5 "Finding a suitable `perl' command")
4784 (or (tramp-find-executable vec
"perl5" (tramp-get-remote-path vec
))
4785 (tramp-find-executable
4786 vec
"perl" (tramp-get-remote-path vec
)))))
4787 ;; We must check also for some Perl modules.
4789 (with-connection-property vec
"perl-file-spec"
4790 (tramp-send-command-and-check
4791 vec
(format "%s -e 'use File::Spec;'" result
)))
4792 (with-connection-property vec
"perl-cwd-realpath"
4793 (tramp-send-command-and-check
4794 vec
(format "%s -e 'use Cwd \"realpath\";'" result
))))
4797 (defun tramp-get-remote-stat (vec)
4798 (with-connection-property vec
"stat"
4799 (tramp-message vec
5 "Finding a suitable `stat' command")
4800 (let ((result (tramp-find-executable
4801 vec
"stat" (tramp-get-remote-path vec
)))
4803 ;; Check whether stat(1) returns usable syntax. %s does not
4804 ;; work on older AIX systems.
4807 ;; We don't want to display an error message.
4808 (tramp-compat-with-temp-message (or (current-message) "")
4810 (tramp-send-command-and-read
4811 vec
(format "%s -c '(\"%%N\" %%s)' /" result
)))))
4812 (unless (and (listp tmp
) (stringp (car tmp
))
4813 (string-match "^./.$" (car tmp
))
4814 (integerp (cadr tmp
)))
4818 (defun tramp-get-remote-readlink (vec)
4819 (with-connection-property vec
"readlink"
4820 (tramp-message vec
5 "Finding a suitable `readlink' command")
4821 (let ((result (tramp-find-executable
4822 vec
"readlink" (tramp-get-remote-path vec
))))
4824 ;; We don't want to display an error message.
4825 (tramp-compat-with-temp-message (or (current-message) "")
4827 (tramp-send-command-and-check
4828 vec
(format "%s --canonicalize-missing /" result
)))))
4831 (defun tramp-get-remote-trash (vec)
4832 (with-connection-property vec
"trash"
4833 (tramp-message vec
5 "Finding a suitable `trash' command")
4834 (tramp-find-executable vec
"trash" (tramp-get-remote-path vec
))))
4836 (defun tramp-get-remote-id (vec)
4837 (with-connection-property vec
"id"
4838 (tramp-message vec
5 "Finding POSIX `id' command")
4841 (let ((dl (tramp-get-remote-path vec
))
4843 (while (and dl
(setq result
(tramp-find-executable vec
"id" dl t t
)))
4844 ;; Check POSIX parameter.
4845 (when (tramp-send-command-and-check vec
(format "%s -u" result
))
4846 (throw 'id-found result
))
4847 (setq dl
(cdr dl
)))))
4848 (tramp-error vec
'file-error
"Couldn't find a POSIX `id' command"))))
4850 (defun tramp-get-remote-uid (vec id-format
)
4851 (with-connection-property vec
(format "uid-%s" id-format
)
4852 (let ((res (tramp-send-command-and-read
4854 (format "%s -u%s %s"
4855 (tramp-get-remote-id vec
)
4856 (if (equal id-format
'integer
) "" "n")
4857 (if (equal id-format
'integer
)
4858 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
4859 ;; The command might not always return a number.
4860 (if (and (equal id-format
'integer
) (not (integerp res
))) -
1 res
))))
4862 (defun tramp-get-remote-gid (vec id-format
)
4863 (with-connection-property vec
(format "gid-%s" id-format
)
4864 (let ((res (tramp-send-command-and-read
4866 (format "%s -g%s %s"
4867 (tramp-get-remote-id vec
)
4868 (if (equal id-format
'integer
) "" "n")
4869 (if (equal id-format
'integer
)
4870 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
4871 ;; The command might not always return a number.
4872 (if (and (equal id-format
'integer
) (not (integerp res
))) -
1 res
))))
4874 (defun tramp-get-local-uid (id-format)
4875 (if (equal id-format
'integer
) (user-uid) (user-login-name)))
4877 (defun tramp-get-local-gid (id-format)
4878 (nth 3 (tramp-compat-file-attributes "~/" id-format
)))
4880 ;; Some predefined connection properties.
4881 (defun tramp-get-inline-compress (vec prop size
)
4882 "Return the compress command related to PROP.
4883 PROP is either `inline-compress' or `inline-decompress'. SIZE is
4884 the length of the file to be compressed.
4886 If no corresponding command is found, nil is returned."
4887 (when (and (integerp tramp-inline-compress-start-size
)
4888 (> size tramp-inline-compress-start-size
))
4889 (with-connection-property vec prop
4890 (tramp-find-inline-compress vec
)
4891 (tramp-get-connection-property vec prop nil
))))
4893 (defun tramp-get-inline-coding (vec prop size
)
4894 "Return the coding command related to PROP.
4895 PROP is either `remote-encoding', `remode-decoding',
4896 `local-encoding' or `local-decoding'.
4898 SIZE is the length of the file to be coded. Depending on SIZE,
4899 compression might be applied.
4901 If no corresponding command is found, nil is returned.
4902 Otherwise, either a string is returned which contains a `%s' mark
4903 to be used for the respective input or output file; or a Lisp
4904 function cell is returned to be applied on a buffer."
4905 ;; We must catch the errors, because we want to return `nil', when
4906 ;; no inline coding is found.
4909 (with-connection-property vec prop
4910 (tramp-find-inline-encoding vec
)
4911 (tramp-get-connection-property vec prop nil
)))
4912 (prop1 (if (string-match "encoding" prop
)
4913 "inline-compress" "inline-decompress"))
4915 ;; The connection property might have been cached. So we must
4916 ;; send the script to the remote side - maybe.
4917 (when (and coding
(symbolp coding
) (string-match "remote" prop
))
4918 (let ((name (symbol-name coding
)))
4919 (while (string-match (regexp-quote "-") name
)
4920 (setq name
(replace-match "_" nil t name
)))
4921 (tramp-maybe-send-script vec
(symbol-value coding
) name
)
4922 (setq coding name
)))
4924 ;; Check for the `compress' command.
4925 (setq compress
(tramp-get-inline-compress vec prop1 size
))
4926 ;; Return the value.
4928 ((and compress
(symbolp coding
))
4929 (if (string-match "decompress" prop1
)
4932 (let ((coding-system-for-write 'binary
)
4933 (coding-system-for-read 'binary
))
4935 'call-process-region
(point-min) (point-max)
4936 (car (split-string ,compress
)) t t nil
4937 (cdr (split-string ,compress
)))))
4939 (let ((coding-system-for-write 'binary
)
4940 (coding-system-for-read 'binary
))
4942 'call-process-region beg end
4943 (car (split-string ,compress
)) t t nil
4944 (cdr (split-string ,compress
))))
4945 (,coding
(point-min) (point-max)))))
4948 ((and compress
(string-match "decoding" prop
))
4949 (format "(%s | %s >%%s)" coding compress
))
4951 (format "(%s <%%s | %s)" compress coding
))
4952 ((string-match "decoding" prop
)
4953 (format "%s >%%s" coding
))
4955 (format "%s <%%s" coding
)))))))
4957 ;;; Integration of eshell.el:
4960 (defvar eshell-path-env
))
4962 ;; eshell.el keeps the path in `eshell-path-env'. We must change it
4963 ;; when `default-directory' points to another host.
4964 (defun tramp-eshell-directory-change ()
4965 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
4966 (setq eshell-path-env
4967 (if (file-remote-p default-directory
)
4968 (with-parsed-tramp-file-name default-directory nil
4971 (tramp-get-remote-path v
)
4975 (eval-after-load "esh-util"
4977 (tramp-eshell-directory-change)
4978 (add-hook 'eshell-directory-change-hook
4979 'tramp-eshell-directory-change
)
4980 (add-hook 'tramp-unload-hook
4982 (remove-hook 'eshell-directory-change-hook
4983 'tramp-eshell-directory-change
)))))
4985 (add-hook 'tramp-unload-hook
4987 (unload-feature 'tramp-sh
'force
)))
4993 ;; * Don't use globbing for directories with many files, as this is
4994 ;; likely to produce long command lines, and some shells choke on
4995 ;; long command lines.
4996 ;; * Make it work for different encodings, and for different file name
4997 ;; encodings, too. (Daniel Pittman)
4998 ;; * Don't search for perl5 and perl. Instead, only search for perl and
4999 ;; then look if it's the right version (with `perl -v').
5000 ;; * When editing a remote CVS controlled file as a different user, VC
5001 ;; gets confused about the file locking status. Try to find out why
5002 ;; the workaround doesn't work.
5003 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5004 ;; until the last but one hop via `start-file-process'. Apply it
5005 ;; also for ftp and smb.
5006 ;; * WIBNI if we had a command "trampclient"? If I was editing in
5007 ;; some shell with root priviledges, it would be nice if I could
5009 ;; trampclient filename.c
5010 ;; as an editor, and the _current_ shell would connect to an Emacs
5011 ;; server and would be used in an existing non-priviledged Emacs
5012 ;; session for doing the editing in question.
5013 ;; That way, I need not tell Emacs my password again and be afraid
5014 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
5015 ;; once display a just typed password in the context of a keyboard
5016 ;; sequence prompt for a question immediately following in a shell
5017 ;; script run within Emacs -- nasty).
5018 ;; And if I have some ssh session running to a different computer,
5019 ;; having the possibility of passing a local file there to a local
5020 ;; Emacs session (in case I can arrange for a connection back) would
5022 ;; Likely the corresponding Tramp server should not allow the
5023 ;; equivalent of the emacsclient -eval option in order to make this
5024 ;; reasonably unproblematic. And maybe trampclient should have some
5025 ;; way of passing credentials, like by using an SSL socket or
5026 ;; something. (David Kastrup)
5027 ;; * Reconnect directly to a compliant shell without first going
5028 ;; through the user's default shell. (Pete Forman)
5029 ;; * How can I interrupt the remote process with a signal
5030 ;; (interrupt-process seems not to work)? (Markus Triska)
5031 ;; * Avoid the local shell entirely for starting remote processes. If
5032 ;; so, I think even a signal, when delivered directly to the local
5033 ;; SSH instance, would correctly be propagated to the remote process
5034 ;; automatically; possibly SSH would have to be started with
5035 ;; "-t". (Markus Triska)
5036 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5037 ;; isn't on the remote host. (Mark A. Hershberger)
5038 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
5039 ;; * Optimize out-of-band copying, when both methods are scp-like (not
5041 ;; * Keep a second connection open for out-of-band methods like scp or
5043 ;; * Try telnet+curl as new method. It might be useful for busybox,
5044 ;; without built-in uuencode/uudecode.
5046 ;;; tramp-sh.el ends here