1 ;;; tramp.el --- Transparent Remote Access, Multiple Protocol
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009 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 21.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
) ; from Gnus 5.8, also in tar ball
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 ;; Load gateways. It needs `make-network-process' from Emacs 22.
144 (when (functionp 'make-network-process
) 'tramp-gw
)))
148 (add-hook 'tramp-unload-hook
150 (when (featurep (quote ,feature
))
151 (unload-feature (quote ,feature
) 'force
)))))))
153 ;;; User Customizable Internal Variables:
156 "Edit remote files with a combination of rsh and rcp or similar programs."
160 ;; Maybe we need once a real Tramp mode, with key bindings etc.
162 (defcustom tramp-mode t
163 "*Whether Tramp is enabled.
164 If it is set to nil, all remote file names are used literally."
168 (defcustom tramp-verbose
3
169 "*Verbosity level for Tramp.
170 Any level x includes messages for all levels 1 .. x-1. The levels are
172 0 silent (no tramp messages at all)
175 3 connection to remote hosts (default level)
178 6 sent and received strings
180 8 connection properties
187 (when (boundp 'backup-directory-alist
)
188 (defcustom tramp-backup-directory-alist nil
189 "Alist of filename patterns and backup directory names.
190 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
191 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
192 is a local file name, the backup directory is prepended with Tramp file
193 name prefix \(method, user, host\) of file.
195 \(setq tramp-backup-directory-alist backup-directory-alist\)
197 gives the same backup policy for Tramp files on their hosts like the
198 policy for local files."
200 :type
'(repeat (cons (regexp :tag
"Regexp matching filename")
201 (directory :tag
"Backup directory name"))))))
203 ;; XEmacs case. We cannot check for `bkup-backup-directory-info', because
204 ;; the package "backup-dir" might not be loaded yet.
206 (when (featurep 'xemacs
)
207 (defcustom tramp-bkup-backup-directory-info nil
208 "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
209 It has the same meaning like `bkup-backup-directory-info' from package
210 `backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local
211 file name, the backup directory is prepended with Tramp file name prefix
212 \(method, user, host\) of file.
214 \(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\)
216 gives the same backup policy for Tramp files on their hosts like the
217 policy for local files."
219 (list (regexp :tag
"File regexp")
220 (string :tag
"Backup Dir")
225 (const search-upward
))))
228 (defcustom tramp-auto-save-directory nil
229 "*Put auto-save files in this directory, if set.
230 The idea is to use a local directory so that auto-saving is faster."
232 :type
'(choice (const nil
) string
))
234 (defcustom tramp-encoding-shell
235 (if (memq system-type
'(windows-nt))
238 "*Use this program for encoding and decoding commands on the local host.
239 This shell is used to execute the encoding and decoding command on the
240 local host, so if you want to use `~' in those commands, you should
241 choose a shell here which groks tilde expansion. `/bin/sh' normally
242 does not understand tilde expansion.
244 For encoding and deocding, commands like the following are executed:
246 /bin/sh -c COMMAND < INPUT > OUTPUT
248 This variable can be used to change the \"/bin/sh\" part. See the
249 variable `tramp-encoding-command-switch' for the \"-c\" part.
251 Note that this variable is not used for remote commands. There are
252 mechanisms in tramp.el which automatically determine the right shell to
253 use for the remote host."
255 :type
'(file :must-match t
))
257 (defcustom tramp-encoding-command-switch
258 (if (string-match "cmd\\.exe" tramp-encoding-shell
)
261 "*Use this switch together with `tramp-encoding-shell' for local commands.
262 See the variable `tramp-encoding-shell' for more information."
266 (defcustom tramp-copy-size-limit
10240
267 "*The maximum file size where inline copying is preferred over an out-of-the-band copy."
271 (defcustom tramp-terminal-type
"dumb"
272 "*Value of TERM environment variable for logging in to remote host.
273 Because Tramp wants to parse the output of the remote shell, it is easily
274 confused by ANSI color escape sequences and suchlike. Often, shell init
275 files conditionalize this setup based on the TERM environment variable."
279 (defvar tramp-methods
280 `(("rcp" (tramp-login-program "rsh")
281 (tramp-login-args (("%h") ("-l" "%u")))
282 (tramp-remote-sh "/bin/sh")
283 (tramp-copy-program "rcp")
284 (tramp-copy-args (("-p" "%k")))
285 (tramp-copy-keep-date t
)
286 (tramp-password-end-of-line nil
))
287 ("scp" (tramp-login-program "ssh")
288 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
290 (tramp-remote-sh "/bin/sh")
291 (tramp-copy-program "scp")
292 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q")))
293 (tramp-copy-keep-date t
)
294 (tramp-password-end-of-line nil
)
295 (tramp-gw-args (("-o"
296 "GlobalKnownHostsFile=/dev/null")
297 ("-o" "UserKnownHostsFile=/dev/null")
298 ("-o" "StrictHostKeyChecking=no")))
299 (tramp-default-port 22))
300 ("scp1" (tramp-login-program "ssh")
301 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
303 (tramp-remote-sh "/bin/sh")
304 (tramp-copy-program "scp")
305 (tramp-copy-args (("-1") ("-P" "%p") ("-p" "%k")
307 (tramp-copy-keep-date t
)
308 (tramp-password-end-of-line nil
)
309 (tramp-gw-args (("-o"
310 "GlobalKnownHostsFile=/dev/null")
311 ("-o" "UserKnownHostsFile=/dev/null")
312 ("-o" "StrictHostKeyChecking=no")))
313 (tramp-default-port 22))
314 ("scp2" (tramp-login-program "ssh")
315 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
317 (tramp-remote-sh "/bin/sh")
318 (tramp-copy-program "scp")
319 (tramp-copy-args (("-2") ("-P" "%p") ("-p" "%k")
321 (tramp-copy-keep-date t
)
322 (tramp-password-end-of-line nil
)
323 (tramp-gw-args (("-o"
324 "GlobalKnownHostsFile=/dev/null")
325 ("-o" "UserKnownHostsFile=/dev/null")
326 ("-o" "StrictHostKeyChecking=no")))
327 (tramp-default-port 22))
329 (tramp-login-program "ssh1")
330 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
332 (tramp-remote-sh "/bin/sh")
333 (tramp-copy-program "scp1")
334 (tramp-copy-args (("-p" "%k")))
335 (tramp-copy-keep-date t
)
336 (tramp-password-end-of-line nil
))
338 (tramp-login-program "ssh2")
339 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
341 (tramp-remote-sh "/bin/sh")
342 (tramp-copy-program "scp2")
343 (tramp-copy-args (("-p" "%k")))
344 (tramp-copy-keep-date t
)
345 (tramp-password-end-of-line nil
))
346 ("sftp" (tramp-login-program "ssh")
347 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
349 (tramp-remote-sh "/bin/sh")
350 (tramp-copy-program "sftp")
351 (tramp-copy-args nil
)
352 (tramp-copy-keep-date nil
)
353 (tramp-password-end-of-line nil
))
354 ("rsync" (tramp-login-program "ssh")
355 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
357 (tramp-remote-sh "/bin/sh")
358 (tramp-copy-program "rsync")
359 (tramp-copy-args (("-e" "ssh") ("-t" "%k")))
360 (tramp-copy-keep-date t
)
361 (tramp-password-end-of-line nil
))
362 ("remcp" (tramp-login-program "remsh")
363 (tramp-login-args (("%h") ("-l" "%u")))
364 (tramp-remote-sh "/bin/sh")
365 (tramp-copy-program "rcp")
366 (tramp-copy-args (("-p" "%k")))
367 (tramp-copy-keep-date t
)
368 (tramp-password-end-of-line nil
))
369 ("rsh" (tramp-login-program "rsh")
370 (tramp-login-args (("%h") ("-l" "%u")))
371 (tramp-remote-sh "/bin/sh")
372 (tramp-copy-program nil
)
373 (tramp-copy-args nil
)
374 (tramp-copy-keep-date nil
)
375 (tramp-password-end-of-line nil
))
376 ("ssh" (tramp-login-program "ssh")
377 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
379 (tramp-remote-sh "/bin/sh")
380 (tramp-copy-program nil
)
381 (tramp-copy-args nil
)
382 (tramp-copy-keep-date nil
)
383 (tramp-password-end-of-line nil
)
384 (tramp-gw-args (("-o"
385 "GlobalKnownHostsFile=/dev/null")
386 ("-o" "UserKnownHostsFile=/dev/null")
387 ("-o" "StrictHostKeyChecking=no")))
388 (tramp-default-port 22))
389 ("ssh1" (tramp-login-program "ssh")
390 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
392 (tramp-remote-sh "/bin/sh")
393 (tramp-copy-program nil
)
394 (tramp-copy-args nil
)
395 (tramp-copy-keep-date nil
)
396 (tramp-password-end-of-line nil
)
397 (tramp-gw-args (("-o"
398 "GlobalKnownHostsFile=/dev/null")
399 ("-o" "UserKnownHostsFile=/dev/null")
400 ("-o" "StrictHostKeyChecking=no")))
401 (tramp-default-port 22))
402 ("ssh2" (tramp-login-program "ssh")
403 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
405 (tramp-remote-sh "/bin/sh")
406 (tramp-copy-program nil
)
407 (tramp-copy-args nil
)
408 (tramp-copy-keep-date nil
)
409 (tramp-password-end-of-line nil
)
410 (tramp-gw-args (("-o"
411 "GlobalKnownHostsFile=/dev/null")
412 ("-o" "UserKnownHostsFile=/dev/null")
413 ("-o" "StrictHostKeyChecking=no")))
414 (tramp-default-port 22))
416 (tramp-login-program "ssh1")
417 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
419 (tramp-remote-sh "/bin/sh")
420 (tramp-copy-program nil
)
421 (tramp-copy-args nil
)
422 (tramp-copy-keep-date nil
)
423 (tramp-password-end-of-line nil
))
425 (tramp-login-program "ssh2")
426 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
428 (tramp-remote-sh "/bin/sh")
429 (tramp-copy-program nil
)
430 (tramp-copy-args nil
)
431 (tramp-copy-keep-date nil
)
432 (tramp-password-end-of-line nil
))
433 ("remsh" (tramp-login-program "remsh")
434 (tramp-login-args (("%h") ("-l" "%u")))
435 (tramp-remote-sh "/bin/sh")
436 (tramp-copy-program nil
)
437 (tramp-copy-args nil
)
438 (tramp-copy-keep-date nil
)
439 (tramp-password-end-of-line nil
))
441 (tramp-login-program "telnet")
442 (tramp-login-args (("%h") ("%p")))
443 (tramp-remote-sh "/bin/sh")
444 (tramp-copy-program nil
)
445 (tramp-copy-args nil
)
446 (tramp-copy-keep-date nil
)
447 (tramp-password-end-of-line nil
)
448 (tramp-default-port 23))
449 ("su" (tramp-login-program "su")
450 (tramp-login-args (("-") ("%u")))
451 (tramp-remote-sh "/bin/sh")
452 (tramp-copy-program nil
)
453 (tramp-copy-args nil
)
454 (tramp-copy-keep-date nil
)
455 (tramp-password-end-of-line nil
))
456 ("sudo" (tramp-login-program "sudo")
457 (tramp-login-args (("-u" "%u")
458 ("-s") ("-H") ("-p" "Password:")))
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 ("scpc" (tramp-login-program "ssh")
465 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
466 ("-o" "ControlPath=%t.%%r@%%h:%%p")
467 ("-o" "ControlMaster=yes")
469 (tramp-remote-sh "/bin/sh")
470 (tramp-copy-program "scp")
471 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q")
472 ("-o" "ControlPath=%t.%%r@%%h:%%p")
473 ("-o" "ControlMaster=auto")))
474 (tramp-copy-keep-date t
)
475 (tramp-password-end-of-line nil
)
476 (tramp-gw-args (("-o"
477 "GlobalKnownHostsFile=/dev/null")
478 ("-o" "UserKnownHostsFile=/dev/null")
479 ("-o" "StrictHostKeyChecking=no")))
480 (tramp-default-port 22))
481 ("scpx" (tramp-login-program "ssh")
482 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
483 ("-e" "none" "-t" "-t" "/bin/sh")))
484 (tramp-remote-sh "/bin/sh")
485 (tramp-copy-program "scp")
486 (tramp-copy-args (("-p" "%k")))
487 (tramp-copy-keep-date t
)
488 (tramp-password-end-of-line nil
)
489 (tramp-gw-args (("-o"
490 "GlobalKnownHostsFile=/dev/null")
491 ("-o" "UserKnownHostsFile=/dev/null")
492 ("-o" "StrictHostKeyChecking=no")))
493 (tramp-default-port 22))
494 ("sshx" (tramp-login-program "ssh")
495 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
496 ("-e" "none" "-t" "-t" "/bin/sh")))
497 (tramp-remote-sh "/bin/sh")
498 (tramp-copy-program nil
)
499 (tramp-copy-args nil
)
500 (tramp-copy-keep-date nil
)
501 (tramp-password-end-of-line nil
)
502 (tramp-gw-args (("-o"
503 "GlobalKnownHostsFile=/dev/null")
504 ("-o" "UserKnownHostsFile=/dev/null")
505 ("-o" "StrictHostKeyChecking=no")))
506 (tramp-default-port 22))
508 (tramp-login-program "krlogin")
509 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
510 (tramp-remote-sh "/bin/sh")
511 (tramp-copy-program nil
)
512 (tramp-copy-args nil
)
513 (tramp-copy-keep-date nil
)
514 (tramp-password-end-of-line nil
))
515 ("plink" (tramp-login-program "plink")
516 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
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 "xy") ;see docstring for "xy"
523 (tramp-default-port 22))
525 (tramp-login-program "plink")
526 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
528 (tramp-remote-sh "/bin/sh")
529 (tramp-copy-program nil
)
530 (tramp-copy-args nil
)
531 (tramp-copy-keep-date nil
)
532 (tramp-password-end-of-line "xy") ;see docstring for "xy"
533 (tramp-default-port 22))
535 (tramp-login-program "plink")
536 ;; ("%h") must be a single element, see
537 ;; `tramp-compute-multi-hops'.
538 (tramp-login-args (("-load") ("%h") ("-t")
540 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=$ '"
541 tramp-terminal-type
))
543 (tramp-remote-sh "/bin/sh")
544 (tramp-copy-program nil
)
545 (tramp-copy-args nil
)
546 (tramp-copy-keep-date nil
)
547 (tramp-password-end-of-line nil
))
548 ("pscp" (tramp-login-program "plink")
549 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
551 (tramp-remote-sh "/bin/sh")
552 (tramp-copy-program "pscp")
553 (tramp-copy-args (("-P" "%p") ("-scp") ("-p" "%k")))
554 (tramp-copy-keep-date t
)
555 (tramp-password-end-of-line "xy") ;see docstring for "xy"
556 (tramp-default-port 22))
557 ("psftp" (tramp-login-program "plink")
558 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
560 (tramp-remote-sh "/bin/sh")
561 (tramp-copy-program "pscp")
562 (tramp-copy-args (("-P" "%p") ("-sftp") ("-p" "%k")))
563 (tramp-copy-keep-date t
)
564 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
565 ("fcp" (tramp-login-program "fsh")
566 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
567 (tramp-remote-sh "/bin/sh -i")
568 (tramp-copy-program "fcp")
569 (tramp-copy-args (("-p" "%k")))
570 (tramp-copy-keep-date t
)
571 (tramp-password-end-of-line nil
)))
572 "*Alist of methods for remote files.
573 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
574 Each NAME stands for a remote access method. Each PARAM is a
575 pair of the form (KEY VALUE). The following KEYs are defined:
577 This specifies the Bourne shell to use on the remote host. This
578 MUST be a Bourne-like shell. It is normally not necessary to set
579 this to any value other than \"/bin/sh\": Tramp wants to use a shell
580 which groks tilde expansion, but it can search for it. Also note
581 that \"/bin/sh\" exists on all Unixen, this might not be true for
582 the value that you decide to use. You Have Been Warned.
583 * `tramp-login-program'
584 This specifies the name of the program to use for logging in to the
585 remote host. This may be the name of rsh or a workalike program,
586 or the name of telnet or a workalike, or the name of su or a workalike.
588 This specifies the list of arguments to pass to the above
589 mentioned program. Please note that this is a list of list of arguments,
590 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
591 here. Instead, you want a list (\"-a\" \"-b\"), or (\"-f\" \"foo\").
592 There are some patterns: \"%h\" in this list is replaced by the host
593 name, \"%u\" is replaced by the user name, \"%p\" is replaced by the
594 port number, and \"%%\" can be used to obtain a literal percent character.
595 If a list containing \"%h\", \"%u\" or \"%p\" is unchanged during
596 expansion (i.e. no host or no user specified), this list is not used as
597 argument. By this, arguments like (\"-l\" \"%u\") are optional.
598 \"%t\" is replaced by the temporary file name produced with
599 `tramp-make-tramp-temp-file'. \"%k\" indicates the keep-date
600 parameter of a program, if exists.
601 * `tramp-copy-program'
602 This specifies the name of the program to use for remotely copying
603 the file; this might be the absolute filename of rcp or the name of
606 This specifies the list of parameters to pass to the above mentioned
607 program, the hints for `tramp-login-args' also apply here.
608 * `tramp-copy-keep-date'
609 This specifies whether the copying program when the preserves the
610 timestamp of the original file.
611 * `tramp-default-port'
612 The default port of a method is needed in case of gateway connections.
613 Additionally, it is used as indication which method is prepared for
616 As the attribute name says, additional arguments are specified here
617 when a method is applied via a gateway.
618 * `tramp-password-end-of-line'
619 This specifies the string to use for terminating the line after
620 submitting the password. If this method parameter is nil, then the
621 value of the normal variable `tramp-default-password-end-of-line'
622 is used. This parameter is necessary because the \"plink\" program
623 requires any two characters after sending the password. These do
624 not have to be newline or carriage return characters. Other login
625 programs are happy with just one character, the newline character.
626 We use \"xy\" as the value for methods using \"plink\".
628 What does all this mean? Well, you should specify `tramp-login-program'
629 for all methods; this program is used to log in to the remote site. Then,
630 there are two ways to actually transfer the files between the local and the
631 remote side. One way is using an additional rcp-like program. If you want
632 to do this, set `tramp-copy-program' in the method.
634 Another possibility for file transfer is inline transfer, i.e. the
635 file is passed through the same buffer used by `tramp-login-program'. In
636 this case, the file contents need to be protected since the
637 `tramp-login-program' might use escape codes or the connection might not
638 be eight-bit clean. Therefore, file contents are encoded for transit.
639 See the variables `tramp-local-coding-commands' and
640 `tramp-remote-coding-commands' for details.
642 So, to summarize: if the method is an out-of-band method, then you
643 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
644 inline method, then these two parameters should be nil. Methods which
645 are fit for gateways must have `tramp-default-port' at least.
649 When using `su' or `sudo' the phrase `open connection to a remote
650 host' sounds strange, but it is used nevertheless, for consistency.
651 No connection is opened to a remote host, but `su' or `sudo' is
652 started on the local host. You should specify a remote host
653 `localhost' or the name of the local host. Another host name is
654 useful only in combination with `tramp-default-proxies-alist'.")
656 (defcustom tramp-default-method
657 ;; An external copy method seems to be preferred, because it is much
658 ;; more performant for large files, and it hasn't too serious delays
659 ;; for small files. But it must be ensured that there aren't
660 ;; permanent password queries. Either a password agent like
661 ;; "ssh-agent" or "Pageant" shall run, or the optional password.el
662 ;; package shall be active for password caching. "scpc" would be
663 ;; another good choice because of the "ControlMaster" option, but
664 ;; this is a more modern alternative in OpenSSH 4, which cannot be
667 ;; PuTTY is installed.
668 ((executable-find "pscp")
669 (if (or (fboundp 'password-read
)
670 ;; Pageant is running.
671 (and (fboundp 'w32-window-exists-p
)
672 (funcall (symbol-function 'w32-window-exists-p
)
673 "Pageant" "Pageant")))
676 ;; There is an ssh installation.
677 ((executable-find "scp")
678 (if (or (fboundp 'password-read
)
679 ;; ssh-agent is running.
680 (getenv "SSH_AUTH_SOCK")
681 (getenv "SSH_AGENT_PID"))
686 "*Default method to use for transferring files.
687 See `tramp-methods' for possibilities.
688 Also see `tramp-default-method-alist'."
692 (defcustom tramp-default-method-alist
693 '(("\\`localhost\\'" "\\`root\\'" "su"))
694 "*Default method to use for specific host/user pairs.
695 This is an alist of items (HOST USER METHOD). The first matching item
696 specifies the method to use for a file name which does not specify a
697 method. HOST and USER are regular expressions or nil, which is
698 interpreted as a regular expression which always matches. If no entry
699 matches, the variable `tramp-default-method' takes effect.
701 If the file name does not specify the user, lookup is done using the
702 empty string for the user name.
704 See `tramp-methods' for a list of possibilities for METHOD."
706 :type
'(repeat (list (regexp :tag
"Host regexp")
707 (regexp :tag
"User regexp")
708 (string :tag
"Method"))))
710 (defcustom tramp-default-user
712 "*Default user to use for transferring files.
713 It is nil by default; otherwise settings in configuration files like
714 \"~/.ssh/config\" would be overwritten. Also see `tramp-default-user-alist'.
716 This variable is regarded as obsolete, and will be removed soon."
718 :type
'(choice (const nil
) string
))
720 (defcustom tramp-default-user-alist
721 `(("\\`su\\(do\\)?\\'" nil
"root")
722 ("\\`r\\(em\\)?\\(cp\\|sh\\)\\|telnet\\|plink1?\\'"
723 nil
,(user-login-name)))
724 "*Default user to use for specific method/host pairs.
725 This is an alist of items (METHOD HOST USER). The first matching item
726 specifies the user to use for a file name which does not specify a
727 user. METHOD and USER are regular expressions or nil, which is
728 interpreted as a regular expression which always matches. If no entry
729 matches, the variable `tramp-default-user' takes effect.
731 If the file name does not specify the method, lookup is done using the
732 empty string for the method name."
734 :type
'(repeat (list (regexp :tag
"Method regexp")
735 (regexp :tag
"Host regexp")
736 (string :tag
"User"))))
738 (defcustom tramp-default-host
740 "*Default host to use for transferring files.
741 Useful for su and sudo methods mostly."
745 (defcustom tramp-default-proxies-alist nil
746 "*Route to be followed for specific host/user pairs.
747 This is an alist of items (HOST USER PROXY). The first matching
748 item specifies the proxy to be passed for a file name located on
749 a remote target matching USER@HOST. HOST and USER are regular
750 expressions or nil, which is interpreted as a regular expression
751 which always matches. PROXY must be a Tramp filename without a
752 localname part. Method and user name on PROXY are optional,
753 which is interpreted with the default values. PROXY can contain
754 the patterns %h and %u, which are replaced by the strings
755 matching HOST or USER, respectively."
757 :type
'(repeat (list (regexp :tag
"Host regexp")
758 (regexp :tag
"User regexp")
759 (string :tag
"Proxy remote name"))))
761 (defconst tramp-local-host-regexp
763 "^" (regexp-opt (list "localhost" (system-name) "127\.0\.0\.1" "::1") t
) "$")
764 "*Host names which are regarded as local host.")
766 (defconst tramp-completion-function-alist-rsh
767 '((tramp-parse-rhosts "/etc/hosts.equiv")
768 (tramp-parse-rhosts "~/.rhosts"))
769 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
771 (defconst tramp-completion-function-alist-ssh
772 '((tramp-parse-rhosts "/etc/hosts.equiv")
773 (tramp-parse-rhosts "/etc/shosts.equiv")
774 (tramp-parse-shosts "/etc/ssh_known_hosts")
775 (tramp-parse-sconfig "/etc/ssh_config")
776 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
777 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
778 (tramp-parse-rhosts "~/.rhosts")
779 (tramp-parse-rhosts "~/.shosts")
780 (tramp-parse-shosts "~/.ssh/known_hosts")
781 (tramp-parse-sconfig "~/.ssh/config")
782 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
783 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
784 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
786 (defconst tramp-completion-function-alist-telnet
787 '((tramp-parse-hosts "/etc/hosts"))
788 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
790 (defconst tramp-completion-function-alist-su
791 '((tramp-parse-passwd "/etc/passwd"))
792 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
794 (defconst tramp-completion-function-alist-putty
796 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"))
797 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty methods.")
799 (defvar tramp-completion-function-alist nil
800 "*Alist of methods for remote files.
801 This is a list of entries of the form (NAME PAIR1 PAIR2 ...).
802 Each NAME stands for a remote access method. Each PAIR is of the form
803 \(FUNCTION FILE). FUNCTION is responsible to extract user names and host
804 names from FILE for completion. The following predefined FUNCTIONs exists:
806 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
807 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
808 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
809 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
810 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
811 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
812 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
813 * `tramp-parse-netrc' for \"~/.netrc\" like files.
814 * `tramp-parse-putty' for PuTTY registry keys.
816 FUNCTION can also be a customer defined function. For more details see
819 (eval-after-load "tramp"
821 (tramp-set-completion-function
822 "rcp" tramp-completion-function-alist-rsh
)
823 (tramp-set-completion-function
824 "scp" tramp-completion-function-alist-ssh
)
825 (tramp-set-completion-function
826 "scp1" tramp-completion-function-alist-ssh
)
827 (tramp-set-completion-function
828 "scp2" tramp-completion-function-alist-ssh
)
829 (tramp-set-completion-function
830 "scp1_old" tramp-completion-function-alist-ssh
)
831 (tramp-set-completion-function
832 "scp2_old" tramp-completion-function-alist-ssh
)
833 (tramp-set-completion-function
834 "rsync" tramp-completion-function-alist-rsh
)
835 (tramp-set-completion-function
836 "remcp" tramp-completion-function-alist-rsh
)
837 (tramp-set-completion-function
838 "rsh" tramp-completion-function-alist-rsh
)
839 (tramp-set-completion-function
840 "ssh" tramp-completion-function-alist-ssh
)
841 (tramp-set-completion-function
842 "ssh1" tramp-completion-function-alist-ssh
)
843 (tramp-set-completion-function
844 "ssh2" tramp-completion-function-alist-ssh
)
845 (tramp-set-completion-function
846 "ssh1_old" tramp-completion-function-alist-ssh
)
847 (tramp-set-completion-function
848 "ssh2_old" tramp-completion-function-alist-ssh
)
849 (tramp-set-completion-function
850 "remsh" tramp-completion-function-alist-rsh
)
851 (tramp-set-completion-function
852 "telnet" tramp-completion-function-alist-telnet
)
853 (tramp-set-completion-function
854 "su" tramp-completion-function-alist-su
)
855 (tramp-set-completion-function
856 "sudo" tramp-completion-function-alist-su
)
857 (tramp-set-completion-function
858 "scpx" tramp-completion-function-alist-ssh
)
859 (tramp-set-completion-function
860 "sshx" tramp-completion-function-alist-ssh
)
861 (tramp-set-completion-function
862 "krlogin" tramp-completion-function-alist-rsh
)
863 (tramp-set-completion-function
864 "plink" tramp-completion-function-alist-ssh
)
865 (tramp-set-completion-function
866 "plink1" tramp-completion-function-alist-ssh
)
867 (tramp-set-completion-function
868 "plinkx" tramp-completion-function-alist-putty
)
869 (tramp-set-completion-function
870 "pscp" tramp-completion-function-alist-ssh
)
871 (tramp-set-completion-function
872 "fcp" tramp-completion-function-alist-ssh
)))
874 (defconst tramp-echo-mark-marker
"_echo"
875 "String marker to surround echoed commands.")
877 (defconst tramp-echo-mark
"_echo\b\b\b\b\b"
878 "String mark to be transmitted around shell commands.
879 Used to separate their echo from the output they produce. This
880 will only be used if we cannot disable remote echo via stty.
881 This string must have no effect on the remote shell except for
882 producing some echo which can later be detected by
883 `tramp-echoed-echo-mark-regexp'. Using `tramp-echo-mark-marker',
884 followed by an equal number of backspaces to erase them will
887 (defconst tramp-echoed-echo-mark-regexp
"_echo\\(\b\\( \b\\)?\\)\\{5\\}"
888 "Regexp which matches `tramp-echo-mark' as it gets echoed by
891 (defcustom tramp-rsh-end-of-line
"\n"
892 "*String used for end of line in rsh connections.
893 I don't think this ever needs to be changed, so please tell me about it
894 if you need to change this.
895 Also see the method parameter `tramp-password-end-of-line' and the normal
896 variable `tramp-default-password-end-of-line'."
900 (defcustom tramp-default-password-end-of-line
901 tramp-rsh-end-of-line
902 "*String used for end of line after sending a password.
903 This variable provides the default value for the method parameter
904 `tramp-password-end-of-line', see `tramp-methods' for more details.
906 It seems that people using plink under Windows need to send
907 \"\\r\\n\" (carriage-return, then newline) after a password, but just
908 \"\\n\" after all other lines. This variable can be used for the
909 password, see `tramp-rsh-end-of-line' for the other cases.
911 The default value is to use the same value as `tramp-rsh-end-of-line'."
915 ;; "getconf PATH" yields:
916 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
917 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
918 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
919 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
920 (defcustom tramp-remote-path
921 '(tramp-default-remote-path "/usr/sbin" "/usr/local/bin"
922 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
923 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
924 "*List of directories to search for executables on remote host.
925 For every remote host, this variable will be set buffer local,
926 keeping the list of existing directories on that host.
928 You can use `~' in this list, but when searching for a shell which groks
929 tilde expansion, all directory names starting with `~' will be ignored.
931 `Default Directories' represent the list of directories given by
932 the command \"getconf PATH\". It is recommended to use this
933 entry on top of this list, because these are the default
934 directories for POSIX compatible commands."
936 :type
'(repeat (choice
937 (const :tag
"Default Directories" tramp-default-remote-path
)
938 (string :tag
"Directory"))))
940 (defcustom tramp-remote-process-environment
941 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "LC_ALL=C"
942 ,(concat "TERM=" tramp-terminal-type
)
943 "EMACS=t" ;; Deprecated.
944 ,(format "INSIDE_EMACS=%s,tramp:%s" emacs-version tramp-version
)
945 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH="
946 "autocorrect=" "correct=")
948 "*List of environment variables to be set on the remote host.
950 Each element should be a string of the form ENVVARNAME=VALUE. An
951 entry ENVVARNAME= diables the corresponding environment variable,
952 which might have been set in the init files like ~/.profile.
954 Special handling is applied to the PATH environment, which should
955 not be set here. Instead of, it should be set via `tramp-remote-path'."
957 :type
'(repeat string
))
959 (defcustom tramp-login-prompt-regexp
960 ".*ogin\\( .*\\)?: *"
961 "*Regexp matching login-like prompts.
962 The regexp should match at end of buffer.
964 Sometimes the prompt is reported to look like \"login as:\"."
968 (defcustom tramp-shell-prompt-pattern
969 "^[^#$%>\n]*[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*"
970 "Regexp to match prompts from remote shell.
971 Normally, Tramp expects you to configure `shell-prompt-pattern'
972 correctly, but sometimes it happens that you are connecting to a
973 remote host which sends a different kind of shell prompt. Therefore,
974 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
975 and also things matched by this variable. The default value of this
976 variable is similar to the default value of `shell-prompt-pattern',
977 which should work well in many cases."
981 (defcustom tramp-password-prompt-regexp
982 "^.*\\([pP]assword\\|[pP]assphrase\\).*:\^@? *"
983 "*Regexp matching password-like prompts.
984 The regexp should match at end of buffer.
986 The `sudo' program appears to insert a `^@' character into the prompt."
990 (defcustom tramp-wrong-passwd-regexp
992 ;; These strings should be on the last line
993 (regexp-opt '("Permission denied"
999 "Name or service not known"
1000 "Host key verification failed."
1001 "No supported authentication methods left to try!"
1002 "Tramp connection closed") t
)
1006 ;; Here comes a list of regexes, separated by \\|
1007 "Received signal [0-9]+"
1009 "*Regexp matching a `login failed' message.
1010 The regexp should match at end of buffer."
1014 (defcustom tramp-yesno-prompt-regexp
1016 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t
)
1018 "Regular expression matching all yes/no queries which need to be confirmed.
1019 The confirmation should be done with yes or no.
1020 The regexp should match at end of buffer.
1021 See also `tramp-yn-prompt-regexp'."
1025 (defcustom tramp-yn-prompt-regexp
1027 (regexp-opt '("Store key in cache? (y/n)"
1028 "Update cached key? (y/n, Return cancels connection)") t
)
1030 "Regular expression matching all y/n queries which need to be confirmed.
1031 The confirmation should be done with y or n.
1032 The regexp should match at end of buffer.
1033 See also `tramp-yesno-prompt-regexp'."
1037 (defcustom tramp-terminal-prompt-regexp
1041 "Terminal type\\? \\[.*\\]"
1043 "Regular expression matching all terminal setting prompts.
1044 The regexp should match at end of buffer.
1045 The answer will be provided by `tramp-action-terminal', which see."
1049 (defcustom tramp-operation-not-permitted-regexp
1050 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
1051 (regexp-opt '("Operation not permitted") t
))
1052 "Regular expression matching keep-date problems in (s)cp operations.
1053 Copying has been performed successfully already, so this message can
1058 (defcustom tramp-copy-failed-regexp
1060 (regexp-opt '("Permission denied"
1061 "not a regular file"
1063 "No such file or directory") t
)
1065 "Regular expression matching copy problems in (s)cp operations."
1069 (defcustom tramp-process-alive-regexp
1071 "Regular expression indicating a process has finished.
1072 In fact this expression is empty by intention, it will be used only to
1073 check regularly the status of the associated process.
1074 The answer will be provided by `tramp-action-process-alive',
1075 `tramp-action-out-of-band', which see."
1079 (defcustom tramp-temp-name-prefix
"tramp."
1080 "*Prefix to use for temporary files.
1081 If this is a relative file name (such as \"tramp.\"), it is considered
1082 relative to the directory name returned by the function
1083 `tramp-compat-temporary-file-directory' (which see). It may also be an
1084 absolute file name; don't forget to include a prefix for the filename
1089 (defconst tramp-temp-buffer-name
" *tramp temp*"
1090 "Buffer name for a temporary buffer.
1091 It shall be used in combination with `generate-new-buffer-name'.")
1093 (defcustom tramp-sh-extra-args
'(("/bash\\'" .
"-norc -noprofile"))
1094 "*Alist specifying extra arguments to pass to the remote shell.
1095 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
1096 matching the shell file name and ARGS is a string specifying the
1099 This variable is only used when Tramp needs to start up another shell
1100 for tilde expansion. The extra arguments should typically prevent the
1101 shell from reading its init file."
1103 ;; This might be the wrong way to test whether the widget type
1104 ;; `alist' is available. Who knows the right way to test it?
1105 :type
(if (get 'alist
'widget-type
)
1106 '(alist :key-type string
:value-type string
)
1107 '(repeat (cons string string
))))
1109 ;; XEmacs is distributed with few Lisp packages. Further packages are
1110 ;; installed using EFS. If we use a unified filename format, then
1111 ;; Tramp is required in addition to EFS. (But why can't Tramp just
1112 ;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
1113 ;; just like before.) Another reason for using a separate filename
1114 ;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
1115 ;; Tramp only knows how to deal with `file-name-handler-alist', not
1116 ;; the other places.
1118 ;; Currently, we have the choice between 'ftp, 'sep, and 'url.
1120 (defcustom tramp-syntax
1121 (if (featurep 'xemacs
) 'sep
'ftp
)
1122 "Tramp filename syntax to be used.
1124 It can have the following values:
1126 'ftp -- Ange-FTP respective EFS like syntax (GNU Emacs default)
1127 'sep -- Syntax as defined for XEmacs (not available yet for GNU Emacs)
1128 'url -- URL-like syntax."
1130 :type
(if (featurep 'xemacs
)
1131 '(choice (const :tag
"EFS" ftp
)
1132 (const :tag
"XEmacs" sep
)
1133 (const :tag
"URL" url
))
1134 '(choice (const :tag
"Ange-FTP" ftp
)
1135 (const :tag
"URL" url
))))
1137 (defconst tramp-prefix-format
1138 (cond ((equal tramp-syntax
'ftp
) "/")
1139 ((equal tramp-syntax
'sep
) "/[")
1140 ((equal tramp-syntax
'url
) "/")
1141 (t (error "Wrong `tramp-syntax' defined")))
1142 "*String matching the very beginning of Tramp file names.
1143 Used in `tramp-make-tramp-file-name'.")
1145 (defconst tramp-prefix-regexp
1146 (concat "^" (regexp-quote tramp-prefix-format
))
1147 "*Regexp matching the very beginning of Tramp file names.
1148 Should always start with \"^\". Derived from `tramp-prefix-format'.")
1150 (defconst tramp-method-regexp
1152 "*Regexp matching methods identifiers.")
1154 (defconst tramp-postfix-method-format
1155 (cond ((equal tramp-syntax
'ftp
) ":")
1156 ((equal tramp-syntax
'sep
) "/")
1157 ((equal tramp-syntax
'url
) "://")
1158 (t (error "Wrong `tramp-syntax' defined")))
1159 "*String matching delimeter between method and user or host names.
1160 Used in `tramp-make-tramp-file-name'.")
1162 (defconst tramp-postfix-method-regexp
1163 (regexp-quote tramp-postfix-method-format
)
1164 "*Regexp matching delimeter between method and user or host names.
1165 Derived from `tramp-postfix-method-format'.")
1167 (defconst tramp-user-regexp
1169 "*Regexp matching user names.")
1171 (defconst tramp-prefix-domain-format
"%"
1172 "*String matching delimeter between user and domain names.")
1174 (defconst tramp-prefix-domain-regexp
1175 (regexp-quote tramp-prefix-domain-format
)
1176 "*Regexp matching delimeter between user and domain names.
1177 Derived from `tramp-prefix-domain-format'.")
1179 (defconst tramp-domain-regexp
1181 "*Regexp matching domain names.")
1183 (defconst tramp-user-with-domain-regexp
1184 (concat "\\(" tramp-user-regexp
"\\)"
1185 tramp-prefix-domain-regexp
1186 "\\(" tramp-domain-regexp
"\\)")
1187 "*Regexp matching user names with domain names.")
1189 (defconst tramp-postfix-user-format
1191 "*String matching delimeter between user and host names.
1192 Used in `tramp-make-tramp-file-name'.")
1194 (defconst tramp-postfix-user-regexp
1195 (regexp-quote tramp-postfix-user-format
)
1196 "*Regexp matching delimeter between user and host names.
1197 Derived from `tramp-postfix-user-format'.")
1199 (defconst tramp-host-regexp
1201 "*Regexp matching host names.")
1203 (defconst tramp-prefix-ipv6-format
1204 (cond ((equal tramp-syntax
'ftp
) "[")
1205 ((equal tramp-syntax
'sep
) "")
1206 ((equal tramp-syntax
'url
) "[")
1207 (t (error "Wrong `tramp-syntax' defined")))
1208 "*String matching left hand side of IPv6 addresses.
1209 Used in `tramp-make-tramp-file-name'.")
1211 (defconst tramp-prefix-ipv6-regexp
1212 (regexp-quote tramp-prefix-ipv6-format
)
1213 "*Regexp matching left hand side of IPv6 addresses.
1214 Derived from `tramp-prefix-ipv6-format'.")
1216 ;; The following regexp is a bit sloppy. But it shall serve our
1217 ;; purposes. It covers also IPv4 mapped IPv6 addresses, like in
1218 ;; "::ffff:192.168.0.1".
1219 (defconst tramp-ipv6-regexp
1220 "\\(?:\\(?:[a-zA-Z0-9]+\\)?:\\)+[a-zA-Z0-9.]+"
1221 "*Regexp matching IPv6 addresses.")
1223 (defconst tramp-postfix-ipv6-format
1224 (cond ((equal tramp-syntax
'ftp
) "]")
1225 ((equal tramp-syntax
'sep
) "")
1226 ((equal tramp-syntax
'url
) "]")
1227 (t (error "Wrong `tramp-syntax' defined")))
1228 "*String matching right hand side of IPv6 addresses.
1229 Used in `tramp-make-tramp-file-name'.")
1231 (defconst tramp-postfix-ipv6-regexp
1232 (regexp-quote tramp-postfix-ipv6-format
)
1233 "*Regexp matching right hand side of IPv6 addresses.
1234 Derived from `tramp-postfix-ipv6-format'.")
1236 (defconst tramp-prefix-port-format
1237 (cond ((equal tramp-syntax
'ftp
) "#")
1238 ((equal tramp-syntax
'sep
) "#")
1239 ((equal tramp-syntax
'url
) ":")
1240 (t (error "Wrong `tramp-syntax' defined")))
1241 "*String matching delimeter between host names and port numbers.")
1243 (defconst tramp-prefix-port-regexp
1244 (regexp-quote tramp-prefix-port-format
)
1245 "*Regexp matching delimeter between host names and port numbers.
1246 Derived from `tramp-prefix-port-format'.")
1248 (defconst tramp-port-regexp
1250 "*Regexp matching port numbers.")
1252 (defconst tramp-host-with-port-regexp
1253 (concat "\\(" tramp-host-regexp
"\\)"
1254 tramp-prefix-port-regexp
1255 "\\(" tramp-port-regexp
"\\)")
1256 "*Regexp matching host names with port numbers.")
1258 (defconst tramp-postfix-host-format
1259 (cond ((equal tramp-syntax
'ftp
) ":")
1260 ((equal tramp-syntax
'sep
) "]")
1261 ((equal tramp-syntax
'url
) "")
1262 (t (error "Wrong `tramp-syntax' defined")))
1263 "*String matching delimeter between host names and localnames.
1264 Used in `tramp-make-tramp-file-name'.")
1266 (defconst tramp-postfix-host-regexp
1267 (regexp-quote tramp-postfix-host-format
)
1268 "*Regexp matching delimeter between host names and localnames.
1269 Derived from `tramp-postfix-host-format'.")
1271 (defconst tramp-localname-regexp
1273 "*Regexp matching localnames.")
1275 ;; File name format.
1277 (defconst tramp-file-name-structure
1281 "\\(" "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
"\\)?"
1282 "\\(" "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
"\\)?"
1283 "\\(" "\\(" tramp-host-regexp
1285 tramp-prefix-ipv6-regexp tramp-ipv6-regexp
1286 tramp-postfix-ipv6-regexp
"\\)"
1287 "\\(" tramp-prefix-port-regexp tramp-port-regexp
"\\)?" "\\)?"
1288 tramp-postfix-host-regexp
1289 "\\(" tramp-localname-regexp
"\\)")
1292 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1293 the Tramp file name structure.
1295 The first element REGEXP is a regular expression matching a Tramp file
1296 name. The regex should contain parentheses around the method name,
1297 the user name, the host name, and the file name parts.
1299 The second element METHOD is a number, saying which pair of
1300 parentheses matches the method name. The third element USER is
1301 similar, but for the user name. The fourth element HOST is similar,
1302 but for the host name. The fifth element FILE is for the file name.
1303 These numbers are passed directly to `match-string', which see. That
1304 means the opening parentheses are counted to identify the pair.
1306 See also `tramp-file-name-regexp'.")
1309 (defconst tramp-file-name-regexp-unified
1310 "\\`/\\([^[/:]+\\|[^/]+]\\):"
1311 "Value for `tramp-file-name-regexp' for unified remoting.
1312 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1313 Tramp. See `tramp-file-name-structure' for more explanations.")
1316 (defconst tramp-file-name-regexp-separate
1318 "Value for `tramp-file-name-regexp' for separate remoting.
1319 XEmacs uses a separate filename syntax for Tramp and EFS.
1320 See `tramp-file-name-structure' for more explanations.")
1323 (defconst tramp-file-name-regexp-url
1325 "Value for `tramp-file-name-regexp' for URL-like remoting.
1326 See `tramp-file-name-structure' for more explanations.")
1329 (defconst tramp-file-name-regexp
1330 (cond ((equal tramp-syntax
'ftp
) tramp-file-name-regexp-unified
)
1331 ((equal tramp-syntax
'sep
) tramp-file-name-regexp-separate
)
1332 ((equal tramp-syntax
'url
) tramp-file-name-regexp-url
)
1333 (t (error "Wrong `tramp-syntax' defined")))
1334 "*Regular expression matching file names handled by Tramp.
1335 This regexp should match Tramp file names but no other file names.
1336 \(When tramp.el is loaded, this regular expression is prepended to
1337 `file-name-handler-alist', and that is searched sequentially. Thus,
1338 if the Tramp entry appears rather early in the `file-name-handler-alist'
1339 and is a bit too general, then some files might be considered Tramp
1340 files which are not really Tramp files.
1342 Please note that the entry in `file-name-handler-alist' is made when
1343 this file (tramp.el) is loaded. This means that this variable must be set
1344 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1345 updated after changing this variable.
1347 Also see `tramp-file-name-structure'.")
1350 (defconst tramp-root-regexp
1351 (if (memq system-type
'(cygwin windows-nt
))
1352 "^\\([a-zA-Z]:\\)?/"
1354 "Beginning of an incomplete Tramp file name.
1355 Usually, it is just \"^/\". On W32 systems, there might be a
1356 volume letter, which will be removed by `tramp-drop-volume-letter'.")
1359 (defconst tramp-completion-file-name-regexp-unified
1360 (concat tramp-root-regexp
"[^/]*$")
1361 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1362 GNU Emacs uses a unified filename syntax for Tramp and Ange-FTP.
1363 See `tramp-file-name-structure' for more explanations.")
1366 (defconst tramp-completion-file-name-regexp-separate
1367 (concat tramp-root-regexp
"\\([[][^]]*\\)?$")
1368 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1369 XEmacs uses a separate filename syntax for Tramp and EFS.
1370 See `tramp-file-name-structure' for more explanations.")
1373 (defconst tramp-completion-file-name-regexp-url
1374 (concat tramp-root-regexp
"[^/:]+\\(:\\(/\\(/[^/]*\\)?\\)?\\)?$")
1375 "Value for `tramp-completion-file-name-regexp' for URL-like remoting.
1376 See `tramp-file-name-structure' for more explanations.")
1379 (defconst tramp-completion-file-name-regexp
1380 (cond ((equal tramp-syntax
'ftp
) tramp-completion-file-name-regexp-unified
)
1381 ((equal tramp-syntax
'sep
) tramp-completion-file-name-regexp-separate
)
1382 ((equal tramp-syntax
'url
) tramp-completion-file-name-regexp-url
)
1383 (t (error "Wrong `tramp-syntax' defined")))
1384 "*Regular expression matching file names handled by Tramp completion.
1385 This regexp should match partial Tramp file names only.
1387 Please note that the entry in `file-name-handler-alist' is made when
1388 this file (tramp.el) is loaded. This means that this variable must be set
1389 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1390 updated after changing this variable.
1392 Also see `tramp-file-name-structure'.")
1394 (defconst tramp-actions-before-shell
1395 '((tramp-login-prompt-regexp tramp-action-login
)
1396 (tramp-password-prompt-regexp tramp-action-password
)
1397 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
1398 (shell-prompt-pattern tramp-action-succeed
)
1399 (tramp-shell-prompt-pattern tramp-action-succeed
)
1400 (tramp-yesno-prompt-regexp tramp-action-yesno
)
1401 (tramp-yn-prompt-regexp tramp-action-yn
)
1402 (tramp-terminal-prompt-regexp tramp-action-terminal
)
1403 (tramp-process-alive-regexp tramp-action-process-alive
))
1404 "List of pattern/action pairs.
1405 Whenever a pattern matches, the corresponding action is performed.
1406 Each item looks like (PATTERN ACTION).
1408 The PATTERN should be a symbol, a variable. The value of this
1409 variable gives the regular expression to search for. Note that the
1410 regexp must match at the end of the buffer, \"\\'\" is implicitly
1413 The ACTION should also be a symbol, but a function. When the
1414 corresponding PATTERN matches, the ACTION function is called.")
1416 (defconst tramp-actions-copy-out-of-band
1417 '((tramp-password-prompt-regexp tramp-action-password
)
1418 (tramp-wrong-passwd-regexp tramp-action-permission-denied
)
1419 (tramp-copy-failed-regexp tramp-action-permission-denied
)
1420 (tramp-process-alive-regexp tramp-action-out-of-band
))
1421 "List of pattern/action pairs.
1422 This list is used for copying/renaming with out-of-band methods.
1424 See `tramp-actions-before-shell' for more info.")
1426 ;; Chunked sending kludge. We set this to 500 for black-listed constellations
1427 ;; known to have a bug in `process-send-string'; some ssh connections appear
1428 ;; to drop bytes when data is sent too quickly. There is also a connection
1429 ;; buffer local variable, which is computed depending on remote host properties
1430 ;; when `tramp-chunksize' is zero or nil.
1431 (defcustom tramp-chunksize
1432 (when (and (not (featurep 'xemacs
))
1433 (memq system-type
'(hpux)))
1435 ;; Parentheses in docstring starting at beginning of line are escaped.
1436 ;; Fontification is messed up when
1437 ;; `open-paren-in-column-0-is-defun-start' set to t.
1438 "*If non-nil, chunksize for sending input to local process.
1439 It is necessary only on systems which have a buggy `process-send-string'
1440 implementation. The necessity, whether this variable must be set, can be
1441 checked via the following code:
1444 (let* ((user \"xxx\") (host \"yyy\")
1446 (sent init) (received init))
1447 (while (= sent received)
1448 (setq sent (+ sent step))
1450 (let ((proc (start-process (buffer-name) (current-buffer)
1451 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1452 (when (memq (process-status proc) '(run open))
1453 (process-send-string proc (make-string sent ?\\ ))
1454 (process-send-eof proc)
1455 (process-send-eof proc))
1456 (while (not (progn (goto-char (point-min))
1457 (re-search-forward \"\\\\w+\" (point-max) t)))
1458 (accept-process-output proc 1))
1459 (when (memq (process-status proc) '(run open))
1460 (setq received (string-to-number (match-string 0)))
1461 (delete-process proc)
1462 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1464 (if (> sent (+ init step))
1465 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1467 (message \"Test does not work\")
1468 (display-buffer (current-buffer))
1471 In the Emacs normally running Tramp, evaluate the above code
1472 \(replace \"xxx\" and \"yyy\" by the remote user and host name,
1473 respectively). You can do this, for example, by pasting it into
1474 the `*scratch*' buffer and then hitting C-j with the cursor after the
1475 last closing parenthesis. Note that it works only if you have configured
1476 \"ssh\" to run without password query, see ssh-agent(1).
1478 You will see the number of bytes sent successfully to the remote host.
1479 If that number exceeds 1000, you can stop the execution by hitting
1480 C-g, because your Emacs is likely clean.
1482 When it is necessary to set `tramp-chunksize', you might consider to
1483 use an out-of-the-band method (like \"scp\") instead of an internal one
1484 \(like \"ssh\"), because setting `tramp-chunksize' to non-nil decreases
1487 If your Emacs is buggy, the code stops and gives you an indication
1488 about the value `tramp-chunksize' should be set. Maybe you could just
1489 experiment a bit, e.g. changing the values of `init' and `step'
1490 in the third line of the code.
1492 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1493 this variable to be set as well."
1495 :type
'(choice (const nil
) integer
))
1497 ;; Logging in to a remote host normally requires obtaining a pty. But
1498 ;; Emacs on MacOS X has process-connection-type set to nil by default,
1499 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1500 ;; for an override of the system default.
1501 (defcustom tramp-process-connection-type t
1502 "Overrides `process-connection-type' for connections from Tramp.
1503 Tramp binds process-connection-type to the value given here before
1504 opening a connection to a remote host."
1506 :type
'(choice (const nil
) (const t
) (const pty
)))
1508 (defcustom tramp-completion-reread-directory-timeout
10
1509 "Defines seconds since last remote command before rereading a directory.
1510 A remote directory might have changed its contents. In order to
1511 make it visible during file name completion in the minibuffer,
1512 Tramp flushes its cache and rereads the directory contents when
1513 more than `tramp-completion-reread-directory-timeout' seconds
1514 have been gone since last remote command execution. A value of 0
1515 would require an immediate reread during filename completion, nil
1516 means to use always cached values for the directory contents."
1518 :type
'(choice (const nil
) integer
))
1520 ;;; Internal Variables:
1522 (defvar tramp-end-of-output
1525 tramp-rsh-end-of-line
1526 (md5 (concat (prin1-to-string process-environment
) (current-time-string)))
1527 tramp-rsh-end-of-line
)
1528 "String used to recognize end of output.")
1530 (defvar tramp-current-method nil
1531 "Connection method for this *tramp* buffer.")
1533 (defvar tramp-current-user nil
1534 "Remote login name for this *tramp* buffer.")
1536 (defvar tramp-current-host nil
1537 "Remote host for this *tramp* buffer.")
1539 (defconst tramp-uudecode
1540 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1542 rm -f /tmp/tramp.$$"
1543 "Shell function to implement `uudecode' to standard output.
1544 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
1545 for this or `uudecode -p', but some systems don't, and for them
1546 we have this shell function.")
1548 ;; Perl script to implement `file-attributes' in a Lisp `read'able
1549 ;; output. If you are hacking on this, note that you get *no* output
1550 ;; unless this spits out a complete line, including the '\n' at the
1552 ;; The device number is returned as "-1", because there will be a virtual
1553 ;; device number set in `tramp-handle-file-attributes'.
1554 (defconst tramp-perl-file-attributes
1556 @stat = lstat($ARGV[0]);
1557 if (($stat[2] & 0170000) == 0120000)
1559 $type = readlink($ARGV[0]);
1560 $type = \"\\\"$type\\\"\";
1562 elsif (($stat[2] & 0170000) == 040000)
1570 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1571 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1573 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
1578 $stat[8] >> 16 & 0xffff,
1580 $stat[9] >> 16 & 0xffff,
1582 $stat[10] >> 16 & 0xffff,
1586 $stat[1] >> 16 & 0xffff,
1588 );' \"$1\" \"$2\" \"$3\" 2>/dev/null"
1589 "Perl script to produce output suitable for use with `file-attributes'
1590 on the remote file system.
1591 Escape sequence %s is replaced with name of Perl binary.
1592 This string is passed to `format', so percent characters need to be doubled.")
1594 (defconst tramp-perl-directory-files-and-attributes
1596 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
1597 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
1598 @list = readdir(DIR);
1602 for($i = 0; $i < $n; $i++)
1604 $filename = $list[$i];
1605 @stat = lstat($filename);
1606 if (($stat[2] & 0170000) == 0120000)
1608 $type = readlink($filename);
1609 $type = \"\\\"$type\\\"\";
1611 elsif (($stat[2] & 0170000) == 040000)
1619 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1620 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1622 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
1628 $stat[8] >> 16 & 0xffff,
1630 $stat[9] >> 16 & 0xffff,
1632 $stat[10] >> 16 & 0xffff,
1636 $stat[1] >> 16 & 0xffff,
1638 $stat[0] >> 16 & 0xffff,
1641 printf(\")\\n\");' \"$1\" \"$2\" \"$3\" 2>/dev/null"
1642 "Perl script implementing `directory-files-attributes' as Lisp `read'able
1644 Escape sequence %s is replaced with name of Perl binary.
1645 This string is passed to `format', so percent characters need to be doubled.")
1647 ;; ;; These two use uu encoding.
1648 ;; (defvar tramp-perl-encode "%s -e'\
1649 ;; print qq(begin 644 xxx\n);
1652 ;; while (read(STDIN, $s, 45)) {
1653 ;; print pack(q(u), $s);
1658 ;; "Perl program to use for encoding a file.
1659 ;; Escape sequence %s is replaced with name of Perl binary.")
1661 ;; (defvar tramp-perl-decode "%s -ne '
1662 ;; print unpack q(u), $_;
1664 ;; "Perl program to use for decoding a file.
1665 ;; Escape sequence %s is replaced with name of Perl binary.")
1667 ;; These two use base64 encoding.
1668 (defconst tramp-perl-encode-with-module
1669 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
1670 "Perl program to use for encoding a file.
1671 Escape sequence %s is replaced with name of Perl binary.
1672 This string is passed to `format', so percent characters need to be doubled.
1673 This implementation requires the MIME::Base64 Perl module to be installed
1674 on the remote host.")
1676 (defconst tramp-perl-decode-with-module
1677 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
1678 "Perl program to use for decoding a file.
1679 Escape sequence %s is replaced with name of Perl binary.
1680 This string is passed to `format', so percent characters need to be doubled.
1681 This implementation requires the MIME::Base64 Perl module to be installed
1682 on the remote host.")
1684 (defconst tramp-perl-encode
1686 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1687 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
1688 # Free Software Foundation, Inc.
1693 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1694 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1699 # We read in chunks of 54 bytes, to generate output lines
1700 # of 72 chars (plus end of line)
1703 while (my $data = <STDIN>) {
1706 # Only for the last chunk, and only if did not fill the last three-byte packet
1708 my $mod = length($data) %% 3;
1709 $pad = q(=) x (3 - $mod) if $mod;
1712 # Not the fastest method, but it is simple: unpack to binary string, split
1713 # by groups of 6 bits and convert back from binary to byte; then map into
1714 # the translation table
1718 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1722 "Perl program to use for encoding a file.
1723 Escape sequence %s is replaced with name of Perl binary.
1724 This string is passed to `format', so percent characters need to be doubled.")
1726 (defconst tramp-perl-decode
1728 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1729 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
1730 # Free Software Foundation, Inc.
1735 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1736 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1739 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1743 # We are going to accumulate into $pending to accept any line length
1744 # (we do not check they are <= 76 chars as the RFC says)
1747 while (my $data = <STDIN>) {
1750 # If we find one or two =, we have reached the end and
1751 # any following data is to be discarded
1752 my $finished = $data =~ s/(==?).*/$1/;
1755 my $len = length($pending);
1756 my $chunk = substr($pending, 0, $len & ~3);
1757 $pending = substr($pending, $len & ~3 + 1);
1759 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1760 # split in 8-bit chunks and convert back to char.
1763 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1767 "Perl program to use for decoding a file.
1768 Escape sequence %s is replaced with name of Perl binary.
1769 This string is passed to `format', so percent characters need to be doubled.")
1771 (defconst tramp-file-mode-type-map
1772 '((0 .
"-") ; Normal file (SVID-v2 and XPG2)
1774 (2 .
"c") ; character device
1775 (3 .
"m") ; multiplexed character device (v7)
1776 (4 .
"d") ; directory
1777 (5 .
"?") ; Named special file (XENIX)
1778 (6 .
"b") ; block device
1779 (7 .
"?") ; multiplexed block device (v7)
1780 (8 .
"-") ; regular file
1781 (9 .
"n") ; network special file (HP-UX)
1782 (10 .
"l") ; symlink
1783 (11 .
"?") ; ACL shadow inode (Solaris, not userspace)
1785 (13 .
"D") ; door special (Solaris)
1786 (14 .
"w")) ; whiteout (BSD)
1787 "A list of file types returned from the `stat' system call.
1788 This is used to map a mode number to a permission string.")
1790 ;; New handlers should be added here. The following operations can be
1791 ;; handled using the normal primitives: file-name-sans-versions,
1793 (defconst tramp-file-name-handler-alist
1794 '((load . tramp-handle-load
)
1795 (make-symbolic-link . tramp-handle-make-symbolic-link
)
1796 (file-name-as-directory . tramp-handle-file-name-as-directory
)
1797 (file-name-directory . tramp-handle-file-name-directory
)
1798 (file-name-nondirectory . tramp-handle-file-name-nondirectory
)
1799 (file-truename . tramp-handle-file-truename
)
1800 (file-exists-p . tramp-handle-file-exists-p
)
1801 (file-directory-p . tramp-handle-file-directory-p
)
1802 (file-executable-p . tramp-handle-file-executable-p
)
1803 (file-readable-p . tramp-handle-file-readable-p
)
1804 (file-regular-p . tramp-handle-file-regular-p
)
1805 (file-symlink-p . tramp-handle-file-symlink-p
)
1806 (file-writable-p . tramp-handle-file-writable-p
)
1807 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p
)
1808 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p
)
1809 (file-attributes . tramp-handle-file-attributes
)
1810 (file-modes . tramp-handle-file-modes
)
1811 (directory-files . tramp-handle-directory-files
)
1812 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes
)
1813 (file-name-all-completions . tramp-handle-file-name-all-completions
)
1814 (file-name-completion . tramp-handle-file-name-completion
)
1815 (add-name-to-file . tramp-handle-add-name-to-file
)
1816 (copy-file . tramp-handle-copy-file
)
1817 (rename-file . tramp-handle-rename-file
)
1818 (set-file-modes . tramp-handle-set-file-modes
)
1819 (set-file-times . tramp-handle-set-file-times
)
1820 (make-directory . tramp-handle-make-directory
)
1821 (delete-directory . tramp-handle-delete-directory
)
1822 (delete-file . tramp-handle-delete-file
)
1823 (directory-file-name . tramp-handle-directory-file-name
)
1824 ;; `executable-find' is not official yet.
1825 (executable-find . tramp-handle-executable-find
)
1826 (start-file-process . tramp-handle-start-file-process
)
1827 (process-file . tramp-handle-process-file
)
1828 (shell-command . tramp-handle-shell-command
)
1829 (insert-directory . tramp-handle-insert-directory
)
1830 (expand-file-name . tramp-handle-expand-file-name
)
1831 (substitute-in-file-name . tramp-handle-substitute-in-file-name
)
1832 (file-local-copy . tramp-handle-file-local-copy
)
1833 (file-remote-p . tramp-handle-file-remote-p
)
1834 (insert-file-contents . tramp-handle-insert-file-contents
)
1835 (insert-file-contents-literally
1836 . tramp-handle-insert-file-contents-literally
)
1837 (write-region . tramp-handle-write-region
)
1838 (find-backup-file-name . tramp-handle-find-backup-file-name
)
1839 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name
)
1840 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory
)
1841 (dired-compress-file . tramp-handle-dired-compress-file
)
1842 (dired-recursive-delete-directory
1843 . tramp-handle-dired-recursive-delete-directory
)
1844 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime
)
1845 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime
))
1846 "Alist of handler functions.
1847 Operations not mentioned here will be handled by the normal Emacs functions.")
1849 ;; Handlers for partial Tramp file names. For Emacs just
1850 ;; `file-name-all-completions' is needed.
1852 (defconst tramp-completion-file-name-handler-alist
1853 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions
)
1854 (file-name-completion . tramp-completion-handle-file-name-completion
))
1855 "Alist of completion handler functions.
1856 Used for file names matching `tramp-file-name-regexp'. Operations not
1857 mentioned here will be handled by `tramp-file-name-handler-alist' or the
1858 normal Emacs functions.")
1860 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
1861 (defvar tramp-foreign-file-name-handler-alist
1862 ;; (identity . tramp-sh-file-name-handler) should always be the last
1863 ;; entry, since `identity' always matches.
1864 '((identity . tramp-sh-file-name-handler
))
1865 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
1866 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
1869 ;;; Internal functions which must come first:
1871 (defsubst tramp-debug-message
(vec fmt-string
&rest args
)
1872 "Append message to debug buffer.
1873 Message is formatted with FMT-STRING as control string and the remaining
1874 ARGS to actually emit the message (if applicable)."
1875 (when (get-buffer (tramp-buffer-name vec
))
1876 (with-current-buffer (tramp-get-debug-buffer vec
)
1877 (goto-char (point-max))
1881 (insert (format-time-string "%T "))
1883 (let ((btn 1) btf fn
)
1885 (setq btf
(nth 1 (backtrace-frame btn
)))
1889 (setq fn
(symbol-name btf
))
1890 (unless (and (string-match "^tramp" fn
)
1892 "^tramp\\(-debug\\)?\\(-message\\|-error\\)$"
1895 (setq btn
(1+ btn
))))
1896 ;; The following code inserts filename and line number.
1897 ;; Should be deactivated by default, because it is time
1899 ; (let ((ffn (find-function-noselect (intern fn))))
1903 ; (file-name-nondirectory (buffer-file-name (car ffn)))
1904 ; (with-current-buffer (car ffn)
1905 ; (1+ (count-lines (point-min) (cdr ffn)))))))
1906 (insert (format "%s " fn
)))
1908 (insert (apply 'format fmt-string args
)))))
1910 (defsubst tramp-message
(vec-or-proc level fmt-string
&rest args
)
1911 "Emit a message depending on verbosity level.
1912 VEC-OR-PROC identifies the Tramp buffer to use. It can be either a
1913 vector or a process. LEVEL says to be quiet if `tramp-verbose' is
1914 less than LEVEL. The message is emitted only if `tramp-verbose' is
1915 greater than or equal to LEVEL.
1917 The message is also logged into the debug buffer when `tramp-verbose'
1918 is greater than or equal 4.
1920 Calls functions `message' and `tramp-debug-message' with FMT-STRING as
1921 control string and the remaining ARGS to actually emit the message (if
1924 (when (<= level tramp-verbose
)
1925 ;; Match data must be preserved!
1927 ;; Display only when there is a minimum level.
1934 ((= level
2) "Warning: ")
1938 ;; Log only when there is a minimum level.
1939 (when (>= tramp-verbose
4)
1940 (when (and vec-or-proc
1941 (processp vec-or-proc
)
1942 (buffer-name (process-buffer vec-or-proc
)))
1943 (with-current-buffer (process-buffer vec-or-proc
)
1944 ;; Translate proc to vec.
1945 (setq vec-or-proc
(tramp-dissect-file-name default-directory
))))
1946 (when (and vec-or-proc
(vectorp vec-or-proc
))
1947 (apply 'tramp-debug-message
1949 (concat (format "(%d) # " level
) fmt-string
)
1951 ;; Suppress all errors.
1954 (defsubst tramp-error
(vec-or-proc signal fmt-string
&rest args
)
1956 VEC-OR-PROC identifies the connection to use, SIGNAL is the
1957 signal identifier to be raised, remaining args passed to
1958 `tramp-message'. Finally, signal SIGNAL is raised."
1961 (error-message-string
1962 (list signal
(get signal
'error-message
) (apply 'format fmt-string args
))))
1963 (signal signal
(list (apply 'format fmt-string args
))))
1965 (defsubst tramp-error-with-buffer
1966 (buffer vec-or-proc signal fmt-string
&rest args
)
1967 "Emit an error, and show BUFFER.
1968 If BUFFER is nil, show the connection buffer. Wait for 30\", or until
1969 an input event arrives. The other arguments are passed to `tramp-error'."
1970 (save-window-excursion
1972 (apply 'tramp-error vec-or-proc signal fmt-string args
)
1973 (when (and vec-or-proc
(not (zerop tramp-verbose
)))
1974 (let ((enable-recursive-minibuffers t
))
1976 (or (and (bufferp buffer
) buffer
)
1977 (and (processp vec-or-proc
) (process-buffer vec-or-proc
))
1978 (tramp-get-buffer vec-or-proc
)))
1981 (defmacro with-parsed-tramp-file-name
(filename var
&rest body
)
1982 "Parse a Tramp filename and make components available in the body.
1984 First arg FILENAME is evaluated and dissected into its components.
1985 Second arg VAR is a symbol. It is used as a variable name to hold
1986 the filename structure. It is also used as a prefix for the variables
1987 holding the components. For example, if VAR is the symbol `foo', then
1988 `foo' will be bound to the whole structure, `foo-method' will be bound to
1989 the method component, and so on for `foo-user', `foo-host', `foo-localname'.
1991 Remaining args are Lisp expressions to be evaluated (inside an implicit
1994 If VAR is nil, then we bind `v' to the structure and `method', `user',
1995 `host', `localname' to the components."
1996 `(let* ((,(or var
'v
) (tramp-dissect-file-name ,filename
))
1997 (,(if var
(intern (concat (symbol-name var
) "-method")) 'method
)
1998 (tramp-file-name-method ,(or var
'v
)))
1999 (,(if var
(intern (concat (symbol-name var
) "-user")) 'user
)
2000 (tramp-file-name-user ,(or var
'v
)))
2001 (,(if var
(intern (concat (symbol-name var
) "-host")) 'host
)
2002 (tramp-file-name-host ,(or var
'v
)))
2003 (,(if var
(intern (concat (symbol-name var
) "-localname")) 'localname
)
2004 (tramp-file-name-localname ,(or var
'v
))))
2007 (put 'with-parsed-tramp-file-name
'lisp-indent-function
2)
2008 (put 'with-parsed-tramp-file-name
'edebug-form-spec
'(form symbolp body
))
2009 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<with-parsed-tramp-file-name\\>"))
2011 (defmacro with-file-property
(vec file property
&rest body
)
2012 "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
2013 FILE must be a local file name on a connection identified via VEC."
2014 `(if (file-name-absolute-p ,file
)
2015 (let ((value (tramp-get-file-property ,vec
,file
,property
'undef
)))
2016 (when (eq value
'undef
)
2017 ;; We cannot pass @body as parameter to
2018 ;; `tramp-set-file-property' because it mangles our
2020 (setq value
(progn ,@body
))
2021 (tramp-set-file-property ,vec
,file
,property value
))
2025 (put 'with-file-property
'lisp-indent-function
3)
2026 (put 'with-file-property
'edebug-form-spec t
)
2027 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<with-file-property\\>"))
2029 (defmacro with-connection-property
(key property
&rest body
)
2030 "Checks in Tramp for property PROPERTY, otherwise executes BODY and set."
2031 `(let ((value (tramp-get-connection-property ,key
,property
'undef
)))
2032 (when (eq value
'undef
)
2033 ;; We cannot pass ,@body as parameter to
2034 ;; `tramp-set-connection-property' because it mangles our debug
2036 (setq value
(progn ,@body
))
2037 (tramp-set-connection-property ,key
,property value
))
2040 (put 'with-connection-property
'lisp-indent-function
2)
2041 (put 'with-connection-property
'edebug-form-spec t
)
2042 (font-lock-add-keywords 'emacs-lisp-mode
'("\\<with-connection-property\\>"))
2044 (eval-and-compile ; silence compiler
2045 (if (memq system-type
'(cygwin windows-nt
))
2046 (defun tramp-drop-volume-letter (name)
2047 "Cut off unnecessary drive letter from file NAME.
2048 The function `tramp-handle-expand-file-name' calls `expand-file-name'
2049 locally on a remote file name. When the local system is a W32 system
2050 but the remote system is Unix, this introduces a superfluous drive
2051 letter into the file name. This function removes it."
2053 (if (string-match tramp-root-regexp name
)
2054 (replace-match "/" nil t name
)
2057 (defalias 'tramp-drop-volume-letter
'identity
)))
2059 (defsubst tramp-make-tramp-temp-file
(vec)
2060 "Create a temporary file on the remote host identified by VEC.
2061 Return the local name of the temporary file."
2063 (tramp-make-tramp-file-name
2064 (tramp-file-name-method vec
)
2065 (tramp-file-name-user vec
)
2066 (tramp-file-name-host vec
)
2067 (tramp-drop-volume-letter
2069 tramp-temp-name-prefix
(tramp-get-remote-tmpdir vec
)))))
2072 ;; `make-temp-file' would be the natural choice for
2073 ;; implementation. But it calls `write-region' internally,
2074 ;; which also needs a temporary file - we would end in an
2076 (setq result
(make-temp-name prefix
))
2077 (if (file-exists-p result
)
2079 ;; This creates the file by side effect.
2080 (set-file-times result
)
2081 (set-file-modes result
(tramp-octal-to-decimal "0700"))))
2083 ;; Return the local part.
2084 (with-parsed-tramp-file-name result nil localname
)))
2087 ;;; Config Manipulation Functions:
2089 (defun tramp-set-completion-function (method function-list
)
2090 "Sets the list of completion functions for METHOD.
2091 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2092 The FUNCTION is intended to parse FILE according its syntax.
2093 It might be a predefined FUNCTION, or a user defined FUNCTION.
2094 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
2095 `tramp-parse-sconfig', `tramp-parse-hosts', `tramp-parse-passwd',
2096 and `tramp-parse-netrc'.
2100 (tramp-set-completion-function
2102 '((tramp-parse-sconfig \"/etc/ssh_config\")
2103 (tramp-parse-sconfig \"~/.ssh/config\")))"
2105 (let ((r function-list
)
2107 (setq tramp-completion-function-alist
2108 (delete (assoc method tramp-completion-function-alist
)
2109 tramp-completion-function-alist
))
2112 ;; Remove double entries.
2113 (when (member (car v
) (cdr v
))
2114 (setcdr v
(delete (car v
) (cdr v
))))
2115 ;; Check for function and file or registry key.
2116 (unless (and (functionp (nth 0 (car v
)))
2117 (if (string-match "^HKEY_CURRENT_USER" (nth 1 (car v
)))
2118 ;; Windows registry.
2119 (and (memq system-type
'(cygwin windows-nt
))
2121 (tramp-local-call-process
2122 "reg" nil nil nil
"query" (nth 1 (car v
)))))
2123 ;; Configuration file.
2124 (file-exists-p (nth 1 (car v
)))))
2125 (setq r
(delete (car v
) r
)))
2129 (add-to-list 'tramp-completion-function-alist
2132 (defun tramp-get-completion-function (method)
2133 "Returns a list of completion functions for METHOD.
2134 For definition of that list see `tramp-set-completion-function'."
2136 ;; Hosts visited once shall be remembered.
2137 `(tramp-parse-connection-properties ,method
)
2138 ;; The method related defaults.
2139 (cdr (assoc method tramp-completion-function-alist
))))
2142 ;;; Fontification of `read-file-name':
2144 ;; rfn-eshadow.el is part of Emacs 22. It is autoloaded.
2145 (defvar tramp-rfn-eshadow-overlay
)
2146 (make-variable-buffer-local 'tramp-rfn-eshadow-overlay
)
2148 (defun tramp-rfn-eshadow-setup-minibuffer ()
2149 "Set up a minibuffer for `file-name-shadow-mode'.
2150 Adds another overlay hiding filename parts according to Tramp's
2151 special handling of `substitute-in-file-name'."
2152 (when (symbol-value 'minibuffer-completing-file-name
)
2153 (setq tramp-rfn-eshadow-overlay
2154 (funcall (symbol-function 'make-overlay
)
2155 (funcall (symbol-function 'minibuffer-prompt-end
))
2156 (funcall (symbol-function 'minibuffer-prompt-end
))))
2157 ;; Copy rfn-eshadow-overlay properties.
2158 (let ((props (funcall (symbol-function 'overlay-properties
)
2159 (symbol-value 'rfn-eshadow-overlay
))))
2161 (funcall (symbol-function 'overlay-put
)
2162 tramp-rfn-eshadow-overlay
(pop props
) (pop props
))))))
2164 (when (boundp 'rfn-eshadow-setup-minibuffer-hook
)
2165 (add-hook 'rfn-eshadow-setup-minibuffer-hook
2166 'tramp-rfn-eshadow-setup-minibuffer
)
2167 (add-hook 'tramp-unload-hook
2169 (remove-hook 'rfn-eshadow-setup-minibuffer-hook
2170 'tramp-rfn-eshadow-setup-minibuffer
))))
2172 (defun tramp-rfn-eshadow-update-overlay ()
2173 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
2174 This is intended to be used as a minibuffer `post-command-hook' for
2175 `file-name-shadow-mode'; the minibuffer should have already
2176 been set up by `rfn-eshadow-setup-minibuffer'."
2177 ;; In remote files name, there is a shadowing just for the local part.
2178 (let ((end (or (funcall (symbol-function 'overlay-end
)
2179 (symbol-value 'rfn-eshadow-overlay
))
2180 (funcall (symbol-function 'minibuffer-prompt-end
)))))
2181 (when (file-remote-p (buffer-substring-no-properties end
(point-max)))
2185 (1+ (or (string-match "/" (buffer-string) end
) end
)) (point-max))
2186 (let ((rfn-eshadow-overlay tramp-rfn-eshadow-overlay
)
2187 (rfn-eshadow-update-overlay-hook nil
))
2188 (move-overlay rfn-eshadow-overlay
(point-max) (point-max))
2189 (funcall (symbol-function 'rfn-eshadow-update-overlay
))))))))
2191 (when (boundp 'rfn-eshadow-update-overlay-hook
)
2192 (add-hook 'rfn-eshadow-update-overlay-hook
2193 'tramp-rfn-eshadow-update-overlay
))
2196 ;;; File Name Handler Functions:
2198 (defun tramp-handle-make-symbolic-link
2199 (filename linkname
&optional ok-if-already-exists
)
2200 "Like `make-symbolic-link' for Tramp files.
2201 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
2202 the symlink. If LINKNAME is a Tramp file, only the localname component is
2203 used as the target of the symlink.
2205 If LINKNAME is a Tramp file and the localname component is relative, then
2206 it is expanded first, before the localname component is taken. Note that
2207 this can give surprising results if the user/host for the source and
2208 target of the symlink differ."
2209 (with-parsed-tramp-file-name linkname l
2210 (let ((ln (tramp-get-remote-ln l
))
2211 (cwd (tramp-run-real-handler
2212 'file-name-directory
(list l-localname
))))
2216 "Making a symbolic link. ln(1) does not exist on the remote host."))
2218 ;; Do the 'confirm if exists' thing.
2219 (when (file-exists-p linkname
)
2221 (if (or (null ok-if-already-exists
) ; not allowed to exist
2222 (and (numberp ok-if-already-exists
)
2225 "File %s already exists; make it a link anyway? "
2228 l
'file-already-exists
"File %s already exists" l-localname
)
2229 (delete-file linkname
)))
2231 ;; If FILENAME is a Tramp name, use just the localname component.
2232 (when (tramp-tramp-file-p filename
)
2234 (tramp-file-name-localname
2235 (tramp-dissect-file-name (expand-file-name filename
)))))
2237 ;; Right, they are on the same host, regardless of user, method, etc.
2238 ;; We now make the link on the remote machine. This will occur as the user
2239 ;; that FILENAME belongs to.
2241 (tramp-send-command-and-check
2242 l
(format "cd %s && %s -sf %s %s" cwd ln filename l-localname
) t
)))))
2244 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix
)
2245 "Like `load' for Tramp files."
2246 (with-parsed-tramp-file-name (expand-file-name file
) nil
2248 (cond ((file-exists-p (concat file
".elc"))
2249 (setq file
(concat file
".elc")))
2250 ((file-exists-p (concat file
".el"))
2251 (setq file
(concat file
".el")))))
2253 ;; The first condition is always true for absolute file names.
2254 ;; Included for safety's sake.
2255 (unless (or (file-name-directory file
)
2256 (string-match "\\.elc?\\'" file
))
2259 "File `%s' does not include a `.el' or `.elc' suffix" file
)))
2261 (when (not (file-exists-p file
))
2262 (tramp-error v
'file-error
"Cannot load nonexistent file `%s'" file
)))
2263 (if (not (file-exists-p file
))
2265 (unless nomessage
(tramp-message v
0 "Loading %s..." file
))
2266 (let ((local-copy (file-local-copy file
)))
2267 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2269 (load local-copy noerror t t
)
2270 (delete-file local-copy
)))
2271 (unless nomessage
(tramp-message v
0 "Loading %s...done" file
))
2274 ;; Localname manipulation functions that grok Tramp localnames...
2275 (defun tramp-handle-file-name-as-directory (file)
2276 "Like `file-name-as-directory' but aware of Tramp files."
2277 ;; `file-name-as-directory' would be sufficient except localname is
2278 ;; the empty string.
2279 (let ((v (tramp-dissect-file-name file t
)))
2280 ;; Run the command on the localname portion only.
2281 (tramp-make-tramp-file-name
2282 (tramp-file-name-method v
)
2283 (tramp-file-name-user v
)
2284 (tramp-file-name-host v
)
2285 (tramp-run-real-handler
2286 'file-name-as-directory
(list (or (tramp-file-name-localname v
) ""))))))
2288 (defun tramp-handle-file-name-directory (file)
2289 "Like `file-name-directory' but aware of Tramp files."
2290 ;; Everything except the last filename thing is the directory. We
2291 ;; cannot apply `with-parsed-tramp-file-name', because this expands
2292 ;; the remote file name parts. This is a problem when we are in
2293 ;; file name completion.
2294 (let ((v (tramp-dissect-file-name file t
)))
2295 ;; Run the command on the localname portion only.
2296 (tramp-make-tramp-file-name
2297 (tramp-file-name-method v
)
2298 (tramp-file-name-user v
)
2299 (tramp-file-name-host v
)
2300 (tramp-run-real-handler
2301 'file-name-directory
(list (or (tramp-file-name-localname v
) ""))))))
2303 (defun tramp-handle-file-name-nondirectory (file)
2304 "Like `file-name-nondirectory' but aware of Tramp files."
2305 (with-parsed-tramp-file-name file nil
2306 (tramp-run-real-handler 'file-name-nondirectory
(list localname
))))
2308 (defun tramp-handle-file-truename (filename &optional counter prev-dirs
)
2309 "Like `file-truename' for Tramp files."
2310 (with-parsed-tramp-file-name (expand-file-name filename
) nil
2311 (with-file-property v localname
"file-truename"
2312 (let* ((directory-sep-char ?
/) ; for XEmacs
2313 (steps (tramp-split-string localname
"/"))
2314 (localnamedir (tramp-run-real-handler
2315 'file-name-as-directory
(list localname
)))
2316 (is-dir (string= localname localnamedir
))
2319 ;; Don't make the following value larger than necessary.
2320 ;; People expect an error message in a timely fashion when
2321 ;; something is wrong; otherwise they might think that Emacs
2322 ;; is hung. Of course, correctness has to come first.
2324 (result nil
) ;result steps in reverse order
2326 (tramp-message v
4 "Finding true name for `%s'" filename
)
2327 (while (and steps
(< numchase numchase-limit
))
2328 (setq thisstep
(pop steps
))
2331 (mapconcat 'identity
2332 (append '("") (reverse result
) (list thisstep
))
2334 (setq symlink-target
2335 (nth 0 (file-attributes
2336 (tramp-make-tramp-file-name
2338 (mapconcat 'identity
2343 (cond ((string= "." thisstep
)
2344 (tramp-message v
5 "Ignoring step `.'"))
2345 ((string= ".." thisstep
)
2346 (tramp-message v
5 "Processing step `..'")
2348 ((stringp symlink-target
)
2349 ;; It's a symlink, follow it.
2350 (tramp-message v
5 "Follow symlink to %s" symlink-target
)
2351 (setq numchase
(1+ numchase
))
2352 (when (file-name-absolute-p symlink-target
)
2354 ;; If the symlink was absolute, we'll get a string like
2355 ;; "/user@host:/some/target"; extract the
2356 ;; "/some/target" part from it.
2357 (when (tramp-tramp-file-p symlink-target
)
2358 (unless (tramp-equal-remote filename symlink-target
)
2361 "Symlink target `%s' on wrong host" symlink-target
))
2362 (setq symlink-target localname
))
2364 (append (tramp-split-string symlink-target
"/")
2368 (setq result
(cons thisstep result
)))))
2369 (when (>= numchase numchase-limit
)
2372 "Maximum number (%d) of symlinks exceeded" numchase-limit
))
2373 (setq result
(reverse result
))
2374 ;; Combine list to form string.
2377 (mapconcat 'identity
(cons "" result
) "/")
2379 (when (and is-dir
(or (string= "" result
)
2380 (not (string= (substring result -
1) "/"))))
2381 (setq result
(concat result
"/")))
2382 (tramp-message v
4 "True name of `%s' is `%s'" filename result
)
2383 (tramp-make-tramp-file-name method user host result
)))))
2387 (defun tramp-handle-file-exists-p (filename)
2388 "Like `file-exists-p' for Tramp files."
2389 (with-parsed-tramp-file-name filename nil
2390 (with-file-property v localname
"file-exists-p"
2391 (zerop (tramp-send-command-and-check
2395 (tramp-get-file-exists-command v
)
2396 (tramp-shell-quote-argument localname
)))))))
2398 ;; Inodes don't exist for some file systems. Therefore we must
2399 ;; generate virtual ones. Used in `find-buffer-visiting'. The method
2400 ;; applied might be not so efficient (Ange-FTP uses hashes). But
2401 ;; performance isn't the major issue given that file transfer will
2403 (defvar tramp-inodes nil
2404 "Keeps virtual inodes numbers.")
2406 ;; Devices must distinguish physical file systems. The device numbers
2407 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2408 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2409 ;; EFS use device number "-1". In order to be different, we use device number
2410 ;; (-1 . x), whereby "x" is unique for a given (method user host).
2411 (defvar tramp-devices nil
2412 "Keeps virtual device numbers.")
2414 ;; CCC: This should check for an error condition and signal failure
2415 ;; when something goes wrong.
2416 ;; Daniel Pittman <daniel@danann.net>
2417 (defun tramp-handle-file-attributes (filename &optional id-format
)
2418 "Like `file-attributes' for Tramp files."
2419 (unless id-format
(setq id-format
'integer
))
2420 (with-parsed-tramp-file-name (expand-file-name filename
) nil
2421 (with-file-property v localname
(format "file-attributes-%s" id-format
)
2422 (when (file-exists-p filename
)
2423 ;; file exists, find out stuff
2425 (tramp-convert-file-attributes
2427 (if (tramp-get-remote-stat v
)
2428 (tramp-handle-file-attributes-with-stat v localname id-format
)
2429 (if (tramp-get-remote-perl v
)
2430 (tramp-handle-file-attributes-with-perl v localname id-format
)
2431 (tramp-handle-file-attributes-with-ls
2432 v localname id-format
)))))))))
2434 (defun tramp-handle-file-attributes-with-ls (vec localname
&optional id-format
)
2435 "Implement `file-attributes' for Tramp files using the ls(1) command."
2437 res-inode res-filemodes res-numlinks
2438 res-uid res-gid res-size res-symlink-target
)
2439 (tramp-message vec
5 "file attributes with ls: %s" localname
)
2443 (tramp-get-ls-command vec
)
2444 (if (eq id-format
'integer
) "-ildn" "-ild")
2445 (tramp-shell-quote-argument localname
)))
2446 ;; parse `ls -l' output ...
2447 (with-current-buffer (tramp-get-buffer vec
)
2448 (goto-char (point-min))
2452 (read (current-buffer))
2453 (invalid-read-syntax
2454 (when (and (equal (cadr err
)
2455 "Integer constant overflow in reader")
2457 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2459 (let* ((big (read (substring (car (cddr err
)) 0
2460 (match-beginning 1))))
2461 (small (read (match-string 1 (car (cddr err
)))))
2462 (twiddle (/ small
65536)))
2463 (cons (+ big twiddle
)
2464 (- small
(* twiddle
65536))))))))
2465 ;; ... file mode flags
2466 (setq res-filemodes
(symbol-name (read (current-buffer))))
2468 (setq res-numlinks
(read (current-buffer)))
2470 (setq res-uid
(read (current-buffer)))
2471 (setq res-gid
(read (current-buffer)))
2472 (if (eq id-format
'integer
)
2474 (unless (numberp res-uid
) (setq res-uid -
1))
2475 (unless (numberp res-gid
) (setq res-gid -
1)))
2477 (unless (stringp res-uid
) (setq res-uid
(symbol-name res-uid
)))
2478 (unless (stringp res-gid
) (setq res-gid
(symbol-name res-gid
)))))
2480 (setq res-size
(read (current-buffer)))
2481 ;; From the file modes, figure out other stuff.
2482 (setq symlinkp
(eq ?l
(aref res-filemodes
0)))
2483 (setq dirp
(eq ?d
(aref res-filemodes
0)))
2484 ;; if symlink, find out file name pointed to
2486 (search-forward "-> ")
2487 (setq res-symlink-target
2488 (buffer-substring (point) (tramp-compat-line-end-position))))
2489 ;; return data gathered
2491 ;; 0. t for directory, string (name linked to) for symbolic
2493 (or dirp res-symlink-target
)
2494 ;; 1. Number of links to file.
2500 ;; 4. Last access time, as a list of two integers. First
2501 ;; integer has high-order 16 bits of time, second has low 16
2503 ;; 5. Last modification time, likewise.
2504 ;; 6. Last status change time, likewise.
2505 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2506 ;; 7. Size in bytes (-1, if number is out of range).
2508 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2510 ;; 9. t if file's gid would change if file were deleted and
2511 ;; recreated. Will be set in `tramp-convert-file-attributes'
2513 ;; 10. inode number.
2515 ;; 11. Device number. Will be replaced by a virtual device number.
2519 (defun tramp-handle-file-attributes-with-perl
2520 (vec localname
&optional id-format
)
2521 "Implement `file-attributes' for Tramp files using a Perl script."
2522 (tramp-message vec
5 "file attributes with perl: %s" localname
)
2523 (tramp-maybe-send-script
2524 vec tramp-perl-file-attributes
"tramp_perl_file_attributes")
2525 (tramp-send-command-and-read
2527 (format "tramp_perl_file_attributes %s %s"
2528 (tramp-shell-quote-argument localname
) id-format
)))
2530 (defun tramp-handle-file-attributes-with-stat
2531 (vec localname
&optional id-format
)
2532 "Implement `file-attributes' for Tramp files using stat(1) command."
2533 (tramp-message vec
5 "file attributes with stat: %s" localname
)
2534 (tramp-send-command-and-read
2537 "%s -c '((\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)' %s"
2538 (tramp-get-remote-stat vec
)
2539 (if (eq id-format
'integer
) "%u" "\"%U\"")
2540 (if (eq id-format
'integer
) "%g" "\"%G\"")
2541 (tramp-shell-quote-argument localname
))))
2543 (defun tramp-handle-set-visited-file-modtime (&optional time-list
)
2544 "Like `set-visited-file-modtime' for Tramp files."
2545 (unless (buffer-file-name)
2546 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2549 (tramp-run-real-handler 'set-visited-file-modtime
(list time-list
))
2550 (let ((f (buffer-file-name))
2552 (with-parsed-tramp-file-name f nil
2553 (let* ((attr (file-attributes f
))
2554 ;; '(-1 65535) means file doesn't exists yet.
2555 (modtime (or (nth 5 attr
) '(-1 65535))))
2556 (when (boundp 'last-coding-system-used
)
2557 (setq coding-system-used
(symbol-value 'last-coding-system-used
)))
2558 ;; We use '(0 0) as a don't-know value. See also
2559 ;; `tramp-handle-file-attributes-with-ls'.
2560 (if (not (equal modtime
'(0 0)))
2561 (tramp-run-real-handler 'set-visited-file-modtime
(list modtime
))
2565 (format "%s -ild %s"
2566 (tramp-get-ls-command v
)
2567 (tramp-shell-quote-argument localname
)))
2568 (setq attr
(buffer-substring (point)
2569 (progn (end-of-line) (point)))))
2570 (tramp-set-file-property
2571 v localname
"visited-file-modtime-ild" attr
))
2572 (when (boundp 'last-coding-system-used
)
2573 (set 'last-coding-system-used coding-system-used
))
2576 ;; CCC continue here
2578 ;; This function makes the same assumption as
2579 ;; `tramp-handle-set-visited-file-modtime'.
2580 (defun tramp-handle-verify-visited-file-modtime (buf)
2581 "Like `verify-visited-file-modtime' for Tramp files.
2582 At the time `verify-visited-file-modtime' calls this function, we
2583 already know that the buffer is visiting a file and that
2584 `visited-file-modtime' does not return 0. Do not call this
2585 function directly, unless those two cases are already taken care
2587 (with-current-buffer buf
2588 ;; There is no file visiting the buffer, or the buffer has no
2589 ;; recorded last modification time.
2590 (if (or (not (buffer-file-name))
2591 (eq (visited-file-modtime) 0))
2593 (let ((f (buffer-file-name)))
2594 (with-parsed-tramp-file-name f nil
2595 (tramp-flush-file-property v localname
)
2596 (let* ((attr (file-attributes f
))
2597 (modtime (nth 5 attr
))
2598 (mt (visited-file-modtime)))
2601 ;; file exists, and has a known modtime.
2602 ((and attr
(not (equal modtime
'(0 0))))
2603 (< (abs (tramp-time-diff
2605 ;; For compatibility, deal with both the old
2606 ;; (HIGH . LOW) and the new (HIGH LOW)
2607 ;; return values of `visited-file-modtime'.
2609 (list (car mt
) (cdr mt
))
2612 ;; modtime has the don't know value.
2616 (format "%s -ild %s"
2617 (tramp-get-ls-command v
)
2618 (tramp-shell-quote-argument localname
)))
2619 (with-current-buffer (tramp-get-buffer v
)
2620 (setq attr
(buffer-substring
2621 (point) (progn (end-of-line) (point)))))
2624 (tramp-get-file-property
2625 v localname
"visited-file-modtime-ild" "")))
2626 ;; If file does not exist, say it is not modified
2627 ;; if and only if that agrees with the buffer's record.
2628 (t (equal mt
'(-1 65535))))))))))
2630 (defun tramp-handle-set-file-modes (filename mode
)
2631 "Like `set-file-modes' for Tramp files."
2632 (with-parsed-tramp-file-name filename nil
2633 (tramp-flush-file-property v localname
)
2634 (unless (zerop (tramp-send-command-and-check
2636 (format "chmod %s %s"
2637 (tramp-decimal-to-octal mode
)
2638 (tramp-shell-quote-argument localname
))))
2639 ;; FIXME: extract the proper text from chmod's stderr.
2641 v
'file-error
"Error while changing file's mode %s" filename
))))
2643 (defun tramp-handle-set-file-times (filename &optional time
)
2644 "Like `set-file-times' for Tramp files."
2646 (if (file-remote-p filename
)
2647 (with-parsed-tramp-file-name filename nil
2648 (tramp-flush-file-property v localname
)
2649 (let ((time (if (or (null time
) (equal time
'(0 0)))
2653 ;; With GNU Emacs, `format-time-string' has an
2654 ;; optional parameter UNIVERSAL. This is preferred,
2655 ;; because we could handle the case when the remote
2656 ;; host is located in a different time zone as the
2658 (and (functionp 'subr-arity
)
2659 (subrp (symbol-function 'format-time-string
))
2660 (= 3 (cdr (funcall (symbol-function 'subr-arity
)
2662 'format-time-string
)))))))
2663 (tramp-send-command-and-check
2664 v
(format "%s touch -t %s %s"
2665 (if utc
"TZ=UTC; export TZ;" "")
2667 (format-time-string "%Y%m%d%H%M.%S" time t
)
2668 (format-time-string "%Y%m%d%H%M.%S" time
))
2669 (tramp-shell-quote-argument localname
)))))
2671 ;; We handle also the local part, because in older Emacsen,
2672 ;; without `set-file-times', this function is an alias for this.
2673 ;; We are local, so we don't need the UTC settings.
2674 (tramp-local-call-process
2675 "touch" nil nil nil
"-t"
2676 (format-time-string "%Y%m%d%H%M.%S" time
)
2677 (tramp-shell-quote-argument filename
)))))
2679 (defun tramp-set-file-uid-gid (filename &optional uid gid
)
2680 "Set the ownership for FILENAME.
2681 If UID and GID are provided, these values are used; otherwise uid
2682 and gid of the corresponding user is taken. Both parameters must be integers."
2683 ;; CCC: Modern Unices allow chown only for root. So we might need
2684 ;; another implementation, see `dired-do-chown'. OTOH, it is
2685 ;; mostly working with su(do)? when it is needed, so it shall
2686 ;; succeed in the majority of cases.
2687 (if (file-remote-p filename
)
2688 (with-parsed-tramp-file-name filename nil
2689 (let ((uid (or (and (integerp uid
) uid
)
2690 (tramp-get-remote-uid v
'integer
)))
2691 (gid (or (and (integerp gid
) gid
)
2692 (tramp-get-remote-gid v
'integer
))))
2695 "chown %d:%d %s" uid gid
2696 (tramp-shell-quote-argument localname
)))))
2698 ;; We handle also the local part, because there doesn't exist
2699 ;; `set-file-uid-gid'. On Win32 "chown" might not work.
2700 (let ((uid (or (and (integerp uid
) uid
) (tramp-get-local-uid 'integer
)))
2701 (gid (or (and (integerp gid
) gid
) (tramp-get-local-gid 'integer
))))
2702 (tramp-local-call-process
2704 (format "%d:%d" uid gid
) (tramp-shell-quote-argument filename
)))))
2706 ;; Simple functions using the `test' command.
2708 (defun tramp-handle-file-executable-p (filename)
2709 "Like `file-executable-p' for Tramp files."
2710 (with-parsed-tramp-file-name filename nil
2711 (with-file-property v localname
"file-executable-p"
2712 (zerop (tramp-run-test "-x" filename
)))))
2714 (defun tramp-handle-file-readable-p (filename)
2715 "Like `file-readable-p' for Tramp files."
2716 (with-parsed-tramp-file-name filename nil
2717 (with-file-property v localname
"file-readable-p"
2718 (zerop (tramp-run-test "-r" filename
)))))
2720 ;; When the remote shell is started, it looks for a shell which groks
2721 ;; tilde expansion. Here, we assume that all shells which grok tilde
2722 ;; expansion will also provide a `test' command which groks `-nt' (for
2723 ;; newer than). If this breaks, tell me about it and I'll try to do
2724 ;; something smarter about it.
2725 (defun tramp-handle-file-newer-than-file-p (file1 file2
)
2726 "Like `file-newer-than-file-p' for Tramp files."
2727 (cond ((not (file-exists-p file1
))
2729 ((not (file-exists-p file2
))
2731 ;; We are sure both files exist at this point.
2734 ;; We try to get the mtime of both files. If they are not
2735 ;; equal to the "dont-know" value, then we subtract the times
2736 ;; and obtain the result.
2737 (let ((fa1 (file-attributes file1
))
2738 (fa2 (file-attributes file2
)))
2739 (if (and (not (equal (nth 5 fa1
) '(0 0)))
2740 (not (equal (nth 5 fa2
) '(0 0))))
2741 (> 0 (tramp-time-diff (nth 5 fa2
) (nth 5 fa1
)))
2742 ;; If one of them is the dont-know value, then we can
2743 ;; still try to run a shell command on the remote host.
2744 ;; However, this only works if both files are Tramp
2745 ;; files and both have the same method, same user, same
2747 (unless (tramp-equal-remote file1 file2
)
2748 (with-parsed-tramp-file-name
2749 (if (tramp-tramp-file-p file1
) file1 file2
) nil
2752 "Files %s and %s must have same method, user, host"
2754 (with-parsed-tramp-file-name file1 nil
2755 (zerop (tramp-run-test2
2756 (tramp-get-test-nt-command v
) file1 file2
)))))))))
2758 ;; Functions implemented using the basic functions above.
2760 (defun tramp-handle-file-modes (filename)
2761 "Like `file-modes' for Tramp files."
2762 (let ((truename (or (file-truename filename
) filename
)))
2763 (when (file-exists-p truename
)
2764 (tramp-mode-string-to-int (nth 8 (file-attributes truename
))))))
2766 (defun tramp-handle-file-directory-p (filename)
2767 "Like `file-directory-p' for Tramp files."
2768 ;; Care must be taken that this function returns `t' for symlinks
2769 ;; pointing to directories. Surely the most obvious implementation
2770 ;; would be `test -d', but that returns false for such symlinks.
2771 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
2772 ;; I now think he's right. So we could be using `test -d', couldn't
2775 ;; Alternatives: `cd %s', `test -d %s'
2776 (with-parsed-tramp-file-name filename nil
2777 (with-file-property v localname
"file-directory-p"
2778 (zerop (tramp-run-test "-d" filename
)))))
2780 (defun tramp-handle-file-regular-p (filename)
2781 "Like `file-regular-p' for Tramp files."
2782 (and (file-exists-p filename
)
2783 (eq ?-
(aref (nth 8 (file-attributes filename
)) 0))))
2785 (defun tramp-handle-file-symlink-p (filename)
2786 "Like `file-symlink-p' for Tramp files."
2787 (with-parsed-tramp-file-name filename nil
2788 (let ((x (car (file-attributes filename
))))
2790 ;; When Tramp is running on VMS, then `file-name-absolute-p'
2791 ;; might do weird things.
2792 (if (file-name-absolute-p x
)
2793 (tramp-make-tramp-file-name method user host x
)
2796 (defun tramp-handle-file-writable-p (filename)
2797 "Like `file-writable-p' for Tramp files."
2798 (with-parsed-tramp-file-name filename nil
2799 (with-file-property v localname
"file-writable-p"
2800 (if (file-exists-p filename
)
2801 ;; Existing files must be writable.
2802 (zerop (tramp-run-test "-w" filename
))
2803 ;; If file doesn't exist, check if directory is writable.
2804 (and (zerop (tramp-run-test
2805 "-d" (file-name-directory filename
)))
2806 (zerop (tramp-run-test
2807 "-w" (file-name-directory filename
))))))))
2809 (defun tramp-handle-file-ownership-preserved-p (filename)
2810 "Like `file-ownership-preserved-p' for Tramp files."
2811 (with-parsed-tramp-file-name filename nil
2812 (with-file-property v localname
"file-ownership-preserved-p"
2813 (let ((attributes (file-attributes filename
)))
2814 ;; Return t if the file doesn't exist, since it's true that no
2815 ;; information would be lost by an (attempted) delete and create.
2816 (or (null attributes
)
2817 (= (nth 2 attributes
) (tramp-get-remote-uid v
'integer
)))))))
2819 ;; Other file name ops.
2821 (defun tramp-handle-directory-file-name (directory)
2822 "Like `directory-file-name' for Tramp files."
2823 ;; If localname component of filename is "/", leave it unchanged.
2824 ;; Otherwise, remove any trailing slash from localname component.
2825 ;; Method, host, etc, are unchanged. Does it make sense to try
2826 ;; to avoid parsing the filename?
2827 (with-parsed-tramp-file-name directory nil
2828 (if (and (not (zerop (length localname
)))
2829 (eq (aref localname
(1- (length localname
))) ?
/)
2830 (not (string= localname
"/")))
2831 (substring directory
0 -
1)
2834 ;; Directory listings.
2836 (defun tramp-handle-directory-files
2837 (directory &optional full match nosort files-only
)
2838 "Like `directory-files' for Tramp files."
2839 ;; FILES-ONLY is valid for XEmacs only.
2840 (when (file-directory-p directory
)
2841 (setq directory
(expand-file-name directory
))
2842 (let ((temp (nreverse (file-name-all-completions "" directory
)))
2846 (setq item
(directory-file-name (pop temp
)))
2847 (when (and (or (null match
) (string-match match item
))
2848 (or (null files-only
)
2850 (and (equal files-only t
) (file-regular-p item
))
2852 (file-directory-p item
)))
2853 (push (if full
(expand-file-name item directory
) item
)
2857 (defun tramp-handle-directory-files-and-attributes
2858 (directory &optional full match nosort id-format
)
2859 "Like `directory-files-and-attributes' for Tramp files."
2860 (unless id-format
(setq id-format
'integer
))
2861 (when (file-directory-p directory
)
2862 (setq directory
(expand-file-name directory
))
2864 (tramp-compat-copy-tree
2865 (with-parsed-tramp-file-name directory nil
2868 (format "directory-files-and-attributes-%s" id-format
)
2873 (tramp-convert-file-attributes v
(cdr x
))))
2874 (if (tramp-get-remote-stat v
)
2875 (tramp-handle-directory-files-and-attributes-with-stat
2876 v localname id-format
)
2877 (if (tramp-get-remote-perl v
)
2878 (tramp-handle-directory-files-and-attributes-with-perl
2879 v localname id-format
)))))))))
2883 (setq item
(pop temp
))
2884 (when (or (null match
) (string-match match
(car item
)))
2886 (setcar item
(expand-file-name (car item
) directory
)))
2887 (push item result
)))
2891 (sort result
(lambda (x y
) (string< (car x
) (car y
))))))))
2893 (defun tramp-handle-directory-files-and-attributes-with-perl
2894 (vec localname
&optional id-format
)
2895 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
2896 (tramp-message vec
5 "directory-files-and-attributes with perl: %s" localname
)
2897 (tramp-maybe-send-script
2898 vec tramp-perl-directory-files-and-attributes
2899 "tramp_perl_directory_files_and_attributes")
2901 (tramp-send-command-and-read
2903 (format "tramp_perl_directory_files_and_attributes %s %s"
2904 (tramp-shell-quote-argument localname
) id-format
))))
2905 (when (stringp object
) (tramp-error vec
'file-error object
))
2908 (defun tramp-handle-directory-files-and-attributes-with-stat
2909 (vec localname
&optional id-format
)
2910 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
2911 (tramp-message vec
5 "directory-files-and-attributes with stat: %s" localname
)
2912 (tramp-send-command-and-read
2916 "cd %s; echo \"(\"; (%s -ab | xargs "
2917 "%s -c '(\"%%n\" (\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)'); "
2919 (tramp-shell-quote-argument localname
)
2920 (tramp-get-ls-command vec
)
2921 (tramp-get-remote-stat vec
)
2922 (if (eq id-format
'integer
) "%u" "\"%U\"")
2923 (if (eq id-format
'integer
) "%g" "\"%G\""))))
2925 ;; This function should return "foo/" for directories and "bar" for
2927 (defun tramp-handle-file-name-all-completions (filename directory
)
2928 "Like `file-name-all-completions' for Tramp files."
2929 (unless (save-match-data (string-match "/" filename
))
2930 (with-parsed-tramp-file-name (expand-file-name directory
) nil
2931 ;; Flush the directory cache. There could be changed directory
2933 (when (and (integerp tramp-completion-reread-directory-timeout
)
2936 (tramp-get-file-property
2937 v localname
"last-completion" '(0 0 0)))
2938 tramp-completion-reread-directory-timeout
))
2939 (tramp-flush-file-property v localname
))
2945 (with-file-property v localname
"file-name-all-completions"
2947 (tramp-barf-unless-okay
2949 (format "cd %s" (tramp-shell-quote-argument localname
))
2950 "tramp-handle-file-name-all-completions: Couldn't `cd %s'"
2951 (tramp-shell-quote-argument localname
))
2953 ;; Get a list of directories and files, including reliably
2954 ;; tagging the directories with a trailing '/'. Because I
2955 ;; rock. --daniel@danann.net
2958 (format (concat "%s -ab 2>/dev/null | while read f; do "
2959 "if %s -d \"$f\" 2>/dev/null; "
2960 "then echo \"$f/\"; else echo \"$f\"; fi; done")
2961 (tramp-get-ls-command v
)
2962 (tramp-get-test-command v
)))
2964 ;; Now grab the output.
2965 (with-current-buffer (tramp-get-buffer v
)
2966 (goto-char (point-max))
2967 (while (zerop (forward-line -
1))
2968 (push (buffer-substring
2969 (point) (tramp-compat-line-end-position))
2972 (tramp-set-file-property
2973 v localname
"last-completion" (current-time))
2976 ;; The following isn't needed for Emacs 20 but for 19.34?
2977 (defun tramp-handle-file-name-completion
2978 (filename directory
&optional predicate
)
2979 "Like `file-name-completion' for Tramp files."
2980 (unless (tramp-tramp-file-p directory
)
2982 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
2986 (mapcar 'list
(file-name-all-completions filename directory
))
2988 (lambda (x) (funcall predicate
(expand-file-name (car x
) directory
))))))
2992 (defun tramp-handle-add-name-to-file
2993 (filename newname
&optional ok-if-already-exists
)
2994 "Like `add-name-to-file' for Tramp files."
2995 (unless (tramp-equal-remote filename newname
)
2996 (with-parsed-tramp-file-name
2997 (if (tramp-tramp-file-p filename
) filename newname
) nil
3000 "add-name-to-file: %s"
3001 "only implemented for same method, same user, same host")))
3002 (with-parsed-tramp-file-name filename v1
3003 (with-parsed-tramp-file-name newname v2
3004 (let ((ln (when v1
(tramp-get-remote-ln v1
))))
3005 (when (and (not ok-if-already-exists
)
3006 (file-exists-p newname
)
3007 (not (numberp ok-if-already-exists
))
3010 "File %s already exists; make it a new name anyway? "
3014 "add-name-to-file: file %s already exists" newname
))
3015 (tramp-flush-file-property v2 v2-localname
)
3016 (tramp-barf-unless-okay
3018 (format "%s %s %s" ln
(tramp-shell-quote-argument v1-localname
)
3019 (tramp-shell-quote-argument v2-localname
))
3020 "error with add-name-to-file, see buffer `%s' for details"
3023 (defun tramp-handle-copy-file
3024 (filename newname
&optional ok-if-already-exists keep-date preserve-uid-gid
)
3025 "Like `copy-file' for Tramp files."
3026 ;; Check if both files are local -- invoke normal copy-file.
3027 ;; Otherwise, use Tramp from local system.
3028 (setq filename
(expand-file-name filename
))
3029 (setq newname
(expand-file-name newname
))
3031 ;; At least one file a Tramp file?
3032 ((or (tramp-tramp-file-p filename
)
3033 (tramp-tramp-file-p newname
))
3034 (tramp-do-copy-or-rename-file
3035 'copy filename newname ok-if-already-exists keep-date preserve-uid-gid
))
3038 (tramp-run-real-handler
3040 (list filename newname ok-if-already-exists keep-date preserve-uid-gid
)))
3042 (tramp-run-real-handler
3043 'copy-file
(list filename newname ok-if-already-exists keep-date
)))))
3045 (defun tramp-handle-rename-file
3046 (filename newname
&optional ok-if-already-exists
)
3047 "Like `rename-file' for Tramp files."
3048 ;; Check if both files are local -- invoke normal rename-file.
3049 ;; Otherwise, use Tramp from local system.
3050 (setq filename
(expand-file-name filename
))
3051 (setq newname
(expand-file-name newname
))
3052 ;; At least one file a Tramp file?
3053 (if (or (tramp-tramp-file-p filename
)
3054 (tramp-tramp-file-p newname
))
3055 (tramp-do-copy-or-rename-file
3056 'rename filename newname ok-if-already-exists t t
)
3057 (tramp-run-real-handler
3058 'rename-file
(list filename newname ok-if-already-exists
))))
3060 (defun tramp-do-copy-or-rename-file
3061 (op filename newname
&optional ok-if-already-exists keep-date preserve-uid-gid
)
3062 "Copy or rename a remote file.
3063 OP must be `copy' or `rename' and indicates the operation to perform.
3064 FILENAME specifies the file to copy or rename, NEWNAME is the name of
3065 the new file (for copy) or the new name of the file (for rename).
3066 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
3067 KEEP-DATE means to make sure that NEWNAME has the same timestamp
3068 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3069 the uid and gid if both files are on the same host.
3071 This function is invoked by `tramp-handle-copy-file' and
3072 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
3073 and `rename'. FILENAME and NEWNAME must be absolute file names."
3074 (unless (memq op
'(copy rename
))
3075 (error "Unknown operation `%s', must be `copy' or `rename'" op
))
3076 (let ((t1 (tramp-tramp-file-p filename
))
3077 (t2 (tramp-tramp-file-p newname
)))
3079 (when (and (not ok-if-already-exists
) (file-exists-p newname
))
3080 (with-parsed-tramp-file-name (if t1 filename newname
) nil
3082 v
'file-already-exists
"File %s already exists" newname
)))
3086 ;; Both are Tramp files.
3088 (with-parsed-tramp-file-name filename v1
3089 (with-parsed-tramp-file-name newname v2
3091 ;; Shortcut: if method, host, user are the same for both
3092 ;; files, we invoke `cp' or `mv' on the remote host
3094 ((tramp-equal-remote filename newname
)
3095 (tramp-do-copy-or-rename-file-directly
3097 ok-if-already-exists keep-date preserve-uid-gid
))
3099 ;; If both source and target are Tramp files,
3100 ;; both are using the same copy-program, then we
3101 ;; can invoke rcp directly. Note that
3102 ;; default-directory should point to a local
3103 ;; directory if we want to invoke rcp.
3104 ((and (equal v1-method v2-method
)
3105 (tramp-method-out-of-band-p v1
)
3106 (> (nth 7 (file-attributes filename
))
3107 tramp-copy-size-limit
))
3108 (tramp-do-copy-or-rename-file-out-of-band
3109 op filename newname keep-date
))
3111 ;; No shortcut was possible. So we copy the
3112 ;; file first. If the operation was `rename', we go
3113 ;; back and delete the original file (if the copy was
3114 ;; successful). The approach is simple-minded: we
3115 ;; create a new buffer, insert the contents of the
3116 ;; source file into it, then write out the buffer to
3117 ;; the target file. The advantage is that it doesn't
3118 ;; matter which filename handlers are used for the
3119 ;; source and target file.
3121 (tramp-do-copy-or-rename-file-via-buffer
3122 op filename newname keep-date
))))))
3124 ;; One file is a Tramp file, the other one is local.
3126 (with-parsed-tramp-file-name (if t1 filename newname
) nil
3128 ;; Fast track on local machine.
3129 ((tramp-local-host-p v
)
3130 (tramp-do-copy-or-rename-file-directly
3132 ok-if-already-exists keep-date preserve-uid-gid
))
3134 ;; If the Tramp file has an out-of-band method, the corresponding
3135 ;; copy-program can be invoked.
3136 ((and (tramp-method-out-of-band-p v
)
3137 (> (nth 7 (file-attributes filename
))
3138 tramp-copy-size-limit
))
3139 (tramp-do-copy-or-rename-file-out-of-band
3140 op filename newname keep-date
))
3142 ;; Use the inline method via a Tramp buffer.
3143 (t (tramp-do-copy-or-rename-file-via-buffer
3144 op filename newname keep-date
)))))
3147 ;; One of them must be a Tramp file.
3148 (error "Tramp implementation says this cannot happen")))
3150 ;; In case of `rename', we must flush the cache of the source file.
3151 (when (and t1
(eq op
'rename
))
3152 (with-parsed-tramp-file-name filename nil
3153 (tramp-flush-file-property v localname
)))
3155 ;; When newname did exist, we have wrong cached values.
3157 (with-parsed-tramp-file-name newname nil
3158 (tramp-flush-file-property v localname
))))))
3160 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date
)
3161 "Use an Emacs buffer to copy or rename a file.
3162 First arg OP is either `copy' or `rename' and indicates the operation.
3163 FILENAME is the source file, NEWNAME the target file.
3164 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
3166 ;; We must disable multibyte, because binary data shall not be
3168 (set-buffer-multibyte nil
)
3169 (let ((coding-system-for-read 'binary
)
3170 (jka-compr-inhibit t
))
3171 (insert-file-contents-literally filename
))
3172 ;; We don't want the target file to be compressed, so we let-bind
3173 ;; `jka-compr-inhibit' to t.
3174 (let ((coding-system-for-write 'binary
)
3175 (jka-compr-inhibit t
))
3176 (write-region (point-min) (point-max) newname
)))
3177 ;; KEEP-DATE handling.
3178 (when keep-date
(set-file-times newname
(nth 5 (file-attributes filename
))))
3180 (set-file-modes newname
(file-modes filename
))
3181 ;; If the operation was `rename', delete the original file.
3182 (unless (eq op
'copy
) (delete-file filename
)))
3184 (defun tramp-do-copy-or-rename-file-directly
3185 (op filename newname ok-if-already-exists keep-date preserve-uid-gid
)
3186 "Invokes `cp' or `mv' on the remote system.
3187 OP must be one of `copy' or `rename', indicating `cp' or `mv',
3188 respectively. FILENAME specifies the file to copy or rename,
3189 NEWNAME is the name of the new file (for copy) or the new name of
3190 the file (for rename). Both files must reside on the same host.
3191 KEEP-DATE means to make sure that NEWNAME has the same timestamp
3192 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3193 the uid and gid from FILENAME."
3194 (let ((t1 (tramp-tramp-file-p filename
))
3195 (t2 (tramp-tramp-file-p newname
)))
3196 (with-parsed-tramp-file-name (if t1 filename newname
) nil
3197 (let* ((cmd (cond ((and (eq op
'copy
) preserve-uid-gid
) "cp -f -p")
3198 ((eq op
'copy
) "cp -f")
3199 ((eq op
'rename
) "mv -f")
3202 "Unknown operation `%s', must be `copy' or `rename'"
3205 (if t1
(tramp-handle-file-remote-p filename
'localname
) filename
))
3207 (if t2
(tramp-handle-file-remote-p newname
'localname
) newname
))
3208 (prefix (file-remote-p (if t1 filename newname
))))
3211 ;; Both files are on a remote host, with same user.
3215 (format "%s %s %s" cmd
3216 (tramp-shell-quote-argument localname1
)
3217 (tramp-shell-quote-argument localname2
)))
3218 (with-current-buffer (tramp-get-buffer v
)
3219 (goto-char (point-min))
3223 ;; Mask cp -f error.
3225 tramp-operation-not-permitted-regexp nil t
))
3226 (zerop (tramp-send-command-and-check v nil
)))
3227 (tramp-error-with-buffer
3229 "Copying directly failed, see buffer `%s' for details."
3232 ;; We are on the local host.
3235 ;; We can do it directly.
3236 ((let (file-name-handler-alist)
3237 (and (file-readable-p localname1
)
3238 (file-writable-p (file-name-directory localname2
))
3239 (or (file-directory-p localname2
)
3240 (file-writable-p localname2
))))
3242 (tramp-compat-copy-file
3243 localname1 localname2 ok-if-already-exists
3244 keep-date preserve-uid-gid
)
3245 (tramp-run-real-handler
3246 'rename-file
(list localname1 localname2 ok-if-already-exists
))))
3248 ;; We can do it directly with `tramp-send-command'
3249 ((let (file-name-handler-alist)
3250 (and (file-readable-p (concat prefix localname1
))
3252 (file-name-directory (concat prefix localname2
)))))
3253 (tramp-do-copy-or-rename-file-directly
3254 op
(concat prefix localname1
) (concat prefix localname2
)
3255 ok-if-already-exists keep-date t
)
3256 ;; We must change the ownership to the local user.
3257 (tramp-set-file-uid-gid
3258 (concat prefix localname2
)
3259 (tramp-get-local-uid 'integer
)
3260 (tramp-get-local-gid 'integer
)))
3262 ;; We need a temporary file in between.
3264 ;; Create the temporary file.
3265 (let ((tmpfile (tramp-compat-make-temp-file localname1
)))
3273 (tramp-shell-quote-argument localname1
)
3274 (tramp-shell-quote-argument tmpfile
)))
3275 ;; We must change the ownership as remote user.
3276 (tramp-set-file-uid-gid
3277 (concat prefix tmpfile
)
3278 (tramp-get-local-uid 'integer
)
3279 (tramp-get-local-gid 'integer
)))
3282 (tramp-compat-copy-file
3283 localname1 tmpfile ok-if-already-exists
3284 keep-date preserve-uid-gid
)
3285 (tramp-run-real-handler
3287 (list localname1 tmpfile ok-if-already-exists
)))
3288 ;; We must change the ownership as local user.
3289 (tramp-set-file-uid-gid
3291 (tramp-get-remote-uid v
'integer
)
3292 (tramp-get-remote-gid v
'integer
))))
3294 ;; Move the temporary file to its destination.
3300 (tramp-shell-quote-argument tmpfile
)
3301 (tramp-shell-quote-argument localname2
))))
3303 (tramp-run-real-handler
3305 (list tmpfile localname2 ok-if-already-exists
)))))
3309 (delete-file tmpfile
)
3310 (signal (car err
) (cdr err
))))))))))
3312 ;; Set the time and mode. Mask possible errors.
3313 ;; Won't be applied for 'rename.
3315 (when (and keep-date
(not preserve-uid-gid
))
3316 (set-file-times newname
(nth 5 (file-attributes filename
)))
3317 (set-file-modes newname
(file-modes filename
)))
3320 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date
)
3321 "Invoke rcp program to copy.
3322 One of FILENAME and NEWNAME must be a Tramp name, the other must
3323 be a local filename. The method used must be an out-of-band method."
3324 (let ((t1 (tramp-tramp-file-p filename
))
3325 (t2 (tramp-tramp-file-p newname
))
3326 copy-program copy-args copy-keep-date port spec
3329 (with-parsed-tramp-file-name (if t1 filename newname
) nil
3331 ;; Expand hops. Might be necessary for gateway methods.
3332 (setq v
(car (tramp-compute-multi-hops v
)))
3333 (aset v
3 localname
)
3335 ;; Check which ones of source and target are Tramp files.
3336 (setq source
(if t1
(tramp-make-copy-program-file-name v
) filename
)
3337 target
(if t2
(tramp-make-copy-program-file-name v
) newname
))
3339 ;; Check for port number. Until now, there's no need for handling
3340 ;; like method, user, host.
3341 (setq host
(tramp-file-name-real-host v
)
3342 port
(tramp-file-name-port v
)
3343 port
(or (and port
(number-to-string port
)) ""))
3345 ;; Compose copy command.
3346 (setq spec
`((?h .
,host
) (?u .
,user
) (?p .
,port
)
3347 (?t .
,(tramp-get-connection-property
3348 (tramp-get-connection-process v
) "temp-file" ""))
3349 (?k .
,(if keep-date
" " "")))
3350 copy-program
(tramp-get-method-parameter
3351 method
'tramp-copy-program
)
3352 copy-keep-date
(tramp-get-method-parameter
3353 method
'tramp-copy-keep-date
)
3360 ;; " " is indication for keep-date argument.
3361 x
(delete " " (mapcar '(lambda (y) (format-spec y spec
)) x
)))
3362 (unless (member "" x
) (mapconcat 'identity x
" ")))
3363 (tramp-get-method-parameter method
'tramp-copy-args
))))
3365 ;; Check for program.
3366 (when (and (fboundp 'executable-find
)
3367 (not (let ((default-directory
3368 (tramp-compat-temporary-file-directory)))
3369 (executable-find copy-program
))))
3371 v
'file-error
"Cannot find copy program: %s" copy-program
))
3373 (tramp-message v
0 "Transferring %s to %s..." filename newname
)
3377 ;; The default directory must be remote.
3378 (let ((default-directory
3379 (file-name-directory (if t1 filename newname
))))
3380 ;; Set the transfer process properties.
3381 (tramp-set-connection-property
3382 v
"process-name" (buffer-name (current-buffer)))
3383 (tramp-set-connection-property
3384 v
"process-buffer" (current-buffer))
3386 ;; Use an asynchronous process. By this, password can
3387 ;; be handled. The default directory must be local, in
3388 ;; order to apply the correct `copy-program'. We don't
3389 ;; set a timeout, because the copying of large files can
3390 ;; last longer than 60 secs.
3391 (let ((p (let ((default-directory
3392 (tramp-compat-temporary-file-directory)))
3393 (apply 'start-process
3394 (tramp-get-connection-property
3395 v
"process-name" nil
)
3396 (tramp-get-connection-property
3397 v
"process-buffer" nil
)
3399 (append copy-args
(list source target
))))))
3401 v
6 "%s" (mapconcat 'identity
(process-command p
) " "))
3402 (tramp-set-process-query-on-exit-flag p nil
)
3403 (tramp-process-actions p v tramp-actions-copy-out-of-band
))))
3405 ;; Reset the transfer process properties.
3406 (tramp-set-connection-property v
"process-name" nil
)
3407 (tramp-set-connection-property v
"process-buffer" nil
))
3409 (tramp-message v
0 "Transferring %s to %s...done" filename newname
)
3411 ;; Handle KEEP-DATE argument.
3412 (when (and keep-date
(not copy-keep-date
))
3413 (set-file-times newname
(nth 5 (file-attributes filename
))))
3416 (unless (and keep-date copy-keep-date
)
3417 (set-file-modes newname
(file-modes filename
))))
3419 ;; If the operation was `rename', delete the original file.
3420 (unless (eq op
'copy
)
3421 (delete-file filename
))))
3423 (defun tramp-handle-make-directory (dir &optional parents
)
3424 "Like `make-directory' for Tramp files."
3425 (setq dir
(expand-file-name dir
))
3426 (with-parsed-tramp-file-name dir nil
3428 (tramp-barf-unless-okay
3431 (if parents
"mkdir -p" "mkdir")
3432 (tramp-shell-quote-argument localname
))
3433 "Couldn't make directory %s" dir
))))
3435 (defun tramp-handle-delete-directory (directory)
3436 "Like `delete-directory' for Tramp files."
3437 (setq directory
(expand-file-name directory
))
3438 (with-parsed-tramp-file-name directory nil
3439 (tramp-flush-directory-property v localname
)
3440 (unless (zerop (tramp-send-command-and-check
3442 (format "rmdir %s" (tramp-shell-quote-argument localname
))))
3443 (tramp-error v
'file-error
"Couldn't delete %s" directory
))))
3445 (defun tramp-handle-delete-file (filename)
3446 "Like `delete-file' for Tramp files."
3447 (setq filename
(expand-file-name filename
))
3448 (with-parsed-tramp-file-name filename nil
3449 (tramp-flush-file-property v localname
)
3450 (unless (zerop (tramp-send-command-and-check
3453 (tramp-shell-quote-argument localname
))))
3454 (tramp-error v
'file-error
"Couldn't delete %s" filename
))))
3458 ;; CCC: This does not seem to be enough. Something dies when
3459 ;; we try and delete two directories under Tramp :/
3460 (defun tramp-handle-dired-recursive-delete-directory (filename)
3461 "Recursively delete the directory given.
3462 This is like `dired-recursive-delete-directory' for Tramp files."
3463 (with-parsed-tramp-file-name filename nil
3464 (tramp-flush-directory-property v filename
)
3465 ;; Run a shell command 'rm -r <localname>'
3466 ;; Code shamelessly stolen for the dired implementation and, um, hacked :)
3467 (unless (file-exists-p filename
)
3468 (tramp-error v
'file-error
"No such directory: %s" filename
))
3469 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
3472 (format "rm -rf %s" (tramp-shell-quote-argument localname
))
3473 ;; Don't read the output, do it explicitely.
3475 ;; Wait for the remote system to return to us...
3476 ;; This might take a while, allow it plenty of time.
3477 (tramp-wait-for-output (tramp-get-connection-process v
) 120)
3478 ;; Make sure that it worked...
3479 (and (file-exists-p filename
)
3481 v
'file-error
"Failed to recursively delete %s" filename
))))
3483 (defun tramp-handle-dired-compress-file (file &rest ok-flag
)
3484 "Like `dired-compress-file' for Tramp files."
3485 ;; OK-FLAG is valid for XEmacs only, but not implemented.
3486 ;; Code stolen mainly from dired-aux.el.
3487 (with-parsed-tramp-file-name file nil
3488 (tramp-flush-file-property v localname
)
3491 (if (not (featurep 'xemacs
))
3493 (symbol-value 'dired-compress-file-suffixes
)
3494 ;; XEmacs has `dired-compression-method-alist', which is
3495 ;; transformed into `dired-compress-file-suffixes' structure.
3498 (list (concat (regexp-quote (nth 1 x
)) "\\'")
3500 (mapconcat 'identity
(nth 3 x
) " ")))
3501 (symbol-value 'dired-compression-method-alist
))))
3503 ;; See if any suffix rule matches this file name.
3505 (let (case-fold-search)
3506 (if (string-match (car (car suffixes
)) localname
)
3507 (setq suffix
(car suffixes
) suffixes nil
))
3508 (setq suffixes
(cdr suffixes
))))
3510 (cond ((file-symlink-p file
)
3512 ((and suffix
(nth 2 suffix
))
3513 ;; We found an uncompression rule.
3514 (tramp-message v
0 "Uncompressing %s..." file
)
3515 (when (zerop (tramp-send-command-and-check
3516 v
(concat (nth 2 suffix
) " " localname
)))
3517 (tramp-message v
0 "Uncompressing %s...done" file
)
3518 ;; `dired-remove-file' is not defined in XEmacs
3519 (funcall (symbol-function 'dired-remove-file
) file
)
3520 (string-match (car suffix
) file
)
3521 (concat (substring file
0 (match-beginning 0)))))
3523 ;; We don't recognize the file as compressed, so compress it.
3525 (tramp-message v
0 "Compressing %s..." file
)
3526 (when (zerop (tramp-send-command-and-check
3527 v
(concat "gzip -f " localname
)))
3528 (tramp-message v
0 "Compressing %s...done" file
)
3529 ;; `dired-remove-file' is not defined in XEmacs
3530 (funcall (symbol-function 'dired-remove-file
) file
)
3531 (cond ((file-exists-p (concat file
".gz"))
3532 (concat file
".gz"))
3533 ((file-exists-p (concat file
".z"))
3537 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
3538 ;; not sure at all that this is the right way to do it, but let's hope
3539 ;; it works for now, and wait for a guru to point out the Right Way to
3541 ;;(eval-when-compile
3542 ;; (unless (fboundp 'dired-insert-set-properties)
3543 ;; (fset 'dired-insert-set-properties 'ignore)))
3544 ;; Gerd suggests this:
3545 (eval-when-compile (require 'dired
))
3546 ;; Note that dired is required at run-time, too, when it is needed.
3547 ;; It is only needed on XEmacs for the function
3548 ;; `dired-insert-set-properties'.
3550 (defun tramp-handle-insert-directory
3551 (filename switches
&optional wildcard full-directory-p
)
3552 "Like `insert-directory' for Tramp files."
3553 (setq filename
(expand-file-name filename
))
3554 (with-parsed-tramp-file-name filename nil
3555 (tramp-flush-file-property v localname
)
3556 (if (and (featurep 'ls-lisp
)
3557 (not (symbol-value 'ls-lisp-use-insert-directory-program
)))
3558 (tramp-run-real-handler
3559 'insert-directory
(list filename switches wildcard full-directory-p
))
3560 ;; For the moment, we assume that the remote "ls" program does not
3561 ;; grok "--dired". In the future, we should detect this on
3562 ;; connection setup.
3563 (when (string-match "^--dired\\s-+" switches
)
3564 (setq switches
(replace-match "" nil t switches
)))
3566 v
4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
3567 switches filename
(if wildcard
"yes" "no")
3568 (if full-directory-p
"yes" "no"))
3570 (setq wildcard
(tramp-run-real-handler
3571 'file-name-nondirectory
(list localname
)))
3572 (setq localname
(tramp-run-real-handler
3573 'file-name-directory
(list localname
))))
3574 (when (listp switches
)
3575 (setq switches
(mapconcat 'identity switches
" ")))
3576 (unless full-directory-p
3577 (setq switches
(concat "-d " switches
)))
3579 (setq switches
(concat switches
" " wildcard
)))
3580 ;; If `full-directory-p', we just say `ls -l FILENAME'.
3581 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
3582 (if full-directory-p
3586 (tramp-get-ls-command v
)
3590 (tramp-shell-quote-argument (concat localname
".")))))
3591 (tramp-barf-unless-okay
3593 (format "cd %s" (tramp-shell-quote-argument
3594 (tramp-run-real-handler
3595 'file-name-directory
(list localname
))))
3597 (tramp-shell-quote-argument
3598 (tramp-run-real-handler 'file-name-directory
(list localname
))))
3602 (tramp-get-ls-command v
)
3606 (tramp-run-real-handler
3607 'file-name-nondirectory
(list localname
)))))
3609 (tramp-shell-quote-argument
3610 (tramp-run-real-handler
3611 'file-name-nondirectory
(list localname
)))))))
3612 ;; We cannot use `insert-buffer-substring' because the Tramp buffer
3613 ;; changes its contents before insertion due to calling
3614 ;; `expand-file' and alike.
3616 (with-current-buffer (tramp-get-buffer v
)
3617 (buffer-string))))))
3619 (defun tramp-handle-unhandled-file-name-directory (filename)
3620 "Like `unhandled-file-name-directory' for Tramp files."
3621 ;; With Emacs 23, we could simply return `nil'. But we must keep it
3622 ;; for backward compatibility.
3623 (expand-file-name "~/"))
3625 ;; Canonicalization of file names.
3627 (defun tramp-handle-expand-file-name (name &optional dir
)
3628 "Like `expand-file-name' for Tramp files.
3629 If the localname part of the given filename starts with \"/../\" then
3630 the result will be a local, non-Tramp, filename."
3631 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
3632 (setq dir
(or dir default-directory
"/"))
3633 ;; Unless NAME is absolute, concat DIR and NAME.
3634 (unless (file-name-absolute-p name
)
3635 (setq name
(concat (file-name-as-directory dir
) name
)))
3636 ;; If NAME is not a Tramp file, run the real handler.
3637 (if (not (tramp-tramp-file-p name
))
3638 (tramp-run-real-handler 'expand-file-name
(list name nil
))
3640 (with-parsed-tramp-file-name name nil
3641 (unless (tramp-run-real-handler 'file-name-absolute-p
(list localname
))
3642 (setq localname
(concat "~/" localname
)))
3643 ;; Tilde expansion if necessary. This needs a shell which
3644 ;; groks tilde expansion! The function `tramp-find-shell' is
3645 ;; supposed to find such a shell on the remote host. Please
3646 ;; tell me about it when this doesn't work on your system.
3647 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname
)
3648 (let ((uname (match-string 1 localname
))
3649 (fname (match-string 2 localname
)))
3650 ;; We cannot simply apply "~/", because under sudo "~/" is
3651 ;; expanded to the local user home directory but to the
3652 ;; root home directory. On the other hand, using always
3653 ;; the default user name for tilde expansion is not
3654 ;; appropriate either, because ssh and companions might
3655 ;; use a user name from the config file.
3656 (when (and (string-equal uname
"~")
3657 (string-match "\\`su\\(do\\)?\\'" method
))
3658 (setq uname
(concat uname user
)))
3660 (with-connection-property v uname
3661 (tramp-send-command v
(format "cd %s; pwd" uname
))
3662 (with-current-buffer (tramp-get-buffer v
)
3663 (goto-char (point-min))
3664 (buffer-substring (point) (tramp-compat-line-end-position)))))
3665 (setq localname
(concat uname fname
))))
3666 ;; There might be a double slash, for example when "~/"
3667 ;; expands to "/". Remove this.
3668 (while (string-match "//" localname
)
3669 (setq localname
(replace-match "/" t t localname
)))
3670 ;; No tilde characters in file name, do normal
3671 ;; expand-file-name (this does "/./" and "/../"). We bind
3672 ;; `directory-sep-char' here for XEmacs on Windows, which
3673 ;; would otherwise use backslash. `default-directory' is
3674 ;; bound, because on Windows there would be problems with UNC
3675 ;; shares or Cygwin mounts.
3676 (let ((directory-sep-char ?
/)
3677 (default-directory (tramp-compat-temporary-file-directory)))
3678 (tramp-make-tramp-file-name
3680 (tramp-drop-volume-letter
3681 (tramp-run-real-handler
3682 'expand-file-name
(list localname
))))))))
3684 (defun tramp-replace-environment-variables (filename)
3685 "Replace environment variables in FILENAME.
3686 Return the string with the replaced variables."
3688 (let ((idx (string-match "$\\w+" filename
)))
3689 ;; `$' is coded as `$$'.
3690 (when (and idx
(or (zerop idx
) (not (eq ?$
(aref filename
(1- idx
))))))
3693 (substitute-in-file-name (match-string 0 filename
))
3697 (defun tramp-handle-substitute-in-file-name (filename)
3698 "Like `substitute-in-file-name' for Tramp files.
3699 \"//\" and \"/~\" substitute only in the local filename part.
3700 If the URL Tramp syntax is chosen, \"//\" as method delimeter and \"/~\" at
3701 beginning of local filename are not substituted."
3702 ;; First, we must replace environment variables.
3703 (setq filename
(tramp-replace-environment-variables filename
))
3704 (with-parsed-tramp-file-name filename nil
3705 (if (equal tramp-syntax
'url
)
3706 ;; We need to check localname only. The other parts cannot contain
3708 (if (and (> (length localname
) 1)
3709 (or (string-match "//" localname
)
3710 (string-match "/~" localname
1)))
3711 (tramp-run-real-handler 'substitute-in-file-name
(list filename
))
3712 (tramp-make-tramp-file-name
3713 (when method
(substitute-in-file-name method
))
3714 (when user
(substitute-in-file-name user
))
3715 (when host
(substitute-in-file-name host
))
3717 (tramp-run-real-handler
3718 'substitute-in-file-name
(list localname
)))))
3719 ;; Ignore in LOCALNAME everything before "//" or "/~".
3720 (when (and (stringp localname
) (string-match ".+?/\\(/\\|~\\)" localname
))
3722 (concat (file-remote-p filename
)
3723 (replace-match "\\1" nil nil localname
)))
3724 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
3725 (when (string-match "~$" filename
)
3726 (setq filename
(concat filename
"/"))))
3727 (tramp-run-real-handler 'substitute-in-file-name
(list filename
)))))
3729 ;; In XEmacs, electricity is implemented via a key map for ?/ and ?~,
3730 ;; which calls corresponding functions (see minibuf.el).
3731 (when (fboundp 'minibuffer-electric-separator
)
3736 (around ,(intern (format "tramp-advice-%s" x
)) activate
)
3737 "Invoke `substitute-in-file-name' for Tramp files."
3738 (if (and (symbol-value 'minibuffer-electric-file-name-behavior
)
3739 (tramp-tramp-file-p (buffer-substring)))
3740 ;; We don't need to handle `last-input-event', because
3741 ;; due to the key map we know it must be ?/ or ?~.
3742 (let ((s (concat (buffer-substring (point-min) (point))
3743 (string last-command-char
))))
3744 (delete-region (point-min) (point))
3745 (insert (substitute-in-file-name s
))
3746 (setq ad-return-value last-command-char
))
3749 '(minibuffer-electric-separator
3750 minibuffer-electric-tilde
)))
3753 ;;; Remote commands:
3755 (defun tramp-handle-executable-find (command)
3756 "Like `executable-find' for Tramp files."
3757 (with-parsed-tramp-file-name default-directory nil
3758 (tramp-find-executable v command
(tramp-get-remote-path v
) t
)))
3760 ;; We use BUFFER also as connection buffer during setup. Because of
3761 ;; this, its original contents must be saved, and restored once
3762 ;; connection has been setup.
3763 (defun tramp-handle-start-file-process (name buffer program
&rest args
)
3764 "Like `start-file-process' for Tramp files."
3765 (with-parsed-tramp-file-name default-directory nil
3770 ;; BUFFER can be nil. We use a temporary buffer.
3771 (setq buffer
(generate-new-buffer tramp-temp-buffer-name
)))
3772 (while (get-process name1
)
3773 ;; NAME must be unique as process name.
3775 name1
(format "%s<%d>" name i
)))
3777 ;; Set the new process properties.
3778 (tramp-set-connection-property v
"process-name" name
)
3779 (tramp-set-connection-property v
"process-buffer" buffer
)
3780 ;; Activate narrowing in order to save BUFFER contents.
3781 ;; Clear also the modification time; otherwise we might be
3782 ;; interrupted by `verify-visited-file-modtime'.
3783 (with-current-buffer (tramp-get-connection-buffer v
)
3784 (clear-visited-file-modtime)
3785 (narrow-to-region (point-max) (point-max)))
3786 ;; Goto working directory. `tramp-send-command' opens a new
3789 v
(format "cd %s" (tramp-shell-quote-argument localname
)))
3790 ;; Send the command.
3794 (mapconcat 'tramp-shell-quote-argument
3795 (cons program args
) " "))
3797 ;; Set query flag for this process.
3798 (tramp-set-process-query-on-exit-flag
3799 (tramp-get-connection-process v
) t
)
3801 (tramp-get-connection-process v
))
3803 (with-current-buffer (tramp-get-connection-buffer v
)
3804 (if (string-match tramp-temp-buffer-name
(buffer-name))
3806 (set-process-buffer (tramp-get-connection-process v
) nil
)
3807 (kill-buffer (current-buffer)))
3809 (goto-char (point-max))))
3810 (tramp-set-connection-property v
"process-name" nil
)
3811 (tramp-set-connection-property v
"process-buffer" nil
))))
3813 (defun tramp-handle-process-file
3814 (program &optional infile destination display
&rest args
)
3815 "Like `process-file' for Tramp files."
3816 ;; The implementation is not complete yet.
3817 (when (and (numberp destination
) (zerop destination
))
3818 (error "Implementation does not handle immediate return"))
3820 (with-parsed-tramp-file-name default-directory nil
3821 (let (command input tmpinput stderr tmpstderr outbuf ret
)
3823 (setq command
(mapconcat 'tramp-shell-quote-argument
3824 (cons program args
) " "))
3827 (setq input
"/dev/null")
3828 (setq infile
(expand-file-name infile
))
3829 (if (tramp-equal-remote default-directory infile
)
3830 ;; INFILE is on the same remote host.
3831 (setq input
(with-parsed-tramp-file-name infile nil localname
))
3832 ;; INFILE must be copied to remote host.
3833 (setq input
(tramp-make-tramp-temp-file v
)
3834 tmpinput
(tramp-make-tramp-file-name method user host input
))
3835 (copy-file infile tmpinput t
)))
3836 (when input
(setq command
(format "%s <%s" command input
)))
3838 ;; Determine output.
3841 ((bufferp destination
)
3842 (setq outbuf destination
))
3844 ((stringp destination
)
3845 (setq outbuf
(get-buffer-create destination
)))
3846 ;; (REAL-DESTINATION ERROR-DESTINATION)
3847 ((consp destination
)
3850 ((bufferp (car destination
))
3851 (setq outbuf
(car destination
)))
3852 ((stringp (car destination
))
3853 (setq outbuf
(get-buffer-create (car destination
))))
3855 (setq outbuf
(current-buffer))))
3858 ((stringp (cadr destination
))
3859 (setcar (cdr destination
) (expand-file-name (cadr destination
)))
3860 (if (tramp-equal-remote default-directory
(cadr destination
))
3861 ;; stderr is on the same remote host.
3862 (setq stderr
(with-parsed-tramp-file-name
3863 (cadr destination
) nil localname
))
3864 ;; stderr must be copied to remote host. The temporary
3865 ;; file must be deleted after execution.
3866 (setq stderr
(tramp-make-tramp-temp-file v
)
3867 tmpstderr
(tramp-make-tramp-file-name
3868 method user host stderr
))))
3869 ;; stderr to be discarded
3870 ((null (cadr destination
))
3871 (setq stderr
"/dev/null"))))
3874 (setq outbuf
(current-buffer))))
3875 (when stderr
(setq command
(format "%s 2>%s" command stderr
)))
3877 ;; Goto working directory.
3879 v
(format "cd %s" (tramp-shell-quote-argument localname
)))
3880 ;; Send the command. It might not return in time, so we protect it.
3883 (tramp-send-command v command
)
3884 ;; We should show the output anyway.
3886 (let ((output-string
3887 (with-current-buffer (tramp-get-connection-buffer v
)
3888 (buffer-substring (point-min) (point-max)))))
3889 (with-current-buffer outbuf
3890 (insert output-string
)))
3891 (when display
(display-buffer outbuf
))))
3892 ;; When the user did interrupt, we should do it also.
3894 (kill-buffer (tramp-get-connection-buffer v
))
3897 ;; Check return code.
3898 (unless ret
(setq ret
(tramp-send-command-and-check v nil
)))
3899 ;; Provide error file.
3900 (when tmpstderr
(rename-file tmpstderr
(cadr destination
) t
))
3902 (when tmpinput
(delete-file tmpinput
))
3903 ;; Return exit status.
3906 (defun tramp-local-call-process
3907 (program &optional infile destination display
&rest args
)
3908 "Calls `call-process' on the local host.
3909 This is needed because for some Emacs flavors Tramp has
3910 defadviced `call-process' to behave like `process-file'. The
3911 Lisp error raised when PROGRAM is nil is trapped also, returning 1."
3912 (let ((default-directory
3913 (if (file-remote-p default-directory
)
3914 (tramp-compat-temporary-file-directory)
3915 default-directory
)))
3916 (if (executable-find program
)
3917 (apply 'call-process program infile destination display args
)
3920 (defun tramp-handle-call-process-region
3921 (start end program
&optional delete buffer display
&rest args
)
3922 "Like `call-process-region' for Tramp files."
3923 (let ((tmpfile (tramp-compat-make-temp-file "")))
3924 (write-region start end tmpfile
)
3925 (when delete
(delete-region start end
))
3927 (apply 'call-process program tmpfile buffer display args
)
3928 (delete-file tmpfile
))))
3930 (defun tramp-handle-shell-command
3931 (command &optional output-buffer error-buffer
)
3932 "Like `shell-command' for Tramp files."
3933 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command
))
3934 ;; We cannot use `shell-file-name' and `shell-command-switch',
3935 ;; they are variables of the local host.
3936 (args (list "/bin/sh" "-c" (substring command
0 asynchronous
)))
3940 ((bufferp output-buffer
) output-buffer
)
3941 ((stringp output-buffer
) (get-buffer-create output-buffer
))
3943 (setq current-buffer-p t
)
3945 (t (get-buffer-create
3947 "*Async Shell Command*"
3948 "*Shell Command Output*")))))
3951 ((bufferp error-buffer
) error-buffer
)
3952 ((stringp error-buffer
) (get-buffer-create error-buffer
))))
3954 (if (and (not asynchronous
) error-buffer
)
3955 (with-parsed-tramp-file-name default-directory nil
3956 (list output-buffer
(tramp-make-tramp-temp-file v
)))
3958 (p (get-buffer-process output-buffer
)))
3960 ;; Check whether there is another process running. Tramp does not
3961 ;; support 2 (asynchronous) processes in parallel.
3963 (if (yes-or-no-p "A command is running. Kill it? ")
3967 (error "Shell command in progress")))
3969 (if current-buffer-p
3971 (barf-if-buffer-read-only)
3973 (with-current-buffer output-buffer
3974 (setq buffer-read-only nil
)
3977 (if (and (not current-buffer-p
) (integerp asynchronous
))
3980 (apply 'start-file-process
"*Async Shell*" buffer args
)
3982 (pop-to-buffer output-buffer
)
3983 (setq mode-line-process
'(":%s"))
3984 (require 'shell
) (shell-mode))
3988 (apply 'process-file
(car args
) nil buffer nil
(cdr args
))
3989 ;; Insert error messages if they were separated.
3990 (when (listp buffer
)
3991 (with-current-buffer error-buffer
3992 (insert-file-contents (cadr buffer
)))
3993 (delete-file (cadr buffer
)))
3994 (if current-buffer-p
3995 ;; This is like exchange-point-and-mark, but doesn't
3996 ;; activate the mark. It is cleaner to avoid activation,
3997 ;; even though the command loop would deactivate the mark
3998 ;; because we inserted text.
3999 (goto-char (prog1 (mark t
)
4000 (set-marker (mark-marker) (point)
4002 ;; There's some output, display it.
4003 (when (with-current-buffer output-buffer
(> (point-max) (point-min)))
4004 (if (functionp 'display-message-or-buffer
)
4005 (funcall (symbol-function 'display-message-or-buffer
)
4007 (pop-to-buffer output-buffer
))))))))
4011 (defvar tramp-handle-file-local-copy-hook nil
4012 "Normal hook to be run at the end of `tramp-handle-file-local-copy'.")
4014 (defun tramp-handle-file-local-copy (filename)
4015 "Like `file-local-copy' for Tramp files."
4017 (with-parsed-tramp-file-name filename nil
4018 (unless (file-exists-p filename
)
4021 "Cannot make local copy of non-existing file `%s'" filename
))
4023 (let ((rem-enc (tramp-get-remote-coding v
"remote-encoding"))
4024 (loc-dec (tramp-get-local-coding v
"local-decoding"))
4025 (tmpfile (tramp-compat-make-temp-file filename
)))
4029 ;; `copy-file' handles direct copy and out-of-band methods.
4030 ((or (tramp-local-host-p v
)
4031 (and (tramp-method-out-of-band-p v
)
4032 (> (nth 7 (file-attributes filename
))
4033 tramp-copy-size-limit
)))
4034 (copy-file filename tmpfile t t
))
4036 ;; Use inline encoding for file transfer.
4039 (tramp-message v
5 "Encoding remote file %s..." filename
)
4040 (tramp-barf-unless-okay
4042 (format "%s < %s" rem-enc
(tramp-shell-quote-argument localname
))
4043 "Encoding remote file failed")
4044 (tramp-message v
5 "Encoding remote file %s...done" filename
)
4046 (if (and (symbolp loc-dec
) (fboundp loc-dec
))
4047 ;; If local decoding is a function, we call it. We
4048 ;; must disable multibyte, because
4049 ;; `uudecode-decode-region' doesn't handle it
4052 (set-buffer-multibyte nil
)
4053 (insert-buffer-substring (tramp-get-buffer v
))
4055 v
5 "Decoding remote file %s with function %s..."
4057 (funcall loc-dec
(point-min) (point-max))
4058 (let ((coding-system-for-write 'binary
))
4059 (write-region (point-min) (point-max) tmpfile
)))
4061 ;; If tramp-decoding-function is not defined for this
4062 ;; method, we invoke tramp-decoding-command instead.
4063 (let ((tmpfile2 (tramp-compat-make-temp-file filename
)))
4064 (let ((coding-system-for-write 'binary
))
4065 (write-region (point-min) (point-max) tmpfile2
))
4067 v
5 "Decoding remote file %s with command %s..."
4070 (tramp-call-local-coding-command loc-dec tmpfile2 tmpfile
)
4071 (delete-file tmpfile2
))))
4073 (tramp-message v
5 "Decoding remote file %s...done" filename
)
4074 ;; Set proper permissions.
4075 (set-file-modes tmpfile
(file-modes filename
))
4076 ;; Set local user ownership.
4077 (tramp-set-file-uid-gid tmpfile
)))
4079 ;; Oops, I don't know what to do.
4081 v
'file-error
"Wrong method specification for `%s'" method
)))
4085 (delete-file tmpfile
)
4086 (signal (car err
) (cdr err
))))
4088 (run-hooks 'tramp-handle-file-local-copy-hook
)
4091 (defun tramp-handle-file-remote-p (filename &optional identification connected
)
4092 "Like `file-remote-p' for Tramp files."
4093 (when (tramp-tramp-file-p filename
)
4094 (with-parsed-tramp-file-name filename nil
4095 (and (or (not connected
)
4096 (let ((p (tramp-get-connection-process v
)))
4097 (and p
(processp p
) (memq (process-status p
) '(run open
)))))
4099 ((eq identification
'method
) method
)
4100 ((eq identification
'user
) user
)
4101 ((eq identification
'host
) host
)
4102 ((eq identification
'localname
) localname
)
4103 (t (tramp-make-tramp-file-name method user host
"")))))))
4105 (defun tramp-find-file-name-coding-system-alist (filename tmpname
)
4106 "Like `find-operation-coding-system' for Tramp filenames.
4107 Tramp's `insert-file-contents' and `write-region' work over
4108 temporary file names. If `file-coding-system-alist' contains an
4109 expression, which matches more than the file name suffix, the
4110 coding system might not be determined. This function repairs it."
4112 (dolist (elt file-coding-system-alist result
)
4113 (when (and (consp elt
) (string-match (car elt
) filename
))
4114 ;; We found a matching entry in `file-coding-system-alist'.
4115 ;; So we add a similar entry, but with the temporary file name
4118 'result
(cons (regexp-quote tmpname
) (cdr elt
)) 'append
)))))
4120 (defun tramp-handle-insert-file-contents
4121 (filename &optional visit beg end replace
)
4122 "Like `insert-file-contents' for Tramp files."
4123 (barf-if-buffer-read-only)
4124 (setq filename
(expand-file-name filename
))
4125 (let (coding-system-used result
)
4126 (with-parsed-tramp-file-name filename nil
4128 (if (not (file-exists-p filename
))
4131 (setq buffer-file-name filename
)
4132 (set-visited-file-modtime)
4133 (set-buffer-modified-p nil
))
4134 ;; We don't raise a Tramp error, because it might be
4135 ;; suppressed, like in `find-file-noselect-1'.
4136 (signal 'file-error
(list "File not found on remote host" filename
))
4137 (list (expand-file-name filename
) 0))
4139 (if (and (tramp-local-host-p v
)
4140 (let (file-name-handler-alist) (file-readable-p localname
)))
4141 ;; Short track: if we are on the local host, we can run directly.
4143 (tramp-run-real-handler
4144 'insert-file-contents
4145 (list localname visit beg end replace
)))
4147 ;; `insert-file-contents-literally' takes care to avoid calling
4148 ;; jka-compr. By let-binding inhibit-file-name-operation, we
4149 ;; propagate that care to the file-local-copy operation.
4151 (let ((inhibit-file-name-operation
4152 (when (eq inhibit-file-name-operation
4153 'insert-file-contents
)
4155 (file-local-copy filename
))))
4156 (tramp-message v
4 "Inserting local temp file `%s'..." local-copy
)
4157 ;; We must ensure that `file-coding-system-alist' matches
4160 (let ((file-coding-system-alist
4161 (tramp-find-file-name-coding-system-alist
4162 filename local-copy
)))
4164 (insert-file-contents local-copy nil beg end replace
))
4165 ;; Now `last-coding-system-used' has right value. Remember it.
4166 (when (boundp 'last-coding-system-used
)
4167 (setq coding-system-used
4168 (symbol-value 'last-coding-system-used
))))
4169 (delete-file local-copy
))
4171 v
4 "Inserting local temp file `%s'...done" local-copy
)
4172 (when (boundp 'last-coding-system-used
)
4173 (set 'last-coding-system-used coding-system-used
))))
4176 (setq buffer-read-only
(not (file-writable-p filename
)))
4177 (setq buffer-file-name filename
)
4178 (set-visited-file-modtime)
4179 (set-buffer-modified-p nil
))
4180 (list (expand-file-name filename
)
4183 ;; This is needed for XEmacs only. Code stolen from files.el.
4184 (defun tramp-handle-insert-file-contents-literally
4185 (filename &optional visit beg end replace
)
4186 "Like `insert-file-contents-literally' for Tramp files."
4187 (let ((format-alist nil
)
4188 (after-insert-file-functions nil
)
4189 (coding-system-for-read 'no-conversion
)
4190 (coding-system-for-write 'no-conversion
)
4191 (find-buffer-file-type-function
4192 (if (fboundp 'find-buffer-file-type
)
4193 (symbol-function 'find-buffer-file-type
)
4195 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler
))
4196 (inhibit-file-name-operation 'insert-file-contents
))
4199 (fset 'find-buffer-file-type
(lambda (filename) t
))
4200 (insert-file-contents filename visit beg end replace
))
4201 (if find-buffer-file-type-function
4202 (fset 'find-buffer-file-type find-buffer-file-type-function
)
4203 (fmakunbound 'find-buffer-file-type
)))))
4205 (defun tramp-handle-find-backup-file-name (filename)
4206 "Like `find-backup-file-name' for Tramp files."
4207 (with-parsed-tramp-file-name filename nil
4208 ;; We set both variables. It doesn't matter whether it is
4210 (let ((backup-directory-alist
4212 (when (boundp 'backup-directory-alist
)
4213 (if (boundp 'tramp-backup-directory-alist
)
4218 (if (and (stringp (cdr x
))
4219 (file-name-absolute-p (cdr x
))
4220 (not (tramp-file-name-p (cdr x
))))
4221 (tramp-make-tramp-file-name method user host
(cdr x
))
4223 (symbol-value 'tramp-backup-directory-alist
))
4224 (symbol-value 'backup-directory-alist
))))
4226 (bkup-backup-directory-info
4228 (when (boundp 'bkup-backup-directory-info
)
4229 (if (boundp 'tramp-bkup-backup-directory-info
)
4235 (if (and (stringp (car (cdr x
)))
4236 (file-name-absolute-p (car (cdr x
)))
4237 (not (tramp-file-name-p (car (cdr x
)))))
4238 (tramp-make-tramp-file-name
4239 method user host
(car (cdr x
)))
4242 (symbol-value 'tramp-bkup-backup-directory-info
))
4243 (symbol-value 'bkup-backup-directory-info
)))))
4245 (tramp-run-real-handler 'find-backup-file-name
(list filename
)))))
4247 (defun tramp-handle-make-auto-save-file-name ()
4248 "Like `make-auto-save-file-name' for Tramp files.
4249 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
4250 (let ((tramp-auto-save-directory tramp-auto-save-directory
)
4252 (tramp-subst-strs-in-string
4259 (buffer-file-name))))
4260 ;; File name must be unique. This is ensured with Emacs 22 (see
4261 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
4262 ;; all other cases we must do it ourselves.
4263 (when (boundp 'auto-save-file-name-transforms
)
4266 (when (and (string-match (car x
) buffer-file-name
)
4267 (not (car (cddr x
))))
4268 (setq tramp-auto-save-directory
4269 (or tramp-auto-save-directory
4270 (tramp-compat-temporary-file-directory)))))
4271 (symbol-value 'auto-save-file-name-transforms
)))
4272 ;; Create directory.
4273 (when tramp-auto-save-directory
4274 (setq buffer-file-name
4275 (expand-file-name buffer-file-name tramp-auto-save-directory
))
4276 (unless (file-exists-p tramp-auto-save-directory
)
4277 (make-directory tramp-auto-save-directory t
)))
4278 ;; Run plain `make-auto-save-file-name'. There might be an advice when
4279 ;; it is not a magic file name operation (since Emacs 22).
4280 ;; We must deactivate it temporarily.
4281 (if (not (ad-is-active 'make-auto-save-file-name
))
4282 (tramp-run-real-handler 'make-auto-save-file-name nil
)
4284 (ad-deactivate 'make-auto-save-file-name
)
4286 (tramp-run-real-handler 'make-auto-save-file-name nil
)
4287 (ad-activate 'make-auto-save-file-name
)))))
4289 (defvar tramp-handle-write-region-hook nil
4290 "Normal hook to be run at the end of `tramp-handle-write-region'.")
4292 ;; CCC grok APPEND, LOCKNAME
4293 (defun tramp-handle-write-region
4294 (start end filename
&optional append visit lockname confirm
)
4295 "Like `write-region' for Tramp files."
4296 (setq filename
(expand-file-name filename
))
4297 (with-parsed-tramp-file-name filename nil
4298 (unless (null append
)
4300 v
'file-error
"Cannot append to file using Tramp (`%s')" filename
))
4301 ;; Following part commented out because we don't know what to do about
4302 ;; file locking, and it does not appear to be a problem to ignore it.
4303 ;; Ange-ftp ignores it, too.
4304 ;; (when (and lockname (stringp lockname))
4305 ;; (setq lockname (expand-file-name lockname)))
4306 ;; (unless (or (eq lockname nil)
4307 ;; (string= lockname filename))
4309 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
4311 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
4312 (when (and (not (featurep 'xemacs
)) confirm
(file-exists-p filename
))
4313 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename
))
4314 (tramp-error v
'file-error
"File not overwritten")))
4316 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename
'integer
))
4317 (tramp-get-remote-uid v
'integer
)))
4318 (gid (or (nth 3 (tramp-compat-file-attributes filename
'integer
))
4319 (tramp-get-remote-gid v
'integer
))))
4321 (if (and (tramp-local-host-p v
)
4322 ;; `file-writable-p' calls 'file-expand-file-name'. We
4323 ;; cannot use `tramp-run-real-handler' therefore.
4324 (let (file-name-handler-alist)
4326 (file-writable-p (file-name-directory localname
))
4327 (or (file-directory-p localname
)
4328 (file-writable-p localname
)))))
4329 ;; Short track: if we are on the local host, we can run directly.
4330 (tramp-run-real-handler
4332 (list start end localname append
'no-message lockname confirm
))
4334 (let ((rem-dec (tramp-get-remote-coding v
"remote-decoding"))
4335 (loc-enc (tramp-get-local-coding v
"local-encoding"))
4336 (modes (save-excursion (file-modes filename
)))
4337 ;; We use this to save the value of
4338 ;; `last-coding-system-used' after writing the tmp file.
4339 ;; At the end of the function, we set
4340 ;; `last-coding-system-used' to this saved value. This
4341 ;; way, any intermediary coding systems used while
4342 ;; talking to the remote shell or suchlike won't hose
4343 ;; this variable. This approach was snarfed from
4346 ;; Write region into a tmp file. This isn't really
4347 ;; needed if we use an encoding function, but currently
4348 ;; we use it always because this makes the logic
4350 (tmpfile (tramp-compat-make-temp-file filename
)))
4352 ;; We say `no-message' here because we don't want the
4353 ;; visited file modtime data to be clobbered from the temp
4354 ;; file. We call `set-visited-file-modtime' ourselves later
4355 ;; on. We must ensure that `file-coding-system-alist'
4356 ;; matches `tmpfile'.
4357 (let ((file-coding-system-alist
4358 (tramp-find-file-name-coding-system-alist filename tmpfile
)))
4360 (tramp-run-real-handler
4362 (list start end tmpfile append
'no-message lockname confirm
))
4364 (delete-file tmpfile
)
4365 (signal (car err
) (cdr err
))))
4367 ;; Now, `last-coding-system-used' has the right value. Remember it.
4368 (when (boundp 'last-coding-system-used
)
4369 (setq coding-system-used
4370 (symbol-value 'last-coding-system-used
))))
4372 ;; The permissions of the temporary file should be set. If
4373 ;; filename does not exist (eq modes nil) it has been
4374 ;; renamed to the backup file. This case `save-buffer'
4375 ;; handles permissions.
4376 (when modes
(set-file-modes tmpfile modes
))
4378 ;; This is a bit lengthy due to the different methods
4379 ;; possible for file transfer. First, we check whether the
4380 ;; method uses an rcp program. If so, we call it.
4381 ;; Otherwise, both encoding and decoding command must be
4382 ;; specified. However, if the method _also_ specifies an
4383 ;; encoding function, then that is used for encoding the
4384 ;; contents of the tmp file.
4386 ;; `rename-file' handles direct copy and out-of-band methods.
4387 ((or (tramp-local-host-p v
)
4388 (and (tramp-method-out-of-band-p v
)
4389 (> (- (or end
(point-max)) (or start
(point-min)))
4390 tramp-copy-size-limit
)))
4392 (rename-file tmpfile filename t
)
4394 (delete-file tmpfile
)
4395 (signal (car err
) (cdr err
)))))
4397 ;; Use inline file transfer.
4400 (tramp-message v
5 "Encoding region...")
4403 ;; Use encoding function or command.
4404 (if (and (symbolp loc-enc
) (fboundp loc-enc
))
4407 v
5 "Encoding region using function `%s'..."
4408 (symbol-name loc-enc
))
4409 (let ((coding-system-for-read 'binary
))
4410 (insert-file-contents-literally tmpfile
))
4411 ;; CCC. The following `let' is a workaround
4412 ;; for the base64.el that comes with
4413 ;; pgnus-0.84. If both of the following
4414 ;; conditions are satisfied, it tries to write
4415 ;; to a local file in default-directory, but
4416 ;; at this point, default-directory is remote.
4417 ;; (`call-process-region' can't write to remote
4418 ;; files, it seems.) The file in question is
4419 ;; a tmp file anyway.
4420 (let ((default-directory
4421 (tramp-compat-temporary-file-directory)))
4422 (funcall loc-enc
(point-min) (point-max))))
4425 v
5 "Encoding region using command `%s'..." loc-enc
)
4426 (unless (equal 0 (tramp-call-local-coding-command
4430 "Cannot write to `%s', local encoding command `%s' failed"
4433 ;; Send buffer into remote decoding command which
4434 ;; writes to remote file. Because this happens on
4435 ;; the remote host, we cannot use the function.
4436 (goto-char (point-max))
4437 (unless (bolp) (newline))
4439 v
5 "Decoding region into remote file %s..." filename
)
4443 "%s >%s <<'EOF'\n%sEOF"
4445 (tramp-shell-quote-argument localname
)
4447 (tramp-barf-unless-okay
4449 "Couldn't write region to `%s', decode using `%s' failed"
4451 ;; When `file-precious-flag' is set, the region is
4452 ;; written to a temporary file. Check that the
4453 ;; checksum is equal to that from the local tmpfile.
4454 (when file-precious-flag
4457 ;; cksum runs locally, if possible.
4458 (zerop (tramp-local-call-process "cksum" tmpfile t
))
4459 ;; cksum runs remotely.
4461 (tramp-send-command-and-check
4464 "cksum <%s" (tramp-shell-quote-argument localname
))))
4465 ;; ... they are different.
4469 (with-current-buffer (tramp-get-buffer v
)
4473 (concat "Couldn't write region to `%s',"
4474 " decode using `%s' failed")
4477 v
5 "Decoding region into remote file %s...done" filename
)
4478 (tramp-flush-file-property v localname
))
4481 (delete-file tmpfile
)))
4483 ;; That's not expected.
4487 (concat "Method `%s' should specify both encoding and "
4488 "decoding command or an rcp program")
4491 ;; Make `last-coding-system-used' have the right value.
4492 (when coding-system-used
4493 (set 'last-coding-system-used coding-system-used
))))
4495 ;; We must protect `last-coding-system-used', now we have set it
4496 ;; to its correct value.
4497 (let (last-coding-system-used)
4498 ;; Set file modification time.
4499 (when (or (eq visit t
) (stringp visit
))
4500 (set-visited-file-modtime
4501 ;; We must pass modtime explicitely, because filename can
4502 ;; be different from (buffer-file-name), f.e. if
4503 ;; `file-precious-flag' is set.
4504 (nth 5 (file-attributes filename
))))
4506 ;; Set the ownership.
4507 (tramp-set-file-uid-gid filename uid gid
)
4508 (when (or (eq visit t
) (null visit
) (stringp visit
))
4509 (tramp-message v
0 "Wrote %s" filename
))
4510 (run-hooks 'tramp-handle-write-region-hook
)))))
4513 (progn (defun tramp-run-real-handler (operation args
)
4514 "Invoke normal file name handler for OPERATION.
4515 First arg specifies the OPERATION, second arg is a list of arguments to
4516 pass to the OPERATION."
4517 (let* ((inhibit-file-name-handlers
4518 `(tramp-file-name-handler
4519 tramp-completion-file-name-handler
4520 cygwin-mount-name-hook-function
4521 cygwin-mount-map-drive-hook-function
4523 ,(and (eq inhibit-file-name-operation operation
)
4524 inhibit-file-name-handlers
)))
4525 (inhibit-file-name-operation operation
))
4526 (apply operation args
))))
4529 (progn (defun tramp-completion-run-real-handler (operation args
)
4530 "Invoke `tramp-file-name-handler' for OPERATION.
4531 First arg specifies the OPERATION, second arg is a list of arguments to
4532 pass to the OPERATION."
4533 (let* ((inhibit-file-name-handlers
4534 `(tramp-completion-file-name-handler
4535 cygwin-mount-name-hook-function
4536 cygwin-mount-map-drive-hook-function
4538 ,(and (eq inhibit-file-name-operation operation
)
4539 inhibit-file-name-handlers
)))
4540 (inhibit-file-name-operation operation
))
4541 (apply operation args
))))
4543 ;; We handle here all file primitives. Most of them have the file
4544 ;; name as first parameter; nevertheless we check for them explicitly
4545 ;; in order to be signaled if a new primitive appears. This
4546 ;; scenario is needed because there isn't a way to decide by
4547 ;; syntactical means whether a foreign method must be called. It would
4548 ;; ease the life if `file-name-handler-alist' would support a decision
4549 ;; function as well but regexp only.
4550 (defun tramp-file-name-for-operation (operation &rest args
)
4551 "Return file name related to OPERATION file primitive.
4552 ARGS are the arguments OPERATION has been called with."
4554 ; FILE resp DIRECTORY
4556 (list 'access-file
'byte-compiler-base-file-name
'delete-directory
4557 'delete-file
'diff-latest-backup-file
'directory-file-name
4558 'directory-files
'directory-files-and-attributes
4559 'dired-compress-file
'dired-uncache
4560 'file-accessible-directory-p
'file-attributes
4561 'file-directory-p
'file-executable-p
'file-exists-p
4562 'file-local-copy
'file-remote-p
'file-modes
4563 'file-name-as-directory
'file-name-directory
4564 'file-name-nondirectory
'file-name-sans-versions
4565 'file-ownership-preserved-p
'file-readable-p
4566 'file-regular-p
'file-symlink-p
'file-truename
4567 'file-writable-p
'find-backup-file-name
'find-file-noselect
4568 'get-file-buffer
'insert-directory
'insert-file-contents
4569 'load
'make-directory
'make-directory-internal
4570 'set-file-modes
'substitute-in-file-name
4571 'unhandled-file-name-directory
'vc-registered
4575 'abbreviate-file-name
'create-file-buffer
4576 'dired-file-modtime
'dired-make-compressed-filename
4577 'dired-recursive-delete-directory
'dired-set-file-modtime
4578 'dired-shell-unhandle-file-name
'dired-uucode-file
4579 'insert-file-contents-literally
'make-temp-name
'recover-file
4580 'vm-imap-check-mail
'vm-pop-check-mail
'vm-spool-check-mail
))
4581 (if (file-name-absolute-p (nth 0 args
))
4583 (expand-file-name (nth 0 args
))))
4584 ; FILE DIRECTORY resp FILE1 FILE2
4586 (list 'add-name-to-file
'copy-file
'expand-file-name
4587 'file-name-all-completions
'file-name-completion
4588 'file-newer-than-file-p
'make-symbolic-link
'rename-file
4590 'dired-make-relative-symlink
4591 'vm-imap-move-mail
'vm-pop-move-mail
'vm-spool-move-mail
))
4594 ((string-match tramp-file-name-regexp
(nth 0 args
)) (nth 0 args
))
4595 ((string-match tramp-file-name-regexp
(nth 1 args
)) (nth 1 args
))
4596 (t (buffer-file-name (current-buffer))))))
4598 ((eq operation
'write-region
)
4602 (list 'set-visited-file-modtime
'verify-visited-file-modtime
4603 ; since Emacs 22 only
4604 'make-auto-save-file-name
4608 (if (bufferp (nth 0 args
)) (nth 0 args
) (current-buffer))))
4611 (list ; not in Emacs 23
4615 ; since Emacs 22 only
4617 ; since Emacs 23 only
4620 'dired-print-file
'dired-shell-call-process
4622 'executable-find
'start-process
'call-process
))
4624 ; unknown file primitive
4625 (t (error "unknown file I/O primitive: %s" operation
))))
4627 (defun tramp-find-foreign-file-name-handler (filename)
4628 "Return foreign file name handler if exists."
4629 (when (and (stringp filename
) (tramp-tramp-file-p filename
))
4630 (let ((v (tramp-dissect-file-name filename t
))
4631 (handler tramp-foreign-file-name-handler-alist
)
4633 ;; When we are not fully sure that filename completion is safe,
4634 ;; we should not return a handler.
4635 (when (or (tramp-file-name-method v
) (tramp-file-name-user v
)
4636 (and (tramp-file-name-host v
)
4637 (not (member (tramp-file-name-host v
)
4638 (mapcar 'car tramp-methods
))))
4639 (not (tramp-completion-mode-p)))
4641 (setq elt
(car handler
)
4642 handler
(cdr handler
))
4643 (when (funcall (car elt
) filename
)
4650 (defun tramp-file-name-handler (operation &rest args
)
4651 "Invoke Tramp file name handler.
4652 Falls back to normal file name handler if no Tramp file name handler exists."
4656 (tramp-replace-environment-variables
4657 (apply 'tramp-file-name-for-operation operation args
)))
4658 (completion (tramp-completion-mode-p))
4659 (foreign (tramp-find-foreign-file-name-handler filename
)))
4660 (with-parsed-tramp-file-name filename nil
4662 ;; When we are in completion mode, some operations
4663 ;; shouldn't be handled by backend.
4664 ((and completion
(zerop (length localname
))
4665 (memq operation
'(file-exists-p file-directory-p
)))
4667 ((and completion
(zerop (length localname
))
4668 (memq operation
'(file-name-as-directory)))
4670 ;; Call the backend function.
4671 (foreign (apply foreign operation args
))
4672 ;; Nothing to do for us.
4673 (t (tramp-run-real-handler operation args
))))))
4674 ;; When `tramp-mode' is not enabled, we don't do anything.
4675 (tramp-run-real-handler operation args
)))
4677 ;; In Emacs, there is some concurrency due to timers. If a timer
4678 ;; interrupts Tramp and wishes to use the same connection buffer as
4679 ;; the "main" Emacs, then garbage might occur in the connection
4680 ;; buffer. Therefore, we need to make sure that a timer does not use
4681 ;; the same connection buffer as the "main" Emacs. We implement a
4682 ;; cheap global lock, instead of locking each connection buffer
4683 ;; separately. The global lock is based on two variables,
4684 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
4685 ;; (with setq) to indicate a lock. But Tramp also calls itself during
4686 ;; processing of a single file operation, so we need to allow
4687 ;; recursive calls. That's where the `tramp-locker' variable comes in
4688 ;; -- it is let-bound to t during the execution of the current
4689 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
4690 ;; then we should just proceed because we have been called
4691 ;; recursively. But if `tramp-locker' is nil, then we are a timer
4692 ;; interrupting the "main" Emacs, and then we signal an error.
4694 (defvar tramp-locked nil
4695 "If non-nil, then Tramp is currently busy.
4696 Together with `tramp-locker', this implements a locking mechanism
4697 preventing reentrant calls of Tramp.")
4699 (defvar tramp-locker nil
4700 "If non-nil, then a caller has locked Tramp.
4701 Together with `tramp-locked', this implements a locking mechanism
4702 preventing reentrant calls of Tramp.")
4704 (defun tramp-sh-file-name-handler (operation &rest args
)
4705 "Invoke remote-shell Tramp file name handler.
4706 Fall back to normal file name handler if no Tramp handler exists."
4707 (when (and tramp-locked
(not tramp-locker
))
4708 (setq tramp-locked nil
)
4709 (signal 'file-error
(list "Forbidden reentrant call of Tramp")))
4710 (let ((tl tramp-locked
))
4713 (setq tramp-locked t
)
4714 (let ((tramp-locker t
))
4716 (let ((fn (assoc operation tramp-file-name-handler-alist
)))
4718 (apply (cdr fn
) args
)
4719 (tramp-run-real-handler operation args
))))))
4720 (setq tramp-locked tl
))))
4723 (progn (defun tramp-completion-file-name-handler (operation &rest args
)
4724 "Invoke Tramp file name completion handler.
4725 Falls back to normal file name handler if no Tramp file name handler exists."
4726 ;; We bind `directory-sep-char' here for XEmacs on Windows, which
4727 ;; would otherwise use backslash.
4728 (let ((directory-sep-char ?
/)
4729 (fn (assoc operation tramp-completion-file-name-handler-alist
)))
4730 ;; When `tramp-mode' is not enabled, we don't do anything.
4731 (if (and fn tramp-mode
)
4732 (save-match-data (apply (cdr fn
) args
))
4733 (tramp-completion-run-real-handler operation args
)))))
4736 (defsubst tramp-register-file-name-handler
()
4737 "Add Tramp file name handler to `file-name-handler-alist'."
4738 ;; Remove autoloaded handler from file name handler alist. Useful,
4739 ;; if `tramp-syntax' has been changed.
4740 (let ((a1 (rassq 'tramp-file-name-handler file-name-handler-alist
)))
4741 (setq file-name-handler-alist
(delete a1 file-name-handler-alist
)))
4743 (add-to-list 'file-name-handler-alist
4744 (cons tramp-file-name-regexp
'tramp-file-name-handler
))
4745 ;; If jka-compr is already loaded, move it to the front of
4746 ;; `file-name-handler-alist'.
4747 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist
)))
4749 (setq file-name-handler-alist
4750 (cons jka
(delete jka file-name-handler-alist
))))))
4752 ;; `tramp-file-name-handler' must be registered before evaluation of
4753 ;; site-start and init files, because there might exist remote files
4754 ;; already, f.e. files kept via recentf-mode.
4755 ;;;###autoload(tramp-register-file-name-handler)
4756 (tramp-register-file-name-handler)
4759 (defsubst tramp-register-completion-file-name-handler
()
4760 "Add Tramp completion file name handler to `file-name-handler-alist'."
4761 ;; Remove autoloaded handler from file name handler alist. Useful,
4762 ;; if `tramp-syntax' has been changed.
4764 'tramp-completion-file-name-handler file-name-handler-alist
)))
4765 (setq file-name-handler-alist
(delete a1 file-name-handler-alist
)))
4766 ;; `partial-completion-mode' is unknown in XEmacs. So we should
4767 ;; load it unconditionally there. In the GNU Emacs case, method/
4768 ;; user/host name completion shall be bound to `partial-completion-mode'.
4769 ;; `ido-mode' and `icy-mode' are other packages which extend file
4771 (when (or (not (boundp 'partial-completion-mode
))
4772 (symbol-value 'partial-completion-mode
)
4774 (featurep 'icicles
))
4775 (add-to-list 'file-name-handler-alist
4776 (cons tramp-completion-file-name-regexp
4777 'tramp-completion-file-name-handler
))
4778 (put 'tramp-completion-file-name-handler
'safe-magic t
))
4779 ;; If jka-compr is already loaded, move it to the front of
4780 ;; `file-name-handler-alist'.
4781 (let ((jka (rassoc 'jka-compr-handler file-name-handler-alist
)))
4783 (setq file-name-handler-alist
4784 (cons jka
(delete jka file-name-handler-alist
))))))
4786 ;; During autoload, it shall be checked whether
4787 ;; `partial-completion-mode' is active. Therefore registering of
4788 ;; `tramp-completion-file-name-handler' will be delayed.
4789 ;;;###autoload(add-hook
4790 ;;;###autoload 'after-init-hook
4791 ;;;###autoload 'tramp-register-completion-file-name-handler)
4792 (tramp-register-completion-file-name-handler)
4795 (defun tramp-unload-file-name-handlers ()
4796 (setq file-name-handler-alist
4797 (delete (rassoc 'tramp-file-name-handler
4798 file-name-handler-alist
)
4799 (delete (rassoc 'tramp-completion-file-name-handler
4800 file-name-handler-alist
)
4801 file-name-handler-alist
))))
4803 (add-hook 'tramp-unload-hook
'tramp-unload-file-name-handlers
)
4805 ;;; File name handler functions for completion mode:
4807 (defvar tramp-completion-mode nil
4808 "If non-nil, external packages signal that they are in file name completion.
4810 This is necessary, because Tramp uses a heuristic depending on last
4811 input event. This fails when external packages use other characters
4812 but <TAB>, <SPACE> or ?\\? for file name completion. This variable
4813 should never be set globally, the intention is to let-bind it.")
4815 ;; Necessary because `tramp-file-name-regexp-unified' and
4816 ;; `tramp-completion-file-name-regexp-unified' aren't different. If
4817 ;; nil, `tramp-completion-run-real-handler' is called (i.e. forwarding
4818 ;; to `tramp-file-name-handler'). Otherwise, it takes
4819 ;; `tramp-run-real-handler'. Using `last-input-event' is a little bit
4820 ;; risky, because completing a file might require loading other files,
4821 ;; like "~/.netrc", and for them it shouldn't be decided based on that
4822 ;; variable. On the other hand, those files shouldn't have partial
4823 ;; Tramp file name syntax. Maybe another variable should be introduced
4824 ;; overwriting this check in such cases. Or we change Tramp file name
4825 ;; syntax in order to avoid ambiguities, like in XEmacs ...
4826 (defun tramp-completion-mode-p ()
4827 "Checks whether method / user name / host name completion is active."
4829 ;; Signal from outside.
4830 tramp-completion-mode
4832 (equal last-input-event
'tab
)
4833 (and (natnump last-input-event
)
4835 ;; ?\t has event-modifier 'control.
4836 (equal last-input-event ?
\t)
4837 (and (not (event-modifiers last-input-event
))
4838 (or (equal last-input-event ?
\?)
4839 (equal last-input-event ?\
)))))
4841 (and (featurep 'xemacs
)
4842 ;; `last-input-event' might be nil.
4843 (not (null last-input-event
))
4844 ;; `last-input-event' may have no character approximation.
4845 (funcall (symbol-function 'event-to-character
) last-input-event
)
4847 ;; ?\t has event-modifier 'control.
4849 (funcall (symbol-function 'event-to-character
)
4850 last-input-event
) ?
\t)
4851 (and (not (event-modifiers last-input-event
))
4853 (funcall (symbol-function 'event-to-character
)
4854 last-input-event
) ?
\?)
4856 (funcall (symbol-function 'event-to-character
)
4857 last-input-event
) ?\
)))))))
4859 ;; Method, host name and user name completion.
4860 ;; `tramp-completion-dissect-file-name' returns a list of
4861 ;; tramp-file-name structures. For all of them we return possible completions.
4863 (defun tramp-completion-handle-file-name-all-completions (filename directory
)
4864 "Like `file-name-all-completions' for partial Tramp files."
4866 (let* ((fullname (tramp-drop-volume-letter
4867 (expand-file-name filename directory
)))
4868 ;; Possible completion structures.
4869 (v (tramp-completion-dissect-file-name fullname
))
4873 (let* ((car (car v
))
4874 (method (tramp-file-name-method car
))
4875 (user (tramp-file-name-user car
))
4876 (host (tramp-file-name-host car
))
4877 (localname (tramp-file-name-localname car
))
4878 (m (tramp-find-method method user host
))
4879 (tramp-current-user user
) ; see `tramp-parse-passwd'
4882 (unless localname
;; Nothing to complete.
4886 ;; Method dependent user / host combinations.
4890 (setq all-user-hosts
4891 (append all-user-hosts
4892 (funcall (nth 0 x
) (nth 1 x
)))))
4893 (tramp-get-completion-function m
))
4899 (tramp-get-completion-user-host
4900 method user host
(nth 0 x
) (nth 1 x
)))
4901 (delq nil all-user-hosts
)))))
4903 ;; Possible methods.
4905 (append result
(tramp-get-completion-methods m
)))))
4909 ;; Unify list, remove nil elements.
4911 (let ((car (car result
)))
4915 (substring car
(length (tramp-drop-volume-letter directory
)))))
4916 (setq result
(cdr result
))))
4918 ;; Complete local parts.
4922 (tramp-completion-run-real-handler
4923 'file-name-all-completions
(list filename directory
))
4926 ;; Method, host name and user name completion for a file.
4928 (defun tramp-completion-handle-file-name-completion
4929 (filename directory
&optional predicate
)
4930 "Like `file-name-completion' for Tramp files."
4933 (mapcar 'list
(file-name-all-completions filename directory
))
4935 (lambda (x) (funcall predicate
(expand-file-name (car x
) directory
))))))
4937 ;; I misuse a little bit the tramp-file-name structure in order to handle
4938 ;; completion possibilities for partial methods / user names / host names.
4939 ;; Return value is a list of tramp-file-name structures according to possible
4940 ;; completions. If "localname" is non-nil it means there
4941 ;; shouldn't be a completion anymore.
4943 ;; Expected results:
4945 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
4946 ;; [nil nil "x" nil] [nil "x" nil nil] [nil "x" "y" nil]
4947 ;; [nil "x" nil nil]
4948 ;; ["x" nil nil nil]
4950 ;; "/x:" "/x:y" "/x:y:"
4951 ;; [nil nil "x" ""] [nil nil "x" "y"] ["x" nil "y" ""]
4953 ;; ["x" nil "" nil] ["x" nil "y" nil]
4954 ;; ["x" "" nil nil] ["x" "y" nil nil]
4956 ;; "/x:y@" "/x:y@z" "/x:y@z:"
4957 ;; [nil nil "x" "y@"] [nil nil "x" "y@z"] ["x" "y" "z" ""]
4958 ;; "/[x/y@" "/[x/y@z"
4959 ;; ["x" nil "y" nil] ["x" "y" "z" nil]
4960 (defun tramp-completion-dissect-file-name (name)
4961 "Returns a list of `tramp-file-name' structures.
4962 They are collected by `tramp-completion-dissect-file-name1'."
4966 (tramp-completion-ipv6-regexp
4969 (if (zerop (length tramp-postfix-ipv6-format
))
4970 tramp-postfix-host-format
4971 tramp-postfix-ipv6-format
)))
4972 ;; "/method" "/[method"
4973 (tramp-completion-file-name-structure1
4974 (list (concat tramp-prefix-regexp
"\\(" tramp-method-regexp x-nil
"\\)$")
4977 (tramp-completion-file-name-structure2
4978 (list (concat tramp-prefix-regexp
"\\(" tramp-user-regexp x-nil
"\\)$")
4981 (tramp-completion-file-name-structure3
4982 (list (concat tramp-prefix-regexp
"\\(" tramp-host-regexp x-nil
"\\)$")
4984 ;; "/[ipv6" "/[ipv6"
4985 (tramp-completion-file-name-structure4
4986 (list (concat tramp-prefix-regexp
4987 tramp-prefix-ipv6-regexp
4988 "\\(" tramp-completion-ipv6-regexp x-nil
"\\)$")
4990 ;; "/user@host" "/[user@host"
4991 (tramp-completion-file-name-structure5
4992 (list (concat tramp-prefix-regexp
4993 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
4994 "\\(" tramp-host-regexp x-nil
"\\)$")
4996 ;; "/user@[ipv6" "/[user@ipv6"
4997 (tramp-completion-file-name-structure6
4998 (list (concat tramp-prefix-regexp
4999 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
5000 tramp-prefix-ipv6-regexp
5001 "\\(" tramp-completion-ipv6-regexp x-nil
"\\)$")
5003 ;; "/method:user" "/[method/user" "/method://user"
5004 (tramp-completion-file-name-structure7
5005 (list (concat tramp-prefix-regexp
5006 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
5007 "\\(" tramp-user-regexp x-nil
"\\)$")
5009 ;; "/method:host" "/[method/host" "/method://host"
5010 (tramp-completion-file-name-structure8
5011 (list (concat tramp-prefix-regexp
5012 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
5013 "\\(" tramp-host-regexp x-nil
"\\)$")
5015 ;; "/method:[ipv6" "/[method/ipv6" "/method://[ipv6"
5016 (tramp-completion-file-name-structure9
5017 (list (concat tramp-prefix-regexp
5018 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
5019 tramp-prefix-ipv6-regexp
5020 "\\(" tramp-completion-ipv6-regexp x-nil
"\\)$")
5022 ;; "/method:user@host" "/[method/user@host" "/method://user@host"
5023 (tramp-completion-file-name-structure10
5024 (list (concat tramp-prefix-regexp
5025 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
5026 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
5027 "\\(" tramp-host-regexp x-nil
"\\)$")
5029 ;; "/method:user@[ipv6" "/[method/user@ipv6" "/method://user@[ipv6"
5030 (tramp-completion-file-name-structure11
5031 (list (concat tramp-prefix-regexp
5032 "\\(" tramp-method-regexp
"\\)" tramp-postfix-method-regexp
5033 "\\(" tramp-user-regexp
"\\)" tramp-postfix-user-regexp
5034 tramp-prefix-ipv6-regexp
5035 "\\(" tramp-completion-ipv6-regexp x-nil
"\\)$")
5037 ;; "/method: "/method:/"
5038 (tramp-completion-file-name-structure12
5040 (if (equal tramp-syntax
'url
)
5041 (concat tramp-prefix-regexp
5042 "\\(" tramp-method-regexp
"\\)"
5043 "\\(" (substring tramp-postfix-method-regexp
0 1)
5044 "\\|" (substring tramp-postfix-method-regexp
1 2) "\\)"
5046 ;; Should not match if not URL syntax.
5047 (concat tramp-prefix-regexp
"/$"))
5049 ;; "/method: "/method:/"
5050 (tramp-completion-file-name-structure13
5052 (if (equal tramp-syntax
'url
)
5053 (concat tramp-prefix-regexp
5054 "\\(" tramp-method-regexp
"\\)"
5055 "\\(" (substring tramp-postfix-method-regexp
0 1)
5056 "\\|" (substring tramp-postfix-method-regexp
1 2) "\\)"
5058 ;; Should not match if not URL syntax.
5059 (concat tramp-prefix-regexp
"/$"))
5062 (mapc (lambda (regexp)
5063 (add-to-list 'result
5064 (tramp-completion-dissect-file-name1 regexp name
)))
5066 tramp-completion-file-name-structure1
5067 tramp-completion-file-name-structure2
5068 tramp-completion-file-name-structure3
5069 tramp-completion-file-name-structure4
5070 tramp-completion-file-name-structure5
5071 tramp-completion-file-name-structure6
5072 tramp-completion-file-name-structure7
5073 tramp-completion-file-name-structure8
5074 tramp-completion-file-name-structure9
5075 tramp-completion-file-name-structure10
5076 tramp-completion-file-name-structure11
5077 tramp-completion-file-name-structure12
5078 tramp-completion-file-name-structure13
5079 tramp-file-name-structure
))
5083 (defun tramp-completion-dissect-file-name1 (structure name
)
5084 "Returns a `tramp-file-name' structure matching STRUCTURE.
5085 The structure consists of remote method, remote user,
5086 remote host and localname (filename on remote host)."
5089 (when (string-match (nth 0 structure
) name
)
5090 (let ((method (and (nth 1 structure
)
5091 (match-string (nth 1 structure
) name
)))
5092 (user (and (nth 2 structure
)
5093 (match-string (nth 2 structure
) name
)))
5094 (host (and (nth 3 structure
)
5095 (match-string (nth 3 structure
) name
)))
5096 (localname (and (nth 4 structure
)
5097 (match-string (nth 4 structure
) name
))))
5098 (vector method user host localname
)))))
5100 ;; This function returns all possible method completions, adding the
5101 ;; trailing method delimeter.
5102 (defun tramp-get-completion-methods (partial-method)
5103 "Returns all method completions for PARTIAL-METHOD."
5107 (string-match (concat "^" (regexp-quote partial-method
)) method
)
5108 (tramp-completion-make-tramp-file-name method nil nil nil
)))
5109 (mapcar 'car tramp-methods
)))
5111 ;; Compares partial user and host names with possible completions.
5112 (defun tramp-get-completion-user-host (method partial-user partial-host user host
)
5113 "Returns the most expanded string for user and host name completion.
5114 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
5117 ((and partial-user partial-host
)
5119 (string-match (concat "^" (regexp-quote partial-host
)) host
)
5120 (string-equal partial-user
(or user partial-user
)))
5121 (setq user partial-user
)
5128 (and user
(string-match (concat "^" (regexp-quote partial-user
)) user
))
5134 (and host
(string-match (concat "^" (regexp-quote partial-host
)) host
))
5140 (unless (zerop (+ (length user
) (length host
)))
5141 (tramp-completion-make-tramp-file-name method user host nil
)))
5143 (defun tramp-parse-rhosts (filename)
5144 "Return a list of (user host) tuples allowed to access.
5145 Either user or host may be nil."
5146 ;; On Windows, there are problems in completion when
5147 ;; `default-directory' is remote.
5148 (let ((default-directory (tramp-compat-temporary-file-directory))
5150 (when (file-readable-p filename
)
5152 (insert-file-contents filename
)
5153 (goto-char (point-min))
5155 (push (tramp-parse-rhosts-group) res
))))
5158 (defun tramp-parse-rhosts-group ()
5159 "Return a (user host) tuple allowed to access.
5160 Either user or host may be nil."
5164 "^\\(" tramp-host-regexp
"\\)"
5165 "\\([ \t]+" "\\(" tramp-user-regexp
"\\)" "\\)?")))
5166 (narrow-to-region (point) (tramp-compat-line-end-position))
5167 (when (re-search-forward regexp nil t
)
5168 (setq result
(append (list (match-string 3) (match-string 1)))))
5173 (defun tramp-parse-shosts (filename)
5174 "Return a list of (user host) tuples allowed to access.
5175 User is always nil."
5176 ;; On Windows, there are problems in completion when
5177 ;; `default-directory' is remote.
5178 (let ((default-directory (tramp-compat-temporary-file-directory))
5180 (when (file-readable-p filename
)
5182 (insert-file-contents filename
)
5183 (goto-char (point-min))
5185 (push (tramp-parse-shosts-group) res
))))
5188 (defun tramp-parse-shosts-group ()
5189 "Return a (user host) tuple allowed to access.
5190 User is always nil."
5192 (regexp (concat "^\\(" tramp-host-regexp
"\\)")))
5193 (narrow-to-region (point) (tramp-compat-line-end-position))
5194 (when (re-search-forward regexp nil t
)
5195 (setq result
(list nil
(match-string 1))))
5198 (> (skip-chars-forward ",") 0)
5202 (defun tramp-parse-sconfig (filename)
5203 "Return a list of (user host) tuples allowed to access.
5204 User is always nil."
5205 ;; On Windows, there are problems in completion when
5206 ;; `default-directory' is remote.
5207 (let ((default-directory (tramp-compat-temporary-file-directory))
5209 (when (file-readable-p filename
)
5211 (insert-file-contents filename
)
5212 (goto-char (point-min))
5214 (push (tramp-parse-sconfig-group) res
))))
5217 (defun tramp-parse-sconfig-group ()
5218 "Return a (user host) tuple allowed to access.
5219 User is always nil."
5221 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp
"\\)")))
5222 (narrow-to-region (point) (tramp-compat-line-end-position))
5223 (when (re-search-forward regexp nil t
)
5224 (setq result
(list nil
(match-string 1))))
5227 (> (skip-chars-forward ",") 0)
5231 (defun tramp-parse-shostkeys (dirname)
5232 "Return a list of (user host) tuples allowed to access.
5233 User is always nil."
5234 ;; On Windows, there are problems in completion when
5235 ;; `default-directory' is remote.
5236 (let* ((default-directory (tramp-compat-temporary-file-directory))
5237 (regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp
"\\)\\.pub$"))
5238 (files (when (file-directory-p dirname
) (directory-files dirname
)))
5241 (when (string-match regexp
(car files
))
5242 (push (list nil
(match-string 1 (car files
))) result
))
5243 (setq files
(cdr files
)))
5246 (defun tramp-parse-sknownhosts (dirname)
5247 "Return a list of (user host) tuples allowed to access.
5248 User is always nil."
5249 ;; On Windows, there are problems in completion when
5250 ;; `default-directory' is remote.
5251 (let* ((default-directory (tramp-compat-temporary-file-directory))
5252 (regexp (concat "^\\(" tramp-host-regexp
5253 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
5254 (files (when (file-directory-p dirname
) (directory-files dirname
)))
5257 (when (string-match regexp
(car files
))
5258 (push (list nil
(match-string 1 (car files
))) result
))
5259 (setq files
(cdr files
)))
5262 (defun tramp-parse-hosts (filename)
5263 "Return a list of (user host) tuples allowed to access.
5264 User is always nil."
5265 ;; On Windows, there are problems in completion when
5266 ;; `default-directory' is remote.
5267 (let ((default-directory (tramp-compat-temporary-file-directory))
5269 (when (file-readable-p filename
)
5271 (insert-file-contents filename
)
5272 (goto-char (point-min))
5274 (push (tramp-parse-hosts-group) res
))))
5277 (defun tramp-parse-hosts-group ()
5278 "Return a (user host) tuple allowed to access.
5279 User is always nil."
5282 (concat "^\\(" tramp-ipv6-regexp
"\\|" tramp-host-regexp
"\\)")))
5283 (narrow-to-region (point) (tramp-compat-line-end-position))
5284 (when (re-search-forward regexp nil t
)
5285 (setq result
(list nil
(match-string 1))))
5288 (> (skip-chars-forward " \t") 0)
5292 ;; For su-alike methods it would be desirable to return "root@localhost"
5293 ;; as default. Unfortunately, we have no information whether any user name
5294 ;; has been typed already. So we use `tramp-current-user' as indication,
5295 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
5296 (defun tramp-parse-passwd (filename)
5297 "Return a list of (user host) tuples allowed to access.
5298 Host is always \"localhost\"."
5299 ;; On Windows, there are problems in completion when
5300 ;; `default-directory' is remote.
5301 (let ((default-directory (tramp-compat-temporary-file-directory))
5303 (if (zerop (length tramp-current-user
))
5305 (when (file-readable-p filename
)
5307 (insert-file-contents filename
)
5308 (goto-char (point-min))
5310 (push (tramp-parse-passwd-group) res
))))
5313 (defun tramp-parse-passwd-group ()
5314 "Return a (user host) tuple allowed to access.
5315 Host is always \"localhost\"."
5317 (regexp (concat "^\\(" tramp-user-regexp
"\\):")))
5318 (narrow-to-region (point) (tramp-compat-line-end-position))
5319 (when (re-search-forward regexp nil t
)
5320 (setq result
(list (match-string 1) "localhost")))
5325 (defun tramp-parse-netrc (filename)
5326 "Return a list of (user host) tuples allowed to access.
5328 ;; On Windows, there are problems in completion when
5329 ;; `default-directory' is remote.
5330 (let ((default-directory (tramp-compat-temporary-file-directory))
5332 (when (file-readable-p filename
)
5334 (insert-file-contents filename
)
5335 (goto-char (point-min))
5337 (push (tramp-parse-netrc-group) res
))))
5340 (defun tramp-parse-netrc-group ()
5341 "Return a (user host) tuple allowed to access.
5346 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp
"\\)"
5347 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp
"\\)" "\\)?")))
5348 (narrow-to-region (point) (tramp-compat-line-end-position))
5349 (when (re-search-forward regexp nil t
)
5350 (setq result
(list (match-string 3) (match-string 1))))
5355 (defun tramp-parse-putty (registry)
5356 "Return a list of (user host) tuples allowed to access.
5357 User is always nil."
5358 ;; On Windows, there are problems in completion when
5359 ;; `default-directory' is remote.
5360 (let ((default-directory (tramp-compat-temporary-file-directory))
5363 (when (zerop (tramp-local-call-process "reg" nil t nil
"query" registry
))
5364 (goto-char (point-min))
5366 (push (tramp-parse-putty-group registry
) res
))))
5369 (defun tramp-parse-putty-group (registry)
5370 "Return a (user host) tuple allowed to access.
5371 User is always nil."
5373 (regexp (concat (regexp-quote registry
) "\\\\\\(.+\\)")))
5374 (narrow-to-region (point) (tramp-compat-line-end-position))
5375 (when (re-search-forward regexp nil t
)
5376 (setq result
(list nil
(match-string 1))))
5381 ;;; Internal Functions:
5383 (defun tramp-maybe-send-script (vec script name
)
5384 "Define in remote shell function NAME implemented as SCRIPT.
5385 Only send the definition if it has not already been done."
5386 (let* ((p (tramp-get-connection-process vec
))
5387 (scripts (tramp-get-connection-property p
"scripts" nil
)))
5388 (unless (member name scripts
)
5389 (tramp-message vec
5 "Sending script `%s'..." name
)
5390 ;; The script could contain a call of Perl. This is masked with `%s'.
5391 (tramp-send-command-and-check
5393 (format "%s () {\n%s\n}" name
5394 (format script
(tramp-get-remote-perl vec
))))
5395 (tramp-set-connection-property p
"scripts" (cons name scripts
))
5396 (tramp-message vec
5 "Sending script `%s'...done." name
))))
5398 (defun tramp-set-auto-save ()
5399 (when (and ;; ange-ftp has its own auto-save mechanism
5400 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
5401 'tramp-sh-file-name-handler
)
5403 (auto-save-mode 1)))
5404 (add-hook 'find-file-hooks
'tramp-set-auto-save t
)
5405 (add-hook 'tramp-unload-hook
5407 (remove-hook 'find-file-hooks
'tramp-set-auto-save
)))
5409 (defun tramp-run-test (switch filename
)
5410 "Run `test' on the remote system, given a SWITCH and a FILENAME.
5411 Returns the exit code of the `test' program."
5412 (with-parsed-tramp-file-name filename nil
5413 (tramp-send-command-and-check
5417 (tramp-get-test-command v
)
5419 (tramp-shell-quote-argument localname
)))))
5421 (defun tramp-run-test2 (format-string file1 file2
)
5422 "Run `test'-like program on the remote system, given FILE1, FILE2.
5423 FORMAT-STRING contains the program name, switches, and place holders.
5424 Returns the exit code of the `test' program. Barfs if the methods,
5425 hosts, or files, disagree."
5426 (unless (tramp-equal-remote file1 file2
)
5427 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1
) file1 file2
) nil
5430 "tramp-run-test2 only implemented for same method, user, host")))
5431 (with-parsed-tramp-file-name file1 v1
5432 (with-parsed-tramp-file-name file1 v2
5433 (tramp-send-command-and-check
5435 (format format-string
5436 (tramp-shell-quote-argument v1-localname
)
5437 (tramp-shell-quote-argument v2-localname
))))))
5439 (defun tramp-buffer-name (vec)
5440 "A name for the connection buffer VEC."
5441 ;; We must use `tramp-file-name-real-host', because for gateway
5442 ;; methods the default port will be expanded later on, which would
5444 (let ((method (tramp-file-name-method vec
))
5445 (user (tramp-file-name-user vec
))
5446 (host (tramp-file-name-real-host vec
)))
5447 (if (not (zerop (length user
)))
5448 (format "*tramp/%s %s@%s*" method user host
)
5449 (format "*tramp/%s %s*" method host
))))
5451 (defun tramp-get-buffer (vec)
5452 "Get the connection buffer to be used for VEC."
5453 (or (get-buffer (tramp-buffer-name vec
))
5454 (with-current-buffer (get-buffer-create (tramp-buffer-name vec
))
5455 (setq buffer-undo-list t
)
5456 (setq default-directory
5457 (tramp-make-tramp-file-name
5458 (tramp-file-name-method vec
)
5459 (tramp-file-name-user vec
)
5460 (tramp-file-name-host vec
)
5464 (defun tramp-get-connection-buffer (vec)
5465 "Get the connection buffer to be used for VEC.
5466 In case a second asynchronous communication has been started, it is different
5467 from `tramp-get-buffer'."
5468 (or (tramp-get-connection-property vec
"process-buffer" nil
)
5469 (tramp-get-buffer vec
)))
5471 (defun tramp-get-connection-process (vec)
5472 "Get the connection process to be used for VEC.
5473 In case a second asynchronous communication has been started, it is different
5474 from the default one."
5476 (or (tramp-get-connection-property vec
"process-name" nil
)
5477 (tramp-buffer-name vec
))))
5479 (defun tramp-debug-buffer-name (vec)
5480 "A name for the debug buffer for VEC."
5481 ;; We must use `tramp-file-name-real-host', because for gateway
5482 ;; methods the default port will be expanded later on, which would
5484 (let ((method (tramp-file-name-method vec
))
5485 (user (tramp-file-name-user vec
))
5486 (host (tramp-file-name-real-host vec
)))
5487 (if (not (zerop (length user
)))
5488 (format "*debug tramp/%s %s@%s*" method user host
)
5489 (format "*debug tramp/%s %s*" method host
))))
5491 (defun tramp-get-debug-buffer (vec)
5492 "Get the debug buffer for VEC."
5493 (with-current-buffer
5494 (get-buffer-create (tramp-debug-buffer-name vec
))
5496 (setq buffer-undo-list t
)
5497 ;; Activate outline-mode. This runs `text-mode-hook' and
5498 ;; `outline-mode-hook'. We must prevent that local processes
5499 ;; die. Yes: I've seen `flyspell-mode', which starts "ispell"
5501 (let ((default-directory (tramp-compat-temporary-file-directory)))
5503 (set (make-local-variable 'outline-regexp
)
5504 "[0-9]+:[0-9]+:[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #")
5505 ; (set (make-local-variable 'outline-regexp)
5506 ; "[a-z.-]+:[0-9]+: [a-z0-9-]+ (\\([0-9]+\\)) #")
5507 (set (make-local-variable 'outline-level
) 'tramp-outline-level
))
5510 (defun tramp-outline-level ()
5511 "Return the depth to which a statement is nested in the outline.
5512 Point must be at the beginning of a header line.
5514 The outline level is equal to the verbosity of the Tramp message."
5515 (1+ (string-to-number (match-string 1))))
5517 (defun tramp-find-executable
5518 (vec progname dirlist
&optional ignore-tilde ignore-path
)
5519 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
5520 First arg VEC specifies the connection, PROGNAME is the program
5521 to search for, and DIRLIST gives the list of directories to
5522 search. If IGNORE-TILDE is non-nil, directory names starting
5523 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
5526 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
5528 This function expects to be in the right *tramp* buffer."
5529 (with-current-buffer (tramp-get-buffer vec
)
5531 ;; Check whether the executable is in $PATH. "which(1)" does not
5532 ;; report always a correct error code; therefore we check the
5533 ;; number of words it returns.
5535 (tramp-send-command vec
(format "which \\%s | wc -w" progname
))
5536 (goto-char (point-min))
5537 (if (looking-at "^1$")
5538 (setq result
(concat "\\" progname
))))
5541 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
5542 ;; `remove' is in CL, and we want to avoid CL dependencies.
5545 (setq d
(car dirlist
))
5546 (setq dirlist
(cdr dirlist
))
5547 (unless (char-equal ?~
(aref d
0))
5548 (setq newdl
(cons d newdl
))))
5549 (setq dirlist
(nreverse newdl
))))
5552 (format (concat "while read d; "
5553 "do if test -x $d/%s -a -f $d/%s; "
5554 "then echo tramp_executable $d/%s; "
5555 "break; fi; done <<'EOF'\n"
5557 progname progname progname
(mapconcat 'identity dirlist
"\n")))
5558 (goto-char (point-max))
5559 (when (search-backward "tramp_executable " nil t
)
5560 (skip-chars-forward "^ ")
5561 (skip-chars-forward " ")
5562 (setq result
(buffer-substring
5563 (point) (tramp-compat-line-end-position)))))
5566 (defun tramp-set-remote-path (vec)
5567 "Sets the remote environment PATH to existing directories.
5568 I.e., for each directory in `tramp-remote-path', it is tested
5569 whether it exists and if so, it is added to the environment
5571 (tramp-message vec
5 (format "Setting $PATH environment variable"))
5573 vec
(format "PATH=%s; export PATH"
5574 (mapconcat 'identity
(tramp-get-remote-path vec
) ":"))))
5576 ;; ------------------------------------------------------------
5577 ;; -- Communication with external shell --
5578 ;; ------------------------------------------------------------
5580 (defun tramp-find-file-exists-command (vec)
5581 "Find a command on the remote host for checking if a file exists.
5582 Here, we are looking for a command which has zero exit status if the
5583 file exists and nonzero exit status otherwise."
5584 (let ((existing "/")
5586 (tramp-shell-quote-argument "/ this file does not exist "))
5588 ;; The algorithm is as follows: we try a list of several commands.
5589 ;; For each command, we first run `$cmd /' -- this should return
5590 ;; true, as the root directory always exists. And then we run
5591 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
5592 ;; does not exist. This should return false. We use the first
5593 ;; command we find that seems to work.
5594 ;; The list of commands to try is as follows:
5595 ;; `ls -d' This works on most systems, but NetBSD 1.4
5596 ;; has a bug: `ls' always returns zero exit
5597 ;; status, even for files which don't exist.
5598 ;; `test -e' Some Bourne shells have a `test' builtin
5599 ;; which does not know the `-e' option.
5600 ;; `/bin/test -e' For those, the `test' binary on disk normally
5601 ;; provides the option. Alas, the binary
5602 ;; is sometimes `/bin/test' and sometimes it's
5604 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
5606 (and (setq result
(format "%s -e" (tramp-get-test-command vec
)))
5607 (zerop (tramp-send-command-and-check
5608 vec
(format "%s %s" result existing
)))
5609 (not (zerop (tramp-send-command-and-check
5610 vec
(format "%s %s" result nonexisting
)))))
5611 (and (setq result
"/bin/test -e")
5612 (zerop (tramp-send-command-and-check
5613 vec
(format "%s %s" result existing
)))
5614 (not (zerop (tramp-send-command-and-check
5615 vec
(format "%s %s" result nonexisting
)))))
5616 (and (setq result
"/usr/bin/test -e")
5617 (zerop (tramp-send-command-and-check
5618 vec
(format "%s %s" result existing
)))
5619 (not (zerop (tramp-send-command-and-check
5620 vec
(format "%s %s" result nonexisting
)))))
5621 (and (setq result
(format "%s -d" (tramp-get-ls-command vec
)))
5622 (zerop (tramp-send-command-and-check
5623 vec
(format "%s %s" result existing
)))
5624 (not (zerop (tramp-send-command-and-check
5625 vec
(format "%s %s" result nonexisting
))))))
5627 vec
'file-error
"Couldn't find command to check if file exists"))
5630 ;; CCC test ksh or bash found for tilde expansion?
5631 (defun tramp-find-shell (vec)
5632 "Opens a shell on the remote host which groks tilde expansion."
5633 (unless (tramp-get-connection-property vec
"remote-shell" nil
)
5635 (with-current-buffer (tramp-get-buffer vec
)
5636 (tramp-send-command vec
"echo ~root" t
)
5638 ((string-match "^~root$" (buffer-string))
5640 (or (tramp-find-executable
5641 vec
"bash" (tramp-get-remote-path vec
) t
)
5642 (tramp-find-executable
5643 vec
"ksh" (tramp-get-remote-path vec
) t
)))
5647 "Couldn't find a shell which groks tilde expansion"))
5648 ;; Find arguments for this shell.
5649 (let ((alist tramp-sh-extra-args
)
5651 (while (and alist
(null extra-args
))
5652 (setq item
(pop alist
))
5653 (when (string-match (car item
) shell
)
5654 (setq extra-args
(cdr item
))))
5655 (when extra-args
(setq shell
(concat shell
" " extra-args
))))
5657 vec
5 "Starting remote shell `%s' for tilde expansion..." shell
)
5658 (let ((tramp-end-of-output "$ "))
5661 (format "PROMPT_COMMAND='' PS1='$ ' PS2='' PS3='' exec %s" shell
)
5664 (tramp-message vec
5 "Setting remote shell prompt...")
5665 (tramp-send-command vec
(format "PS1='%s'" tramp-end-of-output
) t
)
5666 (tramp-send-command vec
"PS2=''" t
)
5667 (tramp-send-command vec
"PS3=''" t
)
5668 (tramp-send-command vec
"PROMPT_COMMAND=''" t
)
5669 (tramp-message vec
5 "Setting remote shell prompt...done"))
5672 vec
5 "Remote `%s' groks tilde expansion, good"
5673 (tramp-get-method-parameter
5674 (tramp-file-name-method vec
) 'tramp-remote-sh
))
5675 (tramp-set-connection-property
5677 (tramp-get-method-parameter
5678 (tramp-file-name-method vec
) 'tramp-remote-sh
))))))))
5680 ;; ------------------------------------------------------------
5681 ;; -- Functions for establishing connection --
5682 ;; ------------------------------------------------------------
5684 ;; The following functions are actions to be taken when seeing certain
5685 ;; prompts from the remote host. See the variable
5686 ;; `tramp-actions-before-shell' for usage of these functions.
5688 (defun tramp-action-login (proc vec
)
5689 "Send the login name."
5690 (when (not (stringp tramp-current-user
))
5691 (save-window-excursion
5692 (let ((enable-recursive-minibuffers t
))
5693 (pop-to-buffer (tramp-get-connection-buffer vec
))
5694 (setq tramp-current-user
(read-string (match-string 0))))))
5695 (tramp-message vec
3 "Sending login name `%s'" tramp-current-user
)
5696 (with-current-buffer (tramp-get-connection-buffer vec
)
5697 (tramp-message vec
6 "\n%s" (buffer-string)))
5698 (tramp-send-string vec tramp-current-user
))
5700 (defun tramp-action-password (proc vec
)
5701 "Query the user for a password."
5702 (tramp-message vec
3 "Sending password")
5703 (tramp-enter-password proc
))
5705 (defun tramp-action-succeed (proc vec
)
5706 "Signal success in finding shell prompt."
5707 (throw 'tramp-action
'ok
))
5709 (defun tramp-action-permission-denied (proc vec
)
5710 "Signal permission denied."
5712 (throw 'tramp-action
'permission-denied
))
5714 (defun tramp-action-yesno (proc vec
)
5715 "Ask the user for confirmation using `yes-or-no-p'.
5716 Send \"yes\" to remote process on confirmation, abort otherwise.
5717 See also `tramp-action-yn'."
5718 (save-window-excursion
5719 (let ((enable-recursive-minibuffers t
))
5720 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec
)))
5721 (unless (yes-or-no-p (match-string 0))
5723 (throw 'tramp-action
'permission-denied
))
5724 (with-current-buffer (tramp-get-connection-buffer vec
)
5725 (tramp-message vec
6 "\n%s" (buffer-string)))
5726 (tramp-send-string vec
"yes"))))
5728 (defun tramp-action-yn (proc vec
)
5729 "Ask the user for confirmation using `y-or-n-p'.
5730 Send \"y\" to remote process on confirmation, abort otherwise.
5731 See also `tramp-action-yesno'."
5732 (save-window-excursion
5733 (let ((enable-recursive-minibuffers t
))
5734 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec
)))
5735 (unless (y-or-n-p (match-string 0))
5737 (throw 'tramp-action
'permission-denied
))
5738 (with-current-buffer (tramp-get-connection-buffer vec
)
5739 (tramp-message vec
6 "\n%s" (buffer-string)))
5740 (tramp-send-string vec
"y"))))
5742 (defun tramp-action-terminal (proc vec
)
5743 "Tell the remote host which terminal type to use.
5744 The terminal type can be configured with `tramp-terminal-type'."
5745 (tramp-message vec
5 "Setting `%s' as terminal type." tramp-terminal-type
)
5746 (with-current-buffer (tramp-get-connection-buffer vec
)
5747 (tramp-message vec
6 "\n%s" (buffer-string)))
5748 (tramp-send-string vec tramp-terminal-type
))
5750 (defun tramp-action-process-alive (proc vec
)
5751 "Check whether a process has finished."
5752 (unless (memq (process-status proc
) '(run open
))
5753 (throw 'tramp-action
'process-died
)))
5755 (defun tramp-action-out-of-band (proc vec
)
5756 "Check whether an out-of-band copy has finished."
5757 (cond ((and (memq (process-status proc
) '(stop exit
))
5758 (zerop (process-exit-status proc
)))
5759 (tramp-message vec
3 "Process has finished.")
5760 (throw 'tramp-action
'ok
))
5761 ((or (and (memq (process-status proc
) '(stop exit
))
5762 (not (zerop (process-exit-status proc
))))
5763 (memq (process-status proc
) '(signal)))
5764 ;; `scp' could have copied correctly, but set modes could have failed.
5765 ;; This can be ignored.
5766 (with-current-buffer (process-buffer proc
)
5767 (goto-char (point-min))
5768 (if (re-search-forward tramp-operation-not-permitted-regexp nil t
)
5770 (tramp-message vec
5 "'set mode' error ignored.")
5771 (tramp-message vec
3 "Process has finished.")
5772 (throw 'tramp-action
'ok
))
5773 (tramp-message vec
3 "Process has died.")
5774 (throw 'tramp-action
'process-died
))))
5777 ;; Functions for processing the actions.
5779 (defun tramp-process-one-action (proc vec actions
)
5780 "Wait for output from the shell and perform one action."
5781 (let (found todo item pattern action
)
5783 ;; Reread output once all actions have been performed.
5784 ;; Obviously, the output was not complete.
5785 (tramp-accept-process-output proc
1)
5788 (setq item
(pop todo
))
5789 (setq pattern
(format "\\(%s\\)\\'" (symbol-value (nth 0 item
))))
5790 (setq action
(nth 1 item
))
5792 vec
5 "Looking for regexp \"%s\" from remote shell" pattern
)
5793 (when (tramp-check-for-regexp proc pattern
)
5794 (tramp-message vec
5 "Call `%s'" (symbol-name action
))
5795 (setq found
(funcall action proc vec
)))))
5798 (defun tramp-process-actions (proc vec actions
&optional timeout
)
5799 "Perform actions until success or TIMEOUT."
5800 ;; Enable auth-sorce and password-cache.
5801 (tramp-set-connection-property proc
"first-password-request" t
)
5804 (tramp-message proc
3 "Waiting for prompts from remote shell")
5806 (catch 'tramp-action
5808 (with-timeout (timeout)
5809 (tramp-process-one-action proc vec actions
))
5810 (tramp-process-one-action proc vec actions
)))))
5811 (with-current-buffer (tramp-get-connection-buffer vec
)
5812 (tramp-message vec
6 "\n%s" (buffer-string)))
5813 (unless (eq exit
'ok
)
5814 (tramp-clear-passwd vec
)
5815 (tramp-error-with-buffer
5818 ((eq exit
'permission-denied
) "Permission denied")
5819 ((eq exit
'process-died
) "Process died")
5820 (t "Login failed"))))))
5822 ;; Utility functions.
5824 (defun tramp-accept-process-output (&optional proc timeout timeout-msecs
)
5825 "Like `accept-process-output' for Tramp processes.
5826 This is needed in order to hide `last-coding-system-used', which is set
5827 for process communication also."
5828 (with-current-buffer (process-buffer proc
)
5829 (tramp-message proc
10 "%s %s" proc
(process-status proc
))
5830 (let (buffer-read-only last-coding-system-used
)
5831 ;; Under Windows XP, accept-process-output doesn't return
5832 ;; sometimes. So we add an additional timeout.
5833 (with-timeout ((or timeout
1))
5834 (accept-process-output proc timeout timeout-msecs
)))
5835 (tramp-message proc
10 "\n%s" (buffer-string))))
5837 (defun tramp-check-for-regexp (proc regexp
)
5838 "Check whether REGEXP is contained in process buffer of PROC.
5839 Erase echoed commands if exists."
5840 (with-current-buffer (process-buffer proc
)
5841 (goto-char (point-min))
5843 ;; Check whether we need to remove echo output.
5844 (when (and (tramp-get-connection-property proc
"check-remote-echo" nil
)
5845 (re-search-forward tramp-echoed-echo-mark-regexp nil t
))
5846 (let ((begin (match-beginning 0)))
5847 (when (re-search-forward tramp-echoed-echo-mark-regexp nil t
)
5848 ;; Discard echo from remote output.
5849 (tramp-set-connection-property proc
"check-remote-echo" nil
)
5850 (tramp-message proc
5 "echo-mark found")
5852 (delete-region begin
(point))
5853 (goto-char (point-min)))))
5856 ;; No echo to be handled, now we can look for the regexp.
5857 (not (tramp-get-connection-property proc
"check-remote-echo" nil
))
5858 ;; Sometimes the echo is invisible.
5859 (not (re-search-forward tramp-echo-mark-marker nil t
)))
5860 (goto-char (point-min))
5861 (re-search-forward regexp nil t
))))
5863 (defun tramp-wait-for-regexp (proc timeout regexp
)
5864 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
5865 Expects the output of PROC to be sent to the current buffer. Returns
5866 the string that matched, or nil. Waits indefinitely if TIMEOUT is
5868 (with-current-buffer (process-buffer proc
)
5869 (let ((found (tramp-check-for-regexp proc regexp
))
5870 (start-time (current-time)))
5872 ;; Work around a bug in XEmacs 21, where the timeout
5873 ;; expires faster than it should. This degenerates
5874 ;; to polling for buggy XEmacsen, but oh, well.
5875 (while (and (not found
)
5876 (< (tramp-time-diff (current-time) start-time
)
5878 (with-timeout (timeout)
5880 (tramp-accept-process-output proc
1)
5881 (unless (memq (process-status proc
) '(run open
))
5882 (tramp-error-with-buffer
5883 nil proc
'file-error
"Process has died"))
5884 (setq found
(tramp-check-for-regexp proc regexp
))))))
5887 (tramp-accept-process-output proc
1)
5888 (unless (memq (process-status proc
) '(run open
))
5889 (tramp-error-with-buffer
5890 nil proc
'file-error
"Process has died"))
5891 (setq found
(tramp-check-for-regexp proc regexp
)))))
5892 (tramp-message proc
6 "\n%s" (buffer-string))
5896 proc
'file-error
"[[Regexp `%s' not found in %d secs]]"
5898 (tramp-error proc
'file-error
"[[Regexp `%s' not found]]" regexp
)))
5901 (defun tramp-barf-if-no-shell-prompt (proc timeout
&rest error-args
)
5902 "Wait for shell prompt and barf if none appears.
5903 Looks at process PROC to see if a shell prompt appears in TIMEOUT
5904 seconds. If not, it produces an error message with the given ERROR-ARGS."
5906 (tramp-wait-for-regexp
5909 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern
))
5910 (apply 'tramp-error-with-buffer nil proc
'file-error error-args
)))
5912 ;; We don't call `tramp-send-string' in order to hide the password
5913 ;; from the debug buffer, and because end-of-line handling of the
5915 (defun tramp-enter-password (proc)
5916 "Prompt for a password and send it to the remote end."
5917 (process-send-string
5918 proc
(concat (tramp-read-passwd proc
)
5919 (or (tramp-get-method-parameter
5920 tramp-current-method
5921 'tramp-password-end-of-line
)
5922 tramp-default-password-end-of-line
))))
5924 (defun tramp-open-connection-setup-interactive-shell (proc vec
)
5925 "Set up an interactive shell.
5926 Mainly sets the prompt and the echo correctly. PROC is the shell
5927 process to set up. VEC specifies the connection."
5928 (let ((tramp-end-of-output "$ "))
5929 ;; It is useful to set the prompt in the following command because
5930 ;; some people have a setting for $PS1 which /bin/sh doesn't know
5931 ;; about and thus /bin/sh will display a strange prompt. For
5932 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
5933 ;; display the current working directory but /bin/sh will display
5934 ;; a dollar sign. The following command line sets $PS1 to a sane
5935 ;; value, and works under Bourne-ish shells as well as csh-like
5936 ;; shells. Daniel Pittman reports that the unusual positioning of
5937 ;; the single quotes makes it work under `rc', too. We also unset
5938 ;; the variable $ENV because that is read by some sh
5939 ;; implementations (eg, bash when called as sh) on startup; this
5940 ;; way, we avoid the startup file clobbering $PS1. $PROMP_COMMAND
5941 ;; is another way to set the prompt in /bin/bash, it must be
5942 ;; discarded as well.
5946 "exec env ENV='' PROMPT_COMMAND='' PS1='$ ' PS2='' PS3='' %s"
5947 (tramp-get-method-parameter
5948 (tramp-file-name-method vec
) 'tramp-remote-sh
))
5952 (tramp-message vec
5 "Setting up remote shell environment")
5953 (tramp-send-command vec
"stty -inlcr -echo kill '^U' erase '^H'" t
)
5954 ;; Check whether the echo has really been disabled. Some
5955 ;; implementations, like busybox of embedded GNU/Linux, don't
5956 ;; support disabling.
5957 (tramp-send-command vec
"echo foo" t
)
5958 (with-current-buffer (process-buffer proc
)
5959 (goto-char (point-min))
5960 (when (looking-at "echo foo")
5961 (tramp-set-connection-property proc
"remote-echo" t
)
5962 (tramp-message vec
5 "Remote echo still on. Ok.")
5963 ;; Make sure backspaces and their echo are enabled and no line
5964 ;; width magic interferes with them.
5965 (tramp-send-command vec
"stty icanon erase ^H cols 32767" t
))))
5967 (tramp-message vec
5 "Setting shell prompt")
5968 ;; We can set $PS1 to `tramp-end-of-output' only when the echo has
5969 ;; been disabled. Otherwise, the echo of the command would be
5970 ;; regarded as prompt already.
5971 (tramp-send-command vec
(format "PS1='%s'" tramp-end-of-output
) t
)
5972 (tramp-send-command vec
"PS2=''" t
)
5973 (tramp-send-command vec
"PS3=''" t
)
5974 (tramp-send-command vec
"PROMPT_COMMAND=''" t
)
5976 ;; Try to set up the coding system correctly.
5977 ;; CCC this can't be the right way to do it. Hm.
5978 (tramp-message vec
5 "Determining coding system")
5979 (tramp-send-command vec
"echo foo ; echo bar" t
)
5980 (with-current-buffer (process-buffer proc
)
5981 (goto-char (point-min))
5982 (if (featurep 'mule
)
5983 ;; Use MULE to select the right EOL convention for communicating
5984 ;; with the process.
5985 (let* ((cs (or (funcall (symbol-function 'process-coding-system
) proc
)
5986 (cons 'undecided
'undecided
)))
5987 cs-decode cs-encode
)
5988 (when (symbolp cs
) (setq cs
(cons cs cs
)))
5989 (setq cs-decode
(car cs
))
5990 (setq cs-encode
(cdr cs
))
5991 (unless cs-decode
(setq cs-decode
'undecided
))
5992 (unless cs-encode
(setq cs-encode
'undecided
))
5993 (setq cs-encode
(tramp-coding-system-change-eol-conversion
5995 (when (search-forward "\r" nil t
)
5996 (setq cs-decode
(tramp-coding-system-change-eol-conversion
5998 (funcall (symbol-function 'set-buffer-process-coding-system
)
5999 cs-decode cs-encode
))
6000 ;; Look for ^M and do something useful if found.
6001 (when (search-forward "\r" nil t
)
6002 ;; We have found a ^M but cannot frob the process coding system
6003 ;; because we're running on a non-MULE Emacs. Let's try
6005 (tramp-send-command vec
"stty -onlcr" t
))))
6006 (tramp-send-command vec
"set +o vi +o emacs" t
)
6008 ;; Check whether the output of "uname -sr" has been changed. If
6009 ;; yes, this is a strong indication that we must expire all
6010 ;; connection properties. We start again with
6011 ;; `tramp-maybe-open-connection', it will be catched there.
6012 (tramp-message vec
5 "Checking system information")
6013 (let ((old-uname (tramp-get-connection-property vec
"uname" nil
))
6015 (tramp-set-connection-property
6017 (tramp-send-command-and-read vec
"echo \\\"`uname -sr`\\\""))))
6018 (when (and (stringp old-uname
) (not (string-equal old-uname new-uname
)))
6019 (with-current-buffer (tramp-get-debug-buffer vec
)
6020 ;; Keep the debug buffer
6022 (generate-new-buffer-name tramp-temp-buffer-name
) 'unique
)
6023 (funcall (symbol-function 'tramp-cleanup-connection
) vec
)
6024 (if (= (point-min) (point-max))
6026 (rename-buffer (tramp-debug-buffer-name vec
) 'unique
))
6027 ;; We call `tramp-get-buffer' in order to keep the debug buffer.
6028 (tramp-get-buffer vec
)
6031 "Connection reset, because remote host changed from `%s' to `%s'"
6032 old-uname new-uname
)
6033 (throw 'uname-changed
(tramp-maybe-open-connection vec
)))))
6035 ;; Check whether the remote host suffers from buggy
6036 ;; `send-process-string'. This is known for FreeBSD (see comment in
6037 ;; `send_process', file process.c). I've tested sending 624 bytes
6038 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
6039 ;; this host type is detected locally. It cannot handle remote
6041 (with-connection-property proc
"chunksize"
6043 ((and (integerp tramp-chunksize
) (> tramp-chunksize
0))
6047 vec
5 "Checking remote host type for `send-process-string' bug")
6049 "^FreeBSD" (tramp-get-connection-property vec
"uname" ""))
6052 ;; Set remote PATH variable.
6053 (tramp-set-remote-path vec
)
6055 ;; Search for a good shell before searching for a command which
6056 ;; checks if a file exists. This is done because Tramp wants to use
6057 ;; "test foo; echo $?" to check if various conditions hold, and
6058 ;; there are buggy /bin/sh implementations which don't execute the
6059 ;; "echo $?" part if the "test" part has an error. In particular,
6060 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
6061 ;; with buggy /bin/sh implementations will have a working bash or
6063 (tramp-find-shell vec
)
6065 ;; Disable unexpected output.
6066 (tramp-send-command vec
"mesg n; biff n" t
)
6068 ;; Set the environment.
6069 (tramp-message vec
5 "Setting default environment")
6070 (let ((env (copy-sequence tramp-remote-process-environment
))
6073 (setq item
(split-string (car env
) "="))
6074 (if (and (stringp (cadr item
)) (not (string-equal (cadr item
) "")))
6076 vec
(format "%s=%s; export %s" (car item
) (cadr item
) (car item
)) t
)
6077 (push (car item
) unset
))
6078 (setq env
(cdr env
)))
6081 vec
(format "unset %s" (mapconcat 'identity unset
" "))))) t
)
6083 ;; CCC: We should either implement a Perl version of base64 encoding
6084 ;; and decoding. Then we just use that in the last item. The other
6085 ;; alternative is to use the Perl version of UU encoding. But then
6086 ;; we need a Lisp version of uuencode.
6088 ;; Old text from documentation of tramp-methods:
6089 ;; Using a uuencode/uudecode inline method is discouraged, please use one
6090 ;; of the base64 methods instead since base64 encoding is much more
6091 ;; reliable and the commands are more standardized between the different
6092 ;; Unix versions. But if you can't use base64 for some reason, please
6093 ;; note that the default uudecode command does not work well for some
6094 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
6095 ;; the following command for uudecode:
6097 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
6099 ;; For Irix, no solution is known yet.
6101 (defconst tramp-local-coding-commands
6102 '((b64 base64-encode-region base64-decode-region
)
6103 (uu tramp-uuencode-region uudecode-decode-region
)
6105 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
6106 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
6107 "List of local coding commands for inline transfer.
6108 Each item is a list that looks like this:
6110 \(FORMAT ENCODING DECODING)
6112 FORMAT is symbol describing the encoding/decoding format. It can be
6113 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
6115 ENCODING and DECODING can be strings, giving commands, or symbols,
6116 giving functions. If they are strings, then they can contain
6117 the \"%s\" format specifier. If that specifier is present, the input
6118 filename will be put into the command line at that spot. If the
6119 specifier is not present, the input should be read from standard
6122 If they are functions, they will be called with two arguments, start
6123 and end of region, and are expected to replace the region contents
6124 with the encoded or decoded results, respectively.")
6126 (defconst tramp-remote-coding-commands
6127 '((b64 "mimencode -b" "mimencode -u -b")
6128 (b64 "mmencode -b" "mmencode -u -b")
6129 (b64 "recode data..base64" "recode base64..data")
6130 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module
)
6131 (b64 tramp-perl-encode tramp-perl-decode
)
6132 (uu "uuencode xxx" "uudecode -o /dev/stdout")
6133 (uu "uuencode xxx" "uudecode -o -")
6134 (uu "uuencode xxx" "uudecode -p")
6135 (uu "uuencode xxx" tramp-uudecode
)
6137 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
6138 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
6139 "List of remote coding commands for inline transfer.
6140 Each item is a list that looks like this:
6142 \(FORMAT ENCODING DECODING)
6144 FORMAT is symbol describing the encoding/decoding format. It can be
6145 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
6147 ENCODING and DECODING can be strings, giving commands, or symbols,
6148 giving variables. If they are strings, then they can contain
6149 the \"%s\" format specifier. If that specifier is present, the input
6150 filename will be put into the command line at that spot. If the
6151 specifier is not present, the input should be read from standard
6154 If they are variables, this variable is a string containing a Perl
6155 implementation for this functionality. This Perl program will be transferred
6156 to the remote host, and it is avalible as shell function with the same name.")
6158 (defun tramp-find-inline-encoding (vec)
6159 "Find an inline transfer encoding that works.
6160 Goes through the list `tramp-local-coding-commands' and
6161 `tramp-remote-coding-commands'."
6163 (let ((local-commands tramp-local-coding-commands
)
6165 loc-enc loc-dec rem-enc rem-dec litem ritem found
)
6166 (while (and local-commands
(not found
))
6167 (setq litem
(pop local-commands
))
6168 (catch 'wont-work-local
6169 (let ((format (nth 0 litem
))
6170 (remote-commands tramp-remote-coding-commands
))
6171 (setq loc-enc
(nth 1 litem
))
6172 (setq loc-dec
(nth 2 litem
))
6173 ;; If the local encoder or decoder is a string, the
6174 ;; corresponding command has to work locally.
6175 (if (not (stringp loc-enc
))
6177 vec
5 "Checking local encoding function `%s'" loc-enc
)
6179 vec
5 "Checking local encoding command `%s' for sanity" loc-enc
)
6180 (unless (zerop (tramp-call-local-coding-command
6182 (throw 'wont-work-local nil
)))
6183 (if (not (stringp loc-dec
))
6185 vec
5 "Checking local decoding function `%s'" loc-dec
)
6187 vec
5 "Checking local decoding command `%s' for sanity" loc-dec
)
6188 (unless (zerop (tramp-call-local-coding-command
6190 (throw 'wont-work-local nil
)))
6191 ;; Search for remote coding commands with the same format
6192 (while (and remote-commands
(not found
))
6193 (setq ritem
(pop remote-commands
))
6194 (catch 'wont-work-remote
6195 (when (equal format
(nth 0 ritem
))
6196 (setq rem-enc
(nth 1 ritem
))
6197 (setq rem-dec
(nth 2 ritem
))
6198 ;; Check if remote encoding and decoding commands can be
6199 ;; called remotely with null input and output. This makes
6200 ;; sure there are no syntax errors and the command is really
6201 ;; found. Note that we do not redirect stdout to /dev/null,
6202 ;; for two reasons: when checking the decoding command, we
6203 ;; actually check the output it gives. And also, when
6204 ;; redirecting "mimencode" output to /dev/null, then as root
6205 ;; it might change the permissions of /dev/null!
6206 (when (not (stringp rem-enc
))
6207 (let ((name (symbol-name rem-enc
)))
6208 (while (string-match (regexp-quote "-") name
)
6209 (setq name
(replace-match "_" nil t name
)))
6210 (tramp-maybe-send-script vec
(symbol-value rem-enc
) name
)
6211 (setq rem-enc name
)))
6214 "Checking remote encoding command `%s' for sanity" rem-enc
)
6215 (unless (zerop (tramp-send-command-and-check
6216 vec
(format "%s </dev/null" rem-enc
) t
))
6217 (throw 'wont-work-remote nil
))
6219 (when (not (stringp rem-dec
))
6220 (let ((name (symbol-name rem-dec
)))
6221 (while (string-match (regexp-quote "-") name
)
6222 (setq name
(replace-match "_" nil t name
)))
6223 (tramp-maybe-send-script vec
(symbol-value rem-dec
) name
)
6224 (setq rem-dec name
)))
6227 "Checking remote decoding command `%s' for sanity" rem-dec
)
6228 (unless (zerop (tramp-send-command-and-check
6230 (format "echo %s | %s | %s"
6231 magic rem-enc rem-dec
) t
))
6232 (throw 'wont-work-remote nil
))
6234 (with-current-buffer (tramp-get-buffer vec
)
6235 (goto-char (point-min))
6236 (unless (looking-at (regexp-quote magic
))
6237 (throw 'wont-work-remote nil
)))
6239 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
6240 (setq rem-enc
(nth 1 ritem
))
6241 (setq rem-dec
(nth 2 ritem
))
6242 (setq found t
)))))))
6244 ;; Did we find something?
6246 (tramp-message vec
2 "Couldn't find an inline transfer encoding"))
6248 ;; Set connection properties.
6249 (tramp-message vec
5 "Using local encoding `%s'" loc-enc
)
6250 (tramp-set-connection-property vec
"local-encoding" loc-enc
)
6251 (tramp-message vec
5 "Using local decoding `%s'" loc-dec
)
6252 (tramp-set-connection-property vec
"local-decoding" loc-dec
)
6253 (tramp-message vec
5 "Using remote encoding `%s'" rem-enc
)
6254 (tramp-set-connection-property vec
"remote-encoding" rem-enc
)
6255 (tramp-message vec
5 "Using remote decoding `%s'" rem-dec
)
6256 (tramp-set-connection-property vec
"remote-decoding" rem-dec
))))
6258 (defun tramp-call-local-coding-command (cmd input output
)
6259 "Call the local encoding or decoding command.
6260 If CMD contains \"%s\", provide input file INPUT there in command.
6261 Otherwise, INPUT is passed via standard input.
6262 INPUT can also be nil which means `/dev/null'.
6263 OUTPUT can be a string (which specifies a filename), or t (which
6264 means standard output and thus the current buffer), or nil (which
6266 (tramp-local-call-process
6267 tramp-encoding-shell
6268 (when (and input
(not (string-match "%s" cmd
))) input
)
6269 (if (eq output t
) t nil
)
6271 tramp-encoding-command-switch
6273 (if (string-match "%s" cmd
) (format cmd input
) cmd
)
6274 (if (stringp output
) (concat "> " output
) ""))))
6276 (defun tramp-compute-multi-hops (vec)
6277 "Expands VEC according to `tramp-default-proxies-alist'.
6278 Gateway hops are already opened."
6279 (let ((target-alist `(,vec
))
6280 (choices tramp-default-proxies-alist
)
6283 ;; Look for proxy hosts to be passed.
6285 (setq item
(pop choices
)
6289 (string-match (or (nth 0 item
) "")
6290 (or (tramp-file-name-host (car target-alist
)) ""))
6292 (string-match (or (nth 1 item
) "")
6293 (or (tramp-file-name-user (car target-alist
)) "")))
6295 ;; No more hops needed.
6297 ;; Replace placeholders.
6301 `((?u .
,(or (tramp-file-name-user (car target-alist
)) ""))
6302 (?h .
,(or (tramp-file-name-host (car target-alist
)) "")))))
6303 (with-parsed-tramp-file-name proxy l
6305 (add-to-list 'target-alist l
)
6306 ;; Start next search.
6307 (setq choices tramp-default-proxies-alist
)))))
6310 (when (and (boundp 'tramp-gw-tunnel-method
)
6311 (string-match (format
6313 (symbol-value 'tramp-gw-tunnel-method
)
6314 (symbol-value 'tramp-gw-socks-method
))
6315 (tramp-file-name-method (car target-alist
))))
6316 (let ((gw (pop target-alist
))
6317 (hop (pop target-alist
)))
6318 ;; Is the method prepared for gateways?
6319 (unless (tramp-get-method-parameter
6320 (tramp-file-name-method hop
) 'tramp-default-port
)
6323 "Method `%s' is not supported for gateway access."
6324 (tramp-file-name-method hop
)))
6325 ;; Add default port if needed.
6328 tramp-host-with-port-regexp
(tramp-file-name-host hop
))
6331 (tramp-file-name-host hop
) tramp-prefix-port-format
6333 (tramp-get-method-parameter
6334 (tramp-file-name-method hop
) 'tramp-default-port
)))))
6335 ;; Open the gateway connection.
6339 (tramp-file-name-method hop
) (tramp-file-name-user hop
)
6340 (funcall (symbol-function 'tramp-gw-open-connection
) vec gw hop
) nil
))
6341 ;; For the password prompt, we need the correct values.
6342 ;; Therefore, we must remember the gateway vector. But we
6343 ;; cannot do it as connection property, because it shouldn't
6344 ;; be persistent. And we have no started process yet either.
6345 (tramp-set-file-property (car target-alist
) "" "gateway" hop
)))
6347 ;; Foreign and out-of-band methods are not supported for multi-hops.
6348 (when (cdr target-alist
)
6349 (setq choices target-alist
)
6351 (setq item
(pop choices
))
6355 (tramp-get-method-parameter
6356 (tramp-file-name-method item
) 'tramp-login-program
))
6357 (tramp-get-method-parameter
6358 (tramp-file-name-method item
) 'tramp-copy-program
))
6361 "Method `%s' is not supported for multi-hops."
6362 (tramp-file-name-method item
)))))
6364 ;; In case the host name is not used for the remote shell
6365 ;; command, the user could be misguided by applying a random
6367 (let* ((v (car target-alist
))
6368 (method (tramp-file-name-method v
))
6369 (host (tramp-file-name-host v
)))
6372 ;; There are multi-hops.
6374 ;; The host name is used for the remote shell command.
6376 '("%h") (tramp-get-method-parameter method
'tramp-login-args
))
6377 ;; The host is local. We cannot use `tramp-local-host-p'
6378 ;; here, because it opens a connection as well.
6379 (string-match tramp-local-host-regexp host
))
6382 "Host `%s' looks like a remote host, `%s' can only use the local host"
6388 (defun tramp-maybe-open-connection (vec)
6389 "Maybe open a connection VEC.
6390 Does not do anything if a connection is already open, but re-opens the
6391 connection if a previous connection has died for some reason."
6392 (catch 'uname-changed
6393 (let ((p (tramp-get-connection-process vec
))
6394 (process-environment (copy-sequence process-environment
)))
6396 ;; If too much time has passed since last command was sent, look
6397 ;; whether process is still alive. If it isn't, kill it. When
6398 ;; using ssh, it can sometimes happen that the remote end has
6399 ;; hung up but the local ssh client doesn't recognize this until
6400 ;; it tries to send some data to the remote end. So that's why
6401 ;; we try to send a command from time to time, then look again
6402 ;; whether the process is really alive.
6404 (when (and (> (tramp-time-diff
6406 (tramp-get-connection-property
6407 p
"last-cmd-time" '(0 0 0)))
6409 p
(processp p
) (memq (process-status p
) '(run open
)))
6410 (tramp-send-command vec
"echo are you awake" t t
)
6411 (unless (and (memq (process-status p
) '(run open
))
6412 (tramp-wait-for-output p
10))
6413 ;; The error will be catched locally.
6414 (tramp-error vec
'file-error
"Awake did fail")))
6416 (tramp-flush-connection-property vec
)
6417 (tramp-flush-connection-property p
)
6421 ;; New connection must be opened.
6422 (unless (and p
(processp p
) (memq (process-status p
) '(run open
)))
6424 ;; We call `tramp-get-buffer' in order to get a debug buffer for
6425 ;; messages from the beginning.
6426 (tramp-get-buffer vec
)
6427 (if (zerop (length (tramp-file-name-user vec
)))
6429 vec
3 "Opening connection for %s using %s..."
6430 (tramp-file-name-host vec
)
6431 (tramp-file-name-method vec
))
6433 vec
3 "Opening connection for %s@%s using %s..."
6434 (tramp-file-name-user vec
)
6435 (tramp-file-name-host vec
)
6436 (tramp-file-name-method vec
)))
6438 ;; Start new process.
6439 (when (and p
(processp p
))
6441 (setenv "TERM" tramp-terminal-type
)
6442 (setenv "LC_ALL" "C")
6443 (setenv "PROMPT_COMMAND")
6445 (let* ((target-alist (tramp-compute-multi-hops vec
))
6446 (process-connection-type tramp-process-connection-type
)
6447 (process-adaptive-read-buffering nil
)
6448 (coding-system-for-read nil
)
6449 ;; This must be done in order to avoid our file name handler.
6450 (p (let ((default-directory
6451 (tramp-compat-temporary-file-directory)))
6453 (or (tramp-get-connection-property vec
"process-name" nil
)
6454 (tramp-buffer-name vec
))
6455 (tramp-get-connection-buffer vec
)
6456 tramp-encoding-shell
)))
6460 vec
6 "%s" (mapconcat 'identity
(process-command p
) " "))
6462 ;; Check whether process is alive.
6463 (tramp-set-process-query-on-exit-flag p nil
)
6464 (tramp-message vec
3 "Waiting 60s for local shell to come up...")
6465 (tramp-barf-if-no-shell-prompt
6466 p
60 "Couldn't find local shell prompt %s" tramp-encoding-shell
)
6468 ;; Now do all the connections as specified.
6470 (let* ((hop (car target-alist
))
6471 (l-method (tramp-file-name-method hop
))
6472 (l-user (tramp-file-name-user hop
))
6473 (l-host (tramp-file-name-host hop
))
6476 (tramp-get-method-parameter l-method
'tramp-login-program
))
6478 (tramp-get-method-parameter l-method
'tramp-login-args
))
6480 (tramp-get-method-parameter l-method
'tramp-gw-args
))
6481 (gw (tramp-get-file-property hop
"" "gateway" nil
))
6482 (g-method (and gw
(tramp-file-name-method gw
)))
6483 (g-user (and gw
(tramp-file-name-user gw
)))
6484 (g-host (and gw
(tramp-file-name-host gw
)))
6485 (command login-program
)
6486 ;; We don't create the temporary file. In fact, it
6487 ;; is just a prefix for the ControlPath option of
6488 ;; ssh; the real temporary file has another name, and
6489 ;; it is created and protected by ssh. It is also
6490 ;; removed by ssh, when the connection is closed.
6492 (tramp-set-connection-property
6496 tramp-temp-name-prefix
6497 (tramp-compat-temporary-file-directory)))))
6500 ;; Add gateway arguments if necessary.
6501 (when (and gw gw-args
)
6502 (setq login-args
(append login-args gw-args
)))
6504 ;; Check for port number. Until now, there's no need
6505 ;; for handling like method, user, host.
6506 (when (string-match tramp-host-with-port-regexp l-host
)
6507 (setq l-port
(match-string 2 l-host
)
6508 l-host
(match-string 1 l-host
)))
6510 ;; Set variables for computing the prompt for reading
6511 ;; password. They can also be derived from a gateway.
6512 (setq tramp-current-method
(or g-method l-method
)
6513 tramp-current-user
(or g-user l-user
)
6514 tramp-current-host
(or g-host l-host
))
6516 ;; Replace login-args place holders.
6518 l-host
(or l-host
"")
6519 l-user
(or l-user
"")
6520 l-port
(or l-port
"")
6521 spec
`((?h .
,l-host
) (?u .
,l-user
) (?p .
,l-port
)
6528 (setq x
(mapcar '(lambda (y) (format-spec y spec
)) x
))
6529 (unless (member "" x
) (mapconcat 'identity x
" ")))
6531 ;; String to detect failed connection. Every single
6532 ;; word must be enclosed with '\"'; otherwise it is
6533 ;; detected during connection setup.
6534 ;; Local shell could be a Windows COMSPEC. It doesn't
6535 ;; know the ";" syntax, but we must exit always for
6536 ;; `start-process'. "exec" does not work either.
6539 "; echo \"Tramp\" \"connection\" \"closed\"; sleep 1"))
6540 ;; We don't reach a Windows shell. Could be initial only.
6543 ;; Send the command.
6544 (tramp-message vec
3 "Sending command `%s'" command
)
6545 (tramp-send-command vec command t t
)
6546 (tramp-process-actions p vec tramp-actions-before-shell
60)
6547 (tramp-message vec
3 "Found remote shell prompt on `%s'" l-host
))
6549 (setq target-alist
(cdr target-alist
)))
6551 ;; Make initial shell settings.
6552 (tramp-open-connection-setup-interactive-shell p vec
))))))
6554 (defun tramp-send-command (vec command
&optional neveropen nooutput
)
6555 "Send the COMMAND to connection VEC.
6556 Erases temporary buffer before sending the command. If optional
6557 arg NEVEROPEN is non-nil, never try to open the connection. This
6558 is meant to be used from `tramp-maybe-open-connection' only. The
6559 function waits for output unless NOOUTPUT is set."
6560 (unless neveropen
(tramp-maybe-open-connection vec
))
6561 (let ((p (tramp-get-connection-process vec
)))
6562 (when (tramp-get-connection-property p
"remote-echo" nil
)
6563 ;; We mark the command string that it can be erased in the output buffer.
6564 (tramp-set-connection-property p
"check-remote-echo" t
)
6565 (setq command
(format "%s%s%s" tramp-echo-mark command tramp-echo-mark
)))
6566 (tramp-message vec
6 "%s" command
)
6567 (tramp-send-string vec command
)
6568 (unless nooutput
(tramp-wait-for-output p
))))
6570 (defun tramp-wait-for-output (proc &optional timeout
)
6571 "Wait for output from remote rsh command."
6572 (with-current-buffer (process-buffer proc
)
6573 ;; Initially, `tramp-end-of-output' is "$ ". There might be
6574 ;; leading escape sequences, which must be ignored.
6576 (if (string-match (regexp-quote "\n") tramp-end-of-output
)
6578 'identity
(split-string tramp-end-of-output
"\n") "\r?\n")
6579 (format "^[^$\n]*%s\r?$" (regexp-quote tramp-end-of-output
))))
6580 (found (tramp-wait-for-regexp proc timeout regexp
)))
6582 (let (buffer-read-only)
6583 (goto-char (point-max))
6584 (re-search-backward regexp nil t
)
6585 (delete-region (point) (point-max)))
6589 "[[Remote prompt `%s' not found in %d secs]]"
6590 tramp-end-of-output timeout
)
6593 "[[Remote prompt `%s' not found]]" tramp-end-of-output
)))
6594 ;; Return value is whether end-of-output sentinel was found.
6597 (defun tramp-send-command-and-check (vec command
&optional subshell
)
6598 "Run COMMAND and check its exit status.
6599 Sends `echo $?' along with the COMMAND for checking the exit status. If
6600 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
6602 If the optional argument SUBSHELL is non-nil, the command is executed in
6603 a subshell, ie surrounded by parentheses."
6606 (concat (if subshell
"( " "")
6608 (if command
" 2>/dev/null; " "")
6609 "echo tramp_exit_status $?"
6610 (if subshell
" )" " ")))
6611 (with-current-buffer (tramp-get-connection-buffer vec
)
6612 (goto-char (point-max))
6613 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t
)
6615 vec
'file-error
"Couldn't find exit status of `%s'" command
))
6616 (skip-chars-forward "^ ")
6618 (read (current-buffer))
6619 (let (buffer-read-only) (delete-region (match-beginning 0) (point-max))))))
6621 (defun tramp-barf-unless-okay (vec command fmt
&rest args
)
6622 "Run COMMAND, check exit status, throw error if exit status not okay.
6623 Similar to `tramp-send-command-and-check' but accepts two more arguments
6624 FMT and ARGS which are passed to `error'."
6625 (unless (zerop (tramp-send-command-and-check vec command
))
6626 (apply 'tramp-error vec
'file-error fmt args
)))
6628 (defun tramp-send-command-and-read (vec command
)
6629 "Run COMMAND and return the output, which must be a Lisp expression.
6630 In case there is no valid Lisp expression, it raises an error"
6631 (tramp-barf-unless-okay vec command
"`%s' returns with error" command
)
6632 (with-current-buffer (tramp-get-connection-buffer vec
)
6633 ;; Read the expression.
6634 (goto-char (point-min))
6636 (prog1 (read (current-buffer))
6638 (when (re-search-forward "\\S-" (tramp-compat-line-end-position) t
)
6642 "`%s' does not return a valid Lisp expression: `%s'"
6643 command
(buffer-string))))))
6645 ;; It seems that Tru64 Unix does not like it if long strings are sent
6646 ;; to it in one go. (This happens when sending the Perl
6647 ;; `file-attributes' implementation, for instance.) Therefore, we
6648 ;; have this function which sends the string in chunks.
6649 (defun tramp-send-string (vec string
)
6650 "Send the STRING via connection VEC.
6652 The STRING is expected to use Unix line-endings, but the lines sent to
6653 the remote host use line-endings as defined in the variable
6654 `tramp-rsh-end-of-line'. The communication buffer is erased before sending."
6655 (let* ((p (tramp-get-connection-process vec
))
6656 (chunksize (tramp-get-connection-property p
"chunksize" nil
)))
6659 vec
'file-error
"Can't send string to remote host -- not logged in"))
6660 (tramp-set-connection-property p
"last-cmd-time" (current-time))
6661 (tramp-message vec
10 "%s" string
)
6662 (with-current-buffer (tramp-get-connection-buffer vec
)
6663 ;; Clean up the buffer. We cannot call `erase-buffer' because
6664 ;; narrowing might be in effect.
6665 (let (buffer-read-only) (delete-region (point-min) (point-max)))
6666 ;; Replace "\n" by `tramp-rsh-end-of-line'.
6668 (mapconcat 'identity
6669 (split-string string
"\n")
6670 tramp-rsh-end-of-line
))
6671 (unless (or (string= string
"")
6672 (string-equal (substring string -
1) tramp-rsh-end-of-line
))
6673 (setq string
(concat string tramp-rsh-end-of-line
)))
6675 (if (and chunksize
(not (zerop chunksize
)))
6677 (end (length string
)))
6680 vec
10 "Sending chunk from %s to %s"
6681 pos
(min (+ pos chunksize
) end
))
6682 (process-send-string
6683 p
(substring string pos
(min (+ pos chunksize
) end
)))
6684 (setq pos
(+ pos chunksize
))))
6685 (process-send-string p string
)))))
6687 (defun tramp-mode-string-to-int (mode-string)
6688 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
6689 (let* (case-fold-search
6690 (mode-chars (string-to-vector mode-string
))
6691 (owner-read (aref mode-chars
1))
6692 (owner-write (aref mode-chars
2))
6693 (owner-execute-or-setid (aref mode-chars
3))
6694 (group-read (aref mode-chars
4))
6695 (group-write (aref mode-chars
5))
6696 (group-execute-or-setid (aref mode-chars
6))
6697 (other-read (aref mode-chars
7))
6698 (other-write (aref mode-chars
8))
6699 (other-execute-or-sticky (aref mode-chars
9)))
6703 ((char-equal owner-read ?r
) (tramp-octal-to-decimal "00400"))
6704 ((char-equal owner-read ?-
) 0)
6705 (t (error "Second char `%c' must be one of `r-'" owner-read
)))
6707 ((char-equal owner-write ?w
) (tramp-octal-to-decimal "00200"))
6708 ((char-equal owner-write ?-
) 0)
6709 (t (error "Third char `%c' must be one of `w-'" owner-write
)))
6711 ((char-equal owner-execute-or-setid ?x
)
6712 (tramp-octal-to-decimal "00100"))
6713 ((char-equal owner-execute-or-setid ?S
)
6714 (tramp-octal-to-decimal "04000"))
6715 ((char-equal owner-execute-or-setid ?s
)
6716 (tramp-octal-to-decimal "04100"))
6717 ((char-equal owner-execute-or-setid ?-
) 0)
6718 (t (error "Fourth char `%c' must be one of `xsS-'"
6719 owner-execute-or-setid
)))
6721 ((char-equal group-read ?r
) (tramp-octal-to-decimal "00040"))
6722 ((char-equal group-read ?-
) 0)
6723 (t (error "Fifth char `%c' must be one of `r-'" group-read
)))
6725 ((char-equal group-write ?w
) (tramp-octal-to-decimal "00020"))
6726 ((char-equal group-write ?-
) 0)
6727 (t (error "Sixth char `%c' must be one of `w-'" group-write
)))
6729 ((char-equal group-execute-or-setid ?x
)
6730 (tramp-octal-to-decimal "00010"))
6731 ((char-equal group-execute-or-setid ?S
)
6732 (tramp-octal-to-decimal "02000"))
6733 ((char-equal group-execute-or-setid ?s
)
6734 (tramp-octal-to-decimal "02010"))
6735 ((char-equal group-execute-or-setid ?-
) 0)
6736 (t (error "Seventh char `%c' must be one of `xsS-'"
6737 group-execute-or-setid
)))
6739 ((char-equal other-read ?r
)
6740 (tramp-octal-to-decimal "00004"))
6741 ((char-equal other-read ?-
) 0)
6742 (t (error "Eighth char `%c' must be one of `r-'" other-read
)))
6744 ((char-equal other-write ?w
) (tramp-octal-to-decimal "00002"))
6745 ((char-equal other-write ?-
) 0)
6746 (t (error "Nineth char `%c' must be one of `w-'" other-write
)))
6748 ((char-equal other-execute-or-sticky ?x
)
6749 (tramp-octal-to-decimal "00001"))
6750 ((char-equal other-execute-or-sticky ?T
)
6751 (tramp-octal-to-decimal "01000"))
6752 ((char-equal other-execute-or-sticky ?t
)
6753 (tramp-octal-to-decimal "01001"))
6754 ((char-equal other-execute-or-sticky ?-
) 0)
6755 (t (error "Tenth char `%c' must be one of `xtT-'"
6756 other-execute-or-sticky
)))))))
6758 (defun tramp-convert-file-attributes (vec attr
)
6759 "Convert file-attributes ATTR generated by perl script, stat or ls.
6760 Convert file mode bits to string and set virtual device number.
6762 ;; Convert last access time.
6763 (unless (listp (nth 4 attr
))
6764 (setcar (nthcdr 4 attr
)
6765 (list (floor (nth 4 attr
) 65536)
6766 (floor (mod (nth 4 attr
) 65536)))))
6767 ;; Convert last modification time.
6768 (unless (listp (nth 5 attr
))
6769 (setcar (nthcdr 5 attr
)
6770 (list (floor (nth 5 attr
) 65536)
6771 (floor (mod (nth 5 attr
) 65536)))))
6772 ;; Convert last status change time.
6773 (unless (listp (nth 6 attr
))
6774 (setcar (nthcdr 6 attr
)
6775 (list (floor (nth 6 attr
) 65536)
6776 (floor (mod (nth 6 attr
) 65536)))))
6777 ;; Convert file size.
6778 (when (< (nth 7 attr
) 0)
6779 (setcar (nthcdr 7 attr
) -
1))
6780 (when (and (floatp (nth 7 attr
))
6781 (<= (nth 7 attr
) (tramp-compat-most-positive-fixnum)))
6782 (setcar (nthcdr 7 attr
) (round (nth 7 attr
))))
6783 ;; Convert file mode bits to string.
6784 (unless (stringp (nth 8 attr
))
6785 (setcar (nthcdr 8 attr
) (tramp-file-mode-from-int (nth 8 attr
)))
6786 (when (stringp (car attr
))
6787 (aset (nth 8 attr
) 0 ?l
)))
6788 ;; Convert directory indication bit.
6789 (when (string-match "^d" (nth 8 attr
))
6791 ;; Convert symlink from `tramp-handle-file-attributes-with-stat'.
6792 (when (consp (car attr
))
6793 (if (and (stringp (caar attr
))
6794 (string-match ".+ -> .\\(.+\\)." (caar attr
)))
6795 (setcar attr
(match-string 1 (caar attr
)))
6797 ;; Set file's gid change bit.
6798 (setcar (nthcdr 9 attr
)
6799 (if (numberp (nth 3 attr
))
6800 (not (= (nth 3 attr
)
6801 (tramp-get-remote-gid vec
'integer
)))
6804 (tramp-get-remote-gid vec
'string
)))))
6806 (unless (listp (nth 10 attr
))
6807 (setcar (nthcdr 10 attr
)
6809 (cons (floor (nth 10 attr
) 65536)
6810 (floor (mod (nth 10 attr
) 65536)))
6811 ;; Inodes can be incredible huge. We must hide this.
6812 (error (tramp-get-inode vec
)))))
6813 ;; Set virtual device number.
6814 (setcar (nthcdr 11 attr
)
6815 (tramp-get-device vec
))
6818 (defun tramp-get-inode (vec)
6819 "Returns the virtual inode number.
6820 If it doesn't exist, generate a new one."
6821 (let ((string (tramp-make-tramp-file-name
6822 (tramp-file-name-method vec
)
6823 (tramp-file-name-user vec
)
6824 (tramp-file-name-host vec
)
6826 (unless (assoc string tramp-inodes
)
6827 (add-to-list 'tramp-inodes
6828 (list string
(length tramp-inodes
))))
6829 (nth 1 (assoc string tramp-inodes
))))
6831 (defun tramp-get-device (vec)
6832 "Returns the virtual device number.
6833 If it doesn't exist, generate a new one."
6834 (let ((string (tramp-make-tramp-file-name
6835 (tramp-file-name-method vec
)
6836 (tramp-file-name-user vec
)
6837 (tramp-file-name-host vec
)
6839 (unless (assoc string tramp-devices
)
6840 (add-to-list 'tramp-devices
6841 (list string
(length tramp-devices
))))
6842 (cons -
1 (nth 1 (assoc string tramp-devices
)))))
6844 (defun tramp-file-mode-from-int (mode)
6845 "Turn an integer representing a file mode into an ls(1)-like string."
6846 (let ((type (cdr (assoc (logand (lsh mode -
12) 15) tramp-file-mode-type-map
)))
6847 (user (logand (lsh mode -
6) 7))
6848 (group (logand (lsh mode -
3) 7))
6849 (other (logand (lsh mode -
0) 7))
6850 (suid (> (logand (lsh mode -
9) 4) 0))
6851 (sgid (> (logand (lsh mode -
9) 2) 0))
6852 (sticky (> (logand (lsh mode -
9) 1) 0)))
6853 (setq user
(tramp-file-mode-permissions user suid
"s"))
6854 (setq group
(tramp-file-mode-permissions group sgid
"s"))
6855 (setq other
(tramp-file-mode-permissions other sticky
"t"))
6856 (concat type user group other
)))
6858 (defun tramp-file-mode-permissions (perm suid suid-text
)
6859 "Convert a permission bitset into a string.
6860 This is used internally by `tramp-file-mode-from-int'."
6861 (let ((r (> (logand perm
4) 0))
6862 (w (> (logand perm
2) 0))
6863 (x (> (logand perm
1) 0)))
6864 (concat (or (and r
"r") "-")
6865 (or (and w
"w") "-")
6866 (or (and suid x suid-text
) ; suid, execute
6867 (and suid
(upcase suid-text
)) ; suid, !execute
6868 (and x
"x") "-")))) ; !suid
6870 (defun tramp-decimal-to-octal (i)
6871 "Return a string consisting of the octal digits of I.
6872 Not actually used. Use `(format \"%o\" i)' instead?"
6873 (cond ((< i
0) (error "Cannot convert negative number to octal"))
6874 ((not (integerp i
)) (error "Cannot convert non-integer to octal"))
6876 (t (concat (tramp-decimal-to-octal (/ i
8))
6877 (number-to-string (% i
8))))))
6879 ;; Kudos to Gerd Moellmann for this suggestion.
6880 (defun tramp-octal-to-decimal (ostr)
6881 "Given a string of octal digits, return a decimal number."
6882 (let ((x (or ostr
"")))
6883 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
6884 (unless (string-match "\\`[0-7]*\\'" x
)
6885 (error "Non-octal junk in string `%s'" x
))
6886 (string-to-number ostr
8)))
6888 (defun tramp-shell-case-fold (string)
6889 "Converts STRING to shell glob pattern which ignores case."
6892 (if (equal (downcase c
) (upcase c
))
6894 (format "[%c%c]" (downcase c
) (upcase c
))))
6899 ;; ------------------------------------------------------------
6900 ;; -- Tramp file names --
6901 ;; ------------------------------------------------------------
6902 ;; Conversion functions between external representation and
6903 ;; internal data structure. Convenience functions for internal
6906 (defun tramp-file-name-p (vec)
6907 "Check whether VEC is a Tramp object."
6908 (and (vectorp vec
) (= 4 (length vec
))))
6910 (defun tramp-file-name-method (vec)
6911 "Return method component of VEC."
6912 (and (tramp-file-name-p vec
) (aref vec
0)))
6914 (defun tramp-file-name-user (vec)
6915 "Return user component of VEC."
6916 (and (tramp-file-name-p vec
) (aref vec
1)))
6918 (defun tramp-file-name-host (vec)
6919 "Return host component of VEC."
6920 (and (tramp-file-name-p vec
) (aref vec
2)))
6922 (defun tramp-file-name-localname (vec)
6923 "Return localname component of VEC."
6924 (and (tramp-file-name-p vec
) (aref vec
3)))
6926 ;; The user part of a Tramp file name vector can be of kind
6927 ;; "user%domain". Sometimes, we must extract these parts.
6928 (defun tramp-file-name-real-user (vec)
6929 "Return the user name of VEC without domain."
6930 (let ((user (tramp-file-name-user vec
)))
6931 (if (and (stringp user
)
6932 (string-match tramp-user-with-domain-regexp user
))
6933 (match-string 1 user
)
6936 (defun tramp-file-name-domain (vec)
6937 "Return the domain name of VEC."
6938 (let ((user (tramp-file-name-user vec
)))
6940 (string-match tramp-user-with-domain-regexp user
)
6941 (match-string 2 user
))))
6943 ;; The host part of a Tramp file name vector can be of kind
6944 ;; "host#port". Sometimes, we must extract these parts.
6945 (defun tramp-file-name-real-host (vec)
6946 "Return the host name of VEC without port."
6947 (let ((host (tramp-file-name-host vec
)))
6948 (if (and (stringp host
)
6949 (string-match tramp-host-with-port-regexp host
))
6950 (match-string 1 host
)
6953 (defun tramp-file-name-port (vec)
6954 "Return the port number of VEC."
6955 (let ((host (tramp-file-name-host vec
)))
6957 (string-match tramp-host-with-port-regexp host
)
6958 (string-to-number (match-string 2 host
)))))
6960 (defun tramp-tramp-file-p (name)
6961 "Return t if NAME is a Tramp file."
6963 (string-match tramp-file-name-regexp name
)))
6965 (defun tramp-find-method (method user host
)
6966 "Return the right method string to use.
6967 This is METHOD, if non-nil. Otherwise, do a lookup in
6968 `tramp-default-method-alist'."
6970 (let ((choices tramp-default-method-alist
)
6973 (setq item
(pop choices
))
6974 (when (and (string-match (or (nth 0 item
) "") (or host
""))
6975 (string-match (or (nth 1 item
) "") (or user
"")))
6976 (setq lmethod
(nth 2 item
))
6977 (setq choices nil
)))
6979 tramp-default-method
))
6981 (defun tramp-find-user (method user host
)
6982 "Return the right user string to use.
6983 This is USER, if non-nil. Otherwise, do a lookup in
6984 `tramp-default-user-alist'."
6986 (let ((choices tramp-default-user-alist
)
6989 (setq item
(pop choices
))
6990 (when (and (string-match (or (nth 0 item
) "") (or method
""))
6991 (string-match (or (nth 1 item
) "") (or host
"")))
6992 (setq luser
(nth 2 item
))
6993 (setq choices nil
)))
6995 tramp-default-user
))
6997 (defun tramp-find-host (method user host
)
6998 "Return the right host string to use.
6999 This is HOST, if non-nil. Otherwise, it is `tramp-default-host'."
7000 (or (and (> (length host
) 0) host
)
7001 tramp-default-host
))
7003 (defun tramp-dissect-file-name (name &optional nodefault
)
7004 "Return a `tramp-file-name' structure.
7005 The structure consists of remote method, remote user, remote host
7006 and localname (file name on remote host). If NODEFAULT is
7007 non-nil, the file name parts are not expanded to their default
7010 (let ((match (string-match (nth 0 tramp-file-name-structure
) name
)))
7011 (unless match
(error "Not a Tramp file name: %s" name
))
7012 (let ((method (match-string (nth 1 tramp-file-name-structure
) name
))
7013 (user (match-string (nth 2 tramp-file-name-structure
) name
))
7014 (host (match-string (nth 3 tramp-file-name-structure
) name
))
7015 (localname (match-string (nth 4 tramp-file-name-structure
) name
)))
7016 (when (member method
'("multi" "multiu"))
7018 "`%s' method is no longer supported, see (info \"(tramp)Multi-hops\")"
7021 (when (string-match tramp-prefix-ipv6-regexp host
)
7022 (setq host
(replace-match "" nil t host
)))
7023 (when (string-match tramp-postfix-ipv6-regexp host
)
7024 (setq host
(replace-match "" nil t host
))))
7026 (vector method user host localname
)
7028 (tramp-find-method method user host
)
7029 (tramp-find-user method user host
)
7030 (tramp-find-host method user host
)
7033 (defun tramp-equal-remote (file1 file2
)
7034 "Checks, whether the remote parts of FILE1 and FILE2 are identical.
7035 The check depends on method, user and host name of the files. If
7036 one of the components is missing, the default values are used.
7037 The local file name parts of FILE1 and FILE2 are not taken into
7042 (tramp-equal-remote \"/ssh::/etc\" \"/<your host name>:/home\")
7044 would yield `t'. On the other hand, the following check results in nil:
7046 (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")"
7047 (and (stringp (file-remote-p file1
))
7048 (stringp (file-remote-p file2
))
7049 (string-equal (file-remote-p file1
) (file-remote-p file2
))))
7051 (defun tramp-make-tramp-file-name (method user host localname
)
7052 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME."
7053 (concat tramp-prefix-format
7054 (when (not (zerop (length method
)))
7055 (concat method tramp-postfix-method-format
))
7056 (when (not (zerop (length user
)))
7057 (concat user tramp-postfix-user-format
))
7059 (if (string-match tramp-ipv6-regexp host
)
7060 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format
)
7062 tramp-postfix-host-format
7063 (when localname localname
)))
7065 (defun tramp-completion-make-tramp-file-name (method user host localname
)
7066 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME.
7067 It must not be a complete Tramp file name, but as long as there are
7068 necessary only. This function will be used in file name completion."
7069 (concat tramp-prefix-format
7070 (when (not (zerop (length method
)))
7071 (concat method tramp-postfix-method-format
))
7072 (when (not (zerop (length user
)))
7073 (concat user tramp-postfix-user-format
))
7074 (when (not (zerop (length host
)))
7076 (if (string-match tramp-ipv6-regexp host
)
7077 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format
)
7079 tramp-postfix-host-format
))
7080 (when localname localname
)))
7082 (defun tramp-make-copy-program-file-name (vec)
7083 "Create a file name suitable to be passed to `rcp' and workalikes."
7084 (let ((user (tramp-file-name-user vec
))
7085 (host (tramp-file-name-real-host vec
))
7086 (localname (tramp-shell-quote-argument
7087 (tramp-file-name-localname vec
))))
7088 (if (not (zerop (length user
)))
7089 (format "%s@%s:%s" user host localname
)
7090 (format "%s:%s" host localname
))))
7092 (defun tramp-method-out-of-band-p (vec)
7093 "Return t if this is an out-of-band method, nil otherwise."
7094 (tramp-get-method-parameter (tramp-file-name-method vec
) 'tramp-copy-program
))
7096 (defun tramp-local-host-p (vec)
7097 "Return t if this points to the local host, nil otherwise."
7098 ;; We cannot use `tramp-file-name-real-host'. A port is an
7099 ;; indication for an ssh tunnel or alike.
7100 (let ((host (tramp-file-name-host vec
)))
7103 (string-match tramp-local-host-regexp host
)
7104 ;; The local temp directory must be writable for the other user.
7106 (tramp-make-tramp-file-name
7107 (tramp-file-name-method vec
)
7108 (tramp-file-name-user vec
)
7110 (tramp-compat-temporary-file-directory))))))
7112 ;; Variables local to connection.
7114 (defun tramp-get-remote-path (vec)
7115 (with-connection-property vec
"remote-path"
7116 (let* ((remote-path (tramp-compat-copy-tree tramp-remote-path
))
7117 (elt (memq 'tramp-default-remote-path remote-path
))
7118 (default-remote-path
7122 (tramp-send-command-and-read vec
"getconf PATH"))
7123 ;; Default if "getconf" is not available.
7127 "`getconf PATH' not successful, using default value \"%s\"."
7129 "/bin:/usr/bin")))))
7131 ;; Replace place holder `tramp-default-remote-path'.
7134 (tramp-split-string default-remote-path
":")
7136 (setq remote-path
(delq 'tramp-default-remote-path remote-path
)))
7138 ;; Remove non-existing directories.
7144 (with-connection-property vec x
7146 (tramp-make-tramp-file-name
7147 (tramp-file-name-method vec
)
7148 (tramp-file-name-user vec
)
7149 (tramp-file-name-host vec
)
7154 (defun tramp-get-remote-tmpdir (vec)
7155 (with-connection-property vec
"tmp-directory"
7156 (let ((dir (tramp-shell-quote-argument "/tmp")))
7158 (tramp-send-command-and-check
7159 vec
(format "%s -d %s" (tramp-get-test-command vec
) dir
)))
7161 (tramp-send-command-and-check
7162 vec
(format "%s -w %s" (tramp-get-test-command vec
) dir
))))
7164 (tramp-error vec
'file-error
"Directory %s not accessible" dir
)))))
7166 (defun tramp-get-ls-command (vec)
7167 (with-connection-property vec
"ls"
7168 (with-current-buffer (tramp-get-buffer vec
)
7169 (tramp-message vec
5 "Finding a suitable `ls' command")
7172 (dolist (cmd '("ls" "gnuls" "gls"))
7173 (let ((dl (tramp-get-remote-path vec
))
7179 (tramp-find-executable vec cmd dl t t
)))
7181 (when (zerop (tramp-send-command-and-check
7182 vec
(format "%s -lnd /" result
)))
7183 (throw 'ls-found result
))
7184 (setq dl
(cdr dl
))))))
7185 (tramp-error vec
'file-error
"Couldn't find a proper `ls' command")))))
7187 (defun tramp-get-test-command (vec)
7188 (with-connection-property vec
"test"
7189 (with-current-buffer (tramp-get-buffer vec
)
7190 (tramp-message vec
5 "Finding a suitable `test' command")
7191 (if (zerop (tramp-send-command-and-check vec
"test 0"))
7193 (tramp-find-executable vec
"test" (tramp-get-remote-path vec
))))))
7195 (defun tramp-get-test-nt-command (vec)
7196 ;; Does `test A -nt B' work? Use abominable `find' construct if it
7197 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
7198 ;; for otherwise the shell crashes.
7199 (with-connection-property vec
"test-nt"
7203 vec
(format "( %s / -nt / )" (tramp-get-test-command vec
)))
7204 (with-current-buffer (tramp-get-buffer vec
)
7205 (goto-char (point-min))
7206 (when (looking-at (regexp-quote tramp-end-of-output
))
7207 (format "%s %%s -nt %%s" (tramp-get-test-command vec
)))))
7212 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
7213 (tramp-get-test-command vec
)))
7214 "tramp_test_nt %s %s"))))
7216 (defun tramp-get-file-exists-command (vec)
7217 (with-connection-property vec
"file-exists"
7218 (with-current-buffer (tramp-get-buffer vec
)
7219 (tramp-message vec
5 "Finding command to check if file exists")
7220 (tramp-find-file-exists-command vec
))))
7222 (defun tramp-get-remote-ln (vec)
7223 (with-connection-property vec
"ln"
7224 (with-current-buffer (tramp-get-buffer vec
)
7225 (tramp-message vec
5 "Finding a suitable `ln' command")
7226 (tramp-find-executable vec
"ln" (tramp-get-remote-path vec
)))))
7228 (defun tramp-get-remote-perl (vec)
7229 (with-connection-property vec
"perl"
7230 (with-current-buffer (tramp-get-buffer vec
)
7231 (tramp-message vec
5 "Finding a suitable `perl' command")
7232 (or (tramp-find-executable vec
"perl5" (tramp-get-remote-path vec
))
7233 (tramp-find-executable vec
"perl" (tramp-get-remote-path vec
))))))
7235 (defun tramp-get-remote-stat (vec)
7236 (with-connection-property vec
"stat"
7237 (with-current-buffer (tramp-get-buffer vec
)
7238 (tramp-message vec
5 "Finding a suitable `stat' command")
7239 (let ((result (tramp-find-executable
7240 vec
"stat" (tramp-get-remote-path vec
)))
7242 ;; Check whether stat(1) returns usable syntax. %s does not
7243 ;; work on older AIX systems.
7246 ;; We don't want to display an error message.
7247 (with-temp-message (or (current-message) "")
7249 (tramp-send-command-and-read
7250 vec
(format "%s -c '(\"%%N\" %%s)' /" result
))
7252 (unless (and (listp tmp
) (stringp (car tmp
))
7253 (string-match "^./.$" (car tmp
))
7254 (integerp (cadr tmp
)))
7258 (defun tramp-get-remote-id (vec)
7259 (with-connection-property vec
"id"
7260 (with-current-buffer (tramp-get-buffer vec
)
7261 (tramp-message vec
5 "Finding POSIX `id' command")
7264 (let ((dl (tramp-get-remote-path vec
))
7270 (tramp-find-executable vec
"id" dl t t
)))
7271 ;; Check POSIX parameter.
7272 (when (zerop (tramp-send-command-and-check
7273 vec
(format "%s -u" result
)))
7274 (throw 'id-found result
))
7275 (setq dl
(cdr dl
)))))
7276 (tramp-error vec
'file-error
"Couldn't find a POSIX `id' command")))))
7278 (defun tramp-get-remote-uid (vec id-format
)
7279 (with-connection-property vec
(format "uid-%s" id-format
)
7280 (let ((res (tramp-send-command-and-read
7282 (format "%s -u%s %s"
7283 (tramp-get-remote-id vec
)
7284 (if (equal id-format
'integer
) "" "n")
7285 (if (equal id-format
'integer
)
7286 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
7287 ;; The command might not always return a number.
7288 (if (and (equal id-format
'integer
) (not (integerp res
))) -
1 res
))))
7290 (defun tramp-get-remote-gid (vec id-format
)
7291 (with-connection-property vec
(format "gid-%s" id-format
)
7292 (let ((res (tramp-send-command-and-read
7294 (format "%s -g%s %s"
7295 (tramp-get-remote-id vec
)
7296 (if (equal id-format
'integer
) "" "n")
7297 (if (equal id-format
'integer
)
7298 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
7299 ;; The command might not always return a number.
7300 (if (and (equal id-format
'integer
) (not (integerp res
))) -
1 res
))))
7302 (defun tramp-get-local-uid (id-format)
7303 (if (equal id-format
'integer
) (user-uid) (user-login-name)))
7305 (defun tramp-get-local-gid (id-format)
7306 (nth 3 (tramp-compat-file-attributes "~/" id-format
)))
7308 ;; Some predefined connection properties.
7309 (defun tramp-get-remote-coding (vec prop
)
7310 ;; Local coding handles properties like remote coding. So we could
7311 ;; call it without pain.
7312 (let ((ret (tramp-get-local-coding vec prop
)))
7313 ;; The connection property might have been cached. So we must send
7314 ;; the script - maybe.
7315 (when (and ret
(symbolp ret
))
7316 (let ((name (symbol-name ret
)))
7317 (while (string-match (regexp-quote "-") name
)
7318 (setq name
(replace-match "_" nil t name
)))
7319 (tramp-maybe-send-script vec
(symbol-value ret
) name
)
7321 ;; Return the value.
7324 (defun tramp-get-local-coding (vec prop
)
7326 (tramp-get-connection-property vec prop nil
)
7328 (tramp-find-inline-encoding vec
)
7329 (tramp-get-connection-property vec prop nil
))))
7331 (defun tramp-get-method-parameter (method param
)
7332 "Return the method parameter PARAM.
7333 If the `tramp-methods' entry does not exist, return NIL."
7334 (let ((entry (assoc param
(assoc method tramp-methods
))))
7335 (when entry
(cadr entry
))))
7337 ;; Auto saving to a special directory.
7339 (defun tramp-exists-file-name-handler (operation &rest args
)
7340 "Checks whether OPERATION runs a file name handler."
7341 ;; The file name handler is determined on base of either an
7342 ;; argument, `buffer-file-name', or `default-directory'.
7344 (let* ((buffer-file-name "/")
7345 (default-directory "/")
7346 (fnha file-name-handler-alist
)
7347 (check-file-name-operation operation
)
7348 (file-name-handler-alist
7351 '(lambda (operation &rest args
)
7352 "Returns OPERATION if it is the one to be checked."
7353 (if (equal check-file-name-operation operation
)
7355 (let ((file-name-handler-alist fnha
))
7356 (apply operation args
))))))))
7357 (equal (apply operation args
) operation
))
7360 (unless (tramp-exists-file-name-handler 'make-auto-save-file-name
)
7361 (defadvice make-auto-save-file-name
7362 (around tramp-advice-make-auto-save-file-name
() activate
)
7363 "Invoke `tramp-handle-make-auto-save-file-name' for Tramp files."
7364 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name)))
7365 (setq ad-return-value
(tramp-handle-make-auto-save-file-name))
7367 (add-hook 'tramp-unload-hook
7368 '(lambda () (ad-unadvise 'make-auto-save-file-name
))))
7370 ;; In Emacs < 22 and XEmacs < 21.5 autosaved remote files have
7371 ;; permission 0666 minus umask. This is a security threat.
7373 (defun tramp-set-auto-save-file-modes ()
7374 "Set permissions of autosaved remote files to the original permissions."
7375 (let ((bfn (buffer-file-name)))
7376 (when (and (stringp bfn
)
7377 (tramp-tramp-file-p bfn
)
7379 (stringp buffer-auto-save-file-name
)
7380 (not (equal bfn buffer-auto-save-file-name
)))
7381 (unless (file-exists-p buffer-auto-save-file-name
)
7382 (write-region "" nil buffer-auto-save-file-name
))
7383 ;; Permissions should be set always, because there might be an old
7384 ;; auto-saved file belonging to another original file. This could
7385 ;; be a security threat.
7386 (set-file-modes buffer-auto-save-file-name
7387 (or (file-modes bfn
) (tramp-octal-to-decimal "0600"))))))
7389 (unless (or (> emacs-major-version
21)
7390 (and (featurep 'xemacs
)
7391 (= emacs-major-version
21)
7392 (> emacs-minor-version
4)))
7393 (add-hook 'auto-save-hook
'tramp-set-auto-save-file-modes
)
7394 (add-hook 'tramp-unload-hook
7396 (remove-hook 'auto-save-hook
'tramp-set-auto-save-file-modes
))))
7398 (defun tramp-subst-strs-in-string (alist string
)
7399 "Replace all occurrences of the string FROM with TO in STRING.
7400 ALIST is of the form ((FROM . TO) ...)."
7403 (let* ((pr (car alist
))
7406 (while (string-match (regexp-quote from
) string
)
7407 (setq string
(replace-match to t t string
)))
7408 (setq alist
(cdr alist
))))
7411 ;; ------------------------------------------------------------
7412 ;; -- Compatibility functions section --
7413 ;; ------------------------------------------------------------
7415 (defun tramp-read-passwd (proc &optional prompt
)
7416 "Read a password from user (compat function).
7417 Consults the auth-source package.
7418 Invokes `password-read' if available, `read-passwd' else."
7419 (let* ((key (tramp-make-tramp-file-name
7420 tramp-current-method tramp-current-user
7421 tramp-current-host
""))
7424 (with-current-buffer (process-buffer proc
)
7425 (tramp-check-for-regexp proc tramp-password-prompt-regexp
)
7426 (format "%s for %s " (capitalize (match-string 1)) key
)))))
7429 ;; See if auth-sources contains something useful, if it's bound.
7430 (and (boundp 'auth-sources
)
7431 (tramp-get-connection-property proc
"first-password-request" nil
)
7432 ;; Try with Tramp's current method.
7433 (funcall (symbol-function 'auth-source-user-or-password
)
7434 "password" tramp-current-host tramp-current-method
))
7435 ;; Try the password cache.
7436 (when (functionp 'password-read
)
7437 (unless (tramp-get-connection-property
7438 proc
"first-password-request" nil
)
7439 (funcall (symbol-function 'password-cache-remove
) key
))
7441 (funcall (symbol-function 'password-read
) pw-prompt key
)))
7442 (funcall (symbol-function 'password-cache-add
) key password
)
7444 ;; Else, get the password interactively.
7445 (read-passwd pw-prompt
))
7446 (tramp-set-connection-property proc
"first-password-request" nil
))))
7448 (defun tramp-clear-passwd (vec)
7449 "Clear password cache for connection related to VEC."
7450 (when (functionp 'password-cache-remove
)
7452 (symbol-function 'password-cache-remove
)
7453 (tramp-make-tramp-file-name
7454 (tramp-file-name-method vec
)
7455 (tramp-file-name-user vec
)
7456 (tramp-file-name-host vec
)
7459 ;; Snarfed code from time-date.el and parse-time.el
7461 (defconst tramp-half-a-year
'(241 17024)
7462 "Evaluated by \"(days-to-time 183)\".")
7464 (defconst tramp-parse-time-months
7465 '(("jan" .
1) ("feb" .
2) ("mar" .
3)
7466 ("apr" .
4) ("may" .
5) ("jun" .
6)
7467 ("jul" .
7) ("aug" .
8) ("sep" .
9)
7468 ("oct" .
10) ("nov" .
11) ("dec" .
12))
7469 "Alist mapping month names to integers.")
7471 (defun tramp-time-less-p (t1 t2
)
7472 "Say whether time value T1 is less than time value T2."
7473 (unless t1
(setq t1
'(0 0)))
7474 (unless t2
(setq t2
'(0 0)))
7475 (or (< (car t1
) (car t2
))
7476 (and (= (car t1
) (car t2
))
7477 (< (nth 1 t1
) (nth 1 t2
)))))
7479 (defun tramp-time-subtract (t1 t2
)
7480 "Subtract two time values.
7481 Return the difference in the format of a time value."
7482 (unless t1
(setq t1
'(0 0)))
7483 (unless t2
(setq t2
'(0 0)))
7484 (let ((borrow (< (cadr t1
) (cadr t2
))))
7485 (list (- (car t1
) (car t2
) (if borrow
1 0))
7486 (- (+ (if borrow
65536 0) (cadr t1
)) (cadr t2
)))))
7488 (defun tramp-time-diff (t1 t2
)
7489 "Return the difference between the two times, in seconds.
7490 T1 and T2 are time values (as returned by `current-time' for example)."
7491 ;; Pacify byte-compiler with `symbol-function'.
7492 (cond ((and (fboundp 'subtract-time
)
7493 (fboundp 'float-time
))
7494 (funcall (symbol-function 'float-time
)
7495 (funcall (symbol-function 'subtract-time
) t1 t2
)))
7496 ((and (fboundp 'subtract-time
)
7497 (fboundp 'time-to-seconds
))
7498 (funcall (symbol-function 'time-to-seconds
)
7499 (funcall (symbol-function 'subtract-time
) t1 t2
)))
7500 ((fboundp 'itimer-time-difference
)
7501 (funcall (symbol-function 'itimer-time-difference
)
7502 (if (< (length t1
) 3) (append t1
'(0)) t1
)
7503 (if (< (length t2
) 3) (append t2
'(0)) t2
)))
7505 (let ((time (tramp-time-subtract t1 t2
)))
7506 (+ (* (car time
) 65536.0)
7508 (/ (or (nth 2 time
) 0) 1000000.0))))))
7510 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type
)
7511 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
7512 EOL-TYPE can be one of `dos', `unix', or `mac'."
7513 (cond ((fboundp 'coding-system-change-eol-conversion
)
7514 (funcall (symbol-function 'coding-system-change-eol-conversion
)
7515 coding-system eol-type
))
7516 ((fboundp 'subsidiary-coding-system
)
7517 (funcall (symbol-function 'subsidiary-coding-system
)
7519 (cond ((eq eol-type
'dos
) 'crlf
)
7520 ((eq eol-type
'unix
) 'lf
)
7521 ((eq eol-type
'mac
) 'cr
)
7523 (error "Unknown EOL-TYPE `%s', must be %s"
7525 "`dos', `unix', or `mac'")))))
7526 (t (error "Can't change EOL conversion -- is MULE missing?"))))
7528 (defun tramp-split-string (string pattern
)
7529 "Like `split-string' but omit empty strings.
7530 In Emacs, (split-string \"/foo/bar\" \"/\") returns (\"foo\" \"bar\").
7531 This is, the first, empty, element is omitted. In XEmacs, the first
7532 element is not omitted.
7534 Note: this function has been written for `tramp-handle-file-truename'.
7535 If you want to use it for something else, you'll have to check whether
7536 it does the right thing."
7537 (delete "" (split-string string pattern
)))
7539 (defun tramp-set-process-query-on-exit-flag (process flag
)
7540 "Specify if query is needed for process when Emacs is exited.
7541 If the second argument flag is non-nil, Emacs will query the user before
7542 exiting if process is running."
7543 (if (fboundp 'set-process-query-on-exit-flag
)
7544 (funcall (symbol-function 'set-process-query-on-exit-flag
) process flag
)
7545 (funcall (symbol-function 'process-kill-without-query
) process flag
)))
7548 ;; ------------------------------------------------------------
7549 ;; -- Kludges section --
7550 ;; ------------------------------------------------------------
7552 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
7553 ;; does not deal well with newline characters. Newline is replaced by
7554 ;; backslash newline. But if, say, the string `a backslash newline b'
7555 ;; is passed to a shell, the shell will expand this into "ab",
7556 ;; completely omitting the newline. This is not what was intended.
7557 ;; It does not appear to be possible to make the function
7558 ;; `shell-quote-argument' work with newlines without making it
7559 ;; dependent on the shell used. But within this package, we know that
7560 ;; we will always use a Bourne-like shell, so we use an approach which
7563 ;; The approach is simple: we call `shell-quote-argument', then
7564 ;; massage the newline part of the result.
7566 ;; This function should produce a string which is grokked by a Unix
7567 ;; shell, even if the Emacs is running on Windows. Since this is the
7568 ;; kludges section, we bind `system-type' in such a way that
7569 ;; `shell-quote-arguments' behaves as if on Unix.
7571 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
7572 ;; function to work with Bourne-like shells.
7574 ;; CCC: This function should be rewritten so that
7575 ;; `shell-quote-argument' is not used. This way, we are safe from
7576 ;; changes in `shell-quote-argument'.
7577 (defun tramp-shell-quote-argument (s)
7578 "Similar to `shell-quote-argument', but groks newlines.
7579 Only works for Bourne-like shells."
7580 (let ((system-type 'not-windows
))
7582 (let ((result (shell-quote-argument s
))
7583 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line
))))
7584 (when (and (>= (length result
) 2)
7585 (string= (substring result
0 2) "\\~"))
7586 (setq result
(substring result
1)))
7587 (while (string-match nl result
)
7588 (setq result
(replace-match (format "'%s'" tramp-rsh-end-of-line
)
7592 ;; We currently (sometimes) use "[" and "]" in the filename format.
7593 ;; This means that Emacs wants to expand wildcards if
7594 ;; `find-file-wildcards' is non-nil, and then barfs because no
7595 ;; expansion could be found. We detect this situation and do
7596 ;; something really awful: we have `file-expand-wildcards' return the
7597 ;; original filename if it can't expand anything. Let's just hope
7598 ;; that this doesn't break anything else.
7599 ;; CCC: This check is now also really awful; we should search all
7600 ;; of the filename format, not just the prefix.
7601 (when (string-match "\\[" tramp-prefix-format
)
7602 (defadvice file-expand-wildcards
7603 (around tramp-advice-file-expand-wildcards activate
)
7604 (let ((name (ad-get-arg 0)))
7605 (if (tramp-tramp-file-p name
)
7606 ;; If it's a Tramp file, dissect it and look if wildcards
7607 ;; need to be expanded at all.
7610 (tramp-file-name-localname (tramp-dissect-file-name name
)))
7611 (setq ad-return-value
(or ad-do-it
(list name
)))
7612 (setq ad-return-value
(list name
)))
7613 ;; If it is not a Tramp file, just run the original function.
7614 (setq ad-return-value
(or ad-do-it
(list name
))))))
7615 (add-hook 'tramp-unload-hook
7616 '(lambda () (ad-unadvise 'file-expand-wildcards
))))
7618 ;; Checklist for `tramp-unload-hook'
7619 ;; - Unload all `tramp-*' packages
7620 ;; - Reset `file-name-handler-alist'
7621 ;; - Cleanup hooks where Tramp functions are in
7622 ;; - Cleanup advised functions
7623 ;; - Cleanup autoloads
7625 (defun tramp-unload-tramp ()
7626 "Discard Tramp from loading remote files."
7628 ;; When Tramp is not loaded yet, its autoloads are still active.
7629 (tramp-unload-file-name-handlers)
7630 ;; ange-ftp settings must be enabled.
7631 (when (functionp 'tramp-ftp-enable-ange-ftp
)
7632 (funcall (symbol-function 'tramp-ftp-enable-ange-ftp
)))
7633 ;; Maybe its not loaded yet.
7635 (unload-feature 'tramp
'force
)
7638 (when (and load-in-progress
7639 (string-match "Loading tramp..." (or (current-message) "")))
7640 (message "Loading tramp...done"))
7646 ;; * Allow putting passwords in the filename.
7647 ;; This should be implemented via a general mechanism to add
7648 ;; parameters in filenames. There is currently a kludge for
7649 ;; putting the port number into the filename for ssh and ftp
7650 ;; files. This could be subsumed by the new mechanism as well.
7651 ;; Another approach is to read a netrc file like ~/.authinfo
7653 ;; * Handle nonlocal exits such as C-g.
7654 ;; * But it would probably be better to use with-local-quit at the
7655 ;; place where it's actually needed: around any potentially
7656 ;; indefinitely blocking piece of code. In this case it would be
7657 ;; within Tramp around one of its calls to accept-process-output (or
7658 ;; around one of the loops that calls accept-process-output)
7659 ;; (Stefan Monnier).
7660 ;; * Autodetect if remote `ls' groks the "--dired" switch.
7661 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
7662 ;; `shell-quote-argument'.
7663 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
7664 ;; by the files in that directory. Add this here.
7665 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
7666 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
7667 ;; * When logging in, keep looking for questions according to an alist
7668 ;; and then invoke the right function.
7669 ;; * Case-insensitive filename completion. (Norbert Goevert.)
7670 ;; * Don't use globbing for directories with many files, as this is
7671 ;; likely to produce long command lines, and some shells choke on
7672 ;; long command lines.
7673 ;; * `vc-directory' does not work. It never displays any files, even
7674 ;; if it does show files when run locally.
7675 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
7676 ;; * Grok `append' parameter for `write-region'.
7677 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
7678 ;; * abbreviate-file-name
7679 ;; * better error checking. At least whenever we see something
7680 ;; strange when doing zerop, we should kill the process and start
7681 ;; again. (Greg Stark)
7682 ;; * Provide a local cache of old versions of remote files for the rsync
7683 ;; transfer method to use. (Greg Stark)
7684 ;; * Remove unneeded parameters from methods.
7685 ;; * Invoke rsync once for copying a whole directory hierarchy.
7686 ;; (Francesco Potortì)
7687 ;; * Make it work for different encodings, and for different file name
7688 ;; encodings, too. (Daniel Pittman)
7689 ;; * Progress reports while copying files. (Michael Kifer)
7690 ;; * Don't search for perl5 and perl. Instead, only search for perl and
7691 ;; then look if it's the right version (with `perl -v').
7692 ;; * When editing a remote CVS controlled file as a different user, VC
7693 ;; gets confused about the file locking status. Try to find out why
7694 ;; the workaround doesn't work.
7695 ;; * Username and hostname completion.
7696 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode-p'.
7697 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
7698 ;; Code is nearly identical.
7699 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
7700 ;; until the last but one hop via `start-file-process'. Apply it
7701 ;; also for ftp and smb.
7702 ;; * WIBNI if we had a command "trampclient"? If I was editing in
7703 ;; some shell with root priviledges, it would be nice if I could
7705 ;; trampclient filename.c
7706 ;; as an editor, and the _current_ shell would connect to an Emacs
7707 ;; server and would be used in an existing non-priviledged Emacs
7708 ;; session for doing the editing in question.
7709 ;; That way, I need not tell Emacs my password again and be afraid
7710 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
7711 ;; once display a just typed password in the context of a keyboard
7712 ;; sequence prompt for a question immediately following in a shell
7713 ;; script run within Emacs -- nasty).
7714 ;; And if I have some ssh session running to a different computer,
7715 ;; having the possibility of passing a local file there to a local
7716 ;; Emacs session (in case I can arrange for a connection back) would
7718 ;; Likely the corresponding Tramp server should not allow the
7719 ;; equivalent of the emacsclient -eval option in order to make this
7720 ;; reasonably unproblematic. And maybe trampclient should have some
7721 ;; way of passing credentials, like by using an SSL socket or
7722 ;; something. (David Kastrup)
7723 ;; * Could Tramp reasonably look for a prompt after ^M rather than
7724 ;; only after ^J ? (Stefan Monnier)
7725 ;; * Reconnect directly to a compliant shell without first going
7726 ;; through the user's default shell. (Pete Forman)
7727 ;; * Make `tramp-default-user' obsolete.
7728 ;; * Tramp shall reconnect automatically to its ssh connection when it
7729 ;; detects that the process "has died". (David Reitter)
7730 ;; * How can I interrupt the remote process with a signal
7731 ;; (interrupt-process seems not to work)? (Markus Triska)
7732 ;; * Avoid the local shell entirely for starting remote processes. If
7733 ;; so, I think even a signal, when delivered directly to the local
7734 ;; SSH instance, would correctly be propagated to the remote process
7735 ;; automatically; possibly SSH would have to be started with
7736 ;; "-t". (Markus Triska)
7737 ;; * Add gvfs support.
7738 ;; * Set `tramp-copy-size-limit' to 0, when there is no remote
7739 ;; encoding routine.
7740 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
7741 ;; isn't on the remote host. (Mark A. Hershberger)
7743 ;; Functions for file-name-handler-alist:
7744 ;; diff-latest-backup-file -- in diff.el
7745 ;; dired-uncache -- this will be needed when we do insert-directory caching
7746 ;; file-name-sans-versions -- use primitive?
7747 ;; get-file-buffer -- use primitive
7750 ;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
7751 ;;; tramp.el ends here