1 ;;; -*- mode: Emacs-Lisp; coding: iso-2022-7bit; -*-
2 ;;; tramp.el --- Transparent Remote Access, Multiple Protocol
4 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
5 ;; 2005, 2006 Free Software Foundation, Inc.
7 ;; Author: Kai Gro\e,A_\e(Bjohann <kai.grossjohann@gmx.net>
8 ;; Michael Albinus <michael.albinus@gmx.de>
9 ;; Keywords: comm, processes
11 ;; This file is part of GNU Emacs.
13 ;; GNU Emacs is free software; you can redistribute it and/or modify
14 ;; it under the terms of the GNU General Public License as published by
15 ;; the Free Software Foundation; either version 2, or (at your option)
18 ;; GNU Emacs is distributed in the hope that it will be useful,
19 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
20 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
21 ;; GNU General Public License for more details.
23 ;; You should have received a copy of the GNU General Public License
24 ;; along with GNU Emacs; see the file COPYING. If not, write to the
25 ;; Free Software Foundation, Inc., 51 Franklin Street, Fifth Floor,
26 ;; Boston, MA 02110-1301, USA.
30 ;; This package provides remote file editing, similar to ange-ftp.
31 ;; The difference is that ange-ftp uses FTP to transfer files between
32 ;; the local and the remote host, whereas tramp.el uses a combination
33 ;; of rsh and rcp or other work-alike programs, such as ssh/scp.
35 ;; For more detailed instructions, please see the info file.
40 ;; This package only works for Emacs 20 and higher, and for XEmacs 21
41 ;; and higher. (XEmacs 20 is missing the `with-timeout' macro. Emacs
42 ;; 19 is reported to have other problems. For XEmacs 21, you need the
43 ;; package `fsf-compat' for the `with-timeout' macro.)
45 ;; This version might not work with pre-Emacs 21 VC unless VC is
46 ;; loaded before tramp.el. Could you please test this and tell me about
47 ;; the result? Thanks.
49 ;; Also see the todo list at the bottom of this file.
51 ;; The current version of Tramp can be retrieved from the following URL:
52 ;; http://ftp.gnu.org/gnu/tramp/
54 ;; There's a mailing list for this, as well. Its name is:
55 ;; tramp-devel@gnu.org
56 ;; You can use the Web to subscribe, under the following URL:
57 ;; http://lists.gnu.org/mailman/listinfo/tramp-devel
59 ;; For the adventurous, the current development sources are available
60 ;; via CVS. You can find instructions about this at the following URL:
61 ;; http://savannah.gnu.org/projects/tramp/
62 ;; Click on "CVS" in the navigation bar near the top.
64 ;; Don't forget to put on your asbestos longjohns, first!
68 ;; The Tramp version number and bug report address, as prepared by configure.
70 (add-hook 'tramp-unload-hook
72 (when (featurep 'trampver
)
73 (unload-feature 'trampver
'force
))))
75 (if (featurep 'xemacs
)
76 (require 'timer-funcs
)
79 (require 'format-spec
) ;from Gnus 5.8, also in tar ball
80 ;; As long as password.el is not part of (X)Emacs, it shouldn't
82 (if (featurep 'xemacs
)
83 (load "password" 'noerror
)
84 (require 'password nil
'noerror
)) ;from No Gnus, also in tar ball
86 ;; The explicit check is not necessary in Emacs, which provides the
87 ;; feature even if implemented in C, but it appears to be necessary
89 (unless (and (fboundp 'base64-encode-region
)
90 (fboundp 'base64-decode-region
))
91 (require 'base64
)) ;for the mimencode methods
95 (autoload 'tramp-uuencode-region
"tramp-uu"
96 "Implementation of `uuencode' in Lisp.")
97 (add-hook 'tramp-unload-hook
99 (when (featurep 'tramp-uu
)
100 (unload-feature 'tramp-uu
'force
))))
102 (unless (fboundp 'uudecode-decode-region
)
103 (autoload 'uudecode-decode-region
"uudecode"))
105 ;; XEmacs is distributed with few Lisp packages. Further packages are
106 ;; installed using EFS. If we use a unified filename format, then
107 ;; Tramp is required in addition to EFS. (But why can't Tramp just
108 ;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
109 ;; just like before.) Another reason for using a separate filename
110 ;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
111 ;; Tramp only knows how to deal with `file-name-handler-alist', not
114 (defvar tramp-unified-filenames
(not (featurep 'xemacs
))
115 "Non-nil means to use unified Ange-FTP/Tramp filename syntax.
116 Otherwise, use a separate filename syntax for Tramp.")
118 ;; Load foreign methods. Because they do require Tramp internally, this
119 ;; must be done with the `eval-after-load' trick.
121 ;; tramp-ftp supports Ange-FTP only. Not suited for XEmacs therefore.
122 (unless (featurep 'xemacs
)
123 (eval-after-load "tramp"
126 (add-hook 'tramp-unload-hook
128 (when (featurep 'tramp-ftp
)
129 (unload-feature 'tramp-ftp
'force
)))))))
130 (when (and tramp-unified-filenames
(featurep 'xemacs
))
131 (eval-after-load "tramp"
134 (add-hook 'tramp-unload-hook
136 (when (featurep 'tramp-efs
)
137 (unload-feature 'tramp-efs
'force
)))))))
139 ;; tramp-smb uses "smbclient" from Samba.
140 ;; Not available under Cygwin and Windows, because they don't offer
141 ;; "smbclient". And even not necessary there, because Emacs supports
142 ;; UNC file names like "//host/share/localname".
143 (unless (memq system-type
'(cygwin windows-nt
))
144 (eval-after-load "tramp"
147 (add-hook 'tramp-unload-hook
149 (when (featurep 'tramp-smb
)
150 (unload-feature 'tramp-smb
'force
)))))))
154 ;; Emacs 19.34 compatibility hack -- is this needed?
155 (or (>= emacs-major-version
20)
158 (unless (boundp 'custom-print-functions
)
159 (defvar custom-print-functions nil
)) ; not autoloaded before Emacs 20.4
161 ;; Avoid byte-compiler warnings if the byte-compiler supports this.
162 ;; Currently, XEmacs supports this.
164 (when (featurep 'xemacs
)
165 (let (unused-vars) ; Pacify Emacs byte-compiler
166 (defalias 'warnings
'identity
) ; Pacify Emacs byte-compiler
167 (byte-compiler-options (warnings (- unused-vars
))))))
169 ;; `directory-sep-char' is an obsolete variable in Emacs. But it is
170 ;; used in XEmacs, so we set it here and there. The following is needed
171 ;; to pacify Emacs byte-compiler.
173 (when (boundp 'byte-compile-not-obsolete-var
)
174 (setq byte-compile-not-obsolete-var
'directory-sep-char
)))
176 ;;; User Customizable Internal Variables:
179 "Edit remote files with a combination of rsh and rcp or similar programs."
183 (defcustom tramp-verbose
9
184 "*Verbosity level for tramp.el. 0 means be silent, 10 is most verbose."
188 (defcustom tramp-debug-buffer nil
189 "*Whether to send all commands and responses to a debug buffer."
195 (when (boundp 'backup-directory-alist
)
196 (defcustom tramp-backup-directory-alist nil
197 "Alist of filename patterns and backup directory names.
198 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
199 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
200 is a local file name, the backup directory is prepended with Tramp file
201 name prefix \(multi-method, method, user, host\) of file.
203 \(setq tramp-backup-directory-alist backup-directory-alist\)
205 gives the same backup policy for Tramp files on their hosts like the
206 policy for local files."
208 :type
'(repeat (cons (regexp :tag
"Regexp matching filename")
209 (directory :tag
"Backup directory name"))))))
211 ;; XEmacs case. We cannot check for `bkup-backup-directory-info', because
212 ;; the package "backup-dir" might not be loaded yet.
214 (when (featurep 'xemacs
)
215 (defcustom tramp-bkup-backup-directory-info nil
216 "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
217 It has the same meaning like `bkup-backup-directory-info' from package
218 `backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local
219 file name, the backup directory is prepended with Tramp file name prefix
220 \(multi-method, method, user, host\) of file.
222 \(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\)
224 gives the same backup policy for Tramp files on their hosts like the
225 policy for local files."
227 (list (regexp :tag
"File regexp")
228 (string :tag
"Backup Dir")
233 (const search-upward
))))
236 (defcustom tramp-auto-save-directory nil
237 "*Put auto-save files in this directory, if set.
238 The idea is to use a local directory so that auto-saving is faster."
240 :type
'(choice (const nil
)
243 (defcustom tramp-encoding-shell
244 (if (memq system-type
'(windows-nt))
247 "*Use this program for encoding and decoding commands on the local host.
248 This shell is used to execute the encoding and decoding command on the
249 local host, so if you want to use `~' in those commands, you should
250 choose a shell here which groks tilde expansion. `/bin/sh' normally
251 does not understand tilde expansion.
253 For encoding and deocding, commands like the following are executed:
255 /bin/sh -c COMMAND < INPUT > OUTPUT
257 This variable can be used to change the \"/bin/sh\" part. See the
258 variable `tramp-encoding-command-switch' for the \"-c\" part. Also, see the
259 variable `tramp-encoding-reads-stdin' to specify whether the commands read
260 standard input or a file.
262 Note that this variable is not used for remote commands. There are
263 mechanisms in tramp.el which automatically determine the right shell to
264 use for the remote host."
266 :type
'(file :must-match t
))
268 (defcustom tramp-encoding-command-switch
269 (if (string-match "cmd\\.exe" tramp-encoding-shell
)
272 "*Use this switch together with `tramp-encoding-shell' for local commands.
273 See the variable `tramp-encoding-shell' for more information."
277 (defcustom tramp-encoding-reads-stdin t
278 "*If non-nil, encoding commands read from standard input.
279 If nil, the filename is the last argument.
281 Note that the commands always must write to standard output."
285 (defcustom tramp-multi-sh-program
287 "*Use this program for bootstrapping multi-hop connections.
288 This variable is similar to `tramp-encoding-shell', but it is only used
289 when initializing a multi-hop connection. Therefore, the set of
290 commands sent to this shell is quite restricted, and if you are
291 careful it works to use CMD.EXE under Windows (instead of a Bourne-ish
292 shell which does not normally exist on Windows anyway).
294 To use multi-hop methods from Windows, you also need suitable entries
295 in `tramp-multi-connection-function-alist' for the first hop.
297 This variable defaults to the value of `tramp-encoding-shell'."
299 :type
'(file :must-match t
))
301 ;; CCC I have changed all occurrences of comint-quote-filename with
302 ;; tramp-shell-quote-argument, except in tramp-handle-expand-many-files.
303 ;; There, comint-quote-filename was removed altogether. If it turns
304 ;; out to be necessary there, something will need to be done.
305 ;;-(defcustom tramp-file-name-quote-list
306 ;;- '(?] ?[ ?\| ?& ?< ?> ?\( ?\) ?\; ?\ ?\* ?\? ?\! ?\" ?\' ?\` ?# ?\@ ?\+ )
307 ;;- "*Protect these characters from the remote shell.
308 ;;-Any character in this list is quoted (preceded with a backslash)
309 ;;-because it means something special to the shell. This takes effect
310 ;;-when sending file and directory names to the remote shell.
312 ;;-See `comint-file-name-quote-list' for details."
314 ;;- :type '(repeat character))
316 (defcustom tramp-methods
317 '( ("rcp" (tramp-connection-function tramp-open-connection-rsh
)
318 (tramp-login-program "rsh")
319 (tramp-copy-program "rcp")
320 (tramp-remote-sh "/bin/sh")
321 (tramp-login-args nil
)
322 (tramp-copy-args nil
)
323 (tramp-copy-keep-date-arg "-p")
324 (tramp-password-end-of-line nil
))
325 ("scp" (tramp-connection-function tramp-open-connection-rsh
)
326 (tramp-login-program "ssh")
327 (tramp-copy-program "scp")
328 (tramp-remote-sh "/bin/sh")
329 (tramp-login-args ("-e" "none"))
330 (tramp-copy-args nil
)
331 (tramp-copy-keep-date-arg "-p")
332 (tramp-password-end-of-line nil
))
333 ("scp1" (tramp-connection-function tramp-open-connection-rsh
)
334 (tramp-login-program "ssh")
335 (tramp-copy-program "scp")
336 (tramp-remote-sh "/bin/sh")
337 (tramp-login-args ("-1" "-e" "none"))
338 (tramp-copy-args ("-1"))
339 (tramp-copy-keep-date-arg "-p")
340 (tramp-password-end-of-line nil
))
341 ("scp2" (tramp-connection-function tramp-open-connection-rsh
)
342 (tramp-login-program "ssh")
343 (tramp-copy-program "scp")
344 (tramp-remote-sh "/bin/sh")
345 (tramp-login-args ("-2" "-e" "none"))
346 (tramp-copy-args ("-2"))
347 (tramp-copy-keep-date-arg "-p")
348 (tramp-password-end-of-line nil
))
350 (tramp-connection-function tramp-open-connection-rsh
)
351 (tramp-login-program "ssh1")
352 (tramp-copy-program "scp1")
353 (tramp-remote-sh "/bin/sh")
354 (tramp-login-args ("-e" "none"))
355 (tramp-copy-args nil
)
356 (tramp-copy-keep-date-arg "-p")
357 (tramp-password-end-of-line nil
))
359 (tramp-connection-function tramp-open-connection-rsh
)
360 (tramp-login-program "ssh2")
361 (tramp-copy-program "scp2")
362 (tramp-remote-sh "/bin/sh")
363 (tramp-login-args ("-e" "none"))
364 (tramp-copy-args nil
)
365 (tramp-copy-keep-date-arg "-p")
366 (tramp-password-end-of-line nil
))
367 ("rsync" (tramp-connection-function tramp-open-connection-rsh
)
368 (tramp-login-program "ssh")
369 (tramp-copy-program "rsync")
370 (tramp-remote-sh "/bin/sh")
371 (tramp-login-args ("-e" "none"))
372 (tramp-copy-args ("-e" "ssh"))
373 (tramp-copy-keep-date-arg "-t")
374 (tramp-password-end-of-line nil
))
375 ("remcp" (tramp-connection-function tramp-open-connection-rsh
)
376 (tramp-login-program "remsh")
377 (tramp-copy-program "rcp")
378 (tramp-remote-sh "/bin/sh")
379 (tramp-login-args nil
)
380 (tramp-copy-args nil
)
381 (tramp-copy-keep-date-arg "-p")
382 (tramp-password-end-of-line nil
))
383 ("rsh" (tramp-connection-function tramp-open-connection-rsh
)
384 (tramp-login-program "rsh")
385 (tramp-copy-program nil
)
386 (tramp-remote-sh "/bin/sh")
387 (tramp-login-args nil
)
388 (tramp-copy-args nil
)
389 (tramp-copy-keep-date-arg nil
)
390 (tramp-password-end-of-line nil
))
391 ("ssh" (tramp-connection-function tramp-open-connection-rsh
)
392 (tramp-login-program "ssh")
393 (tramp-copy-program nil
)
394 (tramp-remote-sh "/bin/sh")
395 (tramp-login-args ("-e" "none"))
396 (tramp-copy-args nil
)
397 (tramp-copy-keep-date-arg nil
)
398 (tramp-password-end-of-line nil
))
399 ("ssh1" (tramp-connection-function tramp-open-connection-rsh
)
400 (tramp-login-program "ssh")
401 (tramp-copy-program nil
)
402 (tramp-remote-sh "/bin/sh")
403 (tramp-login-args ("-1" "-e" "none"))
404 (tramp-copy-args ("-1"))
405 (tramp-copy-keep-date-arg nil
)
406 (tramp-password-end-of-line nil
))
407 ("ssh2" (tramp-connection-function tramp-open-connection-rsh
)
408 (tramp-login-program "ssh")
409 (tramp-copy-program nil
)
410 (tramp-remote-sh "/bin/sh")
411 (tramp-login-args ("-2" "-e" "none"))
412 (tramp-copy-args ("-2"))
413 (tramp-copy-keep-date-arg nil
)
414 (tramp-password-end-of-line nil
))
416 (tramp-connection-function tramp-open-connection-rsh
)
417 (tramp-login-program "ssh1")
418 (tramp-copy-program nil
)
419 (tramp-remote-sh "/bin/sh")
420 (tramp-login-args ("-e" "none"))
421 (tramp-copy-args nil
)
422 (tramp-copy-keep-date-arg nil
)
423 (tramp-password-end-of-line nil
))
425 (tramp-connection-function tramp-open-connection-rsh
)
426 (tramp-login-program "ssh2")
427 (tramp-copy-program nil
)
428 (tramp-remote-sh "/bin/sh")
429 (tramp-login-args ("-e" "none"))
430 (tramp-copy-args nil
)
431 (tramp-copy-keep-date-arg nil
)
432 (tramp-password-end-of-line nil
))
433 ("remsh" (tramp-connection-function tramp-open-connection-rsh
)
434 (tramp-login-program "remsh")
435 (tramp-copy-program nil
)
436 (tramp-remote-sh "/bin/sh")
437 (tramp-login-args nil
)
438 (tramp-copy-args nil
)
439 (tramp-copy-keep-date-arg nil
)
440 (tramp-password-end-of-line nil
))
442 (tramp-connection-function tramp-open-connection-telnet
)
443 (tramp-login-program "telnet")
444 (tramp-copy-program nil
)
445 (tramp-remote-sh "/bin/sh")
446 (tramp-login-args nil
)
447 (tramp-copy-args nil
)
448 (tramp-copy-keep-date-arg nil
)
449 (tramp-password-end-of-line nil
))
450 ("su" (tramp-connection-function tramp-open-connection-su
)
451 (tramp-login-program "su")
452 (tramp-copy-program nil
)
453 (tramp-remote-sh "/bin/sh")
454 (tramp-login-args ("-" "%u"))
455 (tramp-copy-args nil
)
456 (tramp-copy-keep-date-arg nil
)
457 (tramp-password-end-of-line nil
))
458 ("sudo" (tramp-connection-function tramp-open-connection-su
)
459 (tramp-login-program "sudo")
460 (tramp-copy-program nil
)
461 (tramp-remote-sh "/bin/sh")
462 (tramp-login-args ("-u" "%u" "-s"
464 (tramp-copy-args nil
)
465 (tramp-copy-keep-date-arg nil
)
466 (tramp-password-end-of-line nil
))
467 ("multi" (tramp-connection-function tramp-open-connection-multi
)
468 (tramp-login-program nil
)
469 (tramp-copy-program nil
)
470 (tramp-remote-sh "/bin/sh")
471 (tramp-login-args nil
)
472 (tramp-copy-args nil
)
473 (tramp-copy-keep-date-arg nil
)
474 (tramp-password-end-of-line nil
))
475 ("scpc" (tramp-connection-function tramp-open-connection-rsh
)
476 (tramp-login-program "ssh")
477 (tramp-copy-program "scp")
478 (tramp-remote-sh "/bin/sh")
479 (tramp-login-args ("-o" "ControlPath=%t.%%r@%%h:%%p"
480 "-o" "ControlMaster=yes"
482 (tramp-copy-args ("-o" "ControlPath=%t.%%r@%%h:%%p"
483 "-o" "ControlMaster=auto"))
484 (tramp-copy-keep-date-arg "-p")
485 (tramp-password-end-of-line nil
))
486 ("scpx" (tramp-connection-function tramp-open-connection-rsh
)
487 (tramp-login-program "ssh")
488 (tramp-copy-program "scp")
489 (tramp-remote-sh "/bin/sh")
490 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
491 (tramp-copy-args nil
)
492 (tramp-copy-keep-date-arg "-p")
493 (tramp-password-end-of-line nil
))
494 ("sshx" (tramp-connection-function tramp-open-connection-rsh
)
495 (tramp-login-program "ssh")
496 (tramp-copy-program nil
)
497 (tramp-remote-sh "/bin/sh")
498 (tramp-login-args ("-e" "none" "-t" "-t" "/bin/sh"))
499 (tramp-copy-args nil
)
500 (tramp-copy-keep-date-arg nil
)
501 (tramp-password-end-of-line nil
))
503 (tramp-connection-function tramp-open-connection-rsh
)
504 (tramp-login-program "krlogin")
505 (tramp-copy-program nil
)
506 (tramp-remote-sh "/bin/sh")
507 (tramp-login-args ("-x"))
508 (tramp-copy-args nil
)
509 (tramp-copy-keep-date-arg nil
)
510 (tramp-password-end-of-line nil
))
512 (tramp-connection-function tramp-open-connection-rsh
)
513 (tramp-login-program "plink")
514 (tramp-copy-program nil
)
515 (tramp-remote-sh "/bin/sh")
516 (tramp-login-args ("-ssh")) ;optionally add "-v"
517 (tramp-copy-args nil
)
518 (tramp-copy-keep-date-arg nil
)
519 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
521 (tramp-connection-function tramp-open-connection-rsh
)
522 (tramp-login-program "plink")
523 (tramp-copy-program nil
)
524 (tramp-remote-sh "/bin/sh")
525 (tramp-login-args ("-1" "-ssh")) ;optionally add "-v"
526 (tramp-copy-args nil
)
527 (tramp-copy-keep-date-arg nil
)
528 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
530 (tramp-connection-function tramp-open-connection-rsh
)
531 (tramp-login-program "plink")
532 (tramp-copy-program "pscp")
533 (tramp-remote-sh "/bin/sh")
534 (tramp-login-args ("-ssh"))
535 (tramp-copy-args nil
)
536 (tramp-copy-keep-date-arg "-p")
537 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
539 (tramp-connection-function tramp-open-connection-rsh
)
540 (tramp-login-program "fsh")
541 (tramp-copy-program "fcp")
542 (tramp-remote-sh "/bin/sh -i")
543 (tramp-login-args ("sh" "-i"))
544 (tramp-copy-args nil
)
545 (tramp-copy-keep-date-arg "-p")
546 (tramp-password-end-of-line nil
))
548 "*Alist of methods for remote files.
549 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
550 Each NAME stands for a remote access method. Each PARAM is a
551 pair of the form (KEY VALUE). The following KEYs are defined:
552 * `tramp-connection-function'
553 This specifies the function to use to connect to the remote host.
554 Currently, `tramp-open-connection-rsh', `tramp-open-connection-telnet'
555 and `tramp-open-connection-su' are defined. See the documentation
556 of these functions for more details.
558 This specifies the Bourne shell to use on the remote host. This
559 MUST be a Bourne-like shell. It is normally not necessary to set
560 this to any value other than \"/bin/sh\": tramp wants to use a shell
561 which groks tilde expansion, but it can search for it. Also note
562 that \"/bin/sh\" exists on all Unixen, this might not be true for
563 the value that you decide to use. You Have Been Warned.
564 * `tramp-login-program'
565 This specifies the name of the program to use for logging in to the
566 remote host. Depending on `tramp-connection-function', this may be
567 the name of rsh or a workalike program (when
568 `tramp-connection-function' is `tramp-open-connection-rsh'), or the
569 name of telnet or a workalike (for `tramp-open-connection-telnet'),
570 or the name of su or a workalike (for `tramp-open-connection-su').
572 This specifies the list of arguments to pass to the above
573 mentioned program. Please note that this is a list of arguments,
574 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
575 here. Instead, you want two list elements, one for \"-a\" and one
576 for \"-b\", or one for \"-f\" and one for \"foo\".
577 If `tramp-connection-function' is `tramp-open-connection-su', then
578 \"%u\" in this list is replaced by the user name, and \"%%\" can
579 be used to obtain a literal percent character.
580 \"%t\" is replaced by the temporary file name for `scp'-like methods.
581 * `tramp-copy-program'
582 This specifies the name of the program to use for remotely copying
583 the file; this might be the absolute filename of rcp or the name of
586 This specifies the list of parameters to pass to the above mentioned
587 program, the hints for `tramp-login-args' also apply here.
588 * `tramp-copy-keep-date-arg'
589 This specifies the parameter to use for the copying program when the
590 timestamp of the original file should be kept. For `rcp', use `-p', for
592 * `tramp-password-end-of-line'
593 This specifies the string to use for terminating the line after
594 submitting the password. If this method parameter is nil, then the
595 value of the normal variable `tramp-default-password-end-of-line'
596 is used. This parameter is necessary because the \"plink\" program
597 requires any two characters after sending the password. These do
598 not have to be newline or carriage return characters. Other login
599 programs are happy with just one character, the newline character.
600 We use \"xy\" as the value for methods using \"plink\".
602 What does all this mean? Well, you should specify `tramp-login-program'
603 for all methods; this program is used to log in to the remote site. Then,
604 there are two ways to actually transfer the files between the local and the
605 remote side. One way is using an additional rcp-like program. If you want
606 to do this, set `tramp-copy-program' in the method.
608 Another possibility for file transfer is inline transfer, i.e. the
609 file is passed through the same buffer used by `tramp-login-program'. In
610 this case, the file contents need to be protected since the
611 `tramp-login-program' might use escape codes or the connection might not
612 be eight-bit clean. Therefore, file contents are encoded for transit.
613 See the variable `tramp-coding-commands' for details.
615 So, to summarize: if the method is an out-of-band method, then you
616 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
617 inline method, then these two parameters should be nil. Every method,
618 inline or out of band, must specify `tramp-connection-function' plus
619 the associated arguments (for example, the login program if you chose
620 `tramp-open-connection-telnet').
624 When using `tramp-open-connection-su' the phrase `open connection to a
625 remote host' sounds strange, but it is used nevertheless, for
626 consistency. No connection is opened to a remote host, but `su' is
627 started on the local host. You are not allowed to specify a remote
628 host other than `localhost' or the name of the local host."
632 (set (list (const tramp-connection-function
) function
)
633 (list (const tramp-login-program
)
634 (choice (const nil
) string
))
635 (list (const tramp-copy-program
)
636 (choice (const nil
) string
))
637 (list (const tramp-remote-sh
)
638 (choice (const nil
) string
))
639 (list (const tramp-login-args
) (repeat string
))
640 (list (const tramp-copy-args
) (repeat string
))
641 (list (const tramp-copy-keep-date-arg
)
642 (choice (const nil
) string
))
643 (list (const tramp-encoding-command
)
644 (choice (const nil
) string
))
645 (list (const tramp-decoding-command
)
646 (choice (const nil
) string
))
647 (list (const tramp-encoding-function
)
648 (choice (const nil
) function
))
649 (list (const tramp-decoding-function
)
650 (choice (const nil
) function
))
651 (list (const tramp-password-end-of-line
)
652 (choice (const nil
) string
))))))
654 (defcustom tramp-multi-methods
'("multi" "multiu")
655 "*List of multi-hop methods.
656 Each entry in this list should be a method name as mentioned in the
657 variable `tramp-methods'."
659 :type
'(repeat string
))
661 (defcustom tramp-multi-connection-function-alist
662 '(("telnet" tramp-multi-connect-telnet
"telnet %h%n")
663 ("rsh" tramp-multi-connect-rlogin
"rsh %h -l %u%n")
664 ("remsh" tramp-multi-connect-rlogin
"remsh %h -l %u%n")
665 ("ssh" tramp-multi-connect-rlogin
"ssh %h -l %u%n")
666 ("ssht" tramp-multi-connect-rlogin
"ssh %h -e none -t -t -l %u%n")
667 ("su" tramp-multi-connect-su
"su - %u%n")
668 ("sudo" tramp-multi-connect-su
"sudo -u %u -s -p Password:%n"))
669 "*List of connection functions for multi-hop methods.
670 Each list item is a list of three items (METHOD FUNCTION COMMAND),
671 where METHOD is the name as used in the file name, FUNCTION is the
672 function to be executed, and COMMAND is the shell command used for
675 COMMAND may contain percent escapes. `%u' will be replaced with the
676 user name, `%h' will be replaced with the host name, and `%n' will be
677 replaced with an end-of-line character, as specified in the variable
678 `tramp-rsh-end-of-line'. Use `%%' for a literal percent character.
679 Note that the interpretation of the percent escapes also depends on
680 the FUNCTION. For example, the `%u' escape is forbidden with the
681 function `tramp-multi-connect-telnet'. See the documentation of the
682 various functions for details."
684 :type
'(repeat (list string function string
)))
686 (defcustom tramp-default-method
688 ;; An external copy method seems to be preferred, because it is
689 ;; much more performant for large files, and it hasn't too serious
690 ;; delays for small files. But it must be ensured that there
691 ;; aren't permanent password queries. Either a password agent like
692 ;; "ssh-agent" or "Pageant" shall run, or the optional password.el
693 ;; package shall be active for password caching.
694 ((executable-find "pscp")
695 ;; PuTTY is installed.
696 (if (or (fboundp 'password-read
)
697 ;; Pageant is running.
698 (and (fboundp 'w32-window-exists-p
)
699 (funcall (symbol-function 'w32-window-exists-p
)
700 "Pageant" "Pageant")))
703 ;; There is an ssh installation.
704 ((executable-find "scp")
705 (if (or (fboundp 'password-read
)
706 ;; ssh-agent is running.
707 (getenv "SSH_AUTH_SOCK")
708 (getenv "SSH_AGENT_PID"))
713 "*Default method to use for transferring files.
714 See `tramp-methods' for possibilities.
715 Also see `tramp-default-method-alist'."
719 (defcustom tramp-default-method-alist
720 '(("\\`localhost\\'" "\\`root\\'" "su"))
721 "*Default method to use for specific user/host pairs.
722 This is an alist of items (HOST USER METHOD). The first matching item
723 specifies the method to use for a file name which does not specify a
724 method. HOST and USER are regular expressions or nil, which is
725 interpreted as a regular expression which always matches. If no entry
726 matches, the variable `tramp-default-method' takes effect.
728 If the file name does not specify the user, lookup is done using the
729 empty string for the user name.
731 See `tramp-methods' for a list of possibilities for METHOD."
733 :type
'(repeat (list (regexp :tag
"Host regexp")
734 (regexp :tag
"User regexp")
735 (string :tag
"Method"))))
737 ;; Default values for non-Unices seeked
738 (defconst tramp-completion-function-alist-rsh
739 (unless (memq system-type
'(windows-nt))
740 '((tramp-parse-rhosts "/etc/hosts.equiv")
741 (tramp-parse-rhosts "~/.rhosts")))
742 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
744 ;; Default values for non-Unices seeked
745 (defconst tramp-completion-function-alist-ssh
746 (unless (memq system-type
'(windows-nt))
747 '((tramp-parse-rhosts "/etc/hosts.equiv")
748 (tramp-parse-rhosts "/etc/shosts.equiv")
749 (tramp-parse-shosts "/etc/ssh_known_hosts")
750 (tramp-parse-sconfig "/etc/ssh_config")
751 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
752 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
753 (tramp-parse-rhosts "~/.rhosts")
754 (tramp-parse-rhosts "~/.shosts")
755 (tramp-parse-shosts "~/.ssh/known_hosts")
756 (tramp-parse-sconfig "~/.ssh/config")
757 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
758 (tramp-parse-sknownhosts "~/.ssh2/knownhosts")))
759 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
761 ;; Default values for non-Unices seeked
762 (defconst tramp-completion-function-alist-telnet
763 (unless (memq system-type
'(windows-nt))
764 '((tramp-parse-hosts "/etc/hosts")))
765 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
767 ;; Default values for non-Unices seeked
768 (defconst tramp-completion-function-alist-su
769 (unless (memq system-type
'(windows-nt))
770 '((tramp-parse-passwd "/etc/passwd")))
771 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
773 (defvar tramp-completion-function-alist nil
774 "*Alist of methods for remote files.
775 This is a list of entries of the form (NAME PAIR1 PAIR2 ...).
776 Each NAME stands for a remote access method. Each PAIR is of the form
777 \(FUNCTION FILE). FUNCTION is responsible to extract user names and host
778 names from FILE for completion. The following predefined FUNCTIONs exists:
780 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
781 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
782 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
783 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
784 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
785 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
786 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
787 * `tramp-parse-netrc' for \"~/.netrc\" like files.
789 FUNCTION can also be a customer defined function. For more details see
792 (eval-after-load "tramp"
794 (tramp-set-completion-function
795 "rcp" tramp-completion-function-alist-rsh
)
796 (tramp-set-completion-function
797 "scp" tramp-completion-function-alist-ssh
)
798 (tramp-set-completion-function
799 "scp1" tramp-completion-function-alist-ssh
)
800 (tramp-set-completion-function
801 "scp2" tramp-completion-function-alist-ssh
)
802 (tramp-set-completion-function
803 "scp1_old" tramp-completion-function-alist-ssh
)
804 (tramp-set-completion-function
805 "scp2_old" tramp-completion-function-alist-ssh
)
806 (tramp-set-completion-function
807 "rsync" tramp-completion-function-alist-rsh
)
808 (tramp-set-completion-function
809 "remcp" tramp-completion-function-alist-rsh
)
810 (tramp-set-completion-function
811 "rsh" tramp-completion-function-alist-rsh
)
812 (tramp-set-completion-function
813 "ssh" tramp-completion-function-alist-ssh
)
814 (tramp-set-completion-function
815 "ssh1" tramp-completion-function-alist-ssh
)
816 (tramp-set-completion-function
817 "ssh2" tramp-completion-function-alist-ssh
)
818 (tramp-set-completion-function
819 "ssh1_old" tramp-completion-function-alist-ssh
)
820 (tramp-set-completion-function
821 "ssh2_old" tramp-completion-function-alist-ssh
)
822 (tramp-set-completion-function
823 "remsh" tramp-completion-function-alist-rsh
)
824 (tramp-set-completion-function
825 "telnet" tramp-completion-function-alist-telnet
)
826 (tramp-set-completion-function
827 "su" tramp-completion-function-alist-su
)
828 (tramp-set-completion-function
829 "sudo" tramp-completion-function-alist-su
)
830 (tramp-set-completion-function
832 (tramp-set-completion-function
833 "scpx" tramp-completion-function-alist-ssh
)
834 (tramp-set-completion-function
835 "sshx" tramp-completion-function-alist-ssh
)
836 (tramp-set-completion-function
837 "krlogin" tramp-completion-function-alist-rsh
)
838 (tramp-set-completion-function
839 "plink" tramp-completion-function-alist-ssh
)
840 (tramp-set-completion-function
841 "plink1" tramp-completion-function-alist-ssh
)
842 (tramp-set-completion-function
843 "pscp" tramp-completion-function-alist-ssh
)
844 (tramp-set-completion-function
845 "fcp" tramp-completion-function-alist-ssh
)))
847 (defcustom tramp-rsh-end-of-line
"\n"
848 "*String used for end of line in rsh connections.
849 I don't think this ever needs to be changed, so please tell me about it
850 if you need to change this.
851 Also see the method parameter `tramp-password-end-of-line' and the normal
852 variable `tramp-default-password-end-of-line'."
856 (defcustom tramp-default-password-end-of-line
857 tramp-rsh-end-of-line
858 "*String used for end of line after sending a password.
859 This variable provides the default value for the method parameter
860 `tramp-password-end-of-line', see `tramp-methods' for more details.
862 It seems that people using plink under Windows need to send
863 \"\\r\\n\" (carriage-return, then newline) after a password, but just
864 \"\\n\" after all other lines. This variable can be used for the
865 password, see `tramp-rsh-end-of-line' for the other cases.
867 The default value is to use the same value as `tramp-rsh-end-of-line'."
871 (defcustom tramp-remote-path
872 ;; "/usr/xpg4/bin" has been placed first, because on Solaris a POSIX
873 ;; compatible "id" is needed.
874 '("/usr/xpg4/bin" "/bin" "/usr/bin" "/usr/sbin" "/usr/local/bin"
875 "/usr/ccs/bin" "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
876 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
877 "*List of directories to search for executables on remote host.
878 Please notify me about other semi-standard directories to include here.
880 You can use `~' in this list, but when searching for a shell which groks
881 tilde expansion, all directory names starting with `~' will be ignored."
883 :type
'(repeat string
))
885 (defcustom tramp-login-prompt-regexp
886 ".*ogin\\( .*\\)?: *"
887 "*Regexp matching login-like prompts.
888 The regexp should match at end of buffer.
890 Sometimes the prompt is reported to look like \"login as:\"."
894 (defcustom tramp-shell-prompt-pattern
895 "^[^#$%>\n]*[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*"
896 "Regexp to match prompts from remote shell.
897 Normally, Tramp expects you to configure `shell-prompt-pattern'
898 correctly, but sometimes it happens that you are connecting to a
899 remote host which sends a different kind of shell prompt. Therefore,
900 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
901 and also things matched by this variable. The default value of this
902 variable is similar to the default value of `shell-prompt-pattern',
903 which should work well in many cases."
907 (defcustom tramp-password-prompt-regexp
908 "^.*\\([pP]assword\\|passphrase\\).*:\^@? *"
909 "*Regexp matching password-like prompts.
910 The regexp should match at end of buffer.
912 The `sudo' program appears to insert a `^@' character into the prompt."
916 (defcustom tramp-wrong-passwd-regexp
918 ;; These strings should be on the last line
919 (regexp-opt '("Permission denied."
925 "Name or service not known"
926 "Host key verification failed.") t
)
930 ;; Here comes a list of regexes, separated by \\|
931 "Received signal [0-9]+"
933 "*Regexp matching a `login failed' message.
934 The regexp should match at end of buffer."
938 (defcustom tramp-yesno-prompt-regexp
940 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t
)
942 "Regular expression matching all yes/no queries which need to be confirmed.
943 The confirmation should be done with yes or no.
944 The regexp should match at end of buffer.
945 See also `tramp-yn-prompt-regexp'."
949 (defcustom tramp-yn-prompt-regexp
950 (concat (regexp-opt '("Store key in cache? (y/n)") t
)
952 "Regular expression matching all y/n queries which need to be confirmed.
953 The confirmation should be done with y or n.
954 The regexp should match at end of buffer.
955 See also `tramp-yesno-prompt-regexp'."
959 (defcustom tramp-terminal-prompt-regexp
963 "Terminal type\\? \\[.*\\]"
965 "Regular expression matching all terminal setting prompts.
966 The regexp should match at end of buffer.
967 The answer will be provided by `tramp-action-terminal', which see."
971 (defcustom tramp-operation-not-permitted-regexp
972 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
973 (regexp-opt '("Operation not permitted") t
))
974 "Regular expression matching keep-date problems in (s)cp operations.
975 Copying has been performed successfully already, so this message can
980 (defcustom tramp-copy-failed-regexp
982 (regexp-opt '("Permission denied"
985 "No such file or directory") t
)
987 "Regular expression matching copy problems in (s)cp operations."
991 (defcustom tramp-process-alive-regexp
993 "Regular expression indicating a process has finished.
994 In fact this expression is empty by intention, it will be used only to
995 check regularly the status of the associated process.
996 The answer will be provided by `tramp-action-process-alive',
997 `tramp-multi-action-process-alive' and`tramp-action-out-of-band', which see."
1001 (defcustom tramp-temp-name-prefix
"tramp."
1002 "*Prefix to use for temporary files.
1003 If this is a relative file name (such as \"tramp.\"), it is considered
1004 relative to the directory name returned by the function
1005 `tramp-temporary-file-directory' (which see). It may also be an
1006 absolute file name; don't forget to include a prefix for the filename
1011 (defcustom tramp-discard-garbage nil
1012 "*If non-nil, try to discard garbage sent by remote shell.
1013 Some shells send such garbage upon connection setup."
1017 (defcustom tramp-sh-extra-args
'(("/bash\\'" .
"-norc -noprofile"))
1018 "*Alist specifying extra arguments to pass to the remote shell.
1019 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
1020 matching the shell file name and ARGS is a string specifying the
1023 This variable is only used when Tramp needs to start up another shell
1024 for tilde expansion. The extra arguments should typically prevent the
1025 shell from reading its init file."
1027 ;; This might be the wrong way to test whether the widget type
1028 ;; `alist' is available. Who knows the right way to test it?
1029 :type
(if (get 'alist
'widget-type
)
1030 '(alist :key-type string
:value-type string
)
1031 '(repeat (cons string string
))))
1033 (defcustom tramp-prefix-format
1034 (if tramp-unified-filenames
"/" "/[")
1035 "*String matching the very beginning of tramp file names.
1036 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1040 (defcustom tramp-prefix-regexp
1041 (concat "^" (regexp-quote tramp-prefix-format
))
1042 "*Regexp matching the very beginning of tramp file names.
1043 Should always start with \"^\". Derived from `tramp-prefix-format'."
1047 (defcustom tramp-method-regexp
1049 "*Regexp matching methods identifiers."
1053 ;; It is a little bit annoying that in XEmacs case this delimeter is different
1054 ;; for single-hop and multi-hop cases.
1055 (defcustom tramp-postfix-single-method-format
1056 (if tramp-unified-filenames
":" "/")
1057 "*String matching delimeter between method and user or host names.
1058 Applicable for single-hop methods.
1059 Used in `tramp-make-tramp-file-name'."
1063 (defcustom tramp-postfix-single-method-regexp
1064 (regexp-quote tramp-postfix-single-method-format
)
1065 "*Regexp matching delimeter between method and user or host names.
1066 Applicable for single-hop methods.
1067 Derived from `tramp-postfix-single-method-format'."
1071 (defcustom tramp-postfix-multi-method-format
1073 "*String matching delimeter between method and user or host names.
1074 Applicable for multi-hop methods.
1075 Used in `tramp-make-tramp-multi-file-name'."
1079 (defcustom tramp-postfix-multi-method-regexp
1080 (regexp-quote tramp-postfix-multi-method-format
)
1081 "*Regexp matching delimeter between method and user or host names.
1082 Applicable for multi-hop methods.
1083 Derived from `tramp-postfix-multi-method-format'."
1087 (defcustom tramp-postfix-multi-hop-format
1088 (if tramp-unified-filenames
":" "/")
1089 "*String matching delimeter between host and next method.
1090 Applicable for multi-hop methods.
1091 Used in `tramp-make-tramp-multi-file-name'."
1095 (defcustom tramp-postfix-multi-hop-regexp
1096 (regexp-quote tramp-postfix-multi-hop-format
)
1097 "*Regexp matching delimeter between host and next method.
1098 Applicable for multi-hop methods.
1099 Derived from `tramp-postfix-multi-hop-format'."
1103 (defcustom tramp-user-regexp
1105 "*Regexp matching user names."
1109 (defcustom tramp-postfix-user-format
1111 "*String matching delimeter between user and host names.
1112 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1116 (defcustom tramp-postfix-user-regexp
1117 (regexp-quote tramp-postfix-user-format
)
1118 "*Regexp matching delimeter between user and host names.
1119 Derived from `tramp-postfix-user-format'."
1123 (defcustom tramp-host-regexp
1125 "*Regexp matching host names."
1129 (defcustom tramp-host-with-port-regexp
1131 "*Regexp matching host names."
1135 (defcustom tramp-postfix-host-format
1136 (if tramp-unified-filenames
":" "]")
1137 "*String matching delimeter between host names and localnames.
1138 Used in `tramp-make-tramp-file-name' and `tramp-make-tramp-multi-file-name'."
1142 (defcustom tramp-postfix-host-regexp
1143 (regexp-quote tramp-postfix-host-format
)
1144 "*Regexp matching delimeter between host names and localnames.
1145 Derived from `tramp-postfix-host-format'."
1149 (defcustom tramp-localname-regexp
1151 "*Regexp matching localnames."
1155 ;; File name format.
1157 (defcustom tramp-file-name-structure
1161 "\\(" "\\(" tramp-method-regexp
"\\)" tramp-postfix-single-method-regexp
"\\)?"
1162 "\\(" "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
"\\)?"
1163 "\\(" tramp-host-with-port-regexp
"\\)" tramp-postfix-host-regexp
1164 "\\(" tramp-localname-regexp
"\\)")
1167 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1168 the tramp file name structure.
1170 The first element REGEXP is a regular expression matching a tramp file
1171 name. The regex should contain parentheses around the method name,
1172 the user name, the host name, and the file name parts.
1174 The second element METHOD is a number, saying which pair of
1175 parentheses matches the method name. The third element USER is
1176 similar, but for the user name. The fourth element HOST is similar,
1177 but for the host name. The fifth element FILE is for the file name.
1178 These numbers are passed directly to `match-string', which see. That
1179 means the opening parentheses are counted to identify the pair.
1181 See also `tramp-file-name-regexp'."
1183 :type
'(list (regexp :tag
"File name regexp")
1184 (integer :tag
"Paren pair for method name")
1185 (integer :tag
"Paren pair for user name ")
1186 (integer :tag
"Paren pair for host name ")
1187 (integer :tag
"Paren pair for file name ")))
1190 (defconst tramp-file-name-regexp-unified
1192 "Value for `tramp-file-name-regexp' for unified remoting.
1193 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1194 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1197 (defconst tramp-file-name-regexp-separate
1199 "Value for `tramp-file-name-regexp' for separate remoting.
1200 XEmacs uses a separate filename syntax for Tramp and EFS.
1201 See `tramp-file-name-structure-separate' for more explanations.")
1204 (defcustom tramp-file-name-regexp
1205 (if tramp-unified-filenames
1206 tramp-file-name-regexp-unified
1207 tramp-file-name-regexp-separate
)
1208 "*Regular expression matching file names handled by tramp.
1209 This regexp should match tramp file names but no other file names.
1210 \(When tramp.el is loaded, this regular expression is prepended to
1211 `file-name-handler-alist', and that is searched sequentially. Thus,
1212 if the tramp entry appears rather early in the `file-name-handler-alist'
1213 and is a bit too general, then some files might be considered tramp
1214 files which are not really tramp files.
1216 Please note that the entry in `file-name-handler-alist' is made when
1217 this file (tramp.el) is loaded. This means that this variable must be set
1218 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1219 updated after changing this variable.
1221 Also see `tramp-file-name-structure'."
1226 (defconst tramp-completion-file-name-regexp-unified
1227 "^/$\\|^/[^/:][^/]*$"
1228 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1229 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1230 Tramp. See `tramp-file-name-structure-unified' for more explanations.")
1233 (defconst tramp-completion-file-name-regexp-separate
1234 "^/\\([[][^]]*\\)?$"
1235 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1236 XEmacs uses a separate filename syntax for Tramp and EFS.
1237 See `tramp-file-name-structure-separate' for more explanations.")
1240 (defcustom tramp-completion-file-name-regexp
1241 (if tramp-unified-filenames
1242 tramp-completion-file-name-regexp-unified
1243 tramp-completion-file-name-regexp-separate
)
1244 "*Regular expression matching file names handled by tramp completion.
1245 This regexp should match partial tramp file names only.
1247 Please note that the entry in `file-name-handler-alist' is made when
1248 this file (tramp.el) is loaded. This means that this variable must be set
1249 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1250 updated after changing this variable.
1252 Also see `tramp-file-name-structure'."
1256 (defcustom tramp-multi-file-name-structure
1260 "\\(" "\\(" tramp-method-regexp
"\\)" "\\)?"
1261 "\\(" "\\(" tramp-postfix-multi-hop-regexp
"%s" "\\)+" "\\)?"
1262 tramp-postfix-host-regexp
"\\(" tramp-localname-regexp
"\\)")
1264 "*Describes the file name structure of `multi' files.
1265 Multi files allow you to contact a remote host in several hops.
1266 This is a list of four elements (REGEXP METHOD HOP LOCALNAME).
1268 The first element, REGEXP, gives a regular expression to match against
1269 the file name. In this regular expression, `%s' is replaced with the
1270 value of `tramp-multi-file-name-hop-structure'. (Note: in order to
1271 allow multiple hops, you normally want to use something like
1272 \"\\\\(\\\\(%s\\\\)+\\\\)\" in the regular expression. The outer pair
1273 of parentheses is used for the HOP element, see below.)
1275 All remaining elements are numbers. METHOD gives the number of the
1276 paren pair which matches the method name. HOP gives the number of the
1277 paren pair which matches the hop sequence. LOCALNAME gives the number of
1278 the paren pair which matches the localname (pathname) on the remote host.
1280 LOCALNAME can also be negative, which means to count from the end. Ie, a
1281 value of -1 means the last paren pair.
1283 I think it would be good if the regexp matches the whole of the
1284 string, but I haven't actually tried what happens if it doesn't..."
1286 :type
'(list (regexp :tag
"File name regexp")
1287 (integer :tag
"Paren pair for method name")
1288 (integer :tag
"Paren pair for hops")
1289 (integer :tag
"Paren pair to match localname")))
1291 (defcustom tramp-multi-file-name-hop-structure
1294 "\\(" tramp-method-regexp
"\\)" tramp-postfix-multi-method-regexp
1295 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
1296 "\\(" tramp-host-with-port-regexp
"\\)")
1298 "*Describes the structure of a hop in multi files.
1299 This is a list of four elements (REGEXP METHOD USER HOST). First
1300 element REGEXP is used to match against the hop. Pair number METHOD
1301 matches the method of one hop, pair number USER matches the user of
1302 one hop, pair number HOST matches the host of one hop.
1304 This regular expression should match exactly all of one hop."
1306 :type
'(list (regexp :tag
"Hop regexp")
1307 (integer :tag
"Paren pair for method name")
1308 (integer :tag
"Paren pair for user name")
1309 (integer :tag
"Paren pair for host name")))
1311 (defcustom tramp-make-multi-tramp-file-format
1313 (concat tramp-prefix-format
"%m")
1314 (concat tramp-postfix-multi-hop-format
1315 "%m" tramp-postfix-multi-method-format
1316 "%u" tramp-postfix-user-format
1318 (concat tramp-postfix-host-format
"%p"))
1319 "*Describes how to construct a `multi' file name.
1320 This is a list of three elements PREFIX, HOP and LOCALNAME.
1322 The first element PREFIX says how to construct the prefix, the second
1323 element HOP specifies what each hop looks like, and the final element
1324 LOCALNAME says how to construct the localname (pathname).
1326 In PREFIX, `%%' means `%' and `%m' means the method name.
1328 In HOP, `%%' means `%' and `%m', `%u', `%h' mean the hop method, hop
1329 user and hop host, respectively.
1331 In LOCALNAME, `%%' means `%' and `%p' means the localname.
1333 The resulting file name always contains one copy of PREFIX and one
1334 copy of LOCALNAME, but there is one copy of HOP for each hop in the file
1337 Note: the current implementation requires the prefix to contain the
1338 method name, followed by all the hops, and the localname must come
1341 :type
'(list string string string
))
1343 (defcustom tramp-terminal-type
"dumb"
1344 "*Value of TERM environment variable for logging in to remote host.
1345 Because Tramp wants to parse the output of the remote shell, it is easily
1346 confused by ANSI color escape sequences and suchlike. Often, shell init
1347 files conditionalize this setup based on the TERM environment variable."
1351 (defcustom tramp-completion-without-shell-p nil
1352 "*If nil, use shell wildcards for completion, else rely on Lisp only.
1353 Using shell wildcards for completions has the advantage that it can be
1354 fast even in large directories, but completion is always
1355 case-sensitive. Relying on Lisp only means that case-insensitive
1356 completion is possible (subject to the variable `completion-ignore-case'),
1357 but it might be slow on large directories."
1361 (defcustom tramp-actions-before-shell
1362 '((tramp-password-prompt-regexp tramp-action-password
)
1363 (tramp-login-prompt-regexp tramp-action-login
)
1364 (shell-prompt-pattern tramp-action-succeed
)
1365 (tramp-shell-prompt-pattern tramp-action-succeed
)
1366 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
1367 (tramp-yesno-prompt-regexp tramp-action-yesno
)
1368 (tramp-yn-prompt-regexp tramp-action-yn
)
1369 (tramp-terminal-prompt-regexp tramp-action-terminal
)
1370 (tramp-process-alive-regexp tramp-action-process-alive
))
1371 "List of pattern/action pairs.
1372 Whenever a pattern matches, the corresponding action is performed.
1373 Each item looks like (PATTERN ACTION).
1375 The PATTERN should be a symbol, a variable. The value of this
1376 variable gives the regular expression to search for. Note that the
1377 regexp must match at the end of the buffer, \"\\'\" is implicitly
1380 The ACTION should also be a symbol, but a function. When the
1381 corresponding PATTERN matches, the ACTION function is called."
1383 :type
'(repeat (list variable function
)))
1385 (defcustom tramp-actions-copy-out-of-band
1386 '((tramp-password-prompt-regexp tramp-action-password
)
1387 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
1388 (tramp-copy-failed-regexp tramp-action-copy-failed
)
1389 (tramp-process-alive-regexp tramp-action-out-of-band
))
1390 "List of pattern/action pairs.
1391 This list is used for copying/renaming with out-of-band methods.
1392 See `tramp-actions-before-shell' for more info."
1394 :type
'(repeat (list variable function
)))
1396 (defcustom tramp-multi-actions
1397 '((tramp-password-prompt-regexp tramp-multi-action-password
)
1398 (tramp-login-prompt-regexp tramp-multi-action-login
)
1399 (shell-prompt-pattern tramp-multi-action-succeed
)
1400 (tramp-shell-prompt-pattern tramp-multi-action-succeed
)
1401 (tramp-wrong-passwd-regexp tramp-multi-action-permission-denied
)
1402 (tramp-process-alive-regexp tramp-multi-action-process-alive
))
1403 "List of pattern/action pairs.
1404 This list is used for each hop in multi-hop connections.
1405 See `tramp-actions-before-shell' for more info."
1407 :type
'(repeat (list variable function
)))
1409 (defcustom tramp-initial-commands
1412 "unset autocorrect")
1413 "List of commands to send to the first remote shell that we see.
1414 These commands will be sent to any shell, and thus they should be
1415 designed to work in such circumstances. Also, restrict the commands
1416 to the bare necessity for getting the remote shell into a state
1417 where it is possible to execute the Bourne-ish shell.
1419 At the moment, the command to execute the Bourne-ish shell uses strange
1420 quoting which `tcsh' tries to correct, so we send the command \"unset
1421 autocorrect\" to the remote host."
1423 :type
'(repeat string
))
1425 ;; Chunked sending kluge. We set this to 500 for black-listed constellations
1426 ;; known to have a bug in `process-send-string'; some ssh connections appear
1427 ;; to drop bytes when data is sent too quickly. There is also a connection
1428 ;; buffer local variable, which is computed depending on remote host properties
1429 ;; when `tramp-chunksize' is zero or nil.
1430 (defcustom tramp-chunksize
1431 (when (and (not (featurep 'xemacs
))
1432 (memq system-type
'(hpux)))
1434 ;; Parentheses in docstring starting at beginning of line are escaped.
1435 ;; Fontification is messed up when
1436 ;; `open-paren-in-column-0-is-defun-start' set to t.
1437 "*If non-nil, chunksize for sending input to local process.
1438 It is necessary only on systems which have a buggy `process-send-string'
1439 implementation. The necessity, whether this variable must be set, can be
1440 checked via the following code:
1443 (let* ((user \"xxx\") (host \"yyy\")
1445 (sent init) (received init))
1446 (while (= sent received)
1447 (setq sent (+ sent step))
1449 (let ((proc (start-process (buffer-name) (current-buffer)
1450 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1451 (when (memq (process-status proc) '(run open))
1452 (process-send-string proc (make-string sent ?\\ ))
1453 (process-send-eof proc)
1454 (process-send-eof proc))
1455 (while (not (progn (goto-char (point-min))
1456 (re-search-forward \"\\\\w+\" (point-max) t)))
1457 (accept-process-output proc 1))
1458 (when (memq (process-status proc) '(run open))
1459 (setq received (string-to-number (match-string 0)))
1460 (delete-process proc)
1461 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1463 (if (> sent (+ init step))
1464 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1466 (message \"Test does not work\")
1467 (display-buffer (current-buffer))
1470 In the Emacs normally running Tramp, evaluate the above code
1471 \(replace \"xxx\" and \"yyy\" by the remote user and host name,
1472 respectively). You can do this, for example, by pasting it into
1473 the `*scratch*' buffer and then hitting C-j with the cursor after the
1474 last closing parenthesis. Note that it works only if you have configured
1475 \"ssh\" to run without password query, see ssh-agent(1).
1477 You will see the number of bytes sent successfully to the remote host.
1478 If that number exceeds 1000, you can stop the execution by hitting
1479 C-g, because your Emacs is likely clean.
1481 If your Emacs is buggy, the code stops and gives you an indication
1482 about the value `tramp-chunksize' should be set. Maybe you could just
1483 experiment a bit, e.g. changing the values of `init' and `step'
1484 in the third line of the code.
1486 When it is necessary to set `tramp-chunksize', you might consider to
1487 use an out-of-the-band method (like \"scp\") instead of an internal one
1488 \(like \"ssh\"), because setting `tramp-chunksize' to non-nil decreases
1491 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1492 this variable to be set as well."
1494 :type
'(choice (const nil
) integer
))
1496 ;; Logging in to a remote host normally requires obtaining a pty. But
1497 ;; Emacs on MacOS X has process-connection-type set to nil by default,
1498 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1499 ;; for an override of the system default.
1500 (defcustom tramp-process-connection-type t
1501 "Overrides `process-connection-type' for connections from Tramp.
1502 Tramp binds process-connection-type to the value given here before
1503 opening a connection to a remote host."
1505 :type
'(choice (const nil
) (const t
) (const pty
)))
1507 ;;; Internal Variables:
1509 (defvar tramp-buffer-file-attributes nil
1510 "Holds the `ls -ild' output for the current buffer.
1511 This variable is local to each buffer. It is not used if the remote
1512 machine groks Perl. If it is used, it's used as an emulation for
1513 the visited file modtime.")
1514 (make-variable-buffer-local 'tramp-buffer-file-attributes
)
1516 (defvar tramp-md5-function
1517 (cond ((and (require 'md5
) (fboundp 'md5
)) 'md5
)
1518 ((fboundp 'md5-encode
)
1519 (lambda (x) (base64-encode-string
1520 (funcall (symbol-function 'md5-encode
) x
))))
1521 (t (error "Couldn't find an `md5' function")))
1522 "Function to call for running the MD5 algorithm.")
1524 (defvar tramp-end-of-output
1526 (funcall tramp-md5-function
1528 (prin1-to-string process-environment
)
1529 (current-time-string)
1531 ;; (if (fboundp 'directory-files-and-attributes)
1532 ;; (funcall 'directory-files-and-attributes
1533 ;; (or (getenv "HOME")
1534 ;; (tramp-temporary-file-directory)))
1537 ;; (cons x (file-attributes x)))
1538 ;; (directory-files (or (getenv "HOME")
1539 ;; (tramp-temporary-file-directory))
1542 "String used to recognize end of output.")
1544 (defvar tramp-connection-function nil
1545 "This internal variable holds a parameter for `tramp-methods'.
1546 In the connection buffer, this variable has the value of the like-named
1547 method parameter, as specified in `tramp-methods' (which see).")
1549 (defvar tramp-remote-sh nil
1550 "This internal variable holds a parameter for `tramp-methods'.
1551 In the connection buffer, this variable has the value of the like-named
1552 method parameter, as specified in `tramp-methods' (which see).")
1554 (defvar tramp-login-program nil
1555 "This internal variable holds a parameter for `tramp-methods'.
1556 In the connection buffer, this variable has the value of the like-named
1557 method parameter, as specified in `tramp-methods' (which see).")
1559 (defvar tramp-login-args nil
1560 "This internal variable holds a parameter for `tramp-methods'.
1561 In the connection buffer, this variable has the value of the like-named
1562 method parameter, as specified in `tramp-methods' (which see).")
1564 (defvar tramp-copy-program nil
1565 "This internal variable holds a parameter for `tramp-methods'.
1566 In the connection buffer, this variable has the value of the like-named
1567 method parameter, as specified in `tramp-methods' (which see).")
1569 (defvar tramp-copy-args nil
1570 "This internal variable holds a parameter for `tramp-methods'.
1571 In the connection buffer, this variable has the value of the like-named
1572 method parameter, as specified in `tramp-methods' (which see).")
1574 (defvar tramp-copy-keep-date-arg nil
1575 "This internal variable holds a parameter for `tramp-methods'.
1576 In the connection buffer, this variable has the value of the like-named
1577 method parameter, as specified in `tramp-methods' (which see).")
1579 (defvar tramp-encoding-command nil
1580 "This internal variable holds a parameter for `tramp-methods'.
1581 In the connection buffer, this variable has the value of the like-named
1582 method parameter, as specified in `tramp-methods' (which see).")
1584 (defvar tramp-decoding-command nil
1585 "This internal variable holds a parameter for `tramp-methods'.
1586 In the connection buffer, this variable has the value of the like-named
1587 method parameter, as specified in `tramp-methods' (which see).")
1589 (defvar tramp-encoding-function nil
1590 "This internal variable holds a parameter for `tramp-methods'.
1591 In the connection buffer, this variable has the value of the like-named
1592 method parameter, as specified in `tramp-methods' (which see).")
1594 (defvar tramp-decoding-function nil
1595 "This internal variable holds a parameter for `tramp-methods'.
1596 In the connection buffer, this variable has the value of the like-named
1597 method parameter, as specified in `tramp-methods' (which see).")
1599 (defvar tramp-password-end-of-line nil
1600 "This internal variable holds a parameter for `tramp-methods'.
1601 In the connection buffer, this variable has the value of the like-named
1602 method parameter, as specified in `tramp-methods' (which see).")
1604 ;; CCC `local in each buffer'?
1605 (defvar tramp-ls-command nil
1606 "This command is used to get a long listing with numeric user and group ids.
1607 This variable is automatically made buffer-local to each rsh process buffer
1608 upon opening the connection.")
1610 (defvar tramp-current-multi-method nil
1611 "Name of `multi' connection method for this *tramp* buffer, or nil if not multi.
1612 This variable is automatically made buffer-local to each rsh process buffer
1613 upon opening the connection.")
1615 (defvar tramp-current-method nil
1616 "Connection method for this *tramp* buffer.
1617 This variable is automatically made buffer-local to each rsh process buffer
1618 upon opening the connection.")
1620 (defvar tramp-current-user nil
1621 "Remote login name for this *tramp* buffer.
1622 This variable is automatically made buffer-local to each rsh process buffer
1623 upon opening the connection.")
1625 (defvar tramp-current-host nil
1626 "Remote host for this *tramp* buffer.
1627 This variable is automatically made buffer-local to each rsh process buffer
1628 upon opening the connection.")
1630 (defvar tramp-test-groks-nt nil
1631 "Whether the `test' command groks the `-nt' switch.
1632 \(`test A -nt B' tests if file A is newer than file B.)
1633 This variable is automatically made buffer-local to each rsh process buffer
1634 upon opening the connection.")
1636 (defvar tramp-file-exists-command nil
1637 "Command to use for checking if a file exists.
1638 This variable is automatically made buffer-local to each rsh process buffer
1639 upon opening the connection.")
1641 (defconst tramp-uudecode
"\
1643 \(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1647 "Shell function to implement `uudecode' to standard output.
1648 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
1649 for this or `uudecode -p', but some systems don't, and for them
1650 we have this shell function.")
1652 ;; Perl script to implement `file-attributes' in a Lisp `read'able
1653 ;; output. If you are hacking on this, note that you get *no* output
1654 ;; unless this spits out a complete line, including the '\n' at the
1656 ;; The device number is returned as "-1", because there will be a virtual
1657 ;; device number set in `tramp-handle-file-attributes'
1658 (defconst tramp-perl-file-attributes
"\
1659 @stat = lstat($ARGV[0]);
1660 if (($stat[2] & 0170000) == 0120000)
1662 $type = readlink($ARGV[0]);
1663 $type = \"\\\"$type\\\"\";
1665 elsif (($stat[2] & 0170000) == 040000)
1673 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1674 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1676 \"(%s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) -1)\\n\",
1681 $stat[8] >> 16 & 0xffff,
1683 $stat[9] >> 16 & 0xffff,
1685 $stat[10] >> 16 & 0xffff,
1689 $stat[1] >> 16 & 0xffff,
1692 "Perl script to produce output suitable for use with `file-attributes'
1693 on the remote file system.")
1695 (defconst tramp-perl-directory-files-and-attributes
"\
1696 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
1697 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
1698 @list = readdir(DIR);
1702 for($i = 0; $i < $n; $i++)
1704 $filename = $list[$i];
1705 @stat = lstat($filename);
1706 if (($stat[2] & 0170000) == 0120000)
1708 $type = readlink($filename);
1709 $type = \"\\\"$type\\\"\";
1711 elsif (($stat[2] & 0170000) == 040000)
1719 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1720 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1722 \"(\\\"%s\\\" %s %u %s %s (%u %u) (%u %u) (%u %u) %u %u t (%u . %u) (%u %u))\\n\",
1728 $stat[8] >> 16 & 0xffff,
1730 $stat[9] >> 16 & 0xffff,
1732 $stat[10] >> 16 & 0xffff,
1736 $stat[1] >> 16 & 0xffff,
1738 $stat[0] >> 16 & 0xffff,
1742 "Perl script implementing `directory-files-attributes' as Lisp `read'able
1745 ;; ;; These two use uu encoding.
1746 ;; (defvar tramp-perl-encode "%s -e'\
1747 ;; print qq(begin 644 xxx\n);
1750 ;; while (read(STDIN, $s, 45)) {
1751 ;; print pack(q(u), $s);
1756 ;; "Perl program to use for encoding a file.
1757 ;; Escape sequence %s is replaced with name of Perl binary.")
1759 ;; (defvar tramp-perl-decode "%s -ne '
1760 ;; print unpack q(u), $_;
1762 ;; "Perl program to use for decoding a file.
1763 ;; Escape sequence %s is replaced with name of Perl binary.")
1765 ;; These two use base64 encoding.
1766 (defvar tramp-perl-encode-with-module
1767 "perl -MMIME::Base64 -0777 -ne 'print encode_base64($_)'"
1768 "Perl program to use for encoding a file.
1769 Escape sequence %s is replaced with name of Perl binary.
1770 This string is passed to `format', so percent characters need to be doubled.
1771 This implementation requires the MIME::Base64 Perl module to be installed
1772 on the remote host.")
1774 (defvar tramp-perl-decode-with-module
1775 "perl -MMIME::Base64 -0777 -ne 'print decode_base64($_)'"
1776 "Perl program to use for decoding a file.
1777 Escape sequence %s is replaced with name of Perl binary.
1778 This string is passed to `format', so percent characters need to be doubled.
1779 This implementation requires the MIME::Base64 Perl module to be installed
1780 on the remote host.")
1782 (defvar tramp-perl-encode
1784 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1785 # Copyright (C) 2002, 2006 Free Software Foundation, Inc.
1790 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1791 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1796 # We read in chunks of 54 bytes, to generate output lines
1797 # of 72 chars (plus end of line)
1800 while (my $data = <STDIN>) {
1803 # Only for the last chunk, and only if did not fill the last three-byte packet
1805 my $mod = length($data) %% 3;
1806 $pad = q(=) x (3 - $mod) if $mod;
1809 # Not the fastest method, but it is simple: unpack to binary string, split
1810 # by groups of 6 bits and convert back from binary to byte; then map into
1811 # the translation table
1815 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1820 "Perl program to use for encoding a file.
1821 Escape sequence %s is replaced with name of Perl binary.
1822 This string is passed to `format', so percent characters need to be doubled.")
1824 (defvar tramp-perl-decode
1826 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1827 # Copyright (C) 2002, 2006 Free Software Foundation, Inc.
1832 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1833 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1836 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1840 # We are going to accumulate into $pending to accept any line length
1841 # (we do not check they are <= 76 chars as the RFC says)
1844 while (my $data = <STDIN>) {
1847 # If we find one or two =, we have reached the end and
1848 # any following data is to be discarded
1849 my $finished = $data =~ s/(==?).*/$1/;
1852 my $len = length($pending);
1853 my $chunk = substr($pending, 0, $len & ~3);
1854 $pending = substr($pending, $len & ~3 + 1);
1856 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1857 # split in 8-bit chunks and convert back to char.
1860 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1865 "Perl program to use for decoding a file.
1866 Escape sequence %s is replaced with name of Perl binary.
1867 This string is passed to `format', so percent characters need to be doubled.")
1869 (defconst tramp-file-mode-type-map
'((0 .
"-") ; Normal file (SVID-v2 and XPG2)
1871 (2 .
"c") ; character device
1872 (3 .
"m") ; multiplexed character device (v7)
1873 (4 .
"d") ; directory
1874 (5 .
"?") ; Named special file (XENIX)
1875 (6 .
"b") ; block device
1876 (7 .
"?") ; multiplexed block device (v7)
1877 (8 .
"-") ; regular file
1878 (9 .
"n") ; network special file (HP-UX)
1879 (10 .
"l") ; symlink
1880 (11 .
"?") ; ACL shadow inode (Solaris, not userspace)
1882 (13 .
"D") ; door special (Solaris)
1883 (14 .
"w")) ; whiteout (BSD)
1884 "A list of file types returned from the `stat' system call.
1885 This is used to map a mode number to a permission string.")
1887 (defvar tramp-dos-coding-system
1888 (if (and (fboundp 'coding-system-p
)
1889 (funcall 'coding-system-p
'(dos)))
1892 "Some Emacsen know the `dos' coding system, others need `undecided-dos'.")
1894 (defvar tramp-last-cmd nil
1895 "Internal Tramp variable recording the last command sent.
1896 This variable is buffer-local in every buffer.")
1897 (make-variable-buffer-local 'tramp-last-cmd
)
1899 (defvar tramp-process-echoes nil
1900 "Whether to process echoes from the remote shell.")
1902 (defvar tramp-last-cmd-time nil
1903 "Internal Tramp variable recording the time when the last cmd was sent.
1904 This variable is buffer-local in every buffer.")
1905 (make-variable-buffer-local 'tramp-last-cmd-time
)
1907 ;; This variable does not have the right value in XEmacs. What should
1908 ;; I use instead of find-operation-coding-system in XEmacs?
1909 (defvar tramp-feature-write-region-fix
1910 (when (fboundp 'find-operation-coding-system
)
1911 (let ((file-coding-system-alist '(("test" emacs-mule
))))
1912 (funcall (symbol-function 'find-operation-coding-system
)
1913 'write-region
0 0 "" nil
"test")))
1914 "Internal variable to say if `write-region' chooses the right coding.
1915 Older versions of Emacs chose the coding system for `write-region' based
1916 on the FILENAME argument, even if VISIT was a string.")
1918 ;; New handlers should be added here. The following operations can be
1919 ;; handled using the normal primitives: file-name-as-directory,
1920 ;; file-name-directory, file-name-nondirectory,
1921 ;; file-name-sans-versions, get-file-buffer.
1922 (defconst tramp-file-name-handler-alist
1924 (load . tramp-handle-load
)
1925 (make-symbolic-link . tramp-handle-make-symbolic-link
)
1926 (file-name-directory . tramp-handle-file-name-directory
)
1927 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
1928 (file-truename . tramp-handle-file-truename
)
1929 (file-exists-p . tramp-handle-file-exists-p
)
1930 (file-directory-p . tramp-handle-file-directory-p
)
1931 (file-executable-p . tramp-handle-file-executable-p
)
1932 (file-accessible-directory-p . tramp-handle-file-accessible-directory-p
)
1933 (file-readable-p . tramp-handle-file-readable-p
)
1934 (file-regular-p . tramp-handle-file-regular-p
)
1935 (file-symlink-p . tramp-handle-file-symlink-p
)
1936 (file-writable-p . tramp-handle-file-writable-p
)
1937 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p
)
1938 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p
)
1939 (file-attributes . tramp-handle-file-attributes
)
1940 (file-modes . tramp-handle-file-modes
)
1941 (directory-files . tramp-handle-directory-files
)
1942 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes
)
1943 (file-name-all-completions . tramp-handle-file-name-all-completions
)
1944 (file-name-completion . tramp-handle-file-name-completion
)
1945 (add-name-to-file . tramp-handle-add-name-to-file
)
1946 (copy-file . tramp-handle-copy-file
)
1947 (rename-file . tramp-handle-rename-file
)
1948 (set-file-modes . tramp-handle-set-file-modes
)
1949 (make-directory . tramp-handle-make-directory
)
1950 (delete-directory . tramp-handle-delete-directory
)
1951 (delete-file . tramp-handle-delete-file
)
1952 (directory-file-name . tramp-handle-directory-file-name
)
1953 (shell-command . tramp-handle-shell-command
)
1954 (process-file . tramp-handle-process-file
)
1955 (insert-directory . tramp-handle-insert-directory
)
1956 (expand-file-name . tramp-handle-expand-file-name
)
1957 (file-local-copy . tramp-handle-file-local-copy
)
1958 (file-remote-p . tramp-handle-file-remote-p
)
1959 (insert-file-contents . tramp-handle-insert-file-contents
)
1960 (write-region . tramp-handle-write-region
)
1961 (find-backup-file-name . tramp-handle-find-backup-file-name
)
1962 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name
)
1963 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
1964 (dired-compress-file . tramp-handle-dired-compress-file
)
1965 (dired-call-process . tramp-handle-dired-call-process
)
1966 (dired-recursive-delete-directory
1967 . tramp-handle-dired-recursive-delete-directory
)
1968 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime
)
1969 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime
))
1970 "Alist of handler functions.
1971 Operations not mentioned here will be handled by the normal Emacs functions.")
1973 ;; Handlers for partial tramp file names. For Emacs just
1974 ;; `file-name-all-completions' is needed.
1976 (defconst tramp-completion-file-name-handler-alist
1977 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions
)
1978 (file-name-completion . tramp-completion-handle-file-name-completion
))
1979 "Alist of completion handler functions.
1980 Used for file names matching `tramp-file-name-regexp'. Operations not
1981 mentioned here will be handled by `tramp-file-name-handler-alist' or the
1982 normal Emacs functions.")
1984 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
1985 (defvar tramp-foreign-file-name-handler-alist
1986 ;; (identity . tramp-sh-file-name-handler) should always be the last
1987 ;; entry, since `identity' always matches.
1988 '((identity . tramp-sh-file-name-handler
))
1989 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1990 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1993 ;;; Internal functions which must come first.
1995 (defsubst tramp-message
(level fmt-string
&rest args
)
1996 "Emit a message depending on verbosity level.
1997 First arg LEVEL says to be quiet if `tramp-verbose' is less than LEVEL. The
1998 message is emitted only if `tramp-verbose' is greater than or equal to LEVEL.
1999 Calls function `message' with FMT-STRING as control string and the remaining
2000 ARGS to actually emit the message (if applicable).
2002 This function expects to be called from the tramp buffer only!"
2003 (when (<= level tramp-verbose
)
2004 (apply #'message
(concat "tramp: " fmt-string
) args
)
2005 (when tramp-debug-buffer
2008 (tramp-get-debug-buffer
2009 tramp-current-multi-method tramp-current-method
2010 tramp-current-user tramp-current-host
))
2011 (goto-char (point-max))
2014 (tramp-insert-with-face
2016 (concat "# " (apply #'format fmt-string args
) "\n"))))))
2018 (defun tramp-message-for-buffer
2019 (multi-method method user host level fmt-string
&rest args
)
2020 "Like `tramp-message' but temporarily switches to the tramp buffer.
2021 First three args METHOD, USER, and HOST identify the tramp buffer to use,
2022 remaining args passed to `tramp-message'."
2024 (set-buffer (tramp-get-buffer multi-method method user host
))
2025 (apply 'tramp-message level fmt-string args
)))
2027 (defsubst tramp-line-end-position nil
2028 "Return point at end of line.
2029 Calls `line-end-position' or `point-at-eol' if defined, else
2030 own implementation."
2032 ((fboundp 'line-end-position
) (funcall (symbol-function 'line-end-position
)))
2033 ((fboundp 'point-at-eol
) (funcall (symbol-function 'point-at-eol
)))
2034 (t (save-excursion (end-of-line) (point)))))
2036 (defmacro with-parsed-tramp-file-name
(filename var
&rest body
)
2037 "Parse a Tramp filename and make components available in the body.
2039 First arg FILENAME is evaluated and dissected into its components.
2040 Second arg VAR is a symbol. It is used as a variable name to hold
2041 the filename structure. It is also used as a prefix for the variables
2042 holding the components. For example, if VAR is the symbol `foo', then
2043 `foo' will be bound to the whole structure, `foo-multi-method' will
2044 be bound to the multi-method component, and so on for `foo-method',
2045 `foo-user', `foo-host', `foo-localname'.
2047 Remaining args are Lisp expressions to be evaluated (inside an implicit
2050 If VAR is nil, then we bind `v' to the structure and `multi-method',
2051 `method', `user', `host', `localname' to the components."
2052 `(let* ((,(or var
'v
) (tramp-dissect-file-name ,filename
))
2053 (,(if var
(intern (concat (symbol-name var
) "-multi-method")) 'multi-method
)
2054 (tramp-file-name-multi-method ,(or var
'v
)))
2055 (,(if var
(intern (concat (symbol-name var
) "-method")) 'method
)
2056 (tramp-file-name-method ,(or var
'v
)))
2057 (,(if var
(intern (concat (symbol-name var
) "-user")) 'user
)
2058 (tramp-file-name-user ,(or var
'v
)))
2059 (,(if var
(intern (concat (symbol-name var
) "-host")) 'host
)
2060 (tramp-file-name-host ,(or var
'v
)))
2061 (,(if var
(intern (concat (symbol-name var
) "-localname")) 'localname
)
2062 (tramp-file-name-localname ,(or var
'v
))))
2065 (put 'with-parsed-tramp-file-name
'lisp-indent-function
2)
2066 ;; Enable debugging.
2067 (def-edebug-spec with-parsed-tramp-file-name
(form symbolp body
))
2068 ;; Highlight as keyword.
2069 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<with-parsed-tramp-file-name\\>"))
2071 (defmacro tramp-let-maybe
(variable value
&rest body
)
2072 "Let-bind VARIABLE to VALUE in BODY, but only if VARIABLE is not obsolete.
2073 BODY is executed whether or not the variable is obsolete.
2074 The intent is to protect against `obsolete variable' warnings."
2075 `(if (get ',variable
'byte-obsolete-variable
)
2077 (let ((,variable
,value
))
2079 (put 'tramp-let-maybe
'lisp-indent-function
2)
2080 (put 'tramp-let-maybe
'edebug-form-spec t
)
2082 ;;; Config Manipulation Functions:
2084 (defun tramp-set-completion-function (method function-list
)
2085 "Sets the list of completion functions for METHOD.
2086 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2087 The FUNCTION is intended to parse FILE according its syntax.
2088 It might be a predefined FUNCTION, or a user defined FUNCTION.
2089 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
2090 `tramp-parse-sconfig',`tramp-parse-hosts', `tramp-parse-passwd',
2091 and `tramp-parse-netrc'.
2095 (tramp-set-completion-function
2097 '((tramp-parse-sconfig \"/etc/ssh_config\")
2098 (tramp-parse-sconfig \"~/.ssh/config\")))"
2100 (let ((r function-list
)
2102 (setq tramp-completion-function-alist
2103 (delete (assoc method tramp-completion-function-alist
)
2104 tramp-completion-function-alist
))
2107 ;; Remove double entries
2108 (when (member (car v
) (cdr v
))
2109 (setcdr v
(delete (car v
) (cdr v
))))
2110 ;; Check for function and file
2111 (unless (and (functionp (nth 0 (car v
)))
2112 (file-exists-p (nth 1 (car v
))))
2113 (setq r
(delete (car v
) r
)))
2117 (add-to-list 'tramp-completion-function-alist
2120 (defun tramp-get-completion-function (method)
2121 "Returns list of completion functions for METHOD.
2122 For definition of that list see `tramp-set-completion-function'."
2123 (cdr (assoc method tramp-completion-function-alist
)))
2125 ;;; File Name Handler Functions:
2127 (defun tramp-handle-make-symbolic-link
2128 (filename linkname
&optional ok-if-already-exists
)
2129 "Like `make-symbolic-link' for tramp files.
2130 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
2131 the symlink. If LINKNAME is a Tramp file, only the localname component is
2132 used as the target of the symlink.
2134 If LINKNAME is a Tramp file and the localname component is relative, then
2135 it is expanded first, before the localname component is taken. Note that
2136 this can give surprising results if the user/host for the source and
2137 target of the symlink differ."
2138 (with-parsed-tramp-file-name linkname l
2139 (let ((ln (tramp-get-remote-ln l-multi-method l-method l-user l-host
))
2140 (cwd (file-name-directory l-localname
)))
2143 (list "Making a symbolic link."
2144 "ln(1) does not exist on the remote host.")))
2146 ;; Do the 'confirm if exists' thing.
2147 (when (file-exists-p linkname
)
2149 (if (or (null ok-if-already-exists
) ; not allowed to exist
2150 (and (numberp ok-if-already-exists
)
2153 "File %s already exists; make it a link anyway? "
2155 (signal 'file-already-exists
(list "File already exists" l-localname
))
2156 (delete-file linkname
)))
2158 ;; If FILENAME is a Tramp name, use just the localname component.
2159 (when (tramp-tramp-file-p filename
)
2160 (setq filename
(tramp-file-name-localname
2161 (tramp-dissect-file-name
2162 (expand-file-name filename
)))))
2164 ;; Right, they are on the same host, regardless of user, method, etc.
2165 ;; We now make the link on the remote machine. This will occur as the user
2166 ;; that FILENAME belongs to.
2168 (tramp-send-command-and-check
2169 l-multi-method l-method l-user l-host
2170 (format "cd %s && %s -sf %s %s"
2177 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix
)
2178 "Like `load' for tramp files. Not implemented!"
2179 (unless (file-name-absolute-p file
)
2180 (error "Tramp cannot `load' files without absolute file name"))
2181 (with-parsed-tramp-file-name file nil
2183 (cond ((file-exists-p (concat file
".elc"))
2184 (setq file
(concat file
".elc")))
2185 ((file-exists-p (concat file
".el"))
2186 (setq file
(concat file
".el")))))
2188 ;; The first condition is always true for absolute file names.
2189 ;; Included for safety's sake.
2190 (unless (or (file-name-directory file
)
2191 (string-match "\\.elc?\\'" file
))
2192 (error "File `%s' does not include a `.el' or `.elc' suffix"
2195 (when (not (file-exists-p file
))
2196 (error "Cannot load nonexistent file `%s'" file
)))
2197 (if (not (file-exists-p file
))
2200 (message "Loading %s..." file
))
2201 (let ((local-copy (file-local-copy file
)))
2202 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2203 (load local-copy noerror t t
)
2204 (delete-file local-copy
))
2206 (message "Loading %s...done" file
))
2209 ;; Localname manipulation functions that grok TRAMP localnames...
2210 (defun tramp-handle-file-name-directory (file)
2211 "Like `file-name-directory' but aware of TRAMP files."
2212 ;; Everything except the last filename thing is the directory.
2213 (with-parsed-tramp-file-name file nil
2214 ;; Run the command on the localname portion only.
2215 (tramp-make-tramp-file-name
2216 multi-method method user host
(file-name-directory (or localname
"")))))
2218 (defun tramp-handle-file-name-nondirectory (file)
2219 "Like `file-name-nondirectory' but aware of TRAMP files."
2220 (with-parsed-tramp-file-name file nil
2221 (file-name-nondirectory localname
)))
2223 (defun tramp-handle-file-truename (filename &optional counter prev-dirs
)
2224 "Like `file-truename' for tramp files."
2225 (with-parsed-tramp-file-name (expand-file-name filename
) nil
2226 (let* ((steps (tramp-split-string localname
"/"))
2227 (localnamedir (tramp-let-maybe directory-sep-char ?
/ ;for XEmacs
2228 (file-name-as-directory localname
)))
2229 (is-dir (string= localname localnamedir
))
2232 ;; Don't make the following value larger than necessary.
2233 ;; People expect an error message in a timely fashion when
2234 ;; something is wrong; otherwise they might think that Emacs
2235 ;; is hung. Of course, correctness has to come first.
2237 (result nil
) ;result steps in reverse order
2239 (tramp-message-for-buffer
2240 multi-method method user host
2241 10 "Finding true name for `%s'" filename
)
2242 (while (and steps
(< numchase numchase-limit
))
2243 (setq thisstep
(pop steps
))
2244 (tramp-message-for-buffer
2245 multi-method method user host
2247 (mapconcat 'identity
2248 (append '("") (reverse result
) (list thisstep
))
2250 (setq symlink-target
2251 (nth 0 (file-attributes
2252 (tramp-make-tramp-file-name
2253 multi-method method user host
2254 (mapconcat 'identity
2259 (cond ((string= "." thisstep
)
2260 (tramp-message-for-buffer multi-method method user host
2261 10 "Ignoring step `.'"))
2262 ((string= ".." thisstep
)
2263 (tramp-message-for-buffer multi-method method user host
2264 10 "Processing step `..'")
2266 ((stringp symlink-target
)
2267 ;; It's a symlink, follow it.
2268 (tramp-message-for-buffer
2269 multi-method method user host
2270 10 "Follow symlink to %s" symlink-target
)
2271 (setq numchase
(1+ numchase
))
2272 (when (file-name-absolute-p symlink-target
)
2274 ;; If the symlink was absolute, we'll get a string like
2275 ;; "/user@host:/some/target"; extract the
2276 ;; "/some/target" part from it.
2277 (when (tramp-tramp-file-p symlink-target
)
2278 (with-parsed-tramp-file-name symlink-target sym
2279 (unless (equal (list multi-method method user host
)
2280 (list sym-multi-method sym-method
2282 (error "Symlink target `%s' on wrong host"
2284 (setq symlink-target localname
)))
2286 (append (tramp-split-string symlink-target
"/") steps
)))
2289 (setq result
(cons thisstep result
)))))
2290 (when (>= numchase numchase-limit
)
2291 (error "Maximum number (%d) of symlinks exceeded" numchase-limit
))
2292 (setq result
(reverse result
))
2293 ;; Combine list to form string.
2296 (mapconcat 'identity
(cons "" result
) "/")
2298 (when (and is-dir
(or (string= "" result
)
2299 (not (string= (substring result -
1) "/"))))
2300 (setq result
(concat result
"/")))
2301 (tramp-message-for-buffer
2302 multi-method method user host
2303 10 "True name of `%s' is `%s'" filename result
)
2304 (tramp-make-tramp-file-name
2305 multi-method method user host result
))))
2309 (defun tramp-handle-file-exists-p (filename)
2310 "Like `file-exists-p' for tramp files."
2311 (with-parsed-tramp-file-name filename nil
2313 (zerop (tramp-send-command-and-check
2314 multi-method method user host
2316 (tramp-get-file-exists-command multi-method method user host
)
2317 (tramp-shell-quote-argument localname
)))))))
2319 ;; Devices must distinguish physical file systems. The device numbers
2320 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2321 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2322 ;; EFS use device number "-1". In order to be different, we use device number
2323 ;; (-1 x), whereby "x" is unique for a given (multi-method method user host).
2324 (defvar tramp-devices nil
2325 "Keeps virtual device numbers.")
2327 ;; CCC: This should check for an error condition and signal failure
2328 ;; when something goes wrong.
2329 ;; Daniel Pittman <daniel@danann.net>
2330 (defun tramp-handle-file-attributes (filename &optional id-format
)
2331 "Like `file-attributes' for tramp files."
2332 (when (file-exists-p filename
)
2333 ;; file exists, find out stuff
2334 (unless id-format
(setq id-format
'integer
))
2335 (with-parsed-tramp-file-name filename nil
2337 (tramp-convert-file-attributes
2338 multi-method method user host
2339 (if (tramp-get-remote-perl multi-method method user host
)
2340 (tramp-handle-file-attributes-with-perl multi-method method user host
2341 localname id-format
)
2342 (tramp-handle-file-attributes-with-ls multi-method method user host
2343 localname id-format
)))))))
2345 (defun tramp-handle-file-attributes-with-ls
2346 (multi-method method user host localname
&optional id-format
)
2347 "Implement `file-attributes' for tramp files using the ls(1) command."
2349 res-inode res-filemodes res-numlinks
2350 res-uid res-gid res-size res-symlink-target
)
2351 (tramp-message-for-buffer multi-method method user host
10
2352 "file attributes with ls: %s"
2353 (tramp-make-tramp-file-name
2354 multi-method method user host localname
))
2356 multi-method method user host
2358 (tramp-get-ls-command multi-method method user host
)
2359 (if (eq id-format
'integer
) "-ildn" "-ild")
2360 (tramp-shell-quote-argument localname
)))
2361 (tramp-wait-for-output)
2362 ;; parse `ls -l' output ...
2366 (read (current-buffer))
2367 (invalid-read-syntax
2368 (when (and (equal (cadr err
)
2369 "Integer constant overflow in reader")
2371 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2373 (let* ((big (read (substring (caddr err
) 0
2374 (match-beginning 1))))
2375 (small (read (match-string 1 (caddr err
))))
2376 (twiddle (/ small
65536)))
2377 (cons (+ big twiddle
)
2378 (- small
(* twiddle
65536))))))))
2379 ;; ... file mode flags
2380 (setq res-filemodes
(symbol-name (read (current-buffer))))
2382 (setq res-numlinks
(read (current-buffer)))
2384 (setq res-uid
(read (current-buffer)))
2385 (setq res-gid
(read (current-buffer)))
2386 (when (eq id-format
'integer
)
2387 (unless (numberp res-uid
) (setq res-uid -
1))
2388 (unless (numberp res-gid
) (setq res-gid -
1)))
2390 (setq res-size
(read (current-buffer)))
2391 ;; From the file modes, figure out other stuff.
2392 (setq symlinkp
(eq ?l
(aref res-filemodes
0)))
2393 (setq dirp
(eq ?d
(aref res-filemodes
0)))
2394 ;; if symlink, find out file name pointed to
2396 (search-forward "-> ")
2397 (setq res-symlink-target
2398 (buffer-substring (point)
2399 (tramp-line-end-position))))
2400 ;; return data gathered
2402 ;; 0. t for directory, string (name linked to) for symbolic
2404 (or dirp res-symlink-target nil
)
2405 ;; 1. Number of links to file.
2411 ;; 4. Last access time, as a list of two integers. First
2412 ;; integer has high-order 16 bits of time, second has low 16
2414 ;; 5. Last modification time, likewise.
2415 ;; 6. Last status change time, likewise.
2416 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2417 ;; 7. Size in bytes (-1, if number is out of range).
2419 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2421 ;; 9. t iff file's gid would change if file were deleted and
2422 ;; recreated. Will be set in `tramp-convert-file-attributes'
2424 ;; 10. inode number.
2426 ;; 11. Device number. Will be replaced by a virtual device number.
2430 (defun tramp-handle-file-attributes-with-perl
2431 (multi-method method user host localname
&optional id-format
)
2432 "Implement `file-attributes' for tramp files using a Perl script."
2433 (tramp-message-for-buffer multi-method method user host
10
2434 "file attributes with perl: %s"
2435 (tramp-make-tramp-file-name
2436 multi-method method user host localname
))
2437 (tramp-maybe-send-perl-script multi-method method user host
2438 tramp-perl-file-attributes
2439 "tramp_file_attributes")
2440 (tramp-send-command multi-method method user host
2441 (format "tramp_file_attributes %s %s"
2442 (tramp-shell-quote-argument localname
) id-format
))
2443 (tramp-wait-for-output)
2444 (read (current-buffer)))
2446 (defun tramp-handle-set-visited-file-modtime (&optional time-list
)
2447 "Like `set-visited-file-modtime' for tramp files."
2448 (unless (buffer-file-name)
2449 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2452 (tramp-run-real-handler 'set-visited-file-modtime
(list time-list
))
2453 (let ((f (buffer-file-name))
2455 (with-parsed-tramp-file-name f nil
2456 (let* ((attr (file-attributes f
))
2457 ;; '(-1 65535) means file doesn't exists yet.
2458 (modtime (or (nth 5 attr
) '(-1 65535))))
2459 (when (boundp 'last-coding-system-used
)
2460 (setq coding-system-used
(symbol-value 'last-coding-system-used
)))
2461 ;; We use '(0 0) as a don't-know value. See also
2462 ;; `tramp-handle-file-attributes-with-ls'.
2463 (if (not (equal modtime
'(0 0)))
2464 (tramp-run-real-handler 'set-visited-file-modtime
(list modtime
))
2467 multi-method method user host
2468 (format "%s -ild %s"
2469 (tramp-get-ls-command multi-method method user host
)
2470 (tramp-shell-quote-argument localname
)))
2471 (tramp-wait-for-output)
2472 (setq attr
(buffer-substring (point)
2473 (progn (end-of-line) (point)))))
2474 (setq tramp-buffer-file-attributes attr
))
2475 (when (boundp 'last-coding-system-used
)
2476 (set 'last-coding-system-used coding-system-used
))
2479 ;; CCC continue here
2481 ;; This function makes the same assumption as
2482 ;; `tramp-handle-set-visited-file-modtime'.
2483 (defun tramp-handle-verify-visited-file-modtime (buf)
2484 "Like `verify-visited-file-modtime' for tramp files.
2485 At the time `verify-visited-file-modtime' calls this function, we
2486 already know that the buffer is visiting a file and that
2487 `visited-file-modtime' does not return 0. Do not call this
2488 function directly, unless those two cases are already taken care
2490 (with-current-buffer buf
2491 ;; There is no file visiting the buffer, or the buffer has no
2492 ;; recorded last modification time.
2493 (if (or (not (buffer-file-name))
2494 (eq (visited-file-modtime) 0))
2496 (let ((f (buffer-file-name)))
2497 (with-parsed-tramp-file-name f nil
2498 (let* ((attr (file-attributes f
))
2499 (modtime (nth 5 attr
))
2500 (mt (visited-file-modtime)))
2503 ;; file exists, and has a known modtime.
2504 ((and attr
(not (equal modtime
'(0 0))))
2505 (< (abs (tramp-time-diff
2507 ;; For compatibility, deal with both the old
2508 ;; (HIGH . LOW) and the new (HIGH LOW)
2509 ;; return values of `visited-file-modtime'.
2511 (list (car mt
) (cdr mt
))
2514 ;; modtime has the don't know value.
2518 multi-method method user host
2519 (format "%s -ild %s"
2520 (tramp-get-ls-command multi-method method user host
)
2521 (tramp-shell-quote-argument localname
)))
2522 (tramp-wait-for-output)
2523 (setq attr
(buffer-substring
2524 (point) (progn (end-of-line) (point)))))
2525 (equal tramp-buffer-file-attributes attr
))
2526 ;; If file does not exist, say it is not modified
2527 ;; if and only if that agrees with the buffer's record.
2528 (t (equal mt
'(-1 65535))))))))))
2530 (defun tramp-handle-set-file-modes (filename mode
)
2531 "Like `set-file-modes' for tramp files."
2532 (with-parsed-tramp-file-name filename nil
2534 (unless (zerop (tramp-send-command-and-check
2535 multi-method method user host
2536 (format "chmod %s %s"
2537 (tramp-decimal-to-octal mode
)
2538 (tramp-shell-quote-argument localname
))))
2541 ;; FIXME: extract the proper text from chmod's stderr.
2542 "error while changing file's mode"
2545 ;; Simple functions using the `test' command.
2547 (defun tramp-handle-file-executable-p (filename)
2548 "Like `file-executable-p' for tramp files."
2549 (with-parsed-tramp-file-name filename nil
2550 (zerop (tramp-run-test "-x" filename
))))
2552 (defun tramp-handle-file-readable-p (filename)
2553 "Like `file-readable-p' for tramp files."
2554 (with-parsed-tramp-file-name filename nil
2555 (zerop (tramp-run-test "-r" filename
))))
2557 (defun tramp-handle-file-accessible-directory-p (filename)
2558 "Like `file-accessible-directory-p' for tramp files."
2559 (with-parsed-tramp-file-name filename nil
2560 (and (zerop (tramp-run-test "-d" filename
))
2561 (zerop (tramp-run-test "-r" filename
))
2562 (zerop (tramp-run-test "-x" filename
)))))
2564 ;; When the remote shell is started, it looks for a shell which groks
2565 ;; tilde expansion. Here, we assume that all shells which grok tilde
2566 ;; expansion will also provide a `test' command which groks `-nt' (for
2567 ;; newer than). If this breaks, tell me about it and I'll try to do
2568 ;; something smarter about it.
2569 (defun tramp-handle-file-newer-than-file-p (file1 file2
)
2570 "Like `file-newer-than-file-p' for tramp files."
2571 (cond ((not (file-exists-p file1
))
2573 ((not (file-exists-p file2
))
2575 ;; We are sure both files exist at this point.
2578 ;; We try to get the mtime of both files. If they are not
2579 ;; equal to the "dont-know" value, then we subtract the times
2580 ;; and obtain the result.
2581 (let ((fa1 (file-attributes file1
))
2582 (fa2 (file-attributes file2
)))
2583 (if (and (not (equal (nth 5 fa1
) '(0 0)))
2584 (not (equal (nth 5 fa2
) '(0 0))))
2585 (> 0 (tramp-time-diff (nth 5 fa2
) (nth 5 fa1
)))
2586 ;; If one of them is the dont-know value, then we can
2587 ;; still try to run a shell command on the remote host.
2588 ;; However, this only works if both files are Tramp
2589 ;; files and both have the same method, same user, same
2591 (unless (and (tramp-tramp-file-p file1
)
2592 (tramp-tramp-file-p file2
))
2596 "Cannot check if Tramp file is newer than non-Tramp file"
2598 (with-parsed-tramp-file-name file1 v1
2599 (with-parsed-tramp-file-name file2 v2
2600 (unless (and (equal v1-multi-method v2-multi-method
)
2601 (equal v1-method v2-method
)
2602 (equal v1-user v2-user
)
2603 (equal v1-host v2-host
))
2605 (list "Files must have same method, user, host"
2607 (unless (and (tramp-tramp-file-p file1
)
2608 (tramp-tramp-file-p file2
))
2610 (list "Files must be tramp files on same host"
2612 (if (tramp-get-test-groks-nt
2613 v1-multi-method v1-method v1-user v1-host
)
2614 (zerop (tramp-run-test2 "test" file1 file2
"-nt"))
2615 (zerop (tramp-run-test2
2616 "tramp_test_nt" file1 file2
)))))))))))
2618 ;; Functions implemented using the basic functions above.
2620 (defun tramp-handle-file-modes (filename)
2621 "Like `file-modes' for tramp files."
2622 (with-parsed-tramp-file-name filename nil
2623 (when (file-exists-p filename
)
2624 (tramp-mode-string-to-int
2625 (nth 8 (file-attributes filename
))))))
2627 (defun tramp-handle-file-directory-p (filename)
2628 "Like `file-directory-p' for tramp files."
2629 ;; Care must be taken that this function returns `t' for symlinks
2630 ;; pointing to directories. Surely the most obvious implementation
2631 ;; would be `test -d', but that returns false for such symlinks.
2632 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
2633 ;; I now think he's right. So we could be using `test -d', couldn't
2636 ;; Alternatives: `cd %s', `test -d %s'
2637 (with-parsed-tramp-file-name filename nil
2640 (tramp-send-command-and-check
2641 multi-method method user host
2642 (format "test -d %s"
2643 (tramp-shell-quote-argument localname
))
2644 t
))))) ;run command in subshell
2646 (defun tramp-handle-file-regular-p (filename)
2647 "Like `file-regular-p' for tramp files."
2648 (with-parsed-tramp-file-name filename nil
2649 (and (file-exists-p filename
)
2650 (eq ?-
(aref (nth 8 (file-attributes filename
)) 0)))))
2652 (defun tramp-handle-file-symlink-p (filename)
2653 "Like `file-symlink-p' for tramp files."
2654 (with-parsed-tramp-file-name filename nil
2655 (let ((x (car (file-attributes filename
))))
2657 ;; When Tramp is running on VMS, then `file-name-absolute-p'
2658 ;; might do weird things.
2659 (if (file-name-absolute-p x
)
2660 (tramp-make-tramp-file-name
2661 multi-method method user host x
)
2664 (defun tramp-handle-file-writable-p (filename)
2665 "Like `file-writable-p' for tramp files."
2666 (with-parsed-tramp-file-name filename nil
2667 (if (file-exists-p filename
)
2668 ;; Existing files must be writable.
2669 (zerop (tramp-run-test "-w" filename
))
2670 ;; If file doesn't exist, check if directory is writable.
2671 (and (zerop (tramp-run-test
2672 "-d" (file-name-directory filename
)))
2673 (zerop (tramp-run-test
2674 "-w" (file-name-directory filename
)))))))
2676 (defun tramp-handle-file-ownership-preserved-p (filename)
2677 "Like `file-ownership-preserved-p' for tramp files."
2678 (with-parsed-tramp-file-name filename nil
2679 (let ((attributes (file-attributes filename
)))
2680 ;; Return t if the file doesn't exist, since it's true that no
2681 ;; information would be lost by an (attempted) delete and create.
2682 (or (null attributes
)
2683 (= (nth 2 attributes
)
2684 (tramp-get-remote-uid multi-method method user host
))))))
2686 ;; Other file name ops.
2688 ;; ;; Matthias K\e,Av\e(Bppe <mkoeppe@mail.math.uni-magdeburg.de>
2689 ;; (defun tramp-handle-directory-file-name (directory)
2690 ;; "Like `directory-file-name' for tramp files."
2691 ;; (if (and (eq (aref directory (- (length directory) 1)) ?/)
2692 ;; (not (eq (aref directory (- (length directory) 2)) ?:)))
2693 ;; (substring directory 0 (- (length directory) 1))
2696 ;; ;; Philippe Troin <phil@fifi.org>
2697 ;; (defun tramp-handle-directory-file-name (directory)
2698 ;; "Like `directory-file-name' for tramp files."
2699 ;; (with-parsed-tramp-file-name directory nil
2700 ;; (let ((directory-length-1 (1- (length directory))))
2702 ;; (if (and (eq (aref directory directory-length-1) ?/)
2703 ;; (eq (string-match tramp-file-name-regexp directory) 0)
2704 ;; (/= (match-end 0) directory-length-1))
2705 ;; (substring directory 0 directory-length-1)
2708 (defun tramp-handle-directory-file-name (directory)
2709 "Like `directory-file-name' for tramp files."
2710 ;; If localname component of filename is "/", leave it unchanged.
2711 ;; Otherwise, remove any trailing slash from localname component.
2712 ;; Method, host, etc, are unchanged. Does it make sense to try
2713 ;; to avoid parsing the filename?
2714 (with-parsed-tramp-file-name directory nil
2715 (if (and (not (zerop (length localname
)))
2716 (eq (aref localname
(1- (length localname
))) ?
/)
2717 (not (string= localname
"/")))
2718 (substring directory
0 -
1)
2721 ;; Directory listings.
2723 (defun tramp-handle-directory-files (directory
2724 &optional full match nosort files-only
)
2725 "Like `directory-files' for tramp files."
2726 (with-parsed-tramp-file-name directory nil
2729 (tramp-barf-unless-okay
2730 multi-method method user host
2731 (concat "cd " (tramp-shell-quote-argument localname
))
2734 "tramp-handle-directory-files: couldn't `cd %s'"
2735 (tramp-shell-quote-argument localname
))
2737 multi-method method user host
2738 (concat (tramp-get-ls-command multi-method method user host
)
2740 (tramp-wait-for-output)
2741 (goto-char (point-max))
2742 (while (zerop (forward-line -
1))
2743 (setq x
(buffer-substring (point)
2744 (tramp-line-end-position)))
2745 (when (or (not match
) (string-match match x
))
2747 (push (concat (file-name-as-directory directory
)
2751 (tramp-send-command multi-method method user host
"cd")
2752 (tramp-wait-for-output)
2753 ;; Remove non-files or non-directories if necessary. Using
2754 ;; the remote shell for this would probably be way faster.
2755 ;; Maybe something could be adapted from
2756 ;; tramp-handle-file-name-all-completions.
2758 (let ((temp (nreverse result
))
2761 (if (equal files-only t
)
2764 (setq item
(pop temp
))
2765 (when (file-regular-p item
)
2766 (push item result
)))
2769 (setq item
(pop temp
))
2770 (when (file-directory-p item
)
2771 (push item result
)))))))
2774 (defun tramp-handle-directory-files-and-attributes
2775 (directory &optional full match nosort id-format
)
2776 "Like `directory-files-and-attributes' for tramp files."
2777 (when (tramp-handle-file-exists-p directory
)
2779 (setq directory
(tramp-handle-expand-file-name directory
))
2780 (with-parsed-tramp-file-name directory nil
2781 (tramp-maybe-send-perl-script multi-method method user host
2782 tramp-perl-directory-files-and-attributes
2783 "tramp_directory_files_and_attributes")
2784 (tramp-send-command multi-method method user host
2785 (format "tramp_directory_files_and_attributes %s %s"
2786 (tramp-shell-quote-argument localname
)
2787 (or id-format
'integer
)))
2788 (tramp-wait-for-output)
2789 (let* ((root (cons nil
(let ((object (read (current-buffer))))
2790 (when (stringp object
)
2795 (if (and match
(not (string-match match
(caadr cell
))))
2797 (setcdr cell
(cddr cell
))
2799 (setq cell
(cdr cell
))
2800 (let ((l (car cell
)))
2801 (tramp-convert-file-attributes multi-method method user host
2803 ;; If FULL, make file name absolute
2804 (when full
(setcar l
(concat directory
"/" (car l
)))))))
2807 (sort (cdr root
) (lambda (x y
) (string< (car x
) (car y
))))))))))
2809 ;; This function should return "foo/" for directories and "bar" for
2810 ;; files. We use `ls -ad' to get a list of files (including
2811 ;; directories), and `find . -type d \! -name . -prune' to get a list
2813 (defun tramp-handle-file-name-all-completions (filename directory
)
2814 "Like `file-name-all-completions' for tramp files."
2815 (with-parsed-tramp-file-name directory nil
2816 (unless (save-match-data (string-match "/" filename
))
2817 (let* ((nowild tramp-completion-without-shell-p
)
2820 (tramp-barf-unless-okay
2821 multi-method method user host
2822 (format "cd %s" (tramp-shell-quote-argument localname
))
2824 "tramp-handle-file-name-all-completions: Couldn't `cd %s'"
2825 (tramp-shell-quote-argument localname
))
2827 ;; Get a list of directories and files, including reliably
2828 ;; tagging the directories with a trailing '/'. Because I
2829 ;; rock. --daniel@danann.net
2831 multi-method method user host
2832 (format (concat "%s -a %s 2>/dev/null | while read f; do "
2833 "if test -d \"$f\" 2>/dev/null; "
2834 "then echo \"$f/\"; else echo \"$f\"; fi; done")
2835 (tramp-get-ls-command multi-method method user host
)
2836 (if (or nowild
(zerop (length filename
)))
2839 (tramp-shell-quote-argument filename
)))))
2841 ;; Now grab the output.
2842 (tramp-wait-for-output)
2843 (goto-char (point-max))
2844 (while (zerop (forward-line -
1))
2845 (push (buffer-substring (point)
2846 (tramp-line-end-position))
2849 (tramp-send-command multi-method method user host
"cd")
2850 (tramp-wait-for-output)
2854 (all-completions filename
(mapcar 'list result
))
2858 ;; The following isn't needed for Emacs 20 but for 19.34?
2859 (defun tramp-handle-file-name-completion
2860 (filename directory
&optional predicate
)
2861 "Like `file-name-completion' for tramp files."
2862 (unless (tramp-tramp-file-p directory
)
2864 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2866 (with-parsed-tramp-file-name directory nil
2869 (mapcar (lambda (x) (cons x nil
))
2870 (file-name-all-completions filename directory
))
2875 (defun tramp-handle-add-name-to-file
2876 (filename newname
&optional ok-if-already-exists
)
2877 "Like `add-name-to-file' for tramp files."
2878 (with-parsed-tramp-file-name filename v1
2879 (with-parsed-tramp-file-name newname v2
2880 (let ((ln (when v1
(tramp-get-remote-ln
2881 v1-multi-method v1-method v1-user v1-host
))))
2882 (unless (and v1-method v2-method v1-user v2-user v1-host v2-host
2883 (equal v1-multi-method v2-multi-method
)
2884 (equal v1-method v2-method
)
2885 (equal v1-user v2-user
)
2886 (equal v1-host v2-host
))
2887 (error "add-name-to-file: %s"
2888 "only implemented for same method, same user, same host"))
2889 (when (and (not ok-if-already-exists
)
2890 (file-exists-p newname
)
2891 (not (numberp ok-if-already-exists
))
2894 "File %s already exists; make it a new name anyway? "
2896 (error "add-name-to-file: file %s already exists" newname
))
2897 (tramp-barf-unless-okay
2898 v1-multi-method v1-method v1-user v1-host
2899 (format "%s %s %s" ln
(tramp-shell-quote-argument v1-localname
)
2900 (tramp-shell-quote-argument v2-localname
))
2902 "error with add-name-to-file, see buffer `%s' for details"
2905 (defun tramp-handle-copy-file
2906 (filename newname
&optional ok-if-already-exists keep-date
)
2907 "Like `copy-file' for tramp files."
2908 ;; Check if both files are local -- invoke normal copy-file.
2909 ;; Otherwise, use tramp from local system.
2910 (setq filename
(expand-file-name filename
))
2911 (setq newname
(expand-file-name newname
))
2912 ;; At least one file a tramp file?
2913 (if (or (tramp-tramp-file-p filename
)
2914 (tramp-tramp-file-p newname
))
2915 (tramp-do-copy-or-rename-file
2916 'copy filename newname ok-if-already-exists keep-date
)
2917 (tramp-run-real-handler
2919 (list filename newname ok-if-already-exists keep-date
))))
2921 (defun tramp-handle-rename-file
2922 (filename newname
&optional ok-if-already-exists
)
2923 "Like `rename-file' for tramp files."
2924 ;; Check if both files are local -- invoke normal rename-file.
2925 ;; Otherwise, use tramp from local system.
2926 (setq filename
(expand-file-name filename
))
2927 (setq newname
(expand-file-name newname
))
2928 ;; At least one file a tramp file?
2929 (if (or (tramp-tramp-file-p filename
)
2930 (tramp-tramp-file-p newname
))
2931 (tramp-do-copy-or-rename-file
2932 'rename filename newname ok-if-already-exists
)
2933 (tramp-run-real-handler 'rename-file
2934 (list filename newname ok-if-already-exists
))))
2936 (defun tramp-do-copy-or-rename-file
2937 (op filename newname
&optional ok-if-already-exists keep-date
)
2938 "Copy or rename a remote file.
2939 OP must be `copy' or `rename' and indicates the operation to perform.
2940 FILENAME specifies the file to copy or rename, NEWNAME is the name of
2941 the new file (for copy) or the new name of the file (for rename).
2942 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
2943 KEEP-DATE means to make sure that NEWNAME has the same timestamp
2946 This function is invoked by `tramp-handle-copy-file' and
2947 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
2948 and `rename'. FILENAME and NEWNAME must be absolute file names."
2949 (unless (memq op
'(copy rename
))
2950 (error "Unknown operation `%s', must be `copy' or `rename'" op
))
2951 (unless ok-if-already-exists
2952 (when (file-exists-p newname
)
2953 (signal 'file-already-exists
2954 (list "File already exists" newname
))))
2955 (let ((t1 (tramp-tramp-file-p filename
))
2956 (t2 (tramp-tramp-file-p newname
))
2957 v1-multi-method v1-method v1-user v1-host v1-localname
2958 v2-multi-method v2-method v2-user v2-host v2-localname
)
2960 ;; Check which ones of source and target are Tramp files.
2961 ;; We cannot invoke `with-parsed-tramp-file-name';
2962 ;; it fails if the file isn't a Tramp file name.
2964 (with-parsed-tramp-file-name filename l
2965 (setq v1-multi-method l-multi-method
2969 v1-localname l-localname
))
2970 (setq v1-localname filename
))
2972 (with-parsed-tramp-file-name newname l
2973 (setq v2-multi-method l-multi-method
2977 v2-localname l-localname
))
2978 (setq v2-localname newname
))
2981 ;; Both are Tramp files.
2984 ;; Shortcut: if method, host, user are the same for both
2985 ;; files, we invoke `cp' or `mv' on the remote host
2987 ((and (equal v1-multi-method v2-multi-method
)
2988 (equal v1-method v2-method
)
2989 (equal v1-user v2-user
)
2990 (equal v1-host v2-host
))
2991 (tramp-do-copy-or-rename-file-directly
2992 op v1-multi-method v1-method v1-user v1-host
2993 v1-localname v2-localname keep-date
))
2994 ;; If both source and target are Tramp files,
2995 ;; both are using the same copy-program, then we
2996 ;; can invoke rcp directly. Note that
2997 ;; default-directory should point to a local
2998 ;; directory if we want to invoke rcp.
2999 ((and (not v1-multi-method
)
3000 (not v2-multi-method
)
3001 (equal v1-method v2-method
)
3002 (tramp-method-out-of-band-p
3003 v1-multi-method v1-method v1-user v1-host
)
3004 (not (string-match "\\([^#]*\\)#\\(.*\\)" v1-host
))
3005 (not (string-match "\\([^#]*\\)#\\(.*\\)" v2-host
)))
3006 (tramp-do-copy-or-rename-file-out-of-band
3007 op filename newname keep-date
))
3008 ;; No shortcut was possible. So we copy the
3009 ;; file first. If the operation was `rename', we go
3010 ;; back and delete the original file (if the copy was
3011 ;; successful). The approach is simple-minded: we
3012 ;; create a new buffer, insert the contents of the
3013 ;; source file into it, then write out the buffer to
3014 ;; the target file. The advantage is that it doesn't
3015 ;; matter which filename handlers are used for the
3016 ;; source and target file.
3018 (tramp-do-copy-or-rename-file-via-buffer
3019 op filename newname keep-date
))))
3021 ;; One file is a Tramp file, the other one is local.
3023 ;; If the Tramp file has an out-of-band method, the corresponding
3024 ;; copy-program can be invoked.
3025 (if (and (not v1-multi-method
)
3026 (not v2-multi-method
)
3027 (or (and t1
(tramp-method-out-of-band-p
3028 v1-multi-method v1-method v1-user v1-host
))
3029 (and t2
(tramp-method-out-of-band-p
3030 v2-multi-method v2-method v2-user v2-host
))))
3031 (tramp-do-copy-or-rename-file-out-of-band
3032 op filename newname keep-date
)
3033 ;; Use the generic method via a Tramp buffer.
3034 (tramp-do-copy-or-rename-file-via-buffer
3035 op filename newname keep-date
)))
3038 ;; One of them must be a Tramp file.
3039 (error "Tramp implementation says this cannot happen")))))
3041 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date
)
3042 "Use an Emacs buffer to copy or rename a file.
3043 First arg OP is either `copy' or `rename' and indicates the operation.
3044 FILENAME is the source file, NEWNAME the target file.
3045 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
3046 (let ((trampbuf (get-buffer-create "*tramp output*"))
3047 (modtime (nth 5 (file-attributes filename
))))
3048 (when (and keep-date
(or (null modtime
) (equal modtime
'(0 0))))
3050 1 (concat "Warning: cannot preserve file time stamp"
3051 " with inline copying across machines")))
3053 (set-buffer trampbuf
) (erase-buffer)
3054 (insert-file-contents-literally filename
)
3055 ;; We don't want the target file to be compressed, so we let-bind
3056 ;; `jka-compr-inhibit' to t.
3057 (let ((coding-system-for-write 'binary
)
3058 (jka-compr-inhibit t
))
3059 (write-region (point-min) (point-max) newname
))
3060 ;; KEEP-DATE handling.
3062 (when (and (not (null modtime
))
3063 (not (equal modtime
'(0 0))))
3064 (tramp-touch newname modtime
)))
3066 (set-file-modes newname
(file-modes filename
)))
3067 ;; If the operation was `rename', delete the original file.
3068 (unless (eq op
'copy
)
3069 (delete-file filename
))))
3071 (defun tramp-do-copy-or-rename-file-directly
3072 (op multi-method method user host localname1 localname2 keep-date
)
3073 "Invokes `cp' or `mv' on the remote system.
3074 OP must be one of `copy' or `rename', indicating `cp' or `mv',
3075 respectively. METHOD, USER, and HOST specify the connection.
3076 LOCALNAME1 and LOCALNAME2 specify the two arguments of `cp' or `mv'.
3077 If KEEP-DATE is non-nil, preserve the time stamp when copying."
3078 ;; CCC: What happens to the timestamp when renaming?
3079 (let ((cmd (cond ((and (eq op
'copy
) keep-date
) "cp -f -p")
3080 ((eq op
'copy
) "cp -f")
3081 ((eq op
'rename
) "mv -f")
3083 "Unknown operation `%s', must be `copy' or `rename'"
3087 multi-method method user host
3090 (tramp-shell-quote-argument localname1
)
3091 (tramp-shell-quote-argument localname2
)))
3092 (tramp-wait-for-output)
3093 (goto-char (point-min))
3096 (and (eq op
'copy
) keep-date
3097 ;; Mask cp -f error.
3098 (re-search-forward tramp-operation-not-permitted-regexp nil t
))
3099 (zerop (tramp-send-command-and-check
3100 multi-method method user host nil nil
)))
3101 (pop-to-buffer (current-buffer))
3103 (format "Copying directly failed, see buffer `%s' for details."
3106 ;; CCC: Maybe `chmod --reference=localname1 localname2' could be used
3108 (unless (or (eq op
'rename
) keep-date
)
3110 (tramp-make-tramp-file-name multi-method method user host localname2
)
3112 (tramp-make-tramp-file-name
3113 multi-method method user host localname1
))))))
3115 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date
)
3116 "Invoke rcp program to copy.
3117 One of FILENAME and NEWNAME must be a Tramp name, the other must
3118 be a local filename. The method used must be an out-of-band method."
3119 (let ((t1 (tramp-tramp-file-p filename
))
3120 (t2 (tramp-tramp-file-p newname
))
3121 v1-multi-method v1-method v1-user v1-host v1-localname
3122 v2-multi-method v2-method v2-user v2-host v2-localname
3123 multi-method method user host copy-program copy-args
3124 source target trampbuf
)
3126 ;; Check which ones of source and target are Tramp files.
3127 ;; We cannot invoke `with-parsed-tramp-file-name';
3128 ;; it fails if the file isn't a Tramp file name.
3130 (with-parsed-tramp-file-name filename l
3131 (setq v1-multi-method l-multi-method
3135 v1-localname l-localname
3136 multi-method l-multi-method
3137 method
(tramp-find-method
3138 v1-multi-method v1-method v1-user v1-host
)
3141 copy-program
(tramp-get-method-parameter
3142 v1-multi-method method
3143 v1-user v1-host
'tramp-copy-program
)
3144 copy-args
(tramp-get-method-parameter
3145 v1-multi-method method
3146 v1-user v1-host
'tramp-copy-args
)))
3147 (setq v1-localname filename
))
3150 (with-parsed-tramp-file-name newname l
3151 (setq v2-multi-method l-multi-method
3155 v2-localname l-localname
3156 multi-method l-multi-method
3157 method
(tramp-find-method
3158 v2-multi-method v2-method v2-user v2-host
)
3161 copy-program
(tramp-get-method-parameter
3162 v2-multi-method method
3163 v2-user v2-host
'tramp-copy-program
)
3164 copy-args
(tramp-get-method-parameter
3165 v2-multi-method method
3166 v2-user v2-host
'tramp-copy-args
)))
3167 (setq v2-localname newname
))
3169 ;; The following should be changed. We need a more general
3170 ;; mechanism to parse extra host args.
3172 (setq source v1-localname
)
3173 (when (string-match "\\([^#]*\\)#\\(.*\\)" v1-host
)
3174 (setq copy-args
(cons "-P" (cons (match-string 2 v1-host
) copy-args
)))
3175 (setq v1-host
(match-string 1 v1-host
)))
3177 (tramp-make-copy-program-file-name
3179 (tramp-shell-quote-argument v1-localname
))))
3182 (setq target v2-localname
)
3183 (when (string-match "\\([^#]*\\)#\\(.*\\)" v2-host
)
3184 (setq copy-args
(cons "-P" (cons (match-string 2 v2-host
) copy-args
)))
3185 (setq v2-host
(match-string 1 v2-host
)))
3187 (tramp-make-copy-program-file-name
3189 (tramp-shell-quote-argument v2-localname
))))
3191 ;; Handle ControlMaster/ControlPath
3196 x
`((?t .
,(format "/tmp/%s" tramp-temp-name-prefix
)))))
3199 ;; Handle keep-date argument
3203 (cons (tramp-get-method-parameter
3204 v1-multi-method method
3205 v1-user v1-host
'tramp-copy-keep-date-arg
)
3208 (cons (tramp-get-method-parameter
3209 v2-multi-method method
3210 v2-user v2-host
'tramp-copy-keep-date-arg
)
3213 (setq copy-args
(append copy-args
(list source target
))
3214 trampbuf
(generate-new-buffer
3215 (tramp-buffer-name multi-method method user host
)))
3217 ;; Use an asynchronous process. By this, password can be handled.
3220 ;; Check for program.
3221 (when (and (fboundp 'executable-find
)
3222 (not (executable-find copy-program
)))
3223 (error "Cannot find copy program: %s" copy-program
))
3225 (set-buffer trampbuf
)
3226 (setq tramp-current-multi-method multi-method
3227 tramp-current-method method
3228 tramp-current-user user
3229 tramp-current-host host
)
3230 (message "Transferring %s to %s..." filename newname
)
3232 ;; Use rcp-like program for file transfer.
3234 (let ((p (apply 'start-process
(buffer-name trampbuf
) trampbuf
3235 copy-program copy-args
)))
3236 (tramp-set-process-query-on-exit-flag p nil
)
3237 (tramp-process-actions p multi-method method user host
3238 tramp-actions-copy-out-of-band
))
3239 (kill-buffer trampbuf
))
3240 (message "Transferring %s to %s...done" filename newname
)
3244 (set-file-modes newname
(file-modes filename
))))
3246 ;; If the operation was `rename', delete the original file.
3247 (unless (eq op
'copy
)
3248 (delete-file filename
))))
3251 (defun tramp-handle-make-directory (dir &optional parents
)
3252 "Like `make-directory' for tramp files."
3253 (setq dir
(expand-file-name dir
))
3254 (with-parsed-tramp-file-name dir nil
3256 (tramp-barf-unless-okay
3257 multi-method method user host
3259 (if parents
"mkdir -p" "mkdir")
3260 (tramp-shell-quote-argument localname
))
3262 "Couldn't make directory %s" dir
))))
3264 ;; CCC error checking?
3265 (defun tramp-handle-delete-directory (directory)
3266 "Like `delete-directory' for tramp files."
3267 (setq directory
(expand-file-name directory
))
3268 (with-parsed-tramp-file-name directory nil
3271 multi-method method user host
3272 (format "rmdir %s ; echo ok"
3273 (tramp-shell-quote-argument localname
)))
3274 (tramp-wait-for-output))))
3276 (defun tramp-handle-delete-file (filename)
3277 "Like `delete-file' for tramp files."
3278 (setq filename
(expand-file-name filename
))
3279 (with-parsed-tramp-file-name filename nil
3281 (unless (zerop (tramp-send-command-and-check
3282 multi-method method user host
3284 (tramp-shell-quote-argument localname
))))
3285 (signal 'file-error
"Couldn't delete Tramp file")))))
3289 ;; CCC: This does not seem to be enough. Something dies when
3290 ;; we try and delete two directories under TRAMP :/
3291 (defun tramp-handle-dired-recursive-delete-directory (filename)
3292 "Recursively delete the directory given.
3293 This is like `dired-recursive-delete-directory' for tramp files."
3294 (with-parsed-tramp-file-name filename nil
3295 ;; run a shell command 'rm -r <localname>'
3296 ;; Code shamelessly stolen for the dired implementation and, um, hacked :)
3297 (or (file-exists-p filename
)
3300 (list "Removing old file name" "no such directory" filename
)))
3301 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
3302 (tramp-send-command multi-method method user host
3303 (format "rm -r %s" (tramp-shell-quote-argument localname
)))
3304 ;; Wait for the remote system to return to us...
3305 ;; This might take a while, allow it plenty of time.
3306 (tramp-wait-for-output 120)
3307 ;; Make sure that it worked...
3308 (and (file-exists-p filename
)
3309 (error "Failed to recursively delete %s" filename
))))
3311 (defun tramp-handle-dired-call-process (program discard
&rest arguments
)
3312 "Like `dired-call-process' for tramp files."
3313 (with-parsed-tramp-file-name default-directory nil
3315 (tramp-barf-unless-okay
3316 multi-method method user host
3317 (format "cd %s" (tramp-shell-quote-argument localname
))
3319 "tramp-handle-dired-call-process: Couldn't `cd %s'"
3320 (tramp-shell-quote-argument localname
))
3322 multi-method method user host
3323 (mapconcat #'tramp-shell-quote-argument
(cons program arguments
) " "))
3324 (tramp-wait-for-output))
3326 ;; We cannot use `insert-buffer' because the tramp buffer
3327 ;; changes its contents before insertion due to calling
3328 ;; `expand-file' and alike.
3330 (with-current-buffer
3331 (tramp-get-buffer multi-method method user host
)
3335 (tramp-send-command-and-check multi-method method user host nil
)
3336 (tramp-send-command multi-method method user host
"cd")
3337 (tramp-wait-for-output)))))
3339 (defun tramp-handle-dired-compress-file (file &rest ok-flag
)
3340 "Like `dired-compress-file' for tramp files."
3341 ;; OK-FLAG is valid for XEmacs only, but not implemented.
3342 ;; Code stolen mainly from dired-aux.el.
3343 (with-parsed-tramp-file-name file nil
3346 (if (not (featurep 'xemacs
))
3348 (symbol-value 'dired-compress-file-suffixes
)
3349 ;; XEmacs has `dired-compression-method-alist', which is
3350 ;; transformed into `dired-compress-file-suffixes' structure.
3353 (list (concat (regexp-quote (nth 1 x
)) "\\'")
3355 (mapconcat 'identity
(nth 3 x
) " ")))
3356 (symbol-value 'dired-compression-method-alist
))))
3358 ;; See if any suffix rule matches this file name.
3360 (let (case-fold-search)
3361 (if (string-match (car (car suffixes
)) localname
)
3362 (setq suffix
(car suffixes
) suffixes nil
))
3363 (setq suffixes
(cdr suffixes
))))
3365 (cond ((file-symlink-p file
)
3367 ((and suffix
(nth 2 suffix
))
3368 ;; We found an uncompression rule.
3369 (message "Uncompressing %s..." file
)
3370 (when (zerop (tramp-send-command-and-check
3371 multi-method method user host
3372 (concat (nth 2 suffix
) " " localname
)))
3373 (message "Uncompressing %s...done" file
)
3374 ;; `dired-remove-file' is not defined in XEmacs
3375 (funcall (symbol-function 'dired-remove-file
) file
)
3376 (string-match (car suffix
) file
)
3377 (concat (substring file
0 (match-beginning 0)))))
3379 ;; We don't recognize the file as compressed, so compress it.
3381 (message "Compressing %s..." file
)
3382 (when (zerop (tramp-send-command-and-check
3383 multi-method method user host
3384 (concat "gzip -f " localname
)))
3385 (message "Compressing %s...done" file
)
3386 ;; `dired-remove-file' is not defined in XEmacs
3387 (funcall (symbol-function 'dired-remove-file
) file
)
3388 (cond ((file-exists-p (concat file
".gz"))
3389 (concat file
".gz"))
3390 ((file-exists-p (concat file
".z"))
3394 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
3395 ;; not sure at all that this is the right way to do it, but let's hope
3396 ;; it works for now, and wait for a guru to point out the Right Way to
3398 ;;(eval-when-compile
3399 ;; (unless (fboundp 'dired-insert-set-properties)
3400 ;; (fset 'dired-insert-set-properties 'ignore)))
3401 ;; Gerd suggests this:
3402 (eval-when-compile (require 'dired
))
3403 ;; Note that dired is required at run-time, too, when it is needed.
3404 ;; It is only needed on XEmacs for the function
3405 ;; `dired-insert-set-properties'.
3407 (defun tramp-handle-insert-directory
3408 (filename switches
&optional wildcard full-directory-p
)
3409 "Like `insert-directory' for tramp files."
3410 (if (and (boundp 'ls-lisp-use-insert-directory-program
)
3411 (not (symbol-value 'ls-lisp-use-insert-directory-program
)))
3412 (tramp-run-real-handler 'insert-directory
3413 (list filename switches wildcard full-directory-p
))
3414 ;; For the moment, we assume that the remote "ls" program does not
3415 ;; grok "--dired". In the future, we should detect this on
3416 ;; connection setup.
3417 (when (string-match "^--dired\\s-+" switches
)
3418 (setq switches
(replace-match "" nil t switches
)))
3419 (setq filename
(expand-file-name filename
))
3420 (with-parsed-tramp-file-name filename nil
3421 (tramp-message-for-buffer
3422 multi-method method user host
10
3423 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
3424 switches filename
(if wildcard
"yes" "no")
3425 (if full-directory-p
"yes" "no"))
3427 (setq wildcard
(file-name-nondirectory localname
))
3428 (setq localname
(file-name-directory localname
)))
3429 (when (listp switches
)
3430 (setq switches
(mapconcat 'identity switches
" ")))
3431 (unless full-directory-p
3432 (setq switches
(concat "-d " switches
)))
3434 (setq switches
(concat switches
" " wildcard
)))
3436 ;; If `full-directory-p', we just say `ls -l FILENAME'.
3437 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
3438 (if full-directory-p
3440 multi-method method user host
3442 (tramp-get-ls-command multi-method method user host
)
3446 (tramp-shell-quote-argument (concat localname
".")))))
3447 (tramp-barf-unless-okay
3448 multi-method method user host
3449 (format "cd %s" (tramp-shell-quote-argument
3450 (file-name-directory localname
)))
3453 (tramp-shell-quote-argument (file-name-directory localname
)))
3455 multi-method method user host
3457 (tramp-get-ls-command multi-method method user host
)
3461 (if (zerop (length (file-name-nondirectory localname
)))
3463 (tramp-shell-quote-argument
3464 (file-name-nondirectory localname
)))))))
3465 (sit-for 1) ;needed for rsh but not ssh?
3466 (tramp-wait-for-output))
3467 ;; The following let-binding is used by code that's commented
3468 ;; out. Let's leave the let-binding in for a while to see
3469 ;; that the commented-out code is really not needed. Commenting-out
3470 ;; happened on 2003-03-13.
3471 (let ((old-pos (point)))
3472 ;; We cannot use `insert-buffer' because the tramp buffer
3473 ;; changes its contents before insertion due to calling
3474 ;; `expand-file' and alike.
3476 (with-current-buffer
3477 (tramp-get-buffer multi-method method user host
)
3479 ;; On XEmacs, we want to call (exchange-point-and-mark t), but
3480 ;; that doesn't exist on Emacs, so we use this workaround instead.
3481 ;; Since zmacs-region-stays doesn't exist in Emacs, this ought to
3482 ;; be safe. Thanks to Daniel Pittman <daniel@danann.net>.
3483 ;; (let ((zmacs-region-stays t))
3484 ;; (exchange-point-and-mark))
3486 (tramp-send-command multi-method method user host
"cd")
3487 (tramp-wait-for-output))
3488 ;; For the time being, the XEmacs kludge is commented out.
3489 ;; Please test it on various XEmacs versions to see if it works.
3490 ;; ;; Another XEmacs specialty follows. What's the right way to do
3492 ;; (when (and (featurep 'xemacs)
3493 ;; (eq major-mode 'dired-mode))
3496 ;; (dired-insert-set-properties old-pos (point))))
3499 ;; Continuation of kluge to pacify byte-compiler.
3500 ;;(eval-when-compile
3501 ;; (when (eq (symbol-function 'dired-insert-set-properties) 'ignore)
3502 ;; (fmakunbound 'dired-insert-set-properties)))
3504 ;; CCC is this the right thing to do?
3505 (defun tramp-handle-unhandled-file-name-directory (filename)
3506 "Like `unhandled-file-name-directory' for tramp files."
3507 (with-parsed-tramp-file-name filename nil
3508 (expand-file-name "~/")))
3510 ;; Canonicalization of file names.
3512 (defun tramp-drop-volume-letter (name)
3513 "Cut off unnecessary drive letter from file NAME.
3514 The function `tramp-handle-expand-file-name' calls `expand-file-name'
3515 locally on a remote file name. When the local system is a W32 system
3516 but the remote system is Unix, this introduces a superfluous drive
3517 letter into the file name. This function removes it.
3519 Doesn't do anything if the NAME does not start with a drive letter."
3520 (if (and (> (length name
) 1)
3521 (char-equal (aref name
1) ?
:)
3522 (let ((c1 (aref name
0)))
3523 (or (and (>= c1 ?A
) (<= c1 ?Z
))
3524 (and (>= c1 ?a
) (<= c1 ?z
)))))
3528 (defun tramp-handle-expand-file-name (name &optional dir
)
3529 "Like `expand-file-name' for tramp files.
3530 If the localname part of the given filename starts with \"/../\" then
3531 the result will be a local, non-Tramp, filename."
3532 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
3533 (setq dir
(or dir default-directory
"/"))
3534 ;; Unless NAME is absolute, concat DIR and NAME.
3535 (unless (file-name-absolute-p name
)
3536 (setq name
(concat (file-name-as-directory dir
) name
)))
3537 ;; If NAME is not a tramp file, run the real handler
3538 (if (not (tramp-tramp-file-p name
))
3539 (tramp-run-real-handler 'expand-file-name
3542 (with-parsed-tramp-file-name name nil
3543 (unless (file-name-absolute-p localname
)
3544 (setq localname
(concat "~/" localname
)))
3546 ;; Tilde expansion if necessary. This needs a shell which
3547 ;; groks tilde expansion! The function `tramp-find-shell' is
3548 ;; supposed to find such a shell on the remote host. Please
3549 ;; tell me about it when this doesn't work on your system.
3550 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname
)
3551 (let ((uname (match-string 1 localname
))
3552 (fname (match-string 2 localname
)))
3553 ;; We cannot simply apply "~/", because under sudo "~/" is
3554 ;; expanded to the local user home directory but to the
3555 ;; root home directory. On the other hand, using always
3556 ;; the default user name for tilde expansion is not
3557 ;; appropriate either, because ssh and companions might
3558 ;; use a user name from the config file.
3559 (when (and (string-equal uname
"~")
3562 (tramp-find-method multi-method method user host
)))
3563 (setq uname
(concat uname
(or user
"root"))))
3564 ;; CCC fanatic error checking?
3565 (set-buffer (tramp-get-buffer multi-method method user host
))
3568 multi-method method user host
3569 (format "cd %s; pwd" uname
)
3571 (tramp-wait-for-output)
3572 (goto-char (point-min))
3573 (setq uname
(buffer-substring (point) (tramp-line-end-position)))
3574 (setq localname
(concat uname fname
))
3576 ;; There might be a double slash, for example when "~/"
3577 ;; expands to "/". Remove this.
3578 (while (string-match "//" localname
)
3579 (setq localname
(replace-match "/" t t localname
)))
3580 ;; No tilde characters in file name, do normal
3581 ;; expand-file-name (this does "/./" and "/../"). We bind
3582 ;; directory-sep-char here for XEmacs on Windows, which would
3583 ;; otherwise use backslash. `default-directory' is bound to
3584 ;; "/", because on Windows there would be problems with UNC
3585 ;; shares or Cygwin mounts.
3586 (tramp-let-maybe directory-sep-char ?
/
3587 (let ((default-directory "/"))
3588 (tramp-make-tramp-file-name
3589 multi-method
(or method
(tramp-find-default-method user host
))
3591 (tramp-drop-volume-letter
3592 (tramp-run-real-handler 'expand-file-name
3593 (list localname
))))))))))
3595 ;; old version follows. it uses ".." to cross file handler
3597 ;; ;; Look if localname starts with "/../" construct. If this is
3598 ;; ;; the case, then we return a local name instead of a remote name.
3599 ;; (if (string-match "^/\\.\\./" localname)
3600 ;; (expand-file-name (substring localname 3))
3601 ;; ;; No tilde characters in file name, do normal
3602 ;; ;; expand-file-name (this does "/./" and "/../"). We bind
3603 ;; ;; directory-sep-char here for XEmacs on Windows, which
3604 ;; ;; would otherwise use backslash.
3605 ;; (let ((directory-sep-char ?/))
3606 ;; (tramp-make-tramp-file-name
3607 ;; multi-method method user host
3608 ;; (tramp-drop-volume-letter
3609 ;; (tramp-run-real-handler 'expand-file-name
3610 ;; (list localname))))))))))
3614 (defvar tramp-async-proc nil
3615 "Global variable keeping asynchronous process object.
3616 Used in `tramp-handle-shell-command'")
3618 (defvar tramp-display-shell-command-buffer t
3619 "Whether to display output buffer of `shell-command'.
3620 This is necessary for handling DISPLAY of `process-file'.")
3622 (defun tramp-handle-shell-command (command &optional output-buffer error-buffer
)
3623 "Like `shell-command' for tramp files.
3624 This will break if COMMAND prints a newline, followed by the value of
3625 `tramp-end-of-output', followed by another newline."
3626 ;; Asynchronous processes are far from being perfect. But it works at least
3627 ;; for `find-grep-dired' and `find-name-dired' in Emacs 22.
3628 (if (tramp-tramp-file-p default-directory
)
3629 (with-parsed-tramp-file-name default-directory nil
3630 (let ((curbuf (current-buffer))
3631 (asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
3633 (unless output-buffer
3637 "*Async Shell Command*"
3638 "*Shell Command Output*")))
3639 (set-buffer output-buffer
)
3641 (unless (bufferp output-buffer
)
3642 (setq output-buffer
(current-buffer)))
3643 (set-buffer output-buffer
)
3644 ;; Tramp doesn't handle the asynchronous case by an asynchronous
3645 ;; process. Instead of, another asynchronous process is opened
3646 ;; which gets the output of the (synchronous) Tramp process
3647 ;; via process-filter. ERROR-BUFFER is disabled.
3649 (setq command
(substring command
0 (match-beginning 0))
3651 tramp-async-proc
(start-process (buffer-name output-buffer
)
3652 output-buffer
"cat")))
3654 (tramp-barf-unless-okay
3655 multi-method method user host
3656 (format "cd %s" (tramp-shell-quote-argument localname
))
3658 "tramp-handle-shell-command: Couldn't `cd %s'"
3659 (tramp-shell-quote-argument localname
))
3660 ;; Define the process filter
3664 (tramp-get-buffer multi-method method user host
))
3665 '(lambda (process string
)
3666 ;; Write the output into the Tramp Process
3667 (save-current-buffer
3668 (set-buffer (process-buffer process
))
3669 (goto-char (point-max))
3671 ;; Hand-over output to asynchronous process.
3674 (regexp-quote tramp-end-of-output
) string
)))
3677 (substring string
0 (1- (match-beginning 0)))))
3678 (process-send-string tramp-async-proc string
)
3680 (set-process-filter process nil
)
3681 (process-send-eof tramp-async-proc
))))))
3684 multi-method method user host
3686 (format "( %s ) 2>/tmp/tramp.$$.err; tramp_old_status=$?"
3688 (format "%s; tramp_old_status=$?" command
)))
3689 (unless asynchronous
3690 (tramp-wait-for-output)))
3691 (unless asynchronous
3692 ;; We cannot use `insert-buffer' because the tramp buffer
3693 ;; changes its contents before insertion due to calling
3694 ;; `expand-file' and alike.
3696 (with-current-buffer
3697 (tramp-get-buffer multi-method method user host
)
3701 (unless (bufferp error-buffer
)
3702 (setq error-buffer
(get-buffer-create error-buffer
)))
3704 multi-method method user host
3705 "cat /tmp/tramp.$$.err")
3706 (tramp-wait-for-output)
3707 (set-buffer error-buffer
)
3708 ;; Same comment as above
3710 (with-current-buffer
3711 (tramp-get-buffer multi-method method user host
)
3713 (tramp-send-command-and-check
3714 multi-method method user host
"rm -f /tmp/tramp.$$.err")))
3716 (tramp-send-command multi-method method user host
"cd")
3717 (unless asynchronous
3718 (tramp-wait-for-output))
3720 multi-method method user host
3721 (concat "tramp_set_exit_status $tramp_old_status;"
3722 " echo tramp_exit_status $?"))
3723 (unless asynchronous
3724 (tramp-wait-for-output)
3725 (goto-char (point-max))
3726 (unless (search-backward "tramp_exit_status " nil t
)
3727 (error "Couldn't find exit status of `%s'" command
))
3728 (skip-chars-forward "^ ")
3729 (setq status
(read (current-buffer)))))
3730 (unless (zerop (buffer-size))
3731 (when tramp-display-shell-command-buffer
3732 (display-buffer output-buffer
)))
3735 ;; The following is only executed if something strange was
3736 ;; happening. Emit a helpful message and do it anyway.
3737 (message "tramp-handle-shell-command called with non-tramp directory: `%s'"
3739 (tramp-run-real-handler 'shell-command
3740 (list command output-buffer error-buffer
))))
3742 (defun tramp-handle-process-file (program &optional infile buffer display
&rest args
)
3743 "Like `process-file' for Tramp files."
3744 (when infile
(error "Implementation does not handle input from file"))
3745 (when (and (numberp buffer
) (zerop buffer
))
3746 (error "Implementation does not handle immediate return"))
3747 (when (consp buffer
) (error "Implementation does not handle error files"))
3748 (let ((tramp-display-shell-command-buffer display
))
3750 (mapconcat 'tramp-shell-quote-argument
(cons program args
) " ")
3755 (defsubst tramp-make-temp-file
()
3756 (funcall (if (fboundp 'make-temp-file
) 'make-temp-file
'make-temp-name
)
3757 (expand-file-name tramp-temp-name-prefix
3758 (tramp-temporary-file-directory))))
3760 (defun tramp-handle-file-local-copy (filename)
3761 "Like `file-local-copy' for tramp files."
3762 (with-parsed-tramp-file-name filename nil
3763 (let ((tramp-buf (tramp-get-buffer multi-method method user host
))
3764 ;; We used to bind the following as late as possible.
3765 ;; loc-enc and loc-dec were bound directly before the if
3766 ;; statement that checks them. But the functions
3767 ;; tramp-get-* might invoke the "are you awake" check in
3768 ;; tramp-maybe-open-connection, which is an unfortunate time
3769 ;; since we rely on the buffer contents at that spot.
3770 (rem-enc (tramp-get-remote-encoding multi-method method user host
))
3771 (rem-dec (tramp-get-remote-decoding multi-method method user host
))
3772 (loc-enc (tramp-get-local-encoding multi-method method user host
))
3773 (loc-dec (tramp-get-local-decoding multi-method method user host
))
3775 (unless (file-exists-p filename
)
3776 (error "Cannot make local copy of non-existing file `%s'"
3778 (setq tmpfil
(tramp-make-temp-file))
3780 (cond ((tramp-method-out-of-band-p multi-method method user host
)
3781 ;; `copy-file' handles out-of-band methods
3782 (copy-file filename tmpfil t t
))
3784 ((and rem-enc rem-dec
)
3785 ;; Use inline encoding for file transfer.
3787 ;; Following line for setting tramp-current-method,
3788 ;; tramp-current-user, tramp-current-host.
3789 (set-buffer tramp-buf
)
3790 (tramp-message 5 "Encoding remote file %s..." filename
)
3791 (tramp-barf-unless-okay
3792 multi-method method user host
3793 (concat rem-enc
" < " (tramp-shell-quote-argument localname
))
3795 "Encoding remote file failed, see buffer `%s' for details"
3797 ;; Remove trailing status code
3798 (goto-char (point-max))
3799 (delete-region (point) (progn (forward-line -
1) (point)))
3801 (tramp-message 5 "Decoding remote file %s..." filename
)
3803 ;; Here is where loc-enc and loc-dec used to be let-bound.
3804 (if (and (symbolp loc-dec
) (fboundp loc-dec
))
3805 ;; If local decoding is a function, we call it.
3806 (let ((tmpbuf (get-buffer-create " *tramp tmp*")))
3809 (insert-buffer-substring tramp-buf
)
3810 (tramp-message-for-buffer
3811 multi-method method user host
3812 6 "Decoding remote file %s with function %s..."
3815 ;; Douglas Gray Stephens <DGrayStephens@slb.com>
3816 ;; says that we need to strip tramp_exit_status
3817 ;; line from the output here. Go to point-max,
3818 ;; search backward for tramp_exit_status, delete
3819 ;; between point and point-max if found.
3820 (let ((coding-system-for-write 'binary
))
3821 (funcall loc-dec
(point-min) (point-max))
3822 (write-region (point-min) (point-max) tmpfil
))
3823 (kill-buffer tmpbuf
))
3824 ;; If tramp-decoding-function is not defined for this
3825 ;; method, we invoke tramp-decoding-command instead.
3826 (let ((tmpfil2 (tramp-make-temp-file)))
3827 (write-region (point-min) (point-max) tmpfil2
)
3829 6 "Decoding remote file %s with command %s..."
3831 (tramp-call-local-coding-command
3832 loc-dec tmpfil2 tmpfil
)
3833 (delete-file tmpfil2
)))
3834 (tramp-message-for-buffer
3835 multi-method method user host
3836 5 "Decoding remote file %s...done" filename
)
3837 ;; Set proper permissions.
3838 (set-file-modes tmpfil
(file-modes filename
))))
3840 (t (error "Wrong method specification for `%s'" method
)))
3843 (defun tramp-handle-file-remote-p (filename)
3844 "Like `file-remote-p' for tramp files."
3845 (when (tramp-tramp-file-p filename
)
3846 (with-parsed-tramp-file-name filename nil
3847 (make-tramp-file-name
3848 :multi-method multi-method
3854 (defun tramp-handle-insert-file-contents
3855 (filename &optional visit beg end replace
)
3856 "Like `insert-file-contents' for tramp files."
3857 (barf-if-buffer-read-only)
3858 (setq filename
(expand-file-name filename
))
3859 (with-parsed-tramp-file-name filename nil
3860 (if (not (file-exists-p filename
))
3863 (setq buffer-file-name filename
)
3864 (set-visited-file-modtime)
3865 (set-buffer-modified-p nil
))
3867 (format "File `%s' not found on remote host" filename
))
3868 (list (expand-file-name filename
) 0))
3869 ;; `insert-file-contents-literally' takes care to avoid calling
3870 ;; jka-compr. By let-binding inhibit-file-name-operation, we
3871 ;; propagate that care to the file-local-copy operation.
3873 (let ((inhibit-file-name-operation
3874 (when (eq inhibit-file-name-operation
3875 'insert-file-contents
)
3877 (file-local-copy filename
)))
3878 coding-system-used result
)
3880 (setq buffer-file-name filename
)
3881 (set-visited-file-modtime)
3882 (set-buffer-modified-p nil
))
3883 (tramp-message-for-buffer
3884 multi-method method user host
3885 9 "Inserting local temp file `%s'..." local-copy
)
3886 (setq result
(insert-file-contents local-copy nil beg end replace
))
3887 ;; Now `last-coding-system-used' has right value. Remember it.
3888 (when (boundp 'last-coding-system-used
)
3889 (setq coding-system-used
(symbol-value 'last-coding-system-used
)))
3890 (tramp-message-for-buffer
3891 multi-method method user host
3892 9 "Inserting local temp file `%s'...done" local-copy
)
3893 (delete-file local-copy
)
3894 (when (boundp 'last-coding-system-used
)
3895 (set 'last-coding-system-used coding-system-used
))
3896 (list (expand-file-name filename
)
3897 (second result
))))))
3900 (defun tramp-handle-find-backup-file-name (filename)
3901 "Like `find-backup-file-name' for tramp files."
3902 (with-parsed-tramp-file-name filename nil
3903 ;; We set both variables. It doesn't matter whether it is
3905 (let ((backup-directory-alist
3907 (when (boundp 'backup-directory-alist
)
3908 (if (boundp 'tramp-backup-directory-alist
)
3913 (if (and (stringp (cdr x
))
3914 (file-name-absolute-p (cdr x
))
3915 (not (tramp-file-name-p (cdr x
))))
3916 (tramp-make-tramp-file-name
3917 multi-method method user host
(cdr x
))
3919 (symbol-value 'tramp-backup-directory-alist
))
3920 (symbol-value 'backup-directory-alist
))))
3922 (bkup-backup-directory-info
3924 (when (boundp 'bkup-backup-directory-info
)
3925 (if (boundp 'tramp-bkup-backup-directory-info
)
3931 (if (and (stringp (car (cdr x
)))
3932 (file-name-absolute-p (car (cdr x
)))
3933 (not (tramp-file-name-p (car (cdr x
)))))
3934 (tramp-make-tramp-file-name
3935 multi-method method user host
(car (cdr x
)))
3938 (symbol-value 'tramp-bkup-backup-directory-info
))
3939 (symbol-value 'bkup-backup-directory-info
)))))
3941 (tramp-run-real-handler 'find-backup-file-name
(list filename
)))))
3943 (defun tramp-handle-make-auto-save-file-name ()
3944 "Like `make-auto-save-file-name' for tramp files.
3945 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
3946 (let ((tramp-auto-save-directory tramp-auto-save-directory
))
3947 ;; File name must be unique. This is ensured with Emacs 22 (see
3948 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
3949 ;; all other cases we must do it ourselves.
3950 (when (boundp 'auto-save-file-name-transforms
)
3953 (when (and (string-match (car x
) buffer-file-name
)
3954 (not (car (cddr x
))))
3955 (setq tramp-auto-save-directory
3956 (or tramp-auto-save-directory temporary-file-directory
))))
3957 (symbol-value 'auto-save-file-name-transforms
)))
3958 ;; Create directory.
3959 (when tramp-auto-save-directory
3960 (unless (file-exists-p tramp-auto-save-directory
)
3961 (make-directory tramp-auto-save-directory t
)))
3962 ;; jka-compr doesn't like auto-saving, so by appending "~" to the
3963 ;; file name we make sure that jka-compr isn't used for the
3965 (let ((buffer-file-name
3966 (if tramp-auto-save-directory
3968 (tramp-subst-strs-in-string
3976 tramp-auto-save-directory
)
3977 (buffer-file-name))))
3978 ;; Run plain `make-auto-save-file-name'. There might be an advice when
3979 ;; it is not a magic file name operation (since Emacs 22).
3980 ;; We must deactivate it temporarily.
3981 (if (not (ad-is-active 'make-auto-save-file-name
))
3982 (tramp-run-real-handler
3983 'make-auto-save-file-name nil
)
3985 (ad-deactivate 'make-auto-save-file-name
)
3987 (tramp-run-real-handler
3988 'make-auto-save-file-name nil
)
3989 (ad-activate 'make-auto-save-file-name
))))))
3992 ;; CCC grok APPEND, LOCKNAME, CONFIRM
3993 (defun tramp-handle-write-region
3994 (start end filename
&optional append visit lockname confirm
)
3995 "Like `write-region' for tramp files."
3996 (unless (eq append nil
)
3997 (error "Cannot append to file using tramp (`%s')" filename
))
3998 (setq filename
(expand-file-name filename
))
3999 ;; Following part commented out because we don't know what to do about
4000 ;; file locking, and it does not appear to be a problem to ignore it.
4001 ;; Ange-ftp ignores it, too.
4002 ;; (when (and lockname (stringp lockname))
4003 ;; (setq lockname (expand-file-name lockname)))
4004 ;; (unless (or (eq lockname nil)
4005 ;; (string= lockname filename))
4007 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
4008 ;; XEmacs takes a coding system as the seventh argument, not `confirm'
4009 (when (and (not (featurep 'xemacs
))
4010 confirm
(file-exists-p filename
))
4011 (unless (y-or-n-p (format "File %s exists; overwrite anyway? "
4013 (error "File not overwritten")))
4014 (with-parsed-tramp-file-name filename nil
4015 (let ((curbuf (current-buffer))
4016 (rem-enc (tramp-get-remote-encoding multi-method method user host
))
4017 (rem-dec (tramp-get-remote-decoding multi-method method user host
))
4018 (loc-enc (tramp-get-local-encoding multi-method method user host
))
4019 (loc-dec (tramp-get-local-decoding multi-method method user host
))
4020 (trampbuf (get-buffer-create "*tramp output*"))
4021 (modes (file-modes filename
))
4022 ;; We use this to save the value of `last-coding-system-used'
4023 ;; after writing the tmp file. At the end of the function,
4024 ;; we set `last-coding-system-used' to this saved value.
4025 ;; This way, any intermediary coding systems used while
4026 ;; talking to the remote shell or suchlike won't hose this
4027 ;; variable. This approach was snarfed from ange-ftp.el.
4030 ;; Write region into a tmp file. This isn't really needed if we
4031 ;; use an encoding function, but currently we use it always
4032 ;; because this makes the logic simpler.
4033 (setq tmpfil
(tramp-make-temp-file))
4034 ;; Set current buffer. If connection wasn't open, `file-modes' has
4035 ;; changed it accidently.
4037 ;; We say `no-message' here because we don't want the visited file
4038 ;; modtime data to be clobbered from the temp file. We call
4039 ;; `set-visited-file-modtime' ourselves later on.
4040 (tramp-run-real-handler
4042 (if confirm
; don't pass this arg unless defined for backward compat.
4043 (list start end tmpfil append
'no-message lockname confirm
)
4044 (list start end tmpfil append
'no-message lockname
)))
4045 ;; Now, `last-coding-system-used' has the right value. Remember it.
4046 (when (boundp 'last-coding-system-used
)
4047 (setq coding-system-used
(symbol-value 'last-coding-system-used
)))
4048 ;; The permissions of the temporary file should be set. If
4049 ;; filename does not exist (eq modes nil) it has been renamed to
4050 ;; the backup file. This case `save-buffer' handles
4052 (when modes
(set-file-modes tmpfil modes
))
4053 ;; This is a bit lengthy due to the different methods possible for
4054 ;; file transfer. First, we check whether the method uses an rcp
4055 ;; program. If so, we call it. Otherwise, both encoding and
4056 ;; decoding command must be specified. However, if the method
4057 ;; _also_ specifies an encoding function, then that is used for
4058 ;; encoding the contents of the tmp file.
4059 (cond ((tramp-method-out-of-band-p multi-method method user host
)
4060 ;; `copy-file' handles out-of-band methods
4061 (copy-file tmpfil filename t t
))
4063 ((and rem-enc rem-dec
)
4064 ;; Use inline file transfer
4065 (let ((tmpbuf (get-buffer-create " *tramp file transfer*")))
4067 ;; Encode tmpfil into tmpbuf
4068 (tramp-message-for-buffer multi-method method user host
4069 5 "Encoding region...")
4072 ;; Use encoding function or command.
4073 (if (and (symbolp loc-enc
) (fboundp loc-enc
))
4075 (tramp-message-for-buffer
4076 multi-method method user host
4077 6 "Encoding region using function `%s'..."
4078 (symbol-name loc-enc
))
4079 (insert-file-contents-literally tmpfil
)
4080 ;; CCC. The following `let' is a workaround for
4081 ;; the base64.el that comes with pgnus-0.84. If
4082 ;; both of the following conditions are
4083 ;; satisfied, it tries to write to a local file
4084 ;; in default-directory, but at this point,
4085 ;; default-directory is remote.
4086 ;; (CALL-PROCESS-REGION can't write to remote
4087 ;; files, it seems.) The file in question is a
4089 (let ((default-directory
4090 (tramp-temporary-file-directory)))
4091 (funcall loc-enc
(point-min) (point-max)))
4092 (goto-char (point-max))
4095 (tramp-message-for-buffer
4096 multi-method method user host
4097 6 "Encoding region using command `%s'..." loc-enc
)
4098 (unless (equal 0 (tramp-call-local-coding-command
4100 (pop-to-buffer trampbuf
)
4101 (error (concat "Cannot write to `%s', local encoding"
4102 " command `%s' failed")
4104 ;; Send tmpbuf into remote decoding command which
4105 ;; writes to remote file. Because this happens on the
4106 ;; remote host, we cannot use the function.
4107 (tramp-message-for-buffer
4108 multi-method method user host
4109 5 "Decoding region into remote file %s..." filename
)
4111 multi-method method user host
4112 (format "%s >%s <<'EOF'"
4114 (tramp-shell-quote-argument localname
)))
4116 (tramp-message-for-buffer
4117 multi-method method user host
4118 6 "Sending data to remote host...")
4119 (tramp-send-string multi-method method user host
4121 ;; wait for remote decoding to complete
4122 (tramp-message-for-buffer
4123 multi-method method user host
4124 6 "Sending end of data token...")
4126 multi-method method user host
"EOF" nil t
)
4127 (tramp-message-for-buffer
4128 multi-method method user host
6
4129 "Waiting for remote host to process data...")
4130 (set-buffer (tramp-get-buffer multi-method method user host
))
4131 (tramp-wait-for-output)
4132 (tramp-barf-unless-okay
4133 multi-method method user host nil nil
'file-error
4134 (concat "Couldn't write region to `%s',"
4135 " decode using `%s' failed")
4137 (tramp-message 5 "Decoding region into remote file %s...done"
4139 (kill-buffer tmpbuf
))))
4142 (concat "Method `%s' should specify both encoding and "
4143 "decoding command or an rcp program")
4145 (delete-file tmpfil
)
4146 (unless (equal curbuf
(current-buffer))
4147 (error "Buffer has changed from `%s' to `%s'"
4148 curbuf
(current-buffer)))
4149 (when (or (eq visit t
) (stringp visit
))
4150 (set-visited-file-modtime
4151 ;; We must pass modtime explicitely, because filename can be different
4152 ;; from (buffer-file-name), f.e. if `file-precious-flag' is set.
4153 (nth 5 (file-attributes filename
))))
4154 ;; Make `last-coding-system-used' have the right value.
4155 (when (boundp 'last-coding-system-used
)
4156 (set 'last-coding-system-used coding-system-used
))
4157 (when (or (eq visit t
)
4160 (message "Wrote %s" filename
)))))
4162 ;; Call down to the real handler.
4163 ;; Because EFS does not play nicely with TRAMP (both systems match a
4164 ;; TRAMP file name) it is needed to disable efs as well as tramp for the
4167 ;; Other than that, this is the canon file-handler code that the doco
4168 ;; says should be used here. Which is nice.
4170 ;; Under XEmacs current, EFS also hooks in as
4171 ;; efs-sifn-handler-function to handle any filename with environment
4172 ;; variables. This has two implications:
4173 ;; 1) That EFS may not be completely dead (yet) for TRAMP filenames
4174 ;; 2) That TRAMP might want to do the same thing.
4175 ;; Details as they come in.
4177 ;; Daniel Pittman <daniel@danann.net>
4179 ;; (defun tramp-run-real-handler (operation args)
4180 ;; "Invoke normal file name handler for OPERATION.
4181 ;; This inhibits EFS and Ange-FTP, too, because they conflict with tramp.
4182 ;; First arg specifies the OPERATION, remaining ARGS are passed to the
4184 ;; (let ((inhibit-file-name-handlers
4185 ;; (list 'tramp-file-name-handler
4186 ;; 'efs-file-handler-function
4187 ;; 'ange-ftp-hook-function
4188 ;; (and (eq inhibit-file-name-operation operation)
4189 ;; inhibit-file-name-handlers)))
4190 ;; (inhibit-file-name-operation operation))
4191 ;; (apply operation args)))
4194 (progn (defun tramp-run-real-handler (operation args
)
4195 "Invoke normal file name handler for OPERATION.
4196 First arg specifies the OPERATION, second arg is a list of arguments to
4197 pass to the OPERATION."
4198 (let* ((inhibit-file-name-handlers
4199 `(tramp-file-name-handler
4200 tramp-completion-file-name-handler
4201 cygwin-mount-name-hook-function
4202 cygwin-mount-map-drive-hook-function
4204 ,(and (eq inhibit-file-name-operation operation
)
4205 inhibit-file-name-handlers
)))
4206 (inhibit-file-name-operation operation
))
4207 (apply operation args
))))
4209 ;; This function is used from `tramp-completion-file-name-handler' functions
4210 ;; only, if `tramp-completion-mode' is true. But this cannot be checked here
4211 ;; because the check is based on a full filename, not available for all
4212 ;; basic I/O operations.
4214 (progn (defun tramp-completion-run-real-handler (operation args
)
4215 "Invoke `tramp-file-name-handler' for OPERATION.
4216 First arg specifies the OPERATION, second arg is a list of arguments to
4217 pass to the OPERATION."
4218 (let* ((inhibit-file-name-handlers
4219 `(tramp-completion-file-name-handler
4220 cygwin-mount-name-hook-function
4221 cygwin-mount-map-drive-hook-function
4223 ,(and (eq inhibit-file-name-operation operation
)
4224 inhibit-file-name-handlers
)))
4225 (inhibit-file-name-operation operation
))
4226 (apply operation args
))))
4228 ;; We handle here all file primitives. Most of them have the file
4229 ;; name as first parameter; nevertheless we check for them explicitly
4230 ;; in order to be signalled if a new primitive appears. This
4231 ;; scenario is needed because there isn't a way to decide by
4232 ;; syntactical means whether a foreign method must be called. It would
4233 ;; ease the life if `file-name-handler-alist' would support a decision
4234 ;; function as well but regexp only.
4235 (defun tramp-file-name-for-operation (operation &rest args
)
4236 "Return file name related to OPERATION file primitive.
4237 ARGS are the arguments OPERATION has been called with."
4239 ; FILE resp DIRECTORY
4241 (list 'access-file
'byte-compiler-base-file-name
'delete-directory
4242 'delete-file
'diff-latest-backup-file
'directory-file-name
4243 'directory-files
'directory-files-and-attributes
4244 'dired-compress-file
'dired-uncache
4245 'file-accessible-directory-p
'file-attributes
4246 'file-directory-p
'file-executable-p
'file-exists-p
4247 'file-local-copy
'file-remote-p
'file-modes
4248 'file-name-as-directory
'file-name-directory
4249 'file-name-nondirectory
'file-name-sans-versions
4250 'file-ownership-preserved-p
'file-readable-p
4251 'file-regular-p
'file-symlink-p
'file-truename
4252 'file-writable-p
'find-backup-file-name
'find-file-noselect
4253 'get-file-buffer
'insert-directory
'insert-file-contents
4254 'load
'make-directory
'make-directory-internal
4255 'set-file-modes
'substitute-in-file-name
4256 'unhandled-file-name-directory
'vc-registered
4258 'abbreviate-file-name
'create-file-buffer
4259 'dired-file-modtime
'dired-make-compressed-filename
4260 'dired-recursive-delete-directory
'dired-set-file-modtime
4261 'dired-shell-unhandle-file-name
'dired-uucode-file
4262 'insert-file-contents-literally
'recover-file
4263 'vm-imap-check-mail
'vm-pop-check-mail
'vm-spool-check-mail
))
4264 (if (file-name-absolute-p (nth 0 args
))
4266 (expand-file-name (nth 0 args
))))
4267 ; FILE DIRECTORY resp FILE1 FILE2
4269 (list 'add-name-to-file
'copy-file
'expand-file-name
4270 'file-name-all-completions
'file-name-completion
4271 'file-newer-than-file-p
'make-symbolic-link
'rename-file
4273 'dired-make-relative-symlink
4274 'vm-imap-move-mail
'vm-pop-move-mail
'vm-spool-move-mail
))
4277 ((string-match tramp-file-name-regexp
(nth 0 args
)) (nth 0 args
))
4278 ((string-match tramp-file-name-regexp
(nth 1 args
)) (nth 1 args
))
4279 (t (buffer-file-name (current-buffer))))))
4281 ((eq operation
'write-region
)
4285 (list 'make-auto-save-file-name
4286 'set-visited-file-modtime
'verify-visited-file-modtime
4290 (if (bufferp (nth 0 args
)) (nth 0 args
) (current-buffer))))
4293 (list 'dired-call-process
4299 'dired-print-file
'dired-shell-call-process
))
4301 ; unknown file primitive
4302 (t (error "unknown file I/O primitive: %s" operation
))))
4304 (defun tramp-find-foreign-file-name-handler (filename)
4305 "Return foreign file name handler if exists."
4306 (when (tramp-tramp-file-p filename
)
4309 (handler-alist tramp-foreign-file-name-handler-alist
))
4310 (while handler-alist
4311 (setq elt
(car handler-alist
)
4312 handler-alist
(cdr handler-alist
))
4313 (when (funcall (car elt
) filename
)
4314 (setq handler-alist nil
)
4315 (setq res
(cdr elt
))))
4320 (defun tramp-file-name-handler (operation &rest args
)
4321 "Invoke Tramp file name handler.
4322 Falls back to normal file name handler if no tramp file name handler exists."
4323 ;; (setq edebug-trace t)
4324 ;; (edebug-trace "%s" (with-output-to-string (backtrace)))
4326 (let* ((filename (apply 'tramp-file-name-for-operation operation args
))
4327 (completion (tramp-completion-mode filename
))
4328 (foreign (tramp-find-foreign-file-name-handler filename
)))
4329 (with-parsed-tramp-file-name filename nil
4331 ;; When we are in completion mode, some operations shouldn' be
4332 ;; handled by backend.
4333 ((and completion
(memq operation
'(expand-file-name)))
4334 (tramp-run-real-handler operation args
))
4335 ((and completion
(zerop (length localname
))
4336 (memq operation
'(file-exists-p file-directory-p
)))
4338 ;; Call the backend function.
4339 (foreign (apply foreign operation args
))
4340 ;; Nothing to do for us.
4341 (t (tramp-run-real-handler operation args
)))))))
4344 ;; In Emacs, there is some concurrency due to timers. If a timer
4345 ;; interrupts Tramp and wishes to use the same connection buffer as
4346 ;; the "main" Emacs, then garbage might occur in the connection
4347 ;; buffer. Therefore, we need to make sure that a timer does not use
4348 ;; the same connection buffer as the "main" Emacs. We implement a
4349 ;; cheap global lock, instead of locking each connection buffer
4350 ;; separately. The global lock is based on two variables,
4351 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
4352 ;; (with setq) to indicate a lock. But Tramp also calls itself during
4353 ;; processing of a single file operation, so we need to allow
4354 ;; recursive calls. That's where the `tramp-locker' variable comes in
4355 ;; -- it is let-bound to t during the execution of the current
4356 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
4357 ;; then we should just proceed because we have been called
4358 ;; recursively. But if `tramp-locker' is nil, then we are a timer
4359 ;; interrupting the "main" Emacs, and then we signal an error.
4361 (defvar tramp-locked nil
4362 "If non-nil, then Tramp is currently busy.
4363 Together with `tramp-locker', this implements a locking mechanism
4364 preventing reentrant calls of Tramp.")
4366 (defvar tramp-locker nil
4367 "If non-nil, then a caller has locked Tramp.
4368 Together with `tramp-locked', this implements a locking mechanism
4369 preventing reentrant calls of Tramp.")
4371 (defun tramp-sh-file-name-handler (operation &rest args
)
4372 "Invoke remote-shell Tramp file name handler.
4373 Fall back to normal file name handler if no Tramp handler exists."
4374 (when (and tramp-locked
(not tramp-locker
))
4375 (signal 'file-error
"Forbidden reentrant call of Tramp"))
4376 (let ((tl tramp-locked
))
4379 (setq tramp-locked t
)
4380 (let ((tramp-locker t
))
4382 (let ((fn (assoc operation tramp-file-name-handler-alist
)))
4384 (apply (cdr fn
) args
)
4385 (tramp-run-real-handler operation args
))))))
4386 (setq tramp-locked tl
))))
4389 (progn (defun tramp-completion-file-name-handler (operation &rest args
)
4390 "Invoke tramp file name completion handler.
4391 Falls back to normal file name handler if no tramp file name handler exists."
4392 ;; (setq edebug-trace t)
4393 ;; (edebug-trace "%s" (with-output-to-string (backtrace)))
4394 (let ((fn (assoc operation tramp-completion-file-name-handler-alist
)))
4396 (save-match-data (apply (cdr fn
) args
))
4397 (tramp-completion-run-real-handler operation args
)))))
4400 (defsubst tramp-register-file-name-handlers
()
4401 "Add tramp file name handlers to `file-name-handler-alist'."
4402 (add-to-list 'file-name-handler-alist
4403 (cons tramp-file-name-regexp
'tramp-file-name-handler
))
4404 ;; `partial-completion-mode' is unknown in XEmacs. So we should
4405 ;; load it unconditionally there. In the GNU Emacs case, method/
4406 ;; user/host name completion shall be bound to `partial-completion-mode'.
4407 (when (or (not (boundp 'partial-completion-mode
))
4408 (symbol-value 'partial-completion-mode
)
4410 (add-to-list 'file-name-handler-alist
4411 (cons tramp-completion-file-name-regexp
4412 'tramp-completion-file-name-handler
))
4413 (put 'tramp-completion-file-name-handler
'safe-magic t
))
4414 ;; If jka-compr is already loaded, move it to the front of
4415 ;; `file-name-handler-alist'.
4416 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist
)))
4418 (setq file-name-handler-alist
4419 (cons jka
(delete jka file-name-handler-alist
))))))
4421 ;; During autoload, it shall be checked whether
4422 ;; `partial-completion-mode' is active. Therefore registering will be
4424 ;;;###autoload(add-hook
4425 ;;;###autoload 'after-init-hook
4426 ;;;###autoload '(lambda () (tramp-register-file-name-handlers)))
4427 (tramp-register-file-name-handlers)
4430 (defun tramp-unload-file-name-handlers ()
4431 (setq file-name-handler-alist
4432 (delete (rassoc 'tramp-file-name-handler
4433 file-name-handler-alist
)
4434 (delete (rassoc 'tramp-completion-file-name-handler
4435 file-name-handler-alist
)
4436 file-name-handler-alist
))))
4438 (add-hook 'tramp-unload-hook
'tramp-unload-file-name-handlers
)
4441 ;;; Interactions with other packages:
4443 ;; -- complete.el --
4445 ;; This function contributed by Ed Sabol
4446 (defun tramp-handle-expand-many-files (name)
4447 "Like `PC-expand-many-files' for tramp files."
4448 (with-parsed-tramp-file-name name nil
4450 (if (or (string-match "\\*" name
)
4451 (string-match "\\?" name
)
4452 (string-match "\\[.*\\]" name
))
4455 ;; CCC: To do it right, we should quote certain characters
4456 ;; in the file name, but since the echo command is going to
4457 ;; break anyway when there are spaces in the file names, we
4459 ;;-(let ((comint-file-name-quote-list
4460 ;;- (set-difference tramp-file-name-quote-list
4461 ;;- '(?\* ?\? ?[ ?]))))
4462 ;;- (tramp-send-command
4463 ;;- multi-method method user host
4464 ;;- (format "echo %s" (comint-quote-filename localname)))
4465 ;;- (tramp-wait-for-output))
4466 (tramp-send-command multi-method method user host
4467 (format "echo %s" localname
))
4468 (tramp-wait-for-output)
4469 (setq bufstr
(buffer-substring (point-min)
4470 (tramp-line-end-position)))
4471 (goto-char (point-min))
4472 (if (string-equal localname bufstr
)
4475 (while (search-forward " " nil t
)
4476 (delete-backward-char 1)
4478 (goto-char (point-max))
4479 (delete-backward-char 1)
4481 (goto-char (point-min))
4483 (function (lambda (x)
4484 (tramp-make-tramp-file-name multi-method method
4486 (read (current-buffer))))))
4487 (list (expand-file-name name
))))))
4489 (eval-after-load "complete"
4491 (defadvice PC-expand-many-files
4492 (around tramp-advice-PC-expand-many-files
(name) activate
)
4493 "Invoke `tramp-handle-expand-many-files' for tramp files."
4494 (if (tramp-tramp-file-p name
)
4495 (setq ad-return-value
(tramp-handle-expand-many-files name
))
4497 (add-hook 'tramp-unload-hook
4498 '(lambda () (ad-unadvise 'PC-expand-many-files
)))))
4500 ;;; File name handler functions for completion mode
4502 (defvar tramp-completion-mode nil
4503 "If non-nil, we are in file name completion mode.")
4505 ;; Necessary because `tramp-file-name-regexp-unified' and
4506 ;; `tramp-completion-file-name-regexp-unified' aren't different.
4507 ;; If nil, `tramp-completion-run-real-handler' is called (i.e. forwarding to
4508 ;; `tramp-file-name-handler'). Otherwise, it takes `tramp-run-real-handler'.
4509 ;; Using `last-input-event' is a little bit risky, because completing a file
4510 ;; might require loading other files, like "~/.netrc", and for them it
4511 ;; shouldn't be decided based on that variable. On the other hand, those files
4512 ;; shouldn't have partial tramp file name syntax. Maybe another variable should
4513 ;; be introduced overwriting this check in such cases. Or we change tramp
4514 ;; file name syntax in order to avoid ambiguities, like in XEmacs ...
4515 ;; In case of non unified file names it can be always true (and wouldn't be
4516 ;; necessary, because there are different regexp).
4517 (defun tramp-completion-mode (file)
4518 "Checks whether method / user name / host name completion is active."
4520 (tramp-completion-mode t
)
4521 ((not tramp-unified-filenames
) t
)
4522 ((string-match "^/.*:.*:$" file
) nil
)
4524 (concat tramp-prefix-regexp
4525 "\\(" tramp-method-regexp
"\\)" tramp-postfix-single-method-regexp
"$")
4527 (member (match-string 1 file
) (mapcar 'car tramp-methods
)))
4528 ((or (equal last-input-event
'tab
)
4530 (and (integerp last-input-event
)
4532 ;; ?\t has event-modifier 'control
4533 (char-equal last-input-event ?
\t)
4534 (and (not (event-modifiers last-input-event
))
4535 (or (char-equal last-input-event ?
\?)
4536 (char-equal last-input-event ?\
)))))
4538 (and (featurep 'xemacs
)
4540 ;; ?\t has event-modifier 'control
4542 (funcall (symbol-function 'event-to-character
)
4543 last-input-event
) ?
\t)
4544 (and (not (event-modifiers last-input-event
))
4546 (funcall (symbol-function 'event-to-character
)
4547 last-input-event
) ?
\?)
4549 (funcall (symbol-function 'event-to-character
)
4550 last-input-event
) ?\
))))))
4553 ;; Method, host name and user name completion.
4554 ;; `tramp-completion-dissect-file-name' returns a list of
4555 ;; tramp-file-name structures. For all of them we return possible completions.
4557 (defun tramp-completion-handle-file-name-all-completions (filename directory
)
4558 "Like `file-name-all-completions' for partial tramp files."
4561 ;; We need to reset `tramp-completion-mode'.
4563 (setq tramp-completion-mode t
)
4565 ((fullname (concat directory filename
))
4566 ;; possible completion structures
4567 (v (tramp-completion-dissect-file-name fullname
))
4571 (let* ((car (car v
))
4572 (multi-method (tramp-file-name-multi-method car
))
4573 (method (tramp-file-name-method car
))
4574 (user (tramp-file-name-user car
))
4575 (host (tramp-file-name-host car
))
4576 (localname (tramp-file-name-localname car
))
4577 (m (tramp-find-method multi-method method user host
))
4578 (tramp-current-user user
) ; see `tramp-parse-passwd'
4581 (unless (or multi-method
;; Not handled (yet).
4582 localname
) ;; Nothing to complete
4586 ;; Method dependent user / host combinations
4590 (setq all-user-hosts
4591 (append all-user-hosts
4592 (funcall (nth 0 x
) (nth 1 x
)))))
4593 (tramp-get-completion-function m
))
4595 (setq result
(append result
4598 (tramp-get-completion-user-host
4599 method user host
(nth 0 x
) (nth 1 x
)))
4600 (delq nil all-user-hosts
)))))
4604 (append result
(tramp-get-completion-methods m
)))))
4608 ;; unify list, remove nil elements
4610 (let ((car (car result
)))
4611 (when car
(add-to-list
4612 'result1
(substring car
(length directory
))))
4613 (setq result
(cdr result
))))
4615 ;; Complete local parts
4620 ;; "/ssh:" does not need to be expanded as hostname.
4621 (tramp-run-real-handler
4622 'file-name-all-completions
(list filename directory
))
4623 ;; No method/user/host found to be expanded.
4624 (tramp-completion-run-real-handler
4625 'file-name-all-completions
(list filename directory
)))
4628 (setq tramp-completion-mode nil
)))
4630 ;; Method, host name and user name completion for a file.
4632 (defun tramp-completion-handle-file-name-completion
4633 (filename directory
&optional predicate
)
4634 "Like `file-name-completion' for tramp files."
4637 (mapcar 'list
(file-name-all-completions filename directory
))
4640 ;; I misuse a little bit the tramp-file-name structure in order to handle
4641 ;; completion possibilities for partial methods / user names / host names.
4642 ;; Return value is a list of tramp-file-name structures according to possible
4643 ;; completions. If "multi-method" or "localname" is non-nil it means there
4644 ;; shouldn't be a completion anymore.
4646 ;; Expected results:
4648 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
4649 ;; [nil nil nil "x" nil] [nil nil "x" nil nil] [nil nil "x" "y" nil]
4650 ;; [nil nil "x" nil nil]
4651 ;; [nil "x" nil nil nil]
4653 ;; "/x:" "/x:y" "/x:y:"
4654 ;; [nil nil nil "x" ""] [nil nil nil "x" "y"] [nil "x" nil "y" ""]
4656 ;; [nil "x" nil "" nil] [nil "x" nil "y" nil]
4657 ;; [nil "x" "" nil nil] [nil "x" "y" nil nil]
4659 ;; "/x:y@" "/x:y@z" "/x:y@z:"
4660 ;; [nil nil nil "x" "y@"] [nil nil nil "x" "y@z"] [nil "x" "y" "z" ""]
4661 ;; "/[x/y@" "/[x/y@z"
4662 ;; [nil "x" nil "y" nil] [nil "x" "y" "z" nil]
4663 (defun tramp-completion-dissect-file-name (name)
4664 "Returns a list of `tramp-file-name' structures.
4665 They are collected by `tramp-completion-dissect-file-name1'."
4669 ;; "/method" "/[method"
4670 (tramp-completion-file-name-structure1
4671 (list (concat tramp-prefix-regexp
"\\(" tramp-method-regexp x-nil
"\\)$")
4674 (tramp-completion-file-name-structure2
4675 (list (concat tramp-prefix-regexp
"\\(" tramp-user-regexp x-nil
"\\)$")
4678 (tramp-completion-file-name-structure3
4679 (list (concat tramp-prefix-regexp
"\\(" tramp-host-regexp x-nil
"\\)$")
4681 ;; "/user@host" "/[user@host"
4682 (tramp-completion-file-name-structure4
4683 (list (concat tramp-prefix-regexp
4684 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
4685 "\\(" tramp-host-regexp x-nil
"\\)$")
4687 ;; "/method:user" "/[method/user"
4688 (tramp-completion-file-name-structure5
4689 (list (concat tramp-prefix-regexp
4690 "\\(" tramp-method-regexp
"\\)" tramp-postfix-single-method-regexp
4691 "\\(" tramp-user-regexp x-nil
"\\)$")
4693 ;; "/method:host" "/[method/host"
4694 (tramp-completion-file-name-structure6
4695 (list (concat tramp-prefix-regexp
4696 "\\(" tramp-method-regexp
"\\)" tramp-postfix-single-method-regexp
4697 "\\(" tramp-host-regexp x-nil
"\\)$")
4699 ;; "/method:user@host" "/[method/user@host"
4700 (tramp-completion-file-name-structure7
4701 (list (concat tramp-prefix-regexp
4702 "\\(" tramp-method-regexp
"\\)" tramp-postfix-single-method-regexp
4703 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
4704 "\\(" tramp-host-regexp x-nil
"\\)$")
4707 (mapcar (lambda (regexp)
4708 (add-to-list 'result
4709 (tramp-completion-dissect-file-name1 regexp name
)))
4711 tramp-completion-file-name-structure1
4712 tramp-completion-file-name-structure2
4713 tramp-completion-file-name-structure3
4714 tramp-completion-file-name-structure4
4715 tramp-completion-file-name-structure5
4716 tramp-completion-file-name-structure6
4717 tramp-completion-file-name-structure7
4718 tramp-file-name-structure
))
4722 (defun tramp-completion-dissect-file-name1 (structure name
)
4723 "Returns a `tramp-file-name' structure matching STRUCTURE.
4724 The structure consists of multi-method, remote method, remote user,
4725 remote host and localname (filename on remote host)."
4729 (when (string-match (nth 0 structure
) name
)
4730 (setq method
(and (nth 1 structure
)
4731 (match-string (nth 1 structure
) name
)))
4732 (if (and method
(member method tramp-multi-methods
))
4733 ;; Not handled (yet).
4734 (make-tramp-file-name
4735 :multi-method method
4740 (let ((user (and (nth 2 structure
)
4741 (match-string (nth 2 structure
) name
)))
4742 (host (and (nth 3 structure
)
4743 (match-string (nth 3 structure
) name
)))
4744 (localname (and (nth 4 structure
)
4745 (match-string (nth 4 structure
) name
))))
4746 (make-tramp-file-name
4751 :localname localname
)))))))
4753 ;; This function returns all possible method completions, adding the
4754 ;; trailing method delimeter.
4755 (defun tramp-get-completion-methods (partial-method)
4756 "Returns all method completions for PARTIAL-METHOD."
4760 (string-match (concat "^" (regexp-quote partial-method
)) method
)
4761 (tramp-make-tramp-file-name nil method nil nil nil
)))
4762 (delete "multi" (mapcar 'car tramp-methods
))))
4764 ;; Compares partial user and host names with possible completions.
4765 (defun tramp-get-completion-user-host (method partial-user partial-host user host
)
4766 "Returns the most expanded string for user and host name completion.
4767 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
4770 ((and partial-user partial-host
)
4772 (string-match (concat "^" (regexp-quote partial-host
)) host
)
4773 (string-equal partial-user
(or user partial-user
)))
4774 (setq user partial-user
)
4781 (and user
(string-match (concat "^" (regexp-quote partial-user
)) user
))
4787 (and host
(string-match (concat "^" (regexp-quote partial-host
)) host
))
4793 (unless (zerop (+ (length user
) (length host
)))
4794 (tramp-make-tramp-file-name nil method user host nil
)))
4796 (defun tramp-parse-rhosts (filename)
4797 "Return a list of (user host) tuples allowed to access.
4798 Either user or host may be nil."
4801 (when (file-readable-p filename
)
4803 (insert-file-contents filename
)
4804 (goto-char (point-min))
4806 (push (tramp-parse-rhosts-group) res
))))
4809 ;; Taken from gnus/netrc.el
4811 (defalias 'tramp-point-at-eol
4812 (if (fboundp 'point-at-eol
)
4814 'line-end-position
)))
4816 (defun tramp-parse-rhosts-group ()
4817 "Return a (user host) tuple allowed to access.
4818 Either user or host may be nil."
4823 "^\\(" tramp-host-regexp
"\\)"
4824 "\\([ \t]+" "\\(" tramp-user-regexp
"\\)" "\\)?")))
4826 (narrow-to-region (point) (tramp-point-at-eol))
4827 (when (re-search-forward regexp nil t
)
4828 (setq result
(append (list (match-string 3) (match-string 1)))))
4833 (defun tramp-parse-shosts (filename)
4834 "Return a list of (user host) tuples allowed to access.
4835 User is always nil."
4838 (when (file-readable-p filename
)
4840 (insert-file-contents filename
)
4841 (goto-char (point-min))
4843 (push (tramp-parse-shosts-group) res
))))
4846 (defun tramp-parse-shosts-group ()
4847 "Return a (user host) tuple allowed to access.
4848 User is always nil."
4851 (regexp (concat "^\\(" tramp-host-regexp
"\\)")))
4853 (narrow-to-region (point) (tramp-point-at-eol))
4854 (when (re-search-forward regexp nil t
)
4855 (setq result
(list nil
(match-string 1))))
4858 (> (skip-chars-forward ",") 0)
4862 (defun tramp-parse-sconfig (filename)
4863 "Return a list of (user host) tuples allowed to access.
4864 User is always nil."
4867 (when (file-readable-p filename
)
4869 (insert-file-contents filename
)
4870 (goto-char (point-min))
4872 (push (tramp-parse-sconfig-group) res
))))
4875 (defun tramp-parse-sconfig-group ()
4876 "Return a (user host) tuple allowed to access.
4877 User is always nil."
4880 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp
"\\)")))
4882 (narrow-to-region (point) (tramp-point-at-eol))
4883 (when (re-search-forward regexp nil t
)
4884 (setq result
(list nil
(match-string 1))))
4887 (> (skip-chars-forward ",") 0)
4891 (defun tramp-parse-shostkeys (dirname)
4892 "Return a list of (user host) tuples allowed to access.
4893 User is always nil."
4895 (let ((regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp
"\\)\\.pub$"))
4896 (files (when (file-directory-p dirname
) (directory-files dirname
)))
4900 (when (string-match regexp
(car files
))
4901 (push (list nil
(match-string 1 (car files
))) result
))
4902 (setq files
(cdr files
)))
4905 (defun tramp-parse-sknownhosts (dirname)
4906 "Return a list of (user host) tuples allowed to access.
4907 User is always nil."
4909 (let ((regexp (concat "^\\(" tramp-host-regexp
4910 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
4911 (files (when (file-directory-p dirname
) (directory-files dirname
)))
4915 (when (string-match regexp
(car files
))
4916 (push (list nil
(match-string 1 (car files
))) result
))
4917 (setq files
(cdr files
)))
4920 (defun tramp-parse-hosts (filename)
4921 "Return a list of (user host) tuples allowed to access.
4922 User is always nil."
4925 (when (file-readable-p filename
)
4927 (insert-file-contents filename
)
4928 (goto-char (point-min))
4930 (push (tramp-parse-hosts-group) res
))))
4933 (defun tramp-parse-hosts-group ()
4934 "Return a (user host) tuple allowed to access.
4935 User is always nil."
4938 (regexp (concat "^\\(" tramp-host-regexp
"\\)")))
4940 (narrow-to-region (point) (tramp-point-at-eol))
4941 (when (re-search-forward regexp nil t
)
4942 (unless (char-equal (or (char-after) ?
\n) ?
:) ; no IPv6
4943 (setq result
(list nil
(match-string 1)))))
4946 (> (skip-chars-forward " \t") 0)
4950 ;; For su-alike methods it would be desirable to return "root@localhost"
4951 ;; as default. Unfortunately, we have no information whether any user name
4952 ;; has been typed already. So we (mis-)use tramp-current-user as indication,
4953 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
4954 (defun tramp-parse-passwd (filename)
4955 "Return a list of (user host) tuples allowed to access.
4956 Host is always \"localhost\"."
4959 (if (zerop (length tramp-current-user
))
4961 (when (file-readable-p filename
)
4963 (insert-file-contents filename
)
4964 (goto-char (point-min))
4966 (push (tramp-parse-passwd-group) res
))))
4969 (defun tramp-parse-passwd-group ()
4970 "Return a (user host) tuple allowed to access.
4971 Host is always \"localhost\"."
4974 (regexp (concat "^\\(" tramp-user-regexp
"\\):")))
4976 (narrow-to-region (point) (tramp-point-at-eol))
4977 (when (re-search-forward regexp nil t
)
4978 (setq result
(list (match-string 1) "localhost")))
4983 (defun tramp-parse-netrc (filename)
4984 "Return a list of (user host) tuples allowed to access.
4988 (when (file-readable-p filename
)
4990 (insert-file-contents filename
)
4991 (goto-char (point-min))
4993 (push (tramp-parse-netrc-group) res
))))
4996 (defun tramp-parse-netrc-group ()
4997 "Return a (user host) tuple allowed to access.
5003 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp
"\\)"
5004 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp
"\\)" "\\)?")))
5006 (narrow-to-region (point) (tramp-point-at-eol))
5007 (when (re-search-forward regexp nil t
)
5008 (setq result
(list (match-string 3) (match-string 1))))
5013 ;;; Internal Functions:
5015 (defun tramp-maybe-send-perl-script (multi-method method user host script name
)
5016 "Define in remote shell function NAME implemented as perl SCRIPT.
5017 Only send the definition if it has not already been done.
5018 Function may have 0-3 parameters."
5019 (let ((remote-perl (tramp-get-remote-perl multi-method method user host
)))
5020 (unless remote-perl
(error "No remote perl"))
5021 (let ((perl-scripts (tramp-get-connection-property "perl-scripts" nil
5022 multi-method method user host
)))
5023 (unless (memq name perl-scripts
)
5024 (with-current-buffer (tramp-get-buffer multi-method method user host
)
5025 (tramp-message 5 (concat "Sending the Perl script `" name
"'..."))
5026 (tramp-send-string multi-method method user host
5032 "' \"$1\" \"$2\" \"$3\" 2>/dev/null\n}"))
5033 (tramp-wait-for-output)
5034 (tramp-set-connection-property "perl-scripts" (cons name perl-scripts
)
5035 multi-method method user host
)
5036 (tramp-message 5 (concat "Sending the Perl script `" name
"'...done.")))))))
5038 (defun tramp-set-auto-save ()
5039 (when (and (buffer-file-name)
5040 (tramp-tramp-file-p (buffer-file-name))
5041 ;; ange-ftp has its own auto-save mechanism
5042 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
5043 'tramp-sh-file-name-handler
)
5045 (auto-save-mode 1)))
5046 (add-hook 'find-file-hooks
'tramp-set-auto-save t
)
5047 (add-hook 'tramp-unload-hook
5049 (remove-hook 'find-file-hooks
'tramp-set-auto-save
)))
5051 (defun tramp-run-test (switch filename
)
5052 "Run `test' on the remote system, given a SWITCH and a FILENAME.
5053 Returns the exit code of the `test' program."
5054 (let ((v (tramp-dissect-file-name filename
)))
5056 (tramp-send-command-and-check
5057 (tramp-file-name-multi-method v
) (tramp-file-name-method v
)
5058 (tramp-file-name-user v
) (tramp-file-name-host v
)
5059 (format "test %s %s" switch
5060 (tramp-shell-quote-argument (tramp-file-name-localname v
)))))))
5062 (defun tramp-run-test2 (program file1 file2
&optional switch
)
5063 "Run `test'-like PROGRAM on the remote system, given FILE1, FILE2.
5064 The optional SWITCH is inserted between the two files.
5065 Returns the exit code of the `test' PROGRAM. Barfs if the methods,
5066 hosts, or files, disagree."
5067 (let* ((v1 (tramp-dissect-file-name file1
))
5068 (v2 (tramp-dissect-file-name file2
))
5069 (mmethod1 (tramp-file-name-multi-method v1
))
5070 (mmethod2 (tramp-file-name-multi-method v2
))
5071 (method1 (tramp-file-name-method v1
))
5072 (method2 (tramp-file-name-method v2
))
5073 (user1 (tramp-file-name-user v1
))
5074 (user2 (tramp-file-name-user v2
))
5075 (host1 (tramp-file-name-host v1
))
5076 (host2 (tramp-file-name-host v2
))
5077 (localname1 (tramp-file-name-localname v1
))
5078 (localname2 (tramp-file-name-localname v2
)))
5079 (unless (and method1 method2 host1 host2
5080 (equal mmethod1 mmethod2
)
5081 (equal method1 method2
)
5083 (equal host1 host2
))
5084 (error "tramp-run-test2: %s"
5085 "only implemented for same method, same user, same host"))
5087 (tramp-send-command-and-check
5088 mmethod1 method1 user1 host1
5089 (format "%s %s %s %s"
5091 (tramp-shell-quote-argument localname1
)
5093 (tramp-shell-quote-argument localname2
))))))
5095 (defun tramp-touch (file time
)
5096 "Set the last-modified timestamp of the given file.
5097 TIME is an Emacs internal time value as returned by `current-time'."
5098 (let ((touch-time (format-time-string "%Y%m%d%H%M.%S" time t
)))
5099 (if (tramp-tramp-file-p file
)
5100 (with-parsed-tramp-file-name file nil
5101 (let ((buf (tramp-get-buffer multi-method method user host
)))
5102 (unless (zerop (tramp-send-command-and-check
5103 multi-method method user host
5104 (format "TZ=UTC; export TZ; touch -t %s %s"
5106 (tramp-shell-quote-argument localname
))
5109 (error "tramp-touch: touch failed, see buffer `%s' for details"
5111 ;; It's a local file
5113 (unless (zerop (call-process
5114 "touch" nil
(current-buffer) nil
"-t" touch-time file
))
5115 (pop-to-buffer (current-buffer))
5116 (error "tramp-touch: touch failed"))))))
5118 (defun tramp-buffer-name (multi-method method user host
)
5119 "A name for the connection buffer for USER at HOST using METHOD."
5121 (tramp-buffer-name-multi-method "tramp" multi-method method user host
)
5122 (let ((method (tramp-find-method multi-method method user host
)))
5124 (format "*tramp/%s %s@%s*" method user host
)
5125 (format "*tramp/%s %s*" method host
)))))
5127 (defun tramp-buffer-name-multi-method (prefix multi-method method user host
)
5128 "A name for the multi method connection buffer.
5129 MULTI-METHOD gives the multi method, METHOD the array of methods,
5130 USER the array of user names, HOST the array of host names."
5131 (unless (and (= (length method
) (length user
))
5132 (= (length method
) (length host
)))
5133 (error "Syntax error in multi method (implementation error)"))
5134 (let ((len (length method
))
5139 (cons (if (aref user i
)
5140 (format "%s#%s@%s:" (aref method i
)
5141 (aref user i
) (aref host i
))
5142 (format "%s@%s:" (aref method i
) (aref host i
)))
5145 (format "*%s/%s %s*"
5147 (apply 'concat
(reverse string-list
)))))
5149 (defun tramp-get-buffer (multi-method method user host
)
5150 "Get the connection buffer to be used for USER at HOST using METHOD."
5151 (with-current-buffer
5152 (get-buffer-create (tramp-buffer-name multi-method method user host
))
5153 (setq buffer-undo-list t
)
5156 (defun tramp-debug-buffer-name (multi-method method user host
)
5157 "A name for the debug buffer for USER at HOST using METHOD."
5159 (tramp-buffer-name-multi-method "debug tramp"
5160 multi-method method user host
)
5161 (let ((method (tramp-find-method multi-method method user host
)))
5163 (format "*debug tramp/%s %s@%s*" method user host
)
5164 (format "*debug tramp/%s %s*" method host
)))))
5166 (defun tramp-get-debug-buffer (multi-method method user host
)
5167 "Get the debug buffer for USER at HOST using METHOD."
5168 (with-current-buffer
5170 (tramp-debug-buffer-name multi-method method user host
))
5171 (setq buffer-undo-list t
)
5174 (defun tramp-find-executable (multi-method method user host
5175 progname dirlist ignore-tilde
)
5176 "Searches for PROGNAME in all directories mentioned in DIRLIST.
5177 First args METHOD, USER and HOST specify the connection, PROGNAME
5178 is the program to search for, and DIRLIST gives the list of directories
5179 to search. If IGNORE-TILDE is non-nil, directory names starting
5180 with `~' will be ignored.
5182 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
5184 This function expects to be in the right *tramp* buffer."
5187 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
5188 ;; `remove' is in CL, and we want to avoid CL dependencies.
5191 (setq d
(car dirlist
))
5192 (setq dirlist
(cdr dirlist
))
5193 (unless (char-equal ?~
(aref d
0))
5194 (setq newdl
(cons d newdl
))))
5195 (setq dirlist
(nreverse newdl
))))
5197 multi-method method user host
5198 (format (concat "while read d; "
5199 "do if test -x $d/%s -a -f $d/%s; "
5200 "then echo tramp_executable $d/%s; "
5201 "break; fi; done <<'EOF'")
5202 progname progname progname
))
5204 (tramp-send-command multi-method method user host d
))
5206 (tramp-send-command multi-method method user host
"EOF")
5207 (tramp-wait-for-output)
5208 (goto-char (point-max))
5209 (when (search-backward "tramp_executable " nil t
)
5210 (skip-chars-forward "^ ")
5211 (skip-chars-forward " ")
5212 (buffer-substring (point) (tramp-line-end-position)))))
5214 (defun tramp-set-remote-path (multi-method method user host var dirlist
)
5215 "Sets the remote environment VAR to existing directories from DIRLIST.
5216 I.e., for each directory in DIRLIST, it is tested whether it exists and if
5217 so, it is added to the environment variable VAR."
5218 (let ((existing-dirs
5223 (tramp-make-tramp-file-name multi-method method user host x
))
5225 (tramp-make-tramp-file-name multi-method method user host x
)))
5229 multi-method method user host
5231 (mapconcat 'identity
(delq nil existing-dirs
) ":")
5233 (tramp-wait-for-output)))
5235 ;; -- communication with external shell --
5237 (defun tramp-find-file-exists-command (multi-method method user host
)
5238 "Find a command on the remote host for checking if a file exists.
5239 Here, we are looking for a command which has zero exit status if the
5240 file exists and nonzero exit status otherwise."
5241 (make-local-variable 'tramp-file-exists-command
)
5242 (tramp-message 9 "Finding command to check if file exists")
5244 (tramp-make-tramp-file-name
5245 multi-method method user host
5246 "/")) ;assume this file always exists
5248 (tramp-make-tramp-file-name
5249 multi-method method user host
5250 "/ this file does not exist "))) ;assume this never exists
5251 ;; The algorithm is as follows: we try a list of several commands.
5252 ;; For each command, we first run `$cmd /' -- this should return
5253 ;; true, as the root directory always exists. And then we run
5254 ;; `$cmd /this\ file\ does\ not\ exist', hoping that the file indeed
5255 ;; does not exist. This should return false. We use the first
5256 ;; command we find that seems to work.
5257 ;; The list of commands to try is as follows:
5258 ;; `ls -d' This works on most systems, but NetBSD 1.4
5259 ;; has a bug: `ls' always returns zero exit
5260 ;; status, even for files which don't exist.
5261 ;; `test -e' Some Bourne shells have a `test' builtin
5262 ;; which does not know the `-e' option.
5263 ;; `/bin/test -e' For those, the `test' binary on disk normally
5264 ;; provides the option. Alas, the binary
5265 ;; is sometimes `/bin/test' and sometimes it's
5267 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
5269 (and (setq tramp-file-exists-command
"test -e %s")
5270 (file-exists-p existing
)
5271 (not (file-exists-p nonexisting
)))
5272 (and (setq tramp-file-exists-command
"/bin/test -e %s")
5273 (file-exists-p existing
)
5274 (not (file-exists-p nonexisting
)))
5275 (and (setq tramp-file-exists-command
"/usr/bin/test -e %s")
5276 (file-exists-p existing
)
5277 (not (file-exists-p nonexisting
)))
5278 (and (setq tramp-file-exists-command
"ls -d %s")
5279 (file-exists-p existing
)
5280 (not (file-exists-p nonexisting
))))
5281 (error "Couldn't find command to check if file exists"))))
5284 ;; CCC test ksh or bash found for tilde expansion?
5285 (defun tramp-find-shell (multi-method method user host
)
5286 "Find a shell on the remote host which groks tilde expansion."
5288 (tramp-send-command multi-method method user host
"echo ~root")
5289 (tramp-wait-for-output)
5291 ((string-match "^~root$" (buffer-string))
5293 (or (tramp-find-executable multi-method method user host
5294 "bash" tramp-remote-path t
)
5295 (tramp-find-executable multi-method method user host
5296 "ksh" tramp-remote-path t
)))
5298 (error "Couldn't find a shell which groks tilde expansion"))
5299 ;; Find arguments for this shell.
5300 (let ((alist tramp-sh-extra-args
)
5302 (while (and alist
(null extra-args
))
5303 (setq item
(pop alist
))
5304 (when (string-match (car item
) shell
)
5305 (setq extra-args
(cdr item
))))
5306 (when extra-args
(setq shell
(concat shell
" " extra-args
))))
5308 5 "Starting remote shell `%s' for tilde expansion..." shell
)
5310 multi-method method user host
5311 (concat "PS1='$ ' exec " shell
)) ;
5312 (tramp-barf-if-no-shell-prompt
5313 (get-buffer-process (current-buffer))
5314 60 "Couldn't find remote `%s' prompt" shell
)
5316 9 "Setting remote shell prompt...")
5317 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we
5318 ;; must use "\n" here, not tramp-rsh-end-of-line. Kai left the
5319 ;; last tramp-rsh-end-of-line, Douglas wanted to replace that,
5321 (process-send-string nil
(format "PS1='%s%s%s'; PS2=''; PS3=''%s"
5322 tramp-rsh-end-of-line
5324 tramp-rsh-end-of-line
5325 tramp-rsh-end-of-line
))
5326 (tramp-wait-for-output)
5328 9 "Setting remote shell prompt...done")
5330 (t (tramp-message 5 "Remote `%s' groks tilde expansion, good"
5331 (tramp-get-method-parameter
5332 multi-method method user host
'tramp-remote-sh
))))))
5334 (defun tramp-check-ls-command (multi-method method user host cmd
)
5335 "Checks whether the given `ls' executable groks `-n'.
5336 METHOD, USER and HOST specify the connection, CMD (the absolute file name of)
5337 the `ls' executable. Returns t if CMD supports the `-n' option, nil
5339 (tramp-message 9 "Checking remote `%s' command for `-n' option" cmd
)
5340 (when (file-executable-p
5341 (tramp-make-tramp-file-name multi-method method user host cmd
))
5343 (tramp-message 7 "Testing remote command `%s' for -n..." cmd
)
5345 (tramp-send-command-and-check
5346 multi-method method user host
5347 (format "%s -lnd / >/dev/null"
5349 (tramp-message 7 "Testing remote command `%s' for -n...%s"
5351 (if (zerop result
) "okay" "failed"))
5354 (defun tramp-check-ls-commands (multi-method method user host cmd dirlist
)
5355 "Checks whether the given `ls' executable in one of the dirs groks `-n'.
5356 Returns nil if none was found, else the command is returned."
5359 (tramp-let-maybe directory-sep-char ?
/ ;for XEmacs
5360 ;; It would be better to use the CL function `find', but
5361 ;; we don't want run-time dependencies on CL.
5362 (while (and dl
(not result
))
5363 (let ((x (concat (file-name-as-directory (car dl
)) cmd
)))
5364 (when (tramp-check-ls-command multi-method method user host x
)
5369 (defun tramp-find-ls-command (multi-method method user host
)
5370 "Finds an `ls' command which groks the `-n' option, returning nil if failed.
5371 \(This option prints numeric user and group ids in a long listing.)"
5372 (tramp-message 9 "Finding a suitable `ls' command")
5374 (tramp-check-ls-commands multi-method method user host
"ls" tramp-remote-path
)
5375 (tramp-check-ls-commands multi-method method user host
"gnuls" tramp-remote-path
)
5376 (tramp-check-ls-commands multi-method method user host
"gls" tramp-remote-path
)))
5378 ;; ------------------------------------------------------------
5379 ;; -- Functions for establishing connection --
5380 ;; ------------------------------------------------------------
5382 ;; The following functions are actions to be taken when seeing certain
5383 ;; prompts from the remote host. See the variable
5384 ;; `tramp-actions-before-shell' for usage of these functions.
5386 (defun tramp-action-login (p multi-method method user host
)
5387 "Send the login name."
5388 (tramp-message 9 "Sending login name `%s'"
5389 (or user
(user-login-name)))
5391 (process-send-string nil
(concat (or user
(user-login-name))
5392 tramp-rsh-end-of-line
)))
5394 (defun tramp-action-password (p multi-method method user host
)
5395 "Query the user for a password."
5397 (format "Password for %s "
5398 (tramp-make-tramp-file-name
5399 nil method user host
""))))
5400 (tramp-message 9 "Sending password")
5401 (tramp-enter-password p pw-prompt user host
)))
5403 (defun tramp-action-succeed (p multi-method method user host
)
5404 "Signal success in finding shell prompt."
5405 (tramp-message 9 "Found remote shell prompt.")
5407 (throw 'tramp-action
'ok
))
5409 (defun tramp-action-permission-denied (p multi-method method user host
)
5410 "Signal permission denied."
5411 (pop-to-buffer (tramp-get-buffer multi-method method user host
))
5412 (tramp-message 9 "Permission denied by remote host.")
5414 (throw 'tramp-action
'permission-denied
))
5416 (defun tramp-action-copy-failed (p multi-method method user host
)
5417 "Signal copy failed."
5419 (error "%s" (match-string 1)))
5421 (defun tramp-action-yesno (p multi-method method user host
)
5422 "Ask the user for confirmation using `yes-or-no-p'.
5423 Send \"yes\" to remote process on confirmation, abort otherwise.
5424 See also `tramp-action-yn'."
5425 (save-window-excursion
5426 (pop-to-buffer (tramp-get-buffer multi-method method user host
))
5427 (unless (yes-or-no-p (match-string 0))
5430 (throw 'tramp-action
'permission-denied
))
5431 (process-send-string p
(concat "yes" tramp-rsh-end-of-line
))
5434 (defun tramp-action-yn (p multi-method method user host
)
5435 "Ask the user for confirmation using `y-or-n-p'.
5436 Send \"y\" to remote process on confirmation, abort otherwise.
5437 See also `tramp-action-yesno'."
5438 (save-window-excursion
5439 (pop-to-buffer (tramp-get-buffer multi-method method user host
))
5440 (unless (y-or-n-p (match-string 0))
5442 (throw 'tramp-action
'permission-denied
))
5444 (process-send-string p
(concat "y" tramp-rsh-end-of-line
))))
5446 (defun tramp-action-terminal (p multi-method method user host
)
5447 "Tell the remote host which terminal type to use.
5448 The terminal type can be configured with `tramp-terminal-type'."
5449 (tramp-message 9 "Setting `%s' as terminal type."
5450 tramp-terminal-type
)
5452 (process-send-string nil
(concat tramp-terminal-type
5453 tramp-rsh-end-of-line
)))
5455 (defun tramp-action-process-alive (p multi-method method user host
)
5456 "Check whether a process has finished."
5457 (unless (memq (process-status p
) '(run open
))
5458 (throw 'tramp-action
'process-died
)))
5460 (defun tramp-action-out-of-band (p multi-method method user host
)
5461 "Check whether an out-of-band copy has finished."
5462 (cond ((and (memq (process-status p
) '(stop exit
))
5463 (zerop (process-exit-status p
)))
5464 (tramp-message 9 "Process has finished.")
5465 (throw 'tramp-action
'ok
))
5466 ((or (and (memq (process-status p
) '(stop exit
))
5467 (not (zerop (process-exit-status p
))))
5468 (memq (process-status p
) '(signal)))
5469 ;; `scp' could have copied correctly, but set modes could have failed.
5470 ;; This can be ignored.
5471 (goto-char (point-min))
5472 (if (re-search-forward tramp-operation-not-permitted-regexp nil t
)
5474 (tramp-message 10 "'set mode' error ignored.")
5475 (tramp-message 9 "Process has finished.")
5476 (throw 'tramp-action
'ok
))
5477 (tramp-message 9 "Process has died.")
5478 (throw 'tramp-action
'process-died
)))
5481 ;; The following functions are specifically for multi connections.
5483 (defun tramp-multi-action-login (p method user host
)
5484 "Send the login name."
5485 (tramp-message 9 "Sending login name `%s'" user
)
5487 (process-send-string p
(concat user tramp-rsh-end-of-line
)))
5489 (defun tramp-multi-action-password (p method user host
)
5490 "Query the user for a password."
5492 (format "Password for %s "
5493 (tramp-make-tramp-file-name
5494 nil method user host
""))))
5495 (tramp-message 9 "Sending password")
5496 (tramp-enter-password p pw-prompt user host
)))
5498 (defun tramp-multi-action-succeed (p method user host
)
5499 "Signal success in finding shell prompt."
5500 (tramp-message 9 "Found shell prompt on `%s'" host
)
5502 (throw 'tramp-action
'ok
))
5504 (defun tramp-multi-action-permission-denied (p method user host
)
5505 "Signal permission denied."
5506 (tramp-message 9 "Permission denied by remote host `%s'" host
)
5509 (throw 'tramp-action
'permission-denied
))
5511 (defun tramp-multi-action-process-alive (p method user host
)
5512 "Check whether a process has finished."
5513 (unless (memq (process-status p
) '(run open
))
5514 (throw 'tramp-action
'process-died
)))
5516 ;; Functions for processing the actions.
5518 (defun tramp-process-one-action (p multi-method method user host actions
)
5519 "Wait for output from the shell and perform one action."
5520 (let (found item pattern action todo
)
5522 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5523 (with-timeout (60 (throw 'tramp-action
'timeout
))
5525 (tramp-accept-process-output p
1)
5526 (goto-char (point-min))
5529 (goto-char (point-min))
5530 (setq item
(pop todo
))
5531 (setq pattern
(symbol-value (nth 0 item
)))
5532 (setq action
(nth 1 item
))
5533 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5535 (when (re-search-forward (concat pattern
"\\'") nil t
)
5536 (setq found
(funcall action p multi-method method user host
)))))
5539 (defun tramp-process-actions (p multi-method method user host actions
)
5540 "Perform actions until success."
5541 (tramp-message 10 "%s" (mapconcat 'identity
(process-command p
) " "))
5544 (tramp-message 9 "Waiting for prompts from remote shell")
5546 (catch 'tramp-action
5547 (tramp-process-one-action
5548 p multi-method method user host actions
)
5550 (unless (eq exit
'ok
)
5551 (tramp-clear-passwd user host
)
5552 (error "Login failed"))))
5554 ;; For multi-actions.
5556 (defun tramp-process-one-multi-action (p method user host actions
)
5557 "Wait for output from the shell and perform one action."
5558 (let (found item pattern action todo
)
5560 (tramp-message 9 "Waiting 60s for prompt from remote shell")
5561 (with-timeout (60 (throw 'tramp-action
'timeout
))
5563 (tramp-accept-process-output p
1)
5565 (goto-char (point-min))
5567 (goto-char (point-min))
5568 (setq item
(pop todo
))
5569 (setq pattern
(symbol-value (nth 0 item
)))
5570 (setq action
(nth 1 item
))
5571 (tramp-message 10 "Looking for regexp \"%s\" from remote shell"
5573 (when (re-search-forward (concat pattern
"\\'") nil t
)
5574 (setq found
(funcall action p method user host
)))))
5577 (defun tramp-process-multi-actions (p method user host actions
)
5578 "Perform actions until success."
5581 (tramp-message 9 "Waiting for prompts from remote shell")
5583 (catch 'tramp-action
5584 (tramp-process-one-multi-action p method user host actions
)
5586 (unless (eq exit
'ok
)
5587 (tramp-clear-passwd user host
)
5588 (error "Login failed"))))
5590 ;; Functions to execute when we have seen the remote shell prompt but
5591 ;; before we exec the Bourne-ish shell. Note that these commands
5592 ;; might be sent to any shell, not just a Bourne-ish shell. This
5593 ;; means that the commands need to work in all shells. (It is also
5594 ;; okay for some commands to just fail with an error message, but
5595 ;; please make sure that they at least don't crash the odd shell people
5596 ;; might be running...)
5597 (defun tramp-process-initial-commands (p
5598 multi-method method user host
5600 "Send list of commands to remote host, in order."
5603 (setq cmd
(pop commands
))
5605 (tramp-message 10 "Sending command to remote shell: %s"
5607 (tramp-send-command multi-method method user host cmd nil t
)
5608 (tramp-barf-if-no-shell-prompt
5609 p
60 "Remote shell command failed: %s" cmd
))
5612 ;; The actual functions for opening connections.
5614 (defun tramp-open-connection-telnet (multi-method method user host
)
5615 "Open a connection using a telnet METHOD.
5616 This starts the command `telnet HOST ARGS'[*], then waits for a remote
5617 login prompt, then sends the user name USER, then waits for a remote
5618 password prompt. It queries the user for the password, then sends the
5619 password to the remote host.
5621 If USER is nil, uses value returned by `(user-login-name)' instead.
5623 Recognition of the remote shell prompt is based on the variables
5624 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5627 Please note that it is NOT possible to use this connection method
5628 together with an out-of-band transfer method! You must use an inline
5631 Maybe the different regular expressions need to be tuned.
5633 * Actually, the telnet program as well as the args to be used can be
5634 specified in the method parameters, see the variable `tramp-methods'."
5636 (when (tramp-method-out-of-band-p multi-method method user host
)
5637 (error "Cannot use out-of-band method `%s' with telnet connection method"
5640 (error "Cannot multi-connect using telnet connection method"))
5641 (tramp-pre-connection multi-method method user host tramp-chunksize
)
5642 (tramp-message 7 "Opening connection for %s@%s using %s..."
5643 (or user
(user-login-name)) host method
)
5644 (let ((process-environment (copy-sequence process-environment
)))
5645 (setenv "TERM" tramp-terminal-type
)
5647 (let* ((default-directory (tramp-temporary-file-directory))
5648 ;; If we omit the conditional here, then we would use
5649 ;; `undecided-dos' in some cases. With the conditional,
5650 ;; we use nil in these cases. Which one is right?
5651 (coding-system-for-read (unless (and (not (featurep 'xemacs
))
5652 (> emacs-major-version
20))
5653 tramp-dos-coding-system
))
5654 (p (apply 'start-process
5655 (tramp-buffer-name multi-method method user host
)
5656 (tramp-get-buffer multi-method method user host
)
5657 (tramp-get-method-parameter
5659 (tramp-find-method multi-method method user host
)
5660 user host
'tramp-login-program
)
5662 (tramp-get-method-parameter
5664 (tramp-find-method multi-method method user host
)
5665 user host
'tramp-login-args
)))
5668 (tramp-set-process-query-on-exit-flag p nil
)
5669 (set-buffer (tramp-get-buffer multi-method method user host
))
5671 (tramp-process-actions p multi-method method user host
5672 tramp-actions-before-shell
)
5673 (tramp-open-connection-setup-interactive-shell
5674 p multi-method method user host
)
5675 (tramp-post-connection multi-method method user host
)))))
5678 (defun tramp-open-connection-rsh (multi-method method user host
)
5679 "Open a connection using an rsh METHOD.
5680 This starts the command `rsh HOST -l USER'[*], then waits for a remote
5681 password or shell prompt. If a password prompt is seen, the user is
5682 queried for a password, this function sends the password to the remote
5683 host and waits for a shell prompt.
5685 If USER is nil, start the command `rsh HOST'[*] instead
5687 Recognition of the remote shell prompt is based on the variables
5688 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5691 Kludgy feature: if HOST has the form \"xx#yy\", then yy is assumed to
5692 be a port number for ssh, and \"-p yy\" will be added to the list of
5693 arguments, and xx will be used as the host name to connect to.
5695 * Actually, the rsh program to be used can be specified in the
5696 method parameters, see the variable `tramp-methods'."
5699 (error "Cannot multi-connect using rsh connection method"))
5700 (tramp-pre-connection multi-method method user host tramp-chunksize
)
5701 (if (and user
(not (string= user
"")))
5702 (tramp-message 7 "Opening connection for %s@%s using %s..."
5704 (tramp-message 7 "Opening connection at %s using %s..." host method
))
5705 (let ((process-environment (copy-sequence process-environment
))
5706 (bufnam (tramp-buffer-name multi-method method user host
))
5707 (buf (tramp-get-buffer multi-method method user host
))
5708 (login-program (tramp-get-method-parameter
5710 (tramp-find-method multi-method method user host
)
5711 user host
'tramp-login-program
))
5715 x
`((?t .
,(format "/tmp/%s" tramp-temp-name-prefix
)))))
5716 (tramp-get-method-parameter
5718 (tramp-find-method multi-method method user host
)
5719 user host
'tramp-login-args
)))
5721 ;; The following should be changed. We need a more general
5722 ;; mechanism to parse extra host args.
5723 (when (string-match "\\([^#]*\\)#\\(.*\\)" host
)
5724 (setq login-args
(cons "-p" (cons (match-string 2 host
) login-args
)))
5725 (setq real-host
(match-string 1 host
)))
5726 (setenv "TERM" tramp-terminal-type
)
5728 (let* ((default-directory (tramp-temporary-file-directory))
5729 ;; If we omit the conditional, we would use
5730 ;; `undecided-dos' in some cases. With the conditional,
5731 ;; we use nil in these cases. Which one is right?
5732 (coding-system-for-read (unless (and (not (featurep 'xemacs
))
5733 (> emacs-major-version
20))
5734 tramp-dos-coding-system
))
5735 (p (if (and user
(not (string= user
"")))
5736 (apply #'start-process bufnam buf login-program
5737 real-host
"-l" user login-args
)
5738 (apply #'start-process bufnam buf login-program
5739 real-host login-args
)))
5741 (tramp-set-process-query-on-exit-flag p nil
)
5744 (tramp-process-actions p multi-method method user host
5745 tramp-actions-before-shell
)
5746 (tramp-message 7 "Initializing remote shell")
5747 (tramp-open-connection-setup-interactive-shell
5748 p multi-method method user host
)
5749 (tramp-post-connection multi-method method user host
)))))
5751 (defun tramp-open-connection-su (multi-method method user host
)
5752 "Open a connection using the `su' program with METHOD.
5753 This starts `su - USER', then waits for a password prompt. The HOST
5754 name must be equal to the local host name or to `localhost'.
5756 If USER is nil, uses value returned by user-login-name instead.
5758 Recognition of the remote shell prompt is based on the variables
5759 `shell-prompt-pattern' and `tramp-shell-prompt-pattern' which must be
5760 set up correctly. Note that the other user may have a different shell
5761 prompt than you do, so it is not at all unlikely that the variable
5762 `shell-prompt-pattern' is set up wrongly!"
5764 (when (tramp-method-out-of-band-p multi-method method user host
)
5765 (error "Cannot use out-of-band method `%s' with `su' connection method"
5767 (unless (or (string-match (concat "^" (regexp-quote host
))
5769 (string= "localhost" host
)
5772 "Cannot connect to different host `%s' with `su' connection method"
5774 (tramp-pre-connection multi-method method user host tramp-chunksize
)
5775 (tramp-message 7 "Opening connection for `%s' using `%s'..."
5776 (or user
"<root>") method
)
5777 (let ((process-environment (copy-sequence process-environment
)))
5778 (setenv "TERM" tramp-terminal-type
)
5780 (let* ((default-directory (tramp-temporary-file-directory))
5781 ;; If we omit the conditional, we use `undecided-dos' in
5782 ;; some cases. With the conditional, we use nil in these
5783 ;; cases. What's the difference? Which one is right?
5784 (coding-system-for-read (unless (and (not (featurep 'xemacs
))
5785 (> emacs-major-version
20))
5786 tramp-dos-coding-system
))
5787 (p (apply 'start-process
5788 (tramp-buffer-name multi-method method user host
)
5789 (tramp-get-buffer multi-method method user host
)
5790 (tramp-get-method-parameter
5792 (tramp-find-method multi-method method user host
)
5793 user host
'tramp-login-program
)
5796 (format-spec x
`((?u .
,(or user
"root")))))
5797 (tramp-get-method-parameter
5799 (tramp-find-method multi-method method user host
)
5800 user host
'tramp-login-args
))))
5803 (tramp-set-process-query-on-exit-flag p nil
)
5804 (set-buffer (tramp-get-buffer multi-method method user host
))
5805 (tramp-process-actions p multi-method method user host
5806 tramp-actions-before-shell
)
5807 (tramp-open-connection-setup-interactive-shell
5808 p multi-method method user host
)
5809 (tramp-post-connection multi-method method
5812 ;; HHH: Not Changed. Multi method. It is not clear to me how this can
5813 ;; handle not giving a user name in the "file name".
5815 ;; This is more difficult than for the single-hop method. In the
5816 ;; multi-hop-method, the desired behaviour should be that the
5817 ;; user must specify names for the telnet hops of which the user
5818 ;; name is different than the "original" name (or different from
5819 ;; the previous hop.
5820 (defun tramp-open-connection-multi (multi-method method user host
)
5821 "Open a multi-hop connection using METHOD.
5822 This uses a slightly changed file name syntax. The idea is to say
5823 [multi/telnet:u1@h1/rsh:u2@h2]/path/to/file
5824 This will use telnet to log in as u1 to h1, then use rsh from there to
5825 log in as u2 to h2."
5827 (unless multi-method
5828 (error "Multi-hop open connection function called on non-multi method"))
5829 (when (tramp-method-out-of-band-p multi-method method user host
)
5830 (error "No out of band multi-hop connections"))
5831 (unless (and (arrayp method
) (not (stringp method
)))
5832 (error "METHOD must be an array of strings for multi methods"))
5833 (unless (and (arrayp user
) (not (stringp user
)))
5834 (error "USER must be an array of strings for multi methods"))
5835 (unless (and (arrayp host
) (not (stringp host
)))
5836 (error "HOST must be an array of strings for multi methods"))
5837 (unless (and (= (length method
) (length user
))
5838 (= (length method
) (length host
)))
5839 (error "Arrays METHOD, USER, HOST must have equal length"))
5840 (tramp-pre-connection multi-method method user host tramp-chunksize
)
5841 (tramp-message 7 "Opening `%s' connection..." multi-method
)
5842 (let ((process-environment (copy-sequence process-environment
)))
5843 (setenv "TERM" tramp-terminal-type
)
5845 (let* ((default-directory (tramp-temporary-file-directory))
5846 ;; If we omit the conditional, we use `undecided-dos' in
5847 ;; some cases. With the conditional, we use nil in these
5848 ;; cases. What's the difference? Which one is right?
5849 (coding-system-for-read (unless (and (not (featurep 'xemacs
))
5850 (> emacs-major-version
20))
5851 tramp-dos-coding-system
))
5852 (p (start-process (tramp-buffer-name multi-method method user host
)
5853 (tramp-get-buffer multi-method method user host
)
5854 tramp-multi-sh-program
))
5855 (num-hops (length method
))
5857 (tramp-set-process-query-on-exit-flag p nil
)
5858 (tramp-message 9 "Waiting 60s for local shell to come up...")
5859 (unless (tramp-wait-for-regexp
5860 p
60 (format "\\(%s\\)\\'\\|\\(%s\\)\\'"
5861 shell-prompt-pattern tramp-shell-prompt-pattern
))
5862 (pop-to-buffer (buffer-name))
5864 (error "Couldn't find local shell prompt"))
5865 ;; Now do all the connections as specified.
5866 (while (< i num-hops
)
5867 (let* ((m (aref method i
))
5870 (entry (assoc m tramp-multi-connection-function-alist
))
5871 (multi-func (nth 1 entry
))
5872 (command (nth 2 entry
)))
5873 ;; The multi-funcs don't need to do save-match-data, as that
5875 (funcall multi-func p m u h command
)
5878 (tramp-open-connection-setup-interactive-shell
5879 p multi-method method user host
)
5880 (tramp-post-connection multi-method method user host
)))))
5882 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5883 ;; of no user name provided. Hack to make it work as it did before:
5884 ;; changed `user' to `(or user (user-login-name))' in the places where
5885 ;; the value is actually used.
5886 (defun tramp-multi-connect-telnet (p method user host command
)
5887 "Issue `telnet' command.
5888 Uses shell COMMAND to issue a `telnet' command to log in as USER to
5889 HOST. You can use percent escapes in COMMAND: `%h' is replaced with
5890 the host name, and `%n' is replaced with an end of line character, as
5891 set in `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5894 If USER is nil, uses the return value of (user-login-name) instead."
5895 (let ((cmd (format-spec command
5896 `((?h .
,host
) (?n .
,tramp-rsh-end-of-line
))))
5897 (cmd1 (format-spec command
`((?h .
,host
) (?n .
""))))
5900 (tramp-message 9 "Sending telnet command `%s'" cmd1
)
5901 (process-send-string p cmd
)
5902 (tramp-process-multi-actions p method user host
5903 tramp-multi-actions
)))
5905 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5906 ;; of no user name provided. Hack to make it work as it did before:
5907 ;; changed `user' to `(or user (user-login-name))' in the places where
5908 ;; the value is actually used.
5909 (defun tramp-multi-connect-rlogin (p method user host command
)
5910 "Issue `rlogin' command.
5911 Uses shell COMMAND to issue an `rlogin' command to log in as USER to
5912 HOST. You can use percent escapes in COMMAND. `%u' will be replaced
5913 with the user name, `%h' will be replaced with the host name, and `%n'
5914 will be replaced with the value of `tramp-rsh-end-of-line'. You can use
5915 `%%' if you want to use a literal percent character.
5917 If USER is nil, uses the return value of (user-login-name) instead."
5918 (let ((cmd (format-spec command
`((?h .
,host
)
5919 (?u .
,(or user
(user-login-name)))
5920 (?n .
,tramp-rsh-end-of-line
))))
5921 (cmd1 (format-spec command
`((?h .
,host
)
5922 (?u .
,(or user
(user-login-name)))
5926 (tramp-message 9 "Sending rlogin command `%s'" cmd1
)
5927 (process-send-string p cmd
)
5928 (tramp-process-multi-actions p method user host
5929 tramp-multi-actions
)))
5931 ;; HHH: Changed. Multi method. Don't know how to handle this in the case
5932 ;; of no user name provided. Hack to make it work as it did before:
5933 ;; changed `user' to `(or user (user-login-name))' in the places where
5934 ;; the value is actually used.
5935 (defun tramp-multi-connect-su (p method user host command
)
5936 "Issue `su' command.
5937 Uses shell COMMAND to issue a `su' command to log in as USER on
5938 HOST. The HOST name is ignored, this just changes the user id on the
5939 host currently logged in to.
5941 If USER is nil, uses the return value of (user-login-name) instead.
5943 You can use percent escapes in the COMMAND. `%u' is replaced with the
5944 user name, and `%n' is replaced with the value of
5945 `tramp-rsh-end-of-line'. Use `%%' if you want a literal percent
5947 (let ((cmd (format-spec command
`((?u .
,(or user
(user-login-name)))
5948 (?n .
,tramp-rsh-end-of-line
))))
5949 (cmd1 (format-spec command
`((?u .
,(or user
(user-login-name)))
5953 (tramp-message 9 "Sending su command `%s'" cmd1
)
5954 (process-send-string p cmd
)
5955 (tramp-process-multi-actions p method user host
5956 tramp-multi-actions
)))
5958 ;; Utility functions.
5960 (defun tramp-accept-process-output
5961 (&optional process timeout timeout-msecs
)
5962 "Like `accept-process-output' for Tramp processes.
5963 This is needed in order to hide `last-coding-system-used', which is set
5964 for process communication also."
5965 (let (last-coding-system-used)
5966 (accept-process-output process timeout timeout-msecs
)))
5968 (defun tramp-wait-for-regexp (proc timeout regexp
)
5969 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
5970 Expects the output of PROC to be sent to the current buffer. Returns
5971 the string that matched, or nil. Waits indefinitely if TIMEOUT is
5974 (start-time (current-time)))
5976 ;; Work around a bug in XEmacs 21, where the timeout
5977 ;; expires faster than it should. This degenerates
5978 ;; to polling for buggy XEmacsen, but oh, well.
5979 (while (and (not found
)
5980 (< (tramp-time-diff (current-time) start-time
)
5982 (with-timeout (timeout)
5984 (tramp-accept-process-output proc
1)
5985 (unless (memq (process-status proc
) '(run open
))
5986 (error "Process has died"))
5987 (goto-char (point-min))
5988 (setq found
(re-search-forward regexp nil t
))))))
5991 (tramp-accept-process-output proc
1)
5992 (unless (memq (process-status proc
) '(run open
))
5993 (error "Process has died"))
5994 (goto-char (point-min))
5995 (setq found
(re-search-forward regexp nil t
)))))
5996 (when tramp-debug-buffer
5998 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
5999 tramp-current-user tramp-current-host
)
6000 (point-min) (point-max))
6004 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6005 tramp-current-user tramp-current-host
))
6006 (goto-char (point-max))
6007 (insert "[[Regexp `" regexp
"' not found"
6008 (if timeout
(format " in %d secs" timeout
) "")
6012 (defun tramp-wait-for-shell-prompt (proc timeout
)
6013 "Wait for the shell prompt to appear from process PROC within TIMEOUT seconds.
6014 See `tramp-wait-for-regexp' for more details.
6015 Shell prompt pattern is determined by variables `shell-prompt-pattern'
6016 and `tramp-shell-prompt-pattern'."
6017 (tramp-wait-for-regexp
6019 (format "\\(%s\\|%s\\)\\'"
6020 shell-prompt-pattern tramp-shell-prompt-pattern
)))
6022 (defun tramp-barf-if-no-shell-prompt (proc timeout
&rest error-args
)
6023 "Wait for shell prompt and barf if none appears.
6024 Looks at process PROC to see if a shell prompt appears in TIMEOUT
6025 seconds. If not, it produces an error message with the given ERROR-ARGS."
6026 (unless (tramp-wait-for-shell-prompt proc timeout
)
6027 (pop-to-buffer (buffer-name))
6028 (apply 'error error-args
)))
6030 (defun tramp-enter-password (p prompt user host
)
6031 "Prompt for a password and send it to the remote end.
6032 Uses PROMPT as a prompt and sends the password to process P."
6033 (let ((pw (tramp-read-passwd user host prompt
)))
6035 (process-send-string
6037 (or (tramp-get-method-parameter
6038 tramp-current-multi-method
6039 tramp-current-method
6042 'tramp-password-end-of-line
)
6043 tramp-default-password-end-of-line
)))))
6045 ;; HHH: Not Changed. This might handle the case where USER is not
6046 ;; given in the "File name" very poorly. Then, the local
6047 ;; variable tramp-current-user will be set to nil.
6048 (defun tramp-pre-connection (multi-method method user host chunksize
)
6049 "Do some setup before actually logging in.
6050 METHOD, USER and HOST specify the connection."
6051 (set-buffer (tramp-get-buffer multi-method method user host
))
6052 (set (make-local-variable 'tramp-current-multi-method
) multi-method
)
6053 (set (make-local-variable 'tramp-current-method
) method
)
6054 (set (make-local-variable 'tramp-current-user
) user
)
6055 (set (make-local-variable 'tramp-current-host
) host
)
6056 (set (make-local-variable 'tramp-chunksize
) chunksize
)
6057 (set (make-local-variable 'inhibit-eol-conversion
) nil
)
6060 (defun tramp-open-connection-setup-interactive-shell
6061 (p multi-method method user host
)
6062 "Set up an interactive shell.
6063 Mainly sets the prompt and the echo correctly. P is the shell process
6064 to set up. METHOD, USER and HOST specify the connection."
6065 ;; Wait a bit in case the remote end feels like sending a little
6066 ;; junk first. It seems that fencepost.gnu.org does this when doing
6067 ;; a Kerberos login.
6069 (tramp-discard-garbage-erase-buffer p multi-method method user host
)
6070 (tramp-process-initial-commands p multi-method method user host
6071 tramp-initial-commands
)
6072 ;; It is useful to set the prompt in the following command because
6073 ;; some people have a setting for $PS1 which /bin/sh doesn't know
6074 ;; about and thus /bin/sh will display a strange prompt. For
6075 ;; example, if $PS1 has "${CWD}" in the value, then ksh will display
6076 ;; the current working directory but /bin/sh will display a dollar
6077 ;; sign. The following command line sets $PS1 to a sane value, and
6078 ;; works under Bourne-ish shells as well as csh-like shells. Daniel
6079 ;; Pittman reports that the unusual positioning of the single quotes
6080 ;; makes it work under `rc', too. We also unset the variable $ENV
6081 ;; because that is read by some sh implementations (eg, bash when
6082 ;; called as sh) on startup; this way, we avoid the startup file
6084 (tramp-send-command-internal
6085 multi-method method user host
6086 (format "exec env 'ENV=' 'PS1=$ ' %s"
6087 (tramp-get-method-parameter
6088 multi-method method user host
'tramp-remote-sh
))
6089 (format "remote `%s' to come up"
6090 (tramp-get-method-parameter
6091 multi-method method user host
'tramp-remote-sh
)))
6092 (tramp-barf-if-no-shell-prompt
6094 "Remote `%s' didn't come up. See buffer `%s' for details"
6095 (tramp-get-method-parameter multi-method method user host
'tramp-remote-sh
)
6097 (tramp-message 8 "Setting up remote shell environment")
6098 (tramp-discard-garbage-erase-buffer p multi-method method user host
)
6099 (tramp-send-command-internal multi-method method user host
6100 "stty -inlcr -echo kill '^U'")
6102 ;; Ignore garbage after stty command.
6103 (tramp-send-command-internal multi-method method user host
6106 (tramp-send-command-internal multi-method method user host
6107 "TERM=dumb; export TERM")
6109 ;; Check whether the remote host suffers from buggy `send-process-string'.
6110 ;; This is known for FreeBSD (see comment in `send_process', file process.c).
6111 ;; I've tested sending 624 bytes successfully, sending 625 bytes failed.
6112 ;; Emacs makes a hack when this host type is detected locally. It cannot
6113 ;; handle remote hosts, though.
6114 (when (or (not tramp-chunksize
) (zerop tramp-chunksize
))
6115 (tramp-message 9 "Checking remote host type for `send-process-string' bug")
6116 (tramp-send-command-internal multi-method method user host
6117 "(uname -sr) 2>/dev/null")
6118 (goto-char (point-min))
6119 (when (looking-at "FreeBSD")
6120 (setq tramp-chunksize
500)))
6122 ;; Try to set up the coding system correctly.
6123 ;; CCC this can't be the right way to do it. Hm.
6126 (tramp-message 9 "Determining coding system")
6127 (tramp-send-command-internal multi-method method user host
6128 "echo foo ; echo bar")
6129 (goto-char (point-min))
6130 (if (featurep 'mule
)
6131 ;; Use MULE to select the right EOL convention for communicating
6132 ;; with the process.
6133 (let* ((cs (or (process-coding-system p
) (cons 'undecided
'undecided
)))
6134 cs-decode cs-encode
)
6135 (when (symbolp cs
) (setq cs
(cons cs cs
)))
6136 (setq cs-decode
(car cs
))
6137 (setq cs-encode
(cdr cs
))
6138 (unless cs-decode
(setq cs-decode
'undecided
))
6139 (unless cs-encode
(setq cs-encode
'undecided
))
6140 (setq cs-encode
(tramp-coding-system-change-eol-conversion
6142 (when (search-forward "\r" nil t
)
6143 (setq cs-decode
(tramp-coding-system-change-eol-conversion
6145 (set-buffer-process-coding-system cs-decode cs-encode
))
6146 ;; Look for ^M and do something useful if found.
6147 (when (search-forward "\r" nil t
)
6148 ;; We have found a ^M but cannot frob the process coding system
6149 ;; because we're running on a non-MULE Emacs. Let's try
6152 (tramp-message 9 "Trying `stty -onlcr'")
6153 (tramp-send-command-internal multi-method method user host
6157 9 "Waiting 30s for `HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE'")
6158 (tramp-send-command-internal
6159 multi-method method user host
6160 "HISTFILE=$HOME/.tramp_history; HISTSIZE=1; export HISTFILE; export HISTSIZE")
6162 (tramp-message 9 "Waiting 30s for `set +o vi +o emacs'")
6163 (tramp-send-command-internal multi-method method user host
6164 "set +o vi +o emacs")
6166 (tramp-message 9 "Waiting 30s for `unset MAIL MAILCHECK MAILPATH'")
6167 (tramp-send-command-internal
6168 multi-method method user host
6169 "unset MAIL MAILCHECK MAILPATH 1>/dev/null 2>/dev/null")
6171 (tramp-message 9 "Waiting 30s for `unset CDPATH'")
6172 (tramp-send-command-internal multi-method method user host
6175 (tramp-message 9 "Setting shell prompt")
6176 ;; Douglas Gray Stephens <DGrayStephens@slb.com> says that we must
6177 ;; use "\n" here, not tramp-rsh-end-of-line. We also manually frob
6178 ;; the last time we sent a command, to avoid tramp-send-command to send
6179 ;; "echo are you awake".
6180 (setq tramp-last-cmd-time
(current-time))
6182 multi-method method user host
6183 (format "PS1='%s%s%s'; PS2=''; PS3=''"
6184 tramp-rsh-end-of-line
6186 tramp-rsh-end-of-line
))
6187 (tramp-wait-for-output))
6189 (defun tramp-post-connection (multi-method method user host
)
6190 "Prepare a remote shell before being able to work on it.
6191 METHOD, USER and HOST specify the connection.
6192 Among other things, this finds a shell which groks tilde expansion,
6193 tries to find an `ls' command which groks the `-n' option, sets the
6194 locale to C and sets up the remote shell search path."
6195 ;; Search for a good shell before searching for a command which
6196 ;; checks if a file exists. This is done because Tramp wants to use
6197 ;; "test foo; echo $?" to check if various conditions hold, and
6198 ;; there are buggy /bin/sh implementations which don't execute the
6199 ;; "echo $?" part if the "test" part has an error. In particular,
6200 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
6201 ;; with buggy /bin/sh implementations will have a working bash or
6203 (tramp-find-shell multi-method method user host
)
6204 ;; Without (sit-for 0.1) at least, my machine will almost always blow
6205 ;; up on 'not numberp /root' - a race that causes the 'echo ~root'
6206 ;; output of (tramp-find-shell) to show up along with the output of
6207 ;; (tramp-find-ls-command) testing.
6209 ;; I can't work out why this is a problem though. The (tramp-wait-for-output)
6210 ;; call in (tramp-find-shell) *should* make this not happen, I thought.
6212 ;; After much debugging I couldn't find any problem with the implementation
6213 ;; of that function though. The workaround stays for me at least. :/
6215 ;; Daniel Pittman <daniel@danann.net>
6218 (tramp-find-file-exists-command multi-method method user host
)
6219 (make-local-variable 'tramp-ls-command
)
6220 (setq tramp-ls-command
(tramp-find-ls-command multi-method method user host
))
6221 (unless tramp-ls-command
6224 "Danger! Couldn't find ls which groks -n. Muddling through anyway")
6225 (setq tramp-ls-command
6226 (tramp-find-executable multi-method method user host
6227 "ls" tramp-remote-path nil
)))
6228 (unless tramp-ls-command
6229 (error "Fatal error: Couldn't find remote executable `ls'"))
6230 (tramp-message 5 "Using remote command `%s' for getting directory listings"
6232 (tramp-send-command multi-method method user host
6233 (concat "tramp_set_exit_status () {" tramp-rsh-end-of-line
6234 "return $1" tramp-rsh-end-of-line
6236 (tramp-wait-for-output)
6237 ;; Set remote PATH variable.
6238 (tramp-set-remote-path multi-method method user host
"PATH" tramp-remote-path
)
6239 ;; Tell remote shell to use standard time format, needed for
6240 ;; parsing `ls -l' output.
6241 (tramp-send-command multi-method method user host
6242 "LC_TIME=C; export LC_TIME; echo huhu")
6243 (tramp-wait-for-output)
6244 (tramp-send-command multi-method method user host
6245 "mesg n; echo huhu")
6246 (tramp-wait-for-output)
6247 (tramp-send-command multi-method method user host
6248 "biff n ; echo huhu")
6249 (tramp-wait-for-output)
6250 ;; Unalias ls(1) to work around issues with those silly people who make it
6251 ;; spit out ANSI escapes or whatever.
6252 (tramp-send-command multi-method method user host
6253 "unalias ls; echo huhu")
6254 (tramp-wait-for-output)
6255 ;; Does `test A -nt B' work? Use abominable `find' construct if it
6256 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
6257 ;; for otherwise the shell crashes.
6259 (make-local-variable 'tramp-test-groks-nt
)
6260 (tramp-send-command multi-method method user host
6262 (tramp-wait-for-output)
6263 (goto-char (point-min))
6264 (setq tramp-test-groks-nt
6265 (looking-at (format "\n%s\r?\n" (regexp-quote tramp-end-of-output
))))
6266 (unless tramp-test-groks-nt
6268 multi-method method user host
6269 (concat "tramp_test_nt () {" tramp-rsh-end-of-line
6270 "test -n \"`find $1 -prune -newer $2 -print`\"" tramp-rsh-end-of-line
6272 (tramp-wait-for-output)
6273 ;; Send the fallback `uudecode' script.
6275 (tramp-send-string multi-method method user host tramp-uudecode
)
6276 (tramp-wait-for-output)
6279 (tramp-set-connection-property "perl-scripts" nil multi-method method user host
)
6280 (let ((tramp-remote-perl
6281 (or (tramp-find-executable multi-method method user host
6282 "perl5" tramp-remote-path nil
)
6283 (tramp-find-executable multi-method method user host
6284 "perl" tramp-remote-path nil
))))
6285 (when tramp-remote-perl
6286 (tramp-set-connection-property "perl" tramp-remote-perl
6287 multi-method method user host
)
6288 (unless (tramp-method-out-of-band-p multi-method method user host
)
6289 (tramp-message 5 "Sending the Perl `mime-encode' implementations.")
6291 multi-method method user host
6292 (concat "tramp_encode () {\n"
6293 (format tramp-perl-encode tramp-remote-perl
)
6296 (tramp-wait-for-output)
6298 multi-method method user host
6299 (concat "tramp_encode_with_module () {\n"
6300 (format tramp-perl-encode-with-module tramp-remote-perl
)
6303 (tramp-wait-for-output)
6304 (tramp-message 5 "Sending the Perl `mime-decode' implementations.")
6306 multi-method method user host
6307 (concat "tramp_decode () {\n"
6308 (format tramp-perl-decode tramp-remote-perl
)
6311 (tramp-wait-for-output)
6313 multi-method method user host
6314 (concat "tramp_decode_with_module () {\n"
6315 (format tramp-perl-decode-with-module tramp-remote-perl
)
6318 (tramp-wait-for-output))))
6321 (let ((ln (tramp-find-executable multi-method method user host
6322 "ln" tramp-remote-path nil
)))
6324 (tramp-set-connection-property "ln" ln multi-method method user host
)))
6327 (tramp-send-command multi-method method user host
"id -u; id -g")
6328 (tramp-wait-for-output)
6329 (goto-char (point-min))
6330 (tramp-set-connection-property
6331 "uid" (read (current-buffer)) multi-method method user host
)
6332 (tramp-set-connection-property
6333 "gid" (read (current-buffer)) multi-method method user host
)
6334 ;; Find the right encoding/decoding commands to use.
6336 (unless (tramp-method-out-of-band-p multi-method method user host
)
6337 (tramp-find-inline-encoding multi-method method user host
))
6338 ;; If encoding/decoding command are given, test to see if they work.
6339 ;; CCC: Maybe it would be useful to run the encoder both locally and
6340 ;; remotely to see if they produce the same result.
6341 (let ((rem-enc (tramp-get-remote-encoding multi-method method user host
))
6342 (rem-dec (tramp-get-remote-decoding multi-method method user host
))
6343 (magic-string "xyzzy"))
6344 (when (and (or rem-dec rem-enc
) (not (and rem-dec rem-enc
)))
6345 (tramp-kill-process multi-method method user host
)
6346 ;; Improve error message and/or error check.
6348 "Must give both decoding and encoding command in method definition"))
6349 (when (and rem-enc rem-dec
)
6352 "Checking to see if encoding/decoding commands work on remote host...")
6354 multi-method method user host
6355 (format "echo %s | %s | %s"
6356 (tramp-shell-quote-argument magic-string
) rem-enc rem-dec
))
6357 (tramp-wait-for-output)
6358 (unless (looking-at (regexp-quote magic-string
))
6359 (tramp-kill-process multi-method method user host
)
6360 (error "Remote host cannot execute de/encoding commands. See buffer `%s' for details"
6364 5 "Checking to see if encoding/decoding commands work on remote host...done"))))
6366 ;; CCC: We should either implement a Perl version of base64 encoding
6367 ;; and decoding. Then we just use that in the last item. The other
6368 ;; alternative is to use the Perl version of UU encoding. But then
6369 ;; we need a Lisp version of uuencode.
6371 ;; Old text from documentation of tramp-methods:
6372 ;; Using a uuencode/uudecode inline method is discouraged, please use one
6373 ;; of the base64 methods instead since base64 encoding is much more
6374 ;; reliable and the commands are more standardized between the different
6375 ;; Unix versions. But if you can't use base64 for some reason, please
6376 ;; note that the default uudecode command does not work well for some
6377 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
6378 ;; the following command for uudecode:
6380 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
6382 ;; For Irix, no solution is known yet.
6384 (defvar tramp-coding-commands
6385 '(("mimencode -b" "mimencode -u -b"
6386 base64-encode-region base64-decode-region
)
6387 ("mmencode -b" "mmencode -u -b"
6388 base64-encode-region base64-decode-region
)
6389 ("recode data..base64" "recode base64..data"
6390 base64-encode-region base64-decode-region
)
6391 ("uuencode xxx" "uudecode -o /dev/stdout"
6392 tramp-uuencode-region uudecode-decode-region
)
6393 ("uuencode xxx" "uudecode -o -"
6394 tramp-uuencode-region uudecode-decode-region
)
6395 ("uuencode xxx" "uudecode -p"
6396 tramp-uuencode-region uudecode-decode-region
)
6397 ("uuencode xxx" "tramp_uudecode"
6398 tramp-uuencode-region uudecode-decode-region
)
6399 ("tramp_encode_with_module" "tramp_decode_with_module"
6400 base64-encode-region base64-decode-region
)
6401 ("tramp_encode" "tramp_decode"
6402 base64-encode-region base64-decode-region
))
6403 "List of coding commands for inline transfer.
6404 Each item is a list that looks like this:
6406 \(REMOTE-ENCODING REMOTE-DECODING LOCAL-ENCODING LOCAL-DECODING)
6408 The REMOTE-ENCODING should be a string, giving a command accepting a
6409 plain file on standard input and writing the encoded file to standard
6410 output. The REMOTE-DECODING should also be a string, giving a command
6411 accepting an encoded file on standard input and writing the decoded
6412 file to standard output.
6414 LOCAL-ENCODING and LOCAL-DECODING can be strings, giving commands, or
6415 symbols, giving functions. If they are strings, then they can contain
6416 the \"%s\" format specifier. If that specifier is present, the input
6417 filename will be put into the command line at that spot. If the
6418 specifier is not present, the input should be read from standard
6421 If they are functions, they will be called with two arguments, start
6422 and end of region, and are expected to replace the region contents
6423 with the encoded or decoded results, respectively.")
6425 (defun tramp-find-inline-encoding (multi-method method user host
)
6426 "Find an inline transfer encoding that works.
6427 Goes through the list `tramp-coding-commands'."
6428 (let ((commands tramp-coding-commands
)
6431 (while (and commands
(null found
))
6432 (setq item
(pop commands
))
6434 (let ((rem-enc (nth 0 item
))
6435 (rem-dec (nth 1 item
))
6436 (loc-enc (nth 2 item
))
6437 (loc-dec (nth 3 item
)))
6438 ;; Check if remote encoding and decoding commands can be
6439 ;; called remotely with null input and output. This makes
6440 ;; sure there are no syntax errors and the command is really
6441 ;; found. Note that we do not redirect stdout to /dev/null,
6442 ;; for two reaons: when checking the decoding command, we
6443 ;; actually check the output it gives. And also, when
6444 ;; redirecting "mimencode" output to /dev/null, then as root
6445 ;; it might change the permissions of /dev/null!
6446 (tramp-message-for-buffer
6447 multi-method method user host
9
6448 "Checking remote encoding command `%s' for sanity" rem-enc
)
6449 (unless (zerop (tramp-send-command-and-check
6450 multi-method method user host
6451 (format "%s </dev/null" rem-enc
) t
))
6452 (throw 'wont-work nil
))
6453 (tramp-message-for-buffer
6454 multi-method method user host
9
6455 "Checking remote decoding command `%s' for sanity" rem-dec
)
6456 (unless (zerop (tramp-send-command-and-check
6457 multi-method method user host
6458 (format "echo %s | %s | %s"
6459 magic rem-enc rem-dec
) t
))
6460 (throw 'wont-work nil
))
6462 (goto-char (point-min))
6463 (unless (looking-at (regexp-quote magic
))
6464 (throw 'wont-work nil
)))
6465 ;; If the local encoder or decoder is a string, the
6466 ;; corresponding command has to work locally.
6467 (when (stringp loc-enc
)
6468 (tramp-message-for-buffer
6469 multi-method method user host
9
6470 "Checking local encoding command `%s' for sanity" loc-enc
)
6471 (unless (zerop (tramp-call-local-coding-command
6473 (throw 'wont-work nil
)))
6474 (when (stringp loc-dec
)
6475 (tramp-message-for-buffer
6476 multi-method method user host
9
6477 "Checking local decoding command `%s' for sanity" loc-dec
)
6478 (unless (zerop (tramp-call-local-coding-command
6480 (throw 'wont-work nil
)))
6481 ;; CCC: At this point, maybe we should check that the output
6482 ;; of the commands is correct. But for the moment we will
6483 ;; assume that commands working on empty input will also
6484 ;; work in practice.
6485 (setq found item
))))
6486 ;; Did we find something? If not, issue error. If so,
6487 ;; set connection properties.
6489 (error "Couldn't find an inline transfer encoding"))
6490 (let ((rem-enc (nth 0 found
))
6491 (rem-dec (nth 1 found
))
6492 (loc-enc (nth 2 found
))
6493 (loc-dec (nth 3 found
)))
6494 (tramp-message 10 "Using remote encoding %s" rem-enc
)
6495 (tramp-set-remote-encoding multi-method method user host rem-enc
)
6496 (tramp-message 10 "Using remote decoding %s" rem-dec
)
6497 (tramp-set-remote-decoding multi-method method user host rem-dec
)
6498 (tramp-message 10 "Using local encoding %s" loc-enc
)
6499 (tramp-set-local-encoding multi-method method user host loc-enc
)
6500 (tramp-message 10 "Using local decoding %s" loc-dec
)
6501 (tramp-set-local-decoding multi-method method user host loc-dec
))))
6503 (defun tramp-call-local-coding-command (cmd input output
)
6504 "Call the local encoding or decoding command.
6505 If CMD contains \"%s\", provide input file INPUT there in command.
6506 Otherwise, INPUT is passed via standard input.
6507 INPUT can also be nil which means `/dev/null'.
6508 OUTPUT can be a string (which specifies a filename), or t (which
6509 means standard output and thus the current buffer), or nil (which
6512 tramp-encoding-shell
;program
6513 (when (and input
(not (string-match "%s" cmd
)))
6515 (if (eq output t
) t nil
) ;output
6517 tramp-encoding-command-switch
6518 ;; actual shell command
6520 (if (string-match "%s" cmd
) (format cmd input
) cmd
)
6521 (if (stringp output
) (concat "> " output
) ""))))
6523 (defun tramp-maybe-open-connection (multi-method method user host
)
6524 "Maybe open a connection to HOST, logging in as USER, using METHOD.
6525 Does not do anything if a connection is already open, but re-opens the
6526 connection if a previous connection has died for some reason."
6527 (let ((p (get-buffer-process
6528 (tramp-get-buffer multi-method method user host
)))
6530 ;; If too much time has passed since last command was sent, look
6531 ;; whether process is still alive. If it isn't, kill it. When
6532 ;; using ssh, it can sometimes happen that the remote end has hung
6533 ;; up but the local ssh client doesn't recognize this until it
6534 ;; tries to send some data to the remote end. So that's why we
6535 ;; try to send a command from time to time, then look again
6536 ;; whether the process is really alive.
6538 (set-buffer (tramp-get-buffer multi-method method user host
))
6539 (when (and tramp-last-cmd-time
6540 (> (tramp-time-diff (current-time) tramp-last-cmd-time
) 60)
6541 p
(processp p
) (memq (process-status p
) '(run open
)))
6543 multi-method method user host
"echo are you awake" nil t
)
6544 (unless (and (memq (process-status p
) '(run open
))
6545 (tramp-wait-for-output 10))
6549 (unless (and p
(processp p
) (memq (process-status p
) '(run open
)))
6550 (when (and p
(processp p
))
6552 (let ((process-connection-type tramp-process-connection-type
))
6553 (funcall (tramp-get-method-parameter
6555 (tramp-find-method multi-method method user host
)
6556 user host
'tramp-connection-function
)
6557 multi-method method user host
)))))
6559 (defun tramp-send-command
6560 (multi-method method user host command
&optional noerase neveropen
)
6561 "Send the COMMAND to USER at HOST (logged in using METHOD).
6562 Erases temporary buffer before sending the command (unless NOERASE
6564 If optional seventh arg NEVEROPEN is non-nil, never try to open the
6565 connection. This is meant to be used from
6566 `tramp-maybe-open-connection' only."
6568 (tramp-maybe-open-connection multi-method method user host
))
6569 (setq tramp-last-cmd-time
(current-time))
6570 (setq tramp-last-cmd command
)
6571 (when tramp-debug-buffer
6573 (set-buffer (tramp-get-debug-buffer multi-method method user host
))
6574 (goto-char (point-max))
6575 (tramp-insert-with-face 'bold
(format "$ %s\n" command
))))
6577 (set-buffer (tramp-get-buffer multi-method method user host
))
6578 (unless noerase
(erase-buffer))
6579 (setq proc
(get-buffer-process (current-buffer)))
6580 (process-send-string proc
6581 (concat command tramp-rsh-end-of-line
))))
6583 (defun tramp-send-command-internal
6584 (multi-method method user host command
&optional msg
)
6585 "Send command to remote host and wait for success.
6586 Sends COMMAND, then waits 30 seconds for shell prompt."
6587 (tramp-send-command multi-method method user host command t t
)
6589 (tramp-message 9 "Waiting 30s for %s..." msg
))
6590 (tramp-barf-if-no-shell-prompt
6592 "Couldn't `%s', see buffer `%s'" command
(buffer-name)))
6594 (defun tramp-wait-for-output (&optional timeout
)
6595 "Wait for output from remote rsh command."
6596 (let ((proc (get-buffer-process (current-buffer)))
6598 (start-time (current-time))
6599 (start-point (point))
6600 (end-of-output (concat "^"
6601 (regexp-quote tramp-end-of-output
)
6603 ;; Algorithm: get waiting output. See if last line contains
6604 ;; end-of-output sentinel. If not, wait a bit and again get
6605 ;; waiting output. Repeat until timeout expires or end-of-output
6606 ;; sentinel is seen. Will hang if timeout is nil and
6607 ;; end-of-output sentinel never appears.
6610 ;; Work around an XEmacs bug, where the timeout expires
6611 ;; faster than it should. This degenerates into polling
6612 ;; for buggy XEmacsen, but oh, well.
6613 (while (and (not found
)
6614 (< (tramp-time-diff (current-time) start-time
)
6616 (with-timeout (timeout)
6618 (tramp-accept-process-output proc
1)
6619 (unless (memq (process-status proc
) '(run open
))
6620 (error "Process has died"))
6621 (goto-char (point-max))
6623 (setq found
(looking-at end-of-output
))))))
6626 (tramp-accept-process-output proc
1)
6627 (unless (memq (process-status proc
) '(run open
))
6628 (error "Process has died"))
6629 (goto-char (point-max))
6631 (setq found
(looking-at end-of-output
))))))
6632 ;; At this point, either the timeout has expired or we have found
6633 ;; the end-of-output sentinel.
6635 (goto-char (point-max))
6637 (delete-region (point) (point-max)))
6638 ;; If processing echoes, look for it in the first line and delete.
6639 (when tramp-process-echoes
6641 (goto-char start-point
)
6642 (when (looking-at (regexp-quote tramp-last-cmd
))
6643 (delete-region (point) (progn (forward-line 1) (point))))))
6644 ;; Add output to debug buffer if appropriate.
6645 (when tramp-debug-buffer
6647 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6648 tramp-current-user tramp-current-host
)
6649 (point-min) (point-max))
6653 (tramp-get-debug-buffer tramp-current-multi-method tramp-current-method
6654 tramp-current-user tramp-current-host
))
6655 (goto-char (point-max))
6656 (insert "[[Remote prompt `" end-of-output
"' not found"
6657 (if timeout
(format " in %d secs" timeout
) "")
6659 (goto-char (point-min))
6660 ;; Return value is whether end-of-output sentinel was found.
6663 (defun tramp-send-command-and-check (multi-method method user host command
6665 "Run COMMAND and check its exit status.
6666 MULTI-METHOD and METHOD specify how to log in (as USER) to the remote HOST.
6667 Sends `echo $?' along with the COMMAND for checking the exit status. If
6668 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
6670 If the optional argument SUBSHELL is non-nil, the command is executed in
6671 a subshell, ie surrounded by parentheses."
6672 (tramp-send-command multi-method method user host
6673 (concat (if subshell
"( " "")
6675 (if command
" 2>/dev/null; " "")
6676 "echo tramp_exit_status $?"
6677 (if subshell
" )" " ")))
6678 (tramp-wait-for-output)
6679 (goto-char (point-max))
6680 (unless (search-backward "tramp_exit_status " nil t
)
6681 (error "Couldn't find exit status of `%s'" command
))
6682 (skip-chars-forward "^ ")
6683 (read (current-buffer)))
6685 (defun tramp-barf-unless-okay (multi-method method user host command subshell
6686 signal fmt
&rest args
)
6687 "Run COMMAND, check exit status, throw error if exit status not okay.
6688 Similar to `tramp-send-command-and-check' but accepts two more arguments
6689 FMT and ARGS which are passed to `error'."
6690 (unless (zerop (tramp-send-command-and-check
6691 multi-method method user host command subshell
))
6692 ;; CCC: really pop-to-buffer? Maybe it's appropriate to be more
6694 (pop-to-buffer (current-buffer))
6695 (funcall 'signal signal
(apply 'format fmt args
))))
6697 ;; It seems that Tru64 Unix does not like it if long strings are sent
6698 ;; to it in one go. (This happens when sending the Perl
6699 ;; `file-attributes' implementation, for instance.) Therefore, we
6700 ;; have this function which waits a bit at each line.
6701 (defun tramp-send-string
6702 (multi-method method user host string
)
6703 "Send the STRING to USER at HOST using METHOD.
6705 The STRING is expected to use Unix line-endings, but the lines sent to
6706 the remote host use line-endings as defined in the variable
6707 `tramp-rsh-end-of-line'."
6708 (let ((proc (get-buffer-process
6709 (tramp-get-buffer multi-method method user host
))))
6711 (error "Can't send string to remote host -- not logged in"))
6713 (when tramp-debug-buffer
6715 (set-buffer (tramp-get-debug-buffer multi-method method user host
))
6716 (goto-char (point-max))
6717 (tramp-insert-with-face 'bold
(format "$ %s\n" string
))))
6718 ;; replace "\n" by `tramp-rsh-end-of-line'
6720 (mapconcat 'identity
6721 (split-string string
"\n")
6722 tramp-rsh-end-of-line
))
6723 (unless (or (string= string
"")
6724 (string-equal (substring string -
1) tramp-rsh-end-of-line
))
6725 (setq string
(concat string tramp-rsh-end-of-line
)))
6727 (if (and tramp-chunksize
(not (zerop tramp-chunksize
)))
6729 (end (length string
)))
6731 (tramp-message-for-buffer
6732 multi-method method user host
10
6733 "Sending chunk from %s to %s"
6734 pos
(min (+ pos tramp-chunksize
) end
))
6735 (process-send-string
6736 proc
(substring string pos
(min (+ pos tramp-chunksize
) end
)))
6737 (setq pos
(+ pos tramp-chunksize
))
6739 (process-send-string proc string
))))
6741 (defun tramp-send-eof (multi-method method user host
)
6742 "Send EOF to the remote end.
6743 METHOD, HOST and USER specify the connection."
6744 (let ((proc (get-buffer-process
6745 (tramp-get-buffer multi-method method user host
))))
6747 (error "Can't send EOF to remote host -- not logged in"))
6748 (process-send-eof proc
)))
6749 ; (process-send-string proc "\^D")))
6751 (defun tramp-kill-process (multi-method method user host
)
6752 "Kill the connection process used by Tramp.
6753 MULTI-METHOD, METHOD, USER, and HOST specify the connection."
6754 (let ((proc (get-buffer-process
6755 (tramp-get-buffer multi-method method user host
))))
6756 (kill-process proc
)))
6758 (defun tramp-discard-garbage-erase-buffer (p multi-method method user host
)
6759 "Erase buffer, then discard subsequent garbage.
6760 If `tramp-discard-garbage' is nil, just erase buffer."
6761 (if (not tramp-discard-garbage
)
6763 (while (prog1 (erase-buffer) (tramp-accept-process-output p
0.25))
6764 (when tramp-debug-buffer
6766 (set-buffer (tramp-get-debug-buffer multi-method method user host
))
6767 (goto-char (point-max))
6768 (tramp-insert-with-face
6769 'bold
(format "Additional characters detected\n")))))))
6771 (defun tramp-mode-string-to-int (mode-string)
6772 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
6773 (let* ((mode-chars (string-to-vector mode-string
))
6774 (owner-read (aref mode-chars
1))
6775 (owner-write (aref mode-chars
2))
6776 (owner-execute-or-setid (aref mode-chars
3))
6777 (group-read (aref mode-chars
4))
6778 (group-write (aref mode-chars
5))
6779 (group-execute-or-setid (aref mode-chars
6))
6780 (other-read (aref mode-chars
7))
6781 (other-write (aref mode-chars
8))
6782 (other-execute-or-sticky (aref mode-chars
9)))
6786 (?r
(tramp-octal-to-decimal "00400")) (?-
0)
6787 (t (error "Second char `%c' must be one of `r-'" owner-read
)))
6789 (?w
(tramp-octal-to-decimal "00200")) (?-
0)
6790 (t (error "Third char `%c' must be one of `w-'" owner-write
)))
6791 (case owner-execute-or-setid
6792 (?x
(tramp-octal-to-decimal "00100"))
6793 (?S
(tramp-octal-to-decimal "04000"))
6794 (?s
(tramp-octal-to-decimal "04100"))
6796 (t (error "Fourth char `%c' must be one of `xsS-'"
6797 owner-execute-or-setid
)))
6799 (?r
(tramp-octal-to-decimal "00040")) (?-
0)
6800 (t (error "Fifth char `%c' must be one of `r-'" group-read
)))
6802 (?w
(tramp-octal-to-decimal "00020")) (?-
0)
6803 (t (error "Sixth char `%c' must be one of `w-'" group-write
)))
6804 (case group-execute-or-setid
6805 (?x
(tramp-octal-to-decimal "00010"))
6806 (?S
(tramp-octal-to-decimal "02000"))
6807 (?s
(tramp-octal-to-decimal "02010"))
6809 (t (error "Seventh char `%c' must be one of `xsS-'"
6810 group-execute-or-setid
)))
6812 (?r
(tramp-octal-to-decimal "00004")) (?-
0)
6813 (t (error "Eighth char `%c' must be one of `r-'" other-read
)))
6815 (?w
(tramp-octal-to-decimal "00002")) (?-
0)
6816 (t (error "Nineth char `%c' must be one of `w-'" other-write
)))
6817 (case other-execute-or-sticky
6818 (?x
(tramp-octal-to-decimal "00001"))
6819 (?T
(tramp-octal-to-decimal "01000"))
6820 (?t
(tramp-octal-to-decimal "01001"))
6822 (t (error "Tenth char `%c' must be one of `xtT-'"
6823 other-execute-or-sticky
)))))))
6825 (defun tramp-convert-file-attributes (multi-method method user host attr
)
6826 "Convert file-attributes ATTR generated by perl script or ls.
6827 Convert file mode bits to string and set virtual device number.
6829 ;; Convert file mode bits to string.
6830 (unless (stringp (nth 8 attr
))
6831 (setcar (nthcdr 8 attr
) (tramp-file-mode-from-int (nth 8 attr
))))
6832 ;; Set file's gid change bit. Possible only when id-format is 'integer.
6833 (when (numberp (nth 3 attr
))
6834 (setcar (nthcdr 9 attr
)
6835 (not (eql (nth 3 attr
)
6836 (tramp-get-remote-gid multi-method method user host
)))))
6837 ;; Set virtual device number.
6838 (setcar (nthcdr 11 attr
)
6839 (tramp-get-device multi-method method user host
))
6842 (defun tramp-get-device (multi-method method user host
)
6843 "Returns the virtual device number.
6844 If it doesn't exist, generate a new one."
6845 (let ((string (tramp-make-tramp-file-name multi-method method user host
"")))
6846 (unless (assoc string tramp-devices
)
6847 (add-to-list 'tramp-devices
6848 (list string
(length tramp-devices
))))
6849 (list -
1 (nth 1 (assoc string tramp-devices
)))))
6851 (defun tramp-file-mode-from-int (mode)
6852 "Turn an integer representing a file mode into an ls(1)-like string."
6853 (let ((type (cdr (assoc (logand (lsh mode -
12) 15) tramp-file-mode-type-map
)))
6854 (user (logand (lsh mode -
6) 7))
6855 (group (logand (lsh mode -
3) 7))
6856 (other (logand (lsh mode -
0) 7))
6857 (suid (> (logand (lsh mode -
9) 4) 0))
6858 (sgid (> (logand (lsh mode -
9) 2) 0))
6859 (sticky (> (logand (lsh mode -
9) 1) 0)))
6860 (setq user
(tramp-file-mode-permissions user suid
"s"))
6861 (setq group
(tramp-file-mode-permissions group sgid
"s"))
6862 (setq other
(tramp-file-mode-permissions other sticky
"t"))
6863 (concat type user group other
)))
6866 (defun tramp-file-mode-permissions (perm suid suid-text
)
6867 "Convert a permission bitset into a string.
6868 This is used internally by `tramp-file-mode-from-int'."
6869 (let ((r (> (logand perm
4) 0))
6870 (w (> (logand perm
2) 0))
6871 (x (> (logand perm
1) 0)))
6872 (concat (or (and r
"r") "-")
6873 (or (and w
"w") "-")
6874 (or (and suid x suid-text
) ; suid, execute
6875 (and suid
(upcase suid-text
)) ; suid, !execute
6876 (and x
"x") "-")))) ; !suid
6879 (defun tramp-decimal-to-octal (i)
6880 "Return a string consisting of the octal digits of I.
6881 Not actually used. Use `(format \"%o\" i)' instead?"
6882 (cond ((< i
0) (error "Cannot convert negative number to octal"))
6883 ((not (integerp i
)) (error "Cannot convert non-integer to octal"))
6885 (t (concat (tramp-decimal-to-octal (/ i
8))
6886 (number-to-string (% i
8))))))
6889 ;;(defun tramp-octal-to-decimal (ostr)
6890 ;; "Given a string of octal digits, return a decimal number."
6891 ;; (cond ((null ostr) 0)
6892 ;; ((string= "" ostr) 0)
6893 ;; (t (let ((last (aref ostr (1- (length ostr))))
6894 ;; (rest (substring ostr 0 (1- (length ostr)))))
6895 ;; (unless (and (>= last ?0)
6897 ;; (error "Not an octal digit: %c" last))
6898 ;; (+ (- last ?0) (* 8 (tramp-octal-to-decimal rest)))))))
6899 ;; Kudos to Gerd Moellmann for this suggestion.
6900 (defun tramp-octal-to-decimal (ostr)
6901 "Given a string of octal digits, return a decimal number."
6902 (let ((x (or ostr
"")))
6903 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
6904 (unless (string-match "\\`[0-7]*\\'" x
)
6905 (error "Non-octal junk in string `%s'" x
))
6906 (string-to-number ostr
8)))
6908 (defun tramp-shell-case-fold (string)
6909 "Converts STRING to shell glob pattern which ignores case."
6912 (if (equal (downcase c
) (upcase c
))
6914 (format "[%c%c]" (downcase c
) (upcase c
))))
6919 ;; ------------------------------------------------------------
6920 ;; -- TRAMP file names --
6921 ;; ------------------------------------------------------------
6922 ;; Conversion functions between external representation and
6923 ;; internal data structure. Convenience functions for internal
6926 (defstruct tramp-file-name multi-method method user host localname
)
6928 (defun tramp-tramp-file-p (name)
6929 "Return t iff NAME is a tramp file."
6931 (string-match tramp-file-name-regexp name
)))
6933 ;; HHH: Changed. Used to assign the return value of (user-login-name)
6934 ;; to the `user' part of the structure if a user name was not
6935 ;; provided, now it assigns nil.
6936 (defun tramp-dissect-file-name (name)
6937 "Return an `tramp-file-name' structure.
6938 The structure consists of remote method, remote user, remote host and
6939 localname (file name on remote host)."
6941 (let* ((match (string-match (nth 0 tramp-file-name-structure
) name
))
6944 (if match
(match-string (nth 1 tramp-file-name-structure
) name
)
6947 (format (nth 0 tramp-multi-file-name-structure
)
6948 (nth 0 tramp-multi-file-name-hop-structure
)) name
)
6949 (match-string (nth 1 tramp-multi-file-name-structure
) name
))))
6950 (if (and method
(member method tramp-multi-methods
))
6951 ;; If it's a multi method, the file name structure contains
6952 ;; arrays of method, user and host.
6953 (tramp-dissect-multi-file-name name
)
6954 ;; Normal method. First, find out default method.
6955 (unless match
(error "Not a tramp file name: %s" name
))
6956 (let ((user (match-string (nth 2 tramp-file-name-structure
) name
))
6957 (host (match-string (nth 3 tramp-file-name-structure
) name
))
6958 (localname (match-string (nth 4 tramp-file-name-structure
) name
)))
6959 (make-tramp-file-name
6964 :localname localname
))))))
6966 (defun tramp-find-default-method (user host
)
6967 "Look up the right method to use in `tramp-default-method-alist'."
6968 (let ((choices tramp-default-method-alist
)
6969 (method tramp-default-method
)
6972 (setq item
(pop choices
))
6973 (when (and (string-match (or (nth 0 item
) "") (or host
""))
6974 (string-match (or (nth 1 item
) "") (or user
"")))
6975 (setq method
(nth 2 item
))
6976 (setq choices nil
)))
6979 (defun tramp-find-method (multi-method method user host
)
6980 "Return the right method string to use.
6981 This is MULTI-METHOD, if non-nil. Otherwise, it is METHOD, if non-nil.
6982 If both MULTI-METHOD and METHOD are nil, do a lookup in
6983 `tramp-default-method-alist'."
6984 (or multi-method method
(tramp-find-default-method user host
)))
6986 ;; HHH: Not Changed. Multi method. Will probably not handle the case where
6987 ;; a user name is not provided in the "file name" very well.
6988 (defun tramp-dissect-multi-file-name (name)
6989 "Not implemented yet."
6990 (let ((regexp (nth 0 tramp-multi-file-name-structure
))
6991 (method-index (nth 1 tramp-multi-file-name-structure
))
6992 (hops-index (nth 2 tramp-multi-file-name-structure
))
6993 (localname-index (nth 3 tramp-multi-file-name-structure
))
6994 (hop-regexp (nth 0 tramp-multi-file-name-hop-structure
))
6995 (hop-method-index (nth 1 tramp-multi-file-name-hop-structure
))
6996 (hop-user-index (nth 2 tramp-multi-file-name-hop-structure
))
6997 (hop-host-index (nth 3 tramp-multi-file-name-hop-structure
))
6998 method hops len hop-methods hop-users hop-hosts localname
)
6999 (unless (string-match (format regexp hop-regexp
) name
)
7000 (error "Not a multi tramp file name: %s" name
))
7001 (setq method
(match-string method-index name
))
7002 (setq hops
(match-string hops-index name
))
7003 (setq len
(/ (length (match-data t
)) 2))
7004 (when (< localname-index
0) (incf localname-index len
))
7005 (setq localname
(match-string localname-index name
))
7007 (while (string-match hop-regexp hops index
)
7008 (setq index
(match-end 0))
7010 (cons (match-string hop-method-index hops
) hop-methods
))
7012 (cons (match-string hop-user-index hops
) hop-users
))
7014 (cons (match-string hop-host-index hops
) hop-hosts
))))
7015 (make-tramp-file-name
7016 :multi-method method
7017 :method
(apply 'vector
(reverse hop-methods
))
7018 :user
(apply 'vector
(reverse hop-users
))
7019 :host
(apply 'vector
(reverse hop-hosts
))
7020 :localname localname
)))
7022 (defun tramp-make-tramp-file-name (multi-method method user host localname
)
7023 "Constructs a tramp file name from METHOD, USER, HOST and LOCALNAME."
7025 (tramp-make-tramp-multi-file-name multi-method method user host localname
)
7027 (concat tramp-prefix-format
7028 (when method
(concat "%m" tramp-postfix-single-method-format
))
7029 (when user
(concat "%u" tramp-postfix-user-format
))
7030 (when host
(concat "%h" tramp-postfix-host-format
))
7031 (when localname
(concat "%p")))
7032 `((?m .
,method
) (?u .
,user
) (?h .
,host
) (?p .
,localname
)))))
7034 ;; CCC: Henrik Holm: Not Changed. Multi Method. What should be done
7035 ;; with this when USER is nil?
7036 (defun tramp-make-tramp-multi-file-name (multi-method method user host localname
)
7037 "Constructs a tramp file name for a multi-hop method."
7038 (unless tramp-make-multi-tramp-file-format
7039 (error "`tramp-make-multi-tramp-file-format' is nil"))
7040 (let* ((prefix-format (nth 0 tramp-make-multi-tramp-file-format
))
7041 (hop-format (nth 1 tramp-make-multi-tramp-file-format
))
7042 (localname-format (nth 2 tramp-make-multi-tramp-file-format
))
7043 (prefix (format-spec prefix-format
`((?m .
,multi-method
))))
7045 (localname (format-spec localname-format
`((?p .
,localname
))))
7047 (len (length method
)))
7049 (let ((m (aref method i
)) (u (aref user i
)) (h (aref host i
)))
7050 (setq hops
(concat hops
(format-spec hop-format
7051 `((?m .
,m
) (?u .
,u
) (?h .
,h
)))))
7053 (concat prefix hops localname
)))
7055 (defun tramp-make-copy-program-file-name (user host localname
)
7056 "Create a file name suitable to be passed to `rcp' and workalikes."
7058 (format "%s@%s:%s" user host localname
)
7059 (format "%s:%s" host localname
)))
7061 (defun tramp-method-out-of-band-p (multi-method method user host
)
7062 "Return t if this is an out-of-band method, nil otherwise."
7063 (tramp-get-method-parameter
7065 (tramp-find-method multi-method method user host
)
7066 user host
'tramp-copy-program
))
7068 ;; Variables local to connection.
7070 (defun tramp-get-ls-command (multi-method method user host
)
7072 (tramp-maybe-open-connection multi-method method user host
)
7073 (set-buffer (tramp-get-buffer multi-method method user host
))
7076 (defun tramp-get-test-groks-nt (multi-method method user host
)
7078 (tramp-maybe-open-connection multi-method method user host
)
7079 (set-buffer (tramp-get-buffer multi-method method user host
))
7080 tramp-test-groks-nt
))
7082 (defun tramp-get-file-exists-command (multi-method method user host
)
7084 (tramp-maybe-open-connection multi-method method user host
)
7085 (set-buffer (tramp-get-buffer multi-method method user host
))
7086 tramp-file-exists-command
))
7088 (defun tramp-get-remote-perl (multi-method method user host
)
7089 (tramp-get-connection-property "perl" nil multi-method method user host
))
7091 (defun tramp-get-remote-ln (multi-method method user host
)
7092 (tramp-get-connection-property "ln" nil multi-method method user host
))
7094 (defun tramp-get-remote-uid (multi-method method user host
)
7095 (tramp-get-connection-property "uid" nil multi-method method user host
))
7097 (defun tramp-get-remote-gid (multi-method method user host
)
7098 (tramp-get-connection-property "gid" nil multi-method method user host
))
7100 ;; Get a property of a TRAMP connection.
7101 (defun tramp-get-connection-property
7102 (property default multi-method method user host
)
7103 "Get the named property for the connection.
7104 If the value is not set for the connection, return `default'"
7105 (tramp-maybe-open-connection multi-method method user host
)
7106 (with-current-buffer (tramp-get-buffer multi-method method user host
)
7109 (symbol-value (intern (concat "tramp-connection-property-" property
)))
7112 ;; Set a property of a TRAMP connection.
7113 (defun tramp-set-connection-property
7114 (property value multi-method method user host
)
7115 "Set the named property of a TRAMP connection."
7116 (tramp-maybe-open-connection multi-method method user host
)
7117 (with-current-buffer (tramp-get-buffer multi-method method user host
)
7118 (set (make-local-variable
7119 (intern (concat "tramp-connection-property-" property
)))
7122 ;; Some predefined connection properties.
7123 (defun tramp-set-remote-encoding (multi-method method user host rem-enc
)
7124 (tramp-set-connection-property "remote-encoding" rem-enc
7125 multi-method method user host
))
7126 (defun tramp-get-remote-encoding (multi-method method user host
)
7127 (tramp-get-connection-property "remote-encoding" nil
7128 multi-method method user host
))
7130 (defun tramp-set-remote-decoding (multi-method method user host rem-dec
)
7131 (tramp-set-connection-property "remote-decoding" rem-dec
7132 multi-method method user host
))
7133 (defun tramp-get-remote-decoding (multi-method method user host
)
7134 (tramp-get-connection-property "remote-decoding" nil
7135 multi-method method user host
))
7137 (defun tramp-set-local-encoding (multi-method method user host loc-enc
)
7138 (tramp-set-connection-property "local-encoding" loc-enc
7139 multi-method method user host
))
7140 (defun tramp-get-local-encoding (multi-method method user host
)
7141 (tramp-get-connection-property "local-encoding" nil
7142 multi-method method user host
))
7144 (defun tramp-set-local-decoding (multi-method method user host loc-dec
)
7145 (tramp-set-connection-property "local-decoding" loc-dec
7146 multi-method method user host
))
7147 (defun tramp-get-local-decoding (multi-method method user host
)
7148 (tramp-get-connection-property "local-decoding" nil
7149 multi-method method user host
))
7151 (defun tramp-get-method-parameter (multi-method method user host param
)
7152 "Return the method parameter PARAM.
7153 If the `tramp-methods' entry does not exist, use the variable PARAM
7155 (unless (boundp param
)
7156 (error "Non-existing method parameter `%s'" param
))
7157 (let ((entry (assoc param
7158 (assoc (tramp-find-method multi-method method user host
)
7162 (symbol-value param
))))
7165 ;; Auto saving to a special directory.
7167 (defun tramp-exists-file-name-handler (operation &rest args
)
7168 (let ((buffer-file-name "/")
7169 (fnha file-name-handler-alist
)
7170 (check-file-name-operation operation
)
7171 (file-name-handler-alist
7174 '(lambda (operation &rest args
)
7175 "Returns OPERATION if it is the one to be checked"
7176 (if (equal check-file-name-operation operation
)
7178 (let ((file-name-handler-alist fnha
))
7179 (apply operation args
))))))))
7180 (eq (apply operation args
) operation
)))
7182 (unless (tramp-exists-file-name-handler 'make-auto-save-file-name
)
7183 (defadvice make-auto-save-file-name
7184 (around tramp-advice-make-auto-save-file-name
() activate
)
7185 "Invoke `tramp-handle-make-auto-save-file-name' for tramp files."
7186 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name)))
7187 (setq ad-return-value
(tramp-handle-make-auto-save-file-name))
7189 (add-hook 'tramp-unload-hook
7190 '(lambda () (ad-unadvise 'make-auto-save-file-name
))))
7192 ;; In Emacs < 22 and XEmacs < 21.5 autosaved remote files have
7193 ;; permission 0666 minus umask. This is a security threat.
7195 (defun tramp-set-auto-save-file-modes ()
7196 "Set permissions of autosaved remote files to the original permissions."
7197 (let ((bfn (buffer-file-name)))
7198 (when (and (stringp bfn
)
7199 (tramp-tramp-file-p bfn
)
7200 (stringp buffer-auto-save-file-name
)
7201 (not (equal bfn buffer-auto-save-file-name
)))
7202 (unless (file-exists-p buffer-auto-save-file-name
)
7203 (write-region "" nil buffer-auto-save-file-name
))
7204 ;; Permissions should be set always, because there might be an old
7205 ;; auto-saved file belonging to another original file. This could
7206 ;; be a security threat.
7207 (set-file-modes buffer-auto-save-file-name
7208 (or (file-modes bfn
) (tramp-octal-to-decimal "0600"))))))
7210 (unless (or (> emacs-major-version
21)
7211 (and (featurep 'xemacs
)
7212 (= emacs-major-version
21)
7213 (> emacs-minor-version
4)))
7214 (add-hook 'auto-save-hook
'tramp-set-auto-save-file-modes
)
7215 (add-hook 'tramp-unload-hook
7217 (remove-hook 'auto-save-hook
'tramp-set-auto-save-file-modes
))))
7219 (defun tramp-subst-strs-in-string (alist string
)
7220 "Replace all occurrences of the string FROM with TO in STRING.
7221 ALIST is of the form ((FROM . TO) ...)."
7224 (let* ((pr (car alist
))
7227 (while (string-match (regexp-quote from
) string
)
7228 (setq string
(replace-match to t t string
)))
7229 (setq alist
(cdr alist
))))
7232 (defun tramp-insert-with-face (face string
)
7233 "Insert text with a specific face."
7234 (let ((start (point)))
7236 (add-text-properties start
(point) (list 'face face
))))
7238 ;; ------------------------------------------------------------
7239 ;; -- Compatibility functions section --
7240 ;; ------------------------------------------------------------
7242 (defun tramp-temporary-file-directory ()
7243 "Return name of directory for temporary files (compat function).
7244 For Emacs, this is the variable `temporary-file-directory', for XEmacs
7245 this is the function `temp-directory'."
7246 (cond ((boundp 'temporary-file-directory
)
7247 (symbol-value 'temporary-file-directory
))
7248 ((fboundp 'temp-directory
)
7249 (funcall (symbol-function 'temp-directory
))) ;pacify byte-compiler
7250 ((let ((d (getenv "TEMP"))) (and d
(file-directory-p d
)))
7251 (file-name-as-directory (getenv "TEMP")))
7252 ((let ((d (getenv "TMP"))) (and d
(file-directory-p d
)))
7253 (file-name-as-directory (getenv "TMP")))
7254 ((let ((d (getenv "TMPDIR"))) (and d
(file-directory-p d
)))
7255 (file-name-as-directory (getenv "TMPDIR")))
7256 ((file-exists-p "c:/temp") (file-name-as-directory "c:/temp"))
7257 (t (message (concat "Neither `temporary-file-directory' nor "
7258 "`temp-directory' is defined -- using /tmp."))
7259 (file-name-as-directory "/tmp"))))
7261 (defun tramp-read-passwd (user host prompt
)
7262 "Read a password from user (compat function).
7263 Invokes `password-read' if available, `read-passwd' else."
7264 (if (functionp 'password-read
)
7265 (let* ((key (concat (or user
(user-login-name)) "@" host
))
7266 (password (apply #'password-read
(list prompt key
))))
7267 (apply #'password-cache-add
(list key password
))
7269 (read-passwd prompt
)))
7271 (defun tramp-clear-passwd (&optional user host
)
7272 "Clear password cache for connection related to current-buffer."
7274 (let ((filename (or buffer-file-name list-buffers-directory
"")))
7275 (when (and (functionp 'password-cache-remove
)
7276 (or (and user host
) (tramp-tramp-file-p filename
)))
7277 (let* ((v (when (tramp-tramp-file-p filename
)
7278 (tramp-dissect-file-name filename
)))
7279 (luser (or user
(tramp-file-name-user v
) (user-login-name)))
7280 (lhost (or host
(tramp-file-name-host v
) (system-name)))
7281 (key (concat luser
"@" lhost
)))
7282 (apply #'password-cache-remove
(list key
))))))
7284 (defun tramp-time-diff (t1 t2
)
7285 "Return the difference between the two times, in seconds.
7286 T1 and T2 are time values (as returned by `current-time' for example)."
7287 ;; Pacify byte-compiler with `symbol-function'.
7288 (cond ((and (fboundp 'subtract-time
)
7289 (fboundp 'float-time
))
7290 (funcall (symbol-function 'float-time
)
7291 (funcall (symbol-function 'subtract-time
) t1 t2
)))
7292 ((and (fboundp 'subtract-time
)
7293 (fboundp 'time-to-seconds
))
7294 (funcall (symbol-function 'time-to-seconds
)
7295 (funcall (symbol-function 'subtract-time
) t1 t2
)))
7296 ((fboundp 'itimer-time-difference
)
7297 (funcall (symbol-function 'itimer-time-difference
)
7298 (if (< (length t1
) 3) (append t1
'(0)) t1
)
7299 (if (< (length t2
) 3) (append t2
'(0)) t2
)))
7301 ;; snarfed from Emacs 21 time-date.el; combining
7302 ;; time-to-seconds and subtract-time
7303 (let ((time (let ((borrow (< (cadr t1
) (cadr t2
))))
7304 (list (- (car t1
) (car t2
) (if borrow
1 0))
7305 (- (+ (if borrow
65536 0) (cadr t1
)) (cadr t2
))))))
7306 (+ (* (car time
) 65536.0)
7308 (/ (or (nth 2 time
) 0) 1000000.0))))))
7310 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type
)
7311 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
7312 EOL-TYPE can be one of `dos', `unix', or `mac'."
7313 (cond ((fboundp 'coding-system-change-eol-conversion
)
7314 (apply #'coding-system-change-eol-conversion
7315 (list coding-system eol-type
)))
7316 ((fboundp 'subsidiary-coding-system
)
7318 #'subsidiary-coding-system
7320 (cond ((eq eol-type
'dos
) 'crlf
)
7321 ((eq eol-type
'unix
) 'lf
)
7322 ((eq eol-type
'mac
) 'cr
)
7324 (error "Unknown EOL-TYPE `%s', must be %s"
7326 "`dos', `unix', or `mac'"))))))
7327 (t (error "Can't change EOL conversion -- is MULE missing?"))))
7329 (defun tramp-split-string (string pattern
)
7330 "Like `split-string' but omit empty strings.
7331 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
7332 This is, the first, empty, element is omitted. In XEmacs, the first
7333 element is not omitted.
7335 Note: this function has been written for `tramp-handle-file-truename'.
7336 If you want to use it for something else, you'll have to check whether
7337 it does the right thing."
7338 (delete "" (split-string string pattern
)))
7340 (defun tramp-set-process-query-on-exit-flag (process flag
)
7341 "Specify if query is needed for process when Emacs is exited.
7342 If the second argument flag is non-nil, Emacs will query the user before
7343 exiting if process is running."
7345 (if (fboundp 'set-process-query-on-exit-flag
)
7346 (symbol-function 'set-process-query-on-exit-flag
)
7347 (symbol-function 'process-kill-without-query
))
7351 ;; ------------------------------------------------------------
7352 ;; -- Kludges section --
7353 ;; ------------------------------------------------------------
7355 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
7356 ;; does not deal well with newline characters. Newline is replaced by
7357 ;; backslash newline. But if, say, the string `a backslash newline b'
7358 ;; is passed to a shell, the shell will expand this into "ab",
7359 ;; completely omitting the newline. This is not what was intended.
7360 ;; It does not appear to be possible to make the function
7361 ;; `shell-quote-argument' work with newlines without making it
7362 ;; dependent on the shell used. But within this package, we know that
7363 ;; we will always use a Bourne-like shell, so we use an approach which
7366 ;; The approach is simple: we call `shell-quote-argument', then
7367 ;; massage the newline part of the result.
7369 ;; This function should produce a string which is grokked by a Unix
7370 ;; shell, even if the Emacs is running on Windows. Since this is the
7371 ;; kludges section, we bind `system-type' in such a way that
7372 ;; `shell-quote-arguments' behaves as if on Unix.
7374 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
7375 ;; function to work with Bourne-like shells.
7377 ;; CCC: This function should be rewritten so that
7378 ;; `shell-quote-argument' is not used. This way, we are safe from
7379 ;; changes in `shell-quote-argument'.
7380 (defun tramp-shell-quote-argument (s)
7381 "Similar to `shell-quote-argument', but groks newlines.
7382 Only works for Bourne-like shells."
7383 (let ((system-type 'not-windows
))
7385 (let ((result (shell-quote-argument s
))
7386 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line
))))
7387 (when (and (>= (length result
) 2)
7388 (string= (substring result
0 2) "\\~"))
7389 (setq result
(substring result
1)))
7390 (while (string-match nl result
)
7391 (setq result
(replace-match (format "'%s'" tramp-rsh-end-of-line
)
7395 ;; ;; EFS hooks itself into the file name handling stuff in more places
7396 ;; ;; than just `file-name-handler-alist'. The following tells EFS to stay
7397 ;; ;; away from tramp.el file names.
7399 ;; ;; This is needed because EFS installs (efs-dired-before-readin) into
7400 ;; ;; 'dired-before-readin-hook'. This prevents EFS from opening an FTP
7401 ;; ;; connection to help it's dired process. Not that I have any real
7402 ;; ;; idea *why* this is helpful to dired.
7404 ;; ;; Anyway, this advice fixes the problem (with a sledgehammer :)
7406 ;; ;; Daniel Pittman <daniel@danann.net>
7408 ;; ;; CCC: when the other defadvice calls have disappeared, make sure
7409 ;; ;; not to call defadvice unless it's necessary. How do we find out whether
7410 ;; ;; it is necessary? (featurep 'efs) is surely the wrong way --
7411 ;; ;; EFS might nicht be loaded yet.
7412 ;; (defadvice efs-ftp-path (around dont-match-tramp-localname activate protect)
7413 ;; "Cause efs-ftp-path to fail when the path is a TRAMP localname."
7414 ;; (if (tramp-tramp-file-p (ad-get-arg 0))
7418 ;; We currently (sometimes) use "[" and "]" in the filename format.
7419 ;; This means that Emacs wants to expand wildcards if
7420 ;; `find-file-wildcards' is non-nil, and then barfs because no
7421 ;; expansion could be found. We detect this situation and do
7422 ;; something really awful: we have `file-expand-wildcards' return the
7423 ;; original filename if it can't expand anything. Let's just hope
7424 ;; that this doesn't break anything else.
7425 ;; CCC: This check is now also really awful; we should search all
7426 ;; of the filename format, not just the prefix.
7427 (when (string-match "\\[" tramp-prefix-format
)
7428 (defadvice file-expand-wildcards
(around tramp-fix activate
)
7429 (let ((name (ad-get-arg 0)))
7430 (if (tramp-tramp-file-p name
)
7431 ;; If it's a Tramp file, dissect it and look if wildcards
7432 ;; need to be expanded at all.
7433 (let ((v (tramp-dissect-file-name name
)))
7434 (if (string-match "[[*?]" (tramp-file-name-localname v
))
7435 (let ((res ad-do-it
))
7436 (setq ad-return-value
(or res
(list name
))))
7437 (setq ad-return-value
(list name
))))
7438 ;; If it is not a Tramp file, just run the original function.
7439 (let ((res ad-do-it
))
7440 (setq ad-return-value
(or res
(list name
)))))))
7441 (add-hook 'tramp-unload-hook
7442 '(lambda () (ad-unadvise 'file-expand-wildcards
))))
7444 ;; Tramp version is useful in a number of situations.
7446 (defun tramp-version (arg)
7447 "Print version number of tramp.el in minibuffer or current buffer."
7449 (if arg
(insert tramp-version
) (message tramp-version
)))
7451 ;; Make the `reporter` functionality available for making bug reports about
7452 ;; the package. A most useful piece of code.
7454 (unless (fboundp 'reporter-submit-bug-report
)
7455 (autoload 'reporter-submit-bug-report
"reporter"))
7458 "Submit a bug report to the TRAMP developers."
7462 (let ((reporter-prompt-for-summary-p t
))
7463 (reporter-submit-bug-report
7464 tramp-bug-report-address
; to-address
7465 (format "tramp (%s)" tramp-version
) ; package name and version
7470 tramp-file-exists-command
7471 tramp-current-multi-method
7472 tramp-current-method
7477 tramp-auto-save-directory
; vars to dump
7478 tramp-default-method
7479 tramp-rsh-end-of-line
7480 tramp-default-password-end-of-line
7482 tramp-login-prompt-regexp
7483 ;; Mask non-7bit characters
7484 (tramp-password-prompt-regexp . tramp-reporter-dump-variable
)
7485 tramp-wrong-passwd-regexp
7486 tramp-yesno-prompt-regexp
7487 tramp-yn-prompt-regexp
7488 tramp-terminal-prompt-regexp
7489 tramp-temp-name-prefix
7490 tramp-file-name-structure
7491 tramp-file-name-regexp
7492 tramp-multi-file-name-structure
7493 tramp-multi-file-name-hop-structure
7495 tramp-multi-connection-function-alist
7498 tramp-coding-commands
7499 tramp-actions-before-shell
7500 tramp-actions-copy-out-of-band
7503 ;; Mask non-7bit characters
7504 (tramp-shell-prompt-pattern . tramp-reporter-dump-variable
)
7506 ,(when (boundp 'tramp-backup-directory-alist
)
7507 'tramp-backup-directory-alist
)
7508 ,(when (boundp 'tramp-bkup-backup-directory-info
)
7509 'tramp-bkup-backup-directory-info
)
7511 ;; Non-tramp variables of interest
7512 ;; Mask non-7bit characters
7513 (shell-prompt-pattern . tramp-reporter-dump-variable
)
7515 backup-by-copying-when-linked
7516 backup-by-copying-when-mismatch
7517 ,(when (boundp 'backup-by-copying-when-privileged-mismatch
)
7518 'backup-by-copying-when-privileged-mismatch
)
7519 ,(when (boundp 'password-cache
)
7521 ,(when (boundp 'password-cache-expiry
)
7522 'password-cache-expiry
)
7523 ,(when (boundp 'backup-directory-alist
)
7524 'backup-directory-alist
)
7525 ,(when (boundp 'bkup-backup-directory-info
)
7526 'bkup-backup-directory-info
)
7527 file-name-handler-alist
))
7529 'tramp-load-report-modules
; pre-hook
7530 'tramp-append-tramp-buffers
; post-hook
7532 Enter your bug report in this message, including as much detail as you
7533 possibly can about the problem, what you did to cause it and what the
7534 local and remote machines are.
7536 If you can give a simple set of instructions to make this bug happen
7537 reliably, please include those. Thank you for helping kill bugs in
7540 Another useful thing to do is to put (setq tramp-debug-buffer t) in
7541 the ~/.emacs file and to repeat the bug. Then, include the contents
7542 of the *tramp/foo* buffer and the *debug tramp/foo* buffer in your bug
7545 --bug report follows this line--
7548 (defun tramp-reporter-dump-variable (varsym mailbuf
)
7549 "Pretty-print the value of the variable in symbol VARSYM.
7550 Used for non-7bit chars in strings."
7551 (let* ((reporter-eval-buffer (symbol-value 'reporter-eval-buffer
))
7552 (val (with-current-buffer reporter-eval-buffer
7553 (symbol-value varsym
))))
7555 ;; There are characters to be masked.
7556 (when (and (boundp 'mm-7bit-chars
)
7558 (concat "[^" (symbol-value 'mm-7bit-chars
) "]") val
))
7559 (with-current-buffer reporter-eval-buffer
7560 (set varsym
(concat "(base64-decode-string \""
7561 (base64-encode-string val
)
7565 (funcall (symbol-function 'reporter-dump-variable
) varsym mailbuf
)
7567 ;; Remove string quotation.
7570 (concat "\\(^.*\\)" "\"" ;; \1 "
7571 "\\((base64-decode-string \\)" "\\\\" ;; \2 \
7572 "\\(\".*\\)" "\\\\" ;; \3 \
7573 "\\(\")\\)" "\"$")) ;; \4 "
7574 (replace-match "\\1\\2\\3\\4")
7576 (insert " ;; variable encoded due to non-printable characters\n"))
7579 ;; Reset VARSYM to old value.
7580 (with-current-buffer reporter-eval-buffer
7583 (defun tramp-load-report-modules ()
7584 "Load needed modules for reporting."
7586 ;; We load message.el and mml.el from Gnus.
7587 (if (featurep 'xemacs
)
7589 (load "message" 'noerror
)
7590 (load "mml" 'noerror
))
7591 (require 'message nil
'noerror
)
7592 (require 'mml nil
'noerror
))
7593 (when (functionp 'message-mode
)
7594 (funcall (symbol-function 'message-mode
)))
7595 (when (functionp 'mml-mode
)
7596 (funcall (symbol-function 'mml-mode
) t
)))
7598 (defun tramp-append-tramp-buffers ()
7599 "Append Tramp buffers into the bug report."
7602 (eq major-mode
'message-mode
)
7604 (symbol-value 'mml-mode
))
7606 (let* ((tramp-buf-regexp "\\*\\(debug \\)?tramp/")
7609 (mapcar '(lambda (b)
7610 (when (string-match tramp-buf-regexp
(buffer-name b
)) b
))
7612 (curbuf (current-buffer)))
7614 ;; There is at least one Tramp buffer.
7616 (switch-to-buffer (list-buffers-noselect nil
))
7617 (delete-other-windows)
7618 (setq buffer-read-only nil
)
7619 (goto-char (point-min))
7621 (if (re-search-forward tramp-buf-regexp
(tramp-point-at-eol) t
)
7624 (let ((start (point)))
7626 (kill-region start
(point)))))
7628 The buffer(s) above will be appended to this message. If you don't want
7629 to append a buffer because it contains sensible data, or because the buffer
7630 is too large, you should delete the respective buffer. The buffer(s) will
7631 contain user and host names. Passwords will never be included there.")
7633 (when (and tramp-debug-buffer
(> tramp-verbose
9))
7635 (let ((start (point)))
7637 Please note that you have set `tramp-verbose' to a value greater than 9.
7638 Therefore, the contents of files might be included in the debug buffer(s).")
7639 (add-text-properties start
(point) (list 'face
'italic
))))
7641 (set-buffer-modified-p nil
)
7642 (setq buffer-read-only t
)
7643 (goto-char (point-min))
7645 (if (y-or-n-p "Do you want to append the buffer(s)? ")
7646 ;; OK, let's send. First we delete the buffer list.
7649 (switch-to-buffer curbuf
)
7650 (goto-char (point-max))
7652 (dolist (buffer buffer-list
)
7653 (funcall (symbol-function 'mml-insert-empty-tag
)
7654 'part
'type
"text/plain" 'encoding
"base64"
7655 'disposition
"attachment" 'buffer
(buffer-name buffer
)
7656 'description
(buffer-name buffer
)))
7657 (set-buffer-modified-p nil
))
7659 ;; Don't send. Delete the message buffer.
7661 (set-buffer-modified-p nil
)
7663 (throw 'dont-send nil
))))))
7665 (defalias 'tramp-submit-bug
'tramp-bug
)
7667 ;; Checklist for `tramp-unload-hook'
7668 ;; - Unload all `tramp-*' packages
7669 ;; - Reset `file-name-handler-alist'
7670 ;; - Cleanup hooks where Tramp functions are in
7671 ;; - Cleanup advised functions
7672 ;; - Cleanup autoloads
7674 (defun tramp-unload-tramp ()
7675 "Discard Tramp from loading remote files."
7677 ;; When Tramp is not loaded yet, its autoloads are still active.
7678 (tramp-unload-file-name-handlers)
7679 ;; ange-ftp settings must be enabled.
7680 (when (functionp 'tramp-ftp-enable-ange-ftp
)
7681 (funcall (symbol-function 'tramp-ftp-enable-ange-ftp
)))
7682 ;; `tramp-util' unloads also `tramp'.
7683 (condition-case nil
;; maybe its not loaded yet.
7684 (unload-feature (if (featurep 'tramp-util
) 'tramp-util
'tramp
) 'force
)
7689 ;; Make sure that we get integration with the VC package.
7690 ;; When it is loaded, we need to pull in the integration module.
7691 ;; This must come after (provide 'tramp) because tramp-vc.el
7693 (eval-after-load "vc"
7696 (add-hook 'tramp-unload-hook
7698 (when (featurep 'tramp-vc
)
7699 (unload-feature 'tramp-vc
'force
))))))
7703 ;; * Allow putting passwords in the filename.
7704 ;; This should be implemented via a general mechanism to add
7705 ;; parameters in filenames. There is currently a kludge for
7706 ;; putting the port number into the filename for ssh and ftp
7707 ;; files. This could be subsumed by the new mechanism as well.
7708 ;; Another approach is to read a netrc file like ~/.authinfo
7710 ;; * Handle nonlocal exits such as C-g.
7711 ;; * Autodetect if remote `ls' groks the "--dired" switch.
7712 ;; * Add fallback for inline encodings. This should be used
7713 ;; if the remote end doesn't support mimencode or a similar program.
7714 ;; For reading files from the remote host, we can just parse the output
7715 ;; of `od -b'. For writing files to the remote host, we construct
7716 ;; a shell program which contains only "safe" ascii characters
7717 ;; and which writes the right bytes to the file. We can use printf(1)
7718 ;; or "echo -e" or the printf function in awk and use octal escapes
7719 ;; for the "dangerous" characters. The null byte might be a problem.
7720 ;; On some systems, the octal escape doesn't work. So we try the following
7721 ;; two commands to write a null byte:
7722 ;; dd if=/dev/zero bs=1 count=1
7723 ;; echo | tr '\n' '\000'
7724 ;; * Separate local `tramp-coding-commands' from remote ones. Connect
7725 ;; the two via a format which can be `uu' or `b64'. Then we can search
7726 ;; for the right local commands and the right remote commands separately.
7727 ;; * Cooperate with PCL-CVS. It uses start-process, which doesn't
7728 ;; work for remote files.
7729 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
7730 ;; `shell-quote-argument'.
7731 ;; * Completion gets confused when you leave out the method name.
7732 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
7733 ;; by the files in that directory. Add this here.
7734 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
7735 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
7736 ;; * When logging in, keep looking for questions according to an alist
7737 ;; and then invoke the right function.
7738 ;; * Case-insensitive filename completion. (Norbert Goevert.)
7739 ;; * Running CVS remotely doesn't appear to work right. It thinks
7740 ;; files are locked by somebody else even if I'm the locking user.
7741 ;; Sometimes, one gets `No CVSROOT specified' errors from CVS.
7743 ;; * Don't use globbing for directories with many files, as this is
7744 ;; likely to produce long command lines, and some shells choke on
7745 ;; long command lines.
7746 ;; * Find out about the new auto-save mechanism in Emacs 21 and
7747 ;; do the right thing.
7748 ;; * `vc-directory' does not work. It never displays any files, even
7749 ;; if it does show files when run locally.
7750 ;; * Allow correction of passwords, if the remote end allows this.
7751 ;; (Mark Hershberger)
7752 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
7753 ;; * Do asynchronous `shell-command's.
7754 ;; * Grok `append' parameter for `write-region'.
7755 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
7756 ;; * abbreviate-file-name
7757 ;; * grok ~ in tramp-remote-path (Henrik Holm <henrikh@tele.ntnu.no>)
7758 ;; * Also allow to omit user names when doing multi-hop. Not sure yet
7759 ;; what the user names should default to, though.
7760 ;; * better error checking. At least whenever we see something
7761 ;; strange when doing zerop, we should kill the process and start
7762 ;; again. (Greg Stark)
7763 ;; * Add caching for filename completion. (Greg Stark)
7764 ;; Of course, this has issues with usability (stale cache bites)
7765 ;; -- <daniel@danann.net>
7766 ;; * Provide a local cache of old versions of remote files for the rsync
7767 ;; transfer method to use. (Greg Stark)
7768 ;; * Remove unneeded parameters from methods.
7769 ;; * Invoke rsync once for copying a whole directory hierarchy.
7770 ;; (Francesco Potort\e,Al\e(B)
7771 ;; * Should we set PATH ourselves or should we rely on the remote end
7773 ;; * Make it work for XEmacs 20, which is missing `with-timeout'.
7774 ;; * Make it work for different encodings, and for different file name
7775 ;; encodings, too. (Daniel Pittman)
7776 ;; * Change applicable functions to pass a struct tramp-file-name rather
7777 ;; than the individual items MULTI-METHOD, METHOD, USER, HOST, LOCALNAME.
7778 ;; * Implement asynchronous shell commands.
7779 ;; * Clean up unused *tramp/foo* buffers after a while. (Pete Forman)
7780 ;; * Progress reports while copying files. (Michael Kifer)
7781 ;; * `Smart' connection method that uses inline for small and out of
7782 ;; band for large files. (Michael Kifer)
7783 ;; * Don't search for perl5 and perl. Instead, only search for perl and
7784 ;; then look if it's the right version (with `perl -v').
7785 ;; * When editing a remote CVS controlled file as a different user, VC
7786 ;; gets confused about the file locking status. Try to find out why
7787 ;; the workaround doesn't work.
7788 ;; * Change `copy-file' to grok the case where the filename handler
7789 ;; for the source and the target file are different. Right now,
7790 ;; it looks at the source file and then calls that handler, if
7791 ;; there is one. But since ange-ftp, for instance, does not know
7792 ;; about Tramp, it does not do the right thing if the target file
7793 ;; name is a Tramp name.
7794 ;; * Username and hostname completion.
7795 ;; ** If `partial-completion-mode' isn't loaded, "/foo:bla" tries to
7796 ;; connect to host "blabla" already if that host is unique. No idea
7797 ;; how to suppress. Maybe not an essential problem.
7798 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode'.
7799 ;; ** Extend `tramp-get-completion-su' for NIS and shadow passwords.
7800 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
7801 ;; Code is nearly identical.
7802 ;; ** Decide whiche files to take for searching user/host names depending on
7803 ;; operating system (windows-nt) in `tramp-completion-function-alist'.
7804 ;; ** Enhance variables for debug.
7805 ;; ** Implement "/multi:" completion.
7806 ;; ** Add a learning mode for completion. Make results persistent.
7807 ;; * Allow out-of-band methods as _last_ multi-hop.
7809 ;; Functions for file-name-handler-alist:
7810 ;; diff-latest-backup-file -- in diff.el
7811 ;; dired-uncache -- this will be needed when we do insert-directory caching
7812 ;; file-name-as-directory -- use primitive?
7813 ;; file-name-sans-versions -- use primitive?
7814 ;; get-file-buffer -- use primitive
7817 ;;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
7818 ;;; tramp.el ends here