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, 2007 Free Software Foundation, Inc.
7 ;; (copyright statements below in code to be updated with the above notice)
9 ;; Author: Kai Gro
\e,A_
\e(Bjohann <kai.grossjohann@gmx.net>
10 ;; Michael Albinus <michael.albinus@gmx.de>
11 ;; Keywords: comm, processes
13 ;; This file is part of GNU Emacs.
15 ;; GNU Emacs is free software; you can redistribute it and/or modify
16 ;; it under the terms of the GNU General Public License as published by
17 ;; the Free Software Foundation; either version 2, or (at your option)
20 ;; GNU Emacs is distributed in the hope that it will be useful,
21 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
22 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
23 ;; GNU General Public License for more details.
25 ;; You should have received a copy of the GNU General Public License
26 ;; along with GNU Emacs; see the file COPYING. If not, write to the
27 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
28 ;; Boston, MA 02110-1301, USA.
32 ;; This package provides remote file editing, similar to ange-ftp.
33 ;; The difference is that ange-ftp uses FTP to transfer files between
34 ;; the local and the remote host, whereas tramp.el uses a combination
35 ;; of rsh and rcp or other work-alike programs, such as ssh/scp.
37 ;; For more detailed instructions, please see the info file.
42 ;; This package only works for Emacs 20 and higher, and for XEmacs 21
43 ;; and higher. (XEmacs 20 is missing the `with-timeout' macro. Emacs
44 ;; 19 is reported to have other problems. For XEmacs 21, you need the
45 ;; package `fsf-compat' for the `with-timeout' macro.)
47 ;; This version might not work with pre-Emacs 21 VC unless VC is
48 ;; loaded before tramp.el. Could you please test this and tell me about
49 ;; the result? Thanks.
51 ;; Also see the todo list at the bottom of this file.
53 ;; The current version of Tramp can be retrieved from the following URL:
54 ;; http://ftp.gnu.org/gnu/tramp/
56 ;; There's a mailing list for this, as well. Its name is:
57 ;; tramp-devel@gnu.org
58 ;; You can use the Web to subscribe, under the following URL:
59 ;; http://lists.gnu.org/mailman/listinfo/tramp-devel
61 ;; For the adventurous, the current development sources are available
62 ;; via CVS. You can find instructions about this at the following URL:
63 ;; http://savannah.gnu.org/projects/tramp/
64 ;; Click on "CVS" in the navigation bar near the top.
66 ;; Don't forget to put on your asbestos longjohns, first!
70 ;; The Tramp version number and bug report address, as prepared by configure.
72 (add-hook 'tramp-unload-hook
74 (when (featurep 'trampver)
75 (unload-feature 'trampver 'force))))
77 (if (featurep 'xemacs)
78 (require 'timer-funcs)
81 (require 'format-spec) ;from Gnus 5.8, also in tar ball
82 ;; As long as password.el is not part of (X)Emacs, it shouldn't
84 (if (featurep 'xemacs)
85 (load "password" 'noerror)
86 (require 'password nil 'noerror)) ;from No Gnus, also in tar ball
88 ;; The explicit check is not necessary in Emacs, which provides the
89 ;; feature even if implemented in C, but it appears to be necessary
91 (unless (and (fboundp 'base64-encode-region)
92 (fboundp 'base64-decode-region))
93 (require 'base64)) ;for the mimencode methods
97 (autoload 'tramp-uuencode-region "tramp-uu"
98 "Implementation of `uuencode' in Lisp.")
99 (add-hook 'tramp-unload-hook
101 (when (featurep 'tramp-uu)
102 (unload-feature 'tramp-uu 'force))))
104 (unless (fboundp 'uudecode-decode-region)
105 (autoload 'uudecode-decode-region "uudecode"))
107 ;; XEmacs is distributed with few Lisp packages. Further packages are
108 ;; installed using EFS. If we use a unified filename format, then
109 ;; Tramp is required in addition to EFS. (But why can't Tramp just
110 ;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
111 ;; just like before.) Another reason for using a separate filename
112 ;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
113 ;; Tramp only knows how to deal with `file-name-handler-alist', not
116 (defvar tramp-unified-filenames (not (featurep 'xemacs))
117 "Non-nil means to use unified Ange-FTP/Tramp filename syntax.
118 Otherwise, use a separate filename syntax for Tramp.")
120 ;; Load foreign methods. Because they do require Tramp internally, this
121 ;; must be done with the `eval-after-load' trick.
123 ;; tramp-ftp supports Ange-FTP only. Not suited for XEmacs therefore.
124 (unless (featurep 'xemacs)
125 (eval-after-load "tramp"
128 (add-hook 'tramp-unload-hook
130 (when (featurep 'tramp-ftp)
131 (unload-feature 'tramp-ftp 'force)))))))
132 (when (and tramp-unified-filenames (featurep 'xemacs))
133 (eval-after-load "tramp"
136 (add-hook 'tramp-unload-hook
138 (when (featurep 'tramp-efs)
139 (unload-feature 'tramp-efs 'force)))))))
141 ;; tramp-smb uses "smbclient" from Samba.
142 ;; Not available under Cygwin and Windows, because they don't offer
143 ;; "smbclient". And even not necessary there, because Emacs supports
144 ;; UNC file names like "//host/share/localname".
145 (unless (memq system-type '(cygwin windows-nt))
146 (eval-after-load "tramp"
149 (add-hook 'tramp-unload-hook
151 (when (featurep 'tramp-smb)
152 (unload-feature 'tramp-smb 'force)))))))
156 ;; Emacs 19.34 compatibility hack -- is this needed?
157 (or (>= emacs-major-version 20)
160 (unless (boundp 'custom-print-functions)
161 (defvar custom-print-functions nil)) ; not autoloaded before Emacs 20.4
163 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
164 ;; Currently, XEmacs supports this.
166 (when (featurep 'xemacs)
167 (let (unused-vars) ; Pacify Emacs byte-compiler
168 (defalias 'warnings 'identity) ; Pacify Emacs byte-compiler
169 (byte-compiler-options (warnings (- unused-vars))))))
171 ;; `directory-sep-char' is an obsolete variable in Emacs. But it is
172 ;; used in XEmacs, so we set it here and there. The following is needed
173 ;; to pacify Emacs byte-compiler.
175 (when (boundp 'byte-compile-not-obsolete-var)
176 (setq byte-compile-not-obsolete-var 'directory-sep-char)))
178 ;;; User Customizable Internal Variables:
181 "Edit remote files with a combination of rsh and rcp or similar programs."
185 (defcustom tramp-verbose 9
186 "*Verbosity level for tramp.el. 0 means be silent, 10 is most verbose."
190 (defcustom tramp-debug-buffer nil
191 "*Whether to send all commands and responses to a debug buffer."
197 (when (boundp 'backup-directory-alist)
198 (defcustom tramp-backup-directory-alist nil
199 "Alist of filename patterns and backup directory names.
200 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
201 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
202 is a local file name, the backup directory is prepended with Tramp file
203 name prefix \(multi-method, method, user, host\) of file.
205 \(setq tramp-backup-directory-alist backup-directory-alist\)
207 gives the same backup policy for Tramp files on their hosts like the
208 policy for local files."
210 :type '(repeat (cons (regexp :tag "Regexp matching filename")
211 (directory :tag "Backup directory name"))))))
213 ;; XEmacs case. We cannot check for `bkup-backup-directory-info', because
214 ;; the package "backup-dir" might not be loaded yet.
216 (when (featurep 'xemacs)
217 (defcustom tramp-bkup-backup-directory-info nil
218 "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
219 It has the same meaning like `bkup-backup-directory-info' from package
220 `backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local
221 file name, the backup directory is prepended with Tramp file name prefix
222 \(multi-method, method, user, host\) of file.
224 \(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\)
226 gives the same backup policy for Tramp files on their hosts like the
227 policy for local files."
229 (list (regexp :tag "File regexp")
230 (string :tag "Backup Dir")
235 (const search-upward))))
238 (defcustom tramp-auto-save-directory nil
239 "*Put auto-save files in this directory, if set.
240 The idea is to use a local directory so that auto-saving is faster."
242 :type '(choice (const nil)
245 (defcustom tramp-encoding-shell
246 (if (memq system-type '(windows-nt))
249 "*Use this program for encoding and decoding commands on the local host.
250 This shell is used to execute the encoding and decoding command on the
251 local host, so if you want to use `~' in those commands, you should
252 choose a shell here which groks tilde expansion. `/bin/sh' normally
253 does not understand tilde expansion.
255 For encoding and deocding, commands like the following are executed:
257 /bin/sh -c COMMAND < INPUT > OUTPUT
259 This variable can be used to change the \"/bin/sh\" part. See the
260 variable `tramp-encoding-command-switch' for the \"-c\" part. Also, see the
261 variable `tramp-encoding-reads-stdin' to specify whether the commands read
262 standard input or a file.
264 Note that this variable is not used for remote commands. There are
265 mechanisms in tramp.el which automatically determine the right shell to
266 use for the remote host."
268 :type '(file :must-match t))
270 (defcustom tramp-encoding-command-switch
271 (if (string-match "cmd\\.exe" tramp-encoding-shell)
274 "*Use this switch together with `tramp-encoding-shell' for local commands.
275 See the variable `tramp-encoding-shell' for more information."
279 (defcustom tramp-encoding-reads-stdin t
280 "*If non-nil, encoding commands read from standard input.
281 If nil, the filename is the last argument.
283 Note that the commands always must write to standard output."
287 (defcustom tramp-multi-sh-program
289 "*Use this program for bootstrapping multi-hop connections.
290 This variable is similar to `tramp-encoding-shell', but it is only used
291 when initializing a multi-hop connection. Therefore, the set of
292 commands sent to this shell is quite restricted, and if you are
293 careful it works to use CMD.EXE under Windows (instead of a Bourne-ish
294 shell which does not normally exist on Windows anyway).
296 To use multi-hop methods from Windows, you also need suitable entries
297 in `tramp-multi-connection-function-alist' for the first hop.
299 This variable defaults to the value of `tramp-encoding-shell'."
301 :type '(file :must-match t))
303 ;; CCC I have changed all occurrences of comint-quote-filename with
304 ;; tramp-shell-quote-argument, except in tramp-handle-expand-many-files.
305 ;; There, comint-quote-filename was removed altogether. If it turns
306 ;; out to be necessary there, something will need to be done.
307 ;;-(defcustom tramp-file-name-quote-list
308 ;;- '(?] ?[ ?\| ?& ?< ?> ?\( ?\) ?\; ?\ ?\* ?\? ?\! ?\" ?\' ?\` ?# ?\@ ?\+ )
309 ;;- "*Protect these characters from the remote shell.
310 ;;-Any character in this list is quoted (preceded with a backslash)
311 ;;-because it means something special to the shell. This takes effect
312 ;;-when sending file and directory names to the remote shell.
314 ;;-See `comint-file-name-quote-list' for details."
316 ;;- :type '(repeat character))
318 (defcustom tramp-methods
319 '( ("rcp" (tramp-connection-function tramp-open-connection-rsh)
320 (tramp-login-program "rsh")
321 (tramp-copy-program "rcp")
322 (tramp-remote-sh "/bin/sh")
323 (tramp-login-args nil)
324 (tramp-copy-args nil)
325 (tramp-copy-keep-date-arg "-p")
326 (tramp-password-end-of-line nil))
327 ("scp" (tramp-connection-function tramp-open-connection-rsh)
328 (tramp-login-program "ssh")
329 (tramp-copy-program "scp")
330 (tramp-remote-sh "/bin/sh")
331 (tramp-login-args ("-e" "none"))
332 (tramp-copy-args nil)
333 (tramp-copy-keep-date-arg "-p")
334 (tramp-password-end-of-line nil))
335 ("scp1" (tramp-connection-function tramp-open-connection-rsh)
336 (tramp-login-program "ssh")
337 (tramp-copy-program "scp")
338 (tramp-remote-sh "/bin/sh")
339 (tramp-login-args ("-1" "-e" "none"))
340 (tramp-copy-args ("-1"))
341 (tramp-copy-keep-date-arg "-p")
342 (tramp-password-end-of-line nil))
343 ("scp2" (tramp-connection-function tramp-open-connection-rsh)
344 (tramp-login-program "ssh")
345 (tramp-copy-program "scp")
346 (tramp-remote-sh "/bin/sh")
347 (tramp-login-args ("-2" "-e" "none"))
348 (tramp-copy-args ("-2"))
349 (tramp-copy-keep-date-arg "-p")
350 (tramp-password-end-of-line nil))
352 (tramp-connection-function tramp-open-connection-rsh)
353 (tramp-login-program "ssh1")
354 (tramp-copy-program "scp1")
355 (tramp-remote-sh "/bin/sh")
356 (tramp-login-args ("-e" "none"))
357 (tramp-copy-args nil)
358 (tramp-copy-keep-date-arg "-p")
359 (tramp-password-end-of-line nil))
361 (tramp-connection-function tramp-open-connection-rsh)
362 (tramp-login-program "ssh2")
363 (tramp-copy-program "scp2")
364 (tramp-remote-sh "/bin/sh")
365 (tramp-login-args ("-e" "none"))
366 (tramp-copy-args nil)
367 (tramp-copy-keep-date-arg "-p")
368 (tramp-password-end-of-line nil))
369 ("rsync" (tramp-connection-function tramp-open-connection-rsh)
370 (tramp-login-program "ssh")
371 (tramp-copy-program "rsync")
372 (tramp-remote-sh "/bin/sh")
373 (tramp-login-args ("-e" "none"))
374 (tramp-copy-args ("-e" "ssh"))
375 (tramp-copy-keep-date-arg "-t")
376 (tramp-password-end-of-line nil))
377 ("remcp" (tramp-connection-function tramp-open-connection-rsh)
378 (tramp-login-program "remsh")
379 (tramp-copy-program "rcp")
380 (tramp-remote-sh "/bin/sh")
381 (tramp-login-args nil)
382 (tramp-copy-args nil)
383 (tramp-copy-keep-date-arg "-p")
384 (tramp-password-end-of-line nil))
385 ("rsh" (tramp-connection-function tramp-open-connection-rsh)
386 (tramp-login-program "rsh")
387 (tramp-copy-program nil)
388 (tramp-remote-sh "/bin/sh")
389 (tramp-login-args nil)
390 (tramp-copy-args nil)
391 (tramp-copy-keep-date-arg nil)
392 (tramp-password-end-of-line nil))
393 ("ssh" (tramp-connection-function tramp-open-connection-rsh)
394 (tramp-login-program "ssh")
395 (tramp-copy-program nil)
396 (tramp-remote-sh "/bin/sh")
397 (tramp-login-args ("-e" "none"))
398 (tramp-copy-args nil)
399 (tramp-copy-keep-date-arg nil)
400 (tramp-password-end-of-line nil))
401 ("ssh1" (tramp-connection-function tramp-open-connection-rsh)
402 (tramp-login-program "ssh")
403 (tramp-copy-program nil)
404 (tramp-remote-sh "/bin/sh")
405 (tramp-login-args ("-1" "-e" "none"))
406 (tramp-copy-args ("-1"))
407 (tramp-copy-keep-date-arg nil)
408 (tramp-password-end-of-line nil))
409 ("ssh2" (tramp-connection-function tramp-open-connection-rsh)
410 (tramp-login-program "ssh")
411 (tramp-copy-program nil)
412 (tramp-remote-sh "/bin/sh")
413 (tramp-login-args ("-2" "-e" "none"))
414 (tramp-copy-args ("-2"))
415 (tramp-copy-keep-date-arg nil)
416 (tramp-password-end-of-line nil))
418 (tramp-connection-function tramp-open-connection-rsh)
419 (tramp-login-program "ssh1")
420 (tramp-copy-program nil)
421 (tramp-remote-sh "/bin/sh")
422 (tramp-login-args ("-e" "none"))
423 (tramp-copy-args nil)
424 (tramp-copy-keep-date-arg nil)
425 (tramp-password-end-of-line nil))
427 (tramp-connection-function tramp-open-connection-rsh)
428 (tramp-login-program "ssh2")
429 (tramp-copy-program nil)
430 (tramp-remote-sh "/bin/sh")
431 (tramp-login-args ("-e" "none"))
432 (tramp-copy-args nil)
433 (tramp-copy-keep-date-arg nil)
434 (tramp-password-end-of-line nil))
435 ("remsh" (tramp-connection-function tramp-open-connection-rsh)
436 (tramp-login-program "remsh")
437 (tramp-copy-program nil)
438 (tramp-remote-sh "/bin/sh")
439 (tramp-login-args nil)
440 (tramp-copy-args nil)
441 (tramp-copy-keep-date-arg nil)
442 (tramp-password-end-of-line nil))
444 (tramp-connection-function tramp-open-connection-telnet)
445 (tramp-login-program "telnet")
446 (tramp-copy-program nil)
447 (tramp-remote-sh "/bin/sh")
448 (tramp-login-args nil)
449 (tramp-copy-args nil)
450 (tramp-copy-keep-date-arg nil)
451 (tramp-password-end-of-line nil))
452 ("su" (tramp-connection-function tramp-open-connection-su)
453 (tramp-login-program "su")
454 (tramp-copy-program nil)
455 (tramp-remote-sh "/bin/sh")
456 (tramp-login-args ("-" "%u"))
457 (tramp-copy-args nil)
458 (tramp-copy-keep-date-arg nil)
459 (tramp-password-end-of-line nil))
460 ("sudo" (tramp-connection-function tramp-open-connection-su)
461 (tramp-login-program "sudo")
462 (tramp-copy-program nil)
463 (tramp-remote-sh "/bin/sh")
464 (tramp-login-args ("-u" "%u" "-s"
466 (tramp-copy-args nil)
467 (tramp-copy-keep-date-arg nil)
468 (tramp-password-end-of-line nil))
469 ("multi" (tramp-connection-function tramp-open-connection-multi)
470 (tramp-login-program nil)
471 (tramp-copy-program nil)
472 (tramp-remote-sh "/bin/sh")
473 (tramp-login-args nil)
474 (tramp-copy-args nil)
475 (tramp-copy-keep-date-arg nil)
476 (tramp-password-end-of-line nil))
477 ("scpc" (tramp-connection-function tramp-open-connection-rsh)
478 (tramp-login-program "ssh")
479 (tramp-copy-program "scp")
480 (tramp-remote-sh "/bin/sh")
481 (tramp-login-args ("-o" "ControlPath=%t.%%r@%%h:%%p"
482 "-o" "ControlMaster=yes"
484 (tramp-copy-args ("-o" "ControlPath=%t.%%r@%%h:%%p"
485 "-o" "ControlMaster=auto"))
486 (tramp-copy-keep-date-arg "-p")
487 (tramp-password-end-of-line nil))
488 ("scpx" (tramp-connection-function tramp-open-connection-rsh)
489 (tramp-login-program "ssh")
490 (tramp-copy-program "scp")
491 (tramp-remote-sh "/bin/sh")
492 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
493 (tramp-copy-args nil)
494 (tramp-copy-keep-date-arg "-p")
495 (tramp-password-end-of-line nil))
496 ("sshx" (tramp-connection-function tramp-open-connection-rsh)
497 (tramp-login-program "ssh")
498 (tramp-copy-program nil)
499 (tramp-remote-sh "/bin/sh")
500 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
501 (tramp-copy-args nil)
502 (tramp-copy-keep-date-arg nil)
503 (tramp-password-end-of-line nil))
505 (tramp-connection-function tramp-open-connection-rsh)
506 (tramp-login-program "krlogin")
507 (tramp-copy-program nil)
508 (tramp-remote-sh "/bin/sh")
509 (tramp-login-args ("-x"))
510 (tramp-copy-args nil)
511 (tramp-copy-keep-date-arg nil)
512 (tramp-password-end-of-line nil))
514 (tramp-connection-function tramp-open-connection-rsh)
515 (tramp-login-program "plink")
516 (tramp-copy-program nil)
517 (tramp-remote-sh "/bin/sh")
518 (tramp-login-args ("-ssh")) ;optionally add "-v"
519 (tramp-copy-args nil)
520 (tramp-copy-keep-date-arg nil)
521 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
523 (tramp-connection-function tramp-open-connection-rsh)
524 (tramp-login-program "plink")
525 (tramp-copy-program nil)
526 (tramp-remote-sh "/bin/sh")
527 (tramp-login-args ("-1" "-ssh")) ;optionally add "-v"
528 (tramp-copy-args nil)
529 (tramp-copy-keep-date-arg nil)
530 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
532 (tramp-connection-function tramp-open-connection-rsh)
533 (tramp-login-program "plink")
534 (tramp-copy-program "pscp")
535 (tramp-remote-sh "/bin/sh")
536 (tramp-login-args ("-ssh"))
537 (tramp-copy-args nil)
538 (tramp-copy-keep-date-arg "-p")
539 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
541 (tramp-connection-function tramp-open-connection-rsh)
542 (tramp-login-program "fsh")
543 (tramp-copy-program "fcp")
544 (tramp-remote-sh "/bin/sh -i")
545 (tramp-login-args ("sh" "-i"))
546 (tramp-copy-args nil)
547 (tramp-copy-keep-date-arg "-p")
548 (tramp-password-end-of-line nil))
550 "*Alist of methods for remote files.
551 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
552 Each NAME stands for a remote access method. Each PARAM is a
553 pair of the form (KEY VALUE). The following KEYs are defined:
554 * `tramp-connection-function'
555 This specifies the function to use to connect to the remote host.
556 Currently, `tramp-open-connection-rsh', `tramp-open-connection-telnet'
557 and `tramp-open-connection-su' are defined. See the documentation
558 of these functions for more details.
560 This specifies the Bourne shell to use on the remote host. This
561 MUST be a Bourne-like shell. It is normally not necessary to set
562 this to any value other than \"/bin/sh\": tramp wants to use a shell
563 which groks tilde expansion, but it can search for it. Also note
564 that \"/bin/sh\" exists on all Unixen, this might not be true for
565 the value that you decide to use. You Have Been Warned.
566 * `tramp-login-program'
567 This specifies the name of the program to use for logging in to the
568 remote host. Depending on `tramp-connection-function', this may be
569 the name of rsh or a workalike program (when
570 `tramp-connection-function' is `tramp-open-connection-rsh'), or the
571 name of telnet or a workalike (for `tramp-open-connection-telnet'),
572 or the name of su or a workalike (for `tramp-open-connection-su').
574 This specifies the list of arguments to pass to the above
575 mentioned program. Please note that this is a list of arguments,
576 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
577 here. Instead, you want two list elements, one for \"-a\" and one
578 for \"-b\", or one for \"-f\" and one for \"foo\".
579 If `tramp-connection-function' is `tramp-open-connection-su', then
580 \"%u\" in this list is replaced by the user name, and \"%%\" can
581 be used to obtain a literal percent character.
582 \"%t\" is replaced by the temporary file name for `scp'-like methods.
583 * `tramp-copy-program'
584 This specifies the name of the program to use for remotely copying
585 the file; this might be the absolute filename of rcp or the name of
588 This specifies the list of parameters to pass to the above mentioned
589 program, the hints for `tramp-login-args' also apply here.
590 * `tramp-copy-keep-date-arg'
591 This specifies the parameter to use for the copying program when the
592 timestamp of the original file should be kept. For `rcp', use `-p', for
594 * `tramp-password-end-of-line'
595 This specifies the string to use for terminating the line after
596 submitting the password. If this method parameter is nil, then the
597 value of the normal variable `tramp-default-password-end-of-line'
598 is used. This parameter is necessary because the \"plink\" program
599 requires any two characters after sending the password. These do
600 not have to be newline or carriage return characters. Other login
601 programs are happy with just one character, the newline character.
602 We use \"xy\" as the value for methods using \"plink\".
604 What does all this mean? Well, you should specify `tramp-login-program'
605 for all methods; this program is used to log in to the remote site. Then,
606 there are two ways to actually transfer the files between the local and the
607 remote side. One way is using an additional rcp-like program. If you want
608 to do this, set `tramp-copy-program' in the method.
610 Another possibility for file transfer is inline transfer, i.e. the
611 file is passed through the same buffer used by `tramp-login-program'. In
612 this case, the file contents need to be protected since the
613 `tramp-login-program' might use escape codes or the connection might not
614 be eight-bit clean. Therefore, file contents are encoded for transit.
615 See the variable `tramp-coding-commands' for details.
617 So, to summarize: if the method is an out-of-band method, then you
618 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
619 inline method, then these two parameters should be nil. Every method,
620 inline or out of band, must specify `tramp-connection-function' plus
621 the associated arguments (for example, the login program if you chose
622 `tramp-open-connection-telnet').
626 When using `tramp-open-connection-su' the phrase `open connection to a
627 remote host' sounds strange, but it is used nevertheless, for
628 consistency. No connection is opened to a remote host, but `su' is
629 started on the local host. You are not allowed to specify a remote
630 host other than `localhost' or the name of the local host."
634 (set (list (const tramp-connection-function) function)
635 (list (const tramp-login-program)
636 (choice (const nil) string))
637 (list (const tramp-copy-program)
638 (choice (const nil) string))
639 (list (const tramp-remote-sh)
640 (choice (const nil) string))
641 (list (const tramp-login-args) (repeat string))
642 (list (const tramp-copy-args) (repeat string))
643 (list (const tramp-copy-keep-date-arg)
644 (choice (const nil) string))
645 (list (const tramp-encoding-command)
646 (choice (const nil) string))
647 (list (const tramp-decoding-command)
648 (choice (const nil) string))
649 (list (const tramp-encoding-function)
650 (choice (const nil) function))
651 (list (const tramp-decoding-function)
652 (choice (const nil) function))
653 (list (const tramp-password-end-of-line)
654 (choice (const nil) string))))))
656 (defcustom tramp-multi-methods '("multi" "multiu")
657 "*List of multi-hop methods.
658 Each entry in this list should be a method name as mentioned in the
659 variable `tramp-methods'."
661 :type '(repeat string))
663 (defcustom tramp-multi-connection-function-alist
664 '(("telnet" tramp-multi-connect-telnet "telnet %h%n")
665 ("rsh" tramp-multi-connect-rlogin "rsh %h -l %u%n")
666 ("remsh" tramp-multi-connect-rlogin "remsh %h -l %u%n")
667 ("ssh" tramp-multi-connect-rlogin "ssh %h -l %u%n")
668 ("ssht" tramp-multi-connect-rlogin "ssh %h -e none -t -t -l %u%n")
669 ("su" tramp-multi-connect-su "su - %u%n")
670 ("sudo" tramp-multi-connect-su "sudo -u %u -s -p Password:%n"))
671 "*List of connection functions for multi-hop methods.
672 Each list item is a list of three items (METHOD FUNCTION COMMAND),
673 where METHOD is the name as used in the file name, FUNCTION is the
674 function to be executed, and COMMAND is the shell command used for
677 COMMAND may contain percent escapes. `%u' will be replaced with the
678 user name, `%h' will be replaced with the host name, and `%n' will be
679 replaced with an end-of-line character, as specified in the variable
680 `tramp-rsh-end-of-line'. Use `%%' for a literal percent character.
681 Note that the interpretation of the percent escapes also depends on
682 the FUNCTION. For example, the `%u' escape is forbidden with the
683 function `tramp-multi-connect-telnet'. See the documentation of the
684 various functions for details."
686 :type '(repeat (list string function string)))
688 (defcustom tramp-default-method
689 ;; An external copy method seems to be preferred, because it is much
690 ;; more performant for large files, and it hasn't too serious delays
691 ;; for small files. But it must be ensured that there aren't
692 ;; permanent password queries. Either a password agent like
693 ;; "ssh-agent" or "Pageant" shall run, or the optional password.el
694 ;; package shall be active for password caching. "scpc" would be
695 ;; another good choice because of the "ControlMaster" option, but
696 ;; this is a more modern alternative in OpenSSH 4, which cannot be
698 (let ((e-f (fboundp 'executable-find)))
700 ;; PuTTY is installed.
701 ((and e-f (funcall 'executable-find "pscp"))
702 (if (or (fboundp 'password-read)
703 ;; Pageant is running.
704 (and (fboundp 'w32-window-exists-p)
705 (funcall 'w32-window-exists-p "Pageant" "Pageant")))
708 ;; There is an ssh installation.
709 ((and e-f (funcall 'executable-find "scp"))
710 (if (or (fboundp 'password-read)
711 ;; ssh-agent is running.
712 (getenv "SSH_AUTH_SOCK")
713 (getenv "SSH_AGENT_PID"))
716 ;; Under Emacs 20, `executable-find' does not exists. So we
717 ;; couldn't check whether there is an ssh implementation. Let's
722 "*Default method to use for transferring files.
723 See `tramp-methods' for possibilities.
724 Also see `tramp-default-method-alist'."
728 (defcustom tramp-default-method-alist
729 '(("\\`localhost\\'" "\\`root\\'" "su"))
730 "*Default method to use for specific user/host pairs.
731 This is an alist of items (HOST USER METHOD). The first matching item
732 specifies the method to use for a file name which does not specify a
733 method. HOST and USER are regular expressions or nil, which is
734 interpreted as a regular expression which always matches. If no entry
735 matches, the variable `tramp-default-method' takes effect.
737 If the file name does not specify the user, lookup is done using the
738 empty string for the user name.
740 See `tramp-methods' for a list of possibilities for METHOD."
742 :type '(repeat (list (regexp :tag "Host regexp")
743 (regexp :tag "User regexp")
744 (string :tag "Method"))))
746 ;; Default values for non-Unices seeked
747 (defconst tramp-completion-function-alist-rsh
748 (unless (memq system-type '(windows-nt))
749 '((tramp-parse-rhosts "/etc/hosts.equiv")
750 (tramp-parse-rhosts "~/.rhosts")))
751 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
753 ;; Default values for non-Unices seeked
754 (defconst tramp-completion-function-alist-ssh
755 (unless (memq system-type '(windows-nt))
756 '((tramp-parse-rhosts "/etc/hosts.equiv")
757 (tramp-parse-rhosts "/etc/shosts.equiv")
758 (tramp-parse-shosts "/etc/ssh_known_hosts")
759 (tramp-parse-sconfig "/etc/ssh_config")
760 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
761 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
762 (tramp-parse-rhosts "~/.rhosts")
763 (tramp-parse-rhosts "~/.shosts")
764 (tramp-parse-shosts "~/.ssh/known_hosts")
765 (tramp-parse-sconfig "~/.ssh/config")
766 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
767 (tramp-parse-sknownhosts "~/.ssh2/knownhosts")))
768 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
770 ;; Default values for non-Unices seeked
771 (defconst tramp-completion-function-alist-telnet
772 (unless (memq system-type '(windows-nt))
773 '((tramp-parse-hosts "/etc/hosts")))
774 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
776 ;; Default values for non-Unices seeked
777 (defconst tramp-completion-function-alist-su
778 (unless (memq system-type '(windows-nt))
779 '((tramp-parse-passwd "/etc/passwd")))
780 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
782 (defvar tramp-completion-function-alist nil
783 "*Alist of methods for remote files.
784 This is a list of entries of the form (NAME PAIR1 PAIR2 ...).
785 Each NAME stands for a remote access method. Each PAIR is of the form
786 \(FUNCTION FILE). FUNCTION is responsible to extract user names and host
787 names from FILE for completion. The following predefined FUNCTIONs exists:
789 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
790 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
791 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
792 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
793 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
794 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
795 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
796 * `tramp-parse-netrc' for \"~/.netrc\" like files.
798 FUNCTION can also be a customer defined function. For more details see
801 (eval-after-load "tramp"
803 (tramp-set-completion-function
804 "rcp" tramp-completion-function-alist-rsh)
805 (tramp-set-completion-function
806 "scp" tramp-completion-function-alist-ssh)
807 (tramp-set-completion-function
808 "scp1" tramp-completion-function-alist-ssh)
809 (tramp-set-completion-function
810 "scp2" tramp-completion-function-alist-ssh)
811 (tramp-set-completion-function
812 "scp1_old" tramp-completion-function-alist-ssh)
813 (tramp-set-completion-function
814 "scp2_old" tramp-completion-function-alist-ssh)
815 (tramp-set-completion-function
816 "rsync" tramp-completion-function-alist-rsh)
817 (tramp-set-completion-function
818 "remcp" tramp-completion-function-alist-rsh)
819 (tramp-set-completion-function
820 "rsh" tramp-completion-function-alist-rsh)
821 (tramp-set-completion-function
822 "ssh" tramp-completion-function-alist-ssh)
823 (tramp-set-completion-function
824 "ssh1" tramp-completion-function-alist-ssh)
825 (tramp-set-completion-function
826 "ssh2" tramp-completion-function-alist-ssh)
827 (tramp-set-completion-function
828 "ssh1_old" tramp-completion-function-alist-ssh)
829 (tramp-set-completion-function
830 "ssh2_old" tramp-completion-function-alist-ssh)
831 (tramp-set-completion-function
832 "remsh" tramp-completion-function-alist-rsh)
833 (tramp-set-completion-function
834 "telnet" tramp-completion-function-alist-telnet)
835 (tramp-set-completion-function
836 "su" tramp-completion-function-alist-su)
837 (tramp-set-completion-function
838 "sudo" tramp-completion-function-alist-su)
839 (tramp-set-completion-function
841 (tramp-set-completion-function
842 "scpx" tramp-completion-function-alist-ssh)
843 (tramp-set-completion-function
844 "sshx" tramp-completion-function-alist-ssh)
845 (tramp-set-completion-function
846 "krlogin" tramp-completion-function-alist-rsh)
847 (tramp-set-completion-function
848 "plink" tramp-completion-function-alist-ssh)
849 (tramp-set-completion-function
850 "plink1" tramp-completion-function-alist-ssh)
851 (tramp-set-completion-function
852 "pscp" tramp-completion-function-alist-ssh)
853 (tramp-set-completion-function
854 "fcp" tramp-completion-function-alist-ssh)))
856 (defcustom tramp-rsh-end-of-line "\n"
857 "*String used for end of line in rsh connections.
858 I don't think this ever needs to be changed, so please tell me about it
859 if you need to change this.
860 Also see the method parameter `tramp-password-end-of-line' and the normal
861 variable `tramp-default-password-end-of-line'."
865 (defcustom tramp-default-password-end-of-line
866 tramp-rsh-end-of-line
867 "*String used for end of line after sending a password.
868 This variable provides the default value for the method parameter
869 `tramp-password-end-of-line', see `tramp-methods' for more details.
871 It seems that people using plink under Windows need to send
872 \"\\r\\n\" (carriage-return, then newline) after a password, but just
873 \"\\n\" after all other lines. This variable can be used for the
874 password, see `tramp-rsh-end-of-line' for the other cases.
876 The default value is to use the same value as `tramp-rsh-end-of-line'."
880 (defcustom tramp-remote-path
881 ;; "/usr/xpg4/bin" has been placed first, because on Solaris a POSIX
882 ;; compatible "id" is needed.
883 '("/usr/xpg4/bin" "/bin" "/usr/bin" "/usr/sbin" "/usr/local/bin"
884 "/usr/ccs/bin" "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
885 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
886 "*List of directories to search for executables on remote host.
887 Please notify me about other semi-standard directories to include here.
889 You can use `~' in this list, but when searching for a shell which groks
890 tilde expansion, all directory names starting with `~' will be ignored."
892 :type '(repeat string))
894 (defcustom tramp-login-prompt-regexp
895 ".*ogin\\( .*\\)?: *"
896 "*Regexp matching login-like prompts.
897 The regexp should match at end of buffer.
899 Sometimes the prompt is reported to look like \"login as:\"."
903 (defcustom tramp-shell-prompt-pattern
904 "^[^#$%>\n]*[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*"
905 "Regexp to match prompts from remote shell.
906 Normally, Tramp expects you to configure `shell-prompt-pattern'
907 correctly, but sometimes it happens that you are connecting to a
908 remote host which sends a different kind of shell prompt. Therefore,
909 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
910 and also things matched by this variable. The default value of this
911 variable is similar to the default value of `shell-prompt-pattern',
912 which should work well in many cases."
916 (defcustom tramp-password-prompt-regexp
917 "^.*\\([pP]assword\\|passphrase\\).*:\^@? *"
918 "*Regexp matching password-like prompts.
919 The regexp should match at end of buffer.
921 The `sudo' program appears to insert a `^@' character into the prompt."
925 (defcustom tramp-wrong-passwd-regexp
927 ;; These strings should be on the last line
928 (regexp-opt '("Permission denied."
934 "Name or service not known"
935 "Host key verification failed.") t)
939 ;; Here comes a list of regexes, separated by \\|
940 "Received signal [0-9]+"
942 "*Regexp matching a `login failed' message.
943 The regexp should match at end of buffer."
947 (defcustom tramp-yesno-prompt-regexp
949 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
951 "Regular expression matching all yes/no queries which need to be confirmed.
952 The confirmation should be done with yes or no.
953 The regexp should match at end of buffer.
954 See also `tramp-yn-prompt-regexp'."
958 (defcustom tramp-yn-prompt-regexp
960 (regexp-opt '("Store key in cache? (y/n)"
961 "Update cached key? (y/n, Return cancels connection)") t)
963 "Regular expression matching all y/n queries which need to be confirmed.
964 The confirmation should be done with y or n.
965 The regexp should match at end of buffer.
966 See also `tramp-yesno-prompt-regexp'."
970 (defcustom tramp-terminal-prompt-regexp
974 "Terminal type\\? \\[.*\\]"
976 "Regular expression matching all terminal setting prompts.
977 The regexp should match at end of buffer.
978 The answer will be provided by `tramp-action-terminal', which see."
982 (defcustom tramp-operation-not-permitted-regexp
983 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
984 (regexp-opt '("Operation not permitted") t))
985 "Regular expression matching keep-date problems in (s)cp operations.
986 Copying has been performed successfully already, so this message can
991 (defcustom tramp-copy-failed-regexp
993 (regexp-opt '("Permission denied"
996 "No such file or directory") t)
998 "Regular expression matching copy problems in (s)cp operations."
1002 (defcustom tramp-process-alive-regexp
1004 "Regular expression indicating a process has finished.
1005 In fact this expression is empty by intention, it will be used only to
1006 check regularly the status of the associated process.
1007 The answer will be provided by `tramp-action-process-alive',
1008 `tramp-multi-action-process-alive' and`tramp-action-out-of-band', which see."
1012 (defcustom tramp-temp-name-prefix "tramp."
1013 "*Prefix to use for temporary files.
1014 If this is a relative file name (such as \"tramp.\"), it is considered
1015 relative to the directory name returned by the function
1016 `tramp-temporary-file-directory' (which see). It may also be an
1017 absolute file name; don't forget to include a prefix for the filename
1022 (defcustom tramp-discard-garbage nil
1023 "*If non-nil, try to discard garbage sent by remote shell.
1024 Some shells send such garbage upon connection setup."
1028 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
1029 "*Alist specifying extra arguments to pass to the remote shell.
1030 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
1031 matching the shell file name and ARGS is a string specifying the
1034 This variable is only used when Tramp needs to start up another shell
1035 for tilde expansion. The extra arguments should typically prevent the
1036 shell from reading its init file."
1038 ;; This might be the wrong way to test whether the widget type
1039 ;; `alist' is available. Who knows the right way to test it?
1040 :type (if (get 'alist 'widget-type)
1041 '(alist :key-type string :value-type string)
1042 '(repeat (cons string string))))
1044 (defcustom tramp-prefix-format
1045 (if tramp-unified-filenames "/" "/[")
1046 "*String matching the very beginning of tramp file names.
1047 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1051 (defcustom tramp-prefix-regexp
1052 (concat "^" (regexp-quote tramp-prefix-format))
1053 "*Regexp matching the very beginning of tramp file names.
1054 Should always start with \"^\". Derived from `tramp-prefix-format'."
1058 (defcustom tramp-method-regexp
1060 "*Regexp matching methods identifiers."
1064 ;; It is a little bit annoying that in XEmacs case this delimeter is different
1065 ;; for single-hop and multi-hop cases.
1066 (defcustom tramp-postfix-single-method-format
1067 (if tramp-unified-filenames ":" "/")
1068 "*String matching delimeter between method and user or host names.
1069 Applicable for single-hop methods.
1070 Used in `tramp-make-tramp-file-name'."
1074 (defcustom tramp-postfix-single-method-regexp
1075 (regexp-quote tramp-postfix-single-method-format)
1076 "*Regexp matching delimeter between method and user or host names.
1077 Applicable for single-hop methods.
1078 Derived from `tramp-postfix-single-method-format'."
1082 (defcustom tramp-postfix-multi-method-format
1084 "*String matching delimeter between method and user or host names.
1085 Applicable for multi-hop methods.
1086 Used in `tramp-make-tramp-multi-file-name'."
1090 (defcustom tramp-postfix-multi-method-regexp
1091 (regexp-quote tramp-postfix-multi-method-format)
1092 "*Regexp matching delimeter between method and user or host names.
1093 Applicable for multi-hop methods.
1094 Derived from `tramp-postfix-multi-method-format'."
1098 (defcustom tramp-postfix-multi-hop-format
1099 (if tramp-unified-filenames ":" "/")
1100 "*String matching delimeter between host and next method.
1101 Applicable for multi-hop methods.
1102 Used in `tramp-make-tramp-multi-file-name'."
1106 (defcustom tramp-postfix-multi-hop-regexp
1107 (regexp-quote tramp-postfix-multi-hop-format)
1108 "*Regexp matching delimeter between host and next method.
1109 Applicable for multi-hop methods.
1110 Derived from `tramp-postfix-multi-hop-format'."
1114 (defcustom tramp-user-regexp
1116 "*Regexp matching user names."
1120 (defcustom tramp-postfix-user-format
1122 "*String matching delimeter between user and host names.
1123 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1127 (defcustom tramp-postfix-user-regexp
1128 (regexp-quote tramp-postfix-user-format)
1129 "*Regexp matching delimeter between user and host names.
1130 Derived from `tramp-postfix-user-format'."
1134 (defcustom tramp-host-regexp
1136 "*Regexp matching host names."
1140 (defcustom tramp-host-with-port-regexp
1142 "*Regexp matching host names."
1146 (defcustom tramp-postfix-host-format
1147 (if tramp-unified-filenames ":" "]")
1148 "*String matching delimeter between host names and localnames.
1149 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1153 (defcustom tramp-postfix-host-regexp
1154 (regexp-quote tramp-postfix-host-format)
1155 "*Regexp matching delimeter between host names and localnames.
1156 Derived from `tramp-postfix-host-format'."
1160 (defcustom tramp-localname-regexp
1162 "*Regexp matching localnames."
1166 ;; File name format.
1168 (defcustom tramp-file-name-structure
1172 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "\\)?"
1173 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
1174 "\\(" tramp-host-with-port-regexp "\\)" tramp-postfix-host-regexp
1175 "\\(" tramp-localname-regexp "\\)")
1178 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1179 the tramp file name structure.
1181 The first element REGEXP is a regular expression matching a tramp file
1182 name. The regex should contain parentheses around the method name,
1183 the user name, the host name, and the file name parts.
1185 The second element METHOD is a number, saying which pair of
1186 parentheses matches the method name. The third element USER is
1187 similar, but for the user name. The fourth element HOST is similar,
1188 but for the host name. The fifth element FILE is for the file name.
1189 These numbers are passed directly to `match-string', which see. That
1190 means the opening parentheses are counted to identify the pair.
1192 See also `tramp-file-name-regexp'."
1194 :type '(list (regexp :tag "File name regexp")
1195 (integer :tag "Paren pair for method name")
1196 (integer :tag "Paren pair for user name ")
1197 (integer :tag "Paren pair for host name ")
1198 (integer :tag "Paren pair for file name ")))
1201 (defconst tramp-file-name-regexp-unified
1203 "Value for `tramp-file-name-regexp' for unified remoting.
1204 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1205 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1208 (defconst tramp-file-name-regexp-separate
1210 "Value for `tramp-file-name-regexp' for separate remoting.
1211 XEmacs uses a separate filename syntax for Tramp and EFS.
1212 See `tramp-file-name-structure-separate' for more explanations.")
1215 (defcustom tramp-file-name-regexp
1216 (if tramp-unified-filenames
1217 tramp-file-name-regexp-unified
1218 tramp-file-name-regexp-separate)
1219 "*Regular expression matching file names handled by tramp.
1220 This regexp should match tramp file names but no other file names.
1221 \(When tramp.el is loaded, this regular expression is prepended to
1222 `file-name-handler-alist', and that is searched sequentially. Thus,
1223 if the tramp entry appears rather early in the `file-name-handler-alist'
1224 and is a bit too general, then some files might be considered tramp
1225 files which are not really tramp files.
1227 Please note that the entry in `file-name-handler-alist' is made when
1228 this file (tramp.el) is loaded. This means that this variable must be set
1229 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1230 updated after changing this variable.
1232 Also see `tramp-file-name-structure'."
1237 (defconst tramp-completion-file-name-regexp-unified
1238 "^/$\\|^/[^/:][^/]*$"
1239 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1240 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1241 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1244 (defconst tramp-completion-file-name-regexp-separate
1245 "^/\\([[][^]]*\\)?$"
1246 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1247 XEmacs uses a separate filename syntax for Tramp and EFS.
1248 See `tramp-file-name-structure-separate' for more explanations.")
1251 (defcustom tramp-completion-file-name-regexp
1252 (if tramp-unified-filenames
1253 tramp-completion-file-name-regexp-unified
1254 tramp-completion-file-name-regexp-separate)
1255 "*Regular expression matching file names handled by tramp completion.
1256 This regexp should match partial tramp file names only.
1258 Please note that the entry in `file-name-handler-alist' is made when
1259 this file (tramp.el) is loaded. This means that this variable must be set
1260 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1261 updated after changing this variable.
1263 Also see `tramp-file-name-structure'."
1267 (defcustom tramp-multi-file-name-structure
1271 "\\(" "\\(" tramp-method-regexp "\\)" "\\)?"
1272 "\\(" "\\(" tramp-postfix-multi-hop-regexp "%s" "\\)+" "\\)?"
1273 tramp-postfix-host-regexp "\\(" tramp-localname-regexp "\\)")
1275 "*Describes the file name structure of `multi' files.
1276 Multi files allow you to contact a remote host in several hops.
1277 This is a list of four elements (REGEXP METHOD HOP LOCALNAME).
1279 The first element, REGEXP, gives a regular expression to match against
1280 the file name. In this regular expression, `%s' is replaced with the
1281 value of `tramp-multi-file-name-hop-structure'. (Note: in order to
1282 allow multiple hops, you normally want to use something like
1283 \"\\\\(\\\\(%s\\\\)+\\\\)\" in the regular expression. The outer pair
1284 of parentheses is used for the HOP element, see below.)
1286 All remaining elements are numbers. METHOD gives the number of the
1287 paren pair which matches the method name. HOP gives the number of the
1288 paren pair which matches the hop sequence. LOCALNAME gives the number of
1289 the paren pair which matches the localname (pathname) on the remote host.
1291 LOCALNAME can also be negative, which means to count from the end. Ie, a
1292 value of -1 means the last paren pair.
1294 I think it would be good if the regexp matches the whole of the
1295 string, but I haven't actually tried what happens if it doesn't..."
1297 :type '(list (regexp :tag "File name regexp")
1298 (integer :tag "Paren pair for method name")
1299 (integer :tag "Paren pair for hops")
1300 (integer :tag "Paren pair to match localname")))
1302 (defcustom tramp-multi-file-name-hop-structure
1305 "\\(" tramp-method-regexp "\\)" tramp-postfix-multi-method-regexp
1306 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
1307 "\\(" tramp-host-with-port-regexp "\\)")
1309 "*Describes the structure of a hop in multi files.
1310 This is a list of four elements (REGEXP METHOD USER HOST). First
1311 element REGEXP is used to match against the hop. Pair number METHOD
1312 matches the method of one hop, pair number USER matches the user of
1313 one hop, pair number HOST matches the host of one hop.
1315 This regular expression should match exactly all of one hop."
1317 :type '(list (regexp :tag "Hop regexp")
1318 (integer :tag "Paren pair for method name")
1319 (integer :tag "Paren pair for user name")
1320 (integer :tag "Paren pair for host name")))
1322 (defcustom tramp-make-multi-tramp-file-format
1324 (concat tramp-prefix-format "%m")
1325 (concat tramp-postfix-multi-hop-format
1326 "%m" tramp-postfix-multi-method-format
1327 "%u" tramp-postfix-user-format
1329 (concat tramp-postfix-host-format "%p"))
1330 "*Describes how to construct a `multi' file name.
1331 This is a list of three elements PREFIX, HOP and LOCALNAME.
1333 The first element PREFIX says how to construct the prefix, the second
1334 element HOP specifies what each hop looks like, and the final element
1335 LOCALNAME says how to construct the localname (pathname).
1337 In PREFIX, `%%' means `%' and `%m' means the method name.
1339 In HOP, `%%' means `%' and `%m', `%u', `%h' mean the hop method, hop
1340 user and hop host, respectively.
1342 In LOCALNAME, `%%' means `%' and `%p' means the localname.
1344 The resulting file name always contains one copy of PREFIX and one
1345 copy of LOCALNAME, but there is one copy of HOP for each hop in the file
1348 Note: the current implementation requires the prefix to contain the
1349 method name, followed by all the hops, and the localname must come
1352 :type '(list string string string))
1354 (defcustom tramp-terminal-type "dumb"
1355 "*Value of TERM environment variable for logging in to remote host.
1356 Because Tramp wants to parse the output of the remote shell, it is easily
1357 confused by ANSI color escape sequences and suchlike. Often, shell init
1358 files conditionalize this setup based on the TERM environment variable."
1362 (defcustom tramp-completion-without-shell-p nil
1363 "*If nil, use shell wildcards for completion, else rely on Lisp only.
1364 Using shell wildcards for completions has the advantage that it can be
1365 fast even in large directories, but completion is always
1366 case-sensitive. Relying on Lisp only means that case-insensitive
1367 completion is possible (subject to the variable `completion-ignore-case'),
1368 but it might be slow on large directories."
1372 (defcustom tramp-actions-before-shell
1373 '((tramp-password-prompt-regexp tramp-action-password)
1374 (tramp-login-prompt-regexp tramp-action-login)
1375 (shell-prompt-pattern tramp-action-succeed)
1376 (tramp-shell-prompt-pattern tramp-action-succeed)
1377 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1378 (tramp-yesno-prompt-regexp tramp-action-yesno)
1379 (tramp-yn-prompt-regexp tramp-action-yn)
1380 (tramp-terminal-prompt-regexp tramp-action-terminal)
1381 (tramp-process-alive-regexp tramp-action-process-alive))
1382 "List of pattern/action pairs.
1383 Whenever a pattern matches, the corresponding action is performed.
1384 Each item looks like (PATTERN ACTION).
1386 The PATTERN should be a symbol, a variable. The value of this
1387 variable gives the regular expression to search for. Note that the
1388 regexp must match at the end of the buffer, \"\\'\" is implicitly
1391 The ACTION should also be a symbol, but a function. When the
1392 corresponding PATTERN matches, the ACTION function is called."
1394 :type '(repeat (list variable function)))
1396 (defcustom tramp-actions-copy-out-of-band
1397 '((tramp-password-prompt-regexp tramp-action-password)
1398 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1399 (tramp-copy-failed-regexp tramp-action-copy-failed)
1400 (tramp-process-alive-regexp tramp-action-out-of-band))
1401 "List of pattern/action pairs.
1402 This list is used for copying/renaming with out-of-band methods.
1403 See `tramp-actions-before-shell' for more info."
1405 :type '(repeat (list variable function)))
1407 (defcustom tramp-multi-actions
1408 '((tramp-password-prompt-regexp tramp-multi-action-password)
1409 (tramp-login-prompt-regexp tramp-multi-action-login)
1410 (shell-prompt-pattern tramp-multi-action-succeed)
1411 (tramp-shell-prompt-pattern tramp-multi-action-succeed)
1412 (tramp-wrong-passwd-regexp tramp-multi-action-permission-denied)
1413 (tramp-process-alive-regexp tramp-multi-action-process-alive))
1414 "List of pattern/action pairs.
1415 This list is used for each hop in multi-hop connections.
1416 See `tramp-actions-before-shell' for more info."
1418 :type '(repeat (list variable function)))
1420 (defcustom tramp-initial-commands
1423 "unset autocorrect")
1424 "List of commands to send to the first remote shell that we see.
1425 These commands will be sent to any shell, and thus they should be
1426 designed to work in such circumstances. Also, restrict the commands
1427 to the bare necessity for getting the remote shell into a state
1428 where it is possible to execute the Bourne-ish shell.
1430 At the moment, the command to execute the Bourne-ish shell uses strange
1431 quoting which `tcsh' tries to correct, so we send the command \"unset
1432 autocorrect\" to the remote host."
1434 :type '(repeat string))
1436 ;; Chunked sending kluge. We set this to 500 for black-listed constellations
1437 ;; known to have a bug in `process-send-string'; some ssh connections appear
1438 ;; to drop bytes when data is sent too quickly. There is also a connection
1439 ;; buffer local variable, which is computed depending on remote host properties
1440 ;; when `tramp-chunksize' is zero or nil.
1441 (defcustom tramp-chunksize
1442 (when (and (not (featurep 'xemacs))
1443 (memq system-type '(hpux)))
1445 ;; Parentheses in docstring starting at beginning of line are escaped.
1446 ;; Fontification is messed up when
1447 ;; `open-paren-in-column-0-is-defun-start' set to t.
1448 "*If non-nil, chunksize for sending input to local process.
1449 It is necessary only on systems which have a buggy `process-send-string'
1450 implementation. The necessity, whether this variable must be set, can be
1451 checked via the following code:
1454 (let* ((user \"xxx\") (host \"yyy\")
1456 (sent init) (received init))
1457 (while (= sent received)
1458 (setq sent (+ sent step))
1460 (let ((proc (start-process (buffer-name) (current-buffer)
1461 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1462 (when (memq (process-status proc) '(run open))
1463 (process-send-string proc (make-string sent ?\\ ))
1464 (process-send-eof proc)
1465 (process-send-eof proc))
1466 (while (not (progn (goto-char (point-min))
1467 (re-search-forward \"\\\\w+\" (point-max) t)))
1468 (accept-process-output proc 1))
1469 (when (memq (process-status proc) '(run open))
1470 (setq received (string-to-number (match-string 0)))
1471 (delete-process proc)
1472 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1474 (if (> sent (+ init step))
1475 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1477 (message \"Test does not work\")
1478 (display-buffer (current-buffer))
1481 In the Emacs normally running Tramp, evaluate the above code
1482 \(replace \"xxx\" and \"yyy\" by the remote user and host name,
1483 respectively). You can do this, for example, by pasting it into
1484 the `*scratch*' buffer and then hitting C-j with the cursor after the
1485 last closing parenthesis. Note that it works only if you have configured
1486 \"ssh\" to run without password query, see ssh-agent(1).
1488 You will see the number of bytes sent successfully to the remote host.
1489 If that number exceeds 1000, you can stop the execution by hitting
1490 C-g, because your Emacs is likely clean.
1492 If your Emacs is buggy, the code stops and gives you an indication
1493 about the value `tramp-chunksize' should be set. Maybe you could just
1494 experiment a bit, e.g. changing the values of `init' and `step'
1495 in the third line of the code.
1497 When it is necessary to set `tramp-chunksize', you might consider to
1498 use an out-of-the-band method (like \"scp\") instead of an internal one
1499 \(like \"ssh\"), because setting `tramp-chunksize' to non-nil decreases
1502 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1503 this variable to be set as well."
1505 :type '(choice (const nil) integer))
1507 ;; Logging in to a remote host normally requires obtaining a pty. But
1508 ;; Emacs on MacOS X has process-connection-type set to nil by default,
1509 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1510 ;; for an override of the system default.
1511 (defcustom tramp-process-connection-type t
1512 "Overrides `process-connection-type' for connections from Tramp.
1513 Tramp binds process-connection-type to the value given here before
1514 opening a connection to a remote host."
1516 :type '(choice (const nil) (const t) (const pty)))
1518 ;;; Internal Variables:
1520 (defvar tramp-buffer-file-attributes nil
1521 "Holds the `ls -ild' output for the current buffer.
1522 This variable is local to each buffer. It is not used if the remote
1523 machine groks Perl. If it is used, it's used as an emulation for
1524 the visited file modtime.")
1525 (make-variable-buffer-local 'tramp-buffer-file-attributes)
1527 (defvar tramp-md5-function
1528 (cond ((and (require 'md5) (fboundp 'md5)) 'md5)
1529 ((fboundp 'md5-encode)
1530 (lambda (x) (base64-encode-string
1531 (funcall (symbol-function 'md5-encode) x))))
1532 (t (error "Couldn't find an `md5' function")))
1533 "Function to call for running the MD5 algorithm.")
1535 (defvar tramp-end-of-output
1537 (funcall tramp-md5-function
1539 (prin1-to-string process-environment)
1540 (current-time-string)
1542 ;; (if (fboundp 'directory-files-and-attributes)
1543 ;; (funcall 'directory-files-and-attributes
1544 ;; (or (getenv "HOME")
1545 ;; (tramp-temporary-file-directory)))
1548 ;; (cons x (file-attributes x)))
1549 ;; (directory-files (or (getenv "HOME")
1550 ;; (tramp-temporary-file-directory))
1553 "String used to recognize end of output.")
1555 (defvar tramp-connection-function nil
1556 "This internal variable holds a parameter for `tramp-methods'.
1557 In the connection buffer, this variable has the value of the like-named
1558 method parameter, as specified in `tramp-methods' (which see).")
1560 (defvar tramp-remote-sh nil
1561 "This internal variable holds a parameter for `tramp-methods'.
1562 In the connection buffer, this variable has the value of the like-named
1563 method parameter, as specified in `tramp-methods' (which see).")
1565 (defvar tramp-login-program nil
1566 "This internal variable holds a parameter for `tramp-methods'.
1567 In the connection buffer, this variable has the value of the like-named
1568 method parameter, as specified in `tramp-methods' (which see).")
1570 (defvar tramp-login-args nil
1571 "This internal variable holds a parameter for `tramp-methods'.
1572 In the connection buffer, this variable has the value of the like-named
1573 method parameter, as specified in `tramp-methods' (which see).")
1575 (defvar tramp-copy-program nil
1576 "This internal variable holds a parameter for `tramp-methods'.
1577 In the connection buffer, this variable has the value of the like-named
1578 method parameter, as specified in `tramp-methods' (which see).")
1580 (defvar tramp-copy-args nil
1581 "This internal variable holds a parameter for `tramp-methods'.
1582 In the connection buffer, this variable has the value of the like-named
1583 method parameter, as specified in `tramp-methods' (which see).")
1585 (defvar tramp-copy-keep-date-arg nil
1586 "This internal variable holds a parameter for `tramp-methods'.
1587 In the connection buffer, this variable has the value of the like-named
1588 method parameter, as specified in `tramp-methods' (which see).")
1590 (defvar tramp-encoding-command nil
1591 "This internal variable holds a parameter for `tramp-methods'.
1592 In the connection buffer, this variable has the value of the like-named
1593 method parameter, as specified in `tramp-methods' (which see).")
1595 (defvar tramp-decoding-command nil
1596 "This internal variable holds a parameter for `tramp-methods'.
1597 In the connection buffer, this variable has the value of the like-named
1598 method parameter, as specified in `tramp-methods' (which see).")
1600 (defvar tramp-encoding-function nil
1601 "This internal variable holds a parameter for `tramp-methods'.
1602 In the connection buffer, this variable has the value of the like-named
1603 method parameter, as specified in `tramp-methods' (which see).")
1605 (defvar tramp-decoding-function nil
1606 "This internal variable holds a parameter for `tramp-methods'.
1607 In the connection buffer, this variable has the value of the like-named
1608 method parameter, as specified in `tramp-methods' (which see).")
1610 (defvar tramp-password-end-of-line nil
1611 "This internal variable holds a parameter for `tramp-methods'.
1612 In the connection buffer, this variable has the value of the like-named
1613 method parameter, as specified in `tramp-methods' (which see).")
1615 ;; CCC `local in each buffer'?
1616 (defvar tramp-ls-command nil
1617 "This command is used to get a long listing with numeric user and group ids.
1618 This variable is automatically made buffer-local to each rsh process buffer
1619 upon opening the connection.")
1621 (defvar tramp-current-multi-method nil
1622 "Name of `multi' connection method for this *tramp* buffer, or nil if not multi.
1623 This variable is automatically made buffer-local to each rsh process buffer
1624 upon opening the connection.")
1626 (defvar tramp-current-method nil
1627 "Connection method for this *tramp* buffer.
1628 This variable is automatically made buffer-local to each rsh process buffer
1629 upon opening the connection.")
1631 (defvar tramp-current-user nil
1632 "Remote login name for this *tramp* buffer.
1633 This variable is automatically made buffer-local to each rsh process buffer
1634 upon opening the connection.")
1636 (defvar tramp-current-host nil
1637 "Remote host for this *tramp* buffer.
1638 This variable is automatically made buffer-local to each rsh process buffer
1639 upon opening the connection.")
1641 (defvar tramp-test-groks-nt nil
1642 "Whether the `test' command groks the `-nt' switch.
1643 \(`test A -nt B' tests if file A is newer than file B.)
1644 This variable is automatically made buffer-local to each rsh process buffer
1645 upon opening the connection.")
1647 (defvar tramp-file-exists-command nil
1648 "Command to use for checking if a file exists.
1649 This variable is automatically made buffer-local to each rsh process buffer
1650 upon opening the connection.")
1652 (defconst tramp-uudecode "\
1654 \(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1658 "Shell function to implement `uudecode' to standard output.
1659 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
1660 for this or `uudecode -p', but some systems don't, and for them
1661 we have this shell function.")
1663 ;; Perl script to implement `file-attributes' in a Lisp `read'able
1664 ;; output. If you are hacking on this, note that you get *no* output
1665 ;; unless this spits out a complete line, including the '\n' at the
1667 ;; The device number is returned as "-1", because there will be a virtual
1668 ;; device number set in `tramp-handle-file-attributes'
1669 (defconst tramp-perl-file-attributes "\
1670 @stat = lstat($ARGV[0]);
1671 if (($stat[2] & 0170000) == 0120000)
1673 $type = readlink($ARGV[0]);
1674 $type = \"\\\"$type\\\"\";
1676 elsif (($stat[2] & 0170000) == 040000)
1684 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1685 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1687 \"(%s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) -1)\\n\",
1692 $stat[8] >> 16 & 0xffff,
1694 $stat[9] >> 16 & 0xffff,
1696 $stat[10] >> 16 & 0xffff,
1700 $stat[1] >> 16 & 0xffff,
1703 "Perl script to produce output suitable for use with `file-attributes'
1704 on the remote file system.")
1706 (defconst tramp-perl-directory-files-and-attributes "\
1707 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
1708 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
1709 @list = readdir(DIR);
1713 for($i = 0; $i < $n; $i++)
1715 $filename = $list[$i];
1716 @stat = lstat($filename);
1717 if (($stat[2] & 0170000) == 0120000)
1719 $type = readlink($filename);
1720 $type = \"\\\"$type\\\"\";
1722 elsif (($stat[2] & 0170000) == 040000)
1730 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1731 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1733 \"(\\\"%s\\\" %s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) (%u %u))\\n\",
1739 $stat[8] >> 16 & 0xffff,
1741 $stat[9] >> 16 & 0xffff,
1743 $stat[10] >> 16 & 0xffff,
1747 $stat[1] >> 16 & 0xffff,
1749 $stat[0] >> 16 & 0xffff,
1753 "Perl script implementing `directory-files-attributes' as Lisp `read'able
1756 ;; ;; These two use uu encoding.
1757 ;; (defvar tramp-perl-encode "%s -e'\
1758 ;; print qq(begin 644 xxx\n);
1761 ;; while (read(STDIN, $s, 45)) {
1762 ;; print pack(q(u), $s);
1767 ;; "Perl program to use for encoding a file.
1768 ;; Escape sequence %s is replaced with name of Perl binary.")
1770 ;; (defvar tramp-perl-decode "%s -ne '
1771 ;; print unpack q(u), $_;
1773 ;; "Perl program to use for decoding a file.
1774 ;; Escape sequence %s is replaced with name of Perl binary.")
1776 ;; These two use base64 encoding.
1777 (defvar tramp-perl-encode-with-module
1778 "perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)'"
1779 "Perl program to use for encoding a file.
1780 Escape sequence %s is replaced with name of Perl binary.
1781 This string is passed to `format', so percent characters need to be doubled.
1782 This implementation requires the MIME::Base64 Perl module to be installed
1783 on the remote host.")
1785 (defvar tramp-perl-decode-with-module
1786 "perl -MMIME::Base64 -0777 -ne 'print decode_base64($_)'"
1787 "Perl program to use for decoding a file.
1788 Escape sequence %s is replaced with name of Perl binary.
1789 This string is passed to `format', so percent characters need to be doubled.
1790 This implementation requires the MIME::Base64 Perl module to be installed
1791 on the remote host.")
1793 (defvar tramp-perl-encode
1795 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1796 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
1797 # Free Software Foundation, Inc.
1802 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1803 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1808 # We read in chunks of 54 bytes, to generate output lines
1809 # of 72 chars (plus end of line)
1812 while (my $data = <STDIN>) {
1815 # Only for the last chunk, and only if did not fill the last three-byte packet
1817 my $mod = length($data) %% 3;
1818 $pad = q(=) x (3 - $mod) if $mod;
1821 # Not the fastest method, but it is simple: unpack to binary string, split
1822 # by groups of 6 bits and convert back from binary to byte; then map into
1823 # the translation table
1827 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1832 "Perl program to use for encoding a file.
1833 Escape sequence %s is replaced with name of Perl binary.
1834 This string is passed to `format', so percent characters need to be doubled.")
1836 (defvar tramp-perl-decode
1838 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1839 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007
1840 # Free Software Foundation, Inc.
1845 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1846 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1849 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1853 # We are going to accumulate into $pending to accept any line length
1854 # (we do not check they are <= 76 chars as the RFC says)
1857 while (my $data = <STDIN>) {
1860 # If we find one or two =, we have reached the end and
1861 # any following data is to be discarded
1862 my $finished = $data =~ s/(==?).*/$1/;
1865 my $len = length($pending);
1866 my $chunk = substr($pending, 0, $len & ~3);
1867 $pending = substr($pending, $len & ~3 + 1);
1869 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1870 # split in 8-bit chunks and convert back to char.
1873 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1878 "Perl program to use for decoding a file.
1879 Escape sequence %s is replaced with name of Perl binary.
1880 This string is passed to `format', so percent characters need to be doubled.")
1882 (defconst tramp-file-mode-type-map '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1884 (2 . "c") ; character device
1885 (3 . "m") ; multiplexed character device (v7)
1886 (4 . "d") ; directory
1887 (5 . "?") ; Named special file (XENIX)
1888 (6 . "b") ; block device
1889 (7 . "?") ; multiplexed block device (v7)
1890 (8 . "-") ; regular file
1891 (9 . "n") ; network special file (HP-UX)
1892 (10 . "l") ; symlink
1893 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
1895 (13 . "D") ; door special (Solaris)
1896 (14 . "w")) ; whiteout (BSD)
1897 "A list of file types returned from the `stat' system call.
1898 This is used to map a mode number to a permission string.")
1900 (defvar tramp-dos-coding-system
1901 (if (and (fboundp 'coding-system-p)
1902 (funcall 'coding-system-p '(dos)))
1905 "Some Emacsen know the `dos' coding system, others need `undecided-dos'.")
1907 (defvar tramp-last-cmd nil
1908 "Internal Tramp variable recording the last command sent.
1909 This variable is buffer-local in every buffer.")
1910 (make-variable-buffer-local 'tramp-last-cmd)
1912 (defvar tramp-process-echoes nil
1913 "Whether to process echoes from the remote shell.")
1915 (defvar tramp-last-cmd-time nil
1916 "Internal Tramp variable recording the time when the last cmd was sent.
1917 This variable is buffer-local in every buffer.")
1918 (make-variable-buffer-local 'tramp-last-cmd-time)
1920 ;; This variable does not have the right value in XEmacs. What should
1921 ;; I use instead of find-operation-coding-system in XEmacs?
1922 (defvar tramp-feature-write-region-fix
1923 (when (fboundp 'find-operation-coding-system)
1924 (let ((file-coding-system-alist '(("test" emacs-mule))))
1925 (funcall (symbol-function 'find-operation-coding-system)
1926 'write-region 0 0 "" nil "test")))
1927 "Internal variable to say if `write-region' chooses the right coding.
1928 Older versions of Emacs chose the coding system for `write-region' based
1929 on the FILENAME argument, even if VISIT was a string.")
1931 ;; New handlers should be added here. The following operations can be
1932 ;; handled using the normal primitives: file-name-as-directory,
1933 ;; file-name-directory, file-name-nondirectory,
1934 ;; file-name-sans-versions, get-file-buffer.
1935 (defconst tramp-file-name-handler-alist
1937 (load . tramp-handle-load)
1938 (make-symbolic-link . tramp-handle-make-symbolic-link)
1939 (file-name-directory . tramp-handle-file-name-directory)
1940 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1941 (file-truename . tramp-handle-file-truename)
1942 (file-exists-p . tramp-handle-file-exists-p)
1943 (file-directory-p . tramp-handle-file-directory-p)
1944 (file-executable-p . tramp-handle-file-executable-p)
1945 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p)
1946 (file-readable-p . tramp-handle-file-readable-p)
1947 (file-regular-p . tramp-handle-file-regular-p)
1948 (file-symlink-p . tramp-handle-file-symlink-p)
1949 (file-writable-p . tramp-handle-file-writable-p)
1950 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
1951 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
1952 (file-attributes . tramp-handle-file-attributes)
1953 (file-modes . tramp-handle-file-modes)
1954 (directory-files . tramp-handle-directory-files)
1955 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes)
1956 (file-name-all-completions . tramp-handle-file-name-all-completions)
1957 (file-name-completion . tramp-handle-file-name-completion)
1958 (add-name-to-file . tramp-handle-add-name-to-file)
1959 (copy-file . tramp-handle-copy-file)
1960 (rename-file . tramp-handle-rename-file)
1961 (set-file-modes . tramp-handle-set-file-modes)
1962 (make-directory . tramp-handle-make-directory)
1963 (delete-directory . tramp-handle-delete-directory)
1964 (delete-file . tramp-handle-delete-file)
1965 (directory-file-name . tramp-handle-directory-file-name)
1966 (shell-command . tramp-handle-shell-command)
1967 (process-file . tramp-handle-process-file)
1968 (insert-directory . tramp-handle-insert-directory)
1969 (expand-file-name . tramp-handle-expand-file-name)
1970 (file-local-copy . tramp-handle-file-local-copy)
1971 (file-remote-p . tramp-handle-file-remote-p)
1972 (insert-file-contents . tramp-handle-insert-file-contents)
1973 (write-region . tramp-handle-write-region)
1974 (find-backup-file-name . tramp-handle-find-backup-file-name)
1975 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
1976 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
1977 (dired-compress-file . tramp-handle-dired-compress-file)
1978 (dired-call-process . tramp-handle-dired-call-process)
1979 (dired-recursive-delete-directory
1980 . tramp-handle-dired-recursive-delete-directory)
1981 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
1982 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime))
1983 "Alist of handler functions.
1984 Operations not mentioned here will be handled by the normal Emacs functions.")
1986 ;; Handlers for partial tramp file names. For Emacs just
1987 ;; `file-name-all-completions' is needed.
1989 (defconst tramp-completion-file-name-handler-alist
1990 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions)
1991 (file-name-completion . tramp-completion-handle-file-name-completion))
1992 "Alist of completion handler functions.
1993 Used for file names matching `tramp-file-name-regexp'. Operations not
1994 mentioned here will be handled by `tramp-file-name-handler-alist' or the
1995 normal Emacs functions.")
1997 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
1998 (defvar tramp-foreign-file-name-handler-alist
1999 ;; (identity . tramp-sh-file-name-handler) should always be the last
2000 ;; entry, since `identity' always matches.
2001 '((identity . tramp-sh-file-name-handler))
2002 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
2003 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
2006 ;;; Internal functions which must come first.
2008 (defsubst tramp-message (level fmt-string &rest args)
2009 "Emit a message depending on verbosity level.
2010 First arg LEVEL says to be quiet if `tramp-verbose' is less than LEVEL. The
2011 message is emitted only if `tramp-verbose' is greater than or equal to LEVEL.
2012 Calls function `message' with FMT-STRING as control string and the remaining
2013 ARGS to actually emit the message (if applicable).
2015 This function expects to be called from the tramp buffer only!"
2016 (when (<= level tramp-verbose)
2017 (apply #'message (concat "tramp: " fmt-string) args)
2018 (when tramp-debug-buffer
2021 (tramp-get-debug-buffer
2022 tramp-current-multi-method tramp-current-method
2023 tramp-current-user tramp-current-host))
2024 (goto-char (point-max))
2027 (tramp-insert-with-face
2029 (concat "# " (apply #'format fmt-string args) "\n"))))))
2031 (defun tramp-message-for-buffer
2032 (multi-method method user host level fmt-string &rest args)
2033 "Like `tramp-message' but temporarily switches to the tramp buffer.
2034 First three args METHOD, USER, and HOST identify the tramp buffer to use,
2035 remaining args passed to `tramp-message'."
2037 (set-buffer (tramp-get-buffer multi-method method user host))
2038 (apply 'tramp-message level fmt-string args)))
2040 (defsubst tramp-line-end-position nil
2041 "Return point at end of line.
2042 Calls `line-end-position' or `point-at-eol' if defined, else
2043 own implementation."
2045 ((fboundp 'line-end-position) (funcall (symbol-function 'line-end-position)))
2046 ((fboundp 'point-at-eol) (funcall (symbol-function 'point-at-eol)))
2047 (t (save-excursion (end-of-line) (point)))))
2049 (defmacro with-parsed-tramp-file-name (filename var &rest body)
2050 "Parse a Tramp filename and make components available in the body.
2052 First arg FILENAME is evaluated and dissected into its components.
2053 Second arg VAR is a symbol. It is used as a variable name to hold
2054 the filename structure. It is also used as a prefix for the variables
2055 holding the components. For example, if VAR is the symbol `foo', then
2056 `foo' will be bound to the whole structure, `foo-multi-method' will
2057 be bound to the multi-method component, and so on for `foo-method',
2058 `foo-user', `foo-host', `foo-localname'.
2060 Remaining args are Lisp expressions to be evaluated (inside an implicit
2063 If VAR is nil, then we bind `v' to the structure and `multi-method',
2064 `method', `user', `host', `localname' to the components."
2065 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
2066 (,(if var (intern (concat (symbol-name var) "-multi-method")) 'multi-method)
2067 (tramp-file-name-multi-method ,(or var 'v)))
2068 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
2069 (tramp-file-name-method ,(or var 'v)))
2070 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
2071 (tramp-file-name-user ,(or var 'v)))
2072 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
2073 (tramp-file-name-host ,(or var 'v)))
2074 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
2075 (tramp-file-name-localname ,(or var 'v))))
2078 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
2079 ;; Enable debugging.
2080 (def-edebug-spec with-parsed-tramp-file-name (form symbolp body))
2081 ;; Highlight as keyword.
2082 (when (functionp 'font-lock-add-keywords)
2083 (funcall 'font-lock-add-keywords
2084 'emacs-lisp-mode '("\\<with-parsed-tramp-file-name\\>")))
2086 (defmacro tramp-let-maybe (variable value &rest body)
2087 "Let-bind VARIABLE to VALUE in BODY, but only if VARIABLE is not obsolete.
2088 BODY is executed whether or not the variable is obsolete.
2089 The intent is to protect against `obsolete variable' warnings."
2090 `(if (get ',variable 'byte-obsolete-variable)
2092 (let ((,variable ,value))
2094 (put 'tramp-let-maybe 'lisp-indent-function 2)
2095 (put 'tramp-let-maybe 'edebug-form-spec t)
2097 ;;; Config Manipulation Functions:
2099 (defun tramp-set-completion-function (method function-list)
2100 "Sets the list of completion functions for METHOD.
2101 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2102 The FUNCTION is intended to parse FILE according its syntax.
2103 It might be a predefined FUNCTION, or a user defined FUNCTION.
2104 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
2105 `tramp-parse-sconfig',`tramp-parse-hosts', `tramp-parse-passwd',
2106 and `tramp-parse-netrc'.
2110 (tramp-set-completion-function
2112 '((tramp-parse-sconfig \"/etc/ssh_config\")
2113 (tramp-parse-sconfig \"~/.ssh/config\")))"
2115 (let ((r function-list)
2117 (setq tramp-completion-function-alist
2118 (delete (assoc method tramp-completion-function-alist)
2119 tramp-completion-function-alist))
2122 ;; Remove double entries
2123 (when (member (car v) (cdr v))
2124 (setcdr v (delete (car v) (cdr v))))
2125 ;; Check for function and file
2126 (unless (and (functionp (nth 0 (car v)))
2127 (file-exists-p (nth 1 (car v))))
2128 (setq r (delete (car v) r)))
2132 (add-to-list 'tramp-completion-function-alist
2135 (defun tramp-get-completion-function (method)
2136 "Returns list of completion functions for METHOD.
2137 For definition of that list see `tramp-set-completion-function'."
2138 (cdr (assoc method tramp-completion-function-alist)))
2140 ;;; File Name Handler Functions:
2142 (defun tramp-handle-make-symbolic-link
2143 (filename linkname &optional ok-if-already-exists)
2144 "Like `make-symbolic-link' for tramp files.
2145 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
2146 the symlink. If LINKNAME is a Tramp file, only the localname component is
2147 used as the target of the symlink.
2149 If LINKNAME is a Tramp file and the localname component is relative, then
2150 it is expanded first, before the localname component is taken. Note that
2151 this can give surprising results if the user/host for the source and
2152 target of the symlink differ."
2153 (with-parsed-tramp-file-name linkname l
2154 (let ((ln (tramp-get-remote-ln l-multi-method l-method l-user l-host))
2155 (cwd (file-name-directory l-localname)))
2158 (list "Making a symbolic link."
2159 "ln(1) does not exist on the remote host.")))
2161 ;; Do the 'confirm if exists' thing.
2162 (when (file-exists-p linkname)
2164 (if (or (null ok-if-already-exists) ; not allowed to exist
2165 (and (numberp ok-if-already-exists)
2168 "File %s already exists; make it a link anyway? "
2170 (signal 'file-already-exists (list "File already exists" l-localname))
2171 (delete-file linkname)))
2173 ;; If FILENAME is a Tramp name, use just the localname component.
2174 (when (tramp-tramp-file-p filename)
2175 (setq filename (tramp-file-name-localname
2176 (tramp-dissect-file-name
2177 (expand-file-name filename)))))
2179 ;; Right, they are on the same host, regardless of user, method, etc.
2180 ;; We now make the link on the remote machine. This will occur as the user
2181 ;; that FILENAME belongs to.
2183 (tramp-send-command-and-check
2184 l-multi-method l-method l-user l-host
2185 (format "cd %s && %s -sf %s %s"
2192 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
2193 "Like `load' for tramp files. Not implemented!"
2194 (unless (file-name-absolute-p file)
2195 (error "Tramp cannot `load' files without absolute file name"))
2196 (with-parsed-tramp-file-name file nil
2198 (cond ((file-exists-p (concat file ".elc"))
2199 (setq file (concat file ".elc")))
2200 ((file-exists-p (concat file ".el"))
2201 (setq file (concat file ".el")))))
2203 ;; The first condition is always true for absolute file names.
2204 ;; Included for safety's sake.
2205 (unless (or (file-name-directory file)
2206 (string-match "\\.elc?\\'" file))
2207 (error "File `%s' does not include a `.el' or `.elc' suffix"
2210 (when (not (file-exists-p file))
2211 (error "Cannot load nonexistent file `%s'" file)))
2212 (if (not (file-exists-p file))
2215 (message "Loading %s..." file))
2216 (let ((local-copy (file-local-copy file)))
2217 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2218 (load local-copy noerror t t)
2219 (delete-file local-copy))
2221 (message "Loading %s...done" file))
2224 ;; Localname manipulation functions that grok TRAMP localnames...
2225 (defun tramp-handle-file-name-directory (file)
2226 "Like `file-name-directory' but aware of TRAMP files."
2227 ;; Everything except the last filename thing is the directory.
2228 (with-parsed-tramp-file-name file nil
2229 ;; Run the command on the localname portion only.
2230 (tramp-make-tramp-file-name
2231 multi-method method user host (file-name-directory (or localname "")))))
2233 (defun tramp-handle-file-name-nondirectory (file)
2234 "Like `file-name-nondirectory' but aware of TRAMP files."
2235 (with-parsed-tramp-file-name file nil
2236 (file-name-nondirectory localname)))
2238 (defun tramp-handle-file-truename (filename &optional counter prev-dirs)
2239 "Like `file-truename' for tramp files."
2240 (with-parsed-tramp-file-name (expand-file-name filename) nil
2241 (let* ((steps (tramp-split-string localname "/"))
2242 (localnamedir (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
2243 (file-name-as-directory localname)))
2244 (is-dir (string= localname localnamedir))
2247 ;; Don't make the following value larger than necessary.
2248 ;; People expect an error message in a timely fashion when
2249 ;; something is wrong; otherwise they might think that Emacs
2250 ;; is hung. Of course, correctness has to come first.
2252 (result nil) ;result steps in reverse order
2254 (tramp-message-for-buffer
2255 multi-method method user host
2256 10 "Finding true name for `%s'" filename)
2257 (while (and steps (< numchase numchase-limit))
2258 (setq thisstep (pop steps))
2259 (tramp-message-for-buffer
2260 multi-method method user host
2262 (mapconcat 'identity
2263 (append '("") (reverse result) (list thisstep))
2265 (setq symlink-target
2266 (nth 0 (file-attributes
2267 (tramp-make-tramp-file-name
2268 multi-method method user host
2269 (mapconcat 'identity
2274 (cond ((string= "." thisstep)
2275 (tramp-message-for-buffer multi-method method user host
2276 10 "Ignoring step `.'"))
2277 ((string= ".." thisstep)
2278 (tramp-message-for-buffer multi-method method user host
2279 10 "Processing step `..'")
2281 ((stringp symlink-target)
2282 ;; It's a symlink, follow it.
2283 (tramp-message-for-buffer
2284 multi-method method user host
2285 10 "Follow symlink to %s" symlink-target)
2286 (setq numchase (1+ numchase))
2287 (when (file-name-absolute-p symlink-target)
2289 ;; If the symlink was absolute, we'll get a string like
2290 ;; "/user@host:/some/target"; extract the
2291 ;; "/some/target" part from it.
2292 (when (tramp-tramp-file-p symlink-target)
2293 (with-parsed-tramp-file-name symlink-target sym
2294 (unless (equal (list multi-method method user host)
2295 (list sym-multi-method sym-method
2297 (error "Symlink target `%s' on wrong host"
2299 (setq symlink-target localname)))
2301 (append (tramp-split-string symlink-target "/") steps)))
2304 (setq result (cons thisstep result)))))
2305 (when (>= numchase numchase-limit)
2306 (error "Maximum number (%d) of symlinks exceeded" numchase-limit))
2307 (setq result (reverse result))
2308 ;; Combine list to form string.
2311 (mapconcat 'identity (cons "" result) "/")
2313 (when (and is-dir (or (string= "" result)
2314 (not (string= (substring result -1) "/"))))
2315 (setq result (concat result "/")))
2316 (tramp-message-for-buffer
2317 multi-method method user host
2318 10 "True name of `%s' is `%s'" filename result)
2319 (tramp-make-tramp-file-name
2320 multi-method method user host result))))
2324 (defun tramp-handle-file-exists-p (filename)
2325 "Like `file-exists-p' for tramp files."
2326 (with-parsed-tramp-file-name filename nil
2328 (zerop (tramp-send-command-and-check
2329 multi-method method user host
2331 (tramp-get-file-exists-command multi-method method user host)
2332 (tramp-shell-quote-argument localname)))))))
2334 ;; Devices must distinguish physical file systems. The device numbers
2335 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2336 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2337 ;; EFS use device number "-1". In order to be different, we use device number
2338 ;; (-1 x), whereby "x" is unique for a given (multi-method method user host).
2339 (defvar tramp-devices nil
2340 "Keeps virtual device numbers.")
2342 ;; CCC: This should check for an error condition and signal failure
2343 ;; when something goes wrong.
2344 ;; Daniel Pittman <daniel@danann.net>
2345 (defun tramp-handle-file-attributes (filename &optional id-format)
2346 "Like `file-attributes' for tramp files."
2347 (when (file-exists-p filename)
2348 ;; file exists, find out stuff
2349 (unless id-format (setq id-format 'integer))
2350 (with-parsed-tramp-file-name filename nil
2352 (tramp-convert-file-attributes
2353 multi-method method user host
2354 (if (tramp-get-remote-perl multi-method method user host)
2355 (tramp-handle-file-attributes-with-perl multi-method method user host
2356 localname id-format)
2357 (tramp-handle-file-attributes-with-ls multi-method method user host
2358 localname id-format)))))))
2360 (defun tramp-handle-file-attributes-with-ls
2361 (multi-method method user host localname &optional id-format)
2362 "Implement `file-attributes' for tramp files using the ls(1) command."
2364 res-inode res-filemodes res-numlinks
2365 res-uid res-gid res-size res-symlink-target)
2366 (tramp-message-for-buffer multi-method method user host 10
2367 "file attributes with ls: %s"
2368 (tramp-make-tramp-file-name
2369 multi-method method user host localname))
2371 multi-method method user host
2373 (tramp-get-ls-command multi-method method user host)
2374 (if (eq id-format 'integer) "-ildn" "-ild")
2375 (tramp-shell-quote-argument localname)))
2376 (tramp-wait-for-output)
2377 ;; parse `ls -l' output ...
2381 (read (current-buffer))
2382 (invalid-read-syntax
2383 (when (and (equal (cadr err)
2384 "Integer constant overflow in reader")
2386 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2388 (let* ((big (read (substring (caddr err) 0
2389 (match-beginning 1))))
2390 (small (read (match-string 1 (caddr err))))
2391 (twiddle (/ small 65536)))
2392 (cons (+ big twiddle)
2393 (- small (* twiddle 65536))))))))
2394 ;; ... file mode flags
2395 (setq res-filemodes (symbol-name (read (current-buffer))))
2397 (setq res-numlinks (read (current-buffer)))
2399 (setq res-uid (read (current-buffer)))
2400 (setq res-gid (read (current-buffer)))
2401 (when (eq id-format 'integer)
2402 (unless (numberp res-uid) (setq res-uid -1))
2403 (unless (numberp res-gid) (setq res-gid -1)))
2405 (setq res-size (read (current-buffer)))
2406 ;; From the file modes, figure out other stuff.
2407 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2408 (setq dirp (eq ?d (aref res-filemodes 0)))
2409 ;; if symlink, find out file name pointed to
2411 (search-forward "-> ")
2412 (setq res-symlink-target
2413 (buffer-substring (point)
2414 (tramp-line-end-position))))
2415 ;; return data gathered
2417 ;; 0. t for directory, string (name linked to) for symbolic
2419 (or dirp res-symlink-target nil)
2420 ;; 1. Number of links to file.
2426 ;; 4. Last access time, as a list of two integers. First
2427 ;; integer has high-order 16 bits of time, second has low 16
2429 ;; 5. Last modification time, likewise.
2430 ;; 6. Last status change time, likewise.
2431 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2432 ;; 7. Size in bytes (-1, if number is out of range).
2434 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2436 ;; 9. t iff file's gid would change if file were deleted and
2437 ;; recreated. Will be set in `tramp-convert-file-attributes'
2439 ;; 10. inode number.
2441 ;; 11. Device number. Will be replaced by a virtual device number.
2445 (defun tramp-handle-file-attributes-with-perl
2446 (multi-method method user host localname &optional id-format)
2447 "Implement `file-attributes' for tramp files using a Perl script."
2448 (tramp-message-for-buffer multi-method method user host 10
2449 "file attributes with perl: %s"
2450 (tramp-make-tramp-file-name
2451 multi-method method user host localname))
2452 (tramp-maybe-send-perl-script multi-method method user host
2453 tramp-perl-file-attributes
2454 "tramp_file_attributes")
2455 (tramp-send-command multi-method method user host
2456 (format "tramp_file_attributes %s %s"
2457 (tramp-shell-quote-argument localname) id-format))
2458 (tramp-wait-for-output)
2459 (read (current-buffer)))
2461 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
2462 "Like `set-visited-file-modtime' for tramp files."
2463 (unless (buffer-file-name)
2464 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2467 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
2468 (let ((f (buffer-file-name))
2470 (with-parsed-tramp-file-name f nil
2471 (let* ((attr (file-attributes f))
2472 ;; '(-1 65535) means file doesn't exists yet.
2473 (modtime (or (nth 5 attr) '(-1 65535))))
2474 (when (boundp 'last-coding-system-used)
2475 (setq coding-system-used (symbol-value 'last-coding-system-used)))
2476 ;; We use '(0 0) as a don't-know value. See also
2477 ;; `tramp-handle-file-attributes-with-ls'.
2478 (if (not (equal modtime '(0 0)))
2479 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
2482 multi-method method user host
2483 (format "%s -ild %s"
2484 (tramp-get-ls-command multi-method method user host)
2485 (tramp-shell-quote-argument localname)))
2486 (tramp-wait-for-output)
2487 (setq attr (buffer-substring (point)
2488 (progn (end-of-line) (point)))))
2489 (setq tramp-buffer-file-attributes attr))
2490 (when (boundp 'last-coding-system-used)
2491 (set 'last-coding-system-used coding-system-used))
2494 ;; CCC continue here
2496 ;; This function makes the same assumption as
2497 ;; `tramp-handle-set-visited-file-modtime'.
2498 (defun tramp-handle-verify-visited-file-modtime (buf)
2499 "Like `verify-visited-file-modtime' for tramp files.
2500 At the time `verify-visited-file-modtime' calls this function, we
2501 already know that the buffer is visiting a file and that
2502 `visited-file-modtime' does not return 0. Do not call this
2503 function directly, unless those two cases are already taken care
2505 (with-current-buffer buf
2506 ;; There is no file visiting the buffer, or the buffer has no
2507 ;; recorded last modification time.
2508 (if (or (not (buffer-file-name))
2509 (eq (visited-file-modtime) 0))
2511 (let ((f (buffer-file-name)))
2512 (with-parsed-tramp-file-name f nil
2513 (let* ((attr (file-attributes f))
2514 (modtime (nth 5 attr))
2515 (mt (visited-file-modtime)))
2518 ;; file exists, and has a known modtime.
2519 ((and attr (not (equal modtime '(0 0))))
2520 (< (abs (tramp-time-diff
2522 ;; For compatibility, deal with both the old
2523 ;; (HIGH . LOW) and the new (HIGH LOW)
2524 ;; return values of `visited-file-modtime'.
2526 (list (car mt) (cdr mt))
2529 ;; modtime has the don't know value.
2533 multi-method method user host
2534 (format "%s -ild %s"
2535 (tramp-get-ls-command multi-method method user host)
2536 (tramp-shell-quote-argument localname)))
2537 (tramp-wait-for-output)
2538 (setq attr (buffer-substring
2539 (point) (progn (end-of-line) (point)))))
2540 (equal tramp-buffer-file-attributes attr))
2541 ;; If file does not exist, say it is not modified
2542 ;; if and only if that agrees with the buffer's record.
2543 (t (equal mt '(-1 65535))))))))))
2545 (defun tramp-handle-set-file-modes (filename mode)
2546 "Like `set-file-modes' for tramp files."
2547 (with-parsed-tramp-file-name filename nil
2549 (unless (zerop (tramp-send-command-and-check
2550 multi-method method user host
2551 (format "chmod %s %s"
2552 (tramp-decimal-to-octal mode)
2553 (tramp-shell-quote-argument localname))))
2556 ;; FIXME: extract the proper text from chmod's stderr.
2557 "error while changing file's mode"
2560 ;; Simple functions using the `test' command.
2562 (defun tramp-handle-file-executable-p (filename)
2563 "Like `file-executable-p' for tramp files."
2564 (with-parsed-tramp-file-name filename nil
2565 (zerop (tramp-run-test "-x" filename))))
2567 (defun tramp-handle-file-readable-p (filename)
2568 "Like `file-readable-p' for tramp files."
2569 (with-parsed-tramp-file-name filename nil
2570 (zerop (tramp-run-test "-r" filename))))
2572 (defun tramp-handle-file-accessible-directory-p (filename)
2573 "Like `file-accessible-directory-p' for tramp files."
2574 (with-parsed-tramp-file-name filename nil
2575 (and (zerop (tramp-run-test "-d" filename))
2576 (zerop (tramp-run-test "-r" filename))
2577 (zerop (tramp-run-test "-x" filename)))))
2579 ;; When the remote shell is started, it looks for a shell which groks
2580 ;; tilde expansion. Here, we assume that all shells which grok tilde
2581 ;; expansion will also provide a `test' command which groks `-nt' (for
2582 ;; newer than). If this breaks, tell me about it and I'll try to do
2583 ;; something smarter about it.
2584 (defun tramp-handle-file-newer-than-file-p (file1 file2)
2585 "Like `file-newer-than-file-p' for tramp files."
2586 (cond ((not (file-exists-p file1))
2588 ((not (file-exists-p file2))
2590 ;; We are sure both files exist at this point.
2593 ;; We try to get the mtime of both files. If they are not
2594 ;; equal to the "dont-know" value, then we subtract the times
2595 ;; and obtain the result.
2596 (let ((fa1 (file-attributes file1))
2597 (fa2 (file-attributes file2)))
2598 (if (and (not (equal (nth 5 fa1) '(0 0)))
2599 (not (equal (nth 5 fa2) '(0 0))))
2600 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
2601 ;; If one of them is the dont-know value, then we can
2602 ;; still try to run a shell command on the remote host.
2603 ;; However, this only works if both files are Tramp
2604 ;; files and both have the same method, same user, same
2606 (unless (and (tramp-tramp-file-p file1)
2607 (tramp-tramp-file-p file2))
2611 "Cannot check if Tramp file is newer than non-Tramp file"
2613 (with-parsed-tramp-file-name file1 v1
2614 (with-parsed-tramp-file-name file2 v2
2615 (unless (and (equal v1-multi-method v2-multi-method)
2616 (equal v1-method v2-method)
2617 (equal v1-user v2-user)
2618 (equal v1-host v2-host))
2620 (list "Files must have same method, user, host"
2622 (unless (and (tramp-tramp-file-p file1)
2623 (tramp-tramp-file-p file2))
2625 (list "Files must be tramp files on same host"
2627 (if (tramp-get-test-groks-nt
2628 v1-multi-method v1-method v1-user v1-host)
2629 (zerop (tramp-run-test2 "test" file1 file2 "-nt"))
2630 (zerop (tramp-run-test2
2631 "tramp_test_nt" file1 file2)))))))))))
2633 ;; Functions implemented using the basic functions above.
2635 (defun tramp-handle-file-modes (filename)
2636 "Like `file-modes' for tramp files."
2637 (with-parsed-tramp-file-name filename nil
2638 (when (file-exists-p filename)
2639 (tramp-mode-string-to-int
2640 (nth 8 (file-attributes filename))))))
2642 (defun tramp-handle-file-directory-p (filename)
2643 "Like `file-directory-p' for tramp files."
2644 ;; Care must be taken that this function returns `t' for symlinks
2645 ;; pointing to directories. Surely the most obvious implementation
2646 ;; would be `test -d', but that returns false for such symlinks.
2647 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
2648 ;; I now think he's right. So we could be using `test -d', couldn't
2651 ;; Alternatives: `cd %s', `test -d %s'
2652 (with-parsed-tramp-file-name filename nil
2655 (tramp-send-command-and-check
2656 multi-method method user host
2657 (format "test -d %s"
2658 (tramp-shell-quote-argument localname))
2659 t))))) ;run command in subshell
2661 (defun tramp-handle-file-regular-p (filename)
2662 "Like `file-regular-p' for tramp files."
2663 (with-parsed-tramp-file-name filename nil
2664 (and (file-exists-p filename)
2665 (eq ?- (aref (nth 8 (file-attributes filename)) 0)))))
2667 (defun tramp-handle-file-symlink-p (filename)
2668 "Like `file-symlink-p' for tramp files."
2669 (with-parsed-tramp-file-name filename nil
2670 (let ((x (car (file-attributes filename))))
2672 ;; When Tramp is running on VMS, then `file-name-absolute-p'
2673 ;; might do weird things.
2674 (if (file-name-absolute-p x)
2675 (tramp-make-tramp-file-name
2676 multi-method method user host x)
2679 (defun tramp-handle-file-writable-p (filename)
2680 "Like `file-writable-p' for tramp files."
2681 (with-parsed-tramp-file-name filename nil
2682 (if (file-exists-p filename)
2683 ;; Existing files must be writable.
2684 (zerop (tramp-run-test "-w" filename))
2685 ;; If file doesn't exist, check if directory is writable.
2686 (and (zerop (tramp-run-test
2687 "-d" (file-name-directory filename)))
2688 (zerop (tramp-run-test
2689 "-w" (file-name-directory filename)))))))
2691 (defun tramp-handle-file-ownership-preserved-p (filename)
2692 "Like `file-ownership-preserved-p' for tramp files."
2693 (with-parsed-tramp-file-name filename nil
2694 (let ((attributes (file-attributes filename)))
2695 ;; Return t if the file doesn't exist, since it's true that no
2696 ;; information would be lost by an (attempted) delete and create.
2697 (or (null attributes)
2698 (= (nth 2 attributes)
2699 (tramp-get-remote-uid multi-method method user host))))))
2701 ;; Other file name ops.
2703 ;; ;; Matthias K
\e,Av
\e(Bppe <mkoeppe@mail.math.uni-magdeburg.de>
2704 ;; (defun tramp-handle-directory-file-name (directory)
2705 ;; "Like `directory-file-name' for tramp files."
2706 ;; (if (and (eq (aref directory (- (length directory) 1)) ?/)
2707 ;; (not (eq (aref directory (- (length directory) 2)) ?:)))
2708 ;; (substring directory 0 (- (length directory) 1))
2711 ;; ;; Philippe Troin <phil@fifi.org>
2712 ;; (defun tramp-handle-directory-file-name (directory)
2713 ;; "Like `directory-file-name' for tramp files."
2714 ;; (with-parsed-tramp-file-name directory nil
2715 ;; (let ((directory-length-1 (1- (length directory))))
2717 ;; (if (and (eq (aref directory directory-length-1) ?/)
2718 ;; (eq (string-match tramp-file-name-regexp directory) 0)
2719 ;; (/= (match-end 0) directory-length-1))
2720 ;; (substring directory 0 directory-length-1)
2723 (defun tramp-handle-directory-file-name (directory)
2724 "Like `directory-file-name' for tramp files."
2725 ;; If localname component of filename is "/", leave it unchanged.
2726 ;; Otherwise, remove any trailing slash from localname component.
2727 ;; Method, host, etc, are unchanged. Does it make sense to try
2728 ;; to avoid parsing the filename?
2729 (with-parsed-tramp-file-name directory nil
2730 (if (and (not (zerop (length localname)))
2731 (eq (aref localname (1- (length localname))) ?/)
2732 (not (string= localname "/")))
2733 (substring directory 0 -1)
2736 ;; Directory listings.
2738 (defun tramp-handle-directory-files (directory
2739 &optional full match nosort files-only)
2740 "Like `directory-files' for tramp files."
2741 (with-parsed-tramp-file-name directory nil
2744 (tramp-barf-unless-okay
2745 multi-method method user host
2746 (concat "cd " (tramp-shell-quote-argument localname))
2749 "tramp-handle-directory-files: couldn't `cd %s'"
2750 (tramp-shell-quote-argument localname))
2752 multi-method method user host
2753 (concat (tramp-get-ls-command multi-method method user host)
2755 (tramp-wait-for-output)
2756 (goto-char (point-max))
2757 (while (zerop (forward-line -1))
2758 (setq x (buffer-substring (point)
2759 (tramp-line-end-position)))
2760 (when (or (not match) (string-match match x))
2762 (push (concat (file-name-as-directory directory)
2766 (tramp-send-command multi-method method user host "cd")
2767 (tramp-wait-for-output)
2768 ;; Remove non-files or non-directories if necessary. Using
2769 ;; the remote shell for this would probably be way faster.
2770 ;; Maybe something could be adapted from
2771 ;; tramp-handle-file-name-all-completions.
2773 (let ((temp (nreverse result))
2776 (if (equal files-only t)
2779 (setq item (pop temp))
2780 (when (file-regular-p item)
2781 (push item result)))
2784 (setq item (pop temp))
2785 (when (file-directory-p item)
2786 (push item result)))))))
2789 (defun tramp-handle-directory-files-and-attributes
2790 (directory &optional full match nosort id-format)
2791 "Like `directory-files-and-attributes' for tramp files."
2792 (when (tramp-handle-file-exists-p directory)
2794 (setq directory (tramp-handle-expand-file-name directory))
2795 (with-parsed-tramp-file-name directory nil
2796 (tramp-maybe-send-perl-script multi-method method user host
2797 tramp-perl-directory-files-and-attributes
2798 "tramp_directory_files_and_attributes")
2799 (tramp-send-command multi-method method user host
2800 (format "tramp_directory_files_and_attributes %s %s"
2801 (tramp-shell-quote-argument localname)
2802 (or id-format 'integer)))
2803 (tramp-wait-for-output)
2804 (let* ((root (cons nil (let ((object (read (current-buffer))))
2805 (when (stringp object)
2810 (if (and match (not (string-match match (caadr cell))))
2812 (setcdr cell (cddr cell))
2814 (setq cell (cdr cell))
2815 (let ((l (car cell)))
2816 (tramp-convert-file-attributes multi-method method user host
2818 ;; If FULL, make file name absolute
2819 (when full (setcar l (concat directory "/" (car l)))))))
2822 (sort (cdr root) (lambda (x y) (string< (car x) (car y))))))))))
2824 ;; This function should return "foo/" for directories and "bar" for
2825 ;; files. We use `ls -ad' to get a list of files (including
2826 ;; directories), and `find . -type d \! -name . -prune' to get a list
2828 (defun tramp-handle-file-name-all-completions (filename directory)
2829 "Like `file-name-all-completions' for tramp files."
2830 (with-parsed-tramp-file-name directory nil
2831 (unless (save-match-data (string-match "/" filename))
2832 (let* ((nowild tramp-completion-without-shell-p)
2835 (tramp-barf-unless-okay
2836 multi-method method user host
2837 (format "cd %s" (tramp-shell-quote-argument localname))
2839 "tramp-handle-file-name-all-completions: Couldn't `cd %s'"
2840 (tramp-shell-quote-argument localname))
2842 ;; Get a list of directories and files, including reliably
2843 ;; tagging the directories with a trailing '/'. Because I
2844 ;; rock. --daniel@danann.net
2846 multi-method method user host
2847 (format (concat "%s -a %s 2>/dev/null | while read f; do "
2848 "if test -d \"$f\" 2>/dev/null; "
2849 "then echo \"$f/\"; else echo \"$f\"; fi; done")
2850 (tramp-get-ls-command multi-method method user host)
2851 (if (or nowild (zerop (length filename)))
2854 (tramp-shell-quote-argument filename)))))
2856 ;; Now grab the output.
2857 (tramp-wait-for-output)
2858 (goto-char (point-max))
2859 (while (zerop (forward-line -1))
2860 (push (buffer-substring (point)
2861 (tramp-line-end-position))
2864 (tramp-send-command multi-method method user host "cd")
2865 (tramp-wait-for-output)
2869 (all-completions filename (mapcar 'list result))
2873 ;; The following isn't needed for Emacs 20 but for 19.34?
2874 (defun tramp-handle-file-name-completion
2875 (filename directory &optional predicate)
2876 "Like `file-name-completion' for tramp files."
2877 (unless (tramp-tramp-file-p directory)
2879 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2883 (mapcar 'list (file-name-all-completions filename directory))
2885 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
2889 (defun tramp-handle-add-name-to-file
2890 (filename newname &optional ok-if-already-exists)
2891 "Like `add-name-to-file' for tramp files."
2892 (with-parsed-tramp-file-name filename v1
2893 (with-parsed-tramp-file-name newname v2
2894 (let ((ln (when v1 (tramp-get-remote-ln
2895 v1-multi-method v1-method v1-user v1-host))))
2896 (unless (and v1-method v2-method v1-user v2-user v1-host v2-host
2897 (equal v1-multi-method v2-multi-method)
2898 (equal v1-method v2-method)
2899 (equal v1-user v2-user)
2900 (equal v1-host v2-host))
2901 (error "add-name-to-file: %s"
2902 "only implemented for same method, same user, same host"))
2903 (when (and (not ok-if-already-exists)
2904 (file-exists-p newname)
2905 (not (numberp ok-if-already-exists))
2908 "File %s already exists; make it a new name anyway? "
2910 (error "add-name-to-file: file %s already exists" newname))
2911 (tramp-barf-unless-okay
2912 v1-multi-method v1-method v1-user v1-host
2913 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
2914 (tramp-shell-quote-argument v2-localname))
2916 "error with add-name-to-file, see buffer `%s' for details"
2919 (defun tramp-handle-copy-file
2920 (filename newname &optional ok-if-already-exists keep-date)
2921 "Like `copy-file' for tramp files."
2922 ;; Check if both files are local -- invoke normal copy-file.
2923 ;; Otherwise, use tramp from local system.
2924 (setq filename (expand-file-name filename))
2925 (setq newname (expand-file-name newname))
2926 ;; At least one file a tramp file?
2927 (if (or (tramp-tramp-file-p filename)
2928 (tramp-tramp-file-p newname))
2929 (tramp-do-copy-or-rename-file
2930 'copy filename newname ok-if-already-exists keep-date)
2931 (tramp-run-real-handler
2933 (list filename newname ok-if-already-exists keep-date))))
2935 (defun tramp-handle-rename-file
2936 (filename newname &optional ok-if-already-exists)
2937 "Like `rename-file' for tramp files."
2938 ;; Check if both files are local -- invoke normal rename-file.
2939 ;; Otherwise, use tramp from local system.
2940 (setq filename (expand-file-name filename))
2941 (setq newname (expand-file-name newname))
2942 ;; At least one file a tramp file?
2943 (if (or (tramp-tramp-file-p filename)
2944 (tramp-tramp-file-p newname))
2945 (tramp-do-copy-or-rename-file
2946 'rename filename newname ok-if-already-exists)
2947 (tramp-run-real-handler 'rename-file
2948 (list filename newname ok-if-already-exists))))
2950 (defun tramp-do-copy-or-rename-file
2951 (op filename newname &optional ok-if-already-exists keep-date)
2952 "Copy or rename a remote file.
2953 OP must be `copy' or `rename' and indicates the operation to perform.
2954 FILENAME specifies the file to copy or rename, NEWNAME is the name of
2955 the new file (for copy) or the new name of the file (for rename).
2956 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2957 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2960 This function is invoked by `tramp-handle-copy-file' and
2961 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
2962 and `rename'. FILENAME and NEWNAME must be absolute file names."
2963 (unless (memq op '(copy rename))
2964 (error "Unknown operation `%s', must be `copy' or `rename'" op))
2965 (unless ok-if-already-exists
2966 (when (file-exists-p newname)
2967 (signal 'file-already-exists
2968 (list "File already exists" newname))))
2969 (let ((t1 (tramp-tramp-file-p filename))
2970 (t2 (tramp-tramp-file-p newname))
2971 v1-multi-method v1-method v1-user v1-host v1-localname
2972 v2-multi-method v2-method v2-user v2-host v2-localname)
2974 ;; Check which ones of source and target are Tramp files.
2975 ;; We cannot invoke `with-parsed-tramp-file-name';
2976 ;; it fails if the file isn't a Tramp file name.
2978 (with-parsed-tramp-file-name filename l
2979 (setq v1-multi-method l-multi-method
2983 v1-localname l-localname))
2984 (setq v1-localname filename))
2986 (with-parsed-tramp-file-name newname l
2987 (setq v2-multi-method l-multi-method
2991 v2-localname l-localname))
2992 (setq v2-localname newname))
2995 ;; Both are Tramp files.
2998 ;; Shortcut: if method, host, user are the same for both
2999 ;; files, we invoke `cp' or `mv' on the remote host
3001 ((and (equal v1-multi-method v2-multi-method)
3002 (equal v1-method v2-method)
3003 (equal v1-user v2-user)
3004 (equal v1-host v2-host))
3005 (tramp-do-copy-or-rename-file-directly
3006 op v1-multi-method v1-method v1-user v1-host
3007 v1-localname v2-localname keep-date))
3008 ;; If both source and target are Tramp files,
3009 ;; both are using the same copy-program, then we
3010 ;; can invoke rcp directly. Note that
3011 ;; default-directory should point to a local
3012 ;; directory if we want to invoke rcp.
3013 ((and (not v1-multi-method)
3014 (not v2-multi-method)
3015 (equal v1-method v2-method)
3016 (tramp-method-out-of-band-p
3017 v1-multi-method v1-method v1-user v1-host)
3018 (not (string-match "\\([^#]*\\)#\\(.*\\)" v1-host))
3019 (not (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)))
3020 (tramp-do-copy-or-rename-file-out-of-band
3021 op filename newname keep-date))
3022 ;; No shortcut was possible. So we copy the
3023 ;; file first. If the operation was `rename', we go
3024 ;; back and delete the original file (if the copy was
3025 ;; successful). The approach is simple-minded: we
3026 ;; create a new buffer, insert the contents of the
3027 ;; source file into it, then write out the buffer to
3028 ;; the target file. The advantage is that it doesn't
3029 ;; matter which filename handlers are used for the
3030 ;; source and target file.
3032 (tramp-do-copy-or-rename-file-via-buffer
3033 op filename newname keep-date))))
3035 ;; One file is a Tramp file, the other one is local.
3037 ;; If the Tramp file has an out-of-band method, the corresponding
3038 ;; copy-program can be invoked.
3039 (if (and (not v1-multi-method)
3040 (not v2-multi-method)
3041 (or (and t1 (tramp-method-out-of-band-p
3042 v1-multi-method v1-method v1-user v1-host))
3043 (and t2 (tramp-method-out-of-band-p
3044 v2-multi-method v2-method v2-user v2-host))))
3045 (tramp-do-copy-or-rename-file-out-of-band
3046 op filename newname keep-date)
3047 ;; Use the generic method via a Tramp buffer.
3048 (tramp-do-copy-or-rename-file-via-buffer
3049 op filename newname keep-date)))
3052 ;; One of them must be a Tramp file.
3053 (error "Tramp implementation says this cannot happen")))))
3055 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
3056 "Use an Emacs buffer to copy or rename a file.
3057 First arg OP is either `copy' or `rename' and indicates the operation.
3058 FILENAME is the source file, NEWNAME the target file.
3059 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
3060 (let ((trampbuf (get-buffer-create "*tramp output*"))
3061 (modtime (nth 5 (file-attributes filename))))
3062 (when (and keep-date (or (null modtime) (equal modtime '(0 0))))
3064 1 (concat "Warning: cannot preserve file time stamp"
3065 " with inline copying across machines")))
3067 (set-buffer trampbuf) (erase-buffer)
3068 (insert-file-contents-literally filename)
3069 ;; We don't want the target file to be compressed, so we let-bind
3070 ;; `jka-compr-inhibit' to t.
3071 (let ((coding-system-for-write 'binary)
3072 (jka-compr-inhibit t))
3073 (write-region (point-min) (point-max) newname))
3074 ;; KEEP-DATE handling.
3076 (when (and (not (null modtime))
3077 (not (equal modtime '(0 0))))
3078 (tramp-touch newname modtime)))
3080 (set-file-modes newname (file-modes filename)))
3081 ;; If the operation was `rename', delete the original file.
3082 (unless (eq op 'copy)
3083 (delete-file filename))))
3085 (defun tramp-do-copy-or-rename-file-directly
3086 (op multi-method method user host localname1 localname2 keep-date)
3087 "Invokes `cp' or `mv' on the remote system.
3088 OP must be one of `copy' or `rename', indicating `cp' or `mv',
3089 respectively. METHOD, USER, and HOST specify the connection.
3090 LOCALNAME1 and LOCALNAME2 specify the two arguments of `cp' or `mv'.
3091 If KEEP-DATE is non-nil, preserve the time stamp when copying."
3092 ;; CCC: What happens to the timestamp when renaming?
3093 (let ((cmd (cond ((and (eq op 'copy) keep-date) "cp -f -p")
3094 ((eq op 'copy) "cp -f")
3095 ((eq op 'rename) "mv -f")
3097 "Unknown operation `%s', must be `copy' or `rename'"
3101 multi-method method user host
3104 (tramp-shell-quote-argument localname1)
3105 (tramp-shell-quote-argument localname2)))
3106 (tramp-wait-for-output)
3107 (goto-char (point-min))
3110 (and (eq op 'copy) keep-date
3111 ;; Mask cp -f error.
3112 (re-search-forward tramp-operation-not-permitted-regexp nil t))
3113 (zerop (tramp-send-command-and-check
3114 multi-method method user host nil nil)))
3115 (pop-to-buffer (current-buffer))
3117 (format "Copying directly failed, see buffer `%s' for details."
3120 ;; CCC: Maybe `chmod --reference=localname1 localname2' could be used
3122 (unless (or (eq op 'rename) keep-date)
3124 (tramp-make-tramp-file-name multi-method method user host localname2)
3126 (tramp-make-tramp-file-name
3127 multi-method method user host localname1))))))
3129 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
3130 "Invoke rcp program to copy.
3131 One of FILENAME and NEWNAME must be a Tramp name, the other must
3132 be a local filename. The method used must be an out-of-band method."
3133 (let ((t1 (tramp-tramp-file-p filename))
3134 (t2 (tramp-tramp-file-p newname))
3135 v1-multi-method v1-method v1-user v1-host v1-localname
3136 v2-multi-method v2-method v2-user v2-host v2-localname
3137 multi-method method user host copy-program copy-args
3138 source target trampbuf)
3140 ;; Check which ones of source and target are Tramp files.
3141 ;; We cannot invoke `with-parsed-tramp-file-name';
3142 ;; it fails if the file isn't a Tramp file name.
3144 (with-parsed-tramp-file-name filename l
3145 (setq v1-multi-method l-multi-method
3149 v1-localname l-localname
3150 multi-method l-multi-method
3151 method (tramp-find-method
3152 v1-multi-method v1-method v1-user v1-host)
3155 copy-program (tramp-get-method-parameter
3156 v1-multi-method method
3157 v1-user v1-host 'tramp-copy-program)
3158 copy-args (tramp-get-method-parameter
3159 v1-multi-method method
3160 v1-user v1-host 'tramp-copy-args)))
3161 (setq v1-localname filename))
3164 (with-parsed-tramp-file-name newname l
3165 (setq v2-multi-method l-multi-method
3169 v2-localname l-localname
3170 multi-method l-multi-method
3171 method (tramp-find-method
3172 v2-multi-method v2-method v2-user v2-host)
3175 copy-program (tramp-get-method-parameter
3176 v2-multi-method method
3177 v2-user v2-host 'tramp-copy-program)
3178 copy-args (tramp-get-method-parameter
3179 v2-multi-method method
3180 v2-user v2-host 'tramp-copy-args)))
3181 (setq v2-localname newname))
3183 ;; The following should be changed. We need a more general
3184 ;; mechanism to parse extra host args.
3186 (setq source v1-localname)
3187 (when (string-match "\\([^#]*\\)#\\(.*\\)" v1-host)
3188 (setq copy-args (cons "-P" (cons (match-string 2 v1-host) copy-args)))
3189 (setq v1-host (match-string 1 v1-host)))
3191 (tramp-make-copy-program-file-name
3193 (tramp-shell-quote-argument v1-localname))))
3196 (setq target v2-localname)
3197 (when (string-match "\\([^#]*\\)#\\(.*\\)" v2-host)
3198 (setq copy-args (cons "-P" (cons (match-string 2 v2-host) copy-args)))
3199 (setq v2-host (match-string 1 v2-host)))
3201 (tramp-make-copy-program-file-name
3203 (tramp-shell-quote-argument v2-localname))))
3205 ;; Handle ControlMaster/ControlPath
3210 x `((?t . ,(format "/tmp/%s" tramp-temp-name-prefix)))))
3213 ;; Handle keep-date argument
3217 (cons (tramp-get-method-parameter
3218 v1-multi-method method
3219 v1-user v1-host 'tramp-copy-keep-date-arg)
3222 (cons (tramp-get-method-parameter
3223 v2-multi-method method
3224 v2-user v2-host 'tramp-copy-keep-date-arg)
3227 (setq copy-args (append copy-args (list source target))
3228 trampbuf (generate-new-buffer
3229 (tramp-buffer-name multi-method method user host)))
3231 ;; Use an asynchronous process. By this, password can be handled.
3234 ;; Check for program.
3235 (when (and (fboundp 'executable-find)
3236 (not (executable-find copy-program)))
3237 (error "Cannot find copy program: %s" copy-program))
3239 (set-buffer trampbuf)
3240 (setq tramp-current-multi-method multi-method
3241 tramp-current-method method
3242 tramp-current-user user
3243 tramp-current-host host)
3244 (message "Transferring %s to %s..." filename newname)
3246 ;; Use rcp-like program for file transfer.
3248 (let* ((default-directory
3249 (if (and (stringp default-directory)
3250 (file-accessible-directory-p default-directory))
3252 (tramp-temporary-file-directory)))
3253 (p (apply 'start-process (buffer-name trampbuf) trampbuf
3254 copy-program copy-args)))
3255 (tramp-set-process-query-on-exit-flag p nil)
3256 (tramp-process-actions p multi-method method user host
3257 tramp-actions-copy-out-of-band))
3258 (kill-buffer trampbuf))
3259 (message "Transferring %s to %s...done" filename newname)
3263 (set-file-modes newname (file-modes filename))))
3265 ;; If the operation was `rename', delete the original file.
3266 (unless (eq op 'copy)
3267 (delete-file filename))))
3270 (defun tramp-handle-make-directory (dir &optional parents)
3271 "Like `make-directory' for tramp files."
3272 (setq dir (expand-file-name dir))
3273 (with-parsed-tramp-file-name dir nil
3275 (tramp-barf-unless-okay
3276 multi-method method user host
3278 (if parents "mkdir -p" "mkdir")
3279 (tramp-shell-quote-argument localname))
3281 "Couldn't make directory %s" dir))))
3283 ;; CCC error checking?
3284 (defun tramp-handle-delete-directory (directory)
3285 "Like `delete-directory' for tramp files."
3286 (setq directory (expand-file-name directory))
3287 (with-parsed-tramp-file-name directory nil
3290 multi-method method user host
3291 (format "rmdir %s ; echo ok"
3292 (tramp-shell-quote-argument localname)))
3293 (tramp-wait-for-output))))
3295 (defun tramp-handle-delete-file (filename)
3296 "Like `delete-file' for tramp files."
3297 (setq filename (expand-file-name filename))
3298 (with-parsed-tramp-file-name filename nil
3300 (unless (zerop (tramp-send-command-and-check
3301 multi-method method user host
3303 (tramp-shell-quote-argument localname))))
3304 (signal 'file-error "Couldn't delete Tramp file")))))
3308 ;; CCC: This does not seem to be enough. Something dies when
3309 ;; we try and delete two directories under TRAMP :/
3310 (defun tramp-handle-dired-recursive-delete-directory (filename)
3311 "Recursively delete the directory given.
3312 This is like `dired-recursive-delete-directory' for tramp files."
3313 (with-parsed-tramp-file-name filename nil
3314 ;; run a shell command 'rm -r <localname>'
3315 ;; Code shamelessly stolen for the dired implementation and, um, hacked :)
3316 (or (file-exists-p filename)
3319 (list "Removing old file name" "no such directory" filename)))
3320 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
3321 (tramp-send-command multi-method method user host
3322 (format "rm -r %s" (tramp-shell-quote-argument localname)))
3323 ;; Wait for the remote system to return to us...
3324 ;; This might take a while, allow it plenty of time.
3325 (tramp-wait-for-output 120)
3326 ;; Make sure that it worked...
3327 (and (file-exists-p filename)
3328 (error "Failed to recursively delete %s" filename))))
3330 (defun tramp-handle-dired-call-process (program discard &rest arguments)
3331 "Like `dired-call-process' for tramp files."
3332 (with-parsed-tramp-file-name default-directory nil
3334 (tramp-barf-unless-okay
3335 multi-method method user host
3336 (format "cd %s" (tramp-shell-quote-argument localname))
3338 "tramp-handle-dired-call-process: Couldn't `cd %s'"
3339 (tramp-shell-quote-argument localname))
3341 multi-method method user host
3342 (mapconcat #'tramp-shell-quote-argument (cons program arguments) " "))
3343 (tramp-wait-for-output))
3345 ;; We cannot use `insert-buffer' because the tramp buffer
3346 ;; changes its contents before insertion due to calling
3347 ;; `expand-file' and alike.
3349 (with-current-buffer
3350 (tramp-get-buffer multi-method method user host)
3354 (tramp-send-command-and-check multi-method method user host nil)
3355 (tramp-send-command multi-method method user host "cd")
3356 (tramp-wait-for-output)))))
3358 (defun tramp-handle-dired-compress-file (file &rest ok-flag)
3359 "Like `dired-compress-file' for tramp files."
3360 ;; OK-FLAG is valid for XEmacs only, but not implemented.
3361 ;; Code stolen mainly from dired-aux.el.
3362 (with-parsed-tramp-file-name file nil
3365 (if (not (featurep 'xemacs))
3367 (symbol-value 'dired-compress-file-suffixes)
3368 ;; XEmacs has `dired-compression-method-alist', which is
3369 ;; transformed into `dired-compress-file-suffixes' structure.
3372 (list (concat (regexp-quote (nth 1 x)) "\\'")
3374 (mapconcat 'identity (nth 3 x) " ")))
3375 (symbol-value 'dired-compression-method-alist))))
3377 ;; See if any suffix rule matches this file name.
3379 (let (case-fold-search)
3380 (if (string-match (car (car suffixes)) localname)
3381 (setq suffix (car suffixes) suffixes nil))
3382 (setq suffixes (cdr suffixes))))
3384 (cond ((file-symlink-p file)
3386 ((and suffix (nth 2 suffix))
3387 ;; We found an uncompression rule.
3388 (message "Uncompressing %s..." file)
3389 (when (zerop (tramp-send-command-and-check
3390 multi-method method user host
3391 (concat (nth 2 suffix) " " localname)))
3392 (message "Uncompressing %s...done" file)
3393 ;; `dired-remove-file' is not defined in XEmacs
3394 (funcall (symbol-function 'dired-remove-file) file)
3395 (string-match (car suffix) file)
3396 (concat (substring file 0 (match-beginning 0)))))
3398 ;; We don't recognize the file as compressed, so compress it.
3400 (message "Compressing %s..." file)
3401 (when (zerop (tramp-send-command-and-check
3402 multi-method method user host
3403 (concat "gzip -f " localname)))
3404 (message "Compressing %s...done" file)
3405 ;; `dired-remove-file' is not defined in XEmacs
3406 (funcall (symbol-function 'dired-remove-file) file)
3407 (cond ((file-exists-p (concat file ".gz"))
3408 (concat file ".gz"))
3409 ((file-exists-p (concat file ".z"))
3413 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
3414 ;; not sure at all that this is the right way to do it, but let's hope
3415 ;; it works for now, and wait for a guru to point out the Right Way to
3417 ;;(eval-when-compile
3418 ;; (unless (fboundp 'dired-insert-set-properties)
3419 ;; (fset 'dired-insert-set-properties 'ignore)))
3420 ;; Gerd suggests this:
3421 (eval-when-compile (require 'dired))
3422 ;; Note that dired is required at run-time, too, when it is needed.
3423 ;; It is only needed on XEmacs for the function
3424 ;; `dired-insert-set-properties'.
3426 (defun tramp-handle-insert-directory
3427 (filename switches &optional wildcard full-directory-p)
3428 "Like `insert-directory' for tramp files."
3429 (if (and (boundp 'ls-lisp-use-insert-directory-program)
3430 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
3431 (tramp-run-real-handler 'insert-directory
3432 (list filename switches wildcard full-directory-p))
3433 ;; For the moment, we assume that the remote "ls" program does not
3434 ;; grok "--dired". In the future, we should detect this on
3435 ;; connection setup.
3436 (when (string-match "^--dired\\s-+" switches)
3437 (setq switches (replace-match "" nil t switches)))
3438 (setq filename (expand-file-name filename))
3439 (with-parsed-tramp-file-name filename nil
3440 (tramp-message-for-buffer
3441 multi-method method user host 10
3442 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
3443 switches filename (if wildcard "yes" "no")
3444 (if full-directory-p "yes" "no"))
3446 (setq wildcard (file-name-nondirectory localname))
3447 (setq localname (file-name-directory localname)))
3448 (when (listp switches)
3449 (setq switches (mapconcat 'identity switches " ")))
3450 (unless full-directory-p
3451 (setq switches (concat "-d " switches)))
3453 (setq switches (concat switches " " wildcard)))
3455 ;; If `full-directory-p', we just say `ls -l FILENAME'.
3456 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
3457 (if full-directory-p
3459 multi-method method user host
3461 (tramp-get-ls-command multi-method method user host)
3465 (tramp-shell-quote-argument (concat localname ".")))))
3466 (tramp-barf-unless-okay
3467 multi-method method user host
3468 (format "cd %s" (tramp-shell-quote-argument
3469 (file-name-directory localname)))
3472 (tramp-shell-quote-argument (file-name-directory localname)))
3474 multi-method method user host
3476 (tramp-get-ls-command multi-method method user host)
3480 (if (zerop (length (file-name-nondirectory localname)))
3482 (tramp-shell-quote-argument
3483 (file-name-nondirectory localname)))))))
3484 (sit-for 1) ;needed for rsh but not ssh?
3485 (tramp-wait-for-output))
3486 ;; The following let-binding is used by code that's commented
3487 ;; out. Let's leave the let-binding in for a while to see
3488 ;; that the commented-out code is really not needed. Commenting-out
3489 ;; happened on 2003-03-13.
3490 (let ((old-pos (point)))
3491 ;; We cannot use `insert-buffer' because the tramp buffer
3492 ;; changes its contents before insertion due to calling
3493 ;; `expand-file' and alike.
3495 (with-current-buffer
3496 (tramp-get-buffer multi-method method user host)
3498 ;; On XEmacs, we want to call (exchange-point-and-mark t), but
3499 ;; that doesn't exist on Emacs, so we use this workaround instead.
3500 ;; Since zmacs-region-stays doesn't exist in Emacs, this ought to
3501 ;; be safe. Thanks to Daniel Pittman <daniel@danann.net>.
3502 ;; (let ((zmacs-region-stays t))
3503 ;; (exchange-point-and-mark))
3505 (tramp-send-command multi-method method user host "cd")
3506 (tramp-wait-for-output))
3507 ;; For the time being, the XEmacs kludge is commented out.
3508 ;; Please test it on various XEmacs versions to see if it works.
3509 ;; ;; Another XEmacs specialty follows. What's the right way to do
3511 ;; (when (and (featurep 'xemacs)
3512 ;; (eq major-mode 'dired-mode))
3515 ;; (dired-insert-set-properties old-pos (point))))
3518 ;; Continuation of kluge to pacify byte-compiler.
3519 ;;(eval-when-compile
3520 ;; (when (eq (symbol-function 'dired-insert-set-properties) 'ignore)
3521 ;; (fmakunbound 'dired-insert-set-properties)))
3523 ;; CCC is this the right thing to do?
3524 (defun tramp-handle-unhandled-file-name-directory (filename)
3525 "Like `unhandled-file-name-directory' for tramp files."
3526 (with-parsed-tramp-file-name filename nil
3527 (expand-file-name "~/")))
3529 ;; Canonicalization of file names.
3531 (defun tramp-drop-volume-letter (name)
3532 "Cut off unnecessary drive letter from file NAME.
3533 The function `tramp-handle-expand-file-name' calls `expand-file-name'
3534 locally on a remote file name. When the local system is a W32 system
3535 but the remote system is Unix, this introduces a superfluous drive
3536 letter into the file name. This function removes it.
3538 Doesn't do anything if the NAME does not start with a drive letter."
3539 (if (and (> (length name) 1)
3540 (char-equal (aref name 1) ?:)
3541 (let ((c1 (aref name 0)))
3542 (or (and (>= c1 ?A) (<= c1 ?Z))
3543 (and (>= c1 ?a) (<= c1 ?z)))))
3547 (defun tramp-handle-expand-file-name (name &optional dir)
3548 "Like `expand-file-name' for tramp files.
3549 If the localname part of the given filename starts with \"/../\" then
3550 the result will be a local, non-Tramp, filename."
3551 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
3552 (setq dir (or dir default-directory "/"))
3553 ;; Unless NAME is absolute, concat DIR and NAME.
3554 (unless (file-name-absolute-p name)
3555 (setq name (concat (file-name-as-directory dir) name)))
3556 ;; If NAME is not a tramp file, run the real handler
3557 (if (not (tramp-tramp-file-p name))
3558 (tramp-run-real-handler 'expand-file-name
3561 (with-parsed-tramp-file-name name nil
3562 (unless (file-name-absolute-p localname)
3563 (setq localname (concat "~/" localname)))
3565 ;; Tilde expansion if necessary. This needs a shell which
3566 ;; groks tilde expansion! The function `tramp-find-shell' is
3567 ;; supposed to find such a shell on the remote host. Please
3568 ;; tell me about it when this doesn't work on your system.
3569 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
3570 (let ((uname (match-string 1 localname))
3571 (fname (match-string 2 localname)))
3572 ;; We cannot simply apply "~/", because under sudo "~/" is
3573 ;; expanded to the local user home directory but to the
3574 ;; root home directory. On the other hand, using always
3575 ;; the default user name for tilde expansion is not
3576 ;; appropriate either, because ssh and companions might
3577 ;; use a user name from the config file.
3578 (when (and (string-equal uname "~")
3581 (tramp-find-method multi-method method user host)))
3582 (setq uname (concat uname (or user "root"))))
3583 ;; CCC fanatic error checking?
3584 (set-buffer (tramp-get-buffer multi-method method user host))
3587 multi-method method user host
3588 (format "cd %s; pwd" uname)
3590 (tramp-wait-for-output)
3591 (goto-char (point-min))
3592 (setq uname (buffer-substring (point) (tramp-line-end-position)))
3593 (setq localname (concat uname fname))
3595 ;; There might be a double slash, for example when "~/"
3596 ;; expands to "/". Remove this.
3597 (while (string-match "//" localname)
3598 (setq localname (replace-match "/" t t localname)))
3599 ;; No tilde characters in file name, do normal
3600 ;; expand-file-name (this does "/./" and "/../"). We bind
3601 ;; directory-sep-char here for XEmacs on Windows, which would
3602 ;; otherwise use backslash. `default-directory' is bound to
3603 ;; "/", because on Windows there would be problems with UNC
3604 ;; shares or Cygwin mounts.
3605 (tramp-let-maybe directory-sep-char ?/
3606 (let ((default-directory "/"))
3607 (tramp-make-tramp-file-name
3608 multi-method (or method (tramp-find-default-method user host))
3610 (tramp-drop-volume-letter
3611 (tramp-run-real-handler 'expand-file-name
3612 (list localname))))))))))
3614 ;; old version follows. it uses ".." to cross file handler
3616 ;; ;; Look if localname starts with "/../" construct. If this is
3617 ;; ;; the case, then we return a local name instead of a remote name.
3618 ;; (if (string-match "^/\\.\\./" localname)
3619 ;; (expand-file-name (substring localname 3))
3620 ;; ;; No tilde characters in file name, do normal
3621 ;; ;; expand-file-name (this does "/./" and "/../"). We bind
3622 ;; ;; directory-sep-char here for XEmacs on Windows, which
3623 ;; ;; would otherwise use backslash.
3624 ;; (let ((directory-sep-char ?/))
3625 ;; (tramp-make-tramp-file-name
3626 ;; multi-method method user host
3627 ;; (tramp-drop-volume-letter
3628 ;; (tramp-run-real-handler 'expand-file-name
3629 ;; (list localname))))))))))
3633 (defvar tramp-async-proc nil
3634 "Global variable keeping asynchronous process object.
3635 Used in `tramp-handle-shell-command'")
3637 (defvar tramp-display-shell-command-buffer t
3638 "Whether to display output buffer of `shell-command'.
3639 This is necessary for handling DISPLAY of `process-file'.")
3641 (defun tramp-handle-shell-command (command &optional output-buffer error-buffer)
3642 "Like `shell-command' for tramp files.
3643 This will break if COMMAND prints a newline, followed by the value of
3644 `tramp-end-of-output', followed by another newline."
3645 ;; Asynchronous processes are far from being perfect. But it works at least
3646 ;; for `find-grep-dired' and `find-name-dired' in Emacs 22.
3647 (if (tramp-tramp-file-p default-directory)
3648 (with-parsed-tramp-file-name default-directory nil
3649 (let ((curbuf (current-buffer))
3650 (asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
3652 (unless output-buffer
3656 "*Async Shell Command*"
3657 "*Shell Command Output*")))
3658 (set-buffer output-buffer)
3660 (unless (bufferp output-buffer)
3661 (setq output-buffer (current-buffer)))
3662 (set-buffer output-buffer)
3663 ;; Tramp doesn't handle the asynchronous case by an asynchronous
3664 ;; process. Instead of, another asynchronous process is opened
3665 ;; which gets the output of the (synchronous) Tramp process
3666 ;; via process-filter. ERROR-BUFFER is disabled.
3668 (setq command (substring command 0 (match-beginning 0))
3670 tramp-async-proc (start-process (buffer-name output-buffer)
3671 output-buffer "cat")))
3673 (tramp-barf-unless-okay
3674 multi-method method user host
3675 (format "cd %s" (tramp-shell-quote-argument localname))
3677 "tramp-handle-shell-command: Couldn't `cd %s'"
3678 (tramp-shell-quote-argument localname))
3679 ;; Define the process filter
3683 (tramp-get-buffer multi-method method user host))
3684 '(lambda (process string)
3685 ;; Write the output into the Tramp Process
3686 (save-current-buffer
3687 (set-buffer (process-buffer process))
3688 (goto-char (point-max))
3690 ;; Hand-over output to asynchronous process.
3693 (regexp-quote tramp-end-of-output) string)))
3696 (substring string 0 (1- (match-beginning 0)))))
3697 (process-send-string tramp-async-proc string)
3699 (set-process-filter process nil)
3700 (process-send-eof tramp-async-proc))))))
3703 multi-method method user host
3705 (format "( %s ) 2>/tmp/tramp.$$.err; tramp_old_status=$?"
3707 (format "%s; tramp_old_status=$?" command)))
3708 (unless asynchronous
3709 (tramp-wait-for-output)))
3710 (unless asynchronous
3711 ;; We cannot use `insert-buffer' because the tramp buffer
3712 ;; changes its contents before insertion due to calling
3713 ;; `expand-file' and alike.
3715 (with-current-buffer
3716 (tramp-get-buffer multi-method method user host)
3720 (unless (bufferp error-buffer)
3721 (setq error-buffer (get-buffer-create error-buffer)))
3723 multi-method method user host
3724 "cat /tmp/tramp.$$.err")
3725 (tramp-wait-for-output)
3726 (set-buffer error-buffer)
3727 ;; Same comment as above
3729 (with-current-buffer
3730 (tramp-get-buffer multi-method method user host)
3732 (tramp-send-command-and-check
3733 multi-method method user host "rm -f /tmp/tramp.$$.err")))
3735 (tramp-send-command multi-method method user host "cd")
3736 (unless asynchronous
3737 (tramp-wait-for-output))
3739 multi-method method user host
3740 (concat "tramp_set_exit_status $tramp_old_status;"
3741 " echo tramp_exit_status $?"))
3742 (unless asynchronous
3743 (tramp-wait-for-output)
3744 (goto-char (point-max))
3745 (unless (search-backward "tramp_exit_status " nil t)
3746 (error "Couldn't find exit status of `%s'" command))
3747 (skip-chars-forward "^ ")
3748 (setq status (read (current-buffer)))))
3749 (unless (zerop (buffer-size))
3750 (when tramp-display-shell-command-buffer
3751 (display-buffer output-buffer)))
3754 ;; The following is only executed if something strange was
3755 ;; happening. Emit a helpful message and do it anyway.
3756 (message "tramp-handle-shell-command called with non-tramp directory: `%s'"
3758 (tramp-run-real-handler 'shell-command
3759 (list command output-buffer error-buffer))))
3761 (defun tramp-handle-process-file (program &optional infile buffer display &rest args)
3762 "Like `process-file' for Tramp files."
3763 (when infile (error "Implementation does not handle input from file"))
3764 (when (and (numberp buffer) (zerop buffer))
3765 (error "Implementation does not handle immediate return"))
3766 (when (consp buffer) (error "Implementation does not handle error files"))
3767 (let ((tramp-display-shell-command-buffer display))
3769 (mapconcat 'tramp-shell-quote-argument (cons program args) " ")
3774 (defsubst tramp-make-temp-file (filename)
3776 (funcall (if (fboundp 'make-temp-file) 'make-temp-file 'make-temp-name)
3777 (expand-file-name tramp-temp-name-prefix
3778 (tramp-temporary-file-directory)))
3779 (file-name-extension filename t)))
3781 (defun tramp-handle-file-local-copy (filename)
3782 "Like `file-local-copy' for tramp files."
3783 (with-parsed-tramp-file-name filename nil
3784 (let ((tramp-buf (tramp-get-buffer multi-method method user host))
3785 ;; We used to bind the following as late as possible.
3786 ;; loc-enc and loc-dec were bound directly before the if
3787 ;; statement that checks them. But the functions
3788 ;; tramp-get-* might invoke the "are you awake" check in
3789 ;; tramp-maybe-open-connection, which is an unfortunate time
3790 ;; since we rely on the buffer contents at that spot.
3791 (rem-enc (tramp-get-remote-encoding multi-method method user host))
3792 (rem-dec (tramp-get-remote-decoding multi-method method user host))
3793 (loc-enc (tramp-get-local-encoding multi-method method user host))
3794 (loc-dec (tramp-get-local-decoding multi-method method user host))
3796 (unless (file-exists-p filename)
3797 (error "Cannot make local copy of non-existing file `%s'"
3799 (setq tmpfil (tramp-make-temp-file filename))
3801 (cond ((tramp-method-out-of-band-p multi-method method user host)
3802 ;; `copy-file' handles out-of-band methods
3803 (copy-file filename tmpfil t t))
3805 ((and rem-enc rem-dec)
3806 ;; Use inline encoding for file transfer.
3808 ;; Following line for setting tramp-current-method,
3809 ;; tramp-current-user, tramp-current-host.
3810 (set-buffer tramp-buf)
3811 (tramp-message 5 "Encoding remote file %s..." filename)
3812 (tramp-barf-unless-okay
3813 multi-method method user host
3814 (concat rem-enc " < " (tramp-shell-quote-argument localname))
3816 "Encoding remote file failed, see buffer `%s' for details"
3818 ;; Remove trailing status code
3819 (goto-char (point-max))
3820 (delete-region (point) (progn (forward-line -1) (point)))
3822 (tramp-message 5 "Decoding remote file %s..." filename)
3824 ;; Here is where loc-enc and loc-dec used to be let-bound.
3825 (if (and (symbolp loc-dec) (fboundp loc-dec))
3826 ;; If local decoding is a function, we call it. We
3827 ;; must disable multibyte, because
3828 ;; `uudecode-decode-region' doesn't handle it
3830 (let ((tmpbuf (get-buffer-create " *tramp tmp*")))
3833 (set-buffer-multibyte nil)
3834 (insert-buffer-substring tramp-buf)
3835 (tramp-message-for-buffer
3836 multi-method method user host
3837 6 "Decoding remote file %s with function %s..."
3840 ;; Douglas Gray Stephens <DGrayStephens@slb.com>
3841 ;; says that we need to strip tramp_exit_status
3842 ;; line from the output here. Go to point-max,
3843 ;; search backward for tramp_exit_status, delete
3844 ;; between point and point-max if found.
3845 (let ((coding-system-for-write 'binary))
3846 (funcall loc-dec (point-min) (point-max))
3847 (write-region (point-min) (point-max) tmpfil))
3848 (kill-buffer tmpbuf))
3849 ;; If tramp-decoding-function is not defined for this
3850 ;; method, we invoke tramp-decoding-command instead.
3851 (let ((tmpfil2 (tramp-make-temp-file filename)))
3852 (write-region (point-min) (point-max) tmpfil2)
3854 6 "Decoding remote file %s with command %s..."
3856 (tramp-call-local-coding-command
3857 loc-dec tmpfil2 tmpfil)
3858 (delete-file tmpfil2)))
3859 (tramp-message-for-buffer
3860 multi-method method user host
3861 5 "Decoding remote file %s...done" filename)
3862 ;; Set proper permissions.
3863 (set-file-modes tmpfil (file-modes filename))))
3865 (t (error "Wrong method specification for `%s'" method)))
3868 (defun tramp-handle-file-remote-p (filename)
3869 "Like `file-remote-p' for tramp files."
3870 (when (tramp-tramp-file-p filename)
3871 (with-parsed-tramp-file-name filename nil
3872 (make-tramp-file-name
3873 :multi-method multi-method
3879 (defun tramp-handle-insert-file-contents
3880 (filename &optional visit beg end replace)
3881 "Like `insert-file-contents' for tramp files."
3882 (barf-if-buffer-read-only)
3883 (setq filename (expand-file-name filename))
3884 (with-parsed-tramp-file-name filename nil
3885 (if (not (file-exists-p filename))
3888 (setq buffer-file-name filename)
3889 (set-visited-file-modtime)
3890 (set-buffer-modified-p nil))
3892 (format "File `%s' not found on remote host" filename))
3893 (list (expand-file-name filename) 0))
3894 ;; `insert-file-contents-literally' takes care to avoid calling
3895 ;; jka-compr. By let-binding inhibit-file-name-operation, we
3896 ;; propagate that care to the file-local-copy operation.
3898 (let ((inhibit-file-name-operation
3899 (when (eq inhibit-file-name-operation
3900 'insert-file-contents)
3902 (file-local-copy filename)))
3903 coding-system-used result)
3905 (setq buffer-file-name filename)
3906 (set-visited-file-modtime)
3907 (set-buffer-modified-p nil))
3908 (tramp-message-for-buffer
3909 multi-method method user host
3910 9 "Inserting local temp file `%s'..." local-copy)
3911 (setq result (insert-file-contents local-copy nil beg end replace))
3912 ;; Now `last-coding-system-used' has right value. Remember it.
3913 (when (boundp 'last-coding-system-used)
3914 (setq coding-system-used (symbol-value 'last-coding-system-used)))
3915 (tramp-message-for-buffer
3916 multi-method method user host
3917 9 "Inserting local temp file `%s'...done" local-copy)
3918 (delete-file local-copy)
3919 (when (boundp 'last-coding-system-used)
3920 (set 'last-coding-system-used coding-system-used))
3921 (list (expand-file-name filename)
3922 (second result))))))
3925 (defun tramp-handle-find-backup-file-name (filename)
3926 "Like `find-backup-file-name' for tramp files."
3927 (with-parsed-tramp-file-name filename nil
3928 ;; We set both variables. It doesn't matter whether it is
3930 (let ((backup-directory-alist
3932 (when (boundp 'backup-directory-alist)
3933 (if (boundp 'tramp-backup-directory-alist)
3938 (if (and (stringp (cdr x))
3939 (file-name-absolute-p (cdr x))
3940 (not (tramp-file-name-p (cdr x))))
3941 (tramp-make-tramp-file-name
3942 multi-method method user host (cdr x))
3944 (symbol-value 'tramp-backup-directory-alist))
3945 (symbol-value 'backup-directory-alist))))
3947 (bkup-backup-directory-info
3949 (when (boundp 'bkup-backup-directory-info)
3950 (if (boundp 'tramp-bkup-backup-directory-info)
3956 (if (and (stringp (car (cdr x)))
3957 (file-name-absolute-p (car (cdr x)))
3958 (not (tramp-file-name-p (car (cdr x)))))
3959 (tramp-make-tramp-file-name
3960 multi-method method user host (car (cdr x)))
3963 (symbol-value 'tramp-bkup-backup-directory-info))
3964 (symbol-value 'bkup-backup-directory-info)))))
3966 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
3968 (defun tramp-handle-make-auto-save-file-name ()
3969 "Like `make-auto-save-file-name' for tramp files.
3970 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
3971 (let ((tramp-auto-save-directory tramp-auto-save-directory))
3972 ;; File name must be unique. This is ensured with Emacs 22 (see
3973 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
3974 ;; all other cases we must do it ourselves.
3975 (when (boundp 'auto-save-file-name-transforms)
3978 (when (and (string-match (car x) buffer-file-name)
3979 (not (car (cddr x))))
3980 (setq tramp-auto-save-directory
3981 (or tramp-auto-save-directory temporary-file-directory))))
3982 (symbol-value 'auto-save-file-name-transforms)))
3983 ;; Create directory.
3984 (when tramp-auto-save-directory
3985 (unless (file-exists-p tramp-auto-save-directory)
3986 (make-directory tramp-auto-save-directory t)))
3987 ;; jka-compr doesn't like auto-saving, so by appending "~" to the
3988 ;; file name we make sure that jka-compr isn't used for the
3990 (let ((buffer-file-name
3991 (if tramp-auto-save-directory
3993 (tramp-subst-strs-in-string
4001 tramp-auto-save-directory)
4002 (buffer-file-name))))
4003 ;; Run plain `make-auto-save-file-name'. There might be an advice when
4004 ;; it is not a magic file name operation (since Emacs 22).
4005 ;; We must deactivate it temporarily.
4006 (if (not (ad-is-active 'make-auto-save-file-name))
4007 (tramp-run-real-handler
4008 'make-auto-save-file-name nil)
4010 (ad-deactivate 'make-auto-save-file-name)
4012 (tramp-run-real-handler
4013 'make-auto-save-file-name nil)
4014 (ad-activate 'make-auto-save-file-name))))))
4017 ;; CCC grok APPEND, LOCKNAME, CONFIRM
4018 (defun tramp-handle-write-region
4019 (start end filename &optional append visit lockname confirm)
4020 "Like `write-region' for tramp files."
4021 (unless (eq append nil)
4022 (error "Cannot append to file using tramp (`%s')" filename))
4023 (setq filename (expand-file-name filename))
4024 ;; Following part commented out because we don't know what to do about
4025 ;; file locking, and it does not appear to be a problem to ignore it.
4026 ;; Ange-ftp ignores it, too.
4027 ;; (when (and lockname (stringp lockname))
4028 ;; (setq lockname (expand-file-name lockname)))
4029 ;; (unless (or (eq lockname nil)
4030 ;; (string= lockname filename))
4032 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
4033 ;; XEmacs takes a coding system as the seventh argument, not `confirm'
4034 (when (and (not (featurep 'xemacs))
4035 confirm (file-exists-p filename))
4036 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
4038 (error "File not overwritten")))
4039 (with-parsed-tramp-file-name filename nil
4040 (let ((curbuf (current-buffer))
4041 (rem-enc (tramp-get-remote-encoding multi-method method user host))
4042 (rem-dec (tramp-get-remote-decoding multi-method method user host))
4043 (loc-enc (tramp-get-local-encoding multi-method method user host))
4044 (loc-dec (tramp-get-local-decoding multi-method method user host))
4045 (trampbuf (get-buffer-create "*tramp output*"))
4046 (modes (file-modes filename))
4047 ;; We use this to save the value of `last-coding-system-used'
4048 ;; after writing the tmp file. At the end of the function,
4049 ;; we set `last-coding-system-used' to this saved value.
4050 ;; This way, any intermediary coding systems used while
4051 ;; talking to the remote shell or suchlike won't hose this
4052 ;; variable. This approach was snarfed from ange-ftp.el.
4055 ;; Write region into a tmp file. This isn't really needed if we
4056 ;; use an encoding function, but currently we use it always
4057 ;; because this makes the logic simpler.
4058 (setq tmpfil (tramp-make-temp-file filename))
4059 ;; Set current buffer. If connection wasn't open, `file-modes' has
4060 ;; changed it accidently.
4062 ;; We say `no-message' here because we don't want the visited file
4063 ;; modtime data to be clobbered from the temp file. We call
4064 ;; `set-visited-file-modtime' ourselves later on.
4065 (tramp-run-real-handler
4067 (if confirm ; don't pass this arg unless defined for backward compat.
4068 (list start end tmpfil append 'no-message lockname confirm)
4069 (list start end tmpfil append 'no-message lockname)))
4070 ;; Now, `last-coding-system-used' has the right value. Remember it.
4071 (when (boundp 'last-coding-system-used)
4072 (setq coding-system-used (symbol-value 'last-coding-system-used)))
4073 ;; The permissions of the temporary file should be set. If
4074 ;; filename does not exist (eq modes nil) it has been renamed to
4075 ;; the backup file. This case `save-buffer' handles
4077 (when modes (set-file-modes tmpfil modes))
4078 ;; This is a bit lengthy due to the different methods possible for
4079 ;; file transfer. First, we check whether the method uses an rcp
4080 ;; program. If so, we call it. Otherwise, both encoding and
4081 ;; decoding command must be specified. However, if the method
4082 ;; _also_ specifies an encoding function, then that is used for
4083 ;; encoding the contents of the tmp file.
4084 (cond ((tramp-method-out-of-band-p multi-method method user host)
4085 ;; `copy-file' handles out-of-band methods
4086 (copy-file tmpfil filename t t))
4088 ((and rem-enc rem-dec)
4089 ;; Use inline file transfer
4090 (let ((tmpbuf (get-buffer-create " *tramp file transfer*")))
4092 ;; Encode tmpfil into tmpbuf
4093 (tramp-message-for-buffer multi-method method user host
4094 5 "Encoding region...")
4097 ;; Use encoding function or command.
4098 (if (and (symbolp loc-enc) (fboundp loc-enc))
4100 (tramp-message-for-buffer
4101 multi-method method user host
4102 6 "Encoding region using function `%s'..."
4103 (symbol-name loc-enc))
4104 (insert-file-contents-literally tmpfil)
4105 ;; CCC. The following `let' is a workaround for
4106 ;; the base64.el that comes with pgnus-0.84. If
4107 ;; both of the following conditions are
4108 ;; satisfied, it tries to write to a local file
4109 ;; in default-directory, but at this point,
4110 ;; default-directory is remote.
4111 ;; (CALL-PROCESS-REGION can't write to remote
4112 ;; files, it seems.) The file in question is a
4114 (let ((default-directory
4115 (tramp-temporary-file-directory)))
4116 (funcall loc-enc (point-min) (point-max)))
4117 (goto-char (point-max))
4120 (tramp-message-for-buffer
4121 multi-method method user host
4122 6 "Encoding region using command `%s'..." loc-enc)
4123 (unless (equal 0 (tramp-call-local-coding-command
4125 (pop-to-buffer trampbuf)
4126 (error (concat "Cannot write to `%s', local encoding"
4127 " command `%s' failed")
4129 ;; Send tmpbuf into remote decoding command which
4130 ;; writes to remote file. Because this happens on the
4131 ;; remote host, we cannot use the function.
4132 (tramp-message-for-buffer
4133 multi-method method user host
4134 5 "Decoding region into remote file %s..." filename)
4136 multi-method method user host
4137 (format "%s >%s <<'EOF'"
4139 (tramp-shell-quote-argument localname)))
4141 (tramp-message-for-buffer
4142 multi-method method user host
4143 6 "Sending data to remote host...")
4144 (tramp-send-string multi-method method user host
4146 ;; wait for remote decoding to complete
4147 (tramp-message-for-buffer
4148 multi-method method user host
4149 6 "Sending end of data token...")
4151 multi-method method user host "EOF" nil t)
4152 (tramp-message-for-buffer
4153 multi-method method user host 6
4154 "Waiting for remote host to process data...")
4155 (set-buffer (tramp-get-buffer multi-method method user host))
4156 (tramp-wait-for-output)
4157 (tramp-barf-unless-okay
4158 multi-method method user host nil nil 'file-error
4159 (concat "Couldn't write region to `%s',"
4160 " decode using `%s' failed")
4162 (tramp-message 5 "Decoding region into remote file %s...done"
4164 (kill-buffer tmpbuf))))
4167 (concat "Method `%s' should specify both encoding and "
4168 "decoding command or an rcp program")
4170 (delete-file tmpfil)
4171 (unless (equal curbuf (current-buffer))
4172 (error "Buffer has changed from `%s' to `%s'"
4173 curbuf (current-buffer)))
4174 (when (or (eq visit t) (stringp visit))
4175 (set-visited-file-modtime
4176 ;; We must pass modtime explicitely, because filename can be different
4177 ;; from (buffer-file-name), f.e. if `file-precious-flag' is set.
4178 (nth 5 (file-attributes filename))))
4179 ;; Make `last-coding-system-used' have the right value.
4180 (when (boundp 'last-coding-system-used)
4181 (set 'last-coding-system-used coding-system-used))
4182 (when (or (eq visit t)
4185 (message "Wrote %s" filename)))))
4187 ;; Call down to the real handler.
4188 ;; Because EFS does not play nicely with TRAMP (both systems match a
4189 ;; TRAMP file name) it is needed to disable efs as well as tramp for the
4192 ;; Other than that, this is the canon file-handler code that the doco
4193 ;; says should be used here. Which is nice.
4195 ;; Under XEmacs current, EFS also hooks in as
4196 ;; efs-sifn-handler-function to handle any filename with environment
4197 ;; variables. This has two implications:
4198 ;; 1) That EFS may not be completely dead (yet) for TRAMP filenames
4199 ;; 2) That TRAMP might want to do the same thing.
4200 ;; Details as they come in.
4202 ;; Daniel Pittman <daniel@danann.net>
4204 ;; (defun tramp-run-real-handler (operation args)
4205 ;; "Invoke normal file name handler for OPERATION.
4206 ;; This inhibits EFS and Ange-FTP, too, because they conflict with tramp.
4207 ;; First arg specifies the OPERATION, remaining ARGS are passed to the
4209 ;; (let ((inhibit-file-name-handlers
4210 ;; (list 'tramp-file-name-handler
4211 ;; 'efs-file-handler-function
4212 ;; 'ange-ftp-hook-function
4213 ;; (and (eq inhibit-file-name-operation operation)
4214 ;; inhibit-file-name-handlers)))
4215 ;; (inhibit-file-name-operation operation))
4216 ;; (apply operation args)))
4219 (progn (defun tramp-run-real-handler (operation args)
4220 "Invoke normal file name handler for OPERATION.
4221 First arg specifies the OPERATION, second arg is a list of arguments to
4222 pass to the OPERATION."
4223 (let* ((inhibit-file-name-handlers
4224 `(tramp-file-name-handler
4225 tramp-completion-file-name-handler
4226 cygwin-mount-name-hook-function
4227 cygwin-mount-map-drive-hook-function
4229 ,(and (eq inhibit-file-name-operation operation)
4230 inhibit-file-name-handlers)))
4231 (inhibit-file-name-operation operation))
4232 (apply operation args))))
4234 ;; This function is used from `tramp-completion-file-name-handler' functions
4235 ;; only, if `tramp-completion-mode' is true. But this cannot be checked here
4236 ;; because the check is based on a full filename, not available for all
4237 ;; basic I/O operations.
4239 (progn (defun tramp-completion-run-real-handler (operation args)
4240 "Invoke `tramp-file-name-handler' for OPERATION.
4241 First arg specifies the OPERATION, second arg is a list of arguments to
4242 pass to the OPERATION."
4243 (let* ((inhibit-file-name-handlers
4244 `(tramp-completion-file-name-handler
4245 cygwin-mount-name-hook-function
4246 cygwin-mount-map-drive-hook-function
4248 ,(and (eq inhibit-file-name-operation operation)
4249 inhibit-file-name-handlers)))
4250 (inhibit-file-name-operation operation))
4251 (apply operation args))))
4253 ;; We handle here all file primitives. Most of them have the file
4254 ;; name as first parameter; nevertheless we check for them explicitly
4255 ;; in order to be signalled if a new primitive appears. This
4256 ;; scenario is needed because there isn't a way to decide by
4257 ;; syntactical means whether a foreign method must be called. It would
4258 ;; ease the life if `file-name-handler-alist' would support a decision
4259 ;; function as well but regexp only.
4260 (defun tramp-file-name-for-operation (operation &rest args)
4261 "Return file name related to OPERATION file primitive.
4262 ARGS are the arguments OPERATION has been called with."
4264 ; FILE resp DIRECTORY
4266 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
4267 'delete-file 'diff-latest-backup-file 'directory-file-name
4268 'directory-files 'directory-files-and-attributes
4269 'dired-compress-file 'dired-uncache
4270 'file-accessible-directory-p 'file-attributes
4271 'file-directory-p 'file-executable-p 'file-exists-p
4272 'file-local-copy 'file-remote-p 'file-modes
4273 'file-name-as-directory 'file-name-directory
4274 'file-name-nondirectory 'file-name-sans-versions
4275 'file-ownership-preserved-p 'file-readable-p
4276 'file-regular-p 'file-symlink-p 'file-truename
4277 'file-writable-p 'find-backup-file-name 'find-file-noselect
4278 'get-file-buffer 'insert-directory 'insert-file-contents
4279 'load 'make-directory 'make-directory-internal
4280 'set-file-modes 'substitute-in-file-name
4281 'unhandled-file-name-directory 'vc-registered
4283 'abbreviate-file-name 'create-file-buffer
4284 'dired-file-modtime 'dired-make-compressed-filename
4285 'dired-recursive-delete-directory 'dired-set-file-modtime
4286 'dired-shell-unhandle-file-name 'dired-uucode-file
4287 'insert-file-contents-literally 'recover-file
4288 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
4289 (if (file-name-absolute-p (nth 0 args))
4291 (expand-file-name (nth 0 args))))
4292 ; FILE DIRECTORY resp FILE1 FILE2
4294 (list 'add-name-to-file 'copy-file 'expand-file-name
4295 'file-name-all-completions 'file-name-completion
4296 'file-newer-than-file-p 'make-symbolic-link 'rename-file
4298 'dired-make-relative-symlink
4299 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
4302 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
4303 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
4304 (t (buffer-file-name (current-buffer))))))
4306 ((eq operation 'write-region)
4310 (list 'make-auto-save-file-name
4311 'set-visited-file-modtime 'verify-visited-file-modtime
4315 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
4318 (list 'dired-call-process
4324 'dired-print-file 'dired-shell-call-process))
4326 ; unknown file primitive
4327 (t (error "unknown file I/O primitive: %s" operation))))
4329 (defun tramp-find-foreign-file-name-handler (filename)
4330 "Return foreign file name handler if exists."
4331 (when (tramp-tramp-file-p filename)
4334 (handler-alist tramp-foreign-file-name-handler-alist))
4335 (while handler-alist
4336 (setq elt (car handler-alist)
4337 handler-alist (cdr handler-alist))
4338 (when (funcall (car elt) filename)
4339 (setq handler-alist nil)
4340 (setq res (cdr elt))))
4345 (defun tramp-file-name-handler (operation &rest args)
4346 "Invoke Tramp file name handler.
4347 Falls back to normal file name handler if no tramp file name handler exists."
4348 ;; (setq edebug-trace t)
4349 ;; (edebug-trace "%s" (with-output-to-string (backtrace)))
4351 (let* ((filename (apply 'tramp-file-name-for-operation operation args))
4352 (completion (tramp-completion-mode filename))
4353 (foreign (tramp-find-foreign-file-name-handler filename)))
4354 (with-parsed-tramp-file-name filename nil
4356 ;; When we are in completion mode, some operations shouldn' be
4357 ;; handled by backend.
4358 ((and completion (memq operation '(expand-file-name)))
4359 (tramp-run-real-handler operation args))
4360 ((and completion (zerop (length localname))
4361 (memq operation '(file-exists-p file-directory-p)))
4363 ;; Call the backend function.
4364 (foreign (apply foreign operation args))
4365 ;; Nothing to do for us.
4366 (t (tramp-run-real-handler operation args)))))))
4369 ;; In Emacs, there is some concurrency due to timers. If a timer
4370 ;; interrupts Tramp and wishes to use the same connection buffer as
4371 ;; the "main" Emacs, then garbage might occur in the connection
4372 ;; buffer. Therefore, we need to make sure that a timer does not use
4373 ;; the same connection buffer as the "main" Emacs. We implement a
4374 ;; cheap global lock, instead of locking each connection buffer
4375 ;; separately. The global lock is based on two variables,
4376 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
4377 ;; (with setq) to indicate a lock. But Tramp also calls itself during
4378 ;; processing of a single file operation, so we need to allow
4379 ;; recursive calls. That's where the `tramp-locker' variable comes in
4380 ;; -- it is let-bound to t during the execution of the current
4381 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
4382 ;; then we should just proceed because we have been called
4383 ;; recursively. But if `tramp-locker' is nil, then we are a timer
4384 ;; interrupting the "main" Emacs, and then we signal an error.
4386 (defvar tramp-locked nil
4387 "If non-nil, then Tramp is currently busy.
4388 Together with `tramp-locker', this implements a locking mechanism
4389 preventing reentrant calls of Tramp.")
4391 (defvar tramp-locker nil
4392 "If non-nil, then a caller has locked Tramp.
4393 Together with `tramp-locked', this implements a locking mechanism
4394 preventing reentrant calls of Tramp.")
4396 (defun tramp-sh-file-name-handler (operation &rest args)
4397 "Invoke remote-shell Tramp file name handler.
4398 Fall back to normal file name handler if no Tramp handler exists."
4399 (when (and tramp-locked (not tramp-locker))
4400 (signal 'file-error "Forbidden reentrant call of Tramp"))
4401 (let ((tl tramp-locked))
4404 (setq tramp-locked t)
4405 (let ((tramp-locker t))
4407 (let ((fn (assoc operation tramp-file-name-handler-alist)))
4409 (apply (cdr fn) args)
4410 (tramp-run-real-handler operation args))))))
4411 (setq tramp-locked tl))))
4414 (progn (defun tramp-completion-file-name-handler (operation &rest args)
4415 "Invoke tramp file name completion handler.
4416 Falls back to normal file name handler if no tramp file name handler exists."
4417 ;; (setq edebug-trace t)
4418 ;; (edebug-trace "%s" (with-output-to-string (backtrace)))
4419 (let ((fn (assoc operation tramp-completion-file-name-handler-alist)))
4421 (save-match-data (apply (cdr fn) args))
4422 (tramp-completion-run-real-handler operation args)))))
4425 (defsubst tramp-register-file-name-handler ()
4426 "Add tramp file name handler to `file-name-handler-alist'."
4427 (add-to-list 'file-name-handler-alist
4428 (cons tramp-file-name-regexp 'tramp-file-name-handler))
4429 ;; If jka-compr is already loaded, move it to the front of
4430 ;; `file-name-handler-alist'.
4431 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist)))
4433 (setq file-name-handler-alist
4434 (cons jka (delete jka file-name-handler-alist))))))
4437 (defsubst tramp-register-completion-file-name-handler ()
4438 "Add tramp completion file name handler to `file-name-handler-alist'."
4439 ;; `partial-completion-mode' is unknown in XEmacs. So we should
4440 ;; load it unconditionally there. In the GNU Emacs case, method/
4441 ;; user/host name completion shall be bound to `partial-completion-mode'.
4442 (when (or (not (boundp 'partial-completion-mode))
4443 (symbol-value 'partial-completion-mode)
4445 (add-to-list 'file-name-handler-alist
4446 (cons tramp-completion-file-name-regexp
4447 'tramp-completion-file-name-handler))
4448 (put 'tramp-completion-file-name-handler 'safe-magic t))
4449 ;; If jka-compr is already loaded, move it to the front of
4450 ;; `file-name-handler-alist'.
4451 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist)))
4453 (setq file-name-handler-alist
4454 (cons jka (delete jka file-name-handler-alist))))))
4456 ;; `tramp-file-name-handler' must be registered before evaluation of
4457 ;; site-start and init files, because there might exist remote files
4458 ;; already, f.e. files kept via recentf-mode.
4459 ;;;###autoload(tramp-register-file-name-handler)
4460 ;; During autoload, it shall be checked whether
4461 ;; `partial-completion-mode' is active. Therefore registering of
4462 ;; `tramp-completion-file-name-handler' will be delayed.
4463 ;;;###autoload(add-hook
4464 ;;;###autoload 'after-init-hook
4465 ;;;###autoload '(lambda () (tramp-register-completion-file-name-handler)))
4466 (tramp-register-file-name-handler)
4467 (tramp-register-completion-file-name-handler)
4470 (defun tramp-unload-file-name-handlers ()
4471 (setq file-name-handler-alist
4472 (delete (rassoc 'tramp-file-name-handler
4473 file-name-handler-alist)
4474 (delete (rassoc 'tramp-completion-file-name-handler
4475 file-name-handler-alist)
4476 file-name-handler-alist))))
4478 (add-hook 'tramp-unload-hook 'tramp-unload-file-name-handlers)
4481 ;;; Interactions with other packages:
4483 ;; -- complete.el --
4485 ;; This function contributed by Ed Sabol
4486 (defun tramp-handle-expand-many-files (name)
4487 "Like `PC-expand-many-files' for tramp files."
4488 (with-parsed-tramp-file-name name nil
4490 (if (or (string-match "\\*" name)
4491 (string-match "\\?" name)
4492 (string-match "\\[.*\\]" name))
4495 ;; CCC: To do it right, we should quote certain characters
4496 ;; in the file name, but since the echo command is going to
4497 ;; break anyway when there are spaces in the file names, we
4499 ;;-(let ((comint-file-name-quote-list
4500 ;;- (set-difference tramp-file-name-quote-list
4501 ;;- '(?\* ?\? ?[ ?]))))
4502 ;;- (tramp-send-command
4503 ;;- multi-method method user host
4504 ;;- (format "echo %s" (comint-quote-filename localname)))
4505 ;;- (tramp-wait-for-output))
4506 (tramp-send-command multi-method method user host
4507 (format "echo %s" localname))
4508 (tramp-wait-for-output)
4509 (setq bufstr (buffer-substring (point-min)
4510 (tramp-line-end-position)))
4511 (goto-char (point-min))
4512 (if (string-equal localname bufstr)
4515 (while (search-forward " " nil t)
4516 (delete-backward-char 1)
4518 (goto-char (point-max))
4519 (delete-backward-char 1)
4521 (goto-char (point-min))
4523 (function (lambda (x)
4524 (tramp-make-tramp-file-name multi-method method
4526 (read (current-buffer))))))
4527 (list (expand-file-name name))))))
4529 (eval-after-load "complete"
4531 (defadvice PC-expand-many-files
4532 (around tramp-advice-PC-expand-many-files (name) activate)
4533 "Invoke `tramp-handle-expand-many-files' for tramp files."
4534 (if (tramp-tramp-file-p name)
4535 (setq ad-return-value (tramp-handle-expand-many-files name))
4537 (add-hook 'tramp-unload-hook
4538 '(lambda () (ad-unadvise 'PC-expand-many-files)))))
4540 ;;; File name handler functions for completion mode
4542 (defvar tramp-completion-mode nil
4543 "If non-nil, we are in file name completion mode.")
4545 ;; Necessary because `tramp-file-name-regexp-unified' and
4546 ;; `tramp-completion-file-name-regexp-unified' aren't different.
4547 ;; If nil, `tramp-completion-run-real-handler' is called (i.e. forwarding to
4548 ;; `tramp-file-name-handler'). Otherwise, it takes `tramp-run-real-handler'.
4549 ;; Using `last-input-event' is a little bit risky, because completing a file
4550 ;; might require loading other files, like "~/.netrc", and for them it
4551 ;; shouldn't be decided based on that variable. On the other hand, those files
4552 ;; shouldn't have partial tramp file name syntax. Maybe another variable should
4553 ;; be introduced overwriting this check in such cases. Or we change tramp
4554 ;; file name syntax in order to avoid ambiguities, like in XEmacs ...
4555 ;; In case of non unified file names it can be always true (and wouldn't be
4556 ;; necessary, because there are different regexp).
4557 (defun tramp-completion-mode (file)
4558 "Checks whether method / user name / host name completion is active."
4560 (tramp-completion-mode t)
4561 ((string-match "^/.*:.*:$" file) nil)
4563 (concat tramp-prefix-regexp
4564 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp "$")
4566 (member (match-string 1 file) (mapcar 'car tramp-methods)))
4567 ((or (equal last-input-event 'tab)
4569 (and (wholenump last-input-event)
4571 ;; ?\t has event-modifier 'control
4572 (char-equal last-input-event ?\t)
4573 (and (not (event-modifiers last-input-event))
4574 (or (char-equal last-input-event ?\?)
4575 (char-equal last-input-event ?\ )))))
4577 (and (featurep 'xemacs)
4579 ;; ?\t has event-modifier 'control
4581 (funcall (symbol-function 'event-to-character)
4582 last-input-event) ?\t)
4583 (and (not (event-modifiers last-input-event))
4585 (funcall (symbol-function 'event-to-character)
4586 last-input-event) ?\?)
4588 (funcall (symbol-function 'event-to-character)
4589 last-input-event) ?\ ))))))
4592 ;; Method, host name and user name completion.
4593 ;; `tramp-completion-dissect-file-name' returns a list of
4594 ;; tramp-file-name structures. For all of them we return possible completions.
4596 (defun tramp-completion-handle-file-name-all-completions (filename directory)
4597 "Like `file-name-all-completions' for partial tramp files."
4600 ;; We need to reset `tramp-completion-mode'.
4602 (setq tramp-completion-mode t)
4604 ((fullname (concat directory filename))
4605 ;; possible completion structures
4606 (v (tramp-completion-dissect-file-name fullname))
4610 (let* ((car (car v))
4611 (multi-method (tramp-file-name-multi-method car))
4612 (method (tramp-file-name-method car))
4613 (user (tramp-file-name-user car))
4614 (host (tramp-file-name-host car))
4615 (localname (tramp-file-name-localname car))
4616 (m (tramp-find-method multi-method method user host))
4617 (tramp-current-user user) ; see `tramp-parse-passwd'
4620 (unless (or multi-method ;; Not handled (yet).
4621 localname) ;; Nothing to complete
4625 ;; Method dependent user / host combinations
4629 (setq all-user-hosts
4630 (append all-user-hosts
4631 (funcall (nth 0 x) (nth 1 x)))))
4632 (tramp-get-completion-function m))
4634 (setq result (append result
4637 (tramp-get-completion-user-host
4638 method user host (nth 0 x) (nth 1 x)))
4639 (delq nil all-user-hosts)))))
4643 (append result (tramp-get-completion-methods m)))))
4647 ;; unify list, remove nil elements
4649 (let ((car (car result)))
4650 (when car (add-to-list
4651 'result1 (substring car (length directory))))
4652 (setq result (cdr result))))
4654 ;; Complete local parts
4659 ;; "/ssh:" does not need to be expanded as hostname.
4660 (tramp-run-real-handler
4661 'file-name-all-completions (list filename directory))
4662 ;; No method/user/host found to be expanded.
4663 (tramp-completion-run-real-handler
4664 'file-name-all-completions (list filename directory)))
4667 (setq tramp-completion-mode nil)))
4669 ;; Method, host name and user name completion for a file.
4671 (defun tramp-completion-handle-file-name-completion
4672 (filename directory &optional predicate)
4673 "Like `file-name-completion' for tramp files."
4676 (mapcar 'list (file-name-all-completions filename directory))
4678 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
4680 ;; I misuse a little bit the tramp-file-name structure in order to handle
4681 ;; completion possibilities for partial methods / user names / host names.
4682 ;; Return value is a list of tramp-file-name structures according to possible
4683 ;; completions. If "multi-method" or "localname" is non-nil it means there
4684 ;; shouldn't be a completion anymore.
4686 ;; Expected results:
4688 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
4689 ;; [nil nil nil "x" nil] [nil nil "x" nil nil] [nil nil "x" "y" nil]
4690 ;; [nil nil "x" nil nil]
4691 ;; [nil "x" nil nil nil]
4693 ;; "/x:" "/x:y" "/x:y:"
4694 ;; [nil nil nil "x" ""] [nil nil nil "x" "y"] [nil "x" nil "y" ""]
4696 ;; [nil "x" nil "" nil] [nil "x" nil "y" nil]
4697 ;; [nil "x" "" nil nil] [nil "x" "y" nil nil]
4699 ;; "/x:y@" "/x:y@z" "/x:y@z:"
4700 ;; [nil nil nil "x" "y@"] [nil nil nil "x" "y@z"] [nil "x" "y" "z" ""]
4701 ;; "/[x/y@" "/[x/y@z"
4702 ;; [nil "x" nil "y" nil] [nil "x" "y" "z" nil]
4703 (defun tramp-completion-dissect-file-name (name)
4704 "Returns a list of `tramp-file-name' structures.
4705 They are collected by `tramp-completion-dissect-file-name1'."
4709 ;; "/method" "/[method"
4710 (tramp-completion-file-name-structure1
4711 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
4714 (tramp-completion-file-name-structure2
4715 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
4718 (tramp-completion-file-name-structure3
4719 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
4721 ;; "/user@host" "/[user@host"
4722 (tramp-completion-file-name-structure4
4723 (list (concat tramp-prefix-regexp
4724 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4725 "\\(" tramp-host-regexp x-nil "\\)$")
4727 ;; "/method:user" "/[method/user"
4728 (tramp-completion-file-name-structure5
4729 (list (concat tramp-prefix-regexp
4730 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4731 "\\(" tramp-user-regexp x-nil "\\)$")
4733 ;; "/method:host" "/[method/host"
4734 (tramp-completion-file-name-structure6
4735 (list (concat tramp-prefix-regexp
4736 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4737 "\\(" tramp-host-regexp x-nil "\\)$")
4739 ;; "/method:user@host" "/[method/user@host"
4740 (tramp-completion-file-name-structure7
4741 (list (concat tramp-prefix-regexp
4742 "\\(" tramp-method-regexp "\\)" tramp-postfix-single-method-regexp
4743 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
4744 "\\(" tramp-host-regexp x-nil "\\)$")
4747 (mapcar (lambda (regexp)
4748 (add-to-list 'result
4749 (tramp-completion-dissect-file-name1 regexp name)))
4751 tramp-completion-file-name-structure1
4752 tramp-completion-file-name-structure2
4753 tramp-completion-file-name-structure3
4754 tramp-completion-file-name-structure4
4755 tramp-completion-file-name-structure5
4756 tramp-completion-file-name-structure6
4757 tramp-completion-file-name-structure7
4758 tramp-file-name-structure))
4762 (defun tramp-completion-dissect-file-name1 (structure name)
4763 "Returns a `tramp-file-name' structure matching STRUCTURE.
4764 The structure consists of multi-method, remote method, remote user,
4765 remote host and localname (filename on remote host)."
4769 (when (string-match (nth 0 structure) name)
4770 (setq method (and (nth 1 structure)
4771 (match-string (nth 1 structure) name)))
4772 (if (and method (member method tramp-multi-methods))
4773 ;; Not handled (yet).
4774 (make-tramp-file-name
4775 :multi-method method
4780 (let ((user (and (nth 2 structure)
4781 (match-string (nth 2 structure) name)))
4782 (host (and (nth 3 structure)
4783 (match-string (nth 3 structure) name)))
4784 (localname (and (nth 4 structure)
4785 (match-string (nth 4 structure) name))))
4786 (make-tramp-file-name
4791 :localname localname)))))))
4793 ;; This function returns all possible method completions, adding the
4794 ;; trailing method delimeter.
4795 (defun tramp-get-completion-methods (partial-method)
4796 "Returns all method completions for PARTIAL-METHOD."
4800 (string-match (concat "^" (regexp-quote partial-method)) method)
4801 (tramp-make-tramp-file-name nil method nil nil nil)))
4802 (delete "multi" (mapcar 'car tramp-methods))))
4804 ;; Compares partial user and host names with possible completions.
4805 (defun tramp-get-completion-user-host (method partial-user partial-host user host)
4806 "Returns the most expanded string for user and host name completion.
4807 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
4810 ((and partial-user partial-host)
4812 (string-match (concat "^" (regexp-quote partial-host)) host)
4813 (string-equal partial-user (or user partial-user)))
4814 (setq user partial-user)
4821 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
4827 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
4833 (unless (zerop (+ (length user) (length host)))
4834 (tramp-make-tramp-file-name nil method user host nil)))
4836 (defun tramp-parse-rhosts (filename)
4837 "Return a list of (user host) tuples allowed to access.
4838 Either user or host may be nil."
4841 (when (file-readable-p filename)
4843 (insert-file-contents filename)
4844 (goto-char (point-min))
4846 (push (tramp-parse-rhosts-group) res))))
4849 ;; Taken from gnus/netrc.el
4851 (defalias 'tramp-point-at-eol
4852 (if (fboundp 'point-at-eol)
4854 'line-end-position)))
4856 (defun tramp-parse-rhosts-group ()
4857 "Return a (user host) tuple allowed to access.
4858 Either user or host may be nil."
4863 "^\\(" tramp-host-regexp "\\)"
4864 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
4866 (narrow-to-region (point) (tramp-point-at-eol))
4867 (when (re-search-forward regexp nil t)
4868 (setq result (append (list (match-string 3) (match-string 1)))))
4873 (defun tramp-parse-shosts (filename)
4874 "Return a list of (user host) tuples allowed to access.
4875 User is always nil."
4878 (when (file-readable-p filename)
4880 (insert-file-contents filename)
4881 (goto-char (point-min))
4883 (push (tramp-parse-shosts-group) res))))
4886 (defun tramp-parse-shosts-group ()
4887 "Return a (user host) tuple allowed to access.
4888 User is always nil."
4891 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4893 (narrow-to-region (point) (tramp-point-at-eol))
4894 (when (re-search-forward regexp nil t)
4895 (setq result (list nil (match-string 1))))
4898 (> (skip-chars-forward ",") 0)
4902 (defun tramp-parse-sconfig (filename)
4903 "Return a list of (user host) tuples allowed to access.
4904 User is always nil."
4907 (when (file-readable-p filename)
4909 (insert-file-contents filename)
4910 (goto-char (point-min))
4912 (push (tramp-parse-sconfig-group) res))))
4915 (defun tramp-parse-sconfig-group ()
4916 "Return a (user host) tuple allowed to access.
4917 User is always nil."
4920 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
4922 (narrow-to-region (point) (tramp-point-at-eol))
4923 (when (re-search-forward regexp nil t)
4924 (setq result (list nil (match-string 1))))
4927 (> (skip-chars-forward ",") 0)
4931 (defun tramp-parse-shostkeys (dirname)
4932 "Return a list of (user host) tuples allowed to access.
4933 User is always nil."
4935 (let ((regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
4936 (files (when (file-directory-p dirname) (directory-files dirname)))
4940 (when (string-match regexp (car files))
4941 (push (list nil (match-string 1 (car files))) result))
4942 (setq files (cdr files)))
4945 (defun tramp-parse-sknownhosts (dirname)
4946 "Return a list of (user host) tuples allowed to access.
4947 User is always nil."
4949 (let ((regexp (concat "^\\(" tramp-host-regexp
4950 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
4951 (files (when (file-directory-p dirname) (directory-files dirname)))
4955 (when (string-match regexp (car files))
4956 (push (list nil (match-string 1 (car files))) result))
4957 (setq files (cdr files)))
4960 (defun tramp-parse-hosts (filename)
4961 "Return a list of (user host) tuples allowed to access.
4962 User is always nil."
4965 (when (file-readable-p filename)
4967 (insert-file-contents filename)
4968 (goto-char (point-min))
4970 (push (tramp-parse-hosts-group) res))))
4973 (defun tramp-parse-hosts-group ()
4974 "Return a (user host) tuple allowed to access.
4975 User is always nil."
4978 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
4980 (narrow-to-region (point) (tramp-point-at-eol))
4981 (when (re-search-forward regexp nil t)
4982 (unless (char-equal (or (char-after) ?\n) ?:) ; no IPv6
4983 (setq result (list nil (match-string 1)))))
4986 (> (skip-chars-forward " \t") 0)
4990 ;; For su-alike methods it would be desirable to return "root@localhost"
4991 ;; as default. Unfortunately, we have no information whether any user name
4992 ;; has been typed already. So we (mis-)use tramp-current-user as indication,
4993 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
4994 (defun tramp-parse-passwd (filename)
4995 "Return a list of (user host) tuples allowed to access.
4996 Host is always \"localhost\"."
4999 (if (zerop (length tramp-current-user))
5001 (when (file-readable-p filename)
5003 (insert-file-contents filename)
5004 (goto-char (point-min))
5006 (push (tramp-parse-passwd-group) res))))
5009 (defun tramp-parse-passwd-group ()
5010 "Return a (user host) tuple allowed to access.
5011 Host is always \"localhost\"."
5014 (regexp (concat "^\\(" 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 1) "localhost")))
5023 (defun tramp-parse-netrc (filename)
5024 "Return a list of (user host) tuples allowed to access.
5028 (when (file-readable-p filename)
5030 (insert-file-contents filename)
5031 (goto-char (point-min))
5033 (push (tramp-parse-netrc-group) res))))
5036 (defun tramp-parse-netrc-group ()
5037 "Return a (user host) tuple allowed to access.
5043 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
5044 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
5046 (narrow-to-region (point) (tramp-point-at-eol))
5047 (when (re-search-forward regexp nil t)
5048 (setq result (list (match-string 3) (match-string 1))))
5053 ;;; Internal Functions:
5055 (defun tramp-maybe-send-perl-script (multi-method method user host script name)
5056 "Define in remote shell function NAME implemented as perl SCRIPT.
5057 Only send the definition if it has not already been done.
5058 Function may have 0-3 parameters."
5059 (let ((remote-perl (tramp-get-remote-perl multi-method method user host)))
5060 (unless remote-perl (error "No remote perl"))
5061 (let ((perl-scripts (tramp-get-connection-property "perl-scripts" nil
5062 multi-method method user host)))
5063 (unless (memq name perl-scripts)
5064 (with-current-buffer (tramp-get-buffer multi-method method user host)
5065 (tramp-message 5 (concat "Sending the Perl script `" name "'..."))
5066 (tramp-send-string multi-method method user host
5072 "' \"$1\" \"$2\" \"$3\" 2>/dev/null\n}"))
5073 (tramp-wait-for-output)
5074 (tramp-set-connection-property "perl-scripts" (cons name perl-scripts)
5075 multi-method method user host)
5076 (tramp-message 5 (concat "Sending the Perl script `" name "'...done.")))))))
5078 (defun tramp-set-auto-save ()
5079 (when (and (buffer-file-name)
5080 (tramp-tramp-file-p (buffer-file-name))
5081 ;; ange-ftp has its own auto-save mechanism
5082 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
5083 'tramp-sh-file-name-handler)
5085 (auto-save-mode 1)))
5086 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
5087 (add-hook 'tramp-unload-hook
5089 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
5091 (defun tramp-run-test (switch filename)
5092 "Run `test' on the remote system, given a SWITCH and a FILENAME.
5093 Returns the exit code of the `test' program."
5094 (let ((v (tramp-dissect-file-name filename)))
5096 (tramp-send-command-and-check
5097 (tramp-file-name-multi-method v) (tramp-file-name-method v)
5098 (tramp-file-name-user v) (tramp-file-name-host v)
5099 (format "test %s %s" switch
5100 (tramp-shell-quote-argument (tramp-file-name-localname v)))))))
5102 (defun tramp-run-test2 (program file1 file2 &optional switch)
5103 "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2.
5104 The optional SWITCH is inserted between the two files.
5105 Returns the exit code of the `test' PROGRAM. Barfs if the methods,
5106 hosts, or files, disagree."
5107 (let* ((v1 (tramp-dissect-file-name file1))
5108 (v2 (tramp-dissect-file-name file2))
5109 (mmethod1 (tramp-file-name-multi-method v1))
5110 (mmethod2 (tramp-file-name-multi-method v2))
5111 (method1 (tramp-file-name-method v1))
5112 (method2 (tramp-file-name-method v2))
5113 (user1 (tramp-file-name-user v1))
5114 (user2 (tramp-file-name-user v2))
5115 (host1 (tramp-file-name-host v1))
5116 (host2 (tramp-file-name-host v2))
5117 (localname1 (tramp-file-name-localname v1))
5118 (localname2 (tramp-file-name-localname v2)))
5119 (unless (and method1 method2 host1 host2
5120 (equal mmethod1 mmethod2)
5121 (equal method1 method2)
5123 (equal host1 host2))
5124 (error "tramp-run-test2: %s"
5125 "only implemented for same method, same user, same host"))
5127 (tramp-send-command-and-check
5128 mmethod1 method1 user1 host1
5129 (format "%s %s %s %s"
5131 (tramp-shell-quote-argument localname1)
5133 (tramp-shell-quote-argument localname2))))))
5135 (defun tramp-touch (file time)
5136 "Set the last-modified timestamp of the given file.
5137 TIME is an Emacs internal time value as returned by `current-time'."
5139 ;; With GNU Emacs, `format-time-string' has an optional
5140 ;; parameter UNIVERSAL. This is preferred.
5141 (and (functionp 'subr-arity)
5142 (= 3 (cdr (funcall (symbol-function 'subr-arity)
5143 (symbol-function 'format-time-string))))))
5146 (format-time-string "%Y%m%d%H%M.%S" time t)
5147 (format-time-string "%Y%m%d%H%M.%S" time))))
5148 (if (tramp-tramp-file-p file)
5149 (with-parsed-tramp-file-name file nil
5150 (let ((buf (tramp-get-buffer multi-method method user host)))
5151 (unless (zerop (tramp-send-command-and-check
5152 multi-method method user host
5153 (format "%s touch -t %s %s"
5154 (if utc "TZ=UTC; export TZ;" "")
5156 (tramp-shell-quote-argument localname))
5159 (error "tramp-touch: touch failed, see buffer `%s' for details"
5161 ;; It's a local file
5163 (unless (zerop (call-process
5164 "touch" nil (current-buffer) nil "-t" touch-time file))
5165 (pop-to-buffer (current-buffer))
5166 (error "tramp-touch: touch failed"))))))
5168 (defun tramp-buffer-name (multi-method method user host)
5169 "A name for the connection buffer for USER at HOST using METHOD."
5171 (tramp-buffer-name-multi-method "tramp" multi-method method user host)
5172 (let ((method (tramp-find-method multi-method method user host)))
5174 (format "*tramp/%s %s@%s*" method user host)
5175 (format "*tramp/%s %s*" method host)))))
5177 (defun tramp-buffer-name-multi-method (prefix multi-method method user host)
5178 "A name for the multi method connection buffer.
5179 MULTI-METHOD gives the multi method, METHOD the array of methods,
5180 USER the array of user names, HOST the array of host names."
5181 (unless (and (= (length method) (length user))
5182 (= (length method) (length host)))
5183 (error "Syntax error in multi method (implementation error)"))
5184 (let ((len (length method))
5189 (cons (if (aref user i)
5190 (format "%s#%s@%s:" (aref method i)
5191 (aref user i) (aref host i))
5192 (format "%s@%s:" (aref method i) (aref host i)))
5195 (format "*%s/%s %s*"
5197 (apply 'concat (reverse string-list)))))
5199 (defun tramp-get-buffer (multi-method method user host)
5200 "Get the connection buffer to be used for USER at HOST using METHOD."
5201 (with-current-buffer
5202 (get-buffer-create (tramp-buffer-name multi-method method user host))
5203 (setq buffer-undo-list t)
5206 (defun tramp-debug-buffer-name (multi-method method user host)
5207 "A name for the debug buffer for USER at HOST using METHOD."
5209 (tramp-buffer-name-multi-method "debug tramp"
5210 multi-method method user host)
5211 (let ((method (tramp-find-method multi-method method user host)))
5213 (format "*debug tramp/%s %s@%s*" method user host)
5214 (format "*debug tramp/%s %s*" method host)))))
5216 (defun tramp-get-debug-buffer (multi-method method user host)
5217 "Get the debug buffer for USER at HOST using METHOD."
5218 (with-current-buffer
5220 (tramp-debug-buffer-name multi-method method user host))
5221 (setq buffer-undo-list t)
5224 (defun tramp-find-executable (multi-method method user host
5225 progname dirlist ignore-tilde)
5226 "Searches for PROGNAME in all directories mentioned in DIRLIST.
5227 First args METHOD, USER and HOST specify the connection, PROGNAME
5228 is the program to search for, and DIRLIST gives the list of directories
5229 to search. If IGNORE-TILDE is non-nil, directory names starting
5230 with `~' will be ignored.
5232 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
5234 This function expects to be in the right *tramp* buffer."
5237 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
5238 ;; `remove' is in CL, and we want to avoid CL dependencies.
5241 (setq d (car dirlist))
5242 (setq dirlist (cdr dirlist))
5243 (unless (char-equal ?~ (aref d 0))
5244 (setq newdl (cons d newdl))))
5245 (setq dirlist (nreverse newdl))))
5247 multi-method method user host
5248 (format (concat "while read d; "
5249 "do if test -x $d/%s -a -f $d/%s; "
5250 "then echo tramp_executable $d/%s; "
5251 "break; fi; done <<'EOF'")
5252 progname progname progname))
5254 (tramp-send-command multi-method method user host d))
5256 (tramp-send-command multi-method method user host "EOF")
5257 (tramp-wait-for-output)
5258 (goto-char (point-max))
5259 (when (search-backward "tramp_executable " nil t)
5260 (skip-chars-forward "^ ")
5261 (skip-chars-forward " ")
5262 (buffer-substring (point) (tramp-line-end-position)))))
5264 (defun tramp-set-remote-path (multi-method method user host var dirlist)
5265 "Sets the remote environment VAR to existing directories from DIRLIST.
5266 I.e., for each directory in DIRLIST, it is tested whether it exists and if
5267 so, it is added to the environment variable VAR."
5268 (let ((existing-dirs
5273 (tramp-make-tramp-file-name multi-method method user host x))
5275 (tramp-make-tramp-file-name multi-method method user host x)))
5279 multi-method method user host
5281 (mapconcat 'identity (delq nil existing-dirs) ":")
5283 (tramp-wait-for-output)))
5285 ;; -- communication with external shell --
5287 (defun tramp-find-file-exists-command (multi-method method user host)
5288 "Find a command on the remote host for checking if a file exists.
5289 Here, we are looking for a command which has zero exit status if the
5290 file exists and nonzero exit status otherwise."
5291 (make-local-variable 'tramp-file-exists-command)
5292 (tramp-message 9 "Finding command to check if file exists")
5294 (tramp-make-tramp-file-name
5295 multi-method method user host
5296 "/")) ;assume this file always exists
5298 (tramp-make-tramp-file-name
5299 multi-method method user host
5300 "/ this file does not exist "))) ;assume this never exists
5301 ;; The algorithm is as follows: we try a list of several commands.
5302 ;; For each command, we first run `$cmd /' -- this should return
5303 ;; true, as the root directory always exists. And then we run
5304 ;; `$cmd /this\ file\ does\ not\ exist', hoping that the file indeed
5305 ;; does not exist. This should return false. We use the first
5306 ;; command we find that seems to work.
5307 ;; The list of commands to try is as follows:
5308 ;; `ls -d' This works on most systems, but NetBSD 1.4
5309 ;; has a bug: `ls' always returns zero exit
5310 ;; status, even for files which don't exist.
5311 ;; `test -e' Some Bourne shells have a `test' builtin
5312 ;; which does not know the `-e' option.
5313 ;; `/bin/test -e' For those, the `test' binary on disk normally
5314 ;; provides the option. Alas, the binary
5315 ;; is sometimes `/bin/test' and sometimes it's
5317 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
5319 (and (setq tramp-file-exists-command "test -e %s")
5320 (file-exists-p existing)
5321 (not (file-exists-p nonexisting)))
5322 (and (setq tramp-file-exists-command "/bin/test -e %s")
5323 (file-exists-p existing)
5324 (not (file-exists-p nonexisting)))
5325 (and (setq tramp-file-exists-command "/usr/bin/test -e %s")
5326 (file-exists-p existing)
5327 (not (file-exists-p nonexisting)))
5328 (and (setq tramp-file-exists-command "ls -d %s")
5329 (file-exists-p existing)
5330 (not (file-exists-p nonexisting))))
5331 (error "Couldn't find command to check if file exists"))))
5334 ;; CCC test ksh or bash found for tilde expansion?
5335 (defun tramp-find-shell (multi-method method user host)
5336 "Find a shell on the remote host which groks tilde expansion."
5338 (tramp-send-command multi-method method user host "echo ~root")
5339 (tramp-wait-for-output)
5341 ((string-match "^~root$" (buffer-string))
5343 (or (tramp-find-executable multi-method method user host
5344 "bash" tramp-remote-path t)
5345 (tramp-find-executable multi-method method user host
5346 "ksh" tramp-remote-path t)))
5348 (error "Couldn't find a shell which groks tilde expansion"))
5349 ;; Find arguments for this shell.
5350 (let ((alist tramp-sh-extra-args)
5352 (while (and alist (null extra-args))
5353 (setq item (pop alist))
5354 (when (string-match (car item) shell)
5355 (setq extra-args (cdr item))))
5356 (when extra-args (setq shell (concat shell " " extra-args))))
5358 5 "Starting remote shell `%s' for tilde expansion..." shell)
5360 multi-method method user host
5361 (concat "PS1='$ ' exec " shell)) ;
5362 (tramp-barf-if-no-shell-prompt
5363 (get-buffer-process (current-buffer))
5364 60 "Couldn't find remote `%s' prompt" shell)
5366 9 "Setting remote shell prompt...")
5367 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we
5368 ;; must use "\n" here, not tramp-rsh-end-of-line. Kai left the
5369 ;; last tramp-rsh-end-of-line, Douglas wanted to replace that,
5371 (process-send-string nil (format "PS1='%s%s%s'; PS2=''; PS3=''%s"
5372 tramp-rsh-end-of-line
5374 tramp-rsh-end-of-line
5375 tramp-rsh-end-of-line))
5376 (tramp-wait-for-output)
5378 9 "Setting remote shell prompt...done")
5380 (t (tramp-message 5 "Remote `%s' groks tilde expansion, good"
5381 (tramp-get-method-parameter
5382 multi-method method user host 'tramp-remote-sh))))))
5384 (defun tramp-check-ls-command (multi-method method user host cmd)
5385 "Checks whether the given `ls' executable groks `-n'.
5386 METHOD, USER and HOST specify the connection, CMD (the absolute file name of)
5387 the `ls' executable. Returns t if CMD supports the `-n' option, nil
5389 (tramp-message 9 "Checking remote `%s' command for `-n' option" cmd)
5390 (when (file-executable-p
5391 (tramp-make-tramp-file-name multi-method method user host cmd))
5393 (tramp-message 7 "Testing remote command `%s' for -n..." cmd)
5395 (tramp-send-command-and-check
5396 multi-method method user host
5397 (format "%s -lnd / >/dev/null"
5399 (tramp-message 7 "Testing remote command `%s' for -n...%s"
5401 (if (zerop result) "okay" "failed"))
5404 (defun tramp-check-ls-commands (multi-method method user host cmd dirlist)
5405 "Checks whether the given `ls' executable in one of the dirs groks `-n'.
5406 Returns nil if none was found, else the command is returned."
5409 (tramp-let-maybe directory-sep-char ?/ ;for XEmacs
5410 ;; It would be better to use the CL function `find', but
5411 ;; we don't want run-time dependencies on CL.
5412 (while (and dl (not result))
5413 (let ((x (concat (file-name-as-directory (car dl)) cmd)))
5414 (when (tramp-check-ls-command multi-method method user host x)
5419 (defun tramp-find-ls-command (multi-method method user host)
5420 "Finds an `ls' command which groks the `-n' option, returning nil if failed.
5421 \(This option prints numeric user and group ids in a long listing.)"
5422 (tramp-message 9 "Finding a suitable `ls' command")
5424 (tramp-check-ls-commands multi-method method user host "ls" tramp-remote-path)
5425 (tramp-check-ls-commands multi-method method user host "gnuls" tramp-remote-path)
5426 (tramp-check-ls-commands multi-method method user host "gls" tramp-remote-path)))
5428 ;; ------------------------------------------------------------
5429 ;; -- Functions for establishing connection --
5430 ;; ------------------------------------------------------------
5432 ;; The following functions are actions to be taken when seeing certain
5433 ;; prompts from the remote host. See the variable
5434 ;; `tramp-actions-before-shell' for usage of these functions.
5436 (defun tramp-action-login (p multi-method method user host)
5437 "Send the login name."
5438 (tramp-message 9 "Sending login name `%s'"
5439 (or user (user-login-name)))
5441 (process-send-string nil (concat (or user (user-login-name))
5442 tramp-rsh-end-of-line)))
5444 (defun tramp-action-password (p multi-method method user host)
5445 "Query the user for a password."
5447 (format "Password for %s "
5448 (tramp-make-tramp-file-name
5449 nil method user host ""))))
5450 (tramp-message 9 "Sending password")
5451 (tramp-enter-password p pw-prompt user host)))
5453 (defun tramp-action-succeed (p multi-method method user host)
5454 "Signal success in finding shell prompt."
5455 (tramp-message 9 "Found remote shell prompt.")
5457 (throw 'tramp-action 'ok))
5459 (defun tramp-action-permission-denied (p multi-method method user host)
5460 "Signal permission denied."
5461 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5462 (tramp-message 9 "Permission denied by remote host.")
5464 (throw 'tramp-action 'permission-denied))
5466 (defun tramp-action-copy-failed (p multi-method method user host)
5467 "Signal copy failed."
5469 (error "%s" (match-string 1)))
5471 (defun tramp-action-yesno (p multi-method method user host)
5472 "Ask the user for confirmation using `yes-or-no-p'.
5473 Send \"yes\" to remote process on confirmation, abort otherwise.
5474 See also `tramp-action-yn'."
5475 (save-window-excursion
5476 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5477 (unless (yes-or-no-p (match-string 0))
5480 (throw 'tramp-action 'permission-denied))
5481 (process-send-string p (concat "yes" tramp-rsh-end-of-line))
5484 (defun tramp-action-yn (p multi-method method user host)
5485 "Ask the user for confirmation using `y-or-n-p'.
5486 Send \"y\" to remote process on confirmation, abort otherwise.
5487 See also `tramp-action-yesno'."
5488 (save-window-excursion
5489 (pop-to-buffer (tramp-get-buffer multi-method method user host))
5490 (unless (y-or-n-p (match-string 0))
5492 (throw 'tramp-action 'permission-denied))
5494 (process-send-string p (concat "y" tramp-rsh-end-of-line))))
5496 (defun tramp-action-terminal (p multi-method method user host)
5497 "Tell the remote host which terminal type to use.
5498 The terminal type can be configured with `tramp-terminal-type'."
5499 (tramp-message 9 "Setting `%s' as terminal type."
5500 tramp-terminal-type)
5502 (process-send-string nil (concat tramp-terminal-type
5503 tramp-rsh-end-of-line)))
5505 (defun tramp-action-process-alive (p multi-method method user host)
5506 "Check whether a process has finished."
5507 (unless (memq (process-status p) '(run open))
5508 (throw 'tramp-action 'process-died)))
5510 (defun tramp-action-out-of-band (p multi-method method user host)
5511 "Check whether an out-of-band copy has finished."
5512 (cond ((and (memq (process-status p) '(stop exit))
5513 (zerop (process-exit-status p)))
5514 (tramp-message 9 "Process has finished.")
5515 (throw 'tramp-action 'ok))
5516 ((or (and (memq (process-status p) '(stop exit))
5517 (not (zerop (process-exit-status p))))
5518 (memq (process-status p) '(signal)))
5519 ;; `scp' could have copied correctly, but set modes could have failed.
5520 ;; This can be ignored.
5521 (goto-char (point-min))
5522 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
5524 (tramp-message 10 "'set mode' error ignored.")
5525 (tramp-message 9 "Process has finished.")
5526 (throw 'tramp-action 'ok))
5527 (tramp-message 9 "Process has died.")
5528 (throw 'tramp-action 'process-died)))
5531 ;; The following functions are specifically for multi connections.
5533 (defun tramp-multi-action-login (p method user host)
5534 "Send the login name."
5535 (tramp-message 9 "Sending login name `%s'" user)
5537 (process-send-string p (concat user tramp-rsh-end-of-line)))
5539 (defun tramp-multi-action-password (p method user host)
5540 "Query the user for a password."
5542 (format "Password for %s "
5543 (tramp-make-tramp-file-name
5544 nil method user host ""))))
5545 (tramp-message 9 "Sending password")
5546 (tramp-enter-password p pw-prompt user host)))
5548 (defun tramp-multi-action-succeed (p method user host)
5549 "Signal success in finding shell prompt."
5550 (tramp-message 9 "Found shell prompt on `%s'" host)
5552 (throw 'tramp-action 'ok))
5554 (defun tramp-multi-action-permission-denied (p method user host)
5555 "Signal permission denied."
5556 (tramp-message 9 "Permission denied by remote host `%s'" host)
5559 (throw 'tramp-action 'permission-denied))
5561 (defun tramp-multi-action-process-alive (p method user host)
5562 "Check whether a process has finished."
5563 (unless (memq (process-status p) '(run open))
5564 (throw 'tramp-action 'process-died)))
5566 ;; Functions for processing the actions.
5568 (defun tramp-process-one-action (p multi-method method user host actions)
5569 "Wait for output from the shell and perform one action."
5570 (let (found item pattern action todo)
5572 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5574 (tramp-accept-process-output p 1)
5575 (goto-char (point-min))
5578 (goto-char (point-min))
5579 (setq item (pop todo))
5580 (setq pattern (symbol-value (nth 0 item)))
5581 (setq action (nth 1 item))
5582 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5584 (when (re-search-forward (concat pattern "\\'") nil t)
5585 (setq found (funcall action p multi-method method user host)))))
5588 (defun tramp-process-actions
5589 (p multi-method method user host actions &optional timeout)
5590 "Perform actions until success or TIMEOUT."
5591 (tramp-message 10 "%s" (mapconcat 'identity (process-command p) " "))
5594 (tramp-message 9 "Waiting for prompts from remote shell")
5596 (catch 'tramp-action
5598 (with-timeout (timeout)
5599 (tramp-process-one-action
5600 p multi-method method user host actions))
5601 (tramp-process-one-action
5602 p multi-method method user host actions))
5604 (unless (eq exit 'ok)
5605 (tramp-clear-passwd user host)
5606 (error "Login failed"))))
5608 ;; For multi-actions.
5610 (defun tramp-process-one-multi-action (p method user host actions)
5611 "Wait for output from the shell and perform one action."
5612 (let (found item pattern action todo)
5614 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5615 (with-timeout (60 (throw 'tramp-action 'timeout))
5617 (tramp-accept-process-output p 1)
5619 (goto-char (point-min))
5621 (goto-char (point-min))
5622 (setq item (pop todo))
5623 (setq pattern (symbol-value (nth 0 item)))
5624 (setq action (nth 1 item))
5625 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5627 (when (re-search-forward (concat pattern "\\'") nil t)
5628 (setq found (funcall action p method user host)))))
5631 (defun tramp-process-multi-actions (p method user host actions)
5632 "Perform actions until success."
5635 (tramp-message 9 "Waiting for prompts from remote shell")
5637 (catch 'tramp-action
5638 (tramp-process-one-multi-action p method user host actions)
5640 (unless (eq exit 'ok)
5641 (tramp-clear-passwd user host)
5642 (error "Login failed"))))
5644 ;; Functions to execute when we have seen the remote shell prompt but
5645 ;; before we exec the Bourne-ish shell. Note that these commands
5646 ;; might be sent to any shell, not just a Bourne-ish shell. This
5647 ;; means that the commands need to work in all shells. (It is also
5648 ;; okay for some commands to just fail with an error message, but
5649 ;; please make sure that they at least don't crash the odd shell people
5650 ;; might be running...)
5651 (defun tramp-process-initial-commands (p
5652 multi-method method user host
5654 "Send list of commands to remote host, in order."
5657 (setq cmd (pop commands))
5659 (tramp-message 10 "Sending command to remote shell: %s"
5661 (tramp-send-command multi-method method user host cmd nil t)
5662 (tramp-barf-if-no-shell-prompt
5663 p 60 "Remote shell command failed: %s" cmd))
5666 ;; The actual functions for opening connections.
5668 (defun tramp-open-connection-telnet (multi-method method user host)
5669 "Open a connection using a telnet METHOD.
5670 This starts the command `telnet HOST ARGS'[*], then waits for a remote
5671 login prompt, then sends the user name USER, then waits for a remote
5672 password prompt. It queries the user for the password, then sends the
5673 password to the remote host.
5675 If USER is nil, uses value returned by `(user-login-name)' instead.
5677 Recognition of the remote shell prompt is based on the variables
5678 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5681 Please note that it is NOT possible to use this connection method
5682 together with an out-of-band transfer method! You must use an inline
5685 Maybe the different regular expressions need to be tuned.
5687 * Actually, the telnet program as well as the args to be used can be
5688 specified in the method parameters, see the variable `tramp-methods'."
5690 (when (tramp-method-out-of-band-p multi-method method user host)
5691 (error "Cannot use out-of-band method `%s' with telnet connection method"
5694 (error "Cannot multi-connect using telnet connection method"))
5695 (tramp-pre-connection multi-method method user host tramp-chunksize)
5696 (tramp-message 7 "Opening connection for %s@%s using %s..."
5697 (or user (user-login-name)) host method)
5698 (let ((process-environment (copy-sequence process-environment)))
5699 (setenv "TERM" tramp-terminal-type)
5701 (let* ((default-directory (tramp-temporary-file-directory))
5702 ;; If we omit the conditional here, then we would use
5703 ;; `undecided-dos' in some cases. With the conditional,
5704 ;; we use nil in these cases. Which one is right?
5705 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5706 (> emacs-major-version 20))
5707 tramp-dos-coding-system))
5708 (p (apply 'start-process
5709 (tramp-buffer-name multi-method method user host)
5710 (tramp-get-buffer multi-method method user host)
5711 (tramp-get-method-parameter
5713 (tramp-find-method multi-method method user host)
5714 user host 'tramp-login-program)
5716 (tramp-get-method-parameter
5718 (tramp-find-method multi-method method user host)
5719 user host 'tramp-login-args)))
5722 (tramp-set-process-query-on-exit-flag p nil)
5723 (set-buffer (tramp-get-buffer multi-method method user host))
5725 (tramp-process-actions p multi-method method user host
5726 tramp-actions-before-shell 60)
5727 (tramp-open-connection-setup-interactive-shell
5728 p multi-method method user host)
5729 (tramp-post-connection multi-method method user host)))))
5732 (defun tramp-open-connection-rsh (multi-method method user host)
5733 "Open a connection using an rsh METHOD.
5734 This starts the command `rsh HOST -l USER'[*], then waits for a remote
5735 password or shell prompt. If a password prompt is seen, the user is
5736 queried for a password, this function sends the password to the remote
5737 host and waits for a shell prompt.
5739 If USER is nil, start the command `rsh HOST'[*] instead
5741 Recognition of the remote shell prompt is based on the variables
5742 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5745 Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to
5746 be a port number for ssh, and \"-p yy\" will be added to the list of
5747 arguments, and xx will be used as the host name to connect to.
5749 * Actually, the rsh program to be used can be specified in the
5750 method parameters, see the variable `tramp-methods'."
5753 (error "Cannot multi-connect using rsh connection method"))
5754 (tramp-pre-connection multi-method method user host tramp-chunksize)
5755 (if (and user (not (string= user "")))
5756 (tramp-message 7 "Opening connection for %s@%s using %s..."
5758 (tramp-message 7 "Opening connection at %s using %s..." host method))
5759 (let ((process-environment (copy-sequence process-environment))
5760 (bufnam (tramp-buffer-name multi-method method user host))
5761 (buf (tramp-get-buffer multi-method method user host))
5762 (login-program (tramp-get-method-parameter
5764 (tramp-find-method multi-method method user host)
5765 user host 'tramp-login-program))
5769 x `((?t . ,(format "/tmp/%s" tramp-temp-name-prefix)))))
5770 (tramp-get-method-parameter
5772 (tramp-find-method multi-method method user host)
5773 user host 'tramp-login-args)))
5775 ;; The following should be changed. We need a more general
5776 ;; mechanism to parse extra host args.
5777 (when (string-match "\\([^#]*\\)#\\(.*\\)" host)
5778 (setq login-args (cons "-p" (cons (match-string 2 host) login-args)))
5779 (setq real-host (match-string 1 host)))
5780 (setenv "TERM" tramp-terminal-type)
5782 (let* ((default-directory (tramp-temporary-file-directory))
5783 ;; If we omit the conditional, we would use
5784 ;; `undecided-dos' in some cases. With the conditional,
5785 ;; we use nil in these cases. Which one is right?
5786 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5787 (> emacs-major-version 20))
5788 tramp-dos-coding-system))
5789 (p (if (and user (not (string= user "")))
5790 (apply #'start-process bufnam buf login-program
5791 real-host "-l" user login-args)
5792 (apply #'start-process bufnam buf login-program
5793 real-host login-args)))
5795 (tramp-set-process-query-on-exit-flag p nil)
5798 (tramp-process-actions p multi-method method user host
5799 tramp-actions-before-shell 60)
5800 (tramp-message 7 "Initializing remote shell")
5801 (tramp-open-connection-setup-interactive-shell
5802 p multi-method method user host)
5803 (tramp-post-connection multi-method method user host)))))
5805 (defun tramp-open-connection-su (multi-method method user host)
5806 "Open a connection using the `su' program with METHOD.
5807 This starts `su - USER', then waits for a password prompt. The HOST
5808 name must be equal to the local host name or to `localhost'.
5810 If USER is nil, uses value returned by user-login-name instead.
5812 Recognition of the remote shell prompt is based on the variables
5813 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5814 set up correctly. Note that the other user may have a different shell
5815 prompt than you do, so it is not at all unlikely that the variable
5816 `shell-prompt-pattern' is set up wrongly!"
5818 (when (tramp-method-out-of-band-p multi-method method user host)
5819 (error "Cannot use out-of-band method `%s' with `su' connection method"
5821 (unless (or (string-match (concat "^" (regexp-quote host))
5823 (string= "localhost" host)
5826 "Cannot connect to different host `%s' with `su' connection method"
5828 (tramp-pre-connection multi-method method user host tramp-chunksize)
5829 (tramp-message 7 "Opening connection for `%s' using `%s'..."
5830 (or user "<root>") method)
5831 (let ((process-environment (copy-sequence process-environment)))
5832 (setenv "TERM" tramp-terminal-type)
5834 (let* ((default-directory (tramp-temporary-file-directory))
5835 ;; If we omit the conditional, we use `undecided-dos' in
5836 ;; some cases. With the conditional, we use nil in these
5837 ;; cases. What's the difference? Which one is right?
5838 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5839 (> emacs-major-version 20))
5840 tramp-dos-coding-system))
5841 (p (apply 'start-process
5842 (tramp-buffer-name multi-method method user host)
5843 (tramp-get-buffer multi-method method user host)
5844 (tramp-get-method-parameter
5846 (tramp-find-method multi-method method user host)
5847 user host 'tramp-login-program)
5850 (format-spec x `((?u . ,(or user "root")))))
5851 (tramp-get-method-parameter
5853 (tramp-find-method multi-method method user host)
5854 user host 'tramp-login-args))))
5857 (tramp-set-process-query-on-exit-flag p nil)
5858 (set-buffer (tramp-get-buffer multi-method method user host))
5859 (tramp-process-actions p multi-method method user host
5860 tramp-actions-before-shell 60)
5861 (tramp-open-connection-setup-interactive-shell
5862 p multi-method method user host)
5863 (tramp-post-connection multi-method method
5866 ;; HHH: Not Changed. Multi method. It is not clear to me how this can
5867 ;; handle not giving a user name in the "file name".
5869 ;; This is more difficult than for the single-hop method. In the
5870 ;; multi-hop-method, the desired behaviour should be that the
5871 ;; user must specify names for the telnet hops of which the user
5872 ;; name is different than the "original" name (or different from
5873 ;; the previous hop.
5874 (defun tramp-open-connection-multi (multi-method method user host)
5875 "Open a multi-hop connection using METHOD.
5876 This uses a slightly changed file name syntax. The idea is to say
5877 [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file
5878 This will use telnet to log in as u1 to h1, then use rsh from there to
5879 log in as u2 to h2."
5881 (unless multi-method
5882 (error "Multi-hop open connection function called on non-multi method"))
5883 (when (tramp-method-out-of-band-p multi-method method user host)
5884 (error "No out of band multi-hop connections"))
5885 (unless (and (arrayp method) (not (stringp method)))
5886 (error "METHOD must be an array of strings for multi methods"))
5887 (unless (and (arrayp user) (not (stringp user)))
5888 (error "USER must be an array of strings for multi methods"))
5889 (unless (and (arrayp host) (not (stringp host)))
5890 (error "HOST must be an array of strings for multi methods"))
5891 (unless (and (= (length method) (length user))
5892 (= (length method) (length host)))
5893 (error "Arrays METHOD, USER, HOST must have equal length"))
5894 (tramp-pre-connection multi-method method user host tramp-chunksize)
5895 (tramp-message 7 "Opening `%s' connection..." multi-method)
5896 (let ((process-environment (copy-sequence process-environment)))
5897 (setenv "TERM" tramp-terminal-type)
5899 (let* ((default-directory (tramp-temporary-file-directory))
5900 ;; If we omit the conditional, we use `undecided-dos' in
5901 ;; some cases. With the conditional, we use nil in these
5902 ;; cases. What's the difference? Which one is right?
5903 (coding-system-for-read (unless (and (not (featurep 'xemacs))
5904 (> emacs-major-version 20))
5905 tramp-dos-coding-system))
5906 (p (start-process (tramp-buffer-name multi-method method user host)
5907 (tramp-get-buffer multi-method method user host)
5908 tramp-multi-sh-program))
5909 (num-hops (length method))
5911 (tramp-set-process-query-on-exit-flag p nil)
5912 (tramp-message 9 "Waiting 60s for local shell to come up...")
5913 (unless (tramp-wait-for-regexp
5914 p 60 (format "\\(%s\\)\\'\\|\\(%s\\)\\'"
5915 shell-prompt-pattern tramp-shell-prompt-pattern))
5916 (pop-to-buffer (buffer-name))
5918 (error "Couldn't find local shell prompt"))
5919 ;; Now do all the connections as specified.
5920 (while (< i num-hops)
5921 (let* ((m (aref method i))
5924 (entry (assoc m tramp-multi-connection-function-alist))
5925 (multi-func (nth 1 entry))
5926 (command (nth 2 entry)))
5927 ;; The multi-funcs don't need to do save-match-data, as that
5929 (funcall multi-func p m u h command)
5932 (tramp-open-connection-setup-interactive-shell
5933 p multi-method method user host)
5934 (tramp-post-connection multi-method method user host)))))
5936 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5937 ;; of no user name provided. Hack to make it work as it did before:
5938 ;; changed `user' to `(or user (user-login-name))' in the places where
5939 ;; the value is actually used.
5940 (defun tramp-multi-connect-telnet (p method user host command)
5941 "Issue `telnet' command.
5942 Uses shell COMMAND to issue a `telnet' command to log in as USER to
5943 HOST. You can use percent escapes in COMMAND: `%h' is replaced with
5944 the host name, and `%n' is replaced with an end of line character, as
5945 set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5948 If USER is nil, uses the return value of (user-login-name) instead."
5949 (let ((cmd (format-spec command
5950 `((?h . ,host) (?n . ,tramp-rsh-end-of-line))))
5951 (cmd1 (format-spec command `((?h . ,host) (?n . ""))))
5954 (tramp-message 9 "Sending telnet command `%s'" cmd1)
5955 (process-send-string p cmd)
5956 (tramp-process-multi-actions p method user host
5957 tramp-multi-actions)))
5959 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5960 ;; of no user name provided. Hack to make it work as it did before:
5961 ;; changed `user' to `(or user (user-login-name))' in the places where
5962 ;; the value is actually used.
5963 (defun tramp-multi-connect-rlogin (p method user host command)
5964 "Issue `rlogin' command.
5965 Uses shell COMMAND to issue an `rlogin' command to log in as USER to
5966 HOST. You can use percent escapes in COMMAND. `%u' will be replaced
5967 with the user name, `%h' will be replaced with the host name, and `%n'
5968 will be replaced with the value of `tramp-rsh-end-of-line'. You can use
5969 `%%' if you want to use a literal percent character.
5971 If USER is nil, uses the return value of (user-login-name) instead."
5972 (let ((cmd (format-spec command `((?h . ,host)
5973 (?u . ,(or user (user-login-name)))
5974 (?n . ,tramp-rsh-end-of-line))))
5975 (cmd1 (format-spec command `((?h . ,host)
5976 (?u . ,(or user (user-login-name)))
5980 (tramp-message 9 "Sending rlogin command `%s'" cmd1)
5981 (process-send-string p cmd)
5982 (tramp-process-multi-actions p method user host
5983 tramp-multi-actions)))
5985 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5986 ;; of no user name provided. Hack to make it work as it did before:
5987 ;; changed `user' to `(or user (user-login-name))' in the places where
5988 ;; the value is actually used.
5989 (defun tramp-multi-connect-su (p method user host command)
5990 "Issue `su' command.
5991 Uses shell COMMAND to issue a `su' command to log in as USER on
5992 HOST. The HOST name is ignored, this just changes the user id on the
5993 host currently logged in to.
5995 If USER is nil, uses the return value of (user-login-name) instead.
5997 You can use percent escapes in the COMMAND. `%u' is replaced with the
5998 user name, and `%n' is replaced with the value of
5999 `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
6001 (let ((cmd (format-spec command `((?u . ,(or user (user-login-name)))
6002 (?n . ,tramp-rsh-end-of-line))))
6003 (cmd1 (format-spec command `((?u . ,(or user (user-login-name)))
6007 (tramp-message 9 "Sending su command `%s'" cmd1)
6008 (process-send-string p cmd)
6009 (tramp-process-multi-actions p method user host
6010 tramp-multi-actions)))
6012 ;; Utility functions.
6014 (defun tramp-accept-process-output
6015 (&optional process timeout timeout-msecs)
6016 "Like `accept-process-output' for Tramp processes.
6017 This is needed in order to hide `last-coding-system-used', which is set
6018 for process communication also."
6019 (let (last-coding-system-used)
6020 (accept-process-output process timeout timeout-msecs)))
6022 (defun tramp-wait-for-regexp (proc timeout regexp)
6023 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
6024 Expects the output of PROC to be sent to the current buffer. Returns
6025 the string that matched, or nil. Waits indefinitely if TIMEOUT is
6028 (start-time (current-time)))
6030 ;; Work around a bug in XEmacs 21, where the timeout
6031 ;; expires faster than it should. This degenerates
6032 ;; to polling for buggy XEmacsen, but oh, well.
6033 (while (and (not found)
6034 (< (tramp-time-diff (current-time) start-time)
6036 (with-timeout (timeout)
6038 (tramp-accept-process-output proc 1)
6039 (unless (memq (process-status proc) '(run open))
6040 (error "Process has died"))
6041 (goto-char (point-min))
6042 (setq found (re-search-forward regexp nil t))))))
6045 (tramp-accept-process-output proc 1)
6046 (unless (memq (process-status proc) '(run open))
6047 (error "Process has died"))
6048 (goto-char (point-min))
6049 (setq found (re-search-forward regexp nil t)))))
6050 (when tramp-debug-buffer
6052 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6053 tramp-current-user tramp-current-host)
6054 (point-min) (point-max))
6058 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6059 tramp-current-user tramp-current-host))
6060 (goto-char (point-max))
6061 (insert "[[Regexp `" regexp "' not found"
6062 (if timeout (format " in %d secs" timeout) "")
6066 (defun tramp-wait-for-shell-prompt (proc timeout)
6067 "Wait for the shell prompt to appear from process PROC within TIMEOUT seconds.
6068 See `tramp-wait-for-regexp' for more details.
6069 Shell prompt pattern is determined by variables `shell-prompt-pattern'
6070 and `tramp-shell-prompt-pattern'."
6071 (tramp-wait-for-regexp
6073 (format "\\(%s\\|%s\\)\\'"
6074 shell-prompt-pattern tramp-shell-prompt-pattern)))
6076 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
6077 "Wait for shell prompt and barf if none appears.
6078 Looks at process PROC to see if a shell prompt appears in TIMEOUT
6079 seconds. If not, it produces an error message with the given ERROR-ARGS."
6080 (unless (tramp-wait-for-shell-prompt proc timeout)
6081 (pop-to-buffer (buffer-name))
6082 (apply 'error error-args)))
6084 (defun tramp-enter-password (p prompt user host)
6085 "Prompt for a password and send it to the remote end.
6086 Uses PROMPT as a prompt and sends the password to process P."
6087 (let ((pw (tramp-read-passwd user host prompt)))
6089 (process-send-string
6091 (or (tramp-get-method-parameter
6092 tramp-current-multi-method
6093 tramp-current-method
6096 'tramp-password-end-of-line)
6097 tramp-default-password-end-of-line)))))
6099 ;; HHH: Not Changed. This might handle the case where USER is not
6100 ;; given in the "File name" very poorly. Then, the local
6101 ;; variable tramp-current-user will be set to nil.
6102 (defun tramp-pre-connection (multi-method method user host chunksize)
6103 "Do some setup before actually logging in.
6104 METHOD, USER and HOST specify the connection."
6105 (set-buffer (tramp-get-buffer multi-method method user host))
6106 (set (make-local-variable 'tramp-current-multi-method) multi-method)
6107 (set (make-local-variable 'tramp-current-method) method)
6108 (set (make-local-variable 'tramp-current-user) user)
6109 (set (make-local-variable 'tramp-current-host) host)
6110 (set (make-local-variable 'tramp-chunksize) chunksize)
6111 (set (make-local-variable 'inhibit-eol-conversion) nil)
6114 (defun tramp-open-connection-setup-interactive-shell
6115 (p multi-method method user host)
6116 "Set up an interactive shell.
6117 Mainly sets the prompt and the echo correctly. P is the shell process
6118 to set up. METHOD, USER and HOST specify the connection."
6119 ;; Wait a bit in case the remote end feels like sending a little
6120 ;; junk first. It seems that fencepost.gnu.org does this when doing
6121 ;; a Kerberos login.
6123 (tramp-discard-garbage-erase-buffer p multi-method method user host)
6124 (tramp-process-initial-commands p multi-method method user host
6125 tramp-initial-commands)
6126 ;; It is useful to set the prompt in the following command because
6127 ;; some people have a setting for $PS1 which /bin/sh doesn't know
6128 ;; about and thus /bin/sh will display a strange prompt. For
6129 ;; example, if $PS1 has "${CWD}" in the value, then ksh will display
6130 ;; the current working directory but /bin/sh will display a dollar
6131 ;; sign. The following command line sets $PS1 to a sane value, and
6132 ;; works under Bourne-ish shells as well as csh-like shells. Daniel
6133 ;; Pittman reports that the unusual positioning of the single quotes
6134 ;; makes it work under `rc', too. We also unset the variable $ENV
6135 ;; because that is read by some sh implementations (eg, bash when
6136 ;; called as sh) on startup; this way, we avoid the startup file
6138 (tramp-send-command-internal
6139 multi-method method user host
6140 (format "exec env 'ENV=' 'PS1=$ ' %s"
6141 (tramp-get-method-parameter
6142 multi-method method user host 'tramp-remote-sh))
6143 (format "remote `%s' to come up"
6144 (tramp-get-method-parameter
6145 multi-method method user host 'tramp-remote-sh)))
6146 (tramp-barf-if-no-shell-prompt
6148 "Remote `%s' didn't come up. See buffer `%s' for details"
6149 (tramp-get-method-parameter multi-method method user host 'tramp-remote-sh)
6151 (tramp-message 8 "Setting up remote shell environment")
6152 (tramp-discard-garbage-erase-buffer p multi-method method user host)
6153 (tramp-send-command-internal multi-method method user host
6154 "stty -inlcr -echo kill '^U'")
6156 ;; Ignore garbage after stty command.
6157 (tramp-send-command-internal multi-method method user host
6160 (tramp-send-command-internal multi-method method user host
6161 "TERM=dumb; export TERM")
6163 ;; Check whether the remote host suffers from buggy `send-process-string'.
6164 ;; This is known for FreeBSD (see comment in `send_process', file process.c).
6165 ;; I've tested sending 624 bytes successfully, sending 625 bytes failed.
6166 ;; Emacs makes a hack when this host type is detected locally. It cannot
6167 ;; handle remote hosts, though.
6168 (when (or (not tramp-chunksize) (zerop tramp-chunksize))
6169 (tramp-message 9 "Checking remote host type for `send-process-string' bug")
6170 (tramp-send-command-internal multi-method method user host
6171 "(uname -sr) 2>/dev/null")
6172 (goto-char (point-min))
6173 (when (looking-at "FreeBSD")
6174 (setq tramp-chunksize 500)))
6176 ;; Try to set up the coding system correctly.
6177 ;; CCC this can't be the right way to do it. Hm.
6180 (tramp-message 9 "Determining coding system")
6181 (tramp-send-command-internal multi-method method user host
6182 "echo foo ; echo bar")
6183 (goto-char (point-min))
6184 (if (featurep 'mule)
6185 ;; Use MULE to select the right EOL convention for communicating
6186 ;; with the process.
6187 (let* ((cs (or (process-coding-system p) (cons 'undecided 'undecided)))
6188 cs-decode cs-encode)
6189 (when (symbolp cs) (setq cs (cons cs cs)))
6190 (setq cs-decode (car cs))
6191 (setq cs-encode (cdr cs))
6192 (unless cs-decode (setq cs-decode 'undecided))
6193 (unless cs-encode (setq cs-encode 'undecided))
6194 (setq cs-encode (tramp-coding-system-change-eol-conversion
6196 (when (search-forward "\r" nil t)
6197 (setq cs-decode (tramp-coding-system-change-eol-conversion
6199 (set-buffer-process-coding-system cs-decode cs-encode))
6200 ;; Look for ^M and do something useful if found.
6201 (when (search-forward "\r" nil t)
6202 ;; We have found a ^M but cannot frob the process coding system
6203 ;; because we're running on a non-MULE Emacs. Let's try
6206 (tramp-message 9 "Trying `stty -onlcr'")
6207 (tramp-send-command-internal multi-method method user host
6211 9 "Waiting 30s for `HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE'")
6212 (tramp-send-command-internal
6213 multi-method method user host
6214 "HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE")
6216 (tramp-message 9 "Waiting 30s for `set +o vi +o emacs'")
6217 (tramp-send-command-internal multi-method method user host
6218 "set +o vi +o emacs")
6220 (tramp-message 9 "Waiting 30s for `unset MAIL MAILCHECK MAILPATH'")
6221 (tramp-send-command-internal
6222 multi-method method user host
6223 "unset MAIL MAILCHECK MAILPATH 1>/dev/null 2>/dev/null")
6225 (tramp-message 9 "Waiting 30s for `unset CDPATH'")
6226 (tramp-send-command-internal multi-method method user host
6229 (tramp-message 9 "Setting shell prompt")
6230 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we must
6231 ;; use "\n" here, not tramp-rsh-end-of-line. We also manually frob
6232 ;; the last time we sent a command, to avoid tramp-send-command to send
6233 ;; "echo are you awake".
6234 (setq tramp-last-cmd-time (current-time))
6236 multi-method method user host
6237 (format "PS1='%s%s%s'; PS2=''; PS3=''"
6238 tramp-rsh-end-of-line
6240 tramp-rsh-end-of-line))
6241 (tramp-wait-for-output))
6243 (defun tramp-post-connection (multi-method method user host)
6244 "Prepare a remote shell before being able to work on it.
6245 METHOD, USER and HOST specify the connection.
6246 Among other things, this finds a shell which groks tilde expansion,
6247 tries to find an `ls' command which groks the `-n' option, sets the
6248 locale to C and sets up the remote shell search path."
6249 ;; Search for a good shell before searching for a command which
6250 ;; checks if a file exists. This is done because Tramp wants to use
6251 ;; "test foo; echo $?" to check if various conditions hold, and
6252 ;; there are buggy /bin/sh implementations which don't execute the
6253 ;; "echo $?" part if the "test" part has an error. In particular,
6254 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
6255 ;; with buggy /bin/sh implementations will have a working bash or
6257 (tramp-find-shell multi-method method user host)
6258 ;; Without (sit-for 0.1) at least, my machine will almost always blow
6259 ;; up on 'not numberp /root' - a race that causes the 'echo ~root'
6260 ;; output of (tramp-find-shell) to show up along with the output of
6261 ;; (tramp-find-ls-command) testing.
6263 ;; I can't work out why this is a problem though. The (tramp-wait-for-output)
6264 ;; call in (tramp-find-shell) *should* make this not happen, I thought.
6266 ;; After much debugging I couldn't find any problem with the implementation
6267 ;; of that function though. The workaround stays for me at least. :/
6269 ;; Daniel Pittman <daniel@danann.net>
6272 (tramp-find-file-exists-command multi-method method user host)
6273 (make-local-variable 'tramp-ls-command)
6274 (setq tramp-ls-command (tramp-find-ls-command multi-method method user host))
6275 (unless tramp-ls-command
6278 "Danger! Couldn't find ls which groks -n. Muddling through anyway")
6279 (setq tramp-ls-command
6280 (tramp-find-executable multi-method method user host
6281 "ls" tramp-remote-path nil)))
6282 (unless tramp-ls-command
6283 (error "Fatal error: Couldn't find remote executable `ls'"))
6284 (tramp-message 5 "Using remote command `%s' for getting directory listings"
6286 (tramp-send-command multi-method method user host
6287 (concat "tramp_set_exit_status () {" tramp-rsh-end-of-line
6288 "return $1" tramp-rsh-end-of-line
6290 (tramp-wait-for-output)
6291 ;; Set remote PATH variable.
6292 (tramp-set-remote-path multi-method method user host "PATH" tramp-remote-path)
6293 ;; Tell remote shell to use standard time format, needed for
6294 ;; parsing `ls -l' output.
6295 (tramp-send-command multi-method method user host
6296 "LC_TIME=C; export LC_TIME; echo huhu")
6297 (tramp-wait-for-output)
6298 (tramp-send-command multi-method method user host
6299 "mesg n; echo huhu")
6300 (tramp-wait-for-output)
6301 (tramp-send-command multi-method method user host
6302 "biff n ; echo huhu")
6303 (tramp-wait-for-output)
6304 ;; Unalias ls(1) to work around issues with those silly people who make it
6305 ;; spit out ANSI escapes or whatever.
6306 (tramp-send-command multi-method method user host
6307 "unalias ls; echo huhu")
6308 (tramp-wait-for-output)
6309 ;; Does `test A -nt B' work? Use abominable `find' construct if it
6310 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
6311 ;; for otherwise the shell crashes.
6313 (make-local-variable 'tramp-test-groks-nt)
6314 (tramp-send-command multi-method method user host
6316 (tramp-wait-for-output)
6317 (goto-char (point-min))
6318 (setq tramp-test-groks-nt
6319 (looking-at (format "\n%s\r?\n" (regexp-quote tramp-end-of-output))))
6320 (unless tramp-test-groks-nt
6322 multi-method method user host
6323 (concat "tramp_test_nt () {" tramp-rsh-end-of-line
6324 "test -n \"`find $1 -prune -newer $2 -print`\"" tramp-rsh-end-of-line
6326 (tramp-wait-for-output)
6327 ;; Send the fallback `uudecode' script.
6329 (tramp-send-string multi-method method user host tramp-uudecode)
6330 (tramp-wait-for-output)
6333 (tramp-set-connection-property "perl-scripts" nil multi-method method user host)
6334 (let ((tramp-remote-perl
6335 (or (tramp-find-executable multi-method method user host
6336 "perl5" tramp-remote-path nil)
6337 (tramp-find-executable multi-method method user host
6338 "perl" tramp-remote-path nil))))
6339 (when tramp-remote-perl
6340 (tramp-set-connection-property "perl" tramp-remote-perl
6341 multi-method method user host)
6342 (unless (tramp-method-out-of-band-p multi-method method user host)
6343 (tramp-message 5 "Sending the Perl `mime-encode' implementations.")
6345 multi-method method user host
6346 (concat "tramp_encode () {\n"
6347 (format tramp-perl-encode tramp-remote-perl)
6350 (tramp-wait-for-output)
6352 multi-method method user host
6353 (concat "tramp_encode_with_module () {\n"
6354 (format tramp-perl-encode-with-module tramp-remote-perl)
6357 (tramp-wait-for-output)
6358 (tramp-message 5 "Sending the Perl `mime-decode' implementations.")
6360 multi-method method user host
6361 (concat "tramp_decode () {\n"
6362 (format tramp-perl-decode tramp-remote-perl)
6365 (tramp-wait-for-output)
6367 multi-method method user host
6368 (concat "tramp_decode_with_module () {\n"
6369 (format tramp-perl-decode-with-module tramp-remote-perl)
6372 (tramp-wait-for-output))))
6375 (let ((ln (tramp-find-executable multi-method method user host
6376 "ln" tramp-remote-path nil)))
6378 (tramp-set-connection-property "ln" ln multi-method method user host)))
6381 (tramp-send-command multi-method method user host "id -u; id -g")
6382 (tramp-wait-for-output)
6383 (goto-char (point-min))
6384 (tramp-set-connection-property
6385 "uid" (read (current-buffer)) multi-method method user host)
6386 (tramp-set-connection-property
6387 "gid" (read (current-buffer)) multi-method method user host)
6388 ;; Find the right encoding/decoding commands to use.
6390 (unless (tramp-method-out-of-band-p multi-method method user host)
6391 (tramp-find-inline-encoding multi-method method user host))
6392 ;; If encoding/decoding command are given, test to see if they work.
6393 ;; CCC: Maybe it would be useful to run the encoder both locally and
6394 ;; remotely to see if they produce the same result.
6395 (let ((rem-enc (tramp-get-remote-encoding multi-method method user host))
6396 (rem-dec (tramp-get-remote-decoding multi-method method user host))
6397 (magic-string "xyzzy"))
6398 (when (and (or rem-dec rem-enc) (not (and rem-dec rem-enc)))
6399 (tramp-kill-process multi-method method user host)
6400 ;; Improve error message and/or error check.
6402 "Must give both decoding and encoding command in method definition"))
6403 (when (and rem-enc rem-dec)
6406 "Checking to see if encoding/decoding commands work on remote host...")
6408 multi-method method user host
6409 (format "echo %s | %s | %s"
6410 (tramp-shell-quote-argument magic-string) rem-enc rem-dec))
6411 (tramp-wait-for-output)
6412 (unless (looking-at (regexp-quote magic-string))
6413 (tramp-kill-process multi-method method user host)
6414 (error "Remote host cannot execute de/encoding commands. See buffer `%s' for details"
6418 5 "Checking to see if encoding/decoding commands work on remote host...done"))))
6420 ;; CCC: We should either implement a Perl version of base64 encoding
6421 ;; and decoding. Then we just use that in the last item. The other
6422 ;; alternative is to use the Perl version of UU encoding. But then
6423 ;; we need a Lisp version of uuencode.
6425 ;; Old text from documentation of tramp-methods:
6426 ;; Using a uuencode/uudecode inline method is discouraged, please use one
6427 ;; of the base64 methods instead since base64 encoding is much more
6428 ;; reliable and the commands are more standardized between the different
6429 ;; Unix versions. But if you can't use base64 for some reason, please
6430 ;; note that the default uudecode command does not work well for some
6431 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
6432 ;; the following command for uudecode:
6434 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
6436 ;; For Irix, no solution is known yet.
6438 (defvar tramp-coding-commands
6439 '(("mimencode -b" "mimencode -u -b"
6440 base64-encode-region base64-decode-region)
6441 ("mmencode -b" "mmencode -u -b"
6442 base64-encode-region base64-decode-region)
6443 ("recode data..base64" "recode base64..data"
6444 base64-encode-region base64-decode-region)
6445 ("uuencode xxx" "uudecode -o /dev/stdout"
6446 tramp-uuencode-region uudecode-decode-region)
6447 ("uuencode xxx" "uudecode -o -"
6448 tramp-uuencode-region uudecode-decode-region)
6449 ("uuencode xxx" "uudecode -p"
6450 tramp-uuencode-region uudecode-decode-region)
6451 ("uuencode xxx" "tramp_uudecode"
6452 tramp-uuencode-region uudecode-decode-region)
6453 ("tramp_encode_with_module" "tramp_decode_with_module"
6454 base64-encode-region base64-decode-region)
6455 ("tramp_encode" "tramp_decode"
6456 base64-encode-region base64-decode-region))
6457 "List of coding commands for inline transfer.
6458 Each item is a list that looks like this:
6460 \(REMOTE-ENCODING REMOTE-DECODING LOCAL-ENCODING LOCAL-DECODING)
6462 The REMOTE-ENCODING should be a string, giving a command accepting a
6463 plain file on standard input and writing the encoded file to standard
6464 output. The REMOTE-DECODING should also be a string, giving a command
6465 accepting an encoded file on standard input and writing the decoded
6466 file to standard output.
6468 LOCAL-ENCODING and LOCAL-DECODING can be strings, giving commands, or
6469 symbols, giving functions. If they are strings, then they can contain
6470 the \"%s\" format specifier. If that specifier is present, the input
6471 filename will be put into the command line at that spot. If the
6472 specifier is not present, the input should be read from standard
6475 If they are functions, they will be called with two arguments, start
6476 and end of region, and are expected to replace the region contents
6477 with the encoded or decoded results, respectively.")
6479 (defun tramp-find-inline-encoding (multi-method method user host)
6480 "Find an inline transfer encoding that works.
6481 Goes through the list `tramp-coding-commands'."
6482 (let ((commands tramp-coding-commands)
6485 (while (and commands (null found))
6486 (setq item (pop commands))
6488 (let ((rem-enc (nth 0 item))
6489 (rem-dec (nth 1 item))
6490 (loc-enc (nth 2 item))
6491 (loc-dec (nth 3 item)))
6492 ;; Check if remote encoding and decoding commands can be
6493 ;; called remotely with null input and output. This makes
6494 ;; sure there are no syntax errors and the command is really
6495 ;; found. Note that we do not redirect stdout to /dev/null,
6496 ;; for two reaons: when checking the decoding command, we
6497 ;; actually check the output it gives. And also, when
6498 ;; redirecting "mimencode" output to /dev/null, then as root
6499 ;; it might change the permissions of /dev/null!
6500 (tramp-message-for-buffer
6501 multi-method method user host 9
6502 "Checking remote encoding command `%s' for sanity" rem-enc)
6503 (unless (zerop (tramp-send-command-and-check
6504 multi-method method user host
6505 (format "%s </dev/null" rem-enc) t))
6506 (throw 'wont-work nil))
6507 (tramp-message-for-buffer
6508 multi-method method user host 9
6509 "Checking remote decoding command `%s' for sanity" rem-dec)
6510 (unless (zerop (tramp-send-command-and-check
6511 multi-method method user host
6512 (format "echo %s | %s | %s"
6513 magic rem-enc rem-dec) t))
6514 (throw 'wont-work nil))
6516 (goto-char (point-min))
6517 (unless (looking-at (regexp-quote magic))
6518 (throw 'wont-work nil)))
6519 ;; If the local encoder or decoder is a string, the
6520 ;; corresponding command has to work locally.
6521 (when (stringp loc-enc)
6522 (tramp-message-for-buffer
6523 multi-method method user host 9
6524 "Checking local encoding command `%s' for sanity" loc-enc)
6525 (unless (zerop (tramp-call-local-coding-command
6527 (throw 'wont-work nil)))
6528 (when (stringp loc-dec)
6529 (tramp-message-for-buffer
6530 multi-method method user host 9
6531 "Checking local decoding command `%s' for sanity" loc-dec)
6532 (unless (zerop (tramp-call-local-coding-command
6534 (throw 'wont-work nil)))
6535 ;; CCC: At this point, maybe we should check that the output
6536 ;; of the commands is correct. But for the moment we will
6537 ;; assume that commands working on empty input will also
6538 ;; work in practice.
6539 (setq found item))))
6540 ;; Did we find something? If not, issue error. If so,
6541 ;; set connection properties.
6543 (error "Couldn't find an inline transfer encoding"))
6544 (let ((rem-enc (nth 0 found))
6545 (rem-dec (nth 1 found))
6546 (loc-enc (nth 2 found))
6547 (loc-dec (nth 3 found)))
6548 (tramp-message 10 "Using remote encoding %s" rem-enc)
6549 (tramp-set-remote-encoding multi-method method user host rem-enc)
6550 (tramp-message 10 "Using remote decoding %s" rem-dec)
6551 (tramp-set-remote-decoding multi-method method user host rem-dec)
6552 (tramp-message 10 "Using local encoding %s" loc-enc)
6553 (tramp-set-local-encoding multi-method method user host loc-enc)
6554 (tramp-message 10 "Using local decoding %s" loc-dec)
6555 (tramp-set-local-decoding multi-method method user host loc-dec))))
6557 (defun tramp-call-local-coding-command (cmd input output)
6558 "Call the local encoding or decoding command.
6559 If CMD contains \"%s\", provide input file INPUT there in command.
6560 Otherwise, INPUT is passed via standard input.
6561 INPUT can also be nil which means `/dev/null'.
6562 OUTPUT can be a string (which specifies a filename), or t (which
6563 means standard output and thus the current buffer), or nil (which
6566 tramp-encoding-shell ;program
6567 (when (and input (not (string-match "%s" cmd)))
6569 (if (eq output t) t nil) ;output
6571 tramp-encoding-command-switch
6572 ;; actual shell command
6574 (if (string-match "%s" cmd) (format cmd input) cmd)
6575 (if (stringp output) (concat "> " output) ""))))
6577 (defun tramp-maybe-open-connection (multi-method method user host)
6578 "Maybe open a connection to HOST, logging in as USER, using METHOD.
6579 Does not do anything if a connection is already open, but re-opens the
6580 connection if a previous connection has died for some reason."
6581 (let ((p (get-buffer-process
6582 (tramp-get-buffer multi-method method user host)))
6584 ;; If too much time has passed since last command was sent, look
6585 ;; whether process is still alive. If it isn't, kill it. When
6586 ;; using ssh, it can sometimes happen that the remote end has hung
6587 ;; up but the local ssh client doesn't recognize this until it
6588 ;; tries to send some data to the remote end. So that's why we
6589 ;; try to send a command from time to time, then look again
6590 ;; whether the process is really alive.
6592 (set-buffer (tramp-get-buffer multi-method method user host))
6593 (when (and tramp-last-cmd-time
6594 (> (tramp-time-diff (current-time) tramp-last-cmd-time) 60)
6595 p (processp p) (memq (process-status p) '(run open)))
6597 multi-method method user host "echo are you awake" nil t)
6598 (unless (and (memq (process-status p) '(run open))
6599 (tramp-wait-for-output 10))
6603 (unless (and p (processp p) (memq (process-status p) '(run open)))
6604 (when (and p (processp p))
6606 (let ((process-connection-type tramp-process-connection-type))
6607 (funcall (tramp-get-method-parameter
6609 (tramp-find-method multi-method method user host)
6610 user host 'tramp-connection-function)
6611 multi-method method user host)))))
6613 (defun tramp-send-command
6614 (multi-method method user host command &optional noerase neveropen)
6615 "Send the COMMAND to USER at HOST (logged in using METHOD).
6616 Erases temporary buffer before sending the command (unless NOERASE
6618 If optional seventh arg NEVEROPEN is non-nil, never try to open the
6619 connection. This is meant to be used from
6620 `tramp-maybe-open-connection' only."
6622 (tramp-maybe-open-connection multi-method method user host))
6623 (setq tramp-last-cmd-time (current-time))
6624 (setq tramp-last-cmd command)
6625 (when tramp-debug-buffer
6627 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6628 (goto-char (point-max))
6629 (tramp-insert-with-face 'bold (format "$ %s\n" command))))
6631 (set-buffer (tramp-get-buffer multi-method method user host))
6632 (unless noerase (erase-buffer))
6633 (setq proc (get-buffer-process (current-buffer)))
6634 (process-send-string proc
6635 (concat command tramp-rsh-end-of-line))))
6637 (defun tramp-send-command-internal
6638 (multi-method method user host command &optional msg)
6639 "Send command to remote host and wait for success.
6640 Sends COMMAND, then waits 30 seconds for shell prompt."
6641 (tramp-send-command multi-method method user host command t t)
6643 (tramp-message 9 "Waiting 30s for %s..." msg))
6644 (tramp-barf-if-no-shell-prompt
6646 "Couldn't `%s', see buffer `%s'" command (buffer-name)))
6648 (defun tramp-wait-for-output (&optional timeout)
6649 "Wait for output from remote rsh command."
6650 (let ((proc (get-buffer-process (current-buffer)))
6652 (start-time (current-time))
6653 (start-point (point))
6654 (end-of-output (concat "^"
6655 (regexp-quote tramp-end-of-output)
6657 ;; Algorithm: get waiting output. See if last line contains
6658 ;; end-of-output sentinel. If not, wait a bit and again get
6659 ;; waiting output. Repeat until timeout expires or end-of-output
6660 ;; sentinel is seen. Will hang if timeout is nil and
6661 ;; end-of-output sentinel never appears.
6664 ;; Work around an XEmacs bug, where the timeout expires
6665 ;; faster than it should. This degenerates into polling
6666 ;; for buggy XEmacsen, but oh, well.
6667 (while (and (not found)
6668 (< (tramp-time-diff (current-time) start-time)
6670 (with-timeout (timeout)
6672 (tramp-accept-process-output proc 1)
6673 (unless (memq (process-status proc) '(run open))
6674 (error "Process has died"))
6675 (goto-char (point-max))
6677 (setq found (looking-at end-of-output))))))
6680 (tramp-accept-process-output proc 1)
6681 (unless (memq (process-status proc) '(run open))
6682 (error "Process has died"))
6683 (goto-char (point-max))
6685 (setq found (looking-at end-of-output))))))
6686 ;; At this point, either the timeout has expired or we have found
6687 ;; the end-of-output sentinel.
6689 (goto-char (point-max))
6691 (delete-region (point) (point-max)))
6692 ;; If processing echoes, look for it in the first line and delete.
6693 (when tramp-process-echoes
6695 (goto-char start-point)
6696 (when (looking-at (regexp-quote tramp-last-cmd))
6697 (delete-region (point) (progn (forward-line 1) (point))))))
6698 ;; Add output to debug buffer if appropriate.
6699 (when tramp-debug-buffer
6701 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6702 tramp-current-user tramp-current-host)
6703 (point-min) (point-max))
6707 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6708 tramp-current-user tramp-current-host))
6709 (goto-char (point-max))
6710 (insert "[[Remote prompt `" end-of-output "' not found"
6711 (if timeout (format " in %d secs" timeout) "")
6713 (goto-char (point-min))
6714 ;; Return value is whether end-of-output sentinel was found.
6717 (defun tramp-send-command-and-check (multi-method method user host command
6719 "Run COMMAND and check its exit status.
6720 MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST.
6721 Sends `echo $?' along with the COMMAND for checking the exit status. If
6722 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
6724 If the optional argument SUBSHELL is non-nil, the command is executed in
6725 a subshell, ie surrounded by parentheses."
6726 (tramp-send-command multi-method method user host
6727 (concat (if subshell "( " "")
6729 (if command " 2>/dev/null; " "")
6730 "echo tramp_exit_status $?"
6731 (if subshell " )" " ")))
6732 (tramp-wait-for-output)
6733 (goto-char (point-max))
6734 (unless (search-backward "tramp_exit_status " nil t)
6735 (error "Couldn't find exit status of `%s'" command))
6736 (skip-chars-forward "^ ")
6737 (read (current-buffer)))
6739 (defun tramp-barf-unless-okay (multi-method method user host command subshell
6740 signal fmt &rest args)
6741 "Run COMMAND, check exit status, throw error if exit status not okay.
6742 Similar to `tramp-send-command-and-check' but accepts two more arguments
6743 FMT and ARGS which are passed to `error'."
6744 (unless (zerop (tramp-send-command-and-check
6745 multi-method method user host command subshell))
6746 ;; CCC: really pop-to-buffer? Maybe it's appropriate to be more
6748 (pop-to-buffer (current-buffer))
6749 (funcall 'signal signal (apply 'format fmt args))))
6751 ;; It seems that Tru64 Unix does not like it if long strings are sent
6752 ;; to it in one go. (This happens when sending the Perl
6753 ;; `file-attributes' implementation, for instance.) Therefore, we
6754 ;; have this function which waits a bit at each line.
6755 (defun tramp-send-string
6756 (multi-method method user host string)
6757 "Send the STRING to USER at HOST using METHOD.
6759 The STRING is expected to use Unix line-endings, but the lines sent to
6760 the remote host use line-endings as defined in the variable
6761 `tramp-rsh-end-of-line'."
6762 (let ((proc (get-buffer-process
6763 (tramp-get-buffer multi-method method user host))))
6765 (error "Can't send string to remote host -- not logged in"))
6767 (when tramp-debug-buffer
6769 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6770 (goto-char (point-max))
6771 (tramp-insert-with-face 'bold (format "$ %s\n" string))))
6772 ;; replace "\n" by `tramp-rsh-end-of-line'
6774 (mapconcat 'identity
6775 (split-string string "\n")
6776 tramp-rsh-end-of-line))
6777 (unless (or (string= string "")
6778 (string-equal (substring string -1) tramp-rsh-end-of-line))
6779 (setq string (concat string tramp-rsh-end-of-line)))
6781 (if (and tramp-chunksize (not (zerop tramp-chunksize)))
6783 (end (length string)))
6785 (tramp-message-for-buffer
6786 multi-method method user host 10
6787 "Sending chunk from %s to %s"
6788 pos (min (+ pos tramp-chunksize) end))
6789 (process-send-string
6790 proc (substring string pos (min (+ pos tramp-chunksize) end)))
6791 (setq pos (+ pos tramp-chunksize))
6793 (process-send-string proc string))))
6795 (defun tramp-send-eof (multi-method method user host)
6796 "Send EOF to the remote end.
6797 METHOD, HOST and USER specify the connection."
6798 (let ((proc (get-buffer-process
6799 (tramp-get-buffer multi-method method user host))))
6801 (error "Can't send EOF to remote host -- not logged in"))
6802 (process-send-eof proc)))
6803 ; (process-send-string proc "\^D")))
6805 (defun tramp-kill-process (multi-method method user host)
6806 "Kill the connection process used by Tramp.
6807 MULTI-METHOD, METHOD, USER, and HOST specify the connection."
6808 (let ((proc (get-buffer-process
6809 (tramp-get-buffer multi-method method user host))))
6810 (kill-process proc)))
6812 (defun tramp-discard-garbage-erase-buffer (p multi-method method user host)
6813 "Erase buffer, then discard subsequent garbage.
6814 If `tramp-discard-garbage' is nil, just erase buffer."
6815 (if (not tramp-discard-garbage)
6817 (while (prog1 (erase-buffer) (tramp-accept-process-output p 0.25))
6818 (when tramp-debug-buffer
6820 (set-buffer (tramp-get-debug-buffer multi-method method user host))
6821 (goto-char (point-max))
6822 (tramp-insert-with-face
6823 'bold (format "Additional characters detected\n")))))))
6825 (defun tramp-mode-string-to-int (mode-string)
6826 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
6827 (let* ((mode-chars (string-to-vector mode-string))
6828 (owner-read (aref mode-chars 1))
6829 (owner-write (aref mode-chars 2))
6830 (owner-execute-or-setid (aref mode-chars 3))
6831 (group-read (aref mode-chars 4))
6832 (group-write (aref mode-chars 5))
6833 (group-execute-or-setid (aref mode-chars 6))
6834 (other-read (aref mode-chars 7))
6835 (other-write (aref mode-chars 8))
6836 (other-execute-or-sticky (aref mode-chars 9)))
6840 (?r (tramp-octal-to-decimal "00400")) (?- 0)
6841 (t (error "Second char `%c' must be one of `r-'" owner-read)))
6843 (?w (tramp-octal-to-decimal "00200")) (?- 0)
6844 (t (error "Third char `%c' must be one of `w-'" owner-write)))
6845 (case owner-execute-or-setid
6846 (?x (tramp-octal-to-decimal "00100"))
6847 (?S (tramp-octal-to-decimal "04000"))
6848 (?s (tramp-octal-to-decimal "04100"))
6850 (t (error "Fourth char `%c' must be one of `xsS-'"
6851 owner-execute-or-setid)))
6853 (?r (tramp-octal-to-decimal "00040")) (?- 0)
6854 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
6856 (?w (tramp-octal-to-decimal "00020")) (?- 0)
6857 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
6858 (case group-execute-or-setid
6859 (?x (tramp-octal-to-decimal "00010"))
6860 (?S (tramp-octal-to-decimal "02000"))
6861 (?s (tramp-octal-to-decimal "02010"))
6863 (t (error "Seventh char `%c' must be one of `xsS-'"
6864 group-execute-or-setid)))
6866 (?r (tramp-octal-to-decimal "00004")) (?- 0)
6867 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
6869 (?w (tramp-octal-to-decimal "00002")) (?- 0)
6870 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
6871 (case other-execute-or-sticky
6872 (?x (tramp-octal-to-decimal "00001"))
6873 (?T (tramp-octal-to-decimal "01000"))
6874 (?t (tramp-octal-to-decimal "01001"))
6876 (t (error "Tenth char `%c' must be one of `xtT-'"
6877 other-execute-or-sticky)))))))
6879 (defun tramp-convert-file-attributes (multi-method method user host attr)
6880 "Convert file-attributes ATTR generated by perl script or ls.
6881 Convert file mode bits to string and set virtual device number.
6883 ;; Convert file mode bits to string.
6884 (unless (stringp (nth 8 attr))
6885 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr))))
6886 ;; Set file's gid change bit. Possible only when id-format is 'integer.
6887 (when (numberp (nth 3 attr))
6888 (setcar (nthcdr 9 attr)
6889 (not (eql (nth 3 attr)
6890 (tramp-get-remote-gid multi-method method user host)))))
6891 ;; Set virtual device number.
6892 (setcar (nthcdr 11 attr)
6893 (tramp-get-device multi-method method user host))
6896 (defun tramp-get-device (multi-method method user host)
6897 "Returns the virtual device number.
6898 If it doesn't exist, generate a new one."
6899 (let ((string (tramp-make-tramp-file-name multi-method method user host "")))
6900 (unless (assoc string tramp-devices)
6901 (add-to-list 'tramp-devices
6902 (list string (length tramp-devices))))
6903 (list -1 (nth 1 (assoc string tramp-devices)))))
6905 (defun tramp-file-mode-from-int (mode)
6906 "Turn an integer representing a file mode into an ls(1)-like string."
6907 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
6908 (user (logand (lsh mode -6) 7))
6909 (group (logand (lsh mode -3) 7))
6910 (other (logand (lsh mode -0) 7))
6911 (suid (> (logand (lsh mode -9) 4) 0))
6912 (sgid (> (logand (lsh mode -9) 2) 0))
6913 (sticky (> (logand (lsh mode -9) 1) 0)))
6914 (setq user (tramp-file-mode-permissions user suid "s"))
6915 (setq group (tramp-file-mode-permissions group sgid "s"))
6916 (setq other (tramp-file-mode-permissions other sticky "t"))
6917 (concat type user group other)))
6920 (defun tramp-file-mode-permissions (perm suid suid-text)
6921 "Convert a permission bitset into a string.
6922 This is used internally by `tramp-file-mode-from-int'."
6923 (let ((r (> (logand perm 4) 0))
6924 (w (> (logand perm 2) 0))
6925 (x (> (logand perm 1) 0)))
6926 (concat (or (and r "r") "-")
6927 (or (and w "w") "-")
6928 (or (and suid x suid-text) ; suid, execute
6929 (and suid (upcase suid-text)) ; suid, !execute
6930 (and x "x") "-")))) ; !suid
6933 (defun tramp-decimal-to-octal (i)
6934 "Return a string consisting of the octal digits of I.
6935 Not actually used. Use `(format \"%o\" i)' instead?"
6936 (cond ((< i 0) (error "Cannot convert negative number to octal"))
6937 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
6939 (t (concat (tramp-decimal-to-octal (/ i 8))
6940 (number-to-string (% i 8))))))
6943 ;;(defun tramp-octal-to-decimal (ostr)
6944 ;; "Given a string of octal digits, return a decimal number."
6945 ;; (cond ((null ostr) 0)
6946 ;; ((string= "" ostr) 0)
6947 ;; (t (let ((last (aref ostr (1- (length ostr))))
6948 ;; (rest (substring ostr 0 (1- (length ostr)))))
6949 ;; (unless (and (>= last ?0)
6951 ;; (error "Not an octal digit: %c" last))
6952 ;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest)))))))
6953 ;; Kudos to Gerd Moellmann for this suggestion.
6954 (defun tramp-octal-to-decimal (ostr)
6955 "Given a string of octal digits, return a decimal number."
6956 (let ((x (or ostr "")))
6957 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
6958 (unless (string-match "\\`[0-7]*\\'" x)
6959 (error "Non-octal junk in string `%s'" x))
6960 (string-to-number ostr 8)))
6962 (defun tramp-shell-case-fold (string)
6963 "Converts STRING to shell glob pattern which ignores case."
6966 (if (equal (downcase c) (upcase c))
6968 (format "[%c%c]" (downcase c) (upcase c))))
6973 ;; ------------------------------------------------------------
6974 ;; -- TRAMP file names --
6975 ;; ------------------------------------------------------------
6976 ;; Conversion functions between external representation and
6977 ;; internal data structure. Convenience functions for internal
6980 (defstruct tramp-file-name multi-method method user host localname)
6982 (defun tramp-tramp-file-p (name)
6983 "Return t iff NAME is a tramp file."
6985 (string-match tramp-file-name-regexp name)))
6987 ;; HHH: Changed. Used to assign the return value of (user-login-name)
6988 ;; to the `user' part of the structure if a user name was not
6989 ;; provided, now it assigns nil.
6990 (defun tramp-dissect-file-name (name)
6991 "Return an `tramp-file-name' structure.
6992 The structure consists of remote method, remote user, remote host and
6993 localname (file name on remote host)."
6995 (let* ((match (string-match (nth 0 tramp-file-name-structure) name))
6998 (if match (match-string (nth 1 tramp-file-name-structure) name)
7001 (format (nth 0 tramp-multi-file-name-structure)
7002 (nth 0 tramp-multi-file-name-hop-structure)) name)
7003 (match-string (nth 1 tramp-multi-file-name-structure) name))))
7004 (if (and method (member method tramp-multi-methods))
7005 ;; If it's a multi method, the file name structure contains
7006 ;; arrays of method, user and host.
7007 (tramp-dissect-multi-file-name name)
7008 ;; Normal method. First, find out default method.
7009 (unless match (error "Not a tramp file name: %s" name))
7010 (let ((user (match-string (nth 2 tramp-file-name-structure) name))
7011 (host (match-string (nth 3 tramp-file-name-structure) name))
7012 (localname (match-string (nth 4 tramp-file-name-structure) name)))
7013 (make-tramp-file-name
7018 :localname localname))))))
7020 (defun tramp-find-default-method (user host)
7021 "Look up the right method to use in `tramp-default-method-alist'."
7022 (let ((choices tramp-default-method-alist)
7023 (method tramp-default-method)
7026 (setq item (pop choices))
7027 (when (and (string-match (or (nth 0 item) "") (or host ""))
7028 (string-match (or (nth 1 item) "") (or user "")))
7029 (setq method (nth 2 item))
7030 (setq choices nil)))
7033 (defun tramp-find-method (multi-method method user host)
7034 "Return the right method string to use.
7035 This is MULTI-METHOD, if non-nil. Otherwise, it is METHOD, if non-nil.
7036 If both MULTI-METHOD and METHOD are nil, do a lookup in
7037 `tramp-default-method-alist'."
7038 (or multi-method method (tramp-find-default-method user host)))
7040 ;; HHH: Not Changed. Multi method. Will probably not handle the case where
7041 ;; a user name is not provided in the "file name" very well.
7042 (defun tramp-dissect-multi-file-name (name)
7043 "Not implemented yet."
7044 (let ((regexp (nth 0 tramp-multi-file-name-structure))
7045 (method-index (nth 1 tramp-multi-file-name-structure))
7046 (hops-index (nth 2 tramp-multi-file-name-structure))
7047 (localname-index (nth 3 tramp-multi-file-name-structure))
7048 (hop-regexp (nth 0 tramp-multi-file-name-hop-structure))
7049 (hop-method-index (nth 1 tramp-multi-file-name-hop-structure))
7050 (hop-user-index (nth 2 tramp-multi-file-name-hop-structure))
7051 (hop-host-index (nth 3 tramp-multi-file-name-hop-structure))
7052 method hops len hop-methods hop-users hop-hosts localname)
7053 (unless (string-match (format regexp hop-regexp) name)
7054 (error "Not a multi tramp file name: %s" name))
7055 (setq method (match-string method-index name))
7056 (setq hops (match-string hops-index name))
7057 (setq len (/ (length (match-data t)) 2))
7058 (when (< localname-index 0) (incf localname-index len))
7059 (setq localname (match-string localname-index name))
7061 (while (string-match hop-regexp hops index)
7062 (setq index (match-end 0))
7064 (cons (match-string hop-method-index hops) hop-methods))
7066 (cons (match-string hop-user-index hops) hop-users))
7068 (cons (match-string hop-host-index hops) hop-hosts))))
7069 (make-tramp-file-name
7070 :multi-method method
7071 :method (apply 'vector (reverse hop-methods))
7072 :user (apply 'vector (reverse hop-users))
7073 :host (apply 'vector (reverse hop-hosts))
7074 :localname localname)))
7076 (defun tramp-make-tramp-file-name (multi-method method user host localname)
7077 "Constructs a tramp file name from METHOD, USER, HOST and LOCALNAME."
7079 (tramp-make-tramp-multi-file-name multi-method method user host localname)
7081 (concat tramp-prefix-format
7082 (when method (concat "%m" tramp-postfix-single-method-format))
7083 (when user (concat "%u" tramp-postfix-user-format))
7084 (when host (concat "%h" tramp-postfix-host-format))
7085 (when localname (concat "%p")))
7086 `((?m . ,method) (?u . ,user) (?h . ,host) (?p . ,localname)))))
7088 ;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done
7089 ;; with this when USER is nil?
7090 (defun tramp-make-tramp-multi-file-name (multi-method method user host localname)
7091 "Constructs a tramp file name for a multi-hop method."
7092 (unless tramp-make-multi-tramp-file-format
7093 (error "`tramp-make-multi-tramp-file-format' is nil"))
7094 (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format))
7095 (hop-format (nth 1 tramp-make-multi-tramp-file-format))
7096 (localname-format (nth 2 tramp-make-multi-tramp-file-format))
7097 (prefix (format-spec prefix-format `((?m . ,multi-method))))
7099 (localname (format-spec localname-format `((?p . ,localname))))
7101 (len (length method)))
7103 (let ((m (aref method i)) (u (aref user i)) (h (aref host i)))
7104 (setq hops (concat hops (format-spec hop-format
7105 `((?m . ,m) (?u . ,u) (?h . ,h)))))
7107 (concat prefix hops localname)))
7109 (defun tramp-make-copy-program-file-name (user host localname)
7110 "Create a file name suitable to be passed to `rcp' and workalikes."
7112 (format "%s@%s:%s" user host localname)
7113 (format "%s:%s" host localname)))
7115 (defun tramp-method-out-of-band-p (multi-method method user host)
7116 "Return t if this is an out-of-band method, nil otherwise."
7117 (tramp-get-method-parameter
7119 (tramp-find-method multi-method method user host)
7120 user host 'tramp-copy-program))
7122 ;; Variables local to connection.
7124 (defun tramp-get-ls-command (multi-method method user host)
7127 (tramp-maybe-open-connection multi-method method user host)
7128 (set-buffer (tramp-get-buffer multi-method method user host))
7130 (error "Couldn't find remote `ls' command")))
7132 (defun tramp-get-test-groks-nt (multi-method method user host)
7134 (tramp-maybe-open-connection multi-method method user host)
7135 (set-buffer (tramp-get-buffer multi-method method user host))
7136 tramp-test-groks-nt))
7138 (defun tramp-get-file-exists-command (multi-method method user host)
7141 (tramp-maybe-open-connection multi-method method user host)
7142 (set-buffer (tramp-get-buffer multi-method method user host))
7143 tramp-file-exists-command)
7144 (error "Couldn't find remote `test -e' command")))
7146 (defun tramp-get-remote-perl (multi-method method user host)
7147 (tramp-get-connection-property "perl" nil multi-method method user host))
7149 (defun tramp-get-remote-ln (multi-method method user host)
7151 (tramp-get-connection-property "ln" nil multi-method method user host)
7152 (error "Couldn't find remote `ln' command")))
7154 (defun tramp-get-remote-uid (multi-method method user host)
7155 (tramp-get-connection-property "uid" nil multi-method method user host))
7157 (defun tramp-get-remote-gid (multi-method method user host)
7158 (tramp-get-connection-property "gid" nil multi-method method user host))
7160 ;; Get a property of a TRAMP connection.
7161 (defun tramp-get-connection-property
7162 (property default multi-method method user host)
7163 "Get the named property for the connection.
7164 If the value is not set for the connection, return `default'"
7165 (tramp-maybe-open-connection multi-method method user host)
7166 (with-current-buffer (tramp-get-buffer multi-method method user host)
7169 (symbol-value (intern (concat "tramp-connection-property-" property)))
7172 ;; Set a property of a TRAMP connection.
7173 (defun tramp-set-connection-property
7174 (property value multi-method method user host)
7175 "Set the named property of a TRAMP connection."
7176 (tramp-maybe-open-connection multi-method method user host)
7177 (with-current-buffer (tramp-get-buffer multi-method method user host)
7178 (set (make-local-variable
7179 (intern (concat "tramp-connection-property-" property)))
7182 ;; Some predefined connection properties.
7183 (defun tramp-set-remote-encoding (multi-method method user host rem-enc)
7184 (tramp-set-connection-property "remote-encoding" rem-enc
7185 multi-method method user host))
7186 (defun tramp-get-remote-encoding (multi-method method user host)
7187 (tramp-get-connection-property "remote-encoding" nil
7188 multi-method method user host))
7190 (defun tramp-set-remote-decoding (multi-method method user host rem-dec)
7191 (tramp-set-connection-property "remote-decoding" rem-dec
7192 multi-method method user host))
7193 (defun tramp-get-remote-decoding (multi-method method user host)
7194 (tramp-get-connection-property "remote-decoding" nil
7195 multi-method method user host))
7197 (defun tramp-set-local-encoding (multi-method method user host loc-enc)
7198 (tramp-set-connection-property "local-encoding" loc-enc
7199 multi-method method user host))
7200 (defun tramp-get-local-encoding (multi-method method user host)
7201 (tramp-get-connection-property "local-encoding" nil
7202 multi-method method user host))
7204 (defun tramp-set-local-decoding (multi-method method user host loc-dec)
7205 (tramp-set-connection-property "local-decoding" loc-dec
7206 multi-method method user host))
7207 (defun tramp-get-local-decoding (multi-method method user host)
7208 (tramp-get-connection-property "local-decoding" nil
7209 multi-method method user host))
7211 (defun tramp-get-method-parameter (multi-method method user host param)
7212 "Return the method parameter PARAM.
7213 If the `tramp-methods' entry does not exist, use the variable PARAM
7215 (unless (boundp param)
7216 (error "Non-existing method parameter `%s'" param))
7217 (let ((entry (assoc param
7218 (assoc (tramp-find-method multi-method method user host)
7222 (symbol-value param))))
7225 ;; Auto saving to a special directory.
7227 (defun tramp-exists-file-name-handler (operation &rest args)
7228 (let ((buffer-file-name "/")
7229 (fnha file-name-handler-alist)
7230 (check-file-name-operation operation)
7231 (file-name-handler-alist
7234 '(lambda (operation &rest args)
7235 "Returns OPERATION if it is the one to be checked"
7236 (if (equal check-file-name-operation operation)
7238 (let ((file-name-handler-alist fnha))
7239 (apply operation args))))))))
7240 (eq (apply operation args) operation)))
7242 (unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
7243 (defadvice make-auto-save-file-name
7244 (around tramp-advice-make-auto-save-file-name () activate)
7245 "Invoke `tramp-handle-make-auto-save-file-name' for tramp files."
7246 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name)))
7247 (setq ad-return-value (tramp-handle-make-auto-save-file-name))
7249 (add-hook 'tramp-unload-hook
7250 '(lambda () (ad-unadvise 'make-auto-save-file-name))))
7252 ;; In Emacs < 22 and XEmacs < 21.5 autosaved remote files have
7253 ;; permission 0666 minus umask. This is a security threat.
7255 (defun tramp-set-auto-save-file-modes ()
7256 "Set permissions of autosaved remote files to the original permissions."
7257 (let ((bfn (buffer-file-name)))
7258 (when (and (stringp bfn)
7259 (tramp-tramp-file-p bfn)
7260 (stringp buffer-auto-save-file-name)
7261 (not (equal bfn buffer-auto-save-file-name)))
7262 (unless (file-exists-p buffer-auto-save-file-name)
7263 (write-region "" nil buffer-auto-save-file-name))
7264 ;; Permissions should be set always, because there might be an old
7265 ;; auto-saved file belonging to another original file. This could
7266 ;; be a security threat.
7267 (set-file-modes buffer-auto-save-file-name
7268 (or (file-modes bfn) (tramp-octal-to-decimal "0600"))))))
7270 (unless (or (> emacs-major-version 21)
7271 (and (featurep 'xemacs)
7272 (= emacs-major-version 21)
7273 (> emacs-minor-version 4)))
7274 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes)
7275 (add-hook 'tramp-unload-hook
7277 (remove-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))))
7279 (defun tramp-subst-strs-in-string (alist string)
7280 "Replace all occurrences of the string FROM with TO in STRING.
7281 ALIST is of the form ((FROM . TO) ...)."
7284 (let* ((pr (car alist))
7287 (while (string-match (regexp-quote from) string)
7288 (setq string (replace-match to t t string)))
7289 (setq alist (cdr alist))))
7292 (defun tramp-insert-with-face (face string)
7293 "Insert text with a specific face."
7294 (let ((start (point)))
7296 (add-text-properties start (point) (list 'face face))))
7298 ;; ------------------------------------------------------------
7299 ;; -- Compatibility functions section --
7300 ;; ------------------------------------------------------------
7302 (defun tramp-temporary-file-directory ()
7303 "Return name of directory for temporary files (compat function).
7304 For Emacs, this is the variable `temporary-file-directory', for XEmacs
7305 this is the function `temp-directory'."
7306 (cond ((boundp 'temporary-file-directory)
7307 (symbol-value 'temporary-file-directory))
7308 ((fboundp 'temp-directory)
7309 (funcall (symbol-function 'temp-directory))) ;pacify byte-compiler
7310 ((let ((d (getenv "TEMP"))) (and d (file-directory-p d)))
7311 (file-name-as-directory (getenv "TEMP")))
7312 ((let ((d (getenv "TMP"))) (and d (file-directory-p d)))
7313 (file-name-as-directory (getenv "TMP")))
7314 ((let ((d (getenv "TMPDIR"))) (and d (file-directory-p d)))
7315 (file-name-as-directory (getenv "TMPDIR")))
7316 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
7317 (t (message (concat "Neither `temporary-file-directory' nor "
7318 "`temp-directory' is defined -- using /tmp."))
7319 (file-name-as-directory "/tmp"))))
7321 (defun tramp-read-passwd (user host prompt)
7322 "Read a password from user (compat function).
7323 Invokes `password-read' if available, `read-passwd' else."
7324 (if (functionp 'password-read)
7325 (let* ((key (concat (or user (user-login-name)) "@" host))
7326 (password (apply #'password-read (list prompt key))))
7327 (apply #'password-cache-add (list key password))
7329 (read-passwd prompt)))
7331 (defun tramp-clear-passwd (&optional user host)
7332 "Clear password cache for connection related to current-buffer."
7334 (let ((filename (or buffer-file-name list-buffers-directory "")))
7335 (when (and (functionp 'password-cache-remove)
7336 (or (and user host) (tramp-tramp-file-p filename)))
7337 (let* ((v (when (tramp-tramp-file-p filename)
7338 (tramp-dissect-file-name filename)))
7339 (luser (or user (tramp-file-name-user v) (user-login-name)))
7340 (lhost (or host (tramp-file-name-host v) (system-name)))
7341 (key (concat luser "@" lhost)))
7342 (apply #'password-cache-remove (list key))))))
7344 (defun tramp-time-diff (t1 t2)
7345 "Return the difference between the two times, in seconds.
7346 T1 and T2 are time values (as returned by `current-time' for example)."
7347 ;; Pacify byte-compiler with `symbol-function'.
7348 (cond ((and (fboundp 'subtract-time)
7349 (fboundp 'float-time))
7350 (funcall (symbol-function 'float-time)
7351 (funcall (symbol-function 'subtract-time) t1 t2)))
7352 ((and (fboundp 'subtract-time)
7353 (fboundp 'time-to-seconds))
7354 (funcall (symbol-function 'time-to-seconds)
7355 (funcall (symbol-function 'subtract-time) t1 t2)))
7356 ((fboundp 'itimer-time-difference)
7357 (funcall (symbol-function 'itimer-time-difference)
7358 (if (< (length t1) 3) (append t1 '(0)) t1)
7359 (if (< (length t2) 3) (append t2 '(0)) t2)))
7361 ;; snarfed from Emacs 21 time-date.el; combining
7362 ;; time-to-seconds and subtract-time
7363 (let ((time (let ((borrow (< (cadr t1) (cadr t2))))
7364 (list (- (car t1) (car t2) (if borrow 1 0))
7365 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2))))))
7366 (+ (* (car time) 65536.0)
7368 (/ (or (nth 2 time) 0) 1000000.0))))))
7370 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
7371 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
7372 EOL-TYPE can be one of `dos', `unix', or `mac'."
7373 (cond ((fboundp 'coding-system-change-eol-conversion)
7374 (apply #'coding-system-change-eol-conversion
7375 (list coding-system eol-type)))
7376 ((fboundp 'subsidiary-coding-system)
7378 #'subsidiary-coding-system
7380 (cond ((eq eol-type 'dos) 'crlf)
7381 ((eq eol-type 'unix) 'lf)
7382 ((eq eol-type 'mac) 'cr)
7384 (error "Unknown EOL-TYPE `%s', must be %s"
7386 "`dos', `unix', or `mac'"))))))
7387 (t (error "Can't change EOL conversion -- is MULE missing?"))))
7389 (defun tramp-split-string (string pattern)
7390 "Like `split-string' but omit empty strings.
7391 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
7392 This is, the first, empty, element is omitted. In XEmacs, the first
7393 element is not omitted.
7395 Note: this function has been written for `tramp-handle-file-truename'.
7396 If you want to use it for something else, you'll have to check whether
7397 it does the right thing."
7398 (delete "" (split-string string pattern)))
7400 (defun tramp-set-process-query-on-exit-flag (process flag)
7401 "Specify if query is needed for process when Emacs is exited.
7402 If the second argument flag is non-nil, Emacs will query the user before
7403 exiting if process is running."
7405 (if (fboundp 'set-process-query-on-exit-flag)
7406 (symbol-function 'set-process-query-on-exit-flag)
7407 (symbol-function 'process-kill-without-query))
7411 ;; ------------------------------------------------------------
7412 ;; -- Kludges section --
7413 ;; ------------------------------------------------------------
7415 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
7416 ;; does not deal well with newline characters. Newline is replaced by
7417 ;; backslash newline. But if, say, the string `a backslash newline b'
7418 ;; is passed to a shell, the shell will expand this into "ab",
7419 ;; completely omitting the newline. This is not what was intended.
7420 ;; It does not appear to be possible to make the function
7421 ;; `shell-quote-argument' work with newlines without making it
7422 ;; dependent on the shell used. But within this package, we know that
7423 ;; we will always use a Bourne-like shell, so we use an approach which
7426 ;; The approach is simple: we call `shell-quote-argument', then
7427 ;; massage the newline part of the result.
7429 ;; This function should produce a string which is grokked by a Unix
7430 ;; shell, even if the Emacs is running on Windows. Since this is the
7431 ;; kludges section, we bind `system-type' in such a way that
7432 ;; `shell-quote-arguments' behaves as if on Unix.
7434 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
7435 ;; function to work with Bourne-like shells.
7437 ;; CCC: This function should be rewritten so that
7438 ;; `shell-quote-argument' is not used. This way, we are safe from
7439 ;; changes in `shell-quote-argument'.
7440 (defun tramp-shell-quote-argument (s)
7441 "Similar to `shell-quote-argument', but groks newlines.
7442 Only works for Bourne-like shells."
7443 (let ((system-type 'not-windows))
7445 (let ((result (shell-quote-argument s))
7446 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
7447 (when (and (>= (length result) 2)
7448 (string= (substring result 0 2) "\\~"))
7449 (setq result (substring result 1)))
7450 (while (string-match nl result)
7451 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
7455 ;; ;; EFS hooks itself into the file name handling stuff in more places
7456 ;; ;; than just `file-name-handler-alist'. The following tells EFS to stay
7457 ;; ;; away from tramp.el file names.
7459 ;; ;; This is needed because EFS installs (efs-dired-before-readin) into
7460 ;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP
7461 ;; ;; connection to help it's dired process. Not that I have any real
7462 ;; ;; idea *why* this is helpful to dired.
7464 ;; ;; Anyway, this advice fixes the problem (with a sledgehammer :)
7466 ;; ;; Daniel Pittman <daniel@danann.net>
7468 ;; ;; CCC: when the other defadvice calls have disappeared, make sure
7469 ;; ;; not to call defadvice unless it's necessary. How do we find out whether
7470 ;; ;; it is necessary? (featurep 'efs) is surely the wrong way --
7471 ;; ;; EFS might nicht be loaded yet.
7472 ;; (defadvice efs-ftp-path (around dont-match-tramp-localname activate protect)
7473 ;; "Cause efs-ftp-path to fail when the path is a TRAMP localname."
7474 ;; (if (tramp-tramp-file-p (ad-get-arg 0))
7478 ;; We currently (sometimes) use "[" and "]" in the filename format.
7479 ;; This means that Emacs wants to expand wildcards if
7480 ;; `find-file-wildcards' is non-nil, and then barfs because no
7481 ;; expansion could be found. We detect this situation and do
7482 ;; something really awful: we have `file-expand-wildcards' return the
7483 ;; original filename if it can't expand anything. Let's just hope
7484 ;; that this doesn't break anything else.
7485 ;; CCC: This check is now also really awful; we should search all
7486 ;; of the filename format, not just the prefix.
7487 (when (string-match "\\[" tramp-prefix-format)
7488 (defadvice file-expand-wildcards (around tramp-fix activate)
7489 (let ((name (ad-get-arg 0)))
7490 (if (tramp-tramp-file-p name)
7491 ;; If it's a Tramp file, dissect it and look if wildcards
7492 ;; need to be expanded at all.
7493 (let ((v (tramp-dissect-file-name name)))
7494 (if (string-match "[[*?]" (tramp-file-name-localname v))
7495 (let ((res ad-do-it))
7496 (setq ad-return-value (or res (list name))))
7497 (setq ad-return-value (list name))))
7498 ;; If it is not a Tramp file, just run the original function.
7499 (let ((res ad-do-it))
7500 (setq ad-return-value (or res (list name)))))))
7501 (add-hook 'tramp-unload-hook
7502 '(lambda () (ad-unadvise 'file-expand-wildcards))))
7504 ;; Tramp version is useful in a number of situations.
7506 (defun tramp-version (arg)
7507 "Print version number of tramp.el in minibuffer or current buffer."
7509 (if arg (insert tramp-version) (message tramp-version)))
7511 ;; Make the `reporter` functionality available for making bug reports about
7512 ;; the package. A most useful piece of code.
7514 (unless (fboundp 'reporter-submit-bug-report)
7515 (autoload 'reporter-submit-bug-report "reporter"))
7518 "Submit a bug report to the TRAMP developers."
7522 (let ((reporter-prompt-for-summary-p t))
7523 (reporter-submit-bug-report
7524 tramp-bug-report-address ; to-address
7525 (format "tramp (%s)" tramp-version) ; package name and version
7530 tramp-file-exists-command
7531 tramp-current-multi-method
7532 tramp-current-method
7537 tramp-auto-save-directory ; vars to dump
7538 tramp-default-method
7539 tramp-rsh-end-of-line
7540 tramp-default-password-end-of-line
7542 tramp-login-prompt-regexp
7543 ;; Mask non-7bit characters
7544 (tramp-password-prompt-regexp . tramp-reporter-dump-variable)
7545 tramp-wrong-passwd-regexp
7546 tramp-yesno-prompt-regexp
7547 tramp-yn-prompt-regexp
7548 tramp-terminal-prompt-regexp
7549 tramp-temp-name-prefix
7550 tramp-file-name-structure
7551 tramp-file-name-regexp
7552 tramp-multi-file-name-structure
7553 tramp-multi-file-name-hop-structure
7555 tramp-multi-connection-function-alist
7558 tramp-coding-commands
7559 tramp-actions-before-shell
7560 tramp-actions-copy-out-of-band
7563 ;; Mask non-7bit characters
7564 (tramp-shell-prompt-pattern . tramp-reporter-dump-variable)
7566 ,(when (boundp 'tramp-backup-directory-alist)
7567 'tramp-backup-directory-alist)
7568 ,(when (boundp 'tramp-bkup-backup-directory-info)
7569 'tramp-bkup-backup-directory-info)
7571 ;; Non-tramp variables of interest
7572 ;; Mask non-7bit characters
7573 (shell-prompt-pattern . tramp-reporter-dump-variable)
7575 backup-by-copying-when-linked
7576 backup-by-copying-when-mismatch
7577 ,(when (boundp 'backup-by-copying-when-privileged-mismatch)
7578 'backup-by-copying-when-privileged-mismatch)
7579 ,(when (boundp 'password-cache)
7581 ,(when (boundp 'password-cache-expiry)
7582 'password-cache-expiry)
7583 ,(when (boundp 'backup-directory-alist)
7584 'backup-directory-alist)
7585 ,(when (boundp 'bkup-backup-directory-info)
7586 'bkup-backup-directory-info)
7587 file-name-handler-alist))
7589 'tramp-load-report-modules ; pre-hook
7590 'tramp-append-tramp-buffers ; post-hook
7592 Enter your bug report in this message, including as much detail as you
7593 possibly can about the problem, what you did to cause it and what the
7594 local and remote machines are.
7596 If you can give a simple set of instructions to make this bug happen
7597 reliably, please include those. Thank you for helping kill bugs in
7600 Another useful thing to do is to put (setq tramp-debug-buffer t) in
7601 the ~/.emacs file and to repeat the bug. Then, include the contents
7602 of the *tramp/foo* buffer and the *debug tramp/foo* buffer in your bug
7605 --bug report follows this line--
7608 (defun tramp-reporter-dump-variable (varsym mailbuf)
7609 "Pretty-print the value of the variable in symbol VARSYM.
7610 Used for non-7bit chars in strings."
7611 (let* ((reporter-eval-buffer (symbol-value 'reporter-eval-buffer))
7612 (val (with-current-buffer reporter-eval-buffer
7613 (symbol-value varsym))))
7615 ;; There are characters to be masked.
7616 (when (and (boundp 'mm-7bit-chars)
7618 (concat "[^" (symbol-value 'mm-7bit-chars) "]") val))
7619 (with-current-buffer reporter-eval-buffer
7620 (set varsym (concat "(base64-decode-string \""
7621 (base64-encode-string val)
7625 (funcall (symbol-function 'reporter-dump-variable) varsym mailbuf)
7627 ;; Remove string quotation.
7630 (concat "\\(^.*\\)" "\"" ;; \1 "
7631 "\\((base64-decode-string \\)" "\\\\" ;; \2 \
7632 "\\(\".*\\)" "\\\\" ;; \3 \
7633 "\\(\")\\)" "\"$")) ;; \4 "
7634 (replace-match "\\1\\2\\3\\4")
7636 (insert " ;; variable encoded due to non-printable characters\n"))
7639 ;; Reset VARSYM to old value.
7640 (with-current-buffer reporter-eval-buffer
7643 (defun tramp-load-report-modules ()
7644 "Load needed modules for reporting."
7646 ;; We load message.el and mml.el from Gnus.
7647 (if (featurep 'xemacs)
7649 (load "message" 'noerror)
7650 (load "mml" 'noerror))
7651 (require 'message nil 'noerror)
7652 (require 'mml nil 'noerror))
7653 (when (functionp 'message-mode)
7654 (funcall (symbol-function 'message-mode)))
7655 (when (functionp 'mml-mode)
7656 (funcall (symbol-function 'mml-mode) t)))
7658 (defun tramp-append-tramp-buffers ()
7659 "Append Tramp buffers into the bug report."
7662 (eq major-mode 'message-mode)
7664 (symbol-value 'mml-mode))
7666 (let* ((tramp-buf-regexp "\\*\\(debug \\)?tramp/")
7669 (mapcar '(lambda (b)
7670 (when (string-match tramp-buf-regexp (buffer-name b)) b))
7672 (curbuf (current-buffer)))
7674 ;; There is at least one Tramp buffer.
7676 (switch-to-buffer (list-buffers-noselect nil))
7677 (delete-other-windows)
7678 (setq buffer-read-only nil)
7679 (goto-char (point-min))
7681 (if (re-search-forward tramp-buf-regexp (tramp-point-at-eol) t)
7684 (let ((start (point)))
7686 (kill-region start (point)))))
7688 The buffer(s) above will be appended to this message. If you don't want
7689 to append a buffer because it contains sensible data, or because the buffer
7690 is too large, you should delete the respective buffer. The buffer(s) will
7691 contain user and host names. Passwords will never be included there.")
7693 (when (and tramp-debug-buffer (> tramp-verbose 9))
7695 (let ((start (point)))
7697 Please note that you have set `tramp-verbose' to a value greater than 9.
7698 Therefore, the contents of files might be included in the debug buffer(s).")
7699 (add-text-properties start (point) (list 'face 'italic))))
7701 (set-buffer-modified-p nil)
7702 (setq buffer-read-only t)
7703 (goto-char (point-min))
7705 (if (y-or-n-p "Do you want to append the buffer(s)? ")
7706 ;; OK, let's send. First we delete the buffer list.
7709 (switch-to-buffer curbuf)
7710 (goto-char (point-max))
7712 (dolist (buffer buffer-list)
7713 (funcall (symbol-function 'mml-insert-empty-tag)
7714 'part 'type "text/plain" 'encoding "base64"
7715 'disposition "attachment" 'buffer (buffer-name buffer)
7716 'description (buffer-name buffer)))
7717 (set-buffer-modified-p nil))
7719 ;; Don't send. Delete the message buffer.
7721 (set-buffer-modified-p nil)
7723 (throw 'dont-send nil))))))
7725 (defalias 'tramp-submit-bug 'tramp-bug)
7727 ;; Checklist for `tramp-unload-hook'
7728 ;; - Unload all `tramp-*' packages
7729 ;; - Reset `file-name-handler-alist'
7730 ;; - Cleanup hooks where Tramp functions are in
7731 ;; - Cleanup advised functions
7732 ;; - Cleanup autoloads
7734 (defun tramp-unload-tramp ()
7735 "Discard Tramp from loading remote files."
7737 ;; When Tramp is not loaded yet, its autoloads are still active.
7738 (tramp-unload-file-name-handlers)
7739 ;; ange-ftp settings must be enabled.
7740 (when (functionp 'tramp-ftp-enable-ange-ftp)
7741 (funcall (symbol-function 'tramp-ftp-enable-ange-ftp)))
7742 ;; `tramp-util' unloads also `tramp'.
7743 (condition-case nil ;; maybe its not loaded yet.
7744 (unload-feature (if (featurep 'tramp-util) 'tramp-util 'tramp) 'force)
7749 ;; Make sure that we get integration with the VC package.
7750 ;; When it is loaded, we need to pull in the integration module.
7751 ;; This must come after (provide 'tramp) because tramp-vc.el
7753 (eval-after-load "vc"
7756 (add-hook 'tramp-unload-hook
7758 (when (featurep 'tramp-vc)
7759 (unload-feature 'tramp-vc 'force))))))
7763 ;; * Allow putting passwords in the filename.
7764 ;; This should be implemented via a general mechanism to add
7765 ;; parameters in filenames. There is currently a kludge for
7766 ;; putting the port number into the filename for ssh and ftp
7767 ;; files. This could be subsumed by the new mechanism as well.
7768 ;; Another approach is to read a netrc file like ~/.authinfo
7770 ;; * Handle nonlocal exits such as C-g.
7771 ;; * Autodetect if remote `ls' groks the "--dired" switch.
7772 ;; * Add fallback for inline encodings. This should be used
7773 ;; if the remote end doesn't support mimencode or a similar program.
7774 ;; For reading files from the remote host, we can just parse the output
7775 ;; of `od -b'. For writing files to the remote host, we construct
7776 ;; a shell program which contains only "safe" ascii characters
7777 ;; and which writes the right bytes to the file. We can use printf(1)
7778 ;; or "echo -e" or the printf function in awk and use octal escapes
7779 ;; for the "dangerous" characters. The null byte might be a problem.
7780 ;; On some systems, the octal escape doesn't work. So we try the following
7781 ;; two commands to write a null byte:
7782 ;; dd if=/dev/zero bs=1 count=1
7783 ;; echo | tr '\n' '\000'
7784 ;; * Separate local `tramp-coding-commands' from remote ones. Connect
7785 ;; the two via a format which can be `uu' or `b64'. Then we can search
7786 ;; for the right local commands and the right remote commands separately.
7787 ;; * Cooperate with PCL-CVS. It uses start-process, which doesn't
7788 ;; work for remote files.
7789 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
7790 ;; `shell-quote-argument'.
7791 ;; * Completion gets confused when you leave out the method name.
7792 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
7793 ;; by the files in that directory. Add this here.
7794 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
7795 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
7796 ;; * When logging in, keep looking for questions according to an alist
7797 ;; and then invoke the right function.
7798 ;; * Case-insensitive filename completion. (Norbert Goevert.)
7799 ;; * Running CVS remotely doesn't appear to work right. It thinks
7800 ;; files are locked by somebody else even if I'm the locking user.
7801 ;; Sometimes, one gets `No CVSROOT specified' errors from CVS.
7803 ;; * Don't use globbing for directories with many files, as this is
7804 ;; likely to produce long command lines, and some shells choke on
7805 ;; long command lines.
7806 ;; * Find out about the new auto-save mechanism in Emacs 21 and
7807 ;; do the right thing.
7808 ;; * `vc-directory' does not work. It never displays any files, even
7809 ;; if it does show files when run locally.
7810 ;; * Allow correction of passwords, if the remote end allows this.
7811 ;; (Mark Hershberger)
7812 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
7813 ;; * Do asynchronous `shell-command's.
7814 ;; * Grok `append' parameter for `write-region'.
7815 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
7816 ;; * abbreviate-file-name
7817 ;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>)
7818 ;; * Also allow to omit user names when doing multi-hop. Not sure yet
7819 ;; what the user names should default to, though.
7820 ;; * better error checking. At least whenever we see something
7821 ;; strange when doing zerop, we should kill the process and start
7822 ;; again. (Greg Stark)
7823 ;; * Add caching for filename completion. (Greg Stark)
7824 ;; Of course, this has issues with usability (stale cache bites)
7825 ;; -- <daniel@danann.net>
7826 ;; * Provide a local cache of old versions of remote files for the rsync
7827 ;; transfer method to use. (Greg Stark)
7828 ;; * Remove unneeded parameters from methods.
7829 ;; * Invoke rsync once for copying a whole directory hierarchy.
7830 ;; (Francesco Potort
\e,Al
\e(B)
7831 ;; * Should we set PATH ourselves or should we rely on the remote end
7833 ;; * Make it work for XEmacs 20, which is missing `with-timeout'.
7834 ;; * Make it work for different encodings, and for different file name
7835 ;; encodings, too. (Daniel Pittman)
7836 ;; * Change applicable functions to pass a struct tramp-file-name rather
7837 ;; than the individual items MULTI-METHOD, METHOD, USER, HOST, LOCALNAME.
7838 ;; * Implement asynchronous shell commands.
7839 ;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
7840 ;; * Progress reports while copying files. (Michael Kifer)
7841 ;; * `Smart' connection method that uses inline for small and out of
7842 ;; band for large files. (Michael Kifer)
7843 ;; * Don't search for perl5 and perl. Instead, only search for perl and
7844 ;; then look if it's the right version (with `perl -v').
7845 ;; * When editing a remote CVS controlled file as a different user, VC
7846 ;; gets confused about the file locking status. Try to find out why
7847 ;; the workaround doesn't work.
7848 ;; * Change `copy-file' to grok the case where the filename handler
7849 ;; for the source and the target file are different. Right now,
7850 ;; it looks at the source file and then calls that handler, if
7851 ;; there is one. But since ange-ftp, for instance, does not know
7852 ;; about Tramp, it does not do the right thing if the target file
7853 ;; name is a Tramp name.
7854 ;; * Username and hostname completion.
7855 ;; ** If `partial-completion-mode' isn't loaded, "/foo:bla" tries to
7856 ;; connect to host "blabla" already if that host is unique. No idea
7857 ;; how to suppress. Maybe not an essential problem.
7858 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode'.
7859 ;; ** Extend `tramp-get-completion-su' for NIS and shadow passwords.
7860 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
7861 ;; Code is nearly identical.
7862 ;; ** Decide whiche files to take for searching user/host names depending on
7863 ;; operating system (windows-nt) in `tramp-completion-function-alist'.
7864 ;; ** Enhance variables for debug.
7865 ;; ** Implement "/multi:" completion.
7866 ;; ** Add a learning mode for completion. Make results persistent.
7867 ;; * Allow out-of-band methods as _last_ multi-hop.
7869 ;; Functions for file-name-handler-alist:
7870 ;; diff-latest-backup-file -- in diff.el
7871 ;; dired-uncache -- this will be needed when we do insert-directory caching
7872 ;; file-name-as-directory -- use primitive?
7873 ;; file-name-sans-versions -- use primitive?
7874 ;; get-file-buffer -- use primitive
7877 ;;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
7878 ;;; tramp.el ends here