]> code.delx.au - gnu-emacs/blob - lisp/net/tramp.el
Fix stack overflow in string creation (Bug#6214).
[gnu-emacs] / lisp / net / tramp.el
1 ;;; tramp.el --- Transparent Remote Access, Multiple Protocol
2
3 ;; Copyright (C) 1998, 1999, 2000, 2001, 2002, 2003, 2004,
4 ;; 2005, 2006, 2007, 2008, 2009, 2010 Free Software Foundation, Inc.
5
6 ;; (copyright statements below in code to be updated with the above notice)
7
8 ;; Author: Kai Großjohann <kai.grossjohann@gmx.net>
9 ;; Michael Albinus <michael.albinus@gmx.de>
10 ;; Keywords: comm, processes
11
12 ;; This file is part of GNU Emacs.
13
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.
18
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.
23
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/>.
26
27 ;;; Commentary:
28
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.
33 ;;
34 ;; For more detailed instructions, please see the info file.
35 ;;
36 ;; Notes:
37 ;; -----
38 ;;
39 ;; This package only works for Emacs 22.1 and higher, and for XEmacs 21.4
40 ;; and higher. For XEmacs 21, you need the package `fsf-compat' for
41 ;; the `with-timeout' macro.
42 ;;
43 ;; Also see the todo list at the bottom of this file.
44 ;;
45 ;; The current version of Tramp can be retrieved from the following URL:
46 ;; http://ftp.gnu.org/gnu/tramp/
47 ;;
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
52 ;;
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.
57 ;;
58 ;; Don't forget to put on your asbestos longjohns, first!
59
60 ;;; Code:
61
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..."))
68
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))))
75
76 (require 'tramp-compat)
77 (add-hook 'tramp-unload-hook
78 (lambda ()
79 (when (featurep 'tramp-compat)
80 (unload-feature 'tramp-compat 'force))))
81
82 (require 'format-spec)
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
89
90 (require 'shell)
91 (require 'advice)
92
93 (eval-and-compile
94 (if (featurep 'xemacs)
95 (load "auth-source" 'noerror)
96 (require 'auth-source nil 'noerror)))
97
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))))
111
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))))
118
119 (autoload 'uudecode-decode-region "uudecode")
120
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
127
128 ;; Tramp interactive commands.
129 'tramp-cmds
130
131 ;; Load foreign FTP method.
132 (if (featurep 'xemacs) 'tramp-efs 'tramp-ftp)
133
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)
139
140 ;; Load foreign FISH method.
141 'tramp-fish
142
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 (tramp-compat-funcall 'dbus-get-unique-name :session)
149 (error nil))
150 (tramp-compat-process-running-p "gvfs-fuse-daemon"))
151 'tramp-gvfs)
152
153 ;; Load gateways. It needs `make-network-process' from Emacs 22.
154 (when (functionp 'make-network-process) 'tramp-gw)
155
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)))
160
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)))))
173
174 ;;; User Customizable Internal Variables:
175
176 (defgroup tramp nil
177 "Edit remote files with a combination of rsh and rcp or similar programs."
178 :group 'files
179 :group 'comm
180 :version "22.1")
181
182 ;; Maybe we need once a real Tramp mode, with key bindings etc.
183 ;;;###autoload
184 (defcustom tramp-mode t
185 "*Whether Tramp is enabled.
186 If it is set to nil, all remote file names are used literally."
187 :group 'tramp
188 :type 'boolean)
189
190 (defcustom tramp-verbose 3
191 "*Verbosity level for Tramp messages.
192 Any level x includes messages for all levels 1 .. x-1. The levels are
193
194 0 silent (no tramp messages at all)
195 1 errors
196 2 warnings
197 3 connection to remote hosts (default level)
198 4 activities
199 5 internal
200 6 sent and received strings
201 7 file caching
202 8 connection properties
203 10 traces (huge)."
204 :group 'tramp
205 :type 'integer)
206
207 ;; Emacs case.
208 (eval-and-compile
209 (when (boundp 'backup-directory-alist)
210 (defcustom tramp-backup-directory-alist nil
211 "Alist of filename patterns and backup directory names.
212 Each element looks like (REGEXP . DIRECTORY), with the same meaning like
213 in `backup-directory-alist'. If a Tramp file is backed up, and DIRECTORY
214 is a local file name, the backup directory is prepended with Tramp file
215 name prefix \(method, user, host\) of file.
216
217 \(setq tramp-backup-directory-alist backup-directory-alist\)
218
219 gives the same backup policy for Tramp files on their hosts like the
220 policy for local files."
221 :group 'tramp
222 :type '(repeat (cons (regexp :tag "Regexp matching filename")
223 (directory :tag "Backup directory name"))))))
224
225 ;; XEmacs case. We cannot check for `bkup-backup-directory-info', because
226 ;; the package "backup-dir" might not be loaded yet.
227 (eval-and-compile
228 (when (featurep 'xemacs)
229 (defcustom tramp-bkup-backup-directory-info nil
230 "*Alist of (FILE-REGEXP BACKUP-DIR OPTIONS ...))
231 It has the same meaning like `bkup-backup-directory-info' from package
232 `backup-dir'. If a Tramp file is backed up, and BACKUP-DIR is a local
233 file name, the backup directory is prepended with Tramp file name prefix
234 \(method, user, host\) of file.
235
236 \(setq tramp-bkup-backup-directory-info bkup-backup-directory-info\)
237
238 gives the same backup policy for Tramp files on their hosts like the
239 policy for local files."
240 :type '(repeat
241 (list (regexp :tag "File regexp")
242 (string :tag "Backup Dir")
243 (set :inline t
244 (const ok-create)
245 (const full-path)
246 (const prepend-name)
247 (const search-upward))))
248 :group 'tramp)))
249
250 (defcustom tramp-auto-save-directory nil
251 "*Put auto-save files in this directory, if set.
252 The idea is to use a local directory so that auto-saving is faster."
253 :group 'tramp
254 :type '(choice (const nil) string))
255
256 (defcustom tramp-encoding-shell
257 (if (memq system-type '(windows-nt))
258 (getenv "COMSPEC")
259 "/bin/sh")
260 "*Use this program for encoding and decoding commands on the local host.
261 This shell is used to execute the encoding and decoding command on the
262 local host, so if you want to use `~' in those commands, you should
263 choose a shell here which groks tilde expansion. `/bin/sh' normally
264 does not understand tilde expansion.
265
266 For encoding and deocding, commands like the following are executed:
267
268 /bin/sh -c COMMAND < INPUT > OUTPUT
269
270 This variable can be used to change the \"/bin/sh\" part. See the
271 variable `tramp-encoding-command-switch' for the \"-c\" part.
272
273 Note that this variable is not used for remote commands. There are
274 mechanisms in tramp.el which automatically determine the right shell to
275 use for the remote host."
276 :group 'tramp
277 :type '(file :must-match t))
278
279 (defcustom tramp-encoding-command-switch
280 (if (string-match "cmd\\.exe" tramp-encoding-shell)
281 "/c"
282 "-c")
283 "*Use this switch together with `tramp-encoding-shell' for local commands.
284 See the variable `tramp-encoding-shell' for more information."
285 :group 'tramp
286 :type 'string)
287
288 (defcustom tramp-inline-compress-start-size 4096
289 "*The minimum size of compressing where inline transfer.
290 When inline transfer, compress transfered data of file
291 whose size is this value or above (up to `tramp-copy-size-limit').
292 If it is nil, no compression at all will be applied."
293 :group 'tramp
294 :type '(choice (const nil) integer))
295
296 (defcustom tramp-copy-size-limit 10240
297 "*The maximum file size where inline copying is preferred over an out-of-the-band copy.
298 If it is nil, inline out-of-the-band copy will be used without a check."
299 :group 'tramp
300 :type '(choice (const nil) integer))
301
302 (defcustom tramp-terminal-type "dumb"
303 "*Value of TERM environment variable for logging in to remote host.
304 Because Tramp wants to parse the output of the remote shell, it is easily
305 confused by ANSI color escape sequences and suchlike. Often, shell init
306 files conditionalize this setup based on the TERM environment variable."
307 :group 'tramp
308 :type 'string)
309
310 ;; ksh on OpenBSD 4.5 requires, that PS1 contains a `#' character for
311 ;; root users. It uses the `$' character for other users. In order
312 ;; to guarantee a proper prompt, we use "#$" for the prompt.
313
314 (defvar tramp-end-of-output
315 (format
316 "///%s#$"
317 (md5 (concat (prin1-to-string process-environment) (current-time-string))))
318 "String used to recognize end of output.
319 The '$' character at the end is quoted; the string cannot be
320 detected as prompt when being sent on echoing hosts, therefore.")
321
322 (defconst tramp-initial-end-of-output "#$ "
323 "Prompt when establishing a connection.")
324
325 (defvar tramp-methods
326 `(("rcp" (tramp-login-program "rsh")
327 (tramp-login-args (("%h") ("-l" "%u")))
328 (tramp-remote-sh "/bin/sh")
329 (tramp-copy-program "rcp")
330 (tramp-copy-args (("-p" "%k") ("-r")))
331 (tramp-copy-keep-date t)
332 (tramp-copy-recursive t)
333 (tramp-password-end-of-line nil))
334 ("scp" (tramp-login-program "ssh")
335 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
336 ("-e" "none")))
337 (tramp-remote-sh "/bin/sh")
338 (tramp-copy-program "scp")
339 (tramp-copy-args (("-P" "%p") ("-p" "%k")
340 ("-q") ("-r")))
341 (tramp-copy-keep-date t)
342 (tramp-copy-recursive t)
343 (tramp-password-end-of-line nil)
344 (tramp-gw-args (("-o"
345 "GlobalKnownHostsFile=/dev/null")
346 ("-o" "UserKnownHostsFile=/dev/null")
347 ("-o" "StrictHostKeyChecking=no")))
348 (tramp-default-port 22))
349 ("scp1" (tramp-login-program "ssh")
350 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
351 ("-1" "-e" "none")))
352 (tramp-remote-sh "/bin/sh")
353 (tramp-copy-program "scp")
354 (tramp-copy-args (("-1") ("-P" "%p") ("-p" "%k")
355 ("-q") ("-r")))
356 (tramp-copy-keep-date t)
357 (tramp-copy-recursive t)
358 (tramp-password-end-of-line nil)
359 (tramp-gw-args (("-o"
360 "GlobalKnownHostsFile=/dev/null")
361 ("-o" "UserKnownHostsFile=/dev/null")
362 ("-o" "StrictHostKeyChecking=no")))
363 (tramp-default-port 22))
364 ("scp2" (tramp-login-program "ssh")
365 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
366 ("-2" "-e" "none")))
367 (tramp-remote-sh "/bin/sh")
368 (tramp-copy-program "scp")
369 (tramp-copy-args (("-2") ("-P" "%p") ("-p" "%k")
370 ("-q") ("-r")))
371 (tramp-copy-keep-date t)
372 (tramp-copy-recursive t)
373 (tramp-password-end-of-line nil)
374 (tramp-gw-args (("-o"
375 "GlobalKnownHostsFile=/dev/null")
376 ("-o" "UserKnownHostsFile=/dev/null")
377 ("-o" "StrictHostKeyChecking=no")))
378 (tramp-default-port 22))
379 ("scp1_old"
380 (tramp-login-program "ssh1")
381 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
382 ("-e" "none")))
383 (tramp-remote-sh "/bin/sh")
384 (tramp-copy-program "scp1")
385 (tramp-copy-args (("-p" "%k") ("-r")))
386 (tramp-copy-keep-date t)
387 (tramp-copy-recursive t)
388 (tramp-password-end-of-line nil))
389 ("scp2_old"
390 (tramp-login-program "ssh2")
391 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
392 ("-e" "none")))
393 (tramp-remote-sh "/bin/sh")
394 (tramp-copy-program "scp2")
395 (tramp-copy-args (("-p" "%k") ("-r")))
396 (tramp-copy-keep-date t)
397 (tramp-copy-recursive t)
398 (tramp-password-end-of-line nil))
399 ("sftp" (tramp-login-program "ssh")
400 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
401 ("-e" "none")))
402 (tramp-remote-sh "/bin/sh")
403 (tramp-copy-program "sftp")
404 (tramp-copy-args nil)
405 (tramp-copy-keep-date nil)
406 (tramp-password-end-of-line nil))
407 ("rsync" (tramp-login-program "ssh")
408 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
409 ("-e" "none")))
410 (tramp-remote-sh "/bin/sh")
411 (tramp-copy-program "rsync")
412 (tramp-copy-args (("-e" "ssh") ("-t" "%k") ("-r")))
413 (tramp-copy-keep-date t)
414 (tramp-copy-keep-tmpfile t)
415 (tramp-copy-recursive t)
416 (tramp-password-end-of-line nil))
417 ("rsyncc"
418 (tramp-login-program "ssh")
419 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
420 ("-o" "ControlPath=%t.%%r@%%h:%%p")
421 ("-o" "ControlMaster=yes")
422 ("-e" "none")))
423 (tramp-remote-sh "/bin/sh")
424 (tramp-copy-program "rsync")
425 (tramp-copy-args (("-t" "%k") ("-r")))
426 (tramp-copy-env (("RSYNC_RSH")
427 (,(concat
428 "ssh"
429 " -o ControlPath=%t.%%r@%%h:%%p"
430 " -o ControlMaster=auto"))))
431 (tramp-copy-keep-date t)
432 (tramp-copy-keep-tmpfile t)
433 (tramp-copy-recursive t)
434 (tramp-password-end-of-line nil))
435 ("remcp" (tramp-login-program "remsh")
436 (tramp-login-args (("%h") ("-l" "%u")))
437 (tramp-remote-sh "/bin/sh")
438 (tramp-copy-program "rcp")
439 (tramp-copy-args (("-p" "%k")))
440 (tramp-copy-keep-date t)
441 (tramp-password-end-of-line nil))
442 ("rsh" (tramp-login-program "rsh")
443 (tramp-login-args (("%h") ("-l" "%u")))
444 (tramp-remote-sh "/bin/sh")
445 (tramp-copy-program nil)
446 (tramp-copy-args nil)
447 (tramp-copy-keep-date nil)
448 (tramp-password-end-of-line nil))
449 ("ssh" (tramp-login-program "ssh")
450 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
451 ("-e" "none")))
452 (tramp-remote-sh "/bin/sh")
453 (tramp-copy-program nil)
454 (tramp-copy-args nil)
455 (tramp-copy-keep-date nil)
456 (tramp-password-end-of-line nil)
457 (tramp-gw-args (("-o"
458 "GlobalKnownHostsFile=/dev/null")
459 ("-o" "UserKnownHostsFile=/dev/null")
460 ("-o" "StrictHostKeyChecking=no")))
461 (tramp-default-port 22))
462 ("ssh1" (tramp-login-program "ssh")
463 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
464 ("-1" "-e" "none")))
465 (tramp-remote-sh "/bin/sh")
466 (tramp-copy-program nil)
467 (tramp-copy-args nil)
468 (tramp-copy-keep-date nil)
469 (tramp-password-end-of-line nil)
470 (tramp-gw-args (("-o"
471 "GlobalKnownHostsFile=/dev/null")
472 ("-o" "UserKnownHostsFile=/dev/null")
473 ("-o" "StrictHostKeyChecking=no")))
474 (tramp-default-port 22))
475 ("ssh2" (tramp-login-program "ssh")
476 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
477 ("-2" "-e" "none")))
478 (tramp-remote-sh "/bin/sh")
479 (tramp-copy-program nil)
480 (tramp-copy-args nil)
481 (tramp-copy-keep-date nil)
482 (tramp-password-end-of-line nil)
483 (tramp-gw-args (("-o"
484 "GlobalKnownHostsFile=/dev/null")
485 ("-o" "UserKnownHostsFile=/dev/null")
486 ("-o" "StrictHostKeyChecking=no")))
487 (tramp-default-port 22))
488 ("ssh1_old"
489 (tramp-login-program "ssh1")
490 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
491 ("-e" "none")))
492 (tramp-remote-sh "/bin/sh")
493 (tramp-copy-program nil)
494 (tramp-copy-args nil)
495 (tramp-copy-keep-date nil)
496 (tramp-password-end-of-line nil))
497 ("ssh2_old"
498 (tramp-login-program "ssh2")
499 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
500 ("-e" "none")))
501 (tramp-remote-sh "/bin/sh")
502 (tramp-copy-program nil)
503 (tramp-copy-args nil)
504 (tramp-copy-keep-date nil)
505 (tramp-password-end-of-line nil))
506 ("remsh" (tramp-login-program "remsh")
507 (tramp-login-args (("%h") ("-l" "%u")))
508 (tramp-remote-sh "/bin/sh")
509 (tramp-copy-program nil)
510 (tramp-copy-args nil)
511 (tramp-copy-keep-date nil)
512 (tramp-password-end-of-line nil))
513 ("telnet"
514 (tramp-login-program "telnet")
515 (tramp-login-args (("%h") ("%p")))
516 (tramp-remote-sh "/bin/sh")
517 (tramp-copy-program nil)
518 (tramp-copy-args nil)
519 (tramp-copy-keep-date nil)
520 (tramp-password-end-of-line nil)
521 (tramp-default-port 23))
522 ("su" (tramp-login-program "su")
523 (tramp-login-args (("-") ("%u")))
524 (tramp-remote-sh "/bin/sh")
525 (tramp-copy-program nil)
526 (tramp-copy-args nil)
527 (tramp-copy-keep-date nil)
528 (tramp-password-end-of-line nil))
529 ("sudo" (tramp-login-program "sudo")
530 (tramp-login-args (("-u" "%u")
531 ("-s") ("-H") ("-p" "Password:")))
532 (tramp-remote-sh "/bin/sh")
533 (tramp-copy-program nil)
534 (tramp-copy-args nil)
535 (tramp-copy-keep-date nil)
536 (tramp-password-end-of-line nil))
537 ("scpc" (tramp-login-program "ssh")
538 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
539 ("-o" "ControlPath=%t.%%r@%%h:%%p")
540 ("-o" "ControlMaster=yes")
541 ("-e" "none")))
542 (tramp-remote-sh "/bin/sh")
543 (tramp-copy-program "scp")
544 (tramp-copy-args (("-P" "%p") ("-p" "%k") ("-q")
545 ("-o" "ControlPath=%t.%%r@%%h:%%p")
546 ("-o" "ControlMaster=auto")))
547 (tramp-copy-keep-date t)
548 (tramp-password-end-of-line nil)
549 (tramp-gw-args (("-o"
550 "GlobalKnownHostsFile=/dev/null")
551 ("-o" "UserKnownHostsFile=/dev/null")
552 ("-o" "StrictHostKeyChecking=no")))
553 (tramp-default-port 22))
554 ("scpx" (tramp-login-program "ssh")
555 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
556 ("-e" "none" "-t" "-t" "/bin/sh")))
557 (tramp-remote-sh "/bin/sh")
558 (tramp-copy-program "scp")
559 (tramp-copy-args (("-p" "%k")))
560 (tramp-copy-keep-date t)
561 (tramp-password-end-of-line nil)
562 (tramp-gw-args (("-o"
563 "GlobalKnownHostsFile=/dev/null")
564 ("-o" "UserKnownHostsFile=/dev/null")
565 ("-o" "StrictHostKeyChecking=no")))
566 (tramp-default-port 22))
567 ("sshx" (tramp-login-program "ssh")
568 (tramp-login-args (("%h") ("-l" "%u") ("-p" "%p")
569 ("-e" "none" "-t" "-t" "/bin/sh")))
570 (tramp-remote-sh "/bin/sh")
571 (tramp-copy-program nil)
572 (tramp-copy-args nil)
573 (tramp-copy-keep-date nil)
574 (tramp-password-end-of-line nil)
575 (tramp-gw-args (("-o"
576 "GlobalKnownHostsFile=/dev/null")
577 ("-o" "UserKnownHostsFile=/dev/null")
578 ("-o" "StrictHostKeyChecking=no")))
579 (tramp-default-port 22))
580 ("krlogin"
581 (tramp-login-program "krlogin")
582 (tramp-login-args (("%h") ("-l" "%u") ("-x")))
583 (tramp-remote-sh "/bin/sh")
584 (tramp-copy-program nil)
585 (tramp-copy-args nil)
586 (tramp-copy-keep-date nil)
587 (tramp-password-end-of-line nil))
588 ("plink" (tramp-login-program "plink")
589 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
590 ("-ssh")))
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 "xy") ;see docstring for "xy"
596 (tramp-default-port 22))
597 ("plink1"
598 (tramp-login-program "plink")
599 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
600 ("-1" "-ssh")))
601 (tramp-remote-sh "/bin/sh")
602 (tramp-copy-program nil)
603 (tramp-copy-args nil)
604 (tramp-copy-keep-date nil)
605 (tramp-password-end-of-line "xy") ;see docstring for "xy"
606 (tramp-default-port 22))
607 ("plinkx"
608 (tramp-login-program "plink")
609 ;; ("%h") must be a single element, see
610 ;; `tramp-compute-multi-hops'.
611 (tramp-login-args (("-load") ("%h") ("-t")
612 (,(format
613 "env 'TERM=%s' 'PROMPT_COMMAND=' 'PS1=%s'"
614 tramp-terminal-type
615 tramp-initial-end-of-output))
616 ("/bin/sh")))
617 (tramp-remote-sh "/bin/sh")
618 (tramp-copy-program nil)
619 (tramp-copy-args nil)
620 (tramp-copy-keep-date nil)
621 (tramp-password-end-of-line nil))
622 ("pscp" (tramp-login-program "plink")
623 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
624 ("-ssh")))
625 (tramp-remote-sh "/bin/sh")
626 (tramp-copy-program "pscp")
627 (tramp-copy-args (("-P" "%p") ("-scp") ("-p" "%k")))
628 (tramp-copy-keep-date t)
629 (tramp-password-end-of-line "xy") ;see docstring for "xy"
630 (tramp-default-port 22))
631 ("psftp" (tramp-login-program "plink")
632 (tramp-login-args (("%h") ("-l" "%u") ("-P" "%p")
633 ("-ssh")))
634 (tramp-remote-sh "/bin/sh")
635 (tramp-copy-program "pscp")
636 (tramp-copy-args (("-P" "%p") ("-sftp") ("-p" "%k")))
637 (tramp-copy-keep-date t)
638 (tramp-password-end-of-line "xy")) ;see docstring for "xy"
639 ("fcp" (tramp-login-program "fsh")
640 (tramp-login-args (("%h") ("-l" "%u") ("sh" "-i")))
641 (tramp-remote-sh "/bin/sh -i")
642 (tramp-copy-program "fcp")
643 (tramp-copy-args (("-p" "%k")))
644 (tramp-copy-keep-date t)
645 (tramp-password-end-of-line nil)))
646 "*Alist of methods for remote files.
647 This is a list of entries of the form (NAME PARAM1 PARAM2 ...).
648 Each NAME stands for a remote access method. Each PARAM is a
649 pair of the form (KEY VALUE). The following KEYs are defined:
650 * `tramp-remote-sh'
651 This specifies the Bourne shell to use on the remote host. This
652 MUST be a Bourne-like shell. It is normally not necessary to set
653 this to any value other than \"/bin/sh\": Tramp wants to use a shell
654 which groks tilde expansion, but it can search for it. Also note
655 that \"/bin/sh\" exists on all Unixen, this might not be true for
656 the value that you decide to use. You Have Been Warned.
657 * `tramp-login-program'
658 This specifies the name of the program to use for logging in to the
659 remote host. This may be the name of rsh or a workalike program,
660 or the name of telnet or a workalike, or the name of su or a workalike.
661 * `tramp-login-args'
662 This specifies the list of arguments to pass to the above
663 mentioned program. Please note that this is a list of list of arguments,
664 that is, normally you don't want to put \"-a -b\" or \"-f foo\"
665 here. Instead, you want a list (\"-a\" \"-b\"), or (\"-f\" \"foo\").
666 There are some patterns: \"%h\" in this list is replaced by the host
667 name, \"%u\" is replaced by the user name, \"%p\" is replaced by the
668 port number, and \"%%\" can be used to obtain a literal percent character.
669 If a list containing \"%h\", \"%u\" or \"%p\" is unchanged during
670 expansion (i.e. no host or no user specified), this list is not used as
671 argument. By this, arguments like (\"-l\" \"%u\") are optional.
672 \"%t\" is replaced by the temporary file name produced with
673 `tramp-make-tramp-temp-file'. \"%k\" indicates the keep-date
674 parameter of a program, if exists.
675 * `tramp-copy-program'
676 This specifies the name of the program to use for remotely copying
677 the file; this might be the absolute filename of rcp or the name of
678 a workalike program.
679 * `tramp-copy-args'
680 This specifies the list of parameters to pass to the above mentioned
681 program, the hints for `tramp-login-args' also apply here.
682 * `tramp-copy-keep-date'
683 This specifies whether the copying program when the preserves the
684 timestamp of the original file.
685 * `tramp-copy-keep-tmpfile'
686 This specifies whether a temporary local file shall be kept
687 for optimization reasons (useful for \"rsync\" methods).
688 * `tramp-copy-recursive'
689 Whether the operation copies directories recursively.
690 * `tramp-default-port'
691 The default port of a method is needed in case of gateway connections.
692 Additionally, it is used as indication which method is prepared for
693 passing gateways.
694 * `tramp-gw-args'
695 As the attribute name says, additional arguments are specified here
696 when a method is applied via a gateway.
697 * `tramp-password-end-of-line'
698 This specifies the string to use for terminating the line after
699 submitting the password. If this method parameter is nil, then the
700 value of the normal variable `tramp-default-password-end-of-line'
701 is used. This parameter is necessary because the \"plink\" program
702 requires any two characters after sending the password. These do
703 not have to be newline or carriage return characters. Other login
704 programs are happy with just one character, the newline character.
705 We use \"xy\" as the value for methods using \"plink\".
706
707 What does all this mean? Well, you should specify `tramp-login-program'
708 for all methods; this program is used to log in to the remote site. Then,
709 there are two ways to actually transfer the files between the local and the
710 remote side. One way is using an additional rcp-like program. If you want
711 to do this, set `tramp-copy-program' in the method.
712
713 Another possibility for file transfer is inline transfer, i.e. the
714 file is passed through the same buffer used by `tramp-login-program'. In
715 this case, the file contents need to be protected since the
716 `tramp-login-program' might use escape codes or the connection might not
717 be eight-bit clean. Therefore, file contents are encoded for transit.
718 See the variables `tramp-local-coding-commands' and
719 `tramp-remote-coding-commands' for details.
720
721 So, to summarize: if the method is an out-of-band method, then you
722 must specify `tramp-copy-program' and `tramp-copy-args'. If it is an
723 inline method, then these two parameters should be nil. Methods which
724 are fit for gateways must have `tramp-default-port' at least.
725
726 Notes:
727
728 When using `su' or `sudo' the phrase `open connection to a remote
729 host' sounds strange, but it is used nevertheless, for consistency.
730 No connection is opened to a remote host, but `su' or `sudo' is
731 started on the local host. You should specify a remote host
732 `localhost' or the name of the local host. Another host name is
733 useful only in combination with `tramp-default-proxies-alist'.")
734
735 (defun tramp-detect-ssh-controlmaster ()
736 "Call ssh to detect whether it supports the ControlMaster argument.
737 This function may return nil when the argument is supported, but
738 shouldn't return t when it isn't."
739 (ignore-errors
740 (with-temp-buffer
741 (call-process "ssh" nil t nil "-o" "ControlMaster")
742 (goto-char (point-min))
743 (search-forward-regexp "Missing ControlMaster argument" nil t))))
744
745 (defcustom tramp-default-method
746 ;; An external copy method seems to be preferred, because it is much
747 ;; more performant for large files, and it hasn't too serious delays
748 ;; for small files. But it must be ensured that there aren't
749 ;; permanent password queries. Either a password agent like
750 ;; "ssh-agent" or "Pageant" shall run, or the optional
751 ;; password-cache.el or auth-sources.el packages shall be active for
752 ;; password caching. "scpc" is chosen if we detect that the user is
753 ;; running OpenSSH 4.0 or newer.
754 (cond
755 ;; PuTTY is installed.
756 ((executable-find "pscp")
757 (if (or (fboundp 'password-read)
758 (fboundp 'auth-source-user-or-password)
759 ;; Pageant is running.
760 (tramp-compat-process-running-p "Pageant"))
761 "pscp"
762 "plink"))
763 ;; There is an ssh installation.
764 ((executable-find "scp")
765 (cond
766 ((tramp-detect-ssh-controlmaster) "scpc")
767 ((or (fboundp 'password-read)
768 (fboundp 'auth-source-user-or-password)
769 ;; ssh-agent is running.
770 (getenv "SSH_AUTH_SOCK")
771 (getenv "SSH_AGENT_PID"))
772 "scp")
773 (t "ssh")))
774 ;; Fallback.
775 (t "ftp"))
776 "*Default method to use for transferring files.
777 See `tramp-methods' for possibilities.
778 Also see `tramp-default-method-alist'."
779 :group 'tramp
780 :type 'string)
781
782 (defcustom tramp-default-method-alist
783 '(("\\`localhost\\'" "\\`root\\'" "su"))
784 "*Default method to use for specific host/user pairs.
785 This is an alist of items (HOST USER METHOD). The first matching item
786 specifies the method to use for a file name which does not specify a
787 method. HOST and USER are regular expressions or nil, which is
788 interpreted as a regular expression which always matches. If no entry
789 matches, the variable `tramp-default-method' takes effect.
790
791 If the file name does not specify the user, lookup is done using the
792 empty string for the user name.
793
794 See `tramp-methods' for a list of possibilities for METHOD."
795 :group 'tramp
796 :type '(repeat (list (regexp :tag "Host regexp")
797 (regexp :tag "User regexp")
798 (string :tag "Method"))))
799
800 (defcustom tramp-default-user
801 nil
802 "*Default user to use for transferring files.
803 It is nil by default; otherwise settings in configuration files like
804 \"~/.ssh/config\" would be overwritten. Also see `tramp-default-user-alist'.
805
806 This variable is regarded as obsolete, and will be removed soon."
807 :group 'tramp
808 :type '(choice (const nil) string))
809
810 (defcustom tramp-default-user-alist
811 `(("\\`su\\(do\\)?\\'" nil "root")
812 ("\\`r\\(em\\)?\\(cp\\|sh\\)\\|telnet\\|plink1?\\'"
813 nil ,(user-login-name)))
814 "*Default user to use for specific method/host pairs.
815 This is an alist of items (METHOD HOST USER). The first matching item
816 specifies the user to use for a file name which does not specify a
817 user. METHOD and USER are regular expressions or nil, which is
818 interpreted as a regular expression which always matches. If no entry
819 matches, the variable `tramp-default-user' takes effect.
820
821 If the file name does not specify the method, lookup is done using the
822 empty string for the method name."
823 :group 'tramp
824 :type '(repeat (list (regexp :tag "Method regexp")
825 (regexp :tag "Host regexp")
826 (string :tag "User"))))
827
828 (defcustom tramp-default-host
829 (system-name)
830 "*Default host to use for transferring files.
831 Useful for su and sudo methods mostly."
832 :group 'tramp
833 :type 'string)
834
835 (defcustom tramp-default-proxies-alist nil
836 "*Route to be followed for specific host/user pairs.
837 This is an alist of items (HOST USER PROXY). The first matching
838 item specifies the proxy to be passed for a file name located on
839 a remote target matching USER@HOST. HOST and USER are regular
840 expressions. PROXY must be a Tramp filename without a localname
841 part. Method and user name on PROXY are optional, which is
842 interpreted with the default values. PROXY can contain the
843 patterns %h and %u, which are replaced by the strings matching
844 HOST or USER, respectively.
845
846 HOST, USER or PROXY could also be Lisp forms, which will be
847 evaluated. The result must be a string or nil, which is
848 interpreted as a regular expression which always matches."
849 :group 'tramp
850 :type '(repeat (list (choice :tag "Host regexp" regexp sexp)
851 (choice :tag "User regexp" regexp sexp)
852 (choice :tag "Proxy remote name" string (const nil)))))
853
854 (defconst tramp-local-host-regexp
855 (concat
856 "^" (regexp-opt (list "localhost" (system-name) "127\.0\.0\.1" "::1") t) "$")
857 "*Host names which are regarded as local host.")
858
859 (defconst tramp-completion-function-alist-rsh
860 '((tramp-parse-rhosts "/etc/hosts.equiv")
861 (tramp-parse-rhosts "~/.rhosts"))
862 "Default list of (FUNCTION FILE) pairs to be examined for rsh methods.")
863
864 (defconst tramp-completion-function-alist-ssh
865 '((tramp-parse-rhosts "/etc/hosts.equiv")
866 (tramp-parse-rhosts "/etc/shosts.equiv")
867 (tramp-parse-shosts "/etc/ssh_known_hosts")
868 (tramp-parse-sconfig "/etc/ssh_config")
869 (tramp-parse-shostkeys "/etc/ssh2/hostkeys")
870 (tramp-parse-sknownhosts "/etc/ssh2/knownhosts")
871 (tramp-parse-rhosts "~/.rhosts")
872 (tramp-parse-rhosts "~/.shosts")
873 (tramp-parse-shosts "~/.ssh/known_hosts")
874 (tramp-parse-sconfig "~/.ssh/config")
875 (tramp-parse-shostkeys "~/.ssh2/hostkeys")
876 (tramp-parse-sknownhosts "~/.ssh2/knownhosts"))
877 "Default list of (FUNCTION FILE) pairs to be examined for ssh methods.")
878
879 (defconst tramp-completion-function-alist-telnet
880 '((tramp-parse-hosts "/etc/hosts"))
881 "Default list of (FUNCTION FILE) pairs to be examined for telnet methods.")
882
883 (defconst tramp-completion-function-alist-su
884 '((tramp-parse-passwd "/etc/passwd"))
885 "Default list of (FUNCTION FILE) pairs to be examined for su methods.")
886
887 (defconst tramp-completion-function-alist-putty
888 '((tramp-parse-putty
889 "HKEY_CURRENT_USER\\Software\\SimonTatham\\PuTTY\\Sessions"))
890 "Default list of (FUNCTION REGISTRY) pairs to be examined for putty methods.")
891
892 (defvar tramp-completion-function-alist nil
893 "*Alist of methods for remote files.
894 This is a list of entries of the form \(NAME PAIR1 PAIR2 ...\).
895 Each NAME stands for a remote access method. Each PAIR is of the form
896 \(FUNCTION FILE\). FUNCTION is responsible to extract user names and host
897 names from FILE for completion. The following predefined FUNCTIONs exists:
898
899 * `tramp-parse-rhosts' for \"~/.rhosts\" like files,
900 * `tramp-parse-shosts' for \"~/.ssh/known_hosts\" like files,
901 * `tramp-parse-sconfig' for \"~/.ssh/config\" like files,
902 * `tramp-parse-shostkeys' for \"~/.ssh2/hostkeys/*\" like files,
903 * `tramp-parse-sknownhosts' for \"~/.ssh2/knownhosts/*\" like files,
904 * `tramp-parse-hosts' for \"/etc/hosts\" like files,
905 * `tramp-parse-passwd' for \"/etc/passwd\" like files.
906 * `tramp-parse-netrc' for \"~/.netrc\" like files.
907 * `tramp-parse-putty' for PuTTY registry keys.
908
909 FUNCTION can also be a customer defined function. For more details see
910 the info pages.")
911
912 (eval-after-load "tramp"
913 '(progn
914 (tramp-set-completion-function
915 "rcp" tramp-completion-function-alist-rsh)
916 (tramp-set-completion-function
917 "scp" tramp-completion-function-alist-ssh)
918 (tramp-set-completion-function
919 "scp1" tramp-completion-function-alist-ssh)
920 (tramp-set-completion-function
921 "scp2" tramp-completion-function-alist-ssh)
922 (tramp-set-completion-function
923 "scp1_old" tramp-completion-function-alist-ssh)
924 (tramp-set-completion-function
925 "scp2_old" tramp-completion-function-alist-ssh)
926 (tramp-set-completion-function
927 "rsync" tramp-completion-function-alist-ssh)
928 (tramp-set-completion-function
929 "rsyncc" tramp-completion-function-alist-ssh)
930 (tramp-set-completion-function
931 "remcp" tramp-completion-function-alist-rsh)
932 (tramp-set-completion-function
933 "rsh" tramp-completion-function-alist-rsh)
934 (tramp-set-completion-function
935 "ssh" tramp-completion-function-alist-ssh)
936 (tramp-set-completion-function
937 "ssh1" tramp-completion-function-alist-ssh)
938 (tramp-set-completion-function
939 "ssh2" tramp-completion-function-alist-ssh)
940 (tramp-set-completion-function
941 "ssh1_old" tramp-completion-function-alist-ssh)
942 (tramp-set-completion-function
943 "ssh2_old" tramp-completion-function-alist-ssh)
944 (tramp-set-completion-function
945 "remsh" tramp-completion-function-alist-rsh)
946 (tramp-set-completion-function
947 "telnet" tramp-completion-function-alist-telnet)
948 (tramp-set-completion-function
949 "su" tramp-completion-function-alist-su)
950 (tramp-set-completion-function
951 "sudo" tramp-completion-function-alist-su)
952 (tramp-set-completion-function
953 "scpx" tramp-completion-function-alist-ssh)
954 (tramp-set-completion-function
955 "sshx" tramp-completion-function-alist-ssh)
956 (tramp-set-completion-function
957 "krlogin" tramp-completion-function-alist-rsh)
958 (tramp-set-completion-function
959 "plink" tramp-completion-function-alist-ssh)
960 (tramp-set-completion-function
961 "plink1" tramp-completion-function-alist-ssh)
962 (tramp-set-completion-function
963 "plinkx" tramp-completion-function-alist-putty)
964 (tramp-set-completion-function
965 "pscp" tramp-completion-function-alist-ssh)
966 (tramp-set-completion-function
967 "fcp" tramp-completion-function-alist-ssh)))
968
969 (defconst tramp-echo-mark-marker "_echo"
970 "String marker to surround echoed commands.")
971
972 (defconst tramp-echo-mark-marker-length (length tramp-echo-mark-marker)
973 "String length of `tramp-echo-mark-marker'.")
974
975 (defconst tramp-echo-mark
976 (concat tramp-echo-mark-marker
977 (make-string tramp-echo-mark-marker-length ?\b))
978 "String mark to be transmitted around shell commands.
979 Used to separate their echo from the output they produce. This
980 will only be used if we cannot disable remote echo via stty.
981 This string must have no effect on the remote shell except for
982 producing some echo which can later be detected by
983 `tramp-echoed-echo-mark-regexp'. Using `tramp-echo-mark-marker',
984 followed by an equal number of backspaces to erase them will
985 usually suffice.")
986
987 (defconst tramp-echoed-echo-mark-regexp
988 (format "%s\\(\b\\( \b\\)?\\)\\{%d\\}"
989 tramp-echo-mark-marker tramp-echo-mark-marker-length)
990 "Regexp which matches `tramp-echo-mark' as it gets echoed by
991 the remote shell.")
992
993 (defcustom tramp-rsh-end-of-line "\n"
994 "*String used for end of line in rsh connections.
995 I don't think this ever needs to be changed, so please tell me about it
996 if you need to change this.
997 Also see the method parameter `tramp-password-end-of-line' and the normal
998 variable `tramp-default-password-end-of-line'."
999 :group 'tramp
1000 :type 'string)
1001
1002 (defcustom tramp-default-password-end-of-line
1003 tramp-rsh-end-of-line
1004 "*String used for end of line after sending a password.
1005 This variable provides the default value for the method parameter
1006 `tramp-password-end-of-line', see `tramp-methods' for more details.
1007
1008 It seems that people using plink under Windows need to send
1009 \"\\r\\n\" (carriage-return, then newline) after a password, but just
1010 \"\\n\" after all other lines. This variable can be used for the
1011 password, see `tramp-rsh-end-of-line' for the other cases.
1012
1013 The default value is to use the same value as `tramp-rsh-end-of-line'."
1014 :group 'tramp
1015 :type 'string)
1016
1017 ;; "getconf PATH" yields:
1018 ;; HP-UX: /usr/bin:/usr/ccs/bin:/opt/ansic/bin:/opt/langtools/bin:/opt/fortran/bin
1019 ;; Solaris: /usr/xpg4/bin:/usr/ccs/bin:/usr/bin:/opt/SUNWspro/bin
1020 ;; GNU/Linux (Debian, Suse): /bin:/usr/bin
1021 ;; FreeBSD: /usr/bin:/bin:/usr/sbin:/sbin: - beware trailing ":"!
1022 (defcustom tramp-remote-path
1023 '(tramp-default-remote-path "/usr/sbin" "/usr/local/bin"
1024 "/local/bin" "/local/freeware/bin" "/local/gnu/bin"
1025 "/usr/freeware/bin" "/usr/pkg/bin" "/usr/contrib/bin")
1026 "*List of directories to search for executables on remote host.
1027 For every remote host, this variable will be set buffer local,
1028 keeping the list of existing directories on that host.
1029
1030 You can use `~' in this list, but when searching for a shell which groks
1031 tilde expansion, all directory names starting with `~' will be ignored.
1032
1033 `Default Directories' represent the list of directories given by
1034 the command \"getconf PATH\". It is recommended to use this
1035 entry on top of this list, because these are the default
1036 directories for POSIX compatible commands.
1037
1038 `Private Directories' are the settings of the $PATH environment,
1039 as given in your `~/.profile'."
1040 :group 'tramp
1041 :type '(repeat (choice
1042 (const :tag "Default Directories" tramp-default-remote-path)
1043 (const :tag "Private Directories" tramp-own-remote-path)
1044 (string :tag "Directory"))))
1045
1046 (defcustom tramp-remote-process-environment
1047 `("HISTFILE=$HOME/.tramp_history" "HISTSIZE=1" "LC_ALL=C"
1048 ,(format "TERM=%s" tramp-terminal-type)
1049 "EMACS=t" ;; Deprecated.
1050 ,(format "INSIDE_EMACS=%s,tramp:%s" emacs-version tramp-version)
1051 "CDPATH=" "HISTORY=" "MAIL=" "MAILCHECK=" "MAILPATH="
1052 "autocorrect=" "correct=")
1053
1054 "*List of environment variables to be set on the remote host.
1055
1056 Each element should be a string of the form ENVVARNAME=VALUE. An
1057 entry ENVVARNAME= diables the corresponding environment variable,
1058 which might have been set in the init files like ~/.profile.
1059
1060 Special handling is applied to the PATH environment, which should
1061 not be set here. Instead of, it should be set via `tramp-remote-path'."
1062 :group 'tramp
1063 :type '(repeat string))
1064
1065 (defcustom tramp-login-prompt-regexp
1066 ".*ogin\\( .*\\)?: *"
1067 "*Regexp matching login-like prompts.
1068 The regexp should match at end of buffer.
1069
1070 Sometimes the prompt is reported to look like \"login as:\"."
1071 :group 'tramp
1072 :type 'regexp)
1073
1074 (defcustom tramp-shell-prompt-pattern
1075 ;; Allow a prompt to start right after a ^M since it indeed would be
1076 ;; displayed at the beginning of the line (and Zsh uses it).
1077 "\\(?:^\\|\r\\)[^#$%>\n]*#?[#$%>] *\\(\e\\[[0-9;]*[a-zA-Z] *\\)*"
1078 "Regexp to match prompts from remote shell.
1079 Normally, Tramp expects you to configure `shell-prompt-pattern'
1080 correctly, but sometimes it happens that you are connecting to a
1081 remote host which sends a different kind of shell prompt. Therefore,
1082 Tramp recognizes things matched by `shell-prompt-pattern' as prompt,
1083 and also things matched by this variable. The default value of this
1084 variable is similar to the default value of `shell-prompt-pattern',
1085 which should work well in many cases.
1086
1087 This regexp must match both `tramp-initial-end-of-output' and
1088 `tramp-end-of-output'."
1089 :group 'tramp
1090 :type 'regexp)
1091
1092 (defcustom tramp-password-prompt-regexp
1093 "^.*\\([pP]assword\\|[pP]assphrase\\).*:\^@? *"
1094 "*Regexp matching password-like prompts.
1095 The regexp should match at end of buffer.
1096
1097 The `sudo' program appears to insert a `^@' character into the prompt."
1098 :group 'tramp
1099 :type 'regexp)
1100
1101 (defcustom tramp-wrong-passwd-regexp
1102 (concat "^.*"
1103 ;; These strings should be on the last line
1104 (regexp-opt '("Permission denied"
1105 "Login incorrect"
1106 "Login Incorrect"
1107 "Connection refused"
1108 "Connection closed"
1109 "Timeout, server not responding."
1110 "Sorry, try again."
1111 "Name or service not known"
1112 "Host key verification failed."
1113 "No supported authentication methods left to try!") t)
1114 ".*"
1115 "\\|"
1116 "^.*\\("
1117 ;; Here comes a list of regexes, separated by \\|
1118 "Received signal [0-9]+"
1119 "\\).*")
1120 "*Regexp matching a `login failed' message.
1121 The regexp should match at end of buffer."
1122 :group 'tramp
1123 :type 'regexp)
1124
1125 (defcustom tramp-yesno-prompt-regexp
1126 (concat
1127 (regexp-opt '("Are you sure you want to continue connecting (yes/no)?") t)
1128 "\\s-*")
1129 "Regular expression matching all yes/no queries which need to be confirmed.
1130 The confirmation should be done with yes or no.
1131 The regexp should match at end of buffer.
1132 See also `tramp-yn-prompt-regexp'."
1133 :group 'tramp
1134 :type 'regexp)
1135
1136 (defcustom tramp-yn-prompt-regexp
1137 (concat
1138 (regexp-opt '("Store key in cache? (y/n)"
1139 "Update cached key? (y/n, Return cancels connection)") t)
1140 "\\s-*")
1141 "Regular expression matching all y/n queries which need to be confirmed.
1142 The confirmation should be done with y or n.
1143 The regexp should match at end of buffer.
1144 See also `tramp-yesno-prompt-regexp'."
1145 :group 'tramp
1146 :type 'regexp)
1147
1148 (defcustom tramp-terminal-prompt-regexp
1149 (concat "\\("
1150 "TERM = (.*)"
1151 "\\|"
1152 "Terminal type\\? \\[.*\\]"
1153 "\\)\\s-*")
1154 "Regular expression matching all terminal setting prompts.
1155 The regexp should match at end of buffer.
1156 The answer will be provided by `tramp-action-terminal', which see."
1157 :group 'tramp
1158 :type 'regexp)
1159
1160 (defcustom tramp-operation-not-permitted-regexp
1161 (concat "\\(" "preserving times.*" "\\|" "set mode" "\\)" ":\\s-*"
1162 (regexp-opt '("Operation not permitted") t))
1163 "Regular expression matching keep-date problems in (s)cp operations.
1164 Copying has been performed successfully already, so this message can
1165 be ignored safely."
1166 :group 'tramp
1167 :type 'regexp)
1168
1169 (defcustom tramp-copy-failed-regexp
1170 (concat "\\(.+: "
1171 (regexp-opt '("Permission denied"
1172 "not a regular file"
1173 "is a directory"
1174 "No such file or directory") t)
1175 "\\)\\s-*")
1176 "Regular expression matching copy problems in (s)cp operations."
1177 :group 'tramp
1178 :type 'regexp)
1179
1180 (defcustom tramp-process-alive-regexp
1181 ""
1182 "Regular expression indicating a process has finished.
1183 In fact this expression is empty by intention, it will be used only to
1184 check regularly the status of the associated process.
1185 The answer will be provided by `tramp-action-process-alive',
1186 `tramp-action-out-of-band', which see."
1187 :group 'tramp
1188 :type 'regexp)
1189
1190 (defcustom tramp-temp-name-prefix "tramp."
1191 "*Prefix to use for temporary files.
1192 If this is a relative file name (such as \"tramp.\"), it is considered
1193 relative to the directory name returned by the function
1194 `tramp-compat-temporary-file-directory' (which see). It may also be an
1195 absolute file name; don't forget to include a prefix for the filename
1196 part, though."
1197 :group 'tramp
1198 :type 'string)
1199
1200 (defconst tramp-temp-buffer-name " *tramp temp*"
1201 "Buffer name for a temporary buffer.
1202 It shall be used in combination with `generate-new-buffer-name'.")
1203
1204 (defvar tramp-temp-buffer-file-name nil
1205 "File name of a persistent local temporary file.
1206 Useful for \"rsync\" like methods.")
1207 (make-variable-buffer-local 'tramp-temp-buffer-file-name)
1208
1209 (defcustom tramp-sh-extra-args '(("/bash\\'" . "-norc -noprofile"))
1210 "*Alist specifying extra arguments to pass to the remote shell.
1211 Entries are (REGEXP . ARGS) where REGEXP is a regular expression
1212 matching the shell file name and ARGS is a string specifying the
1213 arguments.
1214
1215 This variable is only used when Tramp needs to start up another shell
1216 for tilde expansion. The extra arguments should typically prevent the
1217 shell from reading its init file."
1218 :group 'tramp
1219 ;; This might be the wrong way to test whether the widget type
1220 ;; `alist' is available. Who knows the right way to test it?
1221 :type (if (get 'alist 'widget-type)
1222 '(alist :key-type string :value-type string)
1223 '(repeat (cons string string))))
1224
1225 ;; XEmacs is distributed with few Lisp packages. Further packages are
1226 ;; installed using EFS. If we use a unified filename format, then
1227 ;; Tramp is required in addition to EFS. (But why can't Tramp just
1228 ;; disable EFS when Tramp is loaded? Then XEmacs can ship with EFS
1229 ;; just like before.) Another reason for using a separate filename
1230 ;; syntax on XEmacs is that EFS hooks into XEmacs in many places, but
1231 ;; Tramp only knows how to deal with `file-name-handler-alist', not
1232 ;; the other places.
1233
1234 ;; Currently, we have the choice between 'ftp, 'sep, and 'url.
1235 ;;;###autoload
1236 (defcustom tramp-syntax
1237 (if (featurep 'xemacs) 'sep 'ftp)
1238 "Tramp filename syntax to be used.
1239
1240 It can have the following values:
1241
1242 'ftp -- Ange-FTP respective EFS like syntax (GNU Emacs default)
1243 'sep -- Syntax as defined for XEmacs (not available yet for GNU Emacs)
1244 'url -- URL-like syntax."
1245 :group 'tramp
1246 :type (if (featurep 'xemacs)
1247 '(choice (const :tag "EFS" ftp)
1248 (const :tag "XEmacs" sep)
1249 (const :tag "URL" url))
1250 '(choice (const :tag "Ange-FTP" ftp)
1251 (const :tag "URL" url))))
1252
1253 (defconst tramp-prefix-format
1254 (cond ((equal tramp-syntax 'ftp) "/")
1255 ((equal tramp-syntax 'sep) "/[")
1256 ((equal tramp-syntax 'url) "/")
1257 (t (error "Wrong `tramp-syntax' defined")))
1258 "*String matching the very beginning of Tramp file names.
1259 Used in `tramp-make-tramp-file-name'.")
1260
1261 (defconst tramp-prefix-regexp
1262 (concat "^" (regexp-quote tramp-prefix-format))
1263 "*Regexp matching the very beginning of Tramp file names.
1264 Should always start with \"^\". Derived from `tramp-prefix-format'.")
1265
1266 (defconst tramp-method-regexp
1267 "[a-zA-Z_0-9-]+"
1268 "*Regexp matching methods identifiers.")
1269
1270 (defconst tramp-postfix-method-format
1271 (cond ((equal tramp-syntax 'ftp) ":")
1272 ((equal tramp-syntax 'sep) "/")
1273 ((equal tramp-syntax 'url) "://")
1274 (t (error "Wrong `tramp-syntax' defined")))
1275 "*String matching delimeter between method and user or host names.
1276 Used in `tramp-make-tramp-file-name'.")
1277
1278 (defconst tramp-postfix-method-regexp
1279 (regexp-quote tramp-postfix-method-format)
1280 "*Regexp matching delimeter between method and user or host names.
1281 Derived from `tramp-postfix-method-format'.")
1282
1283 (defconst tramp-user-regexp
1284 "[^:/ \t]+"
1285 "*Regexp matching user names.")
1286
1287 (defconst tramp-prefix-domain-format "%"
1288 "*String matching delimeter between user and domain names.")
1289
1290 (defconst tramp-prefix-domain-regexp
1291 (regexp-quote tramp-prefix-domain-format)
1292 "*Regexp matching delimeter between user and domain names.
1293 Derived from `tramp-prefix-domain-format'.")
1294
1295 (defconst tramp-domain-regexp
1296 "[-a-zA-Z0-9_.]+"
1297 "*Regexp matching domain names.")
1298
1299 (defconst tramp-user-with-domain-regexp
1300 (concat "\\(" tramp-user-regexp "\\)"
1301 tramp-prefix-domain-regexp
1302 "\\(" tramp-domain-regexp "\\)")
1303 "*Regexp matching user names with domain names.")
1304
1305 (defconst tramp-postfix-user-format
1306 "@"
1307 "*String matching delimeter between user and host names.
1308 Used in `tramp-make-tramp-file-name'.")
1309
1310 (defconst tramp-postfix-user-regexp
1311 (regexp-quote tramp-postfix-user-format)
1312 "*Regexp matching delimeter between user and host names.
1313 Derived from `tramp-postfix-user-format'.")
1314
1315 (defconst tramp-host-regexp
1316 "[a-zA-Z0-9_.-]+"
1317 "*Regexp matching host names.")
1318
1319 (defconst tramp-prefix-ipv6-format
1320 (cond ((equal tramp-syntax 'ftp) "[")
1321 ((equal tramp-syntax 'sep) "")
1322 ((equal tramp-syntax 'url) "[")
1323 (t (error "Wrong `tramp-syntax' defined")))
1324 "*String matching left hand side of IPv6 addresses.
1325 Used in `tramp-make-tramp-file-name'.")
1326
1327 (defconst tramp-prefix-ipv6-regexp
1328 (regexp-quote tramp-prefix-ipv6-format)
1329 "*Regexp matching left hand side of IPv6 addresses.
1330 Derived from `tramp-prefix-ipv6-format'.")
1331
1332 ;; The following regexp is a bit sloppy. But it shall serve our
1333 ;; purposes. It covers also IPv4 mapped IPv6 addresses, like in
1334 ;; "::ffff:192.168.0.1".
1335 (defconst tramp-ipv6-regexp
1336 "\\(?:\\(?:[a-zA-Z0-9]+\\)?:\\)+[a-zA-Z0-9.]+"
1337 "*Regexp matching IPv6 addresses.")
1338
1339 (defconst tramp-postfix-ipv6-format
1340 (cond ((equal tramp-syntax 'ftp) "]")
1341 ((equal tramp-syntax 'sep) "")
1342 ((equal tramp-syntax 'url) "]")
1343 (t (error "Wrong `tramp-syntax' defined")))
1344 "*String matching right hand side of IPv6 addresses.
1345 Used in `tramp-make-tramp-file-name'.")
1346
1347 (defconst tramp-postfix-ipv6-regexp
1348 (regexp-quote tramp-postfix-ipv6-format)
1349 "*Regexp matching right hand side of IPv6 addresses.
1350 Derived from `tramp-postfix-ipv6-format'.")
1351
1352 (defconst tramp-prefix-port-format
1353 (cond ((equal tramp-syntax 'ftp) "#")
1354 ((equal tramp-syntax 'sep) "#")
1355 ((equal tramp-syntax 'url) ":")
1356 (t (error "Wrong `tramp-syntax' defined")))
1357 "*String matching delimeter between host names and port numbers.")
1358
1359 (defconst tramp-prefix-port-regexp
1360 (regexp-quote tramp-prefix-port-format)
1361 "*Regexp matching delimeter between host names and port numbers.
1362 Derived from `tramp-prefix-port-format'.")
1363
1364 (defconst tramp-port-regexp
1365 "[0-9]+"
1366 "*Regexp matching port numbers.")
1367
1368 (defconst tramp-host-with-port-regexp
1369 (concat "\\(" tramp-host-regexp "\\)"
1370 tramp-prefix-port-regexp
1371 "\\(" tramp-port-regexp "\\)")
1372 "*Regexp matching host names with port numbers.")
1373
1374 (defconst tramp-postfix-host-format
1375 (cond ((equal tramp-syntax 'ftp) ":")
1376 ((equal tramp-syntax 'sep) "]")
1377 ((equal tramp-syntax 'url) "")
1378 (t (error "Wrong `tramp-syntax' defined")))
1379 "*String matching delimeter between host names and localnames.
1380 Used in `tramp-make-tramp-file-name'.")
1381
1382 (defconst tramp-postfix-host-regexp
1383 (regexp-quote tramp-postfix-host-format)
1384 "*Regexp matching delimeter between host names and localnames.
1385 Derived from `tramp-postfix-host-format'.")
1386
1387 (defconst tramp-localname-regexp
1388 ".*$"
1389 "*Regexp matching localnames.")
1390
1391 ;; File name format.
1392
1393 (defconst tramp-file-name-structure
1394 (list
1395 (concat
1396 tramp-prefix-regexp
1397 "\\(" "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp "\\)?"
1398 "\\(" "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp "\\)?"
1399 "\\(" "\\(" tramp-host-regexp
1400 "\\|"
1401 tramp-prefix-ipv6-regexp tramp-ipv6-regexp
1402 tramp-postfix-ipv6-regexp "\\)"
1403 "\\(" tramp-prefix-port-regexp tramp-port-regexp "\\)?" "\\)?"
1404 tramp-postfix-host-regexp
1405 "\\(" tramp-localname-regexp "\\)")
1406 2 4 5 8)
1407
1408 "*List of five elements (REGEXP METHOD USER HOST FILE), detailing \
1409 the Tramp file name structure.
1410
1411 The first element REGEXP is a regular expression matching a Tramp file
1412 name. The regex should contain parentheses around the method name,
1413 the user name, the host name, and the file name parts.
1414
1415 The second element METHOD is a number, saying which pair of
1416 parentheses matches the method name. The third element USER is
1417 similar, but for the user name. The fourth element HOST is similar,
1418 but for the host name. The fifth element FILE is for the file name.
1419 These numbers are passed directly to `match-string', which see. That
1420 means the opening parentheses are counted to identify the pair.
1421
1422 See also `tramp-file-name-regexp'.")
1423
1424 ;;;###autoload
1425 (defconst tramp-file-name-regexp-unified
1426 "\\`/\\([^[/:]+\\|[^/]+]\\):"
1427 "Value for `tramp-file-name-regexp' for unified remoting.
1428 Emacs (not XEmacs) uses a unified filename syntax for Ange-FTP and
1429 Tramp. See `tramp-file-name-structure' for more explanations.")
1430
1431 ;;;###autoload
1432 (defconst tramp-file-name-regexp-separate
1433 "\\`/\\[.*\\]"
1434 "Value for `tramp-file-name-regexp' for separate remoting.
1435 XEmacs uses a separate filename syntax for Tramp and EFS.
1436 See `tramp-file-name-structure' for more explanations.")
1437
1438 ;;;###autoload
1439 (defconst tramp-file-name-regexp-url
1440 "\\`/[^/:]+://"
1441 "Value for `tramp-file-name-regexp' for URL-like remoting.
1442 See `tramp-file-name-structure' for more explanations.")
1443
1444 ;;;###autoload
1445 (defconst tramp-file-name-regexp
1446 (cond ((equal tramp-syntax 'ftp) tramp-file-name-regexp-unified)
1447 ((equal tramp-syntax 'sep) tramp-file-name-regexp-separate)
1448 ((equal tramp-syntax 'url) tramp-file-name-regexp-url)
1449 (t (error "Wrong `tramp-syntax' defined")))
1450 "*Regular expression matching file names handled by Tramp.
1451 This regexp should match Tramp file names but no other file names.
1452 When tramp.el is loaded, this regular expression is prepended to
1453 `file-name-handler-alist', and that is searched sequentially. Thus,
1454 if the Tramp entry appears rather early in the `file-name-handler-alist'
1455 and is a bit too general, then some files might be considered Tramp
1456 files which are not really Tramp files.
1457
1458 Please note that the entry in `file-name-handler-alist' is made when
1459 this file \(tramp.el\) is loaded. This means that this variable must be set
1460 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1461 updated after changing this variable.
1462
1463 Also see `tramp-file-name-structure'.")
1464
1465 ;;;###autoload
1466 (defconst tramp-root-regexp
1467 (if (memq system-type '(cygwin windows-nt))
1468 "\\`\\([a-zA-Z]:\\)?/"
1469 "\\`/")
1470 "Beginning of an incomplete Tramp file name.
1471 Usually, it is just \"\\\\`/\". On W32 systems, there might be a
1472 volume letter, which will be removed by `tramp-drop-volume-letter'.")
1473
1474 ;;;###autoload
1475 (defconst tramp-completion-file-name-regexp-unified
1476 (concat tramp-root-regexp "[^/]*\\'")
1477 "Value for `tramp-completion-file-name-regexp' for unified remoting.
1478 GNU Emacs uses a unified filename syntax for Tramp and Ange-FTP.
1479 See `tramp-file-name-structure' for more explanations.")
1480
1481 ;;;###autoload
1482 (defconst tramp-completion-file-name-regexp-separate
1483 (concat tramp-root-regexp "\\([[][^]]*\\)?\\'")
1484 "Value for `tramp-completion-file-name-regexp' for separate remoting.
1485 XEmacs uses a separate filename syntax for Tramp and EFS.
1486 See `tramp-file-name-structure' for more explanations.")
1487
1488 ;;;###autoload
1489 (defconst tramp-completion-file-name-regexp-url
1490 (concat tramp-root-regexp "[^/:]+\\(:\\(/\\(/[^/]*\\)?\\)?\\)?\\'")
1491 "Value for `tramp-completion-file-name-regexp' for URL-like remoting.
1492 See `tramp-file-name-structure' for more explanations.")
1493
1494 ;;;###autoload
1495 (defconst tramp-completion-file-name-regexp
1496 (cond ((equal tramp-syntax 'ftp) tramp-completion-file-name-regexp-unified)
1497 ((equal tramp-syntax 'sep) tramp-completion-file-name-regexp-separate)
1498 ((equal tramp-syntax 'url) tramp-completion-file-name-regexp-url)
1499 (t (error "Wrong `tramp-syntax' defined")))
1500 "*Regular expression matching file names handled by Tramp completion.
1501 This regexp should match partial Tramp file names only.
1502
1503 Please note that the entry in `file-name-handler-alist' is made when
1504 this file (tramp.el) is loaded. This means that this variable must be set
1505 before loading tramp.el. Alternatively, `file-name-handler-alist' can be
1506 updated after changing this variable.
1507
1508 Also see `tramp-file-name-structure'.")
1509
1510 (defconst tramp-actions-before-shell
1511 '((tramp-login-prompt-regexp tramp-action-login)
1512 (tramp-password-prompt-regexp tramp-action-password)
1513 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1514 (shell-prompt-pattern tramp-action-succeed)
1515 (tramp-shell-prompt-pattern tramp-action-succeed)
1516 (tramp-yesno-prompt-regexp tramp-action-yesno)
1517 (tramp-yn-prompt-regexp tramp-action-yn)
1518 (tramp-terminal-prompt-regexp tramp-action-terminal)
1519 (tramp-process-alive-regexp tramp-action-process-alive))
1520 "List of pattern/action pairs.
1521 Whenever a pattern matches, the corresponding action is performed.
1522 Each item looks like (PATTERN ACTION).
1523
1524 The PATTERN should be a symbol, a variable. The value of this
1525 variable gives the regular expression to search for. Note that the
1526 regexp must match at the end of the buffer, \"\\'\" is implicitly
1527 appended to it.
1528
1529 The ACTION should also be a symbol, but a function. When the
1530 corresponding PATTERN matches, the ACTION function is called.")
1531
1532 (defconst tramp-actions-copy-out-of-band
1533 '((tramp-password-prompt-regexp tramp-action-password)
1534 (tramp-wrong-passwd-regexp tramp-action-permission-denied)
1535 (tramp-copy-failed-regexp tramp-action-permission-denied)
1536 (tramp-process-alive-regexp tramp-action-out-of-band))
1537 "List of pattern/action pairs.
1538 This list is used for copying/renaming with out-of-band methods.
1539
1540 See `tramp-actions-before-shell' for more info.")
1541
1542 ;; Chunked sending kludge. We set this to 500 for black-listed constellations
1543 ;; known to have a bug in `process-send-string'; some ssh connections appear
1544 ;; to drop bytes when data is sent too quickly. There is also a connection
1545 ;; buffer local variable, which is computed depending on remote host properties
1546 ;; when `tramp-chunksize' is zero or nil.
1547 (defcustom tramp-chunksize
1548 (when (and (not (featurep 'xemacs))
1549 (memq system-type '(hpux)))
1550 500)
1551 ;; Parentheses in docstring starting at beginning of line are escaped.
1552 ;; Fontification is messed up when
1553 ;; `open-paren-in-column-0-is-defun-start' set to t.
1554 "*If non-nil, chunksize for sending input to local process.
1555 It is necessary only on systems which have a buggy `process-send-string'
1556 implementation. The necessity, whether this variable must be set, can be
1557 checked via the following code:
1558
1559 (with-temp-buffer
1560 (let* ((user \"xxx\") (host \"yyy\")
1561 (init 0) (step 50)
1562 (sent init) (received init))
1563 (while (= sent received)
1564 (setq sent (+ sent step))
1565 (erase-buffer)
1566 (let ((proc (start-process (buffer-name) (current-buffer)
1567 \"ssh\" \"-l\" user host \"wc\" \"-c\")))
1568 (when (memq (process-status proc) '(run open))
1569 (process-send-string proc (make-string sent ?\\ ))
1570 (process-send-eof proc)
1571 (process-send-eof proc))
1572 (while (not (progn (goto-char (point-min))
1573 (re-search-forward \"\\\\w+\" (point-max) t)))
1574 (accept-process-output proc 1))
1575 (when (memq (process-status proc) '(run open))
1576 (setq received (string-to-number (match-string 0)))
1577 (delete-process proc)
1578 (message \"Bytes sent: %s\\tBytes received: %s\" sent received)
1579 (sit-for 0))))
1580 (if (> sent (+ init step))
1581 (message \"You should set `tramp-chunksize' to a maximum of %s\"
1582 (- sent step))
1583 (message \"Test does not work\")
1584 (display-buffer (current-buffer))
1585 (sit-for 30))))
1586
1587 In the Emacs normally running Tramp, evaluate the above code
1588 \(replace \"xxx\" and \"yyy\" by the remote user and host name,
1589 respectively\). You can do this, for example, by pasting it into
1590 the `*scratch*' buffer and then hitting C-j with the cursor after the
1591 last closing parenthesis. Note that it works only if you have configured
1592 \"ssh\" to run without password query, see ssh-agent\(1\).
1593
1594 You will see the number of bytes sent successfully to the remote host.
1595 If that number exceeds 1000, you can stop the execution by hitting
1596 C-g, because your Emacs is likely clean.
1597
1598 When it is necessary to set `tramp-chunksize', you might consider to
1599 use an out-of-the-band method \(like \"scp\"\) instead of an internal one
1600 \(like \"ssh\"\), because setting `tramp-chunksize' to non-nil decreases
1601 performance.
1602
1603 If your Emacs is buggy, the code stops and gives you an indication
1604 about the value `tramp-chunksize' should be set. Maybe you could just
1605 experiment a bit, e.g. changing the values of `init' and `step'
1606 in the third line of the code.
1607
1608 Please raise a bug report via \"M-x tramp-bug\" if your system needs
1609 this variable to be set as well."
1610 :group 'tramp
1611 :type '(choice (const nil) integer))
1612
1613 ;; Logging in to a remote host normally requires obtaining a pty. But
1614 ;; Emacs on MacOS X has process-connection-type set to nil by default,
1615 ;; so on those systems Tramp doesn't obtain a pty. Here, we allow
1616 ;; for an override of the system default.
1617 (defcustom tramp-process-connection-type t
1618 "Overrides `process-connection-type' for connections from Tramp.
1619 Tramp binds process-connection-type to the value given here before
1620 opening a connection to a remote host."
1621 :group 'tramp
1622 :type '(choice (const nil) (const t) (const pty)))
1623
1624 (defcustom tramp-completion-reread-directory-timeout 10
1625 "Defines seconds since last remote command before rereading a directory.
1626 A remote directory might have changed its contents. In order to
1627 make it visible during file name completion in the minibuffer,
1628 Tramp flushes its cache and rereads the directory contents when
1629 more than `tramp-completion-reread-directory-timeout' seconds
1630 have been gone since last remote command execution. A value of 0
1631 would require an immediate reread during filename completion, nil
1632 means to use always cached values for the directory contents."
1633 :group 'tramp
1634 :type '(choice (const nil) integer))
1635
1636 ;;; Internal Variables:
1637
1638 (defvar tramp-current-method nil
1639 "Connection method for this *tramp* buffer.")
1640
1641 (defvar tramp-current-user nil
1642 "Remote login name for this *tramp* buffer.")
1643
1644 (defvar tramp-current-host nil
1645 "Remote host for this *tramp* buffer.")
1646
1647 (defconst tramp-uudecode
1648 "(echo begin 600 /tmp/tramp.$$; tail +2) | uudecode
1649 cat /tmp/tramp.$$
1650 rm -f /tmp/tramp.$$"
1651 "Shell function to implement `uudecode' to standard output.
1652 Many systems support `uudecode -o /dev/stdout' or `uudecode -o -'
1653 for this or `uudecode -p', but some systems don't, and for them
1654 we have this shell function.")
1655
1656 (defconst tramp-perl-file-truename
1657 "%s -e '
1658 use File::Spec;
1659 use Cwd \"realpath\";
1660
1661 sub recursive {
1662 my ($volume, @dirs) = @_;
1663 my $real = realpath(File::Spec->catpath(
1664 $volume, File::Spec->catdir(@dirs), \"\"));
1665 if ($real) {
1666 my ($vol, $dir) = File::Spec->splitpath($real, 1);
1667 return ($vol, File::Spec->splitdir($dir));
1668 }
1669 else {
1670 my $last = pop(@dirs);
1671 ($volume, @dirs) = recursive($volume, @dirs);
1672 push(@dirs, $last);
1673 return ($volume, @dirs);
1674 }
1675 }
1676
1677 $result = realpath($ARGV[0]);
1678 if (!$result) {
1679 my ($vol, $dir) = File::Spec->splitpath($ARGV[0], 1);
1680 ($vol, @dirs) = recursive($vol, File::Spec->splitdir($dir));
1681
1682 $result = File::Spec->catpath($vol, File::Spec->catdir(@dirs), \"\");
1683 }
1684
1685 if ($ARGV[0] =~ /\\/$/) {
1686 $result = $result . \"/\";
1687 }
1688
1689 print \"\\\"$result\\\"\\n\";
1690 ' \"$1\" 2>/dev/null"
1691 "Perl script to produce output suitable for use with `file-truename'
1692 on the remote file system.
1693 Escape sequence %s is replaced with name of Perl binary.
1694 This string is passed to `format', so percent characters need to be doubled.")
1695
1696 (defconst tramp-perl-file-name-all-completions
1697 "%s -e 'sub case {
1698 my $str = shift;
1699 if ($ARGV[2]) {
1700 return lc($str);
1701 }
1702 else {
1703 return $str;
1704 }
1705 }
1706 opendir(d, $ARGV[0]) || die(\"$ARGV[0]: $!\\nfail\\n\");
1707 @files = readdir(d); closedir(d);
1708 foreach $f (@files) {
1709 if (case(substr($f, 0, length($ARGV[1]))) eq case($ARGV[1])) {
1710 if (-d \"$ARGV[0]/$f\") {
1711 print \"$f/\\n\";
1712 }
1713 else {
1714 print \"$f\\n\";
1715 }
1716 }
1717 }
1718 print \"ok\\n\"
1719 ' \"$1\" \"$2\" \"$3\" 2>/dev/null"
1720 "Perl script to produce output suitable for use with
1721 `file-name-all-completions' on the remote file system. Escape
1722 sequence %s is replaced with name of Perl binary. This string is
1723 passed to `format', so percent characters need to be doubled.")
1724
1725 ;; Perl script to implement `file-attributes' in a Lisp `read'able
1726 ;; output. If you are hacking on this, note that you get *no* output
1727 ;; unless this spits out a complete line, including the '\n' at the
1728 ;; end.
1729 ;; The device number is returned as "-1", because there will be a virtual
1730 ;; device number set in `tramp-handle-file-attributes'.
1731 (defconst tramp-perl-file-attributes
1732 "%s -e '
1733 @stat = lstat($ARGV[0]);
1734 if (!@stat) {
1735 print \"nil\\n\";
1736 exit 0;
1737 }
1738 if (($stat[2] & 0170000) == 0120000)
1739 {
1740 $type = readlink($ARGV[0]);
1741 $type = \"\\\"$type\\\"\";
1742 }
1743 elsif (($stat[2] & 0170000) == 040000)
1744 {
1745 $type = \"t\";
1746 }
1747 else
1748 {
1749 $type = \"nil\"
1750 };
1751 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1752 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1753 printf(
1754 \"(%%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) -1)\\n\",
1755 $type,
1756 $stat[3],
1757 $uid,
1758 $gid,
1759 $stat[8] >> 16 & 0xffff,
1760 $stat[8] & 0xffff,
1761 $stat[9] >> 16 & 0xffff,
1762 $stat[9] & 0xffff,
1763 $stat[10] >> 16 & 0xffff,
1764 $stat[10] & 0xffff,
1765 $stat[7],
1766 $stat[2],
1767 $stat[1] >> 16 & 0xffff,
1768 $stat[1] & 0xffff
1769 );' \"$1\" \"$2\" \"$3\" 2>/dev/null"
1770 "Perl script to produce output suitable for use with `file-attributes'
1771 on the remote file system.
1772 Escape sequence %s is replaced with name of Perl binary.
1773 This string is passed to `format', so percent characters need to be doubled.")
1774
1775 (defconst tramp-perl-directory-files-and-attributes
1776 "%s -e '
1777 chdir($ARGV[0]) or printf(\"\\\"Cannot change to $ARGV[0]: $''!''\\\"\\n\"), exit();
1778 opendir(DIR,\".\") or printf(\"\\\"Cannot open directory $ARGV[0]: $''!''\\\"\\n\"), exit();
1779 @list = readdir(DIR);
1780 closedir(DIR);
1781 $n = scalar(@list);
1782 printf(\"(\\n\");
1783 for($i = 0; $i < $n; $i++)
1784 {
1785 $filename = $list[$i];
1786 @stat = lstat($filename);
1787 if (($stat[2] & 0170000) == 0120000)
1788 {
1789 $type = readlink($filename);
1790 $type = \"\\\"$type\\\"\";
1791 }
1792 elsif (($stat[2] & 0170000) == 040000)
1793 {
1794 $type = \"t\";
1795 }
1796 else
1797 {
1798 $type = \"nil\"
1799 };
1800 $uid = ($ARGV[1] eq \"integer\") ? $stat[4] : \"\\\"\" . getpwuid($stat[4]) . \"\\\"\";
1801 $gid = ($ARGV[1] eq \"integer\") ? $stat[5] : \"\\\"\" . getgrgid($stat[5]) . \"\\\"\";
1802 printf(
1803 \"(\\\"%%s\\\" %%s %%u %%s %%s (%%u %%u) (%%u %%u) (%%u %%u) %%u.0 %%u t (%%u . %%u) (%%u . %%u))\\n\",
1804 $filename,
1805 $type,
1806 $stat[3],
1807 $uid,
1808 $gid,
1809 $stat[8] >> 16 & 0xffff,
1810 $stat[8] & 0xffff,
1811 $stat[9] >> 16 & 0xffff,
1812 $stat[9] & 0xffff,
1813 $stat[10] >> 16 & 0xffff,
1814 $stat[10] & 0xffff,
1815 $stat[7],
1816 $stat[2],
1817 $stat[1] >> 16 & 0xffff,
1818 $stat[1] & 0xffff,
1819 $stat[0] >> 16 & 0xffff,
1820 $stat[0] & 0xffff);
1821 }
1822 printf(\")\\n\");' \"$1\" \"$2\" \"$3\" 2>/dev/null"
1823 "Perl script implementing `directory-files-attributes' as Lisp `read'able
1824 output.
1825 Escape sequence %s is replaced with name of Perl binary.
1826 This string is passed to `format', so percent characters need to be doubled.")
1827
1828 ;; ;; These two use uu encoding.
1829 ;; (defvar tramp-perl-encode "%s -e'\
1830 ;; print qq(begin 644 xxx\n);
1831 ;; my $s = q();
1832 ;; my $res = q();
1833 ;; while (read(STDIN, $s, 45)) {
1834 ;; print pack(q(u), $s);
1835 ;; }
1836 ;; print qq(`\n);
1837 ;; print qq(end\n);
1838 ;; '"
1839 ;; "Perl program to use for encoding a file.
1840 ;; Escape sequence %s is replaced with name of Perl binary.")
1841
1842 ;; (defvar tramp-perl-decode "%s -ne '
1843 ;; print unpack q(u), $_;
1844 ;; '"
1845 ;; "Perl program to use for decoding a file.
1846 ;; Escape sequence %s is replaced with name of Perl binary.")
1847
1848 ;; These two use base64 encoding.
1849 (defconst tramp-perl-encode-with-module
1850 "%s -MMIME::Base64 -0777 -ne 'print encode_base64($_)' 2>/dev/null"
1851 "Perl program to use for encoding a file.
1852 Escape sequence %s is replaced with name of Perl binary.
1853 This string is passed to `format', so percent characters need to be doubled.
1854 This implementation requires the MIME::Base64 Perl module to be installed
1855 on the remote host.")
1856
1857 (defconst tramp-perl-decode-with-module
1858 "%s -MMIME::Base64 -0777 -ne 'print decode_base64($_)' 2>/dev/null"
1859 "Perl program to use for decoding a file.
1860 Escape sequence %s is replaced with name of Perl binary.
1861 This string is passed to `format', so percent characters need to be doubled.
1862 This implementation requires the MIME::Base64 Perl module to be installed
1863 on the remote host.")
1864
1865 (defconst tramp-perl-encode
1866 "%s -e '
1867 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1868 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
1869 # Free Software Foundation, Inc.
1870 use strict;
1871
1872 my %%trans = do {
1873 my $i = 0;
1874 map {(substr(unpack(q(B8), chr $i++), 2, 6), $_)}
1875 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/);
1876 };
1877
1878 binmode(\\*STDIN);
1879
1880 # We read in chunks of 54 bytes, to generate output lines
1881 # of 72 chars (plus end of line)
1882 $/ = \\54;
1883
1884 while (my $data = <STDIN>) {
1885 my $pad = q();
1886
1887 # Only for the last chunk, and only if did not fill the last three-byte packet
1888 if (eof) {
1889 my $mod = length($data) %% 3;
1890 $pad = q(=) x (3 - $mod) if $mod;
1891 }
1892
1893 # Not the fastest method, but it is simple: unpack to binary string, split
1894 # by groups of 6 bits and convert back from binary to byte; then map into
1895 # the translation table
1896 print
1897 join q(),
1898 map($trans{$_},
1899 (substr(unpack(q(B*), $data) . q(00000), 0, 432) =~ /....../g)),
1900 $pad,
1901 qq(\\n);
1902 }' 2>/dev/null"
1903 "Perl program to use for encoding a file.
1904 Escape sequence %s is replaced with name of Perl binary.
1905 This string is passed to `format', so percent characters need to be doubled.")
1906
1907 (defconst tramp-perl-decode
1908 "%s -e '
1909 # This script contributed by Juanma Barranquero <lektu@terra.es>.
1910 # Copyright (C) 2002, 2003, 2004, 2005, 2006, 2007, 2008, 2009, 2010
1911 # Free Software Foundation, Inc.
1912 use strict;
1913
1914 my %%trans = do {
1915 my $i = 0;
1916 map {($_, substr(unpack(q(B8), chr $i++), 2, 6))}
1917 split //, q(ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/)
1918 };
1919
1920 my %%bytes = map {(unpack(q(B8), chr $_), chr $_)} 0 .. 255;
1921
1922 binmode(\\*STDOUT);
1923
1924 # We are going to accumulate into $pending to accept any line length
1925 # (we do not check they are <= 76 chars as the RFC says)
1926 my $pending = q();
1927
1928 while (my $data = <STDIN>) {
1929 chomp $data;
1930
1931 # If we find one or two =, we have reached the end and
1932 # any following data is to be discarded
1933 my $finished = $data =~ s/(==?).*/$1/;
1934 $pending .= $data;
1935
1936 my $len = length($pending);
1937 my $chunk = substr($pending, 0, $len & ~3);
1938 $pending = substr($pending, $len & ~3 + 1);
1939
1940 # Easy method: translate from chars to (pregenerated) six-bit packets, join,
1941 # split in 8-bit chunks and convert back to char.
1942 print join q(),
1943 map $bytes{$_},
1944 ((join q(), map {$trans{$_} || q()} split //, $chunk) =~ /......../g);
1945
1946 last if $finished;
1947 }' 2>/dev/null"
1948 "Perl program to use for decoding a file.
1949 Escape sequence %s is replaced with name of Perl binary.
1950 This string is passed to `format', so percent characters need to be doubled.")
1951
1952 (defconst tramp-vc-registered-read-file-names
1953 "echo \"(\"
1954 for file in \"$@\"; do
1955 if %s $file; then
1956 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" t)\"
1957 else
1958 echo \"(\\\"$file\\\" \\\"file-exists-p\\\" nil)\"
1959 fi
1960 if %s $file; then
1961 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" t)\"
1962 else
1963 echo \"(\\\"$file\\\" \\\"file-readable-p\\\" nil)\"
1964 fi
1965 done
1966 echo \")\""
1967 "Script to check existence of VC related files.
1968 It must be send formatted with two strings; the tests for file
1969 existence, and file readability.")
1970
1971 (defconst tramp-file-mode-type-map
1972 '((0 . "-") ; Normal file (SVID-v2 and XPG2)
1973 (1 . "p") ; fifo
1974 (2 . "c") ; character device
1975 (3 . "m") ; multiplexed character device (v7)
1976 (4 . "d") ; directory
1977 (5 . "?") ; Named special file (XENIX)
1978 (6 . "b") ; block device
1979 (7 . "?") ; multiplexed block device (v7)
1980 (8 . "-") ; regular file
1981 (9 . "n") ; network special file (HP-UX)
1982 (10 . "l") ; symlink
1983 (11 . "?") ; ACL shadow inode (Solaris, not userspace)
1984 (12 . "s") ; socket
1985 (13 . "D") ; door special (Solaris)
1986 (14 . "w")) ; whiteout (BSD)
1987 "A list of file types returned from the `stat' system call.
1988 This is used to map a mode number to a permission string.")
1989
1990 ;; New handlers should be added here. The following operations can be
1991 ;; handled using the normal primitives: file-name-sans-versions,
1992 ;; get-file-buffer.
1993 (defconst tramp-file-name-handler-alist
1994 '((load . tramp-handle-load)
1995 (make-symbolic-link . tramp-handle-make-symbolic-link)
1996 (file-name-as-directory . tramp-handle-file-name-as-directory)
1997 (file-name-directory . tramp-handle-file-name-directory)
1998 (file-name-nondirectory . tramp-handle-file-name-nondirectory)
1999 (file-truename . tramp-handle-file-truename)
2000 (file-exists-p . tramp-handle-file-exists-p)
2001 (file-directory-p . tramp-handle-file-directory-p)
2002 (file-executable-p . tramp-handle-file-executable-p)
2003 (file-readable-p . tramp-handle-file-readable-p)
2004 (file-regular-p . tramp-handle-file-regular-p)
2005 (file-symlink-p . tramp-handle-file-symlink-p)
2006 (file-writable-p . tramp-handle-file-writable-p)
2007 (file-ownership-preserved-p . tramp-handle-file-ownership-preserved-p)
2008 (file-newer-than-file-p . tramp-handle-file-newer-than-file-p)
2009 (file-attributes . tramp-handle-file-attributes)
2010 (file-modes . tramp-handle-file-modes)
2011 (directory-files . tramp-handle-directory-files)
2012 (directory-files-and-attributes . tramp-handle-directory-files-and-attributes)
2013 (file-name-all-completions . tramp-handle-file-name-all-completions)
2014 (file-name-completion . tramp-handle-file-name-completion)
2015 (add-name-to-file . tramp-handle-add-name-to-file)
2016 (copy-file . tramp-handle-copy-file)
2017 (copy-directory . tramp-handle-copy-directory)
2018 (rename-file . tramp-handle-rename-file)
2019 (set-file-modes . tramp-handle-set-file-modes)
2020 (set-file-times . tramp-handle-set-file-times)
2021 (make-directory . tramp-handle-make-directory)
2022 (delete-directory . tramp-handle-delete-directory)
2023 (delete-file . tramp-handle-delete-file)
2024 (directory-file-name . tramp-handle-directory-file-name)
2025 ;; `executable-find' is not official yet.
2026 (executable-find . tramp-handle-executable-find)
2027 (start-file-process . tramp-handle-start-file-process)
2028 (process-file . tramp-handle-process-file)
2029 (shell-command . tramp-handle-shell-command)
2030 (insert-directory . tramp-handle-insert-directory)
2031 (expand-file-name . tramp-handle-expand-file-name)
2032 (substitute-in-file-name . tramp-handle-substitute-in-file-name)
2033 (file-local-copy . tramp-handle-file-local-copy)
2034 (file-remote-p . tramp-handle-file-remote-p)
2035 (insert-file-contents . tramp-handle-insert-file-contents)
2036 (insert-file-contents-literally
2037 . tramp-handle-insert-file-contents-literally)
2038 (write-region . tramp-handle-write-region)
2039 (find-backup-file-name . tramp-handle-find-backup-file-name)
2040 (make-auto-save-file-name . tramp-handle-make-auto-save-file-name)
2041 (unhandled-file-name-directory . tramp-handle-unhandled-file-name-directory)
2042 (dired-compress-file . tramp-handle-dired-compress-file)
2043 (dired-recursive-delete-directory
2044 . tramp-handle-dired-recursive-delete-directory)
2045 (dired-uncache . tramp-handle-dired-uncache)
2046 (set-visited-file-modtime . tramp-handle-set-visited-file-modtime)
2047 (verify-visited-file-modtime . tramp-handle-verify-visited-file-modtime)
2048 (file-selinux-context . tramp-handle-file-selinux-context)
2049 (set-file-selinux-context . tramp-handle-set-file-selinux-context)
2050 (vc-registered . tramp-handle-vc-registered))
2051 "Alist of handler functions.
2052 Operations not mentioned here will be handled by the normal Emacs functions.")
2053
2054 ;; Handlers for partial Tramp file names. For Emacs just
2055 ;; `file-name-all-completions' is needed.
2056 ;;;###autoload
2057 (defconst tramp-completion-file-name-handler-alist
2058 '((file-name-all-completions . tramp-completion-handle-file-name-all-completions)
2059 (file-name-completion . tramp-completion-handle-file-name-completion))
2060 "Alist of completion handler functions.
2061 Used for file names matching `tramp-file-name-regexp'. Operations not
2062 mentioned here will be handled by `tramp-file-name-handler-alist' or the
2063 normal Emacs functions.")
2064
2065 ;; Handlers for foreign methods, like FTP or SMB, shall be plugged here.
2066 (defvar tramp-foreign-file-name-handler-alist
2067 ;; (identity . tramp-sh-file-name-handler) should always be the last
2068 ;; entry, because `identity' always matches.
2069 '((identity . tramp-sh-file-name-handler))
2070 "Alist of elements (FUNCTION . HANDLER) for foreign methods handled specially.
2071 If (FUNCTION FILENAME) returns non-nil, then all I/O on that file is done by
2072 calling HANDLER.")
2073
2074 ;;; Internal functions which must come first:
2075
2076 (defsubst tramp-debug-message (vec fmt-string &rest args)
2077 "Append message to debug buffer.
2078 Message is formatted with FMT-STRING as control string and the remaining
2079 ARGS to actually emit the message (if applicable)."
2080 (when (get-buffer (tramp-buffer-name vec))
2081 (with-current-buffer (tramp-get-debug-buffer vec)
2082 (goto-char (point-max))
2083 ;; Headline.
2084 (when (bobp)
2085 (insert
2086 (format
2087 ";; %sEmacs: %s Tramp: %s -*- mode: outline; -*-"
2088 (if (featurep 'sxemacs) "SX" (if (featurep 'xemacs) "X" "GNU "))
2089 emacs-version tramp-version)))
2090 (unless (bolp)
2091 (insert "\n"))
2092 ;; Timestamp.
2093 (let ((now (current-time)))
2094 (insert (format-time-string "%T." now))
2095 (insert (format "%06d " (nth 2 now))))
2096 ;; Calling function.
2097 (let ((btn 1) btf fn)
2098 (while (not fn)
2099 (setq btf (nth 1 (backtrace-frame btn)))
2100 (if (not btf)
2101 (setq fn "")
2102 (when (symbolp btf)
2103 (setq fn (symbol-name btf))
2104 (unless (and (string-match "^tramp" fn)
2105 (not (string-match
2106 "^tramp\\(-debug\\)?\\(-message\\|-error\\)$"
2107 fn)))
2108 (setq fn nil)))
2109 (setq btn (1+ btn))))
2110 ;; The following code inserts filename and line number.
2111 ;; Should be deactivated by default, because it is time
2112 ;; consuming.
2113 ; (let ((ffn (find-function-noselect (intern fn))))
2114 ; (insert
2115 ; (format
2116 ; "%s:%d: "
2117 ; (file-name-nondirectory (buffer-file-name (car ffn)))
2118 ; (with-current-buffer (car ffn)
2119 ; (1+ (count-lines (point-min) (cdr ffn)))))))
2120 (insert (format "%s " fn)))
2121 ;; The message.
2122 (insert (apply 'format fmt-string args)))))
2123
2124 (defvar tramp-message-show-message t
2125 "Show Tramp message in the minibuffer.
2126 This variable is used to disable messages from `tramp-error'.
2127 The messages are visible anyway, because an error is raised.")
2128
2129 (defsubst tramp-message (vec-or-proc level fmt-string &rest args)
2130 "Emit a message depending on verbosity level.
2131 VEC-OR-PROC identifies the Tramp buffer to use. It can be either a
2132 vector or a process. LEVEL says to be quiet if `tramp-verbose' is
2133 less than LEVEL. The message is emitted only if `tramp-verbose' is
2134 greater than or equal to LEVEL.
2135
2136 The message is also logged into the debug buffer when `tramp-verbose'
2137 is greater than or equal 4.
2138
2139 Calls functions `message' and `tramp-debug-message' with FMT-STRING as
2140 control string and the remaining ARGS to actually emit the message (if
2141 applicable)."
2142 (condition-case nil
2143 (when (<= level tramp-verbose)
2144 ;; Match data must be preserved!
2145 (save-match-data
2146 ;; Display only when there is a minimum level.
2147 (when (and tramp-message-show-message (<= level 3))
2148 (apply 'message
2149 (concat
2150 (cond
2151 ((= level 0) "")
2152 ((= level 1) "")
2153 ((= level 2) "Warning: ")
2154 (t "Tramp: "))
2155 fmt-string)
2156 args))
2157 ;; Log only when there is a minimum level.
2158 (when (>= tramp-verbose 4)
2159 (when (and vec-or-proc
2160 (processp vec-or-proc)
2161 (buffer-name (process-buffer vec-or-proc)))
2162 (with-current-buffer (process-buffer vec-or-proc)
2163 ;; Translate proc to vec.
2164 (setq vec-or-proc (tramp-dissect-file-name default-directory))))
2165 (when (and vec-or-proc (vectorp vec-or-proc))
2166 (apply 'tramp-debug-message
2167 vec-or-proc
2168 (concat (format "(%d) # " level) fmt-string)
2169 args)))))
2170 ;; Suppress all errors.
2171 (error nil)))
2172
2173 (defsubst tramp-error (vec-or-proc signal fmt-string &rest args)
2174 "Emit an error.
2175 VEC-OR-PROC identifies the connection to use, SIGNAL is the
2176 signal identifier to be raised, remaining args passed to
2177 `tramp-message'. Finally, signal SIGNAL is raised."
2178 (let (tramp-message-show-message)
2179 (tramp-message
2180 vec-or-proc 1 "%s"
2181 (error-message-string
2182 (list signal
2183 (get signal 'error-message)
2184 (apply 'format fmt-string args))))
2185 (signal signal (list (apply 'format fmt-string args)))))
2186
2187 (defsubst tramp-error-with-buffer
2188 (buffer vec-or-proc signal fmt-string &rest args)
2189 "Emit an error, and show BUFFER.
2190 If BUFFER is nil, show the connection buffer. Wait for 30\", or until
2191 an input event arrives. The other arguments are passed to `tramp-error'."
2192 (save-window-excursion
2193 (unwind-protect
2194 (apply 'tramp-error vec-or-proc signal fmt-string args)
2195 (when (and vec-or-proc
2196 (not (zerop tramp-verbose))
2197 (not (tramp-completion-mode-p)))
2198 (let ((enable-recursive-minibuffers t))
2199 (pop-to-buffer
2200 (or (and (bufferp buffer) buffer)
2201 (and (processp vec-or-proc) (process-buffer vec-or-proc))
2202 (tramp-get-buffer vec-or-proc)))
2203 (sit-for 30))))))
2204
2205 (defmacro with-parsed-tramp-file-name (filename var &rest body)
2206 "Parse a Tramp filename and make components available in the body.
2207
2208 First arg FILENAME is evaluated and dissected into its components.
2209 Second arg VAR is a symbol. It is used as a variable name to hold
2210 the filename structure. It is also used as a prefix for the variables
2211 holding the components. For example, if VAR is the symbol `foo', then
2212 `foo' will be bound to the whole structure, `foo-method' will be bound to
2213 the method component, and so on for `foo-user', `foo-host', `foo-localname'.
2214
2215 Remaining args are Lisp expressions to be evaluated (inside an implicit
2216 `progn').
2217
2218 If VAR is nil, then we bind `v' to the structure and `method', `user',
2219 `host', `localname' to the components."
2220 `(let* ((,(or var 'v) (tramp-dissect-file-name ,filename))
2221 (,(if var (intern (concat (symbol-name var) "-method")) 'method)
2222 (tramp-file-name-method ,(or var 'v)))
2223 (,(if var (intern (concat (symbol-name var) "-user")) 'user)
2224 (tramp-file-name-user ,(or var 'v)))
2225 (,(if var (intern (concat (symbol-name var) "-host")) 'host)
2226 (tramp-file-name-host ,(or var 'v)))
2227 (,(if var (intern (concat (symbol-name var) "-localname")) 'localname)
2228 (tramp-file-name-localname ,(or var 'v))))
2229 ,@body))
2230
2231 (put 'with-parsed-tramp-file-name 'lisp-indent-function 2)
2232 (put 'with-parsed-tramp-file-name 'edebug-form-spec '(form symbolp body))
2233 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-parsed-tramp-file-name\\>"))
2234
2235 (defmacro with-file-property (vec file property &rest body)
2236 "Check in Tramp cache for PROPERTY, otherwise execute BODY and set cache.
2237 FILE must be a local file name on a connection identified via VEC."
2238 `(if (file-name-absolute-p ,file)
2239 (let ((value (tramp-get-file-property ,vec ,file ,property 'undef)))
2240 (when (eq value 'undef)
2241 ;; We cannot pass @body as parameter to
2242 ;; `tramp-set-file-property' because it mangles our
2243 ;; debug messages.
2244 (setq value (progn ,@body))
2245 (tramp-set-file-property ,vec ,file ,property value))
2246 value)
2247 ,@body))
2248
2249 (put 'with-file-property 'lisp-indent-function 3)
2250 (put 'with-file-property 'edebug-form-spec t)
2251 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-file-property\\>"))
2252
2253 (defmacro with-connection-property (key property &rest body)
2254 "Check in Tramp for property PROPERTY, otherwise executes BODY and set."
2255 `(let ((value (tramp-get-connection-property ,key ,property 'undef)))
2256 (when (eq value 'undef)
2257 ;; We cannot pass ,@body as parameter to
2258 ;; `tramp-set-connection-property' because it mangles our debug
2259 ;; messages.
2260 (setq value (progn ,@body))
2261 (tramp-set-connection-property ,key ,property value))
2262 value))
2263
2264 (put 'with-connection-property 'lisp-indent-function 2)
2265 (put 'with-connection-property 'edebug-form-spec t)
2266 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-connection-property\\>"))
2267
2268 (defmacro with-progress-reporter (vec level message &rest body)
2269 "Executes BODY, spinning a progress reporter with MESSAGE."
2270 `(let (pr tm)
2271 (tramp-message ,vec ,level "%s..." ,message)
2272 ;; We start a pulsing progress reporter after 3 seconds. Feature
2273 ;; introduced in Emacs 24.1.
2274 (when (and tramp-message-show-message
2275 ;; Display only when there is a minimum level.
2276 (<= ,level (min tramp-verbose 3)))
2277 (condition-case nil
2278 (setq pr (tramp-compat-funcall 'make-progress-reporter ,message)
2279 tm (if pr (run-at-time 3 0.1 'progress-reporter-update pr)))
2280 (error nil)))
2281 (unwind-protect
2282 ;; Execute the body.
2283 (let ((tramp-message-show-message
2284 (and tramp-message-show-message (not tm))))
2285 ,@body)
2286 ;; Stop progress reporter.
2287 (if tm (tramp-compat-funcall 'cancel-timer tm))
2288 (tramp-message ,vec ,level "%s...done" ,message))))
2289
2290 (put 'with-progress-reporter 'lisp-indent-function 3)
2291 (put 'with-progress-reporter 'edebug-form-spec t)
2292 (font-lock-add-keywords 'emacs-lisp-mode '("\\<with-progress-reporter\\>"))
2293
2294 (eval-and-compile ;; Silence compiler.
2295 (if (memq system-type '(cygwin windows-nt))
2296 (defun tramp-drop-volume-letter (name)
2297 "Cut off unnecessary drive letter from file NAME.
2298 The function `tramp-handle-expand-file-name' calls `expand-file-name'
2299 locally on a remote file name. When the local system is a W32 system
2300 but the remote system is Unix, this introduces a superfluous drive
2301 letter into the file name. This function removes it."
2302 (save-match-data
2303 (if (string-match tramp-root-regexp name)
2304 (replace-match "/" nil t name)
2305 name)))
2306
2307 (defalias 'tramp-drop-volume-letter 'identity)))
2308
2309 (defsubst tramp-make-tramp-temp-file (vec)
2310 "Create a temporary file on the remote host identified by VEC.
2311 Return the local name of the temporary file."
2312 (let ((prefix
2313 (tramp-make-tramp-file-name
2314 (tramp-file-name-method vec)
2315 (tramp-file-name-user vec)
2316 (tramp-file-name-host vec)
2317 (tramp-drop-volume-letter
2318 (expand-file-name
2319 tramp-temp-name-prefix (tramp-get-remote-tmpdir vec)))))
2320 result)
2321 (while (not result)
2322 ;; `make-temp-file' would be the natural choice for
2323 ;; implementation. But it calls `write-region' internally,
2324 ;; which also needs a temporary file - we would end in an
2325 ;; infinite loop.
2326 (setq result (make-temp-name prefix))
2327 (if (file-exists-p result)
2328 (setq result nil)
2329 ;; This creates the file by side effect.
2330 (set-file-times result)
2331 (set-file-modes result (tramp-octal-to-decimal "0700"))))
2332
2333 ;; Return the local part.
2334 (with-parsed-tramp-file-name result nil localname)))
2335
2336
2337 ;;; Config Manipulation Functions:
2338
2339 (defun tramp-set-completion-function (method function-list)
2340 "Sets the list of completion functions for METHOD.
2341 FUNCTION-LIST is a list of entries of the form (FUNCTION FILE).
2342 The FUNCTION is intended to parse FILE according its syntax.
2343 It might be a predefined FUNCTION, or a user defined FUNCTION.
2344 Predefined FUNCTIONs are `tramp-parse-rhosts', `tramp-parse-shosts',
2345 `tramp-parse-sconfig', `tramp-parse-hosts', `tramp-parse-passwd',
2346 and `tramp-parse-netrc'.
2347
2348 Example:
2349
2350 (tramp-set-completion-function
2351 \"ssh\"
2352 '((tramp-parse-sconfig \"/etc/ssh_config\")
2353 (tramp-parse-sconfig \"~/.ssh/config\")))"
2354
2355 (let ((r function-list)
2356 (v function-list))
2357 (setq tramp-completion-function-alist
2358 (delete (assoc method tramp-completion-function-alist)
2359 tramp-completion-function-alist))
2360
2361 (while v
2362 ;; Remove double entries.
2363 (when (member (car v) (cdr v))
2364 (setcdr v (delete (car v) (cdr v))))
2365 ;; Check for function and file or registry key.
2366 (unless (and (functionp (nth 0 (car v)))
2367 (if (string-match "^HKEY_CURRENT_USER" (nth 1 (car v)))
2368 ;; Windows registry.
2369 (and (memq system-type '(cygwin windows-nt))
2370 (zerop
2371 (tramp-local-call-process
2372 "reg" nil nil nil "query" (nth 1 (car v)))))
2373 ;; Configuration file.
2374 (file-exists-p (nth 1 (car v)))))
2375 (setq r (delete (car v) r)))
2376 (setq v (cdr v)))
2377
2378 (when r
2379 (add-to-list 'tramp-completion-function-alist
2380 (cons method r)))))
2381
2382 (defun tramp-get-completion-function (method)
2383 "Returns a list of completion functions for METHOD.
2384 For definition of that list see `tramp-set-completion-function'."
2385 (cons
2386 ;; Hosts visited once shall be remembered.
2387 `(tramp-parse-connection-properties ,method)
2388 ;; The method related defaults.
2389 (cdr (assoc method tramp-completion-function-alist))))
2390
2391
2392 ;;; Fontification of `read-file-name':
2393
2394 ;; rfn-eshadow.el is part of Emacs 22. It is autoloaded.
2395 (defvar tramp-rfn-eshadow-overlay)
2396 (make-variable-buffer-local 'tramp-rfn-eshadow-overlay)
2397
2398 (defun tramp-rfn-eshadow-setup-minibuffer ()
2399 "Set up a minibuffer for `file-name-shadow-mode'.
2400 Adds another overlay hiding filename parts according to Tramp's
2401 special handling of `substitute-in-file-name'."
2402 (when (symbol-value 'minibuffer-completing-file-name)
2403 (setq tramp-rfn-eshadow-overlay
2404 (tramp-compat-funcall
2405 'make-overlay
2406 (tramp-compat-funcall 'minibuffer-prompt-end)
2407 (tramp-compat-funcall 'minibuffer-prompt-end)))
2408 ;; Copy rfn-eshadow-overlay properties.
2409 (let ((props (tramp-compat-funcall
2410 'overlay-properties (symbol-value 'rfn-eshadow-overlay))))
2411 (while props
2412 (tramp-compat-funcall
2413 'overlay-put tramp-rfn-eshadow-overlay (pop props) (pop props))))))
2414
2415 (when (boundp 'rfn-eshadow-setup-minibuffer-hook)
2416 (add-hook 'rfn-eshadow-setup-minibuffer-hook
2417 'tramp-rfn-eshadow-setup-minibuffer)
2418 (add-hook 'tramp-unload-hook
2419 (lambda ()
2420 (remove-hook 'rfn-eshadow-setup-minibuffer-hook
2421 'tramp-rfn-eshadow-setup-minibuffer))))
2422
2423 (defconst tramp-rfn-eshadow-update-overlay-regexp
2424 (format "[^%s/~]*\\(/\\|~\\)" tramp-postfix-host-format))
2425
2426 (defun tramp-rfn-eshadow-update-overlay ()
2427 "Update `rfn-eshadow-overlay' to cover shadowed part of minibuffer input.
2428 This is intended to be used as a minibuffer `post-command-hook' for
2429 `file-name-shadow-mode'; the minibuffer should have already
2430 been set up by `rfn-eshadow-setup-minibuffer'."
2431 ;; In remote files name, there is a shadowing just for the local part.
2432 (let ((end (or (tramp-compat-funcall
2433 'overlay-end (symbol-value 'rfn-eshadow-overlay))
2434 (tramp-compat-funcall 'minibuffer-prompt-end))))
2435 (when
2436 (file-remote-p
2437 (tramp-compat-funcall 'buffer-substring-no-properties end (point-max)))
2438 (save-excursion
2439 (save-restriction
2440 (narrow-to-region
2441 (1+ (or (string-match
2442 tramp-rfn-eshadow-update-overlay-regexp (buffer-string) end)
2443 end))
2444 (point-max))
2445 (let ((rfn-eshadow-overlay tramp-rfn-eshadow-overlay)
2446 (rfn-eshadow-update-overlay-hook nil))
2447 (tramp-compat-funcall
2448 'move-overlay rfn-eshadow-overlay (point-max) (point-max))
2449 (tramp-compat-funcall 'rfn-eshadow-update-overlay)))))))
2450
2451 (when (boundp 'rfn-eshadow-update-overlay-hook)
2452 (add-hook 'rfn-eshadow-update-overlay-hook
2453 'tramp-rfn-eshadow-update-overlay)
2454 (add-hook 'tramp-unload-hook
2455 (lambda ()
2456 (remove-hook 'rfn-eshadow-update-overlay-hook
2457 'tramp-rfn-eshadow-update-overlay))))
2458
2459
2460 ;;; Integration of eshell.el:
2461
2462 (eval-when-compile
2463 (defvar eshell-path-env))
2464
2465 ;; eshell.el keeps the path in `eshell-path-env'. We must change it
2466 ;; when `default-directory' points to another host.
2467 (defun tramp-eshell-directory-change ()
2468 "Set `eshell-path-env' to $PATH of the host related to `default-directory'."
2469 (setq eshell-path-env
2470 (if (file-remote-p default-directory)
2471 (with-parsed-tramp-file-name default-directory nil
2472 (mapconcat
2473 'identity
2474 (tramp-get-remote-path v)
2475 ":"))
2476 (getenv "PATH"))))
2477
2478 (eval-after-load "esh-util"
2479 '(progn
2480 (tramp-eshell-directory-change)
2481 (add-hook 'eshell-directory-change-hook
2482 'tramp-eshell-directory-change)
2483 (add-hook 'tramp-unload-hook
2484 (lambda ()
2485 (remove-hook 'eshell-directory-change-hook
2486 'tramp-eshell-directory-change)))))
2487
2488
2489 ;;; File Name Handler Functions:
2490
2491 (defun tramp-handle-make-symbolic-link
2492 (filename linkname &optional ok-if-already-exists)
2493 "Like `make-symbolic-link' for Tramp files.
2494 If LINKNAME is a non-Tramp file, it is used verbatim as the target of
2495 the symlink. If LINKNAME is a Tramp file, only the localname component is
2496 used as the target of the symlink.
2497
2498 If LINKNAME is a Tramp file and the localname component is relative, then
2499 it is expanded first, before the localname component is taken. Note that
2500 this can give surprising results if the user/host for the source and
2501 target of the symlink differ."
2502 (with-parsed-tramp-file-name linkname l
2503 (let ((ln (tramp-get-remote-ln l))
2504 (cwd (tramp-run-real-handler
2505 'file-name-directory (list l-localname))))
2506 (unless ln
2507 (tramp-error
2508 l 'file-error
2509 "Making a symbolic link. ln(1) does not exist on the remote host."))
2510
2511 ;; Do the 'confirm if exists' thing.
2512 (when (file-exists-p linkname)
2513 ;; What to do?
2514 (if (or (null ok-if-already-exists) ; not allowed to exist
2515 (and (numberp ok-if-already-exists)
2516 (not (yes-or-no-p
2517 (format
2518 "File %s already exists; make it a link anyway? "
2519 l-localname)))))
2520 (tramp-error
2521 l 'file-already-exists "File %s already exists" l-localname)
2522 (tramp-compat-delete-file linkname 'force)))
2523
2524 ;; If FILENAME is a Tramp name, use just the localname component.
2525 (when (tramp-tramp-file-p filename)
2526 (setq filename
2527 (tramp-file-name-localname
2528 (tramp-dissect-file-name (expand-file-name filename)))))
2529
2530 ;; Right, they are on the same host, regardless of user, method, etc.
2531 ;; We now make the link on the remote machine. This will occur as the user
2532 ;; that FILENAME belongs to.
2533 (zerop
2534 (tramp-send-command-and-check
2535 l
2536 (format
2537 "cd %s && %s -sf %s %s"
2538 (tramp-shell-quote-argument cwd)
2539 ln
2540 (tramp-shell-quote-argument filename)
2541 (tramp-shell-quote-argument l-localname))
2542 t)))))
2543
2544 (defun tramp-handle-load (file &optional noerror nomessage nosuffix must-suffix)
2545 "Like `load' for Tramp files."
2546 (with-parsed-tramp-file-name (expand-file-name file) nil
2547 (unless nosuffix
2548 (cond ((file-exists-p (concat file ".elc"))
2549 (setq file (concat file ".elc")))
2550 ((file-exists-p (concat file ".el"))
2551 (setq file (concat file ".el")))))
2552 (when must-suffix
2553 ;; The first condition is always true for absolute file names.
2554 ;; Included for safety's sake.
2555 (unless (or (file-name-directory file)
2556 (string-match "\\.elc?\\'" file))
2557 (tramp-error
2558 v 'file-error
2559 "File `%s' does not include a `.el' or `.elc' suffix" file)))
2560 (unless noerror
2561 (when (not (file-exists-p file))
2562 (tramp-error v 'file-error "Cannot load nonexistent file `%s'" file)))
2563 (if (not (file-exists-p file))
2564 nil
2565 (let ((tramp-message-show-message (not nomessage)))
2566 (with-progress-reporter v 0 (format "Loading %s" file)
2567 (let ((local-copy (file-local-copy file)))
2568 ;; MUST-SUFFIX doesn't exist on XEmacs, so let it default to nil.
2569 (unwind-protect
2570 (load local-copy noerror t t)
2571 (tramp-compat-delete-file local-copy 'force)))))
2572 t)))
2573
2574 ;; Localname manipulation functions that grok Tramp localnames...
2575 (defun tramp-handle-file-name-as-directory (file)
2576 "Like `file-name-as-directory' but aware of Tramp files."
2577 ;; `file-name-as-directory' would be sufficient except localname is
2578 ;; the empty string.
2579 (let ((v (tramp-dissect-file-name file t)))
2580 ;; Run the command on the localname portion only.
2581 (tramp-make-tramp-file-name
2582 (tramp-file-name-method v)
2583 (tramp-file-name-user v)
2584 (tramp-file-name-host v)
2585 (tramp-run-real-handler
2586 'file-name-as-directory (list (or (tramp-file-name-localname v) ""))))))
2587
2588 (defun tramp-handle-file-name-directory (file)
2589 "Like `file-name-directory' but aware of Tramp files."
2590 ;; Everything except the last filename thing is the directory. We
2591 ;; cannot apply `with-parsed-tramp-file-name', because this expands
2592 ;; the remote file name parts. This is a problem when we are in
2593 ;; file name completion.
2594 (let ((v (tramp-dissect-file-name file t)))
2595 ;; Run the command on the localname portion only.
2596 (tramp-make-tramp-file-name
2597 (tramp-file-name-method v)
2598 (tramp-file-name-user v)
2599 (tramp-file-name-host v)
2600 (tramp-run-real-handler
2601 'file-name-directory (list (or (tramp-file-name-localname v) ""))))))
2602
2603 (defun tramp-handle-file-name-nondirectory (file)
2604 "Like `file-name-nondirectory' but aware of Tramp files."
2605 (with-parsed-tramp-file-name file nil
2606 (tramp-run-real-handler 'file-name-nondirectory (list localname))))
2607
2608 (defun tramp-handle-file-truename (filename &optional counter prev-dirs)
2609 "Like `file-truename' for Tramp files."
2610 (with-parsed-tramp-file-name (expand-file-name filename) nil
2611 (with-file-property v localname "file-truename"
2612 (let ((result nil)) ; result steps in reverse order
2613 (tramp-message v 4 "Finding true name for `%s'" filename)
2614 (cond
2615 ;; Use GNU readlink --canonicalize-missing where available.
2616 ((tramp-get-remote-readlink v)
2617 (setq result
2618 (tramp-send-command-and-read
2619 v
2620 (format "echo \"\\\"`%s --canonicalize-missing %s`\\\"\""
2621 (tramp-get-remote-readlink v)
2622 (tramp-shell-quote-argument localname)))))
2623
2624 ;; Use Perl implementation.
2625 ((and (tramp-get-remote-perl v)
2626 (tramp-get-connection-property v "perl-file-spec" nil)
2627 (tramp-get-connection-property v "perl-cwd-realpath" nil))
2628 (tramp-maybe-send-script
2629 v tramp-perl-file-truename "tramp_perl_file_truename")
2630 (setq result
2631 (tramp-send-command-and-read
2632 v
2633 (format "tramp_perl_file_truename %s"
2634 (tramp-shell-quote-argument localname)))))
2635
2636 ;; Do it yourself. We bind `directory-sep-char' here for
2637 ;; XEmacs on Windows, which would otherwise use backslash.
2638 (t (let* ((directory-sep-char ?/)
2639 (steps (tramp-compat-split-string localname "/"))
2640 (localnamedir (tramp-run-real-handler
2641 'file-name-as-directory (list localname)))
2642 (is-dir (string= localname localnamedir))
2643 (thisstep nil)
2644 (numchase 0)
2645 ;; Don't make the following value larger than
2646 ;; necessary. People expect an error message in a
2647 ;; timely fashion when something is wrong;
2648 ;; otherwise they might think that Emacs is hung.
2649 ;; Of course, correctness has to come first.
2650 (numchase-limit 20)
2651 symlink-target)
2652 (while (and steps (< numchase numchase-limit))
2653 (setq thisstep (pop steps))
2654 (tramp-message
2655 v 5 "Check %s"
2656 (mapconcat 'identity
2657 (append '("") (reverse result) (list thisstep))
2658 "/"))
2659 (setq symlink-target
2660 (nth 0 (file-attributes
2661 (tramp-make-tramp-file-name
2662 method user host
2663 (mapconcat 'identity
2664 (append '("")
2665 (reverse result)
2666 (list thisstep))
2667 "/")))))
2668 (cond ((string= "." thisstep)
2669 (tramp-message v 5 "Ignoring step `.'"))
2670 ((string= ".." thisstep)
2671 (tramp-message v 5 "Processing step `..'")
2672 (pop result))
2673 ((stringp symlink-target)
2674 ;; It's a symlink, follow it.
2675 (tramp-message v 5 "Follow symlink to %s" symlink-target)
2676 (setq numchase (1+ numchase))
2677 (when (file-name-absolute-p symlink-target)
2678 (setq result nil))
2679 ;; If the symlink was absolute, we'll get a string like
2680 ;; "/user@host:/some/target"; extract the
2681 ;; "/some/target" part from it.
2682 (when (tramp-tramp-file-p symlink-target)
2683 (unless (tramp-equal-remote filename symlink-target)
2684 (tramp-error
2685 v 'file-error
2686 "Symlink target `%s' on wrong host" symlink-target))
2687 (setq symlink-target localname))
2688 (setq steps
2689 (append (tramp-compat-split-string
2690 symlink-target "/")
2691 steps)))
2692 (t
2693 ;; It's a file.
2694 (setq result (cons thisstep result)))))
2695 (when (>= numchase numchase-limit)
2696 (tramp-error
2697 v 'file-error
2698 "Maximum number (%d) of symlinks exceeded" numchase-limit))
2699 (setq result (reverse result))
2700 ;; Combine list to form string.
2701 (setq result
2702 (if result
2703 (mapconcat 'identity (cons "" result) "/")
2704 "/"))
2705 (when (and is-dir (or (string= "" result)
2706 (not (string= (substring result -1) "/"))))
2707 (setq result (concat result "/"))))))
2708
2709 (tramp-message v 4 "True name of `%s' is `%s'" filename result)
2710 (tramp-make-tramp-file-name method user host result)))))
2711
2712 ;; Basic functions.
2713
2714 (defun tramp-handle-file-exists-p (filename)
2715 "Like `file-exists-p' for Tramp files."
2716 (with-parsed-tramp-file-name filename nil
2717 (with-file-property v localname "file-exists-p"
2718 (or (not (null (tramp-get-file-property
2719 v localname "file-attributes-integer" nil)))
2720 (not (null (tramp-get-file-property
2721 v localname "file-attributes-string" nil)))
2722 (zerop (tramp-send-command-and-check
2723 v
2724 (format
2725 "%s %s"
2726 (tramp-get-file-exists-command v)
2727 (tramp-shell-quote-argument localname))))))))
2728
2729 ;; Inodes don't exist for some file systems. Therefore we must
2730 ;; generate virtual ones. Used in `find-buffer-visiting'. The method
2731 ;; applied might be not so efficient (Ange-FTP uses hashes). But
2732 ;; performance isn't the major issue given that file transfer will
2733 ;; take time.
2734 (defvar tramp-inodes nil
2735 "Keeps virtual inodes numbers.")
2736
2737 ;; Devices must distinguish physical file systems. The device numbers
2738 ;; provided by "lstat" aren't unique, because we operate on different hosts.
2739 ;; So we use virtual device numbers, generated by Tramp. Both Ange-FTP and
2740 ;; EFS use device number "-1". In order to be different, we use device number
2741 ;; (-1 . x), whereby "x" is unique for a given (method user host).
2742 (defvar tramp-devices nil
2743 "Keeps virtual device numbers.")
2744
2745 ;; CCC: This should check for an error condition and signal failure
2746 ;; when something goes wrong.
2747 ;; Daniel Pittman <daniel@danann.net>
2748 (defun tramp-handle-file-attributes (filename &optional id-format)
2749 "Like `file-attributes' for Tramp files."
2750 (unless id-format (setq id-format 'integer))
2751 ;; Don't modify `last-coding-system-used' by accident.
2752 (let ((last-coding-system-used last-coding-system-used))
2753 (with-parsed-tramp-file-name (expand-file-name filename) nil
2754 (with-file-property v localname (format "file-attributes-%s" id-format)
2755 (save-excursion
2756 (tramp-convert-file-attributes
2757 v
2758 (cond
2759 ((tramp-get-remote-stat v)
2760 (tramp-do-file-attributes-with-stat v localname id-format))
2761 ((tramp-get-remote-perl v)
2762 (tramp-do-file-attributes-with-perl v localname id-format))
2763 (t
2764 (tramp-do-file-attributes-with-ls v localname id-format)))))))))
2765
2766 (defun tramp-do-file-attributes-with-ls (vec localname &optional id-format)
2767 "Implement `file-attributes' for Tramp files using the ls(1) command."
2768 (let (symlinkp dirp
2769 res-inode res-filemodes res-numlinks
2770 res-uid res-gid res-size res-symlink-target)
2771 (tramp-message vec 5 "file attributes with ls: %s" localname)
2772 (tramp-send-command
2773 vec
2774 (format "(%s %s || %s -h %s) && %s %s %s"
2775 (tramp-get-file-exists-command vec)
2776 (tramp-shell-quote-argument localname)
2777 (tramp-get-test-command vec)
2778 (tramp-shell-quote-argument localname)
2779 (tramp-get-ls-command vec)
2780 (if (eq id-format 'integer) "-ildn" "-ild")
2781 (tramp-shell-quote-argument localname)))
2782 ;; parse `ls -l' output ...
2783 (with-current-buffer (tramp-get-buffer vec)
2784 (when (> (buffer-size) 0)
2785 (goto-char (point-min))
2786 ;; ... inode
2787 (setq res-inode
2788 (condition-case err
2789 (read (current-buffer))
2790 (invalid-read-syntax
2791 (when (and (equal (cadr err)
2792 "Integer constant overflow in reader")
2793 (string-match
2794 "^[0-9]+\\([0-9][0-9][0-9][0-9][0-9]\\)\\'"
2795 (car (cddr err))))
2796 (let* ((big (read (substring (car (cddr err)) 0
2797 (match-beginning 1))))
2798 (small (read (match-string 1 (car (cddr err)))))
2799 (twiddle (/ small 65536)))
2800 (cons (+ big twiddle)
2801 (- small (* twiddle 65536))))))))
2802 ;; ... file mode flags
2803 (setq res-filemodes (symbol-name (read (current-buffer))))
2804 ;; ... number links
2805 (setq res-numlinks (read (current-buffer)))
2806 ;; ... uid and gid
2807 (setq res-uid (read (current-buffer)))
2808 (setq res-gid (read (current-buffer)))
2809 (if (eq id-format 'integer)
2810 (progn
2811 (unless (numberp res-uid) (setq res-uid -1))
2812 (unless (numberp res-gid) (setq res-gid -1)))
2813 (progn
2814 (unless (stringp res-uid) (setq res-uid (symbol-name res-uid)))
2815 (unless (stringp res-gid) (setq res-gid (symbol-name res-gid)))))
2816 ;; ... size
2817 (setq res-size (read (current-buffer)))
2818 ;; From the file modes, figure out other stuff.
2819 (setq symlinkp (eq ?l (aref res-filemodes 0)))
2820 (setq dirp (eq ?d (aref res-filemodes 0)))
2821 ;; if symlink, find out file name pointed to
2822 (when symlinkp
2823 (search-forward "-> ")
2824 (setq res-symlink-target
2825 (buffer-substring (point) (tramp-compat-line-end-position))))
2826 ;; return data gathered
2827 (list
2828 ;; 0. t for directory, string (name linked to) for symbolic
2829 ;; link, or nil.
2830 (or dirp res-symlink-target)
2831 ;; 1. Number of links to file.
2832 res-numlinks
2833 ;; 2. File uid.
2834 res-uid
2835 ;; 3. File gid.
2836 res-gid
2837 ;; 4. Last access time, as a list of two integers. First
2838 ;; integer has high-order 16 bits of time, second has low 16
2839 ;; bits.
2840 ;; 5. Last modification time, likewise.
2841 ;; 6. Last status change time, likewise.
2842 '(0 0) '(0 0) '(0 0) ;CCC how to find out?
2843 ;; 7. Size in bytes (-1, if number is out of range).
2844 res-size
2845 ;; 8. File modes, as a string of ten letters or dashes as in ls -l.
2846 res-filemodes
2847 ;; 9. t if file's gid would change if file were deleted and
2848 ;; recreated. Will be set in `tramp-convert-file-attributes'
2849 t
2850 ;; 10. inode number.
2851 res-inode
2852 ;; 11. Device number. Will be replaced by a virtual device number.
2853 -1
2854 )))))
2855
2856 (defun tramp-do-file-attributes-with-perl
2857 (vec localname &optional id-format)
2858 "Implement `file-attributes' for Tramp files using a Perl script."
2859 (tramp-message vec 5 "file attributes with perl: %s" localname)
2860 (tramp-maybe-send-script
2861 vec tramp-perl-file-attributes "tramp_perl_file_attributes")
2862 (tramp-send-command-and-read
2863 vec
2864 (format "tramp_perl_file_attributes %s %s"
2865 (tramp-shell-quote-argument localname) id-format)))
2866
2867 (defun tramp-do-file-attributes-with-stat
2868 (vec localname &optional id-format)
2869 "Implement `file-attributes' for Tramp files using stat(1) command."
2870 (tramp-message vec 5 "file attributes with stat: %s" localname)
2871 (tramp-send-command-and-read
2872 vec
2873 (format
2874 ;; On Opsware, pdksh (which is the true name of ksh there) doesn't
2875 ;; parse correctly the sequence "((". Therefore, we add a space.
2876 "( (%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)"
2877 (tramp-get-file-exists-command vec)
2878 (tramp-shell-quote-argument localname)
2879 (tramp-get-test-command vec)
2880 (tramp-shell-quote-argument localname)
2881 (tramp-get-remote-stat vec)
2882 (if (eq id-format 'integer) "%u" "\"%U\"")
2883 (if (eq id-format 'integer) "%g" "\"%G\"")
2884 (tramp-shell-quote-argument localname))))
2885
2886 (defun tramp-handle-set-visited-file-modtime (&optional time-list)
2887 "Like `set-visited-file-modtime' for Tramp files."
2888 (unless (buffer-file-name)
2889 (error "Can't set-visited-file-modtime: buffer `%s' not visiting a file"
2890 (buffer-name)))
2891 (if time-list
2892 (tramp-run-real-handler 'set-visited-file-modtime (list time-list))
2893 (let ((f (buffer-file-name))
2894 coding-system-used)
2895 (with-parsed-tramp-file-name f nil
2896 (let* ((attr (file-attributes f))
2897 ;; '(-1 65535) means file doesn't exists yet.
2898 (modtime (or (nth 5 attr) '(-1 65535))))
2899 (when (boundp 'last-coding-system-used)
2900 (setq coding-system-used (symbol-value 'last-coding-system-used)))
2901 ;; We use '(0 0) as a don't-know value. See also
2902 ;; `tramp-do-file-attributes-with-ls'.
2903 (if (not (equal modtime '(0 0)))
2904 (tramp-run-real-handler 'set-visited-file-modtime (list modtime))
2905 (progn
2906 (tramp-send-command
2907 v
2908 (format "%s -ild %s"
2909 (tramp-get-ls-command v)
2910 (tramp-shell-quote-argument localname)))
2911 (setq attr (buffer-substring (point)
2912 (progn (end-of-line) (point)))))
2913 (tramp-set-file-property
2914 v localname "visited-file-modtime-ild" attr))
2915 (when (boundp 'last-coding-system-used)
2916 (set 'last-coding-system-used coding-system-used))
2917 nil)))))
2918
2919 ;; This function makes the same assumption as
2920 ;; `tramp-handle-set-visited-file-modtime'.
2921 (defun tramp-handle-verify-visited-file-modtime (buf)
2922 "Like `verify-visited-file-modtime' for Tramp files.
2923 At the time `verify-visited-file-modtime' calls this function, we
2924 already know that the buffer is visiting a file and that
2925 `visited-file-modtime' does not return 0. Do not call this
2926 function directly, unless those two cases are already taken care
2927 of."
2928 (with-current-buffer buf
2929 (let ((f (buffer-file-name)))
2930 ;; There is no file visiting the buffer, or the buffer has no
2931 ;; recorded last modification time, or there is no established
2932 ;; connection.
2933 (if (or (not f)
2934 (eq (visited-file-modtime) 0)
2935 (not (tramp-file-name-handler 'file-remote-p f nil 'connected)))
2936 t
2937 (with-parsed-tramp-file-name f nil
2938 (tramp-flush-file-property v localname)
2939 (let* ((attr (file-attributes f))
2940 (modtime (nth 5 attr))
2941 (mt (visited-file-modtime)))
2942
2943 (cond
2944 ;; File exists, and has a known modtime.
2945 ((and attr (not (equal modtime '(0 0))))
2946 (< (abs (tramp-time-diff
2947 modtime
2948 ;; For compatibility, deal with both the old
2949 ;; (HIGH . LOW) and the new (HIGH LOW) return
2950 ;; values of `visited-file-modtime'.
2951 (if (atom (cdr mt))
2952 (list (car mt) (cdr mt))
2953 mt)))
2954 2))
2955 ;; Modtime has the don't know value.
2956 (attr
2957 (tramp-send-command
2958 v
2959 (format "%s -ild %s"
2960 (tramp-get-ls-command v)
2961 (tramp-shell-quote-argument localname)))
2962 (with-current-buffer (tramp-get-buffer v)
2963 (setq attr (buffer-substring
2964 (point) (progn (end-of-line) (point)))))
2965 (equal
2966 attr
2967 (tramp-get-file-property
2968 v localname "visited-file-modtime-ild" "")))
2969 ;; If file does not exist, say it is not modified if and
2970 ;; only if that agrees with the buffer's record.
2971 (t (equal mt '(-1 65535))))))))))
2972
2973 (defun tramp-handle-set-file-modes (filename mode)
2974 "Like `set-file-modes' for Tramp files."
2975 (with-parsed-tramp-file-name filename nil
2976 (tramp-flush-file-property v localname)
2977 (unless (zerop (tramp-send-command-and-check
2978 v
2979 (format "chmod %s %s"
2980 (tramp-decimal-to-octal mode)
2981 (tramp-shell-quote-argument localname))))
2982 ;; FIXME: extract the proper text from chmod's stderr.
2983 (tramp-error
2984 v 'file-error "Error while changing file's mode %s" filename))))
2985
2986 (defun tramp-handle-set-file-times (filename &optional time)
2987 "Like `set-file-times' for Tramp files."
2988 (zerop
2989 (if (file-remote-p filename)
2990 (with-parsed-tramp-file-name filename nil
2991 (tramp-flush-file-property v localname)
2992 (let ((time (if (or (null time) (equal time '(0 0)))
2993 (current-time)
2994 time))
2995 ;; With GNU Emacs, `format-time-string' has an optional
2996 ;; parameter UNIVERSAL. This is preferred, because we
2997 ;; could handle the case when the remote host is
2998 ;; located in a different time zone as the local host.
2999 (utc (not (featurep 'xemacs))))
3000 (tramp-send-command-and-check
3001 v (format "%s touch -t %s %s"
3002 (if utc "TZ=UTC; export TZ;" "")
3003 (if utc
3004 (format-time-string "%Y%m%d%H%M.%S" time t)
3005 (format-time-string "%Y%m%d%H%M.%S" time))
3006 (tramp-shell-quote-argument localname)))))
3007
3008 ;; We handle also the local part, because in older Emacsen,
3009 ;; without `set-file-times', this function is an alias for this.
3010 ;; We are local, so we don't need the UTC settings.
3011 (tramp-local-call-process
3012 "touch" nil nil nil "-t"
3013 (format-time-string "%Y%m%d%H%M.%S" time)
3014 (tramp-shell-quote-argument filename)))))
3015
3016 (defun tramp-set-file-uid-gid (filename &optional uid gid)
3017 "Set the ownership for FILENAME.
3018 If UID and GID are provided, these values are used; otherwise uid
3019 and gid of the corresponding user is taken. Both parameters must be integers."
3020 ;; Modern Unices allow chown only for root. So we might need
3021 ;; another implementation, see `dired-do-chown'. OTOH, it is mostly
3022 ;; working with su(do)? when it is needed, so it shall succeed in
3023 ;; the majority of cases.
3024 ;; Don't modify `last-coding-system-used' by accident.
3025 (let ((last-coding-system-used last-coding-system-used))
3026 (if (file-remote-p filename)
3027 (with-parsed-tramp-file-name filename nil
3028 (if (and (zerop (user-uid)) (tramp-local-host-p v))
3029 ;; If we are root on the local host, we can do it directly.
3030 (tramp-set-file-uid-gid localname uid gid)
3031 (let ((uid (or (and (integerp uid) uid)
3032 (tramp-get-remote-uid v 'integer)))
3033 (gid (or (and (integerp gid) gid)
3034 (tramp-get-remote-gid v 'integer))))
3035 (tramp-send-command
3036 v (format
3037 "chown %d:%d %s" uid gid
3038 (tramp-shell-quote-argument localname))))))
3039
3040 ;; We handle also the local part, because there doesn't exist
3041 ;; `set-file-uid-gid'. On W32 "chown" might not work.
3042 (let ((uid (or (and (integerp uid) uid) (tramp-get-local-uid 'integer)))
3043 (gid (or (and (integerp gid) gid) (tramp-get-local-gid 'integer))))
3044 (tramp-local-call-process
3045 "chown" nil nil nil
3046 (format "%d:%d" uid gid) (tramp-shell-quote-argument filename))))))
3047
3048 (defun tramp-remote-selinux-p (vec)
3049 "Check, whether SELINUX is enabled on the remote host."
3050 (with-connection-property (tramp-get-connection-process vec) "selinux-p"
3051 (let ((result (tramp-find-executable
3052 vec "getenforce" (tramp-get-remote-path vec) t t)))
3053 (and result
3054 (string-equal
3055 (tramp-send-command-and-read
3056 vec (format "echo \\\"`%S`\\\"" result))
3057 "Enforcing")))))
3058
3059 (defun tramp-handle-file-selinux-context (filename)
3060 "Like `file-selinux-context' for Tramp files."
3061 (with-parsed-tramp-file-name filename nil
3062 (with-file-property v localname "file-selinux-context"
3063 (let ((context '(nil nil nil nil))
3064 (regexp (concat "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\):"
3065 "\\([a-z0-9_]+\\):" "\\([a-z0-9_]+\\)")))
3066 (when (and (tramp-remote-selinux-p v)
3067 (zerop (tramp-send-command-and-check
3068 v (format
3069 "%s -d -Z %s"
3070 (tramp-get-ls-command v)
3071 (tramp-shell-quote-argument localname)))))
3072 (with-current-buffer (tramp-get-connection-buffer v)
3073 (goto-char (point-min))
3074 (when (re-search-forward regexp (tramp-compat-line-end-position) t)
3075 (setq context (list (match-string 1) (match-string 2)
3076 (match-string 3) (match-string 4))))))
3077 ;; Return the context.
3078 context))))
3079
3080 (defun tramp-handle-set-file-selinux-context (filename context)
3081 "Like `set-file-selinux-context' for Tramp files."
3082 (with-parsed-tramp-file-name filename nil
3083 (if (and (consp context)
3084 (tramp-remote-selinux-p v)
3085 (zerop (tramp-send-command-and-check
3086 v (format "chcon %s %s %s %s %s"
3087 (if (stringp (nth 0 context))
3088 (format "--user=%s" (nth 0 context)) "")
3089 (if (stringp (nth 1 context))
3090 (format "--role=%s" (nth 1 context)) "")
3091 (if (stringp (nth 2 context))
3092 (format "--type=%s" (nth 2 context)) "")
3093 (if (stringp (nth 3 context))
3094 (format "--range=%s" (nth 3 context)) "")
3095 (tramp-shell-quote-argument localname)))))
3096 (tramp-set-file-property v localname "file-selinux-context" context)
3097 (tramp-set-file-property v localname "file-selinux-context" 'undef)))
3098 ;; We always return nil.
3099 nil)
3100
3101 ;; Simple functions using the `test' command.
3102
3103 (defun tramp-handle-file-executable-p (filename)
3104 "Like `file-executable-p' for Tramp files."
3105 (with-parsed-tramp-file-name filename nil
3106 (with-file-property v localname "file-executable-p"
3107 ;; Examine `file-attributes' cache to see if request can be
3108 ;; satisfied without remote operation.
3109 (or (tramp-check-cached-permissions v ?x)
3110 (zerop (tramp-run-test "-x" filename))))))
3111
3112 (defun tramp-handle-file-readable-p (filename)
3113 "Like `file-readable-p' for Tramp files."
3114 (with-parsed-tramp-file-name filename nil
3115 (with-file-property v localname "file-readable-p"
3116 ;; Examine `file-attributes' cache to see if request can be
3117 ;; satisfied without remote operation.
3118 (or (tramp-check-cached-permissions v ?r)
3119 (zerop (tramp-run-test "-r" filename))))))
3120
3121 ;; When the remote shell is started, it looks for a shell which groks
3122 ;; tilde expansion. Here, we assume that all shells which grok tilde
3123 ;; expansion will also provide a `test' command which groks `-nt' (for
3124 ;; newer than). If this breaks, tell me about it and I'll try to do
3125 ;; something smarter about it.
3126 (defun tramp-handle-file-newer-than-file-p (file1 file2)
3127 "Like `file-newer-than-file-p' for Tramp files."
3128 (cond ((not (file-exists-p file1))
3129 nil)
3130 ((not (file-exists-p file2))
3131 t)
3132 ;; We are sure both files exist at this point.
3133 (t
3134 (save-excursion
3135 ;; We try to get the mtime of both files. If they are not
3136 ;; equal to the "dont-know" value, then we subtract the times
3137 ;; and obtain the result.
3138 (let ((fa1 (file-attributes file1))
3139 (fa2 (file-attributes file2)))
3140 (if (and (not (equal (nth 5 fa1) '(0 0)))
3141 (not (equal (nth 5 fa2) '(0 0))))
3142 (> 0 (tramp-time-diff (nth 5 fa2) (nth 5 fa1)))
3143 ;; If one of them is the dont-know value, then we can
3144 ;; still try to run a shell command on the remote host.
3145 ;; However, this only works if both files are Tramp
3146 ;; files and both have the same method, same user, same
3147 ;; host.
3148 (unless (tramp-equal-remote file1 file2)
3149 (with-parsed-tramp-file-name
3150 (if (tramp-tramp-file-p file1) file1 file2) nil
3151 (tramp-error
3152 v 'file-error
3153 "Files %s and %s must have same method, user, host"
3154 file1 file2)))
3155 (with-parsed-tramp-file-name file1 nil
3156 (zerop (tramp-run-test2
3157 (tramp-get-test-nt-command v) file1 file2)))))))))
3158
3159 ;; Functions implemented using the basic functions above.
3160
3161 (defun tramp-handle-file-modes (filename)
3162 "Like `file-modes' for Tramp files."
3163 (let ((truename (or (file-truename filename) filename)))
3164 (when (file-exists-p truename)
3165 (tramp-mode-string-to-int (nth 8 (file-attributes truename))))))
3166
3167 (defun tramp-default-file-modes (filename)
3168 "Return file modes of FILENAME as integer.
3169 If the file modes of FILENAME cannot be determined, return the
3170 value of `default-file-modes', without execute permissions."
3171 (or (file-modes filename)
3172 (logand (default-file-modes) (tramp-octal-to-decimal "0666"))))
3173
3174 (defun tramp-handle-file-directory-p (filename)
3175 "Like `file-directory-p' for Tramp files."
3176 ;; Care must be taken that this function returns `t' for symlinks
3177 ;; pointing to directories. Surely the most obvious implementation
3178 ;; would be `test -d', but that returns false for such symlinks.
3179 ;; CCC: Stefan Monnier says that `test -d' follows symlinks. And
3180 ;; I now think he's right. So we could be using `test -d', couldn't
3181 ;; we?
3182 ;;
3183 ;; Alternatives: `cd %s', `test -d %s'
3184 (with-parsed-tramp-file-name filename nil
3185 (with-file-property v localname "file-directory-p"
3186 (zerop (tramp-run-test "-d" filename)))))
3187
3188 (defun tramp-handle-file-regular-p (filename)
3189 "Like `file-regular-p' for Tramp files."
3190 (and (file-exists-p filename)
3191 (eq ?- (aref (nth 8 (file-attributes filename)) 0))))
3192
3193 (defun tramp-handle-file-symlink-p (filename)
3194 "Like `file-symlink-p' for Tramp files."
3195 (with-parsed-tramp-file-name filename nil
3196 (let ((x (car (file-attributes filename))))
3197 (when (stringp x)
3198 ;; When Tramp is running on VMS, then `file-name-absolute-p'
3199 ;; might do weird things.
3200 (if (file-name-absolute-p x)
3201 (tramp-make-tramp-file-name method user host x)
3202 x)))))
3203
3204 (defun tramp-handle-file-writable-p (filename)
3205 "Like `file-writable-p' for Tramp files."
3206 (with-parsed-tramp-file-name filename nil
3207 (with-file-property v localname "file-writable-p"
3208 (if (file-exists-p filename)
3209 ;; Examine `file-attributes' cache to see if request can be
3210 ;; satisfied without remote operation.
3211 (or (tramp-check-cached-permissions v ?w)
3212 (zerop (tramp-run-test "-w" filename)))
3213 ;; If file doesn't exist, check if directory is writable.
3214 (and (zerop (tramp-run-test
3215 "-d" (file-name-directory filename)))
3216 (zerop (tramp-run-test
3217 "-w" (file-name-directory filename))))))))
3218
3219 (defun tramp-handle-file-ownership-preserved-p (filename)
3220 "Like `file-ownership-preserved-p' for Tramp files."
3221 (with-parsed-tramp-file-name filename nil
3222 (with-file-property v localname "file-ownership-preserved-p"
3223 (let ((attributes (file-attributes filename)))
3224 ;; Return t if the file doesn't exist, since it's true that no
3225 ;; information would be lost by an (attempted) delete and create.
3226 (or (null attributes)
3227 (= (nth 2 attributes) (tramp-get-remote-uid v 'integer)))))))
3228
3229 ;; Other file name ops.
3230
3231 (defun tramp-handle-directory-file-name (directory)
3232 "Like `directory-file-name' for Tramp files."
3233 ;; If localname component of filename is "/", leave it unchanged.
3234 ;; Otherwise, remove any trailing slash from localname component.
3235 ;; Method, host, etc, are unchanged. Does it make sense to try
3236 ;; to avoid parsing the filename?
3237 (with-parsed-tramp-file-name directory nil
3238 (if (and (not (zerop (length localname)))
3239 (eq (aref localname (1- (length localname))) ?/)
3240 (not (string= localname "/")))
3241 (substring directory 0 -1)
3242 directory)))
3243
3244 ;; Directory listings.
3245
3246 (defun tramp-handle-directory-files
3247 (directory &optional full match nosort files-only)
3248 "Like `directory-files' for Tramp files."
3249 ;; FILES-ONLY is valid for XEmacs only.
3250 (when (file-directory-p directory)
3251 (setq directory (file-name-as-directory (expand-file-name directory)))
3252 (let ((temp (nreverse (file-name-all-completions "" directory)))
3253 result item)
3254
3255 (while temp
3256 (setq item (directory-file-name (pop temp)))
3257 (when (and (or (null match) (string-match match item))
3258 (or (null files-only)
3259 ;; Files only.
3260 (and (equal files-only t) (file-regular-p item))
3261 ;; Directories only.
3262 (file-directory-p item)))
3263 (push (if full (concat directory item) item)
3264 result)))
3265 (if nosort result (sort result 'string<)))))
3266
3267 (defun tramp-handle-directory-files-and-attributes
3268 (directory &optional full match nosort id-format)
3269 "Like `directory-files-and-attributes' for Tramp files."
3270 (unless id-format (setq id-format 'integer))
3271 (when (file-directory-p directory)
3272 (setq directory (expand-file-name directory))
3273 (let* ((temp
3274 (copy-tree
3275 (with-parsed-tramp-file-name directory nil
3276 (with-file-property
3277 v localname
3278 (format "directory-files-and-attributes-%s" id-format)
3279 (save-excursion
3280 (mapcar
3281 (lambda (x)
3282 (cons (car x)
3283 (tramp-convert-file-attributes v (cdr x))))
3284 (cond
3285 ((tramp-get-remote-stat v)
3286 (tramp-do-directory-files-and-attributes-with-stat
3287 v localname id-format))
3288 ((tramp-get-remote-perl v)
3289 (tramp-do-directory-files-and-attributes-with-perl
3290 v localname id-format)))))))))
3291 result item)
3292
3293 (while temp
3294 (setq item (pop temp))
3295 (when (or (null match) (string-match match (car item)))
3296 (when full
3297 (setcar item (expand-file-name (car item) directory)))
3298 (push item result)))
3299
3300 (if nosort
3301 result
3302 (sort result (lambda (x y) (string< (car x) (car y))))))))
3303
3304 (defun tramp-do-directory-files-and-attributes-with-perl
3305 (vec localname &optional id-format)
3306 "Implement `directory-files-and-attributes' for Tramp files using a Perl script."
3307 (tramp-message vec 5 "directory-files-and-attributes with perl: %s" localname)
3308 (tramp-maybe-send-script
3309 vec tramp-perl-directory-files-and-attributes
3310 "tramp_perl_directory_files_and_attributes")
3311 (let ((object
3312 (tramp-send-command-and-read
3313 vec
3314 (format "tramp_perl_directory_files_and_attributes %s %s"
3315 (tramp-shell-quote-argument localname) id-format))))
3316 (when (stringp object) (tramp-error vec 'file-error object))
3317 object))
3318
3319 (defun tramp-do-directory-files-and-attributes-with-stat
3320 (vec localname &optional id-format)
3321 "Implement `directory-files-and-attributes' for Tramp files using stat(1) command."
3322 (tramp-message vec 5 "directory-files-and-attributes with stat: %s" localname)
3323 (tramp-send-command-and-read
3324 vec
3325 (format
3326 (concat
3327 ;; We must care about filenames with spaces, or starting with
3328 ;; "-"; this would confuse xargs. "ls -aQ" might be a solution,
3329 ;; but it does not work on all remote systems. Therefore, we
3330 ;; quote the filenames via sed.
3331 "cd %s; echo \"(\"; (%s -a | sed -e s/\\$/\\\"/g -e s/^/\\\"/g | xargs "
3332 "%s -c '(\"%%n\" (\"%%N\") %%h %s %s %%X.0 %%Y.0 %%Z.0 %%s.0 \"%%A\" t %%i.0 -1)'); "
3333 "echo \")\"")
3334 (tramp-shell-quote-argument localname)
3335 (tramp-get-ls-command vec)
3336 (tramp-get-remote-stat vec)
3337 (if (eq id-format 'integer) "%u" "\"%U\"")
3338 (if (eq id-format 'integer) "%g" "\"%G\""))))
3339
3340 ;; This function should return "foo/" for directories and "bar" for
3341 ;; files.
3342 (defun tramp-handle-file-name-all-completions (filename directory)
3343 "Like `file-name-all-completions' for Tramp files."
3344 (unless (save-match-data (string-match "/" filename))
3345 (with-parsed-tramp-file-name (expand-file-name directory) nil
3346
3347 (all-completions
3348 filename
3349 (mapcar
3350 'list
3351 (or
3352 ;; Try cache first
3353 (and
3354 ;; Ignore if expired
3355 (or (not (integerp tramp-completion-reread-directory-timeout))
3356 (<= (tramp-time-diff
3357 (current-time)
3358 (tramp-get-file-property
3359 v localname "last-completion" '(0 0 0)))
3360 tramp-completion-reread-directory-timeout))
3361
3362 ;; Try cache entries for filename, filename with last
3363 ;; character removed, filename with last two characters
3364 ;; removed, ..., and finally the empty string - all
3365 ;; concatenated to the local directory name
3366
3367 ;; This is inefficient for very long filenames, pity
3368 ;; `reduce' is not available...
3369 (car
3370 (apply
3371 'append
3372 (mapcar
3373 (lambda (x)
3374 (let ((cache-hit
3375 (tramp-get-file-property
3376 v
3377 (concat localname (substring filename 0 x))
3378 "file-name-all-completions"
3379 nil)))
3380 (when cache-hit (list cache-hit))))
3381 (tramp-compat-number-sequence (length filename) 0 -1)))))
3382
3383 ;; Cache expired or no matching cache entry found so we need
3384 ;; to perform a remote operation
3385 (let (result)
3386 ;; Get a list of directories and files, including reliably
3387 ;; tagging the directories with a trailing '/'. Because I
3388 ;; rock. --daniel@danann.net
3389
3390 ;; Changed to perform `cd' in the same remote op and only
3391 ;; get entries starting with `filename'. Capture any `cd'
3392 ;; error messages. Ensure any `cd' and `echo' aliases are
3393 ;; ignored.
3394 (tramp-send-command
3395 v
3396 (if (tramp-get-remote-perl v)
3397 (progn
3398 (tramp-maybe-send-script
3399 v tramp-perl-file-name-all-completions
3400 "tramp_perl_file_name_all_completions")
3401 (format "tramp_perl_file_name_all_completions %s %s %d"
3402 (tramp-shell-quote-argument localname)
3403 (tramp-shell-quote-argument filename)
3404 (if (symbol-value
3405 ;; `read-file-name-completion-ignore-case'
3406 ;; is introduced with Emacs 22.1.
3407 (if (boundp
3408 'read-file-name-completion-ignore-case)
3409 'read-file-name-completion-ignore-case
3410 'completion-ignore-case))
3411 1 0)))
3412
3413 (format (concat
3414 "(\\cd %s 2>&1 && (%s %s -a 2>/dev/null"
3415 ;; `ls' with wildcard might fail with `Argument
3416 ;; list too long' error in some corner cases; if
3417 ;; `ls' fails after `cd' succeeded, chances are
3418 ;; that's the case, so let's retry without
3419 ;; wildcard. This will return "too many" entries
3420 ;; but that isn't harmful.
3421 " || %s -a 2>/dev/null)"
3422 " | while read f; do"
3423 " if %s -d \"$f\" 2>/dev/null;"
3424 " then \\echo \"$f/\"; else \\echo \"$f\"; fi; done"
3425 " && \\echo ok) || \\echo fail")
3426 (tramp-shell-quote-argument localname)
3427 (tramp-get-ls-command v)
3428 ;; When `filename' is empty, just `ls' without
3429 ;; filename argument is more efficient than `ls *'
3430 ;; for very large directories and might avoid the
3431 ;; `Argument list too long' error.
3432 ;;
3433 ;; With and only with wildcard, we need to add
3434 ;; `-d' to prevent `ls' from descending into
3435 ;; sub-directories.
3436 (if (zerop (length filename))
3437 "."
3438 (concat (tramp-shell-quote-argument filename) "* -d"))
3439 (tramp-get-ls-command v)
3440 (tramp-get-test-command v))))
3441
3442 ;; Now grab the output.
3443 (with-current-buffer (tramp-get-buffer v)
3444 (goto-char (point-max))
3445
3446 ;; Check result code, found in last line of output
3447 (forward-line -1)
3448 (if (looking-at "^fail$")
3449 (progn
3450 ;; Grab error message from line before last line
3451 ;; (it was put there by `cd 2>&1')
3452 (forward-line -1)
3453 (tramp-error
3454 v 'file-error
3455 "tramp-handle-file-name-all-completions: %s"
3456 (buffer-substring
3457 (point) (tramp-compat-line-end-position))))
3458 ;; For peace of mind, if buffer doesn't end in `fail'
3459 ;; then it should end in `ok'. If neither are in the
3460 ;; buffer something went seriously wrong on the remote
3461 ;; side.
3462 (unless (looking-at "^ok$")
3463 (tramp-error
3464 v 'file-error
3465 "\
3466 tramp-handle-file-name-all-completions: internal error accessing `%s': `%s'"
3467 (tramp-shell-quote-argument localname) (buffer-string))))
3468
3469 (while (zerop (forward-line -1))
3470 (push (buffer-substring
3471 (point) (tramp-compat-line-end-position))
3472 result)))
3473
3474 ;; Because the remote op went through OK we know the
3475 ;; directory we `cd'-ed to exists
3476 (tramp-set-file-property
3477 v localname "file-exists-p" t)
3478
3479 ;; Because the remote op went through OK we know every
3480 ;; file listed by `ls' exists.
3481 (mapc (lambda (entry)
3482 (tramp-set-file-property
3483 v (concat localname entry) "file-exists-p" t))
3484 result)
3485
3486 (tramp-set-file-property
3487 v localname "last-completion" (current-time))
3488
3489 ;; Store result in the cache
3490 (tramp-set-file-property
3491 v (concat localname filename)
3492 "file-name-all-completions"
3493 result))))))))
3494
3495 (defun tramp-handle-file-name-completion
3496 (filename directory &optional predicate)
3497 "Like `file-name-completion' for Tramp files."
3498 (unless (tramp-tramp-file-p directory)
3499 (error
3500 "tramp-handle-file-name-completion invoked on non-tramp directory `%s'"
3501 directory))
3502 (try-completion
3503 filename
3504 (mapcar 'list (file-name-all-completions filename directory))
3505 (when predicate
3506 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
3507
3508 ;; cp, mv and ln
3509
3510 (defun tramp-handle-add-name-to-file
3511 (filename newname &optional ok-if-already-exists)
3512 "Like `add-name-to-file' for Tramp files."
3513 (unless (tramp-equal-remote filename newname)
3514 (with-parsed-tramp-file-name
3515 (if (tramp-tramp-file-p filename) filename newname) nil
3516 (tramp-error
3517 v 'file-error
3518 "add-name-to-file: %s"
3519 "only implemented for same method, same user, same host")))
3520 (with-parsed-tramp-file-name filename v1
3521 (with-parsed-tramp-file-name newname v2
3522 (let ((ln (when v1 (tramp-get-remote-ln v1))))
3523 (when (and (not ok-if-already-exists)
3524 (file-exists-p newname)
3525 (not (numberp ok-if-already-exists))
3526 (y-or-n-p
3527 (format
3528 "File %s already exists; make it a new name anyway? "
3529 newname)))
3530 (tramp-error
3531 v2 'file-error
3532 "add-name-to-file: file %s already exists" newname))
3533 (tramp-flush-file-property v2 (file-name-directory v2-localname))
3534 (tramp-flush-file-property v2 v2-localname)
3535 (tramp-barf-unless-okay
3536 v1
3537 (format "%s %s %s" ln (tramp-shell-quote-argument v1-localname)
3538 (tramp-shell-quote-argument v2-localname))
3539 "error with add-name-to-file, see buffer `%s' for details"
3540 (buffer-name))))))
3541
3542 (defun tramp-handle-copy-file
3543 (filename newname &optional ok-if-already-exists keep-date
3544 preserve-uid-gid preserve-selinux-context)
3545 "Like `copy-file' for Tramp files."
3546 (setq filename (expand-file-name filename))
3547 (setq newname (expand-file-name newname))
3548 (cond
3549 ;; At least one file a Tramp file?
3550 ((or (tramp-tramp-file-p filename)
3551 (tramp-tramp-file-p newname))
3552 (tramp-do-copy-or-rename-file
3553 'copy filename newname ok-if-already-exists keep-date
3554 preserve-uid-gid preserve-selinux-context))
3555 ;; Compat section.
3556 (preserve-selinux-context
3557 (tramp-run-real-handler
3558 'copy-file
3559 (list filename newname ok-if-already-exists keep-date
3560 preserve-uid-gid preserve-selinux-context)))
3561 (preserve-uid-gid
3562 (tramp-run-real-handler
3563 'copy-file
3564 (list filename newname ok-if-already-exists keep-date preserve-uid-gid)))
3565 (t
3566 (tramp-run-real-handler
3567 'copy-file (list filename newname ok-if-already-exists keep-date)))))
3568
3569 (defun tramp-handle-copy-directory (dirname newname &optional keep-date parents)
3570 "Like `copy-directory' for Tramp files."
3571 (let ((t1 (tramp-tramp-file-p dirname))
3572 (t2 (tramp-tramp-file-p newname)))
3573 (with-parsed-tramp-file-name (if t1 dirname newname) nil
3574 (if (and (tramp-get-method-parameter method 'tramp-copy-recursive)
3575 ;; When DIRNAME and NEWNAME are remote, they must have
3576 ;; the same method.
3577 (or (null t1) (null t2)
3578 (string-equal
3579 (tramp-file-name-method (tramp-dissect-file-name dirname))
3580 (tramp-file-name-method (tramp-dissect-file-name newname)))))
3581 ;; scp or rsync DTRT.
3582 (progn
3583 (setq dirname (directory-file-name (expand-file-name dirname))
3584 newname (directory-file-name (expand-file-name newname)))
3585 (if (and (file-directory-p newname)
3586 (not (string-equal (file-name-nondirectory dirname)
3587 (file-name-nondirectory newname))))
3588 (setq newname
3589 (expand-file-name
3590 (file-name-nondirectory dirname) newname)))
3591 (if (not (file-directory-p (file-name-directory newname)))
3592 (make-directory (file-name-directory newname) parents))
3593 (tramp-do-copy-or-rename-file-out-of-band
3594 'copy dirname newname keep-date))
3595 ;; We must do it file-wise.
3596 (tramp-run-real-handler
3597 'copy-directory (list dirname newname keep-date parents)))
3598
3599 ;; When newname did exist, we have wrong cached values.
3600 (when t2
3601 (with-parsed-tramp-file-name newname nil
3602 (tramp-flush-file-property v (file-name-directory localname))
3603 (tramp-flush-file-property v localname))))))
3604
3605 (defun tramp-handle-rename-file
3606 (filename newname &optional ok-if-already-exists)
3607 "Like `rename-file' for Tramp files."
3608 ;; Check if both files are local -- invoke normal rename-file.
3609 ;; Otherwise, use Tramp from local system.
3610 (setq filename (expand-file-name filename))
3611 (setq newname (expand-file-name newname))
3612 ;; At least one file a Tramp file?
3613 (if (or (tramp-tramp-file-p filename)
3614 (tramp-tramp-file-p newname))
3615 (tramp-do-copy-or-rename-file
3616 'rename filename newname ok-if-already-exists t t)
3617 (tramp-run-real-handler
3618 'rename-file (list filename newname ok-if-already-exists))))
3619
3620 (defun tramp-do-copy-or-rename-file
3621 (op filename newname &optional ok-if-already-exists keep-date
3622 preserve-uid-gid preserve-selinux-context)
3623 "Copy or rename a remote file.
3624 OP must be `copy' or `rename' and indicates the operation to perform.
3625 FILENAME specifies the file to copy or rename, NEWNAME is the name of
3626 the new file (for copy) or the new name of the file (for rename).
3627 OK-IF-ALREADY-EXISTS means don't barf if NEWNAME exists already.
3628 KEEP-DATE means to make sure that NEWNAME has the same timestamp
3629 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3630 the uid and gid if both files are on the same host.
3631 PRESERVE-SELINUX-CONTEXT activates selinux commands.
3632
3633 This function is invoked by `tramp-handle-copy-file' and
3634 `tramp-handle-rename-file'. It is an error if OP is neither of `copy'
3635 and `rename'. FILENAME and NEWNAME must be absolute file names."
3636 (unless (memq op '(copy rename))
3637 (error "Unknown operation `%s', must be `copy' or `rename'" op))
3638 (let ((t1 (tramp-tramp-file-p filename))
3639 (t2 (tramp-tramp-file-p newname))
3640 (context (and preserve-selinux-context
3641 (apply 'file-selinux-context (list filename))))
3642 pr tm)
3643
3644 (when (and (not ok-if-already-exists) (file-exists-p newname))
3645 (with-parsed-tramp-file-name (if t1 filename newname) nil
3646 (tramp-error
3647 v 'file-already-exists "File %s already exists" newname)))
3648
3649 (with-parsed-tramp-file-name (if t1 filename newname) nil
3650 (with-progress-reporter
3651 v 0 (format "Transferring %s to %s" filename newname)
3652
3653 (cond
3654 ;; Both are Tramp files.
3655 ((and t1 t2)
3656 (with-parsed-tramp-file-name filename v1
3657 (with-parsed-tramp-file-name newname v2
3658 (cond
3659 ;; Shortcut: if method, host, user are the same for both
3660 ;; files, we invoke `cp' or `mv' on the remote host
3661 ;; directly.
3662 ((tramp-equal-remote filename newname)
3663 (tramp-do-copy-or-rename-file-directly
3664 op filename newname
3665 ok-if-already-exists keep-date preserve-uid-gid))
3666
3667 ;; Try out-of-band operation.
3668 ((tramp-method-out-of-band-p
3669 v1 (nth 7 (file-attributes filename)))
3670 (tramp-do-copy-or-rename-file-out-of-band
3671 op filename newname keep-date))
3672
3673 ;; No shortcut was possible. So we copy the
3674 ;; file first. If the operation was `rename', we go
3675 ;; back and delete the original file (if the copy was
3676 ;; successful). The approach is simple-minded: we
3677 ;; create a new buffer, insert the contents of the
3678 ;; source file into it, then write out the buffer to
3679 ;; the target file. The advantage is that it doesn't
3680 ;; matter which filename handlers are used for the
3681 ;; source and target file.
3682 (t
3683 (tramp-do-copy-or-rename-file-via-buffer
3684 op filename newname keep-date))))))
3685
3686 ;; One file is a Tramp file, the other one is local.
3687 ((or t1 t2)
3688 (cond
3689 ;; Fast track on local machine.
3690 ((tramp-local-host-p v)
3691 (tramp-do-copy-or-rename-file-directly
3692 op filename newname
3693 ok-if-already-exists keep-date preserve-uid-gid))
3694
3695 ;; If the Tramp file has an out-of-band method, the corresponding
3696 ;; copy-program can be invoked.
3697 ((tramp-method-out-of-band-p v (nth 7 (file-attributes filename)))
3698 (tramp-do-copy-or-rename-file-out-of-band
3699 op filename newname keep-date))
3700
3701 ;; Use the inline method via a Tramp buffer.
3702 (t (tramp-do-copy-or-rename-file-via-buffer
3703 op filename newname keep-date))))
3704
3705 (t
3706 ;; One of them must be a Tramp file.
3707 (error "Tramp implementation says this cannot happen")))
3708
3709 ;; Handle `preserve-selinux-context'.
3710 (when context (apply 'set-file-selinux-context (list newname context)))
3711
3712 ;; In case of `rename', we must flush the cache of the source file.
3713 (when (and t1 (eq op 'rename))
3714 (with-parsed-tramp-file-name filename v1
3715 (tramp-flush-file-property v1 (file-name-directory localname))
3716 (tramp-flush-file-property v1 localname)))
3717
3718 ;; When newname did exist, we have wrong cached values.
3719 (when t2
3720 (with-parsed-tramp-file-name newname v2
3721 (tramp-flush-file-property v2 (file-name-directory localname))
3722 (tramp-flush-file-property v2 localname)))))))
3723
3724 (defun tramp-do-copy-or-rename-file-via-buffer (op filename newname keep-date)
3725 "Use an Emacs buffer to copy or rename a file.
3726 First arg OP is either `copy' or `rename' and indicates the operation.
3727 FILENAME is the source file, NEWNAME the target file.
3728 KEEP-DATE is non-nil if NEWNAME should have the same timestamp as FILENAME."
3729 (with-temp-buffer
3730 ;; We must disable multibyte, because binary data shall not be
3731 ;; converted.
3732 (set-buffer-multibyte nil)
3733 (let ((coding-system-for-read 'binary)
3734 (jka-compr-inhibit t))
3735 (insert-file-contents-literally filename))
3736 ;; We don't want the target file to be compressed, so we let-bind
3737 ;; `jka-compr-inhibit' to t.
3738 (let ((coding-system-for-write 'binary)
3739 (jka-compr-inhibit t))
3740 (write-region (point-min) (point-max) newname)))
3741 ;; KEEP-DATE handling.
3742 (when keep-date (set-file-times newname (nth 5 (file-attributes filename))))
3743 ;; Set the mode.
3744 (set-file-modes newname (tramp-default-file-modes filename))
3745 ;; If the operation was `rename', delete the original file.
3746 (unless (eq op 'copy) (tramp-compat-delete-file filename 'force)))
3747
3748 (defun tramp-do-copy-or-rename-file-directly
3749 (op filename newname ok-if-already-exists keep-date preserve-uid-gid)
3750 "Invokes `cp' or `mv' on the remote system.
3751 OP must be one of `copy' or `rename', indicating `cp' or `mv',
3752 respectively. FILENAME specifies the file to copy or rename,
3753 NEWNAME is the name of the new file (for copy) or the new name of
3754 the file (for rename). Both files must reside on the same host.
3755 KEEP-DATE means to make sure that NEWNAME has the same timestamp
3756 as FILENAME. PRESERVE-UID-GID, when non-nil, instructs to keep
3757 the uid and gid from FILENAME."
3758 (let ((t1 (tramp-tramp-file-p filename))
3759 (t2 (tramp-tramp-file-p newname))
3760 (file-times (nth 5 (file-attributes filename)))
3761 (file-modes (tramp-default-file-modes filename)))
3762 (with-parsed-tramp-file-name (if t1 filename newname) nil
3763 (let* ((cmd (cond ((and (eq op 'copy) preserve-uid-gid) "cp -f -p")
3764 ((eq op 'copy) "cp -f")
3765 ((eq op 'rename) "mv -f")
3766 (t (tramp-error
3767 v 'file-error
3768 "Unknown operation `%s', must be `copy' or `rename'"
3769 op))))
3770 (localname1
3771 (if t1
3772 (tramp-file-name-handler 'file-remote-p filename 'localname)
3773 filename))
3774 (localname2
3775 (if t2
3776 (tramp-file-name-handler 'file-remote-p newname 'localname)
3777 newname))
3778 (prefix (file-remote-p (if t1 filename newname)))
3779 cmd-result)
3780
3781 (cond
3782 ;; Both files are on a remote host, with same user.
3783 ((and t1 t2)
3784 (setq cmd-result
3785 (tramp-send-command-and-check
3786 v
3787 (format "%s %s %s" cmd
3788 (tramp-shell-quote-argument localname1)
3789 (tramp-shell-quote-argument localname2))))
3790 (with-current-buffer (tramp-get-buffer v)
3791 (goto-char (point-min))
3792 (unless
3793 (or
3794 (and keep-date
3795 ;; Mask cp -f error.
3796 (re-search-forward
3797 tramp-operation-not-permitted-regexp nil t))
3798 (zerop cmd-result))
3799 (tramp-error-with-buffer
3800 nil v 'file-error
3801 "Copying directly failed, see buffer `%s' for details."
3802 (buffer-name)))))
3803
3804 ;; We are on the local host.
3805 ((or t1 t2)
3806 (cond
3807 ;; We can do it directly.
3808 ((let (file-name-handler-alist)
3809 (and (file-readable-p localname1)
3810 (file-writable-p (file-name-directory localname2))
3811 (or (file-directory-p localname2)
3812 (file-writable-p localname2))))
3813 (if (eq op 'copy)
3814 (tramp-compat-copy-file
3815 localname1 localname2 ok-if-already-exists
3816 keep-date preserve-uid-gid)
3817 (tramp-run-real-handler
3818 'rename-file (list localname1 localname2 ok-if-already-exists))))
3819
3820 ;; We can do it directly with `tramp-send-command'
3821 ((and (file-readable-p (concat prefix localname1))
3822 (file-writable-p
3823 (file-name-directory (concat prefix localname2)))
3824 (or (file-directory-p (concat prefix localname2))
3825 (file-writable-p (concat prefix localname2))))
3826 (tramp-do-copy-or-rename-file-directly
3827 op (concat prefix localname1) (concat prefix localname2)
3828 ok-if-already-exists keep-date t)
3829 ;; We must change the ownership to the local user.
3830 (tramp-set-file-uid-gid
3831 (concat prefix localname2)
3832 (tramp-get-local-uid 'integer)
3833 (tramp-get-local-gid 'integer)))
3834
3835 ;; We need a temporary file in between.
3836 (t
3837 ;; Create the temporary file.
3838 (let ((tmpfile (tramp-compat-make-temp-file localname1)))
3839 (unwind-protect
3840 (progn
3841 (cond
3842 (t1
3843 (or
3844 (zerop
3845 (tramp-send-command-and-check
3846 v (format
3847 "%s %s %s" cmd
3848 (tramp-shell-quote-argument localname1)
3849 (tramp-shell-quote-argument tmpfile))))
3850 (tramp-error-with-buffer
3851 nil v 'file-error
3852 "Copying directly failed, see buffer `%s' for details."
3853 (tramp-get-buffer v)))
3854 ;; We must change the ownership as remote user.
3855 ;; Since this does not work reliable, we also
3856 ;; give read permissions.
3857 (set-file-modes
3858 (concat prefix tmpfile) (tramp-octal-to-decimal "0777"))
3859 (tramp-set-file-uid-gid
3860 (concat prefix tmpfile)
3861 (tramp-get-local-uid 'integer)
3862 (tramp-get-local-gid 'integer)))
3863 (t2
3864 (if (eq op 'copy)
3865 (tramp-compat-copy-file
3866 localname1 tmpfile t
3867 keep-date preserve-uid-gid)
3868 (tramp-run-real-handler
3869 'rename-file
3870 (list localname1 tmpfile t)))
3871 ;; We must change the ownership as local user.
3872 ;; Since this does not work reliable, we also
3873 ;; give read permissions.
3874 (set-file-modes tmpfile (tramp-octal-to-decimal "0777"))
3875 (tramp-set-file-uid-gid
3876 tmpfile
3877 (tramp-get-remote-uid v 'integer)
3878 (tramp-get-remote-gid v 'integer))))
3879
3880 ;; Move the temporary file to its destination.
3881 (cond
3882 (t2
3883 (or
3884 (zerop
3885 (tramp-send-command-and-check
3886 v (format
3887 "cp -f -p %s %s"
3888 (tramp-shell-quote-argument tmpfile)
3889 (tramp-shell-quote-argument localname2))))
3890 (tramp-error-with-buffer
3891 nil v 'file-error
3892 "Copying directly failed, see buffer `%s' for details."
3893 (tramp-get-buffer v))))
3894 (t1
3895 (tramp-run-real-handler
3896 'rename-file
3897 (list tmpfile localname2 ok-if-already-exists)))))
3898
3899 ;; Save exit.
3900 (condition-case nil
3901 (tramp-compat-delete-file tmpfile 'force)
3902 (error)))))))))
3903
3904 ;; Set the time and mode. Mask possible errors.
3905 (condition-case nil
3906 (when keep-date
3907 (set-file-times newname file-times)
3908 (set-file-modes newname file-modes))
3909 (error)))))
3910
3911 (defun tramp-do-copy-or-rename-file-out-of-band (op filename newname keep-date)
3912 "Invoke rcp program to copy.
3913 The method used must be an out-of-band method."
3914 (let ((t1 (tramp-tramp-file-p filename))
3915 (t2 (tramp-tramp-file-p newname))
3916 copy-program copy-args copy-env copy-keep-date port spec
3917 source target)
3918
3919 (with-parsed-tramp-file-name (if t1 filename newname) nil
3920 (if (and t1 t2)
3921
3922 ;; Both are Tramp files. We shall optimize it, when the
3923 ;; methods for filename and newname are the same.
3924 (let* ((dir-flag (file-directory-p filename))
3925 (tmpfile (tramp-compat-make-temp-file localname dir-flag)))
3926 (if dir-flag
3927 (setq tmpfile
3928 (expand-file-name
3929 (file-name-nondirectory newname) tmpfile)))
3930 (unwind-protect
3931 (progn
3932 (tramp-do-copy-or-rename-file-out-of-band
3933 op filename tmpfile keep-date)
3934 (tramp-do-copy-or-rename-file-out-of-band
3935 'rename tmpfile newname keep-date))
3936 ;; Save exit.
3937 (condition-case nil
3938 (if dir-flag
3939 (tramp-compat-delete-directory
3940 (expand-file-name ".." tmpfile) 'recursive)
3941 (tramp-compat-delete-file tmpfile 'force))
3942 (error))))
3943
3944 ;; Expand hops. Might be necessary for gateway methods.
3945 (setq v (car (tramp-compute-multi-hops v)))
3946 (aset v 3 localname)
3947
3948 ;; Check which ones of source and target are Tramp files.
3949 (setq source (if t1 (tramp-make-copy-program-file-name v) filename)
3950 target (funcall
3951 (if (and (file-directory-p filename)
3952 (string-equal
3953 (file-name-nondirectory filename)
3954 (file-name-nondirectory newname)))
3955 'file-name-directory
3956 'identity)
3957 (if t2 (tramp-make-copy-program-file-name v) newname)))
3958
3959 ;; Check for port number. Until now, there's no need for handling
3960 ;; like method, user, host.
3961 (setq host (tramp-file-name-real-host v)
3962 port (tramp-file-name-port v)
3963 port (or (and port (number-to-string port)) ""))
3964
3965 ;; Compose copy command.
3966 (setq spec (format-spec-make
3967 ?h host ?u user ?p port
3968 ?t (tramp-get-connection-property
3969 (tramp-get-connection-process v) "temp-file" "")
3970 ?k (if keep-date " " ""))
3971 copy-program (tramp-get-method-parameter
3972 method 'tramp-copy-program)
3973 copy-keep-date (tramp-get-method-parameter
3974 method 'tramp-copy-keep-date)
3975 copy-args
3976 (delq
3977 nil
3978 (mapcar
3979 (lambda (x)
3980 (setq
3981 x
3982 ;; " " is indication for keep-date argument.
3983 (delete " " (mapcar (lambda (y) (format-spec y spec)) x)))
3984 (unless (member "" x) (mapconcat 'identity x " ")))
3985 (tramp-get-method-parameter method 'tramp-copy-args)))
3986 copy-env
3987 (delq
3988 nil
3989 (mapcar
3990 (lambda (x)
3991 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
3992 (unless (member "" x) (mapconcat 'identity x " ")))
3993 (tramp-get-method-parameter method 'tramp-copy-env))))
3994
3995 ;; Check for program.
3996 (when (and (fboundp 'executable-find)
3997 (not (let ((default-directory
3998 (tramp-compat-temporary-file-directory)))
3999 (executable-find copy-program))))
4000 (tramp-error
4001 v 'file-error "Cannot find copy program: %s" copy-program))
4002
4003 ;; Set variables for computing the prompt for reading
4004 ;; password.
4005 (setq tramp-current-method (tramp-file-name-method v)
4006 tramp-current-user (tramp-file-name-user v)
4007 tramp-current-host (tramp-file-name-host v))
4008
4009 (unwind-protect
4010 (with-temp-buffer
4011 ;; The default directory must be remote.
4012 (let ((default-directory
4013 (file-name-directory (if t1 filename newname)))
4014 (process-environment (copy-sequence process-environment)))
4015 ;; Set the transfer process properties.
4016 (tramp-set-connection-property
4017 v "process-name" (buffer-name (current-buffer)))
4018 (tramp-set-connection-property
4019 v "process-buffer" (current-buffer))
4020 (while copy-env
4021 (tramp-message v 5 "%s=\"%s\"" (car copy-env) (cadr copy-env))
4022 (setenv (pop copy-env) (pop copy-env)))
4023
4024 ;; Use an asynchronous process. By this, password can
4025 ;; be handled. The default directory must be local, in
4026 ;; order to apply the correct `copy-program'. We don't
4027 ;; set a timeout, because the copying of large files can
4028 ;; last longer than 60 secs.
4029 (let ((p (let ((default-directory
4030 (tramp-compat-temporary-file-directory)))
4031 (apply 'start-process
4032 (tramp-get-connection-property
4033 v "process-name" nil)
4034 (tramp-get-connection-property
4035 v "process-buffer" nil)
4036 copy-program
4037 (append copy-args (list source target))))))
4038 (tramp-message
4039 v 6 "%s" (mapconcat 'identity (process-command p) " "))
4040 (tramp-set-process-query-on-exit-flag p nil)
4041 (tramp-process-actions p v tramp-actions-copy-out-of-band))))
4042
4043 ;; Reset the transfer process properties.
4044 (tramp-set-connection-property v "process-name" nil)
4045 (tramp-set-connection-property v "process-buffer" nil))
4046
4047 ;; Handle KEEP-DATE argument.
4048 (when (and keep-date (not copy-keep-date))
4049 (set-file-times newname (nth 5 (file-attributes filename))))
4050
4051 ;; Set the mode.
4052 (unless (and keep-date copy-keep-date)
4053 (ignore-errors
4054 (set-file-modes newname (tramp-default-file-modes filename)))))
4055
4056 ;; If the operation was `rename', delete the original file.
4057 (unless (eq op 'copy)
4058 (if (file-regular-p filename)
4059 (tramp-compat-delete-file filename 'force)
4060 (tramp-compat-delete-directory filename 'recursive))))))
4061
4062 (defun tramp-handle-make-directory (dir &optional parents)
4063 "Like `make-directory' for Tramp files."
4064 (setq dir (expand-file-name dir))
4065 (with-parsed-tramp-file-name dir nil
4066 (tramp-flush-directory-property v (file-name-directory localname))
4067 (save-excursion
4068 (tramp-barf-unless-okay
4069 v
4070 (format "%s %s"
4071 (if parents "mkdir -p" "mkdir")
4072 (tramp-shell-quote-argument localname))
4073 "Couldn't make directory %s" dir))))
4074
4075 (defun tramp-handle-delete-directory (directory &optional recursive)
4076 "Like `delete-directory' for Tramp files."
4077 (setq directory (expand-file-name directory))
4078 (with-parsed-tramp-file-name directory nil
4079 (tramp-flush-file-property v (file-name-directory localname))
4080 (tramp-flush-directory-property v localname)
4081 (unless (zerop (tramp-send-command-and-check
4082 v
4083 (format
4084 "%s %s"
4085 (if recursive "rm -rf" "rmdir")
4086 (tramp-shell-quote-argument localname))))
4087 (tramp-error v 'file-error "Couldn't delete %s" directory))))
4088
4089 (defun tramp-handle-delete-file (filename &optional force)
4090 "Like `delete-file' for Tramp files."
4091 (setq filename (expand-file-name filename))
4092 (with-parsed-tramp-file-name filename nil
4093 (tramp-flush-file-property v (file-name-directory localname))
4094 (tramp-flush-file-property v localname)
4095 (unless (zerop (tramp-send-command-and-check
4096 v
4097 (format "rm -f %s"
4098 (tramp-shell-quote-argument localname))))
4099 (tramp-error v 'file-error "Couldn't delete %s" filename))))
4100
4101 ;; Dired.
4102
4103 ;; CCC: This does not seem to be enough. Something dies when
4104 ;; we try and delete two directories under Tramp :/
4105 (defun tramp-handle-dired-recursive-delete-directory (filename)
4106 "Recursively delete the directory given.
4107 This is like `dired-recursive-delete-directory' for Tramp files."
4108 (with-parsed-tramp-file-name filename nil
4109 ;; Run a shell command 'rm -r <localname>'
4110 ;; Code shamelessly stolen from the dired implementation and, um, hacked :)
4111 (unless (file-exists-p filename)
4112 (tramp-error v 'file-error "No such directory: %s" filename))
4113 ;; Which is better, -r or -R? (-r works for me <daniel@danann.net>)
4114 (tramp-send-command
4115 v
4116 (format "rm -rf %s" (tramp-shell-quote-argument localname))
4117 ;; Don't read the output, do it explicitely.
4118 nil t)
4119 ;; Wait for the remote system to return to us...
4120 ;; This might take a while, allow it plenty of time.
4121 (tramp-wait-for-output (tramp-get-connection-process v) 120)
4122 ;; Make sure that it worked...
4123 (tramp-flush-file-property v (file-name-directory localname))
4124 (tramp-flush-directory-property v localname)
4125 (and (file-exists-p filename)
4126 (tramp-error
4127 v 'file-error "Failed to recursively delete %s" filename))))
4128
4129 (defun tramp-handle-dired-compress-file (file &rest ok-flag)
4130 "Like `dired-compress-file' for Tramp files."
4131 ;; OK-FLAG is valid for XEmacs only, but not implemented.
4132 ;; Code stolen mainly from dired-aux.el.
4133 (with-parsed-tramp-file-name file nil
4134 (tramp-flush-file-property v localname)
4135 (save-excursion
4136 (let ((suffixes
4137 (if (not (featurep 'xemacs))
4138 ;; Emacs case
4139 (symbol-value 'dired-compress-file-suffixes)
4140 ;; XEmacs has `dired-compression-method-alist', which is
4141 ;; transformed into `dired-compress-file-suffixes' structure.
4142 (mapcar
4143 (lambda (x)
4144 (list (concat (regexp-quote (nth 1 x)) "\\'")
4145 nil
4146 (mapconcat 'identity (nth 3 x) " ")))
4147 (symbol-value 'dired-compression-method-alist))))
4148 suffix)
4149 ;; See if any suffix rule matches this file name.
4150 (while suffixes
4151 (let (case-fold-search)
4152 (if (string-match (car (car suffixes)) localname)
4153 (setq suffix (car suffixes) suffixes nil))
4154 (setq suffixes (cdr suffixes))))
4155
4156 (cond ((file-symlink-p file)
4157 nil)
4158 ((and suffix (nth 2 suffix))
4159 ;; We found an uncompression rule.
4160 (with-progress-reporter v 0 (format "Uncompressing %s" file)
4161 (when (zerop
4162 (tramp-send-command-and-check
4163 v (concat (nth 2 suffix) " "
4164 (tramp-shell-quote-argument localname))))
4165 ;; `dired-remove-file' is not defined in XEmacs.
4166 (tramp-compat-funcall 'dired-remove-file file)
4167 (string-match (car suffix) file)
4168 (concat (substring file 0 (match-beginning 0))))))
4169 (t
4170 ;; We don't recognize the file as compressed, so compress it.
4171 ;; Try gzip.
4172 (with-progress-reporter v 0 (format "Compressing %s" file)
4173 (when (zerop
4174 (tramp-send-command-and-check
4175 v (concat "gzip -f "
4176 (tramp-shell-quote-argument localname))))
4177 ;; `dired-remove-file' is not defined in XEmacs.
4178 (tramp-compat-funcall 'dired-remove-file file)
4179 (cond ((file-exists-p (concat file ".gz"))
4180 (concat file ".gz"))
4181 ((file-exists-p (concat file ".z"))
4182 (concat file ".z"))
4183 (t nil))))))))))
4184
4185 (defun tramp-handle-dired-uncache (dir &optional dir-p)
4186 "Like `dired-uncache' for Tramp files."
4187 ;; DIR-P is valid for XEmacs only.
4188 (with-parsed-tramp-file-name
4189 (if (or dir-p (file-directory-p dir)) dir (file-name-directory dir)) nil
4190 (tramp-flush-file-property v localname)))
4191
4192 ;; Pacify byte-compiler. The function is needed on XEmacs only. I'm
4193 ;; not sure at all that this is the right way to do it, but let's hope
4194 ;; it works for now, and wait for a guru to point out the Right Way to
4195 ;; achieve this.
4196 ;;(eval-when-compile
4197 ;; (unless (fboundp 'dired-insert-set-properties)
4198 ;; (fset 'dired-insert-set-properties 'ignore)))
4199 ;; Gerd suggests this:
4200 (eval-when-compile (require 'dired))
4201 ;; Note that dired is required at run-time, too, when it is needed.
4202 ;; It is only needed on XEmacs for the function
4203 ;; `dired-insert-set-properties'.
4204
4205 (defun tramp-handle-insert-directory
4206 (filename switches &optional wildcard full-directory-p)
4207 "Like `insert-directory' for Tramp files."
4208 (setq filename (expand-file-name filename))
4209 (with-parsed-tramp-file-name filename nil
4210 (if (and (featurep 'ls-lisp)
4211 (not (symbol-value 'ls-lisp-use-insert-directory-program)))
4212 (tramp-run-real-handler
4213 'insert-directory (list filename switches wildcard full-directory-p))
4214 (when (stringp switches)
4215 (setq switches (split-string switches)))
4216 (when (and (member "--dired" switches)
4217 (not (tramp-get-ls-command-with-dired v)))
4218 (setq switches (delete "--dired" switches)))
4219 (when wildcard
4220 (setq wildcard (tramp-run-real-handler
4221 'file-name-nondirectory (list localname)))
4222 (setq localname (tramp-run-real-handler
4223 'file-name-directory (list localname))))
4224 (unless full-directory-p
4225 (setq switches (add-to-list 'switches "-d" 'append)))
4226 (setq switches (mapconcat 'tramp-shell-quote-argument switches " "))
4227 (when wildcard
4228 (setq switches (concat switches " " wildcard)))
4229 (tramp-message
4230 v 4 "Inserting directory `ls %s %s', wildcard %s, fulldir %s"
4231 switches filename (if wildcard "yes" "no")
4232 (if full-directory-p "yes" "no"))
4233 ;; If `full-directory-p', we just say `ls -l FILENAME'.
4234 ;; Else we chdir to the parent directory, then say `ls -ld BASENAME'.
4235 (if full-directory-p
4236 (tramp-send-command
4237 v
4238 (format "%s %s %s 2>/dev/null"
4239 (tramp-get-ls-command v)
4240 switches
4241 (if wildcard
4242 localname
4243 (tramp-shell-quote-argument (concat localname ".")))))
4244 (tramp-barf-unless-okay
4245 v
4246 (format "cd %s" (tramp-shell-quote-argument
4247 (tramp-run-real-handler
4248 'file-name-directory (list localname))))
4249 "Couldn't `cd %s'"
4250 (tramp-shell-quote-argument
4251 (tramp-run-real-handler 'file-name-directory (list localname))))
4252 (tramp-send-command
4253 v
4254 (format "%s %s %s"
4255 (tramp-get-ls-command v)
4256 switches
4257 (if (or wildcard
4258 (zerop (length
4259 (tramp-run-real-handler
4260 'file-name-nondirectory (list localname)))))
4261 ""
4262 (tramp-shell-quote-argument
4263 (tramp-run-real-handler
4264 'file-name-nondirectory (list localname)))))))
4265 (let ((beg (point)))
4266 ;; We cannot use `insert-buffer-substring' because the Tramp
4267 ;; buffer changes its contents before insertion due to calling
4268 ;; `expand-file' and alike.
4269 (insert
4270 (with-current-buffer (tramp-get-buffer v)
4271 (buffer-string)))
4272
4273 ;; Check for "--dired" output.
4274 (forward-line -2)
4275 (when (looking-at "//SUBDIRED//")
4276 (forward-line -1))
4277 (when (looking-at "//DIRED//")
4278 (let ((end (tramp-compat-line-end-position))
4279 (linebeg (point)))
4280 ;; Now read the numeric positions of file names.
4281 (goto-char linebeg)
4282 (forward-word 1)
4283 (forward-char 3)
4284 (while (< (point) end)
4285 (let ((start (+ beg (read (current-buffer))))
4286 (end (+ beg (read (current-buffer)))))
4287 (if (memq (char-after end) '(?\n ?\ ))
4288 ;; End is followed by \n or by " -> ".
4289 (put-text-property start end 'dired-filename t))))))
4290 ;; Remove trailing lines.
4291 (goto-char (tramp-compat-line-beginning-position))
4292 (while (looking-at "//")
4293 (forward-line 1)
4294 (delete-region (match-beginning 0) (point)))
4295
4296 ;; The inserted file could be from somewhere else.
4297 (when (and (not wildcard) (not full-directory-p))
4298 (goto-char (point-max))
4299 (when (file-symlink-p filename)
4300 (goto-char (search-backward "->" beg 'noerror)))
4301 (search-backward
4302 (if (zerop (length (file-name-nondirectory filename)))
4303 "."
4304 (file-name-nondirectory filename))
4305 beg 'noerror)
4306 (replace-match (file-relative-name filename) t))
4307
4308 (goto-char (point-max))))))
4309
4310 (defun tramp-handle-unhandled-file-name-directory (filename)
4311 "Like `unhandled-file-name-directory' for Tramp files."
4312 ;; With Emacs 23, we could simply return `nil'. But we must keep it
4313 ;; for backward compatibility.
4314 (expand-file-name "~/"))
4315
4316 ;; Canonicalization of file names.
4317
4318 (defun tramp-handle-expand-file-name (name &optional dir)
4319 "Like `expand-file-name' for Tramp files.
4320 If the localname part of the given filename starts with \"/../\" then
4321 the result will be a local, non-Tramp, filename."
4322 ;; If DIR is not given, use DEFAULT-DIRECTORY or "/".
4323 (setq dir (or dir default-directory "/"))
4324 ;; Unless NAME is absolute, concat DIR and NAME.
4325 (unless (file-name-absolute-p name)
4326 (setq name (concat (file-name-as-directory dir) name)))
4327 ;; If NAME is not a Tramp file, run the real handler.
4328 (if (not (tramp-connectable-p name))
4329 (tramp-run-real-handler 'expand-file-name (list name nil))
4330 ;; Dissect NAME.
4331 (with-parsed-tramp-file-name name nil
4332 (unless (tramp-run-real-handler 'file-name-absolute-p (list localname))
4333 (setq localname (concat "~/" localname)))
4334 ;; Tilde expansion if necessary. This needs a shell which
4335 ;; groks tilde expansion! The function `tramp-find-shell' is
4336 ;; supposed to find such a shell on the remote host. Please
4337 ;; tell me about it when this doesn't work on your system.
4338 (when (string-match "\\`\\(~[^/]*\\)\\(.*\\)\\'" localname)
4339 (let ((uname (match-string 1 localname))
4340 (fname (match-string 2 localname)))
4341 ;; We cannot simply apply "~/", because under sudo "~/" is
4342 ;; expanded to the local user home directory but to the
4343 ;; root home directory. On the other hand, using always
4344 ;; the default user name for tilde expansion is not
4345 ;; appropriate either, because ssh and companions might
4346 ;; use a user name from the config file.
4347 (when (and (string-equal uname "~")
4348 (string-match "\\`su\\(do\\)?\\'" method))
4349 (setq uname (concat uname user)))
4350 (setq uname
4351 (with-connection-property v uname
4352 (tramp-send-command
4353 v (format "cd %s; pwd" (tramp-shell-quote-argument uname)))
4354 (with-current-buffer (tramp-get-buffer v)
4355 (goto-char (point-min))
4356 (buffer-substring
4357 (point) (tramp-compat-line-end-position)))))
4358 (setq localname (concat uname fname))))
4359 ;; There might be a double slash, for example when "~/"
4360 ;; expands to "/". Remove this.
4361 (while (string-match "//" localname)
4362 (setq localname (replace-match "/" t t localname)))
4363 ;; No tilde characters in file name, do normal
4364 ;; `expand-file-name' (this does "/./" and "/../"). We bind
4365 ;; `directory-sep-char' here for XEmacs on Windows, which would
4366 ;; otherwise use backslash. `default-directory' is bound,
4367 ;; because on Windows there would be problems with UNC shares or
4368 ;; Cygwin mounts.
4369 (let ((directory-sep-char ?/)
4370 (default-directory (tramp-compat-temporary-file-directory)))
4371 (tramp-make-tramp-file-name
4372 method user host
4373 (tramp-drop-volume-letter
4374 (tramp-run-real-handler
4375 'expand-file-name (list localname))))))))
4376
4377 (defun tramp-replace-environment-variables (filename)
4378 "Replace environment variables in FILENAME.
4379 Return the string with the replaced variables."
4380 (save-match-data
4381 (let ((idx (string-match "$\\(\\w+\\)" filename)))
4382 ;; `$' is coded as `$$'.
4383 (when (and idx
4384 (or (zerop idx) (not (eq ?$ (aref filename (1- idx)))))
4385 (getenv (match-string 1 filename)))
4386 (setq filename
4387 (replace-match
4388 (substitute-in-file-name (match-string 0 filename))
4389 t nil filename)))
4390 filename)))
4391
4392 (defun tramp-handle-substitute-in-file-name (filename)
4393 "Like `substitute-in-file-name' for Tramp files.
4394 \"//\" and \"/~\" substitute only in the local filename part.
4395 If the URL Tramp syntax is chosen, \"//\" as method delimeter and \"/~\" at
4396 beginning of local filename are not substituted."
4397 ;; First, we must replace environment variables.
4398 (setq filename (tramp-replace-environment-variables filename))
4399 (with-parsed-tramp-file-name filename nil
4400 (if (equal tramp-syntax 'url)
4401 ;; We need to check localname only. The other parts cannot contain
4402 ;; "//" or "/~".
4403 (if (and (> (length localname) 1)
4404 (or (string-match "//" localname)
4405 (string-match "/~" localname 1)))
4406 (tramp-run-real-handler 'substitute-in-file-name (list filename))
4407 (tramp-make-tramp-file-name
4408 (when method (substitute-in-file-name method))
4409 (when user (substitute-in-file-name user))
4410 (when host (substitute-in-file-name host))
4411 (when localname
4412 (tramp-run-real-handler
4413 'substitute-in-file-name (list localname)))))
4414 ;; Ignore in LOCALNAME everything before "//" or "/~".
4415 (when (and (stringp localname) (string-match ".+?/\\(/\\|~\\)" localname))
4416 (setq filename
4417 (concat (file-remote-p filename)
4418 (replace-match "\\1" nil nil localname)))
4419 ;; "/m:h:~" does not work for completion. We use "/m:h:~/".
4420 (when (string-match "~$" filename)
4421 (setq filename (concat filename "/"))))
4422 (tramp-run-real-handler 'substitute-in-file-name (list filename)))))
4423
4424 ;; In XEmacs, electricity is implemented via a key map for ?/ and ?~,
4425 ;; which calls corresponding functions (see minibuf.el).
4426 (when (fboundp 'minibuffer-electric-separator)
4427 (mapc
4428 (lambda (x)
4429 (eval
4430 `(defadvice ,x
4431 (around ,(intern (format "tramp-advice-%s" x)) activate)
4432 "Invoke `substitute-in-file-name' for Tramp files."
4433 (if (and (symbol-value 'minibuffer-electric-file-name-behavior)
4434 (tramp-tramp-file-p (buffer-substring)))
4435 ;; We don't need to handle `last-input-event', because
4436 ;; due to the key map we know it must be ?/ or ?~.
4437 (let ((s (concat (buffer-substring (point-min) (point))
4438 (string last-command-char))))
4439 (delete-region (point-min) (point))
4440 (insert (substitute-in-file-name s))
4441 (setq ad-return-value last-command-char))
4442 ad-do-it)))
4443 (eval
4444 `(add-hook
4445 'tramp-unload-hook
4446 (lambda ()
4447 (ad-remove-advice ',x 'around ',(intern (format "tramp-advice-%s" x)))
4448 (ad-activate ',x)))))
4449
4450 '(minibuffer-electric-separator
4451 minibuffer-electric-tilde)))
4452
4453
4454 ;;; Remote commands:
4455
4456 (defun tramp-handle-executable-find (command)
4457 "Like `executable-find' for Tramp files."
4458 (with-parsed-tramp-file-name default-directory nil
4459 (tramp-find-executable v command (tramp-get-remote-path v) t)))
4460
4461 ;; We use BUFFER also as connection buffer during setup. Because of
4462 ;; this, its original contents must be saved, and restored once
4463 ;; connection has been setup.
4464 (defun tramp-handle-start-file-process (name buffer program &rest args)
4465 "Like `start-file-process' for Tramp files."
4466 (with-parsed-tramp-file-name default-directory nil
4467 (unless (stringp program)
4468 (tramp-error
4469 v 'file-error "pty association is not supported for `%s'" name))
4470 (unwind-protect
4471 (let ((command (format "cd %s; exec %s"
4472 (tramp-shell-quote-argument localname)
4473 (mapconcat 'tramp-shell-quote-argument
4474 (cons program args) " ")))
4475 (name1 name)
4476 (i 0))
4477 (unless buffer
4478 ;; BUFFER can be nil. We use a temporary buffer.
4479 (setq buffer (generate-new-buffer tramp-temp-buffer-name)))
4480 (while (get-process name1)
4481 ;; NAME must be unique as process name.
4482 (setq i (1+ i)
4483 name1 (format "%s<%d>" name i)))
4484 (setq name name1)
4485 ;; Set the new process properties.
4486 (tramp-set-connection-property v "process-name" name)
4487 (tramp-set-connection-property v "process-buffer" buffer)
4488 ;; Activate narrowing in order to save BUFFER contents.
4489 ;; Clear also the modification time; otherwise we might be
4490 ;; interrupted by `verify-visited-file-modtime'.
4491 (with-current-buffer (tramp-get-connection-buffer v)
4492 (clear-visited-file-modtime)
4493 (narrow-to-region (point-max) (point-max)))
4494 ;; Send the command. `tramp-send-command' opens a new
4495 ;; connection.
4496 (tramp-send-command v command nil t) ; nooutput
4497 ;; Set query flag for this process.
4498 (tramp-set-process-query-on-exit-flag
4499 (tramp-get-connection-process v) t)
4500 ;; Return process.
4501 (tramp-get-connection-process v))
4502 ;; Save exit.
4503 (with-current-buffer (tramp-get-connection-buffer v)
4504 (if (string-match tramp-temp-buffer-name (buffer-name))
4505 (progn
4506 (set-process-buffer (tramp-get-connection-process v) nil)
4507 (kill-buffer (current-buffer)))
4508 (widen)
4509 (goto-char (point-max))))
4510 (tramp-set-connection-property v "process-name" nil)
4511 (tramp-set-connection-property v "process-buffer" nil))))
4512
4513 (defun tramp-handle-process-file
4514 (program &optional infile destination display &rest args)
4515 "Like `process-file' for Tramp files."
4516 ;; The implementation is not complete yet.
4517 (when (and (numberp destination) (zerop destination))
4518 (error "Implementation does not handle immediate return"))
4519
4520 (with-parsed-tramp-file-name default-directory nil
4521 (let (command input tmpinput stderr tmpstderr outbuf ret)
4522 ;; Compute command.
4523 (setq command (mapconcat 'tramp-shell-quote-argument
4524 (cons program args) " "))
4525 ;; Determine input.
4526 (if (null infile)
4527 (setq input "/dev/null")
4528 (setq infile (expand-file-name infile))
4529 (if (tramp-equal-remote default-directory infile)
4530 ;; INFILE is on the same remote host.
4531 (setq input (with-parsed-tramp-file-name infile nil localname))
4532 ;; INFILE must be copied to remote host.
4533 (setq input (tramp-make-tramp-temp-file v)
4534 tmpinput (tramp-make-tramp-file-name method user host input))
4535 (copy-file infile tmpinput t)))
4536 (when input (setq command (format "%s <%s" command input)))
4537
4538 ;; Determine output.
4539 (cond
4540 ;; Just a buffer.
4541 ((bufferp destination)
4542 (setq outbuf destination))
4543 ;; A buffer name.
4544 ((stringp destination)
4545 (setq outbuf (get-buffer-create destination)))
4546 ;; (REAL-DESTINATION ERROR-DESTINATION)
4547 ((consp destination)
4548 ;; output.
4549 (cond
4550 ((bufferp (car destination))
4551 (setq outbuf (car destination)))
4552 ((stringp (car destination))
4553 (setq outbuf (get-buffer-create (car destination))))
4554 ((car destination)
4555 (setq outbuf (current-buffer))))
4556 ;; stderr.
4557 (cond
4558 ((stringp (cadr destination))
4559 (setcar (cdr destination) (expand-file-name (cadr destination)))
4560 (if (tramp-equal-remote default-directory (cadr destination))
4561 ;; stderr is on the same remote host.
4562 (setq stderr (with-parsed-tramp-file-name
4563 (cadr destination) nil localname))
4564 ;; stderr must be copied to remote host. The temporary
4565 ;; file must be deleted after execution.
4566 (setq stderr (tramp-make-tramp-temp-file v)
4567 tmpstderr (tramp-make-tramp-file-name
4568 method user host stderr))))
4569 ;; stderr to be discarded.
4570 ((null (cadr destination))
4571 (setq stderr "/dev/null"))))
4572 ;; 't
4573 (destination
4574 (setq outbuf (current-buffer))))
4575 (when stderr (setq command (format "%s 2>%s" command stderr)))
4576
4577 ;; Send the command. It might not return in time, so we protect it.
4578 (condition-case nil
4579 (unwind-protect
4580 (setq ret
4581 (tramp-send-command-and-check
4582 v (format "\\cd %s; %s"
4583 (tramp-shell-quote-argument localname)
4584 command)
4585 nil t))
4586 ;; We should show the output anyway.
4587 (when outbuf
4588 (with-current-buffer outbuf
4589 (insert
4590 (with-current-buffer (tramp-get-connection-buffer v)
4591 (buffer-string))))
4592 (when display (display-buffer outbuf))))
4593 ;; When the user did interrupt, we should do it also. We use
4594 ;; return code -1 as marker.
4595 (quit
4596 (kill-buffer (tramp-get-connection-buffer v))
4597 (setq ret -1))
4598 ;; Handle errors.
4599 (error
4600 (kill-buffer (tramp-get-connection-buffer v))
4601 (setq ret 1)))
4602
4603 ;; Provide error file.
4604 (when tmpstderr (rename-file tmpstderr (cadr destination) t))
4605
4606 ;; Cleanup. We remove all file cache values for the connection,
4607 ;; because the remote process could have changed them.
4608 (when tmpinput (tramp-compat-delete-file tmpinput 'force))
4609
4610 ;; `process-file-side-effects' has been introduced with GNU
4611 ;; Emacs 23.2. If set to `nil', no remote file will be changed
4612 ;; by `program'. If it doesn't exist, we assume its default
4613 ;; value 't'.
4614 (unless (and (boundp 'process-file-side-effects)
4615 (not (symbol-value 'process-file-side-effects)))
4616 (tramp-flush-directory-property v ""))
4617
4618 ;; Return exit status.
4619 (if (equal ret -1)
4620 (keyboard-quit)
4621 ret))))
4622
4623 (defun tramp-local-call-process
4624 (program &optional infile destination display &rest args)
4625 "Calls `call-process' on the local host.
4626 This is needed because for some Emacs flavors Tramp has
4627 defadviced `call-process' to behave like `process-file'. The
4628 Lisp error raised when PROGRAM is nil is trapped also, returning 1."
4629 (let ((default-directory
4630 (if (file-remote-p default-directory)
4631 (tramp-compat-temporary-file-directory)
4632 default-directory)))
4633 (if (executable-find program)
4634 (apply 'call-process program infile destination display args)
4635 1)))
4636
4637 (defun tramp-handle-call-process-region
4638 (start end program &optional delete buffer display &rest args)
4639 "Like `call-process-region' for Tramp files."
4640 (let ((tmpfile (tramp-compat-make-temp-file "")))
4641 (write-region start end tmpfile)
4642 (when delete (delete-region start end))
4643 (unwind-protect
4644 (apply 'call-process program tmpfile buffer display args)
4645 (tramp-compat-delete-file tmpfile 'force))))
4646
4647 (defun tramp-handle-shell-command
4648 (command &optional output-buffer error-buffer)
4649 "Like `shell-command' for Tramp files."
4650 (let* ((asynchronous (string-match "[ \t]*&[ \t]*\\'" command))
4651 ;; We cannot use `shell-file-name' and `shell-command-switch',
4652 ;; they are variables of the local host.
4653 (args (list "/bin/sh" "-c" (substring command 0 asynchronous)))
4654 current-buffer-p
4655 (output-buffer
4656 (cond
4657 ((bufferp output-buffer) output-buffer)
4658 ((stringp output-buffer) (get-buffer-create output-buffer))
4659 (output-buffer
4660 (setq current-buffer-p t)
4661 (current-buffer))
4662 (t (get-buffer-create
4663 (if asynchronous
4664 "*Async Shell Command*"
4665 "*Shell Command Output*")))))
4666 (error-buffer
4667 (cond
4668 ((bufferp error-buffer) error-buffer)
4669 ((stringp error-buffer) (get-buffer-create error-buffer))))
4670 (buffer
4671 (if (and (not asynchronous) error-buffer)
4672 (with-parsed-tramp-file-name default-directory nil
4673 (list output-buffer (tramp-make-tramp-temp-file v)))
4674 output-buffer))
4675 (p (get-buffer-process output-buffer)))
4676
4677 ;; Check whether there is another process running. Tramp does not
4678 ;; support 2 (asynchronous) processes in parallel.
4679 (when p
4680 (if (yes-or-no-p "A command is running. Kill it? ")
4681 (condition-case nil
4682 (kill-process p)
4683 (error nil))
4684 (error "Shell command in progress")))
4685
4686 (if current-buffer-p
4687 (progn
4688 (barf-if-buffer-read-only)
4689 (push-mark nil t))
4690 (with-current-buffer output-buffer
4691 (setq buffer-read-only nil)
4692 (erase-buffer)))
4693
4694 (if (and (not current-buffer-p) (integerp asynchronous))
4695 (prog1
4696 ;; Run the process.
4697 (apply 'start-file-process "*Async Shell*" buffer args)
4698 ;; Display output.
4699 (pop-to-buffer output-buffer)
4700 (setq mode-line-process '(":%s"))
4701 (require 'shell) (shell-mode))
4702
4703 (prog1
4704 ;; Run the process.
4705 (apply 'process-file (car args) nil buffer nil (cdr args))
4706 ;; Insert error messages if they were separated.
4707 (when (listp buffer)
4708 (with-current-buffer error-buffer
4709 (insert-file-contents (cadr buffer)))
4710 (tramp-compat-delete-file (cadr buffer) 'force))
4711 (if current-buffer-p
4712 ;; This is like exchange-point-and-mark, but doesn't
4713 ;; activate the mark. It is cleaner to avoid activation,
4714 ;; even though the command loop would deactivate the mark
4715 ;; because we inserted text.
4716 (goto-char (prog1 (mark t)
4717 (set-marker (mark-marker) (point)
4718 (current-buffer))))
4719 ;; There's some output, display it.
4720 (when (with-current-buffer output-buffer (> (point-max) (point-min)))
4721 (if (functionp 'display-message-or-buffer)
4722 (tramp-compat-funcall 'display-message-or-buffer output-buffer)
4723 (pop-to-buffer output-buffer))))))))
4724
4725 ;; File Editing.
4726
4727 (defvar tramp-handle-file-local-copy-hook nil
4728 "Normal hook to be run at the end of `tramp-handle-file-local-copy'.")
4729
4730 (defun tramp-handle-file-local-copy (filename)
4731 "Like `file-local-copy' for Tramp files."
4732
4733 (with-parsed-tramp-file-name filename nil
4734 (unless (file-exists-p filename)
4735 (tramp-error
4736 v 'file-error
4737 "Cannot make local copy of non-existing file `%s'" filename))
4738
4739 (let* ((size (nth 7 (file-attributes filename)))
4740 (rem-enc (tramp-get-inline-coding v "remote-encoding" size))
4741 (loc-dec (tramp-get-inline-coding v "local-decoding" size))
4742 (tmpfile (tramp-compat-make-temp-file filename)))
4743
4744 (condition-case err
4745 (cond
4746 ;; `copy-file' handles direct copy and out-of-band methods.
4747 ((or (tramp-local-host-p v)
4748 (tramp-method-out-of-band-p v size))
4749 (copy-file filename tmpfile t t))
4750
4751 ;; Use inline encoding for file transfer.
4752 (rem-enc
4753 (save-excursion
4754 (with-progress-reporter
4755 v 5 (format "Encoding remote file %s" filename)
4756 (tramp-barf-unless-okay
4757 v (format rem-enc (tramp-shell-quote-argument localname))
4758 "Encoding remote file failed"))
4759
4760 (if (functionp loc-dec)
4761 ;; If local decoding is a function, we call it. We
4762 ;; must disable multibyte, because
4763 ;; `uudecode-decode-region' doesn't handle it
4764 ;; correctly.
4765 (with-temp-buffer
4766 (set-buffer-multibyte nil)
4767 (insert-buffer-substring (tramp-get-buffer v))
4768 (with-progress-reporter
4769 v 3 (format "Decoding remote file %s with function %s"
4770 filename loc-dec)
4771 (funcall loc-dec (point-min) (point-max))
4772 ;; Unset `file-name-handler-alist'. Otherwise,
4773 ;; epa-file gets confused.
4774 (let (file-name-handler-alist
4775 (coding-system-for-write 'binary))
4776 (write-region (point-min) (point-max) tmpfile))))
4777
4778 ;; If tramp-decoding-function is not defined for this
4779 ;; method, we invoke tramp-decoding-command instead.
4780 (let ((tmpfile2 (tramp-compat-make-temp-file filename)))
4781 ;; Unset `file-name-handler-alist'. Otherwise,
4782 ;; epa-file gets confused.
4783 (let (file-name-handler-alist
4784 (coding-system-for-write 'binary))
4785 (write-region (point-min) (point-max) tmpfile2))
4786 (with-progress-reporter
4787 v 3 (format "Decoding remote file %s with command %s"
4788 filename loc-dec)
4789 (unwind-protect
4790 (tramp-call-local-coding-command
4791 loc-dec tmpfile2 tmpfile)
4792 (tramp-compat-delete-file tmpfile2 'force)))))
4793
4794 ;; Set proper permissions.
4795 (set-file-modes tmpfile (tramp-default-file-modes filename))
4796 ;; Set local user ownership.
4797 (tramp-set-file-uid-gid tmpfile)))
4798
4799 ;; Oops, I don't know what to do.
4800 (t (tramp-error
4801 v 'file-error "Wrong method specification for `%s'" method)))
4802
4803 ;; Error handling.
4804 ((error quit)
4805 (tramp-compat-delete-file tmpfile 'force)
4806 (signal (car err) (cdr err))))
4807
4808 (run-hooks 'tramp-handle-file-local-copy-hook)
4809 tmpfile)))
4810
4811 (defun tramp-handle-file-remote-p (filename &optional identification connected)
4812 "Like `file-remote-p' for Tramp files."
4813 (let ((tramp-verbose 3))
4814 (when (tramp-tramp-file-p filename)
4815 (let* ((v (tramp-dissect-file-name filename))
4816 (p (tramp-get-connection-process v))
4817 (c (and p (processp p) (memq (process-status p) '(run open)))))
4818 ;; We expand the file name only, if there is already a connection.
4819 (with-parsed-tramp-file-name
4820 (if c (expand-file-name filename) filename) nil
4821 (and (or (not connected) c)
4822 (cond
4823 ((eq identification 'method) method)
4824 ((eq identification 'user) user)
4825 ((eq identification 'host) host)
4826 ((eq identification 'localname) localname)
4827 (t (tramp-make-tramp-file-name method user host "")))))))))
4828
4829 (defun tramp-find-file-name-coding-system-alist (filename tmpname)
4830 "Like `find-operation-coding-system' for Tramp filenames.
4831 Tramp's `insert-file-contents' and `write-region' work over
4832 temporary file names. If `file-coding-system-alist' contains an
4833 expression, which matches more than the file name suffix, the
4834 coding system might not be determined. This function repairs it."
4835 (let (result)
4836 (dolist (elt file-coding-system-alist result)
4837 (when (and (consp elt) (string-match (car elt) filename))
4838 ;; We found a matching entry in `file-coding-system-alist'.
4839 ;; So we add a similar entry, but with the temporary file name
4840 ;; as regexp.
4841 (add-to-list
4842 'result (cons (regexp-quote tmpname) (cdr elt)) 'append)))))
4843
4844 (defun tramp-handle-insert-file-contents
4845 (filename &optional visit beg end replace)
4846 "Like `insert-file-contents' for Tramp files."
4847 (barf-if-buffer-read-only)
4848 (setq filename (expand-file-name filename))
4849 (let (result local-copy remote-copy)
4850 (with-parsed-tramp-file-name filename nil
4851 (unwind-protect
4852 (if (not (file-exists-p filename))
4853 ;; We don't raise a Tramp error, because it might be
4854 ;; suppressed, like in `find-file-noselect-1'.
4855 (signal 'file-error
4856 (list "File not found on remote host" filename))
4857
4858 (if (and (tramp-local-host-p v)
4859 (let (file-name-handler-alist)
4860 (file-readable-p localname)))
4861 ;; Short track: if we are on the local host, we can
4862 ;; run directly.
4863 (setq result
4864 (tramp-run-real-handler
4865 'insert-file-contents
4866 (list localname visit beg end replace)))
4867
4868 ;; When we shall insert only a part of the file, we copy
4869 ;; this part.
4870 (when (or beg end)
4871 (setq remote-copy (tramp-make-tramp-temp-file v))
4872 (tramp-send-command
4873 v
4874 (cond
4875 ((and beg end)
4876 (format "tail -c +%d %s | head -c +%d >%s"
4877 (1+ beg) (tramp-shell-quote-argument localname)
4878 (- end beg) remote-copy))
4879 (beg
4880 (format "tail -c +%d %s >%s"
4881 (1+ beg) (tramp-shell-quote-argument localname)
4882 remote-copy))
4883 (end
4884 (format "head -c +%d %s >%s"
4885 (1+ end) (tramp-shell-quote-argument localname)
4886 remote-copy)))))
4887
4888 ;; `insert-file-contents-literally' takes care to avoid
4889 ;; calling jka-compr. By let-binding
4890 ;; `inhibit-file-name-operation', we propagate that care
4891 ;; to the `file-local-copy' operation.
4892 (setq local-copy
4893 (let ((inhibit-file-name-operation
4894 (when (eq inhibit-file-name-operation
4895 'insert-file-contents)
4896 'file-local-copy)))
4897 (cond
4898 ((stringp remote-copy)
4899 (file-local-copy
4900 (tramp-make-tramp-file-name
4901 method user host remote-copy)))
4902 ((stringp tramp-temp-buffer-file-name)
4903 (copy-file filename tramp-temp-buffer-file-name 'ok)
4904 tramp-temp-buffer-file-name)
4905 (t (file-local-copy filename)))))
4906
4907 ;; When the file is not readable for the owner, it
4908 ;; cannot be inserted, even it is redable for the group
4909 ;; or for everybody.
4910 (set-file-modes local-copy (tramp-octal-to-decimal "0600"))
4911
4912 (when (and (null remote-copy)
4913 (tramp-get-method-parameter
4914 method 'tramp-copy-keep-tmpfile))
4915 ;; We keep the local file for performance reasons,
4916 ;; useful for "rsync".
4917 (setq tramp-temp-buffer-file-name local-copy)
4918 (put 'tramp-temp-buffer-file-name 'permanent-local t))
4919
4920 (with-progress-reporter
4921 v 3 (format "Inserting local temp file `%s'" local-copy)
4922 ;; We must ensure that `file-coding-system-alist'
4923 ;; matches `local-copy'.
4924 (let ((file-coding-system-alist
4925 (tramp-find-file-name-coding-system-alist
4926 filename local-copy)))
4927 (setq result
4928 (insert-file-contents
4929 local-copy nil nil nil replace))))))
4930
4931 ;; Save exit.
4932 (progn
4933 (when visit
4934 (setq buffer-file-name filename)
4935 (setq buffer-read-only (not (file-writable-p filename)))
4936 (set-visited-file-modtime)
4937 (set-buffer-modified-p nil))
4938 (when (and (stringp local-copy)
4939 (or remote-copy (null tramp-temp-buffer-file-name)))
4940 (tramp-compat-delete-file local-copy 'force))
4941 (when (stringp remote-copy)
4942 (tramp-compat-delete-file
4943 (tramp-make-tramp-file-name method user host remote-copy)
4944 'force)))))
4945
4946 ;; Result.
4947 (list (expand-file-name filename)
4948 (cadr result))))
4949
4950 ;; This is needed for XEmacs only. Code stolen from files.el.
4951 (defun tramp-handle-insert-file-contents-literally
4952 (filename &optional visit beg end replace)
4953 "Like `insert-file-contents-literally' for Tramp files."
4954 (let ((format-alist nil)
4955 (after-insert-file-functions nil)
4956 (coding-system-for-read 'no-conversion)
4957 (coding-system-for-write 'no-conversion)
4958 (find-buffer-file-type-function
4959 (if (fboundp 'find-buffer-file-type)
4960 (symbol-function 'find-buffer-file-type)
4961 nil))
4962 (inhibit-file-name-handlers '(jka-compr-handler image-file-handler))
4963 (inhibit-file-name-operation 'insert-file-contents))
4964 (unwind-protect
4965 (progn
4966 (fset 'find-buffer-file-type (lambda (filename) t))
4967 (insert-file-contents filename visit beg end replace))
4968 ;; Save exit.
4969 (if find-buffer-file-type-function
4970 (fset 'find-buffer-file-type find-buffer-file-type-function)
4971 (fmakunbound 'find-buffer-file-type)))))
4972
4973 (defun tramp-handle-find-backup-file-name (filename)
4974 "Like `find-backup-file-name' for Tramp files."
4975 (with-parsed-tramp-file-name filename nil
4976 ;; We set both variables. It doesn't matter whether it is
4977 ;; Emacs or XEmacs.
4978 (let ((backup-directory-alist
4979 ;; Emacs case.
4980 (when (boundp 'backup-directory-alist)
4981 (if (symbol-value 'tramp-backup-directory-alist)
4982 (mapcar
4983 (lambda (x)
4984 (cons
4985 (car x)
4986 (if (and (stringp (cdr x))
4987 (file-name-absolute-p (cdr x))
4988 (not (tramp-file-name-p (cdr x))))
4989 (tramp-make-tramp-file-name method user host (cdr x))
4990 (cdr x))))
4991 (symbol-value 'tramp-backup-directory-alist))
4992 (symbol-value 'backup-directory-alist))))
4993
4994 (bkup-backup-directory-info
4995 ;; XEmacs case.
4996 (when (boundp 'bkup-backup-directory-info)
4997 (if (symbol-value 'tramp-bkup-backup-directory-info)
4998 (mapcar
4999 (lambda (x)
5000 (nconc
5001 (list (car x))
5002 (list
5003 (if (and (stringp (car (cdr x)))
5004 (file-name-absolute-p (car (cdr x)))
5005 (not (tramp-file-name-p (car (cdr x)))))
5006 (tramp-make-tramp-file-name
5007 method user host (car (cdr x)))
5008 (car (cdr x))))
5009 (cdr (cdr x))))
5010 (symbol-value 'tramp-bkup-backup-directory-info))
5011 (symbol-value 'bkup-backup-directory-info)))))
5012
5013 (tramp-run-real-handler 'find-backup-file-name (list filename)))))
5014
5015 (defun tramp-handle-make-auto-save-file-name ()
5016 "Like `make-auto-save-file-name' for Tramp files.
5017 Returns a file name in `tramp-auto-save-directory' for autosaving this file."
5018 (let ((tramp-auto-save-directory tramp-auto-save-directory)
5019 (buffer-file-name
5020 (tramp-subst-strs-in-string
5021 '(("_" . "|")
5022 ("/" . "_a")
5023 (":" . "_b")
5024 ("|" . "__")
5025 ("[" . "_l")
5026 ("]" . "_r"))
5027 (buffer-file-name))))
5028 ;; File name must be unique. This is ensured with Emacs 22 (see
5029 ;; UNIQUIFY element of `auto-save-file-name-transforms'); but for
5030 ;; all other cases we must do it ourselves.
5031 (when (boundp 'auto-save-file-name-transforms)
5032 (mapc
5033 (lambda (x)
5034 (when (and (string-match (car x) buffer-file-name)
5035 (not (car (cddr x))))
5036 (setq tramp-auto-save-directory
5037 (or tramp-auto-save-directory
5038 (tramp-compat-temporary-file-directory)))))
5039 (symbol-value 'auto-save-file-name-transforms)))
5040 ;; Create directory.
5041 (when tramp-auto-save-directory
5042 (setq buffer-file-name
5043 (expand-file-name buffer-file-name tramp-auto-save-directory))
5044 (unless (file-exists-p tramp-auto-save-directory)
5045 (make-directory tramp-auto-save-directory t)))
5046 ;; Run plain `make-auto-save-file-name'. There might be an advice when
5047 ;; it is not a magic file name operation (since Emacs 22).
5048 ;; We must deactivate it temporarily.
5049 (if (not (ad-is-active 'make-auto-save-file-name))
5050 (tramp-run-real-handler 'make-auto-save-file-name nil)
5051 ;; else
5052 (ad-deactivate 'make-auto-save-file-name)
5053 (prog1
5054 (tramp-run-real-handler 'make-auto-save-file-name nil)
5055 (ad-activate 'make-auto-save-file-name)))))
5056
5057 (defvar tramp-handle-write-region-hook nil
5058 "Normal hook to be run at the end of `tramp-handle-write-region'.")
5059
5060 ;; CCC grok LOCKNAME
5061 (defun tramp-handle-write-region
5062 (start end filename &optional append visit lockname confirm)
5063 "Like `write-region' for Tramp files."
5064 (setq filename (expand-file-name filename))
5065 (with-parsed-tramp-file-name filename nil
5066 ;; Following part commented out because we don't know what to do about
5067 ;; file locking, and it does not appear to be a problem to ignore it.
5068 ;; Ange-ftp ignores it, too.
5069 ;; (when (and lockname (stringp lockname))
5070 ;; (setq lockname (expand-file-name lockname)))
5071 ;; (unless (or (eq lockname nil)
5072 ;; (string= lockname filename))
5073 ;; (error
5074 ;; "tramp-handle-write-region: LOCKNAME must be nil or equal FILENAME"))
5075
5076 ;; XEmacs takes a coding system as the seventh argument, not `confirm'.
5077 (when (and (not (featurep 'xemacs)) confirm (file-exists-p filename))
5078 (unless (y-or-n-p (format "File %s exists; overwrite anyway? " filename))
5079 (tramp-error v 'file-error "File not overwritten")))
5080
5081 (let ((uid (or (nth 2 (tramp-compat-file-attributes filename 'integer))
5082 (tramp-get-remote-uid v 'integer)))
5083 (gid (or (nth 3 (tramp-compat-file-attributes filename 'integer))
5084 (tramp-get-remote-gid v 'integer))))
5085
5086 (if (and (tramp-local-host-p v)
5087 ;; `file-writable-p' calls `file-expand-file-name'. We
5088 ;; cannot use `tramp-run-real-handler' therefore.
5089 (let (file-name-handler-alist)
5090 (and
5091 (file-writable-p (file-name-directory localname))
5092 (or (file-directory-p localname)
5093 (file-writable-p localname)))))
5094 ;; Short track: if we are on the local host, we can run directly.
5095 (tramp-run-real-handler
5096 'write-region
5097 (list start end localname append 'no-message lockname confirm))
5098
5099 (let ((modes (save-excursion (tramp-default-file-modes filename)))
5100 ;; We use this to save the value of
5101 ;; `last-coding-system-used' after writing the tmp
5102 ;; file. At the end of the function, we set
5103 ;; `last-coding-system-used' to this saved value. This
5104 ;; way, any intermediary coding systems used while
5105 ;; talking to the remote shell or suchlike won't hose
5106 ;; this variable. This approach was snarfed from
5107 ;; ange-ftp.el.
5108 coding-system-used
5109 ;; Write region into a tmp file. This isn't really
5110 ;; needed if we use an encoding function, but currently
5111 ;; we use it always because this makes the logic
5112 ;; simpler.
5113 (tmpfile (or tramp-temp-buffer-file-name
5114 (tramp-compat-make-temp-file filename))))
5115
5116 ;; If `append' is non-nil, we copy the file locally, and let
5117 ;; the native `write-region' implementation do the job.
5118 (when append (copy-file filename tmpfile 'ok))
5119
5120 ;; We say `no-message' here because we don't want the
5121 ;; visited file modtime data to be clobbered from the temp
5122 ;; file. We call `set-visited-file-modtime' ourselves later
5123 ;; on. We must ensure that `file-coding-system-alist'
5124 ;; matches `tmpfile'.
5125 (let (file-name-handler-alist
5126 (file-coding-system-alist
5127 (tramp-find-file-name-coding-system-alist filename tmpfile)))
5128 (condition-case err
5129 (tramp-run-real-handler
5130 'write-region
5131 (list start end tmpfile append 'no-message lockname confirm))
5132 ((error quit)
5133 (setq tramp-temp-buffer-file-name nil)
5134 (tramp-compat-delete-file tmpfile 'force)
5135 (signal (car err) (cdr err))))
5136
5137 ;; Now, `last-coding-system-used' has the right value. Remember it.
5138 (when (boundp 'last-coding-system-used)
5139 (setq coding-system-used
5140 (symbol-value 'last-coding-system-used))))
5141
5142 ;; The permissions of the temporary file should be set. If
5143 ;; filename does not exist (eq modes nil) it has been
5144 ;; renamed to the backup file. This case `save-buffer'
5145 ;; handles permissions.
5146 ;; Ensure, that it is still readable.
5147 (when modes
5148 (set-file-modes
5149 tmpfile (logior (or modes 0) (tramp-octal-to-decimal "0400"))))
5150
5151 ;; This is a bit lengthy due to the different methods
5152 ;; possible for file transfer. First, we check whether the
5153 ;; method uses an rcp program. If so, we call it.
5154 ;; Otherwise, both encoding and decoding command must be
5155 ;; specified. However, if the method _also_ specifies an
5156 ;; encoding function, then that is used for encoding the
5157 ;; contents of the tmp file.
5158 (let* ((size (nth 7 (file-attributes tmpfile)))
5159 (rem-dec (tramp-get-inline-coding v "remote-decoding" size))
5160 (loc-enc (tramp-get-inline-coding v "local-encoding" size)))
5161 (cond
5162 ;; `copy-file' handles direct copy and out-of-band methods.
5163 ((or (tramp-local-host-p v)
5164 (tramp-method-out-of-band-p v size))
5165 (if (and (not (stringp start))
5166 (= (or end (point-max)) (point-max))
5167 (= (or start (point-min)) (point-min))
5168 (tramp-get-method-parameter
5169 method 'tramp-copy-keep-tmpfile))
5170 (progn
5171 (setq tramp-temp-buffer-file-name tmpfile)
5172 (condition-case err
5173 ;; We keep the local file for performance
5174 ;; reasons, useful for "rsync".
5175 (copy-file tmpfile filename t)
5176 ((error quit)
5177 (setq tramp-temp-buffer-file-name nil)
5178 (tramp-compat-delete-file tmpfile 'force)
5179 (signal (car err) (cdr err)))))
5180 (setq tramp-temp-buffer-file-name nil)
5181 ;; Don't rename, in order to keep context in SELinux.
5182 (unwind-protect
5183 (copy-file tmpfile filename t)
5184 (tramp-compat-delete-file tmpfile 'force))))
5185
5186 ;; Use inline file transfer.
5187 (rem-dec
5188 ;; Encode tmpfile.
5189 (unwind-protect
5190 (with-temp-buffer
5191 (set-buffer-multibyte nil)
5192 ;; Use encoding function or command.
5193 (if (functionp loc-enc)
5194 (with-progress-reporter
5195 v 3 (format "Encoding region using function `%s'"
5196 loc-enc)
5197 (let ((coding-system-for-read 'binary))
5198 (insert-file-contents-literally tmpfile))
5199 ;; The following `let' is a workaround for the
5200 ;; base64.el that comes with pgnus-0.84. If
5201 ;; both of the following conditions are
5202 ;; satisfied, it tries to write to a local
5203 ;; file in default-directory, but at this
5204 ;; point, default-directory is remote.
5205 ;; (`call-process-region' can't write to
5206 ;; remote files, it seems.) The file in
5207 ;; question is a tmp file anyway.
5208 (let ((default-directory
5209 (tramp-compat-temporary-file-directory)))
5210 (funcall loc-enc (point-min) (point-max))))
5211
5212 (with-progress-reporter
5213 v 3 (format "Encoding region using command `%s'"
5214 loc-enc)
5215 (unless (zerop (tramp-call-local-coding-command
5216 loc-enc tmpfile t))
5217 (tramp-error
5218 v 'file-error
5219 (concat "Cannot write to `%s', "
5220 "local encoding command `%s' failed")
5221 filename loc-enc))))
5222
5223 ;; Send buffer into remote decoding command which
5224 ;; writes to remote file. Because this happens on
5225 ;; the remote host, we cannot use the function.
5226 (with-progress-reporter
5227 v 3
5228 (format "Decoding region into remote file %s" filename)
5229 (goto-char (point-max))
5230 (unless (bolp) (newline))
5231 (tramp-send-command
5232 v
5233 (format
5234 (concat rem-dec " <<'EOF'\n%sEOF")
5235 (tramp-shell-quote-argument localname)
5236 (buffer-string)))
5237 (tramp-barf-unless-okay
5238 v nil
5239 "Couldn't write region to `%s', decode using `%s' failed"
5240 filename rem-dec)
5241 ;; When `file-precious-flag' is set, the region is
5242 ;; written to a temporary file. Check that the
5243 ;; checksum is equal to that from the local tmpfile.
5244 (when file-precious-flag
5245 (erase-buffer)
5246 (and
5247 ;; cksum runs locally, if possible.
5248 (zerop (tramp-local-call-process "cksum" tmpfile t))
5249 ;; cksum runs remotely.
5250 (zerop
5251 (tramp-send-command-and-check
5252 v
5253 (format
5254 "cksum <%s"
5255 (tramp-shell-quote-argument localname))))
5256 ;; ... they are different.
5257 (not
5258 (string-equal
5259 (buffer-string)
5260 (with-current-buffer (tramp-get-buffer v)
5261 (buffer-string))))
5262 (tramp-error
5263 v 'file-error
5264 (concat "Couldn't write region to `%s',"
5265 " decode using `%s' failed")
5266 filename rem-dec)))))
5267
5268 ;; Save exit.
5269 (tramp-compat-delete-file tmpfile 'force)))
5270
5271 ;; That's not expected.
5272 (t
5273 (tramp-error
5274 v 'file-error
5275 (concat "Method `%s' should specify both encoding and "
5276 "decoding command or an rcp program")
5277 method))))
5278
5279 ;; Make `last-coding-system-used' have the right value.
5280 (when coding-system-used
5281 (set 'last-coding-system-used coding-system-used))))
5282
5283 (tramp-flush-file-property v (file-name-directory localname))
5284 (tramp-flush-file-property v localname)
5285
5286 ;; We must protect `last-coding-system-used', now we have set it
5287 ;; to its correct value.
5288 (let (last-coding-system-used (need-chown t))
5289 ;; Set file modification time.
5290 (when (or (eq visit t) (stringp visit))
5291 (let ((file-attr (file-attributes filename)))
5292 (set-visited-file-modtime
5293 ;; We must pass modtime explicitely, because filename can
5294 ;; be different from (buffer-file-name), f.e. if
5295 ;; `file-precious-flag' is set.
5296 (nth 5 file-attr))
5297 (when (and (eq (nth 2 file-attr) uid)
5298 (eq (nth 3 file-attr) gid))
5299 (setq need-chown nil))))
5300
5301 ;; Set the ownership.
5302 (when need-chown
5303 (tramp-set-file-uid-gid filename uid gid))
5304 (when (or (eq visit t) (null visit) (stringp visit))
5305 (tramp-message v 0 "Wrote %s" filename))
5306 (run-hooks 'tramp-handle-write-region-hook)))))
5307
5308 (defvar tramp-vc-registered-file-names nil
5309 "List used to collect file names, which are checked during `vc-registered'.")
5310
5311 ;; VC backends check for the existence of various different special
5312 ;; files. This is very time consuming, because every single check
5313 ;; requires a remote command (the file cache must be invalidated).
5314 ;; Therefore, we apply a kind of optimization. We install the file
5315 ;; name handler `tramp-vc-file-name-handler', which does nothing but
5316 ;; remembers all file names for which `file-exists-p' or
5317 ;; `file-readable-p' has been applied. A first run of `vc-registered'
5318 ;; is performed. Afterwards, a script is applied for all collected
5319 ;; file names, using just one remote command. The result of this
5320 ;; script is used to fill the file cache with actual values. Now we
5321 ;; can reset the file name handlers, and we make a second run of
5322 ;; `vc-registered', which returns the expected result without sending
5323 ;; any other remote command.
5324 (defun tramp-handle-vc-registered (file)
5325 "Like `vc-registered' for Tramp files."
5326 (with-parsed-tramp-file-name file nil
5327
5328 ;; There could be new files, created by the vc backend. We cannot
5329 ;; reuse the old cache entries, therefore.
5330 (let (tramp-vc-registered-file-names
5331 (tramp-cache-inhibit-cache (current-time))
5332 (file-name-handler-alist
5333 `((,tramp-file-name-regexp . tramp-vc-file-name-handler))))
5334
5335 ;; Here we collect only file names, which need an operation.
5336 (tramp-run-real-handler 'vc-registered (list file))
5337 (tramp-message v 10 "\n%s" tramp-vc-registered-file-names)
5338
5339 ;; Send just one command, in order to fill the cache.
5340 (when tramp-vc-registered-file-names
5341 (tramp-maybe-send-script
5342 v
5343 (format tramp-vc-registered-read-file-names
5344 (tramp-get-file-exists-command v)
5345 (format "%s -r" (tramp-get-test-command v)))
5346 "tramp_vc_registered_read_file_names")
5347
5348 (dolist
5349 (elt
5350 (tramp-send-command-and-read
5351 v
5352 (format
5353 "tramp_vc_registered_read_file_names %s"
5354 (mapconcat 'tramp-shell-quote-argument
5355 tramp-vc-registered-file-names
5356 " "))))
5357
5358 (tramp-set-file-property v (car elt) (cadr elt) (cadr (cdr elt))))))
5359
5360 ;; Second run. Now all `file-exists-p' or `file-readable-p' calls
5361 ;; shall be answered from the file cache.
5362 ;; We unset `process-file-side-effects' in order to keep the cache
5363 ;; when `process-file' calls appear.
5364 (let (process-file-side-effects)
5365 (tramp-run-real-handler 'vc-registered (list file)))))
5366
5367 ;;;###autoload
5368 (progn (defun tramp-run-real-handler (operation args)
5369 "Invoke normal file name handler for OPERATION.
5370 First arg specifies the OPERATION, second arg is a list of arguments to
5371 pass to the OPERATION."
5372 (let* ((inhibit-file-name-handlers
5373 `(tramp-file-name-handler
5374 tramp-vc-file-name-handler
5375 tramp-completion-file-name-handler
5376 cygwin-mount-name-hook-function
5377 cygwin-mount-map-drive-hook-function
5378 .
5379 ,(and (eq inhibit-file-name-operation operation)
5380 inhibit-file-name-handlers)))
5381 (inhibit-file-name-operation operation))
5382 (apply operation args))))
5383
5384 ;;;###autoload
5385 (progn (defun tramp-completion-run-real-handler (operation args)
5386 "Invoke `tramp-file-name-handler' for OPERATION.
5387 First arg specifies the OPERATION, second arg is a list of arguments to
5388 pass to the OPERATION."
5389 (let* ((inhibit-file-name-handlers
5390 `(tramp-completion-file-name-handler
5391 cygwin-mount-name-hook-function
5392 cygwin-mount-map-drive-hook-function
5393 .
5394 ,(and (eq inhibit-file-name-operation operation)
5395 inhibit-file-name-handlers)))
5396 (inhibit-file-name-operation operation))
5397 (apply operation args))))
5398
5399 ;; We handle here all file primitives. Most of them have the file
5400 ;; name as first parameter; nevertheless we check for them explicitly
5401 ;; in order to be signaled if a new primitive appears. This
5402 ;; scenario is needed because there isn't a way to decide by
5403 ;; syntactical means whether a foreign method must be called. It would
5404 ;; ease the life if `file-name-handler-alist' would support a decision
5405 ;; function as well but regexp only.
5406 (defun tramp-file-name-for-operation (operation &rest args)
5407 "Return file name related to OPERATION file primitive.
5408 ARGS are the arguments OPERATION has been called with."
5409 (cond
5410 ;; FILE resp DIRECTORY.
5411 ((member operation
5412 (list 'access-file 'byte-compiler-base-file-name 'delete-directory
5413 'delete-file 'diff-latest-backup-file 'directory-file-name
5414 'directory-files 'directory-files-and-attributes
5415 'dired-compress-file 'dired-uncache
5416 'file-accessible-directory-p 'file-attributes
5417 'file-directory-p 'file-executable-p 'file-exists-p
5418 'file-local-copy 'file-remote-p 'file-modes
5419 'file-name-as-directory 'file-name-directory
5420 'file-name-nondirectory 'file-name-sans-versions
5421 'file-ownership-preserved-p 'file-readable-p
5422 'file-regular-p 'file-symlink-p 'file-truename
5423 'file-writable-p 'find-backup-file-name 'find-file-noselect
5424 'get-file-buffer 'insert-directory 'insert-file-contents
5425 'load 'make-directory 'make-directory-internal
5426 'set-file-modes 'substitute-in-file-name
5427 'unhandled-file-name-directory 'vc-registered
5428 ;; Emacs 22+ only.
5429 'set-file-times
5430 ;; Emacs 24+ only.
5431 'file-selinux-context 'set-file-selinux-context
5432 ;; XEmacs only.
5433 'abbreviate-file-name 'create-file-buffer
5434 'dired-file-modtime 'dired-make-compressed-filename
5435 'dired-recursive-delete-directory 'dired-set-file-modtime
5436 'dired-shell-unhandle-file-name 'dired-uucode-file
5437 'insert-file-contents-literally 'make-temp-name 'recover-file
5438 'vm-imap-check-mail 'vm-pop-check-mail 'vm-spool-check-mail))
5439 (if (file-name-absolute-p (nth 0 args))
5440 (nth 0 args)
5441 (expand-file-name (nth 0 args))))
5442 ;; FILE DIRECTORY resp FILE1 FILE2.
5443 ((member operation
5444 (list 'add-name-to-file 'copy-file 'expand-file-name
5445 'file-name-all-completions 'file-name-completion
5446 'file-newer-than-file-p 'make-symbolic-link 'rename-file
5447 ;; Emacs 23+ only.
5448 'copy-directory
5449 ;; XEmacs only.
5450 'dired-make-relative-symlink
5451 'vm-imap-move-mail 'vm-pop-move-mail 'vm-spool-move-mail))
5452 (save-match-data
5453 (cond
5454 ((string-match tramp-file-name-regexp (nth 0 args)) (nth 0 args))
5455 ((string-match tramp-file-name-regexp (nth 1 args)) (nth 1 args))
5456 (t (buffer-file-name (current-buffer))))))
5457 ;; START END FILE.
5458 ((eq operation 'write-region)
5459 (nth 2 args))
5460 ;; BUFFER.
5461 ((member operation
5462 (list 'set-visited-file-modtime 'verify-visited-file-modtime
5463 ;; Emacs 22+ only.
5464 'make-auto-save-file-name
5465 ;; XEmacs only.
5466 'backup-buffer))
5467 (buffer-file-name
5468 (if (bufferp (nth 0 args)) (nth 0 args) (current-buffer))))
5469 ;; COMMAND.
5470 ((member operation
5471 (list ;; not in Emacs 23+.
5472 'dired-call-process
5473 ;; Emacs only.
5474 'shell-command
5475 ;; Emacs 22+ only.
5476 'process-file
5477 ;; Emacs 23+ only.
5478 'start-file-process
5479 ;; XEmacs only.
5480 'dired-print-file 'dired-shell-call-process
5481 ;; nowhere yet.
5482 'executable-find 'start-process 'call-process))
5483 default-directory)
5484 ;; Unknown file primitive.
5485 (t (error "unknown file I/O primitive: %s" operation))))
5486
5487 (defun tramp-find-foreign-file-name-handler (filename)
5488 "Return foreign file name handler if exists."
5489 (when (tramp-tramp-file-p filename)
5490 (let ((v (tramp-dissect-file-name filename t))
5491 (handler tramp-foreign-file-name-handler-alist)
5492 elt res)
5493 ;; When we are not fully sure that filename completion is safe,
5494 ;; we should not return a handler.
5495 (when (or (tramp-file-name-method v) (tramp-file-name-user v)
5496 (and (tramp-file-name-host v)
5497 (not (member (tramp-file-name-host v)
5498 (mapcar 'car tramp-methods))))
5499 (not (tramp-completion-mode-p)))
5500 (while handler
5501 (setq elt (car handler)
5502 handler (cdr handler))
5503 (when (funcall (car elt) filename)
5504 (setq handler nil
5505 res (cdr elt))))
5506 res))))
5507
5508 ;; Main function.
5509 ;;;###autoload
5510 (defun tramp-file-name-handler (operation &rest args)
5511 "Invoke Tramp file name handler.
5512 Falls back to normal file name handler if no Tramp file name handler exists."
5513 (if tramp-mode
5514 (save-match-data
5515 (let* ((filename
5516 (tramp-replace-environment-variables
5517 (apply 'tramp-file-name-for-operation operation args)))
5518 (completion (tramp-completion-mode-p))
5519 (foreign (tramp-find-foreign-file-name-handler filename)))
5520 (with-parsed-tramp-file-name filename nil
5521 ;; Call the backend function.
5522 (if foreign
5523 (condition-case err
5524 (apply foreign operation args)
5525 (error
5526 (cond
5527 ;; When we are in completion mode, some failed
5528 ;; operations shall return at least a default
5529 ;; value in order to give the user a chance to
5530 ;; correct the file name in the minibuffer.
5531 ((and completion (zerop (length localname))
5532 (memq operation '(file-exists-p file-directory-p)))
5533 t)
5534 ((and completion (zerop (length localname))
5535 (memq operation
5536 '(expand-file-name file-name-as-directory)))
5537 filename)
5538 ;; Propagate the error.
5539 (t (signal (car err) (cdr err))))))
5540 ;; Nothing to do for us.
5541 (tramp-run-real-handler operation args)))))
5542
5543 ;; When `tramp-mode' is not enabled, we don't do anything.
5544 (tramp-run-real-handler operation args)))
5545
5546 ;; In Emacs, there is some concurrency due to timers. If a timer
5547 ;; interrupts Tramp and wishes to use the same connection buffer as
5548 ;; the "main" Emacs, then garbage might occur in the connection
5549 ;; buffer. Therefore, we need to make sure that a timer does not use
5550 ;; the same connection buffer as the "main" Emacs. We implement a
5551 ;; cheap global lock, instead of locking each connection buffer
5552 ;; separately. The global lock is based on two variables,
5553 ;; `tramp-locked' and `tramp-locker'. `tramp-locked' is set to true
5554 ;; (with setq) to indicate a lock. But Tramp also calls itself during
5555 ;; processing of a single file operation, so we need to allow
5556 ;; recursive calls. That's where the `tramp-locker' variable comes in
5557 ;; -- it is let-bound to t during the execution of the current
5558 ;; handler. So if `tramp-locked' is t and `tramp-locker' is also t,
5559 ;; then we should just proceed because we have been called
5560 ;; recursively. But if `tramp-locker' is nil, then we are a timer
5561 ;; interrupting the "main" Emacs, and then we signal an error.
5562
5563 (defvar tramp-locked nil
5564 "If non-nil, then Tramp is currently busy.
5565 Together with `tramp-locker', this implements a locking mechanism
5566 preventing reentrant calls of Tramp.")
5567
5568 (defvar tramp-locker nil
5569 "If non-nil, then a caller has locked Tramp.
5570 Together with `tramp-locked', this implements a locking mechanism
5571 preventing reentrant calls of Tramp.")
5572
5573 (defun tramp-sh-file-name-handler (operation &rest args)
5574 "Invoke remote-shell Tramp file name handler.
5575 Fall back to normal file name handler if no Tramp handler exists."
5576 (when (and tramp-locked (not tramp-locker))
5577 (setq tramp-locked nil)
5578 (signal 'file-error (list "Forbidden reentrant call of Tramp")))
5579 (let ((tl tramp-locked))
5580 (unwind-protect
5581 (progn
5582 (setq tramp-locked t)
5583 (let ((tramp-locker t))
5584 (save-match-data
5585 (let ((fn (assoc operation tramp-file-name-handler-alist)))
5586 (if fn
5587 (apply (cdr fn) args)
5588 (tramp-run-real-handler operation args))))))
5589 (setq tramp-locked tl))))
5590
5591 (defun tramp-vc-file-name-handler (operation &rest args)
5592 "Invoke special file name handler, which collects files to be handled."
5593 (save-match-data
5594 (let ((filename
5595 (tramp-replace-environment-variables
5596 (apply 'tramp-file-name-for-operation operation args)))
5597 (fn (assoc operation tramp-file-name-handler-alist)))
5598 (with-parsed-tramp-file-name filename nil
5599 (cond
5600 ;; That's what we want: file names, for which checks are
5601 ;; applied. We assume, that VC uses only `file-exists-p' and
5602 ;; `file-readable-p' checks; otherwise we must extend the
5603 ;; list. We do not perform any action, but return nil, in
5604 ;; order to keep `vc-registered' running.
5605 ((and fn (memq operation '(file-exists-p file-readable-p)))
5606 (add-to-list 'tramp-vc-registered-file-names localname 'append)
5607 nil)
5608 ;; Tramp file name handlers like `expand-file-name'. They
5609 ;; must still work.
5610 (fn
5611 (save-match-data (apply (cdr fn) args)))
5612 ;; Default file name handlers, we don't care.
5613 (t (tramp-run-real-handler operation args)))))))
5614
5615 ;;;###autoload
5616 (progn (defun tramp-completion-file-name-handler (operation &rest args)
5617 "Invoke Tramp file name completion handler.
5618 Falls back to normal file name handler if no Tramp file name handler exists."
5619 ;; We bind `directory-sep-char' here for XEmacs on Windows, which
5620 ;; would otherwise use backslash.
5621 (let ((directory-sep-char ?/)
5622 (fn (assoc operation tramp-completion-file-name-handler-alist)))
5623 (if (and
5624 ;; When `tramp-mode' is not enabled, we don't do anything.
5625 fn tramp-mode
5626 ;; For other syntaxes than `sep', the regexp matches many common
5627 ;; situations where the user doesn't actually want to use Tramp.
5628 ;; So to avoid autoloading Tramp after typing just "/s", we
5629 ;; disable this part of the completion, unless the user implicitly
5630 ;; indicated his interest in using a fancier completion system.
5631 (or (eq tramp-syntax 'sep)
5632 (featurep 'tramp) ;; If it's loaded, we may as well use it.
5633 ;; `partial-completion-mode' does not exist in XEmacs.
5634 ;; It is obsoleted with Emacs 24.1.
5635 (and (boundp 'partial-completion-mode)
5636 (symbol-value 'partial-completion-mode))
5637 ;; FIXME: These may have been loaded even if the user never
5638 ;; intended to use them.
5639 (featurep 'ido)
5640 (featurep 'icicles)))
5641 (save-match-data (apply (cdr fn) args))
5642 (tramp-completion-run-real-handler operation args)))))
5643
5644 ;;;###autoload
5645 (progn (defun tramp-register-file-name-handlers ()
5646 "Add Tramp file name handlers to `file-name-handler-alist'."
5647 ;; Remove autoloaded handlers from file name handler alist. Useful,
5648 ;; if `tramp-syntax' has been changed.
5649 (let ((a1 (rassq 'tramp-file-name-handler file-name-handler-alist)))
5650 (setq file-name-handler-alist (delq a1 file-name-handler-alist)))
5651 (let ((a1 (rassq
5652 'tramp-completion-file-name-handler file-name-handler-alist)))
5653 (setq file-name-handler-alist (delq a1 file-name-handler-alist)))
5654 ;; Add the handlers.
5655 (add-to-list 'file-name-handler-alist
5656 (cons tramp-file-name-regexp 'tramp-file-name-handler))
5657 (put 'tramp-file-name-handler 'safe-magic t)
5658 (add-to-list 'file-name-handler-alist
5659 (cons tramp-completion-file-name-regexp
5660 'tramp-completion-file-name-handler))
5661 (put 'tramp-completion-file-name-handler 'safe-magic t)
5662 ;; If jka-compr or epa-file are already loaded, move them to the
5663 ;; front of `file-name-handler-alist'.
5664 (dolist (fnh '(epa-file-handler jka-compr-handler))
5665 (let ((entry (rassoc fnh file-name-handler-alist)))
5666 (when entry
5667 (setq file-name-handler-alist
5668 (cons entry (delete entry file-name-handler-alist))))))))
5669
5670 ;; `tramp-file-name-handler' must be registered before evaluation of
5671 ;; site-start and init files, because there might exist remote files
5672 ;; already, f.e. files kept via recentf-mode.
5673 ;;;###autoload(tramp-register-file-name-handlers)
5674 (tramp-register-file-name-handlers)
5675
5676 ;;;###autoload
5677 (defun tramp-unload-file-name-handlers ()
5678 (setq file-name-handler-alist
5679 (delete (rassoc 'tramp-file-name-handler
5680 file-name-handler-alist)
5681 (delete (rassoc 'tramp-completion-file-name-handler
5682 file-name-handler-alist)
5683 file-name-handler-alist))))
5684
5685 (add-hook 'tramp-unload-hook 'tramp-unload-file-name-handlers)
5686
5687 ;;; File name handler functions for completion mode:
5688
5689 (defvar tramp-completion-mode nil
5690 "If non-nil, external packages signal that they are in file name completion.
5691
5692 This is necessary, because Tramp uses a heuristic depending on last
5693 input event. This fails when external packages use other characters
5694 but <TAB>, <SPACE> or ?\\? for file name completion. This variable
5695 should never be set globally, the intention is to let-bind it.")
5696
5697 ;; Necessary because `tramp-file-name-regexp-unified' and
5698 ;; `tramp-completion-file-name-regexp-unified' aren't different. If
5699 ;; nil, `tramp-completion-run-real-handler' is called (i.e. forwarding
5700 ;; to `tramp-file-name-handler'). Otherwise, it takes
5701 ;; `tramp-run-real-handler'. Using `last-input-event' is a little bit
5702 ;; risky, because completing a file might require loading other files,
5703 ;; like "~/.netrc", and for them it shouldn't be decided based on that
5704 ;; variable. On the other hand, those files shouldn't have partial
5705 ;; Tramp file name syntax. Maybe another variable should be introduced
5706 ;; overwriting this check in such cases. Or we change Tramp file name
5707 ;; syntax in order to avoid ambiguities, like in XEmacs ...
5708 (defun tramp-completion-mode-p ()
5709 "Check, whether method / user name / host name completion is active."
5710 (or
5711 ;; Signal from outside. `non-essential' has been introduced in Emacs 24.
5712 (and (boundp 'non-essential) (symbol-value 'non-essential))
5713 tramp-completion-mode
5714 ;; Emacs.
5715 (equal last-input-event 'tab)
5716 (and (natnump last-input-event)
5717 (or
5718 ;; ?\t has event-modifier 'control.
5719 (equal last-input-event ?\t)
5720 (and (not (event-modifiers last-input-event))
5721 (or (equal last-input-event ?\?)
5722 (equal last-input-event ?\ )))))
5723 ;; XEmacs.
5724 (and (featurep 'xemacs)
5725 ;; `last-input-event' might be nil.
5726 (not (null last-input-event))
5727 ;; `last-input-event' may have no character approximation.
5728 (tramp-compat-funcall 'event-to-character last-input-event)
5729 (or
5730 ;; ?\t has event-modifier 'control.
5731 (equal
5732 (tramp-compat-funcall 'event-to-character last-input-event) ?\t)
5733 (and (not (event-modifiers last-input-event))
5734 (or (equal
5735 (tramp-compat-funcall 'event-to-character last-input-event)
5736 ?\?)
5737 (equal
5738 (tramp-compat-funcall 'event-to-character last-input-event)
5739 ?\ )))))))
5740
5741 (defun tramp-connectable-p (filename)
5742 "Check, whether it is possible to connect the remote host w/o side-effects.
5743 This is true, if either the remote host is already connected, or if we are
5744 not in completion mode."
5745 (and (tramp-tramp-file-p filename)
5746 (with-parsed-tramp-file-name filename nil
5747 (or (get-buffer (tramp-buffer-name v))
5748 (not (tramp-completion-mode-p))))))
5749
5750 ;; Method, host name and user name completion.
5751 ;; `tramp-completion-dissect-file-name' returns a list of
5752 ;; tramp-file-name structures. For all of them we return possible completions.
5753 ;;;###autoload
5754 (defun tramp-completion-handle-file-name-all-completions (filename directory)
5755 "Like `file-name-all-completions' for partial Tramp files."
5756
5757 (let* ((fullname (tramp-drop-volume-letter
5758 (expand-file-name filename directory)))
5759 ;; Possible completion structures.
5760 (v (tramp-completion-dissect-file-name fullname))
5761 result result1)
5762
5763 (while v
5764 (let* ((car (car v))
5765 (method (tramp-file-name-method car))
5766 (user (tramp-file-name-user car))
5767 (host (tramp-file-name-host car))
5768 (localname (tramp-file-name-localname car))
5769 (m (tramp-find-method method user host))
5770 (tramp-current-user user) ; see `tramp-parse-passwd'
5771 all-user-hosts)
5772
5773 (unless localname ;; Nothing to complete.
5774
5775 (if (or user host)
5776
5777 ;; Method dependent user / host combinations.
5778 (progn
5779 (mapc
5780 (lambda (x)
5781 (setq all-user-hosts
5782 (append all-user-hosts
5783 (funcall (nth 0 x) (nth 1 x)))))
5784 (tramp-get-completion-function m))
5785
5786 (setq result
5787 (append result
5788 (mapcar
5789 (lambda (x)
5790 (tramp-get-completion-user-host
5791 method user host (nth 0 x) (nth 1 x)))
5792 (delq nil all-user-hosts)))))
5793
5794 ;; Possible methods.
5795 (setq result
5796 (append result (tramp-get-completion-methods m)))))
5797
5798 (setq v (cdr v))))
5799
5800 ;; Unify list, remove nil elements.
5801 (while result
5802 (let ((car (car result)))
5803 (when car
5804 (add-to-list
5805 'result1
5806 (substring car (length (tramp-drop-volume-letter directory)))))
5807 (setq result (cdr result))))
5808
5809 ;; Complete local parts.
5810 (append
5811 result1
5812 (condition-case nil
5813 (apply (if (tramp-connectable-p fullname)
5814 'tramp-completion-run-real-handler
5815 'tramp-run-real-handler)
5816 'file-name-all-completions (list (list filename directory)))
5817 (error nil)))))
5818
5819 ;; Method, host name and user name completion for a file.
5820 ;;;###autoload
5821 (defun tramp-completion-handle-file-name-completion
5822 (filename directory &optional predicate)
5823 "Like `file-name-completion' for Tramp files."
5824 (try-completion
5825 filename
5826 (mapcar 'list (file-name-all-completions filename directory))
5827 (when (and predicate
5828 (tramp-connectable-p (expand-file-name filename directory)))
5829 (lambda (x) (funcall predicate (expand-file-name (car x) directory))))))
5830
5831 ;; I misuse a little bit the tramp-file-name structure in order to handle
5832 ;; completion possibilities for partial methods / user names / host names.
5833 ;; Return value is a list of tramp-file-name structures according to possible
5834 ;; completions. If "localname" is non-nil it means there
5835 ;; shouldn't be a completion anymore.
5836
5837 ;; Expected results:
5838
5839 ;; "/x" "/[x" "/x@" "/[x@" "/x@y" "/[x@y"
5840 ;; [nil nil "x" nil] [nil "x" nil nil] [nil "x" "y" nil]
5841 ;; [nil "x" nil nil]
5842 ;; ["x" nil nil nil]
5843
5844 ;; "/x:" "/x:y" "/x:y:"
5845 ;; [nil nil "x" ""] [nil nil "x" "y"] ["x" nil "y" ""]
5846 ;; "/[x/" "/[x/y"
5847 ;; ["x" nil "" nil] ["x" nil "y" nil]
5848 ;; ["x" "" nil nil] ["x" "y" nil nil]
5849
5850 ;; "/x:y@" "/x:y@z" "/x:y@z:"
5851 ;; [nil nil "x" "y@"] [nil nil "x" "y@z"] ["x" "y" "z" ""]
5852 ;; "/[x/y@" "/[x/y@z"
5853 ;; ["x" nil "y" nil] ["x" "y" "z" nil]
5854 (defun tramp-completion-dissect-file-name (name)
5855 "Returns a list of `tramp-file-name' structures.
5856 They are collected by `tramp-completion-dissect-file-name1'."
5857
5858 (let* ((result)
5859 (x-nil "\\|\\(\\)")
5860 (tramp-completion-ipv6-regexp
5861 (format
5862 "[^%s]*"
5863 (if (zerop (length tramp-postfix-ipv6-format))
5864 tramp-postfix-host-format
5865 tramp-postfix-ipv6-format)))
5866 ;; "/method" "/[method"
5867 (tramp-completion-file-name-structure1
5868 (list (concat tramp-prefix-regexp "\\(" tramp-method-regexp x-nil "\\)$")
5869 1 nil nil nil))
5870 ;; "/user" "/[user"
5871 (tramp-completion-file-name-structure2
5872 (list (concat tramp-prefix-regexp "\\(" tramp-user-regexp x-nil "\\)$")
5873 nil 1 nil nil))
5874 ;; "/host" "/[host"
5875 (tramp-completion-file-name-structure3
5876 (list (concat tramp-prefix-regexp "\\(" tramp-host-regexp x-nil "\\)$")
5877 nil nil 1 nil))
5878 ;; "/[ipv6" "/[ipv6"
5879 (tramp-completion-file-name-structure4
5880 (list (concat tramp-prefix-regexp
5881 tramp-prefix-ipv6-regexp
5882 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5883 nil nil 1 nil))
5884 ;; "/user@host" "/[user@host"
5885 (tramp-completion-file-name-structure5
5886 (list (concat tramp-prefix-regexp
5887 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5888 "\\(" tramp-host-regexp x-nil "\\)$")
5889 nil 1 2 nil))
5890 ;; "/user@[ipv6" "/[user@ipv6"
5891 (tramp-completion-file-name-structure6
5892 (list (concat tramp-prefix-regexp
5893 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5894 tramp-prefix-ipv6-regexp
5895 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5896 nil 1 2 nil))
5897 ;; "/method:user" "/[method/user" "/method://user"
5898 (tramp-completion-file-name-structure7
5899 (list (concat tramp-prefix-regexp
5900 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5901 "\\(" tramp-user-regexp x-nil "\\)$")
5902 1 2 nil nil))
5903 ;; "/method:host" "/[method/host" "/method://host"
5904 (tramp-completion-file-name-structure8
5905 (list (concat tramp-prefix-regexp
5906 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5907 "\\(" tramp-host-regexp x-nil "\\)$")
5908 1 nil 2 nil))
5909 ;; "/method:[ipv6" "/[method/ipv6" "/method://[ipv6"
5910 (tramp-completion-file-name-structure9
5911 (list (concat tramp-prefix-regexp
5912 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5913 tramp-prefix-ipv6-regexp
5914 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5915 1 nil 2 nil))
5916 ;; "/method:user@host" "/[method/user@host" "/method://user@host"
5917 (tramp-completion-file-name-structure10
5918 (list (concat tramp-prefix-regexp
5919 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5920 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5921 "\\(" tramp-host-regexp x-nil "\\)$")
5922 1 2 3 nil))
5923 ;; "/method:user@[ipv6" "/[method/user@ipv6" "/method://user@[ipv6"
5924 (tramp-completion-file-name-structure11
5925 (list (concat tramp-prefix-regexp
5926 "\\(" tramp-method-regexp "\\)" tramp-postfix-method-regexp
5927 "\\(" tramp-user-regexp "\\)" tramp-postfix-user-regexp
5928 tramp-prefix-ipv6-regexp
5929 "\\(" tramp-completion-ipv6-regexp x-nil "\\)$")
5930 1 2 3 nil))
5931 ;; "/method: "/method:/"
5932 (tramp-completion-file-name-structure12
5933 (list
5934 (if (equal tramp-syntax 'url)
5935 (concat tramp-prefix-regexp
5936 "\\(" tramp-method-regexp "\\)"
5937 "\\(" (substring tramp-postfix-method-regexp 0 1)
5938 "\\|" (substring tramp-postfix-method-regexp 1 2) "\\)"
5939 "\\(" "\\)$")
5940 ;; Should not match if not URL syntax.
5941 (concat tramp-prefix-regexp "/$"))
5942 1 3 nil nil))
5943 ;; "/method: "/method:/"
5944 (tramp-completion-file-name-structure13
5945 (list
5946 (if (equal tramp-syntax 'url)
5947 (concat tramp-prefix-regexp
5948 "\\(" tramp-method-regexp "\\)"
5949 "\\(" (substring tramp-postfix-method-regexp 0 1)
5950 "\\|" (substring tramp-postfix-method-regexp 1 2) "\\)"
5951 "\\(" "\\)$")
5952 ;; Should not match if not URL syntax.
5953 (concat tramp-prefix-regexp "/$"))
5954 1 nil 3 nil)))
5955
5956 (mapc (lambda (regexp)
5957 (add-to-list 'result
5958 (tramp-completion-dissect-file-name1 regexp name)))
5959 (list
5960 tramp-completion-file-name-structure1
5961 tramp-completion-file-name-structure2
5962 tramp-completion-file-name-structure3
5963 tramp-completion-file-name-structure4
5964 tramp-completion-file-name-structure5
5965 tramp-completion-file-name-structure6
5966 tramp-completion-file-name-structure7
5967 tramp-completion-file-name-structure8
5968 tramp-completion-file-name-structure9
5969 tramp-completion-file-name-structure10
5970 tramp-completion-file-name-structure11
5971 tramp-completion-file-name-structure12
5972 tramp-completion-file-name-structure13
5973 tramp-file-name-structure))
5974
5975 (delq nil result)))
5976
5977 (defun tramp-completion-dissect-file-name1 (structure name)
5978 "Returns a `tramp-file-name' structure matching STRUCTURE.
5979 The structure consists of remote method, remote user,
5980 remote host and localname (filename on remote host)."
5981
5982 (save-match-data
5983 (when (string-match (nth 0 structure) name)
5984 (let ((method (and (nth 1 structure)
5985 (match-string (nth 1 structure) name)))
5986 (user (and (nth 2 structure)
5987 (match-string (nth 2 structure) name)))
5988 (host (and (nth 3 structure)
5989 (match-string (nth 3 structure) name)))
5990 (localname (and (nth 4 structure)
5991 (match-string (nth 4 structure) name))))
5992 (vector method user host localname)))))
5993
5994 ;; This function returns all possible method completions, adding the
5995 ;; trailing method delimeter.
5996 (defun tramp-get-completion-methods (partial-method)
5997 "Returns all method completions for PARTIAL-METHOD."
5998 (mapcar
5999 (lambda (method)
6000 (and method
6001 (string-match (concat "^" (regexp-quote partial-method)) method)
6002 (tramp-completion-make-tramp-file-name method nil nil nil)))
6003 (mapcar 'car tramp-methods)))
6004
6005 ;; Compares partial user and host names with possible completions.
6006 (defun tramp-get-completion-user-host (method partial-user partial-host user host)
6007 "Returns the most expanded string for user and host name completion.
6008 PARTIAL-USER must match USER, PARTIAL-HOST must match HOST."
6009 (cond
6010
6011 ((and partial-user partial-host)
6012 (if (and host
6013 (string-match (concat "^" (regexp-quote partial-host)) host)
6014 (string-equal partial-user (or user partial-user)))
6015 (setq user partial-user)
6016 (setq user nil
6017 host nil)))
6018
6019 (partial-user
6020 (setq host nil)
6021 (unless
6022 (and user (string-match (concat "^" (regexp-quote partial-user)) user))
6023 (setq user nil)))
6024
6025 (partial-host
6026 (setq user nil)
6027 (unless
6028 (and host (string-match (concat "^" (regexp-quote partial-host)) host))
6029 (setq host nil)))
6030
6031 (t (setq user nil
6032 host nil)))
6033
6034 (unless (zerop (+ (length user) (length host)))
6035 (tramp-completion-make-tramp-file-name method user host nil)))
6036
6037 (defun tramp-parse-rhosts (filename)
6038 "Return a list of (user host) tuples allowed to access.
6039 Either user or host may be nil."
6040 ;; On Windows, there are problems in completion when
6041 ;; `default-directory' is remote.
6042 (let ((default-directory (tramp-compat-temporary-file-directory))
6043 res)
6044 (when (file-readable-p filename)
6045 (with-temp-buffer
6046 (insert-file-contents filename)
6047 (goto-char (point-min))
6048 (while (not (eobp))
6049 (push (tramp-parse-rhosts-group) res))))
6050 res))
6051
6052 (defun tramp-parse-rhosts-group ()
6053 "Return a (user host) tuple allowed to access.
6054 Either user or host may be nil."
6055 (let ((result)
6056 (regexp
6057 (concat
6058 "^\\(" tramp-host-regexp "\\)"
6059 "\\([ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
6060 (narrow-to-region (point) (tramp-compat-line-end-position))
6061 (when (re-search-forward regexp nil t)
6062 (setq result (append (list (match-string 3) (match-string 1)))))
6063 (widen)
6064 (forward-line 1)
6065 result))
6066
6067 (defun tramp-parse-shosts (filename)
6068 "Return a list of (user host) tuples allowed to access.
6069 User is always nil."
6070 ;; On Windows, there are problems in completion when
6071 ;; `default-directory' is remote.
6072 (let ((default-directory (tramp-compat-temporary-file-directory))
6073 res)
6074 (when (file-readable-p filename)
6075 (with-temp-buffer
6076 (insert-file-contents filename)
6077 (goto-char (point-min))
6078 (while (not (eobp))
6079 (push (tramp-parse-shosts-group) res))))
6080 res))
6081
6082 (defun tramp-parse-shosts-group ()
6083 "Return a (user host) tuple allowed to access.
6084 User is always nil."
6085 (let ((result)
6086 (regexp (concat "^\\(" tramp-host-regexp "\\)")))
6087 (narrow-to-region (point) (tramp-compat-line-end-position))
6088 (when (re-search-forward regexp nil t)
6089 (setq result (list nil (match-string 1))))
6090 (widen)
6091 (or
6092 (> (skip-chars-forward ",") 0)
6093 (forward-line 1))
6094 result))
6095
6096 (defun tramp-parse-sconfig (filename)
6097 "Return a list of (user host) tuples allowed to access.
6098 User is always nil."
6099 ;; On Windows, there are problems in completion when
6100 ;; `default-directory' is remote.
6101 (let ((default-directory (tramp-compat-temporary-file-directory))
6102 res)
6103 (when (file-readable-p filename)
6104 (with-temp-buffer
6105 (insert-file-contents filename)
6106 (goto-char (point-min))
6107 (while (not (eobp))
6108 (push (tramp-parse-sconfig-group) res))))
6109 res))
6110
6111 (defun tramp-parse-sconfig-group ()
6112 "Return a (user host) tuple allowed to access.
6113 User is always nil."
6114 (let ((result)
6115 (regexp (concat "^[ \t]*Host[ \t]+" "\\(" tramp-host-regexp "\\)")))
6116 (narrow-to-region (point) (tramp-compat-line-end-position))
6117 (when (re-search-forward regexp nil t)
6118 (setq result (list nil (match-string 1))))
6119 (widen)
6120 (or
6121 (> (skip-chars-forward ",") 0)
6122 (forward-line 1))
6123 result))
6124
6125 (defun tramp-parse-shostkeys (dirname)
6126 "Return a list of (user host) tuples allowed to access.
6127 User is always nil."
6128 ;; On Windows, there are problems in completion when
6129 ;; `default-directory' is remote.
6130 (let* ((default-directory (tramp-compat-temporary-file-directory))
6131 (regexp (concat "^key_[0-9]+_\\(" tramp-host-regexp "\\)\\.pub$"))
6132 (files (when (file-directory-p dirname) (directory-files dirname)))
6133 result)
6134 (while files
6135 (when (string-match regexp (car files))
6136 (push (list nil (match-string 1 (car files))) result))
6137 (setq files (cdr files)))
6138 result))
6139
6140 (defun tramp-parse-sknownhosts (dirname)
6141 "Return a list of (user host) tuples allowed to access.
6142 User is always nil."
6143 ;; On Windows, there are problems in completion when
6144 ;; `default-directory' is remote.
6145 (let* ((default-directory (tramp-compat-temporary-file-directory))
6146 (regexp (concat "^\\(" tramp-host-regexp
6147 "\\)\\.ssh-\\(dss\\|rsa\\)\\.pub$"))
6148 (files (when (file-directory-p dirname) (directory-files dirname)))
6149 result)
6150 (while files
6151 (when (string-match regexp (car files))
6152 (push (list nil (match-string 1 (car files))) result))
6153 (setq files (cdr files)))
6154 result))
6155
6156 (defun tramp-parse-hosts (filename)
6157 "Return a list of (user host) tuples allowed to access.
6158 User is always nil."
6159 ;; On Windows, there are problems in completion when
6160 ;; `default-directory' is remote.
6161 (let ((default-directory (tramp-compat-temporary-file-directory))
6162 res)
6163 (when (file-readable-p filename)
6164 (with-temp-buffer
6165 (insert-file-contents filename)
6166 (goto-char (point-min))
6167 (while (not (eobp))
6168 (push (tramp-parse-hosts-group) res))))
6169 res))
6170
6171 (defun tramp-parse-hosts-group ()
6172 "Return a (user host) tuple allowed to access.
6173 User is always nil."
6174 (let ((result)
6175 (regexp
6176 (concat "^\\(" tramp-ipv6-regexp "\\|" tramp-host-regexp "\\)")))
6177 (narrow-to-region (point) (tramp-compat-line-end-position))
6178 (when (re-search-forward regexp nil t)
6179 (setq result (list nil (match-string 1))))
6180 (widen)
6181 (or
6182 (> (skip-chars-forward " \t") 0)
6183 (forward-line 1))
6184 result))
6185
6186 ;; For su-alike methods it would be desirable to return "root@localhost"
6187 ;; as default. Unfortunately, we have no information whether any user name
6188 ;; has been typed already. So we use `tramp-current-user' as indication,
6189 ;; assuming it is set in `tramp-completion-handle-file-name-all-completions'.
6190 (defun tramp-parse-passwd (filename)
6191 "Return a list of (user host) tuples allowed to access.
6192 Host is always \"localhost\"."
6193 ;; On Windows, there are problems in completion when
6194 ;; `default-directory' is remote.
6195 (let ((default-directory (tramp-compat-temporary-file-directory))
6196 res)
6197 (if (zerop (length tramp-current-user))
6198 '(("root" nil))
6199 (when (file-readable-p filename)
6200 (with-temp-buffer
6201 (insert-file-contents filename)
6202 (goto-char (point-min))
6203 (while (not (eobp))
6204 (push (tramp-parse-passwd-group) res))))
6205 res)))
6206
6207 (defun tramp-parse-passwd-group ()
6208 "Return a (user host) tuple allowed to access.
6209 Host is always \"localhost\"."
6210 (let ((result)
6211 (regexp (concat "^\\(" tramp-user-regexp "\\):")))
6212 (narrow-to-region (point) (tramp-compat-line-end-position))
6213 (when (re-search-forward regexp nil t)
6214 (setq result (list (match-string 1) "localhost")))
6215 (widen)
6216 (forward-line 1)
6217 result))
6218
6219 (defun tramp-parse-netrc (filename)
6220 "Return a list of (user host) tuples allowed to access.
6221 User may be nil."
6222 ;; On Windows, there are problems in completion when
6223 ;; `default-directory' is remote.
6224 (let ((default-directory (tramp-compat-temporary-file-directory))
6225 res)
6226 (when (file-readable-p filename)
6227 (with-temp-buffer
6228 (insert-file-contents filename)
6229 (goto-char (point-min))
6230 (while (not (eobp))
6231 (push (tramp-parse-netrc-group) res))))
6232 res))
6233
6234 (defun tramp-parse-netrc-group ()
6235 "Return a (user host) tuple allowed to access.
6236 User may be nil."
6237 (let ((result)
6238 (regexp
6239 (concat
6240 "^[ \t]*machine[ \t]+" "\\(" tramp-host-regexp "\\)"
6241 "\\([ \t]+login[ \t]+" "\\(" tramp-user-regexp "\\)" "\\)?")))
6242 (narrow-to-region (point) (tramp-compat-line-end-position))
6243 (when (re-search-forward regexp nil t)
6244 (setq result (list (match-string 3) (match-string 1))))
6245 (widen)
6246 (forward-line 1)
6247 result))
6248
6249 (defun tramp-parse-putty (registry)
6250 "Return a list of (user host) tuples allowed to access.
6251 User is always nil."
6252 ;; On Windows, there are problems in completion when
6253 ;; `default-directory' is remote.
6254 (let ((default-directory (tramp-compat-temporary-file-directory))
6255 res)
6256 (with-temp-buffer
6257 (when (zerop (tramp-local-call-process "reg" nil t nil "query" registry))
6258 (goto-char (point-min))
6259 (while (not (eobp))
6260 (push (tramp-parse-putty-group registry) res))))
6261 res))
6262
6263 (defun tramp-parse-putty-group (registry)
6264 "Return a (user host) tuple allowed to access.
6265 User is always nil."
6266 (let ((result)
6267 (regexp (concat (regexp-quote registry) "\\\\\\(.+\\)")))
6268 (narrow-to-region (point) (tramp-compat-line-end-position))
6269 (when (re-search-forward regexp nil t)
6270 (setq result (list nil (match-string 1))))
6271 (widen)
6272 (forward-line 1)
6273 result))
6274
6275 ;;; Internal Functions:
6276
6277 (defun tramp-maybe-send-script (vec script name)
6278 "Define in remote shell function NAME implemented as SCRIPT.
6279 Only send the definition if it has not already been done."
6280 (let* ((p (tramp-get-connection-process vec))
6281 (scripts (tramp-get-connection-property p "scripts" nil)))
6282 (unless (member name scripts)
6283 (with-progress-reporter vec 5 (format "Sending script `%s'" name)
6284 ;; The script could contain a call of Perl. This is masked with `%s'.
6285 (tramp-send-command-and-check
6286 vec
6287 (format "%s () {\n%s\n}" name
6288 (format script (tramp-get-remote-perl vec))))
6289 (tramp-set-connection-property p "scripts" (cons name scripts))))))
6290
6291 (defun tramp-set-auto-save ()
6292 (when (and ;; ange-ftp has its own auto-save mechanism
6293 (eq (tramp-find-foreign-file-name-handler (buffer-file-name))
6294 'tramp-sh-file-name-handler)
6295 auto-save-default)
6296 (auto-save-mode 1)))
6297 (add-hook 'find-file-hooks 'tramp-set-auto-save t)
6298 (add-hook 'tramp-unload-hook
6299 (lambda ()
6300 (remove-hook 'find-file-hooks 'tramp-set-auto-save)))
6301
6302 (defun tramp-run-test (switch filename)
6303 "Run `test' on the remote system, given a SWITCH and a FILENAME.
6304 Returns the exit code of the `test' program."
6305 (with-parsed-tramp-file-name filename nil
6306 (tramp-send-command-and-check
6307 v
6308 (format
6309 "%s %s %s"
6310 (tramp-get-test-command v)
6311 switch
6312 (tramp-shell-quote-argument localname)))))
6313
6314 (defun tramp-run-test2 (format-string file1 file2)
6315 "Run `test'-like program on the remote system, given FILE1, FILE2.
6316 FORMAT-STRING contains the program name, switches, and place holders.
6317 Returns the exit code of the `test' program. Barfs if the methods,
6318 hosts, or files, disagree."
6319 (unless (tramp-equal-remote file1 file2)
6320 (with-parsed-tramp-file-name (if (tramp-tramp-file-p file1) file1 file2) nil
6321 (tramp-error
6322 v 'file-error
6323 "tramp-run-test2 only implemented for same method, user, host")))
6324 (with-parsed-tramp-file-name file1 v1
6325 (with-parsed-tramp-file-name file1 v2
6326 (tramp-send-command-and-check
6327 v1
6328 (format format-string
6329 (tramp-shell-quote-argument v1-localname)
6330 (tramp-shell-quote-argument v2-localname))))))
6331
6332 (defun tramp-buffer-name (vec)
6333 "A name for the connection buffer VEC."
6334 ;; We must use `tramp-file-name-real-host', because for gateway
6335 ;; methods the default port will be expanded later on, which would
6336 ;; tamper the name.
6337 (let ((method (tramp-file-name-method vec))
6338 (user (tramp-file-name-user vec))
6339 (host (tramp-file-name-real-host vec)))
6340 (if (not (zerop (length user)))
6341 (format "*tramp/%s %s@%s*" method user host)
6342 (format "*tramp/%s %s*" method host))))
6343
6344 (defun tramp-delete-temp-file-function ()
6345 "Remove temporary files related to current buffer."
6346 (when (stringp tramp-temp-buffer-file-name)
6347 (condition-case nil
6348 (tramp-compat-delete-file tramp-temp-buffer-file-name 'force)
6349 (error nil))))
6350
6351 (add-hook 'kill-buffer-hook 'tramp-delete-temp-file-function)
6352 (add-hook 'tramp-cache-unload-hook
6353 (lambda ()
6354 (remove-hook 'kill-buffer-hook
6355 'tramp-delete-temp-file-function)))
6356
6357 (defun tramp-get-buffer (vec)
6358 "Get the connection buffer to be used for VEC."
6359 (or (get-buffer (tramp-buffer-name vec))
6360 (with-current-buffer (get-buffer-create (tramp-buffer-name vec))
6361 (setq buffer-undo-list t)
6362 (setq default-directory
6363 (tramp-make-tramp-file-name
6364 (tramp-file-name-method vec)
6365 (tramp-file-name-user vec)
6366 (tramp-file-name-host vec)
6367 "/"))
6368 (current-buffer))))
6369
6370 (defun tramp-get-connection-buffer (vec)
6371 "Get the connection buffer to be used for VEC.
6372 In case a second asynchronous communication has been started, it is different
6373 from `tramp-get-buffer'."
6374 (or (tramp-get-connection-property vec "process-buffer" nil)
6375 (tramp-get-buffer vec)))
6376
6377 (defun tramp-get-connection-process (vec)
6378 "Get the connection process to be used for VEC.
6379 In case a second asynchronous communication has been started, it is different
6380 from the default one."
6381 (get-process
6382 (or (tramp-get-connection-property vec "process-name" nil)
6383 (tramp-buffer-name vec))))
6384
6385 (defun tramp-debug-buffer-name (vec)
6386 "A name for the debug buffer for VEC."
6387 ;; We must use `tramp-file-name-real-host', because for gateway
6388 ;; methods the default port will be expanded later on, which would
6389 ;; tamper the name.
6390 (let ((method (tramp-file-name-method vec))
6391 (user (tramp-file-name-user vec))
6392 (host (tramp-file-name-real-host vec)))
6393 (if (not (zerop (length user)))
6394 (format "*debug tramp/%s %s@%s*" method user host)
6395 (format "*debug tramp/%s %s*" method host))))
6396
6397 (defconst tramp-debug-outline-regexp
6398 "[0-9]+:[0-9]+:[0-9]+\\.[0-9]+ [a-z0-9-]+ (\\([0-9]+\\)) #")
6399
6400 (defun tramp-get-debug-buffer (vec)
6401 "Get the debug buffer for VEC."
6402 (with-current-buffer
6403 (get-buffer-create (tramp-debug-buffer-name vec))
6404 (when (bobp)
6405 (setq buffer-undo-list t)
6406 ;; Activate `outline-mode'. This runs `text-mode-hook' and
6407 ;; `outline-mode-hook'. We must prevent that local processes
6408 ;; die. Yes: I've seen `flyspell-mode', which starts "ispell".
6409 ;; Furthermore, `outline-regexp' must have the correct value
6410 ;; already, because it is used by `font-lock-compile-keywords'.
6411 (let ((default-directory (tramp-compat-temporary-file-directory))
6412 (outline-regexp tramp-debug-outline-regexp))
6413 (outline-mode))
6414 (set (make-local-variable 'outline-regexp) tramp-debug-outline-regexp)
6415 (set (make-local-variable 'outline-level) 'tramp-outline-level))
6416 (current-buffer)))
6417
6418 (defun tramp-outline-level ()
6419 "Return the depth to which a statement is nested in the outline.
6420 Point must be at the beginning of a header line.
6421
6422 The outline level is equal to the verbosity of the Tramp message."
6423 (1+ (string-to-number (match-string 1))))
6424
6425 (defun tramp-find-executable
6426 (vec progname dirlist &optional ignore-tilde ignore-path)
6427 "Searches for PROGNAME in $PATH and all directories mentioned in DIRLIST.
6428 First arg VEC specifies the connection, PROGNAME is the program
6429 to search for, and DIRLIST gives the list of directories to
6430 search. If IGNORE-TILDE is non-nil, directory names starting
6431 with `~' will be ignored. If IGNORE-PATH is non-nil, searches
6432 only in DIRLIST.
6433
6434 Returns the absolute file name of PROGNAME, if found, and nil otherwise.
6435
6436 This function expects to be in the right *tramp* buffer."
6437 (with-current-buffer (tramp-get-connection-buffer vec)
6438 (let (result)
6439 ;; Check whether the executable is in $PATH. "which(1)" does not
6440 ;; report always a correct error code; therefore we check the
6441 ;; number of words it returns.
6442 (unless ignore-path
6443 (tramp-send-command vec (format "which \\%s | wc -w" progname))
6444 (goto-char (point-min))
6445 (if (looking-at "^\\s-*1$")
6446 (setq result (concat "\\" progname))))
6447 (unless result
6448 (when ignore-tilde
6449 ;; Remove all ~/foo directories from dirlist. In XEmacs,
6450 ;; `remove' is in CL, and we want to avoid CL dependencies.
6451 (let (newdl d)
6452 (while dirlist
6453 (setq d (car dirlist))
6454 (setq dirlist (cdr dirlist))
6455 (unless (char-equal ?~ (aref d 0))
6456 (setq newdl (cons d newdl))))
6457 (setq dirlist (nreverse newdl))))
6458 (tramp-send-command
6459 vec
6460 (format (concat "while read d; "
6461 "do if test -x $d/%s -a -f $d/%s; "
6462 "then echo tramp_executable $d/%s; "
6463 "break; fi; done <<'EOF'\n"
6464 "%s\nEOF")
6465 progname progname progname (mapconcat 'identity dirlist "\n")))
6466 (goto-char (point-max))
6467 (when (search-backward "tramp_executable " nil t)
6468 (skip-chars-forward "^ ")
6469 (skip-chars-forward " ")
6470 (setq result (buffer-substring
6471 (point) (tramp-compat-line-end-position)))))
6472 result)))
6473
6474 (defun tramp-set-remote-path (vec)
6475 "Sets the remote environment PATH to existing directories.
6476 I.e., for each directory in `tramp-remote-path', it is tested
6477 whether it exists and if so, it is added to the environment
6478 variable PATH."
6479 (tramp-message vec 5 (format "Setting $PATH environment variable"))
6480 (tramp-send-command
6481 vec (format "PATH=%s; export PATH"
6482 (mapconcat 'identity (tramp-get-remote-path vec) ":"))))
6483
6484 ;; ------------------------------------------------------------
6485 ;; -- Communication with external shell --
6486 ;; ------------------------------------------------------------
6487
6488 (defun tramp-find-file-exists-command (vec)
6489 "Find a command on the remote host for checking if a file exists.
6490 Here, we are looking for a command which has zero exit status if the
6491 file exists and nonzero exit status otherwise."
6492 (let ((existing "/")
6493 (nonexisting
6494 (tramp-shell-quote-argument "/ this file does not exist "))
6495 result)
6496 ;; The algorithm is as follows: we try a list of several commands.
6497 ;; For each command, we first run `$cmd /' -- this should return
6498 ;; true, as the root directory always exists. And then we run
6499 ;; `$cmd /this\ file\ does\ not\ exist ', hoping that the file indeed
6500 ;; does not exist. This should return false. We use the first
6501 ;; command we find that seems to work.
6502 ;; The list of commands to try is as follows:
6503 ;; `ls -d' This works on most systems, but NetBSD 1.4
6504 ;; has a bug: `ls' always returns zero exit
6505 ;; status, even for files which don't exist.
6506 ;; `test -e' Some Bourne shells have a `test' builtin
6507 ;; which does not know the `-e' option.
6508 ;; `/bin/test -e' For those, the `test' binary on disk normally
6509 ;; provides the option. Alas, the binary
6510 ;; is sometimes `/bin/test' and sometimes it's
6511 ;; `/usr/bin/test'.
6512 ;; `/usr/bin/test -e' In case `/bin/test' does not exist.
6513 (unless (or
6514 (and (setq result (format "%s -e" (tramp-get-test-command vec)))
6515 (zerop (tramp-send-command-and-check
6516 vec (format "%s %s" result existing)))
6517 (not (zerop (tramp-send-command-and-check
6518 vec (format "%s %s" result nonexisting)))))
6519 (and (setq result "/bin/test -e")
6520 (zerop (tramp-send-command-and-check
6521 vec (format "%s %s" result existing)))
6522 (not (zerop (tramp-send-command-and-check
6523 vec (format "%s %s" result nonexisting)))))
6524 (and (setq result "/usr/bin/test -e")
6525 (zerop (tramp-send-command-and-check
6526 vec (format "%s %s" result existing)))
6527 (not (zerop (tramp-send-command-and-check
6528 vec (format "%s %s" result nonexisting)))))
6529 (and (setq result (format "%s -d" (tramp-get-ls-command vec)))
6530 (zerop (tramp-send-command-and-check
6531 vec (format "%s %s" result existing)))
6532 (not (zerop (tramp-send-command-and-check
6533 vec (format "%s %s" result nonexisting))))))
6534 (tramp-error
6535 vec 'file-error "Couldn't find command to check if file exists"))
6536 result))
6537
6538 ;; CCC test ksh or bash found for tilde expansion?
6539 (defun tramp-find-shell (vec)
6540 "Opens a shell on the remote host which groks tilde expansion."
6541 (unless (tramp-get-connection-property vec "remote-shell" nil)
6542 (let (shell)
6543 (with-current-buffer (tramp-get-buffer vec)
6544 (tramp-send-command vec "echo ~root" t)
6545 (cond
6546 ((or (string-match "^~root$" (buffer-string))
6547 ;; The default shell (ksh93) of OpenSolaris is buggy.
6548 (string-equal (tramp-get-connection-property vec "uname" "")
6549 "SunOS 5.11"))
6550 (setq shell
6551 (or (tramp-find-executable
6552 vec "bash" (tramp-get-remote-path vec) t t)
6553 (tramp-find-executable
6554 vec "ksh" (tramp-get-remote-path vec) t t)))
6555 (unless shell
6556 (tramp-error
6557 vec 'file-error
6558 "Couldn't find a shell which groks tilde expansion"))
6559 ;; Find arguments for this shell.
6560 (let ((alist tramp-sh-extra-args)
6561 item extra-args)
6562 (while (and alist (null extra-args))
6563 (setq item (pop alist))
6564 (when (string-match (car item) shell)
6565 (setq extra-args (cdr item))))
6566 (when extra-args (setq shell (concat shell " " extra-args))))
6567 (tramp-message
6568 vec 5 "Starting remote shell `%s' for tilde expansion" shell)
6569 (let ((tramp-end-of-output tramp-initial-end-of-output))
6570 (tramp-send-command
6571 vec
6572 (format "PROMPT_COMMAND='' PS1=%s PS2='' PS3='' exec %s"
6573 (shell-quote-argument tramp-end-of-output) shell)
6574 t))
6575 ;; Setting prompts.
6576 (with-progress-reporter vec 5 (format "Setting remote shell prompt")
6577 (tramp-send-command
6578 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
6579 (tramp-send-command vec "PS2=''" t)
6580 (tramp-send-command vec "PS3=''" t)
6581 (tramp-send-command vec "PROMPT_COMMAND=''" t)))
6582
6583 (t (tramp-message
6584 vec 5 "Remote `%s' groks tilde expansion, good"
6585 (tramp-get-method-parameter
6586 (tramp-file-name-method vec) 'tramp-remote-sh))
6587 (tramp-set-connection-property
6588 vec "remote-shell"
6589 (tramp-get-method-parameter
6590 (tramp-file-name-method vec) 'tramp-remote-sh))))))))
6591
6592 ;; ------------------------------------------------------------
6593 ;; -- Functions for establishing connection --
6594 ;; ------------------------------------------------------------
6595
6596 ;; The following functions are actions to be taken when seeing certain
6597 ;; prompts from the remote host. See the variable
6598 ;; `tramp-actions-before-shell' for usage of these functions.
6599
6600 (defun tramp-action-login (proc vec)
6601 "Send the login name."
6602 (when (not (stringp tramp-current-user))
6603 (save-window-excursion
6604 (let ((enable-recursive-minibuffers t))
6605 (pop-to-buffer (tramp-get-connection-buffer vec))
6606 (setq tramp-current-user (read-string (match-string 0))))))
6607 (tramp-message vec 3 "Sending login name `%s'" tramp-current-user)
6608 (with-current-buffer (tramp-get-connection-buffer vec)
6609 (tramp-message vec 6 "\n%s" (buffer-string)))
6610 (tramp-send-string vec tramp-current-user))
6611
6612 (defun tramp-action-password (proc vec)
6613 "Query the user for a password."
6614 (with-current-buffer (process-buffer proc)
6615 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
6616 (tramp-message vec 3 "Sending %s" (match-string 1)))
6617 (tramp-enter-password proc))
6618
6619 (defun tramp-action-succeed (proc vec)
6620 "Signal success in finding shell prompt."
6621 (throw 'tramp-action 'ok))
6622
6623 (defun tramp-action-permission-denied (proc vec)
6624 "Signal permission denied."
6625 (kill-process proc)
6626 (throw 'tramp-action 'permission-denied))
6627
6628 (defun tramp-action-yesno (proc vec)
6629 "Ask the user for confirmation using `yes-or-no-p'.
6630 Send \"yes\" to remote process on confirmation, abort otherwise.
6631 See also `tramp-action-yn'."
6632 (save-window-excursion
6633 (let ((enable-recursive-minibuffers t))
6634 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
6635 (unless (yes-or-no-p (match-string 0))
6636 (kill-process proc)
6637 (throw 'tramp-action 'permission-denied))
6638 (with-current-buffer (tramp-get-connection-buffer vec)
6639 (tramp-message vec 6 "\n%s" (buffer-string)))
6640 (tramp-send-string vec "yes"))))
6641
6642 (defun tramp-action-yn (proc vec)
6643 "Ask the user for confirmation using `y-or-n-p'.
6644 Send \"y\" to remote process on confirmation, abort otherwise.
6645 See also `tramp-action-yesno'."
6646 (save-window-excursion
6647 (let ((enable-recursive-minibuffers t))
6648 (save-match-data (pop-to-buffer (tramp-get-connection-buffer vec)))
6649 (unless (y-or-n-p (match-string 0))
6650 (kill-process proc)
6651 (throw 'tramp-action 'permission-denied))
6652 (with-current-buffer (tramp-get-connection-buffer vec)
6653 (tramp-message vec 6 "\n%s" (buffer-string)))
6654 (tramp-send-string vec "y"))))
6655
6656 (defun tramp-action-terminal (proc vec)
6657 "Tell the remote host which terminal type to use.
6658 The terminal type can be configured with `tramp-terminal-type'."
6659 (tramp-message vec 5 "Setting `%s' as terminal type." tramp-terminal-type)
6660 (with-current-buffer (tramp-get-connection-buffer vec)
6661 (tramp-message vec 6 "\n%s" (buffer-string)))
6662 (tramp-send-string vec tramp-terminal-type))
6663
6664 (defun tramp-action-process-alive (proc vec)
6665 "Check, whether a process has finished."
6666 (unless (memq (process-status proc) '(run open))
6667 (throw 'tramp-action 'process-died)))
6668
6669 (defun tramp-action-out-of-band (proc vec)
6670 "Check, whether an out-of-band copy has finished."
6671 (cond ((and (memq (process-status proc) '(stop exit))
6672 (zerop (process-exit-status proc)))
6673 (tramp-message vec 3 "Process has finished.")
6674 (throw 'tramp-action 'ok))
6675 ((or (and (memq (process-status proc) '(stop exit))
6676 (not (zerop (process-exit-status proc))))
6677 (memq (process-status proc) '(signal)))
6678 ;; `scp' could have copied correctly, but set modes could have failed.
6679 ;; This can be ignored.
6680 (with-current-buffer (process-buffer proc)
6681 (goto-char (point-min))
6682 (if (re-search-forward tramp-operation-not-permitted-regexp nil t)
6683 (progn
6684 (tramp-message vec 5 "'set mode' error ignored.")
6685 (tramp-message vec 3 "Process has finished.")
6686 (throw 'tramp-action 'ok))
6687 (tramp-message vec 3 "Process has died.")
6688 (throw 'tramp-action 'process-died))))
6689 (t nil)))
6690
6691 ;; Functions for processing the actions.
6692
6693 (defun tramp-process-one-action (proc vec actions)
6694 "Wait for output from the shell and perform one action."
6695 (let (found todo item pattern action)
6696 (while (not found)
6697 ;; Reread output once all actions have been performed.
6698 ;; Obviously, the output was not complete.
6699 (tramp-accept-process-output proc 1)
6700 (setq todo actions)
6701 (while todo
6702 (setq item (pop todo))
6703 (setq pattern (format "\\(%s\\)\\'" (symbol-value (nth 0 item))))
6704 (setq action (nth 1 item))
6705 (tramp-message
6706 vec 5 "Looking for regexp \"%s\" from remote shell" pattern)
6707 (when (tramp-check-for-regexp proc pattern)
6708 (tramp-message vec 5 "Call `%s'" (symbol-name action))
6709 (setq found (funcall action proc vec)))))
6710 found))
6711
6712 (defun tramp-process-actions (proc vec actions &optional timeout)
6713 "Perform actions until success or TIMEOUT."
6714 ;; Enable auth-source and password-cache.
6715 (tramp-set-connection-property vec "first-password-request" t)
6716 (let (exit)
6717 (while (not exit)
6718 (tramp-message proc 3 "Waiting for prompts from remote shell")
6719 (setq exit
6720 (catch 'tramp-action
6721 (if timeout
6722 (with-timeout (timeout)
6723 (tramp-process-one-action proc vec actions))
6724 (tramp-process-one-action proc vec actions)))))
6725 (with-current-buffer (tramp-get-connection-buffer vec)
6726 (tramp-message vec 6 "\n%s" (buffer-string)))
6727 (unless (eq exit 'ok)
6728 (tramp-clear-passwd vec)
6729 (tramp-error-with-buffer
6730 nil vec 'file-error
6731 (cond
6732 ((eq exit 'permission-denied) "Permission denied")
6733 ((eq exit 'process-died) "Process died")
6734 (t "Login failed"))))))
6735
6736 ;; Utility functions.
6737
6738 (defun tramp-accept-process-output (&optional proc timeout timeout-msecs)
6739 "Like `accept-process-output' for Tramp processes.
6740 This is needed in order to hide `last-coding-system-used', which is set
6741 for process communication also."
6742 (with-current-buffer (process-buffer proc)
6743 (tramp-message proc 10 "%s %s" proc (process-status proc))
6744 (let (buffer-read-only last-coding-system-used)
6745 ;; Under Windows XP, accept-process-output doesn't return
6746 ;; sometimes. So we add an additional timeout.
6747 (with-timeout ((or timeout 1))
6748 (accept-process-output proc timeout timeout-msecs)))
6749 (tramp-message proc 10 "\n%s" (buffer-string))))
6750
6751 (defun tramp-check-for-regexp (proc regexp)
6752 "Check, whether REGEXP is contained in process buffer of PROC.
6753 Erase echoed commands if exists."
6754 (with-current-buffer (process-buffer proc)
6755 (goto-char (point-min))
6756
6757 ;; Check whether we need to remove echo output.
6758 (when (and (tramp-get-connection-property proc "check-remote-echo" nil)
6759 (re-search-forward tramp-echoed-echo-mark-regexp nil t))
6760 (let ((begin (match-beginning 0)))
6761 (when (re-search-forward tramp-echoed-echo-mark-regexp nil t)
6762 ;; Discard echo from remote output.
6763 (tramp-set-connection-property proc "check-remote-echo" nil)
6764 (tramp-message proc 5 "echo-mark found")
6765 (forward-line 1)
6766 (delete-region begin (point))
6767 (goto-char (point-min)))))
6768
6769 (when (or (not (tramp-get-connection-property proc "check-remote-echo" nil))
6770 ;; Sometimes, the echo string is suppressed on the remote side.
6771 (not (string-equal
6772 (tramp-compat-funcall
6773 'substring-no-properties tramp-echo-mark-marker
6774 0 (min tramp-echo-mark-marker-length (1- (point-max))))
6775 (tramp-compat-funcall
6776 'buffer-substring-no-properties
6777 1 (min (1+ tramp-echo-mark-marker-length) (point-max))))))
6778 ;; No echo to be handled, now we can look for the regexp.
6779 (goto-char (point-min))
6780 (re-search-forward regexp nil t))))
6781
6782 (defun tramp-wait-for-regexp (proc timeout regexp)
6783 "Wait for a REGEXP to appear from process PROC within TIMEOUT seconds.
6784 Expects the output of PROC to be sent to the current buffer. Returns
6785 the string that matched, or nil. Waits indefinitely if TIMEOUT is
6786 nil."
6787 (with-current-buffer (process-buffer proc)
6788 (let ((found (tramp-check-for-regexp proc regexp))
6789 (start-time (current-time)))
6790 (cond (timeout
6791 ;; Work around a bug in XEmacs 21, where the timeout
6792 ;; expires faster than it should. This degenerates
6793 ;; to polling for buggy XEmacsen, but oh, well.
6794 (while (and (not found)
6795 (< (tramp-time-diff (current-time) start-time)
6796 timeout))
6797 (with-timeout (timeout)
6798 (while (not found)
6799 (tramp-accept-process-output proc 1)
6800 (unless (memq (process-status proc) '(run open))
6801 (tramp-error-with-buffer
6802 nil proc 'file-error "Process has died"))
6803 (setq found (tramp-check-for-regexp proc regexp))))))
6804 (t
6805 (while (not found)
6806 (tramp-accept-process-output proc 1)
6807 (unless (memq (process-status proc) '(run open))
6808 (tramp-error-with-buffer
6809 nil proc 'file-error "Process has died"))
6810 (setq found (tramp-check-for-regexp proc regexp)))))
6811 (tramp-message proc 6 "\n%s" (buffer-string))
6812 (when (not found)
6813 (if timeout
6814 (tramp-error
6815 proc 'file-error "[[Regexp `%s' not found in %d secs]]"
6816 regexp timeout)
6817 (tramp-error proc 'file-error "[[Regexp `%s' not found]]" regexp)))
6818 found)))
6819
6820 (defun tramp-barf-if-no-shell-prompt (proc timeout &rest error-args)
6821 "Wait for shell prompt and barf if none appears.
6822 Looks at process PROC to see if a shell prompt appears in TIMEOUT
6823 seconds. If not, it produces an error message with the given ERROR-ARGS."
6824 (unless
6825 (tramp-wait-for-regexp
6826 proc timeout
6827 (format
6828 "\\(%s\\|%s\\)\\'" shell-prompt-pattern tramp-shell-prompt-pattern))
6829 (apply 'tramp-error-with-buffer nil proc 'file-error error-args)))
6830
6831 ;; We don't call `tramp-send-string' in order to hide the password
6832 ;; from the debug buffer, and because end-of-line handling of the
6833 ;; string.
6834 (defun tramp-enter-password (proc)
6835 "Prompt for a password and send it to the remote end."
6836 (process-send-string
6837 proc (concat (tramp-read-passwd proc)
6838 (or (tramp-get-method-parameter
6839 tramp-current-method
6840 'tramp-password-end-of-line)
6841 tramp-default-password-end-of-line))))
6842
6843 (defun tramp-open-connection-setup-interactive-shell (proc vec)
6844 "Set up an interactive shell.
6845 Mainly sets the prompt and the echo correctly. PROC is the shell
6846 process to set up. VEC specifies the connection."
6847 (let ((tramp-end-of-output tramp-initial-end-of-output))
6848 ;; It is useful to set the prompt in the following command because
6849 ;; some people have a setting for $PS1 which /bin/sh doesn't know
6850 ;; about and thus /bin/sh will display a strange prompt. For
6851 ;; example, if $PS1 has "${CWD}" in the value, then ksh will
6852 ;; display the current working directory but /bin/sh will display
6853 ;; a dollar sign. The following command line sets $PS1 to a sane
6854 ;; value, and works under Bourne-ish shells as well as csh-like
6855 ;; shells. Daniel Pittman reports that the unusual positioning of
6856 ;; the single quotes makes it work under `rc', too. We also unset
6857 ;; the variable $ENV because that is read by some sh
6858 ;; implementations (eg, bash when called as sh) on startup; this
6859 ;; way, we avoid the startup file clobbering $PS1. $PROMP_COMMAND
6860 ;; is another way to set the prompt in /bin/bash, it must be
6861 ;; discarded as well.
6862 (tramp-send-command
6863 vec
6864 (format
6865 "exec env ENV='' PROMPT_COMMAND='' PS1=%s PS2='' PS3='' %s"
6866 (shell-quote-argument tramp-end-of-output)
6867 (tramp-get-method-parameter
6868 (tramp-file-name-method vec) 'tramp-remote-sh))
6869 t)
6870
6871 ;; Disable echo.
6872 (tramp-message vec 5 "Setting up remote shell environment")
6873 (tramp-send-command vec "stty -inlcr -echo kill '^U' erase '^H'" t)
6874 ;; Check whether the echo has really been disabled. Some
6875 ;; implementations, like busybox of embedded GNU/Linux, don't
6876 ;; support disabling.
6877 (tramp-send-command vec "echo foo" t)
6878 (with-current-buffer (process-buffer proc)
6879 (goto-char (point-min))
6880 (when (looking-at "echo foo")
6881 (tramp-set-connection-property proc "remote-echo" t)
6882 (tramp-message vec 5 "Remote echo still on. Ok.")
6883 ;; Make sure backspaces and their echo are enabled and no line
6884 ;; width magic interferes with them.
6885 (tramp-send-command vec "stty icanon erase ^H cols 32767" t))))
6886
6887 (tramp-message vec 5 "Setting shell prompt")
6888 (tramp-send-command
6889 vec (format "PS1=%s" (shell-quote-argument tramp-end-of-output)) t)
6890 (tramp-send-command vec "PS2=''" t)
6891 (tramp-send-command vec "PS3=''" t)
6892 (tramp-send-command vec "PROMPT_COMMAND=''" t)
6893
6894 ;; Try to set up the coding system correctly.
6895 ;; CCC this can't be the right way to do it. Hm.
6896 (tramp-message vec 5 "Determining coding system")
6897 (tramp-send-command vec "echo foo ; echo bar" t)
6898 (with-current-buffer (process-buffer proc)
6899 (goto-char (point-min))
6900 (if (featurep 'mule)
6901 ;; Use MULE to select the right EOL convention for communicating
6902 ;; with the process.
6903 (let* ((cs (or (tramp-compat-funcall 'process-coding-system proc)
6904 (cons 'undecided 'undecided)))
6905 cs-decode cs-encode)
6906 (when (symbolp cs) (setq cs (cons cs cs)))
6907 (setq cs-decode (car cs))
6908 (setq cs-encode (cdr cs))
6909 (unless cs-decode (setq cs-decode 'undecided))
6910 (unless cs-encode (setq cs-encode 'undecided))
6911 (setq cs-encode (tramp-coding-system-change-eol-conversion
6912 cs-encode 'unix))
6913 (when (search-forward "\r" nil t)
6914 (setq cs-decode (tramp-coding-system-change-eol-conversion
6915 cs-decode 'dos)))
6916 (tramp-compat-funcall
6917 'set-buffer-process-coding-system cs-decode cs-encode)
6918 (tramp-message
6919 vec 5 "Setting coding system to `%s' and `%s'" cs-decode cs-encode))
6920 ;; Look for ^M and do something useful if found.
6921 (when (search-forward "\r" nil t)
6922 ;; We have found a ^M but cannot frob the process coding system
6923 ;; because we're running on a non-MULE Emacs. Let's try
6924 ;; stty, instead.
6925 (tramp-send-command vec "stty -onlcr" t))))
6926 ;; Dump stty settings in the traces.
6927 (when (>= tramp-verbose 10)
6928 (tramp-send-command vec "stty -a" t))
6929 (tramp-send-command vec "set +o vi +o emacs" t)
6930
6931 ;; Check whether the output of "uname -sr" has been changed. If
6932 ;; yes, this is a strong indication that we must expire all
6933 ;; connection properties. We start again with
6934 ;; `tramp-maybe-open-connection', it will be catched there.
6935 (tramp-message vec 5 "Checking system information")
6936 (let ((old-uname (tramp-get-connection-property vec "uname" nil))
6937 (new-uname
6938 (tramp-set-connection-property
6939 vec "uname"
6940 (tramp-send-command-and-read vec "echo \\\"`uname -sr`\\\""))))
6941 (when (and (stringp old-uname) (not (string-equal old-uname new-uname)))
6942 (with-current-buffer (tramp-get-debug-buffer vec)
6943 ;; Keep the debug buffer.
6944 (rename-buffer
6945 (generate-new-buffer-name tramp-temp-buffer-name) 'unique)
6946 (tramp-compat-funcall 'tramp-cleanup-connection vec)
6947 (if (= (point-min) (point-max))
6948 (kill-buffer nil)
6949 (rename-buffer (tramp-debug-buffer-name vec) 'unique))
6950 ;; We call `tramp-get-buffer' in order to keep the debug buffer.
6951 (tramp-get-buffer vec)
6952 (tramp-message
6953 vec 3
6954 "Connection reset, because remote host changed from `%s' to `%s'"
6955 old-uname new-uname)
6956 (throw 'uname-changed (tramp-maybe-open-connection vec)))))
6957
6958 ;; Check whether the remote host suffers from buggy
6959 ;; `send-process-string'. This is known for FreeBSD (see comment in
6960 ;; `send_process', file process.c). I've tested sending 624 bytes
6961 ;; successfully, sending 625 bytes failed. Emacs makes a hack when
6962 ;; this host type is detected locally. It cannot handle remote
6963 ;; hosts, though.
6964 (with-connection-property proc "chunksize"
6965 (cond
6966 ((and (integerp tramp-chunksize) (> tramp-chunksize 0))
6967 tramp-chunksize)
6968 (t
6969 (tramp-message
6970 vec 5 "Checking remote host type for `send-process-string' bug")
6971 (if (string-match
6972 "^FreeBSD" (tramp-get-connection-property vec "uname" ""))
6973 500 0))))
6974
6975 ;; Set remote PATH variable.
6976 (tramp-set-remote-path vec)
6977
6978 ;; Search for a good shell before searching for a command which
6979 ;; checks if a file exists. This is done because Tramp wants to use
6980 ;; "test foo; echo $?" to check if various conditions hold, and
6981 ;; there are buggy /bin/sh implementations which don't execute the
6982 ;; "echo $?" part if the "test" part has an error. In particular,
6983 ;; the OpenSolaris /bin/sh is a problem. There are also other
6984 ;; problems with /bin/sh of OpenSolaris, like redirection of stderr
6985 ;; in in function declarations, or changing HISTFILE in place.
6986 ;; Therefore, OpenSolaris' /bin/sh is replaced by bash, when
6987 ;; detected.
6988 (tramp-find-shell vec)
6989
6990 ;; Disable unexpected output.
6991 (tramp-send-command vec "mesg n; biff n" t)
6992
6993 ;; Set the environment.
6994 (tramp-message vec 5 "Setting default environment")
6995
6996 (let ((env (copy-sequence tramp-remote-process-environment))
6997 unset item)
6998 (while env
6999 (setq item (tramp-compat-split-string (car env) "="))
7000 (setcdr item (mapconcat 'identity (cdr item) "="))
7001 (if (and (stringp (cdr item)) (not (string-equal (cdr item) "")))
7002 (tramp-send-command
7003 vec (format "%s=%s; export %s" (car item) (cdr item) (car item)) t)
7004 (push (car item) unset))
7005 (setq env (cdr env)))
7006 (when unset
7007 (tramp-send-command
7008 vec (format "unset %s" (mapconcat 'identity unset " "))))) t)
7009
7010 ;; CCC: We should either implement a Perl version of base64 encoding
7011 ;; and decoding. Then we just use that in the last item. The other
7012 ;; alternative is to use the Perl version of UU encoding. But then
7013 ;; we need a Lisp version of uuencode.
7014 ;;
7015 ;; Old text from documentation of tramp-methods:
7016 ;; Using a uuencode/uudecode inline method is discouraged, please use one
7017 ;; of the base64 methods instead since base64 encoding is much more
7018 ;; reliable and the commands are more standardized between the different
7019 ;; Unix versions. But if you can't use base64 for some reason, please
7020 ;; note that the default uudecode command does not work well for some
7021 ;; Unices, in particular AIX and Irix. For AIX, you might want to use
7022 ;; the following command for uudecode:
7023 ;;
7024 ;; sed '/^begin/d;/^[` ]$/d;/^end/d' | iconv -f uucode -t ISO8859-1
7025 ;;
7026 ;; For Irix, no solution is known yet.
7027
7028 (defconst tramp-local-coding-commands
7029 '((b64 base64-encode-region base64-decode-region)
7030 (uu tramp-uuencode-region uudecode-decode-region)
7031 (pack
7032 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
7033 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
7034 "List of local coding commands for inline transfer.
7035 Each item is a list that looks like this:
7036
7037 \(FORMAT ENCODING DECODING\)
7038
7039 FORMAT is symbol describing the encoding/decoding format. It can be
7040 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
7041
7042 ENCODING and DECODING can be strings, giving commands, or symbols,
7043 giving functions. If they are strings, then they can contain
7044 the \"%s\" format specifier. If that specifier is present, the input
7045 filename will be put into the command line at that spot. If the
7046 specifier is not present, the input should be read from standard
7047 input.
7048
7049 If they are functions, they will be called with two arguments, start
7050 and end of region, and are expected to replace the region contents
7051 with the encoded or decoded results, respectively.")
7052
7053 (defconst tramp-remote-coding-commands
7054 '((b64 "base64" "base64 -d")
7055 (b64 "mimencode -b" "mimencode -u -b")
7056 (b64 "mmencode -b" "mmencode -u -b")
7057 (b64 "recode data..base64" "recode base64..data")
7058 (b64 tramp-perl-encode-with-module tramp-perl-decode-with-module)
7059 (b64 tramp-perl-encode tramp-perl-decode)
7060 (uu "uuencode xxx" "uudecode -o /dev/stdout")
7061 (uu "uuencode xxx" "uudecode -o -")
7062 (uu "uuencode xxx" "uudecode -p")
7063 (uu "uuencode xxx" tramp-uudecode)
7064 (pack
7065 "perl -e 'binmode STDIN; binmode STDOUT; print pack(q{u*}, join q{}, <>)'"
7066 "perl -e 'binmode STDIN; binmode STDOUT; print unpack(q{u*}, join q{}, <>)'"))
7067 "List of remote coding commands for inline transfer.
7068 Each item is a list that looks like this:
7069
7070 \(FORMAT ENCODING DECODING\)
7071
7072 FORMAT is symbol describing the encoding/decoding format. It can be
7073 `b64' for base64 encoding, `uu' for uu encoding, or `pack' for simple packing.
7074
7075 ENCODING and DECODING can be strings, giving commands, or symbols,
7076 giving variables. If they are strings, then they can contain
7077 the \"%s\" format specifier. If that specifier is present, the input
7078 filename will be put into the command line at that spot. If the
7079 specifier is not present, the input should be read from standard
7080 input.
7081
7082 If they are variables, this variable is a string containing a Perl
7083 implementation for this functionality. This Perl program will be transferred
7084 to the remote host, and it is available as shell function with the same name.")
7085
7086 (defun tramp-find-inline-encoding (vec)
7087 "Find an inline transfer encoding that works.
7088 Goes through the list `tramp-local-coding-commands' and
7089 `tramp-remote-coding-commands'."
7090 (save-excursion
7091 (let ((local-commands tramp-local-coding-commands)
7092 (magic "xyzzy")
7093 loc-enc loc-dec rem-enc rem-dec litem ritem found)
7094 (while (and local-commands (not found))
7095 (setq litem (pop local-commands))
7096 (catch 'wont-work-local
7097 (let ((format (nth 0 litem))
7098 (remote-commands tramp-remote-coding-commands))
7099 (setq loc-enc (nth 1 litem))
7100 (setq loc-dec (nth 2 litem))
7101 ;; If the local encoder or decoder is a string, the
7102 ;; corresponding command has to work locally.
7103 (if (not (stringp loc-enc))
7104 (tramp-message
7105 vec 5 "Checking local encoding function `%s'" loc-enc)
7106 (tramp-message
7107 vec 5 "Checking local encoding command `%s' for sanity" loc-enc)
7108 (unless (zerop (tramp-call-local-coding-command
7109 loc-enc nil nil))
7110 (throw 'wont-work-local nil)))
7111 (if (not (stringp loc-dec))
7112 (tramp-message
7113 vec 5 "Checking local decoding function `%s'" loc-dec)
7114 (tramp-message
7115 vec 5 "Checking local decoding command `%s' for sanity" loc-dec)
7116 (unless (zerop (tramp-call-local-coding-command
7117 loc-dec nil nil))
7118 (throw 'wont-work-local nil)))
7119 ;; Search for remote coding commands with the same format
7120 (while (and remote-commands (not found))
7121 (setq ritem (pop remote-commands))
7122 (catch 'wont-work-remote
7123 (when (equal format (nth 0 ritem))
7124 (setq rem-enc (nth 1 ritem))
7125 (setq rem-dec (nth 2 ritem))
7126 ;; Check if remote encoding and decoding commands can be
7127 ;; called remotely with null input and output. This makes
7128 ;; sure there are no syntax errors and the command is really
7129 ;; found. Note that we do not redirect stdout to /dev/null,
7130 ;; for two reasons: when checking the decoding command, we
7131 ;; actually check the output it gives. And also, when
7132 ;; redirecting "mimencode" output to /dev/null, then as root
7133 ;; it might change the permissions of /dev/null!
7134 (when (not (stringp rem-enc))
7135 (let ((name (symbol-name rem-enc)))
7136 (while (string-match (regexp-quote "-") name)
7137 (setq name (replace-match "_" nil t name)))
7138 (tramp-maybe-send-script vec (symbol-value rem-enc) name)
7139 (setq rem-enc name)))
7140 (tramp-message
7141 vec 5
7142 "Checking remote encoding command `%s' for sanity" rem-enc)
7143 (unless (zerop (tramp-send-command-and-check
7144 vec (format "%s </dev/null" rem-enc) t))
7145 (throw 'wont-work-remote nil))
7146
7147 (when (not (stringp rem-dec))
7148 (let ((name (symbol-name rem-dec)))
7149 (while (string-match (regexp-quote "-") name)
7150 (setq name (replace-match "_" nil t name)))
7151 (tramp-maybe-send-script vec (symbol-value rem-dec) name)
7152 (setq rem-dec name)))
7153 (tramp-message
7154 vec 5
7155 "Checking remote decoding command `%s' for sanity" rem-dec)
7156 (unless (zerop (tramp-send-command-and-check
7157 vec
7158 (format "echo %s | %s | %s"
7159 magic rem-enc rem-dec)
7160 t))
7161 (throw 'wont-work-remote nil))
7162
7163 (with-current-buffer (tramp-get-buffer vec)
7164 (goto-char (point-min))
7165 (unless (looking-at (regexp-quote magic))
7166 (throw 'wont-work-remote nil)))
7167
7168 ;; `rem-enc' and `rem-dec' could be a string meanwhile.
7169 (setq rem-enc (nth 1 ritem))
7170 (setq rem-dec (nth 2 ritem))
7171 (setq found t)))))))
7172
7173 ;; Did we find something?
7174 (unless found
7175 (tramp-error
7176 vec 'file-error "Couldn't find an inline transfer encoding"))
7177
7178 ;; Set connection properties.
7179 (tramp-message vec 5 "Using local encoding `%s'" loc-enc)
7180 (tramp-set-connection-property vec "local-encoding" loc-enc)
7181 (tramp-message vec 5 "Using local decoding `%s'" loc-dec)
7182 (tramp-set-connection-property vec "local-decoding" loc-dec)
7183 (tramp-message vec 5 "Using remote encoding `%s'" rem-enc)
7184 (tramp-set-connection-property vec "remote-encoding" rem-enc)
7185 (tramp-message vec 5 "Using remote decoding `%s'" rem-dec)
7186 (tramp-set-connection-property vec "remote-decoding" rem-dec))))
7187
7188 (defun tramp-call-local-coding-command (cmd input output)
7189 "Call the local encoding or decoding command.
7190 If CMD contains \"%s\", provide input file INPUT there in command.
7191 Otherwise, INPUT is passed via standard input.
7192 INPUT can also be nil which means `/dev/null'.
7193 OUTPUT can be a string (which specifies a filename), or t (which
7194 means standard output and thus the current buffer), or nil (which
7195 means discard it)."
7196 (tramp-local-call-process
7197 tramp-encoding-shell
7198 (when (and input (not (string-match "%s" cmd))) input)
7199 (if (eq output t) t nil)
7200 nil
7201 tramp-encoding-command-switch
7202 (concat
7203 (if (string-match "%s" cmd) (format cmd input) cmd)
7204 (if (stringp output) (concat "> " output) ""))))
7205
7206 (defconst tramp-inline-compress-commands
7207 '(("gzip" "gzip -d")
7208 ("bzip2" "bzip2 -d")
7209 ("compress" "compress -d"))
7210 "List of compress and decompress commands for inline transfer.
7211 Each item is a list that looks like this:
7212
7213 \(COMPRESS DECOMPRESS\)
7214
7215 COMPRESS or DECOMPRESS are strings with the respective commands.")
7216
7217 (defun tramp-find-inline-compress (vec)
7218 "Find an inline transfer compress command that works.
7219 Goes through the list `tramp-inline-compress-commands'."
7220 (save-excursion
7221 (let ((commands tramp-inline-compress-commands)
7222 (magic "xyzzy")
7223 item compress decompress
7224 found)
7225 (while (and commands (not found))
7226 (catch 'next
7227 (setq item (pop commands)
7228 compress (nth 0 item)
7229 decompress (nth 1 item))
7230 (tramp-message
7231 vec 5
7232 "Checking local compress command `%s', `%s' for sanity"
7233 compress decompress)
7234 (unless (zerop (tramp-call-local-coding-command
7235 (format "echo %s | %s | %s"
7236 magic compress decompress) nil nil))
7237 (throw 'next nil))
7238 (tramp-message
7239 vec 5
7240 "Checking remote compress command `%s', `%s' for sanity"
7241 compress decompress)
7242 (unless (zerop (tramp-send-command-and-check
7243 vec (format "echo %s | %s | %s"
7244 magic compress decompress) t))
7245 (throw 'next nil))
7246 (setq found t)))
7247
7248 ;; Did we find something?
7249 (if found
7250 (progn
7251 ;; Set connection properties.
7252 (tramp-message
7253 vec 5 "Using inline transfer compress command `%s'" compress)
7254 (tramp-set-connection-property vec "inline-compress" compress)
7255 (tramp-message
7256 vec 5 "Using inline transfer decompress command `%s'" decompress)
7257 (tramp-set-connection-property vec "inline-decompress" decompress))
7258
7259 (tramp-set-connection-property vec "inline-compress" nil)
7260 (tramp-set-connection-property vec "inline-decompress" nil)
7261 (tramp-message
7262 vec 2 "Couldn't find an inline transfer compress command")))))
7263
7264 (defun tramp-compute-multi-hops (vec)
7265 "Expands VEC according to `tramp-default-proxies-alist'.
7266 Gateway hops are already opened."
7267 (let ((target-alist `(,vec))
7268 (choices tramp-default-proxies-alist)
7269 item proxy)
7270
7271 ;; Look for proxy hosts to be passed.
7272 (while choices
7273 (setq item (pop choices)
7274 proxy (eval (nth 2 item)))
7275 (when (and
7276 ;; host
7277 (string-match (or (eval (nth 0 item)) "")
7278 (or (tramp-file-name-host (car target-alist)) ""))
7279 ;; user
7280 (string-match (or (eval (nth 1 item)) "")
7281 (or (tramp-file-name-user (car target-alist)) "")))
7282 (if (null proxy)
7283 ;; No more hops needed.
7284 (setq choices nil)
7285 ;; Replace placeholders.
7286 (setq proxy
7287 (format-spec
7288 proxy
7289 (format-spec-make
7290 ?u (or (tramp-file-name-user (car target-alist)) "")
7291 ?h (or (tramp-file-name-host (car target-alist)) ""))))
7292 (with-parsed-tramp-file-name proxy l
7293 ;; Add the hop.
7294 (add-to-list 'target-alist l)
7295 ;; Start next search.
7296 (setq choices tramp-default-proxies-alist)))))
7297
7298 ;; Handle gateways.
7299 (when (and (boundp 'tramp-gw-tunnel-method)
7300 (string-match (format
7301 "^\\(%s\\|%s\\)$"
7302 (symbol-value 'tramp-gw-tunnel-method)
7303 (symbol-value 'tramp-gw-socks-method))
7304 (tramp-file-name-method (car target-alist))))
7305 (let ((gw (pop target-alist))
7306 (hop (pop target-alist)))
7307 ;; Is the method prepared for gateways?
7308 (unless (tramp-get-method-parameter
7309 (tramp-file-name-method hop) 'tramp-default-port)
7310 (tramp-error
7311 vec 'file-error
7312 "Method `%s' is not supported for gateway access."
7313 (tramp-file-name-method hop)))
7314 ;; Add default port if needed.
7315 (unless
7316 (string-match
7317 tramp-host-with-port-regexp (tramp-file-name-host hop))
7318 (aset hop 2
7319 (concat
7320 (tramp-file-name-host hop) tramp-prefix-port-format
7321 (number-to-string
7322 (tramp-get-method-parameter
7323 (tramp-file-name-method hop) 'tramp-default-port)))))
7324 ;; Open the gateway connection.
7325 (add-to-list
7326 'target-alist
7327 (vector
7328 (tramp-file-name-method hop) (tramp-file-name-user hop)
7329 (tramp-compat-funcall 'tramp-gw-open-connection vec gw hop) nil))
7330 ;; For the password prompt, we need the correct values.
7331 ;; Therefore, we must remember the gateway vector. But we
7332 ;; cannot do it as connection property, because it shouldn't
7333 ;; be persistent. And we have no started process yet either.
7334 (tramp-set-file-property (car target-alist) "" "gateway" hop)))
7335
7336 ;; Foreign and out-of-band methods are not supported for multi-hops.
7337 (when (cdr target-alist)
7338 (setq choices target-alist)
7339 (while choices
7340 (setq item (pop choices))
7341 (when
7342 (or
7343 (not
7344 (tramp-get-method-parameter
7345 (tramp-file-name-method item) 'tramp-login-program))
7346 (tramp-get-method-parameter
7347 (tramp-file-name-method item) 'tramp-copy-program))
7348 (tramp-error
7349 vec 'file-error
7350 "Method `%s' is not supported for multi-hops."
7351 (tramp-file-name-method item)))))
7352
7353 ;; In case the host name is not used for the remote shell
7354 ;; command, the user could be misguided by applying a random
7355 ;; hostname.
7356 (let* ((v (car target-alist))
7357 (method (tramp-file-name-method v))
7358 (host (tramp-file-name-host v)))
7359 (unless
7360 (or
7361 ;; There are multi-hops.
7362 (cdr target-alist)
7363 ;; The host name is used for the remote shell command.
7364 (member
7365 '("%h") (tramp-get-method-parameter method 'tramp-login-args))
7366 ;; The host is local. We cannot use `tramp-local-host-p'
7367 ;; here, because it opens a connection as well.
7368 (string-match tramp-local-host-regexp host))
7369 (tramp-error
7370 v 'file-error
7371 "Host `%s' looks like a remote host, `%s' can only use the local host"
7372 host method)))
7373
7374 ;; Result.
7375 target-alist))
7376
7377 (defun tramp-maybe-open-connection (vec)
7378 "Maybe open a connection VEC.
7379 Does not do anything if a connection is already open, but re-opens the
7380 connection if a previous connection has died for some reason."
7381 (catch 'uname-changed
7382 (let ((p (tramp-get-connection-process vec))
7383 (process-environment (copy-sequence process-environment)))
7384
7385 ;; If too much time has passed since last command was sent, look
7386 ;; whether process is still alive. If it isn't, kill it. When
7387 ;; using ssh, it can sometimes happen that the remote end has
7388 ;; hung up but the local ssh client doesn't recognize this until
7389 ;; it tries to send some data to the remote end. So that's why
7390 ;; we try to send a command from time to time, then look again
7391 ;; whether the process is really alive.
7392 (condition-case nil
7393 (when (and (> (tramp-time-diff
7394 (current-time)
7395 (tramp-get-connection-property
7396 p "last-cmd-time" '(0 0 0)))
7397 60)
7398 p (processp p) (memq (process-status p) '(run open)))
7399 (tramp-send-command vec "echo are you awake" t t)
7400 (unless (and (memq (process-status p) '(run open))
7401 (tramp-wait-for-output p 10))
7402 ;; The error will be catched locally.
7403 (tramp-error vec 'file-error "Awake did fail")))
7404 (file-error
7405 (tramp-flush-connection-property vec)
7406 (tramp-flush-connection-property p)
7407 (delete-process p)
7408 (setq p nil)))
7409
7410 ;; New connection must be opened.
7411 (unless (and p (processp p) (memq (process-status p) '(run open)))
7412
7413 ;; We call `tramp-get-buffer' in order to get a debug buffer for
7414 ;; messages from the beginning.
7415 (tramp-get-buffer vec)
7416 (if (zerop (length (tramp-file-name-user vec)))
7417 (tramp-message
7418 vec 3 "Opening connection for %s using %s"
7419 (tramp-file-name-host vec)
7420 (tramp-file-name-method vec))
7421 (tramp-message
7422 vec 3 "Opening connection for %s@%s using %s"
7423 (tramp-file-name-user vec)
7424 (tramp-file-name-host vec)
7425 (tramp-file-name-method vec)))
7426
7427 ;; Start new process.
7428 (when (and p (processp p))
7429 (delete-process p))
7430 (setenv "TERM" tramp-terminal-type)
7431 (setenv "LC_ALL" "C")
7432 (setenv "PROMPT_COMMAND")
7433 (setenv "PS1" tramp-initial-end-of-output)
7434 (let* ((target-alist (tramp-compute-multi-hops vec))
7435 (process-connection-type tramp-process-connection-type)
7436 (process-adaptive-read-buffering nil)
7437 (coding-system-for-read nil)
7438 ;; This must be done in order to avoid our file name handler.
7439 (p (let ((default-directory
7440 (tramp-compat-temporary-file-directory)))
7441 (start-process
7442 (or (tramp-get-connection-property vec "process-name" nil)
7443 (tramp-buffer-name vec))
7444 (tramp-get-connection-buffer vec)
7445 tramp-encoding-shell))))
7446
7447 (tramp-message
7448 vec 6 "%s" (mapconcat 'identity (process-command p) " "))
7449
7450 ;; Check whether process is alive.
7451 (tramp-set-process-query-on-exit-flag p nil)
7452 (with-progress-reporter vec 3 "Waiting 60s for local shell to come up"
7453 (tramp-barf-if-no-shell-prompt
7454 p 60 "Couldn't find local shell prompt %s" tramp-encoding-shell))
7455
7456 ;; Now do all the connections as specified.
7457 (while target-alist
7458 (let* ((hop (car target-alist))
7459 (l-method (tramp-file-name-method hop))
7460 (l-user (tramp-file-name-user hop))
7461 (l-host (tramp-file-name-host hop))
7462 (l-port nil)
7463 (login-program
7464 (tramp-get-method-parameter l-method 'tramp-login-program))
7465 (login-args
7466 (tramp-get-method-parameter l-method 'tramp-login-args))
7467 (gw-args
7468 (tramp-get-method-parameter l-method 'tramp-gw-args))
7469 (gw (tramp-get-file-property hop "" "gateway" nil))
7470 (g-method (and gw (tramp-file-name-method gw)))
7471 (g-user (and gw (tramp-file-name-user gw)))
7472 (g-host (and gw (tramp-file-name-host gw)))
7473 (command login-program)
7474 ;; We don't create the temporary file. In fact, it
7475 ;; is just a prefix for the ControlPath option of
7476 ;; ssh; the real temporary file has another name, and
7477 ;; it is created and protected by ssh. It is also
7478 ;; removed by ssh, when the connection is closed.
7479 (tmpfile
7480 (tramp-set-connection-property
7481 p "temp-file"
7482 (make-temp-name
7483 (expand-file-name
7484 tramp-temp-name-prefix
7485 (tramp-compat-temporary-file-directory)))))
7486 spec)
7487
7488 ;; Add gateway arguments if necessary.
7489 (when (and gw gw-args)
7490 (setq login-args (append login-args gw-args)))
7491
7492 ;; Check for port number. Until now, there's no need
7493 ;; for handling like method, user, host.
7494 (when (string-match tramp-host-with-port-regexp l-host)
7495 (setq l-port (match-string 2 l-host)
7496 l-host (match-string 1 l-host)))
7497
7498 ;; Set variables for computing the prompt for reading
7499 ;; password. They can also be derived from a gateway.
7500 (setq tramp-current-method (or g-method l-method)
7501 tramp-current-user (or g-user l-user)
7502 tramp-current-host (or g-host l-host))
7503
7504 ;; Replace login-args place holders.
7505 (setq
7506 l-host (or l-host "")
7507 l-user (or l-user "")
7508 l-port (or l-port "")
7509 spec (format-spec-make ?h l-host ?u l-user ?p l-port ?t tmpfile)
7510 command
7511 (concat
7512 ;; We do not want to see the trailing local prompt in
7513 ;; `start-file-process'.
7514 (unless (memq system-type '(windows-nt)) "exec ")
7515 command " "
7516 (mapconcat
7517 (lambda (x)
7518 (setq x (mapcar (lambda (y) (format-spec y spec)) x))
7519 (unless (member "" x) (mapconcat 'identity x " ")))
7520 login-args " ")
7521 ;; Local shell could be a Windows COMSPEC. It doesn't
7522 ;; know the ";" syntax, but we must exit always for
7523 ;; `start-file-process'. "exec" does not work either.
7524 (if (memq system-type '(windows-nt)) " && exit || exit")))
7525
7526 ;; Send the command.
7527 (tramp-message vec 3 "Sending command `%s'" command)
7528 (tramp-send-command vec command t t)
7529 (tramp-process-actions p vec tramp-actions-before-shell 60)
7530 (tramp-message vec 3 "Found remote shell prompt on `%s'" l-host))
7531 ;; Next hop.
7532 (setq target-alist (cdr target-alist)))
7533
7534 ;; Make initial shell settings.
7535 (tramp-open-connection-setup-interactive-shell p vec))))))
7536
7537 (defun tramp-send-command (vec command &optional neveropen nooutput)
7538 "Send the COMMAND to connection VEC.
7539 Erases temporary buffer before sending the command. If optional
7540 arg NEVEROPEN is non-nil, never try to open the connection. This
7541 is meant to be used from `tramp-maybe-open-connection' only. The
7542 function waits for output unless NOOUTPUT is set."
7543 (unless neveropen (tramp-maybe-open-connection vec))
7544 (let ((p (tramp-get-connection-process vec)))
7545 (when (tramp-get-connection-property p "remote-echo" nil)
7546 ;; We mark the command string that it can be erased in the output buffer.
7547 (tramp-set-connection-property p "check-remote-echo" t)
7548 (setq command (format "%s%s%s" tramp-echo-mark command tramp-echo-mark)))
7549 (tramp-message vec 6 "%s" command)
7550 (tramp-send-string vec command)
7551 (unless nooutput (tramp-wait-for-output p))))
7552
7553 (defun tramp-wait-for-output (proc &optional timeout)
7554 "Wait for output from remote command."
7555 (unless (buffer-live-p (process-buffer proc))
7556 (delete-process proc)
7557 (tramp-error proc 'file-error "Process `%s' not available, try again" proc))
7558 (with-current-buffer (process-buffer proc)
7559 (let* (;; Initially, `tramp-end-of-output' is "#$ ". There might
7560 ;; be leading escape sequences, which must be ignored.
7561 (regexp (format "[^#$\n]*%s\r?$" (regexp-quote tramp-end-of-output)))
7562 ;; Sometimes, the commands do not return a newline but a
7563 ;; null byte before the shell prompt, for example "git
7564 ;; ls-files -c -z ...".
7565 (regexp1 (format "\\(^\\|\000\\)%s" regexp))
7566 (found (tramp-wait-for-regexp proc timeout regexp1)))
7567 (if found
7568 (let (buffer-read-only)
7569 ;; A simple-minded busybox has sent " ^H" sequences.
7570 ;; Delete them.
7571 (goto-char (point-min))
7572 (when (re-search-forward
7573 "^\\(.\b\\)+$" (tramp-compat-line-end-position) t)
7574 (forward-line 1)
7575 (delete-region (point-min) (point)))
7576 ;; Delete the prompt.
7577 (goto-char (point-max))
7578 (re-search-backward regexp nil t)
7579 (delete-region (point) (point-max)))
7580 (if timeout
7581 (tramp-error
7582 proc 'file-error
7583 "[[Remote prompt `%s' not found in %d secs]]"
7584 tramp-end-of-output timeout)
7585 (tramp-error
7586 proc 'file-error
7587 "[[Remote prompt `%s' not found]]" tramp-end-of-output)))
7588 ;; Return value is whether end-of-output sentinel was found.
7589 found)))
7590
7591 (defun tramp-send-command-and-check
7592 (vec command &optional subshell dont-suppress-err)
7593 "Run COMMAND and check its exit status.
7594 Sends `echo $?' along with the COMMAND for checking the exit status. If
7595 COMMAND is nil, just sends `echo $?'. Returns the exit status found.
7596
7597 If the optional argument SUBSHELL is non-nil, the command is
7598 executed in a subshell, ie surrounded by parentheses. If
7599 DONT-SUPPRESS-ERR is non-nil, stderr won't be sent to /dev/null."
7600 (tramp-send-command
7601 vec
7602 (concat (if subshell "( " "")
7603 command
7604 (if command (if dont-suppress-err "; " " 2>/dev/null; ") "")
7605 "echo tramp_exit_status $?"
7606 (if subshell " )" "")))
7607 (with-current-buffer (tramp-get-connection-buffer vec)
7608 (goto-char (point-max))
7609 (unless (re-search-backward "tramp_exit_status [0-9]+" nil t)
7610 (tramp-error
7611 vec 'file-error "Couldn't find exit status of `%s'" command))
7612 (skip-chars-forward "^ ")
7613 (prog1
7614 (read (current-buffer))
7615 (let (buffer-read-only) (delete-region (match-beginning 0) (point-max))))))
7616
7617 (defun tramp-barf-unless-okay (vec command fmt &rest args)
7618 "Run COMMAND, check exit status, throw error if exit status not okay.
7619 Similar to `tramp-send-command-and-check' but accepts two more arguments
7620 FMT and ARGS which are passed to `error'."
7621 (unless (zerop (tramp-send-command-and-check vec command))
7622 (apply 'tramp-error vec 'file-error fmt args)))
7623
7624 (defun tramp-send-command-and-read (vec command)
7625 "Run COMMAND and return the output, which must be a Lisp expression.
7626 In case there is no valid Lisp expression, it raises an error"
7627 (tramp-barf-unless-okay vec command "`%s' returns with error" command)
7628 (with-current-buffer (tramp-get-connection-buffer vec)
7629 ;; Read the expression.
7630 (goto-char (point-min))
7631 (condition-case nil
7632 (prog1 (read (current-buffer))
7633 ;; Error handling.
7634 (when (re-search-forward "\\S-" (tramp-compat-line-end-position) t)
7635 (error nil)))
7636 (error (tramp-error
7637 vec 'file-error
7638 "`%s' does not return a valid Lisp expression: `%s'"
7639 command (buffer-string))))))
7640
7641 ;; It seems that Tru64 Unix does not like it if long strings are sent
7642 ;; to it in one go. (This happens when sending the Perl
7643 ;; `file-attributes' implementation, for instance.) Therefore, we
7644 ;; have this function which sends the string in chunks.
7645 (defun tramp-send-string (vec string)
7646 "Send the STRING via connection VEC.
7647
7648 The STRING is expected to use Unix line-endings, but the lines sent to
7649 the remote host use line-endings as defined in the variable
7650 `tramp-rsh-end-of-line'. The communication buffer is erased before sending."
7651 (let* ((p (tramp-get-connection-process vec))
7652 (chunksize (tramp-get-connection-property p "chunksize" nil)))
7653 (unless p
7654 (tramp-error
7655 vec 'file-error "Can't send string to remote host -- not logged in"))
7656 (tramp-set-connection-property p "last-cmd-time" (current-time))
7657 (tramp-message vec 10 "%s" string)
7658 (with-current-buffer (tramp-get-connection-buffer vec)
7659 ;; Clean up the buffer. We cannot call `erase-buffer' because
7660 ;; narrowing might be in effect.
7661 (let (buffer-read-only) (delete-region (point-min) (point-max)))
7662 ;; Replace "\n" by `tramp-rsh-end-of-line'.
7663 (setq string
7664 (mapconcat 'identity
7665 (tramp-compat-split-string string "\n")
7666 tramp-rsh-end-of-line))
7667 (unless (or (string= string "")
7668 (string-equal (substring string -1) tramp-rsh-end-of-line))
7669 (setq string (concat string tramp-rsh-end-of-line)))
7670 ;; Send the string.
7671 (if (and chunksize (not (zerop chunksize)))
7672 (let ((pos 0)
7673 (end (length string)))
7674 (while (< pos end)
7675 (tramp-message
7676 vec 10 "Sending chunk from %s to %s"
7677 pos (min (+ pos chunksize) end))
7678 (process-send-string
7679 p (substring string pos (min (+ pos chunksize) end)))
7680 (setq pos (+ pos chunksize))))
7681 (process-send-string p string)))))
7682
7683 (defun tramp-mode-string-to-int (mode-string)
7684 "Converts a ten-letter `drwxrwxrwx'-style mode string into mode bits."
7685 (let* (case-fold-search
7686 (mode-chars (string-to-vector mode-string))
7687 (owner-read (aref mode-chars 1))
7688 (owner-write (aref mode-chars 2))
7689 (owner-execute-or-setid (aref mode-chars 3))
7690 (group-read (aref mode-chars 4))
7691 (group-write (aref mode-chars 5))
7692 (group-execute-or-setid (aref mode-chars 6))
7693 (other-read (aref mode-chars 7))
7694 (other-write (aref mode-chars 8))
7695 (other-execute-or-sticky (aref mode-chars 9)))
7696 (save-match-data
7697 (logior
7698 (cond
7699 ((char-equal owner-read ?r) (tramp-octal-to-decimal "00400"))
7700 ((char-equal owner-read ?-) 0)
7701 (t (error "Second char `%c' must be one of `r-'" owner-read)))
7702 (cond
7703 ((char-equal owner-write ?w) (tramp-octal-to-decimal "00200"))
7704 ((char-equal owner-write ?-) 0)
7705 (t (error "Third char `%c' must be one of `w-'" owner-write)))
7706 (cond
7707 ((char-equal owner-execute-or-setid ?x)
7708 (tramp-octal-to-decimal "00100"))
7709 ((char-equal owner-execute-or-setid ?S)
7710 (tramp-octal-to-decimal "04000"))
7711 ((char-equal owner-execute-or-setid ?s)
7712 (tramp-octal-to-decimal "04100"))
7713 ((char-equal owner-execute-or-setid ?-) 0)
7714 (t (error "Fourth char `%c' must be one of `xsS-'"
7715 owner-execute-or-setid)))
7716 (cond
7717 ((char-equal group-read ?r) (tramp-octal-to-decimal "00040"))
7718 ((char-equal group-read ?-) 0)
7719 (t (error "Fifth char `%c' must be one of `r-'" group-read)))
7720 (cond
7721 ((char-equal group-write ?w) (tramp-octal-to-decimal "00020"))
7722 ((char-equal group-write ?-) 0)
7723 (t (error "Sixth char `%c' must be one of `w-'" group-write)))
7724 (cond
7725 ((char-equal group-execute-or-setid ?x)
7726 (tramp-octal-to-decimal "00010"))
7727 ((char-equal group-execute-or-setid ?S)
7728 (tramp-octal-to-decimal "02000"))
7729 ((char-equal group-execute-or-setid ?s)
7730 (tramp-octal-to-decimal "02010"))
7731 ((char-equal group-execute-or-setid ?-) 0)
7732 (t (error "Seventh char `%c' must be one of `xsS-'"
7733 group-execute-or-setid)))
7734 (cond
7735 ((char-equal other-read ?r)
7736 (tramp-octal-to-decimal "00004"))
7737 ((char-equal other-read ?-) 0)
7738 (t (error "Eighth char `%c' must be one of `r-'" other-read)))
7739 (cond
7740 ((char-equal other-write ?w) (tramp-octal-to-decimal "00002"))
7741 ((char-equal other-write ?-) 0)
7742 (t (error "Nineth char `%c' must be one of `w-'" other-write)))
7743 (cond
7744 ((char-equal other-execute-or-sticky ?x)
7745 (tramp-octal-to-decimal "00001"))
7746 ((char-equal other-execute-or-sticky ?T)
7747 (tramp-octal-to-decimal "01000"))
7748 ((char-equal other-execute-or-sticky ?t)
7749 (tramp-octal-to-decimal "01001"))
7750 ((char-equal other-execute-or-sticky ?-) 0)
7751 (t (error "Tenth char `%c' must be one of `xtT-'"
7752 other-execute-or-sticky)))))))
7753
7754 (defun tramp-convert-file-attributes (vec attr)
7755 "Convert file-attributes ATTR generated by perl script, stat or ls.
7756 Convert file mode bits to string and set virtual device number.
7757 Return ATTR."
7758 (when attr
7759 ;; Convert last access time.
7760 (unless (listp (nth 4 attr))
7761 (setcar (nthcdr 4 attr)
7762 (list (floor (nth 4 attr) 65536)
7763 (floor (mod (nth 4 attr) 65536)))))
7764 ;; Convert last modification time.
7765 (unless (listp (nth 5 attr))
7766 (setcar (nthcdr 5 attr)
7767 (list (floor (nth 5 attr) 65536)
7768 (floor (mod (nth 5 attr) 65536)))))
7769 ;; Convert last status change time.
7770 (unless (listp (nth 6 attr))
7771 (setcar (nthcdr 6 attr)
7772 (list (floor (nth 6 attr) 65536)
7773 (floor (mod (nth 6 attr) 65536)))))
7774 ;; Convert file size.
7775 (when (< (nth 7 attr) 0)
7776 (setcar (nthcdr 7 attr) -1))
7777 (when (and (floatp (nth 7 attr))
7778 (<= (nth 7 attr) (tramp-compat-most-positive-fixnum)))
7779 (setcar (nthcdr 7 attr) (round (nth 7 attr))))
7780 ;; Convert file mode bits to string.
7781 (unless (stringp (nth 8 attr))
7782 (setcar (nthcdr 8 attr) (tramp-file-mode-from-int (nth 8 attr)))
7783 (when (stringp (car attr))
7784 (aset (nth 8 attr) 0 ?l)))
7785 ;; Convert directory indication bit.
7786 (when (string-match "^d" (nth 8 attr))
7787 (setcar attr t))
7788 ;; Convert symlink from `tramp-do-file-attributes-with-stat'.
7789 (when (consp (car attr))
7790 (if (and (stringp (caar attr))
7791 (string-match ".+ -> .\\(.+\\)." (caar attr)))
7792 (setcar attr (match-string 1 (caar attr)))
7793 (setcar attr nil)))
7794 ;; Set file's gid change bit.
7795 (setcar (nthcdr 9 attr)
7796 (if (numberp (nth 3 attr))
7797 (not (= (nth 3 attr)
7798 (tramp-get-remote-gid vec 'integer)))
7799 (not (string-equal
7800 (nth 3 attr)
7801 (tramp-get-remote-gid vec 'string)))))
7802 ;; Convert inode.
7803 (unless (listp (nth 10 attr))
7804 (setcar (nthcdr 10 attr)
7805 (condition-case nil
7806 (cons (floor (nth 10 attr) 65536)
7807 (floor (mod (nth 10 attr) 65536)))
7808 ;; Inodes can be incredible huge. We must hide this.
7809 (error (tramp-get-inode vec)))))
7810 ;; Set virtual device number.
7811 (setcar (nthcdr 11 attr)
7812 (tramp-get-device vec))
7813 attr))
7814
7815 (defun tramp-check-cached-permissions (vec access)
7816 "Check `file-attributes' caches for VEC.
7817 Return t if according to the cache access type ACCESS is known to
7818 be granted."
7819 (let ((result nil)
7820 (offset (cond
7821 ((eq ?r access) 1)
7822 ((eq ?w access) 2)
7823 ((eq ?x access) 3))))
7824 (dolist (suffix '("string" "integer") result)
7825 (setq
7826 result
7827 (or
7828 result
7829 (let ((file-attr
7830 (tramp-get-file-property
7831 vec (tramp-file-name-localname vec)
7832 (concat "file-attributes-" suffix) nil))
7833 (remote-uid
7834 (tramp-get-connection-property
7835 vec (concat "uid-" suffix) nil))
7836 (remote-gid
7837 (tramp-get-connection-property
7838 vec (concat "gid-" suffix) nil)))
7839 (and
7840 file-attr
7841 (or
7842 ;; Not a symlink
7843 (eq t (car file-attr))
7844 (null (car file-attr)))
7845 (or
7846 ;; World accessible.
7847 (eq access (aref (nth 8 file-attr) (+ offset 6)))
7848 ;; User accessible and owned by user.
7849 (and
7850 (eq access (aref (nth 8 file-attr) offset))
7851 (equal remote-uid (nth 2 file-attr)))
7852 ;; Group accessible and owned by user's
7853 ;; principal group.
7854 (and
7855 (eq access (aref (nth 8 file-attr) (+ offset 3)))
7856 (equal remote-gid (nth 3 file-attr)))))))))))
7857
7858 (defun tramp-get-inode (vec)
7859 "Returns the virtual inode number.
7860 If it doesn't exist, generate a new one."
7861 (let ((string (tramp-make-tramp-file-name
7862 (tramp-file-name-method vec)
7863 (tramp-file-name-user vec)
7864 (tramp-file-name-host vec)
7865 "")))
7866 (unless (assoc string tramp-inodes)
7867 (add-to-list 'tramp-inodes
7868 (list string (length tramp-inodes))))
7869 (nth 1 (assoc string tramp-inodes))))
7870
7871 (defun tramp-get-device (vec)
7872 "Returns the virtual device number.
7873 If it doesn't exist, generate a new one."
7874 (let ((string (tramp-make-tramp-file-name
7875 (tramp-file-name-method vec)
7876 (tramp-file-name-user vec)
7877 (tramp-file-name-host vec)
7878 "")))
7879 (unless (assoc string tramp-devices)
7880 (add-to-list 'tramp-devices
7881 (list string (length tramp-devices))))
7882 (cons -1 (nth 1 (assoc string tramp-devices)))))
7883
7884 (defun tramp-file-mode-from-int (mode)
7885 "Turn an integer representing a file mode into an ls(1)-like string."
7886 (let ((type (cdr (assoc (logand (lsh mode -12) 15) tramp-file-mode-type-map)))
7887 (user (logand (lsh mode -6) 7))
7888 (group (logand (lsh mode -3) 7))
7889 (other (logand (lsh mode -0) 7))
7890 (suid (> (logand (lsh mode -9) 4) 0))
7891 (sgid (> (logand (lsh mode -9) 2) 0))
7892 (sticky (> (logand (lsh mode -9) 1) 0)))
7893 (setq user (tramp-file-mode-permissions user suid "s"))
7894 (setq group (tramp-file-mode-permissions group sgid "s"))
7895 (setq other (tramp-file-mode-permissions other sticky "t"))
7896 (concat type user group other)))
7897
7898 (defun tramp-file-mode-permissions (perm suid suid-text)
7899 "Convert a permission bitset into a string.
7900 This is used internally by `tramp-file-mode-from-int'."
7901 (let ((r (> (logand perm 4) 0))
7902 (w (> (logand perm 2) 0))
7903 (x (> (logand perm 1) 0)))
7904 (concat (or (and r "r") "-")
7905 (or (and w "w") "-")
7906 (or (and suid x suid-text) ; suid, execute
7907 (and suid (upcase suid-text)) ; suid, !execute
7908 (and x "x") "-")))) ; !suid
7909
7910 (defun tramp-decimal-to-octal (i)
7911 "Return a string consisting of the octal digits of I.
7912 Not actually used. Use `(format \"%o\" i)' instead?"
7913 (cond ((< i 0) (error "Cannot convert negative number to octal"))
7914 ((not (integerp i)) (error "Cannot convert non-integer to octal"))
7915 ((zerop i) "0")
7916 (t (concat (tramp-decimal-to-octal (/ i 8))
7917 (number-to-string (% i 8))))))
7918
7919 ;; Kudos to Gerd Moellmann for this suggestion.
7920 (defun tramp-octal-to-decimal (ostr)
7921 "Given a string of octal digits, return a decimal number."
7922 (let ((x (or ostr "")))
7923 ;; `save-match' is in `tramp-mode-string-to-int' which calls this.
7924 (unless (string-match "\\`[0-7]*\\'" x)
7925 (error "Non-octal junk in string `%s'" x))
7926 (string-to-number ostr 8)))
7927
7928 (defun tramp-shell-case-fold (string)
7929 "Converts STRING to shell glob pattern which ignores case."
7930 (mapconcat
7931 (lambda (c)
7932 (if (equal (downcase c) (upcase c))
7933 (vector c)
7934 (format "[%c%c]" (downcase c) (upcase c))))
7935 string
7936 ""))
7937
7938
7939 ;; ------------------------------------------------------------
7940 ;; -- Tramp file names --
7941 ;; ------------------------------------------------------------
7942 ;; Conversion functions between external representation and
7943 ;; internal data structure. Convenience functions for internal
7944 ;; data structure.
7945
7946 (defun tramp-file-name-p (vec)
7947 "Check, whether VEC is a Tramp object."
7948 (and (vectorp vec) (= 4 (length vec))))
7949
7950 (defun tramp-file-name-method (vec)
7951 "Return method component of VEC."
7952 (and (tramp-file-name-p vec) (aref vec 0)))
7953
7954 (defun tramp-file-name-user (vec)
7955 "Return user component of VEC."
7956 (and (tramp-file-name-p vec) (aref vec 1)))
7957
7958 (defun tramp-file-name-host (vec)
7959 "Return host component of VEC."
7960 (and (tramp-file-name-p vec) (aref vec 2)))
7961
7962 (defun tramp-file-name-localname (vec)
7963 "Return localname component of VEC."
7964 (and (tramp-file-name-p vec) (aref vec 3)))
7965
7966 ;; The user part of a Tramp file name vector can be of kind
7967 ;; "user%domain". Sometimes, we must extract these parts.
7968 (defun tramp-file-name-real-user (vec)
7969 "Return the user name of VEC without domain."
7970 (save-match-data
7971 (let ((user (tramp-file-name-user vec)))
7972 (if (and (stringp user)
7973 (string-match tramp-user-with-domain-regexp user))
7974 (match-string 1 user)
7975 user))))
7976
7977 (defun tramp-file-name-domain (vec)
7978 "Return the domain name of VEC."
7979 (save-match-data
7980 (let ((user (tramp-file-name-user vec)))
7981 (and (stringp user)
7982 (string-match tramp-user-with-domain-regexp user)
7983 (match-string 2 user)))))
7984
7985 ;; The host part of a Tramp file name vector can be of kind
7986 ;; "host#port". Sometimes, we must extract these parts.
7987 (defun tramp-file-name-real-host (vec)
7988 "Return the host name of VEC without port."
7989 (save-match-data
7990 (let ((host (tramp-file-name-host vec)))
7991 (if (and (stringp host)
7992 (string-match tramp-host-with-port-regexp host))
7993 (match-string 1 host)
7994 host))))
7995
7996 (defun tramp-file-name-port (vec)
7997 "Return the port number of VEC."
7998 (save-match-data
7999 (let ((host (tramp-file-name-host vec)))
8000 (and (stringp host)
8001 (string-match tramp-host-with-port-regexp host)
8002 (string-to-number (match-string 2 host))))))
8003
8004 (defun tramp-tramp-file-p (name)
8005 "Return t if NAME is a string with Tramp file name syntax."
8006 (save-match-data
8007 (and (stringp name) (string-match tramp-file-name-regexp name))))
8008
8009 (defun tramp-find-method (method user host)
8010 "Return the right method string to use.
8011 This is METHOD, if non-nil. Otherwise, do a lookup in
8012 `tramp-default-method-alist'."
8013 (or method
8014 (let ((choices tramp-default-method-alist)
8015 lmethod item)
8016 (while choices
8017 (setq item (pop choices))
8018 (when (and (string-match (or (nth 0 item) "") (or host ""))
8019 (string-match (or (nth 1 item) "") (or user "")))
8020 (setq lmethod (nth 2 item))
8021 (setq choices nil)))
8022 lmethod)
8023 tramp-default-method))
8024
8025 (defun tramp-find-user (method user host)
8026 "Return the right user string to use.
8027 This is USER, if non-nil. Otherwise, do a lookup in
8028 `tramp-default-user-alist'."
8029 (or user
8030 (let ((choices tramp-default-user-alist)
8031 luser item)
8032 (while choices
8033 (setq item (pop choices))
8034 (when (and (string-match (or (nth 0 item) "") (or method ""))
8035 (string-match (or (nth 1 item) "") (or host "")))
8036 (setq luser (nth 2 item))
8037 (setq choices nil)))
8038 luser)
8039 tramp-default-user))
8040
8041 (defun tramp-find-host (method user host)
8042 "Return the right host string to use.
8043 This is HOST, if non-nil. Otherwise, it is `tramp-default-host'."
8044 (or (and (> (length host) 0) host)
8045 tramp-default-host))
8046
8047 (defun tramp-dissect-file-name (name &optional nodefault)
8048 "Return a `tramp-file-name' structure.
8049 The structure consists of remote method, remote user, remote host
8050 and localname (file name on remote host). If NODEFAULT is
8051 non-nil, the file name parts are not expanded to their default
8052 values."
8053 (save-match-data
8054 (let ((match (string-match (nth 0 tramp-file-name-structure) name)))
8055 (unless match (error "Not a Tramp file name: %s" name))
8056 (let ((method (match-string (nth 1 tramp-file-name-structure) name))
8057 (user (match-string (nth 2 tramp-file-name-structure) name))
8058 (host (match-string (nth 3 tramp-file-name-structure) name))
8059 (localname (match-string (nth 4 tramp-file-name-structure) name)))
8060 (when (member method '("multi" "multiu"))
8061 (error
8062 "`%s' method is no longer supported, see (info \"(tramp)Multi-hops\")"
8063 method))
8064 (when host
8065 (when (string-match tramp-prefix-ipv6-regexp host)
8066 (setq host (replace-match "" nil t host)))
8067 (when (string-match tramp-postfix-ipv6-regexp host)
8068 (setq host (replace-match "" nil t host))))
8069 (if nodefault
8070 (vector method user host localname)
8071 (vector
8072 (tramp-find-method method user host)
8073 (tramp-find-user method user host)
8074 (tramp-find-host method user host)
8075 localname))))))
8076
8077 (defun tramp-equal-remote (file1 file2)
8078 "Check, whether the remote parts of FILE1 and FILE2 are identical.
8079 The check depends on method, user and host name of the files. If
8080 one of the components is missing, the default values are used.
8081 The local file name parts of FILE1 and FILE2 are not taken into
8082 account.
8083
8084 Example:
8085
8086 (tramp-equal-remote \"/ssh::/etc\" \"/<your host name>:/home\")
8087
8088 would yield `t'. On the other hand, the following check results in nil:
8089
8090 (tramp-equal-remote \"/sudo::/etc\" \"/su::/etc\")"
8091 (and (stringp (file-remote-p file1))
8092 (stringp (file-remote-p file2))
8093 (string-equal (file-remote-p file1) (file-remote-p file2))))
8094
8095 (defun tramp-make-tramp-file-name (method user host localname)
8096 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME."
8097 (concat tramp-prefix-format
8098 (when (not (zerop (length method)))
8099 (concat method tramp-postfix-method-format))
8100 (when (not (zerop (length user)))
8101 (concat user tramp-postfix-user-format))
8102 (when host
8103 (if (string-match tramp-ipv6-regexp host)
8104 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
8105 host))
8106 tramp-postfix-host-format
8107 (when localname localname)))
8108
8109 (defun tramp-completion-make-tramp-file-name (method user host localname)
8110 "Constructs a Tramp file name from METHOD, USER, HOST and LOCALNAME.
8111 It must not be a complete Tramp file name, but as long as there are
8112 necessary only. This function will be used in file name completion."
8113 (concat tramp-prefix-format
8114 (when (not (zerop (length method)))
8115 (concat method tramp-postfix-method-format))
8116 (when (not (zerop (length user)))
8117 (concat user tramp-postfix-user-format))
8118 (when (not (zerop (length host)))
8119 (concat
8120 (if (string-match tramp-ipv6-regexp host)
8121 (concat tramp-prefix-ipv6-format host tramp-postfix-ipv6-format)
8122 host)
8123 tramp-postfix-host-format))
8124 (when localname localname)))
8125
8126 (defun tramp-make-copy-program-file-name (vec)
8127 "Create a file name suitable to be passed to `rcp' and workalikes."
8128 (let ((user (tramp-file-name-user vec))
8129 (host (tramp-file-name-real-host vec))
8130 (localname (tramp-shell-quote-argument
8131 (tramp-file-name-localname vec))))
8132 (if (not (zerop (length user)))
8133 (format "%s@%s:%s" user host localname)
8134 (format "%s:%s" host localname))))
8135
8136 (defun tramp-method-out-of-band-p (vec size)
8137 "Return t if this is an out-of-band method, nil otherwise."
8138 (and
8139 ;; It shall be an out-of-band method.
8140 (tramp-get-method-parameter (tramp-file-name-method vec) 'tramp-copy-program)
8141 ;; Either the file size is large enough, or (in rare cases) there
8142 ;; does not exist a remote encoding.
8143 (or (null tramp-copy-size-limit)
8144 (> size tramp-copy-size-limit)
8145 (null (tramp-get-inline-coding vec "remote-encoding" size)))))
8146
8147 (defun tramp-local-host-p (vec)
8148 "Return t if this points to the local host, nil otherwise."
8149 ;; We cannot use `tramp-file-name-real-host'. A port is an
8150 ;; indication for an ssh tunnel or alike.
8151 (let ((host (tramp-file-name-host vec)))
8152 (and
8153 (stringp host)
8154 (string-match tramp-local-host-regexp host)
8155 ;; The method shall be applied to one of the shell file name
8156 ;; handler. `tramp-local-host-p' is also called for "smb" and
8157 ;; alike, where it must fail.
8158 (tramp-get-method-parameter
8159 (tramp-file-name-method vec) 'tramp-login-program)
8160 ;; The local temp directory must be writable for the other user.
8161 (file-writable-p
8162 (tramp-make-tramp-file-name
8163 (tramp-file-name-method vec)
8164 (tramp-file-name-user vec)
8165 host
8166 (tramp-compat-temporary-file-directory)))
8167 ;; On some systems, chown runs only for root.
8168 (or (zerop (user-uid))
8169 (zerop (tramp-get-remote-uid vec 'integer))))))
8170
8171 ;; Variables local to connection.
8172
8173 (defun tramp-get-remote-path (vec)
8174 (with-connection-property
8175 ;; When `tramp-own-remote-path' is in `tramp-remote-path', we
8176 ;; cache the result for the session only. Otherwise, the result
8177 ;; is cached persistently.
8178 (if (memq 'tramp-own-remote-path tramp-remote-path)
8179 (tramp-get-connection-process vec)
8180 vec)
8181 "remote-path"
8182 (let* ((remote-path (copy-tree tramp-remote-path))
8183 (elt1 (memq 'tramp-default-remote-path remote-path))
8184 (elt2 (memq 'tramp-own-remote-path remote-path))
8185 (default-remote-path
8186 (when elt1
8187 (condition-case nil
8188 (tramp-send-command-and-read
8189 vec "echo \\\"`getconf PATH`\\\"")
8190 ;; Default if "getconf" is not available.
8191 (error
8192 (tramp-message
8193 vec 3
8194 "`getconf PATH' not successful, using default value \"%s\"."
8195 "/bin:/usr/bin")
8196 "/bin:/usr/bin"))))
8197 (own-remote-path
8198 (when elt2
8199 (condition-case nil
8200 (tramp-send-command-and-read vec "echo \\\"$PATH\\\"")
8201 ;; Default if "getconf" is not available.
8202 (error
8203 (tramp-message
8204 vec 3 "$PATH not set, ignoring `tramp-own-remote-path'.")
8205 nil)))))
8206
8207 ;; Replace place holder `tramp-default-remote-path'.
8208 (when elt1
8209 (setcdr elt1
8210 (append
8211 (tramp-compat-split-string default-remote-path ":")
8212 (cdr elt1)))
8213 (setq remote-path (delq 'tramp-default-remote-path remote-path)))
8214
8215 ;; Replace place holder `tramp-own-remote-path'.
8216 (when elt2
8217 (setcdr elt2
8218 (append
8219 (tramp-compat-split-string own-remote-path ":")
8220 (cdr elt2)))
8221 (setq remote-path (delq 'tramp-own-remote-path remote-path)))
8222
8223 ;; Remove double entries.
8224 (setq elt1 remote-path)
8225 (while (consp elt1)
8226 (while (and (car elt1) (setq elt2 (member (car elt1) (cdr elt1))))
8227 (setcar elt2 nil))
8228 (setq elt1 (cdr elt1)))
8229
8230 ;; Remove non-existing directories.
8231 (delq
8232 nil
8233 (mapcar
8234 (lambda (x)
8235 (and
8236 (stringp x)
8237 (file-directory-p
8238 (tramp-make-tramp-file-name
8239 (tramp-file-name-method vec)
8240 (tramp-file-name-user vec)
8241 (tramp-file-name-host vec)
8242 x))
8243 x))
8244 remote-path)))))
8245
8246 (defun tramp-get-remote-tmpdir (vec)
8247 (with-connection-property vec "tmp-directory"
8248 (let ((dir (tramp-shell-quote-argument "/tmp")))
8249 (if (and (zerop
8250 (tramp-send-command-and-check
8251 vec (format "%s -d %s" (tramp-get-test-command vec) dir)))
8252 (zerop
8253 (tramp-send-command-and-check
8254 vec (format "%s -w %s" (tramp-get-test-command vec) dir))))
8255 dir
8256 (tramp-error vec 'file-error "Directory %s not accessible" dir)))))
8257
8258 (defun tramp-get-ls-command (vec)
8259 (with-connection-property vec "ls"
8260 (tramp-message vec 5 "Finding a suitable `ls' command")
8261 (or
8262 (catch 'ls-found
8263 (dolist (cmd '("ls" "gnuls" "gls"))
8264 (let ((dl (tramp-get-remote-path vec))
8265 result)
8266 (while (and dl (setq result (tramp-find-executable vec cmd dl t t)))
8267 ;; Check parameters. On busybox, "ls" output coloring is
8268 ;; enabled by default sometimes. So we try to disable it
8269 ;; when possible. $LS_COLORING is not supported there.
8270 (when (zerop (tramp-send-command-and-check
8271 vec (format "%s -lnd /" result)))
8272 (when (zerop (tramp-send-command-and-check
8273 vec (format "%s --color=never /" result)))
8274 (setq result (concat result " --color=never")))
8275 (throw 'ls-found result))
8276 (setq dl (cdr dl))))))
8277 (tramp-error vec 'file-error "Couldn't find a proper `ls' command"))))
8278
8279 (defun tramp-get-ls-command-with-dired (vec)
8280 (save-match-data
8281 (with-connection-property vec "ls-dired"
8282 (tramp-message vec 5 "Checking, whether `ls --dired' works")
8283 (zerop (tramp-send-command-and-check
8284 vec (format "%s --dired /" (tramp-get-ls-command vec)))))))
8285
8286 (defun tramp-get-test-command (vec)
8287 (with-connection-property vec "test"
8288 (tramp-message vec 5 "Finding a suitable `test' command")
8289 (if (zerop (tramp-send-command-and-check vec "test 0"))
8290 "test"
8291 (tramp-find-executable vec "test" (tramp-get-remote-path vec)))))
8292
8293 (defun tramp-get-test-nt-command (vec)
8294 ;; Does `test A -nt B' work? Use abominable `find' construct if it
8295 ;; doesn't. BSD/OS 4.0 wants the parentheses around the command,
8296 ;; for otherwise the shell crashes.
8297 (with-connection-property vec "test-nt"
8298 (or
8299 (progn
8300 (tramp-send-command
8301 vec (format "( %s / -nt / )" (tramp-get-test-command vec)))
8302 (with-current-buffer (tramp-get-buffer vec)
8303 (goto-char (point-min))
8304 (when (looking-at (regexp-quote tramp-end-of-output))
8305 (format "%s %%s -nt %%s" (tramp-get-test-command vec)))))
8306 (progn
8307 (tramp-send-command
8308 vec
8309 (format
8310 "tramp_test_nt () {\n%s -n \"`find $1 -prune -newer $2 -print`\"\n}"
8311 (tramp-get-test-command vec)))
8312 "tramp_test_nt %s %s"))))
8313
8314 (defun tramp-get-file-exists-command (vec)
8315 (with-connection-property vec "file-exists"
8316 (tramp-message vec 5 "Finding command to check if file exists")
8317 (tramp-find-file-exists-command vec)))
8318
8319 (defun tramp-get-remote-ln (vec)
8320 (with-connection-property vec "ln"
8321 (tramp-message vec 5 "Finding a suitable `ln' command")
8322 (tramp-find-executable vec "ln" (tramp-get-remote-path vec))))
8323
8324 (defun tramp-get-remote-perl (vec)
8325 (with-connection-property vec "perl"
8326 (tramp-message vec 5 "Finding a suitable `perl' command")
8327 (let ((result
8328 (or (tramp-find-executable vec "perl5" (tramp-get-remote-path vec))
8329 (tramp-find-executable
8330 vec "perl" (tramp-get-remote-path vec)))))
8331 ;; We must check also for some Perl modules.
8332 (when result
8333 (with-connection-property vec "perl-file-spec"
8334 (zerop
8335 (tramp-send-command-and-check
8336 vec (format "%s -e 'use File::Spec;'" result))))
8337 (with-connection-property vec "perl-cwd-realpath"
8338 (zerop
8339 (tramp-send-command-and-check
8340 vec (format "%s -e 'use Cwd \"realpath\";'" result)))))
8341 result)))
8342
8343 (defun tramp-get-remote-stat (vec)
8344 (with-connection-property vec "stat"
8345 (tramp-message vec 5 "Finding a suitable `stat' command")
8346 (let ((result (tramp-find-executable
8347 vec "stat" (tramp-get-remote-path vec)))
8348 tmp)
8349 ;; Check whether stat(1) returns usable syntax. %s does not
8350 ;; work on older AIX systems.
8351 (when result
8352 (setq tmp
8353 ;; We don't want to display an error message.
8354 (with-temp-message (or (current-message) "")
8355 (condition-case nil
8356 (tramp-send-command-and-read
8357 vec (format "%s -c '(\"%%N\" %%s)' /" result))
8358 (error nil))))
8359 (unless (and (listp tmp) (stringp (car tmp))
8360 (string-match "^./.$" (car tmp))
8361 (integerp (cadr tmp)))
8362 (setq result nil)))
8363 result)))
8364
8365 (defun tramp-get-remote-readlink (vec)
8366 (with-connection-property vec "readlink"
8367 (tramp-message vec 5 "Finding a suitable `readlink' command")
8368 (let ((result (tramp-find-executable
8369 vec "readlink" (tramp-get-remote-path vec))))
8370 (when (and result
8371 ;; We don't want to display an error message.
8372 (with-temp-message (or (current-message) "")
8373 (condition-case nil
8374 (zerop
8375 (tramp-send-command-and-check
8376 vec (format "%s --canonicalize-missing /" result)))
8377 (error nil))))
8378 result))))
8379
8380 (defun tramp-get-remote-id (vec)
8381 (with-connection-property vec "id"
8382 (tramp-message vec 5 "Finding POSIX `id' command")
8383 (or
8384 (catch 'id-found
8385 (let ((dl (tramp-get-remote-path vec))
8386 result)
8387 (while (and dl (setq result (tramp-find-executable vec "id" dl t t)))
8388 ;; Check POSIX parameter.
8389 (when (zerop (tramp-send-command-and-check
8390 vec (format "%s -u" result)))
8391 (throw 'id-found result))
8392 (setq dl (cdr dl)))))
8393 (tramp-error vec 'file-error "Couldn't find a POSIX `id' command"))))
8394
8395 (defun tramp-get-remote-uid (vec id-format)
8396 (with-connection-property vec (format "uid-%s" id-format)
8397 (let ((res (tramp-send-command-and-read
8398 vec
8399 (format "%s -u%s %s"
8400 (tramp-get-remote-id vec)
8401 (if (equal id-format 'integer) "" "n")
8402 (if (equal id-format 'integer)
8403 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8404 ;; The command might not always return a number.
8405 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
8406
8407 (defun tramp-get-remote-gid (vec id-format)
8408 (with-connection-property vec (format "gid-%s" id-format)
8409 (let ((res (tramp-send-command-and-read
8410 vec
8411 (format "%s -g%s %s"
8412 (tramp-get-remote-id vec)
8413 (if (equal id-format 'integer) "" "n")
8414 (if (equal id-format 'integer)
8415 "" "| sed -e s/^/\\\"/ -e s/\$/\\\"/")))))
8416 ;; The command might not always return a number.
8417 (if (and (equal id-format 'integer) (not (integerp res))) -1 res))))
8418
8419 (defun tramp-get-local-uid (id-format)
8420 (if (equal id-format 'integer) (user-uid) (user-login-name)))
8421
8422 (defun tramp-get-local-gid (id-format)
8423 (nth 3 (tramp-compat-file-attributes "~/" id-format)))
8424
8425 ;; Some predefined connection properties.
8426 (defun tramp-get-inline-compress (vec prop size)
8427 "Return the compress command related to PROP.
8428 PROP is either `inline-compress' or `inline-decompress'. SIZE is
8429 the length of the file to be compressed.
8430
8431 If no corresponding command is found, nil is returned."
8432 (when (and (integerp tramp-inline-compress-start-size)
8433 (> size tramp-inline-compress-start-size))
8434 (with-connection-property vec prop
8435 (tramp-find-inline-compress vec)
8436 (tramp-get-connection-property vec prop nil))))
8437
8438 (defun tramp-get-inline-coding (vec prop size)
8439 "Return the coding command related to PROP.
8440 PROP is either `remote-encoding', `remode-decoding',
8441 `local-encoding' or `local-decoding'.
8442
8443 SIZE is the length of the file to be coded. Depending on SIZE,
8444 compression might be applied.
8445
8446 If no corresponding command is found, nil is returned.
8447 Otherwise, either a string is returned which contains a `%s' mark
8448 to be used for the respective input or output file; or a Lisp
8449 function cell is returned to be applied on a buffer."
8450 (let ((coding
8451 (with-connection-property vec prop
8452 (tramp-find-inline-encoding vec)
8453 (tramp-get-connection-property vec prop nil)))
8454 (prop1 (if (string-match "encoding" prop)
8455 "inline-compress" "inline-decompress"))
8456 compress)
8457 ;; The connection property might have been cached. So we must send
8458 ;; the script to the remote side - maybe.
8459 (when (and coding (symbolp coding) (string-match "remote" prop))
8460 (let ((name (symbol-name coding)))
8461 (while (string-match (regexp-quote "-") name)
8462 (setq name (replace-match "_" nil t name)))
8463 (tramp-maybe-send-script vec (symbol-value coding) name)
8464 (setq coding name)))
8465 (when coding
8466 ;; Check for the `compress' command.
8467 (setq compress (tramp-get-inline-compress vec prop1 size))
8468 ;; Return the value.
8469 (cond
8470 ((and compress (symbolp coding))
8471 (if (string-match "decompress" prop1)
8472 `(lambda (beg end)
8473 (,coding beg end)
8474 (let ((coding-system-for-write 'binary)
8475 (coding-system-for-read 'binary))
8476 (apply
8477 'call-process-region (point-min) (point-max)
8478 (car (split-string ,compress)) t t nil
8479 (cdr (split-string ,compress)))))
8480 `(lambda (beg end)
8481 (let ((coding-system-for-write 'binary)
8482 (coding-system-for-read 'binary))
8483 (apply
8484 'call-process-region beg end
8485 (car (split-string ,compress)) t t nil
8486 (cdr (split-string ,compress))))
8487 (,coding (point-min) (point-max)))))
8488 ((symbolp coding)
8489 coding)
8490 ((and compress (string-match "decoding" prop))
8491 (format "(%s | %s >%%s)" coding compress))
8492 (compress
8493 (format "(%s <%%s | %s)" compress coding))
8494 ((string-match "decoding" prop)
8495 (format "%s >%%s" coding))
8496 (t
8497 (format "%s <%%s" coding))))))
8498
8499 (defun tramp-get-method-parameter (method param)
8500 "Return the method parameter PARAM.
8501 If the `tramp-methods' entry does not exist, return nil."
8502 (let ((entry (assoc param (assoc method tramp-methods))))
8503 (when entry (cadr entry))))
8504
8505 ;; Auto saving to a special directory.
8506
8507 (defun tramp-exists-file-name-handler (operation &rest args)
8508 "Check, whether OPERATION runs a file name handler."
8509 ;; The file name handler is determined on base of either an
8510 ;; argument, `buffer-file-name', or `default-directory'.
8511 (condition-case nil
8512 (let* ((buffer-file-name "/")
8513 (default-directory "/")
8514 (fnha file-name-handler-alist)
8515 (check-file-name-operation operation)
8516 (file-name-handler-alist
8517 (list
8518 (cons "/"
8519 (lambda (operation &rest args)
8520 "Returns OPERATION if it is the one to be checked."
8521 (if (equal check-file-name-operation operation)
8522 operation
8523 (let ((file-name-handler-alist fnha))
8524 (apply operation args))))))))
8525 (equal (apply operation args) operation))
8526 (error nil)))
8527
8528 (unless (tramp-exists-file-name-handler 'make-auto-save-file-name)
8529 (defadvice make-auto-save-file-name
8530 (around tramp-advice-make-auto-save-file-name () activate)
8531 "Invoke `tramp-handle-make-auto-save-file-name' for Tramp files."
8532 (if (tramp-tramp-file-p (buffer-file-name))
8533 ;; We cannot call `tramp-handle-make-auto-save-file-name'
8534 ;; directly, because this would bypass the locking mechanism.
8535 (setq ad-return-value
8536 (tramp-file-name-handler 'make-auto-save-file-name))
8537 ad-do-it))
8538 (add-hook
8539 'tramp-unload-hook
8540 (lambda ()
8541 (ad-remove-advice
8542 'make-auto-save-file-name
8543 'around 'tramp-advice-make-auto-save-file-name)
8544 (ad-activate 'make-auto-save-file-name))))
8545
8546 ;; In XEmacs < 21.5, autosaved remote files have permission 0666 minus
8547 ;; umask. This is a security threat.
8548
8549 (defun tramp-set-auto-save-file-modes ()
8550 "Set permissions of autosaved remote files to the original permissions."
8551 (let ((bfn (buffer-file-name)))
8552 (when (and (tramp-tramp-file-p bfn)
8553 (buffer-modified-p)
8554 (stringp buffer-auto-save-file-name)
8555 (not (equal bfn buffer-auto-save-file-name)))
8556 (unless (file-exists-p buffer-auto-save-file-name)
8557 (write-region "" nil buffer-auto-save-file-name))
8558 ;; Permissions should be set always, because there might be an old
8559 ;; auto-saved file belonging to another original file. This could
8560 ;; be a security threat.
8561 (set-file-modes buffer-auto-save-file-name
8562 (or (file-modes bfn) (tramp-octal-to-decimal "0600"))))))
8563
8564 (unless (and (featurep 'xemacs)
8565 (= emacs-major-version 21)
8566 (> emacs-minor-version 4))
8567 (add-hook 'auto-save-hook 'tramp-set-auto-save-file-modes)
8568 (add-hook 'tramp-unload-hook
8569 (lambda ()
8570 (remove-hook 'auto-save-hook 'tramp-set-auto-save-file-modes))))
8571
8572 (defun tramp-subst-strs-in-string (alist string)
8573 "Replace all occurrences of the string FROM with TO in STRING.
8574 ALIST is of the form ((FROM . TO) ...)."
8575 (save-match-data
8576 (while alist
8577 (let* ((pr (car alist))
8578 (from (car pr))
8579 (to (cdr pr)))
8580 (while (string-match (regexp-quote from) string)
8581 (setq string (replace-match to t t string)))
8582 (setq alist (cdr alist))))
8583 string))
8584
8585 ;; ------------------------------------------------------------
8586 ;; -- Compatibility functions section --
8587 ;; ------------------------------------------------------------
8588
8589 (defun tramp-read-passwd (proc &optional prompt)
8590 "Read a password from user (compat function).
8591 Consults the auth-source package.
8592 Invokes `password-read' if available, `read-passwd' else."
8593 (let* ((key (tramp-make-tramp-file-name
8594 tramp-current-method tramp-current-user
8595 tramp-current-host ""))
8596 (pw-prompt
8597 (or prompt
8598 (with-current-buffer (process-buffer proc)
8599 (tramp-check-for-regexp proc tramp-password-prompt-regexp)
8600 (format "%s for %s " (capitalize (match-string 1)) key)))))
8601 (with-parsed-tramp-file-name key nil
8602 (prog1
8603 (or
8604 ;; See if auth-sources contains something useful, if it's bound.
8605 (and (boundp 'auth-sources)
8606 (tramp-get-connection-property v "first-password-request" nil)
8607 ;; Try with Tramp's current method.
8608 (tramp-compat-funcall
8609 'auth-source-user-or-password
8610 "password" tramp-current-host tramp-current-method))
8611 ;; Try the password cache.
8612 (when (functionp 'password-read)
8613 (unless (tramp-get-connection-property
8614 v "first-password-request" nil)
8615 (tramp-compat-funcall 'password-cache-remove key))
8616 (let ((password
8617 (tramp-compat-funcall 'password-read pw-prompt key)))
8618 (tramp-compat-funcall 'password-cache-add key password)
8619 password))
8620 ;; Else, get the password interactively.
8621 (read-passwd pw-prompt))
8622 (tramp-set-connection-property v "first-password-request" nil)))))
8623
8624 (defun tramp-clear-passwd (vec)
8625 "Clear password cache for connection related to VEC."
8626 (tramp-compat-funcall
8627 'password-cache-remove
8628 (tramp-make-tramp-file-name
8629 (tramp-file-name-method vec)
8630 (tramp-file-name-user vec)
8631 (tramp-file-name-host vec)
8632 "")))
8633
8634 ;; Snarfed code from time-date.el and parse-time.el
8635
8636 (defconst tramp-half-a-year '(241 17024)
8637 "Evaluated by \"(days-to-time 183)\".")
8638
8639 (defconst tramp-parse-time-months
8640 '(("jan" . 1) ("feb" . 2) ("mar" . 3)
8641 ("apr" . 4) ("may" . 5) ("jun" . 6)
8642 ("jul" . 7) ("aug" . 8) ("sep" . 9)
8643 ("oct" . 10) ("nov" . 11) ("dec" . 12))
8644 "Alist mapping month names to integers.")
8645
8646 (defun tramp-time-less-p (t1 t2)
8647 "Say whether time value T1 is less than time value T2."
8648 (unless t1 (setq t1 '(0 0)))
8649 (unless t2 (setq t2 '(0 0)))
8650 (or (< (car t1) (car t2))
8651 (and (= (car t1) (car t2))
8652 (< (nth 1 t1) (nth 1 t2)))))
8653
8654 (defun tramp-time-subtract (t1 t2)
8655 "Subtract two time values.
8656 Return the difference in the format of a time value."
8657 (unless t1 (setq t1 '(0 0)))
8658 (unless t2 (setq t2 '(0 0)))
8659 (let ((borrow (< (cadr t1) (cadr t2))))
8660 (list (- (car t1) (car t2) (if borrow 1 0))
8661 (- (+ (if borrow 65536 0) (cadr t1)) (cadr t2)))))
8662
8663 (defun tramp-time-diff (t1 t2)
8664 "Return the difference between the two times, in seconds.
8665 T1 and T2 are time values (as returned by `current-time' for example)."
8666 ;; Pacify byte-compiler with `symbol-function'.
8667 (cond ((and (fboundp 'subtract-time)
8668 (fboundp 'float-time))
8669 (tramp-compat-funcall
8670 'float-time (tramp-compat-funcall 'subtract-time t1 t2)))
8671 ((and (fboundp 'subtract-time)
8672 (fboundp 'time-to-seconds))
8673 (tramp-compat-funcall
8674 'time-to-seconds (tramp-compat-funcall 'subtract-time t1 t2)))
8675 ((fboundp 'itimer-time-difference)
8676 (tramp-compat-funcall
8677 'itimer-time-difference
8678 (if (< (length t1) 3) (append t1 '(0)) t1)
8679 (if (< (length t2) 3) (append t2 '(0)) t2)))
8680 (t
8681 (let ((time (tramp-time-subtract t1 t2)))
8682 (+ (* (car time) 65536.0)
8683 (cadr time)
8684 (/ (or (nth 2 time) 0) 1000000.0))))))
8685
8686 (defun tramp-coding-system-change-eol-conversion (coding-system eol-type)
8687 "Return a coding system like CODING-SYSTEM but with given EOL-TYPE.
8688 EOL-TYPE can be one of `dos', `unix', or `mac'."
8689 (cond ((fboundp 'coding-system-change-eol-conversion)
8690 (tramp-compat-funcall
8691 'coding-system-change-eol-conversion coding-system eol-type))
8692 ((fboundp 'subsidiary-coding-system)
8693 (tramp-compat-funcall
8694 'subsidiary-coding-system coding-system
8695 (cond ((eq eol-type 'dos) 'crlf)
8696 ((eq eol-type 'unix) 'lf)
8697 ((eq eol-type 'mac) 'cr)
8698 (t
8699 (error "Unknown EOL-TYPE `%s', must be %s"
8700 eol-type
8701 "`dos', `unix', or `mac'")))))
8702 (t (error "Can't change EOL conversion -- is MULE missing?"))))
8703
8704 (defun tramp-set-process-query-on-exit-flag (process flag)
8705 "Specify if query is needed for process when Emacs is exited.
8706 If the second argument flag is non-nil, Emacs will query the user before
8707 exiting if process is running."
8708 (if (fboundp 'set-process-query-on-exit-flag)
8709 (tramp-compat-funcall 'set-process-query-on-exit-flag process flag)
8710 (tramp-compat-funcall 'process-kill-without-query) process flag))
8711
8712
8713 ;; ------------------------------------------------------------
8714 ;; -- Kludges section --
8715 ;; ------------------------------------------------------------
8716
8717 ;; Currently (as of Emacs 20.5), the function `shell-quote-argument'
8718 ;; does not deal well with newline characters. Newline is replaced by
8719 ;; backslash newline. But if, say, the string `a backslash newline b'
8720 ;; is passed to a shell, the shell will expand this into "ab",
8721 ;; completely omitting the newline. This is not what was intended.
8722 ;; It does not appear to be possible to make the function
8723 ;; `shell-quote-argument' work with newlines without making it
8724 ;; dependent on the shell used. But within this package, we know that
8725 ;; we will always use a Bourne-like shell, so we use an approach which
8726 ;; groks newlines.
8727 ;;
8728 ;; The approach is simple: we call `shell-quote-argument', then
8729 ;; massage the newline part of the result.
8730 ;;
8731 ;; This function should produce a string which is grokked by a Unix
8732 ;; shell, even if the Emacs is running on Windows. Since this is the
8733 ;; kludges section, we bind `system-type' in such a way that
8734 ;; `shell-quote-arguments' behaves as if on Unix.
8735 ;;
8736 ;; Thanks to Mario DeWeerd for the hint that it is sufficient for this
8737 ;; function to work with Bourne-like shells.
8738 ;;
8739 ;; CCC: This function should be rewritten so that
8740 ;; `shell-quote-argument' is not used. This way, we are safe from
8741 ;; changes in `shell-quote-argument'.
8742 (defun tramp-shell-quote-argument (s)
8743 "Similar to `shell-quote-argument', but groks newlines.
8744 Only works for Bourne-like shells."
8745 (let ((system-type 'not-windows))
8746 (save-match-data
8747 (let ((result (shell-quote-argument s))
8748 (nl (regexp-quote (format "\\%s" tramp-rsh-end-of-line))))
8749 (when (and (>= (length result) 2)
8750 (string= (substring result 0 2) "\\~"))
8751 (setq result (substring result 1)))
8752 (while (string-match nl result)
8753 (setq result (replace-match (format "'%s'" tramp-rsh-end-of-line)
8754 t t result)))
8755 result))))
8756
8757 ;; Checklist for `tramp-unload-hook'
8758 ;; - Unload all `tramp-*' packages
8759 ;; - Reset `file-name-handler-alist'
8760 ;; - Cleanup hooks where Tramp functions are in
8761 ;; - Cleanup advised functions
8762 ;; - Cleanup autoloads
8763 ;;;###autoload
8764 (defun tramp-unload-tramp ()
8765 "Discard Tramp from loading remote files."
8766 (interactive)
8767 ;; When Tramp is not loaded yet, its autoloads are still active.
8768 (tramp-unload-file-name-handlers)
8769 ;; ange-ftp settings must be enabled.
8770 (tramp-compat-funcall 'tramp-ftp-enable-ange-ftp)
8771 ;; Maybe its not loaded yet.
8772 (condition-case nil
8773 (unload-feature 'tramp 'force)
8774 (error nil)))
8775
8776 (when (and load-in-progress
8777 (string-match "Loading tramp..." (or (current-message) "")))
8778 (message "Loading tramp...done"))
8779
8780 (provide 'tramp)
8781
8782 ;;; TODO:
8783
8784 ;; * Handle nonlocal exits such as C-g.
8785 ;; * But it would probably be better to use with-local-quit at the
8786 ;; place where it's actually needed: around any potentially
8787 ;; indefinitely blocking piece of code. In this case it would be
8788 ;; within Tramp around one of its calls to accept-process-output (or
8789 ;; around one of the loops that calls accept-process-output)
8790 ;; (Stefan Monnier).
8791 ;; * Rewrite `tramp-shell-quote-argument' to abstain from using
8792 ;; `shell-quote-argument'.
8793 ;; * In Emacs 21, `insert-directory' shows total number of bytes used
8794 ;; by the files in that directory. Add this here.
8795 ;; * Avoid screen blanking when hitting `g' in dired. (Eli Tziperman)
8796 ;; * Make ffap.el grok Tramp filenames. (Eli Tziperman)
8797 ;; * Case-insensitive filename completion. (Norbert Goevert.)
8798 ;; * Don't use globbing for directories with many files, as this is
8799 ;; likely to produce long command lines, and some shells choke on
8800 ;; long command lines.
8801 ;; * How to deal with MULE in `insert-file-contents' and `write-region'?
8802 ;; * Test remote ksh or bash for tilde expansion in `tramp-find-shell'?
8803 ;; * abbreviate-file-name
8804 ;; * Better error checking. At least whenever we see something
8805 ;; strange when doing zerop, we should kill the process and start
8806 ;; again. (Greg Stark)
8807 ;; * Remove unneeded parameters from methods.
8808 ;; * Make it work for different encodings, and for different file name
8809 ;; encodings, too. (Daniel Pittman)
8810 ;; * Don't search for perl5 and perl. Instead, only search for perl and
8811 ;; then look if it's the right version (with `perl -v').
8812 ;; * When editing a remote CVS controlled file as a different user, VC
8813 ;; gets confused about the file locking status. Try to find out why
8814 ;; the workaround doesn't work.
8815 ;; * Username and hostname completion.
8816 ;; ** Try to avoid usage of `last-input-event' in `tramp-completion-mode-p'.
8817 ;; ** Unify `tramp-parse-{rhosts,shosts,sconfig,hosts,passwd,netrc}'.
8818 ;; Code is nearly identical.
8819 ;; * Allow out-of-band methods as _last_ multi-hop. Open a connection
8820 ;; until the last but one hop via `start-file-process'. Apply it
8821 ;; also for ftp and smb.
8822 ;; * WIBNI if we had a command "trampclient"? If I was editing in
8823 ;; some shell with root priviledges, it would be nice if I could
8824 ;; just call
8825 ;; trampclient filename.c
8826 ;; as an editor, and the _current_ shell would connect to an Emacs
8827 ;; server and would be used in an existing non-priviledged Emacs
8828 ;; session for doing the editing in question.
8829 ;; That way, I need not tell Emacs my password again and be afraid
8830 ;; that it makes it into core dumps or other ugly stuff (I had Emacs
8831 ;; once display a just typed password in the context of a keyboard
8832 ;; sequence prompt for a question immediately following in a shell
8833 ;; script run within Emacs -- nasty).
8834 ;; And if I have some ssh session running to a different computer,
8835 ;; having the possibility of passing a local file there to a local
8836 ;; Emacs session (in case I can arrange for a connection back) would
8837 ;; be nice.
8838 ;; Likely the corresponding Tramp server should not allow the
8839 ;; equivalent of the emacsclient -eval option in order to make this
8840 ;; reasonably unproblematic. And maybe trampclient should have some
8841 ;; way of passing credentials, like by using an SSL socket or
8842 ;; something. (David Kastrup)
8843 ;; * Reconnect directly to a compliant shell without first going
8844 ;; through the user's default shell. (Pete Forman)
8845 ;; * Make `tramp-default-user' obsolete.
8846 ;; * How can I interrupt the remote process with a signal
8847 ;; (interrupt-process seems not to work)? (Markus Triska)
8848 ;; * Avoid the local shell entirely for starting remote processes. If
8849 ;; so, I think even a signal, when delivered directly to the local
8850 ;; SSH instance, would correctly be propagated to the remote process
8851 ;; automatically; possibly SSH would have to be started with
8852 ;; "-t". (Markus Triska)
8853 ;; * It makes me wonder if tramp couldn't fall back to ssh when scp
8854 ;; isn't on the remote host. (Mark A. Hershberger)
8855 ;; * Use lsh instead of ssh. (Alfred M. Szmidt)
8856 ;; * Implement a general server-local-variable mechanism, as there are
8857 ;; probably other variables that need different values for different
8858 ;; servers too. The user could then configure a variable (such as
8859 ;; tramp-server-local-variable-alist) to define any such variables
8860 ;; that they need to, which would then be let bound as appropriate
8861 ;; in tramp functions. (Jason Rumney)
8862 ;; * Optimize out-of-band copying, when both methods are scp-like (not
8863 ;; rsync).
8864 ;; * Keep a second connection open for out-of-band methods like scp or
8865 ;; rsync.
8866 ;; * Support ptys in `tramp-handle-start-file-process'. (Bug#4604)
8867 ;; * IMHO, it's a drawback that currently Tramp doesn't support
8868 ;; Unicode in Dired file names by default. Is it possible to
8869 ;; improve Tramp to set LC_ALL to "C" only for commands where Tramp
8870 ;; expects English? Or just to set LC_MESSAGES to "C" if Tramp
8871 ;; expects only English messages? (Juri Linkov)
8872 ;; * Make shadowfile.el grok Tramp filenames. (Bug#4526, Bug#4846)
8873 ;; * Do not handle files with drive letter as remote. (Bug#5447)
8874 ;; * Load Tramp subpackages only when needed. (Bug#1529, Bug#5448, Bug#5705)
8875 ;; * Try telnet+curl as new method. It might be useful for busybox,
8876 ;; without built-in uuencode/uudecode.
8877 ;; * Let `shell-dynamic-complete-*' and `comint-dynamic-complete' work
8878 ;; on remote hosts.
8879 ;; * Use secrets.el for password handling.
8880 ;; * Load ~/.emacs_SHELLNAME on the remote host for `shell'.
8881
8882 ;; Functions for file-name-handler-alist:
8883 ;; diff-latest-backup-file -- in diff.el
8884
8885 ;; arch-tag: 3a21a994-182b-48fa-b0cd-c1d9fede424a
8886 ;;; tramp.el ends here
8887
8888 ;; Local Variables:
8889 ;; mode: Emacs-Lisp
8890 ;; coding: utf-8
8891 ;; End: