1 ;;; tramp.el --- Transparent Remote Access, Multiple Protocol
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
6 ;; (copyright statements below in code to be updated with the above notice)
8 ;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
9 ;; Michael Albinus <michael.albinus@gmx.de>
10 ;; Keywords: comm, processes
12 ;; This file is part of GNU Emacs.
14 ;; GNU Emacs is free software: you can redistribute it and/or modify
15 ;; it under the terms of the GNU General Public License as published by
16 ;; the Free Software Foundation, either version 3 of the License, or
17 ;; (at your option) any later version.
19 ;; GNU Emacs is distributed in the hope that it will be useful,
20 ;; but WITHOUT ANY WARRANTY; without even the implied warranty of
21 ;; MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
22 ;; GNU General Public License for more details.
24 ;; You should have received a copy of the GNU General Public License
25 ;; along with GNU Emacs. If not, see <http://www.gnu.org/licenses/>.
29 ;; This package provides remote file editing, similar to ange-ftp.
30 ;; The difference is that ange-ftp uses FTP to transfer files between
31 ;; the local and the remote host, whereas tramp.el uses a combination
32 ;; of rsh and rcp or other work-alike programs, such as ssh/scp.
34 ;; For more detailed instructions, please see the info file.
39 ;; This package only works for Emacs 22.1 and higher, and for XEmacs 21.4
40 ;; and higher. For XEmacs 21, you need the package `fsf-compat' for
41 ;; the `with-timeout' macro.
43 ;; Also see the todo list at the bottom of this file.
45 ;; The current version of Tramp can be retrieved from the following URL:
46 ;; http://ftp.gnu.org/gnu/tramp/
48 ;; There's a mailing list for this, as well. Its name is:
49 ;; tramp-devel@gnu.org
50 ;; You can use the Web to subscribe, under the following URL:
51 ;; http://lists.gnu.org/mailman/listinfo/tramp-devel
53 ;; For the adventurous, the current development sources are available
54 ;; via CVS. You can find instructions about this at the following URL:
55 ;; http://savannah.gnu.org/projects/tramp/
56 ;; Click on "CVS" in the navigation bar near the top.
58 ;; Don't forget to put on your asbestos longjohns, first!
62 ;; Since Emacs 23.1, loading messages have been disabled during
63 ;; autoload. However, loading Tramp takes a while, and it could
64 ;; happen while typing a filename in the minibuffer. Therefore, Tramp
65 ;; shall inform about.
66 (when (and load-in-progress
(null (current-message)))
67 (message "Loading tramp..."))
69 ;; The Tramp version number and bug report address, as prepared by configure.
71 (add-hook 'tramp-unload-hook
73 (when (featurep 'trampver
)
74 (unload-feature 'trampver
'force
))))
76 (require 'tramp-compat
)
77 (add-hook 'tramp-unload-hook
79 (when (featurep 'tramp-compat
)
80 (unload-feature 'tramp-compat
'force
))))
82 (require 'format-spec
)
83 ;; As long as password.el is not part of (X)Emacs, it shouldn't
85 (if (featurep 'xemacs
)
86 (load "password" 'noerror
)
87 (or (require 'password-cache nil
'noerror
)
88 (require 'password nil
'noerror
))) ; from No Gnus, also in tar ball
94 (if (featurep 'xemacs
)
95 (load "auth-source" 'noerror
)
96 (require 'auth-source nil
'noerror
)))
98 ;; Requiring 'tramp-cache results in an endless loop.
99 (autoload 'tramp-get-file-property
"tramp-cache")
100 (autoload 'tramp-set-file-property
"tramp-cache")
101 (autoload 'tramp-flush-file-property
"tramp-cache")
102 (autoload 'tramp-flush-directory-property
"tramp-cache")
103 (autoload 'tramp-get-connection-property
"tramp-cache")
104 (autoload 'tramp-set-connection-property
"tramp-cache")
105 (autoload 'tramp-flush-connection-property
"tramp-cache")
106 (autoload 'tramp-parse-connection-properties
"tramp-cache")
107 (add-hook 'tramp-unload-hook
109 (when (featurep 'tramp-cache
)
110 (unload-feature 'tramp-cache
'force
))))
112 (autoload 'tramp-uuencode-region
"tramp-uu"
113 "Implementation of `uuencode' in Lisp.")
114 (add-hook 'tramp-unload-hook
116 (when (featurep 'tramp-uu
)
117 (unload-feature 'tramp-uu
'force
))))
119 (autoload 'uudecode-decode-region
"uudecode")
121 ;; The following Tramp packages must be loaded after tramp.el, because
122 ;; they require it as well.
123 (eval-after-load "tramp"
128 ;; Tramp interactive commands.
131 ;; Load foreign FTP method.
132 (if (featurep 'xemacs
) 'tramp-efs
'tramp-ftp
)
134 ;; tramp-smb uses "smbclient" from Samba. Not available
135 ;; under Cygwin and Windows, because they don't offer
136 ;; "smbclient". And even not necessary there, because Emacs
137 ;; supports UNC file names like "//host/share/localname".
138 (unless (memq system-type
'(cygwin windows-nt
)) 'tramp-smb
)
140 ;; Load foreign FISH method.
143 ;; tramp-gvfs needs D-Bus messages. Available since Emacs 23
144 ;; on some system types. We don't call `dbus-ping', because
145 ;; this would load dbus.el.
146 (when (and (featurep 'dbusbind
)
148 (tramp-compat-funcall 'dbus-get-unique-name
:session
)
150 (tramp-compat-process-running-p "gvfs-fuse-daemon"))
153 ;; Load gateways. It needs `make-network-process' from Emacs 22.
154 (when (functionp 'make-network-process
) 'tramp-gw
)
156 ;; tramp-imap needs both epa (from Emacs 23.1) and imap-hash
157 ;; (from Emacs 23.2).
158 (when (and (locate-library "epa") (locate-library "imap-hash"))
162 ;; We have used just some basic tests, whether a package shall
163 ;; be added. There might still be other errors during loading,
164 ;; which we will catch here.
165 (catch 'tramp-loading
167 (add-hook 'tramp-unload-hook
169 (when (featurep (quote ,feature
))
170 (unload-feature (quote ,feature
) 'force
)))))
171 (unless (featurep feature
)
172 (message "Loading %s failed, ignoring this package" feature
)))))
174 ;;; User Customizable Internal Variables:
177 "Edit remote files with a combination of rsh and rcp or similar programs."
182 ;; Maybe we need once a real Tramp mode, with key bindings etc.
184 (defcustom tramp-mode t
185 "*Whether Tramp is enabled.
186 If it is set to nil, all remote file names are used literally."
190 (defcustom tramp-verbose
3
191 "*Verbosity level for Tramp messages.
192 Any level x includes messages for all levels 1 .. x-1. The levels are
194 0 silent (no tramp messages at all)
197 3 connection to remote hosts (default level)
200 6 sent and received strings
202 8 connection properties
209 (when (boundp 'backup-directory-alist
)
210 (defcustom tramp-backup-directory-alist nil
211 "Alist of filename patterns and backup directory names.
212 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
213 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
214 is a local file name, the backup directory is prepended with Tramp file
215 name prefix \(method, user, host\) of file.
217 \(setq tramp-backup-directory-alist backup-directory-alist\)
219 gives the same backup policy for Tramp files on their hosts like the
220 policy for local files."
222 :type
'(repeat (cons (regexp :tag
"Regexp matching filename")
223 (directory :tag
"Backup directory name"))))))
225 ;; XEmacs case. We cannot check for `bkup-backup-directory-info', because
226 ;; the package "backup-dir" might not be loaded yet.
228 (when (featurep 'xemacs
)
229 (defcustom tramp-bkup-backup-directory-info nil
230 "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
231 It has the same meaning like `bkup-backup-directory-info' from package
232 `backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local
233 file name, the backup directory is prepended with Tramp file name prefix
234 \(method, user, host\) of file.
236 \(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\)
238 gives the same backup policy for Tramp files on their hosts like the
239 policy for local files."
241 (list (regexp :tag
"File regexp")
242 (string :tag
"Backup Dir")
247 (const search-upward
))))
250 (defcustom tramp-auto-save-directory nil
251 "*Put auto-save files in this directory, if set.
252 The idea is to use a local directory so that auto-saving is faster."
254 :type
'(choice (const nil
) string
))
256 (defcustom tramp-encoding-shell
257 (if (memq system-type
'(windows-nt))
260 "*Use this program for encoding and decoding commands on the local host.
261 This shell is used to execute the encoding and decoding command on the
262 local host, so if you want to use `~' in those commands, you should
263 choose a shell here which groks tilde expansion. `/bin/sh' normally
264 does not understand tilde expansion.
266 For encoding and deocding, commands like the following are executed:
268 /bin/sh -c COMMAND < INPUT > OUTPUT
270 This variable can be used to change the \"/bin/sh\" part. See the
271 variable `tramp-encoding-command-switch' for the \"-c\" part.
273 Note that this variable is not used for remote commands. There are
274 mechanisms in tramp.el which automatically determine the right shell to
275 use for the remote host."
277 :type
'(file :must-match t
))
279 (defcustom tramp-encoding-command-switch
280 (if (string-match "cmd\\.exe" tramp-encoding-shell
)
283 "*Use this switch together with `tramp-encoding-shell' for local commands.
284 See the variable `tramp-encoding-shell' for more information."
288 (defcustom tramp-inline-compress-start-size
4096
289 "*The minimum size of compressing where inline transfer.
290 When inline transfer, compress transfered data of file
291 whose size is this value or above (up to `tramp-copy-size-limit').
292 If it is nil, no compression at all will be applied."
294 :type
'(choice (const nil
) integer
))
296 (defcustom tramp-copy-size-limit
10240
297 "*The maximum file size where inline copying is preferred over an out-of-the-band copy.
298 If it is nil, inline out-of-the-band copy will be used without a check."
300 :type
'(choice (const nil
) integer
))
302 (defcustom tramp-terminal-type
"dumb"
303 "*Value of TERM environment variable for logging in to remote host.
304 Because Tramp wants to parse the output of the remote shell, it is easily
305 confused by ANSI color escape sequences and suchlike. Often, shell init
306 files conditionalize this setup based on the TERM environment variable."
310 ;; ksh on OpenBSD 4.5 requires, that PS1 contains a `#' character for
311 ;; root users. It uses the `$' character for other users. In order
312 ;; to guarantee a proper prompt, we use "#$" for the prompt.
314 (defvar tramp-end-of-output
317 (md5 (concat (prin1-to-string process-environment
) (current-time-string))))
318 "String used to recognize end of output.
319 The '$' character at the end is quoted; the string cannot be
320 detected as prompt when being sent on echoing hosts, therefore.")
322 (defconst tramp-initial-end-of-output
"#$ "
323 "Prompt when establishing a connection.")
325 (defvar tramp-methods
326 `(("rcp" (tramp-login-program "rsh")
327 (tramp-login-args (("%h") ("-l" "%u")))
328 (tramp-remote-sh "/bin/sh")
329 (tramp-copy-program "rcp")
330 (tramp-copy-args (("-p" "%k") ("-r")))
331 (tramp-copy-keep-date t
)
332 (tramp-copy-recursive t
)
333 (tramp-password-end-of-line nil
))
334 ("scp" (tramp-login-program "ssh")
335 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
337 (tramp-async-args (("-q")))
338 (tramp-remote-sh "/bin/sh")
339 (tramp-copy-program "scp")
340 (tramp-copy-args (("-P" "%p") ("-p" "%k")
342 (tramp-copy-keep-date t
)
343 (tramp-copy-recursive t
)
344 (tramp-password-end-of-line nil
)
345 (tramp-gw-args (("-o"
346 "GlobalKnownHostsFile=/dev/null")
347 ("-o" "UserKnownHostsFile=/dev/null")
348 ("-o" "StrictHostKeyChecking=no")))
349 (tramp-default-port 22))
350 ("scp1" (tramp-login-program "ssh")
351 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
353 (tramp-async-args (("-q")))
354 (tramp-remote-sh "/bin/sh")
355 (tramp-copy-program "scp")
356 (tramp-copy-args (("-1") ("-P" "%p") ("-p" "%k")
358 (tramp-copy-keep-date t
)
359 (tramp-copy-recursive t
)
360 (tramp-password-end-of-line nil
)
361 (tramp-gw-args (("-o"
362 "GlobalKnownHostsFile=/dev/null")
363 ("-o" "UserKnownHostsFile=/dev/null")
364 ("-o" "StrictHostKeyChecking=no")))
365 (tramp-default-port 22))
366 ("scp2" (tramp-login-program "ssh")
367 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
369 (tramp-async-args (("-q")))
370 (tramp-remote-sh "/bin/sh")
371 (tramp-copy-program "scp")
372 (tramp-copy-args (("-2") ("-P" "%p") ("-p" "%k")
374 (tramp-copy-keep-date t
)
375 (tramp-copy-recursive t
)
376 (tramp-password-end-of-line nil
)
377 (tramp-gw-args (("-o"
378 "GlobalKnownHostsFile=/dev/null")
379 ("-o" "UserKnownHostsFile=/dev/null")
380 ("-o" "StrictHostKeyChecking=no")))
381 (tramp-default-port 22))
383 (tramp-login-program "ssh1")
384 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
386 (tramp-remote-sh "/bin/sh")
387 (tramp-copy-program "scp1")
388 (tramp-copy-args (("-p" "%k") ("-r")))
389 (tramp-copy-keep-date t
)
390 (tramp-copy-recursive t
)
391 (tramp-password-end-of-line nil
))
393 (tramp-login-program "ssh2")
394 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
396 (tramp-remote-sh "/bin/sh")
397 (tramp-copy-program "scp2")
398 (tramp-copy-args (("-p" "%k") ("-r")))
399 (tramp-copy-keep-date t
)
400 (tramp-copy-recursive t
)
401 (tramp-password-end-of-line nil
))
402 ("sftp" (tramp-login-program "ssh")
403 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
405 (tramp-async-args (("-q")))
406 (tramp-remote-sh "/bin/sh")
407 (tramp-copy-program "sftp")
408 (tramp-copy-args nil
)
409 (tramp-copy-keep-date nil
)
410 (tramp-password-end-of-line nil
))
411 ("rsync" (tramp-login-program "ssh")
412 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
414 (tramp-async-args (("-q")))
415 (tramp-remote-sh "/bin/sh")
416 (tramp-copy-program "rsync")
417 (tramp-copy-args (("-e" "ssh") ("-t" "%k") ("-r")))
418 (tramp-copy-keep-date t
)
419 (tramp-copy-keep-tmpfile t
)
420 (tramp-copy-recursive t
)
421 (tramp-password-end-of-line nil
))
423 (tramp-login-program "ssh")
424 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
425 ("-o" "ControlPath=%t.%%r@%%h:%%p")
426 ("-o" "ControlMaster=yes")
428 (tramp-async-args (("-q")))
429 (tramp-remote-sh "/bin/sh")
430 (tramp-copy-program "rsync")
431 (tramp-copy-args (("-t" "%k") ("-r")))
432 (tramp-copy-env (("RSYNC_RSH")
435 " -o ControlPath=%t.%%r@%%h:%%p"
436 " -o ControlMaster=auto"))))
437 (tramp-copy-keep-date t
)
438 (tramp-copy-keep-tmpfile t
)
439 (tramp-copy-recursive t
)
440 (tramp-password-end-of-line nil
))
441 ("remcp" (tramp-login-program "remsh")
442 (tramp-login-args (("%h") ("-l" "%u")))
443 (tramp-remote-sh "/bin/sh")
444 (tramp-copy-program "rcp")
445 (tramp-copy-args (("-p" "%k")))
446 (tramp-copy-keep-date t
)
447 (tramp-password-end-of-line nil
))
448 ("rsh" (tramp-login-program "rsh")
449 (tramp-login-args (("%h") ("-l" "%u")))
450 (tramp-remote-sh "/bin/sh")
451 (tramp-copy-program nil
)
452 (tramp-copy-args nil
)
453 (tramp-copy-keep-date nil
)
454 (tramp-password-end-of-line nil
))
455 ("ssh" (tramp-login-program "ssh")
456 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
458 (tramp-async-args (("-q")))
459 (tramp-remote-sh "/bin/sh")
460 (tramp-copy-program nil
)
461 (tramp-copy-args nil
)
462 (tramp-copy-keep-date nil
)
463 (tramp-password-end-of-line nil
)
464 (tramp-gw-args (("-o"
465 "GlobalKnownHostsFile=/dev/null")
466 ("-o" "UserKnownHostsFile=/dev/null")
467 ("-o" "StrictHostKeyChecking=no")))
468 (tramp-default-port 22))
469 ("ssh1" (tramp-login-program "ssh")
470 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
472 (tramp-async-args (("-q")))
473 (tramp-remote-sh "/bin/sh")
474 (tramp-copy-program nil
)
475 (tramp-copy-args nil
)
476 (tramp-copy-keep-date nil
)
477 (tramp-password-end-of-line nil
)
478 (tramp-gw-args (("-o"
479 "GlobalKnownHostsFile=/dev/null")
480 ("-o" "UserKnownHostsFile=/dev/null")
481 ("-o" "StrictHostKeyChecking=no")))
482 (tramp-default-port 22))
483 ("ssh2" (tramp-login-program "ssh")
484 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
486 (tramp-async-args (("-q")))
487 (tramp-remote-sh "/bin/sh")
488 (tramp-copy-program nil
)
489 (tramp-copy-args nil
)
490 (tramp-copy-keep-date nil
)
491 (tramp-password-end-of-line nil
)
492 (tramp-gw-args (("-o"
493 "GlobalKnownHostsFile=/dev/null")
494 ("-o" "UserKnownHostsFile=/dev/null")
495 ("-o" "StrictHostKeyChecking=no")))
496 (tramp-default-port 22))
498 (tramp-login-program "ssh1")
499 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
501 (tramp-async-args (("-q")))
502 (tramp-remote-sh "/bin/sh")
503 (tramp-copy-program nil
)
504 (tramp-copy-args nil
)
505 (tramp-copy-keep-date nil
)
506 (tramp-password-end-of-line nil
))
508 (tramp-login-program "ssh2")
509 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
511 (tramp-remote-sh "/bin/sh")
512 (tramp-copy-program nil
)
513 (tramp-copy-args nil
)
514 (tramp-copy-keep-date nil
)
515 (tramp-password-end-of-line nil
))
516 ("remsh" (tramp-login-program "remsh")
517 (tramp-login-args (("%h") ("-l" "%u")))
518 (tramp-remote-sh "/bin/sh")
519 (tramp-copy-program nil
)
520 (tramp-copy-args nil
)
521 (tramp-copy-keep-date nil
)
522 (tramp-password-end-of-line nil
))
524 (tramp-login-program "telnet")
525 (tramp-login-args (("%h") ("%p")))
526 (tramp-remote-sh "/bin/sh")
527 (tramp-copy-program nil
)
528 (tramp-copy-args nil
)
529 (tramp-copy-keep-date nil
)
530 (tramp-password-end-of-line nil
)
531 (tramp-default-port 23))
532 ("su" (tramp-login-program "su")
533 (tramp-login-args (("-") ("%u")))
534 (tramp-remote-sh "/bin/sh")
535 (tramp-copy-program nil
)
536 (tramp-copy-args nil
)
537 (tramp-copy-keep-date nil
)
538 (tramp-password-end-of-line nil
))
539 ("sudo" (tramp-login-program "sudo")
540 (tramp-login-args (("-u" "%u")
541 ("-s") ("-H") ("-p" "Password:")))
542 (tramp-remote-sh "/bin/sh")
543 (tramp-copy-program nil
)
544 (tramp-copy-args nil
)
545 (tramp-copy-keep-date nil
)
546 (tramp-password-end-of-line nil
))
547 ("scpc" (tramp-login-program "ssh")
548 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
549 ("-o" "ControlPath=%t.%%r@%%h:%%p")
550 ("-o" "ControlMaster=yes")
552 (tramp-async-args (("-q")))
553 (tramp-remote-sh "/bin/sh")
554 (tramp-copy-program "scp")
555 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q")
556 ("-o" "ControlPath=%t.%%r@%%h:%%p")
557 ("-o" "ControlMaster=auto")))
558 (tramp-copy-keep-date t
)
559 (tramp-password-end-of-line nil
)
560 (tramp-gw-args (("-o"
561 "GlobalKnownHostsFile=/dev/null")
562 ("-o" "UserKnownHostsFile=/dev/null")
563 ("-o" "StrictHostKeyChecking=no")))
564 (tramp-default-port 22))
565 ("scpx" (tramp-login-program "ssh")
566 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
567 ("-e" "none" "-t" "-t" "/bin/sh")))
568 (tramp-async-args (("-q")))
569 (tramp-remote-sh "/bin/sh")
570 (tramp-copy-program "scp")
571 (tramp-copy-args (("-p" "%k")))
572 (tramp-copy-keep-date t
)
573 (tramp-password-end-of-line nil
)
574 (tramp-gw-args (("-o"
575 "GlobalKnownHostsFile=/dev/null")
576 ("-o" "UserKnownHostsFile=/dev/null")
577 ("-o" "StrictHostKeyChecking=no")))
578 (tramp-default-port 22))
579 ("sshx" (tramp-login-program "ssh")
580 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
581 ("-e" "none" "-t" "-t" "/bin/sh")))
582 (tramp-async-args (("-q")))
583 (tramp-remote-sh "/bin/sh")
584 (tramp-copy-program nil
)
585 (tramp-copy-args nil
)
586 (tramp-copy-keep-date nil
)
587 (tramp-password-end-of-line nil
)
588 (tramp-gw-args (("-o"
589 "GlobalKnownHostsFile=/dev/null")
590 ("-o" "UserKnownHostsFile=/dev/null")
591 ("-o" "StrictHostKeyChecking=no")))
592 (tramp-default-port 22))
594 (tramp-login-program "krlogin")
595 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
596 (tramp-remote-sh "/bin/sh")
597 (tramp-copy-program nil
)
598 (tramp-copy-args nil
)
599 (tramp-copy-keep-date nil
)
600 (tramp-password-end-of-line nil
))
601 ("plink" (tramp-login-program "plink")
602 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
604 (tramp-remote-sh "/bin/sh")
605 (tramp-copy-program nil
)
606 (tramp-copy-args nil
)
607 (tramp-copy-keep-date nil
)
608 (tramp-password-end-of-line "xy") ;see docstring for "xy"
609 (tramp-default-port 22))
611 (tramp-login-program "plink")
612 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
614 (tramp-remote-sh "/bin/sh")
615 (tramp-copy-program nil
)
616 (tramp-copy-args nil
)
617 (tramp-copy-keep-date nil
)
618 (tramp-password-end-of-line "xy") ;see docstring for "xy"
619 (tramp-default-port 22))
621 (tramp-login-program "plink")
622 ;; ("%h") must be a single element, see
623 ;; `tramp-compute-multi-hops'.
624 (tramp-login-args (("-load") ("%h") ("-t")
626 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
628 tramp-initial-end-of-output
))
630 (tramp-remote-sh "/bin/sh")
631 (tramp-copy-program nil
)
632 (tramp-copy-args nil
)
633 (tramp-copy-keep-date nil
)
634 (tramp-password-end-of-line nil
))
635 ("pscp" (tramp-login-program "plink")
636 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
638 (tramp-remote-sh "/bin/sh")
639 (tramp-copy-program "pscp")
640 (tramp-copy-args (("-P" "%p") ("-scp") ("-p" "%k")))
641 (tramp-copy-keep-date t
)
642 (tramp-password-end-of-line "xy") ;see docstring for "xy"
643 (tramp-default-port 22))
644 ("psftp" (tramp-login-program "plink")
645 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
647 (tramp-remote-sh "/bin/sh")
648 (tramp-copy-program "pscp")
649 (tramp-copy-args (("-P" "%p") ("-sftp") ("-p" "%k")))
650 (tramp-copy-keep-date t
)
651 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
652 ("fcp" (tramp-login-program "fsh")
653 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
654 (tramp-remote-sh "/bin/sh -i")
655 (tramp-copy-program "fcp")
656 (tramp-copy-args (("-p" "%k")))
657 (tramp-copy-keep-date t
)
658 (tramp-password-end-of-line nil
)))
659 "*Alist of methods for remote files.
660 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
661 Each NAME stands for a remote access method. Each PARAM is a
662 pair of the form (KEY VALUE). The following KEYs are defined:
664 This specifies the Bourne shell to use on the remote host. This
665 MUST be a Bourne-like shell. It is normally not necessary to set
666 this to any value other than \"/bin/sh\": Tramp wants to use a shell
667 which groks tilde expansion, but it can search for it. Also note
668 that \"/bin/sh\" exists on all Unixen, this might not be true for
669 the value that you decide to use. You Have Been Warned.
670 * `tramp-login-program'
671 This specifies the name of the program to use for logging in to the
672 remote host. This may be the name of rsh or a workalike program,
673 or the name of telnet or a workalike, or the name of su or a workalike.
675 This specifies the list of arguments to pass to the above
676 mentioned program. Please note that this is a list of list of arguments,
677 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
678 here. Instead, you want a list (\"-a\" \"-b\"), or (\"-f\" \"foo\").
679 There are some patterns: \"%h\" in this list is replaced by the host
680 name, \"%u\" is replaced by the user name, \"%p\" is replaced by the
681 port number, and \"%%\" can be used to obtain a literal percent character.
682 If a list containing \"%h\", \"%u\" or \"%p\" is unchanged during
683 expansion (i.e. no host or no user specified), this list is not used as
684 argument. By this, arguments like (\"-l\" \"%u\") are optional.
685 \"%t\" is replaced by the temporary file name produced with
686 `tramp-make-tramp-temp-file'. \"%k\" indicates the keep-date
687 parameter of a program, if exists.
689 When an asynchronous process is started, we know already that
690 the connection works. Therefore, we can pass additional
691 parameters to suppress diagnostic messages, in order not to
692 tamper the process output.
693 * `tramp-copy-program'
694 This specifies the name of the program to use for remotely copying
695 the file; this might be the absolute filename of rcp or the name of
698 This specifies the list of parameters to pass to the above mentioned
699 program, the hints for `tramp-login-args' also apply here.
700 * `tramp-copy-keep-date'
701 This specifies whether the copying program when the preserves the
702 timestamp of the original file.
703 * `tramp-copy-keep-tmpfile'
704 This specifies whether a temporary local file shall be kept
705 for optimization reasons (useful for \"rsync\" methods).
706 * `tramp-copy-recursive'
707 Whether the operation copies directories recursively.
708 * `tramp-default-port'
709 The default port of a method is needed in case of gateway connections.
710 Additionally, it is used as indication which method is prepared for
713 As the attribute name says, additional arguments are specified here
714 when a method is applied via a gateway.
715 * `tramp-password-end-of-line'
716 This specifies the string to use for terminating the line after
717 submitting the password. If this method parameter is nil, then the
718 value of the normal variable `tramp-default-password-end-of-line'
719 is used. This parameter is necessary because the \"plink\" program
720 requires any two characters after sending the password. These do
721 not have to be newline or carriage return characters. Other login
722 programs are happy with just one character, the newline character.
723 We use \"xy\" as the value for methods using \"plink\".
725 What does all this mean? Well, you should specify `tramp-login-program'
726 for all methods; this program is used to log in to the remote site. Then,
727 there are two ways to actually transfer the files between the local and the
728 remote side. One way is using an additional rcp-like program. If you want
729 to do this, set `tramp-copy-program' in the method.
731 Another possibility for file transfer is inline transfer, i.e. the
732 file is passed through the same buffer used by `tramp-login-program'. In
733 this case, the file contents need to be protected since the
734 `tramp-login-program' might use escape codes or the connection might not
735 be eight-bit clean. Therefore, file contents are encoded for transit.
736 See the variables `tramp-local-coding-commands' and
737 `tramp-remote-coding-commands' for details.
739 So, to summarize: if the method is an out-of-band method, then you
740 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
741 inline method, then these two parameters should be nil. Methods which
742 are fit for gateways must have `tramp-default-port' at least.
746 When using `su' or `sudo' the phrase `open connection to a remote
747 host' sounds strange, but it is used nevertheless, for consistency.
748 No connection is opened to a remote host, but `su' or `sudo' is
749 started on the local host. You should specify a remote host
750 `localhost' or the name of the local host. Another host name is
751 useful only in combination with `tramp-default-proxies-alist'.")
753 (defun tramp-detect-ssh-controlmaster ()
754 "Call ssh to detect whether it supports the ControlMaster argument.
755 This function may return nil when the argument is supported, but
756 shouldn't return t when it isn't."
759 (call-process "ssh" nil t nil
"-o" "ControlMaster")
760 (goto-char (point-min))
761 (search-forward-regexp "Missing ControlMaster argument" nil t
))))
763 (defcustom tramp-default-method
764 ;; An external copy method seems to be preferred, because it is much
765 ;; more performant for large files, and it hasn't too serious delays
766 ;; for small files. But it must be ensured that there aren't
767 ;; permanent password queries. Either a password agent like
768 ;; "ssh-agent" or "Pageant" shall run, or the optional
769 ;; password-cache.el or auth-sources.el packages shall be active for
770 ;; password caching. "scpc" is chosen if we detect that the user is
771 ;; running OpenSSH 4.0 or newer.
773 ;; PuTTY is installed.
774 ((executable-find "pscp")
775 (if (or (fboundp 'password-read
)
776 (fboundp 'auth-source-user-or-password
)
777 ;; Pageant is running.
778 (tramp-compat-process-running-p "Pageant"))
781 ;; There is an ssh installation.
782 ((executable-find "scp")
784 ((tramp-detect-ssh-controlmaster) "scpc")
785 ((or (fboundp 'password-read
)
786 (fboundp 'auth-source-user-or-password
)
787 ;; ssh-agent is running.
788 (getenv "SSH_AUTH_SOCK")
789 (getenv "SSH_AGENT_PID"))
794 "*Default method to use for transferring files.
795 See `tramp-methods' for possibilities.
796 Also see `tramp-default-method-alist'."
800 (defcustom tramp-default-method-alist
801 '(("\\`localhost\\'" "\\`root\\'" "su"))
802 "*Default method to use for specific host/user pairs.
803 This is an alist of items (HOST USER METHOD). The first matching item
804 specifies the method to use for a file name which does not specify a
805 method. HOST and USER are regular expressions or nil, which is
806 interpreted as a regular expression which always matches. If no entry
807 matches, the variable `tramp-default-method' takes effect.
809 If the file name does not specify the user, lookup is done using the
810 empty string for the user name.
812 See `tramp-methods' for a list of possibilities for METHOD."
814 :type
'(repeat (list (regexp :tag
"Host regexp")
815 (regexp :tag
"User regexp")
816 (string :tag
"Method"))))
818 (defcustom tramp-default-user
820 "*Default user to use for transferring files.
821 It is nil by default; otherwise settings in configuration files like
822 \"~/.ssh/config\" would be overwritten. Also see `tramp-default-user-alist'.
824 This variable is regarded as obsolete, and will be removed soon."
826 :type
'(choice (const nil
) string
))
828 (defcustom tramp-default-user-alist
829 `(("\\`su\\(do\\)?\\'" nil
"root")
830 ("\\`r\\(em\\)?\\(cp\\|sh\\)\\|telnet\\|plink1?\\'"
831 nil
,(user-login-name)))
832 "*Default user to use for specific method/host pairs.
833 This is an alist of items (METHOD HOST USER). The first matching item
834 specifies the user to use for a file name which does not specify a
835 user. METHOD and USER are regular expressions or nil, which is
836 interpreted as a regular expression which always matches. If no entry
837 matches, the variable `tramp-default-user' takes effect.
839 If the file name does not specify the method, lookup is done using the
840 empty string for the method name."
842 :type
'(repeat (list (regexp :tag
"Method regexp")
843 (regexp :tag
"Host regexp")
844 (string :tag
"User"))))
846 (defcustom tramp-default-host
848 "*Default host to use for transferring files.
849 Useful for su and sudo methods mostly."
853 (defcustom tramp-default-proxies-alist nil
854 "*Route to be followed for specific host/user pairs.
855 This is an alist of items (HOST USER PROXY). The first matching
856 item specifies the proxy to be passed for a file name located on
857 a remote target matching USER@HOST. HOST and USER are regular
858 expressions. PROXY must be a Tramp filename without a localname
859 part. Method and user name on PROXY are optional, which is
860 interpreted with the default values. PROXY can contain the
861 patterns %h and %u, which are replaced by the strings matching
862 HOST or USER, respectively.
864 HOST, USER or PROXY could also be Lisp forms, which will be
865 evaluated. The result must be a string or nil, which is
866 interpreted as a regular expression which always matches."
868 :type
'(repeat (list (choice :tag
"Host regexp" regexp sexp
)
869 (choice :tag
"User regexp" regexp sexp
)
870 (choice :tag
"Proxy remote name" string
(const nil
)))))
872 (defconst tramp-local-host-regexp
874 "^" (regexp-opt (list "localhost" (system-name) "127\.0\.0\.1" "::1") t
) "$")
875 "*Host names which are regarded as local host.")
877 (defconst tramp-completion-function-alist-rsh
878 '((tramp-parse-rhosts "/etc/hosts.equiv")
879 (tramp-parse-rhosts "~/.rhosts"))
880 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
882 (defconst tramp-completion-function-alist-ssh
883 '((tramp-parse-rhosts "/etc/hosts.equiv")
884 (tramp-parse-rhosts "/etc/shosts.equiv")
885 (tramp-parse-shosts "/etc/ssh_known_hosts")
886 (tramp-parse-sconfig "/etc/ssh_config")
887 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
888 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
889 (tramp-parse-rhosts "~/.rhosts")
890 (tramp-parse-rhosts "~/.shosts")
891 (tramp-parse-shosts "~/.ssh/known_hosts")
892 (tramp-parse-sconfig "~/.ssh/config")
893 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
894 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
895 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
897 (defconst tramp-completion-function-alist-telnet
898 '((tramp-parse-hosts "/etc/hosts"))
899 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
901 (defconst tramp-completion-function-alist-su
902 '((tramp-parse-passwd "/etc/passwd"))
903 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
905 (defconst tramp-completion-function-alist-putty
907 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"))
908 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty methods.")
910 (defvar tramp-completion-function-alist nil
911 "*Alist of methods for remote files.
912 This is a list of entries of the form \(NAME PAIR1 PAIR2 ...\).
913 Each NAME stands for a remote access method. Each PAIR is of the form
914 \(FUNCTION FILE\). FUNCTION is responsible to extract user names and host
915 names from FILE for completion. The following predefined FUNCTIONs exists:
917 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
918 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
919 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
920 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
921 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
922 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
923 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
924 * `tramp-parse-netrc' for \"~/.netrc\" like files.
925 * `tramp-parse-putty' for PuTTY registry keys.
927 FUNCTION can also be a customer defined function. For more details see
930 (eval-after-load "tramp"
932 (tramp-set-completion-function
933 "rcp" tramp-completion-function-alist-rsh
)
934 (tramp-set-completion-function
935 "scp" tramp-completion-function-alist-ssh
)
936 (tramp-set-completion-function
937 "scp1" tramp-completion-function-alist-ssh
)
938 (tramp-set-completion-function
939 "scp2" tramp-completion-function-alist-ssh
)
940 (tramp-set-completion-function
941 "scp1_old" tramp-completion-function-alist-ssh
)
942 (tramp-set-completion-function
943 "scp2_old" tramp-completion-function-alist-ssh
)
944 (tramp-set-completion-function
945 "rsync" tramp-completion-function-alist-ssh
)
946 (tramp-set-completion-function
947 "rsyncc" tramp-completion-function-alist-ssh
)
948 (tramp-set-completion-function
949 "remcp" tramp-completion-function-alist-rsh
)
950 (tramp-set-completion-function
951 "rsh" tramp-completion-function-alist-rsh
)
952 (tramp-set-completion-function
953 "ssh" tramp-completion-function-alist-ssh
)
954 (tramp-set-completion-function
955 "ssh1" tramp-completion-function-alist-ssh
)
956 (tramp-set-completion-function
957 "ssh2" tramp-completion-function-alist-ssh
)
958 (tramp-set-completion-function
959 "ssh1_old" tramp-completion-function-alist-ssh
)
960 (tramp-set-completion-function
961 "ssh2_old" tramp-completion-function-alist-ssh
)
962 (tramp-set-completion-function
963 "remsh" tramp-completion-function-alist-rsh
)
964 (tramp-set-completion-function
965 "telnet" tramp-completion-function-alist-telnet
)
966 (tramp-set-completion-function
967 "su" tramp-completion-function-alist-su
)
968 (tramp-set-completion-function
969 "sudo" tramp-completion-function-alist-su
)
970 (tramp-set-completion-function
971 "scpx" tramp-completion-function-alist-ssh
)
972 (tramp-set-completion-function
973 "sshx" tramp-completion-function-alist-ssh
)
974 (tramp-set-completion-function
975 "krlogin" tramp-completion-function-alist-rsh
)
976 (tramp-set-completion-function
977 "plink" tramp-completion-function-alist-ssh
)
978 (tramp-set-completion-function
979 "plink1" tramp-completion-function-alist-ssh
)
980 (tramp-set-completion-function
981 "plinkx" tramp-completion-function-alist-putty
)
982 (tramp-set-completion-function
983 "pscp" tramp-completion-function-alist-ssh
)
984 (tramp-set-completion-function
985 "fcp" tramp-completion-function-alist-ssh
)))
987 (defconst tramp-echo-mark-marker
"_echo"
988 "String marker to surround echoed commands.")
990 (defconst tramp-echo-mark-marker-length
(length tramp-echo-mark-marker
)
991 "String length of `tramp-echo-mark-marker'.")
993 (defconst tramp-echo-mark
994 (concat tramp-echo-mark-marker
995 (make-string tramp-echo-mark-marker-length ?
\b))
996 "String mark to be transmitted around shell commands.
997 Used to separate their echo from the output they produce. This
998 will only be used if we cannot disable remote echo via stty.
999 This string must have no effect on the remote shell except for
1000 producing some echo which can later be detected by
1001 `tramp-echoed-echo-mark-regexp'. Using `tramp-echo-mark-marker',
1002 followed by an equal number of backspaces to erase them will
1005 (defconst tramp-echoed-echo-mark-regexp
1006 (format "%s\\(\b\\( \b\\)?\\)\\{%d\\}"
1007 tramp-echo-mark-marker tramp-echo-mark-marker-length
)
1008 "Regexp which matches `tramp-echo-mark' as it gets echoed by
1011 (defcustom tramp-rsh-end-of-line
"\n"
1012 "*String used for end of line in rsh connections.
1013 I don't think this ever needs to be changed, so please tell me about it
1014 if you need to change this.
1015 Also see the method parameter `tramp-password-end-of-line' and the normal
1016 variable `tramp-default-password-end-of-line'."
1020 (defcustom tramp-default-password-end-of-line
1021 tramp-rsh-end-of-line
1022 "*String used for end of line after sending a password.
1023 This variable provides the default value for the method parameter
1024 `tramp-password-end-of-line', see `tramp-methods' for more details.
1026 It seems that people using plink under Windows need to send
1027 \"\\r\\n\" (carriage-return, then newline) after a password, but just
1028 \"\\n\" after all other lines. This variable can be used for the
1029 password, see `tramp-rsh-end-of-line' for the other cases.
1031 The default value is to use the same value as `tramp-rsh-end-of-line'."
1035 ;; "getconf PATH" yields:
1036 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
1037 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
1038 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
1039 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
1041 (defcustom tramp-remote-path
1042 '(tramp-default-remote-path "/usr/sbin" "/usr/local/bin"
1043 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
1044 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
1045 "*List of directories to search for executables on remote host.
1046 For every remote host, this variable will be set buffer local,
1047 keeping the list of existing directories on that host.
1049 You can use `~' in this list, but when searching for a shell which groks
1050 tilde expansion, all directory names starting with `~' will be ignored.
1052 `Default Directories' represent the list of directories given by
1053 the command \"getconf PATH\". It is recommended to use this
1054 entry on top of this list, because these are the default
1055 directories for POSIX compatible commands.
1057 `Private Directories' are the settings of the $PATH environment,
1058 as given in your `~/.profile'."
1060 :type
'(repeat (choice
1061 (const :tag
"Default Directories" tramp-default-remote-path
)
1062 (const :tag
"Private Directories" tramp-own-remote-path
)
1063 (string :tag
"Directory"))))
1065 (defcustom tramp-remote-process-environment
1066 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "LC_ALL=C"
1067 ,(format "TERM=%s" tramp-terminal-type
)
1068 "EMACS=t" ;; Deprecated.
1069 ,(format "INSIDE_EMACS='%s,tramp:%s'" emacs-version tramp-version
)
1070 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH="
1071 "autocorrect=" "correct=")
1073 "*List of environment variables to be set on the remote host.
1075 Each element should be a string of the form ENVVARNAME=VALUE. An
1076 entry ENVVARNAME= diables the corresponding environment variable,
1077 which might have been set in the init files like ~/.profile.
1079 Special handling is applied to the PATH environment, which should
1080 not be set here. Instead of, it should be set via `tramp-remote-path'."
1082 :type
'(repeat string
))
1084 (defcustom tramp-login-prompt-regexp
1085 ".*ogin\\( .*\\)?: *"
1086 "*Regexp matching login-like prompts.
1087 The regexp should match at end of buffer.
1089 Sometimes the prompt is reported to look like \"login as:\"."
1093 (defcustom tramp-shell-prompt-pattern
1094 ;; Allow a prompt to start right after a ^M since it indeed would be
1095 ;; displayed at the beginning of the line (and Zsh uses it). This
1096 ;; regexp works only for GNU Emacs.
1097 (concat (if (featurep 'xemacs
) "" "\\(?:^\\|\r\\)")
1098 "[^#$%>\n]*#?[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*")
1099 "Regexp to match prompts from remote shell.
1100 Normally, Tramp expects you to configure `shell-prompt-pattern'
1101 correctly, but sometimes it happens that you are connecting to a
1102 remote host which sends a different kind of shell prompt. Therefore,
1103 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
1104 and also things matched by this variable. The default value of this
1105 variable is similar to the default value of `shell-prompt-pattern',
1106 which should work well in many cases.
1108 This regexp must match both `tramp-initial-end-of-output' and
1109 `tramp-end-of-output'."
1113 (defcustom tramp-password-prompt-regexp
1114 "^.*\\([pP]assword\\|[pP]assphrase\\).*:\^@? *"
1115 "*Regexp matching password-like prompts.
1116 The regexp should match at end of buffer.
1118 The `sudo' program appears to insert a `^@' character into the prompt."
1122 (defcustom tramp-wrong-passwd-regexp
1124 ;; These strings should be on the last line
1125 (regexp-opt '("Permission denied"
1128 "Connection refused"
1130 "Timeout, server not responding."
1132 "Name or service not known"
1133 "Host key verification failed."
1134 "No supported authentication methods left to try!") t
)
1138 ;; Here comes a list of regexes, separated by \\|
1139 "Received signal [0-9]+"
1141 "*Regexp matching a `login failed' message.
1142 The regexp should match at end of buffer."
1146 (defcustom tramp-yesno-prompt-regexp
1148 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t
)
1150 "Regular expression matching all yes/no queries which need to be confirmed.
1151 The confirmation should be done with yes or no.
1152 The regexp should match at end of buffer.
1153 See also `tramp-yn-prompt-regexp'."
1157 (defcustom tramp-yn-prompt-regexp
1159 (regexp-opt '("Store key in cache? (y/n)"
1160 "Update cached key? (y/n, Return cancels connection)") t
)
1162 "Regular expression matching all y/n queries which need to be confirmed.
1163 The confirmation should be done with y or n.
1164 The regexp should match at end of buffer.
1165 See also `tramp-yesno-prompt-regexp'."
1169 (defcustom tramp-terminal-prompt-regexp
1173 "Terminal type\\? \\[.*\\]"
1175 "Regular expression matching all terminal setting prompts.
1176 The regexp should match at end of buffer.
1177 The answer will be provided by `tramp-action-terminal', which see."
1181 (defcustom tramp-operation-not-permitted-regexp
1182 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
1183 (regexp-opt '("Operation not permitted") t
))
1184 "Regular expression matching keep-date problems in (s)cp operations.
1185 Copying has been performed successfully already, so this message can
1190 (defcustom tramp-copy-failed-regexp
1192 (regexp-opt '("Permission denied"
1193 "not a regular file"
1195 "No such file or directory") t
)
1197 "Regular expression matching copy problems in (s)cp operations."
1201 (defcustom tramp-process-alive-regexp
1203 "Regular expression indicating a process has finished.
1204 In fact this expression is empty by intention, it will be used only to
1205 check regularly the status of the associated process.
1206 The answer will be provided by `tramp-action-process-alive',
1207 `tramp-action-out-of-band', which see."
1211 (defcustom tramp-temp-name-prefix
"tramp."
1212 "*Prefix to use for temporary files.
1213 If this is a relative file name (such as \"tramp.\"), it is considered
1214 relative to the directory name returned by the function
1215 `tramp-compat-temporary-file-directory' (which see). It may also be an
1216 absolute file name; don't forget to include a prefix for the filename
1221 (defconst tramp-temp-buffer-name
" *tramp temp*"
1222 "Buffer name for a temporary buffer.
1223 It shall be used in combination with `generate-new-buffer-name'.")
1225 (defvar tramp-temp-buffer-file-name nil
1226 "File name of a persistent local temporary file.
1227 Useful for \"rsync\" like methods.")
1228 (make-variable-buffer-local 'tramp-temp-buffer-file-name
)
1230 (defcustom tramp-sh-extra-args
'(("/bash\\'" .
"-norc -noprofile"))
1231 "*Alist specifying extra arguments to pass to the remote shell.
1232 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
1233 matching the shell file name and ARGS is a string specifying the
1236 This variable is only used when Tramp needs to start up another shell
1237 for tilde expansion. The extra arguments should typically prevent the
1238 shell from reading its init file."
1240 ;; This might be the wrong way to test whether the widget type
1241 ;; `alist' is available. Who knows the right way to test it?
1242 :type
(if (get 'alist
'widget-type
)
1243 '(alist :key-type string
:value-type string
)
1244 '(repeat (cons string string
))))
1246 ;; XEmacs is distributed with few Lisp packages. Further packages are
1247 ;; installed using EFS. If we use a unified filename format, then
1248 ;; Tramp is required in addition to EFS. (But why can't Tramp just
1249 ;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
1250 ;; just like before.) Another reason for using a separate filename
1251 ;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
1252 ;; Tramp only knows how to deal with `file-name-handler-alist', not
1253 ;; the other places.
1255 ;; Currently, we have the choice between 'ftp, 'sep, and 'url.
1257 (defcustom tramp-syntax
1258 (if (featurep 'xemacs
) 'sep
'ftp
)
1259 "Tramp filename syntax to be used.
1261 It can have the following values:
1263 'ftp -- Ange-FTP respective EFS like syntax (GNU Emacs default)
1264 'sep -- Syntax as defined for XEmacs (not available yet for GNU Emacs)
1265 'url -- URL-like syntax."
1267 :type
(if (featurep 'xemacs
)
1268 '(choice (const :tag
"EFS" ftp
)
1269 (const :tag
"XEmacs" sep
)
1270 (const :tag
"URL" url
))
1271 '(choice (const :tag
"Ange-FTP" ftp
)
1272 (const :tag
"URL" url
))))
1274 (defconst tramp-prefix-format
1275 (cond ((equal tramp-syntax
'ftp
) "/")
1276 ((equal tramp-syntax
'sep
) "/[")
1277 ((equal tramp-syntax
'url
) "/")
1278 (t (error "Wrong `tramp-syntax' defined")))
1279 "*String matching the very beginning of Tramp file names.
1280 Used in `tramp-make-tramp-file-name'.")
1282 (defconst tramp-prefix-regexp
1283 (concat "^" (regexp-quote tramp-prefix-format
))
1284 "*Regexp matching the very beginning of Tramp file names.
1285 Should always start with \"^\". Derived from `tramp-prefix-format'.")
1287 (defconst tramp-method-regexp
1289 "*Regexp matching methods identifiers.")
1291 (defconst tramp-postfix-method-format
1292 (cond ((equal tramp-syntax
'ftp
) ":")
1293 ((equal tramp-syntax
'sep
) "/")
1294 ((equal tramp-syntax
'url
) "://")
1295 (t (error "Wrong `tramp-syntax' defined")))
1296 "*String matching delimeter between method and user or host names.
1297 Used in `tramp-make-tramp-file-name'.")
1299 (defconst tramp-postfix-method-regexp
1300 (regexp-quote tramp-postfix-method-format
)
1301 "*Regexp matching delimeter between method and user or host names.
1302 Derived from `tramp-postfix-method-format'.")
1304 (defconst tramp-user-regexp
1306 "*Regexp matching user names.")
1308 (defconst tramp-prefix-domain-format
"%"
1309 "*String matching delimeter between user and domain names.")
1311 (defconst tramp-prefix-domain-regexp
1312 (regexp-quote tramp-prefix-domain-format
)
1313 "*Regexp matching delimeter between user and domain names.
1314 Derived from `tramp-prefix-domain-format'.")
1316 (defconst tramp-domain-regexp
1318 "*Regexp matching domain names.")
1320 (defconst tramp-user-with-domain-regexp
1321 (concat "\\(" tramp-user-regexp
"\\)"
1322 tramp-prefix-domain-regexp
1323 "\\(" tramp-domain-regexp
"\\)")
1324 "*Regexp matching user names with domain names.")
1326 (defconst tramp-postfix-user-format
1328 "*String matching delimeter between user and host names.
1329 Used in `tramp-make-tramp-file-name'.")
1331 (defconst tramp-postfix-user-regexp
1332 (regexp-quote tramp-postfix-user-format
)
1333 "*Regexp matching delimeter between user and host names.
1334 Derived from `tramp-postfix-user-format'.")
1336 (defconst tramp-host-regexp
1338 "*Regexp matching host names.")
1340 (defconst tramp-prefix-ipv6-format
1341 (cond ((equal tramp-syntax
'ftp
) "[")
1342 ((equal tramp-syntax
'sep
) "")
1343 ((equal tramp-syntax
'url
) "[")
1344 (t (error "Wrong `tramp-syntax' defined")))
1345 "*String matching left hand side of IPv6 addresses.
1346 Used in `tramp-make-tramp-file-name'.")
1348 (defconst tramp-prefix-ipv6-regexp
1349 (regexp-quote tramp-prefix-ipv6-format
)
1350 "*Regexp matching left hand side of IPv6 addresses.
1351 Derived from `tramp-prefix-ipv6-format'.")
1353 ;; The following regexp is a bit sloppy. But it shall serve our
1354 ;; purposes. It covers also IPv4 mapped IPv6 addresses, like in
1355 ;; "::ffff:192.168.0.1".
1356 (defconst tramp-ipv6-regexp
1357 "\\(?:\\(?:[a-zA-Z0-9]+\\)?:\\)+[a-zA-Z0-9.]+"
1358 "*Regexp matching IPv6 addresses.")
1360 (defconst tramp-postfix-ipv6-format
1361 (cond ((equal tramp-syntax
'ftp
) "]")
1362 ((equal tramp-syntax
'sep
) "")
1363 ((equal tramp-syntax
'url
) "]")
1364 (t (error "Wrong `tramp-syntax' defined")))
1365 "*String matching right hand side of IPv6 addresses.
1366 Used in `tramp-make-tramp-file-name'.")
1368 (defconst tramp-postfix-ipv6-regexp
1369 (regexp-quote tramp-postfix-ipv6-format
)
1370 "*Regexp matching right hand side of IPv6 addresses.
1371 Derived from `tramp-postfix-ipv6-format'.")
1373 (defconst tramp-prefix-port-format
1374 (cond ((equal tramp-syntax
'ftp
) "#")
1375 ((equal tramp-syntax
'sep
) "#")
1376 ((equal tramp-syntax
'url
) ":")
1377 (t (error "Wrong `tramp-syntax' defined")))
1378 "*String matching delimeter between host names and port numbers.")
1380 (defconst tramp-prefix-port-regexp
1381 (regexp-quote tramp-prefix-port-format
)
1382 "*Regexp matching delimeter between host names and port numbers.
1383 Derived from `tramp-prefix-port-format'.")
1385 (defconst tramp-port-regexp
1387 "*Regexp matching port numbers.")
1389 (defconst tramp-host-with-port-regexp
1390 (concat "\\(" tramp-host-regexp
"\\)"
1391 tramp-prefix-port-regexp
1392 "\\(" tramp-port-regexp
"\\)")
1393 "*Regexp matching host names with port numbers.")
1395 (defconst tramp-postfix-host-format
1396 (cond ((equal tramp-syntax
'ftp
) ":")
1397 ((equal tramp-syntax
'sep
) "]")
1398 ((equal tramp-syntax
'url
) "")
1399 (t (error "Wrong `tramp-syntax' defined")))
1400 "*String matching delimeter between host names and localnames.
1401 Used in `tramp-make-tramp-file-name'.")
1403 (defconst tramp-postfix-host-regexp
1404 (regexp-quote tramp-postfix-host-format
)
1405 "*Regexp matching delimeter between host names and localnames.
1406 Derived from `tramp-postfix-host-format'.")
1408 (defconst tramp-localname-regexp
1410 "*Regexp matching localnames.")
1412 ;; File name format.
1414 (defconst tramp-file-name-structure
1418 "\\(" "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
"\\)?"
1419 "\\(" "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
"\\)?"
1420 "\\(" "\\(" tramp-host-regexp
1422 tramp-prefix-ipv6-regexp tramp-ipv6-regexp
1423 tramp-postfix-ipv6-regexp
"\\)"
1424 "\\(" tramp-prefix-port-regexp tramp-port-regexp
"\\)?" "\\)?"
1425 tramp-postfix-host-regexp
1426 "\\(" tramp-localname-regexp
"\\)")
1429 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1430 the Tramp file name structure.
1432 The first element REGEXP is a regular expression matching a Tramp file
1433 name. The regex should contain parentheses around the method name,
1434 the user name, the host name, and the file name parts.
1436 The second element METHOD is a number, saying which pair of
1437 parentheses matches the method name. The third element USER is
1438 similar, but for the user name. The fourth element HOST is similar,
1439 but for the host name. The fifth element FILE is for the file name.
1440 These numbers are passed directly to `match-string', which see. That
1441 means the opening parentheses are counted to identify the pair.
1443 See also `tramp-file-name-regexp'.")
1446 (defconst tramp-file-name-regexp-unified
1447 "\\`/\\([^[/:]+\\|[^/]+]\\):"
1448 "Value for `tramp-file-name-regexp' for unified remoting.
1449 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1450 Tramp. See `tramp-file-name-structure' for more explanations.")
1453 (defconst tramp-file-name-regexp-separate
1455 "Value for `tramp-file-name-regexp' for separate remoting.
1456 XEmacs uses a separate filename syntax for Tramp and EFS.
1457 See `tramp-file-name-structure' for more explanations.")
1460 (defconst tramp-file-name-regexp-url
1462 "Value for `tramp-file-name-regexp' for URL-like remoting.
1463 See `tramp-file-name-structure' for more explanations.")
1466 (defconst tramp-file-name-regexp
1467 (cond ((equal tramp-syntax
'ftp
) tramp-file-name-regexp-unified
)
1468 ((equal tramp-syntax
'sep
) tramp-file-name-regexp-separate
)
1469 ((equal tramp-syntax
'url
) tramp-file-name-regexp-url
)
1470 (t (error "Wrong `tramp-syntax' defined")))
1471 "*Regular expression matching file names handled by Tramp.
1472 This regexp should match Tramp file names but no other file names.
1473 When tramp.el is loaded, this regular expression is prepended to
1474 `file-name-handler-alist', and that is searched sequentially. Thus,
1475 if the Tramp entry appears rather early in the `file-name-handler-alist'
1476 and is a bit too general, then some files might be considered Tramp
1477 files which are not really Tramp files.
1479 Please note that the entry in `file-name-handler-alist' is made when
1480 this file \(tramp.el\) is loaded. This means that this variable must be set
1481 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1482 updated after changing this variable.
1484 Also see `tramp-file-name-structure'.")
1487 (defconst tramp-root-regexp
1488 (if (memq system-type
'(cygwin windows-nt
))
1489 "\\`\\([a-zA-Z]:\\)?/"
1491 "Beginning of an incomplete Tramp file name.
1492 Usually, it is just \"\\\\`/\". On W32 systems, there might be a
1493 volume letter, which will be removed by `tramp-drop-volume-letter'.")
1496 (defconst tramp-completion-file-name-regexp-unified
1497 (concat tramp-root-regexp
"[^/]*\\'")
1498 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1499 GNU Emacs uses a unified filename syntax for Tramp and Ange-FTP.
1500 See `tramp-file-name-structure' for more explanations.")
1503 (defconst tramp-completion-file-name-regexp-separate
1504 (concat tramp-root-regexp
"\\([[][^]]*\\)?\\'")
1505 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1506 XEmacs uses a separate filename syntax for Tramp and EFS.
1507 See `tramp-file-name-structure' for more explanations.")
1510 (defconst tramp-completion-file-name-regexp-url
1511 (concat tramp-root-regexp
"[^/:]+\\(:\\(/\\(/[^/]*\\)?\\)?\\)?\\'")
1512 "Value for `tramp-completion-file-name-regexp' for URL-like remoting.
1513 See `tramp-file-name-structure' for more explanations.")
1516 (defconst tramp-completion-file-name-regexp
1517 (cond ((equal tramp-syntax
'ftp
) tramp-completion-file-name-regexp-unified
)
1518 ((equal tramp-syntax
'sep
) tramp-completion-file-name-regexp-separate
)
1519 ((equal tramp-syntax
'url
) tramp-completion-file-name-regexp-url
)
1520 (t (error "Wrong `tramp-syntax' defined")))
1521 "*Regular expression matching file names handled by Tramp completion.
1522 This regexp should match partial Tramp file names only.
1524 Please note that the entry in `file-name-handler-alist' is made when
1525 this file (tramp.el) is loaded. This means that this variable must be set
1526 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1527 updated after changing this variable.
1529 Also see `tramp-file-name-structure'.")
1531 (defconst tramp-actions-before-shell
1532 '((tramp-login-prompt-regexp tramp-action-login
)
1533 (tramp-password-prompt-regexp tramp-action-password
)
1534 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
1535 (shell-prompt-pattern tramp-action-succeed
)
1536 (tramp-shell-prompt-pattern tramp-action-succeed
)
1537 (tramp-yesno-prompt-regexp tramp-action-yesno
)
1538 (tramp-yn-prompt-regexp tramp-action-yn
)
1539 (tramp-terminal-prompt-regexp tramp-action-terminal
)
1540 (tramp-process-alive-regexp tramp-action-process-alive
))
1541 "List of pattern/action pairs.
1542 Whenever a pattern matches, the corresponding action is performed.
1543 Each item looks like (PATTERN ACTION).
1545 The PATTERN should be a symbol, a variable. The value of this
1546 variable gives the regular expression to search for. Note that the
1547 regexp must match at the end of the buffer, \"\\'\" is implicitly
1550 The ACTION should also be a symbol, but a function. When the
1551 corresponding PATTERN matches, the ACTION function is called.")
1553 (defconst tramp-actions-copy-out-of-band
1554 '((tramp-password-prompt-regexp tramp-action-password
)
1555 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
1556 (tramp-copy-failed-regexp tramp-action-permission-denied
)
1557 (tramp-process-alive-regexp tramp-action-out-of-band
))
1558 "List of pattern/action pairs.
1559 This list is used for copying/renaming with out-of-band methods.
1561 See `tramp-actions-before-shell' for more info.")
1563 ;; Chunked sending kludge. We set this to 500 for black-listed constellations
1564 ;; known to have a bug in `process-send-string'; some ssh connections appear
1565 ;; to drop bytes when data is sent too quickly. There is also a connection
1566 ;; buffer local variable, which is computed depending on remote host properties
1567 ;; when `tramp-chunksize' is zero or nil.
1568 (defcustom tramp-chunksize
1569 (when (and (not (featurep 'xemacs
))
1570 (memq system-type
'(hpux)))
1572 ;; Parentheses in docstring starting at beginning of line are escaped.
1573 ;; Fontification is messed up when
1574 ;; `open-paren-in-column-0-is-defun-start' set to t.
1575 "*If non-nil, chunksize for sending input to local process.
1576 It is necessary only on systems which have a buggy `process-send-string'
1577 implementation. The necessity, whether this variable must be set, can be
1578 checked via the following code:
1581 (let* ((user \"xxx\") (host \"yyy\")
1583 (sent init) (received init))
1584 (while (= sent received)
1585 (setq sent (+ sent step))
1587 (let ((proc (start-process (buffer-name) (current-buffer)
1588 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1589 (when (memq (process-status proc) '(run open))
1590 (process-send-string proc (make-string sent ?\\ ))
1591 (process-send-eof proc)
1592 (process-send-eof proc))
1593 (while (not (progn (goto-char (point-min))
1594 (re-search-forward \"\\\\w+\" (point-max) t)))
1595 (accept-process-output proc 1))
1596 (when (memq (process-status proc) '(run open))
1597 (setq received (string-to-number (match-string 0)))
1598 (delete-process proc)
1599 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1601 (if (> sent (+ init step))
1602 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1604 (message \"Test does not work\")
1605 (display-buffer (current-buffer))
1608 In the Emacs normally running Tramp, evaluate the above code
1609 \(replace \"xxx\" and \"yyy\" by the remote user and host name,
1610 respectively\). You can do this, for example, by pasting it into
1611 the `*scratch*' buffer and then hitting C-j with the cursor after the
1612 last closing parenthesis. Note that it works only if you have configured
1613 \"ssh\" to run without password query, see ssh-agent\(1\).
1615 You will see the number of bytes sent successfully to the remote host.
1616 If that number exceeds 1000, you can stop the execution by hitting
1617 C-g, because your Emacs is likely clean.
1619 When it is necessary to set `tramp-chunksize', you might consider to
1620 use an out-of-the-band method \(like \"scp\"\) instead of an internal one
1621 \(like \"ssh\"\), because setting `tramp-chunksize' to non-nil decreases
1624 If your Emacs is buggy, the code stops and gives you an indication
1625 about the value `tramp-chunksize' should be set. Maybe you could just
1626 experiment a bit, e.g. changing the values of `init' and `step'
1627 in the third line of the code.
1629 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1630 this variable to be set as well."
1632 :type
'(choice (const nil
) integer
))
1634 ;; Logging in to a remote host normally requires obtaining a pty. But
1635 ;; Emacs on MacOS X has process-connection-type set to nil by default,
1636 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1637 ;; for an override of the system default.
1638 (defcustom tramp-process-connection-type t
1639 "Overrides `process-connection-type' for connections from Tramp.
1640 Tramp binds process-connection-type to the value given here before
1641 opening a connection to a remote host."
1643 :type
'(choice (const nil
) (const t
) (const pty
)))
1645 (defcustom tramp-completion-reread-directory-timeout
10
1646 "Defines seconds since last remote command before rereading a directory.
1647 A remote directory might have changed its contents. In order to
1648 make it visible during file name completion in the minibuffer,
1649 Tramp flushes its cache and rereads the directory contents when
1650 more than `tramp-completion-reread-directory-timeout' seconds
1651 have been gone since last remote command execution. A value of 0
1652 would require an immediate reread during filename completion, nil
1653 means to use always cached values for the directory contents."
1655 :type
'(choice (const nil
) integer
))
1657 ;;; Internal Variables:
1659 (defvar tramp-current-method nil
1660 "Connection method for this *tramp* buffer.")
1662 (defvar tramp-current-user nil
1663 "Remote login name for this *tramp* buffer.")
1665 (defvar tramp-current-host nil
1666 "Remote host for this *tramp* buffer.")
1668 (defconst tramp-uudecode
1669 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1671 rm -f /tmp/tramp.$$"
1672 "Shell function to implement `uudecode' to standard output.
1673 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
1674 for this or `uudecode -p', but some systems don't, and for them
1675 we have this shell function.")
1677 (defconst tramp-perl-file-truename
1680 use Cwd \"realpath\";
1683 my ($volume, @dirs) = @_;
1684 my $real = realpath(File::Spec->catpath(
1685 $volume, File::Spec->catdir(@dirs), \"\"));
1687 my ($vol, $dir) = File::Spec->splitpath($real, 1);
1688 return ($vol, File::Spec->splitdir($dir));
1691 my $last = pop(@dirs);
1692 ($volume, @dirs) = recursive($volume, @dirs);
1694 return ($volume, @dirs);
1698 $result = realpath($ARGV[0]);
1700 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
1701 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
1703 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
1706 if ($ARGV[0] =~ /\\/$/) {
1707 $result = $result . \"/\";
1710 print \"\\\"$result\\\"\\n\";
1711 ' \"$1\" 2>/dev/null"
1712 "Perl script to produce output suitable for use with `file-truename'
1713 on the remote file system.
1714 Escape sequence %s is replaced with name of Perl binary.
1715 This string is passed to `format', so percent characters need to be doubled.")
1717 (defconst tramp-perl-file-name-all-completions
1727 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
1728 @files = readdir(d); closedir(d);
1729 foreach $f (@files) {
1730 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
1731 if (-d \"$ARGV[0]/$f\") {
1740 ' \"$1\" \"$2\" \"$3\" 2>/dev/null"
1741 "Perl script to produce output suitable for use with
1742 `file-name-all-completions' on the remote file system. Escape
1743 sequence %s is replaced with name of Perl binary. This string is
1744 passed to `format', so percent characters need to be doubled.")
1746 ;; Perl script to implement `file-attributes' in a Lisp `read'able
1747 ;; output. If you are hacking on this, note that you get *no* output
1748 ;; unless this spits out a complete line, including the '\n' at the
1750 ;; The device number is returned as "-1", because there will be a virtual
1751 ;; device number set in `tramp-handle-file-attributes'.
1752 (defconst tramp-perl-file-attributes
1754 @stat = lstat($ARGV[0]);
1759 if (($stat[2] & 0170000) == 0120000)
1761 $type = readlink($ARGV[0]);
1762 $type = \"\\\"$type\\\"\";
1764 elsif (($stat[2] & 0170000) == 040000)
1772 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1773 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1775 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
1780 $stat[8] >> 16 & 0xffff,
1782 $stat[9] >> 16 & 0xffff,
1784 $stat[10] >> 16 & 0xffff,
1788 $stat[1] >> 16 & 0xffff,
1790 );' \"$1\" \"$2\" 2>/dev/null"
1791 "Perl script to produce output suitable for use with `file-attributes'
1792 on the remote file system.
1793 Escape sequence %s is replaced with name of Perl binary.
1794 This string is passed to `format', so percent characters need to be doubled.")
1796 (defconst tramp-perl-directory-files-and-attributes
1798 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
1799 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
1800 @list = readdir(DIR);
1804 for($i = 0; $i < $n; $i++)
1806 $filename = $list[$i];
1807 @stat = lstat($filename);
1808 if (($stat[2] & 0170000) == 0120000)
1810 $type = readlink($filename);
1811 $type = \"\\\"$type\\\"\";
1813 elsif (($stat[2] & 0170000) == 040000)
1821 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1822 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1824 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
1830 $stat[8] >> 16 & 0xffff,
1832 $stat[9] >> 16 & 0xffff,
1834 $stat[10] >> 16 & 0xffff,
1838 $stat[1] >> 16 & 0xffff,
1840 $stat[0] >> 16 & 0xffff,
1843 printf(\")\\n\");' \"$1\" \"$2\" 2>/dev/null"
1844 "Perl script implementing `directory-files-attributes' as Lisp `read'able
1846 Escape sequence %s is replaced with name of Perl binary.
1847 This string is passed to `format', so percent characters need to be doubled.")
1849 ;; ;; These two use uu encoding.
1850 ;; (defvar tramp-perl-encode "%s -e'\
1851 ;; print qq(begin 644 xxx\n);
1854 ;; while (read(STDIN, $s, 45)) {
1855 ;; print pack(q(u), $s);
1860 ;; "Perl program to use for encoding a file.
1861 ;; Escape sequence %s is replaced with name of Perl binary.")
1863 ;; (defvar tramp-perl-decode "%s -ne '
1864 ;; print unpack q(u), $_;
1866 ;; "Perl program to use for decoding a file.
1867 ;; Escape sequence %s is replaced with name of Perl binary.")
1869 ;; These two use base64 encoding.
1870 (defconst tramp-perl-encode-with-module
1871 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
1872 "Perl program to use for encoding a file.
1873 Escape sequence %s is replaced with name of Perl binary.
1874 This string is passed to `format', so percent characters need to be doubled.
1875 This implementation requires the MIME::Base64 Perl module to be installed
1876 on the remote host.")
1878 (defconst tramp-perl-decode-with-module
1879 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
1880 "Perl program to use for decoding a file.
1881 Escape sequence %s is replaced with name of Perl binary.
1882 This string is passed to `format', so percent characters need to be doubled.
1883 This implementation requires the MIME::Base64 Perl module to be installed
1884 on the remote host.")
1886 (defconst tramp-perl-encode
1888 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1889 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
1890 # Free Software Foundation, Inc.
1895 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1896 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1901 # We read in chunks of 54 bytes, to generate output lines
1902 # of 72 chars (plus end of line)
1905 while (my $data = <STDIN>) {
1908 # Only for the last chunk, and only if did not fill the last three-byte packet
1910 my $mod = length($data) %% 3;
1911 $pad = q(=) x (3 - $mod) if $mod;
1914 # Not the fastest method, but it is simple: unpack to binary string, split
1915 # by groups of 6 bits and convert back from binary to byte; then map into
1916 # the translation table
1920 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1924 "Perl program to use for encoding a file.
1925 Escape sequence %s is replaced with name of Perl binary.
1926 This string is passed to `format', so percent characters need to be doubled.")
1928 (defconst tramp-perl-decode
1930 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1931 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
1932 # Free Software Foundation, Inc.
1937 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1938 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1941 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1945 # We are going to accumulate into $pending to accept any line length
1946 # (we do not check they are <= 76 chars as the RFC says)
1949 while (my $data = <STDIN>) {
1952 # If we find one or two =, we have reached the end and
1953 # any following data is to be discarded
1954 my $finished = $data =~ s/(==?).*/$1/;
1957 my $len = length($pending);
1958 my $chunk = substr($pending, 0, $len & ~3);
1959 $pending = substr($pending, $len & ~3 + 1);
1961 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1962 # split in 8-bit chunks and convert back to char.
1965 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1969 "Perl program to use for decoding a file.
1970 Escape sequence %s is replaced with name of Perl binary.
1971 This string is passed to `format', so percent characters need to be doubled.")
1973 (defconst tramp-vc-registered-read-file-names
1975 for file in \"$@\"; do
1977 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
1979 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
1982 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
1984 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
1988 "Script to check existence of VC related files.
1989 It must be send formatted with two strings; the tests for file
1990 existence, and file readability.")
1992 (defconst tramp-file-mode-type-map
1993 '((0 .
"-") ; Normal file (SVID-v2 and XPG2)
1995 (2 .
"c") ; character device
1996 (3 .
"m") ; multiplexed character device (v7)
1997 (4 .
"d") ; directory
1998 (5 .
"?") ; Named special file (XENIX)
1999 (6 .
"b") ; block device
2000 (7 .
"?") ; multiplexed block device (v7)
2001 (8 .
"-") ; regular file
2002 (9 .
"n") ; network special file (HP-UX)
2003 (10 .
"l") ; symlink
2004 (11 .
"?") ; ACL shadow inode (Solaris, not userspace)
2006 (13 .
"D") ; door special (Solaris)
2007 (14 .
"w")) ; whiteout (BSD)
2008 "A list of file types returned from the `stat' system call.
2009 This is used to map a mode number to a permission string.")
2011 ;; New handlers should be added here. The following operations can be
2012 ;; handled using the normal primitives: file-name-sans-versions,
2014 (defconst tramp-file-name-handler-alist
2015 '((load . tramp-handle-load
)
2016 (make-symbolic-link . tramp-handle-make-symbolic-link
)
2017 (file-name-as-directory . tramp-handle-file-name-as-directory
)
2018 (file-name-directory . tramp-handle-file-name-directory
)
2019 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
2020 (file-truename . tramp-handle-file-truename
)
2021 (file-exists-p . tramp-handle-file-exists-p
)
2022 (file-directory-p . tramp-handle-file-directory-p
)
2023 (file-executable-p . tramp-handle-file-executable-p
)
2024 (file-readable-p . tramp-handle-file-readable-p
)
2025 (file-regular-p . tramp-handle-file-regular-p
)
2026 (file-symlink-p . tramp-handle-file-symlink-p
)
2027 (file-writable-p . tramp-handle-file-writable-p
)
2028 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p
)
2029 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p
)
2030 (file-attributes . tramp-handle-file-attributes
)
2031 (file-modes . tramp-handle-file-modes
)
2032 (directory-files . tramp-handle-directory-files
)
2033 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes
)
2034 (file-name-all-completions . tramp-handle-file-name-all-completions
)
2035 (file-name-completion . tramp-handle-file-name-completion
)
2036 (add-name-to-file . tramp-handle-add-name-to-file
)
2037 (copy-file . tramp-handle-copy-file
)
2038 (copy-directory . tramp-handle-copy-directory
)
2039 (rename-file . tramp-handle-rename-file
)
2040 (set-file-modes . tramp-handle-set-file-modes
)
2041 (set-file-times . tramp-handle-set-file-times
)
2042 (make-directory . tramp-handle-make-directory
)
2043 (delete-directory . tramp-handle-delete-directory
)
2044 (delete-file . tramp-handle-delete-file
)
2045 (directory-file-name . tramp-handle-directory-file-name
)
2046 ;; `executable-find' is not official yet.
2047 (executable-find . tramp-handle-executable-find
)
2048 (start-file-process . tramp-handle-start-file-process
)
2049 (process-file . tramp-handle-process-file
)
2050 (shell-command . tramp-handle-shell-command
)
2051 (insert-directory . tramp-handle-insert-directory
)
2052 (expand-file-name . tramp-handle-expand-file-name
)
2053 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
2054 (file-local-copy . tramp-handle-file-local-copy
)
2055 (file-remote-p . tramp-handle-file-remote-p
)
2056 (insert-file-contents . tramp-handle-insert-file-contents
)
2057 (insert-file-contents-literally
2058 . tramp-handle-insert-file-contents-literally
)
2059 (write-region . tramp-handle-write-region
)
2060 (find-backup-file-name . tramp-handle-find-backup-file-name
)
2061 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name
)
2062 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
2063 (dired-compress-file . tramp-handle-dired-compress-file
)
2064 (dired-recursive-delete-directory
2065 . tramp-handle-dired-recursive-delete-directory
)
2066 (dired-uncache . tramp-handle-dired-uncache
)
2067 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime
)
2068 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime
)
2069 (file-selinux-context . tramp-handle-file-selinux-context
)
2070 (set-file-selinux-context . tramp-handle-set-file-selinux-context
)
2071 (vc-registered . tramp-handle-vc-registered
))
2072 "Alist of handler functions.
2073 Operations not mentioned here will be handled by the normal Emacs functions.")
2075 ;; Handlers for partial Tramp file names. For Emacs just
2076 ;; `file-name-all-completions' is needed.
2078 (defconst tramp-completion-file-name-handler-alist
2079 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions
)
2080 (file-name-completion . tramp-completion-handle-file-name-completion
))
2081 "Alist of completion handler functions.
2082 Used for file names matching `tramp-file-name-regexp'. Operations not
2083 mentioned here will be handled by `tramp-file-name-handler-alist' or the
2084 normal Emacs functions.")
2086 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
2087 (defvar tramp-foreign-file-name-handler-alist
2088 ;; (identity . tramp-sh-file-name-handler) should always be the last
2089 ;; entry, because `identity' always matches.
2090 '((identity . tramp-sh-file-name-handler
))
2091 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
2092 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
2095 ;;; Internal functions which must come first:
2097 (defsubst tramp-debug-message
(vec fmt-string
&rest args
)
2098 "Append message to debug buffer.
2099 Message is formatted with FMT-STRING as control string and the remaining
2100 ARGS to actually emit the message (if applicable)."
2101 (when (get-buffer (tramp-buffer-name vec
))
2102 (with-current-buffer (tramp-get-debug-buffer vec
)
2103 (goto-char (point-max))
2108 ";; %sEmacs: %s Tramp: %s -*- mode: outline; -*-"
2109 (if (featurep 'sxemacs
) "SX" (if (featurep 'xemacs
) "X" "GNU "))
2110 emacs-version tramp-version
)))
2114 (let ((now (current-time)))
2115 (insert (format-time-string "%T." now
))
2116 (insert (format "%06d " (nth 2 now
))))
2117 ;; Calling function.
2118 (let ((btn 1) btf fn
)
2120 (setq btf
(nth 1 (backtrace-frame btn
)))
2124 (setq fn
(symbol-name btf
))
2125 (unless (and (string-match "^tramp" fn
)
2127 "^tramp\\(-debug\\)?\\(-message\\|-error\\|-compat-funcall\\)$"
2130 (setq btn
(1+ btn
))))
2131 ;; The following code inserts filename and line number.
2132 ;; Should be deactivated by default, because it is time
2134 ; (let ((ffn (find-function-noselect (intern fn))))
2138 ; (file-name-nondirectory (buffer-file-name (car ffn)))
2139 ; (with-current-buffer (car ffn)
2140 ; (1+ (count-lines (point-min) (cdr ffn)))))))
2141 (insert (format "%s " fn
)))
2143 (insert (apply 'format fmt-string args
)))))
2145 (defvar tramp-message-show-message t
2146 "Show Tramp message in the minibuffer.
2147 This variable is used to disable messages from `tramp-error'.
2148 The messages are visible anyway, because an error is raised.")
2150 (defsubst tramp-message
(vec-or-proc level fmt-string
&rest args
)
2151 "Emit a message depending on verbosity level.
2152 VEC-OR-PROC identifies the Tramp buffer to use. It can be either a
2153 vector or a process. LEVEL says to be quiet if `tramp-verbose' is
2154 less than LEVEL. The message is emitted only if `tramp-verbose' is
2155 greater than or equal to LEVEL.
2157 The message is also logged into the debug buffer when `tramp-verbose'
2158 is greater than or equal 4.
2160 Calls functions `message' and `tramp-debug-message' with FMT-STRING as
2161 control string and the remaining ARGS to actually emit the message (if
2164 (when (<= level tramp-verbose
)
2165 ;; Match data must be preserved!
2167 ;; Display only when there is a minimum level.
2168 (when (and tramp-message-show-message
(<= level
3))
2174 ((= level
2) "Warning: ")
2178 ;; Log only when there is a minimum level.
2179 (when (>= tramp-verbose
4)
2180 (when (and vec-or-proc
2181 (processp vec-or-proc
)
2182 (buffer-name (process-buffer vec-or-proc
)))
2183 (with-current-buffer (process-buffer vec-or-proc
)
2184 ;; Translate proc to vec.
2185 (setq vec-or-proc
(tramp-dissect-file-name default-directory
))))
2186 (when (and vec-or-proc
(vectorp vec-or-proc
))
2187 (apply 'tramp-debug-message
2189 (concat (format "(%d) # " level
) fmt-string
)
2191 ;; Suppress all errors.
2194 (defsubst tramp-error
(vec-or-proc signal fmt-string
&rest args
)
2196 VEC-OR-PROC identifies the connection to use, SIGNAL is the
2197 signal identifier to be raised, remaining args passed to
2198 `tramp-message'. Finally, signal SIGNAL is raised."
2199 (let (tramp-message-show-message)
2202 (error-message-string
2204 (get signal
'error-message
)
2205 (apply 'format fmt-string args
))))
2206 (signal signal
(list (apply 'format fmt-string args
)))))
2208 (defsubst tramp-error-with-buffer
2209 (buffer vec-or-proc signal fmt-string
&rest args
)
2210 "Emit an error, and show BUFFER.
2211 If BUFFER is nil, show the connection buffer. Wait for 30\", or until
2212 an input event arrives. The other arguments are passed to `tramp-error'."
2213 (save-window-excursion
2215 (apply 'tramp-error vec-or-proc signal fmt-string args
)
2216 (when (and vec-or-proc
2217 (not (zerop tramp-verbose
))
2218 (not (tramp-completion-mode-p)))
2219 (let ((enable-recursive-minibuffers t
))
2221 (or (and (bufferp buffer
) buffer
)
2222 (and (processp vec-or-proc
) (process-buffer vec-or-proc
))
2223 (tramp-get-buffer vec-or-proc
)))
2226 (defmacro with-parsed-tramp-file-name
(filename var
&rest body
)
2227 "Parse a Tramp filename and make components available in the body.
2229 First arg FILENAME is evaluated and dissected into its components.
2230 Second arg VAR is a symbol. It is used as a variable name to hold
2231 the filename structure. It is also used as a prefix for the variables
2232 holding the components. For example, if VAR is the symbol `foo', then
2233 `foo' will be bound to the whole structure, `foo-method' will be bound to
2234 the method component, and so on for `foo-user', `foo-host', `foo-localname'.
2236 Remaining args are Lisp expressions to be evaluated (inside an implicit
2239 If VAR is nil, then we bind `v' to the structure and `method', `user',
2240 `host', `localname' to the components."
2241 `(let* ((,(or var
'v
) (tramp-dissect-file-name ,filename
))
2242 (,(if var
(intern (concat (symbol-name var
) "-method")) 'method
)
2243 (tramp-file-name-method ,(or var
'v
)))
2244 (,(if var
(intern (concat (symbol-name var
) "-user")) 'user
)
2245 (tramp-file-name-user ,(or var
'v
)))
2246 (,(if var
(intern (concat (symbol-name var
) "-host")) 'host
)
2247 (tramp-file-name-host ,(or var
'v
)))
2248 (,(if var
(intern (concat (symbol-name var
) "-localname")) 'localname
)
2249 (tramp-file-name-localname ,(or var
'v
))))
2252 (put 'with-parsed-tramp-file-name
'lisp-indent-function
2)
2253 (put 'with-parsed-tramp-file-name
'edebug-form-spec
'(form symbolp body
))
2254 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<with-parsed-tramp-file-name\\>"))
2256 (defmacro with-file-property
(vec file property
&rest body
)
2257 "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
2258 FILE must be a local file name on a connection identified via VEC."
2259 `(if (file-name-absolute-p ,file
)
2260 (let ((value (tramp-get-file-property ,vec
,file
,property
'undef
)))
2261 (when (eq value
'undef
)
2262 ;; We cannot pass @body as parameter to
2263 ;; `tramp-set-file-property' because it mangles our
2265 (setq value
(progn ,@body
))
2266 (tramp-set-file-property ,vec
,file
,property value
))
2270 (put 'with-file-property
'lisp-indent-function
3)
2271 (put 'with-file-property
'edebug-form-spec t
)
2272 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<with-file-property\\>"))
2274 (defmacro with-connection-property
(key property
&rest body
)
2275 "Check in Tramp for property PROPERTY, otherwise executes BODY and set."
2276 `(let ((value (tramp-get-connection-property ,key
,property
'undef
)))
2277 (when (eq value
'undef
)
2278 ;; We cannot pass ,@body as parameter to
2279 ;; `tramp-set-connection-property' because it mangles our debug
2281 (setq value
(progn ,@body
))
2282 (tramp-set-connection-property ,key
,property value
))
2285 (put 'with-connection-property
'lisp-indent-function
2)
2286 (put 'with-connection-property
'edebug-form-spec t
)
2287 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<with-connection-property\\>"))
2289 (defun tramp-progress-reporter-update (reporter &optional value
)
2290 (let* ((parameters (cdr reporter
))
2291 (message (aref parameters
3)))
2292 (when (string-match message
(or (current-message) ""))
2293 (funcall 'progress-reporter-update reporter value
))))
2295 (defmacro with-progress-reporter
(vec level message
&rest body
)
2296 "Executes BODY, spinning a progress reporter with MESSAGE.
2297 If LEVEL does not fit for visible messages, or if this is a
2298 nested call of the macro, there are only traces without a visible
2301 (tramp-message ,vec
,level
"%s..." ,message
)
2302 ;; We start a pulsing progress reporter after 3 seconds. Feature
2303 ;; introduced in Emacs 24.1.
2304 (when (and tramp-message-show-message
2305 ;; Display only when there is a minimum level.
2306 (<= ,level
(min tramp-verbose
3)))
2308 (setq pr
(tramp-compat-funcall 'make-progress-reporter
,message
)
2310 (run-at-time 3 0.1 'tramp-progress-reporter-update pr
)))
2313 ;; Execute the body. Unset `tramp-message-show-message' when
2314 ;; the timer object is created, in order to suppress
2315 ;; concurrent timers.
2316 (let ((tramp-message-show-message
2317 (and tramp-message-show-message
(not tm
))))
2319 ;; Stop progress reporter.
2320 (if tm
(tramp-compat-funcall 'cancel-timer tm
))
2321 (tramp-message ,vec
,level
"%s...done" ,message
))))
2323 (put 'with-progress-reporter
'lisp-indent-function
3)
2324 (put 'with-progress-reporter
'edebug-form-spec t
)
2325 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<with-progress-reporter\\>"))
2327 (eval-and-compile ;; Silence compiler.
2328 (if (memq system-type
'(cygwin windows-nt
))
2329 (defun tramp-drop-volume-letter (name)
2330 "Cut off unnecessary drive letter from file NAME.
2331 The function `tramp-handle-expand-file-name' calls `expand-file-name'
2332 locally on a remote file name. When the local system is a W32 system
2333 but the remote system is Unix, this introduces a superfluous drive
2334 letter into the file name. This function removes it."
2336 (if (string-match tramp-root-regexp name
)
2337 (replace-match "/" nil t name
)
2340 (defalias 'tramp-drop-volume-letter
'identity
)))
2342 (defsubst tramp-make-tramp-temp-file
(vec)
2343 "Create a temporary file on the remote host identified by VEC.
2344 Return the local name of the temporary file."
2346 (tramp-make-tramp-file-name
2347 (tramp-file-name-method vec
)
2348 (tramp-file-name-user vec
)
2349 (tramp-file-name-host vec
)
2350 (tramp-drop-volume-letter
2352 tramp-temp-name-prefix
(tramp-get-remote-tmpdir vec
)))))
2355 ;; `make-temp-file' would be the natural choice for
2356 ;; implementation. But it calls `write-region' internally,
2357 ;; which also needs a temporary file - we would end in an
2359 (setq result
(make-temp-name prefix
))
2360 (if (file-exists-p result
)
2362 ;; This creates the file by side effect.
2363 (set-file-times result
)
2364 (set-file-modes result
(tramp-octal-to-decimal "0700"))))
2366 ;; Return the local part.
2367 (with-parsed-tramp-file-name result nil localname
)))
2370 ;;; Config Manipulation Functions:
2372 (defun tramp-set-completion-function (method function-list
)
2373 "Sets the list of completion functions for METHOD.
2374 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2375 The FUNCTION is intended to parse FILE according its syntax.
2376 It might be a predefined FUNCTION, or a user defined FUNCTION.
2377 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
2378 `tramp-parse-sconfig', `tramp-parse-hosts', `tramp-parse-passwd',
2379 and `tramp-parse-netrc'.
2383 (tramp-set-completion-function
2385 '((tramp-parse-sconfig \"/etc/ssh_config\")
2386 (tramp-parse-sconfig \"~/.ssh/config\")))"
2388 (let ((r function-list
)
2390 (setq tramp-completion-function-alist
2391 (delete (assoc method tramp-completion-function-alist
)
2392 tramp-completion-function-alist
))
2395 ;; Remove double entries.
2396 (when (member (car v
) (cdr v
))
2397 (setcdr v
(delete (car v
) (cdr v
))))
2398 ;; Check for function and file or registry key.
2399 (unless (and (functionp (nth 0 (car v
)))
2400 (if (string-match "^HKEY_CURRENT_USER" (nth 1 (car v
)))
2401 ;; Windows registry.
2402 (and (memq system-type
'(cygwin windows-nt
))
2404 (tramp-local-call-process
2405 "reg" nil nil nil
"query" (nth 1 (car v
)))))
2406 ;; Configuration file.
2407 (file-exists-p (nth 1 (car v
)))))
2408 (setq r
(delete (car v
) r
)))
2412 (add-to-list 'tramp-completion-function-alist
2415 (defun tramp-get-completion-function (method)
2416 "Returns a list of completion functions for METHOD.
2417 For definition of that list see `tramp-set-completion-function'."
2419 ;; Hosts visited once shall be remembered.
2420 `(tramp-parse-connection-properties ,method
)
2421 ;; The method related defaults.
2422 (cdr (assoc method tramp-completion-function-alist
))))
2425 ;;; Fontification of `read-file-name':
2427 ;; rfn-eshadow.el is part of Emacs 22. It is autoloaded.
2428 (defvar tramp-rfn-eshadow-overlay
)
2429 (make-variable-buffer-local 'tramp-rfn-eshadow-overlay
)
2431 (defun tramp-rfn-eshadow-setup-minibuffer ()
2432 "Set up a minibuffer for `file-name-shadow-mode'.
2433 Adds another overlay hiding filename parts according to Tramp's
2434 special handling of `substitute-in-file-name'."
2435 (when (symbol-value 'minibuffer-completing-file-name
)
2436 (setq tramp-rfn-eshadow-overlay
2437 (tramp-compat-funcall
2439 (tramp-compat-funcall 'minibuffer-prompt-end
)
2440 (tramp-compat-funcall 'minibuffer-prompt-end
)))
2441 ;; Copy rfn-eshadow-overlay properties.
2442 (let ((props (tramp-compat-funcall
2443 'overlay-properties
(symbol-value 'rfn-eshadow-overlay
))))
2445 (tramp-compat-funcall
2446 'overlay-put tramp-rfn-eshadow-overlay
(pop props
) (pop props
))))))
2448 (when (boundp 'rfn-eshadow-setup-minibuffer-hook
)
2449 (add-hook 'rfn-eshadow-setup-minibuffer-hook
2450 'tramp-rfn-eshadow-setup-minibuffer
)
2451 (add-hook 'tramp-unload-hook
2453 (remove-hook 'rfn-eshadow-setup-minibuffer-hook
2454 'tramp-rfn-eshadow-setup-minibuffer
))))
2456 (defconst tramp-rfn-eshadow-update-overlay-regexp
2457 (format "[^%s/~]*\\(/\\|~\\)" tramp-postfix-host-format
))
2459 (defun tramp-rfn-eshadow-update-overlay ()
2460 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
2461 This is intended to be used as a minibuffer `post-command-hook' for
2462 `file-name-shadow-mode'; the minibuffer should have already
2463 been set up by `rfn-eshadow-setup-minibuffer'."
2464 ;; In remote files name, there is a shadowing just for the local part.
2465 (let ((end (or (tramp-compat-funcall
2466 'overlay-end
(symbol-value 'rfn-eshadow-overlay
))
2467 (tramp-compat-funcall 'minibuffer-prompt-end
))))
2470 (tramp-compat-funcall 'buffer-substring-no-properties end
(point-max)))
2474 (1+ (or (string-match
2475 tramp-rfn-eshadow-update-overlay-regexp
(buffer-string) end
)
2478 (let ((rfn-eshadow-overlay tramp-rfn-eshadow-overlay
)
2479 (rfn-eshadow-update-overlay-hook nil
))
2480 (tramp-compat-funcall
2481 'move-overlay rfn-eshadow-overlay
(point-max) (point-max))
2482 (tramp-compat-funcall 'rfn-eshadow-update-overlay
)))))))
2484 (when (boundp 'rfn-eshadow-update-overlay-hook
)
2485 (add-hook 'rfn-eshadow-update-overlay-hook
2486 'tramp-rfn-eshadow-update-overlay
)
2487 (add-hook 'tramp-unload-hook
2489 (remove-hook 'rfn-eshadow-update-overlay-hook
2490 'tramp-rfn-eshadow-update-overlay
))))
2493 ;;; Integration of eshell.el:
2496 (defvar eshell-path-env
))
2498 ;; eshell.el keeps the path in `eshell-path-env'. We must change it
2499 ;; when `default-directory' points to another host.
2500 (defun tramp-eshell-directory-change ()
2501 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
2502 (setq eshell-path-env
2503 (if (file-remote-p default-directory
)
2504 (with-parsed-tramp-file-name default-directory nil
2507 (tramp-get-remote-path v
)
2511 (eval-after-load "esh-util"
2513 (tramp-eshell-directory-change)
2514 (add-hook 'eshell-directory-change-hook
2515 'tramp-eshell-directory-change
)
2516 (add-hook 'tramp-unload-hook
2518 (remove-hook 'eshell-directory-change-hook
2519 'tramp-eshell-directory-change
)))))
2522 ;;; File Name Handler Functions:
2524 (defun tramp-handle-make-symbolic-link
2525 (filename linkname
&optional ok-if-already-exists
)
2526 "Like `make-symbolic-link' for Tramp files.
2527 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
2528 the symlink. If LINKNAME is a Tramp file, only the localname component is
2529 used as the target of the symlink.
2531 If LINKNAME is a Tramp file and the localname component is relative, then
2532 it is expanded first, before the localname component is taken. Note that
2533 this can give surprising results if the user/host for the source and
2534 target of the symlink differ."
2535 (with-parsed-tramp-file-name linkname l
2536 (let ((ln (tramp-get-remote-ln l
))
2537 (cwd (tramp-run-real-handler
2538 'file-name-directory
(list l-localname
))))
2542 "Making a symbolic link. ln(1) does not exist on the remote host."))
2544 ;; Do the 'confirm if exists' thing.
2545 (when (file-exists-p linkname
)
2547 (if (or (null ok-if-already-exists
) ; not allowed to exist
2548 (and (numberp ok-if-already-exists
)
2551 "File %s already exists; make it a link anyway? "
2554 l
'file-already-exists
"File %s already exists" l-localname
)
2555 (delete-file linkname
)))
2557 ;; If FILENAME is a Tramp name, use just the localname component.
2558 (when (tramp-tramp-file-p filename
)
2560 (tramp-file-name-localname
2561 (tramp-dissect-file-name (expand-file-name filename
)))))
2563 ;; Right, they are on the same host, regardless of user, method, etc.
2564 ;; We now make the link on the remote machine. This will occur as the user
2565 ;; that FILENAME belongs to.
2567 (tramp-send-command-and-check
2570 "cd %s && %s -sf %s %s"
2571 (tramp-shell-quote-argument cwd
)
2573 (tramp-shell-quote-argument filename
)
2574 (tramp-shell-quote-argument l-localname
))
2577 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix
)
2578 "Like `load' for Tramp files."
2579 (with-parsed-tramp-file-name (expand-file-name file
) nil
2581 (cond ((file-exists-p (concat file
".elc"))
2582 (setq file
(concat file
".elc")))
2583 ((file-exists-p (concat file
".el"))
2584 (setq file
(concat file
".el")))))
2586 ;; The first condition is always true for absolute file names.
2587 ;; Included for safety's sake.
2588 (unless (or (file-name-directory file
)
2589 (string-match "\\.elc?\\'" file
))
2592 "File `%s' does not include a `.el' or `.elc' suffix" file
)))
2594 (when (not (file-exists-p file
))
2595 (tramp-error v
'file-error
"Cannot load nonexistent file `%s'" file
)))
2596 (if (not (file-exists-p file
))
2598 (let ((tramp-message-show-message (not nomessage
)))
2599 (with-progress-reporter v
0 (format "Loading %s" file
)
2600 (let ((local-copy (file-local-copy file
)))
2601 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2603 (load local-copy noerror t t
)
2604 (delete-file local-copy
)))))
2607 ;; Localname manipulation functions that grok Tramp localnames...
2608 (defun tramp-handle-file-name-as-directory (file)
2609 "Like `file-name-as-directory' but aware of Tramp files."
2610 ;; `file-name-as-directory' would be sufficient except localname is
2611 ;; the empty string.
2612 (let ((v (tramp-dissect-file-name file t
)))
2613 ;; Run the command on the localname portion only.
2614 (tramp-make-tramp-file-name
2615 (tramp-file-name-method v
)
2616 (tramp-file-name-user v
)
2617 (tramp-file-name-host v
)
2618 (tramp-run-real-handler
2619 'file-name-as-directory
(list (or (tramp-file-name-localname v
) ""))))))
2621 (defun tramp-handle-file-name-directory (file)
2622 "Like `file-name-directory' but aware of Tramp files."
2623 ;; Everything except the last filename thing is the directory. We
2624 ;; cannot apply `with-parsed-tramp-file-name', because this expands
2625 ;; the remote file name parts. This is a problem when we are in
2626 ;; file name completion.
2627 (let ((v (tramp-dissect-file-name file t
)))
2628 ;; Run the command on the localname portion only.
2629 (tramp-make-tramp-file-name
2630 (tramp-file-name-method v
)
2631 (tramp-file-name-user v
)
2632 (tramp-file-name-host v
)
2633 (tramp-run-real-handler
2634 'file-name-directory
(list (or (tramp-file-name-localname v
) ""))))))
2636 (defun tramp-handle-file-name-nondirectory (file)
2637 "Like `file-name-nondirectory' but aware of Tramp files."
2638 (with-parsed-tramp-file-name file nil
2639 (tramp-run-real-handler 'file-name-nondirectory
(list localname
))))
2641 (defun tramp-handle-file-truename (filename &optional counter prev-dirs
)
2642 "Like `file-truename' for Tramp files."
2643 (with-parsed-tramp-file-name (expand-file-name filename
) nil
2644 (with-file-property v localname
"file-truename"
2645 (let ((result nil
)) ; result steps in reverse order
2646 (tramp-message v
4 "Finding true name for `%s'" filename
)
2648 ;; Use GNU readlink --canonicalize-missing where available.
2649 ((tramp-get-remote-readlink v
)
2651 (tramp-send-command-and-read
2653 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
2654 (tramp-get-remote-readlink v
)
2655 (tramp-shell-quote-argument localname
)))))
2657 ;; Use Perl implementation.
2658 ((and (tramp-get-remote-perl v
)
2659 (tramp-get-connection-property v
"perl-file-spec" nil
)
2660 (tramp-get-connection-property v
"perl-cwd-realpath" nil
))
2661 (tramp-maybe-send-script
2662 v tramp-perl-file-truename
"tramp_perl_file_truename")
2664 (tramp-send-command-and-read
2666 (format "tramp_perl_file_truename %s"
2667 (tramp-shell-quote-argument localname
)))))
2669 ;; Do it yourself. We bind `directory-sep-char' here for
2670 ;; XEmacs on Windows, which would otherwise use backslash.
2671 (t (let* ((directory-sep-char ?
/)
2672 (steps (tramp-compat-split-string localname
"/"))
2673 (localnamedir (tramp-run-real-handler
2674 'file-name-as-directory
(list localname
)))
2675 (is-dir (string= localname localnamedir
))
2678 ;; Don't make the following value larger than
2679 ;; necessary. People expect an error message in a
2680 ;; timely fashion when something is wrong;
2681 ;; otherwise they might think that Emacs is hung.
2682 ;; Of course, correctness has to come first.
2685 (while (and steps
(< numchase numchase-limit
))
2686 (setq thisstep
(pop steps
))
2689 (mapconcat 'identity
2690 (append '("") (reverse result
) (list thisstep
))
2692 (setq symlink-target
2693 (nth 0 (file-attributes
2694 (tramp-make-tramp-file-name
2696 (mapconcat 'identity
2701 (cond ((string= "." thisstep
)
2702 (tramp-message v
5 "Ignoring step `.'"))
2703 ((string= ".." thisstep
)
2704 (tramp-message v
5 "Processing step `..'")
2706 ((stringp symlink-target
)
2707 ;; It's a symlink, follow it.
2708 (tramp-message v
5 "Follow symlink to %s" symlink-target
)
2709 (setq numchase
(1+ numchase
))
2710 (when (file-name-absolute-p symlink-target
)
2712 ;; If the symlink was absolute, we'll get a string like
2713 ;; "/user@host:/some/target"; extract the
2714 ;; "/some/target" part from it.
2715 (when (tramp-tramp-file-p symlink-target
)
2716 (unless (tramp-equal-remote filename symlink-target
)
2719 "Symlink target `%s' on wrong host" symlink-target
))
2720 (setq symlink-target localname
))
2722 (append (tramp-compat-split-string
2727 (setq result
(cons thisstep result
)))))
2728 (when (>= numchase numchase-limit
)
2731 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
2732 (setq result
(reverse result
))
2733 ;; Combine list to form string.
2736 (mapconcat 'identity
(cons "" result
) "/")
2738 (when (and is-dir
(or (string= "" result
)
2739 (not (string= (substring result -
1) "/"))))
2740 (setq result
(concat result
"/"))))))
2742 (tramp-message v
4 "True name of `%s' is `%s'" filename result
)
2743 (tramp-make-tramp-file-name method user host result
)))))
2747 (defun tramp-handle-file-exists-p (filename)
2748 "Like `file-exists-p' for Tramp files."
2749 (with-parsed-tramp-file-name filename nil
2750 (with-file-property v localname
"file-exists-p"
2751 (or (not (null (tramp-get-file-property
2752 v localname
"file-attributes-integer" nil
)))
2753 (not (null (tramp-get-file-property
2754 v localname
"file-attributes-string" nil
)))
2755 (zerop (tramp-send-command-and-check
2759 (tramp-get-file-exists-command v
)
2760 (tramp-shell-quote-argument localname
))))))))
2762 ;; Inodes don't exist for some file systems. Therefore we must
2763 ;; generate virtual ones. Used in `find-buffer-visiting'. The method
2764 ;; applied might be not so efficient (Ange-FTP uses hashes). But
2765 ;; performance isn't the major issue given that file transfer will
2767 (defvar tramp-inodes nil
2768 "Keeps virtual inodes numbers.")
2770 ;; Devices must distinguish physical file systems. The device numbers
2771 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2772 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2773 ;; EFS use device number "-1". In order to be different, we use device number
2774 ;; (-1 . x), whereby "x" is unique for a given (method user host).
2775 (defvar tramp-devices nil
2776 "Keeps virtual device numbers.")
2778 ;; CCC: This should check for an error condition and signal failure
2779 ;; when something goes wrong.
2780 ;; Daniel Pittman <daniel@danann.net>
2781 (defun tramp-handle-file-attributes (filename &optional id-format
)
2782 "Like `file-attributes' for Tramp files."
2783 (unless id-format
(setq id-format
'integer
))
2784 ;; Don't modify `last-coding-system-used' by accident.
2785 (let ((last-coding-system-used last-coding-system-used
))
2786 (with-parsed-tramp-file-name (expand-file-name filename
) nil
2787 (with-file-property v localname
(format "file-attributes-%s" id-format
)
2789 (tramp-convert-file-attributes
2792 ((tramp-get-remote-stat v
)
2793 (tramp-do-file-attributes-with-stat v localname id-format
))
2794 ((tramp-get-remote-perl v
)
2795 (tramp-do-file-attributes-with-perl v localname id-format
))
2797 (tramp-do-file-attributes-with-ls v localname id-format
)))))))))
2799 (defun tramp-do-file-attributes-with-ls (vec localname
&optional id-format
)
2800 "Implement `file-attributes' for Tramp files using the ls(1) command."
2802 res-inode res-filemodes res-numlinks
2803 res-uid res-gid res-size res-symlink-target
)
2804 (tramp-message vec
5 "file attributes with ls: %s" localname
)
2807 (format "(%s %s || %s -h %s) && %s %s %s"
2808 (tramp-get-file-exists-command vec
)
2809 (tramp-shell-quote-argument localname
)
2810 (tramp-get-test-command vec
)
2811 (tramp-shell-quote-argument localname
)
2812 (tramp-get-ls-command vec
)
2813 (if (eq id-format
'integer
) "-ildn" "-ild")
2814 (tramp-shell-quote-argument localname
)))
2815 ;; parse `ls -l' output ...
2816 (with-current-buffer (tramp-get-buffer vec
)
2817 (when (> (buffer-size) 0)
2818 (goto-char (point-min))
2822 (read (current-buffer))
2823 (invalid-read-syntax
2824 (when (and (equal (cadr err
)
2825 "Integer constant overflow in reader")
2827 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2829 (let* ((big (read (substring (car (cddr err
)) 0
2830 (match-beginning 1))))
2831 (small (read (match-string 1 (car (cddr err
)))))
2832 (twiddle (/ small
65536)))
2833 (cons (+ big twiddle
)
2834 (- small
(* twiddle
65536))))))))
2835 ;; ... file mode flags
2836 (setq res-filemodes
(symbol-name (read (current-buffer))))
2838 (setq res-numlinks
(read (current-buffer)))
2840 (setq res-uid
(read (current-buffer)))
2841 (setq res-gid
(read (current-buffer)))
2842 (if (eq id-format
'integer
)
2844 (unless (numberp res-uid
) (setq res-uid -
1))
2845 (unless (numberp res-gid
) (setq res-gid -
1)))
2847 (unless (stringp res-uid
) (setq res-uid
(symbol-name res-uid
)))
2848 (unless (stringp res-gid
) (setq res-gid
(symbol-name res-gid
)))))
2850 (setq res-size
(read (current-buffer)))
2851 ;; From the file modes, figure out other stuff.
2852 (setq symlinkp
(eq ?l
(aref res-filemodes
0)))
2853 (setq dirp
(eq ?d
(aref res-filemodes
0)))
2854 ;; if symlink, find out file name pointed to
2856 (search-forward "-> ")
2857 (setq res-symlink-target
2858 (buffer-substring (point) (tramp-compat-line-end-position))))
2859 ;; return data gathered
2861 ;; 0. t for directory, string (name linked to) for symbolic
2863 (or dirp res-symlink-target
)
2864 ;; 1. Number of links to file.
2870 ;; 4. Last access time, as a list of two integers. First
2871 ;; integer has high-order 16 bits of time, second has low 16
2873 ;; 5. Last modification time, likewise.
2874 ;; 6. Last status change time, likewise.
2875 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2876 ;; 7. Size in bytes (-1, if number is out of range).
2878 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2880 ;; 9. t if file's gid would change if file were deleted and
2881 ;; recreated. Will be set in `tramp-convert-file-attributes'
2883 ;; 10. inode number.
2885 ;; 11. Device number. Will be replaced by a virtual device number.
2889 (defun tramp-do-file-attributes-with-perl
2890 (vec localname
&optional id-format
)
2891 "Implement `file-attributes' for Tramp files using a Perl script."
2892 (tramp-message vec
5 "file attributes with perl: %s" localname
)
2893 (tramp-maybe-send-script
2894 vec tramp-perl-file-attributes
"tramp_perl_file_attributes")
2895 (tramp-send-command-and-read
2897 (format "tramp_perl_file_attributes %s %s"
2898 (tramp-shell-quote-argument localname
) id-format
)))
2900 (defun tramp-do-file-attributes-with-stat
2901 (vec localname
&optional id-format
)
2902 "Implement `file-attributes' for Tramp files using stat(1) command."
2903 (tramp-message vec
5 "file attributes with stat: %s" localname
)
2904 (tramp-send-command-and-read
2907 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
2908 ;; parse correctly the sequence "((". Therefore, we add a space.
2909 "( (%s %s || %s -h %s) && %s -c '( (\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)' %s || echo nil)"
2910 (tramp-get-file-exists-command vec
)
2911 (tramp-shell-quote-argument localname
)
2912 (tramp-get-test-command vec
)
2913 (tramp-shell-quote-argument localname
)
2914 (tramp-get-remote-stat vec
)
2915 (if (eq id-format
'integer
) "%u" "\"%U\"")
2916 (if (eq id-format
'integer
) "%g" "\"%G\"")
2917 (tramp-shell-quote-argument localname
))))
2919 (defun tramp-handle-set-visited-file-modtime (&optional time-list
)
2920 "Like `set-visited-file-modtime' for Tramp files."
2921 (unless (buffer-file-name)
2922 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2925 (tramp-run-real-handler 'set-visited-file-modtime
(list time-list
))
2926 (let ((f (buffer-file-name))
2928 (with-parsed-tramp-file-name f nil
2929 (let* ((attr (file-attributes f
))
2930 ;; '(-1 65535) means file doesn't exists yet.
2931 (modtime (or (nth 5 attr
) '(-1 65535))))
2932 (when (boundp 'last-coding-system-used
)
2933 (setq coding-system-used
(symbol-value 'last-coding-system-used
)))
2934 ;; We use '(0 0) as a don't-know value. See also
2935 ;; `tramp-do-file-attributes-with-ls'.
2936 (if (not (equal modtime
'(0 0)))
2937 (tramp-run-real-handler 'set-visited-file-modtime
(list modtime
))
2941 (format "%s -ild %s"
2942 (tramp-get-ls-command v
)
2943 (tramp-shell-quote-argument localname
)))
2944 (setq attr
(buffer-substring (point)
2945 (progn (end-of-line) (point)))))
2946 (tramp-set-file-property
2947 v localname
"visited-file-modtime-ild" attr
))
2948 (when (boundp 'last-coding-system-used
)
2949 (set 'last-coding-system-used coding-system-used
))
2952 ;; This function makes the same assumption as
2953 ;; `tramp-handle-set-visited-file-modtime'.
2954 (defun tramp-handle-verify-visited-file-modtime (buf)
2955 "Like `verify-visited-file-modtime' for Tramp files.
2956 At the time `verify-visited-file-modtime' calls this function, we
2957 already know that the buffer is visiting a file and that
2958 `visited-file-modtime' does not return 0. Do not call this
2959 function directly, unless those two cases are already taken care
2961 (with-current-buffer buf
2962 (let ((f (buffer-file-name)))
2963 ;; There is no file visiting the buffer, or the buffer has no
2964 ;; recorded last modification time, or there is no established
2967 (eq (visited-file-modtime) 0)
2968 (not (tramp-file-name-handler 'file-remote-p f nil
'connected
)))
2970 (with-parsed-tramp-file-name f nil
2971 (tramp-flush-file-property v localname
)
2972 (let* ((attr (file-attributes f
))
2973 (modtime (nth 5 attr
))
2974 (mt (visited-file-modtime)))
2977 ;; File exists, and has a known modtime.
2978 ((and attr
(not (equal modtime
'(0 0))))
2979 (< (abs (tramp-time-diff
2981 ;; For compatibility, deal with both the old
2982 ;; (HIGH . LOW) and the new (HIGH LOW) return
2983 ;; values of `visited-file-modtime'.
2985 (list (car mt
) (cdr mt
))
2988 ;; Modtime has the don't know value.
2992 (format "%s -ild %s"
2993 (tramp-get-ls-command v
)
2994 (tramp-shell-quote-argument localname
)))
2995 (with-current-buffer (tramp-get-buffer v
)
2996 (setq attr
(buffer-substring
2997 (point) (progn (end-of-line) (point)))))
3000 (tramp-get-file-property
3001 v localname
"visited-file-modtime-ild" "")))
3002 ;; If file does not exist, say it is not modified if and
3003 ;; only if that agrees with the buffer's record.
3004 (t (equal mt
'(-1 65535))))))))))
3006 (defun tramp-handle-set-file-modes (filename mode
)
3007 "Like `set-file-modes' for Tramp files."
3008 (with-parsed-tramp-file-name filename nil
3009 (tramp-flush-file-property v localname
)
3010 (unless (zerop (tramp-send-command-and-check
3012 (format "chmod %s %s"
3013 (tramp-decimal-to-octal mode
)
3014 (tramp-shell-quote-argument localname
))))
3015 ;; FIXME: extract the proper text from chmod's stderr.
3017 v
'file-error
"Error while changing file's mode %s" filename
))))
3019 (defun tramp-handle-set-file-times (filename &optional time
)
3020 "Like `set-file-times' for Tramp files."
3022 (if (file-remote-p filename
)
3023 (with-parsed-tramp-file-name filename nil
3024 (tramp-flush-file-property v localname
)
3025 (let ((time (if (or (null time
) (equal time
'(0 0)))
3028 ;; With GNU Emacs, `format-time-string' has an optional
3029 ;; parameter UNIVERSAL. This is preferred, because we
3030 ;; could handle the case when the remote host is
3031 ;; located in a different time zone as the local host.
3032 (utc (not (featurep 'xemacs
))))
3033 (tramp-send-command-and-check
3034 v
(format "%s touch -t %s %s"
3035 (if utc
"TZ=UTC; export TZ;" "")
3037 (format-time-string "%Y%m%d%H%M.%S" time t
)
3038 (format-time-string "%Y%m%d%H%M.%S" time
))
3039 (tramp-shell-quote-argument localname
)))))
3041 ;; We handle also the local part, because in older Emacsen,
3042 ;; without `set-file-times', this function is an alias for this.
3043 ;; We are local, so we don't need the UTC settings.
3044 (tramp-local-call-process
3045 "touch" nil nil nil
"-t"
3046 (format-time-string "%Y%m%d%H%M.%S" time
)
3047 (tramp-shell-quote-argument filename
)))))
3049 (defun tramp-set-file-uid-gid (filename &optional uid gid
)
3050 "Set the ownership for FILENAME.
3051 If UID and GID are provided, these values are used; otherwise uid
3052 and gid of the corresponding user is taken. Both parameters must be integers."
3053 ;; Modern Unices allow chown only for root. So we might need
3054 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
3055 ;; working with su(do)? when it is needed, so it shall succeed in
3056 ;; the majority of cases.
3057 ;; Don't modify `last-coding-system-used' by accident.
3058 (let ((last-coding-system-used last-coding-system-used
))
3059 (if (file-remote-p filename
)
3060 (with-parsed-tramp-file-name filename nil
3061 (if (and (zerop (user-uid)) (tramp-local-host-p v
))
3062 ;; If we are root on the local host, we can do it directly.
3063 (tramp-set-file-uid-gid localname uid gid
)
3064 (let ((uid (or (and (integerp uid
) uid
)
3065 (tramp-get-remote-uid v
'integer
)))
3066 (gid (or (and (integerp gid
) gid
)
3067 (tramp-get-remote-gid v
'integer
))))
3070 "chown %d:%d %s" uid gid
3071 (tramp-shell-quote-argument localname
))))))
3073 ;; We handle also the local part, because there doesn't exist
3074 ;; `set-file-uid-gid'. On W32 "chown" might not work.
3075 (let ((uid (or (and (integerp uid
) uid
) (tramp-get-local-uid 'integer
)))
3076 (gid (or (and (integerp gid
) gid
) (tramp-get-local-gid 'integer
))))
3077 (tramp-local-call-process
3079 (format "%d:%d" uid gid
) (tramp-shell-quote-argument filename
))))))
3081 (defun tramp-remote-selinux-p (vec)
3082 "Check, whether SELINUX is enabled on the remote host."
3083 (with-connection-property (tramp-get-connection-process vec
) "selinux-p"
3084 (let ((result (tramp-find-executable
3085 vec
"getenforce" (tramp-get-remote-path vec
) t t
)))
3088 (tramp-send-command-and-read
3089 vec
(format "echo \\\"`%S`\\\"" result
))
3092 (defun tramp-handle-file-selinux-context (filename)
3093 "Like `file-selinux-context' for Tramp files."
3094 (with-parsed-tramp-file-name filename nil
3095 (with-file-property v localname
"file-selinux-context"
3096 (let ((context '(nil nil nil nil
))
3097 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
3098 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
3099 (when (and (tramp-remote-selinux-p v
)
3100 (zerop (tramp-send-command-and-check
3103 (tramp-get-ls-command v
)
3104 (tramp-shell-quote-argument localname
)))))
3105 (with-current-buffer (tramp-get-connection-buffer v
)
3106 (goto-char (point-min))
3107 (when (re-search-forward regexp
(tramp-compat-line-end-position) t
)
3108 (setq context
(list (match-string 1) (match-string 2)
3109 (match-string 3) (match-string 4))))))
3110 ;; Return the context.
3113 (defun tramp-handle-set-file-selinux-context (filename context
)
3114 "Like `set-file-selinux-context' for Tramp files."
3115 (with-parsed-tramp-file-name filename nil
3116 (if (and (consp context
)
3117 (tramp-remote-selinux-p v
)
3118 (zerop (tramp-send-command-and-check
3119 v
(format "chcon %s %s %s %s %s"
3120 (if (stringp (nth 0 context
))
3121 (format "--user=%s" (nth 0 context
)) "")
3122 (if (stringp (nth 1 context
))
3123 (format "--role=%s" (nth 1 context
)) "")
3124 (if (stringp (nth 2 context
))
3125 (format "--type=%s" (nth 2 context
)) "")
3126 (if (stringp (nth 3 context
))
3127 (format "--range=%s" (nth 3 context
)) "")
3128 (tramp-shell-quote-argument localname
)))))
3129 (tramp-set-file-property v localname
"file-selinux-context" context
)
3130 (tramp-set-file-property v localname
"file-selinux-context" 'undef
)))
3131 ;; We always return nil.
3134 ;; Simple functions using the `test' command.
3136 (defun tramp-handle-file-executable-p (filename)
3137 "Like `file-executable-p' for Tramp files."
3138 (with-parsed-tramp-file-name filename nil
3139 (with-file-property v localname
"file-executable-p"
3140 ;; Examine `file-attributes' cache to see if request can be
3141 ;; satisfied without remote operation.
3142 (or (tramp-check-cached-permissions v ?x
)
3143 (zerop (tramp-run-test "-x" filename
))))))
3145 (defun tramp-handle-file-readable-p (filename)
3146 "Like `file-readable-p' for Tramp files."
3147 (with-parsed-tramp-file-name filename nil
3148 (with-file-property v localname
"file-readable-p"
3149 ;; Examine `file-attributes' cache to see if request can be
3150 ;; satisfied without remote operation.
3151 (or (tramp-check-cached-permissions v ?r
)
3152 (zerop (tramp-run-test "-r" filename
))))))
3154 ;; When the remote shell is started, it looks for a shell which groks
3155 ;; tilde expansion. Here, we assume that all shells which grok tilde
3156 ;; expansion will also provide a `test' command which groks `-nt' (for
3157 ;; newer than). If this breaks, tell me about it and I'll try to do
3158 ;; something smarter about it.
3159 (defun tramp-handle-file-newer-than-file-p (file1 file2
)
3160 "Like `file-newer-than-file-p' for Tramp files."
3161 (cond ((not (file-exists-p file1
))
3163 ((not (file-exists-p file2
))
3165 ;; We are sure both files exist at this point.
3168 ;; We try to get the mtime of both files. If they are not
3169 ;; equal to the "dont-know" value, then we subtract the times
3170 ;; and obtain the result.
3171 (let ((fa1 (file-attributes file1
))
3172 (fa2 (file-attributes file2
)))
3173 (if (and (not (equal (nth 5 fa1
) '(0 0)))
3174 (not (equal (nth 5 fa2
) '(0 0))))
3175 (> 0 (tramp-time-diff (nth 5 fa2
) (nth 5 fa1
)))
3176 ;; If one of them is the dont-know value, then we can
3177 ;; still try to run a shell command on the remote host.
3178 ;; However, this only works if both files are Tramp
3179 ;; files and both have the same method, same user, same
3181 (unless (tramp-equal-remote file1 file2
)
3182 (with-parsed-tramp-file-name
3183 (if (tramp-tramp-file-p file1
) file1 file2
) nil
3186 "Files %s and %s must have same method, user, host"
3188 (with-parsed-tramp-file-name file1 nil
3189 (zerop (tramp-run-test2
3190 (tramp-get-test-nt-command v
) file1 file2
)))))))))
3192 ;; Functions implemented using the basic functions above.
3194 (defun tramp-handle-file-modes (filename)
3195 "Like `file-modes' for Tramp files."
3196 (let ((truename (or (file-truename filename
) filename
)))
3197 (when (file-exists-p truename
)
3198 (tramp-mode-string-to-int (nth 8 (file-attributes truename
))))))
3200 (defun tramp-default-file-modes (filename)
3201 "Return file modes of FILENAME as integer.
3202 If the file modes of FILENAME cannot be determined, return the
3203 value of `default-file-modes', without execute permissions."
3204 (or (file-modes filename
)
3205 (logand (default-file-modes) (tramp-octal-to-decimal "0666"))))
3207 (defun tramp-handle-file-directory-p (filename)
3208 "Like `file-directory-p' for Tramp files."
3209 ;; Care must be taken that this function returns `t' for symlinks
3210 ;; pointing to directories. Surely the most obvious implementation
3211 ;; would be `test -d', but that returns false for such symlinks.
3212 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
3213 ;; I now think he's right. So we could be using `test -d', couldn't
3216 ;; Alternatives: `cd %s', `test -d %s'
3217 (with-parsed-tramp-file-name filename nil
3218 (with-file-property v localname
"file-directory-p"
3219 (zerop (tramp-run-test "-d" filename
)))))
3221 (defun tramp-handle-file-regular-p (filename)
3222 "Like `file-regular-p' for Tramp files."
3223 (and (file-exists-p filename
)
3224 (eq ?-
(aref (nth 8 (file-attributes filename
)) 0))))
3226 (defun tramp-handle-file-symlink-p (filename)
3227 "Like `file-symlink-p' for Tramp files."
3228 (with-parsed-tramp-file-name filename nil
3229 (let ((x (car (file-attributes filename
))))
3231 ;; When Tramp is running on VMS, then `file-name-absolute-p'
3232 ;; might do weird things.
3233 (if (file-name-absolute-p x
)
3234 (tramp-make-tramp-file-name method user host x
)
3237 (defun tramp-handle-file-writable-p (filename)
3238 "Like `file-writable-p' for Tramp files."
3239 (with-parsed-tramp-file-name filename nil
3240 (with-file-property v localname
"file-writable-p"
3241 (if (file-exists-p filename
)
3242 ;; Examine `file-attributes' cache to see if request can be
3243 ;; satisfied without remote operation.
3244 (or (tramp-check-cached-permissions v ?w
)
3245 (zerop (tramp-run-test "-w" filename
)))
3246 ;; If file doesn't exist, check if directory is writable.
3247 (and (zerop (tramp-run-test
3248 "-d" (file-name-directory filename
)))
3249 (zerop (tramp-run-test
3250 "-w" (file-name-directory filename
))))))))
3252 (defun tramp-handle-file-ownership-preserved-p (filename)
3253 "Like `file-ownership-preserved-p' for Tramp files."
3254 (with-parsed-tramp-file-name filename nil
3255 (with-file-property v localname
"file-ownership-preserved-p"
3256 (let ((attributes (file-attributes filename
)))
3257 ;; Return t if the file doesn't exist, since it's true that no
3258 ;; information would be lost by an (attempted) delete and create.
3259 (or (null attributes
)
3260 (= (nth 2 attributes
) (tramp-get-remote-uid v
'integer
)))))))
3262 ;; Other file name ops.
3264 (defun tramp-handle-directory-file-name (directory)
3265 "Like `directory-file-name' for Tramp files."
3266 ;; If localname component of filename is "/", leave it unchanged.
3267 ;; Otherwise, remove any trailing slash from localname component.
3268 ;; Method, host, etc, are unchanged. Does it make sense to try
3269 ;; to avoid parsing the filename?
3270 (with-parsed-tramp-file-name directory nil
3271 (if (and (not (zerop (length localname
)))
3272 (eq (aref localname
(1- (length localname
))) ?
/)
3273 (not (string= localname
"/")))
3274 (substring directory
0 -
1)
3277 ;; Directory listings.
3279 (defun tramp-handle-directory-files
3280 (directory &optional full match nosort files-only
)
3281 "Like `directory-files' for Tramp files."
3282 ;; FILES-ONLY is valid for XEmacs only.
3283 (when (file-directory-p directory
)
3284 (setq directory
(file-name-as-directory (expand-file-name directory
)))
3285 (let ((temp (nreverse (file-name-all-completions "" directory
)))
3289 (setq item
(directory-file-name (pop temp
)))
3290 (when (and (or (null match
) (string-match match item
))
3291 (or (null files-only
)
3293 (and (equal files-only t
) (file-regular-p item
))
3294 ;; Directories only.
3295 (file-directory-p item
)))
3296 (push (if full
(concat directory item
) item
)
3298 (if nosort result
(sort result
'string
<)))))
3300 (defun tramp-handle-directory-files-and-attributes
3301 (directory &optional full match nosort id-format
)
3302 "Like `directory-files-and-attributes' for Tramp files."
3303 (unless id-format
(setq id-format
'integer
))
3304 (when (file-directory-p directory
)
3305 (setq directory
(expand-file-name directory
))
3308 (with-parsed-tramp-file-name directory nil
3311 (format "directory-files-and-attributes-%s" id-format
)
3316 (tramp-convert-file-attributes v
(cdr x
))))
3318 ((tramp-get-remote-stat v
)
3319 (tramp-do-directory-files-and-attributes-with-stat
3320 v localname id-format
))
3321 ((tramp-get-remote-perl v
)
3322 (tramp-do-directory-files-and-attributes-with-perl
3323 v localname id-format
)))))))))
3327 (setq item
(pop temp
))
3328 (when (or (null match
) (string-match match
(car item
)))
3330 (setcar item
(expand-file-name (car item
) directory
)))
3331 (push item result
)))
3335 (sort result
(lambda (x y
) (string< (car x
) (car y
))))))))
3337 (defun tramp-do-directory-files-and-attributes-with-perl
3338 (vec localname
&optional id-format
)
3339 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
3340 (tramp-message vec
5 "directory-files-and-attributes with perl: %s" localname
)
3341 (tramp-maybe-send-script
3342 vec tramp-perl-directory-files-and-attributes
3343 "tramp_perl_directory_files_and_attributes")
3345 (tramp-send-command-and-read
3347 (format "tramp_perl_directory_files_and_attributes %s %s"
3348 (tramp-shell-quote-argument localname
) id-format
))))
3349 (when (stringp object
) (tramp-error vec
'file-error object
))
3352 (defun tramp-do-directory-files-and-attributes-with-stat
3353 (vec localname
&optional id-format
)
3354 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
3355 (tramp-message vec
5 "directory-files-and-attributes with stat: %s" localname
)
3356 (tramp-send-command-and-read
3360 ;; We must care about filenames with spaces, or starting with
3361 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
3362 ;; but it does not work on all remote systems. Therefore, we
3363 ;; quote the filenames via sed.
3364 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | xargs "
3365 "%s -c '(\"%%n\" (\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)'); "
3367 (tramp-shell-quote-argument localname
)
3368 (tramp-get-ls-command vec
)
3369 (tramp-get-remote-stat vec
)
3370 (if (eq id-format
'integer
) "%u" "\"%U\"")
3371 (if (eq id-format
'integer
) "%g" "\"%G\""))))
3373 ;; This function should return "foo/" for directories and "bar" for
3375 (defun tramp-handle-file-name-all-completions (filename directory
)
3376 "Like `file-name-all-completions' for Tramp files."
3377 (unless (save-match-data (string-match "/" filename
))
3378 (with-parsed-tramp-file-name (expand-file-name directory
) nil
3387 ;; Ignore if expired
3388 (or (not (integerp tramp-completion-reread-directory-timeout
))
3389 (<= (tramp-time-diff
3391 (tramp-get-file-property
3392 v localname
"last-completion" '(0 0 0)))
3393 tramp-completion-reread-directory-timeout
))
3395 ;; Try cache entries for filename, filename with last
3396 ;; character removed, filename with last two characters
3397 ;; removed, ..., and finally the empty string - all
3398 ;; concatenated to the local directory name
3400 ;; This is inefficient for very long filenames, pity
3401 ;; `reduce' is not available...
3408 (tramp-get-file-property
3410 (concat localname
(substring filename
0 x
))
3411 "file-name-all-completions"
3413 (when cache-hit
(list cache-hit
))))
3414 (tramp-compat-number-sequence (length filename
) 0 -
1)))))
3416 ;; Cache expired or no matching cache entry found so we need
3417 ;; to perform a remote operation
3419 ;; Get a list of directories and files, including reliably
3420 ;; tagging the directories with a trailing '/'. Because I
3421 ;; rock. --daniel@danann.net
3423 ;; Changed to perform `cd' in the same remote op and only
3424 ;; get entries starting with `filename'. Capture any `cd'
3425 ;; error messages. Ensure any `cd' and `echo' aliases are
3429 (if (tramp-get-remote-perl v
)
3431 (tramp-maybe-send-script
3432 v tramp-perl-file-name-all-completions
3433 "tramp_perl_file_name_all_completions")
3434 (format "tramp_perl_file_name_all_completions %s %s %d"
3435 (tramp-shell-quote-argument localname
)
3436 (tramp-shell-quote-argument filename
)
3438 ;; `read-file-name-completion-ignore-case'
3439 ;; is introduced with Emacs 22.1.
3441 'read-file-name-completion-ignore-case
)
3442 'read-file-name-completion-ignore-case
3443 'completion-ignore-case
))
3447 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
3448 ;; `ls' with wildcard might fail with `Argument
3449 ;; list too long' error in some corner cases; if
3450 ;; `ls' fails after `cd' succeeded, chances are
3451 ;; that's the case, so let's retry without
3452 ;; wildcard. This will return "too many" entries
3453 ;; but that isn't harmful.
3454 " || %s -a 2>/dev/null)"
3455 " | while read f; do"
3456 " if %s -d \"$f\" 2>/dev/null;"
3457 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
3458 " && \\echo ok) || \\echo fail")
3459 (tramp-shell-quote-argument localname
)
3460 (tramp-get-ls-command v
)
3461 ;; When `filename' is empty, just `ls' without
3462 ;; filename argument is more efficient than `ls *'
3463 ;; for very large directories and might avoid the
3464 ;; `Argument list too long' error.
3466 ;; With and only with wildcard, we need to add
3467 ;; `-d' to prevent `ls' from descending into
3469 (if (zerop (length filename
))
3471 (concat (tramp-shell-quote-argument filename
) "* -d"))
3472 (tramp-get-ls-command v
)
3473 (tramp-get-test-command v
))))
3475 ;; Now grab the output.
3476 (with-current-buffer (tramp-get-buffer v
)
3477 (goto-char (point-max))
3479 ;; Check result code, found in last line of output
3481 (if (looking-at "^fail$")
3483 ;; Grab error message from line before last line
3484 ;; (it was put there by `cd 2>&1')
3488 "tramp-handle-file-name-all-completions: %s"
3490 (point) (tramp-compat-line-end-position))))
3491 ;; For peace of mind, if buffer doesn't end in `fail'
3492 ;; then it should end in `ok'. If neither are in the
3493 ;; buffer something went seriously wrong on the remote
3495 (unless (looking-at "^ok$")
3499 tramp-handle-file-name-all-completions: internal error accessing `%s': `%s'"
3500 (tramp-shell-quote-argument localname
) (buffer-string))))
3502 (while (zerop (forward-line -
1))
3503 (push (buffer-substring
3504 (point) (tramp-compat-line-end-position))
3507 ;; Because the remote op went through OK we know the
3508 ;; directory we `cd'-ed to exists
3509 (tramp-set-file-property
3510 v localname
"file-exists-p" t
)
3512 ;; Because the remote op went through OK we know every
3513 ;; file listed by `ls' exists.
3514 (mapc (lambda (entry)
3515 (tramp-set-file-property
3516 v
(concat localname entry
) "file-exists-p" t
))
3519 (tramp-set-file-property
3520 v localname
"last-completion" (current-time))
3522 ;; Store result in the cache
3523 (tramp-set-file-property
3524 v
(concat localname filename
)
3525 "file-name-all-completions"
3528 (defun tramp-handle-file-name-completion
3529 (filename directory
&optional predicate
)
3530 "Like `file-name-completion' for Tramp files."
3531 (unless (tramp-tramp-file-p directory
)
3533 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
3537 (mapcar 'list
(file-name-all-completions filename directory
))
3539 (lambda (x) (funcall predicate
(expand-file-name (car x
) directory
))))))
3543 (defun tramp-handle-add-name-to-file
3544 (filename newname
&optional ok-if-already-exists
)
3545 "Like `add-name-to-file' for Tramp files."
3546 (unless (tramp-equal-remote filename newname
)
3547 (with-parsed-tramp-file-name
3548 (if (tramp-tramp-file-p filename
) filename newname
) nil
3551 "add-name-to-file: %s"
3552 "only implemented for same method, same user, same host")))
3553 (with-parsed-tramp-file-name filename v1
3554 (with-parsed-tramp-file-name newname v2
3555 (let ((ln (when v1
(tramp-get-remote-ln v1
))))
3556 (when (and (not ok-if-already-exists
)
3557 (file-exists-p newname
)
3558 (not (numberp ok-if-already-exists
))
3561 "File %s already exists; make it a new name anyway? "
3565 "add-name-to-file: file %s already exists" newname
))
3566 (tramp-flush-file-property v2
(file-name-directory v2-localname
))
3567 (tramp-flush-file-property v2 v2-localname
)
3568 (tramp-barf-unless-okay
3570 (format "%s %s %s" ln
(tramp-shell-quote-argument v1-localname
)
3571 (tramp-shell-quote-argument v2-localname
))
3572 "error with add-name-to-file, see buffer `%s' for details"
3575 (defun tramp-handle-copy-file
3576 (filename newname
&optional ok-if-already-exists keep-date
3577 preserve-uid-gid preserve-selinux-context
)
3578 "Like `copy-file' for Tramp files."
3579 (setq filename
(expand-file-name filename
))
3580 (setq newname
(expand-file-name newname
))
3582 ;; At least one file a Tramp file?
3583 ((or (tramp-tramp-file-p filename
)
3584 (tramp-tramp-file-p newname
))
3585 (tramp-do-copy-or-rename-file
3586 'copy filename newname ok-if-already-exists keep-date
3587 preserve-uid-gid preserve-selinux-context
))
3589 (preserve-selinux-context
3590 (tramp-run-real-handler
3592 (list filename newname ok-if-already-exists keep-date
3593 preserve-uid-gid preserve-selinux-context
)))
3595 (tramp-run-real-handler
3597 (list filename newname ok-if-already-exists keep-date preserve-uid-gid
)))
3599 (tramp-run-real-handler
3600 'copy-file
(list filename newname ok-if-already-exists keep-date
)))))
3602 (defun tramp-handle-copy-directory (dirname newname
&optional keep-date parents
)
3603 "Like `copy-directory' for Tramp files."
3604 (let ((t1 (tramp-tramp-file-p dirname
))
3605 (t2 (tramp-tramp-file-p newname
)))
3606 (with-parsed-tramp-file-name (if t1 dirname newname
) nil
3607 (if (and (tramp-get-method-parameter method
'tramp-copy-recursive
)
3608 ;; When DIRNAME and NEWNAME are remote, they must have
3610 (or (null t1
) (null t2
)
3612 (tramp-file-name-method (tramp-dissect-file-name dirname
))
3613 (tramp-file-name-method (tramp-dissect-file-name newname
)))))
3614 ;; scp or rsync DTRT.
3616 (setq dirname
(directory-file-name (expand-file-name dirname
))
3617 newname
(directory-file-name (expand-file-name newname
)))
3618 (if (and (file-directory-p newname
)
3619 (not (string-equal (file-name-nondirectory dirname
)
3620 (file-name-nondirectory newname
))))
3623 (file-name-nondirectory dirname
) newname
)))
3624 (if (not (file-directory-p (file-name-directory newname
)))
3625 (make-directory (file-name-directory newname
) parents
))
3626 (tramp-do-copy-or-rename-file-out-of-band
3627 'copy dirname newname keep-date
))
3628 ;; We must do it file-wise.
3629 (tramp-run-real-handler
3630 'copy-directory
(list dirname newname keep-date parents
)))
3632 ;; When newname did exist, we have wrong cached values.
3634 (with-parsed-tramp-file-name newname nil
3635 (tramp-flush-file-property v
(file-name-directory localname
))
3636 (tramp-flush-file-property v localname
))))))
3638 (defun tramp-handle-rename-file
3639 (filename newname
&optional ok-if-already-exists
)
3640 "Like `rename-file' for Tramp files."
3641 ;; Check if both files are local -- invoke normal rename-file.
3642 ;; Otherwise, use Tramp from local system.
3643 (setq filename
(expand-file-name filename
))
3644 (setq newname
(expand-file-name newname
))
3645 ;; At least one file a Tramp file?
3646 (if (or (tramp-tramp-file-p filename
)
3647 (tramp-tramp-file-p newname
))
3648 (tramp-do-copy-or-rename-file
3649 'rename filename newname ok-if-already-exists t t
)
3650 (tramp-run-real-handler
3651 'rename-file
(list filename newname ok-if-already-exists
))))
3653 (defun tramp-do-copy-or-rename-file
3654 (op filename newname
&optional ok-if-already-exists keep-date
3655 preserve-uid-gid preserve-selinux-context
)
3656 "Copy or rename a remote file.
3657 OP must be `copy' or `rename' and indicates the operation to perform.
3658 FILENAME specifies the file to copy or rename, NEWNAME is the name of
3659 the new file (for copy) or the new name of the file (for rename).
3660 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
3661 KEEP-DATE means to make sure that NEWNAME has the same timestamp
3662 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3663 the uid and gid if both files are on the same host.
3664 PRESERVE-SELINUX-CONTEXT activates selinux commands.
3666 This function is invoked by `tramp-handle-copy-file' and
3667 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
3668 and `rename'. FILENAME and NEWNAME must be absolute file names."
3669 (unless (memq op
'(copy rename
))
3670 (error "Unknown operation `%s', must be `copy' or `rename'" op
))
3671 (let ((t1 (tramp-tramp-file-p filename
))
3672 (t2 (tramp-tramp-file-p newname
))
3673 (context (and preserve-selinux-context
3674 (apply 'file-selinux-context
(list filename
))))
3677 (with-parsed-tramp-file-name (if t1 filename newname
) nil
3678 (when (and (not ok-if-already-exists
) (file-exists-p newname
))
3680 v
'file-already-exists
"File %s already exists" newname
))
3682 (with-progress-reporter
3683 v
0 (format "%s %s to %s"
3684 (if (eq op
'copy
) "Copying" "Renaming")
3688 ;; Both are Tramp files.
3690 (with-parsed-tramp-file-name filename v1
3691 (with-parsed-tramp-file-name newname v2
3693 ;; Shortcut: if method, host, user are the same for
3694 ;; both files, we invoke `cp' or `mv' on the remote
3696 ((tramp-equal-remote filename newname
)
3697 (tramp-do-copy-or-rename-file-directly
3699 ok-if-already-exists keep-date preserve-uid-gid
))
3701 ;; Try out-of-band operation.
3702 ((tramp-method-out-of-band-p
3703 v1
(nth 7 (file-attributes filename
)))
3704 (tramp-do-copy-or-rename-file-out-of-band
3705 op filename newname keep-date
))
3707 ;; No shortcut was possible. So we copy the file
3708 ;; first. If the operation was `rename', we go back
3709 ;; and delete the original file (if the copy was
3710 ;; successful). The approach is simple-minded: we
3711 ;; create a new buffer, insert the contents of the
3712 ;; source file into it, then write out the buffer to
3713 ;; the target file. The advantage is that it doesn't
3714 ;; matter which filename handlers are used for the
3715 ;; source and target file.
3717 (tramp-do-copy-or-rename-file-via-buffer
3718 op filename newname keep-date
))))))
3720 ;; One file is a Tramp file, the other one is local.
3723 ;; Fast track on local machine.
3724 ((tramp-local-host-p v
)
3725 (tramp-do-copy-or-rename-file-directly
3727 ok-if-already-exists keep-date preserve-uid-gid
))
3729 ;; If the Tramp file has an out-of-band method, the
3730 ;; corresponding copy-program can be invoked.
3731 ((tramp-method-out-of-band-p v
(nth 7 (file-attributes filename
)))
3732 (tramp-do-copy-or-rename-file-out-of-band
3733 op filename newname keep-date
))
3735 ;; Use the inline method via a Tramp buffer.
3736 (t (tramp-do-copy-or-rename-file-via-buffer
3737 op filename newname keep-date
))))
3740 ;; One of them must be a Tramp file.
3741 (error "Tramp implementation says this cannot happen")))
3743 ;; Handle `preserve-selinux-context'.
3744 (when context
(apply 'set-file-selinux-context
(list newname context
)))
3746 ;; In case of `rename', we must flush the cache of the source file.
3747 (when (and t1
(eq op
'rename
))
3748 (with-parsed-tramp-file-name filename v1
3749 (tramp-flush-file-property v1
(file-name-directory localname
))
3750 (tramp-flush-file-property v1 localname
)))
3752 ;; When newname did exist, we have wrong cached values.
3754 (with-parsed-tramp-file-name newname v2
3755 (tramp-flush-file-property v2
(file-name-directory localname
))
3756 (tramp-flush-file-property v2 localname
)))))))
3758 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date
)
3759 "Use an Emacs buffer to copy or rename a file.
3760 First arg OP is either `copy' or `rename' and indicates the operation.
3761 FILENAME is the source file, NEWNAME the target file.
3762 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
3764 ;; We must disable multibyte, because binary data shall not be
3766 (set-buffer-multibyte nil
)
3767 (let ((coding-system-for-read 'binary
)
3768 (jka-compr-inhibit t
))
3769 (insert-file-contents-literally filename
))
3770 ;; We don't want the target file to be compressed, so we let-bind
3771 ;; `jka-compr-inhibit' to t.
3772 (let ((coding-system-for-write 'binary
)
3773 (jka-compr-inhibit t
))
3774 (write-region (point-min) (point-max) newname
)))
3775 ;; KEEP-DATE handling.
3776 (when keep-date
(set-file-times newname
(nth 5 (file-attributes filename
))))
3778 (set-file-modes newname
(tramp-default-file-modes filename
))
3779 ;; If the operation was `rename', delete the original file.
3780 (unless (eq op
'copy
) (delete-file filename
)))
3782 (defun tramp-do-copy-or-rename-file-directly
3783 (op filename newname ok-if-already-exists keep-date preserve-uid-gid
)
3784 "Invokes `cp' or `mv' on the remote system.
3785 OP must be one of `copy' or `rename', indicating `cp' or `mv',
3786 respectively. FILENAME specifies the file to copy or rename,
3787 NEWNAME is the name of the new file (for copy) or the new name of
3788 the file (for rename). Both files must reside on the same host.
3789 KEEP-DATE means to make sure that NEWNAME has the same timestamp
3790 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3791 the uid and gid from FILENAME."
3792 (let ((t1 (tramp-tramp-file-p filename
))
3793 (t2 (tramp-tramp-file-p newname
))
3794 (file-times (nth 5 (file-attributes filename
)))
3795 (file-modes (tramp-default-file-modes filename
)))
3796 (with-parsed-tramp-file-name (if t1 filename newname
) nil
3797 (let* ((cmd (cond ((and (eq op
'copy
) preserve-uid-gid
) "cp -f -p")
3798 ((eq op
'copy
) "cp -f")
3799 ((eq op
'rename
) "mv -f")
3802 "Unknown operation `%s', must be `copy' or `rename'"
3806 (tramp-file-name-handler 'file-remote-p filename
'localname
)
3810 (tramp-file-name-handler 'file-remote-p newname
'localname
)
3812 (prefix (file-remote-p (if t1 filename newname
)))
3816 ;; Both files are on a remote host, with same user.
3819 (tramp-send-command-and-check
3821 (format "%s %s %s" cmd
3822 (tramp-shell-quote-argument localname1
)
3823 (tramp-shell-quote-argument localname2
))))
3824 (with-current-buffer (tramp-get-buffer v
)
3825 (goto-char (point-min))
3829 ;; Mask cp -f error.
3831 tramp-operation-not-permitted-regexp nil t
))
3833 (tramp-error-with-buffer
3835 "Copying directly failed, see buffer `%s' for details."
3838 ;; We are on the local host.
3841 ;; We can do it directly.
3842 ((let (file-name-handler-alist)
3843 (and (file-readable-p localname1
)
3844 (file-writable-p (file-name-directory localname2
))
3845 (or (file-directory-p localname2
)
3846 (file-writable-p localname2
))))
3848 (tramp-compat-copy-file
3849 localname1 localname2 ok-if-already-exists
3850 keep-date preserve-uid-gid
)
3851 (tramp-run-real-handler
3852 'rename-file
(list localname1 localname2 ok-if-already-exists
))))
3854 ;; We can do it directly with `tramp-send-command'
3855 ((and (file-readable-p (concat prefix localname1
))
3857 (file-name-directory (concat prefix localname2
)))
3858 (or (file-directory-p (concat prefix localname2
))
3859 (file-writable-p (concat prefix localname2
))))
3860 (tramp-do-copy-or-rename-file-directly
3861 op
(concat prefix localname1
) (concat prefix localname2
)
3862 ok-if-already-exists keep-date t
)
3863 ;; We must change the ownership to the local user.
3864 (tramp-set-file-uid-gid
3865 (concat prefix localname2
)
3866 (tramp-get-local-uid 'integer
)
3867 (tramp-get-local-gid 'integer
)))
3869 ;; We need a temporary file in between.
3871 ;; Create the temporary file.
3872 (let ((tmpfile (tramp-compat-make-temp-file localname1
)))
3879 (tramp-send-command-and-check
3882 (tramp-shell-quote-argument localname1
)
3883 (tramp-shell-quote-argument tmpfile
))))
3884 (tramp-error-with-buffer
3886 "Copying directly failed, see buffer `%s' for details."
3887 (tramp-get-buffer v
)))
3888 ;; We must change the ownership as remote user.
3889 ;; Since this does not work reliable, we also
3890 ;; give read permissions.
3892 (concat prefix tmpfile
) (tramp-octal-to-decimal "0777"))
3893 (tramp-set-file-uid-gid
3894 (concat prefix tmpfile
)
3895 (tramp-get-local-uid 'integer
)
3896 (tramp-get-local-gid 'integer
)))
3899 (tramp-compat-copy-file
3900 localname1 tmpfile t
3901 keep-date preserve-uid-gid
)
3902 (tramp-run-real-handler
3904 (list localname1 tmpfile t
)))
3905 ;; We must change the ownership as local user.
3906 ;; Since this does not work reliable, we also
3907 ;; give read permissions.
3908 (set-file-modes tmpfile
(tramp-octal-to-decimal "0777"))
3909 (tramp-set-file-uid-gid
3911 (tramp-get-remote-uid v
'integer
)
3912 (tramp-get-remote-gid v
'integer
))))
3914 ;; Move the temporary file to its destination.
3919 (tramp-send-command-and-check
3922 (tramp-shell-quote-argument tmpfile
)
3923 (tramp-shell-quote-argument localname2
))))
3924 (tramp-error-with-buffer
3926 "Copying directly failed, see buffer `%s' for details."
3927 (tramp-get-buffer v
))))
3929 (tramp-run-real-handler
3931 (list tmpfile localname2 ok-if-already-exists
)))))
3935 (delete-file tmpfile
)
3938 ;; Set the time and mode. Mask possible errors.
3941 (set-file-times newname file-times
)
3942 (set-file-modes newname file-modes
))
3945 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date
)
3946 "Invoke rcp program to copy.
3947 The method used must be an out-of-band method."
3948 (let ((t1 (tramp-tramp-file-p filename
))
3949 (t2 (tramp-tramp-file-p newname
))
3950 copy-program copy-args copy-env copy-keep-date port spec
3953 (with-parsed-tramp-file-name (if t1 filename newname
) nil
3956 ;; Both are Tramp files. We shall optimize it, when the
3957 ;; methods for filename and newname are the same.
3958 (let* ((dir-flag (file-directory-p filename
))
3959 (tmpfile (tramp-compat-make-temp-file localname dir-flag
)))
3963 (file-name-nondirectory newname
) tmpfile
)))
3966 (tramp-do-copy-or-rename-file-out-of-band
3967 op filename tmpfile keep-date
)
3968 (tramp-do-copy-or-rename-file-out-of-band
3969 'rename tmpfile newname keep-date
))
3973 (tramp-compat-delete-directory
3974 (expand-file-name ".." tmpfile
) 'recursive
)
3975 (delete-file tmpfile
))
3978 ;; Expand hops. Might be necessary for gateway methods.
3979 (setq v
(car (tramp-compute-multi-hops v
)))
3980 (aset v
3 localname
)
3982 ;; Check which ones of source and target are Tramp files.
3983 (setq source
(if t1
(tramp-make-copy-program-file-name v
) filename
)
3985 (if (and (file-directory-p filename
)
3987 (file-name-nondirectory filename
)
3988 (file-name-nondirectory newname
)))
3989 'file-name-directory
3991 (if t2
(tramp-make-copy-program-file-name v
) newname
)))
3993 ;; Check for port number. Until now, there's no need for handling
3994 ;; like method, user, host.
3995 (setq host
(tramp-file-name-real-host v
)
3996 port
(tramp-file-name-port v
)
3997 port
(or (and port
(number-to-string port
)) ""))
3999 ;; Compose copy command.
4000 (setq spec
(format-spec-make
4001 ?h host ?u user ?p port
4002 ?t
(tramp-get-connection-property
4003 (tramp-get-connection-process v
) "temp-file" "")
4004 ?k
(if keep-date
" " ""))
4005 copy-program
(tramp-get-method-parameter
4006 method
'tramp-copy-program
)
4007 copy-keep-date
(tramp-get-method-parameter
4008 method
'tramp-copy-keep-date
)
4016 ;; " " is indication for keep-date argument.
4017 (delete " " (mapcar (lambda (y) (format-spec y spec
)) x
)))
4018 (unless (member "" x
) (mapconcat 'identity x
" ")))
4019 (tramp-get-method-parameter method
'tramp-copy-args
)))
4025 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
4026 (unless (member "" x
) (mapconcat 'identity x
" ")))
4027 (tramp-get-method-parameter method
'tramp-copy-env
))))
4029 ;; Check for program.
4030 (when (and (fboundp 'executable-find
)
4031 (not (let ((default-directory
4032 (tramp-compat-temporary-file-directory)))
4033 (executable-find copy-program
))))
4035 v
'file-error
"Cannot find copy program: %s" copy-program
))
4037 ;; Set variables for computing the prompt for reading
4039 (setq tramp-current-method
(tramp-file-name-method v
)
4040 tramp-current-user
(tramp-file-name-user v
)
4041 tramp-current-host
(tramp-file-name-host v
))
4045 ;; The default directory must be remote.
4046 (let ((default-directory
4047 (file-name-directory (if t1 filename newname
)))
4048 (process-environment (copy-sequence process-environment
)))
4049 ;; Set the transfer process properties.
4050 (tramp-set-connection-property
4051 v
"process-name" (buffer-name (current-buffer)))
4052 (tramp-set-connection-property
4053 v
"process-buffer" (current-buffer))
4055 (tramp-message v
5 "%s=\"%s\"" (car copy-env
) (cadr copy-env
))
4056 (setenv (pop copy-env
) (pop copy-env
)))
4058 ;; Use an asynchronous process. By this, password can
4059 ;; be handled. The default directory must be local, in
4060 ;; order to apply the correct `copy-program'. We don't
4061 ;; set a timeout, because the copying of large files can
4062 ;; last longer than 60 secs.
4063 (let ((p (let ((default-directory
4064 (tramp-compat-temporary-file-directory)))
4065 (apply 'start-process
4066 (tramp-get-connection-property
4067 v
"process-name" nil
)
4068 (tramp-get-connection-property
4069 v
"process-buffer" nil
)
4071 (append copy-args
(list source target
))))))
4073 v
6 "%s" (mapconcat 'identity
(process-command p
) " "))
4074 (tramp-set-process-query-on-exit-flag p nil
)
4075 (tramp-process-actions p v tramp-actions-copy-out-of-band
))))
4077 ;; Reset the transfer process properties.
4078 (tramp-set-connection-property v
"process-name" nil
)
4079 (tramp-set-connection-property v
"process-buffer" nil
))
4081 ;; Handle KEEP-DATE argument.
4082 (when (and keep-date
(not copy-keep-date
))
4083 (set-file-times newname
(nth 5 (file-attributes filename
))))
4086 (unless (and keep-date copy-keep-date
)
4088 (set-file-modes newname
(tramp-default-file-modes filename
)))))
4090 ;; If the operation was `rename', delete the original file.
4091 (unless (eq op
'copy
)
4092 (if (file-regular-p filename
)
4093 (delete-file filename
)
4094 (tramp-compat-delete-directory filename
'recursive
))))))
4096 (defun tramp-handle-make-directory (dir &optional parents
)
4097 "Like `make-directory' for Tramp files."
4098 (setq dir
(expand-file-name dir
))
4099 (with-parsed-tramp-file-name dir nil
4100 (tramp-flush-directory-property v
(file-name-directory localname
))
4102 (tramp-barf-unless-okay
4105 (if parents
"mkdir -p" "mkdir")
4106 (tramp-shell-quote-argument localname
))
4107 "Couldn't make directory %s" dir
))))
4109 (defun tramp-handle-delete-directory (directory &optional recursive
)
4110 "Like `delete-directory' for Tramp files."
4111 (setq directory
(expand-file-name directory
))
4112 (with-parsed-tramp-file-name directory nil
4113 (tramp-flush-file-property v
(file-name-directory localname
))
4114 (tramp-flush-directory-property v localname
)
4115 (unless (zerop (tramp-send-command-and-check
4119 (if recursive
"rm -rf" "rmdir")
4120 (tramp-shell-quote-argument localname
))))
4121 (tramp-error v
'file-error
"Couldn't delete %s" directory
))))
4123 (defun tramp-handle-delete-file (filename &optional trash
)
4124 "Like `delete-file' for Tramp files."
4125 (setq filename
(expand-file-name filename
))
4126 (with-parsed-tramp-file-name filename nil
4127 (tramp-flush-file-property v
(file-name-directory localname
))
4128 (tramp-flush-file-property v localname
)
4131 (tramp-send-command-and-check
4133 (or (and trash
(tramp-get-remote-trash v
)) "rm -f")
4134 (tramp-shell-quote-argument localname
))))
4135 (tramp-error v
'file-error
"Couldn't delete %s" filename
))))
4139 ;; CCC: This does not seem to be enough. Something dies when
4140 ;; we try and delete two directories under Tramp :/
4141 (defun tramp-handle-dired-recursive-delete-directory (filename)
4142 "Recursively delete the directory given.
4143 This is like `dired-recursive-delete-directory' for Tramp files."
4144 (with-parsed-tramp-file-name filename nil
4145 ;; Run a shell command 'rm -r <localname>'
4146 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
4147 (unless (file-exists-p filename
)
4148 (tramp-error v
'file-error
"No such directory: %s" filename
))
4149 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
4152 (format "rm -rf %s" (tramp-shell-quote-argument localname
))
4153 ;; Don't read the output, do it explicitely.
4155 ;; Wait for the remote system to return to us...
4156 ;; This might take a while, allow it plenty of time.
4157 (tramp-wait-for-output (tramp-get-connection-process v
) 120)
4158 ;; Make sure that it worked...
4159 (tramp-flush-file-property v
(file-name-directory localname
))
4160 (tramp-flush-directory-property v localname
)
4161 (and (file-exists-p filename
)
4163 v
'file-error
"Failed to recursively delete %s" filename
))))
4165 (defun tramp-handle-dired-compress-file (file &rest ok-flag
)
4166 "Like `dired-compress-file' for Tramp files."
4167 ;; OK-FLAG is valid for XEmacs only, but not implemented.
4168 ;; Code stolen mainly from dired-aux.el.
4169 (with-parsed-tramp-file-name file nil
4170 (tramp-flush-file-property v localname
)
4173 (if (not (featurep 'xemacs
))
4175 (symbol-value 'dired-compress-file-suffixes
)
4176 ;; XEmacs has `dired-compression-method-alist', which is
4177 ;; transformed into `dired-compress-file-suffixes' structure.
4180 (list (concat (regexp-quote (nth 1 x
)) "\\'")
4182 (mapconcat 'identity
(nth 3 x
) " ")))
4183 (symbol-value 'dired-compression-method-alist
))))
4185 ;; See if any suffix rule matches this file name.
4187 (let (case-fold-search)
4188 (if (string-match (car (car suffixes
)) localname
)
4189 (setq suffix
(car suffixes
) suffixes nil
))
4190 (setq suffixes
(cdr suffixes
))))
4192 (cond ((file-symlink-p file
)
4194 ((and suffix
(nth 2 suffix
))
4195 ;; We found an uncompression rule.
4196 (with-progress-reporter v
0 (format "Uncompressing %s" file
)
4198 (tramp-send-command-and-check
4199 v
(concat (nth 2 suffix
) " "
4200 (tramp-shell-quote-argument localname
))))
4201 ;; `dired-remove-file' is not defined in XEmacs.
4202 (tramp-compat-funcall 'dired-remove-file file
)
4203 (string-match (car suffix
) file
)
4204 (concat (substring file
0 (match-beginning 0))))))
4206 ;; We don't recognize the file as compressed, so compress it.
4208 (with-progress-reporter v
0 (format "Compressing %s" file
)
4210 (tramp-send-command-and-check
4211 v
(concat "gzip -f "
4212 (tramp-shell-quote-argument localname
))))
4213 ;; `dired-remove-file' is not defined in XEmacs.
4214 (tramp-compat-funcall 'dired-remove-file file
)
4215 (cond ((file-exists-p (concat file
".gz"))
4216 (concat file
".gz"))
4217 ((file-exists-p (concat file
".z"))
4221 (defun tramp-handle-dired-uncache (dir &optional dir-p
)
4222 "Like `dired-uncache' for Tramp files."
4223 ;; DIR-P is valid for XEmacs only.
4224 (with-parsed-tramp-file-name
4225 (if (or dir-p
(file-directory-p dir
)) dir
(file-name-directory dir
)) nil
4226 (tramp-flush-file-property v localname
)))
4228 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
4229 ;; not sure at all that this is the right way to do it, but let's hope
4230 ;; it works for now, and wait for a guru to point out the Right Way to
4232 ;;(eval-when-compile
4233 ;; (unless (fboundp 'dired-insert-set-properties)
4234 ;; (fset 'dired-insert-set-properties 'ignore)))
4235 ;; Gerd suggests this:
4236 (eval-when-compile (require 'dired
))
4237 ;; Note that dired is required at run-time, too, when it is needed.
4238 ;; It is only needed on XEmacs for the function
4239 ;; `dired-insert-set-properties'.
4241 (defun tramp-handle-insert-directory
4242 (filename switches
&optional wildcard full-directory-p
)
4243 "Like `insert-directory' for Tramp files."
4244 (setq filename
(expand-file-name filename
))
4245 (with-parsed-tramp-file-name filename nil
4246 (if (and (featurep 'ls-lisp
)
4247 (not (symbol-value 'ls-lisp-use-insert-directory-program
)))
4248 (tramp-run-real-handler
4249 'insert-directory
(list filename switches wildcard full-directory-p
))
4250 (when (stringp switches
)
4251 (setq switches
(split-string switches
)))
4252 (when (and (member "--dired" switches
)
4253 (not (tramp-get-ls-command-with-dired v
)))
4254 (setq switches
(delete "--dired" switches
)))
4256 (setq wildcard
(tramp-run-real-handler
4257 'file-name-nondirectory
(list localname
)))
4258 (setq localname
(tramp-run-real-handler
4259 'file-name-directory
(list localname
))))
4260 (unless full-directory-p
4261 (setq switches
(add-to-list 'switches
"-d" 'append
)))
4262 (setq switches
(mapconcat 'tramp-shell-quote-argument switches
" "))
4264 (setq switches
(concat switches
" " wildcard
)))
4266 v
4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
4267 switches filename
(if wildcard
"yes" "no")
4268 (if full-directory-p
"yes" "no"))
4269 ;; If `full-directory-p', we just say `ls -l FILENAME'.
4270 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
4271 (if full-directory-p
4274 (format "%s %s %s 2>/dev/null"
4275 (tramp-get-ls-command v
)
4279 (tramp-shell-quote-argument (concat localname
".")))))
4280 (tramp-barf-unless-okay
4282 (format "cd %s" (tramp-shell-quote-argument
4283 (tramp-run-real-handler
4284 'file-name-directory
(list localname
))))
4286 (tramp-shell-quote-argument
4287 (tramp-run-real-handler 'file-name-directory
(list localname
))))
4291 (tramp-get-ls-command v
)
4295 (tramp-run-real-handler
4296 'file-name-nondirectory
(list localname
)))))
4298 (tramp-shell-quote-argument
4299 (tramp-run-real-handler
4300 'file-name-nondirectory
(list localname
)))))))
4301 (let ((beg (point)))
4302 ;; We cannot use `insert-buffer-substring' because the Tramp
4303 ;; buffer changes its contents before insertion due to calling
4304 ;; `expand-file' and alike.
4306 (with-current-buffer (tramp-get-buffer v
)
4309 ;; Check for "--dired" output.
4311 (when (looking-at "//SUBDIRED//")
4313 (when (looking-at "//DIRED//\\s-+")
4314 (let ((databeg (match-end 0))
4315 (end (tramp-compat-line-end-position)))
4316 ;; Now read the numeric positions of file names.
4318 (while (< (point) end
)
4319 (let ((start (+ beg
(read (current-buffer))))
4320 (end (+ beg
(read (current-buffer)))))
4321 (if (memq (char-after end
) '(?
\n ?\
))
4322 ;; End is followed by \n or by " -> ".
4323 (put-text-property start end
'dired-filename t
))))))
4324 ;; Remove trailing lines.
4325 (goto-char (tramp-compat-line-beginning-position))
4326 (while (looking-at "//")
4328 (delete-region (match-beginning 0) (point)))
4330 ;; The inserted file could be from somewhere else.
4331 (when (and (not wildcard
) (not full-directory-p
))
4332 (goto-char (point-max))
4333 (when (file-symlink-p filename
)
4334 (goto-char (search-backward "->" beg
'noerror
)))
4336 (if (zerop (length (file-name-nondirectory filename
)))
4338 (file-name-nondirectory filename
))
4340 (replace-match (file-relative-name filename
) t
))
4342 (goto-char (point-max))))))
4344 (defun tramp-handle-unhandled-file-name-directory (filename)
4345 "Like `unhandled-file-name-directory' for Tramp files."
4346 ;; With Emacs 23, we could simply return `nil'. But we must keep it
4347 ;; for backward compatibility.
4348 (expand-file-name "~/"))
4350 ;; Canonicalization of file names.
4352 (defun tramp-handle-expand-file-name (name &optional dir
)
4353 "Like `expand-file-name' for Tramp files.
4354 If the localname part of the given filename starts with \"/../\" then
4355 the result will be a local, non-Tramp, filename."
4356 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
4357 (setq dir
(or dir default-directory
"/"))
4358 ;; Unless NAME is absolute, concat DIR and NAME.
4359 (unless (file-name-absolute-p name
)
4360 (setq name
(concat (file-name-as-directory dir
) name
)))
4361 ;; If NAME is not a Tramp file, run the real handler.
4362 (if (not (tramp-connectable-p name
))
4363 (tramp-run-real-handler 'expand-file-name
(list name nil
))
4365 (with-parsed-tramp-file-name name nil
4366 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
4367 (setq localname
(concat "~/" localname
)))
4368 ;; Tilde expansion if necessary. This needs a shell which
4369 ;; groks tilde expansion! The function `tramp-find-shell' is
4370 ;; supposed to find such a shell on the remote host. Please
4371 ;; tell me about it when this doesn't work on your system.
4372 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname
)
4373 (let ((uname (match-string 1 localname
))
4374 (fname (match-string 2 localname
)))
4375 ;; We cannot simply apply "~/", because under sudo "~/" is
4376 ;; expanded to the local user home directory but to the
4377 ;; root home directory. On the other hand, using always
4378 ;; the default user name for tilde expansion is not
4379 ;; appropriate either, because ssh and companions might
4380 ;; use a user name from the config file.
4381 (when (and (string-equal uname
"~")
4382 (string-match "\\`su\\(do\\)?\\'" method
))
4383 (setq uname
(concat uname user
)))
4385 (with-connection-property v uname
4387 v
(format "cd %s; pwd" (tramp-shell-quote-argument uname
)))
4388 (with-current-buffer (tramp-get-buffer v
)
4389 (goto-char (point-min))
4391 (point) (tramp-compat-line-end-position)))))
4392 (setq localname
(concat uname fname
))))
4393 ;; There might be a double slash, for example when "~/"
4394 ;; expands to "/". Remove this.
4395 (while (string-match "//" localname
)
4396 (setq localname
(replace-match "/" t t localname
)))
4397 ;; No tilde characters in file name, do normal
4398 ;; `expand-file-name' (this does "/./" and "/../"). We bind
4399 ;; `directory-sep-char' here for XEmacs on Windows, which would
4400 ;; otherwise use backslash. `default-directory' is bound,
4401 ;; because on Windows there would be problems with UNC shares or
4403 (let ((directory-sep-char ?
/)
4404 (default-directory (tramp-compat-temporary-file-directory)))
4405 (tramp-make-tramp-file-name
4407 (tramp-drop-volume-letter
4408 (tramp-run-real-handler
4409 'expand-file-name
(list localname
))))))))
4411 (defun tramp-replace-environment-variables (filename)
4412 "Replace environment variables in FILENAME.
4413 Return the string with the replaced variables."
4415 (let ((idx (string-match "$\\(\\w+\\)" filename
)))
4416 ;; `$' is coded as `$$'.
4418 (or (zerop idx
) (not (eq ?$
(aref filename
(1- idx
)))))
4419 (getenv (match-string 1 filename
)))
4422 (substitute-in-file-name (match-string 0 filename
))
4426 (defun tramp-handle-substitute-in-file-name (filename)
4427 "Like `substitute-in-file-name' for Tramp files.
4428 \"//\" and \"/~\" substitute only in the local filename part.
4429 If the URL Tramp syntax is chosen, \"//\" as method delimeter and \"/~\" at
4430 beginning of local filename are not substituted."
4431 ;; First, we must replace environment variables.
4432 (setq filename
(tramp-replace-environment-variables filename
))
4433 (with-parsed-tramp-file-name filename nil
4434 (if (equal tramp-syntax
'url
)
4435 ;; We need to check localname only. The other parts cannot contain
4437 (if (and (> (length localname
) 1)
4438 (or (string-match "//" localname
)
4439 (string-match "/~" localname
1)))
4440 (tramp-run-real-handler 'substitute-in-file-name
(list filename
))
4441 (tramp-make-tramp-file-name
4442 (when method
(substitute-in-file-name method
))
4443 (when user
(substitute-in-file-name user
))
4444 (when host
(substitute-in-file-name host
))
4446 (tramp-run-real-handler
4447 'substitute-in-file-name
(list localname
)))))
4448 ;; Ignore in LOCALNAME everything before "//" or "/~".
4449 (when (and (stringp localname
) (string-match ".+?/\\(/\\|~\\)" localname
))
4451 (concat (file-remote-p filename
)
4452 (replace-match "\\1" nil nil localname
)))
4453 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
4454 (when (string-match "~$" filename
)
4455 (setq filename
(concat filename
"/"))))
4456 (tramp-run-real-handler 'substitute-in-file-name
(list filename
)))))
4458 ;; In XEmacs, electricity is implemented via a key map for ?/ and ?~,
4459 ;; which calls corresponding functions (see minibuf.el).
4460 (when (fboundp 'minibuffer-electric-separator
)
4465 (around ,(intern (format "tramp-advice-%s" x
)) activate
)
4466 "Invoke `substitute-in-file-name' for Tramp files."
4467 (if (and (symbol-value 'minibuffer-electric-file-name-behavior
)
4468 (tramp-tramp-file-p (buffer-substring)))
4469 ;; We don't need to handle `last-input-event', because
4470 ;; due to the key map we know it must be ?/ or ?~.
4471 (let ((s (concat (buffer-substring (point-min) (point))
4472 (string last-command-char
))))
4473 (delete-region (point-min) (point))
4474 (insert (substitute-in-file-name s
))
4475 (setq ad-return-value last-command-char
))
4481 (ad-remove-advice ',x
'around
',(intern (format "tramp-advice-%s" x
)))
4482 (ad-activate ',x
)))))
4484 '(minibuffer-electric-separator
4485 minibuffer-electric-tilde
)))
4488 ;;; Remote commands:
4490 (defun tramp-handle-executable-find (command)
4491 "Like `executable-find' for Tramp files."
4492 (with-parsed-tramp-file-name default-directory nil
4493 (tramp-find-executable v command
(tramp-get-remote-path v
) t
)))
4495 ;; We use BUFFER also as connection buffer during setup. Because of
4496 ;; this, its original contents must be saved, and restored once
4497 ;; connection has been setup.
4498 (defun tramp-handle-start-file-process (name buffer program
&rest args
)
4499 "Like `start-file-process' for Tramp files."
4500 (with-parsed-tramp-file-name default-directory nil
4501 (unless (stringp program
)
4503 v
'file-error
"pty association is not supported for `%s'" name
))
4505 (let ((command (format "cd %s; exec %s"
4506 (tramp-shell-quote-argument localname
)
4507 (mapconcat 'tramp-shell-quote-argument
4508 (cons program args
) " ")))
4512 ;; BUFFER can be nil. We use a temporary buffer.
4513 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
4514 (while (get-process name1
)
4515 ;; NAME must be unique as process name.
4517 name1
(format "%s<%d>" name i
)))
4519 ;; Set the new process properties.
4520 (tramp-set-connection-property v
"process-name" name
)
4521 (tramp-set-connection-property v
"process-buffer" buffer
)
4522 ;; Activate narrowing in order to save BUFFER contents.
4523 ;; Clear also the modification time; otherwise we might be
4524 ;; interrupted by `verify-visited-file-modtime'.
4525 (with-current-buffer (tramp-get-connection-buffer v
)
4526 (clear-visited-file-modtime)
4527 (narrow-to-region (point-max) (point-max)))
4528 ;; Send the command. `tramp-send-command' opens a new
4530 (tramp-send-command v command nil t
) ; nooutput
4531 ;; Set query flag for this process.
4532 (tramp-set-process-query-on-exit-flag
4533 (tramp-get-connection-process v
) t
)
4535 (tramp-get-connection-process v
))
4537 (with-current-buffer (tramp-get-connection-buffer v
)
4538 (if (string-match tramp-temp-buffer-name
(buffer-name))
4540 (set-process-buffer (tramp-get-connection-process v
) nil
)
4541 (kill-buffer (current-buffer)))
4543 (goto-char (point-max))))
4544 (tramp-set-connection-property v
"process-name" nil
)
4545 (tramp-set-connection-property v
"process-buffer" nil
))))
4547 (defun tramp-handle-process-file
4548 (program &optional infile destination display
&rest args
)
4549 "Like `process-file' for Tramp files."
4550 ;; The implementation is not complete yet.
4551 (when (and (numberp destination
) (zerop destination
))
4552 (error "Implementation does not handle immediate return"))
4554 (with-parsed-tramp-file-name default-directory nil
4555 (let (command input tmpinput stderr tmpstderr outbuf ret
)
4557 (setq command
(mapconcat 'tramp-shell-quote-argument
4558 (cons program args
) " "))
4561 (setq input
"/dev/null")
4562 (setq infile
(expand-file-name infile
))
4563 (if (tramp-equal-remote default-directory infile
)
4564 ;; INFILE is on the same remote host.
4565 (setq input
(with-parsed-tramp-file-name infile nil localname
))
4566 ;; INFILE must be copied to remote host.
4567 (setq input
(tramp-make-tramp-temp-file v
)
4568 tmpinput
(tramp-make-tramp-file-name method user host input
))
4569 (copy-file infile tmpinput t
)))
4570 (when input
(setq command
(format "%s <%s" command input
)))
4572 ;; Determine output.
4575 ((bufferp destination
)
4576 (setq outbuf destination
))
4578 ((stringp destination
)
4579 (setq outbuf
(get-buffer-create destination
)))
4580 ;; (REAL-DESTINATION ERROR-DESTINATION)
4581 ((consp destination
)
4584 ((bufferp (car destination
))
4585 (setq outbuf
(car destination
)))
4586 ((stringp (car destination
))
4587 (setq outbuf
(get-buffer-create (car destination
))))
4589 (setq outbuf
(current-buffer))))
4592 ((stringp (cadr destination
))
4593 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
4594 (if (tramp-equal-remote default-directory
(cadr destination
))
4595 ;; stderr is on the same remote host.
4596 (setq stderr
(with-parsed-tramp-file-name
4597 (cadr destination
) nil localname
))
4598 ;; stderr must be copied to remote host. The temporary
4599 ;; file must be deleted after execution.
4600 (setq stderr
(tramp-make-tramp-temp-file v
)
4601 tmpstderr
(tramp-make-tramp-file-name
4602 method user host stderr
))))
4603 ;; stderr to be discarded.
4604 ((null (cadr destination
))
4605 (setq stderr
"/dev/null"))))
4608 (setq outbuf
(current-buffer))))
4609 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
4611 ;; Send the command. It might not return in time, so we protect it.
4615 (tramp-send-command-and-check
4616 v
(format "\\cd %s; %s"
4617 (tramp-shell-quote-argument localname
)
4620 ;; We should show the output anyway.
4622 (with-current-buffer outbuf
4624 (with-current-buffer (tramp-get-connection-buffer v
)
4626 (when display
(display-buffer outbuf
))))
4627 ;; When the user did interrupt, we should do it also. We use
4628 ;; return code -1 as marker.
4630 (kill-buffer (tramp-get-connection-buffer v
))
4634 (kill-buffer (tramp-get-connection-buffer v
))
4637 ;; Provide error file.
4638 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
4640 ;; Cleanup. We remove all file cache values for the connection,
4641 ;; because the remote process could have changed them.
4642 (when tmpinput
(delete-file tmpinput
))
4644 ;; `process-file-side-effects' has been introduced with GNU
4645 ;; Emacs 23.2. If set to `nil', no remote file will be changed
4646 ;; by `program'. If it doesn't exist, we assume its default
4648 (unless (and (boundp 'process-file-side-effects
)
4649 (not (symbol-value 'process-file-side-effects
)))
4650 (tramp-flush-directory-property v
""))
4652 ;; Return exit status.
4657 (defun tramp-local-call-process
4658 (program &optional infile destination display
&rest args
)
4659 "Calls `call-process' on the local host.
4660 This is needed because for some Emacs flavors Tramp has
4661 defadviced `call-process' to behave like `process-file'. The
4662 Lisp error raised when PROGRAM is nil is trapped also, returning 1."
4663 (let ((default-directory
4664 (if (file-remote-p default-directory
)
4665 (tramp-compat-temporary-file-directory)
4666 default-directory
)))
4667 (if (executable-find program
)
4668 (apply 'call-process program infile destination display args
)
4671 (defun tramp-handle-call-process-region
4672 (start end program
&optional delete buffer display
&rest args
)
4673 "Like `call-process-region' for Tramp files."
4674 (let ((tmpfile (tramp-compat-make-temp-file "")))
4675 (write-region start end tmpfile
)
4676 (when delete
(delete-region start end
))
4678 (apply 'call-process program tmpfile buffer display args
)
4679 (delete-file tmpfile
))))
4681 (defun tramp-handle-shell-command
4682 (command &optional output-buffer error-buffer
)
4683 "Like `shell-command' for Tramp files."
4684 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
4685 ;; We cannot use `shell-file-name' and `shell-command-switch',
4686 ;; they are variables of the local host.
4688 (tramp-get-method-parameter
4689 (tramp-file-name-method
4690 (tramp-dissect-file-name default-directory
))
4692 "-c" (substring command
0 asynchronous
)))
4696 ((bufferp output-buffer
) output-buffer
)
4697 ((stringp output-buffer
) (get-buffer-create output-buffer
))
4699 (setq current-buffer-p t
)
4701 (t (get-buffer-create
4703 "*Async Shell Command*"
4704 "*Shell Command Output*")))))
4707 ((bufferp error-buffer
) error-buffer
)
4708 ((stringp error-buffer
) (get-buffer-create error-buffer
))))
4710 (if (and (not asynchronous
) error-buffer
)
4711 (with-parsed-tramp-file-name default-directory nil
4712 (list output-buffer
(tramp-make-tramp-temp-file v
)))
4714 (p (get-buffer-process output-buffer
)))
4716 ;; Check whether there is another process running. Tramp does not
4717 ;; support 2 (asynchronous) processes in parallel.
4719 (if (yes-or-no-p "A command is running. Kill it? ")
4723 (error "Shell command in progress")))
4725 (if current-buffer-p
4727 (barf-if-buffer-read-only)
4729 (with-current-buffer output-buffer
4730 (setq buffer-read-only nil
)
4733 (if (and (not current-buffer-p
) (integerp asynchronous
))
4736 (apply 'start-file-process
"*Async Shell*" buffer args
)
4738 (pop-to-buffer output-buffer
)
4739 (setq mode-line-process
'(":%s"))
4740 (require 'shell
) (shell-mode))
4744 (apply 'process-file
(car args
) nil buffer nil
(cdr args
))
4745 ;; Insert error messages if they were separated.
4746 (when (listp buffer
)
4747 (with-current-buffer error-buffer
4748 (insert-file-contents (cadr buffer
)))
4749 (delete-file (cadr buffer
)))
4750 (if current-buffer-p
4751 ;; This is like exchange-point-and-mark, but doesn't
4752 ;; activate the mark. It is cleaner to avoid activation,
4753 ;; even though the command loop would deactivate the mark
4754 ;; because we inserted text.
4755 (goto-char (prog1 (mark t
)
4756 (set-marker (mark-marker) (point)
4758 ;; There's some output, display it.
4759 (when (with-current-buffer output-buffer
(> (point-max) (point-min)))
4760 (if (functionp 'display-message-or-buffer
)
4761 (tramp-compat-funcall 'display-message-or-buffer output-buffer
)
4762 (pop-to-buffer output-buffer
))))))))
4766 (defvar tramp-handle-file-local-copy-hook nil
4767 "Normal hook to be run at the end of `tramp-handle-file-local-copy'.")
4769 (defun tramp-handle-file-local-copy (filename)
4770 "Like `file-local-copy' for Tramp files."
4772 (with-parsed-tramp-file-name filename nil
4773 (unless (file-exists-p filename
)
4776 "Cannot make local copy of non-existing file `%s'" filename
))
4778 (let* ((size (nth 7 (file-attributes filename
)))
4779 (rem-enc (tramp-get-inline-coding v
"remote-encoding" size
))
4780 (loc-dec (tramp-get-inline-coding v
"local-decoding" size
))
4781 (tmpfile (tramp-compat-make-temp-file filename
)))
4785 ;; `copy-file' handles direct copy and out-of-band methods.
4786 ((or (tramp-local-host-p v
)
4787 (tramp-method-out-of-band-p v size
))
4788 (copy-file filename tmpfile t t
))
4790 ;; Use inline encoding for file transfer.
4793 (with-progress-reporter
4794 v
3 (format "Encoding remote file %s" filename
)
4795 (tramp-barf-unless-okay
4796 v
(format rem-enc
(tramp-shell-quote-argument localname
))
4797 "Encoding remote file failed"))
4799 (if (functionp loc-dec
)
4800 ;; If local decoding is a function, we call it. We
4801 ;; must disable multibyte, because
4802 ;; `uudecode-decode-region' doesn't handle it
4805 (set-buffer-multibyte nil
)
4806 (insert-buffer-substring (tramp-get-buffer v
))
4807 (with-progress-reporter
4808 v
3 (format "Decoding remote file %s with function %s"
4810 (funcall loc-dec
(point-min) (point-max))
4811 ;; Unset `file-name-handler-alist'. Otherwise,
4812 ;; epa-file gets confused.
4813 (let (file-name-handler-alist
4814 (coding-system-for-write 'binary
))
4815 (write-region (point-min) (point-max) tmpfile
))))
4817 ;; If tramp-decoding-function is not defined for this
4818 ;; method, we invoke tramp-decoding-command instead.
4819 (let ((tmpfile2 (tramp-compat-make-temp-file filename
)))
4820 ;; Unset `file-name-handler-alist'. Otherwise,
4821 ;; epa-file gets confused.
4822 (let (file-name-handler-alist
4823 (coding-system-for-write 'binary
))
4824 (write-region (point-min) (point-max) tmpfile2
))
4825 (with-progress-reporter
4826 v
3 (format "Decoding remote file %s with command %s"
4829 (tramp-call-local-coding-command
4830 loc-dec tmpfile2 tmpfile
)
4831 (delete-file tmpfile2
)))))
4833 ;; Set proper permissions.
4834 (set-file-modes tmpfile
(tramp-default-file-modes filename
))
4835 ;; Set local user ownership.
4836 (tramp-set-file-uid-gid tmpfile
)))
4838 ;; Oops, I don't know what to do.
4840 v
'file-error
"Wrong method specification for `%s'" method
)))
4844 (delete-file tmpfile
)
4845 (signal (car err
) (cdr err
))))
4847 (run-hooks 'tramp-handle-file-local-copy-hook
)
4850 (defun tramp-handle-file-remote-p (filename &optional identification connected
)
4851 "Like `file-remote-p' for Tramp files."
4852 (let ((tramp-verbose 3))
4853 (when (tramp-tramp-file-p filename
)
4854 (let* ((v (tramp-dissect-file-name filename
))
4855 (p (tramp-get-connection-process v
))
4856 (c (and p
(processp p
) (memq (process-status p
) '(run open
)))))
4857 ;; We expand the file name only, if there is already a connection.
4858 (with-parsed-tramp-file-name
4859 (if c
(expand-file-name filename
) filename
) nil
4860 (and (or (not connected
) c
)
4862 ((eq identification
'method
) method
)
4863 ((eq identification
'user
) user
)
4864 ((eq identification
'host
) host
)
4865 ((eq identification
'localname
) localname
)
4866 (t (tramp-make-tramp-file-name method user host
"")))))))))
4868 (defun tramp-find-file-name-coding-system-alist (filename tmpname
)
4869 "Like `find-operation-coding-system' for Tramp filenames.
4870 Tramp's `insert-file-contents' and `write-region' work over
4871 temporary file names. If `file-coding-system-alist' contains an
4872 expression, which matches more than the file name suffix, the
4873 coding system might not be determined. This function repairs it."
4875 (dolist (elt file-coding-system-alist result
)
4876 (when (and (consp elt
) (string-match (car elt
) filename
))
4877 ;; We found a matching entry in `file-coding-system-alist'.
4878 ;; So we add a similar entry, but with the temporary file name
4881 'result
(cons (regexp-quote tmpname
) (cdr elt
)) 'append
)))))
4883 (defun tramp-handle-insert-file-contents
4884 (filename &optional visit beg end replace
)
4885 "Like `insert-file-contents' for Tramp files."
4886 (barf-if-buffer-read-only)
4887 (setq filename
(expand-file-name filename
))
4888 (let (result local-copy remote-copy
)
4889 (with-parsed-tramp-file-name filename nil
4891 (if (not (file-exists-p filename
))
4892 ;; We don't raise a Tramp error, because it might be
4893 ;; suppressed, like in `find-file-noselect-1'.
4895 (list "File not found on remote host" filename
))
4897 (if (and (tramp-local-host-p v
)
4898 (let (file-name-handler-alist)
4899 (file-readable-p localname
)))
4900 ;; Short track: if we are on the local host, we can
4903 (tramp-run-real-handler
4904 'insert-file-contents
4905 (list localname visit beg end replace
)))
4907 ;; When we shall insert only a part of the file, we copy
4910 (setq remote-copy
(tramp-make-tramp-temp-file v
))
4915 (format "tail -c +%d %s | head -c +%d >%s"
4916 (1+ beg
) (tramp-shell-quote-argument localname
)
4917 (- end beg
) remote-copy
))
4919 (format "tail -c +%d %s >%s"
4920 (1+ beg
) (tramp-shell-quote-argument localname
)
4923 (format "head -c +%d %s >%s"
4924 (1+ end
) (tramp-shell-quote-argument localname
)
4927 ;; `insert-file-contents-literally' takes care to avoid
4928 ;; calling jka-compr. By let-binding
4929 ;; `inhibit-file-name-operation', we propagate that care
4930 ;; to the `file-local-copy' operation.
4932 (let ((inhibit-file-name-operation
4933 (when (eq inhibit-file-name-operation
4934 'insert-file-contents
)
4937 ((stringp remote-copy
)
4939 (tramp-make-tramp-file-name
4940 method user host remote-copy
)))
4941 ((stringp tramp-temp-buffer-file-name
)
4942 (copy-file filename tramp-temp-buffer-file-name
'ok
)
4943 tramp-temp-buffer-file-name
)
4944 (t (file-local-copy filename
)))))
4946 ;; When the file is not readable for the owner, it
4947 ;; cannot be inserted, even it is redable for the group
4948 ;; or for everybody.
4949 (set-file-modes local-copy
(tramp-octal-to-decimal "0600"))
4951 (when (and (null remote-copy
)
4952 (tramp-get-method-parameter
4953 method
'tramp-copy-keep-tmpfile
))
4954 ;; We keep the local file for performance reasons,
4955 ;; useful for "rsync".
4956 (setq tramp-temp-buffer-file-name local-copy
)
4957 (put 'tramp-temp-buffer-file-name
'permanent-local t
))
4959 (with-progress-reporter
4960 v
3 (format "Inserting local temp file `%s'" local-copy
)
4961 ;; We must ensure that `file-coding-system-alist'
4962 ;; matches `local-copy'.
4963 (let ((file-coding-system-alist
4964 (tramp-find-file-name-coding-system-alist
4965 filename local-copy
)))
4967 (insert-file-contents
4968 local-copy nil nil nil replace
))))))
4973 (setq buffer-file-name filename
)
4974 (setq buffer-read-only
(not (file-writable-p filename
)))
4975 (set-visited-file-modtime)
4976 (set-buffer-modified-p nil
))
4977 (when (and (stringp local-copy
)
4978 (or remote-copy
(null tramp-temp-buffer-file-name
)))
4979 (delete-file local-copy
))
4980 (when (stringp remote-copy
)
4982 (tramp-make-tramp-file-name method user host remote-copy
))))))
4985 (list (expand-file-name filename
)
4988 ;; This is needed for XEmacs only. Code stolen from files.el.
4989 (defun tramp-handle-insert-file-contents-literally
4990 (filename &optional visit beg end replace
)
4991 "Like `insert-file-contents-literally' for Tramp files."
4992 (let ((format-alist nil
)
4993 (after-insert-file-functions nil
)
4994 (coding-system-for-read 'no-conversion
)
4995 (coding-system-for-write 'no-conversion
)
4996 (find-buffer-file-type-function
4997 (if (fboundp 'find-buffer-file-type
)
4998 (symbol-function 'find-buffer-file-type
)
5000 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler
))
5001 (inhibit-file-name-operation 'insert-file-contents
))
5004 (fset 'find-buffer-file-type
(lambda (filename) t
))
5005 (insert-file-contents filename visit beg end replace
))
5007 (if find-buffer-file-type-function
5008 (fset 'find-buffer-file-type find-buffer-file-type-function
)
5009 (fmakunbound 'find-buffer-file-type
)))))
5011 (defun tramp-handle-find-backup-file-name (filename)
5012 "Like `find-backup-file-name' for Tramp files."
5013 (with-parsed-tramp-file-name filename nil
5014 ;; We set both variables. It doesn't matter whether it is
5016 (let ((backup-directory-alist
5018 (when (boundp 'backup-directory-alist
)
5019 (if (symbol-value 'tramp-backup-directory-alist
)
5024 (if (and (stringp (cdr x
))
5025 (file-name-absolute-p (cdr x
))
5026 (not (tramp-file-name-p (cdr x
))))
5027 (tramp-make-tramp-file-name method user host
(cdr x
))
5029 (symbol-value 'tramp-backup-directory-alist
))
5030 (symbol-value 'backup-directory-alist
))))
5032 (bkup-backup-directory-info
5034 (when (boundp 'bkup-backup-directory-info
)
5035 (if (symbol-value 'tramp-bkup-backup-directory-info
)
5041 (if (and (stringp (car (cdr x
)))
5042 (file-name-absolute-p (car (cdr x
)))
5043 (not (tramp-file-name-p (car (cdr x
)))))
5044 (tramp-make-tramp-file-name
5045 method user host
(car (cdr x
)))
5048 (symbol-value 'tramp-bkup-backup-directory-info
))
5049 (symbol-value 'bkup-backup-directory-info
)))))
5051 (tramp-run-real-handler 'find-backup-file-name
(list filename
)))))
5053 (defun tramp-handle-make-auto-save-file-name ()
5054 "Like `make-auto-save-file-name' for Tramp files.
5055 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
5056 (let ((tramp-auto-save-directory tramp-auto-save-directory
)
5058 (tramp-subst-strs-in-string
5065 (buffer-file-name))))
5066 ;; File name must be unique. This is ensured with Emacs 22 (see
5067 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
5068 ;; all other cases we must do it ourselves.
5069 (when (boundp 'auto-save-file-name-transforms
)
5072 (when (and (string-match (car x
) buffer-file-name
)
5073 (not (car (cddr x
))))
5074 (setq tramp-auto-save-directory
5075 (or tramp-auto-save-directory
5076 (tramp-compat-temporary-file-directory)))))
5077 (symbol-value 'auto-save-file-name-transforms
)))
5078 ;; Create directory.
5079 (when tramp-auto-save-directory
5080 (setq buffer-file-name
5081 (expand-file-name buffer-file-name tramp-auto-save-directory
))
5082 (unless (file-exists-p tramp-auto-save-directory
)
5083 (make-directory tramp-auto-save-directory t
)))
5084 ;; Run plain `make-auto-save-file-name'. There might be an advice when
5085 ;; it is not a magic file name operation (since Emacs 22).
5086 ;; We must deactivate it temporarily.
5087 (if (not (ad-is-active 'make-auto-save-file-name
))
5088 (tramp-run-real-handler 'make-auto-save-file-name nil
)
5090 (ad-deactivate 'make-auto-save-file-name
)
5092 (tramp-run-real-handler 'make-auto-save-file-name nil
)
5093 (ad-activate 'make-auto-save-file-name
)))))
5095 (defvar tramp-handle-write-region-hook nil
5096 "Normal hook to be run at the end of `tramp-handle-write-region'.")
5098 ;; CCC grok LOCKNAME
5099 (defun tramp-handle-write-region
5100 (start end filename
&optional append visit lockname confirm
)
5101 "Like `write-region' for Tramp files."
5102 (setq filename
(expand-file-name filename
))
5103 (with-parsed-tramp-file-name filename nil
5104 ;; Following part commented out because we don't know what to do about
5105 ;; file locking, and it does not appear to be a problem to ignore it.
5106 ;; Ange-ftp ignores it, too.
5107 ;; (when (and lockname (stringp lockname))
5108 ;; (setq lockname (expand-file-name lockname)))
5109 ;; (unless (or (eq lockname nil)
5110 ;; (string= lockname filename))
5112 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
5114 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
5115 (when (and (not (featurep 'xemacs
)) confirm
(file-exists-p filename
))
5116 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename
))
5117 (tramp-error v
'file-error
"File not overwritten")))
5119 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename
'integer
))
5120 (tramp-get-remote-uid v
'integer
)))
5121 (gid (or (nth 3 (tramp-compat-file-attributes filename
'integer
))
5122 (tramp-get-remote-gid v
'integer
))))
5124 (if (and (tramp-local-host-p v
)
5125 ;; `file-writable-p' calls `file-expand-file-name'. We
5126 ;; cannot use `tramp-run-real-handler' therefore.
5127 (let (file-name-handler-alist)
5129 (file-writable-p (file-name-directory localname
))
5130 (or (file-directory-p localname
)
5131 (file-writable-p localname
)))))
5132 ;; Short track: if we are on the local host, we can run directly.
5133 (tramp-run-real-handler
5135 (list start end localname append
'no-message lockname confirm
))
5137 (let ((modes (save-excursion (tramp-default-file-modes filename
)))
5138 ;; We use this to save the value of
5139 ;; `last-coding-system-used' after writing the tmp
5140 ;; file. At the end of the function, we set
5141 ;; `last-coding-system-used' to this saved value. This
5142 ;; way, any intermediary coding systems used while
5143 ;; talking to the remote shell or suchlike won't hose
5144 ;; this variable. This approach was snarfed from
5147 ;; Write region into a tmp file. This isn't really
5148 ;; needed if we use an encoding function, but currently
5149 ;; we use it always because this makes the logic
5151 (tmpfile (or tramp-temp-buffer-file-name
5152 (tramp-compat-make-temp-file filename
))))
5154 ;; If `append' is non-nil, we copy the file locally, and let
5155 ;; the native `write-region' implementation do the job.
5156 (when append
(copy-file filename tmpfile
'ok
))
5158 ;; We say `no-message' here because we don't want the
5159 ;; visited file modtime data to be clobbered from the temp
5160 ;; file. We call `set-visited-file-modtime' ourselves later
5161 ;; on. We must ensure that `file-coding-system-alist'
5162 ;; matches `tmpfile'.
5163 (let (file-name-handler-alist
5164 (file-coding-system-alist
5165 (tramp-find-file-name-coding-system-alist filename tmpfile
)))
5167 (tramp-run-real-handler
5169 (list start end tmpfile append
'no-message lockname confirm
))
5171 (setq tramp-temp-buffer-file-name nil
)
5172 (delete-file tmpfile
)
5173 (signal (car err
) (cdr err
))))
5175 ;; Now, `last-coding-system-used' has the right value. Remember it.
5176 (when (boundp 'last-coding-system-used
)
5177 (setq coding-system-used
5178 (symbol-value 'last-coding-system-used
))))
5180 ;; The permissions of the temporary file should be set. If
5181 ;; filename does not exist (eq modes nil) it has been
5182 ;; renamed to the backup file. This case `save-buffer'
5183 ;; handles permissions.
5184 ;; Ensure, that it is still readable.
5187 tmpfile
(logior (or modes
0) (tramp-octal-to-decimal "0400"))))
5189 ;; This is a bit lengthy due to the different methods
5190 ;; possible for file transfer. First, we check whether the
5191 ;; method uses an rcp program. If so, we call it.
5192 ;; Otherwise, both encoding and decoding command must be
5193 ;; specified. However, if the method _also_ specifies an
5194 ;; encoding function, then that is used for encoding the
5195 ;; contents of the tmp file.
5196 (let* ((size (nth 7 (file-attributes tmpfile
)))
5197 (rem-dec (tramp-get-inline-coding v
"remote-decoding" size
))
5198 (loc-enc (tramp-get-inline-coding v
"local-encoding" size
)))
5200 ;; `copy-file' handles direct copy and out-of-band methods.
5201 ((or (tramp-local-host-p v
)
5202 (tramp-method-out-of-band-p v size
))
5203 (if (and (not (stringp start
))
5204 (= (or end
(point-max)) (point-max))
5205 (= (or start
(point-min)) (point-min))
5206 (tramp-get-method-parameter
5207 method
'tramp-copy-keep-tmpfile
))
5209 (setq tramp-temp-buffer-file-name tmpfile
)
5211 ;; We keep the local file for performance
5212 ;; reasons, useful for "rsync".
5213 (copy-file tmpfile filename t
)
5215 (setq tramp-temp-buffer-file-name nil
)
5216 (delete-file tmpfile
)
5217 (signal (car err
) (cdr err
)))))
5218 (setq tramp-temp-buffer-file-name nil
)
5219 ;; Don't rename, in order to keep context in SELinux.
5221 (copy-file tmpfile filename t
)
5222 (delete-file tmpfile
))))
5224 ;; Use inline file transfer.
5229 (set-buffer-multibyte nil
)
5230 ;; Use encoding function or command.
5231 (if (functionp loc-enc
)
5232 (with-progress-reporter
5233 v
3 (format "Encoding region using function `%s'"
5235 (let ((coding-system-for-read 'binary
))
5236 (insert-file-contents-literally tmpfile
))
5237 ;; The following `let' is a workaround for the
5238 ;; base64.el that comes with pgnus-0.84. If
5239 ;; both of the following conditions are
5240 ;; satisfied, it tries to write to a local
5241 ;; file in default-directory, but at this
5242 ;; point, default-directory is remote.
5243 ;; (`call-process-region' can't write to
5244 ;; remote files, it seems.) The file in
5245 ;; question is a tmp file anyway.
5246 (let ((default-directory
5247 (tramp-compat-temporary-file-directory)))
5248 (funcall loc-enc
(point-min) (point-max))))
5250 (with-progress-reporter
5251 v
3 (format "Encoding region using command `%s'"
5253 (unless (zerop (tramp-call-local-coding-command
5257 (concat "Cannot write to `%s', "
5258 "local encoding command `%s' failed")
5259 filename loc-enc
))))
5261 ;; Send buffer into remote decoding command which
5262 ;; writes to remote file. Because this happens on
5263 ;; the remote host, we cannot use the function.
5264 (with-progress-reporter
5266 (format "Decoding region into remote file %s" filename
)
5267 (goto-char (point-max))
5268 (unless (bolp) (newline))
5272 (concat rem-dec
" <<'EOF'\n%sEOF")
5273 (tramp-shell-quote-argument localname
)
5275 (tramp-barf-unless-okay
5277 "Couldn't write region to `%s', decode using `%s' failed"
5279 ;; When `file-precious-flag' is set, the region is
5280 ;; written to a temporary file. Check that the
5281 ;; checksum is equal to that from the local tmpfile.
5282 (when file-precious-flag
5285 ;; cksum runs locally, if possible.
5286 (zerop (tramp-local-call-process "cksum" tmpfile t
))
5287 ;; cksum runs remotely.
5289 (tramp-send-command-and-check
5293 (tramp-shell-quote-argument localname
))))
5294 ;; ... they are different.
5298 (with-current-buffer (tramp-get-buffer v
)
5302 (concat "Couldn't write region to `%s',"
5303 " decode using `%s' failed")
5304 filename rem-dec
)))))
5307 (delete-file tmpfile
)))
5309 ;; That's not expected.
5313 (concat "Method `%s' should specify both encoding and "
5314 "decoding command or an rcp program")
5317 ;; Make `last-coding-system-used' have the right value.
5318 (when coding-system-used
5319 (set 'last-coding-system-used coding-system-used
))))
5321 (tramp-flush-file-property v
(file-name-directory localname
))
5322 (tramp-flush-file-property v localname
)
5324 ;; We must protect `last-coding-system-used', now we have set it
5325 ;; to its correct value.
5326 (let (last-coding-system-used (need-chown t
))
5327 ;; Set file modification time.
5328 (when (or (eq visit t
) (stringp visit
))
5329 (let ((file-attr (file-attributes filename
)))
5330 (set-visited-file-modtime
5331 ;; We must pass modtime explicitely, because filename can
5332 ;; be different from (buffer-file-name), f.e. if
5333 ;; `file-precious-flag' is set.
5335 (when (and (eq (nth 2 file-attr
) uid
)
5336 (eq (nth 3 file-attr
) gid
))
5337 (setq need-chown nil
))))
5339 ;; Set the ownership.
5341 (tramp-set-file-uid-gid filename uid gid
))
5342 (when (or (eq visit t
) (null visit
) (stringp visit
))
5343 (tramp-message v
0 "Wrote %s" filename
))
5344 (run-hooks 'tramp-handle-write-region-hook
)))))
5346 (defvar tramp-vc-registered-file-names nil
5347 "List used to collect file names, which are checked during `vc-registered'.")
5349 ;; VC backends check for the existence of various different special
5350 ;; files. This is very time consuming, because every single check
5351 ;; requires a remote command (the file cache must be invalidated).
5352 ;; Therefore, we apply a kind of optimization. We install the file
5353 ;; name handler `tramp-vc-file-name-handler', which does nothing but
5354 ;; remembers all file names for which `file-exists-p' or
5355 ;; `file-readable-p' has been applied. A first run of `vc-registered'
5356 ;; is performed. Afterwards, a script is applied for all collected
5357 ;; file names, using just one remote command. The result of this
5358 ;; script is used to fill the file cache with actual values. Now we
5359 ;; can reset the file name handlers, and we make a second run of
5360 ;; `vc-registered', which returns the expected result without sending
5361 ;; any other remote command.
5362 (defun tramp-handle-vc-registered (file)
5363 "Like `vc-registered' for Tramp files."
5364 (with-temp-message ""
5365 (with-parsed-tramp-file-name file nil
5366 (with-progress-reporter
5367 v
3 (format "Checking `vc-registered' for %s" file
)
5369 ;; There could be new files, created by the vc backend. We
5370 ;; cannot reuse the old cache entries, therefore.
5371 (let (tramp-vc-registered-file-names
5372 (tramp-cache-inhibit-cache (current-time))
5373 (file-name-handler-alist
5374 `((,tramp-file-name-regexp . tramp-vc-file-name-handler
))))
5376 ;; Here we collect only file names, which need an operation.
5377 (tramp-run-real-handler 'vc-registered
(list file
))
5378 (tramp-message v
10 "\n%s" tramp-vc-registered-file-names
)
5380 ;; Send just one command, in order to fill the cache.
5381 (when tramp-vc-registered-file-names
5382 (tramp-maybe-send-script
5384 (format tramp-vc-registered-read-file-names
5385 (tramp-get-file-exists-command v
)
5386 (format "%s -r" (tramp-get-test-command v
)))
5387 "tramp_vc_registered_read_file_names")
5391 (tramp-send-command-and-read
5394 "tramp_vc_registered_read_file_names %s"
5395 (mapconcat 'tramp-shell-quote-argument
5396 tramp-vc-registered-file-names
5399 (tramp-set-file-property
5400 v
(car elt
) (cadr elt
) (cadr (cdr elt
))))))
5402 ;; Second run. Now all `file-exists-p' or `file-readable-p'
5403 ;; calls shall be answered from the file cache. We unset
5404 ;; `process-file-side-effects' in order to keep the cache when
5405 ;; `process-file' calls appear.
5406 (let (process-file-side-effects)
5407 (tramp-run-real-handler 'vc-registered
(list file
)))))))
5410 (progn (defun tramp-run-real-handler (operation args
)
5411 "Invoke normal file name handler for OPERATION.
5412 First arg specifies the OPERATION, second arg is a list of arguments to
5413 pass to the OPERATION."
5414 (let* ((inhibit-file-name-handlers
5415 `(tramp-file-name-handler
5416 tramp-vc-file-name-handler
5417 tramp-completion-file-name-handler
5418 cygwin-mount-name-hook-function
5419 cygwin-mount-map-drive-hook-function
5421 ,(and (eq inhibit-file-name-operation operation
)
5422 inhibit-file-name-handlers
)))
5423 (inhibit-file-name-operation operation
))
5424 (apply operation args
))))
5427 (progn (defun tramp-completion-run-real-handler (operation args
)
5428 "Invoke `tramp-file-name-handler' for OPERATION.
5429 First arg specifies the OPERATION, second arg is a list of arguments to
5430 pass to the OPERATION."
5431 (let* ((inhibit-file-name-handlers
5432 `(tramp-completion-file-name-handler
5433 cygwin-mount-name-hook-function
5434 cygwin-mount-map-drive-hook-function
5436 ,(and (eq inhibit-file-name-operation operation
)
5437 inhibit-file-name-handlers
)))
5438 (inhibit-file-name-operation operation
))
5439 (apply operation args
))))
5441 ;; We handle here all file primitives. Most of them have the file
5442 ;; name as first parameter; nevertheless we check for them explicitly
5443 ;; in order to be signaled if a new primitive appears. This
5444 ;; scenario is needed because there isn't a way to decide by
5445 ;; syntactical means whether a foreign method must be called. It would
5446 ;; ease the life if `file-name-handler-alist' would support a decision
5447 ;; function as well but regexp only.
5448 (defun tramp-file-name-for-operation (operation &rest args
)
5449 "Return file name related to OPERATION file primitive.
5450 ARGS are the arguments OPERATION has been called with."
5452 ;; FILE resp DIRECTORY.
5454 (list 'access-file
'byte-compiler-base-file-name
'delete-directory
5455 'delete-file
'diff-latest-backup-file
'directory-file-name
5456 'directory-files
'directory-files-and-attributes
5457 'dired-compress-file
'dired-uncache
5458 'file-accessible-directory-p
'file-attributes
5459 'file-directory-p
'file-executable-p
'file-exists-p
5460 'file-local-copy
'file-remote-p
'file-modes
5461 'file-name-as-directory
'file-name-directory
5462 'file-name-nondirectory
'file-name-sans-versions
5463 'file-ownership-preserved-p
'file-readable-p
5464 'file-regular-p
'file-symlink-p
'file-truename
5465 'file-writable-p
'find-backup-file-name
'find-file-noselect
5466 'get-file-buffer
'insert-directory
'insert-file-contents
5467 'load
'make-directory
'make-directory-internal
5468 'set-file-modes
'substitute-in-file-name
5469 'unhandled-file-name-directory
'vc-registered
5473 'file-selinux-context
'set-file-selinux-context
5475 'abbreviate-file-name
'create-file-buffer
5476 'dired-file-modtime
'dired-make-compressed-filename
5477 'dired-recursive-delete-directory
'dired-set-file-modtime
5478 'dired-shell-unhandle-file-name
'dired-uucode-file
5479 'insert-file-contents-literally
'make-temp-name
'recover-file
5480 'vm-imap-check-mail
'vm-pop-check-mail
'vm-spool-check-mail
))
5481 (if (file-name-absolute-p (nth 0 args
))
5483 (expand-file-name (nth 0 args
))))
5484 ;; FILE DIRECTORY resp FILE1 FILE2.
5486 (list 'add-name-to-file
'copy-file
'expand-file-name
5487 'file-name-all-completions
'file-name-completion
5488 'file-newer-than-file-p
'make-symbolic-link
'rename-file
5492 'dired-make-relative-symlink
5493 'vm-imap-move-mail
'vm-pop-move-mail
'vm-spool-move-mail
))
5496 ((string-match tramp-file-name-regexp
(nth 0 args
)) (nth 0 args
))
5497 ((string-match tramp-file-name-regexp
(nth 1 args
)) (nth 1 args
))
5498 (t (buffer-file-name (current-buffer))))))
5500 ((eq operation
'write-region
)
5504 (list 'set-visited-file-modtime
'verify-visited-file-modtime
5506 'make-auto-save-file-name
5510 (if (bufferp (nth 0 args
)) (nth 0 args
) (current-buffer))))
5513 (list ;; not in Emacs 23+.
5522 'dired-print-file
'dired-shell-call-process
5524 'executable-find
'start-process
5525 'call-process
'call-process-region
))
5527 ;; Unknown file primitive.
5528 (t (error "unknown file I/O primitive: %s" operation
))))
5530 (defun tramp-find-foreign-file-name-handler (filename)
5531 "Return foreign file name handler if exists."
5532 (when (tramp-tramp-file-p filename
)
5533 (let ((v (tramp-dissect-file-name filename t
))
5534 (handler tramp-foreign-file-name-handler-alist
)
5536 ;; When we are not fully sure that filename completion is safe,
5537 ;; we should not return a handler.
5538 (when (or (tramp-file-name-method v
) (tramp-file-name-user v
)
5539 (and (tramp-file-name-host v
)
5540 (not (member (tramp-file-name-host v
)
5541 (mapcar 'car tramp-methods
))))
5542 (not (tramp-completion-mode-p)))
5544 (setq elt
(car handler
)
5545 handler
(cdr handler
))
5546 (when (funcall (car elt
) filename
)
5553 (defun tramp-file-name-handler (operation &rest args
)
5554 "Invoke Tramp file name handler.
5555 Falls back to normal file name handler if no Tramp file name handler exists."
5559 (tramp-replace-environment-variables
5560 (apply 'tramp-file-name-for-operation operation args
)))
5561 (completion (tramp-completion-mode-p))
5562 (foreign (tramp-find-foreign-file-name-handler filename
)))
5563 (with-parsed-tramp-file-name filename nil
5564 ;; Call the backend function.
5567 (apply foreign operation args
)
5569 ;; Trace that somebody has interrupted the
5572 (let (tramp-message-show-message)
5574 v
1 "Interrupt received in operation %s"
5575 (append (list operation
) args
)))
5576 ;; Propagate the quit signal.
5577 (signal (car err
) (cdr err
)))
5579 ;; When we are in completion mode, some failed
5580 ;; operations shall return at least a default value
5581 ;; in order to give the user a chance to correct the
5582 ;; file name in the minibuffer.
5585 ((and completion
(zerop (length localname
))
5586 (memq operation
'(file-exists-p file-directory-p
)))
5588 ((and completion
(zerop (length localname
))
5590 '(expand-file-name file-name-as-directory
)))
5592 ;; Propagate the error.
5593 (t (signal (car err
) (cdr err
))))))
5595 ;; Nothing to do for us.
5596 (tramp-run-real-handler operation args
)))))
5598 ;; When `tramp-mode' is not enabled, we don't do anything.
5599 (tramp-run-real-handler operation args
)))
5601 ;; In Emacs, there is some concurrency due to timers. If a timer
5602 ;; interrupts Tramp and wishes to use the same connection buffer as
5603 ;; the "main" Emacs, then garbage might occur in the connection
5604 ;; buffer. Therefore, we need to make sure that a timer does not use
5605 ;; the same connection buffer as the "main" Emacs. We implement a
5606 ;; cheap global lock, instead of locking each connection buffer
5607 ;; separately. The global lock is based on two variables,
5608 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
5609 ;; (with setq) to indicate a lock. But Tramp also calls itself during
5610 ;; processing of a single file operation, so we need to allow
5611 ;; recursive calls. That's where the `tramp-locker' variable comes in
5612 ;; -- it is let-bound to t during the execution of the current
5613 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
5614 ;; then we should just proceed because we have been called
5615 ;; recursively. But if `tramp-locker' is nil, then we are a timer
5616 ;; interrupting the "main" Emacs, and then we signal an error.
5618 (defvar tramp-locked nil
5619 "If non-nil, then Tramp is currently busy.
5620 Together with `tramp-locker', this implements a locking mechanism
5621 preventing reentrant calls of Tramp.")
5623 (defvar tramp-locker nil
5624 "If non-nil, then a caller has locked Tramp.
5625 Together with `tramp-locked', this implements a locking mechanism
5626 preventing reentrant calls of Tramp.")
5628 (defun tramp-sh-file-name-handler (operation &rest args
)
5629 "Invoke remote-shell Tramp file name handler.
5630 Fall back to normal file name handler if no Tramp handler exists."
5631 (when (and tramp-locked
(not tramp-locker
))
5632 (setq tramp-locked nil
)
5633 (signal 'file-error
(list "Forbidden reentrant call of Tramp")))
5634 (let ((tl tramp-locked
))
5637 (setq tramp-locked t
)
5638 (let ((tramp-locker t
))
5640 (let ((fn (assoc operation tramp-file-name-handler-alist
)))
5642 (apply (cdr fn
) args
)
5643 (tramp-run-real-handler operation args
))))))
5644 (setq tramp-locked tl
))))
5646 (defun tramp-vc-file-name-handler (operation &rest args
)
5647 "Invoke special file name handler, which collects files to be handled."
5650 (tramp-replace-environment-variables
5651 (apply 'tramp-file-name-for-operation operation args
)))
5652 (fn (assoc operation tramp-file-name-handler-alist
)))
5653 (with-parsed-tramp-file-name filename nil
5655 ;; That's what we want: file names, for which checks are
5656 ;; applied. We assume, that VC uses only `file-exists-p' and
5657 ;; `file-readable-p' checks; otherwise we must extend the
5658 ;; list. We do not perform any action, but return nil, in
5659 ;; order to keep `vc-registered' running.
5660 ((and fn
(memq operation
'(file-exists-p file-readable-p
)))
5661 (add-to-list 'tramp-vc-registered-file-names localname
'append
)
5663 ;; Tramp file name handlers like `expand-file-name'. They
5666 (save-match-data (apply (cdr fn
) args
)))
5667 ;; Default file name handlers, we don't care.
5668 (t (tramp-run-real-handler operation args
)))))))
5671 (progn (defun tramp-completion-file-name-handler (operation &rest args
)
5672 "Invoke Tramp file name completion handler.
5673 Falls back to normal file name handler if no Tramp file name handler exists."
5674 ;; We bind `directory-sep-char' here for XEmacs on Windows, which
5675 ;; would otherwise use backslash.
5676 (let ((directory-sep-char ?
/)
5677 (fn (assoc operation tramp-completion-file-name-handler-alist
)))
5679 ;; When `tramp-mode' is not enabled, we don't do anything.
5681 ;; For other syntaxes than `sep', the regexp matches many common
5682 ;; situations where the user doesn't actually want to use Tramp.
5683 ;; So to avoid autoloading Tramp after typing just "/s", we
5684 ;; disable this part of the completion, unless the user implicitly
5685 ;; indicated his interest in using a fancier completion system.
5686 (or (eq tramp-syntax
'sep
)
5687 (featurep 'tramp
) ;; If it's loaded, we may as well use it.
5688 ;; `partial-completion-mode' does not exist in XEmacs.
5689 ;; It is obsoleted with Emacs 24.1.
5690 (and (boundp 'partial-completion-mode
)
5691 (symbol-value 'partial-completion-mode
))
5692 ;; FIXME: These may have been loaded even if the user never
5693 ;; intended to use them.
5695 (featurep 'icicles
)))
5696 (save-match-data (apply (cdr fn
) args
))
5697 (tramp-completion-run-real-handler operation args
)))))
5700 (progn (defun tramp-register-file-name-handlers ()
5701 "Add Tramp file name handlers to `file-name-handler-alist'."
5702 ;; Remove autoloaded handlers from file name handler alist. Useful,
5703 ;; if `tramp-syntax' has been changed.
5704 (let ((a1 (rassq 'tramp-file-name-handler file-name-handler-alist
)))
5705 (setq file-name-handler-alist
(delq a1 file-name-handler-alist
)))
5707 'tramp-completion-file-name-handler file-name-handler-alist
)))
5708 (setq file-name-handler-alist
(delq a1 file-name-handler-alist
)))
5709 ;; Add the handlers.
5710 (add-to-list 'file-name-handler-alist
5711 (cons tramp-file-name-regexp
'tramp-file-name-handler
))
5712 (put 'tramp-file-name-handler
'safe-magic t
)
5713 (add-to-list 'file-name-handler-alist
5714 (cons tramp-completion-file-name-regexp
5715 'tramp-completion-file-name-handler
))
5716 (put 'tramp-completion-file-name-handler
'safe-magic t
)
5717 ;; If jka-compr or epa-file are already loaded, move them to the
5718 ;; front of `file-name-handler-alist'.
5719 (dolist (fnh '(epa-file-handler jka-compr-handler
))
5720 (let ((entry (rassoc fnh file-name-handler-alist
)))
5722 (setq file-name-handler-alist
5723 (cons entry
(delete entry file-name-handler-alist
))))))))
5725 ;; `tramp-file-name-handler' must be registered before evaluation of
5726 ;; site-start and init files, because there might exist remote files
5727 ;; already, f.e. files kept via recentf-mode.
5728 ;;;###autoload(tramp-register-file-name-handlers)
5729 (tramp-register-file-name-handlers)
5732 (defun tramp-unload-file-name-handlers ()
5733 (setq file-name-handler-alist
5734 (delete (rassoc 'tramp-file-name-handler
5735 file-name-handler-alist
)
5736 (delete (rassoc 'tramp-completion-file-name-handler
5737 file-name-handler-alist
)
5738 file-name-handler-alist
))))
5740 (add-hook 'tramp-unload-hook
'tramp-unload-file-name-handlers
)
5742 ;;; File name handler functions for completion mode:
5744 (defvar tramp-completion-mode nil
5745 "If non-nil, external packages signal that they are in file name completion.
5747 This is necessary, because Tramp uses a heuristic depending on last
5748 input event. This fails when external packages use other characters
5749 but <TAB>, <SPACE> or ?\\? for file name completion. This variable
5750 should never be set globally, the intention is to let-bind it.")
5752 ;; Necessary because `tramp-file-name-regexp-unified' and
5753 ;; `tramp-completion-file-name-regexp-unified' aren't different. If
5754 ;; nil, `tramp-completion-run-real-handler' is called (i.e. forwarding
5755 ;; to `tramp-file-name-handler'). Otherwise, it takes
5756 ;; `tramp-run-real-handler'. Using `last-input-event' is a little bit
5757 ;; risky, because completing a file might require loading other files,
5758 ;; like "~/.netrc", and for them it shouldn't be decided based on that
5759 ;; variable. On the other hand, those files shouldn't have partial
5760 ;; Tramp file name syntax. Maybe another variable should be introduced
5761 ;; overwriting this check in such cases. Or we change Tramp file name
5762 ;; syntax in order to avoid ambiguities, like in XEmacs ...
5763 (defun tramp-completion-mode-p ()
5764 "Check, whether method / user name / host name completion is active."
5766 ;; Signal from outside. `non-essential' has been introduced in Emacs 24.
5767 (and (boundp 'non-essential
) (symbol-value 'non-essential
))
5768 tramp-completion-mode
5770 (equal last-input-event
'tab
)
5771 (and (natnump last-input-event
)
5773 ;; ?\t has event-modifier 'control.
5774 (equal last-input-event ?
\t)
5775 (and (not (event-modifiers last-input-event
))
5776 (or (equal last-input-event ?
\?)
5777 (equal last-input-event ?\
)))))
5779 (and (featurep 'xemacs
)
5780 ;; `last-input-event' might be nil.
5781 (not (null last-input-event
))
5782 ;; `last-input-event' may have no character approximation.
5783 (tramp-compat-funcall 'event-to-character last-input-event
)
5785 ;; ?\t has event-modifier 'control.
5787 (tramp-compat-funcall 'event-to-character last-input-event
) ?
\t)
5788 (and (not (event-modifiers last-input-event
))
5790 (tramp-compat-funcall 'event-to-character last-input-event
)
5793 (tramp-compat-funcall 'event-to-character last-input-event
)
5796 (defun tramp-connectable-p (filename)
5797 "Check, whether it is possible to connect the remote host w/o side-effects.
5798 This is true, if either the remote host is already connected, or if we are
5799 not in completion mode."
5800 (and (tramp-tramp-file-p filename
)
5801 (with-parsed-tramp-file-name filename nil
5802 (or (get-buffer (tramp-buffer-name v
))
5803 (not (tramp-completion-mode-p))))))
5805 ;; Method, host name and user name completion.
5806 ;; `tramp-completion-dissect-file-name' returns a list of
5807 ;; tramp-file-name structures. For all of them we return possible completions.
5809 (defun tramp-completion-handle-file-name-all-completions (filename directory
)
5810 "Like `file-name-all-completions' for partial Tramp files."
5812 (let* ((fullname (tramp-drop-volume-letter
5813 (expand-file-name filename directory
)))
5814 ;; Possible completion structures.
5815 (v (tramp-completion-dissect-file-name fullname
))
5819 (let* ((car (car v
))
5820 (method (tramp-file-name-method car
))
5821 (user (tramp-file-name-user car
))
5822 (host (tramp-file-name-host car
))
5823 (localname (tramp-file-name-localname car
))
5824 (m (tramp-find-method method user host
))
5825 (tramp-current-user user
) ; see `tramp-parse-passwd'
5828 (unless localname
;; Nothing to complete.
5832 ;; Method dependent user / host combinations.
5836 (setq all-user-hosts
5837 (append all-user-hosts
5838 (funcall (nth 0 x
) (nth 1 x
)))))
5839 (tramp-get-completion-function m
))
5845 (tramp-get-completion-user-host
5846 method user host
(nth 0 x
) (nth 1 x
)))
5847 (delq nil all-user-hosts
)))))
5849 ;; Possible methods.
5851 (append result
(tramp-get-completion-methods m
)))))
5855 ;; Unify list, remove nil elements.
5857 (let ((car (car result
)))
5861 (substring car
(length (tramp-drop-volume-letter directory
)))))
5862 (setq result
(cdr result
))))
5864 ;; Complete local parts.
5868 (apply (if (tramp-connectable-p fullname
)
5869 'tramp-completion-run-real-handler
5870 'tramp-run-real-handler
)
5871 'file-name-all-completions
(list (list filename directory
)))
5874 ;; Method, host name and user name completion for a file.
5876 (defun tramp-completion-handle-file-name-completion
5877 (filename directory
&optional predicate
)
5878 "Like `file-name-completion' for Tramp files."
5881 (mapcar 'list
(file-name-all-completions filename directory
))
5882 (when (and predicate
5883 (tramp-connectable-p (expand-file-name filename directory
)))
5884 (lambda (x) (funcall predicate
(expand-file-name (car x
) directory
))))))
5886 ;; I misuse a little bit the tramp-file-name structure in order to handle
5887 ;; completion possibilities for partial methods / user names / host names.
5888 ;; Return value is a list of tramp-file-name structures according to possible
5889 ;; completions. If "localname" is non-nil it means there
5890 ;; shouldn't be a completion anymore.
5892 ;; Expected results:
5894 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
5895 ;; [nil nil "x" nil] [nil "x" nil nil] [nil "x" "y" nil]
5896 ;; [nil "x" nil nil]
5897 ;; ["x" nil nil nil]
5899 ;; "/x:" "/x:y" "/x:y:"
5900 ;; [nil nil "x" ""] [nil nil "x" "y"] ["x" nil "y" ""]
5902 ;; ["x" nil "" nil] ["x" nil "y" nil]
5903 ;; ["x" "" nil nil] ["x" "y" nil nil]
5905 ;; "/x:y@" "/x:y@z" "/x:y@z:"
5906 ;; [nil nil "x" "y@"] [nil nil "x" "y@z"] ["x" "y" "z" ""]
5907 ;; "/[x/y@" "/[x/y@z"
5908 ;; ["x" nil "y" nil] ["x" "y" "z" nil]
5909 (defun tramp-completion-dissect-file-name (name)
5910 "Returns a list of `tramp-file-name' structures.
5911 They are collected by `tramp-completion-dissect-file-name1'."
5915 (tramp-completion-ipv6-regexp
5918 (if (zerop (length tramp-postfix-ipv6-format
))
5919 tramp-postfix-host-format
5920 tramp-postfix-ipv6-format
)))
5921 ;; "/method" "/[method"
5922 (tramp-completion-file-name-structure1
5923 (list (concat tramp-prefix-regexp
"\\(" tramp-method-regexp x-nil
"\\)$")
5926 (tramp-completion-file-name-structure2
5927 (list (concat tramp-prefix-regexp
"\\(" tramp-user-regexp x-nil
"\\)$")
5930 (tramp-completion-file-name-structure3
5931 (list (concat tramp-prefix-regexp
"\\(" tramp-host-regexp x-nil
"\\)$")
5933 ;; "/[ipv6" "/[ipv6"
5934 (tramp-completion-file-name-structure4
5935 (list (concat tramp-prefix-regexp
5936 tramp-prefix-ipv6-regexp
5937 "\\(" tramp-completion-ipv6-regexp x-nil
"\\)$")
5939 ;; "/user@host" "/[user@host"
5940 (tramp-completion-file-name-structure5
5941 (list (concat tramp-prefix-regexp
5942 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
5943 "\\(" tramp-host-regexp x-nil
"\\)$")
5945 ;; "/user@[ipv6" "/[user@ipv6"
5946 (tramp-completion-file-name-structure6
5947 (list (concat tramp-prefix-regexp
5948 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
5949 tramp-prefix-ipv6-regexp
5950 "\\(" tramp-completion-ipv6-regexp x-nil
"\\)$")
5952 ;; "/method:user" "/[method/user" "/method://user"
5953 (tramp-completion-file-name-structure7
5954 (list (concat tramp-prefix-regexp
5955 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
5956 "\\(" tramp-user-regexp x-nil
"\\)$")
5958 ;; "/method:host" "/[method/host" "/method://host"
5959 (tramp-completion-file-name-structure8
5960 (list (concat tramp-prefix-regexp
5961 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
5962 "\\(" tramp-host-regexp x-nil
"\\)$")
5964 ;; "/method:[ipv6" "/[method/ipv6" "/method://[ipv6"
5965 (tramp-completion-file-name-structure9
5966 (list (concat tramp-prefix-regexp
5967 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
5968 tramp-prefix-ipv6-regexp
5969 "\\(" tramp-completion-ipv6-regexp x-nil
"\\)$")
5971 ;; "/method:user@host" "/[method/user@host" "/method://user@host"
5972 (tramp-completion-file-name-structure10
5973 (list (concat tramp-prefix-regexp
5974 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
5975 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
5976 "\\(" tramp-host-regexp x-nil
"\\)$")
5978 ;; "/method:user@[ipv6" "/[method/user@ipv6" "/method://user@[ipv6"
5979 (tramp-completion-file-name-structure11
5980 (list (concat tramp-prefix-regexp
5981 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
5982 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
5983 tramp-prefix-ipv6-regexp
5984 "\\(" tramp-completion-ipv6-regexp x-nil
"\\)$")
5986 ;; "/method: "/method:/"
5987 (tramp-completion-file-name-structure12
5989 (if (equal tramp-syntax
'url
)
5990 (concat tramp-prefix-regexp
5991 "\\(" tramp-method-regexp
"\\)"
5992 "\\(" (substring tramp-postfix-method-regexp
0 1)
5993 "\\|" (substring tramp-postfix-method-regexp
1 2) "\\)"
5995 ;; Should not match if not URL syntax.
5996 (concat tramp-prefix-regexp
"/$"))
5998 ;; "/method: "/method:/"
5999 (tramp-completion-file-name-structure13
6001 (if (equal tramp-syntax
'url
)
6002 (concat tramp-prefix-regexp
6003 "\\(" tramp-method-regexp
"\\)"
6004 "\\(" (substring tramp-postfix-method-regexp
0 1)
6005 "\\|" (substring tramp-postfix-method-regexp
1 2) "\\)"
6007 ;; Should not match if not URL syntax.
6008 (concat tramp-prefix-regexp
"/$"))
6011 (mapc (lambda (regexp)
6012 (add-to-list 'result
6013 (tramp-completion-dissect-file-name1 regexp name
)))
6015 tramp-completion-file-name-structure1
6016 tramp-completion-file-name-structure2
6017 tramp-completion-file-name-structure3
6018 tramp-completion-file-name-structure4
6019 tramp-completion-file-name-structure5
6020 tramp-completion-file-name-structure6
6021 tramp-completion-file-name-structure7
6022 tramp-completion-file-name-structure8
6023 tramp-completion-file-name-structure9
6024 tramp-completion-file-name-structure10
6025 tramp-completion-file-name-structure11
6026 tramp-completion-file-name-structure12
6027 tramp-completion-file-name-structure13
6028 tramp-file-name-structure
))
6032 (defun tramp-completion-dissect-file-name1 (structure name
)
6033 "Returns a `tramp-file-name' structure matching STRUCTURE.
6034 The structure consists of remote method, remote user,
6035 remote host and localname (filename on remote host)."
6038 (when (string-match (nth 0 structure
) name
)
6039 (let ((method (and (nth 1 structure
)
6040 (match-string (nth 1 structure
) name
)))
6041 (user (and (nth 2 structure
)
6042 (match-string (nth 2 structure
) name
)))
6043 (host (and (nth 3 structure
)
6044 (match-string (nth 3 structure
) name
)))
6045 (localname (and (nth 4 structure
)
6046 (match-string (nth 4 structure
) name
))))
6047 (vector method user host localname
)))))
6049 ;; This function returns all possible method completions, adding the
6050 ;; trailing method delimeter.
6051 (defun tramp-get-completion-methods (partial-method)
6052 "Returns all method completions for PARTIAL-METHOD."
6056 (string-match (concat "^" (regexp-quote partial-method
)) method
)
6057 (tramp-completion-make-tramp-file-name method nil nil nil
)))
6058 (mapcar 'car tramp-methods
)))
6060 ;; Compares partial user and host names with possible completions.
6061 (defun tramp-get-completion-user-host (method partial-user partial-host user host
)
6062 "Returns the most expanded string for user and host name completion.
6063 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
6066 ((and partial-user partial-host
)
6068 (string-match (concat "^" (regexp-quote partial-host
)) host
)
6069 (string-equal partial-user
(or user partial-user
)))
6070 (setq user partial-user
)
6077 (and user
(string-match (concat "^" (regexp-quote partial-user
)) user
))
6083 (and host
(string-match (concat "^" (regexp-quote partial-host
)) host
))
6089 (unless (zerop (+ (length user
) (length host
)))
6090 (tramp-completion-make-tramp-file-name method user host nil
)))
6092 (defun tramp-parse-rhosts (filename)
6093 "Return a list of (user host) tuples allowed to access.
6094 Either user or host may be nil."
6095 ;; On Windows, there are problems in completion when
6096 ;; `default-directory' is remote.
6097 (let ((default-directory (tramp-compat-temporary-file-directory))
6099 (when (file-readable-p filename
)
6101 (insert-file-contents filename
)
6102 (goto-char (point-min))
6104 (push (tramp-parse-rhosts-group) res
))))
6107 (defun tramp-parse-rhosts-group ()
6108 "Return a (user host) tuple allowed to access.
6109 Either user or host may be nil."
6113 "^\\(" tramp-host-regexp
"\\)"
6114 "\\([ \t]+" "\\(" tramp-user-regexp
"\\)" "\\)?")))
6115 (narrow-to-region (point) (tramp-compat-line-end-position))
6116 (when (re-search-forward regexp nil t
)
6117 (setq result
(append (list (match-string 3) (match-string 1)))))
6122 (defun tramp-parse-shosts (filename)
6123 "Return a list of (user host) tuples allowed to access.
6124 User is always nil."
6125 ;; On Windows, there are problems in completion when
6126 ;; `default-directory' is remote.
6127 (let ((default-directory (tramp-compat-temporary-file-directory))
6129 (when (file-readable-p filename
)
6131 (insert-file-contents filename
)
6132 (goto-char (point-min))
6134 (push (tramp-parse-shosts-group) res
))))
6137 (defun tramp-parse-shosts-group ()
6138 "Return a (user host) tuple allowed to access.
6139 User is always nil."
6141 (regexp (concat "^\\(" tramp-host-regexp
"\\)")))
6142 (narrow-to-region (point) (tramp-compat-line-end-position))
6143 (when (re-search-forward regexp nil t
)
6144 (setq result
(list nil
(match-string 1))))
6147 (> (skip-chars-forward ",") 0)
6151 (defun tramp-parse-sconfig (filename)
6152 "Return a list of (user host) tuples allowed to access.
6153 User is always nil."
6154 ;; On Windows, there are problems in completion when
6155 ;; `default-directory' is remote.
6156 (let ((default-directory (tramp-compat-temporary-file-directory))
6158 (when (file-readable-p filename
)
6160 (insert-file-contents filename
)
6161 (goto-char (point-min))
6163 (push (tramp-parse-sconfig-group) res
))))
6166 (defun tramp-parse-sconfig-group ()
6167 "Return a (user host) tuple allowed to access.
6168 User is always nil."
6170 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp
"\\)")))
6171 (narrow-to-region (point) (tramp-compat-line-end-position))
6172 (when (re-search-forward regexp nil t
)
6173 (setq result
(list nil
(match-string 1))))
6176 (> (skip-chars-forward ",") 0)
6180 (defun tramp-parse-shostkeys (dirname)
6181 "Return a list of (user host) tuples allowed to access.
6182 User is always nil."
6183 ;; On Windows, there are problems in completion when
6184 ;; `default-directory' is remote.
6185 (let* ((default-directory (tramp-compat-temporary-file-directory))
6186 (regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp
"\\)\\.pub$"))
6187 (files (when (file-directory-p dirname
) (directory-files dirname
)))
6190 (when (string-match regexp
(car files
))
6191 (push (list nil
(match-string 1 (car files
))) result
))
6192 (setq files
(cdr files
)))
6195 (defun tramp-parse-sknownhosts (dirname)
6196 "Return a list of (user host) tuples allowed to access.
6197 User is always nil."
6198 ;; On Windows, there are problems in completion when
6199 ;; `default-directory' is remote.
6200 (let* ((default-directory (tramp-compat-temporary-file-directory))
6201 (regexp (concat "^\\(" tramp-host-regexp
6202 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
6203 (files (when (file-directory-p dirname
) (directory-files dirname
)))
6206 (when (string-match regexp
(car files
))
6207 (push (list nil
(match-string 1 (car files
))) result
))
6208 (setq files
(cdr files
)))
6211 (defun tramp-parse-hosts (filename)
6212 "Return a list of (user host) tuples allowed to access.
6213 User is always nil."
6214 ;; On Windows, there are problems in completion when
6215 ;; `default-directory' is remote.
6216 (let ((default-directory (tramp-compat-temporary-file-directory))
6218 (when (file-readable-p filename
)
6220 (insert-file-contents filename
)
6221 (goto-char (point-min))
6223 (push (tramp-parse-hosts-group) res
))))
6226 (defun tramp-parse-hosts-group ()
6227 "Return a (user host) tuple allowed to access.
6228 User is always nil."
6231 (concat "^\\(" tramp-ipv6-regexp
"\\|" tramp-host-regexp
"\\)")))
6232 (narrow-to-region (point) (tramp-compat-line-end-position))
6233 (when (re-search-forward regexp nil t
)
6234 (setq result
(list nil
(match-string 1))))
6237 (> (skip-chars-forward " \t") 0)
6241 ;; For su-alike methods it would be desirable to return "root@localhost"
6242 ;; as default. Unfortunately, we have no information whether any user name
6243 ;; has been typed already. So we use `tramp-current-user' as indication,
6244 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
6245 (defun tramp-parse-passwd (filename)
6246 "Return a list of (user host) tuples allowed to access.
6247 Host is always \"localhost\"."
6248 ;; On Windows, there are problems in completion when
6249 ;; `default-directory' is remote.
6250 (let ((default-directory (tramp-compat-temporary-file-directory))
6252 (if (zerop (length tramp-current-user
))
6254 (when (file-readable-p filename
)
6256 (insert-file-contents filename
)
6257 (goto-char (point-min))
6259 (push (tramp-parse-passwd-group) res
))))
6262 (defun tramp-parse-passwd-group ()
6263 "Return a (user host) tuple allowed to access.
6264 Host is always \"localhost\"."
6266 (regexp (concat "^\\(" tramp-user-regexp
"\\):")))
6267 (narrow-to-region (point) (tramp-compat-line-end-position))
6268 (when (re-search-forward regexp nil t
)
6269 (setq result
(list (match-string 1) "localhost")))
6274 (defun tramp-parse-netrc (filename)
6275 "Return a list of (user host) tuples allowed to access.
6277 ;; On Windows, there are problems in completion when
6278 ;; `default-directory' is remote.
6279 (let ((default-directory (tramp-compat-temporary-file-directory))
6281 (when (file-readable-p filename
)
6283 (insert-file-contents filename
)
6284 (goto-char (point-min))
6286 (push (tramp-parse-netrc-group) res
))))
6289 (defun tramp-parse-netrc-group ()
6290 "Return a (user host) tuple allowed to access.
6295 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp
"\\)"
6296 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp
"\\)" "\\)?")))
6297 (narrow-to-region (point) (tramp-compat-line-end-position))
6298 (when (re-search-forward regexp nil t
)
6299 (setq result
(list (match-string 3) (match-string 1))))
6304 (defun tramp-parse-putty (registry)
6305 "Return a list of (user host) tuples allowed to access.
6306 User is always nil."
6307 ;; On Windows, there are problems in completion when
6308 ;; `default-directory' is remote.
6309 (let ((default-directory (tramp-compat-temporary-file-directory))
6312 (when (zerop (tramp-local-call-process "reg" nil t nil
"query" registry
))
6313 (goto-char (point-min))
6315 (push (tramp-parse-putty-group registry
) res
))))
6318 (defun tramp-parse-putty-group (registry)
6319 "Return a (user host) tuple allowed to access.
6320 User is always nil."
6322 (regexp (concat (regexp-quote registry
) "\\\\\\(.+\\)")))
6323 (narrow-to-region (point) (tramp-compat-line-end-position))
6324 (when (re-search-forward regexp nil t
)
6325 (setq result
(list nil
(match-string 1))))
6330 ;;; Internal Functions:
6332 (defun tramp-maybe-send-script (vec script name
)
6333 "Define in remote shell function NAME implemented as SCRIPT.
6334 Only send the definition if it has not already been done."
6335 (let* ((p (tramp-get-connection-process vec
))
6336 (scripts (tramp-get-connection-property p
"scripts" nil
)))
6337 (unless (member name scripts
)
6338 (with-progress-reporter vec
5 (format "Sending script `%s'" name
)
6339 ;; The script could contain a call of Perl. This is masked with `%s'.
6340 (tramp-send-command-and-check
6342 (format "%s () {\n%s\n}" name
6343 (format script
(tramp-get-remote-perl vec
))))
6344 (tramp-set-connection-property p
"scripts" (cons name scripts
))))))
6346 (defun tramp-set-auto-save ()
6347 (when (and ;; ange-ftp has its own auto-save mechanism
6348 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
6349 'tramp-sh-file-name-handler
)
6351 (auto-save-mode 1)))
6352 (add-hook 'find-file-hooks
'tramp-set-auto-save t
)
6353 (add-hook 'tramp-unload-hook
6355 (remove-hook 'find-file-hooks
'tramp-set-auto-save
)))
6357 (defun tramp-run-test (switch filename
)
6358 "Run `test' on the remote system, given a SWITCH and a FILENAME.
6359 Returns the exit code of the `test' program."
6360 (with-parsed-tramp-file-name filename nil
6361 (tramp-send-command-and-check
6365 (tramp-get-test-command v
)
6367 (tramp-shell-quote-argument localname
)))))
6369 (defun tramp-run-test2 (format-string file1 file2
)
6370 "Run `test'-like program on the remote system, given FILE1, FILE2.
6371 FORMAT-STRING contains the program name, switches, and place holders.
6372 Returns the exit code of the `test' program. Barfs if the methods,
6373 hosts, or files, disagree."
6374 (unless (tramp-equal-remote file1 file2
)
6375 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1
) file1 file2
) nil
6378 "tramp-run-test2 only implemented for same method, user, host")))
6379 (with-parsed-tramp-file-name file1 v1
6380 (with-parsed-tramp-file-name file1 v2
6381 (tramp-send-command-and-check
6383 (format format-string
6384 (tramp-shell-quote-argument v1-localname
)
6385 (tramp-shell-quote-argument v2-localname
))))))
6387 (defun tramp-buffer-name (vec)
6388 "A name for the connection buffer VEC."
6389 ;; We must use `tramp-file-name-real-host', because for gateway
6390 ;; methods the default port will be expanded later on, which would
6392 (let ((method (tramp-file-name-method vec
))
6393 (user (tramp-file-name-user vec
))
6394 (host (tramp-file-name-real-host vec
)))
6395 (if (not (zerop (length user
)))
6396 (format "*tramp/%s %s@%s*" method user host
)
6397 (format "*tramp/%s %s*" method host
))))
6399 (defun tramp-delete-temp-file-function ()
6400 "Remove temporary files related to current buffer."
6401 (when (stringp tramp-temp-buffer-file-name
)
6403 (delete-file tramp-temp-buffer-file-name
)
6406 (add-hook 'kill-buffer-hook
'tramp-delete-temp-file-function
)
6407 (add-hook 'tramp-cache-unload-hook
6409 (remove-hook 'kill-buffer-hook
6410 'tramp-delete-temp-file-function
)))
6412 (defun tramp-get-buffer (vec)
6413 "Get the connection buffer to be used for VEC."
6414 (or (get-buffer (tramp-buffer-name vec
))
6415 (with-current-buffer (get-buffer-create (tramp-buffer-name vec
))
6416 (setq buffer-undo-list t
)
6417 (setq default-directory
6418 (tramp-make-tramp-file-name
6419 (tramp-file-name-method vec
)
6420 (tramp-file-name-user vec
)
6421 (tramp-file-name-host vec
)
6425 (defun tramp-get-connection-buffer (vec)
6426 "Get the connection buffer to be used for VEC.
6427 In case a second asynchronous communication has been started, it is different
6428 from `tramp-get-buffer'."
6429 (or (tramp-get-connection-property vec
"process-buffer" nil
)
6430 (tramp-get-buffer vec
)))
6432 (defun tramp-get-connection-process (vec)
6433 "Get the connection process to be used for VEC.
6434 In case a second asynchronous communication has been started, it is different
6435 from the default one."
6437 (or (tramp-get-connection-property vec
"process-name" nil
)
6438 (tramp-buffer-name vec
))))
6440 (defun tramp-debug-buffer-name (vec)
6441 "A name for the debug buffer for VEC."
6442 ;; We must use `tramp-file-name-real-host', because for gateway
6443 ;; methods the default port will be expanded later on, which would
6445 (let ((method (tramp-file-name-method vec
))
6446 (user (tramp-file-name-user vec
))
6447 (host (tramp-file-name-real-host vec
)))
6448 (if (not (zerop (length user
)))
6449 (format "*debug tramp/%s %s@%s*" method user host
)
6450 (format "*debug tramp/%s %s*" method host
))))
6452 (defconst tramp-debug-outline-regexp
6453 "[0-9]+:[0-9]+:[0-9]+\\.[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #")
6455 (defun tramp-get-debug-buffer (vec)
6456 "Get the debug buffer for VEC."
6457 (with-current-buffer
6458 (get-buffer-create (tramp-debug-buffer-name vec
))
6460 (setq buffer-undo-list t
)
6461 ;; Activate `outline-mode'. This runs `text-mode-hook' and
6462 ;; `outline-mode-hook'. We must prevent that local processes
6463 ;; die. Yes: I've seen `flyspell-mode', which starts "ispell".
6464 ;; Furthermore, `outline-regexp' must have the correct value
6465 ;; already, because it is used by `font-lock-compile-keywords'.
6466 (let ((default-directory (tramp-compat-temporary-file-directory))
6467 (outline-regexp tramp-debug-outline-regexp
))
6469 (set (make-local-variable 'outline-regexp
) tramp-debug-outline-regexp
)
6470 (set (make-local-variable 'outline-level
) 'tramp-outline-level
))
6473 (defun tramp-outline-level ()
6474 "Return the depth to which a statement is nested in the outline.
6475 Point must be at the beginning of a header line.
6477 The outline level is equal to the verbosity of the Tramp message."
6478 (1+ (string-to-number (match-string 1))))
6480 (defun tramp-find-executable
6481 (vec progname dirlist
&optional ignore-tilde ignore-path
)
6482 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
6483 First arg VEC specifies the connection, PROGNAME is the program
6484 to search for, and DIRLIST gives the list of directories to
6485 search. If IGNORE-TILDE is non-nil, directory names starting
6486 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
6489 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
6491 This function expects to be in the right *tramp* buffer."
6492 (with-current-buffer (tramp-get-connection-buffer vec
)
6494 ;; Check whether the executable is in $PATH. "which(1)" does not
6495 ;; report always a correct error code; therefore we check the
6496 ;; number of words it returns.
6498 (tramp-send-command vec
(format "which \\%s | wc -w" progname
))
6499 (goto-char (point-min))
6500 (if (looking-at "^\\s-*1$")
6501 (setq result
(concat "\\" progname
))))
6504 ;; Remove all ~/foo directories from dirlist. In XEmacs,
6505 ;; `remove' is in CL, and we want to avoid CL dependencies.
6508 (setq d
(car dirlist
))
6509 (setq dirlist
(cdr dirlist
))
6510 (unless (char-equal ?~
(aref d
0))
6511 (setq newdl
(cons d newdl
))))
6512 (setq dirlist
(nreverse newdl
))))
6515 (format (concat "while read d; "
6516 "do if test -x $d/%s -a -f $d/%s; "
6517 "then echo tramp_executable $d/%s; "
6518 "break; fi; done <<'EOF'\n"
6520 progname progname progname
(mapconcat 'identity dirlist
"\n")))
6521 (goto-char (point-max))
6522 (when (search-backward "tramp_executable " nil t
)
6523 (skip-chars-forward "^ ")
6524 (skip-chars-forward " ")
6525 (setq result
(buffer-substring
6526 (point) (tramp-compat-line-end-position)))))
6529 (defun tramp-set-remote-path (vec)
6530 "Sets the remote environment PATH to existing directories.
6531 I.e., for each directory in `tramp-remote-path', it is tested
6532 whether it exists and if so, it is added to the environment
6534 (tramp-message vec
5 (format "Setting $PATH environment variable"))
6536 vec
(format "PATH=%s; export PATH"
6537 (mapconcat 'identity
(tramp-get-remote-path vec
) ":"))))
6539 ;; ------------------------------------------------------------
6540 ;; -- Communication with external shell --
6541 ;; ------------------------------------------------------------
6543 (defun tramp-find-file-exists-command (vec)
6544 "Find a command on the remote host for checking if a file exists.
6545 Here, we are looking for a command which has zero exit status if the
6546 file exists and nonzero exit status otherwise."
6547 (let ((existing "/")
6549 (tramp-shell-quote-argument "/ this file does not exist "))
6551 ;; The algorithm is as follows: we try a list of several commands.
6552 ;; For each command, we first run `$cmd /' -- this should return
6553 ;; true, as the root directory always exists. And then we run
6554 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
6555 ;; does not exist. This should return false. We use the first
6556 ;; command we find that seems to work.
6557 ;; The list of commands to try is as follows:
6558 ;; `ls -d' This works on most systems, but NetBSD 1.4
6559 ;; has a bug: `ls' always returns zero exit
6560 ;; status, even for files which don't exist.
6561 ;; `test -e' Some Bourne shells have a `test' builtin
6562 ;; which does not know the `-e' option.
6563 ;; `/bin/test -e' For those, the `test' binary on disk normally
6564 ;; provides the option. Alas, the binary
6565 ;; is sometimes `/bin/test' and sometimes it's
6567 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
6569 (and (setq result
(format "%s -e" (tramp-get-test-command vec
)))
6570 (zerop (tramp-send-command-and-check
6571 vec
(format "%s %s" result existing
)))
6572 (not (zerop (tramp-send-command-and-check
6573 vec
(format "%s %s" result nonexisting
)))))
6574 (and (setq result
"/bin/test -e")
6575 (zerop (tramp-send-command-and-check
6576 vec
(format "%s %s" result existing
)))
6577 (not (zerop (tramp-send-command-and-check
6578 vec
(format "%s %s" result nonexisting
)))))
6579 (and (setq result
"/usr/bin/test -e")
6580 (zerop (tramp-send-command-and-check
6581 vec
(format "%s %s" result existing
)))
6582 (not (zerop (tramp-send-command-and-check
6583 vec
(format "%s %s" result nonexisting
)))))
6584 (and (setq result
(format "%s -d" (tramp-get-ls-command vec
)))
6585 (zerop (tramp-send-command-and-check
6586 vec
(format "%s %s" result existing
)))
6587 (not (zerop (tramp-send-command-and-check
6588 vec
(format "%s %s" result nonexisting
))))))
6590 vec
'file-error
"Couldn't find command to check if file exists"))
6593 ;; CCC test ksh or bash found for tilde expansion?
6594 (defun tramp-find-shell (vec)
6595 "Opens a shell on the remote host which groks tilde expansion."
6596 (unless (tramp-get-connection-property vec
"remote-shell" nil
)
6598 (with-current-buffer (tramp-get-buffer vec
)
6599 (tramp-send-command vec
"echo ~root" t
)
6601 ((or (string-match "^~root$" (buffer-string))
6602 ;; The default shell (ksh93) of OpenSolaris is buggy.
6603 (string-equal (tramp-get-connection-property vec
"uname" "")
6606 (or (tramp-find-executable
6607 vec
"bash" (tramp-get-remote-path vec
) t t
)
6608 (tramp-find-executable
6609 vec
"ksh" (tramp-get-remote-path vec
) t t
)))
6613 "Couldn't find a shell which groks tilde expansion"))
6614 ;; Find arguments for this shell.
6615 (let ((alist tramp-sh-extra-args
)
6617 (while (and alist
(null extra-args
))
6618 (setq item
(pop alist
))
6619 (when (string-match (car item
) shell
)
6620 (setq extra-args
(cdr item
))))
6621 (when extra-args
(setq shell
(concat shell
" " extra-args
))))
6623 vec
5 "Starting remote shell `%s' for tilde expansion" shell
)
6624 (let ((tramp-end-of-output tramp-initial-end-of-output
))
6627 (format "PROMPT_COMMAND='' PS1=%s PS2='' PS3='' exec %s"
6628 (shell-quote-argument tramp-end-of-output
) shell
)
6631 (with-progress-reporter vec
5 (format "Setting remote shell prompt")
6633 vec
(format "PS1=%s" (shell-quote-argument tramp-end-of-output
)) t
)
6634 (tramp-send-command vec
"PS2=''" t
)
6635 (tramp-send-command vec
"PS3=''" t
)
6636 (tramp-send-command vec
"PROMPT_COMMAND=''" t
)))
6639 vec
5 "Remote `%s' groks tilde expansion, good"
6640 (tramp-set-connection-property
6642 (tramp-get-method-parameter
6643 (tramp-file-name-method vec
) 'tramp-remote-sh
)))))))))
6645 ;; ------------------------------------------------------------
6646 ;; -- Functions for establishing connection --
6647 ;; ------------------------------------------------------------
6649 ;; The following functions are actions to be taken when seeing certain
6650 ;; prompts from the remote host. See the variable
6651 ;; `tramp-actions-before-shell' for usage of these functions.
6653 (defun tramp-action-login (proc vec
)
6654 "Send the login name."
6655 (when (not (stringp tramp-current-user
))
6656 (save-window-excursion
6657 (let ((enable-recursive-minibuffers t
))
6658 (pop-to-buffer (tramp-get-connection-buffer vec
))
6659 (setq tramp-current-user
(read-string (match-string 0))))))
6660 (tramp-message vec
3 "Sending login name `%s'" tramp-current-user
)
6661 (with-current-buffer (tramp-get-connection-buffer vec
)
6662 (tramp-message vec
6 "\n%s" (buffer-string)))
6663 (tramp-send-string vec tramp-current-user
))
6665 (defun tramp-action-password (proc vec
)
6666 "Query the user for a password."
6667 (with-current-buffer (process-buffer proc
)
6668 (tramp-check-for-regexp proc tramp-password-prompt-regexp
)
6669 (tramp-message vec
3 "Sending %s" (match-string 1)))
6670 (tramp-enter-password proc
))
6672 (defun tramp-action-succeed (proc vec
)
6673 "Signal success in finding shell prompt."
6674 (throw 'tramp-action
'ok
))
6676 (defun tramp-action-permission-denied (proc vec
)
6677 "Signal permission denied."
6679 (throw 'tramp-action
'permission-denied
))
6681 (defun tramp-action-yesno (proc vec
)
6682 "Ask the user for confirmation using `yes-or-no-p'.
6683 Send \"yes\" to remote process on confirmation, abort otherwise.
6684 See also `tramp-action-yn'."
6685 (save-window-excursion
6686 (let ((enable-recursive-minibuffers t
))
6687 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec
)))
6688 (unless (yes-or-no-p (match-string 0))
6690 (throw 'tramp-action
'permission-denied
))
6691 (with-current-buffer (tramp-get-connection-buffer vec
)
6692 (tramp-message vec
6 "\n%s" (buffer-string)))
6693 (tramp-send-string vec
"yes"))))
6695 (defun tramp-action-yn (proc vec
)
6696 "Ask the user for confirmation using `y-or-n-p'.
6697 Send \"y\" to remote process on confirmation, abort otherwise.
6698 See also `tramp-action-yesno'."
6699 (save-window-excursion
6700 (let ((enable-recursive-minibuffers t
))
6701 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec
)))
6702 (unless (y-or-n-p (match-string 0))
6704 (throw 'tramp-action
'permission-denied
))
6705 (with-current-buffer (tramp-get-connection-buffer vec
)
6706 (tramp-message vec
6 "\n%s" (buffer-string)))
6707 (tramp-send-string vec
"y"))))
6709 (defun tramp-action-terminal (proc vec
)
6710 "Tell the remote host which terminal type to use.
6711 The terminal type can be configured with `tramp-terminal-type'."
6712 (tramp-message vec
5 "Setting `%s' as terminal type." tramp-terminal-type
)
6713 (with-current-buffer (tramp-get-connection-buffer vec
)
6714 (tramp-message vec
6 "\n%s" (buffer-string)))
6715 (tramp-send-string vec tramp-terminal-type
))
6717 (defun tramp-action-process-alive (proc vec
)
6718 "Check, whether a process has finished."
6719 (unless (memq (process-status proc
) '(run open
))
6720 (throw 'tramp-action
'process-died
)))
6722 (defun tramp-action-out-of-band (proc vec
)
6723 "Check, whether an out-of-band copy has finished."
6724 (cond ((and (memq (process-status proc
) '(stop exit
))
6725 (zerop (process-exit-status proc
)))
6726 (tramp-message vec
3 "Process has finished.")
6727 (throw 'tramp-action
'ok
))
6728 ((or (and (memq (process-status proc
) '(stop exit
))
6729 (not (zerop (process-exit-status proc
))))
6730 (memq (process-status proc
) '(signal)))
6731 ;; `scp' could have copied correctly, but set modes could have failed.
6732 ;; This can be ignored.
6733 (with-current-buffer (process-buffer proc
)
6734 (goto-char (point-min))
6735 (if (re-search-forward tramp-operation-not-permitted-regexp nil t
)
6737 (tramp-message vec
5 "'set mode' error ignored.")
6738 (tramp-message vec
3 "Process has finished.")
6739 (throw 'tramp-action
'ok
))
6740 (tramp-message vec
3 "Process has died.")
6741 (throw 'tramp-action
'process-died
))))
6744 ;; Functions for processing the actions.
6746 (defun tramp-process-one-action (proc vec actions
)
6747 "Wait for output from the shell and perform one action."
6748 (let (found todo item pattern action
)
6750 ;; Reread output once all actions have been performed.
6751 ;; Obviously, the output was not complete.
6752 (tramp-accept-process-output proc
1)
6755 (setq item
(pop todo
))
6756 (setq pattern
(format "\\(%s\\)\\'" (symbol-value (nth 0 item
))))
6757 (setq action
(nth 1 item
))
6759 vec
5 "Looking for regexp \"%s\" from remote shell" pattern
)
6760 (when (tramp-check-for-regexp proc pattern
)
6761 (tramp-message vec
5 "Call `%s'" (symbol-name action
))
6762 (setq found
(funcall action proc vec
)))))
6765 (defun tramp-process-actions (proc vec actions
&optional timeout
)
6766 "Perform actions until success or TIMEOUT."
6767 ;; Preserve message for `progress-reporter'.
6768 (with-temp-message ""
6769 ;; Enable auth-source and password-cache.
6770 (tramp-set-connection-property vec
"first-password-request" t
)
6773 (tramp-message proc
3 "Waiting for prompts from remote shell")
6775 (catch 'tramp-action
6777 (with-timeout (timeout)
6778 (tramp-process-one-action proc vec actions
))
6779 (tramp-process-one-action proc vec actions
)))))
6780 (with-current-buffer (tramp-get-connection-buffer vec
)
6781 (tramp-message vec
6 "\n%s" (buffer-string)))
6782 (unless (eq exit
'ok
)
6783 (tramp-clear-passwd vec
)
6784 (tramp-error-with-buffer
6787 ((eq exit
'permission-denied
) "Permission denied")
6788 ((eq exit
'process-died
) "Process died")
6789 (t "Login failed")))))))
6791 ;; Utility functions.
6793 (defun tramp-accept-process-output (&optional proc timeout timeout-msecs
)
6794 "Like `accept-process-output' for Tramp processes.
6795 This is needed in order to hide `last-coding-system-used', which is set
6796 for process communication also."
6797 (with-current-buffer (process-buffer proc
)
6798 (tramp-message proc
10 "%s %s" proc
(process-status proc
))
6799 (let (buffer-read-only last-coding-system-used
)
6800 ;; Under Windows XP, accept-process-output doesn't return
6801 ;; sometimes. So we add an additional timeout.
6802 (with-timeout ((or timeout
1))
6803 (accept-process-output proc timeout timeout-msecs
)))
6804 (tramp-message proc
10 "\n%s" (buffer-string))))
6806 (defun tramp-check-for-regexp (proc regexp
)
6807 "Check, whether REGEXP is contained in process buffer of PROC.
6808 Erase echoed commands if exists."
6809 (with-current-buffer (process-buffer proc
)
6810 (goto-char (point-min))
6812 ;; Check whether we need to remove echo output.
6813 (when (and (tramp-get-connection-property proc
"check-remote-echo" nil
)
6814 (re-search-forward tramp-echoed-echo-mark-regexp nil t
))
6815 (let ((begin (match-beginning 0)))
6816 (when (re-search-forward tramp-echoed-echo-mark-regexp nil t
)
6817 ;; Discard echo from remote output.
6818 (tramp-set-connection-property proc
"check-remote-echo" nil
)
6819 (tramp-message proc
5 "echo-mark found")
6821 (delete-region begin
(point))
6822 (goto-char (point-min)))))
6824 (when (or (not (tramp-get-connection-property proc
"check-remote-echo" nil
))
6825 ;; Sometimes, the echo string is suppressed on the remote side.
6827 (tramp-compat-funcall
6828 'substring-no-properties tramp-echo-mark-marker
6829 0 (min tramp-echo-mark-marker-length
(1- (point-max))))
6830 (tramp-compat-funcall
6831 'buffer-substring-no-properties
6832 1 (min (1+ tramp-echo-mark-marker-length
) (point-max))))))
6833 ;; No echo to be handled, now we can look for the regexp.
6834 (goto-char (point-min))
6835 (re-search-forward regexp nil t
))))
6837 (defun tramp-wait-for-regexp (proc timeout regexp
)
6838 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
6839 Expects the output of PROC to be sent to the current buffer. Returns
6840 the string that matched, or nil. Waits indefinitely if TIMEOUT is
6842 (with-current-buffer (process-buffer proc
)
6843 (let ((found (tramp-check-for-regexp proc regexp
))
6844 (start-time (current-time)))
6846 ;; Work around a bug in XEmacs 21, where the timeout
6847 ;; expires faster than it should. This degenerates
6848 ;; to polling for buggy XEmacsen, but oh, well.
6849 (while (and (not found
)
6850 (< (tramp-time-diff (current-time) start-time
)
6852 (with-timeout (timeout)
6854 (tramp-accept-process-output proc
1)
6855 (unless (memq (process-status proc
) '(run open
))
6856 (tramp-error-with-buffer
6857 nil proc
'file-error
"Process has died"))
6858 (setq found
(tramp-check-for-regexp proc regexp
))))))
6861 (tramp-accept-process-output proc
1)
6862 (unless (memq (process-status proc
) '(run open
))
6863 (tramp-error-with-buffer
6864 nil proc
'file-error
"Process has died"))
6865 (setq found
(tramp-check-for-regexp proc regexp
)))))
6866 (tramp-message proc
6 "\n%s" (buffer-string))
6870 proc
'file-error
"[[Regexp `%s' not found in %d secs]]"
6872 (tramp-error proc
'file-error
"[[Regexp `%s' not found]]" regexp
)))
6875 (defun tramp-barf-if-no-shell-prompt (proc timeout
&rest error-args
)
6876 "Wait for shell prompt and barf if none appears.
6877 Looks at process PROC to see if a shell prompt appears in TIMEOUT
6878 seconds. If not, it produces an error message with the given ERROR-ARGS."
6880 (tramp-wait-for-regexp
6883 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern
))
6884 (apply 'tramp-error-with-buffer nil proc
'file-error error-args
)))
6886 ;; We don't call `tramp-send-string' in order to hide the password
6887 ;; from the debug buffer, and because end-of-line handling of the
6889 (defun tramp-enter-password (proc)
6890 "Prompt for a password and send it to the remote end."
6891 (process-send-string
6892 proc
(concat (tramp-read-passwd proc
)
6893 (or (tramp-get-method-parameter
6894 tramp-current-method
6895 'tramp-password-end-of-line
)
6896 tramp-default-password-end-of-line
))))
6898 (defun tramp-open-connection-setup-interactive-shell (proc vec
)
6899 "Set up an interactive shell.
6900 Mainly sets the prompt and the echo correctly. PROC is the shell
6901 process to set up. VEC specifies the connection."
6902 (let ((tramp-end-of-output tramp-initial-end-of-output
))
6903 ;; It is useful to set the prompt in the following command because
6904 ;; some people have a setting for $PS1 which /bin/sh doesn't know
6905 ;; about and thus /bin/sh will display a strange prompt. For
6906 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
6907 ;; display the current working directory but /bin/sh will display
6908 ;; a dollar sign. The following command line sets $PS1 to a sane
6909 ;; value, and works under Bourne-ish shells as well as csh-like
6910 ;; shells. Daniel Pittman reports that the unusual positioning of
6911 ;; the single quotes makes it work under `rc', too. We also unset
6912 ;; the variable $ENV because that is read by some sh
6913 ;; implementations (eg, bash when called as sh) on startup; this
6914 ;; way, we avoid the startup file clobbering $PS1. $PROMP_COMMAND
6915 ;; is another way to set the prompt in /bin/bash, it must be
6916 ;; discarded as well.
6920 "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s"
6921 (shell-quote-argument tramp-end-of-output
)
6922 (tramp-get-method-parameter
6923 (tramp-file-name-method vec
) 'tramp-remote-sh
))
6927 (tramp-message vec
5 "Setting up remote shell environment")
6928 (tramp-send-command vec
"stty -inlcr -echo kill '^U' erase '^H'" t
)
6929 ;; Check whether the echo has really been disabled. Some
6930 ;; implementations, like busybox of embedded GNU/Linux, don't
6931 ;; support disabling.
6932 (tramp-send-command vec
"echo foo" t
)
6933 (with-current-buffer (process-buffer proc
)
6934 (goto-char (point-min))
6935 (when (looking-at "echo foo")
6936 (tramp-set-connection-property proc
"remote-echo" t
)
6937 (tramp-message vec
5 "Remote echo still on. Ok.")
6938 ;; Make sure backspaces and their echo are enabled and no line
6939 ;; width magic interferes with them.
6940 (tramp-send-command vec
"stty icanon erase ^H cols 32767" t
))))
6942 (tramp-message vec
5 "Setting shell prompt")
6944 vec
(format "PS1=%s" (shell-quote-argument tramp-end-of-output
)) t
)
6945 (tramp-send-command vec
"PS2=''" t
)
6946 (tramp-send-command vec
"PS3=''" t
)
6947 (tramp-send-command vec
"PROMPT_COMMAND=''" t
)
6949 ;; Try to set up the coding system correctly.
6950 ;; CCC this can't be the right way to do it. Hm.
6951 (tramp-message vec
5 "Determining coding system")
6952 (tramp-send-command vec
"echo foo ; echo bar" t
)
6953 (with-current-buffer (process-buffer proc
)
6954 (goto-char (point-min))
6955 (if (featurep 'mule
)
6956 ;; Use MULE to select the right EOL convention for communicating
6957 ;; with the process.
6958 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc
)
6959 (cons 'undecided
'undecided
)))
6960 cs-decode cs-encode
)
6961 (when (symbolp cs
) (setq cs
(cons cs cs
)))
6962 (setq cs-decode
(car cs
))
6963 (setq cs-encode
(cdr cs
))
6964 (unless cs-decode
(setq cs-decode
'undecided
))
6965 (unless cs-encode
(setq cs-encode
'undecided
))
6966 (setq cs-encode
(tramp-coding-system-change-eol-conversion
6968 (when (search-forward "\r" nil t
)
6969 (setq cs-decode
(tramp-coding-system-change-eol-conversion
6971 (tramp-compat-funcall
6972 'set-buffer-process-coding-system cs-decode cs-encode
)
6974 vec
5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode
))
6975 ;; Look for ^M and do something useful if found.
6976 (when (search-forward "\r" nil t
)
6977 ;; We have found a ^M but cannot frob the process coding system
6978 ;; because we're running on a non-MULE Emacs. Let's try
6980 (tramp-send-command vec
"stty -onlcr" t
))))
6981 ;; Dump stty settings in the traces.
6982 (when (>= tramp-verbose
10)
6983 (tramp-send-command vec
"stty -a" t
))
6984 (tramp-send-command vec
"set +o vi +o emacs" t
)
6986 ;; Check whether the output of "uname -sr" has been changed. If
6987 ;; yes, this is a strong indication that we must expire all
6988 ;; connection properties. We start again with
6989 ;; `tramp-maybe-open-connection', it will be catched there.
6990 (tramp-message vec
5 "Checking system information")
6991 (let ((old-uname (tramp-get-connection-property vec
"uname" nil
))
6993 (tramp-set-connection-property
6995 (tramp-send-command-and-read vec
"echo \\\"`uname -sr`\\\""))))
6996 (when (and (stringp old-uname
) (not (string-equal old-uname new-uname
)))
6997 (with-current-buffer (tramp-get-debug-buffer vec
)
6998 ;; Keep the debug buffer.
7000 (generate-new-buffer-name tramp-temp-buffer-name
) 'unique
)
7001 (tramp-compat-funcall 'tramp-cleanup-connection vec
)
7002 (if (= (point-min) (point-max))
7004 (rename-buffer (tramp-debug-buffer-name vec
) 'unique
))
7005 ;; We call `tramp-get-buffer' in order to keep the debug buffer.
7006 (tramp-get-buffer vec
)
7009 "Connection reset, because remote host changed from `%s' to `%s'"
7010 old-uname new-uname
)
7011 (throw 'uname-changed
(tramp-maybe-open-connection vec
)))))
7013 ;; Check whether the remote host suffers from buggy
7014 ;; `send-process-string'. This is known for FreeBSD (see comment in
7015 ;; `send_process', file process.c). I've tested sending 624 bytes
7016 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
7017 ;; this host type is detected locally. It cannot handle remote
7019 (with-connection-property proc
"chunksize"
7021 ((and (integerp tramp-chunksize
) (> tramp-chunksize
0))
7025 vec
5 "Checking remote host type for `send-process-string' bug")
7027 "^FreeBSD" (tramp-get-connection-property vec
"uname" ""))
7030 ;; Set remote PATH variable.
7031 (tramp-set-remote-path vec
)
7033 ;; Search for a good shell before searching for a command which
7034 ;; checks if a file exists. This is done because Tramp wants to use
7035 ;; "test foo; echo $?" to check if various conditions hold, and
7036 ;; there are buggy /bin/sh implementations which don't execute the
7037 ;; "echo $?" part if the "test" part has an error. In particular,
7038 ;; the OpenSolaris /bin/sh is a problem. There are also other
7039 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
7040 ;; in in function declarations, or changing HISTFILE in place.
7041 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
7043 (tramp-find-shell vec
)
7045 ;; Disable unexpected output.
7046 (tramp-send-command vec
"mesg n; biff n" t
)
7048 ;; IRIX64 bash expands "!" even when in single quotes. This
7049 ;; destroys our shell functions, we must disable it. See
7050 ;; <http://stackoverflow.com/questions/3291692/irix-bash-shell-expands-expression-in-single-quotes-yet-shouldnt>.
7051 (when (string-match "^IRIX64" (tramp-get-connection-property vec
"uname" ""))
7052 (tramp-send-command vec
"set +H" t
))
7054 ;; Set the environment.
7055 (tramp-message vec
5 "Setting default environment")
7057 (let ((env (copy-sequence tramp-remote-process-environment
))
7060 (setq item
(tramp-compat-split-string (car env
) "="))
7061 (setcdr item
(mapconcat 'identity
(cdr item
) "="))
7062 (if (and (stringp (cdr item
)) (not (string-equal (cdr item
) "")))
7064 vec
(format "%s=%s; export %s" (car item
) (cdr item
) (car item
)) t
)
7065 (push (car item
) unset
))
7066 (setq env
(cdr env
)))
7069 vec
(format "unset %s" (mapconcat 'identity unset
" ")) t
))))
7071 ;; CCC: We should either implement a Perl version of base64 encoding
7072 ;; and decoding. Then we just use that in the last item. The other
7073 ;; alternative is to use the Perl version of UU encoding. But then
7074 ;; we need a Lisp version of uuencode.
7076 ;; Old text from documentation of tramp-methods:
7077 ;; Using a uuencode/uudecode inline method is discouraged, please use one
7078 ;; of the base64 methods instead since base64 encoding is much more
7079 ;; reliable and the commands are more standardized between the different
7080 ;; Unix versions. But if you can't use base64 for some reason, please
7081 ;; note that the default uudecode command does not work well for some
7082 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
7083 ;; the following command for uudecode:
7085 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
7087 ;; For Irix, no solution is known yet.
7089 (defconst tramp-local-coding-commands
7090 '((b64 base64-encode-region base64-decode-region
)
7091 (uu tramp-uuencode-region uudecode-decode-region
)
7093 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
7094 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
7095 "List of local coding commands for inline transfer.
7096 Each item is a list that looks like this:
7098 \(FORMAT ENCODING DECODING\)
7100 FORMAT is symbol describing the encoding/decoding format. It can be
7101 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
7103 ENCODING and DECODING can be strings, giving commands, or symbols,
7104 giving functions. If they are strings, then they can contain
7105 the \"%s\" format specifier. If that specifier is present, the input
7106 filename will be put into the command line at that spot. If the
7107 specifier is not present, the input should be read from standard
7110 If they are functions, they will be called with two arguments, start
7111 and end of region, and are expected to replace the region contents
7112 with the encoded or decoded results, respectively.")
7114 (defconst tramp-remote-coding-commands
7115 '((b64 "base64" "base64 -d")
7116 (b64 "mimencode -b" "mimencode -u -b")
7117 (b64 "mmencode -b" "mmencode -u -b")
7118 (b64 "recode data..base64" "recode base64..data")
7119 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module
)
7120 (b64 tramp-perl-encode tramp-perl-decode
)
7121 (uu "uuencode xxx" "uudecode -o /dev/stdout")
7122 (uu "uuencode xxx" "uudecode -o -")
7123 (uu "uuencode xxx" "uudecode -p")
7124 (uu "uuencode xxx" tramp-uudecode
)
7126 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
7127 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
7128 "List of remote coding commands for inline transfer.
7129 Each item is a list that looks like this:
7131 \(FORMAT ENCODING DECODING\)
7133 FORMAT is symbol describing the encoding/decoding format. It can be
7134 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
7136 ENCODING and DECODING can be strings, giving commands, or symbols,
7137 giving variables. If they are strings, then they can contain
7138 the \"%s\" format specifier. If that specifier is present, the input
7139 filename will be put into the command line at that spot. If the
7140 specifier is not present, the input should be read from standard
7143 If they are variables, this variable is a string containing a Perl
7144 implementation for this functionality. This Perl program will be transferred
7145 to the remote host, and it is available as shell function with the same name.")
7147 (defun tramp-find-inline-encoding (vec)
7148 "Find an inline transfer encoding that works.
7149 Goes through the list `tramp-local-coding-commands' and
7150 `tramp-remote-coding-commands'."
7152 (let ((local-commands tramp-local-coding-commands
)
7154 loc-enc loc-dec rem-enc rem-dec litem ritem found
)
7155 (while (and local-commands
(not found
))
7156 (setq litem
(pop local-commands
))
7157 (catch 'wont-work-local
7158 (let ((format (nth 0 litem
))
7159 (remote-commands tramp-remote-coding-commands
))
7160 (setq loc-enc
(nth 1 litem
))
7161 (setq loc-dec
(nth 2 litem
))
7162 ;; If the local encoder or decoder is a string, the
7163 ;; corresponding command has to work locally.
7164 (if (not (stringp loc-enc
))
7166 vec
5 "Checking local encoding function `%s'" loc-enc
)
7168 vec
5 "Checking local encoding command `%s' for sanity" loc-enc
)
7169 (unless (zerop (tramp-call-local-coding-command
7171 (throw 'wont-work-local nil
)))
7172 (if (not (stringp loc-dec
))
7174 vec
5 "Checking local decoding function `%s'" loc-dec
)
7176 vec
5 "Checking local decoding command `%s' for sanity" loc-dec
)
7177 (unless (zerop (tramp-call-local-coding-command
7179 (throw 'wont-work-local nil
)))
7180 ;; Search for remote coding commands with the same format
7181 (while (and remote-commands
(not found
))
7182 (setq ritem
(pop remote-commands
))
7183 (catch 'wont-work-remote
7184 (when (equal format
(nth 0 ritem
))
7185 (setq rem-enc
(nth 1 ritem
))
7186 (setq rem-dec
(nth 2 ritem
))
7187 ;; Check if remote encoding and decoding commands can be
7188 ;; called remotely with null input and output. This makes
7189 ;; sure there are no syntax errors and the command is really
7190 ;; found. Note that we do not redirect stdout to /dev/null,
7191 ;; for two reasons: when checking the decoding command, we
7192 ;; actually check the output it gives. And also, when
7193 ;; redirecting "mimencode" output to /dev/null, then as root
7194 ;; it might change the permissions of /dev/null!
7195 (when (not (stringp rem-enc
))
7196 (let ((name (symbol-name rem-enc
)))
7197 (while (string-match (regexp-quote "-") name
)
7198 (setq name
(replace-match "_" nil t name
)))
7199 (tramp-maybe-send-script vec
(symbol-value rem-enc
) name
)
7200 (setq rem-enc name
)))
7203 "Checking remote encoding command `%s' for sanity" rem-enc
)
7204 (unless (zerop (tramp-send-command-and-check
7205 vec
(format "%s </dev/null" rem-enc
) t
))
7206 (throw 'wont-work-remote nil
))
7208 (when (not (stringp rem-dec
))
7209 (let ((name (symbol-name rem-dec
)))
7210 (while (string-match (regexp-quote "-") name
)
7211 (setq name
(replace-match "_" nil t name
)))
7212 (tramp-maybe-send-script vec
(symbol-value rem-dec
) name
)
7213 (setq rem-dec name
)))
7216 "Checking remote decoding command `%s' for sanity" rem-dec
)
7217 (unless (zerop (tramp-send-command-and-check
7219 (format "echo %s | %s | %s"
7220 magic rem-enc rem-dec
)
7222 (throw 'wont-work-remote nil
))
7224 (with-current-buffer (tramp-get-buffer vec
)
7225 (goto-char (point-min))
7226 (unless (looking-at (regexp-quote magic
))
7227 (throw 'wont-work-remote nil
)))
7229 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
7230 (setq rem-enc
(nth 1 ritem
))
7231 (setq rem-dec
(nth 2 ritem
))
7232 (setq found t
)))))))
7234 ;; Did we find something?
7237 vec
'file-error
"Couldn't find an inline transfer encoding"))
7239 ;; Set connection properties.
7240 (tramp-message vec
5 "Using local encoding `%s'" loc-enc
)
7241 (tramp-set-connection-property vec
"local-encoding" loc-enc
)
7242 (tramp-message vec
5 "Using local decoding `%s'" loc-dec
)
7243 (tramp-set-connection-property vec
"local-decoding" loc-dec
)
7244 (tramp-message vec
5 "Using remote encoding `%s'" rem-enc
)
7245 (tramp-set-connection-property vec
"remote-encoding" rem-enc
)
7246 (tramp-message vec
5 "Using remote decoding `%s'" rem-dec
)
7247 (tramp-set-connection-property vec
"remote-decoding" rem-dec
))))
7249 (defun tramp-call-local-coding-command (cmd input output
)
7250 "Call the local encoding or decoding command.
7251 If CMD contains \"%s\", provide input file INPUT there in command.
7252 Otherwise, INPUT is passed via standard input.
7253 INPUT can also be nil which means `/dev/null'.
7254 OUTPUT can be a string (which specifies a filename), or t (which
7255 means standard output and thus the current buffer), or nil (which
7257 (tramp-local-call-process
7258 tramp-encoding-shell
7259 (when (and input
(not (string-match "%s" cmd
))) input
)
7260 (if (eq output t
) t nil
)
7262 tramp-encoding-command-switch
7264 (if (string-match "%s" cmd
) (format cmd input
) cmd
)
7265 (if (stringp output
) (concat "> " output
) ""))))
7267 (defconst tramp-inline-compress-commands
7268 '(("gzip" "gzip -d")
7269 ("bzip2" "bzip2 -d")
7270 ("compress" "compress -d"))
7271 "List of compress and decompress commands for inline transfer.
7272 Each item is a list that looks like this:
7274 \(COMPRESS DECOMPRESS\)
7276 COMPRESS or DECOMPRESS are strings with the respective commands.")
7278 (defun tramp-find-inline-compress (vec)
7279 "Find an inline transfer compress command that works.
7280 Goes through the list `tramp-inline-compress-commands'."
7282 (let ((commands tramp-inline-compress-commands
)
7284 item compress decompress
7286 (while (and commands
(not found
))
7288 (setq item
(pop commands
)
7289 compress
(nth 0 item
)
7290 decompress
(nth 1 item
))
7293 "Checking local compress command `%s', `%s' for sanity"
7294 compress decompress
)
7295 (unless (zerop (tramp-call-local-coding-command
7296 (format "echo %s | %s | %s"
7297 magic compress decompress
) nil nil
))
7301 "Checking remote compress command `%s', `%s' for sanity"
7302 compress decompress
)
7303 (unless (zerop (tramp-send-command-and-check
7304 vec
(format "echo %s | %s | %s"
7305 magic compress decompress
) t
))
7309 ;; Did we find something?
7312 ;; Set connection properties.
7314 vec
5 "Using inline transfer compress command `%s'" compress
)
7315 (tramp-set-connection-property vec
"inline-compress" compress
)
7317 vec
5 "Using inline transfer decompress command `%s'" decompress
)
7318 (tramp-set-connection-property vec
"inline-decompress" decompress
))
7320 (tramp-set-connection-property vec
"inline-compress" nil
)
7321 (tramp-set-connection-property vec
"inline-decompress" nil
)
7323 vec
2 "Couldn't find an inline transfer compress command")))))
7325 (defun tramp-compute-multi-hops (vec)
7326 "Expands VEC according to `tramp-default-proxies-alist'.
7327 Gateway hops are already opened."
7328 (let ((target-alist `(,vec
))
7329 (choices tramp-default-proxies-alist
)
7332 ;; Look for proxy hosts to be passed.
7334 (setq item
(pop choices
)
7335 proxy
(eval (nth 2 item
)))
7338 (string-match (or (eval (nth 0 item
)) "")
7339 (or (tramp-file-name-host (car target-alist
)) ""))
7341 (string-match (or (eval (nth 1 item
)) "")
7342 (or (tramp-file-name-user (car target-alist
)) "")))
7344 ;; No more hops needed.
7346 ;; Replace placeholders.
7351 ?u
(or (tramp-file-name-user (car target-alist
)) "")
7352 ?h
(or (tramp-file-name-host (car target-alist
)) ""))))
7353 (with-parsed-tramp-file-name proxy l
7355 (add-to-list 'target-alist l
)
7356 ;; Start next search.
7357 (setq choices tramp-default-proxies-alist
)))))
7360 (when (and (boundp 'tramp-gw-tunnel-method
)
7361 (string-match (format
7363 (symbol-value 'tramp-gw-tunnel-method
)
7364 (symbol-value 'tramp-gw-socks-method
))
7365 (tramp-file-name-method (car target-alist
))))
7366 (let ((gw (pop target-alist
))
7367 (hop (pop target-alist
)))
7368 ;; Is the method prepared for gateways?
7369 (unless (tramp-get-method-parameter
7370 (tramp-file-name-method hop
) 'tramp-default-port
)
7373 "Method `%s' is not supported for gateway access."
7374 (tramp-file-name-method hop
)))
7375 ;; Add default port if needed.
7378 tramp-host-with-port-regexp
(tramp-file-name-host hop
))
7381 (tramp-file-name-host hop
) tramp-prefix-port-format
7383 (tramp-get-method-parameter
7384 (tramp-file-name-method hop
) 'tramp-default-port
)))))
7385 ;; Open the gateway connection.
7389 (tramp-file-name-method hop
) (tramp-file-name-user hop
)
7390 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop
) nil
))
7391 ;; For the password prompt, we need the correct values.
7392 ;; Therefore, we must remember the gateway vector. But we
7393 ;; cannot do it as connection property, because it shouldn't
7394 ;; be persistent. And we have no started process yet either.
7395 (tramp-set-file-property (car target-alist
) "" "gateway" hop
)))
7397 ;; Foreign and out-of-band methods are not supported for multi-hops.
7398 (when (cdr target-alist
)
7399 (setq choices target-alist
)
7401 (setq item
(pop choices
))
7405 (tramp-get-method-parameter
7406 (tramp-file-name-method item
) 'tramp-login-program
))
7407 (tramp-get-method-parameter
7408 (tramp-file-name-method item
) 'tramp-copy-program
))
7411 "Method `%s' is not supported for multi-hops."
7412 (tramp-file-name-method item
)))))
7414 ;; In case the host name is not used for the remote shell
7415 ;; command, the user could be misguided by applying a random
7417 (let* ((v (car target-alist
))
7418 (method (tramp-file-name-method v
))
7419 (host (tramp-file-name-host v
)))
7422 ;; There are multi-hops.
7424 ;; The host name is used for the remote shell command.
7426 '("%h") (tramp-get-method-parameter method
'tramp-login-args
))
7427 ;; The host is local. We cannot use `tramp-local-host-p'
7428 ;; here, because it opens a connection as well.
7429 (string-match tramp-local-host-regexp host
))
7432 "Host `%s' looks like a remote host, `%s' can only use the local host"
7438 (defun tramp-maybe-open-connection (vec)
7439 "Maybe open a connection VEC.
7440 Does not do anything if a connection is already open, but re-opens the
7441 connection if a previous connection has died for some reason."
7442 (catch 'uname-changed
7443 (let ((p (tramp-get-connection-process vec
))
7444 (process-name (tramp-get-connection-property vec
"process-name" nil
))
7445 (process-environment (copy-sequence process-environment
)))
7447 ;; If too much time has passed since last command was sent, look
7448 ;; whether process is still alive. If it isn't, kill it. When
7449 ;; using ssh, it can sometimes happen that the remote end has
7450 ;; hung up but the local ssh client doesn't recognize this until
7451 ;; it tries to send some data to the remote end. So that's why
7452 ;; we try to send a command from time to time, then look again
7453 ;; whether the process is really alive.
7455 (when (and (> (tramp-time-diff
7457 (tramp-get-connection-property
7458 p
"last-cmd-time" '(0 0 0)))
7460 p
(processp p
) (memq (process-status p
) '(run open
)))
7461 (tramp-send-command vec
"echo are you awake" t t
)
7462 (unless (and (memq (process-status p
) '(run open
))
7463 (tramp-wait-for-output p
10))
7464 ;; The error will be catched locally.
7465 (tramp-error vec
'file-error
"Awake did fail")))
7467 (tramp-flush-connection-property vec
)
7468 (tramp-flush-connection-property p
)
7472 ;; New connection must be opened.
7473 (unless (and p
(processp p
) (memq (process-status p
) '(run open
)))
7475 ;; We call `tramp-get-buffer' in order to get a debug buffer for
7476 ;; messages from the beginning.
7477 (tramp-get-buffer vec
)
7478 (with-progress-reporter
7480 (if (zerop (length (tramp-file-name-user vec
)))
7481 (format "Opening connection for %s using %s"
7482 (tramp-file-name-host vec
)
7483 (tramp-file-name-method vec
))
7484 (format "Opening connection for %s@%s using %s"
7485 (tramp-file-name-user vec
)
7486 (tramp-file-name-host vec
)
7487 (tramp-file-name-method vec
)))
7489 ;; Start new process.
7490 (when (and p
(processp p
))
7492 (setenv "TERM" tramp-terminal-type
)
7493 (setenv "LC_ALL" "C")
7494 (setenv "PROMPT_COMMAND")
7495 (setenv "PS1" tramp-initial-end-of-output
)
7496 (let* ((target-alist (tramp-compute-multi-hops vec
))
7497 (process-connection-type tramp-process-connection-type
)
7498 (process-adaptive-read-buffering nil
)
7499 (coding-system-for-read nil
)
7500 ;; This must be done in order to avoid our file name handler.
7501 (p (let ((default-directory
7502 (tramp-compat-temporary-file-directory)))
7504 (or process-name
(tramp-buffer-name vec
))
7505 (tramp-get-connection-buffer vec
)
7506 tramp-encoding-shell
))))
7509 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
7511 ;; Check whether process is alive.
7512 (tramp-set-process-query-on-exit-flag p nil
)
7513 (tramp-barf-if-no-shell-prompt
7514 p
60 "Couldn't find local shell prompt %s" tramp-encoding-shell
)
7516 ;; Now do all the connections as specified.
7518 (let* ((hop (car target-alist
))
7519 (l-method (tramp-file-name-method hop
))
7520 (l-user (tramp-file-name-user hop
))
7521 (l-host (tramp-file-name-host hop
))
7524 (tramp-get-method-parameter
7525 l-method
'tramp-login-program
))
7527 (tramp-get-method-parameter l-method
'tramp-login-args
))
7529 (tramp-get-method-parameter l-method
'tramp-async-args
))
7531 (tramp-get-method-parameter l-method
'tramp-gw-args
))
7532 (gw (tramp-get-file-property hop
"" "gateway" nil
))
7533 (g-method (and gw
(tramp-file-name-method gw
)))
7534 (g-user (and gw
(tramp-file-name-user gw
)))
7535 (g-host (and gw
(tramp-file-name-host gw
)))
7536 (command login-program
)
7537 ;; We don't create the temporary file. In fact,
7538 ;; it is just a prefix for the ControlPath option
7539 ;; of ssh; the real temporary file has another
7540 ;; name, and it is created and protected by ssh.
7541 ;; It is also removed by ssh, when the connection
7544 (tramp-set-connection-property
7548 tramp-temp-name-prefix
7549 (tramp-compat-temporary-file-directory)))))
7552 ;; Add arguments for asynchrononous processes.
7553 (when (and process-name async-args
)
7554 (setq login-args
(append async-args login-args
)))
7556 ;; Add gateway arguments if necessary.
7557 (when (and gw gw-args
)
7558 (setq login-args
(append gw-args login-args
)))
7560 ;; Check for port number. Until now, there's no need
7561 ;; for handling like method, user, host.
7562 (when (string-match tramp-host-with-port-regexp l-host
)
7563 (setq l-port
(match-string 2 l-host
)
7564 l-host
(match-string 1 l-host
)))
7566 ;; Set variables for computing the prompt for reading
7567 ;; password. They can also be derived from a gateway.
7568 (setq tramp-current-method
(or g-method l-method
)
7569 tramp-current-user
(or g-user l-user
)
7570 tramp-current-host
(or g-host l-host
))
7572 ;; Replace login-args place holders.
7574 l-host
(or l-host
"")
7575 l-user
(or l-user
"")
7576 l-port
(or l-port
"")
7577 spec
(format-spec-make
7578 ?h l-host ?u l-user ?p l-port ?t tmpfile
)
7581 ;; We do not want to see the trailing local prompt in
7582 ;; `start-file-process'.
7583 (unless (memq system-type
'(windows-nt)) "exec ")
7587 (setq x
(mapcar (lambda (y) (format-spec y spec
)) x
))
7588 (unless (member "" x
) (mapconcat 'identity x
" ")))
7590 ;; Local shell could be a Windows COMSPEC. It
7591 ;; doesn't know the ";" syntax, but we must exit
7592 ;; always for `start-file-process'. "exec" does not
7594 (if (memq system-type
'(windows-nt)) " && exit || exit")))
7596 ;; Send the command.
7597 (tramp-message vec
3 "Sending command `%s'" command
)
7598 (tramp-send-command vec command t t
)
7599 (tramp-process-actions p vec tramp-actions-before-shell
60)
7601 vec
3 "Found remote shell prompt on `%s'" l-host
))
7603 (setq target-alist
(cdr target-alist
)))
7605 ;; Make initial shell settings.
7606 (tramp-open-connection-setup-interactive-shell p vec
)))))))
7608 (defun tramp-send-command (vec command
&optional neveropen nooutput
)
7609 "Send the COMMAND to connection VEC.
7610 Erases temporary buffer before sending the command. If optional
7611 arg NEVEROPEN is non-nil, never try to open the connection. This
7612 is meant to be used from `tramp-maybe-open-connection' only. The
7613 function waits for output unless NOOUTPUT is set."
7614 (unless neveropen
(tramp-maybe-open-connection vec
))
7615 (let ((p (tramp-get-connection-process vec
)))
7616 (when (tramp-get-connection-property p
"remote-echo" nil
)
7617 ;; We mark the command string that it can be erased in the output buffer.
7618 (tramp-set-connection-property p
"check-remote-echo" t
)
7619 (setq command
(format "%s%s%s" tramp-echo-mark command tramp-echo-mark
)))
7620 (tramp-message vec
6 "%s" command
)
7621 (tramp-send-string vec command
)
7622 (unless nooutput
(tramp-wait-for-output p
))))
7624 (defun tramp-wait-for-output (proc &optional timeout
)
7625 "Wait for output from remote command."
7626 (unless (buffer-live-p (process-buffer proc
))
7627 (delete-process proc
)
7628 (tramp-error proc
'file-error
"Process `%s' not available, try again" proc
))
7629 (with-current-buffer (process-buffer proc
)
7630 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
7631 ;; be leading escape sequences, which must be ignored.
7632 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output
)))
7633 ;; Sometimes, the commands do not return a newline but a
7634 ;; null byte before the shell prompt, for example "git
7635 ;; ls-files -c -z ...".
7636 (regexp1 (format "\\(^\\|\000\\)%s" regexp
))
7637 (found (tramp-wait-for-regexp proc timeout regexp1
)))
7639 (let (buffer-read-only)
7640 ;; A simple-minded busybox has sent " ^H" sequences.
7642 (goto-char (point-min))
7643 (when (re-search-forward
7644 "^\\(.\b\\)+$" (tramp-compat-line-end-position) t
)
7646 (delete-region (point-min) (point)))
7647 ;; Delete the prompt.
7648 (goto-char (point-max))
7649 (re-search-backward regexp nil t
)
7650 (delete-region (point) (point-max)))
7654 "[[Remote prompt `%s' not found in %d secs]]"
7655 tramp-end-of-output timeout
)
7658 "[[Remote prompt `%s' not found]]" tramp-end-of-output
)))
7659 ;; Return value is whether end-of-output sentinel was found.
7662 (defun tramp-send-command-and-check
7663 (vec command
&optional subshell dont-suppress-err
)
7664 "Run COMMAND and check its exit status.
7665 Sends `echo $?' along with the COMMAND for checking the exit status. If
7666 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
7668 If the optional argument SUBSHELL is non-nil, the command is
7669 executed in a subshell, ie surrounded by parentheses. If
7670 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
7673 (concat (if subshell
"( " "")
7675 (if command
(if dont-suppress-err
"; " " 2>/dev/null; ") "")
7676 "echo tramp_exit_status $?"
7677 (if subshell
" )" "")))
7678 (with-current-buffer (tramp-get-connection-buffer vec
)
7679 (goto-char (point-max))
7680 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
7682 vec
'file-error
"Couldn't find exit status of `%s'" command
))
7683 (skip-chars-forward "^ ")
7685 (read (current-buffer))
7686 (let (buffer-read-only) (delete-region (match-beginning 0) (point-max))))))
7688 (defun tramp-barf-unless-okay (vec command fmt
&rest args
)
7689 "Run COMMAND, check exit status, throw error if exit status not okay.
7690 Similar to `tramp-send-command-and-check' but accepts two more arguments
7691 FMT and ARGS which are passed to `error'."
7692 (unless (zerop (tramp-send-command-and-check vec command
))
7693 (apply 'tramp-error vec
'file-error fmt args
)))
7695 (defun tramp-send-command-and-read (vec command
)
7696 "Run COMMAND and return the output, which must be a Lisp expression.
7697 In case there is no valid Lisp expression, it raises an error"
7698 (tramp-barf-unless-okay vec command
"`%s' returns with error" command
)
7699 (with-current-buffer (tramp-get-connection-buffer vec
)
7700 ;; Read the expression.
7701 (goto-char (point-min))
7703 (prog1 (read (current-buffer))
7705 (when (re-search-forward "\\S-" (tramp-compat-line-end-position) t
)
7709 "`%s' does not return a valid Lisp expression: `%s'"
7710 command
(buffer-string))))))
7712 ;; It seems that Tru64 Unix does not like it if long strings are sent
7713 ;; to it in one go. (This happens when sending the Perl
7714 ;; `file-attributes' implementation, for instance.) Therefore, we
7715 ;; have this function which sends the string in chunks.
7716 (defun tramp-send-string (vec string
)
7717 "Send the STRING via connection VEC.
7719 The STRING is expected to use Unix line-endings, but the lines sent to
7720 the remote host use line-endings as defined in the variable
7721 `tramp-rsh-end-of-line'. The communication buffer is erased before sending."
7722 (let* ((p (tramp-get-connection-process vec
))
7723 (chunksize (tramp-get-connection-property p
"chunksize" nil
)))
7726 vec
'file-error
"Can't send string to remote host -- not logged in"))
7727 (tramp-set-connection-property p
"last-cmd-time" (current-time))
7728 (tramp-message vec
10 "%s" string
)
7729 (with-current-buffer (tramp-get-connection-buffer vec
)
7730 ;; Clean up the buffer. We cannot call `erase-buffer' because
7731 ;; narrowing might be in effect.
7732 (let (buffer-read-only) (delete-region (point-min) (point-max)))
7733 ;; Replace "\n" by `tramp-rsh-end-of-line'.
7735 (mapconcat 'identity
7736 (tramp-compat-split-string string
"\n")
7737 tramp-rsh-end-of-line
))
7738 (unless (or (string= string
"")
7739 (string-equal (substring string -
1) tramp-rsh-end-of-line
))
7740 (setq string
(concat string tramp-rsh-end-of-line
)))
7742 (if (and chunksize
(not (zerop chunksize
)))
7744 (end (length string
)))
7747 vec
10 "Sending chunk from %s to %s"
7748 pos
(min (+ pos chunksize
) end
))
7749 (process-send-string
7750 p
(substring string pos
(min (+ pos chunksize
) end
)))
7751 (setq pos
(+ pos chunksize
))))
7752 (process-send-string p string
)))))
7754 (defun tramp-mode-string-to-int (mode-string)
7755 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
7756 (let* (case-fold-search
7757 (mode-chars (string-to-vector mode-string
))
7758 (owner-read (aref mode-chars
1))
7759 (owner-write (aref mode-chars
2))
7760 (owner-execute-or-setid (aref mode-chars
3))
7761 (group-read (aref mode-chars
4))
7762 (group-write (aref mode-chars
5))
7763 (group-execute-or-setid (aref mode-chars
6))
7764 (other-read (aref mode-chars
7))
7765 (other-write (aref mode-chars
8))
7766 (other-execute-or-sticky (aref mode-chars
9)))
7770 ((char-equal owner-read ?r
) (tramp-octal-to-decimal "00400"))
7771 ((char-equal owner-read ?-
) 0)
7772 (t (error "Second char `%c' must be one of `r-'" owner-read
)))
7774 ((char-equal owner-write ?w
) (tramp-octal-to-decimal "00200"))
7775 ((char-equal owner-write ?-
) 0)
7776 (t (error "Third char `%c' must be one of `w-'" owner-write
)))
7778 ((char-equal owner-execute-or-setid ?x
)
7779 (tramp-octal-to-decimal "00100"))
7780 ((char-equal owner-execute-or-setid ?S
)
7781 (tramp-octal-to-decimal "04000"))
7782 ((char-equal owner-execute-or-setid ?s
)
7783 (tramp-octal-to-decimal "04100"))
7784 ((char-equal owner-execute-or-setid ?-
) 0)
7785 (t (error "Fourth char `%c' must be one of `xsS-'"
7786 owner-execute-or-setid
)))
7788 ((char-equal group-read ?r
) (tramp-octal-to-decimal "00040"))
7789 ((char-equal group-read ?-
) 0)
7790 (t (error "Fifth char `%c' must be one of `r-'" group-read
)))
7792 ((char-equal group-write ?w
) (tramp-octal-to-decimal "00020"))
7793 ((char-equal group-write ?-
) 0)
7794 (t (error "Sixth char `%c' must be one of `w-'" group-write
)))
7796 ((char-equal group-execute-or-setid ?x
)
7797 (tramp-octal-to-decimal "00010"))
7798 ((char-equal group-execute-or-setid ?S
)
7799 (tramp-octal-to-decimal "02000"))
7800 ((char-equal group-execute-or-setid ?s
)
7801 (tramp-octal-to-decimal "02010"))
7802 ((char-equal group-execute-or-setid ?-
) 0)
7803 (t (error "Seventh char `%c' must be one of `xsS-'"
7804 group-execute-or-setid
)))
7806 ((char-equal other-read ?r
)
7807 (tramp-octal-to-decimal "00004"))
7808 ((char-equal other-read ?-
) 0)
7809 (t (error "Eighth char `%c' must be one of `r-'" other-read
)))
7811 ((char-equal other-write ?w
) (tramp-octal-to-decimal "00002"))
7812 ((char-equal other-write ?-
) 0)
7813 (t (error "Nineth char `%c' must be one of `w-'" other-write
)))
7815 ((char-equal other-execute-or-sticky ?x
)
7816 (tramp-octal-to-decimal "00001"))
7817 ((char-equal other-execute-or-sticky ?T
)
7818 (tramp-octal-to-decimal "01000"))
7819 ((char-equal other-execute-or-sticky ?t
)
7820 (tramp-octal-to-decimal "01001"))
7821 ((char-equal other-execute-or-sticky ?-
) 0)
7822 (t (error "Tenth char `%c' must be one of `xtT-'"
7823 other-execute-or-sticky
)))))))
7825 (defun tramp-convert-file-attributes (vec attr
)
7826 "Convert file-attributes ATTR generated by perl script, stat or ls.
7827 Convert file mode bits to string and set virtual device number.
7830 ;; Convert last access time.
7831 (unless (listp (nth 4 attr
))
7832 (setcar (nthcdr 4 attr
)
7833 (list (floor (nth 4 attr
) 65536)
7834 (floor (mod (nth 4 attr
) 65536)))))
7835 ;; Convert last modification time.
7836 (unless (listp (nth 5 attr
))
7837 (setcar (nthcdr 5 attr
)
7838 (list (floor (nth 5 attr
) 65536)
7839 (floor (mod (nth 5 attr
) 65536)))))
7840 ;; Convert last status change time.
7841 (unless (listp (nth 6 attr
))
7842 (setcar (nthcdr 6 attr
)
7843 (list (floor (nth 6 attr
) 65536)
7844 (floor (mod (nth 6 attr
) 65536)))))
7845 ;; Convert file size.
7846 (when (< (nth 7 attr
) 0)
7847 (setcar (nthcdr 7 attr
) -
1))
7848 (when (and (floatp (nth 7 attr
))
7849 (<= (nth 7 attr
) (tramp-compat-most-positive-fixnum)))
7850 (setcar (nthcdr 7 attr
) (round (nth 7 attr
))))
7851 ;; Convert file mode bits to string.
7852 (unless (stringp (nth 8 attr
))
7853 (setcar (nthcdr 8 attr
) (tramp-file-mode-from-int (nth 8 attr
)))
7854 (when (stringp (car attr
))
7855 (aset (nth 8 attr
) 0 ?l
)))
7856 ;; Convert directory indication bit.
7857 (when (string-match "^d" (nth 8 attr
))
7859 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
7860 (when (consp (car attr
))
7861 (if (and (stringp (caar attr
))
7862 (string-match ".+ -> .\\(.+\\)." (caar attr
)))
7863 (setcar attr
(match-string 1 (caar attr
)))
7865 ;; Set file's gid change bit.
7866 (setcar (nthcdr 9 attr
)
7867 (if (numberp (nth 3 attr
))
7868 (not (= (nth 3 attr
)
7869 (tramp-get-remote-gid vec
'integer
)))
7872 (tramp-get-remote-gid vec
'string
)))))
7874 (unless (listp (nth 10 attr
))
7875 (setcar (nthcdr 10 attr
)
7877 (cons (floor (nth 10 attr
) 65536)
7878 (floor (mod (nth 10 attr
) 65536)))
7879 ;; Inodes can be incredible huge. We must hide this.
7880 (error (tramp-get-inode vec
)))))
7881 ;; Set virtual device number.
7882 (setcar (nthcdr 11 attr
)
7883 (tramp-get-device vec
))
7886 (defun tramp-check-cached-permissions (vec access
)
7887 "Check `file-attributes' caches for VEC.
7888 Return t if according to the cache access type ACCESS is known to
7894 ((eq ?x access
) 3))))
7895 (dolist (suffix '("string" "integer") result
)
7901 (tramp-get-file-property
7902 vec
(tramp-file-name-localname vec
)
7903 (concat "file-attributes-" suffix
) nil
))
7905 (tramp-get-connection-property
7906 vec
(concat "uid-" suffix
) nil
))
7908 (tramp-get-connection-property
7909 vec
(concat "gid-" suffix
) nil
)))
7914 (eq t
(car file-attr
))
7915 (null (car file-attr
)))
7917 ;; World accessible.
7918 (eq access
(aref (nth 8 file-attr
) (+ offset
6)))
7919 ;; User accessible and owned by user.
7921 (eq access
(aref (nth 8 file-attr
) offset
))
7922 (equal remote-uid
(nth 2 file-attr
)))
7923 ;; Group accessible and owned by user's
7926 (eq access
(aref (nth 8 file-attr
) (+ offset
3)))
7927 (equal remote-gid
(nth 3 file-attr
)))))))))))
7929 (defun tramp-get-inode (vec)
7930 "Returns the virtual inode number.
7931 If it doesn't exist, generate a new one."
7932 (let ((string (tramp-make-tramp-file-name
7933 (tramp-file-name-method vec
)
7934 (tramp-file-name-user vec
)
7935 (tramp-file-name-host vec
)
7937 (unless (assoc string tramp-inodes
)
7938 (add-to-list 'tramp-inodes
7939 (list string
(length tramp-inodes
))))
7940 (nth 1 (assoc string tramp-inodes
))))
7942 (defun tramp-get-device (vec)
7943 "Returns the virtual device number.
7944 If it doesn't exist, generate a new one."
7945 (let ((string (tramp-make-tramp-file-name
7946 (tramp-file-name-method vec
)
7947 (tramp-file-name-user vec
)
7948 (tramp-file-name-host vec
)
7950 (unless (assoc string tramp-devices
)
7951 (add-to-list 'tramp-devices
7952 (list string
(length tramp-devices
))))
7953 (cons -
1 (nth 1 (assoc string tramp-devices
)))))
7955 (defun tramp-file-mode-from-int (mode)
7956 "Turn an integer representing a file mode into an ls(1)-like string."
7957 (let ((type (cdr (assoc (logand (lsh mode -
12) 15) tramp-file-mode-type-map
)))
7958 (user (logand (lsh mode -
6) 7))
7959 (group (logand (lsh mode -
3) 7))
7960 (other (logand (lsh mode -
0) 7))
7961 (suid (> (logand (lsh mode -
9) 4) 0))
7962 (sgid (> (logand (lsh mode -
9) 2) 0))
7963 (sticky (> (logand (lsh mode -
9) 1) 0)))
7964 (setq user
(tramp-file-mode-permissions user suid
"s"))
7965 (setq group
(tramp-file-mode-permissions group sgid
"s"))
7966 (setq other
(tramp-file-mode-permissions other sticky
"t"))
7967 (concat type user group other
)))
7969 (defun tramp-file-mode-permissions (perm suid suid-text
)
7970 "Convert a permission bitset into a string.
7971 This is used internally by `tramp-file-mode-from-int'."
7972 (let ((r (> (logand perm
4) 0))
7973 (w (> (logand perm
2) 0))
7974 (x (> (logand perm
1) 0)))
7975 (concat (or (and r
"r") "-")
7976 (or (and w
"w") "-")
7977 (or (and suid x suid-text
) ; suid, execute
7978 (and suid
(upcase suid-text
)) ; suid, !execute
7979 (and x
"x") "-")))) ; !suid
7981 (defun tramp-decimal-to-octal (i)
7982 "Return a string consisting of the octal digits of I.
7983 Not actually used. Use `(format \"%o\" i)' instead?"
7984 (cond ((< i
0) (error "Cannot convert negative number to octal"))
7985 ((not (integerp i
)) (error "Cannot convert non-integer to octal"))
7987 (t (concat (tramp-decimal-to-octal (/ i
8))
7988 (number-to-string (% i
8))))))
7990 ;; Kudos to Gerd Moellmann for this suggestion.
7991 (defun tramp-octal-to-decimal (ostr)
7992 "Given a string of octal digits, return a decimal number."
7993 (let ((x (or ostr
"")))
7994 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
7995 (unless (string-match "\\`[0-7]*\\'" x
)
7996 (error "Non-octal junk in string `%s'" x
))
7997 (string-to-number ostr
8)))
7999 (defun tramp-shell-case-fold (string)
8000 "Converts STRING to shell glob pattern which ignores case."
8003 (if (equal (downcase c
) (upcase c
))
8005 (format "[%c%c]" (downcase c
) (upcase c
))))
8010 ;; ------------------------------------------------------------
8011 ;; -- Tramp file names --
8012 ;; ------------------------------------------------------------
8013 ;; Conversion functions between external representation and
8014 ;; internal data structure. Convenience functions for internal
8017 (defun tramp-file-name-p (vec)
8018 "Check, whether VEC is a Tramp object."
8019 (and (vectorp vec
) (= 4 (length vec
))))
8021 (defun tramp-file-name-method (vec)
8022 "Return method component of VEC."
8023 (and (tramp-file-name-p vec
) (aref vec
0)))
8025 (defun tramp-file-name-user (vec)
8026 "Return user component of VEC."
8027 (and (tramp-file-name-p vec
) (aref vec
1)))
8029 (defun tramp-file-name-host (vec)
8030 "Return host component of VEC."
8031 (and (tramp-file-name-p vec
) (aref vec
2)))
8033 (defun tramp-file-name-localname (vec)
8034 "Return localname component of VEC."
8035 (and (tramp-file-name-p vec
) (aref vec
3)))
8037 ;; The user part of a Tramp file name vector can be of kind
8038 ;; "user%domain". Sometimes, we must extract these parts.
8039 (defun tramp-file-name-real-user (vec)
8040 "Return the user name of VEC without domain."
8042 (let ((user (tramp-file-name-user vec
)))
8043 (if (and (stringp user
)
8044 (string-match tramp-user-with-domain-regexp user
))
8045 (match-string 1 user
)
8048 (defun tramp-file-name-domain (vec)
8049 "Return the domain name of VEC."
8051 (let ((user (tramp-file-name-user vec
)))
8053 (string-match tramp-user-with-domain-regexp user
)
8054 (match-string 2 user
)))))
8056 ;; The host part of a Tramp file name vector can be of kind
8057 ;; "host#port". Sometimes, we must extract these parts.
8058 (defun tramp-file-name-real-host (vec)
8059 "Return the host name of VEC without port."
8061 (let ((host (tramp-file-name-host vec
)))
8062 (if (and (stringp host
)
8063 (string-match tramp-host-with-port-regexp host
))
8064 (match-string 1 host
)
8067 (defun tramp-file-name-port (vec)
8068 "Return the port number of VEC."
8070 (let ((host (tramp-file-name-host vec
)))
8072 (string-match tramp-host-with-port-regexp host
)
8073 (string-to-number (match-string 2 host
))))))
8075 (defun tramp-tramp-file-p (name)
8076 "Return t if NAME is a string with Tramp file name syntax."
8078 (and (stringp name
) (string-match tramp-file-name-regexp name
))))
8080 (defun tramp-find-method (method user host
)
8081 "Return the right method string to use.
8082 This is METHOD, if non-nil. Otherwise, do a lookup in
8083 `tramp-default-method-alist'."
8085 (let ((choices tramp-default-method-alist
)
8088 (setq item
(pop choices
))
8089 (when (and (string-match (or (nth 0 item
) "") (or host
""))
8090 (string-match (or (nth 1 item
) "") (or user
"")))
8091 (setq lmethod
(nth 2 item
))
8092 (setq choices nil
)))
8094 tramp-default-method
))
8096 (defun tramp-find-user (method user host
)
8097 "Return the right user string to use.
8098 This is USER, if non-nil. Otherwise, do a lookup in
8099 `tramp-default-user-alist'."
8101 (let ((choices tramp-default-user-alist
)
8104 (setq item
(pop choices
))
8105 (when (and (string-match (or (nth 0 item
) "") (or method
""))
8106 (string-match (or (nth 1 item
) "") (or host
"")))
8107 (setq luser
(nth 2 item
))
8108 (setq choices nil
)))
8110 tramp-default-user
))
8112 (defun tramp-find-host (method user host
)
8113 "Return the right host string to use.
8114 This is HOST, if non-nil. Otherwise, it is `tramp-default-host'."
8115 (or (and (> (length host
) 0) host
)
8116 tramp-default-host
))
8118 (defun tramp-dissect-file-name (name &optional nodefault
)
8119 "Return a `tramp-file-name' structure.
8120 The structure consists of remote method, remote user, remote host
8121 and localname (file name on remote host). If NODEFAULT is
8122 non-nil, the file name parts are not expanded to their default
8125 (let ((match (string-match (nth 0 tramp-file-name-structure
) name
)))
8126 (unless match
(error "Not a Tramp file name: %s" name
))
8127 (let ((method (match-string (nth 1 tramp-file-name-structure
) name
))
8128 (user (match-string (nth 2 tramp-file-name-structure
) name
))
8129 (host (match-string (nth 3 tramp-file-name-structure
) name
))
8130 (localname (match-string (nth 4 tramp-file-name-structure
) name
)))
8131 (when (member method
'("multi" "multiu"))
8133 "`%s' method is no longer supported, see (info \"(tramp)Multi-hops\")"
8136 (when (string-match tramp-prefix-ipv6-regexp host
)
8137 (setq host
(replace-match "" nil t host
)))
8138 (when (string-match tramp-postfix-ipv6-regexp host
)
8139 (setq host
(replace-match "" nil t host
))))
8141 (vector method user host localname
)
8143 (tramp-find-method method user host
)
8144 (tramp-find-user method user host
)
8145 (tramp-find-host method user host
)
8148 (defun tramp-equal-remote (file1 file2
)
8149 "Check, whether the remote parts of FILE1 and FILE2 are identical.
8150 The check depends on method, user and host name of the files. If
8151 one of the components is missing, the default values are used.
8152 The local file name parts of FILE1 and FILE2 are not taken into
8157 (tramp-equal-remote \"/ssh::/etc\" \"/<your host name>:/home\")
8159 would yield `t'. On the other hand, the following check results in nil:
8161 (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")"
8162 (and (stringp (file-remote-p file1
))
8163 (stringp (file-remote-p file2
))
8164 (string-equal (file-remote-p file1
) (file-remote-p file2
))))
8166 (defun tramp-make-tramp-file-name (method user host localname
)
8167 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME."
8168 (concat tramp-prefix-format
8169 (when (not (zerop (length method
)))
8170 (concat method tramp-postfix-method-format
))
8171 (when (not (zerop (length user
)))
8172 (concat user tramp-postfix-user-format
))
8174 (if (string-match tramp-ipv6-regexp host
)
8175 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format
)
8177 tramp-postfix-host-format
8178 (when localname localname
)))
8180 (defun tramp-completion-make-tramp-file-name (method user host localname
)
8181 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME.
8182 It must not be a complete Tramp file name, but as long as there are
8183 necessary only. This function will be used in file name completion."
8184 (concat tramp-prefix-format
8185 (when (not (zerop (length method
)))
8186 (concat method tramp-postfix-method-format
))
8187 (when (not (zerop (length user
)))
8188 (concat user tramp-postfix-user-format
))
8189 (when (not (zerop (length host
)))
8191 (if (string-match tramp-ipv6-regexp host
)
8192 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format
)
8194 tramp-postfix-host-format
))
8195 (when localname localname
)))
8197 (defun tramp-make-copy-program-file-name (vec)
8198 "Create a file name suitable to be passed to `rcp' and workalikes."
8199 (let ((user (tramp-file-name-user vec
))
8200 (host (tramp-file-name-real-host vec
))
8201 (localname (tramp-shell-quote-argument
8202 (tramp-file-name-localname vec
))))
8203 (if (not (zerop (length user
)))
8204 (format "%s@%s:%s" user host localname
)
8205 (format "%s:%s" host localname
))))
8207 (defun tramp-method-out-of-band-p (vec size
)
8208 "Return t if this is an out-of-band method, nil otherwise."
8210 ;; It shall be an out-of-band method.
8211 (tramp-get-method-parameter (tramp-file-name-method vec
) 'tramp-copy-program
)
8212 ;; Either the file size is large enough, or (in rare cases) there
8213 ;; does not exist a remote encoding.
8214 (or (null tramp-copy-size-limit
)
8215 (> size tramp-copy-size-limit
)
8216 (null (tramp-get-inline-coding vec
"remote-encoding" size
)))))
8218 (defun tramp-local-host-p (vec)
8219 "Return t if this points to the local host, nil otherwise."
8220 ;; We cannot use `tramp-file-name-real-host'. A port is an
8221 ;; indication for an ssh tunnel or alike.
8222 (let ((host (tramp-file-name-host vec
)))
8225 (string-match tramp-local-host-regexp host
)
8226 ;; The method shall be applied to one of the shell file name
8227 ;; handler. `tramp-local-host-p' is also called for "smb" and
8228 ;; alike, where it must fail.
8229 (tramp-get-method-parameter
8230 (tramp-file-name-method vec
) 'tramp-login-program
)
8231 ;; The local temp directory must be writable for the other user.
8233 (tramp-make-tramp-file-name
8234 (tramp-file-name-method vec
)
8235 (tramp-file-name-user vec
)
8237 (tramp-compat-temporary-file-directory)))
8238 ;; On some systems, chown runs only for root.
8239 (or (zerop (user-uid))
8240 (zerop (tramp-get-remote-uid vec
'integer
))))))
8242 ;; Variables local to connection.
8244 (defun tramp-get-remote-path (vec)
8245 (with-connection-property
8246 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
8247 ;; cache the result for the session only. Otherwise, the result
8248 ;; is cached persistently.
8249 (if (memq 'tramp-own-remote-path tramp-remote-path
)
8250 (tramp-get-connection-process vec
)
8253 (let* ((remote-path (copy-tree tramp-remote-path
))
8254 (elt1 (memq 'tramp-default-remote-path remote-path
))
8255 (elt2 (memq 'tramp-own-remote-path remote-path
))
8256 (default-remote-path
8259 (tramp-send-command-and-read
8260 vec
"echo \\\"`getconf PATH`\\\"")
8261 ;; Default if "getconf" is not available.
8265 "`getconf PATH' not successful, using default value \"%s\"."
8271 (tramp-send-command-and-read vec
"echo \\\"$PATH\\\"")
8272 ;; Default if "getconf" is not available.
8275 vec
3 "$PATH not set, ignoring `tramp-own-remote-path'.")
8278 ;; Replace place holder `tramp-default-remote-path'.
8282 (tramp-compat-split-string default-remote-path
":")
8284 (setq remote-path
(delq 'tramp-default-remote-path remote-path
)))
8286 ;; Replace place holder `tramp-own-remote-path'.
8290 (tramp-compat-split-string own-remote-path
":")
8292 (setq remote-path
(delq 'tramp-own-remote-path remote-path
)))
8294 ;; Remove double entries.
8295 (setq elt1 remote-path
)
8297 (while (and (car elt1
) (setq elt2
(member (car elt1
) (cdr elt1
))))
8299 (setq elt1
(cdr elt1
)))
8301 ;; Remove non-existing directories.
8309 (tramp-make-tramp-file-name
8310 (tramp-file-name-method vec
)
8311 (tramp-file-name-user vec
)
8312 (tramp-file-name-host vec
)
8317 (defun tramp-get-remote-tmpdir (vec)
8318 (with-connection-property vec
"tmp-directory"
8319 (let ((dir (tramp-shell-quote-argument "/tmp")))
8321 (tramp-send-command-and-check
8322 vec
(format "%s -d %s" (tramp-get-test-command vec
) dir
)))
8324 (tramp-send-command-and-check
8325 vec
(format "%s -w %s" (tramp-get-test-command vec
) dir
))))
8327 (tramp-error vec
'file-error
"Directory %s not accessible" dir
)))))
8329 (defun tramp-get-ls-command (vec)
8330 (with-connection-property vec
"ls"
8331 (tramp-message vec
5 "Finding a suitable `ls' command")
8334 (dolist (cmd '("ls" "gnuls" "gls"))
8335 (let ((dl (tramp-get-remote-path vec
))
8337 (while (and dl
(setq result
(tramp-find-executable vec cmd dl t t
)))
8338 ;; Check parameters. On busybox, "ls" output coloring is
8339 ;; enabled by default sometimes. So we try to disable it
8340 ;; when possible. $LS_COLORING is not supported there.
8341 ;; Some "ls" versions are sensible wrt the order of
8342 ;; arguments, they fail when "-al" is after the
8343 ;; "--color=never" argument (for example on FreeBSD).
8344 (when (zerop (tramp-send-command-and-check
8345 vec
(format "%s -lnd /" result
)))
8346 (when (zerop (tramp-send-command-and-check
8348 "%s --color=never -al /dev/null" result
)))
8349 (setq result
(concat result
" --color=never")))
8350 (throw 'ls-found result
))
8351 (setq dl
(cdr dl
))))))
8352 (tramp-error vec
'file-error
"Couldn't find a proper `ls' command"))))
8354 (defun tramp-get-ls-command-with-dired (vec)
8356 (with-connection-property vec
"ls-dired"
8357 (tramp-message vec
5 "Checking, whether `ls --dired' works")
8358 ;; Some "ls" versions are sensible wrt the order of arguments,
8359 ;; they fail when "-al" is after the "--dired" argument (for
8360 ;; example on FreeBSD).
8361 (zerop (tramp-send-command-and-check
8362 vec
(format "%s --dired -al /dev/null"
8363 (tramp-get-ls-command vec
)))))))
8365 (defun tramp-get-test-command (vec)
8366 (with-connection-property vec
"test"
8367 (tramp-message vec
5 "Finding a suitable `test' command")
8368 (if (zerop (tramp-send-command-and-check vec
"test 0"))
8370 (tramp-find-executable vec
"test" (tramp-get-remote-path vec
)))))
8372 (defun tramp-get-test-nt-command (vec)
8373 ;; Does `test A -nt B' work? Use abominable `find' construct if it
8374 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
8375 ;; for otherwise the shell crashes.
8376 (with-connection-property vec
"test-nt"
8380 vec
(format "( %s / -nt / )" (tramp-get-test-command vec
)))
8381 (with-current-buffer (tramp-get-buffer vec
)
8382 (goto-char (point-min))
8383 (when (looking-at (regexp-quote tramp-end-of-output
))
8384 (format "%s %%s -nt %%s" (tramp-get-test-command vec
)))))
8389 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
8390 (tramp-get-test-command vec
)))
8391 "tramp_test_nt %s %s"))))
8393 (defun tramp-get-file-exists-command (vec)
8394 (with-connection-property vec
"file-exists"
8395 (tramp-message vec
5 "Finding command to check if file exists")
8396 (tramp-find-file-exists-command vec
)))
8398 (defun tramp-get-remote-ln (vec)
8399 (with-connection-property vec
"ln"
8400 (tramp-message vec
5 "Finding a suitable `ln' command")
8401 (tramp-find-executable vec
"ln" (tramp-get-remote-path vec
))))
8403 (defun tramp-get-remote-perl (vec)
8404 (with-connection-property vec
"perl"
8405 (tramp-message vec
5 "Finding a suitable `perl' command")
8407 (or (tramp-find-executable vec
"perl5" (tramp-get-remote-path vec
))
8408 (tramp-find-executable
8409 vec
"perl" (tramp-get-remote-path vec
)))))
8410 ;; We must check also for some Perl modules.
8412 (with-connection-property vec
"perl-file-spec"
8414 (tramp-send-command-and-check
8415 vec
(format "%s -e 'use File::Spec;'" result
))))
8416 (with-connection-property vec
"perl-cwd-realpath"
8418 (tramp-send-command-and-check
8419 vec
(format "%s -e 'use Cwd \"realpath\";'" result
)))))
8422 (defun tramp-get-remote-stat (vec)
8423 (with-connection-property vec
"stat"
8424 (tramp-message vec
5 "Finding a suitable `stat' command")
8425 (let ((result (tramp-find-executable
8426 vec
"stat" (tramp-get-remote-path vec
)))
8428 ;; Check whether stat(1) returns usable syntax. %s does not
8429 ;; work on older AIX systems.
8432 ;; We don't want to display an error message.
8433 (with-temp-message (or (current-message) "")
8435 (tramp-send-command-and-read
8436 vec
(format "%s -c '(\"%%N\" %%s)' /" result
))
8438 (unless (and (listp tmp
) (stringp (car tmp
))
8439 (string-match "^./.$" (car tmp
))
8440 (integerp (cadr tmp
)))
8444 (defun tramp-get-remote-readlink (vec)
8445 (with-connection-property vec
"readlink"
8446 (tramp-message vec
5 "Finding a suitable `readlink' command")
8447 (let ((result (tramp-find-executable
8448 vec
"readlink" (tramp-get-remote-path vec
))))
8450 ;; We don't want to display an error message.
8451 (with-temp-message (or (current-message) "")
8454 (tramp-send-command-and-check
8455 vec
(format "%s --canonicalize-missing /" result
)))
8459 (defun tramp-get-remote-trash (vec)
8460 (with-connection-property vec
"trash"
8461 (tramp-message vec
5 "Finding a suitable `trash' command")
8462 (tramp-find-executable vec
"trash" (tramp-get-remote-path vec
))))
8464 (defun tramp-get-remote-id (vec)
8465 (with-connection-property vec
"id"
8466 (tramp-message vec
5 "Finding POSIX `id' command")
8469 (let ((dl (tramp-get-remote-path vec
))
8471 (while (and dl
(setq result
(tramp-find-executable vec
"id" dl t t
)))
8472 ;; Check POSIX parameter.
8473 (when (zerop (tramp-send-command-and-check
8474 vec
(format "%s -u" result
)))
8475 (throw 'id-found result
))
8476 (setq dl
(cdr dl
)))))
8477 (tramp-error vec
'file-error
"Couldn't find a POSIX `id' command"))))
8479 (defun tramp-get-remote-uid (vec id-format
)
8480 (with-connection-property vec
(format "uid-%s" id-format
)
8481 (let ((res (tramp-send-command-and-read
8483 (format "%s -u%s %s"
8484 (tramp-get-remote-id vec
)
8485 (if (equal id-format
'integer
) "" "n")
8486 (if (equal id-format
'integer
)
8487 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8488 ;; The command might not always return a number.
8489 (if (and (equal id-format
'integer
) (not (integerp res
))) -
1 res
))))
8491 (defun tramp-get-remote-gid (vec id-format
)
8492 (with-connection-property vec
(format "gid-%s" id-format
)
8493 (let ((res (tramp-send-command-and-read
8495 (format "%s -g%s %s"
8496 (tramp-get-remote-id vec
)
8497 (if (equal id-format
'integer
) "" "n")
8498 (if (equal id-format
'integer
)
8499 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8500 ;; The command might not always return a number.
8501 (if (and (equal id-format
'integer
) (not (integerp res
))) -
1 res
))))
8503 (defun tramp-get-local-uid (id-format)
8504 (if (equal id-format
'integer
) (user-uid) (user-login-name)))
8506 (defun tramp-get-local-gid (id-format)
8507 (nth 3 (tramp-compat-file-attributes "~/" id-format
)))
8509 ;; Some predefined connection properties.
8510 (defun tramp-get-inline-compress (vec prop size
)
8511 "Return the compress command related to PROP.
8512 PROP is either `inline-compress' or `inline-decompress'. SIZE is
8513 the length of the file to be compressed.
8515 If no corresponding command is found, nil is returned."
8516 (when (and (integerp tramp-inline-compress-start-size
)
8517 (> size tramp-inline-compress-start-size
))
8518 (with-connection-property vec prop
8519 (tramp-find-inline-compress vec
)
8520 (tramp-get-connection-property vec prop nil
))))
8522 (defun tramp-get-inline-coding (vec prop size
)
8523 "Return the coding command related to PROP.
8524 PROP is either `remote-encoding', `remode-decoding',
8525 `local-encoding' or `local-decoding'.
8527 SIZE is the length of the file to be coded. Depending on SIZE,
8528 compression might be applied.
8530 If no corresponding command is found, nil is returned.
8531 Otherwise, either a string is returned which contains a `%s' mark
8532 to be used for the respective input or output file; or a Lisp
8533 function cell is returned to be applied on a buffer."
8535 (with-connection-property vec prop
8536 (tramp-find-inline-encoding vec
)
8537 (tramp-get-connection-property vec prop nil
)))
8538 (prop1 (if (string-match "encoding" prop
)
8539 "inline-compress" "inline-decompress"))
8541 ;; The connection property might have been cached. So we must send
8542 ;; the script to the remote side - maybe.
8543 (when (and coding
(symbolp coding
) (string-match "remote" prop
))
8544 (let ((name (symbol-name coding
)))
8545 (while (string-match (regexp-quote "-") name
)
8546 (setq name
(replace-match "_" nil t name
)))
8547 (tramp-maybe-send-script vec
(symbol-value coding
) name
)
8548 (setq coding name
)))
8550 ;; Check for the `compress' command.
8551 (setq compress
(tramp-get-inline-compress vec prop1 size
))
8552 ;; Return the value.
8554 ((and compress
(symbolp coding
))
8555 (if (string-match "decompress" prop1
)
8558 (let ((coding-system-for-write 'binary
)
8559 (coding-system-for-read 'binary
))
8561 'call-process-region
(point-min) (point-max)
8562 (car (split-string ,compress
)) t t nil
8563 (cdr (split-string ,compress
)))))
8565 (let ((coding-system-for-write 'binary
)
8566 (coding-system-for-read 'binary
))
8568 'call-process-region beg end
8569 (car (split-string ,compress
)) t t nil
8570 (cdr (split-string ,compress
))))
8571 (,coding
(point-min) (point-max)))))
8574 ((and compress
(string-match "decoding" prop
))
8575 (format "(%s | %s >%%s)" coding compress
))
8577 (format "(%s <%%s | %s)" compress coding
))
8578 ((string-match "decoding" prop
)
8579 (format "%s >%%s" coding
))
8581 (format "%s <%%s" coding
))))))
8583 (defun tramp-get-method-parameter (method param
)
8584 "Return the method parameter PARAM.
8585 If the `tramp-methods' entry does not exist, return nil."
8586 (let ((entry (assoc param
(assoc method tramp-methods
))))
8587 (when entry
(cadr entry
))))
8589 ;; Auto saving to a special directory.
8591 (defun tramp-exists-file-name-handler (operation &rest args
)
8592 "Check, whether OPERATION runs a file name handler."
8593 ;; The file name handler is determined on base of either an
8594 ;; argument, `buffer-file-name', or `default-directory'.
8596 (let* ((buffer-file-name "/")
8597 (default-directory "/")
8598 (fnha file-name-handler-alist
)
8599 (check-file-name-operation operation
)
8600 (file-name-handler-alist
8603 (lambda (operation &rest args
)
8604 "Returns OPERATION if it is the one to be checked."
8605 (if (equal check-file-name-operation operation
)
8607 (let ((file-name-handler-alist fnha
))
8608 (apply operation args
))))))))
8609 (equal (apply operation args
) operation
))
8612 (unless (tramp-exists-file-name-handler 'make-auto-save-file-name
)
8613 (defadvice make-auto-save-file-name
8614 (around tramp-advice-make-auto-save-file-name
() activate
)
8615 "Invoke `tramp-handle-make-auto-save-file-name' for Tramp files."
8616 (if (tramp-tramp-file-p (buffer-file-name))
8617 ;; We cannot call `tramp-handle-make-auto-save-file-name'
8618 ;; directly, because this would bypass the locking mechanism.
8619 (setq ad-return-value
8620 (tramp-file-name-handler 'make-auto-save-file-name
))
8626 'make-auto-save-file-name
8627 'around
'tramp-advice-make-auto-save-file-name
)
8628 (ad-activate 'make-auto-save-file-name
))))
8630 ;; In XEmacs < 21.5, autosaved remote files have permission 0666 minus
8631 ;; umask. This is a security threat.
8633 (defun tramp-set-auto-save-file-modes ()
8634 "Set permissions of autosaved remote files to the original permissions."
8635 (let ((bfn (buffer-file-name)))
8636 (when (and (tramp-tramp-file-p bfn
)
8638 (stringp buffer-auto-save-file-name
)
8639 (not (equal bfn buffer-auto-save-file-name
)))
8640 (unless (file-exists-p buffer-auto-save-file-name
)
8641 (write-region "" nil buffer-auto-save-file-name
))
8642 ;; Permissions should be set always, because there might be an old
8643 ;; auto-saved file belonging to another original file. This could
8644 ;; be a security threat.
8645 (set-file-modes buffer-auto-save-file-name
8646 (or (file-modes bfn
) (tramp-octal-to-decimal "0600"))))))
8648 (unless (and (featurep 'xemacs
)
8649 (= emacs-major-version
21)
8650 (> emacs-minor-version
4))
8651 (add-hook 'auto-save-hook
'tramp-set-auto-save-file-modes
)
8652 (add-hook 'tramp-unload-hook
8654 (remove-hook 'auto-save-hook
'tramp-set-auto-save-file-modes
))))
8656 (defun tramp-subst-strs-in-string (alist string
)
8657 "Replace all occurrences of the string FROM with TO in STRING.
8658 ALIST is of the form ((FROM . TO) ...)."
8661 (let* ((pr (car alist
))
8664 (while (string-match (regexp-quote from
) string
)
8665 (setq string
(replace-match to t t string
)))
8666 (setq alist
(cdr alist
))))
8669 ;; ------------------------------------------------------------
8670 ;; -- Compatibility functions section --
8671 ;; ------------------------------------------------------------
8673 (defun tramp-read-passwd (proc &optional prompt
)
8674 "Read a password from user (compat function).
8675 Consults the auth-source package.
8676 Invokes `password-read' if available, `read-passwd' else."
8677 (let* ((key (tramp-make-tramp-file-name
8678 tramp-current-method tramp-current-user
8679 tramp-current-host
""))
8682 (with-current-buffer (process-buffer proc
)
8683 (tramp-check-for-regexp proc tramp-password-prompt-regexp
)
8684 (format "%s for %s " (capitalize (match-string 1)) key
)))))
8685 (with-parsed-tramp-file-name key nil
8688 ;; See if auth-sources contains something useful, if it's bound.
8689 (and (boundp 'auth-sources
)
8690 (tramp-get-connection-property v
"first-password-request" nil
)
8691 ;; Try with Tramp's current method.
8692 (tramp-compat-funcall
8693 'auth-source-user-or-password
8694 "password" tramp-current-host tramp-current-method
))
8695 ;; Try the password cache.
8696 (when (functionp 'password-read
)
8697 (unless (tramp-get-connection-property
8698 v
"first-password-request" nil
)
8699 (tramp-compat-funcall 'password-cache-remove key
))
8701 (tramp-compat-funcall 'password-read pw-prompt key
)))
8702 (tramp-compat-funcall 'password-cache-add key password
)
8704 ;; Else, get the password interactively.
8705 (read-passwd pw-prompt
))
8706 (tramp-set-connection-property v
"first-password-request" nil
)))))
8708 (defun tramp-clear-passwd (vec)
8709 "Clear password cache for connection related to VEC."
8710 (tramp-compat-funcall
8711 'password-cache-remove
8712 (tramp-make-tramp-file-name
8713 (tramp-file-name-method vec
)
8714 (tramp-file-name-user vec
)
8715 (tramp-file-name-host vec
)
8718 ;; Snarfed code from time-date.el and parse-time.el
8720 (defconst tramp-half-a-year
'(241 17024)
8721 "Evaluated by \"(days-to-time 183)\".")
8723 (defconst tramp-parse-time-months
8724 '(("jan" .
1) ("feb" .
2) ("mar" .
3)
8725 ("apr" .
4) ("may" .
5) ("jun" .
6)
8726 ("jul" .
7) ("aug" .
8) ("sep" .
9)
8727 ("oct" .
10) ("nov" .
11) ("dec" .
12))
8728 "Alist mapping month names to integers.")
8730 (defun tramp-time-less-p (t1 t2
)
8731 "Say whether time value T1 is less than time value T2."
8732 (unless t1
(setq t1
'(0 0)))
8733 (unless t2
(setq t2
'(0 0)))
8734 (or (< (car t1
) (car t2
))
8735 (and (= (car t1
) (car t2
))
8736 (< (nth 1 t1
) (nth 1 t2
)))))
8738 (defun tramp-time-subtract (t1 t2
)
8739 "Subtract two time values.
8740 Return the difference in the format of a time value."
8741 (unless t1
(setq t1
'(0 0)))
8742 (unless t2
(setq t2
'(0 0)))
8743 (let ((borrow (< (cadr t1
) (cadr t2
))))
8744 (list (- (car t1
) (car t2
) (if borrow
1 0))
8745 (- (+ (if borrow
65536 0) (cadr t1
)) (cadr t2
)))))
8747 (defun tramp-time-diff (t1 t2
)
8748 "Return the difference between the two times, in seconds.
8749 T1 and T2 are time values (as returned by `current-time' for example)."
8750 ;; Pacify byte-compiler with `symbol-function'.
8751 (cond ((and (fboundp 'subtract-time
)
8752 (fboundp 'float-time
))
8753 (tramp-compat-funcall
8754 'float-time
(tramp-compat-funcall 'subtract-time t1 t2
)))
8755 ((and (fboundp 'subtract-time
)
8756 (fboundp 'time-to-seconds
))
8757 (tramp-compat-funcall
8758 'time-to-seconds
(tramp-compat-funcall 'subtract-time t1 t2
)))
8759 ((fboundp 'itimer-time-difference
)
8760 (tramp-compat-funcall
8761 'itimer-time-difference
8762 (if (< (length t1
) 3) (append t1
'(0)) t1
)
8763 (if (< (length t2
) 3) (append t2
'(0)) t2
)))
8765 (let ((time (tramp-time-subtract t1 t2
)))
8766 (+ (* (car time
) 65536.0)
8768 (/ (or (nth 2 time
) 0) 1000000.0))))))
8770 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type
)
8771 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
8772 EOL-TYPE can be one of `dos', `unix', or `mac'."
8773 (cond ((fboundp 'coding-system-change-eol-conversion
)
8774 (tramp-compat-funcall
8775 'coding-system-change-eol-conversion coding-system eol-type
))
8776 ((fboundp 'subsidiary-coding-system
)
8777 (tramp-compat-funcall
8778 'subsidiary-coding-system coding-system
8779 (cond ((eq eol-type
'dos
) 'crlf
)
8780 ((eq eol-type
'unix
) 'lf
)
8781 ((eq eol-type
'mac
) 'cr
)
8783 (error "Unknown EOL-TYPE `%s', must be %s"
8785 "`dos', `unix', or `mac'")))))
8786 (t (error "Can't change EOL conversion -- is MULE missing?"))))
8788 (defun tramp-set-process-query-on-exit-flag (process flag
)
8789 "Specify if query is needed for process when Emacs is exited.
8790 If the second argument flag is non-nil, Emacs will query the user before
8791 exiting if process is running."
8792 (if (fboundp 'set-process-query-on-exit-flag
)
8793 (tramp-compat-funcall 'set-process-query-on-exit-flag process flag
)
8794 (tramp-compat-funcall 'process-kill-without-query process flag
)))
8797 ;; ------------------------------------------------------------
8798 ;; -- Kludges section --
8799 ;; ------------------------------------------------------------
8801 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
8802 ;; does not deal well with newline characters. Newline is replaced by
8803 ;; backslash newline. But if, say, the string `a backslash newline b'
8804 ;; is passed to a shell, the shell will expand this into "ab",
8805 ;; completely omitting the newline. This is not what was intended.
8806 ;; It does not appear to be possible to make the function
8807 ;; `shell-quote-argument' work with newlines without making it
8808 ;; dependent on the shell used. But within this package, we know that
8809 ;; we will always use a Bourne-like shell, so we use an approach which
8812 ;; The approach is simple: we call `shell-quote-argument', then
8813 ;; massage the newline part of the result.
8815 ;; This function should produce a string which is grokked by a Unix
8816 ;; shell, even if the Emacs is running on Windows. Since this is the
8817 ;; kludges section, we bind `system-type' in such a way that
8818 ;; `shell-quote-arguments' behaves as if on Unix.
8820 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
8821 ;; function to work with Bourne-like shells.
8823 ;; CCC: This function should be rewritten so that
8824 ;; `shell-quote-argument' is not used. This way, we are safe from
8825 ;; changes in `shell-quote-argument'.
8826 (defun tramp-shell-quote-argument (s)
8827 "Similar to `shell-quote-argument', but groks newlines.
8828 Only works for Bourne-like shells."
8829 (let ((system-type 'not-windows
))
8831 (let ((result (shell-quote-argument s
))
8832 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line
))))
8833 (when (and (>= (length result
) 2)
8834 (string= (substring result
0 2) "\\~"))
8835 (setq result
(substring result
1)))
8836 (while (string-match nl result
)
8837 (setq result
(replace-match (format "'%s'" tramp-rsh-end-of-line
)
8841 ;; Checklist for `tramp-unload-hook'
8842 ;; - Unload all `tramp-*' packages
8843 ;; - Reset `file-name-handler-alist'
8844 ;; - Cleanup hooks where Tramp functions are in
8845 ;; - Cleanup advised functions
8846 ;; - Cleanup autoloads
8848 (defun tramp-unload-tramp ()
8849 "Discard Tramp from loading remote files."
8851 ;; When Tramp is not loaded yet, its autoloads are still active.
8852 (tramp-unload-file-name-handlers)
8853 ;; ange-ftp settings must be enabled.
8854 (tramp-compat-funcall 'tramp-ftp-enable-ange-ftp
)
8855 ;; Maybe its not loaded yet.
8857 (unload-feature 'tramp
'force
)
8860 (when (and load-in-progress
8861 (string-match "Loading tramp..." (or (current-message) "")))
8862 (message "Loading tramp...done"))
8868 ;; * Handle nonlocal exits such as C-g.
8869 ;; * But it would probably be better to use with-local-quit at the
8870 ;; place where it's actually needed: around any potentially
8871 ;; indefinitely blocking piece of code. In this case it would be
8872 ;; within Tramp around one of its calls to accept-process-output (or
8873 ;; around one of the loops that calls accept-process-output)
8874 ;; (Stefan Monnier).
8875 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
8876 ;; `shell-quote-argument'.
8877 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
8878 ;; by the files in that directory. Add this here.
8879 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
8880 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
8881 ;; * Don't use globbing for directories with many files, as this is
8882 ;; likely to produce long command lines, and some shells choke on
8883 ;; long command lines.
8884 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
8885 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
8886 ;; * abbreviate-file-name
8887 ;; * Better error checking. At least whenever we see something
8888 ;; strange when doing zerop, we should kill the process and start
8889 ;; again. (Greg Stark)
8890 ;; * Remove unneeded parameters from methods.
8891 ;; * Make it work for different encodings, and for different file name
8892 ;; encodings, too. (Daniel Pittman)
8893 ;; * Don't search for perl5 and perl. Instead, only search for perl and
8894 ;; then look if it's the right version (with `perl -v').
8895 ;; * When editing a remote CVS controlled file as a different user, VC
8896 ;; gets confused about the file locking status. Try to find out why
8897 ;; the workaround doesn't work.
8898 ;; * Username and hostname completion.
8899 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode-p'.
8900 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
8901 ;; Code is nearly identical.
8902 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
8903 ;; until the last but one hop via `start-file-process'. Apply it
8904 ;; also for ftp and smb.
8905 ;; * WIBNI if we had a command "trampclient"? If I was editing in
8906 ;; some shell with root priviledges, it would be nice if I could
8908 ;; trampclient filename.c
8909 ;; as an editor, and the _current_ shell would connect to an Emacs
8910 ;; server and would be used in an existing non-priviledged Emacs
8911 ;; session for doing the editing in question.
8912 ;; That way, I need not tell Emacs my password again and be afraid
8913 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
8914 ;; once display a just typed password in the context of a keyboard
8915 ;; sequence prompt for a question immediately following in a shell
8916 ;; script run within Emacs -- nasty).
8917 ;; And if I have some ssh session running to a different computer,
8918 ;; having the possibility of passing a local file there to a local
8919 ;; Emacs session (in case I can arrange for a connection back) would
8921 ;; Likely the corresponding Tramp server should not allow the
8922 ;; equivalent of the emacsclient -eval option in order to make this
8923 ;; reasonably unproblematic. And maybe trampclient should have some
8924 ;; way of passing credentials, like by using an SSL socket or
8925 ;; something. (David Kastrup)
8926 ;; * Reconnect directly to a compliant shell without first going
8927 ;; through the user's default shell. (Pete Forman)
8928 ;; * Make `tramp-default-user' obsolete.
8929 ;; * How can I interrupt the remote process with a signal
8930 ;; (interrupt-process seems not to work)? (Markus Triska)
8931 ;; * Avoid the local shell entirely for starting remote processes. If
8932 ;; so, I think even a signal, when delivered directly to the local
8933 ;; SSH instance, would correctly be propagated to the remote process
8934 ;; automatically; possibly SSH would have to be started with
8935 ;; "-t". (Markus Triska)
8936 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
8937 ;; isn't on the remote host. (Mark A. Hershberger)
8938 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
8939 ;; * Implement a general server-local-variable mechanism, as there are
8940 ;; probably other variables that need different values for different
8941 ;; servers too. The user could then configure a variable (such as
8942 ;; tramp-server-local-variable-alist) to define any such variables
8943 ;; that they need to, which would then be let bound as appropriate
8944 ;; in tramp functions. (Jason Rumney)
8945 ;; * Optimize out-of-band copying, when both methods are scp-like (not
8947 ;; * Keep a second connection open for out-of-band methods like scp or
8949 ;; * Support ptys in `tramp-handle-start-file-process'. (Bug#4604, Bug#6360)
8950 ;; * IMHO, it's a drawback that currently Tramp doesn't support
8951 ;; Unicode in Dired file names by default. Is it possible to
8952 ;; improve Tramp to set LC_ALL to "C" only for commands where Tramp
8953 ;; expects English? Or just to set LC_MESSAGES to "C" if Tramp
8954 ;; expects only English messages? (Juri Linkov)
8955 ;; * Make shadowfile.el grok Tramp filenames. (Bug#4526, Bug#4846)
8956 ;; * Do not handle files with drive letter as remote. (Bug#5447)
8957 ;; * Load Tramp subpackages only when needed. (Bug#1529, Bug#5448, Bug#5705)
8958 ;; * Try telnet+curl as new method. It might be useful for busybox,
8959 ;; without built-in uuencode/uudecode.
8960 ;; * Let `shell-dynamic-complete-*' and `comint-dynamic-complete' work
8962 ;; * Load ~/.emacs_SHELLNAME on the remote host for `shell'.
8964 ;; Functions for file-name-handler-alist:
8965 ;; diff-latest-backup-file -- in diff.el
8967 ;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
8968 ;;; tramp.el ends here