1 ;;; tramp-sh.el --- Tramp access functions for (s)sh-like connections
3 ;; Copyright (C) 1998-2012 Free Software Foundation, Inc.
5 ;; (copyright statements below in code to be updated with the above notice)
7 ;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
8 ;; Michael Albinus <michael.albinus@gmx.de>
9 ;; Keywords: comm, processes
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 (eval-when-compile (require 'cl)) ; ignore-errors
32 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
33 ;; not sure at all that this is the right way to do it, but let's hope
34 ;; it works for now, and wait for a guru to point out the Right Way to
37 ;; (unless (fboundp 'dired-insert-set-properties)
38 ;; (fset 'dired-insert-set-properties 'ignore)))
39 ;; Gerd suggests this:
40 (eval-when-compile (require 'dired))
41 ;; Note that dired is required at run-time, too, when it is needed.
42 ;; It is only needed on XEmacs for the function
43 ;; `dired-insert-set-properties'.
45 (defcustom tramp-inline-compress-start-size 4096
46 "The minimum size of compressing where inline transfer.
47 When inline transfer, compress transferred data of file
48 whose size is this value or above (up to `tramp-copy-size-limit').
49 If it is nil, no compression at all will be applied."
51 :type '(choice (const nil) integer))
53 (defcustom tramp-copy-size-limit 10240
54 "The maximum file size where inline copying is preferred over an \
56 If it is nil, out-of-the-band copy will be used without a check."
58 :type '(choice (const nil) integer))
61 (defcustom tramp-terminal-type "dumb"
62 "Value of TERM environment variable for logging in to remote host.
63 Because Tramp wants to parse the output of the remote shell, it is easily
64 confused by ANSI color escape sequences and suchlike. Often, shell init
65 files conditionalize this setup based on the TERM environment variable."
69 (defconst tramp-color-escape-sequence-regexp "\e[[;0-9]+m"
70 "Escape sequences produced by the \"ls\" command.")
72 ;; ksh on OpenBSD 4.5 requires that $PS1 contains a `#' character for
73 ;; root users. It uses the `$' character for other users. In order
74 ;; to guarantee a proper prompt, we use "#$ " for the prompt.
76 (defvar tramp-end-of-output
79 (md5 (concat (prin1-to-string process-environment) (current-time-string))))
80 "String used to recognize end of output.
81 The '$' character at the end is quoted; the string cannot be
82 detected as prompt when being sent on echoing hosts, therefore.")
85 (defconst tramp-initial-end-of-output "#$ "
86 "Prompt when establishing a connection.")
88 ;; Initialize `tramp-methods' with the supported methods.
90 (add-to-list 'tramp-methods
92 (tramp-login-program "rsh")
93 (tramp-login-args (("%h") ("-l" "%u")))
94 (tramp-remote-shell "/bin/sh")
95 (tramp-remote-shell-args ("-c"))
96 (tramp-copy-program "rcp")
97 (tramp-copy-args (("-p" "%k") ("-r")))
98 (tramp-copy-keep-date t)
99 (tramp-copy-recursive t)))
101 (add-to-list 'tramp-methods
103 (tramp-login-program "remsh")
104 (tramp-login-args (("%h") ("-l" "%u")))
105 (tramp-remote-shell "/bin/sh")
106 (tramp-remote-shell-args ("-c"))
107 (tramp-copy-program "rcp")
108 (tramp-copy-args (("-p" "%k")))
109 (tramp-copy-keep-date t)))
111 (add-to-list 'tramp-methods
113 (tramp-login-program "ssh")
114 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
115 (tramp-async-args (("-q")))
116 (tramp-remote-shell "/bin/sh")
117 (tramp-remote-shell-args ("-c"))
118 (tramp-copy-program "scp")
119 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r")))
120 (tramp-copy-keep-date t)
121 (tramp-copy-recursive t)
122 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
123 ("-o" "UserKnownHostsFile=/dev/null")
124 ("-o" "StrictHostKeyChecking=no")))
125 (tramp-default-port 22)))
127 (add-to-list 'tramp-methods
129 (tramp-login-program "ssh")
130 (tramp-login-args (("-l" "%u") ("-p" "%p")
131 ("-1") ("-e" "none") ("%h")))
132 (tramp-async-args (("-q")))
133 (tramp-remote-shell "/bin/sh")
134 (tramp-remote-shell-args ("-c"))
135 (tramp-copy-program "scp")
136 (tramp-copy-args (("-1") ("-P" "%p") ("-p" "%k") ("-q") ("-r")))
137 (tramp-copy-keep-date t)
138 (tramp-copy-recursive t)
139 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
140 ("-o" "UserKnownHostsFile=/dev/null")
141 ("-o" "StrictHostKeyChecking=no")))
142 (tramp-default-port 22)))
144 (add-to-list 'tramp-methods
146 (tramp-login-program "ssh")
147 (tramp-login-args (("-l" "%u") ("-p" "%p")
148 ("-2") ("-e" "none") ("%h")))
149 (tramp-async-args (("-q")))
150 (tramp-remote-shell "/bin/sh")
151 (tramp-remote-shell-args ("-c"))
152 (tramp-copy-program "scp")
153 (tramp-copy-args (("-2") ("-P" "%p") ("-p" "%k") ("-q") ("-r")))
154 (tramp-copy-keep-date t)
155 (tramp-copy-recursive t)
156 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
157 ("-o" "UserKnownHostsFile=/dev/null")
158 ("-o" "StrictHostKeyChecking=no")))
159 (tramp-default-port 22)))
161 (add-to-list 'tramp-methods
163 (tramp-login-program "ssh")
164 (tramp-login-args (("-l" "%u") ("-p" "%p")
165 ("-o" "ControlPath=%t.%%r@%%h:%%p")
166 ("-o" "ControlMaster=yes")
167 ("-e" "none") ("%h")))
168 (tramp-async-args (("-q")))
169 (tramp-remote-shell "/bin/sh")
170 (tramp-remote-shell-args ("-c"))
171 (tramp-copy-program "scp")
172 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r")
173 ("-o" "ControlPath=%t.%%r@%%h:%%p")
174 ("-o" "ControlMaster=auto")))
175 (tramp-copy-keep-date t)
176 (tramp-copy-recursive t)
177 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
178 ("-o" "UserKnownHostsFile=/dev/null")
179 ("-o" "StrictHostKeyChecking=no")))
180 (tramp-default-port 22)))
182 (add-to-list 'tramp-methods
184 (tramp-login-program "ssh")
185 (tramp-login-args (("-l" "%u") ("-p" "%p")
186 ("-e" "none") ("-t" "-t")
188 (tramp-async-args (("-q")))
189 (tramp-remote-shell "/bin/sh")
190 (tramp-remote-shell-args ("-c"))
191 (tramp-copy-program "scp")
192 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q") ("-r")))
193 (tramp-copy-keep-date t)
194 (tramp-copy-recursive t)
195 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
196 ("-o" "UserKnownHostsFile=/dev/null")
197 ("-o" "StrictHostKeyChecking=no")))
198 (tramp-default-port 22)))
200 (add-to-list 'tramp-methods
202 (tramp-login-program "ssh")
203 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
204 (tramp-async-args (("-q")))
205 (tramp-remote-shell "/bin/sh")
206 (tramp-remote-shell-args ("-c"))
207 (tramp-copy-program "sftp")))
209 (add-to-list 'tramp-methods
211 (tramp-login-program "ssh")
212 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
213 (tramp-async-args (("-q")))
214 (tramp-remote-shell "/bin/sh")
215 (tramp-remote-shell-args ("-c"))
216 (tramp-copy-program "rsync")
217 (tramp-copy-args (("-e" "ssh") ("-t" "%k") ("-r")))
218 (tramp-copy-keep-date t)
219 (tramp-copy-keep-tmpfile t)
220 (tramp-copy-recursive t)))
222 (add-to-list 'tramp-methods
224 (tramp-login-program "ssh")
225 (tramp-login-args (("-l" "%u") ("-p" "%p")
226 ("-o" "ControlPath=%t.%%r@%%h:%%p")
227 ("-o" "ControlMaster=yes")
228 ("-e" "none") ("%h")))
229 (tramp-async-args (("-q")))
230 (tramp-remote-shell "/bin/sh")
231 (tramp-remote-shell-args ("-c"))
232 (tramp-copy-program "rsync")
233 (tramp-copy-args (("-t" "%k") ("-r")))
234 (tramp-copy-env (("RSYNC_RSH")
237 " -o ControlPath=%t.%%r@%%h:%%p"
238 " -o ControlMaster=auto"))))
239 (tramp-copy-keep-date t)
240 (tramp-copy-keep-tmpfile t)
241 (tramp-copy-recursive t)))
243 (add-to-list 'tramp-methods
245 (tramp-login-program "rsh")
246 (tramp-login-args (("%h") ("-l" "%u")))
247 (tramp-remote-shell "/bin/sh")
248 (tramp-remote-shell-args ("-c"))))
250 (add-to-list 'tramp-methods
252 (tramp-login-program "remsh")
253 (tramp-login-args (("%h") ("-l" "%u")))
254 (tramp-remote-shell "/bin/sh")
255 (tramp-remote-shell-args ("-c"))))
257 (add-to-list 'tramp-methods
259 (tramp-login-program "ssh")
260 (tramp-login-args (("-l" "%u") ("-p" "%p") ("-e" "none") ("%h")))
261 (tramp-async-args (("-q")))
262 (tramp-remote-shell "/bin/sh")
263 (tramp-remote-shell-args ("-c"))
264 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
265 ("-o" "UserKnownHostsFile=/dev/null")
266 ("-o" "StrictHostKeyChecking=no")))
267 (tramp-default-port 22)))
269 (add-to-list 'tramp-methods
271 (tramp-login-program "ssh")
272 (tramp-login-args (("-l" "%u") ("-p" "%p")
273 ("-1") ("-e" "none") ("%h")))
274 (tramp-async-args (("-q")))
275 (tramp-remote-shell "/bin/sh")
276 (tramp-remote-shell-args ("-c"))
277 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
278 ("-o" "UserKnownHostsFile=/dev/null")
279 ("-o" "StrictHostKeyChecking=no")))
280 (tramp-default-port 22)))
282 (add-to-list 'tramp-methods
284 (tramp-login-program "ssh")
285 (tramp-login-args (("-l" "%u") ("-p" "%p")
286 ("-2") ("-e" "none") ("%h")))
287 (tramp-async-args (("-q")))
288 (tramp-remote-shell "/bin/sh")
289 (tramp-remote-shell-args ("-c"))
290 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
291 ("-o" "UserKnownHostsFile=/dev/null")
292 ("-o" "StrictHostKeyChecking=no")))
293 (tramp-default-port 22)))
295 (add-to-list 'tramp-methods
297 (tramp-login-program "ssh")
298 (tramp-login-args (("-l" "%u") ("-p" "%p")
299 ("-e" "none") ("-t" "-t")
301 (tramp-async-args (("-q")))
302 (tramp-remote-shell "/bin/sh")
303 (tramp-remote-shell-args ("-c"))
304 (tramp-gw-args (("-o" "GlobalKnownHostsFile=/dev/null")
305 ("-o" "UserKnownHostsFile=/dev/null")
306 ("-o" "StrictHostKeyChecking=no")))
307 (tramp-default-port 22)))
309 (add-to-list 'tramp-methods
311 (tramp-login-program "telnet")
312 (tramp-login-args (("%h") ("%p")))
313 (tramp-remote-shell "/bin/sh")
314 (tramp-remote-shell-args ("-c"))
315 (tramp-default-port 23)))
317 (add-to-list 'tramp-methods
319 (tramp-login-program "su")
320 (tramp-login-args (("-") ("%u")))
321 (tramp-remote-shell "/bin/sh")
322 (tramp-remote-shell-args ("-c"))))
324 (add-to-list 'tramp-methods
326 (tramp-login-program "sudo")
327 (tramp-login-args (("-u" "%u") ("-s") ("-H") ("-p" "Password:")))
328 (tramp-remote-shell "/bin/sh")
329 (tramp-remote-shell-args ("-c"))))
331 (add-to-list 'tramp-methods
333 (tramp-login-program "ksu")
334 (tramp-login-args (("%u") ("-q")))
335 (tramp-remote-shell "/bin/sh")
336 (tramp-remote-shell-args ("-c"))))
338 (add-to-list 'tramp-methods
340 (tramp-login-program "krlogin")
341 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
342 (tramp-remote-shell "/bin/sh")
343 (tramp-remote-shell-args ("-c"))))
345 (add-to-list 'tramp-methods
347 (tramp-login-program "plink")
348 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
349 (tramp-remote-shell "/bin/sh")
350 (tramp-remote-shell-args ("-c"))
351 (tramp-default-port 22)))
353 (add-to-list 'tramp-methods
355 (tramp-login-program "plink")
356 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-1" "-ssh") ("%h")))
357 (tramp-remote-shell "/bin/sh")
358 (tramp-remote-shell-args ("-c"))
359 (tramp-default-port 22)))
361 (add-to-list 'tramp-methods
363 (tramp-login-program "plink")
364 ;; ("%h") must be a single element, see
365 ;; `tramp-compute-multi-hops'.
366 (tramp-login-args (("-load") ("%h") ("-t")
368 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
370 tramp-initial-end-of-output))
372 (tramp-remote-shell "/bin/sh")
373 (tramp-remote-shell-args ("-c"))))
375 (add-to-list 'tramp-methods
377 (tramp-login-program "plink")
378 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
379 (tramp-remote-shell "/bin/sh")
380 (tramp-remote-shell-args ("-c"))
381 (tramp-copy-program "pscp")
382 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-scp") ("-p" "%k")
384 (tramp-copy-keep-date t)
385 (tramp-copy-recursive t)
386 (tramp-default-port 22)))
388 (add-to-list 'tramp-methods
390 (tramp-login-program "plink")
391 (tramp-login-args (("-l" "%u") ("-P" "%p") ("-ssh") ("%h")))
392 (tramp-remote-shell "/bin/sh")
393 (tramp-remote-shell-args ("-c"))
394 (tramp-copy-program "pscp")
395 (tramp-copy-args (("-l" "%u") ("-P" "%p") ("-sftp") ("-p" "%k")
397 (tramp-copy-keep-date t)
398 (tramp-copy-recursive t)))
400 (add-to-list 'tramp-methods
402 (tramp-login-program "fsh")
403 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
404 (tramp-remote-shell "/bin/sh")
405 (tramp-remote-shell-args ("-i") ("-c"))
406 (tramp-copy-program "fcp")
407 (tramp-copy-args (("-p" "%k")))
408 (tramp-copy-keep-date t)))
411 (add-to-list 'tramp-default-method-alist
412 `(,tramp-local-host-regexp "\\`root\\'" "su"))
415 (add-to-list 'tramp-default-user-alist
416 `(,(concat "\\`" (regexp-opt '("su" "sudo" "ksu")) "\\'")
418 ;; Do not add "ssh" based methods, otherwise ~/.ssh/config would be ignored.
419 ;; Do not add "plink" based methods, they ask interactively for the user.
421 (add-to-list 'tramp-default-user-alist
424 (regexp-opt '("rcp" "remcp" "rsh" "telnet" "krlogin" "fcp"))
426 nil ,(user-login-name)))
429 (defconst tramp-completion-function-alist-rsh
430 '((tramp-parse-rhosts "/etc/hosts.equiv")
431 (tramp-parse-rhosts "~/.rhosts"))
432 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
435 (defconst tramp-completion-function-alist-ssh
436 '((tramp-parse-rhosts "/etc/hosts.equiv")
437 (tramp-parse-rhosts "/etc/shosts.equiv")
438 (tramp-parse-shosts "/etc/ssh_known_hosts")
439 (tramp-parse-sconfig "/etc/ssh_config")
440 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
441 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
442 (tramp-parse-rhosts "~/.rhosts")
443 (tramp-parse-rhosts "~/.shosts")
444 (tramp-parse-shosts "~/.ssh/known_hosts")
445 (tramp-parse-sconfig "~/.ssh/config")
446 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
447 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
448 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
451 (defconst tramp-completion-function-alist-telnet
452 '((tramp-parse-hosts "/etc/hosts"))
453 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
456 (defconst tramp-completion-function-alist-su
457 '((tramp-parse-passwd "/etc/passwd"))
458 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
461 (defconst tramp-completion-function-alist-putty
463 ,(if (memq system-type '(windows-nt))
464 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"
465 "~/.putty/sessions")))
466 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty sessions.")
469 (eval-after-load 'tramp
471 (tramp-set-completion-function "rcp" tramp-completion-function-alist-rsh)
472 (tramp-set-completion-function "remcp" tramp-completion-function-alist-rsh)
473 (tramp-set-completion-function "scp" tramp-completion-function-alist-ssh)
474 (tramp-set-completion-function "scp1" tramp-completion-function-alist-ssh)
475 (tramp-set-completion-function "scp2" tramp-completion-function-alist-ssh)
476 (tramp-set-completion-function "scpc" tramp-completion-function-alist-ssh)
477 (tramp-set-completion-function "scpx" tramp-completion-function-alist-ssh)
478 (tramp-set-completion-function "sftp" tramp-completion-function-alist-ssh)
479 (tramp-set-completion-function "rsync" tramp-completion-function-alist-ssh)
480 (tramp-set-completion-function
481 "rsyncc" tramp-completion-function-alist-ssh)
482 (tramp-set-completion-function "rsh" tramp-completion-function-alist-rsh)
483 (tramp-set-completion-function "remsh" tramp-completion-function-alist-rsh)
484 (tramp-set-completion-function "ssh" tramp-completion-function-alist-ssh)
485 (tramp-set-completion-function "ssh1" tramp-completion-function-alist-ssh)
486 (tramp-set-completion-function "ssh2" tramp-completion-function-alist-ssh)
487 (tramp-set-completion-function
488 "ssh1_old" tramp-completion-function-alist-ssh)
489 (tramp-set-completion-function
490 "ssh2_old" tramp-completion-function-alist-ssh)
491 (tramp-set-completion-function "sshx" tramp-completion-function-alist-ssh)
492 (tramp-set-completion-function
493 "telnet" tramp-completion-function-alist-telnet)
494 (tramp-set-completion-function "su" tramp-completion-function-alist-su)
495 (tramp-set-completion-function "sudo" tramp-completion-function-alist-su)
496 (tramp-set-completion-function "ksu" tramp-completion-function-alist-su)
497 (tramp-set-completion-function
498 "krlogin" tramp-completion-function-alist-rsh)
499 (tramp-set-completion-function "plink" tramp-completion-function-alist-ssh)
500 (tramp-set-completion-function
501 "plink1" tramp-completion-function-alist-ssh)
502 (tramp-set-completion-function
503 "plinkx" tramp-completion-function-alist-putty)
504 (tramp-set-completion-function "pscp" tramp-completion-function-alist-ssh)
505 (tramp-set-completion-function "fcp" tramp-completion-function-alist-ssh)))
507 ;; "getconf PATH" yields:
508 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
509 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
510 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
511 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
514 (defcustom tramp-remote-path
515 '(tramp-default-remote-path "/bin" "/usr/bin" "/sbin" "/usr/sbin"
516 "/usr/local/bin" "/usr/local/sbin" "/local/bin" "/local/freeware/bin"
517 "/local/gnu/bin" "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin"
518 "/opt/bin" "/opt/sbin" "/opt/local/bin")
519 "List of directories to search for executables on remote host.
520 For every remote host, this variable will be set buffer local,
521 keeping the list of existing directories on that host.
523 You can use `~' in this list, but when searching for a shell which groks
524 tilde expansion, all directory names starting with `~' will be ignored.
526 `Default Directories' represent the list of directories given by
527 the command \"getconf PATH\". It is recommended to use this
528 entry on top of this list, because these are the default
529 directories for POSIX compatible commands. On remote hosts which
530 do not offer the getconf command (like cygwin), the value
531 \"/bin:/usr/bin\" is used instead of.
533 `Private Directories' are the settings of the $PATH environment,
534 as given in your `~/.profile'."
536 :type '(repeat (choice
537 (const :tag "Default Directories" tramp-default-remote-path)
538 (const :tag "Private Directories" tramp-own-remote-path)
539 (string :tag "Directory"))))
541 (defcustom tramp-remote-process-environment
542 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "LC_ALL=C"
543 ,(format "TERM=%s" tramp-terminal-type)
544 "EMACS=t" ;; Deprecated.
545 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version)
546 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH=" "PAGER=\"\""
547 "autocorrect=" "correct=")
548 "List of environment variables to be set on the remote host.
550 Each element should be a string of the form ENVVARNAME=VALUE. An
551 entry ENVVARNAME= disables the corresponding environment variable,
552 which might have been set in the init files like ~/.profile.
554 Special handling is applied to the PATH environment, which should
555 not be set here. Instead, it should be set via `tramp-remote-path'."
557 :type '(repeat string))
559 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
560 "Alist specifying extra arguments to pass to the remote shell.
561 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
562 matching the shell file name and ARGS is a string specifying the
565 This variable is only used when Tramp needs to start up another shell
566 for tilde expansion. The extra arguments should typically prevent the
567 shell from reading its init file."
569 ;; This might be the wrong way to test whether the widget type
570 ;; `alist' is available. Who knows the right way to test it?
571 :type (if (get 'alist 'widget-type)
572 '(alist :key-type string :value-type string)
573 '(repeat (cons string string))))
575 (defconst tramp-actions-before-shell
576 '((tramp-login-prompt-regexp tramp-action-login)
577 (tramp-password-prompt-regexp tramp-action-password)
578 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
579 (shell-prompt-pattern tramp-action-succeed)
580 (tramp-shell-prompt-pattern tramp-action-succeed)
581 (tramp-yesno-prompt-regexp tramp-action-yesno)
582 (tramp-yn-prompt-regexp tramp-action-yn)
583 (tramp-terminal-prompt-regexp tramp-action-terminal)
584 (tramp-process-alive-regexp tramp-action-process-alive))
585 "List of pattern/action pairs.
586 Whenever a pattern matches, the corresponding action is performed.
587 Each item looks like (PATTERN ACTION).
589 The PATTERN should be a symbol, a variable. The value of this
590 variable gives the regular expression to search for. Note that the
591 regexp must match at the end of the buffer, \"\\'\" is implicitly
594 The ACTION should also be a symbol, but a function. When the
595 corresponding PATTERN matches, the ACTION function is called.")
597 (defconst tramp-actions-copy-out-of-band
598 '((tramp-password-prompt-regexp tramp-action-password)
599 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
600 (tramp-copy-failed-regexp tramp-action-permission-denied)
601 (tramp-process-alive-regexp tramp-action-out-of-band))
602 "List of pattern/action pairs.
603 This list is used for copying/renaming with out-of-band methods.
605 See `tramp-actions-before-shell' for more info.")
607 (defconst tramp-uudecode
608 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
611 "Shell function to implement `uudecode' to standard output.
612 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
613 for this or `uudecode -p', but some systems don't, and for them
614 we have this shell function.")
616 (defconst tramp-perl-file-truename
619 use Cwd \"realpath\";
622 my ($volume, @dirs) = @_;
623 my $real = realpath(File::Spec->catpath(
624 $volume, File::Spec->catdir(@dirs), \"\"));
626 my ($vol, $dir) = File::Spec->splitpath($real, 1);
627 return ($vol, File::Spec->splitdir($dir));
630 my $last = pop(@dirs);
631 ($volume, @dirs) = recursive($volume, @dirs);
633 return ($volume, @dirs);
637 $result = realpath($ARGV[0]);
639 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
640 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
642 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
645 if ($ARGV[0] =~ /\\/$/) {
646 $result = $result . \"/\";
649 print \"\\\"$result\\\"\\n\";
650 ' \"$1\" 2>/dev/null"
651 "Perl script to produce output suitable for use with `file-truename'
652 on the remote file system.
653 Escape sequence %s is replaced with name of Perl binary.
654 This string is passed to `format', so percent characters need to be doubled.")
656 (defconst tramp-perl-file-name-all-completions
666 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
667 @files = readdir(d); closedir(d);
668 foreach $f (@files) {
669 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
670 if (-d \"$ARGV[0]/$f\") {
679 ' \"$1\" \"$2\" \"$3\" 2>/dev/null"
680 "Perl script to produce output suitable for use with
681 `file-name-all-completions' on the remote file system. Escape
682 sequence %s is replaced with name of Perl binary. This string is
683 passed to `format', so percent characters need to be doubled.")
685 ;; Perl script to implement `file-attributes' in a Lisp `read'able
686 ;; output. If you are hacking on this, note that you get *no* output
687 ;; unless this spits out a complete line, including the '\n' at the
689 ;; The device number is returned as "-1", because there will be a virtual
690 ;; device number set in `tramp-sh-handle-file-attributes'.
691 (defconst tramp-perl-file-attributes
693 @stat = lstat($ARGV[0]);
698 if (($stat[2] & 0170000) == 0120000)
700 $type = readlink($ARGV[0]);
701 $type = \"\\\"$type\\\"\";
703 elsif (($stat[2] & 0170000) == 040000)
711 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
712 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
714 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
719 $stat[8] >> 16 & 0xffff,
721 $stat[9] >> 16 & 0xffff,
723 $stat[10] >> 16 & 0xffff,
727 $stat[1] >> 16 & 0xffff,
729 );' \"$1\" \"$2\" 2>/dev/null"
730 "Perl script to produce output suitable for use with `file-attributes'
731 on the remote file system.
732 Escape sequence %s is replaced with name of Perl binary.
733 This string is passed to `format', so percent characters need to be doubled.")
735 (defconst tramp-perl-directory-files-and-attributes
737 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
738 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
739 @list = readdir(DIR);
743 for($i = 0; $i < $n; $i++)
745 $filename = $list[$i];
746 @stat = lstat($filename);
747 if (($stat[2] & 0170000) == 0120000)
749 $type = readlink($filename);
750 $type = \"\\\"$type\\\"\";
752 elsif (($stat[2] & 0170000) == 040000)
760 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
761 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
763 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
769 $stat[8] >> 16 & 0xffff,
771 $stat[9] >> 16 & 0xffff,
773 $stat[10] >> 16 & 0xffff,
777 $stat[1] >> 16 & 0xffff,
779 $stat[0] >> 16 & 0xffff,
782 printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
783 "Perl script implementing `directory-files-attributes' as Lisp `read'able
785 Escape sequence %s is replaced with name of Perl binary.
786 This string is passed to `format', so percent characters need to be doubled.")
788 ;; These two use base64 encoding.
789 (defconst tramp-perl-encode-with-module
790 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
791 "Perl program to use for encoding a file.
792 Escape sequence %s is replaced with name of Perl binary.
793 This string is passed to `format', so percent characters need to be doubled.
794 This implementation requires the MIME::Base64 Perl module to be installed
795 on the remote host.")
797 (defconst tramp-perl-decode-with-module
798 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
799 "Perl program to use for decoding a file.
800 Escape sequence %s is replaced with name of Perl binary.
801 This string is passed to `format', so percent characters need to be doubled.
802 This implementation requires the MIME::Base64 Perl module to be installed
803 on the remote host.")
805 (defconst tramp-perl-encode
807 # This script contributed by Juanma Barranquero <lektu@terra.es>.
808 # Copyright (C) 2002-2012 Free Software Foundation, Inc.
813 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
814 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
818 # We read in chunks of 54 bytes, to generate output lines
819 # of 72 chars (plus end of line)
820 while (read STDIN, $data, 54) {
823 # Only for the last chunk, and only if did not fill the last three-byte packet
825 my $mod = length($data) %% 3;
826 $pad = q(=) x (3 - $mod) if $mod;
829 # Not the fastest method, but it is simple: unpack to binary string, split
830 # by groups of 6 bits and convert back from binary to byte; then map into
831 # the translation table
835 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
839 "Perl program to use for encoding a file.
840 Escape sequence %s is replaced with name of Perl binary.
841 This string is passed to `format', so percent characters need to be doubled.")
843 (defconst tramp-perl-decode
845 # This script contributed by Juanma Barranquero <lektu@terra.es>.
846 # Copyright (C) 2002-2012 Free Software Foundation, Inc.
851 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
852 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
855 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
859 # We are going to accumulate into $pending to accept any line length
860 # (we do not check they are <= 76 chars as the RFC says)
863 while (my $data = <STDIN>) {
866 # If we find one or two =, we have reached the end and
867 # any following data is to be discarded
868 my $finished = $data =~ s/(==?).*/$1/;
871 my $len = length($pending);
872 my $chunk = substr($pending, 0, $len & ~3);
873 $pending = substr($pending, $len & ~3 + 1);
875 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
876 # split in 8-bit chunks and convert back to char.
879 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
883 "Perl program to use for decoding a file.
884 Escape sequence %s is replaced with name of Perl binary.
885 This string is passed to `format', so percent characters need to be doubled.")
887 (defconst tramp-vc-registered-read-file-names
890 if %s \"$file\"; then
891 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
893 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
895 if %s \"$file\"; then
896 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
898 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
902 "Script to check existence of VC related files.
903 It must be send formatted with two strings; the tests for file
904 existence, and file readability. Input shall be read via
905 here-document, otherwise the command could exceed maximum length
908 (defconst tramp-file-mode-type-map
909 '((0 . "-") ; Normal file (SVID-v2 and XPG2)
911 (2 . "c") ; character device
912 (3 . "m") ; multiplexed character device (v7)
913 (4 . "d") ; directory
914 (5 . "?") ; Named special file (XENIX)
915 (6 . "b") ; block device
916 (7 . "?") ; multiplexed block device (v7)
917 (8 . "-") ; regular file
918 (9 . "n") ; network special file (HP-UX)
920 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
922 (13 . "D") ; door special (Solaris)
923 (14 . "w")) ; whiteout (BSD)
924 "A list of file types returned from the `stat' system call.
925 This is used to map a mode number to a permission string.")
927 ;; New handlers should be added here. The following operations can be
928 ;; handled using the normal primitives: file-name-sans-versions,
930 (defconst tramp-sh-file-name-handler-alist
931 '((load . tramp-handle-load)
932 (make-symbolic-link . tramp-sh-handle-make-symbolic-link)
933 (file-name-as-directory . tramp-handle-file-name-as-directory)
934 (file-name-directory . tramp-handle-file-name-directory)
935 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
936 (file-truename . tramp-sh-handle-file-truename)
937 (file-exists-p . tramp-sh-handle-file-exists-p)
938 (file-directory-p . tramp-sh-handle-file-directory-p)
939 (file-executable-p . tramp-sh-handle-file-executable-p)
940 (file-readable-p . tramp-sh-handle-file-readable-p)
941 (file-regular-p . tramp-handle-file-regular-p)
942 (file-symlink-p . tramp-handle-file-symlink-p)
943 (file-writable-p . tramp-sh-handle-file-writable-p)
944 (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p)
945 (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p)
946 (file-attributes . tramp-sh-handle-file-attributes)
947 (file-modes . tramp-handle-file-modes)
948 (directory-files . tramp-handle-directory-files)
949 (directory-files-and-attributes
950 . tramp-sh-handle-directory-files-and-attributes)
951 (file-name-all-completions . tramp-sh-handle-file-name-all-completions)
952 (file-name-completion . tramp-handle-file-name-completion)
953 (add-name-to-file . tramp-sh-handle-add-name-to-file)
954 (copy-file . tramp-sh-handle-copy-file)
955 (copy-directory . tramp-sh-handle-copy-directory)
956 (rename-file . tramp-sh-handle-rename-file)
957 (set-file-modes . tramp-sh-handle-set-file-modes)
958 (set-file-times . tramp-sh-handle-set-file-times)
959 (make-directory . tramp-sh-handle-make-directory)
960 (delete-directory . tramp-sh-handle-delete-directory)
961 (delete-file . tramp-sh-handle-delete-file)
962 (directory-file-name . tramp-handle-directory-file-name)
963 ;; `executable-find' is not official yet.
964 (executable-find . tramp-sh-handle-executable-find)
965 (start-file-process . tramp-sh-handle-start-file-process)
966 (process-file . tramp-sh-handle-process-file)
967 (shell-command . tramp-handle-shell-command)
968 (insert-directory . tramp-sh-handle-insert-directory)
969 (expand-file-name . tramp-sh-handle-expand-file-name)
970 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
971 (file-local-copy . tramp-sh-handle-file-local-copy)
972 (file-remote-p . tramp-handle-file-remote-p)
973 (insert-file-contents . tramp-handle-insert-file-contents)
974 (insert-file-contents-literally
975 . tramp-sh-handle-insert-file-contents-literally)
976 (write-region . tramp-sh-handle-write-region)
977 (find-backup-file-name . tramp-handle-find-backup-file-name)
978 (make-auto-save-file-name . tramp-sh-handle-make-auto-save-file-name)
979 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
980 (dired-compress-file . tramp-sh-handle-dired-compress-file)
981 (dired-recursive-delete-directory
982 . tramp-sh-handle-dired-recursive-delete-directory)
983 (dired-uncache . tramp-handle-dired-uncache)
984 (set-visited-file-modtime . tramp-sh-handle-set-visited-file-modtime)
985 (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime)
986 (file-selinux-context . tramp-sh-handle-file-selinux-context)
987 (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context)
988 (vc-registered . tramp-sh-handle-vc-registered))
989 "Alist of handler functions.
990 Operations not mentioned here will be handled by the normal Emacs functions.")
992 ;; This must be the last entry, because `identity' always matches.
994 (add-to-list 'tramp-foreign-file-name-handler-alist
995 '(identity . tramp-sh-file-name-handler) 'append)
997 ;;; File Name Handler Functions:
999 (defun tramp-sh-handle-make-symbolic-link
1000 (filename linkname &optional ok-if-already-exists)
1001 "Like `make-symbolic-link' for Tramp files.
1002 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1003 the symlink. If LINKNAME is a Tramp file, only the localname component is
1004 used as the target of the symlink.
1006 If LINKNAME is a Tramp file and the localname component is relative, then
1007 it is expanded first, before the localname component is taken. Note that
1008 this can give surprising results if the user/host for the source and
1009 target of the symlink differ."
1010 (with-parsed-tramp-file-name linkname l
1011 (let ((ln (tramp-get-remote-ln l))
1012 (cwd (tramp-run-real-handler
1013 'file-name-directory (list l-localname))))
1017 "Making a symbolic link. ln(1) does not exist on the remote host."))
1019 ;; Do the 'confirm if exists' thing.
1020 (when (file-exists-p linkname)
1022 (if (or (null ok-if-already-exists) ; not allowed to exist
1023 (and (numberp ok-if-already-exists)
1026 "File %s already exists; make it a link anyway? "
1029 l 'file-already-exists "File %s already exists" l-localname)
1030 (delete-file linkname)))
1032 ;; If FILENAME is a Tramp name, use just the localname component.
1033 (when (tramp-tramp-file-p filename)
1035 (tramp-file-name-localname
1036 (tramp-dissect-file-name (expand-file-name filename)))))
1038 (tramp-flush-file-property l (file-name-directory l-localname))
1039 (tramp-flush-file-property l l-localname)
1041 ;; Right, they are on the same host, regardless of user, method,
1042 ;; etc. We now make the link on the remote machine. This will
1043 ;; occur as the user that FILENAME belongs to.
1044 (tramp-send-command-and-check
1047 "cd %s && %s -sf %s %s"
1048 (tramp-shell-quote-argument cwd)
1050 (tramp-shell-quote-argument filename)
1051 (tramp-shell-quote-argument l-localname))
1054 (defun tramp-sh-handle-file-truename (filename &optional counter prev-dirs)
1055 "Like `file-truename' for Tramp files."
1056 (with-parsed-tramp-file-name (expand-file-name filename) nil
1057 (tramp-make-tramp-file-name method user host
1058 (with-tramp-file-property v localname "file-truename"
1059 (let ((result nil)) ; result steps in reverse order
1060 (tramp-message v 4 "Finding true name for `%s'" filename)
1062 ;; Use GNU readlink --canonicalize-missing where available.
1063 ((tramp-get-remote-readlink v)
1065 (tramp-send-command-and-read
1067 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
1068 (tramp-get-remote-readlink v)
1069 (tramp-shell-quote-argument localname)))))
1071 ;; Use Perl implementation.
1072 ((and (tramp-get-remote-perl v)
1073 (tramp-get-connection-property v "perl-file-spec" nil)
1074 (tramp-get-connection-property v "perl-cwd-realpath" nil))
1075 (tramp-maybe-send-script
1076 v tramp-perl-file-truename "tramp_perl_file_truename")
1078 (tramp-send-command-and-read
1080 (format "tramp_perl_file_truename %s"
1081 (tramp-shell-quote-argument localname)))))
1083 ;; Do it yourself. We bind `directory-sep-char' here for
1084 ;; XEmacs on Windows, which would otherwise use backslash.
1085 (t (let* ((directory-sep-char ?/)
1086 (steps (tramp-compat-split-string localname "/"))
1087 (localnamedir (tramp-run-real-handler
1088 'file-name-as-directory (list localname)))
1089 (is-dir (string= localname localnamedir))
1092 ;; Don't make the following value larger than
1093 ;; necessary. People expect an error message in
1094 ;; a timely fashion when something is wrong;
1095 ;; otherwise they might think that Emacs is hung.
1096 ;; Of course, correctness has to come first.
1099 (while (and steps (< numchase numchase-limit))
1100 (setq thisstep (pop steps))
1103 (mapconcat 'identity
1104 (append '("") (reverse result) (list thisstep))
1106 (setq symlink-target
1107 (nth 0 (file-attributes
1108 (tramp-make-tramp-file-name
1110 (mapconcat 'identity
1115 (cond ((string= "." thisstep)
1116 (tramp-message v 5 "Ignoring step `.'"))
1117 ((string= ".." thisstep)
1118 (tramp-message v 5 "Processing step `..'")
1120 ((stringp symlink-target)
1121 ;; It's a symlink, follow it.
1123 v 5 "Follow symlink to %s" symlink-target)
1124 (setq numchase (1+ numchase))
1125 (when (file-name-absolute-p symlink-target)
1127 ;; If the symlink was absolute, we'll get a
1128 ;; string like "/user@host:/some/target";
1129 ;; extract the "/some/target" part from it.
1130 (when (tramp-tramp-file-p symlink-target)
1131 (unless (tramp-equal-remote filename symlink-target)
1134 "Symlink target `%s' on wrong host"
1136 (setq symlink-target localname))
1138 (append (tramp-compat-split-string
1143 (setq result (cons thisstep result)))))
1144 (when (>= numchase numchase-limit)
1147 "Maximum number (%d) of symlinks exceeded" numchase-limit))
1148 (setq result (reverse result))
1149 ;; Combine list to form string.
1152 (mapconcat 'identity (cons "" result) "/")
1155 (or (string= "" result)
1156 (not (string= (substring result -1) "/"))))
1157 (setq result (concat result "/"))))))
1159 (tramp-message v 4 "True name of `%s' is `%s'" localname result)
1164 (defun tramp-sh-handle-file-exists-p (filename)
1165 "Like `file-exists-p' for Tramp files."
1166 (with-parsed-tramp-file-name filename nil
1167 (with-tramp-file-property v localname "file-exists-p"
1168 (or (not (null (tramp-get-file-property
1169 v localname "file-attributes-integer" nil)))
1170 (not (null (tramp-get-file-property
1171 v localname "file-attributes-string" nil)))
1172 (tramp-send-command-and-check
1176 (tramp-get-file-exists-command v)
1177 (tramp-shell-quote-argument localname)))))))
1179 (defun tramp-sh-handle-file-attributes (filename &optional id-format)
1180 "Like `file-attributes' for Tramp files."
1181 (unless id-format (setq id-format 'integer))
1182 ;; Don't modify `last-coding-system-used' by accident.
1183 (let ((last-coding-system-used last-coding-system-used))
1184 (with-parsed-tramp-file-name (expand-file-name filename) nil
1185 (with-tramp-file-property
1186 v localname (format "file-attributes-%s" id-format)
1188 (tramp-convert-file-attributes
1192 ((tramp-get-remote-stat v)
1193 (tramp-do-file-attributes-with-stat v localname id-format))
1194 ((tramp-get-remote-perl v)
1195 (tramp-do-file-attributes-with-perl v localname id-format))
1197 ;; The scripts could fail, for example with huge file size.
1198 (tramp-do-file-attributes-with-ls v localname id-format))))))))
1200 (defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
1201 "Implement `file-attributes' for Tramp files using the ls(1) command."
1203 res-inode res-filemodes res-numlinks
1204 res-uid res-gid res-size res-symlink-target)
1205 (tramp-message vec 5 "file attributes with ls: %s" localname)
1208 (format "(%s %s || %s -h %s) && %s %s %s"
1209 (tramp-get-file-exists-command vec)
1210 (tramp-shell-quote-argument localname)
1211 (tramp-get-test-command vec)
1212 (tramp-shell-quote-argument localname)
1213 (tramp-get-ls-command vec)
1214 (if (eq id-format 'integer) "-ildn" "-ild")
1215 (tramp-shell-quote-argument localname)))
1216 ;; parse `ls -l' output ...
1217 (with-current-buffer (tramp-get-buffer vec)
1218 (when (> (buffer-size) 0)
1219 (goto-char (point-min))
1223 (read (current-buffer))
1224 (invalid-read-syntax
1225 (when (and (equal (cadr err)
1226 "Integer constant overflow in reader")
1228 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1230 (let* ((big (read (substring (car (cddr err)) 0
1231 (match-beginning 1))))
1232 (small (read (match-string 1 (car (cddr err)))))
1233 (twiddle (/ small 65536)))
1234 (cons (+ big twiddle)
1235 (- small (* twiddle 65536))))))))
1236 ;; ... file mode flags
1237 (setq res-filemodes (symbol-name (read (current-buffer))))
1239 (setq res-numlinks (read (current-buffer)))
1241 (setq res-uid (read (current-buffer)))
1242 (setq res-gid (read (current-buffer)))
1243 (if (eq id-format 'integer)
1245 (unless (numberp res-uid) (setq res-uid -1))
1246 (unless (numberp res-gid) (setq res-gid -1)))
1248 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
1249 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
1251 (setq res-size (read (current-buffer)))
1252 ;; From the file modes, figure out other stuff.
1253 (setq symlinkp (eq ?l (aref res-filemodes 0)))
1254 (setq dirp (eq ?d (aref res-filemodes 0)))
1255 ;; if symlink, find out file name pointed to
1257 (search-forward "-> ")
1258 (setq res-symlink-target (buffer-substring (point) (point-at-eol))))
1259 ;; return data gathered
1261 ;; 0. t for directory, string (name linked to) for symbolic
1263 (or dirp res-symlink-target)
1264 ;; 1. Number of links to file.
1270 ;; 4. Last access time, as a list of integers. Normally this
1271 ;; would be in the same format as `current-time', but the
1272 ;; subseconds part is not currently implemented, and (0 0)
1273 ;; denotes an unknown time.
1274 ;; 5. Last modification time, likewise.
1275 ;; 6. Last status change time, likewise.
1276 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1277 ;; 7. Size in bytes (-1, if number is out of range).
1279 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1281 ;; 9. t if file's gid would change if file were deleted and
1282 ;; recreated. Will be set in `tramp-convert-file-attributes'
1284 ;; 10. inode number.
1286 ;; 11. Device number. Will be replaced by a virtual device number.
1290 (defun tramp-do-file-attributes-with-perl
1291 (vec localname &optional id-format)
1292 "Implement `file-attributes' for Tramp files using a Perl script."
1293 (tramp-message vec 5 "file attributes with perl: %s" localname)
1294 (tramp-maybe-send-script
1295 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
1296 (tramp-send-command-and-read
1298 (format "tramp_perl_file_attributes %s %s"
1299 (tramp-shell-quote-argument localname) id-format)))
1301 (defun tramp-do-file-attributes-with-stat
1302 (vec localname &optional id-format)
1303 "Implement `file-attributes' for Tramp files using stat(1) command."
1304 (tramp-message vec 5 "file attributes with stat: %s" localname)
1305 (tramp-send-command-and-read
1308 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
1309 ;; parse correctly the sequence "((". Therefore, we add a space.
1310 "( (%s %s || %s -h %s) && %s -c '((\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)' %s || echo nil)"
1311 (tramp-get-file-exists-command vec)
1312 (tramp-shell-quote-argument localname)
1313 (tramp-get-test-command vec)
1314 (tramp-shell-quote-argument localname)
1315 (tramp-get-remote-stat vec)
1316 (if (eq id-format 'integer) "%ue0" "\"%U\"")
1317 (if (eq id-format 'integer) "%ge0" "\"%G\"")
1318 (tramp-shell-quote-argument localname))))
1320 (defun tramp-sh-handle-set-visited-file-modtime (&optional time-list)
1321 "Like `set-visited-file-modtime' for Tramp files."
1322 (unless (buffer-file-name)
1323 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1326 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
1327 (let ((f (buffer-file-name))
1329 (with-parsed-tramp-file-name f nil
1330 (let* ((attr (file-attributes f))
1331 ;; '(-1 65535) means file doesn't exists yet.
1332 (modtime (or (nth 5 attr) '(-1 65535))))
1333 (when (boundp 'last-coding-system-used)
1334 (setq coding-system-used (symbol-value 'last-coding-system-used)))
1335 ;; We use '(0 0) as a don't-know value. See also
1336 ;; `tramp-do-file-attributes-with-ls'.
1337 (if (not (equal modtime '(0 0)))
1338 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
1342 (format "%s -ild %s"
1343 (tramp-get-ls-command v)
1344 (tramp-shell-quote-argument localname)))
1345 (setq attr (buffer-substring (point)
1346 (progn (end-of-line) (point)))))
1347 (tramp-set-file-property
1348 v localname "visited-file-modtime-ild" attr))
1349 (when (boundp 'last-coding-system-used)
1350 (set 'last-coding-system-used coding-system-used))
1353 ;; This function makes the same assumption as
1354 ;; `tramp-sh-handle-set-visited-file-modtime'.
1355 (defun tramp-sh-handle-verify-visited-file-modtime (buf)
1356 "Like `verify-visited-file-modtime' for Tramp files.
1357 At the time `verify-visited-file-modtime' calls this function, we
1358 already know that the buffer is visiting a file and that
1359 `visited-file-modtime' does not return 0. Do not call this
1360 function directly, unless those two cases are already taken care
1362 (with-current-buffer buf
1363 (let ((f (buffer-file-name)))
1364 ;; There is no file visiting the buffer, or the buffer has no
1365 ;; recorded last modification time, or there is no established
1368 (eq (visited-file-modtime) 0)
1369 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
1371 (with-parsed-tramp-file-name f nil
1372 (let* ((remote-file-name-inhibit-cache t)
1373 (attr (file-attributes f))
1374 (modtime (nth 5 attr))
1375 (mt (visited-file-modtime)))
1378 ;; File exists, and has a known modtime.
1379 ((and attr (not (equal modtime '(0 0))))
1380 (< (abs (tramp-time-diff
1382 ;; For compatibility, deal with both the old
1383 ;; (HIGH . LOW) and the new (HIGH LOW) return
1384 ;; values of `visited-file-modtime'.
1386 (list (car mt) (cdr mt))
1389 ;; Modtime has the don't know value.
1393 (format "%s -ild %s"
1394 (tramp-get-ls-command v)
1395 (tramp-shell-quote-argument localname)))
1396 (with-current-buffer (tramp-get-buffer v)
1397 (setq attr (buffer-substring
1398 (point) (progn (end-of-line) (point)))))
1401 (tramp-get-file-property
1402 v localname "visited-file-modtime-ild" "")))
1403 ;; If file does not exist, say it is not modified if and
1404 ;; only if that agrees with the buffer's record.
1405 (t (equal mt '(-1 65535))))))))))
1407 (defun tramp-sh-handle-set-file-modes (filename mode)
1408 "Like `set-file-modes' for Tramp files."
1409 (with-parsed-tramp-file-name filename nil
1410 (tramp-flush-file-property v localname)
1411 ;; FIXME: extract the proper text from chmod's stderr.
1412 (tramp-barf-unless-okay
1414 (format "chmod %s %s"
1415 (tramp-compat-decimal-to-octal mode)
1416 (tramp-shell-quote-argument localname))
1417 "Error while changing file's mode %s" filename)))
1419 (defun tramp-sh-handle-set-file-times (filename &optional time)
1420 "Like `set-file-times' for Tramp files."
1421 (if (file-remote-p filename)
1422 (with-parsed-tramp-file-name filename nil
1423 (tramp-flush-file-property v localname)
1424 (let ((time (if (or (null time) (equal time '(0 0)))
1427 ;; With GNU Emacs, `format-time-string' has an optional
1428 ;; parameter UNIVERSAL. This is preferred, because we
1429 ;; could handle the case when the remote host is located
1430 ;; in a different time zone as the local host.
1431 (utc (not (featurep 'xemacs))))
1432 (tramp-send-command-and-check
1433 v (format "%s touch -t %s %s"
1434 (if utc "TZ=UTC; export TZ;" "")
1436 (format-time-string "%Y%m%d%H%M.%S" time t)
1437 (format-time-string "%Y%m%d%H%M.%S" time))
1438 (tramp-shell-quote-argument localname)))))
1440 ;; We handle also the local part, because in older Emacsen,
1441 ;; without `set-file-times', this function is an alias for this.
1442 ;; We are local, so we don't need the UTC settings.
1444 (tramp-compat-call-process
1445 "touch" nil nil nil "-t"
1446 (format-time-string "%Y%m%d%H%M.%S" time)
1447 (tramp-shell-quote-argument filename)))))
1449 (defun tramp-set-file-uid-gid (filename &optional uid gid)
1450 "Set the ownership for FILENAME.
1451 If UID and GID are provided, these values are used; otherwise uid
1452 and gid of the corresponding user is taken. Both parameters must be integers."
1453 ;; Modern Unices allow chown only for root. So we might need
1454 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1455 ;; working with su(do)? when it is needed, so it shall succeed in
1456 ;; the majority of cases.
1457 ;; Don't modify `last-coding-system-used' by accident.
1458 (let ((last-coding-system-used last-coding-system-used))
1459 (if (file-remote-p filename)
1460 (with-parsed-tramp-file-name filename nil
1461 (if (and (zerop (user-uid)) (tramp-local-host-p v))
1462 ;; If we are root on the local host, we can do it directly.
1463 (tramp-set-file-uid-gid localname uid gid)
1464 (let ((uid (or (and (integerp uid) uid)
1465 (tramp-get-remote-uid v 'integer)))
1466 (gid (or (and (integerp gid) gid)
1467 (tramp-get-remote-gid v 'integer))))
1470 "chown %d:%d %s" uid gid
1471 (tramp-shell-quote-argument localname))))))
1473 ;; We handle also the local part, because there doesn't exist
1474 ;; `set-file-uid-gid'. On W32 "chown" might not work.
1475 (let ((uid (or (and (integerp uid) uid) (tramp-get-local-uid 'integer)))
1476 (gid (or (and (integerp gid) gid) (tramp-get-local-gid 'integer))))
1477 (tramp-compat-call-process
1479 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
1481 (defun tramp-remote-selinux-p (vec)
1482 "Check, whether SELINUX is enabled on the remote host."
1483 (with-tramp-connection-property
1484 (tramp-get-connection-process vec) "selinux-p"
1485 (let ((result (tramp-find-executable
1486 vec "getenforce" (tramp-get-remote-path vec) t t)))
1489 (tramp-send-command-and-read
1490 vec (format "echo \\\"`%S`\\\"" result))
1493 (defun tramp-sh-handle-file-selinux-context (filename)
1494 "Like `file-selinux-context' for Tramp files."
1495 (with-parsed-tramp-file-name filename nil
1496 (with-tramp-file-property v localname "file-selinux-context"
1497 (let ((context '(nil nil nil nil))
1498 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1499 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1500 (when (and (tramp-remote-selinux-p v)
1501 (tramp-send-command-and-check
1504 (tramp-get-ls-command v)
1505 (tramp-shell-quote-argument localname))))
1506 (with-current-buffer (tramp-get-connection-buffer v)
1507 (goto-char (point-min))
1508 (when (re-search-forward regexp (point-at-eol) t)
1509 (setq context (list (match-string 1) (match-string 2)
1510 (match-string 3) (match-string 4))))))
1511 ;; Return the context.
1514 (defun tramp-sh-handle-set-file-selinux-context (filename context)
1515 "Like `set-file-selinux-context' for Tramp files."
1516 (with-parsed-tramp-file-name filename nil
1517 (if (and (consp context)
1518 (tramp-remote-selinux-p v)
1519 (tramp-send-command-and-check
1520 v (format "chcon %s %s %s %s %s"
1521 (if (stringp (nth 0 context))
1522 (format "--user=%s" (nth 0 context)) "")
1523 (if (stringp (nth 1 context))
1524 (format "--role=%s" (nth 1 context)) "")
1525 (if (stringp (nth 2 context))
1526 (format "--type=%s" (nth 2 context)) "")
1527 (if (stringp (nth 3 context))
1528 (format "--range=%s" (nth 3 context)) "")
1529 (tramp-shell-quote-argument localname))))
1530 (tramp-set-file-property v localname "file-selinux-context" context)
1531 (tramp-set-file-property v localname "file-selinux-context" 'undef)))
1532 ;; We always return nil.
1535 ;; Simple functions using the `test' command.
1537 (defun tramp-sh-handle-file-executable-p (filename)
1538 "Like `file-executable-p' for Tramp files."
1539 (with-parsed-tramp-file-name filename nil
1540 (with-tramp-file-property v localname "file-executable-p"
1541 ;; Examine `file-attributes' cache to see if request can be
1542 ;; satisfied without remote operation.
1543 (or (tramp-check-cached-permissions v ?x)
1544 (tramp-run-test "-x" filename)))))
1546 (defun tramp-sh-handle-file-readable-p (filename)
1547 "Like `file-readable-p' for Tramp files."
1548 (with-parsed-tramp-file-name filename nil
1549 (with-tramp-file-property v localname "file-readable-p"
1550 ;; Examine `file-attributes' cache to see if request can be
1551 ;; satisfied without remote operation.
1552 (or (tramp-check-cached-permissions v ?r)
1553 (tramp-run-test "-r" filename)))))
1555 ;; When the remote shell is started, it looks for a shell which groks
1556 ;; tilde expansion. Here, we assume that all shells which grok tilde
1557 ;; expansion will also provide a `test' command which groks `-nt' (for
1558 ;; newer than). If this breaks, tell me about it and I'll try to do
1559 ;; something smarter about it.
1560 (defun tramp-sh-handle-file-newer-than-file-p (file1 file2)
1561 "Like `file-newer-than-file-p' for Tramp files."
1562 (cond ((not (file-exists-p file1))
1564 ((not (file-exists-p file2))
1566 ;; We are sure both files exist at this point.
1569 ;; We try to get the mtime of both files. If they are not
1570 ;; equal to the "dont-know" value, then we subtract the times
1571 ;; and obtain the result.
1572 (let ((fa1 (file-attributes file1))
1573 (fa2 (file-attributes file2)))
1574 (if (and (not (equal (nth 5 fa1) '(0 0)))
1575 (not (equal (nth 5 fa2) '(0 0))))
1576 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
1577 ;; If one of them is the dont-know value, then we can
1578 ;; still try to run a shell command on the remote host.
1579 ;; However, this only works if both files are Tramp
1580 ;; files and both have the same method, same user, same
1582 (unless (tramp-equal-remote file1 file2)
1583 (with-parsed-tramp-file-name
1584 (if (tramp-tramp-file-p file1) file1 file2) nil
1587 "Files %s and %s must have same method, user, host"
1589 (with-parsed-tramp-file-name file1 nil
1591 (tramp-get-test-nt-command v) file1 file2))))))))
1593 ;; Functions implemented using the basic functions above.
1595 (defun tramp-sh-handle-file-directory-p (filename)
1596 "Like `file-directory-p' for Tramp files."
1597 (with-parsed-tramp-file-name filename nil
1598 ;; `file-directory-p' is used as predicate for filename completion.
1599 ;; Sometimes, when a connection is not established yet, it is
1600 ;; desirable to return t immediately for "/method:foo:". It can
1601 ;; be expected that this is always a directory.
1602 (or (zerop (length localname))
1603 (with-tramp-file-property v localname "file-directory-p"
1604 (tramp-run-test "-d" filename)))))
1606 (defun tramp-sh-handle-file-writable-p (filename)
1607 "Like `file-writable-p' for Tramp files."
1608 (with-parsed-tramp-file-name filename nil
1609 (with-tramp-file-property v localname "file-writable-p"
1610 (if (file-exists-p filename)
1611 ;; Examine `file-attributes' cache to see if request can be
1612 ;; satisfied without remote operation.
1613 (or (tramp-check-cached-permissions v ?w)
1614 (tramp-run-test "-w" filename))
1615 ;; If file doesn't exist, check if directory is writable.
1616 (and (tramp-run-test "-d" (file-name-directory filename))
1617 (tramp-run-test "-w" (file-name-directory filename)))))))
1619 (defun tramp-sh-handle-file-ownership-preserved-p (filename)
1620 "Like `file-ownership-preserved-p' for Tramp files."
1621 (with-parsed-tramp-file-name filename nil
1622 (with-tramp-file-property v localname "file-ownership-preserved-p"
1623 (let ((attributes (file-attributes filename)))
1624 ;; Return t if the file doesn't exist, since it's true that no
1625 ;; information would be lost by an (attempted) delete and create.
1626 (or (null attributes)
1627 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer)))))))
1629 ;; Directory listings.
1631 (defun tramp-sh-handle-directory-files-and-attributes
1632 (directory &optional full match nosort id-format)
1633 "Like `directory-files-and-attributes' for Tramp files."
1634 (unless id-format (setq id-format 'integer))
1635 (when (file-directory-p directory)
1636 (setq directory (expand-file-name directory))
1639 (with-parsed-tramp-file-name directory nil
1640 (with-tramp-file-property
1642 (format "directory-files-and-attributes-%s" id-format)
1647 (tramp-convert-file-attributes v (cdr x))))
1649 ((tramp-get-remote-stat v)
1650 (tramp-do-directory-files-and-attributes-with-stat
1651 v localname id-format))
1652 ((tramp-get-remote-perl v)
1653 (tramp-do-directory-files-and-attributes-with-perl
1654 v localname id-format)))))))))
1658 (setq item (pop temp))
1659 (when (or (null match) (string-match match (car item)))
1661 (setcar item (expand-file-name (car item) directory)))
1662 (push item result)))
1666 (sort result (lambda (x y) (string< (car x) (car y))))))))
1668 (defun tramp-do-directory-files-and-attributes-with-perl
1669 (vec localname &optional id-format)
1670 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1671 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
1672 (tramp-maybe-send-script
1673 vec tramp-perl-directory-files-and-attributes
1674 "tramp_perl_directory_files_and_attributes")
1676 (tramp-send-command-and-read
1678 (format "tramp_perl_directory_files_and_attributes %s %s"
1679 (tramp-shell-quote-argument localname) id-format))))
1680 (when (stringp object) (tramp-error vec 'file-error object))
1683 (defun tramp-do-directory-files-and-attributes-with-stat
1684 (vec localname &optional id-format)
1685 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1686 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
1687 (tramp-send-command-and-read
1691 ;; We must care about filenames with spaces, or starting with
1692 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1693 ;; but it does not work on all remote systems. Therefore, we
1694 ;; quote the filenames via sed.
1695 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | "
1697 "'(\"%%n\" (\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)'"
1698 " 2>/dev/null); echo \")\"")
1699 (tramp-shell-quote-argument localname)
1700 (tramp-get-ls-command vec)
1701 (tramp-get-remote-stat vec)
1702 (if (eq id-format 'integer) "%ue0" "\"%U\"")
1703 (if (eq id-format 'integer) "%ge0" "\"%G\""))))
1705 ;; This function should return "foo/" for directories and "bar" for
1707 (defun tramp-sh-handle-file-name-all-completions (filename directory)
1708 "Like `file-name-all-completions' for Tramp files."
1709 (unless (save-match-data (string-match "/" filename))
1710 (with-parsed-tramp-file-name (expand-file-name directory) nil
1717 ;; Try cache entries for filename, filename with last
1718 ;; character removed, filename with last two characters
1719 ;; removed, ..., and finally the empty string - all
1720 ;; concatenated to the local directory name.
1721 (let ((remote-file-name-inhibit-cache
1722 (or remote-file-name-inhibit-cache
1723 tramp-completion-reread-directory-timeout)))
1725 ;; This is inefficient for very long filenames, pity
1726 ;; `reduce' is not available...
1733 (tramp-get-file-property
1735 (concat localname (substring filename 0 x))
1736 "file-name-all-completions"
1738 (when cache-hit (list cache-hit))))
1739 ;; We cannot use a length of 0, because file properties
1740 ;; for "foo" and "foo/" are identical.
1741 (tramp-compat-number-sequence (length filename) 1 -1)))))
1743 ;; Cache expired or no matching cache entry found so we need
1744 ;; to perform a remote operation.
1746 ;; Get a list of directories and files, including reliably
1747 ;; tagging the directories with a trailing '/'. Because I
1748 ;; rock. --daniel@danann.net
1750 ;; Changed to perform `cd' in the same remote op and only
1751 ;; get entries starting with `filename'. Capture any `cd'
1752 ;; error messages. Ensure any `cd' and `echo' aliases are
1756 (if (tramp-get-remote-perl v)
1758 (tramp-maybe-send-script
1759 v tramp-perl-file-name-all-completions
1760 "tramp_perl_file_name_all_completions")
1761 (format "tramp_perl_file_name_all_completions %s %s %d"
1762 (tramp-shell-quote-argument localname)
1763 (tramp-shell-quote-argument filename)
1765 ;; `read-file-name-completion-ignore-case'
1766 ;; is introduced with Emacs 22.1.
1768 'read-file-name-completion-ignore-case)
1769 'read-file-name-completion-ignore-case
1770 'completion-ignore-case))
1774 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
1775 ;; `ls' with wildcard might fail with `Argument
1776 ;; list too long' error in some corner cases; if
1777 ;; `ls' fails after `cd' succeeded, chances are
1778 ;; that's the case, so let's retry without
1779 ;; wildcard. This will return "too many" entries
1780 ;; but that isn't harmful.
1781 " || %s -a 2>/dev/null)"
1782 " | while read f; do"
1783 " if %s -d \"$f\" 2>/dev/null;"
1784 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1785 " && \\echo ok) || \\echo fail")
1786 (tramp-shell-quote-argument localname)
1787 (tramp-get-ls-command v)
1788 ;; When `filename' is empty, just `ls' without
1789 ;; filename argument is more efficient than `ls *'
1790 ;; for very large directories and might avoid the
1791 ;; `Argument list too long' error.
1793 ;; With and only with wildcard, we need to add
1794 ;; `-d' to prevent `ls' from descending into
1796 (if (zerop (length filename))
1798 (concat (tramp-shell-quote-argument filename) "* -d"))
1799 (tramp-get-ls-command v)
1800 (tramp-get-test-command v))))
1802 ;; Now grab the output.
1803 (with-current-buffer (tramp-get-buffer v)
1804 (goto-char (point-max))
1806 ;; Check result code, found in last line of output.
1808 (if (looking-at "^fail$")
1810 ;; Grab error message from line before last line
1811 ;; (it was put there by `cd 2>&1').
1815 "tramp-sh-handle-file-name-all-completions: %s"
1816 (buffer-substring (point) (point-at-eol))))
1817 ;; For peace of mind, if buffer doesn't end in `fail'
1818 ;; then it should end in `ok'. If neither are in the
1819 ;; buffer something went seriously wrong on the remote
1821 (unless (looking-at "^ok$")
1825 tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
1826 (tramp-shell-quote-argument localname) (buffer-string))))
1828 (while (zerop (forward-line -1))
1829 (push (buffer-substring (point) (point-at-eol)) result)))
1831 ;; Because the remote op went through OK we know the
1832 ;; directory we `cd'-ed to exists.
1833 (tramp-set-file-property v localname "file-exists-p" t)
1835 ;; Because the remote op went through OK we know every
1836 ;; file listed by `ls' exists.
1837 (mapc (lambda (entry)
1838 (tramp-set-file-property
1839 v (concat localname entry) "file-exists-p" t))
1842 ;; Store result in the cache.
1843 (tramp-set-file-property
1844 v (concat localname filename)
1845 "file-name-all-completions" result))))))))
1849 (defun tramp-sh-handle-add-name-to-file
1850 (filename newname &optional ok-if-already-exists)
1851 "Like `add-name-to-file' for Tramp files."
1852 (unless (tramp-equal-remote filename newname)
1853 (with-parsed-tramp-file-name
1854 (if (tramp-tramp-file-p filename) filename newname) nil
1857 "add-name-to-file: %s"
1858 "only implemented for same method, same user, same host")))
1859 (with-parsed-tramp-file-name filename v1
1860 (with-parsed-tramp-file-name newname v2
1861 (let ((ln (when v1 (tramp-get-remote-ln v1))))
1862 (when (and (not ok-if-already-exists)
1863 (file-exists-p newname)
1864 (not (numberp ok-if-already-exists))
1867 "File %s already exists; make it a new name anyway? "
1871 "add-name-to-file: file %s already exists" newname))
1872 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1873 (tramp-flush-file-property v2 v2-localname)
1874 (tramp-barf-unless-okay
1876 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
1877 (tramp-shell-quote-argument v2-localname))
1878 "error with add-name-to-file, see buffer `%s' for details"
1881 (defun tramp-sh-handle-copy-file
1882 (filename newname &optional ok-if-already-exists keep-date
1883 preserve-uid-gid preserve-selinux-context)
1884 "Like `copy-file' for Tramp files."
1885 (setq filename (expand-file-name filename))
1886 (setq newname (expand-file-name newname))
1888 ;; At least one file a Tramp file?
1889 ((or (tramp-tramp-file-p filename)
1890 (tramp-tramp-file-p newname))
1891 (tramp-do-copy-or-rename-file
1892 'copy filename newname ok-if-already-exists keep-date
1893 preserve-uid-gid preserve-selinux-context))
1895 (preserve-selinux-context
1896 (tramp-run-real-handler
1898 (list filename newname ok-if-already-exists keep-date
1899 preserve-uid-gid preserve-selinux-context)))
1901 (tramp-run-real-handler
1903 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
1905 (tramp-run-real-handler
1906 'copy-file (list filename newname ok-if-already-exists keep-date)))))
1908 (defun tramp-sh-handle-copy-directory
1909 (dirname newname &optional keep-date parents copy-contents)
1910 "Like `copy-directory' for Tramp files."
1911 (let ((t1 (tramp-tramp-file-p dirname))
1912 (t2 (tramp-tramp-file-p newname)))
1913 (with-parsed-tramp-file-name (if t1 dirname newname) nil
1914 (if (and (tramp-get-method-parameter method 'tramp-copy-recursive)
1915 ;; When DIRNAME and NEWNAME are remote, they must have
1917 (or (null t1) (null t2)
1919 (tramp-file-name-method (tramp-dissect-file-name dirname))
1920 (tramp-file-name-method (tramp-dissect-file-name newname)))))
1921 ;; scp or rsync DTRT.
1923 (setq dirname (directory-file-name (expand-file-name dirname))
1924 newname (directory-file-name (expand-file-name newname)))
1925 (if (and (file-directory-p newname)
1926 (not (string-equal (file-name-nondirectory dirname)
1927 (file-name-nondirectory newname))))
1930 (file-name-nondirectory dirname) newname)))
1931 (if (not (file-directory-p (file-name-directory newname)))
1932 (make-directory (file-name-directory newname) parents))
1933 (tramp-do-copy-or-rename-file-out-of-band
1934 'copy dirname newname keep-date))
1935 ;; We must do it file-wise.
1936 (tramp-run-real-handler
1937 'copy-directory (list dirname newname keep-date parents)))
1939 ;; When newname did exist, we have wrong cached values.
1941 (with-parsed-tramp-file-name newname nil
1942 (tramp-flush-file-property v (file-name-directory localname))
1943 (tramp-flush-file-property v localname))))))
1945 (defun tramp-sh-handle-rename-file
1946 (filename newname &optional ok-if-already-exists)
1947 "Like `rename-file' for Tramp files."
1948 ;; Check if both files are local -- invoke normal rename-file.
1949 ;; Otherwise, use Tramp from local system.
1950 (setq filename (expand-file-name filename))
1951 (setq newname (expand-file-name newname))
1952 ;; At least one file a Tramp file?
1953 (if (or (tramp-tramp-file-p filename)
1954 (tramp-tramp-file-p newname))
1955 (tramp-do-copy-or-rename-file
1956 'rename filename newname ok-if-already-exists t t)
1957 (tramp-run-real-handler
1958 'rename-file (list filename newname ok-if-already-exists))))
1960 (defun tramp-do-copy-or-rename-file
1961 (op filename newname &optional ok-if-already-exists keep-date
1962 preserve-uid-gid preserve-selinux-context)
1963 "Copy or rename a remote file.
1964 OP must be `copy' or `rename' and indicates the operation to perform.
1965 FILENAME specifies the file to copy or rename, NEWNAME is the name of
1966 the new file (for copy) or the new name of the file (for rename).
1967 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
1968 KEEP-DATE means to make sure that NEWNAME has the same timestamp
1969 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
1970 the uid and gid if both files are on the same host.
1971 PRESERVE-SELINUX-CONTEXT activates selinux commands.
1973 This function is invoked by `tramp-sh-handle-copy-file' and
1974 `tramp-sh-handle-rename-file'. It is an error if OP is neither
1975 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
1977 (unless (memq op '(copy rename))
1978 (error "Unknown operation `%s', must be `copy' or `rename'" op))
1979 (let ((t1 (tramp-tramp-file-p filename))
1980 (t2 (tramp-tramp-file-p newname))
1981 (length (nth 7 (file-attributes (file-truename filename))))
1982 (context (and preserve-selinux-context
1983 (apply 'file-selinux-context (list filename))))
1986 (with-parsed-tramp-file-name (if t1 filename newname) nil
1987 (when (and (not ok-if-already-exists) (file-exists-p newname))
1989 v 'file-already-exists "File %s already exists" newname))
1991 (with-tramp-progress-reporter
1992 v 0 (format "%s %s to %s"
1993 (if (eq op 'copy) "Copying" "Renaming")
1997 ;; Both are Tramp files.
1999 (with-parsed-tramp-file-name filename v1
2000 (with-parsed-tramp-file-name newname v2
2002 ;; Shortcut: if method, host, user are the same for
2003 ;; both files, we invoke `cp' or `mv' on the remote
2005 ((tramp-equal-remote filename newname)
2006 (tramp-do-copy-or-rename-file-directly
2008 ok-if-already-exists keep-date preserve-uid-gid))
2010 ;; Try out-of-band operation.
2012 (tramp-method-out-of-band-p v1 length)
2013 (tramp-method-out-of-band-p v2 length))
2014 (tramp-do-copy-or-rename-file-out-of-band
2015 op filename newname keep-date))
2017 ;; No shortcut was possible. So we copy the file
2018 ;; first. If the operation was `rename', we go back
2019 ;; and delete the original file (if the copy was
2020 ;; successful). The approach is simple-minded: we
2021 ;; create a new buffer, insert the contents of the
2022 ;; source file into it, then write out the buffer to
2023 ;; the target file. The advantage is that it doesn't
2024 ;; matter which filename handlers are used for the
2025 ;; source and target file.
2027 (tramp-do-copy-or-rename-file-via-buffer
2028 op filename newname keep-date))))))
2030 ;; One file is a Tramp file, the other one is local.
2033 ;; Fast track on local machine.
2034 ((tramp-local-host-p v)
2035 (tramp-do-copy-or-rename-file-directly
2037 ok-if-already-exists keep-date preserve-uid-gid))
2039 ;; If the Tramp file has an out-of-band method, the
2040 ;; corresponding copy-program can be invoked.
2041 ((tramp-method-out-of-band-p v length)
2042 (tramp-do-copy-or-rename-file-out-of-band
2043 op filename newname keep-date))
2045 ;; Use the inline method via a Tramp buffer.
2046 (t (tramp-do-copy-or-rename-file-via-buffer
2047 op filename newname keep-date))))
2050 ;; One of them must be a Tramp file.
2051 (error "Tramp implementation says this cannot happen")))
2053 ;; Handle `preserve-selinux-context'.
2054 (when context (apply 'set-file-selinux-context (list newname context)))
2056 ;; In case of `rename', we must flush the cache of the source file.
2057 (when (and t1 (eq op 'rename))
2058 (with-parsed-tramp-file-name filename v1
2059 (tramp-flush-file-property v1 (file-name-directory localname))
2060 (tramp-flush-file-property v1 localname)))
2062 ;; When newname did exist, we have wrong cached values.
2064 (with-parsed-tramp-file-name newname v2
2065 (tramp-flush-file-property v2 (file-name-directory localname))
2066 (tramp-flush-file-property v2 localname)))))))
2068 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
2069 "Use an Emacs buffer to copy or rename a file.
2070 First arg OP is either `copy' or `rename' and indicates the operation.
2071 FILENAME is the source file, NEWNAME the target file.
2072 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2074 ;; We must disable multibyte, because binary data shall not be
2076 (set-buffer-multibyte nil)
2077 (let ((coding-system-for-read 'binary)
2078 (jka-compr-inhibit t))
2079 (insert-file-contents-literally filename))
2080 ;; We don't want the target file to be compressed, so we let-bind
2081 ;; `jka-compr-inhibit' to t.
2082 (let ((coding-system-for-write 'binary)
2083 (jka-compr-inhibit t))
2084 (write-region (point-min) (point-max) newname)))
2085 ;; KEEP-DATE handling.
2086 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
2088 (set-file-modes newname (tramp-default-file-modes filename))
2089 ;; If the operation was `rename', delete the original file.
2090 (unless (eq op 'copy) (delete-file filename)))
2092 (defun tramp-do-copy-or-rename-file-directly
2093 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
2094 "Invokes `cp' or `mv' on the remote system.
2095 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2096 respectively. FILENAME specifies the file to copy or rename,
2097 NEWNAME is the name of the new file (for copy) or the new name of
2098 the file (for rename). Both files must reside on the same host.
2099 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2100 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2101 the uid and gid from FILENAME."
2102 (let ((t1 (tramp-tramp-file-p filename))
2103 (t2 (tramp-tramp-file-p newname))
2104 (file-times (nth 5 (file-attributes filename)))
2105 (file-modes (tramp-default-file-modes filename)))
2106 (with-parsed-tramp-file-name (if t1 filename newname) nil
2107 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
2108 ((eq op 'copy) "cp -f")
2109 ((eq op 'rename) "mv -f")
2112 "Unknown operation `%s', must be `copy' or `rename'"
2116 (tramp-file-name-handler 'file-remote-p filename 'localname)
2120 (tramp-file-name-handler 'file-remote-p newname 'localname)
2122 (prefix (file-remote-p (if t1 filename newname)))
2126 ;; Both files are on a remote host, with same user.
2129 (tramp-send-command-and-check
2130 v (format "%s %s %s" cmd
2131 (tramp-shell-quote-argument localname1)
2132 (tramp-shell-quote-argument localname2))))
2133 (with-current-buffer (tramp-get-buffer v)
2134 (goto-char (point-min))
2138 ;; Mask cp -f error.
2140 tramp-operation-not-permitted-regexp nil t))
2142 (tramp-error-with-buffer
2144 "Copying directly failed, see buffer `%s' for details."
2147 ;; We are on the local host.
2150 ;; We can do it directly.
2151 ((let (file-name-handler-alist)
2152 (and (file-readable-p localname1)
2153 (file-writable-p (file-name-directory localname2))
2154 (or (file-directory-p localname2)
2155 (file-writable-p localname2))))
2157 (tramp-compat-copy-file
2158 localname1 localname2 ok-if-already-exists
2159 keep-date preserve-uid-gid)
2160 (tramp-run-real-handler
2161 'rename-file (list localname1 localname2 ok-if-already-exists))))
2163 ;; We can do it directly with `tramp-send-command'
2164 ((and (file-readable-p (concat prefix localname1))
2166 (file-name-directory (concat prefix localname2)))
2167 (or (file-directory-p (concat prefix localname2))
2168 (file-writable-p (concat prefix localname2))))
2169 (tramp-do-copy-or-rename-file-directly
2170 op (concat prefix localname1) (concat prefix localname2)
2171 ok-if-already-exists keep-date t)
2172 ;; We must change the ownership to the local user.
2173 (tramp-set-file-uid-gid
2174 (concat prefix localname2)
2175 (tramp-get-local-uid 'integer)
2176 (tramp-get-local-gid 'integer)))
2178 ;; We need a temporary file in between.
2180 ;; Create the temporary file.
2181 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
2186 (tramp-barf-unless-okay
2189 (tramp-shell-quote-argument localname1)
2190 (tramp-shell-quote-argument tmpfile))
2191 "Copying directly failed, see buffer `%s' for details."
2192 (tramp-get-buffer v))
2193 ;; We must change the ownership as remote user.
2194 ;; Since this does not work reliable, we also
2195 ;; give read permissions.
2197 (concat prefix tmpfile)
2198 (tramp-compat-octal-to-decimal "0777"))
2199 (tramp-set-file-uid-gid
2200 (concat prefix tmpfile)
2201 (tramp-get-local-uid 'integer)
2202 (tramp-get-local-gid 'integer)))
2205 (tramp-compat-copy-file
2206 localname1 tmpfile t
2207 keep-date preserve-uid-gid)
2208 (tramp-run-real-handler
2210 (list localname1 tmpfile t)))
2211 ;; We must change the ownership as local user.
2212 ;; Since this does not work reliable, we also
2213 ;; give read permissions.
2215 tmpfile (tramp-compat-octal-to-decimal "0777"))
2216 (tramp-set-file-uid-gid
2218 (tramp-get-remote-uid v 'integer)
2219 (tramp-get-remote-gid v 'integer))))
2221 ;; Move the temporary file to its destination.
2224 (tramp-barf-unless-okay
2227 (tramp-shell-quote-argument tmpfile)
2228 (tramp-shell-quote-argument localname2))
2229 "Copying directly failed, see buffer `%s' for details."
2230 (tramp-get-buffer v)))
2232 (tramp-run-real-handler
2234 (list tmpfile localname2 ok-if-already-exists)))))
2237 (ignore-errors (delete-file tmpfile)))))))))
2239 ;; Set the time and mode. Mask possible errors.
2242 (set-file-times newname file-times)
2243 (set-file-modes newname file-modes))))))
2245 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
2246 "Invoke rcp program to copy.
2247 The method used must be an out-of-band method."
2248 (let* ((t1 (tramp-tramp-file-p filename))
2249 (t2 (tramp-tramp-file-p newname))
2250 (orig-vec (tramp-dissect-file-name (if t1 filename newname)))
2251 copy-program copy-args copy-env copy-keep-date port spec
2254 (with-parsed-tramp-file-name (if t1 filename newname) nil
2257 ;; Both are Tramp files. We shall optimize it when the
2258 ;; methods for filename and newname are the same.
2259 (let* ((dir-flag (file-directory-p filename))
2260 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
2264 (file-name-nondirectory newname) tmpfile)))
2267 (tramp-do-copy-or-rename-file-out-of-band
2268 op filename tmpfile keep-date)
2269 (tramp-do-copy-or-rename-file-out-of-band
2270 'rename tmpfile newname keep-date))
2274 (tramp-compat-delete-directory
2275 (expand-file-name ".." tmpfile) 'recursive)
2276 (delete-file tmpfile)))))
2278 ;; Set variables for computing the prompt for reading
2280 (setq tramp-current-method (tramp-file-name-method v)
2281 tramp-current-user (or (tramp-file-name-user v)
2282 (tramp-get-connection-property
2284 tramp-current-host (tramp-file-name-real-host v))
2286 ;; Expand hops. Might be necessary for gateway methods.
2287 (setq v (car (tramp-compute-multi-hops v)))
2288 (aset v 3 localname)
2290 ;; Check which ones of source and target are Tramp files.
2291 (setq source (if t1 (tramp-make-copy-program-file-name v) filename)
2293 (if (and (file-directory-p filename)
2295 (file-name-nondirectory filename)
2296 (file-name-nondirectory newname)))
2297 'file-name-directory
2299 (if t2 (tramp-make-copy-program-file-name v) newname)))
2301 ;; Check for host and port number. We cannot use
2302 ;; `tramp-file-name-port', because this returns also
2303 ;; `tramp-default-port', which might clash with settings in
2305 (setq host (tramp-file-name-host v)
2307 (when (string-match tramp-host-with-port-regexp host)
2308 (setq port (string-to-number (match-string 2 host))
2309 host (string-to-number (match-string 1 host))))
2311 ;; Check for user. There might be an interactive setting.
2312 (setq user (or (tramp-file-name-user v)
2313 (tramp-get-connection-property v "login-as" nil)))
2315 ;; Compose copy command.
2316 (setq host (or host "")
2319 spec (format-spec-make
2320 ?h host ?u user ?p port
2321 ?t (tramp-get-connection-property
2322 (tramp-get-connection-process v) "temp-file" "")
2323 ?k (if keep-date " " ""))
2324 copy-program (tramp-get-method-parameter
2325 method 'tramp-copy-program)
2326 copy-keep-date (tramp-get-method-parameter
2327 method 'tramp-copy-keep-date)
2330 ;; " " has either been a replacement of "%k" (when
2331 ;; keep-date argument is non-nil), or a replacement
2332 ;; for the whole keep-date sublist.
2336 (tramp-get-method-parameter method 'tramp-copy-args)
2341 (let ((y (mapcar (lambda (z) (format-spec z spec)) x)))
2342 (if (member "" y) '(" ") y))))))
2348 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
2349 (unless (member "" x) (mapconcat 'identity x " ")))
2350 (tramp-get-method-parameter method 'tramp-copy-env))))
2352 ;; Check for program.
2353 (unless (let ((default-directory
2354 (tramp-compat-temporary-file-directory)))
2355 (executable-find copy-program))
2357 v 'file-error "Cannot find copy program: %s" copy-program))
2361 ;; The default directory must be remote.
2362 (let ((default-directory
2363 (file-name-directory (if t1 filename newname)))
2364 (process-environment (copy-sequence process-environment)))
2365 ;; Set the transfer process properties.
2366 (tramp-set-connection-property
2367 v "process-name" (buffer-name (current-buffer)))
2368 (tramp-set-connection-property
2369 v "process-buffer" (current-buffer))
2372 orig-vec 5 "%s=\"%s\"" (car copy-env) (cadr copy-env))
2373 (setenv (pop copy-env) (pop copy-env)))
2375 ;; Use an asynchronous process. By this, password can
2376 ;; be handled. The default directory must be local, in
2377 ;; order to apply the correct `copy-program'. We don't
2378 ;; set a timeout, because the copying of large files can
2379 ;; last longer than 60 secs.
2380 (let ((p (let ((default-directory
2381 (tramp-compat-temporary-file-directory)))
2382 (apply 'start-process-shell-command
2383 (tramp-get-connection-name v)
2384 (tramp-get-connection-buffer v)
2389 (shell-quote-argument source)
2390 (shell-quote-argument target))
2391 (unless (memq system-type '(windows-nt))
2393 "tramp_exit_status" "$?")))))))
2396 (mapconcat 'identity (process-command p) " "))
2397 (tramp-compat-set-process-query-on-exit-flag p nil)
2398 (tramp-process-actions
2399 p v nil tramp-actions-copy-out-of-band)
2401 ;; Check the return code. This does not work under
2403 (unless (memq system-type '(windows-nt))
2404 (goto-char (point-max))
2406 (re-search-backward "tramp_exit_status [0-9]+" nil t)
2408 orig-vec 'file-error
2409 "Couldn't find exit status of `%s'" (process-command p)))
2410 (skip-chars-forward "^ ")
2411 (unless (zerop (read (current-buffer)))
2414 orig-vec 'file-error
2415 "Error copying: `%s'"
2416 (buffer-substring (point-min) (point-at-eol)))))))
2418 ;; Reset the transfer process properties.
2419 (tramp-message orig-vec 6 "\n%s" (buffer-string))
2420 (tramp-set-connection-property v "process-name" nil)
2421 (tramp-set-connection-property v "process-buffer" nil)))
2423 ;; Handle KEEP-DATE argument.
2424 (when (and keep-date (not copy-keep-date))
2425 (set-file-times newname (nth 5 (file-attributes filename))))
2428 (unless (and keep-date copy-keep-date)
2430 (set-file-modes newname (tramp-default-file-modes filename)))))
2432 ;; If the operation was `rename', delete the original file.
2433 (unless (eq op 'copy)
2434 (if (file-regular-p filename)
2435 (delete-file filename)
2436 (tramp-compat-delete-directory filename 'recursive))))))
2438 (defun tramp-sh-handle-make-directory (dir &optional parents)
2439 "Like `make-directory' for Tramp files."
2440 (setq dir (expand-file-name dir))
2441 (with-parsed-tramp-file-name dir nil
2442 (tramp-flush-directory-property v (file-name-directory localname))
2444 (tramp-barf-unless-okay
2446 (if parents "mkdir -p" "mkdir")
2447 (tramp-shell-quote-argument localname))
2448 "Couldn't make directory %s" dir))))
2450 (defun tramp-sh-handle-delete-directory (directory &optional recursive)
2451 "Like `delete-directory' for Tramp files."
2452 (setq directory (expand-file-name directory))
2453 (with-parsed-tramp-file-name directory nil
2454 (tramp-flush-file-property v (file-name-directory localname))
2455 (tramp-flush-directory-property v localname)
2456 (tramp-barf-unless-okay
2458 (if recursive "rm -rf" "rmdir")
2459 (tramp-shell-quote-argument localname))
2460 "Couldn't delete %s" directory)))
2462 (defun tramp-sh-handle-delete-file (filename &optional trash)
2463 "Like `delete-file' for Tramp files."
2464 (setq filename (expand-file-name filename))
2465 (with-parsed-tramp-file-name filename nil
2466 (tramp-flush-file-property v (file-name-directory localname))
2467 (tramp-flush-file-property v localname)
2468 (tramp-barf-unless-okay
2470 (or (and trash (tramp-get-remote-trash v)) "rm -f")
2471 (tramp-shell-quote-argument localname))
2472 "Couldn't delete %s" filename)))
2476 ;; CCC: This does not seem to be enough. Something dies when
2477 ;; we try and delete two directories under Tramp :/
2478 (defun tramp-sh-handle-dired-recursive-delete-directory (filename)
2479 "Recursively delete the directory given.
2480 This is like `dired-recursive-delete-directory' for Tramp files."
2481 (with-parsed-tramp-file-name filename nil
2482 ;; Run a shell command 'rm -r <localname>'.
2483 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
2484 (unless (file-exists-p filename)
2485 (tramp-error v 'file-error "No such directory: %s" filename))
2486 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>).
2489 (format "rm -rf %s" (tramp-shell-quote-argument localname))
2490 ;; Don't read the output, do it explicitly.
2492 ;; Wait for the remote system to return to us...
2493 ;; This might take a while, allow it plenty of time.
2494 (tramp-wait-for-output (tramp-get-connection-process v) 120)
2495 ;; Make sure that it worked...
2496 (tramp-flush-file-property v (file-name-directory localname))
2497 (tramp-flush-directory-property v localname)
2498 (and (file-exists-p filename)
2500 v 'file-error "Failed to recursively delete %s" filename))))
2502 (defun tramp-sh-handle-dired-compress-file (file &rest ok-flag)
2503 "Like `dired-compress-file' for Tramp files."
2504 ;; OK-FLAG is valid for XEmacs only, but not implemented.
2505 ;; Code stolen mainly from dired-aux.el.
2506 (with-parsed-tramp-file-name file nil
2507 (tramp-flush-file-property v localname)
2510 (if (not (featurep 'xemacs))
2512 (symbol-value 'dired-compress-file-suffixes)
2513 ;; XEmacs has `dired-compression-method-alist', which is
2514 ;; transformed into `dired-compress-file-suffixes' structure.
2517 (list (concat (regexp-quote (nth 1 x)) "\\'")
2519 (mapconcat 'identity (nth 3 x) " ")))
2520 (symbol-value 'dired-compression-method-alist))))
2522 ;; See if any suffix rule matches this file name.
2524 (let (case-fold-search)
2525 (if (string-match (car (car suffixes)) localname)
2526 (setq suffix (car suffixes) suffixes nil))
2527 (setq suffixes (cdr suffixes))))
2529 (cond ((file-symlink-p file)
2531 ((and suffix (nth 2 suffix))
2532 ;; We found an uncompression rule.
2533 (with-tramp-progress-reporter
2534 v 0 (format "Uncompressing %s" file)
2535 (when (tramp-send-command-and-check
2536 v (concat (nth 2 suffix) " "
2537 (tramp-shell-quote-argument localname)))
2538 ;; `dired-remove-file' is not defined in XEmacs.
2539 (tramp-compat-funcall 'dired-remove-file file)
2540 (string-match (car suffix) file)
2541 (concat (substring file 0 (match-beginning 0))))))
2543 ;; We don't recognize the file as compressed, so compress it.
2545 (with-tramp-progress-reporter v 0 (format "Compressing %s" file)
2546 (when (tramp-send-command-and-check
2547 v (concat "gzip -f "
2548 (tramp-shell-quote-argument localname)))
2549 ;; `dired-remove-file' is not defined in XEmacs.
2550 (tramp-compat-funcall 'dired-remove-file file)
2551 (cond ((file-exists-p (concat file ".gz"))
2552 (concat file ".gz"))
2553 ((file-exists-p (concat file ".z"))
2557 (defun tramp-sh-handle-insert-directory
2558 (filename switches &optional wildcard full-directory-p)
2559 "Like `insert-directory' for Tramp files."
2560 (setq filename (expand-file-name filename))
2561 (with-parsed-tramp-file-name filename nil
2562 (if (and (featurep 'ls-lisp)
2563 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
2564 (tramp-run-real-handler
2565 'insert-directory (list filename switches wildcard full-directory-p))
2566 (when (stringp switches)
2567 (setq switches (split-string switches)))
2568 (when (and (member "--dired" switches)
2569 (not (tramp-get-ls-command-with-dired v)))
2570 (setq switches (delete "--dired" switches)))
2572 (setq wildcard (tramp-run-real-handler
2573 'file-name-nondirectory (list localname)))
2574 (setq localname (tramp-run-real-handler
2575 'file-name-directory (list localname))))
2576 (unless full-directory-p
2577 (setq switches (add-to-list 'switches "-d" 'append)))
2578 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
2580 (setq switches (concat switches " " wildcard)))
2582 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2583 switches filename (if wildcard "yes" "no")
2584 (if full-directory-p "yes" "no"))
2585 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2586 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2587 (if full-directory-p
2590 (format "%s %s %s 2>/dev/null"
2591 (tramp-get-ls-command v)
2595 (tramp-shell-quote-argument (concat localname ".")))))
2596 (tramp-barf-unless-okay
2598 (format "cd %s" (tramp-shell-quote-argument
2599 (tramp-run-real-handler
2600 'file-name-directory (list localname))))
2602 (tramp-shell-quote-argument
2603 (tramp-run-real-handler 'file-name-directory (list localname))))
2607 (tramp-get-ls-command v)
2611 (tramp-run-real-handler
2612 'file-name-nondirectory (list localname)))))
2614 (tramp-shell-quote-argument
2615 (tramp-run-real-handler
2616 'file-name-nondirectory (list localname)))))))
2617 (let ((beg (point)))
2618 ;; We cannot use `insert-buffer-substring' because the Tramp
2619 ;; buffer changes its contents before insertion due to calling
2620 ;; `expand-file' and alike.
2622 (with-current-buffer (tramp-get-buffer v)
2625 ;; Check for "--dired" output.
2627 (when (looking-at "//SUBDIRED//")
2629 (when (looking-at "//DIRED//\\s-+")
2630 (let ((databeg (match-end 0))
2631 (end (point-at-eol)))
2632 ;; Now read the numeric positions of file names.
2634 (while (< (point) end)
2635 (let ((start (+ beg (read (current-buffer))))
2636 (end (+ beg (read (current-buffer)))))
2637 (if (memq (char-after end) '(?\n ?\ ))
2638 ;; End is followed by \n or by " -> ".
2639 (put-text-property start end 'dired-filename t))))))
2640 ;; Remove trailing lines.
2641 (goto-char (point-at-bol))
2642 (while (looking-at "//")
2644 (delete-region (match-beginning 0) (point)))
2646 ;; Some busyboxes are reluctant to discard colors.
2647 (unless (string-match "color" (tramp-get-connection-property v "ls" ""))
2649 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
2650 (replace-match "")))
2652 ;; The inserted file could be from somewhere else.
2653 (when (and (not wildcard) (not full-directory-p))
2654 (goto-char (point-max))
2655 (when (file-symlink-p filename)
2656 (goto-char (search-backward "->" beg 'noerror)))
2658 (if (zerop (length (file-name-nondirectory filename)))
2660 (file-name-nondirectory filename))
2662 (replace-match (file-relative-name filename) t))
2664 (goto-char (point-max))))))
2666 ;; Canonicalization of file names.
2668 (defun tramp-sh-handle-expand-file-name (name &optional dir)
2669 "Like `expand-file-name' for Tramp files.
2670 If the localname part of the given filename starts with \"/../\" then
2671 the result will be a local, non-Tramp, filename."
2672 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
2673 (setq dir (or dir default-directory "/"))
2674 ;; Unless NAME is absolute, concat DIR and NAME.
2675 (unless (file-name-absolute-p name)
2676 (setq name (concat (file-name-as-directory dir) name)))
2677 ;; If NAME is not a Tramp file, run the real handler.
2678 (if (not (tramp-connectable-p name))
2679 (tramp-run-real-handler 'expand-file-name (list name nil))
2681 (with-parsed-tramp-file-name name nil
2682 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
2683 (setq localname (concat "~/" localname)))
2684 ;; Tilde expansion if necessary. This needs a shell which
2685 ;; groks tilde expansion! The function `tramp-find-shell' is
2686 ;; supposed to find such a shell on the remote host. Please
2687 ;; tell me about it when this doesn't work on your system.
2688 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
2689 (let ((uname (match-string 1 localname))
2690 (fname (match-string 2 localname)))
2691 ;; We cannot simply apply "~/", because under sudo "~/" is
2692 ;; expanded to the local user home directory but to the
2693 ;; root home directory. On the other hand, using always
2694 ;; the default user name for tilde expansion is not
2695 ;; appropriate either, because ssh and companions might
2696 ;; use a user name from the config file.
2697 (when (and (string-equal uname "~")
2698 (string-match "\\`su\\(do\\)?\\'" method))
2699 (setq uname (concat uname user)))
2701 (with-tramp-connection-property v uname
2703 v (format "cd %s; pwd" (tramp-shell-quote-argument uname)))
2704 (with-current-buffer (tramp-get-buffer v)
2705 (goto-char (point-min))
2706 (buffer-substring (point) (point-at-eol)))))
2707 (setq localname (concat uname fname))))
2708 ;; There might be a double slash, for example when "~/"
2709 ;; expands to "/". Remove this.
2710 (while (string-match "//" localname)
2711 (setq localname (replace-match "/" t t localname)))
2712 ;; No tilde characters in file name, do normal
2713 ;; `expand-file-name' (this does "/./" and "/../"). We bind
2714 ;; `directory-sep-char' here for XEmacs on Windows, which would
2715 ;; otherwise use backslash. `default-directory' is bound,
2716 ;; because on Windows there would be problems with UNC shares or
2718 (let ((directory-sep-char ?/)
2719 (default-directory (tramp-compat-temporary-file-directory)))
2720 (tramp-make-tramp-file-name
2722 (tramp-drop-volume-letter
2723 (tramp-run-real-handler
2724 'expand-file-name (list localname)))
2727 ;;; Remote commands:
2729 (defun tramp-sh-handle-executable-find (command)
2730 "Like `executable-find' for Tramp files."
2731 (with-parsed-tramp-file-name default-directory nil
2732 (tramp-find-executable v command (tramp-get-remote-path v) t)))
2734 (defun tramp-process-sentinel (proc event)
2735 "Flush file caches."
2736 (unless (memq (process-status proc) '(run open))
2737 (let ((vec (tramp-get-connection-property proc "vector" nil)))
2739 (tramp-message vec 5 "Sentinel called: `%s' `%s'" proc event)
2740 (tramp-flush-connection-property proc)
2741 (tramp-flush-directory-property vec "")))))
2743 ;; We use BUFFER also as connection buffer during setup. Because of
2744 ;; this, its original contents must be saved, and restored once
2745 ;; connection has been setup.
2746 (defun tramp-sh-handle-start-file-process (name buffer program &rest args)
2747 "Like `start-file-process' for Tramp files."
2748 (with-parsed-tramp-file-name default-directory nil
2749 ;; When PROGRAM is nil, we just provide a tty.
2751 (when (stringp program)
2752 (format "cd %s; exec env PS1=%s %s"
2753 (tramp-shell-quote-argument localname)
2754 ;; Use a human-friendly prompt, for example for `shell'.
2755 (tramp-shell-quote-argument
2757 (file-remote-p default-directory)
2758 tramp-initial-end-of-output))
2759 (mapconcat 'tramp-shell-quote-argument
2760 (cons program args) " "))))
2761 (tramp-process-connection-type
2762 (or (null program) tramp-process-connection-type))
2763 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
2768 ;; BUFFER can be nil. We use a temporary buffer.
2769 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
2770 (while (get-process name1)
2771 ;; NAME must be unique as process name.
2773 name1 (format "%s<%d>" name i)))
2775 ;; Set the new process properties.
2776 (tramp-set-connection-property v "process-name" name)
2777 (tramp-set-connection-property v "process-buffer" buffer)
2779 (with-current-buffer (tramp-get-connection-buffer v)
2783 ;; Activate narrowing in order to save BUFFER
2784 ;; contents. Clear also the modification time;
2785 ;; otherwise we might be interrupted by
2786 ;; `verify-visited-file-modtime'.
2787 (let ((buffer-undo-list t)
2788 (buffer-read-only nil)
2790 (clear-visited-file-modtime)
2791 (narrow-to-region (point-max) (point-max))
2792 ;; We call `tramp-maybe-open-connection', in order
2793 ;; to cleanup the prompt afterwards.
2794 (tramp-maybe-open-connection v)
2796 (delete-region mark (point))
2797 (narrow-to-region (point-max) (point-max))
2800 ;; Send the command.
2801 (tramp-send-command v command nil t) ; nooutput
2802 ;; Check, whether a pty is associated.
2803 (unless (tramp-compat-process-get
2804 (tramp-get-connection-process v) 'remote-tty)
2807 "pty association is not supported for `%s'" name))))
2808 (let ((p (tramp-get-connection-process v)))
2809 ;; Set query flag for this process. We ignore errors,
2810 ;; because the process could have finished already.
2812 (tramp-compat-set-process-query-on-exit-flag p t))
2817 (if (string-match tramp-temp-buffer-name (buffer-name))
2819 (set-process-buffer (tramp-get-connection-process v) nil)
2820 (kill-buffer (current-buffer)))
2821 (set-buffer-modified-p bmp))
2822 (tramp-set-connection-property v "process-name" nil)
2823 (tramp-set-connection-property v "process-buffer" nil))))))
2825 (defun tramp-sh-handle-process-file
2826 (program &optional infile destination display &rest args)
2827 "Like `process-file' for Tramp files."
2828 ;; The implementation is not complete yet.
2829 (when (and (numberp destination) (zerop destination))
2830 (error "Implementation does not handle immediate return"))
2832 (with-parsed-tramp-file-name default-directory nil
2833 (let (command input tmpinput stderr tmpstderr outbuf ret)
2835 (setq command (mapconcat 'tramp-shell-quote-argument
2836 (cons program args) " "))
2839 (setq input "/dev/null")
2840 (setq infile (expand-file-name infile))
2841 (if (tramp-equal-remote default-directory infile)
2842 ;; INFILE is on the same remote host.
2843 (setq input (with-parsed-tramp-file-name infile nil localname))
2844 ;; INFILE must be copied to remote host.
2845 (setq input (tramp-make-tramp-temp-file v)
2846 tmpinput (tramp-make-tramp-file-name method user host input))
2847 (copy-file infile tmpinput t)))
2848 (when input (setq command (format "%s <%s" command input)))
2850 ;; Determine output.
2853 ((bufferp destination)
2854 (setq outbuf destination))
2856 ((stringp destination)
2857 (setq outbuf (get-buffer-create destination)))
2858 ;; (REAL-DESTINATION ERROR-DESTINATION)
2859 ((consp destination)
2862 ((bufferp (car destination))
2863 (setq outbuf (car destination)))
2864 ((stringp (car destination))
2865 (setq outbuf (get-buffer-create (car destination))))
2867 (setq outbuf (current-buffer))))
2870 ((stringp (cadr destination))
2871 (setcar (cdr destination) (expand-file-name (cadr destination)))
2872 (if (tramp-equal-remote default-directory (cadr destination))
2873 ;; stderr is on the same remote host.
2874 (setq stderr (with-parsed-tramp-file-name
2875 (cadr destination) nil localname))
2876 ;; stderr must be copied to remote host. The temporary
2877 ;; file must be deleted after execution.
2878 (setq stderr (tramp-make-tramp-temp-file v)
2879 tmpstderr (tramp-make-tramp-file-name
2880 method user host stderr))))
2881 ;; stderr to be discarded.
2882 ((null (cadr destination))
2883 (setq stderr "/dev/null"))))
2886 (setq outbuf (current-buffer))))
2887 (when stderr (setq command (format "%s 2>%s" command stderr)))
2889 ;; Send the command. It might not return in time, so we protect
2890 ;; it. Call it in a subshell, in order to preserve working
2895 (if (tramp-send-command-and-check
2896 v (format "\\cd %s; %s"
2897 (tramp-shell-quote-argument localname)
2901 ;; We should show the output anyway.
2903 (with-current-buffer outbuf
2905 (with-current-buffer (tramp-get-connection-buffer v)
2907 (when display (display-buffer outbuf))))
2908 ;; When the user did interrupt, we should do it also. We use
2909 ;; return code -1 as marker.
2911 (kill-buffer (tramp-get-connection-buffer v))
2915 (kill-buffer (tramp-get-connection-buffer v))
2918 ;; Provide error file.
2919 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
2921 ;; Cleanup. We remove all file cache values for the connection,
2922 ;; because the remote process could have changed them.
2923 (when tmpinput (delete-file tmpinput))
2925 ;; `process-file-side-effects' has been introduced with GNU
2926 ;; Emacs 23.2. If set to `nil', no remote file will be changed
2927 ;; by `program'. If it doesn't exist, we assume its default
2929 (unless (and (boundp 'process-file-side-effects)
2930 (not (symbol-value 'process-file-side-effects)))
2931 (tramp-flush-directory-property v ""))
2933 ;; Return exit status.
2938 (defun tramp-sh-handle-call-process-region
2939 (start end program &optional delete buffer display &rest args)
2940 "Like `call-process-region' for Tramp files."
2941 (let ((tmpfile (tramp-compat-make-temp-file "")))
2942 (write-region start end tmpfile)
2943 (when delete (delete-region start end))
2945 (apply 'call-process program tmpfile buffer display args)
2946 (delete-file tmpfile))))
2948 (defun tramp-sh-handle-file-local-copy (filename)
2949 "Like `file-local-copy' for Tramp files."
2950 (with-parsed-tramp-file-name filename nil
2951 (unless (file-exists-p filename)
2954 "Cannot make local copy of non-existing file `%s'" filename))
2956 (let* ((size (nth 7 (file-attributes (file-truename filename))))
2957 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
2958 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
2959 (tmpfile (tramp-compat-make-temp-file filename)))
2963 ;; `copy-file' handles direct copy and out-of-band methods.
2964 ((or (tramp-local-host-p v)
2965 (tramp-method-out-of-band-p v size))
2966 (copy-file filename tmpfile t t))
2968 ;; Use inline encoding for file transfer.
2971 (with-tramp-progress-reporter
2972 v 3 (format "Encoding remote file %s" filename)
2973 (tramp-barf-unless-okay
2974 v (format rem-enc (tramp-shell-quote-argument localname))
2975 "Encoding remote file failed"))
2977 (if (functionp loc-dec)
2978 ;; If local decoding is a function, we call it. We
2979 ;; must disable multibyte, because
2980 ;; `uudecode-decode-region' doesn't handle it
2983 (set-buffer-multibyte nil)
2984 (insert-buffer-substring (tramp-get-buffer v))
2985 (with-tramp-progress-reporter
2986 v 3 (format "Decoding remote file %s with function %s"
2988 (funcall loc-dec (point-min) (point-max))
2989 ;; Unset `file-name-handler-alist'. Otherwise,
2990 ;; epa-file gets confused.
2991 (let (file-name-handler-alist
2992 (coding-system-for-write 'binary))
2993 (write-region (point-min) (point-max) tmpfile))))
2995 ;; If tramp-decoding-function is not defined for this
2996 ;; method, we invoke tramp-decoding-command instead.
2997 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
2998 ;; Unset `file-name-handler-alist'. Otherwise,
2999 ;; epa-file gets confused.
3000 (let (file-name-handler-alist
3001 (coding-system-for-write 'binary))
3002 (write-region (point-min) (point-max) tmpfile2))
3003 (with-tramp-progress-reporter
3004 v 3 (format "Decoding remote file %s with command %s"
3007 (tramp-call-local-coding-command
3008 loc-dec tmpfile2 tmpfile)
3009 (delete-file tmpfile2)))))
3011 ;; Set proper permissions.
3012 (set-file-modes tmpfile (tramp-default-file-modes filename))
3013 ;; Set local user ownership.
3014 (tramp-set-file-uid-gid tmpfile)))
3016 ;; Oops, I don't know what to do.
3018 v 'file-error "Wrong method specification for `%s'" method)))
3022 (delete-file tmpfile)
3023 (signal (car err) (cdr err))))
3025 (run-hooks 'tramp-handle-file-local-copy-hook)
3028 ;; This is needed for XEmacs only. Code stolen from files.el.
3029 (defun tramp-sh-handle-insert-file-contents-literally
3030 (filename &optional visit beg end replace)
3031 "Like `insert-file-contents-literally' for Tramp files."
3032 (let ((format-alist nil)
3033 (after-insert-file-functions nil)
3034 (coding-system-for-read 'no-conversion)
3035 (coding-system-for-write 'no-conversion)
3036 (find-buffer-file-type-function
3037 (if (fboundp 'find-buffer-file-type)
3038 (symbol-function 'find-buffer-file-type)
3040 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler))
3041 (inhibit-file-name-operation 'insert-file-contents))
3044 (fset 'find-buffer-file-type (lambda (filename) t))
3045 (insert-file-contents filename visit beg end replace))
3047 (if find-buffer-file-type-function
3048 (fset 'find-buffer-file-type find-buffer-file-type-function)
3049 (fmakunbound 'find-buffer-file-type)))))
3051 (defun tramp-sh-handle-make-auto-save-file-name ()
3052 "Like `make-auto-save-file-name' for Tramp files.
3053 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
3054 (let ((tramp-auto-save-directory tramp-auto-save-directory)
3056 (tramp-subst-strs-in-string
3063 (buffer-file-name))))
3064 ;; File name must be unique. This is ensured with Emacs 22 (see
3065 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
3066 ;; all other cases we must do it ourselves.
3067 (when (boundp 'auto-save-file-name-transforms)
3070 (when (and (string-match (car x) buffer-file-name)
3071 (not (car (cddr x))))
3072 (setq tramp-auto-save-directory
3073 (or tramp-auto-save-directory
3074 (tramp-compat-temporary-file-directory)))))
3075 (symbol-value 'auto-save-file-name-transforms)))
3076 ;; Create directory.
3077 (when tramp-auto-save-directory
3078 (setq buffer-file-name
3079 (expand-file-name buffer-file-name tramp-auto-save-directory))
3080 (unless (file-exists-p tramp-auto-save-directory)
3081 (make-directory tramp-auto-save-directory t)))
3082 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3083 ;; it is not a magic file name operation (since Emacs 22).
3084 ;; We must deactivate it temporarily.
3085 (if (not (ad-is-active 'make-auto-save-file-name))
3086 (tramp-run-real-handler 'make-auto-save-file-name nil)
3088 (ad-deactivate 'make-auto-save-file-name)
3090 (tramp-run-real-handler 'make-auto-save-file-name nil)
3091 (ad-activate 'make-auto-save-file-name)))))
3093 ;; CCC grok LOCKNAME
3094 (defun tramp-sh-handle-write-region
3095 (start end filename &optional append visit lockname confirm)
3096 "Like `write-region' for Tramp files."
3097 (setq filename (expand-file-name filename))
3098 (with-parsed-tramp-file-name filename nil
3099 ;; Following part commented out because we don't know what to do about
3100 ;; file locking, and it does not appear to be a problem to ignore it.
3101 ;; Ange-ftp ignores it, too.
3102 ;; (when (and lockname (stringp lockname))
3103 ;; (setq lockname (expand-file-name lockname)))
3104 ;; (unless (or (eq lockname nil)
3105 ;; (string= lockname filename))
3107 ;; "tramp-sh-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3109 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
3110 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
3111 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
3112 (tramp-error v 'file-error "File not overwritten")))
3114 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
3115 (tramp-get-remote-uid v 'integer)))
3116 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
3117 (tramp-get-remote-gid v 'integer))))
3119 (if (and (tramp-local-host-p v)
3120 ;; `file-writable-p' calls `file-expand-file-name'. We
3121 ;; cannot use `tramp-run-real-handler' therefore.
3122 (let (file-name-handler-alist)
3124 (file-writable-p (file-name-directory localname))
3125 (or (file-directory-p localname)
3126 (file-writable-p localname)))))
3127 ;; Short track: if we are on the local host, we can run directly.
3128 (tramp-run-real-handler
3130 (list start end localname append 'no-message lockname confirm))
3132 (let* ((modes (save-excursion (tramp-default-file-modes filename)))
3133 ;; We use this to save the value of
3134 ;; `last-coding-system-used' after writing the tmp
3135 ;; file. At the end of the function, we set
3136 ;; `last-coding-system-used' to this saved value. This
3137 ;; way, any intermediary coding systems used while
3138 ;; talking to the remote shell or suchlike won't hose
3139 ;; this variable. This approach was snarfed from
3142 ;; Write region into a tmp file. This isn't really
3143 ;; needed if we use an encoding function, but currently
3144 ;; we use it always because this makes the logic
3145 ;; simpler. We must also set `temporary-file-directory',
3146 ;; because it could point to a remote directory.
3147 (temporary-file-directory
3148 (tramp-compat-temporary-file-directory))
3149 (tmpfile (or tramp-temp-buffer-file-name
3150 (tramp-compat-make-temp-file filename))))
3152 ;; If `append' is non-nil, we copy the file locally, and let
3153 ;; the native `write-region' implementation do the job.
3154 (when append (copy-file filename tmpfile 'ok))
3156 ;; We say `no-message' here because we don't want the
3157 ;; visited file modtime data to be clobbered from the temp
3158 ;; file. We call `set-visited-file-modtime' ourselves later
3159 ;; on. We must ensure that `file-coding-system-alist'
3160 ;; matches `tmpfile'.
3161 (let (file-name-handler-alist
3162 (file-coding-system-alist
3163 (tramp-find-file-name-coding-system-alist filename tmpfile)))
3165 (tramp-run-real-handler
3167 (list start end tmpfile append 'no-message lockname confirm))
3169 (setq tramp-temp-buffer-file-name nil)
3170 (delete-file tmpfile)
3171 (signal (car err) (cdr err))))
3173 ;; Now, `last-coding-system-used' has the right value. Remember it.
3174 (when (boundp 'last-coding-system-used)
3175 (setq coding-system-used
3176 (symbol-value 'last-coding-system-used))))
3178 ;; The permissions of the temporary file should be set. If
3179 ;; filename does not exist (eq modes nil) it has been
3180 ;; renamed to the backup file. This case `save-buffer'
3181 ;; handles permissions.
3182 ;; Ensure that it is still readable.
3186 (logior (or modes 0) (tramp-compat-octal-to-decimal "0400"))))
3188 ;; This is a bit lengthy due to the different methods
3189 ;; possible for file transfer. First, we check whether the
3190 ;; method uses an rcp program. If so, we call it.
3191 ;; Otherwise, both encoding and decoding command must be
3192 ;; specified. However, if the method _also_ specifies an
3193 ;; encoding function, then that is used for encoding the
3194 ;; contents of the tmp file.
3195 (let* ((size (nth 7 (file-attributes tmpfile)))
3196 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
3197 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
3199 ;; `copy-file' handles direct copy and out-of-band methods.
3200 ((or (tramp-local-host-p v)
3201 (tramp-method-out-of-band-p v size))
3202 (if (and (not (stringp start))
3203 (= (or end (point-max)) (point-max))
3204 (= (or start (point-min)) (point-min))
3205 (tramp-get-method-parameter
3206 method 'tramp-copy-keep-tmpfile))
3208 (setq tramp-temp-buffer-file-name tmpfile)
3210 ;; We keep the local file for performance
3211 ;; reasons, useful for "rsync".
3212 (copy-file tmpfile filename t)
3214 (setq tramp-temp-buffer-file-name nil)
3215 (delete-file tmpfile)
3216 (signal (car err) (cdr err)))))
3217 (setq tramp-temp-buffer-file-name nil)
3218 ;; Don't rename, in order to keep context in SELinux.
3220 (copy-file tmpfile filename t)
3221 (delete-file tmpfile))))
3223 ;; Use inline file transfer.
3228 (set-buffer-multibyte nil)
3229 ;; Use encoding function or command.
3230 (if (functionp loc-enc)
3231 (with-tramp-progress-reporter
3232 v 3 (format "Encoding region using function `%s'"
3234 (let ((coding-system-for-read 'binary))
3235 (insert-file-contents-literally tmpfile))
3236 ;; The following `let' is a workaround for the
3237 ;; base64.el that comes with pgnus-0.84. If
3238 ;; both of the following conditions are
3239 ;; satisfied, it tries to write to a local
3240 ;; file in default-directory, but at this
3241 ;; point, default-directory is remote.
3242 ;; (`call-process-region' can't write to
3243 ;; remote files, it seems.) The file in
3244 ;; question is a tmp file anyway.
3245 (let ((default-directory
3246 (tramp-compat-temporary-file-directory)))
3247 (funcall loc-enc (point-min) (point-max))))
3249 (with-tramp-progress-reporter
3250 v 3 (format "Encoding region using command `%s'"
3252 (unless (zerop (tramp-call-local-coding-command
3256 (concat "Cannot write to `%s', "
3257 "local encoding command `%s' failed")
3258 filename loc-enc))))
3260 ;; Send buffer into remote decoding command which
3261 ;; writes to remote file. Because this happens on
3262 ;; the remote host, we cannot use the function.
3263 (with-tramp-progress-reporter
3265 (format "Decoding region into remote file %s" filename)
3266 (goto-char (point-max))
3267 (unless (bolp) (newline))
3271 (concat rem-dec " <<'EOF'\n%sEOF")
3272 (tramp-shell-quote-argument localname)
3274 (tramp-barf-unless-okay
3276 "Couldn't write region to `%s', decode using `%s' failed"
3278 ;; When `file-precious-flag' is set, the region is
3279 ;; written to a temporary file. Check that the
3280 ;; checksum is equal to that from the local tmpfile.
3281 (when file-precious-flag
3284 ;; cksum runs locally, if possible.
3285 (zerop (tramp-compat-call-process "cksum" tmpfile t))
3286 ;; cksum runs remotely.
3287 (tramp-send-command-and-check
3290 "cksum <%s" (tramp-shell-quote-argument localname)))
3291 ;; ... they are different.
3295 (with-current-buffer (tramp-get-buffer v)
3299 (concat "Couldn't write region to `%s',"
3300 " decode using `%s' failed")
3301 filename rem-dec)))))
3304 (delete-file tmpfile)))
3306 ;; That's not expected.
3310 (concat "Method `%s' should specify both encoding and "
3311 "decoding command or an rcp program")
3314 ;; Make `last-coding-system-used' have the right value.
3315 (when coding-system-used
3316 (set 'last-coding-system-used coding-system-used))))
3318 (tramp-flush-file-property v (file-name-directory localname))
3319 (tramp-flush-file-property v localname)
3321 ;; We must protect `last-coding-system-used', now we have set it
3322 ;; to its correct value.
3323 (let (last-coding-system-used (need-chown t))
3324 ;; Set file modification time.
3325 (when (or (eq visit t) (stringp visit))
3326 (let ((file-attr (tramp-compat-file-attributes filename 'integer)))
3327 (set-visited-file-modtime
3328 ;; We must pass modtime explicitly, because filename can
3329 ;; be different from (buffer-file-name), f.e. if
3330 ;; `file-precious-flag' is set.
3332 (when (and (= (nth 2 file-attr) uid)
3333 (= (nth 3 file-attr) gid))
3334 (setq need-chown nil))))
3336 ;; Set the ownership.
3338 (tramp-set-file-uid-gid filename uid gid))
3339 (when (or (eq visit t) (null visit) (stringp visit))
3340 (tramp-message v 0 "Wrote %s" filename))
3341 (run-hooks 'tramp-handle-write-region-hook)))))
3343 (defvar tramp-vc-registered-file-names nil
3344 "List used to collect file names, which are checked during `vc-registered'.")
3346 ;; VC backends check for the existence of various different special
3347 ;; files. This is very time consuming, because every single check
3348 ;; requires a remote command (the file cache must be invalidated).
3349 ;; Therefore, we apply a kind of optimization. We install the file
3350 ;; name handler `tramp-vc-file-name-handler', which does nothing but
3351 ;; remembers all file names for which `file-exists-p' or
3352 ;; `file-readable-p' has been applied. A first run of `vc-registered'
3353 ;; is performed. Afterwards, a script is applied for all collected
3354 ;; file names, using just one remote command. The result of this
3355 ;; script is used to fill the file cache with actual values. Now we
3356 ;; can reset the file name handlers, and we make a second run of
3357 ;; `vc-registered', which returns the expected result without sending
3358 ;; any other remote command.
3359 (defun tramp-sh-handle-vc-registered (file)
3360 "Like `vc-registered' for Tramp files."
3361 (tramp-compat-with-temp-message ""
3362 (with-parsed-tramp-file-name file nil
3363 (with-tramp-progress-reporter
3364 v 3 (format "Checking `vc-registered' for %s" file)
3366 ;; There could be new files, created by the vc backend. We
3367 ;; cannot reuse the old cache entries, therefore.
3368 (let (tramp-vc-registered-file-names
3369 (remote-file-name-inhibit-cache (current-time))
3370 (file-name-handler-alist
3371 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
3373 ;; Here we collect only file names, which need an operation.
3374 (ignore-errors (tramp-run-real-handler 'vc-registered (list file)))
3375 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
3377 ;; Send just one command, in order to fill the cache.
3378 (when tramp-vc-registered-file-names
3379 (tramp-maybe-send-script
3381 (format tramp-vc-registered-read-file-names
3382 (tramp-get-file-exists-command v)
3383 (format "%s -r" (tramp-get-test-command v)))
3384 "tramp_vc_registered_read_file_names")
3388 (tramp-send-command-and-read
3391 "tramp_vc_registered_read_file_names <<'EOF'\n%s\nEOF\n"
3392 (mapconcat 'tramp-shell-quote-argument
3393 tramp-vc-registered-file-names
3396 (tramp-set-file-property
3397 v (car elt) (cadr elt) (cadr (cdr elt))))))
3399 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3400 ;; calls shall be answered from the file cache. We unset
3401 ;; `process-file-side-effects' in order to keep the cache when
3402 ;; `process-file' calls appear.
3403 (let (process-file-side-effects)
3404 (tramp-run-real-handler 'vc-registered (list file)))))))
3406 ;;;###tramp-autoload
3407 (defun tramp-sh-file-name-handler (operation &rest args)
3408 "Invoke remote-shell Tramp file name handler.
3409 Fall back to normal file name handler if no Tramp handler exists."
3410 (when (and tramp-locked (not tramp-locker))
3411 (setq tramp-locked nil)
3412 (signal 'file-error (list "Forbidden reentrant call of Tramp")))
3413 (let ((tl tramp-locked))
3416 (setq tramp-locked t)
3417 (let ((tramp-locker t))
3419 (let ((fn (assoc operation tramp-sh-file-name-handler-alist)))
3421 (apply (cdr fn) args)
3422 (tramp-run-real-handler operation args))))))
3423 (setq tramp-locked tl))))
3425 (defun tramp-vc-file-name-handler (operation &rest args)
3426 "Invoke special file name handler, which collects files to be handled."
3429 (tramp-replace-environment-variables
3430 (apply 'tramp-file-name-for-operation operation args)))
3431 (fn (assoc operation tramp-sh-file-name-handler-alist)))
3432 (with-parsed-tramp-file-name filename nil
3434 ;; That's what we want: file names, for which checks are
3435 ;; applied. We assume that VC uses only `file-exists-p' and
3436 ;; `file-readable-p' checks; otherwise we must extend the
3437 ;; list. We do not perform any action, but return nil, in
3438 ;; order to keep `vc-registered' running.
3439 ((and fn (memq operation '(file-exists-p file-readable-p)))
3440 (add-to-list 'tramp-vc-registered-file-names localname 'append)
3442 ;; `process-file' and `start-file-process' shall be ignored.
3443 ((and fn (eq operation 'process-file) 0))
3444 ((and fn (eq operation 'start-file-process) nil))
3445 ;; Tramp file name handlers like `expand-file-name'. They
3447 (fn (save-match-data (apply (cdr fn) args)))
3448 ;; Default file name handlers, we don't care.
3449 (t (tramp-run-real-handler operation args)))))))
3451 ;;; Internal Functions:
3453 (defun tramp-maybe-send-script (vec script name)
3454 "Define in remote shell function NAME implemented as SCRIPT.
3455 Only send the definition if it has not already been done."
3456 ;; We cannot let-bind (tramp-get-connection-process vec) because it
3458 (let ((scripts (tramp-get-connection-property
3459 (tramp-get-connection-process vec) "scripts" nil)))
3460 (unless (member name scripts)
3461 (with-tramp-progress-reporter vec 5 (format "Sending script `%s'" name)
3462 ;; The script could contain a call of Perl. This is masked with `%s'.
3463 (tramp-barf-unless-okay
3465 (format "%s () {\n%s\n}" name
3466 (format script (tramp-get-remote-perl vec)))
3467 "Script %s sending failed" name)
3468 (tramp-set-connection-property
3469 (tramp-get-connection-process vec) "scripts" (cons name scripts))))))
3471 (defun tramp-set-auto-save ()
3472 (when (and ;; ange-ftp has its own auto-save mechanism
3473 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
3474 'tramp-sh-file-name-handler)
3476 (auto-save-mode 1)))
3477 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
3478 (add-hook 'tramp-unload-hook
3480 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
3482 (defun tramp-run-test (switch filename)
3483 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3484 Returns the exit code of the `test' program."
3485 (with-parsed-tramp-file-name filename nil
3486 (tramp-send-command-and-check
3490 (tramp-get-test-command v)
3492 (tramp-shell-quote-argument localname)))))
3494 (defun tramp-run-test2 (format-string file1 file2)
3495 "Run `test'-like program on the remote system, given FILE1, FILE2.
3496 FORMAT-STRING contains the program name, switches, and place holders.
3497 Returns the exit code of the `test' program. Barfs if the methods,
3498 hosts, or files, disagree."
3499 (unless (tramp-equal-remote file1 file2)
3500 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
3503 "tramp-run-test2 only implemented for same method, user, host")))
3504 (with-parsed-tramp-file-name file1 v1
3505 (with-parsed-tramp-file-name file1 v2
3506 (tramp-send-command-and-check
3508 (format format-string
3509 (tramp-shell-quote-argument v1-localname)
3510 (tramp-shell-quote-argument v2-localname))))))
3512 (defun tramp-find-executable
3513 (vec progname dirlist &optional ignore-tilde ignore-path)
3514 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3515 First arg VEC specifies the connection, PROGNAME is the program
3516 to search for, and DIRLIST gives the list of directories to
3517 search. If IGNORE-TILDE is non-nil, directory names starting
3518 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3521 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3523 This function expects to be in the right *tramp* buffer."
3524 (with-current-buffer (tramp-get-connection-buffer vec)
3526 ;; Check whether the executable is in $PATH. "which(1)" does not
3527 ;; report always a correct error code; therefore we check the
3528 ;; number of words it returns.
3530 (tramp-send-command vec (format "which \\%s | wc -w" progname))
3531 (goto-char (point-min))
3532 (if (looking-at "^\\s-*1$")
3533 (setq result (concat "\\" progname))))
3536 ;; Remove all ~/foo directories from dirlist. In XEmacs,
3537 ;; `remove' is in CL, and we want to avoid CL dependencies.
3540 (setq d (car dirlist))
3541 (setq dirlist (cdr dirlist))
3542 (unless (char-equal ?~ (aref d 0))
3543 (setq newdl (cons d newdl))))
3544 (setq dirlist (nreverse newdl))))
3547 (format (concat "while read d; "
3548 "do if test -x $d/%s -a -f $d/%s; "
3549 "then echo tramp_executable $d/%s; "
3550 "break; fi; done <<'EOF'\n"
3552 progname progname progname (mapconcat 'identity dirlist "\n")))
3553 (goto-char (point-max))
3554 (when (search-backward "tramp_executable " nil t)
3555 (skip-chars-forward "^ ")
3556 (skip-chars-forward " ")
3557 (setq result (buffer-substring (point) (point-at-eol)))))
3560 (defun tramp-set-remote-path (vec)
3561 "Sets the remote environment PATH to existing directories.
3562 I.e., for each directory in `tramp-remote-path', it is tested
3563 whether it exists and if so, it is added to the environment
3565 (tramp-message vec 5 (format "Setting $PATH environment variable"))
3567 vec (format "PATH=%s; export PATH"
3568 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
3570 ;; ------------------------------------------------------------
3571 ;; -- Communication with external shell --
3572 ;; ------------------------------------------------------------
3574 (defun tramp-find-file-exists-command (vec)
3575 "Find a command on the remote host for checking if a file exists.
3576 Here, we are looking for a command which has zero exit status if the
3577 file exists and nonzero exit status otherwise."
3578 (let ((existing "/")
3580 (tramp-shell-quote-argument "/ this file does not exist "))
3582 ;; The algorithm is as follows: we try a list of several commands.
3583 ;; For each command, we first run `$cmd /' -- this should return
3584 ;; true, as the root directory always exists. And then we run
3585 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
3586 ;; does not exist. This should return false. We use the first
3587 ;; command we find that seems to work.
3588 ;; The list of commands to try is as follows:
3589 ;; `ls -d' This works on most systems, but NetBSD 1.4
3590 ;; has a bug: `ls' always returns zero exit
3591 ;; status, even for files which don't exist.
3592 ;; `test -e' Some Bourne shells have a `test' builtin
3593 ;; which does not know the `-e' option.
3594 ;; `/bin/test -e' For those, the `test' binary on disk normally
3595 ;; provides the option. Alas, the binary
3596 ;; is sometimes `/bin/test' and sometimes it's
3598 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3601 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
3602 (tramp-send-command-and-check
3603 vec (format "%s %s" result existing))
3604 (not (tramp-send-command-and-check
3605 vec (format "%s %s" result nonexistent)))))
3607 (and (setq result "/bin/test -e")
3608 (tramp-send-command-and-check
3609 vec (format "%s %s" result existing))
3610 (not (tramp-send-command-and-check
3611 vec (format "%s %s" result nonexistent)))))
3613 (and (setq result "/usr/bin/test -e")
3614 (tramp-send-command-and-check
3615 vec (format "%s %s" result existing))
3616 (not (tramp-send-command-and-check
3617 vec (format "%s %s" result nonexistent)))))
3619 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
3620 (tramp-send-command-and-check
3621 vec (format "%s %s" result existing))
3622 (not (tramp-send-command-and-check
3623 vec (format "%s %s" result nonexistent))))))
3625 vec 'file-error "Couldn't find command to check if file exists"))
3628 (defun tramp-open-shell (vec shell)
3629 "Opens shell SHELL."
3630 (with-tramp-progress-reporter
3631 vec 5 (format "Opening remote shell `%s'" shell)
3632 ;; Find arguments for this shell.
3633 (let ((tramp-end-of-output tramp-initial-end-of-output)
3634 (alist tramp-sh-extra-args)
3636 (while (and alist (null extra-args))
3637 (setq item (pop alist))
3638 (when (string-match (car item) shell)
3639 (setq extra-args (cdr item))))
3642 "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s"
3643 (tramp-shell-quote-argument tramp-end-of-output)
3644 shell (or extra-args ""))
3646 (tramp-set-connection-property
3647 (tramp-get-connection-process vec) "remote-shell" shell)
3650 vec (format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output)) t)
3651 (tramp-send-command vec "PS2=''" t)
3652 (tramp-send-command vec "PS3=''" t)
3653 (tramp-send-command vec "PROMPT_COMMAND=''" t)))
3655 (defun tramp-find-shell (vec)
3656 "Opens a shell on the remote host which groks tilde expansion."
3657 (with-current-buffer (tramp-get-buffer vec)
3658 (let ((default-shell
3660 (tramp-get-connection-property
3661 (tramp-get-connection-process vec) "remote-shell" nil)
3662 (tramp-get-method-parameter
3663 (tramp-file-name-method vec) 'tramp-remote-shell)))
3666 (with-tramp-connection-property vec "remote-shell"
3667 ;; CCC: "root" does not exist always, see QNAP 459.
3668 ;; Which check could we apply instead?
3669 (tramp-send-command vec "echo ~root" t)
3670 (if (or (string-match "^~root$" (buffer-string))
3671 ;; The default shell (ksh93) of OpenSolaris and
3672 ;; Solaris is buggy. We've got reports for
3673 ;; "SunOS 5.10" and "SunOS 5.11" so far.
3674 (string-match (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3675 (tramp-get-connection-property
3678 (or (tramp-find-executable
3679 vec "bash" (tramp-get-remote-path vec) t t)
3680 (tramp-find-executable
3681 vec "ksh" (tramp-get-remote-path vec) t t)
3682 ;; Maybe it works at least for some other commands.
3688 "Couldn't find a remote shell which groks tilde "
3689 "expansion, using `%s'")
3694 ;; Open a new shell if needed.
3695 (unless (string-equal shell default-shell)
3697 vec 5 "Starting remote shell `%s' for tilde expansion" shell)
3698 (tramp-open-shell vec shell))
3700 ;; Busyboxes tend to behave strange. We check for the existence.
3701 (with-tramp-connection-property vec "busybox"
3702 (tramp-send-command vec (format "%s --version" shell) t)
3703 (let ((case-fold-search t))
3704 (and (string-match "busybox" (buffer-string)) t))))))
3706 ;; Utility functions.
3708 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
3709 "Wait for shell prompt and barf if none appears.
3710 Looks at process PROC to see if a shell prompt appears in TIMEOUT
3711 seconds. If not, it produces an error message with the given ERROR-ARGS."
3713 (tramp-wait-for-regexp
3716 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
3717 (apply 'tramp-error-with-buffer nil proc 'file-error error-args)))
3719 (defun tramp-open-connection-setup-interactive-shell (proc vec)
3720 "Set up an interactive shell.
3721 Mainly sets the prompt and the echo correctly. PROC is the shell
3722 process to set up. VEC specifies the connection."
3723 (let ((tramp-end-of-output tramp-initial-end-of-output))
3724 ;; It is useful to set the prompt in the following command because
3725 ;; some people have a setting for $PS1 which /bin/sh doesn't know
3726 ;; about and thus /bin/sh will display a strange prompt. For
3727 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
3728 ;; display the current working directory but /bin/sh will display
3729 ;; a dollar sign. The following command line sets $PS1 to a sane
3730 ;; value, and works under Bourne-ish shells as well as csh-like
3731 ;; shells. Daniel Pittman reports that the unusual positioning of
3732 ;; the single quotes makes it work under `rc', too. We also unset
3733 ;; the variable $ENV because that is read by some sh
3734 ;; implementations (eg, bash when called as sh) on startup; this
3735 ;; way, we avoid the startup file clobbering $PS1. $PROMPT_COMMAND
3736 ;; is another way to set the prompt in /bin/bash, it must be
3737 ;; discarded as well.
3740 (or (tramp-get-connection-property vec "remote-shell" nil)
3741 (tramp-get-method-parameter
3742 (tramp-file-name-method vec) 'tramp-remote-shell)))
3745 (tramp-message vec 5 "Setting up remote shell environment")
3746 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
3747 ;; Check whether the echo has really been disabled. Some
3748 ;; implementations, like busybox of embedded GNU/Linux, don't
3749 ;; support disabling.
3750 (tramp-send-command vec "echo foo" t)
3751 (with-current-buffer (process-buffer proc)
3752 (goto-char (point-min))
3753 (when (looking-at "echo foo")
3754 (tramp-set-connection-property proc "remote-echo" t)
3755 (tramp-message vec 5 "Remote echo still on. Ok.")
3756 ;; Make sure backspaces and their echo are enabled and no line
3757 ;; width magic interferes with them.
3758 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
3760 (tramp-message vec 5 "Setting shell prompt")
3762 vec (format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output)) t)
3763 (tramp-send-command vec "PS2=''" t)
3764 (tramp-send-command vec "PS3=''" t)
3765 (tramp-send-command vec "PROMPT_COMMAND=''" t)
3767 ;; Try to set up the coding system correctly.
3768 ;; CCC this can't be the right way to do it. Hm.
3769 (tramp-message vec 5 "Determining coding system")
3770 (tramp-send-command vec "echo foo ; echo bar" t)
3771 (with-current-buffer (process-buffer proc)
3772 (goto-char (point-min))
3773 (if (featurep 'mule)
3774 ;; Use MULE to select the right EOL convention for communicating
3775 ;; with the process.
3776 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc)
3777 (cons 'undecided 'undecided)))
3778 cs-decode cs-encode)
3779 (when (symbolp cs) (setq cs (cons cs cs)))
3780 (setq cs-decode (car cs))
3781 (setq cs-encode (cdr cs))
3782 (unless cs-decode (setq cs-decode 'undecided))
3783 (unless cs-encode (setq cs-encode 'undecided))
3784 (setq cs-encode (tramp-compat-coding-system-change-eol-conversion
3786 (when (search-forward "\r" nil t)
3787 (setq cs-decode (tramp-compat-coding-system-change-eol-conversion
3789 (tramp-compat-funcall
3790 'set-buffer-process-coding-system cs-decode cs-encode)
3792 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
3793 ;; Look for ^M and do something useful if found.
3794 (when (search-forward "\r" nil t)
3795 ;; We have found a ^M but cannot frob the process coding system
3796 ;; because we're running on a non-MULE Emacs. Let's try
3798 (tramp-send-command vec "stty -onlcr" t))))
3800 (tramp-send-command vec "set +o vi +o emacs" t)
3802 ;; Check whether the output of "uname -sr" has been changed. If
3803 ;; yes, this is a strong indication that we must expire all
3804 ;; connection properties. We start again with
3805 ;; `tramp-maybe-open-connection', it will be caught there.
3806 (tramp-message vec 5 "Checking system information")
3807 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
3809 (tramp-set-connection-property
3811 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
3812 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
3816 "Connection reset, because remote host changed from `%s' to `%s'"
3817 old-uname new-uname)
3818 (throw 'uname-changed (tramp-maybe-open-connection vec))))
3820 ;; Check whether the remote host suffers from buggy
3821 ;; `send-process-string'. This is known for FreeBSD (see comment in
3822 ;; `send_process', file process.c). I've tested sending 624 bytes
3823 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
3824 ;; this host type is detected locally. It cannot handle remote
3826 (with-tramp-connection-property proc "chunksize"
3828 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
3832 vec 5 "Checking remote host type for `send-process-string' bug")
3834 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
3837 ;; Set remote PATH variable.
3838 (tramp-set-remote-path vec)
3840 ;; Search for a good shell before searching for a command which
3841 ;; checks if a file exists. This is done because Tramp wants to use
3842 ;; "test foo; echo $?" to check if various conditions hold, and
3843 ;; there are buggy /bin/sh implementations which don't execute the
3844 ;; "echo $?" part if the "test" part has an error. In particular,
3845 ;; the OpenSolaris /bin/sh is a problem. There are also other
3846 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
3847 ;; in function declarations, or changing HISTFILE in place.
3848 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
3850 (tramp-find-shell vec)
3852 ;; Disable unexpected output.
3853 (tramp-send-command vec "mesg n; biff n" t)
3855 ;; IRIX64 bash expands "!" even when in single quotes. This
3856 ;; destroys our shell functions, we must disable it. See
3857 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
3858 (when (string-match "^IRIX64" (tramp-get-connection-property vec "uname" ""))
3859 (tramp-send-command vec "set +H" t))
3861 ;; On BSD-like systems, ?\t is expanded to spaces. Suppress this.
3862 (when (string-match "BSD\\|Darwin"
3863 (tramp-get-connection-property vec "uname" ""))
3864 (tramp-send-command vec "stty -oxtabs" t))
3866 ;; Set `remote-tty' process property.
3867 (let ((tty (tramp-send-command-and-read vec "echo \\\"`tty`\\\"" 'noerror)))
3868 (unless (zerop (length tty))
3869 (tramp-compat-process-put proc 'remote-tty tty)))
3871 ;; Dump stty settings in the traces.
3872 (when (>= tramp-verbose 9)
3873 (tramp-send-command vec "stty -a" t))
3875 ;; Set the environment.
3876 (tramp-message vec 5 "Setting default environment")
3878 (let ((env (copy-sequence tramp-remote-process-environment))
3881 (setq item (tramp-compat-split-string (car env) "="))
3882 (setcdr item (mapconcat 'identity (cdr item) "="))
3883 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
3885 vec (format "%s=%s; export %s" (car item) (cdr item) (car item)) t)
3886 (push (car item) unset))
3887 (setq env (cdr env)))
3890 vec (format "unset %s" (mapconcat 'identity unset " ")) t))))
3892 ;; CCC: We should either implement a Perl version of base64 encoding
3893 ;; and decoding. Then we just use that in the last item. The other
3894 ;; alternative is to use the Perl version of UU encoding. But then
3895 ;; we need a Lisp version of uuencode.
3897 ;; Old text from documentation of tramp-methods:
3898 ;; Using a uuencode/uudecode inline method is discouraged, please use one
3899 ;; of the base64 methods instead since base64 encoding is much more
3900 ;; reliable and the commands are more standardized between the different
3901 ;; Unix versions. But if you can't use base64 for some reason, please
3902 ;; note that the default uudecode command does not work well for some
3903 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
3904 ;; the following command for uudecode:
3906 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
3908 ;; For Irix, no solution is known yet.
3910 (autoload 'uudecode-decode-region "uudecode")
3912 (defconst tramp-local-coding-commands
3913 '((b64 base64-encode-region base64-decode-region)
3914 (uu tramp-uuencode-region uudecode-decode-region)
3916 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
3917 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
3918 "List of local coding commands for inline transfer.
3919 Each item is a list that looks like this:
3921 \(FORMAT ENCODING DECODING\)
3923 FORMAT is symbol describing the encoding/decoding format. It can be
3924 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3926 ENCODING and DECODING can be strings, giving commands, or symbols,
3927 giving functions. If they are strings, then they can contain
3928 the \"%s\" format specifier. If that specifier is present, the input
3929 filename will be put into the command line at that spot. If the
3930 specifier is not present, the input should be read from standard
3933 If they are functions, they will be called with two arguments, start
3934 and end of region, and are expected to replace the region contents
3935 with the encoded or decoded results, respectively.")
3937 (defconst tramp-remote-coding-commands
3938 '((b64 "base64" "base64 -d -i")
3939 ;; "-i" is more robust with older base64 from GNU coreutils.
3940 ;; However, I don't know whether all base64 versions do supports
3942 (b64 "base64" "base64 -d")
3943 (b64 "mimencode -b" "mimencode -u -b")
3944 (b64 "mmencode -b" "mmencode -u -b")
3945 (b64 "recode data..base64" "recode base64..data")
3946 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
3947 (b64 tramp-perl-encode tramp-perl-decode)
3948 (uu "uuencode xxx" "uudecode -o /dev/stdout" "test -c /dev/stdout")
3949 (uu "uuencode xxx" "uudecode -o -")
3950 (uu "uuencode xxx" "uudecode -p")
3951 (uu "uuencode xxx" tramp-uudecode)
3953 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
3954 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
3955 "List of remote coding commands for inline transfer.
3956 Each item is a list that looks like this:
3958 \(FORMAT ENCODING DECODING [TEST]\)
3960 FORMAT is symbol describing the encoding/decoding format. It can be
3961 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3963 ENCODING and DECODING can be strings, giving commands, or symbols,
3964 giving variables. If they are strings, then they can contain
3965 the \"%s\" format specifier. If that specifier is present, the input
3966 filename will be put into the command line at that spot. If the
3967 specifier is not present, the input should be read from standard
3970 If they are variables, this variable is a string containing a Perl
3971 implementation for this functionality. This Perl program will be transferred
3972 to the remote host, and it is available as shell function with the same name.
3974 The optional TEST command can be used for further tests, whether
3975 ENCODING and DECODING are applicable.")
3977 (defun tramp-find-inline-encoding (vec)
3978 "Find an inline transfer encoding that works.
3979 Goes through the list `tramp-local-coding-commands' and
3980 `tramp-remote-coding-commands'."
3982 (let ((local-commands tramp-local-coding-commands)
3984 (p (tramp-get-connection-process vec))
3985 loc-enc loc-dec rem-enc rem-dec rem-test litem ritem found)
3986 (while (and local-commands (not found))
3987 (setq litem (pop local-commands))
3988 (catch 'wont-work-local
3989 (let ((format (nth 0 litem))
3990 (remote-commands tramp-remote-coding-commands))
3991 (setq loc-enc (nth 1 litem))
3992 (setq loc-dec (nth 2 litem))
3993 ;; If the local encoder or decoder is a string, the
3994 ;; corresponding command has to work locally.
3995 (if (not (stringp loc-enc))
3997 vec 5 "Checking local encoding function `%s'" loc-enc)
3999 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
4000 (unless (zerop (tramp-call-local-coding-command
4002 (throw 'wont-work-local nil)))
4003 (if (not (stringp loc-dec))
4005 vec 5 "Checking local decoding function `%s'" loc-dec)
4007 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
4008 (unless (zerop (tramp-call-local-coding-command
4010 (throw 'wont-work-local nil)))
4011 ;; Search for remote coding commands with the same format
4012 (while (and remote-commands (not found))
4013 (setq ritem (pop remote-commands))
4014 (catch 'wont-work-remote
4015 (when (equal format (nth 0 ritem))
4016 (setq rem-enc (nth 1 ritem))
4017 (setq rem-dec (nth 2 ritem))
4018 (setq rem-test (nth 3 ritem))
4019 ;; Check the remote test command if exists.
4020 (when (stringp rem-test)
4022 vec 5 "Checking remote test command `%s'" rem-test)
4023 (unless (tramp-send-command-and-check vec rem-test t)
4024 (throw 'wont-work-remote nil)))
4025 ;; Check if remote encoding and decoding commands can be
4026 ;; called remotely with null input and output. This makes
4027 ;; sure there are no syntax errors and the command is really
4028 ;; found. Note that we do not redirect stdout to /dev/null,
4029 ;; for two reasons: when checking the decoding command, we
4030 ;; actually check the output it gives. And also, when
4031 ;; redirecting "mimencode" output to /dev/null, then as root
4032 ;; it might change the permissions of /dev/null!
4033 (when (not (stringp rem-enc))
4034 (let ((name (symbol-name rem-enc)))
4035 (while (string-match (regexp-quote "-") name)
4036 (setq name (replace-match "_" nil t name)))
4037 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
4038 (setq rem-enc name)))
4041 "Checking remote encoding command `%s' for sanity" rem-enc)
4042 (unless (tramp-send-command-and-check
4043 vec (format "%s </dev/null" rem-enc) t)
4044 (throw 'wont-work-remote nil))
4046 (when (not (stringp rem-dec))
4047 (let ((name (symbol-name rem-dec)))
4048 (while (string-match (regexp-quote "-") name)
4049 (setq name (replace-match "_" nil t name)))
4050 (tramp-maybe-send-script vec (symbol-value rem-dec) name)
4051 (setq rem-dec name)))
4054 "Checking remote decoding command `%s' for sanity" rem-dec)
4055 (unless (tramp-send-command-and-check
4057 (format "echo %s | %s | %s" magic rem-enc rem-dec)
4059 (throw 'wont-work-remote nil))
4061 (with-current-buffer (tramp-get-buffer vec)
4062 (goto-char (point-min))
4063 (unless (looking-at (regexp-quote magic))
4064 (throw 'wont-work-remote nil)))
4066 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
4067 (setq rem-enc (nth 1 ritem))
4068 (setq rem-dec (nth 2 ritem))
4069 (setq found t)))))))
4071 ;; Did we find something?
4074 vec 'file-error "Couldn't find an inline transfer encoding"))
4076 ;; Set connection properties. Since the commands are risky (due
4077 ;; to output direction), we cache them in the process cache.
4078 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
4079 (tramp-set-connection-property p "local-encoding" loc-enc)
4080 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
4081 (tramp-set-connection-property p "local-decoding" loc-dec)
4082 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
4083 (tramp-set-connection-property p "remote-encoding" rem-enc)
4084 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
4085 (tramp-set-connection-property p "remote-decoding" rem-dec))))
4087 (defun tramp-call-local-coding-command (cmd input output)
4088 "Call the local encoding or decoding command.
4089 If CMD contains \"%s\", provide input file INPUT there in command.
4090 Otherwise, INPUT is passed via standard input.
4091 INPUT can also be nil which means `/dev/null'.
4092 OUTPUT can be a string (which specifies a filename), or t (which
4093 means standard output and thus the current buffer), or nil (which
4095 (tramp-compat-call-process
4096 tramp-encoding-shell
4097 (when (and input (not (string-match "%s" cmd))) input)
4098 (if (eq output t) t nil)
4100 tramp-encoding-command-switch
4102 (if (string-match "%s" cmd) (format cmd input) cmd)
4103 (if (stringp output) (concat "> " output) ""))))
4105 (defconst tramp-inline-compress-commands
4106 '(("gzip" "gzip -d")
4107 ("bzip2" "bzip2 -d")
4109 ("compress" "compress -d"))
4110 "List of compress and decompress commands for inline transfer.
4111 Each item is a list that looks like this:
4113 \(COMPRESS DECOMPRESS\)
4115 COMPRESS or DECOMPRESS are strings with the respective commands.")
4117 (defun tramp-find-inline-compress (vec)
4118 "Find an inline transfer compress command that works.
4119 Goes through the list `tramp-inline-compress-commands'."
4121 (let ((commands tramp-inline-compress-commands)
4123 (p (tramp-get-connection-process vec))
4124 item compress decompress found)
4125 (while (and commands (not found))
4127 (setq item (pop commands)
4128 compress (nth 0 item)
4129 decompress (nth 1 item))
4132 "Checking local compress command `%s', `%s' for sanity"
4133 compress decompress)
4136 (tramp-call-local-coding-command
4138 ;; Windows shells need the program file name after
4139 ;; the pipe symbol be quoted if they use forward
4140 ;; slashes as directory separators.
4141 (if (memq system-type '(windows-nt))
4142 "echo %s | \"%s\" | \"%s\""
4143 "echo %s | %s | %s")
4144 magic compress decompress) nil nil))
4148 "Checking remote compress command `%s', `%s' for sanity"
4149 compress decompress)
4150 (unless (tramp-send-command-and-check
4151 vec (format "echo %s | %s | %s" magic compress decompress) t)
4155 ;; Did we find something?
4158 ;; Set connection properties. Since the commands are
4159 ;; risky (due to output direction), we cache them in the
4162 vec 5 "Using inline transfer compress command `%s'" compress)
4163 (tramp-set-connection-property p "inline-compress" compress)
4165 vec 5 "Using inline transfer decompress command `%s'" decompress)
4166 (tramp-set-connection-property p "inline-decompress" decompress))
4168 (tramp-set-connection-property p "inline-compress" nil)
4169 (tramp-set-connection-property p "inline-decompress" nil)
4171 vec 2 "Couldn't find an inline transfer compress command")))))
4173 (defun tramp-compute-multi-hops (vec)
4174 "Expands VEC according to `tramp-default-proxies-alist'.
4175 Gateway hops are already opened."
4176 (let ((target-alist `(,vec))
4177 (hops (or (tramp-file-name-hop vec) ""))
4181 ;; Ad-hoc proxy definitions.
4182 (dolist (proxy (reverse (split-string hops tramp-postfix-hop-regexp 'omit)))
4183 (let ((user (tramp-file-name-user item))
4184 (host (tramp-file-name-host item))
4186 tramp-prefix-format proxy tramp-postfix-host-format)))
4188 vec 5 "Add proxy (\"%s\" \"%s\" \"%s\")"
4189 (and (stringp host) (regexp-quote host))
4190 (and (stringp user) (regexp-quote user))
4194 'tramp-default-proxies-alist
4195 (list (and (stringp host) (regexp-quote host))
4196 (and (stringp user) (regexp-quote user))
4198 (setq item (tramp-dissect-file-name proxy))))
4199 ;; Save the new value.
4200 (when (and hops tramp-save-ad-hoc-proxies)
4201 (customize-save-variable
4202 'tramp-default-proxies-alist tramp-default-proxies-alist))
4204 ;; Look for proxy hosts to be passed.
4205 (setq choices tramp-default-proxies-alist)
4207 (setq item (pop choices)
4208 proxy (eval (nth 2 item)))
4211 (string-match (or (eval (nth 0 item)) "")
4212 (or (tramp-file-name-host (car target-alist)) ""))
4214 (string-match (or (eval (nth 1 item)) "")
4215 (or (tramp-file-name-user (car target-alist)) "")))
4217 ;; No more hops needed.
4219 ;; Replace placeholders.
4224 ?u (or (tramp-file-name-user (car target-alist)) "")
4225 ?h (or (tramp-file-name-host (car target-alist)) ""))))
4226 (with-parsed-tramp-file-name proxy l
4228 (add-to-list 'target-alist l)
4229 ;; Start next search.
4230 (setq choices tramp-default-proxies-alist)))))
4235 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method)
4236 (tramp-file-name-method (car target-alist)))
4237 (let ((gw (pop target-alist))
4238 (hop (pop target-alist)))
4239 ;; Is the method prepared for gateways?
4240 (unless (tramp-file-name-port hop)
4243 "Connection `%s' is not supported for gateway access." hop))
4244 ;; Open the gateway connection.
4248 (tramp-file-name-method hop) (tramp-file-name-user hop)
4249 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop) nil nil))
4250 ;; For the password prompt, we need the correct values.
4251 ;; Therefore, we must remember the gateway vector. But we
4252 ;; cannot do it as connection property, because it shouldn't
4253 ;; be persistent. And we have no started process yet either.
4254 (tramp-set-file-property (car target-alist) "" "gateway" hop)))
4256 ;; Foreign and out-of-band methods are not supported for multi-hops.
4257 (when (cdr target-alist)
4258 (setq choices target-alist)
4260 (setq item (pop choices))
4264 (tramp-get-method-parameter
4265 (tramp-file-name-method item) 'tramp-login-program))
4266 (tramp-get-method-parameter
4267 (tramp-file-name-method item) 'tramp-copy-program))
4270 "Method `%s' is not supported for multi-hops."
4271 (tramp-file-name-method item)))))
4273 ;; In case the host name is not used for the remote shell
4274 ;; command, the user could be misguided by applying a random
4276 (let* ((v (car target-alist))
4277 (method (tramp-file-name-method v))
4278 (host (tramp-file-name-host v)))
4281 ;; There are multi-hops.
4283 ;; The host name is used for the remote shell command.
4285 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
4286 ;; The host is local. We cannot use `tramp-local-host-p'
4287 ;; here, because it opens a connection as well.
4288 (string-match tramp-local-host-regexp host))
4291 "Host `%s' looks like a remote host, `%s' can only use the local host"
4297 (defvar tramp-current-connection nil
4298 "Last connection timestamp.")
4300 (defun tramp-maybe-open-connection (vec)
4301 "Maybe open a connection VEC.
4302 Does not do anything if a connection is already open, but re-opens the
4303 connection if a previous connection has died for some reason."
4304 (catch 'uname-changed
4305 (let ((p (tramp-get-connection-process vec))
4306 (process-name (tramp-get-connection-property vec "process-name" nil))
4307 (process-environment (copy-sequence process-environment))
4308 (pos (with-current-buffer (tramp-get-connection-buffer vec) (point))))
4310 ;; If Tramp opens the same connection within a short time frame,
4311 ;; there is a problem. We shall signal this.
4312 (unless (or (and p (processp p) (memq (process-status p) '(run open)))
4313 (not (equal (butlast (append vec nil))
4314 (car tramp-current-connection)))
4316 (current-time) (cdr tramp-current-connection))
4318 (throw 'suppress 'suppress))
4320 ;; If too much time has passed since last command was sent, look
4321 ;; whether process is still alive. If it isn't, kill it. When
4322 ;; using ssh, it can sometimes happen that the remote end has
4323 ;; hung up but the local ssh client doesn't recognize this until
4324 ;; it tries to send some data to the remote end. So that's why
4325 ;; we try to send a command from time to time, then look again
4326 ;; whether the process is really alive.
4328 (when (and (> (tramp-time-diff
4330 (tramp-get-connection-property
4331 p "last-cmd-time" '(0 0 0)))
4333 p (processp p) (memq (process-status p) '(run open)))
4334 (tramp-send-command vec "echo are you awake" t t)
4335 (unless (and (memq (process-status p) '(run open))
4336 (tramp-wait-for-output p 10))
4337 ;; The error will be caught locally.
4338 (tramp-error vec 'file-error "Awake did fail")))
4343 ;; New connection must be opened.
4345 (unless (and p (processp p) (memq (process-status p) '(run open)))
4347 ;; We call `tramp-get-buffer' in order to get a debug
4348 ;; buffer for messages from the beginning.
4349 (tramp-get-buffer vec)
4351 ;; If `non-essential' is non-nil, don't reopen a new connection.
4352 (when (and (boundp 'non-essential) (symbol-value 'non-essential))
4353 (throw 'non-essential 'non-essential))
4355 (with-tramp-progress-reporter
4357 (if (zerop (length (tramp-file-name-user vec)))
4358 (format "Opening connection for %s using %s"
4359 (tramp-file-name-host vec)
4360 (tramp-file-name-method vec))
4361 (format "Opening connection for %s@%s using %s"
4362 (tramp-file-name-user vec)
4363 (tramp-file-name-host vec)
4364 (tramp-file-name-method vec)))
4366 ;; Start new process.
4367 (when (and p (processp p))
4369 (setenv "TERM" tramp-terminal-type)
4370 (setenv "LC_ALL" "C")
4371 (setenv "PROMPT_COMMAND")
4372 (setenv "PS1" tramp-initial-end-of-output)
4373 (let* ((target-alist (tramp-compute-multi-hops vec))
4374 (process-connection-type tramp-process-connection-type)
4375 (process-adaptive-read-buffering nil)
4376 (coding-system-for-read nil)
4377 ;; This must be done in order to avoid our file
4379 (p (let ((default-directory
4380 (tramp-compat-temporary-file-directory)))
4383 (tramp-get-connection-name vec)
4384 (tramp-get-connection-buffer vec)
4385 (if tramp-encoding-command-interactive
4386 (list tramp-encoding-shell
4387 tramp-encoding-command-interactive)
4388 (list tramp-encoding-shell))))))
4390 ;; Set sentinel and query flag.
4391 (tramp-set-connection-property p "vector" vec)
4392 (set-process-sentinel p 'tramp-process-sentinel)
4393 (tramp-compat-set-process-query-on-exit-flag p nil)
4394 (setq tramp-current-connection
4395 (cons (butlast (append vec nil)) (current-time))
4396 tramp-current-host (system-name))
4399 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
4401 ;; Check whether process is alive.
4402 (tramp-barf-if-no-shell-prompt
4404 "Couldn't find local shell prompt %s" tramp-encoding-shell)
4406 ;; Now do all the connections as specified.
4408 (let* ((hop (car target-alist))
4409 (l-method (tramp-file-name-method hop))
4410 (l-user (tramp-file-name-user hop))
4411 (l-host (tramp-file-name-host hop))
4414 (tramp-get-method-parameter
4415 l-method 'tramp-login-program))
4417 (tramp-get-method-parameter
4418 l-method 'tramp-login-args))
4420 (tramp-get-method-parameter
4421 l-method 'tramp-async-args))
4423 (tramp-get-method-parameter l-method 'tramp-gw-args))
4424 (gw (tramp-get-file-property hop "" "gateway" nil))
4425 (g-method (and gw (tramp-file-name-method gw)))
4426 (g-user (and gw (tramp-file-name-user gw)))
4427 (g-host (and gw (tramp-file-name-real-host gw)))
4428 (command login-program)
4429 ;; We don't create the temporary file. In
4430 ;; fact, it is just a prefix for the
4431 ;; ControlPath option of ssh; the real
4432 ;; temporary file has another name, and it is
4433 ;; created and protected by ssh. It is also
4434 ;; removed by ssh when the connection is
4437 (tramp-set-connection-property
4441 tramp-temp-name-prefix
4442 (tramp-compat-temporary-file-directory)))))
4445 ;; Add arguments for asynchronous processes.
4446 (when (and process-name async-args)
4447 (setq login-args (append async-args login-args)))
4449 ;; Add gateway arguments if necessary.
4450 (when (and gw gw-args)
4451 (setq login-args (append gw-args login-args)))
4453 ;; Check for port number. Until now, there's no
4454 ;; need for handling like method, user, host.
4455 (when (string-match tramp-host-with-port-regexp l-host)
4456 (setq l-port (match-string 2 l-host)
4457 l-host (match-string 1 l-host)))
4459 ;; Check, whether there is a restricted shell.
4460 (dolist (elt tramp-restricted-shell-hosts-alist)
4461 (when (string-match elt tramp-current-host)
4464 ;; Set variables for computing the prompt for
4465 ;; reading password. They can also be derived
4467 (setq tramp-current-method (or g-method l-method)
4468 tramp-current-user (or g-user l-user)
4469 tramp-current-host (or g-host l-host))
4471 ;; Replace login-args place holders.
4473 l-host (or l-host "")
4474 l-user (or l-user "")
4475 l-port (or l-port "")
4476 spec (format-spec-make
4477 ?h l-host ?u l-user ?p l-port ?t tmpfile)
4480 ;; We do not want to see the trailing local
4481 ;; prompt in `start-file-process'.
4482 (unless r-shell "exec ")
4486 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
4487 (unless (member "" x) (mapconcat 'identity x " ")))
4489 ;; Local shell could be a Windows COMSPEC. It
4490 ;; doesn't know the ";" syntax, but we must exit
4491 ;; always for `start-file-process'. It could
4492 ;; also be a restricted shell, which does not
4494 (when r-shell " && exit || exit")))
4496 ;; Send the command.
4497 (tramp-message vec 3 "Sending command `%s'" command)
4498 (tramp-send-command vec command t t)
4499 (tramp-process-actions
4500 p vec pos tramp-actions-before-shell 60)
4502 vec 3 "Found remote shell prompt on `%s'" l-host))
4504 (setq target-alist (cdr target-alist)))
4506 ;; Make initial shell settings.
4507 (tramp-open-connection-setup-interactive-shell p vec))))
4509 ;; When the user did interrupt, we must cleanup.
4512 ;; Propagate the quit signal.
4513 (signal (car err) (cdr err)))))))
4515 (defun tramp-send-command (vec command &optional neveropen nooutput)
4516 "Send the COMMAND to connection VEC.
4517 Erases temporary buffer before sending the command. If optional
4518 arg NEVEROPEN is non-nil, never try to open the connection. This
4519 is meant to be used from `tramp-maybe-open-connection' only. The
4520 function waits for output unless NOOUTPUT is set."
4521 (unless neveropen (tramp-maybe-open-connection vec))
4522 (let ((p (tramp-get-connection-process vec)))
4523 (when (tramp-get-connection-property p "remote-echo" nil)
4524 ;; We mark the command string that it can be erased in the output buffer.
4525 (tramp-set-connection-property p "check-remote-echo" t)
4526 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
4527 (when (and (string-match "<<'EOF'" command)
4528 (not (tramp-get-connection-property vec "busybox" nil)))
4529 ;; Unset $PS1 when using here documents, in order to avoid
4530 ;; multiple prompts.
4531 (setq command (concat "(PS1= ; " command "\n)")))
4532 ;; Send the command.
4533 (tramp-message vec 6 "%s" command)
4534 (tramp-send-string vec command)
4535 (unless nooutput (tramp-wait-for-output p))))
4537 (defun tramp-wait-for-output (proc &optional timeout)
4538 "Wait for output from remote command."
4539 (unless (buffer-live-p (process-buffer proc))
4540 (delete-process proc)
4541 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
4542 (with-current-buffer (process-buffer proc)
4543 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
4544 ;; be leading escape sequences, which must be ignored.
4545 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
4546 ;; Sometimes, the commands do not return a newline but a
4547 ;; null byte before the shell prompt, for example "git
4548 ;; ls-files -c -z ...".
4549 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
4550 (found (tramp-wait-for-regexp proc timeout regexp1)))
4552 (let (buffer-read-only)
4553 ;; A simple-minded busybox has sent " ^H" sequences.
4555 (goto-char (point-min))
4556 (when (re-search-forward "^\\(.\b\\)+$" (point-at-eol) t)
4558 (delete-region (point-min) (point)))
4559 ;; Delete the prompt.
4560 (goto-char (point-max))
4561 (re-search-backward regexp nil t)
4562 (delete-region (point) (point-max)))
4566 "[[Remote prompt `%s' not found in %d secs]]"
4567 tramp-end-of-output timeout)
4570 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
4571 ;; Return value is whether end-of-output sentinel was found.
4574 (defun tramp-send-command-and-check
4575 (vec command &optional subshell dont-suppress-err)
4576 "Run COMMAND and check its exit status.
4577 Sends `echo $?' along with the COMMAND for checking the exit status. If
4578 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
4580 If the optional argument SUBSHELL is non-nil, the command is
4581 executed in a subshell, ie surrounded by parentheses. If
4582 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
4585 (concat (if subshell "( " "")
4587 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
4588 "echo tramp_exit_status $?"
4589 (if subshell " )" "")))
4590 (with-current-buffer (tramp-get-connection-buffer vec)
4591 (goto-char (point-max))
4592 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
4594 vec 'file-error "Couldn't find exit status of `%s'" command))
4595 (skip-chars-forward "^ ")
4597 (zerop (read (current-buffer)))
4598 (let (buffer-read-only)
4599 (delete-region (match-beginning 0) (point-max))))))
4601 (defun tramp-barf-unless-okay (vec command fmt &rest args)
4602 "Run COMMAND, check exit status, throw error if exit status not okay.
4603 Similar to `tramp-send-command-and-check' but accepts two more arguments
4604 FMT and ARGS which are passed to `error'."
4605 (or (tramp-send-command-and-check vec command)
4606 (apply 'tramp-error vec 'file-error fmt args)))
4608 (defun tramp-send-command-and-read (vec command &optional noerror)
4609 "Run COMMAND and return the output, which must be a Lisp expression.
4610 In case there is no valid Lisp expression and NOERROR is nil, it
4613 (tramp-send-command-and-check vec command)
4614 (tramp-barf-unless-okay
4615 vec command "`%s' returns with error" command))
4616 (with-current-buffer (tramp-get-connection-buffer vec)
4617 ;; Read the expression.
4618 (goto-char (point-min))
4620 (prog1 (read (current-buffer))
4622 (when (re-search-forward "\\S-" (point-at-eol) t)
4624 (error (unless noerror
4627 "`%s' does not return a valid Lisp expression: `%s'"
4628 command (buffer-string))))))))
4630 (defun tramp-convert-file-attributes (vec attr)
4631 "Convert file-attributes ATTR generated by perl script, stat or ls.
4632 Convert file mode bits to string and set virtual device number.
4635 ;; Remove color escape sequences from symlink.
4636 (when (stringp (car attr))
4637 (while (string-match tramp-color-escape-sequence-regexp (car attr))
4638 (setcar attr (replace-match "" nil nil (car attr)))))
4639 ;; Convert last access time.
4640 (unless (listp (nth 4 attr))
4641 (setcar (nthcdr 4 attr)
4642 (list (floor (nth 4 attr) 65536)
4643 (floor (mod (nth 4 attr) 65536)))))
4644 ;; Convert last modification time.
4645 (unless (listp (nth 5 attr))
4646 (setcar (nthcdr 5 attr)
4647 (list (floor (nth 5 attr) 65536)
4648 (floor (mod (nth 5 attr) 65536)))))
4649 ;; Convert last status change time.
4650 (unless (listp (nth 6 attr))
4651 (setcar (nthcdr 6 attr)
4652 (list (floor (nth 6 attr) 65536)
4653 (floor (mod (nth 6 attr) 65536)))))
4654 ;; Convert file size.
4655 (when (< (nth 7 attr) 0)
4656 (setcar (nthcdr 7 attr) -1))
4657 (when (and (floatp (nth 7 attr))
4658 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
4659 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
4660 ;; Convert file mode bits to string.
4661 (unless (stringp (nth 8 attr))
4662 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
4663 (when (stringp (car attr))
4664 (aset (nth 8 attr) 0 ?l)))
4665 ;; Convert directory indication bit.
4666 (when (string-match "^d" (nth 8 attr))
4668 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
4669 (when (consp (car attr))
4670 (if (and (stringp (caar attr))
4671 (string-match ".+ -> .\\(.+\\)." (caar attr)))
4672 (setcar attr (match-string 1 (caar attr)))
4674 ;; Set file's gid change bit.
4675 (setcar (nthcdr 9 attr)
4676 (if (numberp (nth 3 attr))
4677 (not (= (nth 3 attr)
4678 (tramp-get-remote-gid vec 'integer)))
4681 (tramp-get-remote-gid vec 'string)))))
4683 (unless (listp (nth 10 attr))
4684 (setcar (nthcdr 10 attr)
4686 (cons (floor (nth 10 attr) 65536)
4687 (floor (mod (nth 10 attr) 65536)))
4688 ;; Inodes can be incredible huge. We must hide this.
4689 (error (tramp-get-inode vec)))))
4690 ;; Set virtual device number.
4691 (setcar (nthcdr 11 attr)
4692 (tramp-get-device vec))
4695 (defun tramp-check-cached-permissions (vec access)
4696 "Check `file-attributes' caches for VEC.
4697 Return t if according to the cache access type ACCESS is known to
4703 ((eq ?x access) 3))))
4704 (dolist (suffix '("string" "integer") result)
4710 (tramp-get-file-property
4711 vec (tramp-file-name-localname vec)
4712 (concat "file-attributes-" suffix) nil))
4714 (tramp-get-connection-property
4715 vec (concat "uid-" suffix) nil))
4717 (tramp-get-connection-property
4718 vec (concat "gid-" suffix) nil)))
4723 (eq t (car file-attr))
4724 (null (car file-attr)))
4726 ;; World accessible.
4727 (eq access (aref (nth 8 file-attr) (+ offset 6)))
4728 ;; User accessible and owned by user.
4730 (eq access (aref (nth 8 file-attr) offset))
4731 (equal remote-uid (nth 2 file-attr)))
4732 ;; Group accessible and owned by user's
4735 (eq access (aref (nth 8 file-attr) (+ offset 3)))
4736 (equal remote-gid (nth 3 file-attr)))))))))))
4738 (defun tramp-file-mode-from-int (mode)
4739 "Turn an integer representing a file mode into an ls(1)-like string."
4741 (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
4742 (user (logand (lsh mode -6) 7))
4743 (group (logand (lsh mode -3) 7))
4744 (other (logand (lsh mode -0) 7))
4745 (suid (> (logand (lsh mode -9) 4) 0))
4746 (sgid (> (logand (lsh mode -9) 2) 0))
4747 (sticky (> (logand (lsh mode -9) 1) 0)))
4748 (setq user (tramp-file-mode-permissions user suid "s"))
4749 (setq group (tramp-file-mode-permissions group sgid "s"))
4750 (setq other (tramp-file-mode-permissions other sticky "t"))
4751 (concat type user group other)))
4753 (defun tramp-file-mode-permissions (perm suid suid-text)
4754 "Convert a permission bitset into a string.
4755 This is used internally by `tramp-file-mode-from-int'."
4756 (let ((r (> (logand perm 4) 0))
4757 (w (> (logand perm 2) 0))
4758 (x (> (logand perm 1) 0)))
4759 (concat (or (and r "r") "-")
4760 (or (and w "w") "-")
4761 (or (and suid x suid-text) ; suid, execute
4762 (and suid (upcase suid-text)) ; suid, !execute
4763 (and x "x") "-")))) ; !suid
4765 (defun tramp-shell-case-fold (string)
4766 "Converts STRING to shell glob pattern which ignores case."
4769 (if (equal (downcase c) (upcase c))
4771 (format "[%c%c]" (downcase c) (upcase c))))
4775 (defun tramp-make-copy-program-file-name (vec)
4776 "Create a file name suitable to be passed to `rcp' and workalikes."
4777 (let ((user (tramp-file-name-user vec))
4778 (host (tramp-file-name-real-host vec))
4779 (localname (tramp-shell-quote-argument
4780 (tramp-file-name-localname vec))))
4781 (if (not (zerop (length user)))
4782 (format "%s@%s:%s" user host localname)
4783 (format "%s:%s" host localname))))
4785 (defun tramp-method-out-of-band-p (vec size)
4786 "Return t if this is an out-of-band method, nil otherwise."
4788 ;; It shall be an out-of-band method.
4789 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
4790 ;; There must be a size, otherwise the file doesn't exist.
4792 ;; Either the file size is large enough, or (in rare cases) there
4793 ;; does not exist a remote encoding.
4794 (or (null tramp-copy-size-limit)
4795 (> size tramp-copy-size-limit)
4796 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
4798 ;; Variables local to connection.
4800 (defun tramp-get-remote-path (vec)
4801 (with-tramp-connection-property
4802 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
4803 ;; cache the result for the session only. Otherwise, the result
4804 ;; is cached persistently.
4805 (if (memq 'tramp-own-remote-path tramp-remote-path)
4806 (tramp-get-connection-process vec)
4809 (let* ((remote-path (copy-tree tramp-remote-path))
4810 (elt1 (memq 'tramp-default-remote-path remote-path))
4811 (elt2 (memq 'tramp-own-remote-path remote-path))
4812 (default-remote-path
4815 (tramp-send-command-and-read
4816 vec "echo \\\"`getconf PATH 2>/dev/null`\\\"" 'noerror)
4817 ;; Default if "getconf" is not available.
4821 "`getconf PATH' not successful, using default value \"%s\"."
4827 (tramp-send-command-and-read vec "echo \\\"$PATH\\\"")
4830 vec 3 "$PATH not set, ignoring `tramp-own-remote-path'.")
4833 ;; Replace place holder `tramp-default-remote-path'.
4837 (tramp-compat-split-string default-remote-path ":")
4839 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
4841 ;; Replace place holder `tramp-own-remote-path'.
4845 (tramp-compat-split-string own-remote-path ":")
4847 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
4849 ;; Remove double entries.
4850 (setq elt1 remote-path)
4852 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
4854 (setq elt1 (cdr elt1)))
4856 ;; Remove non-existing directories.
4864 (tramp-make-tramp-file-name
4865 (tramp-file-name-method vec)
4866 (tramp-file-name-user vec)
4867 (tramp-file-name-host vec)
4872 (defun tramp-get-ls-command (vec)
4873 (with-tramp-connection-property vec "ls"
4874 (tramp-message vec 5 "Finding a suitable `ls' command")
4877 (dolist (cmd '("ls" "gnuls" "gls"))
4878 (let ((dl (tramp-get-remote-path vec))
4880 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
4881 ;; Check parameters. On busybox, "ls" output coloring is
4882 ;; enabled by default sometimes. So we try to disable it
4883 ;; when possible. $LS_COLORING is not supported there.
4884 ;; Some "ls" versions are sensible wrt the order of
4885 ;; arguments, they fail when "-al" is after the
4886 ;; "--color=never" argument (for example on FreeBSD).
4887 (when (tramp-send-command-and-check
4888 vec (format "%s -lnd /" result))
4889 (when (tramp-send-command-and-check
4891 "%s --color=never -al /dev/null" result))
4892 (setq result (concat result " --color=never")))
4893 (throw 'ls-found result))
4894 (setq dl (cdr dl))))))
4895 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
4897 (defun tramp-get-ls-command-with-dired (vec)
4899 (with-tramp-connection-property vec "ls-dired"
4900 (tramp-message vec 5 "Checking, whether `ls --dired' works")
4901 ;; Some "ls" versions are sensible wrt the order of arguments,
4902 ;; they fail when "-al" is after the "--dired" argument (for
4903 ;; example on FreeBSD).
4904 (tramp-send-command-and-check
4905 vec (format "%s --dired -al /dev/null" (tramp-get-ls-command vec))))))
4907 (defun tramp-get-test-command (vec)
4908 (with-tramp-connection-property vec "test"
4909 (tramp-message vec 5 "Finding a suitable `test' command")
4910 (if (tramp-send-command-and-check vec "test 0")
4912 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
4914 (defun tramp-get-test-nt-command (vec)
4915 ;; Does `test A -nt B' work? Use abominable `find' construct if it
4916 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
4917 ;; for otherwise the shell crashes.
4918 (with-tramp-connection-property vec "test-nt"
4922 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
4923 (with-current-buffer (tramp-get-buffer vec)
4924 (goto-char (point-min))
4925 (when (looking-at (regexp-quote tramp-end-of-output))
4926 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
4931 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
4932 (tramp-get-test-command vec)))
4933 "tramp_test_nt %s %s"))))
4935 (defun tramp-get-file-exists-command (vec)
4936 (with-tramp-connection-property vec "file-exists"
4937 (tramp-message vec 5 "Finding command to check if file exists")
4938 (tramp-find-file-exists-command vec)))
4940 (defun tramp-get-remote-ln (vec)
4941 (with-tramp-connection-property vec "ln"
4942 (tramp-message vec 5 "Finding a suitable `ln' command")
4943 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
4945 (defun tramp-get-remote-perl (vec)
4946 (with-tramp-connection-property vec "perl"
4947 (tramp-message vec 5 "Finding a suitable `perl' command")
4949 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
4950 (tramp-find-executable
4951 vec "perl" (tramp-get-remote-path vec)))))
4952 ;; We must check also for some Perl modules.
4954 (with-tramp-connection-property vec "perl-file-spec"
4955 (tramp-send-command-and-check
4956 vec (format "%s -e 'use File::Spec;'" result)))
4957 (with-tramp-connection-property vec "perl-cwd-realpath"
4958 (tramp-send-command-and-check
4959 vec (format "%s -e 'use Cwd \"realpath\";'" result))))
4962 (defun tramp-get-remote-stat (vec)
4963 (with-tramp-connection-property vec "stat"
4964 (tramp-message vec 5 "Finding a suitable `stat' command")
4965 (let ((result (tramp-find-executable
4966 vec "stat" (tramp-get-remote-path vec)))
4968 ;; Check whether stat(1) returns usable syntax. "%s" does not
4969 ;; work on older AIX systems.
4972 (tramp-send-command-and-read
4973 vec (format "%s -c '(\"%%N\" %%s)' /" result) 'noerror))
4974 (unless (and (listp tmp) (stringp (car tmp))
4975 (string-match "^./.$" (car tmp))
4976 (integerp (cadr tmp)))
4980 (defun tramp-get-remote-readlink (vec)
4981 (with-tramp-connection-property vec "readlink"
4982 (tramp-message vec 5 "Finding a suitable `readlink' command")
4983 (let ((result (tramp-find-executable
4984 vec "readlink" (tramp-get-remote-path vec))))
4986 (tramp-send-command-and-check
4987 vec (format "%s --canonicalize-missing /" result)))
4990 (defun tramp-get-remote-trash (vec)
4991 (with-tramp-connection-property vec "trash"
4992 (tramp-message vec 5 "Finding a suitable `trash' command")
4993 (tramp-find-executable vec "trash" (tramp-get-remote-path vec))))
4995 (defun tramp-get-remote-id (vec)
4996 (with-tramp-connection-property vec "id"
4997 (tramp-message vec 5 "Finding POSIX `id' command")
5000 (let ((dl (tramp-get-remote-path vec))
5002 (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
5003 ;; Check POSIX parameter.
5004 (when (tramp-send-command-and-check vec (format "%s -u" result))
5005 (throw 'id-found result))
5006 (setq dl (cdr dl)))))
5007 (tramp-error vec 'file-error "Couldn't find a POSIX `id' command"))))
5009 (defun tramp-get-remote-uid (vec id-format)
5010 (with-tramp-connection-property vec (format "uid-%s" id-format)
5011 (let ((res (tramp-send-command-and-read
5013 (format "%s -u%s %s"
5014 (tramp-get-remote-id vec)
5015 (if (equal id-format 'integer) "" "n")
5016 (if (equal id-format 'integer)
5017 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
5018 ;; The command might not always return a number.
5019 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
5021 (defun tramp-get-remote-gid (vec id-format)
5022 (with-tramp-connection-property vec (format "gid-%s" id-format)
5023 (let ((res (tramp-send-command-and-read
5025 (format "%s -g%s %s"
5026 (tramp-get-remote-id vec)
5027 (if (equal id-format 'integer) "" "n")
5028 (if (equal id-format 'integer)
5029 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
5030 ;; The command might not always return a number.
5031 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
5033 (defun tramp-get-local-uid (id-format)
5034 (if (equal id-format 'integer) (user-uid) (user-login-name)))
5036 (defun tramp-get-local-gid (id-format)
5037 (nth 3 (tramp-compat-file-attributes "~/" id-format)))
5039 ;; Some predefined connection properties.
5040 (defun tramp-get-inline-compress (vec prop size)
5041 "Return the compress command related to PROP.
5042 PROP is either `inline-compress' or `inline-decompress'. SIZE is
5043 the length of the file to be compressed.
5045 If no corresponding command is found, nil is returned."
5046 (when (and (integerp tramp-inline-compress-start-size)
5047 (> size tramp-inline-compress-start-size))
5048 (with-tramp-connection-property (tramp-get-connection-process vec) prop
5049 (tramp-find-inline-compress vec)
5050 (tramp-get-connection-property
5051 (tramp-get-connection-process vec) prop nil))))
5053 (defun tramp-get-inline-coding (vec prop size)
5054 "Return the coding command related to PROP.
5055 PROP is either `remote-encoding', `remote-decoding',
5056 `local-encoding' or `local-decoding'.
5058 SIZE is the length of the file to be coded. Depending on SIZE,
5059 compression might be applied.
5061 If no corresponding command is found, nil is returned.
5062 Otherwise, either a string is returned which contains a `%s' mark
5063 to be used for the respective input or output file; or a Lisp
5064 function cell is returned to be applied on a buffer."
5065 ;; We must catch the errors, because we want to return `nil', when
5066 ;; no inline coding is found.
5069 (with-tramp-connection-property
5070 (tramp-get-connection-process vec) prop
5071 (tramp-find-inline-encoding vec)
5072 (tramp-get-connection-property
5073 (tramp-get-connection-process vec) prop nil)))
5074 (prop1 (if (string-match "encoding" prop)
5075 "inline-compress" "inline-decompress"))
5077 ;; The connection property might have been cached. So we must
5078 ;; send the script to the remote side - maybe.
5079 (when (and coding (symbolp coding) (string-match "remote" prop))
5080 (let ((name (symbol-name coding)))
5081 (while (string-match (regexp-quote "-") name)
5082 (setq name (replace-match "_" nil t name)))
5083 (tramp-maybe-send-script vec (symbol-value coding) name)
5084 (setq coding name)))
5086 ;; Check for the `compress' command.
5087 (setq compress (tramp-get-inline-compress vec prop1 size))
5088 ;; Return the value.
5090 ((and compress (symbolp coding))
5091 (if (string-match "decompress" prop1)
5094 (let ((coding-system-for-write 'binary)
5095 (coding-system-for-read 'binary))
5097 'call-process-region (point-min) (point-max)
5098 (car (split-string ,compress)) t t nil
5099 (cdr (split-string ,compress)))))
5101 (let ((coding-system-for-write 'binary)
5102 (coding-system-for-read 'binary))
5104 'call-process-region beg end
5105 (car (split-string ,compress)) t t nil
5106 (cdr (split-string ,compress))))
5107 (,coding (point-min) (point-max)))))
5110 ((and compress (string-match "decoding" prop))
5112 ;; Windows shells need the program file name after
5113 ;; the pipe symbol be quoted if they use forward
5114 ;; slashes as directory separators.
5115 (if (and (string-match "local" prop)
5116 (memq system-type '(windows-nt)))
5117 "(%s | \"%s\" >%%s)"
5122 ;; Windows shells need the program file name after
5123 ;; the pipe symbol be quoted if they use forward
5124 ;; slashes as directory separators.
5125 (if (and (string-match "local" prop)
5126 (memq system-type '(windows-nt)))
5127 "(%s <%%s | \"%s\")"
5130 ((string-match "decoding" prop)
5131 (format "%s >%%s" coding))
5133 (format "%s <%%s" coding)))))))
5135 ;;; Integration of eshell.el:
5138 (defvar eshell-path-env))
5140 ;; eshell.el keeps the path in `eshell-path-env'. We must change it
5141 ;; when `default-directory' points to another host.
5142 (defun tramp-eshell-directory-change ()
5143 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
5144 (setq eshell-path-env
5145 (if (file-remote-p default-directory)
5146 (with-parsed-tramp-file-name default-directory nil
5149 (tramp-get-remote-path v)
5153 (eval-after-load "esh-util"
5155 (tramp-eshell-directory-change)
5156 (add-hook 'eshell-directory-change-hook
5157 'tramp-eshell-directory-change)
5158 (add-hook 'tramp-unload-hook
5160 (remove-hook 'eshell-directory-change-hook
5161 'tramp-eshell-directory-change)))))
5163 (add-hook 'tramp-unload-hook
5165 (unload-feature 'tramp-sh 'force)))
5171 ;; * Don't use globbing for directories with many files, as this is
5172 ;; likely to produce long command lines, and some shells choke on
5173 ;; long command lines.
5174 ;; * Make it work for different encodings, and for different file name
5175 ;; encodings, too. (Daniel Pittman)
5176 ;; * Don't search for perl5 and perl. Instead, only search for perl and
5177 ;; then look if it's the right version (with `perl -v').
5178 ;; * When editing a remote CVS controlled file as a different user, VC
5179 ;; gets confused about the file locking status. Try to find out why
5180 ;; the workaround doesn't work.
5181 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5182 ;; until the last but one hop via `start-file-process'. Apply it
5183 ;; also for ftp and smb.
5184 ;; * WIBNI if we had a command "trampclient"? If I was editing in
5185 ;; some shell with root privileges, it would be nice if I could
5187 ;; trampclient filename.c
5188 ;; as an editor, and the _current_ shell would connect to an Emacs
5189 ;; server and would be used in an existing non-privileged Emacs
5190 ;; session for doing the editing in question.
5191 ;; That way, I need not tell Emacs my password again and be afraid
5192 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
5193 ;; once display a just typed password in the context of a keyboard
5194 ;; sequence prompt for a question immediately following in a shell
5195 ;; script run within Emacs -- nasty).
5196 ;; And if I have some ssh session running to a different computer,
5197 ;; having the possibility of passing a local file there to a local
5198 ;; Emacs session (in case I can arrange for a connection back) would
5200 ;; Likely the corresponding Tramp server should not allow the
5201 ;; equivalent of the emacsclient -eval option in order to make this
5202 ;; reasonably unproblematic. And maybe trampclient should have some
5203 ;; way of passing credentials, like by using an SSL socket or
5204 ;; something. (David Kastrup)
5205 ;; * Reconnect directly to a compliant shell without first going
5206 ;; through the user's default shell. (Pete Forman)
5207 ;; * How can I interrupt the remote process with a signal
5208 ;; (interrupt-process seems not to work)? (Markus Triska)
5209 ;; * Avoid the local shell entirely for starting remote processes. If
5210 ;; so, I think even a signal, when delivered directly to the local
5211 ;; SSH instance, would correctly be propagated to the remote process
5212 ;; automatically; possibly SSH would have to be started with
5213 ;; "-t". (Markus Triska)
5214 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5215 ;; isn't on the remote host. (Mark A. Hershberger)
5216 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
5217 ;; * Optimize out-of-band copying when both methods are scp-like (not
5219 ;; * Keep a second connection open for out-of-band methods like scp or
5221 ;; * Try telnet+curl as new method. It might be useful for busybox,
5222 ;; without built-in uuencode/uudecode.
5224 ;;; tramp-sh.el ends here