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