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