]> code.delx.au - gnu-emacs/blob - lisp/net/tramp-sh.el
Merge branch 'emacs-24'.
[gnu-emacs] / lisp / net / tramp-sh.el
1 ;;; tramp-sh.el --- Tramp access functions for (s)sh-like connections
2
3 ;; Copyright (C) 1998-2014 Free Software Foundation, Inc.
4
5 ;; (copyright statements below in code to be updated with the above notice)
6
7 ;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
8 ;; Michael Albinus <michael.albinus@gmx.de>
9 ;; Keywords: comm, processes
10 ;; Package: tramp
11
12 ;; This file is part of GNU Emacs.
13
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
18
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
23
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
26
27 ;;; Code:
28
29 (require 'tramp)
30
31 ;; Pacify byte-compiler.
32 (eval-when-compile
33 (require 'cl)
34 (require 'dired))
35 (defvar directory-sep-char)
36 (defvar tramp-gw-tunnel-method)
37 (defvar tramp-gw-socks-method)
38 (defvar vc-handled-backends)
39 (defvar vc-bzr-program)
40 (defvar vc-git-program)
41 (defvar vc-hg-program)
42
43 (defcustom tramp-inline-compress-start-size 4096
44 "The minimum size of compressing where inline transfer.
45 When inline transfer, compress transferred data of file
46 whose size is this value or above (up to `tramp-copy-size-limit').
47 If it is nil, no compression at all will be applied."
48 :group 'tramp
49 :type '(choice (const nil) integer))
50
51 (defcustom tramp-copy-size-limit 10240
52 "The maximum file size where inline copying is preferred over an \
53 out-of-the-band copy.
54 If it is nil, out-of-the-band copy will be used without a check."
55 :group 'tramp
56 :type '(choice (const nil) integer))
57
58 ;;;###tramp-autoload
59 (defcustom tramp-terminal-type "dumb"
60 "Value of TERM environment variable for logging in to remote host.
61 Because Tramp wants to parse the output of the remote shell, it is easily
62 confused by ANSI color escape sequences and suchlike. Often, shell init
63 files conditionalize this setup based on the TERM environment variable."
64 :group 'tramp
65 :type 'string)
66
67 ;;;###tramp-autoload
68 (defconst tramp-color-escape-sequence-regexp "\e[[;0-9]+m"
69 "Escape sequences produced by the \"ls\" command.")
70
71 ;; ksh on OpenBSD 4.5 requires that $PS1 contains a `#' character for
72 ;; root users. It uses the `$' character for other users. In order
73 ;; to guarantee a proper prompt, we use "#$ " for the prompt.
74
75 (defvar tramp-end-of-output
76 (format
77 "///%s#$"
78 (md5 (concat (prin1-to-string process-environment) (current-time-string))))
79 "String used to recognize end of output.
80 The '$' character at the end is quoted; the string cannot be
81 detected as prompt when being sent on echoing hosts, therefore.")
82
83 ;;;###tramp-autoload
84 (defconst tramp-initial-end-of-output "#$ "
85 "Prompt when establishing a connection.")
86
87 (defconst tramp-end-of-heredoc (md5 tramp-end-of-output)
88 "String used to recognize end of heredoc strings.")
89
90 ;; Initialize `tramp-methods' with the supported methods.
91 ;;;###tramp-autoload
92 (add-to-list 'tramp-methods
93 '("rcp"
94 (tramp-login-program "rsh")
95 (tramp-login-args (("%h") ("-l" "%u")))
96 (tramp-remote-shell "/bin/sh")
97 (tramp-remote-shell-args ("-c"))
98 (tramp-copy-program "rcp")
99 (tramp-copy-args (("-p" "%k") ("-r")))
100 (tramp-copy-keep-date t)
101 (tramp-copy-recursive t)))
102 ;;;###tramp-autoload
103 (add-to-list 'tramp-methods
104 '("remcp"
105 (tramp-login-program "remsh")
106 (tramp-login-args (("%h") ("-l" "%u")))
107 (tramp-remote-shell "/bin/sh")
108 (tramp-remote-shell-args ("-c"))
109 (tramp-copy-program "rcp")
110 (tramp-copy-args (("-p" "%k")))
111 (tramp-copy-keep-date t)))
112 ;;;###tramp-autoload
113 (add-to-list 'tramp-methods
114 '("scp"
115 (tramp-login-program "ssh")
116 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
117 ("-e" "none") ("%h")))
118 (tramp-async-args (("-q")))
119 (tramp-remote-shell "/bin/sh")
120 (tramp-remote-shell-args ("-c"))
121 (tramp-copy-program "scp")
122 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r") ("%c")))
123 (tramp-copy-keep-date t)
124 (tramp-copy-recursive t)
125 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
126 ("-o" "UserKnownHostsFile=/dev/null")
127 ("-o" "StrictHostKeyChecking=no")))
128 (tramp-default-port 22)))
129 ;;;###tramp-autoload
130 (add-to-list 'tramp-methods
131 '("scpx"
132 (tramp-login-program "ssh")
133 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
134 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
135 (tramp-async-args (("-q")))
136 (tramp-remote-shell "/bin/sh")
137 (tramp-remote-shell-args ("-c"))
138 (tramp-copy-program "scp")
139 (tramp-copy-args (("-P" "%p") ("-p" "%k")
140 ("-q") ("-r") ("%c")))
141 (tramp-copy-keep-date t)
142 (tramp-copy-recursive t)
143 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
144 ("-o" "UserKnownHostsFile=/dev/null")
145 ("-o" "StrictHostKeyChecking=no")))
146 (tramp-default-port 22)))
147 ;;;###tramp-autoload
148 (add-to-list 'tramp-methods
149 '("rsync"
150 (tramp-login-program "ssh")
151 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
152 ("-e" "none") ("%h")))
153 (tramp-async-args (("-q")))
154 (tramp-remote-shell "/bin/sh")
155 (tramp-remote-shell-args ("-c"))
156 (tramp-copy-program "rsync")
157 (tramp-copy-args (("-t" "%k") ("-r")))
158 (tramp-copy-env (("RSYNC_RSH") ("ssh" "%c")))
159 (tramp-copy-keep-date t)
160 (tramp-copy-keep-tmpfile t)
161 (tramp-copy-recursive t)))
162 ;;;###tramp-autoload
163 (add-to-list 'tramp-methods
164 '("rsh"
165 (tramp-login-program "rsh")
166 (tramp-login-args (("%h") ("-l" "%u")))
167 (tramp-remote-shell "/bin/sh")
168 (tramp-remote-shell-args ("-c"))))
169 ;;;###tramp-autoload
170 (add-to-list 'tramp-methods
171 '("remsh"
172 (tramp-login-program "remsh")
173 (tramp-login-args (("%h") ("-l" "%u")))
174 (tramp-remote-shell "/bin/sh")
175 (tramp-remote-shell-args ("-c"))))
176 ;;;###tramp-autoload
177 (add-to-list 'tramp-methods
178 '("ssh"
179 (tramp-login-program "ssh")
180 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
181 ("-e" "none") ("%h")))
182 (tramp-async-args (("-q")))
183 (tramp-remote-shell "/bin/sh")
184 (tramp-remote-shell-args ("-c"))
185 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
186 ("-o" "UserKnownHostsFile=/dev/null")
187 ("-o" "StrictHostKeyChecking=no")))
188 (tramp-default-port 22)))
189 ;;;###tramp-autoload
190 (add-to-list 'tramp-methods
191 '("sshx"
192 (tramp-login-program "ssh")
193 (tramp-login-args (("-l" "%u") ("-p" "%p") ("%c")
194 ("-e" "none") ("-t" "-t") ("%h") ("/bin/sh")))
195 (tramp-async-args (("-q")))
196 (tramp-remote-shell "/bin/sh")
197 (tramp-remote-shell-args ("-c"))
198 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
199 ("-o" "UserKnownHostsFile=/dev/null")
200 ("-o" "StrictHostKeyChecking=no")))
201 (tramp-default-port 22)))
202 ;;;###tramp-autoload
203 (add-to-list 'tramp-methods
204 '("telnet"
205 (tramp-login-program "telnet")
206 (tramp-login-args (("%h") ("%p") ("2>/dev/null")))
207 (tramp-remote-shell "/bin/sh")
208 (tramp-remote-shell-args ("-c"))
209 (tramp-default-port 23)))
210 ;;;###tramp-autoload
211 (add-to-list 'tramp-methods
212 '("nc"
213 (tramp-login-program "telnet")
214 (tramp-login-args (("%h") ("%p") ("2>/dev/null")))
215 (tramp-remote-shell "/bin/sh")
216 (tramp-remote-shell-args ("-c"))
217 (tramp-copy-program "nc")
218 ;; We use "-v" for better error tracking.
219 (tramp-copy-args (("-w" "1") ("-v") ("%h") ("%r")))
220 (tramp-remote-copy-program "nc")
221 ;; We use "-p" as required for newer busyboxes. For older
222 ;; busybox/nc versions, the value must be (("-l") ("%r")). This
223 ;; can be achieved by tweaking `tramp-connection-properties'.
224 (tramp-remote-copy-args (("-l") ("-p" "%r")))
225 (tramp-default-port 23)))
226 ;;;###tramp-autoload
227 (add-to-list 'tramp-methods
228 '("su"
229 (tramp-login-program "su")
230 (tramp-login-args (("-") ("%u")))
231 (tramp-remote-shell "/bin/sh")
232 (tramp-remote-shell-args ("-c"))
233 (tramp-connection-timeout 10)))
234 ;;;###tramp-autoload
235 (add-to-list 'tramp-methods
236 '("sudo"
237 (tramp-login-program "sudo")
238 (tramp-login-args (("-u" "%u") ("-s") ("-H") ("-p" "Password:")))
239 ;; Local $SHELL could be a nasty one, like zsh or fish. Let's override it.
240 (tramp-login-env (("SHELL") ("/bin/sh")))
241 (tramp-remote-shell "/bin/sh")
242 (tramp-remote-shell-args ("-c"))
243 (tramp-connection-timeout 10)))
244 ;;;###tramp-autoload
245 (add-to-list 'tramp-methods
246 '("ksu"
247 (tramp-login-program "ksu")
248 (tramp-login-args (("%u") ("-q")))
249 (tramp-remote-shell "/bin/sh")
250 (tramp-remote-shell-args ("-c"))
251 (tramp-connection-timeout 10)))
252 ;;;###tramp-autoload
253 (add-to-list 'tramp-methods
254 '("krlogin"
255 (tramp-login-program "krlogin")
256 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
257 (tramp-remote-shell "/bin/sh")
258 (tramp-remote-shell-args ("-c"))))
259 ;;;###tramp-autoload
260 (add-to-list 'tramp-methods
261 `("plink"
262 (tramp-login-program "plink")
263 ;; ("%h") must be a single element, see `tramp-compute-multi-hops'.
264 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
265 ("%h") ("\"")
266 (,(format
267 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
268 tramp-terminal-type
269 tramp-initial-end-of-output))
270 ("/bin/sh") ("\"")))
271 (tramp-remote-shell "/bin/sh")
272 (tramp-remote-shell-args ("-c"))
273 (tramp-default-port 22)))
274 ;;;###tramp-autoload
275 (add-to-list 'tramp-methods
276 `("plinkx"
277 (tramp-login-program "plink")
278 (tramp-login-args (("-load") ("%h") ("-t") ("\"")
279 (,(format
280 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
281 tramp-terminal-type
282 tramp-initial-end-of-output))
283 ("/bin/sh") ("\"")))
284 (tramp-remote-shell "/bin/sh")
285 (tramp-remote-shell-args ("-c"))))
286 ;;;###tramp-autoload
287 (add-to-list 'tramp-methods
288 `("pscp"
289 (tramp-login-program "plink")
290 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
291 ("%h") ("\"")
292 (,(format
293 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
294 tramp-terminal-type
295 tramp-initial-end-of-output))
296 ("/bin/sh") ("\"")))
297 (tramp-remote-shell "/bin/sh")
298 (tramp-remote-shell-args ("-c"))
299 (tramp-copy-program "pscp")
300 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-scp") ("-p" "%k")
301 ("-q") ("-r")))
302 (tramp-copy-keep-date t)
303 (tramp-copy-recursive t)
304 (tramp-default-port 22)))
305 ;;;###tramp-autoload
306 (add-to-list 'tramp-methods
307 `("psftp"
308 (tramp-login-program "plink")
309 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("-t")
310 ("%h") ("\"")
311 (,(format
312 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
313 tramp-terminal-type
314 tramp-initial-end-of-output))
315 ("/bin/sh") ("\"")))
316 (tramp-remote-shell "/bin/sh")
317 (tramp-remote-shell-args ("-c"))
318 (tramp-copy-program "pscp")
319 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-sftp") ("-p" "%k")
320 ("-q") ("-r")))
321 (tramp-copy-keep-date t)
322 (tramp-copy-recursive t)))
323 ;;;###tramp-autoload
324 (add-to-list 'tramp-methods
325 '("fcp"
326 (tramp-login-program "fsh")
327 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
328 (tramp-remote-shell "/bin/sh")
329 (tramp-remote-shell-args ("-i") ("-c"))
330 (tramp-copy-program "fcp")
331 (tramp-copy-args (("-p" "%k")))
332 (tramp-copy-keep-date t)))
333
334 ;;;###tramp-autoload
335 (add-to-list 'tramp-default-method-alist
336 `(,tramp-local-host-regexp "\\`root\\'" "su"))
337
338 ;;;###tramp-autoload
339 (add-to-list 'tramp-default-user-alist
340 `(,(concat "\\`" (regexp-opt '("su" "sudo" "ksu")) "\\'")
341 nil "root"))
342 ;; Do not add "ssh" based methods, otherwise ~/.ssh/config would be ignored.
343 ;; Do not add "plink" based methods, they ask interactively for the user.
344 ;;;###tramp-autoload
345 (add-to-list 'tramp-default-user-alist
346 `(,(concat
347 "\\`"
348 (regexp-opt
349 '("rcp" "remcp" "rsh" "telnet" "nc" "krlogin" "fcp"))
350 "\\'")
351 nil ,(user-login-name)))
352
353 ;;;###tramp-autoload
354 (defconst tramp-completion-function-alist-rsh
355 '((tramp-parse-rhosts "/etc/hosts.equiv")
356 (tramp-parse-rhosts "~/.rhosts"))
357 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
358
359 ;;;###tramp-autoload
360 (defconst tramp-completion-function-alist-ssh
361 '((tramp-parse-rhosts "/etc/hosts.equiv")
362 (tramp-parse-rhosts "/etc/shosts.equiv")
363 (tramp-parse-shosts "/etc/ssh_known_hosts")
364 (tramp-parse-sconfig "/etc/ssh_config")
365 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
366 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
367 (tramp-parse-rhosts "~/.rhosts")
368 (tramp-parse-rhosts "~/.shosts")
369 (tramp-parse-shosts "~/.ssh/known_hosts")
370 (tramp-parse-sconfig "~/.ssh/config")
371 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
372 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
373 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
374
375 ;;;###tramp-autoload
376 (defconst tramp-completion-function-alist-telnet
377 '((tramp-parse-hosts "/etc/hosts"))
378 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
379
380 ;;;###tramp-autoload
381 (defconst tramp-completion-function-alist-su
382 '((tramp-parse-passwd "/etc/passwd"))
383 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
384
385 ;;;###tramp-autoload
386 (defconst tramp-completion-function-alist-putty
387 `((tramp-parse-putty
388 ,(if (memq system-type '(windows-nt))
389 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"
390 "~/.putty/sessions")))
391 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty sessions.")
392
393 ;;;###tramp-autoload
394 (eval-after-load 'tramp
395 '(progn
396 (tramp-set-completion-function "rcp" tramp-completion-function-alist-rsh)
397 (tramp-set-completion-function "remcp" tramp-completion-function-alist-rsh)
398 (tramp-set-completion-function "scp" tramp-completion-function-alist-ssh)
399 (tramp-set-completion-function "scpx" tramp-completion-function-alist-ssh)
400 (tramp-set-completion-function "rsync" tramp-completion-function-alist-ssh)
401 (tramp-set-completion-function "rsh" tramp-completion-function-alist-rsh)
402 (tramp-set-completion-function "remsh" tramp-completion-function-alist-rsh)
403 (tramp-set-completion-function "ssh" tramp-completion-function-alist-ssh)
404 (tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh)
405 (tramp-set-completion-function
406 "telnet" tramp-completion-function-alist-telnet)
407 (tramp-set-completion-function "nc" tramp-completion-function-alist-telnet)
408 (tramp-set-completion-function "su" tramp-completion-function-alist-su)
409 (tramp-set-completion-function "sudo" tramp-completion-function-alist-su)
410 (tramp-set-completion-function "ksu" tramp-completion-function-alist-su)
411 (tramp-set-completion-function
412 "krlogin" tramp-completion-function-alist-rsh)
413 (tramp-set-completion-function "plink" tramp-completion-function-alist-ssh)
414 (tramp-set-completion-function
415 "plinkx" tramp-completion-function-alist-putty)
416 (tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh)
417 (tramp-set-completion-function "psftp" tramp-completion-function-alist-ssh)
418 (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh)))
419
420 ;; "getconf PATH" yields:
421 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
422 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
423 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
424 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
425 ;; IRIX64: /usr/bin
426 ;;;###tramp-autoload
427 (defcustom tramp-remote-path
428 '(tramp-default-remote-path "/bin" "/usr/bin" "/sbin" "/usr/sbin"
429 "/usr/local/bin" "/usr/local/sbin" "/local/bin" "/local/freeware/bin"
430 "/local/gnu/bin" "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin"
431 "/opt/bin" "/opt/sbin" "/opt/local/bin")
432 "List of directories to search for executables on remote host.
433 For every remote host, this variable will be set buffer local,
434 keeping the list of existing directories on that host.
435
436 You can use `~' in this list, but when searching for a shell which groks
437 tilde expansion, all directory names starting with `~' will be ignored.
438
439 `Default Directories' represent the list of directories given by
440 the command \"getconf PATH\". It is recommended to use this
441 entry on top of this list, because these are the default
442 directories for POSIX compatible commands. On remote hosts which
443 do not offer the getconf command (like cygwin), the value
444 \"/bin:/usr/bin\" is used instead of.
445
446 `Private Directories' are the settings of the $PATH environment,
447 as given in your `~/.profile'."
448 :group 'tramp
449 :type '(repeat (choice
450 (const :tag "Default Directories" tramp-default-remote-path)
451 (const :tag "Private Directories" tramp-own-remote-path)
452 (string :tag "Directory"))))
453
454 ;;;###tramp-autoload
455 (defcustom tramp-remote-process-environment
456 `("TMOUT=0" "LC_CTYPE=''"
457 ,(format "TERM=%s" tramp-terminal-type)
458 "EMACS=t" ;; Deprecated.
459 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version)
460 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH=" "PAGER=\"\""
461 "autocorrect=" "correct=")
462 "List of environment variables to be set on the remote host.
463
464 Each element should be a string of the form ENVVARNAME=VALUE. An
465 entry ENVVARNAME= disables the corresponding environment variable,
466 which might have been set in the init files like ~/.profile.
467
468 Special handling is applied to the PATH environment, which should
469 not be set here. Instead, it should be set via `tramp-remote-path'."
470 :group 'tramp
471 :version "24.4"
472 :type '(repeat string))
473
474 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
475 "Alist specifying extra arguments to pass to the remote shell.
476 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
477 matching the shell file name and ARGS is a string specifying the
478 arguments.
479
480 This variable is only used when Tramp needs to start up another shell
481 for tilde expansion. The extra arguments should typically prevent the
482 shell from reading its init file."
483 :group 'tramp
484 ;; This might be the wrong way to test whether the widget type
485 ;; `alist' is available. Who knows the right way to test it?
486 :type (if (get 'alist 'widget-type)
487 '(alist :key-type string :value-type string)
488 '(repeat (cons string string))))
489
490 (defconst tramp-actions-before-shell
491 '((tramp-login-prompt-regexp tramp-action-login)
492 (tramp-password-prompt-regexp tramp-action-password)
493 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
494 (shell-prompt-pattern tramp-action-succeed)
495 (tramp-shell-prompt-pattern tramp-action-succeed)
496 (tramp-yesno-prompt-regexp tramp-action-yesno)
497 (tramp-yn-prompt-regexp tramp-action-yn)
498 (tramp-terminal-prompt-regexp tramp-action-terminal)
499 (tramp-process-alive-regexp tramp-action-process-alive))
500 "List of pattern/action pairs.
501 Whenever a pattern matches, the corresponding action is performed.
502 Each item looks like (PATTERN ACTION).
503
504 The PATTERN should be a symbol, a variable. The value of this
505 variable gives the regular expression to search for. Note that the
506 regexp must match at the end of the buffer, \"\\'\" is implicitly
507 appended to it.
508
509 The ACTION should also be a symbol, but a function. When the
510 corresponding PATTERN matches, the ACTION function is called.")
511
512 (defconst tramp-actions-copy-out-of-band
513 '((tramp-password-prompt-regexp tramp-action-password)
514 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
515 (tramp-copy-failed-regexp tramp-action-permission-denied)
516 (tramp-process-alive-regexp tramp-action-out-of-band))
517 "List of pattern/action pairs.
518 This list is used for copying/renaming with out-of-band methods.
519
520 See `tramp-actions-before-shell' for more info.")
521
522 (defconst tramp-uudecode
523 "(echo begin 600 %t; tail -n +2) | uudecode
524 cat %t
525 rm -f %t"
526 "Shell function to implement `uudecode' to standard output.
527 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
528 for this or `uudecode -p', but some systems don't, and for them
529 we have this shell function.")
530
531 (defconst tramp-perl-file-truename
532 "%s -e '
533 use File::Spec;
534 use Cwd \"realpath\";
535
536 sub recursive {
537 my ($volume, @dirs) = @_;
538 my $real = realpath(File::Spec->catpath(
539 $volume, File::Spec->catdir(@dirs), \"\"));
540 if ($real) {
541 my ($vol, $dir) = File::Spec->splitpath($real, 1);
542 return ($vol, File::Spec->splitdir($dir));
543 }
544 else {
545 my $last = pop(@dirs);
546 ($volume, @dirs) = recursive($volume, @dirs);
547 push(@dirs, $last);
548 return ($volume, @dirs);
549 }
550 }
551
552 $result = realpath($ARGV[0]);
553 if (!$result) {
554 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
555 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
556
557 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
558 }
559
560 if ($ARGV[0] =~ /\\/$/) {
561 $result = $result . \"/\";
562 }
563
564 print \"\\\"$result\\\"\\n\";
565 ' \"$1\" 2>/dev/null"
566 "Perl script to produce output suitable for use with `file-truename'
567 on the remote file system.
568 Escape sequence %s is replaced with name of Perl binary.
569 This string is passed to `format', so percent characters need to be doubled.")
570
571 (defconst tramp-perl-file-name-all-completions
572 "%s -e 'sub case {
573 my $str = shift;
574 if ($ARGV[2]) {
575 return lc($str);
576 }
577 else {
578 return $str;
579 }
580 }
581 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
582 @files = readdir(d); closedir(d);
583 foreach $f (@files) {
584 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
585 if (-d \"$ARGV[0]/$f\") {
586 print \"$f/\\n\";
587 }
588 else {
589 print \"$f\\n\";
590 }
591 }
592 }
593 print \"ok\\n\"
594 ' \"$1\" \"$2\" \"$3\" 2>/dev/null"
595 "Perl script to produce output suitable for use with
596 `file-name-all-completions' on the remote file system. Escape
597 sequence %s is replaced with name of Perl binary. This string is
598 passed to `format', so percent characters need to be doubled.")
599
600 ;; Perl script to implement `file-attributes' in a Lisp `read'able
601 ;; output. If you are hacking on this, note that you get *no* output
602 ;; unless this spits out a complete line, including the '\n' at the
603 ;; end.
604 ;; The device number is returned as "-1", because there will be a virtual
605 ;; device number set in `tramp-sh-handle-file-attributes'.
606 (defconst tramp-perl-file-attributes
607 "%s -e '
608 @stat = lstat($ARGV[0]);
609 if (!@stat) {
610 print \"nil\\n\";
611 exit 0;
612 }
613 if (($stat[2] & 0170000) == 0120000)
614 {
615 $type = readlink($ARGV[0]);
616 $type = \"\\\"$type\\\"\";
617 }
618 elsif (($stat[2] & 0170000) == 040000)
619 {
620 $type = \"t\";
621 }
622 else
623 {
624 $type = \"nil\"
625 };
626 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
627 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
628 printf(
629 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
630 $type,
631 $stat[3],
632 $uid,
633 $gid,
634 $stat[8] >> 16 & 0xffff,
635 $stat[8] & 0xffff,
636 $stat[9] >> 16 & 0xffff,
637 $stat[9] & 0xffff,
638 $stat[10] >> 16 & 0xffff,
639 $stat[10] & 0xffff,
640 $stat[7],
641 $stat[2],
642 $stat[1] >> 16 & 0xffff,
643 $stat[1] & 0xffff
644 );' \"$1\" \"$2\" 2>/dev/null"
645 "Perl script to produce output suitable for use with `file-attributes'
646 on the remote file system.
647 Escape sequence %s is replaced with name of Perl binary.
648 This string is passed to `format', so percent characters need to be doubled.")
649
650 (defconst tramp-perl-directory-files-and-attributes
651 "%s -e '
652 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
653 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
654 @list = readdir(DIR);
655 closedir(DIR);
656 $n = scalar(@list);
657 printf(\"(\\n\");
658 for($i = 0; $i < $n; $i++)
659 {
660 $filename = $list[$i];
661 @stat = lstat($filename);
662 if (($stat[2] & 0170000) == 0120000)
663 {
664 $type = readlink($filename);
665 $type = \"\\\"$type\\\"\";
666 }
667 elsif (($stat[2] & 0170000) == 040000)
668 {
669 $type = \"t\";
670 }
671 else
672 {
673 $type = \"nil\"
674 };
675 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
676 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
677 printf(
678 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
679 $filename,
680 $type,
681 $stat[3],
682 $uid,
683 $gid,
684 $stat[8] >> 16 & 0xffff,
685 $stat[8] & 0xffff,
686 $stat[9] >> 16 & 0xffff,
687 $stat[9] & 0xffff,
688 $stat[10] >> 16 & 0xffff,
689 $stat[10] & 0xffff,
690 $stat[7],
691 $stat[2],
692 $stat[1] >> 16 & 0xffff,
693 $stat[1] & 0xffff,
694 $stat[0] >> 16 & 0xffff,
695 $stat[0] & 0xffff);
696 }
697 printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
698 "Perl script implementing `directory-files-attributes' as Lisp `read'able
699 output.
700 Escape sequence %s is replaced with name of Perl binary.
701 This string is passed to `format', so percent characters need to be doubled.")
702
703 ;; These two use base64 encoding.
704 (defconst tramp-perl-encode-with-module
705 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
706 "Perl program to use for encoding a file.
707 Escape sequence %s is replaced with name of Perl binary.
708 This string is passed to `format', so percent characters need to be doubled.
709 This implementation requires the MIME::Base64 Perl module to be installed
710 on the remote host.")
711
712 (defconst tramp-perl-decode-with-module
713 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
714 "Perl program to use for decoding a file.
715 Escape sequence %s is replaced with name of Perl binary.
716 This string is passed to `format', so percent characters need to be doubled.
717 This implementation requires the MIME::Base64 Perl module to be installed
718 on the remote host.")
719
720 (defconst tramp-perl-encode
721 "%s -e '
722 # This script contributed by Juanma Barranquero <lektu@terra.es>.
723 # Copyright (C) 2002-2014 Free Software Foundation, Inc.
724 use strict;
725
726 my %%trans = do {
727 my $i = 0;
728 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
729 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
730 };
731 my $data;
732
733 # We read in chunks of 54 bytes, to generate output lines
734 # of 72 chars (plus end of line)
735 while (read STDIN, $data, 54) {
736 my $pad = q();
737
738 # Only for the last chunk, and only if did not fill the last three-byte packet
739 if (eof) {
740 my $mod = length($data) %% 3;
741 $pad = q(=) x (3 - $mod) if $mod;
742 }
743
744 # Not the fastest method, but it is simple: unpack to binary string, split
745 # by groups of 6 bits and convert back from binary to byte; then map into
746 # the translation table
747 print
748 join q(),
749 map($trans{$_},
750 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
751 $pad,
752 qq(\\n);
753 }' 2>/dev/null"
754 "Perl program to use for encoding a file.
755 Escape sequence %s is replaced with name of Perl binary.
756 This string is passed to `format', so percent characters need to be doubled.")
757
758 (defconst tramp-perl-decode
759 "%s -e '
760 # This script contributed by Juanma Barranquero <lektu@terra.es>.
761 # Copyright (C) 2002-2014 Free Software Foundation, Inc.
762 use strict;
763
764 my %%trans = do {
765 my $i = 0;
766 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
767 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
768 };
769
770 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
771
772 binmode(\\*STDOUT);
773
774 # We are going to accumulate into $pending to accept any line length
775 # (we do not check they are <= 76 chars as the RFC says)
776 my $pending = q();
777
778 while (my $data = <STDIN>) {
779 chomp $data;
780
781 # If we find one or two =, we have reached the end and
782 # any following data is to be discarded
783 my $finished = $data =~ s/(==?).*/$1/;
784 $pending .= $data;
785
786 my $len = length($pending);
787 my $chunk = substr($pending, 0, $len & ~3);
788 $pending = substr($pending, $len & ~3 + 1);
789
790 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
791 # split in 8-bit chunks and convert back to char.
792 print join q(),
793 map $bytes{$_},
794 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
795
796 last if $finished;
797 }' 2>/dev/null"
798 "Perl program to use for decoding a file.
799 Escape sequence %s is replaced with name of Perl binary.
800 This string is passed to `format', so percent characters need to be doubled.")
801
802 (defconst tramp-perl-pack
803 "%s -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
804 "Perl program to use for encoding a file.
805 Escape sequence %s is replaced with name of Perl binary.")
806
807 (defconst tramp-perl-unpack
808 "%s -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"
809 "Perl program to use for decoding a file.
810 Escape sequence %s is replaced with name of Perl binary.")
811
812 (defconst tramp-vc-registered-read-file-names
813 "echo \"(\"
814 while read file; do
815 if %s \"$file\"; then
816 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
817 else
818 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
819 fi
820 if %s \"$file\"; then
821 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
822 else
823 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
824 fi
825 done
826 echo \")\""
827 "Script to check existence of VC related files.
828 It must be send formatted with two strings; the tests for file
829 existence, and file readability. Input shall be read via
830 here-document, otherwise the command could exceed maximum length
831 of command line.")
832
833 ;; New handlers should be added here.
834 (defconst tramp-sh-file-name-handler-alist
835 '(;; `access-file' performed by default handler.
836 (add-name-to-file . tramp-sh-handle-add-name-to-file)
837 ;; `byte-compiler-base-file-name' performed by default handler.
838 (copy-directory . tramp-sh-handle-copy-directory)
839 (copy-file . tramp-sh-handle-copy-file)
840 (delete-directory . tramp-sh-handle-delete-directory)
841 (delete-file . tramp-sh-handle-delete-file)
842 ;; `diff-latest-backup-file' performed by default handler.
843 (directory-file-name . tramp-handle-directory-file-name)
844 (directory-files . tramp-handle-directory-files)
845 (directory-files-and-attributes
846 . tramp-sh-handle-directory-files-and-attributes)
847 ;; `dired-call-process' performed by default handler.
848 (dired-compress-file . tramp-sh-handle-dired-compress-file)
849 (dired-recursive-delete-directory
850 . tramp-sh-handle-dired-recursive-delete-directory)
851 (dired-uncache . tramp-handle-dired-uncache)
852 (expand-file-name . tramp-sh-handle-expand-file-name)
853 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
854 (file-acl . tramp-sh-handle-file-acl)
855 (file-attributes . tramp-sh-handle-file-attributes)
856 (file-directory-p . tramp-sh-handle-file-directory-p)
857 ;; `file-equal-p' performed by default handler.
858 (file-executable-p . tramp-sh-handle-file-executable-p)
859 (file-exists-p . tramp-sh-handle-file-exists-p)
860 ;; `file-in-directory-p' performed by default handler.
861 (file-local-copy . tramp-sh-handle-file-local-copy)
862 (file-modes . tramp-handle-file-modes)
863 (file-name-all-completions . tramp-sh-handle-file-name-all-completions)
864 (file-name-as-directory . tramp-handle-file-name-as-directory)
865 (file-name-completion . tramp-handle-file-name-completion)
866 (file-name-directory . tramp-handle-file-name-directory)
867 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
868 ;; `file-name-sans-versions' performed by default handler.
869 (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p)
870 (file-notify-add-watch . tramp-sh-handle-file-notify-add-watch)
871 (file-notify-rm-watch . tramp-handle-file-notify-rm-watch)
872 (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p)
873 (file-readable-p . tramp-sh-handle-file-readable-p)
874 (file-regular-p . tramp-handle-file-regular-p)
875 (file-remote-p . tramp-handle-file-remote-p)
876 (file-selinux-context . tramp-sh-handle-file-selinux-context)
877 (file-symlink-p . tramp-handle-file-symlink-p)
878 (file-truename . tramp-sh-handle-file-truename)
879 (file-writable-p . tramp-sh-handle-file-writable-p)
880 (find-backup-file-name . tramp-handle-find-backup-file-name)
881 ;; `find-file-noselect' performed by default handler.
882 ;; `get-file-buffer' performed by default handler.
883 (insert-directory . tramp-sh-handle-insert-directory)
884 (insert-file-contents . tramp-handle-insert-file-contents)
885 (insert-file-contents-literally
886 . tramp-sh-handle-insert-file-contents-literally)
887 (load . tramp-handle-load)
888 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
889 (make-directory . tramp-sh-handle-make-directory)
890 (make-symbolic-link . tramp-sh-handle-make-symbolic-link)
891 (process-file . tramp-sh-handle-process-file)
892 (rename-file . tramp-sh-handle-rename-file)
893 (set-file-acl . tramp-sh-handle-set-file-acl)
894 (set-file-modes . tramp-sh-handle-set-file-modes)
895 (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context)
896 (set-file-times . tramp-sh-handle-set-file-times)
897 (set-visited-file-modtime . tramp-sh-handle-set-visited-file-modtime)
898 (shell-command . tramp-handle-shell-command)
899 (start-file-process . tramp-sh-handle-start-file-process)
900 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
901 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
902 (vc-registered . tramp-sh-handle-vc-registered)
903 (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime)
904 (write-region . tramp-sh-handle-write-region))
905 "Alist of handler functions.
906 Operations not mentioned here will be handled by the normal Emacs functions.")
907
908 ;; This must be the last entry, because `identity' always matches.
909 ;;;###tramp-autoload
910 (add-to-list 'tramp-foreign-file-name-handler-alist
911 '(identity . tramp-sh-file-name-handler) 'append)
912
913 ;;; File Name Handler Functions:
914
915 (defun tramp-sh-handle-make-symbolic-link
916 (filename linkname &optional ok-if-already-exists)
917 "Like `make-symbolic-link' for Tramp files.
918 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
919 the symlink. If LINKNAME is a Tramp file, only the localname component is
920 used as the target of the symlink.
921
922 If LINKNAME is a Tramp file and the localname component is relative, then
923 it is expanded first, before the localname component is taken. Note that
924 this can give surprising results if the user/host for the source and
925 target of the symlink differ."
926 (with-parsed-tramp-file-name linkname l
927 (let ((ln (tramp-get-remote-ln l))
928 (cwd (tramp-run-real-handler
929 'file-name-directory (list l-localname))))
930 (unless ln
931 (tramp-error
932 l 'file-error
933 "Making a symbolic link. ln(1) does not exist on the remote host."))
934
935 ;; Do the 'confirm if exists' thing.
936 (when (file-exists-p linkname)
937 ;; What to do?
938 (if (or (null ok-if-already-exists) ; not allowed to exist
939 (and (numberp ok-if-already-exists)
940 (not (yes-or-no-p
941 (format
942 "File %s already exists; make it a link anyway? "
943 l-localname)))))
944 (tramp-error
945 l 'file-already-exists "File %s already exists" l-localname)
946 (delete-file linkname)))
947
948 ;; If FILENAME is a Tramp name, use just the localname component.
949 (when (tramp-tramp-file-p filename)
950 (setq filename
951 (tramp-file-name-localname
952 (tramp-dissect-file-name (expand-file-name filename)))))
953
954 (tramp-flush-file-property l (file-name-directory l-localname))
955 (tramp-flush-file-property l l-localname)
956
957 ;; Right, they are on the same host, regardless of user, method,
958 ;; etc. We now make the link on the remote machine. This will
959 ;; occur as the user that FILENAME belongs to.
960 (tramp-send-command-and-check
961 l
962 (format
963 "cd %s && %s -sf %s %s"
964 (tramp-shell-quote-argument cwd)
965 ln
966 (tramp-shell-quote-argument filename)
967 (tramp-shell-quote-argument l-localname))
968 t))))
969
970 (defun tramp-sh-handle-file-truename (filename)
971 "Like `file-truename' for Tramp files."
972 (format
973 "%s%s"
974 (with-parsed-tramp-file-name (expand-file-name filename) nil
975 (tramp-make-tramp-file-name
976 method user host
977 (with-tramp-file-property v localname "file-truename"
978 (let ((result nil)) ; result steps in reverse order
979 (tramp-message v 4 "Finding true name for `%s'" filename)
980 (cond
981 ;; Use GNU readlink --canonicalize-missing where available.
982 ((tramp-get-remote-readlink v)
983 (tramp-send-command-and-check
984 v
985 (format "%s --canonicalize-missing %s"
986 (tramp-get-remote-readlink v)
987 (tramp-shell-quote-argument localname)))
988 (with-current-buffer (tramp-get-connection-buffer v)
989 (goto-char (point-min))
990 (setq result (buffer-substring (point-min) (point-at-eol)))))
991
992 ;; Use Perl implementation.
993 ((and (tramp-get-remote-perl v)
994 (tramp-get-connection-property v "perl-file-spec" nil)
995 (tramp-get-connection-property v "perl-cwd-realpath" nil))
996 (tramp-maybe-send-script
997 v tramp-perl-file-truename "tramp_perl_file_truename")
998 (setq result
999 (tramp-send-command-and-read
1000 v
1001 (format "tramp_perl_file_truename %s"
1002 (tramp-shell-quote-argument localname)))))
1003
1004 ;; Do it yourself. We bind `directory-sep-char' here for
1005 ;; XEmacs on Windows, which would otherwise use backslash.
1006 (t (let* ((directory-sep-char ?/)
1007 (steps (tramp-compat-split-string localname "/"))
1008 (localnamedir (tramp-run-real-handler
1009 'file-name-as-directory (list localname)))
1010 (is-dir (string= localname localnamedir))
1011 (thisstep nil)
1012 (numchase 0)
1013 ;; Don't make the following value larger than
1014 ;; necessary. People expect an error message in
1015 ;; a timely fashion when something is wrong;
1016 ;; otherwise they might think that Emacs is hung.
1017 ;; Of course, correctness has to come first.
1018 (numchase-limit 20)
1019 symlink-target)
1020 (while (and steps (< numchase numchase-limit))
1021 (setq thisstep (pop steps))
1022 (tramp-message
1023 v 5 "Check %s"
1024 (mapconcat 'identity
1025 (append '("") (reverse result) (list thisstep))
1026 "/"))
1027 (setq symlink-target
1028 (nth 0 (file-attributes
1029 (tramp-make-tramp-file-name
1030 method user host
1031 (mapconcat 'identity
1032 (append '("")
1033 (reverse result)
1034 (list thisstep))
1035 "/")))))
1036 (cond ((string= "." thisstep)
1037 (tramp-message v 5 "Ignoring step `.'"))
1038 ((string= ".." thisstep)
1039 (tramp-message v 5 "Processing step `..'")
1040 (pop result))
1041 ((stringp symlink-target)
1042 ;; It's a symlink, follow it.
1043 (tramp-message
1044 v 5 "Follow symlink to %s" symlink-target)
1045 (setq numchase (1+ numchase))
1046 (when (file-name-absolute-p symlink-target)
1047 (setq result nil))
1048 ;; If the symlink was absolute, we'll get a
1049 ;; string like "/user@host:/some/target";
1050 ;; extract the "/some/target" part from it.
1051 (when (tramp-tramp-file-p symlink-target)
1052 (unless (tramp-equal-remote filename symlink-target)
1053 (tramp-error
1054 v 'file-error
1055 "Symlink target `%s' on wrong host"
1056 symlink-target))
1057 (setq symlink-target localname))
1058 (setq steps
1059 (append (tramp-compat-split-string
1060 symlink-target "/")
1061 steps)))
1062 (t
1063 ;; It's a file.
1064 (setq result (cons thisstep result)))))
1065 (when (>= numchase numchase-limit)
1066 (tramp-error
1067 v 'file-error
1068 "Maximum number (%d) of symlinks exceeded" numchase-limit))
1069 (setq result (reverse result))
1070 ;; Combine list to form string.
1071 (setq result
1072 (if result
1073 (mapconcat 'identity (cons "" result) "/")
1074 "/"))
1075 (when (and is-dir
1076 (or (string= "" result)
1077 (not (string= (substring result -1) "/"))))
1078 (setq result (concat result "/"))))))
1079
1080 (tramp-message v 4 "True name of `%s' is `%s'" localname result)
1081 result))))
1082
1083 ;; Preserve trailing "/".
1084 (if (string-equal (file-name-nondirectory filename) "") "/" "")))
1085
1086 ;; Basic functions.
1087
1088 (defun tramp-sh-handle-file-exists-p (filename)
1089 "Like `file-exists-p' for Tramp files."
1090 (with-parsed-tramp-file-name filename nil
1091 (with-tramp-file-property v localname "file-exists-p"
1092 (or (not (null (tramp-get-file-property
1093 v localname "file-attributes-integer" nil)))
1094 (not (null (tramp-get-file-property
1095 v localname "file-attributes-string" nil)))
1096 (tramp-send-command-and-check
1097 v
1098 (format
1099 "%s %s"
1100 (tramp-get-file-exists-command v)
1101 (tramp-shell-quote-argument localname)))))))
1102
1103 (defun tramp-sh-handle-file-attributes (filename &optional id-format)
1104 "Like `file-attributes' for Tramp files."
1105 (unless id-format (setq id-format 'integer))
1106 (ignore-errors
1107 ;; Don't modify `last-coding-system-used' by accident.
1108 (let ((last-coding-system-used last-coding-system-used))
1109 (with-parsed-tramp-file-name (expand-file-name filename) nil
1110 (with-tramp-file-property
1111 v localname (format "file-attributes-%s" id-format)
1112 (save-excursion
1113 (tramp-convert-file-attributes
1114 v
1115 (or
1116 (cond
1117 ((tramp-get-remote-stat v)
1118 (tramp-do-file-attributes-with-stat v localname id-format))
1119 ((tramp-get-remote-perl v)
1120 (tramp-do-file-attributes-with-perl v localname id-format))
1121 (t nil))
1122 ;; The scripts could fail, for example with huge file size.
1123 (tramp-do-file-attributes-with-ls v localname id-format)))))))))
1124
1125 (defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
1126 "Implement `file-attributes' for Tramp files using the ls(1) command."
1127 (let (symlinkp dirp
1128 res-inode res-filemodes res-numlinks
1129 res-uid res-gid res-size res-symlink-target)
1130 (tramp-message vec 5 "file attributes with ls: %s" localname)
1131 (tramp-send-command
1132 vec
1133 (format "(%s %s || %s -h %s) && %s %s %s"
1134 (tramp-get-file-exists-command vec)
1135 (tramp-shell-quote-argument localname)
1136 (tramp-get-test-command vec)
1137 (tramp-shell-quote-argument localname)
1138 (tramp-get-ls-command vec)
1139 (if (eq id-format 'integer) "-ildn" "-ild")
1140 (tramp-shell-quote-argument localname)))
1141 ;; parse `ls -l' output ...
1142 (with-current-buffer (tramp-get-buffer vec)
1143 (when (> (buffer-size) 0)
1144 (goto-char (point-min))
1145 ;; ... inode
1146 (setq res-inode
1147 (condition-case err
1148 (read (current-buffer))
1149 (invalid-read-syntax
1150 (when (and (equal (cadr err)
1151 "Integer constant overflow in reader")
1152 (string-match
1153 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1154 (car (cddr err))))
1155 (let* ((big (read (substring (car (cddr err)) 0
1156 (match-beginning 1))))
1157 (small (read (match-string 1 (car (cddr err)))))
1158 (twiddle (/ small 65536)))
1159 (cons (+ big twiddle)
1160 (- small (* twiddle 65536))))))))
1161 ;; ... file mode flags
1162 (setq res-filemodes (symbol-name (read (current-buffer))))
1163 ;; ... number links
1164 (setq res-numlinks (read (current-buffer)))
1165 ;; ... uid and gid
1166 (setq res-uid (read (current-buffer)))
1167 (setq res-gid (read (current-buffer)))
1168 (if (eq id-format 'integer)
1169 (progn
1170 (unless (numberp res-uid) (setq res-uid -1))
1171 (unless (numberp res-gid) (setq res-gid -1)))
1172 (progn
1173 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
1174 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
1175 ;; ... size
1176 (setq res-size (read (current-buffer)))
1177 ;; From the file modes, figure out other stuff.
1178 (setq symlinkp (eq ?l (aref res-filemodes 0)))
1179 (setq dirp (eq ?d (aref res-filemodes 0)))
1180 ;; if symlink, find out file name pointed to
1181 (when symlinkp
1182 (search-forward "-> ")
1183 (setq res-symlink-target (buffer-substring (point) (point-at-eol))))
1184 ;; return data gathered
1185 (list
1186 ;; 0. t for directory, string (name linked to) for symbolic
1187 ;; link, or nil.
1188 (or dirp res-symlink-target)
1189 ;; 1. Number of links to file.
1190 res-numlinks
1191 ;; 2. File uid.
1192 res-uid
1193 ;; 3. File gid.
1194 res-gid
1195 ;; 4. Last access time, as a list of integers. Normally this
1196 ;; would be in the same format as `current-time', but the
1197 ;; subseconds part is not currently implemented, and (0 0)
1198 ;; denotes an unknown time.
1199 ;; 5. Last modification time, likewise.
1200 ;; 6. Last status change time, likewise.
1201 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1202 ;; 7. Size in bytes (-1, if number is out of range).
1203 res-size
1204 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1205 res-filemodes
1206 ;; 9. t if file's gid would change if file were deleted and
1207 ;; recreated. Will be set in `tramp-convert-file-attributes'
1208 t
1209 ;; 10. inode number.
1210 res-inode
1211 ;; 11. Device number. Will be replaced by a virtual device number.
1212 -1
1213 )))))
1214
1215 (defun tramp-do-file-attributes-with-perl
1216 (vec localname &optional id-format)
1217 "Implement `file-attributes' for Tramp files using a Perl script."
1218 (tramp-message vec 5 "file attributes with perl: %s" localname)
1219 (tramp-maybe-send-script
1220 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
1221 (tramp-send-command-and-read
1222 vec
1223 (format "tramp_perl_file_attributes %s %s"
1224 (tramp-shell-quote-argument localname) id-format)))
1225
1226 (defun tramp-do-file-attributes-with-stat
1227 (vec localname &optional id-format)
1228 "Implement `file-attributes' for Tramp files using stat(1) command."
1229 (tramp-message vec 5 "file attributes with stat: %s" localname)
1230 (tramp-send-command-and-read
1231 vec
1232 (format
1233 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
1234 ;; parse correctly the sequence "((". Therefore, we add a space.
1235 "( (%s %s || %s -h %s) && %s -c '((\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)' %s || echo nil)"
1236 (tramp-get-file-exists-command vec)
1237 (tramp-shell-quote-argument localname)
1238 (tramp-get-test-command vec)
1239 (tramp-shell-quote-argument localname)
1240 (tramp-get-remote-stat vec)
1241 (if (eq id-format 'integer) "%ue0" "\"%U\"")
1242 (if (eq id-format 'integer) "%ge0" "\"%G\"")
1243 (tramp-shell-quote-argument localname))))
1244
1245 (defun tramp-sh-handle-set-visited-file-modtime (&optional time-list)
1246 "Like `set-visited-file-modtime' for Tramp files."
1247 (unless (buffer-file-name)
1248 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1249 (buffer-name)))
1250 (if time-list
1251 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
1252 (let ((f (buffer-file-name))
1253 coding-system-used)
1254 (with-parsed-tramp-file-name f nil
1255 (let* ((remote-file-name-inhibit-cache t)
1256 (attr (file-attributes f))
1257 ;; '(-1 65535) means file doesn't exists yet.
1258 (modtime (or (nth 5 attr) '(-1 65535))))
1259 (when (boundp 'last-coding-system-used)
1260 (setq coding-system-used (symbol-value 'last-coding-system-used)))
1261 ;; We use '(0 0) as a don't-know value. See also
1262 ;; `tramp-do-file-attributes-with-ls'.
1263 (if (not (equal modtime '(0 0)))
1264 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
1265 (progn
1266 (tramp-send-command
1267 v
1268 (format "%s -ild %s"
1269 (tramp-get-ls-command v)
1270 (tramp-shell-quote-argument localname)))
1271 (setq attr (buffer-substring (point) (point-at-eol))))
1272 (tramp-set-file-property
1273 v localname "visited-file-modtime-ild" attr))
1274 (when (boundp 'last-coding-system-used)
1275 (set 'last-coding-system-used coding-system-used))
1276 nil)))))
1277
1278 ;; This function makes the same assumption as
1279 ;; `tramp-sh-handle-set-visited-file-modtime'.
1280 (defun tramp-sh-handle-verify-visited-file-modtime (&optional buf)
1281 "Like `verify-visited-file-modtime' for Tramp files.
1282 At the time `verify-visited-file-modtime' calls this function, we
1283 already know that the buffer is visiting a file and that
1284 `visited-file-modtime' does not return 0. Do not call this
1285 function directly, unless those two cases are already taken care
1286 of."
1287 (with-current-buffer (or buf (current-buffer))
1288 (let ((f (buffer-file-name)))
1289 ;; There is no file visiting the buffer, or the buffer has no
1290 ;; recorded last modification time, or there is no established
1291 ;; connection.
1292 (if (or (not f)
1293 (eq (visited-file-modtime) 0)
1294 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
1295 t
1296 (with-parsed-tramp-file-name f nil
1297 (let* ((remote-file-name-inhibit-cache t)
1298 (attr (file-attributes f))
1299 (modtime (nth 5 attr))
1300 (mt (visited-file-modtime)))
1301
1302 (cond
1303 ;; File exists, and has a known modtime.
1304 ((and attr (not (equal modtime '(0 0))))
1305 (< (abs (tramp-time-diff
1306 modtime
1307 ;; For compatibility, deal with both the old
1308 ;; (HIGH . LOW) and the new (HIGH LOW) return
1309 ;; values of `visited-file-modtime'.
1310 (if (atom (cdr mt))
1311 (list (car mt) (cdr mt))
1312 mt)))
1313 2))
1314 ;; Modtime has the don't know value.
1315 (attr
1316 (tramp-send-command
1317 v
1318 (format "%s -ild %s"
1319 (tramp-get-ls-command v)
1320 (tramp-shell-quote-argument localname)))
1321 (with-current-buffer (tramp-get-buffer v)
1322 (setq attr (buffer-substring (point) (point-at-eol))))
1323 (equal
1324 attr
1325 (tramp-get-file-property
1326 v localname "visited-file-modtime-ild" "")))
1327 ;; If file does not exist, say it is not modified if and
1328 ;; only if that agrees with the buffer's record.
1329 (t (equal mt '(-1 65535))))))))))
1330
1331 (defun tramp-sh-handle-set-file-modes (filename mode)
1332 "Like `set-file-modes' for Tramp files."
1333 (with-parsed-tramp-file-name filename nil
1334 (tramp-flush-file-property v localname)
1335 ;; FIXME: extract the proper text from chmod's stderr.
1336 (tramp-barf-unless-okay
1337 v
1338 (format "chmod %s %s"
1339 (tramp-compat-decimal-to-octal mode)
1340 (tramp-shell-quote-argument localname))
1341 "Error while changing file's mode %s" filename)))
1342
1343 (defun tramp-sh-handle-set-file-times (filename &optional time)
1344 "Like `set-file-times' for Tramp files."
1345 (if (tramp-tramp-file-p filename)
1346 (with-parsed-tramp-file-name filename nil
1347 (when (tramp-get-remote-touch v)
1348 (tramp-flush-file-property v localname)
1349 (let ((time (if (or (null time) (equal time '(0 0)))
1350 (current-time)
1351 time))
1352 ;; With GNU Emacs, `format-time-string' has an
1353 ;; optional parameter UNIVERSAL. This is preferred,
1354 ;; because we could handle the case when the remote
1355 ;; host is located in a different time zone as the
1356 ;; local host.
1357 (utc (not (featurep 'xemacs))))
1358 (tramp-send-command-and-check
1359 v (format
1360 "%s %s %s %s"
1361 (if utc "env TZ=UTC" "")
1362 (tramp-get-remote-touch v)
1363 (if (tramp-get-connection-property v "touch-t" nil)
1364 (format "-t %s"
1365 (if utc
1366 (format-time-string "%Y%m%d%H%M.%S" time t)
1367 (format-time-string "%Y%m%d%H%M.%S" time)))
1368 "")
1369 (tramp-shell-quote-argument localname))))))
1370
1371 ;; We handle also the local part, because in older Emacsen,
1372 ;; without `set-file-times', this function is an alias for this.
1373 ;; We are local, so we don't need the UTC settings.
1374 (zerop
1375 (tramp-call-process
1376 nil "touch" nil nil nil "-t"
1377 (format-time-string "%Y%m%d%H%M.%S" time)
1378 (tramp-shell-quote-argument filename)))))
1379
1380 (defun tramp-set-file-uid-gid (filename &optional uid gid)
1381 "Set the ownership for FILENAME.
1382 If UID and GID are provided, these values are used; otherwise uid
1383 and gid of the corresponding user is taken. Both parameters must
1384 be non-negative integers."
1385 ;; Modern Unices allow chown only for root. So we might need
1386 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1387 ;; working with su(do)? when it is needed, so it shall succeed in
1388 ;; the majority of cases.
1389 ;; Don't modify `last-coding-system-used' by accident.
1390 (let ((last-coding-system-used last-coding-system-used))
1391 (if (tramp-tramp-file-p filename)
1392 (with-parsed-tramp-file-name filename nil
1393 (if (and (zerop (user-uid)) (tramp-local-host-p v))
1394 ;; If we are root on the local host, we can do it directly.
1395 (tramp-set-file-uid-gid localname uid gid)
1396 (let ((uid (or (and (natnump uid) uid)
1397 (tramp-get-remote-uid v 'integer)))
1398 (gid (or (and (natnump gid) gid)
1399 (tramp-get-remote-gid v 'integer))))
1400 (tramp-send-command
1401 v (format
1402 "chown %d:%d %s" uid gid
1403 (tramp-shell-quote-argument localname))))))
1404
1405 ;; We handle also the local part, because there doesn't exist
1406 ;; `set-file-uid-gid'. On W32 "chown" might not work.
1407 (let ((uid (or (and (natnump uid) uid) (tramp-get-local-uid 'integer)))
1408 (gid (or (and (natnump gid) gid) (tramp-get-local-gid 'integer))))
1409 (tramp-call-process
1410 nil "chown" nil nil nil
1411 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
1412
1413 (defun tramp-remote-selinux-p (vec)
1414 "Check, whether SELINUX is enabled on the remote host."
1415 (with-tramp-connection-property
1416 (tramp-get-connection-process vec) "selinux-p"
1417 (let ((result (tramp-find-executable
1418 vec "getenforce" (tramp-get-remote-path vec) t t)))
1419 (and result
1420 (string-equal
1421 (tramp-send-command-and-read
1422 vec (format "echo \\\"`%S`\\\"" result))
1423 "Enforcing")))))
1424
1425 (defun tramp-sh-handle-file-selinux-context (filename)
1426 "Like `file-selinux-context' for Tramp files."
1427 (with-parsed-tramp-file-name filename nil
1428 (with-tramp-file-property v localname "file-selinux-context"
1429 (let ((context '(nil nil nil nil))
1430 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1431 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1432 (when (and (tramp-remote-selinux-p v)
1433 (tramp-send-command-and-check
1434 v (format
1435 "%s -d -Z %s"
1436 (tramp-get-ls-command v)
1437 (tramp-shell-quote-argument localname))))
1438 (with-current-buffer (tramp-get-connection-buffer v)
1439 (goto-char (point-min))
1440 (when (re-search-forward regexp (point-at-eol) t)
1441 (setq context (list (match-string 1) (match-string 2)
1442 (match-string 3) (match-string 4))))))
1443 ;; Return the context.
1444 context))))
1445
1446 (defun tramp-sh-handle-set-file-selinux-context (filename context)
1447 "Like `set-file-selinux-context' for Tramp files."
1448 (with-parsed-tramp-file-name filename nil
1449 (if (and (consp context)
1450 (tramp-remote-selinux-p v)
1451 (tramp-send-command-and-check
1452 v (format "chcon %s %s %s %s %s"
1453 (if (stringp (nth 0 context))
1454 (format "--user=%s" (nth 0 context)) "")
1455 (if (stringp (nth 1 context))
1456 (format "--role=%s" (nth 1 context)) "")
1457 (if (stringp (nth 2 context))
1458 (format "--type=%s" (nth 2 context)) "")
1459 (if (stringp (nth 3 context))
1460 (format "--range=%s" (nth 3 context)) "")
1461 (tramp-shell-quote-argument localname))))
1462 (progn
1463 (tramp-set-file-property v localname "file-selinux-context" context)
1464 t)
1465 (tramp-set-file-property v localname "file-selinux-context" 'undef)
1466 nil)))
1467
1468 (defun tramp-remote-acl-p (vec)
1469 "Check, whether ACL is enabled on the remote host."
1470 (with-tramp-connection-property (tramp-get-connection-process vec) "acl-p"
1471 (tramp-send-command-and-check vec "getfacl /")))
1472
1473 (defun tramp-sh-handle-file-acl (filename)
1474 "Like `file-acl' for Tramp files."
1475 (with-parsed-tramp-file-name filename nil
1476 (with-tramp-file-property v localname "file-acl"
1477 (when (and (tramp-remote-acl-p v)
1478 (tramp-send-command-and-check
1479 v (format
1480 "getfacl -ac %s 2>/dev/null"
1481 (tramp-shell-quote-argument localname))))
1482 (with-current-buffer (tramp-get-connection-buffer v)
1483 (goto-char (point-max))
1484 (delete-blank-lines)
1485 (when (> (point-max) (point-min))
1486 (tramp-compat-funcall
1487 'substring-no-properties (buffer-string))))))))
1488
1489 (defun tramp-sh-handle-set-file-acl (filename acl-string)
1490 "Like `set-file-acl' for Tramp files."
1491 (with-parsed-tramp-file-name (expand-file-name filename) nil
1492 (if (and (stringp acl-string) (tramp-remote-acl-p v)
1493 (progn
1494 (tramp-send-command
1495 v (format "setfacl --set-file=- %s <<'%s'\n%s\n%s\n"
1496 (tramp-shell-quote-argument localname)
1497 tramp-end-of-heredoc
1498 acl-string
1499 tramp-end-of-heredoc))
1500 (tramp-send-command-and-check v nil)))
1501 ;; Success.
1502 (progn
1503 (tramp-set-file-property v localname "file-acl" acl-string)
1504 t)
1505 ;; In case of errors, we return `nil'.
1506 (tramp-set-file-property v localname "file-acl-string" 'undef)
1507 nil)))
1508
1509 ;; Simple functions using the `test' command.
1510
1511 (defun tramp-sh-handle-file-executable-p (filename)
1512 "Like `file-executable-p' for Tramp files."
1513 (with-parsed-tramp-file-name filename nil
1514 (with-tramp-file-property v localname "file-executable-p"
1515 ;; Examine `file-attributes' cache to see if request can be
1516 ;; satisfied without remote operation.
1517 (or (tramp-check-cached-permissions v ?x)
1518 (tramp-run-test "-x" filename)))))
1519
1520 (defun tramp-sh-handle-file-readable-p (filename)
1521 "Like `file-readable-p' for Tramp files."
1522 (with-parsed-tramp-file-name filename nil
1523 (with-tramp-file-property v localname "file-readable-p"
1524 ;; Examine `file-attributes' cache to see if request can be
1525 ;; satisfied without remote operation.
1526 (or (tramp-check-cached-permissions v ?r)
1527 (tramp-run-test "-r" filename)))))
1528
1529 ;; When the remote shell is started, it looks for a shell which groks
1530 ;; tilde expansion. Here, we assume that all shells which grok tilde
1531 ;; expansion will also provide a `test' command which groks `-nt' (for
1532 ;; newer than). If this breaks, tell me about it and I'll try to do
1533 ;; something smarter about it.
1534 (defun tramp-sh-handle-file-newer-than-file-p (file1 file2)
1535 "Like `file-newer-than-file-p' for Tramp files."
1536 (cond ((not (file-exists-p file1))
1537 nil)
1538 ((not (file-exists-p file2))
1539 t)
1540 ;; We are sure both files exist at this point.
1541 (t
1542 (save-excursion
1543 ;; We try to get the mtime of both files. If they are not
1544 ;; equal to the "dont-know" value, then we subtract the times
1545 ;; and obtain the result.
1546 (let ((fa1 (file-attributes file1))
1547 (fa2 (file-attributes file2)))
1548 (if (and (not (equal (nth 5 fa1) '(0 0)))
1549 (not (equal (nth 5 fa2) '(0 0))))
1550 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
1551 ;; If one of them is the dont-know value, then we can
1552 ;; still try to run a shell command on the remote host.
1553 ;; However, this only works if both files are Tramp
1554 ;; files and both have the same method, same user, same
1555 ;; host.
1556 (unless (tramp-equal-remote file1 file2)
1557 (with-parsed-tramp-file-name
1558 (if (tramp-tramp-file-p file1) file1 file2) nil
1559 (tramp-error
1560 v 'file-error
1561 "Files %s and %s must have same method, user, host"
1562 file1 file2)))
1563 (with-parsed-tramp-file-name file1 nil
1564 (tramp-run-test2
1565 (tramp-get-test-nt-command v) file1 file2))))))))
1566
1567 ;; Functions implemented using the basic functions above.
1568
1569 (defun tramp-sh-handle-file-directory-p (filename)
1570 "Like `file-directory-p' for Tramp files."
1571 (with-parsed-tramp-file-name filename nil
1572 ;; `file-directory-p' is used as predicate for file name completion.
1573 ;; Sometimes, when a connection is not established yet, it is
1574 ;; desirable to return t immediately for "/method:foo:". It can
1575 ;; be expected that this is always a directory.
1576 (or (zerop (length localname))
1577 (with-tramp-file-property v localname "file-directory-p"
1578 (tramp-run-test "-d" filename)))))
1579
1580 (defun tramp-sh-handle-file-writable-p (filename)
1581 "Like `file-writable-p' for Tramp files."
1582 (with-parsed-tramp-file-name filename nil
1583 (with-tramp-file-property v localname "file-writable-p"
1584 (if (file-exists-p filename)
1585 ;; Examine `file-attributes' cache to see if request can be
1586 ;; satisfied without remote operation.
1587 (or (tramp-check-cached-permissions v ?w)
1588 (tramp-run-test "-w" filename))
1589 ;; If file doesn't exist, check if directory is writable.
1590 (and (tramp-run-test "-d" (file-name-directory filename))
1591 (tramp-run-test "-w" (file-name-directory filename)))))))
1592
1593 (defun tramp-sh-handle-file-ownership-preserved-p (filename &optional group)
1594 "Like `file-ownership-preserved-p' for Tramp files."
1595 (with-parsed-tramp-file-name filename nil
1596 (with-tramp-file-property v localname "file-ownership-preserved-p"
1597 (let ((attributes (file-attributes filename)))
1598 ;; Return t if the file doesn't exist, since it's true that no
1599 ;; information would be lost by an (attempted) delete and create.
1600 (or (null attributes)
1601 (and
1602 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer))
1603 (or (not group)
1604 (= (nth 3 attributes) (tramp-get-remote-gid v 'integer)))))))))
1605
1606 ;; Directory listings.
1607
1608 (defun tramp-sh-handle-directory-files-and-attributes
1609 (directory &optional full match nosort id-format)
1610 "Like `directory-files-and-attributes' for Tramp files."
1611 (if (with-parsed-tramp-file-name directory nil
1612 (not (or (tramp-get-remote-stat v) (tramp-get-remote-perl v))))
1613 (tramp-handle-directory-files-and-attributes
1614 directory full match nosort id-format)
1615
1616 ;; Do it directly.
1617 (unless id-format (setq id-format 'integer))
1618 (when (file-directory-p directory)
1619 (setq directory (expand-file-name directory))
1620 (let* ((temp
1621 (copy-tree
1622 (with-parsed-tramp-file-name directory nil
1623 (with-tramp-file-property
1624 v localname
1625 (format "directory-files-and-attributes-%s" id-format)
1626 (save-excursion
1627 (mapcar
1628 (lambda (x)
1629 (cons (car x)
1630 (tramp-convert-file-attributes v (cdr x))))
1631 (cond
1632 ((tramp-get-remote-stat v)
1633 (tramp-do-directory-files-and-attributes-with-stat
1634 v localname id-format))
1635 ((tramp-get-remote-perl v)
1636 (tramp-do-directory-files-and-attributes-with-perl
1637 v localname id-format)))))))))
1638 result item)
1639
1640 (while temp
1641 (setq item (pop temp))
1642 (when (or (null match) (string-match match (car item)))
1643 (when full
1644 (setcar item (expand-file-name (car item) directory)))
1645 (push item result)))
1646
1647 (if nosort
1648 result
1649 (sort result (lambda (x y) (string< (car x) (car y)))))))))
1650
1651 (defun tramp-do-directory-files-and-attributes-with-perl
1652 (vec localname &optional id-format)
1653 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1654 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
1655 (tramp-maybe-send-script
1656 vec tramp-perl-directory-files-and-attributes
1657 "tramp_perl_directory_files_and_attributes")
1658 (let ((object
1659 (tramp-send-command-and-read
1660 vec
1661 (format "tramp_perl_directory_files_and_attributes %s %s"
1662 (tramp-shell-quote-argument localname) id-format))))
1663 (when (stringp object) (tramp-error vec 'file-error object))
1664 object))
1665
1666 (defun tramp-do-directory-files-and-attributes-with-stat
1667 (vec localname &optional id-format)
1668 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1669 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
1670 (tramp-send-command-and-read
1671 vec
1672 (format
1673 (concat
1674 ;; We must care about file names with spaces, or starting with
1675 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1676 ;; but it does not work on all remote systems. Therefore, we
1677 ;; quote the file names via sed.
1678 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | "
1679 "xargs %s -c "
1680 "'(\"%%n\" (\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)'"
1681 " 2>/dev/null); echo \")\"")
1682 (tramp-shell-quote-argument localname)
1683 (tramp-get-ls-command vec)
1684 (tramp-get-remote-stat vec)
1685 (if (eq id-format 'integer) "%ue0" "\"%U\"")
1686 (if (eq id-format 'integer) "%ge0" "\"%G\""))))
1687
1688 ;; This function should return "foo/" for directories and "bar" for
1689 ;; files.
1690 (defun tramp-sh-handle-file-name-all-completions (filename directory)
1691 "Like `file-name-all-completions' for Tramp files."
1692 (unless (save-match-data (string-match "/" filename))
1693 (with-parsed-tramp-file-name (expand-file-name directory) nil
1694
1695 (all-completions
1696 filename
1697 (mapcar
1698 'list
1699 (or
1700 ;; Try cache entries for `filename', `filename' with last
1701 ;; character removed, `filename' with last two characters
1702 ;; removed, ..., and finally the empty string - all
1703 ;; concatenated to the local directory name.
1704 (let ((remote-file-name-inhibit-cache
1705 (or remote-file-name-inhibit-cache
1706 tramp-completion-reread-directory-timeout)))
1707
1708 ;; This is inefficient for very long file names, pity
1709 ;; `reduce' is not available...
1710 (car
1711 (apply
1712 'append
1713 (mapcar
1714 (lambda (x)
1715 (let ((cache-hit
1716 (tramp-get-file-property
1717 v
1718 (concat localname (substring filename 0 x))
1719 "file-name-all-completions"
1720 nil)))
1721 (when cache-hit (list cache-hit))))
1722 ;; We cannot use a length of 0, because file properties
1723 ;; for "foo" and "foo/" are identical.
1724 (tramp-compat-number-sequence (length filename) 1 -1)))))
1725
1726 ;; Cache expired or no matching cache entry found so we need
1727 ;; to perform a remote operation.
1728 (let (result)
1729 ;; Get a list of directories and files, including reliably
1730 ;; tagging the directories with a trailing '/'. Because I
1731 ;; rock. --daniel@danann.net
1732
1733 ;; Changed to perform `cd' in the same remote op and only
1734 ;; get entries starting with `filename'. Capture any `cd'
1735 ;; error messages. Ensure any `cd' and `echo' aliases are
1736 ;; ignored.
1737 (tramp-send-command
1738 v
1739 (if (tramp-get-remote-perl v)
1740 (progn
1741 (tramp-maybe-send-script
1742 v tramp-perl-file-name-all-completions
1743 "tramp_perl_file_name_all_completions")
1744 (format "tramp_perl_file_name_all_completions %s %s %d"
1745 (tramp-shell-quote-argument localname)
1746 (tramp-shell-quote-argument filename)
1747 (if (symbol-value
1748 ;; `read-file-name-completion-ignore-case'
1749 ;; is introduced with Emacs 22.1.
1750 (if (boundp
1751 'read-file-name-completion-ignore-case)
1752 'read-file-name-completion-ignore-case
1753 'completion-ignore-case))
1754 1 0)))
1755
1756 (format (concat
1757 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
1758 ;; `ls' with wildcard might fail with `Argument
1759 ;; list too long' error in some corner cases; if
1760 ;; `ls' fails after `cd' succeeded, chances are
1761 ;; that's the case, so let's retry without
1762 ;; wildcard. This will return "too many" entries
1763 ;; but that isn't harmful.
1764 " || %s -a 2>/dev/null)"
1765 " | while IFS= read f; do"
1766 " if %s -d \"$f\" 2>/dev/null;"
1767 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1768 " && \\echo ok) || \\echo fail")
1769 (tramp-shell-quote-argument localname)
1770 (tramp-get-ls-command v)
1771 ;; When `filename' is empty, just `ls' without
1772 ;; `filename' argument is more efficient than `ls *'
1773 ;; for very large directories and might avoid the
1774 ;; `Argument list too long' error.
1775 ;;
1776 ;; With and only with wildcard, we need to add
1777 ;; `-d' to prevent `ls' from descending into
1778 ;; sub-directories.
1779 (if (zerop (length filename))
1780 "."
1781 (concat (tramp-shell-quote-argument filename) "* -d"))
1782 (tramp-get-ls-command v)
1783 (tramp-get-test-command v))))
1784
1785 ;; Now grab the output.
1786 (with-current-buffer (tramp-get-buffer v)
1787 (goto-char (point-max))
1788
1789 ;; Check result code, found in last line of output.
1790 (forward-line -1)
1791 (if (looking-at "^fail$")
1792 (progn
1793 ;; Grab error message from line before last line
1794 ;; (it was put there by `cd 2>&1').
1795 (forward-line -1)
1796 (tramp-error
1797 v 'file-error
1798 "tramp-sh-handle-file-name-all-completions: %s"
1799 (buffer-substring (point) (point-at-eol))))
1800 ;; For peace of mind, if buffer doesn't end in `fail'
1801 ;; then it should end in `ok'. If neither are in the
1802 ;; buffer something went seriously wrong on the remote
1803 ;; side.
1804 (unless (looking-at "^ok$")
1805 (tramp-error
1806 v 'file-error
1807 "\
1808 tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
1809 (tramp-shell-quote-argument localname) (buffer-string))))
1810
1811 (while (zerop (forward-line -1))
1812 (push (buffer-substring (point) (point-at-eol)) result)))
1813
1814 ;; Because the remote op went through OK we know the
1815 ;; directory we `cd'-ed to exists.
1816 (tramp-set-file-property v localname "file-exists-p" t)
1817
1818 ;; Because the remote op went through OK we know every
1819 ;; file listed by `ls' exists.
1820 (mapc (lambda (entry)
1821 (tramp-set-file-property
1822 v (concat localname entry) "file-exists-p" t))
1823 result)
1824
1825 ;; Store result in the cache.
1826 (tramp-set-file-property
1827 v (concat localname filename)
1828 "file-name-all-completions" result))))))))
1829
1830 ;; cp, mv and ln
1831
1832 (defun tramp-sh-handle-add-name-to-file
1833 (filename newname &optional ok-if-already-exists)
1834 "Like `add-name-to-file' for Tramp files."
1835 (unless (tramp-equal-remote filename newname)
1836 (with-parsed-tramp-file-name
1837 (if (tramp-tramp-file-p filename) filename newname) nil
1838 (tramp-error
1839 v 'file-error
1840 "add-name-to-file: %s"
1841 "only implemented for same method, same user, same host")))
1842 (with-parsed-tramp-file-name filename v1
1843 (with-parsed-tramp-file-name newname v2
1844 (let ((ln (when v1 (tramp-get-remote-ln v1))))
1845 (when (and (numberp ok-if-already-exists)
1846 (file-exists-p newname)
1847 (yes-or-no-p
1848 (format
1849 "File %s already exists; make it a new name anyway? "
1850 newname)))
1851 (tramp-error
1852 v2 'file-error "add-name-to-file: file %s already exists" newname))
1853 (when ok-if-already-exists (setq ln (concat ln " -f")))
1854 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1855 (tramp-flush-file-property v2 v2-localname)
1856 (tramp-barf-unless-okay
1857 v1
1858 (format "%s %s %s" ln
1859 (tramp-shell-quote-argument v1-localname)
1860 (tramp-shell-quote-argument v2-localname))
1861 "error with add-name-to-file, see buffer `%s' for details"
1862 (buffer-name))))))
1863
1864 (defun tramp-sh-handle-copy-file
1865 (filename newname &optional ok-if-already-exists keep-date
1866 preserve-uid-gid preserve-extended-attributes)
1867 "Like `copy-file' for Tramp files."
1868 (setq filename (expand-file-name filename))
1869 (setq newname (expand-file-name newname))
1870 (cond
1871 ;; At least one file a Tramp file?
1872 ((or (tramp-tramp-file-p filename)
1873 (tramp-tramp-file-p newname))
1874 (tramp-do-copy-or-rename-file
1875 'copy filename newname ok-if-already-exists keep-date
1876 preserve-uid-gid preserve-extended-attributes))
1877 ;; Compat section.
1878 (preserve-extended-attributes
1879 (tramp-run-real-handler
1880 'copy-file
1881 (list filename newname ok-if-already-exists keep-date
1882 preserve-uid-gid preserve-extended-attributes)))
1883 (preserve-uid-gid
1884 (tramp-run-real-handler
1885 'copy-file
1886 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
1887 (t
1888 (tramp-run-real-handler
1889 'copy-file (list filename newname ok-if-already-exists keep-date)))))
1890
1891 (defun tramp-sh-handle-copy-directory
1892 (dirname newname &optional keep-date parents copy-contents)
1893 "Like `copy-directory' for Tramp files."
1894 (let ((t1 (tramp-tramp-file-p dirname))
1895 (t2 (tramp-tramp-file-p newname)))
1896 (with-parsed-tramp-file-name (if t1 dirname newname) nil
1897 (if (and (not copy-contents)
1898 (tramp-get-method-parameter method 'tramp-copy-recursive)
1899 ;; When DIRNAME and NEWNAME are remote, they must have
1900 ;; the same method.
1901 (or (null t1) (null t2)
1902 (string-equal
1903 (tramp-file-name-method (tramp-dissect-file-name dirname))
1904 (tramp-file-name-method
1905 (tramp-dissect-file-name newname)))))
1906 ;; scp or rsync DTRT.
1907 (progn
1908 (setq dirname (directory-file-name (expand-file-name dirname))
1909 newname (directory-file-name (expand-file-name newname)))
1910 (if (and (file-directory-p newname)
1911 (not (string-equal (file-name-nondirectory dirname)
1912 (file-name-nondirectory newname))))
1913 (setq newname
1914 (expand-file-name
1915 (file-name-nondirectory dirname) newname)))
1916 (if (not (file-directory-p (file-name-directory newname)))
1917 (make-directory (file-name-directory newname) parents))
1918 (tramp-do-copy-or-rename-file-out-of-band
1919 'copy dirname newname keep-date))
1920 ;; We must do it file-wise.
1921 (tramp-run-real-handler
1922 'copy-directory
1923 (if copy-contents
1924 (list dirname newname keep-date parents copy-contents)
1925 (list dirname newname keep-date parents))))
1926
1927 ;; When newname did exist, we have wrong cached values.
1928 (when t2
1929 (with-parsed-tramp-file-name newname nil
1930 (tramp-flush-file-property v (file-name-directory localname))
1931 (tramp-flush-file-property v localname))))))
1932
1933 (defun tramp-sh-handle-rename-file
1934 (filename newname &optional ok-if-already-exists)
1935 "Like `rename-file' for Tramp files."
1936 ;; Check if both files are local -- invoke normal rename-file.
1937 ;; Otherwise, use Tramp from local system.
1938 (setq filename (expand-file-name filename))
1939 (setq newname (expand-file-name newname))
1940 ;; At least one file a Tramp file?
1941 (if (or (tramp-tramp-file-p filename)
1942 (tramp-tramp-file-p newname))
1943 (tramp-do-copy-or-rename-file
1944 'rename filename newname ok-if-already-exists t t)
1945 (tramp-run-real-handler
1946 'rename-file (list filename newname ok-if-already-exists))))
1947
1948 (defun tramp-do-copy-or-rename-file
1949 (op filename newname &optional ok-if-already-exists keep-date
1950 preserve-uid-gid preserve-extended-attributes)
1951 "Copy or rename a remote file.
1952 OP must be `copy' or `rename' and indicates the operation to perform.
1953 FILENAME specifies the file to copy or rename, NEWNAME is the name of
1954 the new file (for copy) or the new name of the file (for rename).
1955 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
1956 KEEP-DATE means to make sure that NEWNAME has the same timestamp
1957 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
1958 the uid and gid if both files are on the same host.
1959 PRESERVE-EXTENDED-ATTRIBUTES activates selinux and acl commands.
1960
1961 This function is invoked by `tramp-sh-handle-copy-file' and
1962 `tramp-sh-handle-rename-file'. It is an error if OP is neither
1963 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
1964 file names."
1965 (unless (memq op '(copy rename))
1966 (error "Unknown operation `%s', must be `copy' or `rename'" op))
1967 (let ((t1 (tramp-tramp-file-p filename))
1968 (t2 (tramp-tramp-file-p newname))
1969 (length (nth 7 (file-attributes (file-truename filename))))
1970 (attributes (and preserve-extended-attributes
1971 (apply 'file-extended-attributes (list filename)))))
1972
1973 (with-parsed-tramp-file-name (if t1 filename newname) nil
1974 (when (and (not ok-if-already-exists) (file-exists-p newname))
1975 (tramp-error
1976 v 'file-already-exists "File %s already exists" newname))
1977
1978 (with-tramp-progress-reporter
1979 v 0 (format "%s %s to %s"
1980 (if (eq op 'copy) "Copying" "Renaming")
1981 filename newname)
1982
1983 (cond
1984 ;; Both are Tramp files.
1985 ((and t1 t2)
1986 (with-parsed-tramp-file-name filename v1
1987 (with-parsed-tramp-file-name newname v2
1988 (cond
1989 ;; Shortcut: if method, host, user are the same for
1990 ;; both files, we invoke `cp' or `mv' on the remote
1991 ;; host directly.
1992 ((tramp-equal-remote filename newname)
1993 (tramp-do-copy-or-rename-file-directly
1994 op filename newname
1995 ok-if-already-exists keep-date preserve-uid-gid))
1996
1997 ;; Try out-of-band operation.
1998 ((and
1999 (tramp-method-out-of-band-p v1 length)
2000 (tramp-method-out-of-band-p v2 length))
2001 (tramp-do-copy-or-rename-file-out-of-band
2002 op filename newname keep-date))
2003
2004 ;; No shortcut was possible. So we copy the file
2005 ;; first. If the operation was `rename', we go back
2006 ;; and delete the original file (if the copy was
2007 ;; successful). The approach is simple-minded: we
2008 ;; create a new buffer, insert the contents of the
2009 ;; source file into it, then write out the buffer to
2010 ;; the target file. The advantage is that it doesn't
2011 ;; matter which file name handlers are used for the
2012 ;; source and target file.
2013 (t
2014 (tramp-do-copy-or-rename-file-via-buffer
2015 op filename newname keep-date))))))
2016
2017 ;; One file is a Tramp file, the other one is local.
2018 ((or t1 t2)
2019 (cond
2020 ;; Fast track on local machine.
2021 ((tramp-local-host-p v)
2022 (tramp-do-copy-or-rename-file-directly
2023 op filename newname
2024 ok-if-already-exists keep-date preserve-uid-gid))
2025
2026 ;; If the Tramp file has an out-of-band method, the
2027 ;; corresponding copy-program can be invoked.
2028 ((tramp-method-out-of-band-p v length)
2029 (tramp-do-copy-or-rename-file-out-of-band
2030 op filename newname keep-date))
2031
2032 ;; Use the inline method via a Tramp buffer.
2033 (t (tramp-do-copy-or-rename-file-via-buffer
2034 op filename newname keep-date))))
2035
2036 (t
2037 ;; One of them must be a Tramp file.
2038 (error "Tramp implementation says this cannot happen")))
2039
2040 ;; Handle `preserve-extended-attributes'. We ignore possible
2041 ;; errors, because ACL strings could be incompatible.
2042 (when attributes
2043 (ignore-errors
2044 (apply 'set-file-extended-attributes (list newname attributes))))
2045
2046 ;; In case of `rename', we must flush the cache of the source file.
2047 (when (and t1 (eq op 'rename))
2048 (with-parsed-tramp-file-name filename v1
2049 (tramp-flush-file-property v1 (file-name-directory v1-localname))
2050 (tramp-flush-file-property v1 v1-localname)))
2051
2052 ;; When newname did exist, we have wrong cached values.
2053 (when t2
2054 (with-parsed-tramp-file-name newname v2
2055 (tramp-flush-file-property v2 (file-name-directory v2-localname))
2056 (tramp-flush-file-property v2 v2-localname)))))))
2057
2058 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
2059 "Use an Emacs buffer to copy or rename a file.
2060 First arg OP is either `copy' or `rename' and indicates the operation.
2061 FILENAME is the source file, NEWNAME the target file.
2062 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2063 (with-temp-buffer
2064 ;; We must disable multibyte, because binary data shall not be
2065 ;; converted. We remove `tramp-file-name-handler' from
2066 ;; `inhibit-file-name-handlers'; otherwise the file name handler
2067 ;; for `insert-file-contents' might be deactivated in some corner
2068 ;; cases.
2069 (set-buffer-multibyte nil)
2070 (let ((coding-system-for-read 'binary)
2071 (jka-compr-inhibit t)
2072 (inhibit-file-name-handlers
2073 (remq 'tramp-file-name-handler inhibit-file-name-handlers)))
2074 (insert-file-contents-literally filename))
2075 ;; We don't want the target file to be compressed, so we let-bind
2076 ;; `jka-compr-inhibit' to t.
2077 (let ((coding-system-for-write 'binary)
2078 (jka-compr-inhibit t))
2079 (write-region (point-min) (point-max) newname nil 'no-message)))
2080 ;; KEEP-DATE handling.
2081 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
2082 ;; Set the mode.
2083 (set-file-modes newname (tramp-default-file-modes filename))
2084 ;; If the operation was `rename', delete the original file.
2085 (unless (eq op 'copy) (delete-file filename)))
2086
2087 (defun tramp-do-copy-or-rename-file-directly
2088 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
2089 "Invokes `cp' or `mv' on the remote system.
2090 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2091 respectively. FILENAME specifies the file to copy or rename,
2092 NEWNAME is the name of the new file (for copy) or the new name of
2093 the file (for rename). Both files must reside on the same host.
2094 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2095 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2096 the uid and gid from FILENAME."
2097 (let ((t1 (tramp-tramp-file-p filename))
2098 (t2 (tramp-tramp-file-p newname))
2099 (file-times (nth 5 (file-attributes filename)))
2100 (file-modes (tramp-default-file-modes filename)))
2101 (with-parsed-tramp-file-name (if t1 filename newname) nil
2102 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
2103 ((eq op 'copy) "cp -f")
2104 ((eq op 'rename) "mv -f")
2105 (t (tramp-error
2106 v 'file-error
2107 "Unknown operation `%s', must be `copy' or `rename'"
2108 op))))
2109 (localname1
2110 (if t1
2111 (tramp-file-name-handler 'file-remote-p filename 'localname)
2112 filename))
2113 (localname2
2114 (if t2
2115 (tramp-file-name-handler 'file-remote-p newname 'localname)
2116 newname))
2117 (prefix (file-remote-p (if t1 filename newname)))
2118 cmd-result)
2119
2120 (cond
2121 ;; Both files are on a remote host, with same user.
2122 ((and t1 t2)
2123 (setq cmd-result
2124 (tramp-send-command-and-check
2125 v (format "%s %s %s" cmd
2126 (tramp-shell-quote-argument localname1)
2127 (tramp-shell-quote-argument localname2))))
2128 (with-current-buffer (tramp-get-buffer v)
2129 (goto-char (point-min))
2130 (unless
2131 (or
2132 (and keep-date
2133 ;; Mask cp -f error.
2134 (re-search-forward
2135 tramp-operation-not-permitted-regexp nil t))
2136 cmd-result)
2137 (tramp-error-with-buffer
2138 nil v 'file-error
2139 "Copying directly failed, see buffer `%s' for details."
2140 (buffer-name)))))
2141
2142 ;; We are on the local host.
2143 ((or t1 t2)
2144 (cond
2145 ;; We can do it directly.
2146 ((let (file-name-handler-alist)
2147 (and (file-readable-p localname1)
2148 ;; No sticky bit when renaming.
2149 (or (eq op 'copy)
2150 (zerop
2151 (logand
2152 (file-modes (file-name-directory localname1))
2153 (tramp-compat-octal-to-decimal "1000"))))
2154 (file-writable-p (file-name-directory localname2))
2155 (or (file-directory-p localname2)
2156 (file-writable-p localname2))))
2157 (if (eq op 'copy)
2158 (tramp-compat-copy-file
2159 localname1 localname2 ok-if-already-exists
2160 keep-date preserve-uid-gid)
2161 (tramp-run-real-handler
2162 'rename-file (list localname1 localname2 ok-if-already-exists))))
2163
2164 ;; We can do it directly with `tramp-send-command'
2165 ((and (file-readable-p (concat prefix localname1))
2166 (file-writable-p
2167 (file-name-directory (concat prefix localname2)))
2168 (or (file-directory-p (concat prefix localname2))
2169 (file-writable-p (concat prefix localname2))))
2170 (tramp-do-copy-or-rename-file-directly
2171 op (concat prefix localname1) (concat prefix localname2)
2172 ok-if-already-exists keep-date t)
2173 ;; We must change the ownership to the local user.
2174 (tramp-set-file-uid-gid
2175 (concat prefix localname2)
2176 (tramp-get-local-uid 'integer)
2177 (tramp-get-local-gid 'integer)))
2178
2179 ;; We need a temporary file in between.
2180 (t
2181 ;; Create the temporary file.
2182 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
2183 (unwind-protect
2184 (progn
2185 (cond
2186 (t1
2187 (tramp-barf-unless-okay
2188 v (format
2189 "%s %s %s" cmd
2190 (tramp-shell-quote-argument localname1)
2191 (tramp-shell-quote-argument tmpfile))
2192 "Copying directly failed, see buffer `%s' for details."
2193 (tramp-get-buffer v))
2194 ;; We must change the ownership as remote user.
2195 ;; Since this does not work reliable, we also
2196 ;; give read permissions.
2197 (set-file-modes
2198 (concat prefix tmpfile)
2199 (tramp-compat-octal-to-decimal "0777"))
2200 (tramp-set-file-uid-gid
2201 (concat prefix tmpfile)
2202 (tramp-get-local-uid 'integer)
2203 (tramp-get-local-gid 'integer)))
2204 (t2
2205 (if (eq op 'copy)
2206 (tramp-compat-copy-file
2207 localname1 tmpfile t
2208 keep-date preserve-uid-gid)
2209 (tramp-run-real-handler
2210 'rename-file
2211 (list localname1 tmpfile t)))
2212 ;; We must change the ownership as local user.
2213 ;; Since this does not work reliable, we also
2214 ;; give read permissions.
2215 (set-file-modes
2216 tmpfile (tramp-compat-octal-to-decimal "0777"))
2217 (tramp-set-file-uid-gid
2218 tmpfile
2219 (tramp-get-remote-uid v 'integer)
2220 (tramp-get-remote-gid v 'integer))))
2221
2222 ;; Move the temporary file to its destination.
2223 (cond
2224 (t2
2225 (tramp-barf-unless-okay
2226 v (format
2227 "cp -f -p %s %s"
2228 (tramp-shell-quote-argument tmpfile)
2229 (tramp-shell-quote-argument localname2))
2230 "Copying directly failed, see buffer `%s' for details."
2231 (tramp-get-buffer v)))
2232 (t1
2233 (tramp-run-real-handler
2234 'rename-file
2235 (list tmpfile localname2 ok-if-already-exists)))))
2236
2237 ;; Save exit.
2238 (ignore-errors (delete-file tmpfile)))))))))
2239
2240 ;; Set the time and mode. Mask possible errors.
2241 (ignore-errors
2242 (when keep-date
2243 (set-file-times newname file-times)
2244 (set-file-modes newname file-modes))))))
2245
2246 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
2247 "Invoke `scp' program to copy.
2248 The method used must be an out-of-band method."
2249 (let* ((t1 (tramp-tramp-file-p filename))
2250 (t2 (tramp-tramp-file-p newname))
2251 (orig-vec (tramp-dissect-file-name (if t1 filename newname)))
2252 copy-program copy-args copy-env copy-keep-date port listener spec
2253 options source target remote-copy-program remote-copy-args)
2254
2255 (with-parsed-tramp-file-name (if t1 filename newname) nil
2256 (if (and t1 t2)
2257
2258 ;; Both are Tramp files. We shall optimize it when the
2259 ;; methods for FILENAME and NEWNAME are the same.
2260 (let* ((dir-flag (file-directory-p filename))
2261 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
2262 (if dir-flag
2263 (setq tmpfile
2264 (expand-file-name
2265 (file-name-nondirectory newname) tmpfile)))
2266 (unwind-protect
2267 (progn
2268 (tramp-do-copy-or-rename-file-out-of-band
2269 op filename tmpfile keep-date)
2270 (tramp-do-copy-or-rename-file-out-of-band
2271 'rename tmpfile newname keep-date))
2272 ;; Save exit.
2273 (ignore-errors
2274 (if dir-flag
2275 (tramp-compat-delete-directory
2276 (expand-file-name ".." tmpfile) 'recursive)
2277 (delete-file tmpfile)))))
2278
2279 ;; Set variables for computing the prompt for reading
2280 ;; password.
2281 (setq tramp-current-method (tramp-file-name-method v)
2282 tramp-current-user (or (tramp-file-name-user v)
2283 (tramp-get-connection-property
2284 v "login-as" nil))
2285 tramp-current-host (tramp-file-name-real-host v))
2286
2287 ;; Expand hops. Might be necessary for gateway methods.
2288 (setq v (car (tramp-compute-multi-hops v)))
2289 (aset v 3 localname)
2290
2291 ;; Check which ones of source and target are Tramp files.
2292 (setq source (if t1
2293 (tramp-make-copy-program-file-name v)
2294 (shell-quote-argument filename))
2295 target (funcall
2296 (if (and (file-directory-p filename)
2297 (string-equal
2298 (file-name-nondirectory filename)
2299 (file-name-nondirectory newname)))
2300 'file-name-directory
2301 'identity)
2302 (if t2
2303 (tramp-make-copy-program-file-name v)
2304 (shell-quote-argument newname))))
2305
2306 ;; Check for host and port number. We cannot use
2307 ;; `tramp-file-name-port', because this returns also
2308 ;; `tramp-default-port', which might clash with settings in
2309 ;; "~/.ssh/config".
2310 (setq host (tramp-file-name-host v)
2311 port "")
2312 (when (string-match tramp-host-with-port-regexp host)
2313 (setq port (string-to-number (match-string 2 host))
2314 host (string-to-number (match-string 1 host))))
2315
2316 ;; Check for user. There might be an interactive setting.
2317 (setq user (or (tramp-file-name-user v)
2318 (tramp-get-connection-property v "login-as" nil)))
2319
2320 ;; Check for listener port.
2321 (when (tramp-get-method-parameter method 'tramp-remote-copy-args)
2322 (setq listener (number-to-string (+ 50000 (random 10000))))
2323 (while
2324 (zerop (tramp-call-process v "nc" nil nil nil "-z" host listener))
2325 (setq listener (number-to-string (+ 50000 (random 10000))))))
2326
2327 ;; Compose copy command.
2328 (setq host (or host "")
2329 user (or user "")
2330 port (or port "")
2331 spec (format-spec-make
2332 ?t (tramp-get-connection-property
2333 (tramp-get-connection-process v) "temp-file" ""))
2334 options (format-spec
2335 (if tramp-use-ssh-controlmaster-options
2336 tramp-ssh-controlmaster-options "")
2337 spec)
2338 spec (format-spec-make
2339 ?h host ?u user ?p port ?r listener ?c options
2340 ?k (if keep-date " " ""))
2341 copy-program (tramp-get-method-parameter
2342 method 'tramp-copy-program)
2343 copy-keep-date (tramp-get-method-parameter
2344 method 'tramp-copy-keep-date)
2345
2346 copy-args
2347 (delete
2348 ;; " " has either been a replacement of "%k" (when
2349 ;; keep-date argument is non-nil), or a replacement
2350 ;; for the whole keep-date sublist.
2351 " "
2352 (dolist
2353 (x
2354 (tramp-get-method-parameter method 'tramp-copy-args)
2355 copy-args)
2356 (setq copy-args
2357 (append
2358 copy-args
2359 (let ((y (mapcar (lambda (z) (format-spec z spec)) x)))
2360 (if (member "" y) '(" ") y))))))
2361
2362 copy-env
2363 (delq
2364 nil
2365 (mapcar
2366 (lambda (x)
2367 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
2368 (unless (member "" x) (mapconcat 'identity x " ")))
2369 (tramp-get-method-parameter method 'tramp-copy-env)))
2370
2371 remote-copy-program
2372 (tramp-get-method-parameter method 'tramp-remote-copy-program))
2373
2374 (dolist
2375 (x
2376 (or
2377 (tramp-get-connection-property v "remote-copy-args" nil)
2378 (tramp-get-method-parameter method 'tramp-remote-copy-args)))
2379 (setq remote-copy-args
2380 (append
2381 remote-copy-args
2382 (let ((y (mapcar (lambda (z) (format-spec z spec)) x)))
2383 (if (member "" y) '(" ") y)))))
2384
2385 ;; Check for local copy program.
2386 (unless (executable-find copy-program)
2387 (tramp-error
2388 v 'file-error "Cannot find local copy program: %s" copy-program))
2389
2390 ;; Install listener on the remote side. The prompt must be
2391 ;; consumed later on, when the process does not listen anymore.
2392 (when remote-copy-program
2393 (unless (with-tramp-connection-property
2394 v (concat "remote-copy-program-" remote-copy-program)
2395 (tramp-find-executable
2396 v remote-copy-program (tramp-get-remote-path v)))
2397 (tramp-error
2398 v 'file-error
2399 "Cannot find remote listener: %s" remote-copy-program))
2400 (setq remote-copy-program
2401 (mapconcat
2402 'identity
2403 (append
2404 (list remote-copy-program) remote-copy-args
2405 (list (if t1 (concat "<" source) (concat ">" target)) "&"))
2406 " "))
2407 (tramp-send-command v remote-copy-program)
2408 (with-timeout
2409 (1 (tramp-error
2410 v 'file-error
2411 "Listener process not running on remote host: `%s'"
2412 remote-copy-program))
2413 (tramp-send-command v (format "netstat -l | grep -q :%s" listener))
2414 (while (not (tramp-send-command-and-check v nil))
2415 (tramp-send-command
2416 v (format "netstat -l | grep -q :%s" listener)))))
2417
2418 (with-temp-buffer
2419 (unwind-protect
2420 ;; The default directory must be remote.
2421 (let ((default-directory
2422 (file-name-directory (if t1 filename newname)))
2423 (process-environment (copy-sequence process-environment)))
2424 ;; Set the transfer process properties.
2425 (tramp-set-connection-property
2426 v "process-name" (buffer-name (current-buffer)))
2427 (tramp-set-connection-property
2428 v "process-buffer" (current-buffer))
2429 (while copy-env
2430 (tramp-message
2431 orig-vec 6 "%s=\"%s\"" (car copy-env) (cadr copy-env))
2432 (setenv (pop copy-env) (pop copy-env)))
2433 (setq
2434 copy-args
2435 (append
2436 copy-args
2437 (if remote-copy-program
2438 (list (if t1 (concat ">" target) (concat "<" source)))
2439 (list source target))))
2440
2441 ;; Use an asynchronous process. By this, password can
2442 ;; be handled. We don't set a timeout, because the
2443 ;; copying of large files can last longer than 60
2444 ;; secs.
2445 (let ((p (apply 'start-process-shell-command
2446 (tramp-get-connection-name v)
2447 (tramp-get-connection-buffer v)
2448 copy-program
2449 (append
2450 copy-args
2451 (list "&&" "echo" "tramp_exit_status" "0"
2452 "||" "echo" "tramp_exit_status" "1")))))
2453 (tramp-message
2454 orig-vec 6 "%s"
2455 (mapconcat 'identity (process-command p) " "))
2456 (tramp-set-connection-property p "vector" orig-vec)
2457 (tramp-compat-set-process-query-on-exit-flag p nil)
2458 (tramp-process-actions
2459 p v nil tramp-actions-copy-out-of-band)
2460
2461 ;; Check the return code.
2462 (goto-char (point-max))
2463 (unless
2464 (re-search-backward "tramp_exit_status [0-9]+" nil t)
2465 (tramp-error
2466 orig-vec 'file-error
2467 "Couldn't find exit status of `%s'"
2468 (mapconcat 'identity (process-command p) " ")))
2469 (skip-chars-forward "^ ")
2470 (unless (zerop (read (current-buffer)))
2471 (forward-line -1)
2472 (tramp-error
2473 orig-vec 'file-error
2474 "Error copying: `%s'"
2475 (buffer-substring (point-min) (point-at-eol))))))
2476
2477 ;; Reset the transfer process properties.
2478 (tramp-set-connection-property v "process-name" nil)
2479 (tramp-set-connection-property v "process-buffer" nil)
2480 ;; Clear the remote prompt.
2481 (when (and remote-copy-program
2482 (not (tramp-send-command-and-check v nil)))
2483 ;; Houston, we have a problem! Likely, the listener is
2484 ;; still running, so let's clear everything (but the
2485 ;; cached password).
2486 (tramp-cleanup-connection v 'keep-debug 'keep-password))))
2487
2488 ;; Handle KEEP-DATE argument.
2489 (when (and keep-date (not copy-keep-date))
2490 (set-file-times newname (nth 5 (file-attributes filename))))
2491
2492 ;; Set the mode.
2493 (unless (and keep-date copy-keep-date)
2494 (ignore-errors
2495 (set-file-modes newname (tramp-default-file-modes filename)))))
2496
2497 ;; If the operation was `rename', delete the original file.
2498 (unless (eq op 'copy)
2499 (if (file-regular-p filename)
2500 (delete-file filename)
2501 (tramp-compat-delete-directory filename 'recursive))))))
2502
2503 (defun tramp-sh-handle-make-directory (dir &optional parents)
2504 "Like `make-directory' for Tramp files."
2505 (setq dir (expand-file-name dir))
2506 (with-parsed-tramp-file-name dir nil
2507 (tramp-flush-directory-property v (file-name-directory localname))
2508 (save-excursion
2509 (tramp-barf-unless-okay
2510 v (format "%s %s"
2511 (if parents "mkdir -p" "mkdir")
2512 (tramp-shell-quote-argument localname))
2513 "Couldn't make directory %s" dir))))
2514
2515 (defun tramp-sh-handle-delete-directory (directory &optional recursive)
2516 "Like `delete-directory' for Tramp files."
2517 (setq directory (expand-file-name directory))
2518 (with-parsed-tramp-file-name directory nil
2519 (tramp-flush-file-property v (file-name-directory localname))
2520 (tramp-flush-directory-property v localname)
2521 (tramp-barf-unless-okay
2522 v (format "%s %s"
2523 (if recursive "rm -rf" "rmdir")
2524 (tramp-shell-quote-argument localname))
2525 "Couldn't delete %s" directory)))
2526
2527 (defun tramp-sh-handle-delete-file (filename &optional trash)
2528 "Like `delete-file' for Tramp files."
2529 (setq filename (expand-file-name filename))
2530 (with-parsed-tramp-file-name filename nil
2531 (tramp-flush-file-property v (file-name-directory localname))
2532 (tramp-flush-file-property v localname)
2533 (tramp-barf-unless-okay
2534 v (format "%s %s"
2535 (or (and trash (tramp-get-remote-trash v)) "rm -f")
2536 (tramp-shell-quote-argument localname))
2537 "Couldn't delete %s" filename)))
2538
2539 ;; Dired.
2540
2541 ;; CCC: This does not seem to be enough. Something dies when
2542 ;; we try and delete two directories under Tramp :/
2543 (defun tramp-sh-handle-dired-recursive-delete-directory (filename)
2544 "Recursively delete the directory given.
2545 This is like `dired-recursive-delete-directory' for Tramp files."
2546 (with-parsed-tramp-file-name filename nil
2547 ;; Run a shell command 'rm -r <localname>'.
2548 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
2549 (unless (file-exists-p filename)
2550 (tramp-error v 'file-error "No such directory: %s" filename))
2551 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>).
2552 (tramp-send-command
2553 v
2554 (format "rm -rf %s" (tramp-shell-quote-argument localname))
2555 ;; Don't read the output, do it explicitly.
2556 nil t)
2557 ;; Wait for the remote system to return to us...
2558 ;; This might take a while, allow it plenty of time.
2559 (tramp-wait-for-output (tramp-get-connection-process v) 120)
2560 ;; Make sure that it worked...
2561 (tramp-flush-file-property v (file-name-directory localname))
2562 (tramp-flush-directory-property v localname)
2563 (and (file-exists-p filename)
2564 (tramp-error
2565 v 'file-error "Failed to recursively delete %s" filename))))
2566
2567 (defun tramp-sh-handle-dired-compress-file (file &rest _ok-flag)
2568 "Like `dired-compress-file' for Tramp files."
2569 ;; OK-FLAG is valid for XEmacs only, but not implemented.
2570 ;; Code stolen mainly from dired-aux.el.
2571 (with-parsed-tramp-file-name file nil
2572 (tramp-flush-file-property v localname)
2573 (save-excursion
2574 (let ((suffixes
2575 (if (not (featurep 'xemacs))
2576 ;; Emacs case
2577 (symbol-value 'dired-compress-file-suffixes)
2578 ;; XEmacs has `dired-compression-method-alist', which is
2579 ;; transformed into `dired-compress-file-suffixes' structure.
2580 (mapcar
2581 (lambda (x)
2582 (list (concat (regexp-quote (nth 1 x)) "\\'")
2583 nil
2584 (mapconcat 'identity (nth 3 x) " ")))
2585 (symbol-value 'dired-compression-method-alist))))
2586 suffix)
2587 ;; See if any suffix rule matches this file name.
2588 (while suffixes
2589 (let (case-fold-search)
2590 (if (string-match (car (car suffixes)) localname)
2591 (setq suffix (car suffixes) suffixes nil))
2592 (setq suffixes (cdr suffixes))))
2593
2594 (cond ((file-symlink-p file)
2595 nil)
2596 ((and suffix (nth 2 suffix))
2597 ;; We found an uncompression rule.
2598 (with-tramp-progress-reporter
2599 v 0 (format "Uncompressing %s" file)
2600 (when (tramp-send-command-and-check
2601 v (concat (nth 2 suffix) " "
2602 (tramp-shell-quote-argument localname)))
2603 ;; `dired-remove-file' is not defined in XEmacs.
2604 (tramp-compat-funcall 'dired-remove-file file)
2605 (string-match (car suffix) file)
2606 (concat (substring file 0 (match-beginning 0))))))
2607 (t
2608 ;; We don't recognize the file as compressed, so compress it.
2609 ;; Try gzip.
2610 (with-tramp-progress-reporter v 0 (format "Compressing %s" file)
2611 (when (tramp-send-command-and-check
2612 v (concat "gzip -f "
2613 (tramp-shell-quote-argument localname)))
2614 ;; `dired-remove-file' is not defined in XEmacs.
2615 (tramp-compat-funcall 'dired-remove-file file)
2616 (cond ((file-exists-p (concat file ".gz"))
2617 (concat file ".gz"))
2618 ((file-exists-p (concat file ".z"))
2619 (concat file ".z"))
2620 (t nil))))))))))
2621
2622 (defun tramp-sh-handle-insert-directory
2623 (filename switches &optional wildcard full-directory-p)
2624 "Like `insert-directory' for Tramp files."
2625 (setq filename (expand-file-name filename))
2626 (unless switches (setq switches ""))
2627 (with-parsed-tramp-file-name filename nil
2628 (if (and (featurep 'ls-lisp)
2629 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
2630 (tramp-handle-insert-directory
2631 filename switches wildcard full-directory-p)
2632 (when (stringp switches)
2633 (setq switches (split-string switches)))
2634 (when (and (member "--dired" switches)
2635 (not (tramp-get-ls-command-with-dired v)))
2636 (setq switches (delete "--dired" switches)))
2637 (when wildcard
2638 (setq wildcard (tramp-run-real-handler
2639 'file-name-nondirectory (list localname)))
2640 (setq localname (tramp-run-real-handler
2641 'file-name-directory (list localname))))
2642 (unless (or full-directory-p (member "-d" switches))
2643 (setq switches (append switches '("-d"))))
2644 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
2645 (when wildcard
2646 (setq switches (concat switches " " wildcard)))
2647 (tramp-message
2648 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2649 switches filename (if wildcard "yes" "no")
2650 (if full-directory-p "yes" "no"))
2651 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2652 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2653 (if full-directory-p
2654 (tramp-send-command
2655 v
2656 (format "%s %s %s 2>/dev/null"
2657 (tramp-get-ls-command v)
2658 switches
2659 (if wildcard
2660 localname
2661 (tramp-shell-quote-argument (concat localname ".")))))
2662 (tramp-barf-unless-okay
2663 v
2664 (format "cd %s" (tramp-shell-quote-argument
2665 (tramp-run-real-handler
2666 'file-name-directory (list localname))))
2667 "Couldn't `cd %s'"
2668 (tramp-shell-quote-argument
2669 (tramp-run-real-handler 'file-name-directory (list localname))))
2670 (tramp-send-command
2671 v
2672 (format "%s %s %s 2>/dev/null"
2673 (tramp-get-ls-command v)
2674 switches
2675 (if (or wildcard
2676 (zerop (length
2677 (tramp-run-real-handler
2678 'file-name-nondirectory (list localname)))))
2679 ""
2680 (tramp-shell-quote-argument
2681 (tramp-run-real-handler
2682 'file-name-nondirectory (list localname)))))))
2683
2684 (save-restriction
2685 (let ((beg (point)))
2686 (narrow-to-region (point) (point))
2687 ;; We cannot use `insert-buffer-substring' because the Tramp
2688 ;; buffer changes its contents before insertion due to calling
2689 ;; `expand-file' and alike.
2690 (insert
2691 (with-current-buffer (tramp-get-buffer v)
2692 (buffer-string)))
2693
2694 ;; Check for "--dired" output.
2695 (forward-line -2)
2696 (when (looking-at "//SUBDIRED//")
2697 (forward-line -1))
2698 (when (looking-at "//DIRED//\\s-+")
2699 (let ((databeg (match-end 0))
2700 (end (point-at-eol)))
2701 ;; Now read the numeric positions of file names.
2702 (goto-char databeg)
2703 (while (< (point) end)
2704 (let ((start (+ beg (read (current-buffer))))
2705 (end (+ beg (read (current-buffer)))))
2706 (if (memq (char-after end) '(?\n ?\ ))
2707 ;; End is followed by \n or by " -> ".
2708 (put-text-property start end 'dired-filename t))))))
2709 ;; Remove trailing lines.
2710 (goto-char (point-at-bol))
2711 (while (looking-at "//")
2712 (forward-line 1)
2713 (delete-region (match-beginning 0) (point)))
2714
2715 ;; Some busyboxes are reluctant to discard colors.
2716 (unless
2717 (string-match "color" (tramp-get-connection-property v "ls" ""))
2718 (goto-char beg)
2719 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
2720 (replace-match "")))
2721
2722 ;; Decode the output, it could be multibyte.
2723 (decode-coding-region
2724 beg (point-max)
2725 (or file-name-coding-system
2726 (and (boundp 'default-file-name-coding-system)
2727 (symbol-value 'default-file-name-coding-system))))
2728
2729 ;; The inserted file could be from somewhere else.
2730 (when (and (not wildcard) (not full-directory-p))
2731 (goto-char (point-max))
2732 (when (file-symlink-p filename)
2733 (goto-char (search-backward "->" beg 'noerror)))
2734 (search-backward
2735 (if (zerop (length (file-name-nondirectory filename)))
2736 "."
2737 (file-name-nondirectory filename))
2738 beg 'noerror)
2739 (replace-match (file-relative-name filename) t))
2740
2741 (goto-char (point-max)))))))
2742
2743 ;; Canonicalization of file names.
2744
2745 (defun tramp-sh-handle-expand-file-name (name &optional dir)
2746 "Like `expand-file-name' for Tramp files.
2747 If the localname part of the given file name starts with \"/../\" then
2748 the result will be a local, non-Tramp, file name."
2749 ;; If DIR is not given, use `default-directory' or "/".
2750 (setq dir (or dir default-directory "/"))
2751 ;; Unless NAME is absolute, concat DIR and NAME.
2752 (unless (file-name-absolute-p name)
2753 (setq name (concat (file-name-as-directory dir) name)))
2754 ;; If NAME is not a Tramp file, run the real handler.
2755 (if (not (tramp-connectable-p name))
2756 (tramp-run-real-handler 'expand-file-name (list name nil))
2757 ;; Dissect NAME.
2758 (with-parsed-tramp-file-name name nil
2759 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
2760 (setq localname (concat "~/" localname)))
2761 ;; Tilde expansion if necessary. This needs a shell which
2762 ;; groks tilde expansion! The function `tramp-find-shell' is
2763 ;; supposed to find such a shell on the remote host. Please
2764 ;; tell me about it when this doesn't work on your system.
2765 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
2766 (let ((uname (match-string 1 localname))
2767 (fname (match-string 2 localname)))
2768 ;; We cannot simply apply "~/", because under sudo "~/" is
2769 ;; expanded to the local user home directory but to the
2770 ;; root home directory. On the other hand, using always
2771 ;; the default user name for tilde expansion is not
2772 ;; appropriate either, because ssh and companions might
2773 ;; use a user name from the config file.
2774 (when (and (string-equal uname "~")
2775 (string-match "\\`su\\(do\\)?\\'" method))
2776 (setq uname (concat uname user)))
2777 (setq uname
2778 (with-tramp-connection-property v uname
2779 (tramp-send-command
2780 v (format "cd %s; pwd" (tramp-shell-quote-argument uname)))
2781 (with-current-buffer (tramp-get-buffer v)
2782 (goto-char (point-min))
2783 (buffer-substring (point) (point-at-eol)))))
2784 (setq localname (concat uname fname))))
2785 ;; There might be a double slash, for example when "~/"
2786 ;; expands to "/". Remove this.
2787 (while (string-match "//" localname)
2788 (setq localname (replace-match "/" t t localname)))
2789 ;; No tilde characters in file name, do normal
2790 ;; `expand-file-name' (this does "/./" and "/../"). We bind
2791 ;; `directory-sep-char' here for XEmacs on Windows, which would
2792 ;; otherwise use backslash. `default-directory' is bound,
2793 ;; because on Windows there would be problems with UNC shares or
2794 ;; Cygwin mounts.
2795 (let ((directory-sep-char ?/)
2796 (default-directory (tramp-compat-temporary-file-directory)))
2797 (tramp-make-tramp-file-name
2798 method user host
2799 (tramp-drop-volume-letter
2800 (tramp-run-real-handler
2801 'expand-file-name (list localname)))
2802 hop)))))
2803
2804 ;;; Remote commands:
2805
2806 (defun tramp-process-sentinel (proc event)
2807 "Flush file caches."
2808 (unless (memq (process-status proc) '(run open))
2809 (let ((vec (tramp-get-connection-property proc "vector" nil)))
2810 (when vec
2811 (tramp-message vec 5 "Sentinel called: `%S' `%s'" proc event)
2812 (tramp-flush-connection-property proc)
2813 (tramp-flush-directory-property vec "")))))
2814
2815 ;; We use BUFFER also as connection buffer during setup. Because of
2816 ;; this, its original contents must be saved, and restored once
2817 ;; connection has been setup.
2818 (defun tramp-sh-handle-start-file-process (name buffer program &rest args)
2819 "Like `start-file-process' for Tramp files."
2820 (with-parsed-tramp-file-name (expand-file-name default-directory) nil
2821 (let* (;; When PROGRAM matches "*sh", and the first arg is "-c",
2822 ;; it might be that the arguments exceed the command line
2823 ;; length. Therefore, we modify the command.
2824 (heredoc (and (stringp program)
2825 (string-match "sh$" program)
2826 (string-equal "-c" (car args))
2827 (= (length args) 2)))
2828 ;; When PROGRAM is nil, we just provide a tty.
2829 (args (if (not heredoc) args
2830 (let ((i 250))
2831 (while (and (< i (length (cadr args)))
2832 (string-match " " (cadr args) i))
2833 (setcdr
2834 args
2835 (list (replace-match " \\\\\n" nil nil (cadr args))))
2836 (setq i (+ i 250))))
2837 (cdr args)))
2838 ;; Use a human-friendly prompt, for example for `shell'.
2839 (prompt (format "PS1=%s"
2840 (format "%s %s"
2841 (file-remote-p default-directory)
2842 tramp-initial-end-of-output)))
2843 ;; We use as environment the difference to toplevel
2844 ;; `process-environment'.
2845 env
2846 (env
2847 (dolist
2848 (elt
2849 (cons prompt (nreverse (copy-sequence process-environment)))
2850 env)
2851 (or (member elt (default-toplevel-value 'process-environment))
2852 (setq env (cons elt env)))))
2853 (command
2854 (when (stringp program)
2855 (format "cd %s && exec %s env %s %s"
2856 (tramp-shell-quote-argument localname)
2857 (if heredoc (format "<<'%s'" tramp-end-of-heredoc) "")
2858 (mapconcat 'tramp-shell-quote-argument env " ")
2859 (if heredoc
2860 (format "%s\n(\n%s\n) </dev/tty\n%s"
2861 program (car args) tramp-end-of-heredoc)
2862 (mapconcat 'tramp-shell-quote-argument
2863 (cons program args) " ")))))
2864 (tramp-process-connection-type
2865 (or (null program) tramp-process-connection-type))
2866 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
2867 (name1 name)
2868 (i 0)
2869 ;; We do not want to raise an error when
2870 ;; `start-file-process' has been started several time in
2871 ;; `eshell' and friends.
2872 (tramp-current-connection nil))
2873
2874 (unless buffer
2875 ;; BUFFER can be nil. We use a temporary buffer.
2876 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
2877 (while (get-process name1)
2878 ;; NAME must be unique as process name.
2879 (setq i (1+ i)
2880 name1 (format "%s<%d>" name i)))
2881 (setq name name1)
2882 ;; Set the new process properties.
2883 (tramp-set-connection-property v "process-name" name)
2884 (tramp-set-connection-property v "process-buffer" buffer)
2885
2886 (with-current-buffer (tramp-get-connection-buffer v)
2887 (unwind-protect
2888 ;; We catch this event. Otherwise, `start-process' could
2889 ;; be called on the local host.
2890 (save-excursion
2891 (save-restriction
2892 ;; Activate narrowing in order to save BUFFER
2893 ;; contents. Clear also the modification time;
2894 ;; otherwise we might be interrupted by
2895 ;; `verify-visited-file-modtime'.
2896 (let ((buffer-undo-list t)
2897 (buffer-read-only nil)
2898 (mark (point-max)))
2899 (clear-visited-file-modtime)
2900 (narrow-to-region (point-max) (point-max))
2901 ;; We call `tramp-maybe-open-connection', in order
2902 ;; to cleanup the prompt afterwards.
2903 (catch 'suppress
2904 (tramp-maybe-open-connection v)
2905 (widen)
2906 (delete-region mark (point))
2907 (narrow-to-region (point-max) (point-max))
2908 ;; Now do it.
2909 (if command
2910 ;; Send the command.
2911 (tramp-send-command v command nil t) ; nooutput
2912 ;; Check, whether a pty is associated.
2913 (unless (tramp-compat-process-get
2914 (tramp-get-connection-process v) 'remote-tty)
2915 (tramp-error
2916 v 'file-error
2917 "pty association is not supported for `%s'" name))))
2918 (let ((p (tramp-get-connection-process v)))
2919 ;; Set query flag and process marker for this
2920 ;; process. We ignore errors, because the process
2921 ;; could have finished already.
2922 (ignore-errors
2923 (tramp-compat-set-process-query-on-exit-flag p t)
2924 (set-marker (process-mark p) (point)))
2925 ;; Return process.
2926 p))))
2927
2928 ;; Save exit.
2929 (if (string-match tramp-temp-buffer-name (buffer-name))
2930 (ignore-errors
2931 (set-process-buffer (tramp-get-connection-process v) nil)
2932 (kill-buffer (current-buffer)))
2933 (set-buffer-modified-p bmp))
2934 (tramp-set-connection-property v "process-name" nil)
2935 (tramp-set-connection-property v "process-buffer" nil))))))
2936
2937 (defun tramp-sh-handle-process-file
2938 (program &optional infile destination display &rest args)
2939 "Like `process-file' for Tramp files."
2940 ;; The implementation is not complete yet.
2941 (when (and (numberp destination) (zerop destination))
2942 (error "Implementation does not handle immediate return"))
2943
2944 (with-parsed-tramp-file-name default-directory nil
2945 (let (command env input tmpinput stderr tmpstderr outbuf ret)
2946 ;; Compute command.
2947 (setq command (mapconcat 'tramp-shell-quote-argument
2948 (cons program args) " "))
2949 ;; We use as environment the difference to toplevel `process-environment'.
2950 (setq env
2951 (dolist (elt (nreverse (copy-sequence process-environment)) env)
2952 (or (member elt (default-toplevel-value 'process-environment))
2953 (setq env (cons elt env)))))
2954 (when env
2955 (setq command
2956 (format
2957 "env %s %s"
2958 (mapconcat 'tramp-shell-quote-argument env " ") command)))
2959 ;; Determine input.
2960 (if (null infile)
2961 (setq input "/dev/null")
2962 (setq infile (expand-file-name infile))
2963 (if (tramp-equal-remote default-directory infile)
2964 ;; INFILE is on the same remote host.
2965 (setq input (with-parsed-tramp-file-name infile nil localname))
2966 ;; INFILE must be copied to remote host.
2967 (setq input (tramp-make-tramp-temp-file v)
2968 tmpinput (tramp-make-tramp-file-name method user host input))
2969 (copy-file infile tmpinput t)))
2970 (when input (setq command (format "%s <%s" command input)))
2971
2972 ;; Determine output.
2973 (cond
2974 ;; Just a buffer.
2975 ((bufferp destination)
2976 (setq outbuf destination))
2977 ;; A buffer name.
2978 ((stringp destination)
2979 (setq outbuf (get-buffer-create destination)))
2980 ;; (REAL-DESTINATION ERROR-DESTINATION)
2981 ((consp destination)
2982 ;; output.
2983 (cond
2984 ((bufferp (car destination))
2985 (setq outbuf (car destination)))
2986 ((stringp (car destination))
2987 (setq outbuf (get-buffer-create (car destination))))
2988 ((car destination)
2989 (setq outbuf (current-buffer))))
2990 ;; stderr.
2991 (cond
2992 ((stringp (cadr destination))
2993 (setcar (cdr destination) (expand-file-name (cadr destination)))
2994 (if (tramp-equal-remote default-directory (cadr destination))
2995 ;; stderr is on the same remote host.
2996 (setq stderr (with-parsed-tramp-file-name
2997 (cadr destination) nil localname))
2998 ;; stderr must be copied to remote host. The temporary
2999 ;; file must be deleted after execution.
3000 (setq stderr (tramp-make-tramp-temp-file v)
3001 tmpstderr (tramp-make-tramp-file-name
3002 method user host stderr))))
3003 ;; stderr to be discarded.
3004 ((null (cadr destination))
3005 (setq stderr "/dev/null"))))
3006 ;; 't
3007 (destination
3008 (setq outbuf (current-buffer))))
3009 (when stderr (setq command (format "%s 2>%s" command stderr)))
3010
3011 ;; Send the command. It might not return in time, so we protect
3012 ;; it. Call it in a subshell, in order to preserve working
3013 ;; directory.
3014 (condition-case nil
3015 (unwind-protect
3016 (setq ret
3017 (if (tramp-send-command-and-check
3018 v (format "\\cd %s; %s"
3019 (tramp-shell-quote-argument localname)
3020 command)
3021 t t)
3022 0 1))
3023 ;; We should add the output anyway.
3024 (when outbuf
3025 (with-current-buffer outbuf
3026 (insert
3027 (with-current-buffer (tramp-get-connection-buffer v)
3028 (buffer-string))))
3029 (when (and display (get-buffer-window outbuf t)) (redisplay))))
3030 ;; When the user did interrupt, we should do it also. We use
3031 ;; return code -1 as marker.
3032 (quit
3033 (kill-buffer (tramp-get-connection-buffer v))
3034 (setq ret -1))
3035 ;; Handle errors.
3036 (error
3037 (kill-buffer (tramp-get-connection-buffer v))
3038 (setq ret 1)))
3039
3040 ;; Provide error file.
3041 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
3042
3043 ;; Cleanup. We remove all file cache values for the connection,
3044 ;; because the remote process could have changed them.
3045 (when tmpinput (delete-file tmpinput))
3046
3047 ;; `process-file-side-effects' has been introduced with GNU
3048 ;; Emacs 23.2. If set to `nil', no remote file will be changed
3049 ;; by `program'. If it doesn't exist, we assume its default
3050 ;; value `t'.
3051 (unless (and (boundp 'process-file-side-effects)
3052 (not (symbol-value 'process-file-side-effects)))
3053 (tramp-flush-directory-property v ""))
3054
3055 ;; Return exit status.
3056 (if (equal ret -1)
3057 (keyboard-quit)
3058 ret))))
3059
3060 (defun tramp-sh-handle-file-local-copy (filename)
3061 "Like `file-local-copy' for Tramp files."
3062 (with-parsed-tramp-file-name filename nil
3063 (unless (file-exists-p filename)
3064 (tramp-error
3065 v 'file-error
3066 "Cannot make local copy of non-existing file `%s'" filename))
3067
3068 (let* ((size (nth 7 (file-attributes (file-truename filename))))
3069 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
3070 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
3071 (tmpfile (tramp-compat-make-temp-file filename)))
3072
3073 (condition-case err
3074 (cond
3075 ;; `copy-file' handles direct copy and out-of-band methods.
3076 ((or (tramp-local-host-p v)
3077 (tramp-method-out-of-band-p v size))
3078 (copy-file filename tmpfile t t))
3079
3080 ;; Use inline encoding for file transfer.
3081 (rem-enc
3082 (save-excursion
3083 (with-tramp-progress-reporter
3084 v 3
3085 (format "Encoding remote file `%s' with `%s'" filename rem-enc)
3086 (tramp-barf-unless-okay
3087 v (format rem-enc (tramp-shell-quote-argument localname))
3088 "Encoding remote file failed"))
3089
3090 (with-tramp-progress-reporter
3091 v 3 (format "Decoding local file `%s' with `%s'"
3092 tmpfile loc-dec)
3093 (if (functionp loc-dec)
3094 ;; If local decoding is a function, we call it.
3095 ;; We must disable multibyte, because
3096 ;; `uudecode-decode-region' doesn't handle it
3097 ;; correctly.
3098 (with-temp-buffer
3099 (set-buffer-multibyte nil)
3100 (insert-buffer-substring (tramp-get-buffer v))
3101 (funcall loc-dec (point-min) (point-max))
3102 ;; Unset `file-name-handler-alist'. Otherwise,
3103 ;; epa-file gets confused.
3104 (let (file-name-handler-alist
3105 (coding-system-for-write 'binary))
3106 (write-region
3107 (point-min) (point-max) tmpfile nil 'no-message)))
3108
3109 ;; If tramp-decoding-function is not defined for this
3110 ;; method, we invoke tramp-decoding-command instead.
3111 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
3112 ;; Unset `file-name-handler-alist'. Otherwise,
3113 ;; epa-file gets confused.
3114 (let (file-name-handler-alist
3115 (coding-system-for-write 'binary))
3116 (with-current-buffer (tramp-get-buffer v)
3117 (write-region
3118 (point-min) (point-max) tmpfile2 nil 'no-message)))
3119 (unwind-protect
3120 (tramp-call-local-coding-command
3121 loc-dec tmpfile2 tmpfile)
3122 (delete-file tmpfile2)))))
3123
3124 ;; Set proper permissions.
3125 (set-file-modes tmpfile (tramp-default-file-modes filename))
3126 ;; Set local user ownership.
3127 (tramp-set-file-uid-gid tmpfile)))
3128
3129 ;; Oops, I don't know what to do.
3130 (t (tramp-error
3131 v 'file-error "Wrong method specification for `%s'" method)))
3132
3133 ;; Error handling.
3134 ((error quit)
3135 (delete-file tmpfile)
3136 (signal (car err) (cdr err))))
3137
3138 (run-hooks 'tramp-handle-file-local-copy-hook)
3139 tmpfile)))
3140
3141 ;; This is needed for XEmacs only. Code stolen from files.el.
3142 (defun tramp-sh-handle-insert-file-contents-literally
3143 (filename &optional visit beg end replace)
3144 "Like `insert-file-contents-literally' for Tramp files."
3145 (let ((format-alist nil)
3146 (after-insert-file-functions nil)
3147 (coding-system-for-read 'no-conversion)
3148 (coding-system-for-write 'no-conversion)
3149 (find-buffer-file-type-function
3150 (if (fboundp 'find-buffer-file-type)
3151 (symbol-function 'find-buffer-file-type)
3152 nil))
3153 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler))
3154 (inhibit-file-name-operation 'insert-file-contents))
3155 (unwind-protect
3156 (progn
3157 (fset 'find-buffer-file-type (lambda (_filename) t))
3158 (insert-file-contents filename visit beg end replace))
3159 ;; Save exit.
3160 (if find-buffer-file-type-function
3161 (fset 'find-buffer-file-type find-buffer-file-type-function)
3162 (fmakunbound 'find-buffer-file-type)))))
3163
3164 ;; CCC grok LOCKNAME
3165 (defun tramp-sh-handle-write-region
3166 (start end filename &optional append visit lockname confirm)
3167 "Like `write-region' for Tramp files."
3168 (setq filename (expand-file-name filename))
3169 (with-parsed-tramp-file-name filename nil
3170 ;; Following part commented out because we don't know what to do about
3171 ;; file locking, and it does not appear to be a problem to ignore it.
3172 ;; Ange-ftp ignores it, too.
3173 ;; (when (and lockname (stringp lockname))
3174 ;; (setq lockname (expand-file-name lockname)))
3175 ;; (unless (or (eq lockname nil)
3176 ;; (string= lockname filename))
3177 ;; (error
3178 ;; "tramp-sh-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3179
3180 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
3181 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
3182 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
3183 (tramp-error v 'file-error "File not overwritten")))
3184
3185 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
3186 (tramp-get-remote-uid v 'integer)))
3187 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
3188 (tramp-get-remote-gid v 'integer))))
3189
3190 (if (and (tramp-local-host-p v)
3191 ;; `file-writable-p' calls `file-expand-file-name'. We
3192 ;; cannot use `tramp-run-real-handler' therefore.
3193 (let (file-name-handler-alist)
3194 (and
3195 (file-writable-p (file-name-directory localname))
3196 (or (file-directory-p localname)
3197 (file-writable-p localname)))))
3198 ;; Short track: if we are on the local host, we can run directly.
3199 (tramp-run-real-handler
3200 'write-region
3201 (list start end localname append 'no-message lockname confirm))
3202
3203 (let* ((modes (save-excursion (tramp-default-file-modes filename)))
3204 ;; We use this to save the value of
3205 ;; `last-coding-system-used' after writing the tmp
3206 ;; file. At the end of the function, we set
3207 ;; `last-coding-system-used' to this saved value. This
3208 ;; way, any intermediary coding systems used while
3209 ;; talking to the remote shell or suchlike won't hose
3210 ;; this variable. This approach was snarfed from
3211 ;; ange-ftp.el.
3212 coding-system-used
3213 ;; Write region into a tmp file. This isn't really
3214 ;; needed if we use an encoding function, but currently
3215 ;; we use it always because this makes the logic
3216 ;; simpler. We must also set `temporary-file-directory',
3217 ;; because it could point to a remote directory.
3218 (temporary-file-directory
3219 (tramp-compat-temporary-file-directory))
3220 (tmpfile (or tramp-temp-buffer-file-name
3221 (tramp-compat-make-temp-file filename))))
3222
3223 ;; If `append' is non-nil, we copy the file locally, and let
3224 ;; the native `write-region' implementation do the job.
3225 (when append (copy-file filename tmpfile 'ok))
3226
3227 ;; We say `no-message' here because we don't want the
3228 ;; visited file modtime data to be clobbered from the temp
3229 ;; file. We call `set-visited-file-modtime' ourselves later
3230 ;; on. We must ensure that `file-coding-system-alist'
3231 ;; matches `tmpfile'.
3232 (let (file-name-handler-alist
3233 (file-coding-system-alist
3234 (tramp-find-file-name-coding-system-alist filename tmpfile)))
3235 (condition-case err
3236 (tramp-run-real-handler
3237 'write-region
3238 (list start end tmpfile append 'no-message lockname confirm))
3239 ((error quit)
3240 (setq tramp-temp-buffer-file-name nil)
3241 (delete-file tmpfile)
3242 (signal (car err) (cdr err))))
3243
3244 ;; Now, `last-coding-system-used' has the right value. Remember it.
3245 (when (boundp 'last-coding-system-used)
3246 (setq coding-system-used
3247 (symbol-value 'last-coding-system-used))))
3248
3249 ;; The permissions of the temporary file should be set. If
3250 ;; FILENAME does not exist (eq modes nil) it has been
3251 ;; renamed to the backup file. This case `save-buffer'
3252 ;; handles permissions.
3253 ;; Ensure that it is still readable.
3254 (when modes
3255 (set-file-modes
3256 tmpfile
3257 (logior (or modes 0) (tramp-compat-octal-to-decimal "0400"))))
3258
3259 ;; This is a bit lengthy due to the different methods
3260 ;; possible for file transfer. First, we check whether the
3261 ;; method uses an scp program. If so, we call it.
3262 ;; Otherwise, both encoding and decoding command must be
3263 ;; specified. However, if the method _also_ specifies an
3264 ;; encoding function, then that is used for encoding the
3265 ;; contents of the tmp file.
3266 (let* ((size (nth 7 (file-attributes tmpfile)))
3267 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
3268 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
3269 (cond
3270 ;; `copy-file' handles direct copy and out-of-band methods.
3271 ((or (tramp-local-host-p v)
3272 (tramp-method-out-of-band-p v size))
3273 (if (and (not (stringp start))
3274 (= (or end (point-max)) (point-max))
3275 (= (or start (point-min)) (point-min))
3276 (tramp-get-method-parameter
3277 method 'tramp-copy-keep-tmpfile))
3278 (progn
3279 (setq tramp-temp-buffer-file-name tmpfile)
3280 (condition-case err
3281 ;; We keep the local file for performance
3282 ;; reasons, useful for "rsync".
3283 (copy-file tmpfile filename t)
3284 ((error quit)
3285 (setq tramp-temp-buffer-file-name nil)
3286 (delete-file tmpfile)
3287 (signal (car err) (cdr err)))))
3288 (setq tramp-temp-buffer-file-name nil)
3289 ;; Don't rename, in order to keep context in SELinux.
3290 (unwind-protect
3291 (copy-file tmpfile filename t)
3292 (delete-file tmpfile))))
3293
3294 ;; Use inline file transfer.
3295 (rem-dec
3296 ;; Encode tmpfile.
3297 (unwind-protect
3298 (with-temp-buffer
3299 (set-buffer-multibyte nil)
3300 ;; Use encoding function or command.
3301 (with-tramp-progress-reporter
3302 v 3 (format "Encoding local file `%s' using `%s'"
3303 tmpfile loc-enc)
3304 (if (functionp loc-enc)
3305 ;; The following `let' is a workaround for
3306 ;; the base64.el that comes with pgnus-0.84.
3307 ;; If both of the following conditions are
3308 ;; satisfied, it tries to write to a local
3309 ;; file in default-directory, but at this
3310 ;; point, default-directory is remote.
3311 ;; (`call-process-region' can't write to
3312 ;; remote files, it seems.) The file in
3313 ;; question is a tmp file anyway.
3314 (let ((coding-system-for-read 'binary)
3315 (default-directory
3316 (tramp-compat-temporary-file-directory)))
3317 (insert-file-contents-literally tmpfile)
3318 (funcall loc-enc (point-min) (point-max)))
3319
3320 (unless (zerop (tramp-call-local-coding-command
3321 loc-enc tmpfile t))
3322 (tramp-error
3323 v 'file-error
3324 (concat "Cannot write to `%s', "
3325 "local encoding command `%s' failed")
3326 filename loc-enc))))
3327
3328 ;; Send buffer into remote decoding command which
3329 ;; writes to remote file. Because this happens on
3330 ;; the remote host, we cannot use the function.
3331 (with-tramp-progress-reporter
3332 v 3 (format "Decoding remote file `%s' using `%s'"
3333 filename rem-dec)
3334 (goto-char (point-max))
3335 (unless (bolp) (newline))
3336 (tramp-send-command
3337 v
3338 (format
3339 (concat rem-dec " <<'%s'\n%s%s")
3340 (tramp-shell-quote-argument localname)
3341 tramp-end-of-heredoc
3342 (buffer-string)
3343 tramp-end-of-heredoc))
3344 (tramp-barf-unless-okay
3345 v nil
3346 "Couldn't write region to `%s', decode using `%s' failed"
3347 filename rem-dec)
3348 ;; When `file-precious-flag' is set, the region is
3349 ;; written to a temporary file. Check that the
3350 ;; checksum is equal to that from the local tmpfile.
3351 (when file-precious-flag
3352 (erase-buffer)
3353 (and
3354 ;; cksum runs locally, if possible.
3355 (zerop (tramp-call-process v "cksum" tmpfile t))
3356 ;; cksum runs remotely.
3357 (tramp-send-command-and-check
3358 v
3359 (format
3360 "cksum <%s" (tramp-shell-quote-argument localname)))
3361 ;; ... they are different.
3362 (not
3363 (string-equal
3364 (buffer-string)
3365 (with-current-buffer (tramp-get-buffer v)
3366 (buffer-string))))
3367 (tramp-error
3368 v 'file-error
3369 (concat "Couldn't write region to `%s',"
3370 " decode using `%s' failed")
3371 filename rem-dec)))))
3372
3373 ;; Save exit.
3374 (delete-file tmpfile)))
3375
3376 ;; That's not expected.
3377 (t
3378 (tramp-error
3379 v 'file-error
3380 (concat "Method `%s' should specify both encoding and "
3381 "decoding command or an scp program")
3382 method))))
3383
3384 ;; Make `last-coding-system-used' have the right value.
3385 (when coding-system-used
3386 (set 'last-coding-system-used coding-system-used))))
3387
3388 (tramp-flush-file-property v (file-name-directory localname))
3389 (tramp-flush-file-property v localname)
3390
3391 ;; We must protect `last-coding-system-used', now we have set it
3392 ;; to its correct value.
3393 (let (last-coding-system-used (need-chown t))
3394 ;; Set file modification time.
3395 (when (or (eq visit t) (stringp visit))
3396 (let ((file-attr (tramp-compat-file-attributes filename 'integer)))
3397 (set-visited-file-modtime
3398 ;; We must pass modtime explicitly, because FILENAME can
3399 ;; be different from (buffer-file-name), f.e. if
3400 ;; `file-precious-flag' is set.
3401 (nth 5 file-attr))
3402 (when (and (= (nth 2 file-attr) uid)
3403 (= (nth 3 file-attr) gid))
3404 (setq need-chown nil))))
3405
3406 ;; Set the ownership.
3407 (when need-chown
3408 (tramp-set-file-uid-gid filename uid gid))
3409 (when (or (eq visit t) (null visit) (stringp visit))
3410 (tramp-message v 0 "Wrote %s" filename))
3411 (run-hooks 'tramp-handle-write-region-hook)))))
3412
3413 (defvar tramp-vc-registered-file-names nil
3414 "List used to collect file names, which are checked during `vc-registered'.")
3415
3416 ;; VC backends check for the existence of various different special
3417 ;; files. This is very time consuming, because every single check
3418 ;; requires a remote command (the file cache must be invalidated).
3419 ;; Therefore, we apply a kind of optimization. We install the file
3420 ;; name handler `tramp-vc-file-name-handler', which does nothing but
3421 ;; remembers all file names for which `file-exists-p' or
3422 ;; `file-readable-p' has been applied. A first run of `vc-registered'
3423 ;; is performed. Afterwards, a script is applied for all collected
3424 ;; file names, using just one remote command. The result of this
3425 ;; script is used to fill the file cache with actual values. Now we
3426 ;; can reset the file name handlers, and we make a second run of
3427 ;; `vc-registered', which returns the expected result without sending
3428 ;; any other remote command.
3429 (defun tramp-sh-handle-vc-registered (file)
3430 "Like `vc-registered' for Tramp files."
3431 (tramp-compat-with-temp-message ""
3432 (with-parsed-tramp-file-name file nil
3433 (with-tramp-progress-reporter
3434 v 3 (format "Checking `vc-registered' for %s" file)
3435
3436 ;; There could be new files, created by the vc backend. We
3437 ;; cannot reuse the old cache entries, therefore. In
3438 ;; `tramp-get-file-property', `remote-file-name-inhibit-cache'
3439 ;; could also be a timestamp as `current-time' returns. This
3440 ;; means invalidate all cache entries with an older timestamp.
3441 (let (tramp-vc-registered-file-names
3442 (remote-file-name-inhibit-cache (current-time))
3443 (file-name-handler-alist
3444 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
3445
3446 ;; Here we collect only file names, which need an operation.
3447 (ignore-errors (tramp-run-real-handler 'vc-registered (list file)))
3448 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
3449
3450 ;; Send just one command, in order to fill the cache.
3451 (when tramp-vc-registered-file-names
3452 (tramp-maybe-send-script
3453 v
3454 (format tramp-vc-registered-read-file-names
3455 (tramp-get-file-exists-command v)
3456 (format "%s -r" (tramp-get-test-command v)))
3457 "tramp_vc_registered_read_file_names")
3458
3459 (dolist
3460 (elt
3461 (ignore-errors
3462 ;; We cannot use `tramp-send-command-and-read',
3463 ;; because this does not cooperate well with
3464 ;; heredoc documents.
3465 (tramp-send-command
3466 v
3467 (format
3468 "tramp_vc_registered_read_file_names <<'%s'\n%s\n%s\n"
3469 tramp-end-of-heredoc
3470 (mapconcat 'tramp-shell-quote-argument
3471 tramp-vc-registered-file-names
3472 "\n")
3473 tramp-end-of-heredoc))
3474 (with-current-buffer (tramp-get-connection-buffer v)
3475 ;; Read the expression.
3476 (goto-char (point-min))
3477 (read (current-buffer)))))
3478
3479 (tramp-set-file-property
3480 v (car elt) (cadr elt) (cadr (cdr elt))))))
3481
3482 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3483 ;; calls shall be answered from the file cache. We unset
3484 ;; `process-file-side-effects' and `remote-file-name-inhibit-cache'
3485 ;; in order to keep the cache.
3486 (let ((vc-handled-backends vc-handled-backends)
3487 remote-file-name-inhibit-cache process-file-side-effects)
3488 ;; Reduce `vc-handled-backends' in order to minimize process calls.
3489 (when (and (memq 'Bzr vc-handled-backends)
3490 (boundp 'vc-bzr-program)
3491 (not (with-tramp-connection-property v vc-bzr-program
3492 (tramp-find-executable
3493 v vc-bzr-program (tramp-get-remote-path v)))))
3494 (setq vc-handled-backends (remq 'Bzr vc-handled-backends)))
3495 (when (and (memq 'Git vc-handled-backends)
3496 (boundp 'vc-git-program)
3497 (not (with-tramp-connection-property v vc-git-program
3498 (tramp-find-executable
3499 v vc-git-program (tramp-get-remote-path v)))))
3500 (setq vc-handled-backends (remq 'Git vc-handled-backends)))
3501 (when (and (memq 'Hg vc-handled-backends)
3502 (boundp 'vc-hg-program)
3503 (not (with-tramp-connection-property v vc-hg-program
3504 (tramp-find-executable
3505 v vc-hg-program (tramp-get-remote-path v)))))
3506 (setq vc-handled-backends (remq 'Hg vc-handled-backends)))
3507 ;; Run.
3508 (ignore-errors
3509 (tramp-run-real-handler 'vc-registered (list file))))))))
3510
3511 ;;;###tramp-autoload
3512 (defun tramp-sh-file-name-handler (operation &rest args)
3513 "Invoke remote-shell Tramp file name handler.
3514 Fall back to normal file name handler if no Tramp handler exists."
3515 (when (and tramp-locked (not tramp-locker))
3516 (setq tramp-locked nil)
3517 (tramp-error
3518 (car-safe tramp-current-connection) 'file-error
3519 "Forbidden reentrant call of Tramp"))
3520 (let ((tl tramp-locked))
3521 (setq tramp-locked t)
3522 (unwind-protect
3523 (let ((tramp-locker t))
3524 (save-match-data
3525 (let ((fn (assoc operation tramp-sh-file-name-handler-alist)))
3526 (if fn
3527 (apply (cdr fn) args)
3528 (tramp-run-real-handler operation args)))))
3529 (setq tramp-locked tl))))
3530
3531 (defun tramp-vc-file-name-handler (operation &rest args)
3532 "Invoke special file name handler, which collects files to be handled."
3533 (save-match-data
3534 (let ((filename
3535 (tramp-replace-environment-variables
3536 (apply 'tramp-file-name-for-operation operation args)))
3537 (fn (assoc operation tramp-sh-file-name-handler-alist)))
3538 (with-parsed-tramp-file-name filename nil
3539 (cond
3540 ;; That's what we want: file names, for which checks are
3541 ;; applied. We assume that VC uses only `file-exists-p' and
3542 ;; `file-readable-p' checks; otherwise we must extend the
3543 ;; list. We do not perform any action, but return nil, in
3544 ;; order to keep `vc-registered' running.
3545 ((and fn (memq operation '(file-exists-p file-readable-p)))
3546 (add-to-list 'tramp-vc-registered-file-names localname 'append)
3547 nil)
3548 ;; `process-file' and `start-file-process' shall be ignored.
3549 ((and fn (eq operation 'process-file) 0))
3550 ((and fn (eq operation 'start-file-process) nil))
3551 ;; Tramp file name handlers like `expand-file-name'. They
3552 ;; must still work.
3553 (fn (save-match-data (apply (cdr fn) args)))
3554 ;; Default file name handlers, we don't care.
3555 (t (tramp-run-real-handler operation args)))))))
3556
3557 (defun tramp-sh-handle-file-notify-add-watch (file-name flags _callback)
3558 "Like `file-notify-add-watch' for Tramp files."
3559 (setq file-name (expand-file-name file-name))
3560 (with-parsed-tramp-file-name file-name nil
3561 (let* ((default-directory (file-name-directory file-name))
3562 command events filter p sequence)
3563 (cond
3564 ;; gvfs-monitor-dir.
3565 ((setq command (tramp-get-remote-gvfs-monitor-dir v))
3566 (setq filter 'tramp-sh-file-gvfs-monitor-dir-process-filter
3567 sequence `(,command ,localname)))
3568 ;; inotifywait.
3569 ((setq command (tramp-get-remote-inotifywait v))
3570 (setq filter 'tramp-sh-file-inotifywait-process-filter
3571 events
3572 (cond
3573 ((and (memq 'change flags) (memq 'attribute-change flags))
3574 "create,modify,move,delete,attrib")
3575 ((memq 'change flags) "create,modify,move,delete")
3576 ((memq 'attribute-change flags) "attrib"))
3577 sequence `(,command "-mq" "-e" ,events ,localname)))
3578 ;; None.
3579 (t (tramp-error
3580 v 'file-notify-error
3581 "No file notification program found on %s"
3582 (file-remote-p file-name))))
3583 ;; Start process.
3584 (setq p (apply
3585 'start-file-process
3586 (file-name-nondirectory command)
3587 (generate-new-buffer
3588 (format " *%s*" (file-name-nondirectory command)))
3589 sequence))
3590 ;; Return the process object as watch-descriptor.
3591 (if (not (processp p))
3592 (tramp-error
3593 v 'file-notify-error
3594 "`%s' failed to start on remote host"
3595 (mapconcat 'identity sequence " "))
3596 (tramp-message v 6 "Run `%s', %S" (mapconcat 'identity sequence " ") p)
3597 (tramp-set-connection-property p "vector" v)
3598 (tramp-compat-set-process-query-on-exit-flag p nil)
3599 (set-process-filter p filter)
3600 p))))
3601
3602 (defun tramp-sh-file-gvfs-monitor-dir-process-filter (proc string)
3603 "Read output from \"gvfs-monitor-dir\" and add corresponding file-notify events."
3604 (let ((remote-prefix
3605 (with-current-buffer (process-buffer proc)
3606 (file-remote-p default-directory)))
3607 (rest-string (tramp-compat-process-get proc 'rest-string)))
3608 (when rest-string
3609 (tramp-message proc 10 "Previous string:\n%s" rest-string))
3610 (tramp-message proc 6 "%S\n%s" proc string)
3611 (setq string (concat rest-string string)
3612 ;; Attribute change is returned in unused wording.
3613 string (tramp-compat-replace-regexp-in-string
3614 "ATTRIB CHANGED" "ATTRIBUTE_CHANGED" string))
3615
3616 (while (string-match
3617 (concat "^[\n\r]*"
3618 "Directory Monitor Event:[\n\r]+"
3619 "Child = \\([^\n\r]+\\)[\n\r]+"
3620 "\\(Other = \\([^\n\r]+\\)[\n\r]+\\)?"
3621 "Event = \\([^[:blank:]]+\\)[\n\r]+")
3622 string)
3623 (let ((object
3624 (list
3625 proc
3626 (intern-soft
3627 (tramp-compat-replace-regexp-in-string
3628 "_" "-" (downcase (match-string 4 string))))
3629 ;; File names are returned as absolute paths. We must
3630 ;; add the remote prefix.
3631 (concat remote-prefix (match-string 1 string))
3632 (when (match-string 3 string)
3633 (concat remote-prefix (match-string 3 string))))))
3634 (setq string (replace-match "" nil nil string))
3635 ;; Usually, we would add an Emacs event now. Unfortunately,
3636 ;; `unread-command-events' does not accept several events at
3637 ;; once. Therefore, we apply the callback directly.
3638 (tramp-compat-funcall 'file-notify-callback object)))
3639
3640 ;; Save rest of the string.
3641 (when (zerop (length string)) (setq string nil))
3642 (when string (tramp-message proc 10 "Rest string:\n%s" string))
3643 (tramp-compat-process-put proc 'rest-string string)))
3644
3645 (defun tramp-sh-file-inotifywait-process-filter (proc string)
3646 "Read output from \"inotifywait\" and add corresponding file-notify events."
3647 (tramp-message proc 6 "%S\n%s" proc string)
3648 (dolist (line (split-string string "[\n\r]+" 'omit-nulls))
3649 ;; Check, whether there is a problem.
3650 (unless
3651 (string-match
3652 (concat "^[^[:blank:]]+"
3653 "[[:blank:]]+\\([^[:blank:]]+\\)+"
3654 "\\([[:blank:]]+\\([^\n\r]+\\)\\)?")
3655 line)
3656 (tramp-error proc 'file-notify-error "%s" line))
3657
3658 (let ((object
3659 (list
3660 proc
3661 (mapcar
3662 (lambda (x)
3663 (intern-soft
3664 (tramp-compat-replace-regexp-in-string "_" "-" (downcase x))))
3665 (split-string (match-string 1 line) "," 'omit-nulls))
3666 (match-string 3 line))))
3667 ;; Usually, we would add an Emacs event now. Unfortunately,
3668 ;; `unread-command-events' does not accept several events at
3669 ;; once. Therefore, we apply the callback directly.
3670 (tramp-compat-funcall 'file-notify-callback object))))
3671
3672 ;;; Internal Functions:
3673
3674 (defun tramp-maybe-send-script (vec script name)
3675 "Define in remote shell function NAME implemented as SCRIPT.
3676 Only send the definition if it has not already been done."
3677 ;; We cannot let-bind (tramp-get-connection-process vec) because it
3678 ;; might be nil.
3679 (let ((scripts (tramp-get-connection-property
3680 (tramp-get-connection-process vec) "scripts" nil)))
3681 (unless (member name scripts)
3682 (with-tramp-progress-reporter vec 5 (format "Sending script `%s'" name)
3683 ;; The script could contain a call of Perl. This is masked with `%s'.
3684 (when (and (string-match "%s" script)
3685 (not (tramp-get-remote-perl vec)))
3686 (tramp-error vec 'file-error "No Perl available on remote host"))
3687 (tramp-barf-unless-okay
3688 vec
3689 (format "%s () {\n%s\n}" name
3690 (format script (tramp-get-remote-perl vec)))
3691 "Script %s sending failed" name)
3692 (tramp-set-connection-property
3693 (tramp-get-connection-process vec) "scripts" (cons name scripts))))))
3694
3695 (defun tramp-set-auto-save ()
3696 (when (and ;; ange-ftp has its own auto-save mechanism
3697 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
3698 'tramp-sh-file-name-handler)
3699 auto-save-default)
3700 (auto-save-mode 1)))
3701 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
3702 (add-hook 'tramp-unload-hook
3703 (lambda ()
3704 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
3705
3706 (defun tramp-run-test (switch filename)
3707 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3708 Returns the exit code of the `test' program."
3709 (with-parsed-tramp-file-name filename nil
3710 (tramp-send-command-and-check
3711 v
3712 (format
3713 "%s %s %s"
3714 (tramp-get-test-command v)
3715 switch
3716 (tramp-shell-quote-argument localname)))))
3717
3718 (defun tramp-run-test2 (format-string file1 file2)
3719 "Run `test'-like program on the remote system, given FILE1, FILE2.
3720 FORMAT-STRING contains the program name, switches, and place holders.
3721 Returns the exit code of the `test' program. Barfs if the methods,
3722 hosts, or files, disagree."
3723 (unless (tramp-equal-remote file1 file2)
3724 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
3725 (tramp-error
3726 v 'file-error
3727 "tramp-run-test2 only implemented for same method, user, host")))
3728 (with-parsed-tramp-file-name file1 v1
3729 (with-parsed-tramp-file-name file1 v2
3730 (tramp-send-command-and-check
3731 v1
3732 (format format-string
3733 (tramp-shell-quote-argument v1-localname)
3734 (tramp-shell-quote-argument v2-localname))))))
3735
3736 (defun tramp-find-executable
3737 (vec progname dirlist &optional ignore-tilde ignore-path)
3738 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3739 First arg VEC specifies the connection, PROGNAME is the program
3740 to search for, and DIRLIST gives the list of directories to
3741 search. If IGNORE-TILDE is non-nil, directory names starting
3742 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3743 only in DIRLIST.
3744
3745 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3746
3747 This function expects to be in the right *tramp* buffer."
3748 (with-current-buffer (tramp-get-connection-buffer vec)
3749 (let (result)
3750 ;; Check whether the executable is in $PATH. "which(1)" does not
3751 ;; report always a correct error code; therefore we check the
3752 ;; number of words it returns. "SunOS 5.10" (and maybe "SunOS
3753 ;; 5.11") have problems with this command, we disable the call
3754 ;; therefore.
3755 (unless (or ignore-path
3756 (string-match
3757 (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3758 (tramp-get-connection-property vec "uname" "")))
3759 (tramp-send-command vec (format "which \\%s | wc -w" progname))
3760 (goto-char (point-min))
3761 (if (looking-at "^\\s-*1$")
3762 (setq result (concat "\\" progname))))
3763 (unless result
3764 (when ignore-tilde
3765 ;; Remove all ~/foo directories from dirlist. In XEmacs,
3766 ;; `remove' is in CL, and we want to avoid CL dependencies.
3767 (let (newdl d)
3768 (while dirlist
3769 (setq d (car dirlist))
3770 (setq dirlist (cdr dirlist))
3771 (unless (char-equal ?~ (aref d 0))
3772 (setq newdl (cons d newdl))))
3773 (setq dirlist (nreverse newdl))))
3774 (tramp-send-command
3775 vec
3776 (format (concat "while read d; "
3777 "do if test -x $d/%s -a -f $d/%s; "
3778 "then echo tramp_executable $d/%s; "
3779 "break; fi; done <<'%s'\n"
3780 "%s\n%s")
3781 progname progname progname
3782 tramp-end-of-heredoc
3783 (mapconcat 'identity dirlist "\n")
3784 tramp-end-of-heredoc))
3785 (goto-char (point-max))
3786 (when (search-backward "tramp_executable " nil t)
3787 (skip-chars-forward "^ ")
3788 (skip-chars-forward " ")
3789 (setq result (buffer-substring (point) (point-at-eol)))))
3790 result)))
3791
3792 (defun tramp-set-remote-path (vec)
3793 "Sets the remote environment PATH to existing directories.
3794 I.e., for each directory in `tramp-remote-path', it is tested
3795 whether it exists and if so, it is added to the environment
3796 variable PATH."
3797 (tramp-message vec 5 "Setting $PATH environment variable")
3798 (tramp-send-command
3799 vec (format "PATH=%s; export PATH"
3800 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
3801
3802 ;; ------------------------------------------------------------
3803 ;; -- Communication with external shell --
3804 ;; ------------------------------------------------------------
3805
3806 (defun tramp-find-file-exists-command (vec)
3807 "Find a command on the remote host for checking if a file exists.
3808 Here, we are looking for a command which has zero exit status if the
3809 file exists and nonzero exit status otherwise."
3810 (let ((existing "/")
3811 (nonexistent
3812 (tramp-shell-quote-argument "/ this file does not exist "))
3813 result)
3814 ;; The algorithm is as follows: we try a list of several commands.
3815 ;; For each command, we first run `$cmd /' -- this should return
3816 ;; true, as the root directory always exists. And then we run
3817 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
3818 ;; does not exist. This should return false. We use the first
3819 ;; command we find that seems to work.
3820 ;; The list of commands to try is as follows:
3821 ;; `ls -d' This works on most systems, but NetBSD 1.4
3822 ;; has a bug: `ls' always returns zero exit
3823 ;; status, even for files which don't exist.
3824 ;; `test -e' Some Bourne shells have a `test' builtin
3825 ;; which does not know the `-e' option.
3826 ;; `/bin/test -e' For those, the `test' binary on disk normally
3827 ;; provides the option. Alas, the binary
3828 ;; is sometimes `/bin/test' and sometimes it's
3829 ;; `/usr/bin/test'.
3830 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3831 (unless (or
3832 (ignore-errors
3833 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
3834 (tramp-send-command-and-check
3835 vec (format "%s %s" result existing))
3836 (not (tramp-send-command-and-check
3837 vec (format "%s %s" result nonexistent)))))
3838 (ignore-errors
3839 (and (setq result "/bin/test -e")
3840 (tramp-send-command-and-check
3841 vec (format "%s %s" result existing))
3842 (not (tramp-send-command-and-check
3843 vec (format "%s %s" result nonexistent)))))
3844 (ignore-errors
3845 (and (setq result "/usr/bin/test -e")
3846 (tramp-send-command-and-check
3847 vec (format "%s %s" result existing))
3848 (not (tramp-send-command-and-check
3849 vec (format "%s %s" result nonexistent)))))
3850 (ignore-errors
3851 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
3852 (tramp-send-command-and-check
3853 vec (format "%s %s" result existing))
3854 (not (tramp-send-command-and-check
3855 vec (format "%s %s" result nonexistent))))))
3856 (tramp-error
3857 vec 'file-error "Couldn't find command to check if file exists"))
3858 result))
3859
3860 (defun tramp-open-shell (vec shell)
3861 "Opens shell SHELL."
3862 (with-tramp-progress-reporter
3863 vec 5 (format "Opening remote shell `%s'" shell)
3864 ;; Find arguments for this shell.
3865 (let ((alist tramp-sh-extra-args)
3866 item extra-args)
3867 (while (and alist (null extra-args))
3868 (setq item (pop alist))
3869 (when (string-match (car item) shell)
3870 (setq extra-args (cdr item))))
3871 (tramp-send-command
3872 vec (format
3873 "exec env ENV='' HISTFILE=/dev/null PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s"
3874 (tramp-shell-quote-argument tramp-end-of-output)
3875 shell (or extra-args ""))
3876 t))
3877 (tramp-set-connection-property
3878 (tramp-get-connection-process vec) "remote-shell" shell)))
3879
3880 (defun tramp-find-shell (vec)
3881 "Opens a shell on the remote host which groks tilde expansion."
3882 (with-current-buffer (tramp-get-buffer vec)
3883 (let ((default-shell
3884 (or
3885 (tramp-get-connection-property
3886 (tramp-get-connection-process vec) "remote-shell" nil)
3887 (tramp-get-method-parameter
3888 (tramp-file-name-method vec) 'tramp-remote-shell)))
3889 shell)
3890 (setq shell
3891 (with-tramp-connection-property vec "remote-shell"
3892 ;; CCC: "root" does not exist always, see QNAP 459.
3893 ;; Which check could we apply instead?
3894 (tramp-send-command vec "echo ~root" t)
3895 (if (or (string-match "^~root$" (buffer-string))
3896 ;; The default shell (ksh93) of OpenSolaris and
3897 ;; Solaris is buggy. We've got reports for
3898 ;; "SunOS 5.10" and "SunOS 5.11" so far.
3899 (string-match (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3900 (tramp-get-connection-property
3901 vec "uname" "")))
3902
3903 (or (tramp-find-executable
3904 vec "bash" (tramp-get-remote-path vec) t t)
3905 (tramp-find-executable
3906 vec "ksh" (tramp-get-remote-path vec) t t)
3907 ;; Maybe it works at least for some other commands.
3908 (prog1
3909 default-shell
3910 (tramp-message
3911 vec 2
3912 (concat
3913 "Couldn't find a remote shell which groks tilde "
3914 "expansion, using `%s'")
3915 default-shell)))
3916
3917 default-shell)))
3918
3919 ;; Open a new shell if needed.
3920 (unless (string-equal shell default-shell)
3921 (tramp-message
3922 vec 5 "Starting remote shell `%s' for tilde expansion" shell)
3923 (tramp-open-shell vec shell)))))
3924
3925 ;; Utility functions.
3926
3927 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
3928 "Wait for shell prompt and barf if none appears.
3929 Looks at process PROC to see if a shell prompt appears in TIMEOUT
3930 seconds. If not, it produces an error message with the given ERROR-ARGS."
3931 (let ((vec (tramp-get-connection-property proc "vector" nil)))
3932 (condition-case nil
3933 (tramp-wait-for-regexp
3934 proc timeout
3935 (format
3936 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
3937 (error
3938 (delete-process proc)
3939 (apply 'tramp-error-with-buffer
3940 (tramp-get-connection-buffer vec) vec 'file-error error-args)))))
3941
3942 (defun tramp-open-connection-setup-interactive-shell (proc vec)
3943 "Set up an interactive shell.
3944 Mainly sets the prompt and the echo correctly. PROC is the shell
3945 process to set up. VEC specifies the connection."
3946 (let ((tramp-end-of-output tramp-initial-end-of-output))
3947 ;; It is useful to set the prompt in the following command because
3948 ;; some people have a setting for $PS1 which /bin/sh doesn't know
3949 ;; about and thus /bin/sh will display a strange prompt. For
3950 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
3951 ;; display the current working directory but /bin/sh will display
3952 ;; a dollar sign. The following command line sets $PS1 to a sane
3953 ;; value, and works under Bourne-ish shells as well as csh-like
3954 ;; shells. Daniel Pittman reports that the unusual positioning of
3955 ;; the single quotes makes it work under `rc', too. We also unset
3956 ;; the variable $ENV because that is read by some sh
3957 ;; implementations (eg, bash when called as sh) on startup; this
3958 ;; way, we avoid the startup file clobbering $PS1. $PROMPT_COMMAND
3959 ;; is another way to set the prompt in /bin/bash, it must be
3960 ;; discarded as well.
3961 (tramp-open-shell
3962 vec
3963 (or (tramp-get-connection-property vec "remote-shell" nil)
3964 (tramp-get-method-parameter
3965 (tramp-file-name-method vec) 'tramp-remote-shell)))
3966
3967 ;; Disable echo.
3968 (tramp-message vec 5 "Setting up remote shell environment")
3969 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
3970 ;; Check whether the echo has really been disabled. Some
3971 ;; implementations, like busybox of embedded GNU/Linux, don't
3972 ;; support disabling.
3973 (tramp-send-command vec "echo foo" t)
3974 (with-current-buffer (process-buffer proc)
3975 (goto-char (point-min))
3976 (when (looking-at "echo foo")
3977 (tramp-set-connection-property proc "remote-echo" t)
3978 (tramp-message vec 5 "Remote echo still on. Ok.")
3979 ;; Make sure backspaces and their echo are enabled and no line
3980 ;; width magic interferes with them.
3981 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
3982
3983 (tramp-message vec 5 "Setting shell prompt")
3984 (tramp-send-command
3985 vec (format "PS1=%s PS2='' PS3='' PROMPT_COMMAND=''"
3986 (tramp-shell-quote-argument tramp-end-of-output)) t)
3987
3988 ;; Try to set up the coding system correctly.
3989 ;; CCC this can't be the right way to do it. Hm.
3990 (tramp-message vec 5 "Determining coding system")
3991 (with-current-buffer (process-buffer proc)
3992 (if (featurep 'mule)
3993 ;; Use MULE to select the right EOL convention for communicating
3994 ;; with the process.
3995 (let ((cs (or (and (memq 'utf-8 (coding-system-list))
3996 (string-match "utf8" (tramp-get-remote-locale vec))
3997 (cons 'utf-8 'utf-8))
3998 (tramp-compat-funcall 'process-coding-system proc)
3999 (cons 'undecided 'undecided)))
4000 cs-decode cs-encode)
4001 (when (symbolp cs) (setq cs (cons cs cs)))
4002 (setq cs-decode (car cs))
4003 (setq cs-encode (cdr cs))
4004 (unless cs-decode (setq cs-decode 'undecided))
4005 (unless cs-encode (setq cs-encode 'undecided))
4006 (setq cs-encode (tramp-compat-coding-system-change-eol-conversion
4007 cs-encode 'unix))
4008 (tramp-send-command vec "echo foo ; echo bar" t)
4009 (goto-char (point-min))
4010 (when (search-forward "\r" nil t)
4011 (setq cs-decode (tramp-compat-coding-system-change-eol-conversion
4012 cs-decode 'dos)))
4013 (tramp-compat-funcall
4014 'set-buffer-process-coding-system cs-decode cs-encode)
4015 (tramp-message
4016 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
4017 ;; Look for ^M and do something useful if found.
4018 (when (search-forward "\r" nil t)
4019 ;; We have found a ^M but cannot frob the process coding system
4020 ;; because we're running on a non-MULE Emacs. Let's try
4021 ;; stty, instead.
4022 (tramp-send-command vec "stty -onlcr" t))))
4023
4024 (tramp-send-command vec "set +o vi +o emacs" t)
4025
4026 ;; Check whether the output of "uname -sr" has been changed. If
4027 ;; yes, this is a strong indication that we must expire all
4028 ;; connection properties. We start again with
4029 ;; `tramp-maybe-open-connection', it will be caught there.
4030 (tramp-message vec 5 "Checking system information")
4031 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
4032 (new-uname
4033 (tramp-set-connection-property
4034 vec "uname"
4035 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
4036 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
4037 (tramp-message
4038 vec 3
4039 "Connection reset, because remote host changed from `%s' to `%s'"
4040 old-uname new-uname)
4041 ;; We want to keep the password.
4042 (tramp-cleanup-connection vec t t)
4043 (throw 'uname-changed (tramp-maybe-open-connection vec))))
4044
4045 ;; Check whether the remote host suffers from buggy
4046 ;; `send-process-string'. This is known for FreeBSD (see comment in
4047 ;; `send_process', file process.c). I've tested sending 624 bytes
4048 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
4049 ;; this host type is detected locally. It cannot handle remote
4050 ;; hosts, though.
4051 (with-tramp-connection-property proc "chunksize"
4052 (cond
4053 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
4054 tramp-chunksize)
4055 (t
4056 (tramp-message
4057 vec 5 "Checking remote host type for `send-process-string' bug")
4058 (if (string-match
4059 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
4060 500 0))))
4061
4062 ;; Set remote PATH variable.
4063 (tramp-set-remote-path vec)
4064
4065 ;; Search for a good shell before searching for a command which
4066 ;; checks if a file exists. This is done because Tramp wants to use
4067 ;; "test foo; echo $?" to check if various conditions hold, and
4068 ;; there are buggy /bin/sh implementations which don't execute the
4069 ;; "echo $?" part if the "test" part has an error. In particular,
4070 ;; the OpenSolaris /bin/sh is a problem. There are also other
4071 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
4072 ;; in function declarations, or changing HISTFILE in place.
4073 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
4074 ;; detected.
4075 (tramp-find-shell vec)
4076
4077 ;; Disable unexpected output.
4078 (tramp-send-command vec "mesg n; biff n" t)
4079
4080 ;; IRIX64 bash expands "!" even when in single quotes. This
4081 ;; destroys our shell functions, we must disable it. See
4082 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
4083 (when (string-match "^IRIX64" (tramp-get-connection-property vec "uname" ""))
4084 (tramp-send-command vec "set +H" t))
4085
4086 ;; On BSD-like systems, ?\t is expanded to spaces. Suppress this.
4087 (when (string-match "BSD\\|Darwin"
4088 (tramp-get-connection-property vec "uname" ""))
4089 (tramp-send-command vec "stty -oxtabs" t))
4090
4091 ;; Set `remote-tty' process property.
4092 (let ((tty (tramp-send-command-and-read vec "echo \\\"`tty`\\\"" 'noerror)))
4093 (unless (zerop (length tty))
4094 (tramp-compat-process-put proc 'remote-tty tty)))
4095
4096 ;; Dump stty settings in the traces.
4097 (when (>= tramp-verbose 9)
4098 (tramp-send-command vec "stty -a" t))
4099
4100 ;; Set the environment.
4101 (tramp-message vec 5 "Setting default environment")
4102
4103 (let ((env (append `(,(tramp-get-remote-locale vec))
4104 (copy-sequence tramp-remote-process-environment)))
4105 unset vars item)
4106 (while env
4107 (setq item (tramp-compat-split-string (car env) "="))
4108 (setcdr item (mapconcat 'identity (cdr item) "="))
4109 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
4110 (push (format "%s %s" (car item) (cdr item)) vars)
4111 (push (car item) unset))
4112 (setq env (cdr env)))
4113 (when vars
4114 (tramp-send-command
4115 vec
4116 (format "while read var val; do export $var=$val; done <<'%s'\n%s\n%s"
4117 tramp-end-of-heredoc
4118 (mapconcat 'identity vars "\n")
4119 tramp-end-of-heredoc)
4120 t))
4121 (when unset
4122 (tramp-send-command
4123 vec (format "unset %s" (mapconcat 'identity unset " ")) t))))
4124
4125 ;; Old text from documentation of tramp-methods:
4126 ;; Using a uuencode/uudecode inline method is discouraged, please use one
4127 ;; of the base64 methods instead since base64 encoding is much more
4128 ;; reliable and the commands are more standardized between the different
4129 ;; Unix versions. But if you can't use base64 for some reason, please
4130 ;; note that the default uudecode command does not work well for some
4131 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
4132 ;; the following command for uudecode:
4133 ;;
4134 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
4135 ;;
4136 ;; For Irix, no solution is known yet.
4137
4138 (autoload 'uudecode-decode-region "uudecode")
4139
4140 (defconst tramp-local-coding-commands
4141 `((b64 base64-encode-region base64-decode-region)
4142 (uu tramp-uuencode-region uudecode-decode-region)
4143 (pack ,(format tramp-perl-pack "perl") ,(format tramp-perl-unpack "perl")))
4144 "List of local coding commands for inline transfer.
4145 Each item is a list that looks like this:
4146
4147 \(FORMAT ENCODING DECODING\)
4148
4149 FORMAT is symbol describing the encoding/decoding format. It can be
4150 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
4151
4152 ENCODING and DECODING can be strings, giving commands, or symbols,
4153 giving functions. If they are strings, then they can contain
4154 the \"%s\" format specifier. If that specifier is present, the input
4155 file name will be put into the command line at that spot. If the
4156 specifier is not present, the input should be read from standard
4157 input.
4158
4159 If they are functions, they will be called with two arguments, start
4160 and end of region, and are expected to replace the region contents
4161 with the encoded or decoded results, respectively.")
4162
4163 (defconst tramp-remote-coding-commands
4164 '((b64 "base64" "base64 -d -i")
4165 ;; "-i" is more robust with older base64 from GNU coreutils.
4166 ;; However, I don't know whether all base64 versions do supports
4167 ;; this option.
4168 (b64 "base64" "base64 -d")
4169 (b64 "mimencode -b" "mimencode -u -b")
4170 (b64 "mmencode -b" "mmencode -u -b")
4171 (b64 "recode data..base64" "recode base64..data")
4172 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
4173 (b64 tramp-perl-encode tramp-perl-decode)
4174 (uu "uuencode xxx" "uudecode -o /dev/stdout" "test -c /dev/stdout")
4175 (uu "uuencode xxx" "uudecode -o -")
4176 (uu "uuencode xxx" "uudecode -p")
4177 (uu "uuencode xxx" tramp-uudecode)
4178 (pack tramp-perl-pack tramp-perl-unpack))
4179 "List of remote coding commands for inline transfer.
4180 Each item is a list that looks like this:
4181
4182 \(FORMAT ENCODING DECODING [TEST]\)
4183
4184 FORMAT is a symbol describing the encoding/decoding format. It can be
4185 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
4186
4187 ENCODING and DECODING can be strings, giving commands, or symbols,
4188 giving variables. If they are strings, then they can contain
4189 the \"%s\" format specifier. If that specifier is present, the input
4190 file name will be put into the command line at that spot. If the
4191 specifier is not present, the input should be read from standard
4192 input.
4193
4194 If they are variables, this variable is a string containing a
4195 Perl or Shell implementation for this functionality. This
4196 program will be transferred to the remote host, and it is
4197 available as shell function with the same name. A \"%t\" format
4198 specifier in the variable value denotes a temporary file.
4199
4200 The optional TEST command can be used for further tests, whether
4201 ENCODING and DECODING are applicable.")
4202
4203 (defun tramp-find-inline-encoding (vec)
4204 "Find an inline transfer encoding that works.
4205 Goes through the list `tramp-local-coding-commands' and
4206 `tramp-remote-coding-commands'."
4207 (save-excursion
4208 (let ((local-commands tramp-local-coding-commands)
4209 (magic "xyzzy")
4210 (p (tramp-get-connection-process vec))
4211 loc-enc loc-dec rem-enc rem-dec rem-test litem ritem found)
4212 (while (and local-commands (not found))
4213 (setq litem (pop local-commands))
4214 (catch 'wont-work-local
4215 (let ((format (nth 0 litem))
4216 (remote-commands tramp-remote-coding-commands))
4217 (setq loc-enc (nth 1 litem))
4218 (setq loc-dec (nth 2 litem))
4219 ;; If the local encoder or decoder is a string, the
4220 ;; corresponding command has to work locally.
4221 (if (not (stringp loc-enc))
4222 (tramp-message
4223 vec 5 "Checking local encoding function `%s'" loc-enc)
4224 (tramp-message
4225 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
4226 (unless (zerop (tramp-call-local-coding-command
4227 loc-enc nil nil))
4228 (throw 'wont-work-local nil)))
4229 (if (not (stringp loc-dec))
4230 (tramp-message
4231 vec 5 "Checking local decoding function `%s'" loc-dec)
4232 (tramp-message
4233 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
4234 (unless (zerop (tramp-call-local-coding-command
4235 loc-dec nil nil))
4236 (throw 'wont-work-local nil)))
4237 ;; Search for remote coding commands with the same format
4238 (while (and remote-commands (not found))
4239 (setq ritem (pop remote-commands))
4240 (catch 'wont-work-remote
4241 (when (equal format (nth 0 ritem))
4242 (setq rem-enc (nth 1 ritem))
4243 (setq rem-dec (nth 2 ritem))
4244 (setq rem-test (nth 3 ritem))
4245 ;; Check the remote test command if exists.
4246 (when (stringp rem-test)
4247 (tramp-message
4248 vec 5 "Checking remote test command `%s'" rem-test)
4249 (unless (tramp-send-command-and-check vec rem-test t)
4250 (throw 'wont-work-remote nil)))
4251 ;; Check if remote perl exists when necessary.
4252 (when (and (not (stringp rem-enc))
4253 (not (tramp-get-remote-perl vec)))
4254 (throw 'wont-work-remote nil))
4255 ;; Check if remote encoding and decoding commands can be
4256 ;; called remotely with null input and output. This makes
4257 ;; sure there are no syntax errors and the command is really
4258 ;; found. Note that we do not redirect stdout to /dev/null,
4259 ;; for two reasons: when checking the decoding command, we
4260 ;; actually check the output it gives. And also, when
4261 ;; redirecting "mimencode" output to /dev/null, then as root
4262 ;; it might change the permissions of /dev/null!
4263 (when (not (stringp rem-enc))
4264 (let ((name (symbol-name rem-enc)))
4265 (while (string-match (regexp-quote "-") name)
4266 (setq name (replace-match "_" nil t name)))
4267 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
4268 (setq rem-enc name)))
4269 (tramp-message
4270 vec 5
4271 "Checking remote encoding command `%s' for sanity" rem-enc)
4272 (unless (tramp-send-command-and-check
4273 vec (format "%s </dev/null" rem-enc) t)
4274 (throw 'wont-work-remote nil))
4275
4276 (when (not (stringp rem-dec))
4277 (let ((name (symbol-name rem-dec))
4278 (value (symbol-value rem-dec))
4279 tmpfile)
4280 (while (string-match (regexp-quote "-") name)
4281 (setq name (replace-match "_" nil t name)))
4282 (when (string-match "\\(^\\|[^%]\\)%t" value)
4283 (setq tmpfile
4284 (make-temp-name
4285 (expand-file-name
4286 tramp-temp-name-prefix
4287 (tramp-get-remote-tmpdir vec)))
4288 value
4289 (format-spec
4290 value
4291 (format-spec-make
4292 ?t
4293 (tramp-file-name-handler
4294 'file-remote-p tmpfile 'localname)))))
4295 (tramp-maybe-send-script vec value name)
4296 (setq rem-dec name)))
4297 (tramp-message
4298 vec 5
4299 "Checking remote decoding command `%s' for sanity" rem-dec)
4300 (unless (tramp-send-command-and-check
4301 vec
4302 (format "echo %s | %s | %s" magic rem-enc rem-dec)
4303 t)
4304 (throw 'wont-work-remote nil))
4305
4306 (with-current-buffer (tramp-get-buffer vec)
4307 (goto-char (point-min))
4308 (unless (looking-at (regexp-quote magic))
4309 (throw 'wont-work-remote nil)))
4310
4311 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
4312 (setq rem-enc (nth 1 ritem))
4313 (setq rem-dec (nth 2 ritem))
4314 (setq found t)))))))
4315
4316 (when found
4317 ;; Set connection properties. Since the commands are risky
4318 ;; (due to output direction), we cache them in the process cache.
4319 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
4320 (tramp-set-connection-property p "local-encoding" loc-enc)
4321 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
4322 (tramp-set-connection-property p "local-decoding" loc-dec)
4323 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
4324 (tramp-set-connection-property p "remote-encoding" rem-enc)
4325 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
4326 (tramp-set-connection-property p "remote-decoding" rem-dec)))))
4327
4328 (defun tramp-call-local-coding-command (cmd input output)
4329 "Call the local encoding or decoding command.
4330 If CMD contains \"%s\", provide input file INPUT there in command.
4331 Otherwise, INPUT is passed via standard input.
4332 INPUT can also be nil which means `/dev/null'.
4333 OUTPUT can be a string (which specifies a file name), or t (which
4334 means standard output and thus the current buffer), or nil (which
4335 means discard it)."
4336 (tramp-call-process
4337 nil tramp-encoding-shell
4338 (when (and input (not (string-match "%s" cmd))) input)
4339 (if (eq output t) t nil)
4340 nil
4341 tramp-encoding-command-switch
4342 (concat
4343 (if (string-match "%s" cmd) (format cmd input) cmd)
4344 (if (stringp output) (concat " >" output) ""))))
4345
4346 (defconst tramp-inline-compress-commands
4347 '(("gzip" "gzip -d")
4348 ("bzip2" "bzip2 -d")
4349 ("xz" "xz -d")
4350 ("compress" "compress -d"))
4351 "List of compress and decompress commands for inline transfer.
4352 Each item is a list that looks like this:
4353
4354 \(COMPRESS DECOMPRESS\)
4355
4356 COMPRESS or DECOMPRESS are strings with the respective commands.")
4357
4358 (defun tramp-find-inline-compress (vec)
4359 "Find an inline transfer compress command that works.
4360 Goes through the list `tramp-inline-compress-commands'."
4361 (save-excursion
4362 (let ((commands tramp-inline-compress-commands)
4363 (magic "xyzzy")
4364 (p (tramp-get-connection-process vec))
4365 item compress decompress found)
4366 (while (and commands (not found))
4367 (catch 'next
4368 (setq item (pop commands)
4369 compress (nth 0 item)
4370 decompress (nth 1 item))
4371 (tramp-message
4372 vec 5
4373 "Checking local compress commands `%s', `%s' for sanity"
4374 compress decompress)
4375 (unless
4376 (zerop
4377 (tramp-call-local-coding-command
4378 (format
4379 ;; Windows shells need the program file name after
4380 ;; the pipe symbol be quoted if they use forward
4381 ;; slashes as directory separators.
4382 (if (memq system-type '(windows-nt))
4383 "echo %s | \"%s\" | \"%s\""
4384 "echo %s | %s | %s")
4385 magic compress decompress) nil nil))
4386 (throw 'next nil))
4387 (tramp-message
4388 vec 5
4389 "Checking remote compress commands `%s', `%s' for sanity"
4390 compress decompress)
4391 (unless (tramp-send-command-and-check
4392 vec (format "echo %s | %s | %s" magic compress decompress) t)
4393 (throw 'next nil))
4394 (setq found t)))
4395
4396 ;; Did we find something?
4397 (if found
4398 (progn
4399 ;; Set connection properties. Since the commands are
4400 ;; risky (due to output direction), we cache them in the
4401 ;; process cache.
4402 (tramp-message
4403 vec 5 "Using inline transfer compress command `%s'" compress)
4404 (tramp-set-connection-property p "inline-compress" compress)
4405 (tramp-message
4406 vec 5 "Using inline transfer decompress command `%s'" decompress)
4407 (tramp-set-connection-property p "inline-decompress" decompress))
4408
4409 (tramp-set-connection-property p "inline-compress" nil)
4410 (tramp-set-connection-property p "inline-decompress" nil)
4411 (tramp-message
4412 vec 2 "Couldn't find an inline transfer compress command")))))
4413
4414 (defun tramp-compute-multi-hops (vec)
4415 "Expands VEC according to `tramp-default-proxies-alist'.
4416 Gateway hops are already opened."
4417 (let ((target-alist `(,vec))
4418 (hops (or (tramp-file-name-hop vec) ""))
4419 (item vec)
4420 choices proxy)
4421
4422 ;; Ad-hoc proxy definitions.
4423 (dolist (proxy (reverse (split-string hops tramp-postfix-hop-regexp 'omit)))
4424 (let ((user (tramp-file-name-user item))
4425 (host (tramp-file-name-host item))
4426 (proxy (concat
4427 tramp-prefix-format proxy tramp-postfix-host-format)))
4428 (tramp-message
4429 vec 5 "Add proxy (\"%s\" \"%s\" \"%s\")"
4430 (and (stringp host) (regexp-quote host))
4431 (and (stringp user) (regexp-quote user))
4432 proxy)
4433 ;; Add the hop.
4434 (add-to-list
4435 'tramp-default-proxies-alist
4436 (list (and (stringp host) (regexp-quote host))
4437 (and (stringp user) (regexp-quote user))
4438 proxy))
4439 (setq item (tramp-dissect-file-name proxy))))
4440 ;; Save the new value.
4441 (when (and hops tramp-save-ad-hoc-proxies)
4442 (customize-save-variable
4443 'tramp-default-proxies-alist tramp-default-proxies-alist))
4444
4445 ;; Look for proxy hosts to be passed.
4446 (setq choices tramp-default-proxies-alist)
4447 (while choices
4448 (setq item (pop choices)
4449 proxy (eval (nth 2 item)))
4450 (when (and
4451 ;; Host.
4452 (string-match (or (eval (nth 0 item)) "")
4453 (or (tramp-file-name-host (car target-alist)) ""))
4454 ;; User.
4455 (string-match (or (eval (nth 1 item)) "")
4456 (or (tramp-file-name-user (car target-alist)) "")))
4457 (if (null proxy)
4458 ;; No more hops needed.
4459 (setq choices nil)
4460 ;; Replace placeholders.
4461 (setq proxy
4462 (format-spec
4463 proxy
4464 (format-spec-make
4465 ?u (or (tramp-file-name-user (car target-alist)) "")
4466 ?h (or (tramp-file-name-host (car target-alist)) ""))))
4467 (with-parsed-tramp-file-name proxy l
4468 ;; Add the hop.
4469 (push l target-alist)
4470 ;; Start next search.
4471 (setq choices tramp-default-proxies-alist)))))
4472
4473 ;; Handle gateways.
4474 (when (and (boundp 'tramp-gw-tunnel-method) (boundp 'tramp-gw-socks-method)
4475 (string-match
4476 (format
4477 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method)
4478 (tramp-file-name-method (car target-alist))))
4479 (let ((gw (pop target-alist))
4480 (hop (pop target-alist)))
4481 ;; Is the method prepared for gateways?
4482 (unless (tramp-file-name-port hop)
4483 (tramp-error
4484 vec 'file-error
4485 "Connection `%s' is not supported for gateway access." hop))
4486 ;; Open the gateway connection.
4487 (push
4488 (vector
4489 (tramp-file-name-method hop) (tramp-file-name-user hop)
4490 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop) nil nil)
4491 target-alist)
4492 ;; For the password prompt, we need the correct values.
4493 ;; Therefore, we must remember the gateway vector. But we
4494 ;; cannot do it as connection property, because it shouldn't
4495 ;; be persistent. And we have no started process yet either.
4496 (tramp-set-file-property (car target-alist) "" "gateway" hop)))
4497
4498 ;; Foreign and out-of-band methods are not supported for multi-hops.
4499 (when (cdr target-alist)
4500 (setq choices target-alist)
4501 (while choices
4502 (setq item (pop choices))
4503 (when
4504 (or
4505 (not
4506 (tramp-get-method-parameter
4507 (tramp-file-name-method item) 'tramp-login-program))
4508 (tramp-get-method-parameter
4509 (tramp-file-name-method item) 'tramp-copy-program))
4510 (tramp-error
4511 vec 'file-error
4512 "Method `%s' is not supported for multi-hops."
4513 (tramp-file-name-method item)))))
4514
4515 ;; In case the host name is not used for the remote shell
4516 ;; command, the user could be misguided by applying a random
4517 ;; hostname.
4518 (let* ((v (car target-alist))
4519 (method (tramp-file-name-method v))
4520 (host (tramp-file-name-host v)))
4521 (unless
4522 (or
4523 ;; There are multi-hops.
4524 (cdr target-alist)
4525 ;; The host name is used for the remote shell command.
4526 (member
4527 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
4528 ;; The host is local. We cannot use `tramp-local-host-p'
4529 ;; here, because it opens a connection as well.
4530 (string-match tramp-local-host-regexp host))
4531 (tramp-error
4532 v 'file-error
4533 "Host `%s' looks like a remote host, `%s' can only use the local host"
4534 host method)))
4535
4536 ;; Result.
4537 target-alist))
4538
4539 (defun tramp-maybe-open-connection (vec)
4540 "Maybe open a connection VEC.
4541 Does not do anything if a connection is already open, but re-opens the
4542 connection if a previous connection has died for some reason."
4543 (tramp-check-proper-method-and-host vec)
4544
4545 (let ((p (tramp-get-connection-process vec))
4546 (process-name (tramp-get-connection-property vec "process-name" nil))
4547 (process-environment (copy-sequence process-environment))
4548 (pos (with-current-buffer (tramp-get-connection-buffer vec) (point))))
4549
4550 ;; If Tramp opens the same connection within a short time frame,
4551 ;; there is a problem. We shall signal this.
4552 (unless (or (and p (processp p) (memq (process-status p) '(run open)))
4553 (not (equal (butlast (append vec nil) 2)
4554 (car tramp-current-connection)))
4555 (> (tramp-time-diff
4556 (current-time) (cdr tramp-current-connection))
4557 (or tramp-connection-min-time-diff 0)))
4558 (throw 'suppress 'suppress))
4559
4560 ;; If too much time has passed since last command was sent, look
4561 ;; whether process is still alive. If it isn't, kill it. When
4562 ;; using ssh, it can sometimes happen that the remote end has hung
4563 ;; up but the local ssh client doesn't recognize this until it
4564 ;; tries to send some data to the remote end. So that's why we
4565 ;; try to send a command from time to time, then look again
4566 ;; whether the process is really alive.
4567 (condition-case nil
4568 (when (and (> (tramp-time-diff
4569 (current-time)
4570 (tramp-get-connection-property
4571 p "last-cmd-time" '(0 0 0)))
4572 60)
4573 p (processp p) (memq (process-status p) '(run open)))
4574 (tramp-send-command vec "echo are you awake" t t)
4575 (unless (and (memq (process-status p) '(run open))
4576 (tramp-wait-for-output p 10))
4577 ;; The error will be caught locally.
4578 (tramp-error vec 'file-error "Awake did fail")))
4579 (file-error
4580 (tramp-cleanup-connection vec t)
4581 (setq p nil)))
4582
4583 ;; New connection must be opened.
4584 (condition-case err
4585 (unless (and p (processp p) (memq (process-status p) '(run open)))
4586
4587 ;; If `non-essential' is non-nil, don't reopen a new connection.
4588 (when (and (boundp 'non-essential) (symbol-value 'non-essential))
4589 (throw 'non-essential 'non-essential))
4590
4591 (with-tramp-progress-reporter
4592 vec 3
4593 (if (zerop (length (tramp-file-name-user vec)))
4594 (format "Opening connection for %s using %s"
4595 (tramp-file-name-host vec)
4596 (tramp-file-name-method vec))
4597 (format "Opening connection for %s@%s using %s"
4598 (tramp-file-name-user vec)
4599 (tramp-file-name-host vec)
4600 (tramp-file-name-method vec)))
4601
4602 (catch 'uname-changed
4603 ;; Start new process.
4604 (when (and p (processp p))
4605 (delete-process p))
4606 (setenv "TERM" tramp-terminal-type)
4607 (setenv "LC_ALL" "en_US.utf8")
4608 (setenv "HISTFILE" "/dev/null")
4609 (setenv "PROMPT_COMMAND")
4610 (setenv "PS1" tramp-initial-end-of-output)
4611 (let* ((target-alist (tramp-compute-multi-hops vec))
4612 ;; We will apply `tramp-ssh-controlmaster-options'
4613 ;; only for the first hop.
4614 (options (if tramp-use-ssh-controlmaster-options
4615 tramp-ssh-controlmaster-options ""))
4616 (process-connection-type tramp-process-connection-type)
4617 (process-adaptive-read-buffering nil)
4618 (coding-system-for-read nil)
4619 ;; This must be done in order to avoid our file
4620 ;; name handler.
4621 (p (let ((default-directory
4622 (tramp-compat-temporary-file-directory)))
4623 (apply
4624 'start-process
4625 (tramp-get-connection-name vec)
4626 (tramp-get-connection-buffer vec)
4627 (if tramp-encoding-command-interactive
4628 (list tramp-encoding-shell
4629 tramp-encoding-command-interactive)
4630 (list tramp-encoding-shell))))))
4631
4632 ;; Set sentinel and query flag.
4633 (tramp-set-connection-property p "vector" vec)
4634 (set-process-sentinel p 'tramp-process-sentinel)
4635 (tramp-compat-set-process-query-on-exit-flag p nil)
4636 (setq tramp-current-connection
4637 (cons (butlast (append vec nil) 2) (current-time))
4638 tramp-current-host (system-name))
4639
4640 (tramp-message
4641 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
4642
4643 ;; Check whether process is alive.
4644 (tramp-barf-if-no-shell-prompt
4645 p 10
4646 "Couldn't find local shell prompt for %s" tramp-encoding-shell)
4647
4648 ;; Now do all the connections as specified.
4649 (while target-alist
4650 (let* ((hop (car target-alist))
4651 (l-method (tramp-file-name-method hop))
4652 (l-user (tramp-file-name-user hop))
4653 (l-host (tramp-file-name-host hop))
4654 (l-port nil)
4655 (login-program
4656 (tramp-get-method-parameter
4657 l-method 'tramp-login-program))
4658 (login-args
4659 (tramp-get-method-parameter
4660 l-method 'tramp-login-args))
4661 (login-env
4662 (tramp-get-method-parameter
4663 l-method 'tramp-login-env))
4664 (async-args
4665 (tramp-get-method-parameter
4666 l-method 'tramp-async-args))
4667 (connection-timeout
4668 (tramp-get-method-parameter
4669 l-method 'tramp-connection-timeout))
4670 (gw-args
4671 (tramp-get-method-parameter l-method 'tramp-gw-args))
4672 (gw (tramp-get-file-property hop "" "gateway" nil))
4673 (g-method (and gw (tramp-file-name-method gw)))
4674 (g-user (and gw (tramp-file-name-user gw)))
4675 (g-host (and gw (tramp-file-name-real-host gw)))
4676 (command login-program)
4677 ;; We don't create the temporary file. In
4678 ;; fact, it is just a prefix for the
4679 ;; ControlPath option of ssh; the real
4680 ;; temporary file has another name, and it is
4681 ;; created and protected by ssh. It is also
4682 ;; removed by ssh when the connection is
4683 ;; closed. The temporary file name is cached
4684 ;; in the main connection process, therefore
4685 ;; we cannot use `tramp-get-connection-process'.
4686 (tmpfile
4687 (with-tramp-connection-property
4688 (get-process (tramp-buffer-name vec)) "temp-file"
4689 (make-temp-name
4690 (expand-file-name
4691 tramp-temp-name-prefix
4692 (tramp-compat-temporary-file-directory)))))
4693 spec r-shell)
4694
4695 ;; Add arguments for asynchronous processes.
4696 (when (and process-name async-args)
4697 (setq login-args (append async-args login-args)))
4698
4699 ;; Add gateway arguments if necessary.
4700 (when (and gw gw-args)
4701 (setq login-args (append gw-args login-args)))
4702
4703 ;; Check for port number. Until now, there's no
4704 ;; need for handling like method, user, host.
4705 (when (string-match tramp-host-with-port-regexp l-host)
4706 (setq l-port (match-string 2 l-host)
4707 l-host (match-string 1 l-host)))
4708
4709 ;; Check, whether there is a restricted shell.
4710 (dolist (elt tramp-restricted-shell-hosts-alist)
4711 (when (string-match elt tramp-current-host)
4712 (setq r-shell t)))
4713
4714 ;; Set variables for computing the prompt for
4715 ;; reading password. They can also be derived
4716 ;; from a gateway.
4717 (setq tramp-current-method (or g-method l-method)
4718 tramp-current-user (or g-user l-user)
4719 tramp-current-host (or g-host l-host))
4720
4721 ;; Add login environment.
4722 (when login-env
4723 (setq
4724 login-env
4725 (mapcar
4726 (lambda (x)
4727 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
4728 (unless (member "" x) (mapconcat 'identity x " ")))
4729 login-env))
4730 (while login-env
4731 (setq command
4732 (format
4733 "%s=%s %s"
4734 (pop login-env)
4735 (tramp-shell-quote-argument (pop login-env))
4736 command)))
4737 (setq command (concat "env " command)))
4738
4739 ;; Replace `login-args' place holders.
4740 (setq
4741 l-host (or l-host "")
4742 l-user (or l-user "")
4743 l-port (or l-port "")
4744 spec (format-spec-make ?t tmpfile)
4745 options (format-spec options spec)
4746 spec (format-spec-make
4747 ?h l-host ?u l-user ?p l-port ?c options)
4748 command
4749 (concat
4750 ;; We do not want to see the trailing local
4751 ;; prompt in `start-file-process'.
4752 (unless r-shell "exec ")
4753 command " "
4754 (mapconcat
4755 (lambda (x)
4756 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
4757 (unless (member "" x) (mapconcat 'identity x " ")))
4758 login-args " ")
4759 ;; Local shell could be a Windows COMSPEC. It
4760 ;; doesn't know the ";" syntax, but we must exit
4761 ;; always for `start-file-process'. It could
4762 ;; also be a restricted shell, which does not
4763 ;; allow "exec".
4764 (when r-shell " && exit || exit")))
4765
4766 ;; Send the command.
4767 (tramp-message vec 3 "Sending command `%s'" command)
4768 (tramp-send-command vec command t t)
4769 (tramp-process-actions
4770 p vec pos tramp-actions-before-shell
4771 (or connection-timeout tramp-connection-timeout))
4772 (tramp-message
4773 vec 3 "Found remote shell prompt on `%s'" l-host))
4774 ;; Next hop.
4775 (setq options ""
4776 target-alist (cdr target-alist)))
4777
4778 ;; Make initial shell settings.
4779 (tramp-open-connection-setup-interactive-shell p vec)))))
4780
4781 ;; When the user did interrupt, we must cleanup.
4782 (quit
4783 (tramp-cleanup-connection vec t)
4784 ;; Propagate the quit signal.
4785 (signal (car err) (cdr err))))))
4786
4787 (defun tramp-send-command (vec command &optional neveropen nooutput)
4788 "Send the COMMAND to connection VEC.
4789 Erases temporary buffer before sending the command. If optional
4790 arg NEVEROPEN is non-nil, never try to open the connection. This
4791 is meant to be used from `tramp-maybe-open-connection' only. The
4792 function waits for output unless NOOUTPUT is set."
4793 (unless neveropen (tramp-maybe-open-connection vec))
4794 (let ((p (tramp-get-connection-process vec)))
4795 (when (tramp-get-connection-property p "remote-echo" nil)
4796 ;; We mark the command string that it can be erased in the output buffer.
4797 (tramp-set-connection-property p "check-remote-echo" t)
4798 ;; If we put `tramp-echo-mark' after a trailing newline (which
4799 ;; is assumed to be unquoted) `tramp-send-string' doesn't see
4800 ;; that newline and adds `tramp-rsh-end-of-line' right after
4801 ;; `tramp-echo-mark', so the remote shell sees two consecutive
4802 ;; trailing line endings and sends two prompts after executing
4803 ;; the command, which confuses `tramp-wait-for-output'.
4804 (when (and (not (string= command ""))
4805 (string-equal (substring command -1) "\n"))
4806 (setq command (substring command 0 -1)))
4807 ;; No need to restore a trailing newline here since `tramp-send-string'
4808 ;; makes sure that the string ends in `tramp-rsh-end-of-line', anyway.
4809 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
4810 ;; Send the command.
4811 (tramp-message vec 6 "%s" command)
4812 (tramp-send-string vec command)
4813 (unless nooutput (tramp-wait-for-output p))))
4814
4815 (defun tramp-wait-for-output (proc &optional timeout)
4816 "Wait for output from remote command."
4817 (unless (buffer-live-p (process-buffer proc))
4818 (delete-process proc)
4819 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
4820 (with-current-buffer (process-buffer proc)
4821 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
4822 ;; be leading escape sequences, which must be ignored.
4823 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
4824 ;; Sometimes, the commands do not return a newline but a
4825 ;; null byte before the shell prompt, for example "git
4826 ;; ls-files -c -z ...".
4827 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
4828 (found (tramp-wait-for-regexp proc timeout regexp1)))
4829 (if found
4830 (let (buffer-read-only)
4831 ;; A simple-minded busybox has sent " ^H" sequences.
4832 ;; Delete them.
4833 (goto-char (point-min))
4834 (when (re-search-forward "^\\(.\b\\)+$" (point-at-eol) t)
4835 (forward-line 1)
4836 (delete-region (point-min) (point)))
4837 ;; Delete the prompt.
4838 (goto-char (point-max))
4839 (re-search-backward regexp nil t)
4840 (delete-region (point) (point-max)))
4841 (if timeout
4842 (tramp-error
4843 proc 'file-error
4844 "[[Remote prompt `%s' not found in %d secs]]"
4845 tramp-end-of-output timeout)
4846 (tramp-error
4847 proc 'file-error
4848 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
4849 ;; Return value is whether end-of-output sentinel was found.
4850 found)))
4851
4852 (defun tramp-send-command-and-check
4853 (vec command &optional subshell dont-suppress-err)
4854 "Run COMMAND and check its exit status.
4855 Sends `echo $?' along with the COMMAND for checking the exit status.
4856 If COMMAND is nil, just sends `echo $?'. Returns `t' if the exit
4857 status is 0, and `nil' otherwise.
4858
4859 If the optional argument SUBSHELL is non-nil, the command is
4860 executed in a subshell, ie surrounded by parentheses. If
4861 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
4862 (tramp-send-command
4863 vec
4864 (concat (if subshell "( " "")
4865 command
4866 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
4867 "echo tramp_exit_status $?"
4868 (if subshell " )" "")))
4869 (with-current-buffer (tramp-get-connection-buffer vec)
4870 (goto-char (point-max))
4871 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
4872 (tramp-error
4873 vec 'file-error "Couldn't find exit status of `%s'" command))
4874 (skip-chars-forward "^ ")
4875 (prog1
4876 (zerop (read (current-buffer)))
4877 (let (buffer-read-only)
4878 (delete-region (match-beginning 0) (point-max))))))
4879
4880 (defun tramp-barf-unless-okay (vec command fmt &rest args)
4881 "Run COMMAND, check exit status, throw error if exit status not okay.
4882 Similar to `tramp-send-command-and-check' but accepts two more arguments
4883 FMT and ARGS which are passed to `error'."
4884 (or (tramp-send-command-and-check vec command)
4885 (apply 'tramp-error vec 'file-error fmt args)))
4886
4887 (defun tramp-send-command-and-read (vec command &optional noerror)
4888 "Run COMMAND and return the output, which must be a Lisp expression.
4889 In case there is no valid Lisp expression and NOERROR is nil, it
4890 raises an error."
4891 (when (if noerror
4892 (tramp-send-command-and-check vec command)
4893 (tramp-barf-unless-okay
4894 vec command "`%s' returns with error" command))
4895 (with-current-buffer (tramp-get-connection-buffer vec)
4896 ;; Read the expression.
4897 (goto-char (point-min))
4898 (condition-case nil
4899 (prog1 (read (current-buffer))
4900 ;; Error handling.
4901 (when (re-search-forward "\\S-" (point-at-eol) t)
4902 (error nil)))
4903 (error (unless noerror
4904 (tramp-error
4905 vec 'file-error
4906 "`%s' does not return a valid Lisp expression: `%s'"
4907 command (buffer-string))))))))
4908
4909 (defun tramp-convert-file-attributes (vec attr)
4910 "Convert `file-attributes' ATTR generated by perl script, stat or ls.
4911 Convert file mode bits to string and set virtual device number.
4912 Return ATTR."
4913 (when attr
4914 ;; Remove color escape sequences from symlink.
4915 (when (stringp (car attr))
4916 (while (string-match tramp-color-escape-sequence-regexp (car attr))
4917 (setcar attr (replace-match "" nil nil (car attr)))))
4918 ;; Convert uid and gid. Use -1 as indication of unusable value.
4919 (when (and (numberp (nth 2 attr)) (< (nth 2 attr) 0))
4920 (setcar (nthcdr 2 attr) -1))
4921 (when (and (floatp (nth 2 attr))
4922 (<= (nth 2 attr) (tramp-compat-most-positive-fixnum)))
4923 (setcar (nthcdr 2 attr) (round (nth 2 attr))))
4924 (when (and (numberp (nth 3 attr)) (< (nth 3 attr) 0))
4925 (setcar (nthcdr 3 attr) -1))
4926 (when (and (floatp (nth 3 attr))
4927 (<= (nth 3 attr) (tramp-compat-most-positive-fixnum)))
4928 (setcar (nthcdr 3 attr) (round (nth 3 attr))))
4929 ;; Convert last access time.
4930 (unless (listp (nth 4 attr))
4931 (setcar (nthcdr 4 attr)
4932 (list (floor (nth 4 attr) 65536)
4933 (floor (mod (nth 4 attr) 65536)))))
4934 ;; Convert last modification time.
4935 (unless (listp (nth 5 attr))
4936 (setcar (nthcdr 5 attr)
4937 (list (floor (nth 5 attr) 65536)
4938 (floor (mod (nth 5 attr) 65536)))))
4939 ;; Convert last status change time.
4940 (unless (listp (nth 6 attr))
4941 (setcar (nthcdr 6 attr)
4942 (list (floor (nth 6 attr) 65536)
4943 (floor (mod (nth 6 attr) 65536)))))
4944 ;; Convert file size.
4945 (when (< (nth 7 attr) 0)
4946 (setcar (nthcdr 7 attr) -1))
4947 (when (and (floatp (nth 7 attr))
4948 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
4949 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
4950 ;; Convert file mode bits to string.
4951 (unless (stringp (nth 8 attr))
4952 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
4953 (when (stringp (car attr))
4954 (aset (nth 8 attr) 0 ?l)))
4955 ;; Convert directory indication bit.
4956 (when (string-match "^d" (nth 8 attr))
4957 (setcar attr t))
4958 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
4959 (when (consp (car attr))
4960 (if (and (stringp (caar attr))
4961 (string-match ".+ -> .\\(.+\\)." (caar attr)))
4962 (setcar attr (match-string 1 (caar attr)))
4963 (setcar attr nil)))
4964 ;; Set file's gid change bit.
4965 (setcar (nthcdr 9 attr)
4966 (if (numberp (nth 3 attr))
4967 (not (= (nth 3 attr)
4968 (tramp-get-remote-gid vec 'integer)))
4969 (not (string-equal
4970 (nth 3 attr)
4971 (tramp-get-remote-gid vec 'string)))))
4972 ;; Convert inode.
4973 (unless (listp (nth 10 attr))
4974 (setcar (nthcdr 10 attr)
4975 (condition-case nil
4976 (cons (floor (nth 10 attr) 65536)
4977 (floor (mod (nth 10 attr) 65536)))
4978 ;; Inodes can be incredible huge. We must hide this.
4979 (error (tramp-get-inode vec)))))
4980 ;; Set virtual device number.
4981 (setcar (nthcdr 11 attr)
4982 (tramp-get-device vec))
4983 attr))
4984
4985 (defun tramp-shell-case-fold (string)
4986 "Converts STRING to shell glob pattern which ignores case."
4987 (mapconcat
4988 (lambda (c)
4989 (if (equal (downcase c) (upcase c))
4990 (vector c)
4991 (format "[%c%c]" (downcase c) (upcase c))))
4992 string
4993 ""))
4994
4995 (defun tramp-make-copy-program-file-name (vec)
4996 "Create a file name suitable to be passed to `scp' or `nc' and workalikes."
4997 (let ((method (tramp-file-name-method vec))
4998 (user (tramp-file-name-user vec))
4999 (host (tramp-file-name-real-host vec))
5000 (localname (tramp-shell-quote-argument
5001 (tramp-file-name-localname vec))))
5002 (cond
5003 ((tramp-get-method-parameter method 'tramp-remote-copy-program)
5004 localname)
5005 ((not (zerop (length user)))
5006 (shell-quote-argument (format "%s@%s:%s" user host localname)))
5007 (t (shell-quote-argument (format "%s:%s" host localname))))))
5008
5009 (defun tramp-method-out-of-band-p (vec size)
5010 "Return t if this is an out-of-band method, nil otherwise."
5011 (and
5012 ;; It shall be an out-of-band method.
5013 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
5014 ;; There must be a size, otherwise the file doesn't exist.
5015 (numberp size)
5016 ;; Either the file size is large enough, or (in rare cases) there
5017 ;; does not exist a remote encoding.
5018 (or (null tramp-copy-size-limit)
5019 (> size tramp-copy-size-limit)
5020 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
5021
5022 ;; Variables local to connection.
5023
5024 (defun tramp-get-remote-path (vec)
5025 (with-tramp-connection-property
5026 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
5027 ;; cache the result for the session only. Otherwise, the result
5028 ;; is cached persistently.
5029 (if (memq 'tramp-own-remote-path tramp-remote-path)
5030 (tramp-get-connection-process vec)
5031 vec)
5032 "remote-path"
5033 (let* ((remote-path (copy-tree tramp-remote-path))
5034 (elt1 (memq 'tramp-default-remote-path remote-path))
5035 (elt2 (memq 'tramp-own-remote-path remote-path))
5036 (default-remote-path
5037 (when elt1
5038 (or
5039 (tramp-send-command-and-read
5040 vec "echo \\\"`getconf PATH 2>/dev/null`\\\"" 'noerror)
5041 ;; Default if "getconf" is not available.
5042 (progn
5043 (tramp-message
5044 vec 3
5045 "`getconf PATH' not successful, using default value \"%s\"."
5046 "/bin:/usr/bin")
5047 "/bin:/usr/bin"))))
5048 (own-remote-path
5049 (when elt2
5050 (condition-case nil
5051 (tramp-send-command-and-read vec "echo \\\"$PATH\\\"")
5052 (error
5053 (tramp-message
5054 vec 3 "$PATH not set, ignoring `tramp-own-remote-path'.")
5055 nil)))))
5056
5057 ;; Replace place holder `tramp-default-remote-path'.
5058 (when elt1
5059 (setcdr elt1
5060 (append
5061 (tramp-compat-split-string (or default-remote-path "") ":")
5062 (cdr elt1)))
5063 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
5064
5065 ;; Replace place holder `tramp-own-remote-path'.
5066 (when elt2
5067 (setcdr elt2
5068 (append
5069 (tramp-compat-split-string (or own-remote-path "") ":")
5070 (cdr elt2)))
5071 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
5072
5073 ;; Remove double entries.
5074 (setq elt1 remote-path)
5075 (while (consp elt1)
5076 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
5077 (setcar elt2 nil))
5078 (setq elt1 (cdr elt1)))
5079
5080 ;; Remove non-existing directories.
5081 (delq
5082 nil
5083 (mapcar
5084 (lambda (x)
5085 (and
5086 (stringp x)
5087 (file-directory-p
5088 (tramp-make-tramp-file-name
5089 (tramp-file-name-method vec)
5090 (tramp-file-name-user vec)
5091 (tramp-file-name-host vec)
5092 x))
5093 x))
5094 remote-path)))))
5095
5096 (defun tramp-get-remote-locale (vec)
5097 (with-tramp-connection-property vec "locale"
5098 (tramp-send-command vec "locale -a")
5099 (let ((candidates '("en_US.utf8" "C.utf8"))
5100 locale)
5101 (with-current-buffer (tramp-get-connection-buffer vec)
5102 (while candidates
5103 (goto-char (point-min))
5104 (if (string-match (concat "^" (car candidates) "$") (buffer-string))
5105 (setq locale (car candidates)
5106 candidates nil)
5107 (setq candidates (cdr candidates)))))
5108 ;; Return value.
5109 (format "LC_ALL=%s" (or locale "C")))))
5110
5111 (defun tramp-get-ls-command (vec)
5112 (with-tramp-connection-property vec "ls"
5113 (tramp-message vec 5 "Finding a suitable `ls' command")
5114 (or
5115 (catch 'ls-found
5116 (dolist (cmd '("ls" "gnuls" "gls"))
5117 (let ((dl (tramp-get-remote-path vec))
5118 result)
5119 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
5120 ;; Check parameters. On busybox, "ls" output coloring is
5121 ;; enabled by default sometimes. So we try to disable it
5122 ;; when possible. $LS_COLORING is not supported there.
5123 ;; Some "ls" versions are sensible wrt the order of
5124 ;; arguments, they fail when "-al" is after the
5125 ;; "--color=never" argument (for example on FreeBSD).
5126 (when (tramp-send-command-and-check
5127 vec (format "%s -lnd /" result))
5128 (when (tramp-send-command-and-check
5129 vec (format
5130 "%s --color=never -al /dev/null" result))
5131 (setq result (concat result " --color=never")))
5132 (throw 'ls-found result))
5133 (setq dl (cdr dl))))))
5134 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
5135
5136 (defun tramp-get-ls-command-with-dired (vec)
5137 (save-match-data
5138 (with-tramp-connection-property vec "ls-dired"
5139 (tramp-message vec 5 "Checking, whether `ls --dired' works")
5140 ;; Some "ls" versions are sensible wrt the order of arguments,
5141 ;; they fail when "-al" is after the "--dired" argument (for
5142 ;; example on FreeBSD).
5143 (tramp-send-command-and-check
5144 vec (format "%s --dired -al /dev/null" (tramp-get-ls-command vec))))))
5145
5146 (defun tramp-get-test-command (vec)
5147 (with-tramp-connection-property vec "test"
5148 (tramp-message vec 5 "Finding a suitable `test' command")
5149 (if (tramp-send-command-and-check vec "test 0")
5150 "test"
5151 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
5152
5153 (defun tramp-get-test-nt-command (vec)
5154 ;; Does `test A -nt B' work? Use abominable `find' construct if it
5155 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
5156 ;; for otherwise the shell crashes.
5157 (with-tramp-connection-property vec "test-nt"
5158 (or
5159 (progn
5160 (tramp-send-command
5161 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
5162 (with-current-buffer (tramp-get-buffer vec)
5163 (goto-char (point-min))
5164 (when (looking-at (regexp-quote tramp-end-of-output))
5165 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
5166 (progn
5167 (tramp-send-command
5168 vec
5169 (format
5170 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
5171 (tramp-get-test-command vec)))
5172 "tramp_test_nt %s %s"))))
5173
5174 (defun tramp-get-file-exists-command (vec)
5175 (with-tramp-connection-property vec "file-exists"
5176 (tramp-message vec 5 "Finding command to check if file exists")
5177 (tramp-find-file-exists-command vec)))
5178
5179 (defun tramp-get-remote-ln (vec)
5180 (with-tramp-connection-property vec "ln"
5181 (tramp-message vec 5 "Finding a suitable `ln' command")
5182 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
5183
5184 (defun tramp-get-remote-perl (vec)
5185 (with-tramp-connection-property vec "perl"
5186 (tramp-message vec 5 "Finding a suitable `perl' command")
5187 (let ((result
5188 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
5189 (tramp-find-executable
5190 vec "perl" (tramp-get-remote-path vec)))))
5191 ;; We must check also for some Perl modules.
5192 (when result
5193 (with-tramp-connection-property vec "perl-file-spec"
5194 (tramp-send-command-and-check
5195 vec (format "%s -e 'use File::Spec;'" result)))
5196 (with-tramp-connection-property vec "perl-cwd-realpath"
5197 (tramp-send-command-and-check
5198 vec (format "%s -e 'use Cwd \"realpath\";'" result))))
5199 result)))
5200
5201 (defun tramp-get-remote-stat (vec)
5202 (with-tramp-connection-property vec "stat"
5203 (tramp-message vec 5 "Finding a suitable `stat' command")
5204 (let ((result (tramp-find-executable
5205 vec "stat" (tramp-get-remote-path vec)))
5206 tmp)
5207 ;; Check whether stat(1) returns usable syntax. "%s" does not
5208 ;; work on older AIX systems.
5209 (when result
5210 (setq tmp
5211 (tramp-send-command-and-read
5212 vec (format "%s -c '(\"%%N\" %%s)' /" result) 'noerror))
5213 (unless (and (listp tmp) (stringp (car tmp))
5214 (string-match "^./.$" (car tmp))
5215 (integerp (cadr tmp)))
5216 (setq result nil)))
5217 result)))
5218
5219 (defun tramp-get-remote-readlink (vec)
5220 (with-tramp-connection-property vec "readlink"
5221 (tramp-message vec 5 "Finding a suitable `readlink' command")
5222 (let ((result (tramp-find-executable
5223 vec "readlink" (tramp-get-remote-path vec))))
5224 (when (and result
5225 (tramp-send-command-and-check
5226 vec (format "%s --canonicalize-missing /" result)))
5227 result))))
5228
5229 (defun tramp-get-remote-trash (vec)
5230 (with-tramp-connection-property vec "trash"
5231 (tramp-message vec 5 "Finding a suitable `trash' command")
5232 (tramp-find-executable vec "trash" (tramp-get-remote-path vec))))
5233
5234 (defun tramp-get-remote-touch (vec)
5235 (with-tramp-connection-property vec "touch"
5236 (tramp-message vec 5 "Finding a suitable `touch' command")
5237 (let ((result (tramp-find-executable
5238 vec "touch" (tramp-get-remote-path vec)))
5239 (tmpfile
5240 (make-temp-name
5241 (expand-file-name
5242 tramp-temp-name-prefix (tramp-get-remote-tmpdir vec)))))
5243 ;; Busyboxes do support the "-t" option only when they have been
5244 ;; built with the DESKTOP config option. Let's check it.
5245 (when result
5246 (tramp-set-connection-property
5247 vec "touch-t"
5248 (tramp-send-command-and-check
5249 vec
5250 (format
5251 "%s -t %s %s"
5252 result
5253 (format-time-string "%Y%m%d%H%M.%S")
5254 (tramp-file-name-handler 'file-remote-p tmpfile 'localname))))
5255 (delete-file tmpfile))
5256 result)))
5257
5258 (defun tramp-get-remote-gvfs-monitor-dir (vec)
5259 (with-tramp-connection-property vec "gvfs-monitor-dir"
5260 (tramp-message vec 5 "Finding a suitable `gvfs-monitor-dir' command")
5261 (tramp-find-executable
5262 vec "gvfs-monitor-dir" (tramp-get-remote-path vec) t t)))
5263
5264 (defun tramp-get-remote-inotifywait (vec)
5265 (with-tramp-connection-property vec "inotifywait"
5266 (tramp-message vec 5 "Finding a suitable `inotifywait' command")
5267 (tramp-find-executable vec "inotifywait" (tramp-get-remote-path vec) t t)))
5268
5269 (defun tramp-get-remote-id (vec)
5270 (with-tramp-connection-property vec "id"
5271 (tramp-message vec 5 "Finding POSIX `id' command")
5272 (catch 'id-found
5273 (dolist (cmd '("id" "gid"))
5274 (let ((dl (tramp-get-remote-path vec))
5275 result)
5276 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
5277 ;; Check POSIX parameter.
5278 (when (tramp-send-command-and-check vec (format "%s -u" result))
5279 (throw 'id-found result))
5280 (setq dl (cdr dl))))))))
5281
5282 (defun tramp-get-remote-uid-with-id (vec id-format)
5283 (tramp-send-command-and-read
5284 vec
5285 (format "%s -u%s %s"
5286 (tramp-get-remote-id vec)
5287 (if (equal id-format 'integer) "" "n")
5288 (if (equal id-format 'integer)
5289 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/"))))
5290
5291 (defun tramp-get-remote-uid-with-perl (vec id-format)
5292 (tramp-send-command-and-read
5293 vec
5294 (format "%s -le '%s'"
5295 (tramp-get-remote-perl vec)
5296 (if (equal id-format 'integer)
5297 "print $>"
5298 "print \"\\\"\", scalar getpwuid($>), \"\\\"\""))))
5299
5300 (defun tramp-get-remote-python (vec)
5301 (with-tramp-connection-property vec "python"
5302 (tramp-message vec 5 "Finding a suitable `python' command")
5303 (or (tramp-find-executable vec "python" (tramp-get-remote-path vec))
5304 (tramp-find-executable vec "python2" (tramp-get-remote-path vec))
5305 (tramp-find-executable vec "python3" (tramp-get-remote-path vec)))))
5306
5307 (defun tramp-get-remote-uid-with-python (vec id-format)
5308 (tramp-send-command-and-read
5309 vec
5310 (format "%s -c \"%s\""
5311 (tramp-get-remote-python vec)
5312 (if (equal id-format 'integer)
5313 "import os; print (os.getuid())"
5314 "import os, pwd; print ('\\\"' + pwd.getpwuid(os.getuid())[0] + '\\\"')"))))
5315
5316 (defun tramp-get-remote-uid (vec id-format)
5317 (with-tramp-connection-property vec (format "uid-%s" id-format)
5318 (let ((res
5319 (ignore-errors
5320 (cond
5321 ((tramp-get-remote-id vec)
5322 (tramp-get-remote-uid-with-id vec id-format))
5323 ((tramp-get-remote-perl vec)
5324 (tramp-get-remote-uid-with-perl vec id-format))
5325 ((tramp-get-remote-python vec)
5326 (tramp-get-remote-uid-with-python vec id-format))))))
5327 ;; Ensure there is a valid result.
5328 (cond
5329 ((and (equal id-format 'integer) (not (integerp res))) -1)
5330 ((and (equal id-format 'string) (not (stringp res))) "UNKNOWN")
5331 (t res)))))
5332
5333 (defun tramp-get-remote-gid-with-id (vec id-format)
5334 (tramp-send-command-and-read
5335 vec
5336 (format "%s -g%s %s"
5337 (tramp-get-remote-id vec)
5338 (if (equal id-format 'integer) "" "n")
5339 (if (equal id-format 'integer)
5340 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/"))))
5341
5342 (defun tramp-get-remote-gid-with-perl (vec id-format)
5343 (tramp-send-command-and-read
5344 vec
5345 (format "%s -le '%s'"
5346 (tramp-get-remote-perl vec)
5347 (if (equal id-format 'integer)
5348 "print ($)=~/(\\d+)/)"
5349 "print \"\\\"\", scalar getgrgid($)), \"\\\"\""))))
5350
5351 (defun tramp-get-remote-gid-with-python (vec id-format)
5352 (tramp-send-command-and-read
5353 vec
5354 (format "%s -c \"%s\""
5355 (tramp-get-remote-python vec)
5356 (if (equal id-format 'integer)
5357 "import os; print (os.getgid())"
5358 "import os, grp; print ('\\\"' + grp.getgrgid(os.getgid())[0] + '\\\"')"))))
5359
5360 (defun tramp-get-remote-gid (vec id-format)
5361 (with-tramp-connection-property vec (format "gid-%s" id-format)
5362 (let ((res
5363 (ignore-errors
5364 (cond
5365 ((tramp-get-remote-id vec)
5366 (tramp-get-remote-gid-with-id vec id-format))
5367 ((tramp-get-remote-perl vec)
5368 (tramp-get-remote-gid-with-perl vec id-format))
5369 ((tramp-get-remote-python vec)
5370 (tramp-get-remote-gid-with-python vec id-format))))))
5371 ;; Ensure there is a valid result.
5372 (cond
5373 ((and (equal id-format 'integer) (not (integerp res))) -1)
5374 ((and (equal id-format 'string) (not (stringp res))) "UNKNOWN")
5375 (t res)))))
5376
5377 ;; Some predefined connection properties.
5378 (defun tramp-get-inline-compress (vec prop size)
5379 "Return the compress command related to PROP.
5380 PROP is either `inline-compress' or `inline-decompress'. SIZE is
5381 the length of the file to be compressed.
5382
5383 If no corresponding command is found, nil is returned."
5384 (when (and (integerp tramp-inline-compress-start-size)
5385 (> size tramp-inline-compress-start-size))
5386 (with-tramp-connection-property (tramp-get-connection-process vec) prop
5387 (tramp-find-inline-compress vec)
5388 (tramp-get-connection-property
5389 (tramp-get-connection-process vec) prop nil))))
5390
5391 (defun tramp-get-inline-coding (vec prop size)
5392 "Return the coding command related to PROP.
5393 PROP is either `remote-encoding', `remote-decoding',
5394 `local-encoding' or `local-decoding'.
5395
5396 SIZE is the length of the file to be coded. Depending on SIZE,
5397 compression might be applied.
5398
5399 If no corresponding command is found, nil is returned.
5400 Otherwise, either a string is returned which contains a `%s' mark
5401 to be used for the respective input or output file; or a Lisp
5402 function cell is returned to be applied on a buffer."
5403 ;; We must catch the errors, because we want to return `nil', when
5404 ;; no inline coding is found.
5405 (ignore-errors
5406 (let ((coding
5407 (with-tramp-connection-property
5408 (tramp-get-connection-process vec) prop
5409 (tramp-find-inline-encoding vec)
5410 (tramp-get-connection-property
5411 (tramp-get-connection-process vec) prop nil)))
5412 (prop1 (if (string-match "encoding" prop)
5413 "inline-compress" "inline-decompress"))
5414 compress)
5415 ;; The connection property might have been cached. So we must
5416 ;; send the script to the remote side - maybe.
5417 (when (and coding (symbolp coding) (string-match "remote" prop))
5418 (let ((name (symbol-name coding)))
5419 (while (string-match (regexp-quote "-") name)
5420 (setq name (replace-match "_" nil t name)))
5421 (tramp-maybe-send-script vec (symbol-value coding) name)
5422 (setq coding name)))
5423 (when coding
5424 ;; Check for the `compress' command.
5425 (setq compress (tramp-get-inline-compress vec prop1 size))
5426 ;; Return the value.
5427 (cond
5428 ((and compress (symbolp coding))
5429 (if (string-match "decompress" prop1)
5430 `(lambda (beg end)
5431 (,coding beg end)
5432 (let ((coding-system-for-write 'binary)
5433 (coding-system-for-read 'binary))
5434 (apply
5435 'call-process-region (point-min) (point-max)
5436 (car (split-string ,compress)) t t nil
5437 (cdr (split-string ,compress)))))
5438 `(lambda (beg end)
5439 (let ((coding-system-for-write 'binary)
5440 (coding-system-for-read 'binary))
5441 (apply
5442 'call-process-region beg end
5443 (car (split-string ,compress)) t t nil
5444 (cdr (split-string ,compress))))
5445 (,coding (point-min) (point-max)))))
5446 ((symbolp coding)
5447 coding)
5448 ((and compress (string-match "decoding" prop))
5449 (format
5450 ;; Windows shells need the program file name after
5451 ;; the pipe symbol be quoted if they use forward
5452 ;; slashes as directory separators.
5453 (cond
5454 ((and (string-match "local" prop)
5455 (memq system-type '(windows-nt)))
5456 "(%s | \"%s\")")
5457 ((string-match "local" prop) "(%s | %s)")
5458 (t "(%s | %s >%%s)"))
5459 coding compress))
5460 (compress
5461 (format
5462 ;; Windows shells need the program file name after
5463 ;; the pipe symbol be quoted if they use forward
5464 ;; slashes as directory separators.
5465 (if (and (string-match "local" prop)
5466 (memq system-type '(windows-nt)))
5467 "(%s <%%s | \"%s\")"
5468 "(%s <%%s | %s)")
5469 compress coding))
5470 ((string-match "decoding" prop)
5471 (cond
5472 ((string-match "local" prop) (format "%s" coding))
5473 (t (format "%s >%%s" coding))))
5474 (t
5475 (format "%s <%%s" coding)))))))
5476
5477 (add-hook 'tramp-unload-hook
5478 (lambda ()
5479 (unload-feature 'tramp-sh 'force)))
5480
5481 (provide 'tramp-sh)
5482
5483 ;;; TODO:
5484
5485 ;; * Don't use globbing for directories with many files, as this is
5486 ;; likely to produce long command lines, and some shells choke on
5487 ;; long command lines.
5488 ;; * Make it work for different encodings, and for different file name
5489 ;; encodings, too. (Daniel Pittman)
5490 ;; * Don't search for perl5 and perl. Instead, only search for perl and
5491 ;; then look if it's the right version (with `perl -v').
5492 ;; * When editing a remote CVS controlled file as a different user, VC
5493 ;; gets confused about the file locking status. Try to find out why
5494 ;; the workaround doesn't work.
5495 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5496 ;; until the last but one hop via `start-file-process'. Apply it
5497 ;; also for ftp and smb.
5498 ;; * WIBNI if we had a command "trampclient"? If I was editing in
5499 ;; some shell with root privileges, it would be nice if I could
5500 ;; just call
5501 ;; trampclient filename.c
5502 ;; as an editor, and the _current_ shell would connect to an Emacs
5503 ;; server and would be used in an existing non-privileged Emacs
5504 ;; session for doing the editing in question.
5505 ;; That way, I need not tell Emacs my password again and be afraid
5506 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
5507 ;; once display a just typed password in the context of a keyboard
5508 ;; sequence prompt for a question immediately following in a shell
5509 ;; script run within Emacs -- nasty).
5510 ;; And if I have some ssh session running to a different computer,
5511 ;; having the possibility of passing a local file there to a local
5512 ;; Emacs session (in case I can arrange for a connection back) would
5513 ;; be nice.
5514 ;; Likely the corresponding Tramp server should not allow the
5515 ;; equivalent of the emacsclient -eval option in order to make this
5516 ;; reasonably unproblematic. And maybe trampclient should have some
5517 ;; way of passing credentials, like by using an SSL socket or
5518 ;; something. (David Kastrup)
5519 ;; * Reconnect directly to a compliant shell without first going
5520 ;; through the user's default shell. (Pete Forman)
5521 ;; * How can I interrupt the remote process with a signal
5522 ;; (interrupt-process seems not to work)? (Markus Triska)
5523 ;; * Avoid the local shell entirely for starting remote processes. If
5524 ;; so, I think even a signal, when delivered directly to the local
5525 ;; SSH instance, would correctly be propagated to the remote process
5526 ;; automatically; possibly SSH would have to be started with
5527 ;; "-t". (Markus Triska)
5528 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5529 ;; isn't on the remote host. (Mark A. Hershberger)
5530 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
5531 ;; * Optimize out-of-band copying when both methods are scp-like (not
5532 ;; rsync).
5533 ;; * Keep a second connection open for out-of-band methods like scp or
5534 ;; rsync.
5535
5536 ;;; tramp-sh.el ends here