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