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