1 ;;; -*- mode: Emacs-Lisp; coding: iso-2022-7bit; -*-
2 ;;; tramp.el --- Transparent Remote Access, Multiple Protocol
4 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 ;; 2005, 2006 Free Software Foundation, Inc.
7 ;; Author: Kai Gro
\e,A_
\e(Bjohann <kai.grossjohann@gmx.net>
8 ;; Michael Albinus <michael.albinus@gmx.de>
9 ;; Keywords: comm, processes
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; This package provides remote file editing, similar to ange-ftp.
31 ;; The difference is that ange-ftp uses FTP to transfer files between
32 ;; the local and the remote host, whereas tramp.el uses a combination
33 ;; of rsh and rcp or other work-alike programs, such as ssh/scp.
35 ;; For more detailed instructions, please see the info file.
40 ;; This package only works for Emacs 20 and higher, and for XEmacs 21
41 ;; and higher. (XEmacs 20 is missing the `with-timeout' macro. Emacs
42 ;; 19 is reported to have other problems. For XEmacs 21, you need the
43 ;; package `fsf-compat' for the `with-timeout' macro.)
45 ;; This version might not work with pre-Emacs 21 VC unless VC is
46 ;; loaded before tramp.el. Could you please test this and tell me about
47 ;; the result? Thanks.
49 ;; Also see the todo list at the bottom of this file.
51 ;; The current version of Tramp can be retrieved from the following URL:
52 ;; http://ftp.gnu.org/gnu/tramp/
54 ;; There's a mailing list for this, as well. Its name is:
55 ;; tramp-devel@gnu.org
56 ;; You can use the Web to subscribe, under the following URL:
57 ;; http://lists.gnu.org/mailman/listinfo/tramp-devel
59 ;; For the adventurous, the current development sources are available
60 ;; via CVS. You can find instructions about this at the following URL:
61 ;; http://savannah.gnu.org/projects/tramp/
62 ;; Click on "CVS" in the navigation bar near the top.
64 ;; Don't forget to put on your asbestos longjohns, first!
68 ;; The Tramp version number and bug report address, as prepared by configure.
70 (add-hook 'tramp-unload-hook
72 (when (featurep 'trampver)
73 (unload-feature 'trampver 'force))))
75 (if (featurep 'xemacs)
76 (require 'timer-funcs)
79 (require 'format-spec) ;from Gnus 5.8, also in tar ball
80 ;; As long as password.el is not part of (X)Emacs, it shouldn't
82 (if (featurep 'xemacs)
83 (load "password" 'noerror)
84 (require 'password nil 'noerror)) ;from No Gnus, also in tar ball
86 ;; The explicit check is not necessary in Emacs, which provides the
87 ;; feature even if implemented in C, but it appears to be necessary
89 (unless (and (fboundp 'base64-encode-region)
90 (fboundp 'base64-decode-region))
91 (require 'base64)) ;for the mimencode methods
95 (autoload 'tramp-uuencode-region "tramp-uu"
96 "Implementation of `uuencode' in Lisp.")
97 (add-hook 'tramp-unload-hook
99 (when (featurep 'tramp-uu)
100 (unload-feature 'tramp-uu 'force))))
102 (unless (fboundp 'uudecode-decode-region)
103 (autoload 'uudecode-decode-region "uudecode"))
105 ;; XEmacs is distributed with few Lisp packages. Further packages are
106 ;; installed using EFS. If we use a unified filename format, then
107 ;; Tramp is required in addition to EFS. (But why can't Tramp just
108 ;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
109 ;; just like before.) Another reason for using a separate filename
110 ;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
111 ;; Tramp only knows how to deal with `file-name-handler-alist', not
114 (defvar tramp-unified-filenames (not (featurep 'xemacs))
115 "Non-nil means to use unified Ange-FTP/Tramp filename syntax.
116 Otherwise, use a separate filename syntax for Tramp.")
118 ;; Load foreign methods. Because they do require Tramp internally, this
119 ;; must be done with the `eval-after-load' trick.
121 ;; tramp-ftp supports Ange-FTP only. Not suited for XEmacs therefore.
122 (unless (featurep 'xemacs)
123 (eval-after-load "tramp"
126 (add-hook 'tramp-unload-hook
128 (when (featurep 'tramp-ftp)
129 (unload-feature 'tramp-ftp 'force)))))))
130 (when (and tramp-unified-filenames (featurep 'xemacs))
131 (eval-after-load "tramp"
134 (add-hook 'tramp-unload-hook
136 (when (featurep 'tramp-efs)
137 (unload-feature 'tramp-efs 'force)))))))
139 ;; tramp-smb uses "smbclient" from Samba.
140 ;; Not available under Cygwin and Windows, because they don't offer
141 ;; "smbclient". And even not necessary there, because Emacs supports
142 ;; UNC file names like "//host/share/localname".
143 (unless (memq system-type '(cygwin windows-nt))
144 (eval-after-load "tramp"
147 (add-hook 'tramp-unload-hook
149 (when (featurep 'tramp-smb)
150 (unload-feature 'tramp-smb 'force)))))))
154 ;; Emacs 19.34 compatibility hack -- is this needed?
155 (or (>= emacs-major-version 20)
158 (unless (boundp 'custom-print-functions)
159 (defvar custom-print-functions nil)) ; not autoloaded before Emacs 20.4
161 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
162 ;; Currently, XEmacs supports this.
164 (when (featurep 'xemacs)
165 (let (unused-vars) ; Pacify Emacs byte-compiler
166 (defalias 'warnings 'identity) ; Pacify Emacs byte-compiler
167 (byte-compiler-options (warnings (- unused-vars))))))
169 ;; `directory-sep-char' is an obsolete variable in Emacs. But it is
170 ;; used in XEmacs, so we set it here and there. The following is needed
171 ;; to pacify Emacs byte-compiler.
173 (when (boundp 'byte-compile-not-obsolete-var)
174 (setq byte-compile-not-obsolete-var 'directory-sep-char)))
176 ;;; User Customizable Internal Variables:
179 "Edit remote files with a combination of rsh and rcp or similar programs."
183 (defcustom tramp-verbose 9
184 "*Verbosity level for tramp.el. 0 means be silent, 10 is most verbose."
188 (defcustom tramp-debug-buffer nil
189 "*Whether to send all commands and responses to a debug buffer."
195 (when (boundp 'backup-directory-alist)
196 (defcustom tramp-backup-directory-alist nil
197 "Alist of filename patterns and backup directory names.
198 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
199 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
200 is a local file name, the backup directory is prepended with Tramp file
201 name prefix \(multi-method, method, user, host\) of file.
203 \(setq tramp-backup-directory-alist backup-directory-alist\)
205 gives the same backup policy for Tramp files on their hosts like the
206 policy for local files."
208 :type '(repeat (cons (regexp :tag "Regexp matching filename")
209 (directory :tag "Backup directory name"))))))
211 ;; XEmacs case. We cannot check for `bkup-backup-directory-info', because
212 ;; the package "backup-dir" might not be loaded yet.
214 (when (featurep 'xemacs)
215 (defcustom tramp-bkup-backup-directory-info nil
216 "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
217 It has the same meaning like `bkup-backup-directory-info' from package
218 `backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local
219 file name, the backup directory is prepended with Tramp file name prefix
220 \(multi-method, method, user, host\) of file.
222 \(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\)
224 gives the same backup policy for Tramp files on their hosts like the
225 policy for local files."
227 (list (regexp :tag "File regexp")
228 (string :tag "Backup Dir")
233 (const search-upward))))
236 (defcustom tramp-auto-save-directory nil
237 "*Put auto-save files in this directory, if set.
238 The idea is to use a local directory so that auto-saving is faster."
240 :type '(choice (const nil)
243 (defcustom tramp-encoding-shell
244 (if (memq system-type '(windows-nt))
247 "*Use this program for encoding and decoding commands on the local host.
248 This shell is used to execute the encoding and decoding command on the
249 local host, so if you want to use `~' in those commands, you should
250 choose a shell here which groks tilde expansion. `/bin/sh' normally
251 does not understand tilde expansion.
253 For encoding and deocding, commands like the following are executed:
255 /bin/sh -c COMMAND < INPUT > OUTPUT
257 This variable can be used to change the \"/bin/sh\" part. See the
258 variable `tramp-encoding-command-switch' for the \"-c\" part. Also, see the
259 variable `tramp-encoding-reads-stdin' to specify whether the commands read
260 standard input or a file.
262 Note that this variable is not used for remote commands. There are
263 mechanisms in tramp.el which automatically determine the right shell to
264 use for the remote host."
266 :type '(file :must-match t))
268 (defcustom tramp-encoding-command-switch
269 (if (string-match "cmd\\.exe" tramp-encoding-shell)
272 "*Use this switch together with `tramp-encoding-shell' for local commands.
273 See the variable `tramp-encoding-shell' for more information."
277 (defcustom tramp-encoding-reads-stdin t
278 "*If non-nil, encoding commands read from standard input.
279 If nil, the filename is the last argument.
281 Note that the commands always must write to standard output."
285 (defcustom tramp-multi-sh-program
287 "*Use this program for bootstrapping multi-hop connections.
288 This variable is similar to `tramp-encoding-shell', but it is only used
289 when initializing a multi-hop connection. Therefore, the set of
290 commands sent to this shell is quite restricted, and if you are
291 careful it works to use CMD.EXE under Windows (instead of a Bourne-ish
292 shell which does not normally exist on Windows anyway).
294 To use multi-hop methods from Windows, you also need suitable entries
295 in `tramp-multi-connection-function-alist' for the first hop.
297 This variable defaults to the value of `tramp-encoding-shell'."
299 :type '(file :must-match t))
301 ;; CCC I have changed all occurrences of comint-quote-filename with
302 ;; tramp-shell-quote-argument, except in tramp-handle-expand-many-files.
303 ;; There, comint-quote-filename was removed altogether. If it turns
304 ;; out to be necessary there, something will need to be done.
305 ;;-(defcustom tramp-file-name-quote-list
306 ;;- '(?] ?[ ?\| ?& ?< ?> ?\( ?\) ?\; ?\ ?\* ?\? ?\! ?\" ?\' ?\` ?# ?\@ ?\+ )
307 ;;- "*Protect these characters from the remote shell.
308 ;;-Any character in this list is quoted (preceded with a backslash)
309 ;;-because it means something special to the shell. This takes effect
310 ;;-when sending file and directory names to the remote shell.
312 ;;-See `comint-file-name-quote-list' for details."
314 ;;- :type '(repeat character))
316 (defcustom tramp-methods
317 '( ("rcp" (tramp-connection-function tramp-open-connection-rsh)
318 (tramp-login-program "rsh")
319 (tramp-copy-program "rcp")
320 (tramp-remote-sh "/bin/sh")
321 (tramp-login-args nil)
322 (tramp-copy-args nil)
323 (tramp-copy-keep-date-arg "-p")
324 (tramp-password-end-of-line nil))
325 ("scp" (tramp-connection-function tramp-open-connection-rsh)
326 (tramp-login-program "ssh")
327 (tramp-copy-program "scp")
328 (tramp-remote-sh "/bin/sh")
329 (tramp-login-args ("-e" "none"))
330 (tramp-copy-args nil)
331 (tramp-copy-keep-date-arg "-p")
332 (tramp-password-end-of-line nil))
333 ("scp1" (tramp-connection-function tramp-open-connection-rsh)
334 (tramp-login-program "ssh")
335 (tramp-copy-program "scp")
336 (tramp-remote-sh "/bin/sh")
337 (tramp-login-args ("-1" "-e" "none"))
338 (tramp-copy-args ("-1"))
339 (tramp-copy-keep-date-arg "-p")
340 (tramp-password-end-of-line nil))
341 ("scp2" (tramp-connection-function tramp-open-connection-rsh)
342 (tramp-login-program "ssh")
343 (tramp-copy-program "scp")
344 (tramp-remote-sh "/bin/sh")
345 (tramp-login-args ("-2" "-e" "none"))
346 (tramp-copy-args ("-2"))
347 (tramp-copy-keep-date-arg "-p")
348 (tramp-password-end-of-line nil))
350 (tramp-connection-function tramp-open-connection-rsh)
351 (tramp-login-program "ssh1")
352 (tramp-copy-program "scp1")
353 (tramp-remote-sh "/bin/sh")
354 (tramp-login-args ("-e" "none"))
355 (tramp-copy-args nil)
356 (tramp-copy-keep-date-arg "-p")
357 (tramp-password-end-of-line nil))
359 (tramp-connection-function tramp-open-connection-rsh)
360 (tramp-login-program "ssh2")
361 (tramp-copy-program "scp2")
362 (tramp-remote-sh "/bin/sh")
363 (tramp-login-args ("-e" "none"))
364 (tramp-copy-args nil)
365 (tramp-copy-keep-date-arg "-p")
366 (tramp-password-end-of-line nil))
367 ("rsync" (tramp-connection-function tramp-open-connection-rsh)
368 (tramp-login-program "ssh")
369 (tramp-copy-program "rsync")
370 (tramp-remote-sh "/bin/sh")
371 (tramp-login-args ("-e" "none"))
372 (tramp-copy-args ("-e" "ssh"))
373 (tramp-copy-keep-date-arg "-t")
374 (tramp-password-end-of-line nil))
375 ("remcp" (tramp-connection-function tramp-open-connection-rsh)
376 (tramp-login-program "remsh")
377 (tramp-copy-program "rcp")
378 (tramp-remote-sh "/bin/sh")
379 (tramp-login-args nil)
380 (tramp-copy-args nil)
381 (tramp-copy-keep-date-arg "-p")
382 (tramp-password-end-of-line nil))
383 ("rsh" (tramp-connection-function tramp-open-connection-rsh)
384 (tramp-login-program "rsh")
385 (tramp-copy-program nil)
386 (tramp-remote-sh "/bin/sh")
387 (tramp-login-args nil)
388 (tramp-copy-args nil)
389 (tramp-copy-keep-date-arg nil)
390 (tramp-password-end-of-line nil))
391 ("ssh" (tramp-connection-function tramp-open-connection-rsh)
392 (tramp-login-program "ssh")
393 (tramp-copy-program nil)
394 (tramp-remote-sh "/bin/sh")
395 (tramp-login-args ("-e" "none"))
396 (tramp-copy-args nil)
397 (tramp-copy-keep-date-arg nil)
398 (tramp-password-end-of-line nil))
399 ("ssh1" (tramp-connection-function tramp-open-connection-rsh)
400 (tramp-login-program "ssh")
401 (tramp-copy-program nil)
402 (tramp-remote-sh "/bin/sh")
403 (tramp-login-args ("-1" "-e" "none"))
404 (tramp-copy-args ("-1"))
405 (tramp-copy-keep-date-arg nil)
406 (tramp-password-end-of-line nil))
407 ("ssh2" (tramp-connection-function tramp-open-connection-rsh)
408 (tramp-login-program "ssh")
409 (tramp-copy-program nil)
410 (tramp-remote-sh "/bin/sh")
411 (tramp-login-args ("-2" "-e" "none"))
412 (tramp-copy-args ("-2"))
413 (tramp-copy-keep-date-arg nil)
414 (tramp-password-end-of-line nil))
416 (tramp-connection-function tramp-open-connection-rsh)
417 (tramp-login-program "ssh1")
418 (tramp-copy-program nil)
419 (tramp-remote-sh "/bin/sh")
420 (tramp-login-args ("-e" "none"))
421 (tramp-copy-args nil)
422 (tramp-copy-keep-date-arg nil)
423 (tramp-password-end-of-line nil))
425 (tramp-connection-function tramp-open-connection-rsh)
426 (tramp-login-program "ssh2")
427 (tramp-copy-program nil)
428 (tramp-remote-sh "/bin/sh")
429 (tramp-login-args ("-e" "none"))
430 (tramp-copy-args nil)
431 (tramp-copy-keep-date-arg nil)
432 (tramp-password-end-of-line nil))
433 ("remsh" (tramp-connection-function tramp-open-connection-rsh)
434 (tramp-login-program "remsh")
435 (tramp-copy-program nil)
436 (tramp-remote-sh "/bin/sh")
437 (tramp-login-args nil)
438 (tramp-copy-args nil)
439 (tramp-copy-keep-date-arg nil)
440 (tramp-password-end-of-line nil))
442 (tramp-connection-function tramp-open-connection-telnet)
443 (tramp-login-program "telnet")
444 (tramp-copy-program nil)
445 (tramp-remote-sh "/bin/sh")
446 (tramp-login-args nil)
447 (tramp-copy-args nil)
448 (tramp-copy-keep-date-arg nil)
449 (tramp-password-end-of-line nil))
450 ("su" (tramp-connection-function tramp-open-connection-su)
451 (tramp-login-program "su")
452 (tramp-copy-program nil)
453 (tramp-remote-sh "/bin/sh")
454 (tramp-login-args ("-" "%u"))
455 (tramp-copy-args nil)
456 (tramp-copy-keep-date-arg nil)
457 (tramp-password-end-of-line nil))
458 ("sudo" (tramp-connection-function tramp-open-connection-su)
459 (tramp-login-program "sudo")
460 (tramp-copy-program nil)
461 (tramp-remote-sh "/bin/sh")
462 (tramp-login-args ("-u" "%u" "-s"
464 (tramp-copy-args nil)
465 (tramp-copy-keep-date-arg nil)
466 (tramp-password-end-of-line nil))
467 ("multi" (tramp-connection-function tramp-open-connection-multi)
468 (tramp-login-program nil)
469 (tramp-copy-program nil)
470 (tramp-remote-sh "/bin/sh")
471 (tramp-login-args nil)
472 (tramp-copy-args nil)
473 (tramp-copy-keep-date-arg nil)
474 (tramp-password-end-of-line nil))
475 ("scpc" (tramp-connection-function tramp-open-connection-rsh)
476 (tramp-login-program "ssh")
477 (tramp-copy-program "scp")
478 (tramp-remote-sh "/bin/sh")
479 (tramp-login-args ("-o" "ControlPath=%t.%%r@%%h:%%p"
480 "-o" "ControlMaster=yes"
482 (tramp-copy-args ("-o" "ControlPath=%t.%%r@%%h:%%p"
483 "-o" "ControlMaster=auto"))
484 (tramp-copy-keep-date-arg "-p")
485 (tramp-password-end-of-line nil))
486 ("scpx" (tramp-connection-function tramp-open-connection-rsh)
487 (tramp-login-program "ssh")
488 (tramp-copy-program "scp")
489 (tramp-remote-sh "/bin/sh")
490 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
491 (tramp-copy-args nil)
492 (tramp-copy-keep-date-arg "-p")
493 (tramp-password-end-of-line nil))
494 ("sshx" (tramp-connection-function tramp-open-connection-rsh)
495 (tramp-login-program "ssh")
496 (tramp-copy-program nil)
497 (tramp-remote-sh "/bin/sh")
498 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
499 (tramp-copy-args nil)
500 (tramp-copy-keep-date-arg nil)
501 (tramp-password-end-of-line nil))
503 (tramp-connection-function tramp-open-connection-rsh)
504 (tramp-login-program "krlogin")
505 (tramp-copy-program nil)
506 (tramp-remote-sh "/bin/sh")
507 (tramp-login-args ("-x"))
508 (tramp-copy-args nil)
509 (tramp-copy-keep-date-arg nil)
510 (tramp-password-end-of-line nil))
512 (tramp-connection-function tramp-open-connection-rsh)
513 (tramp-login-program "plink")
514 (tramp-copy-program nil)
515 (tramp-remote-sh "/bin/sh")
516 (tramp-login-args ("-ssh")) ;optionally add "-v"
517 (tramp-copy-args nil)
518 (tramp-copy-keep-date-arg nil)
519 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
521 (tramp-connection-function tramp-open-connection-rsh)
522 (tramp-login-program "plink")
523 (tramp-copy-program nil)
524 (tramp-remote-sh "/bin/sh")
525 (tramp-login-args ("-1" "-ssh")) ;optionally add "-v"
526 (tramp-copy-args nil)
527 (tramp-copy-keep-date-arg nil)
528 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
530 (tramp-connection-function tramp-open-connection-rsh)
531 (tramp-login-program "plink")
532 (tramp-copy-program "pscp")
533 (tramp-remote-sh "/bin/sh")
534 (tramp-login-args ("-ssh"))
535 (tramp-copy-args nil)
536 (tramp-copy-keep-date-arg "-p")
537 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
539 (tramp-connection-function tramp-open-connection-rsh)
540 (tramp-login-program "fsh")
541 (tramp-copy-program "fcp")
542 (tramp-remote-sh "/bin/sh -i")
543 (tramp-login-args ("sh" "-i"))
544 (tramp-copy-args nil)
545 (tramp-copy-keep-date-arg "-p")
546 (tramp-password-end-of-line nil))
548 "*Alist of methods for remote files.
549 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
550 Each NAME stands for a remote access method. Each PARAM is a
551 pair of the form (KEY VALUE). The following KEYs are defined:
552 * `tramp-connection-function'
553 This specifies the function to use to connect to the remote host.
554 Currently, `tramp-open-connection-rsh', `tramp-open-connection-telnet'
555 and `tramp-open-connection-su' are defined. See the documentation
556 of these functions for more details.
558 This specifies the Bourne shell to use on the remote host. This
559 MUST be a Bourne-like shell. It is normally not necessary to set
560 this to any value other than \"/bin/sh\": tramp wants to use a shell
561 which groks tilde expansion, but it can search for it. Also note
562 that \"/bin/sh\" exists on all Unixen, this might not be true for
563 the value that you decide to use. You Have Been Warned.
564 * `tramp-login-program'
565 This specifies the name of the program to use for logging in to the
566 remote host. Depending on `tramp-connection-function', this may be
567 the name of rsh or a workalike program (when
568 `tramp-connection-function' is `tramp-open-connection-rsh'), or the
569 name of telnet or a workalike (for `tramp-open-connection-telnet'),
570 or the name of su or a workalike (for `tramp-open-connection-su').
572 This specifies the list of arguments to pass to the above
573 mentioned program. Please note that this is a list of arguments,
574 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
575 here. Instead, you want two list elements, one for \"-a\" and one
576 for \"-b\", or one for \"-f\" and one for \"foo\".
577 If `tramp-connection-function' is `tramp-open-connection-su', then
578 \"%u\" in this list is replaced by the user name, and \"%%\" can
579 be used to obtain a literal percent character.
580 \"%t\" is replaced by the temporary file name for `scp'-like methods.
581 * `tramp-copy-program'
582 This specifies the name of the program to use for remotely copying
583 the file; this might be the absolute filename of rcp or the name of
586 This specifies the list of parameters to pass to the above mentioned
587 program, the hints for `tramp-login-args' also apply here.
588 * `tramp-copy-keep-date-arg'
589 This specifies the parameter to use for the copying program when the
590 timestamp of the original file should be kept. For `rcp', use `-p', for
592 * `tramp-password-end-of-line'
593 This specifies the string to use for terminating the line after
594 submitting the password. If this method parameter is nil, then the
595 value of the normal variable `tramp-default-password-end-of-line'
596 is used. This parameter is necessary because the \"plink\" program
597 requires any two characters after sending the password. These do
598 not have to be newline or carriage return characters. Other login
599 programs are happy with just one character, the newline character.
600 We use \"xy\" as the value for methods using \"plink\".
602 What does all this mean? Well, you should specify `tramp-login-program'
603 for all methods; this program is used to log in to the remote site. Then,
604 there are two ways to actually transfer the files between the local and the
605 remote side. One way is using an additional rcp-like program. If you want
606 to do this, set `tramp-copy-program' in the method.
608 Another possibility for file transfer is inline transfer, i.e. the
609 file is passed through the same buffer used by `tramp-login-program'. In
610 this case, the file contents need to be protected since the
611 `tramp-login-program' might use escape codes or the connection might not
612 be eight-bit clean. Therefore, file contents are encoded for transit.
613 See the variable `tramp-coding-commands' for details.
615 So, to summarize: if the method is an out-of-band method, then you
616 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
617 inline method, then these two parameters should be nil. Every method,
618 inline or out of band, must specify `tramp-connection-function' plus
619 the associated arguments (for example, the login program if you chose
620 `tramp-open-connection-telnet').
624 When using `tramp-open-connection-su' the phrase `open connection to a
625 remote host' sounds strange, but it is used nevertheless, for
626 consistency. No connection is opened to a remote host, but `su' is
627 started on the local host. You are not allowed to specify a remote
628 host other than `localhost' or the name of the local host."
632 (set (list (const tramp-connection-function) function)
633 (list (const tramp-login-program)
634 (choice (const nil) string))
635 (list (const tramp-copy-program)
636 (choice (const nil) string))
637 (list (const tramp-remote-sh)
638 (choice (const nil) string))
639 (list (const tramp-login-args) (repeat string))
640 (list (const tramp-copy-args) (repeat string))
641 (list (const tramp-copy-keep-date-arg)
642 (choice (const nil) string))
643 (list (const tramp-encoding-command)
644 (choice (const nil) string))
645 (list (const tramp-decoding-command)
646 (choice (const nil) string))
647 (list (const tramp-encoding-function)
648 (choice (const nil) function))
649 (list (const tramp-decoding-function)
650 (choice (const nil) function))
651 (list (const tramp-password-end-of-line)
652 (choice (const nil) string))))))
654 (defcustom tramp-multi-methods '("multi" "multiu")
655 "*List of multi-hop methods.
656 Each entry in this list should be a method name as mentioned in the
657 variable `tramp-methods'."
659 :type '(repeat string))
661 (defcustom tramp-multi-connection-function-alist
662 '(("telnet" tramp-multi-connect-telnet "telnet %h%n")
663 ("rsh" tramp-multi-connect-rlogin "rsh %h -l %u%n")
664 ("remsh" tramp-multi-connect-rlogin "remsh %h -l %u%n")
665 ("ssh" tramp-multi-connect-rlogin "ssh %h -l %u%n")
666 ("ssht" tramp-multi-connect-rlogin "ssh %h -e none -t -t -l %u%n")
667 ("su" tramp-multi-connect-su "su - %u%n")
668 ("sudo" tramp-multi-connect-su "sudo -u %u -s -p Password:%n"))
669 "*List of connection functions for multi-hop methods.
670 Each list item is a list of three items (METHOD FUNCTION COMMAND),
671 where METHOD is the name as used in the file name, FUNCTION is the
672 function to be executed, and COMMAND is the shell command used for
675 COMMAND may contain percent escapes. `%u' will be replaced with the
676 user name, `%h' will be replaced with the host name, and `%n' will be
677 replaced with an end-of-line character, as specified in the variable
678 `tramp-rsh-end-of-line'. Use `%%' for a literal percent character.
679 Note that the interpretation of the percent escapes also depends on
680 the FUNCTION. For example, the `%u' escape is forbidden with the
681 function `tramp-multi-connect-telnet'. See the documentation of the
682 various functions for details."
684 :type '(repeat (list string function string)))
686 (defcustom tramp-default-method
687 ;; An external copy method seems to be preferred, because it is much
688 ;; more performant for large files, and it hasn't too serious delays
689 ;; for small files. But it must be ensured that there aren't
690 ;; permanent password queries. Either a password agent like
691 ;; "ssh-agent" or "Pageant" shall run, or the optional password.el
692 ;; package shall be active for password caching. "scpc" would be
693 ;; another good choice because of the "ControlMaster" option, but
694 ;; this is a more modern alternative in OpenSSH 4, which cannot be
696 (let ((e-f (fboundp 'executable-find)))
698 ;; PuTTY is installed.
699 ((and e-f (funcall 'executable-find "pscp"))
700 (if (or (fboundp 'password-read)
701 ;; Pageant is running.
702 (and (fboundp 'w32-window-exists-p)
703 (funcall 'w32-window-exists-p "Pageant" "Pageant")))
706 ;; There is an ssh installation.
707 ((and e-f (funcall 'executable-find "scp"))
708 (if (or (fboundp 'password-read)
709 ;; ssh-agent is running.
710 (getenv "SSH_AUTH_SOCK")
711 (getenv "SSH_AGENT_PID"))
714 ;; Under Emacs 20, `executable-find' does not exists. So we
715 ;; couldn't check whether there is an ssh implementation. Let's
720 "*Default method to use for transferring files.
721 See `tramp-methods' for possibilities.
722 Also see `tramp-default-method-alist'."
726 (defcustom tramp-default-method-alist
727 '(("\\`localhost\\'" "\\`root\\'" "su"))
728 "*Default method to use for specific user/host pairs.
729 This is an alist of items (HOST USER METHOD). The first matching item
730 specifies the method to use for a file name which does not specify a
731 method. HOST and USER are regular expressions or nil, which is
732 interpreted as a regular expression which always matches. If no entry
733 matches, the variable `tramp-default-method' takes effect.
735 If the file name does not specify the user, lookup is done using the
736 empty string for the user name.
738 See `tramp-methods' for a list of possibilities for METHOD."
740 :type '(repeat (list (regexp :tag "Host regexp")
741 (regexp :tag "User regexp")
742 (string :tag "Method"))))
744 ;; Default values for non-Unices seeked
745 (defconst tramp-completion-function-alist-rsh
746 (unless (memq system-type '(windows-nt))
747 '((tramp-parse-rhosts "/etc/hosts.equiv")
748 (tramp-parse-rhosts "~/.rhosts")))
749 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
751 ;; Default values for non-Unices seeked
752 (defconst tramp-completion-function-alist-ssh
753 (unless (memq system-type '(windows-nt))
754 '((tramp-parse-rhosts "/etc/hosts.equiv")
755 (tramp-parse-rhosts "/etc/shosts.equiv")
756 (tramp-parse-shosts "/etc/ssh_known_hosts")
757 (tramp-parse-sconfig "/etc/ssh_config")
758 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
759 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
760 (tramp-parse-rhosts "~/.rhosts")
761 (tramp-parse-rhosts "~/.shosts")
762 (tramp-parse-shosts "~/.ssh/known_hosts")
763 (tramp-parse-sconfig "~/.ssh/config")
764 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
765 (tramp-parse-sknownhosts "~/.ssh2/knownhosts")))
766 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
768 ;; Default values for non-Unices seeked
769 (defconst tramp-completion-function-alist-telnet
770 (unless (memq system-type '(windows-nt))
771 '((tramp-parse-hosts "/etc/hosts")))
772 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
774 ;; Default values for non-Unices seeked
775 (defconst tramp-completion-function-alist-su
776 (unless (memq system-type '(windows-nt))
777 '((tramp-parse-passwd "/etc/passwd")))
778 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
780 (defvar tramp-completion-function-alist nil
781 "*Alist of methods for remote files.
782 This is a list of entries of the form (NAME PAIR1 PAIR2 ...).
783 Each NAME stands for a remote access method. Each PAIR is of the form
784 \(FUNCTION FILE). FUNCTION is responsible to extract user names and host
785 names from FILE for completion. The following predefined FUNCTIONs exists:
787 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
788 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
789 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
790 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
791 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
792 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
793 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
794 * `tramp-parse-netrc' for \"~/.netrc\" like files.
796 FUNCTION can also be a customer defined function. For more details see
799 (eval-after-load "tramp"
801 (tramp-set-completion-function
802 "rcp" tramp-completion-function-alist-rsh)
803 (tramp-set-completion-function
804 "scp" tramp-completion-function-alist-ssh)
805 (tramp-set-completion-function
806 "scp1" tramp-completion-function-alist-ssh)
807 (tramp-set-completion-function
808 "scp2" tramp-completion-function-alist-ssh)
809 (tramp-set-completion-function
810 "scp1_old" tramp-completion-function-alist-ssh)
811 (tramp-set-completion-function
812 "scp2_old" tramp-completion-function-alist-ssh)
813 (tramp-set-completion-function
814 "rsync" tramp-completion-function-alist-rsh)
815 (tramp-set-completion-function
816 "remcp" tramp-completion-function-alist-rsh)
817 (tramp-set-completion-function
818 "rsh" tramp-completion-function-alist-rsh)
819 (tramp-set-completion-function
820 "ssh" tramp-completion-function-alist-ssh)
821 (tramp-set-completion-function
822 "ssh1" tramp-completion-function-alist-ssh)
823 (tramp-set-completion-function
824 "ssh2" tramp-completion-function-alist-ssh)
825 (tramp-set-completion-function
826 "ssh1_old" tramp-completion-function-alist-ssh)
827 (tramp-set-completion-function
828 "ssh2_old" tramp-completion-function-alist-ssh)
829 (tramp-set-completion-function
830 "remsh" tramp-completion-function-alist-rsh)
831 (tramp-set-completion-function
832 "telnet" tramp-completion-function-alist-telnet)
833 (tramp-set-completion-function
834 "su" tramp-completion-function-alist-su)
835 (tramp-set-completion-function
836 "sudo" tramp-completion-function-alist-su)
837 (tramp-set-completion-function
839 (tramp-set-completion-function
840 "scpx" tramp-completion-function-alist-ssh)
841 (tramp-set-completion-function
842 "sshx" tramp-completion-function-alist-ssh)
843 (tramp-set-completion-function
844 "krlogin" tramp-completion-function-alist-rsh)
845 (tramp-set-completion-function
846 "plink" tramp-completion-function-alist-ssh)
847 (tramp-set-completion-function
848 "plink1" tramp-completion-function-alist-ssh)
849 (tramp-set-completion-function
850 "pscp" tramp-completion-function-alist-ssh)
851 (tramp-set-completion-function
852 "fcp" tramp-completion-function-alist-ssh)))
854 (defcustom tramp-rsh-end-of-line "\n"
855 "*String used for end of line in rsh connections.
856 I don't think this ever needs to be changed, so please tell me about it
857 if you need to change this.
858 Also see the method parameter `tramp-password-end-of-line' and the normal
859 variable `tramp-default-password-end-of-line'."
863 (defcustom tramp-default-password-end-of-line
864 tramp-rsh-end-of-line
865 "*String used for end of line after sending a password.
866 This variable provides the default value for the method parameter
867 `tramp-password-end-of-line', see `tramp-methods' for more details.
869 It seems that people using plink under Windows need to send
870 \"\\r\\n\" (carriage-return, then newline) after a password, but just
871 \"\\n\" after all other lines. This variable can be used for the
872 password, see `tramp-rsh-end-of-line' for the other cases.
874 The default value is to use the same value as `tramp-rsh-end-of-line'."
878 (defcustom tramp-remote-path
879 ;; "/usr/xpg4/bin" has been placed first, because on Solaris a POSIX
880 ;; compatible "id" is needed.
881 '("/usr/xpg4/bin" "/bin" "/usr/bin" "/usr/sbin" "/usr/local/bin"
882 "/usr/ccs/bin" "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
883 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
884 "*List of directories to search for executables on remote host.
885 Please notify me about other semi-standard directories to include here.
887 You can use `~' in this list, but when searching for a shell which groks
888 tilde expansion, all directory names starting with `~' will be ignored."
890 :type '(repeat string))
892 (defcustom tramp-login-prompt-regexp
893 ".*ogin\\( .*\\)?: *"
894 "*Regexp matching login-like prompts.
895 The regexp should match at end of buffer.
897 Sometimes the prompt is reported to look like \"login as:\"."
901 (defcustom tramp-shell-prompt-pattern
902 "^[^#$%>\n]*[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*"
903 "Regexp to match prompts from remote shell.
904 Normally, Tramp expects you to configure `shell-prompt-pattern'
905 correctly, but sometimes it happens that you are connecting to a
906 remote host which sends a different kind of shell prompt. Therefore,
907 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
908 and also things matched by this variable. The default value of this
909 variable is similar to the default value of `shell-prompt-pattern',
910 which should work well in many cases."
914 (defcustom tramp-password-prompt-regexp
915 "^.*\\([pP]assword\\|passphrase\\).*:\^@? *"
916 "*Regexp matching password-like prompts.
917 The regexp should match at end of buffer.
919 The `sudo' program appears to insert a `^@' character into the prompt."
923 (defcustom tramp-wrong-passwd-regexp
925 ;; These strings should be on the last line
926 (regexp-opt '("Permission denied."
932 "Name or service not known"
933 "Host key verification failed.") t)
937 ;; Here comes a list of regexes, separated by \\|
938 "Received signal [0-9]+"
940 "*Regexp matching a `login failed' message.
941 The regexp should match at end of buffer."
945 (defcustom tramp-yesno-prompt-regexp
947 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
949 "Regular expression matching all yes/no queries which need to be confirmed.
950 The confirmation should be done with yes or no.
951 The regexp should match at end of buffer.
952 See also `tramp-yn-prompt-regexp'."
956 (defcustom tramp-yn-prompt-regexp
958 (regexp-opt '("Store key in cache? (y/n)"
959 "Update cached key? (y/n, Return cancels connection)") t)
961 "Regular expression matching all y/n queries which need to be confirmed.
962 The confirmation should be done with y or n.
963 The regexp should match at end of buffer.
964 See also `tramp-yesno-prompt-regexp'."
968 (defcustom tramp-terminal-prompt-regexp
972 "Terminal type\\? \\[.*\\]"
974 "Regular expression matching all terminal setting prompts.
975 The regexp should match at end of buffer.
976 The answer will be provided by `tramp-action-terminal', which see."
980 (defcustom tramp-operation-not-permitted-regexp
981 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
982 (regexp-opt '("Operation not permitted") t))
983 "Regular expression matching keep-date problems in (s)cp operations.
984 Copying has been performed successfully already, so this message can
989 (defcustom tramp-copy-failed-regexp
991 (regexp-opt '("Permission denied"
994 "No such file or directory") t)
996 "Regular expression matching copy problems in (s)cp operations."
1000 (defcustom tramp-process-alive-regexp
1002 "Regular expression indicating a process has finished.
1003 In fact this expression is empty by intention, it will be used only to
1004 check regularly the status of the associated process.
1005 The answer will be provided by `tramp-action-process-alive',
1006 `tramp-multi-action-process-alive' and`tramp-action-out-of-band', which see."
1010 (defcustom tramp-temp-name-prefix "tramp."
1011 "*Prefix to use for temporary files.
1012 If this is a relative file name (such as \"tramp.\"), it is considered
1013 relative to the directory name returned by the function
1014 `tramp-temporary-file-directory' (which see). It may also be an
1015 absolute file name; don't forget to include a prefix for the filename
1020 (defcustom tramp-discard-garbage nil
1021 "*If non-nil, try to discard garbage sent by remote shell.
1022 Some shells send such garbage upon connection setup."
1026 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
1027 "*Alist specifying extra arguments to pass to the remote shell.
1028 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
1029 matching the shell file name and ARGS is a string specifying the
1032 This variable is only used when Tramp needs to start up another shell
1033 for tilde expansion. The extra arguments should typically prevent the
1034 shell from reading its init file."
1036 ;; This might be the wrong way to test whether the widget type
1037 ;; `alist' is available. Who knows the right way to test it?
1038 :type (if (get 'alist 'widget-type)
1039 '(alist :key-type string :value-type string)
1040 '(repeat (cons string string))))
1042 (defcustom tramp-prefix-format
1043 (if tramp-unified-filenames "/" "/[")
1044 "*String matching the very beginning of tramp file names.
1045 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1049 (defcustom tramp-prefix-regexp
1050 (concat "^" (regexp-quote tramp-prefix-format))
1051 "*Regexp matching the very beginning of tramp file names.
1052 Should always start with \"^\". Derived from `tramp-prefix-format'."
1056 (defcustom tramp-method-regexp
1058 "*Regexp matching methods identifiers."
1062 ;; It is a little bit annoying that in XEmacs case this delimeter is different
1063 ;; for single-hop and multi-hop cases.
1064 (defcustom tramp-postfix-single-method-format
1065 (if tramp-unified-filenames ":" "/")
1066 "*String matching delimeter between method and user or host names.
1067 Applicable for single-hop methods.
1068 Used in `tramp-make-tramp-file-name'."
1072 (defcustom tramp-postfix-single-method-regexp
1073 (regexp-quote tramp-postfix-single-method-format)
1074 "*Regexp matching delimeter between method and user or host names.
1075 Applicable for single-hop methods.
1076 Derived from `tramp-postfix-single-method-format'."
1080 (defcustom tramp-postfix-multi-method-format
1082 "*String matching delimeter between method and user or host names.
1083 Applicable for multi-hop methods.
1084 Used in `tramp-make-tramp-multi-file-name'."
1088 (defcustom tramp-postfix-multi-method-regexp
1089 (regexp-quote tramp-postfix-multi-method-format)
1090 "*Regexp matching delimeter between method and user or host names.
1091 Applicable for multi-hop methods.
1092 Derived from `tramp-postfix-multi-method-format'."
1096 (defcustom tramp-postfix-multi-hop-format
1097 (if tramp-unified-filenames ":" "/")
1098 "*String matching delimeter between host and next method.
1099 Applicable for multi-hop methods.
1100 Used in `tramp-make-tramp-multi-file-name'."
1104 (defcustom tramp-postfix-multi-hop-regexp
1105 (regexp-quote tramp-postfix-multi-hop-format)
1106 "*Regexp matching delimeter between host and next method.
1107 Applicable for multi-hop methods.
1108 Derived from `tramp-postfix-multi-hop-format'."
1112 (defcustom tramp-user-regexp
1114 "*Regexp matching user names."
1118 (defcustom tramp-postfix-user-format
1120 "*String matching delimeter between user and host names.
1121 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1125 (defcustom tramp-postfix-user-regexp
1126 (regexp-quote tramp-postfix-user-format)
1127 "*Regexp matching delimeter between user and host names.
1128 Derived from `tramp-postfix-user-format'."
1132 (defcustom tramp-host-regexp
1134 "*Regexp matching host names."
1138 (defcustom tramp-host-with-port-regexp
1140 "*Regexp matching host names."
1144 (defcustom tramp-postfix-host-format
1145 (if tramp-unified-filenames ":" "]")
1146 "*String matching delimeter between host names and localnames.
1147 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1151 (defcustom tramp-postfix-host-regexp
1152 (regexp-quote tramp-postfix-host-format)
1153 "*Regexp matching delimeter between host names and localnames.
1154 Derived from `tramp-postfix-host-format'."
1158 (defcustom tramp-localname-regexp
1160 "*Regexp matching localnames."
1164 ;; File name format.
1166 (defcustom tramp-file-name-structure
1170 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "\\)?"
1171 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
1172 "\\(" tramp-host-with-port-regexp "\\)" tramp-postfix-host-regexp
1173 "\\(" tramp-localname-regexp "\\)")
1176 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1177 the tramp file name structure.
1179 The first element REGEXP is a regular expression matching a tramp file
1180 name. The regex should contain parentheses around the method name,
1181 the user name, the host name, and the file name parts.
1183 The second element METHOD is a number, saying which pair of
1184 parentheses matches the method name. The third element USER is
1185 similar, but for the user name. The fourth element HOST is similar,
1186 but for the host name. The fifth element FILE is for the file name.
1187 These numbers are passed directly to `match-string', which see. That
1188 means the opening parentheses are counted to identify the pair.
1190 See also `tramp-file-name-regexp'."
1192 :type '(list (regexp :tag "File name regexp")
1193 (integer :tag "Paren pair for method name")
1194 (integer :tag "Paren pair for user name ")
1195 (integer :tag "Paren pair for host name ")
1196 (integer :tag "Paren pair for file name ")))
1199 (defconst tramp-file-name-regexp-unified
1201 "Value for `tramp-file-name-regexp' for unified remoting.
1202 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1203 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1206 (defconst tramp-file-name-regexp-separate
1208 "Value for `tramp-file-name-regexp' for separate remoting.
1209 XEmacs uses a separate filename syntax for Tramp and EFS.
1210 See `tramp-file-name-structure-separate' for more explanations.")
1213 (defcustom tramp-file-name-regexp
1214 (if tramp-unified-filenames
1215 tramp-file-name-regexp-unified
1216 tramp-file-name-regexp-separate)
1217 "*Regular expression matching file names handled by tramp.
1218 This regexp should match tramp file names but no other file names.
1219 \(When tramp.el is loaded, this regular expression is prepended to
1220 `file-name-handler-alist', and that is searched sequentially. Thus,
1221 if the tramp entry appears rather early in the `file-name-handler-alist'
1222 and is a bit too general, then some files might be considered tramp
1223 files which are not really tramp files.
1225 Please note that the entry in `file-name-handler-alist' is made when
1226 this file (tramp.el) is loaded. This means that this variable must be set
1227 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1228 updated after changing this variable.
1230 Also see `tramp-file-name-structure'."
1235 (defconst tramp-completion-file-name-regexp-unified
1236 "^/$\\|^/[^/:][^/]*$"
1237 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1238 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1239 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1242 (defconst tramp-completion-file-name-regexp-separate
1243 "^/\\([[][^]]*\\)?$"
1244 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1245 XEmacs uses a separate filename syntax for Tramp and EFS.
1246 See `tramp-file-name-structure-separate' for more explanations.")
1249 (defcustom tramp-completion-file-name-regexp
1250 (if tramp-unified-filenames
1251 tramp-completion-file-name-regexp-unified
1252 tramp-completion-file-name-regexp-separate)
1253 "*Regular expression matching file names handled by tramp completion.
1254 This regexp should match partial tramp file names only.
1256 Please note that the entry in `file-name-handler-alist' is made when
1257 this file (tramp.el) is loaded. This means that this variable must be set
1258 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1259 updated after changing this variable.
1261 Also see `tramp-file-name-structure'."
1265 (defcustom tramp-multi-file-name-structure
1269 "\\(" "\\(" tramp-method-regexp "\\)" "\\)?"
1270 "\\(" "\\(" tramp-postfix-multi-hop-regexp "%s" "\\)+" "\\)?"
1271 tramp-postfix-host-regexp "\\(" tramp-localname-regexp "\\)")
1273 "*Describes the file name structure of `multi' files.
1274 Multi files allow you to contact a remote host in several hops.
1275 This is a list of four elements (REGEXP METHOD HOP LOCALNAME).
1277 The first element, REGEXP, gives a regular expression to match against
1278 the file name. In this regular expression, `%s' is replaced with the
1279 value of `tramp-multi-file-name-hop-structure'. (Note: in order to
1280 allow multiple hops, you normally want to use something like
1281 \"\\\\(\\\\(%s\\\\)+\\\\)\" in the regular expression. The outer pair
1282 of parentheses is used for the HOP element, see below.)
1284 All remaining elements are numbers. METHOD gives the number of the
1285 paren pair which matches the method name. HOP gives the number of the
1286 paren pair which matches the hop sequence. LOCALNAME gives the number of
1287 the paren pair which matches the localname (pathname) on the remote host.
1289 LOCALNAME can also be negative, which means to count from the end. Ie, a
1290 value of -1 means the last paren pair.
1292 I think it would be good if the regexp matches the whole of the
1293 string, but I haven't actually tried what happens if it doesn't..."
1295 :type '(list (regexp :tag "File name regexp")
1296 (integer :tag "Paren pair for method name")
1297 (integer :tag "Paren pair for hops")
1298 (integer :tag "Paren pair to match localname")))
1300 (defcustom tramp-multi-file-name-hop-structure
1303 "\\(" tramp-method-regexp "\\)" tramp-postfix-multi-method-regexp
1304 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
1305 "\\(" tramp-host-with-port-regexp "\\)")
1307 "*Describes the structure of a hop in multi files.
1308 This is a list of four elements (REGEXP METHOD USER HOST). First
1309 element REGEXP is used to match against the hop. Pair number METHOD
1310 matches the method of one hop, pair number USER matches the user of
1311 one hop, pair number HOST matches the host of one hop.
1313 This regular expression should match exactly all of one hop."
1315 :type '(list (regexp :tag "Hop regexp")
1316 (integer :tag "Paren pair for method name")
1317 (integer :tag "Paren pair for user name")
1318 (integer :tag "Paren pair for host name")))
1320 (defcustom tramp-make-multi-tramp-file-format
1322 (concat tramp-prefix-format "%m")
1323 (concat tramp-postfix-multi-hop-format
1324 "%m" tramp-postfix-multi-method-format
1325 "%u" tramp-postfix-user-format
1327 (concat tramp-postfix-host-format "%p"))
1328 "*Describes how to construct a `multi' file name.
1329 This is a list of three elements PREFIX, HOP and LOCALNAME.
1331 The first element PREFIX says how to construct the prefix, the second
1332 element HOP specifies what each hop looks like, and the final element
1333 LOCALNAME says how to construct the localname (pathname).
1335 In PREFIX, `%%' means `%' and `%m' means the method name.
1337 In HOP, `%%' means `%' and `%m', `%u', `%h' mean the hop method, hop
1338 user and hop host, respectively.
1340 In LOCALNAME, `%%' means `%' and `%p' means the localname.
1342 The resulting file name always contains one copy of PREFIX and one
1343 copy of LOCALNAME, but there is one copy of HOP for each hop in the file
1346 Note: the current implementation requires the prefix to contain the
1347 method name, followed by all the hops, and the localname must come
1350 :type '(list string string string))
1352 (defcustom tramp-terminal-type "dumb"
1353 "*Value of TERM environment variable for logging in to remote host.
1354 Because Tramp wants to parse the output of the remote shell, it is easily
1355 confused by ANSI color escape sequences and suchlike. Often, shell init
1356 files conditionalize this setup based on the TERM environment variable."
1360 (defcustom tramp-completion-without-shell-p nil
1361 "*If nil, use shell wildcards for completion, else rely on Lisp only.
1362 Using shell wildcards for completions has the advantage that it can be
1363 fast even in large directories, but completion is always
1364 case-sensitive. Relying on Lisp only means that case-insensitive
1365 completion is possible (subject to the variable `completion-ignore-case'),
1366 but it might be slow on large directories."
1370 (defcustom tramp-actions-before-shell
1371 '((tramp-password-prompt-regexp tramp-action-password)
1372 (tramp-login-prompt-regexp tramp-action-login)
1373 (shell-prompt-pattern tramp-action-succeed)
1374 (tramp-shell-prompt-pattern tramp-action-succeed)
1375 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1376 (tramp-yesno-prompt-regexp tramp-action-yesno)
1377 (tramp-yn-prompt-regexp tramp-action-yn)
1378 (tramp-terminal-prompt-regexp tramp-action-terminal)
1379 (tramp-process-alive-regexp tramp-action-process-alive))
1380 "List of pattern/action pairs.
1381 Whenever a pattern matches, the corresponding action is performed.
1382 Each item looks like (PATTERN ACTION).
1384 The PATTERN should be a symbol, a variable. The value of this
1385 variable gives the regular expression to search for. Note that the
1386 regexp must match at the end of the buffer, \"\\'\" is implicitly
1389 The ACTION should also be a symbol, but a function. When the
1390 corresponding PATTERN matches, the ACTION function is called."
1392 :type '(repeat (list variable function)))
1394 (defcustom tramp-actions-copy-out-of-band
1395 '((tramp-password-prompt-regexp tramp-action-password)
1396 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1397 (tramp-copy-failed-regexp tramp-action-copy-failed)
1398 (tramp-process-alive-regexp tramp-action-out-of-band))
1399 "List of pattern/action pairs.
1400 This list is used for copying/renaming with out-of-band methods.
1401 See `tramp-actions-before-shell' for more info."
1403 :type '(repeat (list variable function)))
1405 (defcustom tramp-multi-actions
1406 '((tramp-password-prompt-regexp tramp-multi-action-password)
1407 (tramp-login-prompt-regexp tramp-multi-action-login)
1408 (shell-prompt-pattern tramp-multi-action-succeed)
1409 (tramp-shell-prompt-pattern tramp-multi-action-succeed)
1410 (tramp-wrong-passwd-regexp tramp-multi-action-permission-denied)
1411 (tramp-process-alive-regexp tramp-multi-action-process-alive))
1412 "List of pattern/action pairs.
1413 This list is used for each hop in multi-hop connections.
1414 See `tramp-actions-before-shell' for more info."
1416 :type '(repeat (list variable function)))
1418 (defcustom tramp-initial-commands
1421 "unset autocorrect")
1422 "List of commands to send to the first remote shell that we see.
1423 These commands will be sent to any shell, and thus they should be
1424 designed to work in such circumstances. Also, restrict the commands
1425 to the bare necessity for getting the remote shell into a state
1426 where it is possible to execute the Bourne-ish shell.
1428 At the moment, the command to execute the Bourne-ish shell uses strange
1429 quoting which `tcsh' tries to correct, so we send the command \"unset
1430 autocorrect\" to the remote host."
1432 :type '(repeat string))
1434 ;; Chunked sending kluge. We set this to 500 for black-listed constellations
1435 ;; known to have a bug in `process-send-string'; some ssh connections appear
1436 ;; to drop bytes when data is sent too quickly. There is also a connection
1437 ;; buffer local variable, which is computed depending on remote host properties
1438 ;; when `tramp-chunksize' is zero or nil.
1439 (defcustom tramp-chunksize
1440 (when (and (not (featurep 'xemacs))
1441 (memq system-type '(hpux)))
1443 ;; Parentheses in docstring starting at beginning of line are escaped.
1444 ;; Fontification is messed up when
1445 ;; `open-paren-in-column-0-is-defun-start' set to t.
1446 "*If non-nil, chunksize for sending input to local process.
1447 It is necessary only on systems which have a buggy `process-send-string'
1448 implementation. The necessity, whether this variable must be set, can be
1449 checked via the following code:
1452 (let* ((user \"xxx\") (host \"yyy\")
1454 (sent init) (received init))
1455 (while (= sent received)
1456 (setq sent (+ sent step))
1458 (let ((proc (start-process (buffer-name) (current-buffer)
1459 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1460 (when (memq (process-status proc) '(run open))
1461 (process-send-string proc (make-string sent ?\\ ))
1462 (process-send-eof proc)
1463 (process-send-eof proc))
1464 (while (not (progn (goto-char (point-min))
1465 (re-search-forward \"\\\\w+\" (point-max) t)))
1466 (accept-process-output proc 1))
1467 (when (memq (process-status proc) '(run open))
1468 (setq received (string-to-number (match-string 0)))
1469 (delete-process proc)
1470 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1472 (if (> sent (+ init step))
1473 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1475 (message \"Test does not work\")
1476 (display-buffer (current-buffer))
1479 In the Emacs normally running Tramp, evaluate the above code
1480 \(replace \"xxx\" and \"yyy\" by the remote user and host name,
1481 respectively). You can do this, for example, by pasting it into
1482 the `*scratch*' buffer and then hitting C-j with the cursor after the
1483 last closing parenthesis. Note that it works only if you have configured
1484 \"ssh\" to run without password query, see ssh-agent(1).
1486 You will see the number of bytes sent successfully to the remote host.
1487 If that number exceeds 1000, you can stop the execution by hitting
1488 C-g, because your Emacs is likely clean.
1490 If your Emacs is buggy, the code stops and gives you an indication
1491 about the value `tramp-chunksize' should be set. Maybe you could just
1492 experiment a bit, e.g. changing the values of `init' and `step'
1493 in the third line of the code.
1495 When it is necessary to set `tramp-chunksize', you might consider to
1496 use an out-of-the-band method (like \"scp\") instead of an internal one
1497 \(like \"ssh\"), because setting `tramp-chunksize' to non-nil decreases
1500 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1501 this variable to be set as well."
1503 :type '(choice (const nil) integer))
1505 ;; Logging in to a remote host normally requires obtaining a pty. But
1506 ;; Emacs on MacOS X has process-connection-type set to nil by default,
1507 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1508 ;; for an override of the system default.
1509 (defcustom tramp-process-connection-type t
1510 "Overrides `process-connection-type' for connections from Tramp.
1511 Tramp binds process-connection-type to the value given here before
1512 opening a connection to a remote host."
1514 :type '(choice (const nil) (const t) (const pty)))
1516 ;;; Internal Variables:
1518 (defvar tramp-buffer-file-attributes nil
1519 "Holds the `ls -ild' output for the current buffer.
1520 This variable is local to each buffer. It is not used if the remote
1521 machine groks Perl. If it is used, it's used as an emulation for
1522 the visited file modtime.")
1523 (make-variable-buffer-local 'tramp-buffer-file-attributes)
1525 (defvar tramp-md5-function
1526 (cond ((and (require 'md5) (fboundp 'md5)) 'md5)
1527 ((fboundp 'md5-encode)
1528 (lambda (x) (base64-encode-string
1529 (funcall (symbol-function 'md5-encode) x))))
1530 (t (error "Couldn't find an `md5' function")))
1531 "Function to call for running the MD5 algorithm.")
1533 (defvar tramp-end-of-output
1535 (funcall tramp-md5-function
1537 (prin1-to-string process-environment)
1538 (current-time-string)
1540 ;; (if (fboundp 'directory-files-and-attributes)
1541 ;; (funcall 'directory-files-and-attributes
1542 ;; (or (getenv "HOME")
1543 ;; (tramp-temporary-file-directory)))
1546 ;; (cons x (file-attributes x)))
1547 ;; (directory-files (or (getenv "HOME")
1548 ;; (tramp-temporary-file-directory))
1551 "String used to recognize end of output.")
1553 (defvar tramp-connection-function nil
1554 "This internal variable holds a parameter for `tramp-methods'.
1555 In the connection buffer, this variable has the value of the like-named
1556 method parameter, as specified in `tramp-methods' (which see).")
1558 (defvar tramp-remote-sh nil
1559 "This internal variable holds a parameter for `tramp-methods'.
1560 In the connection buffer, this variable has the value of the like-named
1561 method parameter, as specified in `tramp-methods' (which see).")
1563 (defvar tramp-login-program nil
1564 "This internal variable holds a parameter for `tramp-methods'.
1565 In the connection buffer, this variable has the value of the like-named
1566 method parameter, as specified in `tramp-methods' (which see).")
1568 (defvar tramp-login-args nil
1569 "This internal variable holds a parameter for `tramp-methods'.
1570 In the connection buffer, this variable has the value of the like-named
1571 method parameter, as specified in `tramp-methods' (which see).")
1573 (defvar tramp-copy-program nil
1574 "This internal variable holds a parameter for `tramp-methods'.
1575 In the connection buffer, this variable has the value of the like-named
1576 method parameter, as specified in `tramp-methods' (which see).")
1578 (defvar tramp-copy-args nil
1579 "This internal variable holds a parameter for `tramp-methods'.
1580 In the connection buffer, this variable has the value of the like-named
1581 method parameter, as specified in `tramp-methods' (which see).")
1583 (defvar tramp-copy-keep-date-arg nil
1584 "This internal variable holds a parameter for `tramp-methods'.
1585 In the connection buffer, this variable has the value of the like-named
1586 method parameter, as specified in `tramp-methods' (which see).")
1588 (defvar tramp-encoding-command nil
1589 "This internal variable holds a parameter for `tramp-methods'.
1590 In the connection buffer, this variable has the value of the like-named
1591 method parameter, as specified in `tramp-methods' (which see).")
1593 (defvar tramp-decoding-command nil
1594 "This internal variable holds a parameter for `tramp-methods'.
1595 In the connection buffer, this variable has the value of the like-named
1596 method parameter, as specified in `tramp-methods' (which see).")
1598 (defvar tramp-encoding-function nil
1599 "This internal variable holds a parameter for `tramp-methods'.
1600 In the connection buffer, this variable has the value of the like-named
1601 method parameter, as specified in `tramp-methods' (which see).")
1603 (defvar tramp-decoding-function nil
1604 "This internal variable holds a parameter for `tramp-methods'.
1605 In the connection buffer, this variable has the value of the like-named
1606 method parameter, as specified in `tramp-methods' (which see).")
1608 (defvar tramp-password-end-of-line nil
1609 "This internal variable holds a parameter for `tramp-methods'.
1610 In the connection buffer, this variable has the value of the like-named
1611 method parameter, as specified in `tramp-methods' (which see).")
1613 ;; CCC `local in each buffer'?
1614 (defvar tramp-ls-command nil
1615 "This command is used to get a long listing with numeric user and group ids.
1616 This variable is automatically made buffer-local to each rsh process buffer
1617 upon opening the connection.")
1619 (defvar tramp-current-multi-method nil
1620 "Name of `multi' connection method for this *tramp* buffer, or nil if not multi.
1621 This variable is automatically made buffer-local to each rsh process buffer
1622 upon opening the connection.")
1624 (defvar tramp-current-method nil
1625 "Connection method for this *tramp* buffer.
1626 This variable is automatically made buffer-local to each rsh process buffer
1627 upon opening the connection.")
1629 (defvar tramp-current-user nil
1630 "Remote login name for this *tramp* buffer.
1631 This variable is automatically made buffer-local to each rsh process buffer
1632 upon opening the connection.")
1634 (defvar tramp-current-host nil
1635 "Remote host for this *tramp* buffer.
1636 This variable is automatically made buffer-local to each rsh process buffer
1637 upon opening the connection.")
1639 (defvar tramp-test-groks-nt nil
1640 "Whether the `test' command groks the `-nt' switch.
1641 \(`test A -nt B' tests if file A is newer than file B.)
1642 This variable is automatically made buffer-local to each rsh process buffer
1643 upon opening the connection.")
1645 (defvar tramp-file-exists-command nil
1646 "Command to use for checking if a file exists.
1647 This variable is automatically made buffer-local to each rsh process buffer
1648 upon opening the connection.")
1650 (defconst tramp-uudecode "\
1652 \(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1656 "Shell function to implement `uudecode' to standard output.
1657 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
1658 for this or `uudecode -p', but some systems don't, and for them
1659 we have this shell function.")
1661 ;; Perl script to implement `file-attributes' in a Lisp `read'able
1662 ;; output. If you are hacking on this, note that you get *no* output
1663 ;; unless this spits out a complete line, including the '\n' at the
1665 ;; The device number is returned as "-1", because there will be a virtual
1666 ;; device number set in `tramp-handle-file-attributes'
1667 (defconst tramp-perl-file-attributes "\
1668 @stat = lstat($ARGV[0]);
1669 if (($stat[2] & 0170000) == 0120000)
1671 $type = readlink($ARGV[0]);
1672 $type = \"\\\"$type\\\"\";
1674 elsif (($stat[2] & 0170000) == 040000)
1682 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1683 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1685 \"(%s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) -1)\\n\",
1690 $stat[8] >> 16 & 0xffff,
1692 $stat[9] >> 16 & 0xffff,
1694 $stat[10] >> 16 & 0xffff,
1698 $stat[1] >> 16 & 0xffff,
1701 "Perl script to produce output suitable for use with `file-attributes'
1702 on the remote file system.")
1704 (defconst tramp-perl-directory-files-and-attributes "\
1705 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
1706 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
1707 @list = readdir(DIR);
1711 for($i = 0; $i < $n; $i++)
1713 $filename = $list[$i];
1714 @stat = lstat($filename);
1715 if (($stat[2] & 0170000) == 0120000)
1717 $type = readlink($filename);
1718 $type = \"\\\"$type\\\"\";
1720 elsif (($stat[2] & 0170000) == 040000)
1728 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1729 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1731 \"(\\\"%s\\\" %s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) (%u %u))\\n\",
1737 $stat[8] >> 16 & 0xffff,
1739 $stat[9] >> 16 & 0xffff,
1741 $stat[10] >> 16 & 0xffff,
1745 $stat[1] >> 16 & 0xffff,
1747 $stat[0] >> 16 & 0xffff,
1751 "Perl script implementing `directory-files-attributes' as Lisp `read'able
1754 ;; ;; These two use uu encoding.
1755 ;; (defvar tramp-perl-encode "%s -e'\
1756 ;; print qq(begin 644 xxx\n);
1759 ;; while (read(STDIN, $s, 45)) {
1760 ;; print pack(q(u), $s);
1765 ;; "Perl program to use for encoding a file.
1766 ;; Escape sequence %s is replaced with name of Perl binary.")
1768 ;; (defvar tramp-perl-decode "%s -ne '
1769 ;; print unpack q(u), $_;
1771 ;; "Perl program to use for decoding a file.
1772 ;; Escape sequence %s is replaced with name of Perl binary.")
1774 ;; These two use base64 encoding.
1775 (defvar tramp-perl-encode-with-module
1776 "perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)'"
1777 "Perl program to use for encoding a file.
1778 Escape sequence %s is replaced with name of Perl binary.
1779 This string is passed to `format', so percent characters need to be doubled.
1780 This implementation requires the MIME::Base64 Perl module to be installed
1781 on the remote host.")
1783 (defvar tramp-perl-decode-with-module
1784 "perl -MMIME::Base64 -0777 -ne 'print decode_base64($_)'"
1785 "Perl program to use for decoding a file.
1786 Escape sequence %s is replaced with name of Perl binary.
1787 This string is passed to `format', so percent characters need to be doubled.
1788 This implementation requires the MIME::Base64 Perl module to be installed
1789 on the remote host.")
1791 (defvar tramp-perl-encode
1793 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1794 # Copyright (C) 2002, 2006 Free Software Foundation, Inc.
1799 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1800 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1805 # We read in chunks of 54 bytes, to generate output lines
1806 # of 72 chars (plus end of line)
1809 while (my $data = <STDIN>) {
1812 # Only for the last chunk, and only if did not fill the last three-byte packet
1814 my $mod = length($data) %% 3;
1815 $pad = q(=) x (3 - $mod) if $mod;
1818 # Not the fastest method, but it is simple: unpack to binary string, split
1819 # by groups of 6 bits and convert back from binary to byte; then map into
1820 # the translation table
1824 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1829 "Perl program to use for encoding a file.
1830 Escape sequence %s is replaced with name of Perl binary.
1831 This string is passed to `format', so percent characters need to be doubled.")
1833 (defvar tramp-perl-decode
1835 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1836 # Copyright (C) 2002, 2006 Free Software Foundation, Inc.
1841 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1842 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1845 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1849 # We are going to accumulate into $pending to accept any line length
1850 # (we do not check they are <= 76 chars as the RFC says)
1853 while (my $data = <STDIN>) {
1856 # If we find one or two =, we have reached the end and
1857 # any following data is to be discarded
1858 my $finished = $data =~ s/(==?).*/$1/;
1861 my $len = length($pending);
1862 my $chunk = substr($pending, 0, $len & ~3);
1863 $pending = substr($pending, $len & ~3 + 1);
1865 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1866 # split in 8-bit chunks and convert back to char.
1869 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1874 "Perl program to use for decoding a file.
1875 Escape sequence %s is replaced with name of Perl binary.
1876 This string is passed to `format', so percent characters need to be doubled.")
1878 (defconst tramp-file-mode-type-map '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1880 (2 . "c") ; character device
1881 (3 . "m") ; multiplexed character device (v7)
1882 (4 . "d") ; directory
1883 (5 . "?") ; Named special file (XENIX)
1884 (6 . "b") ; block device
1885 (7 . "?") ; multiplexed block device (v7)
1886 (8 . "-") ; regular file
1887 (9 . "n") ; network special file (HP-UX)
1888 (10 . "l") ; symlink
1889 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
1891 (13 . "D") ; door special (Solaris)
1892 (14 . "w")) ; whiteout (BSD)
1893 "A list of file types returned from the `stat' system call.
1894 This is used to map a mode number to a permission string.")
1896 (defvar tramp-dos-coding-system
1897 (if (and (fboundp 'coding-system-p)
1898 (funcall 'coding-system-p '(dos)))
1901 "Some Emacsen know the `dos' coding system, others need `undecided-dos'.")
1903 (defvar tramp-last-cmd nil
1904 "Internal Tramp variable recording the last command sent.
1905 This variable is buffer-local in every buffer.")
1906 (make-variable-buffer-local 'tramp-last-cmd)
1908 (defvar tramp-process-echoes nil
1909 "Whether to process echoes from the remote shell.")
1911 (defvar tramp-last-cmd-time nil
1912 "Internal Tramp variable recording the time when the last cmd was sent.
1913 This variable is buffer-local in every buffer.")
1914 (make-variable-buffer-local 'tramp-last-cmd-time)
1916 ;; This variable does not have the right value in XEmacs. What should
1917 ;; I use instead of find-operation-coding-system in XEmacs?
1918 (defvar tramp-feature-write-region-fix
1919 (when (fboundp 'find-operation-coding-system)
1920 (let ((file-coding-system-alist '(("test" emacs-mule))))
1921 (funcall (symbol-function 'find-operation-coding-system)
1922 'write-region 0 0 "" nil "test")))
1923 "Internal variable to say if `write-region' chooses the right coding.
1924 Older versions of Emacs chose the coding system for `write-region' based
1925 on the FILENAME argument, even if VISIT was a string.")
1927 ;; New handlers should be added here. The following operations can be
1928 ;; handled using the normal primitives: file-name-as-directory,
1929 ;; file-name-directory, file-name-nondirectory,
1930 ;; file-name-sans-versions, get-file-buffer.
1931 (defconst tramp-file-name-handler-alist
1933 (load . tramp-handle-load)
1934 (make-symbolic-link . tramp-handle-make-symbolic-link)
1935 (file-name-directory . tramp-handle-file-name-directory)
1936 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1937 (file-truename . tramp-handle-file-truename)
1938 (file-exists-p . tramp-handle-file-exists-p)
1939 (file-directory-p . tramp-handle-file-directory-p)
1940 (file-executable-p . tramp-handle-file-executable-p)
1941 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
1942 (file-readable-p . tramp-handle-file-readable-p)
1943 (file-regular-p . tramp-handle-file-regular-p)
1944 (file-symlink-p . tramp-handle-file-symlink-p)
1945 (file-writable-p . tramp-handle-file-writable-p)
1946 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
1947 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
1948 (file-attributes . tramp-handle-file-attributes)
1949 (file-modes . tramp-handle-file-modes)
1950 (directory-files . tramp-handle-directory-files)
1951 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes)
1952 (file-name-all-completions . tramp-handle-file-name-all-completions)
1953 (file-name-completion . tramp-handle-file-name-completion)
1954 (add-name-to-file . tramp-handle-add-name-to-file)
1955 (copy-file . tramp-handle-copy-file)
1956 (rename-file . tramp-handle-rename-file)
1957 (set-file-modes . tramp-handle-set-file-modes)
1958 (make-directory . tramp-handle-make-directory)
1959 (delete-directory . tramp-handle-delete-directory)
1960 (delete-file . tramp-handle-delete-file)
1961 (directory-file-name . tramp-handle-directory-file-name)
1962 (shell-command . tramp-handle-shell-command)
1963 (process-file . tramp-handle-process-file)
1964 (insert-directory . tramp-handle-insert-directory)
1965 (expand-file-name . tramp-handle-expand-file-name)
1966 (file-local-copy . tramp-handle-file-local-copy)
1967 (file-remote-p . tramp-handle-file-remote-p)
1968 (insert-file-contents . tramp-handle-insert-file-contents)
1969 (write-region . tramp-handle-write-region)
1970 (find-backup-file-name . tramp-handle-find-backup-file-name)
1971 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
1972 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
1973 (dired-compress-file . tramp-handle-dired-compress-file)
1974 (dired-call-process . tramp-handle-dired-call-process)
1975 (dired-recursive-delete-directory
1976 . tramp-handle-dired-recursive-delete-directory)
1977 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
1978 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime))
1979 "Alist of handler functions.
1980 Operations not mentioned here will be handled by the normal Emacs functions.")
1982 ;; Handlers for partial tramp file names. For Emacs just
1983 ;; `file-name-all-completions' is needed.
1985 (defconst tramp-completion-file-name-handler-alist
1986 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions)
1987 (file-name-completion . tramp-completion-handle-file-name-completion))
1988 "Alist of completion handler functions.
1989 Used for file names matching `tramp-file-name-regexp'. Operations not
1990 mentioned here will be handled by `tramp-file-name-handler-alist' or the
1991 normal Emacs functions.")
1993 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
1994 (defvar tramp-foreign-file-name-handler-alist
1995 ;; (identity . tramp-sh-file-name-handler) should always be the last
1996 ;; entry, since `identity' always matches.
1997 '((identity . tramp-sh-file-name-handler))
1998 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1999 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
2002 ;;; Internal functions which must come first.
2004 (defsubst tramp-message (level fmt-string &rest args)
2005 "Emit a message depending on verbosity level.
2006 First arg LEVEL says to be quiet if `tramp-verbose' is less than LEVEL. The
2007 message is emitted only if `tramp-verbose' is greater than or equal to LEVEL.
2008 Calls function `message' with FMT-STRING as control string and the remaining
2009 ARGS to actually emit the message (if applicable).
2011 This function expects to be called from the tramp buffer only!"
2012 (when (<= level tramp-verbose)
2013 (apply #'message (concat "tramp: " fmt-string) args)
2014 (when tramp-debug-buffer
2017 (tramp-get-debug-buffer
2018 tramp-current-multi-method tramp-current-method
2019 tramp-current-user tramp-current-host))
2020 (goto-char (point-max))
2023 (tramp-insert-with-face
2025 (concat "# " (apply #'format fmt-string args) "\n"))))))
2027 (defun tramp-message-for-buffer
2028 (multi-method method user host level fmt-string &rest args)
2029 "Like `tramp-message' but temporarily switches to the tramp buffer.
2030 First three args METHOD, USER, and HOST identify the tramp buffer to use,
2031 remaining args passed to `tramp-message'."
2033 (set-buffer (tramp-get-buffer multi-method method user host))
2034 (apply 'tramp-message level fmt-string args)))
2036 (defsubst tramp-line-end-position nil
2037 "Return point at end of line.
2038 Calls `line-end-position' or `point-at-eol' if defined, else
2039 own implementation."
2041 ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position)))
2042 ((fboundp 'point-at-eol) (funcall (symbol-function 'point-at-eol)))
2043 (t (save-excursion (end-of-line) (point)))))
2045 (defmacro with-parsed-tramp-file-name (filename var &rest body)
2046 "Parse a Tramp filename and make components available in the body.
2048 First arg FILENAME is evaluated and dissected into its components.
2049 Second arg VAR is a symbol. It is used as a variable name to hold
2050 the filename structure. It is also used as a prefix for the variables
2051 holding the components. For example, if VAR is the symbol `foo', then
2052 `foo' will be bound to the whole structure, `foo-multi-method' will
2053 be bound to the multi-method component, and so on for `foo-method',
2054 `foo-user', `foo-host', `foo-localname'.
2056 Remaining args are Lisp expressions to be evaluated (inside an implicit
2059 If VAR is nil, then we bind `v' to the structure and `multi-method',
2060 `method', `user', `host', `localname' to the components."
2061 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
2062 (,(if var (intern (concat (symbol-name var) "-multi-method")) 'multi-method)
2063 (tramp-file-name-multi-method ,(or var 'v)))
2064 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
2065 (tramp-file-name-method ,(or var 'v)))
2066 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
2067 (tramp-file-name-user ,(or var 'v)))
2068 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
2069 (tramp-file-name-host ,(or var 'v)))
2070 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
2071 (tramp-file-name-localname ,(or var 'v))))
2074 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
2075 ;; Enable debugging.
2076 (def-edebug-spec with-parsed-tramp-file-name (form symbolp body))
2077 ;; Highlight as keyword.
2078 (when (functionp 'font-lock-add-keywords)
2079 (funcall 'font-lock-add-keywords
2080 'emacs-lisp-mode '("\\<with-parsed-tramp-file-name\\>")))
2082 (defmacro tramp-let-maybe (variable value &rest body)
2083 "Let-bind VARIABLE to VALUE in BODY, but only if VARIABLE is not obsolete.
2084 BODY is executed whether or not the variable is obsolete.
2085 The intent is to protect against `obsolete variable' warnings."
2086 `(if (get ',variable 'byte-obsolete-variable)
2088 (let ((,variable ,value))
2090 (put 'tramp-let-maybe 'lisp-indent-function 2)
2091 (put 'tramp-let-maybe 'edebug-form-spec t)
2093 ;;; Config Manipulation Functions:
2095 (defun tramp-set-completion-function (method function-list)
2096 "Sets the list of completion functions for METHOD.
2097 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2098 The FUNCTION is intended to parse FILE according its syntax.
2099 It might be a predefined FUNCTION, or a user defined FUNCTION.
2100 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
2101 `tramp-parse-sconfig',`tramp-parse-hosts', `tramp-parse-passwd',
2102 and `tramp-parse-netrc'.
2106 (tramp-set-completion-function
2108 '((tramp-parse-sconfig \"/etc/ssh_config\")
2109 (tramp-parse-sconfig \"~/.ssh/config\")))"
2111 (let ((r function-list)
2113 (setq tramp-completion-function-alist
2114 (delete (assoc method tramp-completion-function-alist)
2115 tramp-completion-function-alist))
2118 ;; Remove double entries
2119 (when (member (car v) (cdr v))
2120 (setcdr v (delete (car v) (cdr v))))
2121 ;; Check for function and file
2122 (unless (and (functionp (nth 0 (car v)))
2123 (file-exists-p (nth 1 (car v))))
2124 (setq r (delete (car v) r)))
2128 (add-to-list 'tramp-completion-function-alist
2131 (defun tramp-get-completion-function (method)
2132 "Returns list of completion functions for METHOD.
2133 For definition of that list see `tramp-set-completion-function'."
2134 (cdr (assoc method tramp-completion-function-alist)))
2136 ;;; File Name Handler Functions:
2138 (defun tramp-handle-make-symbolic-link
2139 (filename linkname &optional ok-if-already-exists)
2140 "Like `make-symbolic-link' for tramp files.
2141 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
2142 the symlink. If LINKNAME is a Tramp file, only the localname component is
2143 used as the target of the symlink.
2145 If LINKNAME is a Tramp file and the localname component is relative, then
2146 it is expanded first, before the localname component is taken. Note that
2147 this can give surprising results if the user/host for the source and
2148 target of the symlink differ."
2149 (with-parsed-tramp-file-name linkname l
2150 (let ((ln (tramp-get-remote-ln l-multi-method l-method l-user l-host))
2151 (cwd (file-name-directory l-localname)))
2154 (list "Making a symbolic link."
2155 "ln(1) does not exist on the remote host.")))
2157 ;; Do the 'confirm if exists' thing.
2158 (when (file-exists-p linkname)
2160 (if (or (null ok-if-already-exists) ; not allowed to exist
2161 (and (numberp ok-if-already-exists)
2164 "File %s already exists; make it a link anyway? "
2166 (signal 'file-already-exists (list "File already exists" l-localname))
2167 (delete-file linkname)))
2169 ;; If FILENAME is a Tramp name, use just the localname component.
2170 (when (tramp-tramp-file-p filename)
2171 (setq filename (tramp-file-name-localname
2172 (tramp-dissect-file-name
2173 (expand-file-name filename)))))
2175 ;; Right, they are on the same host, regardless of user, method, etc.
2176 ;; We now make the link on the remote machine. This will occur as the user
2177 ;; that FILENAME belongs to.
2179 (tramp-send-command-and-check
2180 l-multi-method l-method l-user l-host
2181 (format "cd %s && %s -sf %s %s"
2188 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
2189 "Like `load' for tramp files. Not implemented!"
2190 (unless (file-name-absolute-p file)
2191 (error "Tramp cannot `load' files without absolute file name"))
2192 (with-parsed-tramp-file-name file nil
2194 (cond ((file-exists-p (concat file ".elc"))
2195 (setq file (concat file ".elc")))
2196 ((file-exists-p (concat file ".el"))
2197 (setq file (concat file ".el")))))
2199 ;; The first condition is always true for absolute file names.
2200 ;; Included for safety's sake.
2201 (unless (or (file-name-directory file)
2202 (string-match "\\.elc?\\'" file))
2203 (error "File `%s' does not include a `.el' or `.elc' suffix"
2206 (when (not (file-exists-p file))
2207 (error "Cannot load nonexistent file `%s'" file)))
2208 (if (not (file-exists-p file))
2211 (message "Loading %s..." file))
2212 (let ((local-copy (file-local-copy file)))
2213 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2214 (load local-copy noerror t t)
2215 (delete-file local-copy))
2217 (message "Loading %s...done" file))
2220 ;; Localname manipulation functions that grok TRAMP localnames...
2221 (defun tramp-handle-file-name-directory (file)
2222 "Like `file-name-directory' but aware of TRAMP files."
2223 ;; Everything except the last filename thing is the directory.
2224 (with-parsed-tramp-file-name file nil
2225 ;; Run the command on the localname portion only.
2226 (tramp-make-tramp-file-name
2227 multi-method method user host (file-name-directory (or localname "")))))
2229 (defun tramp-handle-file-name-nondirectory (file)
2230 "Like `file-name-nondirectory' but aware of TRAMP files."
2231 (with-parsed-tramp-file-name file nil
2232 (file-name-nondirectory localname)))
2234 (defun tramp-handle-file-truename (filename &optional counter prev-dirs)
2235 "Like `file-truename' for tramp files."
2236 (with-parsed-tramp-file-name (expand-file-name filename) nil
2237 (let* ((steps (tramp-split-string localname "/"))
2238 (localnamedir (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
2239 (file-name-as-directory localname)))
2240 (is-dir (string= localname localnamedir))
2243 ;; Don't make the following value larger than necessary.
2244 ;; People expect an error message in a timely fashion when
2245 ;; something is wrong; otherwise they might think that Emacs
2246 ;; is hung. Of course, correctness has to come first.
2248 (result nil) ;result steps in reverse order
2250 (tramp-message-for-buffer
2251 multi-method method user host
2252 10 "Finding true name for `%s'" filename)
2253 (while (and steps (< numchase numchase-limit))
2254 (setq thisstep (pop steps))
2255 (tramp-message-for-buffer
2256 multi-method method user host
2258 (mapconcat 'identity
2259 (append '("") (reverse result) (list thisstep))
2261 (setq symlink-target
2262 (nth 0 (file-attributes
2263 (tramp-make-tramp-file-name
2264 multi-method method user host
2265 (mapconcat 'identity
2270 (cond ((string= "." thisstep)
2271 (tramp-message-for-buffer multi-method method user host
2272 10 "Ignoring step `.'"))
2273 ((string= ".." thisstep)
2274 (tramp-message-for-buffer multi-method method user host
2275 10 "Processing step `..'")
2277 ((stringp symlink-target)
2278 ;; It's a symlink, follow it.
2279 (tramp-message-for-buffer
2280 multi-method method user host
2281 10 "Follow symlink to %s" symlink-target)
2282 (setq numchase (1+ numchase))
2283 (when (file-name-absolute-p symlink-target)
2285 ;; If the symlink was absolute, we'll get a string like
2286 ;; "/user@host:/some/target"; extract the
2287 ;; "/some/target" part from it.
2288 (when (tramp-tramp-file-p symlink-target)
2289 (with-parsed-tramp-file-name symlink-target sym
2290 (unless (equal (list multi-method method user host)
2291 (list sym-multi-method sym-method
2293 (error "Symlink target `%s' on wrong host"
2295 (setq symlink-target localname)))
2297 (append (tramp-split-string symlink-target "/") steps)))
2300 (setq result (cons thisstep result)))))
2301 (when (>= numchase numchase-limit)
2302 (error "Maximum number (%d) of symlinks exceeded" numchase-limit))
2303 (setq result (reverse result))
2304 ;; Combine list to form string.
2307 (mapconcat 'identity (cons "" result) "/")
2309 (when (and is-dir (or (string= "" result)
2310 (not (string= (substring result -1) "/"))))
2311 (setq result (concat result "/")))
2312 (tramp-message-for-buffer
2313 multi-method method user host
2314 10 "True name of `%s' is `%s'" filename result)
2315 (tramp-make-tramp-file-name
2316 multi-method method user host result))))
2320 (defun tramp-handle-file-exists-p (filename)
2321 "Like `file-exists-p' for tramp files."
2322 (with-parsed-tramp-file-name filename nil
2324 (zerop (tramp-send-command-and-check
2325 multi-method method user host
2327 (tramp-get-file-exists-command multi-method method user host)
2328 (tramp-shell-quote-argument localname)))))))
2330 ;; Devices must distinguish physical file systems. The device numbers
2331 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2332 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2333 ;; EFS use device number "-1". In order to be different, we use device number
2334 ;; (-1 x), whereby "x" is unique for a given (multi-method method user host).
2335 (defvar tramp-devices nil
2336 "Keeps virtual device numbers.")
2338 ;; CCC: This should check for an error condition and signal failure
2339 ;; when something goes wrong.
2340 ;; Daniel Pittman <daniel@danann.net>
2341 (defun tramp-handle-file-attributes (filename &optional id-format)
2342 "Like `file-attributes' for tramp files."
2343 (when (file-exists-p filename)
2344 ;; file exists, find out stuff
2345 (unless id-format (setq id-format 'integer))
2346 (with-parsed-tramp-file-name filename nil
2348 (tramp-convert-file-attributes
2349 multi-method method user host
2350 (if (tramp-get-remote-perl multi-method method user host)
2351 (tramp-handle-file-attributes-with-perl multi-method method user host
2352 localname id-format)
2353 (tramp-handle-file-attributes-with-ls multi-method method user host
2354 localname id-format)))))))
2356 (defun tramp-handle-file-attributes-with-ls
2357 (multi-method method user host localname &optional id-format)
2358 "Implement `file-attributes' for tramp files using the ls(1) command."
2360 res-inode res-filemodes res-numlinks
2361 res-uid res-gid res-size res-symlink-target)
2362 (tramp-message-for-buffer multi-method method user host 10
2363 "file attributes with ls: %s"
2364 (tramp-make-tramp-file-name
2365 multi-method method user host localname))
2367 multi-method method user host
2369 (tramp-get-ls-command multi-method method user host)
2370 (if (eq id-format 'integer) "-ildn" "-ild")
2371 (tramp-shell-quote-argument localname)))
2372 (tramp-wait-for-output)
2373 ;; parse `ls -l' output ...
2377 (read (current-buffer))
2378 (invalid-read-syntax
2379 (when (and (equal (cadr err)
2380 "Integer constant overflow in reader")
2382 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2384 (let* ((big (read (substring (caddr err) 0
2385 (match-beginning 1))))
2386 (small (read (match-string 1 (caddr err))))
2387 (twiddle (/ small 65536)))
2388 (cons (+ big twiddle)
2389 (- small (* twiddle 65536))))))))
2390 ;; ... file mode flags
2391 (setq res-filemodes (symbol-name (read (current-buffer))))
2393 (setq res-numlinks (read (current-buffer)))
2395 (setq res-uid (read (current-buffer)))
2396 (setq res-gid (read (current-buffer)))
2397 (when (eq id-format 'integer)
2398 (unless (numberp res-uid) (setq res-uid -1))
2399 (unless (numberp res-gid) (setq res-gid -1)))
2401 (setq res-size (read (current-buffer)))
2402 ;; From the file modes, figure out other stuff.
2403 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2404 (setq dirp (eq ?d (aref res-filemodes 0)))
2405 ;; if symlink, find out file name pointed to
2407 (search-forward "-> ")
2408 (setq res-symlink-target
2409 (buffer-substring (point)
2410 (tramp-line-end-position))))
2411 ;; return data gathered
2413 ;; 0. t for directory, string (name linked to) for symbolic
2415 (or dirp res-symlink-target nil)
2416 ;; 1. Number of links to file.
2422 ;; 4. Last access time, as a list of two integers. First
2423 ;; integer has high-order 16 bits of time, second has low 16
2425 ;; 5. Last modification time, likewise.
2426 ;; 6. Last status change time, likewise.
2427 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2428 ;; 7. Size in bytes (-1, if number is out of range).
2430 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2432 ;; 9. t iff file's gid would change if file were deleted and
2433 ;; recreated. Will be set in `tramp-convert-file-attributes'
2435 ;; 10. inode number.
2437 ;; 11. Device number. Will be replaced by a virtual device number.
2441 (defun tramp-handle-file-attributes-with-perl
2442 (multi-method method user host localname &optional id-format)
2443 "Implement `file-attributes' for tramp files using a Perl script."
2444 (tramp-message-for-buffer multi-method method user host 10
2445 "file attributes with perl: %s"
2446 (tramp-make-tramp-file-name
2447 multi-method method user host localname))
2448 (tramp-maybe-send-perl-script multi-method method user host
2449 tramp-perl-file-attributes
2450 "tramp_file_attributes")
2451 (tramp-send-command multi-method method user host
2452 (format "tramp_file_attributes %s %s"
2453 (tramp-shell-quote-argument localname) id-format))
2454 (tramp-wait-for-output)
2455 (read (current-buffer)))
2457 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
2458 "Like `set-visited-file-modtime' for tramp files."
2459 (unless (buffer-file-name)
2460 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2463 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
2464 (let ((f (buffer-file-name))
2466 (with-parsed-tramp-file-name f nil
2467 (let* ((attr (file-attributes f))
2468 ;; '(-1 65535) means file doesn't exists yet.
2469 (modtime (or (nth 5 attr) '(-1 65535))))
2470 (when (boundp 'last-coding-system-used)
2471 (setq coding-system-used (symbol-value 'last-coding-system-used)))
2472 ;; We use '(0 0) as a don't-know value. See also
2473 ;; `tramp-handle-file-attributes-with-ls'.
2474 (if (not (equal modtime '(0 0)))
2475 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
2478 multi-method method user host
2479 (format "%s -ild %s"
2480 (tramp-get-ls-command multi-method method user host)
2481 (tramp-shell-quote-argument localname)))
2482 (tramp-wait-for-output)
2483 (setq attr (buffer-substring (point)
2484 (progn (end-of-line) (point)))))
2485 (setq tramp-buffer-file-attributes attr))
2486 (when (boundp 'last-coding-system-used)
2487 (set 'last-coding-system-used coding-system-used))
2490 ;; CCC continue here
2492 ;; This function makes the same assumption as
2493 ;; `tramp-handle-set-visited-file-modtime'.
2494 (defun tramp-handle-verify-visited-file-modtime (buf)
2495 "Like `verify-visited-file-modtime' for tramp files.
2496 At the time `verify-visited-file-modtime' calls this function, we
2497 already know that the buffer is visiting a file and that
2498 `visited-file-modtime' does not return 0. Do not call this
2499 function directly, unless those two cases are already taken care
2501 (with-current-buffer buf
2502 ;; There is no file visiting the buffer, or the buffer has no
2503 ;; recorded last modification time.
2504 (if (or (not (buffer-file-name))
2505 (eq (visited-file-modtime) 0))
2507 (let ((f (buffer-file-name)))
2508 (with-parsed-tramp-file-name f nil
2509 (let* ((attr (file-attributes f))
2510 (modtime (nth 5 attr))
2511 (mt (visited-file-modtime)))
2514 ;; file exists, and has a known modtime.
2515 ((and attr (not (equal modtime '(0 0))))
2516 (< (abs (tramp-time-diff
2518 ;; For compatibility, deal with both the old
2519 ;; (HIGH . LOW) and the new (HIGH LOW)
2520 ;; return values of `visited-file-modtime'.
2522 (list (car mt) (cdr mt))
2525 ;; modtime has the don't know value.
2529 multi-method method user host
2530 (format "%s -ild %s"
2531 (tramp-get-ls-command multi-method method user host)
2532 (tramp-shell-quote-argument localname)))
2533 (tramp-wait-for-output)
2534 (setq attr (buffer-substring
2535 (point) (progn (end-of-line) (point)))))
2536 (equal tramp-buffer-file-attributes attr))
2537 ;; If file does not exist, say it is not modified
2538 ;; if and only if that agrees with the buffer's record.
2539 (t (equal mt '(-1 65535))))))))))
2541 (defun tramp-handle-set-file-modes (filename mode)
2542 "Like `set-file-modes' for tramp files."
2543 (with-parsed-tramp-file-name filename nil
2545 (unless (zerop (tramp-send-command-and-check
2546 multi-method method user host
2547 (format "chmod %s %s"
2548 (tramp-decimal-to-octal mode)
2549 (tramp-shell-quote-argument localname))))
2552 ;; FIXME: extract the proper text from chmod's stderr.
2553 "error while changing file's mode"
2556 ;; Simple functions using the `test' command.
2558 (defun tramp-handle-file-executable-p (filename)
2559 "Like `file-executable-p' for tramp files."
2560 (with-parsed-tramp-file-name filename nil
2561 (zerop (tramp-run-test "-x" filename))))
2563 (defun tramp-handle-file-readable-p (filename)
2564 "Like `file-readable-p' for tramp files."
2565 (with-parsed-tramp-file-name filename nil
2566 (zerop (tramp-run-test "-r" filename))))
2568 (defun tramp-handle-file-accessible-directory-p (filename)
2569 "Like `file-accessible-directory-p' for tramp files."
2570 (with-parsed-tramp-file-name filename nil
2571 (and (zerop (tramp-run-test "-d" filename))
2572 (zerop (tramp-run-test "-r" filename))
2573 (zerop (tramp-run-test "-x" filename)))))
2575 ;; When the remote shell is started, it looks for a shell which groks
2576 ;; tilde expansion. Here, we assume that all shells which grok tilde
2577 ;; expansion will also provide a `test' command which groks `-nt' (for
2578 ;; newer than). If this breaks, tell me about it and I'll try to do
2579 ;; something smarter about it.
2580 (defun tramp-handle-file-newer-than-file-p (file1 file2)
2581 "Like `file-newer-than-file-p' for tramp files."
2582 (cond ((not (file-exists-p file1))
2584 ((not (file-exists-p file2))
2586 ;; We are sure both files exist at this point.
2589 ;; We try to get the mtime of both files. If they are not
2590 ;; equal to the "dont-know" value, then we subtract the times
2591 ;; and obtain the result.
2592 (let ((fa1 (file-attributes file1))
2593 (fa2 (file-attributes file2)))
2594 (if (and (not (equal (nth 5 fa1) '(0 0)))
2595 (not (equal (nth 5 fa2) '(0 0))))
2596 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
2597 ;; If one of them is the dont-know value, then we can
2598 ;; still try to run a shell command on the remote host.
2599 ;; However, this only works if both files are Tramp
2600 ;; files and both have the same method, same user, same
2602 (unless (and (tramp-tramp-file-p file1)
2603 (tramp-tramp-file-p file2))
2607 "Cannot check if Tramp file is newer than non-Tramp file"
2609 (with-parsed-tramp-file-name file1 v1
2610 (with-parsed-tramp-file-name file2 v2
2611 (unless (and (equal v1-multi-method v2-multi-method)
2612 (equal v1-method v2-method)
2613 (equal v1-user v2-user)
2614 (equal v1-host v2-host))
2616 (list "Files must have same method, user, host"
2618 (unless (and (tramp-tramp-file-p file1)
2619 (tramp-tramp-file-p file2))
2621 (list "Files must be tramp files on same host"
2623 (if (tramp-get-test-groks-nt
2624 v1-multi-method v1-method v1-user v1-host)
2625 (zerop (tramp-run-test2 "test" file1 file2 "-nt"))
2626 (zerop (tramp-run-test2
2627 "tramp_test_nt" file1 file2)))))))))))
2629 ;; Functions implemented using the basic functions above.
2631 (defun tramp-handle-file-modes (filename)
2632 "Like `file-modes' for tramp files."
2633 (with-parsed-tramp-file-name filename nil
2634 (when (file-exists-p filename)
2635 (tramp-mode-string-to-int
2636 (nth 8 (file-attributes filename))))))
2638 (defun tramp-handle-file-directory-p (filename)
2639 "Like `file-directory-p' for tramp files."
2640 ;; Care must be taken that this function returns `t' for symlinks
2641 ;; pointing to directories. Surely the most obvious implementation
2642 ;; would be `test -d', but that returns false for such symlinks.
2643 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
2644 ;; I now think he's right. So we could be using `test -d', couldn't
2647 ;; Alternatives: `cd %s', `test -d %s'
2648 (with-parsed-tramp-file-name filename nil
2651 (tramp-send-command-and-check
2652 multi-method method user host
2653 (format "test -d %s"
2654 (tramp-shell-quote-argument localname))
2655 t))))) ;run command in subshell
2657 (defun tramp-handle-file-regular-p (filename)
2658 "Like `file-regular-p' for tramp files."
2659 (with-parsed-tramp-file-name filename nil
2660 (and (file-exists-p filename)
2661 (eq ?- (aref (nth 8 (file-attributes filename)) 0)))))
2663 (defun tramp-handle-file-symlink-p (filename)
2664 "Like `file-symlink-p' for tramp files."
2665 (with-parsed-tramp-file-name filename nil
2666 (let ((x (car (file-attributes filename))))
2668 ;; When Tramp is running on VMS, then `file-name-absolute-p'
2669 ;; might do weird things.
2670 (if (file-name-absolute-p x)
2671 (tramp-make-tramp-file-name
2672 multi-method method user host x)
2675 (defun tramp-handle-file-writable-p (filename)
2676 "Like `file-writable-p' for tramp files."
2677 (with-parsed-tramp-file-name filename nil
2678 (if (file-exists-p filename)
2679 ;; Existing files must be writable.
2680 (zerop (tramp-run-test "-w" filename))
2681 ;; If file doesn't exist, check if directory is writable.
2682 (and (zerop (tramp-run-test
2683 "-d" (file-name-directory filename)))
2684 (zerop (tramp-run-test
2685 "-w" (file-name-directory filename)))))))
2687 (defun tramp-handle-file-ownership-preserved-p (filename)
2688 "Like `file-ownership-preserved-p' for tramp files."
2689 (with-parsed-tramp-file-name filename nil
2690 (let ((attributes (file-attributes filename)))
2691 ;; Return t if the file doesn't exist, since it's true that no
2692 ;; information would be lost by an (attempted) delete and create.
2693 (or (null attributes)
2694 (= (nth 2 attributes)
2695 (tramp-get-remote-uid multi-method method user host))))))
2697 ;; Other file name ops.
2699 ;; ;; Matthias K
\e,Av
\e(Bppe <mkoeppe@mail.math.uni-magdeburg.de>
2700 ;; (defun tramp-handle-directory-file-name (directory)
2701 ;; "Like `directory-file-name' for tramp files."
2702 ;; (if (and (eq (aref directory (- (length directory) 1)) ?/)
2703 ;; (not (eq (aref directory (- (length directory) 2)) ?:)))
2704 ;; (substring directory 0 (- (length directory) 1))
2707 ;; ;; Philippe Troin <phil@fifi.org>
2708 ;; (defun tramp-handle-directory-file-name (directory)
2709 ;; "Like `directory-file-name' for tramp files."
2710 ;; (with-parsed-tramp-file-name directory nil
2711 ;; (let ((directory-length-1 (1- (length directory))))
2713 ;; (if (and (eq (aref directory directory-length-1) ?/)
2714 ;; (eq (string-match tramp-file-name-regexp directory) 0)
2715 ;; (/= (match-end 0) directory-length-1))
2716 ;; (substring directory 0 directory-length-1)
2719 (defun tramp-handle-directory-file-name (directory)
2720 "Like `directory-file-name' for tramp files."
2721 ;; If localname component of filename is "/", leave it unchanged.
2722 ;; Otherwise, remove any trailing slash from localname component.
2723 ;; Method, host, etc, are unchanged. Does it make sense to try
2724 ;; to avoid parsing the filename?
2725 (with-parsed-tramp-file-name directory nil
2726 (if (and (not (zerop (length localname)))
2727 (eq (aref localname (1- (length localname))) ?/)
2728 (not (string= localname "/")))
2729 (substring directory 0 -1)
2732 ;; Directory listings.
2734 (defun tramp-handle-directory-files (directory
2735 &optional full match nosort files-only)
2736 "Like `directory-files' for tramp files."
2737 (with-parsed-tramp-file-name directory nil
2740 (tramp-barf-unless-okay
2741 multi-method method user host
2742 (concat "cd " (tramp-shell-quote-argument localname))
2745 "tramp-handle-directory-files: couldn't `cd %s'"
2746 (tramp-shell-quote-argument localname))
2748 multi-method method user host
2749 (concat (tramp-get-ls-command multi-method method user host)
2751 (tramp-wait-for-output)
2752 (goto-char (point-max))
2753 (while (zerop (forward-line -1))
2754 (setq x (buffer-substring (point)
2755 (tramp-line-end-position)))
2756 (when (or (not match) (string-match match x))
2758 (push (concat (file-name-as-directory directory)
2762 (tramp-send-command multi-method method user host "cd")
2763 (tramp-wait-for-output)
2764 ;; Remove non-files or non-directories if necessary. Using
2765 ;; the remote shell for this would probably be way faster.
2766 ;; Maybe something could be adapted from
2767 ;; tramp-handle-file-name-all-completions.
2769 (let ((temp (nreverse result))
2772 (if (equal files-only t)
2775 (setq item (pop temp))
2776 (when (file-regular-p item)
2777 (push item result)))
2780 (setq item (pop temp))
2781 (when (file-directory-p item)
2782 (push item result)))))))
2785 (defun tramp-handle-directory-files-and-attributes
2786 (directory &optional full match nosort id-format)
2787 "Like `directory-files-and-attributes' for tramp files."
2788 (when (tramp-handle-file-exists-p directory)
2790 (setq directory (tramp-handle-expand-file-name directory))
2791 (with-parsed-tramp-file-name directory nil
2792 (tramp-maybe-send-perl-script multi-method method user host
2793 tramp-perl-directory-files-and-attributes
2794 "tramp_directory_files_and_attributes")
2795 (tramp-send-command multi-method method user host
2796 (format "tramp_directory_files_and_attributes %s %s"
2797 (tramp-shell-quote-argument localname)
2798 (or id-format 'integer)))
2799 (tramp-wait-for-output)
2800 (let* ((root (cons nil (let ((object (read (current-buffer))))
2801 (when (stringp object)
2806 (if (and match (not (string-match match (caadr cell))))
2808 (setcdr cell (cddr cell))
2810 (setq cell (cdr cell))
2811 (let ((l (car cell)))
2812 (tramp-convert-file-attributes multi-method method user host
2814 ;; If FULL, make file name absolute
2815 (when full (setcar l (concat directory "/" (car l)))))))
2818 (sort (cdr root) (lambda (x y) (string< (car x) (car y))))))))))
2820 ;; This function should return "foo/" for directories and "bar" for
2821 ;; files. We use `ls -ad' to get a list of files (including
2822 ;; directories), and `find . -type d \! -name . -prune' to get a list
2824 (defun tramp-handle-file-name-all-completions (filename directory)
2825 "Like `file-name-all-completions' for tramp files."
2826 (with-parsed-tramp-file-name directory nil
2827 (unless (save-match-data (string-match "/" filename))
2828 (let* ((nowild tramp-completion-without-shell-p)
2831 (tramp-barf-unless-okay
2832 multi-method method user host
2833 (format "cd %s" (tramp-shell-quote-argument localname))
2835 "tramp-handle-file-name-all-completions: Couldn't `cd %s'"
2836 (tramp-shell-quote-argument localname))
2838 ;; Get a list of directories and files, including reliably
2839 ;; tagging the directories with a trailing '/'. Because I
2840 ;; rock. --daniel@danann.net
2842 multi-method method user host
2843 (format (concat "%s -a %s 2>/dev/null | while read f; do "
2844 "if test -d \"$f\" 2>/dev/null; "
2845 "then echo \"$f/\"; else echo \"$f\"; fi; done")
2846 (tramp-get-ls-command multi-method method user host)
2847 (if (or nowild (zerop (length filename)))
2850 (tramp-shell-quote-argument filename)))))
2852 ;; Now grab the output.
2853 (tramp-wait-for-output)
2854 (goto-char (point-max))
2855 (while (zerop (forward-line -1))
2856 (push (buffer-substring (point)
2857 (tramp-line-end-position))
2860 (tramp-send-command multi-method method user host "cd")
2861 (tramp-wait-for-output)
2865 (all-completions filename (mapcar 'list result))
2869 ;; The following isn't needed for Emacs 20 but for 19.34?
2870 (defun tramp-handle-file-name-completion
2871 (filename directory &optional predicate)
2872 "Like `file-name-completion' for tramp files."
2873 (unless (tramp-tramp-file-p directory)
2875 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2879 (mapcar 'list (file-name-all-completions filename directory))
2881 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
2885 (defun tramp-handle-add-name-to-file
2886 (filename newname &optional ok-if-already-exists)
2887 "Like `add-name-to-file' for tramp files."
2888 (with-parsed-tramp-file-name filename v1
2889 (with-parsed-tramp-file-name newname v2
2890 (let ((ln (when v1 (tramp-get-remote-ln
2891 v1-multi-method v1-method v1-user v1-host))))
2892 (unless (and v1-method v2-method v1-user v2-user v1-host v2-host
2893 (equal v1-multi-method v2-multi-method)
2894 (equal v1-method v2-method)
2895 (equal v1-user v2-user)
2896 (equal v1-host v2-host))
2897 (error "add-name-to-file: %s"
2898 "only implemented for same method, same user, same host"))
2899 (when (and (not ok-if-already-exists)
2900 (file-exists-p newname)
2901 (not (numberp ok-if-already-exists))
2904 "File %s already exists; make it a new name anyway? "
2906 (error "add-name-to-file: file %s already exists" newname))
2907 (tramp-barf-unless-okay
2908 v1-multi-method v1-method v1-user v1-host
2909 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
2910 (tramp-shell-quote-argument v2-localname))
2912 "error with add-name-to-file, see buffer `%s' for details"
2915 (defun tramp-handle-copy-file
2916 (filename newname &optional ok-if-already-exists keep-date)
2917 "Like `copy-file' for tramp files."
2918 ;; Check if both files are local -- invoke normal copy-file.
2919 ;; Otherwise, use tramp from local system.
2920 (setq filename (expand-file-name filename))
2921 (setq newname (expand-file-name newname))
2922 ;; At least one file a tramp file?
2923 (if (or (tramp-tramp-file-p filename)
2924 (tramp-tramp-file-p newname))
2925 (tramp-do-copy-or-rename-file
2926 'copy filename newname ok-if-already-exists keep-date)
2927 (tramp-run-real-handler
2929 (list filename newname ok-if-already-exists keep-date))))
2931 (defun tramp-handle-rename-file
2932 (filename newname &optional ok-if-already-exists)
2933 "Like `rename-file' for tramp files."
2934 ;; Check if both files are local -- invoke normal rename-file.
2935 ;; Otherwise, use tramp from local system.
2936 (setq filename (expand-file-name filename))
2937 (setq newname (expand-file-name newname))
2938 ;; At least one file a tramp file?
2939 (if (or (tramp-tramp-file-p filename)
2940 (tramp-tramp-file-p newname))
2941 (tramp-do-copy-or-rename-file
2942 'rename filename newname ok-if-already-exists)
2943 (tramp-run-real-handler 'rename-file
2944 (list filename newname ok-if-already-exists))))
2946 (defun tramp-do-copy-or-rename-file
2947 (op filename newname &optional ok-if-already-exists keep-date)
2948 "Copy or rename a remote file.
2949 OP must be `copy' or `rename' and indicates the operation to perform.
2950 FILENAME specifies the file to copy or rename, NEWNAME is the name of
2951 the new file (for copy) or the new name of the file (for rename).
2952 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2953 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2956 This function is invoked by `tramp-handle-copy-file' and
2957 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
2958 and `rename'. FILENAME and NEWNAME must be absolute file names."
2959 (unless (memq op '(copy rename))
2960 (error "Unknown operation `%s', must be `copy' or `rename'" op))
2961 (unless ok-if-already-exists
2962 (when (file-exists-p newname)
2963 (signal 'file-already-exists
2964 (list "File already exists" newname))))
2965 (let ((t1 (tramp-tramp-file-p filename))
2966 (t2 (tramp-tramp-file-p newname))
2967 v1-multi-method v1-method v1-user v1-host v1-localname
2968 v2-multi-method v2-method v2-user v2-host v2-localname)
2970 ;; Check which ones of source and target are Tramp files.
2971 ;; We cannot invoke `with-parsed-tramp-file-name';
2972 ;; it fails if the file isn't a Tramp file name.
2974 (with-parsed-tramp-file-name filename l
2975 (setq v1-multi-method l-multi-method
2979 v1-localname l-localname))
2980 (setq v1-localname filename))
2982 (with-parsed-tramp-file-name newname l
2983 (setq v2-multi-method l-multi-method
2987 v2-localname l-localname))
2988 (setq v2-localname newname))
2991 ;; Both are Tramp files.
2994 ;; Shortcut: if method, host, user are the same for both
2995 ;; files, we invoke `cp' or `mv' on the remote host
2997 ((and (equal v1-multi-method v2-multi-method)
2998 (equal v1-method v2-method)
2999 (equal v1-user v2-user)
3000 (equal v1-host v2-host))
3001 (tramp-do-copy-or-rename-file-directly
3002 op v1-multi-method v1-method v1-user v1-host
3003 v1-localname v2-localname keep-date))
3004 ;; If both source and target are Tramp files,
3005 ;; both are using the same copy-program, then we
3006 ;; can invoke rcp directly. Note that
3007 ;; default-directory should point to a local
3008 ;; directory if we want to invoke rcp.
3009 ((and (not v1-multi-method)
3010 (not v2-multi-method)
3011 (equal v1-method v2-method)
3012 (tramp-method-out-of-band-p
3013 v1-multi-method v1-method v1-user v1-host)
3014 (not (string-match "\\([^#]*\\)#\\(.*\\)" v1-host))
3015 (not (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)))
3016 (tramp-do-copy-or-rename-file-out-of-band
3017 op filename newname keep-date))
3018 ;; No shortcut was possible. So we copy the
3019 ;; file first. If the operation was `rename', we go
3020 ;; back and delete the original file (if the copy was
3021 ;; successful). The approach is simple-minded: we
3022 ;; create a new buffer, insert the contents of the
3023 ;; source file into it, then write out the buffer to
3024 ;; the target file. The advantage is that it doesn't
3025 ;; matter which filename handlers are used for the
3026 ;; source and target file.
3028 (tramp-do-copy-or-rename-file-via-buffer
3029 op filename newname keep-date))))
3031 ;; One file is a Tramp file, the other one is local.
3033 ;; If the Tramp file has an out-of-band method, the corresponding
3034 ;; copy-program can be invoked.
3035 (if (and (not v1-multi-method)
3036 (not v2-multi-method)
3037 (or (and t1 (tramp-method-out-of-band-p
3038 v1-multi-method v1-method v1-user v1-host))
3039 (and t2 (tramp-method-out-of-band-p
3040 v2-multi-method v2-method v2-user v2-host))))
3041 (tramp-do-copy-or-rename-file-out-of-band
3042 op filename newname keep-date)
3043 ;; Use the generic method via a Tramp buffer.
3044 (tramp-do-copy-or-rename-file-via-buffer
3045 op filename newname keep-date)))
3048 ;; One of them must be a Tramp file.
3049 (error "Tramp implementation says this cannot happen")))))
3051 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
3052 "Use an Emacs buffer to copy or rename a file.
3053 First arg OP is either `copy' or `rename' and indicates the operation.
3054 FILENAME is the source file, NEWNAME the target file.
3055 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
3056 (let ((trampbuf (get-buffer-create "*tramp output*"))
3057 (modtime (nth 5 (file-attributes filename))))
3058 (when (and keep-date (or (null modtime) (equal modtime '(0 0))))
3060 1 (concat "Warning: cannot preserve file time stamp"
3061 " with inline copying across machines")))
3063 (set-buffer trampbuf) (erase-buffer)
3064 (insert-file-contents-literally filename)
3065 ;; We don't want the target file to be compressed, so we let-bind
3066 ;; `jka-compr-inhibit' to t.
3067 (let ((coding-system-for-write 'binary)
3068 (jka-compr-inhibit t))
3069 (write-region (point-min) (point-max) newname))
3070 ;; KEEP-DATE handling.
3072 (when (and (not (null modtime))
3073 (not (equal modtime '(0 0))))
3074 (tramp-touch newname modtime)))
3076 (set-file-modes newname (file-modes filename)))
3077 ;; If the operation was `rename', delete the original file.
3078 (unless (eq op 'copy)
3079 (delete-file filename))))
3081 (defun tramp-do-copy-or-rename-file-directly
3082 (op multi-method method user host localname1 localname2 keep-date)
3083 "Invokes `cp' or `mv' on the remote system.
3084 OP must be one of `copy' or `rename', indicating `cp' or `mv',
3085 respectively. METHOD, USER, and HOST specify the connection.
3086 LOCALNAME1 and LOCALNAME2 specify the two arguments of `cp' or `mv'.
3087 If KEEP-DATE is non-nil, preserve the time stamp when copying."
3088 ;; CCC: What happens to the timestamp when renaming?
3089 (let ((cmd (cond ((and (eq op 'copy) keep-date) "cp -f -p")
3090 ((eq op 'copy) "cp -f")
3091 ((eq op 'rename) "mv -f")
3093 "Unknown operation `%s', must be `copy' or `rename'"
3097 multi-method method user host
3100 (tramp-shell-quote-argument localname1)
3101 (tramp-shell-quote-argument localname2)))
3102 (tramp-wait-for-output)
3103 (goto-char (point-min))
3106 (and (eq op 'copy) keep-date
3107 ;; Mask cp -f error.
3108 (re-search-forward tramp-operation-not-permitted-regexp nil t))
3109 (zerop (tramp-send-command-and-check
3110 multi-method method user host nil nil)))
3111 (pop-to-buffer (current-buffer))
3113 (format "Copying directly failed, see buffer `%s' for details."
3116 ;; CCC: Maybe `chmod --reference=localname1 localname2' could be used
3118 (unless (or (eq op 'rename) keep-date)
3120 (tramp-make-tramp-file-name multi-method method user host localname2)
3122 (tramp-make-tramp-file-name
3123 multi-method method user host localname1))))))
3125 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
3126 "Invoke rcp program to copy.
3127 One of FILENAME and NEWNAME must be a Tramp name, the other must
3128 be a local filename. The method used must be an out-of-band method."
3129 (let ((t1 (tramp-tramp-file-p filename))
3130 (t2 (tramp-tramp-file-p newname))
3131 v1-multi-method v1-method v1-user v1-host v1-localname
3132 v2-multi-method v2-method v2-user v2-host v2-localname
3133 multi-method method user host copy-program copy-args
3134 source target trampbuf)
3136 ;; Check which ones of source and target are Tramp files.
3137 ;; We cannot invoke `with-parsed-tramp-file-name';
3138 ;; it fails if the file isn't a Tramp file name.
3140 (with-parsed-tramp-file-name filename l
3141 (setq v1-multi-method l-multi-method
3145 v1-localname l-localname
3146 multi-method l-multi-method
3147 method (tramp-find-method
3148 v1-multi-method v1-method v1-user v1-host)
3151 copy-program (tramp-get-method-parameter
3152 v1-multi-method method
3153 v1-user v1-host 'tramp-copy-program)
3154 copy-args (tramp-get-method-parameter
3155 v1-multi-method method
3156 v1-user v1-host 'tramp-copy-args)))
3157 (setq v1-localname filename))
3160 (with-parsed-tramp-file-name newname l
3161 (setq v2-multi-method l-multi-method
3165 v2-localname l-localname
3166 multi-method l-multi-method
3167 method (tramp-find-method
3168 v2-multi-method v2-method v2-user v2-host)
3171 copy-program (tramp-get-method-parameter
3172 v2-multi-method method
3173 v2-user v2-host 'tramp-copy-program)
3174 copy-args (tramp-get-method-parameter
3175 v2-multi-method method
3176 v2-user v2-host 'tramp-copy-args)))
3177 (setq v2-localname newname))
3179 ;; The following should be changed. We need a more general
3180 ;; mechanism to parse extra host args.
3182 (setq source v1-localname)
3183 (when (string-match "\\([^#]*\\)#\\(.*\\)" v1-host)
3184 (setq copy-args (cons "-P" (cons (match-string 2 v1-host) copy-args)))
3185 (setq v1-host (match-string 1 v1-host)))
3187 (tramp-make-copy-program-file-name
3189 (tramp-shell-quote-argument v1-localname))))
3192 (setq target v2-localname)
3193 (when (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)
3194 (setq copy-args (cons "-P" (cons (match-string 2 v2-host) copy-args)))
3195 (setq v2-host (match-string 1 v2-host)))
3197 (tramp-make-copy-program-file-name
3199 (tramp-shell-quote-argument v2-localname))))
3201 ;; Handle ControlMaster/ControlPath
3206 x `((?t . ,(format "/tmp/%s" tramp-temp-name-prefix)))))
3209 ;; Handle keep-date argument
3213 (cons (tramp-get-method-parameter
3214 v1-multi-method method
3215 v1-user v1-host 'tramp-copy-keep-date-arg)
3218 (cons (tramp-get-method-parameter
3219 v2-multi-method method
3220 v2-user v2-host 'tramp-copy-keep-date-arg)
3223 (setq copy-args (append copy-args (list source target))
3224 trampbuf (generate-new-buffer
3225 (tramp-buffer-name multi-method method user host)))
3227 ;; Use an asynchronous process. By this, password can be handled.
3230 ;; Check for program.
3231 (when (and (fboundp 'executable-find)
3232 (not (executable-find copy-program)))
3233 (error "Cannot find copy program: %s" copy-program))
3235 (set-buffer trampbuf)
3236 (setq tramp-current-multi-method multi-method
3237 tramp-current-method method
3238 tramp-current-user user
3239 tramp-current-host host)
3240 (message "Transferring %s to %s..." filename newname)
3242 ;; Use rcp-like program for file transfer.
3244 (let ((p (apply 'start-process (buffer-name trampbuf) trampbuf
3245 copy-program copy-args)))
3246 (tramp-set-process-query-on-exit-flag p nil)
3247 (tramp-process-actions p multi-method method user host
3248 tramp-actions-copy-out-of-band))
3249 (kill-buffer trampbuf))
3250 (message "Transferring %s to %s...done" filename newname)
3254 (set-file-modes newname (file-modes filename))))
3256 ;; If the operation was `rename', delete the original file.
3257 (unless (eq op 'copy)
3258 (delete-file filename))))
3261 (defun tramp-handle-make-directory (dir &optional parents)
3262 "Like `make-directory' for tramp files."
3263 (setq dir (expand-file-name dir))
3264 (with-parsed-tramp-file-name dir nil
3266 (tramp-barf-unless-okay
3267 multi-method method user host
3269 (if parents "mkdir -p" "mkdir")
3270 (tramp-shell-quote-argument localname))
3272 "Couldn't make directory %s" dir))))
3274 ;; CCC error checking?
3275 (defun tramp-handle-delete-directory (directory)
3276 "Like `delete-directory' for tramp files."
3277 (setq directory (expand-file-name directory))
3278 (with-parsed-tramp-file-name directory nil
3281 multi-method method user host
3282 (format "rmdir %s ; echo ok"
3283 (tramp-shell-quote-argument localname)))
3284 (tramp-wait-for-output))))
3286 (defun tramp-handle-delete-file (filename)
3287 "Like `delete-file' for tramp files."
3288 (setq filename (expand-file-name filename))
3289 (with-parsed-tramp-file-name filename nil
3291 (unless (zerop (tramp-send-command-and-check
3292 multi-method method user host
3294 (tramp-shell-quote-argument localname))))
3295 (signal 'file-error "Couldn't delete Tramp file")))))
3299 ;; CCC: This does not seem to be enough. Something dies when
3300 ;; we try and delete two directories under TRAMP :/
3301 (defun tramp-handle-dired-recursive-delete-directory (filename)
3302 "Recursively delete the directory given.
3303 This is like `dired-recursive-delete-directory' for tramp files."
3304 (with-parsed-tramp-file-name filename nil
3305 ;; run a shell command 'rm -r <localname>'
3306 ;; Code shamelessly stolen for the dired implementation and, um, hacked :)
3307 (or (file-exists-p filename)
3310 (list "Removing old file name" "no such directory" filename)))
3311 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
3312 (tramp-send-command multi-method method user host
3313 (format "rm -r %s" (tramp-shell-quote-argument localname)))
3314 ;; Wait for the remote system to return to us...
3315 ;; This might take a while, allow it plenty of time.
3316 (tramp-wait-for-output 120)
3317 ;; Make sure that it worked...
3318 (and (file-exists-p filename)
3319 (error "Failed to recursively delete %s" filename))))
3321 (defun tramp-handle-dired-call-process (program discard &rest arguments)
3322 "Like `dired-call-process' for tramp files."
3323 (with-parsed-tramp-file-name default-directory nil
3325 (tramp-barf-unless-okay
3326 multi-method method user host
3327 (format "cd %s" (tramp-shell-quote-argument localname))
3329 "tramp-handle-dired-call-process: Couldn't `cd %s'"
3330 (tramp-shell-quote-argument localname))
3332 multi-method method user host
3333 (mapconcat #'tramp-shell-quote-argument (cons program arguments) " "))
3334 (tramp-wait-for-output))
3336 ;; We cannot use `insert-buffer' because the tramp buffer
3337 ;; changes its contents before insertion due to calling
3338 ;; `expand-file' and alike.
3340 (with-current-buffer
3341 (tramp-get-buffer multi-method method user host)
3345 (tramp-send-command-and-check multi-method method user host nil)
3346 (tramp-send-command multi-method method user host "cd")
3347 (tramp-wait-for-output)))))
3349 (defun tramp-handle-dired-compress-file (file &rest ok-flag)
3350 "Like `dired-compress-file' for tramp files."
3351 ;; OK-FLAG is valid for XEmacs only, but not implemented.
3352 ;; Code stolen mainly from dired-aux.el.
3353 (with-parsed-tramp-file-name file nil
3356 (if (not (featurep 'xemacs))
3358 (symbol-value 'dired-compress-file-suffixes)
3359 ;; XEmacs has `dired-compression-method-alist', which is
3360 ;; transformed into `dired-compress-file-suffixes' structure.
3363 (list (concat (regexp-quote (nth 1 x)) "\\'")
3365 (mapconcat 'identity (nth 3 x) " ")))
3366 (symbol-value 'dired-compression-method-alist))))
3368 ;; See if any suffix rule matches this file name.
3370 (let (case-fold-search)
3371 (if (string-match (car (car suffixes)) localname)
3372 (setq suffix (car suffixes) suffixes nil))
3373 (setq suffixes (cdr suffixes))))
3375 (cond ((file-symlink-p file)
3377 ((and suffix (nth 2 suffix))
3378 ;; We found an uncompression rule.
3379 (message "Uncompressing %s..." file)
3380 (when (zerop (tramp-send-command-and-check
3381 multi-method method user host
3382 (concat (nth 2 suffix) " " localname)))
3383 (message "Uncompressing %s...done" file)
3384 ;; `dired-remove-file' is not defined in XEmacs
3385 (funcall (symbol-function 'dired-remove-file) file)
3386 (string-match (car suffix) file)
3387 (concat (substring file 0 (match-beginning 0)))))
3389 ;; We don't recognize the file as compressed, so compress it.
3391 (message "Compressing %s..." file)
3392 (when (zerop (tramp-send-command-and-check
3393 multi-method method user host
3394 (concat "gzip -f " localname)))
3395 (message "Compressing %s...done" file)
3396 ;; `dired-remove-file' is not defined in XEmacs
3397 (funcall (symbol-function 'dired-remove-file) file)
3398 (cond ((file-exists-p (concat file ".gz"))
3399 (concat file ".gz"))
3400 ((file-exists-p (concat file ".z"))
3404 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
3405 ;; not sure at all that this is the right way to do it, but let's hope
3406 ;; it works for now, and wait for a guru to point out the Right Way to
3408 ;;(eval-when-compile
3409 ;; (unless (fboundp 'dired-insert-set-properties)
3410 ;; (fset 'dired-insert-set-properties 'ignore)))
3411 ;; Gerd suggests this:
3412 (eval-when-compile (require 'dired))
3413 ;; Note that dired is required at run-time, too, when it is needed.
3414 ;; It is only needed on XEmacs for the function
3415 ;; `dired-insert-set-properties'.
3417 (defun tramp-handle-insert-directory
3418 (filename switches &optional wildcard full-directory-p)
3419 "Like `insert-directory' for tramp files."
3420 (if (and (boundp 'ls-lisp-use-insert-directory-program)
3421 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
3422 (tramp-run-real-handler 'insert-directory
3423 (list filename switches wildcard full-directory-p))
3424 ;; For the moment, we assume that the remote "ls" program does not
3425 ;; grok "--dired". In the future, we should detect this on
3426 ;; connection setup.
3427 (when (string-match "^--dired\\s-+" switches)
3428 (setq switches (replace-match "" nil t switches)))
3429 (setq filename (expand-file-name filename))
3430 (with-parsed-tramp-file-name filename nil
3431 (tramp-message-for-buffer
3432 multi-method method user host 10
3433 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
3434 switches filename (if wildcard "yes" "no")
3435 (if full-directory-p "yes" "no"))
3437 (setq wildcard (file-name-nondirectory localname))
3438 (setq localname (file-name-directory localname)))
3439 (when (listp switches)
3440 (setq switches (mapconcat 'identity switches " ")))
3441 (unless full-directory-p
3442 (setq switches (concat "-d " switches)))
3444 (setq switches (concat switches " " wildcard)))
3446 ;; If `full-directory-p', we just say `ls -l FILENAME'.
3447 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
3448 (if full-directory-p
3450 multi-method method user host
3452 (tramp-get-ls-command multi-method method user host)
3456 (tramp-shell-quote-argument (concat localname ".")))))
3457 (tramp-barf-unless-okay
3458 multi-method method user host
3459 (format "cd %s" (tramp-shell-quote-argument
3460 (file-name-directory localname)))
3463 (tramp-shell-quote-argument (file-name-directory localname)))
3465 multi-method method user host
3467 (tramp-get-ls-command multi-method method user host)
3471 (if (zerop (length (file-name-nondirectory localname)))
3473 (tramp-shell-quote-argument
3474 (file-name-nondirectory localname)))))))
3475 (sit-for 1) ;needed for rsh but not ssh?
3476 (tramp-wait-for-output))
3477 ;; The following let-binding is used by code that's commented
3478 ;; out. Let's leave the let-binding in for a while to see
3479 ;; that the commented-out code is really not needed. Commenting-out
3480 ;; happened on 2003-03-13.
3481 (let ((old-pos (point)))
3482 ;; We cannot use `insert-buffer' because the tramp buffer
3483 ;; changes its contents before insertion due to calling
3484 ;; `expand-file' and alike.
3486 (with-current-buffer
3487 (tramp-get-buffer multi-method method user host)
3489 ;; On XEmacs, we want to call (exchange-point-and-mark t), but
3490 ;; that doesn't exist on Emacs, so we use this workaround instead.
3491 ;; Since zmacs-region-stays doesn't exist in Emacs, this ought to
3492 ;; be safe. Thanks to Daniel Pittman <daniel@danann.net>.
3493 ;; (let ((zmacs-region-stays t))
3494 ;; (exchange-point-and-mark))
3496 (tramp-send-command multi-method method user host "cd")
3497 (tramp-wait-for-output))
3498 ;; For the time being, the XEmacs kludge is commented out.
3499 ;; Please test it on various XEmacs versions to see if it works.
3500 ;; ;; Another XEmacs specialty follows. What's the right way to do
3502 ;; (when (and (featurep 'xemacs)
3503 ;; (eq major-mode 'dired-mode))
3506 ;; (dired-insert-set-properties old-pos (point))))
3509 ;; Continuation of kluge to pacify byte-compiler.
3510 ;;(eval-when-compile
3511 ;; (when (eq (symbol-function 'dired-insert-set-properties) 'ignore)
3512 ;; (fmakunbound 'dired-insert-set-properties)))
3514 ;; CCC is this the right thing to do?
3515 (defun tramp-handle-unhandled-file-name-directory (filename)
3516 "Like `unhandled-file-name-directory' for tramp files."
3517 (with-parsed-tramp-file-name filename nil
3518 (expand-file-name "~/")))
3520 ;; Canonicalization of file names.
3522 (defun tramp-drop-volume-letter (name)
3523 "Cut off unnecessary drive letter from file NAME.
3524 The function `tramp-handle-expand-file-name' calls `expand-file-name'
3525 locally on a remote file name. When the local system is a W32 system
3526 but the remote system is Unix, this introduces a superfluous drive
3527 letter into the file name. This function removes it.
3529 Doesn't do anything if the NAME does not start with a drive letter."
3530 (if (and (> (length name) 1)
3531 (char-equal (aref name 1) ?:)
3532 (let ((c1 (aref name 0)))
3533 (or (and (>= c1 ?A) (<= c1 ?Z))
3534 (and (>= c1 ?a) (<= c1 ?z)))))
3538 (defun tramp-handle-expand-file-name (name &optional dir)
3539 "Like `expand-file-name' for tramp files.
3540 If the localname part of the given filename starts with \"/../\" then
3541 the result will be a local, non-Tramp, filename."
3542 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
3543 (setq dir (or dir default-directory "/"))
3544 ;; Unless NAME is absolute, concat DIR and NAME.
3545 (unless (file-name-absolute-p name)
3546 (setq name (concat (file-name-as-directory dir) name)))
3547 ;; If NAME is not a tramp file, run the real handler
3548 (if (not (tramp-tramp-file-p name))
3549 (tramp-run-real-handler 'expand-file-name
3552 (with-parsed-tramp-file-name name nil
3553 (unless (file-name-absolute-p localname)
3554 (setq localname (concat "~/" localname)))
3556 ;; Tilde expansion if necessary. This needs a shell which
3557 ;; groks tilde expansion! The function `tramp-find-shell' is
3558 ;; supposed to find such a shell on the remote host. Please
3559 ;; tell me about it when this doesn't work on your system.
3560 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
3561 (let ((uname (match-string 1 localname))
3562 (fname (match-string 2 localname)))
3563 ;; We cannot simply apply "~/", because under sudo "~/" is
3564 ;; expanded to the local user home directory but to the
3565 ;; root home directory. On the other hand, using always
3566 ;; the default user name for tilde expansion is not
3567 ;; appropriate either, because ssh and companions might
3568 ;; use a user name from the config file.
3569 (when (and (string-equal uname "~")
3572 (tramp-find-method multi-method method user host)))
3573 (setq uname (concat uname (or user "root"))))
3574 ;; CCC fanatic error checking?
3575 (set-buffer (tramp-get-buffer multi-method method user host))
3578 multi-method method user host
3579 (format "cd %s; pwd" uname)
3581 (tramp-wait-for-output)
3582 (goto-char (point-min))
3583 (setq uname (buffer-substring (point) (tramp-line-end-position)))
3584 (setq localname (concat uname fname))
3586 ;; There might be a double slash, for example when "~/"
3587 ;; expands to "/". Remove this.
3588 (while (string-match "//" localname)
3589 (setq localname (replace-match "/" t t localname)))
3590 ;; No tilde characters in file name, do normal
3591 ;; expand-file-name (this does "/./" and "/../"). We bind
3592 ;; directory-sep-char here for XEmacs on Windows, which would
3593 ;; otherwise use backslash. `default-directory' is bound to
3594 ;; "/", because on Windows there would be problems with UNC
3595 ;; shares or Cygwin mounts.
3596 (tramp-let-maybe directory-sep-char ?/
3597 (let ((default-directory "/"))
3598 (tramp-make-tramp-file-name
3599 multi-method (or method (tramp-find-default-method user host))
3601 (tramp-drop-volume-letter
3602 (tramp-run-real-handler 'expand-file-name
3603 (list localname))))))))))
3605 ;; old version follows. it uses ".." to cross file handler
3607 ;; ;; Look if localname starts with "/../" construct. If this is
3608 ;; ;; the case, then we return a local name instead of a remote name.
3609 ;; (if (string-match "^/\\.\\./" localname)
3610 ;; (expand-file-name (substring localname 3))
3611 ;; ;; No tilde characters in file name, do normal
3612 ;; ;; expand-file-name (this does "/./" and "/../"). We bind
3613 ;; ;; directory-sep-char here for XEmacs on Windows, which
3614 ;; ;; would otherwise use backslash.
3615 ;; (let ((directory-sep-char ?/))
3616 ;; (tramp-make-tramp-file-name
3617 ;; multi-method method user host
3618 ;; (tramp-drop-volume-letter
3619 ;; (tramp-run-real-handler 'expand-file-name
3620 ;; (list localname))))))))))
3624 (defvar tramp-async-proc nil
3625 "Global variable keeping asynchronous process object.
3626 Used in `tramp-handle-shell-command'")
3628 (defvar tramp-display-shell-command-buffer t
3629 "Whether to display output buffer of `shell-command'.
3630 This is necessary for handling DISPLAY of `process-file'.")
3632 (defun tramp-handle-shell-command (command &optional output-buffer error-buffer)
3633 "Like `shell-command' for tramp files.
3634 This will break if COMMAND prints a newline, followed by the value of
3635 `tramp-end-of-output', followed by another newline."
3636 ;; Asynchronous processes are far from being perfect. But it works at least
3637 ;; for `find-grep-dired' and `find-name-dired' in Emacs 22.
3638 (if (tramp-tramp-file-p default-directory)
3639 (with-parsed-tramp-file-name default-directory nil
3640 (let ((curbuf (current-buffer))
3641 (asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
3643 (unless output-buffer
3647 "*Async Shell Command*"
3648 "*Shell Command Output*")))
3649 (set-buffer output-buffer)
3651 (unless (bufferp output-buffer)
3652 (setq output-buffer (current-buffer)))
3653 (set-buffer output-buffer)
3654 ;; Tramp doesn't handle the asynchronous case by an asynchronous
3655 ;; process. Instead of, another asynchronous process is opened
3656 ;; which gets the output of the (synchronous) Tramp process
3657 ;; via process-filter. ERROR-BUFFER is disabled.
3659 (setq command (substring command 0 (match-beginning 0))
3661 tramp-async-proc (start-process (buffer-name output-buffer)
3662 output-buffer "cat")))
3664 (tramp-barf-unless-okay
3665 multi-method method user host
3666 (format "cd %s" (tramp-shell-quote-argument localname))
3668 "tramp-handle-shell-command: Couldn't `cd %s'"
3669 (tramp-shell-quote-argument localname))
3670 ;; Define the process filter
3674 (tramp-get-buffer multi-method method user host))
3675 '(lambda (process string)
3676 ;; Write the output into the Tramp Process
3677 (save-current-buffer
3678 (set-buffer (process-buffer process))
3679 (goto-char (point-max))
3681 ;; Hand-over output to asynchronous process.
3684 (regexp-quote tramp-end-of-output) string)))
3687 (substring string 0 (1- (match-beginning 0)))))
3688 (process-send-string tramp-async-proc string)
3690 (set-process-filter process nil)
3691 (process-send-eof tramp-async-proc))))))
3694 multi-method method user host
3696 (format "( %s ) 2>/tmp/tramp.$$.err; tramp_old_status=$?"
3698 (format "%s; tramp_old_status=$?" command)))
3699 (unless asynchronous
3700 (tramp-wait-for-output)))
3701 (unless asynchronous
3702 ;; We cannot use `insert-buffer' because the tramp buffer
3703 ;; changes its contents before insertion due to calling
3704 ;; `expand-file' and alike.
3706 (with-current-buffer
3707 (tramp-get-buffer multi-method method user host)
3711 (unless (bufferp error-buffer)
3712 (setq error-buffer (get-buffer-create error-buffer)))
3714 multi-method method user host
3715 "cat /tmp/tramp.$$.err")
3716 (tramp-wait-for-output)
3717 (set-buffer error-buffer)
3718 ;; Same comment as above
3720 (with-current-buffer
3721 (tramp-get-buffer multi-method method user host)
3723 (tramp-send-command-and-check
3724 multi-method method user host "rm -f /tmp/tramp.$$.err")))
3726 (tramp-send-command multi-method method user host "cd")
3727 (unless asynchronous
3728 (tramp-wait-for-output))
3730 multi-method method user host
3731 (concat "tramp_set_exit_status $tramp_old_status;"
3732 " echo tramp_exit_status $?"))
3733 (unless asynchronous
3734 (tramp-wait-for-output)
3735 (goto-char (point-max))
3736 (unless (search-backward "tramp_exit_status " nil t)
3737 (error "Couldn't find exit status of `%s'" command))
3738 (skip-chars-forward "^ ")
3739 (setq status (read (current-buffer)))))
3740 (unless (zerop (buffer-size))
3741 (when tramp-display-shell-command-buffer
3742 (display-buffer output-buffer)))
3745 ;; The following is only executed if something strange was
3746 ;; happening. Emit a helpful message and do it anyway.
3747 (message "tramp-handle-shell-command called with non-tramp directory: `%s'"
3749 (tramp-run-real-handler 'shell-command
3750 (list command output-buffer error-buffer))))
3752 (defun tramp-handle-process-file (program &optional infile buffer display &rest args)
3753 "Like `process-file' for Tramp files."
3754 (when infile (error "Implementation does not handle input from file"))
3755 (when (and (numberp buffer) (zerop buffer))
3756 (error "Implementation does not handle immediate return"))
3757 (when (consp buffer) (error "Implementation does not handle error files"))
3758 (let ((tramp-display-shell-command-buffer display))
3760 (mapconcat 'tramp-shell-quote-argument (cons program args) " ")
3765 (defsubst tramp-make-temp-file ()
3766 (funcall (if (fboundp 'make-temp-file) 'make-temp-file 'make-temp-name)
3767 (expand-file-name tramp-temp-name-prefix
3768 (tramp-temporary-file-directory))))
3770 (defun tramp-handle-file-local-copy (filename)
3771 "Like `file-local-copy' for tramp files."
3772 (with-parsed-tramp-file-name filename nil
3773 (let ((tramp-buf (tramp-get-buffer multi-method method user host))
3774 ;; We used to bind the following as late as possible.
3775 ;; loc-enc and loc-dec were bound directly before the if
3776 ;; statement that checks them. But the functions
3777 ;; tramp-get-* might invoke the "are you awake" check in
3778 ;; tramp-maybe-open-connection, which is an unfortunate time
3779 ;; since we rely on the buffer contents at that spot.
3780 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3781 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3782 (loc-enc (tramp-get-local-encoding multi-method method user host))
3783 (loc-dec (tramp-get-local-decoding multi-method method user host))
3785 (unless (file-exists-p filename)
3786 (error "Cannot make local copy of non-existing file `%s'"
3788 (setq tmpfil (tramp-make-temp-file))
3790 (cond ((tramp-method-out-of-band-p multi-method method user host)
3791 ;; `copy-file' handles out-of-band methods
3792 (copy-file filename tmpfil t t))
3794 ((and rem-enc rem-dec)
3795 ;; Use inline encoding for file transfer.
3797 ;; Following line for setting tramp-current-method,
3798 ;; tramp-current-user, tramp-current-host.
3799 (set-buffer tramp-buf)
3800 (tramp-message 5 "Encoding remote file %s..." filename)
3801 (tramp-barf-unless-okay
3802 multi-method method user host
3803 (concat rem-enc " < " (tramp-shell-quote-argument localname))
3805 "Encoding remote file failed, see buffer `%s' for details"
3807 ;; Remove trailing status code
3808 (goto-char (point-max))
3809 (delete-region (point) (progn (forward-line -1) (point)))
3811 (tramp-message 5 "Decoding remote file %s..." filename)
3813 ;; Here is where loc-enc and loc-dec used to be let-bound.
3814 (if (and (symbolp loc-dec) (fboundp loc-dec))
3815 ;; If local decoding is a function, we call it.
3816 (let ((tmpbuf (get-buffer-create " *tramp tmp*")))
3819 (insert-buffer-substring tramp-buf)
3820 (tramp-message-for-buffer
3821 multi-method method user host
3822 6 "Decoding remote file %s with function %s..."
3825 ;; Douglas Gray Stephens <DGrayStephens@slb.com>
3826 ;; says that we need to strip tramp_exit_status
3827 ;; line from the output here. Go to point-max,
3828 ;; search backward for tramp_exit_status, delete
3829 ;; between point and point-max if found.
3830 (let ((coding-system-for-write 'binary))
3831 (funcall loc-dec (point-min) (point-max))
3832 (write-region (point-min) (point-max) tmpfil))
3833 (kill-buffer tmpbuf))
3834 ;; If tramp-decoding-function is not defined for this
3835 ;; method, we invoke tramp-decoding-command instead.
3836 (let ((tmpfil2 (tramp-make-temp-file)))
3837 (write-region (point-min) (point-max) tmpfil2)
3839 6 "Decoding remote file %s with command %s..."
3841 (tramp-call-local-coding-command
3842 loc-dec tmpfil2 tmpfil)
3843 (delete-file tmpfil2)))
3844 (tramp-message-for-buffer
3845 multi-method method user host
3846 5 "Decoding remote file %s...done" filename)
3847 ;; Set proper permissions.
3848 (set-file-modes tmpfil (file-modes filename))))
3850 (t (error "Wrong method specification for `%s'" method)))
3853 (defun tramp-handle-file-remote-p (filename)
3854 "Like `file-remote-p' for tramp files."
3855 (when (tramp-tramp-file-p filename)
3856 (with-parsed-tramp-file-name filename nil
3857 (make-tramp-file-name
3858 :multi-method multi-method
3864 (defun tramp-handle-insert-file-contents
3865 (filename &optional visit beg end replace)
3866 "Like `insert-file-contents' for tramp files."
3867 (barf-if-buffer-read-only)
3868 (setq filename (expand-file-name filename))
3869 (with-parsed-tramp-file-name filename nil
3870 (if (not (file-exists-p filename))
3873 (setq buffer-file-name filename)
3874 (set-visited-file-modtime)
3875 (set-buffer-modified-p nil))
3877 (format "File `%s' not found on remote host" filename))
3878 (list (expand-file-name filename) 0))
3879 ;; `insert-file-contents-literally' takes care to avoid calling
3880 ;; jka-compr. By let-binding inhibit-file-name-operation, we
3881 ;; propagate that care to the file-local-copy operation.
3883 (let ((inhibit-file-name-operation
3884 (when (eq inhibit-file-name-operation
3885 'insert-file-contents)
3887 (file-local-copy filename)))
3888 coding-system-used result)
3890 (setq buffer-file-name filename)
3891 (set-visited-file-modtime)
3892 (set-buffer-modified-p nil))
3893 (tramp-message-for-buffer
3894 multi-method method user host
3895 9 "Inserting local temp file `%s'..." local-copy)
3896 (setq result (insert-file-contents local-copy nil beg end replace))
3897 ;; Now `last-coding-system-used' has right value. Remember it.
3898 (when (boundp 'last-coding-system-used)
3899 (setq coding-system-used (symbol-value 'last-coding-system-used)))
3900 (tramp-message-for-buffer
3901 multi-method method user host
3902 9 "Inserting local temp file `%s'...done" local-copy)
3903 (delete-file local-copy)
3904 (when (boundp 'last-coding-system-used)
3905 (set 'last-coding-system-used coding-system-used))
3906 (list (expand-file-name filename)
3907 (second result))))))
3910 (defun tramp-handle-find-backup-file-name (filename)
3911 "Like `find-backup-file-name' for tramp files."
3912 (with-parsed-tramp-file-name filename nil
3913 ;; We set both variables. It doesn't matter whether it is
3915 (let ((backup-directory-alist
3917 (when (boundp 'backup-directory-alist)
3918 (if (boundp 'tramp-backup-directory-alist)
3923 (if (and (stringp (cdr x))
3924 (file-name-absolute-p (cdr x))
3925 (not (tramp-file-name-p (cdr x))))
3926 (tramp-make-tramp-file-name
3927 multi-method method user host (cdr x))
3929 (symbol-value 'tramp-backup-directory-alist))
3930 (symbol-value 'backup-directory-alist))))
3932 (bkup-backup-directory-info
3934 (when (boundp 'bkup-backup-directory-info)
3935 (if (boundp 'tramp-bkup-backup-directory-info)
3941 (if (and (stringp (car (cdr x)))
3942 (file-name-absolute-p (car (cdr x)))
3943 (not (tramp-file-name-p (car (cdr x)))))
3944 (tramp-make-tramp-file-name
3945 multi-method method user host (car (cdr x)))
3948 (symbol-value 'tramp-bkup-backup-directory-info))
3949 (symbol-value 'bkup-backup-directory-info)))))
3951 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
3953 (defun tramp-handle-make-auto-save-file-name ()
3954 "Like `make-auto-save-file-name' for tramp files.
3955 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
3956 (let ((tramp-auto-save-directory tramp-auto-save-directory))
3957 ;; File name must be unique. This is ensured with Emacs 22 (see
3958 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
3959 ;; all other cases we must do it ourselves.
3960 (when (boundp 'auto-save-file-name-transforms)
3963 (when (and (string-match (car x) buffer-file-name)
3964 (not (car (cddr x))))
3965 (setq tramp-auto-save-directory
3966 (or tramp-auto-save-directory temporary-file-directory))))
3967 (symbol-value 'auto-save-file-name-transforms)))
3968 ;; Create directory.
3969 (when tramp-auto-save-directory
3970 (unless (file-exists-p tramp-auto-save-directory)
3971 (make-directory tramp-auto-save-directory t)))
3972 ;; jka-compr doesn't like auto-saving, so by appending "~" to the
3973 ;; file name we make sure that jka-compr isn't used for the
3975 (let ((buffer-file-name
3976 (if tramp-auto-save-directory
3978 (tramp-subst-strs-in-string
3986 tramp-auto-save-directory)
3987 (buffer-file-name))))
3988 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3989 ;; it is not a magic file name operation (since Emacs 22).
3990 ;; We must deactivate it temporarily.
3991 (if (not (ad-is-active 'make-auto-save-file-name))
3992 (tramp-run-real-handler
3993 'make-auto-save-file-name nil)
3995 (ad-deactivate 'make-auto-save-file-name)
3997 (tramp-run-real-handler
3998 'make-auto-save-file-name nil)
3999 (ad-activate 'make-auto-save-file-name))))))
4002 ;; CCC grok APPEND, LOCKNAME, CONFIRM
4003 (defun tramp-handle-write-region
4004 (start end filename &optional append visit lockname confirm)
4005 "Like `write-region' for tramp files."
4006 (unless (eq append nil)
4007 (error "Cannot append to file using tramp (`%s')" filename))
4008 (setq filename (expand-file-name filename))
4009 ;; Following part commented out because we don't know what to do about
4010 ;; file locking, and it does not appear to be a problem to ignore it.
4011 ;; Ange-ftp ignores it, too.
4012 ;; (when (and lockname (stringp lockname))
4013 ;; (setq lockname (expand-file-name lockname)))
4014 ;; (unless (or (eq lockname nil)
4015 ;; (string= lockname filename))
4017 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
4018 ;; XEmacs takes a coding system as the seventh argument, not `confirm'
4019 (when (and (not (featurep 'xemacs))
4020 confirm (file-exists-p filename))
4021 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
4023 (error "File not overwritten")))
4024 (with-parsed-tramp-file-name filename nil
4025 (let ((curbuf (current-buffer))
4026 (rem-enc (tramp-get-remote-encoding multi-method method user host))
4027 (rem-dec (tramp-get-remote-decoding multi-method method user host))
4028 (loc-enc (tramp-get-local-encoding multi-method method user host))
4029 (loc-dec (tramp-get-local-decoding multi-method method user host))
4030 (trampbuf (get-buffer-create "*tramp output*"))
4031 (modes (file-modes filename))
4032 ;; We use this to save the value of `last-coding-system-used'
4033 ;; after writing the tmp file. At the end of the function,
4034 ;; we set `last-coding-system-used' to this saved value.
4035 ;; This way, any intermediary coding systems used while
4036 ;; talking to the remote shell or suchlike won't hose this
4037 ;; variable. This approach was snarfed from ange-ftp.el.
4040 ;; Write region into a tmp file. This isn't really needed if we
4041 ;; use an encoding function, but currently we use it always
4042 ;; because this makes the logic simpler.
4043 (setq tmpfil (tramp-make-temp-file))
4044 ;; Set current buffer. If connection wasn't open, `file-modes' has
4045 ;; changed it accidently.
4047 ;; We say `no-message' here because we don't want the visited file
4048 ;; modtime data to be clobbered from the temp file. We call
4049 ;; `set-visited-file-modtime' ourselves later on.
4050 (tramp-run-real-handler
4052 (if confirm ; don't pass this arg unless defined for backward compat.
4053 (list start end tmpfil append 'no-message lockname confirm)
4054 (list start end tmpfil append 'no-message lockname)))
4055 ;; Now, `last-coding-system-used' has the right value. Remember it.
4056 (when (boundp 'last-coding-system-used)
4057 (setq coding-system-used (symbol-value 'last-coding-system-used)))
4058 ;; The permissions of the temporary file should be set. If
4059 ;; filename does not exist (eq modes nil) it has been renamed to
4060 ;; the backup file. This case `save-buffer' handles
4062 (when modes (set-file-modes tmpfil modes))
4063 ;; This is a bit lengthy due to the different methods possible for
4064 ;; file transfer. First, we check whether the method uses an rcp
4065 ;; program. If so, we call it. Otherwise, both encoding and
4066 ;; decoding command must be specified. However, if the method
4067 ;; _also_ specifies an encoding function, then that is used for
4068 ;; encoding the contents of the tmp file.
4069 (cond ((tramp-method-out-of-band-p multi-method method user host)
4070 ;; `copy-file' handles out-of-band methods
4071 (copy-file tmpfil filename t t))
4073 ((and rem-enc rem-dec)
4074 ;; Use inline file transfer
4075 (let ((tmpbuf (get-buffer-create " *tramp file transfer*")))
4077 ;; Encode tmpfil into tmpbuf
4078 (tramp-message-for-buffer multi-method method user host
4079 5 "Encoding region...")
4082 ;; Use encoding function or command.
4083 (if (and (symbolp loc-enc) (fboundp loc-enc))
4085 (tramp-message-for-buffer
4086 multi-method method user host
4087 6 "Encoding region using function `%s'..."
4088 (symbol-name loc-enc))
4089 (insert-file-contents-literally tmpfil)
4090 ;; CCC. The following `let' is a workaround for
4091 ;; the base64.el that comes with pgnus-0.84. If
4092 ;; both of the following conditions are
4093 ;; satisfied, it tries to write to a local file
4094 ;; in default-directory, but at this point,
4095 ;; default-directory is remote.
4096 ;; (CALL-PROCESS-REGION can't write to remote
4097 ;; files, it seems.) The file in question is a
4099 (let ((default-directory
4100 (tramp-temporary-file-directory)))
4101 (funcall loc-enc (point-min) (point-max)))
4102 (goto-char (point-max))
4105 (tramp-message-for-buffer
4106 multi-method method user host
4107 6 "Encoding region using command `%s'..." loc-enc)
4108 (unless (equal 0 (tramp-call-local-coding-command
4110 (pop-to-buffer trampbuf)
4111 (error (concat "Cannot write to `%s', local encoding"
4112 " command `%s' failed")
4114 ;; Send tmpbuf into remote decoding command which
4115 ;; writes to remote file. Because this happens on the
4116 ;; remote host, we cannot use the function.
4117 (tramp-message-for-buffer
4118 multi-method method user host
4119 5 "Decoding region into remote file %s..." filename)
4121 multi-method method user host
4122 (format "%s >%s <<'EOF'"
4124 (tramp-shell-quote-argument localname)))
4126 (tramp-message-for-buffer
4127 multi-method method user host
4128 6 "Sending data to remote host...")
4129 (tramp-send-string multi-method method user host
4131 ;; wait for remote decoding to complete
4132 (tramp-message-for-buffer
4133 multi-method method user host
4134 6 "Sending end of data token...")
4136 multi-method method user host "EOF" nil t)
4137 (tramp-message-for-buffer
4138 multi-method method user host 6
4139 "Waiting for remote host to process data...")
4140 (set-buffer (tramp-get-buffer multi-method method user host))
4141 (tramp-wait-for-output)
4142 (tramp-barf-unless-okay
4143 multi-method method user host nil nil 'file-error
4144 (concat "Couldn't write region to `%s',"
4145 " decode using `%s' failed")
4147 (tramp-message 5 "Decoding region into remote file %s...done"
4149 (kill-buffer tmpbuf))))
4152 (concat "Method `%s' should specify both encoding and "
4153 "decoding command or an rcp program")
4155 (delete-file tmpfil)
4156 (unless (equal curbuf (current-buffer))
4157 (error "Buffer has changed from `%s' to `%s'"
4158 curbuf (current-buffer)))
4159 (when (or (eq visit t) (stringp visit))
4160 (set-visited-file-modtime
4161 ;; We must pass modtime explicitely, because filename can be different
4162 ;; from (buffer-file-name), f.e. if `file-precious-flag' is set.
4163 (nth 5 (file-attributes filename))))
4164 ;; Make `last-coding-system-used' have the right value.
4165 (when (boundp 'last-coding-system-used)
4166 (set 'last-coding-system-used coding-system-used))
4167 (when (or (eq visit t)
4170 (message "Wrote %s" filename)))))
4172 ;; Call down to the real handler.
4173 ;; Because EFS does not play nicely with TRAMP (both systems match a
4174 ;; TRAMP file name) it is needed to disable efs as well as tramp for the
4177 ;; Other than that, this is the canon file-handler code that the doco
4178 ;; says should be used here. Which is nice.
4180 ;; Under XEmacs current, EFS also hooks in as
4181 ;; efs-sifn-handler-function to handle any filename with environment
4182 ;; variables. This has two implications:
4183 ;; 1) That EFS may not be completely dead (yet) for TRAMP filenames
4184 ;; 2) That TRAMP might want to do the same thing.
4185 ;; Details as they come in.
4187 ;; Daniel Pittman <daniel@danann.net>
4189 ;; (defun tramp-run-real-handler (operation args)
4190 ;; "Invoke normal file name handler for OPERATION.
4191 ;; This inhibits EFS and Ange-FTP, too, because they conflict with tramp.
4192 ;; First arg specifies the OPERATION, remaining ARGS are passed to the
4194 ;; (let ((inhibit-file-name-handlers
4195 ;; (list 'tramp-file-name-handler
4196 ;; 'efs-file-handler-function
4197 ;; 'ange-ftp-hook-function
4198 ;; (and (eq inhibit-file-name-operation operation)
4199 ;; inhibit-file-name-handlers)))
4200 ;; (inhibit-file-name-operation operation))
4201 ;; (apply operation args)))
4204 (progn (defun tramp-run-real-handler (operation args)
4205 "Invoke normal file name handler for OPERATION.
4206 First arg specifies the OPERATION, second arg is a list of arguments to
4207 pass to the OPERATION."
4208 (let* ((inhibit-file-name-handlers
4209 `(tramp-file-name-handler
4210 tramp-completion-file-name-handler
4211 cygwin-mount-name-hook-function
4212 cygwin-mount-map-drive-hook-function
4214 ,(and (eq inhibit-file-name-operation operation)
4215 inhibit-file-name-handlers)))
4216 (inhibit-file-name-operation operation))
4217 (apply operation args))))
4219 ;; This function is used from `tramp-completion-file-name-handler' functions
4220 ;; only, if `tramp-completion-mode' is true. But this cannot be checked here
4221 ;; because the check is based on a full filename, not available for all
4222 ;; basic I/O operations.
4224 (progn (defun tramp-completion-run-real-handler (operation args)
4225 "Invoke `tramp-file-name-handler' for OPERATION.
4226 First arg specifies the OPERATION, second arg is a list of arguments to
4227 pass to the OPERATION."
4228 (let* ((inhibit-file-name-handlers
4229 `(tramp-completion-file-name-handler
4230 cygwin-mount-name-hook-function
4231 cygwin-mount-map-drive-hook-function
4233 ,(and (eq inhibit-file-name-operation operation)
4234 inhibit-file-name-handlers)))
4235 (inhibit-file-name-operation operation))
4236 (apply operation args))))
4238 ;; We handle here all file primitives. Most of them have the file
4239 ;; name as first parameter; nevertheless we check for them explicitly
4240 ;; in order to be signalled if a new primitive appears. This
4241 ;; scenario is needed because there isn't a way to decide by
4242 ;; syntactical means whether a foreign method must be called. It would
4243 ;; ease the life if `file-name-handler-alist' would support a decision
4244 ;; function as well but regexp only.
4245 (defun tramp-file-name-for-operation (operation &rest args)
4246 "Return file name related to OPERATION file primitive.
4247 ARGS are the arguments OPERATION has been called with."
4249 ; FILE resp DIRECTORY
4251 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
4252 'delete-file 'diff-latest-backup-file 'directory-file-name
4253 'directory-files 'directory-files-and-attributes
4254 'dired-compress-file 'dired-uncache
4255 'file-accessible-directory-p 'file-attributes
4256 'file-directory-p 'file-executable-p 'file-exists-p
4257 'file-local-copy 'file-remote-p 'file-modes
4258 'file-name-as-directory 'file-name-directory
4259 'file-name-nondirectory 'file-name-sans-versions
4260 'file-ownership-preserved-p 'file-readable-p
4261 'file-regular-p 'file-symlink-p 'file-truename
4262 'file-writable-p 'find-backup-file-name 'find-file-noselect
4263 'get-file-buffer 'insert-directory 'insert-file-contents
4264 'load 'make-directory 'make-directory-internal
4265 'set-file-modes 'substitute-in-file-name
4266 'unhandled-file-name-directory 'vc-registered
4268 'abbreviate-file-name 'create-file-buffer
4269 'dired-file-modtime 'dired-make-compressed-filename
4270 'dired-recursive-delete-directory 'dired-set-file-modtime
4271 'dired-shell-unhandle-file-name 'dired-uucode-file
4272 'insert-file-contents-literally 'recover-file
4273 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
4274 (if (file-name-absolute-p (nth 0 args))
4276 (expand-file-name (nth 0 args))))
4277 ; FILE DIRECTORY resp FILE1 FILE2
4279 (list 'add-name-to-file 'copy-file 'expand-file-name
4280 'file-name-all-completions 'file-name-completion
4281 'file-newer-than-file-p 'make-symbolic-link 'rename-file
4283 'dired-make-relative-symlink
4284 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
4287 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
4288 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
4289 (t (buffer-file-name (current-buffer))))))
4291 ((eq operation 'write-region)
4295 (list 'make-auto-save-file-name
4296 'set-visited-file-modtime 'verify-visited-file-modtime
4300 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
4303 (list 'dired-call-process
4309 'dired-print-file 'dired-shell-call-process))
4311 ; unknown file primitive
4312 (t (error "unknown file I/O primitive: %s" operation))))
4314 (defun tramp-find-foreign-file-name-handler (filename)
4315 "Return foreign file name handler if exists."
4316 (when (tramp-tramp-file-p filename)
4319 (handler-alist tramp-foreign-file-name-handler-alist))
4320 (while handler-alist
4321 (setq elt (car handler-alist)
4322 handler-alist (cdr handler-alist))
4323 (when (funcall (car elt) filename)
4324 (setq handler-alist nil)
4325 (setq res (cdr elt))))
4330 (defun tramp-file-name-handler (operation &rest args)
4331 "Invoke Tramp file name handler.
4332 Falls back to normal file name handler if no tramp file name handler exists."
4333 ;; (setq edebug-trace t)
4334 ;; (edebug-trace "%s" (with-output-to-string (backtrace)))
4336 (let* ((filename (apply 'tramp-file-name-for-operation operation args))
4337 (completion (tramp-completion-mode filename))
4338 (foreign (tramp-find-foreign-file-name-handler filename)))
4339 (with-parsed-tramp-file-name filename nil
4341 ;; When we are in completion mode, some operations shouldn' be
4342 ;; handled by backend.
4343 ((and completion (memq operation '(expand-file-name)))
4344 (tramp-run-real-handler operation args))
4345 ((and completion (zerop (length localname))
4346 (memq operation '(file-exists-p file-directory-p)))
4348 ;; Call the backend function.
4349 (foreign (apply foreign operation args))
4350 ;; Nothing to do for us.
4351 (t (tramp-run-real-handler operation args)))))))
4354 ;; In Emacs, there is some concurrency due to timers. If a timer
4355 ;; interrupts Tramp and wishes to use the same connection buffer as
4356 ;; the "main" Emacs, then garbage might occur in the connection
4357 ;; buffer. Therefore, we need to make sure that a timer does not use
4358 ;; the same connection buffer as the "main" Emacs. We implement a
4359 ;; cheap global lock, instead of locking each connection buffer
4360 ;; separately. The global lock is based on two variables,
4361 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
4362 ;; (with setq) to indicate a lock. But Tramp also calls itself during
4363 ;; processing of a single file operation, so we need to allow
4364 ;; recursive calls. That's where the `tramp-locker' variable comes in
4365 ;; -- it is let-bound to t during the execution of the current
4366 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
4367 ;; then we should just proceed because we have been called
4368 ;; recursively. But if `tramp-locker' is nil, then we are a timer
4369 ;; interrupting the "main" Emacs, and then we signal an error.
4371 (defvar tramp-locked nil
4372 "If non-nil, then Tramp is currently busy.
4373 Together with `tramp-locker', this implements a locking mechanism
4374 preventing reentrant calls of Tramp.")
4376 (defvar tramp-locker nil
4377 "If non-nil, then a caller has locked Tramp.
4378 Together with `tramp-locked', this implements a locking mechanism
4379 preventing reentrant calls of Tramp.")
4381 (defun tramp-sh-file-name-handler (operation &rest args)
4382 "Invoke remote-shell Tramp file name handler.
4383 Fall back to normal file name handler if no Tramp handler exists."
4384 (when (and tramp-locked (not tramp-locker))
4385 (signal 'file-error "Forbidden reentrant call of Tramp"))
4386 (let ((tl tramp-locked))
4389 (setq tramp-locked t)
4390 (let ((tramp-locker t))
4392 (let ((fn (assoc operation tramp-file-name-handler-alist)))
4394 (apply (cdr fn) args)
4395 (tramp-run-real-handler operation args))))))
4396 (setq tramp-locked tl))))
4399 (progn (defun tramp-completion-file-name-handler (operation &rest args)
4400 "Invoke tramp file name completion handler.
4401 Falls back to normal file name handler if no tramp file name handler exists."
4402 ;; (setq edebug-trace t)
4403 ;; (edebug-trace "%s" (with-output-to-string (backtrace)))
4404 (let ((fn (assoc operation tramp-completion-file-name-handler-alist)))
4406 (save-match-data (apply (cdr fn) args))
4407 (tramp-completion-run-real-handler operation args)))))
4410 (defsubst tramp-register-file-name-handlers ()
4411 "Add tramp file name handlers to `file-name-handler-alist'."
4412 (add-to-list 'file-name-handler-alist
4413 (cons tramp-file-name-regexp 'tramp-file-name-handler))
4414 ;; `partial-completion-mode' is unknown in XEmacs. So we should
4415 ;; load it unconditionally there. In the GNU Emacs case, method/
4416 ;; user/host name completion shall be bound to `partial-completion-mode'.
4417 (when (or (not (boundp 'partial-completion-mode))
4418 (symbol-value 'partial-completion-mode)
4420 (add-to-list 'file-name-handler-alist
4421 (cons tramp-completion-file-name-regexp
4422 'tramp-completion-file-name-handler))
4423 (put 'tramp-completion-file-name-handler 'safe-magic t))
4424 ;; If jka-compr is already loaded, move it to the front of
4425 ;; `file-name-handler-alist'.
4426 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist)))
4428 (setq file-name-handler-alist
4429 (cons jka (delete jka file-name-handler-alist))))))
4431 ;; During autoload, it shall be checked whether
4432 ;; `partial-completion-mode' is active. Therefore registering will be
4434 ;;;###autoload(add-hook
4435 ;;;###autoload 'after-init-hook
4436 ;;;###autoload '(lambda () (tramp-register-file-name-handlers)))
4437 (tramp-register-file-name-handlers)
4440 (defun tramp-unload-file-name-handlers ()
4441 (setq file-name-handler-alist
4442 (delete (rassoc 'tramp-file-name-handler
4443 file-name-handler-alist)
4444 (delete (rassoc 'tramp-completion-file-name-handler
4445 file-name-handler-alist)
4446 file-name-handler-alist))))
4448 (add-hook 'tramp-unload-hook 'tramp-unload-file-name-handlers)
4451 ;;; Interactions with other packages:
4453 ;; -- complete.el --
4455 ;; This function contributed by Ed Sabol
4456 (defun tramp-handle-expand-many-files (name)
4457 "Like `PC-expand-many-files' for tramp files."
4458 (with-parsed-tramp-file-name name nil
4460 (if (or (string-match "\\*" name)
4461 (string-match "\\?" name)
4462 (string-match "\\[.*\\]" name))
4465 ;; CCC: To do it right, we should quote certain characters
4466 ;; in the file name, but since the echo command is going to
4467 ;; break anyway when there are spaces in the file names, we
4469 ;;-(let ((comint-file-name-quote-list
4470 ;;- (set-difference tramp-file-name-quote-list
4471 ;;- '(?\* ?\? ?[ ?]))))
4472 ;;- (tramp-send-command
4473 ;;- multi-method method user host
4474 ;;- (format "echo %s" (comint-quote-filename localname)))
4475 ;;- (tramp-wait-for-output))
4476 (tramp-send-command multi-method method user host
4477 (format "echo %s" localname))
4478 (tramp-wait-for-output)
4479 (setq bufstr (buffer-substring (point-min)
4480 (tramp-line-end-position)))
4481 (goto-char (point-min))
4482 (if (string-equal localname bufstr)
4485 (while (search-forward " " nil t)
4486 (delete-backward-char 1)
4488 (goto-char (point-max))
4489 (delete-backward-char 1)
4491 (goto-char (point-min))
4493 (function (lambda (x)
4494 (tramp-make-tramp-file-name multi-method method
4496 (read (current-buffer))))))
4497 (list (expand-file-name name))))))
4499 (eval-after-load "complete"
4501 (defadvice PC-expand-many-files
4502 (around tramp-advice-PC-expand-many-files (name) activate)
4503 "Invoke `tramp-handle-expand-many-files' for tramp files."
4504 (if (tramp-tramp-file-p name)
4505 (setq ad-return-value (tramp-handle-expand-many-files name))
4507 (add-hook 'tramp-unload-hook
4508 '(lambda () (ad-unadvise 'PC-expand-many-files)))))
4510 ;;; File name handler functions for completion mode
4512 (defvar tramp-completion-mode nil
4513 "If non-nil, we are in file name completion mode.")
4515 ;; Necessary because `tramp-file-name-regexp-unified' and
4516 ;; `tramp-completion-file-name-regexp-unified' aren't different.
4517 ;; If nil, `tramp-completion-run-real-handler' is called (i.e. forwarding to
4518 ;; `tramp-file-name-handler'). Otherwise, it takes `tramp-run-real-handler'.
4519 ;; Using `last-input-event' is a little bit risky, because completing a file
4520 ;; might require loading other files, like "~/.netrc", and for them it
4521 ;; shouldn't be decided based on that variable. On the other hand, those files
4522 ;; shouldn't have partial tramp file name syntax. Maybe another variable should
4523 ;; be introduced overwriting this check in such cases. Or we change tramp
4524 ;; file name syntax in order to avoid ambiguities, like in XEmacs ...
4525 ;; In case of non unified file names it can be always true (and wouldn't be
4526 ;; necessary, because there are different regexp).
4527 (defun tramp-completion-mode (file)
4528 "Checks whether method / user name / host name completion is active."
4530 (tramp-completion-mode t)
4531 ((string-match "^/.*:.*:$" file) nil)
4533 (concat tramp-prefix-regexp
4534 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "$")
4536 (member (match-string 1 file) (mapcar 'car tramp-methods)))
4537 ((or (equal last-input-event 'tab)
4539 (and (wholenump last-input-event)
4541 ;; ?\t has event-modifier 'control
4542 (char-equal last-input-event ?\t)
4543 (and (not (event-modifiers last-input-event))
4544 (or (char-equal last-input-event ?\?)
4545 (char-equal last-input-event ?\ )))))
4547 (and (featurep 'xemacs)
4549 ;; ?\t has event-modifier 'control
4551 (funcall (symbol-function 'event-to-character)
4552 last-input-event) ?\t)
4553 (and (not (event-modifiers last-input-event))
4555 (funcall (symbol-function 'event-to-character)
4556 last-input-event) ?\?)
4558 (funcall (symbol-function 'event-to-character)
4559 last-input-event) ?\ ))))))
4562 ;; Method, host name and user name completion.
4563 ;; `tramp-completion-dissect-file-name' returns a list of
4564 ;; tramp-file-name structures. For all of them we return possible completions.
4566 (defun tramp-completion-handle-file-name-all-completions (filename directory)
4567 "Like `file-name-all-completions' for partial tramp files."
4570 ;; We need to reset `tramp-completion-mode'.
4572 (setq tramp-completion-mode t)
4574 ((fullname (concat directory filename))
4575 ;; possible completion structures
4576 (v (tramp-completion-dissect-file-name fullname))
4580 (let* ((car (car v))
4581 (multi-method (tramp-file-name-multi-method car))
4582 (method (tramp-file-name-method car))
4583 (user (tramp-file-name-user car))
4584 (host (tramp-file-name-host car))
4585 (localname (tramp-file-name-localname car))
4586 (m (tramp-find-method multi-method method user host))
4587 (tramp-current-user user) ; see `tramp-parse-passwd'
4590 (unless (or multi-method ;; Not handled (yet).
4591 localname) ;; Nothing to complete
4595 ;; Method dependent user / host combinations
4599 (setq all-user-hosts
4600 (append all-user-hosts
4601 (funcall (nth 0 x) (nth 1 x)))))
4602 (tramp-get-completion-function m))
4604 (setq result (append result
4607 (tramp-get-completion-user-host
4608 method user host (nth 0 x) (nth 1 x)))
4609 (delq nil all-user-hosts)))))
4613 (append result (tramp-get-completion-methods m)))))
4617 ;; unify list, remove nil elements
4619 (let ((car (car result)))
4620 (when car (add-to-list
4621 'result1 (substring car (length directory))))
4622 (setq result (cdr result))))
4624 ;; Complete local parts
4629 ;; "/ssh:" does not need to be expanded as hostname.
4630 (tramp-run-real-handler
4631 'file-name-all-completions (list filename directory))
4632 ;; No method/user/host found to be expanded.
4633 (tramp-completion-run-real-handler
4634 'file-name-all-completions (list filename directory)))
4637 (setq tramp-completion-mode nil)))
4639 ;; Method, host name and user name completion for a file.
4641 (defun tramp-completion-handle-file-name-completion
4642 (filename directory &optional predicate)
4643 "Like `file-name-completion' for tramp files."
4646 (mapcar 'list (file-name-all-completions filename directory))
4648 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
4650 ;; I misuse a little bit the tramp-file-name structure in order to handle
4651 ;; completion possibilities for partial methods / user names / host names.
4652 ;; Return value is a list of tramp-file-name structures according to possible
4653 ;; completions. If "multi-method" or "localname" is non-nil it means there
4654 ;; shouldn't be a completion anymore.
4656 ;; Expected results:
4658 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
4659 ;; [nil nil nil "x" nil] [nil nil "x" nil nil] [nil nil "x" "y" nil]
4660 ;; [nil nil "x" nil nil]
4661 ;; [nil "x" nil nil nil]
4663 ;; "/x:" "/x:y" "/x:y:"
4664 ;; [nil nil nil "x" ""] [nil nil nil "x" "y"] [nil "x" nil "y" ""]
4666 ;; [nil "x" nil "" nil] [nil "x" nil "y" nil]
4667 ;; [nil "x" "" nil nil] [nil "x" "y" nil nil]
4669 ;; "/x:y@" "/x:y@z" "/x:y@z:"
4670 ;; [nil nil nil "x" "y@"] [nil nil nil "x" "y@z"] [nil "x" "y" "z" ""]
4671 ;; "/[x/y@" "/[x/y@z"
4672 ;; [nil "x" nil "y" nil] [nil "x" "y" "z" nil]
4673 (defun tramp-completion-dissect-file-name (name)
4674 "Returns a list of `tramp-file-name' structures.
4675 They are collected by `tramp-completion-dissect-file-name1'."
4679 ;; "/method" "/[method"
4680 (tramp-completion-file-name-structure1
4681 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
4684 (tramp-completion-file-name-structure2
4685 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
4688 (tramp-completion-file-name-structure3
4689 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
4691 ;; "/user@host" "/[user@host"
4692 (tramp-completion-file-name-structure4
4693 (list (concat tramp-prefix-regexp
4694 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4695 "\\(" tramp-host-regexp x-nil "\\)$")
4697 ;; "/method:user" "/[method/user"
4698 (tramp-completion-file-name-structure5
4699 (list (concat tramp-prefix-regexp
4700 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4701 "\\(" tramp-user-regexp x-nil "\\)$")
4703 ;; "/method:host" "/[method/host"
4704 (tramp-completion-file-name-structure6
4705 (list (concat tramp-prefix-regexp
4706 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4707 "\\(" tramp-host-regexp x-nil "\\)$")
4709 ;; "/method:user@host" "/[method/user@host"
4710 (tramp-completion-file-name-structure7
4711 (list (concat tramp-prefix-regexp
4712 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4713 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4714 "\\(" tramp-host-regexp x-nil "\\)$")
4717 (mapcar (lambda (regexp)
4718 (add-to-list 'result
4719 (tramp-completion-dissect-file-name1 regexp name)))
4721 tramp-completion-file-name-structure1
4722 tramp-completion-file-name-structure2
4723 tramp-completion-file-name-structure3
4724 tramp-completion-file-name-structure4
4725 tramp-completion-file-name-structure5
4726 tramp-completion-file-name-structure6
4727 tramp-completion-file-name-structure7
4728 tramp-file-name-structure))
4732 (defun tramp-completion-dissect-file-name1 (structure name)
4733 "Returns a `tramp-file-name' structure matching STRUCTURE.
4734 The structure consists of multi-method, remote method, remote user,
4735 remote host and localname (filename on remote host)."
4739 (when (string-match (nth 0 structure) name)
4740 (setq method (and (nth 1 structure)
4741 (match-string (nth 1 structure) name)))
4742 (if (and method (member method tramp-multi-methods))
4743 ;; Not handled (yet).
4744 (make-tramp-file-name
4745 :multi-method method
4750 (let ((user (and (nth 2 structure)
4751 (match-string (nth 2 structure) name)))
4752 (host (and (nth 3 structure)
4753 (match-string (nth 3 structure) name)))
4754 (localname (and (nth 4 structure)
4755 (match-string (nth 4 structure) name))))
4756 (make-tramp-file-name
4761 :localname localname)))))))
4763 ;; This function returns all possible method completions, adding the
4764 ;; trailing method delimeter.
4765 (defun tramp-get-completion-methods (partial-method)
4766 "Returns all method completions for PARTIAL-METHOD."
4770 (string-match (concat "^" (regexp-quote partial-method)) method)
4771 (tramp-make-tramp-file-name nil method nil nil nil)))
4772 (delete "multi" (mapcar 'car tramp-methods))))
4774 ;; Compares partial user and host names with possible completions.
4775 (defun tramp-get-completion-user-host (method partial-user partial-host user host)
4776 "Returns the most expanded string for user and host name completion.
4777 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
4780 ((and partial-user partial-host)
4782 (string-match (concat "^" (regexp-quote partial-host)) host)
4783 (string-equal partial-user (or user partial-user)))
4784 (setq user partial-user)
4791 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
4797 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
4803 (unless (zerop (+ (length user) (length host)))
4804 (tramp-make-tramp-file-name nil method user host nil)))
4806 (defun tramp-parse-rhosts (filename)
4807 "Return a list of (user host) tuples allowed to access.
4808 Either user or host may be nil."
4811 (when (file-readable-p filename)
4813 (insert-file-contents filename)
4814 (goto-char (point-min))
4816 (push (tramp-parse-rhosts-group) res))))
4819 ;; Taken from gnus/netrc.el
4821 (defalias 'tramp-point-at-eol
4822 (if (fboundp 'point-at-eol)
4824 'line-end-position)))
4826 (defun tramp-parse-rhosts-group ()
4827 "Return a (user host) tuple allowed to access.
4828 Either user or host may be nil."
4833 "^\\(" tramp-host-regexp "\\)"
4834 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4836 (narrow-to-region (point) (tramp-point-at-eol))
4837 (when (re-search-forward regexp nil t)
4838 (setq result (append (list (match-string 3) (match-string 1)))))
4843 (defun tramp-parse-shosts (filename)
4844 "Return a list of (user host) tuples allowed to access.
4845 User is always nil."
4848 (when (file-readable-p filename)
4850 (insert-file-contents filename)
4851 (goto-char (point-min))
4853 (push (tramp-parse-shosts-group) res))))
4856 (defun tramp-parse-shosts-group ()
4857 "Return a (user host) tuple allowed to access.
4858 User is always nil."
4861 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4863 (narrow-to-region (point) (tramp-point-at-eol))
4864 (when (re-search-forward regexp nil t)
4865 (setq result (list nil (match-string 1))))
4868 (> (skip-chars-forward ",") 0)
4872 (defun tramp-parse-sconfig (filename)
4873 "Return a list of (user host) tuples allowed to access.
4874 User is always nil."
4877 (when (file-readable-p filename)
4879 (insert-file-contents filename)
4880 (goto-char (point-min))
4882 (push (tramp-parse-sconfig-group) res))))
4885 (defun tramp-parse-sconfig-group ()
4886 "Return a (user host) tuple allowed to access.
4887 User is always nil."
4890 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
4892 (narrow-to-region (point) (tramp-point-at-eol))
4893 (when (re-search-forward regexp nil t)
4894 (setq result (list nil (match-string 1))))
4897 (> (skip-chars-forward ",") 0)
4901 (defun tramp-parse-shostkeys (dirname)
4902 "Return a list of (user host) tuples allowed to access.
4903 User is always nil."
4905 (let ((regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
4906 (files (when (file-directory-p dirname) (directory-files dirname)))
4910 (when (string-match regexp (car files))
4911 (push (list nil (match-string 1 (car files))) result))
4912 (setq files (cdr files)))
4915 (defun tramp-parse-sknownhosts (dirname)
4916 "Return a list of (user host) tuples allowed to access.
4917 User is always nil."
4919 (let ((regexp (concat "^\\(" tramp-host-regexp
4920 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
4921 (files (when (file-directory-p dirname) (directory-files dirname)))
4925 (when (string-match regexp (car files))
4926 (push (list nil (match-string 1 (car files))) result))
4927 (setq files (cdr files)))
4930 (defun tramp-parse-hosts (filename)
4931 "Return a list of (user host) tuples allowed to access.
4932 User is always nil."
4935 (when (file-readable-p filename)
4937 (insert-file-contents filename)
4938 (goto-char (point-min))
4940 (push (tramp-parse-hosts-group) res))))
4943 (defun tramp-parse-hosts-group ()
4944 "Return a (user host) tuple allowed to access.
4945 User is always nil."
4948 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4950 (narrow-to-region (point) (tramp-point-at-eol))
4951 (when (re-search-forward regexp nil t)
4952 (unless (char-equal (or (char-after) ?\n) ?:) ; no IPv6
4953 (setq result (list nil (match-string 1)))))
4956 (> (skip-chars-forward " \t") 0)
4960 ;; For su-alike methods it would be desirable to return "root@localhost"
4961 ;; as default. Unfortunately, we have no information whether any user name
4962 ;; has been typed already. So we (mis-)use tramp-current-user as indication,
4963 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
4964 (defun tramp-parse-passwd (filename)
4965 "Return a list of (user host) tuples allowed to access.
4966 Host is always \"localhost\"."
4969 (if (zerop (length tramp-current-user))
4971 (when (file-readable-p filename)
4973 (insert-file-contents filename)
4974 (goto-char (point-min))
4976 (push (tramp-parse-passwd-group) res))))
4979 (defun tramp-parse-passwd-group ()
4980 "Return a (user host) tuple allowed to access.
4981 Host is always \"localhost\"."
4984 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
4986 (narrow-to-region (point) (tramp-point-at-eol))
4987 (when (re-search-forward regexp nil t)
4988 (setq result (list (match-string 1) "localhost")))
4993 (defun tramp-parse-netrc (filename)
4994 "Return a list of (user host) tuples allowed to access.
4998 (when (file-readable-p filename)
5000 (insert-file-contents filename)
5001 (goto-char (point-min))
5003 (push (tramp-parse-netrc-group) res))))
5006 (defun tramp-parse-netrc-group ()
5007 "Return a (user host) tuple allowed to access.
5013 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
5014 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
5016 (narrow-to-region (point) (tramp-point-at-eol))
5017 (when (re-search-forward regexp nil t)
5018 (setq result (list (match-string 3) (match-string 1))))
5023 ;;; Internal Functions:
5025 (defun tramp-maybe-send-perl-script (multi-method method user host script name)
5026 "Define in remote shell function NAME implemented as perl SCRIPT.
5027 Only send the definition if it has not already been done.
5028 Function may have 0-3 parameters."
5029 (let ((remote-perl (tramp-get-remote-perl multi-method method user host)))
5030 (unless remote-perl (error "No remote perl"))
5031 (let ((perl-scripts (tramp-get-connection-property "perl-scripts" nil
5032 multi-method method user host)))
5033 (unless (memq name perl-scripts)
5034 (with-current-buffer (tramp-get-buffer multi-method method user host)
5035 (tramp-message 5 (concat "Sending the Perl script `" name "'..."))
5036 (tramp-send-string multi-method method user host
5042 "' \"$1\" \"$2\" \"$3\" 2>/dev/null\n}"))
5043 (tramp-wait-for-output)
5044 (tramp-set-connection-property "perl-scripts" (cons name perl-scripts)
5045 multi-method method user host)
5046 (tramp-message 5 (concat "Sending the Perl script `" name "'...done.")))))))
5048 (defun tramp-set-auto-save ()
5049 (when (and (buffer-file-name)
5050 (tramp-tramp-file-p (buffer-file-name))
5051 ;; ange-ftp has its own auto-save mechanism
5052 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
5053 'tramp-sh-file-name-handler)
5055 (auto-save-mode 1)))
5056 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
5057 (add-hook 'tramp-unload-hook
5059 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
5061 (defun tramp-run-test (switch filename)
5062 "Run `test' on the remote system, given a SWITCH and a FILENAME.
5063 Returns the exit code of the `test' program."
5064 (let ((v (tramp-dissect-file-name filename)))
5066 (tramp-send-command-and-check
5067 (tramp-file-name-multi-method v) (tramp-file-name-method v)
5068 (tramp-file-name-user v) (tramp-file-name-host v)
5069 (format "test %s %s" switch
5070 (tramp-shell-quote-argument (tramp-file-name-localname v)))))))
5072 (defun tramp-run-test2 (program file1 file2 &optional switch)
5073 "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2.
5074 The optional SWITCH is inserted between the two files.
5075 Returns the exit code of the `test' PROGRAM. Barfs if the methods,
5076 hosts, or files, disagree."
5077 (let* ((v1 (tramp-dissect-file-name file1))
5078 (v2 (tramp-dissect-file-name file2))
5079 (mmethod1 (tramp-file-name-multi-method v1))
5080 (mmethod2 (tramp-file-name-multi-method v2))
5081 (method1 (tramp-file-name-method v1))
5082 (method2 (tramp-file-name-method v2))
5083 (user1 (tramp-file-name-user v1))
5084 (user2 (tramp-file-name-user v2))
5085 (host1 (tramp-file-name-host v1))
5086 (host2 (tramp-file-name-host v2))
5087 (localname1 (tramp-file-name-localname v1))
5088 (localname2 (tramp-file-name-localname v2)))
5089 (unless (and method1 method2 host1 host2
5090 (equal mmethod1 mmethod2)
5091 (equal method1 method2)
5093 (equal host1 host2))
5094 (error "tramp-run-test2: %s"
5095 "only implemented for same method, same user, same host"))
5097 (tramp-send-command-and-check
5098 mmethod1 method1 user1 host1
5099 (format "%s %s %s %s"
5101 (tramp-shell-quote-argument localname1)
5103 (tramp-shell-quote-argument localname2))))))
5105 (defun tramp-touch (file time)
5106 "Set the last-modified timestamp of the given file.
5107 TIME is an Emacs internal time value as returned by `current-time'."
5109 ;; With GNU Emacs, `format-time-string' has an optional
5110 ;; parameter UNIVERSAL. This is preferred.
5111 (and (functionp 'subr-arity)
5112 (= 3 (cdr (funcall (symbol-function 'subr-arity)
5113 (symbol-function 'format-time-string))))))
5116 (format-time-string "%Y%m%d%H%M.%S" time t)
5117 (format-time-string "%Y%m%d%H%M.%S" time))))
5118 (if (tramp-tramp-file-p file)
5119 (with-parsed-tramp-file-name file nil
5120 (let ((buf (tramp-get-buffer multi-method method user host)))
5121 (unless (zerop (tramp-send-command-and-check
5122 multi-method method user host
5123 (format "%s touch -t %s %s"
5124 (if utc "TZ=UTC; export TZ;" "")
5126 (tramp-shell-quote-argument localname))
5129 (error "tramp-touch: touch failed, see buffer `%s' for details"
5131 ;; It's a local file
5133 (unless (zerop (call-process
5134 "touch" nil (current-buffer) nil "-t" touch-time file))
5135 (pop-to-buffer (current-buffer))
5136 (error "tramp-touch: touch failed"))))))
5138 (defun tramp-buffer-name (multi-method method user host)
5139 "A name for the connection buffer for USER at HOST using METHOD."
5141 (tramp-buffer-name-multi-method "tramp" multi-method method user host)
5142 (let ((method (tramp-find-method multi-method method user host)))
5144 (format "*tramp/%s %s@%s*" method user host)
5145 (format "*tramp/%s %s*" method host)))))
5147 (defun tramp-buffer-name-multi-method (prefix multi-method method user host)
5148 "A name for the multi method connection buffer.
5149 MULTI-METHOD gives the multi method, METHOD the array of methods,
5150 USER the array of user names, HOST the array of host names."
5151 (unless (and (= (length method) (length user))
5152 (= (length method) (length host)))
5153 (error "Syntax error in multi method (implementation error)"))
5154 (let ((len (length method))
5159 (cons (if (aref user i)
5160 (format "%s#%s@%s:" (aref method i)
5161 (aref user i) (aref host i))
5162 (format "%s@%s:" (aref method i) (aref host i)))
5165 (format "*%s/%s %s*"
5167 (apply 'concat (reverse string-list)))))
5169 (defun tramp-get-buffer (multi-method method user host)
5170 "Get the connection buffer to be used for USER at HOST using METHOD."
5171 (with-current-buffer
5172 (get-buffer-create (tramp-buffer-name multi-method method user host))
5173 (setq buffer-undo-list t)
5176 (defun tramp-debug-buffer-name (multi-method method user host)
5177 "A name for the debug buffer for USER at HOST using METHOD."
5179 (tramp-buffer-name-multi-method "debug tramp"
5180 multi-method method user host)
5181 (let ((method (tramp-find-method multi-method method user host)))
5183 (format "*debug tramp/%s %s@%s*" method user host)
5184 (format "*debug tramp/%s %s*" method host)))))
5186 (defun tramp-get-debug-buffer (multi-method method user host)
5187 "Get the debug buffer for USER at HOST using METHOD."
5188 (with-current-buffer
5190 (tramp-debug-buffer-name multi-method method user host))
5191 (setq buffer-undo-list t)
5194 (defun tramp-find-executable (multi-method method user host
5195 progname dirlist ignore-tilde)
5196 "Searches for PROGNAME in all directories mentioned in DIRLIST.
5197 First args METHOD, USER and HOST specify the connection, PROGNAME
5198 is the program to search for, and DIRLIST gives the list of directories
5199 to search. If IGNORE-TILDE is non-nil, directory names starting
5200 with `~' will be ignored.
5202 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
5204 This function expects to be in the right *tramp* buffer."
5207 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
5208 ;; `remove' is in CL, and we want to avoid CL dependencies.
5211 (setq d (car dirlist))
5212 (setq dirlist (cdr dirlist))
5213 (unless (char-equal ?~ (aref d 0))
5214 (setq newdl (cons d newdl))))
5215 (setq dirlist (nreverse newdl))))
5217 multi-method method user host
5218 (format (concat "while read d; "
5219 "do if test -x $d/%s -a -f $d/%s; "
5220 "then echo tramp_executable $d/%s; "
5221 "break; fi; done <<'EOF'")
5222 progname progname progname))
5224 (tramp-send-command multi-method method user host d))
5226 (tramp-send-command multi-method method user host "EOF")
5227 (tramp-wait-for-output)
5228 (goto-char (point-max))
5229 (when (search-backward "tramp_executable " nil t)
5230 (skip-chars-forward "^ ")
5231 (skip-chars-forward " ")
5232 (buffer-substring (point) (tramp-line-end-position)))))
5234 (defun tramp-set-remote-path (multi-method method user host var dirlist)
5235 "Sets the remote environment VAR to existing directories from DIRLIST.
5236 I.e., for each directory in DIRLIST, it is tested whether it exists and if
5237 so, it is added to the environment variable VAR."
5238 (let ((existing-dirs
5243 (tramp-make-tramp-file-name multi-method method user host x))
5245 (tramp-make-tramp-file-name multi-method method user host x)))
5249 multi-method method user host
5251 (mapconcat 'identity (delq nil existing-dirs) ":")
5253 (tramp-wait-for-output)))
5255 ;; -- communication with external shell --
5257 (defun tramp-find-file-exists-command (multi-method method user host)
5258 "Find a command on the remote host for checking if a file exists.
5259 Here, we are looking for a command which has zero exit status if the
5260 file exists and nonzero exit status otherwise."
5261 (make-local-variable 'tramp-file-exists-command)
5262 (tramp-message 9 "Finding command to check if file exists")
5264 (tramp-make-tramp-file-name
5265 multi-method method user host
5266 "/")) ;assume this file always exists
5268 (tramp-make-tramp-file-name
5269 multi-method method user host
5270 "/ this file does not exist "))) ;assume this never exists
5271 ;; The algorithm is as follows: we try a list of several commands.
5272 ;; For each command, we first run `$cmd /' -- this should return
5273 ;; true, as the root directory always exists. And then we run
5274 ;; `$cmd /this\ file\ does\ not\ exist', hoping that the file indeed
5275 ;; does not exist. This should return false. We use the first
5276 ;; command we find that seems to work.
5277 ;; The list of commands to try is as follows:
5278 ;; `ls -d' This works on most systems, but NetBSD 1.4
5279 ;; has a bug: `ls' always returns zero exit
5280 ;; status, even for files which don't exist.
5281 ;; `test -e' Some Bourne shells have a `test' builtin
5282 ;; which does not know the `-e' option.
5283 ;; `/bin/test -e' For those, the `test' binary on disk normally
5284 ;; provides the option. Alas, the binary
5285 ;; is sometimes `/bin/test' and sometimes it's
5287 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
5289 (and (setq tramp-file-exists-command "test -e %s")
5290 (file-exists-p existing)
5291 (not (file-exists-p nonexisting)))
5292 (and (setq tramp-file-exists-command "/bin/test -e %s")
5293 (file-exists-p existing)
5294 (not (file-exists-p nonexisting)))
5295 (and (setq tramp-file-exists-command "/usr/bin/test -e %s")
5296 (file-exists-p existing)
5297 (not (file-exists-p nonexisting)))
5298 (and (setq tramp-file-exists-command "ls -d %s")
5299 (file-exists-p existing)
5300 (not (file-exists-p nonexisting))))
5301 (error "Couldn't find command to check if file exists"))))
5304 ;; CCC test ksh or bash found for tilde expansion?
5305 (defun tramp-find-shell (multi-method method user host)
5306 "Find a shell on the remote host which groks tilde expansion."
5308 (tramp-send-command multi-method method user host "echo ~root")
5309 (tramp-wait-for-output)
5311 ((string-match "^~root$" (buffer-string))
5313 (or (tramp-find-executable multi-method method user host
5314 "bash" tramp-remote-path t)
5315 (tramp-find-executable multi-method method user host
5316 "ksh" tramp-remote-path t)))
5318 (error "Couldn't find a shell which groks tilde expansion"))
5319 ;; Find arguments for this shell.
5320 (let ((alist tramp-sh-extra-args)
5322 (while (and alist (null extra-args))
5323 (setq item (pop alist))
5324 (when (string-match (car item) shell)
5325 (setq extra-args (cdr item))))
5326 (when extra-args (setq shell (concat shell " " extra-args))))
5328 5 "Starting remote shell `%s' for tilde expansion..." shell)
5330 multi-method method user host
5331 (concat "PS1='$ ' exec " shell)) ;
5332 (tramp-barf-if-no-shell-prompt
5333 (get-buffer-process (current-buffer))
5334 60 "Couldn't find remote `%s' prompt" shell)
5336 9 "Setting remote shell prompt...")
5337 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we
5338 ;; must use "\n" here, not tramp-rsh-end-of-line. Kai left the
5339 ;; last tramp-rsh-end-of-line, Douglas wanted to replace that,
5341 (process-send-string nil (format "PS1='%s%s%s'; PS2=''; PS3=''%s"
5342 tramp-rsh-end-of-line
5344 tramp-rsh-end-of-line
5345 tramp-rsh-end-of-line))
5346 (tramp-wait-for-output)
5348 9 "Setting remote shell prompt...done")
5350 (t (tramp-message 5 "Remote `%s' groks tilde expansion, good"
5351 (tramp-get-method-parameter
5352 multi-method method user host 'tramp-remote-sh))))))
5354 (defun tramp-check-ls-command (multi-method method user host cmd)
5355 "Checks whether the given `ls' executable groks `-n'.
5356 METHOD, USER and HOST specify the connection, CMD (the absolute file name of)
5357 the `ls' executable. Returns t if CMD supports the `-n' option, nil
5359 (tramp-message 9 "Checking remote `%s' command for `-n' option" cmd)
5360 (when (file-executable-p
5361 (tramp-make-tramp-file-name multi-method method user host cmd))
5363 (tramp-message 7 "Testing remote command `%s' for -n..." cmd)
5365 (tramp-send-command-and-check
5366 multi-method method user host
5367 (format "%s -lnd / >/dev/null"
5369 (tramp-message 7 "Testing remote command `%s' for -n...%s"
5371 (if (zerop result) "okay" "failed"))
5374 (defun tramp-check-ls-commands (multi-method method user host cmd dirlist)
5375 "Checks whether the given `ls' executable in one of the dirs groks `-n'.
5376 Returns nil if none was found, else the command is returned."
5379 (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
5380 ;; It would be better to use the CL function `find', but
5381 ;; we don't want run-time dependencies on CL.
5382 (while (and dl (not result))
5383 (let ((x (concat (file-name-as-directory (car dl)) cmd)))
5384 (when (tramp-check-ls-command multi-method method user host x)
5389 (defun tramp-find-ls-command (multi-method method user host)
5390 "Finds an `ls' command which groks the `-n' option, returning nil if failed.
5391 \(This option prints numeric user and group ids in a long listing.)"
5392 (tramp-message 9 "Finding a suitable `ls' command")
5394 (tramp-check-ls-commands multi-method method user host "ls" tramp-remote-path)
5395 (tramp-check-ls-commands multi-method method user host "gnuls" tramp-remote-path)
5396 (tramp-check-ls-commands multi-method method user host "gls" tramp-remote-path)))
5398 ;; ------------------------------------------------------------
5399 ;; -- Functions for establishing connection --
5400 ;; ------------------------------------------------------------
5402 ;; The following functions are actions to be taken when seeing certain
5403 ;; prompts from the remote host. See the variable
5404 ;; `tramp-actions-before-shell' for usage of these functions.
5406 (defun tramp-action-login (p multi-method method user host)
5407 "Send the login name."
5408 (tramp-message 9 "Sending login name `%s'"
5409 (or user (user-login-name)))
5411 (process-send-string nil (concat (or user (user-login-name))
5412 tramp-rsh-end-of-line)))
5414 (defun tramp-action-password (p multi-method method user host)
5415 "Query the user for a password."
5417 (format "Password for %s "
5418 (tramp-make-tramp-file-name
5419 nil method user host ""))))
5420 (tramp-message 9 "Sending password")
5421 (tramp-enter-password p pw-prompt user host)))
5423 (defun tramp-action-succeed (p multi-method method user host)
5424 "Signal success in finding shell prompt."
5425 (tramp-message 9 "Found remote shell prompt.")
5427 (throw 'tramp-action 'ok))
5429 (defun tramp-action-permission-denied (p multi-method method user host)
5430 "Signal permission denied."
5431 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5432 (tramp-message 9 "Permission denied by remote host.")
5434 (throw 'tramp-action 'permission-denied))
5436 (defun tramp-action-copy-failed (p multi-method method user host)
5437 "Signal copy failed."
5439 (error "%s" (match-string 1)))
5441 (defun tramp-action-yesno (p multi-method method user host)
5442 "Ask the user for confirmation using `yes-or-no-p'.
5443 Send \"yes\" to remote process on confirmation, abort otherwise.
5444 See also `tramp-action-yn'."
5445 (save-window-excursion
5446 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5447 (unless (yes-or-no-p (match-string 0))
5450 (throw 'tramp-action 'permission-denied))
5451 (process-send-string p (concat "yes" tramp-rsh-end-of-line))
5454 (defun tramp-action-yn (p multi-method method user host)
5455 "Ask the user for confirmation using `y-or-n-p'.
5456 Send \"y\" to remote process on confirmation, abort otherwise.
5457 See also `tramp-action-yesno'."
5458 (save-window-excursion
5459 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5460 (unless (y-or-n-p (match-string 0))
5462 (throw 'tramp-action 'permission-denied))
5464 (process-send-string p (concat "y" tramp-rsh-end-of-line))))
5466 (defun tramp-action-terminal (p multi-method method user host)
5467 "Tell the remote host which terminal type to use.
5468 The terminal type can be configured with `tramp-terminal-type'."
5469 (tramp-message 9 "Setting `%s' as terminal type."
5470 tramp-terminal-type)
5472 (process-send-string nil (concat tramp-terminal-type
5473 tramp-rsh-end-of-line)))
5475 (defun tramp-action-process-alive (p multi-method method user host)
5476 "Check whether a process has finished."
5477 (unless (memq (process-status p) '(run open))
5478 (throw 'tramp-action 'process-died)))
5480 (defun tramp-action-out-of-band (p multi-method method user host)
5481 "Check whether an out-of-band copy has finished."
5482 (cond ((and (memq (process-status p) '(stop exit))
5483 (zerop (process-exit-status p)))
5484 (tramp-message 9 "Process has finished.")
5485 (throw 'tramp-action 'ok))
5486 ((or (and (memq (process-status p) '(stop exit))
5487 (not (zerop (process-exit-status p))))
5488 (memq (process-status p) '(signal)))
5489 ;; `scp' could have copied correctly, but set modes could have failed.
5490 ;; This can be ignored.
5491 (goto-char (point-min))
5492 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
5494 (tramp-message 10 "'set mode' error ignored.")
5495 (tramp-message 9 "Process has finished.")
5496 (throw 'tramp-action 'ok))
5497 (tramp-message 9 "Process has died.")
5498 (throw 'tramp-action 'process-died)))
5501 ;; The following functions are specifically for multi connections.
5503 (defun tramp-multi-action-login (p method user host)
5504 "Send the login name."
5505 (tramp-message 9 "Sending login name `%s'" user)
5507 (process-send-string p (concat user tramp-rsh-end-of-line)))
5509 (defun tramp-multi-action-password (p method user host)
5510 "Query the user for a password."
5512 (format "Password for %s "
5513 (tramp-make-tramp-file-name
5514 nil method user host ""))))
5515 (tramp-message 9 "Sending password")
5516 (tramp-enter-password p pw-prompt user host)))
5518 (defun tramp-multi-action-succeed (p method user host)
5519 "Signal success in finding shell prompt."
5520 (tramp-message 9 "Found shell prompt on `%s'" host)
5522 (throw 'tramp-action 'ok))
5524 (defun tramp-multi-action-permission-denied (p method user host)
5525 "Signal permission denied."
5526 (tramp-message 9 "Permission denied by remote host `%s'" host)
5529 (throw 'tramp-action 'permission-denied))
5531 (defun tramp-multi-action-process-alive (p method user host)
5532 "Check whether a process has finished."
5533 (unless (memq (process-status p) '(run open))
5534 (throw 'tramp-action 'process-died)))
5536 ;; Functions for processing the actions.
5538 (defun tramp-process-one-action (p multi-method method user host actions)
5539 "Wait for output from the shell and perform one action."
5540 (let (found item pattern action todo)
5542 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5543 (with-timeout (60 (throw 'tramp-action 'timeout))
5545 (tramp-accept-process-output p 1)
5546 (goto-char (point-min))
5549 (goto-char (point-min))
5550 (setq item (pop todo))
5551 (setq pattern (symbol-value (nth 0 item)))
5552 (setq action (nth 1 item))
5553 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5555 (when (re-search-forward (concat pattern "\\'") nil t)
5556 (setq found (funcall action p multi-method method user host)))))
5559 (defun tramp-process-actions (p multi-method method user host actions)
5560 "Perform actions until success."
5561 (tramp-message 10 "%s" (mapconcat 'identity (process-command p) " "))
5564 (tramp-message 9 "Waiting for prompts from remote shell")
5566 (catch 'tramp-action
5567 (tramp-process-one-action
5568 p multi-method method user host actions)
5570 (unless (eq exit 'ok)
5571 (tramp-clear-passwd user host)
5572 (error "Login failed"))))
5574 ;; For multi-actions.
5576 (defun tramp-process-one-multi-action (p method user host actions)
5577 "Wait for output from the shell and perform one action."
5578 (let (found item pattern action todo)
5580 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5581 (with-timeout (60 (throw 'tramp-action 'timeout))
5583 (tramp-accept-process-output p 1)
5585 (goto-char (point-min))
5587 (goto-char (point-min))
5588 (setq item (pop todo))
5589 (setq pattern (symbol-value (nth 0 item)))
5590 (setq action (nth 1 item))
5591 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5593 (when (re-search-forward (concat pattern "\\'") nil t)
5594 (setq found (funcall action p method user host)))))
5597 (defun tramp-process-multi-actions (p method user host actions)
5598 "Perform actions until success."
5601 (tramp-message 9 "Waiting for prompts from remote shell")
5603 (catch 'tramp-action
5604 (tramp-process-one-multi-action p method user host actions)
5606 (unless (eq exit 'ok)
5607 (tramp-clear-passwd user host)
5608 (error "Login failed"))))
5610 ;; Functions to execute when we have seen the remote shell prompt but
5611 ;; before we exec the Bourne-ish shell. Note that these commands
5612 ;; might be sent to any shell, not just a Bourne-ish shell. This
5613 ;; means that the commands need to work in all shells. (It is also
5614 ;; okay for some commands to just fail with an error message, but
5615 ;; please make sure that they at least don't crash the odd shell people
5616 ;; might be running...)
5617 (defun tramp-process-initial-commands (p
5618 multi-method method user host
5620 "Send list of commands to remote host, in order."
5623 (setq cmd (pop commands))
5625 (tramp-message 10 "Sending command to remote shell: %s"
5627 (tramp-send-command multi-method method user host cmd nil t)
5628 (tramp-barf-if-no-shell-prompt
5629 p 60 "Remote shell command failed: %s" cmd))
5632 ;; The actual functions for opening connections.
5634 (defun tramp-open-connection-telnet (multi-method method user host)
5635 "Open a connection using a telnet METHOD.
5636 This starts the command `telnet HOST ARGS'[*], then waits for a remote
5637 login prompt, then sends the user name USER, then waits for a remote
5638 password prompt. It queries the user for the password, then sends the
5639 password to the remote host.
5641 If USER is nil, uses value returned by `(user-login-name)' instead.
5643 Recognition of the remote shell prompt is based on the variables
5644 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5647 Please note that it is NOT possible to use this connection method
5648 together with an out-of-band transfer method! You must use an inline
5651 Maybe the different regular expressions need to be tuned.
5653 * Actually, the telnet program as well as the args to be used can be
5654 specified in the method parameters, see the variable `tramp-methods'."
5656 (when (tramp-method-out-of-band-p multi-method method user host)
5657 (error "Cannot use out-of-band method `%s' with telnet connection method"
5660 (error "Cannot multi-connect using telnet connection method"))
5661 (tramp-pre-connection multi-method method user host tramp-chunksize)
5662 (tramp-message 7 "Opening connection for %s@%s using %s..."
5663 (or user (user-login-name)) host method)
5664 (let ((process-environment (copy-sequence process-environment)))
5665 (setenv "TERM" tramp-terminal-type)
5667 (let* ((default-directory (tramp-temporary-file-directory))
5668 ;; If we omit the conditional here, then we would use
5669 ;; `undecided-dos' in some cases. With the conditional,
5670 ;; we use nil in these cases. Which one is right?
5671 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5672 (> emacs-major-version 20))
5673 tramp-dos-coding-system))
5674 (p (apply 'start-process
5675 (tramp-buffer-name multi-method method user host)
5676 (tramp-get-buffer multi-method method user host)
5677 (tramp-get-method-parameter
5679 (tramp-find-method multi-method method user host)
5680 user host 'tramp-login-program)
5682 (tramp-get-method-parameter
5684 (tramp-find-method multi-method method user host)
5685 user host 'tramp-login-args)))
5688 (tramp-set-process-query-on-exit-flag p nil)
5689 (set-buffer (tramp-get-buffer multi-method method user host))
5691 (tramp-process-actions p multi-method method user host
5692 tramp-actions-before-shell)
5693 (tramp-open-connection-setup-interactive-shell
5694 p multi-method method user host)
5695 (tramp-post-connection multi-method method user host)))))
5698 (defun tramp-open-connection-rsh (multi-method method user host)
5699 "Open a connection using an rsh METHOD.
5700 This starts the command `rsh HOST -l USER'[*], then waits for a remote
5701 password or shell prompt. If a password prompt is seen, the user is
5702 queried for a password, this function sends the password to the remote
5703 host and waits for a shell prompt.
5705 If USER is nil, start the command `rsh HOST'[*] instead
5707 Recognition of the remote shell prompt is based on the variables
5708 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5711 Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to
5712 be a port number for ssh, and \"-p yy\" will be added to the list of
5713 arguments, and xx will be used as the host name to connect to.
5715 * Actually, the rsh program to be used can be specified in the
5716 method parameters, see the variable `tramp-methods'."
5719 (error "Cannot multi-connect using rsh connection method"))
5720 (tramp-pre-connection multi-method method user host tramp-chunksize)
5721 (if (and user (not (string= user "")))
5722 (tramp-message 7 "Opening connection for %s@%s using %s..."
5724 (tramp-message 7 "Opening connection at %s using %s..." host method))
5725 (let ((process-environment (copy-sequence process-environment))
5726 (bufnam (tramp-buffer-name multi-method method user host))
5727 (buf (tramp-get-buffer multi-method method user host))
5728 (login-program (tramp-get-method-parameter
5730 (tramp-find-method multi-method method user host)
5731 user host 'tramp-login-program))
5735 x `((?t . ,(format "/tmp/%s" tramp-temp-name-prefix)))))
5736 (tramp-get-method-parameter
5738 (tramp-find-method multi-method method user host)
5739 user host 'tramp-login-args)))
5741 ;; The following should be changed. We need a more general
5742 ;; mechanism to parse extra host args.
5743 (when (string-match "\\([^#]*\\)#\\(.*\\)" host)
5744 (setq login-args (cons "-p" (cons (match-string 2 host) login-args)))
5745 (setq real-host (match-string 1 host)))
5746 (setenv "TERM" tramp-terminal-type)
5748 (let* ((default-directory (tramp-temporary-file-directory))
5749 ;; If we omit the conditional, we would use
5750 ;; `undecided-dos' in some cases. With the conditional,
5751 ;; we use nil in these cases. Which one is right?
5752 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5753 (> emacs-major-version 20))
5754 tramp-dos-coding-system))
5755 (p (if (and user (not (string= user "")))
5756 (apply #'start-process bufnam buf login-program
5757 real-host "-l" user login-args)
5758 (apply #'start-process bufnam buf login-program
5759 real-host login-args)))
5761 (tramp-set-process-query-on-exit-flag p nil)
5764 (tramp-process-actions p multi-method method user host
5765 tramp-actions-before-shell)
5766 (tramp-message 7 "Initializing remote shell")
5767 (tramp-open-connection-setup-interactive-shell
5768 p multi-method method user host)
5769 (tramp-post-connection multi-method method user host)))))
5771 (defun tramp-open-connection-su (multi-method method user host)
5772 "Open a connection using the `su' program with METHOD.
5773 This starts `su - USER', then waits for a password prompt. The HOST
5774 name must be equal to the local host name or to `localhost'.
5776 If USER is nil, uses value returned by user-login-name instead.
5778 Recognition of the remote shell prompt is based on the variables
5779 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5780 set up correctly. Note that the other user may have a different shell
5781 prompt than you do, so it is not at all unlikely that the variable
5782 `shell-prompt-pattern' is set up wrongly!"
5784 (when (tramp-method-out-of-band-p multi-method method user host)
5785 (error "Cannot use out-of-band method `%s' with `su' connection method"
5787 (unless (or (string-match (concat "^" (regexp-quote host))
5789 (string= "localhost" host)
5792 "Cannot connect to different host `%s' with `su' connection method"
5794 (tramp-pre-connection multi-method method user host tramp-chunksize)
5795 (tramp-message 7 "Opening connection for `%s' using `%s'..."
5796 (or user "<root>") method)
5797 (let ((process-environment (copy-sequence process-environment)))
5798 (setenv "TERM" tramp-terminal-type)
5800 (let* ((default-directory (tramp-temporary-file-directory))
5801 ;; If we omit the conditional, we use `undecided-dos' in
5802 ;; some cases. With the conditional, we use nil in these
5803 ;; cases. What's the difference? Which one is right?
5804 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5805 (> emacs-major-version 20))
5806 tramp-dos-coding-system))
5807 (p (apply 'start-process
5808 (tramp-buffer-name multi-method method user host)
5809 (tramp-get-buffer multi-method method user host)
5810 (tramp-get-method-parameter
5812 (tramp-find-method multi-method method user host)
5813 user host 'tramp-login-program)
5816 (format-spec x `((?u . ,(or user "root")))))
5817 (tramp-get-method-parameter
5819 (tramp-find-method multi-method method user host)
5820 user host 'tramp-login-args))))
5823 (tramp-set-process-query-on-exit-flag p nil)
5824 (set-buffer (tramp-get-buffer multi-method method user host))
5825 (tramp-process-actions p multi-method method user host
5826 tramp-actions-before-shell)
5827 (tramp-open-connection-setup-interactive-shell
5828 p multi-method method user host)
5829 (tramp-post-connection multi-method method
5832 ;; HHH: Not Changed. Multi method. It is not clear to me how this can
5833 ;; handle not giving a user name in the "file name".
5835 ;; This is more difficult than for the single-hop method. In the
5836 ;; multi-hop-method, the desired behaviour should be that the
5837 ;; user must specify names for the telnet hops of which the user
5838 ;; name is different than the "original" name (or different from
5839 ;; the previous hop.
5840 (defun tramp-open-connection-multi (multi-method method user host)
5841 "Open a multi-hop connection using METHOD.
5842 This uses a slightly changed file name syntax. The idea is to say
5843 [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file
5844 This will use telnet to log in as u1 to h1, then use rsh from there to
5845 log in as u2 to h2."
5847 (unless multi-method
5848 (error "Multi-hop open connection function called on non-multi method"))
5849 (when (tramp-method-out-of-band-p multi-method method user host)
5850 (error "No out of band multi-hop connections"))
5851 (unless (and (arrayp method) (not (stringp method)))
5852 (error "METHOD must be an array of strings for multi methods"))
5853 (unless (and (arrayp user) (not (stringp user)))
5854 (error "USER must be an array of strings for multi methods"))
5855 (unless (and (arrayp host) (not (stringp host)))
5856 (error "HOST must be an array of strings for multi methods"))
5857 (unless (and (= (length method) (length user))
5858 (= (length method) (length host)))
5859 (error "Arrays METHOD, USER, HOST must have equal length"))
5860 (tramp-pre-connection multi-method method user host tramp-chunksize)
5861 (tramp-message 7 "Opening `%s' connection..." multi-method)
5862 (let ((process-environment (copy-sequence process-environment)))
5863 (setenv "TERM" tramp-terminal-type)
5865 (let* ((default-directory (tramp-temporary-file-directory))
5866 ;; If we omit the conditional, we use `undecided-dos' in
5867 ;; some cases. With the conditional, we use nil in these
5868 ;; cases. What's the difference? Which one is right?
5869 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5870 (> emacs-major-version 20))
5871 tramp-dos-coding-system))
5872 (p (start-process (tramp-buffer-name multi-method method user host)
5873 (tramp-get-buffer multi-method method user host)
5874 tramp-multi-sh-program))
5875 (num-hops (length method))
5877 (tramp-set-process-query-on-exit-flag p nil)
5878 (tramp-message 9 "Waiting 60s for local shell to come up...")
5879 (unless (tramp-wait-for-regexp
5880 p 60 (format "\\(%s\\)\\'\\|\\(%s\\)\\'"
5881 shell-prompt-pattern tramp-shell-prompt-pattern))
5882 (pop-to-buffer (buffer-name))
5884 (error "Couldn't find local shell prompt"))
5885 ;; Now do all the connections as specified.
5886 (while (< i num-hops)
5887 (let* ((m (aref method i))
5890 (entry (assoc m tramp-multi-connection-function-alist))
5891 (multi-func (nth 1 entry))
5892 (command (nth 2 entry)))
5893 ;; The multi-funcs don't need to do save-match-data, as that
5895 (funcall multi-func p m u h command)
5898 (tramp-open-connection-setup-interactive-shell
5899 p multi-method method user host)
5900 (tramp-post-connection multi-method method user host)))))
5902 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5903 ;; of no user name provided. Hack to make it work as it did before:
5904 ;; changed `user' to `(or user (user-login-name))' in the places where
5905 ;; the value is actually used.
5906 (defun tramp-multi-connect-telnet (p method user host command)
5907 "Issue `telnet' command.
5908 Uses shell COMMAND to issue a `telnet' command to log in as USER to
5909 HOST. You can use percent escapes in COMMAND: `%h' is replaced with
5910 the host name, and `%n' is replaced with an end of line character, as
5911 set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5914 If USER is nil, uses the return value of (user-login-name) instead."
5915 (let ((cmd (format-spec command
5916 `((?h . ,host) (?n . ,tramp-rsh-end-of-line))))
5917 (cmd1 (format-spec command `((?h . ,host) (?n . ""))))
5920 (tramp-message 9 "Sending telnet command `%s'" cmd1)
5921 (process-send-string p cmd)
5922 (tramp-process-multi-actions p method user host
5923 tramp-multi-actions)))
5925 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5926 ;; of no user name provided. Hack to make it work as it did before:
5927 ;; changed `user' to `(or user (user-login-name))' in the places where
5928 ;; the value is actually used.
5929 (defun tramp-multi-connect-rlogin (p method user host command)
5930 "Issue `rlogin' command.
5931 Uses shell COMMAND to issue an `rlogin' command to log in as USER to
5932 HOST. You can use percent escapes in COMMAND. `%u' will be replaced
5933 with the user name, `%h' will be replaced with the host name, and `%n'
5934 will be replaced with the value of `tramp-rsh-end-of-line'. You can use
5935 `%%' if you want to use a literal percent character.
5937 If USER is nil, uses the return value of (user-login-name) instead."
5938 (let ((cmd (format-spec command `((?h . ,host)
5939 (?u . ,(or user (user-login-name)))
5940 (?n . ,tramp-rsh-end-of-line))))
5941 (cmd1 (format-spec command `((?h . ,host)
5942 (?u . ,(or user (user-login-name)))
5946 (tramp-message 9 "Sending rlogin command `%s'" cmd1)
5947 (process-send-string p cmd)
5948 (tramp-process-multi-actions p method user host
5949 tramp-multi-actions)))
5951 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5952 ;; of no user name provided. Hack to make it work as it did before:
5953 ;; changed `user' to `(or user (user-login-name))' in the places where
5954 ;; the value is actually used.
5955 (defun tramp-multi-connect-su (p method user host command)
5956 "Issue `su' command.
5957 Uses shell COMMAND to issue a `su' command to log in as USER on
5958 HOST. The HOST name is ignored, this just changes the user id on the
5959 host currently logged in to.
5961 If USER is nil, uses the return value of (user-login-name) instead.
5963 You can use percent escapes in the COMMAND. `%u' is replaced with the
5964 user name, and `%n' is replaced with the value of
5965 `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5967 (let ((cmd (format-spec command `((?u . ,(or user (user-login-name)))
5968 (?n . ,tramp-rsh-end-of-line))))
5969 (cmd1 (format-spec command `((?u . ,(or user (user-login-name)))
5973 (tramp-message 9 "Sending su command `%s'" cmd1)
5974 (process-send-string p cmd)
5975 (tramp-process-multi-actions p method user host
5976 tramp-multi-actions)))
5978 ;; Utility functions.
5980 (defun tramp-accept-process-output
5981 (&optional process timeout timeout-msecs)
5982 "Like `accept-process-output' for Tramp processes.
5983 This is needed in order to hide `last-coding-system-used', which is set
5984 for process communication also."
5985 (let (last-coding-system-used)
5986 (accept-process-output process timeout timeout-msecs)))
5988 (defun tramp-wait-for-regexp (proc timeout regexp)
5989 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
5990 Expects the output of PROC to be sent to the current buffer. Returns
5991 the string that matched, or nil. Waits indefinitely if TIMEOUT is
5994 (start-time (current-time)))
5996 ;; Work around a bug in XEmacs 21, where the timeout
5997 ;; expires faster than it should. This degenerates
5998 ;; to polling for buggy XEmacsen, but oh, well.
5999 (while (and (not found)
6000 (< (tramp-time-diff (current-time) start-time)
6002 (with-timeout (timeout)
6004 (tramp-accept-process-output proc 1)
6005 (unless (memq (process-status proc) '(run open))
6006 (error "Process has died"))
6007 (goto-char (point-min))
6008 (setq found (re-search-forward regexp nil t))))))
6011 (tramp-accept-process-output proc 1)
6012 (unless (memq (process-status proc) '(run open))
6013 (error "Process has died"))
6014 (goto-char (point-min))
6015 (setq found (re-search-forward regexp nil t)))))
6016 (when tramp-debug-buffer
6018 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6019 tramp-current-user tramp-current-host)
6020 (point-min) (point-max))
6024 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6025 tramp-current-user tramp-current-host))
6026 (goto-char (point-max))
6027 (insert "[[Regexp `" regexp "' not found"
6028 (if timeout (format " in %d secs" timeout) "")
6032 (defun tramp-wait-for-shell-prompt (proc timeout)
6033 "Wait for the shell prompt to appear from process PROC within TIMEOUT seconds.
6034 See `tramp-wait-for-regexp' for more details.
6035 Shell prompt pattern is determined by variables `shell-prompt-pattern'
6036 and `tramp-shell-prompt-pattern'."
6037 (tramp-wait-for-regexp
6039 (format "\\(%s\\|%s\\)\\'"
6040 shell-prompt-pattern tramp-shell-prompt-pattern)))
6042 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
6043 "Wait for shell prompt and barf if none appears.
6044 Looks at process PROC to see if a shell prompt appears in TIMEOUT
6045 seconds. If not, it produces an error message with the given ERROR-ARGS."
6046 (unless (tramp-wait-for-shell-prompt proc timeout)
6047 (pop-to-buffer (buffer-name))
6048 (apply 'error error-args)))
6050 (defun tramp-enter-password (p prompt user host)
6051 "Prompt for a password and send it to the remote end.
6052 Uses PROMPT as a prompt and sends the password to process P."
6053 (let ((pw (tramp-read-passwd user host prompt)))
6055 (process-send-string
6057 (or (tramp-get-method-parameter
6058 tramp-current-multi-method
6059 tramp-current-method
6062 'tramp-password-end-of-line)
6063 tramp-default-password-end-of-line)))))
6065 ;; HHH: Not Changed. This might handle the case where USER is not
6066 ;; given in the "File name" very poorly. Then, the local
6067 ;; variable tramp-current-user will be set to nil.
6068 (defun tramp-pre-connection (multi-method method user host chunksize)
6069 "Do some setup before actually logging in.
6070 METHOD, USER and HOST specify the connection."
6071 (set-buffer (tramp-get-buffer multi-method method user host))
6072 (set (make-local-variable 'tramp-current-multi-method) multi-method)
6073 (set (make-local-variable 'tramp-current-method) method)
6074 (set (make-local-variable 'tramp-current-user) user)
6075 (set (make-local-variable 'tramp-current-host) host)
6076 (set (make-local-variable 'tramp-chunksize) chunksize)
6077 (set (make-local-variable 'inhibit-eol-conversion) nil)
6080 (defun tramp-open-connection-setup-interactive-shell
6081 (p multi-method method user host)
6082 "Set up an interactive shell.
6083 Mainly sets the prompt and the echo correctly. P is the shell process
6084 to set up. METHOD, USER and HOST specify the connection."
6085 ;; Wait a bit in case the remote end feels like sending a little
6086 ;; junk first. It seems that fencepost.gnu.org does this when doing
6087 ;; a Kerberos login.
6089 (tramp-discard-garbage-erase-buffer p multi-method method user host)
6090 (tramp-process-initial-commands p multi-method method user host
6091 tramp-initial-commands)
6092 ;; It is useful to set the prompt in the following command because
6093 ;; some people have a setting for $PS1 which /bin/sh doesn't know
6094 ;; about and thus /bin/sh will display a strange prompt. For
6095 ;; example, if $PS1 has "${CWD}" in the value, then ksh will display
6096 ;; the current working directory but /bin/sh will display a dollar
6097 ;; sign. The following command line sets $PS1 to a sane value, and
6098 ;; works under Bourne-ish shells as well as csh-like shells. Daniel
6099 ;; Pittman reports that the unusual positioning of the single quotes
6100 ;; makes it work under `rc', too. We also unset the variable $ENV
6101 ;; because that is read by some sh implementations (eg, bash when
6102 ;; called as sh) on startup; this way, we avoid the startup file
6104 (tramp-send-command-internal
6105 multi-method method user host
6106 (format "exec env 'ENV=' 'PS1=$ ' %s"
6107 (tramp-get-method-parameter
6108 multi-method method user host 'tramp-remote-sh))
6109 (format "remote `%s' to come up"
6110 (tramp-get-method-parameter
6111 multi-method method user host 'tramp-remote-sh)))
6112 (tramp-barf-if-no-shell-prompt
6114 "Remote `%s' didn't come up. See buffer `%s' for details"
6115 (tramp-get-method-parameter multi-method method user host 'tramp-remote-sh)
6117 (tramp-message 8 "Setting up remote shell environment")
6118 (tramp-discard-garbage-erase-buffer p multi-method method user host)
6119 (tramp-send-command-internal multi-method method user host
6120 "stty -inlcr -echo kill '^U'")
6122 ;; Ignore garbage after stty command.
6123 (tramp-send-command-internal multi-method method user host
6126 (tramp-send-command-internal multi-method method user host
6127 "TERM=dumb; export TERM")
6129 ;; Check whether the remote host suffers from buggy `send-process-string'.
6130 ;; This is known for FreeBSD (see comment in `send_process', file process.c).
6131 ;; I've tested sending 624 bytes successfully, sending 625 bytes failed.
6132 ;; Emacs makes a hack when this host type is detected locally. It cannot
6133 ;; handle remote hosts, though.
6134 (when (or (not tramp-chunksize) (zerop tramp-chunksize))
6135 (tramp-message 9 "Checking remote host type for `send-process-string' bug")
6136 (tramp-send-command-internal multi-method method user host
6137 "(uname -sr) 2>/dev/null")
6138 (goto-char (point-min))
6139 (when (looking-at "FreeBSD")
6140 (setq tramp-chunksize 500)))
6142 ;; Try to set up the coding system correctly.
6143 ;; CCC this can't be the right way to do it. Hm.
6146 (tramp-message 9 "Determining coding system")
6147 (tramp-send-command-internal multi-method method user host
6148 "echo foo ; echo bar")
6149 (goto-char (point-min))
6150 (if (featurep 'mule)
6151 ;; Use MULE to select the right EOL convention for communicating
6152 ;; with the process.
6153 (let* ((cs (or (process-coding-system p) (cons 'undecided 'undecided)))
6154 cs-decode cs-encode)
6155 (when (symbolp cs) (setq cs (cons cs cs)))
6156 (setq cs-decode (car cs))
6157 (setq cs-encode (cdr cs))
6158 (unless cs-decode (setq cs-decode 'undecided))
6159 (unless cs-encode (setq cs-encode 'undecided))
6160 (setq cs-encode (tramp-coding-system-change-eol-conversion
6162 (when (search-forward "\r" nil t)
6163 (setq cs-decode (tramp-coding-system-change-eol-conversion
6165 (set-buffer-process-coding-system cs-decode cs-encode))
6166 ;; Look for ^M and do something useful if found.
6167 (when (search-forward "\r" nil t)
6168 ;; We have found a ^M but cannot frob the process coding system
6169 ;; because we're running on a non-MULE Emacs. Let's try
6172 (tramp-message 9 "Trying `stty -onlcr'")
6173 (tramp-send-command-internal multi-method method user host
6177 9 "Waiting 30s for `HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE'")
6178 (tramp-send-command-internal
6179 multi-method method user host
6180 "HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE")
6182 (tramp-message 9 "Waiting 30s for `set +o vi +o emacs'")
6183 (tramp-send-command-internal multi-method method user host
6184 "set +o vi +o emacs")
6186 (tramp-message 9 "Waiting 30s for `unset MAIL MAILCHECK MAILPATH'")
6187 (tramp-send-command-internal
6188 multi-method method user host
6189 "unset MAIL MAILCHECK MAILPATH 1>/dev/null 2>/dev/null")
6191 (tramp-message 9 "Waiting 30s for `unset CDPATH'")
6192 (tramp-send-command-internal multi-method method user host
6195 (tramp-message 9 "Setting shell prompt")
6196 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we must
6197 ;; use "\n" here, not tramp-rsh-end-of-line. We also manually frob
6198 ;; the last time we sent a command, to avoid tramp-send-command to send
6199 ;; "echo are you awake".
6200 (setq tramp-last-cmd-time (current-time))
6202 multi-method method user host
6203 (format "PS1='%s%s%s'; PS2=''; PS3=''"
6204 tramp-rsh-end-of-line
6206 tramp-rsh-end-of-line))
6207 (tramp-wait-for-output))
6209 (defun tramp-post-connection (multi-method method user host)
6210 "Prepare a remote shell before being able to work on it.
6211 METHOD, USER and HOST specify the connection.
6212 Among other things, this finds a shell which groks tilde expansion,
6213 tries to find an `ls' command which groks the `-n' option, sets the
6214 locale to C and sets up the remote shell search path."
6215 ;; Search for a good shell before searching for a command which
6216 ;; checks if a file exists. This is done because Tramp wants to use
6217 ;; "test foo; echo $?" to check if various conditions hold, and
6218 ;; there are buggy /bin/sh implementations which don't execute the
6219 ;; "echo $?" part if the "test" part has an error. In particular,
6220 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
6221 ;; with buggy /bin/sh implementations will have a working bash or
6223 (tramp-find-shell multi-method method user host)
6224 ;; Without (sit-for 0.1) at least, my machine will almost always blow
6225 ;; up on 'not numberp /root' - a race that causes the 'echo ~root'
6226 ;; output of (tramp-find-shell) to show up along with the output of
6227 ;; (tramp-find-ls-command) testing.
6229 ;; I can't work out why this is a problem though. The (tramp-wait-for-output)
6230 ;; call in (tramp-find-shell) *should* make this not happen, I thought.
6232 ;; After much debugging I couldn't find any problem with the implementation
6233 ;; of that function though. The workaround stays for me at least. :/
6235 ;; Daniel Pittman <daniel@danann.net>
6238 (tramp-find-file-exists-command multi-method method user host)
6239 (make-local-variable 'tramp-ls-command)
6240 (setq tramp-ls-command (tramp-find-ls-command multi-method method user host))
6241 (unless tramp-ls-command
6244 "Danger! Couldn't find ls which groks -n. Muddling through anyway")
6245 (setq tramp-ls-command
6246 (tramp-find-executable multi-method method user host
6247 "ls" tramp-remote-path nil)))
6248 (unless tramp-ls-command
6249 (error "Fatal error: Couldn't find remote executable `ls'"))
6250 (tramp-message 5 "Using remote command `%s' for getting directory listings"
6252 (tramp-send-command multi-method method user host
6253 (concat "tramp_set_exit_status () {" tramp-rsh-end-of-line
6254 "return $1" tramp-rsh-end-of-line
6256 (tramp-wait-for-output)
6257 ;; Set remote PATH variable.
6258 (tramp-set-remote-path multi-method method user host "PATH" tramp-remote-path)
6259 ;; Tell remote shell to use standard time format, needed for
6260 ;; parsing `ls -l' output.
6261 (tramp-send-command multi-method method user host
6262 "LC_TIME=C; export LC_TIME; echo huhu")
6263 (tramp-wait-for-output)
6264 (tramp-send-command multi-method method user host
6265 "mesg n; echo huhu")
6266 (tramp-wait-for-output)
6267 (tramp-send-command multi-method method user host
6268 "biff n ; echo huhu")
6269 (tramp-wait-for-output)
6270 ;; Unalias ls(1) to work around issues with those silly people who make it
6271 ;; spit out ANSI escapes or whatever.
6272 (tramp-send-command multi-method method user host
6273 "unalias ls; echo huhu")
6274 (tramp-wait-for-output)
6275 ;; Does `test A -nt B' work? Use abominable `find' construct if it
6276 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
6277 ;; for otherwise the shell crashes.
6279 (make-local-variable 'tramp-test-groks-nt)
6280 (tramp-send-command multi-method method user host
6282 (tramp-wait-for-output)
6283 (goto-char (point-min))
6284 (setq tramp-test-groks-nt
6285 (looking-at (format "\n%s\r?\n" (regexp-quote tramp-end-of-output))))
6286 (unless tramp-test-groks-nt
6288 multi-method method user host
6289 (concat "tramp_test_nt () {" tramp-rsh-end-of-line
6290 "test -n \"`find $1 -prune -newer $2 -print`\"" tramp-rsh-end-of-line
6292 (tramp-wait-for-output)
6293 ;; Send the fallback `uudecode' script.
6295 (tramp-send-string multi-method method user host tramp-uudecode)
6296 (tramp-wait-for-output)
6299 (tramp-set-connection-property "perl-scripts" nil multi-method method user host)
6300 (let ((tramp-remote-perl
6301 (or (tramp-find-executable multi-method method user host
6302 "perl5" tramp-remote-path nil)
6303 (tramp-find-executable multi-method method user host
6304 "perl" tramp-remote-path nil))))
6305 (when tramp-remote-perl
6306 (tramp-set-connection-property "perl" tramp-remote-perl
6307 multi-method method user host)
6308 (unless (tramp-method-out-of-band-p multi-method method user host)
6309 (tramp-message 5 "Sending the Perl `mime-encode' implementations.")
6311 multi-method method user host
6312 (concat "tramp_encode () {\n"
6313 (format tramp-perl-encode tramp-remote-perl)
6316 (tramp-wait-for-output)
6318 multi-method method user host
6319 (concat "tramp_encode_with_module () {\n"
6320 (format tramp-perl-encode-with-module tramp-remote-perl)
6323 (tramp-wait-for-output)
6324 (tramp-message 5 "Sending the Perl `mime-decode' implementations.")
6326 multi-method method user host
6327 (concat "tramp_decode () {\n"
6328 (format tramp-perl-decode tramp-remote-perl)
6331 (tramp-wait-for-output)
6333 multi-method method user host
6334 (concat "tramp_decode_with_module () {\n"
6335 (format tramp-perl-decode-with-module tramp-remote-perl)
6338 (tramp-wait-for-output))))
6341 (let ((ln (tramp-find-executable multi-method method user host
6342 "ln" tramp-remote-path nil)))
6344 (tramp-set-connection-property "ln" ln multi-method method user host)))
6347 (tramp-send-command multi-method method user host "id -u; id -g")
6348 (tramp-wait-for-output)
6349 (goto-char (point-min))
6350 (tramp-set-connection-property
6351 "uid" (read (current-buffer)) multi-method method user host)
6352 (tramp-set-connection-property
6353 "gid" (read (current-buffer)) multi-method method user host)
6354 ;; Find the right encoding/decoding commands to use.
6356 (unless (tramp-method-out-of-band-p multi-method method user host)
6357 (tramp-find-inline-encoding multi-method method user host))
6358 ;; If encoding/decoding command are given, test to see if they work.
6359 ;; CCC: Maybe it would be useful to run the encoder both locally and
6360 ;; remotely to see if they produce the same result.
6361 (let ((rem-enc (tramp-get-remote-encoding multi-method method user host))
6362 (rem-dec (tramp-get-remote-decoding multi-method method user host))
6363 (magic-string "xyzzy"))
6364 (when (and (or rem-dec rem-enc) (not (and rem-dec rem-enc)))
6365 (tramp-kill-process multi-method method user host)
6366 ;; Improve error message and/or error check.
6368 "Must give both decoding and encoding command in method definition"))
6369 (when (and rem-enc rem-dec)
6372 "Checking to see if encoding/decoding commands work on remote host...")
6374 multi-method method user host
6375 (format "echo %s | %s | %s"
6376 (tramp-shell-quote-argument magic-string) rem-enc rem-dec))
6377 (tramp-wait-for-output)
6378 (unless (looking-at (regexp-quote magic-string))
6379 (tramp-kill-process multi-method method user host)
6380 (error "Remote host cannot execute de/encoding commands. See buffer `%s' for details"
6384 5 "Checking to see if encoding/decoding commands work on remote host...done"))))
6386 ;; CCC: We should either implement a Perl version of base64 encoding
6387 ;; and decoding. Then we just use that in the last item. The other
6388 ;; alternative is to use the Perl version of UU encoding. But then
6389 ;; we need a Lisp version of uuencode.
6391 ;; Old text from documentation of tramp-methods:
6392 ;; Using a uuencode/uudecode inline method is discouraged, please use one
6393 ;; of the base64 methods instead since base64 encoding is much more
6394 ;; reliable and the commands are more standardized between the different
6395 ;; Unix versions. But if you can't use base64 for some reason, please
6396 ;; note that the default uudecode command does not work well for some
6397 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
6398 ;; the following command for uudecode:
6400 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
6402 ;; For Irix, no solution is known yet.
6404 (defvar tramp-coding-commands
6405 '(("mimencode -b" "mimencode -u -b"
6406 base64-encode-region base64-decode-region)
6407 ("mmencode -b" "mmencode -u -b"
6408 base64-encode-region base64-decode-region)
6409 ("recode data..base64" "recode base64..data"
6410 base64-encode-region base64-decode-region)
6411 ("uuencode xxx" "uudecode -o /dev/stdout"
6412 tramp-uuencode-region uudecode-decode-region)
6413 ("uuencode xxx" "uudecode -o -"
6414 tramp-uuencode-region uudecode-decode-region)
6415 ("uuencode xxx" "uudecode -p"
6416 tramp-uuencode-region uudecode-decode-region)
6417 ("uuencode xxx" "tramp_uudecode"
6418 tramp-uuencode-region uudecode-decode-region)
6419 ("tramp_encode_with_module" "tramp_decode_with_module"
6420 base64-encode-region base64-decode-region)
6421 ("tramp_encode" "tramp_decode"
6422 base64-encode-region base64-decode-region))
6423 "List of coding commands for inline transfer.
6424 Each item is a list that looks like this:
6426 \(REMOTE-ENCODING REMOTE-DECODING LOCAL-ENCODING LOCAL-DECODING)
6428 The REMOTE-ENCODING should be a string, giving a command accepting a
6429 plain file on standard input and writing the encoded file to standard
6430 output. The REMOTE-DECODING should also be a string, giving a command
6431 accepting an encoded file on standard input and writing the decoded
6432 file to standard output.
6434 LOCAL-ENCODING and LOCAL-DECODING can be strings, giving commands, or
6435 symbols, giving functions. If they are strings, then they can contain
6436 the \"%s\" format specifier. If that specifier is present, the input
6437 filename will be put into the command line at that spot. If the
6438 specifier is not present, the input should be read from standard
6441 If they are functions, they will be called with two arguments, start
6442 and end of region, and are expected to replace the region contents
6443 with the encoded or decoded results, respectively.")
6445 (defun tramp-find-inline-encoding (multi-method method user host)
6446 "Find an inline transfer encoding that works.
6447 Goes through the list `tramp-coding-commands'."
6448 (let ((commands tramp-coding-commands)
6451 (while (and commands (null found))
6452 (setq item (pop commands))
6454 (let ((rem-enc (nth 0 item))
6455 (rem-dec (nth 1 item))
6456 (loc-enc (nth 2 item))
6457 (loc-dec (nth 3 item)))
6458 ;; Check if remote encoding and decoding commands can be
6459 ;; called remotely with null input and output. This makes
6460 ;; sure there are no syntax errors and the command is really
6461 ;; found. Note that we do not redirect stdout to /dev/null,
6462 ;; for two reaons: when checking the decoding command, we
6463 ;; actually check the output it gives. And also, when
6464 ;; redirecting "mimencode" output to /dev/null, then as root
6465 ;; it might change the permissions of /dev/null!
6466 (tramp-message-for-buffer
6467 multi-method method user host 9
6468 "Checking remote encoding command `%s' for sanity" rem-enc)
6469 (unless (zerop (tramp-send-command-and-check
6470 multi-method method user host
6471 (format "%s </dev/null" rem-enc) t))
6472 (throw 'wont-work nil))
6473 (tramp-message-for-buffer
6474 multi-method method user host 9
6475 "Checking remote decoding command `%s' for sanity" rem-dec)
6476 (unless (zerop (tramp-send-command-and-check
6477 multi-method method user host
6478 (format "echo %s | %s | %s"
6479 magic rem-enc rem-dec) t))
6480 (throw 'wont-work nil))
6482 (goto-char (point-min))
6483 (unless (looking-at (regexp-quote magic))
6484 (throw 'wont-work nil)))
6485 ;; If the local encoder or decoder is a string, the
6486 ;; corresponding command has to work locally.
6487 (when (stringp loc-enc)
6488 (tramp-message-for-buffer
6489 multi-method method user host 9
6490 "Checking local encoding command `%s' for sanity" loc-enc)
6491 (unless (zerop (tramp-call-local-coding-command
6493 (throw 'wont-work nil)))
6494 (when (stringp loc-dec)
6495 (tramp-message-for-buffer
6496 multi-method method user host 9
6497 "Checking local decoding command `%s' for sanity" loc-dec)
6498 (unless (zerop (tramp-call-local-coding-command
6500 (throw 'wont-work nil)))
6501 ;; CCC: At this point, maybe we should check that the output
6502 ;; of the commands is correct. But for the moment we will
6503 ;; assume that commands working on empty input will also
6504 ;; work in practice.
6505 (setq found item))))
6506 ;; Did we find something? If not, issue error. If so,
6507 ;; set connection properties.
6509 (error "Couldn't find an inline transfer encoding"))
6510 (let ((rem-enc (nth 0 found))
6511 (rem-dec (nth 1 found))
6512 (loc-enc (nth 2 found))
6513 (loc-dec (nth 3 found)))
6514 (tramp-message 10 "Using remote encoding %s" rem-enc)
6515 (tramp-set-remote-encoding multi-method method user host rem-enc)
6516 (tramp-message 10 "Using remote decoding %s" rem-dec)
6517 (tramp-set-remote-decoding multi-method method user host rem-dec)
6518 (tramp-message 10 "Using local encoding %s" loc-enc)
6519 (tramp-set-local-encoding multi-method method user host loc-enc)
6520 (tramp-message 10 "Using local decoding %s" loc-dec)
6521 (tramp-set-local-decoding multi-method method user host loc-dec))))
6523 (defun tramp-call-local-coding-command (cmd input output)
6524 "Call the local encoding or decoding command.
6525 If CMD contains \"%s\", provide input file INPUT there in command.
6526 Otherwise, INPUT is passed via standard input.
6527 INPUT can also be nil which means `/dev/null'.
6528 OUTPUT can be a string (which specifies a filename), or t (which
6529 means standard output and thus the current buffer), or nil (which
6532 tramp-encoding-shell ;program
6533 (when (and input (not (string-match "%s" cmd)))
6535 (if (eq output t) t nil) ;output
6537 tramp-encoding-command-switch
6538 ;; actual shell command
6540 (if (string-match "%s" cmd) (format cmd input) cmd)
6541 (if (stringp output) (concat "> " output) ""))))
6543 (defun tramp-maybe-open-connection (multi-method method user host)
6544 "Maybe open a connection to HOST, logging in as USER, using METHOD.
6545 Does not do anything if a connection is already open, but re-opens the
6546 connection if a previous connection has died for some reason."
6547 (let ((p (get-buffer-process
6548 (tramp-get-buffer multi-method method user host)))
6550 ;; If too much time has passed since last command was sent, look
6551 ;; whether process is still alive. If it isn't, kill it. When
6552 ;; using ssh, it can sometimes happen that the remote end has hung
6553 ;; up but the local ssh client doesn't recognize this until it
6554 ;; tries to send some data to the remote end. So that's why we
6555 ;; try to send a command from time to time, then look again
6556 ;; whether the process is really alive.
6558 (set-buffer (tramp-get-buffer multi-method method user host))
6559 (when (and tramp-last-cmd-time
6560 (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60)
6561 p (processp p) (memq (process-status p) '(run open)))
6563 multi-method method user host "echo are you awake" nil t)
6564 (unless (and (memq (process-status p) '(run open))
6565 (tramp-wait-for-output 10))
6569 (unless (and p (processp p) (memq (process-status p) '(run open)))
6570 (when (and p (processp p))
6572 (let ((process-connection-type tramp-process-connection-type))
6573 (funcall (tramp-get-method-parameter
6575 (tramp-find-method multi-method method user host)
6576 user host 'tramp-connection-function)
6577 multi-method method user host)))))
6579 (defun tramp-send-command
6580 (multi-method method user host command &optional noerase neveropen)
6581 "Send the COMMAND to USER at HOST (logged in using METHOD).
6582 Erases temporary buffer before sending the command (unless NOERASE
6584 If optional seventh arg NEVEROPEN is non-nil, never try to open the
6585 connection. This is meant to be used from
6586 `tramp-maybe-open-connection' only."
6588 (tramp-maybe-open-connection multi-method method user host))
6589 (setq tramp-last-cmd-time (current-time))
6590 (setq tramp-last-cmd command)
6591 (when tramp-debug-buffer
6593 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6594 (goto-char (point-max))
6595 (tramp-insert-with-face 'bold (format "$ %s\n" command))))
6597 (set-buffer (tramp-get-buffer multi-method method user host))
6598 (unless noerase (erase-buffer))
6599 (setq proc (get-buffer-process (current-buffer)))
6600 (process-send-string proc
6601 (concat command tramp-rsh-end-of-line))))
6603 (defun tramp-send-command-internal
6604 (multi-method method user host command &optional msg)
6605 "Send command to remote host and wait for success.
6606 Sends COMMAND, then waits 30 seconds for shell prompt."
6607 (tramp-send-command multi-method method user host command t t)
6609 (tramp-message 9 "Waiting 30s for %s..." msg))
6610 (tramp-barf-if-no-shell-prompt
6612 "Couldn't `%s', see buffer `%s'" command (buffer-name)))
6614 (defun tramp-wait-for-output (&optional timeout)
6615 "Wait for output from remote rsh command."
6616 (let ((proc (get-buffer-process (current-buffer)))
6618 (start-time (current-time))
6619 (start-point (point))
6620 (end-of-output (concat "^"
6621 (regexp-quote tramp-end-of-output)
6623 ;; Algorithm: get waiting output. See if last line contains
6624 ;; end-of-output sentinel. If not, wait a bit and again get
6625 ;; waiting output. Repeat until timeout expires or end-of-output
6626 ;; sentinel is seen. Will hang if timeout is nil and
6627 ;; end-of-output sentinel never appears.
6630 ;; Work around an XEmacs bug, where the timeout expires
6631 ;; faster than it should. This degenerates into polling
6632 ;; for buggy XEmacsen, but oh, well.
6633 (while (and (not found)
6634 (< (tramp-time-diff (current-time) start-time)
6636 (with-timeout (timeout)
6638 (tramp-accept-process-output proc 1)
6639 (unless (memq (process-status proc) '(run open))
6640 (error "Process has died"))
6641 (goto-char (point-max))
6643 (setq found (looking-at end-of-output))))))
6646 (tramp-accept-process-output proc 1)
6647 (unless (memq (process-status proc) '(run open))
6648 (error "Process has died"))
6649 (goto-char (point-max))
6651 (setq found (looking-at end-of-output))))))
6652 ;; At this point, either the timeout has expired or we have found
6653 ;; the end-of-output sentinel.
6655 (goto-char (point-max))
6657 (delete-region (point) (point-max)))
6658 ;; If processing echoes, look for it in the first line and delete.
6659 (when tramp-process-echoes
6661 (goto-char start-point)
6662 (when (looking-at (regexp-quote tramp-last-cmd))
6663 (delete-region (point) (progn (forward-line 1) (point))))))
6664 ;; Add output to debug buffer if appropriate.
6665 (when tramp-debug-buffer
6667 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6668 tramp-current-user tramp-current-host)
6669 (point-min) (point-max))
6673 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6674 tramp-current-user tramp-current-host))
6675 (goto-char (point-max))
6676 (insert "[[Remote prompt `" end-of-output "' not found"
6677 (if timeout (format " in %d secs" timeout) "")
6679 (goto-char (point-min))
6680 ;; Return value is whether end-of-output sentinel was found.
6683 (defun tramp-send-command-and-check (multi-method method user host command
6685 "Run COMMAND and check its exit status.
6686 MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST.
6687 Sends `echo $?' along with the COMMAND for checking the exit status. If
6688 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
6690 If the optional argument SUBSHELL is non-nil, the command is executed in
6691 a subshell, ie surrounded by parentheses."
6692 (tramp-send-command multi-method method user host
6693 (concat (if subshell "( " "")
6695 (if command " 2>/dev/null; " "")
6696 "echo tramp_exit_status $?"
6697 (if subshell " )" " ")))
6698 (tramp-wait-for-output)
6699 (goto-char (point-max))
6700 (unless (search-backward "tramp_exit_status " nil t)
6701 (error "Couldn't find exit status of `%s'" command))
6702 (skip-chars-forward "^ ")
6703 (read (current-buffer)))
6705 (defun tramp-barf-unless-okay (multi-method method user host command subshell
6706 signal fmt &rest args)
6707 "Run COMMAND, check exit status, throw error if exit status not okay.
6708 Similar to `tramp-send-command-and-check' but accepts two more arguments
6709 FMT and ARGS which are passed to `error'."
6710 (unless (zerop (tramp-send-command-and-check
6711 multi-method method user host command subshell))
6712 ;; CCC: really pop-to-buffer? Maybe it's appropriate to be more
6714 (pop-to-buffer (current-buffer))
6715 (funcall 'signal signal (apply 'format fmt args))))
6717 ;; It seems that Tru64 Unix does not like it if long strings are sent
6718 ;; to it in one go. (This happens when sending the Perl
6719 ;; `file-attributes' implementation, for instance.) Therefore, we
6720 ;; have this function which waits a bit at each line.
6721 (defun tramp-send-string
6722 (multi-method method user host string)
6723 "Send the STRING to USER at HOST using METHOD.
6725 The STRING is expected to use Unix line-endings, but the lines sent to
6726 the remote host use line-endings as defined in the variable
6727 `tramp-rsh-end-of-line'."
6728 (let ((proc (get-buffer-process
6729 (tramp-get-buffer multi-method method user host))))
6731 (error "Can't send string to remote host -- not logged in"))
6733 (when tramp-debug-buffer
6735 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6736 (goto-char (point-max))
6737 (tramp-insert-with-face 'bold (format "$ %s\n" string))))
6738 ;; replace "\n" by `tramp-rsh-end-of-line'
6740 (mapconcat 'identity
6741 (split-string string "\n")
6742 tramp-rsh-end-of-line))
6743 (unless (or (string= string "")
6744 (string-equal (substring string -1) tramp-rsh-end-of-line))
6745 (setq string (concat string tramp-rsh-end-of-line)))
6747 (if (and tramp-chunksize (not (zerop tramp-chunksize)))
6749 (end (length string)))
6751 (tramp-message-for-buffer
6752 multi-method method user host 10
6753 "Sending chunk from %s to %s"
6754 pos (min (+ pos tramp-chunksize) end))
6755 (process-send-string
6756 proc (substring string pos (min (+ pos tramp-chunksize) end)))
6757 (setq pos (+ pos tramp-chunksize))
6759 (process-send-string proc string))))
6761 (defun tramp-send-eof (multi-method method user host)
6762 "Send EOF to the remote end.
6763 METHOD, HOST and USER specify the connection."
6764 (let ((proc (get-buffer-process
6765 (tramp-get-buffer multi-method method user host))))
6767 (error "Can't send EOF to remote host -- not logged in"))
6768 (process-send-eof proc)))
6769 ; (process-send-string proc "\^D")))
6771 (defun tramp-kill-process (multi-method method user host)
6772 "Kill the connection process used by Tramp.
6773 MULTI-METHOD, METHOD, USER, and HOST specify the connection."
6774 (let ((proc (get-buffer-process
6775 (tramp-get-buffer multi-method method user host))))
6776 (kill-process proc)))
6778 (defun tramp-discard-garbage-erase-buffer (p multi-method method user host)
6779 "Erase buffer, then discard subsequent garbage.
6780 If `tramp-discard-garbage' is nil, just erase buffer."
6781 (if (not tramp-discard-garbage)
6783 (while (prog1 (erase-buffer) (tramp-accept-process-output p 0.25))
6784 (when tramp-debug-buffer
6786 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6787 (goto-char (point-max))
6788 (tramp-insert-with-face
6789 'bold (format "Additional characters detected\n")))))))
6791 (defun tramp-mode-string-to-int (mode-string)
6792 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
6793 (let* ((mode-chars (string-to-vector mode-string))
6794 (owner-read (aref mode-chars 1))
6795 (owner-write (aref mode-chars 2))
6796 (owner-execute-or-setid (aref mode-chars 3))
6797 (group-read (aref mode-chars 4))
6798 (group-write (aref mode-chars 5))
6799 (group-execute-or-setid (aref mode-chars 6))
6800 (other-read (aref mode-chars 7))
6801 (other-write (aref mode-chars 8))
6802 (other-execute-or-sticky (aref mode-chars 9)))
6806 (?r (tramp-octal-to-decimal "00400")) (?- 0)
6807 (t (error "Second char `%c' must be one of `r-'" owner-read)))
6809 (?w (tramp-octal-to-decimal "00200")) (?- 0)
6810 (t (error "Third char `%c' must be one of `w-'" owner-write)))
6811 (case owner-execute-or-setid
6812 (?x (tramp-octal-to-decimal "00100"))
6813 (?S (tramp-octal-to-decimal "04000"))
6814 (?s (tramp-octal-to-decimal "04100"))
6816 (t (error "Fourth char `%c' must be one of `xsS-'"
6817 owner-execute-or-setid)))
6819 (?r (tramp-octal-to-decimal "00040")) (?- 0)
6820 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
6822 (?w (tramp-octal-to-decimal "00020")) (?- 0)
6823 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
6824 (case group-execute-or-setid
6825 (?x (tramp-octal-to-decimal "00010"))
6826 (?S (tramp-octal-to-decimal "02000"))
6827 (?s (tramp-octal-to-decimal "02010"))
6829 (t (error "Seventh char `%c' must be one of `xsS-'"
6830 group-execute-or-setid)))
6832 (?r (tramp-octal-to-decimal "00004")) (?- 0)
6833 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
6835 (?w (tramp-octal-to-decimal "00002")) (?- 0)
6836 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
6837 (case other-execute-or-sticky
6838 (?x (tramp-octal-to-decimal "00001"))
6839 (?T (tramp-octal-to-decimal "01000"))
6840 (?t (tramp-octal-to-decimal "01001"))
6842 (t (error "Tenth char `%c' must be one of `xtT-'"
6843 other-execute-or-sticky)))))))
6845 (defun tramp-convert-file-attributes (multi-method method user host attr)
6846 "Convert file-attributes ATTR generated by perl script or ls.
6847 Convert file mode bits to string and set virtual device number.
6849 ;; Convert file mode bits to string.
6850 (unless (stringp (nth 8 attr))
6851 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr))))
6852 ;; Set file's gid change bit. Possible only when id-format is 'integer.
6853 (when (numberp (nth 3 attr))
6854 (setcar (nthcdr 9 attr)
6855 (not (eql (nth 3 attr)
6856 (tramp-get-remote-gid multi-method method user host)))))
6857 ;; Set virtual device number.
6858 (setcar (nthcdr 11 attr)
6859 (tramp-get-device multi-method method user host))
6862 (defun tramp-get-device (multi-method method user host)
6863 "Returns the virtual device number.
6864 If it doesn't exist, generate a new one."
6865 (let ((string (tramp-make-tramp-file-name multi-method method user host "")))
6866 (unless (assoc string tramp-devices)
6867 (add-to-list 'tramp-devices
6868 (list string (length tramp-devices))))
6869 (list -1 (nth 1 (assoc string tramp-devices)))))
6871 (defun tramp-file-mode-from-int (mode)
6872 "Turn an integer representing a file mode into an ls(1)-like string."
6873 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
6874 (user (logand (lsh mode -6) 7))
6875 (group (logand (lsh mode -3) 7))
6876 (other (logand (lsh mode -0) 7))
6877 (suid (> (logand (lsh mode -9) 4) 0))
6878 (sgid (> (logand (lsh mode -9) 2) 0))
6879 (sticky (> (logand (lsh mode -9) 1) 0)))
6880 (setq user (tramp-file-mode-permissions user suid "s"))
6881 (setq group (tramp-file-mode-permissions group sgid "s"))
6882 (setq other (tramp-file-mode-permissions other sticky "t"))
6883 (concat type user group other)))
6886 (defun tramp-file-mode-permissions (perm suid suid-text)
6887 "Convert a permission bitset into a string.
6888 This is used internally by `tramp-file-mode-from-int'."
6889 (let ((r (> (logand perm 4) 0))
6890 (w (> (logand perm 2) 0))
6891 (x (> (logand perm 1) 0)))
6892 (concat (or (and r "r") "-")
6893 (or (and w "w") "-")
6894 (or (and suid x suid-text) ; suid, execute
6895 (and suid (upcase suid-text)) ; suid, !execute
6896 (and x "x") "-")))) ; !suid
6899 (defun tramp-decimal-to-octal (i)
6900 "Return a string consisting of the octal digits of I.
6901 Not actually used. Use `(format \"%o\" i)' instead?"
6902 (cond ((< i 0) (error "Cannot convert negative number to octal"))
6903 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
6905 (t (concat (tramp-decimal-to-octal (/ i 8))
6906 (number-to-string (% i 8))))))
6909 ;;(defun tramp-octal-to-decimal (ostr)
6910 ;; "Given a string of octal digits, return a decimal number."
6911 ;; (cond ((null ostr) 0)
6912 ;; ((string= "" ostr) 0)
6913 ;; (t (let ((last (aref ostr (1- (length ostr))))
6914 ;; (rest (substring ostr 0 (1- (length ostr)))))
6915 ;; (unless (and (>= last ?0)
6917 ;; (error "Not an octal digit: %c" last))
6918 ;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest)))))))
6919 ;; Kudos to Gerd Moellmann for this suggestion.
6920 (defun tramp-octal-to-decimal (ostr)
6921 "Given a string of octal digits, return a decimal number."
6922 (let ((x (or ostr "")))
6923 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
6924 (unless (string-match "\\`[0-7]*\\'" x)
6925 (error "Non-octal junk in string `%s'" x))
6926 (string-to-number ostr 8)))
6928 (defun tramp-shell-case-fold (string)
6929 "Converts STRING to shell glob pattern which ignores case."
6932 (if (equal (downcase c) (upcase c))
6934 (format "[%c%c]" (downcase c) (upcase c))))
6939 ;; ------------------------------------------------------------
6940 ;; -- TRAMP file names --
6941 ;; ------------------------------------------------------------
6942 ;; Conversion functions between external representation and
6943 ;; internal data structure. Convenience functions for internal
6946 (defstruct tramp-file-name multi-method method user host localname)
6948 (defun tramp-tramp-file-p (name)
6949 "Return t iff NAME is a tramp file."
6951 (string-match tramp-file-name-regexp name)))
6953 ;; HHH: Changed. Used to assign the return value of (user-login-name)
6954 ;; to the `user' part of the structure if a user name was not
6955 ;; provided, now it assigns nil.
6956 (defun tramp-dissect-file-name (name)
6957 "Return an `tramp-file-name' structure.
6958 The structure consists of remote method, remote user, remote host and
6959 localname (file name on remote host)."
6961 (let* ((match (string-match (nth 0 tramp-file-name-structure) name))
6964 (if match (match-string (nth 1 tramp-file-name-structure) name)
6967 (format (nth 0 tramp-multi-file-name-structure)
6968 (nth 0 tramp-multi-file-name-hop-structure)) name)
6969 (match-string (nth 1 tramp-multi-file-name-structure) name))))
6970 (if (and method (member method tramp-multi-methods))
6971 ;; If it's a multi method, the file name structure contains
6972 ;; arrays of method, user and host.
6973 (tramp-dissect-multi-file-name name)
6974 ;; Normal method. First, find out default method.
6975 (unless match (error "Not a tramp file name: %s" name))
6976 (let ((user (match-string (nth 2 tramp-file-name-structure) name))
6977 (host (match-string (nth 3 tramp-file-name-structure) name))
6978 (localname (match-string (nth 4 tramp-file-name-structure) name)))
6979 (make-tramp-file-name
6984 :localname localname))))))
6986 (defun tramp-find-default-method (user host)
6987 "Look up the right method to use in `tramp-default-method-alist'."
6988 (let ((choices tramp-default-method-alist)
6989 (method tramp-default-method)
6992 (setq item (pop choices))
6993 (when (and (string-match (or (nth 0 item) "") (or host ""))
6994 (string-match (or (nth 1 item) "") (or user "")))
6995 (setq method (nth 2 item))
6996 (setq choices nil)))
6999 (defun tramp-find-method (multi-method method user host)
7000 "Return the right method string to use.
7001 This is MULTI-METHOD, if non-nil. Otherwise, it is METHOD, if non-nil.
7002 If both MULTI-METHOD and METHOD are nil, do a lookup in
7003 `tramp-default-method-alist'."
7004 (or multi-method method (tramp-find-default-method user host)))
7006 ;; HHH: Not Changed. Multi method. Will probably not handle the case where
7007 ;; a user name is not provided in the "file name" very well.
7008 (defun tramp-dissect-multi-file-name (name)
7009 "Not implemented yet."
7010 (let ((regexp (nth 0 tramp-multi-file-name-structure))
7011 (method-index (nth 1 tramp-multi-file-name-structure))
7012 (hops-index (nth 2 tramp-multi-file-name-structure))
7013 (localname-index (nth 3 tramp-multi-file-name-structure))
7014 (hop-regexp (nth 0 tramp-multi-file-name-hop-structure))
7015 (hop-method-index (nth 1 tramp-multi-file-name-hop-structure))
7016 (hop-user-index (nth 2 tramp-multi-file-name-hop-structure))
7017 (hop-host-index (nth 3 tramp-multi-file-name-hop-structure))
7018 method hops len hop-methods hop-users hop-hosts localname)
7019 (unless (string-match (format regexp hop-regexp) name)
7020 (error "Not a multi tramp file name: %s" name))
7021 (setq method (match-string method-index name))
7022 (setq hops (match-string hops-index name))
7023 (setq len (/ (length (match-data t)) 2))
7024 (when (< localname-index 0) (incf localname-index len))
7025 (setq localname (match-string localname-index name))
7027 (while (string-match hop-regexp hops index)
7028 (setq index (match-end 0))
7030 (cons (match-string hop-method-index hops) hop-methods))
7032 (cons (match-string hop-user-index hops) hop-users))
7034 (cons (match-string hop-host-index hops) hop-hosts))))
7035 (make-tramp-file-name
7036 :multi-method method
7037 :method (apply 'vector (reverse hop-methods))
7038 :user (apply 'vector (reverse hop-users))
7039 :host (apply 'vector (reverse hop-hosts))
7040 :localname localname)))
7042 (defun tramp-make-tramp-file-name (multi-method method user host localname)
7043 "Constructs a tramp file name from METHOD, USER, HOST and LOCALNAME."
7045 (tramp-make-tramp-multi-file-name multi-method method user host localname)
7047 (concat tramp-prefix-format
7048 (when method (concat "%m" tramp-postfix-single-method-format))
7049 (when user (concat "%u" tramp-postfix-user-format))
7050 (when host (concat "%h" tramp-postfix-host-format))
7051 (when localname (concat "%p")))
7052 `((?m . ,method) (?u . ,user) (?h . ,host) (?p . ,localname)))))
7054 ;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done
7055 ;; with this when USER is nil?
7056 (defun tramp-make-tramp-multi-file-name (multi-method method user host localname)
7057 "Constructs a tramp file name for a multi-hop method."
7058 (unless tramp-make-multi-tramp-file-format
7059 (error "`tramp-make-multi-tramp-file-format' is nil"))
7060 (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format))
7061 (hop-format (nth 1 tramp-make-multi-tramp-file-format))
7062 (localname-format (nth 2 tramp-make-multi-tramp-file-format))
7063 (prefix (format-spec prefix-format `((?m . ,multi-method))))
7065 (localname (format-spec localname-format `((?p . ,localname))))
7067 (len (length method)))
7069 (let ((m (aref method i)) (u (aref user i)) (h (aref host i)))
7070 (setq hops (concat hops (format-spec hop-format
7071 `((?m . ,m) (?u . ,u) (?h . ,h)))))
7073 (concat prefix hops localname)))
7075 (defun tramp-make-copy-program-file-name (user host localname)
7076 "Create a file name suitable to be passed to `rcp' and workalikes."
7078 (format "%s@%s:%s" user host localname)
7079 (format "%s:%s" host localname)))
7081 (defun tramp-method-out-of-band-p (multi-method method user host)
7082 "Return t if this is an out-of-band method, nil otherwise."
7083 (tramp-get-method-parameter
7085 (tramp-find-method multi-method method user host)
7086 user host 'tramp-copy-program))
7088 ;; Variables local to connection.
7090 (defun tramp-get-ls-command (multi-method method user host)
7092 (tramp-maybe-open-connection multi-method method user host)
7093 (set-buffer (tramp-get-buffer multi-method method user host))
7096 (defun tramp-get-test-groks-nt (multi-method method user host)
7098 (tramp-maybe-open-connection multi-method method user host)
7099 (set-buffer (tramp-get-buffer multi-method method user host))
7100 tramp-test-groks-nt))
7102 (defun tramp-get-file-exists-command (multi-method method user host)
7104 (tramp-maybe-open-connection multi-method method user host)
7105 (set-buffer (tramp-get-buffer multi-method method user host))
7106 tramp-file-exists-command))
7108 (defun tramp-get-remote-perl (multi-method method user host)
7109 (tramp-get-connection-property "perl" nil multi-method method user host))
7111 (defun tramp-get-remote-ln (multi-method method user host)
7112 (tramp-get-connection-property "ln" nil multi-method method user host))
7114 (defun tramp-get-remote-uid (multi-method method user host)
7115 (tramp-get-connection-property "uid" nil multi-method method user host))
7117 (defun tramp-get-remote-gid (multi-method method user host)
7118 (tramp-get-connection-property "gid" nil multi-method method user host))
7120 ;; Get a property of a TRAMP connection.
7121 (defun tramp-get-connection-property
7122 (property default multi-method method user host)
7123 "Get the named property for the connection.
7124 If the value is not set for the connection, return `default'"
7125 (tramp-maybe-open-connection multi-method method user host)
7126 (with-current-buffer (tramp-get-buffer multi-method method user host)
7129 (symbol-value (intern (concat "tramp-connection-property-" property)))
7132 ;; Set a property of a TRAMP connection.
7133 (defun tramp-set-connection-property
7134 (property value multi-method method user host)
7135 "Set the named property of a TRAMP connection."
7136 (tramp-maybe-open-connection multi-method method user host)
7137 (with-current-buffer (tramp-get-buffer multi-method method user host)
7138 (set (make-local-variable
7139 (intern (concat "tramp-connection-property-" property)))
7142 ;; Some predefined connection properties.
7143 (defun tramp-set-remote-encoding (multi-method method user host rem-enc)
7144 (tramp-set-connection-property "remote-encoding" rem-enc
7145 multi-method method user host))
7146 (defun tramp-get-remote-encoding (multi-method method user host)
7147 (tramp-get-connection-property "remote-encoding" nil
7148 multi-method method user host))
7150 (defun tramp-set-remote-decoding (multi-method method user host rem-dec)
7151 (tramp-set-connection-property "remote-decoding" rem-dec
7152 multi-method method user host))
7153 (defun tramp-get-remote-decoding (multi-method method user host)
7154 (tramp-get-connection-property "remote-decoding" nil
7155 multi-method method user host))
7157 (defun tramp-set-local-encoding (multi-method method user host loc-enc)
7158 (tramp-set-connection-property "local-encoding" loc-enc
7159 multi-method method user host))
7160 (defun tramp-get-local-encoding (multi-method method user host)
7161 (tramp-get-connection-property "local-encoding" nil
7162 multi-method method user host))
7164 (defun tramp-set-local-decoding (multi-method method user host loc-dec)
7165 (tramp-set-connection-property "local-decoding" loc-dec
7166 multi-method method user host))
7167 (defun tramp-get-local-decoding (multi-method method user host)
7168 (tramp-get-connection-property "local-decoding" nil
7169 multi-method method user host))
7171 (defun tramp-get-method-parameter (multi-method method user host param)
7172 "Return the method parameter PARAM.
7173 If the `tramp-methods' entry does not exist, use the variable PARAM
7175 (unless (boundp param)
7176 (error "Non-existing method parameter `%s'" param))
7177 (let ((entry (assoc param
7178 (assoc (tramp-find-method multi-method method user host)
7182 (symbol-value param))))
7185 ;; Auto saving to a special directory.
7187 (defun tramp-exists-file-name-handler (operation &rest args)
7188 (let ((buffer-file-name "/")
7189 (fnha file-name-handler-alist)
7190 (check-file-name-operation operation)
7191 (file-name-handler-alist
7194 '(lambda (operation &rest args)
7195 "Returns OPERATION if it is the one to be checked"
7196 (if (equal check-file-name-operation operation)
7198 (let ((file-name-handler-alist fnha))
7199 (apply operation args))))))))
7200 (eq (apply operation args) operation)))
7202 (unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
7203 (defadvice make-auto-save-file-name
7204 (around tramp-advice-make-auto-save-file-name () activate)
7205 "Invoke `tramp-handle-make-auto-save-file-name' for tramp files."
7206 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name)))
7207 (setq ad-return-value (tramp-handle-make-auto-save-file-name))
7209 (add-hook 'tramp-unload-hook
7210 '(lambda () (ad-unadvise 'make-auto-save-file-name))))
7212 ;; In Emacs < 22 and XEmacs < 21.5 autosaved remote files have
7213 ;; permission 0666 minus umask. This is a security threat.
7215 (defun tramp-set-auto-save-file-modes ()
7216 "Set permissions of autosaved remote files to the original permissions."
7217 (let ((bfn (buffer-file-name)))
7218 (when (and (stringp bfn)
7219 (tramp-tramp-file-p bfn)
7220 (stringp buffer-auto-save-file-name)
7221 (not (equal bfn buffer-auto-save-file-name)))
7222 (unless (file-exists-p buffer-auto-save-file-name)
7223 (write-region "" nil buffer-auto-save-file-name))
7224 ;; Permissions should be set always, because there might be an old
7225 ;; auto-saved file belonging to another original file. This could
7226 ;; be a security threat.
7227 (set-file-modes buffer-auto-save-file-name
7228 (or (file-modes bfn) (tramp-octal-to-decimal "0600"))))))
7230 (unless (or (> emacs-major-version 21)
7231 (and (featurep 'xemacs)
7232 (= emacs-major-version 21)
7233 (> emacs-minor-version 4)))
7234 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes)
7235 (add-hook 'tramp-unload-hook
7237 (remove-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))))
7239 (defun tramp-subst-strs-in-string (alist string)
7240 "Replace all occurrences of the string FROM with TO in STRING.
7241 ALIST is of the form ((FROM . TO) ...)."
7244 (let* ((pr (car alist))
7247 (while (string-match (regexp-quote from) string)
7248 (setq string (replace-match to t t string)))
7249 (setq alist (cdr alist))))
7252 (defun tramp-insert-with-face (face string)
7253 "Insert text with a specific face."
7254 (let ((start (point)))
7256 (add-text-properties start (point) (list 'face face))))
7258 ;; ------------------------------------------------------------
7259 ;; -- Compatibility functions section --
7260 ;; ------------------------------------------------------------
7262 (defun tramp-temporary-file-directory ()
7263 "Return name of directory for temporary files (compat function).
7264 For Emacs, this is the variable `temporary-file-directory', for XEmacs
7265 this is the function `temp-directory'."
7266 (cond ((boundp 'temporary-file-directory)
7267 (symbol-value 'temporary-file-directory))
7268 ((fboundp 'temp-directory)
7269 (funcall (symbol-function 'temp-directory))) ;pacify byte-compiler
7270 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
7271 (file-name-as-directory (getenv "TEMP")))
7272 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
7273 (file-name-as-directory (getenv "TMP")))
7274 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
7275 (file-name-as-directory (getenv "TMPDIR")))
7276 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
7277 (t (message (concat "Neither `temporary-file-directory' nor "
7278 "`temp-directory' is defined -- using /tmp."))
7279 (file-name-as-directory "/tmp"))))
7281 (defun tramp-read-passwd (user host prompt)
7282 "Read a password from user (compat function).
7283 Invokes `password-read' if available, `read-passwd' else."
7284 (if (functionp 'password-read)
7285 (let* ((key (concat (or user (user-login-name)) "@" host))
7286 (password (apply #'password-read (list prompt key))))
7287 (apply #'password-cache-add (list key password))
7289 (read-passwd prompt)))
7291 (defun tramp-clear-passwd (&optional user host)
7292 "Clear password cache for connection related to current-buffer."
7294 (let ((filename (or buffer-file-name list-buffers-directory "")))
7295 (when (and (functionp 'password-cache-remove)
7296 (or (and user host) (tramp-tramp-file-p filename)))
7297 (let* ((v (when (tramp-tramp-file-p filename)
7298 (tramp-dissect-file-name filename)))
7299 (luser (or user (tramp-file-name-user v) (user-login-name)))
7300 (lhost (or host (tramp-file-name-host v) (system-name)))
7301 (key (concat luser "@" lhost)))
7302 (apply #'password-cache-remove (list key))))))
7304 (defun tramp-time-diff (t1 t2)
7305 "Return the difference between the two times, in seconds.
7306 T1 and T2 are time values (as returned by `current-time' for example)."
7307 ;; Pacify byte-compiler with `symbol-function'.
7308 (cond ((and (fboundp 'subtract-time)
7309 (fboundp 'float-time))
7310 (funcall (symbol-function 'float-time)
7311 (funcall (symbol-function 'subtract-time) t1 t2)))
7312 ((and (fboundp 'subtract-time)
7313 (fboundp 'time-to-seconds))
7314 (funcall (symbol-function 'time-to-seconds)
7315 (funcall (symbol-function 'subtract-time) t1 t2)))
7316 ((fboundp 'itimer-time-difference)
7317 (funcall (symbol-function 'itimer-time-difference)
7318 (if (< (length t1) 3) (append t1 '(0)) t1)
7319 (if (< (length t2) 3) (append t2 '(0)) t2)))
7321 ;; snarfed from Emacs 21 time-date.el; combining
7322 ;; time-to-seconds and subtract-time
7323 (let ((time (let ((borrow (< (cadr t1) (cadr t2))))
7324 (list (- (car t1) (car t2) (if borrow 1 0))
7325 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))))
7326 (+ (* (car time) 65536.0)
7328 (/ (or (nth 2 time) 0) 1000000.0))))))
7330 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
7331 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
7332 EOL-TYPE can be one of `dos', `unix', or `mac'."
7333 (cond ((fboundp 'coding-system-change-eol-conversion)
7334 (apply #'coding-system-change-eol-conversion
7335 (list coding-system eol-type)))
7336 ((fboundp 'subsidiary-coding-system)
7338 #'subsidiary-coding-system
7340 (cond ((eq eol-type 'dos) 'crlf)
7341 ((eq eol-type 'unix) 'lf)
7342 ((eq eol-type 'mac) 'cr)
7344 (error "Unknown EOL-TYPE `%s', must be %s"
7346 "`dos', `unix', or `mac'"))))))
7347 (t (error "Can't change EOL conversion -- is MULE missing?"))))
7349 (defun tramp-split-string (string pattern)
7350 "Like `split-string' but omit empty strings.
7351 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
7352 This is, the first, empty, element is omitted. In XEmacs, the first
7353 element is not omitted.
7355 Note: this function has been written for `tramp-handle-file-truename'.
7356 If you want to use it for something else, you'll have to check whether
7357 it does the right thing."
7358 (delete "" (split-string string pattern)))
7360 (defun tramp-set-process-query-on-exit-flag (process flag)
7361 "Specify if query is needed for process when Emacs is exited.
7362 If the second argument flag is non-nil, Emacs will query the user before
7363 exiting if process is running."
7365 (if (fboundp 'set-process-query-on-exit-flag)
7366 (symbol-function 'set-process-query-on-exit-flag)
7367 (symbol-function 'process-kill-without-query))
7371 ;; ------------------------------------------------------------
7372 ;; -- Kludges section --
7373 ;; ------------------------------------------------------------
7375 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
7376 ;; does not deal well with newline characters. Newline is replaced by
7377 ;; backslash newline. But if, say, the string `a backslash newline b'
7378 ;; is passed to a shell, the shell will expand this into "ab",
7379 ;; completely omitting the newline. This is not what was intended.
7380 ;; It does not appear to be possible to make the function
7381 ;; `shell-quote-argument' work with newlines without making it
7382 ;; dependent on the shell used. But within this package, we know that
7383 ;; we will always use a Bourne-like shell, so we use an approach which
7386 ;; The approach is simple: we call `shell-quote-argument', then
7387 ;; massage the newline part of the result.
7389 ;; This function should produce a string which is grokked by a Unix
7390 ;; shell, even if the Emacs is running on Windows. Since this is the
7391 ;; kludges section, we bind `system-type' in such a way that
7392 ;; `shell-quote-arguments' behaves as if on Unix.
7394 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
7395 ;; function to work with Bourne-like shells.
7397 ;; CCC: This function should be rewritten so that
7398 ;; `shell-quote-argument' is not used. This way, we are safe from
7399 ;; changes in `shell-quote-argument'.
7400 (defun tramp-shell-quote-argument (s)
7401 "Similar to `shell-quote-argument', but groks newlines.
7402 Only works for Bourne-like shells."
7403 (let ((system-type 'not-windows))
7405 (let ((result (shell-quote-argument s))
7406 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
7407 (when (and (>= (length result) 2)
7408 (string= (substring result 0 2) "\\~"))
7409 (setq result (substring result 1)))
7410 (while (string-match nl result)
7411 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
7415 ;; ;; EFS hooks itself into the file name handling stuff in more places
7416 ;; ;; than just `file-name-handler-alist'. The following tells EFS to stay
7417 ;; ;; away from tramp.el file names.
7419 ;; ;; This is needed because EFS installs (efs-dired-before-readin) into
7420 ;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP
7421 ;; ;; connection to help it's dired process. Not that I have any real
7422 ;; ;; idea *why* this is helpful to dired.
7424 ;; ;; Anyway, this advice fixes the problem (with a sledgehammer :)
7426 ;; ;; Daniel Pittman <daniel@danann.net>
7428 ;; ;; CCC: when the other defadvice calls have disappeared, make sure
7429 ;; ;; not to call defadvice unless it's necessary. How do we find out whether
7430 ;; ;; it is necessary? (featurep 'efs) is surely the wrong way --
7431 ;; ;; EFS might nicht be loaded yet.
7432 ;; (defadvice efs-ftp-path (around dont-match-tramp-localname activate protect)
7433 ;; "Cause efs-ftp-path to fail when the path is a TRAMP localname."
7434 ;; (if (tramp-tramp-file-p (ad-get-arg 0))
7438 ;; We currently (sometimes) use "[" and "]" in the filename format.
7439 ;; This means that Emacs wants to expand wildcards if
7440 ;; `find-file-wildcards' is non-nil, and then barfs because no
7441 ;; expansion could be found. We detect this situation and do
7442 ;; something really awful: we have `file-expand-wildcards' return the
7443 ;; original filename if it can't expand anything. Let's just hope
7444 ;; that this doesn't break anything else.
7445 ;; CCC: This check is now also really awful; we should search all
7446 ;; of the filename format, not just the prefix.
7447 (when (string-match "\\[" tramp-prefix-format)
7448 (defadvice file-expand-wildcards (around tramp-fix activate)
7449 (let ((name (ad-get-arg 0)))
7450 (if (tramp-tramp-file-p name)
7451 ;; If it's a Tramp file, dissect it and look if wildcards
7452 ;; need to be expanded at all.
7453 (let ((v (tramp-dissect-file-name name)))
7454 (if (string-match "[[*?]" (tramp-file-name-localname v))
7455 (let ((res ad-do-it))
7456 (setq ad-return-value (or res (list name))))
7457 (setq ad-return-value (list name))))
7458 ;; If it is not a Tramp file, just run the original function.
7459 (let ((res ad-do-it))
7460 (setq ad-return-value (or res (list name)))))))
7461 (add-hook 'tramp-unload-hook
7462 '(lambda () (ad-unadvise 'file-expand-wildcards))))
7464 ;; Tramp version is useful in a number of situations.
7466 (defun tramp-version (arg)
7467 "Print version number of tramp.el in minibuffer or current buffer."
7469 (if arg (insert tramp-version) (message tramp-version)))
7471 ;; Make the `reporter` functionality available for making bug reports about
7472 ;; the package. A most useful piece of code.
7474 (unless (fboundp 'reporter-submit-bug-report)
7475 (autoload 'reporter-submit-bug-report "reporter"))
7478 "Submit a bug report to the TRAMP developers."
7482 (let ((reporter-prompt-for-summary-p t))
7483 (reporter-submit-bug-report
7484 tramp-bug-report-address ; to-address
7485 (format "tramp (%s)" tramp-version) ; package name and version
7490 tramp-file-exists-command
7491 tramp-current-multi-method
7492 tramp-current-method
7497 tramp-auto-save-directory ; vars to dump
7498 tramp-default-method
7499 tramp-rsh-end-of-line
7500 tramp-default-password-end-of-line
7502 tramp-login-prompt-regexp
7503 ;; Mask non-7bit characters
7504 (tramp-password-prompt-regexp . tramp-reporter-dump-variable)
7505 tramp-wrong-passwd-regexp
7506 tramp-yesno-prompt-regexp
7507 tramp-yn-prompt-regexp
7508 tramp-terminal-prompt-regexp
7509 tramp-temp-name-prefix
7510 tramp-file-name-structure
7511 tramp-file-name-regexp
7512 tramp-multi-file-name-structure
7513 tramp-multi-file-name-hop-structure
7515 tramp-multi-connection-function-alist
7518 tramp-coding-commands
7519 tramp-actions-before-shell
7520 tramp-actions-copy-out-of-band
7523 ;; Mask non-7bit characters
7524 (tramp-shell-prompt-pattern . tramp-reporter-dump-variable)
7526 ,(when (boundp 'tramp-backup-directory-alist)
7527 'tramp-backup-directory-alist)
7528 ,(when (boundp 'tramp-bkup-backup-directory-info)
7529 'tramp-bkup-backup-directory-info)
7531 ;; Non-tramp variables of interest
7532 ;; Mask non-7bit characters
7533 (shell-prompt-pattern . tramp-reporter-dump-variable)
7535 backup-by-copying-when-linked
7536 backup-by-copying-when-mismatch
7537 ,(when (boundp 'backup-by-copying-when-privileged-mismatch)
7538 'backup-by-copying-when-privileged-mismatch)
7539 ,(when (boundp 'password-cache)
7541 ,(when (boundp 'password-cache-expiry)
7542 'password-cache-expiry)
7543 ,(when (boundp 'backup-directory-alist)
7544 'backup-directory-alist)
7545 ,(when (boundp 'bkup-backup-directory-info)
7546 'bkup-backup-directory-info)
7547 file-name-handler-alist))
7549 'tramp-load-report-modules ; pre-hook
7550 'tramp-append-tramp-buffers ; post-hook
7552 Enter your bug report in this message, including as much detail as you
7553 possibly can about the problem, what you did to cause it and what the
7554 local and remote machines are.
7556 If you can give a simple set of instructions to make this bug happen
7557 reliably, please include those. Thank you for helping kill bugs in
7560 Another useful thing to do is to put (setq tramp-debug-buffer t) in
7561 the ~/.emacs file and to repeat the bug. Then, include the contents
7562 of the *tramp/foo* buffer and the *debug tramp/foo* buffer in your bug
7565 --bug report follows this line--
7568 (defun tramp-reporter-dump-variable (varsym mailbuf)
7569 "Pretty-print the value of the variable in symbol VARSYM.
7570 Used for non-7bit chars in strings."
7571 (let* ((reporter-eval-buffer (symbol-value 'reporter-eval-buffer))
7572 (val (with-current-buffer reporter-eval-buffer
7573 (symbol-value varsym))))
7575 ;; There are characters to be masked.
7576 (when (and (boundp 'mm-7bit-chars)
7578 (concat "[^" (symbol-value 'mm-7bit-chars) "]") val))
7579 (with-current-buffer reporter-eval-buffer
7580 (set varsym (concat "(base64-decode-string \""
7581 (base64-encode-string val)
7585 (funcall (symbol-function 'reporter-dump-variable) varsym mailbuf)
7587 ;; Remove string quotation.
7590 (concat "\\(^.*\\)" "\"" ;; \1 "
7591 "\\((base64-decode-string \\)" "\\\\" ;; \2 \
7592 "\\(\".*\\)" "\\\\" ;; \3 \
7593 "\\(\")\\)" "\"$")) ;; \4 "
7594 (replace-match "\\1\\2\\3\\4")
7596 (insert " ;; variable encoded due to non-printable characters\n"))
7599 ;; Reset VARSYM to old value.
7600 (with-current-buffer reporter-eval-buffer
7603 (defun tramp-load-report-modules ()
7604 "Load needed modules for reporting."
7606 ;; We load message.el and mml.el from Gnus.
7607 (if (featurep 'xemacs)
7609 (load "message" 'noerror)
7610 (load "mml" 'noerror))
7611 (require 'message nil 'noerror)
7612 (require 'mml nil 'noerror))
7613 (when (functionp 'message-mode)
7614 (funcall (symbol-function 'message-mode)))
7615 (when (functionp 'mml-mode)
7616 (funcall (symbol-function 'mml-mode) t)))
7618 (defun tramp-append-tramp-buffers ()
7619 "Append Tramp buffers into the bug report."
7622 (eq major-mode 'message-mode)
7624 (symbol-value 'mml-mode))
7626 (let* ((tramp-buf-regexp "\\*\\(debug \\)?tramp/")
7629 (mapcar '(lambda (b)
7630 (when (string-match tramp-buf-regexp (buffer-name b)) b))
7632 (curbuf (current-buffer)))
7634 ;; There is at least one Tramp buffer.
7636 (switch-to-buffer (list-buffers-noselect nil))
7637 (delete-other-windows)
7638 (setq buffer-read-only nil)
7639 (goto-char (point-min))
7641 (if (re-search-forward tramp-buf-regexp (tramp-point-at-eol) t)
7644 (let ((start (point)))
7646 (kill-region start (point)))))
7648 The buffer(s) above will be appended to this message. If you don't want
7649 to append a buffer because it contains sensible data, or because the buffer
7650 is too large, you should delete the respective buffer. The buffer(s) will
7651 contain user and host names. Passwords will never be included there.")
7653 (when (and tramp-debug-buffer (> tramp-verbose 9))
7655 (let ((start (point)))
7657 Please note that you have set `tramp-verbose' to a value greater than 9.
7658 Therefore, the contents of files might be included in the debug buffer(s).")
7659 (add-text-properties start (point) (list 'face 'italic))))
7661 (set-buffer-modified-p nil)
7662 (setq buffer-read-only t)
7663 (goto-char (point-min))
7665 (if (y-or-n-p "Do you want to append the buffer(s)? ")
7666 ;; OK, let's send. First we delete the buffer list.
7669 (switch-to-buffer curbuf)
7670 (goto-char (point-max))
7672 (dolist (buffer buffer-list)
7673 (funcall (symbol-function 'mml-insert-empty-tag)
7674 'part 'type "text/plain" 'encoding "base64"
7675 'disposition "attachment" 'buffer (buffer-name buffer)
7676 'description (buffer-name buffer)))
7677 (set-buffer-modified-p nil))
7679 ;; Don't send. Delete the message buffer.
7681 (set-buffer-modified-p nil)
7683 (throw 'dont-send nil))))))
7685 (defalias 'tramp-submit-bug 'tramp-bug)
7687 ;; Checklist for `tramp-unload-hook'
7688 ;; - Unload all `tramp-*' packages
7689 ;; - Reset `file-name-handler-alist'
7690 ;; - Cleanup hooks where Tramp functions are in
7691 ;; - Cleanup advised functions
7692 ;; - Cleanup autoloads
7694 (defun tramp-unload-tramp ()
7695 "Discard Tramp from loading remote files."
7697 ;; When Tramp is not loaded yet, its autoloads are still active.
7698 (tramp-unload-file-name-handlers)
7699 ;; ange-ftp settings must be enabled.
7700 (when (functionp 'tramp-ftp-enable-ange-ftp)
7701 (funcall (symbol-function 'tramp-ftp-enable-ange-ftp)))
7702 ;; `tramp-util' unloads also `tramp'.
7703 (condition-case nil ;; maybe its not loaded yet.
7704 (unload-feature (if (featurep 'tramp-util) 'tramp-util 'tramp) 'force)
7709 ;; Make sure that we get integration with the VC package.
7710 ;; When it is loaded, we need to pull in the integration module.
7711 ;; This must come after (provide 'tramp) because tramp-vc.el
7713 (eval-after-load "vc"
7716 (add-hook 'tramp-unload-hook
7718 (when (featurep 'tramp-vc)
7719 (unload-feature 'tramp-vc 'force))))))
7723 ;; * Allow putting passwords in the filename.
7724 ;; This should be implemented via a general mechanism to add
7725 ;; parameters in filenames. There is currently a kludge for
7726 ;; putting the port number into the filename for ssh and ftp
7727 ;; files. This could be subsumed by the new mechanism as well.
7728 ;; Another approach is to read a netrc file like ~/.authinfo
7730 ;; * Handle nonlocal exits such as C-g.
7731 ;; * Autodetect if remote `ls' groks the "--dired" switch.
7732 ;; * Add fallback for inline encodings. This should be used
7733 ;; if the remote end doesn't support mimencode or a similar program.
7734 ;; For reading files from the remote host, we can just parse the output
7735 ;; of `od -b'. For writing files to the remote host, we construct
7736 ;; a shell program which contains only "safe" ascii characters
7737 ;; and which writes the right bytes to the file. We can use printf(1)
7738 ;; or "echo -e" or the printf function in awk and use octal escapes
7739 ;; for the "dangerous" characters. The null byte might be a problem.
7740 ;; On some systems, the octal escape doesn't work. So we try the following
7741 ;; two commands to write a null byte:
7742 ;; dd if=/dev/zero bs=1 count=1
7743 ;; echo | tr '\n' '\000'
7744 ;; * Separate local `tramp-coding-commands' from remote ones. Connect
7745 ;; the two via a format which can be `uu' or `b64'. Then we can search
7746 ;; for the right local commands and the right remote commands separately.
7747 ;; * Cooperate with PCL-CVS. It uses start-process, which doesn't
7748 ;; work for remote files.
7749 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
7750 ;; `shell-quote-argument'.
7751 ;; * Completion gets confused when you leave out the method name.
7752 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
7753 ;; by the files in that directory. Add this here.
7754 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
7755 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
7756 ;; * When logging in, keep looking for questions according to an alist
7757 ;; and then invoke the right function.
7758 ;; * Case-insensitive filename completion. (Norbert Goevert.)
7759 ;; * Running CVS remotely doesn't appear to work right. It thinks
7760 ;; files are locked by somebody else even if I'm the locking user.
7761 ;; Sometimes, one gets `No CVSROOT specified' errors from CVS.
7763 ;; * Don't use globbing for directories with many files, as this is
7764 ;; likely to produce long command lines, and some shells choke on
7765 ;; long command lines.
7766 ;; * Find out about the new auto-save mechanism in Emacs 21 and
7767 ;; do the right thing.
7768 ;; * `vc-directory' does not work. It never displays any files, even
7769 ;; if it does show files when run locally.
7770 ;; * Allow correction of passwords, if the remote end allows this.
7771 ;; (Mark Hershberger)
7772 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
7773 ;; * Do asynchronous `shell-command's.
7774 ;; * Grok `append' parameter for `write-region'.
7775 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
7776 ;; * abbreviate-file-name
7777 ;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>)
7778 ;; * Also allow to omit user names when doing multi-hop. Not sure yet
7779 ;; what the user names should default to, though.
7780 ;; * better error checking. At least whenever we see something
7781 ;; strange when doing zerop, we should kill the process and start
7782 ;; again. (Greg Stark)
7783 ;; * Add caching for filename completion. (Greg Stark)
7784 ;; Of course, this has issues with usability (stale cache bites)
7785 ;; -- <daniel@danann.net>
7786 ;; * Provide a local cache of old versions of remote files for the rsync
7787 ;; transfer method to use. (Greg Stark)
7788 ;; * Remove unneeded parameters from methods.
7789 ;; * Invoke rsync once for copying a whole directory hierarchy.
7790 ;; (Francesco Potort
\e,Al
\e(B)
7791 ;; * Should we set PATH ourselves or should we rely on the remote end
7793 ;; * Make it work for XEmacs 20, which is missing `with-timeout'.
7794 ;; * Make it work for different encodings, and for different file name
7795 ;; encodings, too. (Daniel Pittman)
7796 ;; * Change applicable functions to pass a struct tramp-file-name rather
7797 ;; than the individual items MULTI-METHOD, METHOD, USER, HOST, LOCALNAME.
7798 ;; * Implement asynchronous shell commands.
7799 ;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
7800 ;; * Progress reports while copying files. (Michael Kifer)
7801 ;; * `Smart' connection method that uses inline for small and out of
7802 ;; band for large files. (Michael Kifer)
7803 ;; * Don't search for perl5 and perl. Instead, only search for perl and
7804 ;; then look if it's the right version (with `perl -v').
7805 ;; * When editing a remote CVS controlled file as a different user, VC
7806 ;; gets confused about the file locking status. Try to find out why
7807 ;; the workaround doesn't work.
7808 ;; * Change `copy-file' to grok the case where the filename handler
7809 ;; for the source and the target file are different. Right now,
7810 ;; it looks at the source file and then calls that handler, if
7811 ;; there is one. But since ange-ftp, for instance, does not know
7812 ;; about Tramp, it does not do the right thing if the target file
7813 ;; name is a Tramp name.
7814 ;; * Username and hostname completion.
7815 ;; ** If `partial-completion-mode' isn't loaded, "/foo:bla" tries to
7816 ;; connect to host "blabla" already if that host is unique. No idea
7817 ;; how to suppress. Maybe not an essential problem.
7818 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode'.
7819 ;; ** Extend `tramp-get-completion-su' for NIS and shadow passwords.
7820 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
7821 ;; Code is nearly identical.
7822 ;; ** Decide whiche files to take for searching user/host names depending on
7823 ;; operating system (windows-nt) in `tramp-completion-function-alist'.
7824 ;; ** Enhance variables for debug.
7825 ;; ** Implement "/multi:" completion.
7826 ;; ** Add a learning mode for completion. Make results persistent.
7827 ;; * Allow out-of-band methods as _last_ multi-hop.
7829 ;; Functions for file-name-handler-alist:
7830 ;; diff-latest-backup-file -- in diff.el
7831 ;; dired-uncache -- this will be needed when we do insert-directory caching
7832 ;; file-name-as-directory -- use primitive?
7833 ;; file-name-sans-versions -- use primitive?
7834 ;; get-file-buffer -- use primitive
7837 ;;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
7838 ;;; tramp.el ends here