* net/tramp.el (tramp-perl-file-truename): New defconst. Perl
[emacs.git] / lisp / net / tramp.el
blobf89c32102e27be199aab8f3733ab8fba7351a8c2
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/>.
27 ;;; Commentary:
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.
36 ;; Notes:
37 ;; -----
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!
60 ;;; Code:
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.
70 (require 'trampver)
71 (add-hook 'tramp-unload-hook
72 (lambda ()
73 (when (featurep 'trampver)
74 (unload-feature 'trampver 'force))))
76 (require 'tramp-compat)
77 (add-hook 'tramp-unload-hook
78 (lambda ()
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
84 ;; be mandatory
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
90 (require 'shell)
91 (require 'advice)
93 (eval-and-compile
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
108 (lambda ()
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
115 (lambda ()
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"
124 '(dolist
125 (feature
126 (list
128 ;; Tramp interactive commands.
129 'tramp-cmds
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.
141 'tramp-fish
143 ;; tramp-gvfs needs D-Bus messages. Available since Emacs 23
144 ;; on some system types. We don't call `dbus-ping', because
145 ;; this would load dbus.el.
146 (when (and (featurep 'dbusbind)
147 (condition-case nil
148 (funcall 'dbus-get-unique-name :session)
149 (error nil))
150 (tramp-compat-process-running-p "gvfs-fuse-daemon"))
151 'tramp-gvfs)
153 ;; Load gateways. It needs `make-network-process' from Emacs 22.
154 (when (functionp 'make-network-process) 'tramp-gw)
156 ;; tramp-imap needs both epa (from Emacs 23.1) and imap-hash
157 ;; (from Emacs 23.2).
158 (when (and (locate-library "epa") (locate-library "imap-hash"))
159 'tramp-imap)))
161 (when feature
162 ;; We have used just some basic tests, whether a package shall
163 ;; be added. There might still be other errors during loading,
164 ;; which we will catch here.
165 (catch 'tramp-loading
166 (require feature)
167 (add-hook 'tramp-unload-hook
168 `(lambda ()
169 (when (featurep (quote ,feature))
170 (unload-feature (quote ,feature) 'force)))))
171 (unless (featurep feature)
172 (message "Loading %s failed, ignoring this package" feature)))))
174 ;;; User Customizable Internal Variables:
176 (defgroup tramp nil
177 "Edit remote files with a combination of rsh and rcp or similar programs."
178 :group 'files
179 :version "22.1")
181 ;; Maybe we need once a real Tramp mode, with key bindings etc.
182 ;;;###autoload
183 (defcustom tramp-mode t
184 "*Whether Tramp is enabled.
185 If it is set to nil, all remote file names are used literally."
186 :group 'tramp
187 :type 'boolean)
189 (defcustom tramp-verbose 3
190 "*Verbosity level for Tramp messages.
191 Any level x includes messages for all levels 1 .. x-1. The levels are
193 0 silent (no tramp messages at all)
194 1 errors
195 2 warnings
196 3 connection to remote hosts (default level)
197 4 activities
198 5 internal
199 6 sent and received strings
200 7 file caching
201 8 connection properties
202 10 traces (huge)."
203 :group 'tramp
204 :type 'integer)
206 ;; Emacs case.
207 (eval-and-compile
208 (when (boundp 'backup-directory-alist)
209 (defcustom tramp-backup-directory-alist nil
210 "Alist of filename patterns and backup directory names.
211 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
212 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
213 is a local file name, the backup directory is prepended with Tramp file
214 name prefix \(method, user, host\) of file.
216 \(setq tramp-backup-directory-alist backup-directory-alist\)
218 gives the same backup policy for Tramp files on their hosts like the
219 policy for local files."
220 :group 'tramp
221 :type '(repeat (cons (regexp :tag "Regexp matching filename")
222 (directory :tag "Backup directory name"))))))
224 ;; XEmacs case. We cannot check for `bkup-backup-directory-info', because
225 ;; the package "backup-dir" might not be loaded yet.
226 (eval-and-compile
227 (when (featurep 'xemacs)
228 (defcustom tramp-bkup-backup-directory-info nil
229 "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
230 It has the same meaning like `bkup-backup-directory-info' from package
231 `backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local
232 file name, the backup directory is prepended with Tramp file name prefix
233 \(method, user, host\) of file.
235 \(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\)
237 gives the same backup policy for Tramp files on their hosts like the
238 policy for local files."
239 :type '(repeat
240 (list (regexp :tag "File regexp")
241 (string :tag "Backup Dir")
242 (set :inline t
243 (const ok-create)
244 (const full-path)
245 (const prepend-name)
246 (const search-upward))))
247 :group 'tramp)))
249 (defcustom tramp-auto-save-directory nil
250 "*Put auto-save files in this directory, if set.
251 The idea is to use a local directory so that auto-saving is faster."
252 :group 'tramp
253 :type '(choice (const nil) string))
255 (defcustom tramp-encoding-shell
256 (if (memq system-type '(windows-nt))
257 (getenv "COMSPEC")
258 "/bin/sh")
259 "*Use this program for encoding and decoding commands on the local host.
260 This shell is used to execute the encoding and decoding command on the
261 local host, so if you want to use `~' in those commands, you should
262 choose a shell here which groks tilde expansion. `/bin/sh' normally
263 does not understand tilde expansion.
265 For encoding and deocding, commands like the following are executed:
267 /bin/sh -c COMMAND < INPUT > OUTPUT
269 This variable can be used to change the \"/bin/sh\" part. See the
270 variable `tramp-encoding-command-switch' for the \"-c\" part.
272 Note that this variable is not used for remote commands. There are
273 mechanisms in tramp.el which automatically determine the right shell to
274 use for the remote host."
275 :group 'tramp
276 :type '(file :must-match t))
278 (defcustom tramp-encoding-command-switch
279 (if (string-match "cmd\\.exe" tramp-encoding-shell)
280 "/c"
281 "-c")
282 "*Use this switch together with `tramp-encoding-shell' for local commands.
283 See the variable `tramp-encoding-shell' for more information."
284 :group 'tramp
285 :type 'string)
287 (defcustom tramp-copy-size-limit 10240
288 "*The maximum file size where inline copying is preferred over an out-of-the-band copy."
289 :group 'tramp
290 :type 'integer)
292 (defcustom tramp-terminal-type "dumb"
293 "*Value of TERM environment variable for logging in to remote host.
294 Because Tramp wants to parse the output of the remote shell, it is easily
295 confused by ANSI color escape sequences and suchlike. Often, shell init
296 files conditionalize this setup based on the TERM environment variable."
297 :group 'tramp
298 :type 'string)
300 (defvar tramp-methods
301 `(("rcp" (tramp-login-program "rsh")
302 (tramp-login-args (("%h") ("-l" "%u")))
303 (tramp-remote-sh "/bin/sh")
304 (tramp-copy-program "rcp")
305 (tramp-copy-args (("-p" "%k") ("-r")))
306 (tramp-copy-keep-date t)
307 (tramp-copy-recursive t)
308 (tramp-password-end-of-line nil))
309 ("scp" (tramp-login-program "ssh")
310 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
311 ("-e" "none")))
312 (tramp-remote-sh "/bin/sh")
313 (tramp-copy-program "scp")
314 (tramp-copy-args (("-P" "%p") ("-p" "%k")
315 ("-q") ("-r")))
316 (tramp-copy-keep-date t)
317 (tramp-copy-recursive t)
318 (tramp-password-end-of-line nil)
319 (tramp-gw-args (("-o"
320 "GlobalKnownHostsFile=/dev/null")
321 ("-o" "UserKnownHostsFile=/dev/null")
322 ("-o" "StrictHostKeyChecking=no")))
323 (tramp-default-port 22))
324 ("scp1" (tramp-login-program "ssh")
325 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
326 ("-1" "-e" "none")))
327 (tramp-remote-sh "/bin/sh")
328 (tramp-copy-program "scp")
329 (tramp-copy-args (("-1") ("-P" "%p") ("-p" "%k")
330 ("-q") ("-r")))
331 (tramp-copy-keep-date t)
332 (tramp-copy-recursive t)
333 (tramp-password-end-of-line nil)
334 (tramp-gw-args (("-o"
335 "GlobalKnownHostsFile=/dev/null")
336 ("-o" "UserKnownHostsFile=/dev/null")
337 ("-o" "StrictHostKeyChecking=no")))
338 (tramp-default-port 22))
339 ("scp2" (tramp-login-program "ssh")
340 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
341 ("-2" "-e" "none")))
342 (tramp-remote-sh "/bin/sh")
343 (tramp-copy-program "scp")
344 (tramp-copy-args (("-2") ("-P" "%p") ("-p" "%k")
345 ("-q") ("-r")))
346 (tramp-copy-keep-date t)
347 (tramp-copy-recursive t)
348 (tramp-password-end-of-line nil)
349 (tramp-gw-args (("-o"
350 "GlobalKnownHostsFile=/dev/null")
351 ("-o" "UserKnownHostsFile=/dev/null")
352 ("-o" "StrictHostKeyChecking=no")))
353 (tramp-default-port 22))
354 ("scp1_old"
355 (tramp-login-program "ssh1")
356 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
357 ("-e" "none")))
358 (tramp-remote-sh "/bin/sh")
359 (tramp-copy-program "scp1")
360 (tramp-copy-args (("-p" "%k") ("-r")))
361 (tramp-copy-keep-date t)
362 (tramp-copy-recursive t)
363 (tramp-password-end-of-line nil))
364 ("scp2_old"
365 (tramp-login-program "ssh2")
366 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
367 ("-e" "none")))
368 (tramp-remote-sh "/bin/sh")
369 (tramp-copy-program "scp2")
370 (tramp-copy-args (("-p" "%k") ("-r")))
371 (tramp-copy-keep-date t)
372 (tramp-copy-recursive t)
373 (tramp-password-end-of-line nil))
374 ("sftp" (tramp-login-program "ssh")
375 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
376 ("-e" "none")))
377 (tramp-remote-sh "/bin/sh")
378 (tramp-copy-program "sftp")
379 (tramp-copy-args nil)
380 (tramp-copy-keep-date nil)
381 (tramp-password-end-of-line nil))
382 ("rsync" (tramp-login-program "ssh")
383 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
384 ("-e" "none")))
385 (tramp-remote-sh "/bin/sh")
386 (tramp-copy-program "rsync")
387 (tramp-copy-args (("-e" "ssh") ("-t" "%k") ("-r")))
388 (tramp-copy-keep-date t)
389 (tramp-copy-keep-tmpfile t)
390 (tramp-copy-recursive t)
391 (tramp-password-end-of-line nil))
392 ("rsyncc"
393 (tramp-login-program "ssh")
394 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
395 ("-o" "ControlPath=%t.%%r@%%h:%%p")
396 ("-o" "ControlMaster=yes")
397 ("-e" "none")))
398 (tramp-remote-sh "/bin/sh")
399 (tramp-copy-program "rsync")
400 (tramp-copy-args (("-t" "%k") ("-r")))
401 (tramp-copy-env (("RSYNC_RSH")
402 (,(concat
403 "ssh"
404 " -o ControlPath=%t.%%r@%%h:%%p"
405 " -o ControlMaster=auto"))))
406 (tramp-copy-keep-date t)
407 (tramp-copy-keep-tmpfile t)
408 (tramp-copy-recursive t)
409 (tramp-password-end-of-line nil))
410 ("remcp" (tramp-login-program "remsh")
411 (tramp-login-args (("%h") ("-l" "%u")))
412 (tramp-remote-sh "/bin/sh")
413 (tramp-copy-program "rcp")
414 (tramp-copy-args (("-p" "%k")))
415 (tramp-copy-keep-date t)
416 (tramp-password-end-of-line nil))
417 ("rsh" (tramp-login-program "rsh")
418 (tramp-login-args (("%h") ("-l" "%u")))
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))
424 ("ssh" (tramp-login-program "ssh")
425 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
426 ("-e" "none")))
427 (tramp-remote-sh "/bin/sh")
428 (tramp-copy-program nil)
429 (tramp-copy-args nil)
430 (tramp-copy-keep-date nil)
431 (tramp-password-end-of-line nil)
432 (tramp-gw-args (("-o"
433 "GlobalKnownHostsFile=/dev/null")
434 ("-o" "UserKnownHostsFile=/dev/null")
435 ("-o" "StrictHostKeyChecking=no")))
436 (tramp-default-port 22))
437 ("ssh1" (tramp-login-program "ssh")
438 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
439 ("-1" "-e" "none")))
440 (tramp-remote-sh "/bin/sh")
441 (tramp-copy-program nil)
442 (tramp-copy-args nil)
443 (tramp-copy-keep-date nil)
444 (tramp-password-end-of-line nil)
445 (tramp-gw-args (("-o"
446 "GlobalKnownHostsFile=/dev/null")
447 ("-o" "UserKnownHostsFile=/dev/null")
448 ("-o" "StrictHostKeyChecking=no")))
449 (tramp-default-port 22))
450 ("ssh2" (tramp-login-program "ssh")
451 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
452 ("-2" "-e" "none")))
453 (tramp-remote-sh "/bin/sh")
454 (tramp-copy-program nil)
455 (tramp-copy-args nil)
456 (tramp-copy-keep-date nil)
457 (tramp-password-end-of-line nil)
458 (tramp-gw-args (("-o"
459 "GlobalKnownHostsFile=/dev/null")
460 ("-o" "UserKnownHostsFile=/dev/null")
461 ("-o" "StrictHostKeyChecking=no")))
462 (tramp-default-port 22))
463 ("ssh1_old"
464 (tramp-login-program "ssh1")
465 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
466 ("-e" "none")))
467 (tramp-remote-sh "/bin/sh")
468 (tramp-copy-program nil)
469 (tramp-copy-args nil)
470 (tramp-copy-keep-date nil)
471 (tramp-password-end-of-line nil))
472 ("ssh2_old"
473 (tramp-login-program "ssh2")
474 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
475 ("-e" "none")))
476 (tramp-remote-sh "/bin/sh")
477 (tramp-copy-program nil)
478 (tramp-copy-args nil)
479 (tramp-copy-keep-date nil)
480 (tramp-password-end-of-line nil))
481 ("remsh" (tramp-login-program "remsh")
482 (tramp-login-args (("%h") ("-l" "%u")))
483 (tramp-remote-sh "/bin/sh")
484 (tramp-copy-program nil)
485 (tramp-copy-args nil)
486 (tramp-copy-keep-date nil)
487 (tramp-password-end-of-line nil))
488 ("telnet"
489 (tramp-login-program "telnet")
490 (tramp-login-args (("%h") ("%p")))
491 (tramp-remote-sh "/bin/sh")
492 (tramp-copy-program nil)
493 (tramp-copy-args nil)
494 (tramp-copy-keep-date nil)
495 (tramp-password-end-of-line nil)
496 (tramp-default-port 23))
497 ("su" (tramp-login-program "su")
498 (tramp-login-args (("-") ("%u")))
499 (tramp-remote-sh "/bin/sh")
500 (tramp-copy-program nil)
501 (tramp-copy-args nil)
502 (tramp-copy-keep-date nil)
503 (tramp-password-end-of-line nil))
504 ("sudo" (tramp-login-program "sudo")
505 (tramp-login-args (("-u" "%u")
506 ("-s") ("-H") ("-p" "Password:")))
507 (tramp-remote-sh "/bin/sh")
508 (tramp-copy-program nil)
509 (tramp-copy-args nil)
510 (tramp-copy-keep-date nil)
511 (tramp-password-end-of-line nil))
512 ("scpc" (tramp-login-program "ssh")
513 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
514 ("-o" "ControlPath=%t.%%r@%%h:%%p")
515 ("-o" "ControlMaster=yes")
516 ("-e" "none")))
517 (tramp-remote-sh "/bin/sh")
518 (tramp-copy-program "scp")
519 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q")
520 ("-o" "ControlPath=%t.%%r@%%h:%%p")
521 ("-o" "ControlMaster=auto")))
522 (tramp-copy-keep-date t)
523 (tramp-password-end-of-line nil)
524 (tramp-gw-args (("-o"
525 "GlobalKnownHostsFile=/dev/null")
526 ("-o" "UserKnownHostsFile=/dev/null")
527 ("-o" "StrictHostKeyChecking=no")))
528 (tramp-default-port 22))
529 ("scpx" (tramp-login-program "ssh")
530 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
531 ("-e" "none" "-t" "-t" "/bin/sh")))
532 (tramp-remote-sh "/bin/sh")
533 (tramp-copy-program "scp")
534 (tramp-copy-args (("-p" "%k")))
535 (tramp-copy-keep-date t)
536 (tramp-password-end-of-line nil)
537 (tramp-gw-args (("-o"
538 "GlobalKnownHostsFile=/dev/null")
539 ("-o" "UserKnownHostsFile=/dev/null")
540 ("-o" "StrictHostKeyChecking=no")))
541 (tramp-default-port 22))
542 ("sshx" (tramp-login-program "ssh")
543 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p") ("-q")
544 ("-e" "none" "-t" "-t" "/bin/sh")))
545 (tramp-remote-sh "/bin/sh")
546 (tramp-copy-program nil)
547 (tramp-copy-args nil)
548 (tramp-copy-keep-date nil)
549 (tramp-password-end-of-line nil)
550 (tramp-gw-args (("-o"
551 "GlobalKnownHostsFile=/dev/null")
552 ("-o" "UserKnownHostsFile=/dev/null")
553 ("-o" "StrictHostKeyChecking=no")))
554 (tramp-default-port 22))
555 ("krlogin"
556 (tramp-login-program "krlogin")
557 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
558 (tramp-remote-sh "/bin/sh")
559 (tramp-copy-program nil)
560 (tramp-copy-args nil)
561 (tramp-copy-keep-date nil)
562 (tramp-password-end-of-line nil))
563 ("plink" (tramp-login-program "plink")
564 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
565 ("-ssh")))
566 (tramp-remote-sh "/bin/sh")
567 (tramp-copy-program nil)
568 (tramp-copy-args nil)
569 (tramp-copy-keep-date nil)
570 (tramp-password-end-of-line "xy") ;see docstring for "xy"
571 (tramp-default-port 22))
572 ("plink1"
573 (tramp-login-program "plink")
574 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
575 ("-1" "-ssh")))
576 (tramp-remote-sh "/bin/sh")
577 (tramp-copy-program nil)
578 (tramp-copy-args nil)
579 (tramp-copy-keep-date nil)
580 (tramp-password-end-of-line "xy") ;see docstring for "xy"
581 (tramp-default-port 22))
582 ("plinkx"
583 (tramp-login-program "plink")
584 ;; ("%h") must be a single element, see
585 ;; `tramp-compute-multi-hops'.
586 (tramp-login-args (("-load") ("%h") ("-t")
587 (,(format
588 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=$ '"
589 tramp-terminal-type))
590 ("/bin/sh")))
591 (tramp-remote-sh "/bin/sh")
592 (tramp-copy-program nil)
593 (tramp-copy-args nil)
594 (tramp-copy-keep-date nil)
595 (tramp-password-end-of-line nil))
596 ("pscp" (tramp-login-program "plink")
597 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
598 ("-ssh")))
599 (tramp-remote-sh "/bin/sh")
600 (tramp-copy-program "pscp")
601 (tramp-copy-args (("-P" "%p") ("-scp") ("-p" "%k")))
602 (tramp-copy-keep-date t)
603 (tramp-password-end-of-line "xy") ;see docstring for "xy"
604 (tramp-default-port 22))
605 ("psftp" (tramp-login-program "plink")
606 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
607 ("-ssh")))
608 (tramp-remote-sh "/bin/sh")
609 (tramp-copy-program "pscp")
610 (tramp-copy-args (("-P" "%p") ("-sftp") ("-p" "%k")))
611 (tramp-copy-keep-date t)
612 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
613 ("fcp" (tramp-login-program "fsh")
614 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
615 (tramp-remote-sh "/bin/sh -i")
616 (tramp-copy-program "fcp")
617 (tramp-copy-args (("-p" "%k")))
618 (tramp-copy-keep-date t)
619 (tramp-password-end-of-line nil)))
620 "*Alist of methods for remote files.
621 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
622 Each NAME stands for a remote access method. Each PARAM is a
623 pair of the form (KEY VALUE). The following KEYs are defined:
624 * `tramp-remote-sh'
625 This specifies the Bourne shell to use on the remote host. This
626 MUST be a Bourne-like shell. It is normally not necessary to set
627 this to any value other than \"/bin/sh\": Tramp wants to use a shell
628 which groks tilde expansion, but it can search for it. Also note
629 that \"/bin/sh\" exists on all Unixen, this might not be true for
630 the value that you decide to use. You Have Been Warned.
631 * `tramp-login-program'
632 This specifies the name of the program to use for logging in to the
633 remote host. This may be the name of rsh or a workalike program,
634 or the name of telnet or a workalike, or the name of su or a workalike.
635 * `tramp-login-args'
636 This specifies the list of arguments to pass to the above
637 mentioned program. Please note that this is a list of list of arguments,
638 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
639 here. Instead, you want a list (\"-a\" \"-b\"), or (\"-f\" \"foo\").
640 There are some patterns: \"%h\" in this list is replaced by the host
641 name, \"%u\" is replaced by the user name, \"%p\" is replaced by the
642 port number, and \"%%\" can be used to obtain a literal percent character.
643 If a list containing \"%h\", \"%u\" or \"%p\" is unchanged during
644 expansion (i.e. no host or no user specified), this list is not used as
645 argument. By this, arguments like (\"-l\" \"%u\") are optional.
646 \"%t\" is replaced by the temporary file name produced with
647 `tramp-make-tramp-temp-file'. \"%k\" indicates the keep-date
648 parameter of a program, if exists.
649 * `tramp-copy-program'
650 This specifies the name of the program to use for remotely copying
651 the file; this might be the absolute filename of rcp or the name of
652 a workalike program.
653 * `tramp-copy-args'
654 This specifies the list of parameters to pass to the above mentioned
655 program, the hints for `tramp-login-args' also apply here.
656 * `tramp-copy-keep-date'
657 This specifies whether the copying program when the preserves the
658 timestamp of the original file.
659 * `tramp-copy-keep-tmpfile'
660 This specifies whether a temporary local file shall be kept
661 for optimization reasons (useful for \"rsync\" methods).
662 * `tramp-copy-recursive'
663 Whether the operation copies directories recursively.
664 * `tramp-default-port'
665 The default port of a method is needed in case of gateway connections.
666 Additionally, it is used as indication which method is prepared for
667 passing gateways.
668 * `tramp-gw-args'
669 As the attribute name says, additional arguments are specified here
670 when a method is applied via a gateway.
671 * `tramp-password-end-of-line'
672 This specifies the string to use for terminating the line after
673 submitting the password. If this method parameter is nil, then the
674 value of the normal variable `tramp-default-password-end-of-line'
675 is used. This parameter is necessary because the \"plink\" program
676 requires any two characters after sending the password. These do
677 not have to be newline or carriage return characters. Other login
678 programs are happy with just one character, the newline character.
679 We use \"xy\" as the value for methods using \"plink\".
681 What does all this mean? Well, you should specify `tramp-login-program'
682 for all methods; this program is used to log in to the remote site. Then,
683 there are two ways to actually transfer the files between the local and the
684 remote side. One way is using an additional rcp-like program. If you want
685 to do this, set `tramp-copy-program' in the method.
687 Another possibility for file transfer is inline transfer, i.e. the
688 file is passed through the same buffer used by `tramp-login-program'. In
689 this case, the file contents need to be protected since the
690 `tramp-login-program' might use escape codes or the connection might not
691 be eight-bit clean. Therefore, file contents are encoded for transit.
692 See the variables `tramp-local-coding-commands' and
693 `tramp-remote-coding-commands' for details.
695 So, to summarize: if the method is an out-of-band method, then you
696 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
697 inline method, then these two parameters should be nil. Methods which
698 are fit for gateways must have `tramp-default-port' at least.
700 Notes:
702 When using `su' or `sudo' the phrase `open connection to a remote
703 host' sounds strange, but it is used nevertheless, for consistency.
704 No connection is opened to a remote host, but `su' or `sudo' is
705 started on the local host. You should specify a remote host
706 `localhost' or the name of the local host. Another host name is
707 useful only in combination with `tramp-default-proxies-alist'.")
709 (defcustom tramp-default-method
710 ;; An external copy method seems to be preferred, because it is much
711 ;; more performant for large files, and it hasn't too serious delays
712 ;; for small files. But it must be ensured that there aren't
713 ;; permanent password queries. Either a password agent like
714 ;; "ssh-agent" or "Pageant" shall run, or the optional
715 ;; password-cache.el or auth-sources.el packages shall be active for
716 ;; password caching. "scpc" would be another good choice because of
717 ;; the "ControlMaster" option, but this is a more modern alternative
718 ;; in OpenSSH 4, which cannot be taken as default.
719 (cond
720 ;; PuTTY is installed.
721 ((executable-find "pscp")
722 (if (or (fboundp 'password-read)
723 (fboundp 'auth-source-user-or-password)
724 ;; Pageant is running.
725 (tramp-compat-process-running-p "Pageant"))
726 "pscp"
727 "plink"))
728 ;; There is an ssh installation.
729 ((executable-find "scp")
730 (if (or (fboundp 'password-read)
731 (fboundp 'auth-source-user-or-password)
732 ;; ssh-agent is running.
733 (getenv "SSH_AUTH_SOCK")
734 (getenv "SSH_AGENT_PID"))
735 "scp"
736 "ssh"))
737 ;; Fallback.
738 (t "ftp"))
739 "*Default method to use for transferring files.
740 See `tramp-methods' for possibilities.
741 Also see `tramp-default-method-alist'."
742 :group 'tramp
743 :type 'string)
745 (defcustom tramp-default-method-alist
746 '(("\\`localhost\\'" "\\`root\\'" "su"))
747 "*Default method to use for specific host/user pairs.
748 This is an alist of items (HOST USER METHOD). The first matching item
749 specifies the method to use for a file name which does not specify a
750 method. HOST and USER are regular expressions or nil, which is
751 interpreted as a regular expression which always matches. If no entry
752 matches, the variable `tramp-default-method' takes effect.
754 If the file name does not specify the user, lookup is done using the
755 empty string for the user name.
757 See `tramp-methods' for a list of possibilities for METHOD."
758 :group 'tramp
759 :type '(repeat (list (regexp :tag "Host regexp")
760 (regexp :tag "User regexp")
761 (string :tag "Method"))))
763 (defcustom tramp-default-user
765 "*Default user to use for transferring files.
766 It is nil by default; otherwise settings in configuration files like
767 \"~/.ssh/config\" would be overwritten. Also see `tramp-default-user-alist'.
769 This variable is regarded as obsolete, and will be removed soon."
770 :group 'tramp
771 :type '(choice (const nil) string))
773 (defcustom tramp-default-user-alist
774 `(("\\`su\\(do\\)?\\'" nil "root")
775 ("\\`r\\(em\\)?\\(cp\\|sh\\)\\|telnet\\|plink1?\\'"
776 nil ,(user-login-name)))
777 "*Default user to use for specific method/host pairs.
778 This is an alist of items (METHOD HOST USER). The first matching item
779 specifies the user to use for a file name which does not specify a
780 user. METHOD and USER are regular expressions or nil, which is
781 interpreted as a regular expression which always matches. If no entry
782 matches, the variable `tramp-default-user' takes effect.
784 If the file name does not specify the method, lookup is done using the
785 empty string for the method name."
786 :group 'tramp
787 :type '(repeat (list (regexp :tag "Method regexp")
788 (regexp :tag "Host regexp")
789 (string :tag "User"))))
791 (defcustom tramp-default-host
792 (system-name)
793 "*Default host to use for transferring files.
794 Useful for su and sudo methods mostly."
795 :group 'tramp
796 :type 'string)
798 (defcustom tramp-default-proxies-alist nil
799 "*Route to be followed for specific host/user pairs.
800 This is an alist of items (HOST USER PROXY). The first matching
801 item specifies the proxy to be passed for a file name located on
802 a remote target matching USER@HOST. HOST and USER are regular
803 expressions. PROXY must be a Tramp filename without a localname
804 part. Method and user name on PROXY are optional, which is
805 interpreted with the default values. PROXY can contain the
806 patterns %h and %u, which are replaced by the strings matching
807 HOST or USER, respectively.
809 HOST, USER or PROXY could also be Lisp forms, which will be
810 evaluated. The result must be a string or nil, which is
811 interpreted as a regular expression which always matches."
812 :group 'tramp
813 :type '(repeat (list (choice :tag "Host regexp" regexp sexp)
814 (choice :tag "User regexp" regexp sexp)
815 (choice :tag "Proxy remote name" string (const nil)))))
817 (defconst tramp-local-host-regexp
818 (concat
819 "^" (regexp-opt (list "localhost" (system-name) "127\.0\.0\.1" "::1") t) "$")
820 "*Host names which are regarded as local host.")
822 (defconst tramp-completion-function-alist-rsh
823 '((tramp-parse-rhosts "/etc/hosts.equiv")
824 (tramp-parse-rhosts "~/.rhosts"))
825 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
827 (defconst tramp-completion-function-alist-ssh
828 '((tramp-parse-rhosts "/etc/hosts.equiv")
829 (tramp-parse-rhosts "/etc/shosts.equiv")
830 (tramp-parse-shosts "/etc/ssh_known_hosts")
831 (tramp-parse-sconfig "/etc/ssh_config")
832 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
833 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
834 (tramp-parse-rhosts "~/.rhosts")
835 (tramp-parse-rhosts "~/.shosts")
836 (tramp-parse-shosts "~/.ssh/known_hosts")
837 (tramp-parse-sconfig "~/.ssh/config")
838 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
839 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
840 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
842 (defconst tramp-completion-function-alist-telnet
843 '((tramp-parse-hosts "/etc/hosts"))
844 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
846 (defconst tramp-completion-function-alist-su
847 '((tramp-parse-passwd "/etc/passwd"))
848 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
850 (defconst tramp-completion-function-alist-putty
851 '((tramp-parse-putty
852 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"))
853 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty methods.")
855 (defvar tramp-completion-function-alist nil
856 "*Alist of methods for remote files.
857 This is a list of entries of the form (NAME PAIR1 PAIR2 ...).
858 Each NAME stands for a remote access method. Each PAIR is of the form
859 \(FUNCTION FILE). FUNCTION is responsible to extract user names and host
860 names from FILE for completion. The following predefined FUNCTIONs exists:
862 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
863 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
864 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
865 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
866 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
867 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
868 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
869 * `tramp-parse-netrc' for \"~/.netrc\" like files.
870 * `tramp-parse-putty' for PuTTY registry keys.
872 FUNCTION can also be a customer defined function. For more details see
873 the info pages.")
875 (eval-after-load "tramp"
876 '(progn
877 (tramp-set-completion-function
878 "rcp" tramp-completion-function-alist-rsh)
879 (tramp-set-completion-function
880 "scp" tramp-completion-function-alist-ssh)
881 (tramp-set-completion-function
882 "scp1" tramp-completion-function-alist-ssh)
883 (tramp-set-completion-function
884 "scp2" tramp-completion-function-alist-ssh)
885 (tramp-set-completion-function
886 "scp1_old" tramp-completion-function-alist-ssh)
887 (tramp-set-completion-function
888 "scp2_old" tramp-completion-function-alist-ssh)
889 (tramp-set-completion-function
890 "rsync" tramp-completion-function-alist-ssh)
891 (tramp-set-completion-function
892 "rsyncc" tramp-completion-function-alist-ssh)
893 (tramp-set-completion-function
894 "remcp" tramp-completion-function-alist-rsh)
895 (tramp-set-completion-function
896 "rsh" tramp-completion-function-alist-rsh)
897 (tramp-set-completion-function
898 "ssh" tramp-completion-function-alist-ssh)
899 (tramp-set-completion-function
900 "ssh1" tramp-completion-function-alist-ssh)
901 (tramp-set-completion-function
902 "ssh2" tramp-completion-function-alist-ssh)
903 (tramp-set-completion-function
904 "ssh1_old" tramp-completion-function-alist-ssh)
905 (tramp-set-completion-function
906 "ssh2_old" tramp-completion-function-alist-ssh)
907 (tramp-set-completion-function
908 "remsh" tramp-completion-function-alist-rsh)
909 (tramp-set-completion-function
910 "telnet" tramp-completion-function-alist-telnet)
911 (tramp-set-completion-function
912 "su" tramp-completion-function-alist-su)
913 (tramp-set-completion-function
914 "sudo" tramp-completion-function-alist-su)
915 (tramp-set-completion-function
916 "scpx" tramp-completion-function-alist-ssh)
917 (tramp-set-completion-function
918 "sshx" tramp-completion-function-alist-ssh)
919 (tramp-set-completion-function
920 "krlogin" tramp-completion-function-alist-rsh)
921 (tramp-set-completion-function
922 "plink" tramp-completion-function-alist-ssh)
923 (tramp-set-completion-function
924 "plink1" tramp-completion-function-alist-ssh)
925 (tramp-set-completion-function
926 "plinkx" tramp-completion-function-alist-putty)
927 (tramp-set-completion-function
928 "pscp" tramp-completion-function-alist-ssh)
929 (tramp-set-completion-function
930 "fcp" tramp-completion-function-alist-ssh)))
932 (defconst tramp-echo-mark-marker "_echo"
933 "String marker to surround echoed commands.")
935 (defconst tramp-echo-mark "_echo\b\b\b\b\b"
936 "String mark to be transmitted around shell commands.
937 Used to separate their echo from the output they produce. This
938 will only be used if we cannot disable remote echo via stty.
939 This string must have no effect on the remote shell except for
940 producing some echo which can later be detected by
941 `tramp-echoed-echo-mark-regexp'. Using `tramp-echo-mark-marker',
942 followed by an equal number of backspaces to erase them will
943 usually suffice.")
945 (defconst tramp-echoed-echo-mark-regexp "_echo\\(\b\\( \b\\)?\\)\\{5\\}"
946 "Regexp which matches `tramp-echo-mark' as it gets echoed by
947 the remote shell.")
949 (defcustom tramp-rsh-end-of-line "\n"
950 "*String used for end of line in rsh connections.
951 I don't think this ever needs to be changed, so please tell me about it
952 if you need to change this.
953 Also see the method parameter `tramp-password-end-of-line' and the normal
954 variable `tramp-default-password-end-of-line'."
955 :group 'tramp
956 :type 'string)
958 (defcustom tramp-default-password-end-of-line
959 tramp-rsh-end-of-line
960 "*String used for end of line after sending a password.
961 This variable provides the default value for the method parameter
962 `tramp-password-end-of-line', see `tramp-methods' for more details.
964 It seems that people using plink under Windows need to send
965 \"\\r\\n\" (carriage-return, then newline) after a password, but just
966 \"\\n\" after all other lines. This variable can be used for the
967 password, see `tramp-rsh-end-of-line' for the other cases.
969 The default value is to use the same value as `tramp-rsh-end-of-line'."
970 :group 'tramp
971 :type 'string)
973 ;; "getconf PATH" yields:
974 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
975 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
976 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
977 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
978 (defcustom tramp-remote-path
979 '(tramp-default-remote-path "/usr/sbin" "/usr/local/bin"
980 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
981 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
982 "*List of directories to search for executables on remote host.
983 For every remote host, this variable will be set buffer local,
984 keeping the list of existing directories on that host.
986 You can use `~' in this list, but when searching for a shell which groks
987 tilde expansion, all directory names starting with `~' will be ignored.
989 `Default Directories' represent the list of directories given by
990 the command \"getconf PATH\". It is recommended to use this
991 entry on top of this list, because these are the default
992 directories for POSIX compatible commands.
994 `Private Directories' are the settings of the $PATH environment,
995 as given in your `~/.profile'."
996 :group 'tramp
997 :type '(repeat (choice
998 (const :tag "Default Directories" tramp-default-remote-path)
999 (const :tag "Private Directories" tramp-own-remote-path)
1000 (string :tag "Directory"))))
1002 (defcustom tramp-remote-process-environment
1003 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "LC_ALL=C"
1004 ,(concat "TERM=" tramp-terminal-type)
1005 "EMACS=t" ;; Deprecated.
1006 ,(format "INSIDE_EMACS=%s,tramp:%s" emacs-version tramp-version)
1007 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH="
1008 "autocorrect=" "correct=")
1010 "*List of environment variables to be set on the remote host.
1012 Each element should be a string of the form ENVVARNAME=VALUE. An
1013 entry ENVVARNAME= diables the corresponding environment variable,
1014 which might have been set in the init files like ~/.profile.
1016 Special handling is applied to the PATH environment, which should
1017 not be set here. Instead of, it should be set via `tramp-remote-path'."
1018 :group 'tramp
1019 :type '(repeat string))
1021 (defcustom tramp-login-prompt-regexp
1022 ".*ogin\\( .*\\)?: *"
1023 "*Regexp matching login-like prompts.
1024 The regexp should match at end of buffer.
1026 Sometimes the prompt is reported to look like \"login as:\"."
1027 :group 'tramp
1028 :type 'regexp)
1030 (defcustom tramp-shell-prompt-pattern
1031 ;; Allow a prompt to start right after a ^M since it indeed would be
1032 ;; displayed at the beginning of the line (and Zsh uses it).
1033 "\\(?:^\\| \\)[^#$%>\n]*[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*"
1034 "Regexp to match prompts from remote shell.
1035 Normally, Tramp expects you to configure `shell-prompt-pattern'
1036 correctly, but sometimes it happens that you are connecting to a
1037 remote host which sends a different kind of shell prompt. Therefore,
1038 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
1039 and also things matched by this variable. The default value of this
1040 variable is similar to the default value of `shell-prompt-pattern',
1041 which should work well in many cases."
1042 :group 'tramp
1043 :type 'regexp)
1045 (defcustom tramp-password-prompt-regexp
1046 "^.*\\([pP]assword\\|[pP]assphrase\\).*:\^@? *"
1047 "*Regexp matching password-like prompts.
1048 The regexp should match at end of buffer.
1050 The `sudo' program appears to insert a `^@' character into the prompt."
1051 :group 'tramp
1052 :type 'regexp)
1054 (defcustom tramp-wrong-passwd-regexp
1055 (concat "^.*"
1056 ;; These strings should be on the last line
1057 (regexp-opt '("Permission denied"
1058 "Login incorrect"
1059 "Login Incorrect"
1060 "Connection refused"
1061 "Connection closed"
1062 "Sorry, try again."
1063 "Name or service not known"
1064 "Host key verification failed."
1065 "No supported authentication methods left to try!") t)
1066 ".*"
1067 "\\|"
1068 "^.*\\("
1069 ;; Here comes a list of regexes, separated by \\|
1070 "Received signal [0-9]+"
1071 "\\).*")
1072 "*Regexp matching a `login failed' message.
1073 The regexp should match at end of buffer."
1074 :group 'tramp
1075 :type 'regexp)
1077 (defcustom tramp-yesno-prompt-regexp
1078 (concat
1079 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
1080 "\\s-*")
1081 "Regular expression matching all yes/no queries which need to be confirmed.
1082 The confirmation should be done with yes or no.
1083 The regexp should match at end of buffer.
1084 See also `tramp-yn-prompt-regexp'."
1085 :group 'tramp
1086 :type 'regexp)
1088 (defcustom tramp-yn-prompt-regexp
1089 (concat
1090 (regexp-opt '("Store key in cache? (y/n)"
1091 "Update cached key? (y/n, Return cancels connection)") t)
1092 "\\s-*")
1093 "Regular expression matching all y/n queries which need to be confirmed.
1094 The confirmation should be done with y or n.
1095 The regexp should match at end of buffer.
1096 See also `tramp-yesno-prompt-regexp'."
1097 :group 'tramp
1098 :type 'regexp)
1100 (defcustom tramp-terminal-prompt-regexp
1101 (concat "\\("
1102 "TERM = (.*)"
1103 "\\|"
1104 "Terminal type\\? \\[.*\\]"
1105 "\\)\\s-*")
1106 "Regular expression matching all terminal setting prompts.
1107 The regexp should match at end of buffer.
1108 The answer will be provided by `tramp-action-terminal', which see."
1109 :group 'tramp
1110 :type 'regexp)
1112 (defcustom tramp-operation-not-permitted-regexp
1113 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
1114 (regexp-opt '("Operation not permitted") t))
1115 "Regular expression matching keep-date problems in (s)cp operations.
1116 Copying has been performed successfully already, so this message can
1117 be ignored safely."
1118 :group 'tramp
1119 :type 'regexp)
1121 (defcustom tramp-copy-failed-regexp
1122 (concat "\\(.+: "
1123 (regexp-opt '("Permission denied"
1124 "not a regular file"
1125 "is a directory"
1126 "No such file or directory") t)
1127 "\\)\\s-*")
1128 "Regular expression matching copy problems in (s)cp operations."
1129 :group 'tramp
1130 :type 'regexp)
1132 (defcustom tramp-process-alive-regexp
1134 "Regular expression indicating a process has finished.
1135 In fact this expression is empty by intention, it will be used only to
1136 check regularly the status of the associated process.
1137 The answer will be provided by `tramp-action-process-alive',
1138 `tramp-action-out-of-band', which see."
1139 :group 'tramp
1140 :type 'regexp)
1142 (defcustom tramp-temp-name-prefix "tramp."
1143 "*Prefix to use for temporary files.
1144 If this is a relative file name (such as \"tramp.\"), it is considered
1145 relative to the directory name returned by the function
1146 `tramp-compat-temporary-file-directory' (which see). It may also be an
1147 absolute file name; don't forget to include a prefix for the filename
1148 part, though."
1149 :group 'tramp
1150 :type 'string)
1152 (defconst tramp-temp-buffer-name " *tramp temp*"
1153 "Buffer name for a temporary buffer.
1154 It shall be used in combination with `generate-new-buffer-name'.")
1156 (defvar tramp-temp-buffer-file-name nil
1157 "File name of a persistent local temporary file.
1158 Useful for \"rsync\" like methods.")
1159 (make-variable-buffer-local 'tramp-temp-buffer-file-name)
1161 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
1162 "*Alist specifying extra arguments to pass to the remote shell.
1163 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
1164 matching the shell file name and ARGS is a string specifying the
1165 arguments.
1167 This variable is only used when Tramp needs to start up another shell
1168 for tilde expansion. The extra arguments should typically prevent the
1169 shell from reading its init file."
1170 :group 'tramp
1171 ;; This might be the wrong way to test whether the widget type
1172 ;; `alist' is available. Who knows the right way to test it?
1173 :type (if (get 'alist 'widget-type)
1174 '(alist :key-type string :value-type string)
1175 '(repeat (cons string string))))
1177 ;; XEmacs is distributed with few Lisp packages. Further packages are
1178 ;; installed using EFS. If we use a unified filename format, then
1179 ;; Tramp is required in addition to EFS. (But why can't Tramp just
1180 ;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
1181 ;; just like before.) Another reason for using a separate filename
1182 ;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
1183 ;; Tramp only knows how to deal with `file-name-handler-alist', not
1184 ;; the other places.
1186 ;; Currently, we have the choice between 'ftp, 'sep, and 'url.
1187 ;;;###autoload
1188 (defcustom tramp-syntax
1189 (if (featurep 'xemacs) 'sep 'ftp)
1190 "Tramp filename syntax to be used.
1192 It can have the following values:
1194 'ftp -- Ange-FTP respective EFS like syntax (GNU Emacs default)
1195 'sep -- Syntax as defined for XEmacs (not available yet for GNU Emacs)
1196 'url -- URL-like syntax."
1197 :group 'tramp
1198 :type (if (featurep 'xemacs)
1199 '(choice (const :tag "EFS" ftp)
1200 (const :tag "XEmacs" sep)
1201 (const :tag "URL" url))
1202 '(choice (const :tag "Ange-FTP" ftp)
1203 (const :tag "URL" url))))
1205 (defconst tramp-prefix-format
1206 (cond ((equal tramp-syntax 'ftp) "/")
1207 ((equal tramp-syntax 'sep) "/[")
1208 ((equal tramp-syntax 'url) "/")
1209 (t (error "Wrong `tramp-syntax' defined")))
1210 "*String matching the very beginning of Tramp file names.
1211 Used in `tramp-make-tramp-file-name'.")
1213 (defconst tramp-prefix-regexp
1214 (concat "^" (regexp-quote tramp-prefix-format))
1215 "*Regexp matching the very beginning of Tramp file names.
1216 Should always start with \"^\". Derived from `tramp-prefix-format'.")
1218 (defconst tramp-method-regexp
1219 "[a-zA-Z_0-9-]+"
1220 "*Regexp matching methods identifiers.")
1222 (defconst tramp-postfix-method-format
1223 (cond ((equal tramp-syntax 'ftp) ":")
1224 ((equal tramp-syntax 'sep) "/")
1225 ((equal tramp-syntax 'url) "://")
1226 (t (error "Wrong `tramp-syntax' defined")))
1227 "*String matching delimeter between method and user or host names.
1228 Used in `tramp-make-tramp-file-name'.")
1230 (defconst tramp-postfix-method-regexp
1231 (regexp-quote tramp-postfix-method-format)
1232 "*Regexp matching delimeter between method and user or host names.
1233 Derived from `tramp-postfix-method-format'.")
1235 (defconst tramp-user-regexp
1236 "[^:/ \t]+"
1237 "*Regexp matching user names.")
1239 (defconst tramp-prefix-domain-format "%"
1240 "*String matching delimeter between user and domain names.")
1242 (defconst tramp-prefix-domain-regexp
1243 (regexp-quote tramp-prefix-domain-format)
1244 "*Regexp matching delimeter between user and domain names.
1245 Derived from `tramp-prefix-domain-format'.")
1247 (defconst tramp-domain-regexp
1248 "[-a-zA-Z0-9_.]+"
1249 "*Regexp matching domain names.")
1251 (defconst tramp-user-with-domain-regexp
1252 (concat "\\(" tramp-user-regexp "\\)"
1253 tramp-prefix-domain-regexp
1254 "\\(" tramp-domain-regexp "\\)")
1255 "*Regexp matching user names with domain names.")
1257 (defconst tramp-postfix-user-format
1259 "*String matching delimeter between user and host names.
1260 Used in `tramp-make-tramp-file-name'.")
1262 (defconst tramp-postfix-user-regexp
1263 (regexp-quote tramp-postfix-user-format)
1264 "*Regexp matching delimeter between user and host names.
1265 Derived from `tramp-postfix-user-format'.")
1267 (defconst tramp-host-regexp
1268 "[a-zA-Z0-9_.-]+"
1269 "*Regexp matching host names.")
1271 (defconst tramp-prefix-ipv6-format
1272 (cond ((equal tramp-syntax 'ftp) "[")
1273 ((equal tramp-syntax 'sep) "")
1274 ((equal tramp-syntax 'url) "[")
1275 (t (error "Wrong `tramp-syntax' defined")))
1276 "*String matching left hand side of IPv6 addresses.
1277 Used in `tramp-make-tramp-file-name'.")
1279 (defconst tramp-prefix-ipv6-regexp
1280 (regexp-quote tramp-prefix-ipv6-format)
1281 "*Regexp matching left hand side of IPv6 addresses.
1282 Derived from `tramp-prefix-ipv6-format'.")
1284 ;; The following regexp is a bit sloppy. But it shall serve our
1285 ;; purposes. It covers also IPv4 mapped IPv6 addresses, like in
1286 ;; "::ffff:192.168.0.1".
1287 (defconst tramp-ipv6-regexp
1288 "\\(?:\\(?:[a-zA-Z0-9]+\\)?:\\)+[a-zA-Z0-9.]+"
1289 "*Regexp matching IPv6 addresses.")
1291 (defconst tramp-postfix-ipv6-format
1292 (cond ((equal tramp-syntax 'ftp) "]")
1293 ((equal tramp-syntax 'sep) "")
1294 ((equal tramp-syntax 'url) "]")
1295 (t (error "Wrong `tramp-syntax' defined")))
1296 "*String matching right hand side of IPv6 addresses.
1297 Used in `tramp-make-tramp-file-name'.")
1299 (defconst tramp-postfix-ipv6-regexp
1300 (regexp-quote tramp-postfix-ipv6-format)
1301 "*Regexp matching right hand side of IPv6 addresses.
1302 Derived from `tramp-postfix-ipv6-format'.")
1304 (defconst tramp-prefix-port-format
1305 (cond ((equal tramp-syntax 'ftp) "#")
1306 ((equal tramp-syntax 'sep) "#")
1307 ((equal tramp-syntax 'url) ":")
1308 (t (error "Wrong `tramp-syntax' defined")))
1309 "*String matching delimeter between host names and port numbers.")
1311 (defconst tramp-prefix-port-regexp
1312 (regexp-quote tramp-prefix-port-format)
1313 "*Regexp matching delimeter between host names and port numbers.
1314 Derived from `tramp-prefix-port-format'.")
1316 (defconst tramp-port-regexp
1317 "[0-9]+"
1318 "*Regexp matching port numbers.")
1320 (defconst tramp-host-with-port-regexp
1321 (concat "\\(" tramp-host-regexp "\\)"
1322 tramp-prefix-port-regexp
1323 "\\(" tramp-port-regexp "\\)")
1324 "*Regexp matching host names with port numbers.")
1326 (defconst tramp-postfix-host-format
1327 (cond ((equal tramp-syntax 'ftp) ":")
1328 ((equal tramp-syntax 'sep) "]")
1329 ((equal tramp-syntax 'url) "")
1330 (t (error "Wrong `tramp-syntax' defined")))
1331 "*String matching delimeter between host names and localnames.
1332 Used in `tramp-make-tramp-file-name'.")
1334 (defconst tramp-postfix-host-regexp
1335 (regexp-quote tramp-postfix-host-format)
1336 "*Regexp matching delimeter between host names and localnames.
1337 Derived from `tramp-postfix-host-format'.")
1339 (defconst tramp-localname-regexp
1340 ".*$"
1341 "*Regexp matching localnames.")
1343 ;; File name format.
1345 (defconst tramp-file-name-structure
1346 (list
1347 (concat
1348 tramp-prefix-regexp
1349 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp "\\)?"
1350 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
1351 "\\(" "\\(" tramp-host-regexp
1352 "\\|"
1353 tramp-prefix-ipv6-regexp tramp-ipv6-regexp
1354 tramp-postfix-ipv6-regexp "\\)"
1355 "\\(" tramp-prefix-port-regexp tramp-port-regexp "\\)?" "\\)?"
1356 tramp-postfix-host-regexp
1357 "\\(" tramp-localname-regexp "\\)")
1358 2 4 5 8)
1360 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1361 the Tramp file name structure.
1363 The first element REGEXP is a regular expression matching a Tramp file
1364 name. The regex should contain parentheses around the method name,
1365 the user name, the host name, and the file name parts.
1367 The second element METHOD is a number, saying which pair of
1368 parentheses matches the method name. The third element USER is
1369 similar, but for the user name. The fourth element HOST is similar,
1370 but for the host name. The fifth element FILE is for the file name.
1371 These numbers are passed directly to `match-string', which see. That
1372 means the opening parentheses are counted to identify the pair.
1374 See also `tramp-file-name-regexp'.")
1376 ;;;###autoload
1377 (defconst tramp-file-name-regexp-unified
1378 "\\`/\\([^[/:]+\\|[^/]+]\\):"
1379 "Value for `tramp-file-name-regexp' for unified remoting.
1380 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1381 Tramp. See `tramp-file-name-structure' for more explanations.")
1383 ;;;###autoload
1384 (defconst tramp-file-name-regexp-separate
1385 "\\`/\\[.*\\]"
1386 "Value for `tramp-file-name-regexp' for separate remoting.
1387 XEmacs uses a separate filename syntax for Tramp and EFS.
1388 See `tramp-file-name-structure' for more explanations.")
1390 ;;;###autoload
1391 (defconst tramp-file-name-regexp-url
1392 "\\`/[^/:]+://"
1393 "Value for `tramp-file-name-regexp' for URL-like remoting.
1394 See `tramp-file-name-structure' for more explanations.")
1396 ;;;###autoload
1397 (defconst tramp-file-name-regexp
1398 (cond ((equal tramp-syntax 'ftp) tramp-file-name-regexp-unified)
1399 ((equal tramp-syntax 'sep) tramp-file-name-regexp-separate)
1400 ((equal tramp-syntax 'url) tramp-file-name-regexp-url)
1401 (t (error "Wrong `tramp-syntax' defined")))
1402 "*Regular expression matching file names handled by Tramp.
1403 This regexp should match Tramp file names but no other file names.
1404 \(When tramp.el is loaded, this regular expression is prepended to
1405 `file-name-handler-alist', and that is searched sequentially. Thus,
1406 if the Tramp entry appears rather early in the `file-name-handler-alist'
1407 and is a bit too general, then some files might be considered Tramp
1408 files which are not really Tramp files.
1410 Please note that the entry in `file-name-handler-alist' is made when
1411 this file (tramp.el) is loaded. This means that this variable must be set
1412 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1413 updated after changing this variable.
1415 Also see `tramp-file-name-structure'.")
1417 ;;;###autoload
1418 (defconst tramp-root-regexp
1419 (if (memq system-type '(cygwin windows-nt))
1420 "\\`\\([a-zA-Z]:\\)?/"
1421 "\\`/")
1422 "Beginning of an incomplete Tramp file name.
1423 Usually, it is just \"\\\\`/\". On W32 systems, there might be a
1424 volume letter, which will be removed by `tramp-drop-volume-letter'.")
1426 ;;;###autoload
1427 (defconst tramp-completion-file-name-regexp-unified
1428 (concat tramp-root-regexp "[^/]*\\'")
1429 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1430 GNU Emacs uses a unified filename syntax for Tramp and Ange-FTP.
1431 See `tramp-file-name-structure' for more explanations.")
1433 ;;;###autoload
1434 (defconst tramp-completion-file-name-regexp-separate
1435 (concat tramp-root-regexp "\\([[][^]]*\\)?\\'")
1436 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1437 XEmacs uses a separate filename syntax for Tramp and EFS.
1438 See `tramp-file-name-structure' for more explanations.")
1440 ;;;###autoload
1441 (defconst tramp-completion-file-name-regexp-url
1442 (concat tramp-root-regexp "[^/:]+\\(:\\(/\\(/[^/]*\\)?\\)?\\)?\\'")
1443 "Value for `tramp-completion-file-name-regexp' for URL-like remoting.
1444 See `tramp-file-name-structure' for more explanations.")
1446 ;;;###autoload
1447 (defconst tramp-completion-file-name-regexp
1448 (cond ((equal tramp-syntax 'ftp) tramp-completion-file-name-regexp-unified)
1449 ((equal tramp-syntax 'sep) tramp-completion-file-name-regexp-separate)
1450 ((equal tramp-syntax 'url) tramp-completion-file-name-regexp-url)
1451 (t (error "Wrong `tramp-syntax' defined")))
1452 "*Regular expression matching file names handled by Tramp completion.
1453 This regexp should match partial Tramp file names only.
1455 Please note that the entry in `file-name-handler-alist' is made when
1456 this file (tramp.el) is loaded. This means that this variable must be set
1457 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1458 updated after changing this variable.
1460 Also see `tramp-file-name-structure'.")
1462 (defconst tramp-actions-before-shell
1463 '((tramp-login-prompt-regexp tramp-action-login)
1464 (tramp-password-prompt-regexp tramp-action-password)
1465 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1466 (shell-prompt-pattern tramp-action-succeed)
1467 (tramp-shell-prompt-pattern tramp-action-succeed)
1468 (tramp-yesno-prompt-regexp tramp-action-yesno)
1469 (tramp-yn-prompt-regexp tramp-action-yn)
1470 (tramp-terminal-prompt-regexp tramp-action-terminal)
1471 (tramp-process-alive-regexp tramp-action-process-alive))
1472 "List of pattern/action pairs.
1473 Whenever a pattern matches, the corresponding action is performed.
1474 Each item looks like (PATTERN ACTION).
1476 The PATTERN should be a symbol, a variable. The value of this
1477 variable gives the regular expression to search for. Note that the
1478 regexp must match at the end of the buffer, \"\\'\" is implicitly
1479 appended to it.
1481 The ACTION should also be a symbol, but a function. When the
1482 corresponding PATTERN matches, the ACTION function is called.")
1484 (defconst tramp-actions-copy-out-of-band
1485 '((tramp-password-prompt-regexp tramp-action-password)
1486 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1487 (tramp-copy-failed-regexp tramp-action-permission-denied)
1488 (tramp-process-alive-regexp tramp-action-out-of-band))
1489 "List of pattern/action pairs.
1490 This list is used for copying/renaming with out-of-band methods.
1492 See `tramp-actions-before-shell' for more info.")
1494 ;; Chunked sending kludge. We set this to 500 for black-listed constellations
1495 ;; known to have a bug in `process-send-string'; some ssh connections appear
1496 ;; to drop bytes when data is sent too quickly. There is also a connection
1497 ;; buffer local variable, which is computed depending on remote host properties
1498 ;; when `tramp-chunksize' is zero or nil.
1499 (defcustom tramp-chunksize
1500 (when (and (not (featurep 'xemacs))
1501 (memq system-type '(hpux)))
1502 500)
1503 ;; Parentheses in docstring starting at beginning of line are escaped.
1504 ;; Fontification is messed up when
1505 ;; `open-paren-in-column-0-is-defun-start' set to t.
1506 "*If non-nil, chunksize for sending input to local process.
1507 It is necessary only on systems which have a buggy `process-send-string'
1508 implementation. The necessity, whether this variable must be set, can be
1509 checked via the following code:
1511 (with-temp-buffer
1512 (let* ((user \"xxx\") (host \"yyy\")
1513 (init 0) (step 50)
1514 (sent init) (received init))
1515 (while (= sent received)
1516 (setq sent (+ sent step))
1517 (erase-buffer)
1518 (let ((proc (start-process (buffer-name) (current-buffer)
1519 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1520 (when (memq (process-status proc) '(run open))
1521 (process-send-string proc (make-string sent ?\\ ))
1522 (process-send-eof proc)
1523 (process-send-eof proc))
1524 (while (not (progn (goto-char (point-min))
1525 (re-search-forward \"\\\\w+\" (point-max) t)))
1526 (accept-process-output proc 1))
1527 (when (memq (process-status proc) '(run open))
1528 (setq received (string-to-number (match-string 0)))
1529 (delete-process proc)
1530 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1531 (sit-for 0))))
1532 (if (> sent (+ init step))
1533 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1534 (- sent step))
1535 (message \"Test does not work\")
1536 (display-buffer (current-buffer))
1537 (sit-for 30))))
1539 In the Emacs normally running Tramp, evaluate the above code
1540 \(replace \"xxx\" and \"yyy\" by the remote user and host name,
1541 respectively). You can do this, for example, by pasting it into
1542 the `*scratch*' buffer and then hitting C-j with the cursor after the
1543 last closing parenthesis. Note that it works only if you have configured
1544 \"ssh\" to run without password query, see ssh-agent(1).
1546 You will see the number of bytes sent successfully to the remote host.
1547 If that number exceeds 1000, you can stop the execution by hitting
1548 C-g, because your Emacs is likely clean.
1550 When it is necessary to set `tramp-chunksize', you might consider to
1551 use an out-of-the-band method (like \"scp\") instead of an internal one
1552 \(like \"ssh\"), because setting `tramp-chunksize' to non-nil decreases
1553 performance.
1555 If your Emacs is buggy, the code stops and gives you an indication
1556 about the value `tramp-chunksize' should be set. Maybe you could just
1557 experiment a bit, e.g. changing the values of `init' and `step'
1558 in the third line of the code.
1560 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1561 this variable to be set as well."
1562 :group 'tramp
1563 :type '(choice (const nil) integer))
1565 ;; Logging in to a remote host normally requires obtaining a pty. But
1566 ;; Emacs on MacOS X has process-connection-type set to nil by default,
1567 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1568 ;; for an override of the system default.
1569 (defcustom tramp-process-connection-type t
1570 "Overrides `process-connection-type' for connections from Tramp.
1571 Tramp binds process-connection-type to the value given here before
1572 opening a connection to a remote host."
1573 :group 'tramp
1574 :type '(choice (const nil) (const t) (const pty)))
1576 (defcustom tramp-completion-reread-directory-timeout 10
1577 "Defines seconds since last remote command before rereading a directory.
1578 A remote directory might have changed its contents. In order to
1579 make it visible during file name completion in the minibuffer,
1580 Tramp flushes its cache and rereads the directory contents when
1581 more than `tramp-completion-reread-directory-timeout' seconds
1582 have been gone since last remote command execution. A value of 0
1583 would require an immediate reread during filename completion, nil
1584 means to use always cached values for the directory contents."
1585 :group 'tramp
1586 :type '(choice (const nil) integer))
1588 ;;; Internal Variables:
1590 (defvar tramp-end-of-output
1591 (format
1592 "///%s$"
1593 (md5 (concat (prin1-to-string process-environment) (current-time-string))))
1594 "String used to recognize end of output.
1595 The '$' character at the end is quoted; the string cannot be
1596 detected as prompt when being sent on echoing hosts, therefore.")
1598 (defvar tramp-current-method nil
1599 "Connection method for this *tramp* buffer.")
1601 (defvar tramp-current-user nil
1602 "Remote login name for this *tramp* buffer.")
1604 (defvar tramp-current-host nil
1605 "Remote host for this *tramp* buffer.")
1607 (defconst tramp-uudecode
1608 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1609 cat /tmp/tramp.$$
1610 rm -f /tmp/tramp.$$"
1611 "Shell function to implement `uudecode' to standard output.
1612 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
1613 for this or `uudecode -p', but some systems don't, and for them
1614 we have this shell function.")
1616 (defconst tramp-perl-file-truename
1617 "%s -e '
1618 use File::Spec;
1619 use Cwd \"realpath\";
1621 sub recursive {
1622 my ($volume, @dirs) = @_;
1623 my $real = realpath(File::Spec->catpath(
1624 $volume, File::Spec->catdir(@dirs), \"\"));
1625 if ($real) {
1626 my ($vol, $dir) = File::Spec->splitpath($real, 1);
1627 return ($vol, File::Spec->splitdir($dir));
1629 else {
1630 my $last = pop(@dirs);
1631 ($volume, @dirs) = recursive($volume, @dirs);
1632 push(@dirs, $last);
1633 return ($volume, @dirs);
1637 $result = realpath($ARGV[0]);
1638 if (!$result) {
1639 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
1640 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
1642 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
1645 if ($ARGV[0] =~ /\\/$/) {
1646 $result = $result . \"/\";
1649 print \"\\\"$result\\\"\\n\";
1650 ' \"$1\" 2>/dev/null"
1651 "Perl script to produce output suitable for use with `file-truename'
1652 on the remote file system.
1653 Escape sequence %s is replaced with name of Perl binary.
1654 This string is passed to `format', so percent characters need to be doubled.")
1656 (defconst tramp-perl-file-name-all-completions
1657 "%s -e 'sub case {
1658 my $str = shift;
1659 if ($ARGV[2]) {
1660 return lc($str);
1662 else {
1663 return $str;
1666 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
1667 @files = readdir(d); closedir(d);
1668 foreach $f (@files) {
1669 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
1670 if (-d \"$ARGV[0]/$f\") {
1671 print \"$f/\\n\";
1673 else {
1674 print \"$f\\n\";
1678 print \"ok\\n\"
1679 ' \"$1\" \"$2\" \"$3\" 2>/dev/null"
1680 "Perl script to produce output suitable for use with
1681 `file-name-all-completions' on the remote file system. Escape
1682 sequence %s is replaced with name of Perl binary. This string is
1683 passed to `format', so percent characters need to be doubled.")
1685 ;; Perl script to implement `file-attributes' in a Lisp `read'able
1686 ;; output. If you are hacking on this, note that you get *no* output
1687 ;; unless this spits out a complete line, including the '\n' at the
1688 ;; end.
1689 ;; The device number is returned as "-1", because there will be a virtual
1690 ;; device number set in `tramp-handle-file-attributes'.
1691 (defconst tramp-perl-file-attributes
1692 "%s -e '
1693 @stat = lstat($ARGV[0]);
1694 if (!@stat) {
1695 print \"nil\\n\";
1696 exit 0;
1698 if (($stat[2] & 0170000) == 0120000)
1700 $type = readlink($ARGV[0]);
1701 $type = \"\\\"$type\\\"\";
1703 elsif (($stat[2] & 0170000) == 040000)
1705 $type = \"t\";
1707 else
1709 $type = \"nil\"
1711 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1712 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1713 printf(
1714 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
1715 $type,
1716 $stat[3],
1717 $uid,
1718 $gid,
1719 $stat[8] >> 16 & 0xffff,
1720 $stat[8] & 0xffff,
1721 $stat[9] >> 16 & 0xffff,
1722 $stat[9] & 0xffff,
1723 $stat[10] >> 16 & 0xffff,
1724 $stat[10] & 0xffff,
1725 $stat[7],
1726 $stat[2],
1727 $stat[1] >> 16 & 0xffff,
1728 $stat[1] & 0xffff
1729 );' \"$1\" \"$2\" \"$3\" 2>/dev/null"
1730 "Perl script to produce output suitable for use with `file-attributes'
1731 on the remote file system.
1732 Escape sequence %s is replaced with name of Perl binary.
1733 This string is passed to `format', so percent characters need to be doubled.")
1735 (defconst tramp-perl-directory-files-and-attributes
1736 "%s -e '
1737 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
1738 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
1739 @list = readdir(DIR);
1740 closedir(DIR);
1741 $n = scalar(@list);
1742 printf(\"(\\n\");
1743 for($i = 0; $i < $n; $i++)
1745 $filename = $list[$i];
1746 @stat = lstat($filename);
1747 if (($stat[2] & 0170000) == 0120000)
1749 $type = readlink($filename);
1750 $type = \"\\\"$type\\\"\";
1752 elsif (($stat[2] & 0170000) == 040000)
1754 $type = \"t\";
1756 else
1758 $type = \"nil\"
1760 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1761 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1762 printf(
1763 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
1764 $filename,
1765 $type,
1766 $stat[3],
1767 $uid,
1768 $gid,
1769 $stat[8] >> 16 & 0xffff,
1770 $stat[8] & 0xffff,
1771 $stat[9] >> 16 & 0xffff,
1772 $stat[9] & 0xffff,
1773 $stat[10] >> 16 & 0xffff,
1774 $stat[10] & 0xffff,
1775 $stat[7],
1776 $stat[2],
1777 $stat[1] >> 16 & 0xffff,
1778 $stat[1] & 0xffff,
1779 $stat[0] >> 16 & 0xffff,
1780 $stat[0] & 0xffff);
1782 printf(\")\\n\");' \"$1\" \"$2\" \"$3\" 2>/dev/null"
1783 "Perl script implementing `directory-files-attributes' as Lisp `read'able
1784 output.
1785 Escape sequence %s is replaced with name of Perl binary.
1786 This string is passed to `format', so percent characters need to be doubled.")
1788 ;; ;; These two use uu encoding.
1789 ;; (defvar tramp-perl-encode "%s -e'\
1790 ;; print qq(begin 644 xxx\n);
1791 ;; my $s = q();
1792 ;; my $res = q();
1793 ;; while (read(STDIN, $s, 45)) {
1794 ;; print pack(q(u), $s);
1795 ;; }
1796 ;; print qq(`\n);
1797 ;; print qq(end\n);
1798 ;; '"
1799 ;; "Perl program to use for encoding a file.
1800 ;; Escape sequence %s is replaced with name of Perl binary.")
1802 ;; (defvar tramp-perl-decode "%s -ne '
1803 ;; print unpack q(u), $_;
1804 ;; '"
1805 ;; "Perl program to use for decoding a file.
1806 ;; Escape sequence %s is replaced with name of Perl binary.")
1808 ;; These two use base64 encoding.
1809 (defconst tramp-perl-encode-with-module
1810 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
1811 "Perl program to use for encoding a file.
1812 Escape sequence %s is replaced with name of Perl binary.
1813 This string is passed to `format', so percent characters need to be doubled.
1814 This implementation requires the MIME::Base64 Perl module to be installed
1815 on the remote host.")
1817 (defconst tramp-perl-decode-with-module
1818 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
1819 "Perl program to use for decoding a file.
1820 Escape sequence %s is replaced with name of Perl binary.
1821 This string is passed to `format', so percent characters need to be doubled.
1822 This implementation requires the MIME::Base64 Perl module to be installed
1823 on the remote host.")
1825 (defconst tramp-perl-encode
1826 "%s -e '
1827 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1828 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
1829 # Free Software Foundation, Inc.
1830 use strict;
1832 my %%trans = do {
1833 my $i = 0;
1834 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1835 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1838 binmode(\\*STDIN);
1840 # We read in chunks of 54 bytes, to generate output lines
1841 # of 72 chars (plus end of line)
1842 $/ = \\54;
1844 while (my $data = <STDIN>) {
1845 my $pad = q();
1847 # Only for the last chunk, and only if did not fill the last three-byte packet
1848 if (eof) {
1849 my $mod = length($data) %% 3;
1850 $pad = q(=) x (3 - $mod) if $mod;
1853 # Not the fastest method, but it is simple: unpack to binary string, split
1854 # by groups of 6 bits and convert back from binary to byte; then map into
1855 # the translation table
1856 print
1857 join q(),
1858 map($trans{$_},
1859 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1860 $pad,
1861 qq(\\n);
1862 }' 2>/dev/null"
1863 "Perl program to use for encoding a file.
1864 Escape sequence %s is replaced with name of Perl binary.
1865 This string is passed to `format', so percent characters need to be doubled.")
1867 (defconst tramp-perl-decode
1868 "%s -e '
1869 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1870 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009
1871 # Free Software Foundation, Inc.
1872 use strict;
1874 my %%trans = do {
1875 my $i = 0;
1876 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1877 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1880 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1882 binmode(\\*STDOUT);
1884 # We are going to accumulate into $pending to accept any line length
1885 # (we do not check they are <= 76 chars as the RFC says)
1886 my $pending = q();
1888 while (my $data = <STDIN>) {
1889 chomp $data;
1891 # If we find one or two =, we have reached the end and
1892 # any following data is to be discarded
1893 my $finished = $data =~ s/(==?).*/$1/;
1894 $pending .= $data;
1896 my $len = length($pending);
1897 my $chunk = substr($pending, 0, $len & ~3);
1898 $pending = substr($pending, $len & ~3 + 1);
1900 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1901 # split in 8-bit chunks and convert back to char.
1902 print join q(),
1903 map $bytes{$_},
1904 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1906 last if $finished;
1907 }' 2>/dev/null"
1908 "Perl program to use for decoding a file.
1909 Escape sequence %s is replaced with name of Perl binary.
1910 This string is passed to `format', so percent characters need to be doubled.")
1912 (defconst tramp-vc-registered-read-file-names
1913 "echo \"(\"
1914 for file in \"$@\"; do
1915 if %s $file; then
1916 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
1917 else
1918 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
1920 if %s $file; then
1921 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
1922 else
1923 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
1925 done
1926 echo \")\""
1927 "Script to check existence of VC related files.
1928 It must be send formatted with two strings; the tests for file
1929 existence, and file readability.")
1931 (defconst tramp-file-mode-type-map
1932 '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1933 (1 . "p") ; fifo
1934 (2 . "c") ; character device
1935 (3 . "m") ; multiplexed character device (v7)
1936 (4 . "d") ; directory
1937 (5 . "?") ; Named special file (XENIX)
1938 (6 . "b") ; block device
1939 (7 . "?") ; multiplexed block device (v7)
1940 (8 . "-") ; regular file
1941 (9 . "n") ; network special file (HP-UX)
1942 (10 . "l") ; symlink
1943 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
1944 (12 . "s") ; socket
1945 (13 . "D") ; door special (Solaris)
1946 (14 . "w")) ; whiteout (BSD)
1947 "A list of file types returned from the `stat' system call.
1948 This is used to map a mode number to a permission string.")
1950 ;; New handlers should be added here. The following operations can be
1951 ;; handled using the normal primitives: file-name-sans-versions,
1952 ;; get-file-buffer.
1953 (defconst tramp-file-name-handler-alist
1954 '((load . tramp-handle-load)
1955 (make-symbolic-link . tramp-handle-make-symbolic-link)
1956 (file-name-as-directory . tramp-handle-file-name-as-directory)
1957 (file-name-directory . tramp-handle-file-name-directory)
1958 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1959 (file-truename . tramp-handle-file-truename)
1960 (file-exists-p . tramp-handle-file-exists-p)
1961 (file-directory-p . tramp-handle-file-directory-p)
1962 (file-executable-p . tramp-handle-file-executable-p)
1963 (file-readable-p . tramp-handle-file-readable-p)
1964 (file-regular-p . tramp-handle-file-regular-p)
1965 (file-symlink-p . tramp-handle-file-symlink-p)
1966 (file-writable-p . tramp-handle-file-writable-p)
1967 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
1968 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
1969 (file-attributes . tramp-handle-file-attributes)
1970 (file-modes . tramp-handle-file-modes)
1971 (directory-files . tramp-handle-directory-files)
1972 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes)
1973 (file-name-all-completions . tramp-handle-file-name-all-completions)
1974 (file-name-completion . tramp-handle-file-name-completion)
1975 (add-name-to-file . tramp-handle-add-name-to-file)
1976 (copy-file . tramp-handle-copy-file)
1977 (copy-directory . tramp-handle-copy-directory)
1978 (rename-file . tramp-handle-rename-file)
1979 (set-file-modes . tramp-handle-set-file-modes)
1980 (set-file-times . tramp-handle-set-file-times)
1981 (make-directory . tramp-handle-make-directory)
1982 (delete-directory . tramp-handle-delete-directory)
1983 (delete-file . tramp-handle-delete-file)
1984 (directory-file-name . tramp-handle-directory-file-name)
1985 ;; `executable-find' is not official yet.
1986 (executable-find . tramp-handle-executable-find)
1987 (start-file-process . tramp-handle-start-file-process)
1988 (process-file . tramp-handle-process-file)
1989 (shell-command . tramp-handle-shell-command)
1990 (insert-directory . tramp-handle-insert-directory)
1991 (expand-file-name . tramp-handle-expand-file-name)
1992 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
1993 (file-local-copy . tramp-handle-file-local-copy)
1994 (file-remote-p . tramp-handle-file-remote-p)
1995 (insert-file-contents . tramp-handle-insert-file-contents)
1996 (insert-file-contents-literally
1997 . tramp-handle-insert-file-contents-literally)
1998 (write-region . tramp-handle-write-region)
1999 (find-backup-file-name . tramp-handle-find-backup-file-name)
2000 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
2001 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
2002 (dired-compress-file . tramp-handle-dired-compress-file)
2003 (dired-recursive-delete-directory
2004 . tramp-handle-dired-recursive-delete-directory)
2005 (dired-uncache . tramp-handle-dired-uncache)
2006 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
2007 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
2008 (vc-registered . tramp-handle-vc-registered))
2009 "Alist of handler functions.
2010 Operations not mentioned here will be handled by the normal Emacs functions.")
2012 ;; Handlers for partial Tramp file names. For Emacs just
2013 ;; `file-name-all-completions' is needed.
2014 ;;;###autoload
2015 (defconst tramp-completion-file-name-handler-alist
2016 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions)
2017 (file-name-completion . tramp-completion-handle-file-name-completion))
2018 "Alist of completion handler functions.
2019 Used for file names matching `tramp-file-name-regexp'. Operations not
2020 mentioned here will be handled by `tramp-file-name-handler-alist' or the
2021 normal Emacs functions.")
2023 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
2024 (defvar tramp-foreign-file-name-handler-alist
2025 ;; (identity . tramp-sh-file-name-handler) should always be the last
2026 ;; entry, because `identity' always matches.
2027 '((identity . tramp-sh-file-name-handler))
2028 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
2029 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
2030 calling HANDLER.")
2032 ;;; Internal functions which must come first:
2034 (defsubst tramp-debug-message (vec fmt-string &rest args)
2035 "Append message to debug buffer.
2036 Message is formatted with FMT-STRING as control string and the remaining
2037 ARGS to actually emit the message (if applicable)."
2038 (when (get-buffer (tramp-buffer-name vec))
2039 (with-current-buffer (tramp-get-debug-buffer vec)
2040 (goto-char (point-max))
2041 ;; Headline.
2042 (when (bobp)
2043 (insert
2044 (format
2045 ";; %sEmacs: %s Tramp: %s -*- mode: outline; -*-"
2046 (if (featurep 'sxemacs) "SX" (if (featurep 'xemacs) "X" "GNU "))
2047 emacs-version tramp-version)))
2048 (unless (bolp)
2049 (insert "\n"))
2050 ;; Timestamp.
2051 (let ((now (current-time)))
2052 (insert (format-time-string "%T." now))
2053 (insert (format "%06d " (nth 2 now))))
2054 ;; Calling function.
2055 (let ((btn 1) btf fn)
2056 (while (not fn)
2057 (setq btf (nth 1 (backtrace-frame btn)))
2058 (if (not btf)
2059 (setq fn "")
2060 (when (symbolp btf)
2061 (setq fn (symbol-name btf))
2062 (unless (and (string-match "^tramp" fn)
2063 (not (string-match
2064 "^tramp\\(-debug\\)?\\(-message\\|-error\\)$"
2065 fn)))
2066 (setq fn nil)))
2067 (setq btn (1+ btn))))
2068 ;; The following code inserts filename and line number.
2069 ;; Should be deactivated by default, because it is time
2070 ;; consuming.
2071 ; (let ((ffn (find-function-noselect (intern fn))))
2072 ; (insert
2073 ; (format
2074 ; "%s:%d: "
2075 ; (file-name-nondirectory (buffer-file-name (car ffn)))
2076 ; (with-current-buffer (car ffn)
2077 ; (1+ (count-lines (point-min) (cdr ffn)))))))
2078 (insert (format "%s " fn)))
2079 ;; The message.
2080 (insert (apply 'format fmt-string args)))))
2082 (defvar tramp-message-show-message t
2083 "Show Tramp message in the minibuffer.
2084 This variable is used to disable messages from `tramp-error'.
2085 The messages are visible anyway, because an error is raised.")
2087 (defsubst tramp-message (vec-or-proc level fmt-string &rest args)
2088 "Emit a message depending on verbosity level.
2089 VEC-OR-PROC identifies the Tramp buffer to use. It can be either a
2090 vector or a process. LEVEL says to be quiet if `tramp-verbose' is
2091 less than LEVEL. The message is emitted only if `tramp-verbose' is
2092 greater than or equal to LEVEL.
2094 The message is also logged into the debug buffer when `tramp-verbose'
2095 is greater than or equal 4.
2097 Calls functions `message' and `tramp-debug-message' with FMT-STRING as
2098 control string and the remaining ARGS to actually emit the message (if
2099 applicable)."
2100 (condition-case nil
2101 (when (<= level tramp-verbose)
2102 ;; Match data must be preserved!
2103 (save-match-data
2104 ;; Display only when there is a minimum level.
2105 (when (and tramp-message-show-message (<= level 3))
2106 (apply 'message
2107 (concat
2108 (cond
2109 ((= level 0) "")
2110 ((= level 1) "")
2111 ((= level 2) "Warning: ")
2112 (t "Tramp: "))
2113 fmt-string)
2114 args))
2115 ;; Log only when there is a minimum level.
2116 (when (>= tramp-verbose 4)
2117 (when (and vec-or-proc
2118 (processp vec-or-proc)
2119 (buffer-name (process-buffer vec-or-proc)))
2120 (with-current-buffer (process-buffer vec-or-proc)
2121 ;; Translate proc to vec.
2122 (setq vec-or-proc (tramp-dissect-file-name default-directory))))
2123 (when (and vec-or-proc (vectorp vec-or-proc))
2124 (apply 'tramp-debug-message
2125 vec-or-proc
2126 (concat (format "(%d) # " level) fmt-string)
2127 args)))))
2128 ;; Suppress all errors.
2129 (error nil)))
2131 (defsubst tramp-error (vec-or-proc signal fmt-string &rest args)
2132 "Emit an error.
2133 VEC-OR-PROC identifies the connection to use, SIGNAL is the
2134 signal identifier to be raised, remaining args passed to
2135 `tramp-message'. Finally, signal SIGNAL is raised."
2136 (let (tramp-message-show-message)
2137 (tramp-message
2138 vec-or-proc 1 "%s"
2139 (error-message-string
2140 (list signal
2141 (get signal 'error-message)
2142 (apply 'format fmt-string args))))
2143 (signal signal (list (apply 'format fmt-string args)))))
2145 (defsubst tramp-error-with-buffer
2146 (buffer vec-or-proc signal fmt-string &rest args)
2147 "Emit an error, and show BUFFER.
2148 If BUFFER is nil, show the connection buffer. Wait for 30\", or until
2149 an input event arrives. The other arguments are passed to `tramp-error'."
2150 (save-window-excursion
2151 (unwind-protect
2152 (apply 'tramp-error vec-or-proc signal fmt-string args)
2153 (when (and vec-or-proc (not (zerop tramp-verbose)))
2154 (let ((enable-recursive-minibuffers t))
2155 (pop-to-buffer
2156 (or (and (bufferp buffer) buffer)
2157 (and (processp vec-or-proc) (process-buffer vec-or-proc))
2158 (tramp-get-buffer vec-or-proc)))
2159 (sit-for 30))))))
2161 (defmacro with-parsed-tramp-file-name (filename var &rest body)
2162 "Parse a Tramp filename and make components available in the body.
2164 First arg FILENAME is evaluated and dissected into its components.
2165 Second arg VAR is a symbol. It is used as a variable name to hold
2166 the filename structure. It is also used as a prefix for the variables
2167 holding the components. For example, if VAR is the symbol `foo', then
2168 `foo' will be bound to the whole structure, `foo-method' will be bound to
2169 the method component, and so on for `foo-user', `foo-host', `foo-localname'.
2171 Remaining args are Lisp expressions to be evaluated (inside an implicit
2172 `progn').
2174 If VAR is nil, then we bind `v' to the structure and `method', `user',
2175 `host', `localname' to the components."
2176 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
2177 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
2178 (tramp-file-name-method ,(or var 'v)))
2179 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
2180 (tramp-file-name-user ,(or var 'v)))
2181 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
2182 (tramp-file-name-host ,(or var 'v)))
2183 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
2184 (tramp-file-name-localname ,(or var 'v))))
2185 ,@body))
2187 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
2188 (put 'with-parsed-tramp-file-name 'edebug-form-spec '(form symbolp body))
2189 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-parsed-tramp-file-name\\>"))
2191 (defmacro with-file-property (vec file property &rest body)
2192 "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
2193 FILE must be a local file name on a connection identified via VEC."
2194 `(if (file-name-absolute-p ,file)
2195 (let ((value (tramp-get-file-property ,vec ,file ,property 'undef)))
2196 (when (eq value 'undef)
2197 ;; We cannot pass @body as parameter to
2198 ;; `tramp-set-file-property' because it mangles our
2199 ;; debug messages.
2200 (setq value (progn ,@body))
2201 (tramp-set-file-property ,vec ,file ,property value))
2202 value)
2203 ,@body))
2205 (put 'with-file-property 'lisp-indent-function 3)
2206 (put 'with-file-property 'edebug-form-spec t)
2207 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-file-property\\>"))
2209 (defmacro with-connection-property (key property &rest body)
2210 "Checks in Tramp for property PROPERTY, otherwise executes BODY and set."
2211 `(let ((value (tramp-get-connection-property ,key ,property 'undef)))
2212 (when (eq value 'undef)
2213 ;; We cannot pass ,@body as parameter to
2214 ;; `tramp-set-connection-property' because it mangles our debug
2215 ;; messages.
2216 (setq value (progn ,@body))
2217 (tramp-set-connection-property ,key ,property value))
2218 value))
2220 (put 'with-connection-property 'lisp-indent-function 2)
2221 (put 'with-connection-property 'edebug-form-spec t)
2222 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-connection-property\\>"))
2224 (eval-and-compile ; silence compiler
2225 (if (memq system-type '(cygwin windows-nt))
2226 (defun tramp-drop-volume-letter (name)
2227 "Cut off unnecessary drive letter from file NAME.
2228 The function `tramp-handle-expand-file-name' calls `expand-file-name'
2229 locally on a remote file name. When the local system is a W32 system
2230 but the remote system is Unix, this introduces a superfluous drive
2231 letter into the file name. This function removes it."
2232 (save-match-data
2233 (if (string-match tramp-root-regexp name)
2234 (replace-match "/" nil t name)
2235 name)))
2237 (defalias 'tramp-drop-volume-letter 'identity)))
2239 (defsubst tramp-make-tramp-temp-file (vec)
2240 "Create a temporary file on the remote host identified by VEC.
2241 Return the local name of the temporary file."
2242 (let ((prefix
2243 (tramp-make-tramp-file-name
2244 (tramp-file-name-method vec)
2245 (tramp-file-name-user vec)
2246 (tramp-file-name-host vec)
2247 (tramp-drop-volume-letter
2248 (expand-file-name
2249 tramp-temp-name-prefix (tramp-get-remote-tmpdir vec)))))
2250 result)
2251 (while (not result)
2252 ;; `make-temp-file' would be the natural choice for
2253 ;; implementation. But it calls `write-region' internally,
2254 ;; which also needs a temporary file - we would end in an
2255 ;; infinite loop.
2256 (setq result (make-temp-name prefix))
2257 (if (file-exists-p result)
2258 (setq result nil)
2259 ;; This creates the file by side effect.
2260 (set-file-times result)
2261 (set-file-modes result (tramp-octal-to-decimal "0700"))))
2263 ;; Return the local part.
2264 (with-parsed-tramp-file-name result nil localname)))
2267 ;;; Config Manipulation Functions:
2269 (defun tramp-set-completion-function (method function-list)
2270 "Sets the list of completion functions for METHOD.
2271 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2272 The FUNCTION is intended to parse FILE according its syntax.
2273 It might be a predefined FUNCTION, or a user defined FUNCTION.
2274 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
2275 `tramp-parse-sconfig', `tramp-parse-hosts', `tramp-parse-passwd',
2276 and `tramp-parse-netrc'.
2278 Example:
2280 (tramp-set-completion-function
2281 \"ssh\"
2282 '((tramp-parse-sconfig \"/etc/ssh_config\")
2283 (tramp-parse-sconfig \"~/.ssh/config\")))"
2285 (let ((r function-list)
2286 (v function-list))
2287 (setq tramp-completion-function-alist
2288 (delete (assoc method tramp-completion-function-alist)
2289 tramp-completion-function-alist))
2291 (while v
2292 ;; Remove double entries.
2293 (when (member (car v) (cdr v))
2294 (setcdr v (delete (car v) (cdr v))))
2295 ;; Check for function and file or registry key.
2296 (unless (and (functionp (nth 0 (car v)))
2297 (if (string-match "^HKEY_CURRENT_USER" (nth 1 (car v)))
2298 ;; Windows registry.
2299 (and (memq system-type '(cygwin windows-nt))
2300 (zerop
2301 (tramp-local-call-process
2302 "reg" nil nil nil "query" (nth 1 (car v)))))
2303 ;; Configuration file.
2304 (file-exists-p (nth 1 (car v)))))
2305 (setq r (delete (car v) r)))
2306 (setq v (cdr v)))
2308 (when r
2309 (add-to-list 'tramp-completion-function-alist
2310 (cons method r)))))
2312 (defun tramp-get-completion-function (method)
2313 "Returns a list of completion functions for METHOD.
2314 For definition of that list see `tramp-set-completion-function'."
2315 (cons
2316 ;; Hosts visited once shall be remembered.
2317 `(tramp-parse-connection-properties ,method)
2318 ;; The method related defaults.
2319 (cdr (assoc method tramp-completion-function-alist))))
2322 ;;; Fontification of `read-file-name':
2324 ;; rfn-eshadow.el is part of Emacs 22. It is autoloaded.
2325 (defvar tramp-rfn-eshadow-overlay)
2326 (make-variable-buffer-local 'tramp-rfn-eshadow-overlay)
2328 (defun tramp-rfn-eshadow-setup-minibuffer ()
2329 "Set up a minibuffer for `file-name-shadow-mode'.
2330 Adds another overlay hiding filename parts according to Tramp's
2331 special handling of `substitute-in-file-name'."
2332 (when (symbol-value 'minibuffer-completing-file-name)
2333 (setq tramp-rfn-eshadow-overlay
2334 (funcall (symbol-function 'make-overlay)
2335 (funcall (symbol-function 'minibuffer-prompt-end))
2336 (funcall (symbol-function 'minibuffer-prompt-end))))
2337 ;; Copy rfn-eshadow-overlay properties.
2338 (let ((props (funcall (symbol-function 'overlay-properties)
2339 (symbol-value 'rfn-eshadow-overlay))))
2340 (while props
2341 (funcall (symbol-function 'overlay-put)
2342 tramp-rfn-eshadow-overlay (pop props) (pop props))))))
2344 (when (boundp 'rfn-eshadow-setup-minibuffer-hook)
2345 (add-hook 'rfn-eshadow-setup-minibuffer-hook
2346 'tramp-rfn-eshadow-setup-minibuffer)
2347 (add-hook 'tramp-unload-hook
2348 (lambda ()
2349 (remove-hook 'rfn-eshadow-setup-minibuffer-hook
2350 'tramp-rfn-eshadow-setup-minibuffer))))
2352 (defconst tramp-rfn-eshadow-update-overlay-regexp
2353 (format "[^%s/~]*\\(/\\|~\\)" tramp-postfix-host-format))
2355 (defun tramp-rfn-eshadow-update-overlay ()
2356 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
2357 This is intended to be used as a minibuffer `post-command-hook' for
2358 `file-name-shadow-mode'; the minibuffer should have already
2359 been set up by `rfn-eshadow-setup-minibuffer'."
2360 ;; In remote files name, there is a shadowing just for the local part.
2361 (let ((end (or (funcall (symbol-function 'overlay-end)
2362 (symbol-value 'rfn-eshadow-overlay))
2363 (funcall (symbol-function 'minibuffer-prompt-end)))))
2364 (when (file-remote-p (buffer-substring-no-properties end (point-max)))
2365 (save-excursion
2366 (save-restriction
2367 (narrow-to-region
2368 (1+ (or (string-match
2369 tramp-rfn-eshadow-update-overlay-regexp (buffer-string) end)
2370 end))
2371 (point-max))
2372 (let ((rfn-eshadow-overlay tramp-rfn-eshadow-overlay)
2373 (rfn-eshadow-update-overlay-hook nil))
2374 (move-overlay rfn-eshadow-overlay (point-max) (point-max))
2375 (funcall (symbol-function 'rfn-eshadow-update-overlay))))))))
2377 (when (boundp 'rfn-eshadow-update-overlay-hook)
2378 (add-hook 'rfn-eshadow-update-overlay-hook
2379 'tramp-rfn-eshadow-update-overlay)
2380 (add-hook 'tramp-unload-hook
2381 (lambda ()
2382 (remove-hook 'rfn-eshadow-update-overlay-hook
2383 'tramp-rfn-eshadow-update-overlay))))
2386 ;;; File Name Handler Functions:
2388 (defun tramp-handle-make-symbolic-link
2389 (filename linkname &optional ok-if-already-exists)
2390 "Like `make-symbolic-link' for Tramp files.
2391 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
2392 the symlink. If LINKNAME is a Tramp file, only the localname component is
2393 used as the target of the symlink.
2395 If LINKNAME is a Tramp file and the localname component is relative, then
2396 it is expanded first, before the localname component is taken. Note that
2397 this can give surprising results if the user/host for the source and
2398 target of the symlink differ."
2399 (with-parsed-tramp-file-name linkname l
2400 (let ((ln (tramp-get-remote-ln l))
2401 (cwd (tramp-run-real-handler
2402 'file-name-directory (list l-localname))))
2403 (unless ln
2404 (tramp-error
2405 l 'file-error
2406 "Making a symbolic link. ln(1) does not exist on the remote host."))
2408 ;; Do the 'confirm if exists' thing.
2409 (when (file-exists-p linkname)
2410 ;; What to do?
2411 (if (or (null ok-if-already-exists) ; not allowed to exist
2412 (and (numberp ok-if-already-exists)
2413 (not (yes-or-no-p
2414 (format
2415 "File %s already exists; make it a link anyway? "
2416 l-localname)))))
2417 (tramp-error
2418 l 'file-already-exists "File %s already exists" l-localname)
2419 (delete-file linkname)))
2421 ;; If FILENAME is a Tramp name, use just the localname component.
2422 (when (tramp-tramp-file-p filename)
2423 (setq filename
2424 (tramp-file-name-localname
2425 (tramp-dissect-file-name (expand-file-name filename)))))
2427 ;; Right, they are on the same host, regardless of user, method, etc.
2428 ;; We now make the link on the remote machine. This will occur as the user
2429 ;; that FILENAME belongs to.
2430 (zerop
2431 (tramp-send-command-and-check
2432 l (format "cd %s && %s -sf %s %s" cwd ln filename l-localname) t)))))
2434 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
2435 "Like `load' for Tramp files."
2436 (with-parsed-tramp-file-name (expand-file-name file) nil
2437 (unless nosuffix
2438 (cond ((file-exists-p (concat file ".elc"))
2439 (setq file (concat file ".elc")))
2440 ((file-exists-p (concat file ".el"))
2441 (setq file (concat file ".el")))))
2442 (when must-suffix
2443 ;; The first condition is always true for absolute file names.
2444 ;; Included for safety's sake.
2445 (unless (or (file-name-directory file)
2446 (string-match "\\.elc?\\'" file))
2447 (tramp-error
2448 v 'file-error
2449 "File `%s' does not include a `.el' or `.elc' suffix" file)))
2450 (unless noerror
2451 (when (not (file-exists-p file))
2452 (tramp-error v 'file-error "Cannot load nonexistent file `%s'" file)))
2453 (if (not (file-exists-p file))
2455 (unless nomessage (tramp-message v 0 "Loading %s..." file))
2456 (let ((local-copy (file-local-copy file)))
2457 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2458 (unwind-protect
2459 (load local-copy noerror t t)
2460 (delete-file local-copy)))
2461 (unless nomessage (tramp-message v 0 "Loading %s...done" file))
2462 t)))
2464 ;; Localname manipulation functions that grok Tramp localnames...
2465 (defun tramp-handle-file-name-as-directory (file)
2466 "Like `file-name-as-directory' but aware of Tramp files."
2467 ;; `file-name-as-directory' would be sufficient except localname is
2468 ;; the empty string.
2469 (let ((v (tramp-dissect-file-name file t)))
2470 ;; Run the command on the localname portion only.
2471 (tramp-make-tramp-file-name
2472 (tramp-file-name-method v)
2473 (tramp-file-name-user v)
2474 (tramp-file-name-host v)
2475 (tramp-run-real-handler
2476 'file-name-as-directory (list (or (tramp-file-name-localname v) ""))))))
2478 (defun tramp-handle-file-name-directory (file)
2479 "Like `file-name-directory' but aware of Tramp files."
2480 ;; Everything except the last filename thing is the directory. We
2481 ;; cannot apply `with-parsed-tramp-file-name', because this expands
2482 ;; the remote file name parts. This is a problem when we are in
2483 ;; file name completion.
2484 (let ((v (tramp-dissect-file-name file t)))
2485 ;; Run the command on the localname portion only.
2486 (tramp-make-tramp-file-name
2487 (tramp-file-name-method v)
2488 (tramp-file-name-user v)
2489 (tramp-file-name-host v)
2490 (tramp-run-real-handler
2491 'file-name-directory (list (or (tramp-file-name-localname v) ""))))))
2493 (defun tramp-handle-file-name-nondirectory (file)
2494 "Like `file-name-nondirectory' but aware of Tramp files."
2495 (with-parsed-tramp-file-name file nil
2496 (tramp-run-real-handler 'file-name-nondirectory (list localname))))
2498 (defun tramp-handle-file-truename (filename &optional counter prev-dirs)
2499 "Like `file-truename' for Tramp files."
2500 (with-parsed-tramp-file-name (expand-file-name filename) nil
2501 (with-file-property v localname "file-truename"
2502 (let ((result nil)) ; result steps in reverse order
2503 (tramp-message v 4 "Finding true name for `%s'" filename)
2504 (cond
2505 ;; Use GNU readlink --canonicalize-missing where available.
2506 ((tramp-get-remote-readlink v)
2507 (setq result
2508 (tramp-send-command-and-read
2510 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
2511 (tramp-get-remote-readlink v)
2512 (tramp-shell-quote-argument localname)))))
2514 ;; Use Perl implementation.
2515 ((and (tramp-get-remote-perl v)
2516 (tramp-get-connection-property v "perl-file-spec" nil)
2517 (tramp-get-connection-property v "perl-cwd-realpath" nil))
2518 (tramp-maybe-send-script
2519 v tramp-perl-file-truename "tramp_perl_file_truename")
2520 (setq result
2521 (tramp-send-command-and-read
2523 (format "tramp_perl_file_truename %s"
2524 (tramp-shell-quote-argument localname)))))
2526 ;; Do it yourself. We bind `directory-sep-char' here for
2527 ;; XEmacs on Windows, which would otherwise use backslash.
2528 (t (let* ((directory-sep-char ?/)
2529 (steps (tramp-compat-split-string localname "/"))
2530 (localnamedir (tramp-run-real-handler
2531 'file-name-as-directory (list localname)))
2532 (is-dir (string= localname localnamedir))
2533 (thisstep nil)
2534 (numchase 0)
2535 ;; Don't make the following value larger than
2536 ;; necessary. People expect an error message in a
2537 ;; timely fashion when something is wrong;
2538 ;; otherwise they might think that Emacs is hung.
2539 ;; Of course, correctness has to come first.
2540 (numchase-limit 20)
2541 symlink-target)
2542 (while (and steps (< numchase numchase-limit))
2543 (setq thisstep (pop steps))
2544 (tramp-message
2545 v 5 "Check %s"
2546 (mapconcat 'identity
2547 (append '("") (reverse result) (list thisstep))
2548 "/"))
2549 (setq symlink-target
2550 (nth 0 (file-attributes
2551 (tramp-make-tramp-file-name
2552 method user host
2553 (mapconcat 'identity
2554 (append '("")
2555 (reverse result)
2556 (list thisstep))
2557 "/")))))
2558 (cond ((string= "." thisstep)
2559 (tramp-message v 5 "Ignoring step `.'"))
2560 ((string= ".." thisstep)
2561 (tramp-message v 5 "Processing step `..'")
2562 (pop result))
2563 ((stringp symlink-target)
2564 ;; It's a symlink, follow it.
2565 (tramp-message v 5 "Follow symlink to %s" symlink-target)
2566 (setq numchase (1+ numchase))
2567 (when (file-name-absolute-p symlink-target)
2568 (setq result nil))
2569 ;; If the symlink was absolute, we'll get a string like
2570 ;; "/user@host:/some/target"; extract the
2571 ;; "/some/target" part from it.
2572 (when (tramp-tramp-file-p symlink-target)
2573 (unless (tramp-equal-remote filename symlink-target)
2574 (tramp-error
2575 v 'file-error
2576 "Symlink target `%s' on wrong host" symlink-target))
2577 (setq symlink-target localname))
2578 (setq steps
2579 (append (tramp-compat-split-string
2580 symlink-target "/")
2581 steps)))
2583 ;; It's a file.
2584 (setq result (cons thisstep result)))))
2585 (when (>= numchase numchase-limit)
2586 (tramp-error
2587 v 'file-error
2588 "Maximum number (%d) of symlinks exceeded" numchase-limit))
2589 (setq result (reverse result))
2590 ;; Combine list to form string.
2591 (setq result
2592 (if result
2593 (mapconcat 'identity (cons "" result) "/")
2594 "/"))
2595 (when (and is-dir (or (string= "" result)
2596 (not (string= (substring result -1) "/"))))
2597 (setq result (concat result "/"))))))
2599 (tramp-message v 4 "True name of `%s' is `%s'" filename result)
2600 (tramp-make-tramp-file-name method user host result)))))
2602 ;; Basic functions.
2604 (defun tramp-handle-file-exists-p (filename)
2605 "Like `file-exists-p' for Tramp files."
2606 (with-parsed-tramp-file-name filename nil
2607 (with-file-property v localname "file-exists-p"
2608 (or (not (null (tramp-get-file-property
2609 v localname "file-attributes-integer" nil)))
2610 (not (null (tramp-get-file-property
2611 v localname "file-attributes-string" nil)))
2612 (zerop (tramp-send-command-and-check
2614 (format
2615 "%s %s"
2616 (tramp-get-file-exists-command v)
2617 (tramp-shell-quote-argument localname))))))))
2619 ;; Inodes don't exist for some file systems. Therefore we must
2620 ;; generate virtual ones. Used in `find-buffer-visiting'. The method
2621 ;; applied might be not so efficient (Ange-FTP uses hashes). But
2622 ;; performance isn't the major issue given that file transfer will
2623 ;; take time.
2624 (defvar tramp-inodes nil
2625 "Keeps virtual inodes numbers.")
2627 ;; Devices must distinguish physical file systems. The device numbers
2628 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2629 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2630 ;; EFS use device number "-1". In order to be different, we use device number
2631 ;; (-1 . x), whereby "x" is unique for a given (method user host).
2632 (defvar tramp-devices nil
2633 "Keeps virtual device numbers.")
2635 ;; CCC: This should check for an error condition and signal failure
2636 ;; when something goes wrong.
2637 ;; Daniel Pittman <daniel@danann.net>
2638 (defun tramp-handle-file-attributes (filename &optional id-format)
2639 "Like `file-attributes' for Tramp files."
2640 (unless id-format (setq id-format 'integer))
2641 ;; Don't modify `last-coding-system-used' by accident.
2642 (let ((last-coding-system-used last-coding-system-used))
2643 (with-parsed-tramp-file-name (expand-file-name filename) nil
2644 (with-file-property v localname (format "file-attributes-%s" id-format)
2645 (save-excursion
2646 (tramp-convert-file-attributes
2648 (cond
2649 ((tramp-get-remote-stat v)
2650 (tramp-do-file-attributes-with-stat v localname id-format))
2651 ((tramp-get-remote-perl v)
2652 (tramp-do-file-attributes-with-perl v localname id-format))
2654 (tramp-do-file-attributes-with-ls v localname id-format)))))))))
2656 (defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
2657 "Implement `file-attributes' for Tramp files using the ls(1) command."
2658 (let (symlinkp dirp
2659 res-inode res-filemodes res-numlinks
2660 res-uid res-gid res-size res-symlink-target)
2661 (tramp-message vec 5 "file attributes with ls: %s" localname)
2662 (tramp-send-command
2664 (format "(%s %s || %s -h %s) && %s %s %s"
2665 (tramp-get-file-exists-command vec)
2666 (tramp-shell-quote-argument localname)
2667 (tramp-get-test-command vec)
2668 (tramp-shell-quote-argument localname)
2669 (tramp-get-ls-command vec)
2670 (if (eq id-format 'integer) "-ildn" "-ild")
2671 (tramp-shell-quote-argument localname)))
2672 ;; parse `ls -l' output ...
2673 (with-current-buffer (tramp-get-buffer vec)
2674 (when (> (buffer-size) 0)
2675 (goto-char (point-min))
2676 ;; ... inode
2677 (setq res-inode
2678 (condition-case err
2679 (read (current-buffer))
2680 (invalid-read-syntax
2681 (when (and (equal (cadr err)
2682 "Integer constant overflow in reader")
2683 (string-match
2684 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2685 (car (cddr err))))
2686 (let* ((big (read (substring (car (cddr err)) 0
2687 (match-beginning 1))))
2688 (small (read (match-string 1 (car (cddr err)))))
2689 (twiddle (/ small 65536)))
2690 (cons (+ big twiddle)
2691 (- small (* twiddle 65536))))))))
2692 ;; ... file mode flags
2693 (setq res-filemodes (symbol-name (read (current-buffer))))
2694 ;; ... number links
2695 (setq res-numlinks (read (current-buffer)))
2696 ;; ... uid and gid
2697 (setq res-uid (read (current-buffer)))
2698 (setq res-gid (read (current-buffer)))
2699 (if (eq id-format 'integer)
2700 (progn
2701 (unless (numberp res-uid) (setq res-uid -1))
2702 (unless (numberp res-gid) (setq res-gid -1)))
2703 (progn
2704 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
2705 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
2706 ;; ... size
2707 (setq res-size (read (current-buffer)))
2708 ;; From the file modes, figure out other stuff.
2709 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2710 (setq dirp (eq ?d (aref res-filemodes 0)))
2711 ;; if symlink, find out file name pointed to
2712 (when symlinkp
2713 (search-forward "-> ")
2714 (setq res-symlink-target
2715 (buffer-substring (point) (tramp-compat-line-end-position))))
2716 ;; return data gathered
2717 (list
2718 ;; 0. t for directory, string (name linked to) for symbolic
2719 ;; link, or nil.
2720 (or dirp res-symlink-target)
2721 ;; 1. Number of links to file.
2722 res-numlinks
2723 ;; 2. File uid.
2724 res-uid
2725 ;; 3. File gid.
2726 res-gid
2727 ;; 4. Last access time, as a list of two integers. First
2728 ;; integer has high-order 16 bits of time, second has low 16
2729 ;; bits.
2730 ;; 5. Last modification time, likewise.
2731 ;; 6. Last status change time, likewise.
2732 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2733 ;; 7. Size in bytes (-1, if number is out of range).
2734 res-size
2735 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2736 res-filemodes
2737 ;; 9. t if file's gid would change if file were deleted and
2738 ;; recreated. Will be set in `tramp-convert-file-attributes'
2740 ;; 10. inode number.
2741 res-inode
2742 ;; 11. Device number. Will be replaced by a virtual device number.
2744 )))))
2746 (defun tramp-do-file-attributes-with-perl
2747 (vec localname &optional id-format)
2748 "Implement `file-attributes' for Tramp files using a Perl script."
2749 (tramp-message vec 5 "file attributes with perl: %s" localname)
2750 (tramp-maybe-send-script
2751 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
2752 (tramp-send-command-and-read
2754 (format "tramp_perl_file_attributes %s %s"
2755 (tramp-shell-quote-argument localname) id-format)))
2757 (defun tramp-do-file-attributes-with-stat
2758 (vec localname &optional id-format)
2759 "Implement `file-attributes' for Tramp files using stat(1) command."
2760 (tramp-message vec 5 "file attributes with stat: %s" localname)
2761 (tramp-send-command-and-read
2763 (format
2764 "((%s %s || %s -h %s) && %s -c '((\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)' %s || echo nil)"
2765 (tramp-get-file-exists-command vec)
2766 (tramp-shell-quote-argument localname)
2767 (tramp-get-test-command vec)
2768 (tramp-shell-quote-argument localname)
2769 (tramp-get-remote-stat vec)
2770 (if (eq id-format 'integer) "%u" "\"%U\"")
2771 (if (eq id-format 'integer) "%g" "\"%G\"")
2772 (tramp-shell-quote-argument localname))))
2774 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
2775 "Like `set-visited-file-modtime' for Tramp files."
2776 (unless (buffer-file-name)
2777 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2778 (buffer-name)))
2779 (if time-list
2780 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
2781 (let ((f (buffer-file-name))
2782 coding-system-used)
2783 (with-parsed-tramp-file-name f nil
2784 (let* ((attr (file-attributes f))
2785 ;; '(-1 65535) means file doesn't exists yet.
2786 (modtime (or (nth 5 attr) '(-1 65535))))
2787 (when (boundp 'last-coding-system-used)
2788 (setq coding-system-used (symbol-value 'last-coding-system-used)))
2789 ;; We use '(0 0) as a don't-know value. See also
2790 ;; `tramp-do-file-attributes-with-ls'.
2791 (if (not (equal modtime '(0 0)))
2792 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
2793 (progn
2794 (tramp-send-command
2796 (format "%s -ild %s"
2797 (tramp-get-ls-command v)
2798 (tramp-shell-quote-argument localname)))
2799 (setq attr (buffer-substring (point)
2800 (progn (end-of-line) (point)))))
2801 (tramp-set-file-property
2802 v localname "visited-file-modtime-ild" attr))
2803 (when (boundp 'last-coding-system-used)
2804 (set 'last-coding-system-used coding-system-used))
2805 nil)))))
2807 ;; This function makes the same assumption as
2808 ;; `tramp-handle-set-visited-file-modtime'.
2809 (defun tramp-handle-verify-visited-file-modtime (buf)
2810 "Like `verify-visited-file-modtime' for Tramp files.
2811 At the time `verify-visited-file-modtime' calls this function, we
2812 already know that the buffer is visiting a file and that
2813 `visited-file-modtime' does not return 0. Do not call this
2814 function directly, unless those two cases are already taken care
2815 of."
2816 (with-current-buffer buf
2817 ;; There is no file visiting the buffer, or the buffer has no
2818 ;; recorded last modification time.
2819 (if (or (not (buffer-file-name))
2820 (eq (visited-file-modtime) 0))
2822 (let ((f (buffer-file-name)))
2823 (with-parsed-tramp-file-name f nil
2824 (tramp-flush-file-property v localname)
2825 (let* ((attr (file-attributes f))
2826 (modtime (nth 5 attr))
2827 (mt (visited-file-modtime)))
2829 (cond
2830 ;; File exists, and has a known modtime.
2831 ((and attr (not (equal modtime '(0 0))))
2832 (< (abs (tramp-time-diff
2833 modtime
2834 ;; For compatibility, deal with both the old
2835 ;; (HIGH . LOW) and the new (HIGH LOW) return
2836 ;; values of `visited-file-modtime'.
2837 (if (atom (cdr mt))
2838 (list (car mt) (cdr mt))
2839 mt)))
2841 ;; Modtime has the don't know value.
2842 (attr
2843 (tramp-send-command
2845 (format "%s -ild %s"
2846 (tramp-get-ls-command v)
2847 (tramp-shell-quote-argument localname)))
2848 (with-current-buffer (tramp-get-buffer v)
2849 (setq attr (buffer-substring
2850 (point) (progn (end-of-line) (point)))))
2851 (equal
2852 attr
2853 (tramp-get-file-property
2854 v localname "visited-file-modtime-ild" "")))
2855 ;; If file does not exist, say it is not modified if and
2856 ;; only if that agrees with the buffer's record.
2857 (t (equal mt '(-1 65535))))))))))
2859 (defun tramp-handle-set-file-modes (filename mode)
2860 "Like `set-file-modes' for Tramp files."
2861 (with-parsed-tramp-file-name filename nil
2862 (tramp-flush-file-property v localname)
2863 (unless (zerop (tramp-send-command-and-check
2865 (format "chmod %s %s"
2866 (tramp-decimal-to-octal mode)
2867 (tramp-shell-quote-argument localname))))
2868 ;; FIXME: extract the proper text from chmod's stderr.
2869 (tramp-error
2870 v 'file-error "Error while changing file's mode %s" filename))))
2872 (defun tramp-handle-set-file-times (filename &optional time)
2873 "Like `set-file-times' for Tramp files."
2874 (zerop
2875 (if (file-remote-p filename)
2876 (with-parsed-tramp-file-name filename nil
2877 (tramp-flush-file-property v localname)
2878 (let ((time (if (or (null time) (equal time '(0 0)))
2879 (current-time)
2880 time))
2881 (utc
2882 ;; With GNU Emacs, `format-time-string' has an
2883 ;; optional parameter UNIVERSAL. This is preferred,
2884 ;; because we could handle the case when the remote
2885 ;; host is located in a different time zone as the
2886 ;; local host.
2887 (and (functionp 'subr-arity)
2888 (subrp (symbol-function 'format-time-string))
2889 (= 3 (cdr (funcall (symbol-function 'subr-arity)
2890 (symbol-function
2891 'format-time-string)))))))
2892 (tramp-send-command-and-check
2893 v (format "%s touch -t %s %s"
2894 (if utc "TZ=UTC; export TZ;" "")
2895 (if utc
2896 (format-time-string "%Y%m%d%H%M.%S" time t)
2897 (format-time-string "%Y%m%d%H%M.%S" time))
2898 (tramp-shell-quote-argument localname)))))
2900 ;; We handle also the local part, because in older Emacsen,
2901 ;; without `set-file-times', this function is an alias for this.
2902 ;; We are local, so we don't need the UTC settings.
2903 (tramp-local-call-process
2904 "touch" nil nil nil "-t"
2905 (format-time-string "%Y%m%d%H%M.%S" time)
2906 (tramp-shell-quote-argument filename)))))
2908 (defun tramp-set-file-uid-gid (filename &optional uid gid)
2909 "Set the ownership for FILENAME.
2910 If UID and GID are provided, these values are used; otherwise uid
2911 and gid of the corresponding user is taken. Both parameters must be integers."
2912 ;; Modern Unices allow chown only for root. So we might need
2913 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
2914 ;; working with su(do)? when it is needed, so it shall succeed in
2915 ;; the majority of cases.
2916 ;; Don't modify `last-coding-system-used' by accident.
2917 (let ((last-coding-system-used last-coding-system-used))
2918 (if (file-remote-p filename)
2919 (with-parsed-tramp-file-name filename nil
2920 (if (and (zerop (user-uid)) (tramp-local-host-p v))
2921 ;; If we are root on the local host, we can do it directly.
2922 (tramp-set-file-uid-gid localname uid gid)
2923 (let ((uid (or (and (integerp uid) uid)
2924 (tramp-get-remote-uid v 'integer)))
2925 (gid (or (and (integerp gid) gid)
2926 (tramp-get-remote-gid v 'integer))))
2927 (tramp-send-command
2928 v (format
2929 "chown %d:%d %s" uid gid
2930 (tramp-shell-quote-argument localname))))))
2932 ;; We handle also the local part, because there doesn't exist
2933 ;; `set-file-uid-gid'. On W32 "chown" might not work.
2934 (let ((uid (or (and (integerp uid) uid) (tramp-get-local-uid 'integer)))
2935 (gid (or (and (integerp gid) gid) (tramp-get-local-gid 'integer))))
2936 (tramp-local-call-process
2937 "chown" nil nil nil
2938 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
2940 ;; Simple functions using the `test' command.
2942 (defun tramp-handle-file-executable-p (filename)
2943 "Like `file-executable-p' for Tramp files."
2944 (with-parsed-tramp-file-name filename nil
2945 (with-file-property v localname "file-executable-p"
2946 ;; Examine `file-attributes' cache to see if request can be
2947 ;; satisfied without remote operation.
2948 (or (tramp-check-cached-permissions v ?x)
2949 (zerop (tramp-run-test "-x" filename))))))
2951 (defun tramp-handle-file-readable-p (filename)
2952 "Like `file-readable-p' for Tramp files."
2953 (with-parsed-tramp-file-name filename nil
2954 (with-file-property v localname "file-readable-p"
2955 ;; Examine `file-attributes' cache to see if request can be
2956 ;; satisfied without remote operation.
2957 (or (tramp-check-cached-permissions v ?r)
2958 (zerop (tramp-run-test "-r" filename))))))
2960 ;; When the remote shell is started, it looks for a shell which groks
2961 ;; tilde expansion. Here, we assume that all shells which grok tilde
2962 ;; expansion will also provide a `test' command which groks `-nt' (for
2963 ;; newer than). If this breaks, tell me about it and I'll try to do
2964 ;; something smarter about it.
2965 (defun tramp-handle-file-newer-than-file-p (file1 file2)
2966 "Like `file-newer-than-file-p' for Tramp files."
2967 (cond ((not (file-exists-p file1))
2968 nil)
2969 ((not (file-exists-p file2))
2971 ;; We are sure both files exist at this point.
2973 (save-excursion
2974 ;; We try to get the mtime of both files. If they are not
2975 ;; equal to the "dont-know" value, then we subtract the times
2976 ;; and obtain the result.
2977 (let ((fa1 (file-attributes file1))
2978 (fa2 (file-attributes file2)))
2979 (if (and (not (equal (nth 5 fa1) '(0 0)))
2980 (not (equal (nth 5 fa2) '(0 0))))
2981 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
2982 ;; If one of them is the dont-know value, then we can
2983 ;; still try to run a shell command on the remote host.
2984 ;; However, this only works if both files are Tramp
2985 ;; files and both have the same method, same user, same
2986 ;; host.
2987 (unless (tramp-equal-remote file1 file2)
2988 (with-parsed-tramp-file-name
2989 (if (tramp-tramp-file-p file1) file1 file2) nil
2990 (tramp-error
2991 v 'file-error
2992 "Files %s and %s must have same method, user, host"
2993 file1 file2)))
2994 (with-parsed-tramp-file-name file1 nil
2995 (zerop (tramp-run-test2
2996 (tramp-get-test-nt-command v) file1 file2)))))))))
2998 ;; Functions implemented using the basic functions above.
3000 (defun tramp-handle-file-modes (filename)
3001 "Like `file-modes' for Tramp files."
3002 (let ((truename (or (file-truename filename) filename)))
3003 (when (file-exists-p truename)
3004 (tramp-mode-string-to-int (nth 8 (file-attributes truename))))))
3006 (defun tramp-default-file-modes (filename)
3007 "Return file modes of FILENAME as integer.
3008 If the file modes of FILENAME cannot be determined, return the
3009 value of `default-file-modes', without execute permissions."
3010 (or (file-modes filename)
3011 (logand (default-file-modes) (tramp-octal-to-decimal "0666"))))
3013 (defun tramp-handle-file-directory-p (filename)
3014 "Like `file-directory-p' for Tramp files."
3015 ;; Care must be taken that this function returns `t' for symlinks
3016 ;; pointing to directories. Surely the most obvious implementation
3017 ;; would be `test -d', but that returns false for such symlinks.
3018 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
3019 ;; I now think he's right. So we could be using `test -d', couldn't
3020 ;; we?
3022 ;; Alternatives: `cd %s', `test -d %s'
3023 (with-parsed-tramp-file-name filename nil
3024 (with-file-property v localname "file-directory-p"
3025 (zerop (tramp-run-test "-d" filename)))))
3027 (defun tramp-handle-file-regular-p (filename)
3028 "Like `file-regular-p' for Tramp files."
3029 (and (file-exists-p filename)
3030 (eq ?- (aref (nth 8 (file-attributes filename)) 0))))
3032 (defun tramp-handle-file-symlink-p (filename)
3033 "Like `file-symlink-p' for Tramp files."
3034 (with-parsed-tramp-file-name filename nil
3035 (let ((x (car (file-attributes filename))))
3036 (when (stringp x)
3037 ;; When Tramp is running on VMS, then `file-name-absolute-p'
3038 ;; might do weird things.
3039 (if (file-name-absolute-p x)
3040 (tramp-make-tramp-file-name method user host x)
3041 x)))))
3043 (defun tramp-handle-file-writable-p (filename)
3044 "Like `file-writable-p' for Tramp files."
3045 (with-parsed-tramp-file-name filename nil
3046 (with-file-property v localname "file-writable-p"
3047 (if (file-exists-p filename)
3048 ;; Examine `file-attributes' cache to see if request can be
3049 ;; satisfied without remote operation.
3050 (or (tramp-check-cached-permissions v ?w)
3051 (zerop (tramp-run-test "-w" filename)))
3052 ;; If file doesn't exist, check if directory is writable.
3053 (and (zerop (tramp-run-test
3054 "-d" (file-name-directory filename)))
3055 (zerop (tramp-run-test
3056 "-w" (file-name-directory filename))))))))
3058 (defun tramp-handle-file-ownership-preserved-p (filename)
3059 "Like `file-ownership-preserved-p' for Tramp files."
3060 (with-parsed-tramp-file-name filename nil
3061 (with-file-property v localname "file-ownership-preserved-p"
3062 (let ((attributes (file-attributes filename)))
3063 ;; Return t if the file doesn't exist, since it's true that no
3064 ;; information would be lost by an (attempted) delete and create.
3065 (or (null attributes)
3066 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer)))))))
3068 ;; Other file name ops.
3070 (defun tramp-handle-directory-file-name (directory)
3071 "Like `directory-file-name' for Tramp files."
3072 ;; If localname component of filename is "/", leave it unchanged.
3073 ;; Otherwise, remove any trailing slash from localname component.
3074 ;; Method, host, etc, are unchanged. Does it make sense to try
3075 ;; to avoid parsing the filename?
3076 (with-parsed-tramp-file-name directory nil
3077 (if (and (not (zerop (length localname)))
3078 (eq (aref localname (1- (length localname))) ?/)
3079 (not (string= localname "/")))
3080 (substring directory 0 -1)
3081 directory)))
3083 ;; Directory listings.
3085 (defun tramp-handle-directory-files
3086 (directory &optional full match nosort files-only)
3087 "Like `directory-files' for Tramp files."
3088 ;; FILES-ONLY is valid for XEmacs only.
3089 (when (file-directory-p directory)
3090 (setq directory (expand-file-name directory))
3091 (let ((temp (nreverse (file-name-all-completions "" directory)))
3092 result item)
3094 (while temp
3095 (setq item (directory-file-name (pop temp)))
3096 (when (and (or (null match) (string-match match item))
3097 (or (null files-only)
3098 ;; files only
3099 (and (equal files-only t) (file-regular-p item))
3100 ;; directories only
3101 (file-directory-p item)))
3102 (push (if full (expand-file-name item directory) item)
3103 result)))
3104 result)))
3106 (defun tramp-handle-directory-files-and-attributes
3107 (directory &optional full match nosort id-format)
3108 "Like `directory-files-and-attributes' for Tramp files."
3109 (unless id-format (setq id-format 'integer))
3110 (when (file-directory-p directory)
3111 (setq directory (expand-file-name directory))
3112 (let* ((temp
3113 (tramp-compat-copy-tree
3114 (with-parsed-tramp-file-name directory nil
3115 (with-file-property
3116 v localname
3117 (format "directory-files-and-attributes-%s" id-format)
3118 (save-excursion
3119 (mapcar
3120 (lambda (x)
3121 (cons (car x)
3122 (tramp-convert-file-attributes v (cdr x))))
3123 (cond
3124 ((tramp-get-remote-stat v)
3125 (tramp-do-directory-files-and-attributes-with-stat
3126 v localname id-format))
3127 ((tramp-get-remote-perl v)
3128 (tramp-do-directory-files-and-attributes-with-perl
3129 v localname id-format)))))))))
3130 result item)
3132 (while temp
3133 (setq item (pop temp))
3134 (when (or (null match) (string-match match (car item)))
3135 (when full
3136 (setcar item (expand-file-name (car item) directory)))
3137 (push item result)))
3139 (if nosort
3140 result
3141 (sort result (lambda (x y) (string< (car x) (car y))))))))
3143 (defun tramp-do-directory-files-and-attributes-with-perl
3144 (vec localname &optional id-format)
3145 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
3146 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
3147 (tramp-maybe-send-script
3148 vec tramp-perl-directory-files-and-attributes
3149 "tramp_perl_directory_files_and_attributes")
3150 (let ((object
3151 (tramp-send-command-and-read
3153 (format "tramp_perl_directory_files_and_attributes %s %s"
3154 (tramp-shell-quote-argument localname) id-format))))
3155 (when (stringp object) (tramp-error vec 'file-error object))
3156 object))
3158 (defun tramp-do-directory-files-and-attributes-with-stat
3159 (vec localname &optional id-format)
3160 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
3161 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
3162 (tramp-send-command-and-read
3164 (format
3165 (concat
3166 ;; We must care about filenames with spaces, or starting with
3167 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
3168 ;; but it does not work on all remote systems. Therefore, we
3169 ;; quote the filenames via sed.
3170 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | xargs "
3171 "%s -c '(\"%%n\" (\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)'); "
3172 "echo \")\"")
3173 (tramp-shell-quote-argument localname)
3174 (tramp-get-ls-command vec)
3175 (tramp-get-remote-stat vec)
3176 (if (eq id-format 'integer) "%u" "\"%U\"")
3177 (if (eq id-format 'integer) "%g" "\"%G\""))))
3179 ;; This function should return "foo/" for directories and "bar" for
3180 ;; files.
3181 (defun tramp-handle-file-name-all-completions (filename directory)
3182 "Like `file-name-all-completions' for Tramp files."
3183 (unless (save-match-data (string-match "/" filename))
3184 (with-parsed-tramp-file-name (expand-file-name directory) nil
3186 (all-completions
3187 filename
3188 (mapcar
3189 'list
3191 ;; Try cache first
3192 (and
3193 ;; Ignore if expired
3194 (or (not (integerp tramp-completion-reread-directory-timeout))
3195 (<= (tramp-time-diff
3196 (current-time)
3197 (tramp-get-file-property
3198 v localname "last-completion" '(0 0 0)))
3199 tramp-completion-reread-directory-timeout))
3201 ;; Try cache entries for filename, filename with last
3202 ;; character removed, filename with last two characters
3203 ;; removed, ..., and finally the empty string - all
3204 ;; concatenated to the local directory name
3206 ;; This is inefficient for very long filenames, pity
3207 ;; `reduce' is not available...
3208 (car
3209 (apply
3210 'append
3211 (mapcar
3212 (lambda (x)
3213 (let ((cache-hit
3214 (tramp-get-file-property
3216 (concat localname (substring filename 0 x))
3217 "file-name-all-completions"
3218 nil)))
3219 (when cache-hit (list cache-hit))))
3220 (tramp-compat-number-sequence (length filename) 0 -1)))))
3222 ;; Cache expired or no matching cache entry found so we need
3223 ;; to perform a remote operation
3224 (let (result)
3225 ;; Get a list of directories and files, including reliably
3226 ;; tagging the directories with a trailing '/'. Because I
3227 ;; rock. --daniel@danann.net
3229 ;; Changed to perform `cd' in the same remote op and only
3230 ;; get entries starting with `filename'. Capture any `cd'
3231 ;; error messages. Ensure any `cd' and `echo' aliases are
3232 ;; ignored.
3233 (tramp-send-command
3235 (if (tramp-get-remote-perl v)
3236 (progn
3237 (tramp-maybe-send-script
3238 v tramp-perl-file-name-all-completions
3239 "tramp_perl_file_name_all_completions")
3240 (format "tramp_perl_file_name_all_completions %s %s %d"
3241 (tramp-shell-quote-argument localname)
3242 (tramp-shell-quote-argument filename)
3243 (if (symbol-value
3244 'read-file-name-completion-ignore-case)
3245 1 0)))
3247 (format (concat
3248 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
3249 ;; `ls' with wildcard might fail with `Argument
3250 ;; list too long' error in some corner cases; if
3251 ;; `ls' fails after `cd' succeeded, chances are
3252 ;; that's the case, so let's retry without
3253 ;; wildcard. This will return "too many" entries
3254 ;; but that isn't harmful.
3255 " || %s -a 2>/dev/null)"
3256 " | while read f; do"
3257 " if %s -d \"$f\" 2>/dev/null;"
3258 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
3259 " && \\echo ok) || \\echo fail")
3260 (tramp-shell-quote-argument localname)
3261 (tramp-get-ls-command v)
3262 ;; When `filename' is empty, just `ls' without
3263 ;; filename argument is more efficient than `ls *'
3264 ;; for very large directories and might avoid the
3265 ;; `Argument list too long' error.
3267 ;; With and only with wildcard, we need to add
3268 ;; `-d' to prevent `ls' from descending into
3269 ;; sub-directories.
3270 (if (zerop (length filename))
3272 (concat (tramp-shell-quote-argument filename) "* -d"))
3273 (tramp-get-ls-command v)
3274 (tramp-get-test-command v))))
3276 ;; Now grab the output.
3277 (with-current-buffer (tramp-get-buffer v)
3278 (goto-char (point-max))
3280 ;; Check result code, found in last line of output
3281 (forward-line -1)
3282 (if (looking-at "^fail$")
3283 (progn
3284 ;; Grab error message from line before last line
3285 ;; (it was put there by `cd 2>&1')
3286 (forward-line -1)
3287 (tramp-error
3288 v 'file-error
3289 "tramp-handle-file-name-all-completions: %s"
3290 (buffer-substring
3291 (point) (tramp-compat-line-end-position))))
3292 ;; For peace of mind, if buffer doesn't end in `fail'
3293 ;; then it should end in `ok'. If neither are in the
3294 ;; buffer something went seriously wrong on the remote
3295 ;; side.
3296 (unless (looking-at "^ok$")
3297 (tramp-error
3298 v 'file-error
3300 tramp-handle-file-name-all-completions: internal error accessing `%s': `%s'"
3301 (tramp-shell-quote-argument localname) (buffer-string))))
3303 (while (zerop (forward-line -1))
3304 (push (buffer-substring
3305 (point) (tramp-compat-line-end-position))
3306 result)))
3308 ;; Because the remote op went through OK we know the
3309 ;; directory we `cd'-ed to exists
3310 (tramp-set-file-property
3311 v localname "file-exists-p" t)
3313 ;; Because the remote op went through OK we know every
3314 ;; file listed by `ls' exists.
3315 (mapc (lambda (entry)
3316 (tramp-set-file-property
3317 v (concat localname entry) "file-exists-p" t))
3318 result)
3320 (tramp-set-file-property
3321 v localname "last-completion" (current-time))
3323 ;; Store result in the cache
3324 (tramp-set-file-property
3325 v (concat localname filename)
3326 "file-name-all-completions"
3327 result))))))))
3329 ;; The following isn't needed for Emacs 20 but for 19.34?
3330 (defun tramp-handle-file-name-completion
3331 (filename directory &optional predicate)
3332 "Like `file-name-completion' for Tramp files."
3333 (unless (tramp-tramp-file-p directory)
3334 (error
3335 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
3336 directory))
3337 (try-completion
3338 filename
3339 (mapcar 'list (file-name-all-completions filename directory))
3340 (when predicate
3341 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
3343 ;; cp, mv and ln
3345 (defun tramp-handle-add-name-to-file
3346 (filename newname &optional ok-if-already-exists)
3347 "Like `add-name-to-file' for Tramp files."
3348 (unless (tramp-equal-remote filename newname)
3349 (with-parsed-tramp-file-name
3350 (if (tramp-tramp-file-p filename) filename newname) nil
3351 (tramp-error
3352 v 'file-error
3353 "add-name-to-file: %s"
3354 "only implemented for same method, same user, same host")))
3355 (with-parsed-tramp-file-name filename v1
3356 (with-parsed-tramp-file-name newname v2
3357 (let ((ln (when v1 (tramp-get-remote-ln v1))))
3358 (when (and (not ok-if-already-exists)
3359 (file-exists-p newname)
3360 (not (numberp ok-if-already-exists))
3361 (y-or-n-p
3362 (format
3363 "File %s already exists; make it a new name anyway? "
3364 newname)))
3365 (tramp-error
3366 v2 'file-error
3367 "add-name-to-file: file %s already exists" newname))
3368 (tramp-flush-file-property v2 v2-localname)
3369 (tramp-barf-unless-okay
3371 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
3372 (tramp-shell-quote-argument v2-localname))
3373 "error with add-name-to-file, see buffer `%s' for details"
3374 (buffer-name))))))
3376 (defun tramp-handle-copy-file
3377 (filename newname &optional ok-if-already-exists keep-date preserve-uid-gid)
3378 "Like `copy-file' for Tramp files."
3379 ;; Check if both files are local -- invoke normal copy-file.
3380 ;; Otherwise, use Tramp from local system.
3381 (setq filename (expand-file-name filename))
3382 (setq newname (expand-file-name newname))
3383 (cond
3384 ;; At least one file a Tramp file?
3385 ((or (tramp-tramp-file-p filename)
3386 (tramp-tramp-file-p newname))
3387 (tramp-do-copy-or-rename-file
3388 'copy filename newname ok-if-already-exists keep-date preserve-uid-gid))
3389 ;; Compat section.
3390 (preserve-uid-gid
3391 (tramp-run-real-handler
3392 'copy-file
3393 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
3395 (tramp-run-real-handler
3396 'copy-file (list filename newname ok-if-already-exists keep-date)))))
3398 (defun tramp-handle-copy-directory (dirname newname &optional keep-date parents)
3399 "Like `copy-directory' for Tramp files."
3400 (let ((t1 (tramp-tramp-file-p dirname))
3401 (t2 (tramp-tramp-file-p newname)))
3402 (with-parsed-tramp-file-name (if t1 dirname newname) nil
3403 (if (and (tramp-get-method-parameter method 'tramp-copy-recursive)
3404 ;; When DIRNAME and NEWNAME are remote, they must have
3405 ;; the same method.
3406 (or (null t1) (null t2)
3407 (string-equal (file-remote-p dirname 'method)
3408 (file-remote-p newname 'method))))
3409 ;; scp or rsync DTRT.
3410 (progn
3411 (setq dirname (directory-file-name (expand-file-name dirname))
3412 newname (directory-file-name (expand-file-name newname)))
3413 (if (and (file-directory-p newname)
3414 (not (string-equal (file-name-nondirectory dirname)
3415 (file-name-nondirectory newname))))
3416 (setq newname
3417 (expand-file-name
3418 (file-name-nondirectory dirname) newname)))
3419 (if (not (file-directory-p (file-name-directory newname)))
3420 (make-directory (file-name-directory newname) parents))
3421 (tramp-do-copy-or-rename-file-out-of-band
3422 'copy dirname newname keep-date))
3423 ;; We must do it file-wise.
3424 (tramp-run-real-handler
3425 'copy-directory (list dirname newname keep-date parents))))))
3427 (defun tramp-handle-rename-file
3428 (filename newname &optional ok-if-already-exists)
3429 "Like `rename-file' for Tramp files."
3430 ;; Check if both files are local -- invoke normal rename-file.
3431 ;; Otherwise, use Tramp from local system.
3432 (setq filename (expand-file-name filename))
3433 (setq newname (expand-file-name newname))
3434 ;; At least one file a Tramp file?
3435 (if (or (tramp-tramp-file-p filename)
3436 (tramp-tramp-file-p newname))
3437 (tramp-do-copy-or-rename-file
3438 'rename filename newname ok-if-already-exists t t)
3439 (tramp-run-real-handler
3440 'rename-file (list filename newname ok-if-already-exists))))
3442 (defun tramp-do-copy-or-rename-file
3443 (op filename newname &optional ok-if-already-exists keep-date preserve-uid-gid)
3444 "Copy or rename a remote file.
3445 OP must be `copy' or `rename' and indicates the operation to perform.
3446 FILENAME specifies the file to copy or rename, NEWNAME is the name of
3447 the new file (for copy) or the new name of the file (for rename).
3448 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
3449 KEEP-DATE means to make sure that NEWNAME has the same timestamp
3450 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3451 the uid and gid if both files are on the same host.
3453 This function is invoked by `tramp-handle-copy-file' and
3454 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
3455 and `rename'. FILENAME and NEWNAME must be absolute file names."
3456 (unless (memq op '(copy rename))
3457 (error "Unknown operation `%s', must be `copy' or `rename'" op))
3458 (let ((t1 (tramp-tramp-file-p filename))
3459 (t2 (tramp-tramp-file-p newname)))
3461 (when (and (not ok-if-already-exists) (file-exists-p newname))
3462 (with-parsed-tramp-file-name (if t1 filename newname) nil
3463 (tramp-error
3464 v 'file-already-exists "File %s already exists" newname)))
3466 (with-parsed-tramp-file-name (if t1 filename newname) nil
3467 (tramp-message v 0 "Transferring %s to %s..." filename newname))
3469 (prog1
3470 (cond
3471 ;; Both are Tramp files.
3472 ((and t1 t2)
3473 (with-parsed-tramp-file-name filename v1
3474 (with-parsed-tramp-file-name newname v2
3475 (cond
3476 ;; Shortcut: if method, host, user are the same for both
3477 ;; files, we invoke `cp' or `mv' on the remote host
3478 ;; directly.
3479 ((tramp-equal-remote filename newname)
3480 (tramp-do-copy-or-rename-file-directly
3481 op filename newname
3482 ok-if-already-exists keep-date preserve-uid-gid))
3484 ;; Try out-of-band operation.
3485 ((tramp-method-out-of-band-p
3486 v1 (nth 7 (file-attributes filename)))
3487 (tramp-do-copy-or-rename-file-out-of-band
3488 op filename newname keep-date))
3490 ;; No shortcut was possible. So we copy the
3491 ;; file first. If the operation was `rename', we go
3492 ;; back and delete the original file (if the copy was
3493 ;; successful). The approach is simple-minded: we
3494 ;; create a new buffer, insert the contents of the
3495 ;; source file into it, then write out the buffer to
3496 ;; the target file. The advantage is that it doesn't
3497 ;; matter which filename handlers are used for the
3498 ;; source and target file.
3500 (tramp-do-copy-or-rename-file-via-buffer
3501 op filename newname keep-date))))))
3503 ;; One file is a Tramp file, the other one is local.
3504 ((or t1 t2)
3505 (with-parsed-tramp-file-name (if t1 filename newname) nil
3506 (cond
3507 ;; Fast track on local machine.
3508 ((tramp-local-host-p v)
3509 (tramp-do-copy-or-rename-file-directly
3510 op filename newname
3511 ok-if-already-exists keep-date preserve-uid-gid))
3513 ;; If the Tramp file has an out-of-band method, the corresponding
3514 ;; copy-program can be invoked.
3515 ((tramp-method-out-of-band-p v (nth 7 (file-attributes filename)))
3516 (tramp-do-copy-or-rename-file-out-of-band
3517 op filename newname keep-date))
3519 ;; Use the inline method via a Tramp buffer.
3520 (t (tramp-do-copy-or-rename-file-via-buffer
3521 op filename newname keep-date)))))
3524 ;; One of them must be a Tramp file.
3525 (error "Tramp implementation says this cannot happen")))
3527 ;; In case of `rename', we must flush the cache of the source file.
3528 (when (and t1 (eq op 'rename))
3529 (with-parsed-tramp-file-name filename nil
3530 (tramp-flush-file-property v localname)))
3532 ;; When newname did exist, we have wrong cached values.
3533 (when t2
3534 (with-parsed-tramp-file-name newname nil
3535 (tramp-flush-file-property v localname)))
3537 (with-parsed-tramp-file-name (if t1 filename newname) nil
3538 (tramp-message v 0 "Transferring %s to %s...done" filename newname)))))
3540 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
3541 "Use an Emacs buffer to copy or rename a file.
3542 First arg OP is either `copy' or `rename' and indicates the operation.
3543 FILENAME is the source file, NEWNAME the target file.
3544 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
3545 (with-temp-buffer
3546 ;; We must disable multibyte, because binary data shall not be
3547 ;; converted.
3548 (set-buffer-multibyte nil)
3549 (let ((coding-system-for-read 'binary)
3550 (jka-compr-inhibit t))
3551 (insert-file-contents-literally filename))
3552 ;; We don't want the target file to be compressed, so we let-bind
3553 ;; `jka-compr-inhibit' to t.
3554 (let ((coding-system-for-write 'binary)
3555 (jka-compr-inhibit t))
3556 (write-region (point-min) (point-max) newname)))
3557 ;; KEEP-DATE handling.
3558 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
3559 ;; Set the mode.
3560 (set-file-modes newname (tramp-default-file-modes filename))
3561 ;; If the operation was `rename', delete the original file.
3562 (unless (eq op 'copy) (delete-file filename)))
3564 (defun tramp-do-copy-or-rename-file-directly
3565 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
3566 "Invokes `cp' or `mv' on the remote system.
3567 OP must be one of `copy' or `rename', indicating `cp' or `mv',
3568 respectively. FILENAME specifies the file to copy or rename,
3569 NEWNAME is the name of the new file (for copy) or the new name of
3570 the file (for rename). Both files must reside on the same host.
3571 KEEP-DATE means to make sure that NEWNAME has the same timestamp
3572 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3573 the uid and gid from FILENAME."
3574 (let ((t1 (tramp-tramp-file-p filename))
3575 (t2 (tramp-tramp-file-p newname))
3576 (file-times (nth 5 (file-attributes filename)))
3577 (file-modes (tramp-default-file-modes filename)))
3578 (with-parsed-tramp-file-name (if t1 filename newname) nil
3579 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
3580 ((eq op 'copy) "cp -f")
3581 ((eq op 'rename) "mv -f")
3582 (t (tramp-error
3583 v 'file-error
3584 "Unknown operation `%s', must be `copy' or `rename'"
3585 op))))
3586 (localname1
3587 (if t1 (tramp-handle-file-remote-p filename 'localname) filename))
3588 (localname2
3589 (if t2 (tramp-handle-file-remote-p newname 'localname) newname))
3590 (prefix (file-remote-p (if t1 filename newname)))
3591 cmd-result)
3593 (cond
3594 ;; Both files are on a remote host, with same user.
3595 ((and t1 t2)
3596 (setq cmd-result
3597 (tramp-send-command-and-check
3599 (format "%s %s %s" cmd
3600 (tramp-shell-quote-argument localname1)
3601 (tramp-shell-quote-argument localname2))))
3602 (with-current-buffer (tramp-get-buffer v)
3603 (goto-char (point-min))
3604 (unless
3606 (and keep-date
3607 ;; Mask cp -f error.
3608 (re-search-forward
3609 tramp-operation-not-permitted-regexp nil t))
3610 (zerop cmd-result))
3611 (tramp-error-with-buffer
3612 nil v 'file-error
3613 "Copying directly failed, see buffer `%s' for details."
3614 (buffer-name)))))
3616 ;; We are on the local host.
3617 ((or t1 t2)
3618 (cond
3619 ;; We can do it directly.
3620 ((let (file-name-handler-alist)
3621 (and (file-readable-p localname1)
3622 (file-writable-p (file-name-directory localname2))
3623 (or (file-directory-p localname2)
3624 (file-writable-p localname2))))
3625 (if (eq op 'copy)
3626 (tramp-compat-copy-file
3627 localname1 localname2 ok-if-already-exists
3628 keep-date preserve-uid-gid)
3629 (tramp-run-real-handler
3630 'rename-file (list localname1 localname2 ok-if-already-exists))))
3632 ;; We can do it directly with `tramp-send-command'
3633 ((and (file-readable-p (concat prefix localname1))
3634 (file-writable-p
3635 (file-name-directory (concat prefix localname2)))
3636 (or (file-directory-p (concat prefix localname2))
3637 (file-writable-p (concat prefix localname2))))
3638 (tramp-do-copy-or-rename-file-directly
3639 op (concat prefix localname1) (concat prefix localname2)
3640 ok-if-already-exists keep-date t)
3641 ;; We must change the ownership to the local user.
3642 (tramp-set-file-uid-gid
3643 (concat prefix localname2)
3644 (tramp-get-local-uid 'integer)
3645 (tramp-get-local-gid 'integer)))
3647 ;; We need a temporary file in between.
3649 ;; Create the temporary file.
3650 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
3651 (unwind-protect
3652 (progn
3653 (cond
3656 (zerop
3657 (tramp-send-command-and-check
3658 v (format
3659 "%s %s %s" cmd
3660 (tramp-shell-quote-argument localname1)
3661 (tramp-shell-quote-argument tmpfile))))
3662 (tramp-error-with-buffer
3663 nil v 'file-error
3664 "Copying directly failed, see buffer `%s' for details."
3665 (tramp-get-buffer v)))
3666 ;; We must change the ownership as remote user.
3667 ;; Since this does not work reliable, we also
3668 ;; give read permissions.
3669 (set-file-modes
3670 (concat prefix tmpfile) (tramp-octal-to-decimal "0777"))
3671 (tramp-set-file-uid-gid
3672 (concat prefix tmpfile)
3673 (tramp-get-local-uid 'integer)
3674 (tramp-get-local-gid 'integer)))
3676 (if (eq op 'copy)
3677 (tramp-compat-copy-file
3678 localname1 tmpfile t
3679 keep-date preserve-uid-gid)
3680 (tramp-run-real-handler
3681 'rename-file
3682 (list localname1 tmpfile t)))
3683 ;; We must change the ownership as local user.
3684 ;; Since this does not work reliable, we also
3685 ;; give read permissions.
3686 (set-file-modes tmpfile (tramp-octal-to-decimal "0777"))
3687 (tramp-set-file-uid-gid
3688 tmpfile
3689 (tramp-get-remote-uid v 'integer)
3690 (tramp-get-remote-gid v 'integer))))
3692 ;; Move the temporary file to its destination.
3693 (cond
3696 (zerop
3697 (tramp-send-command-and-check
3698 v (format
3699 "cp -f -p %s %s"
3700 (tramp-shell-quote-argument tmpfile)
3701 (tramp-shell-quote-argument localname2))))
3702 (tramp-error-with-buffer
3703 nil v 'file-error
3704 "Copying directly failed, see buffer `%s' for details."
3705 (tramp-get-buffer v))))
3707 (tramp-run-real-handler
3708 'rename-file
3709 (list tmpfile localname2 ok-if-already-exists)))))
3711 ;; Save exit.
3712 (condition-case nil
3713 (delete-file tmpfile)
3714 (error)))))))))
3716 ;; Set the time and mode. Mask possible errors.
3717 (condition-case nil
3718 (when keep-date
3719 (set-file-times newname file-times)
3720 (set-file-modes newname file-modes))
3721 (error)))))
3723 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
3724 "Invoke rcp program to copy.
3725 The method used must be an out-of-band method."
3726 (let ((t1 (tramp-tramp-file-p filename))
3727 (t2 (tramp-tramp-file-p newname))
3728 copy-program copy-args copy-env copy-keep-date port spec
3729 source target)
3731 (with-parsed-tramp-file-name (if t1 filename newname) nil
3732 (if (and t1 t2)
3734 ;; Both are Tramp files. We shall optimize it, when the
3735 ;; methods for filename and newname are the same.
3736 (let ((tmpfile (tramp-compat-make-temp-file localname)))
3737 (unwind-protect
3738 (progn
3739 (tramp-do-copy-or-rename-file-out-of-band
3740 op filename tmpfile keep-date)
3741 (tramp-do-copy-or-rename-file-out-of-band
3742 'rename tmpfile newname keep-date))
3743 ;; Save exit.
3744 (condition-case nil
3745 (delete-file tmpfile)
3746 (error))))
3748 ;; Expand hops. Might be necessary for gateway methods.
3749 (setq v (car (tramp-compute-multi-hops v)))
3750 (aset v 3 localname)
3752 ;; Check which ones of source and target are Tramp files.
3753 (setq source (if t1 (tramp-make-copy-program-file-name v) filename)
3754 target (funcall
3755 (if (and (file-directory-p filename)
3756 (string-equal
3757 (file-name-nondirectory filename)
3758 (file-name-nondirectory newname)))
3759 'file-name-directory
3760 'identity)
3761 (if t2 (tramp-make-copy-program-file-name v) newname)))
3763 ;; Check for port number. Until now, there's no need for handling
3764 ;; like method, user, host.
3765 (setq host (tramp-file-name-real-host v)
3766 port (tramp-file-name-port v)
3767 port (or (and port (number-to-string port)) ""))
3769 ;; Compose copy command.
3770 (setq spec `((?h . ,host) (?u . ,user) (?p . ,port)
3771 (?t . ,(tramp-get-connection-property
3772 (tramp-get-connection-process v) "temp-file" ""))
3773 (?k . ,(if keep-date " " "")))
3774 copy-program (tramp-get-method-parameter
3775 method 'tramp-copy-program)
3776 copy-keep-date (tramp-get-method-parameter
3777 method 'tramp-copy-keep-date)
3778 copy-args
3779 (delq
3781 (mapcar
3782 (lambda (x)
3783 (setq
3785 ;; " " is indication for keep-date argument.
3786 (delete " " (mapcar (lambda (y) (format-spec y spec)) x)))
3787 (unless (member "" x) (mapconcat 'identity x " ")))
3788 (tramp-get-method-parameter method 'tramp-copy-args)))
3789 copy-env
3790 (delq
3792 (mapcar
3793 (lambda (x)
3794 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
3795 (unless (member "" x) (mapconcat 'identity x " ")))
3796 (tramp-get-method-parameter method 'tramp-copy-env))))
3798 ;; Check for program.
3799 (when (and (fboundp 'executable-find)
3800 (not (let ((default-directory
3801 (tramp-compat-temporary-file-directory)))
3802 (executable-find copy-program))))
3803 (tramp-error
3804 v 'file-error "Cannot find copy program: %s" copy-program))
3806 (unwind-protect
3807 (with-temp-buffer
3808 ;; The default directory must be remote.
3809 (let ((default-directory
3810 (file-name-directory (if t1 filename newname)))
3811 (process-environment (copy-sequence process-environment)))
3812 ;; Set the transfer process properties.
3813 (tramp-set-connection-property
3814 v "process-name" (buffer-name (current-buffer)))
3815 (tramp-set-connection-property
3816 v "process-buffer" (current-buffer))
3817 (while copy-env
3818 (tramp-message v 5 "%s=\"%s\"" (car copy-env) (cadr copy-env))
3819 (setenv (pop copy-env) (pop copy-env)))
3821 ;; Use an asynchronous process. By this, password can
3822 ;; be handled. The default directory must be local, in
3823 ;; order to apply the correct `copy-program'. We don't
3824 ;; set a timeout, because the copying of large files can
3825 ;; last longer than 60 secs.
3826 (let ((p (let ((default-directory
3827 (tramp-compat-temporary-file-directory)))
3828 (apply 'start-process
3829 (tramp-get-connection-property
3830 v "process-name" nil)
3831 (tramp-get-connection-property
3832 v "process-buffer" nil)
3833 copy-program
3834 (append copy-args (list source target))))))
3835 (tramp-message
3836 v 6 "%s" (mapconcat 'identity (process-command p) " "))
3837 (tramp-set-process-query-on-exit-flag p nil)
3838 (tramp-process-actions p v tramp-actions-copy-out-of-band))))
3840 ;; Reset the transfer process properties.
3841 (tramp-set-connection-property v "process-name" nil)
3842 (tramp-set-connection-property v "process-buffer" nil))
3844 ;; Handle KEEP-DATE argument.
3845 (when (and keep-date (not copy-keep-date))
3846 (set-file-times newname (nth 5 (file-attributes filename))))
3848 ;; Set the mode.
3849 (unless (and keep-date copy-keep-date)
3850 (set-file-modes newname (tramp-default-file-modes filename))))
3852 ;; If the operation was `rename', delete the original file.
3853 (unless (eq op 'copy)
3854 (delete-file filename)))))
3856 (defun tramp-handle-make-directory (dir &optional parents)
3857 "Like `make-directory' for Tramp files."
3858 (setq dir (expand-file-name dir))
3859 (with-parsed-tramp-file-name dir nil
3860 (tramp-flush-directory-property v (file-name-directory localname))
3861 (save-excursion
3862 (tramp-barf-unless-okay
3864 (format "%s %s"
3865 (if parents "mkdir -p" "mkdir")
3866 (tramp-shell-quote-argument localname))
3867 "Couldn't make directory %s" dir))))
3869 (defun tramp-handle-delete-directory (directory &optional recursive)
3870 "Like `delete-directory' for Tramp files."
3871 (setq directory (expand-file-name directory))
3872 (with-parsed-tramp-file-name directory nil
3873 (tramp-flush-directory-property v localname)
3874 (unless (zerop (tramp-send-command-and-check
3876 (format
3877 "%s %s"
3878 (if recursive "rm -rf" "rmdir")
3879 (tramp-shell-quote-argument localname))))
3880 (tramp-error v 'file-error "Couldn't delete %s" directory))))
3882 (defun tramp-handle-delete-file (filename)
3883 "Like `delete-file' for Tramp files."
3884 (setq filename (expand-file-name filename))
3885 (with-parsed-tramp-file-name filename nil
3886 (tramp-flush-file-property v localname)
3887 (unless (zerop (tramp-send-command-and-check
3889 (format "rm -f %s"
3890 (tramp-shell-quote-argument localname))))
3891 (tramp-error v 'file-error "Couldn't delete %s" filename))))
3893 ;; Dired.
3895 ;; CCC: This does not seem to be enough. Something dies when
3896 ;; we try and delete two directories under Tramp :/
3897 (defun tramp-handle-dired-recursive-delete-directory (filename)
3898 "Recursively delete the directory given.
3899 This is like `dired-recursive-delete-directory' for Tramp files."
3900 (with-parsed-tramp-file-name filename nil
3901 ;; Run a shell command 'rm -r <localname>'
3902 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
3903 (unless (file-exists-p filename)
3904 (tramp-error v 'file-error "No such directory: %s" filename))
3905 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
3906 (tramp-send-command
3908 (format "rm -rf %s" (tramp-shell-quote-argument localname))
3909 ;; Don't read the output, do it explicitely.
3910 nil t)
3911 ;; Wait for the remote system to return to us...
3912 ;; This might take a while, allow it plenty of time.
3913 (tramp-wait-for-output (tramp-get-connection-process v) 120)
3914 ;; Make sure that it worked...
3915 (tramp-flush-directory-property v localname)
3916 (and (file-exists-p filename)
3917 (tramp-error
3918 v 'file-error "Failed to recursively delete %s" filename))))
3920 (defun tramp-handle-dired-compress-file (file &rest ok-flag)
3921 "Like `dired-compress-file' for Tramp files."
3922 ;; OK-FLAG is valid for XEmacs only, but not implemented.
3923 ;; Code stolen mainly from dired-aux.el.
3924 (with-parsed-tramp-file-name file nil
3925 (tramp-flush-file-property v localname)
3926 (save-excursion
3927 (let ((suffixes
3928 (if (not (featurep 'xemacs))
3929 ;; Emacs case
3930 (symbol-value 'dired-compress-file-suffixes)
3931 ;; XEmacs has `dired-compression-method-alist', which is
3932 ;; transformed into `dired-compress-file-suffixes' structure.
3933 (mapcar
3934 (lambda (x)
3935 (list (concat (regexp-quote (nth 1 x)) "\\'")
3937 (mapconcat 'identity (nth 3 x) " ")))
3938 (symbol-value 'dired-compression-method-alist))))
3939 suffix)
3940 ;; See if any suffix rule matches this file name.
3941 (while suffixes
3942 (let (case-fold-search)
3943 (if (string-match (car (car suffixes)) localname)
3944 (setq suffix (car suffixes) suffixes nil))
3945 (setq suffixes (cdr suffixes))))
3947 (cond ((file-symlink-p file)
3948 nil)
3949 ((and suffix (nth 2 suffix))
3950 ;; We found an uncompression rule.
3951 (tramp-message v 0 "Uncompressing %s..." file)
3952 (when (zerop (tramp-send-command-and-check
3953 v (concat (nth 2 suffix) " " localname)))
3954 (tramp-message v 0 "Uncompressing %s...done" file)
3955 ;; `dired-remove-file' is not defined in XEmacs
3956 (funcall (symbol-function 'dired-remove-file) file)
3957 (string-match (car suffix) file)
3958 (concat (substring file 0 (match-beginning 0)))))
3960 ;; We don't recognize the file as compressed, so compress it.
3961 ;; Try gzip.
3962 (tramp-message v 0 "Compressing %s..." file)
3963 (when (zerop (tramp-send-command-and-check
3964 v (concat "gzip -f " localname)))
3965 (tramp-message v 0 "Compressing %s...done" file)
3966 ;; `dired-remove-file' is not defined in XEmacs
3967 (funcall (symbol-function 'dired-remove-file) file)
3968 (cond ((file-exists-p (concat file ".gz"))
3969 (concat file ".gz"))
3970 ((file-exists-p (concat file ".z"))
3971 (concat file ".z"))
3972 (t nil)))))))))
3974 (defun tramp-handle-dired-uncache (dir)
3975 "Like `dired-uncache' for Tramp files."
3976 (with-parsed-tramp-file-name dir nil
3977 (tramp-flush-file-property v localname)))
3979 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
3980 ;; not sure at all that this is the right way to do it, but let's hope
3981 ;; it works for now, and wait for a guru to point out the Right Way to
3982 ;; achieve this.
3983 ;;(eval-when-compile
3984 ;; (unless (fboundp 'dired-insert-set-properties)
3985 ;; (fset 'dired-insert-set-properties 'ignore)))
3986 ;; Gerd suggests this:
3987 (eval-when-compile (require 'dired))
3988 ;; Note that dired is required at run-time, too, when it is needed.
3989 ;; It is only needed on XEmacs for the function
3990 ;; `dired-insert-set-properties'.
3992 (defun tramp-handle-insert-directory
3993 (filename switches &optional wildcard full-directory-p)
3994 "Like `insert-directory' for Tramp files."
3995 (setq filename (expand-file-name filename))
3996 (with-parsed-tramp-file-name filename nil
3997 (if (and (featurep 'ls-lisp)
3998 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
3999 (tramp-run-real-handler
4000 'insert-directory (list filename switches wildcard full-directory-p))
4001 (when (and (string-match "^--dired\\s-+" switches)
4002 (not (tramp-get-ls-command-with-dired v)))
4003 (setq switches (replace-match "" nil t switches)))
4004 (tramp-message
4005 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
4006 switches filename (if wildcard "yes" "no")
4007 (if full-directory-p "yes" "no"))
4008 (when wildcard
4009 (setq wildcard (tramp-run-real-handler
4010 'file-name-nondirectory (list localname)))
4011 (setq localname (tramp-run-real-handler
4012 'file-name-directory (list localname))))
4013 (when (listp switches)
4014 (setq switches (mapconcat 'identity switches " ")))
4015 (unless full-directory-p
4016 (setq switches (concat "-d " switches)))
4017 (when wildcard
4018 (setq switches (concat switches " " wildcard)))
4019 ;; If `full-directory-p', we just say `ls -l FILENAME'.
4020 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
4021 (if full-directory-p
4022 (tramp-send-command
4024 (format "%s %s %s"
4025 (tramp-get-ls-command v)
4026 switches
4027 (if wildcard
4028 localname
4029 (tramp-shell-quote-argument (concat localname ".")))))
4030 (tramp-barf-unless-okay
4032 (format "cd %s" (tramp-shell-quote-argument
4033 (tramp-run-real-handler
4034 'file-name-directory (list localname))))
4035 "Couldn't `cd %s'"
4036 (tramp-shell-quote-argument
4037 (tramp-run-real-handler 'file-name-directory (list localname))))
4038 (tramp-send-command
4040 (format "%s %s %s"
4041 (tramp-get-ls-command v)
4042 switches
4043 (if (or wildcard
4044 (zerop (length
4045 (tramp-run-real-handler
4046 'file-name-nondirectory (list localname)))))
4048 (tramp-shell-quote-argument
4049 (tramp-run-real-handler
4050 'file-name-nondirectory (list localname)))))))
4051 (let ((beg (point)))
4052 ;; We cannot use `insert-buffer-substring' because the Tramp
4053 ;; buffer changes its contents before insertion due to calling
4054 ;; `expand-file' and alike.
4055 (insert
4056 (with-current-buffer (tramp-get-buffer v)
4057 (buffer-string)))
4059 ;; Check for "--dired" output.
4060 (forward-line -2)
4061 (when (looking-at "//DIRED//")
4062 (let ((end (tramp-compat-line-end-position))
4063 (linebeg (point)))
4064 ;; Now read the numeric positions of file names.
4065 (goto-char linebeg)
4066 (forward-word 1)
4067 (forward-char 3)
4068 (while (< (point) end)
4069 (let ((start (+ beg (read (current-buffer))))
4070 (end (+ beg (read (current-buffer)))))
4071 (if (memq (char-after end) '(?\n ?\ ))
4072 ;; End is followed by \n or by " -> ".
4073 (put-text-property start end 'dired-filename t)))))
4074 ;; Remove trailing lines.
4075 (goto-char (tramp-compat-line-beginning-position))
4076 (while (looking-at "//")
4077 (forward-line 1)
4078 (delete-region (match-beginning 0) (point)))))
4079 (goto-char (point-max)))))
4081 (defun tramp-handle-unhandled-file-name-directory (filename)
4082 "Like `unhandled-file-name-directory' for Tramp files."
4083 ;; With Emacs 23, we could simply return `nil'. But we must keep it
4084 ;; for backward compatibility.
4085 (expand-file-name "~/"))
4087 ;; Canonicalization of file names.
4089 (defun tramp-handle-expand-file-name (name &optional dir)
4090 "Like `expand-file-name' for Tramp files.
4091 If the localname part of the given filename starts with \"/../\" then
4092 the result will be a local, non-Tramp, filename."
4093 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
4094 (setq dir (or dir default-directory "/"))
4095 ;; Unless NAME is absolute, concat DIR and NAME.
4096 (unless (file-name-absolute-p name)
4097 (setq name (concat (file-name-as-directory dir) name)))
4098 ;; If NAME is not a Tramp file, run the real handler.
4099 (if (not (tramp-tramp-file-p name))
4100 (tramp-run-real-handler 'expand-file-name (list name nil))
4101 ;; Dissect NAME.
4102 (with-parsed-tramp-file-name name nil
4103 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
4104 (setq localname (concat "~/" localname)))
4105 ;; Tilde expansion if necessary. This needs a shell which
4106 ;; groks tilde expansion! The function `tramp-find-shell' is
4107 ;; supposed to find such a shell on the remote host. Please
4108 ;; tell me about it when this doesn't work on your system.
4109 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
4110 (let ((uname (match-string 1 localname))
4111 (fname (match-string 2 localname)))
4112 ;; We cannot simply apply "~/", because under sudo "~/" is
4113 ;; expanded to the local user home directory but to the
4114 ;; root home directory. On the other hand, using always
4115 ;; the default user name for tilde expansion is not
4116 ;; appropriate either, because ssh and companions might
4117 ;; use a user name from the config file.
4118 (when (and (string-equal uname "~")
4119 (string-match "\\`su\\(do\\)?\\'" method))
4120 (setq uname (concat uname user)))
4121 (setq uname
4122 (with-connection-property v uname
4123 (tramp-send-command v (format "cd %s; pwd" uname))
4124 (with-current-buffer (tramp-get-buffer v)
4125 (goto-char (point-min))
4126 (buffer-substring (point) (tramp-compat-line-end-position)))))
4127 (setq localname (concat uname fname))))
4128 ;; There might be a double slash, for example when "~/"
4129 ;; expands to "/". Remove this.
4130 (while (string-match "//" localname)
4131 (setq localname (replace-match "/" t t localname)))
4132 ;; No tilde characters in file name, do normal
4133 ;; `expand-file-name' (this does "/./" and "/../"). We bind
4134 ;; `directory-sep-char' here for XEmacs on Windows, which would
4135 ;; otherwise use backslash. `default-directory' is bound,
4136 ;; because on Windows there would be problems with UNC shares or
4137 ;; Cygwin mounts.
4138 (let ((directory-sep-char ?/)
4139 (default-directory (tramp-compat-temporary-file-directory)))
4140 (tramp-make-tramp-file-name
4141 method user host
4142 (tramp-drop-volume-letter
4143 (tramp-run-real-handler
4144 'expand-file-name (list localname))))))))
4146 (defun tramp-replace-environment-variables (filename)
4147 "Replace environment variables in FILENAME.
4148 Return the string with the replaced variables."
4149 (save-match-data
4150 (let ((idx (string-match "$\\(\\w+\\)" filename)))
4151 ;; `$' is coded as `$$'.
4152 (when (and idx
4153 (or (zerop idx) (not (eq ?$ (aref filename (1- idx)))))
4154 (getenv (match-string 1 filename)))
4155 (setq filename
4156 (replace-match
4157 (substitute-in-file-name (match-string 0 filename))
4158 t nil filename)))
4159 filename)))
4161 (defun tramp-handle-substitute-in-file-name (filename)
4162 "Like `substitute-in-file-name' for Tramp files.
4163 \"//\" and \"/~\" substitute only in the local filename part.
4164 If the URL Tramp syntax is chosen, \"//\" as method delimeter and \"/~\" at
4165 beginning of local filename are not substituted."
4166 ;; First, we must replace environment variables.
4167 (setq filename (tramp-replace-environment-variables filename))
4168 (with-parsed-tramp-file-name filename nil
4169 (if (equal tramp-syntax 'url)
4170 ;; We need to check localname only. The other parts cannot contain
4171 ;; "//" or "/~".
4172 (if (and (> (length localname) 1)
4173 (or (string-match "//" localname)
4174 (string-match "/~" localname 1)))
4175 (tramp-run-real-handler 'substitute-in-file-name (list filename))
4176 (tramp-make-tramp-file-name
4177 (when method (substitute-in-file-name method))
4178 (when user (substitute-in-file-name user))
4179 (when host (substitute-in-file-name host))
4180 (when localname
4181 (tramp-run-real-handler
4182 'substitute-in-file-name (list localname)))))
4183 ;; Ignore in LOCALNAME everything before "//" or "/~".
4184 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
4185 (setq filename
4186 (concat (file-remote-p filename)
4187 (replace-match "\\1" nil nil localname)))
4188 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
4189 (when (string-match "~$" filename)
4190 (setq filename (concat filename "/"))))
4191 (tramp-run-real-handler 'substitute-in-file-name (list filename)))))
4193 ;; In XEmacs, electricity is implemented via a key map for ?/ and ?~,
4194 ;; which calls corresponding functions (see minibuf.el).
4195 (when (fboundp 'minibuffer-electric-separator)
4196 (mapc
4197 (lambda (x)
4198 (eval
4199 `(defadvice ,x
4200 (around ,(intern (format "tramp-advice-%s" x)) activate)
4201 "Invoke `substitute-in-file-name' for Tramp files."
4202 (if (and (symbol-value 'minibuffer-electric-file-name-behavior)
4203 (tramp-tramp-file-p (buffer-substring)))
4204 ;; We don't need to handle `last-input-event', because
4205 ;; due to the key map we know it must be ?/ or ?~.
4206 (let ((s (concat (buffer-substring (point-min) (point))
4207 (string last-command-char))))
4208 (delete-region (point-min) (point))
4209 (insert (substitute-in-file-name s))
4210 (setq ad-return-value last-command-char))
4211 ad-do-it))))
4213 '(minibuffer-electric-separator
4214 minibuffer-electric-tilde)))
4217 ;;; Remote commands:
4219 (defun tramp-handle-executable-find (command)
4220 "Like `executable-find' for Tramp files."
4221 (with-parsed-tramp-file-name default-directory nil
4222 (tramp-find-executable v command (tramp-get-remote-path v) t)))
4224 ;; We use BUFFER also as connection buffer during setup. Because of
4225 ;; this, its original contents must be saved, and restored once
4226 ;; connection has been setup.
4227 (defun tramp-handle-start-file-process (name buffer program &rest args)
4228 "Like `start-file-process' for Tramp files."
4229 (with-parsed-tramp-file-name default-directory nil
4230 (unless (stringp program)
4231 (tramp-error
4232 v 'file-error "pty association is not supported for `%s'" name))
4233 (unwind-protect
4234 (let ((command (format "cd %s; exec %s"
4235 (tramp-shell-quote-argument localname)
4236 (mapconcat 'tramp-shell-quote-argument
4237 (cons program args) " ")))
4238 (name1 name)
4239 (i 0))
4240 (unless buffer
4241 ;; BUFFER can be nil. We use a temporary buffer.
4242 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
4243 (while (get-process name1)
4244 ;; NAME must be unique as process name.
4245 (setq i (1+ i)
4246 name1 (format "%s<%d>" name i)))
4247 (setq name name1)
4248 ;; Set the new process properties.
4249 (tramp-set-connection-property v "process-name" name)
4250 (tramp-set-connection-property v "process-buffer" buffer)
4251 ;; Activate narrowing in order to save BUFFER contents.
4252 ;; Clear also the modification time; otherwise we might be
4253 ;; interrupted by `verify-visited-file-modtime'.
4254 (with-current-buffer (tramp-get-connection-buffer v)
4255 (clear-visited-file-modtime)
4256 (narrow-to-region (point-max) (point-max)))
4257 ;; Send the command. `tramp-send-command' opens a new
4258 ;; connection.
4259 (tramp-send-command v command nil t) ; nooutput
4260 ;; Set query flag for this process.
4261 (tramp-set-process-query-on-exit-flag
4262 (tramp-get-connection-process v) t)
4263 ;; Return process.
4264 (tramp-get-connection-process v))
4265 ;; Save exit.
4266 (with-current-buffer (tramp-get-connection-buffer v)
4267 (if (string-match tramp-temp-buffer-name (buffer-name))
4268 (progn
4269 (set-process-buffer (tramp-get-connection-process v) nil)
4270 (kill-buffer (current-buffer)))
4271 (widen)
4272 (goto-char (point-max))))
4273 (tramp-set-connection-property v "process-name" nil)
4274 (tramp-set-connection-property v "process-buffer" nil))))
4276 (defun tramp-handle-process-file
4277 (program &optional infile destination display &rest args)
4278 "Like `process-file' for Tramp files."
4279 ;; The implementation is not complete yet.
4280 (when (and (numberp destination) (zerop destination))
4281 (error "Implementation does not handle immediate return"))
4283 (with-parsed-tramp-file-name default-directory nil
4284 (let (command input tmpinput stderr tmpstderr outbuf ret)
4285 ;; Compute command.
4286 (setq command (mapconcat 'tramp-shell-quote-argument
4287 (cons program args) " "))
4288 ;; Determine input.
4289 (if (null infile)
4290 (setq input "/dev/null")
4291 (setq infile (expand-file-name infile))
4292 (if (tramp-equal-remote default-directory infile)
4293 ;; INFILE is on the same remote host.
4294 (setq input (with-parsed-tramp-file-name infile nil localname))
4295 ;; INFILE must be copied to remote host.
4296 (setq input (tramp-make-tramp-temp-file v)
4297 tmpinput (tramp-make-tramp-file-name method user host input))
4298 (copy-file infile tmpinput t)))
4299 (when input (setq command (format "%s <%s" command input)))
4301 ;; Determine output.
4302 (cond
4303 ;; Just a buffer.
4304 ((bufferp destination)
4305 (setq outbuf destination))
4306 ;; A buffer name.
4307 ((stringp destination)
4308 (setq outbuf (get-buffer-create destination)))
4309 ;; (REAL-DESTINATION ERROR-DESTINATION)
4310 ((consp destination)
4311 ;; output.
4312 (cond
4313 ((bufferp (car destination))
4314 (setq outbuf (car destination)))
4315 ((stringp (car destination))
4316 (setq outbuf (get-buffer-create (car destination))))
4317 ((car destination)
4318 (setq outbuf (current-buffer))))
4319 ;; stderr.
4320 (cond
4321 ((stringp (cadr destination))
4322 (setcar (cdr destination) (expand-file-name (cadr destination)))
4323 (if (tramp-equal-remote default-directory (cadr destination))
4324 ;; stderr is on the same remote host.
4325 (setq stderr (with-parsed-tramp-file-name
4326 (cadr destination) nil localname))
4327 ;; stderr must be copied to remote host. The temporary
4328 ;; file must be deleted after execution.
4329 (setq stderr (tramp-make-tramp-temp-file v)
4330 tmpstderr (tramp-make-tramp-file-name
4331 method user host stderr))))
4332 ;; stderr to be discarded.
4333 ((null (cadr destination))
4334 (setq stderr "/dev/null"))))
4335 ;; 't
4336 (destination
4337 (setq outbuf (current-buffer))))
4338 (when stderr (setq command (format "%s 2>%s" command stderr)))
4340 ;; Send the command. It might not return in time, so we protect it.
4341 (condition-case nil
4342 (unwind-protect
4343 (setq ret
4344 (tramp-send-command-and-check
4345 v (format "\\cd %s; %s"
4346 (tramp-shell-quote-argument localname)
4347 command)))
4348 ;; We should show the output anyway.
4349 (when outbuf
4350 (with-current-buffer outbuf
4351 (insert
4352 (with-current-buffer (tramp-get-connection-buffer v)
4353 (buffer-string))))
4354 (when display (display-buffer outbuf))))
4355 ;; When the user did interrupt, we should do it also. We use
4356 ;; return code -1 as marker.
4357 (quit
4358 (kill-buffer (tramp-get-connection-buffer v))
4359 (setq ret -1))
4360 ;; Handle errors.
4361 (error
4362 (kill-buffer (tramp-get-connection-buffer v))
4363 (setq ret 1)))
4365 ;; Provide error file.
4366 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
4368 ;; Cleanup. We remove all file cache values for the connection,
4369 ;; because the remote process could have changed them.
4370 (when tmpinput (delete-file tmpinput))
4372 ;; `process-file-side-effects' has been introduced with GNU
4373 ;; Emacs 23.2. If set to `nil', no remote file will be changed
4374 ;; by `program'. If it doesn't exist, we assume its default
4375 ;; value 't'.
4376 (unless (and (boundp 'process-file-side-effects)
4377 (not (symbol-value 'process-file-side-effects)))
4378 (tramp-flush-directory-property v ""))
4380 ;; Return exit status.
4381 (if (equal ret -1)
4382 (keyboard-quit)
4383 ret))))
4385 (defun tramp-local-call-process
4386 (program &optional infile destination display &rest args)
4387 "Calls `call-process' on the local host.
4388 This is needed because for some Emacs flavors Tramp has
4389 defadviced `call-process' to behave like `process-file'. The
4390 Lisp error raised when PROGRAM is nil is trapped also, returning 1."
4391 (let ((default-directory
4392 (if (file-remote-p default-directory)
4393 (tramp-compat-temporary-file-directory)
4394 default-directory)))
4395 (if (executable-find program)
4396 (apply 'call-process program infile destination display args)
4397 1)))
4399 (defun tramp-handle-call-process-region
4400 (start end program &optional delete buffer display &rest args)
4401 "Like `call-process-region' for Tramp files."
4402 (let ((tmpfile (tramp-compat-make-temp-file "")))
4403 (write-region start end tmpfile)
4404 (when delete (delete-region start end))
4405 (unwind-protect
4406 (apply 'call-process program tmpfile buffer display args)
4407 (delete-file tmpfile))))
4409 (defun tramp-handle-shell-command
4410 (command &optional output-buffer error-buffer)
4411 "Like `shell-command' for Tramp files."
4412 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
4413 ;; We cannot use `shell-file-name' and `shell-command-switch',
4414 ;; they are variables of the local host.
4415 (args (list "/bin/sh" "-c" (substring command 0 asynchronous)))
4416 current-buffer-p
4417 (output-buffer
4418 (cond
4419 ((bufferp output-buffer) output-buffer)
4420 ((stringp output-buffer) (get-buffer-create output-buffer))
4421 (output-buffer
4422 (setq current-buffer-p t)
4423 (current-buffer))
4424 (t (get-buffer-create
4425 (if asynchronous
4426 "*Async Shell Command*"
4427 "*Shell Command Output*")))))
4428 (error-buffer
4429 (cond
4430 ((bufferp error-buffer) error-buffer)
4431 ((stringp error-buffer) (get-buffer-create error-buffer))))
4432 (buffer
4433 (if (and (not asynchronous) error-buffer)
4434 (with-parsed-tramp-file-name default-directory nil
4435 (list output-buffer (tramp-make-tramp-temp-file v)))
4436 output-buffer))
4437 (p (get-buffer-process output-buffer)))
4439 ;; Check whether there is another process running. Tramp does not
4440 ;; support 2 (asynchronous) processes in parallel.
4441 (when p
4442 (if (yes-or-no-p "A command is running. Kill it? ")
4443 (condition-case nil
4444 (kill-process p)
4445 (error nil))
4446 (error "Shell command in progress")))
4448 (if current-buffer-p
4449 (progn
4450 (barf-if-buffer-read-only)
4451 (push-mark nil t))
4452 (with-current-buffer output-buffer
4453 (setq buffer-read-only nil)
4454 (erase-buffer)))
4456 (if (and (not current-buffer-p) (integerp asynchronous))
4457 (prog1
4458 ;; Run the process.
4459 (apply 'start-file-process "*Async Shell*" buffer args)
4460 ;; Display output.
4461 (pop-to-buffer output-buffer)
4462 (setq mode-line-process '(":%s"))
4463 (require 'shell) (shell-mode))
4465 (prog1
4466 ;; Run the process.
4467 (apply 'process-file (car args) nil buffer nil (cdr args))
4468 ;; Insert error messages if they were separated.
4469 (when (listp buffer)
4470 (with-current-buffer error-buffer
4471 (insert-file-contents (cadr buffer)))
4472 (delete-file (cadr buffer)))
4473 (if current-buffer-p
4474 ;; This is like exchange-point-and-mark, but doesn't
4475 ;; activate the mark. It is cleaner to avoid activation,
4476 ;; even though the command loop would deactivate the mark
4477 ;; because we inserted text.
4478 (goto-char (prog1 (mark t)
4479 (set-marker (mark-marker) (point)
4480 (current-buffer))))
4481 ;; There's some output, display it.
4482 (when (with-current-buffer output-buffer (> (point-max) (point-min)))
4483 (if (functionp 'display-message-or-buffer)
4484 (funcall (symbol-function 'display-message-or-buffer)
4485 output-buffer)
4486 (pop-to-buffer output-buffer))))))))
4488 ;; File Editing.
4490 (defvar tramp-handle-file-local-copy-hook nil
4491 "Normal hook to be run at the end of `tramp-handle-file-local-copy'.")
4493 (defun tramp-handle-file-local-copy (filename)
4494 "Like `file-local-copy' for Tramp files."
4496 (with-parsed-tramp-file-name filename nil
4497 (unless (file-exists-p filename)
4498 (tramp-error
4499 v 'file-error
4500 "Cannot make local copy of non-existing file `%s'" filename))
4502 (let ((rem-enc (tramp-get-remote-coding v "remote-encoding"))
4503 (loc-dec (tramp-get-local-coding v "local-decoding"))
4504 (tmpfile (tramp-compat-make-temp-file filename)))
4506 (condition-case err
4507 (cond
4508 ;; `copy-file' handles direct copy and out-of-band methods.
4509 ((or (tramp-local-host-p v)
4510 (tramp-method-out-of-band-p
4511 v (nth 7 (file-attributes filename))))
4512 (copy-file filename tmpfile t t))
4514 ;; Use inline encoding for file transfer.
4515 (rem-enc
4516 (save-excursion
4517 (tramp-message v 5 "Encoding remote file %s..." filename)
4518 (tramp-barf-unless-okay
4520 (format "%s < %s" rem-enc (tramp-shell-quote-argument localname))
4521 "Encoding remote file failed")
4522 (tramp-message v 5 "Encoding remote file %s...done" filename)
4524 (if (and (symbolp loc-dec) (fboundp loc-dec))
4525 ;; If local decoding is a function, we call it. We
4526 ;; must disable multibyte, because
4527 ;; `uudecode-decode-region' doesn't handle it
4528 ;; correctly.
4529 (with-temp-buffer
4530 (set-buffer-multibyte nil)
4531 (insert-buffer-substring (tramp-get-buffer v))
4532 (tramp-message
4533 v 5 "Decoding remote file %s with function %s..."
4534 filename loc-dec)
4535 (funcall loc-dec (point-min) (point-max))
4536 ;; Unset `file-name-handler-alist'. Otherwise,
4537 ;; epa-file gets confused.
4538 (let (file-name-handler-alist
4539 (coding-system-for-write 'binary))
4540 (write-region (point-min) (point-max) tmpfile)))
4542 ;; If tramp-decoding-function is not defined for this
4543 ;; method, we invoke tramp-decoding-command instead.
4544 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
4545 ;; Unset `file-name-handler-alist'. Otherwise,
4546 ;; epa-file gets confused.
4547 (let (file-name-handler-alist
4548 (coding-system-for-write 'binary))
4549 (write-region (point-min) (point-max) tmpfile2))
4550 (tramp-message
4551 v 5 "Decoding remote file %s with command %s..."
4552 filename loc-dec)
4553 (unwind-protect
4554 (tramp-call-local-coding-command loc-dec tmpfile2 tmpfile)
4555 (delete-file tmpfile2))))
4557 (tramp-message v 5 "Decoding remote file %s...done" filename)
4558 ;; Set proper permissions.
4559 (set-file-modes tmpfile (tramp-default-file-modes filename))
4560 ;; Set local user ownership.
4561 (tramp-set-file-uid-gid tmpfile)))
4563 ;; Oops, I don't know what to do.
4564 (t (tramp-error
4565 v 'file-error "Wrong method specification for `%s'" method)))
4567 ;; Error handling.
4568 ((error quit)
4569 (delete-file tmpfile)
4570 (signal (car err) (cdr err))))
4572 (run-hooks 'tramp-handle-file-local-copy-hook)
4573 tmpfile)))
4575 (defun tramp-handle-file-remote-p (filename &optional identification connected)
4576 "Like `file-remote-p' for Tramp files."
4577 (when (tramp-tramp-file-p filename)
4578 (with-parsed-tramp-file-name filename nil
4579 (and (or (not connected)
4580 (let ((p (tramp-get-connection-process v)))
4581 (and p (processp p) (memq (process-status p) '(run open)))))
4582 (cond
4583 ((eq identification 'method) method)
4584 ((eq identification 'user) user)
4585 ((eq identification 'host) host)
4586 ((eq identification 'localname) localname)
4587 (t (tramp-make-tramp-file-name method user host "")))))))
4589 (defun tramp-find-file-name-coding-system-alist (filename tmpname)
4590 "Like `find-operation-coding-system' for Tramp filenames.
4591 Tramp's `insert-file-contents' and `write-region' work over
4592 temporary file names. If `file-coding-system-alist' contains an
4593 expression, which matches more than the file name suffix, the
4594 coding system might not be determined. This function repairs it."
4595 (let (result)
4596 (dolist (elt file-coding-system-alist result)
4597 (when (and (consp elt) (string-match (car elt) filename))
4598 ;; We found a matching entry in `file-coding-system-alist'.
4599 ;; So we add a similar entry, but with the temporary file name
4600 ;; as regexp.
4601 (add-to-list
4602 'result (cons (regexp-quote tmpname) (cdr elt)) 'append)))))
4604 (defun tramp-handle-insert-file-contents
4605 (filename &optional visit beg end replace)
4606 "Like `insert-file-contents' for Tramp files."
4607 (barf-if-buffer-read-only)
4608 (setq filename (expand-file-name filename))
4609 (let (coding-system-used result local-copy remote-copy)
4610 (with-parsed-tramp-file-name filename nil
4611 (unwind-protect
4612 (if (not (file-exists-p filename))
4613 ;; We don't raise a Tramp error, because it might be
4614 ;; suppressed, like in `find-file-noselect-1'.
4615 (signal 'file-error
4616 (list "File not found on remote host" filename))
4618 (if (and (tramp-local-host-p v)
4619 (let (file-name-handler-alist)
4620 (file-readable-p localname)))
4621 ;; Short track: if we are on the local host, we can
4622 ;; run directly.
4623 (setq result
4624 (tramp-run-real-handler
4625 'insert-file-contents
4626 (list localname visit beg end replace)))
4628 ;; When we shall insert only a part of the file, we copy
4629 ;; this part.
4630 (when (or beg end)
4631 (setq remote-copy (tramp-make-tramp-temp-file v))
4632 (tramp-send-command
4634 (cond
4635 ((and beg end)
4636 (format "tail -c +%d %s | head -c +%d >%s"
4637 (1+ beg) (tramp-shell-quote-argument localname)
4638 (- end beg) remote-copy))
4639 (beg
4640 (format "tail -c +%d %s >%s"
4641 (1+ beg) (tramp-shell-quote-argument localname)
4642 remote-copy))
4643 (end
4644 (format "head -c +%d %s >%s"
4645 (1+ end) (tramp-shell-quote-argument localname)
4646 remote-copy)))))
4648 ;; `insert-file-contents-literally' takes care to avoid
4649 ;; calling jka-compr. By let-binding
4650 ;; `inhibit-file-name-operation', we propagate that care
4651 ;; to the `file-local-copy' operation.
4652 (setq local-copy
4653 (let ((inhibit-file-name-operation
4654 (when (eq inhibit-file-name-operation
4655 'insert-file-contents)
4656 'file-local-copy)))
4657 (cond
4658 ((stringp remote-copy)
4659 (file-local-copy
4660 (tramp-make-tramp-file-name
4661 method user host remote-copy)))
4662 ((stringp tramp-temp-buffer-file-name)
4663 (copy-file filename tramp-temp-buffer-file-name 'ok)
4664 tramp-temp-buffer-file-name)
4665 (t (file-local-copy filename)))))
4667 (when (and (null remote-copy)
4668 (tramp-get-method-parameter
4669 method 'tramp-copy-keep-tmpfile))
4670 ;; We keep the local file for performance reasons,
4671 ;; useful for "rsync".
4672 (set-file-modes local-copy (tramp-octal-to-decimal "0600"))
4673 (setq tramp-temp-buffer-file-name local-copy)
4674 (put 'tramp-temp-buffer-file-name 'permanent-local t))
4676 (tramp-message
4677 v 4 "Inserting local temp file `%s'..." local-copy)
4679 ;; We must ensure that `file-coding-system-alist'
4680 ;; matches `local-copy'.
4681 (let ((file-coding-system-alist
4682 (tramp-find-file-name-coding-system-alist
4683 filename local-copy)))
4684 (setq result
4685 (insert-file-contents
4686 local-copy nil nil nil replace))
4687 ;; Now `last-coding-system-used' has right value.
4688 ;; Remember it.
4689 (when (boundp 'last-coding-system-used)
4690 (setq coding-system-used
4691 (symbol-value 'last-coding-system-used))))
4693 (tramp-message
4694 v 4 "Inserting local temp file `%s'...done" local-copy)
4695 (when (boundp 'last-coding-system-used)
4696 (set 'last-coding-system-used coding-system-used))))
4698 ;; Save exit.
4699 (progn
4700 (when visit
4701 (setq buffer-file-name filename)
4702 (setq buffer-read-only (not (file-writable-p filename)))
4703 (set-visited-file-modtime)
4704 (set-buffer-modified-p nil))
4705 (when (and (stringp local-copy)
4706 (or remote-copy (null tramp-temp-buffer-file-name)))
4707 (delete-file local-copy))
4708 (when (stringp remote-copy)
4709 (delete-file
4710 (tramp-make-tramp-file-name method user host remote-copy))))))
4712 ;; Result.
4713 (list (expand-file-name filename)
4714 (cadr result))))
4716 ;; This is needed for XEmacs only. Code stolen from files.el.
4717 (defun tramp-handle-insert-file-contents-literally
4718 (filename &optional visit beg end replace)
4719 "Like `insert-file-contents-literally' for Tramp files."
4720 (let ((format-alist nil)
4721 (after-insert-file-functions nil)
4722 (coding-system-for-read 'no-conversion)
4723 (coding-system-for-write 'no-conversion)
4724 (find-buffer-file-type-function
4725 (if (fboundp 'find-buffer-file-type)
4726 (symbol-function 'find-buffer-file-type)
4727 nil))
4728 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler))
4729 (inhibit-file-name-operation 'insert-file-contents))
4730 (unwind-protect
4731 (progn
4732 (fset 'find-buffer-file-type (lambda (filename) t))
4733 (insert-file-contents filename visit beg end replace))
4734 ;; Save exit.
4735 (if find-buffer-file-type-function
4736 (fset 'find-buffer-file-type find-buffer-file-type-function)
4737 (fmakunbound 'find-buffer-file-type)))))
4739 (defun tramp-handle-find-backup-file-name (filename)
4740 "Like `find-backup-file-name' for Tramp files."
4741 (with-parsed-tramp-file-name filename nil
4742 ;; We set both variables. It doesn't matter whether it is
4743 ;; Emacs or XEmacs
4744 (let ((backup-directory-alist
4745 ;; Emacs case
4746 (when (boundp 'backup-directory-alist)
4747 (if (symbol-value 'tramp-backup-directory-alist)
4748 (mapcar
4749 (lambda (x)
4750 (cons
4751 (car x)
4752 (if (and (stringp (cdr x))
4753 (file-name-absolute-p (cdr x))
4754 (not (tramp-file-name-p (cdr x))))
4755 (tramp-make-tramp-file-name method user host (cdr x))
4756 (cdr x))))
4757 (symbol-value 'tramp-backup-directory-alist))
4758 (symbol-value 'backup-directory-alist))))
4760 (bkup-backup-directory-info
4761 ;; XEmacs case
4762 (when (boundp 'bkup-backup-directory-info)
4763 (if (symbol-value 'tramp-bkup-backup-directory-info)
4764 (mapcar
4765 (lambda (x)
4766 (nconc
4767 (list (car x))
4768 (list
4769 (if (and (stringp (car (cdr x)))
4770 (file-name-absolute-p (car (cdr x)))
4771 (not (tramp-file-name-p (car (cdr x)))))
4772 (tramp-make-tramp-file-name
4773 method user host (car (cdr x)))
4774 (car (cdr x))))
4775 (cdr (cdr x))))
4776 (symbol-value 'tramp-bkup-backup-directory-info))
4777 (symbol-value 'bkup-backup-directory-info)))))
4779 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
4781 (defun tramp-handle-make-auto-save-file-name ()
4782 "Like `make-auto-save-file-name' for Tramp files.
4783 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
4784 (let ((tramp-auto-save-directory tramp-auto-save-directory)
4785 (buffer-file-name
4786 (tramp-subst-strs-in-string
4787 '(("_" . "|")
4788 ("/" . "_a")
4789 (":" . "_b")
4790 ("|" . "__")
4791 ("[" . "_l")
4792 ("]" . "_r"))
4793 (buffer-file-name))))
4794 ;; File name must be unique. This is ensured with Emacs 22 (see
4795 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
4796 ;; all other cases we must do it ourselves.
4797 (when (boundp 'auto-save-file-name-transforms)
4798 (mapc
4799 (lambda (x)
4800 (when (and (string-match (car x) buffer-file-name)
4801 (not (car (cddr x))))
4802 (setq tramp-auto-save-directory
4803 (or tramp-auto-save-directory
4804 (tramp-compat-temporary-file-directory)))))
4805 (symbol-value 'auto-save-file-name-transforms)))
4806 ;; Create directory.
4807 (when tramp-auto-save-directory
4808 (setq buffer-file-name
4809 (expand-file-name buffer-file-name tramp-auto-save-directory))
4810 (unless (file-exists-p tramp-auto-save-directory)
4811 (make-directory tramp-auto-save-directory t)))
4812 ;; Run plain `make-auto-save-file-name'. There might be an advice when
4813 ;; it is not a magic file name operation (since Emacs 22).
4814 ;; We must deactivate it temporarily.
4815 (if (not (ad-is-active 'make-auto-save-file-name))
4816 (tramp-run-real-handler 'make-auto-save-file-name nil)
4817 ;; else
4818 (ad-deactivate 'make-auto-save-file-name)
4819 (prog1
4820 (tramp-run-real-handler 'make-auto-save-file-name nil)
4821 (ad-activate 'make-auto-save-file-name)))))
4823 (defvar tramp-handle-write-region-hook nil
4824 "Normal hook to be run at the end of `tramp-handle-write-region'.")
4826 ;; CCC grok LOCKNAME
4827 (defun tramp-handle-write-region
4828 (start end filename &optional append visit lockname confirm)
4829 "Like `write-region' for Tramp files."
4830 (setq filename (expand-file-name filename))
4831 (with-parsed-tramp-file-name filename nil
4832 ;; Following part commented out because we don't know what to do about
4833 ;; file locking, and it does not appear to be a problem to ignore it.
4834 ;; Ange-ftp ignores it, too.
4835 ;; (when (and lockname (stringp lockname))
4836 ;; (setq lockname (expand-file-name lockname)))
4837 ;; (unless (or (eq lockname nil)
4838 ;; (string= lockname filename))
4839 ;; (error
4840 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
4842 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
4843 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
4844 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
4845 (tramp-error v 'file-error "File not overwritten")))
4847 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
4848 (tramp-get-remote-uid v 'integer)))
4849 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
4850 (tramp-get-remote-gid v 'integer))))
4852 (if (and (tramp-local-host-p v)
4853 ;; `file-writable-p' calls `file-expand-file-name'. We
4854 ;; cannot use `tramp-run-real-handler' therefore.
4855 (let (file-name-handler-alist)
4856 (and
4857 (file-writable-p (file-name-directory localname))
4858 (or (file-directory-p localname)
4859 (file-writable-p localname)))))
4860 ;; Short track: if we are on the local host, we can run directly.
4861 (progn
4862 (tramp-run-real-handler
4863 'write-region
4864 (list start end localname append 'no-message lockname confirm))
4865 (tramp-flush-file-property v localname))
4867 (let ((rem-dec (tramp-get-remote-coding v "remote-decoding"))
4868 (loc-enc (tramp-get-local-coding v "local-encoding"))
4869 (modes (save-excursion (tramp-default-file-modes filename)))
4870 ;; We use this to save the value of
4871 ;; `last-coding-system-used' after writing the tmp file.
4872 ;; At the end of the function, we set
4873 ;; `last-coding-system-used' to this saved value. This
4874 ;; way, any intermediary coding systems used while
4875 ;; talking to the remote shell or suchlike won't hose
4876 ;; this variable. This approach was snarfed from
4877 ;; ange-ftp.el.
4878 coding-system-used
4879 ;; Write region into a tmp file. This isn't really
4880 ;; needed if we use an encoding function, but currently
4881 ;; we use it always because this makes the logic
4882 ;; simpler.
4883 (tmpfile (or tramp-temp-buffer-file-name
4884 (tramp-compat-make-temp-file filename))))
4886 ;; If `append' is non-nil, we copy the file locally, and let
4887 ;; the native `write-region' implementation do the job.
4888 (when append (copy-file filename tmpfile 'ok))
4890 ;; We say `no-message' here because we don't want the
4891 ;; visited file modtime data to be clobbered from the temp
4892 ;; file. We call `set-visited-file-modtime' ourselves later
4893 ;; on. We must ensure that `file-coding-system-alist'
4894 ;; matches `tmpfile'.
4895 (let ((file-coding-system-alist
4896 (tramp-find-file-name-coding-system-alist filename tmpfile)))
4897 (condition-case err
4898 (tramp-run-real-handler
4899 'write-region
4900 (list start end tmpfile append 'no-message lockname confirm))
4901 ((error quit)
4902 (setq tramp-temp-buffer-file-name nil)
4903 (delete-file tmpfile)
4904 (signal (car err) (cdr err))))
4906 ;; Now, `last-coding-system-used' has the right value. Remember it.
4907 (when (boundp 'last-coding-system-used)
4908 (setq coding-system-used
4909 (symbol-value 'last-coding-system-used))))
4911 ;; The permissions of the temporary file should be set. If
4912 ;; filename does not exist (eq modes nil) it has been
4913 ;; renamed to the backup file. This case `save-buffer'
4914 ;; handles permissions.
4915 (when modes (set-file-modes tmpfile modes))
4917 ;; This is a bit lengthy due to the different methods
4918 ;; possible for file transfer. First, we check whether the
4919 ;; method uses an rcp program. If so, we call it.
4920 ;; Otherwise, both encoding and decoding command must be
4921 ;; specified. However, if the method _also_ specifies an
4922 ;; encoding function, then that is used for encoding the
4923 ;; contents of the tmp file.
4924 (cond
4925 ;; `rename-file' handles direct copy and out-of-band methods.
4926 ((or (tramp-local-host-p v)
4927 (tramp-method-out-of-band-p
4928 v (- (or end (point-max)) (or start (point-min)))))
4929 (condition-case err
4930 (if (and (= (or end (point-max)) (point-max))
4931 (= (or start (point-min)) (point-min))
4932 (tramp-get-method-parameter
4933 method 'tramp-copy-keep-tmpfile))
4934 (progn
4935 ;; We keep the local file for performance
4936 ;; reasons, useful for "rsync".
4937 (setq tramp-temp-buffer-file-name tmpfile)
4938 (copy-file tmpfile filename t))
4939 (setq tramp-temp-buffer-file-name nil)
4940 (rename-file tmpfile filename t))
4941 ((error quit)
4942 (setq tramp-temp-buffer-file-name nil)
4943 (delete-file tmpfile)
4944 (signal (car err) (cdr err)))))
4946 ;; Use inline file transfer.
4947 (rem-dec
4948 ;; Encode tmpfile.
4949 (tramp-message v 5 "Encoding region...")
4950 (unwind-protect
4951 (with-temp-buffer
4952 ;; Use encoding function or command.
4953 (if (and (symbolp loc-enc) (fboundp loc-enc))
4954 (progn
4955 (tramp-message
4956 v 5 "Encoding region using function `%s'..."
4957 (symbol-name loc-enc))
4958 (let ((coding-system-for-read 'binary))
4959 (insert-file-contents-literally tmpfile))
4960 ;; The following `let' is a workaround for the
4961 ;; base64.el that comes with pgnus-0.84. If
4962 ;; both of the following conditions are
4963 ;; satisfied, it tries to write to a local
4964 ;; file in default-directory, but at this
4965 ;; point, default-directory is remote.
4966 ;; (`call-process-region' can't write to
4967 ;; remote files, it seems.) The file in
4968 ;; question is a tmp file anyway.
4969 (let ((default-directory
4970 (tramp-compat-temporary-file-directory)))
4971 (funcall loc-enc (point-min) (point-max))))
4973 (tramp-message
4974 v 5 "Encoding region using command `%s'..." loc-enc)
4975 (unless (equal 0 (tramp-call-local-coding-command
4976 loc-enc tmpfile t))
4977 (tramp-error
4978 v 'file-error
4979 "Cannot write to `%s', local encoding command `%s' failed"
4980 filename loc-enc)))
4982 ;; Send buffer into remote decoding command which
4983 ;; writes to remote file. Because this happens on
4984 ;; the remote host, we cannot use the function.
4985 (goto-char (point-max))
4986 (unless (bolp) (newline))
4987 (tramp-message
4988 v 5 "Decoding region into remote file %s..." filename)
4989 (tramp-send-command
4991 (format
4992 "%s >%s <<'EOF'\n%sEOF"
4993 rem-dec
4994 (tramp-shell-quote-argument localname)
4995 (buffer-string)))
4996 (tramp-barf-unless-okay
4997 v nil
4998 "Couldn't write region to `%s', decode using `%s' failed"
4999 filename rem-dec)
5000 ;; When `file-precious-flag' is set, the region is
5001 ;; written to a temporary file. Check that the
5002 ;; checksum is equal to that from the local tmpfile.
5003 (when file-precious-flag
5004 (erase-buffer)
5005 (and
5006 ;; cksum runs locally, if possible.
5007 (zerop (tramp-local-call-process "cksum" tmpfile t))
5008 ;; cksum runs remotely.
5009 (zerop
5010 (tramp-send-command-and-check
5012 (format
5013 "cksum <%s" (tramp-shell-quote-argument localname))))
5014 ;; ... they are different.
5015 (not
5016 (string-equal
5017 (buffer-string)
5018 (with-current-buffer (tramp-get-buffer v)
5019 (buffer-string))))
5020 (tramp-error
5021 v 'file-error
5022 (concat "Couldn't write region to `%s',"
5023 " decode using `%s' failed")
5024 filename rem-dec)))
5025 (tramp-message
5026 v 5 "Decoding region into remote file %s...done" filename)
5027 (tramp-flush-file-property v localname))
5029 ;; Save exit.
5030 (delete-file tmpfile)))
5032 ;; That's not expected.
5034 (tramp-error
5035 v 'file-error
5036 (concat "Method `%s' should specify both encoding and "
5037 "decoding command or an rcp program")
5038 method)))
5040 ;; Make `last-coding-system-used' have the right value.
5041 (when coding-system-used
5042 (set 'last-coding-system-used coding-system-used))))
5044 ;; We must protect `last-coding-system-used', now we have set it
5045 ;; to its correct value.
5046 (let (last-coding-system-used (need-chown t))
5047 ;; Set file modification time.
5048 (when (or (eq visit t) (stringp visit))
5049 (let ((file-attr (file-attributes filename)))
5050 (set-visited-file-modtime
5051 ;; We must pass modtime explicitely, because filename can
5052 ;; be different from (buffer-file-name), f.e. if
5053 ;; `file-precious-flag' is set.
5054 (nth 5 file-attr))
5055 (when (and (eq (nth 2 file-attr) uid)
5056 (eq (nth 3 file-attr) gid))
5057 (setq need-chown nil))))
5059 ;; Set the ownership.
5060 (when need-chown
5061 (tramp-set-file-uid-gid filename uid gid))
5062 (when (or (eq visit t) (null visit) (stringp visit))
5063 (tramp-message v 0 "Wrote %s" filename))
5064 (run-hooks 'tramp-handle-write-region-hook)))))
5066 (defvar tramp-vc-registered-file-names nil
5067 "List used to collect file names, which are checked during `vc-registered'.")
5069 ;; VC backends check for the existence of various different special
5070 ;; files. This is very time consuming, because every single check
5071 ;; requires a remote command (the file cache must be invalidated).
5072 ;; Therefore, we apply a kind of optimization. We install the file
5073 ;; name handler `tramp-vc-file-name-handler', which does nothing but
5074 ;; remembers all file names for which `file-exists-p' or
5075 ;; `file-readable-p' has been applied. A first run of `vc-registered'
5076 ;; is performed. Afterwards, a script is applied for all collected
5077 ;; file names, using just one remote command. The result of this
5078 ;; script is used to fill the file cache with actual values. Now we
5079 ;; can reset the file name handlers, and we make a second run of
5080 ;; `vc-registered', which returns the expected result without sending
5081 ;; any other remote command.
5082 (defun tramp-handle-vc-registered (file)
5083 "Like `vc-registered' for Tramp files."
5084 (with-parsed-tramp-file-name file nil
5086 ;; There could be new files, created by the vc backend. We cannot
5087 ;; reuse the old cache entries, therefore.
5088 (let (tramp-vc-registered-file-names
5089 (tramp-cache-inhibit-cache (current-time))
5090 (file-name-handler-alist
5091 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
5093 ;; Here we collect only file names, which need an operation.
5094 (tramp-run-real-handler 'vc-registered (list file))
5095 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
5097 ;; Send just one command, in order to fill the cache.
5098 (when tramp-vc-registered-file-names
5099 (tramp-maybe-send-script
5101 (format tramp-vc-registered-read-file-names
5102 (tramp-get-file-exists-command v)
5103 (format "%s -r" (tramp-get-test-command v)))
5104 "tramp_vc_registered_read_file_names")
5106 (dolist
5107 (elt
5108 (tramp-send-command-and-read
5110 (format
5111 "tramp_vc_registered_read_file_names %s"
5112 (mapconcat 'tramp-shell-quote-argument
5113 tramp-vc-registered-file-names
5114 " "))))
5116 (tramp-set-file-property v (car elt) (cadr elt) (cadr (cdr elt))))))
5118 ;; Second run. Now all `file-exists-p' or `file-readable-p' calls
5119 ;; shall be answered from the file cache.
5120 ;; We unset `process-file-side-effects' in order to keep the cache
5121 ;; when `process-file' calls appear.
5122 (let (process-file-side-effects)
5123 (tramp-run-real-handler 'vc-registered (list file)))))
5125 ;;;###autoload
5126 (progn (defun tramp-run-real-handler (operation args)
5127 "Invoke normal file name handler for OPERATION.
5128 First arg specifies the OPERATION, second arg is a list of arguments to
5129 pass to the OPERATION."
5130 (let* ((inhibit-file-name-handlers
5131 `(tramp-file-name-handler
5132 tramp-vc-file-name-handler
5133 tramp-completion-file-name-handler
5134 cygwin-mount-name-hook-function
5135 cygwin-mount-map-drive-hook-function
5137 ,(and (eq inhibit-file-name-operation operation)
5138 inhibit-file-name-handlers)))
5139 (inhibit-file-name-operation operation))
5140 (apply operation args))))
5142 ;;;###autoload
5143 (progn (defun tramp-completion-run-real-handler (operation args)
5144 "Invoke `tramp-file-name-handler' for OPERATION.
5145 First arg specifies the OPERATION, second arg is a list of arguments to
5146 pass to the OPERATION."
5147 (let* ((inhibit-file-name-handlers
5148 `(tramp-completion-file-name-handler
5149 cygwin-mount-name-hook-function
5150 cygwin-mount-map-drive-hook-function
5152 ,(and (eq inhibit-file-name-operation operation)
5153 inhibit-file-name-handlers)))
5154 (inhibit-file-name-operation operation))
5155 (apply operation args))))
5157 ;; We handle here all file primitives. Most of them have the file
5158 ;; name as first parameter; nevertheless we check for them explicitly
5159 ;; in order to be signaled if a new primitive appears. This
5160 ;; scenario is needed because there isn't a way to decide by
5161 ;; syntactical means whether a foreign method must be called. It would
5162 ;; ease the life if `file-name-handler-alist' would support a decision
5163 ;; function as well but regexp only.
5164 (defun tramp-file-name-for-operation (operation &rest args)
5165 "Return file name related to OPERATION file primitive.
5166 ARGS are the arguments OPERATION has been called with."
5167 (cond
5168 ; FILE resp DIRECTORY
5169 ((member operation
5170 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
5171 'delete-file 'diff-latest-backup-file 'directory-file-name
5172 'directory-files 'directory-files-and-attributes
5173 'dired-compress-file 'dired-uncache
5174 'file-accessible-directory-p 'file-attributes
5175 'file-directory-p 'file-executable-p 'file-exists-p
5176 'file-local-copy 'file-remote-p 'file-modes
5177 'file-name-as-directory 'file-name-directory
5178 'file-name-nondirectory 'file-name-sans-versions
5179 'file-ownership-preserved-p 'file-readable-p
5180 'file-regular-p 'file-symlink-p 'file-truename
5181 'file-writable-p 'find-backup-file-name 'find-file-noselect
5182 'get-file-buffer 'insert-directory 'insert-file-contents
5183 'load 'make-directory 'make-directory-internal
5184 'set-file-modes 'substitute-in-file-name
5185 'unhandled-file-name-directory 'vc-registered
5186 ; Emacs 22 only
5187 'set-file-times
5188 ; XEmacs only
5189 'abbreviate-file-name 'create-file-buffer
5190 'dired-file-modtime 'dired-make-compressed-filename
5191 'dired-recursive-delete-directory 'dired-set-file-modtime
5192 'dired-shell-unhandle-file-name 'dired-uucode-file
5193 'insert-file-contents-literally 'make-temp-name 'recover-file
5194 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
5195 (if (file-name-absolute-p (nth 0 args))
5196 (nth 0 args)
5197 (expand-file-name (nth 0 args))))
5198 ; FILE DIRECTORY resp FILE1 FILE2
5199 ((member operation
5200 (list 'add-name-to-file 'copy-file 'expand-file-name
5201 'file-name-all-completions 'file-name-completion
5202 'file-newer-than-file-p 'make-symbolic-link 'rename-file
5203 ; Emacs 23 only
5204 'copy-directory
5205 ; XEmacs only
5206 'dired-make-relative-symlink
5207 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
5208 (save-match-data
5209 (cond
5210 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
5211 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
5212 (t (buffer-file-name (current-buffer))))))
5213 ; START END FILE
5214 ((eq operation 'write-region)
5215 (nth 2 args))
5216 ; BUF
5217 ((member operation
5218 (list 'set-visited-file-modtime 'verify-visited-file-modtime
5219 ; since Emacs 22 only
5220 'make-auto-save-file-name
5221 ; XEmacs only
5222 'backup-buffer))
5223 (buffer-file-name
5224 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
5225 ; COMMAND
5226 ((member operation
5227 (list ; not in Emacs 23
5228 'dired-call-process
5229 ; Emacs only
5230 'shell-command
5231 ; since Emacs 22 only
5232 'process-file
5233 ; since Emacs 23 only
5234 'start-file-process
5235 ; XEmacs only
5236 'dired-print-file 'dired-shell-call-process
5237 ; nowhere yet
5238 'executable-find 'start-process 'call-process))
5239 default-directory)
5240 ; unknown file primitive
5241 (t (error "unknown file I/O primitive: %s" operation))))
5243 (defun tramp-find-foreign-file-name-handler (filename)
5244 "Return foreign file name handler if exists."
5245 (when (and (stringp filename) (tramp-tramp-file-p filename))
5246 (let ((v (tramp-dissect-file-name filename t))
5247 (handler tramp-foreign-file-name-handler-alist)
5248 elt res)
5249 ;; When we are not fully sure that filename completion is safe,
5250 ;; we should not return a handler.
5251 (when (or (tramp-file-name-method v) (tramp-file-name-user v)
5252 (and (tramp-file-name-host v)
5253 (not (member (tramp-file-name-host v)
5254 (mapcar 'car tramp-methods))))
5255 (not (tramp-completion-mode-p)))
5256 (while handler
5257 (setq elt (car handler)
5258 handler (cdr handler))
5259 (when (funcall (car elt) filename)
5260 (setq handler nil
5261 res (cdr elt))))
5262 res))))
5264 ;; Main function.
5265 ;;;###autoload
5266 (defun tramp-file-name-handler (operation &rest args)
5267 "Invoke Tramp file name handler.
5268 Falls back to normal file name handler if no Tramp file name handler exists."
5269 (if tramp-mode
5270 (save-match-data
5271 (let* ((filename
5272 (tramp-replace-environment-variables
5273 (apply 'tramp-file-name-for-operation operation args)))
5274 (completion (tramp-completion-mode-p))
5275 (foreign (tramp-find-foreign-file-name-handler filename)))
5276 (with-parsed-tramp-file-name filename nil
5277 (cond
5278 ;; When we are in completion mode, some operations
5279 ;; shouldn't be handled by backend.
5280 ((and completion (zerop (length localname))
5281 (memq operation '(file-exists-p file-directory-p)))
5283 ((and completion (zerop (length localname))
5284 (memq operation '(file-name-as-directory)))
5285 filename)
5286 ;; Call the backend function.
5287 (foreign (apply foreign operation args))
5288 ;; Nothing to do for us.
5289 (t (tramp-run-real-handler operation args))))))
5290 ;; When `tramp-mode' is not enabled, we don't do anything.
5291 (tramp-run-real-handler operation args)))
5293 ;; In Emacs, there is some concurrency due to timers. If a timer
5294 ;; interrupts Tramp and wishes to use the same connection buffer as
5295 ;; the "main" Emacs, then garbage might occur in the connection
5296 ;; buffer. Therefore, we need to make sure that a timer does not use
5297 ;; the same connection buffer as the "main" Emacs. We implement a
5298 ;; cheap global lock, instead of locking each connection buffer
5299 ;; separately. The global lock is based on two variables,
5300 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
5301 ;; (with setq) to indicate a lock. But Tramp also calls itself during
5302 ;; processing of a single file operation, so we need to allow
5303 ;; recursive calls. That's where the `tramp-locker' variable comes in
5304 ;; -- it is let-bound to t during the execution of the current
5305 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
5306 ;; then we should just proceed because we have been called
5307 ;; recursively. But if `tramp-locker' is nil, then we are a timer
5308 ;; interrupting the "main" Emacs, and then we signal an error.
5310 (defvar tramp-locked nil
5311 "If non-nil, then Tramp is currently busy.
5312 Together with `tramp-locker', this implements a locking mechanism
5313 preventing reentrant calls of Tramp.")
5315 (defvar tramp-locker nil
5316 "If non-nil, then a caller has locked Tramp.
5317 Together with `tramp-locked', this implements a locking mechanism
5318 preventing reentrant calls of Tramp.")
5320 (defun tramp-sh-file-name-handler (operation &rest args)
5321 "Invoke remote-shell Tramp file name handler.
5322 Fall back to normal file name handler if no Tramp handler exists."
5323 (when (and tramp-locked (not tramp-locker))
5324 (setq tramp-locked nil)
5325 (signal 'file-error (list "Forbidden reentrant call of Tramp")))
5326 (let ((tl tramp-locked))
5327 (unwind-protect
5328 (progn
5329 (setq tramp-locked t)
5330 (let ((tramp-locker t))
5331 (save-match-data
5332 (let ((fn (assoc operation tramp-file-name-handler-alist)))
5333 (if fn
5334 (apply (cdr fn) args)
5335 (tramp-run-real-handler operation args))))))
5336 (setq tramp-locked tl))))
5338 (defun tramp-vc-file-name-handler (operation &rest args)
5339 "Invoke special file name handler, which collects files to be handled."
5340 (save-match-data
5341 (let ((filename
5342 (tramp-replace-environment-variables
5343 (apply 'tramp-file-name-for-operation operation args)))
5344 (fn (assoc operation tramp-file-name-handler-alist)))
5345 (with-parsed-tramp-file-name filename nil
5346 (cond
5347 ;; That's what we want: file names, for which checks are
5348 ;; applied. We assume, that VC uses only `file-exists-p' and
5349 ;; `file-readable-p' checks; otherwise we must extend the
5350 ;; list. We do not perform any action, but return nil, in
5351 ;; order to keep `vc-registered' running.
5352 ((and fn (memq operation '(file-exists-p file-readable-p)))
5353 (add-to-list 'tramp-vc-registered-file-names localname 'append)
5354 nil)
5355 ;; Tramp file name handlers like `expand-file-name'. They
5356 ;; must still work.
5358 (save-match-data (apply (cdr fn) args)))
5359 ;; Default file name handlers, we don't care.
5360 (t (tramp-run-real-handler operation args)))))))
5362 ;;;###autoload
5363 (progn (defun tramp-completion-file-name-handler (operation &rest args)
5364 "Invoke Tramp file name completion handler.
5365 Falls back to normal file name handler if no Tramp file name handler exists."
5366 ;; We bind `directory-sep-char' here for XEmacs on Windows, which
5367 ;; would otherwise use backslash.
5368 (let ((directory-sep-char ?/)
5369 (fn (assoc operation tramp-completion-file-name-handler-alist)))
5370 (if (and
5371 ;; When `tramp-mode' is not enabled, we don't do anything.
5372 fn tramp-mode
5373 ;; For other syntaxes than `sep', the regexp matches many common
5374 ;; situations where the user doesn't actually want to use Tramp.
5375 ;; So to avoid autoloading Tramp after typing just "/s", we
5376 ;; disable this part of the completion, unless the user implicitly
5377 ;; indicated his interest in using a fancier completion system.
5378 (or (eq tramp-syntax 'sep)
5379 (featurep 'tramp) ; If it's loaded, we may as well use it.
5380 (and (boundp 'partial-completion-mode) partial-completion-mode)
5381 ;; FIXME: These may have been loaded even if the user never
5382 ;; intended to use them.
5383 (featurep 'ido)
5384 (featurep 'icicles)))
5385 (save-match-data (apply (cdr fn) args))
5386 (tramp-completion-run-real-handler operation args)))))
5388 ;;;###autoload
5389 (progn (defun tramp-register-file-name-handlers ()
5390 "Add Tramp file name handlers to `file-name-handler-alist'."
5391 ;; Remove autoloaded handlers from file name handler alist. Useful,
5392 ;; if `tramp-syntax' has been changed.
5393 (let ((a1 (rassq 'tramp-file-name-handler file-name-handler-alist)))
5394 (setq file-name-handler-alist (delq a1 file-name-handler-alist)))
5395 (let ((a1 (rassq
5396 'tramp-completion-file-name-handler file-name-handler-alist)))
5397 (setq file-name-handler-alist (delq a1 file-name-handler-alist)))
5398 ;; Add the handlers.
5399 (add-to-list 'file-name-handler-alist
5400 (cons tramp-file-name-regexp 'tramp-file-name-handler))
5401 (add-to-list 'file-name-handler-alist
5402 (cons tramp-completion-file-name-regexp
5403 'tramp-completion-file-name-handler))
5404 (put 'tramp-completion-file-name-handler 'safe-magic t)
5405 ;; If jka-compr or epa-file are already loaded, move them to the
5406 ;; front of `file-name-handler-alist'.
5407 (dolist (fnh '(epa-file-handler jka-compr-handler))
5408 (let ((entry (rassoc fnh file-name-handler-alist)))
5409 (when entry
5410 (setq file-name-handler-alist
5411 (cons entry (delete entry file-name-handler-alist))))))))
5413 ;; `tramp-file-name-handler' must be registered before evaluation of
5414 ;; site-start and init files, because there might exist remote files
5415 ;; already, f.e. files kept via recentf-mode.
5416 ;;;###autoload(tramp-register-file-name-handlers)
5417 (tramp-register-file-name-handlers)
5419 ;;;###autoload
5420 (defun tramp-unload-file-name-handlers ()
5421 (setq file-name-handler-alist
5422 (delete (rassoc 'tramp-file-name-handler
5423 file-name-handler-alist)
5424 (delete (rassoc 'tramp-completion-file-name-handler
5425 file-name-handler-alist)
5426 file-name-handler-alist))))
5428 (add-hook 'tramp-unload-hook 'tramp-unload-file-name-handlers)
5430 ;;; File name handler functions for completion mode:
5432 (defvar tramp-completion-mode nil
5433 "If non-nil, external packages signal that they are in file name completion.
5435 This is necessary, because Tramp uses a heuristic depending on last
5436 input event. This fails when external packages use other characters
5437 but <TAB>, <SPACE> or ?\\? for file name completion. This variable
5438 should never be set globally, the intention is to let-bind it.")
5440 ;; Necessary because `tramp-file-name-regexp-unified' and
5441 ;; `tramp-completion-file-name-regexp-unified' aren't different. If
5442 ;; nil, `tramp-completion-run-real-handler' is called (i.e. forwarding
5443 ;; to `tramp-file-name-handler'). Otherwise, it takes
5444 ;; `tramp-run-real-handler'. Using `last-input-event' is a little bit
5445 ;; risky, because completing a file might require loading other files,
5446 ;; like "~/.netrc", and for them it shouldn't be decided based on that
5447 ;; variable. On the other hand, those files shouldn't have partial
5448 ;; Tramp file name syntax. Maybe another variable should be introduced
5449 ;; overwriting this check in such cases. Or we change Tramp file name
5450 ;; syntax in order to avoid ambiguities, like in XEmacs ...
5451 (defun tramp-completion-mode-p ()
5452 "Checks whether method / user name / host name completion is active."
5454 ;; Signal from outside.
5455 tramp-completion-mode
5456 ;; Emacs.
5457 (equal last-input-event 'tab)
5458 (and (natnump last-input-event)
5460 ;; ?\t has event-modifier 'control.
5461 (equal last-input-event ?\t)
5462 (and (not (event-modifiers last-input-event))
5463 (or (equal last-input-event ?\?)
5464 (equal last-input-event ?\ )))))
5465 ;; XEmacs.
5466 (and (featurep 'xemacs)
5467 ;; `last-input-event' might be nil.
5468 (not (null last-input-event))
5469 ;; `last-input-event' may have no character approximation.
5470 (funcall (symbol-function 'event-to-character) last-input-event)
5472 ;; ?\t has event-modifier 'control.
5473 (equal
5474 (funcall (symbol-function 'event-to-character)
5475 last-input-event) ?\t)
5476 (and (not (event-modifiers last-input-event))
5477 (or (equal
5478 (funcall (symbol-function 'event-to-character)
5479 last-input-event) ?\?)
5480 (equal
5481 (funcall (symbol-function 'event-to-character)
5482 last-input-event) ?\ )))))))
5484 ;; Method, host name and user name completion.
5485 ;; `tramp-completion-dissect-file-name' returns a list of
5486 ;; tramp-file-name structures. For all of them we return possible completions.
5487 ;;;###autoload
5488 (defun tramp-completion-handle-file-name-all-completions (filename directory)
5489 "Like `file-name-all-completions' for partial Tramp files."
5491 (let* ((fullname (tramp-drop-volume-letter
5492 (expand-file-name filename directory)))
5493 ;; Possible completion structures.
5494 (v (tramp-completion-dissect-file-name fullname))
5495 result result1)
5497 (while v
5498 (let* ((car (car v))
5499 (method (tramp-file-name-method car))
5500 (user (tramp-file-name-user car))
5501 (host (tramp-file-name-host car))
5502 (localname (tramp-file-name-localname car))
5503 (m (tramp-find-method method user host))
5504 (tramp-current-user user) ; see `tramp-parse-passwd'
5505 all-user-hosts)
5507 (unless localname ;; Nothing to complete.
5509 (if (or user host)
5511 ;; Method dependent user / host combinations.
5512 (progn
5513 (mapc
5514 (lambda (x)
5515 (setq all-user-hosts
5516 (append all-user-hosts
5517 (funcall (nth 0 x) (nth 1 x)))))
5518 (tramp-get-completion-function m))
5520 (setq result
5521 (append result
5522 (mapcar
5523 (lambda (x)
5524 (tramp-get-completion-user-host
5525 method user host (nth 0 x) (nth 1 x)))
5526 (delq nil all-user-hosts)))))
5528 ;; Possible methods.
5529 (setq result
5530 (append result (tramp-get-completion-methods m)))))
5532 (setq v (cdr v))))
5534 ;; Unify list, remove nil elements.
5535 (while result
5536 (let ((car (car result)))
5537 (when car
5538 (add-to-list
5539 'result1
5540 (substring car (length (tramp-drop-volume-letter directory)))))
5541 (setq result (cdr result))))
5543 ;; Complete local parts.
5544 (append
5545 result1
5546 (condition-case nil
5547 (tramp-completion-run-real-handler
5548 'file-name-all-completions (list filename directory))
5549 (error nil)))))
5551 ;; Method, host name and user name completion for a file.
5552 ;;;###autoload
5553 (defun tramp-completion-handle-file-name-completion
5554 (filename directory &optional predicate)
5555 "Like `file-name-completion' for Tramp files."
5556 (try-completion
5557 filename
5558 (mapcar 'list (file-name-all-completions filename directory))
5559 (when predicate
5560 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
5562 ;; I misuse a little bit the tramp-file-name structure in order to handle
5563 ;; completion possibilities for partial methods / user names / host names.
5564 ;; Return value is a list of tramp-file-name structures according to possible
5565 ;; completions. If "localname" is non-nil it means there
5566 ;; shouldn't be a completion anymore.
5568 ;; Expected results:
5570 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
5571 ;; [nil nil "x" nil] [nil "x" nil nil] [nil "x" "y" nil]
5572 ;; [nil "x" nil nil]
5573 ;; ["x" nil nil nil]
5575 ;; "/x:" "/x:y" "/x:y:"
5576 ;; [nil nil "x" ""] [nil nil "x" "y"] ["x" nil "y" ""]
5577 ;; "/[x/" "/[x/y"
5578 ;; ["x" nil "" nil] ["x" nil "y" nil]
5579 ;; ["x" "" nil nil] ["x" "y" nil nil]
5581 ;; "/x:y@" "/x:y@z" "/x:y@z:"
5582 ;; [nil nil "x" "y@"] [nil nil "x" "y@z"] ["x" "y" "z" ""]
5583 ;; "/[x/y@" "/[x/y@z"
5584 ;; ["x" nil "y" nil] ["x" "y" "z" nil]
5585 (defun tramp-completion-dissect-file-name (name)
5586 "Returns a list of `tramp-file-name' structures.
5587 They are collected by `tramp-completion-dissect-file-name1'."
5589 (let* ((result)
5590 (x-nil "\\|\\(\\)")
5591 (tramp-completion-ipv6-regexp
5592 (format
5593 "[^%s]*"
5594 (if (zerop (length tramp-postfix-ipv6-format))
5595 tramp-postfix-host-format
5596 tramp-postfix-ipv6-format)))
5597 ;; "/method" "/[method"
5598 (tramp-completion-file-name-structure1
5599 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
5600 1 nil nil nil))
5601 ;; "/user" "/[user"
5602 (tramp-completion-file-name-structure2
5603 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
5604 nil 1 nil nil))
5605 ;; "/host" "/[host"
5606 (tramp-completion-file-name-structure3
5607 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
5608 nil nil 1 nil))
5609 ;; "/[ipv6" "/[ipv6"
5610 (tramp-completion-file-name-structure4
5611 (list (concat tramp-prefix-regexp
5612 tramp-prefix-ipv6-regexp
5613 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5614 nil nil 1 nil))
5615 ;; "/user@host" "/[user@host"
5616 (tramp-completion-file-name-structure5
5617 (list (concat tramp-prefix-regexp
5618 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5619 "\\(" tramp-host-regexp x-nil "\\)$")
5620 nil 1 2 nil))
5621 ;; "/user@[ipv6" "/[user@ipv6"
5622 (tramp-completion-file-name-structure6
5623 (list (concat tramp-prefix-regexp
5624 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5625 tramp-prefix-ipv6-regexp
5626 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5627 nil 1 2 nil))
5628 ;; "/method:user" "/[method/user" "/method://user"
5629 (tramp-completion-file-name-structure7
5630 (list (concat tramp-prefix-regexp
5631 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5632 "\\(" tramp-user-regexp x-nil "\\)$")
5633 1 2 nil nil))
5634 ;; "/method:host" "/[method/host" "/method://host"
5635 (tramp-completion-file-name-structure8
5636 (list (concat tramp-prefix-regexp
5637 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5638 "\\(" tramp-host-regexp x-nil "\\)$")
5639 1 nil 2 nil))
5640 ;; "/method:[ipv6" "/[method/ipv6" "/method://[ipv6"
5641 (tramp-completion-file-name-structure9
5642 (list (concat tramp-prefix-regexp
5643 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5644 tramp-prefix-ipv6-regexp
5645 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5646 1 nil 2 nil))
5647 ;; "/method:user@host" "/[method/user@host" "/method://user@host"
5648 (tramp-completion-file-name-structure10
5649 (list (concat tramp-prefix-regexp
5650 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5651 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5652 "\\(" tramp-host-regexp x-nil "\\)$")
5653 1 2 3 nil))
5654 ;; "/method:user@[ipv6" "/[method/user@ipv6" "/method://user@[ipv6"
5655 (tramp-completion-file-name-structure11
5656 (list (concat tramp-prefix-regexp
5657 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5658 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5659 tramp-prefix-ipv6-regexp
5660 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5661 1 2 3 nil))
5662 ;; "/method: "/method:/"
5663 (tramp-completion-file-name-structure12
5664 (list
5665 (if (equal tramp-syntax 'url)
5666 (concat tramp-prefix-regexp
5667 "\\(" tramp-method-regexp "\\)"
5668 "\\(" (substring tramp-postfix-method-regexp 0 1)
5669 "\\|" (substring tramp-postfix-method-regexp 1 2) "\\)"
5670 "\\(" "\\)$")
5671 ;; Should not match if not URL syntax.
5672 (concat tramp-prefix-regexp "/$"))
5673 1 3 nil nil))
5674 ;; "/method: "/method:/"
5675 (tramp-completion-file-name-structure13
5676 (list
5677 (if (equal tramp-syntax 'url)
5678 (concat tramp-prefix-regexp
5679 "\\(" tramp-method-regexp "\\)"
5680 "\\(" (substring tramp-postfix-method-regexp 0 1)
5681 "\\|" (substring tramp-postfix-method-regexp 1 2) "\\)"
5682 "\\(" "\\)$")
5683 ;; Should not match if not URL syntax.
5684 (concat tramp-prefix-regexp "/$"))
5685 1 nil 3 nil)))
5687 (mapc (lambda (regexp)
5688 (add-to-list 'result
5689 (tramp-completion-dissect-file-name1 regexp name)))
5690 (list
5691 tramp-completion-file-name-structure1
5692 tramp-completion-file-name-structure2
5693 tramp-completion-file-name-structure3
5694 tramp-completion-file-name-structure4
5695 tramp-completion-file-name-structure5
5696 tramp-completion-file-name-structure6
5697 tramp-completion-file-name-structure7
5698 tramp-completion-file-name-structure8
5699 tramp-completion-file-name-structure9
5700 tramp-completion-file-name-structure10
5701 tramp-completion-file-name-structure11
5702 tramp-completion-file-name-structure12
5703 tramp-completion-file-name-structure13
5704 tramp-file-name-structure))
5706 (delq nil result)))
5708 (defun tramp-completion-dissect-file-name1 (structure name)
5709 "Returns a `tramp-file-name' structure matching STRUCTURE.
5710 The structure consists of remote method, remote user,
5711 remote host and localname (filename on remote host)."
5713 (save-match-data
5714 (when (string-match (nth 0 structure) name)
5715 (let ((method (and (nth 1 structure)
5716 (match-string (nth 1 structure) name)))
5717 (user (and (nth 2 structure)
5718 (match-string (nth 2 structure) name)))
5719 (host (and (nth 3 structure)
5720 (match-string (nth 3 structure) name)))
5721 (localname (and (nth 4 structure)
5722 (match-string (nth 4 structure) name))))
5723 (vector method user host localname)))))
5725 ;; This function returns all possible method completions, adding the
5726 ;; trailing method delimeter.
5727 (defun tramp-get-completion-methods (partial-method)
5728 "Returns all method completions for PARTIAL-METHOD."
5729 (mapcar
5730 (lambda (method)
5731 (and method
5732 (string-match (concat "^" (regexp-quote partial-method)) method)
5733 (tramp-completion-make-tramp-file-name method nil nil nil)))
5734 (mapcar 'car tramp-methods)))
5736 ;; Compares partial user and host names with possible completions.
5737 (defun tramp-get-completion-user-host (method partial-user partial-host user host)
5738 "Returns the most expanded string for user and host name completion.
5739 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
5740 (cond
5742 ((and partial-user partial-host)
5743 (if (and host
5744 (string-match (concat "^" (regexp-quote partial-host)) host)
5745 (string-equal partial-user (or user partial-user)))
5746 (setq user partial-user)
5747 (setq user nil
5748 host nil)))
5750 (partial-user
5751 (setq host nil)
5752 (unless
5753 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
5754 (setq user nil)))
5756 (partial-host
5757 (setq user nil)
5758 (unless
5759 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
5760 (setq host nil)))
5762 (t (setq user nil
5763 host nil)))
5765 (unless (zerop (+ (length user) (length host)))
5766 (tramp-completion-make-tramp-file-name method user host nil)))
5768 (defun tramp-parse-rhosts (filename)
5769 "Return a list of (user host) tuples allowed to access.
5770 Either user or host may be nil."
5771 ;; On Windows, there are problems in completion when
5772 ;; `default-directory' is remote.
5773 (let ((default-directory (tramp-compat-temporary-file-directory))
5774 res)
5775 (when (file-readable-p filename)
5776 (with-temp-buffer
5777 (insert-file-contents filename)
5778 (goto-char (point-min))
5779 (while (not (eobp))
5780 (push (tramp-parse-rhosts-group) res))))
5781 res))
5783 (defun tramp-parse-rhosts-group ()
5784 "Return a (user host) tuple allowed to access.
5785 Either user or host may be nil."
5786 (let ((result)
5787 (regexp
5788 (concat
5789 "^\\(" tramp-host-regexp "\\)"
5790 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
5791 (narrow-to-region (point) (tramp-compat-line-end-position))
5792 (when (re-search-forward regexp nil t)
5793 (setq result (append (list (match-string 3) (match-string 1)))))
5794 (widen)
5795 (forward-line 1)
5796 result))
5798 (defun tramp-parse-shosts (filename)
5799 "Return a list of (user host) tuples allowed to access.
5800 User is always nil."
5801 ;; On Windows, there are problems in completion when
5802 ;; `default-directory' is remote.
5803 (let ((default-directory (tramp-compat-temporary-file-directory))
5804 res)
5805 (when (file-readable-p filename)
5806 (with-temp-buffer
5807 (insert-file-contents filename)
5808 (goto-char (point-min))
5809 (while (not (eobp))
5810 (push (tramp-parse-shosts-group) res))))
5811 res))
5813 (defun tramp-parse-shosts-group ()
5814 "Return a (user host) tuple allowed to access.
5815 User is always nil."
5816 (let ((result)
5817 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
5818 (narrow-to-region (point) (tramp-compat-line-end-position))
5819 (when (re-search-forward regexp nil t)
5820 (setq result (list nil (match-string 1))))
5821 (widen)
5823 (> (skip-chars-forward ",") 0)
5824 (forward-line 1))
5825 result))
5827 (defun tramp-parse-sconfig (filename)
5828 "Return a list of (user host) tuples allowed to access.
5829 User is always nil."
5830 ;; On Windows, there are problems in completion when
5831 ;; `default-directory' is remote.
5832 (let ((default-directory (tramp-compat-temporary-file-directory))
5833 res)
5834 (when (file-readable-p filename)
5835 (with-temp-buffer
5836 (insert-file-contents filename)
5837 (goto-char (point-min))
5838 (while (not (eobp))
5839 (push (tramp-parse-sconfig-group) res))))
5840 res))
5842 (defun tramp-parse-sconfig-group ()
5843 "Return a (user host) tuple allowed to access.
5844 User is always nil."
5845 (let ((result)
5846 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
5847 (narrow-to-region (point) (tramp-compat-line-end-position))
5848 (when (re-search-forward regexp nil t)
5849 (setq result (list nil (match-string 1))))
5850 (widen)
5852 (> (skip-chars-forward ",") 0)
5853 (forward-line 1))
5854 result))
5856 (defun tramp-parse-shostkeys (dirname)
5857 "Return a list of (user host) tuples allowed to access.
5858 User is always nil."
5859 ;; On Windows, there are problems in completion when
5860 ;; `default-directory' is remote.
5861 (let* ((default-directory (tramp-compat-temporary-file-directory))
5862 (regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
5863 (files (when (file-directory-p dirname) (directory-files dirname)))
5864 result)
5865 (while files
5866 (when (string-match regexp (car files))
5867 (push (list nil (match-string 1 (car files))) result))
5868 (setq files (cdr files)))
5869 result))
5871 (defun tramp-parse-sknownhosts (dirname)
5872 "Return a list of (user host) tuples allowed to access.
5873 User is always nil."
5874 ;; On Windows, there are problems in completion when
5875 ;; `default-directory' is remote.
5876 (let* ((default-directory (tramp-compat-temporary-file-directory))
5877 (regexp (concat "^\\(" tramp-host-regexp
5878 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
5879 (files (when (file-directory-p dirname) (directory-files dirname)))
5880 result)
5881 (while files
5882 (when (string-match regexp (car files))
5883 (push (list nil (match-string 1 (car files))) result))
5884 (setq files (cdr files)))
5885 result))
5887 (defun tramp-parse-hosts (filename)
5888 "Return a list of (user host) tuples allowed to access.
5889 User is always nil."
5890 ;; On Windows, there are problems in completion when
5891 ;; `default-directory' is remote.
5892 (let ((default-directory (tramp-compat-temporary-file-directory))
5893 res)
5894 (when (file-readable-p filename)
5895 (with-temp-buffer
5896 (insert-file-contents filename)
5897 (goto-char (point-min))
5898 (while (not (eobp))
5899 (push (tramp-parse-hosts-group) res))))
5900 res))
5902 (defun tramp-parse-hosts-group ()
5903 "Return a (user host) tuple allowed to access.
5904 User is always nil."
5905 (let ((result)
5906 (regexp
5907 (concat "^\\(" tramp-ipv6-regexp "\\|" tramp-host-regexp "\\)")))
5908 (narrow-to-region (point) (tramp-compat-line-end-position))
5909 (when (re-search-forward regexp nil t)
5910 (setq result (list nil (match-string 1))))
5911 (widen)
5913 (> (skip-chars-forward " \t") 0)
5914 (forward-line 1))
5915 result))
5917 ;; For su-alike methods it would be desirable to return "root@localhost"
5918 ;; as default. Unfortunately, we have no information whether any user name
5919 ;; has been typed already. So we use `tramp-current-user' as indication,
5920 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
5921 (defun tramp-parse-passwd (filename)
5922 "Return a list of (user host) tuples allowed to access.
5923 Host is always \"localhost\"."
5924 ;; On Windows, there are problems in completion when
5925 ;; `default-directory' is remote.
5926 (let ((default-directory (tramp-compat-temporary-file-directory))
5927 res)
5928 (if (zerop (length tramp-current-user))
5929 '(("root" nil))
5930 (when (file-readable-p filename)
5931 (with-temp-buffer
5932 (insert-file-contents filename)
5933 (goto-char (point-min))
5934 (while (not (eobp))
5935 (push (tramp-parse-passwd-group) res))))
5936 res)))
5938 (defun tramp-parse-passwd-group ()
5939 "Return a (user host) tuple allowed to access.
5940 Host is always \"localhost\"."
5941 (let ((result)
5942 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
5943 (narrow-to-region (point) (tramp-compat-line-end-position))
5944 (when (re-search-forward regexp nil t)
5945 (setq result (list (match-string 1) "localhost")))
5946 (widen)
5947 (forward-line 1)
5948 result))
5950 (defun tramp-parse-netrc (filename)
5951 "Return a list of (user host) tuples allowed to access.
5952 User may be nil."
5953 ;; On Windows, there are problems in completion when
5954 ;; `default-directory' is remote.
5955 (let ((default-directory (tramp-compat-temporary-file-directory))
5956 res)
5957 (when (file-readable-p filename)
5958 (with-temp-buffer
5959 (insert-file-contents filename)
5960 (goto-char (point-min))
5961 (while (not (eobp))
5962 (push (tramp-parse-netrc-group) res))))
5963 res))
5965 (defun tramp-parse-netrc-group ()
5966 "Return a (user host) tuple allowed to access.
5967 User may be nil."
5968 (let ((result)
5969 (regexp
5970 (concat
5971 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
5972 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
5973 (narrow-to-region (point) (tramp-compat-line-end-position))
5974 (when (re-search-forward regexp nil t)
5975 (setq result (list (match-string 3) (match-string 1))))
5976 (widen)
5977 (forward-line 1)
5978 result))
5980 (defun tramp-parse-putty (registry)
5981 "Return a list of (user host) tuples allowed to access.
5982 User is always nil."
5983 ;; On Windows, there are problems in completion when
5984 ;; `default-directory' is remote.
5985 (let ((default-directory (tramp-compat-temporary-file-directory))
5986 res)
5987 (with-temp-buffer
5988 (when (zerop (tramp-local-call-process "reg" nil t nil "query" registry))
5989 (goto-char (point-min))
5990 (while (not (eobp))
5991 (push (tramp-parse-putty-group registry) res))))
5992 res))
5994 (defun tramp-parse-putty-group (registry)
5995 "Return a (user host) tuple allowed to access.
5996 User is always nil."
5997 (let ((result)
5998 (regexp (concat (regexp-quote registry) "\\\\\\(.+\\)")))
5999 (narrow-to-region (point) (tramp-compat-line-end-position))
6000 (when (re-search-forward regexp nil t)
6001 (setq result (list nil (match-string 1))))
6002 (widen)
6003 (forward-line 1)
6004 result))
6006 ;;; Internal Functions:
6008 (defun tramp-maybe-send-script (vec script name)
6009 "Define in remote shell function NAME implemented as SCRIPT.
6010 Only send the definition if it has not already been done."
6011 (let* ((p (tramp-get-connection-process vec))
6012 (scripts (tramp-get-connection-property p "scripts" nil)))
6013 (unless (member name scripts)
6014 (tramp-message vec 5 "Sending script `%s'..." name)
6015 ;; The script could contain a call of Perl. This is masked with `%s'.
6016 (tramp-send-command-and-check
6018 (format "%s () {\n%s\n}" name
6019 (format script (tramp-get-remote-perl vec))))
6020 (tramp-set-connection-property p "scripts" (cons name scripts))
6021 (tramp-message vec 5 "Sending script `%s'...done." name))))
6023 (defun tramp-set-auto-save ()
6024 (when (and ;; ange-ftp has its own auto-save mechanism
6025 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
6026 'tramp-sh-file-name-handler)
6027 auto-save-default)
6028 (auto-save-mode 1)))
6029 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
6030 (add-hook 'tramp-unload-hook
6031 (lambda ()
6032 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
6034 (defun tramp-run-test (switch filename)
6035 "Run `test' on the remote system, given a SWITCH and a FILENAME.
6036 Returns the exit code of the `test' program."
6037 (with-parsed-tramp-file-name filename nil
6038 (tramp-send-command-and-check
6040 (format
6041 "%s %s %s"
6042 (tramp-get-test-command v)
6043 switch
6044 (tramp-shell-quote-argument localname)))))
6046 (defun tramp-run-test2 (format-string file1 file2)
6047 "Run `test'-like program on the remote system, given FILE1, FILE2.
6048 FORMAT-STRING contains the program name, switches, and place holders.
6049 Returns the exit code of the `test' program. Barfs if the methods,
6050 hosts, or files, disagree."
6051 (unless (tramp-equal-remote file1 file2)
6052 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
6053 (tramp-error
6054 v 'file-error
6055 "tramp-run-test2 only implemented for same method, user, host")))
6056 (with-parsed-tramp-file-name file1 v1
6057 (with-parsed-tramp-file-name file1 v2
6058 (tramp-send-command-and-check
6060 (format format-string
6061 (tramp-shell-quote-argument v1-localname)
6062 (tramp-shell-quote-argument v2-localname))))))
6064 (defun tramp-buffer-name (vec)
6065 "A name for the connection buffer VEC."
6066 ;; We must use `tramp-file-name-real-host', because for gateway
6067 ;; methods the default port will be expanded later on, which would
6068 ;; tamper the name.
6069 (let ((method (tramp-file-name-method vec))
6070 (user (tramp-file-name-user vec))
6071 (host (tramp-file-name-real-host vec)))
6072 (if (not (zerop (length user)))
6073 (format "*tramp/%s %s@%s*" method user host)
6074 (format "*tramp/%s %s*" method host))))
6076 (defun tramp-delete-temp-file-function ()
6077 "Remove temporary files related to current buffer."
6078 (when (stringp tramp-temp-buffer-file-name)
6079 (condition-case nil
6080 (delete-file tramp-temp-buffer-file-name)
6081 (error nil))))
6083 (add-hook 'kill-buffer-hook 'tramp-delete-temp-file-function)
6084 (add-hook 'tramp-cache-unload-hook
6085 (lambda ()
6086 (remove-hook 'kill-buffer-hook
6087 'tramp-delete-temp-file-function)))
6089 (defun tramp-get-buffer (vec)
6090 "Get the connection buffer to be used for VEC."
6091 (or (get-buffer (tramp-buffer-name vec))
6092 (with-current-buffer (get-buffer-create (tramp-buffer-name vec))
6093 (setq buffer-undo-list t)
6094 (setq default-directory
6095 (tramp-make-tramp-file-name
6096 (tramp-file-name-method vec)
6097 (tramp-file-name-user vec)
6098 (tramp-file-name-host vec)
6099 "/"))
6100 (current-buffer))))
6102 (defun tramp-get-connection-buffer (vec)
6103 "Get the connection buffer to be used for VEC.
6104 In case a second asynchronous communication has been started, it is different
6105 from `tramp-get-buffer'."
6106 (or (tramp-get-connection-property vec "process-buffer" nil)
6107 (tramp-get-buffer vec)))
6109 (defun tramp-get-connection-process (vec)
6110 "Get the connection process to be used for VEC.
6111 In case a second asynchronous communication has been started, it is different
6112 from the default one."
6113 (get-process
6114 (or (tramp-get-connection-property vec "process-name" nil)
6115 (tramp-buffer-name vec))))
6117 (defun tramp-debug-buffer-name (vec)
6118 "A name for the debug buffer for VEC."
6119 ;; We must use `tramp-file-name-real-host', because for gateway
6120 ;; methods the default port will be expanded later on, which would
6121 ;; tamper the name.
6122 (let ((method (tramp-file-name-method vec))
6123 (user (tramp-file-name-user vec))
6124 (host (tramp-file-name-real-host vec)))
6125 (if (not (zerop (length user)))
6126 (format "*debug tramp/%s %s@%s*" method user host)
6127 (format "*debug tramp/%s %s*" method host))))
6129 (defun tramp-get-debug-buffer (vec)
6130 "Get the debug buffer for VEC."
6131 (with-current-buffer
6132 (get-buffer-create (tramp-debug-buffer-name vec))
6133 (when (bobp)
6134 (setq buffer-undo-list t)
6135 ;; Activate outline-mode. This runs `text-mode-hook' and
6136 ;; `outline-mode-hook'. We must prevent that local processes
6137 ;; die. Yes: I've seen `flyspell-mode', which starts "ispell"
6138 ;; ...
6139 (let ((default-directory (tramp-compat-temporary-file-directory)))
6140 (outline-mode))
6141 (set (make-local-variable 'outline-regexp)
6142 "[0-9]+:[0-9]+:[0-9]+\\.[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #")
6143 ; (set (make-local-variable 'outline-regexp)
6144 ; "[a-z.-]+:[0-9]+: [a-z0-9-]+ (\\([0-9]+\\)) #")
6145 (set (make-local-variable 'outline-level) 'tramp-outline-level))
6146 (current-buffer)))
6148 (defun tramp-outline-level ()
6149 "Return the depth to which a statement is nested in the outline.
6150 Point must be at the beginning of a header line.
6152 The outline level is equal to the verbosity of the Tramp message."
6153 (1+ (string-to-number (match-string 1))))
6155 (defun tramp-find-executable
6156 (vec progname dirlist &optional ignore-tilde ignore-path)
6157 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
6158 First arg VEC specifies the connection, PROGNAME is the program
6159 to search for, and DIRLIST gives the list of directories to
6160 search. If IGNORE-TILDE is non-nil, directory names starting
6161 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
6162 only in DIRLIST.
6164 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
6166 This function expects to be in the right *tramp* buffer."
6167 (with-current-buffer (tramp-get-buffer vec)
6168 (let (result)
6169 ;; Check whether the executable is in $PATH. "which(1)" does not
6170 ;; report always a correct error code; therefore we check the
6171 ;; number of words it returns.
6172 (unless ignore-path
6173 (tramp-send-command vec (format "which \\%s | wc -w" progname))
6174 (goto-char (point-min))
6175 (if (looking-at "^1$")
6176 (setq result (concat "\\" progname))))
6177 (unless result
6178 (when ignore-tilde
6179 ;; Remove all ~/foo directories from dirlist. In Emacs 20,
6180 ;; `remove' is in CL, and we want to avoid CL dependencies.
6181 (let (newdl d)
6182 (while dirlist
6183 (setq d (car dirlist))
6184 (setq dirlist (cdr dirlist))
6185 (unless (char-equal ?~ (aref d 0))
6186 (setq newdl (cons d newdl))))
6187 (setq dirlist (nreverse newdl))))
6188 (tramp-send-command
6190 (format (concat "while read d; "
6191 "do if test -x $d/%s -a -f $d/%s; "
6192 "then echo tramp_executable $d/%s; "
6193 "break; fi; done <<'EOF'\n"
6194 "%s\nEOF")
6195 progname progname progname (mapconcat 'identity dirlist "\n")))
6196 (goto-char (point-max))
6197 (when (search-backward "tramp_executable " nil t)
6198 (skip-chars-forward "^ ")
6199 (skip-chars-forward " ")
6200 (setq result (buffer-substring
6201 (point) (tramp-compat-line-end-position)))))
6202 result)))
6204 (defun tramp-set-remote-path (vec)
6205 "Sets the remote environment PATH to existing directories.
6206 I.e., for each directory in `tramp-remote-path', it is tested
6207 whether it exists and if so, it is added to the environment
6208 variable PATH."
6209 (tramp-message vec 5 (format "Setting $PATH environment variable"))
6210 (tramp-send-command
6211 vec (format "PATH=%s; export PATH"
6212 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
6214 ;; ------------------------------------------------------------
6215 ;; -- Communication with external shell --
6216 ;; ------------------------------------------------------------
6218 (defun tramp-find-file-exists-command (vec)
6219 "Find a command on the remote host for checking if a file exists.
6220 Here, we are looking for a command which has zero exit status if the
6221 file exists and nonzero exit status otherwise."
6222 (let ((existing "/")
6223 (nonexisting
6224 (tramp-shell-quote-argument "/ this file does not exist "))
6225 result)
6226 ;; The algorithm is as follows: we try a list of several commands.
6227 ;; For each command, we first run `$cmd /' -- this should return
6228 ;; true, as the root directory always exists. And then we run
6229 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
6230 ;; does not exist. This should return false. We use the first
6231 ;; command we find that seems to work.
6232 ;; The list of commands to try is as follows:
6233 ;; `ls -d' This works on most systems, but NetBSD 1.4
6234 ;; has a bug: `ls' always returns zero exit
6235 ;; status, even for files which don't exist.
6236 ;; `test -e' Some Bourne shells have a `test' builtin
6237 ;; which does not know the `-e' option.
6238 ;; `/bin/test -e' For those, the `test' binary on disk normally
6239 ;; provides the option. Alas, the binary
6240 ;; is sometimes `/bin/test' and sometimes it's
6241 ;; `/usr/bin/test'.
6242 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
6243 (unless (or
6244 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
6245 (zerop (tramp-send-command-and-check
6246 vec (format "%s %s" result existing)))
6247 (not (zerop (tramp-send-command-and-check
6248 vec (format "%s %s" result nonexisting)))))
6249 (and (setq result "/bin/test -e")
6250 (zerop (tramp-send-command-and-check
6251 vec (format "%s %s" result existing)))
6252 (not (zerop (tramp-send-command-and-check
6253 vec (format "%s %s" result nonexisting)))))
6254 (and (setq result "/usr/bin/test -e")
6255 (zerop (tramp-send-command-and-check
6256 vec (format "%s %s" result existing)))
6257 (not (zerop (tramp-send-command-and-check
6258 vec (format "%s %s" result nonexisting)))))
6259 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
6260 (zerop (tramp-send-command-and-check
6261 vec (format "%s %s" result existing)))
6262 (not (zerop (tramp-send-command-and-check
6263 vec (format "%s %s" result nonexisting))))))
6264 (tramp-error
6265 vec 'file-error "Couldn't find command to check if file exists"))
6266 result))
6268 ;; CCC test ksh or bash found for tilde expansion?
6269 (defun tramp-find-shell (vec)
6270 "Opens a shell on the remote host which groks tilde expansion."
6271 (unless (tramp-get-connection-property vec "remote-shell" nil)
6272 (let (shell)
6273 (with-current-buffer (tramp-get-buffer vec)
6274 (tramp-send-command vec "echo ~root" t)
6275 (cond
6276 ((string-match "^~root$" (buffer-string))
6277 (setq shell
6278 (or (tramp-find-executable
6279 vec "bash" (tramp-get-remote-path vec) t)
6280 (tramp-find-executable
6281 vec "ksh" (tramp-get-remote-path vec) t)))
6282 (unless shell
6283 (tramp-error
6284 vec 'file-error
6285 "Couldn't find a shell which groks tilde expansion"))
6286 ;; Find arguments for this shell.
6287 (let ((alist tramp-sh-extra-args)
6288 item extra-args)
6289 (while (and alist (null extra-args))
6290 (setq item (pop alist))
6291 (when (string-match (car item) shell)
6292 (setq extra-args (cdr item))))
6293 (when extra-args (setq shell (concat shell " " extra-args))))
6294 (tramp-message
6295 vec 5 "Starting remote shell `%s' for tilde expansion..." shell)
6296 (let ((tramp-end-of-output "$ "))
6297 (tramp-send-command
6299 (format "PROMPT_COMMAND='' PS1=%s PS2='' PS3='' exec %s"
6300 (shell-quote-argument tramp-end-of-output) shell)
6302 ;; Setting prompts.
6303 (tramp-message vec 5 "Setting remote shell prompt...")
6304 (tramp-send-command
6305 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
6306 (tramp-send-command vec "PS2=''" t)
6307 (tramp-send-command vec "PS3=''" t)
6308 (tramp-send-command vec "PROMPT_COMMAND=''" t)
6309 (tramp-message vec 5 "Setting remote shell prompt...done"))
6311 (t (tramp-message
6312 vec 5 "Remote `%s' groks tilde expansion, good"
6313 (tramp-get-method-parameter
6314 (tramp-file-name-method vec) 'tramp-remote-sh))
6315 (tramp-set-connection-property
6316 vec "remote-shell"
6317 (tramp-get-method-parameter
6318 (tramp-file-name-method vec) 'tramp-remote-sh))))))))
6320 ;; ------------------------------------------------------------
6321 ;; -- Functions for establishing connection --
6322 ;; ------------------------------------------------------------
6324 ;; The following functions are actions to be taken when seeing certain
6325 ;; prompts from the remote host. See the variable
6326 ;; `tramp-actions-before-shell' for usage of these functions.
6328 (defun tramp-action-login (proc vec)
6329 "Send the login name."
6330 (when (not (stringp tramp-current-user))
6331 (save-window-excursion
6332 (let ((enable-recursive-minibuffers t))
6333 (pop-to-buffer (tramp-get-connection-buffer vec))
6334 (setq tramp-current-user (read-string (match-string 0))))))
6335 (tramp-message vec 3 "Sending login name `%s'" tramp-current-user)
6336 (with-current-buffer (tramp-get-connection-buffer vec)
6337 (tramp-message vec 6 "\n%s" (buffer-string)))
6338 (tramp-send-string vec tramp-current-user))
6340 (defun tramp-action-password (proc vec)
6341 "Query the user for a password."
6342 (with-current-buffer (process-buffer proc)
6343 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
6344 (tramp-message vec 3 "Sending %s" (match-string 1)))
6345 (tramp-enter-password proc))
6347 (defun tramp-action-succeed (proc vec)
6348 "Signal success in finding shell prompt."
6349 (throw 'tramp-action 'ok))
6351 (defun tramp-action-permission-denied (proc vec)
6352 "Signal permission denied."
6353 (kill-process proc)
6354 (throw 'tramp-action 'permission-denied))
6356 (defun tramp-action-yesno (proc vec)
6357 "Ask the user for confirmation using `yes-or-no-p'.
6358 Send \"yes\" to remote process on confirmation, abort otherwise.
6359 See also `tramp-action-yn'."
6360 (save-window-excursion
6361 (let ((enable-recursive-minibuffers t))
6362 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
6363 (unless (yes-or-no-p (match-string 0))
6364 (kill-process proc)
6365 (throw 'tramp-action 'permission-denied))
6366 (with-current-buffer (tramp-get-connection-buffer vec)
6367 (tramp-message vec 6 "\n%s" (buffer-string)))
6368 (tramp-send-string vec "yes"))))
6370 (defun tramp-action-yn (proc vec)
6371 "Ask the user for confirmation using `y-or-n-p'.
6372 Send \"y\" to remote process on confirmation, abort otherwise.
6373 See also `tramp-action-yesno'."
6374 (save-window-excursion
6375 (let ((enable-recursive-minibuffers t))
6376 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
6377 (unless (y-or-n-p (match-string 0))
6378 (kill-process proc)
6379 (throw 'tramp-action 'permission-denied))
6380 (with-current-buffer (tramp-get-connection-buffer vec)
6381 (tramp-message vec 6 "\n%s" (buffer-string)))
6382 (tramp-send-string vec "y"))))
6384 (defun tramp-action-terminal (proc vec)
6385 "Tell the remote host which terminal type to use.
6386 The terminal type can be configured with `tramp-terminal-type'."
6387 (tramp-message vec 5 "Setting `%s' as terminal type." tramp-terminal-type)
6388 (with-current-buffer (tramp-get-connection-buffer vec)
6389 (tramp-message vec 6 "\n%s" (buffer-string)))
6390 (tramp-send-string vec tramp-terminal-type))
6392 (defun tramp-action-process-alive (proc vec)
6393 "Check whether a process has finished."
6394 (unless (memq (process-status proc) '(run open))
6395 (throw 'tramp-action 'process-died)))
6397 (defun tramp-action-out-of-band (proc vec)
6398 "Check whether an out-of-band copy has finished."
6399 (cond ((and (memq (process-status proc) '(stop exit))
6400 (zerop (process-exit-status proc)))
6401 (tramp-message vec 3 "Process has finished.")
6402 (throw 'tramp-action 'ok))
6403 ((or (and (memq (process-status proc) '(stop exit))
6404 (not (zerop (process-exit-status proc))))
6405 (memq (process-status proc) '(signal)))
6406 ;; `scp' could have copied correctly, but set modes could have failed.
6407 ;; This can be ignored.
6408 (with-current-buffer (process-buffer proc)
6409 (goto-char (point-min))
6410 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
6411 (progn
6412 (tramp-message vec 5 "'set mode' error ignored.")
6413 (tramp-message vec 3 "Process has finished.")
6414 (throw 'tramp-action 'ok))
6415 (tramp-message vec 3 "Process has died.")
6416 (throw 'tramp-action 'process-died))))
6417 (t nil)))
6419 ;; Functions for processing the actions.
6421 (defun tramp-process-one-action (proc vec actions)
6422 "Wait for output from the shell and perform one action."
6423 (let (found todo item pattern action)
6424 (while (not found)
6425 ;; Reread output once all actions have been performed.
6426 ;; Obviously, the output was not complete.
6427 (tramp-accept-process-output proc 1)
6428 (setq todo actions)
6429 (while todo
6430 (setq item (pop todo))
6431 (setq pattern (format "\\(%s\\)\\'" (symbol-value (nth 0 item))))
6432 (setq action (nth 1 item))
6433 (tramp-message
6434 vec 5 "Looking for regexp \"%s\" from remote shell" pattern)
6435 (when (tramp-check-for-regexp proc pattern)
6436 (tramp-message vec 5 "Call `%s'" (symbol-name action))
6437 (setq found (funcall action proc vec)))))
6438 found))
6440 (defun tramp-process-actions (proc vec actions &optional timeout)
6441 "Perform actions until success or TIMEOUT."
6442 ;; Enable auth-source and password-cache.
6443 (tramp-set-connection-property proc "first-password-request" t)
6444 (let (exit)
6445 (while (not exit)
6446 (tramp-message proc 3 "Waiting for prompts from remote shell")
6447 (setq exit
6448 (catch 'tramp-action
6449 (if timeout
6450 (with-timeout (timeout)
6451 (tramp-process-one-action proc vec actions))
6452 (tramp-process-one-action proc vec actions)))))
6453 (with-current-buffer (tramp-get-connection-buffer vec)
6454 (tramp-message vec 6 "\n%s" (buffer-string)))
6455 (unless (eq exit 'ok)
6456 (tramp-clear-passwd vec)
6457 (tramp-error-with-buffer
6458 nil vec 'file-error
6459 (cond
6460 ((eq exit 'permission-denied) "Permission denied")
6461 ((eq exit 'process-died) "Process died")
6462 (t "Login failed"))))))
6464 ;; Utility functions.
6466 (defun tramp-accept-process-output (&optional proc timeout timeout-msecs)
6467 "Like `accept-process-output' for Tramp processes.
6468 This is needed in order to hide `last-coding-system-used', which is set
6469 for process communication also."
6470 (with-current-buffer (process-buffer proc)
6471 (tramp-message proc 10 "%s %s" proc (process-status proc))
6472 (let (buffer-read-only last-coding-system-used)
6473 ;; Under Windows XP, accept-process-output doesn't return
6474 ;; sometimes. So we add an additional timeout.
6475 (with-timeout ((or timeout 1))
6476 (accept-process-output proc timeout timeout-msecs)))
6477 (tramp-message proc 10 "\n%s" (buffer-string))))
6479 (defun tramp-check-for-regexp (proc regexp)
6480 "Check whether REGEXP is contained in process buffer of PROC.
6481 Erase echoed commands if exists."
6482 (with-current-buffer (process-buffer proc)
6483 (goto-char (point-min))
6485 ;; Check whether we need to remove echo output.
6486 (when (and (tramp-get-connection-property proc "check-remote-echo" nil)
6487 (re-search-forward tramp-echoed-echo-mark-regexp nil t))
6488 (let ((begin (match-beginning 0)))
6489 (when (re-search-forward tramp-echoed-echo-mark-regexp nil t)
6490 ;; Discard echo from remote output.
6491 (tramp-set-connection-property proc "check-remote-echo" nil)
6492 (tramp-message proc 5 "echo-mark found")
6493 (forward-line)
6494 (delete-region begin (point))
6495 (goto-char (point-min)))))
6497 (when (not (tramp-get-connection-property proc "check-remote-echo" nil))
6498 ;; No echo to be handled, now we can look for the regexp.
6499 (goto-char (point-min))
6500 (re-search-forward regexp nil t))))
6502 (defun tramp-wait-for-regexp (proc timeout regexp)
6503 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
6504 Expects the output of PROC to be sent to the current buffer. Returns
6505 the string that matched, or nil. Waits indefinitely if TIMEOUT is
6506 nil."
6507 (with-current-buffer (process-buffer proc)
6508 (let ((found (tramp-check-for-regexp proc regexp))
6509 (start-time (current-time)))
6510 (cond (timeout
6511 ;; Work around a bug in XEmacs 21, where the timeout
6512 ;; expires faster than it should. This degenerates
6513 ;; to polling for buggy XEmacsen, but oh, well.
6514 (while (and (not found)
6515 (< (tramp-time-diff (current-time) start-time)
6516 timeout))
6517 (with-timeout (timeout)
6518 (while (not found)
6519 (tramp-accept-process-output proc 1)
6520 (unless (memq (process-status proc) '(run open))
6521 (tramp-error-with-buffer
6522 nil proc 'file-error "Process has died"))
6523 (setq found (tramp-check-for-regexp proc regexp))))))
6525 (while (not found)
6526 (tramp-accept-process-output proc 1)
6527 (unless (memq (process-status proc) '(run open))
6528 (tramp-error-with-buffer
6529 nil proc 'file-error "Process has died"))
6530 (setq found (tramp-check-for-regexp proc regexp)))))
6531 (tramp-message proc 6 "\n%s" (buffer-string))
6532 (when (not found)
6533 (if timeout
6534 (tramp-error
6535 proc 'file-error "[[Regexp `%s' not found in %d secs]]"
6536 regexp timeout)
6537 (tramp-error proc 'file-error "[[Regexp `%s' not found]]" regexp)))
6538 found)))
6540 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
6541 "Wait for shell prompt and barf if none appears.
6542 Looks at process PROC to see if a shell prompt appears in TIMEOUT
6543 seconds. If not, it produces an error message with the given ERROR-ARGS."
6544 (unless
6545 (tramp-wait-for-regexp
6546 proc timeout
6547 (format
6548 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
6549 (apply 'tramp-error-with-buffer nil proc 'file-error error-args)))
6551 ;; We don't call `tramp-send-string' in order to hide the password
6552 ;; from the debug buffer, and because end-of-line handling of the
6553 ;; string.
6554 (defun tramp-enter-password (proc)
6555 "Prompt for a password and send it to the remote end."
6556 (process-send-string
6557 proc (concat (tramp-read-passwd proc)
6558 (or (tramp-get-method-parameter
6559 tramp-current-method
6560 'tramp-password-end-of-line)
6561 tramp-default-password-end-of-line))))
6563 (defun tramp-open-connection-setup-interactive-shell (proc vec)
6564 "Set up an interactive shell.
6565 Mainly sets the prompt and the echo correctly. PROC is the shell
6566 process to set up. VEC specifies the connection."
6567 (let ((tramp-end-of-output "$ "))
6568 ;; It is useful to set the prompt in the following command because
6569 ;; some people have a setting for $PS1 which /bin/sh doesn't know
6570 ;; about and thus /bin/sh will display a strange prompt. For
6571 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
6572 ;; display the current working directory but /bin/sh will display
6573 ;; a dollar sign. The following command line sets $PS1 to a sane
6574 ;; value, and works under Bourne-ish shells as well as csh-like
6575 ;; shells. Daniel Pittman reports that the unusual positioning of
6576 ;; the single quotes makes it work under `rc', too. We also unset
6577 ;; the variable $ENV because that is read by some sh
6578 ;; implementations (eg, bash when called as sh) on startup; this
6579 ;; way, we avoid the startup file clobbering $PS1. $PROMP_COMMAND
6580 ;; is another way to set the prompt in /bin/bash, it must be
6581 ;; discarded as well.
6582 (tramp-send-command
6584 (format
6585 "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s"
6586 (shell-quote-argument tramp-end-of-output)
6587 (tramp-get-method-parameter
6588 (tramp-file-name-method vec) 'tramp-remote-sh))
6591 ;; Disable echo.
6592 (tramp-message vec 5 "Setting up remote shell environment")
6593 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
6594 ;; Check whether the echo has really been disabled. Some
6595 ;; implementations, like busybox of embedded GNU/Linux, don't
6596 ;; support disabling.
6597 (tramp-send-command vec "echo foo" t)
6598 (with-current-buffer (process-buffer proc)
6599 (goto-char (point-min))
6600 (when (looking-at "echo foo")
6601 (tramp-set-connection-property proc "remote-echo" t)
6602 (tramp-message vec 5 "Remote echo still on. Ok.")
6603 ;; Make sure backspaces and their echo are enabled and no line
6604 ;; width magic interferes with them.
6605 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
6607 (tramp-message vec 5 "Setting shell prompt")
6608 (tramp-send-command
6609 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
6610 (tramp-send-command vec "PS2=''" t)
6611 (tramp-send-command vec "PS3=''" t)
6612 (tramp-send-command vec "PROMPT_COMMAND=''" t)
6614 ;; Try to set up the coding system correctly.
6615 ;; CCC this can't be the right way to do it. Hm.
6616 (tramp-message vec 5 "Determining coding system")
6617 (tramp-send-command vec "echo foo ; echo bar" t)
6618 (with-current-buffer (process-buffer proc)
6619 (goto-char (point-min))
6620 (if (featurep 'mule)
6621 ;; Use MULE to select the right EOL convention for communicating
6622 ;; with the process.
6623 (let* ((cs (or (funcall (symbol-function 'process-coding-system) proc)
6624 (cons 'undecided 'undecided)))
6625 cs-decode cs-encode)
6626 (when (symbolp cs) (setq cs (cons cs cs)))
6627 (setq cs-decode (car cs))
6628 (setq cs-encode (cdr cs))
6629 (unless cs-decode (setq cs-decode 'undecided))
6630 (unless cs-encode (setq cs-encode 'undecided))
6631 (setq cs-encode (tramp-coding-system-change-eol-conversion
6632 cs-encode 'unix))
6633 (when (search-forward "\r" nil t)
6634 (setq cs-decode (tramp-coding-system-change-eol-conversion
6635 cs-decode 'dos)))
6636 (funcall (symbol-function 'set-buffer-process-coding-system)
6637 cs-decode cs-encode)
6638 (tramp-message
6639 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
6640 ;; Look for ^M and do something useful if found.
6641 (when (search-forward "\r" nil t)
6642 ;; We have found a ^M but cannot frob the process coding system
6643 ;; because we're running on a non-MULE Emacs. Let's try
6644 ;; stty, instead.
6645 (tramp-send-command vec "stty -onlcr" t))))
6646 (tramp-send-command vec "set +o vi +o emacs" t)
6648 ;; Check whether the output of "uname -sr" has been changed. If
6649 ;; yes, this is a strong indication that we must expire all
6650 ;; connection properties. We start again with
6651 ;; `tramp-maybe-open-connection', it will be catched there.
6652 (tramp-message vec 5 "Checking system information")
6653 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
6654 (new-uname
6655 (tramp-set-connection-property
6656 vec "uname"
6657 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
6658 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
6659 (with-current-buffer (tramp-get-debug-buffer vec)
6660 ;; Keep the debug buffer
6661 (rename-buffer
6662 (generate-new-buffer-name tramp-temp-buffer-name) 'unique)
6663 (funcall (symbol-function 'tramp-cleanup-connection) vec)
6664 (if (= (point-min) (point-max))
6665 (kill-buffer nil)
6666 (rename-buffer (tramp-debug-buffer-name vec) 'unique))
6667 ;; We call `tramp-get-buffer' in order to keep the debug buffer.
6668 (tramp-get-buffer vec)
6669 (tramp-message
6670 vec 3
6671 "Connection reset, because remote host changed from `%s' to `%s'"
6672 old-uname new-uname)
6673 (throw 'uname-changed (tramp-maybe-open-connection vec)))))
6675 ;; Check whether the remote host suffers from buggy
6676 ;; `send-process-string'. This is known for FreeBSD (see comment in
6677 ;; `send_process', file process.c). I've tested sending 624 bytes
6678 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
6679 ;; this host type is detected locally. It cannot handle remote
6680 ;; hosts, though.
6681 (with-connection-property proc "chunksize"
6682 (cond
6683 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
6684 tramp-chunksize)
6686 (tramp-message
6687 vec 5 "Checking remote host type for `send-process-string' bug")
6688 (if (string-match
6689 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
6690 500 0))))
6692 ;; Set remote PATH variable.
6693 (tramp-set-remote-path vec)
6695 ;; Search for a good shell before searching for a command which
6696 ;; checks if a file exists. This is done because Tramp wants to use
6697 ;; "test foo; echo $?" to check if various conditions hold, and
6698 ;; there are buggy /bin/sh implementations which don't execute the
6699 ;; "echo $?" part if the "test" part has an error. In particular,
6700 ;; the Solaris /bin/sh is a problem. I'm betting that all systems
6701 ;; with buggy /bin/sh implementations will have a working bash or
6702 ;; ksh. Whee...
6703 (tramp-find-shell vec)
6705 ;; Disable unexpected output.
6706 (tramp-send-command vec "mesg n; biff n" t)
6708 ;; Set the environment.
6709 (tramp-message vec 5 "Setting default environment")
6711 ;; On OpenSolaris, there is a bug when HISTFILE is changed in place
6712 ;; <http://bugs.opensolaris.org/view_bug.do?bug_id=6834184>. We
6713 ;; apply the workaround.
6714 (if (string-equal (tramp-get-connection-property vec "uname" "") "SunOS 5.11")
6715 (tramp-send-command vec "unset HISTFILE"))
6717 (let ((env (copy-sequence tramp-remote-process-environment))
6718 unset item)
6719 (while env
6720 (setq item (tramp-compat-split-string (car env) "="))
6721 (if (and (stringp (cadr item)) (not (string-equal (cadr item) "")))
6722 (tramp-send-command
6723 vec (format "%s=%s; export %s" (car item) (cadr item) (car item)) t)
6724 (push (car item) unset))
6725 (setq env (cdr env)))
6726 (when unset
6727 (tramp-send-command
6728 vec (format "unset %s" (mapconcat 'identity unset " "))))) t)
6730 ;; CCC: We should either implement a Perl version of base64 encoding
6731 ;; and decoding. Then we just use that in the last item. The other
6732 ;; alternative is to use the Perl version of UU encoding. But then
6733 ;; we need a Lisp version of uuencode.
6735 ;; Old text from documentation of tramp-methods:
6736 ;; Using a uuencode/uudecode inline method is discouraged, please use one
6737 ;; of the base64 methods instead since base64 encoding is much more
6738 ;; reliable and the commands are more standardized between the different
6739 ;; Unix versions. But if you can't use base64 for some reason, please
6740 ;; note that the default uudecode command does not work well for some
6741 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
6742 ;; the following command for uudecode:
6744 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
6746 ;; For Irix, no solution is known yet.
6748 (defconst tramp-local-coding-commands
6749 '((b64 base64-encode-region base64-decode-region)
6750 (uu tramp-uuencode-region uudecode-decode-region)
6751 (pack
6752 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
6753 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
6754 "List of local coding commands for inline transfer.
6755 Each item is a list that looks like this:
6757 \(FORMAT ENCODING DECODING)
6759 FORMAT is symbol describing the encoding/decoding format. It can be
6760 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
6762 ENCODING and DECODING can be strings, giving commands, or symbols,
6763 giving functions. If they are strings, then they can contain
6764 the \"%s\" format specifier. If that specifier is present, the input
6765 filename will be put into the command line at that spot. If the
6766 specifier is not present, the input should be read from standard
6767 input.
6769 If they are functions, they will be called with two arguments, start
6770 and end of region, and are expected to replace the region contents
6771 with the encoded or decoded results, respectively.")
6773 (defconst tramp-remote-coding-commands
6774 '((b64 "base64" "base64 -d")
6775 (b64 "mimencode -b" "mimencode -u -b")
6776 (b64 "mmencode -b" "mmencode -u -b")
6777 (b64 "recode data..base64" "recode base64..data")
6778 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
6779 (b64 tramp-perl-encode tramp-perl-decode)
6780 (uu "uuencode xxx" "uudecode -o /dev/stdout")
6781 (uu "uuencode xxx" "uudecode -o -")
6782 (uu "uuencode xxx" "uudecode -p")
6783 (uu "uuencode xxx" tramp-uudecode)
6784 (pack
6785 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
6786 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
6787 "List of remote coding commands for inline transfer.
6788 Each item is a list that looks like this:
6790 \(FORMAT ENCODING DECODING)
6792 FORMAT is symbol describing the encoding/decoding format. It can be
6793 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
6795 ENCODING and DECODING can be strings, giving commands, or symbols,
6796 giving variables. If they are strings, then they can contain
6797 the \"%s\" format specifier. If that specifier is present, the input
6798 filename will be put into the command line at that spot. If the
6799 specifier is not present, the input should be read from standard
6800 input.
6802 If they are variables, this variable is a string containing a Perl
6803 implementation for this functionality. This Perl program will be transferred
6804 to the remote host, and it is avalible as shell function with the same name.")
6806 (defun tramp-find-inline-encoding (vec)
6807 "Find an inline transfer encoding that works.
6808 Goes through the list `tramp-local-coding-commands' and
6809 `tramp-remote-coding-commands'."
6810 (save-excursion
6811 (let ((local-commands tramp-local-coding-commands)
6812 (magic "xyzzy")
6813 loc-enc loc-dec rem-enc rem-dec litem ritem found)
6814 (while (and local-commands (not found))
6815 (setq litem (pop local-commands))
6816 (catch 'wont-work-local
6817 (let ((format (nth 0 litem))
6818 (remote-commands tramp-remote-coding-commands))
6819 (setq loc-enc (nth 1 litem))
6820 (setq loc-dec (nth 2 litem))
6821 ;; If the local encoder or decoder is a string, the
6822 ;; corresponding command has to work locally.
6823 (if (not (stringp loc-enc))
6824 (tramp-message
6825 vec 5 "Checking local encoding function `%s'" loc-enc)
6826 (tramp-message
6827 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
6828 (unless (zerop (tramp-call-local-coding-command
6829 loc-enc nil nil))
6830 (throw 'wont-work-local nil)))
6831 (if (not (stringp loc-dec))
6832 (tramp-message
6833 vec 5 "Checking local decoding function `%s'" loc-dec)
6834 (tramp-message
6835 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
6836 (unless (zerop (tramp-call-local-coding-command
6837 loc-dec nil nil))
6838 (throw 'wont-work-local nil)))
6839 ;; Search for remote coding commands with the same format
6840 (while (and remote-commands (not found))
6841 (setq ritem (pop remote-commands))
6842 (catch 'wont-work-remote
6843 (when (equal format (nth 0 ritem))
6844 (setq rem-enc (nth 1 ritem))
6845 (setq rem-dec (nth 2 ritem))
6846 ;; Check if remote encoding and decoding commands can be
6847 ;; called remotely with null input and output. This makes
6848 ;; sure there are no syntax errors and the command is really
6849 ;; found. Note that we do not redirect stdout to /dev/null,
6850 ;; for two reasons: when checking the decoding command, we
6851 ;; actually check the output it gives. And also, when
6852 ;; redirecting "mimencode" output to /dev/null, then as root
6853 ;; it might change the permissions of /dev/null!
6854 (when (not (stringp rem-enc))
6855 (let ((name (symbol-name rem-enc)))
6856 (while (string-match (regexp-quote "-") name)
6857 (setq name (replace-match "_" nil t name)))
6858 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
6859 (setq rem-enc name)))
6860 (tramp-message
6861 vec 5
6862 "Checking remote encoding command `%s' for sanity" rem-enc)
6863 (unless (zerop (tramp-send-command-and-check
6864 vec (format "%s </dev/null" rem-enc) t))
6865 (throw 'wont-work-remote nil))
6867 (when (not (stringp rem-dec))
6868 (let ((name (symbol-name rem-dec)))
6869 (while (string-match (regexp-quote "-") name)
6870 (setq name (replace-match "_" nil t name)))
6871 (tramp-maybe-send-script vec (symbol-value rem-dec) name)
6872 (setq rem-dec name)))
6873 (tramp-message
6874 vec 5
6875 "Checking remote decoding command `%s' for sanity" rem-dec)
6876 (unless (zerop (tramp-send-command-and-check
6878 (format "echo %s | %s | %s"
6879 magic rem-enc rem-dec) t))
6880 (throw 'wont-work-remote nil))
6882 (with-current-buffer (tramp-get-buffer vec)
6883 (goto-char (point-min))
6884 (unless (looking-at (regexp-quote magic))
6885 (throw 'wont-work-remote nil)))
6887 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
6888 (setq rem-enc (nth 1 ritem))
6889 (setq rem-dec (nth 2 ritem))
6890 (setq found t)))))))
6892 ;; Did we find something?
6893 (unless found
6894 (tramp-message vec 2 "Couldn't find an inline transfer encoding"))
6896 ;; Set connection properties.
6897 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
6898 (tramp-set-connection-property vec "local-encoding" loc-enc)
6899 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
6900 (tramp-set-connection-property vec "local-decoding" loc-dec)
6901 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
6902 (tramp-set-connection-property vec "remote-encoding" rem-enc)
6903 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
6904 (tramp-set-connection-property vec "remote-decoding" rem-dec))))
6906 (defun tramp-call-local-coding-command (cmd input output)
6907 "Call the local encoding or decoding command.
6908 If CMD contains \"%s\", provide input file INPUT there in command.
6909 Otherwise, INPUT is passed via standard input.
6910 INPUT can also be nil which means `/dev/null'.
6911 OUTPUT can be a string (which specifies a filename), or t (which
6912 means standard output and thus the current buffer), or nil (which
6913 means discard it)."
6914 (tramp-local-call-process
6915 tramp-encoding-shell
6916 (when (and input (not (string-match "%s" cmd))) input)
6917 (if (eq output t) t nil)
6919 tramp-encoding-command-switch
6920 (concat
6921 (if (string-match "%s" cmd) (format cmd input) cmd)
6922 (if (stringp output) (concat "> " output) ""))))
6924 (defun tramp-compute-multi-hops (vec)
6925 "Expands VEC according to `tramp-default-proxies-alist'.
6926 Gateway hops are already opened."
6927 (let ((target-alist `(,vec))
6928 (choices tramp-default-proxies-alist)
6929 item proxy)
6931 ;; Look for proxy hosts to be passed.
6932 (while choices
6933 (setq item (pop choices)
6934 proxy (eval (nth 2 item)))
6935 (when (and
6936 ;; host
6937 (string-match (or (eval (nth 0 item)) "")
6938 (or (tramp-file-name-host (car target-alist)) ""))
6939 ;; user
6940 (string-match (or (eval (nth 1 item)) "")
6941 (or (tramp-file-name-user (car target-alist)) "")))
6942 (if (null proxy)
6943 ;; No more hops needed.
6944 (setq choices nil)
6945 ;; Replace placeholders.
6946 (setq proxy
6947 (format-spec
6948 proxy
6949 `((?u . ,(or (tramp-file-name-user (car target-alist)) ""))
6950 (?h . ,(or (tramp-file-name-host (car target-alist)) "")))))
6951 (with-parsed-tramp-file-name proxy l
6952 ;; Add the hop.
6953 (add-to-list 'target-alist l)
6954 ;; Start next search.
6955 (setq choices tramp-default-proxies-alist)))))
6957 ;; Handle gateways.
6958 (when (and (boundp 'tramp-gw-tunnel-method)
6959 (string-match (format
6960 "^\\(%s\\|%s\\)$"
6961 (symbol-value 'tramp-gw-tunnel-method)
6962 (symbol-value 'tramp-gw-socks-method))
6963 (tramp-file-name-method (car target-alist))))
6964 (let ((gw (pop target-alist))
6965 (hop (pop target-alist)))
6966 ;; Is the method prepared for gateways?
6967 (unless (tramp-get-method-parameter
6968 (tramp-file-name-method hop) 'tramp-default-port)
6969 (tramp-error
6970 vec 'file-error
6971 "Method `%s' is not supported for gateway access."
6972 (tramp-file-name-method hop)))
6973 ;; Add default port if needed.
6974 (unless
6975 (string-match
6976 tramp-host-with-port-regexp (tramp-file-name-host hop))
6977 (aset hop 2
6978 (concat
6979 (tramp-file-name-host hop) tramp-prefix-port-format
6980 (number-to-string
6981 (tramp-get-method-parameter
6982 (tramp-file-name-method hop) 'tramp-default-port)))))
6983 ;; Open the gateway connection.
6984 (add-to-list
6985 'target-alist
6986 (vector
6987 (tramp-file-name-method hop) (tramp-file-name-user hop)
6988 (funcall (symbol-function 'tramp-gw-open-connection) vec gw hop) nil))
6989 ;; For the password prompt, we need the correct values.
6990 ;; Therefore, we must remember the gateway vector. But we
6991 ;; cannot do it as connection property, because it shouldn't
6992 ;; be persistent. And we have no started process yet either.
6993 (tramp-set-file-property (car target-alist) "" "gateway" hop)))
6995 ;; Foreign and out-of-band methods are not supported for multi-hops.
6996 (when (cdr target-alist)
6997 (setq choices target-alist)
6998 (while choices
6999 (setq item (pop choices))
7000 (when
7002 (not
7003 (tramp-get-method-parameter
7004 (tramp-file-name-method item) 'tramp-login-program))
7005 (tramp-get-method-parameter
7006 (tramp-file-name-method item) 'tramp-copy-program))
7007 (tramp-error
7008 vec 'file-error
7009 "Method `%s' is not supported for multi-hops."
7010 (tramp-file-name-method item)))))
7012 ;; In case the host name is not used for the remote shell
7013 ;; command, the user could be misguided by applying a random
7014 ;; hostname.
7015 (let* ((v (car target-alist))
7016 (method (tramp-file-name-method v))
7017 (host (tramp-file-name-host v)))
7018 (unless
7020 ;; There are multi-hops.
7021 (cdr target-alist)
7022 ;; The host name is used for the remote shell command.
7023 (member
7024 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
7025 ;; The host is local. We cannot use `tramp-local-host-p'
7026 ;; here, because it opens a connection as well.
7027 (string-match tramp-local-host-regexp host))
7028 (tramp-error
7029 v 'file-error
7030 "Host `%s' looks like a remote host, `%s' can only use the local host"
7031 host method)))
7033 ;; Result.
7034 target-alist))
7036 (defun tramp-maybe-open-connection (vec)
7037 "Maybe open a connection VEC.
7038 Does not do anything if a connection is already open, but re-opens the
7039 connection if a previous connection has died for some reason."
7040 (catch 'uname-changed
7041 (let ((p (tramp-get-connection-process vec))
7042 (process-environment (copy-sequence process-environment)))
7044 ;; If too much time has passed since last command was sent, look
7045 ;; whether process is still alive. If it isn't, kill it. When
7046 ;; using ssh, it can sometimes happen that the remote end has
7047 ;; hung up but the local ssh client doesn't recognize this until
7048 ;; it tries to send some data to the remote end. So that's why
7049 ;; we try to send a command from time to time, then look again
7050 ;; whether the process is really alive.
7051 (condition-case nil
7052 (when (and (> (tramp-time-diff
7053 (current-time)
7054 (tramp-get-connection-property
7055 p "last-cmd-time" '(0 0 0)))
7057 p (processp p) (memq (process-status p) '(run open)))
7058 (tramp-send-command vec "echo are you awake" t t)
7059 (unless (and (memq (process-status p) '(run open))
7060 (tramp-wait-for-output p 10))
7061 ;; The error will be catched locally.
7062 (tramp-error vec 'file-error "Awake did fail")))
7063 (file-error
7064 (tramp-flush-connection-property vec)
7065 (tramp-flush-connection-property p)
7066 (delete-process p)
7067 (setq p nil)))
7069 ;; New connection must be opened.
7070 (unless (and p (processp p) (memq (process-status p) '(run open)))
7072 ;; We call `tramp-get-buffer' in order to get a debug buffer for
7073 ;; messages from the beginning.
7074 (tramp-get-buffer vec)
7075 (if (zerop (length (tramp-file-name-user vec)))
7076 (tramp-message
7077 vec 3 "Opening connection for %s using %s..."
7078 (tramp-file-name-host vec)
7079 (tramp-file-name-method vec))
7080 (tramp-message
7081 vec 3 "Opening connection for %s@%s using %s..."
7082 (tramp-file-name-user vec)
7083 (tramp-file-name-host vec)
7084 (tramp-file-name-method vec)))
7086 ;; Start new process.
7087 (when (and p (processp p))
7088 (delete-process p))
7089 (setenv "TERM" tramp-terminal-type)
7090 (setenv "LC_ALL" "C")
7091 (setenv "PROMPT_COMMAND")
7092 (setenv "PS1" "$ ")
7093 (let* ((target-alist (tramp-compute-multi-hops vec))
7094 (process-connection-type tramp-process-connection-type)
7095 (process-adaptive-read-buffering nil)
7096 (coding-system-for-read nil)
7097 ;; This must be done in order to avoid our file name handler.
7098 (p (let ((default-directory
7099 (tramp-compat-temporary-file-directory)))
7100 (start-process
7101 (or (tramp-get-connection-property vec "process-name" nil)
7102 (tramp-buffer-name vec))
7103 (tramp-get-connection-buffer vec)
7104 tramp-encoding-shell))))
7106 (tramp-message
7107 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
7109 ;; Check whether process is alive.
7110 (tramp-set-process-query-on-exit-flag p nil)
7111 (tramp-message vec 3 "Waiting 60s for local shell to come up...")
7112 (tramp-barf-if-no-shell-prompt
7113 p 60 "Couldn't find local shell prompt %s" tramp-encoding-shell)
7115 ;; Now do all the connections as specified.
7116 (while target-alist
7117 (let* ((hop (car target-alist))
7118 (l-method (tramp-file-name-method hop))
7119 (l-user (tramp-file-name-user hop))
7120 (l-host (tramp-file-name-host hop))
7121 (l-port nil)
7122 (login-program
7123 (tramp-get-method-parameter l-method 'tramp-login-program))
7124 (login-args
7125 (tramp-get-method-parameter l-method 'tramp-login-args))
7126 (gw-args
7127 (tramp-get-method-parameter l-method 'tramp-gw-args))
7128 (gw (tramp-get-file-property hop "" "gateway" nil))
7129 (g-method (and gw (tramp-file-name-method gw)))
7130 (g-user (and gw (tramp-file-name-user gw)))
7131 (g-host (and gw (tramp-file-name-host gw)))
7132 (command login-program)
7133 ;; We don't create the temporary file. In fact, it
7134 ;; is just a prefix for the ControlPath option of
7135 ;; ssh; the real temporary file has another name, and
7136 ;; it is created and protected by ssh. It is also
7137 ;; removed by ssh, when the connection is closed.
7138 (tmpfile
7139 (tramp-set-connection-property
7140 p "temp-file"
7141 (make-temp-name
7142 (expand-file-name
7143 tramp-temp-name-prefix
7144 (tramp-compat-temporary-file-directory)))))
7145 spec)
7147 ;; Add gateway arguments if necessary.
7148 (when (and gw gw-args)
7149 (setq login-args (append login-args gw-args)))
7151 ;; Check for port number. Until now, there's no need
7152 ;; for handling like method, user, host.
7153 (when (string-match tramp-host-with-port-regexp l-host)
7154 (setq l-port (match-string 2 l-host)
7155 l-host (match-string 1 l-host)))
7157 ;; Set variables for computing the prompt for reading
7158 ;; password. They can also be derived from a gateway.
7159 (setq tramp-current-method (or g-method l-method)
7160 tramp-current-user (or g-user l-user)
7161 tramp-current-host (or g-host l-host))
7163 ;; Replace login-args place holders.
7164 (setq
7165 l-host (or l-host "")
7166 l-user (or l-user "")
7167 l-port (or l-port "")
7168 spec `((?h . ,l-host) (?u . ,l-user) (?p . ,l-port)
7169 (?t . ,tmpfile))
7170 command
7171 (concat
7172 command " "
7173 (mapconcat
7174 (lambda (x)
7175 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
7176 (unless (member "" x) (mapconcat 'identity x " ")))
7177 login-args " ")
7178 ;; Local shell could be a Windows COMSPEC. It doesn't
7179 ;; know the ";" syntax, but we must exit always for
7180 ;; `start-file-process'. "exec" does not work either.
7181 " && exit || exit"))
7183 ;; Send the command.
7184 (tramp-message vec 3 "Sending command `%s'" command)
7185 (tramp-send-command vec command t t)
7186 (tramp-process-actions p vec tramp-actions-before-shell 60)
7187 (tramp-message vec 3 "Found remote shell prompt on `%s'" l-host))
7188 ;; Next hop.
7189 (setq target-alist (cdr target-alist)))
7191 ;; Make initial shell settings.
7192 (tramp-open-connection-setup-interactive-shell p vec))))))
7194 (defun tramp-send-command (vec command &optional neveropen nooutput)
7195 "Send the COMMAND to connection VEC.
7196 Erases temporary buffer before sending the command. If optional
7197 arg NEVEROPEN is non-nil, never try to open the connection. This
7198 is meant to be used from `tramp-maybe-open-connection' only. The
7199 function waits for output unless NOOUTPUT is set."
7200 (unless neveropen (tramp-maybe-open-connection vec))
7201 (let ((p (tramp-get-connection-process vec)))
7202 (when (tramp-get-connection-property p "remote-echo" nil)
7203 ;; We mark the command string that it can be erased in the output buffer.
7204 (tramp-set-connection-property p "check-remote-echo" t)
7205 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
7206 (tramp-message vec 6 "%s" command)
7207 (tramp-send-string vec command)
7208 (unless nooutput (tramp-wait-for-output p))))
7210 (defun tramp-wait-for-output (proc &optional timeout)
7211 "Wait for output from remote rsh command."
7212 (with-current-buffer (process-buffer proc)
7213 (let* (;; Initially, `tramp-end-of-output' is "$ ". There might
7214 ;; be leading escape sequences, which must be ignored.
7215 (regexp (format "[^$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
7216 ;; Sometimes, the commands do not return a newline but a
7217 ;; null byte before the shell prompt, for example "git
7218 ;; ls-files -c -z ...".
7219 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
7220 (found (tramp-wait-for-regexp proc timeout regexp1)))
7221 (if found
7222 (let (buffer-read-only)
7223 (goto-char (point-max))
7224 (re-search-backward regexp nil t)
7225 (delete-region (point) (point-max)))
7226 (if timeout
7227 (tramp-error
7228 proc 'file-error
7229 "[[Remote prompt `%s' not found in %d secs]]"
7230 tramp-end-of-output timeout)
7231 (tramp-error
7232 proc 'file-error
7233 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
7234 ;; Return value is whether end-of-output sentinel was found.
7235 found)))
7237 (defun tramp-send-command-and-check (vec command &optional subshell)
7238 "Run COMMAND and check its exit status.
7239 Sends `echo $?' along with the COMMAND for checking the exit status. If
7240 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
7242 If the optional argument SUBSHELL is non-nil, the command is executed in
7243 a subshell, ie surrounded by parentheses."
7244 (tramp-send-command
7246 (concat (if subshell "( " "")
7247 command
7248 (if command " 2>/dev/null; " "")
7249 "echo tramp_exit_status $?"
7250 (if subshell " )" " ")))
7251 (with-current-buffer (tramp-get-connection-buffer vec)
7252 (goto-char (point-max))
7253 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
7254 (tramp-error
7255 vec 'file-error "Couldn't find exit status of `%s'" command))
7256 (skip-chars-forward "^ ")
7257 (prog1
7258 (read (current-buffer))
7259 (let (buffer-read-only) (delete-region (match-beginning 0) (point-max))))))
7261 (defun tramp-barf-unless-okay (vec command fmt &rest args)
7262 "Run COMMAND, check exit status, throw error if exit status not okay.
7263 Similar to `tramp-send-command-and-check' but accepts two more arguments
7264 FMT and ARGS which are passed to `error'."
7265 (unless (zerop (tramp-send-command-and-check vec command))
7266 (apply 'tramp-error vec 'file-error fmt args)))
7268 (defun tramp-send-command-and-read (vec command)
7269 "Run COMMAND and return the output, which must be a Lisp expression.
7270 In case there is no valid Lisp expression, it raises an error"
7271 (tramp-barf-unless-okay vec command "`%s' returns with error" command)
7272 (with-current-buffer (tramp-get-connection-buffer vec)
7273 ;; Read the expression.
7274 (goto-char (point-min))
7275 (condition-case nil
7276 (prog1 (read (current-buffer))
7277 ;; Error handling.
7278 (when (re-search-forward "\\S-" (tramp-compat-line-end-position) t)
7279 (error nil)))
7280 (error (tramp-error
7281 vec 'file-error
7282 "`%s' does not return a valid Lisp expression: `%s'"
7283 command (buffer-string))))))
7285 ;; It seems that Tru64 Unix does not like it if long strings are sent
7286 ;; to it in one go. (This happens when sending the Perl
7287 ;; `file-attributes' implementation, for instance.) Therefore, we
7288 ;; have this function which sends the string in chunks.
7289 (defun tramp-send-string (vec string)
7290 "Send the STRING via connection VEC.
7292 The STRING is expected to use Unix line-endings, but the lines sent to
7293 the remote host use line-endings as defined in the variable
7294 `tramp-rsh-end-of-line'. The communication buffer is erased before sending."
7295 (let* ((p (tramp-get-connection-process vec))
7296 (chunksize (tramp-get-connection-property p "chunksize" nil)))
7297 (unless p
7298 (tramp-error
7299 vec 'file-error "Can't send string to remote host -- not logged in"))
7300 (tramp-set-connection-property p "last-cmd-time" (current-time))
7301 (tramp-message vec 10 "%s" string)
7302 (with-current-buffer (tramp-get-connection-buffer vec)
7303 ;; Clean up the buffer. We cannot call `erase-buffer' because
7304 ;; narrowing might be in effect.
7305 (let (buffer-read-only) (delete-region (point-min) (point-max)))
7306 ;; Replace "\n" by `tramp-rsh-end-of-line'.
7307 (setq string
7308 (mapconcat 'identity
7309 (tramp-compat-split-string string "\n")
7310 tramp-rsh-end-of-line))
7311 (unless (or (string= string "")
7312 (string-equal (substring string -1) tramp-rsh-end-of-line))
7313 (setq string (concat string tramp-rsh-end-of-line)))
7314 ;; Send the string.
7315 (if (and chunksize (not (zerop chunksize)))
7316 (let ((pos 0)
7317 (end (length string)))
7318 (while (< pos end)
7319 (tramp-message
7320 vec 10 "Sending chunk from %s to %s"
7321 pos (min (+ pos chunksize) end))
7322 (process-send-string
7323 p (substring string pos (min (+ pos chunksize) end)))
7324 (setq pos (+ pos chunksize))))
7325 (process-send-string p string)))))
7327 (defun tramp-mode-string-to-int (mode-string)
7328 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
7329 (let* (case-fold-search
7330 (mode-chars (string-to-vector mode-string))
7331 (owner-read (aref mode-chars 1))
7332 (owner-write (aref mode-chars 2))
7333 (owner-execute-or-setid (aref mode-chars 3))
7334 (group-read (aref mode-chars 4))
7335 (group-write (aref mode-chars 5))
7336 (group-execute-or-setid (aref mode-chars 6))
7337 (other-read (aref mode-chars 7))
7338 (other-write (aref mode-chars 8))
7339 (other-execute-or-sticky (aref mode-chars 9)))
7340 (save-match-data
7341 (logior
7342 (cond
7343 ((char-equal owner-read ?r) (tramp-octal-to-decimal "00400"))
7344 ((char-equal owner-read ?-) 0)
7345 (t (error "Second char `%c' must be one of `r-'" owner-read)))
7346 (cond
7347 ((char-equal owner-write ?w) (tramp-octal-to-decimal "00200"))
7348 ((char-equal owner-write ?-) 0)
7349 (t (error "Third char `%c' must be one of `w-'" owner-write)))
7350 (cond
7351 ((char-equal owner-execute-or-setid ?x)
7352 (tramp-octal-to-decimal "00100"))
7353 ((char-equal owner-execute-or-setid ?S)
7354 (tramp-octal-to-decimal "04000"))
7355 ((char-equal owner-execute-or-setid ?s)
7356 (tramp-octal-to-decimal "04100"))
7357 ((char-equal owner-execute-or-setid ?-) 0)
7358 (t (error "Fourth char `%c' must be one of `xsS-'"
7359 owner-execute-or-setid)))
7360 (cond
7361 ((char-equal group-read ?r) (tramp-octal-to-decimal "00040"))
7362 ((char-equal group-read ?-) 0)
7363 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
7364 (cond
7365 ((char-equal group-write ?w) (tramp-octal-to-decimal "00020"))
7366 ((char-equal group-write ?-) 0)
7367 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
7368 (cond
7369 ((char-equal group-execute-or-setid ?x)
7370 (tramp-octal-to-decimal "00010"))
7371 ((char-equal group-execute-or-setid ?S)
7372 (tramp-octal-to-decimal "02000"))
7373 ((char-equal group-execute-or-setid ?s)
7374 (tramp-octal-to-decimal "02010"))
7375 ((char-equal group-execute-or-setid ?-) 0)
7376 (t (error "Seventh char `%c' must be one of `xsS-'"
7377 group-execute-or-setid)))
7378 (cond
7379 ((char-equal other-read ?r)
7380 (tramp-octal-to-decimal "00004"))
7381 ((char-equal other-read ?-) 0)
7382 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
7383 (cond
7384 ((char-equal other-write ?w) (tramp-octal-to-decimal "00002"))
7385 ((char-equal other-write ?-) 0)
7386 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
7387 (cond
7388 ((char-equal other-execute-or-sticky ?x)
7389 (tramp-octal-to-decimal "00001"))
7390 ((char-equal other-execute-or-sticky ?T)
7391 (tramp-octal-to-decimal "01000"))
7392 ((char-equal other-execute-or-sticky ?t)
7393 (tramp-octal-to-decimal "01001"))
7394 ((char-equal other-execute-or-sticky ?-) 0)
7395 (t (error "Tenth char `%c' must be one of `xtT-'"
7396 other-execute-or-sticky)))))))
7398 (defun tramp-convert-file-attributes (vec attr)
7399 "Convert file-attributes ATTR generated by perl script, stat or ls.
7400 Convert file mode bits to string and set virtual device number.
7401 Return ATTR."
7402 (when attr
7403 ;; Convert last access time.
7404 (unless (listp (nth 4 attr))
7405 (setcar (nthcdr 4 attr)
7406 (list (floor (nth 4 attr) 65536)
7407 (floor (mod (nth 4 attr) 65536)))))
7408 ;; Convert last modification time.
7409 (unless (listp (nth 5 attr))
7410 (setcar (nthcdr 5 attr)
7411 (list (floor (nth 5 attr) 65536)
7412 (floor (mod (nth 5 attr) 65536)))))
7413 ;; Convert last status change time.
7414 (unless (listp (nth 6 attr))
7415 (setcar (nthcdr 6 attr)
7416 (list (floor (nth 6 attr) 65536)
7417 (floor (mod (nth 6 attr) 65536)))))
7418 ;; Convert file size.
7419 (when (< (nth 7 attr) 0)
7420 (setcar (nthcdr 7 attr) -1))
7421 (when (and (floatp (nth 7 attr))
7422 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
7423 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
7424 ;; Convert file mode bits to string.
7425 (unless (stringp (nth 8 attr))
7426 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
7427 (when (stringp (car attr))
7428 (aset (nth 8 attr) 0 ?l)))
7429 ;; Convert directory indication bit.
7430 (when (string-match "^d" (nth 8 attr))
7431 (setcar attr t))
7432 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
7433 (when (consp (car attr))
7434 (if (and (stringp (caar attr))
7435 (string-match ".+ -> .\\(.+\\)." (caar attr)))
7436 (setcar attr (match-string 1 (caar attr)))
7437 (setcar attr nil)))
7438 ;; Set file's gid change bit.
7439 (setcar (nthcdr 9 attr)
7440 (if (numberp (nth 3 attr))
7441 (not (= (nth 3 attr)
7442 (tramp-get-remote-gid vec 'integer)))
7443 (not (string-equal
7444 (nth 3 attr)
7445 (tramp-get-remote-gid vec 'string)))))
7446 ;; Convert inode.
7447 (unless (listp (nth 10 attr))
7448 (setcar (nthcdr 10 attr)
7449 (condition-case nil
7450 (cons (floor (nth 10 attr) 65536)
7451 (floor (mod (nth 10 attr) 65536)))
7452 ;; Inodes can be incredible huge. We must hide this.
7453 (error (tramp-get-inode vec)))))
7454 ;; Set virtual device number.
7455 (setcar (nthcdr 11 attr)
7456 (tramp-get-device vec))
7457 attr))
7459 (defun tramp-check-cached-permissions (vec access)
7460 "Check `file-attributes' caches for VEC.
7461 Return t if according to the cache access type ACCESS is known to
7462 be granted."
7463 (let ((result nil)
7464 (offset (cond
7465 ((eq ?r access) 1)
7466 ((eq ?w access) 2)
7467 ((eq ?x access) 3))))
7468 (dolist (suffix '("string" "integer") result)
7469 (setq
7470 result
7472 result
7473 (let ((file-attr
7474 (tramp-get-file-property
7475 vec (tramp-file-name-localname vec)
7476 (concat "file-attributes-" suffix) nil))
7477 (remote-uid
7478 (tramp-get-connection-property
7479 vec (concat "uid-" suffix) nil))
7480 (remote-gid
7481 (tramp-get-connection-property
7482 vec (concat "gid-" suffix) nil)))
7483 (and
7484 file-attr
7486 ;; Not a symlink
7487 (eq t (car file-attr))
7488 (null (car file-attr)))
7490 ;; World accessible.
7491 (eq access (aref (nth 8 file-attr) (+ offset 6)))
7492 ;; User accessible and owned by user.
7493 (and
7494 (eq access (aref (nth 8 file-attr) offset))
7495 (equal remote-uid (nth 2 file-attr)))
7496 ;; Group accessible and owned by user's
7497 ;; principal group.
7498 (and
7499 (eq access (aref (nth 8 file-attr) (+ offset 3)))
7500 (equal remote-gid (nth 3 file-attr)))))))))))
7502 (defun tramp-get-inode (vec)
7503 "Returns the virtual inode number.
7504 If it doesn't exist, generate a new one."
7505 (let ((string (tramp-make-tramp-file-name
7506 (tramp-file-name-method vec)
7507 (tramp-file-name-user vec)
7508 (tramp-file-name-host vec)
7509 "")))
7510 (unless (assoc string tramp-inodes)
7511 (add-to-list 'tramp-inodes
7512 (list string (length tramp-inodes))))
7513 (nth 1 (assoc string tramp-inodes))))
7515 (defun tramp-get-device (vec)
7516 "Returns the virtual device number.
7517 If it doesn't exist, generate a new one."
7518 (let ((string (tramp-make-tramp-file-name
7519 (tramp-file-name-method vec)
7520 (tramp-file-name-user vec)
7521 (tramp-file-name-host vec)
7522 "")))
7523 (unless (assoc string tramp-devices)
7524 (add-to-list 'tramp-devices
7525 (list string (length tramp-devices))))
7526 (cons -1 (nth 1 (assoc string tramp-devices)))))
7528 (defun tramp-file-mode-from-int (mode)
7529 "Turn an integer representing a file mode into an ls(1)-like string."
7530 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
7531 (user (logand (lsh mode -6) 7))
7532 (group (logand (lsh mode -3) 7))
7533 (other (logand (lsh mode -0) 7))
7534 (suid (> (logand (lsh mode -9) 4) 0))
7535 (sgid (> (logand (lsh mode -9) 2) 0))
7536 (sticky (> (logand (lsh mode -9) 1) 0)))
7537 (setq user (tramp-file-mode-permissions user suid "s"))
7538 (setq group (tramp-file-mode-permissions group sgid "s"))
7539 (setq other (tramp-file-mode-permissions other sticky "t"))
7540 (concat type user group other)))
7542 (defun tramp-file-mode-permissions (perm suid suid-text)
7543 "Convert a permission bitset into a string.
7544 This is used internally by `tramp-file-mode-from-int'."
7545 (let ((r (> (logand perm 4) 0))
7546 (w (> (logand perm 2) 0))
7547 (x (> (logand perm 1) 0)))
7548 (concat (or (and r "r") "-")
7549 (or (and w "w") "-")
7550 (or (and suid x suid-text) ; suid, execute
7551 (and suid (upcase suid-text)) ; suid, !execute
7552 (and x "x") "-")))) ; !suid
7554 (defun tramp-decimal-to-octal (i)
7555 "Return a string consisting of the octal digits of I.
7556 Not actually used. Use `(format \"%o\" i)' instead?"
7557 (cond ((< i 0) (error "Cannot convert negative number to octal"))
7558 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
7559 ((zerop i) "0")
7560 (t (concat (tramp-decimal-to-octal (/ i 8))
7561 (number-to-string (% i 8))))))
7563 ;; Kudos to Gerd Moellmann for this suggestion.
7564 (defun tramp-octal-to-decimal (ostr)
7565 "Given a string of octal digits, return a decimal number."
7566 (let ((x (or ostr "")))
7567 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
7568 (unless (string-match "\\`[0-7]*\\'" x)
7569 (error "Non-octal junk in string `%s'" x))
7570 (string-to-number ostr 8)))
7572 (defun tramp-shell-case-fold (string)
7573 "Converts STRING to shell glob pattern which ignores case."
7574 (mapconcat
7575 (lambda (c)
7576 (if (equal (downcase c) (upcase c))
7577 (vector c)
7578 (format "[%c%c]" (downcase c) (upcase c))))
7579 string
7580 ""))
7583 ;; ------------------------------------------------------------
7584 ;; -- Tramp file names --
7585 ;; ------------------------------------------------------------
7586 ;; Conversion functions between external representation and
7587 ;; internal data structure. Convenience functions for internal
7588 ;; data structure.
7590 (defun tramp-file-name-p (vec)
7591 "Check whether VEC is a Tramp object."
7592 (and (vectorp vec) (= 4 (length vec))))
7594 (defun tramp-file-name-method (vec)
7595 "Return method component of VEC."
7596 (and (tramp-file-name-p vec) (aref vec 0)))
7598 (defun tramp-file-name-user (vec)
7599 "Return user component of VEC."
7600 (and (tramp-file-name-p vec) (aref vec 1)))
7602 (defun tramp-file-name-host (vec)
7603 "Return host component of VEC."
7604 (and (tramp-file-name-p vec) (aref vec 2)))
7606 (defun tramp-file-name-localname (vec)
7607 "Return localname component of VEC."
7608 (and (tramp-file-name-p vec) (aref vec 3)))
7610 ;; The user part of a Tramp file name vector can be of kind
7611 ;; "user%domain". Sometimes, we must extract these parts.
7612 (defun tramp-file-name-real-user (vec)
7613 "Return the user name of VEC without domain."
7614 (save-match-data
7615 (let ((user (tramp-file-name-user vec)))
7616 (if (and (stringp user)
7617 (string-match tramp-user-with-domain-regexp user))
7618 (match-string 1 user)
7619 user))))
7621 (defun tramp-file-name-domain (vec)
7622 "Return the domain name of VEC."
7623 (save-match-data
7624 (let ((user (tramp-file-name-user vec)))
7625 (and (stringp user)
7626 (string-match tramp-user-with-domain-regexp user)
7627 (match-string 2 user)))))
7629 ;; The host part of a Tramp file name vector can be of kind
7630 ;; "host#port". Sometimes, we must extract these parts.
7631 (defun tramp-file-name-real-host (vec)
7632 "Return the host name of VEC without port."
7633 (save-match-data
7634 (let ((host (tramp-file-name-host vec)))
7635 (if (and (stringp host)
7636 (string-match tramp-host-with-port-regexp host))
7637 (match-string 1 host)
7638 host))))
7640 (defun tramp-file-name-port (vec)
7641 "Return the port number of VEC."
7642 (save-match-data
7643 (let ((host (tramp-file-name-host vec)))
7644 (and (stringp host)
7645 (string-match tramp-host-with-port-regexp host)
7646 (string-to-number (match-string 2 host))))))
7648 (defun tramp-tramp-file-p (name)
7649 "Return t if NAME is a Tramp file."
7650 (save-match-data
7651 (string-match tramp-file-name-regexp name)))
7653 (defun tramp-find-method (method user host)
7654 "Return the right method string to use.
7655 This is METHOD, if non-nil. Otherwise, do a lookup in
7656 `tramp-default-method-alist'."
7657 (or method
7658 (let ((choices tramp-default-method-alist)
7659 lmethod item)
7660 (while choices
7661 (setq item (pop choices))
7662 (when (and (string-match (or (nth 0 item) "") (or host ""))
7663 (string-match (or (nth 1 item) "") (or user "")))
7664 (setq lmethod (nth 2 item))
7665 (setq choices nil)))
7666 lmethod)
7667 tramp-default-method))
7669 (defun tramp-find-user (method user host)
7670 "Return the right user string to use.
7671 This is USER, if non-nil. Otherwise, do a lookup in
7672 `tramp-default-user-alist'."
7673 (or user
7674 (let ((choices tramp-default-user-alist)
7675 luser item)
7676 (while choices
7677 (setq item (pop choices))
7678 (when (and (string-match (or (nth 0 item) "") (or method ""))
7679 (string-match (or (nth 1 item) "") (or host "")))
7680 (setq luser (nth 2 item))
7681 (setq choices nil)))
7682 luser)
7683 tramp-default-user))
7685 (defun tramp-find-host (method user host)
7686 "Return the right host string to use.
7687 This is HOST, if non-nil. Otherwise, it is `tramp-default-host'."
7688 (or (and (> (length host) 0) host)
7689 tramp-default-host))
7691 (defun tramp-dissect-file-name (name &optional nodefault)
7692 "Return a `tramp-file-name' structure.
7693 The structure consists of remote method, remote user, remote host
7694 and localname (file name on remote host). If NODEFAULT is
7695 non-nil, the file name parts are not expanded to their default
7696 values."
7697 (save-match-data
7698 (let ((match (string-match (nth 0 tramp-file-name-structure) name)))
7699 (unless match (error "Not a Tramp file name: %s" name))
7700 (let ((method (match-string (nth 1 tramp-file-name-structure) name))
7701 (user (match-string (nth 2 tramp-file-name-structure) name))
7702 (host (match-string (nth 3 tramp-file-name-structure) name))
7703 (localname (match-string (nth 4 tramp-file-name-structure) name)))
7704 (when (member method '("multi" "multiu"))
7705 (error
7706 "`%s' method is no longer supported, see (info \"(tramp)Multi-hops\")"
7707 method))
7708 (when host
7709 (when (string-match tramp-prefix-ipv6-regexp host)
7710 (setq host (replace-match "" nil t host)))
7711 (when (string-match tramp-postfix-ipv6-regexp host)
7712 (setq host (replace-match "" nil t host))))
7713 (if nodefault
7714 (vector method user host localname)
7715 (vector
7716 (tramp-find-method method user host)
7717 (tramp-find-user method user host)
7718 (tramp-find-host method user host)
7719 localname))))))
7721 (defun tramp-equal-remote (file1 file2)
7722 "Checks, whether the remote parts of FILE1 and FILE2 are identical.
7723 The check depends on method, user and host name of the files. If
7724 one of the components is missing, the default values are used.
7725 The local file name parts of FILE1 and FILE2 are not taken into
7726 account.
7728 Example:
7730 (tramp-equal-remote \"/ssh::/etc\" \"/<your host name>:/home\")
7732 would yield `t'. On the other hand, the following check results in nil:
7734 (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")"
7735 (and (stringp (file-remote-p file1))
7736 (stringp (file-remote-p file2))
7737 (string-equal (file-remote-p file1) (file-remote-p file2))))
7739 (defun tramp-make-tramp-file-name (method user host localname)
7740 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME."
7741 (concat tramp-prefix-format
7742 (when (not (zerop (length method)))
7743 (concat method tramp-postfix-method-format))
7744 (when (not (zerop (length user)))
7745 (concat user tramp-postfix-user-format))
7746 (when host
7747 (if (string-match tramp-ipv6-regexp host)
7748 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
7749 host))
7750 tramp-postfix-host-format
7751 (when localname localname)))
7753 (defun tramp-completion-make-tramp-file-name (method user host localname)
7754 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME.
7755 It must not be a complete Tramp file name, but as long as there are
7756 necessary only. This function will be used in file name completion."
7757 (concat tramp-prefix-format
7758 (when (not (zerop (length method)))
7759 (concat method tramp-postfix-method-format))
7760 (when (not (zerop (length user)))
7761 (concat user tramp-postfix-user-format))
7762 (when (not (zerop (length host)))
7763 (concat
7764 (if (string-match tramp-ipv6-regexp host)
7765 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
7766 host)
7767 tramp-postfix-host-format))
7768 (when localname localname)))
7770 (defun tramp-make-copy-program-file-name (vec)
7771 "Create a file name suitable to be passed to `rcp' and workalikes."
7772 (let ((user (tramp-file-name-user vec))
7773 (host (tramp-file-name-real-host vec))
7774 (localname (tramp-shell-quote-argument
7775 (tramp-file-name-localname vec))))
7776 (if (not (zerop (length user)))
7777 (format "%s@%s:%s" user host localname)
7778 (format "%s:%s" host localname))))
7780 (defun tramp-method-out-of-band-p (vec size)
7781 "Return t if this is an out-of-band method, nil otherwise."
7782 (and
7783 ;; It shall be an out-of-band method.
7784 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
7785 ;; Either the file size is large enough, or (in rare cases) there
7786 ;; does not exist a remote encoding.
7787 (or (> size tramp-copy-size-limit)
7788 (null (tramp-get-remote-coding vec "remote-encoding")))))
7790 (defun tramp-local-host-p (vec)
7791 "Return t if this points to the local host, nil otherwise."
7792 ;; We cannot use `tramp-file-name-real-host'. A port is an
7793 ;; indication for an ssh tunnel or alike.
7794 (let ((host (tramp-file-name-host vec)))
7795 (and
7796 (stringp host)
7797 (string-match tramp-local-host-regexp host)
7798 ;; The method shall be applied to one of the shell file name
7799 ;; handler. `tramp-local-host-p' is also called for "smb" and
7800 ;; alike, where it must fail.
7801 (tramp-get-method-parameter
7802 (tramp-file-name-method vec) 'tramp-login-program)
7803 ;; The local temp directory must be writable for the other user.
7804 (file-writable-p
7805 (tramp-make-tramp-file-name
7806 (tramp-file-name-method vec)
7807 (tramp-file-name-user vec)
7808 host
7809 (tramp-compat-temporary-file-directory)))
7810 ;; On some systems, chown runs only for root.
7811 (or (zerop (user-uid))
7812 (zerop (tramp-get-remote-uid vec 'integer))))))
7814 ;; Variables local to connection.
7816 (defun tramp-get-remote-path (vec)
7817 (with-connection-property
7818 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
7819 ;; cache the result for the session only. Otherwise, the result
7820 ;; is cached persistently.
7821 (if (memq 'tramp-own-remote-path tramp-remote-path)
7822 (tramp-get-connection-process vec)
7823 vec)
7824 "remote-path"
7825 (let* ((remote-path (tramp-compat-copy-tree tramp-remote-path))
7826 (elt1 (memq 'tramp-default-remote-path remote-path))
7827 (elt2 (memq 'tramp-own-remote-path remote-path))
7828 (default-remote-path
7829 (when elt1
7830 (condition-case nil
7831 (tramp-send-command-and-read
7832 vec "echo \\\"`getconf PATH`\\\"")
7833 ;; Default if "getconf" is not available.
7834 (error
7835 (tramp-message
7836 vec 3
7837 "`getconf PATH' not successful, using default value \"%s\"."
7838 "/bin:/usr/bin")
7839 "/bin:/usr/bin"))))
7840 (own-remote-path
7841 (when elt2
7842 (condition-case nil
7843 (tramp-send-command-and-read vec "echo \\\"$PATH\\\"")
7844 ;; Default if "getconf" is not available.
7845 (error
7846 (tramp-message
7847 vec 3 "$PATH not set, ignoring `tramp-own-remote-path'.")
7848 nil)))))
7850 ;; Replace place holder `tramp-default-remote-path'.
7851 (when elt1
7852 (setcdr elt1
7853 (append
7854 (tramp-compat-split-string default-remote-path ":")
7855 (cdr elt1)))
7856 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
7858 ;; Replace place holder `tramp-own-remote-path'.
7859 (when elt2
7860 (setcdr elt2
7861 (append
7862 (tramp-compat-split-string own-remote-path ":")
7863 (cdr elt2)))
7864 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
7866 ;; Remove double entries.
7867 (setq elt1 remote-path)
7868 (while (consp elt1)
7869 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
7870 (setcar elt2 nil))
7871 (setq elt1 (cdr elt1)))
7873 ;; Remove non-existing directories.
7874 (delq
7876 (mapcar
7877 (lambda (x)
7878 (and
7879 (stringp x)
7880 (file-directory-p
7881 (tramp-make-tramp-file-name
7882 (tramp-file-name-method vec)
7883 (tramp-file-name-user vec)
7884 (tramp-file-name-host vec)
7887 remote-path)))))
7889 (defun tramp-get-remote-tmpdir (vec)
7890 (with-connection-property vec "tmp-directory"
7891 (let ((dir (tramp-shell-quote-argument "/tmp")))
7892 (if (and (zerop
7893 (tramp-send-command-and-check
7894 vec (format "%s -d %s" (tramp-get-test-command vec) dir)))
7895 (zerop
7896 (tramp-send-command-and-check
7897 vec (format "%s -w %s" (tramp-get-test-command vec) dir))))
7899 (tramp-error vec 'file-error "Directory %s not accessible" dir)))))
7901 (defun tramp-get-ls-command (vec)
7902 (with-connection-property vec "ls"
7903 (tramp-message vec 5 "Finding a suitable `ls' command")
7905 (catch 'ls-found
7906 (dolist (cmd '("ls" "gnuls" "gls"))
7907 (let ((dl (tramp-get-remote-path vec))
7908 result)
7909 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
7910 ;; Check parameter.
7911 (when (zerop (tramp-send-command-and-check
7912 vec (format "%s -lnd /" result)))
7913 (throw 'ls-found result))
7914 (setq dl (cdr dl))))))
7915 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
7917 (defun tramp-get-ls-command-with-dired (vec)
7918 (save-match-data
7919 (with-connection-property vec "ls-dired"
7920 (tramp-message vec 5 "Checking, whether `ls --dired' works")
7921 (zerop (tramp-send-command-and-check
7922 vec (format "%s --dired /" (tramp-get-ls-command vec)))))))
7924 (defun tramp-get-test-command (vec)
7925 (with-connection-property vec "test"
7926 (tramp-message vec 5 "Finding a suitable `test' command")
7927 (if (zerop (tramp-send-command-and-check vec "test 0"))
7928 "test"
7929 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
7931 (defun tramp-get-test-nt-command (vec)
7932 ;; Does `test A -nt B' work? Use abominable `find' construct if it
7933 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
7934 ;; for otherwise the shell crashes.
7935 (with-connection-property vec "test-nt"
7937 (progn
7938 (tramp-send-command
7939 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
7940 (with-current-buffer (tramp-get-buffer vec)
7941 (goto-char (point-min))
7942 (when (looking-at (regexp-quote tramp-end-of-output))
7943 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
7944 (progn
7945 (tramp-send-command
7947 (format
7948 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
7949 (tramp-get-test-command vec)))
7950 "tramp_test_nt %s %s"))))
7952 (defun tramp-get-file-exists-command (vec)
7953 (with-connection-property vec "file-exists"
7954 (tramp-message vec 5 "Finding command to check if file exists")
7955 (tramp-find-file-exists-command vec)))
7957 (defun tramp-get-remote-ln (vec)
7958 (with-connection-property vec "ln"
7959 (tramp-message vec 5 "Finding a suitable `ln' command")
7960 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
7962 (defun tramp-get-remote-perl (vec)
7963 (with-connection-property vec "perl"
7964 (tramp-message vec 5 "Finding a suitable `perl' command")
7965 (let ((result
7966 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
7967 (tramp-find-executable
7968 vec "perl" (tramp-get-remote-path vec)))))
7969 ;; We must check also for some Perl modules.
7970 (when result
7971 (with-connection-property vec "perl-file-spec"
7972 (zerop
7973 (tramp-send-command-and-check
7974 vec (format "%s -e 'use File::Spec;'" result))))
7975 (with-connection-property vec "perl-cwd-realpath"
7976 (zerop
7977 (tramp-send-command-and-check
7978 vec (format "%s -e 'use Cwd \"realpath\";'" result)))))
7979 result)))
7981 (defun tramp-get-remote-stat (vec)
7982 (with-connection-property vec "stat"
7983 (tramp-message vec 5 "Finding a suitable `stat' command")
7984 (let ((result (tramp-find-executable
7985 vec "stat" (tramp-get-remote-path vec)))
7986 tmp)
7987 ;; Check whether stat(1) returns usable syntax. %s does not
7988 ;; work on older AIX systems.
7989 (when result
7990 (setq tmp
7991 ;; We don't want to display an error message.
7992 (with-temp-message (or (current-message) "")
7993 (condition-case nil
7994 (tramp-send-command-and-read
7995 vec (format "%s -c '(\"%%N\" %%s)' /" result))
7996 (error nil))))
7997 (unless (and (listp tmp) (stringp (car tmp))
7998 (string-match "^./.$" (car tmp))
7999 (integerp (cadr tmp)))
8000 (setq result nil)))
8001 result)))
8003 (defun tramp-get-remote-readlink (vec)
8004 (with-connection-property vec "readlink"
8005 (tramp-message vec 5 "Finding a suitable `readlink' command")
8006 (let ((result (tramp-find-executable
8007 vec "readlink" (tramp-get-remote-path vec))))
8008 (when (and result
8009 ;; We don't want to display an error message.
8010 (with-temp-message (or (current-message) "")
8011 (condition-case nil
8012 (zerop
8013 (tramp-send-command-and-check
8014 vec (format "%s --canonicalize-missing /" result)))
8015 (error nil))))
8016 result))))
8018 (defun tramp-get-remote-id (vec)
8019 (with-connection-property vec "id"
8020 (tramp-message vec 5 "Finding POSIX `id' command")
8022 (catch 'id-found
8023 (let ((dl (tramp-get-remote-path vec))
8024 result)
8025 (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
8026 ;; Check POSIX parameter.
8027 (when (zerop (tramp-send-command-and-check
8028 vec (format "%s -u" result)))
8029 (throw 'id-found result))
8030 (setq dl (cdr dl)))))
8031 (tramp-error vec 'file-error "Couldn't find a POSIX `id' command"))))
8033 (defun tramp-get-remote-uid (vec id-format)
8034 (with-connection-property vec (format "uid-%s" id-format)
8035 (let ((res (tramp-send-command-and-read
8037 (format "%s -u%s %s"
8038 (tramp-get-remote-id vec)
8039 (if (equal id-format 'integer) "" "n")
8040 (if (equal id-format 'integer)
8041 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8042 ;; The command might not always return a number.
8043 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
8045 (defun tramp-get-remote-gid (vec id-format)
8046 (with-connection-property vec (format "gid-%s" id-format)
8047 (let ((res (tramp-send-command-and-read
8049 (format "%s -g%s %s"
8050 (tramp-get-remote-id vec)
8051 (if (equal id-format 'integer) "" "n")
8052 (if (equal id-format 'integer)
8053 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8054 ;; The command might not always return a number.
8055 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
8057 (defun tramp-get-local-uid (id-format)
8058 (if (equal id-format 'integer) (user-uid) (user-login-name)))
8060 (defun tramp-get-local-gid (id-format)
8061 (nth 3 (tramp-compat-file-attributes "~/" id-format)))
8063 ;; Some predefined connection properties.
8064 (defun tramp-get-remote-coding (vec prop)
8065 ;; Local coding handles properties like remote coding. So we could
8066 ;; call it without pain.
8067 (let ((ret (tramp-get-local-coding vec prop)))
8068 ;; The connection property might have been cached. So we must send
8069 ;; the script - maybe.
8070 (when (and ret (symbolp ret))
8071 (let ((name (symbol-name ret)))
8072 (while (string-match (regexp-quote "-") name)
8073 (setq name (replace-match "_" nil t name)))
8074 (tramp-maybe-send-script vec (symbol-value ret) name)
8075 (setq ret name)))
8076 ;; Return the value.
8077 ret))
8079 (defun tramp-get-local-coding (vec prop)
8081 (tramp-get-connection-property vec prop nil)
8082 (progn
8083 (tramp-find-inline-encoding vec)
8084 (tramp-get-connection-property vec prop nil))))
8086 (defun tramp-get-method-parameter (method param)
8087 "Return the method parameter PARAM.
8088 If the `tramp-methods' entry does not exist, return NIL."
8089 (let ((entry (assoc param (assoc method tramp-methods))))
8090 (when entry (cadr entry))))
8092 ;; Auto saving to a special directory.
8094 (defun tramp-exists-file-name-handler (operation &rest args)
8095 "Checks whether OPERATION runs a file name handler."
8096 ;; The file name handler is determined on base of either an
8097 ;; argument, `buffer-file-name', or `default-directory'.
8098 (condition-case nil
8099 (let* ((buffer-file-name "/")
8100 (default-directory "/")
8101 (fnha file-name-handler-alist)
8102 (check-file-name-operation operation)
8103 (file-name-handler-alist
8104 (list
8105 (cons "/"
8106 (lambda (operation &rest args)
8107 "Returns OPERATION if it is the one to be checked."
8108 (if (equal check-file-name-operation operation)
8109 operation
8110 (let ((file-name-handler-alist fnha))
8111 (apply operation args))))))))
8112 (equal (apply operation args) operation))
8113 (error nil)))
8115 (unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
8116 (defadvice make-auto-save-file-name
8117 (around tramp-advice-make-auto-save-file-name () activate)
8118 "Invoke `tramp-handle-make-auto-save-file-name' for Tramp files."
8119 (if (and (buffer-file-name) (tramp-tramp-file-p (buffer-file-name)))
8120 ;; We cannot call `tramp-handle-make-auto-save-file-name'
8121 ;; directly, because this would bypass the locking mechanism.
8122 (setq ad-return-value
8123 (tramp-file-name-handler 'make-auto-save-file-name))
8124 ad-do-it))
8125 (add-hook 'tramp-unload-hook
8126 (lambda () (ad-unadvise 'make-auto-save-file-name))))
8128 ;; In Emacs < 22 and XEmacs < 21.5 autosaved remote files have
8129 ;; permission 0666 minus umask. This is a security threat.
8131 (defun tramp-set-auto-save-file-modes ()
8132 "Set permissions of autosaved remote files to the original permissions."
8133 (let ((bfn (buffer-file-name)))
8134 (when (and (stringp bfn)
8135 (tramp-tramp-file-p bfn)
8136 (buffer-modified-p)
8137 (stringp buffer-auto-save-file-name)
8138 (not (equal bfn buffer-auto-save-file-name)))
8139 (unless (file-exists-p buffer-auto-save-file-name)
8140 (write-region "" nil buffer-auto-save-file-name))
8141 ;; Permissions should be set always, because there might be an old
8142 ;; auto-saved file belonging to another original file. This could
8143 ;; be a security threat.
8144 (set-file-modes buffer-auto-save-file-name
8145 (or (file-modes bfn) (tramp-octal-to-decimal "0600"))))))
8147 (unless (or (> emacs-major-version 21)
8148 (and (featurep 'xemacs)
8149 (= emacs-major-version 21)
8150 (> emacs-minor-version 4)))
8151 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes)
8152 (add-hook 'tramp-unload-hook
8153 (lambda ()
8154 (remove-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))))
8156 (defun tramp-subst-strs-in-string (alist string)
8157 "Replace all occurrences of the string FROM with TO in STRING.
8158 ALIST is of the form ((FROM . TO) ...)."
8159 (save-match-data
8160 (while alist
8161 (let* ((pr (car alist))
8162 (from (car pr))
8163 (to (cdr pr)))
8164 (while (string-match (regexp-quote from) string)
8165 (setq string (replace-match to t t string)))
8166 (setq alist (cdr alist))))
8167 string))
8169 ;; ------------------------------------------------------------
8170 ;; -- Compatibility functions section --
8171 ;; ------------------------------------------------------------
8173 (defun tramp-read-passwd (proc &optional prompt)
8174 "Read a password from user (compat function).
8175 Consults the auth-source package.
8176 Invokes `password-read' if available, `read-passwd' else."
8177 (let* ((key (tramp-make-tramp-file-name
8178 tramp-current-method tramp-current-user
8179 tramp-current-host ""))
8180 (pw-prompt
8181 (or prompt
8182 (with-current-buffer (process-buffer proc)
8183 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
8184 (format "%s for %s " (capitalize (match-string 1)) key)))))
8185 (prog1
8187 ;; See if auth-sources contains something useful, if it's bound.
8188 (and (boundp 'auth-sources)
8189 (tramp-get-connection-property proc "first-password-request" nil)
8190 ;; Try with Tramp's current method.
8191 (funcall (symbol-function 'auth-source-user-or-password)
8192 "password" tramp-current-host tramp-current-method))
8193 ;; Try the password cache.
8194 (when (functionp 'password-read)
8195 (unless (tramp-get-connection-property
8196 proc "first-password-request" nil)
8197 (funcall (symbol-function 'password-cache-remove) key))
8198 (let ((password
8199 (funcall (symbol-function 'password-read) pw-prompt key)))
8200 (funcall (symbol-function 'password-cache-add) key password)
8201 password))
8202 ;; Else, get the password interactively.
8203 (read-passwd pw-prompt))
8204 (tramp-set-connection-property proc "first-password-request" nil))))
8206 (defun tramp-clear-passwd (vec)
8207 "Clear password cache for connection related to VEC."
8208 (when (functionp 'password-cache-remove)
8209 (funcall
8210 (symbol-function 'password-cache-remove)
8211 (tramp-make-tramp-file-name
8212 (tramp-file-name-method vec)
8213 (tramp-file-name-user vec)
8214 (tramp-file-name-host vec)
8215 ""))))
8217 ;; Snarfed code from time-date.el and parse-time.el
8219 (defconst tramp-half-a-year '(241 17024)
8220 "Evaluated by \"(days-to-time 183)\".")
8222 (defconst tramp-parse-time-months
8223 '(("jan" . 1) ("feb" . 2) ("mar" . 3)
8224 ("apr" . 4) ("may" . 5) ("jun" . 6)
8225 ("jul" . 7) ("aug" . 8) ("sep" . 9)
8226 ("oct" . 10) ("nov" . 11) ("dec" . 12))
8227 "Alist mapping month names to integers.")
8229 (defun tramp-time-less-p (t1 t2)
8230 "Say whether time value T1 is less than time value T2."
8231 (unless t1 (setq t1 '(0 0)))
8232 (unless t2 (setq t2 '(0 0)))
8233 (or (< (car t1) (car t2))
8234 (and (= (car t1) (car t2))
8235 (< (nth 1 t1) (nth 1 t2)))))
8237 (defun tramp-time-subtract (t1 t2)
8238 "Subtract two time values.
8239 Return the difference in the format of a time value."
8240 (unless t1 (setq t1 '(0 0)))
8241 (unless t2 (setq t2 '(0 0)))
8242 (let ((borrow (< (cadr t1) (cadr t2))))
8243 (list (- (car t1) (car t2) (if borrow 1 0))
8244 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
8246 (defun tramp-time-diff (t1 t2)
8247 "Return the difference between the two times, in seconds.
8248 T1 and T2 are time values (as returned by `current-time' for example)."
8249 ;; Pacify byte-compiler with `symbol-function'.
8250 (cond ((and (fboundp 'subtract-time)
8251 (fboundp 'float-time))
8252 (funcall (symbol-function 'float-time)
8253 (funcall (symbol-function 'subtract-time) t1 t2)))
8254 ((and (fboundp 'subtract-time)
8255 (fboundp 'time-to-seconds))
8256 (funcall (symbol-function 'time-to-seconds)
8257 (funcall (symbol-function 'subtract-time) t1 t2)))
8258 ((fboundp 'itimer-time-difference)
8259 (funcall (symbol-function 'itimer-time-difference)
8260 (if (< (length t1) 3) (append t1 '(0)) t1)
8261 (if (< (length t2) 3) (append t2 '(0)) t2)))
8263 (let ((time (tramp-time-subtract t1 t2)))
8264 (+ (* (car time) 65536.0)
8265 (cadr time)
8266 (/ (or (nth 2 time) 0) 1000000.0))))))
8268 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
8269 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
8270 EOL-TYPE can be one of `dos', `unix', or `mac'."
8271 (cond ((fboundp 'coding-system-change-eol-conversion)
8272 (funcall (symbol-function 'coding-system-change-eol-conversion)
8273 coding-system eol-type))
8274 ((fboundp 'subsidiary-coding-system)
8275 (funcall (symbol-function 'subsidiary-coding-system)
8276 coding-system
8277 (cond ((eq eol-type 'dos) 'crlf)
8278 ((eq eol-type 'unix) 'lf)
8279 ((eq eol-type 'mac) 'cr)
8281 (error "Unknown EOL-TYPE `%s', must be %s"
8282 eol-type
8283 "`dos', `unix', or `mac'")))))
8284 (t (error "Can't change EOL conversion -- is MULE missing?"))))
8286 (defun tramp-set-process-query-on-exit-flag (process flag)
8287 "Specify if query is needed for process when Emacs is exited.
8288 If the second argument flag is non-nil, Emacs will query the user before
8289 exiting if process is running."
8290 (if (fboundp 'set-process-query-on-exit-flag)
8291 (funcall (symbol-function 'set-process-query-on-exit-flag) process flag)
8292 (funcall (symbol-function 'process-kill-without-query) process flag)))
8295 ;; ------------------------------------------------------------
8296 ;; -- Kludges section --
8297 ;; ------------------------------------------------------------
8299 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
8300 ;; does not deal well with newline characters. Newline is replaced by
8301 ;; backslash newline. But if, say, the string `a backslash newline b'
8302 ;; is passed to a shell, the shell will expand this into "ab",
8303 ;; completely omitting the newline. This is not what was intended.
8304 ;; It does not appear to be possible to make the function
8305 ;; `shell-quote-argument' work with newlines without making it
8306 ;; dependent on the shell used. But within this package, we know that
8307 ;; we will always use a Bourne-like shell, so we use an approach which
8308 ;; groks newlines.
8310 ;; The approach is simple: we call `shell-quote-argument', then
8311 ;; massage the newline part of the result.
8313 ;; This function should produce a string which is grokked by a Unix
8314 ;; shell, even if the Emacs is running on Windows. Since this is the
8315 ;; kludges section, we bind `system-type' in such a way that
8316 ;; `shell-quote-arguments' behaves as if on Unix.
8318 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
8319 ;; function to work with Bourne-like shells.
8321 ;; CCC: This function should be rewritten so that
8322 ;; `shell-quote-argument' is not used. This way, we are safe from
8323 ;; changes in `shell-quote-argument'.
8324 (defun tramp-shell-quote-argument (s)
8325 "Similar to `shell-quote-argument', but groks newlines.
8326 Only works for Bourne-like shells."
8327 (let ((system-type 'not-windows))
8328 (save-match-data
8329 (let ((result (shell-quote-argument s))
8330 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
8331 (when (and (>= (length result) 2)
8332 (string= (substring result 0 2) "\\~"))
8333 (setq result (substring result 1)))
8334 (while (string-match nl result)
8335 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
8336 t t result)))
8337 result))))
8339 ;; We currently (sometimes) use "[" and "]" in the filename format.
8340 ;; This means that Emacs wants to expand wildcards if
8341 ;; `find-file-wildcards' is non-nil, and then barfs because no
8342 ;; expansion could be found. We detect this situation and do
8343 ;; something really awful: we have `file-expand-wildcards' return the
8344 ;; original filename if it can't expand anything. Let's just hope
8345 ;; that this doesn't break anything else.
8346 ;; CCC: This check is now also really awful; we should search all
8347 ;; of the filename format, not just the prefix.
8348 (when (string-match "\\[" tramp-prefix-format)
8349 (defadvice file-expand-wildcards
8350 (around tramp-advice-file-expand-wildcards activate)
8351 (let ((name (ad-get-arg 0)))
8352 (if (tramp-tramp-file-p name)
8353 ;; If it's a Tramp file, dissect it and look if wildcards
8354 ;; need to be expanded at all.
8355 (if (string-match
8356 "[[*?]"
8357 (tramp-file-name-localname (tramp-dissect-file-name name)))
8358 (setq ad-return-value (or ad-do-it (list name)))
8359 (setq ad-return-value (list name)))
8360 ;; If it is not a Tramp file, just run the original function.
8361 (setq ad-return-value (or ad-do-it (list name))))))
8362 (add-hook 'tramp-unload-hook
8363 (lambda () (ad-unadvise 'file-expand-wildcards))))
8365 ;; Checklist for `tramp-unload-hook'
8366 ;; - Unload all `tramp-*' packages
8367 ;; - Reset `file-name-handler-alist'
8368 ;; - Cleanup hooks where Tramp functions are in
8369 ;; - Cleanup advised functions
8370 ;; - Cleanup autoloads
8371 ;;;###autoload
8372 (defun tramp-unload-tramp ()
8373 "Discard Tramp from loading remote files."
8374 (interactive)
8375 ;; When Tramp is not loaded yet, its autoloads are still active.
8376 (tramp-unload-file-name-handlers)
8377 ;; ange-ftp settings must be enabled.
8378 (when (functionp 'tramp-ftp-enable-ange-ftp)
8379 (funcall (symbol-function 'tramp-ftp-enable-ange-ftp)))
8380 ;; Maybe its not loaded yet.
8381 (condition-case nil
8382 (unload-feature 'tramp 'force)
8383 (error nil)))
8385 (when (and load-in-progress
8386 (string-match "Loading tramp..." (or (current-message) "")))
8387 (message "Loading tramp...done"))
8389 (provide 'tramp)
8391 ;;; TODO:
8393 ;; * Handle nonlocal exits such as C-g.
8394 ;; * But it would probably be better to use with-local-quit at the
8395 ;; place where it's actually needed: around any potentially
8396 ;; indefinitely blocking piece of code. In this case it would be
8397 ;; within Tramp around one of its calls to accept-process-output (or
8398 ;; around one of the loops that calls accept-process-output)
8399 ;; (Stefan Monnier).
8400 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
8401 ;; `shell-quote-argument'.
8402 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
8403 ;; by the files in that directory. Add this here.
8404 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
8405 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
8406 ;; * Case-insensitive filename completion. (Norbert Goevert.)
8407 ;; * Don't use globbing for directories with many files, as this is
8408 ;; likely to produce long command lines, and some shells choke on
8409 ;; long command lines.
8410 ;; * `vc-directory' does not work. It never displays any files, even
8411 ;; if it does show files when run locally.
8412 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
8413 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
8414 ;; * abbreviate-file-name
8415 ;; * Better error checking. At least whenever we see something
8416 ;; strange when doing zerop, we should kill the process and start
8417 ;; again. (Greg Stark)
8418 ;; * Remove unneeded parameters from methods.
8419 ;; * Make it work for different encodings, and for different file name
8420 ;; encodings, too. (Daniel Pittman)
8421 ;; * Progress reports while copying files. (Michael Kifer)
8422 ;; * Don't search for perl5 and perl. Instead, only search for perl and
8423 ;; then look if it's the right version (with `perl -v').
8424 ;; * When editing a remote CVS controlled file as a different user, VC
8425 ;; gets confused about the file locking status. Try to find out why
8426 ;; the workaround doesn't work.
8427 ;; * Username and hostname completion.
8428 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode-p'.
8429 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
8430 ;; Code is nearly identical.
8431 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
8432 ;; until the last but one hop via `start-file-process'. Apply it
8433 ;; also for ftp and smb.
8434 ;; * WIBNI if we had a command "trampclient"? If I was editing in
8435 ;; some shell with root priviledges, it would be nice if I could
8436 ;; just call
8437 ;; trampclient filename.c
8438 ;; as an editor, and the _current_ shell would connect to an Emacs
8439 ;; server and would be used in an existing non-priviledged Emacs
8440 ;; session for doing the editing in question.
8441 ;; That way, I need not tell Emacs my password again and be afraid
8442 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
8443 ;; once display a just typed password in the context of a keyboard
8444 ;; sequence prompt for a question immediately following in a shell
8445 ;; script run within Emacs -- nasty).
8446 ;; And if I have some ssh session running to a different computer,
8447 ;; having the possibility of passing a local file there to a local
8448 ;; Emacs session (in case I can arrange for a connection back) would
8449 ;; be nice.
8450 ;; Likely the corresponding Tramp server should not allow the
8451 ;; equivalent of the emacsclient -eval option in order to make this
8452 ;; reasonably unproblematic. And maybe trampclient should have some
8453 ;; way of passing credentials, like by using an SSL socket or
8454 ;; something. (David Kastrup)
8455 ;; * Reconnect directly to a compliant shell without first going
8456 ;; through the user's default shell. (Pete Forman)
8457 ;; * Make `tramp-default-user' obsolete.
8458 ;; * Tramp shall reconnect automatically to its ssh connection when it
8459 ;; detects that the process "has died". (David Reitter)
8460 ;; * How can I interrupt the remote process with a signal
8461 ;; (interrupt-process seems not to work)? (Markus Triska)
8462 ;; * Avoid the local shell entirely for starting remote processes. If
8463 ;; so, I think even a signal, when delivered directly to the local
8464 ;; SSH instance, would correctly be propagated to the remote process
8465 ;; automatically; possibly SSH would have to be started with
8466 ;; "-t". (Markus Triska)
8467 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
8468 ;; isn't on the remote host. (Mark A. Hershberger)
8469 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
8470 ;; * Implement a general server-local-variable mechanism, as there are
8471 ;; probably other variables that need different values for different
8472 ;; servers too. The user could then configure a variable (such as
8473 ;; tramp-server-local-variable-alist) to define any such variables
8474 ;; that they need to, which would then be let bound as appropriate
8475 ;; in tramp functions. (Jason Rumney)
8476 ;; * Optimize out-of-band copying, when both methods are scp-like (not
8477 ;; rsync).
8478 ;; * Keep a second connection open for out-of-band methods like scp or
8479 ;; rsync.
8480 ;; * Support ptys in `tramp-handle-start-file-process'.
8482 ;; Functions for file-name-handler-alist:
8483 ;; diff-latest-backup-file -- in diff.el
8485 ;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
8486 ;;; tramp.el ends here
8488 ;; Local Variables:
8489 ;; mode: Emacs-Lisp
8490 ;; coding: utf-8
8491 ;; End: