* net/tramp.el (tramp-get-ls-command)
[emacs.git] / lisp / net / tramp.el
blob1c9a7015bf2b9e4111ea8bee2fa52d25a4c36fa5
1 ;;; tramp.el --- Transparent Remote Access, Multiple Protocol
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; (copyright statements below in code to be updated with the above notice)
8 ;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
9 ;; Michael Albinus <michael.albinus@gmx.de>
10 ;; Keywords: comm, processes
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
27 ;;; Commentary:
29 ;; This package provides remote file editing, similar to ange-ftp.
30 ;; The difference is that ange-ftp uses FTP to transfer files between
31 ;; the local and the remote host, whereas tramp.el uses a combination
32 ;; of rsh and rcp or other work-alike programs, such as ssh/scp.
34 ;; For more detailed instructions, please see the info file.
36 ;; Notes:
37 ;; -----
39 ;; This package only works for Emacs 22.1 and higher, and for XEmacs 21.4
40 ;; and higher. For XEmacs 21, you need the package `fsf-compat' for
41 ;; the `with-timeout' macro.
43 ;; Also see the todo list at the bottom of this file.
45 ;; The current version of Tramp can be retrieved from the following URL:
46 ;; http://ftp.gnu.org/gnu/tramp/
48 ;; There's a mailing list for this, as well. Its name is:
49 ;; tramp-devel@gnu.org
50 ;; You can use the Web to subscribe, under the following URL:
51 ;; http://lists.gnu.org/mailman/listinfo/tramp-devel
53 ;; For the adventurous, the current development sources are available
54 ;; via CVS. You can find instructions about this at the following URL:
55 ;; http://savannah.gnu.org/projects/tramp/
56 ;; Click on "CVS" in the navigation bar near the top.
58 ;; Don't forget to put on your asbestos longjohns, first!
60 ;;; Code:
62 ;; Since Emacs 23.1, loading messages have been disabled during
63 ;; autoload. However, loading Tramp takes a while, and it could
64 ;; happen while typing a filename in the minibuffer. Therefore, Tramp
65 ;; shall inform about.
66 (when (and load-in-progress (null (current-message)))
67 (message "Loading tramp..."))
69 ;; The Tramp version number and bug report address, as prepared by configure.
70 (require 'trampver)
71 (add-hook 'tramp-unload-hook
72 (lambda ()
73 (when (featurep 'trampver)
74 (unload-feature 'trampver 'force))))
76 (require 'tramp-compat)
77 (add-hook 'tramp-unload-hook
78 (lambda ()
79 (when (featurep 'tramp-compat)
80 (unload-feature 'tramp-compat 'force))))
82 (require 'format-spec)
83 ;; As long as password.el is not part of (X)Emacs, it shouldn't
84 ;; be mandatory
85 (if (featurep 'xemacs)
86 (load "password" 'noerror)
87 (or (require 'password-cache nil 'noerror)
88 (require 'password nil 'noerror))) ; from No Gnus, also in tar ball
90 (require 'shell)
91 (require 'advice)
93 (eval-and-compile
94 (if (featurep 'xemacs)
95 (load "auth-source" 'noerror)
96 (require 'auth-source nil 'noerror)))
98 ;; Requiring 'tramp-cache results in an endless loop.
99 (autoload 'tramp-get-file-property "tramp-cache")
100 (autoload 'tramp-set-file-property "tramp-cache")
101 (autoload 'tramp-flush-file-property "tramp-cache")
102 (autoload 'tramp-flush-directory-property "tramp-cache")
103 (autoload 'tramp-get-connection-property "tramp-cache")
104 (autoload 'tramp-set-connection-property "tramp-cache")
105 (autoload 'tramp-flush-connection-property "tramp-cache")
106 (autoload 'tramp-parse-connection-properties "tramp-cache")
107 (add-hook 'tramp-unload-hook
108 (lambda ()
109 (when (featurep 'tramp-cache)
110 (unload-feature 'tramp-cache 'force))))
112 (autoload 'tramp-uuencode-region "tramp-uu"
113 "Implementation of `uuencode' in Lisp.")
114 (add-hook 'tramp-unload-hook
115 (lambda ()
116 (when (featurep 'tramp-uu)
117 (unload-feature 'tramp-uu 'force))))
119 (autoload 'uudecode-decode-region "uudecode")
121 ;; The following Tramp packages must be loaded after tramp.el, because
122 ;; they require it as well.
123 (eval-after-load "tramp"
124 '(dolist
125 (feature
126 (list
128 ;; Tramp interactive commands.
129 'tramp-cmds
131 ;; Load foreign FTP method.
132 (if (featurep 'xemacs) 'tramp-efs 'tramp-ftp)
134 ;; tramp-smb uses "smbclient" from Samba. Not available
135 ;; under Cygwin and Windows, because they don't offer
136 ;; "smbclient". And even not necessary there, because Emacs
137 ;; supports UNC file names like "//host/share/localname".
138 (unless (memq system-type '(cygwin windows-nt)) 'tramp-smb)
140 ;; Load foreign FISH method.
141 'tramp-fish
143 ;; tramp-gvfs needs D-Bus messages. Available since Emacs 23
144 ;; on some system types. We don't call `dbus-ping', because
145 ;; this would load dbus.el.
146 (when (and (featurep 'dbusbind)
147 (condition-case nil
148 (tramp-compat-funcall 'dbus-get-unique-name :session)
149 (error nil))
150 (tramp-compat-process-running-p "gvfs-fuse-daemon"))
151 'tramp-gvfs)
153 ;; Load gateways. It needs `make-network-process' from Emacs 22.
154 (when (functionp 'make-network-process) 'tramp-gw)
156 ;; tramp-imap needs both epa (from Emacs 23.1) and imap-hash
157 ;; (from Emacs 23.2).
158 (when (and (locate-library "epa") (locate-library "imap-hash"))
159 'tramp-imap)))
161 (when feature
162 ;; We have used just some basic tests, whether a package shall
163 ;; be added. There might still be other errors during loading,
164 ;; which we will catch here.
165 (catch 'tramp-loading
166 (require feature)
167 (add-hook 'tramp-unload-hook
168 `(lambda ()
169 (when (featurep (quote ,feature))
170 (unload-feature (quote ,feature) 'force)))))
171 (unless (featurep feature)
172 (message "Loading %s failed, ignoring this package" feature)))))
174 ;;; User Customizable Internal Variables:
176 (defgroup tramp nil
177 "Edit remote files with a combination of rsh and rcp or similar programs."
178 :group 'files
179 :group 'comm
180 :version "22.1")
182 ;; Maybe we need once a real Tramp mode, with key bindings etc.
183 ;;;###autoload
184 (defcustom tramp-mode t
185 "*Whether Tramp is enabled.
186 If it is set to nil, all remote file names are used literally."
187 :group 'tramp
188 :type 'boolean)
190 (defcustom tramp-verbose 3
191 "*Verbosity level for Tramp messages.
192 Any level x includes messages for all levels 1 .. x-1. The levels are
194 0 silent (no tramp messages at all)
195 1 errors
196 2 warnings
197 3 connection to remote hosts (default level)
198 4 activities
199 5 internal
200 6 sent and received strings
201 7 file caching
202 8 connection properties
203 10 traces (huge)."
204 :group 'tramp
205 :type 'integer)
207 ;; Emacs case.
208 (eval-and-compile
209 (when (boundp 'backup-directory-alist)
210 (defcustom tramp-backup-directory-alist nil
211 "Alist of filename patterns and backup directory names.
212 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
213 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
214 is a local file name, the backup directory is prepended with Tramp file
215 name prefix \(method, user, host\) of file.
217 \(setq tramp-backup-directory-alist backup-directory-alist\)
219 gives the same backup policy for Tramp files on their hosts like the
220 policy for local files."
221 :group 'tramp
222 :type '(repeat (cons (regexp :tag "Regexp matching filename")
223 (directory :tag "Backup directory name"))))))
225 ;; XEmacs case. We cannot check for `bkup-backup-directory-info', because
226 ;; the package "backup-dir" might not be loaded yet.
227 (eval-and-compile
228 (when (featurep 'xemacs)
229 (defcustom tramp-bkup-backup-directory-info nil
230 "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
231 It has the same meaning like `bkup-backup-directory-info' from package
232 `backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local
233 file name, the backup directory is prepended with Tramp file name prefix
234 \(method, user, host\) of file.
236 \(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\)
238 gives the same backup policy for Tramp files on their hosts like the
239 policy for local files."
240 :type '(repeat
241 (list (regexp :tag "File regexp")
242 (string :tag "Backup Dir")
243 (set :inline t
244 (const ok-create)
245 (const full-path)
246 (const prepend-name)
247 (const search-upward))))
248 :group 'tramp)))
250 (defcustom tramp-auto-save-directory nil
251 "*Put auto-save files in this directory, if set.
252 The idea is to use a local directory so that auto-saving is faster."
253 :group 'tramp
254 :type '(choice (const nil) string))
256 (defcustom tramp-encoding-shell
257 (if (memq system-type '(windows-nt))
258 (getenv "COMSPEC")
259 "/bin/sh")
260 "*Use this program for encoding and decoding commands on the local host.
261 This shell is used to execute the encoding and decoding command on the
262 local host, so if you want to use `~' in those commands, you should
263 choose a shell here which groks tilde expansion. `/bin/sh' normally
264 does not understand tilde expansion.
266 For encoding and deocding, commands like the following are executed:
268 /bin/sh -c COMMAND < INPUT > OUTPUT
270 This variable can be used to change the \"/bin/sh\" part. See the
271 variable `tramp-encoding-command-switch' for the \"-c\" part.
273 Note that this variable is not used for remote commands. There are
274 mechanisms in tramp.el which automatically determine the right shell to
275 use for the remote host."
276 :group 'tramp
277 :type '(file :must-match t))
279 (defcustom tramp-encoding-command-switch
280 (if (string-match "cmd\\.exe" tramp-encoding-shell)
281 "/c"
282 "-c")
283 "*Use this switch together with `tramp-encoding-shell' for local commands.
284 See the variable `tramp-encoding-shell' for more information."
285 :group 'tramp
286 :type 'string)
288 (defcustom tramp-inline-compress-start-size 4096
289 "*The minimum size of compressing where inline transfer.
290 When inline transfer, compress transfered data of file
291 whose size is this value or above (up to `tramp-copy-size-limit').
292 If it is nil, no compression at all will be applied."
293 :group 'tramp
294 :type '(choice (const nil) integer))
296 (defcustom tramp-copy-size-limit 10240
297 "*The maximum file size where inline copying is preferred over an out-of-the-band copy.
298 If it is nil, inline out-of-the-band copy will be used without a check."
299 :group 'tramp
300 :type '(choice (const nil) integer))
302 (defcustom tramp-terminal-type "dumb"
303 "*Value of TERM environment variable for logging in to remote host.
304 Because Tramp wants to parse the output of the remote shell, it is easily
305 confused by ANSI color escape sequences and suchlike. Often, shell init
306 files conditionalize this setup based on the TERM environment variable."
307 :group 'tramp
308 :type 'string)
310 ;; ksh on OpenBSD 4.5 requires, that PS1 contains a `#' character for
311 ;; root users. It uses the `$' character for other users. In order
312 ;; to guarantee a proper prompt, we use "#$" for the prompt.
314 (defvar tramp-end-of-output
315 (format
316 "///%s#$"
317 (md5 (concat (prin1-to-string process-environment) (current-time-string))))
318 "String used to recognize end of output.
319 The '$' character at the end is quoted; the string cannot be
320 detected as prompt when being sent on echoing hosts, therefore.")
322 (defconst tramp-initial-end-of-output "#$ "
323 "Prompt when establishing a connection.")
325 (defvar tramp-methods
326 `(("rcp" (tramp-login-program "rsh")
327 (tramp-login-args (("%h") ("-l" "%u")))
328 (tramp-remote-sh "/bin/sh")
329 (tramp-copy-program "rcp")
330 (tramp-copy-args (("-p" "%k") ("-r")))
331 (tramp-copy-keep-date t)
332 (tramp-copy-recursive t)
333 (tramp-password-end-of-line nil))
334 ("scp" (tramp-login-program "ssh")
335 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
336 ("-e" "none")))
337 (tramp-async-args (("-q")))
338 (tramp-remote-sh "/bin/sh")
339 (tramp-copy-program "scp")
340 (tramp-copy-args (("-P" "%p") ("-p" "%k")
341 ("-q") ("-r")))
342 (tramp-copy-keep-date t)
343 (tramp-copy-recursive t)
344 (tramp-password-end-of-line nil)
345 (tramp-gw-args (("-o"
346 "GlobalKnownHostsFile=/dev/null")
347 ("-o" "UserKnownHostsFile=/dev/null")
348 ("-o" "StrictHostKeyChecking=no")))
349 (tramp-default-port 22))
350 ("scp1" (tramp-login-program "ssh")
351 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
352 ("-1" "-e" "none")))
353 (tramp-async-args (("-q")))
354 (tramp-remote-sh "/bin/sh")
355 (tramp-copy-program "scp")
356 (tramp-copy-args (("-1") ("-P" "%p") ("-p" "%k")
357 ("-q") ("-r")))
358 (tramp-copy-keep-date t)
359 (tramp-copy-recursive t)
360 (tramp-password-end-of-line nil)
361 (tramp-gw-args (("-o"
362 "GlobalKnownHostsFile=/dev/null")
363 ("-o" "UserKnownHostsFile=/dev/null")
364 ("-o" "StrictHostKeyChecking=no")))
365 (tramp-default-port 22))
366 ("scp2" (tramp-login-program "ssh")
367 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
368 ("-2" "-e" "none")))
369 (tramp-async-args (("-q")))
370 (tramp-remote-sh "/bin/sh")
371 (tramp-copy-program "scp")
372 (tramp-copy-args (("-2") ("-P" "%p") ("-p" "%k")
373 ("-q") ("-r")))
374 (tramp-copy-keep-date t)
375 (tramp-copy-recursive t)
376 (tramp-password-end-of-line nil)
377 (tramp-gw-args (("-o"
378 "GlobalKnownHostsFile=/dev/null")
379 ("-o" "UserKnownHostsFile=/dev/null")
380 ("-o" "StrictHostKeyChecking=no")))
381 (tramp-default-port 22))
382 ("scp1_old"
383 (tramp-login-program "ssh1")
384 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
385 ("-e" "none")))
386 (tramp-remote-sh "/bin/sh")
387 (tramp-copy-program "scp1")
388 (tramp-copy-args (("-p" "%k") ("-r")))
389 (tramp-copy-keep-date t)
390 (tramp-copy-recursive t)
391 (tramp-password-end-of-line nil))
392 ("scp2_old"
393 (tramp-login-program "ssh2")
394 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
395 ("-e" "none")))
396 (tramp-remote-sh "/bin/sh")
397 (tramp-copy-program "scp2")
398 (tramp-copy-args (("-p" "%k") ("-r")))
399 (tramp-copy-keep-date t)
400 (tramp-copy-recursive t)
401 (tramp-password-end-of-line nil))
402 ("sftp" (tramp-login-program "ssh")
403 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
404 ("-e" "none")))
405 (tramp-async-args (("-q")))
406 (tramp-remote-sh "/bin/sh")
407 (tramp-copy-program "sftp")
408 (tramp-copy-args nil)
409 (tramp-copy-keep-date nil)
410 (tramp-password-end-of-line nil))
411 ("rsync" (tramp-login-program "ssh")
412 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
413 ("-e" "none")))
414 (tramp-async-args (("-q")))
415 (tramp-remote-sh "/bin/sh")
416 (tramp-copy-program "rsync")
417 (tramp-copy-args (("-e" "ssh") ("-t" "%k") ("-r")))
418 (tramp-copy-keep-date t)
419 (tramp-copy-keep-tmpfile t)
420 (tramp-copy-recursive t)
421 (tramp-password-end-of-line nil))
422 ("rsyncc"
423 (tramp-login-program "ssh")
424 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
425 ("-o" "ControlPath=%t.%%r@%%h:%%p")
426 ("-o" "ControlMaster=yes")
427 ("-e" "none")))
428 (tramp-async-args (("-q")))
429 (tramp-remote-sh "/bin/sh")
430 (tramp-copy-program "rsync")
431 (tramp-copy-args (("-t" "%k") ("-r")))
432 (tramp-copy-env (("RSYNC_RSH")
433 (,(concat
434 "ssh"
435 " -o ControlPath=%t.%%r@%%h:%%p"
436 " -o ControlMaster=auto"))))
437 (tramp-copy-keep-date t)
438 (tramp-copy-keep-tmpfile t)
439 (tramp-copy-recursive t)
440 (tramp-password-end-of-line nil))
441 ("remcp" (tramp-login-program "remsh")
442 (tramp-login-args (("%h") ("-l" "%u")))
443 (tramp-remote-sh "/bin/sh")
444 (tramp-copy-program "rcp")
445 (tramp-copy-args (("-p" "%k")))
446 (tramp-copy-keep-date t)
447 (tramp-password-end-of-line nil))
448 ("rsh" (tramp-login-program "rsh")
449 (tramp-login-args (("%h") ("-l" "%u")))
450 (tramp-remote-sh "/bin/sh")
451 (tramp-copy-program nil)
452 (tramp-copy-args nil)
453 (tramp-copy-keep-date nil)
454 (tramp-password-end-of-line nil))
455 ("ssh" (tramp-login-program "ssh")
456 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
457 ("-e" "none")))
458 (tramp-async-args (("-q")))
459 (tramp-remote-sh "/bin/sh")
460 (tramp-copy-program nil)
461 (tramp-copy-args nil)
462 (tramp-copy-keep-date nil)
463 (tramp-password-end-of-line nil)
464 (tramp-gw-args (("-o"
465 "GlobalKnownHostsFile=/dev/null")
466 ("-o" "UserKnownHostsFile=/dev/null")
467 ("-o" "StrictHostKeyChecking=no")))
468 (tramp-default-port 22))
469 ("ssh1" (tramp-login-program "ssh")
470 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
471 ("-1" "-e" "none")))
472 (tramp-async-args (("-q")))
473 (tramp-remote-sh "/bin/sh")
474 (tramp-copy-program nil)
475 (tramp-copy-args nil)
476 (tramp-copy-keep-date nil)
477 (tramp-password-end-of-line nil)
478 (tramp-gw-args (("-o"
479 "GlobalKnownHostsFile=/dev/null")
480 ("-o" "UserKnownHostsFile=/dev/null")
481 ("-o" "StrictHostKeyChecking=no")))
482 (tramp-default-port 22))
483 ("ssh2" (tramp-login-program "ssh")
484 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
485 ("-2" "-e" "none")))
486 (tramp-async-args (("-q")))
487 (tramp-remote-sh "/bin/sh")
488 (tramp-copy-program nil)
489 (tramp-copy-args nil)
490 (tramp-copy-keep-date nil)
491 (tramp-password-end-of-line nil)
492 (tramp-gw-args (("-o"
493 "GlobalKnownHostsFile=/dev/null")
494 ("-o" "UserKnownHostsFile=/dev/null")
495 ("-o" "StrictHostKeyChecking=no")))
496 (tramp-default-port 22))
497 ("ssh1_old"
498 (tramp-login-program "ssh1")
499 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
500 ("-e" "none")))
501 (tramp-async-args (("-q")))
502 (tramp-remote-sh "/bin/sh")
503 (tramp-copy-program nil)
504 (tramp-copy-args nil)
505 (tramp-copy-keep-date nil)
506 (tramp-password-end-of-line nil))
507 ("ssh2_old"
508 (tramp-login-program "ssh2")
509 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
510 ("-e" "none")))
511 (tramp-remote-sh "/bin/sh")
512 (tramp-copy-program nil)
513 (tramp-copy-args nil)
514 (tramp-copy-keep-date nil)
515 (tramp-password-end-of-line nil))
516 ("remsh" (tramp-login-program "remsh")
517 (tramp-login-args (("%h") ("-l" "%u")))
518 (tramp-remote-sh "/bin/sh")
519 (tramp-copy-program nil)
520 (tramp-copy-args nil)
521 (tramp-copy-keep-date nil)
522 (tramp-password-end-of-line nil))
523 ("telnet"
524 (tramp-login-program "telnet")
525 (tramp-login-args (("%h") ("%p")))
526 (tramp-remote-sh "/bin/sh")
527 (tramp-copy-program nil)
528 (tramp-copy-args nil)
529 (tramp-copy-keep-date nil)
530 (tramp-password-end-of-line nil)
531 (tramp-default-port 23))
532 ("su" (tramp-login-program "su")
533 (tramp-login-args (("-") ("%u")))
534 (tramp-remote-sh "/bin/sh")
535 (tramp-copy-program nil)
536 (tramp-copy-args nil)
537 (tramp-copy-keep-date nil)
538 (tramp-password-end-of-line nil))
539 ("sudo" (tramp-login-program "sudo")
540 (tramp-login-args (("-u" "%u")
541 ("-s") ("-H") ("-p" "Password:")))
542 (tramp-remote-sh "/bin/sh")
543 (tramp-copy-program nil)
544 (tramp-copy-args nil)
545 (tramp-copy-keep-date nil)
546 (tramp-password-end-of-line nil))
547 ("scpc" (tramp-login-program "ssh")
548 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
549 ("-o" "ControlPath=%t.%%r@%%h:%%p")
550 ("-o" "ControlMaster=yes")
551 ("-e" "none")))
552 (tramp-async-args (("-q")))
553 (tramp-remote-sh "/bin/sh")
554 (tramp-copy-program "scp")
555 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q")
556 ("-o" "ControlPath=%t.%%r@%%h:%%p")
557 ("-o" "ControlMaster=auto")))
558 (tramp-copy-keep-date t)
559 (tramp-password-end-of-line nil)
560 (tramp-gw-args (("-o"
561 "GlobalKnownHostsFile=/dev/null")
562 ("-o" "UserKnownHostsFile=/dev/null")
563 ("-o" "StrictHostKeyChecking=no")))
564 (tramp-default-port 22))
565 ("scpx" (tramp-login-program "ssh")
566 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
567 ("-e" "none" "-t" "-t" "/bin/sh")))
568 (tramp-async-args (("-q")))
569 (tramp-remote-sh "/bin/sh")
570 (tramp-copy-program "scp")
571 (tramp-copy-args (("-p" "%k")))
572 (tramp-copy-keep-date t)
573 (tramp-password-end-of-line nil)
574 (tramp-gw-args (("-o"
575 "GlobalKnownHostsFile=/dev/null")
576 ("-o" "UserKnownHostsFile=/dev/null")
577 ("-o" "StrictHostKeyChecking=no")))
578 (tramp-default-port 22))
579 ("sshx" (tramp-login-program "ssh")
580 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
581 ("-e" "none" "-t" "-t" "/bin/sh")))
582 (tramp-async-args (("-q")))
583 (tramp-remote-sh "/bin/sh")
584 (tramp-copy-program nil)
585 (tramp-copy-args nil)
586 (tramp-copy-keep-date nil)
587 (tramp-password-end-of-line nil)
588 (tramp-gw-args (("-o"
589 "GlobalKnownHostsFile=/dev/null")
590 ("-o" "UserKnownHostsFile=/dev/null")
591 ("-o" "StrictHostKeyChecking=no")))
592 (tramp-default-port 22))
593 ("krlogin"
594 (tramp-login-program "krlogin")
595 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
596 (tramp-remote-sh "/bin/sh")
597 (tramp-copy-program nil)
598 (tramp-copy-args nil)
599 (tramp-copy-keep-date nil)
600 (tramp-password-end-of-line nil))
601 ("plink" (tramp-login-program "plink")
602 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
603 ("-ssh")))
604 (tramp-remote-sh "/bin/sh")
605 (tramp-copy-program nil)
606 (tramp-copy-args nil)
607 (tramp-copy-keep-date nil)
608 (tramp-password-end-of-line "xy") ;see docstring for "xy"
609 (tramp-default-port 22))
610 ("plink1"
611 (tramp-login-program "plink")
612 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
613 ("-1" "-ssh")))
614 (tramp-remote-sh "/bin/sh")
615 (tramp-copy-program nil)
616 (tramp-copy-args nil)
617 (tramp-copy-keep-date nil)
618 (tramp-password-end-of-line "xy") ;see docstring for "xy"
619 (tramp-default-port 22))
620 ("plinkx"
621 (tramp-login-program "plink")
622 ;; ("%h") must be a single element, see
623 ;; `tramp-compute-multi-hops'.
624 (tramp-login-args (("-load") ("%h") ("-t")
625 (,(format
626 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
627 tramp-terminal-type
628 tramp-initial-end-of-output))
629 ("/bin/sh")))
630 (tramp-remote-sh "/bin/sh")
631 (tramp-copy-program nil)
632 (tramp-copy-args nil)
633 (tramp-copy-keep-date nil)
634 (tramp-password-end-of-line nil))
635 ("pscp" (tramp-login-program "plink")
636 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
637 ("-ssh")))
638 (tramp-remote-sh "/bin/sh")
639 (tramp-copy-program "pscp")
640 (tramp-copy-args (("-P" "%p") ("-scp") ("-p" "%k")))
641 (tramp-copy-keep-date t)
642 (tramp-password-end-of-line "xy") ;see docstring for "xy"
643 (tramp-default-port 22))
644 ("psftp" (tramp-login-program "plink")
645 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
646 ("-ssh")))
647 (tramp-remote-sh "/bin/sh")
648 (tramp-copy-program "pscp")
649 (tramp-copy-args (("-P" "%p") ("-sftp") ("-p" "%k")))
650 (tramp-copy-keep-date t)
651 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
652 ("fcp" (tramp-login-program "fsh")
653 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
654 (tramp-remote-sh "/bin/sh -i")
655 (tramp-copy-program "fcp")
656 (tramp-copy-args (("-p" "%k")))
657 (tramp-copy-keep-date t)
658 (tramp-password-end-of-line nil)))
659 "*Alist of methods for remote files.
660 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
661 Each NAME stands for a remote access method. Each PARAM is a
662 pair of the form (KEY VALUE). The following KEYs are defined:
663 * `tramp-remote-sh'
664 This specifies the Bourne shell to use on the remote host. This
665 MUST be a Bourne-like shell. It is normally not necessary to set
666 this to any value other than \"/bin/sh\": Tramp wants to use a shell
667 which groks tilde expansion, but it can search for it. Also note
668 that \"/bin/sh\" exists on all Unixen, this might not be true for
669 the value that you decide to use. You Have Been Warned.
670 * `tramp-login-program'
671 This specifies the name of the program to use for logging in to the
672 remote host. This may be the name of rsh or a workalike program,
673 or the name of telnet or a workalike, or the name of su or a workalike.
674 * `tramp-login-args'
675 This specifies the list of arguments to pass to the above
676 mentioned program. Please note that this is a list of list of arguments,
677 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
678 here. Instead, you want a list (\"-a\" \"-b\"), or (\"-f\" \"foo\").
679 There are some patterns: \"%h\" in this list is replaced by the host
680 name, \"%u\" is replaced by the user name, \"%p\" is replaced by the
681 port number, and \"%%\" can be used to obtain a literal percent character.
682 If a list containing \"%h\", \"%u\" or \"%p\" is unchanged during
683 expansion (i.e. no host or no user specified), this list is not used as
684 argument. By this, arguments like (\"-l\" \"%u\") are optional.
685 \"%t\" is replaced by the temporary file name produced with
686 `tramp-make-tramp-temp-file'. \"%k\" indicates the keep-date
687 parameter of a program, if exists.
688 * `tramp-async-args'
689 When an asynchronous process is started, we know already that
690 the connection works. Therefore, we can pass additional
691 parameters to suppress diagnostic messages, in order not to
692 tamper the process output.
693 * `tramp-copy-program'
694 This specifies the name of the program to use for remotely copying
695 the file; this might be the absolute filename of rcp or the name of
696 a workalike program.
697 * `tramp-copy-args'
698 This specifies the list of parameters to pass to the above mentioned
699 program, the hints for `tramp-login-args' also apply here.
700 * `tramp-copy-keep-date'
701 This specifies whether the copying program when the preserves the
702 timestamp of the original file.
703 * `tramp-copy-keep-tmpfile'
704 This specifies whether a temporary local file shall be kept
705 for optimization reasons (useful for \"rsync\" methods).
706 * `tramp-copy-recursive'
707 Whether the operation copies directories recursively.
708 * `tramp-default-port'
709 The default port of a method is needed in case of gateway connections.
710 Additionally, it is used as indication which method is prepared for
711 passing gateways.
712 * `tramp-gw-args'
713 As the attribute name says, additional arguments are specified here
714 when a method is applied via a gateway.
715 * `tramp-password-end-of-line'
716 This specifies the string to use for terminating the line after
717 submitting the password. If this method parameter is nil, then the
718 value of the normal variable `tramp-default-password-end-of-line'
719 is used. This parameter is necessary because the \"plink\" program
720 requires any two characters after sending the password. These do
721 not have to be newline or carriage return characters. Other login
722 programs are happy with just one character, the newline character.
723 We use \"xy\" as the value for methods using \"plink\".
725 What does all this mean? Well, you should specify `tramp-login-program'
726 for all methods; this program is used to log in to the remote site. Then,
727 there are two ways to actually transfer the files between the local and the
728 remote side. One way is using an additional rcp-like program. If you want
729 to do this, set `tramp-copy-program' in the method.
731 Another possibility for file transfer is inline transfer, i.e. the
732 file is passed through the same buffer used by `tramp-login-program'. In
733 this case, the file contents need to be protected since the
734 `tramp-login-program' might use escape codes or the connection might not
735 be eight-bit clean. Therefore, file contents are encoded for transit.
736 See the variables `tramp-local-coding-commands' and
737 `tramp-remote-coding-commands' for details.
739 So, to summarize: if the method is an out-of-band method, then you
740 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
741 inline method, then these two parameters should be nil. Methods which
742 are fit for gateways must have `tramp-default-port' at least.
744 Notes:
746 When using `su' or `sudo' the phrase `open connection to a remote
747 host' sounds strange, but it is used nevertheless, for consistency.
748 No connection is opened to a remote host, but `su' or `sudo' is
749 started on the local host. You should specify a remote host
750 `localhost' or the name of the local host. Another host name is
751 useful only in combination with `tramp-default-proxies-alist'.")
753 (defun tramp-detect-ssh-controlmaster ()
754 "Call ssh to detect whether it supports the ControlMaster argument.
755 This function may return nil when the argument is supported, but
756 shouldn't return t when it isn't."
757 (ignore-errors
758 (with-temp-buffer
759 (call-process "ssh" nil t nil "-o" "ControlMaster")
760 (goto-char (point-min))
761 (search-forward-regexp "Missing ControlMaster argument" nil t))))
763 (defcustom tramp-default-method
764 ;; An external copy method seems to be preferred, because it is much
765 ;; more performant for large files, and it hasn't too serious delays
766 ;; for small files. But it must be ensured that there aren't
767 ;; permanent password queries. Either a password agent like
768 ;; "ssh-agent" or "Pageant" shall run, or the optional
769 ;; password-cache.el or auth-sources.el packages shall be active for
770 ;; password caching. "scpc" is chosen if we detect that the user is
771 ;; running OpenSSH 4.0 or newer.
772 (cond
773 ;; PuTTY is installed.
774 ((executable-find "pscp")
775 (if (or (fboundp 'password-read)
776 (fboundp 'auth-source-user-or-password)
777 ;; Pageant is running.
778 (tramp-compat-process-running-p "Pageant"))
779 "pscp"
780 "plink"))
781 ;; There is an ssh installation.
782 ((executable-find "scp")
783 (cond
784 ((tramp-detect-ssh-controlmaster) "scpc")
785 ((or (fboundp 'password-read)
786 (fboundp 'auth-source-user-or-password)
787 ;; ssh-agent is running.
788 (getenv "SSH_AUTH_SOCK")
789 (getenv "SSH_AGENT_PID"))
790 "scp")
791 (t "ssh")))
792 ;; Fallback.
793 (t "ftp"))
794 "*Default method to use for transferring files.
795 See `tramp-methods' for possibilities.
796 Also see `tramp-default-method-alist'."
797 :group 'tramp
798 :type 'string)
800 (defcustom tramp-default-method-alist
801 '(("\\`localhost\\'" "\\`root\\'" "su"))
802 "*Default method to use for specific host/user pairs.
803 This is an alist of items (HOST USER METHOD). The first matching item
804 specifies the method to use for a file name which does not specify a
805 method. HOST and USER are regular expressions or nil, which is
806 interpreted as a regular expression which always matches. If no entry
807 matches, the variable `tramp-default-method' takes effect.
809 If the file name does not specify the user, lookup is done using the
810 empty string for the user name.
812 See `tramp-methods' for a list of possibilities for METHOD."
813 :group 'tramp
814 :type '(repeat (list (regexp :tag "Host regexp")
815 (regexp :tag "User regexp")
816 (string :tag "Method"))))
818 (defcustom tramp-default-user
820 "*Default user to use for transferring files.
821 It is nil by default; otherwise settings in configuration files like
822 \"~/.ssh/config\" would be overwritten. Also see `tramp-default-user-alist'.
824 This variable is regarded as obsolete, and will be removed soon."
825 :group 'tramp
826 :type '(choice (const nil) string))
828 (defcustom tramp-default-user-alist
829 `(("\\`su\\(do\\)?\\'" nil "root")
830 ("\\`r\\(em\\)?\\(cp\\|sh\\)\\|telnet\\|plink1?\\'"
831 nil ,(user-login-name)))
832 "*Default user to use for specific method/host pairs.
833 This is an alist of items (METHOD HOST USER). The first matching item
834 specifies the user to use for a file name which does not specify a
835 user. METHOD and USER are regular expressions or nil, which is
836 interpreted as a regular expression which always matches. If no entry
837 matches, the variable `tramp-default-user' takes effect.
839 If the file name does not specify the method, lookup is done using the
840 empty string for the method name."
841 :group 'tramp
842 :type '(repeat (list (regexp :tag "Method regexp")
843 (regexp :tag "Host regexp")
844 (string :tag "User"))))
846 (defcustom tramp-default-host
847 (system-name)
848 "*Default host to use for transferring files.
849 Useful for su and sudo methods mostly."
850 :group 'tramp
851 :type 'string)
853 (defcustom tramp-default-proxies-alist nil
854 "*Route to be followed for specific host/user pairs.
855 This is an alist of items (HOST USER PROXY). The first matching
856 item specifies the proxy to be passed for a file name located on
857 a remote target matching USER@HOST. HOST and USER are regular
858 expressions. PROXY must be a Tramp filename without a localname
859 part. Method and user name on PROXY are optional, which is
860 interpreted with the default values. PROXY can contain the
861 patterns %h and %u, which are replaced by the strings matching
862 HOST or USER, respectively.
864 HOST, USER or PROXY could also be Lisp forms, which will be
865 evaluated. The result must be a string or nil, which is
866 interpreted as a regular expression which always matches."
867 :group 'tramp
868 :type '(repeat (list (choice :tag "Host regexp" regexp sexp)
869 (choice :tag "User regexp" regexp sexp)
870 (choice :tag "Proxy remote name" string (const nil)))))
872 (defconst tramp-local-host-regexp
873 (concat
874 "^" (regexp-opt (list "localhost" (system-name) "127\.0\.0\.1" "::1") t) "$")
875 "*Host names which are regarded as local host.")
877 (defconst tramp-completion-function-alist-rsh
878 '((tramp-parse-rhosts "/etc/hosts.equiv")
879 (tramp-parse-rhosts "~/.rhosts"))
880 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
882 (defconst tramp-completion-function-alist-ssh
883 '((tramp-parse-rhosts "/etc/hosts.equiv")
884 (tramp-parse-rhosts "/etc/shosts.equiv")
885 (tramp-parse-shosts "/etc/ssh_known_hosts")
886 (tramp-parse-sconfig "/etc/ssh_config")
887 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
888 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
889 (tramp-parse-rhosts "~/.rhosts")
890 (tramp-parse-rhosts "~/.shosts")
891 (tramp-parse-shosts "~/.ssh/known_hosts")
892 (tramp-parse-sconfig "~/.ssh/config")
893 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
894 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
895 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
897 (defconst tramp-completion-function-alist-telnet
898 '((tramp-parse-hosts "/etc/hosts"))
899 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
901 (defconst tramp-completion-function-alist-su
902 '((tramp-parse-passwd "/etc/passwd"))
903 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
905 (defconst tramp-completion-function-alist-putty
906 '((tramp-parse-putty
907 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"))
908 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty methods.")
910 (defvar tramp-completion-function-alist nil
911 "*Alist of methods for remote files.
912 This is a list of entries of the form \(NAME PAIR1 PAIR2 ...\).
913 Each NAME stands for a remote access method. Each PAIR is of the form
914 \(FUNCTION FILE\). FUNCTION is responsible to extract user names and host
915 names from FILE for completion. The following predefined FUNCTIONs exists:
917 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
918 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
919 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
920 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
921 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
922 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
923 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
924 * `tramp-parse-netrc' for \"~/.netrc\" like files.
925 * `tramp-parse-putty' for PuTTY registry keys.
927 FUNCTION can also be a customer defined function. For more details see
928 the info pages.")
930 (eval-after-load "tramp"
931 '(progn
932 (tramp-set-completion-function
933 "rcp" tramp-completion-function-alist-rsh)
934 (tramp-set-completion-function
935 "scp" tramp-completion-function-alist-ssh)
936 (tramp-set-completion-function
937 "scp1" tramp-completion-function-alist-ssh)
938 (tramp-set-completion-function
939 "scp2" tramp-completion-function-alist-ssh)
940 (tramp-set-completion-function
941 "scp1_old" tramp-completion-function-alist-ssh)
942 (tramp-set-completion-function
943 "scp2_old" tramp-completion-function-alist-ssh)
944 (tramp-set-completion-function
945 "rsync" tramp-completion-function-alist-ssh)
946 (tramp-set-completion-function
947 "rsyncc" tramp-completion-function-alist-ssh)
948 (tramp-set-completion-function
949 "remcp" tramp-completion-function-alist-rsh)
950 (tramp-set-completion-function
951 "rsh" tramp-completion-function-alist-rsh)
952 (tramp-set-completion-function
953 "ssh" tramp-completion-function-alist-ssh)
954 (tramp-set-completion-function
955 "ssh1" tramp-completion-function-alist-ssh)
956 (tramp-set-completion-function
957 "ssh2" tramp-completion-function-alist-ssh)
958 (tramp-set-completion-function
959 "ssh1_old" tramp-completion-function-alist-ssh)
960 (tramp-set-completion-function
961 "ssh2_old" tramp-completion-function-alist-ssh)
962 (tramp-set-completion-function
963 "remsh" tramp-completion-function-alist-rsh)
964 (tramp-set-completion-function
965 "telnet" tramp-completion-function-alist-telnet)
966 (tramp-set-completion-function
967 "su" tramp-completion-function-alist-su)
968 (tramp-set-completion-function
969 "sudo" tramp-completion-function-alist-su)
970 (tramp-set-completion-function
971 "scpx" tramp-completion-function-alist-ssh)
972 (tramp-set-completion-function
973 "sshx" tramp-completion-function-alist-ssh)
974 (tramp-set-completion-function
975 "krlogin" tramp-completion-function-alist-rsh)
976 (tramp-set-completion-function
977 "plink" tramp-completion-function-alist-ssh)
978 (tramp-set-completion-function
979 "plink1" tramp-completion-function-alist-ssh)
980 (tramp-set-completion-function
981 "plinkx" tramp-completion-function-alist-putty)
982 (tramp-set-completion-function
983 "pscp" tramp-completion-function-alist-ssh)
984 (tramp-set-completion-function
985 "fcp" tramp-completion-function-alist-ssh)))
987 (defconst tramp-echo-mark-marker "_echo"
988 "String marker to surround echoed commands.")
990 (defconst tramp-echo-mark-marker-length (length tramp-echo-mark-marker)
991 "String length of `tramp-echo-mark-marker'.")
993 (defconst tramp-echo-mark
994 (concat tramp-echo-mark-marker
995 (make-string tramp-echo-mark-marker-length ?\b))
996 "String mark to be transmitted around shell commands.
997 Used to separate their echo from the output they produce. This
998 will only be used if we cannot disable remote echo via stty.
999 This string must have no effect on the remote shell except for
1000 producing some echo which can later be detected by
1001 `tramp-echoed-echo-mark-regexp'. Using `tramp-echo-mark-marker',
1002 followed by an equal number of backspaces to erase them will
1003 usually suffice.")
1005 (defconst tramp-echoed-echo-mark-regexp
1006 (format "%s\\(\b\\( \b\\)?\\)\\{%d\\}"
1007 tramp-echo-mark-marker tramp-echo-mark-marker-length)
1008 "Regexp which matches `tramp-echo-mark' as it gets echoed by
1009 the remote shell.")
1011 (defcustom tramp-rsh-end-of-line "\n"
1012 "*String used for end of line in rsh connections.
1013 I don't think this ever needs to be changed, so please tell me about it
1014 if you need to change this.
1015 Also see the method parameter `tramp-password-end-of-line' and the normal
1016 variable `tramp-default-password-end-of-line'."
1017 :group 'tramp
1018 :type 'string)
1020 (defcustom tramp-default-password-end-of-line
1021 tramp-rsh-end-of-line
1022 "*String used for end of line after sending a password.
1023 This variable provides the default value for the method parameter
1024 `tramp-password-end-of-line', see `tramp-methods' for more details.
1026 It seems that people using plink under Windows need to send
1027 \"\\r\\n\" (carriage-return, then newline) after a password, but just
1028 \"\\n\" after all other lines. This variable can be used for the
1029 password, see `tramp-rsh-end-of-line' for the other cases.
1031 The default value is to use the same value as `tramp-rsh-end-of-line'."
1032 :group 'tramp
1033 :type 'string)
1035 ;; "getconf PATH" yields:
1036 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
1037 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
1038 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
1039 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
1040 ;; IRIX64: /usr/bin
1041 (defcustom tramp-remote-path
1042 '(tramp-default-remote-path "/usr/sbin" "/usr/local/bin"
1043 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
1044 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
1045 "*List of directories to search for executables on remote host.
1046 For every remote host, this variable will be set buffer local,
1047 keeping the list of existing directories on that host.
1049 You can use `~' in this list, but when searching for a shell which groks
1050 tilde expansion, all directory names starting with `~' will be ignored.
1052 `Default Directories' represent the list of directories given by
1053 the command \"getconf PATH\". It is recommended to use this
1054 entry on top of this list, because these are the default
1055 directories for POSIX compatible commands.
1057 `Private Directories' are the settings of the $PATH environment,
1058 as given in your `~/.profile'."
1059 :group 'tramp
1060 :type '(repeat (choice
1061 (const :tag "Default Directories" tramp-default-remote-path)
1062 (const :tag "Private Directories" tramp-own-remote-path)
1063 (string :tag "Directory"))))
1065 (defcustom tramp-remote-process-environment
1066 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "LC_ALL=C"
1067 ,(format "TERM=%s" tramp-terminal-type)
1068 "EMACS=t" ;; Deprecated.
1069 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version)
1070 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH="
1071 "autocorrect=" "correct=")
1073 "*List of environment variables to be set on the remote host.
1075 Each element should be a string of the form ENVVARNAME=VALUE. An
1076 entry ENVVARNAME= diables the corresponding environment variable,
1077 which might have been set in the init files like ~/.profile.
1079 Special handling is applied to the PATH environment, which should
1080 not be set here. Instead of, it should be set via `tramp-remote-path'."
1081 :group 'tramp
1082 :type '(repeat string))
1084 (defcustom tramp-login-prompt-regexp
1085 ".*ogin\\( .*\\)?: *"
1086 "*Regexp matching login-like prompts.
1087 The regexp should match at end of buffer.
1089 Sometimes the prompt is reported to look like \"login as:\"."
1090 :group 'tramp
1091 :type 'regexp)
1093 (defcustom tramp-shell-prompt-pattern
1094 ;; Allow a prompt to start right after a ^M since it indeed would be
1095 ;; displayed at the beginning of the line (and Zsh uses it). This
1096 ;; regexp works only for GNU Emacs.
1097 (concat (if (featurep 'xemacs) "" "\\(?:^\\|\r\\)")
1098 "[^#$%>\n]*#?[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*")
1099 "Regexp to match prompts from remote shell.
1100 Normally, Tramp expects you to configure `shell-prompt-pattern'
1101 correctly, but sometimes it happens that you are connecting to a
1102 remote host which sends a different kind of shell prompt. Therefore,
1103 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
1104 and also things matched by this variable. The default value of this
1105 variable is similar to the default value of `shell-prompt-pattern',
1106 which should work well in many cases.
1108 This regexp must match both `tramp-initial-end-of-output' and
1109 `tramp-end-of-output'."
1110 :group 'tramp
1111 :type 'regexp)
1113 (defcustom tramp-password-prompt-regexp
1114 "^.*\\([pP]assword\\|[pP]assphrase\\).*:\^@? *"
1115 "*Regexp matching password-like prompts.
1116 The regexp should match at end of buffer.
1118 The `sudo' program appears to insert a `^@' character into the prompt."
1119 :group 'tramp
1120 :type 'regexp)
1122 (defcustom tramp-wrong-passwd-regexp
1123 (concat "^.*"
1124 ;; These strings should be on the last line
1125 (regexp-opt '("Permission denied"
1126 "Login incorrect"
1127 "Login Incorrect"
1128 "Connection refused"
1129 "Connection closed"
1130 "Timeout, server not responding."
1131 "Sorry, try again."
1132 "Name or service not known"
1133 "Host key verification failed."
1134 "No supported authentication methods left to try!") t)
1135 ".*"
1136 "\\|"
1137 "^.*\\("
1138 ;; Here comes a list of regexes, separated by \\|
1139 "Received signal [0-9]+"
1140 "\\).*")
1141 "*Regexp matching a `login failed' message.
1142 The regexp should match at end of buffer."
1143 :group 'tramp
1144 :type 'regexp)
1146 (defcustom tramp-yesno-prompt-regexp
1147 (concat
1148 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
1149 "\\s-*")
1150 "Regular expression matching all yes/no queries which need to be confirmed.
1151 The confirmation should be done with yes or no.
1152 The regexp should match at end of buffer.
1153 See also `tramp-yn-prompt-regexp'."
1154 :group 'tramp
1155 :type 'regexp)
1157 (defcustom tramp-yn-prompt-regexp
1158 (concat
1159 (regexp-opt '("Store key in cache? (y/n)"
1160 "Update cached key? (y/n, Return cancels connection)") t)
1161 "\\s-*")
1162 "Regular expression matching all y/n queries which need to be confirmed.
1163 The confirmation should be done with y or n.
1164 The regexp should match at end of buffer.
1165 See also `tramp-yesno-prompt-regexp'."
1166 :group 'tramp
1167 :type 'regexp)
1169 (defcustom tramp-terminal-prompt-regexp
1170 (concat "\\("
1171 "TERM = (.*)"
1172 "\\|"
1173 "Terminal type\\? \\[.*\\]"
1174 "\\)\\s-*")
1175 "Regular expression matching all terminal setting prompts.
1176 The regexp should match at end of buffer.
1177 The answer will be provided by `tramp-action-terminal', which see."
1178 :group 'tramp
1179 :type 'regexp)
1181 (defcustom tramp-operation-not-permitted-regexp
1182 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
1183 (regexp-opt '("Operation not permitted") t))
1184 "Regular expression matching keep-date problems in (s)cp operations.
1185 Copying has been performed successfully already, so this message can
1186 be ignored safely."
1187 :group 'tramp
1188 :type 'regexp)
1190 (defcustom tramp-copy-failed-regexp
1191 (concat "\\(.+: "
1192 (regexp-opt '("Permission denied"
1193 "not a regular file"
1194 "is a directory"
1195 "No such file or directory") t)
1196 "\\)\\s-*")
1197 "Regular expression matching copy problems in (s)cp operations."
1198 :group 'tramp
1199 :type 'regexp)
1201 (defcustom tramp-process-alive-regexp
1203 "Regular expression indicating a process has finished.
1204 In fact this expression is empty by intention, it will be used only to
1205 check regularly the status of the associated process.
1206 The answer will be provided by `tramp-action-process-alive',
1207 `tramp-action-out-of-band', which see."
1208 :group 'tramp
1209 :type 'regexp)
1211 (defcustom tramp-temp-name-prefix "tramp."
1212 "*Prefix to use for temporary files.
1213 If this is a relative file name (such as \"tramp.\"), it is considered
1214 relative to the directory name returned by the function
1215 `tramp-compat-temporary-file-directory' (which see). It may also be an
1216 absolute file name; don't forget to include a prefix for the filename
1217 part, though."
1218 :group 'tramp
1219 :type 'string)
1221 (defconst tramp-temp-buffer-name " *tramp temp*"
1222 "Buffer name for a temporary buffer.
1223 It shall be used in combination with `generate-new-buffer-name'.")
1225 (defvar tramp-temp-buffer-file-name nil
1226 "File name of a persistent local temporary file.
1227 Useful for \"rsync\" like methods.")
1228 (make-variable-buffer-local 'tramp-temp-buffer-file-name)
1230 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
1231 "*Alist specifying extra arguments to pass to the remote shell.
1232 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
1233 matching the shell file name and ARGS is a string specifying the
1234 arguments.
1236 This variable is only used when Tramp needs to start up another shell
1237 for tilde expansion. The extra arguments should typically prevent the
1238 shell from reading its init file."
1239 :group 'tramp
1240 ;; This might be the wrong way to test whether the widget type
1241 ;; `alist' is available. Who knows the right way to test it?
1242 :type (if (get 'alist 'widget-type)
1243 '(alist :key-type string :value-type string)
1244 '(repeat (cons string string))))
1246 ;; XEmacs is distributed with few Lisp packages. Further packages are
1247 ;; installed using EFS. If we use a unified filename format, then
1248 ;; Tramp is required in addition to EFS. (But why can't Tramp just
1249 ;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
1250 ;; just like before.) Another reason for using a separate filename
1251 ;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
1252 ;; Tramp only knows how to deal with `file-name-handler-alist', not
1253 ;; the other places.
1255 ;; Currently, we have the choice between 'ftp, 'sep, and 'url.
1256 ;;;###autoload
1257 (defcustom tramp-syntax
1258 (if (featurep 'xemacs) 'sep 'ftp)
1259 "Tramp filename syntax to be used.
1261 It can have the following values:
1263 'ftp -- Ange-FTP respective EFS like syntax (GNU Emacs default)
1264 'sep -- Syntax as defined for XEmacs (not available yet for GNU Emacs)
1265 'url -- URL-like syntax."
1266 :group 'tramp
1267 :type (if (featurep 'xemacs)
1268 '(choice (const :tag "EFS" ftp)
1269 (const :tag "XEmacs" sep)
1270 (const :tag "URL" url))
1271 '(choice (const :tag "Ange-FTP" ftp)
1272 (const :tag "URL" url))))
1274 (defconst tramp-prefix-format
1275 (cond ((equal tramp-syntax 'ftp) "/")
1276 ((equal tramp-syntax 'sep) "/[")
1277 ((equal tramp-syntax 'url) "/")
1278 (t (error "Wrong `tramp-syntax' defined")))
1279 "*String matching the very beginning of Tramp file names.
1280 Used in `tramp-make-tramp-file-name'.")
1282 (defconst tramp-prefix-regexp
1283 (concat "^" (regexp-quote tramp-prefix-format))
1284 "*Regexp matching the very beginning of Tramp file names.
1285 Should always start with \"^\". Derived from `tramp-prefix-format'.")
1287 (defconst tramp-method-regexp
1288 "[a-zA-Z_0-9-]+"
1289 "*Regexp matching methods identifiers.")
1291 (defconst tramp-postfix-method-format
1292 (cond ((equal tramp-syntax 'ftp) ":")
1293 ((equal tramp-syntax 'sep) "/")
1294 ((equal tramp-syntax 'url) "://")
1295 (t (error "Wrong `tramp-syntax' defined")))
1296 "*String matching delimeter between method and user or host names.
1297 Used in `tramp-make-tramp-file-name'.")
1299 (defconst tramp-postfix-method-regexp
1300 (regexp-quote tramp-postfix-method-format)
1301 "*Regexp matching delimeter between method and user or host names.
1302 Derived from `tramp-postfix-method-format'.")
1304 (defconst tramp-user-regexp
1305 "[^:/ \t]+"
1306 "*Regexp matching user names.")
1308 (defconst tramp-prefix-domain-format "%"
1309 "*String matching delimeter between user and domain names.")
1311 (defconst tramp-prefix-domain-regexp
1312 (regexp-quote tramp-prefix-domain-format)
1313 "*Regexp matching delimeter between user and domain names.
1314 Derived from `tramp-prefix-domain-format'.")
1316 (defconst tramp-domain-regexp
1317 "[-a-zA-Z0-9_.]+"
1318 "*Regexp matching domain names.")
1320 (defconst tramp-user-with-domain-regexp
1321 (concat "\\(" tramp-user-regexp "\\)"
1322 tramp-prefix-domain-regexp
1323 "\\(" tramp-domain-regexp "\\)")
1324 "*Regexp matching user names with domain names.")
1326 (defconst tramp-postfix-user-format
1328 "*String matching delimeter between user and host names.
1329 Used in `tramp-make-tramp-file-name'.")
1331 (defconst tramp-postfix-user-regexp
1332 (regexp-quote tramp-postfix-user-format)
1333 "*Regexp matching delimeter between user and host names.
1334 Derived from `tramp-postfix-user-format'.")
1336 (defconst tramp-host-regexp
1337 "[a-zA-Z0-9_.-]+"
1338 "*Regexp matching host names.")
1340 (defconst tramp-prefix-ipv6-format
1341 (cond ((equal tramp-syntax 'ftp) "[")
1342 ((equal tramp-syntax 'sep) "")
1343 ((equal tramp-syntax 'url) "[")
1344 (t (error "Wrong `tramp-syntax' defined")))
1345 "*String matching left hand side of IPv6 addresses.
1346 Used in `tramp-make-tramp-file-name'.")
1348 (defconst tramp-prefix-ipv6-regexp
1349 (regexp-quote tramp-prefix-ipv6-format)
1350 "*Regexp matching left hand side of IPv6 addresses.
1351 Derived from `tramp-prefix-ipv6-format'.")
1353 ;; The following regexp is a bit sloppy. But it shall serve our
1354 ;; purposes. It covers also IPv4 mapped IPv6 addresses, like in
1355 ;; "::ffff:192.168.0.1".
1356 (defconst tramp-ipv6-regexp
1357 "\\(?:\\(?:[a-zA-Z0-9]+\\)?:\\)+[a-zA-Z0-9.]+"
1358 "*Regexp matching IPv6 addresses.")
1360 (defconst tramp-postfix-ipv6-format
1361 (cond ((equal tramp-syntax 'ftp) "]")
1362 ((equal tramp-syntax 'sep) "")
1363 ((equal tramp-syntax 'url) "]")
1364 (t (error "Wrong `tramp-syntax' defined")))
1365 "*String matching right hand side of IPv6 addresses.
1366 Used in `tramp-make-tramp-file-name'.")
1368 (defconst tramp-postfix-ipv6-regexp
1369 (regexp-quote tramp-postfix-ipv6-format)
1370 "*Regexp matching right hand side of IPv6 addresses.
1371 Derived from `tramp-postfix-ipv6-format'.")
1373 (defconst tramp-prefix-port-format
1374 (cond ((equal tramp-syntax 'ftp) "#")
1375 ((equal tramp-syntax 'sep) "#")
1376 ((equal tramp-syntax 'url) ":")
1377 (t (error "Wrong `tramp-syntax' defined")))
1378 "*String matching delimeter between host names and port numbers.")
1380 (defconst tramp-prefix-port-regexp
1381 (regexp-quote tramp-prefix-port-format)
1382 "*Regexp matching delimeter between host names and port numbers.
1383 Derived from `tramp-prefix-port-format'.")
1385 (defconst tramp-port-regexp
1386 "[0-9]+"
1387 "*Regexp matching port numbers.")
1389 (defconst tramp-host-with-port-regexp
1390 (concat "\\(" tramp-host-regexp "\\)"
1391 tramp-prefix-port-regexp
1392 "\\(" tramp-port-regexp "\\)")
1393 "*Regexp matching host names with port numbers.")
1395 (defconst tramp-postfix-host-format
1396 (cond ((equal tramp-syntax 'ftp) ":")
1397 ((equal tramp-syntax 'sep) "]")
1398 ((equal tramp-syntax 'url) "")
1399 (t (error "Wrong `tramp-syntax' defined")))
1400 "*String matching delimeter between host names and localnames.
1401 Used in `tramp-make-tramp-file-name'.")
1403 (defconst tramp-postfix-host-regexp
1404 (regexp-quote tramp-postfix-host-format)
1405 "*Regexp matching delimeter between host names and localnames.
1406 Derived from `tramp-postfix-host-format'.")
1408 (defconst tramp-localname-regexp
1409 ".*$"
1410 "*Regexp matching localnames.")
1412 ;; File name format.
1414 (defconst tramp-file-name-structure
1415 (list
1416 (concat
1417 tramp-prefix-regexp
1418 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp "\\)?"
1419 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
1420 "\\(" "\\(" tramp-host-regexp
1421 "\\|"
1422 tramp-prefix-ipv6-regexp tramp-ipv6-regexp
1423 tramp-postfix-ipv6-regexp "\\)"
1424 "\\(" tramp-prefix-port-regexp tramp-port-regexp "\\)?" "\\)?"
1425 tramp-postfix-host-regexp
1426 "\\(" tramp-localname-regexp "\\)")
1427 2 4 5 8)
1429 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1430 the Tramp file name structure.
1432 The first element REGEXP is a regular expression matching a Tramp file
1433 name. The regex should contain parentheses around the method name,
1434 the user name, the host name, and the file name parts.
1436 The second element METHOD is a number, saying which pair of
1437 parentheses matches the method name. The third element USER is
1438 similar, but for the user name. The fourth element HOST is similar,
1439 but for the host name. The fifth element FILE is for the file name.
1440 These numbers are passed directly to `match-string', which see. That
1441 means the opening parentheses are counted to identify the pair.
1443 See also `tramp-file-name-regexp'.")
1445 ;;;###autoload
1446 (defconst tramp-file-name-regexp-unified
1447 "\\`/\\([^[/:]+\\|[^/]+]\\):"
1448 "Value for `tramp-file-name-regexp' for unified remoting.
1449 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1450 Tramp. See `tramp-file-name-structure' for more explanations.")
1452 ;;;###autoload
1453 (defconst tramp-file-name-regexp-separate
1454 "\\`/\\[.*\\]"
1455 "Value for `tramp-file-name-regexp' for separate remoting.
1456 XEmacs uses a separate filename syntax for Tramp and EFS.
1457 See `tramp-file-name-structure' for more explanations.")
1459 ;;;###autoload
1460 (defconst tramp-file-name-regexp-url
1461 "\\`/[^/:]+://"
1462 "Value for `tramp-file-name-regexp' for URL-like remoting.
1463 See `tramp-file-name-structure' for more explanations.")
1465 ;;;###autoload
1466 (defconst tramp-file-name-regexp
1467 (cond ((equal tramp-syntax 'ftp) tramp-file-name-regexp-unified)
1468 ((equal tramp-syntax 'sep) tramp-file-name-regexp-separate)
1469 ((equal tramp-syntax 'url) tramp-file-name-regexp-url)
1470 (t (error "Wrong `tramp-syntax' defined")))
1471 "*Regular expression matching file names handled by Tramp.
1472 This regexp should match Tramp file names but no other file names.
1473 When tramp.el is loaded, this regular expression is prepended to
1474 `file-name-handler-alist', and that is searched sequentially. Thus,
1475 if the Tramp entry appears rather early in the `file-name-handler-alist'
1476 and is a bit too general, then some files might be considered Tramp
1477 files which are not really Tramp files.
1479 Please note that the entry in `file-name-handler-alist' is made when
1480 this file \(tramp.el\) is loaded. This means that this variable must be set
1481 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1482 updated after changing this variable.
1484 Also see `tramp-file-name-structure'.")
1486 ;;;###autoload
1487 (defconst tramp-root-regexp
1488 (if (memq system-type '(cygwin windows-nt))
1489 "\\`\\([a-zA-Z]:\\)?/"
1490 "\\`/")
1491 "Beginning of an incomplete Tramp file name.
1492 Usually, it is just \"\\\\`/\". On W32 systems, there might be a
1493 volume letter, which will be removed by `tramp-drop-volume-letter'.")
1495 ;;;###autoload
1496 (defconst tramp-completion-file-name-regexp-unified
1497 (concat tramp-root-regexp "[^/]*\\'")
1498 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1499 GNU Emacs uses a unified filename syntax for Tramp and Ange-FTP.
1500 See `tramp-file-name-structure' for more explanations.")
1502 ;;;###autoload
1503 (defconst tramp-completion-file-name-regexp-separate
1504 (concat tramp-root-regexp "\\([[][^]]*\\)?\\'")
1505 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1506 XEmacs uses a separate filename syntax for Tramp and EFS.
1507 See `tramp-file-name-structure' for more explanations.")
1509 ;;;###autoload
1510 (defconst tramp-completion-file-name-regexp-url
1511 (concat tramp-root-regexp "[^/:]+\\(:\\(/\\(/[^/]*\\)?\\)?\\)?\\'")
1512 "Value for `tramp-completion-file-name-regexp' for URL-like remoting.
1513 See `tramp-file-name-structure' for more explanations.")
1515 ;;;###autoload
1516 (defconst tramp-completion-file-name-regexp
1517 (cond ((equal tramp-syntax 'ftp) tramp-completion-file-name-regexp-unified)
1518 ((equal tramp-syntax 'sep) tramp-completion-file-name-regexp-separate)
1519 ((equal tramp-syntax 'url) tramp-completion-file-name-regexp-url)
1520 (t (error "Wrong `tramp-syntax' defined")))
1521 "*Regular expression matching file names handled by Tramp completion.
1522 This regexp should match partial Tramp file names only.
1524 Please note that the entry in `file-name-handler-alist' is made when
1525 this file (tramp.el) is loaded. This means that this variable must be set
1526 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1527 updated after changing this variable.
1529 Also see `tramp-file-name-structure'.")
1531 (defconst tramp-actions-before-shell
1532 '((tramp-login-prompt-regexp tramp-action-login)
1533 (tramp-password-prompt-regexp tramp-action-password)
1534 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1535 (shell-prompt-pattern tramp-action-succeed)
1536 (tramp-shell-prompt-pattern tramp-action-succeed)
1537 (tramp-yesno-prompt-regexp tramp-action-yesno)
1538 (tramp-yn-prompt-regexp tramp-action-yn)
1539 (tramp-terminal-prompt-regexp tramp-action-terminal)
1540 (tramp-process-alive-regexp tramp-action-process-alive))
1541 "List of pattern/action pairs.
1542 Whenever a pattern matches, the corresponding action is performed.
1543 Each item looks like (PATTERN ACTION).
1545 The PATTERN should be a symbol, a variable. The value of this
1546 variable gives the regular expression to search for. Note that the
1547 regexp must match at the end of the buffer, \"\\'\" is implicitly
1548 appended to it.
1550 The ACTION should also be a symbol, but a function. When the
1551 corresponding PATTERN matches, the ACTION function is called.")
1553 (defconst tramp-actions-copy-out-of-band
1554 '((tramp-password-prompt-regexp tramp-action-password)
1555 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1556 (tramp-copy-failed-regexp tramp-action-permission-denied)
1557 (tramp-process-alive-regexp tramp-action-out-of-band))
1558 "List of pattern/action pairs.
1559 This list is used for copying/renaming with out-of-band methods.
1561 See `tramp-actions-before-shell' for more info.")
1563 ;; Chunked sending kludge. We set this to 500 for black-listed constellations
1564 ;; known to have a bug in `process-send-string'; some ssh connections appear
1565 ;; to drop bytes when data is sent too quickly. There is also a connection
1566 ;; buffer local variable, which is computed depending on remote host properties
1567 ;; when `tramp-chunksize' is zero or nil.
1568 (defcustom tramp-chunksize
1569 (when (and (not (featurep 'xemacs))
1570 (memq system-type '(hpux)))
1571 500)
1572 ;; Parentheses in docstring starting at beginning of line are escaped.
1573 ;; Fontification is messed up when
1574 ;; `open-paren-in-column-0-is-defun-start' set to t.
1575 "*If non-nil, chunksize for sending input to local process.
1576 It is necessary only on systems which have a buggy `process-send-string'
1577 implementation. The necessity, whether this variable must be set, can be
1578 checked via the following code:
1580 (with-temp-buffer
1581 (let* ((user \"xxx\") (host \"yyy\")
1582 (init 0) (step 50)
1583 (sent init) (received init))
1584 (while (= sent received)
1585 (setq sent (+ sent step))
1586 (erase-buffer)
1587 (let ((proc (start-process (buffer-name) (current-buffer)
1588 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1589 (when (memq (process-status proc) '(run open))
1590 (process-send-string proc (make-string sent ?\\ ))
1591 (process-send-eof proc)
1592 (process-send-eof proc))
1593 (while (not (progn (goto-char (point-min))
1594 (re-search-forward \"\\\\w+\" (point-max) t)))
1595 (accept-process-output proc 1))
1596 (when (memq (process-status proc) '(run open))
1597 (setq received (string-to-number (match-string 0)))
1598 (delete-process proc)
1599 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1600 (sit-for 0))))
1601 (if (> sent (+ init step))
1602 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1603 (- sent step))
1604 (message \"Test does not work\")
1605 (display-buffer (current-buffer))
1606 (sit-for 30))))
1608 In the Emacs normally running Tramp, evaluate the above code
1609 \(replace \"xxx\" and \"yyy\" by the remote user and host name,
1610 respectively\). You can do this, for example, by pasting it into
1611 the `*scratch*' buffer and then hitting C-j with the cursor after the
1612 last closing parenthesis. Note that it works only if you have configured
1613 \"ssh\" to run without password query, see ssh-agent\(1\).
1615 You will see the number of bytes sent successfully to the remote host.
1616 If that number exceeds 1000, you can stop the execution by hitting
1617 C-g, because your Emacs is likely clean.
1619 When it is necessary to set `tramp-chunksize', you might consider to
1620 use an out-of-the-band method \(like \"scp\"\) instead of an internal one
1621 \(like \"ssh\"\), because setting `tramp-chunksize' to non-nil decreases
1622 performance.
1624 If your Emacs is buggy, the code stops and gives you an indication
1625 about the value `tramp-chunksize' should be set. Maybe you could just
1626 experiment a bit, e.g. changing the values of `init' and `step'
1627 in the third line of the code.
1629 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1630 this variable to be set as well."
1631 :group 'tramp
1632 :type '(choice (const nil) integer))
1634 ;; Logging in to a remote host normally requires obtaining a pty. But
1635 ;; Emacs on MacOS X has process-connection-type set to nil by default,
1636 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1637 ;; for an override of the system default.
1638 (defcustom tramp-process-connection-type t
1639 "Overrides `process-connection-type' for connections from Tramp.
1640 Tramp binds process-connection-type to the value given here before
1641 opening a connection to a remote host."
1642 :group 'tramp
1643 :type '(choice (const nil) (const t) (const pty)))
1645 (defcustom tramp-completion-reread-directory-timeout 10
1646 "Defines seconds since last remote command before rereading a directory.
1647 A remote directory might have changed its contents. In order to
1648 make it visible during file name completion in the minibuffer,
1649 Tramp flushes its cache and rereads the directory contents when
1650 more than `tramp-completion-reread-directory-timeout' seconds
1651 have been gone since last remote command execution. A value of 0
1652 would require an immediate reread during filename completion, nil
1653 means to use always cached values for the directory contents."
1654 :group 'tramp
1655 :type '(choice (const nil) integer))
1657 ;;; Internal Variables:
1659 (defvar tramp-current-method nil
1660 "Connection method for this *tramp* buffer.")
1662 (defvar tramp-current-user nil
1663 "Remote login name for this *tramp* buffer.")
1665 (defvar tramp-current-host nil
1666 "Remote host for this *tramp* buffer.")
1668 (defconst tramp-uudecode
1669 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1670 cat /tmp/tramp.$$
1671 rm -f /tmp/tramp.$$"
1672 "Shell function to implement `uudecode' to standard output.
1673 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
1674 for this or `uudecode -p', but some systems don't, and for them
1675 we have this shell function.")
1677 (defconst tramp-perl-file-truename
1678 "%s -e '
1679 use File::Spec;
1680 use Cwd \"realpath\";
1682 sub recursive {
1683 my ($volume, @dirs) = @_;
1684 my $real = realpath(File::Spec->catpath(
1685 $volume, File::Spec->catdir(@dirs), \"\"));
1686 if ($real) {
1687 my ($vol, $dir) = File::Spec->splitpath($real, 1);
1688 return ($vol, File::Spec->splitdir($dir));
1690 else {
1691 my $last = pop(@dirs);
1692 ($volume, @dirs) = recursive($volume, @dirs);
1693 push(@dirs, $last);
1694 return ($volume, @dirs);
1698 $result = realpath($ARGV[0]);
1699 if (!$result) {
1700 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
1701 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
1703 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
1706 if ($ARGV[0] =~ /\\/$/) {
1707 $result = $result . \"/\";
1710 print \"\\\"$result\\\"\\n\";
1711 ' \"$1\" 2>/dev/null"
1712 "Perl script to produce output suitable for use with `file-truename'
1713 on the remote file system.
1714 Escape sequence %s is replaced with name of Perl binary.
1715 This string is passed to `format', so percent characters need to be doubled.")
1717 (defconst tramp-perl-file-name-all-completions
1718 "%s -e 'sub case {
1719 my $str = shift;
1720 if ($ARGV[2]) {
1721 return lc($str);
1723 else {
1724 return $str;
1727 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
1728 @files = readdir(d); closedir(d);
1729 foreach $f (@files) {
1730 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
1731 if (-d \"$ARGV[0]/$f\") {
1732 print \"$f/\\n\";
1734 else {
1735 print \"$f\\n\";
1739 print \"ok\\n\"
1740 ' \"$1\" \"$2\" \"$3\" 2>/dev/null"
1741 "Perl script to produce output suitable for use with
1742 `file-name-all-completions' on the remote file system. Escape
1743 sequence %s is replaced with name of Perl binary. This string is
1744 passed to `format', so percent characters need to be doubled.")
1746 ;; Perl script to implement `file-attributes' in a Lisp `read'able
1747 ;; output. If you are hacking on this, note that you get *no* output
1748 ;; unless this spits out a complete line, including the '\n' at the
1749 ;; end.
1750 ;; The device number is returned as "-1", because there will be a virtual
1751 ;; device number set in `tramp-handle-file-attributes'.
1752 (defconst tramp-perl-file-attributes
1753 "%s -e '
1754 @stat = lstat($ARGV[0]);
1755 if (!@stat) {
1756 print \"nil\\n\";
1757 exit 0;
1759 if (($stat[2] & 0170000) == 0120000)
1761 $type = readlink($ARGV[0]);
1762 $type = \"\\\"$type\\\"\";
1764 elsif (($stat[2] & 0170000) == 040000)
1766 $type = \"t\";
1768 else
1770 $type = \"nil\"
1772 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1773 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1774 printf(
1775 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
1776 $type,
1777 $stat[3],
1778 $uid,
1779 $gid,
1780 $stat[8] >> 16 & 0xffff,
1781 $stat[8] & 0xffff,
1782 $stat[9] >> 16 & 0xffff,
1783 $stat[9] & 0xffff,
1784 $stat[10] >> 16 & 0xffff,
1785 $stat[10] & 0xffff,
1786 $stat[7],
1787 $stat[2],
1788 $stat[1] >> 16 & 0xffff,
1789 $stat[1] & 0xffff
1790 );' \"$1\" \"$2\" 2>/dev/null"
1791 "Perl script to produce output suitable for use with `file-attributes'
1792 on the remote file system.
1793 Escape sequence %s is replaced with name of Perl binary.
1794 This string is passed to `format', so percent characters need to be doubled.")
1796 (defconst tramp-perl-directory-files-and-attributes
1797 "%s -e '
1798 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
1799 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
1800 @list = readdir(DIR);
1801 closedir(DIR);
1802 $n = scalar(@list);
1803 printf(\"(\\n\");
1804 for($i = 0; $i < $n; $i++)
1806 $filename = $list[$i];
1807 @stat = lstat($filename);
1808 if (($stat[2] & 0170000) == 0120000)
1810 $type = readlink($filename);
1811 $type = \"\\\"$type\\\"\";
1813 elsif (($stat[2] & 0170000) == 040000)
1815 $type = \"t\";
1817 else
1819 $type = \"nil\"
1821 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1822 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1823 printf(
1824 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
1825 $filename,
1826 $type,
1827 $stat[3],
1828 $uid,
1829 $gid,
1830 $stat[8] >> 16 & 0xffff,
1831 $stat[8] & 0xffff,
1832 $stat[9] >> 16 & 0xffff,
1833 $stat[9] & 0xffff,
1834 $stat[10] >> 16 & 0xffff,
1835 $stat[10] & 0xffff,
1836 $stat[7],
1837 $stat[2],
1838 $stat[1] >> 16 & 0xffff,
1839 $stat[1] & 0xffff,
1840 $stat[0] >> 16 & 0xffff,
1841 $stat[0] & 0xffff);
1843 printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
1844 "Perl script implementing `directory-files-attributes' as Lisp `read'able
1845 output.
1846 Escape sequence %s is replaced with name of Perl binary.
1847 This string is passed to `format', so percent characters need to be doubled.")
1849 ;; ;; These two use uu encoding.
1850 ;; (defvar tramp-perl-encode "%s -e'\
1851 ;; print qq(begin 644 xxx\n);
1852 ;; my $s = q();
1853 ;; my $res = q();
1854 ;; while (read(STDIN, $s, 45)) {
1855 ;; print pack(q(u), $s);
1856 ;; }
1857 ;; print qq(`\n);
1858 ;; print qq(end\n);
1859 ;; '"
1860 ;; "Perl program to use for encoding a file.
1861 ;; Escape sequence %s is replaced with name of Perl binary.")
1863 ;; (defvar tramp-perl-decode "%s -ne '
1864 ;; print unpack q(u), $_;
1865 ;; '"
1866 ;; "Perl program to use for decoding a file.
1867 ;; Escape sequence %s is replaced with name of Perl binary.")
1869 ;; These two use base64 encoding.
1870 (defconst tramp-perl-encode-with-module
1871 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
1872 "Perl program to use for encoding a file.
1873 Escape sequence %s is replaced with name of Perl binary.
1874 This string is passed to `format', so percent characters need to be doubled.
1875 This implementation requires the MIME::Base64 Perl module to be installed
1876 on the remote host.")
1878 (defconst tramp-perl-decode-with-module
1879 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
1880 "Perl program to use for decoding a file.
1881 Escape sequence %s is replaced with name of Perl binary.
1882 This string is passed to `format', so percent characters need to be doubled.
1883 This implementation requires the MIME::Base64 Perl module to be installed
1884 on the remote host.")
1886 (defconst tramp-perl-encode
1887 "%s -e '
1888 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1889 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
1890 # Free Software Foundation, Inc.
1891 use strict;
1893 my %%trans = do {
1894 my $i = 0;
1895 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1896 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1899 binmode(\\*STDIN);
1901 # We read in chunks of 54 bytes, to generate output lines
1902 # of 72 chars (plus end of line)
1903 $/ = \\54;
1905 while (my $data = <STDIN>) {
1906 my $pad = q();
1908 # Only for the last chunk, and only if did not fill the last three-byte packet
1909 if (eof) {
1910 my $mod = length($data) %% 3;
1911 $pad = q(=) x (3 - $mod) if $mod;
1914 # Not the fastest method, but it is simple: unpack to binary string, split
1915 # by groups of 6 bits and convert back from binary to byte; then map into
1916 # the translation table
1917 print
1918 join q(),
1919 map($trans{$_},
1920 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1921 $pad,
1922 qq(\\n);
1923 }' 2>/dev/null"
1924 "Perl program to use for encoding a file.
1925 Escape sequence %s is replaced with name of Perl binary.
1926 This string is passed to `format', so percent characters need to be doubled.")
1928 (defconst tramp-perl-decode
1929 "%s -e '
1930 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1931 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
1932 # Free Software Foundation, Inc.
1933 use strict;
1935 my %%trans = do {
1936 my $i = 0;
1937 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1938 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1941 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1943 binmode(\\*STDOUT);
1945 # We are going to accumulate into $pending to accept any line length
1946 # (we do not check they are <= 76 chars as the RFC says)
1947 my $pending = q();
1949 while (my $data = <STDIN>) {
1950 chomp $data;
1952 # If we find one or two =, we have reached the end and
1953 # any following data is to be discarded
1954 my $finished = $data =~ s/(==?).*/$1/;
1955 $pending .= $data;
1957 my $len = length($pending);
1958 my $chunk = substr($pending, 0, $len & ~3);
1959 $pending = substr($pending, $len & ~3 + 1);
1961 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1962 # split in 8-bit chunks and convert back to char.
1963 print join q(),
1964 map $bytes{$_},
1965 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1967 last if $finished;
1968 }' 2>/dev/null"
1969 "Perl program to use for decoding a file.
1970 Escape sequence %s is replaced with name of Perl binary.
1971 This string is passed to `format', so percent characters need to be doubled.")
1973 (defconst tramp-vc-registered-read-file-names
1974 "echo \"(\"
1975 for file in \"$@\"; do
1976 if %s $file; then
1977 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
1978 else
1979 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
1981 if %s $file; then
1982 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
1983 else
1984 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
1986 done
1987 echo \")\""
1988 "Script to check existence of VC related files.
1989 It must be send formatted with two strings; the tests for file
1990 existence, and file readability.")
1992 (defconst tramp-file-mode-type-map
1993 '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1994 (1 . "p") ; fifo
1995 (2 . "c") ; character device
1996 (3 . "m") ; multiplexed character device (v7)
1997 (4 . "d") ; directory
1998 (5 . "?") ; Named special file (XENIX)
1999 (6 . "b") ; block device
2000 (7 . "?") ; multiplexed block device (v7)
2001 (8 . "-") ; regular file
2002 (9 . "n") ; network special file (HP-UX)
2003 (10 . "l") ; symlink
2004 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
2005 (12 . "s") ; socket
2006 (13 . "D") ; door special (Solaris)
2007 (14 . "w")) ; whiteout (BSD)
2008 "A list of file types returned from the `stat' system call.
2009 This is used to map a mode number to a permission string.")
2011 ;; New handlers should be added here. The following operations can be
2012 ;; handled using the normal primitives: file-name-sans-versions,
2013 ;; get-file-buffer.
2014 (defconst tramp-file-name-handler-alist
2015 '((load . tramp-handle-load)
2016 (make-symbolic-link . tramp-handle-make-symbolic-link)
2017 (file-name-as-directory . tramp-handle-file-name-as-directory)
2018 (file-name-directory . tramp-handle-file-name-directory)
2019 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
2020 (file-truename . tramp-handle-file-truename)
2021 (file-exists-p . tramp-handle-file-exists-p)
2022 (file-directory-p . tramp-handle-file-directory-p)
2023 (file-executable-p . tramp-handle-file-executable-p)
2024 (file-readable-p . tramp-handle-file-readable-p)
2025 (file-regular-p . tramp-handle-file-regular-p)
2026 (file-symlink-p . tramp-handle-file-symlink-p)
2027 (file-writable-p . tramp-handle-file-writable-p)
2028 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
2029 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
2030 (file-attributes . tramp-handle-file-attributes)
2031 (file-modes . tramp-handle-file-modes)
2032 (directory-files . tramp-handle-directory-files)
2033 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes)
2034 (file-name-all-completions . tramp-handle-file-name-all-completions)
2035 (file-name-completion . tramp-handle-file-name-completion)
2036 (add-name-to-file . tramp-handle-add-name-to-file)
2037 (copy-file . tramp-handle-copy-file)
2038 (copy-directory . tramp-handle-copy-directory)
2039 (rename-file . tramp-handle-rename-file)
2040 (set-file-modes . tramp-handle-set-file-modes)
2041 (set-file-times . tramp-handle-set-file-times)
2042 (make-directory . tramp-handle-make-directory)
2043 (delete-directory . tramp-handle-delete-directory)
2044 (delete-file . tramp-handle-delete-file)
2045 (directory-file-name . tramp-handle-directory-file-name)
2046 ;; `executable-find' is not official yet.
2047 (executable-find . tramp-handle-executable-find)
2048 (start-file-process . tramp-handle-start-file-process)
2049 (process-file . tramp-handle-process-file)
2050 (shell-command . tramp-handle-shell-command)
2051 (insert-directory . tramp-handle-insert-directory)
2052 (expand-file-name . tramp-handle-expand-file-name)
2053 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
2054 (file-local-copy . tramp-handle-file-local-copy)
2055 (file-remote-p . tramp-handle-file-remote-p)
2056 (insert-file-contents . tramp-handle-insert-file-contents)
2057 (insert-file-contents-literally
2058 . tramp-handle-insert-file-contents-literally)
2059 (write-region . tramp-handle-write-region)
2060 (find-backup-file-name . tramp-handle-find-backup-file-name)
2061 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
2062 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
2063 (dired-compress-file . tramp-handle-dired-compress-file)
2064 (dired-recursive-delete-directory
2065 . tramp-handle-dired-recursive-delete-directory)
2066 (dired-uncache . tramp-handle-dired-uncache)
2067 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
2068 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
2069 (file-selinux-context . tramp-handle-file-selinux-context)
2070 (set-file-selinux-context . tramp-handle-set-file-selinux-context)
2071 (vc-registered . tramp-handle-vc-registered))
2072 "Alist of handler functions.
2073 Operations not mentioned here will be handled by the normal Emacs functions.")
2075 ;; Handlers for partial Tramp file names. For Emacs just
2076 ;; `file-name-all-completions' is needed.
2077 ;;;###autoload
2078 (defconst tramp-completion-file-name-handler-alist
2079 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions)
2080 (file-name-completion . tramp-completion-handle-file-name-completion))
2081 "Alist of completion handler functions.
2082 Used for file names matching `tramp-file-name-regexp'. Operations not
2083 mentioned here will be handled by `tramp-file-name-handler-alist' or the
2084 normal Emacs functions.")
2086 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
2087 (defvar tramp-foreign-file-name-handler-alist
2088 ;; (identity . tramp-sh-file-name-handler) should always be the last
2089 ;; entry, because `identity' always matches.
2090 '((identity . tramp-sh-file-name-handler))
2091 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
2092 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
2093 calling HANDLER.")
2095 ;;; Internal functions which must come first:
2097 (defsubst tramp-debug-message (vec fmt-string &rest args)
2098 "Append message to debug buffer.
2099 Message is formatted with FMT-STRING as control string and the remaining
2100 ARGS to actually emit the message (if applicable)."
2101 (when (get-buffer (tramp-buffer-name vec))
2102 (with-current-buffer (tramp-get-debug-buffer vec)
2103 (goto-char (point-max))
2104 ;; Headline.
2105 (when (bobp)
2106 (insert
2107 (format
2108 ";; %sEmacs: %s Tramp: %s -*- mode: outline; -*-"
2109 (if (featurep 'sxemacs) "SX" (if (featurep 'xemacs) "X" "GNU "))
2110 emacs-version tramp-version)))
2111 (unless (bolp)
2112 (insert "\n"))
2113 ;; Timestamp.
2114 (let ((now (current-time)))
2115 (insert (format-time-string "%T." now))
2116 (insert (format "%06d " (nth 2 now))))
2117 ;; Calling function.
2118 (let ((btn 1) btf fn)
2119 (while (not fn)
2120 (setq btf (nth 1 (backtrace-frame btn)))
2121 (if (not btf)
2122 (setq fn "")
2123 (when (symbolp btf)
2124 (setq fn (symbol-name btf))
2125 (unless (and (string-match "^tramp" fn)
2126 (not (string-match
2127 "^tramp\\(-debug\\)?\\(-message\\|-error\\|-compat-funcall\\)$"
2128 fn)))
2129 (setq fn nil)))
2130 (setq btn (1+ btn))))
2131 ;; The following code inserts filename and line number.
2132 ;; Should be deactivated by default, because it is time
2133 ;; consuming.
2134 ; (let ((ffn (find-function-noselect (intern fn))))
2135 ; (insert
2136 ; (format
2137 ; "%s:%d: "
2138 ; (file-name-nondirectory (buffer-file-name (car ffn)))
2139 ; (with-current-buffer (car ffn)
2140 ; (1+ (count-lines (point-min) (cdr ffn)))))))
2141 (insert (format "%s " fn)))
2142 ;; The message.
2143 (insert (apply 'format fmt-string args)))))
2145 (defvar tramp-message-show-message t
2146 "Show Tramp message in the minibuffer.
2147 This variable is used to disable messages from `tramp-error'.
2148 The messages are visible anyway, because an error is raised.")
2150 (defsubst tramp-message (vec-or-proc level fmt-string &rest args)
2151 "Emit a message depending on verbosity level.
2152 VEC-OR-PROC identifies the Tramp buffer to use. It can be either a
2153 vector or a process. LEVEL says to be quiet if `tramp-verbose' is
2154 less than LEVEL. The message is emitted only if `tramp-verbose' is
2155 greater than or equal to LEVEL.
2157 The message is also logged into the debug buffer when `tramp-verbose'
2158 is greater than or equal 4.
2160 Calls functions `message' and `tramp-debug-message' with FMT-STRING as
2161 control string and the remaining ARGS to actually emit the message (if
2162 applicable)."
2163 (condition-case nil
2164 (when (<= level tramp-verbose)
2165 ;; Match data must be preserved!
2166 (save-match-data
2167 ;; Display only when there is a minimum level.
2168 (when (and tramp-message-show-message (<= level 3))
2169 (apply 'message
2170 (concat
2171 (cond
2172 ((= level 0) "")
2173 ((= level 1) "")
2174 ((= level 2) "Warning: ")
2175 (t "Tramp: "))
2176 fmt-string)
2177 args))
2178 ;; Log only when there is a minimum level.
2179 (when (>= tramp-verbose 4)
2180 (when (and vec-or-proc
2181 (processp vec-or-proc)
2182 (buffer-name (process-buffer vec-or-proc)))
2183 (with-current-buffer (process-buffer vec-or-proc)
2184 ;; Translate proc to vec.
2185 (setq vec-or-proc (tramp-dissect-file-name default-directory))))
2186 (when (and vec-or-proc (vectorp vec-or-proc))
2187 (apply 'tramp-debug-message
2188 vec-or-proc
2189 (concat (format "(%d) # " level) fmt-string)
2190 args)))))
2191 ;; Suppress all errors.
2192 (error nil)))
2194 (defsubst tramp-error (vec-or-proc signal fmt-string &rest args)
2195 "Emit an error.
2196 VEC-OR-PROC identifies the connection to use, SIGNAL is the
2197 signal identifier to be raised, remaining args passed to
2198 `tramp-message'. Finally, signal SIGNAL is raised."
2199 (let (tramp-message-show-message)
2200 (tramp-message
2201 vec-or-proc 1 "%s"
2202 (error-message-string
2203 (list signal
2204 (get signal 'error-message)
2205 (apply 'format fmt-string args))))
2206 (signal signal (list (apply 'format fmt-string args)))))
2208 (defsubst tramp-error-with-buffer
2209 (buffer vec-or-proc signal fmt-string &rest args)
2210 "Emit an error, and show BUFFER.
2211 If BUFFER is nil, show the connection buffer. Wait for 30\", or until
2212 an input event arrives. The other arguments are passed to `tramp-error'."
2213 (save-window-excursion
2214 (unwind-protect
2215 (apply 'tramp-error vec-or-proc signal fmt-string args)
2216 (when (and vec-or-proc
2217 (not (zerop tramp-verbose))
2218 (not (tramp-completion-mode-p)))
2219 (let ((enable-recursive-minibuffers t))
2220 (pop-to-buffer
2221 (or (and (bufferp buffer) buffer)
2222 (and (processp vec-or-proc) (process-buffer vec-or-proc))
2223 (tramp-get-buffer vec-or-proc)))
2224 (sit-for 30))))))
2226 (defmacro with-parsed-tramp-file-name (filename var &rest body)
2227 "Parse a Tramp filename and make components available in the body.
2229 First arg FILENAME is evaluated and dissected into its components.
2230 Second arg VAR is a symbol. It is used as a variable name to hold
2231 the filename structure. It is also used as a prefix for the variables
2232 holding the components. For example, if VAR is the symbol `foo', then
2233 `foo' will be bound to the whole structure, `foo-method' will be bound to
2234 the method component, and so on for `foo-user', `foo-host', `foo-localname'.
2236 Remaining args are Lisp expressions to be evaluated (inside an implicit
2237 `progn').
2239 If VAR is nil, then we bind `v' to the structure and `method', `user',
2240 `host', `localname' to the components."
2241 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
2242 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
2243 (tramp-file-name-method ,(or var 'v)))
2244 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
2245 (tramp-file-name-user ,(or var 'v)))
2246 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
2247 (tramp-file-name-host ,(or var 'v)))
2248 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
2249 (tramp-file-name-localname ,(or var 'v))))
2250 ,@body))
2252 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
2253 (put 'with-parsed-tramp-file-name 'edebug-form-spec '(form symbolp body))
2254 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-parsed-tramp-file-name\\>"))
2256 (defmacro with-file-property (vec file property &rest body)
2257 "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
2258 FILE must be a local file name on a connection identified via VEC."
2259 `(if (file-name-absolute-p ,file)
2260 (let ((value (tramp-get-file-property ,vec ,file ,property 'undef)))
2261 (when (eq value 'undef)
2262 ;; We cannot pass @body as parameter to
2263 ;; `tramp-set-file-property' because it mangles our
2264 ;; debug messages.
2265 (setq value (progn ,@body))
2266 (tramp-set-file-property ,vec ,file ,property value))
2267 value)
2268 ,@body))
2270 (put 'with-file-property 'lisp-indent-function 3)
2271 (put 'with-file-property 'edebug-form-spec t)
2272 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-file-property\\>"))
2274 (defmacro with-connection-property (key property &rest body)
2275 "Check in Tramp for property PROPERTY, otherwise executes BODY and set."
2276 `(let ((value (tramp-get-connection-property ,key ,property 'undef)))
2277 (when (eq value 'undef)
2278 ;; We cannot pass ,@body as parameter to
2279 ;; `tramp-set-connection-property' because it mangles our debug
2280 ;; messages.
2281 (setq value (progn ,@body))
2282 (tramp-set-connection-property ,key ,property value))
2283 value))
2285 (put 'with-connection-property 'lisp-indent-function 2)
2286 (put 'with-connection-property 'edebug-form-spec t)
2287 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-connection-property\\>"))
2289 (defun tramp-progress-reporter-update (reporter &optional value)
2290 (let* ((parameters (cdr reporter))
2291 (message (aref parameters 3)))
2292 (when (string-match message (or (current-message) ""))
2293 (funcall 'progress-reporter-update reporter value))))
2295 (defmacro with-progress-reporter (vec level message &rest body)
2296 "Executes BODY, spinning a progress reporter with MESSAGE.
2297 If LEVEL does not fit for visible messages, or if this is a
2298 nested call of the macro, there are only traces without a visible
2299 progress reporter."
2300 `(let (pr tm)
2301 (tramp-message ,vec ,level "%s..." ,message)
2302 ;; We start a pulsing progress reporter after 3 seconds. Feature
2303 ;; introduced in Emacs 24.1.
2304 (when (and tramp-message-show-message
2305 ;; Display only when there is a minimum level.
2306 (<= ,level (min tramp-verbose 3)))
2307 (condition-case nil
2308 (setq pr (tramp-compat-funcall 'make-progress-reporter ,message)
2309 tm (when pr
2310 (run-at-time 3 0.1 'tramp-progress-reporter-update pr)))
2311 (error nil)))
2312 (unwind-protect
2313 ;; Execute the body. Unset `tramp-message-show-message' when
2314 ;; the timer object is created, in order to suppress
2315 ;; concurrent timers.
2316 (let ((tramp-message-show-message
2317 (and tramp-message-show-message (not tm))))
2318 ,@body)
2319 ;; Stop progress reporter.
2320 (if tm (tramp-compat-funcall 'cancel-timer tm))
2321 (tramp-message ,vec ,level "%s...done" ,message))))
2323 (put 'with-progress-reporter 'lisp-indent-function 3)
2324 (put 'with-progress-reporter 'edebug-form-spec t)
2325 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-progress-reporter\\>"))
2327 (eval-and-compile ;; Silence compiler.
2328 (if (memq system-type '(cygwin windows-nt))
2329 (defun tramp-drop-volume-letter (name)
2330 "Cut off unnecessary drive letter from file NAME.
2331 The function `tramp-handle-expand-file-name' calls `expand-file-name'
2332 locally on a remote file name. When the local system is a W32 system
2333 but the remote system is Unix, this introduces a superfluous drive
2334 letter into the file name. This function removes it."
2335 (save-match-data
2336 (if (string-match tramp-root-regexp name)
2337 (replace-match "/" nil t name)
2338 name)))
2340 (defalias 'tramp-drop-volume-letter 'identity)))
2342 (defsubst tramp-make-tramp-temp-file (vec)
2343 "Create a temporary file on the remote host identified by VEC.
2344 Return the local name of the temporary file."
2345 (let ((prefix
2346 (tramp-make-tramp-file-name
2347 (tramp-file-name-method vec)
2348 (tramp-file-name-user vec)
2349 (tramp-file-name-host vec)
2350 (tramp-drop-volume-letter
2351 (expand-file-name
2352 tramp-temp-name-prefix (tramp-get-remote-tmpdir vec)))))
2353 result)
2354 (while (not result)
2355 ;; `make-temp-file' would be the natural choice for
2356 ;; implementation. But it calls `write-region' internally,
2357 ;; which also needs a temporary file - we would end in an
2358 ;; infinite loop.
2359 (setq result (make-temp-name prefix))
2360 (if (file-exists-p result)
2361 (setq result nil)
2362 ;; This creates the file by side effect.
2363 (set-file-times result)
2364 (set-file-modes result (tramp-octal-to-decimal "0700"))))
2366 ;; Return the local part.
2367 (with-parsed-tramp-file-name result nil localname)))
2370 ;;; Config Manipulation Functions:
2372 (defun tramp-set-completion-function (method function-list)
2373 "Sets the list of completion functions for METHOD.
2374 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2375 The FUNCTION is intended to parse FILE according its syntax.
2376 It might be a predefined FUNCTION, or a user defined FUNCTION.
2377 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
2378 `tramp-parse-sconfig', `tramp-parse-hosts', `tramp-parse-passwd',
2379 and `tramp-parse-netrc'.
2381 Example:
2383 (tramp-set-completion-function
2384 \"ssh\"
2385 '((tramp-parse-sconfig \"/etc/ssh_config\")
2386 (tramp-parse-sconfig \"~/.ssh/config\")))"
2388 (let ((r function-list)
2389 (v function-list))
2390 (setq tramp-completion-function-alist
2391 (delete (assoc method tramp-completion-function-alist)
2392 tramp-completion-function-alist))
2394 (while v
2395 ;; Remove double entries.
2396 (when (member (car v) (cdr v))
2397 (setcdr v (delete (car v) (cdr v))))
2398 ;; Check for function and file or registry key.
2399 (unless (and (functionp (nth 0 (car v)))
2400 (if (string-match "^HKEY_CURRENT_USER" (nth 1 (car v)))
2401 ;; Windows registry.
2402 (and (memq system-type '(cygwin windows-nt))
2403 (zerop
2404 (tramp-local-call-process
2405 "reg" nil nil nil "query" (nth 1 (car v)))))
2406 ;; Configuration file.
2407 (file-exists-p (nth 1 (car v)))))
2408 (setq r (delete (car v) r)))
2409 (setq v (cdr v)))
2411 (when r
2412 (add-to-list 'tramp-completion-function-alist
2413 (cons method r)))))
2415 (defun tramp-get-completion-function (method)
2416 "Returns a list of completion functions for METHOD.
2417 For definition of that list see `tramp-set-completion-function'."
2418 (cons
2419 ;; Hosts visited once shall be remembered.
2420 `(tramp-parse-connection-properties ,method)
2421 ;; The method related defaults.
2422 (cdr (assoc method tramp-completion-function-alist))))
2425 ;;; Fontification of `read-file-name':
2427 ;; rfn-eshadow.el is part of Emacs 22. It is autoloaded.
2428 (defvar tramp-rfn-eshadow-overlay)
2429 (make-variable-buffer-local 'tramp-rfn-eshadow-overlay)
2431 (defun tramp-rfn-eshadow-setup-minibuffer ()
2432 "Set up a minibuffer for `file-name-shadow-mode'.
2433 Adds another overlay hiding filename parts according to Tramp's
2434 special handling of `substitute-in-file-name'."
2435 (when (symbol-value 'minibuffer-completing-file-name)
2436 (setq tramp-rfn-eshadow-overlay
2437 (tramp-compat-funcall
2438 'make-overlay
2439 (tramp-compat-funcall 'minibuffer-prompt-end)
2440 (tramp-compat-funcall 'minibuffer-prompt-end)))
2441 ;; Copy rfn-eshadow-overlay properties.
2442 (let ((props (tramp-compat-funcall
2443 'overlay-properties (symbol-value 'rfn-eshadow-overlay))))
2444 (while props
2445 (tramp-compat-funcall
2446 'overlay-put tramp-rfn-eshadow-overlay (pop props) (pop props))))))
2448 (when (boundp 'rfn-eshadow-setup-minibuffer-hook)
2449 (add-hook 'rfn-eshadow-setup-minibuffer-hook
2450 'tramp-rfn-eshadow-setup-minibuffer)
2451 (add-hook 'tramp-unload-hook
2452 (lambda ()
2453 (remove-hook 'rfn-eshadow-setup-minibuffer-hook
2454 'tramp-rfn-eshadow-setup-minibuffer))))
2456 (defconst tramp-rfn-eshadow-update-overlay-regexp
2457 (format "[^%s/~]*\\(/\\|~\\)" tramp-postfix-host-format))
2459 (defun tramp-rfn-eshadow-update-overlay ()
2460 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
2461 This is intended to be used as a minibuffer `post-command-hook' for
2462 `file-name-shadow-mode'; the minibuffer should have already
2463 been set up by `rfn-eshadow-setup-minibuffer'."
2464 ;; In remote files name, there is a shadowing just for the local part.
2465 (let ((end (or (tramp-compat-funcall
2466 'overlay-end (symbol-value 'rfn-eshadow-overlay))
2467 (tramp-compat-funcall 'minibuffer-prompt-end))))
2468 (when
2469 (file-remote-p
2470 (tramp-compat-funcall 'buffer-substring-no-properties end (point-max)))
2471 (save-excursion
2472 (save-restriction
2473 (narrow-to-region
2474 (1+ (or (string-match
2475 tramp-rfn-eshadow-update-overlay-regexp (buffer-string) end)
2476 end))
2477 (point-max))
2478 (let ((rfn-eshadow-overlay tramp-rfn-eshadow-overlay)
2479 (rfn-eshadow-update-overlay-hook nil))
2480 (tramp-compat-funcall
2481 'move-overlay rfn-eshadow-overlay (point-max) (point-max))
2482 (tramp-compat-funcall 'rfn-eshadow-update-overlay)))))))
2484 (when (boundp 'rfn-eshadow-update-overlay-hook)
2485 (add-hook 'rfn-eshadow-update-overlay-hook
2486 'tramp-rfn-eshadow-update-overlay)
2487 (add-hook 'tramp-unload-hook
2488 (lambda ()
2489 (remove-hook 'rfn-eshadow-update-overlay-hook
2490 'tramp-rfn-eshadow-update-overlay))))
2493 ;;; Integration of eshell.el:
2495 (eval-when-compile
2496 (defvar eshell-path-env))
2498 ;; eshell.el keeps the path in `eshell-path-env'. We must change it
2499 ;; when `default-directory' points to another host.
2500 (defun tramp-eshell-directory-change ()
2501 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
2502 (setq eshell-path-env
2503 (if (file-remote-p default-directory)
2504 (with-parsed-tramp-file-name default-directory nil
2505 (mapconcat
2506 'identity
2507 (tramp-get-remote-path v)
2508 ":"))
2509 (getenv "PATH"))))
2511 (eval-after-load "esh-util"
2512 '(progn
2513 (tramp-eshell-directory-change)
2514 (add-hook 'eshell-directory-change-hook
2515 'tramp-eshell-directory-change)
2516 (add-hook 'tramp-unload-hook
2517 (lambda ()
2518 (remove-hook 'eshell-directory-change-hook
2519 'tramp-eshell-directory-change)))))
2522 ;;; File Name Handler Functions:
2524 (defun tramp-handle-make-symbolic-link
2525 (filename linkname &optional ok-if-already-exists)
2526 "Like `make-symbolic-link' for Tramp files.
2527 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
2528 the symlink. If LINKNAME is a Tramp file, only the localname component is
2529 used as the target of the symlink.
2531 If LINKNAME is a Tramp file and the localname component is relative, then
2532 it is expanded first, before the localname component is taken. Note that
2533 this can give surprising results if the user/host for the source and
2534 target of the symlink differ."
2535 (with-parsed-tramp-file-name linkname l
2536 (let ((ln (tramp-get-remote-ln l))
2537 (cwd (tramp-run-real-handler
2538 'file-name-directory (list l-localname))))
2539 (unless ln
2540 (tramp-error
2541 l 'file-error
2542 "Making a symbolic link. ln(1) does not exist on the remote host."))
2544 ;; Do the 'confirm if exists' thing.
2545 (when (file-exists-p linkname)
2546 ;; What to do?
2547 (if (or (null ok-if-already-exists) ; not allowed to exist
2548 (and (numberp ok-if-already-exists)
2549 (not (yes-or-no-p
2550 (format
2551 "File %s already exists; make it a link anyway? "
2552 l-localname)))))
2553 (tramp-error
2554 l 'file-already-exists "File %s already exists" l-localname)
2555 (delete-file linkname)))
2557 ;; If FILENAME is a Tramp name, use just the localname component.
2558 (when (tramp-tramp-file-p filename)
2559 (setq filename
2560 (tramp-file-name-localname
2561 (tramp-dissect-file-name (expand-file-name filename)))))
2563 ;; Right, they are on the same host, regardless of user, method, etc.
2564 ;; We now make the link on the remote machine. This will occur as the user
2565 ;; that FILENAME belongs to.
2566 (zerop
2567 (tramp-send-command-and-check
2569 (format
2570 "cd %s && %s -sf %s %s"
2571 (tramp-shell-quote-argument cwd)
2573 (tramp-shell-quote-argument filename)
2574 (tramp-shell-quote-argument l-localname))
2575 t)))))
2577 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
2578 "Like `load' for Tramp files."
2579 (with-parsed-tramp-file-name (expand-file-name file) nil
2580 (unless nosuffix
2581 (cond ((file-exists-p (concat file ".elc"))
2582 (setq file (concat file ".elc")))
2583 ((file-exists-p (concat file ".el"))
2584 (setq file (concat file ".el")))))
2585 (when must-suffix
2586 ;; The first condition is always true for absolute file names.
2587 ;; Included for safety's sake.
2588 (unless (or (file-name-directory file)
2589 (string-match "\\.elc?\\'" file))
2590 (tramp-error
2591 v 'file-error
2592 "File `%s' does not include a `.el' or `.elc' suffix" file)))
2593 (unless noerror
2594 (when (not (file-exists-p file))
2595 (tramp-error v 'file-error "Cannot load nonexistent file `%s'" file)))
2596 (if (not (file-exists-p file))
2598 (let ((tramp-message-show-message (not nomessage)))
2599 (with-progress-reporter v 0 (format "Loading %s" file)
2600 (let ((local-copy (file-local-copy file)))
2601 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2602 (unwind-protect
2603 (load local-copy noerror t t)
2604 (delete-file local-copy)))))
2605 t)))
2607 ;; Localname manipulation functions that grok Tramp localnames...
2608 (defun tramp-handle-file-name-as-directory (file)
2609 "Like `file-name-as-directory' but aware of Tramp files."
2610 ;; `file-name-as-directory' would be sufficient except localname is
2611 ;; the empty string.
2612 (let ((v (tramp-dissect-file-name file t)))
2613 ;; Run the command on the localname portion only.
2614 (tramp-make-tramp-file-name
2615 (tramp-file-name-method v)
2616 (tramp-file-name-user v)
2617 (tramp-file-name-host v)
2618 (tramp-run-real-handler
2619 'file-name-as-directory (list (or (tramp-file-name-localname v) ""))))))
2621 (defun tramp-handle-file-name-directory (file)
2622 "Like `file-name-directory' but aware of Tramp files."
2623 ;; Everything except the last filename thing is the directory. We
2624 ;; cannot apply `with-parsed-tramp-file-name', because this expands
2625 ;; the remote file name parts. This is a problem when we are in
2626 ;; file name completion.
2627 (let ((v (tramp-dissect-file-name file t)))
2628 ;; Run the command on the localname portion only.
2629 (tramp-make-tramp-file-name
2630 (tramp-file-name-method v)
2631 (tramp-file-name-user v)
2632 (tramp-file-name-host v)
2633 (tramp-run-real-handler
2634 'file-name-directory (list (or (tramp-file-name-localname v) ""))))))
2636 (defun tramp-handle-file-name-nondirectory (file)
2637 "Like `file-name-nondirectory' but aware of Tramp files."
2638 (with-parsed-tramp-file-name file nil
2639 (tramp-run-real-handler 'file-name-nondirectory (list localname))))
2641 (defun tramp-handle-file-truename (filename &optional counter prev-dirs)
2642 "Like `file-truename' for Tramp files."
2643 (with-parsed-tramp-file-name (expand-file-name filename) nil
2644 (with-file-property v localname "file-truename"
2645 (let ((result nil)) ; result steps in reverse order
2646 (tramp-message v 4 "Finding true name for `%s'" filename)
2647 (cond
2648 ;; Use GNU readlink --canonicalize-missing where available.
2649 ((tramp-get-remote-readlink v)
2650 (setq result
2651 (tramp-send-command-and-read
2653 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
2654 (tramp-get-remote-readlink v)
2655 (tramp-shell-quote-argument localname)))))
2657 ;; Use Perl implementation.
2658 ((and (tramp-get-remote-perl v)
2659 (tramp-get-connection-property v "perl-file-spec" nil)
2660 (tramp-get-connection-property v "perl-cwd-realpath" nil))
2661 (tramp-maybe-send-script
2662 v tramp-perl-file-truename "tramp_perl_file_truename")
2663 (setq result
2664 (tramp-send-command-and-read
2666 (format "tramp_perl_file_truename %s"
2667 (tramp-shell-quote-argument localname)))))
2669 ;; Do it yourself. We bind `directory-sep-char' here for
2670 ;; XEmacs on Windows, which would otherwise use backslash.
2671 (t (let* ((directory-sep-char ?/)
2672 (steps (tramp-compat-split-string localname "/"))
2673 (localnamedir (tramp-run-real-handler
2674 'file-name-as-directory (list localname)))
2675 (is-dir (string= localname localnamedir))
2676 (thisstep nil)
2677 (numchase 0)
2678 ;; Don't make the following value larger than
2679 ;; necessary. People expect an error message in a
2680 ;; timely fashion when something is wrong;
2681 ;; otherwise they might think that Emacs is hung.
2682 ;; Of course, correctness has to come first.
2683 (numchase-limit 20)
2684 symlink-target)
2685 (while (and steps (< numchase numchase-limit))
2686 (setq thisstep (pop steps))
2687 (tramp-message
2688 v 5 "Check %s"
2689 (mapconcat 'identity
2690 (append '("") (reverse result) (list thisstep))
2691 "/"))
2692 (setq symlink-target
2693 (nth 0 (file-attributes
2694 (tramp-make-tramp-file-name
2695 method user host
2696 (mapconcat 'identity
2697 (append '("")
2698 (reverse result)
2699 (list thisstep))
2700 "/")))))
2701 (cond ((string= "." thisstep)
2702 (tramp-message v 5 "Ignoring step `.'"))
2703 ((string= ".." thisstep)
2704 (tramp-message v 5 "Processing step `..'")
2705 (pop result))
2706 ((stringp symlink-target)
2707 ;; It's a symlink, follow it.
2708 (tramp-message v 5 "Follow symlink to %s" symlink-target)
2709 (setq numchase (1+ numchase))
2710 (when (file-name-absolute-p symlink-target)
2711 (setq result nil))
2712 ;; If the symlink was absolute, we'll get a string like
2713 ;; "/user@host:/some/target"; extract the
2714 ;; "/some/target" part from it.
2715 (when (tramp-tramp-file-p symlink-target)
2716 (unless (tramp-equal-remote filename symlink-target)
2717 (tramp-error
2718 v 'file-error
2719 "Symlink target `%s' on wrong host" symlink-target))
2720 (setq symlink-target localname))
2721 (setq steps
2722 (append (tramp-compat-split-string
2723 symlink-target "/")
2724 steps)))
2726 ;; It's a file.
2727 (setq result (cons thisstep result)))))
2728 (when (>= numchase numchase-limit)
2729 (tramp-error
2730 v 'file-error
2731 "Maximum number (%d) of symlinks exceeded" numchase-limit))
2732 (setq result (reverse result))
2733 ;; Combine list to form string.
2734 (setq result
2735 (if result
2736 (mapconcat 'identity (cons "" result) "/")
2737 "/"))
2738 (when (and is-dir (or (string= "" result)
2739 (not (string= (substring result -1) "/"))))
2740 (setq result (concat result "/"))))))
2742 (tramp-message v 4 "True name of `%s' is `%s'" filename result)
2743 (tramp-make-tramp-file-name method user host result)))))
2745 ;; Basic functions.
2747 (defun tramp-handle-file-exists-p (filename)
2748 "Like `file-exists-p' for Tramp files."
2749 (with-parsed-tramp-file-name filename nil
2750 (with-file-property v localname "file-exists-p"
2751 (or (not (null (tramp-get-file-property
2752 v localname "file-attributes-integer" nil)))
2753 (not (null (tramp-get-file-property
2754 v localname "file-attributes-string" nil)))
2755 (zerop (tramp-send-command-and-check
2757 (format
2758 "%s %s"
2759 (tramp-get-file-exists-command v)
2760 (tramp-shell-quote-argument localname))))))))
2762 ;; Inodes don't exist for some file systems. Therefore we must
2763 ;; generate virtual ones. Used in `find-buffer-visiting'. The method
2764 ;; applied might be not so efficient (Ange-FTP uses hashes). But
2765 ;; performance isn't the major issue given that file transfer will
2766 ;; take time.
2767 (defvar tramp-inodes nil
2768 "Keeps virtual inodes numbers.")
2770 ;; Devices must distinguish physical file systems. The device numbers
2771 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2772 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2773 ;; EFS use device number "-1". In order to be different, we use device number
2774 ;; (-1 . x), whereby "x" is unique for a given (method user host).
2775 (defvar tramp-devices nil
2776 "Keeps virtual device numbers.")
2778 ;; CCC: This should check for an error condition and signal failure
2779 ;; when something goes wrong.
2780 ;; Daniel Pittman <daniel@danann.net>
2781 (defun tramp-handle-file-attributes (filename &optional id-format)
2782 "Like `file-attributes' for Tramp files."
2783 (unless id-format (setq id-format 'integer))
2784 ;; Don't modify `last-coding-system-used' by accident.
2785 (let ((last-coding-system-used last-coding-system-used))
2786 (with-parsed-tramp-file-name (expand-file-name filename) nil
2787 (with-file-property v localname (format "file-attributes-%s" id-format)
2788 (save-excursion
2789 (tramp-convert-file-attributes
2791 (cond
2792 ((tramp-get-remote-stat v)
2793 (tramp-do-file-attributes-with-stat v localname id-format))
2794 ((tramp-get-remote-perl v)
2795 (tramp-do-file-attributes-with-perl v localname id-format))
2797 (tramp-do-file-attributes-with-ls v localname id-format)))))))))
2799 (defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
2800 "Implement `file-attributes' for Tramp files using the ls(1) command."
2801 (let (symlinkp dirp
2802 res-inode res-filemodes res-numlinks
2803 res-uid res-gid res-size res-symlink-target)
2804 (tramp-message vec 5 "file attributes with ls: %s" localname)
2805 (tramp-send-command
2807 (format "(%s %s || %s -h %s) && %s %s %s"
2808 (tramp-get-file-exists-command vec)
2809 (tramp-shell-quote-argument localname)
2810 (tramp-get-test-command vec)
2811 (tramp-shell-quote-argument localname)
2812 (tramp-get-ls-command vec)
2813 (if (eq id-format 'integer) "-ildn" "-ild")
2814 (tramp-shell-quote-argument localname)))
2815 ;; parse `ls -l' output ...
2816 (with-current-buffer (tramp-get-buffer vec)
2817 (when (> (buffer-size) 0)
2818 (goto-char (point-min))
2819 ;; ... inode
2820 (setq res-inode
2821 (condition-case err
2822 (read (current-buffer))
2823 (invalid-read-syntax
2824 (when (and (equal (cadr err)
2825 "Integer constant overflow in reader")
2826 (string-match
2827 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2828 (car (cddr err))))
2829 (let* ((big (read (substring (car (cddr err)) 0
2830 (match-beginning 1))))
2831 (small (read (match-string 1 (car (cddr err)))))
2832 (twiddle (/ small 65536)))
2833 (cons (+ big twiddle)
2834 (- small (* twiddle 65536))))))))
2835 ;; ... file mode flags
2836 (setq res-filemodes (symbol-name (read (current-buffer))))
2837 ;; ... number links
2838 (setq res-numlinks (read (current-buffer)))
2839 ;; ... uid and gid
2840 (setq res-uid (read (current-buffer)))
2841 (setq res-gid (read (current-buffer)))
2842 (if (eq id-format 'integer)
2843 (progn
2844 (unless (numberp res-uid) (setq res-uid -1))
2845 (unless (numberp res-gid) (setq res-gid -1)))
2846 (progn
2847 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
2848 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
2849 ;; ... size
2850 (setq res-size (read (current-buffer)))
2851 ;; From the file modes, figure out other stuff.
2852 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2853 (setq dirp (eq ?d (aref res-filemodes 0)))
2854 ;; if symlink, find out file name pointed to
2855 (when symlinkp
2856 (search-forward "-> ")
2857 (setq res-symlink-target
2858 (buffer-substring (point) (tramp-compat-line-end-position))))
2859 ;; return data gathered
2860 (list
2861 ;; 0. t for directory, string (name linked to) for symbolic
2862 ;; link, or nil.
2863 (or dirp res-symlink-target)
2864 ;; 1. Number of links to file.
2865 res-numlinks
2866 ;; 2. File uid.
2867 res-uid
2868 ;; 3. File gid.
2869 res-gid
2870 ;; 4. Last access time, as a list of two integers. First
2871 ;; integer has high-order 16 bits of time, second has low 16
2872 ;; bits.
2873 ;; 5. Last modification time, likewise.
2874 ;; 6. Last status change time, likewise.
2875 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2876 ;; 7. Size in bytes (-1, if number is out of range).
2877 res-size
2878 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2879 res-filemodes
2880 ;; 9. t if file's gid would change if file were deleted and
2881 ;; recreated. Will be set in `tramp-convert-file-attributes'
2883 ;; 10. inode number.
2884 res-inode
2885 ;; 11. Device number. Will be replaced by a virtual device number.
2887 )))))
2889 (defun tramp-do-file-attributes-with-perl
2890 (vec localname &optional id-format)
2891 "Implement `file-attributes' for Tramp files using a Perl script."
2892 (tramp-message vec 5 "file attributes with perl: %s" localname)
2893 (tramp-maybe-send-script
2894 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
2895 (tramp-send-command-and-read
2897 (format "tramp_perl_file_attributes %s %s"
2898 (tramp-shell-quote-argument localname) id-format)))
2900 (defun tramp-do-file-attributes-with-stat
2901 (vec localname &optional id-format)
2902 "Implement `file-attributes' for Tramp files using stat(1) command."
2903 (tramp-message vec 5 "file attributes with stat: %s" localname)
2904 (tramp-send-command-and-read
2906 (format
2907 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
2908 ;; parse correctly the sequence "((". Therefore, we add a space.
2909 "( (%s %s || %s -h %s) && %s -c '( (\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)' %s || echo nil)"
2910 (tramp-get-file-exists-command vec)
2911 (tramp-shell-quote-argument localname)
2912 (tramp-get-test-command vec)
2913 (tramp-shell-quote-argument localname)
2914 (tramp-get-remote-stat vec)
2915 (if (eq id-format 'integer) "%u" "\"%U\"")
2916 (if (eq id-format 'integer) "%g" "\"%G\"")
2917 (tramp-shell-quote-argument localname))))
2919 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
2920 "Like `set-visited-file-modtime' for Tramp files."
2921 (unless (buffer-file-name)
2922 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2923 (buffer-name)))
2924 (if time-list
2925 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
2926 (let ((f (buffer-file-name))
2927 coding-system-used)
2928 (with-parsed-tramp-file-name f nil
2929 (let* ((attr (file-attributes f))
2930 ;; '(-1 65535) means file doesn't exists yet.
2931 (modtime (or (nth 5 attr) '(-1 65535))))
2932 (when (boundp 'last-coding-system-used)
2933 (setq coding-system-used (symbol-value 'last-coding-system-used)))
2934 ;; We use '(0 0) as a don't-know value. See also
2935 ;; `tramp-do-file-attributes-with-ls'.
2936 (if (not (equal modtime '(0 0)))
2937 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
2938 (progn
2939 (tramp-send-command
2941 (format "%s -ild %s"
2942 (tramp-get-ls-command v)
2943 (tramp-shell-quote-argument localname)))
2944 (setq attr (buffer-substring (point)
2945 (progn (end-of-line) (point)))))
2946 (tramp-set-file-property
2947 v localname "visited-file-modtime-ild" attr))
2948 (when (boundp 'last-coding-system-used)
2949 (set 'last-coding-system-used coding-system-used))
2950 nil)))))
2952 ;; This function makes the same assumption as
2953 ;; `tramp-handle-set-visited-file-modtime'.
2954 (defun tramp-handle-verify-visited-file-modtime (buf)
2955 "Like `verify-visited-file-modtime' for Tramp files.
2956 At the time `verify-visited-file-modtime' calls this function, we
2957 already know that the buffer is visiting a file and that
2958 `visited-file-modtime' does not return 0. Do not call this
2959 function directly, unless those two cases are already taken care
2960 of."
2961 (with-current-buffer buf
2962 (let ((f (buffer-file-name)))
2963 ;; There is no file visiting the buffer, or the buffer has no
2964 ;; recorded last modification time, or there is no established
2965 ;; connection.
2966 (if (or (not f)
2967 (eq (visited-file-modtime) 0)
2968 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
2970 (with-parsed-tramp-file-name f nil
2971 (tramp-flush-file-property v localname)
2972 (let* ((attr (file-attributes f))
2973 (modtime (nth 5 attr))
2974 (mt (visited-file-modtime)))
2976 (cond
2977 ;; File exists, and has a known modtime.
2978 ((and attr (not (equal modtime '(0 0))))
2979 (< (abs (tramp-time-diff
2980 modtime
2981 ;; For compatibility, deal with both the old
2982 ;; (HIGH . LOW) and the new (HIGH LOW) return
2983 ;; values of `visited-file-modtime'.
2984 (if (atom (cdr mt))
2985 (list (car mt) (cdr mt))
2986 mt)))
2988 ;; Modtime has the don't know value.
2989 (attr
2990 (tramp-send-command
2992 (format "%s -ild %s"
2993 (tramp-get-ls-command v)
2994 (tramp-shell-quote-argument localname)))
2995 (with-current-buffer (tramp-get-buffer v)
2996 (setq attr (buffer-substring
2997 (point) (progn (end-of-line) (point)))))
2998 (equal
2999 attr
3000 (tramp-get-file-property
3001 v localname "visited-file-modtime-ild" "")))
3002 ;; If file does not exist, say it is not modified if and
3003 ;; only if that agrees with the buffer's record.
3004 (t (equal mt '(-1 65535))))))))))
3006 (defun tramp-handle-set-file-modes (filename mode)
3007 "Like `set-file-modes' for Tramp files."
3008 (with-parsed-tramp-file-name filename nil
3009 (tramp-flush-file-property v localname)
3010 (unless (zerop (tramp-send-command-and-check
3012 (format "chmod %s %s"
3013 (tramp-decimal-to-octal mode)
3014 (tramp-shell-quote-argument localname))))
3015 ;; FIXME: extract the proper text from chmod's stderr.
3016 (tramp-error
3017 v 'file-error "Error while changing file's mode %s" filename))))
3019 (defun tramp-handle-set-file-times (filename &optional time)
3020 "Like `set-file-times' for Tramp files."
3021 (zerop
3022 (if (file-remote-p filename)
3023 (with-parsed-tramp-file-name filename nil
3024 (tramp-flush-file-property v localname)
3025 (let ((time (if (or (null time) (equal time '(0 0)))
3026 (current-time)
3027 time))
3028 ;; With GNU Emacs, `format-time-string' has an optional
3029 ;; parameter UNIVERSAL. This is preferred, because we
3030 ;; could handle the case when the remote host is
3031 ;; located in a different time zone as the local host.
3032 (utc (not (featurep 'xemacs))))
3033 (tramp-send-command-and-check
3034 v (format "%s touch -t %s %s"
3035 (if utc "TZ=UTC; export TZ;" "")
3036 (if utc
3037 (format-time-string "%Y%m%d%H%M.%S" time t)
3038 (format-time-string "%Y%m%d%H%M.%S" time))
3039 (tramp-shell-quote-argument localname)))))
3041 ;; We handle also the local part, because in older Emacsen,
3042 ;; without `set-file-times', this function is an alias for this.
3043 ;; We are local, so we don't need the UTC settings.
3044 (tramp-local-call-process
3045 "touch" nil nil nil "-t"
3046 (format-time-string "%Y%m%d%H%M.%S" time)
3047 (tramp-shell-quote-argument filename)))))
3049 (defun tramp-set-file-uid-gid (filename &optional uid gid)
3050 "Set the ownership for FILENAME.
3051 If UID and GID are provided, these values are used; otherwise uid
3052 and gid of the corresponding user is taken. Both parameters must be integers."
3053 ;; Modern Unices allow chown only for root. So we might need
3054 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
3055 ;; working with su(do)? when it is needed, so it shall succeed in
3056 ;; the majority of cases.
3057 ;; Don't modify `last-coding-system-used' by accident.
3058 (let ((last-coding-system-used last-coding-system-used))
3059 (if (file-remote-p filename)
3060 (with-parsed-tramp-file-name filename nil
3061 (if (and (zerop (user-uid)) (tramp-local-host-p v))
3062 ;; If we are root on the local host, we can do it directly.
3063 (tramp-set-file-uid-gid localname uid gid)
3064 (let ((uid (or (and (integerp uid) uid)
3065 (tramp-get-remote-uid v 'integer)))
3066 (gid (or (and (integerp gid) gid)
3067 (tramp-get-remote-gid v 'integer))))
3068 (tramp-send-command
3069 v (format
3070 "chown %d:%d %s" uid gid
3071 (tramp-shell-quote-argument localname))))))
3073 ;; We handle also the local part, because there doesn't exist
3074 ;; `set-file-uid-gid'. On W32 "chown" might not work.
3075 (let ((uid (or (and (integerp uid) uid) (tramp-get-local-uid 'integer)))
3076 (gid (or (and (integerp gid) gid) (tramp-get-local-gid 'integer))))
3077 (tramp-local-call-process
3078 "chown" nil nil nil
3079 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
3081 (defun tramp-remote-selinux-p (vec)
3082 "Check, whether SELINUX is enabled on the remote host."
3083 (with-connection-property (tramp-get-connection-process vec) "selinux-p"
3084 (let ((result (tramp-find-executable
3085 vec "getenforce" (tramp-get-remote-path vec) t t)))
3086 (and result
3087 (string-equal
3088 (tramp-send-command-and-read
3089 vec (format "echo \\\"`%S`\\\"" result))
3090 "Enforcing")))))
3092 (defun tramp-handle-file-selinux-context (filename)
3093 "Like `file-selinux-context' for Tramp files."
3094 (with-parsed-tramp-file-name filename nil
3095 (with-file-property v localname "file-selinux-context"
3096 (let ((context '(nil nil nil nil))
3097 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
3098 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
3099 (when (and (tramp-remote-selinux-p v)
3100 (zerop (tramp-send-command-and-check
3101 v (format
3102 "%s -d -Z %s"
3103 (tramp-get-ls-command v)
3104 (tramp-shell-quote-argument localname)))))
3105 (with-current-buffer (tramp-get-connection-buffer v)
3106 (goto-char (point-min))
3107 (when (re-search-forward regexp (tramp-compat-line-end-position) t)
3108 (setq context (list (match-string 1) (match-string 2)
3109 (match-string 3) (match-string 4))))))
3110 ;; Return the context.
3111 context))))
3113 (defun tramp-handle-set-file-selinux-context (filename context)
3114 "Like `set-file-selinux-context' for Tramp files."
3115 (with-parsed-tramp-file-name filename nil
3116 (if (and (consp context)
3117 (tramp-remote-selinux-p v)
3118 (zerop (tramp-send-command-and-check
3119 v (format "chcon %s %s %s %s %s"
3120 (if (stringp (nth 0 context))
3121 (format "--user=%s" (nth 0 context)) "")
3122 (if (stringp (nth 1 context))
3123 (format "--role=%s" (nth 1 context)) "")
3124 (if (stringp (nth 2 context))
3125 (format "--type=%s" (nth 2 context)) "")
3126 (if (stringp (nth 3 context))
3127 (format "--range=%s" (nth 3 context)) "")
3128 (tramp-shell-quote-argument localname)))))
3129 (tramp-set-file-property v localname "file-selinux-context" context)
3130 (tramp-set-file-property v localname "file-selinux-context" 'undef)))
3131 ;; We always return nil.
3132 nil)
3134 ;; Simple functions using the `test' command.
3136 (defun tramp-handle-file-executable-p (filename)
3137 "Like `file-executable-p' for Tramp files."
3138 (with-parsed-tramp-file-name filename nil
3139 (with-file-property v localname "file-executable-p"
3140 ;; Examine `file-attributes' cache to see if request can be
3141 ;; satisfied without remote operation.
3142 (or (tramp-check-cached-permissions v ?x)
3143 (zerop (tramp-run-test "-x" filename))))))
3145 (defun tramp-handle-file-readable-p (filename)
3146 "Like `file-readable-p' for Tramp files."
3147 (with-parsed-tramp-file-name filename nil
3148 (with-file-property v localname "file-readable-p"
3149 ;; Examine `file-attributes' cache to see if request can be
3150 ;; satisfied without remote operation.
3151 (or (tramp-check-cached-permissions v ?r)
3152 (zerop (tramp-run-test "-r" filename))))))
3154 ;; When the remote shell is started, it looks for a shell which groks
3155 ;; tilde expansion. Here, we assume that all shells which grok tilde
3156 ;; expansion will also provide a `test' command which groks `-nt' (for
3157 ;; newer than). If this breaks, tell me about it and I'll try to do
3158 ;; something smarter about it.
3159 (defun tramp-handle-file-newer-than-file-p (file1 file2)
3160 "Like `file-newer-than-file-p' for Tramp files."
3161 (cond ((not (file-exists-p file1))
3162 nil)
3163 ((not (file-exists-p file2))
3165 ;; We are sure both files exist at this point.
3167 (save-excursion
3168 ;; We try to get the mtime of both files. If they are not
3169 ;; equal to the "dont-know" value, then we subtract the times
3170 ;; and obtain the result.
3171 (let ((fa1 (file-attributes file1))
3172 (fa2 (file-attributes file2)))
3173 (if (and (not (equal (nth 5 fa1) '(0 0)))
3174 (not (equal (nth 5 fa2) '(0 0))))
3175 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
3176 ;; If one of them is the dont-know value, then we can
3177 ;; still try to run a shell command on the remote host.
3178 ;; However, this only works if both files are Tramp
3179 ;; files and both have the same method, same user, same
3180 ;; host.
3181 (unless (tramp-equal-remote file1 file2)
3182 (with-parsed-tramp-file-name
3183 (if (tramp-tramp-file-p file1) file1 file2) nil
3184 (tramp-error
3185 v 'file-error
3186 "Files %s and %s must have same method, user, host"
3187 file1 file2)))
3188 (with-parsed-tramp-file-name file1 nil
3189 (zerop (tramp-run-test2
3190 (tramp-get-test-nt-command v) file1 file2)))))))))
3192 ;; Functions implemented using the basic functions above.
3194 (defun tramp-handle-file-modes (filename)
3195 "Like `file-modes' for Tramp files."
3196 (let ((truename (or (file-truename filename) filename)))
3197 (when (file-exists-p truename)
3198 (tramp-mode-string-to-int (nth 8 (file-attributes truename))))))
3200 (defun tramp-default-file-modes (filename)
3201 "Return file modes of FILENAME as integer.
3202 If the file modes of FILENAME cannot be determined, return the
3203 value of `default-file-modes', without execute permissions."
3204 (or (file-modes filename)
3205 (logand (default-file-modes) (tramp-octal-to-decimal "0666"))))
3207 (defun tramp-handle-file-directory-p (filename)
3208 "Like `file-directory-p' for Tramp files."
3209 ;; Care must be taken that this function returns `t' for symlinks
3210 ;; pointing to directories. Surely the most obvious implementation
3211 ;; would be `test -d', but that returns false for such symlinks.
3212 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
3213 ;; I now think he's right. So we could be using `test -d', couldn't
3214 ;; we?
3216 ;; Alternatives: `cd %s', `test -d %s'
3217 (with-parsed-tramp-file-name filename nil
3218 (with-file-property v localname "file-directory-p"
3219 (zerop (tramp-run-test "-d" filename)))))
3221 (defun tramp-handle-file-regular-p (filename)
3222 "Like `file-regular-p' for Tramp files."
3223 (and (file-exists-p filename)
3224 (eq ?- (aref (nth 8 (file-attributes filename)) 0))))
3226 (defun tramp-handle-file-symlink-p (filename)
3227 "Like `file-symlink-p' for Tramp files."
3228 (with-parsed-tramp-file-name filename nil
3229 (let ((x (car (file-attributes filename))))
3230 (when (stringp x)
3231 ;; When Tramp is running on VMS, then `file-name-absolute-p'
3232 ;; might do weird things.
3233 (if (file-name-absolute-p x)
3234 (tramp-make-tramp-file-name method user host x)
3235 x)))))
3237 (defun tramp-handle-file-writable-p (filename)
3238 "Like `file-writable-p' for Tramp files."
3239 (with-parsed-tramp-file-name filename nil
3240 (with-file-property v localname "file-writable-p"
3241 (if (file-exists-p filename)
3242 ;; Examine `file-attributes' cache to see if request can be
3243 ;; satisfied without remote operation.
3244 (or (tramp-check-cached-permissions v ?w)
3245 (zerop (tramp-run-test "-w" filename)))
3246 ;; If file doesn't exist, check if directory is writable.
3247 (and (zerop (tramp-run-test
3248 "-d" (file-name-directory filename)))
3249 (zerop (tramp-run-test
3250 "-w" (file-name-directory filename))))))))
3252 (defun tramp-handle-file-ownership-preserved-p (filename)
3253 "Like `file-ownership-preserved-p' for Tramp files."
3254 (with-parsed-tramp-file-name filename nil
3255 (with-file-property v localname "file-ownership-preserved-p"
3256 (let ((attributes (file-attributes filename)))
3257 ;; Return t if the file doesn't exist, since it's true that no
3258 ;; information would be lost by an (attempted) delete and create.
3259 (or (null attributes)
3260 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer)))))))
3262 ;; Other file name ops.
3264 (defun tramp-handle-directory-file-name (directory)
3265 "Like `directory-file-name' for Tramp files."
3266 ;; If localname component of filename is "/", leave it unchanged.
3267 ;; Otherwise, remove any trailing slash from localname component.
3268 ;; Method, host, etc, are unchanged. Does it make sense to try
3269 ;; to avoid parsing the filename?
3270 (with-parsed-tramp-file-name directory nil
3271 (if (and (not (zerop (length localname)))
3272 (eq (aref localname (1- (length localname))) ?/)
3273 (not (string= localname "/")))
3274 (substring directory 0 -1)
3275 directory)))
3277 ;; Directory listings.
3279 (defun tramp-handle-directory-files
3280 (directory &optional full match nosort files-only)
3281 "Like `directory-files' for Tramp files."
3282 ;; FILES-ONLY is valid for XEmacs only.
3283 (when (file-directory-p directory)
3284 (setq directory (file-name-as-directory (expand-file-name directory)))
3285 (let ((temp (nreverse (file-name-all-completions "" directory)))
3286 result item)
3288 (while temp
3289 (setq item (directory-file-name (pop temp)))
3290 (when (and (or (null match) (string-match match item))
3291 (or (null files-only)
3292 ;; Files only.
3293 (and (equal files-only t) (file-regular-p item))
3294 ;; Directories only.
3295 (file-directory-p item)))
3296 (push (if full (concat directory item) item)
3297 result)))
3298 (if nosort result (sort result 'string<)))))
3300 (defun tramp-handle-directory-files-and-attributes
3301 (directory &optional full match nosort id-format)
3302 "Like `directory-files-and-attributes' for Tramp files."
3303 (unless id-format (setq id-format 'integer))
3304 (when (file-directory-p directory)
3305 (setq directory (expand-file-name directory))
3306 (let* ((temp
3307 (copy-tree
3308 (with-parsed-tramp-file-name directory nil
3309 (with-file-property
3310 v localname
3311 (format "directory-files-and-attributes-%s" id-format)
3312 (save-excursion
3313 (mapcar
3314 (lambda (x)
3315 (cons (car x)
3316 (tramp-convert-file-attributes v (cdr x))))
3317 (cond
3318 ((tramp-get-remote-stat v)
3319 (tramp-do-directory-files-and-attributes-with-stat
3320 v localname id-format))
3321 ((tramp-get-remote-perl v)
3322 (tramp-do-directory-files-and-attributes-with-perl
3323 v localname id-format)))))))))
3324 result item)
3326 (while temp
3327 (setq item (pop temp))
3328 (when (or (null match) (string-match match (car item)))
3329 (when full
3330 (setcar item (expand-file-name (car item) directory)))
3331 (push item result)))
3333 (if nosort
3334 result
3335 (sort result (lambda (x y) (string< (car x) (car y))))))))
3337 (defun tramp-do-directory-files-and-attributes-with-perl
3338 (vec localname &optional id-format)
3339 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
3340 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
3341 (tramp-maybe-send-script
3342 vec tramp-perl-directory-files-and-attributes
3343 "tramp_perl_directory_files_and_attributes")
3344 (let ((object
3345 (tramp-send-command-and-read
3347 (format "tramp_perl_directory_files_and_attributes %s %s"
3348 (tramp-shell-quote-argument localname) id-format))))
3349 (when (stringp object) (tramp-error vec 'file-error object))
3350 object))
3352 (defun tramp-do-directory-files-and-attributes-with-stat
3353 (vec localname &optional id-format)
3354 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
3355 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
3356 (tramp-send-command-and-read
3358 (format
3359 (concat
3360 ;; We must care about filenames with spaces, or starting with
3361 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
3362 ;; but it does not work on all remote systems. Therefore, we
3363 ;; quote the filenames via sed.
3364 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | xargs "
3365 "%s -c '(\"%%n\" (\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)'); "
3366 "echo \")\"")
3367 (tramp-shell-quote-argument localname)
3368 (tramp-get-ls-command vec)
3369 (tramp-get-remote-stat vec)
3370 (if (eq id-format 'integer) "%u" "\"%U\"")
3371 (if (eq id-format 'integer) "%g" "\"%G\""))))
3373 ;; This function should return "foo/" for directories and "bar" for
3374 ;; files.
3375 (defun tramp-handle-file-name-all-completions (filename directory)
3376 "Like `file-name-all-completions' for Tramp files."
3377 (unless (save-match-data (string-match "/" filename))
3378 (with-parsed-tramp-file-name (expand-file-name directory) nil
3380 (all-completions
3381 filename
3382 (mapcar
3383 'list
3385 ;; Try cache first
3386 (and
3387 ;; Ignore if expired
3388 (or (not (integerp tramp-completion-reread-directory-timeout))
3389 (<= (tramp-time-diff
3390 (current-time)
3391 (tramp-get-file-property
3392 v localname "last-completion" '(0 0 0)))
3393 tramp-completion-reread-directory-timeout))
3395 ;; Try cache entries for filename, filename with last
3396 ;; character removed, filename with last two characters
3397 ;; removed, ..., and finally the empty string - all
3398 ;; concatenated to the local directory name
3400 ;; This is inefficient for very long filenames, pity
3401 ;; `reduce' is not available...
3402 (car
3403 (apply
3404 'append
3405 (mapcar
3406 (lambda (x)
3407 (let ((cache-hit
3408 (tramp-get-file-property
3410 (concat localname (substring filename 0 x))
3411 "file-name-all-completions"
3412 nil)))
3413 (when cache-hit (list cache-hit))))
3414 (tramp-compat-number-sequence (length filename) 0 -1)))))
3416 ;; Cache expired or no matching cache entry found so we need
3417 ;; to perform a remote operation
3418 (let (result)
3419 ;; Get a list of directories and files, including reliably
3420 ;; tagging the directories with a trailing '/'. Because I
3421 ;; rock. --daniel@danann.net
3423 ;; Changed to perform `cd' in the same remote op and only
3424 ;; get entries starting with `filename'. Capture any `cd'
3425 ;; error messages. Ensure any `cd' and `echo' aliases are
3426 ;; ignored.
3427 (tramp-send-command
3429 (if (tramp-get-remote-perl v)
3430 (progn
3431 (tramp-maybe-send-script
3432 v tramp-perl-file-name-all-completions
3433 "tramp_perl_file_name_all_completions")
3434 (format "tramp_perl_file_name_all_completions %s %s %d"
3435 (tramp-shell-quote-argument localname)
3436 (tramp-shell-quote-argument filename)
3437 (if (symbol-value
3438 ;; `read-file-name-completion-ignore-case'
3439 ;; is introduced with Emacs 22.1.
3440 (if (boundp
3441 'read-file-name-completion-ignore-case)
3442 'read-file-name-completion-ignore-case
3443 'completion-ignore-case))
3444 1 0)))
3446 (format (concat
3447 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
3448 ;; `ls' with wildcard might fail with `Argument
3449 ;; list too long' error in some corner cases; if
3450 ;; `ls' fails after `cd' succeeded, chances are
3451 ;; that's the case, so let's retry without
3452 ;; wildcard. This will return "too many" entries
3453 ;; but that isn't harmful.
3454 " || %s -a 2>/dev/null)"
3455 " | while read f; do"
3456 " if %s -d \"$f\" 2>/dev/null;"
3457 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
3458 " && \\echo ok) || \\echo fail")
3459 (tramp-shell-quote-argument localname)
3460 (tramp-get-ls-command v)
3461 ;; When `filename' is empty, just `ls' without
3462 ;; filename argument is more efficient than `ls *'
3463 ;; for very large directories and might avoid the
3464 ;; `Argument list too long' error.
3466 ;; With and only with wildcard, we need to add
3467 ;; `-d' to prevent `ls' from descending into
3468 ;; sub-directories.
3469 (if (zerop (length filename))
3471 (concat (tramp-shell-quote-argument filename) "* -d"))
3472 (tramp-get-ls-command v)
3473 (tramp-get-test-command v))))
3475 ;; Now grab the output.
3476 (with-current-buffer (tramp-get-buffer v)
3477 (goto-char (point-max))
3479 ;; Check result code, found in last line of output
3480 (forward-line -1)
3481 (if (looking-at "^fail$")
3482 (progn
3483 ;; Grab error message from line before last line
3484 ;; (it was put there by `cd 2>&1')
3485 (forward-line -1)
3486 (tramp-error
3487 v 'file-error
3488 "tramp-handle-file-name-all-completions: %s"
3489 (buffer-substring
3490 (point) (tramp-compat-line-end-position))))
3491 ;; For peace of mind, if buffer doesn't end in `fail'
3492 ;; then it should end in `ok'. If neither are in the
3493 ;; buffer something went seriously wrong on the remote
3494 ;; side.
3495 (unless (looking-at "^ok$")
3496 (tramp-error
3497 v 'file-error
3499 tramp-handle-file-name-all-completions: internal error accessing `%s': `%s'"
3500 (tramp-shell-quote-argument localname) (buffer-string))))
3502 (while (zerop (forward-line -1))
3503 (push (buffer-substring
3504 (point) (tramp-compat-line-end-position))
3505 result)))
3507 ;; Because the remote op went through OK we know the
3508 ;; directory we `cd'-ed to exists
3509 (tramp-set-file-property
3510 v localname "file-exists-p" t)
3512 ;; Because the remote op went through OK we know every
3513 ;; file listed by `ls' exists.
3514 (mapc (lambda (entry)
3515 (tramp-set-file-property
3516 v (concat localname entry) "file-exists-p" t))
3517 result)
3519 (tramp-set-file-property
3520 v localname "last-completion" (current-time))
3522 ;; Store result in the cache
3523 (tramp-set-file-property
3524 v (concat localname filename)
3525 "file-name-all-completions"
3526 result))))))))
3528 (defun tramp-handle-file-name-completion
3529 (filename directory &optional predicate)
3530 "Like `file-name-completion' for Tramp files."
3531 (unless (tramp-tramp-file-p directory)
3532 (error
3533 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
3534 directory))
3535 (try-completion
3536 filename
3537 (mapcar 'list (file-name-all-completions filename directory))
3538 (when predicate
3539 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
3541 ;; cp, mv and ln
3543 (defun tramp-handle-add-name-to-file
3544 (filename newname &optional ok-if-already-exists)
3545 "Like `add-name-to-file' for Tramp files."
3546 (unless (tramp-equal-remote filename newname)
3547 (with-parsed-tramp-file-name
3548 (if (tramp-tramp-file-p filename) filename newname) nil
3549 (tramp-error
3550 v 'file-error
3551 "add-name-to-file: %s"
3552 "only implemented for same method, same user, same host")))
3553 (with-parsed-tramp-file-name filename v1
3554 (with-parsed-tramp-file-name newname v2
3555 (let ((ln (when v1 (tramp-get-remote-ln v1))))
3556 (when (and (not ok-if-already-exists)
3557 (file-exists-p newname)
3558 (not (numberp ok-if-already-exists))
3559 (y-or-n-p
3560 (format
3561 "File %s already exists; make it a new name anyway? "
3562 newname)))
3563 (tramp-error
3564 v2 'file-error
3565 "add-name-to-file: file %s already exists" newname))
3566 (tramp-flush-file-property v2 (file-name-directory v2-localname))
3567 (tramp-flush-file-property v2 v2-localname)
3568 (tramp-barf-unless-okay
3570 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
3571 (tramp-shell-quote-argument v2-localname))
3572 "error with add-name-to-file, see buffer `%s' for details"
3573 (buffer-name))))))
3575 (defun tramp-handle-copy-file
3576 (filename newname &optional ok-if-already-exists keep-date
3577 preserve-uid-gid preserve-selinux-context)
3578 "Like `copy-file' for Tramp files."
3579 (setq filename (expand-file-name filename))
3580 (setq newname (expand-file-name newname))
3581 (cond
3582 ;; At least one file a Tramp file?
3583 ((or (tramp-tramp-file-p filename)
3584 (tramp-tramp-file-p newname))
3585 (tramp-do-copy-or-rename-file
3586 'copy filename newname ok-if-already-exists keep-date
3587 preserve-uid-gid preserve-selinux-context))
3588 ;; Compat section.
3589 (preserve-selinux-context
3590 (tramp-run-real-handler
3591 'copy-file
3592 (list filename newname ok-if-already-exists keep-date
3593 preserve-uid-gid preserve-selinux-context)))
3594 (preserve-uid-gid
3595 (tramp-run-real-handler
3596 'copy-file
3597 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
3599 (tramp-run-real-handler
3600 'copy-file (list filename newname ok-if-already-exists keep-date)))))
3602 (defun tramp-handle-copy-directory (dirname newname &optional keep-date parents)
3603 "Like `copy-directory' for Tramp files."
3604 (let ((t1 (tramp-tramp-file-p dirname))
3605 (t2 (tramp-tramp-file-p newname)))
3606 (with-parsed-tramp-file-name (if t1 dirname newname) nil
3607 (if (and (tramp-get-method-parameter method 'tramp-copy-recursive)
3608 ;; When DIRNAME and NEWNAME are remote, they must have
3609 ;; the same method.
3610 (or (null t1) (null t2)
3611 (string-equal
3612 (tramp-file-name-method (tramp-dissect-file-name dirname))
3613 (tramp-file-name-method (tramp-dissect-file-name newname)))))
3614 ;; scp or rsync DTRT.
3615 (progn
3616 (setq dirname (directory-file-name (expand-file-name dirname))
3617 newname (directory-file-name (expand-file-name newname)))
3618 (if (and (file-directory-p newname)
3619 (not (string-equal (file-name-nondirectory dirname)
3620 (file-name-nondirectory newname))))
3621 (setq newname
3622 (expand-file-name
3623 (file-name-nondirectory dirname) newname)))
3624 (if (not (file-directory-p (file-name-directory newname)))
3625 (make-directory (file-name-directory newname) parents))
3626 (tramp-do-copy-or-rename-file-out-of-band
3627 'copy dirname newname keep-date))
3628 ;; We must do it file-wise.
3629 (tramp-run-real-handler
3630 'copy-directory (list dirname newname keep-date parents)))
3632 ;; When newname did exist, we have wrong cached values.
3633 (when t2
3634 (with-parsed-tramp-file-name newname nil
3635 (tramp-flush-file-property v (file-name-directory localname))
3636 (tramp-flush-file-property v localname))))))
3638 (defun tramp-handle-rename-file
3639 (filename newname &optional ok-if-already-exists)
3640 "Like `rename-file' for Tramp files."
3641 ;; Check if both files are local -- invoke normal rename-file.
3642 ;; Otherwise, use Tramp from local system.
3643 (setq filename (expand-file-name filename))
3644 (setq newname (expand-file-name newname))
3645 ;; At least one file a Tramp file?
3646 (if (or (tramp-tramp-file-p filename)
3647 (tramp-tramp-file-p newname))
3648 (tramp-do-copy-or-rename-file
3649 'rename filename newname ok-if-already-exists t t)
3650 (tramp-run-real-handler
3651 'rename-file (list filename newname ok-if-already-exists))))
3653 (defun tramp-do-copy-or-rename-file
3654 (op filename newname &optional ok-if-already-exists keep-date
3655 preserve-uid-gid preserve-selinux-context)
3656 "Copy or rename a remote file.
3657 OP must be `copy' or `rename' and indicates the operation to perform.
3658 FILENAME specifies the file to copy or rename, NEWNAME is the name of
3659 the new file (for copy) or the new name of the file (for rename).
3660 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
3661 KEEP-DATE means to make sure that NEWNAME has the same timestamp
3662 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3663 the uid and gid if both files are on the same host.
3664 PRESERVE-SELINUX-CONTEXT activates selinux commands.
3666 This function is invoked by `tramp-handle-copy-file' and
3667 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
3668 and `rename'. FILENAME and NEWNAME must be absolute file names."
3669 (unless (memq op '(copy rename))
3670 (error "Unknown operation `%s', must be `copy' or `rename'" op))
3671 (let ((t1 (tramp-tramp-file-p filename))
3672 (t2 (tramp-tramp-file-p newname))
3673 (context (and preserve-selinux-context
3674 (apply 'file-selinux-context (list filename))))
3675 pr tm)
3677 (with-parsed-tramp-file-name (if t1 filename newname) nil
3678 (when (and (not ok-if-already-exists) (file-exists-p newname))
3679 (tramp-error
3680 v 'file-already-exists "File %s already exists" newname))
3682 (with-progress-reporter
3683 v 0 (format "%s %s to %s"
3684 (if (eq op 'copy) "Copying" "Renaming")
3685 filename newname)
3687 (cond
3688 ;; Both are Tramp files.
3689 ((and t1 t2)
3690 (with-parsed-tramp-file-name filename v1
3691 (with-parsed-tramp-file-name newname v2
3692 (cond
3693 ;; Shortcut: if method, host, user are the same for
3694 ;; both files, we invoke `cp' or `mv' on the remote
3695 ;; host directly.
3696 ((tramp-equal-remote filename newname)
3697 (tramp-do-copy-or-rename-file-directly
3698 op filename newname
3699 ok-if-already-exists keep-date preserve-uid-gid))
3701 ;; Try out-of-band operation.
3702 ((tramp-method-out-of-band-p
3703 v1 (nth 7 (file-attributes filename)))
3704 (tramp-do-copy-or-rename-file-out-of-band
3705 op filename newname keep-date))
3707 ;; No shortcut was possible. So we copy the file
3708 ;; first. If the operation was `rename', we go back
3709 ;; and delete the original file (if the copy was
3710 ;; successful). The approach is simple-minded: we
3711 ;; create a new buffer, insert the contents of the
3712 ;; source file into it, then write out the buffer to
3713 ;; the target file. The advantage is that it doesn't
3714 ;; matter which filename handlers are used for the
3715 ;; source and target file.
3717 (tramp-do-copy-or-rename-file-via-buffer
3718 op filename newname keep-date))))))
3720 ;; One file is a Tramp file, the other one is local.
3721 ((or t1 t2)
3722 (cond
3723 ;; Fast track on local machine.
3724 ((tramp-local-host-p v)
3725 (tramp-do-copy-or-rename-file-directly
3726 op filename newname
3727 ok-if-already-exists keep-date preserve-uid-gid))
3729 ;; If the Tramp file has an out-of-band method, the
3730 ;; corresponding copy-program can be invoked.
3731 ((tramp-method-out-of-band-p v (nth 7 (file-attributes filename)))
3732 (tramp-do-copy-or-rename-file-out-of-band
3733 op filename newname keep-date))
3735 ;; Use the inline method via a Tramp buffer.
3736 (t (tramp-do-copy-or-rename-file-via-buffer
3737 op filename newname keep-date))))
3740 ;; One of them must be a Tramp file.
3741 (error "Tramp implementation says this cannot happen")))
3743 ;; Handle `preserve-selinux-context'.
3744 (when context (apply 'set-file-selinux-context (list newname context)))
3746 ;; In case of `rename', we must flush the cache of the source file.
3747 (when (and t1 (eq op 'rename))
3748 (with-parsed-tramp-file-name filename v1
3749 (tramp-flush-file-property v1 (file-name-directory localname))
3750 (tramp-flush-file-property v1 localname)))
3752 ;; When newname did exist, we have wrong cached values.
3753 (when t2
3754 (with-parsed-tramp-file-name newname v2
3755 (tramp-flush-file-property v2 (file-name-directory localname))
3756 (tramp-flush-file-property v2 localname)))))))
3758 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
3759 "Use an Emacs buffer to copy or rename a file.
3760 First arg OP is either `copy' or `rename' and indicates the operation.
3761 FILENAME is the source file, NEWNAME the target file.
3762 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
3763 (with-temp-buffer
3764 ;; We must disable multibyte, because binary data shall not be
3765 ;; converted.
3766 (set-buffer-multibyte nil)
3767 (let ((coding-system-for-read 'binary)
3768 (jka-compr-inhibit t))
3769 (insert-file-contents-literally filename))
3770 ;; We don't want the target file to be compressed, so we let-bind
3771 ;; `jka-compr-inhibit' to t.
3772 (let ((coding-system-for-write 'binary)
3773 (jka-compr-inhibit t))
3774 (write-region (point-min) (point-max) newname)))
3775 ;; KEEP-DATE handling.
3776 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
3777 ;; Set the mode.
3778 (set-file-modes newname (tramp-default-file-modes filename))
3779 ;; If the operation was `rename', delete the original file.
3780 (unless (eq op 'copy) (delete-file filename)))
3782 (defun tramp-do-copy-or-rename-file-directly
3783 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
3784 "Invokes `cp' or `mv' on the remote system.
3785 OP must be one of `copy' or `rename', indicating `cp' or `mv',
3786 respectively. FILENAME specifies the file to copy or rename,
3787 NEWNAME is the name of the new file (for copy) or the new name of
3788 the file (for rename). Both files must reside on the same host.
3789 KEEP-DATE means to make sure that NEWNAME has the same timestamp
3790 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3791 the uid and gid from FILENAME."
3792 (let ((t1 (tramp-tramp-file-p filename))
3793 (t2 (tramp-tramp-file-p newname))
3794 (file-times (nth 5 (file-attributes filename)))
3795 (file-modes (tramp-default-file-modes filename)))
3796 (with-parsed-tramp-file-name (if t1 filename newname) nil
3797 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
3798 ((eq op 'copy) "cp -f")
3799 ((eq op 'rename) "mv -f")
3800 (t (tramp-error
3801 v 'file-error
3802 "Unknown operation `%s', must be `copy' or `rename'"
3803 op))))
3804 (localname1
3805 (if t1
3806 (tramp-file-name-handler 'file-remote-p filename 'localname)
3807 filename))
3808 (localname2
3809 (if t2
3810 (tramp-file-name-handler 'file-remote-p newname 'localname)
3811 newname))
3812 (prefix (file-remote-p (if t1 filename newname)))
3813 cmd-result)
3815 (cond
3816 ;; Both files are on a remote host, with same user.
3817 ((and t1 t2)
3818 (setq cmd-result
3819 (tramp-send-command-and-check
3821 (format "%s %s %s" cmd
3822 (tramp-shell-quote-argument localname1)
3823 (tramp-shell-quote-argument localname2))))
3824 (with-current-buffer (tramp-get-buffer v)
3825 (goto-char (point-min))
3826 (unless
3828 (and keep-date
3829 ;; Mask cp -f error.
3830 (re-search-forward
3831 tramp-operation-not-permitted-regexp nil t))
3832 (zerop cmd-result))
3833 (tramp-error-with-buffer
3834 nil v 'file-error
3835 "Copying directly failed, see buffer `%s' for details."
3836 (buffer-name)))))
3838 ;; We are on the local host.
3839 ((or t1 t2)
3840 (cond
3841 ;; We can do it directly.
3842 ((let (file-name-handler-alist)
3843 (and (file-readable-p localname1)
3844 (file-writable-p (file-name-directory localname2))
3845 (or (file-directory-p localname2)
3846 (file-writable-p localname2))))
3847 (if (eq op 'copy)
3848 (tramp-compat-copy-file
3849 localname1 localname2 ok-if-already-exists
3850 keep-date preserve-uid-gid)
3851 (tramp-run-real-handler
3852 'rename-file (list localname1 localname2 ok-if-already-exists))))
3854 ;; We can do it directly with `tramp-send-command'
3855 ((and (file-readable-p (concat prefix localname1))
3856 (file-writable-p
3857 (file-name-directory (concat prefix localname2)))
3858 (or (file-directory-p (concat prefix localname2))
3859 (file-writable-p (concat prefix localname2))))
3860 (tramp-do-copy-or-rename-file-directly
3861 op (concat prefix localname1) (concat prefix localname2)
3862 ok-if-already-exists keep-date t)
3863 ;; We must change the ownership to the local user.
3864 (tramp-set-file-uid-gid
3865 (concat prefix localname2)
3866 (tramp-get-local-uid 'integer)
3867 (tramp-get-local-gid 'integer)))
3869 ;; We need a temporary file in between.
3871 ;; Create the temporary file.
3872 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
3873 (unwind-protect
3874 (progn
3875 (cond
3878 (zerop
3879 (tramp-send-command-and-check
3880 v (format
3881 "%s %s %s" cmd
3882 (tramp-shell-quote-argument localname1)
3883 (tramp-shell-quote-argument tmpfile))))
3884 (tramp-error-with-buffer
3885 nil v 'file-error
3886 "Copying directly failed, see buffer `%s' for details."
3887 (tramp-get-buffer v)))
3888 ;; We must change the ownership as remote user.
3889 ;; Since this does not work reliable, we also
3890 ;; give read permissions.
3891 (set-file-modes
3892 (concat prefix tmpfile) (tramp-octal-to-decimal "0777"))
3893 (tramp-set-file-uid-gid
3894 (concat prefix tmpfile)
3895 (tramp-get-local-uid 'integer)
3896 (tramp-get-local-gid 'integer)))
3898 (if (eq op 'copy)
3899 (tramp-compat-copy-file
3900 localname1 tmpfile t
3901 keep-date preserve-uid-gid)
3902 (tramp-run-real-handler
3903 'rename-file
3904 (list localname1 tmpfile t)))
3905 ;; We must change the ownership as local user.
3906 ;; Since this does not work reliable, we also
3907 ;; give read permissions.
3908 (set-file-modes tmpfile (tramp-octal-to-decimal "0777"))
3909 (tramp-set-file-uid-gid
3910 tmpfile
3911 (tramp-get-remote-uid v 'integer)
3912 (tramp-get-remote-gid v 'integer))))
3914 ;; Move the temporary file to its destination.
3915 (cond
3918 (zerop
3919 (tramp-send-command-and-check
3920 v (format
3921 "cp -f -p %s %s"
3922 (tramp-shell-quote-argument tmpfile)
3923 (tramp-shell-quote-argument localname2))))
3924 (tramp-error-with-buffer
3925 nil v 'file-error
3926 "Copying directly failed, see buffer `%s' for details."
3927 (tramp-get-buffer v))))
3929 (tramp-run-real-handler
3930 'rename-file
3931 (list tmpfile localname2 ok-if-already-exists)))))
3933 ;; Save exit.
3934 (condition-case nil
3935 (delete-file tmpfile)
3936 (error)))))))))
3938 ;; Set the time and mode. Mask possible errors.
3939 (condition-case nil
3940 (when keep-date
3941 (set-file-times newname file-times)
3942 (set-file-modes newname file-modes))
3943 (error)))))
3945 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
3946 "Invoke rcp program to copy.
3947 The method used must be an out-of-band method."
3948 (let ((t1 (tramp-tramp-file-p filename))
3949 (t2 (tramp-tramp-file-p newname))
3950 copy-program copy-args copy-env copy-keep-date port spec
3951 source target)
3953 (with-parsed-tramp-file-name (if t1 filename newname) nil
3954 (if (and t1 t2)
3956 ;; Both are Tramp files. We shall optimize it, when the
3957 ;; methods for filename and newname are the same.
3958 (let* ((dir-flag (file-directory-p filename))
3959 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
3960 (if dir-flag
3961 (setq tmpfile
3962 (expand-file-name
3963 (file-name-nondirectory newname) tmpfile)))
3964 (unwind-protect
3965 (progn
3966 (tramp-do-copy-or-rename-file-out-of-band
3967 op filename tmpfile keep-date)
3968 (tramp-do-copy-or-rename-file-out-of-band
3969 'rename tmpfile newname keep-date))
3970 ;; Save exit.
3971 (condition-case nil
3972 (if dir-flag
3973 (tramp-compat-delete-directory
3974 (expand-file-name ".." tmpfile) 'recursive)
3975 (delete-file tmpfile))
3976 (error))))
3978 ;; Expand hops. Might be necessary for gateway methods.
3979 (setq v (car (tramp-compute-multi-hops v)))
3980 (aset v 3 localname)
3982 ;; Check which ones of source and target are Tramp files.
3983 (setq source (if t1 (tramp-make-copy-program-file-name v) filename)
3984 target (funcall
3985 (if (and (file-directory-p filename)
3986 (string-equal
3987 (file-name-nondirectory filename)
3988 (file-name-nondirectory newname)))
3989 'file-name-directory
3990 'identity)
3991 (if t2 (tramp-make-copy-program-file-name v) newname)))
3993 ;; Check for port number. Until now, there's no need for handling
3994 ;; like method, user, host.
3995 (setq host (tramp-file-name-real-host v)
3996 port (tramp-file-name-port v)
3997 port (or (and port (number-to-string port)) ""))
3999 ;; Compose copy command.
4000 (setq spec (format-spec-make
4001 ?h host ?u user ?p port
4002 ?t (tramp-get-connection-property
4003 (tramp-get-connection-process v) "temp-file" "")
4004 ?k (if keep-date " " ""))
4005 copy-program (tramp-get-method-parameter
4006 method 'tramp-copy-program)
4007 copy-keep-date (tramp-get-method-parameter
4008 method 'tramp-copy-keep-date)
4009 copy-args
4010 (delq
4012 (mapcar
4013 (lambda (x)
4014 (setq
4016 ;; " " is indication for keep-date argument.
4017 (delete " " (mapcar (lambda (y) (format-spec y spec)) x)))
4018 (unless (member "" x) (mapconcat 'identity x " ")))
4019 (tramp-get-method-parameter method 'tramp-copy-args)))
4020 copy-env
4021 (delq
4023 (mapcar
4024 (lambda (x)
4025 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
4026 (unless (member "" x) (mapconcat 'identity x " ")))
4027 (tramp-get-method-parameter method 'tramp-copy-env))))
4029 ;; Check for program.
4030 (when (and (fboundp 'executable-find)
4031 (not (let ((default-directory
4032 (tramp-compat-temporary-file-directory)))
4033 (executable-find copy-program))))
4034 (tramp-error
4035 v 'file-error "Cannot find copy program: %s" copy-program))
4037 ;; Set variables for computing the prompt for reading
4038 ;; password.
4039 (setq tramp-current-method (tramp-file-name-method v)
4040 tramp-current-user (tramp-file-name-user v)
4041 tramp-current-host (tramp-file-name-host v))
4043 (unwind-protect
4044 (with-temp-buffer
4045 ;; The default directory must be remote.
4046 (let ((default-directory
4047 (file-name-directory (if t1 filename newname)))
4048 (process-environment (copy-sequence process-environment)))
4049 ;; Set the transfer process properties.
4050 (tramp-set-connection-property
4051 v "process-name" (buffer-name (current-buffer)))
4052 (tramp-set-connection-property
4053 v "process-buffer" (current-buffer))
4054 (while copy-env
4055 (tramp-message v 5 "%s=\"%s\"" (car copy-env) (cadr copy-env))
4056 (setenv (pop copy-env) (pop copy-env)))
4058 ;; Use an asynchronous process. By this, password can
4059 ;; be handled. The default directory must be local, in
4060 ;; order to apply the correct `copy-program'. We don't
4061 ;; set a timeout, because the copying of large files can
4062 ;; last longer than 60 secs.
4063 (let ((p (let ((default-directory
4064 (tramp-compat-temporary-file-directory)))
4065 (apply 'start-process
4066 (tramp-get-connection-property
4067 v "process-name" nil)
4068 (tramp-get-connection-property
4069 v "process-buffer" nil)
4070 copy-program
4071 (append copy-args (list source target))))))
4072 (tramp-message
4073 v 6 "%s" (mapconcat 'identity (process-command p) " "))
4074 (tramp-set-process-query-on-exit-flag p nil)
4075 (tramp-process-actions p v tramp-actions-copy-out-of-band))))
4077 ;; Reset the transfer process properties.
4078 (tramp-set-connection-property v "process-name" nil)
4079 (tramp-set-connection-property v "process-buffer" nil))
4081 ;; Handle KEEP-DATE argument.
4082 (when (and keep-date (not copy-keep-date))
4083 (set-file-times newname (nth 5 (file-attributes filename))))
4085 ;; Set the mode.
4086 (unless (and keep-date copy-keep-date)
4087 (ignore-errors
4088 (set-file-modes newname (tramp-default-file-modes filename)))))
4090 ;; If the operation was `rename', delete the original file.
4091 (unless (eq op 'copy)
4092 (if (file-regular-p filename)
4093 (delete-file filename)
4094 (tramp-compat-delete-directory filename 'recursive))))))
4096 (defun tramp-handle-make-directory (dir &optional parents)
4097 "Like `make-directory' for Tramp files."
4098 (setq dir (expand-file-name dir))
4099 (with-parsed-tramp-file-name dir nil
4100 (tramp-flush-directory-property v (file-name-directory localname))
4101 (save-excursion
4102 (tramp-barf-unless-okay
4104 (format "%s %s"
4105 (if parents "mkdir -p" "mkdir")
4106 (tramp-shell-quote-argument localname))
4107 "Couldn't make directory %s" dir))))
4109 (defun tramp-handle-delete-directory (directory &optional recursive)
4110 "Like `delete-directory' for Tramp files."
4111 (setq directory (expand-file-name directory))
4112 (with-parsed-tramp-file-name directory nil
4113 (tramp-flush-file-property v (file-name-directory localname))
4114 (tramp-flush-directory-property v localname)
4115 (unless (zerop (tramp-send-command-and-check
4117 (format
4118 "%s %s"
4119 (if recursive "rm -rf" "rmdir")
4120 (tramp-shell-quote-argument localname))))
4121 (tramp-error v 'file-error "Couldn't delete %s" directory))))
4123 (defun tramp-handle-delete-file (filename &optional trash)
4124 "Like `delete-file' for Tramp files."
4125 (setq filename (expand-file-name filename))
4126 (with-parsed-tramp-file-name filename nil
4127 (tramp-flush-file-property v (file-name-directory localname))
4128 (tramp-flush-file-property v localname)
4129 (unless
4130 (zerop
4131 (tramp-send-command-and-check
4132 v (format "%s %s"
4133 (or (and trash (tramp-get-remote-trash v)) "rm -f")
4134 (tramp-shell-quote-argument localname))))
4135 (tramp-error v 'file-error "Couldn't delete %s" filename))))
4137 ;; Dired.
4139 ;; CCC: This does not seem to be enough. Something dies when
4140 ;; we try and delete two directories under Tramp :/
4141 (defun tramp-handle-dired-recursive-delete-directory (filename)
4142 "Recursively delete the directory given.
4143 This is like `dired-recursive-delete-directory' for Tramp files."
4144 (with-parsed-tramp-file-name filename nil
4145 ;; Run a shell command 'rm -r <localname>'
4146 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
4147 (unless (file-exists-p filename)
4148 (tramp-error v 'file-error "No such directory: %s" filename))
4149 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
4150 (tramp-send-command
4152 (format "rm -rf %s" (tramp-shell-quote-argument localname))
4153 ;; Don't read the output, do it explicitely.
4154 nil t)
4155 ;; Wait for the remote system to return to us...
4156 ;; This might take a while, allow it plenty of time.
4157 (tramp-wait-for-output (tramp-get-connection-process v) 120)
4158 ;; Make sure that it worked...
4159 (tramp-flush-file-property v (file-name-directory localname))
4160 (tramp-flush-directory-property v localname)
4161 (and (file-exists-p filename)
4162 (tramp-error
4163 v 'file-error "Failed to recursively delete %s" filename))))
4165 (defun tramp-handle-dired-compress-file (file &rest ok-flag)
4166 "Like `dired-compress-file' for Tramp files."
4167 ;; OK-FLAG is valid for XEmacs only, but not implemented.
4168 ;; Code stolen mainly from dired-aux.el.
4169 (with-parsed-tramp-file-name file nil
4170 (tramp-flush-file-property v localname)
4171 (save-excursion
4172 (let ((suffixes
4173 (if (not (featurep 'xemacs))
4174 ;; Emacs case
4175 (symbol-value 'dired-compress-file-suffixes)
4176 ;; XEmacs has `dired-compression-method-alist', which is
4177 ;; transformed into `dired-compress-file-suffixes' structure.
4178 (mapcar
4179 (lambda (x)
4180 (list (concat (regexp-quote (nth 1 x)) "\\'")
4182 (mapconcat 'identity (nth 3 x) " ")))
4183 (symbol-value 'dired-compression-method-alist))))
4184 suffix)
4185 ;; See if any suffix rule matches this file name.
4186 (while suffixes
4187 (let (case-fold-search)
4188 (if (string-match (car (car suffixes)) localname)
4189 (setq suffix (car suffixes) suffixes nil))
4190 (setq suffixes (cdr suffixes))))
4192 (cond ((file-symlink-p file)
4193 nil)
4194 ((and suffix (nth 2 suffix))
4195 ;; We found an uncompression rule.
4196 (with-progress-reporter v 0 (format "Uncompressing %s" file)
4197 (when (zerop
4198 (tramp-send-command-and-check
4199 v (concat (nth 2 suffix) " "
4200 (tramp-shell-quote-argument localname))))
4201 ;; `dired-remove-file' is not defined in XEmacs.
4202 (tramp-compat-funcall 'dired-remove-file file)
4203 (string-match (car suffix) file)
4204 (concat (substring file 0 (match-beginning 0))))))
4206 ;; We don't recognize the file as compressed, so compress it.
4207 ;; Try gzip.
4208 (with-progress-reporter v 0 (format "Compressing %s" file)
4209 (when (zerop
4210 (tramp-send-command-and-check
4211 v (concat "gzip -f "
4212 (tramp-shell-quote-argument localname))))
4213 ;; `dired-remove-file' is not defined in XEmacs.
4214 (tramp-compat-funcall 'dired-remove-file file)
4215 (cond ((file-exists-p (concat file ".gz"))
4216 (concat file ".gz"))
4217 ((file-exists-p (concat file ".z"))
4218 (concat file ".z"))
4219 (t nil))))))))))
4221 (defun tramp-handle-dired-uncache (dir &optional dir-p)
4222 "Like `dired-uncache' for Tramp files."
4223 ;; DIR-P is valid for XEmacs only.
4224 (with-parsed-tramp-file-name
4225 (if (or dir-p (file-directory-p dir)) dir (file-name-directory dir)) nil
4226 (tramp-flush-file-property v localname)))
4228 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
4229 ;; not sure at all that this is the right way to do it, but let's hope
4230 ;; it works for now, and wait for a guru to point out the Right Way to
4231 ;; achieve this.
4232 ;;(eval-when-compile
4233 ;; (unless (fboundp 'dired-insert-set-properties)
4234 ;; (fset 'dired-insert-set-properties 'ignore)))
4235 ;; Gerd suggests this:
4236 (eval-when-compile (require 'dired))
4237 ;; Note that dired is required at run-time, too, when it is needed.
4238 ;; It is only needed on XEmacs for the function
4239 ;; `dired-insert-set-properties'.
4241 (defun tramp-handle-insert-directory
4242 (filename switches &optional wildcard full-directory-p)
4243 "Like `insert-directory' for Tramp files."
4244 (setq filename (expand-file-name filename))
4245 (with-parsed-tramp-file-name filename nil
4246 (if (and (featurep 'ls-lisp)
4247 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
4248 (tramp-run-real-handler
4249 'insert-directory (list filename switches wildcard full-directory-p))
4250 (when (stringp switches)
4251 (setq switches (split-string switches)))
4252 (when (and (member "--dired" switches)
4253 (not (tramp-get-ls-command-with-dired v)))
4254 (setq switches (delete "--dired" switches)))
4255 (when wildcard
4256 (setq wildcard (tramp-run-real-handler
4257 'file-name-nondirectory (list localname)))
4258 (setq localname (tramp-run-real-handler
4259 'file-name-directory (list localname))))
4260 (unless full-directory-p
4261 (setq switches (add-to-list 'switches "-d" 'append)))
4262 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
4263 (when wildcard
4264 (setq switches (concat switches " " wildcard)))
4265 (tramp-message
4266 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
4267 switches filename (if wildcard "yes" "no")
4268 (if full-directory-p "yes" "no"))
4269 ;; If `full-directory-p', we just say `ls -l FILENAME'.
4270 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
4271 (if full-directory-p
4272 (tramp-send-command
4274 (format "%s %s %s 2>/dev/null"
4275 (tramp-get-ls-command v)
4276 switches
4277 (if wildcard
4278 localname
4279 (tramp-shell-quote-argument (concat localname ".")))))
4280 (tramp-barf-unless-okay
4282 (format "cd %s" (tramp-shell-quote-argument
4283 (tramp-run-real-handler
4284 'file-name-directory (list localname))))
4285 "Couldn't `cd %s'"
4286 (tramp-shell-quote-argument
4287 (tramp-run-real-handler 'file-name-directory (list localname))))
4288 (tramp-send-command
4290 (format "%s %s %s"
4291 (tramp-get-ls-command v)
4292 switches
4293 (if (or wildcard
4294 (zerop (length
4295 (tramp-run-real-handler
4296 'file-name-nondirectory (list localname)))))
4298 (tramp-shell-quote-argument
4299 (tramp-run-real-handler
4300 'file-name-nondirectory (list localname)))))))
4301 (let ((beg (point)))
4302 ;; We cannot use `insert-buffer-substring' because the Tramp
4303 ;; buffer changes its contents before insertion due to calling
4304 ;; `expand-file' and alike.
4305 (insert
4306 (with-current-buffer (tramp-get-buffer v)
4307 (buffer-string)))
4309 ;; Check for "--dired" output.
4310 (forward-line -2)
4311 (when (looking-at "//SUBDIRED//")
4312 (forward-line -1))
4313 (when (looking-at "//DIRED//\\s-+")
4314 (let ((databeg (match-end 0))
4315 (end (tramp-compat-line-end-position)))
4316 ;; Now read the numeric positions of file names.
4317 (goto-char databeg)
4318 (while (< (point) end)
4319 (let ((start (+ beg (read (current-buffer))))
4320 (end (+ beg (read (current-buffer)))))
4321 (if (memq (char-after end) '(?\n ?\ ))
4322 ;; End is followed by \n or by " -> ".
4323 (put-text-property start end 'dired-filename t))))))
4324 ;; Remove trailing lines.
4325 (goto-char (tramp-compat-line-beginning-position))
4326 (while (looking-at "//")
4327 (forward-line 1)
4328 (delete-region (match-beginning 0) (point)))
4330 ;; The inserted file could be from somewhere else.
4331 (when (and (not wildcard) (not full-directory-p))
4332 (goto-char (point-max))
4333 (when (file-symlink-p filename)
4334 (goto-char (search-backward "->" beg 'noerror)))
4335 (search-backward
4336 (if (zerop (length (file-name-nondirectory filename)))
4338 (file-name-nondirectory filename))
4339 beg 'noerror)
4340 (replace-match (file-relative-name filename) t))
4342 (goto-char (point-max))))))
4344 (defun tramp-handle-unhandled-file-name-directory (filename)
4345 "Like `unhandled-file-name-directory' for Tramp files."
4346 ;; With Emacs 23, we could simply return `nil'. But we must keep it
4347 ;; for backward compatibility.
4348 (expand-file-name "~/"))
4350 ;; Canonicalization of file names.
4352 (defun tramp-handle-expand-file-name (name &optional dir)
4353 "Like `expand-file-name' for Tramp files.
4354 If the localname part of the given filename starts with \"/../\" then
4355 the result will be a local, non-Tramp, filename."
4356 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
4357 (setq dir (or dir default-directory "/"))
4358 ;; Unless NAME is absolute, concat DIR and NAME.
4359 (unless (file-name-absolute-p name)
4360 (setq name (concat (file-name-as-directory dir) name)))
4361 ;; If NAME is not a Tramp file, run the real handler.
4362 (if (not (tramp-connectable-p name))
4363 (tramp-run-real-handler 'expand-file-name (list name nil))
4364 ;; Dissect NAME.
4365 (with-parsed-tramp-file-name name nil
4366 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
4367 (setq localname (concat "~/" localname)))
4368 ;; Tilde expansion if necessary. This needs a shell which
4369 ;; groks tilde expansion! The function `tramp-find-shell' is
4370 ;; supposed to find such a shell on the remote host. Please
4371 ;; tell me about it when this doesn't work on your system.
4372 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
4373 (let ((uname (match-string 1 localname))
4374 (fname (match-string 2 localname)))
4375 ;; We cannot simply apply "~/", because under sudo "~/" is
4376 ;; expanded to the local user home directory but to the
4377 ;; root home directory. On the other hand, using always
4378 ;; the default user name for tilde expansion is not
4379 ;; appropriate either, because ssh and companions might
4380 ;; use a user name from the config file.
4381 (when (and (string-equal uname "~")
4382 (string-match "\\`su\\(do\\)?\\'" method))
4383 (setq uname (concat uname user)))
4384 (setq uname
4385 (with-connection-property v uname
4386 (tramp-send-command
4387 v (format "cd %s; pwd" (tramp-shell-quote-argument uname)))
4388 (with-current-buffer (tramp-get-buffer v)
4389 (goto-char (point-min))
4390 (buffer-substring
4391 (point) (tramp-compat-line-end-position)))))
4392 (setq localname (concat uname fname))))
4393 ;; There might be a double slash, for example when "~/"
4394 ;; expands to "/". Remove this.
4395 (while (string-match "//" localname)
4396 (setq localname (replace-match "/" t t localname)))
4397 ;; No tilde characters in file name, do normal
4398 ;; `expand-file-name' (this does "/./" and "/../"). We bind
4399 ;; `directory-sep-char' here for XEmacs on Windows, which would
4400 ;; otherwise use backslash. `default-directory' is bound,
4401 ;; because on Windows there would be problems with UNC shares or
4402 ;; Cygwin mounts.
4403 (let ((directory-sep-char ?/)
4404 (default-directory (tramp-compat-temporary-file-directory)))
4405 (tramp-make-tramp-file-name
4406 method user host
4407 (tramp-drop-volume-letter
4408 (tramp-run-real-handler
4409 'expand-file-name (list localname))))))))
4411 (defun tramp-replace-environment-variables (filename)
4412 "Replace environment variables in FILENAME.
4413 Return the string with the replaced variables."
4414 (save-match-data
4415 (let ((idx (string-match "$\\(\\w+\\)" filename)))
4416 ;; `$' is coded as `$$'.
4417 (when (and idx
4418 (or (zerop idx) (not (eq ?$ (aref filename (1- idx)))))
4419 (getenv (match-string 1 filename)))
4420 (setq filename
4421 (replace-match
4422 (substitute-in-file-name (match-string 0 filename))
4423 t nil filename)))
4424 filename)))
4426 (defun tramp-handle-substitute-in-file-name (filename)
4427 "Like `substitute-in-file-name' for Tramp files.
4428 \"//\" and \"/~\" substitute only in the local filename part.
4429 If the URL Tramp syntax is chosen, \"//\" as method delimeter and \"/~\" at
4430 beginning of local filename are not substituted."
4431 ;; First, we must replace environment variables.
4432 (setq filename (tramp-replace-environment-variables filename))
4433 (with-parsed-tramp-file-name filename nil
4434 (if (equal tramp-syntax 'url)
4435 ;; We need to check localname only. The other parts cannot contain
4436 ;; "//" or "/~".
4437 (if (and (> (length localname) 1)
4438 (or (string-match "//" localname)
4439 (string-match "/~" localname 1)))
4440 (tramp-run-real-handler 'substitute-in-file-name (list filename))
4441 (tramp-make-tramp-file-name
4442 (when method (substitute-in-file-name method))
4443 (when user (substitute-in-file-name user))
4444 (when host (substitute-in-file-name host))
4445 (when localname
4446 (tramp-run-real-handler
4447 'substitute-in-file-name (list localname)))))
4448 ;; Ignore in LOCALNAME everything before "//" or "/~".
4449 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
4450 (setq filename
4451 (concat (file-remote-p filename)
4452 (replace-match "\\1" nil nil localname)))
4453 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
4454 (when (string-match "~$" filename)
4455 (setq filename (concat filename "/"))))
4456 (tramp-run-real-handler 'substitute-in-file-name (list filename)))))
4458 ;; In XEmacs, electricity is implemented via a key map for ?/ and ?~,
4459 ;; which calls corresponding functions (see minibuf.el).
4460 (when (fboundp 'minibuffer-electric-separator)
4461 (mapc
4462 (lambda (x)
4463 (eval
4464 `(defadvice ,x
4465 (around ,(intern (format "tramp-advice-%s" x)) activate)
4466 "Invoke `substitute-in-file-name' for Tramp files."
4467 (if (and (symbol-value 'minibuffer-electric-file-name-behavior)
4468 (tramp-tramp-file-p (buffer-substring)))
4469 ;; We don't need to handle `last-input-event', because
4470 ;; due to the key map we know it must be ?/ or ?~.
4471 (let ((s (concat (buffer-substring (point-min) (point))
4472 (string last-command-char))))
4473 (delete-region (point-min) (point))
4474 (insert (substitute-in-file-name s))
4475 (setq ad-return-value last-command-char))
4476 ad-do-it)))
4477 (eval
4478 `(add-hook
4479 'tramp-unload-hook
4480 (lambda ()
4481 (ad-remove-advice ',x 'around ',(intern (format "tramp-advice-%s" x)))
4482 (ad-activate ',x)))))
4484 '(minibuffer-electric-separator
4485 minibuffer-electric-tilde)))
4488 ;;; Remote commands:
4490 (defun tramp-handle-executable-find (command)
4491 "Like `executable-find' for Tramp files."
4492 (with-parsed-tramp-file-name default-directory nil
4493 (tramp-find-executable v command (tramp-get-remote-path v) t)))
4495 ;; We use BUFFER also as connection buffer during setup. Because of
4496 ;; this, its original contents must be saved, and restored once
4497 ;; connection has been setup.
4498 (defun tramp-handle-start-file-process (name buffer program &rest args)
4499 "Like `start-file-process' for Tramp files."
4500 (with-parsed-tramp-file-name default-directory nil
4501 (unless (stringp program)
4502 (tramp-error
4503 v 'file-error "pty association is not supported for `%s'" name))
4504 (unwind-protect
4505 (let ((command (format "cd %s; exec %s"
4506 (tramp-shell-quote-argument localname)
4507 (mapconcat 'tramp-shell-quote-argument
4508 (cons program args) " ")))
4509 (name1 name)
4510 (i 0))
4511 (unless buffer
4512 ;; BUFFER can be nil. We use a temporary buffer.
4513 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
4514 (while (get-process name1)
4515 ;; NAME must be unique as process name.
4516 (setq i (1+ i)
4517 name1 (format "%s<%d>" name i)))
4518 (setq name name1)
4519 ;; Set the new process properties.
4520 (tramp-set-connection-property v "process-name" name)
4521 (tramp-set-connection-property v "process-buffer" buffer)
4522 ;; Activate narrowing in order to save BUFFER contents.
4523 ;; Clear also the modification time; otherwise we might be
4524 ;; interrupted by `verify-visited-file-modtime'.
4525 (with-current-buffer (tramp-get-connection-buffer v)
4526 (clear-visited-file-modtime)
4527 (narrow-to-region (point-max) (point-max)))
4528 ;; Send the command. `tramp-send-command' opens a new
4529 ;; connection.
4530 (tramp-send-command v command nil t) ; nooutput
4531 ;; Set query flag for this process.
4532 (tramp-set-process-query-on-exit-flag
4533 (tramp-get-connection-process v) t)
4534 ;; Return process.
4535 (tramp-get-connection-process v))
4536 ;; Save exit.
4537 (with-current-buffer (tramp-get-connection-buffer v)
4538 (if (string-match tramp-temp-buffer-name (buffer-name))
4539 (progn
4540 (set-process-buffer (tramp-get-connection-process v) nil)
4541 (kill-buffer (current-buffer)))
4542 (widen)
4543 (goto-char (point-max))))
4544 (tramp-set-connection-property v "process-name" nil)
4545 (tramp-set-connection-property v "process-buffer" nil))))
4547 (defun tramp-handle-process-file
4548 (program &optional infile destination display &rest args)
4549 "Like `process-file' for Tramp files."
4550 ;; The implementation is not complete yet.
4551 (when (and (numberp destination) (zerop destination))
4552 (error "Implementation does not handle immediate return"))
4554 (with-parsed-tramp-file-name default-directory nil
4555 (let (command input tmpinput stderr tmpstderr outbuf ret)
4556 ;; Compute command.
4557 (setq command (mapconcat 'tramp-shell-quote-argument
4558 (cons program args) " "))
4559 ;; Determine input.
4560 (if (null infile)
4561 (setq input "/dev/null")
4562 (setq infile (expand-file-name infile))
4563 (if (tramp-equal-remote default-directory infile)
4564 ;; INFILE is on the same remote host.
4565 (setq input (with-parsed-tramp-file-name infile nil localname))
4566 ;; INFILE must be copied to remote host.
4567 (setq input (tramp-make-tramp-temp-file v)
4568 tmpinput (tramp-make-tramp-file-name method user host input))
4569 (copy-file infile tmpinput t)))
4570 (when input (setq command (format "%s <%s" command input)))
4572 ;; Determine output.
4573 (cond
4574 ;; Just a buffer.
4575 ((bufferp destination)
4576 (setq outbuf destination))
4577 ;; A buffer name.
4578 ((stringp destination)
4579 (setq outbuf (get-buffer-create destination)))
4580 ;; (REAL-DESTINATION ERROR-DESTINATION)
4581 ((consp destination)
4582 ;; output.
4583 (cond
4584 ((bufferp (car destination))
4585 (setq outbuf (car destination)))
4586 ((stringp (car destination))
4587 (setq outbuf (get-buffer-create (car destination))))
4588 ((car destination)
4589 (setq outbuf (current-buffer))))
4590 ;; stderr.
4591 (cond
4592 ((stringp (cadr destination))
4593 (setcar (cdr destination) (expand-file-name (cadr destination)))
4594 (if (tramp-equal-remote default-directory (cadr destination))
4595 ;; stderr is on the same remote host.
4596 (setq stderr (with-parsed-tramp-file-name
4597 (cadr destination) nil localname))
4598 ;; stderr must be copied to remote host. The temporary
4599 ;; file must be deleted after execution.
4600 (setq stderr (tramp-make-tramp-temp-file v)
4601 tmpstderr (tramp-make-tramp-file-name
4602 method user host stderr))))
4603 ;; stderr to be discarded.
4604 ((null (cadr destination))
4605 (setq stderr "/dev/null"))))
4606 ;; 't
4607 (destination
4608 (setq outbuf (current-buffer))))
4609 (when stderr (setq command (format "%s 2>%s" command stderr)))
4611 ;; Send the command. It might not return in time, so we protect it.
4612 (condition-case nil
4613 (unwind-protect
4614 (setq ret
4615 (tramp-send-command-and-check
4616 v (format "\\cd %s; %s"
4617 (tramp-shell-quote-argument localname)
4618 command)
4619 nil t))
4620 ;; We should show the output anyway.
4621 (when outbuf
4622 (with-current-buffer outbuf
4623 (insert
4624 (with-current-buffer (tramp-get-connection-buffer v)
4625 (buffer-string))))
4626 (when display (display-buffer outbuf))))
4627 ;; When the user did interrupt, we should do it also. We use
4628 ;; return code -1 as marker.
4629 (quit
4630 (kill-buffer (tramp-get-connection-buffer v))
4631 (setq ret -1))
4632 ;; Handle errors.
4633 (error
4634 (kill-buffer (tramp-get-connection-buffer v))
4635 (setq ret 1)))
4637 ;; Provide error file.
4638 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
4640 ;; Cleanup. We remove all file cache values for the connection,
4641 ;; because the remote process could have changed them.
4642 (when tmpinput (delete-file tmpinput))
4644 ;; `process-file-side-effects' has been introduced with GNU
4645 ;; Emacs 23.2. If set to `nil', no remote file will be changed
4646 ;; by `program'. If it doesn't exist, we assume its default
4647 ;; value 't'.
4648 (unless (and (boundp 'process-file-side-effects)
4649 (not (symbol-value 'process-file-side-effects)))
4650 (tramp-flush-directory-property v ""))
4652 ;; Return exit status.
4653 (if (equal ret -1)
4654 (keyboard-quit)
4655 ret))))
4657 (defun tramp-local-call-process
4658 (program &optional infile destination display &rest args)
4659 "Calls `call-process' on the local host.
4660 This is needed because for some Emacs flavors Tramp has
4661 defadviced `call-process' to behave like `process-file'. The
4662 Lisp error raised when PROGRAM is nil is trapped also, returning 1."
4663 (let ((default-directory
4664 (if (file-remote-p default-directory)
4665 (tramp-compat-temporary-file-directory)
4666 default-directory)))
4667 (if (executable-find program)
4668 (apply 'call-process program infile destination display args)
4669 1)))
4671 (defun tramp-handle-call-process-region
4672 (start end program &optional delete buffer display &rest args)
4673 "Like `call-process-region' for Tramp files."
4674 (let ((tmpfile (tramp-compat-make-temp-file "")))
4675 (write-region start end tmpfile)
4676 (when delete (delete-region start end))
4677 (unwind-protect
4678 (apply 'call-process program tmpfile buffer display args)
4679 (delete-file tmpfile))))
4681 (defun tramp-handle-shell-command
4682 (command &optional output-buffer error-buffer)
4683 "Like `shell-command' for Tramp files."
4684 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
4685 ;; We cannot use `shell-file-name' and `shell-command-switch',
4686 ;; they are variables of the local host.
4687 (args (list
4688 (tramp-get-method-parameter
4689 (tramp-file-name-method
4690 (tramp-dissect-file-name default-directory))
4691 'tramp-remote-sh)
4692 "-c" (substring command 0 asynchronous)))
4693 current-buffer-p
4694 (output-buffer
4695 (cond
4696 ((bufferp output-buffer) output-buffer)
4697 ((stringp output-buffer) (get-buffer-create output-buffer))
4698 (output-buffer
4699 (setq current-buffer-p t)
4700 (current-buffer))
4701 (t (get-buffer-create
4702 (if asynchronous
4703 "*Async Shell Command*"
4704 "*Shell Command Output*")))))
4705 (error-buffer
4706 (cond
4707 ((bufferp error-buffer) error-buffer)
4708 ((stringp error-buffer) (get-buffer-create error-buffer))))
4709 (buffer
4710 (if (and (not asynchronous) error-buffer)
4711 (with-parsed-tramp-file-name default-directory nil
4712 (list output-buffer (tramp-make-tramp-temp-file v)))
4713 output-buffer))
4714 (p (get-buffer-process output-buffer)))
4716 ;; Check whether there is another process running. Tramp does not
4717 ;; support 2 (asynchronous) processes in parallel.
4718 (when p
4719 (if (yes-or-no-p "A command is running. Kill it? ")
4720 (condition-case nil
4721 (kill-process p)
4722 (error nil))
4723 (error "Shell command in progress")))
4725 (if current-buffer-p
4726 (progn
4727 (barf-if-buffer-read-only)
4728 (push-mark nil t))
4729 (with-current-buffer output-buffer
4730 (setq buffer-read-only nil)
4731 (erase-buffer)))
4733 (if (and (not current-buffer-p) (integerp asynchronous))
4734 (prog1
4735 ;; Run the process.
4736 (apply 'start-file-process "*Async Shell*" buffer args)
4737 ;; Display output.
4738 (pop-to-buffer output-buffer)
4739 (setq mode-line-process '(":%s"))
4740 (require 'shell) (shell-mode))
4742 (prog1
4743 ;; Run the process.
4744 (apply 'process-file (car args) nil buffer nil (cdr args))
4745 ;; Insert error messages if they were separated.
4746 (when (listp buffer)
4747 (with-current-buffer error-buffer
4748 (insert-file-contents (cadr buffer)))
4749 (delete-file (cadr buffer)))
4750 (if current-buffer-p
4751 ;; This is like exchange-point-and-mark, but doesn't
4752 ;; activate the mark. It is cleaner to avoid activation,
4753 ;; even though the command loop would deactivate the mark
4754 ;; because we inserted text.
4755 (goto-char (prog1 (mark t)
4756 (set-marker (mark-marker) (point)
4757 (current-buffer))))
4758 ;; There's some output, display it.
4759 (when (with-current-buffer output-buffer (> (point-max) (point-min)))
4760 (if (functionp 'display-message-or-buffer)
4761 (tramp-compat-funcall 'display-message-or-buffer output-buffer)
4762 (pop-to-buffer output-buffer))))))))
4764 ;; File Editing.
4766 (defvar tramp-handle-file-local-copy-hook nil
4767 "Normal hook to be run at the end of `tramp-handle-file-local-copy'.")
4769 (defun tramp-handle-file-local-copy (filename)
4770 "Like `file-local-copy' for Tramp files."
4772 (with-parsed-tramp-file-name filename nil
4773 (unless (file-exists-p filename)
4774 (tramp-error
4775 v 'file-error
4776 "Cannot make local copy of non-existing file `%s'" filename))
4778 (let* ((size (nth 7 (file-attributes filename)))
4779 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
4780 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
4781 (tmpfile (tramp-compat-make-temp-file filename)))
4783 (condition-case err
4784 (cond
4785 ;; `copy-file' handles direct copy and out-of-band methods.
4786 ((or (tramp-local-host-p v)
4787 (tramp-method-out-of-band-p v size))
4788 (copy-file filename tmpfile t t))
4790 ;; Use inline encoding for file transfer.
4791 (rem-enc
4792 (save-excursion
4793 (with-progress-reporter
4794 v 3 (format "Encoding remote file %s" filename)
4795 (tramp-barf-unless-okay
4796 v (format rem-enc (tramp-shell-quote-argument localname))
4797 "Encoding remote file failed"))
4799 (if (functionp loc-dec)
4800 ;; If local decoding is a function, we call it. We
4801 ;; must disable multibyte, because
4802 ;; `uudecode-decode-region' doesn't handle it
4803 ;; correctly.
4804 (with-temp-buffer
4805 (set-buffer-multibyte nil)
4806 (insert-buffer-substring (tramp-get-buffer v))
4807 (with-progress-reporter
4808 v 3 (format "Decoding remote file %s with function %s"
4809 filename loc-dec)
4810 (funcall loc-dec (point-min) (point-max))
4811 ;; Unset `file-name-handler-alist'. Otherwise,
4812 ;; epa-file gets confused.
4813 (let (file-name-handler-alist
4814 (coding-system-for-write 'binary))
4815 (write-region (point-min) (point-max) tmpfile))))
4817 ;; If tramp-decoding-function is not defined for this
4818 ;; method, we invoke tramp-decoding-command instead.
4819 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
4820 ;; Unset `file-name-handler-alist'. Otherwise,
4821 ;; epa-file gets confused.
4822 (let (file-name-handler-alist
4823 (coding-system-for-write 'binary))
4824 (write-region (point-min) (point-max) tmpfile2))
4825 (with-progress-reporter
4826 v 3 (format "Decoding remote file %s with command %s"
4827 filename loc-dec)
4828 (unwind-protect
4829 (tramp-call-local-coding-command
4830 loc-dec tmpfile2 tmpfile)
4831 (delete-file tmpfile2)))))
4833 ;; Set proper permissions.
4834 (set-file-modes tmpfile (tramp-default-file-modes filename))
4835 ;; Set local user ownership.
4836 (tramp-set-file-uid-gid tmpfile)))
4838 ;; Oops, I don't know what to do.
4839 (t (tramp-error
4840 v 'file-error "Wrong method specification for `%s'" method)))
4842 ;; Error handling.
4843 ((error quit)
4844 (delete-file tmpfile)
4845 (signal (car err) (cdr err))))
4847 (run-hooks 'tramp-handle-file-local-copy-hook)
4848 tmpfile)))
4850 (defun tramp-handle-file-remote-p (filename &optional identification connected)
4851 "Like `file-remote-p' for Tramp files."
4852 (let ((tramp-verbose 3))
4853 (when (tramp-tramp-file-p filename)
4854 (let* ((v (tramp-dissect-file-name filename))
4855 (p (tramp-get-connection-process v))
4856 (c (and p (processp p) (memq (process-status p) '(run open)))))
4857 ;; We expand the file name only, if there is already a connection.
4858 (with-parsed-tramp-file-name
4859 (if c (expand-file-name filename) filename) nil
4860 (and (or (not connected) c)
4861 (cond
4862 ((eq identification 'method) method)
4863 ((eq identification 'user) user)
4864 ((eq identification 'host) host)
4865 ((eq identification 'localname) localname)
4866 (t (tramp-make-tramp-file-name method user host "")))))))))
4868 (defun tramp-find-file-name-coding-system-alist (filename tmpname)
4869 "Like `find-operation-coding-system' for Tramp filenames.
4870 Tramp's `insert-file-contents' and `write-region' work over
4871 temporary file names. If `file-coding-system-alist' contains an
4872 expression, which matches more than the file name suffix, the
4873 coding system might not be determined. This function repairs it."
4874 (let (result)
4875 (dolist (elt file-coding-system-alist result)
4876 (when (and (consp elt) (string-match (car elt) filename))
4877 ;; We found a matching entry in `file-coding-system-alist'.
4878 ;; So we add a similar entry, but with the temporary file name
4879 ;; as regexp.
4880 (add-to-list
4881 'result (cons (regexp-quote tmpname) (cdr elt)) 'append)))))
4883 (defun tramp-handle-insert-file-contents
4884 (filename &optional visit beg end replace)
4885 "Like `insert-file-contents' for Tramp files."
4886 (barf-if-buffer-read-only)
4887 (setq filename (expand-file-name filename))
4888 (let (result local-copy remote-copy)
4889 (with-parsed-tramp-file-name filename nil
4890 (unwind-protect
4891 (if (not (file-exists-p filename))
4892 ;; We don't raise a Tramp error, because it might be
4893 ;; suppressed, like in `find-file-noselect-1'.
4894 (signal 'file-error
4895 (list "File not found on remote host" filename))
4897 (if (and (tramp-local-host-p v)
4898 (let (file-name-handler-alist)
4899 (file-readable-p localname)))
4900 ;; Short track: if we are on the local host, we can
4901 ;; run directly.
4902 (setq result
4903 (tramp-run-real-handler
4904 'insert-file-contents
4905 (list localname visit beg end replace)))
4907 ;; When we shall insert only a part of the file, we copy
4908 ;; this part.
4909 (when (or beg end)
4910 (setq remote-copy (tramp-make-tramp-temp-file v))
4911 (tramp-send-command
4913 (cond
4914 ((and beg end)
4915 (format "tail -c +%d %s | head -c +%d >%s"
4916 (1+ beg) (tramp-shell-quote-argument localname)
4917 (- end beg) remote-copy))
4918 (beg
4919 (format "tail -c +%d %s >%s"
4920 (1+ beg) (tramp-shell-quote-argument localname)
4921 remote-copy))
4922 (end
4923 (format "head -c +%d %s >%s"
4924 (1+ end) (tramp-shell-quote-argument localname)
4925 remote-copy)))))
4927 ;; `insert-file-contents-literally' takes care to avoid
4928 ;; calling jka-compr. By let-binding
4929 ;; `inhibit-file-name-operation', we propagate that care
4930 ;; to the `file-local-copy' operation.
4931 (setq local-copy
4932 (let ((inhibit-file-name-operation
4933 (when (eq inhibit-file-name-operation
4934 'insert-file-contents)
4935 'file-local-copy)))
4936 (cond
4937 ((stringp remote-copy)
4938 (file-local-copy
4939 (tramp-make-tramp-file-name
4940 method user host remote-copy)))
4941 ((stringp tramp-temp-buffer-file-name)
4942 (copy-file filename tramp-temp-buffer-file-name 'ok)
4943 tramp-temp-buffer-file-name)
4944 (t (file-local-copy filename)))))
4946 ;; When the file is not readable for the owner, it
4947 ;; cannot be inserted, even it is redable for the group
4948 ;; or for everybody.
4949 (set-file-modes local-copy (tramp-octal-to-decimal "0600"))
4951 (when (and (null remote-copy)
4952 (tramp-get-method-parameter
4953 method 'tramp-copy-keep-tmpfile))
4954 ;; We keep the local file for performance reasons,
4955 ;; useful for "rsync".
4956 (setq tramp-temp-buffer-file-name local-copy)
4957 (put 'tramp-temp-buffer-file-name 'permanent-local t))
4959 (with-progress-reporter
4960 v 3 (format "Inserting local temp file `%s'" local-copy)
4961 ;; We must ensure that `file-coding-system-alist'
4962 ;; matches `local-copy'.
4963 (let ((file-coding-system-alist
4964 (tramp-find-file-name-coding-system-alist
4965 filename local-copy)))
4966 (setq result
4967 (insert-file-contents
4968 local-copy nil nil nil replace))))))
4970 ;; Save exit.
4971 (progn
4972 (when visit
4973 (setq buffer-file-name filename)
4974 (setq buffer-read-only (not (file-writable-p filename)))
4975 (set-visited-file-modtime)
4976 (set-buffer-modified-p nil))
4977 (when (and (stringp local-copy)
4978 (or remote-copy (null tramp-temp-buffer-file-name)))
4979 (delete-file local-copy))
4980 (when (stringp remote-copy)
4981 (delete-file
4982 (tramp-make-tramp-file-name method user host remote-copy))))))
4984 ;; Result.
4985 (list (expand-file-name filename)
4986 (cadr result))))
4988 ;; This is needed for XEmacs only. Code stolen from files.el.
4989 (defun tramp-handle-insert-file-contents-literally
4990 (filename &optional visit beg end replace)
4991 "Like `insert-file-contents-literally' for Tramp files."
4992 (let ((format-alist nil)
4993 (after-insert-file-functions nil)
4994 (coding-system-for-read 'no-conversion)
4995 (coding-system-for-write 'no-conversion)
4996 (find-buffer-file-type-function
4997 (if (fboundp 'find-buffer-file-type)
4998 (symbol-function 'find-buffer-file-type)
4999 nil))
5000 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler))
5001 (inhibit-file-name-operation 'insert-file-contents))
5002 (unwind-protect
5003 (progn
5004 (fset 'find-buffer-file-type (lambda (filename) t))
5005 (insert-file-contents filename visit beg end replace))
5006 ;; Save exit.
5007 (if find-buffer-file-type-function
5008 (fset 'find-buffer-file-type find-buffer-file-type-function)
5009 (fmakunbound 'find-buffer-file-type)))))
5011 (defun tramp-handle-find-backup-file-name (filename)
5012 "Like `find-backup-file-name' for Tramp files."
5013 (with-parsed-tramp-file-name filename nil
5014 ;; We set both variables. It doesn't matter whether it is
5015 ;; Emacs or XEmacs.
5016 (let ((backup-directory-alist
5017 ;; Emacs case.
5018 (when (boundp 'backup-directory-alist)
5019 (if (symbol-value 'tramp-backup-directory-alist)
5020 (mapcar
5021 (lambda (x)
5022 (cons
5023 (car x)
5024 (if (and (stringp (cdr x))
5025 (file-name-absolute-p (cdr x))
5026 (not (tramp-file-name-p (cdr x))))
5027 (tramp-make-tramp-file-name method user host (cdr x))
5028 (cdr x))))
5029 (symbol-value 'tramp-backup-directory-alist))
5030 (symbol-value 'backup-directory-alist))))
5032 (bkup-backup-directory-info
5033 ;; XEmacs case.
5034 (when (boundp 'bkup-backup-directory-info)
5035 (if (symbol-value 'tramp-bkup-backup-directory-info)
5036 (mapcar
5037 (lambda (x)
5038 (nconc
5039 (list (car x))
5040 (list
5041 (if (and (stringp (car (cdr x)))
5042 (file-name-absolute-p (car (cdr x)))
5043 (not (tramp-file-name-p (car (cdr x)))))
5044 (tramp-make-tramp-file-name
5045 method user host (car (cdr x)))
5046 (car (cdr x))))
5047 (cdr (cdr x))))
5048 (symbol-value 'tramp-bkup-backup-directory-info))
5049 (symbol-value 'bkup-backup-directory-info)))))
5051 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
5053 (defun tramp-handle-make-auto-save-file-name ()
5054 "Like `make-auto-save-file-name' for Tramp files.
5055 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
5056 (let ((tramp-auto-save-directory tramp-auto-save-directory)
5057 (buffer-file-name
5058 (tramp-subst-strs-in-string
5059 '(("_" . "|")
5060 ("/" . "_a")
5061 (":" . "_b")
5062 ("|" . "__")
5063 ("[" . "_l")
5064 ("]" . "_r"))
5065 (buffer-file-name))))
5066 ;; File name must be unique. This is ensured with Emacs 22 (see
5067 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
5068 ;; all other cases we must do it ourselves.
5069 (when (boundp 'auto-save-file-name-transforms)
5070 (mapc
5071 (lambda (x)
5072 (when (and (string-match (car x) buffer-file-name)
5073 (not (car (cddr x))))
5074 (setq tramp-auto-save-directory
5075 (or tramp-auto-save-directory
5076 (tramp-compat-temporary-file-directory)))))
5077 (symbol-value 'auto-save-file-name-transforms)))
5078 ;; Create directory.
5079 (when tramp-auto-save-directory
5080 (setq buffer-file-name
5081 (expand-file-name buffer-file-name tramp-auto-save-directory))
5082 (unless (file-exists-p tramp-auto-save-directory)
5083 (make-directory tramp-auto-save-directory t)))
5084 ;; Run plain `make-auto-save-file-name'. There might be an advice when
5085 ;; it is not a magic file name operation (since Emacs 22).
5086 ;; We must deactivate it temporarily.
5087 (if (not (ad-is-active 'make-auto-save-file-name))
5088 (tramp-run-real-handler 'make-auto-save-file-name nil)
5089 ;; else
5090 (ad-deactivate 'make-auto-save-file-name)
5091 (prog1
5092 (tramp-run-real-handler 'make-auto-save-file-name nil)
5093 (ad-activate 'make-auto-save-file-name)))))
5095 (defvar tramp-handle-write-region-hook nil
5096 "Normal hook to be run at the end of `tramp-handle-write-region'.")
5098 ;; CCC grok LOCKNAME
5099 (defun tramp-handle-write-region
5100 (start end filename &optional append visit lockname confirm)
5101 "Like `write-region' for Tramp files."
5102 (setq filename (expand-file-name filename))
5103 (with-parsed-tramp-file-name filename nil
5104 ;; Following part commented out because we don't know what to do about
5105 ;; file locking, and it does not appear to be a problem to ignore it.
5106 ;; Ange-ftp ignores it, too.
5107 ;; (when (and lockname (stringp lockname))
5108 ;; (setq lockname (expand-file-name lockname)))
5109 ;; (unless (or (eq lockname nil)
5110 ;; (string= lockname filename))
5111 ;; (error
5112 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
5114 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
5115 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
5116 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
5117 (tramp-error v 'file-error "File not overwritten")))
5119 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
5120 (tramp-get-remote-uid v 'integer)))
5121 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
5122 (tramp-get-remote-gid v 'integer))))
5124 (if (and (tramp-local-host-p v)
5125 ;; `file-writable-p' calls `file-expand-file-name'. We
5126 ;; cannot use `tramp-run-real-handler' therefore.
5127 (let (file-name-handler-alist)
5128 (and
5129 (file-writable-p (file-name-directory localname))
5130 (or (file-directory-p localname)
5131 (file-writable-p localname)))))
5132 ;; Short track: if we are on the local host, we can run directly.
5133 (tramp-run-real-handler
5134 'write-region
5135 (list start end localname append 'no-message lockname confirm))
5137 (let ((modes (save-excursion (tramp-default-file-modes filename)))
5138 ;; We use this to save the value of
5139 ;; `last-coding-system-used' after writing the tmp
5140 ;; file. At the end of the function, we set
5141 ;; `last-coding-system-used' to this saved value. This
5142 ;; way, any intermediary coding systems used while
5143 ;; talking to the remote shell or suchlike won't hose
5144 ;; this variable. This approach was snarfed from
5145 ;; ange-ftp.el.
5146 coding-system-used
5147 ;; Write region into a tmp file. This isn't really
5148 ;; needed if we use an encoding function, but currently
5149 ;; we use it always because this makes the logic
5150 ;; simpler.
5151 (tmpfile (or tramp-temp-buffer-file-name
5152 (tramp-compat-make-temp-file filename))))
5154 ;; If `append' is non-nil, we copy the file locally, and let
5155 ;; the native `write-region' implementation do the job.
5156 (when append (copy-file filename tmpfile 'ok))
5158 ;; We say `no-message' here because we don't want the
5159 ;; visited file modtime data to be clobbered from the temp
5160 ;; file. We call `set-visited-file-modtime' ourselves later
5161 ;; on. We must ensure that `file-coding-system-alist'
5162 ;; matches `tmpfile'.
5163 (let (file-name-handler-alist
5164 (file-coding-system-alist
5165 (tramp-find-file-name-coding-system-alist filename tmpfile)))
5166 (condition-case err
5167 (tramp-run-real-handler
5168 'write-region
5169 (list start end tmpfile append 'no-message lockname confirm))
5170 ((error quit)
5171 (setq tramp-temp-buffer-file-name nil)
5172 (delete-file tmpfile)
5173 (signal (car err) (cdr err))))
5175 ;; Now, `last-coding-system-used' has the right value. Remember it.
5176 (when (boundp 'last-coding-system-used)
5177 (setq coding-system-used
5178 (symbol-value 'last-coding-system-used))))
5180 ;; The permissions of the temporary file should be set. If
5181 ;; filename does not exist (eq modes nil) it has been
5182 ;; renamed to the backup file. This case `save-buffer'
5183 ;; handles permissions.
5184 ;; Ensure, that it is still readable.
5185 (when modes
5186 (set-file-modes
5187 tmpfile (logior (or modes 0) (tramp-octal-to-decimal "0400"))))
5189 ;; This is a bit lengthy due to the different methods
5190 ;; possible for file transfer. First, we check whether the
5191 ;; method uses an rcp program. If so, we call it.
5192 ;; Otherwise, both encoding and decoding command must be
5193 ;; specified. However, if the method _also_ specifies an
5194 ;; encoding function, then that is used for encoding the
5195 ;; contents of the tmp file.
5196 (let* ((size (nth 7 (file-attributes tmpfile)))
5197 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
5198 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
5199 (cond
5200 ;; `copy-file' handles direct copy and out-of-band methods.
5201 ((or (tramp-local-host-p v)
5202 (tramp-method-out-of-band-p v size))
5203 (if (and (not (stringp start))
5204 (= (or end (point-max)) (point-max))
5205 (= (or start (point-min)) (point-min))
5206 (tramp-get-method-parameter
5207 method 'tramp-copy-keep-tmpfile))
5208 (progn
5209 (setq tramp-temp-buffer-file-name tmpfile)
5210 (condition-case err
5211 ;; We keep the local file for performance
5212 ;; reasons, useful for "rsync".
5213 (copy-file tmpfile filename t)
5214 ((error quit)
5215 (setq tramp-temp-buffer-file-name nil)
5216 (delete-file tmpfile)
5217 (signal (car err) (cdr err)))))
5218 (setq tramp-temp-buffer-file-name nil)
5219 ;; Don't rename, in order to keep context in SELinux.
5220 (unwind-protect
5221 (copy-file tmpfile filename t)
5222 (delete-file tmpfile))))
5224 ;; Use inline file transfer.
5225 (rem-dec
5226 ;; Encode tmpfile.
5227 (unwind-protect
5228 (with-temp-buffer
5229 (set-buffer-multibyte nil)
5230 ;; Use encoding function or command.
5231 (if (functionp loc-enc)
5232 (with-progress-reporter
5233 v 3 (format "Encoding region using function `%s'"
5234 loc-enc)
5235 (let ((coding-system-for-read 'binary))
5236 (insert-file-contents-literally tmpfile))
5237 ;; The following `let' is a workaround for the
5238 ;; base64.el that comes with pgnus-0.84. If
5239 ;; both of the following conditions are
5240 ;; satisfied, it tries to write to a local
5241 ;; file in default-directory, but at this
5242 ;; point, default-directory is remote.
5243 ;; (`call-process-region' can't write to
5244 ;; remote files, it seems.) The file in
5245 ;; question is a tmp file anyway.
5246 (let ((default-directory
5247 (tramp-compat-temporary-file-directory)))
5248 (funcall loc-enc (point-min) (point-max))))
5250 (with-progress-reporter
5251 v 3 (format "Encoding region using command `%s'"
5252 loc-enc)
5253 (unless (zerop (tramp-call-local-coding-command
5254 loc-enc tmpfile t))
5255 (tramp-error
5256 v 'file-error
5257 (concat "Cannot write to `%s', "
5258 "local encoding command `%s' failed")
5259 filename loc-enc))))
5261 ;; Send buffer into remote decoding command which
5262 ;; writes to remote file. Because this happens on
5263 ;; the remote host, we cannot use the function.
5264 (with-progress-reporter
5266 (format "Decoding region into remote file %s" filename)
5267 (goto-char (point-max))
5268 (unless (bolp) (newline))
5269 (tramp-send-command
5271 (format
5272 (concat rem-dec " <<'EOF'\n%sEOF")
5273 (tramp-shell-quote-argument localname)
5274 (buffer-string)))
5275 (tramp-barf-unless-okay
5276 v nil
5277 "Couldn't write region to `%s', decode using `%s' failed"
5278 filename rem-dec)
5279 ;; When `file-precious-flag' is set, the region is
5280 ;; written to a temporary file. Check that the
5281 ;; checksum is equal to that from the local tmpfile.
5282 (when file-precious-flag
5283 (erase-buffer)
5284 (and
5285 ;; cksum runs locally, if possible.
5286 (zerop (tramp-local-call-process "cksum" tmpfile t))
5287 ;; cksum runs remotely.
5288 (zerop
5289 (tramp-send-command-and-check
5291 (format
5292 "cksum <%s"
5293 (tramp-shell-quote-argument localname))))
5294 ;; ... they are different.
5295 (not
5296 (string-equal
5297 (buffer-string)
5298 (with-current-buffer (tramp-get-buffer v)
5299 (buffer-string))))
5300 (tramp-error
5301 v 'file-error
5302 (concat "Couldn't write region to `%s',"
5303 " decode using `%s' failed")
5304 filename rem-dec)))))
5306 ;; Save exit.
5307 (delete-file tmpfile)))
5309 ;; That's not expected.
5311 (tramp-error
5312 v 'file-error
5313 (concat "Method `%s' should specify both encoding and "
5314 "decoding command or an rcp program")
5315 method))))
5317 ;; Make `last-coding-system-used' have the right value.
5318 (when coding-system-used
5319 (set 'last-coding-system-used coding-system-used))))
5321 (tramp-flush-file-property v (file-name-directory localname))
5322 (tramp-flush-file-property v localname)
5324 ;; We must protect `last-coding-system-used', now we have set it
5325 ;; to its correct value.
5326 (let (last-coding-system-used (need-chown t))
5327 ;; Set file modification time.
5328 (when (or (eq visit t) (stringp visit))
5329 (let ((file-attr (file-attributes filename)))
5330 (set-visited-file-modtime
5331 ;; We must pass modtime explicitely, because filename can
5332 ;; be different from (buffer-file-name), f.e. if
5333 ;; `file-precious-flag' is set.
5334 (nth 5 file-attr))
5335 (when (and (eq (nth 2 file-attr) uid)
5336 (eq (nth 3 file-attr) gid))
5337 (setq need-chown nil))))
5339 ;; Set the ownership.
5340 (when need-chown
5341 (tramp-set-file-uid-gid filename uid gid))
5342 (when (or (eq visit t) (null visit) (stringp visit))
5343 (tramp-message v 0 "Wrote %s" filename))
5344 (run-hooks 'tramp-handle-write-region-hook)))))
5346 (defvar tramp-vc-registered-file-names nil
5347 "List used to collect file names, which are checked during `vc-registered'.")
5349 ;; VC backends check for the existence of various different special
5350 ;; files. This is very time consuming, because every single check
5351 ;; requires a remote command (the file cache must be invalidated).
5352 ;; Therefore, we apply a kind of optimization. We install the file
5353 ;; name handler `tramp-vc-file-name-handler', which does nothing but
5354 ;; remembers all file names for which `file-exists-p' or
5355 ;; `file-readable-p' has been applied. A first run of `vc-registered'
5356 ;; is performed. Afterwards, a script is applied for all collected
5357 ;; file names, using just one remote command. The result of this
5358 ;; script is used to fill the file cache with actual values. Now we
5359 ;; can reset the file name handlers, and we make a second run of
5360 ;; `vc-registered', which returns the expected result without sending
5361 ;; any other remote command.
5362 (defun tramp-handle-vc-registered (file)
5363 "Like `vc-registered' for Tramp files."
5364 (with-temp-message ""
5365 (with-parsed-tramp-file-name file nil
5366 (with-progress-reporter
5367 v 3 (format "Checking `vc-registered' for %s" file)
5369 ;; There could be new files, created by the vc backend. We
5370 ;; cannot reuse the old cache entries, therefore.
5371 (let (tramp-vc-registered-file-names
5372 (tramp-cache-inhibit-cache (current-time))
5373 (file-name-handler-alist
5374 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
5376 ;; Here we collect only file names, which need an operation.
5377 (tramp-run-real-handler 'vc-registered (list file))
5378 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
5380 ;; Send just one command, in order to fill the cache.
5381 (when tramp-vc-registered-file-names
5382 (tramp-maybe-send-script
5384 (format tramp-vc-registered-read-file-names
5385 (tramp-get-file-exists-command v)
5386 (format "%s -r" (tramp-get-test-command v)))
5387 "tramp_vc_registered_read_file_names")
5389 (dolist
5390 (elt
5391 (tramp-send-command-and-read
5393 (format
5394 "tramp_vc_registered_read_file_names %s"
5395 (mapconcat 'tramp-shell-quote-argument
5396 tramp-vc-registered-file-names
5397 " "))))
5399 (tramp-set-file-property
5400 v (car elt) (cadr elt) (cadr (cdr elt))))))
5402 ;; Second run. Now all `file-exists-p' or `file-readable-p'
5403 ;; calls shall be answered from the file cache. We unset
5404 ;; `process-file-side-effects' in order to keep the cache when
5405 ;; `process-file' calls appear.
5406 (let (process-file-side-effects)
5407 (tramp-run-real-handler 'vc-registered (list file)))))))
5409 ;;;###autoload
5410 (progn (defun tramp-run-real-handler (operation args)
5411 "Invoke normal file name handler for OPERATION.
5412 First arg specifies the OPERATION, second arg is a list of arguments to
5413 pass to the OPERATION."
5414 (let* ((inhibit-file-name-handlers
5415 `(tramp-file-name-handler
5416 tramp-vc-file-name-handler
5417 tramp-completion-file-name-handler
5418 cygwin-mount-name-hook-function
5419 cygwin-mount-map-drive-hook-function
5421 ,(and (eq inhibit-file-name-operation operation)
5422 inhibit-file-name-handlers)))
5423 (inhibit-file-name-operation operation))
5424 (apply operation args))))
5426 ;;;###autoload
5427 (progn (defun tramp-completion-run-real-handler (operation args)
5428 "Invoke `tramp-file-name-handler' for OPERATION.
5429 First arg specifies the OPERATION, second arg is a list of arguments to
5430 pass to the OPERATION."
5431 (let* ((inhibit-file-name-handlers
5432 `(tramp-completion-file-name-handler
5433 cygwin-mount-name-hook-function
5434 cygwin-mount-map-drive-hook-function
5436 ,(and (eq inhibit-file-name-operation operation)
5437 inhibit-file-name-handlers)))
5438 (inhibit-file-name-operation operation))
5439 (apply operation args))))
5441 ;; We handle here all file primitives. Most of them have the file
5442 ;; name as first parameter; nevertheless we check for them explicitly
5443 ;; in order to be signaled if a new primitive appears. This
5444 ;; scenario is needed because there isn't a way to decide by
5445 ;; syntactical means whether a foreign method must be called. It would
5446 ;; ease the life if `file-name-handler-alist' would support a decision
5447 ;; function as well but regexp only.
5448 (defun tramp-file-name-for-operation (operation &rest args)
5449 "Return file name related to OPERATION file primitive.
5450 ARGS are the arguments OPERATION has been called with."
5451 (cond
5452 ;; FILE resp DIRECTORY.
5453 ((member operation
5454 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
5455 'delete-file 'diff-latest-backup-file 'directory-file-name
5456 'directory-files 'directory-files-and-attributes
5457 'dired-compress-file 'dired-uncache
5458 'file-accessible-directory-p 'file-attributes
5459 'file-directory-p 'file-executable-p 'file-exists-p
5460 'file-local-copy 'file-remote-p 'file-modes
5461 'file-name-as-directory 'file-name-directory
5462 'file-name-nondirectory 'file-name-sans-versions
5463 'file-ownership-preserved-p 'file-readable-p
5464 'file-regular-p 'file-symlink-p 'file-truename
5465 'file-writable-p 'find-backup-file-name 'find-file-noselect
5466 'get-file-buffer 'insert-directory 'insert-file-contents
5467 'load 'make-directory 'make-directory-internal
5468 'set-file-modes 'substitute-in-file-name
5469 'unhandled-file-name-directory 'vc-registered
5470 ;; Emacs 22+ only.
5471 'set-file-times
5472 ;; Emacs 24+ only.
5473 'file-selinux-context 'set-file-selinux-context
5474 ;; XEmacs only.
5475 'abbreviate-file-name 'create-file-buffer
5476 'dired-file-modtime 'dired-make-compressed-filename
5477 'dired-recursive-delete-directory 'dired-set-file-modtime
5478 'dired-shell-unhandle-file-name 'dired-uucode-file
5479 'insert-file-contents-literally 'make-temp-name 'recover-file
5480 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
5481 (if (file-name-absolute-p (nth 0 args))
5482 (nth 0 args)
5483 (expand-file-name (nth 0 args))))
5484 ;; FILE DIRECTORY resp FILE1 FILE2.
5485 ((member operation
5486 (list 'add-name-to-file 'copy-file 'expand-file-name
5487 'file-name-all-completions 'file-name-completion
5488 'file-newer-than-file-p 'make-symbolic-link 'rename-file
5489 ;; Emacs 23+ only.
5490 'copy-directory
5491 ;; XEmacs only.
5492 'dired-make-relative-symlink
5493 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
5494 (save-match-data
5495 (cond
5496 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
5497 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
5498 (t (buffer-file-name (current-buffer))))))
5499 ;; START END FILE.
5500 ((eq operation 'write-region)
5501 (nth 2 args))
5502 ;; BUFFER.
5503 ((member operation
5504 (list 'set-visited-file-modtime 'verify-visited-file-modtime
5505 ;; Emacs 22+ only.
5506 'make-auto-save-file-name
5507 ;; XEmacs only.
5508 'backup-buffer))
5509 (buffer-file-name
5510 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
5511 ;; COMMAND.
5512 ((member operation
5513 (list ;; not in Emacs 23+.
5514 'dired-call-process
5515 ;; Emacs only.
5516 'shell-command
5517 ;; Emacs 22+ only.
5518 'process-file
5519 ;; Emacs 23+ only.
5520 'start-file-process
5521 ;; XEmacs only.
5522 'dired-print-file 'dired-shell-call-process
5523 ;; nowhere yet.
5524 'executable-find 'start-process
5525 'call-process 'call-process-region))
5526 default-directory)
5527 ;; Unknown file primitive.
5528 (t (error "unknown file I/O primitive: %s" operation))))
5530 (defun tramp-find-foreign-file-name-handler (filename)
5531 "Return foreign file name handler if exists."
5532 (when (tramp-tramp-file-p filename)
5533 (let ((v (tramp-dissect-file-name filename t))
5534 (handler tramp-foreign-file-name-handler-alist)
5535 elt res)
5536 ;; When we are not fully sure that filename completion is safe,
5537 ;; we should not return a handler.
5538 (when (or (tramp-file-name-method v) (tramp-file-name-user v)
5539 (and (tramp-file-name-host v)
5540 (not (member (tramp-file-name-host v)
5541 (mapcar 'car tramp-methods))))
5542 (not (tramp-completion-mode-p)))
5543 (while handler
5544 (setq elt (car handler)
5545 handler (cdr handler))
5546 (when (funcall (car elt) filename)
5547 (setq handler nil
5548 res (cdr elt))))
5549 res))))
5551 ;; Main function.
5552 ;;;###autoload
5553 (defun tramp-file-name-handler (operation &rest args)
5554 "Invoke Tramp file name handler.
5555 Falls back to normal file name handler if no Tramp file name handler exists."
5556 (if tramp-mode
5557 (save-match-data
5558 (let* ((filename
5559 (tramp-replace-environment-variables
5560 (apply 'tramp-file-name-for-operation operation args)))
5561 (completion (tramp-completion-mode-p))
5562 (foreign (tramp-find-foreign-file-name-handler filename)))
5563 (with-parsed-tramp-file-name filename nil
5564 ;; Call the backend function.
5565 (if foreign
5566 (condition-case err
5567 (apply foreign operation args)
5569 ;; Trace that somebody has interrupted the
5570 ;; operation.
5571 (quit
5572 (let (tramp-message-show-message)
5573 (tramp-message
5574 v 1 "Interrupt received in operation %s"
5575 (append (list operation) args)))
5576 ;; Propagate the quit signal.
5577 (signal (car err) (cdr err)))
5579 ;; When we are in completion mode, some failed
5580 ;; operations shall return at least a default value
5581 ;; in order to give the user a chance to correct the
5582 ;; file name in the minibuffer.
5583 (error
5584 (cond
5585 ((and completion (zerop (length localname))
5586 (memq operation '(file-exists-p file-directory-p)))
5588 ((and completion (zerop (length localname))
5589 (memq operation
5590 '(expand-file-name file-name-as-directory)))
5591 filename)
5592 ;; Propagate the error.
5593 (t (signal (car err) (cdr err))))))
5595 ;; Nothing to do for us.
5596 (tramp-run-real-handler operation args)))))
5598 ;; When `tramp-mode' is not enabled, we don't do anything.
5599 (tramp-run-real-handler operation args)))
5601 ;; In Emacs, there is some concurrency due to timers. If a timer
5602 ;; interrupts Tramp and wishes to use the same connection buffer as
5603 ;; the "main" Emacs, then garbage might occur in the connection
5604 ;; buffer. Therefore, we need to make sure that a timer does not use
5605 ;; the same connection buffer as the "main" Emacs. We implement a
5606 ;; cheap global lock, instead of locking each connection buffer
5607 ;; separately. The global lock is based on two variables,
5608 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
5609 ;; (with setq) to indicate a lock. But Tramp also calls itself during
5610 ;; processing of a single file operation, so we need to allow
5611 ;; recursive calls. That's where the `tramp-locker' variable comes in
5612 ;; -- it is let-bound to t during the execution of the current
5613 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
5614 ;; then we should just proceed because we have been called
5615 ;; recursively. But if `tramp-locker' is nil, then we are a timer
5616 ;; interrupting the "main" Emacs, and then we signal an error.
5618 (defvar tramp-locked nil
5619 "If non-nil, then Tramp is currently busy.
5620 Together with `tramp-locker', this implements a locking mechanism
5621 preventing reentrant calls of Tramp.")
5623 (defvar tramp-locker nil
5624 "If non-nil, then a caller has locked Tramp.
5625 Together with `tramp-locked', this implements a locking mechanism
5626 preventing reentrant calls of Tramp.")
5628 (defun tramp-sh-file-name-handler (operation &rest args)
5629 "Invoke remote-shell Tramp file name handler.
5630 Fall back to normal file name handler if no Tramp handler exists."
5631 (when (and tramp-locked (not tramp-locker))
5632 (setq tramp-locked nil)
5633 (signal 'file-error (list "Forbidden reentrant call of Tramp")))
5634 (let ((tl tramp-locked))
5635 (unwind-protect
5636 (progn
5637 (setq tramp-locked t)
5638 (let ((tramp-locker t))
5639 (save-match-data
5640 (let ((fn (assoc operation tramp-file-name-handler-alist)))
5641 (if fn
5642 (apply (cdr fn) args)
5643 (tramp-run-real-handler operation args))))))
5644 (setq tramp-locked tl))))
5646 (defun tramp-vc-file-name-handler (operation &rest args)
5647 "Invoke special file name handler, which collects files to be handled."
5648 (save-match-data
5649 (let ((filename
5650 (tramp-replace-environment-variables
5651 (apply 'tramp-file-name-for-operation operation args)))
5652 (fn (assoc operation tramp-file-name-handler-alist)))
5653 (with-parsed-tramp-file-name filename nil
5654 (cond
5655 ;; That's what we want: file names, for which checks are
5656 ;; applied. We assume, that VC uses only `file-exists-p' and
5657 ;; `file-readable-p' checks; otherwise we must extend the
5658 ;; list. We do not perform any action, but return nil, in
5659 ;; order to keep `vc-registered' running.
5660 ((and fn (memq operation '(file-exists-p file-readable-p)))
5661 (add-to-list 'tramp-vc-registered-file-names localname 'append)
5662 nil)
5663 ;; Tramp file name handlers like `expand-file-name'. They
5664 ;; must still work.
5666 (save-match-data (apply (cdr fn) args)))
5667 ;; Default file name handlers, we don't care.
5668 (t (tramp-run-real-handler operation args)))))))
5670 ;;;###autoload
5671 (progn (defun tramp-completion-file-name-handler (operation &rest args)
5672 "Invoke Tramp file name completion handler.
5673 Falls back to normal file name handler if no Tramp file name handler exists."
5674 ;; We bind `directory-sep-char' here for XEmacs on Windows, which
5675 ;; would otherwise use backslash.
5676 (let ((directory-sep-char ?/)
5677 (fn (assoc operation tramp-completion-file-name-handler-alist)))
5678 (if (and
5679 ;; When `tramp-mode' is not enabled, we don't do anything.
5680 fn tramp-mode
5681 ;; For other syntaxes than `sep', the regexp matches many common
5682 ;; situations where the user doesn't actually want to use Tramp.
5683 ;; So to avoid autoloading Tramp after typing just "/s", we
5684 ;; disable this part of the completion, unless the user implicitly
5685 ;; indicated his interest in using a fancier completion system.
5686 (or (eq tramp-syntax 'sep)
5687 (featurep 'tramp) ;; If it's loaded, we may as well use it.
5688 ;; `partial-completion-mode' does not exist in XEmacs.
5689 ;; It is obsoleted with Emacs 24.1.
5690 (and (boundp 'partial-completion-mode)
5691 (symbol-value 'partial-completion-mode))
5692 ;; FIXME: These may have been loaded even if the user never
5693 ;; intended to use them.
5694 (featurep 'ido)
5695 (featurep 'icicles)))
5696 (save-match-data (apply (cdr fn) args))
5697 (tramp-completion-run-real-handler operation args)))))
5699 ;;;###autoload
5700 (progn (defun tramp-register-file-name-handlers ()
5701 "Add Tramp file name handlers to `file-name-handler-alist'."
5702 ;; Remove autoloaded handlers from file name handler alist. Useful,
5703 ;; if `tramp-syntax' has been changed.
5704 (let ((a1 (rassq 'tramp-file-name-handler file-name-handler-alist)))
5705 (setq file-name-handler-alist (delq a1 file-name-handler-alist)))
5706 (let ((a1 (rassq
5707 'tramp-completion-file-name-handler file-name-handler-alist)))
5708 (setq file-name-handler-alist (delq a1 file-name-handler-alist)))
5709 ;; Add the handlers.
5710 (add-to-list 'file-name-handler-alist
5711 (cons tramp-file-name-regexp 'tramp-file-name-handler))
5712 (put 'tramp-file-name-handler 'safe-magic t)
5713 (add-to-list 'file-name-handler-alist
5714 (cons tramp-completion-file-name-regexp
5715 'tramp-completion-file-name-handler))
5716 (put 'tramp-completion-file-name-handler 'safe-magic t)
5717 ;; If jka-compr or epa-file are already loaded, move them to the
5718 ;; front of `file-name-handler-alist'.
5719 (dolist (fnh '(epa-file-handler jka-compr-handler))
5720 (let ((entry (rassoc fnh file-name-handler-alist)))
5721 (when entry
5722 (setq file-name-handler-alist
5723 (cons entry (delete entry file-name-handler-alist))))))))
5725 ;; `tramp-file-name-handler' must be registered before evaluation of
5726 ;; site-start and init files, because there might exist remote files
5727 ;; already, f.e. files kept via recentf-mode.
5728 ;;;###autoload(tramp-register-file-name-handlers)
5729 (tramp-register-file-name-handlers)
5731 ;;;###autoload
5732 (defun tramp-unload-file-name-handlers ()
5733 (setq file-name-handler-alist
5734 (delete (rassoc 'tramp-file-name-handler
5735 file-name-handler-alist)
5736 (delete (rassoc 'tramp-completion-file-name-handler
5737 file-name-handler-alist)
5738 file-name-handler-alist))))
5740 (add-hook 'tramp-unload-hook 'tramp-unload-file-name-handlers)
5742 ;;; File name handler functions for completion mode:
5744 (defvar tramp-completion-mode nil
5745 "If non-nil, external packages signal that they are in file name completion.
5747 This is necessary, because Tramp uses a heuristic depending on last
5748 input event. This fails when external packages use other characters
5749 but <TAB>, <SPACE> or ?\\? for file name completion. This variable
5750 should never be set globally, the intention is to let-bind it.")
5752 ;; Necessary because `tramp-file-name-regexp-unified' and
5753 ;; `tramp-completion-file-name-regexp-unified' aren't different. If
5754 ;; nil, `tramp-completion-run-real-handler' is called (i.e. forwarding
5755 ;; to `tramp-file-name-handler'). Otherwise, it takes
5756 ;; `tramp-run-real-handler'. Using `last-input-event' is a little bit
5757 ;; risky, because completing a file might require loading other files,
5758 ;; like "~/.netrc", and for them it shouldn't be decided based on that
5759 ;; variable. On the other hand, those files shouldn't have partial
5760 ;; Tramp file name syntax. Maybe another variable should be introduced
5761 ;; overwriting this check in such cases. Or we change Tramp file name
5762 ;; syntax in order to avoid ambiguities, like in XEmacs ...
5763 (defun tramp-completion-mode-p ()
5764 "Check, whether method / user name / host name completion is active."
5766 ;; Signal from outside. `non-essential' has been introduced in Emacs 24.
5767 (and (boundp 'non-essential) (symbol-value 'non-essential))
5768 tramp-completion-mode
5769 ;; Emacs.
5770 (equal last-input-event 'tab)
5771 (and (natnump last-input-event)
5773 ;; ?\t has event-modifier 'control.
5774 (equal last-input-event ?\t)
5775 (and (not (event-modifiers last-input-event))
5776 (or (equal last-input-event ?\?)
5777 (equal last-input-event ?\ )))))
5778 ;; XEmacs.
5779 (and (featurep 'xemacs)
5780 ;; `last-input-event' might be nil.
5781 (not (null last-input-event))
5782 ;; `last-input-event' may have no character approximation.
5783 (tramp-compat-funcall 'event-to-character last-input-event)
5785 ;; ?\t has event-modifier 'control.
5786 (equal
5787 (tramp-compat-funcall 'event-to-character last-input-event) ?\t)
5788 (and (not (event-modifiers last-input-event))
5789 (or (equal
5790 (tramp-compat-funcall 'event-to-character last-input-event)
5791 ?\?)
5792 (equal
5793 (tramp-compat-funcall 'event-to-character last-input-event)
5794 ?\ )))))))
5796 (defun tramp-connectable-p (filename)
5797 "Check, whether it is possible to connect the remote host w/o side-effects.
5798 This is true, if either the remote host is already connected, or if we are
5799 not in completion mode."
5800 (and (tramp-tramp-file-p filename)
5801 (with-parsed-tramp-file-name filename nil
5802 (or (get-buffer (tramp-buffer-name v))
5803 (not (tramp-completion-mode-p))))))
5805 ;; Method, host name and user name completion.
5806 ;; `tramp-completion-dissect-file-name' returns a list of
5807 ;; tramp-file-name structures. For all of them we return possible completions.
5808 ;;;###autoload
5809 (defun tramp-completion-handle-file-name-all-completions (filename directory)
5810 "Like `file-name-all-completions' for partial Tramp files."
5812 (let* ((fullname (tramp-drop-volume-letter
5813 (expand-file-name filename directory)))
5814 ;; Possible completion structures.
5815 (v (tramp-completion-dissect-file-name fullname))
5816 result result1)
5818 (while v
5819 (let* ((car (car v))
5820 (method (tramp-file-name-method car))
5821 (user (tramp-file-name-user car))
5822 (host (tramp-file-name-host car))
5823 (localname (tramp-file-name-localname car))
5824 (m (tramp-find-method method user host))
5825 (tramp-current-user user) ; see `tramp-parse-passwd'
5826 all-user-hosts)
5828 (unless localname ;; Nothing to complete.
5830 (if (or user host)
5832 ;; Method dependent user / host combinations.
5833 (progn
5834 (mapc
5835 (lambda (x)
5836 (setq all-user-hosts
5837 (append all-user-hosts
5838 (funcall (nth 0 x) (nth 1 x)))))
5839 (tramp-get-completion-function m))
5841 (setq result
5842 (append result
5843 (mapcar
5844 (lambda (x)
5845 (tramp-get-completion-user-host
5846 method user host (nth 0 x) (nth 1 x)))
5847 (delq nil all-user-hosts)))))
5849 ;; Possible methods.
5850 (setq result
5851 (append result (tramp-get-completion-methods m)))))
5853 (setq v (cdr v))))
5855 ;; Unify list, remove nil elements.
5856 (while result
5857 (let ((car (car result)))
5858 (when car
5859 (add-to-list
5860 'result1
5861 (substring car (length (tramp-drop-volume-letter directory)))))
5862 (setq result (cdr result))))
5864 ;; Complete local parts.
5865 (append
5866 result1
5867 (condition-case nil
5868 (apply (if (tramp-connectable-p fullname)
5869 'tramp-completion-run-real-handler
5870 'tramp-run-real-handler)
5871 'file-name-all-completions (list (list filename directory)))
5872 (error nil)))))
5874 ;; Method, host name and user name completion for a file.
5875 ;;;###autoload
5876 (defun tramp-completion-handle-file-name-completion
5877 (filename directory &optional predicate)
5878 "Like `file-name-completion' for Tramp files."
5879 (try-completion
5880 filename
5881 (mapcar 'list (file-name-all-completions filename directory))
5882 (when (and predicate
5883 (tramp-connectable-p (expand-file-name filename directory)))
5884 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
5886 ;; I misuse a little bit the tramp-file-name structure in order to handle
5887 ;; completion possibilities for partial methods / user names / host names.
5888 ;; Return value is a list of tramp-file-name structures according to possible
5889 ;; completions. If "localname" is non-nil it means there
5890 ;; shouldn't be a completion anymore.
5892 ;; Expected results:
5894 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
5895 ;; [nil nil "x" nil] [nil "x" nil nil] [nil "x" "y" nil]
5896 ;; [nil "x" nil nil]
5897 ;; ["x" nil nil nil]
5899 ;; "/x:" "/x:y" "/x:y:"
5900 ;; [nil nil "x" ""] [nil nil "x" "y"] ["x" nil "y" ""]
5901 ;; "/[x/" "/[x/y"
5902 ;; ["x" nil "" nil] ["x" nil "y" nil]
5903 ;; ["x" "" nil nil] ["x" "y" nil nil]
5905 ;; "/x:y@" "/x:y@z" "/x:y@z:"
5906 ;; [nil nil "x" "y@"] [nil nil "x" "y@z"] ["x" "y" "z" ""]
5907 ;; "/[x/y@" "/[x/y@z"
5908 ;; ["x" nil "y" nil] ["x" "y" "z" nil]
5909 (defun tramp-completion-dissect-file-name (name)
5910 "Returns a list of `tramp-file-name' structures.
5911 They are collected by `tramp-completion-dissect-file-name1'."
5913 (let* ((result)
5914 (x-nil "\\|\\(\\)")
5915 (tramp-completion-ipv6-regexp
5916 (format
5917 "[^%s]*"
5918 (if (zerop (length tramp-postfix-ipv6-format))
5919 tramp-postfix-host-format
5920 tramp-postfix-ipv6-format)))
5921 ;; "/method" "/[method"
5922 (tramp-completion-file-name-structure1
5923 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
5924 1 nil nil nil))
5925 ;; "/user" "/[user"
5926 (tramp-completion-file-name-structure2
5927 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
5928 nil 1 nil nil))
5929 ;; "/host" "/[host"
5930 (tramp-completion-file-name-structure3
5931 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
5932 nil nil 1 nil))
5933 ;; "/[ipv6" "/[ipv6"
5934 (tramp-completion-file-name-structure4
5935 (list (concat tramp-prefix-regexp
5936 tramp-prefix-ipv6-regexp
5937 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5938 nil nil 1 nil))
5939 ;; "/user@host" "/[user@host"
5940 (tramp-completion-file-name-structure5
5941 (list (concat tramp-prefix-regexp
5942 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5943 "\\(" tramp-host-regexp x-nil "\\)$")
5944 nil 1 2 nil))
5945 ;; "/user@[ipv6" "/[user@ipv6"
5946 (tramp-completion-file-name-structure6
5947 (list (concat tramp-prefix-regexp
5948 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5949 tramp-prefix-ipv6-regexp
5950 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5951 nil 1 2 nil))
5952 ;; "/method:user" "/[method/user" "/method://user"
5953 (tramp-completion-file-name-structure7
5954 (list (concat tramp-prefix-regexp
5955 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5956 "\\(" tramp-user-regexp x-nil "\\)$")
5957 1 2 nil nil))
5958 ;; "/method:host" "/[method/host" "/method://host"
5959 (tramp-completion-file-name-structure8
5960 (list (concat tramp-prefix-regexp
5961 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5962 "\\(" tramp-host-regexp x-nil "\\)$")
5963 1 nil 2 nil))
5964 ;; "/method:[ipv6" "/[method/ipv6" "/method://[ipv6"
5965 (tramp-completion-file-name-structure9
5966 (list (concat tramp-prefix-regexp
5967 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5968 tramp-prefix-ipv6-regexp
5969 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5970 1 nil 2 nil))
5971 ;; "/method:user@host" "/[method/user@host" "/method://user@host"
5972 (tramp-completion-file-name-structure10
5973 (list (concat tramp-prefix-regexp
5974 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5975 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5976 "\\(" tramp-host-regexp x-nil "\\)$")
5977 1 2 3 nil))
5978 ;; "/method:user@[ipv6" "/[method/user@ipv6" "/method://user@[ipv6"
5979 (tramp-completion-file-name-structure11
5980 (list (concat tramp-prefix-regexp
5981 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5982 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5983 tramp-prefix-ipv6-regexp
5984 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5985 1 2 3 nil))
5986 ;; "/method: "/method:/"
5987 (tramp-completion-file-name-structure12
5988 (list
5989 (if (equal tramp-syntax 'url)
5990 (concat tramp-prefix-regexp
5991 "\\(" tramp-method-regexp "\\)"
5992 "\\(" (substring tramp-postfix-method-regexp 0 1)
5993 "\\|" (substring tramp-postfix-method-regexp 1 2) "\\)"
5994 "\\(" "\\)$")
5995 ;; Should not match if not URL syntax.
5996 (concat tramp-prefix-regexp "/$"))
5997 1 3 nil nil))
5998 ;; "/method: "/method:/"
5999 (tramp-completion-file-name-structure13
6000 (list
6001 (if (equal tramp-syntax 'url)
6002 (concat tramp-prefix-regexp
6003 "\\(" tramp-method-regexp "\\)"
6004 "\\(" (substring tramp-postfix-method-regexp 0 1)
6005 "\\|" (substring tramp-postfix-method-regexp 1 2) "\\)"
6006 "\\(" "\\)$")
6007 ;; Should not match if not URL syntax.
6008 (concat tramp-prefix-regexp "/$"))
6009 1 nil 3 nil)))
6011 (mapc (lambda (regexp)
6012 (add-to-list 'result
6013 (tramp-completion-dissect-file-name1 regexp name)))
6014 (list
6015 tramp-completion-file-name-structure1
6016 tramp-completion-file-name-structure2
6017 tramp-completion-file-name-structure3
6018 tramp-completion-file-name-structure4
6019 tramp-completion-file-name-structure5
6020 tramp-completion-file-name-structure6
6021 tramp-completion-file-name-structure7
6022 tramp-completion-file-name-structure8
6023 tramp-completion-file-name-structure9
6024 tramp-completion-file-name-structure10
6025 tramp-completion-file-name-structure11
6026 tramp-completion-file-name-structure12
6027 tramp-completion-file-name-structure13
6028 tramp-file-name-structure))
6030 (delq nil result)))
6032 (defun tramp-completion-dissect-file-name1 (structure name)
6033 "Returns a `tramp-file-name' structure matching STRUCTURE.
6034 The structure consists of remote method, remote user,
6035 remote host and localname (filename on remote host)."
6037 (save-match-data
6038 (when (string-match (nth 0 structure) name)
6039 (let ((method (and (nth 1 structure)
6040 (match-string (nth 1 structure) name)))
6041 (user (and (nth 2 structure)
6042 (match-string (nth 2 structure) name)))
6043 (host (and (nth 3 structure)
6044 (match-string (nth 3 structure) name)))
6045 (localname (and (nth 4 structure)
6046 (match-string (nth 4 structure) name))))
6047 (vector method user host localname)))))
6049 ;; This function returns all possible method completions, adding the
6050 ;; trailing method delimeter.
6051 (defun tramp-get-completion-methods (partial-method)
6052 "Returns all method completions for PARTIAL-METHOD."
6053 (mapcar
6054 (lambda (method)
6055 (and method
6056 (string-match (concat "^" (regexp-quote partial-method)) method)
6057 (tramp-completion-make-tramp-file-name method nil nil nil)))
6058 (mapcar 'car tramp-methods)))
6060 ;; Compares partial user and host names with possible completions.
6061 (defun tramp-get-completion-user-host (method partial-user partial-host user host)
6062 "Returns the most expanded string for user and host name completion.
6063 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
6064 (cond
6066 ((and partial-user partial-host)
6067 (if (and host
6068 (string-match (concat "^" (regexp-quote partial-host)) host)
6069 (string-equal partial-user (or user partial-user)))
6070 (setq user partial-user)
6071 (setq user nil
6072 host nil)))
6074 (partial-user
6075 (setq host nil)
6076 (unless
6077 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
6078 (setq user nil)))
6080 (partial-host
6081 (setq user nil)
6082 (unless
6083 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
6084 (setq host nil)))
6086 (t (setq user nil
6087 host nil)))
6089 (unless (zerop (+ (length user) (length host)))
6090 (tramp-completion-make-tramp-file-name method user host nil)))
6092 (defun tramp-parse-rhosts (filename)
6093 "Return a list of (user host) tuples allowed to access.
6094 Either user or host may be nil."
6095 ;; On Windows, there are problems in completion when
6096 ;; `default-directory' is remote.
6097 (let ((default-directory (tramp-compat-temporary-file-directory))
6098 res)
6099 (when (file-readable-p filename)
6100 (with-temp-buffer
6101 (insert-file-contents filename)
6102 (goto-char (point-min))
6103 (while (not (eobp))
6104 (push (tramp-parse-rhosts-group) res))))
6105 res))
6107 (defun tramp-parse-rhosts-group ()
6108 "Return a (user host) tuple allowed to access.
6109 Either user or host may be nil."
6110 (let ((result)
6111 (regexp
6112 (concat
6113 "^\\(" tramp-host-regexp "\\)"
6114 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
6115 (narrow-to-region (point) (tramp-compat-line-end-position))
6116 (when (re-search-forward regexp nil t)
6117 (setq result (append (list (match-string 3) (match-string 1)))))
6118 (widen)
6119 (forward-line 1)
6120 result))
6122 (defun tramp-parse-shosts (filename)
6123 "Return a list of (user host) tuples allowed to access.
6124 User is always nil."
6125 ;; On Windows, there are problems in completion when
6126 ;; `default-directory' is remote.
6127 (let ((default-directory (tramp-compat-temporary-file-directory))
6128 res)
6129 (when (file-readable-p filename)
6130 (with-temp-buffer
6131 (insert-file-contents filename)
6132 (goto-char (point-min))
6133 (while (not (eobp))
6134 (push (tramp-parse-shosts-group) res))))
6135 res))
6137 (defun tramp-parse-shosts-group ()
6138 "Return a (user host) tuple allowed to access.
6139 User is always nil."
6140 (let ((result)
6141 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
6142 (narrow-to-region (point) (tramp-compat-line-end-position))
6143 (when (re-search-forward regexp nil t)
6144 (setq result (list nil (match-string 1))))
6145 (widen)
6147 (> (skip-chars-forward ",") 0)
6148 (forward-line 1))
6149 result))
6151 (defun tramp-parse-sconfig (filename)
6152 "Return a list of (user host) tuples allowed to access.
6153 User is always nil."
6154 ;; On Windows, there are problems in completion when
6155 ;; `default-directory' is remote.
6156 (let ((default-directory (tramp-compat-temporary-file-directory))
6157 res)
6158 (when (file-readable-p filename)
6159 (with-temp-buffer
6160 (insert-file-contents filename)
6161 (goto-char (point-min))
6162 (while (not (eobp))
6163 (push (tramp-parse-sconfig-group) res))))
6164 res))
6166 (defun tramp-parse-sconfig-group ()
6167 "Return a (user host) tuple allowed to access.
6168 User is always nil."
6169 (let ((result)
6170 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
6171 (narrow-to-region (point) (tramp-compat-line-end-position))
6172 (when (re-search-forward regexp nil t)
6173 (setq result (list nil (match-string 1))))
6174 (widen)
6176 (> (skip-chars-forward ",") 0)
6177 (forward-line 1))
6178 result))
6180 (defun tramp-parse-shostkeys (dirname)
6181 "Return a list of (user host) tuples allowed to access.
6182 User is always nil."
6183 ;; On Windows, there are problems in completion when
6184 ;; `default-directory' is remote.
6185 (let* ((default-directory (tramp-compat-temporary-file-directory))
6186 (regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
6187 (files (when (file-directory-p dirname) (directory-files dirname)))
6188 result)
6189 (while files
6190 (when (string-match regexp (car files))
6191 (push (list nil (match-string 1 (car files))) result))
6192 (setq files (cdr files)))
6193 result))
6195 (defun tramp-parse-sknownhosts (dirname)
6196 "Return a list of (user host) tuples allowed to access.
6197 User is always nil."
6198 ;; On Windows, there are problems in completion when
6199 ;; `default-directory' is remote.
6200 (let* ((default-directory (tramp-compat-temporary-file-directory))
6201 (regexp (concat "^\\(" tramp-host-regexp
6202 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
6203 (files (when (file-directory-p dirname) (directory-files dirname)))
6204 result)
6205 (while files
6206 (when (string-match regexp (car files))
6207 (push (list nil (match-string 1 (car files))) result))
6208 (setq files (cdr files)))
6209 result))
6211 (defun tramp-parse-hosts (filename)
6212 "Return a list of (user host) tuples allowed to access.
6213 User is always nil."
6214 ;; On Windows, there are problems in completion when
6215 ;; `default-directory' is remote.
6216 (let ((default-directory (tramp-compat-temporary-file-directory))
6217 res)
6218 (when (file-readable-p filename)
6219 (with-temp-buffer
6220 (insert-file-contents filename)
6221 (goto-char (point-min))
6222 (while (not (eobp))
6223 (push (tramp-parse-hosts-group) res))))
6224 res))
6226 (defun tramp-parse-hosts-group ()
6227 "Return a (user host) tuple allowed to access.
6228 User is always nil."
6229 (let ((result)
6230 (regexp
6231 (concat "^\\(" tramp-ipv6-regexp "\\|" tramp-host-regexp "\\)")))
6232 (narrow-to-region (point) (tramp-compat-line-end-position))
6233 (when (re-search-forward regexp nil t)
6234 (setq result (list nil (match-string 1))))
6235 (widen)
6237 (> (skip-chars-forward " \t") 0)
6238 (forward-line 1))
6239 result))
6241 ;; For su-alike methods it would be desirable to return "root@localhost"
6242 ;; as default. Unfortunately, we have no information whether any user name
6243 ;; has been typed already. So we use `tramp-current-user' as indication,
6244 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
6245 (defun tramp-parse-passwd (filename)
6246 "Return a list of (user host) tuples allowed to access.
6247 Host is always \"localhost\"."
6248 ;; On Windows, there are problems in completion when
6249 ;; `default-directory' is remote.
6250 (let ((default-directory (tramp-compat-temporary-file-directory))
6251 res)
6252 (if (zerop (length tramp-current-user))
6253 '(("root" nil))
6254 (when (file-readable-p filename)
6255 (with-temp-buffer
6256 (insert-file-contents filename)
6257 (goto-char (point-min))
6258 (while (not (eobp))
6259 (push (tramp-parse-passwd-group) res))))
6260 res)))
6262 (defun tramp-parse-passwd-group ()
6263 "Return a (user host) tuple allowed to access.
6264 Host is always \"localhost\"."
6265 (let ((result)
6266 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
6267 (narrow-to-region (point) (tramp-compat-line-end-position))
6268 (when (re-search-forward regexp nil t)
6269 (setq result (list (match-string 1) "localhost")))
6270 (widen)
6271 (forward-line 1)
6272 result))
6274 (defun tramp-parse-netrc (filename)
6275 "Return a list of (user host) tuples allowed to access.
6276 User may be nil."
6277 ;; On Windows, there are problems in completion when
6278 ;; `default-directory' is remote.
6279 (let ((default-directory (tramp-compat-temporary-file-directory))
6280 res)
6281 (when (file-readable-p filename)
6282 (with-temp-buffer
6283 (insert-file-contents filename)
6284 (goto-char (point-min))
6285 (while (not (eobp))
6286 (push (tramp-parse-netrc-group) res))))
6287 res))
6289 (defun tramp-parse-netrc-group ()
6290 "Return a (user host) tuple allowed to access.
6291 User may be nil."
6292 (let ((result)
6293 (regexp
6294 (concat
6295 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
6296 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
6297 (narrow-to-region (point) (tramp-compat-line-end-position))
6298 (when (re-search-forward regexp nil t)
6299 (setq result (list (match-string 3) (match-string 1))))
6300 (widen)
6301 (forward-line 1)
6302 result))
6304 (defun tramp-parse-putty (registry)
6305 "Return a list of (user host) tuples allowed to access.
6306 User is always nil."
6307 ;; On Windows, there are problems in completion when
6308 ;; `default-directory' is remote.
6309 (let ((default-directory (tramp-compat-temporary-file-directory))
6310 res)
6311 (with-temp-buffer
6312 (when (zerop (tramp-local-call-process "reg" nil t nil "query" registry))
6313 (goto-char (point-min))
6314 (while (not (eobp))
6315 (push (tramp-parse-putty-group registry) res))))
6316 res))
6318 (defun tramp-parse-putty-group (registry)
6319 "Return a (user host) tuple allowed to access.
6320 User is always nil."
6321 (let ((result)
6322 (regexp (concat (regexp-quote registry) "\\\\\\(.+\\)")))
6323 (narrow-to-region (point) (tramp-compat-line-end-position))
6324 (when (re-search-forward regexp nil t)
6325 (setq result (list nil (match-string 1))))
6326 (widen)
6327 (forward-line 1)
6328 result))
6330 ;;; Internal Functions:
6332 (defun tramp-maybe-send-script (vec script name)
6333 "Define in remote shell function NAME implemented as SCRIPT.
6334 Only send the definition if it has not already been done."
6335 (let* ((p (tramp-get-connection-process vec))
6336 (scripts (tramp-get-connection-property p "scripts" nil)))
6337 (unless (member name scripts)
6338 (with-progress-reporter vec 5 (format "Sending script `%s'" name)
6339 ;; The script could contain a call of Perl. This is masked with `%s'.
6340 (tramp-send-command-and-check
6342 (format "%s () {\n%s\n}" name
6343 (format script (tramp-get-remote-perl vec))))
6344 (tramp-set-connection-property p "scripts" (cons name scripts))))))
6346 (defun tramp-set-auto-save ()
6347 (when (and ;; ange-ftp has its own auto-save mechanism
6348 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
6349 'tramp-sh-file-name-handler)
6350 auto-save-default)
6351 (auto-save-mode 1)))
6352 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
6353 (add-hook 'tramp-unload-hook
6354 (lambda ()
6355 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
6357 (defun tramp-run-test (switch filename)
6358 "Run `test' on the remote system, given a SWITCH and a FILENAME.
6359 Returns the exit code of the `test' program."
6360 (with-parsed-tramp-file-name filename nil
6361 (tramp-send-command-and-check
6363 (format
6364 "%s %s %s"
6365 (tramp-get-test-command v)
6366 switch
6367 (tramp-shell-quote-argument localname)))))
6369 (defun tramp-run-test2 (format-string file1 file2)
6370 "Run `test'-like program on the remote system, given FILE1, FILE2.
6371 FORMAT-STRING contains the program name, switches, and place holders.
6372 Returns the exit code of the `test' program. Barfs if the methods,
6373 hosts, or files, disagree."
6374 (unless (tramp-equal-remote file1 file2)
6375 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
6376 (tramp-error
6377 v 'file-error
6378 "tramp-run-test2 only implemented for same method, user, host")))
6379 (with-parsed-tramp-file-name file1 v1
6380 (with-parsed-tramp-file-name file1 v2
6381 (tramp-send-command-and-check
6383 (format format-string
6384 (tramp-shell-quote-argument v1-localname)
6385 (tramp-shell-quote-argument v2-localname))))))
6387 (defun tramp-buffer-name (vec)
6388 "A name for the connection buffer VEC."
6389 ;; We must use `tramp-file-name-real-host', because for gateway
6390 ;; methods the default port will be expanded later on, which would
6391 ;; tamper the name.
6392 (let ((method (tramp-file-name-method vec))
6393 (user (tramp-file-name-user vec))
6394 (host (tramp-file-name-real-host vec)))
6395 (if (not (zerop (length user)))
6396 (format "*tramp/%s %s@%s*" method user host)
6397 (format "*tramp/%s %s*" method host))))
6399 (defun tramp-delete-temp-file-function ()
6400 "Remove temporary files related to current buffer."
6401 (when (stringp tramp-temp-buffer-file-name)
6402 (condition-case nil
6403 (delete-file tramp-temp-buffer-file-name)
6404 (error nil))))
6406 (add-hook 'kill-buffer-hook 'tramp-delete-temp-file-function)
6407 (add-hook 'tramp-cache-unload-hook
6408 (lambda ()
6409 (remove-hook 'kill-buffer-hook
6410 'tramp-delete-temp-file-function)))
6412 (defun tramp-get-buffer (vec)
6413 "Get the connection buffer to be used for VEC."
6414 (or (get-buffer (tramp-buffer-name vec))
6415 (with-current-buffer (get-buffer-create (tramp-buffer-name vec))
6416 (setq buffer-undo-list t)
6417 (setq default-directory
6418 (tramp-make-tramp-file-name
6419 (tramp-file-name-method vec)
6420 (tramp-file-name-user vec)
6421 (tramp-file-name-host vec)
6422 "/"))
6423 (current-buffer))))
6425 (defun tramp-get-connection-buffer (vec)
6426 "Get the connection buffer to be used for VEC.
6427 In case a second asynchronous communication has been started, it is different
6428 from `tramp-get-buffer'."
6429 (or (tramp-get-connection-property vec "process-buffer" nil)
6430 (tramp-get-buffer vec)))
6432 (defun tramp-get-connection-process (vec)
6433 "Get the connection process to be used for VEC.
6434 In case a second asynchronous communication has been started, it is different
6435 from the default one."
6436 (get-process
6437 (or (tramp-get-connection-property vec "process-name" nil)
6438 (tramp-buffer-name vec))))
6440 (defun tramp-debug-buffer-name (vec)
6441 "A name for the debug buffer for VEC."
6442 ;; We must use `tramp-file-name-real-host', because for gateway
6443 ;; methods the default port will be expanded later on, which would
6444 ;; tamper the name.
6445 (let ((method (tramp-file-name-method vec))
6446 (user (tramp-file-name-user vec))
6447 (host (tramp-file-name-real-host vec)))
6448 (if (not (zerop (length user)))
6449 (format "*debug tramp/%s %s@%s*" method user host)
6450 (format "*debug tramp/%s %s*" method host))))
6452 (defconst tramp-debug-outline-regexp
6453 "[0-9]+:[0-9]+:[0-9]+\\.[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #")
6455 (defun tramp-get-debug-buffer (vec)
6456 "Get the debug buffer for VEC."
6457 (with-current-buffer
6458 (get-buffer-create (tramp-debug-buffer-name vec))
6459 (when (bobp)
6460 (setq buffer-undo-list t)
6461 ;; Activate `outline-mode'. This runs `text-mode-hook' and
6462 ;; `outline-mode-hook'. We must prevent that local processes
6463 ;; die. Yes: I've seen `flyspell-mode', which starts "ispell".
6464 ;; Furthermore, `outline-regexp' must have the correct value
6465 ;; already, because it is used by `font-lock-compile-keywords'.
6466 (let ((default-directory (tramp-compat-temporary-file-directory))
6467 (outline-regexp tramp-debug-outline-regexp))
6468 (outline-mode))
6469 (set (make-local-variable 'outline-regexp) tramp-debug-outline-regexp)
6470 (set (make-local-variable 'outline-level) 'tramp-outline-level))
6471 (current-buffer)))
6473 (defun tramp-outline-level ()
6474 "Return the depth to which a statement is nested in the outline.
6475 Point must be at the beginning of a header line.
6477 The outline level is equal to the verbosity of the Tramp message."
6478 (1+ (string-to-number (match-string 1))))
6480 (defun tramp-find-executable
6481 (vec progname dirlist &optional ignore-tilde ignore-path)
6482 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
6483 First arg VEC specifies the connection, PROGNAME is the program
6484 to search for, and DIRLIST gives the list of directories to
6485 search. If IGNORE-TILDE is non-nil, directory names starting
6486 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
6487 only in DIRLIST.
6489 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
6491 This function expects to be in the right *tramp* buffer."
6492 (with-current-buffer (tramp-get-connection-buffer vec)
6493 (let (result)
6494 ;; Check whether the executable is in $PATH. "which(1)" does not
6495 ;; report always a correct error code; therefore we check the
6496 ;; number of words it returns.
6497 (unless ignore-path
6498 (tramp-send-command vec (format "which \\%s | wc -w" progname))
6499 (goto-char (point-min))
6500 (if (looking-at "^\\s-*1$")
6501 (setq result (concat "\\" progname))))
6502 (unless result
6503 (when ignore-tilde
6504 ;; Remove all ~/foo directories from dirlist. In XEmacs,
6505 ;; `remove' is in CL, and we want to avoid CL dependencies.
6506 (let (newdl d)
6507 (while dirlist
6508 (setq d (car dirlist))
6509 (setq dirlist (cdr dirlist))
6510 (unless (char-equal ?~ (aref d 0))
6511 (setq newdl (cons d newdl))))
6512 (setq dirlist (nreverse newdl))))
6513 (tramp-send-command
6515 (format (concat "while read d; "
6516 "do if test -x $d/%s -a -f $d/%s; "
6517 "then echo tramp_executable $d/%s; "
6518 "break; fi; done <<'EOF'\n"
6519 "%s\nEOF")
6520 progname progname progname (mapconcat 'identity dirlist "\n")))
6521 (goto-char (point-max))
6522 (when (search-backward "tramp_executable " nil t)
6523 (skip-chars-forward "^ ")
6524 (skip-chars-forward " ")
6525 (setq result (buffer-substring
6526 (point) (tramp-compat-line-end-position)))))
6527 result)))
6529 (defun tramp-set-remote-path (vec)
6530 "Sets the remote environment PATH to existing directories.
6531 I.e., for each directory in `tramp-remote-path', it is tested
6532 whether it exists and if so, it is added to the environment
6533 variable PATH."
6534 (tramp-message vec 5 (format "Setting $PATH environment variable"))
6535 (tramp-send-command
6536 vec (format "PATH=%s; export PATH"
6537 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
6539 ;; ------------------------------------------------------------
6540 ;; -- Communication with external shell --
6541 ;; ------------------------------------------------------------
6543 (defun tramp-find-file-exists-command (vec)
6544 "Find a command on the remote host for checking if a file exists.
6545 Here, we are looking for a command which has zero exit status if the
6546 file exists and nonzero exit status otherwise."
6547 (let ((existing "/")
6548 (nonexisting
6549 (tramp-shell-quote-argument "/ this file does not exist "))
6550 result)
6551 ;; The algorithm is as follows: we try a list of several commands.
6552 ;; For each command, we first run `$cmd /' -- this should return
6553 ;; true, as the root directory always exists. And then we run
6554 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
6555 ;; does not exist. This should return false. We use the first
6556 ;; command we find that seems to work.
6557 ;; The list of commands to try is as follows:
6558 ;; `ls -d' This works on most systems, but NetBSD 1.4
6559 ;; has a bug: `ls' always returns zero exit
6560 ;; status, even for files which don't exist.
6561 ;; `test -e' Some Bourne shells have a `test' builtin
6562 ;; which does not know the `-e' option.
6563 ;; `/bin/test -e' For those, the `test' binary on disk normally
6564 ;; provides the option. Alas, the binary
6565 ;; is sometimes `/bin/test' and sometimes it's
6566 ;; `/usr/bin/test'.
6567 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
6568 (unless (or
6569 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
6570 (zerop (tramp-send-command-and-check
6571 vec (format "%s %s" result existing)))
6572 (not (zerop (tramp-send-command-and-check
6573 vec (format "%s %s" result nonexisting)))))
6574 (and (setq result "/bin/test -e")
6575 (zerop (tramp-send-command-and-check
6576 vec (format "%s %s" result existing)))
6577 (not (zerop (tramp-send-command-and-check
6578 vec (format "%s %s" result nonexisting)))))
6579 (and (setq result "/usr/bin/test -e")
6580 (zerop (tramp-send-command-and-check
6581 vec (format "%s %s" result existing)))
6582 (not (zerop (tramp-send-command-and-check
6583 vec (format "%s %s" result nonexisting)))))
6584 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
6585 (zerop (tramp-send-command-and-check
6586 vec (format "%s %s" result existing)))
6587 (not (zerop (tramp-send-command-and-check
6588 vec (format "%s %s" result nonexisting))))))
6589 (tramp-error
6590 vec 'file-error "Couldn't find command to check if file exists"))
6591 result))
6593 ;; CCC test ksh or bash found for tilde expansion?
6594 (defun tramp-find-shell (vec)
6595 "Opens a shell on the remote host which groks tilde expansion."
6596 (unless (tramp-get-connection-property vec "remote-shell" nil)
6597 (let (shell)
6598 (with-current-buffer (tramp-get-buffer vec)
6599 (tramp-send-command vec "echo ~root" t)
6600 (cond
6601 ((or (string-match "^~root$" (buffer-string))
6602 ;; The default shell (ksh93) of OpenSolaris is buggy.
6603 (string-equal (tramp-get-connection-property vec "uname" "")
6604 "SunOS 5.11"))
6605 (setq shell
6606 (or (tramp-find-executable
6607 vec "bash" (tramp-get-remote-path vec) t t)
6608 (tramp-find-executable
6609 vec "ksh" (tramp-get-remote-path vec) t t)))
6610 (unless shell
6611 (tramp-error
6612 vec 'file-error
6613 "Couldn't find a shell which groks tilde expansion"))
6614 ;; Find arguments for this shell.
6615 (let ((alist tramp-sh-extra-args)
6616 item extra-args)
6617 (while (and alist (null extra-args))
6618 (setq item (pop alist))
6619 (when (string-match (car item) shell)
6620 (setq extra-args (cdr item))))
6621 (when extra-args (setq shell (concat shell " " extra-args))))
6622 (tramp-message
6623 vec 5 "Starting remote shell `%s' for tilde expansion" shell)
6624 (let ((tramp-end-of-output tramp-initial-end-of-output))
6625 (tramp-send-command
6627 (format "PROMPT_COMMAND='' PS1=%s PS2='' PS3='' exec %s"
6628 (shell-quote-argument tramp-end-of-output) shell)
6630 ;; Setting prompts.
6631 (with-progress-reporter vec 5 (format "Setting remote shell prompt")
6632 (tramp-send-command
6633 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
6634 (tramp-send-command vec "PS2=''" t)
6635 (tramp-send-command vec "PS3=''" t)
6636 (tramp-send-command vec "PROMPT_COMMAND=''" t)))
6638 (t (tramp-message
6639 vec 5 "Remote `%s' groks tilde expansion, good"
6640 (tramp-set-connection-property
6641 vec "remote-shell"
6642 (tramp-get-method-parameter
6643 (tramp-file-name-method vec) 'tramp-remote-sh)))))))))
6645 ;; ------------------------------------------------------------
6646 ;; -- Functions for establishing connection --
6647 ;; ------------------------------------------------------------
6649 ;; The following functions are actions to be taken when seeing certain
6650 ;; prompts from the remote host. See the variable
6651 ;; `tramp-actions-before-shell' for usage of these functions.
6653 (defun tramp-action-login (proc vec)
6654 "Send the login name."
6655 (when (not (stringp tramp-current-user))
6656 (save-window-excursion
6657 (let ((enable-recursive-minibuffers t))
6658 (pop-to-buffer (tramp-get-connection-buffer vec))
6659 (setq tramp-current-user (read-string (match-string 0))))))
6660 (tramp-message vec 3 "Sending login name `%s'" tramp-current-user)
6661 (with-current-buffer (tramp-get-connection-buffer vec)
6662 (tramp-message vec 6 "\n%s" (buffer-string)))
6663 (tramp-send-string vec tramp-current-user))
6665 (defun tramp-action-password (proc vec)
6666 "Query the user for a password."
6667 (with-current-buffer (process-buffer proc)
6668 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
6669 (tramp-message vec 3 "Sending %s" (match-string 1)))
6670 (tramp-enter-password proc))
6672 (defun tramp-action-succeed (proc vec)
6673 "Signal success in finding shell prompt."
6674 (throw 'tramp-action 'ok))
6676 (defun tramp-action-permission-denied (proc vec)
6677 "Signal permission denied."
6678 (kill-process proc)
6679 (throw 'tramp-action 'permission-denied))
6681 (defun tramp-action-yesno (proc vec)
6682 "Ask the user for confirmation using `yes-or-no-p'.
6683 Send \"yes\" to remote process on confirmation, abort otherwise.
6684 See also `tramp-action-yn'."
6685 (save-window-excursion
6686 (let ((enable-recursive-minibuffers t))
6687 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
6688 (unless (yes-or-no-p (match-string 0))
6689 (kill-process proc)
6690 (throw 'tramp-action 'permission-denied))
6691 (with-current-buffer (tramp-get-connection-buffer vec)
6692 (tramp-message vec 6 "\n%s" (buffer-string)))
6693 (tramp-send-string vec "yes"))))
6695 (defun tramp-action-yn (proc vec)
6696 "Ask the user for confirmation using `y-or-n-p'.
6697 Send \"y\" to remote process on confirmation, abort otherwise.
6698 See also `tramp-action-yesno'."
6699 (save-window-excursion
6700 (let ((enable-recursive-minibuffers t))
6701 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
6702 (unless (y-or-n-p (match-string 0))
6703 (kill-process proc)
6704 (throw 'tramp-action 'permission-denied))
6705 (with-current-buffer (tramp-get-connection-buffer vec)
6706 (tramp-message vec 6 "\n%s" (buffer-string)))
6707 (tramp-send-string vec "y"))))
6709 (defun tramp-action-terminal (proc vec)
6710 "Tell the remote host which terminal type to use.
6711 The terminal type can be configured with `tramp-terminal-type'."
6712 (tramp-message vec 5 "Setting `%s' as terminal type." tramp-terminal-type)
6713 (with-current-buffer (tramp-get-connection-buffer vec)
6714 (tramp-message vec 6 "\n%s" (buffer-string)))
6715 (tramp-send-string vec tramp-terminal-type))
6717 (defun tramp-action-process-alive (proc vec)
6718 "Check, whether a process has finished."
6719 (unless (memq (process-status proc) '(run open))
6720 (throw 'tramp-action 'process-died)))
6722 (defun tramp-action-out-of-band (proc vec)
6723 "Check, whether an out-of-band copy has finished."
6724 (cond ((and (memq (process-status proc) '(stop exit))
6725 (zerop (process-exit-status proc)))
6726 (tramp-message vec 3 "Process has finished.")
6727 (throw 'tramp-action 'ok))
6728 ((or (and (memq (process-status proc) '(stop exit))
6729 (not (zerop (process-exit-status proc))))
6730 (memq (process-status proc) '(signal)))
6731 ;; `scp' could have copied correctly, but set modes could have failed.
6732 ;; This can be ignored.
6733 (with-current-buffer (process-buffer proc)
6734 (goto-char (point-min))
6735 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
6736 (progn
6737 (tramp-message vec 5 "'set mode' error ignored.")
6738 (tramp-message vec 3 "Process has finished.")
6739 (throw 'tramp-action 'ok))
6740 (tramp-message vec 3 "Process has died.")
6741 (throw 'tramp-action 'process-died))))
6742 (t nil)))
6744 ;; Functions for processing the actions.
6746 (defun tramp-process-one-action (proc vec actions)
6747 "Wait for output from the shell and perform one action."
6748 (let (found todo item pattern action)
6749 (while (not found)
6750 ;; Reread output once all actions have been performed.
6751 ;; Obviously, the output was not complete.
6752 (tramp-accept-process-output proc 1)
6753 (setq todo actions)
6754 (while todo
6755 (setq item (pop todo))
6756 (setq pattern (format "\\(%s\\)\\'" (symbol-value (nth 0 item))))
6757 (setq action (nth 1 item))
6758 (tramp-message
6759 vec 5 "Looking for regexp \"%s\" from remote shell" pattern)
6760 (when (tramp-check-for-regexp proc pattern)
6761 (tramp-message vec 5 "Call `%s'" (symbol-name action))
6762 (setq found (funcall action proc vec)))))
6763 found))
6765 (defun tramp-process-actions (proc vec actions &optional timeout)
6766 "Perform actions until success or TIMEOUT."
6767 ;; Preserve message for `progress-reporter'.
6768 (with-temp-message ""
6769 ;; Enable auth-source and password-cache.
6770 (tramp-set-connection-property vec "first-password-request" t)
6771 (let (exit)
6772 (while (not exit)
6773 (tramp-message proc 3 "Waiting for prompts from remote shell")
6774 (setq exit
6775 (catch 'tramp-action
6776 (if timeout
6777 (with-timeout (timeout)
6778 (tramp-process-one-action proc vec actions))
6779 (tramp-process-one-action proc vec actions)))))
6780 (with-current-buffer (tramp-get-connection-buffer vec)
6781 (tramp-message vec 6 "\n%s" (buffer-string)))
6782 (unless (eq exit 'ok)
6783 (tramp-clear-passwd vec)
6784 (tramp-error-with-buffer
6785 nil vec 'file-error
6786 (cond
6787 ((eq exit 'permission-denied) "Permission denied")
6788 ((eq exit 'process-died) "Process died")
6789 (t "Login failed")))))))
6791 ;; Utility functions.
6793 (defun tramp-accept-process-output (&optional proc timeout timeout-msecs)
6794 "Like `accept-process-output' for Tramp processes.
6795 This is needed in order to hide `last-coding-system-used', which is set
6796 for process communication also."
6797 (with-current-buffer (process-buffer proc)
6798 (tramp-message proc 10 "%s %s" proc (process-status proc))
6799 (let (buffer-read-only last-coding-system-used)
6800 ;; Under Windows XP, accept-process-output doesn't return
6801 ;; sometimes. So we add an additional timeout.
6802 (with-timeout ((or timeout 1))
6803 (accept-process-output proc timeout timeout-msecs)))
6804 (tramp-message proc 10 "\n%s" (buffer-string))))
6806 (defun tramp-check-for-regexp (proc regexp)
6807 "Check, whether REGEXP is contained in process buffer of PROC.
6808 Erase echoed commands if exists."
6809 (with-current-buffer (process-buffer proc)
6810 (goto-char (point-min))
6812 ;; Check whether we need to remove echo output.
6813 (when (and (tramp-get-connection-property proc "check-remote-echo" nil)
6814 (re-search-forward tramp-echoed-echo-mark-regexp nil t))
6815 (let ((begin (match-beginning 0)))
6816 (when (re-search-forward tramp-echoed-echo-mark-regexp nil t)
6817 ;; Discard echo from remote output.
6818 (tramp-set-connection-property proc "check-remote-echo" nil)
6819 (tramp-message proc 5 "echo-mark found")
6820 (forward-line 1)
6821 (delete-region begin (point))
6822 (goto-char (point-min)))))
6824 (when (or (not (tramp-get-connection-property proc "check-remote-echo" nil))
6825 ;; Sometimes, the echo string is suppressed on the remote side.
6826 (not (string-equal
6827 (tramp-compat-funcall
6828 'substring-no-properties tramp-echo-mark-marker
6829 0 (min tramp-echo-mark-marker-length (1- (point-max))))
6830 (tramp-compat-funcall
6831 'buffer-substring-no-properties
6832 1 (min (1+ tramp-echo-mark-marker-length) (point-max))))))
6833 ;; No echo to be handled, now we can look for the regexp.
6834 (goto-char (point-min))
6835 (re-search-forward regexp nil t))))
6837 (defun tramp-wait-for-regexp (proc timeout regexp)
6838 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
6839 Expects the output of PROC to be sent to the current buffer. Returns
6840 the string that matched, or nil. Waits indefinitely if TIMEOUT is
6841 nil."
6842 (with-current-buffer (process-buffer proc)
6843 (let ((found (tramp-check-for-regexp proc regexp))
6844 (start-time (current-time)))
6845 (cond (timeout
6846 ;; Work around a bug in XEmacs 21, where the timeout
6847 ;; expires faster than it should. This degenerates
6848 ;; to polling for buggy XEmacsen, but oh, well.
6849 (while (and (not found)
6850 (< (tramp-time-diff (current-time) start-time)
6851 timeout))
6852 (with-timeout (timeout)
6853 (while (not found)
6854 (tramp-accept-process-output proc 1)
6855 (unless (memq (process-status proc) '(run open))
6856 (tramp-error-with-buffer
6857 nil proc 'file-error "Process has died"))
6858 (setq found (tramp-check-for-regexp proc regexp))))))
6860 (while (not found)
6861 (tramp-accept-process-output proc 1)
6862 (unless (memq (process-status proc) '(run open))
6863 (tramp-error-with-buffer
6864 nil proc 'file-error "Process has died"))
6865 (setq found (tramp-check-for-regexp proc regexp)))))
6866 (tramp-message proc 6 "\n%s" (buffer-string))
6867 (when (not found)
6868 (if timeout
6869 (tramp-error
6870 proc 'file-error "[[Regexp `%s' not found in %d secs]]"
6871 regexp timeout)
6872 (tramp-error proc 'file-error "[[Regexp `%s' not found]]" regexp)))
6873 found)))
6875 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
6876 "Wait for shell prompt and barf if none appears.
6877 Looks at process PROC to see if a shell prompt appears in TIMEOUT
6878 seconds. If not, it produces an error message with the given ERROR-ARGS."
6879 (unless
6880 (tramp-wait-for-regexp
6881 proc timeout
6882 (format
6883 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
6884 (apply 'tramp-error-with-buffer nil proc 'file-error error-args)))
6886 ;; We don't call `tramp-send-string' in order to hide the password
6887 ;; from the debug buffer, and because end-of-line handling of the
6888 ;; string.
6889 (defun tramp-enter-password (proc)
6890 "Prompt for a password and send it to the remote end."
6891 (process-send-string
6892 proc (concat (tramp-read-passwd proc)
6893 (or (tramp-get-method-parameter
6894 tramp-current-method
6895 'tramp-password-end-of-line)
6896 tramp-default-password-end-of-line))))
6898 (defun tramp-open-connection-setup-interactive-shell (proc vec)
6899 "Set up an interactive shell.
6900 Mainly sets the prompt and the echo correctly. PROC is the shell
6901 process to set up. VEC specifies the connection."
6902 (let ((tramp-end-of-output tramp-initial-end-of-output))
6903 ;; It is useful to set the prompt in the following command because
6904 ;; some people have a setting for $PS1 which /bin/sh doesn't know
6905 ;; about and thus /bin/sh will display a strange prompt. For
6906 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
6907 ;; display the current working directory but /bin/sh will display
6908 ;; a dollar sign. The following command line sets $PS1 to a sane
6909 ;; value, and works under Bourne-ish shells as well as csh-like
6910 ;; shells. Daniel Pittman reports that the unusual positioning of
6911 ;; the single quotes makes it work under `rc', too. We also unset
6912 ;; the variable $ENV because that is read by some sh
6913 ;; implementations (eg, bash when called as sh) on startup; this
6914 ;; way, we avoid the startup file clobbering $PS1. $PROMP_COMMAND
6915 ;; is another way to set the prompt in /bin/bash, it must be
6916 ;; discarded as well.
6917 (tramp-send-command
6919 (format
6920 "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s"
6921 (shell-quote-argument tramp-end-of-output)
6922 (tramp-get-method-parameter
6923 (tramp-file-name-method vec) 'tramp-remote-sh))
6926 ;; Disable echo.
6927 (tramp-message vec 5 "Setting up remote shell environment")
6928 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
6929 ;; Check whether the echo has really been disabled. Some
6930 ;; implementations, like busybox of embedded GNU/Linux, don't
6931 ;; support disabling.
6932 (tramp-send-command vec "echo foo" t)
6933 (with-current-buffer (process-buffer proc)
6934 (goto-char (point-min))
6935 (when (looking-at "echo foo")
6936 (tramp-set-connection-property proc "remote-echo" t)
6937 (tramp-message vec 5 "Remote echo still on. Ok.")
6938 ;; Make sure backspaces and their echo are enabled and no line
6939 ;; width magic interferes with them.
6940 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
6942 (tramp-message vec 5 "Setting shell prompt")
6943 (tramp-send-command
6944 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
6945 (tramp-send-command vec "PS2=''" t)
6946 (tramp-send-command vec "PS3=''" t)
6947 (tramp-send-command vec "PROMPT_COMMAND=''" t)
6949 ;; Try to set up the coding system correctly.
6950 ;; CCC this can't be the right way to do it. Hm.
6951 (tramp-message vec 5 "Determining coding system")
6952 (tramp-send-command vec "echo foo ; echo bar" t)
6953 (with-current-buffer (process-buffer proc)
6954 (goto-char (point-min))
6955 (if (featurep 'mule)
6956 ;; Use MULE to select the right EOL convention for communicating
6957 ;; with the process.
6958 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc)
6959 (cons 'undecided 'undecided)))
6960 cs-decode cs-encode)
6961 (when (symbolp cs) (setq cs (cons cs cs)))
6962 (setq cs-decode (car cs))
6963 (setq cs-encode (cdr cs))
6964 (unless cs-decode (setq cs-decode 'undecided))
6965 (unless cs-encode (setq cs-encode 'undecided))
6966 (setq cs-encode (tramp-coding-system-change-eol-conversion
6967 cs-encode 'unix))
6968 (when (search-forward "\r" nil t)
6969 (setq cs-decode (tramp-coding-system-change-eol-conversion
6970 cs-decode 'dos)))
6971 (tramp-compat-funcall
6972 'set-buffer-process-coding-system cs-decode cs-encode)
6973 (tramp-message
6974 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
6975 ;; Look for ^M and do something useful if found.
6976 (when (search-forward "\r" nil t)
6977 ;; We have found a ^M but cannot frob the process coding system
6978 ;; because we're running on a non-MULE Emacs. Let's try
6979 ;; stty, instead.
6980 (tramp-send-command vec "stty -onlcr" t))))
6981 ;; Dump stty settings in the traces.
6982 (when (>= tramp-verbose 10)
6983 (tramp-send-command vec "stty -a" t))
6984 (tramp-send-command vec "set +o vi +o emacs" t)
6986 ;; Check whether the output of "uname -sr" has been changed. If
6987 ;; yes, this is a strong indication that we must expire all
6988 ;; connection properties. We start again with
6989 ;; `tramp-maybe-open-connection', it will be catched there.
6990 (tramp-message vec 5 "Checking system information")
6991 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
6992 (new-uname
6993 (tramp-set-connection-property
6994 vec "uname"
6995 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
6996 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
6997 (with-current-buffer (tramp-get-debug-buffer vec)
6998 ;; Keep the debug buffer.
6999 (rename-buffer
7000 (generate-new-buffer-name tramp-temp-buffer-name) 'unique)
7001 (tramp-compat-funcall 'tramp-cleanup-connection vec)
7002 (if (= (point-min) (point-max))
7003 (kill-buffer nil)
7004 (rename-buffer (tramp-debug-buffer-name vec) 'unique))
7005 ;; We call `tramp-get-buffer' in order to keep the debug buffer.
7006 (tramp-get-buffer vec)
7007 (tramp-message
7008 vec 3
7009 "Connection reset, because remote host changed from `%s' to `%s'"
7010 old-uname new-uname)
7011 (throw 'uname-changed (tramp-maybe-open-connection vec)))))
7013 ;; Check whether the remote host suffers from buggy
7014 ;; `send-process-string'. This is known for FreeBSD (see comment in
7015 ;; `send_process', file process.c). I've tested sending 624 bytes
7016 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
7017 ;; this host type is detected locally. It cannot handle remote
7018 ;; hosts, though.
7019 (with-connection-property proc "chunksize"
7020 (cond
7021 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
7022 tramp-chunksize)
7024 (tramp-message
7025 vec 5 "Checking remote host type for `send-process-string' bug")
7026 (if (string-match
7027 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
7028 500 0))))
7030 ;; Set remote PATH variable.
7031 (tramp-set-remote-path vec)
7033 ;; Search for a good shell before searching for a command which
7034 ;; checks if a file exists. This is done because Tramp wants to use
7035 ;; "test foo; echo $?" to check if various conditions hold, and
7036 ;; there are buggy /bin/sh implementations which don't execute the
7037 ;; "echo $?" part if the "test" part has an error. In particular,
7038 ;; the OpenSolaris /bin/sh is a problem. There are also other
7039 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
7040 ;; in in function declarations, or changing HISTFILE in place.
7041 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
7042 ;; detected.
7043 (tramp-find-shell vec)
7045 ;; Disable unexpected output.
7046 (tramp-send-command vec "mesg n; biff n" t)
7048 ;; IRIX64 bash expands "!" even when in single quotes. This
7049 ;; destroys our shell functions, we must disable it. See
7050 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
7051 (when (string-match "^IRIX64" (tramp-get-connection-property vec "uname" ""))
7052 (tramp-send-command vec "set +H" t))
7054 ;; Set the environment.
7055 (tramp-message vec 5 "Setting default environment")
7057 (let ((env (copy-sequence tramp-remote-process-environment))
7058 unset item)
7059 (while env
7060 (setq item (tramp-compat-split-string (car env) "="))
7061 (setcdr item (mapconcat 'identity (cdr item) "="))
7062 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
7063 (tramp-send-command
7064 vec (format "%s=%s; export %s" (car item) (cdr item) (car item)) t)
7065 (push (car item) unset))
7066 (setq env (cdr env)))
7067 (when unset
7068 (tramp-send-command
7069 vec (format "unset %s" (mapconcat 'identity unset " ")) t))))
7071 ;; CCC: We should either implement a Perl version of base64 encoding
7072 ;; and decoding. Then we just use that in the last item. The other
7073 ;; alternative is to use the Perl version of UU encoding. But then
7074 ;; we need a Lisp version of uuencode.
7076 ;; Old text from documentation of tramp-methods:
7077 ;; Using a uuencode/uudecode inline method is discouraged, please use one
7078 ;; of the base64 methods instead since base64 encoding is much more
7079 ;; reliable and the commands are more standardized between the different
7080 ;; Unix versions. But if you can't use base64 for some reason, please
7081 ;; note that the default uudecode command does not work well for some
7082 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
7083 ;; the following command for uudecode:
7085 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
7087 ;; For Irix, no solution is known yet.
7089 (defconst tramp-local-coding-commands
7090 '((b64 base64-encode-region base64-decode-region)
7091 (uu tramp-uuencode-region uudecode-decode-region)
7092 (pack
7093 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
7094 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
7095 "List of local coding commands for inline transfer.
7096 Each item is a list that looks like this:
7098 \(FORMAT ENCODING DECODING\)
7100 FORMAT is symbol describing the encoding/decoding format. It can be
7101 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
7103 ENCODING and DECODING can be strings, giving commands, or symbols,
7104 giving functions. If they are strings, then they can contain
7105 the \"%s\" format specifier. If that specifier is present, the input
7106 filename will be put into the command line at that spot. If the
7107 specifier is not present, the input should be read from standard
7108 input.
7110 If they are functions, they will be called with two arguments, start
7111 and end of region, and are expected to replace the region contents
7112 with the encoded or decoded results, respectively.")
7114 (defconst tramp-remote-coding-commands
7115 '((b64 "base64" "base64 -d")
7116 (b64 "mimencode -b" "mimencode -u -b")
7117 (b64 "mmencode -b" "mmencode -u -b")
7118 (b64 "recode data..base64" "recode base64..data")
7119 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
7120 (b64 tramp-perl-encode tramp-perl-decode)
7121 (uu "uuencode xxx" "uudecode -o /dev/stdout")
7122 (uu "uuencode xxx" "uudecode -o -")
7123 (uu "uuencode xxx" "uudecode -p")
7124 (uu "uuencode xxx" tramp-uudecode)
7125 (pack
7126 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
7127 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
7128 "List of remote coding commands for inline transfer.
7129 Each item is a list that looks like this:
7131 \(FORMAT ENCODING DECODING\)
7133 FORMAT is symbol describing the encoding/decoding format. It can be
7134 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
7136 ENCODING and DECODING can be strings, giving commands, or symbols,
7137 giving variables. If they are strings, then they can contain
7138 the \"%s\" format specifier. If that specifier is present, the input
7139 filename will be put into the command line at that spot. If the
7140 specifier is not present, the input should be read from standard
7141 input.
7143 If they are variables, this variable is a string containing a Perl
7144 implementation for this functionality. This Perl program will be transferred
7145 to the remote host, and it is available as shell function with the same name.")
7147 (defun tramp-find-inline-encoding (vec)
7148 "Find an inline transfer encoding that works.
7149 Goes through the list `tramp-local-coding-commands' and
7150 `tramp-remote-coding-commands'."
7151 (save-excursion
7152 (let ((local-commands tramp-local-coding-commands)
7153 (magic "xyzzy")
7154 loc-enc loc-dec rem-enc rem-dec litem ritem found)
7155 (while (and local-commands (not found))
7156 (setq litem (pop local-commands))
7157 (catch 'wont-work-local
7158 (let ((format (nth 0 litem))
7159 (remote-commands tramp-remote-coding-commands))
7160 (setq loc-enc (nth 1 litem))
7161 (setq loc-dec (nth 2 litem))
7162 ;; If the local encoder or decoder is a string, the
7163 ;; corresponding command has to work locally.
7164 (if (not (stringp loc-enc))
7165 (tramp-message
7166 vec 5 "Checking local encoding function `%s'" loc-enc)
7167 (tramp-message
7168 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
7169 (unless (zerop (tramp-call-local-coding-command
7170 loc-enc nil nil))
7171 (throw 'wont-work-local nil)))
7172 (if (not (stringp loc-dec))
7173 (tramp-message
7174 vec 5 "Checking local decoding function `%s'" loc-dec)
7175 (tramp-message
7176 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
7177 (unless (zerop (tramp-call-local-coding-command
7178 loc-dec nil nil))
7179 (throw 'wont-work-local nil)))
7180 ;; Search for remote coding commands with the same format
7181 (while (and remote-commands (not found))
7182 (setq ritem (pop remote-commands))
7183 (catch 'wont-work-remote
7184 (when (equal format (nth 0 ritem))
7185 (setq rem-enc (nth 1 ritem))
7186 (setq rem-dec (nth 2 ritem))
7187 ;; Check if remote encoding and decoding commands can be
7188 ;; called remotely with null input and output. This makes
7189 ;; sure there are no syntax errors and the command is really
7190 ;; found. Note that we do not redirect stdout to /dev/null,
7191 ;; for two reasons: when checking the decoding command, we
7192 ;; actually check the output it gives. And also, when
7193 ;; redirecting "mimencode" output to /dev/null, then as root
7194 ;; it might change the permissions of /dev/null!
7195 (when (not (stringp rem-enc))
7196 (let ((name (symbol-name rem-enc)))
7197 (while (string-match (regexp-quote "-") name)
7198 (setq name (replace-match "_" nil t name)))
7199 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
7200 (setq rem-enc name)))
7201 (tramp-message
7202 vec 5
7203 "Checking remote encoding command `%s' for sanity" rem-enc)
7204 (unless (zerop (tramp-send-command-and-check
7205 vec (format "%s </dev/null" rem-enc) t))
7206 (throw 'wont-work-remote nil))
7208 (when (not (stringp rem-dec))
7209 (let ((name (symbol-name rem-dec)))
7210 (while (string-match (regexp-quote "-") name)
7211 (setq name (replace-match "_" nil t name)))
7212 (tramp-maybe-send-script vec (symbol-value rem-dec) name)
7213 (setq rem-dec name)))
7214 (tramp-message
7215 vec 5
7216 "Checking remote decoding command `%s' for sanity" rem-dec)
7217 (unless (zerop (tramp-send-command-and-check
7219 (format "echo %s | %s | %s"
7220 magic rem-enc rem-dec)
7222 (throw 'wont-work-remote nil))
7224 (with-current-buffer (tramp-get-buffer vec)
7225 (goto-char (point-min))
7226 (unless (looking-at (regexp-quote magic))
7227 (throw 'wont-work-remote nil)))
7229 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
7230 (setq rem-enc (nth 1 ritem))
7231 (setq rem-dec (nth 2 ritem))
7232 (setq found t)))))))
7234 ;; Did we find something?
7235 (unless found
7236 (tramp-error
7237 vec 'file-error "Couldn't find an inline transfer encoding"))
7239 ;; Set connection properties.
7240 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
7241 (tramp-set-connection-property vec "local-encoding" loc-enc)
7242 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
7243 (tramp-set-connection-property vec "local-decoding" loc-dec)
7244 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
7245 (tramp-set-connection-property vec "remote-encoding" rem-enc)
7246 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
7247 (tramp-set-connection-property vec "remote-decoding" rem-dec))))
7249 (defun tramp-call-local-coding-command (cmd input output)
7250 "Call the local encoding or decoding command.
7251 If CMD contains \"%s\", provide input file INPUT there in command.
7252 Otherwise, INPUT is passed via standard input.
7253 INPUT can also be nil which means `/dev/null'.
7254 OUTPUT can be a string (which specifies a filename), or t (which
7255 means standard output and thus the current buffer), or nil (which
7256 means discard it)."
7257 (tramp-local-call-process
7258 tramp-encoding-shell
7259 (when (and input (not (string-match "%s" cmd))) input)
7260 (if (eq output t) t nil)
7262 tramp-encoding-command-switch
7263 (concat
7264 (if (string-match "%s" cmd) (format cmd input) cmd)
7265 (if (stringp output) (concat "> " output) ""))))
7267 (defconst tramp-inline-compress-commands
7268 '(("gzip" "gzip -d")
7269 ("bzip2" "bzip2 -d")
7270 ("compress" "compress -d"))
7271 "List of compress and decompress commands for inline transfer.
7272 Each item is a list that looks like this:
7274 \(COMPRESS DECOMPRESS\)
7276 COMPRESS or DECOMPRESS are strings with the respective commands.")
7278 (defun tramp-find-inline-compress (vec)
7279 "Find an inline transfer compress command that works.
7280 Goes through the list `tramp-inline-compress-commands'."
7281 (save-excursion
7282 (let ((commands tramp-inline-compress-commands)
7283 (magic "xyzzy")
7284 item compress decompress
7285 found)
7286 (while (and commands (not found))
7287 (catch 'next
7288 (setq item (pop commands)
7289 compress (nth 0 item)
7290 decompress (nth 1 item))
7291 (tramp-message
7292 vec 5
7293 "Checking local compress command `%s', `%s' for sanity"
7294 compress decompress)
7295 (unless (zerop (tramp-call-local-coding-command
7296 (format "echo %s | %s | %s"
7297 magic compress decompress) nil nil))
7298 (throw 'next nil))
7299 (tramp-message
7300 vec 5
7301 "Checking remote compress command `%s', `%s' for sanity"
7302 compress decompress)
7303 (unless (zerop (tramp-send-command-and-check
7304 vec (format "echo %s | %s | %s"
7305 magic compress decompress) t))
7306 (throw 'next nil))
7307 (setq found t)))
7309 ;; Did we find something?
7310 (if found
7311 (progn
7312 ;; Set connection properties.
7313 (tramp-message
7314 vec 5 "Using inline transfer compress command `%s'" compress)
7315 (tramp-set-connection-property vec "inline-compress" compress)
7316 (tramp-message
7317 vec 5 "Using inline transfer decompress command `%s'" decompress)
7318 (tramp-set-connection-property vec "inline-decompress" decompress))
7320 (tramp-set-connection-property vec "inline-compress" nil)
7321 (tramp-set-connection-property vec "inline-decompress" nil)
7322 (tramp-message
7323 vec 2 "Couldn't find an inline transfer compress command")))))
7325 (defun tramp-compute-multi-hops (vec)
7326 "Expands VEC according to `tramp-default-proxies-alist'.
7327 Gateway hops are already opened."
7328 (let ((target-alist `(,vec))
7329 (choices tramp-default-proxies-alist)
7330 item proxy)
7332 ;; Look for proxy hosts to be passed.
7333 (while choices
7334 (setq item (pop choices)
7335 proxy (eval (nth 2 item)))
7336 (when (and
7337 ;; host
7338 (string-match (or (eval (nth 0 item)) "")
7339 (or (tramp-file-name-host (car target-alist)) ""))
7340 ;; user
7341 (string-match (or (eval (nth 1 item)) "")
7342 (or (tramp-file-name-user (car target-alist)) "")))
7343 (if (null proxy)
7344 ;; No more hops needed.
7345 (setq choices nil)
7346 ;; Replace placeholders.
7347 (setq proxy
7348 (format-spec
7349 proxy
7350 (format-spec-make
7351 ?u (or (tramp-file-name-user (car target-alist)) "")
7352 ?h (or (tramp-file-name-host (car target-alist)) ""))))
7353 (with-parsed-tramp-file-name proxy l
7354 ;; Add the hop.
7355 (add-to-list 'target-alist l)
7356 ;; Start next search.
7357 (setq choices tramp-default-proxies-alist)))))
7359 ;; Handle gateways.
7360 (when (and (boundp 'tramp-gw-tunnel-method)
7361 (string-match (format
7362 "^\\(%s\\|%s\\)$"
7363 (symbol-value 'tramp-gw-tunnel-method)
7364 (symbol-value 'tramp-gw-socks-method))
7365 (tramp-file-name-method (car target-alist))))
7366 (let ((gw (pop target-alist))
7367 (hop (pop target-alist)))
7368 ;; Is the method prepared for gateways?
7369 (unless (tramp-get-method-parameter
7370 (tramp-file-name-method hop) 'tramp-default-port)
7371 (tramp-error
7372 vec 'file-error
7373 "Method `%s' is not supported for gateway access."
7374 (tramp-file-name-method hop)))
7375 ;; Add default port if needed.
7376 (unless
7377 (string-match
7378 tramp-host-with-port-regexp (tramp-file-name-host hop))
7379 (aset hop 2
7380 (concat
7381 (tramp-file-name-host hop) tramp-prefix-port-format
7382 (number-to-string
7383 (tramp-get-method-parameter
7384 (tramp-file-name-method hop) 'tramp-default-port)))))
7385 ;; Open the gateway connection.
7386 (add-to-list
7387 'target-alist
7388 (vector
7389 (tramp-file-name-method hop) (tramp-file-name-user hop)
7390 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop) nil))
7391 ;; For the password prompt, we need the correct values.
7392 ;; Therefore, we must remember the gateway vector. But we
7393 ;; cannot do it as connection property, because it shouldn't
7394 ;; be persistent. And we have no started process yet either.
7395 (tramp-set-file-property (car target-alist) "" "gateway" hop)))
7397 ;; Foreign and out-of-band methods are not supported for multi-hops.
7398 (when (cdr target-alist)
7399 (setq choices target-alist)
7400 (while choices
7401 (setq item (pop choices))
7402 (when
7404 (not
7405 (tramp-get-method-parameter
7406 (tramp-file-name-method item) 'tramp-login-program))
7407 (tramp-get-method-parameter
7408 (tramp-file-name-method item) 'tramp-copy-program))
7409 (tramp-error
7410 vec 'file-error
7411 "Method `%s' is not supported for multi-hops."
7412 (tramp-file-name-method item)))))
7414 ;; In case the host name is not used for the remote shell
7415 ;; command, the user could be misguided by applying a random
7416 ;; hostname.
7417 (let* ((v (car target-alist))
7418 (method (tramp-file-name-method v))
7419 (host (tramp-file-name-host v)))
7420 (unless
7422 ;; There are multi-hops.
7423 (cdr target-alist)
7424 ;; The host name is used for the remote shell command.
7425 (member
7426 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
7427 ;; The host is local. We cannot use `tramp-local-host-p'
7428 ;; here, because it opens a connection as well.
7429 (string-match tramp-local-host-regexp host))
7430 (tramp-error
7431 v 'file-error
7432 "Host `%s' looks like a remote host, `%s' can only use the local host"
7433 host method)))
7435 ;; Result.
7436 target-alist))
7438 (defun tramp-maybe-open-connection (vec)
7439 "Maybe open a connection VEC.
7440 Does not do anything if a connection is already open, but re-opens the
7441 connection if a previous connection has died for some reason."
7442 (catch 'uname-changed
7443 (let ((p (tramp-get-connection-process vec))
7444 (process-name (tramp-get-connection-property vec "process-name" nil))
7445 (process-environment (copy-sequence process-environment)))
7447 ;; If too much time has passed since last command was sent, look
7448 ;; whether process is still alive. If it isn't, kill it. When
7449 ;; using ssh, it can sometimes happen that the remote end has
7450 ;; hung up but the local ssh client doesn't recognize this until
7451 ;; it tries to send some data to the remote end. So that's why
7452 ;; we try to send a command from time to time, then look again
7453 ;; whether the process is really alive.
7454 (condition-case nil
7455 (when (and (> (tramp-time-diff
7456 (current-time)
7457 (tramp-get-connection-property
7458 p "last-cmd-time" '(0 0 0)))
7460 p (processp p) (memq (process-status p) '(run open)))
7461 (tramp-send-command vec "echo are you awake" t t)
7462 (unless (and (memq (process-status p) '(run open))
7463 (tramp-wait-for-output p 10))
7464 ;; The error will be catched locally.
7465 (tramp-error vec 'file-error "Awake did fail")))
7466 (file-error
7467 (tramp-flush-connection-property vec)
7468 (tramp-flush-connection-property p)
7469 (delete-process p)
7470 (setq p nil)))
7472 ;; New connection must be opened.
7473 (unless (and p (processp p) (memq (process-status p) '(run open)))
7475 ;; We call `tramp-get-buffer' in order to get a debug buffer for
7476 ;; messages from the beginning.
7477 (tramp-get-buffer vec)
7478 (with-progress-reporter
7479 vec 3
7480 (if (zerop (length (tramp-file-name-user vec)))
7481 (format "Opening connection for %s using %s"
7482 (tramp-file-name-host vec)
7483 (tramp-file-name-method vec))
7484 (format "Opening connection for %s@%s using %s"
7485 (tramp-file-name-user vec)
7486 (tramp-file-name-host vec)
7487 (tramp-file-name-method vec)))
7489 ;; Start new process.
7490 (when (and p (processp p))
7491 (delete-process p))
7492 (setenv "TERM" tramp-terminal-type)
7493 (setenv "LC_ALL" "C")
7494 (setenv "PROMPT_COMMAND")
7495 (setenv "PS1" tramp-initial-end-of-output)
7496 (let* ((target-alist (tramp-compute-multi-hops vec))
7497 (process-connection-type tramp-process-connection-type)
7498 (process-adaptive-read-buffering nil)
7499 (coding-system-for-read nil)
7500 ;; This must be done in order to avoid our file name handler.
7501 (p (let ((default-directory
7502 (tramp-compat-temporary-file-directory)))
7503 (start-process
7504 (or process-name (tramp-buffer-name vec))
7505 (tramp-get-connection-buffer vec)
7506 tramp-encoding-shell))))
7508 (tramp-message
7509 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
7511 ;; Check whether process is alive.
7512 (tramp-set-process-query-on-exit-flag p nil)
7513 (tramp-barf-if-no-shell-prompt
7514 p 60 "Couldn't find local shell prompt %s" tramp-encoding-shell)
7516 ;; Now do all the connections as specified.
7517 (while target-alist
7518 (let* ((hop (car target-alist))
7519 (l-method (tramp-file-name-method hop))
7520 (l-user (tramp-file-name-user hop))
7521 (l-host (tramp-file-name-host hop))
7522 (l-port nil)
7523 (login-program
7524 (tramp-get-method-parameter
7525 l-method 'tramp-login-program))
7526 (login-args
7527 (tramp-get-method-parameter l-method 'tramp-login-args))
7528 (async-args
7529 (tramp-get-method-parameter l-method 'tramp-async-args))
7530 (gw-args
7531 (tramp-get-method-parameter l-method 'tramp-gw-args))
7532 (gw (tramp-get-file-property hop "" "gateway" nil))
7533 (g-method (and gw (tramp-file-name-method gw)))
7534 (g-user (and gw (tramp-file-name-user gw)))
7535 (g-host (and gw (tramp-file-name-host gw)))
7536 (command login-program)
7537 ;; We don't create the temporary file. In fact,
7538 ;; it is just a prefix for the ControlPath option
7539 ;; of ssh; the real temporary file has another
7540 ;; name, and it is created and protected by ssh.
7541 ;; It is also removed by ssh, when the connection
7542 ;; is closed.
7543 (tmpfile
7544 (tramp-set-connection-property
7545 p "temp-file"
7546 (make-temp-name
7547 (expand-file-name
7548 tramp-temp-name-prefix
7549 (tramp-compat-temporary-file-directory)))))
7550 spec)
7552 ;; Add arguments for asynchrononous processes.
7553 (when (and process-name async-args)
7554 (setq login-args (append async-args login-args)))
7556 ;; Add gateway arguments if necessary.
7557 (when (and gw gw-args)
7558 (setq login-args (append gw-args login-args)))
7560 ;; Check for port number. Until now, there's no need
7561 ;; for handling like method, user, host.
7562 (when (string-match tramp-host-with-port-regexp l-host)
7563 (setq l-port (match-string 2 l-host)
7564 l-host (match-string 1 l-host)))
7566 ;; Set variables for computing the prompt for reading
7567 ;; password. They can also be derived from a gateway.
7568 (setq tramp-current-method (or g-method l-method)
7569 tramp-current-user (or g-user l-user)
7570 tramp-current-host (or g-host l-host))
7572 ;; Replace login-args place holders.
7573 (setq
7574 l-host (or l-host "")
7575 l-user (or l-user "")
7576 l-port (or l-port "")
7577 spec (format-spec-make
7578 ?h l-host ?u l-user ?p l-port ?t tmpfile)
7579 command
7580 (concat
7581 ;; We do not want to see the trailing local prompt in
7582 ;; `start-file-process'.
7583 (unless (memq system-type '(windows-nt)) "exec ")
7584 command " "
7585 (mapconcat
7586 (lambda (x)
7587 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
7588 (unless (member "" x) (mapconcat 'identity x " ")))
7589 login-args " ")
7590 ;; Local shell could be a Windows COMSPEC. It
7591 ;; doesn't know the ";" syntax, but we must exit
7592 ;; always for `start-file-process'. "exec" does not
7593 ;; work either.
7594 (if (memq system-type '(windows-nt)) " && exit || exit")))
7596 ;; Send the command.
7597 (tramp-message vec 3 "Sending command `%s'" command)
7598 (tramp-send-command vec command t t)
7599 (tramp-process-actions p vec tramp-actions-before-shell 60)
7600 (tramp-message
7601 vec 3 "Found remote shell prompt on `%s'" l-host))
7602 ;; Next hop.
7603 (setq target-alist (cdr target-alist)))
7605 ;; Make initial shell settings.
7606 (tramp-open-connection-setup-interactive-shell p vec)))))))
7608 (defun tramp-send-command (vec command &optional neveropen nooutput)
7609 "Send the COMMAND to connection VEC.
7610 Erases temporary buffer before sending the command. If optional
7611 arg NEVEROPEN is non-nil, never try to open the connection. This
7612 is meant to be used from `tramp-maybe-open-connection' only. The
7613 function waits for output unless NOOUTPUT is set."
7614 (unless neveropen (tramp-maybe-open-connection vec))
7615 (let ((p (tramp-get-connection-process vec)))
7616 (when (tramp-get-connection-property p "remote-echo" nil)
7617 ;; We mark the command string that it can be erased in the output buffer.
7618 (tramp-set-connection-property p "check-remote-echo" t)
7619 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
7620 (tramp-message vec 6 "%s" command)
7621 (tramp-send-string vec command)
7622 (unless nooutput (tramp-wait-for-output p))))
7624 (defun tramp-wait-for-output (proc &optional timeout)
7625 "Wait for output from remote command."
7626 (unless (buffer-live-p (process-buffer proc))
7627 (delete-process proc)
7628 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
7629 (with-current-buffer (process-buffer proc)
7630 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
7631 ;; be leading escape sequences, which must be ignored.
7632 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
7633 ;; Sometimes, the commands do not return a newline but a
7634 ;; null byte before the shell prompt, for example "git
7635 ;; ls-files -c -z ...".
7636 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
7637 (found (tramp-wait-for-regexp proc timeout regexp1)))
7638 (if found
7639 (let (buffer-read-only)
7640 ;; A simple-minded busybox has sent " ^H" sequences.
7641 ;; Delete them.
7642 (goto-char (point-min))
7643 (when (re-search-forward
7644 "^\\(.\b\\)+$" (tramp-compat-line-end-position) t)
7645 (forward-line 1)
7646 (delete-region (point-min) (point)))
7647 ;; Delete the prompt.
7648 (goto-char (point-max))
7649 (re-search-backward regexp nil t)
7650 (delete-region (point) (point-max)))
7651 (if timeout
7652 (tramp-error
7653 proc 'file-error
7654 "[[Remote prompt `%s' not found in %d secs]]"
7655 tramp-end-of-output timeout)
7656 (tramp-error
7657 proc 'file-error
7658 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
7659 ;; Return value is whether end-of-output sentinel was found.
7660 found)))
7662 (defun tramp-send-command-and-check
7663 (vec command &optional subshell dont-suppress-err)
7664 "Run COMMAND and check its exit status.
7665 Sends `echo $?' along with the COMMAND for checking the exit status. If
7666 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
7668 If the optional argument SUBSHELL is non-nil, the command is
7669 executed in a subshell, ie surrounded by parentheses. If
7670 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
7671 (tramp-send-command
7673 (concat (if subshell "( " "")
7674 command
7675 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
7676 "echo tramp_exit_status $?"
7677 (if subshell " )" "")))
7678 (with-current-buffer (tramp-get-connection-buffer vec)
7679 (goto-char (point-max))
7680 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
7681 (tramp-error
7682 vec 'file-error "Couldn't find exit status of `%s'" command))
7683 (skip-chars-forward "^ ")
7684 (prog1
7685 (read (current-buffer))
7686 (let (buffer-read-only) (delete-region (match-beginning 0) (point-max))))))
7688 (defun tramp-barf-unless-okay (vec command fmt &rest args)
7689 "Run COMMAND, check exit status, throw error if exit status not okay.
7690 Similar to `tramp-send-command-and-check' but accepts two more arguments
7691 FMT and ARGS which are passed to `error'."
7692 (unless (zerop (tramp-send-command-and-check vec command))
7693 (apply 'tramp-error vec 'file-error fmt args)))
7695 (defun tramp-send-command-and-read (vec command)
7696 "Run COMMAND and return the output, which must be a Lisp expression.
7697 In case there is no valid Lisp expression, it raises an error"
7698 (tramp-barf-unless-okay vec command "`%s' returns with error" command)
7699 (with-current-buffer (tramp-get-connection-buffer vec)
7700 ;; Read the expression.
7701 (goto-char (point-min))
7702 (condition-case nil
7703 (prog1 (read (current-buffer))
7704 ;; Error handling.
7705 (when (re-search-forward "\\S-" (tramp-compat-line-end-position) t)
7706 (error nil)))
7707 (error (tramp-error
7708 vec 'file-error
7709 "`%s' does not return a valid Lisp expression: `%s'"
7710 command (buffer-string))))))
7712 ;; It seems that Tru64 Unix does not like it if long strings are sent
7713 ;; to it in one go. (This happens when sending the Perl
7714 ;; `file-attributes' implementation, for instance.) Therefore, we
7715 ;; have this function which sends the string in chunks.
7716 (defun tramp-send-string (vec string)
7717 "Send the STRING via connection VEC.
7719 The STRING is expected to use Unix line-endings, but the lines sent to
7720 the remote host use line-endings as defined in the variable
7721 `tramp-rsh-end-of-line'. The communication buffer is erased before sending."
7722 (let* ((p (tramp-get-connection-process vec))
7723 (chunksize (tramp-get-connection-property p "chunksize" nil)))
7724 (unless p
7725 (tramp-error
7726 vec 'file-error "Can't send string to remote host -- not logged in"))
7727 (tramp-set-connection-property p "last-cmd-time" (current-time))
7728 (tramp-message vec 10 "%s" string)
7729 (with-current-buffer (tramp-get-connection-buffer vec)
7730 ;; Clean up the buffer. We cannot call `erase-buffer' because
7731 ;; narrowing might be in effect.
7732 (let (buffer-read-only) (delete-region (point-min) (point-max)))
7733 ;; Replace "\n" by `tramp-rsh-end-of-line'.
7734 (setq string
7735 (mapconcat 'identity
7736 (tramp-compat-split-string string "\n")
7737 tramp-rsh-end-of-line))
7738 (unless (or (string= string "")
7739 (string-equal (substring string -1) tramp-rsh-end-of-line))
7740 (setq string (concat string tramp-rsh-end-of-line)))
7741 ;; Send the string.
7742 (if (and chunksize (not (zerop chunksize)))
7743 (let ((pos 0)
7744 (end (length string)))
7745 (while (< pos end)
7746 (tramp-message
7747 vec 10 "Sending chunk from %s to %s"
7748 pos (min (+ pos chunksize) end))
7749 (process-send-string
7750 p (substring string pos (min (+ pos chunksize) end)))
7751 (setq pos (+ pos chunksize))))
7752 (process-send-string p string)))))
7754 (defun tramp-mode-string-to-int (mode-string)
7755 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
7756 (let* (case-fold-search
7757 (mode-chars (string-to-vector mode-string))
7758 (owner-read (aref mode-chars 1))
7759 (owner-write (aref mode-chars 2))
7760 (owner-execute-or-setid (aref mode-chars 3))
7761 (group-read (aref mode-chars 4))
7762 (group-write (aref mode-chars 5))
7763 (group-execute-or-setid (aref mode-chars 6))
7764 (other-read (aref mode-chars 7))
7765 (other-write (aref mode-chars 8))
7766 (other-execute-or-sticky (aref mode-chars 9)))
7767 (save-match-data
7768 (logior
7769 (cond
7770 ((char-equal owner-read ?r) (tramp-octal-to-decimal "00400"))
7771 ((char-equal owner-read ?-) 0)
7772 (t (error "Second char `%c' must be one of `r-'" owner-read)))
7773 (cond
7774 ((char-equal owner-write ?w) (tramp-octal-to-decimal "00200"))
7775 ((char-equal owner-write ?-) 0)
7776 (t (error "Third char `%c' must be one of `w-'" owner-write)))
7777 (cond
7778 ((char-equal owner-execute-or-setid ?x)
7779 (tramp-octal-to-decimal "00100"))
7780 ((char-equal owner-execute-or-setid ?S)
7781 (tramp-octal-to-decimal "04000"))
7782 ((char-equal owner-execute-or-setid ?s)
7783 (tramp-octal-to-decimal "04100"))
7784 ((char-equal owner-execute-or-setid ?-) 0)
7785 (t (error "Fourth char `%c' must be one of `xsS-'"
7786 owner-execute-or-setid)))
7787 (cond
7788 ((char-equal group-read ?r) (tramp-octal-to-decimal "00040"))
7789 ((char-equal group-read ?-) 0)
7790 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
7791 (cond
7792 ((char-equal group-write ?w) (tramp-octal-to-decimal "00020"))
7793 ((char-equal group-write ?-) 0)
7794 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
7795 (cond
7796 ((char-equal group-execute-or-setid ?x)
7797 (tramp-octal-to-decimal "00010"))
7798 ((char-equal group-execute-or-setid ?S)
7799 (tramp-octal-to-decimal "02000"))
7800 ((char-equal group-execute-or-setid ?s)
7801 (tramp-octal-to-decimal "02010"))
7802 ((char-equal group-execute-or-setid ?-) 0)
7803 (t (error "Seventh char `%c' must be one of `xsS-'"
7804 group-execute-or-setid)))
7805 (cond
7806 ((char-equal other-read ?r)
7807 (tramp-octal-to-decimal "00004"))
7808 ((char-equal other-read ?-) 0)
7809 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
7810 (cond
7811 ((char-equal other-write ?w) (tramp-octal-to-decimal "00002"))
7812 ((char-equal other-write ?-) 0)
7813 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
7814 (cond
7815 ((char-equal other-execute-or-sticky ?x)
7816 (tramp-octal-to-decimal "00001"))
7817 ((char-equal other-execute-or-sticky ?T)
7818 (tramp-octal-to-decimal "01000"))
7819 ((char-equal other-execute-or-sticky ?t)
7820 (tramp-octal-to-decimal "01001"))
7821 ((char-equal other-execute-or-sticky ?-) 0)
7822 (t (error "Tenth char `%c' must be one of `xtT-'"
7823 other-execute-or-sticky)))))))
7825 (defun tramp-convert-file-attributes (vec attr)
7826 "Convert file-attributes ATTR generated by perl script, stat or ls.
7827 Convert file mode bits to string and set virtual device number.
7828 Return ATTR."
7829 (when attr
7830 ;; Convert last access time.
7831 (unless (listp (nth 4 attr))
7832 (setcar (nthcdr 4 attr)
7833 (list (floor (nth 4 attr) 65536)
7834 (floor (mod (nth 4 attr) 65536)))))
7835 ;; Convert last modification time.
7836 (unless (listp (nth 5 attr))
7837 (setcar (nthcdr 5 attr)
7838 (list (floor (nth 5 attr) 65536)
7839 (floor (mod (nth 5 attr) 65536)))))
7840 ;; Convert last status change time.
7841 (unless (listp (nth 6 attr))
7842 (setcar (nthcdr 6 attr)
7843 (list (floor (nth 6 attr) 65536)
7844 (floor (mod (nth 6 attr) 65536)))))
7845 ;; Convert file size.
7846 (when (< (nth 7 attr) 0)
7847 (setcar (nthcdr 7 attr) -1))
7848 (when (and (floatp (nth 7 attr))
7849 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
7850 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
7851 ;; Convert file mode bits to string.
7852 (unless (stringp (nth 8 attr))
7853 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
7854 (when (stringp (car attr))
7855 (aset (nth 8 attr) 0 ?l)))
7856 ;; Convert directory indication bit.
7857 (when (string-match "^d" (nth 8 attr))
7858 (setcar attr t))
7859 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
7860 (when (consp (car attr))
7861 (if (and (stringp (caar attr))
7862 (string-match ".+ -> .\\(.+\\)." (caar attr)))
7863 (setcar attr (match-string 1 (caar attr)))
7864 (setcar attr nil)))
7865 ;; Set file's gid change bit.
7866 (setcar (nthcdr 9 attr)
7867 (if (numberp (nth 3 attr))
7868 (not (= (nth 3 attr)
7869 (tramp-get-remote-gid vec 'integer)))
7870 (not (string-equal
7871 (nth 3 attr)
7872 (tramp-get-remote-gid vec 'string)))))
7873 ;; Convert inode.
7874 (unless (listp (nth 10 attr))
7875 (setcar (nthcdr 10 attr)
7876 (condition-case nil
7877 (cons (floor (nth 10 attr) 65536)
7878 (floor (mod (nth 10 attr) 65536)))
7879 ;; Inodes can be incredible huge. We must hide this.
7880 (error (tramp-get-inode vec)))))
7881 ;; Set virtual device number.
7882 (setcar (nthcdr 11 attr)
7883 (tramp-get-device vec))
7884 attr))
7886 (defun tramp-check-cached-permissions (vec access)
7887 "Check `file-attributes' caches for VEC.
7888 Return t if according to the cache access type ACCESS is known to
7889 be granted."
7890 (let ((result nil)
7891 (offset (cond
7892 ((eq ?r access) 1)
7893 ((eq ?w access) 2)
7894 ((eq ?x access) 3))))
7895 (dolist (suffix '("string" "integer") result)
7896 (setq
7897 result
7899 result
7900 (let ((file-attr
7901 (tramp-get-file-property
7902 vec (tramp-file-name-localname vec)
7903 (concat "file-attributes-" suffix) nil))
7904 (remote-uid
7905 (tramp-get-connection-property
7906 vec (concat "uid-" suffix) nil))
7907 (remote-gid
7908 (tramp-get-connection-property
7909 vec (concat "gid-" suffix) nil)))
7910 (and
7911 file-attr
7913 ;; Not a symlink
7914 (eq t (car file-attr))
7915 (null (car file-attr)))
7917 ;; World accessible.
7918 (eq access (aref (nth 8 file-attr) (+ offset 6)))
7919 ;; User accessible and owned by user.
7920 (and
7921 (eq access (aref (nth 8 file-attr) offset))
7922 (equal remote-uid (nth 2 file-attr)))
7923 ;; Group accessible and owned by user's
7924 ;; principal group.
7925 (and
7926 (eq access (aref (nth 8 file-attr) (+ offset 3)))
7927 (equal remote-gid (nth 3 file-attr)))))))))))
7929 (defun tramp-get-inode (vec)
7930 "Returns the virtual inode number.
7931 If it doesn't exist, generate a new one."
7932 (let ((string (tramp-make-tramp-file-name
7933 (tramp-file-name-method vec)
7934 (tramp-file-name-user vec)
7935 (tramp-file-name-host vec)
7936 "")))
7937 (unless (assoc string tramp-inodes)
7938 (add-to-list 'tramp-inodes
7939 (list string (length tramp-inodes))))
7940 (nth 1 (assoc string tramp-inodes))))
7942 (defun tramp-get-device (vec)
7943 "Returns the virtual device number.
7944 If it doesn't exist, generate a new one."
7945 (let ((string (tramp-make-tramp-file-name
7946 (tramp-file-name-method vec)
7947 (tramp-file-name-user vec)
7948 (tramp-file-name-host vec)
7949 "")))
7950 (unless (assoc string tramp-devices)
7951 (add-to-list 'tramp-devices
7952 (list string (length tramp-devices))))
7953 (cons -1 (nth 1 (assoc string tramp-devices)))))
7955 (defun tramp-file-mode-from-int (mode)
7956 "Turn an integer representing a file mode into an ls(1)-like string."
7957 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
7958 (user (logand (lsh mode -6) 7))
7959 (group (logand (lsh mode -3) 7))
7960 (other (logand (lsh mode -0) 7))
7961 (suid (> (logand (lsh mode -9) 4) 0))
7962 (sgid (> (logand (lsh mode -9) 2) 0))
7963 (sticky (> (logand (lsh mode -9) 1) 0)))
7964 (setq user (tramp-file-mode-permissions user suid "s"))
7965 (setq group (tramp-file-mode-permissions group sgid "s"))
7966 (setq other (tramp-file-mode-permissions other sticky "t"))
7967 (concat type user group other)))
7969 (defun tramp-file-mode-permissions (perm suid suid-text)
7970 "Convert a permission bitset into a string.
7971 This is used internally by `tramp-file-mode-from-int'."
7972 (let ((r (> (logand perm 4) 0))
7973 (w (> (logand perm 2) 0))
7974 (x (> (logand perm 1) 0)))
7975 (concat (or (and r "r") "-")
7976 (or (and w "w") "-")
7977 (or (and suid x suid-text) ; suid, execute
7978 (and suid (upcase suid-text)) ; suid, !execute
7979 (and x "x") "-")))) ; !suid
7981 (defun tramp-decimal-to-octal (i)
7982 "Return a string consisting of the octal digits of I.
7983 Not actually used. Use `(format \"%o\" i)' instead?"
7984 (cond ((< i 0) (error "Cannot convert negative number to octal"))
7985 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
7986 ((zerop i) "0")
7987 (t (concat (tramp-decimal-to-octal (/ i 8))
7988 (number-to-string (% i 8))))))
7990 ;; Kudos to Gerd Moellmann for this suggestion.
7991 (defun tramp-octal-to-decimal (ostr)
7992 "Given a string of octal digits, return a decimal number."
7993 (let ((x (or ostr "")))
7994 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
7995 (unless (string-match "\\`[0-7]*\\'" x)
7996 (error "Non-octal junk in string `%s'" x))
7997 (string-to-number ostr 8)))
7999 (defun tramp-shell-case-fold (string)
8000 "Converts STRING to shell glob pattern which ignores case."
8001 (mapconcat
8002 (lambda (c)
8003 (if (equal (downcase c) (upcase c))
8004 (vector c)
8005 (format "[%c%c]" (downcase c) (upcase c))))
8006 string
8007 ""))
8010 ;; ------------------------------------------------------------
8011 ;; -- Tramp file names --
8012 ;; ------------------------------------------------------------
8013 ;; Conversion functions between external representation and
8014 ;; internal data structure. Convenience functions for internal
8015 ;; data structure.
8017 (defun tramp-file-name-p (vec)
8018 "Check, whether VEC is a Tramp object."
8019 (and (vectorp vec) (= 4 (length vec))))
8021 (defun tramp-file-name-method (vec)
8022 "Return method component of VEC."
8023 (and (tramp-file-name-p vec) (aref vec 0)))
8025 (defun tramp-file-name-user (vec)
8026 "Return user component of VEC."
8027 (and (tramp-file-name-p vec) (aref vec 1)))
8029 (defun tramp-file-name-host (vec)
8030 "Return host component of VEC."
8031 (and (tramp-file-name-p vec) (aref vec 2)))
8033 (defun tramp-file-name-localname (vec)
8034 "Return localname component of VEC."
8035 (and (tramp-file-name-p vec) (aref vec 3)))
8037 ;; The user part of a Tramp file name vector can be of kind
8038 ;; "user%domain". Sometimes, we must extract these parts.
8039 (defun tramp-file-name-real-user (vec)
8040 "Return the user name of VEC without domain."
8041 (save-match-data
8042 (let ((user (tramp-file-name-user vec)))
8043 (if (and (stringp user)
8044 (string-match tramp-user-with-domain-regexp user))
8045 (match-string 1 user)
8046 user))))
8048 (defun tramp-file-name-domain (vec)
8049 "Return the domain name of VEC."
8050 (save-match-data
8051 (let ((user (tramp-file-name-user vec)))
8052 (and (stringp user)
8053 (string-match tramp-user-with-domain-regexp user)
8054 (match-string 2 user)))))
8056 ;; The host part of a Tramp file name vector can be of kind
8057 ;; "host#port". Sometimes, we must extract these parts.
8058 (defun tramp-file-name-real-host (vec)
8059 "Return the host name of VEC without port."
8060 (save-match-data
8061 (let ((host (tramp-file-name-host vec)))
8062 (if (and (stringp host)
8063 (string-match tramp-host-with-port-regexp host))
8064 (match-string 1 host)
8065 host))))
8067 (defun tramp-file-name-port (vec)
8068 "Return the port number of VEC."
8069 (save-match-data
8070 (let ((host (tramp-file-name-host vec)))
8071 (and (stringp host)
8072 (string-match tramp-host-with-port-regexp host)
8073 (string-to-number (match-string 2 host))))))
8075 (defun tramp-tramp-file-p (name)
8076 "Return t if NAME is a string with Tramp file name syntax."
8077 (save-match-data
8078 (and (stringp name) (string-match tramp-file-name-regexp name))))
8080 (defun tramp-find-method (method user host)
8081 "Return the right method string to use.
8082 This is METHOD, if non-nil. Otherwise, do a lookup in
8083 `tramp-default-method-alist'."
8084 (or method
8085 (let ((choices tramp-default-method-alist)
8086 lmethod item)
8087 (while choices
8088 (setq item (pop choices))
8089 (when (and (string-match (or (nth 0 item) "") (or host ""))
8090 (string-match (or (nth 1 item) "") (or user "")))
8091 (setq lmethod (nth 2 item))
8092 (setq choices nil)))
8093 lmethod)
8094 tramp-default-method))
8096 (defun tramp-find-user (method user host)
8097 "Return the right user string to use.
8098 This is USER, if non-nil. Otherwise, do a lookup in
8099 `tramp-default-user-alist'."
8100 (or user
8101 (let ((choices tramp-default-user-alist)
8102 luser item)
8103 (while choices
8104 (setq item (pop choices))
8105 (when (and (string-match (or (nth 0 item) "") (or method ""))
8106 (string-match (or (nth 1 item) "") (or host "")))
8107 (setq luser (nth 2 item))
8108 (setq choices nil)))
8109 luser)
8110 tramp-default-user))
8112 (defun tramp-find-host (method user host)
8113 "Return the right host string to use.
8114 This is HOST, if non-nil. Otherwise, it is `tramp-default-host'."
8115 (or (and (> (length host) 0) host)
8116 tramp-default-host))
8118 (defun tramp-dissect-file-name (name &optional nodefault)
8119 "Return a `tramp-file-name' structure.
8120 The structure consists of remote method, remote user, remote host
8121 and localname (file name on remote host). If NODEFAULT is
8122 non-nil, the file name parts are not expanded to their default
8123 values."
8124 (save-match-data
8125 (let ((match (string-match (nth 0 tramp-file-name-structure) name)))
8126 (unless match (error "Not a Tramp file name: %s" name))
8127 (let ((method (match-string (nth 1 tramp-file-name-structure) name))
8128 (user (match-string (nth 2 tramp-file-name-structure) name))
8129 (host (match-string (nth 3 tramp-file-name-structure) name))
8130 (localname (match-string (nth 4 tramp-file-name-structure) name)))
8131 (when (member method '("multi" "multiu"))
8132 (error
8133 "`%s' method is no longer supported, see (info \"(tramp)Multi-hops\")"
8134 method))
8135 (when host
8136 (when (string-match tramp-prefix-ipv6-regexp host)
8137 (setq host (replace-match "" nil t host)))
8138 (when (string-match tramp-postfix-ipv6-regexp host)
8139 (setq host (replace-match "" nil t host))))
8140 (if nodefault
8141 (vector method user host localname)
8142 (vector
8143 (tramp-find-method method user host)
8144 (tramp-find-user method user host)
8145 (tramp-find-host method user host)
8146 localname))))))
8148 (defun tramp-equal-remote (file1 file2)
8149 "Check, whether the remote parts of FILE1 and FILE2 are identical.
8150 The check depends on method, user and host name of the files. If
8151 one of the components is missing, the default values are used.
8152 The local file name parts of FILE1 and FILE2 are not taken into
8153 account.
8155 Example:
8157 (tramp-equal-remote \"/ssh::/etc\" \"/<your host name>:/home\")
8159 would yield `t'. On the other hand, the following check results in nil:
8161 (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")"
8162 (and (stringp (file-remote-p file1))
8163 (stringp (file-remote-p file2))
8164 (string-equal (file-remote-p file1) (file-remote-p file2))))
8166 (defun tramp-make-tramp-file-name (method user host localname)
8167 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME."
8168 (concat tramp-prefix-format
8169 (when (not (zerop (length method)))
8170 (concat method tramp-postfix-method-format))
8171 (when (not (zerop (length user)))
8172 (concat user tramp-postfix-user-format))
8173 (when host
8174 (if (string-match tramp-ipv6-regexp host)
8175 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
8176 host))
8177 tramp-postfix-host-format
8178 (when localname localname)))
8180 (defun tramp-completion-make-tramp-file-name (method user host localname)
8181 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME.
8182 It must not be a complete Tramp file name, but as long as there are
8183 necessary only. This function will be used in file name completion."
8184 (concat tramp-prefix-format
8185 (when (not (zerop (length method)))
8186 (concat method tramp-postfix-method-format))
8187 (when (not (zerop (length user)))
8188 (concat user tramp-postfix-user-format))
8189 (when (not (zerop (length host)))
8190 (concat
8191 (if (string-match tramp-ipv6-regexp host)
8192 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
8193 host)
8194 tramp-postfix-host-format))
8195 (when localname localname)))
8197 (defun tramp-make-copy-program-file-name (vec)
8198 "Create a file name suitable to be passed to `rcp' and workalikes."
8199 (let ((user (tramp-file-name-user vec))
8200 (host (tramp-file-name-real-host vec))
8201 (localname (tramp-shell-quote-argument
8202 (tramp-file-name-localname vec))))
8203 (if (not (zerop (length user)))
8204 (format "%s@%s:%s" user host localname)
8205 (format "%s:%s" host localname))))
8207 (defun tramp-method-out-of-band-p (vec size)
8208 "Return t if this is an out-of-band method, nil otherwise."
8209 (and
8210 ;; It shall be an out-of-band method.
8211 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
8212 ;; Either the file size is large enough, or (in rare cases) there
8213 ;; does not exist a remote encoding.
8214 (or (null tramp-copy-size-limit)
8215 (> size tramp-copy-size-limit)
8216 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
8218 (defun tramp-local-host-p (vec)
8219 "Return t if this points to the local host, nil otherwise."
8220 ;; We cannot use `tramp-file-name-real-host'. A port is an
8221 ;; indication for an ssh tunnel or alike.
8222 (let ((host (tramp-file-name-host vec)))
8223 (and
8224 (stringp host)
8225 (string-match tramp-local-host-regexp host)
8226 ;; The method shall be applied to one of the shell file name
8227 ;; handler. `tramp-local-host-p' is also called for "smb" and
8228 ;; alike, where it must fail.
8229 (tramp-get-method-parameter
8230 (tramp-file-name-method vec) 'tramp-login-program)
8231 ;; The local temp directory must be writable for the other user.
8232 (file-writable-p
8233 (tramp-make-tramp-file-name
8234 (tramp-file-name-method vec)
8235 (tramp-file-name-user vec)
8236 host
8237 (tramp-compat-temporary-file-directory)))
8238 ;; On some systems, chown runs only for root.
8239 (or (zerop (user-uid))
8240 (zerop (tramp-get-remote-uid vec 'integer))))))
8242 ;; Variables local to connection.
8244 (defun tramp-get-remote-path (vec)
8245 (with-connection-property
8246 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
8247 ;; cache the result for the session only. Otherwise, the result
8248 ;; is cached persistently.
8249 (if (memq 'tramp-own-remote-path tramp-remote-path)
8250 (tramp-get-connection-process vec)
8251 vec)
8252 "remote-path"
8253 (let* ((remote-path (copy-tree tramp-remote-path))
8254 (elt1 (memq 'tramp-default-remote-path remote-path))
8255 (elt2 (memq 'tramp-own-remote-path remote-path))
8256 (default-remote-path
8257 (when elt1
8258 (condition-case nil
8259 (tramp-send-command-and-read
8260 vec "echo \\\"`getconf PATH`\\\"")
8261 ;; Default if "getconf" is not available.
8262 (error
8263 (tramp-message
8264 vec 3
8265 "`getconf PATH' not successful, using default value \"%s\"."
8266 "/bin:/usr/bin")
8267 "/bin:/usr/bin"))))
8268 (own-remote-path
8269 (when elt2
8270 (condition-case nil
8271 (tramp-send-command-and-read vec "echo \\\"$PATH\\\"")
8272 ;; Default if "getconf" is not available.
8273 (error
8274 (tramp-message
8275 vec 3 "$PATH not set, ignoring `tramp-own-remote-path'.")
8276 nil)))))
8278 ;; Replace place holder `tramp-default-remote-path'.
8279 (when elt1
8280 (setcdr elt1
8281 (append
8282 (tramp-compat-split-string default-remote-path ":")
8283 (cdr elt1)))
8284 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
8286 ;; Replace place holder `tramp-own-remote-path'.
8287 (when elt2
8288 (setcdr elt2
8289 (append
8290 (tramp-compat-split-string own-remote-path ":")
8291 (cdr elt2)))
8292 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
8294 ;; Remove double entries.
8295 (setq elt1 remote-path)
8296 (while (consp elt1)
8297 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
8298 (setcar elt2 nil))
8299 (setq elt1 (cdr elt1)))
8301 ;; Remove non-existing directories.
8302 (delq
8304 (mapcar
8305 (lambda (x)
8306 (and
8307 (stringp x)
8308 (file-directory-p
8309 (tramp-make-tramp-file-name
8310 (tramp-file-name-method vec)
8311 (tramp-file-name-user vec)
8312 (tramp-file-name-host vec)
8315 remote-path)))))
8317 (defun tramp-get-remote-tmpdir (vec)
8318 (with-connection-property vec "tmp-directory"
8319 (let ((dir (tramp-shell-quote-argument "/tmp")))
8320 (if (and (zerop
8321 (tramp-send-command-and-check
8322 vec (format "%s -d %s" (tramp-get-test-command vec) dir)))
8323 (zerop
8324 (tramp-send-command-and-check
8325 vec (format "%s -w %s" (tramp-get-test-command vec) dir))))
8327 (tramp-error vec 'file-error "Directory %s not accessible" dir)))))
8329 (defun tramp-get-ls-command (vec)
8330 (with-connection-property vec "ls"
8331 (tramp-message vec 5 "Finding a suitable `ls' command")
8333 (catch 'ls-found
8334 (dolist (cmd '("ls" "gnuls" "gls"))
8335 (let ((dl (tramp-get-remote-path vec))
8336 result)
8337 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
8338 ;; Check parameters. On busybox, "ls" output coloring is
8339 ;; enabled by default sometimes. So we try to disable it
8340 ;; when possible. $LS_COLORING is not supported there.
8341 ;; Some "ls" versions are sensible wrt the order of
8342 ;; arguments, they fail when "-al" is after the
8343 ;; "--color=never" argument (for example on FreeBSD).
8344 (when (zerop (tramp-send-command-and-check
8345 vec (format "%s -lnd /" result)))
8346 (when (zerop (tramp-send-command-and-check
8347 vec (format
8348 "%s --color=never -al /dev/null" result)))
8349 (setq result (concat result " --color=never")))
8350 (throw 'ls-found result))
8351 (setq dl (cdr dl))))))
8352 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
8354 (defun tramp-get-ls-command-with-dired (vec)
8355 (save-match-data
8356 (with-connection-property vec "ls-dired"
8357 (tramp-message vec 5 "Checking, whether `ls --dired' works")
8358 ;; Some "ls" versions are sensible wrt the order of arguments,
8359 ;; they fail when "-al" is after the "--dired" argument (for
8360 ;; example on FreeBSD).
8361 (zerop (tramp-send-command-and-check
8362 vec (format "%s --dired -al /dev/null"
8363 (tramp-get-ls-command vec)))))))
8365 (defun tramp-get-test-command (vec)
8366 (with-connection-property vec "test"
8367 (tramp-message vec 5 "Finding a suitable `test' command")
8368 (if (zerop (tramp-send-command-and-check vec "test 0"))
8369 "test"
8370 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
8372 (defun tramp-get-test-nt-command (vec)
8373 ;; Does `test A -nt B' work? Use abominable `find' construct if it
8374 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
8375 ;; for otherwise the shell crashes.
8376 (with-connection-property vec "test-nt"
8378 (progn
8379 (tramp-send-command
8380 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
8381 (with-current-buffer (tramp-get-buffer vec)
8382 (goto-char (point-min))
8383 (when (looking-at (regexp-quote tramp-end-of-output))
8384 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
8385 (progn
8386 (tramp-send-command
8388 (format
8389 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
8390 (tramp-get-test-command vec)))
8391 "tramp_test_nt %s %s"))))
8393 (defun tramp-get-file-exists-command (vec)
8394 (with-connection-property vec "file-exists"
8395 (tramp-message vec 5 "Finding command to check if file exists")
8396 (tramp-find-file-exists-command vec)))
8398 (defun tramp-get-remote-ln (vec)
8399 (with-connection-property vec "ln"
8400 (tramp-message vec 5 "Finding a suitable `ln' command")
8401 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
8403 (defun tramp-get-remote-perl (vec)
8404 (with-connection-property vec "perl"
8405 (tramp-message vec 5 "Finding a suitable `perl' command")
8406 (let ((result
8407 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
8408 (tramp-find-executable
8409 vec "perl" (tramp-get-remote-path vec)))))
8410 ;; We must check also for some Perl modules.
8411 (when result
8412 (with-connection-property vec "perl-file-spec"
8413 (zerop
8414 (tramp-send-command-and-check
8415 vec (format "%s -e 'use File::Spec;'" result))))
8416 (with-connection-property vec "perl-cwd-realpath"
8417 (zerop
8418 (tramp-send-command-and-check
8419 vec (format "%s -e 'use Cwd \"realpath\";'" result)))))
8420 result)))
8422 (defun tramp-get-remote-stat (vec)
8423 (with-connection-property vec "stat"
8424 (tramp-message vec 5 "Finding a suitable `stat' command")
8425 (let ((result (tramp-find-executable
8426 vec "stat" (tramp-get-remote-path vec)))
8427 tmp)
8428 ;; Check whether stat(1) returns usable syntax. %s does not
8429 ;; work on older AIX systems.
8430 (when result
8431 (setq tmp
8432 ;; We don't want to display an error message.
8433 (with-temp-message (or (current-message) "")
8434 (condition-case nil
8435 (tramp-send-command-and-read
8436 vec (format "%s -c '(\"%%N\" %%s)' /" result))
8437 (error nil))))
8438 (unless (and (listp tmp) (stringp (car tmp))
8439 (string-match "^./.$" (car tmp))
8440 (integerp (cadr tmp)))
8441 (setq result nil)))
8442 result)))
8444 (defun tramp-get-remote-readlink (vec)
8445 (with-connection-property vec "readlink"
8446 (tramp-message vec 5 "Finding a suitable `readlink' command")
8447 (let ((result (tramp-find-executable
8448 vec "readlink" (tramp-get-remote-path vec))))
8449 (when (and result
8450 ;; We don't want to display an error message.
8451 (with-temp-message (or (current-message) "")
8452 (condition-case nil
8453 (zerop
8454 (tramp-send-command-and-check
8455 vec (format "%s --canonicalize-missing /" result)))
8456 (error nil))))
8457 result))))
8459 (defun tramp-get-remote-trash (vec)
8460 (with-connection-property vec "trash"
8461 (tramp-message vec 5 "Finding a suitable `trash' command")
8462 (tramp-find-executable vec "trash" (tramp-get-remote-path vec))))
8464 (defun tramp-get-remote-id (vec)
8465 (with-connection-property vec "id"
8466 (tramp-message vec 5 "Finding POSIX `id' command")
8468 (catch 'id-found
8469 (let ((dl (tramp-get-remote-path vec))
8470 result)
8471 (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
8472 ;; Check POSIX parameter.
8473 (when (zerop (tramp-send-command-and-check
8474 vec (format "%s -u" result)))
8475 (throw 'id-found result))
8476 (setq dl (cdr dl)))))
8477 (tramp-error vec 'file-error "Couldn't find a POSIX `id' command"))))
8479 (defun tramp-get-remote-uid (vec id-format)
8480 (with-connection-property vec (format "uid-%s" id-format)
8481 (let ((res (tramp-send-command-and-read
8483 (format "%s -u%s %s"
8484 (tramp-get-remote-id vec)
8485 (if (equal id-format 'integer) "" "n")
8486 (if (equal id-format 'integer)
8487 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8488 ;; The command might not always return a number.
8489 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
8491 (defun tramp-get-remote-gid (vec id-format)
8492 (with-connection-property vec (format "gid-%s" id-format)
8493 (let ((res (tramp-send-command-and-read
8495 (format "%s -g%s %s"
8496 (tramp-get-remote-id vec)
8497 (if (equal id-format 'integer) "" "n")
8498 (if (equal id-format 'integer)
8499 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8500 ;; The command might not always return a number.
8501 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
8503 (defun tramp-get-local-uid (id-format)
8504 (if (equal id-format 'integer) (user-uid) (user-login-name)))
8506 (defun tramp-get-local-gid (id-format)
8507 (nth 3 (tramp-compat-file-attributes "~/" id-format)))
8509 ;; Some predefined connection properties.
8510 (defun tramp-get-inline-compress (vec prop size)
8511 "Return the compress command related to PROP.
8512 PROP is either `inline-compress' or `inline-decompress'. SIZE is
8513 the length of the file to be compressed.
8515 If no corresponding command is found, nil is returned."
8516 (when (and (integerp tramp-inline-compress-start-size)
8517 (> size tramp-inline-compress-start-size))
8518 (with-connection-property vec prop
8519 (tramp-find-inline-compress vec)
8520 (tramp-get-connection-property vec prop nil))))
8522 (defun tramp-get-inline-coding (vec prop size)
8523 "Return the coding command related to PROP.
8524 PROP is either `remote-encoding', `remode-decoding',
8525 `local-encoding' or `local-decoding'.
8527 SIZE is the length of the file to be coded. Depending on SIZE,
8528 compression might be applied.
8530 If no corresponding command is found, nil is returned.
8531 Otherwise, either a string is returned which contains a `%s' mark
8532 to be used for the respective input or output file; or a Lisp
8533 function cell is returned to be applied on a buffer."
8534 (let ((coding
8535 (with-connection-property vec prop
8536 (tramp-find-inline-encoding vec)
8537 (tramp-get-connection-property vec prop nil)))
8538 (prop1 (if (string-match "encoding" prop)
8539 "inline-compress" "inline-decompress"))
8540 compress)
8541 ;; The connection property might have been cached. So we must send
8542 ;; the script to the remote side - maybe.
8543 (when (and coding (symbolp coding) (string-match "remote" prop))
8544 (let ((name (symbol-name coding)))
8545 (while (string-match (regexp-quote "-") name)
8546 (setq name (replace-match "_" nil t name)))
8547 (tramp-maybe-send-script vec (symbol-value coding) name)
8548 (setq coding name)))
8549 (when coding
8550 ;; Check for the `compress' command.
8551 (setq compress (tramp-get-inline-compress vec prop1 size))
8552 ;; Return the value.
8553 (cond
8554 ((and compress (symbolp coding))
8555 (if (string-match "decompress" prop1)
8556 `(lambda (beg end)
8557 (,coding beg end)
8558 (let ((coding-system-for-write 'binary)
8559 (coding-system-for-read 'binary))
8560 (apply
8561 'call-process-region (point-min) (point-max)
8562 (car (split-string ,compress)) t t nil
8563 (cdr (split-string ,compress)))))
8564 `(lambda (beg end)
8565 (let ((coding-system-for-write 'binary)
8566 (coding-system-for-read 'binary))
8567 (apply
8568 'call-process-region beg end
8569 (car (split-string ,compress)) t t nil
8570 (cdr (split-string ,compress))))
8571 (,coding (point-min) (point-max)))))
8572 ((symbolp coding)
8573 coding)
8574 ((and compress (string-match "decoding" prop))
8575 (format "(%s | %s >%%s)" coding compress))
8576 (compress
8577 (format "(%s <%%s | %s)" compress coding))
8578 ((string-match "decoding" prop)
8579 (format "%s >%%s" coding))
8581 (format "%s <%%s" coding))))))
8583 (defun tramp-get-method-parameter (method param)
8584 "Return the method parameter PARAM.
8585 If the `tramp-methods' entry does not exist, return nil."
8586 (let ((entry (assoc param (assoc method tramp-methods))))
8587 (when entry (cadr entry))))
8589 ;; Auto saving to a special directory.
8591 (defun tramp-exists-file-name-handler (operation &rest args)
8592 "Check, whether OPERATION runs a file name handler."
8593 ;; The file name handler is determined on base of either an
8594 ;; argument, `buffer-file-name', or `default-directory'.
8595 (condition-case nil
8596 (let* ((buffer-file-name "/")
8597 (default-directory "/")
8598 (fnha file-name-handler-alist)
8599 (check-file-name-operation operation)
8600 (file-name-handler-alist
8601 (list
8602 (cons "/"
8603 (lambda (operation &rest args)
8604 "Returns OPERATION if it is the one to be checked."
8605 (if (equal check-file-name-operation operation)
8606 operation
8607 (let ((file-name-handler-alist fnha))
8608 (apply operation args))))))))
8609 (equal (apply operation args) operation))
8610 (error nil)))
8612 (unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
8613 (defadvice make-auto-save-file-name
8614 (around tramp-advice-make-auto-save-file-name () activate)
8615 "Invoke `tramp-handle-make-auto-save-file-name' for Tramp files."
8616 (if (tramp-tramp-file-p (buffer-file-name))
8617 ;; We cannot call `tramp-handle-make-auto-save-file-name'
8618 ;; directly, because this would bypass the locking mechanism.
8619 (setq ad-return-value
8620 (tramp-file-name-handler 'make-auto-save-file-name))
8621 ad-do-it))
8622 (add-hook
8623 'tramp-unload-hook
8624 (lambda ()
8625 (ad-remove-advice
8626 'make-auto-save-file-name
8627 'around 'tramp-advice-make-auto-save-file-name)
8628 (ad-activate 'make-auto-save-file-name))))
8630 ;; In XEmacs < 21.5, autosaved remote files have permission 0666 minus
8631 ;; umask. This is a security threat.
8633 (defun tramp-set-auto-save-file-modes ()
8634 "Set permissions of autosaved remote files to the original permissions."
8635 (let ((bfn (buffer-file-name)))
8636 (when (and (tramp-tramp-file-p bfn)
8637 (buffer-modified-p)
8638 (stringp buffer-auto-save-file-name)
8639 (not (equal bfn buffer-auto-save-file-name)))
8640 (unless (file-exists-p buffer-auto-save-file-name)
8641 (write-region "" nil buffer-auto-save-file-name))
8642 ;; Permissions should be set always, because there might be an old
8643 ;; auto-saved file belonging to another original file. This could
8644 ;; be a security threat.
8645 (set-file-modes buffer-auto-save-file-name
8646 (or (file-modes bfn) (tramp-octal-to-decimal "0600"))))))
8648 (unless (and (featurep 'xemacs)
8649 (= emacs-major-version 21)
8650 (> emacs-minor-version 4))
8651 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes)
8652 (add-hook 'tramp-unload-hook
8653 (lambda ()
8654 (remove-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))))
8656 (defun tramp-subst-strs-in-string (alist string)
8657 "Replace all occurrences of the string FROM with TO in STRING.
8658 ALIST is of the form ((FROM . TO) ...)."
8659 (save-match-data
8660 (while alist
8661 (let* ((pr (car alist))
8662 (from (car pr))
8663 (to (cdr pr)))
8664 (while (string-match (regexp-quote from) string)
8665 (setq string (replace-match to t t string)))
8666 (setq alist (cdr alist))))
8667 string))
8669 ;; ------------------------------------------------------------
8670 ;; -- Compatibility functions section --
8671 ;; ------------------------------------------------------------
8673 (defun tramp-read-passwd (proc &optional prompt)
8674 "Read a password from user (compat function).
8675 Consults the auth-source package.
8676 Invokes `password-read' if available, `read-passwd' else."
8677 (let* ((key (tramp-make-tramp-file-name
8678 tramp-current-method tramp-current-user
8679 tramp-current-host ""))
8680 (pw-prompt
8681 (or prompt
8682 (with-current-buffer (process-buffer proc)
8683 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
8684 (format "%s for %s " (capitalize (match-string 1)) key)))))
8685 (with-parsed-tramp-file-name key nil
8686 (prog1
8688 ;; See if auth-sources contains something useful, if it's bound.
8689 (and (boundp 'auth-sources)
8690 (tramp-get-connection-property v "first-password-request" nil)
8691 ;; Try with Tramp's current method.
8692 (tramp-compat-funcall
8693 'auth-source-user-or-password
8694 "password" tramp-current-host tramp-current-method))
8695 ;; Try the password cache.
8696 (when (functionp 'password-read)
8697 (unless (tramp-get-connection-property
8698 v "first-password-request" nil)
8699 (tramp-compat-funcall 'password-cache-remove key))
8700 (let ((password
8701 (tramp-compat-funcall 'password-read pw-prompt key)))
8702 (tramp-compat-funcall 'password-cache-add key password)
8703 password))
8704 ;; Else, get the password interactively.
8705 (read-passwd pw-prompt))
8706 (tramp-set-connection-property v "first-password-request" nil)))))
8708 (defun tramp-clear-passwd (vec)
8709 "Clear password cache for connection related to VEC."
8710 (tramp-compat-funcall
8711 'password-cache-remove
8712 (tramp-make-tramp-file-name
8713 (tramp-file-name-method vec)
8714 (tramp-file-name-user vec)
8715 (tramp-file-name-host vec)
8716 "")))
8718 ;; Snarfed code from time-date.el and parse-time.el
8720 (defconst tramp-half-a-year '(241 17024)
8721 "Evaluated by \"(days-to-time 183)\".")
8723 (defconst tramp-parse-time-months
8724 '(("jan" . 1) ("feb" . 2) ("mar" . 3)
8725 ("apr" . 4) ("may" . 5) ("jun" . 6)
8726 ("jul" . 7) ("aug" . 8) ("sep" . 9)
8727 ("oct" . 10) ("nov" . 11) ("dec" . 12))
8728 "Alist mapping month names to integers.")
8730 (defun tramp-time-less-p (t1 t2)
8731 "Say whether time value T1 is less than time value T2."
8732 (unless t1 (setq t1 '(0 0)))
8733 (unless t2 (setq t2 '(0 0)))
8734 (or (< (car t1) (car t2))
8735 (and (= (car t1) (car t2))
8736 (< (nth 1 t1) (nth 1 t2)))))
8738 (defun tramp-time-subtract (t1 t2)
8739 "Subtract two time values.
8740 Return the difference in the format of a time value."
8741 (unless t1 (setq t1 '(0 0)))
8742 (unless t2 (setq t2 '(0 0)))
8743 (let ((borrow (< (cadr t1) (cadr t2))))
8744 (list (- (car t1) (car t2) (if borrow 1 0))
8745 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
8747 (defun tramp-time-diff (t1 t2)
8748 "Return the difference between the two times, in seconds.
8749 T1 and T2 are time values (as returned by `current-time' for example)."
8750 ;; Pacify byte-compiler with `symbol-function'.
8751 (cond ((and (fboundp 'subtract-time)
8752 (fboundp 'float-time))
8753 (tramp-compat-funcall
8754 'float-time (tramp-compat-funcall 'subtract-time t1 t2)))
8755 ((and (fboundp 'subtract-time)
8756 (fboundp 'time-to-seconds))
8757 (tramp-compat-funcall
8758 'time-to-seconds (tramp-compat-funcall 'subtract-time t1 t2)))
8759 ((fboundp 'itimer-time-difference)
8760 (tramp-compat-funcall
8761 'itimer-time-difference
8762 (if (< (length t1) 3) (append t1 '(0)) t1)
8763 (if (< (length t2) 3) (append t2 '(0)) t2)))
8765 (let ((time (tramp-time-subtract t1 t2)))
8766 (+ (* (car time) 65536.0)
8767 (cadr time)
8768 (/ (or (nth 2 time) 0) 1000000.0))))))
8770 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
8771 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
8772 EOL-TYPE can be one of `dos', `unix', or `mac'."
8773 (cond ((fboundp 'coding-system-change-eol-conversion)
8774 (tramp-compat-funcall
8775 'coding-system-change-eol-conversion coding-system eol-type))
8776 ((fboundp 'subsidiary-coding-system)
8777 (tramp-compat-funcall
8778 'subsidiary-coding-system coding-system
8779 (cond ((eq eol-type 'dos) 'crlf)
8780 ((eq eol-type 'unix) 'lf)
8781 ((eq eol-type 'mac) 'cr)
8783 (error "Unknown EOL-TYPE `%s', must be %s"
8784 eol-type
8785 "`dos', `unix', or `mac'")))))
8786 (t (error "Can't change EOL conversion -- is MULE missing?"))))
8788 (defun tramp-set-process-query-on-exit-flag (process flag)
8789 "Specify if query is needed for process when Emacs is exited.
8790 If the second argument flag is non-nil, Emacs will query the user before
8791 exiting if process is running."
8792 (if (fboundp 'set-process-query-on-exit-flag)
8793 (tramp-compat-funcall 'set-process-query-on-exit-flag process flag)
8794 (tramp-compat-funcall 'process-kill-without-query process flag)))
8797 ;; ------------------------------------------------------------
8798 ;; -- Kludges section --
8799 ;; ------------------------------------------------------------
8801 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
8802 ;; does not deal well with newline characters. Newline is replaced by
8803 ;; backslash newline. But if, say, the string `a backslash newline b'
8804 ;; is passed to a shell, the shell will expand this into "ab",
8805 ;; completely omitting the newline. This is not what was intended.
8806 ;; It does not appear to be possible to make the function
8807 ;; `shell-quote-argument' work with newlines without making it
8808 ;; dependent on the shell used. But within this package, we know that
8809 ;; we will always use a Bourne-like shell, so we use an approach which
8810 ;; groks newlines.
8812 ;; The approach is simple: we call `shell-quote-argument', then
8813 ;; massage the newline part of the result.
8815 ;; This function should produce a string which is grokked by a Unix
8816 ;; shell, even if the Emacs is running on Windows. Since this is the
8817 ;; kludges section, we bind `system-type' in such a way that
8818 ;; `shell-quote-arguments' behaves as if on Unix.
8820 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
8821 ;; function to work with Bourne-like shells.
8823 ;; CCC: This function should be rewritten so that
8824 ;; `shell-quote-argument' is not used. This way, we are safe from
8825 ;; changes in `shell-quote-argument'.
8826 (defun tramp-shell-quote-argument (s)
8827 "Similar to `shell-quote-argument', but groks newlines.
8828 Only works for Bourne-like shells."
8829 (let ((system-type 'not-windows))
8830 (save-match-data
8831 (let ((result (shell-quote-argument s))
8832 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
8833 (when (and (>= (length result) 2)
8834 (string= (substring result 0 2) "\\~"))
8835 (setq result (substring result 1)))
8836 (while (string-match nl result)
8837 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
8838 t t result)))
8839 result))))
8841 ;; Checklist for `tramp-unload-hook'
8842 ;; - Unload all `tramp-*' packages
8843 ;; - Reset `file-name-handler-alist'
8844 ;; - Cleanup hooks where Tramp functions are in
8845 ;; - Cleanup advised functions
8846 ;; - Cleanup autoloads
8847 ;;;###autoload
8848 (defun tramp-unload-tramp ()
8849 "Discard Tramp from loading remote files."
8850 (interactive)
8851 ;; When Tramp is not loaded yet, its autoloads are still active.
8852 (tramp-unload-file-name-handlers)
8853 ;; ange-ftp settings must be enabled.
8854 (tramp-compat-funcall 'tramp-ftp-enable-ange-ftp)
8855 ;; Maybe its not loaded yet.
8856 (condition-case nil
8857 (unload-feature 'tramp 'force)
8858 (error nil)))
8860 (when (and load-in-progress
8861 (string-match "Loading tramp..." (or (current-message) "")))
8862 (message "Loading tramp...done"))
8864 (provide 'tramp)
8866 ;;; TODO:
8868 ;; * Handle nonlocal exits such as C-g.
8869 ;; * But it would probably be better to use with-local-quit at the
8870 ;; place where it's actually needed: around any potentially
8871 ;; indefinitely blocking piece of code. In this case it would be
8872 ;; within Tramp around one of its calls to accept-process-output (or
8873 ;; around one of the loops that calls accept-process-output)
8874 ;; (Stefan Monnier).
8875 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
8876 ;; `shell-quote-argument'.
8877 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
8878 ;; by the files in that directory. Add this here.
8879 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
8880 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
8881 ;; * Don't use globbing for directories with many files, as this is
8882 ;; likely to produce long command lines, and some shells choke on
8883 ;; long command lines.
8884 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
8885 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
8886 ;; * abbreviate-file-name
8887 ;; * Better error checking. At least whenever we see something
8888 ;; strange when doing zerop, we should kill the process and start
8889 ;; again. (Greg Stark)
8890 ;; * Remove unneeded parameters from methods.
8891 ;; * Make it work for different encodings, and for different file name
8892 ;; encodings, too. (Daniel Pittman)
8893 ;; * Don't search for perl5 and perl. Instead, only search for perl and
8894 ;; then look if it's the right version (with `perl -v').
8895 ;; * When editing a remote CVS controlled file as a different user, VC
8896 ;; gets confused about the file locking status. Try to find out why
8897 ;; the workaround doesn't work.
8898 ;; * Username and hostname completion.
8899 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode-p'.
8900 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
8901 ;; Code is nearly identical.
8902 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
8903 ;; until the last but one hop via `start-file-process'. Apply it
8904 ;; also for ftp and smb.
8905 ;; * WIBNI if we had a command "trampclient"? If I was editing in
8906 ;; some shell with root priviledges, it would be nice if I could
8907 ;; just call
8908 ;; trampclient filename.c
8909 ;; as an editor, and the _current_ shell would connect to an Emacs
8910 ;; server and would be used in an existing non-priviledged Emacs
8911 ;; session for doing the editing in question.
8912 ;; That way, I need not tell Emacs my password again and be afraid
8913 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
8914 ;; once display a just typed password in the context of a keyboard
8915 ;; sequence prompt for a question immediately following in a shell
8916 ;; script run within Emacs -- nasty).
8917 ;; And if I have some ssh session running to a different computer,
8918 ;; having the possibility of passing a local file there to a local
8919 ;; Emacs session (in case I can arrange for a connection back) would
8920 ;; be nice.
8921 ;; Likely the corresponding Tramp server should not allow the
8922 ;; equivalent of the emacsclient -eval option in order to make this
8923 ;; reasonably unproblematic. And maybe trampclient should have some
8924 ;; way of passing credentials, like by using an SSL socket or
8925 ;; something. (David Kastrup)
8926 ;; * Reconnect directly to a compliant shell without first going
8927 ;; through the user's default shell. (Pete Forman)
8928 ;; * Make `tramp-default-user' obsolete.
8929 ;; * How can I interrupt the remote process with a signal
8930 ;; (interrupt-process seems not to work)? (Markus Triska)
8931 ;; * Avoid the local shell entirely for starting remote processes. If
8932 ;; so, I think even a signal, when delivered directly to the local
8933 ;; SSH instance, would correctly be propagated to the remote process
8934 ;; automatically; possibly SSH would have to be started with
8935 ;; "-t". (Markus Triska)
8936 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
8937 ;; isn't on the remote host. (Mark A. Hershberger)
8938 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
8939 ;; * Implement a general server-local-variable mechanism, as there are
8940 ;; probably other variables that need different values for different
8941 ;; servers too. The user could then configure a variable (such as
8942 ;; tramp-server-local-variable-alist) to define any such variables
8943 ;; that they need to, which would then be let bound as appropriate
8944 ;; in tramp functions. (Jason Rumney)
8945 ;; * Optimize out-of-band copying, when both methods are scp-like (not
8946 ;; rsync).
8947 ;; * Keep a second connection open for out-of-band methods like scp or
8948 ;; rsync.
8949 ;; * Support ptys in `tramp-handle-start-file-process'. (Bug#4604, Bug#6360)
8950 ;; * IMHO, it's a drawback that currently Tramp doesn't support
8951 ;; Unicode in Dired file names by default. Is it possible to
8952 ;; improve Tramp to set LC_ALL to "C" only for commands where Tramp
8953 ;; expects English? Or just to set LC_MESSAGES to "C" if Tramp
8954 ;; expects only English messages? (Juri Linkov)
8955 ;; * Make shadowfile.el grok Tramp filenames. (Bug#4526, Bug#4846)
8956 ;; * Do not handle files with drive letter as remote. (Bug#5447)
8957 ;; * Load Tramp subpackages only when needed. (Bug#1529, Bug#5448, Bug#5705)
8958 ;; * Try telnet+curl as new method. It might be useful for busybox,
8959 ;; without built-in uuencode/uudecode.
8960 ;; * Let `shell-dynamic-complete-*' and `comint-dynamic-complete' work
8961 ;; on remote hosts.
8962 ;; * Load ~/.emacs_SHELLNAME on the remote host for `shell'.
8964 ;; Functions for file-name-handler-alist:
8965 ;; diff-latest-backup-file -- in diff.el
8967 ;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
8968 ;;; tramp.el ends here
8970 ;; Local Variables:
8971 ;; mode: Emacs-Lisp
8972 ;; coding: utf-8
8973 ;; End: