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+/);
819 # We read in chunks of 54 bytes, to generate output lines
820 # of 72 chars (plus end of line)
823 while (my $data = <STDIN>) {
826 # Only for the last chunk, and only if did not fill the last three-byte packet
828 my $mod = length($data) %% 3;
829 $pad = q(=) x (3 - $mod) if $mod;
832 # Not the fastest method, but it is simple: unpack to binary string, split
833 # by groups of 6 bits and convert back from binary to byte; then map into
834 # the translation table
838 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
842 "Perl program to use for encoding a file.
843 Escape sequence %s is replaced with name of Perl binary.
844 This string is passed to `format', so percent characters need to be doubled.")
846 (defconst tramp-perl-decode
848 # This script contributed by Juanma Barranquero <lektu@terra.es>.
849 # Copyright (C) 2002-2012 Free Software Foundation, Inc.
854 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
855 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
858 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
862 # We are going to accumulate into $pending to accept any line length
863 # (we do not check they are <= 76 chars as the RFC says)
866 while (my $data = <STDIN>) {
869 # If we find one or two =, we have reached the end and
870 # any following data is to be discarded
871 my $finished = $data =~ s/(==?).*/$1/;
874 my $len = length($pending);
875 my $chunk = substr($pending, 0, $len & ~3);
876 $pending = substr($pending, $len & ~3 + 1);
878 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
879 # split in 8-bit chunks and convert back to char.
882 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
886 "Perl program to use for decoding a file.
887 Escape sequence %s is replaced with name of Perl binary.
888 This string is passed to `format', so percent characters need to be doubled.")
890 (defconst tramp-vc-registered-read-file-names
893 if %s \"$file\"; then
894 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
896 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
898 if %s \"$file\"; then
899 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
901 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
905 "Script to check existence of VC related files.
906 It must be send formatted with two strings; the tests for file
907 existence, and file readability. Input shall be read via
908 here-document, otherwise the command could exceed maximum length
911 (defconst tramp-file-mode-type-map
912 '((0 . "-") ; Normal file (SVID-v2 and XPG2)
914 (2 . "c") ; character device
915 (3 . "m") ; multiplexed character device (v7)
916 (4 . "d") ; directory
917 (5 . "?") ; Named special file (XENIX)
918 (6 . "b") ; block device
919 (7 . "?") ; multiplexed block device (v7)
920 (8 . "-") ; regular file
921 (9 . "n") ; network special file (HP-UX)
923 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
925 (13 . "D") ; door special (Solaris)
926 (14 . "w")) ; whiteout (BSD)
927 "A list of file types returned from the `stat' system call.
928 This is used to map a mode number to a permission string.")
930 ;; New handlers should be added here. The following operations can be
931 ;; handled using the normal primitives: file-name-sans-versions,
933 (defconst tramp-sh-file-name-handler-alist
934 '((load . tramp-handle-load)
935 (make-symbolic-link . tramp-sh-handle-make-symbolic-link)
936 (file-name-as-directory . tramp-handle-file-name-as-directory)
937 (file-name-directory . tramp-handle-file-name-directory)
938 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
939 (file-truename . tramp-sh-handle-file-truename)
940 (file-exists-p . tramp-sh-handle-file-exists-p)
941 (file-directory-p . tramp-sh-handle-file-directory-p)
942 (file-executable-p . tramp-sh-handle-file-executable-p)
943 (file-readable-p . tramp-sh-handle-file-readable-p)
944 (file-regular-p . tramp-handle-file-regular-p)
945 (file-symlink-p . tramp-handle-file-symlink-p)
946 (file-writable-p . tramp-sh-handle-file-writable-p)
947 (file-ownership-preserved-p . tramp-sh-handle-file-ownership-preserved-p)
948 (file-newer-than-file-p . tramp-sh-handle-file-newer-than-file-p)
949 (file-attributes . tramp-sh-handle-file-attributes)
950 (file-modes . tramp-handle-file-modes)
951 (directory-files . tramp-handle-directory-files)
952 (directory-files-and-attributes
953 . tramp-sh-handle-directory-files-and-attributes)
954 (file-name-all-completions . tramp-sh-handle-file-name-all-completions)
955 (file-name-completion . tramp-handle-file-name-completion)
956 (add-name-to-file . tramp-sh-handle-add-name-to-file)
957 (copy-file . tramp-sh-handle-copy-file)
958 (copy-directory . tramp-sh-handle-copy-directory)
959 (rename-file . tramp-sh-handle-rename-file)
960 (set-file-modes . tramp-sh-handle-set-file-modes)
961 (set-file-times . tramp-sh-handle-set-file-times)
962 (make-directory . tramp-sh-handle-make-directory)
963 (delete-directory . tramp-sh-handle-delete-directory)
964 (delete-file . tramp-sh-handle-delete-file)
965 (directory-file-name . tramp-handle-directory-file-name)
966 ;; `executable-find' is not official yet.
967 (executable-find . tramp-sh-handle-executable-find)
968 (start-file-process . tramp-sh-handle-start-file-process)
969 (process-file . tramp-sh-handle-process-file)
970 (shell-command . tramp-handle-shell-command)
971 (insert-directory . tramp-sh-handle-insert-directory)
972 (expand-file-name . tramp-sh-handle-expand-file-name)
973 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
974 (file-local-copy . tramp-sh-handle-file-local-copy)
975 (file-remote-p . tramp-handle-file-remote-p)
976 (insert-file-contents . tramp-handle-insert-file-contents)
977 (insert-file-contents-literally
978 . tramp-sh-handle-insert-file-contents-literally)
979 (write-region . tramp-sh-handle-write-region)
980 (find-backup-file-name . tramp-handle-find-backup-file-name)
981 (make-auto-save-file-name . tramp-sh-handle-make-auto-save-file-name)
982 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
983 (dired-compress-file . tramp-sh-handle-dired-compress-file)
984 (dired-recursive-delete-directory
985 . tramp-sh-handle-dired-recursive-delete-directory)
986 (dired-uncache . tramp-handle-dired-uncache)
987 (set-visited-file-modtime . tramp-sh-handle-set-visited-file-modtime)
988 (verify-visited-file-modtime . tramp-sh-handle-verify-visited-file-modtime)
989 (file-selinux-context . tramp-sh-handle-file-selinux-context)
990 (set-file-selinux-context . tramp-sh-handle-set-file-selinux-context)
991 (vc-registered . tramp-sh-handle-vc-registered))
992 "Alist of handler functions.
993 Operations not mentioned here will be handled by the normal Emacs functions.")
995 ;; This must be the last entry, because `identity' always matches.
997 (add-to-list 'tramp-foreign-file-name-handler-alist
998 '(identity . tramp-sh-file-name-handler) 'append)
1000 ;;; File Name Handler Functions:
1002 (defun tramp-sh-handle-make-symbolic-link
1003 (filename linkname &optional ok-if-already-exists)
1004 "Like `make-symbolic-link' for Tramp files.
1005 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
1006 the symlink. If LINKNAME is a Tramp file, only the localname component is
1007 used as the target of the symlink.
1009 If LINKNAME is a Tramp file and the localname component is relative, then
1010 it is expanded first, before the localname component is taken. Note that
1011 this can give surprising results if the user/host for the source and
1012 target of the symlink differ."
1013 (with-parsed-tramp-file-name linkname l
1014 (let ((ln (tramp-get-remote-ln l))
1015 (cwd (tramp-run-real-handler
1016 'file-name-directory (list l-localname))))
1020 "Making a symbolic link. ln(1) does not exist on the remote host."))
1022 ;; Do the 'confirm if exists' thing.
1023 (when (file-exists-p linkname)
1025 (if (or (null ok-if-already-exists) ; not allowed to exist
1026 (and (numberp ok-if-already-exists)
1029 "File %s already exists; make it a link anyway? "
1032 l 'file-already-exists "File %s already exists" l-localname)
1033 (delete-file linkname)))
1035 ;; If FILENAME is a Tramp name, use just the localname component.
1036 (when (tramp-tramp-file-p filename)
1038 (tramp-file-name-localname
1039 (tramp-dissect-file-name (expand-file-name filename)))))
1041 (tramp-flush-file-property l (file-name-directory l-localname))
1042 (tramp-flush-file-property l l-localname)
1044 ;; Right, they are on the same host, regardless of user, method,
1045 ;; etc. We now make the link on the remote machine. This will
1046 ;; occur as the user that FILENAME belongs to.
1047 (tramp-send-command-and-check
1050 "cd %s && %s -sf %s %s"
1051 (tramp-shell-quote-argument cwd)
1053 (tramp-shell-quote-argument filename)
1054 (tramp-shell-quote-argument l-localname))
1057 (defun tramp-sh-handle-file-truename (filename &optional counter prev-dirs)
1058 "Like `file-truename' for Tramp files."
1059 (with-parsed-tramp-file-name (expand-file-name filename) nil
1060 (tramp-make-tramp-file-name method user host
1061 (with-tramp-file-property v localname "file-truename"
1062 (let ((result nil)) ; result steps in reverse order
1063 (tramp-message v 4 "Finding true name for `%s'" filename)
1065 ;; Use GNU readlink --canonicalize-missing where available.
1066 ((tramp-get-remote-readlink v)
1068 (tramp-send-command-and-read
1070 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
1071 (tramp-get-remote-readlink v)
1072 (tramp-shell-quote-argument localname)))))
1074 ;; Use Perl implementation.
1075 ((and (tramp-get-remote-perl v)
1076 (tramp-get-connection-property v "perl-file-spec" nil)
1077 (tramp-get-connection-property v "perl-cwd-realpath" nil))
1078 (tramp-maybe-send-script
1079 v tramp-perl-file-truename "tramp_perl_file_truename")
1081 (tramp-send-command-and-read
1083 (format "tramp_perl_file_truename %s"
1084 (tramp-shell-quote-argument localname)))))
1086 ;; Do it yourself. We bind `directory-sep-char' here for
1087 ;; XEmacs on Windows, which would otherwise use backslash.
1088 (t (let* ((directory-sep-char ?/)
1089 (steps (tramp-compat-split-string localname "/"))
1090 (localnamedir (tramp-run-real-handler
1091 'file-name-as-directory (list localname)))
1092 (is-dir (string= localname localnamedir))
1095 ;; Don't make the following value larger than
1096 ;; necessary. People expect an error message in
1097 ;; a timely fashion when something is wrong;
1098 ;; otherwise they might think that Emacs is hung.
1099 ;; Of course, correctness has to come first.
1102 (while (and steps (< numchase numchase-limit))
1103 (setq thisstep (pop steps))
1106 (mapconcat 'identity
1107 (append '("") (reverse result) (list thisstep))
1109 (setq symlink-target
1110 (nth 0 (file-attributes
1111 (tramp-make-tramp-file-name
1113 (mapconcat 'identity
1118 (cond ((string= "." thisstep)
1119 (tramp-message v 5 "Ignoring step `.'"))
1120 ((string= ".." thisstep)
1121 (tramp-message v 5 "Processing step `..'")
1123 ((stringp symlink-target)
1124 ;; It's a symlink, follow it.
1126 v 5 "Follow symlink to %s" symlink-target)
1127 (setq numchase (1+ numchase))
1128 (when (file-name-absolute-p symlink-target)
1130 ;; If the symlink was absolute, we'll get a
1131 ;; string like "/user@host:/some/target";
1132 ;; extract the "/some/target" part from it.
1133 (when (tramp-tramp-file-p symlink-target)
1134 (unless (tramp-equal-remote filename symlink-target)
1137 "Symlink target `%s' on wrong host"
1139 (setq symlink-target localname))
1141 (append (tramp-compat-split-string
1146 (setq result (cons thisstep result)))))
1147 (when (>= numchase numchase-limit)
1150 "Maximum number (%d) of symlinks exceeded" numchase-limit))
1151 (setq result (reverse result))
1152 ;; Combine list to form string.
1155 (mapconcat 'identity (cons "" result) "/")
1158 (or (string= "" result)
1159 (not (string= (substring result -1) "/"))))
1160 (setq result (concat result "/"))))))
1162 (tramp-message v 4 "True name of `%s' is `%s'" localname result)
1167 (defun tramp-sh-handle-file-exists-p (filename)
1168 "Like `file-exists-p' for Tramp files."
1169 (with-parsed-tramp-file-name filename nil
1170 (with-tramp-file-property v localname "file-exists-p"
1171 (or (not (null (tramp-get-file-property
1172 v localname "file-attributes-integer" nil)))
1173 (not (null (tramp-get-file-property
1174 v localname "file-attributes-string" nil)))
1175 (tramp-send-command-and-check
1179 (tramp-get-file-exists-command v)
1180 (tramp-shell-quote-argument localname)))))))
1182 (defun tramp-sh-handle-file-attributes (filename &optional id-format)
1183 "Like `file-attributes' for Tramp files."
1184 (unless id-format (setq id-format 'integer))
1185 ;; Don't modify `last-coding-system-used' by accident.
1186 (let ((last-coding-system-used last-coding-system-used))
1187 (with-parsed-tramp-file-name (expand-file-name filename) nil
1188 (with-tramp-file-property
1189 v localname (format "file-attributes-%s" id-format)
1191 (tramp-convert-file-attributes
1195 ((tramp-get-remote-stat v)
1196 (tramp-do-file-attributes-with-stat v localname id-format))
1197 ((tramp-get-remote-perl v)
1198 (tramp-do-file-attributes-with-perl v localname id-format))
1200 ;; The scripts could fail, for example with huge file size.
1201 (tramp-do-file-attributes-with-ls v localname id-format))))))))
1203 (defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
1204 "Implement `file-attributes' for Tramp files using the ls(1) command."
1206 res-inode res-filemodes res-numlinks
1207 res-uid res-gid res-size res-symlink-target)
1208 (tramp-message vec 5 "file attributes with ls: %s" localname)
1211 (format "(%s %s || %s -h %s) && %s %s %s"
1212 (tramp-get-file-exists-command vec)
1213 (tramp-shell-quote-argument localname)
1214 (tramp-get-test-command vec)
1215 (tramp-shell-quote-argument localname)
1216 (tramp-get-ls-command vec)
1217 (if (eq id-format 'integer) "-ildn" "-ild")
1218 (tramp-shell-quote-argument localname)))
1219 ;; parse `ls -l' output ...
1220 (with-current-buffer (tramp-get-buffer vec)
1221 (when (> (buffer-size) 0)
1222 (goto-char (point-min))
1226 (read (current-buffer))
1227 (invalid-read-syntax
1228 (when (and (equal (cadr err)
1229 "Integer constant overflow in reader")
1231 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
1233 (let* ((big (read (substring (car (cddr err)) 0
1234 (match-beginning 1))))
1235 (small (read (match-string 1 (car (cddr err)))))
1236 (twiddle (/ small 65536)))
1237 (cons (+ big twiddle)
1238 (- small (* twiddle 65536))))))))
1239 ;; ... file mode flags
1240 (setq res-filemodes (symbol-name (read (current-buffer))))
1242 (setq res-numlinks (read (current-buffer)))
1244 (setq res-uid (read (current-buffer)))
1245 (setq res-gid (read (current-buffer)))
1246 (if (eq id-format 'integer)
1248 (unless (numberp res-uid) (setq res-uid -1))
1249 (unless (numberp res-gid) (setq res-gid -1)))
1251 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
1252 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
1254 (setq res-size (read (current-buffer)))
1255 ;; From the file modes, figure out other stuff.
1256 (setq symlinkp (eq ?l (aref res-filemodes 0)))
1257 (setq dirp (eq ?d (aref res-filemodes 0)))
1258 ;; if symlink, find out file name pointed to
1260 (search-forward "-> ")
1261 (setq res-symlink-target (buffer-substring (point) (point-at-eol))))
1262 ;; return data gathered
1264 ;; 0. t for directory, string (name linked to) for symbolic
1266 (or dirp res-symlink-target)
1267 ;; 1. Number of links to file.
1273 ;; 4. Last access time, as a list of integers. Normally this
1274 ;; would be in the same format as `current-time', but the
1275 ;; subseconds part is not currently implemented, and (0 0)
1276 ;; denotes an unknown time.
1277 ;; 5. Last modification time, likewise.
1278 ;; 6. Last status change time, likewise.
1279 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
1280 ;; 7. Size in bytes (-1, if number is out of range).
1282 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
1284 ;; 9. t if file's gid would change if file were deleted and
1285 ;; recreated. Will be set in `tramp-convert-file-attributes'
1287 ;; 10. inode number.
1289 ;; 11. Device number. Will be replaced by a virtual device number.
1293 (defun tramp-do-file-attributes-with-perl
1294 (vec localname &optional id-format)
1295 "Implement `file-attributes' for Tramp files using a Perl script."
1296 (tramp-message vec 5 "file attributes with perl: %s" localname)
1297 (tramp-maybe-send-script
1298 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
1299 (tramp-send-command-and-read
1301 (format "tramp_perl_file_attributes %s %s"
1302 (tramp-shell-quote-argument localname) id-format)))
1304 (defun tramp-do-file-attributes-with-stat
1305 (vec localname &optional id-format)
1306 "Implement `file-attributes' for Tramp files using stat(1) command."
1307 (tramp-message vec 5 "file attributes with stat: %s" localname)
1308 (tramp-send-command-and-read
1311 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
1312 ;; parse correctly the sequence "((". Therefore, we add a space.
1313 "( (%s %s || %s -h %s) && %s -c '((\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)' %s || echo nil)"
1314 (tramp-get-file-exists-command vec)
1315 (tramp-shell-quote-argument localname)
1316 (tramp-get-test-command vec)
1317 (tramp-shell-quote-argument localname)
1318 (tramp-get-remote-stat vec)
1319 (if (eq id-format 'integer) "%ue0" "\"%U\"")
1320 (if (eq id-format 'integer) "%ge0" "\"%G\"")
1321 (tramp-shell-quote-argument localname))))
1323 (defun tramp-sh-handle-set-visited-file-modtime (&optional time-list)
1324 "Like `set-visited-file-modtime' for Tramp files."
1325 (unless (buffer-file-name)
1326 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
1329 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
1330 (let ((f (buffer-file-name))
1332 (with-parsed-tramp-file-name f nil
1333 (let* ((attr (file-attributes f))
1334 ;; '(-1 65535) means file doesn't exists yet.
1335 (modtime (or (nth 5 attr) '(-1 65535))))
1336 (when (boundp 'last-coding-system-used)
1337 (setq coding-system-used (symbol-value 'last-coding-system-used)))
1338 ;; We use '(0 0) as a don't-know value. See also
1339 ;; `tramp-do-file-attributes-with-ls'.
1340 (if (not (equal modtime '(0 0)))
1341 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
1345 (format "%s -ild %s"
1346 (tramp-get-ls-command v)
1347 (tramp-shell-quote-argument localname)))
1348 (setq attr (buffer-substring (point)
1349 (progn (end-of-line) (point)))))
1350 (tramp-set-file-property
1351 v localname "visited-file-modtime-ild" attr))
1352 (when (boundp 'last-coding-system-used)
1353 (set 'last-coding-system-used coding-system-used))
1356 ;; This function makes the same assumption as
1357 ;; `tramp-sh-handle-set-visited-file-modtime'.
1358 (defun tramp-sh-handle-verify-visited-file-modtime (buf)
1359 "Like `verify-visited-file-modtime' for Tramp files.
1360 At the time `verify-visited-file-modtime' calls this function, we
1361 already know that the buffer is visiting a file and that
1362 `visited-file-modtime' does not return 0. Do not call this
1363 function directly, unless those two cases are already taken care
1365 (with-current-buffer buf
1366 (let ((f (buffer-file-name)))
1367 ;; There is no file visiting the buffer, or the buffer has no
1368 ;; recorded last modification time, or there is no established
1371 (eq (visited-file-modtime) 0)
1372 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
1374 (with-parsed-tramp-file-name f nil
1375 (let* ((remote-file-name-inhibit-cache t)
1376 (attr (file-attributes f))
1377 (modtime (nth 5 attr))
1378 (mt (visited-file-modtime)))
1381 ;; File exists, and has a known modtime.
1382 ((and attr (not (equal modtime '(0 0))))
1383 (< (abs (tramp-time-diff
1385 ;; For compatibility, deal with both the old
1386 ;; (HIGH . LOW) and the new (HIGH LOW) return
1387 ;; values of `visited-file-modtime'.
1389 (list (car mt) (cdr mt))
1392 ;; Modtime has the don't know value.
1396 (format "%s -ild %s"
1397 (tramp-get-ls-command v)
1398 (tramp-shell-quote-argument localname)))
1399 (with-current-buffer (tramp-get-buffer v)
1400 (setq attr (buffer-substring
1401 (point) (progn (end-of-line) (point)))))
1404 (tramp-get-file-property
1405 v localname "visited-file-modtime-ild" "")))
1406 ;; If file does not exist, say it is not modified if and
1407 ;; only if that agrees with the buffer's record.
1408 (t (equal mt '(-1 65535))))))))))
1410 (defun tramp-sh-handle-set-file-modes (filename mode)
1411 "Like `set-file-modes' for Tramp files."
1412 (with-parsed-tramp-file-name filename nil
1413 (tramp-flush-file-property v localname)
1414 ;; FIXME: extract the proper text from chmod's stderr.
1415 (tramp-barf-unless-okay
1417 (format "chmod %s %s"
1418 (tramp-compat-decimal-to-octal mode)
1419 (tramp-shell-quote-argument localname))
1420 "Error while changing file's mode %s" filename)))
1422 (defun tramp-sh-handle-set-file-times (filename &optional time)
1423 "Like `set-file-times' for Tramp files."
1424 (if (file-remote-p filename)
1425 (with-parsed-tramp-file-name filename nil
1426 (tramp-flush-file-property v localname)
1427 (let ((time (if (or (null time) (equal time '(0 0)))
1430 ;; With GNU Emacs, `format-time-string' has an optional
1431 ;; parameter UNIVERSAL. This is preferred, because we
1432 ;; could handle the case when the remote host is located
1433 ;; in a different time zone as the local host.
1434 (utc (not (featurep 'xemacs))))
1435 (tramp-send-command-and-check
1436 v (format "%s touch -t %s %s"
1437 (if utc "TZ=UTC; export TZ;" "")
1439 (format-time-string "%Y%m%d%H%M.%S" time t)
1440 (format-time-string "%Y%m%d%H%M.%S" time))
1441 (tramp-shell-quote-argument localname)))))
1443 ;; We handle also the local part, because in older Emacsen,
1444 ;; without `set-file-times', this function is an alias for this.
1445 ;; We are local, so we don't need the UTC settings.
1447 (tramp-compat-call-process
1448 "touch" nil nil nil "-t"
1449 (format-time-string "%Y%m%d%H%M.%S" time)
1450 (tramp-shell-quote-argument filename)))))
1452 (defun tramp-set-file-uid-gid (filename &optional uid gid)
1453 "Set the ownership for FILENAME.
1454 If UID and GID are provided, these values are used; otherwise uid
1455 and gid of the corresponding user is taken. Both parameters must be integers."
1456 ;; Modern Unices allow chown only for root. So we might need
1457 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
1458 ;; working with su(do)? when it is needed, so it shall succeed in
1459 ;; the majority of cases.
1460 ;; Don't modify `last-coding-system-used' by accident.
1461 (let ((last-coding-system-used last-coding-system-used))
1462 (if (file-remote-p filename)
1463 (with-parsed-tramp-file-name filename nil
1464 (if (and (zerop (user-uid)) (tramp-local-host-p v))
1465 ;; If we are root on the local host, we can do it directly.
1466 (tramp-set-file-uid-gid localname uid gid)
1467 (let ((uid (or (and (integerp uid) uid)
1468 (tramp-get-remote-uid v 'integer)))
1469 (gid (or (and (integerp gid) gid)
1470 (tramp-get-remote-gid v 'integer))))
1473 "chown %d:%d %s" uid gid
1474 (tramp-shell-quote-argument localname))))))
1476 ;; We handle also the local part, because there doesn't exist
1477 ;; `set-file-uid-gid'. On W32 "chown" might not work.
1478 (let ((uid (or (and (integerp uid) uid) (tramp-get-local-uid 'integer)))
1479 (gid (or (and (integerp gid) gid) (tramp-get-local-gid 'integer))))
1480 (tramp-compat-call-process
1482 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
1484 (defun tramp-remote-selinux-p (vec)
1485 "Check, whether SELINUX is enabled on the remote host."
1486 (with-tramp-connection-property
1487 (tramp-get-connection-process vec) "selinux-p"
1488 (let ((result (tramp-find-executable
1489 vec "getenforce" (tramp-get-remote-path vec) t t)))
1492 (tramp-send-command-and-read
1493 vec (format "echo \\\"`%S`\\\"" result))
1496 (defun tramp-sh-handle-file-selinux-context (filename)
1497 "Like `file-selinux-context' for Tramp files."
1498 (with-parsed-tramp-file-name filename nil
1499 (with-tramp-file-property v localname "file-selinux-context"
1500 (let ((context '(nil nil nil nil))
1501 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
1502 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
1503 (when (and (tramp-remote-selinux-p v)
1504 (tramp-send-command-and-check
1507 (tramp-get-ls-command v)
1508 (tramp-shell-quote-argument localname))))
1509 (with-current-buffer (tramp-get-connection-buffer v)
1510 (goto-char (point-min))
1511 (when (re-search-forward regexp (point-at-eol) t)
1512 (setq context (list (match-string 1) (match-string 2)
1513 (match-string 3) (match-string 4))))))
1514 ;; Return the context.
1517 (defun tramp-sh-handle-set-file-selinux-context (filename context)
1518 "Like `set-file-selinux-context' for Tramp files."
1519 (with-parsed-tramp-file-name filename nil
1520 (if (and (consp context)
1521 (tramp-remote-selinux-p v)
1522 (tramp-send-command-and-check
1523 v (format "chcon %s %s %s %s %s"
1524 (if (stringp (nth 0 context))
1525 (format "--user=%s" (nth 0 context)) "")
1526 (if (stringp (nth 1 context))
1527 (format "--role=%s" (nth 1 context)) "")
1528 (if (stringp (nth 2 context))
1529 (format "--type=%s" (nth 2 context)) "")
1530 (if (stringp (nth 3 context))
1531 (format "--range=%s" (nth 3 context)) "")
1532 (tramp-shell-quote-argument localname))))
1533 (tramp-set-file-property v localname "file-selinux-context" context)
1534 (tramp-set-file-property v localname "file-selinux-context" 'undef)))
1535 ;; We always return nil.
1538 ;; Simple functions using the `test' command.
1540 (defun tramp-sh-handle-file-executable-p (filename)
1541 "Like `file-executable-p' for Tramp files."
1542 (with-parsed-tramp-file-name filename nil
1543 (with-tramp-file-property v localname "file-executable-p"
1544 ;; Examine `file-attributes' cache to see if request can be
1545 ;; satisfied without remote operation.
1546 (or (tramp-check-cached-permissions v ?x)
1547 (tramp-run-test "-x" filename)))))
1549 (defun tramp-sh-handle-file-readable-p (filename)
1550 "Like `file-readable-p' for Tramp files."
1551 (with-parsed-tramp-file-name filename nil
1552 (with-tramp-file-property v localname "file-readable-p"
1553 ;; Examine `file-attributes' cache to see if request can be
1554 ;; satisfied without remote operation.
1555 (or (tramp-check-cached-permissions v ?r)
1556 (tramp-run-test "-r" filename)))))
1558 ;; When the remote shell is started, it looks for a shell which groks
1559 ;; tilde expansion. Here, we assume that all shells which grok tilde
1560 ;; expansion will also provide a `test' command which groks `-nt' (for
1561 ;; newer than). If this breaks, tell me about it and I'll try to do
1562 ;; something smarter about it.
1563 (defun tramp-sh-handle-file-newer-than-file-p (file1 file2)
1564 "Like `file-newer-than-file-p' for Tramp files."
1565 (cond ((not (file-exists-p file1))
1567 ((not (file-exists-p file2))
1569 ;; We are sure both files exist at this point.
1572 ;; We try to get the mtime of both files. If they are not
1573 ;; equal to the "dont-know" value, then we subtract the times
1574 ;; and obtain the result.
1575 (let ((fa1 (file-attributes file1))
1576 (fa2 (file-attributes file2)))
1577 (if (and (not (equal (nth 5 fa1) '(0 0)))
1578 (not (equal (nth 5 fa2) '(0 0))))
1579 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
1580 ;; If one of them is the dont-know value, then we can
1581 ;; still try to run a shell command on the remote host.
1582 ;; However, this only works if both files are Tramp
1583 ;; files and both have the same method, same user, same
1585 (unless (tramp-equal-remote file1 file2)
1586 (with-parsed-tramp-file-name
1587 (if (tramp-tramp-file-p file1) file1 file2) nil
1590 "Files %s and %s must have same method, user, host"
1592 (with-parsed-tramp-file-name file1 nil
1594 (tramp-get-test-nt-command v) file1 file2))))))))
1596 ;; Functions implemented using the basic functions above.
1598 (defun tramp-sh-handle-file-directory-p (filename)
1599 "Like `file-directory-p' for Tramp files."
1600 (with-parsed-tramp-file-name filename nil
1601 ;; `file-directory-p' is used as predicate for filename completion.
1602 ;; Sometimes, when a connection is not established yet, it is
1603 ;; desirable to return t immediately for "/method:foo:". It can
1604 ;; be expected that this is always a directory.
1605 (or (zerop (length localname))
1606 (with-tramp-file-property v localname "file-directory-p"
1607 (tramp-run-test "-d" filename)))))
1609 (defun tramp-sh-handle-file-writable-p (filename)
1610 "Like `file-writable-p' for Tramp files."
1611 (with-parsed-tramp-file-name filename nil
1612 (with-tramp-file-property v localname "file-writable-p"
1613 (if (file-exists-p filename)
1614 ;; Examine `file-attributes' cache to see if request can be
1615 ;; satisfied without remote operation.
1616 (or (tramp-check-cached-permissions v ?w)
1617 (tramp-run-test "-w" filename))
1618 ;; If file doesn't exist, check if directory is writable.
1619 (and (tramp-run-test "-d" (file-name-directory filename))
1620 (tramp-run-test "-w" (file-name-directory filename)))))))
1622 (defun tramp-sh-handle-file-ownership-preserved-p (filename)
1623 "Like `file-ownership-preserved-p' for Tramp files."
1624 (with-parsed-tramp-file-name filename nil
1625 (with-tramp-file-property v localname "file-ownership-preserved-p"
1626 (let ((attributes (file-attributes filename)))
1627 ;; Return t if the file doesn't exist, since it's true that no
1628 ;; information would be lost by an (attempted) delete and create.
1629 (or (null attributes)
1630 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer)))))))
1632 ;; Directory listings.
1634 (defun tramp-sh-handle-directory-files-and-attributes
1635 (directory &optional full match nosort id-format)
1636 "Like `directory-files-and-attributes' for Tramp files."
1637 (unless id-format (setq id-format 'integer))
1638 (when (file-directory-p directory)
1639 (setq directory (expand-file-name directory))
1642 (with-parsed-tramp-file-name directory nil
1643 (with-tramp-file-property
1645 (format "directory-files-and-attributes-%s" id-format)
1650 (tramp-convert-file-attributes v (cdr x))))
1652 ((tramp-get-remote-stat v)
1653 (tramp-do-directory-files-and-attributes-with-stat
1654 v localname id-format))
1655 ((tramp-get-remote-perl v)
1656 (tramp-do-directory-files-and-attributes-with-perl
1657 v localname id-format)))))))))
1661 (setq item (pop temp))
1662 (when (or (null match) (string-match match (car item)))
1664 (setcar item (expand-file-name (car item) directory)))
1665 (push item result)))
1669 (sort result (lambda (x y) (string< (car x) (car y))))))))
1671 (defun tramp-do-directory-files-and-attributes-with-perl
1672 (vec localname &optional id-format)
1673 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
1674 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
1675 (tramp-maybe-send-script
1676 vec tramp-perl-directory-files-and-attributes
1677 "tramp_perl_directory_files_and_attributes")
1679 (tramp-send-command-and-read
1681 (format "tramp_perl_directory_files_and_attributes %s %s"
1682 (tramp-shell-quote-argument localname) id-format))))
1683 (when (stringp object) (tramp-error vec 'file-error object))
1686 (defun tramp-do-directory-files-and-attributes-with-stat
1687 (vec localname &optional id-format)
1688 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
1689 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
1690 (tramp-send-command-and-read
1694 ;; We must care about filenames with spaces, or starting with
1695 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
1696 ;; but it does not work on all remote systems. Therefore, we
1697 ;; quote the filenames via sed.
1698 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | "
1700 "'(\"%%n\" (\"%%N\") %%h %s %s %%Xe0 %%Ye0 %%Ze0 %%se0 \"%%A\" t %%ie0 -1)'"
1701 " 2>/dev/null); echo \")\"")
1702 (tramp-shell-quote-argument localname)
1703 (tramp-get-ls-command vec)
1704 (tramp-get-remote-stat vec)
1705 (if (eq id-format 'integer) "%ue0" "\"%U\"")
1706 (if (eq id-format 'integer) "%ge0" "\"%G\""))))
1708 ;; This function should return "foo/" for directories and "bar" for
1710 (defun tramp-sh-handle-file-name-all-completions (filename directory)
1711 "Like `file-name-all-completions' for Tramp files."
1712 (unless (save-match-data (string-match "/" filename))
1713 (with-parsed-tramp-file-name (expand-file-name directory) nil
1720 ;; Try cache entries for filename, filename with last
1721 ;; character removed, filename with last two characters
1722 ;; removed, ..., and finally the empty string - all
1723 ;; concatenated to the local directory name.
1724 (let ((remote-file-name-inhibit-cache
1725 (or remote-file-name-inhibit-cache
1726 tramp-completion-reread-directory-timeout)))
1728 ;; This is inefficient for very long filenames, pity
1729 ;; `reduce' is not available...
1736 (tramp-get-file-property
1738 (concat localname (substring filename 0 x))
1739 "file-name-all-completions"
1741 (when cache-hit (list cache-hit))))
1742 ;; We cannot use a length of 0, because file properties
1743 ;; for "foo" and "foo/" are identical.
1744 (tramp-compat-number-sequence (length filename) 1 -1)))))
1746 ;; Cache expired or no matching cache entry found so we need
1747 ;; to perform a remote operation.
1749 ;; Get a list of directories and files, including reliably
1750 ;; tagging the directories with a trailing '/'. Because I
1751 ;; rock. --daniel@danann.net
1753 ;; Changed to perform `cd' in the same remote op and only
1754 ;; get entries starting with `filename'. Capture any `cd'
1755 ;; error messages. Ensure any `cd' and `echo' aliases are
1759 (if (tramp-get-remote-perl v)
1761 (tramp-maybe-send-script
1762 v tramp-perl-file-name-all-completions
1763 "tramp_perl_file_name_all_completions")
1764 (format "tramp_perl_file_name_all_completions %s %s %d"
1765 (tramp-shell-quote-argument localname)
1766 (tramp-shell-quote-argument filename)
1768 ;; `read-file-name-completion-ignore-case'
1769 ;; is introduced with Emacs 22.1.
1771 'read-file-name-completion-ignore-case)
1772 'read-file-name-completion-ignore-case
1773 'completion-ignore-case))
1777 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
1778 ;; `ls' with wildcard might fail with `Argument
1779 ;; list too long' error in some corner cases; if
1780 ;; `ls' fails after `cd' succeeded, chances are
1781 ;; that's the case, so let's retry without
1782 ;; wildcard. This will return "too many" entries
1783 ;; but that isn't harmful.
1784 " || %s -a 2>/dev/null)"
1785 " | while read f; do"
1786 " if %s -d \"$f\" 2>/dev/null;"
1787 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
1788 " && \\echo ok) || \\echo fail")
1789 (tramp-shell-quote-argument localname)
1790 (tramp-get-ls-command v)
1791 ;; When `filename' is empty, just `ls' without
1792 ;; filename argument is more efficient than `ls *'
1793 ;; for very large directories and might avoid the
1794 ;; `Argument list too long' error.
1796 ;; With and only with wildcard, we need to add
1797 ;; `-d' to prevent `ls' from descending into
1799 (if (zerop (length filename))
1801 (concat (tramp-shell-quote-argument filename) "* -d"))
1802 (tramp-get-ls-command v)
1803 (tramp-get-test-command v))))
1805 ;; Now grab the output.
1806 (with-current-buffer (tramp-get-buffer v)
1807 (goto-char (point-max))
1809 ;; Check result code, found in last line of output.
1811 (if (looking-at "^fail$")
1813 ;; Grab error message from line before last line
1814 ;; (it was put there by `cd 2>&1').
1818 "tramp-sh-handle-file-name-all-completions: %s"
1819 (buffer-substring (point) (point-at-eol))))
1820 ;; For peace of mind, if buffer doesn't end in `fail'
1821 ;; then it should end in `ok'. If neither are in the
1822 ;; buffer something went seriously wrong on the remote
1824 (unless (looking-at "^ok$")
1828 tramp-sh-handle-file-name-all-completions: internal error accessing `%s': `%s'"
1829 (tramp-shell-quote-argument localname) (buffer-string))))
1831 (while (zerop (forward-line -1))
1832 (push (buffer-substring (point) (point-at-eol)) result)))
1834 ;; Because the remote op went through OK we know the
1835 ;; directory we `cd'-ed to exists.
1836 (tramp-set-file-property v localname "file-exists-p" t)
1838 ;; Because the remote op went through OK we know every
1839 ;; file listed by `ls' exists.
1840 (mapc (lambda (entry)
1841 (tramp-set-file-property
1842 v (concat localname entry) "file-exists-p" t))
1845 ;; Store result in the cache.
1846 (tramp-set-file-property
1847 v (concat localname filename)
1848 "file-name-all-completions" result))))))))
1852 (defun tramp-sh-handle-add-name-to-file
1853 (filename newname &optional ok-if-already-exists)
1854 "Like `add-name-to-file' for Tramp files."
1855 (unless (tramp-equal-remote filename newname)
1856 (with-parsed-tramp-file-name
1857 (if (tramp-tramp-file-p filename) filename newname) nil
1860 "add-name-to-file: %s"
1861 "only implemented for same method, same user, same host")))
1862 (with-parsed-tramp-file-name filename v1
1863 (with-parsed-tramp-file-name newname v2
1864 (let ((ln (when v1 (tramp-get-remote-ln v1))))
1865 (when (and (not ok-if-already-exists)
1866 (file-exists-p newname)
1867 (not (numberp ok-if-already-exists))
1870 "File %s already exists; make it a new name anyway? "
1874 "add-name-to-file: file %s already exists" newname))
1875 (tramp-flush-file-property v2 (file-name-directory v2-localname))
1876 (tramp-flush-file-property v2 v2-localname)
1877 (tramp-barf-unless-okay
1879 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
1880 (tramp-shell-quote-argument v2-localname))
1881 "error with add-name-to-file, see buffer `%s' for details"
1884 (defun tramp-sh-handle-copy-file
1885 (filename newname &optional ok-if-already-exists keep-date
1886 preserve-uid-gid preserve-selinux-context)
1887 "Like `copy-file' for Tramp files."
1888 (setq filename (expand-file-name filename))
1889 (setq newname (expand-file-name newname))
1891 ;; At least one file a Tramp file?
1892 ((or (tramp-tramp-file-p filename)
1893 (tramp-tramp-file-p newname))
1894 (tramp-do-copy-or-rename-file
1895 'copy filename newname ok-if-already-exists keep-date
1896 preserve-uid-gid preserve-selinux-context))
1898 (preserve-selinux-context
1899 (tramp-run-real-handler
1901 (list filename newname ok-if-already-exists keep-date
1902 preserve-uid-gid preserve-selinux-context)))
1904 (tramp-run-real-handler
1906 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
1908 (tramp-run-real-handler
1909 'copy-file (list filename newname ok-if-already-exists keep-date)))))
1911 (defun tramp-sh-handle-copy-directory
1912 (dirname newname &optional keep-date parents copy-contents)
1913 "Like `copy-directory' for Tramp files."
1914 (let ((t1 (tramp-tramp-file-p dirname))
1915 (t2 (tramp-tramp-file-p newname)))
1916 (with-parsed-tramp-file-name (if t1 dirname newname) nil
1917 (if (and (tramp-get-method-parameter method 'tramp-copy-recursive)
1918 ;; When DIRNAME and NEWNAME are remote, they must have
1920 (or (null t1) (null t2)
1922 (tramp-file-name-method (tramp-dissect-file-name dirname))
1923 (tramp-file-name-method (tramp-dissect-file-name newname)))))
1924 ;; scp or rsync DTRT.
1926 (setq dirname (directory-file-name (expand-file-name dirname))
1927 newname (directory-file-name (expand-file-name newname)))
1928 (if (and (file-directory-p newname)
1929 (not (string-equal (file-name-nondirectory dirname)
1930 (file-name-nondirectory newname))))
1933 (file-name-nondirectory dirname) newname)))
1934 (if (not (file-directory-p (file-name-directory newname)))
1935 (make-directory (file-name-directory newname) parents))
1936 (tramp-do-copy-or-rename-file-out-of-band
1937 'copy dirname newname keep-date))
1938 ;; We must do it file-wise.
1939 (tramp-run-real-handler
1940 'copy-directory (list dirname newname keep-date parents)))
1942 ;; When newname did exist, we have wrong cached values.
1944 (with-parsed-tramp-file-name newname nil
1945 (tramp-flush-file-property v (file-name-directory localname))
1946 (tramp-flush-file-property v localname))))))
1948 (defun tramp-sh-handle-rename-file
1949 (filename newname &optional ok-if-already-exists)
1950 "Like `rename-file' for Tramp files."
1951 ;; Check if both files are local -- invoke normal rename-file.
1952 ;; Otherwise, use Tramp from local system.
1953 (setq filename (expand-file-name filename))
1954 (setq newname (expand-file-name newname))
1955 ;; At least one file a Tramp file?
1956 (if (or (tramp-tramp-file-p filename)
1957 (tramp-tramp-file-p newname))
1958 (tramp-do-copy-or-rename-file
1959 'rename filename newname ok-if-already-exists t t)
1960 (tramp-run-real-handler
1961 'rename-file (list filename newname ok-if-already-exists))))
1963 (defun tramp-do-copy-or-rename-file
1964 (op filename newname &optional ok-if-already-exists keep-date
1965 preserve-uid-gid preserve-selinux-context)
1966 "Copy or rename a remote file.
1967 OP must be `copy' or `rename' and indicates the operation to perform.
1968 FILENAME specifies the file to copy or rename, NEWNAME is the name of
1969 the new file (for copy) or the new name of the file (for rename).
1970 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
1971 KEEP-DATE means to make sure that NEWNAME has the same timestamp
1972 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
1973 the uid and gid if both files are on the same host.
1974 PRESERVE-SELINUX-CONTEXT activates selinux commands.
1976 This function is invoked by `tramp-sh-handle-copy-file' and
1977 `tramp-sh-handle-rename-file'. It is an error if OP is neither
1978 of `copy' and `rename'. FILENAME and NEWNAME must be absolute
1980 (unless (memq op '(copy rename))
1981 (error "Unknown operation `%s', must be `copy' or `rename'" op))
1982 (let ((t1 (tramp-tramp-file-p filename))
1983 (t2 (tramp-tramp-file-p newname))
1984 (length (nth 7 (file-attributes (file-truename filename))))
1985 (context (and preserve-selinux-context
1986 (apply 'file-selinux-context (list filename))))
1989 (with-parsed-tramp-file-name (if t1 filename newname) nil
1990 (when (and (not ok-if-already-exists) (file-exists-p newname))
1992 v 'file-already-exists "File %s already exists" newname))
1994 (with-tramp-progress-reporter
1995 v 0 (format "%s %s to %s"
1996 (if (eq op 'copy) "Copying" "Renaming")
2000 ;; Both are Tramp files.
2002 (with-parsed-tramp-file-name filename v1
2003 (with-parsed-tramp-file-name newname v2
2005 ;; Shortcut: if method, host, user are the same for
2006 ;; both files, we invoke `cp' or `mv' on the remote
2008 ((tramp-equal-remote filename newname)
2009 (tramp-do-copy-or-rename-file-directly
2011 ok-if-already-exists keep-date preserve-uid-gid))
2013 ;; Try out-of-band operation.
2015 (tramp-method-out-of-band-p v1 length)
2016 (tramp-method-out-of-band-p v2 length))
2017 (tramp-do-copy-or-rename-file-out-of-band
2018 op filename newname keep-date))
2020 ;; No shortcut was possible. So we copy the file
2021 ;; first. If the operation was `rename', we go back
2022 ;; and delete the original file (if the copy was
2023 ;; successful). The approach is simple-minded: we
2024 ;; create a new buffer, insert the contents of the
2025 ;; source file into it, then write out the buffer to
2026 ;; the target file. The advantage is that it doesn't
2027 ;; matter which filename handlers are used for the
2028 ;; source and target file.
2030 (tramp-do-copy-or-rename-file-via-buffer
2031 op filename newname keep-date))))))
2033 ;; One file is a Tramp file, the other one is local.
2036 ;; Fast track on local machine.
2037 ((tramp-local-host-p v)
2038 (tramp-do-copy-or-rename-file-directly
2040 ok-if-already-exists keep-date preserve-uid-gid))
2042 ;; If the Tramp file has an out-of-band method, the
2043 ;; corresponding copy-program can be invoked.
2044 ((tramp-method-out-of-band-p v length)
2045 (tramp-do-copy-or-rename-file-out-of-band
2046 op filename newname keep-date))
2048 ;; Use the inline method via a Tramp buffer.
2049 (t (tramp-do-copy-or-rename-file-via-buffer
2050 op filename newname keep-date))))
2053 ;; One of them must be a Tramp file.
2054 (error "Tramp implementation says this cannot happen")))
2056 ;; Handle `preserve-selinux-context'.
2057 (when context (apply 'set-file-selinux-context (list newname context)))
2059 ;; In case of `rename', we must flush the cache of the source file.
2060 (when (and t1 (eq op 'rename))
2061 (with-parsed-tramp-file-name filename v1
2062 (tramp-flush-file-property v1 (file-name-directory localname))
2063 (tramp-flush-file-property v1 localname)))
2065 ;; When newname did exist, we have wrong cached values.
2067 (with-parsed-tramp-file-name newname v2
2068 (tramp-flush-file-property v2 (file-name-directory localname))
2069 (tramp-flush-file-property v2 localname)))))))
2071 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
2072 "Use an Emacs buffer to copy or rename a file.
2073 First arg OP is either `copy' or `rename' and indicates the operation.
2074 FILENAME is the source file, NEWNAME the target file.
2075 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
2077 ;; We must disable multibyte, because binary data shall not be
2079 (set-buffer-multibyte nil)
2080 (let ((coding-system-for-read 'binary)
2081 (jka-compr-inhibit t))
2082 (insert-file-contents-literally filename))
2083 ;; We don't want the target file to be compressed, so we let-bind
2084 ;; `jka-compr-inhibit' to t.
2085 (let ((coding-system-for-write 'binary)
2086 (jka-compr-inhibit t))
2087 (write-region (point-min) (point-max) newname)))
2088 ;; KEEP-DATE handling.
2089 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
2091 (set-file-modes newname (tramp-default-file-modes filename))
2092 ;; If the operation was `rename', delete the original file.
2093 (unless (eq op 'copy) (delete-file filename)))
2095 (defun tramp-do-copy-or-rename-file-directly
2096 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
2097 "Invokes `cp' or `mv' on the remote system.
2098 OP must be one of `copy' or `rename', indicating `cp' or `mv',
2099 respectively. FILENAME specifies the file to copy or rename,
2100 NEWNAME is the name of the new file (for copy) or the new name of
2101 the file (for rename). Both files must reside on the same host.
2102 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2103 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
2104 the uid and gid from FILENAME."
2105 (let ((t1 (tramp-tramp-file-p filename))
2106 (t2 (tramp-tramp-file-p newname))
2107 (file-times (nth 5 (file-attributes filename)))
2108 (file-modes (tramp-default-file-modes filename)))
2109 (with-parsed-tramp-file-name (if t1 filename newname) nil
2110 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
2111 ((eq op 'copy) "cp -f")
2112 ((eq op 'rename) "mv -f")
2115 "Unknown operation `%s', must be `copy' or `rename'"
2119 (tramp-file-name-handler 'file-remote-p filename 'localname)
2123 (tramp-file-name-handler 'file-remote-p newname 'localname)
2125 (prefix (file-remote-p (if t1 filename newname)))
2129 ;; Both files are on a remote host, with same user.
2132 (tramp-send-command-and-check
2133 v (format "%s %s %s" cmd
2134 (tramp-shell-quote-argument localname1)
2135 (tramp-shell-quote-argument localname2))))
2136 (with-current-buffer (tramp-get-buffer v)
2137 (goto-char (point-min))
2141 ;; Mask cp -f error.
2143 tramp-operation-not-permitted-regexp nil t))
2145 (tramp-error-with-buffer
2147 "Copying directly failed, see buffer `%s' for details."
2150 ;; We are on the local host.
2153 ;; We can do it directly.
2154 ((let (file-name-handler-alist)
2155 (and (file-readable-p localname1)
2156 (file-writable-p (file-name-directory localname2))
2157 (or (file-directory-p localname2)
2158 (file-writable-p localname2))))
2160 (tramp-compat-copy-file
2161 localname1 localname2 ok-if-already-exists
2162 keep-date preserve-uid-gid)
2163 (tramp-run-real-handler
2164 'rename-file (list localname1 localname2 ok-if-already-exists))))
2166 ;; We can do it directly with `tramp-send-command'
2167 ((and (file-readable-p (concat prefix localname1))
2169 (file-name-directory (concat prefix localname2)))
2170 (or (file-directory-p (concat prefix localname2))
2171 (file-writable-p (concat prefix localname2))))
2172 (tramp-do-copy-or-rename-file-directly
2173 op (concat prefix localname1) (concat prefix localname2)
2174 ok-if-already-exists keep-date t)
2175 ;; We must change the ownership to the local user.
2176 (tramp-set-file-uid-gid
2177 (concat prefix localname2)
2178 (tramp-get-local-uid 'integer)
2179 (tramp-get-local-gid 'integer)))
2181 ;; We need a temporary file in between.
2183 ;; Create the temporary file.
2184 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
2189 (tramp-barf-unless-okay
2192 (tramp-shell-quote-argument localname1)
2193 (tramp-shell-quote-argument tmpfile))
2194 "Copying directly failed, see buffer `%s' for details."
2195 (tramp-get-buffer v))
2196 ;; We must change the ownership as remote user.
2197 ;; Since this does not work reliable, we also
2198 ;; give read permissions.
2200 (concat prefix tmpfile)
2201 (tramp-compat-octal-to-decimal "0777"))
2202 (tramp-set-file-uid-gid
2203 (concat prefix tmpfile)
2204 (tramp-get-local-uid 'integer)
2205 (tramp-get-local-gid 'integer)))
2208 (tramp-compat-copy-file
2209 localname1 tmpfile t
2210 keep-date preserve-uid-gid)
2211 (tramp-run-real-handler
2213 (list localname1 tmpfile t)))
2214 ;; We must change the ownership as local user.
2215 ;; Since this does not work reliable, we also
2216 ;; give read permissions.
2218 tmpfile (tramp-compat-octal-to-decimal "0777"))
2219 (tramp-set-file-uid-gid
2221 (tramp-get-remote-uid v 'integer)
2222 (tramp-get-remote-gid v 'integer))))
2224 ;; Move the temporary file to its destination.
2227 (tramp-barf-unless-okay
2230 (tramp-shell-quote-argument tmpfile)
2231 (tramp-shell-quote-argument localname2))
2232 "Copying directly failed, see buffer `%s' for details."
2233 (tramp-get-buffer v)))
2235 (tramp-run-real-handler
2237 (list tmpfile localname2 ok-if-already-exists)))))
2240 (ignore-errors (delete-file tmpfile)))))))))
2242 ;; Set the time and mode. Mask possible errors.
2245 (set-file-times newname file-times)
2246 (set-file-modes newname file-modes))))))
2248 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
2249 "Invoke rcp program to copy.
2250 The method used must be an out-of-band method."
2251 (let* ((t1 (tramp-tramp-file-p filename))
2252 (t2 (tramp-tramp-file-p newname))
2253 (orig-vec (tramp-dissect-file-name (if t1 filename newname)))
2254 copy-program copy-args copy-env copy-keep-date port spec
2257 (with-parsed-tramp-file-name (if t1 filename newname) nil
2260 ;; Both are Tramp files. We shall optimize it when the
2261 ;; methods for filename and newname are the same.
2262 (let* ((dir-flag (file-directory-p filename))
2263 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
2267 (file-name-nondirectory newname) tmpfile)))
2270 (tramp-do-copy-or-rename-file-out-of-band
2271 op filename tmpfile keep-date)
2272 (tramp-do-copy-or-rename-file-out-of-band
2273 'rename tmpfile newname keep-date))
2277 (tramp-compat-delete-directory
2278 (expand-file-name ".." tmpfile) 'recursive)
2279 (delete-file tmpfile)))))
2281 ;; Set variables for computing the prompt for reading
2283 (setq tramp-current-method (tramp-file-name-method v)
2284 tramp-current-user (or (tramp-file-name-user v)
2285 (tramp-get-connection-property
2287 tramp-current-host (tramp-file-name-real-host v))
2289 ;; Expand hops. Might be necessary for gateway methods.
2290 (setq v (car (tramp-compute-multi-hops v)))
2291 (aset v 3 localname)
2293 ;; Check which ones of source and target are Tramp files.
2294 (setq source (if t1 (tramp-make-copy-program-file-name v) filename)
2296 (if (and (file-directory-p filename)
2298 (file-name-nondirectory filename)
2299 (file-name-nondirectory newname)))
2300 'file-name-directory
2302 (if t2 (tramp-make-copy-program-file-name v) newname)))
2304 ;; Check for host and port number. We cannot use
2305 ;; `tramp-file-name-port', because this returns also
2306 ;; `tramp-default-port', which might clash with settings in
2308 (setq host (tramp-file-name-host v)
2310 (when (string-match tramp-host-with-port-regexp host)
2311 (setq port (string-to-number (match-string 2 host))
2312 host (string-to-number (match-string 1 host))))
2314 ;; Check for user. There might be an interactive setting.
2315 (setq user (or (tramp-file-name-user v)
2316 (tramp-get-connection-property v "login-as" nil)))
2318 ;; Compose copy command.
2319 (setq host (or host "")
2322 spec (format-spec-make
2323 ?h host ?u user ?p port
2324 ?t (tramp-get-connection-property
2325 (tramp-get-connection-process v) "temp-file" "")
2326 ?k (if keep-date " " ""))
2327 copy-program (tramp-get-method-parameter
2328 method 'tramp-copy-program)
2329 copy-keep-date (tramp-get-method-parameter
2330 method 'tramp-copy-keep-date)
2333 ;; " " has either been a replacement of "%k" (when
2334 ;; keep-date argument is non-nil), or a replacement
2335 ;; for the whole keep-date sublist.
2339 (tramp-get-method-parameter method 'tramp-copy-args)
2344 (let ((y (mapcar (lambda (z) (format-spec z spec)) x)))
2345 (if (member "" y) '(" ") y))))))
2351 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
2352 (unless (member "" x) (mapconcat 'identity x " ")))
2353 (tramp-get-method-parameter method 'tramp-copy-env))))
2355 ;; Check for program.
2356 (unless (let ((default-directory
2357 (tramp-compat-temporary-file-directory)))
2358 (executable-find copy-program))
2360 v 'file-error "Cannot find copy program: %s" copy-program))
2364 ;; The default directory must be remote.
2365 (let ((default-directory
2366 (file-name-directory (if t1 filename newname)))
2367 (process-environment (copy-sequence process-environment)))
2368 ;; Set the transfer process properties.
2369 (tramp-set-connection-property
2370 v "process-name" (buffer-name (current-buffer)))
2371 (tramp-set-connection-property
2372 v "process-buffer" (current-buffer))
2375 orig-vec 5 "%s=\"%s\"" (car copy-env) (cadr copy-env))
2376 (setenv (pop copy-env) (pop copy-env)))
2378 ;; Use an asynchronous process. By this, password can
2379 ;; be handled. The default directory must be local, in
2380 ;; order to apply the correct `copy-program'. We don't
2381 ;; set a timeout, because the copying of large files can
2382 ;; last longer than 60 secs.
2383 (let ((p (let ((default-directory
2384 (tramp-compat-temporary-file-directory)))
2385 (apply 'start-process
2386 (tramp-get-connection-name v)
2387 (tramp-get-connection-buffer v)
2389 (append copy-args (list source target))))))
2392 (mapconcat 'identity (process-command p) " "))
2393 (tramp-compat-set-process-query-on-exit-flag p nil)
2394 (tramp-process-actions
2395 p v nil tramp-actions-copy-out-of-band)))
2397 ;; Reset the transfer process properties.
2398 (tramp-message orig-vec 6 "\n%s" (buffer-string))
2399 (tramp-set-connection-property v "process-name" nil)
2400 (tramp-set-connection-property v "process-buffer" nil)))
2402 ;; Handle KEEP-DATE argument.
2403 (when (and keep-date (not copy-keep-date))
2404 (set-file-times newname (nth 5 (file-attributes filename))))
2407 (unless (and keep-date copy-keep-date)
2409 (set-file-modes newname (tramp-default-file-modes filename)))))
2411 ;; If the operation was `rename', delete the original file.
2412 (unless (eq op 'copy)
2413 (if (file-regular-p filename)
2414 (delete-file filename)
2415 (tramp-compat-delete-directory filename 'recursive))))))
2417 (defun tramp-sh-handle-make-directory (dir &optional parents)
2418 "Like `make-directory' for Tramp files."
2419 (setq dir (expand-file-name dir))
2420 (with-parsed-tramp-file-name dir nil
2421 (tramp-flush-directory-property v (file-name-directory localname))
2423 (tramp-barf-unless-okay
2425 (if parents "mkdir -p" "mkdir")
2426 (tramp-shell-quote-argument localname))
2427 "Couldn't make directory %s" dir))))
2429 (defun tramp-sh-handle-delete-directory (directory &optional recursive)
2430 "Like `delete-directory' for Tramp files."
2431 (setq directory (expand-file-name directory))
2432 (with-parsed-tramp-file-name directory nil
2433 (tramp-flush-file-property v (file-name-directory localname))
2434 (tramp-flush-directory-property v localname)
2435 (tramp-barf-unless-okay
2437 (if recursive "rm -rf" "rmdir")
2438 (tramp-shell-quote-argument localname))
2439 "Couldn't delete %s" directory)))
2441 (defun tramp-sh-handle-delete-file (filename &optional trash)
2442 "Like `delete-file' for Tramp files."
2443 (setq filename (expand-file-name filename))
2444 (with-parsed-tramp-file-name filename nil
2445 (tramp-flush-file-property v (file-name-directory localname))
2446 (tramp-flush-file-property v localname)
2447 (tramp-barf-unless-okay
2449 (or (and trash (tramp-get-remote-trash v)) "rm -f")
2450 (tramp-shell-quote-argument localname))
2451 "Couldn't delete %s" filename)))
2455 ;; CCC: This does not seem to be enough. Something dies when
2456 ;; we try and delete two directories under Tramp :/
2457 (defun tramp-sh-handle-dired-recursive-delete-directory (filename)
2458 "Recursively delete the directory given.
2459 This is like `dired-recursive-delete-directory' for Tramp files."
2460 (with-parsed-tramp-file-name filename nil
2461 ;; Run a shell command 'rm -r <localname>'.
2462 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
2463 (unless (file-exists-p filename)
2464 (tramp-error v 'file-error "No such directory: %s" filename))
2465 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>).
2468 (format "rm -rf %s" (tramp-shell-quote-argument localname))
2469 ;; Don't read the output, do it explicitly.
2471 ;; Wait for the remote system to return to us...
2472 ;; This might take a while, allow it plenty of time.
2473 (tramp-wait-for-output (tramp-get-connection-process v) 120)
2474 ;; Make sure that it worked...
2475 (tramp-flush-file-property v (file-name-directory localname))
2476 (tramp-flush-directory-property v localname)
2477 (and (file-exists-p filename)
2479 v 'file-error "Failed to recursively delete %s" filename))))
2481 (defun tramp-sh-handle-dired-compress-file (file &rest ok-flag)
2482 "Like `dired-compress-file' for Tramp files."
2483 ;; OK-FLAG is valid for XEmacs only, but not implemented.
2484 ;; Code stolen mainly from dired-aux.el.
2485 (with-parsed-tramp-file-name file nil
2486 (tramp-flush-file-property v localname)
2489 (if (not (featurep 'xemacs))
2491 (symbol-value 'dired-compress-file-suffixes)
2492 ;; XEmacs has `dired-compression-method-alist', which is
2493 ;; transformed into `dired-compress-file-suffixes' structure.
2496 (list (concat (regexp-quote (nth 1 x)) "\\'")
2498 (mapconcat 'identity (nth 3 x) " ")))
2499 (symbol-value 'dired-compression-method-alist))))
2501 ;; See if any suffix rule matches this file name.
2503 (let (case-fold-search)
2504 (if (string-match (car (car suffixes)) localname)
2505 (setq suffix (car suffixes) suffixes nil))
2506 (setq suffixes (cdr suffixes))))
2508 (cond ((file-symlink-p file)
2510 ((and suffix (nth 2 suffix))
2511 ;; We found an uncompression rule.
2512 (with-tramp-progress-reporter
2513 v 0 (format "Uncompressing %s" file)
2514 (when (tramp-send-command-and-check
2515 v (concat (nth 2 suffix) " "
2516 (tramp-shell-quote-argument localname)))
2517 ;; `dired-remove-file' is not defined in XEmacs.
2518 (tramp-compat-funcall 'dired-remove-file file)
2519 (string-match (car suffix) file)
2520 (concat (substring file 0 (match-beginning 0))))))
2522 ;; We don't recognize the file as compressed, so compress it.
2524 (with-tramp-progress-reporter v 0 (format "Compressing %s" file)
2525 (when (tramp-send-command-and-check
2526 v (concat "gzip -f "
2527 (tramp-shell-quote-argument localname)))
2528 ;; `dired-remove-file' is not defined in XEmacs.
2529 (tramp-compat-funcall 'dired-remove-file file)
2530 (cond ((file-exists-p (concat file ".gz"))
2531 (concat file ".gz"))
2532 ((file-exists-p (concat file ".z"))
2536 (defun tramp-sh-handle-insert-directory
2537 (filename switches &optional wildcard full-directory-p)
2538 "Like `insert-directory' for Tramp files."
2539 (setq filename (expand-file-name filename))
2540 (with-parsed-tramp-file-name filename nil
2541 (if (and (featurep 'ls-lisp)
2542 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
2543 (tramp-run-real-handler
2544 'insert-directory (list filename switches wildcard full-directory-p))
2545 (when (stringp switches)
2546 (setq switches (split-string switches)))
2547 (when (and (member "--dired" switches)
2548 (not (tramp-get-ls-command-with-dired v)))
2549 (setq switches (delete "--dired" switches)))
2551 (setq wildcard (tramp-run-real-handler
2552 'file-name-nondirectory (list localname)))
2553 (setq localname (tramp-run-real-handler
2554 'file-name-directory (list localname))))
2555 (unless full-directory-p
2556 (setq switches (add-to-list 'switches "-d" 'append)))
2557 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
2559 (setq switches (concat switches " " wildcard)))
2561 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
2562 switches filename (if wildcard "yes" "no")
2563 (if full-directory-p "yes" "no"))
2564 ;; If `full-directory-p', we just say `ls -l FILENAME'.
2565 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
2566 (if full-directory-p
2569 (format "%s %s %s 2>/dev/null"
2570 (tramp-get-ls-command v)
2574 (tramp-shell-quote-argument (concat localname ".")))))
2575 (tramp-barf-unless-okay
2577 (format "cd %s" (tramp-shell-quote-argument
2578 (tramp-run-real-handler
2579 'file-name-directory (list localname))))
2581 (tramp-shell-quote-argument
2582 (tramp-run-real-handler 'file-name-directory (list localname))))
2586 (tramp-get-ls-command v)
2590 (tramp-run-real-handler
2591 'file-name-nondirectory (list localname)))))
2593 (tramp-shell-quote-argument
2594 (tramp-run-real-handler
2595 'file-name-nondirectory (list localname)))))))
2596 (let ((beg (point)))
2597 ;; We cannot use `insert-buffer-substring' because the Tramp
2598 ;; buffer changes its contents before insertion due to calling
2599 ;; `expand-file' and alike.
2601 (with-current-buffer (tramp-get-buffer v)
2604 ;; Check for "--dired" output.
2606 (when (looking-at "//SUBDIRED//")
2608 (when (looking-at "//DIRED//\\s-+")
2609 (let ((databeg (match-end 0))
2610 (end (point-at-eol)))
2611 ;; Now read the numeric positions of file names.
2613 (while (< (point) end)
2614 (let ((start (+ beg (read (current-buffer))))
2615 (end (+ beg (read (current-buffer)))))
2616 (if (memq (char-after end) '(?\n ?\ ))
2617 ;; End is followed by \n or by " -> ".
2618 (put-text-property start end 'dired-filename t))))))
2619 ;; Remove trailing lines.
2620 (goto-char (point-at-bol))
2621 (while (looking-at "//")
2623 (delete-region (match-beginning 0) (point)))
2625 ;; Some busyboxes are reluctant to discard colors.
2626 (unless (string-match "color" (tramp-get-connection-property v "ls" ""))
2628 (while (re-search-forward tramp-color-escape-sequence-regexp nil t)
2629 (replace-match "")))
2631 ;; The inserted file could be from somewhere else.
2632 (when (and (not wildcard) (not full-directory-p))
2633 (goto-char (point-max))
2634 (when (file-symlink-p filename)
2635 (goto-char (search-backward "->" beg 'noerror)))
2637 (if (zerop (length (file-name-nondirectory filename)))
2639 (file-name-nondirectory filename))
2641 (replace-match (file-relative-name filename) t))
2643 (goto-char (point-max))))))
2645 ;; Canonicalization of file names.
2647 (defun tramp-sh-handle-expand-file-name (name &optional dir)
2648 "Like `expand-file-name' for Tramp files.
2649 If the localname part of the given filename starts with \"/../\" then
2650 the result will be a local, non-Tramp, filename."
2651 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
2652 (setq dir (or dir default-directory "/"))
2653 ;; Unless NAME is absolute, concat DIR and NAME.
2654 (unless (file-name-absolute-p name)
2655 (setq name (concat (file-name-as-directory dir) name)))
2656 ;; If NAME is not a Tramp file, run the real handler.
2657 (if (not (tramp-connectable-p name))
2658 (tramp-run-real-handler 'expand-file-name (list name nil))
2660 (with-parsed-tramp-file-name name nil
2661 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
2662 (setq localname (concat "~/" localname)))
2663 ;; Tilde expansion if necessary. This needs a shell which
2664 ;; groks tilde expansion! The function `tramp-find-shell' is
2665 ;; supposed to find such a shell on the remote host. Please
2666 ;; tell me about it when this doesn't work on your system.
2667 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
2668 (let ((uname (match-string 1 localname))
2669 (fname (match-string 2 localname)))
2670 ;; We cannot simply apply "~/", because under sudo "~/" is
2671 ;; expanded to the local user home directory but to the
2672 ;; root home directory. On the other hand, using always
2673 ;; the default user name for tilde expansion is not
2674 ;; appropriate either, because ssh and companions might
2675 ;; use a user name from the config file.
2676 (when (and (string-equal uname "~")
2677 (string-match "\\`su\\(do\\)?\\'" method))
2678 (setq uname (concat uname user)))
2680 (with-tramp-connection-property v uname
2682 v (format "cd %s; pwd" (tramp-shell-quote-argument uname)))
2683 (with-current-buffer (tramp-get-buffer v)
2684 (goto-char (point-min))
2685 (buffer-substring (point) (point-at-eol)))))
2686 (setq localname (concat uname fname))))
2687 ;; There might be a double slash, for example when "~/"
2688 ;; expands to "/". Remove this.
2689 (while (string-match "//" localname)
2690 (setq localname (replace-match "/" t t localname)))
2691 ;; No tilde characters in file name, do normal
2692 ;; `expand-file-name' (this does "/./" and "/../"). We bind
2693 ;; `directory-sep-char' here for XEmacs on Windows, which would
2694 ;; otherwise use backslash. `default-directory' is bound,
2695 ;; because on Windows there would be problems with UNC shares or
2697 (let ((directory-sep-char ?/)
2698 (default-directory (tramp-compat-temporary-file-directory)))
2699 (tramp-make-tramp-file-name
2701 (tramp-drop-volume-letter
2702 (tramp-run-real-handler
2703 'expand-file-name (list localname)))
2706 ;;; Remote commands:
2708 (defun tramp-sh-handle-executable-find (command)
2709 "Like `executable-find' for Tramp files."
2710 (with-parsed-tramp-file-name default-directory nil
2711 (tramp-find-executable v command (tramp-get-remote-path v) t)))
2713 (defun tramp-process-sentinel (proc event)
2714 "Flush file caches."
2715 (unless (memq (process-status proc) '(run open))
2716 (let ((vec (tramp-get-connection-property proc "vector" nil)))
2718 (tramp-message vec 5 "Sentinel called: `%s' `%s'" proc event)
2719 (tramp-flush-connection-property proc)
2720 (tramp-flush-directory-property vec "")))))
2722 ;; We use BUFFER also as connection buffer during setup. Because of
2723 ;; this, its original contents must be saved, and restored once
2724 ;; connection has been setup.
2725 (defun tramp-sh-handle-start-file-process (name buffer program &rest args)
2726 "Like `start-file-process' for Tramp files."
2727 (with-parsed-tramp-file-name default-directory nil
2728 ;; When PROGRAM is nil, we just provide a tty.
2730 (when (stringp program)
2731 (format "cd %s; exec env PS1=%s %s"
2732 (tramp-shell-quote-argument localname)
2733 ;; Use a human-friendly prompt, for example for `shell'.
2734 (tramp-shell-quote-argument
2736 (file-remote-p default-directory)
2737 tramp-initial-end-of-output))
2738 (mapconcat 'tramp-shell-quote-argument
2739 (cons program args) " "))))
2740 (tramp-process-connection-type
2741 (or (null program) tramp-process-connection-type))
2742 (bmp (and (buffer-live-p buffer) (buffer-modified-p buffer)))
2747 ;; BUFFER can be nil. We use a temporary buffer.
2748 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
2749 (while (get-process name1)
2750 ;; NAME must be unique as process name.
2752 name1 (format "%s<%d>" name i)))
2754 ;; Set the new process properties.
2755 (tramp-set-connection-property v "process-name" name)
2756 (tramp-set-connection-property v "process-buffer" buffer)
2758 (with-current-buffer (tramp-get-connection-buffer v)
2762 ;; Activate narrowing in order to save BUFFER
2763 ;; contents. Clear also the modification time;
2764 ;; otherwise we might be interrupted by
2765 ;; `verify-visited-file-modtime'.
2766 (let ((buffer-undo-list t)
2767 (buffer-read-only nil)
2769 (clear-visited-file-modtime)
2770 (narrow-to-region (point-max) (point-max))
2771 ;; We call `tramp-maybe-open-connection', in order
2772 ;; to cleanup the prompt afterwards.
2773 (tramp-maybe-open-connection v)
2775 (delete-region mark (point))
2776 (narrow-to-region (point-max) (point-max))
2779 ;; Send the command.
2780 (tramp-send-command v command nil t) ; nooutput
2781 ;; Check, whether a pty is associated.
2782 (unless (tramp-compat-process-get
2783 (tramp-get-connection-process v) 'remote-tty)
2786 "pty association is not supported for `%s'" name))))
2787 (let ((p (tramp-get-connection-process v)))
2788 ;; Set query flag for this process. We ignore errors,
2789 ;; because the process could have finished already.
2791 (tramp-compat-set-process-query-on-exit-flag p t))
2796 (if (string-match tramp-temp-buffer-name (buffer-name))
2798 (set-process-buffer (tramp-get-connection-process v) nil)
2799 (kill-buffer (current-buffer)))
2800 (set-buffer-modified-p bmp))
2801 (tramp-set-connection-property v "process-name" nil)
2802 (tramp-set-connection-property v "process-buffer" nil))))))
2804 (defun tramp-sh-handle-process-file
2805 (program &optional infile destination display &rest args)
2806 "Like `process-file' for Tramp files."
2807 ;; The implementation is not complete yet.
2808 (when (and (numberp destination) (zerop destination))
2809 (error "Implementation does not handle immediate return"))
2811 (with-parsed-tramp-file-name default-directory nil
2812 (let (command input tmpinput stderr tmpstderr outbuf ret)
2814 (setq command (mapconcat 'tramp-shell-quote-argument
2815 (cons program args) " "))
2818 (setq input "/dev/null")
2819 (setq infile (expand-file-name infile))
2820 (if (tramp-equal-remote default-directory infile)
2821 ;; INFILE is on the same remote host.
2822 (setq input (with-parsed-tramp-file-name infile nil localname))
2823 ;; INFILE must be copied to remote host.
2824 (setq input (tramp-make-tramp-temp-file v)
2825 tmpinput (tramp-make-tramp-file-name method user host input))
2826 (copy-file infile tmpinput t)))
2827 (when input (setq command (format "%s <%s" command input)))
2829 ;; Determine output.
2832 ((bufferp destination)
2833 (setq outbuf destination))
2835 ((stringp destination)
2836 (setq outbuf (get-buffer-create destination)))
2837 ;; (REAL-DESTINATION ERROR-DESTINATION)
2838 ((consp destination)
2841 ((bufferp (car destination))
2842 (setq outbuf (car destination)))
2843 ((stringp (car destination))
2844 (setq outbuf (get-buffer-create (car destination))))
2846 (setq outbuf (current-buffer))))
2849 ((stringp (cadr destination))
2850 (setcar (cdr destination) (expand-file-name (cadr destination)))
2851 (if (tramp-equal-remote default-directory (cadr destination))
2852 ;; stderr is on the same remote host.
2853 (setq stderr (with-parsed-tramp-file-name
2854 (cadr destination) nil localname))
2855 ;; stderr must be copied to remote host. The temporary
2856 ;; file must be deleted after execution.
2857 (setq stderr (tramp-make-tramp-temp-file v)
2858 tmpstderr (tramp-make-tramp-file-name
2859 method user host stderr))))
2860 ;; stderr to be discarded.
2861 ((null (cadr destination))
2862 (setq stderr "/dev/null"))))
2865 (setq outbuf (current-buffer))))
2866 (when stderr (setq command (format "%s 2>%s" command stderr)))
2868 ;; Send the command. It might not return in time, so we protect
2869 ;; it. Call it in a subshell, in order to preserve working
2874 (if (tramp-send-command-and-check
2875 v (format "\\cd %s; %s"
2876 (tramp-shell-quote-argument localname)
2880 ;; We should show the output anyway.
2882 (with-current-buffer outbuf
2884 (with-current-buffer (tramp-get-connection-buffer v)
2886 (when display (display-buffer outbuf))))
2887 ;; When the user did interrupt, we should do it also. We use
2888 ;; return code -1 as marker.
2890 (kill-buffer (tramp-get-connection-buffer v))
2894 (kill-buffer (tramp-get-connection-buffer v))
2897 ;; Provide error file.
2898 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
2900 ;; Cleanup. We remove all file cache values for the connection,
2901 ;; because the remote process could have changed them.
2902 (when tmpinput (delete-file tmpinput))
2904 ;; `process-file-side-effects' has been introduced with GNU
2905 ;; Emacs 23.2. If set to `nil', no remote file will be changed
2906 ;; by `program'. If it doesn't exist, we assume its default
2908 (unless (and (boundp 'process-file-side-effects)
2909 (not (symbol-value 'process-file-side-effects)))
2910 (tramp-flush-directory-property v ""))
2912 ;; Return exit status.
2917 (defun tramp-sh-handle-call-process-region
2918 (start end program &optional delete buffer display &rest args)
2919 "Like `call-process-region' for Tramp files."
2920 (let ((tmpfile (tramp-compat-make-temp-file "")))
2921 (write-region start end tmpfile)
2922 (when delete (delete-region start end))
2924 (apply 'call-process program tmpfile buffer display args)
2925 (delete-file tmpfile))))
2927 (defun tramp-sh-handle-file-local-copy (filename)
2928 "Like `file-local-copy' for Tramp files."
2929 (with-parsed-tramp-file-name filename nil
2930 (unless (file-exists-p filename)
2933 "Cannot make local copy of non-existing file `%s'" filename))
2935 (let* ((size (nth 7 (file-attributes (file-truename filename))))
2936 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
2937 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
2938 (tmpfile (tramp-compat-make-temp-file filename)))
2942 ;; `copy-file' handles direct copy and out-of-band methods.
2943 ((or (tramp-local-host-p v)
2944 (tramp-method-out-of-band-p v size))
2945 (copy-file filename tmpfile t t))
2947 ;; Use inline encoding for file transfer.
2950 (with-tramp-progress-reporter
2951 v 3 (format "Encoding remote file %s" filename)
2952 (tramp-barf-unless-okay
2953 v (format rem-enc (tramp-shell-quote-argument localname))
2954 "Encoding remote file failed"))
2956 (if (functionp loc-dec)
2957 ;; If local decoding is a function, we call it. We
2958 ;; must disable multibyte, because
2959 ;; `uudecode-decode-region' doesn't handle it
2962 (set-buffer-multibyte nil)
2963 (insert-buffer-substring (tramp-get-buffer v))
2964 (with-tramp-progress-reporter
2965 v 3 (format "Decoding remote file %s with function %s"
2967 (funcall loc-dec (point-min) (point-max))
2968 ;; Unset `file-name-handler-alist'. Otherwise,
2969 ;; epa-file gets confused.
2970 (let (file-name-handler-alist
2971 (coding-system-for-write 'binary))
2972 (write-region (point-min) (point-max) tmpfile))))
2974 ;; If tramp-decoding-function is not defined for this
2975 ;; method, we invoke tramp-decoding-command instead.
2976 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
2977 ;; Unset `file-name-handler-alist'. Otherwise,
2978 ;; epa-file gets confused.
2979 (let (file-name-handler-alist
2980 (coding-system-for-write 'binary))
2981 (write-region (point-min) (point-max) tmpfile2))
2982 (with-tramp-progress-reporter
2983 v 3 (format "Decoding remote file %s with command %s"
2986 (tramp-call-local-coding-command
2987 loc-dec tmpfile2 tmpfile)
2988 (delete-file tmpfile2)))))
2990 ;; Set proper permissions.
2991 (set-file-modes tmpfile (tramp-default-file-modes filename))
2992 ;; Set local user ownership.
2993 (tramp-set-file-uid-gid tmpfile)))
2995 ;; Oops, I don't know what to do.
2997 v 'file-error "Wrong method specification for `%s'" method)))
3001 (delete-file tmpfile)
3002 (signal (car err) (cdr err))))
3004 (run-hooks 'tramp-handle-file-local-copy-hook)
3007 ;; This is needed for XEmacs only. Code stolen from files.el.
3008 (defun tramp-sh-handle-insert-file-contents-literally
3009 (filename &optional visit beg end replace)
3010 "Like `insert-file-contents-literally' for Tramp files."
3011 (let ((format-alist nil)
3012 (after-insert-file-functions nil)
3013 (coding-system-for-read 'no-conversion)
3014 (coding-system-for-write 'no-conversion)
3015 (find-buffer-file-type-function
3016 (if (fboundp 'find-buffer-file-type)
3017 (symbol-function 'find-buffer-file-type)
3019 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler))
3020 (inhibit-file-name-operation 'insert-file-contents))
3023 (fset 'find-buffer-file-type (lambda (filename) t))
3024 (insert-file-contents filename visit beg end replace))
3026 (if find-buffer-file-type-function
3027 (fset 'find-buffer-file-type find-buffer-file-type-function)
3028 (fmakunbound 'find-buffer-file-type)))))
3030 (defun tramp-sh-handle-make-auto-save-file-name ()
3031 "Like `make-auto-save-file-name' for Tramp files.
3032 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
3033 (let ((tramp-auto-save-directory tramp-auto-save-directory)
3035 (tramp-subst-strs-in-string
3042 (buffer-file-name))))
3043 ;; File name must be unique. This is ensured with Emacs 22 (see
3044 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
3045 ;; all other cases we must do it ourselves.
3046 (when (boundp 'auto-save-file-name-transforms)
3049 (when (and (string-match (car x) buffer-file-name)
3050 (not (car (cddr x))))
3051 (setq tramp-auto-save-directory
3052 (or tramp-auto-save-directory
3053 (tramp-compat-temporary-file-directory)))))
3054 (symbol-value 'auto-save-file-name-transforms)))
3055 ;; Create directory.
3056 (when tramp-auto-save-directory
3057 (setq buffer-file-name
3058 (expand-file-name buffer-file-name tramp-auto-save-directory))
3059 (unless (file-exists-p tramp-auto-save-directory)
3060 (make-directory tramp-auto-save-directory t)))
3061 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3062 ;; it is not a magic file name operation (since Emacs 22).
3063 ;; We must deactivate it temporarily.
3064 (if (not (ad-is-active 'make-auto-save-file-name))
3065 (tramp-run-real-handler 'make-auto-save-file-name nil)
3067 (ad-deactivate 'make-auto-save-file-name)
3069 (tramp-run-real-handler 'make-auto-save-file-name nil)
3070 (ad-activate 'make-auto-save-file-name)))))
3072 ;; CCC grok LOCKNAME
3073 (defun tramp-sh-handle-write-region
3074 (start end filename &optional append visit lockname confirm)
3075 "Like `write-region' for Tramp files."
3076 (setq filename (expand-file-name filename))
3077 (with-parsed-tramp-file-name filename nil
3078 ;; Following part commented out because we don't know what to do about
3079 ;; file locking, and it does not appear to be a problem to ignore it.
3080 ;; Ange-ftp ignores it, too.
3081 ;; (when (and lockname (stringp lockname))
3082 ;; (setq lockname (expand-file-name lockname)))
3083 ;; (unless (or (eq lockname nil)
3084 ;; (string= lockname filename))
3086 ;; "tramp-sh-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
3088 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
3089 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
3090 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
3091 (tramp-error v 'file-error "File not overwritten")))
3093 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
3094 (tramp-get-remote-uid v 'integer)))
3095 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
3096 (tramp-get-remote-gid v 'integer))))
3098 (if (and (tramp-local-host-p v)
3099 ;; `file-writable-p' calls `file-expand-file-name'. We
3100 ;; cannot use `tramp-run-real-handler' therefore.
3101 (let (file-name-handler-alist)
3103 (file-writable-p (file-name-directory localname))
3104 (or (file-directory-p localname)
3105 (file-writable-p localname)))))
3106 ;; Short track: if we are on the local host, we can run directly.
3107 (tramp-run-real-handler
3109 (list start end localname append 'no-message lockname confirm))
3111 (let* ((modes (save-excursion (tramp-default-file-modes filename)))
3112 ;; We use this to save the value of
3113 ;; `last-coding-system-used' after writing the tmp
3114 ;; file. At the end of the function, we set
3115 ;; `last-coding-system-used' to this saved value. This
3116 ;; way, any intermediary coding systems used while
3117 ;; talking to the remote shell or suchlike won't hose
3118 ;; this variable. This approach was snarfed from
3121 ;; Write region into a tmp file. This isn't really
3122 ;; needed if we use an encoding function, but currently
3123 ;; we use it always because this makes the logic
3124 ;; simpler. We must also set `temporary-file-directory',
3125 ;; because it could point to a remote directory.
3126 (temporary-file-directory
3127 (tramp-compat-temporary-file-directory))
3128 (tmpfile (or tramp-temp-buffer-file-name
3129 (tramp-compat-make-temp-file filename))))
3131 ;; If `append' is non-nil, we copy the file locally, and let
3132 ;; the native `write-region' implementation do the job.
3133 (when append (copy-file filename tmpfile 'ok))
3135 ;; We say `no-message' here because we don't want the
3136 ;; visited file modtime data to be clobbered from the temp
3137 ;; file. We call `set-visited-file-modtime' ourselves later
3138 ;; on. We must ensure that `file-coding-system-alist'
3139 ;; matches `tmpfile'.
3140 (let (file-name-handler-alist
3141 (file-coding-system-alist
3142 (tramp-find-file-name-coding-system-alist filename tmpfile)))
3144 (tramp-run-real-handler
3146 (list start end tmpfile append 'no-message lockname confirm))
3148 (setq tramp-temp-buffer-file-name nil)
3149 (delete-file tmpfile)
3150 (signal (car err) (cdr err))))
3152 ;; Now, `last-coding-system-used' has the right value. Remember it.
3153 (when (boundp 'last-coding-system-used)
3154 (setq coding-system-used
3155 (symbol-value 'last-coding-system-used))))
3157 ;; The permissions of the temporary file should be set. If
3158 ;; filename does not exist (eq modes nil) it has been
3159 ;; renamed to the backup file. This case `save-buffer'
3160 ;; handles permissions.
3161 ;; Ensure that it is still readable.
3165 (logior (or modes 0) (tramp-compat-octal-to-decimal "0400"))))
3167 ;; This is a bit lengthy due to the different methods
3168 ;; possible for file transfer. First, we check whether the
3169 ;; method uses an rcp program. If so, we call it.
3170 ;; Otherwise, both encoding and decoding command must be
3171 ;; specified. However, if the method _also_ specifies an
3172 ;; encoding function, then that is used for encoding the
3173 ;; contents of the tmp file.
3174 (let* ((size (nth 7 (file-attributes tmpfile)))
3175 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
3176 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
3178 ;; `copy-file' handles direct copy and out-of-band methods.
3179 ((or (tramp-local-host-p v)
3180 (tramp-method-out-of-band-p v size))
3181 (if (and (not (stringp start))
3182 (= (or end (point-max)) (point-max))
3183 (= (or start (point-min)) (point-min))
3184 (tramp-get-method-parameter
3185 method 'tramp-copy-keep-tmpfile))
3187 (setq tramp-temp-buffer-file-name tmpfile)
3189 ;; We keep the local file for performance
3190 ;; reasons, useful for "rsync".
3191 (copy-file tmpfile filename t)
3193 (setq tramp-temp-buffer-file-name nil)
3194 (delete-file tmpfile)
3195 (signal (car err) (cdr err)))))
3196 (setq tramp-temp-buffer-file-name nil)
3197 ;; Don't rename, in order to keep context in SELinux.
3199 (copy-file tmpfile filename t)
3200 (delete-file tmpfile))))
3202 ;; Use inline file transfer.
3207 (set-buffer-multibyte nil)
3208 ;; Use encoding function or command.
3209 (if (functionp loc-enc)
3210 (with-tramp-progress-reporter
3211 v 3 (format "Encoding region using function `%s'"
3213 (let ((coding-system-for-read 'binary))
3214 (insert-file-contents-literally tmpfile))
3215 ;; The following `let' is a workaround for the
3216 ;; base64.el that comes with pgnus-0.84. If
3217 ;; both of the following conditions are
3218 ;; satisfied, it tries to write to a local
3219 ;; file in default-directory, but at this
3220 ;; point, default-directory is remote.
3221 ;; (`call-process-region' can't write to
3222 ;; remote files, it seems.) The file in
3223 ;; question is a tmp file anyway.
3224 (let ((default-directory
3225 (tramp-compat-temporary-file-directory)))
3226 (funcall loc-enc (point-min) (point-max))))
3228 (with-tramp-progress-reporter
3229 v 3 (format "Encoding region using command `%s'"
3231 (unless (zerop (tramp-call-local-coding-command
3235 (concat "Cannot write to `%s', "
3236 "local encoding command `%s' failed")
3237 filename loc-enc))))
3239 ;; Send buffer into remote decoding command which
3240 ;; writes to remote file. Because this happens on
3241 ;; the remote host, we cannot use the function.
3242 (with-tramp-progress-reporter
3244 (format "Decoding region into remote file %s" filename)
3245 (goto-char (point-max))
3246 (unless (bolp) (newline))
3250 (concat rem-dec " <<'EOF'\n%sEOF")
3251 (tramp-shell-quote-argument localname)
3253 (tramp-barf-unless-okay
3255 "Couldn't write region to `%s', decode using `%s' failed"
3257 ;; When `file-precious-flag' is set, the region is
3258 ;; written to a temporary file. Check that the
3259 ;; checksum is equal to that from the local tmpfile.
3260 (when file-precious-flag
3263 ;; cksum runs locally, if possible.
3264 (zerop (tramp-compat-call-process "cksum" tmpfile t))
3265 ;; cksum runs remotely.
3266 (tramp-send-command-and-check
3269 "cksum <%s" (tramp-shell-quote-argument localname)))
3270 ;; ... they are different.
3274 (with-current-buffer (tramp-get-buffer v)
3278 (concat "Couldn't write region to `%s',"
3279 " decode using `%s' failed")
3280 filename rem-dec)))))
3283 (delete-file tmpfile)))
3285 ;; That's not expected.
3289 (concat "Method `%s' should specify both encoding and "
3290 "decoding command or an rcp program")
3293 ;; Make `last-coding-system-used' have the right value.
3294 (when coding-system-used
3295 (set 'last-coding-system-used coding-system-used))))
3297 (tramp-flush-file-property v (file-name-directory localname))
3298 (tramp-flush-file-property v localname)
3300 ;; We must protect `last-coding-system-used', now we have set it
3301 ;; to its correct value.
3302 (let (last-coding-system-used (need-chown t))
3303 ;; Set file modification time.
3304 (when (or (eq visit t) (stringp visit))
3305 (let ((file-attr (tramp-compat-file-attributes filename 'integer)))
3306 (set-visited-file-modtime
3307 ;; We must pass modtime explicitly, because filename can
3308 ;; be different from (buffer-file-name), f.e. if
3309 ;; `file-precious-flag' is set.
3311 (when (and (= (nth 2 file-attr) uid)
3312 (= (nth 3 file-attr) gid))
3313 (setq need-chown nil))))
3315 ;; Set the ownership.
3317 (tramp-set-file-uid-gid filename uid gid))
3318 (when (or (eq visit t) (null visit) (stringp visit))
3319 (tramp-message v 0 "Wrote %s" filename))
3320 (run-hooks 'tramp-handle-write-region-hook)))))
3322 (defvar tramp-vc-registered-file-names nil
3323 "List used to collect file names, which are checked during `vc-registered'.")
3325 ;; VC backends check for the existence of various different special
3326 ;; files. This is very time consuming, because every single check
3327 ;; requires a remote command (the file cache must be invalidated).
3328 ;; Therefore, we apply a kind of optimization. We install the file
3329 ;; name handler `tramp-vc-file-name-handler', which does nothing but
3330 ;; remembers all file names for which `file-exists-p' or
3331 ;; `file-readable-p' has been applied. A first run of `vc-registered'
3332 ;; is performed. Afterwards, a script is applied for all collected
3333 ;; file names, using just one remote command. The result of this
3334 ;; script is used to fill the file cache with actual values. Now we
3335 ;; can reset the file name handlers, and we make a second run of
3336 ;; `vc-registered', which returns the expected result without sending
3337 ;; any other remote command.
3338 (defun tramp-sh-handle-vc-registered (file)
3339 "Like `vc-registered' for Tramp files."
3340 (tramp-compat-with-temp-message ""
3341 (with-parsed-tramp-file-name file nil
3342 (with-tramp-progress-reporter
3343 v 3 (format "Checking `vc-registered' for %s" file)
3345 ;; There could be new files, created by the vc backend. We
3346 ;; cannot reuse the old cache entries, therefore.
3347 (let (tramp-vc-registered-file-names
3348 (remote-file-name-inhibit-cache (current-time))
3349 (file-name-handler-alist
3350 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
3352 ;; Here we collect only file names, which need an operation.
3353 (ignore-errors (tramp-run-real-handler 'vc-registered (list file)))
3354 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
3356 ;; Send just one command, in order to fill the cache.
3357 (when tramp-vc-registered-file-names
3358 (tramp-maybe-send-script
3360 (format tramp-vc-registered-read-file-names
3361 (tramp-get-file-exists-command v)
3362 (format "%s -r" (tramp-get-test-command v)))
3363 "tramp_vc_registered_read_file_names")
3367 (tramp-send-command-and-read
3370 "tramp_vc_registered_read_file_names <<'EOF'\n%s\nEOF\n"
3371 (mapconcat 'tramp-shell-quote-argument
3372 tramp-vc-registered-file-names
3375 (tramp-set-file-property
3376 v (car elt) (cadr elt) (cadr (cdr elt))))))
3378 ;; Second run. Now all `file-exists-p' or `file-readable-p'
3379 ;; calls shall be answered from the file cache. We unset
3380 ;; `process-file-side-effects' in order to keep the cache when
3381 ;; `process-file' calls appear.
3382 (let (process-file-side-effects)
3383 (tramp-run-real-handler 'vc-registered (list file)))))))
3385 ;;;###tramp-autoload
3386 (defun tramp-sh-file-name-handler (operation &rest args)
3387 "Invoke remote-shell Tramp file name handler.
3388 Fall back to normal file name handler if no Tramp handler exists."
3389 (when (and tramp-locked (not tramp-locker))
3390 (setq tramp-locked nil)
3391 (signal 'file-error (list "Forbidden reentrant call of Tramp")))
3392 (let ((tl tramp-locked))
3395 (setq tramp-locked t)
3396 (let ((tramp-locker t))
3398 (let ((fn (assoc operation tramp-sh-file-name-handler-alist)))
3400 (apply (cdr fn) args)
3401 (tramp-run-real-handler operation args))))))
3402 (setq tramp-locked tl))))
3404 (defun tramp-vc-file-name-handler (operation &rest args)
3405 "Invoke special file name handler, which collects files to be handled."
3408 (tramp-replace-environment-variables
3409 (apply 'tramp-file-name-for-operation operation args)))
3410 (fn (assoc operation tramp-sh-file-name-handler-alist)))
3411 (with-parsed-tramp-file-name filename nil
3413 ;; That's what we want: file names, for which checks are
3414 ;; applied. We assume that VC uses only `file-exists-p' and
3415 ;; `file-readable-p' checks; otherwise we must extend the
3416 ;; list. We do not perform any action, but return nil, in
3417 ;; order to keep `vc-registered' running.
3418 ((and fn (memq operation '(file-exists-p file-readable-p)))
3419 (add-to-list 'tramp-vc-registered-file-names localname 'append)
3421 ;; `process-file' and `start-file-process' shall be ignored.
3422 ((and fn (eq operation 'process-file) 0))
3423 ((and fn (eq operation 'start-file-process) nil))
3424 ;; Tramp file name handlers like `expand-file-name'. They
3426 (fn (save-match-data (apply (cdr fn) args)))
3427 ;; Default file name handlers, we don't care.
3428 (t (tramp-run-real-handler operation args)))))))
3430 ;;; Internal Functions:
3432 (defun tramp-maybe-send-script (vec script name)
3433 "Define in remote shell function NAME implemented as SCRIPT.
3434 Only send the definition if it has not already been done."
3435 ;; We cannot let-bind (tramp-get-connection-process vec) because it
3437 (let ((scripts (tramp-get-connection-property
3438 (tramp-get-connection-process vec) "scripts" nil)))
3439 (unless (member name scripts)
3440 (with-tramp-progress-reporter vec 5 (format "Sending script `%s'" name)
3441 ;; The script could contain a call of Perl. This is masked with `%s'.
3442 (tramp-barf-unless-okay
3444 (format "%s () {\n%s\n}" name
3445 (format script (tramp-get-remote-perl vec)))
3446 "Script %s sending failed" name)
3447 (tramp-set-connection-property
3448 (tramp-get-connection-process vec) "scripts" (cons name scripts))))))
3450 (defun tramp-set-auto-save ()
3451 (when (and ;; ange-ftp has its own auto-save mechanism
3452 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
3453 'tramp-sh-file-name-handler)
3455 (auto-save-mode 1)))
3456 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
3457 (add-hook 'tramp-unload-hook
3459 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
3461 (defun tramp-run-test (switch filename)
3462 "Run `test' on the remote system, given a SWITCH and a FILENAME.
3463 Returns the exit code of the `test' program."
3464 (with-parsed-tramp-file-name filename nil
3465 (tramp-send-command-and-check
3469 (tramp-get-test-command v)
3471 (tramp-shell-quote-argument localname)))))
3473 (defun tramp-run-test2 (format-string file1 file2)
3474 "Run `test'-like program on the remote system, given FILE1, FILE2.
3475 FORMAT-STRING contains the program name, switches, and place holders.
3476 Returns the exit code of the `test' program. Barfs if the methods,
3477 hosts, or files, disagree."
3478 (unless (tramp-equal-remote file1 file2)
3479 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
3482 "tramp-run-test2 only implemented for same method, user, host")))
3483 (with-parsed-tramp-file-name file1 v1
3484 (with-parsed-tramp-file-name file1 v2
3485 (tramp-send-command-and-check
3487 (format format-string
3488 (tramp-shell-quote-argument v1-localname)
3489 (tramp-shell-quote-argument v2-localname))))))
3491 (defun tramp-find-executable
3492 (vec progname dirlist &optional ignore-tilde ignore-path)
3493 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
3494 First arg VEC specifies the connection, PROGNAME is the program
3495 to search for, and DIRLIST gives the list of directories to
3496 search. If IGNORE-TILDE is non-nil, directory names starting
3497 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
3500 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
3502 This function expects to be in the right *tramp* buffer."
3503 (with-current-buffer (tramp-get-connection-buffer vec)
3505 ;; Check whether the executable is in $PATH. "which(1)" does not
3506 ;; report always a correct error code; therefore we check the
3507 ;; number of words it returns.
3509 (tramp-send-command vec (format "which \\%s | wc -w" progname))
3510 (goto-char (point-min))
3511 (if (looking-at "^\\s-*1$")
3512 (setq result (concat "\\" progname))))
3515 ;; Remove all ~/foo directories from dirlist. In XEmacs,
3516 ;; `remove' is in CL, and we want to avoid CL dependencies.
3519 (setq d (car dirlist))
3520 (setq dirlist (cdr dirlist))
3521 (unless (char-equal ?~ (aref d 0))
3522 (setq newdl (cons d newdl))))
3523 (setq dirlist (nreverse newdl))))
3526 (format (concat "while read d; "
3527 "do if test -x $d/%s -a -f $d/%s; "
3528 "then echo tramp_executable $d/%s; "
3529 "break; fi; done <<'EOF'\n"
3531 progname progname progname (mapconcat 'identity dirlist "\n")))
3532 (goto-char (point-max))
3533 (when (search-backward "tramp_executable " nil t)
3534 (skip-chars-forward "^ ")
3535 (skip-chars-forward " ")
3536 (setq result (buffer-substring (point) (point-at-eol)))))
3539 (defun tramp-set-remote-path (vec)
3540 "Sets the remote environment PATH to existing directories.
3541 I.e., for each directory in `tramp-remote-path', it is tested
3542 whether it exists and if so, it is added to the environment
3544 (tramp-message vec 5 (format "Setting $PATH environment variable"))
3546 vec (format "PATH=%s; export PATH"
3547 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
3549 ;; ------------------------------------------------------------
3550 ;; -- Communication with external shell --
3551 ;; ------------------------------------------------------------
3553 (defun tramp-find-file-exists-command (vec)
3554 "Find a command on the remote host for checking if a file exists.
3555 Here, we are looking for a command which has zero exit status if the
3556 file exists and nonzero exit status otherwise."
3557 (let ((existing "/")
3559 (tramp-shell-quote-argument "/ this file does not exist "))
3561 ;; The algorithm is as follows: we try a list of several commands.
3562 ;; For each command, we first run `$cmd /' -- this should return
3563 ;; true, as the root directory always exists. And then we run
3564 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
3565 ;; does not exist. This should return false. We use the first
3566 ;; command we find that seems to work.
3567 ;; The list of commands to try is as follows:
3568 ;; `ls -d' This works on most systems, but NetBSD 1.4
3569 ;; has a bug: `ls' always returns zero exit
3570 ;; status, even for files which don't exist.
3571 ;; `test -e' Some Bourne shells have a `test' builtin
3572 ;; which does not know the `-e' option.
3573 ;; `/bin/test -e' For those, the `test' binary on disk normally
3574 ;; provides the option. Alas, the binary
3575 ;; is sometimes `/bin/test' and sometimes it's
3577 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
3580 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
3581 (tramp-send-command-and-check
3582 vec (format "%s %s" result existing))
3583 (not (tramp-send-command-and-check
3584 vec (format "%s %s" result nonexistent)))))
3586 (and (setq result "/bin/test -e")
3587 (tramp-send-command-and-check
3588 vec (format "%s %s" result existing))
3589 (not (tramp-send-command-and-check
3590 vec (format "%s %s" result nonexistent)))))
3592 (and (setq result "/usr/bin/test -e")
3593 (tramp-send-command-and-check
3594 vec (format "%s %s" result existing))
3595 (not (tramp-send-command-and-check
3596 vec (format "%s %s" result nonexistent)))))
3598 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
3599 (tramp-send-command-and-check
3600 vec (format "%s %s" result existing))
3601 (not (tramp-send-command-and-check
3602 vec (format "%s %s" result nonexistent))))))
3604 vec 'file-error "Couldn't find command to check if file exists"))
3607 (defun tramp-open-shell (vec shell)
3608 "Opens shell SHELL."
3609 (with-tramp-progress-reporter
3610 vec 5 (format "Opening remote shell `%s'" shell)
3611 ;; Find arguments for this shell.
3612 (let ((tramp-end-of-output tramp-initial-end-of-output)
3613 (alist tramp-sh-extra-args)
3615 (while (and alist (null extra-args))
3616 (setq item (pop alist))
3617 (when (string-match (car item) shell)
3618 (setq extra-args (cdr item))))
3621 "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s %s"
3622 (tramp-shell-quote-argument tramp-end-of-output)
3623 shell (or extra-args ""))
3625 (tramp-set-connection-property
3626 (tramp-get-connection-process vec) "remote-shell" shell)
3629 vec (format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output)) t)
3630 (tramp-send-command vec "PS2=''" t)
3631 (tramp-send-command vec "PS3=''" t)
3632 (tramp-send-command vec "PROMPT_COMMAND=''" t)))
3634 (defun tramp-find-shell (vec)
3635 "Opens a shell on the remote host which groks tilde expansion."
3636 (with-current-buffer (tramp-get-buffer vec)
3637 (let ((default-shell
3639 (tramp-get-connection-property
3640 (tramp-get-connection-process vec) "remote-shell" nil)
3641 (tramp-get-method-parameter
3642 (tramp-file-name-method vec) 'tramp-remote-shell)))
3645 (with-tramp-connection-property vec "remote-shell"
3646 ;; CCC: "root" does not exist always, see QNAP 459.
3647 ;; Which check could we apply instead?
3648 (tramp-send-command vec "echo ~root" t)
3649 (if (or (string-match "^~root$" (buffer-string))
3650 ;; The default shell (ksh93) of OpenSolaris and
3651 ;; Solaris is buggy. We've got reports for
3652 ;; "SunOS 5.10" and "SunOS 5.11" so far.
3653 (string-match (regexp-opt '("SunOS 5.10" "SunOS 5.11"))
3654 (tramp-get-connection-property
3657 (or (tramp-find-executable
3658 vec "bash" (tramp-get-remote-path vec) t t)
3659 (tramp-find-executable
3660 vec "ksh" (tramp-get-remote-path vec) t t)
3661 ;; Maybe it works at least for some other commands.
3667 "Couldn't find a remote shell which groks tilde "
3668 "expansion, using `%s'")
3673 ;; Open a new shell if needed.
3674 (unless (string-equal shell default-shell)
3676 vec 5 "Starting remote shell `%s' for tilde expansion" shell)
3677 (tramp-open-shell vec shell))
3679 ;; Busyboxes tend to behave strange. We check for the existence.
3680 (with-tramp-connection-property vec "busybox"
3681 (tramp-send-command vec (format "%s --version" shell) t)
3682 (let ((case-fold-search t))
3683 (and (string-match "busybox" (buffer-string)) t))))))
3685 ;; Utility functions.
3687 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
3688 "Wait for shell prompt and barf if none appears.
3689 Looks at process PROC to see if a shell prompt appears in TIMEOUT
3690 seconds. If not, it produces an error message with the given ERROR-ARGS."
3692 (tramp-wait-for-regexp
3695 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
3696 (apply 'tramp-error-with-buffer nil proc 'file-error error-args)))
3698 (defun tramp-open-connection-setup-interactive-shell (proc vec)
3699 "Set up an interactive shell.
3700 Mainly sets the prompt and the echo correctly. PROC is the shell
3701 process to set up. VEC specifies the connection."
3702 (let ((tramp-end-of-output tramp-initial-end-of-output))
3703 ;; It is useful to set the prompt in the following command because
3704 ;; some people have a setting for $PS1 which /bin/sh doesn't know
3705 ;; about and thus /bin/sh will display a strange prompt. For
3706 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
3707 ;; display the current working directory but /bin/sh will display
3708 ;; a dollar sign. The following command line sets $PS1 to a sane
3709 ;; value, and works under Bourne-ish shells as well as csh-like
3710 ;; shells. Daniel Pittman reports that the unusual positioning of
3711 ;; the single quotes makes it work under `rc', too. We also unset
3712 ;; the variable $ENV because that is read by some sh
3713 ;; implementations (eg, bash when called as sh) on startup; this
3714 ;; way, we avoid the startup file clobbering $PS1. $PROMPT_COMMAND
3715 ;; is another way to set the prompt in /bin/bash, it must be
3716 ;; discarded as well.
3719 (or (tramp-get-connection-property vec "remote-shell" nil)
3720 (tramp-get-method-parameter
3721 (tramp-file-name-method vec) 'tramp-remote-shell)))
3724 (tramp-message vec 5 "Setting up remote shell environment")
3725 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
3726 ;; Check whether the echo has really been disabled. Some
3727 ;; implementations, like busybox of embedded GNU/Linux, don't
3728 ;; support disabling.
3729 (tramp-send-command vec "echo foo" t)
3730 (with-current-buffer (process-buffer proc)
3731 (goto-char (point-min))
3732 (when (looking-at "echo foo")
3733 (tramp-set-connection-property proc "remote-echo" t)
3734 (tramp-message vec 5 "Remote echo still on. Ok.")
3735 ;; Make sure backspaces and their echo are enabled and no line
3736 ;; width magic interferes with them.
3737 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
3739 (tramp-message vec 5 "Setting shell prompt")
3741 vec (format "PS1=%s" (tramp-shell-quote-argument tramp-end-of-output)) t)
3742 (tramp-send-command vec "PS2=''" t)
3743 (tramp-send-command vec "PS3=''" t)
3744 (tramp-send-command vec "PROMPT_COMMAND=''" t)
3746 ;; Try to set up the coding system correctly.
3747 ;; CCC this can't be the right way to do it. Hm.
3748 (tramp-message vec 5 "Determining coding system")
3749 (tramp-send-command vec "echo foo ; echo bar" t)
3750 (with-current-buffer (process-buffer proc)
3751 (goto-char (point-min))
3752 (if (featurep 'mule)
3753 ;; Use MULE to select the right EOL convention for communicating
3754 ;; with the process.
3755 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc)
3756 (cons 'undecided 'undecided)))
3757 cs-decode cs-encode)
3758 (when (symbolp cs) (setq cs (cons cs cs)))
3759 (setq cs-decode (car cs))
3760 (setq cs-encode (cdr cs))
3761 (unless cs-decode (setq cs-decode 'undecided))
3762 (unless cs-encode (setq cs-encode 'undecided))
3763 (setq cs-encode (tramp-compat-coding-system-change-eol-conversion
3765 (when (search-forward "\r" nil t)
3766 (setq cs-decode (tramp-compat-coding-system-change-eol-conversion
3768 (tramp-compat-funcall
3769 'set-buffer-process-coding-system cs-decode cs-encode)
3771 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
3772 ;; Look for ^M and do something useful if found.
3773 (when (search-forward "\r" nil t)
3774 ;; We have found a ^M but cannot frob the process coding system
3775 ;; because we're running on a non-MULE Emacs. Let's try
3777 (tramp-send-command vec "stty -onlcr" t))))
3779 (tramp-send-command vec "set +o vi +o emacs" t)
3781 ;; Check whether the output of "uname -sr" has been changed. If
3782 ;; yes, this is a strong indication that we must expire all
3783 ;; connection properties. We start again with
3784 ;; `tramp-maybe-open-connection', it will be caught there.
3785 (tramp-message vec 5 "Checking system information")
3786 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
3788 (tramp-set-connection-property
3790 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
3791 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
3795 "Connection reset, because remote host changed from `%s' to `%s'"
3796 old-uname new-uname)
3797 (throw 'uname-changed (tramp-maybe-open-connection vec))))
3799 ;; Check whether the remote host suffers from buggy
3800 ;; `send-process-string'. This is known for FreeBSD (see comment in
3801 ;; `send_process', file process.c). I've tested sending 624 bytes
3802 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
3803 ;; this host type is detected locally. It cannot handle remote
3805 (with-tramp-connection-property proc "chunksize"
3807 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
3811 vec 5 "Checking remote host type for `send-process-string' bug")
3813 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
3816 ;; Set remote PATH variable.
3817 (tramp-set-remote-path vec)
3819 ;; Search for a good shell before searching for a command which
3820 ;; checks if a file exists. This is done because Tramp wants to use
3821 ;; "test foo; echo $?" to check if various conditions hold, and
3822 ;; there are buggy /bin/sh implementations which don't execute the
3823 ;; "echo $?" part if the "test" part has an error. In particular,
3824 ;; the OpenSolaris /bin/sh is a problem. There are also other
3825 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
3826 ;; in function declarations, or changing HISTFILE in place.
3827 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
3829 (tramp-find-shell vec)
3831 ;; Disable unexpected output.
3832 (tramp-send-command vec "mesg n; biff n" t)
3834 ;; IRIX64 bash expands "!" even when in single quotes. This
3835 ;; destroys our shell functions, we must disable it. See
3836 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
3837 (when (string-match "^IRIX64" (tramp-get-connection-property vec "uname" ""))
3838 (tramp-send-command vec "set +H" t))
3840 ;; On BSD-like systems, ?\t is expanded to spaces. Suppress this.
3841 (when (string-match "BSD\\|Darwin"
3842 (tramp-get-connection-property vec "uname" ""))
3843 (tramp-send-command vec "stty -oxtabs" t))
3845 ;; Set `remote-tty' process property.
3846 (let ((tty (tramp-send-command-and-read vec "echo \\\"`tty`\\\"" 'noerror)))
3847 (unless (zerop (length tty))
3848 (tramp-compat-process-put proc 'remote-tty tty)))
3850 ;; Dump stty settings in the traces.
3851 (when (>= tramp-verbose 9)
3852 (tramp-send-command vec "stty -a" t))
3854 ;; Set the environment.
3855 (tramp-message vec 5 "Setting default environment")
3857 (let ((env (copy-sequence tramp-remote-process-environment))
3860 (setq item (tramp-compat-split-string (car env) "="))
3861 (setcdr item (mapconcat 'identity (cdr item) "="))
3862 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
3864 vec (format "%s=%s; export %s" (car item) (cdr item) (car item)) t)
3865 (push (car item) unset))
3866 (setq env (cdr env)))
3869 vec (format "unset %s" (mapconcat 'identity unset " ")) t))))
3871 ;; CCC: We should either implement a Perl version of base64 encoding
3872 ;; and decoding. Then we just use that in the last item. The other
3873 ;; alternative is to use the Perl version of UU encoding. But then
3874 ;; we need a Lisp version of uuencode.
3876 ;; Old text from documentation of tramp-methods:
3877 ;; Using a uuencode/uudecode inline method is discouraged, please use one
3878 ;; of the base64 methods instead since base64 encoding is much more
3879 ;; reliable and the commands are more standardized between the different
3880 ;; Unix versions. But if you can't use base64 for some reason, please
3881 ;; note that the default uudecode command does not work well for some
3882 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
3883 ;; the following command for uudecode:
3885 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
3887 ;; For Irix, no solution is known yet.
3889 (autoload 'uudecode-decode-region "uudecode")
3891 (defconst tramp-local-coding-commands
3892 '((b64 base64-encode-region base64-decode-region)
3893 (uu tramp-uuencode-region uudecode-decode-region)
3895 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
3896 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
3897 "List of local coding commands for inline transfer.
3898 Each item is a list that looks like this:
3900 \(FORMAT ENCODING DECODING\)
3902 FORMAT is symbol describing the encoding/decoding format. It can be
3903 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3905 ENCODING and DECODING can be strings, giving commands, or symbols,
3906 giving functions. If they are strings, then they can contain
3907 the \"%s\" format specifier. If that specifier is present, the input
3908 filename will be put into the command line at that spot. If the
3909 specifier is not present, the input should be read from standard
3912 If they are functions, they will be called with two arguments, start
3913 and end of region, and are expected to replace the region contents
3914 with the encoded or decoded results, respectively.")
3916 (defconst tramp-remote-coding-commands
3917 '((b64 "base64" "base64 -d -i")
3918 ;; "-i" is more robust with older base64 from GNU coreutils.
3919 ;; However, I don't know whether all base64 versions do supports
3921 (b64 "base64" "base64 -d")
3922 (b64 "mimencode -b" "mimencode -u -b")
3923 (b64 "mmencode -b" "mmencode -u -b")
3924 (b64 "recode data..base64" "recode base64..data")
3925 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
3926 (b64 tramp-perl-encode tramp-perl-decode)
3927 (uu "uuencode xxx" "uudecode -o /dev/stdout" "test -c /dev/stdout")
3928 (uu "uuencode xxx" "uudecode -o -")
3929 (uu "uuencode xxx" "uudecode -p")
3930 (uu "uuencode xxx" tramp-uudecode)
3932 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
3933 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
3934 "List of remote coding commands for inline transfer.
3935 Each item is a list that looks like this:
3937 \(FORMAT ENCODING DECODING [TEST]\)
3939 FORMAT is symbol describing the encoding/decoding format. It can be
3940 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
3942 ENCODING and DECODING can be strings, giving commands, or symbols,
3943 giving variables. If they are strings, then they can contain
3944 the \"%s\" format specifier. If that specifier is present, the input
3945 filename will be put into the command line at that spot. If the
3946 specifier is not present, the input should be read from standard
3949 If they are variables, this variable is a string containing a Perl
3950 implementation for this functionality. This Perl program will be transferred
3951 to the remote host, and it is available as shell function with the same name.
3953 The optional TEST command can be used for further tests, whether
3954 ENCODING and DECODING are applicable.")
3956 (defun tramp-find-inline-encoding (vec)
3957 "Find an inline transfer encoding that works.
3958 Goes through the list `tramp-local-coding-commands' and
3959 `tramp-remote-coding-commands'."
3961 (let ((local-commands tramp-local-coding-commands)
3963 (p (tramp-get-connection-process vec))
3964 loc-enc loc-dec rem-enc rem-dec rem-test litem ritem found)
3965 (while (and local-commands (not found))
3966 (setq litem (pop local-commands))
3967 (catch 'wont-work-local
3968 (let ((format (nth 0 litem))
3969 (remote-commands tramp-remote-coding-commands))
3970 (setq loc-enc (nth 1 litem))
3971 (setq loc-dec (nth 2 litem))
3972 ;; If the local encoder or decoder is a string, the
3973 ;; corresponding command has to work locally.
3974 (if (not (stringp loc-enc))
3976 vec 5 "Checking local encoding function `%s'" loc-enc)
3978 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
3979 (unless (zerop (tramp-call-local-coding-command
3981 (throw 'wont-work-local nil)))
3982 (if (not (stringp loc-dec))
3984 vec 5 "Checking local decoding function `%s'" loc-dec)
3986 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
3987 (unless (zerop (tramp-call-local-coding-command
3989 (throw 'wont-work-local nil)))
3990 ;; Search for remote coding commands with the same format
3991 (while (and remote-commands (not found))
3992 (setq ritem (pop remote-commands))
3993 (catch 'wont-work-remote
3994 (when (equal format (nth 0 ritem))
3995 (setq rem-enc (nth 1 ritem))
3996 (setq rem-dec (nth 2 ritem))
3997 (setq rem-test (nth 3 ritem))
3998 ;; Check the remote test command if exists.
3999 (when (stringp rem-test)
4001 vec 5 "Checking remote test command `%s'" rem-test)
4002 (unless (tramp-send-command-and-check vec rem-test t)
4003 (throw 'wont-work-remote nil)))
4004 ;; Check if remote encoding and decoding commands can be
4005 ;; called remotely with null input and output. This makes
4006 ;; sure there are no syntax errors and the command is really
4007 ;; found. Note that we do not redirect stdout to /dev/null,
4008 ;; for two reasons: when checking the decoding command, we
4009 ;; actually check the output it gives. And also, when
4010 ;; redirecting "mimencode" output to /dev/null, then as root
4011 ;; it might change the permissions of /dev/null!
4012 (when (not (stringp rem-enc))
4013 (let ((name (symbol-name rem-enc)))
4014 (while (string-match (regexp-quote "-") name)
4015 (setq name (replace-match "_" nil t name)))
4016 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
4017 (setq rem-enc name)))
4020 "Checking remote encoding command `%s' for sanity" rem-enc)
4021 (unless (tramp-send-command-and-check
4022 vec (format "%s </dev/null" rem-enc) t)
4023 (throw 'wont-work-remote nil))
4025 (when (not (stringp rem-dec))
4026 (let ((name (symbol-name rem-dec)))
4027 (while (string-match (regexp-quote "-") name)
4028 (setq name (replace-match "_" nil t name)))
4029 (tramp-maybe-send-script vec (symbol-value rem-dec) name)
4030 (setq rem-dec name)))
4033 "Checking remote decoding command `%s' for sanity" rem-dec)
4034 (unless (tramp-send-command-and-check
4036 (format "echo %s | %s | %s" magic rem-enc rem-dec)
4038 (throw 'wont-work-remote nil))
4040 (with-current-buffer (tramp-get-buffer vec)
4041 (goto-char (point-min))
4042 (unless (looking-at (regexp-quote magic))
4043 (throw 'wont-work-remote nil)))
4045 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
4046 (setq rem-enc (nth 1 ritem))
4047 (setq rem-dec (nth 2 ritem))
4048 (setq found t)))))))
4050 ;; Did we find something?
4053 vec 'file-error "Couldn't find an inline transfer encoding"))
4055 ;; Set connection properties. Since the commands are risky (due
4056 ;; to output direction), we cache them in the process cache.
4057 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
4058 (tramp-set-connection-property p "local-encoding" loc-enc)
4059 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
4060 (tramp-set-connection-property p "local-decoding" loc-dec)
4061 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
4062 (tramp-set-connection-property p "remote-encoding" rem-enc)
4063 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
4064 (tramp-set-connection-property p "remote-decoding" rem-dec))))
4066 (defun tramp-call-local-coding-command (cmd input output)
4067 "Call the local encoding or decoding command.
4068 If CMD contains \"%s\", provide input file INPUT there in command.
4069 Otherwise, INPUT is passed via standard input.
4070 INPUT can also be nil which means `/dev/null'.
4071 OUTPUT can be a string (which specifies a filename), or t (which
4072 means standard output and thus the current buffer), or nil (which
4074 (tramp-compat-call-process
4075 tramp-encoding-shell
4076 (when (and input (not (string-match "%s" cmd))) input)
4077 (if (eq output t) t nil)
4079 tramp-encoding-command-switch
4081 (if (string-match "%s" cmd) (format cmd input) cmd)
4082 (if (stringp output) (concat "> " output) ""))))
4084 (defconst tramp-inline-compress-commands
4085 '(("gzip" "gzip -d")
4086 ("bzip2" "bzip2 -d")
4088 ("compress" "compress -d"))
4089 "List of compress and decompress commands for inline transfer.
4090 Each item is a list that looks like this:
4092 \(COMPRESS DECOMPRESS\)
4094 COMPRESS or DECOMPRESS are strings with the respective commands.")
4096 (defun tramp-find-inline-compress (vec)
4097 "Find an inline transfer compress command that works.
4098 Goes through the list `tramp-inline-compress-commands'."
4100 (let ((commands tramp-inline-compress-commands)
4102 (p (tramp-get-connection-process vec))
4103 item compress decompress found)
4104 (while (and commands (not found))
4106 (setq item (pop commands)
4107 compress (nth 0 item)
4108 decompress (nth 1 item))
4111 "Checking local compress command `%s', `%s' for sanity"
4112 compress decompress)
4115 (tramp-call-local-coding-command
4117 ;; Windows shells need the program file name after
4118 ;; the pipe symbol be quoted if they use forward
4119 ;; slashes as directory separators.
4120 (if (memq system-type '(windows-nt))
4121 "echo %s | \"%s\" | \"%s\""
4122 "echo %s | %s | %s")
4123 magic compress decompress) nil nil))
4127 "Checking remote compress command `%s', `%s' for sanity"
4128 compress decompress)
4129 (unless (tramp-send-command-and-check
4130 vec (format "echo %s | %s | %s" magic compress decompress) t)
4134 ;; Did we find something?
4137 ;; Set connection properties. Since the commands are
4138 ;; risky (due to output direction), we cache them in the
4141 vec 5 "Using inline transfer compress command `%s'" compress)
4142 (tramp-set-connection-property p "inline-compress" compress)
4144 vec 5 "Using inline transfer decompress command `%s'" decompress)
4145 (tramp-set-connection-property p "inline-decompress" decompress))
4147 (tramp-set-connection-property p "inline-compress" nil)
4148 (tramp-set-connection-property p "inline-decompress" nil)
4150 vec 2 "Couldn't find an inline transfer compress command")))))
4152 (defun tramp-compute-multi-hops (vec)
4153 "Expands VEC according to `tramp-default-proxies-alist'.
4154 Gateway hops are already opened."
4155 (let ((target-alist `(,vec))
4156 (hops (or (tramp-file-name-hop vec) ""))
4160 ;; Ad-hoc proxy definitions.
4161 (dolist (proxy (reverse (split-string hops tramp-postfix-hop-regexp 'omit)))
4162 (let ((user (tramp-file-name-user item))
4163 (host (tramp-file-name-host item))
4165 tramp-prefix-format proxy tramp-postfix-host-format)))
4167 vec 5 "Add proxy (\"%s\" \"%s\" \"%s\")"
4168 (and (stringp host) (regexp-quote host))
4169 (and (stringp user) (regexp-quote user))
4173 'tramp-default-proxies-alist
4174 (list (and (stringp host) (regexp-quote host))
4175 (and (stringp user) (regexp-quote user))
4177 (setq item (tramp-dissect-file-name proxy))))
4178 ;; Save the new value.
4179 (when (and hops tramp-save-ad-hoc-proxies)
4180 (customize-save-variable
4181 'tramp-default-proxies-alist tramp-default-proxies-alist))
4183 ;; Look for proxy hosts to be passed.
4184 (setq choices tramp-default-proxies-alist)
4186 (setq item (pop choices)
4187 proxy (eval (nth 2 item)))
4190 (string-match (or (eval (nth 0 item)) "")
4191 (or (tramp-file-name-host (car target-alist)) ""))
4193 (string-match (or (eval (nth 1 item)) "")
4194 (or (tramp-file-name-user (car target-alist)) "")))
4196 ;; No more hops needed.
4198 ;; Replace placeholders.
4203 ?u (or (tramp-file-name-user (car target-alist)) "")
4204 ?h (or (tramp-file-name-host (car target-alist)) ""))))
4205 (with-parsed-tramp-file-name proxy l
4207 (add-to-list 'target-alist l)
4208 ;; Start next search.
4209 (setq choices tramp-default-proxies-alist)))))
4214 "^\\(%s\\|%s\\)$" tramp-gw-tunnel-method tramp-gw-socks-method)
4215 (tramp-file-name-method (car target-alist)))
4216 (let ((gw (pop target-alist))
4217 (hop (pop target-alist)))
4218 ;; Is the method prepared for gateways?
4219 (unless (tramp-file-name-port hop)
4222 "Connection `%s' is not supported for gateway access." hop))
4223 ;; Open the gateway connection.
4227 (tramp-file-name-method hop) (tramp-file-name-user hop)
4228 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop) nil nil))
4229 ;; For the password prompt, we need the correct values.
4230 ;; Therefore, we must remember the gateway vector. But we
4231 ;; cannot do it as connection property, because it shouldn't
4232 ;; be persistent. And we have no started process yet either.
4233 (tramp-set-file-property (car target-alist) "" "gateway" hop)))
4235 ;; Foreign and out-of-band methods are not supported for multi-hops.
4236 (when (cdr target-alist)
4237 (setq choices target-alist)
4239 (setq item (pop choices))
4243 (tramp-get-method-parameter
4244 (tramp-file-name-method item) 'tramp-login-program))
4245 (tramp-get-method-parameter
4246 (tramp-file-name-method item) 'tramp-copy-program))
4249 "Method `%s' is not supported for multi-hops."
4250 (tramp-file-name-method item)))))
4252 ;; In case the host name is not used for the remote shell
4253 ;; command, the user could be misguided by applying a random
4255 (let* ((v (car target-alist))
4256 (method (tramp-file-name-method v))
4257 (host (tramp-file-name-host v)))
4260 ;; There are multi-hops.
4262 ;; The host name is used for the remote shell command.
4264 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
4265 ;; The host is local. We cannot use `tramp-local-host-p'
4266 ;; here, because it opens a connection as well.
4267 (string-match tramp-local-host-regexp host))
4270 "Host `%s' looks like a remote host, `%s' can only use the local host"
4276 (defvar tramp-current-connection nil
4277 "Last connection timestamp.")
4279 (defun tramp-maybe-open-connection (vec)
4280 "Maybe open a connection VEC.
4281 Does not do anything if a connection is already open, but re-opens the
4282 connection if a previous connection has died for some reason."
4283 (catch 'uname-changed
4284 (let ((p (tramp-get-connection-process vec))
4285 (process-name (tramp-get-connection-property vec "process-name" nil))
4286 (process-environment (copy-sequence process-environment))
4287 (pos (with-current-buffer (tramp-get-connection-buffer vec) (point))))
4289 ;; If Tramp opens the same connection within a short time frame,
4290 ;; there is a problem. We shall signal this.
4291 (unless (or (and p (processp p) (memq (process-status p) '(run open)))
4292 (not (equal (butlast (append vec nil))
4293 (car tramp-current-connection)))
4295 (current-time) (cdr tramp-current-connection))
4297 (throw 'suppress 'suppress))
4299 ;; If too much time has passed since last command was sent, look
4300 ;; whether process is still alive. If it isn't, kill it. When
4301 ;; using ssh, it can sometimes happen that the remote end has
4302 ;; hung up but the local ssh client doesn't recognize this until
4303 ;; it tries to send some data to the remote end. So that's why
4304 ;; we try to send a command from time to time, then look again
4305 ;; whether the process is really alive.
4307 (when (and (> (tramp-time-diff
4309 (tramp-get-connection-property
4310 p "last-cmd-time" '(0 0 0)))
4312 p (processp p) (memq (process-status p) '(run open)))
4313 (tramp-send-command vec "echo are you awake" t t)
4314 (unless (and (memq (process-status p) '(run open))
4315 (tramp-wait-for-output p 10))
4316 ;; The error will be caught locally.
4317 (tramp-error vec 'file-error "Awake did fail")))
4322 ;; New connection must be opened.
4324 (unless (and p (processp p) (memq (process-status p) '(run open)))
4326 ;; We call `tramp-get-buffer' in order to get a debug
4327 ;; buffer for messages from the beginning.
4328 (tramp-get-buffer vec)
4330 ;; If `non-essential' is non-nil, don't reopen a new connection.
4331 (when (and (boundp 'non-essential) (symbol-value 'non-essential))
4332 (throw 'non-essential 'non-essential))
4334 (with-tramp-progress-reporter
4336 (if (zerop (length (tramp-file-name-user vec)))
4337 (format "Opening connection for %s using %s"
4338 (tramp-file-name-host vec)
4339 (tramp-file-name-method vec))
4340 (format "Opening connection for %s@%s using %s"
4341 (tramp-file-name-user vec)
4342 (tramp-file-name-host vec)
4343 (tramp-file-name-method vec)))
4345 ;; Start new process.
4346 (when (and p (processp p))
4348 (setenv "TERM" tramp-terminal-type)
4349 (setenv "LC_ALL" "C")
4350 (setenv "PROMPT_COMMAND")
4351 (setenv "PS1" tramp-initial-end-of-output)
4352 (let* ((target-alist (tramp-compute-multi-hops vec))
4353 (process-connection-type tramp-process-connection-type)
4354 (process-adaptive-read-buffering nil)
4355 (coding-system-for-read nil)
4356 ;; This must be done in order to avoid our file
4358 (p (let ((default-directory
4359 (tramp-compat-temporary-file-directory)))
4362 (tramp-get-connection-name vec)
4363 (tramp-get-connection-buffer vec)
4364 (if tramp-encoding-command-interactive
4365 (list tramp-encoding-shell
4366 tramp-encoding-command-interactive)
4367 (list tramp-encoding-shell))))))
4369 ;; Set sentinel and query flag.
4370 (tramp-set-connection-property p "vector" vec)
4371 (set-process-sentinel p 'tramp-process-sentinel)
4372 (tramp-compat-set-process-query-on-exit-flag p nil)
4373 (setq tramp-current-connection
4374 (cons (butlast (append vec nil)) (current-time))
4375 tramp-current-host (system-name))
4378 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
4380 ;; Check whether process is alive.
4381 (tramp-barf-if-no-shell-prompt
4383 "Couldn't find local shell prompt %s" tramp-encoding-shell)
4385 ;; Now do all the connections as specified.
4387 (let* ((hop (car target-alist))
4388 (l-method (tramp-file-name-method hop))
4389 (l-user (tramp-file-name-user hop))
4390 (l-host (tramp-file-name-host hop))
4393 (tramp-get-method-parameter
4394 l-method 'tramp-login-program))
4396 (tramp-get-method-parameter
4397 l-method 'tramp-login-args))
4399 (tramp-get-method-parameter
4400 l-method 'tramp-async-args))
4402 (tramp-get-method-parameter l-method 'tramp-gw-args))
4403 (gw (tramp-get-file-property hop "" "gateway" nil))
4404 (g-method (and gw (tramp-file-name-method gw)))
4405 (g-user (and gw (tramp-file-name-user gw)))
4406 (g-host (and gw (tramp-file-name-real-host gw)))
4407 (command login-program)
4408 ;; We don't create the temporary file. In
4409 ;; fact, it is just a prefix for the
4410 ;; ControlPath option of ssh; the real
4411 ;; temporary file has another name, and it is
4412 ;; created and protected by ssh. It is also
4413 ;; removed by ssh when the connection is
4416 (tramp-set-connection-property
4420 tramp-temp-name-prefix
4421 (tramp-compat-temporary-file-directory)))))
4424 ;; Add arguments for asynchronous processes.
4425 (when (and process-name async-args)
4426 (setq login-args (append async-args login-args)))
4428 ;; Add gateway arguments if necessary.
4429 (when (and gw gw-args)
4430 (setq login-args (append gw-args login-args)))
4432 ;; Check for port number. Until now, there's no
4433 ;; need for handling like method, user, host.
4434 (when (string-match tramp-host-with-port-regexp l-host)
4435 (setq l-port (match-string 2 l-host)
4436 l-host (match-string 1 l-host)))
4438 ;; Check, whether there is a restricted shell.
4439 (dolist (elt tramp-restricted-shell-hosts-alist)
4440 (when (string-match elt tramp-current-host)
4443 ;; Set variables for computing the prompt for
4444 ;; reading password. They can also be derived
4446 (setq tramp-current-method (or g-method l-method)
4447 tramp-current-user (or g-user l-user)
4448 tramp-current-host (or g-host l-host))
4450 ;; Replace login-args place holders.
4452 l-host (or l-host "")
4453 l-user (or l-user "")
4454 l-port (or l-port "")
4455 spec (format-spec-make
4456 ?h l-host ?u l-user ?p l-port ?t tmpfile)
4459 ;; We do not want to see the trailing local
4460 ;; prompt in `start-file-process'.
4461 (unless r-shell "exec ")
4465 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
4466 (unless (member "" x) (mapconcat 'identity x " ")))
4468 ;; Local shell could be a Windows COMSPEC. It
4469 ;; doesn't know the ";" syntax, but we must exit
4470 ;; always for `start-file-process'. It could
4471 ;; also be a restricted shell, which does not
4473 (when r-shell " && exit || exit")))
4475 ;; Send the command.
4476 (tramp-message vec 3 "Sending command `%s'" command)
4477 (tramp-send-command vec command t t)
4478 (tramp-process-actions
4479 p vec pos tramp-actions-before-shell 60)
4481 vec 3 "Found remote shell prompt on `%s'" l-host))
4483 (setq target-alist (cdr target-alist)))
4485 ;; Make initial shell settings.
4486 (tramp-open-connection-setup-interactive-shell p vec))))
4488 ;; When the user did interrupt, we must cleanup.
4491 ;; Propagate the quit signal.
4492 (signal (car err) (cdr err)))))))
4494 (defun tramp-send-command (vec command &optional neveropen nooutput)
4495 "Send the COMMAND to connection VEC.
4496 Erases temporary buffer before sending the command. If optional
4497 arg NEVEROPEN is non-nil, never try to open the connection. This
4498 is meant to be used from `tramp-maybe-open-connection' only. The
4499 function waits for output unless NOOUTPUT is set."
4500 (unless neveropen (tramp-maybe-open-connection vec))
4501 (let ((p (tramp-get-connection-process vec)))
4502 (when (tramp-get-connection-property p "remote-echo" nil)
4503 ;; We mark the command string that it can be erased in the output buffer.
4504 (tramp-set-connection-property p "check-remote-echo" t)
4505 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
4506 (when (and (string-match "<<'EOF'" command)
4507 (not (tramp-get-connection-property vec "busybox" nil)))
4508 ;; Unset $PS1 when using here documents, in order to avoid
4509 ;; multiple prompts.
4510 (setq command (concat "(PS1= ; " command "\n)")))
4511 ;; Send the command.
4512 (tramp-message vec 6 "%s" command)
4513 (tramp-send-string vec command)
4514 (unless nooutput (tramp-wait-for-output p))))
4516 (defun tramp-wait-for-output (proc &optional timeout)
4517 "Wait for output from remote command."
4518 (unless (buffer-live-p (process-buffer proc))
4519 (delete-process proc)
4520 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
4521 (with-current-buffer (process-buffer proc)
4522 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
4523 ;; be leading escape sequences, which must be ignored.
4524 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
4525 ;; Sometimes, the commands do not return a newline but a
4526 ;; null byte before the shell prompt, for example "git
4527 ;; ls-files -c -z ...".
4528 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
4529 (found (tramp-wait-for-regexp proc timeout regexp1)))
4531 (let (buffer-read-only)
4532 ;; A simple-minded busybox has sent " ^H" sequences.
4534 (goto-char (point-min))
4535 (when (re-search-forward "^\\(.\b\\)+$" (point-at-eol) t)
4537 (delete-region (point-min) (point)))
4538 ;; Delete the prompt.
4539 (goto-char (point-max))
4540 (re-search-backward regexp nil t)
4541 (delete-region (point) (point-max)))
4545 "[[Remote prompt `%s' not found in %d secs]]"
4546 tramp-end-of-output timeout)
4549 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
4550 ;; Return value is whether end-of-output sentinel was found.
4553 (defun tramp-send-command-and-check
4554 (vec command &optional subshell dont-suppress-err)
4555 "Run COMMAND and check its exit status.
4556 Sends `echo $?' along with the COMMAND for checking the exit status. If
4557 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
4559 If the optional argument SUBSHELL is non-nil, the command is
4560 executed in a subshell, ie surrounded by parentheses. If
4561 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
4564 (concat (if subshell "( " "")
4566 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
4567 "echo tramp_exit_status $?"
4568 (if subshell " )" "")))
4569 (with-current-buffer (tramp-get-connection-buffer vec)
4570 (goto-char (point-max))
4571 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
4573 vec 'file-error "Couldn't find exit status of `%s'" command))
4574 (skip-chars-forward "^ ")
4576 (zerop (read (current-buffer)))
4577 (let (buffer-read-only)
4578 (delete-region (match-beginning 0) (point-max))))))
4580 (defun tramp-barf-unless-okay (vec command fmt &rest args)
4581 "Run COMMAND, check exit status, throw error if exit status not okay.
4582 Similar to `tramp-send-command-and-check' but accepts two more arguments
4583 FMT and ARGS which are passed to `error'."
4584 (or (tramp-send-command-and-check vec command)
4585 (apply 'tramp-error vec 'file-error fmt args)))
4587 (defun tramp-send-command-and-read (vec command &optional noerror)
4588 "Run COMMAND and return the output, which must be a Lisp expression.
4589 In case there is no valid Lisp expression and NOERROR is nil, it
4592 (tramp-send-command-and-check vec command)
4593 (tramp-barf-unless-okay
4594 vec command "`%s' returns with error" command))
4595 (with-current-buffer (tramp-get-connection-buffer vec)
4596 ;; Read the expression.
4597 (goto-char (point-min))
4599 (prog1 (read (current-buffer))
4601 (when (re-search-forward "\\S-" (point-at-eol) t)
4603 (error (unless noerror
4606 "`%s' does not return a valid Lisp expression: `%s'"
4607 command (buffer-string))))))))
4609 (defun tramp-convert-file-attributes (vec attr)
4610 "Convert file-attributes ATTR generated by perl script, stat or ls.
4611 Convert file mode bits to string and set virtual device number.
4614 ;; Remove color escape sequences from symlink.
4615 (when (stringp (car attr))
4616 (while (string-match tramp-color-escape-sequence-regexp (car attr))
4617 (setcar attr (replace-match "" nil nil (car attr)))))
4618 ;; Convert last access time.
4619 (unless (listp (nth 4 attr))
4620 (setcar (nthcdr 4 attr)
4621 (list (floor (nth 4 attr) 65536)
4622 (floor (mod (nth 4 attr) 65536)))))
4623 ;; Convert last modification time.
4624 (unless (listp (nth 5 attr))
4625 (setcar (nthcdr 5 attr)
4626 (list (floor (nth 5 attr) 65536)
4627 (floor (mod (nth 5 attr) 65536)))))
4628 ;; Convert last status change time.
4629 (unless (listp (nth 6 attr))
4630 (setcar (nthcdr 6 attr)
4631 (list (floor (nth 6 attr) 65536)
4632 (floor (mod (nth 6 attr) 65536)))))
4633 ;; Convert file size.
4634 (when (< (nth 7 attr) 0)
4635 (setcar (nthcdr 7 attr) -1))
4636 (when (and (floatp (nth 7 attr))
4637 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
4638 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
4639 ;; Convert file mode bits to string.
4640 (unless (stringp (nth 8 attr))
4641 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
4642 (when (stringp (car attr))
4643 (aset (nth 8 attr) 0 ?l)))
4644 ;; Convert directory indication bit.
4645 (when (string-match "^d" (nth 8 attr))
4647 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
4648 (when (consp (car attr))
4649 (if (and (stringp (caar attr))
4650 (string-match ".+ -> .\\(.+\\)." (caar attr)))
4651 (setcar attr (match-string 1 (caar attr)))
4653 ;; Set file's gid change bit.
4654 (setcar (nthcdr 9 attr)
4655 (if (numberp (nth 3 attr))
4656 (not (= (nth 3 attr)
4657 (tramp-get-remote-gid vec 'integer)))
4660 (tramp-get-remote-gid vec 'string)))))
4662 (unless (listp (nth 10 attr))
4663 (setcar (nthcdr 10 attr)
4665 (cons (floor (nth 10 attr) 65536)
4666 (floor (mod (nth 10 attr) 65536)))
4667 ;; Inodes can be incredible huge. We must hide this.
4668 (error (tramp-get-inode vec)))))
4669 ;; Set virtual device number.
4670 (setcar (nthcdr 11 attr)
4671 (tramp-get-device vec))
4674 (defun tramp-check-cached-permissions (vec access)
4675 "Check `file-attributes' caches for VEC.
4676 Return t if according to the cache access type ACCESS is known to
4682 ((eq ?x access) 3))))
4683 (dolist (suffix '("string" "integer") result)
4689 (tramp-get-file-property
4690 vec (tramp-file-name-localname vec)
4691 (concat "file-attributes-" suffix) nil))
4693 (tramp-get-connection-property
4694 vec (concat "uid-" suffix) nil))
4696 (tramp-get-connection-property
4697 vec (concat "gid-" suffix) nil)))
4702 (eq t (car file-attr))
4703 (null (car file-attr)))
4705 ;; World accessible.
4706 (eq access (aref (nth 8 file-attr) (+ offset 6)))
4707 ;; User accessible and owned by user.
4709 (eq access (aref (nth 8 file-attr) offset))
4710 (equal remote-uid (nth 2 file-attr)))
4711 ;; Group accessible and owned by user's
4714 (eq access (aref (nth 8 file-attr) (+ offset 3)))
4715 (equal remote-gid (nth 3 file-attr)))))))))))
4717 (defun tramp-file-mode-from-int (mode)
4718 "Turn an integer representing a file mode into an ls(1)-like string."
4720 (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
4721 (user (logand (lsh mode -6) 7))
4722 (group (logand (lsh mode -3) 7))
4723 (other (logand (lsh mode -0) 7))
4724 (suid (> (logand (lsh mode -9) 4) 0))
4725 (sgid (> (logand (lsh mode -9) 2) 0))
4726 (sticky (> (logand (lsh mode -9) 1) 0)))
4727 (setq user (tramp-file-mode-permissions user suid "s"))
4728 (setq group (tramp-file-mode-permissions group sgid "s"))
4729 (setq other (tramp-file-mode-permissions other sticky "t"))
4730 (concat type user group other)))
4732 (defun tramp-file-mode-permissions (perm suid suid-text)
4733 "Convert a permission bitset into a string.
4734 This is used internally by `tramp-file-mode-from-int'."
4735 (let ((r (> (logand perm 4) 0))
4736 (w (> (logand perm 2) 0))
4737 (x (> (logand perm 1) 0)))
4738 (concat (or (and r "r") "-")
4739 (or (and w "w") "-")
4740 (or (and suid x suid-text) ; suid, execute
4741 (and suid (upcase suid-text)) ; suid, !execute
4742 (and x "x") "-")))) ; !suid
4744 (defun tramp-shell-case-fold (string)
4745 "Converts STRING to shell glob pattern which ignores case."
4748 (if (equal (downcase c) (upcase c))
4750 (format "[%c%c]" (downcase c) (upcase c))))
4754 (defun tramp-make-copy-program-file-name (vec)
4755 "Create a file name suitable to be passed to `rcp' and workalikes."
4756 (let ((user (tramp-file-name-user vec))
4757 (host (tramp-file-name-real-host vec))
4758 (localname (tramp-shell-quote-argument
4759 (tramp-file-name-localname vec))))
4760 (if (not (zerop (length user)))
4761 (format "%s@%s:%s" user host localname)
4762 (format "%s:%s" host localname))))
4764 (defun tramp-method-out-of-band-p (vec size)
4765 "Return t if this is an out-of-band method, nil otherwise."
4767 ;; It shall be an out-of-band method.
4768 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
4769 ;; There must be a size, otherwise the file doesn't exist.
4771 ;; Either the file size is large enough, or (in rare cases) there
4772 ;; does not exist a remote encoding.
4773 (or (null tramp-copy-size-limit)
4774 (> size tramp-copy-size-limit)
4775 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
4777 ;; Variables local to connection.
4779 (defun tramp-get-remote-path (vec)
4780 (with-tramp-connection-property
4781 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
4782 ;; cache the result for the session only. Otherwise, the result
4783 ;; is cached persistently.
4784 (if (memq 'tramp-own-remote-path tramp-remote-path)
4785 (tramp-get-connection-process vec)
4788 (let* ((remote-path (copy-tree tramp-remote-path))
4789 (elt1 (memq 'tramp-default-remote-path remote-path))
4790 (elt2 (memq 'tramp-own-remote-path remote-path))
4791 (default-remote-path
4794 (tramp-send-command-and-read
4795 vec "echo \\\"`getconf PATH 2>/dev/null`\\\"" 'noerror)
4796 ;; Default if "getconf" is not available.
4800 "`getconf PATH' not successful, using default value \"%s\"."
4806 (tramp-send-command-and-read vec "echo \\\"$PATH\\\"")
4809 vec 3 "$PATH not set, ignoring `tramp-own-remote-path'.")
4812 ;; Replace place holder `tramp-default-remote-path'.
4816 (tramp-compat-split-string default-remote-path ":")
4818 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
4820 ;; Replace place holder `tramp-own-remote-path'.
4824 (tramp-compat-split-string own-remote-path ":")
4826 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
4828 ;; Remove double entries.
4829 (setq elt1 remote-path)
4831 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
4833 (setq elt1 (cdr elt1)))
4835 ;; Remove non-existing directories.
4843 (tramp-make-tramp-file-name
4844 (tramp-file-name-method vec)
4845 (tramp-file-name-user vec)
4846 (tramp-file-name-host vec)
4851 (defun tramp-get-ls-command (vec)
4852 (with-tramp-connection-property vec "ls"
4853 (tramp-message vec 5 "Finding a suitable `ls' command")
4856 (dolist (cmd '("ls" "gnuls" "gls"))
4857 (let ((dl (tramp-get-remote-path vec))
4859 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
4860 ;; Check parameters. On busybox, "ls" output coloring is
4861 ;; enabled by default sometimes. So we try to disable it
4862 ;; when possible. $LS_COLORING is not supported there.
4863 ;; Some "ls" versions are sensible wrt the order of
4864 ;; arguments, they fail when "-al" is after the
4865 ;; "--color=never" argument (for example on FreeBSD).
4866 (when (tramp-send-command-and-check
4867 vec (format "%s -lnd /" result))
4868 (when (tramp-send-command-and-check
4870 "%s --color=never -al /dev/null" result))
4871 (setq result (concat result " --color=never")))
4872 (throw 'ls-found result))
4873 (setq dl (cdr dl))))))
4874 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
4876 (defun tramp-get-ls-command-with-dired (vec)
4878 (with-tramp-connection-property vec "ls-dired"
4879 (tramp-message vec 5 "Checking, whether `ls --dired' works")
4880 ;; Some "ls" versions are sensible wrt the order of arguments,
4881 ;; they fail when "-al" is after the "--dired" argument (for
4882 ;; example on FreeBSD).
4883 (tramp-send-command-and-check
4884 vec (format "%s --dired -al /dev/null" (tramp-get-ls-command vec))))))
4886 (defun tramp-get-test-command (vec)
4887 (with-tramp-connection-property vec "test"
4888 (tramp-message vec 5 "Finding a suitable `test' command")
4889 (if (tramp-send-command-and-check vec "test 0")
4891 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
4893 (defun tramp-get-test-nt-command (vec)
4894 ;; Does `test A -nt B' work? Use abominable `find' construct if it
4895 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
4896 ;; for otherwise the shell crashes.
4897 (with-tramp-connection-property vec "test-nt"
4901 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
4902 (with-current-buffer (tramp-get-buffer vec)
4903 (goto-char (point-min))
4904 (when (looking-at (regexp-quote tramp-end-of-output))
4905 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
4910 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
4911 (tramp-get-test-command vec)))
4912 "tramp_test_nt %s %s"))))
4914 (defun tramp-get-file-exists-command (vec)
4915 (with-tramp-connection-property vec "file-exists"
4916 (tramp-message vec 5 "Finding command to check if file exists")
4917 (tramp-find-file-exists-command vec)))
4919 (defun tramp-get-remote-ln (vec)
4920 (with-tramp-connection-property vec "ln"
4921 (tramp-message vec 5 "Finding a suitable `ln' command")
4922 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
4924 (defun tramp-get-remote-perl (vec)
4925 (with-tramp-connection-property vec "perl"
4926 (tramp-message vec 5 "Finding a suitable `perl' command")
4928 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
4929 (tramp-find-executable
4930 vec "perl" (tramp-get-remote-path vec)))))
4931 ;; We must check also for some Perl modules.
4933 (with-tramp-connection-property vec "perl-file-spec"
4934 (tramp-send-command-and-check
4935 vec (format "%s -e 'use File::Spec;'" result)))
4936 (with-tramp-connection-property vec "perl-cwd-realpath"
4937 (tramp-send-command-and-check
4938 vec (format "%s -e 'use Cwd \"realpath\";'" result))))
4941 (defun tramp-get-remote-stat (vec)
4942 (with-tramp-connection-property vec "stat"
4943 (tramp-message vec 5 "Finding a suitable `stat' command")
4944 (let ((result (tramp-find-executable
4945 vec "stat" (tramp-get-remote-path vec)))
4947 ;; Check whether stat(1) returns usable syntax. "%s" does not
4948 ;; work on older AIX systems.
4951 (tramp-send-command-and-read
4952 vec (format "%s -c '(\"%%N\" %%s)' /" result) 'noerror))
4953 (unless (and (listp tmp) (stringp (car tmp))
4954 (string-match "^./.$" (car tmp))
4955 (integerp (cadr tmp)))
4959 (defun tramp-get-remote-readlink (vec)
4960 (with-tramp-connection-property vec "readlink"
4961 (tramp-message vec 5 "Finding a suitable `readlink' command")
4962 (let ((result (tramp-find-executable
4963 vec "readlink" (tramp-get-remote-path vec))))
4965 (tramp-send-command-and-check
4966 vec (format "%s --canonicalize-missing /" result)))
4969 (defun tramp-get-remote-trash (vec)
4970 (with-tramp-connection-property vec "trash"
4971 (tramp-message vec 5 "Finding a suitable `trash' command")
4972 (tramp-find-executable vec "trash" (tramp-get-remote-path vec))))
4974 (defun tramp-get-remote-id (vec)
4975 (with-tramp-connection-property vec "id"
4976 (tramp-message vec 5 "Finding POSIX `id' command")
4979 (let ((dl (tramp-get-remote-path vec))
4981 (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
4982 ;; Check POSIX parameter.
4983 (when (tramp-send-command-and-check vec (format "%s -u" result))
4984 (throw 'id-found result))
4985 (setq dl (cdr dl)))))
4986 (tramp-error vec 'file-error "Couldn't find a POSIX `id' command"))))
4988 (defun tramp-get-remote-uid (vec id-format)
4989 (with-tramp-connection-property vec (format "uid-%s" id-format)
4990 (let ((res (tramp-send-command-and-read
4992 (format "%s -u%s %s"
4993 (tramp-get-remote-id vec)
4994 (if (equal id-format 'integer) "" "n")
4995 (if (equal id-format 'integer)
4996 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
4997 ;; The command might not always return a number.
4998 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
5000 (defun tramp-get-remote-gid (vec id-format)
5001 (with-tramp-connection-property vec (format "gid-%s" id-format)
5002 (let ((res (tramp-send-command-and-read
5004 (format "%s -g%s %s"
5005 (tramp-get-remote-id vec)
5006 (if (equal id-format 'integer) "" "n")
5007 (if (equal id-format 'integer)
5008 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
5009 ;; The command might not always return a number.
5010 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
5012 (defun tramp-get-local-uid (id-format)
5013 (if (equal id-format 'integer) (user-uid) (user-login-name)))
5015 (defun tramp-get-local-gid (id-format)
5016 (nth 3 (tramp-compat-file-attributes "~/" id-format)))
5018 ;; Some predefined connection properties.
5019 (defun tramp-get-inline-compress (vec prop size)
5020 "Return the compress command related to PROP.
5021 PROP is either `inline-compress' or `inline-decompress'. SIZE is
5022 the length of the file to be compressed.
5024 If no corresponding command is found, nil is returned."
5025 (when (and (integerp tramp-inline-compress-start-size)
5026 (> size tramp-inline-compress-start-size))
5027 (with-tramp-connection-property (tramp-get-connection-process vec) prop
5028 (tramp-find-inline-compress vec)
5029 (tramp-get-connection-property
5030 (tramp-get-connection-process vec) prop nil))))
5032 (defun tramp-get-inline-coding (vec prop size)
5033 "Return the coding command related to PROP.
5034 PROP is either `remote-encoding', `remote-decoding',
5035 `local-encoding' or `local-decoding'.
5037 SIZE is the length of the file to be coded. Depending on SIZE,
5038 compression might be applied.
5040 If no corresponding command is found, nil is returned.
5041 Otherwise, either a string is returned which contains a `%s' mark
5042 to be used for the respective input or output file; or a Lisp
5043 function cell is returned to be applied on a buffer."
5044 ;; We must catch the errors, because we want to return `nil', when
5045 ;; no inline coding is found.
5048 (with-tramp-connection-property
5049 (tramp-get-connection-process vec) prop
5050 (tramp-find-inline-encoding vec)
5051 (tramp-get-connection-property
5052 (tramp-get-connection-process vec) prop nil)))
5053 (prop1 (if (string-match "encoding" prop)
5054 "inline-compress" "inline-decompress"))
5056 ;; The connection property might have been cached. So we must
5057 ;; send the script to the remote side - maybe.
5058 (when (and coding (symbolp coding) (string-match "remote" prop))
5059 (let ((name (symbol-name coding)))
5060 (while (string-match (regexp-quote "-") name)
5061 (setq name (replace-match "_" nil t name)))
5062 (tramp-maybe-send-script vec (symbol-value coding) name)
5063 (setq coding name)))
5065 ;; Check for the `compress' command.
5066 (setq compress (tramp-get-inline-compress vec prop1 size))
5067 ;; Return the value.
5069 ((and compress (symbolp coding))
5070 (if (string-match "decompress" prop1)
5073 (let ((coding-system-for-write 'binary)
5074 (coding-system-for-read 'binary))
5076 'call-process-region (point-min) (point-max)
5077 (car (split-string ,compress)) t t nil
5078 (cdr (split-string ,compress)))))
5080 (let ((coding-system-for-write 'binary)
5081 (coding-system-for-read 'binary))
5083 'call-process-region beg end
5084 (car (split-string ,compress)) t t nil
5085 (cdr (split-string ,compress))))
5086 (,coding (point-min) (point-max)))))
5089 ((and compress (string-match "decoding" prop))
5091 ;; Windows shells need the program file name after
5092 ;; the pipe symbol be quoted if they use forward
5093 ;; slashes as directory separators.
5094 (if (and (string-match "local" prop)
5095 (memq system-type '(windows-nt)))
5096 "(%s | \"%s\" >%%s)"
5101 ;; Windows shells need the program file name after
5102 ;; the pipe symbol be quoted if they use forward
5103 ;; slashes as directory separators.
5104 (if (and (string-match "local" prop)
5105 (memq system-type '(windows-nt)))
5106 "(%s <%%s | \"%s\")"
5109 ((string-match "decoding" prop)
5110 (format "%s >%%s" coding))
5112 (format "%s <%%s" coding)))))))
5114 ;;; Integration of eshell.el:
5117 (defvar eshell-path-env))
5119 ;; eshell.el keeps the path in `eshell-path-env'. We must change it
5120 ;; when `default-directory' points to another host.
5121 (defun tramp-eshell-directory-change ()
5122 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
5123 (setq eshell-path-env
5124 (if (file-remote-p default-directory)
5125 (with-parsed-tramp-file-name default-directory nil
5128 (tramp-get-remote-path v)
5132 (eval-after-load "esh-util"
5134 (tramp-eshell-directory-change)
5135 (add-hook 'eshell-directory-change-hook
5136 'tramp-eshell-directory-change)
5137 (add-hook 'tramp-unload-hook
5139 (remove-hook 'eshell-directory-change-hook
5140 'tramp-eshell-directory-change)))))
5142 (add-hook 'tramp-unload-hook
5144 (unload-feature 'tramp-sh 'force)))
5150 ;; * Don't use globbing for directories with many files, as this is
5151 ;; likely to produce long command lines, and some shells choke on
5152 ;; long command lines.
5153 ;; * Make it work for different encodings, and for different file name
5154 ;; encodings, too. (Daniel Pittman)
5155 ;; * Don't search for perl5 and perl. Instead, only search for perl and
5156 ;; then look if it's the right version (with `perl -v').
5157 ;; * When editing a remote CVS controlled file as a different user, VC
5158 ;; gets confused about the file locking status. Try to find out why
5159 ;; the workaround doesn't work.
5160 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
5161 ;; until the last but one hop via `start-file-process'. Apply it
5162 ;; also for ftp and smb.
5163 ;; * WIBNI if we had a command "trampclient"? If I was editing in
5164 ;; some shell with root privileges, it would be nice if I could
5166 ;; trampclient filename.c
5167 ;; as an editor, and the _current_ shell would connect to an Emacs
5168 ;; server and would be used in an existing non-privileged Emacs
5169 ;; session for doing the editing in question.
5170 ;; That way, I need not tell Emacs my password again and be afraid
5171 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
5172 ;; once display a just typed password in the context of a keyboard
5173 ;; sequence prompt for a question immediately following in a shell
5174 ;; script run within Emacs -- nasty).
5175 ;; And if I have some ssh session running to a different computer,
5176 ;; having the possibility of passing a local file there to a local
5177 ;; Emacs session (in case I can arrange for a connection back) would
5179 ;; Likely the corresponding Tramp server should not allow the
5180 ;; equivalent of the emacsclient -eval option in order to make this
5181 ;; reasonably unproblematic. And maybe trampclient should have some
5182 ;; way of passing credentials, like by using an SSL socket or
5183 ;; something. (David Kastrup)
5184 ;; * Reconnect directly to a compliant shell without first going
5185 ;; through the user's default shell. (Pete Forman)
5186 ;; * How can I interrupt the remote process with a signal
5187 ;; (interrupt-process seems not to work)? (Markus Triska)
5188 ;; * Avoid the local shell entirely for starting remote processes. If
5189 ;; so, I think even a signal, when delivered directly to the local
5190 ;; SSH instance, would correctly be propagated to the remote process
5191 ;; automatically; possibly SSH would have to be started with
5192 ;; "-t". (Markus Triska)
5193 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
5194 ;; isn't on the remote host. (Mark A. Hershberger)
5195 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
5196 ;; * Optimize out-of-band copying when both methods are scp-like (not
5198 ;; * Keep a second connection open for out-of-band methods like scp or
5200 ;; * Try telnet+curl as new method. It might be useful for busybox,
5201 ;; without built-in uuencode/uudecode.
5203 ;;; tramp-sh.el ends here